fix(server): a subagent's events are no longer stored twice across runs #14665
t3code/codex-turn-mapping fromrefs/pull/14665/head·merged by juliusmarminge View on GitHubTwo runs could ingest the same provider-native subagent child thread at once, so each of its events was stored twice: once per run. In a child thread this shows up as its root turn going running → completed → running → completed, and as duplicate messages, tool calls and status updates.
Follow-up to #14641, where this first showed up.
Why#
RunExecutionService.routeProviderEvent decides which run ingests an event, using the set of thread ids each run owns. A child thread could end up owned by two runs that were both still ingesting, in two ways:
- Turn start took over running subagents.
ProviderTurnStartServiceseeds a new run withrelatedThreadIds: the child threads of every subagent on the thread thatcanRouteRelatedSubagentallowed. That excluded failed, interrupted and cancelled subagents, but still included running ones. The run that launched a running subagent keeps ingesting until the subagent ends (activeChildSubagentsholds it open), so the launch run and the new run both stored its events. Every background subagent that outlives its launch turn hits this when the next run starts, whether that run is a wake continuation or a user prompt. - Settled runs kept adopting new child threads.
app_thread.createdadds a child thread to every run that owns its parent thread. A run that is still ingesting after its root turn ended (for example, held open for a background subagent) therefore also adopted child threads started later. With a nested subagent started while the root is idle, the settled launch run and the continuation run both adopted it.
Fix#
Both changes are in RunExecutionService.ts:
canRouteRelatedSubagentis nowstatus === "completed". A new run only takes over a completed subagent's child thread, which is what resuming one with a later message needs. A running subagent stays with the run that launched it.- The routing state gets a
rootTurnEndedflag. It is set when the run's ownturn.terminalarrives, and after thatapp_thread.createdno longer adds child threads to the run. Child threads created before the root turn ended stay owned, so late events from them still route as before.
Each change is needed by itself. With only the first, the nested fixture still fails. With only the second, all five fixtures below still fail.
Which providers this affects#
The core rule is provider-agnostic, but only Claude's fixtures hit it today:
- Codex, Cursor and OpenCode 1 don't keep a run open for background subagents after the root turn.
- Grok (ACP) and OpenCode 2 hold the root run open until background subagents end, so the subagent has finished before the next run starts.
Claude settles the root turn while background subagents keep running, which is what makes both cases reachable.
Tests#
assertProviderNativeSubagentRootTurns(run for every replay fixture) now fails when two different runs store the same child-thread update. On the base it fails these 5 Claude fixtures, and with this change all 120 pass:claude_background_subagent_after_rootclaude_background_subagent_lifecycleclaude_background_wake_before_queued_promptclaude_background_wake_before_queued_prompt_no_echoclaude_nested_background_subagent_wake
Repeats within one run are left alone: several adapters legitimately re-emit an unchanged update.
RunExecutionService.test.ts:- New: "leaves a child thread created after the root turn ended to the run that is live then".
- The interrupted-ownership test now also asserts that a running subagent is not handed over.
Both fail on the base.
Verification#
RunExecutionService,ProviderTurnStartServiceandProviderEventIngestorunit tests, plus theClaudeAdapterV2tests: pass.- Replay suites: Orchestrator fixtures (integration and contract), Claude and Codex fixtures, recovery, restart background note, provider switch, thread fork and merge-back. With the unit tests above, that's 13 files and 406 tests, all passing.
vp run typecheckinapps/server: no errors.vp linton the touched files: only the existing unusedassertUserMessagesExcludewarning inshared.ts, which is on the base too.- Not run: repo-wide checks.
Model: Claude Opus 5.5 (Claude Code)
🤖 Generated with Claude Code
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
juliusmarminge added labelsmacroscope-review
Approvability#
Verdict: Approved at
e5116ccMacroscope's review found this PR approvable — This is a narrowly scoped server bug fix that makes subagent event ownership exclusive across runs, with focused unit and replay-fixture coverage for both handoff and late-child scenarios. The remaining changes are test-only and introduce no schema, deployment, security, billing, or default-behavior changes.
You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).Thread transfer impact#
✅ Thread transfer remains within every enforced ceiling.
ℹ️ No successful
mainbaseline artifact is available yet. This run establishes the initial measurement.Provider Metric Main baseline This PR Impact PR ceiling Codex Total thread wire — 4.9 KiB — 6.8 KiB ✅ Codex Thread snapshot wire — 3.7 KiB — 4.9 KiB ✅ Codex Live turn WebSocket wire — 1.2 KiB — 2.0 KiB ✅ Codex Live turn WebSocket decoded — 20.4 KiB — 29.3 KiB ✅ Codex Live turn messages — 2 — 8 ✅ Claude Total thread wire — 4.9 KiB — 6.8 KiB ✅ Claude Thread snapshot wire — 3.7 KiB — 4.9 KiB ✅ Claude Live turn WebSocket wire — 1.2 KiB — 2.0 KiB ✅ Claude Live turn WebSocket decoded — 20.8 KiB — 29.3 KiB ✅ Claude Live turn messages — 2 — 8 ✅ Baseline: unavailable · PR result:
e5116cc· Source CI: successScenario and decoded snapshot size
10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.
- Codex decoded thread snapshot: 106.1 KiB
- Claude decoded thread snapshot: 106.4 KiB
Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.
juliusmarminge merged thisf438d65
juliusmarminge closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes