Repository navigation
Keep observing a subagent whose chat outlives its spawning turn - #334072
Ryan Ewen (RyanEwen) wants to merge 2 commits into
Conversation
Subagent chat observations were owned by the parent turn's `_observeTurn` store, so `onTurnEnded` disposed them along with the child chat subscription. When the parent turn completes before the subagent's chat is created, that disposal sends an `unsubscribe` while the client's `subscribe` is still in flight; the server's post-await liveness check then fails it and the protocol handler reports it as `Resource not found`. Nothing re-subscribes afterwards, so the subagent's tool calls are never claimed and its turn never closes. The same ownership breaks background subagents outright: those are designed to outlive their parent turn, so their chat must stay observable after it ends. Subagent observations are now handed off at turn end instead of being torn down. An observation whose child chat is still running, or has not been created yet, moves to a session-scoped map and is released when the subagent settles, when a live in-turn observation supersedes it, or when the chat session is torn down. Observations whose subagent already finished are disposed exactly as before. A detached observer stops emitting into its parent's now-closed response, but keeps the subscription and the tool call claim that let the subagent finish.
There was a problem hiding this comment.
🟡 Changes recommended
One or more issues must be addressed before approval.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Keeps subagent observers alive beyond their spawning parent turn so late tool calls can execute and complete.
Changes:
- Moves live subagent observations into session-scoped storage at turn completion.
- Releases detached observers upon child completion or session teardown.
- Adds lifecycle regression tests.
File summaries
| File | Description |
|---|---|
| src/vs/workbench/contrib/chat/test/browser/agentSessions/agentHostChatContribution.test.ts | Updated as part of this pull request. |
| src/vs/workbench/contrib/chat/browser/agentSessions/agentHost/agentHostSessionHandler.ts | Updated as part of this pull request. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 2
- Review effort level: Balanced
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| return; | ||
| } | ||
| // Before the entry is dropped, so `_hasOtherSessionHold` still pins the shared session subscription. | ||
| this._disposeDetachedSubagentObservations(resolvedSession.toString()); |
There was a problem hiding this comment.
Correct, and this was a real bug in the fix. Fixed in b4245e5, though not by tracking sessionResource per entry.
Per-entry scoping would give the detached observer a shorter lifetime than the thing it exists to feed. The observer's only job is to supply the claimant in _renderedRequests that _watchForSessionInputNeeded requires before it will execute a client tool with chat context, and that watcher is already backend-session scoped and reference counted, disposing only when its last reference goes. Scoping the observer to one chat would mean closing the subagent's own chat strands it while the watcher is still alive and still waiting for a claimant: the same stall, triggered by closing the owning chat instead of a sibling.
So the sweep now defers to the last backend-session holder, using the existing _hasOtherSessionHold, which is there for exactly this shared-per-backend-session problem and is what the shared session subscription already uses. The call moved after _activeSessions.delete, which is that helper's documented contract.
Two tests, and the second is the one that would have caught this. closing a peer chat leaves the default chat's background subagent observer running opens both chats on one backend session, spawns a background subagent, completes the parent turn, disposes only the peer, then has the subagent request a client tool. It asserts who claimed the invocation, not just that it ran, because the tool in question is not confirmable: without an observer the watcher still executes it headlessly and only the context is lost, so asserting execution counts alone would have passed against the buggy code.
Reverting just this hunk reproduces the reviewed code exactly and fails that test on the claim assertion.
AI disclosure: this comment and the related code were written with the assistance of AI.
| await turnPromise; | ||
| await timeout(0); | ||
|
|
||
| assert.strictEqual(agentHostService.hasLiveSubscription(childChatUri), true, 'the not-yet-created subagent chat must stay subscribed past its parent turn'); |
There was a problem hiding this comment.
Correct. The test asserted only that the mock still held a subscription reference, which is not the regression's critical path. Strengthened in b4245e5: after the parent turn completes it now fires the child turn start and the client-contributed tool call actions on the child chat, asserts the detached observer processes that late update and claims the client tool, then completes the child turn and asserts the subscription is released.
The full claim, execute and complete assertions went into the client-tools test file rather than this one, because that file already owns the execution harness and the sibling default-plus-peer chat pattern. Porting its tools-service mock across would have duplicated a convention rather than reused it. That is also where the regression test for the cross-chat teardown lives.
Against main the strengthened test and its two companions all fail; against the previous revision the new peer-chat test fails on the claim assertion alone.
AI disclosure: this comment and the related code were written with the assistance of AI.
…loses Detached subagent observers are shared by every chat of a backend session, but the per-chat teardown disposed all of them. A session can have the default chat and one or more peer chats open at once, so closing an unrelated peer chat tore down a background observer owned by the still-open default chat, leaving the subagent's later tool requests unclaimed again. They now follow the same rule as the session-level input-needed watcher they feed: release only once no sibling chat of that backend session is still active or hydrating, tested through `_hasOtherSessionHold`. Scoping them per chat instead would strand the subagent whenever its own chat closed, even though the watcher that executes its tools outlives that chat. The late-subagent test now exercises the path it names. It emits the child turn and a client-contributed tool call after the parent turn has completed, and asserts the detached observer processes them and claims the tool, rather than only asserting a subscription reference still exists. The new peer-chat test asserts the whole path: with both chats open, closing the peer must leave the subagent's client tool begun, invoked once, completed, and attributed to the default chat that claimed it.
Keeps observing a subagent's chat when that chat outlives the turn that spawned it, so its tool calls are still claimed and executed and its turn can close.
Fixes #333931. #332073 shares this root.
Defect
The client subscribes to a subagent's chat when it first sees the spawning tool call, but registers the release on the disposable store owned by the parent turn. When the parent turn ends, the subscription is torn down. If the subagent chat is created after that point, the in-flight subscribe is cancelled and the client never observes the chat again: nothing re-subscribes, since the server-initiated-turn watcher covers only the default chat and the enrichment path is restore-only.
The consequence is the reported hang. Client tool requests are read from session-level input-needed state, but execution is gated on a claimant that only the chat observer sets. With no observation there is no claim, so the tool is never executed and the subagent's turn never closes. A confirmation dispatched on that chat is likewise never rendered or answered.
Measured on a live host: the spawning tool call completed at 46.189 and the subagent chat was added at 48.316, two seconds after the parent turn had completed at 48.065. Across ten runs the subscribe failed exactly when the parent turn completed before the subagent chat was added, and never otherwise.
Why the fix is here and not in the provider
Making the provider hold the parent turn open until the subagent starts looks smaller but is wrong. Background spawns are specified to outlive the parent turn:
drainForegroundSpawnsevicts only non-background entries and its documentation says background completions arrive later through a task notification, a background spawn's Task result deliberately does not firesubagent_completed, and turn completion is driven by the prompt queue, which never consults the subagent registry. A provider-side hold would wait forever on a background subagent, and on any foreground subagent that forwards no content. A parent-turn-scoped observation is therefore broken for background subagents independently of any race.Fix
Subagent observations are no longer owned by the parent turn's store. At turn end an observation whose child chat has not settled is moved into a session-scoped map instead of being disposed, and is released when the subagent settles, when a live in-turn observation supersedes it, or when the chat session tears down. Settled subagents are disposed exactly as before. The detached observer's progress sink is wrapped so it cannot emit into its parent's closed response, while the tool-call claim and tool-specific data, which do not go through the sink, keep working. A re-entrancy guard stops a nested subagent turn unwinding inside a detached observation from detaching back into the map being torn down.
Tests
a subagent chat created after the parent turn completed is still observeda background subagent stays observed after the parent turn ends and is released when it finishesa subagent that finished during its parent turn is released when that turn endsThe first two fail without the fix.
agentHostChatContribution.test369,agentHostClientTools.test58,stateToProgressAdapter.test149, and the whole agentSessions browser folder at 1338 passing, 0 failing. No new type errors.The re-entrancy guard was not speculative: without it the client-tools suite failed its leak check on the nested second-level subagent test.
Related, not addressed here
_ensureAdditionalChatSubscriptionkeeps one non-refcounted reference per chat URI, so any single holder's release strands every other observer of that chat. The message router drops any message arriving while no turn is in flight, which can discard a background subagent's messages landing between turns. Both deserve their own changes.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.