Repository navigation
Claude agent host: complete background subagents that finish while th… - #348923
Soham Patwardhan (sohampatwardhan) wants to merge 3 commits into
Conversation
…e parent is idle
A background subagent usually finishes after the parent's turn has ended, so its `task_notification` arrives while no turn is queued. `ClaudeSdkMessageRouter.handle` returned before mapping any message without a turn id, so the only signal that completes a background spawn was dropped and the subagent chat stayed active ("N subagents running" never cleared).
Route turn-less `system` messages through `mapSubagentSystemMessage`, which needs no turn id, and log a terminal `task_notification` that closes no subagent chat.
Fixes microsoft#348900
Co-Authored-By: Claude Opus 5.5 <[email protected]>
📬 CODENOTIFYThe following users are being notified based on files changed in this PR: TylerLeonhardtMatched files:
|
|
@microsoft-github-policy-service agree |
There was a problem hiding this comment.
🟡 Changes recommended
Deleting completed spawn records causes harmless duplicate terminal notifications to log at info rather than debug, making them indistinguishable from lost-spawn diagnostics.
1 open issue
What changed in this PR
This PR addresses the idle-parent case in #348900 by allowing Claude background subagents to complete after their parent turn ends, clearing their running status in VS Code. Rebuild and resume reconciliation remain out of scope.
Changes:
- Routes subagent system lifecycle messages when no parent turn is active.
- Adds diagnostics for terminal notifications that do not close a subagent chat.
- Adds tests for idle-parent completion, duplicate notifications, and logging.
| File | Description |
|---|---|
src/vs/platform/agentHost/test/node/claudeSubagentSignals.test.ts |
Tests terminal-notification diagnostics. |
src/vs/platform/agentHost/test/node/claudeSdkMessageRouter.test.ts |
Tests idle-parent completion and duplicate-signal suppression. |
src/vs/platform/agentHost/node/claude/claudeSubagentSignals.ts |
Adds optional logging for unhandled terminal notifications. |
src/vs/platform/agentHost/node/claude/claudeSdkMessageRouter.ts |
Processes subagent system messages without an active parent turn. |
src/vs/platform/agentHost/node/claude/claudeMapSessionEvents.ts |
Passes the logger through to subagent system-message mapping. |
Review effort: Balanced
Track a bounded window of recently completed spawn ids in SubagentRegistry so a duplicate terminal task_notification reaches the "already completed" debug branch instead of the "no open spawn" info branch. Inner-tool edge cleanup in removeSpawn is unchanged. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
| logService?.debug(`[claudeSubagentSignals] task_notification status=${status} for tool_use_id=${m.tool_use_id} ignored; spawn already completed`); | ||
| return []; | ||
| } | ||
| logService?.info(`[claudeSubagentSignals] task_notification status=${status} for tool_use_id=${m.tool_use_id} matches no open subagent spawn; no subagent chat closed`); |
There was a problem hiding this comment.
Fixed in 97ed7e3. A terminal task_notification that matches no open spawn is now logged at debug, and the expectation in claudeSubagentSignals.test.ts is updated.
It affects more than MCP. The SDK sends the same terminal task_notification for background Bash commands (run_in_background, and commands moved to the background on timeout), and their tool_use_id is never a subagent spawn either. So with the old code, every normal background shell completion would have hit this info line.
I didn't try to separate non-subagent tasks before logging. In SDK 0.3.304, SDKTaskNotificationMessage has no task_type; only task_started has one. Telling them apart would mean recording every task_started, and a spawn lost on rebuild has lost its task_started too, so that still couldn't name the case the info line was meant for. The debug message now names both possible causes.
The SDK also sends a terminal task_notification for background Bash commands and auto-backgrounded MCP tool calls. Their tool_use_id is never a subagent spawn, so logging the unmatched case at info fired on every normal background shell or MCP completion. The notification has no task_type to tell these apart from a lost subagent spawn, so log the unmatched case at debug. Co-Authored-By: Claude Opus 5.5 <[email protected]>


Fixes #348900
Problem
In a Claude agent host session, a subagent launched with
run_in_background: truestays "running" after it finishes. The chat footer keeps showing "N subagents running" and the subagent's chat keeps its in-progress status.ClaudeSdkPipeline._processMessagespassesturnId = this._queue.peekParent()?.turnIdtoClaudeSdkMessageRouter.handle. While the parent chat is idle that isundefined, andhandlereturned before mapping the message. A background subagent usually finishes after the parent's turn has ended, so its terminalsystem/task_notificationwas dropped. That notification is the only thing that emitssubagent_completedfor a background spawn, so the subagent chat'sactiveTurnwas never closed. Nothing was logged.Change
claudeSdkMessageRouter.ts: when there is no turn and the message istype: 'system', map it withmapSubagentSystemMessageand fire the signals. That function needs no turn id. Background spawns already survivedrainForegroundSpawns()at turn end, andAgentSideEffects.completeSubagentSessiondoes not need a parent turn. Every other turn-less message is still dropped, as before.claudeSubagentSignals.ts:mapSubagentSystemMessagetakes an optionalILogService. A terminaltask_notificationthat closes no subagent chat is logged atdebug: when the id is missing, when it matches no open spawn (most often a background Bash or MCP task, which the SDK also reports throughtask_notification), or when the spawn has already completed.claudeMapSessionEvents.ts: passes itslogServicethrough.Tests
claudeSdkMessageRouter.test.ts: anAgentspawn,task_startedand the "launched"tool_resultonturn-1, then twotask_notifications withturnId = undefined. Expects exactly onesubagent_completed. Fails onmain, passes with this change.claudeSubagentSignals.test.ts: the new log lines.How to test:
scripts/test.sh --grep "ClaudeSdkMessageRouter|claudeSubagentSignals"Locally:
tsc -p src/tsconfig.json --noEmitreports nothing new in the changed files.node build/oxlint.tsis clean on the 5 files.claudeAgent.test.tsbecause I didn't build the native addons, so I did not run it.gulp hygiene(its dependencies were not installed).git diff --checkis clean and indentation is tabs.To check manually: in a Claude session, have the agent start a background subagent and end its turn. When the subagent reports back, the footer should clear.
Not in this PR
SubagentRegistry. A notification for a spawn from before the rebuild still finds nothing; it is now logged atdebug. Reconciling restored subagent chats with the SDK's task state is a separate change.systemlifecycle messages. See Claude agent host (1.146): a turn's response is silently dropped when the session is rebuilt twice before the turn, and the chat hangs on "This is taking a little longer than usual" #347909 / Agent host: Claude session spins forever after rebind, though turn completed #348078.