diff --git a/core/session/timeline/agents.go b/core/session/timeline/agents.go index 2fdc62f0b..cf80fc8b9 100644 --- a/core/session/timeline/agents.go +++ b/core/session/timeline/agents.go @@ -35,6 +35,9 @@ var ( // backgroundRe is Claude Code's result for a shell command it started in the background; // the command's later names the same id. backgroundRe = regexp.MustCompile(`^Command running in background with ID: ([A-Za-z0-9_-]+)`) + // stoppedRe is TaskStop's result. A stopped job gets no , so without it + // the job stays open and covers every later wait of its agent. + stoppedRe = regexp.MustCompile(`^\{"message":"Successfully stopped (?:task: |background command )([A-Za-z0-9_-]+)`) ) // failedFinish are the finish reasons of a reply that did not complete: none at all, a refusal @@ -126,6 +129,12 @@ func buildAgents(t *Timeline, events []model.SessionEvent) { t.Jobs = append(t.Jobs, Job{Agent: c.agent, ID: m[1], ToolUseID: r.ToolUseID, Start: c.req.At, Seq: c.req.Seq}) } } + if m := stoppedRe.FindStringSubmatch(r.Content); m != nil { + if j, ok := jobs[m[1]]; ok && t.Jobs[j].End == nil { + at := c.req.At + t.Jobs[j].End = &at + } + } } } diff --git a/core/session/timeline/timeline.go b/core/session/timeline/timeline.go index 2375bfedf..f1b8d6f7b 100644 --- a/core/session/timeline/timeline.go +++ b/core/session/timeline/timeline.go @@ -138,7 +138,8 @@ type Tool struct { } // Job is a background shell command: from the tool result that acknowledged it ("Command -// running in background with ID: …") to the naming that id. Its tool call +// running in background with ID: …") to the naming that id, or to the +// TaskStop result that stopped it. Its tool call // ends at the acknowledgement, so without this the agent looks idle while the job runs. type Job struct { Agent string `json:"agent"` diff --git a/core/session/timeline/timeline_test.go b/core/session/timeline/timeline_test.go index d1384fd0c..1b7a42002 100644 --- a/core/session/timeline/timeline_test.go +++ b/core/session/timeline/timeline_test.go @@ -791,6 +791,26 @@ func TestBackgroundJobRunsUntilItsNotification(t *testing.T) { } } +// A job that TaskStop stops gets no notification: it ends at the TaskStop result. A shell on +// another host has its own result text. +func TestBackgroundJobEndsAtTaskStop(t *testing.T) { + for _, stopped := range []string{ + `{"message":"Successfully stopped task: bk42 (llama-server --port 18090)","task_id":"bk42"}`, + `{"message":"Successfully stopped background command bk42 on devbox","task_id":"bk42","task_type":"local_bash"}`, + } { + var r rec + h := []pipeline.InferenceMessage{user("go")} + r.turn("", 0, 1, h, nil, "tool_use", pipeline.InferenceToolCall{ID: "t1", Name: "Bash"}) + r.turn("", 2, 1, h, []pipeline.InferenceToolResult{result("t1", "Command running in background with ID: bk42.")}, "tool_use", pipeline.InferenceToolCall{ID: "t2", Name: "TaskStop"}) + r.turn("", 26, 1, h, []pipeline.InferenceToolResult{result("t2", stopped)}, "end_turn") + r.turn("", 600, 1, h, nil, "end_turn") + tl := Build("s", r.events, at(900)) + if len(tl.Jobs) != 1 || tl.Jobs[0].End == nil || !tl.Jobs[0].End.Equal(at(26)) { + t.Fatalf("%s: jobs = %+v, want bk42 to end at 26s", stopped, tl.Jobs) + } + } +} + // A result that only quotes the acknowledgement, a file or diff read back, starts no job. func TestQuotedBackgroundMarkerStartsNoJob(t *testing.T) { var r rec diff --git a/docs/session-api.md b/docs/session-api.md index ce0e28402..d6c849846 100644 --- a/docs/session-api.md +++ b/docs/session-api.md @@ -284,9 +284,10 @@ has no spawn link: nothing on the wire names the call that started it. had not sent before. - A **job** is a background shell command, from the tool result that acknowledged it (`Command running in background with ID: …`) to the first - `` naming that id. Its tool call ends at the + `` naming that id, or to the `TaskStop` result that + stopped it (a stopped job gets no notification). Its tool call ends at the acknowledgement, so the job is what shows it still running. A job with no - `end` is one no notification named: still running, or never reported, as a + `end` is one no notification or `TaskStop` named: still running, or never reported, as a server left running when its agent exited is. A job whose agent was cut off ends where its tools do: at the first failed dial to a model host after the agent's last call. Absent when the session started none.