Skip to content

Conversation

@dwebster123
Copy link

@dwebster123 dwebster123 commented Jan 27, 2026

Summary

Fixes #11029

When sliding window truncation removes assistant messages containing tool_use blocks, user messages with tool_result blocks referencing those removed tool_use_ids were still being sent to the API. Anthropic's API requires every tool_result to have a corresponding tool_use in the immediately preceding assistant message, so these orphaned references cause the request to be rejected with:

LLM request rejected: unexpected tool_use_id in tool_result blocks

Root Cause

The existing orphan tool_result filtering logic in getEffectiveApiHistory() only ran in the summary (condensation) code path. When there was no summary and only sliding window truncation had been applied, orphan tool_result blocks were not filtered out.

Fix

Added the same orphan tool_result filtering to the no-summary code path in getEffectiveApiHistory():

  1. After determining which messages are visible (not truncated), collect all valid tool_use IDs from visible assistant messages
  2. Filter out any tool_result blocks in visible user messages that reference tool_use_ids not in the visible set
  3. Remove user messages that become empty after filtering

This mirrors the existing pattern already used in the condensation code path.

Tests

Added 3 test cases:

  • Orphan tool_result filtered when both assistant and user messages are truncated by truncationParent
  • Orphan tool_result filtered when user message survives truncation but referenced assistant is truncated
  • Mixed user messages retain non-orphan content while orphan tool_result blocks are stripped

All 5161 existing tests continue to pass.


Important

Adds orphan tool_result filtering to getEffectiveApiHistory() for sliding window truncation in index.ts.

  • Behavior:
    • Adds orphan tool_result filtering to the no-summary path in getEffectiveApiHistory() in index.ts.
    • Filters tool_result blocks referencing non-visible tool_use IDs and removes empty user messages.
  • Tests:
    • Adds test cases in index.spec.ts for orphan tool_result filtering when assistant messages are truncated, user messages survive truncation, and mixed content in user messages.
    • Confirms all 5161 existing tests pass.

This description was created by Ellipsis for ac1d172. You can customize this summary. It will automatically update as commits are pushed.

… (no summary)

When sliding window truncation removes assistant messages containing tool_use
blocks, user messages with tool_result blocks referencing those removed IDs
were being sent to the API, causing Anthropic to reject the request with
'unexpected tool_use_id in tool_result blocks'.

The existing orphan filtering logic only ran in the summary (condensation)
code path. This adds the same filtering to the no-summary code path in
getEffectiveApiHistory(), ensuring tool_use/tool_result pairs remain atomic
regardless of how truncation occurs.

Fixes RooCodeInc#11029
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working labels Jan 27, 2026
@roomote
Copy link
Contributor

roomote bot commented Jan 27, 2026

Rooviewer Clock   See task on Roo Cloud

Review complete. No issues found.

The implementation correctly adds orphan tool_result filtering to the no-summary code path in getEffectiveApiHistory(), mirroring the existing pattern from the summary code path. The fix addresses the root cause where orphan tool_result blocks were being sent to Anthropic's API after sliding window truncation removed their corresponding tool_use messages.

Mention @roomote in a comment to request specific changes to this pull request or fix all unresolved issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:L This PR changes 100-499 lines, ignoring generated files.

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

LLM request rejected: unexpected tool_use_id in tool_result blocks

1 participant