Skip to content

fix(ai): use toolCallId for parallel tool execution tracking#11966

Open
delta575 wants to merge 2 commits intovercel:mainfrom
delta575:fix/parallel-tool-tracking-id
Open

fix(ai): use toolCallId for parallel tool execution tracking#11966
delta575 wants to merge 2 commits intovercel:mainfrom
delta575:fix/parallel-tool-tracking-id

Conversation

@delta575
Copy link

Background

Split from #11907 per review feedback to separate from the close guard fix.

The generateId parameter serves a valid purpose: allowing frameworks to control message IDs for grouping. For example, @convex-dev/agent overrides it to return the same ID for all messages in a request:

generateId: pendingMessageId
  ? () => pendingMessageId ?? crypto.randomUUID()
  : undefined,

However, generateId() was also being used internally to track outstanding tool executions via a Set. When a framework returns the same ID for message grouping, the Set tracks only one entry instead of N tools:

Tool A: outstandingToolResults.add("msg-123") → Set: {"msg-123"}
Tool B: outstandingToolResults.add("msg-123") → Set: {"msg-123"} (duplicate ignored)
Tool C: outstandingToolResults.add("msg-123") → Set: {"msg-123"}
Tool B completes: outstandingToolResults.delete("msg-123") → Set: {} (empty!)
attemptClose() sees empty set → closes stream prematurely
Tool A and C results are LOST

Summary

Use toolCall.toolCallId instead of generateId() for tool execution tracking.

toolCallId is unique per tool call from the LLM and is the correct identifier for tracking individual tool executions. This separates the concerns of message ID generation (user-controlled) from internal tool execution tracking (implementation detail).

Manual Verification

The included test deterministically reproduces the issue:

  • Simulates framework behavior with generateId returning a constant
  • Without fix: only 1 of 3 tool results captured
  • With fix: all 3 tool results captured

Also tested end-to-end with @convex-dev/agent and Gemini 2.5 Flash with 5+ parallel tool calls.

Checklist

  • Tests have been added / updated (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features)
  • A patch changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root)
  • I have reviewed this pull request (self-review)

Related Issues

When tracking outstanding tool executions, use `toolCall.toolCallId` instead
of `generateId()`. This fixes an issue where frameworks that override
`generateId` for message grouping (returning the same ID for all tools in a
request) would cause the `outstandingToolResults` Set to track only one tool.

Problem:
- Frameworks like @convex-dev/agent override `generateId` to return a constant
  `pendingMessageId` for message association purposes
- This caused `outstandingToolResults.add(generateId())` to add the same ID
  multiple times, but Sets ignore duplicates
- When the first tool completed and deleted the ID, the Set became empty
- Stream closed prematurely, losing results from other tools

Solution:
- Use `toolCall.toolCallId` which is guaranteed unique per tool call from the LLM
- This ensures each tool execution is tracked independently

Test verification:
- Test fails without fix: only 1 of 3 results captured
- Test passes with fix: all 3 results captured

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant