Repository navigation
Admit a client tool call once, instead of racing two triggers - #334146
Closed
Ryan Ewen (RyanEwen) wants to merge 2 commits into
Closed
Ryan Ewen (RyanEwen) wants to merge 2 commits into
Ryan Ewen (RyanEwen) wants to merge 2 commits into
Conversation
A client tool call had two independent triggers and no single record of ownership. The stream mapper's ready action drove the session-input-needed contribution to publish an execution request, while the in-process MCP handler separately parked the SDK. Nothing tied "a client owns this call" to "the runtime is parked waiting for it", so an ownerless or unclaimed call could hang, a replayed call had no owner, and waiting for a claim was unbounded. Add a `client_tool_invoked` provider signal carrying the root chat, the SDK `tool_use_id`, the tool name, the input, and a subagent's `parentToolCallId`. The Claude session fires it from `registerAndFire` where the MCP handler parks, so a result buffered before the SDK asked for it short-circuits without firing. Providers opt in through `IAgent.drivesClientToolExecution`; Copilot and Codex keep executing on the streamed ready. AgentSideEffects owns the admission record (invoked, admitted, settled, abandoned) and stamps the admission into the tool call's protocol `_meta`, so it survives a state snapshot and a client reconnect. The session-input-needed contribution publishes a client execution request only for an admitted call, and only for a provider that opts in. A call the runtime replays with no prior state has its start and ready synthesized with the owner; a call no connected client provides is failed once and forwarded once as `toolUnavailable`; an admitted call no connected client claims within the existing client-disconnect window is failed the same way, while a call whose owner is still connected is left to run. The disconnect window moves from protocolServerHandler to agentHostClientConnectionService so both uses share one number, and a rebind now keeps buffered results instead of discarding an answer the SDK is about to ask for again.
This was referenced Sep 2, 2026
Contributor
📬 CODENOTIFYThe following users are being notified based on files changed in this PR: TylerLeonhardtMatched files:
|
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Duplicate and late invocations can leave Claude client-tool promises unresolved indefinitely.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Adds runtime-driven admission for Claude client tools, preventing competing execution triggers and preserving admission across reconnects.
Changes:
- Adds the
client_tool_invokedsignal and provider opt-in. - Tracks admission, repairs runtime input, and bounds abandoned calls.
- Preserves buffered results across Claude rebinds and expands tests.
File summaries
| File | Description |
|---|---|
common/agent.ts |
Defines the signal and provider capability. |
common/meta/agentToolCallMeta.ts |
Persists admission metadata. |
common/pendingRequestRegistry.ts |
Optionally retains buffered results. |
node/agentHostClientConnectionService.ts |
Exposes the shared timeout. |
node/agentSideEffects.ts |
Implements admission and failure handling. |
node/chatContributions/sessionInputNeeded/sessionInputNeededContribution.ts |
Gates execution requests on admission. |
node/claude/claudeAgent.ts |
Enables Claude runtime-driven execution. |
node/claude/claudeAgentSession.ts |
Emits invocation signals and parks results. |
node/claude/claudeSdkOptions.ts |
Wires the invocation callback. |
node/claude/clientTools/claudeClientToolMcpServer.ts |
Forwards tool names and runtime arguments. |
node/protocolServerHandler.ts |
Uses the shared timeout constant. |
test/common/pendingRequestRegistry.test.ts |
Tests retained buffered results. |
test/node/agentSideEffects.test.ts |
Tests admission and timeout behavior. |
test/node/claudeAgent.test.ts |
Tests Claude and subagent signals. |
test/node/clientTools/claudeClientToolMcpServer.test.ts |
Tests callback arguments. |
test/node/mockAgent.ts |
Adds configurable provider opt-in. |
Review details
Suppressed comments (2)
src/vs/platform/agentHost/node/agentSideEffects.ts:1319
- This admission state machine, timeout ownership, and terminal-action cleanup are cross-cutting chat lifecycle behavior, not routing fabric. The repository contribution model requires such behavior to live in a dedicated chat contribution (see
node/chatContributions/TODO.md:3-7and the existingSessionInputNeededContribution); keeping ~130 lines here reverses the ongoing AgentSideEffects extraction and bypasses contribution ordering/failure isolation. Please retain only signal/subagent routing here and move admission lifecycle ownership behind the contribution model.
private _handleClientToolInvoked(signal: IAgentClientToolInvokedSignal, chatChannel: ProtocolURI, turnId: string): void {
const key = this._clientToolAdmissionKey(chatChannel, signal.toolCallId);
if (this._clientToolAdmissions.has(key)) {
// A rebind re-parks the same call; its admission already stands.
return;
src/vs/platform/agentHost/node/agentSideEffects.ts:1358
- This is the safety-critical branch that prevents admission from bypassing a pending user confirmation, but the new tests only cover calls that transition directly to
Running. Add a regression that invokes a call while it isPendingConfirmation, verifies no client-execution request is published, then approves it and verifies exactly one request is published with the admission metadata preserved.
...(streamed?.status === ToolCallStatus.PendingConfirmation ? {} : { confirmed: toolCallConfirmation(streamed) ?? ToolCallConfirmationReason.NotNeeded }),
- Files reviewed: 16/16 changed files
- Comments generated: 2
- Review effort level: Balanced
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
…ed one Two holes the admission path left open. A `client_tool_invoked` arriving with no active turn was logged and dropped, but the provider parks its deferred before firing the signal, so it waited until session disposal. It is now failed back through the same completion path the ownerless case uses. `registerAndFire` replaced an entry that was still parked, unlike `register`, which cancels the superseded deferred deliberately. Since the admission guard suppresses the duplicate execution, only one response is ever produced and it settled only the newer promise. A duplicate now awaits the parked request and does not fire again, so the single response settles both callers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Gives a client tool call a single event that says a client now owns it, so execution is no longer driven by two independent triggers racing.
Supersedes #330933.
The problem
Two things independently decide that a client tool should run: the stream mapper's ready action, which drives the session-input-needed contribution to publish an execution request, and the in-process MCP handler registering the parked request. Nothing connects "a client owns this" to "the SDK is parked waiting for it", so a replayed call has no owner, an unclaimed call waits forever, and there is no bound on either.
What this adds
A
client_tool_invokedsignal carrying the root chat, the SDK tool use id, the tool name, the input and the parent tool call id for a subagent. The Claude session fires it from the registry's fire-on-register path at the moment the MCP handler parks, so a result buffered before the SDK asked for it short-circuits and never fires. Providers opt in, and only Claude does, so Copilot and Codex take the unchanged path.Admission is recorded in the side effects, next to the tool-call tracking already there, and stamped into the tool call's protocol meta rather than living only in a process-local map, so it survives a client reconnect. The stamp rides a re-dispatched ready that also repairs the input from the runtime's arguments. Two details that took reading rather than assuming: the reducer replaces the meta bag wholesale rather than merging, so the streamed bag is carried forward explicitly, following the existing precedent for the auto-approve stamp; and a call sitting in pending confirmation is re-readied without the confirmed flag, which the reducer treats as a refresh, so admission can never bypass a user confirmation.
The contribution then publishes a client execution request only for an admitted call, and only for an opted-in provider.
An admitted call that nobody claims within the existing client disconnect window is failed with the same error shape the ownerless path uses. The constant was moved rather than duplicated, into the dependency-light connection service both users already import. The timer no-ops if the call settled or its owning client is still active, so it cannot fire while a client is running the tool. The failure is dispatched as a server action and forwarded to the provider exactly once by the existing envelope listener, which is why the proposal's explicit forward is not carried over: on current main it would forward twice.
Rebind now keeps buffered results, because the fire-on-register path consults that buffer and a destroyed buffer would turn an answered call into a second admission. Admission is also idempotent host-side, so a re-fired invocation for a known call returns early.
Why the proposal could not simply be rebased
#330933's base was closed, and the publish point it patches has since moved out of the side effects into a chat contribution, so its central hunk no longer exists. What was worth keeping is kept: the signal shape, the opt-in flag and its placement on the agent rather than the wire-visible capability bag, repairing the input from the runtime arguments, synthesising start and ready with the resolved owner for a replayed call, the registration ordering, and its replay and subagent test intents.
Scope
No protocol change; admission rides the existing open meta bag and nothing under the synced protocol directory is touched. The mapper is unchanged, the workbench is unchanged, and the bound does not re-arm after no-opping for a connected owner, since a client that claims and then vanishes is the disconnect sweep's job.
Tests
Eleven new, including that a streamed ready alone does not publish for an opted-in provider, that an invocation admits and repairs the input and publishes exactly one request, that a non-opting provider still publishes on ready, that an ownerless call fails once and forwards once, that the bound does not fire while the owner is connected and does once it is gone, that a replayed call synthesises start and ready with the owner, and that a subagent call is admitted on the subagent chat and completes back on the root chat.
Reverting only the gate fails the streamed-ready test. Across the agent host suites: 5227 passing in node, 731 in common, Copilot 1059, Codex 402, all with zero failures. No new type errors.
Merge note
This conflicts with #334127 in one place worth deciding rather than resolving mechanically: that PR tags the owning client inside the MCP server builder, while this replaces that parameter with a callback. The tagging belongs in the session's await path, which already holds both the tool diff and the registry, whichever branch carries it.
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.