64e51ee751
The host counted any tool_result containing "running in background with ID" as a launch acknowledgement, so a subagent's foreground `sed` of fake-claude.py (or translate.go) became a background task that never ends. A subagent's background command whose completion notification preceded its launch result was also revived. Both stayed "Running" in the chat for days. - claudehost: only a Bash/Agent/Task call's own first-line acknowledgement starts a task; finished ids are never revived by late acks/progress; background_tasks_changed (the CLI's running set) ends unlisted tasks; tool output no longer counts as a task notification. - claude: same strict acknowledgement; background_tasks_changed settles tracked tasks; on reattach, host tasks without a native id are checked against the main and subagent transcripts, so phantoms from hosts still running the old build disappear. - Fake CLI "subagent" turn and regression tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
154 lines
8.7 KiB
Go
154 lines
8.7 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")
|
|
}
|
|
}
|
|
|
|
// A subagent's foreground Bash that prints source quoting the background
|
|
// acknowledgement (sed of fake-claude.py, seen live) is not a background task.
|
|
func TestForegroundOutputQuotingLaunchIsNotATask(t *testing.T) {
|
|
h := &host{}
|
|
h.trackTasksLocked([]byte(`{"type":"assistant","parent_tool_use_id":"agent","message":{"content":[{"type":"tool_use","id":"sed","name":"Bash","input":{"command":"sed -n 1,230p internal/claude/testdata/fake-claude.py"}}]}}`))
|
|
h.trackTasksLocked([]byte(`{"type":"user","parent_tool_use_id":"agent","message":{"content":[{"type":"tool_result","tool_use_id":"sed","content":"#!/usr/bin/env python3\n \"content\": \"Command running in background with ID: b1.\"\n<task-notification><tool-use-id>other</tool-use-id></task-notification>"}]}}`))
|
|
// A result for no known Bash/Agent call never starts a task either.
|
|
h.trackTasksLocked([]byte(`{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"unknown","content":"Command running in background with ID: b2."}]}}`))
|
|
if status := h.statusLocked(); len(*status.Tasks) != 0 {
|
|
t.Fatalf("foreground output registered a task: %+v", *status.Tasks)
|
|
}
|
|
}
|
|
|
|
// The CLI may report a subagent's background command complete before the
|
|
// tool_result acknowledging its launch; the late acknowledgement and progress
|
|
// must not revive it (seen live: Xvfb launched by a subagent).
|
|
func TestCompletionBeforeLaunchAcknowledgementStaysFinished(t *testing.T) {
|
|
h := &host{}
|
|
h.trackTasksLocked([]byte(`{"type":"assistant","parent_tool_use_id":"agent","message":{"content":[{"type":"tool_use","id":"xvfb","name":"Bash","input":{"command":"Xvfb :99 &","run_in_background":true}}]}}`))
|
|
h.trackTasksLocked([]byte(`{"type":"system","subtype":"task_started","task_id":"bz","tool_use_id":"xvfb","owned_by_subagent":true,"is_backgrounded":true}`))
|
|
h.trackTasksLocked([]byte(`{"type":"system","subtype":"task_notification","task_id":"bz","tool_use_id":"xvfb","status":"completed"}`))
|
|
h.trackTasksLocked([]byte(`{"type":"user","parent_tool_use_id":"agent","message":{"content":[{"type":"tool_result","tool_use_id":"xvfb","content":"Command running in background with ID: bz. Output is being written to: /tmp/bz.output"}]}}`))
|
|
h.trackTasksLocked([]byte(`{"type":"system","subtype":"task_progress","task_id":"bz","tool_use_id":"xvfb"}`))
|
|
if status := h.statusLocked(); len(*status.Tasks) != 0 {
|
|
t.Fatalf("finished task revived: %+v", *status.Tasks)
|
|
}
|
|
}
|
|
|
|
// background_tasks_changed is the CLI's full running set; a task it omits ended.
|
|
func TestBackgroundTasksChangedDropsUnlistedTasks(t *testing.T) {
|
|
h := &host{}
|
|
for _, id := range []string{"a", "b"} {
|
|
h.trackTasksLocked([]byte(`{"type":"assistant","message":{"content":[{"type":"tool_use","id":"tool-` + id + `","name":"Bash","input":{"command":"sleep 600","run_in_background":true}}]}}`))
|
|
h.trackTasksLocked([]byte(`{"type":"system","subtype":"task_started","task_id":"native-` + id + `","tool_use_id":"tool-` + id + `"}`))
|
|
}
|
|
h.trackTasksLocked([]byte(`{"type":"system","subtype":"background_tasks_changed","tasks":[{"task_id":"native-b","task_type":"local_bash"}]}`))
|
|
status := h.statusLocked()
|
|
if len(*status.Tasks) != 1 || (*status.Tasks)[0].ToolUseID != "tool-b" {
|
|
t.Fatalf("running set not reconciled: %+v", *status.Tasks)
|
|
}
|
|
}
|