Skip to content

Keep a primed subagent spawn alive past the next turn - #334132

Closed
Ryan Ewen (RyanEwen) wants to merge 1 commit into
microsoft:mainfrom
RyanEwen:fix/subagent-client-tool-routing
Closed

Ryan Ewen (RyanEwen) wants to merge 1 commit into
microsoft:mainfrom
RyanEwen:fix/subagent-client-tool-routing

Conversation

@RyanEwen

@RyanEwen Ryan Ewen (RyanEwen) commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

Keeps a subagent spawn that was primed from the transcript alive past the next turn, so a restored subagent's identity survives and a background subagent that outlived a host restart can still complete.

Defect

SubagentRegistry doubles as the agent-id cache the subagent transcript resolver reads, and primeFromTranscript fills it on resume. Those records were written through recordSpawn, so they looked like ordinary in-flight foreground spawns, and drainForegroundSpawns removed every one of them at the end of the next turn while warning that a pending spawn had been abandoned.

Two consequences. The resolver's warm cache is destroyed one turn after priming, so reopening a restored subagent chat re-runs the whole strategy chain. More seriously, a background subagent that outlived a host restart has no task-started event in the resumed stream to mark it as background, so its record is drained too, and the later task notification finds no spawn to complete.

A live foreground spawn is already removed on its own tool result, so the drain was catching almost nothing else.

Fix

A spawn records whether it is actually in flight. The drain now takes only in-flight foreground spawns, and priming records an agent id without claiming the spawn is running.

Tests

  • drainForegroundSpawns keeps agentId-only records, so a spawn primed from the transcript survives the first resumed turn, driving the real priming path
  • a background subagent known only from the transcript still completes after a turn ends, driving the mapper with a result followed by a task notification

Reverting the gate fails both; the second is the substantive one, producing no completion signal at all. 586 passing across the Claude agent, subagent signals, mapper, side effects, registry, resolver, router and replay suites. No new type errors.

Scope

This does not touch the message router. Two open PRs already cover the messages that arrive while no turn is in flight: #331872 routes the background subagent's system notification, and #332075 maps resumed output under the spawn's turn. This change is what makes the spawn still be there when those messages arrive.

Its visible payoff for the restart case is gated on a separate gap: the restore path never creates a subagent chat entry, so the completion this restores currently has nowhere to land. The fix is correct and prerequisite either way, and the registry and mapper behaviour is asserted directly.

Part of #333174.

AI disclosure: this comment and the related code were written with the assistance of AI.

Public patches and patcher scripts

Source patch and installed-bundle workaround mapping. The public collection includes the maintained patchers, rollback instructions, regression scripts, and historical snapshots. Build restrictions and exact installer coverage are documented there.

`SubagentRegistry` doubles as the agentId cache that `getSubagentTranscript`
reads, and `primeFromTranscript` fills it on resume from the Task tool_result
suffixes in the parent transcript. Those records described Tasks that had
already produced a result, but they were written through `recordSpawn`, so
`drainForegroundSpawns` treated every one of them as an in-flight foreground
spawn and dropped it at the end of the next turn, with a warning claiming a
pending spawn had been abandoned.

Two things went with it. The resolver lost its warm cache one turn after
priming it, so reopening a restored subagent chat re-ran the whole strategy
chain. Worse, a background subagent that outlived a host restart has no
`task_started` in the resumed stream to mark its spawn background, so its
record was drained too and the later `system.task_notification` no longer
found a spawn to complete.

Record an agentId learned from a transcript through a separate `cacheAgentId`
entry point that does not claim the spawn is in flight, and drain only spawns
this process saw start.
@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/claudeSubagentRegistry.ts
  • src/vs/platform/agentHost/node/claude/claudeSubagentResolver.ts

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

The focused lifecycle fix is internally consistent and covered by relevant regression tests.

Pull request overview

Preserves transcript-primed subagent identities across turn cleanup so restored background subagents can complete correctly.

Changes:

  • Distinguishes live spawns from agent-ID-only cache entries.
  • Retains cached records during foreground draining.
  • Adds regression coverage for restoration and completion.
File summaries
File Description
claudeSubagentSignals.test.ts Tests restored background completion.
claudeSubagentRegistry.test.ts Tests cache retention during draining.
claudeSubagentResolver.ts Uses the non-live cache API.
claudeSubagentRegistry.ts Tracks in-flight state and preserves cached identities.
Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 0
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

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.

3 participants