Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(opencode): a finished background subagent keeps its thread busy until the wake run is dispatched #40396

Opennkoynov(GitHub profile, opens in a new tab)wants to merge 2 commits intomain fromrefs/pull/40396/headopened View on GitHub

nkoynov(GitHub profile, opens in a new tab)commented

Problem

When an OpenCode thread's last background subagent finishes, the desktop and web apps announce "Thread completed" for the run before it, and the wake run that reads the subagent's report starts a moment later. The OpenCode 2 adapter drops the finished child from pendingBackgroundTasks as soon as its report arrives (syncRoster), but the wake run only exists once ProviderContinuationService has dispatched the continuation the adapter offers (onWake → offerWake). In between, the thread shell is completed with no active run and nothing holding it, so the client gate notifies at once (only background-wake completions get RELEASED_COMPLETION_GRACE_MS).

On our host the gap was 0.2–3.3 s, mostly the continuation's projection read. From 2026-11-20 10:40 to 2026-11-21 14:27 (UTC+2), 165 of 493 completions the push relay deferred were followed by a new run within 5 s. The relay skips those; the desktop app alerts on every one. Live case: subagent completed 12:21:28.225 UTC, Mac banner 12:21:28.670, wake run requested 12:21:31.621.

Change

The adapter keeps each background call a subagent report settled, as a pending subagent task, until the run that reads the report has been dispatched:

  • the report entry carries the tasks it settled; syncRoster lists them while the report waits for a wake;
  • onWake moves them onto the wake, which lists them until the continuation request is settled: dispatchIfCurrent ending (the run was dispatched, the wake was dropped and declined, or the dispatch failed) or clearIfCurrent (which ProviderContinuationService calls when it gives up before dispatchIfCurrent, e.g. when reading the thread fails or the thread is archived). Both run the same idempotent release;
  • a report cancelled or delivered into a running turn, a stopped child, and a Stop or lost stream (which clear reports and wakes as before) release them.

Only root threads list them, so a subagent's own roster is unchanged. hasBackground already counted pending reports and wakes, so pendingBackgroundWork doesn't change; only the published task list now matches it. No client change: every client sees the thread as waiting until the wake run exists.

Restart recovery reads the persisted roster, so a server restart inside that window would record the finished subagent as cut off by the restart, mark the run interrupted and wake it with a "cancelled" note. The recovery projection's turnItems hold only open items, so it can't see that the subagent already ended. getRuntimeRecoveryProjection now also returns settledRosterSubagents: ended subagent items whose native id the thread's provider roster still lists as a subagent task. The query starts from the roster, so it reads items only while a subagent is listed, and then uses the (thread_id, type, status) index. Recovery (ProviderRuntimeRecoveryService, RestartContinuation.hasOpenAwaitedWork) skips a listed subagent when it has such an ended item and no open item with the same native id (rosterTaskEnded). That task was only waiting for its wake, and before this change recovery didn't see it at all because it wasn't listed.

Scope

Part of #31087: this covers OpenCode background subagents only, so it shouldn't close the issue. The same gap for Claude background commands (the case in @diwakar-s-maurya's comment) is in the Claude adapter and isn't covered here. #31130 and #31357 hold app-owned delegated tasks only.

Verification

lists a finished background subagent until its wake's continuation is <outcome> (adapter): a background subagent finishes, its report arrives, OpenCode starts the wake; the roster must keep call-background from the report until the hold ends, then empty. Outcomes: dispatched, failed, cleared (clearIfCurrent), stopped (a user Stop drops the wake, then dispatchIfCurrent declines it), cancelled (the report is cancelled before any wake).

origin/main 0775dc450f: dispatched × failed × cleared × stopped ×  (expected [] to deeply equal ['call-background'])
                        cancelled ✓ (it checks only that a cancel releases the hold)
this branch:            all 5 ✓

Recovery, through the real SQLite ProjectionStore: cuts off and wakes for a listed native subagent only while its item is open (<status>) (ProjectionRecovery.test.ts). A settled run's provider thread lists a native subagent. If its item has completed, recovery leaves the run settled and wakes nothing; if it is still running, recovery records it as cut off and wakes the run, as before. On origin/main the completed case fails: even with the settledRosterSubagents assertion removed, recovery queues effect:restart-continuation for the finished subagent. The running case passes on both. Unit tests: does not record a listed subagent that finished before the restart as cancelled (ProviderRuntimeRecoveryService.test.ts) and does not wake a settled run for a listed subagent that had already finished (RestartContinuation.test.ts). The second also checks that a listed subagent with no ended item, or with an open item under the same native id, still counts.

Five existing adapter test cases waited for an empty roster after a report that no wake run ever reads in the replay (settles every call that joined a running subagent with its one report and the four settles a resumed subagent's calls when … cases); they now wait until no listed task is still running. The subagent, held-wake and resumed-subagent tests in OpenCode2AdapterV2.test.ts, ProviderRuntimeRecoveryService.test.ts and RestartContinuation.test.ts pass, and so does vp run check:changed.

End to end on a VM running nightly 5024 with the same change applied, a thread whose turn starts one background subagent (with the continuation dispatch slowed by 3 s to match our host's read time), recording every orchestration.subscribeShell update: without the change 2 of 2 runs published a completed shell with no active run and an empty task list for the first run before the wake run started; with it 0 of 2, and the final completion still arrived once each.

Done with Claude Opus 5.5 in OpenCode, driven from T3 Code.

    1. Pushed912e82d

Sign in to join this conversation.