fix(server): an idle Claude thread now wakes when its background command finishes #36029
main fromrefs/pull/36029/head·opened View on GitHubProblem#
When a background command ends while a Claude thread is idle, Claude Code sends task_notification and then starts its own turn to handle it. T3 can close the Claude process before that turn sends its first frame. The agent never sees the result, and the thread stays idle until the next user message. The resumed Claude transcript then shows a <synthetic> "No response requested." entry right after the <task-notification>.
In that gap, hasPendingBackgroundWork in ClaudeAdapterV2.ts returns false. The task has left the roster, and the wake turn has not started. A session release that checks the probe in the gap closes the process.
Change#
hasPendingBackgroundWork now returns true while any remembered native thread has an owed wake task. It reads the existing owedWakeTasksForNativeThread, so it adds no state and no replay. It covers awaited and unawaited notices.
The session still releases after the wake turn consumes the notice, after a continuation run retires it, on a process reset, and on Stop. If a notice is never consumed, the session stays open until one of those happens or the 4-hour idle pin limit (DEFAULT_MAX_IDLE_PIN_MS) runs out. Query replacement keeps its own check.
Scope#
Bug fix. #33041 and #37252 report this bug. Both were closed because the maintainer could not reproduce it. Later comments on #33041 and on this PR add provider logs from v0.1.9 that show the same sequence.
This PR does not fix a wake that is lost when the Claude process exits (#30156) or when auto-settle detaches the session after a PR merge (#26083).
Verification#
I found the bug when my Claude agents went silent after a background command ended. Each resumed transcript had the "No response requested." entry right after the notice.
- Before the fix, four cases in
ClaudeAdapterV2.test.tsfailed because the probe returned false after the notification. The cases include both orders of the empty roster and the notice, and a sibling task. After the fix,vp run check:changedpassed 409 tests, and format, lint, Knip, GraphQL, and three typechecks passed. CI passes on the current head. - Other users see the same sequence on v0.1.9. The log from moazessam376-dev on this PR shows
query.close71 ms aftertask_notification. BartWaardenburg logged 28 lost wakes in 10 days (#33041) and reports that this change covers 26 of them. - I did not reproduce the bug live on a patched build. The tests use offline protocol fixtures.
Implemented with GPT-6 Astra through Codex in T3 Code. Correctness reviewed with Claude Opus 5.5.
- 1
tanstack-source added labelsbug
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XS
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
Approvability#
Verdict: Approved at
538393aMacroscope's review found this PR approvable — Claude sessions now remain alive while already-owed wake turns are pending, preventing idle cleanup from discarding that work. The change is confined to existing session lifecycle tracking and introduces no new product defaults or permissions.
You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).No actionable comments were generated in the recent review.
Walkthrough
-
ClaudeAdapterV2 now reports pending background work when a remembered native thread has owed wake tasks. Tests cover retention across roster snapshots, notifications, and process replacement, plus release after wake processing or an unstarted run.
Changes: Owed wake tracking
Layer / File(s) Summary Pending-work detection and wake lifecycle
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts,apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.tshasPendingBackgroundWorkchecks remembered native threads for owed wake tasks. Tests verify pending work remains across empty snapshots, notifications, wake initialization, and process replacement. Tests also verify it clears after wake processing oronRunEndedWithoutStarting.Estimated code review effort: 2 (Simple) | ~10 minutes
Change: Bug fix
Suggested reviewers:
juliusmarminge
Merge Risk:
Minimal · up to5cc2e-
Claude sessions are now kept alive while wake turns are still owed. The one edge case found, a notification whose query exits before the wake starts, delays idle closure only until Stop, an explicit reset, or a session limit applies. It is not a merge blocker.
Pre-merge checks:
4-
Check name Status Explanation Linked Issues check Check skipped because no linked issues were found for this pull request. Out of Scope Changes check Check skipped because no linked issues were found for this pull request. Title check The title names the main change: an idle Claude thread now wakes when its background command finishes. It uses the conventional commit form and matches the PR's stated fix, which makes hasPendingBackg… Description check The description follows the template. It has Problem, Change, Scope, and Verification sections. The Problem section gives the failing sequence and the gap where hasPendingBackgroundWork returns false.… Full details: Title check
-
Explanation
The title names the main change: an idle Claude thread now wakes when its background command finishes. It uses the conventional commit form and matches the PR's stated fix, which makes hasPendingBackgroundWork return true while an owed wake task remains. The title is specific enough for a teammate scanning history.
Full details: Description check
-
Explanation
The description follows the template. It has Problem, Change, Scope, and Verification sections. The Problem section gives the failing sequence and the gap where hasPendingBackgroundWork returns false. The Change section explains the new predicate and its release conditions. The Scope section states this is a bug fix, links the related issues, and names what is excluded. The Verification section lists the failing and passing tests, the check results, and what was not verified live. It also states the model and harness used, as the template asks. The description is complete and specific.
-
Finishing Touches
-
Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info-
Run configuration-
- Source mode: Auto
- Repository: pingdotgg/t3code
- Branch: main
- File: .coderabbit.config.ts
- Commit: 4860a73
- Layers: Repository TS config
- Review profile: CHILL
- Plan: Advanced
- Run ID:
7e51f3d6-d4fa-4173-b146-547600236351
Commits-
Reviewing files that changed from the base of the PR and between 9803b37a26774305c7b9e30347653962c9cb1b49 and 5cc2e2a4399a2ee6a817afec3ebf0b0801ca0bd6.
Files selected for processing: 2-
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.tsapps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
-
-
[gpt-6-astra] on behalf of Alan
Reviewed integration head 9803b37a26774305c7b9e30347653962c9cb1b49 against its upstream parent 5dd8cde0831113058f2af74a699499c0eeb3b31b. This supplements the prior signed review on 538393a5da398f371435f8d63569b3d776ebef2e.
Standards#
Prior standards conclusions remain applicable: the production and test patch is equivalent after line shifts. No new findings.
Spec#
A fresh independent native verifier confirmed all-owed ownership including unawaited tasks, shared published pendingBackgroundWork semantics, no new state or replay, preserved query-replacement predicate, and meaningful release assertions for consumption, retirement, reset and Stop. No findings.
Correctness#
Focused independent integration verification inspected pinned new source, both patches, changed upstream lifecycle code, runtime contracts and callers. Ownership cleanup remains compatible; ProviderSessionManager retains the session probe and existing idle-pin cap. Its new release-time cleanup does not bypass the probe. Prior outside Claude correctness conclusions remain applicable to the fix, supplemented by this verification. No findings.
Verification: new-head CI passed lint, build/typecheck, test and all eight server shards; Macroscope approved this head. No local tests rerun and no code edited. Prior local409 tests/check:changed evidence applies to the original patch; current integration execution is evidenced by CI.
Limits: this is focused integration verification, not a full review of unrelated upstream changes. No live Claude cancellation, dispatch acknowledgment or resumed-context normalization verified. astack #165 remains open.
Remaining findings: Standards0; Spec0; Correctness0.
[gpt-6-astra] on behalf of Alan
Reviewed integration head 5cc2e2a4399a2ee6a817afec3ebf0b0801ca0bd6 against upstream parent 4860a730695762331cc482253ba60beda7875c40, supplementing prior review of 9803b37a.
Standards#
Prior review remains applicable: the patch is identical apart from line offsets and blob IDs. No findings.
Spec#
Fresh independent native verification confirms all existing owed wakes, including unawaited tasks, retain lifetime. No new state or replay; query replacement retains its separate guard. Consumption, retirement, reset and Stop retain observable release assertions. No findings.
Correctness#
The verifier inspected the pinned patches, ownership cleanup and runtime callers. Upstream missing-conversation recovery and cooldown changes remain compatible. Prior outside correctness coverage remains applicable to the unchanged fix. No findings.
Verification: GitHub CI on this exact head passed lint, build/typecheck, Test, all eight server shards and aggregate Check. No local tests rerun or source edits. This focused integration check does not review all unrelated upstream changes or verify live Claude cancellation, exposure or resumed context. astack #165 remains open.
Remaining findings: Standards 0; Spec 0; Correctness 0.
Hit this today on v0.1.9 (macOS desktop, Claude Code 2.1.325, opus-5-5), with provider logs, in case another real-world case helps since #33041 and #37252 were both closed as non-repro.
Setup: the agent started a background Bash command, awaited it with
t3_background_wait, and ended its turn. The thread was idle with that command as the only background task. All of the thread's linked PRs were already merged, which might matter (see the last point).From the provider event log:
20:07:20.160Z in system/task_updated status: completed 20:07:20.160Z in system/task_notification "Background command ... completed (exit code 0)" 20:07:20.162Z in system/background_tasks_changed tasks: [] 20:07:20.215Z in system/init 20:07:20.215Z in system/status status: requesting <- CLI has started its wake turn 20:07:20.231Z out query.close <- 71 ms after the notification 20:07:22.561Z out query.open (resume) 20:07:25.188Z out query.close <- no prompt.offer in between 20:08:00.626Z out query.open + prompt.offer <- my next messageSo the wake turn was already requesting when T3 closed the CLI. T3 then started a continuation run (ordinal 14, the "Command ... finished" notice in the timeline). It opened a resumed query, never sent anything, and finalized about 1.4 s later with
executedModel: null. The thread showed as settled with nobody handling the result, and it only picked up again when I sent a message about 40 s later.One more thing in the server trace:
thread.auto-settlewas dispatched at 20:07:20.405, about 170 ms after the close. It isn't what closed the session here, but the settle-on-merge sweep fires in the same window. #30837 might be worth looking at alongside this one.Happy to share more of the log if it helps.
Note
Written by Claude Opus 5.5 on behalf of @BartWaardenburg
Thanks for this fix. It covers 26 of the 28 lost wakes in my logs (data in #33041), so please merge it soon.
A follow-up idea: some close paths still drop a wake (the 4-hour pin expiry, a model switch, a crash). The Codex adapter does not have this problem, because T3 itself starts the wake run from the completion event. Claude could do the same: T3 records the owed wake and turns it into a continuation run on any close. Then correctness no longer depends on the timing of the close.
Details for an implementer (code paths, proposal, test)
Close paths that still drop a wake on
main(f5f8c8d)#This PR keeps the process alive while a wake is owed. Only the Claude CLI can deliver the wake, so each close path must ask
hasPendingBackgroundWork. These paths can still drop it in silence:- Idle-pin expiry. After
DEFAULT_MAX_IDLE_PIN_MS(4 hours),releaseIfStillIdleinProviderSessionManager.tsreleases a pinned session anyway. A wait of more than 4 hours loses its wake. - Query replacement. A model or policy switch closes the query. The PR body says that it leaves this path unchanged.
- A process crash, or a server restart.
restartCancelledBackgroundWorkNotereaches the agent only at the next user message. It does not wake the thread. - Check then act. The release fiber reads the probe, and the message ingestion runs on another fiber. A notification can arrive between the check and
query.close.
The Codex model#
When a Codex background command ends between turns,
CodexAdapterV2.tsbuilds "Background command completed (exit N): ..." plus the output tail from the event, and callscontinuationRequests.offeritself ("Codex starts a new turn from this text, so nothing is buffered"). A close after that point loses nothing.Proposal for
ClaudeAdapterV2.ts#- When a wake-eligible
task_notificationarrives and no root turn runs, record an owed wake (thread, task id, summary, output file) undersdkMessageLock.owedWakeTasksForNativeThreadalready computes this set. The new part is a durable record with one owner. - Retire the record when the native wake turn sends its first root frame (
isClaudeRootTurnFrame, with the existing replay tombstone), or when a continuation run consumes it. - On every close path (
releaseEntry, query replacement, process exit, restart recovery), turn each record that is not retired into a continuation run, through the same path thatbufferWakeMessageuses. - Keep the hold of this PR. It saves a process restart in the common case.
- Log
orchestration-v2.claude-close-dropped-owed-wakeat warn level if a close cannot convert a record. Today the only trace is a<synthetic>"No response requested." in the transcript, hours later.
Test#
One table or property test in
ClaudeAdapterV2.test.tsover the event orders:task_notification,background_tasks_changed [],task_updated, first root frame,result, and each close path (idle release, pin expiry, query replacement, process exit). Assert exactly one delivery for each order. The current tests check the probe value, not the delivery.A larger question (not a request)#
For Claude and Codex both, the background command runs inside the provider process, so a release or a crash also kills the work itself. Claude Code can turn off its own background mode:
CLAUDE_CODE_DISABLE_BACKGROUND_TASKS=1removesrun_in_backgroundfrom Bash (checked on 2.1.327), andMonitorandWorkflowcan go indisallowedTools. T3 could then run background commands detached, through its own MCP tools, and deliver each result as a run, for every provider. Is that a direction that you would consider?- Idle-pin expiry. After
syanga(GitHub profile, opens in a new tab) changed the title
Sign in to join this conversation.
syanga(GitHub profile, opens in a new tab) reviewed
[gpt-6-astra] on behalf of Alan
Reviewed 538393a5da398f371435f8d63569b3d776ebef2e against 0b0a61a01f2fd35319cfc55cb9a4145ec3c27593. Converged with no remaining findings.
Standards#
Fresh native review found no documented-standard breaches or actionable smells. Existing domain helper reused; tests extend observable behavior.
Spec#
Fresh native review found no missing requirements or scope creep. All owed wakes include unawaited notices; no new state/replay; query replacement preserved. Explicit audit of legacy assertions confirmed pending ownership and retained consumption, retirement, reset and Stop release controls.
Correctness#
Outside Claude Opus 5.5 review found no defects and recommended shipping. Inspected ownership/helper, tombstones, consumption, retirement, reset/Stop, replacement and ProviderSessionManager idle release. Accepted unconsumed-notice retention remains bounded by existing session-manager policy. No tests run by reviewer; fiber timing and every rememberProviderThread caller were not exhaustively traced.
Original reviewed candidate a2097937d3eda18996fbda13ca1c1e6149d8e0b8 was rebased onto upstream test refactoring. A fresh native focused review confirmed identical ordered patch changes after whitespace normalization, byte-identical production code, preserved fixture messages/release controls, and unchanged lifetime caller. Earlier reviews remain applicable to this head.
Required vp run check:changed passed: 409 tests, format/lint/Knip/GraphQL/three typechecks. No live Claude cancellation/provenance claims; astack #165 remains open.
Remaining findings: Standards 0; Spec 0; Correctness 0.