Repository navigation
Conversation
… still wakes the agent Claude Code queues a task notification that arrives mid-turn and hands it to the model with the first request after the next tool step. A notification that arrives while the turn's last request is in flight (or before a Stop hook's follow-up request, which carries no queued input) is still queued at the result, and Claude answers it in a turn of its own. The adapter dropped the task from the roster as soon as the notification arrived, so the turn ended with nothing owed. A finished delegate's session release (and any other close that reads the roster) then closed the query while Claude's own follow-up turn was starting, and the agent never saw the result. The adapter now keeps such a task owed until a request after a tool step carries it, and at the turn's end buffers it as an idle wake, so Claude's follow-up turn runs as the wake continuation.
…h a recorded replay Review fixes for the in-turn task notification wake: - Track the root request phase per native thread as frames arrive, so a continuation that attaches during Claude's own wake request keeps it. - Never hold a background command a subagent started; it reports to that subagent. - Keep the wake report recorded during the turn, and drop the reports of tasks a request folded. - When a user turn that started first receives the wake's output, retire the notifications buffered for that wake. - Add a recorded replay, claude_background_task_last_request, in which the command ends during Claude's last request.
…its request phase per process Review fixes for the in-turn task notification wake: - A foreign task-notification result retires only the buffered notification whose run id it names (all of them when the CLI reports none), through replaceWakeBuffer. - Keep the root request phase on the live query context, so a replaced process's late frames cannot clear the new process's phase. - Honour owned_by_subagent on a completion, and record it on a foreground start that moves to the background later; the fallback set lives on the query.
…s run Review fixes for retiring a buffered notification from a foreign task-notification result in result_only echo mode: - A result that names no run retires only notifications without a run id; scheduled triggers and peer deliveries share that origin. - Notifications the result did not answer keep their wake reports, so their own wake still names the command.
… cover The recorded replay claude_background_task_last_request already covers a task that finishes during the last request and its wake. Keep the Stop hook case, the guards that keep a folded or subagent-owned task off the roster, and the result_only retirement cases.
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
|
This PR conflicts with See the conflict window. |
Problem
When a Claude agent's background command finishes while the turn's last model request is in flight, Claude Code queues the notification and answers it in a turn of its own after
result.ClaudeAdapterV2has already dropped the task from the roster by then, so nothing is owed when the turn ends, and anything that closes an idle session kills the turn Claude Code starts for it. In adelegate_taskchild,releaseFinishedDelegateSessionsstarts releasing the session about 0.1 s afterresult, itsquery.closeabout 0.3 s later kills that turn, and the child never answers the notification.Steps to reproduce, timelines and the earlier stalls are in #43838.
Change
ClaudeAdapterV2now tracks Claude's root request phase per process as frames arrive: a request is in flight from a rootstatus: requestinguntil the next root tool result,resultorinit. It is tracked as frames arrive, because buffered frames replay later. Each process keeps its own phase, so a replaced process's late frames don't change the new one's.task_notificationarrives while a root request is in flight stays owed: the published roster still lists it, the same way an idle completion is held.result_onlyecho mode, a user turn that starts before Claude's follow-up turn receives that turn's output. When no wake turn is buffered, its foreigntask-notificationresult now retires the buffered notification it answered, matched by the notification'srun_idagainst the result'sorigin.runId. A result naming no run (scheduled triggers and peer deliveries share that origin) retires only notifications with no run id. Notifications it didn't answer keep their wake reports. Onmainthe same interleaving leaves an idle completion listed for good; this change would otherwise extend that to the in-turn case.main. So is a background command a subagent owns (owned_by_subagenton the start or the completion), since it reports to that subagent and no root turn answers it.Not covered, as on
main: Claude Code snapshots its queue after a tool step's results and then prepares the next request. A notification that lands in that gap, before the nextstatus: requesting, misses the snapshot, and if that request is the turn's last its wake is still lost. The adapter can't see the snapshot on the stream, and holding every such notification would leave a phantom roster entry, which blocks a delegate's release and any settle, whenever the snapshot did take it. Across 945 mid-turn folds in local Claude Code transcripts, that gap had a median of 35 ms and a p90 of 195 ms.Scope
Bug fix in the Claude adapter, one problem: a background task that finishes during a turn's last request loses its wake. Issue: #43838.
#36029 keeps an idle session open while a wake is owed. It covers a task that finishes after the turn has ended. Here the task finishes during the turn, so nothing is owed when it ends; with #36029 applied, the recorded replay below still fails the same way. Two gaps stay as they are on
mainfor idle completions.hasPendingBackgroundWorkand the query-replacement guard read the live roster rather than owed wakes, so a provider switch or a policy change in the fraction of a second before Claude's follow-upinitcan still close the process; #36029 changeshasPendingBackgroundWork. And a wake continuation drains everything buffered, including a later task's notification, which retires that task before its own wake turn.Verification
Recorded replay
claude_background_task_last_request, from Claude Code 2.1.332 (claude-sonnet-4-6): Claude startssleep 4 && echo BG_DONEin the background, then writes a long reply with no further tool calls, so the notification arrives during that last request and Claude answers it in a turn of its own.ClaudeReplayFixtures.integration.test.tsreplays it through the orchestrator, adapter and projections, and checks that the roster at the root run's completion still lists the task, that the wake run repliesWAKE_DONE, and that the roster ends empty with one "Command … finished" notification. Onmainthe wake run still answers, because nothing closes the session in the replay, but the roster at the root run's end is empty, so it fails withexpected [] to deeply equal [ 'b80cmxx3h' ]; with this change it passes. The recording's hook output and task paths are sanitised like the existing fixtures', and the scenario is registered inscripts/record-claude-agent-sdk-replay-fixture.ts.ClaudeAdapterV2.test.ts, for sequences a recording can't produce on demand:owned_by_subagenton the start, on the completion, or on a foreground start later moved to the background.result_onlyecho): its own run, another run, a result naming no run. The last two also check that the task's own wake still names the command.On
mainthe Stop hook case, the three retirement cases and the replay fail; the four guards pass on both. No test covers a new turn's first request folding a notification, a wake request that a continuation joins after it went out, the per-process phase after a query replacement, or the clearing on exit, Stop or replacement. A unit case for the joined wake request passed on this branch and was cut to keep the tests small.ClaudeAdapterV2.test.ts,ClaudeReplayFixtures.integration.test.ts,ClaudeAutomaticDelivery.integration.test.ts,DelegatedCompletionDelivery.test.tsandOrchestratorReplayRecovery.integration.test.tspass on this head, and so doesvp run check:changed. The orchestrator needs no change:DelegatedCompletionDelivery.test.tsalready shows that a listed background task keeps a finished delegate's process.Runtime (UTC, 26 Nov), on an isolated
t3 servewith Claude Haiku 5.5 for parent and child, using the steps in #43838 (the child runssleep 15; echo <token>in the background, callst3_background_wait, then writes one to one thousand in words):task_notificationresultinitmainat the time (9868261)query.close15:01:06.989Made with Claude Opus 5.5 in Claude Code, run from T3 Code. Reviewed by GPT-6.2 Sol in Codex.