Skip to content

Claude agent host: complete background subagents that finish while th… - #348923

Open
Soham Patwardhan (sohampatwardhan) wants to merge 3 commits into
microsoft:mainfrom
sohampatwardhan:fix/348900-background-subagent-completion
Open

Soham Patwardhan (sohampatwardhan) wants to merge 3 commits into
microsoft:mainfrom
sohampatwardhan:fix/348900-background-subagent-completion

Conversation

@sohampatwardhan

@sohampatwardhan Soham Patwardhan (sohampatwardhan) commented Nov 17, 2026 •

Copy link
Copy Markdown

Fixes #348900

Problem

In a Claude agent host session, a subagent launched with run_in_background: true stays "running" after it finishes. The chat footer keeps showing "N subagents running" and the subagent's chat keeps its in-progress status.

ClaudeSdkPipeline._processMessages passes turnId = this._queue.peekParent()?.turnId to ClaudeSdkMessageRouter.handle. While the parent chat is idle that is undefined, and handle returned before mapping the message. A background subagent usually finishes after the parent's turn has ended, so its terminal system / task_notification was dropped. That notification is the only thing that emits subagent_completed for a background spawn, so the subagent chat's activeTurn was never closed. Nothing was logged.

Change

  • claudeSdkMessageRouter.ts: when there is no turn and the message is type: 'system', map it with mapSubagentSystemMessage and fire the signals. That function needs no turn id. Background spawns already survive drainForegroundSpawns() at turn end, and AgentSideEffects.completeSubagentSession does not need a parent turn. Every other turn-less message is still dropped, as before.
  • claudeSubagentSignals.ts: mapSubagentSystemMessage takes an optional ILogService. A terminal task_notification that closes no subagent chat is logged at debug: when the id is missing, when it matches no open spawn (most often a background Bash or MCP task, which the SDK also reports through task_notification), or when the spawn has already completed.
  • claudeMapSessionEvents.ts: passes its logService through.

Tests

  • claudeSdkMessageRouter.test.ts: an Agent spawn, task_started and the "launched" tool_result on turn-1, then two task_notifications with turnId = undefined. Expects exactly one subagent_completed. Fails on main, 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 --noEmit reports nothing new in the changed files.
  • node build/oxlint.ts is clean on the 5 files.
  • The Claude agent-host unit suites pass under mocha. I could not load claudeAgent.test.ts because I didn't build the native addons, so I did not run it.
  • I did not run gulp hygiene (its dependencies were not installed). git diff --check is 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

…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]>
Copilot AI balanced review requested due to automatic review settings November 17, 2026 20:04
@vs-code-engineering

Copy link
Copy Markdown
Contributor

📬 CODENOTIFY

The following users are being notified based on files changed in this PR:

TylerLeonhardt

Matched files:

  • src/vs/platform/agentHost/node/claude/claudeMapSessionEvents.ts
  • src/vs/platform/agentHost/node/claude/claudeSdkMessageRouter.ts
  • src/vs/platform/agentHost/node/claude/claudeSubagentSignals.ts

@sohampatwardhan

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 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]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues remain for the change handling subagent completion notifications while the parent chat is idle.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Normal completions of auto-backgrounded MCP tools trigger misleading info-level missing-spawn diagnostics, diluting the signal for genuinely lost subagent completions.

1 open issue

Review effort: Max

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`);

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.

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]>

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Agent host (Claude): background subagents stay "running" after they finish ("4 subagents running" never clears)

4 participants