Repository navigation
Description
Problem
With --attach, the import sends each turn once, when the next prompt appears. A
background sub-agent's start and stop sit in the turn that launched it, at its
async_launched result. Its transcript is read as it stands at that moment. Everything
the agent writes afterwards is never sent, so its span keeps no model or tool calls, no
output, and an end time at its launch.
A plain import of the finished session is complete. Live tracing handles this since #304,
but that late-call path is switched off for import events.
Impact
--attach is for following a running session. Background agents usually keep working
after the turn that launched them ends. On one engineer's machine, 363 background agents
in 97 sessions made 8,027 model calls after their launching turn closed, and an attached
import loses that work.
Steps to reproduce
In a directory holding notes.txt (Claude Code 2.1.325, bt 0.33.0):
-
Start this run:
claude -p --model haiku --allowedTools "Agent Bash Read" --output-format json "Use the Agent tool twice in one message, both with subagent_type general-purpose and run_in_background set to true. Agent A prompt: \"Reply with exactly: A done\". Agent B prompt: \"Do these steps one at a time, one tool call per message: 1) run the Bash command sleep 12; 2) read notes.txt with the Read tool; 3) run the Bash command sleep 12; 4) read notes.txt again with the Read tool. Then reply with exactly: B done\". After launching both, end your turn immediately without waiting. Whenever you are notified that an agent finished, reply with one short line naming it. Once both are done, reply: all done."
-
As soon as its transcript appears, run
bt trace import claude <session-id> --attach --destination project_logs:<project-id>.
Stop it with Ctrl-C a few seconds afterclaudeexits. -
Compare it with a plain
bt trace import claude <session-id>of the finished session.
Agent A finishes almost at once, and its notification closes Turn 1. Agent B makes all 5
of its model calls after that.
| Agent B's LLM spans | Agent B's tool spans | Agent B's span | |
|---|---|---|---|
| On disk | 5 calls | 4 (2 Bash, 2 Read) | ends about 29 s after launch, "B done" |
--attach, today |
0 | 0 | ends at its launch, output null |
| Plain import | 5 | 4 | correct |
Expected: the attached import ends with the same tree as the plain import.
Root cause
- Each turn is sent once.
Tail::pollsends a turn when the next prompt exists, then
advances past it for good
(transcript_import/claude.rs:49-108).
It also re-reads only when the main transcript changes
(transcript_import/mod.rs:473-475). - The stop is read once. The agent's
SubagentStopsits in the launching turn, and
stop_subagentreads the sub-agent transcript as captured then. - Capture late Claude subagent model calls #304's polling is skipped. The polling for calls written after a stop runs only for
events without_bt_import_through_offset
(translate/claude.rs:814-818),
and every import event carries it.
Possible fixes
- Enable Capture late Claude subagent model calls #304's stopped-sub-agent polling for attached imports. Finalize then picks up
the rest. It reuses the live path's fix. - Have the attached tail follow sub-agent files. Re-send a stop or transcript update
for an agent whose file grows after its turn was sent.
Related
- Live Claude Code tracing sometimes loses a sub-agent's final model call #300, Capture late Claude subagent model calls #304: the same problem for live tracing, fixed there.
Activity