Keep native Claude background tasks alive across detached sessions
This commit is contained in:
+43
-23
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user