-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix: validate and fix tool_result IDs before API requests #9952
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Fixes ApiError: Expected toolResult blocks for mismatched IDs ## Problem Tool_result blocks sometimes had incorrect tool_use_id values, causing the Anthropic API to reject requests with errors like: "Expected toolResult blocks at messages.6.content for the following Ids: toolu_ABC, but found: toolu_XYZ" ## Solution Added centralized validation in addToApiConversationHistory() that: - Validates tool_result IDs against tool_use IDs from previous assistant message - Fixes mismatches using position-based matching - Logs warnings for debugging when corrections are made ## Changes - Added src/core/task/validateToolResultIds.ts with validation logic - Added src/core/task/__tests__/validateToolResultIds.spec.ts (12 tests) - Modified src/core/task/Task.ts to call validation - Simplified src/core/assistant-message/presentAssistantMessage.ts by removing redundant source-level validation
Review complete. No issues found. This PR adds centralized validation to correct tool_result ID mismatches before API requests. The implementation is well-structured with comprehensive test coverage (12 tests) and handles edge cases gracefully. The position-based matching strategy is appropriate for the stated use cases (race conditions, message editing, resume/delegation scenarios). Mention @roomote in a comment to request specific changes to this pull request or fix all unresolved issues. |
Review complete. No issues found. This commit (11fdfd8) fixes a subtle bug in position-based matching when duplicate tool_use_ids are present:
All previous review comments remain resolved:
Mention @roomote in a comment to request specific changes to this pull request or fix all unresolved issues. |
|
I think in general it would be nice to fully understand the root cause of this, and a solid repro case would help with that. Short of that, we should considering emitting an event to Posthog every time we detect that a correct is needed. |
- Replace .slice().reverse().find() with direct backwards loop - Avoids creating two intermediate arrays for large conversation histories - Applied to both addToApiConversationHistory and flushPendingToolResultsToHistory
- Replace manual backwards loop with findLastIndex in both addToApiConversationHistory and flushPendingToolResultsToHistory - Improves clarity and consistency with existing code patterns
f9952b0 to
ddf88d8
Compare
Problem
Tool_result blocks sometimes had incorrect tool_use_id values, causing the Anthropic API to reject requests with errors like:
This could happen due to:
Solution
Added centralized validation in
addToApiConversationHistory()that:Changes
src/core/task/validateToolResultIds.tswith centralized validation logicsrc/core/task/__tests__/validateToolResultIds.spec.tswith 12 comprehensive testssrc/core/task/Task.tsto call validation inaddToApiConversationHistory()src/core/assistant-message/presentAssistantMessage.tsby removing redundant source-level validationPerformance
The validation adds negligible overhead (~0.1-0.5ms) compared to API call times (500ms-30s). It only runs on user messages containing tool_result blocks.
Testing
All 4589 tests pass.
Important
Add validation and correction for
tool_resultIDs in user messages to match previous assistanttool_useIDs, with telemetry and tests.validateAndFixToolResultIds()andToolResultIdMismatchErrorinvalidateToolResultIds.tsto validate/correcttool_result.tool_use_idby position against prior assistanttool_useIDs; reports mismatches via telemetry.Task.addToApiConversationHistory()andflushPendingToolResultsToHistory(), run validation on user messages before pushing toapiConversationHistory.validateToolResultIds.spec.tswith comprehensive cases (matching, mismatched, extra/missing items, non-tool content, string content, telemetry).This description was created by
for 11fdfd8. You can customize this summary. It will automatically update as commits are pushed.
Note
Validates and auto-corrects
tool_result.tool_use_idvalues against the previous assistanttool_useblocks before persisting user messages, with telemetry and tests.validateAndFixToolResultIds()andToolResultIdMismatchErrorinsrc/core/task/validateToolResultIds.tsto validate/correcttool_result.tool_use_idby position against prior assistanttool_useIDs; reports mismatches via telemetry.Task.addToApiConversationHistory()andflushPendingToolResultsToHistory(), run validation on user messages before pushing toapiConversationHistory.src/core/task/__tests__/validateToolResultIds.spec.tswith comprehensive cases (matching, mismatched, extra/missing items, non-tool content, string content, telemetry).Written by Cursor Bugbot for commit c07b9f9. This will update automatically on new commits. Configure here.