fix: add pre-send validation for tool_use/tool_result pairing#12183
Draft
roomote-v0[bot] wants to merge 1 commit intomainfrom
Draft
fix: add pre-send validation for tool_use/tool_result pairing#12183roomote-v0[bot] wants to merge 1 commit intomainfrom
roomote-v0[bot] wants to merge 1 commit intomainfrom
Conversation
Adds validateMessageHistoryBeforeSend() as a safety net that runs on the final message array right before the API call. This catches any tool_use blocks that are missing corresponding tool_result blocks - a mismatch that causes Anthropic API rejection. The validation: - Iterates through the final messages looking for assistant messages with tool_use blocks - Checks the following user message has matching tool_result blocks - Injects placeholder tool_results for any missing pairings - Inserts synthetic user messages when none follow an assistant message - Reports mismatches to telemetry via MissingToolResultError This addresses cases where post-processing steps (getEffectiveApiHistory, mergeConsecutiveApiMessages, buildCleanConversationHistory) may introduce mismatches after the existing insert-time validation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related GitHub Issue
Closes: #11777
Description
This PR attempts to address Issue #11777 by adding a pre-send validation safety net that ensures every
tool_useblock has a correspondingtool_resultblock before API requests are sent to Anthropic.The codebase already has
validateAndFixToolResultIds()which validates at message insert time. However, several post-processing steps run between insert-time and the actual API call (getEffectiveApiHistory,mergeConsecutiveApiMessages,buildCleanConversationHistory) that can introduce new mismatches.Changes:
New function
validateMessageHistoryBeforeSend()insrc/core/task/validateToolResultIds.ts:tool_useblockstool_resultblocks for eachtool_usetool_resultblocks for any missing pairingsMissingToolResultErrorIntegration in
Task.ts: Called right afterbuildCleanConversationHistory()and beforecreateMessage()-- the last possible moment before the API call.Feedback and guidance are welcome.
Test Procedure
src/core/task/__tests__/validateToolResultIds.spec.tscovering:cd src && npx vitest run core/task/__tests__/validateToolResultIds.spec.tsPre-Submission Checklist
Documentation Updates
No documentation updates required. This is an internal validation mechanism.
Interactively review PR in Roo Code Cloud