Repository navigation
fix: Return a handed-back subagent at its agent-message #1935
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 <task-notification>: 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 <agent-message> at once, and a | ||
| // <task-notification> 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 <agent-message> 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 | ||
|
Comment on lines
+237
to
+238
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Record when the hand-back happened, and count delivery only after it.
Record the hand-back response time when
|
||
| } | ||
| 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}) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (non-blocking, pre-existing): the late notification is now ignored as a return, but it still advances
stopped[from]. If the parent resumes the subagent withSendMessagebefore Claude Code sends the late notification for the first run, that notification lands after the resumed run's requests started. Thestopped[tool.Agent].After(askedAt[i])check then closes the resumed run's in-flight tools as lost. I reproduced this: hand-back at 1s, delivery at 5s, resumedBashcall at 10s, late notification at 15s. TheBashtool comes outLost=true, onmainand on this head alike, so this PR doesn't introduce it. Since the PR's own data shows these notifications arrive a median 22s late (and up to hours), the fix is probably to track each notification against the run it belongs to. (Skipping thestoppedupdate whendelivered[from]is clear would not work: a resumed request clearsdelivered, so a cleared flag looks the same as one never set.) A follow-up is fine.