Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(server): OpenCode 2 stops subagents a dropped background reply started #14760

Mergedjuliusmarmingemerged 16 commits intov2/oc2-7-continuation-wake-identity fromrefs/pull/14760/headmerged by juliusmarminge View on GitHub

juliusmarmingecommented

Reported from a downstream V2 fork. Stacked on #14752: both change where held background replies are dropped (runWake, Stop, reconnect). It also merges in #14744, which lands first: both change reconcile's early return, so that conflict is resolved here, and #14744's changes show in this diff until it lands. Review only the commits after that merge, 1869202f5c. Once #14744 and #14752 land, restack with git rebase --onto <main> 1869202f5c.

Problem

When a background subagent ends, OpenCode 2 runs the parent's reply on its own. The adapter holds that reply's events for a continuation turn. If the reply starts a subagent of its own, OpenCode announces the child's session live (session.created), and the adapter records it in childOwners and announced right away. The call that names the child, though, sits in the held events.

The held reply can be dropped before it is replayed: by a Stop, by a reconnect, or by a later continuation that skips it. In that case the announcement stayed. The child kept running and requestTurn routed it to the thread's active turn. Its permission requests and forms then showed up under whatever unrelated turn ran next.

Reproduction

New adapter replay test: keeps a subagent a stopped background reply started off the next turn, in OpenCode2AdapterV2.test.ts, using the 2.0.18 frame shapes.

  1. A turn starts two background subagents.
  2. The first one's report wakes the parent into a held reply, which starts subagent D.
  3. The user presses Stop before the continuation runs.
  4. An unrelated prompt starts, and D asks for a permission.

On main with #14752 the replay stops at the Stop: the adapter never tries to stop D. Without that, the unrelated turn shows per_spawned as pending and then cancels it when the turn ends. With the fix, the next turn shows no request.

Fix

  • Each held reply (Wake) records the subagent sessions announced while it was held.
  • discardWakes replaces the bare wake.dropped = true at the Stop, reconnect and continuation-skip sites. It does bookkeeping only. A child of a dropped reply whose call never registered (still only in announced) leaves announced/childOwners at once, so it stops routing to the thread. It then becomes a stray.
  • A stray is marked stopped on the session that called it, as a Stop marks the children it reaches. Its report then wakes an execution that is stopped, and no continuation is offered.
  • A subagent a stray starts is a stray too. Dropping a reply also retires every subagent that its subagents announced before the drop, at any depth. Before this, a third level (reply starts D, D starts N, N starts E) kept its owner and routed E's requests to an unrelated turn.
  • A request from a stray is refused (a reply-less reject, which ends its run) and is never shown under a turn.
  • stopStrays interrupts a thread's strays one by one, as Stop does for background subagents, and never under the adapter's event lock:
    • Stop awaits it after releasing the lock.
    • A continuation that skips a reply, or a reconnect, forks it.
  • A stray is forgotten only once an interrupt reaches it or the server no longer runs it. If an interrupt fails, the next Stop tries again.
  • Children that registered their own call (attachChild already consumed the announcement) are left alone, as the report asks.
  • Replaying a reply is unchanged: its events attach the child as before.

Verification

  • keeps a subagent a stopped background reply started off the next turn:

    1. D's first interrupt fails.
    2. During the next turn, D and its own subagent N both ask for permissions. Both are refused, and nothing is shown.
    3. A later Stop reaches D, then N.
    4. D's report wakes an execution, which is stopped. No new continuation is offered.
    5. A further Stop sends nothing.

    It fails on main with #14752 (no Stop of D, and the request leaks into the next turn) and on each earlier head of this PR. It passes now.

  • ends a continuation while a skipped reply's subagent does not answer its Stop: a hung interrupt doesn't block B's continuation from replaying and ending. It times out on the first head and passes now. It waits on a Deferred the second offer completes, not a poll.

  • keeps every subagent a stopped background reply's subagent started off the next turn: three levels (D, N, E) are announced under the held reply before a Stop drops it. All three are stopped, and E's permission during the next turn is refused and never shown. Without the descendant retirement it fails: no Stop of E, and per_deep shows under the unrelated turn.

  • lets a prompt join a background reply after a lost stream took a Stop's end: after a reconnect loses a timed-out Stop's end, a user prompt joins the reply OpenCode started. It pins #14744's rule that a later execution start clears the Stop's mark. With only that line removed, the prompt checks session.active first (actual: { type: 'session.active' }), and the turn interrupts the reply and fails.

  • The reconnect and failed-prompt continuation tests merged in from #14744 started their continuation with a made-up message id. Here wakes are matched by id, so nothing matched and the continuation ended at once: the reconnect test passed even with #14744's fix removed. They now start from the offered id and check that CHILD_OK renders. With the made-up id the reply check fails (expected [] to include 'CHILD_OK'), and with #14744's fix removed the reconnect test fails ([ 'interrupted', 'failed' ]).

  • Inside unshare -U --map-current-user -p -f --mount-proc:

    • OpenCode2AdapterV2.test.ts + ProviderContinuationService.test.ts: 112/112 (adapter file 93/93).
    • testkit/OrchestratorReplayFixtures.integration.test.ts -t opencode2: 16/16.
  • vp exec tsc --noEmit -p apps/server/tsconfig.json: exit 0. git merge-tree --write-tree of these commits over main + #14744 + #14752: clean. vp lint and vp fmt --check on touched files: clean.

  • Follow-ups, not chased here (same design, deferred after two review rounds):

    • Strays aren't persisted, so a T3 restart forgets them, like the rest of the adapter's in-memory state.
    • A stray whose interrupt keeps failing is retried only on the next Stop or reconnect.
    • Strays don't count as pending background work, and are keyed by the caller's ThreadState. So idle release can unload a thread while a stray whose interrupt failed still runs, and a resumed thread's new ThreadState no longer matches it. A spawned server stops with its last borrower, which ends the stray; an external server keeps it running.
      • The fix is small: count strays in owesWork/hasBackground, and key them by the root session id.
      • But counting them means a stray that never answers pins the session against idle release. That trade-off is worth deciding separately.
  • Not run: live OpenCode. Dropping a held reply while its child is still running can't be produced on demand.

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

    2. juliusmarminge added labelsmacroscope-review

    3. github-actions[bot](GitHub profile, opens in a new tab)commented

      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—5.0 KiB—6.8 KiB✅
      CodexThread snapshot wire—3.8 KiB—4.9 KiB✅
      CodexLive turn WebSocket wire—1.2 KiB—2.0 KiB✅
      CodexLive turn WebSocket decoded—20.9 KiB—29.3 KiB✅
      CodexLive turn messages—2—8✅
      ClaudeTotal thread wire—5.0 KiB—6.8 KiB✅
      ClaudeThread snapshot wire—3.8 KiB—4.9 KiB✅
      ClaudeLive turn WebSocket wire—1.2 KiB—2.0 KiB✅
      ClaudeLive turn WebSocket decoded—21.2 KiB—29.3 KiB✅
      ClaudeLive turn messages—2—8✅

      Baseline: unavailable · PR result: 1045f2a · 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: 108.5 KiB
      • Claude decoded thread snapshot: 108.8 KiB

      Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.

    4. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      Approvability

      Verdict: Not approved

      Macroscope's review found this PR not approvable — The production adapter changes continuation, Stop, reconnect, and subagent event-routing behavior through new stateful and asynchronous lifecycle logic. The replay tests are extensive, but the runtime change is broader and more complex than a self-contained bug fix, so it merits human review.

      You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    5. juliusmarminge merged thisa7034aa

    6. juliusmarminge closed this

Sign in to join this conversation.