Skip to content

Resolve a client tool to the client that is still there - #331300

Closed
Ryan Ewen (RyanEwen) wants to merge 3 commits into
microsoft:mainfrom
RyanEwen:fix/client-tool-owner-after-reconnect
Closed

Ryan Ewen (RyanEwen) wants to merge 3 commits into
microsoft:mainfrom
RyanEwen:fix/client-tool-owner-after-reconnect

Conversation

@RyanEwen

@RyanEwen Ryan Ewen (RyanEwen) commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #331289.

A client tool call is stamped with an owning clientId resolved through SessionClientToolsModel.ownerOf. With no caller-supplied preference that resolved to the first-inserted contributor, and a window reload connects with a new clientId while the departed client's entry can still be present. Every subsequent call was then stamped with a client that had gone away, and failed on arrival:

17:14:38.827  chat/toolCallStart      clientId=<old>
17:14:38.830  chat/toolCallComplete   Client <old> disconnected before completing openBrowserPage
17:14:40.646  chat/toolCallComplete   …another active client now provides openBrowserPage. You may try calling the tool again.

Three milliseconds from start to failure, and the retry advice cannot be taken: the retry resolves to the same departed client. In the session this came from it was still happening 2 hours 8 minutes after that client disconnected, so it is not the disconnect-grace window elapsing — the tool is dead for the life of the session, and the model eventually concludes the tool itself is broken.

The host is simultaneously right and wrong about this: the "another active client provides it" sentence is guarded by _hasReplacementActiveClientTool, which reads state.activeClients and correctly sees the live client, while ownerOf reads the tool model and answers with the dead one. Two registries, permanently disagreeing.

This makes the resolution self-correcting rather than chasing the disagreement. The model now tracks the most recent contributor and prefers it when the caller expresses no preference. A reconnecting window re-pushes its tool list, so the newest contributor is the one most recently known to be alive; an explicit preferredClientId still wins, and removing the latest contributor falls back to whoever remains.

canRefineContributor makes this worth fixing at the point of resolution: a client contributor may only be refined by the same client, so once a call is stamped with a departed id nothing downstream can re-point it.

Scope is deliberately narrow — only the Claude path goes through SessionClientToolsModel. codexMapAppServerEvents.ts:748 resolves through ActiveClientToolSet directly and has the same shape, but I have not reproduced it there and have left it alone.

Two existing tests asserted the first-inserted default and are updated to the new behaviour, since that default is what produced the bug. Added coverage for the reconnect case itself and for the fallback when the latest contributor is removed.

AI disclosure: this description and the code it proposes 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.

A tool call is stamped with an owning clientId from ownerOf, which without a
caller preference returned the first-inserted contributor. A window reload
connects with a new clientId while the departed client's entry can still be
present, so every later call was stamped with a client that had gone away and
failed on arrival — permanently, not for the grace window.

The host is both right and wrong about this at once: the "another active client
provides it" advice reads state.activeClients and sees the live client, while
ownerOf reads the tool model and answers with the dead one. Retrying, as the
advice suggests, resolves to the same departed client.

The model now tracks the most recent contributor and prefers it when the caller
expresses none. A reconnecting window re-pushes its tools, so the newest
contributor is the one most recently known to be alive. An explicit preference
still wins, and removing the latest contributor falls back to whoever remains.

Two tests asserted the first-inserted default, which is what produced the bug,
and are updated. Coverage added for the reconnect case and the fallback.
Copilot AI balanced review requested due to automatic review settings August 17, 2026 19:51

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.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Updates tool ownership resolution to prefer the most recently contributing client, addressing stale client IDs after reconnects, and adjusts/adds tests to match the new ownership policy.

Changes:

  • Track the latest tool contributor and use it as the default preference when resolving tool ownership.
  • Update existing ownership tests to reflect “most recent contributor wins”.
  • Add tests for reconnect and owner fallback after removing the latest contributor.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/vs/platform/agentHost/node/claude/clientTools/claudeSessionClientToolsModel.ts Implements “latest contributor wins” ownership behavior via _latestContributor.
src/vs/platform/agentHost/test/node/clientTools/claudeSessionClientToolsModel.test.ts Updates expectations and adds new test cases covering reconnect and removal behavior.

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

@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/clientTools/claudeSessionClientToolsModel.ts

Review feedback: clearing the latest contributor on removal fell back to
insertion order, which is the behaviour this change exists to avoid. With
three clients, removing the newest resolved to the oldest.

Recency is now an ordered list, so removing a contributor falls back to the
next most recent. Re-contributing moves a client back to the front, which
matters because a reconnect re-pushes a structurally identical tool list and
the merged observable dedupes that write.

Tests cover three contributors removed newest-first, and re-contribution.
@RyanEwen

Ryan Ewen (RyanEwen) commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Both comments were right; fixed and pushed.

Recency needs to be an order, not a single id. Clearing the latest contributor on removal fell back to ActiveClientToolSet's insertion-order default — which is exactly the behaviour this change exists to avoid, so the bug reappeared as soon as a third client was involved. Recency is now an ordered list and removing a contributor falls back to the next most recent.

Coverage for that case added, as suggested: three contributors, removed newest-first, asserting c3 → c2 → c1 rather than dropping to c1.

I added one more that the comment did not ask for but the fix needs. A reconnecting window re-pushes a structurally identical tool list, and merged deliberately dedupes structurally-equal writes, so nothing downstream fires. Recency still has to update on that write, otherwise the reconnect case this PR targets would silently keep resolving to the departed client. The test sets c1, c2, then c1 again and asserts c1 owns the tool.

While verifying this I found that npm run test-node runs mocha directly against out/ and never compiles, and npm run compile-client currently aborts in extensions/markdown-language-features/markdown-editor-src on a stale @vscode/markdown-editor (LinkPresentationKind, ILinkPresentation and friends are not exported), before it reaches src/vs. So local node runs had been silently testing stale output — 14615 tests, missing 102 of them. Transpiling with node build/next/index.ts transpile sidesteps the extension typecheck; on a fresh out/ the suite is 14717 passing, 0 failing, with these tests confirmed executing.

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

@RyanEwen

Ryan Ewen (RyanEwen) commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Note on scope, after trying to widen it.

Codex resolves ownership the same way — codexMapAppServerEvents.ts calls state.clientToolSet.ownerOf(params.item.tool) with no preferred id — and its clients are removed through the same removeActiveClient path, so it is exposed to the same stale-owner failure. I tried moving the recency tracking down into ActiveClientToolSet so both providers were fixed at once.

That breaks Copilot, and the test that catches it is worth reading before anyone else tries the same thing:

CopilotAgent > client tool refresh on reload (#319516)
  multiple active clients merge their tools and removal isolates per client
  expected 'client-A', got 'client-B'

Copilot deliberately ties ownership to the merged dedupe order: merged() dedupes a shared tool name in favour of the first-inserted client, so the SDK is told about client A's description and schema for shared, and ownerOf('shared') must therefore return A. Routing that call to B would execute a tool the model was never shown. Recency-based ownership breaks that pairing for every consumer of the shared class.

So this PR stays scoped to the Claude model, where ownerOf is only used for stamping tool calls. The same misalignment exists there in principle — Claude's merged() still dedupes first-inserted while ownership now prefers recency — but it is only observable when two live clients contribute the same tool name with different schemas, whereas the case this fixes is one client reconnecting and re-pushing an identical list. I would rather have that narrow inconsistency than tool calls that fail for the life of a session.

Fixing codex properly means deciding whether merged() should follow recency too, so dedupe and ownership stay paired. That is a bigger change than this PR and affects what the SDK is told, so I have left codex alone rather than half-fix it.

AI disclosure: this comment was written with the assistance of AI.

@RyanEwen

Ryan Ewen (RyanEwen) commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Verified against a real reconnect, not just by unit test.

I ported this change into an installed agent host, ran a session in a dev container, reloaded the window so the client reconnected with a new id, and then asked for a client-contributed tool. Reading the host log for which client the call was stamped with:

clients announced active, in order of first appearance:
   [0] 3827544b-b077-4cf7-9a2e-fd5707aae845
   [1] 06d38459-c62d-4c1d-b2b5-50ce4d4058f7
   [2] d5b9e8d1-70c8-49f5-a058-1ba325bca2a9
   [3] 53835cdd-9543-42fd-9cae-cd0132406486

client-contributed tool calls:
   openBrowserPage        -> [3] 53835cdd   <= NEWEST (live)

Four client generations accumulated in the tool set across reloads, and the call resolved to the live one. Insertion order would have picked [0], which is the failure in #331289: the call is handed to a client that is gone, fails in about three milliseconds with "disconnected before completing", and the retry advice that follows cannot succeed because the retry resolves the same way.

Note the departed entries are still present — four of them — which is the point. Nothing removed them, and nothing needs to: resolution no longer depends on them being cleaned up.

AI disclosure: this comment was written with the assistance of AI.

The guidelines allow 1-2 short sentences of JSDoc and at most one line
inline. The reasoning belongs in the commit message and the pull request,
which is where it already is.
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: after a client reconnects, new client tool calls are still attributed to the disconnected client and fail immediately

3 participants