Skip to content

fix(server): an idle Claude thread now wakes when its background command finishes - #36029

Open
syanga wants to merge 3 commits into
pingdotgg:mainfrom
syanga:fix/claude-notice-ownership
Open

syanga wants to merge 3 commits into
pingdotgg:mainfrom
syanga:fix/claude-notice-ownership

Conversation

@syanga

@syanga syanga commented Nov 13, 2026 •

Copy link
Copy Markdown

Problem

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.ts failed 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:changed passed 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.close 71 ms after task_notification. BartWaardenburg logged 28 lost wakes in 10 days (Claude session closed right after a background job ends, so the wake turn never runs #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.

@syanga syanga left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gpt-6-astra] on behalf of Alan

Reviewed 538393a against 0b0a61a. 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.

@github-actions github-actions Bot added vouch:unvouched Author is not yet trusted in the VOUCHED list. size:XS 0-9 changed lines (additions + deletions). labels Nov 13, 2026
@macroscopeapp

macroscopeapp Bot commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Approved at 538393a

Macroscope'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.

@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

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.ts
hasPendingBackgroundWork checks 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 or onRunEndedWithoutStarting.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: juliusmarminge

Merge Risk: Minimal · up to 5cc2e

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: Passed 4

Check name Status Explanation
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Title check Passed 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 Passed 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

Commits

Reviewing files that changed from the base of the PR and between 9803b37 and 5cc2e2a.

Files selected for processing: 2

  • apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
  • apps/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.

@syanga syanga left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gpt-6-astra] on behalf of Alan

Reviewed integration head 9803b37 against its upstream parent 5dd8cde. This supplements the prior signed review on 538393a.

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.

@syanga syanga left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gpt-6-astra] on behalf of Alan

Reviewed integration head 5cc2e2a against upstream parent 4860a73, supplementing prior review of 9803b37.

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.

@moazessam376-dev

Copy link
Copy Markdown

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 message

So 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-settle was 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.

@BartWaardenburg

Copy link
Copy Markdown

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), releaseIfStillIdle in ProviderSessionManager.ts releases 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. restartCancelledBackgroundWorkNote reaches 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.ts builds "Background command completed (exit N): ..." plus the output tail from the event, and calls continuationRequests.offer itself ("Codex starts a new turn from this text, so nothing is buffered"). A close after that point loses nothing.

Proposal for ClaudeAdapterV2.ts

  1. When a wake-eligible task_notification arrives and no root turn runs, record an owed wake (thread, task id, summary, output file) under sdkMessageLock. owedWakeTasksForNativeThread already computes this set. The new part is a durable record with one owner.
  2. 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.
  3. 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 that bufferWakeMessage uses.
  4. Keep the hold of this PR. It saves a process restart in the common case.
  5. Log orchestration-v2.claude-close-dropped-owed-wake at 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.ts over 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=1 removes run_in_background from Bash (checked on 2.1.327), and Monitor and Workflow can go in disallowedTools. 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?

@syanga syanga changed the title fix(server): keep Claude sessions alive for owed wake turns fix(server): an idle Claude thread now wakes when its background command finishes Nov 19, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XS 0-9 changed lines (additions + deletions). vouch:unvouched Author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants