Keep native Claude background tasks alive across detached sessions

This commit is contained in:
2026-11-07 16:22:16 +00:00
parent fd82eaa576
commit 1d8076ebdc
3 changed files with 339 additions and 23 deletions
+43 -23
View File
@@ -22,7 +22,7 @@ import (
// Defaults for a host started without explicit limits.
const (
DefaultIdle = 20 * time.Minute
DefaultMaxDetached = 24 * time.Hour
DefaultMaxDetached = time.Duration(0)
DefaultSpoolLimit = 64 << 20
maxLineBytes = 32 << 20
stderrRetained = 8 << 10
@@ -85,24 +85,26 @@ type host struct {
trans []transition
busy bool
// pending maps unanswered CLI control request ids to their offsets.
pending map[string]int64
initID string
init json.RawMessage
modelReqs map[string]string
modeReqs map[string]string
model string
mode string
exited bool
exitErr string
exitedAt time.Time
stderr strings.Builder
changed chan struct{}
active *client
detached time.Time
activity time.Time
stopping bool
stopped chan struct{}
claudeEnd chan struct{}
tasks map[string]string // running native background tools, independent of foreground busy
taskOrigins map[string]Task // native identity/metadata retained independently of spool compaction
pending map[string]int64
initID string
init json.RawMessage
modelReqs map[string]string
modeReqs map[string]string
model string
mode string
exited bool
exitErr string
exitedAt time.Time
stderr strings.Builder
changed chan struct{}
active *client
detached time.Time
activity time.Time
stopping bool
stopped chan struct{}
claudeEnd chan struct{}
}
// Main runs a host. args are the host flags, then "--", then the Claude Code
@@ -113,7 +115,7 @@ func Main(args []string) int {
dir := flags.String("dir", "", "private host directory (registry, socket, spool)")
id := flags.String("id", "", "Claude session id")
idle := flags.Duration("idle", DefaultIdle, "release an idle CLI after this long with no server attached")
maxDetached := flags.Duration("max-detached", DefaultMaxDetached, "release the CLI after this long with no server attached, even when busy")
maxDetached := flags.Duration("max-detached", DefaultMaxDetached, "optional detached lifetime limit (0 keeps live sessions until explicitly closed)")
spoolLimit := flags.Int64("spool-limit", DefaultSpoolLimit, "bytes of acknowledged output kept in the spool")
if err := flags.Parse(args); err != nil {
return 2
@@ -341,6 +343,7 @@ func (h *host) appendLine(line []byte) {
h.end += int64(len(data))
h.lines = append(h.lines, lineRef{start: start, end: h.end})
h.activity = time.Now()
h.trackTasksLocked(line)
switch env.Type {
case "control_request":
if env.RequestID != "" {
@@ -425,10 +428,27 @@ func (h *host) statusLocked() *Status {
}
sort.Slice(pending, func(i, j int) bool { return pending[i].Offset < pending[j].Offset })
cwd, _ := os.Getwd()
tasks := make([]Task, 0, len(h.tasks))
for key, taskID := range h.tasks {
task := h.taskOrigins[key]
if task.ToolUseID == "" && !strings.HasPrefix(key, "task:") {
task.ToolUseID = key
}
if task.TaskID == "" {
task.TaskID = taskID
}
tasks = append(tasks, task)
}
sort.Slice(tasks, func(i, j int) bool {
if tasks[i].ToolUseID == tasks[j].ToolUseID {
return tasks[i].TaskID < tasks[j].TaskID
}
return tasks[i].ToolUseID < tasks[j].ToolUseID
})
status := &Status{
Version: Version, SessionID: h.id, HostPID: os.Getpid(),
Base: h.base, End: h.end, Acked: h.acked, Busy: h.busy, BusyAtAcked: h.busyAtLocked(h.acked),
Init: h.init, Model: h.model, Mode: h.mode, Pending: pending,
Init: h.init, Model: h.model, Mode: h.mode, Pending: pending, Tasks: &tasks,
Exited: h.exited, ExitErr: h.exitErr, Stderr: h.stderr.String(), Directory: cwd,
}
if h.cmd != nil && h.cmd.Process != nil {
@@ -688,8 +708,8 @@ func (h *host) watchIdle() {
now := time.Now()
h.mu.Lock()
attached := h.active != nil
idle := !h.busy && len(h.pending) == 0 && now.Sub(h.detached) > h.idle && now.Sub(h.activity) > h.idle
tooLong := now.Sub(h.detached) > h.maxDetached
idle := !h.busy && len(h.pending) == 0 && len(h.tasks) == 0 && now.Sub(h.detached) > h.idle && now.Sub(h.activity) > h.idle
tooLong := h.maxDetached > 0 && now.Sub(h.detached) > h.maxDetached
exitedLong := h.exited && now.Sub(h.exitedAt) > h.idle
h.mu.Unlock()
if attached {
+198
View File
@@ -0,0 +1,198 @@
package claudehost
import (
"encoding/json"
"regexp"
"strings"
"unicode/utf8"
)
// The host owns the real CLI even with no Pipelines connection. Foreground
// result messages do not end background tools; only their native completion
// notifications do. Keep this lifecycle state independently of spool acks.
func (h *host) trackTasksLocked(line []byte) {
var event struct {
Type string `json:"type"`
Subtype string `json:"subtype"`
ToolUseID string `json:"tool_use_id"`
TaskID string `json:"task_id"`
Status string `json:"status"`
Description string `json:"description"`
TaskType string `json:"task_type"`
Timestamp string `json:"timestamp"`
Message struct {
Content json.RawMessage `json:"content"`
} `json:"message"`
}
if json.Unmarshal(line, &event) != nil {
return
}
if h.tasks == nil {
h.tasks = map[string]string{}
}
if h.taskOrigins == nil {
h.taskOrigins = map[string]Task{}
}
switch event.Subtype {
case "task_started", "task_progress":
id := event.ToolUseID
if id == "" {
for key, task := range h.tasks {
if event.TaskID != "" && task == event.TaskID {
id = key
break
}
}
if id == "" {
id = "task:" + event.TaskID
}
}
if id != "task:" {
if event.ToolUseID != "" && event.TaskID != "" {
fallback := "task:" + event.TaskID
if prior, exists := h.taskOrigins[fallback]; exists {
if _, recorded := h.taskOrigins[id]; !recorded {
h.taskOrigins[id] = prior
}
delete(h.tasks, fallback)
delete(h.taskOrigins, fallback)
}
}
h.tasks[id] = event.TaskID
origin := h.taskOrigins[id]
if event.ToolUseID != "" {
origin.ToolUseID = event.ToolUseID
}
if event.TaskID != "" {
origin.TaskID = event.TaskID
}
if event.Description != "" {
origin.Description = taskText(event.Description, 2048)
}
if event.TaskType != "" {
origin.TaskType = taskText(event.TaskType, 128)
}
if origin.Timestamp == "" {
origin.Timestamp = event.Timestamp
}
h.taskOrigins[id] = origin
}
case "task_notification":
if event.Status == "completed" || event.Status == "failed" || event.Status == "stopped" {
for key, task := range h.tasks {
if key == event.ToolUseID || (event.TaskID != "" && (task == event.TaskID || key == "task:"+event.TaskID)) {
delete(h.tasks, key)
delete(h.taskOrigins, key)
}
}
}
}
if event.Type == "result" {
for key := range h.taskOrigins {
if _, running := h.tasks[key]; !running {
delete(h.taskOrigins, key)
}
}
}
var blocks []taskBlock
if json.Unmarshal(event.Message.Content, &blocks) != nil {
var text string
if json.Unmarshal(event.Message.Content, &text) != nil {
return
}
blocks = []taskBlock{{Type: "text", Text: text}}
}
finishText := func(text string) {
for _, match := range completedTaskNotice.FindAllStringSubmatch(text, -1) {
key := match[1]
taskID := h.tasks[key]
delete(h.tasks, key)
delete(h.taskOrigins, key)
if taskID != "" {
delete(h.tasks, "task:"+taskID)
delete(h.taskOrigins, "task:"+taskID)
}
}
}
for _, block := range blocks {
switch block.Type {
case "tool_use":
if block.ID != "" && (block.Name == "Agent" || block.Name == "Task" || block.Name == "Bash") {
if block.Input.Background {
if _, exists := h.tasks[block.ID]; !exists {
h.tasks[block.ID] = ""
}
}
origin := h.taskOrigins[block.ID]
origin.ToolUseID, origin.Name = block.ID, block.Name
origin.Command = taskText(block.Input.Command, 64<<10)
if block.Input.Description != "" {
origin.Description = taskText(block.Input.Description, 2048)
}
if origin.TaskType == "" {
if block.Name == "Bash" {
origin.TaskType = "shell"
} else {
origin.TaskType = "agent"
}
}
if origin.Timestamp == "" {
origin.Timestamp = event.Timestamp
}
h.taskOrigins[block.ID] = origin
}
case "tool_result":
var text string
if json.Unmarshal(block.Content, &text) != nil {
var parts []struct {
Text string `json:"text"`
}
_ = json.Unmarshal(block.Content, &parts)
for _, part := range parts {
text += part.Text + "\n"
}
}
if strings.Contains(text, "running in background with ID") || strings.HasPrefix(strings.TrimSpace(text), "Async agent launched") {
if block.ToolUseID != "" {
if _, exists := h.tasks[block.ToolUseID]; !exists {
h.tasks[block.ToolUseID] = ""
}
}
} else {
delete(h.tasks, block.ToolUseID)
delete(h.taskOrigins, block.ToolUseID)
}
finishText(text)
case "text":
finishText(block.Text)
}
}
}
var completedTaskNotice = regexp.MustCompile(`(?s)<task-notification>.*?<tool-use-id>\s*([^<\s]+)\s*</tool-use-id>.*?</task-notification>`)
// Native stream-json message content can be an array or a plain notification.
type taskBlock struct {
Type string `json:"type"`
ID string `json:"id"`
ToolUseID string `json:"tool_use_id"`
Name string `json:"name"`
Input struct {
Background bool `json:"run_in_background"`
Command string `json:"command"`
Description string `json:"description"`
} `json:"input"`
Text string `json:"text"`
Content json.RawMessage `json:"content"`
}
func taskText(s string, limit int) string {
if len(s) <= limit {
return s
}
for limit > 0 && !utf8.ValidString(s[:limit]) {
limit--
}
return s[:limit]
}
+98
View File
@@ -0,0 +1,98 @@
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 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")
}
}