Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(server): a subagent's events are no longer stored twice across runs #14665

Mergedjuliusmarmingemerged 1 commit intot3code/codex-turn-mapping fromrefs/pull/14665/headmerged by juliusmarminge View on GitHub

juliusmarmingecommented

Two 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:

  1. Turn start took over running subagents. ProviderTurnStartService seeds a new run with relatedThreadIds: the child threads of every subagent on the thread that canRouteRelatedSubagent allowed. 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 (activeChildSubagents holds 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.
  2. Settled runs kept adopting new child threads. app_thread.created adds 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:

  • canRouteRelatedSubagent is now status === "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 rootTurnEnded flag. It is set when the run's own turn.terminal arrives, and after that app_thread.created no 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_root
    • claude_background_subagent_lifecycle
    • claude_background_wake_before_queued_prompt
    • claude_background_wake_before_queued_prompt_no_echo
    • claude_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, ProviderTurnStartService and ProviderEventIngestor unit tests, plus the ClaudeAdapterV2 tests: 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 typecheck in apps/server: no errors.
  • vp lint on the touched files: only the existing unused assertUserMessagesExclude warning in shared.ts, which is on the base too.
  • Not run: repo-wide checks.

Model: Claude Opus 5.5 (Claude Code)

🤖 Generated with Claude Code


Devin Review
    1. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted

    2. juliusmarminge added labelsmacroscope-review

    3. Approvability

      Verdict: Approved at e5116cc

      Macroscope'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).
    4. Thread transfer impact

      ✅ Thread transfer remains within every enforced ceiling.

      ℹ️ No successful main baseline artifact is available yet. This run establishes the initial measurement.

      ProviderMetricMain baselineThis PRImpactPR ceiling
      CodexTotal thread wire—4.9 KiB—6.8 KiB✅
      CodexThread snapshot wire—3.7 KiB—4.9 KiB✅
      CodexLive turn WebSocket wire—1.2 KiB—2.0 KiB✅
      CodexLive turn WebSocket decoded—20.4 KiB—29.3 KiB✅
      CodexLive turn messages—2—8✅
      ClaudeTotal thread wire—4.9 KiB—6.8 KiB✅
      ClaudeThread snapshot wire—3.7 KiB—4.9 KiB✅
      ClaudeLive turn WebSocket wire—1.2 KiB—2.0 KiB✅
      ClaudeLive turn WebSocket decoded—20.8 KiB—29.3 KiB✅
      ClaudeLive turn messages—2—8✅

      Baseline: unavailable · PR result: e5116cc · Source CI: success

      Scenario 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.

    5. juliusmarminge merged thisf438d65

    6. juliusmarminge closed this

Sign in to join this conversation.