Skip to content

Execute a client tool the SDK replays from the transcript - #330746

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

Ryan Ewen (RyanEwen) wants to merge 1 commit into
microsoft:mainfrom
RyanEwen:fix/claude-replayed-client-tool-execution

Conversation

@RyanEwen

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

Copy link
Copy Markdown
Contributor

Companion to #330730; together they close the remaining ways a client tool call wedges a Claude session in a remote window (#322990).

The failure

Cancel a turn while a client tool call is pending and the transcript keeps an assistant tool_use with no tool_result. On the next message the SDK resumes and replays that call: it invokes the in-process MCP tool directly, without re-streaming the assistant message. The stream mapper — the sole trigger for client-tool execution — sees nothing, so no execution is ever requested. The SDK waits forever, emitting only tool_progress heartbeats, and the UI sits at "Considering".

Worse, the dangling tool_use is durable: every subsequent resume replays it, so the session is permanently wedged. Observed live on 1.133.0:

transcript tail:  assistant  tool_use: mcp__client__openBrowserPage (toolu_01Kx9CBY…)   ← no result
21:22:49  chat/turnStarted            ← resume
          (no stream events, no toolCallStart — nothing on the wire)
21:23:10  tool_progress toolu_01Kx9CBY…-heartbeat-0
21:23:39  tool_progress …-heartbeat-1   (forever)

The fix

The MCP handler already receives everything needed — the tool name, the args, and the tool_use_id. It now parks via PendingRequestRegistry.registerAndFire, firing a client_tool_invoked signal. The host checks turn state:

  • The call streamed normally → it has protocol state and a client already executing it → the signal is a no-op.
  • The call is absent (replay) → the host synthesizes the ChatToolCallStart / ChatToolCallReady pair the stream would have produced, which drives the existing ToolClientExecution request path, so the client executes it like any other call.

registerAndFire collects a result buffered before registration without firing, so this composes with #330730's eager-execution buffering rather than re-triggering execution.

The synthesis is deliberately host-side (agentSideEffects), where turn state lives — the reducer appends blindly on ChatToolCallStart, so the session cannot safely emit unconditionally.

Testing

  • an SDK client tool invocation fires client_tool_invoked and resolves on completion — session-level, through the real MCP handler wiring.
  • client_tool_invoked synthesizes execution for a replayed call and leaves a streamed one alone — host-level: the replayed call gains a Running tool call with the owning client's contributor and a ToolClientExecution request; a second signal for the same id does not duplicate state.
  • handler forwards the tool name and args to awaitResult — factory-level.
  • Suites: AgentSideEffects 220 passing, ClaudeAgent 266 passing / 3 failing (same 3 on an unmodified checkout — model catalog / credential tests, unrelated), buildClientToolMcpServer 6 passing.
  • npm run typecheck-client clean.

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.

Copilot AI balanced review requested due to automatic review settings August 13, 2026 21:38
@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/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

Adds recovery for Claude SDK client-tool calls replayed without stream events.

Changes:

  • Introduces a client_tool_invoked signal carrying replay details.
  • Synthesizes protocol state and client execution requests for replayed calls.
  • Adds coverage across MCP handling, session signaling, and host side effects.

Reviewed changes

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

Show a summary per file
File Description
src/vs/platform/agentHost/common/agent.ts Defines the new invocation signal.
src/vs/platform/agentHost/node/agentSideEffects.ts Synthesizes replayed tool-call execution.
src/vs/platform/agentHost/node/claude/claudeAgentSession.ts Parks invocations and emits signals.
src/vs/platform/agentHost/node/claude/claudeSdkOptions.ts Plumbs the result callback.
src/vs/platform/agentHost/node/claude/clientTools/claudeClientToolMcpServer.ts Forwards tool names and arguments.
src/vs/platform/agentHost/test/node/agentSideEffects.test.ts Tests synthesized execution and deduplication.
src/vs/platform/agentHost/test/node/claudeAgent.test.ts Tests session signaling and completion.
src/vs/platform/agentHost/test/node/clientTools/claudeClientToolMcpServer.test.ts Tests MCP argument forwarding.

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

@connor4312

Copy link
Copy Markdown
Member

cc Tyler for Claude specifics

@RyanEwen

Ryan Ewen (RyanEwen) commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor Author

Pushed two fixes from review on #330933, both in code this PR introduces.

A client tool invoked inside a subagent was announced without its parent, so the signal was handled on the parent chat rather than the subagent one. It now resolves the parent spawn as the permission path does and carries parentToolCallId, with a regression test.

A call whose tool no connected client provides left the runtime parked forever, since the MCP handler is already awaiting when the signal fires. It is now completed as a failure in protocol state and to the provider, so the turn unwinds instead of hanging.

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

@RyanEwen

Copy link
Copy Markdown
Contributor Author

Two notes from reviewing this alongside #331300, since both decide which client owns a tool call and they no longer agree.

Ownership is resolved three ways after these land. The stream stamp in claudeMapSessionEvents goes through the Claude tool model, which #331300 makes prefer the most recent contributor. The replay path here picks state.activeClients.find(client => client.tools.some(...)), the first active client that provides the tool. Copilot and Codex resolve through ActiveClientToolSet.ownerOf, first-inserted and deliberately paired with the merged dedupe order. In the reload case that motivated #331300 this is fine, because state.activeClients is cleaned reliably (it is the tool model that goes stale), so find lands on the live client. With two live windows on one session, though, a streamed call routes to the newest and a replayed call to the first-listed. Not a failure, but worth choosing one rule; I would rather this path asked the provider than re-derived it.

Cosmetic: JSON.stringify(args ?? {}) ?? '{}' has a dead right-hand side; stringify of an object never returns undefined.

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

@RyanEwen
Ryan Ewen (RyanEwen) force-pushed the fix/claude-replayed-client-tool-execution branch from 2ce84ab to 1fe9d0e Compare August 28, 2026 16:08
@RyanEwen

Copy link
Copy Markdown
Contributor Author

Closing in favour of #330933, which supersedes this.

Both PRs move client tool execution off the stream-mapper ready and onto the runtime's invocation, and #330933 is the general form: four of the five tests here also exist there, including the replayed-call cases this PR was opened for.

  • client_tool_invoked synthesizes execution for a replayed call and leaves a streamed one alone
  • an SDK client tool invocation fires client_tool_invoked and resolves on completion
  • a client tool invoked inside a subagent carries its parent so the host routes it to the subagent chat
  • handler forwards the tool name and args to awaitResult

The one test unique to this PR is a replayed client tool invocation routes to the subagent chat that owns the call. If that case is worth keeping as its own coverage, it is a small addition to #330933 rather than a separate change.

Both are rebased onto current main, so nothing is lost by consolidating here.

AI disclosure: this comment and the related code 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