Skip to content

Conversation

@daniel-lxs
Copy link
Member

@daniel-lxs daniel-lxs commented Jan 15, 2026

Problem

After multiple condensations, tool_use blocks preserved in earlier summary messages can end up outside the bounded search window of N_MESSAGES_TO_KEEP (3 messages). This causes orphaned tool_result blocks that trigger 400 errors from the API:

messages.0.content.0: unexpected tool_use_id found in tool_result blocks

Root Cause

The bounded search in getKeepMessagesWithToolBlocks was too restrictive:

// Only searches 3 messages before the keep boundary
const searchStart = startIndex - 1
const searchEnd = Math.max(0, startIndex - N_MESSAGES_TO_KEEP)
const messagesToSearch = messages.slice(searchEnd, searchStart + 1)

After multiple condensations, tool_use blocks can be in earlier summary messages that are outside this window.

Solution

Changed the search to cover the entire condensed region:

// Search the entire condensed region (from index 0 to startIndex-1)
const messagesToSearch = messages.slice(0, startIndex)

Tests

Added two new tests:

  • should find tool_use even when it is far back in the message history (ROO-520 fix)
  • should find tool_use in previous summary message after multiple condensations (ROO-520)

Fixes: https://linear.app/roocode/issue/ROO-520


Important

Expands search in getKeepMessagesWithToolBlocks to cover entire condensed region, fixing orphaned tool_result blocks and preventing 400 API errors.

  • Behavior:
    • Expands search in getKeepMessagesWithToolBlocks to cover entire condensed region, ensuring tool_use blocks are found even if far back in history.
    • Fixes issue causing orphaned tool_result blocks and 400 API errors.
  • Tests:
    • Adds test should find tool_use even when it is far back in the message history (ROO-520 fix) in index.spec.ts.
    • Adds test should find tool_use in previous summary message after multiple condensations (ROO-520) in index.spec.ts.

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

The bounded search window in getKeepMessagesWithToolBlocks was too
restrictive after multiple condensations. When tool_use blocks are
preserved in earlier summary messages, they can end up outside the
N_MESSAGES_TO_KEEP search window, causing orphaned tool_result blocks
that trigger 400 errors from the API.

Changed the search to cover the entire condensed region (from index 0
to startIndex-1) instead of just the last N_MESSAGES_TO_KEEP messages.

Fixes: https://linear.app/roocode/issue/ROO-520
@daniel-lxs daniel-lxs requested a review from jr as a code owner January 15, 2026 16:33
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working labels Jan 15, 2026
@roomote
Copy link
Contributor

roomote bot commented Jan 15, 2026

Rooviewer Clock   See task on Roo Cloud

Review complete. No issues found.

The fix correctly addresses ROO-520 by expanding the search for tool_use blocks to cover the entire condensed region instead of a bounded window. This ensures tool_use blocks preserved in earlier summary messages are found after multiple condensations. The tests comprehensively verify the fix.

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.

2 participants