Repository navigation
Description
Problem
A background sub-agent's span hangs from the span that was current when it started. When
the agent ends, only the user turn that owns it is extended to cover it, not that parent
span. So the child ends long after its parent:
- An agent launched while the model handles an earlier agent's result hangs from
"Continuation: subagent result", which ends at its ownStop. That's on import and live. - On import, a workflow agent hangs from the
Workflowtool span. That span ends as soon
asWorkflowreturnsasync_launched.
The plugin spec's TRACE-13 says background work should "remain open until native
completion, cancellation, or defensive flush".
Impact
On one engineer's machine:
- 120 sub-agents in 28 sessions run past a continuation, by up to 62 minutes;
- all 29 workflow agents run past their
Workflowspan, by up to 93 minutes.
Nested background agents show the same pattern. Trace views that size or order spans by
their parent show children far outside it.
Steps to reproduce
In a git repository holding src/a.txt and src/b.txt (Claude Code 2.1.325, bt 0.33.0).
Continuation, live (also on import of a plain run of the same prompt):
bt trace run --project my-coding-agent --no-input claude -- -p --model haiku --allowedTools "Agent Bash" --output-format json "Step 1: launch one general-purpose sub-agent with the Agent tool and run_in_background set to true; its prompt is: reply with the words A done. Then end your turn without waiting. Step 2: when you are notified that this first agent finished, launch a second general-purpose sub-agent with run_in_background true whose prompt is: run the Bash command sleep 20, then reply with the words B done. End your turn without waiting. Step 3: when the second agent finishes, report both results in one line."Turn 1 18.206 – 45.278
Continuation: subagent result 20.699 – 22.742
Agent (tool) 21.679 – 21.752
subagent: general-purpose 21.816 – 43.496 <- 20.8 s past its parent
The import of a plain run stored the same shape, 20.5 s past.
Workflow, import:
claude -p --model haiku --allowedTools "Workflow Glob Read Agent Bash" --output-format json "ultracode: use a workflow with two parallel agents: one lists the files in src/ with Glob, the other runs the Bash command sleep 15 and then reads src/b.txt. Report what each found."
bt trace import claude <session-id> --destination project_logs:<project-id>Workflow (tool) 20.502 – 20.509
subagent: workflow-subagent 20.525 – 22.797 <- 2.3 s past its parent
subagent: workflow-subagent 21.155 – 38.085 <- 17.6 s past its parent
Live tracing hangs workflow agents from the turn, which is extended, so live isn't
affected for workflows.
Expected: a parent span ends no earlier than its children.
Root cause
- When a sub-agent ends,
stop_subagentextends only the owning user turn
(translate/claude.rs:880-887,
viaextend_user_turnat
:487-509). - The agent's own parent is the continuation, or
tool:<Workflow call>on import
(:597-613),
and it gets no end update. - A continuation ends at its own
Stop
(:511-527),
and a tool span ends at its result
(:753-768).
Possible fixes
- Extend every ancestor up to the user turn when a child ends, not only the user turn.
One change covers continuations,Workflowspans and nested agents. It extends what
extend_user_turnalready does for the turn.
Related
bt trace import claudedrops nested and workflow sub-agents #297, Import nested Claude agents and workflows #303: workflow agents hang from theWorkflowspan since Import nested Claude agents and workflows #303.
Activity