Skip to content

fix: add pre-send validation for tool_use/tool_result pairing#12183

Draft
roomote-v0[bot] wants to merge 1 commit intomainfrom
feature/pre-send-tool-result-validation
Draft

fix: add pre-send validation for tool_use/tool_result pairing#12183
roomote-v0[bot] wants to merge 1 commit intomainfrom
feature/pre-send-tool-result-validation

Conversation

@roomote-v0
Copy link
Copy Markdown
Contributor

@roomote-v0 roomote-v0 Bot commented Apr 24, 2026

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_use block has a corresponding tool_result block 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:

  1. New function validateMessageHistoryBeforeSend() in src/core/task/validateToolResultIds.ts:

    • Iterates through the final message array looking for assistant messages with tool_use blocks
    • Checks the following user message has matching tool_result blocks for each tool_use
    • Injects placeholder tool_result blocks for any missing pairings
    • Inserts synthetic user messages when none follow an assistant message with tool_use
    • Reports mismatches to telemetry via MissingToolResultError
    • Returns the original array reference (no allocation) when no fixes are needed
  2. Integration in Task.ts: Called right after buildCleanConversationHistory() and before createMessage() -- the last possible moment before the API call.

Feedback and guidance are welcome.

Test Procedure

  • 7 new unit tests added to src/core/task/__tests__/validateToolResultIds.spec.ts covering:
    • All tool_use blocks have matching tool_results (no modification)
    • Missing tool_results for multiple tool_use IDs (placeholders injected)
    • No following user message (synthetic message inserted)
    • Multiple assistant messages with tool_use blocks in conversation
    • Messages without tool_use blocks (passthrough)
    • Assistant messages with string content (passthrough)
    • Next message being assistant instead of user (synthetic user message inserted)
  • All 34 tests pass: cd src && npx vitest run core/task/__tests__/validateToolResultIds.spec.ts
  • Lint and type checks pass across all packages

Pre-Submission Checklist

  • Issue Linked: This PR is linked to an approved GitHub Issue (see "Related GitHub Issue" above).
  • Scope: My changes are focused on the linked issue (one major feature/fix per PR).
  • Self-Review: I have performed a thorough self-review of my code.
  • Testing: New and/or updated tests have been added to cover my changes.
  • Documentation Impact: No documentation updates needed (internal validation logic).
  • Contribution Guidelines: I have read and agree to the Contributor Guidelines.

Documentation Updates

No documentation updates required. This is an internal validation mechanism.

Interactively review PR in Roo Code Cloud

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.
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.

🚨 CRITICAL: tool_use blocks missing tool_result blocks - Complete system failure

1 participant