Skip to content

Proposal: execute a client tool on the runtime's invocation - #330933

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

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

Conversation

@RyanEwen

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

Copy link
Copy Markdown
Contributor

Proposal, and easy to decline. #330899 asks whether the stream-mapper ready is meant to be actionable. This is what the alternative looks like, offered so the option is concrete rather than hypothetical. If the current ordering is deliberate, closing this costs nothing and the three symptom PRs still stand on their own.

Stacked on #330746, which introduces the client_tool_invoked signal this builds on.

The seam

A client tool is executed when its streamed tool call reaches Running (agentSideEffects.ts:566-575), in parallel with the runtime invoking the same tool through its in-process MCP server. The MCP handler discards its arguments and parks, so the streamed ready is the active trigger and the invocation is a passive await.

Three defects follow from that one trigger:

Defect Why the trigger allows it PR
The tool runs twice The ready fires from the stream mapper and again from the permission flow #330683
The tool runs with {} A synthesized ready carries no toolInput #330684
The result is dropped Execution can finish before the SDK registers the handler that awaits it #330730

The change

A provider may declare drivesClientToolExecution, meaning its runtime reports every client tool invocation. For those providers the host admits a call to execution only once the invocation arrives, rather than on the streamed ready. Claude opts in, since its in-process MCP server sees every call the SDK makes. Providers with no such signal are untouched, so Copilot and Codex keep executing on Running.

Because the invocation carries the runtime's own arguments, a streamed call that arrived without input is repaired from them before it runs.

All three defects then become structurally impossible for Claude rather than individually patched: one trigger instead of two, real arguments instead of {}, and registerAndFire ordering registration before execution.

What this costs

Execution starts at the SDK round trip rather than when the arguments finish streaming. If that latency is the reason the ready is actionable today, this is the wrong trade and I would rather know than guess. That is the question in #330899.

Verification

New test: with a provider that announces invocations, a streamed ready produces no execution request and the invocation does. 5298 agent host tests pass; the 3 failures are present on main unchanged.

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.

@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/claudeAgent.ts
  • src/vs/platform/agentHost/node/claude/claudeAgentSession.ts
  • src/vs/platform/agentHost/node/claude/claudeSdkOptions.ts
  • src/vs/platform/agentHost/node/claude/clientTools/claudeClientToolMcpServer.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.

Pull request overview

Moves Claude client-tool execution from streamed readiness to runtime invocation signals, preventing duplicate, premature, and argument-less execution.

Changes:

  • Adds invocation-driven execution capability and routing.
  • Forwards authoritative tool arguments from Claude’s MCP handler.
  • Adds tests for signaling, replay, and execution ordering.

Reviewed changes

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

Show a summary per file
File Description
common/agent.ts Defines the invocation signal and provider capability.
node/agentSideEffects.ts Gates and synthesizes client-tool execution.
node/claude/claudeAgent.ts Enables invocation-driven execution for Claude.
node/claude/claudeAgentSession.ts Emits invocation signals and awaits results.
node/claude/claudeSdkOptions.ts Wires the invocation callback into MCP setup.
node/claude/clientTools/claudeClientToolMcpServer.ts Forwards tool names and arguments.
test/node/agentSideEffects.test.ts Tests replay and invocation ordering.
test/node/claudeAgent.test.ts Tests Claude invocation signaling.
test/node/clientTools/claudeClientToolMcpServer.test.ts Tests MCP argument forwarding.
test/node/mockAgent.ts Adds configurable invocation capability.

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

@RyanEwen
Ryan Ewen (RyanEwen) force-pushed the fix/client-tool-single-trigger branch from fc7452f to deda8ce Compare August 14, 2026 19:45
@RyanEwen

Ryan Ewen (RyanEwen) commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor Author

Two of the three hold and are fixed in #330746, since that is where the lines live; this PR is rebased on it.

Subagent routing. Correct, and it would have been a hang rather than a cosmetic issue: with the gate on, the real subagent call never receives an execution request. The signal now resolves the parent spawn the same way the permission path does, via subagents.getParentSpawn(toolUseId), and carries parentToolCallId when present. Regression test added: a tool invoked under a recorded spawn announces its parent.

Unownable call wedging the turn. Also correct. The MCP handler is already parked when the signal fires, so returning after the log left the runtime awaiting forever. The call is now completed as a failure both in protocol state and to the provider, so the turn unwinds with an attributable error instead of hanging.

JSON.stringify typing. This one does not reproduce: JSON.stringify(value: any): string is the overload selected here, and npm run typecheck-client is clean before and after. I added the ?? '{}' fallback anyway, since it costs nothing and documents the intended empty-object case.

5299 agent host tests pass, with the same 3 failures present on main.

Still very much a proposal, and still easy to decline: the latency question in #330899 is the part I cannot answer from outside.

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

A field note from running an approximation of this against a live dev container for a day, in case it is useful evidence for the design.

I patched a local build to gate client-tool execution on the runtime invocation, as this PR does, but took a shortcut: my version resynced using a turn id stashed on the session when a message was sent, rather than carrying the turn id with the invocation.

That shortcut broke, and the failure is worth knowing about. Client tool calls issued inside a steering turn were stranded permanently — readied, never executed, and the turn never completed. Three cases in one session, all of them in steering-promoted turns, none in ordinary turns. The reason is that a promoted steering turn is not created through the ordinary send path, so the stashed id still pointed at the preempted turn; the resync then asked for a request under the wrong turn, matched nothing, and no execution was ever requested. Nothing was logged, because the resync path had no logging.

This PR does not have that problem: _handleClientToolInvoked(signal, sessionKey, turnId) receives the turn id along with the invocation rather than reading it from session state. That distinction is load-bearing and easy to lose in a refactor, so it may be worth a comment or a test that pins it — a steering turn is exactly the case where the two sources diverge.

Fixing my version to record each call's own turn id at the point it streams resolved it, confirmed on live traffic: the diagnostic showed the two ids genuinely differing (turn=request_3cd585fe… stashed=request_b839df59…) with the tool then executing normally.

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

@RyanEwen

Copy link
Copy Markdown
Contributor Author

Housekeeping on the review threads above, since the branch was squashed after they were written.

#330746 was closed rather than merged and its changes are folded into this PR's single commit, so 3491889 and ddbcb432 cited in the threads are no longer on this branch. At head:

  • A client tool invoked inside a subagent carries parentToolCallId, resolved from subagents.getParentSpawn(toolUseId) in _awaitClientToolResult, and _handleAgentSignal routes the signal to the subagent chat. Test: a client tool invoked inside a subagent carries its parent so the host routes it to the subagent chat.
  • A replayed call that no active client provides is completed as a failure (toolUnavailable) in _handleClientToolInvoked, with toolCallStart and toolCallReady dispatched first so the failure is visible, and onClientToolCallComplete called so the parked MCP handler unwinds. Test: a replayed call no client provides completes as a visible failure.

One correction to my earlier comment: the ?? '{}' fallback on JSON.stringify is not in the current revision. The lib the repository compiles against declares JSON.stringify(value: any, ...): string, so the fallback was unreachable and was dropped in the squash rather than kept as documentation.

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

@RyanEwen

Copy link
Copy Markdown
Contributor Author

Superseded by #334146, which I am closing this in favour of.

The proposal was right about the shape of the problem and its signal, and #334146 keeps that: the same client_tool_invoked event, the provider opt-in flag, repairing the input from the runtime's arguments, synthesising start and ready with the resolved owner for a replayed call, the registration ordering, and the replay and subagent test intents.

It could not be rebased. Its base #330746 was closed, and the publish point it patches has since moved out of agentSideEffects into a chat contribution, so its central hunk no longer exists on main.

Three things changed in the reimplementation, each for a reason worth recording. Admission is stamped into the tool call's protocol meta rather than living only in a process-local set, so it survives a client reconnect. The ownerless failure is dispatched as a server action and left to the existing envelope listener, because the explicit forward this proposal added would deliver it twice on current main. And the input is always repaired from the runtime's arguments rather than only when the streamed input looks empty, since the runtime's are authoritative.

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

@vs-code-engineering vs-code-engineering Bot locked and limited conversation to collaborators Oct 21, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants