Repository navigation
Description
Summary
In a Claude agent host session, subagents started with run_in_background: true keep showing as running after they finish. The chat footer stays at "4 subagents running", and each subagent's read-only chat tab still shows a live status (Working, Considering, Analyzing, Reasoning). This was still the case 30+ minutes after all four had finished.
On the Claude side the subagents ended normally:
- each
subagents/agent-<id>.meta.jsonhas"finished": true; - each subagent transcript ends with its
SubagentHandbacktool call, the tool result{"success":true,"message":"Report delivered to your caller."}, then a successfulSubagentStophook; - the parent conversation received a
<task-notification>with<status>completed</status>and the matching<tool-use-id>for each one, and continued.
Environment
- VS Code 1.146.0 (
69047191fc427f2f92828279aae1d90d8eeda7ab), macOS 27.2.1, arm64 - Agent host Claude SDK
@anthropic-ai/claude-agent-sdk0.3.304 (bundled CLI 2.1.304), fromagent-host/sdk-cache - Claude Code extension 2.1.326
- Session in the Chat view's Claude tab, Auto Mode
Steps to reproduce
- In a Claude agent host session, have the agent launch several subagents with the Agent tool and
run_in_background: true. In my case these were fourfeature-dev:code-reviewer/general-purposeagents that each ran 3–12 minutes and ended throughSubagentHandback. - Let them all finish. The parent gets a completion notification for each one.
- Optional, possibly a trigger: the session is later rebuilt.
agenthost.log:rebinding before turn … for changed client customizations→rebuild isResume=true.
Expected: each subagent's tool part becomes inactive and the "N subagents running" footer clears.
Actual: the footer stays at "4 subagents running" and the four subagent tabs keep their in-progress status. Stopping the tasks from the parent fails with "No task found" because the CLI has already removed them.
What I found in the 1.146.0 code
- The renderer counts a subagent as running when
toolSpecificData.kind === "subagent"andtoolSpecificData.isActive ?? !isComplete(part)._observeSubagentSessionsetsisActive = !!subagentChat.activeTurn. - For a background spawn,
task_startedsetsspawn.background = true. The Agent tool's placeholdertool_resulttherefore doesn't complete it (S && !S.background && S.markCompleted()). The only path that emitssubagent_completedis thesystem/task_notificationhandler. That handler returns[]iftool_use_idis missing, if the status isn'tcompleted/failed/stopped, or ifgetSpawn(tool_use_id)is undefined or already marked completed. Each of these drops the event silently, so the subagent chat'sactiveTurnis never closed. - The bundled CLI 2.1.304 emits
task_notificationwithtool_use_id: task.toolUseId, including thehandback/handback_reportpath. So the id should be present, which suggests the spawn lookup fails or the event is lost (for example across a rebuild). agenthost.logat info level records none of this: there's no log line when atask_notificationis dropped.
Possibly relevant timing:
- All four subagents finished between 13:08 and 13:19 local time.
- The session was rebuilt (
isResume=true) at 13:38:53. - The screenshot showing "4 subagents running" was taken at 13:39:24.
#347909 / #348078 (a rebuilt Claude session never delivers completion to the UI) may share a cause.
A fifth, trivial background subagent started later, after the rebuild, was followed 12 s after it finished by [ProtocolServer] Request 'subscribe' … failed Resource not found: ahp-chat:/<subagent chat>. That error handler is the other place that clears isActive. No such line exists for the four stuck subagents.
Suggestions
- Log (at least at debug) when a
task_notificationdoesn't map to a known spawn. - On resume or rebuild, reconcile restored subagent chats against the SDK's task state or
meta.json(finished: true) instead of leavingactiveTurnopen. - Treat a subagent whose transcript ends with a
SubagentHandbackresult followed bySubagentStopas complete.
Activity
sohampatwardhan commentedon Nov 17, 2026
Root cause, and a patch for the most likely path
I traced this on
main(fc08017c). The problem is beforemapSubagentSystemMessage. In most background runs that function never sees thetask_notification.ClaudeSdkPipeline._processMessagespassesturnId = this._queue.peekParent()?.turnIdtoClaudeSdkMessageRouter.handle. While the parent chat is idle the queue is empty, soturnIdisundefined.handlethen returns before mapping anything:A background spawn usually finishes after the parent's turn has ended (launch, end the turn, wait for the notification), so its terminal
system/task_notificationarrives with no turn queued and is dropped. Nothing else emitssubagent_completedfor a background spawn, and the subagent chat'sactiveTurnstays open. That matches the report: the CLI completed and removed the tasks, the parent got<task-notification>and continued, the footer stayed at "4 subagents running", and nothing was logged. The existing router testhandle with turnId=undefined produces no signalsasserts this drop, but only for a stream event.SubagentSpawndoes not depend on a turn. Background spawns survivedrainForegroundSpawns()at turn end, andAgentSideEffects.completeSubagentSessioncloses the subagent's own turn without needing a parent turn. So the turn-lesssystemmessage can just be routed.Patch
claudeSdkMessageRouter.ts: whenturnId === undefinedand the message istype: 'system', run it throughmapSubagentSystemMessageand fire the resulting signals. Everything else is still dropped while idle, as before.claudeSubagentSignals.ts:mapSubagentSystemMessagetakes an optionalILogService. A terminaltask_notificationthat closes nothing is now logged:infowhen thetool_use_idmatches no open spawn (the rebuild case below),debugwhen the id is missing or the spawn was already completed. This is the first suggestion in the report.claudeMapSessionEvents.ts: passes itslogServicethrough.claudeSdkMessageRouter.test.ts: anAgentspawn,task_started, and the "launched"tool_result, all onturn-1, then twotask_notifications withturnId = undefined. Exactly onesubagent_completedis expected. On unpatchedmainthis test fails (no signal).claudeSubagentSignals.test.ts: covers the new log lines.Verification, run locally on a partial checkout installed with
pnpm install --ignore-scripts:tsc -p src/tsconfig.json --noEmitgives the same error set as unmodifiedmain. The remaining errors are allelectronand excludedextensions/paths.claudeSdkMessageRouter(21),claudeSubagentSignals(12),claudeMapSessionEvents,claudeSubagentRegistry,claudeSdkPipeline(29), andclaudeReplayMapper.claudeAgent.test.tswould not load without the nativevscode_fsaddon, so I did not run it. OneclaudeSavedChatReadertest and oneclaudeAgentSdkServicetest fail for environment reasons only (no nativesqlite3, no test bootstrap forFileAccess).Diff against
fc08017cNot covered by this patch
SubagentRegistry(ClaudeAgentSession.subagents), so a notification for a spawn from before the rebuild still finds no spawn. With this patch it is at least logged atinfo. The restored subagent chats also need reconciling with the SDK's task state, orsubagents/agent-<id>.meta.jsonfinished: true, on resume. That is the report's second suggestion, and it needs a decision on where the restored chat'sactiveTurncomes from. I have not attempted it.turnId === undefinedbranch drops everything the CLI emits for the turn it starts on its own to deliver the<task-notification>(assistant text, tool calls,result). This patch routes only thesystemlifecycle messages. That may be part of 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.Workaround until a fix ships: keep the parent turn open until the background subagents report back, for example by having the agent wait on them in the same turn rather than ending it.
jlgascon-hiberuscom commentedon Nov 18, 2026
I can reproduce the same issue.
The subagents finish successfully, but VS Code continues to display N subagents running indefinitely. The parent agent continues normally, so the displayed subagent state appears to remain stale after completion.
jaibatrik commentedon Nov 26, 2026
This issue is effectively preventing me from using VS Code as the agentic control panel.