112 lines
5.5 KiB
Go
112 lines
5.5 KiB
Go
package claudehost
|
|
|
|
import (
|
|
"os"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestNativeTaskSnapshotOutlivesForegroundAndSpool(t *testing.T) {
|
|
h := &host{}
|
|
h.trackTasksLocked([]byte(`{"type":"assistant","timestamp":"2026-11-07T12:00:00Z","message":{"content":[{"type":"tool_use","id":"tool-bg","name":"Bash","input":{"command":"sleep 600","run_in_background":true}}]}}`))
|
|
h.trackTasksLocked([]byte(`{"type":"system","subtype":"task_started","tool_use_id":"tool-bg","task_id":"native-bg","description":"Background build","task_type":"local_bash"}`))
|
|
h.trackTasksLocked([]byte(`{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"tool-bg","content":"Command running in background with ID: native-bg"}]}}`))
|
|
h.trackTasksLocked([]byte(`{"type":"result"}`))
|
|
h.trackTasksLocked([]byte(`{"type":"system","subtype":"task_progress","task_id":"native-bg"}`))
|
|
// Acknowledged stream data may be compacted; live membership must not be.
|
|
h.base, h.end, h.acked = 10000, 10000, 10000
|
|
h.lines = nil
|
|
status := h.statusLocked()
|
|
if status.Tasks == nil || len(*status.Tasks) != 1 {
|
|
t.Fatalf("native snapshot: %+v", status.Tasks)
|
|
}
|
|
task := (*status.Tasks)[0]
|
|
if task.ToolUseID != "tool-bg" || task.TaskID != "native-bg" || task.Command != "sleep 600" || task.Description != "Background build" || task.Timestamp != "2026-11-07T12:00:00Z" {
|
|
t.Fatalf("identity/metadata lost: %+v", task)
|
|
}
|
|
h.trackTasksLocked([]byte(`{"type":"system","subtype":"task_notification","task_id":"native-bg","status":"completed"}`))
|
|
if status = h.statusLocked(); status.Tasks == nil || len(*status.Tasks) != 0 {
|
|
t.Fatalf("completion not authoritative: %+v", status.Tasks)
|
|
}
|
|
}
|
|
|
|
func TestNativeTaskProgressWithoutIDRetainsCompletionAssociation(t *testing.T) {
|
|
h := &host{}
|
|
h.trackTasksLocked([]byte(`{"type":"system","subtype":"task_started","tool_use_id":"tool","task_id":"native"}`))
|
|
h.trackTasksLocked([]byte(`{"type":"system","subtype":"task_progress","tool_use_id":"tool"}`))
|
|
if h.tasks["tool"] != "native" {
|
|
t.Fatal("progress erased native identity")
|
|
}
|
|
h.trackTasksLocked([]byte(`{"type":"system","subtype":"task_notification","task_id":"native","status":"completed"}`))
|
|
if len(h.tasks) != 0 {
|
|
t.Fatal("native completion did not clear running membership")
|
|
}
|
|
}
|
|
|
|
func TestNativeTaskAliasesAndTextCompletion(t *testing.T) {
|
|
h := &host{}
|
|
h.trackTasksLocked([]byte(`{"type":"system","subtype":"task_progress","task_id":"native-agent"}`))
|
|
h.trackTasksLocked([]byte(`{"type":"system","subtype":"task_started","task_id":"native-agent","tool_use_id":"tool-agent","description":"Review"}`))
|
|
h.trackTasksLocked([]byte(`{"type":"assistant","message":{"content":[{"type":"tool_use","id":"tool-agent","name":"Agent","input":{"description":"Review"}}]}}`))
|
|
h.trackTasksLocked([]byte(`{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"tool-agent","content":[{"type":"text","text":"Async agent launched successfully."}]}]}}`))
|
|
if len(h.tasks) != 1 || h.tasks["tool-agent"] != "native-agent" {
|
|
t.Fatalf("alias lost or duplicated: %+v", h.tasks)
|
|
}
|
|
h.trackTasksLocked([]byte(`{"type":"user","message":{"content":"<task-notification><tool-use-id>tool-agent</tool-use-id><status>completed</status></task-notification>"}}`))
|
|
if len(h.tasks) != 0 || len(h.taskOrigins) != 0 {
|
|
t.Fatalf("text completion retained native work: %+v %+v", h.tasks, h.taskOrigins)
|
|
}
|
|
h.trackTasksLocked([]byte(`{"type":"assistant","message":{"content":[{"type":"tool_use","id":"foreground","name":"Agent","input":{"description":"Sync review"}}]}}`))
|
|
h.trackTasksLocked([]byte(`{"type":"result"}`))
|
|
if len(h.tasks) != 0 || len(h.taskOrigins) != 0 {
|
|
t.Fatal("foreground task leaked into detached liveness")
|
|
}
|
|
}
|
|
|
|
func TestDetachedBackgroundCLIIsNotReapedAsIdle(t *testing.T) {
|
|
dir, err := os.MkdirTemp("/tmp", "phbg")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer os.RemoveAll(dir)
|
|
now := time.Now()
|
|
h := &host{dir: dir, id: "background-survival", paths: PathsFor(dir, "background-survival"), idle: 50 * time.Millisecond, maxDetached: DefaultMaxDetached, spoolLimit: 64 << 20, pending: map[string]int64{}, modelReqs: map[string]string{}, modeReqs: map[string]string{}, changed: make(chan struct{}), stopped: make(chan struct{}), claudeEnd: make(chan struct{}), activity: now, detached: now}
|
|
// This is an actual CLI process owned by a separate host lifecycle, not a
|
|
// synthetic task row. It remains quiet after finishing its foreground turn.
|
|
script := `printf '%s\n' '{"type":"assistant","message":{"content":[{"type":"tool_use","id":"tool-bg","name":"Bash","input":{"command":"quiet work","run_in_background":true}}]}}' '{"type":"result"}'; sleep 30`
|
|
if err := h.start([]string{"/bin/sh", "-c", script}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer h.shutdown("test cleanup")
|
|
deadline := time.Now().Add(time.Second)
|
|
for time.Now().Before(deadline) {
|
|
h.mu.Lock()
|
|
ready := len(h.tasks) == 1 && !h.busy
|
|
h.mu.Unlock()
|
|
if ready {
|
|
break
|
|
}
|
|
time.Sleep(time.Millisecond)
|
|
}
|
|
go h.watchIdle()
|
|
time.Sleep(180 * time.Millisecond)
|
|
select {
|
|
case <-h.stopped:
|
|
t.Fatal("detached background-only CLI was killed by idle expiry")
|
|
default:
|
|
}
|
|
if syscall.Kill(h.cmd.Process.Pid, 0) != nil {
|
|
t.Fatal("native CLI did not survive detached idle window")
|
|
}
|
|
t.Logf("native CLI PID=%d survived detached background-only idle window", h.cmd.Process.Pid)
|
|
h.mu.Lock()
|
|
h.trackTasksLocked([]byte(`{"type":"system","subtype":"task_notification","tool_use_id":"tool-bg","status":"completed"}`))
|
|
h.mu.Unlock()
|
|
select {
|
|
case <-h.stopped:
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("completed idle CLI was not released")
|
|
}
|
|
}
|