Skip to content

Claude sub-agent spans outlive the span they hang from (continuations and Workflow) #321

In progress

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 own Stop. That's on import and live.
  • On import, a workflow agent hangs from the Workflow tool span. That span ends as soon
    as Workflow returns async_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 Workflow span, 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_subagent extends only the owning user turn
    (translate/claude.rs:880-887,
    via extend_user_turn at
    :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, Workflow spans and nested agents. It extends what
    extend_user_turn already does for the turn.

Related

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions