fix(server): threads waiting on background work wake after a restart #15438
main fromrefs/pull/15438/head·opened View on GitHubProblem#
A Claude or OpenCode agent can end its turn waiting on background work (a run_in_background command, a Monitor, a background subagent) and rely on the provider's completion notice to wake it. When the server dies, that work dies with the provider process. Startup recovery already records what was lost on the run, but it only woke the thread when Continue threads after restarts was on. That setting is off by default, so the thread sat idle until someone sent a message.

Fixes #15358
Change#
Waking a settled run whose own provider thread lost background work no longer needs the opt-in. The provider would have woken the agent when that work ended; the restart swallowed the ending, so recovery delivers it as the existing cancellation note. Resuming a turn the restart cut off mid-flight stays behind the setting.
restartContinuationRuntakes aresumeInterruptedflag. Startup reconciliation andprepareForShutdownpass the project's setting into it instead of skipping the thread, so the crash path and the graceful-restart path both record the wake.continueRestartedRunapplies the setting only when the continuation is not a note wake (restartContinuationNote(...).settled). Failed runs, cancelled provider turns, archived threads, provider switches, newer user messages and/compact-style turns still block it, as before.- A wake cut by a second restart before it reached the provider is offered again. The recovery read holds that continuation but not the run it continues, so selection offers any prepared continuation and delivery, which reads the full history, tells a wake from a resume.
Because the wake now happens by default, two cases that used to sit behind the opt-in are guarded for every restart continuation:
- A settled thread (manual or automatic) is not reopened by a restart. The orchestrator's serialized acceptance checks this too, so a settle that lands between reading and delivering the wake still wins.
- The Stop check from #15323 now applies to the attempt the Stop targeted. A Stop the crash cut off still blocks the continuation; a steer after the Stop starts a new attempt and is no longer blocked by it.
Per provider: only Claude and OpenCode leave work running past a settled turn, and both wake the agent natively when it ends. Codex, Cursor, Grok, Pi, Antigravity and ACP settle their items with the turn, so recovery never records lost work for them and nothing changes there. The wake goes through the existing continuation path, so scheduled tasks and MCP-driven threads get it the same way UI threads do.
Scope and approval#
Triaged bug #15358 (bug, via-triage). The triage note names this as the first option ("always continue a thread that lost background work, even with the setting off"). This PR limits it to settled runs whose own provider thread lost work, the case where waiting for the user is always wrong. Exposing background state through t3_thread_read / t3_thread_wait is left for #15099, as the triage suggests.
Verification#
Live repro, web client. Worktree dev server with its own fresh .t3, setting at its default (off), Claude Sonnet 5 via Claude Code. The prompt asks the agent to start until [ -e <marker> ]; do sleep 1; done with run_in_background: true and end its turn. Server killed with taskkill /F on its PID (the Windows equivalent of kill -9), then started again. Run and outbox state read from the worktree's statev2.sqlite.
Before (main at 243d1e7c44) | After (fix on the same base) | After (final head cdf03be019, on current main) | |
|---|---|---|---|
| Server hard-killed | 20:42:18 | 20:57:03 | 22:40:56 |
| Server listening again | 20:43:54 | 20:57:30 | 22:41:39.0 |
| Run 1 after recovery | completed, lost work recorded (shell … (id bdaulg52a)) | same | same |
| Wake | none; no provider-runtime.continue effect; idle at 20:45:06 and after the marker was released | continuation run requested 20:57:30.2, completed 20:58:04 | continuation run requested 22:41:39.7, completed 22:42:07 |
The before run was on main before #15323 merged. #15323 kept the wake behind the setting, so current main behaves the same with it off. Recording of the middle run (7x, wall clock bottom right):

Focused tests (cd apps/server && ../../node_modules/.bin/vp test run …):
RestartContinuation,testkit/OrchestratorReplayRestartBackgroundNote.integration,RestartBackgroundNote,ProviderRuntimeRecoveryService,ProviderRuntimeRecoveryService.regression,ProviderRuntimeRecoveryPerformance,ProjectionRecovery,DelegatedCompletionDelivery,FoundationPersistence,EffectWorker: 117 passed, 1 skipped.runtimeLayer.test.ts: 58 passed.- Without the fix, the new tests fail: the note dispatch with the opt-in off, the recovery wake with the opt-in off (startup records the note and queues one wake; a graceful restart queues it before shutdown and not again), the Claude replay test where a restart cuts a background subagent with the setting off (the provider never receives the note prompt), and the carry-forward test with the opt-in off (a wake cut by a second restart).
- Each guard test fails without its guard: settled thread (selection and delivery), a settle racing the delivery (real orchestrator in
runtimeLayer.test.ts), and a Stop superseded by a steer. - The replay test previously pinned the old behavior (setting off: no wake, note only on the next user message). It now runs with the setting on and off and expects the wake both times. Delivery of the note on the next user turn is still covered by
RestartBackgroundNote.test.ts. #15323's Stop fixture now carries the cut turn'sproviderTurnId, as a real Stop does. tsc --noEmitforapps/server: 0 errors.vp linton the touched files: no new warnings (one existinglayerUnavailablewarning inOrchestrator.ts).
Not checked:
- OpenCode live. The same recovery path records its background subagents, but I only ran Claude end to end.
- Linux/macOS live runs. The live repro was on Windows; the reporter's environment was Linux. The server path does not depend on the platform.
- The settled, Stop and second-restart guards live. They are covered by the tests above.
- Mobile and desktop clients. No client code changed; they render the same continuation run.
Two things I noticed in the client that this PR doesn't touch: the note shows "Sent by another agent", and while the woken turn runs, its "Working for" timer counts from the first run's start (it showed 3m 10s, then "Worked for 34s" once done).
Implementation: Claude Opus 5.5 via Claude Code in T3 Code. Review: gpt-6-astra (high) via Codex CLI, six rounds. Its settled-thread, settle-race, Stop-then-steer and second-restart findings are fixed here (its swallowed-Stop finding was already handled by #15323); the last round had no findings.
🤖 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:L
Navigate logical layers of code changes, visualize relationships, and explore their blast radius.
🧰 Additional context used
📚 Code guidelines (2)
docs/internals/effect-services.md — auto-discovered AGENTS.md — auto-discoveredNo actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
- Configuration used: Repository: pingdotgg/t3code/.coderabbit.yaml
- Review profile: CHILL
- Plan: Advanced
- Run ID:
5f8fe20a-fd78-48a1-90d7-691f2f68634a
📥 Commits
Reviewing files that changed from the base of the PR and between 1826fb55cc4c9c6246d31175e15a496ba4778e07 and cdf03be019726434a9c755ab9a581a9a902af537.
📒 Files selected for processing (8)
apps/server/src/orchestration-v2/Orchestrator.tsapps/server/src/orchestration-v2/ProviderRuntimeRecoveryService.tsapps/server/src/orchestration-v2/RestartContinuation.test.tsapps/server/src/orchestration-v2/RestartContinuation.tsapps/server/src/orchestration-v2/runtimeLayer.test.tsapps/server/src/orchestration-v2/testkit/OrchestratorReplayRestartBackgroundNote.integration.test.tsdocs/internals/server-updates.mddocs/user/updating.md
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
📝 Walkthrough
Walkthrough#
Restart recovery now offers continuations for cancelled background work even when continuation is disabled. The setting still controls resumption of interrupted runs. Settled threads do not accept restart continuations.
Changes#
Restart continuation recovery
Layer / File(s) Summary Continuation eligibility and notes
apps/server/src/orchestration-v2/RestartContinuation.ts,apps/server/src/orchestration-v2/RestartContinuation.test.tsThe helper distinguishes interrupted runs from continuations for settled runs with cancelled background work. It excludes put-away threads and only treats a Stop as blocking when it matches the source run’s active attempt. Tests cover opt-in settings, carried-forward notes, and wake selection. Recovery wiring and settled-thread protection
apps/server/src/orchestration-v2/ProviderRuntimeRecoveryService.ts,apps/server/src/orchestration-v2/Orchestrator.ts,apps/server/src/orchestration-v2/runtimeLayer.test.ts,apps/server/src/orchestration-v2/testkit/OrchestratorReplayRestartBackgroundNote.integration.test.ts,docs/internals/server-updates.md,docs/user/updating.mdStartup and shutdown recovery pass the project setting to continuation eligibility. Tests cover cancelled-work recovery with continuation enabled and disabled, and confirm settled threads remain settled. Documentation describes the setting and cancellation note. Priority: ➖ Normal
Estimated code review effort: 3 (Moderate) | ~25 minutes
Change: Bug fix · Severity of issue fixed: Medium
Suggested reviewers:
juliusmarmingeMerge Risk: ⚪ Minimal · up to
cdf03No actionable merge-blocking issue is established. Normal checks can proceed before merging.
Security Architecture Review#
Security architecture risk: 🔵 Low · up to
cdf03The change enables automatic restart wakes for lost background work while preserving stop, explicit settlement, provider identity and existing execution-policy checks. The behavior is bounded, but provider-specific restart behavior has not been fully validated.
Retained concerns No architecture-level concerns identified.
Security review details
Security Blast Radius
- inferred — The behavioral expansion applies across eligible recoverable threads, but each wake is bound to its saved source thread, run and provider. It can initiate further provider execution through the ordinary runtime-policy path; the reviewed change does not establish a cross-thread privilege grant.
Trust Boundaries and Controls
- observed — The server creates the continuation message using the source run's model selection and explicit source-run linkage. Acceptance rechecks archived, deleted or explicitly settled state, provider identity and newer executed work before permitting dispatch.
Resilience and Maintainability Implications
- observed — Stale continuations receive an accepted receipt without reopening work, limiting repeated stale delivery. Declined continuations invoke delegated-task recovery, and the effect worker also invokes that cleanup on terminal failure when no retry remains.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation Title check ✅ Passed The title clearly summarizes the main change: waking threads waiting on background work after a restart. Description check ✅ Passed The description covers the required Problem, Change, Scope and approval, and Verification sections. It explains the behavior, links the triaged issue, and reports test results and untested cases. Linked Issues check ✅ Passed [ #15358] Startup and graceful-shutdown recovery now prepare restart continuations for settled runs that lost their own provider-thread background work, even when continuation is disabled. `RestartCont…Out of Scope Changes check ✅ Passed The changes in ProviderRuntimeRecoveryService.ts,RestartContinuation.ts,Orchestrator.ts, and their tests support the restart wake required by [#15358] or guard its delivery. The documentation …Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 6 files. (2 skipped: 2 … ✨ Finishing Touches
🧪 Generate unit tests (beta)
- Create a new PR
- Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Comment `@coderabbitai help` to get the list of available commands.Closing. Waking threads by default isn't the right fix: it starts unrequested turns after every update, and the note can be wrong while orphaned work is still running (#15357). I've proposed keeping the wake opt-in and showing the lost work instead: https://github.com/pingdotgg/t3code/issues/15358#issuecomment-5976431027
SunkenInTime(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
Approvability#
Verdict: Not approved
Macroscope's review found this PR not approvable — This changes the effective default restart behavior by automatically waking settled threads that lost background work even when the continuation setting is off. The recovery-path changes are well covered by tests but affect production orchestration and warrant human review.
You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).