Skip to content

fix: match parallel tool calls by tool use id instead of tool name - #110

Merged
patoles merged 2 commits into
patoles:mainfrom
jjohnnystwsss:fix/107-tool-call-id-matching
Nov 20, 2026
Merged

patoles merged 2 commits into
patoles:mainfrom
jjohnnystwsss:fix/107-tool-call-id-matching

Conversation

@jjohnnystwsss

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #107

Parallel calls to the same tool were deduplicated by agent + tool name inside the 3 s window, so three parallel Read calls became one node with only the first file in file attention. Results were also matched by name, so finishing the second call completed the first one. This follows the direction in the issue thread: the ids already exist, so this change sends them to the web view and matches on them.

Extension (emit toolUseId on tool_call_start and tool_call_end)

  • transcript-parser.ts: the tool_use block's id on start, and the tool_result's tool_use_id on end.
  • hook-server.ts: the hook payload's tool_use_id on PreToolUse, PostToolUse and PostToolUseFailure.
  • codex-rollout-parser.ts: call_id for function_call / custom_tool_call and their outputs. web_search_call has no id, so it keeps name matching (start and end are emitted together anyway).
  • protocol.ts: documents the new payload field. Payloads are still Record<string, unknown>, so this is a doc comment and not a new type.

Web view (handle-tool-events.ts)

  • Tool call nodes are keyed on tool-<agent>-<toolUseId> when an id is present, and store toolUseId (new optional field on ToolCallNode). Parallel calls processed in the same frame no longer overwrite each other.
  • Dedupe: when both the event and an existing node have an id, a start is a duplicate only if the ids are equal (any state, no time window). The hook server and the transcript report the same id, so the second report is dropped. A late duplicate that arrives after the call finished no longer creates a new node either.
  • End: completes the running call with the same id. The file-attention token cost goes to that call's file, not to the first call with the same name.
  • Fallback: when either side has no id (older recordings, mock and stress scenarios, hook payloads without tool_use_id), the old agent + tool name matching and the 3 s window apply unchanged.

Test setup (package.json)

  • The root test script now runs the web/ tests with TSX_TSCONFIG_PATH=web/tsconfig.json. handle-tool-events.ts imports through the @/ alias, which tsx can't resolve without the web tsconfig. The scripts/ and app/ tests still run as before, in a separate command. Happy to change this if you'd prefer another setup.

How to test

pnpm install --frozen-lockfile
pnpm test                                   # includes web/hooks/simulation/handle-tool-events.test.ts
pnpm --dir extension test
pnpm --filter agent-flow run lint
pnpm --dir web exec tsc --noEmit

New tests:

  • web/hooks/simulation/handle-tool-events.test.ts:
    • three parallel Read calls with different ids stay three nodes with three file-attention entries
    • the same id reported twice, including once after completion, gives one node
    • finishing the second call completes the second, not the first, and its token cost goes to its own file
    • the fallback without ids behaves as before
  • extension/test/transcript-parser.test.ts: toolUseId on start and end, with results arriving out of order.
  • extension/test/codex-rollout-parser.test.ts: toolUseId equals call_id for exec_command and apply_patch.

There is no hook-server test file. HookServer imports vscode, so the hook change is covered by the typecheck only.

Results (local, Node 24.14.1, pnpm 10.34.9):

  • Before the fix, the three id-based web tests failed, and so did the three extension tests that check toolUseId (two extended Codex tests and one new transcript test). The fallback test passed.
  • After the fix: root pnpm test 39/39 + 12/12, extension tests 79/79, extension lint and lint:test (tsc) clean, web tsc --noEmit clean.

I did not test this against a live Claude Code or Codex session in VS Code.

Checklist

AI assistance

The code, tests and this description were written with Claude Code (Claude Opus 5.5).

🤖 Generated with Claude Code

Parallel calls to the same tool collapsed into one node (deduped by name
inside the 3 s window) and a result could complete the wrong call (first
running call with that name). The ids already existed but never reached
the web view.

- Emit toolUseId on tool_call_start / tool_call_end from the transcript
  (tool_use id / tool_use_id), the hook server (tool_use_id) and Codex
  rollouts (call_id).
- Key, dedupe and finish tool calls on it in handle-tool-events.ts. The
  hook server and the transcript report the same id, so the duplicate is
  dropped by id rather than by time window.
- Keep name-based matching as the fallback when either side has no id
  (older recordings, hook payloads without tool_use_id).
- Run the web tests with web/tsconfig.json so they can import modules
  that use the @/ alias.

Fixes patoles#107

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cla-assistant

cla-assistant Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@patoles

patoles commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Thanks for picking this up, and for such a thorough write-up! The approach is exactly what we discussed, and the fallback for events without ids is handled carefully. One small request: the new root test script sets TSX_TSCONFIG_PATH=... inline, which doesn't work in Windows' default shell, and a few contributors are on Windows. Could you use tsx's flag instead, e.g. tsx --tsconfig web/tsconfig.json --test "web/hooks/**/*.test.ts" "web/lib/**/*.test.ts"? That works on every platform.

Optional, only if you feel like it: extension/test/request-guard.test.ts starts a real HookServer through a small vscode shim, so the hook path could get a test the same way. Not required for merging.

TSX_TSCONFIG_PATH=... inline does not work in the Windows default shell.
Also start a real HookServer, like request-guard.test.ts does, to check
that tool_use_id reaches tool_call_start and tool_call_end.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jjohnnystwsss

Copy link
Copy Markdown
Contributor Author

Thanks for the review! Both are in the latest commit:

  • The root test script now runs the web tests with tsx --tsconfig web/tsconfig.json --test ... instead of the inline TSX_TSCONFIG_PATH.
  • Added extension/test/hook-server.test.ts, which starts a real HookServer through the vscode shim like request-guard.test.ts. It posts PreToolUse, PostToolUse and PostToolUseFailure hooks and checks that tool_use_id reaches tool_call_start / tool_call_end (and stays undefined when the hook has none). It fails without the hook-server.ts change.

pnpm test (39 + 12), the extension tests (81) and tsc --noEmit for the extension and its tests all pass locally.

@patoles
patoles merged commit 71b83cb into patoles:main Nov 20, 2026
1 check passed
@patoles

patoles commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Merged, thanks a lot! Great work on this, especially adding the hook-server test even though it was optional. It will ship in the next release.

@patoles patoles mentioned this pull request Nov 20, 2026
2 tasks done
@jjohnnystwsss

Copy link
Copy Markdown
Contributor Author

Thanks for the quick review and the helpful pointers, glad I could help! Looking forward to the release.

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.

Parallel tool calls are incorrectly deduplicated and completed by tool name instead of call identity

2 participants