diff --git a/core/session/timeline/agents.go b/core/session/timeline/agents.go index cf80fc8b9..7374d1354 100644 --- a/core/session/timeline/agents.go +++ b/core/session/timeline/agents.go @@ -99,12 +99,19 @@ func buildAgents(t *Timeline, events []model.SessionEvent) { agentTools := map[string]bool{} seenReturn := map[string]bool{} stopped := map[string]time.Time{} // agent → its latest : its run ended - var askedAt []time.Time // per t.Tools entry: when the request that asked for it started + // A run that ends with SubagentHandback returns twice: its at once, and a + // that Claude Code sends later, often with the parent's next request. + // handedBack: the agent's latest response called SubagentHandback. delivered: that report + // then reached a parent. A later request of the agent clears both: it runs again. + handedBack, delivered := map[string]bool{}, map[string]bool{} + var askedAt []time.Time // per t.Tools entry: when the request that asked for it started var spawns []*spawn var calBytes, calTokens int64 // sums over the turns that set t.bytesPerToken for _, c := range calls { inf := c.req.Inference + delete(handedBack, c.agent) + delete(delivered, c.agent) st, known := agents[c.agent] if !known { st = &agentState{first: c.req.At} @@ -168,7 +175,15 @@ func buildAgents(t *Timeline, events []model.SessionEvent) { continue // a fork inherits its parent's history, deliveries included } seenReturn[key] = true - t.Returns = append(t.Returns, Return{Agent: from, Into: c.agent, At: c.req.At, Seq: c.req.Seq}) + // The notification after a delivered hand-back is not a return: it came a median + // 22s and up to hours later, and riding on the user's next prompt it labelled the + // user's time as waiting on the subagent. + if re == agentMessageRe && handedBack[from] { + delivered[from] = true + } + if re == agentMessageRe || !delivered[from] { + t.Returns = append(t.Returns, Return{Agent: from, Into: c.agent, At: c.req.At, Seq: c.req.Seq}) + } // Only a notification says the run ended: an can arrive mid-run. if re == taskIDRe && c.req.At.After(stopped[from]) { stopped[from] = c.req.At @@ -219,6 +234,9 @@ func buildAgents(t *Timeline, events []model.SessionEvent) { tool := Tool{Agent: c.agent, ID: tc.ID, Name: tc.Name, Start: end, Preview: preview(tc.Name, a), Seq: c.resp.Seq} t.Tools = append(t.Tools, tool) askedAt = append(askedAt, c.req.At) + if tc.Name == "SubagentHandback" { + handedBack[c.agent] = true + } if tc.Name == "Agent" || tc.Name == "Task" { agentTools[tc.ID] = true spawns = append(spawns, &spawn{by: c.agent, turnAt: c.req.At, desc: a.Description, typ: a.SubagentType}) diff --git a/core/session/timeline/timeline_test.go b/core/session/timeline/timeline_test.go index 1b7a42002..e586b466c 100644 --- a/core/session/timeline/timeline_test.go +++ b/core/session/timeline/timeline_test.go @@ -584,6 +584,50 @@ func TestReturnedAtIsTheLastReturn(t *testing.T) { } } +// A run that ends with SubagentHandback returns at its . Claude Code sends the +// later, here with the user's next prompt, and as a return it made the +// user's 3 minutes read as waiting on the subagent (session 27737d97, seq 611). +func TestNotificationAfterADeliveredHandBackIsNotAReturn(t *testing.T) { + note := user("\nc3\ncompleted") + var r rec + r.turn("", 0, 1, []pipeline.InferenceMessage{user("go")}, nil, "tool_use", pipeline.InferenceToolCall{ID: "s1", Name: "Agent"}) + r.turn("c3", 1, 1, []pipeline.InferenceMessage{user("p")}, nil, "tool_use", pipeline.InferenceToolCall{ID: "hb", Name: "SubagentHandback"}) + h := []pipeline.InferenceMessage{user("go"), user(` report`)} + r.turn("", 5, 1, h, nil, "end_turn") + r.turn("", 200, 1, append(h, user("next question"), note), nil, "end_turn") + tl := Build("s", r.events, time.Time{}) + if len(tl.Returns) != 1 || !tl.Returns[0].At.Equal(at(5)) { + t.Errorf("returns = %+v, want one at 5s", tl.Returns) + } + if a := findAgent(t, tl, "c3"); a.ReturnedAt == nil || !a.ReturnedAt.Equal(at(5)) { + t.Errorf("returnedAt = %v, want 5s", a.ReturnedAt) + } + + // Without a hand-back call, an is a message sent mid-run, and the + // notification is still the return. + r = rec{} + r.turn("", 0, 1, []pipeline.InferenceMessage{user("go")}, nil, "tool_use", pipeline.InferenceToolCall{ID: "s1", Name: "Agent"}) + r.turn("c3", 1, 1, []pipeline.InferenceMessage{user("p")}, nil, "end_turn") + r.turn("", 5, 1, h, nil, "end_turn") + r.turn("", 200, 1, append(h, note), nil, "end_turn") + if a := findAgent(t, Build("s", r.events, time.Time{}), "c3"); a.ReturnedAt == nil || !a.ReturnedAt.Equal(at(200)) { + t.Errorf("no hand-back: returnedAt = %v, want 200s", a.ReturnedAt) + } + + // A resumed run that hands back again returns again, at its second . + r = rec{} + r.turn("", 0, 1, []pipeline.InferenceMessage{user("go")}, nil, "tool_use", pipeline.InferenceToolCall{ID: "s1", Name: "Agent"}) + r.turn("c3", 1, 1, []pipeline.InferenceMessage{user("p")}, nil, "tool_use", pipeline.InferenceToolCall{ID: "hb", Name: "SubagentHandback"}) + r.turn("", 5, 1, h, nil, "end_turn") + r.turn("c3", 10, 1, []pipeline.InferenceMessage{user("p"), user("more")}, nil, "tool_use", pipeline.InferenceToolCall{ID: "hb2", Name: "SubagentHandback"}) + h2 := append(h, note, user(` second report`)) + r.turn("", 20, 1, h2, nil, "end_turn") + r.turn("", 300, 1, append(h2, user("thanks"), note), nil, "end_turn") + if a := findAgent(t, Build("s", r.events, time.Time{}), "c3"); a.ReturnedAt == nil || !a.ReturnedAt.Equal(at(20)) { + t.Errorf("resumed: returnedAt = %v, want 20s", a.ReturnedAt) + } +} + // Clip moves spans within their slices; a lost span's End must not point into one. func TestLostSpansSurviveClip(t *testing.T) { var r rec diff --git a/docs/session-api.md b/docs/session-api.md index d6c849846..b4b873726 100644 --- a/docs/session-api.md +++ b/docs/session-api.md @@ -281,7 +281,9 @@ has no spawn link: nothing on the wire names the call that started it. proxy captured agent ids are one `subagents` agent. - A **return** is a subagent's result reaching another agent: `` or `` in a message that agent - had not sent before. + had not sent before. A run that ends with `SubagentHandback` returns at its + ``. The `` that Claude Code sends after it + is not a return: it comes later, often with the user's next prompt. - 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, or to the `TaskStop` result that