Skip to content

Conversation

@daniel-lxs
Copy link
Member

@daniel-lxs daniel-lxs commented Dec 3, 2025

Summary

Fixes the "unexpected tool_use_id found in tool_result blocks" error that occurred when editing messages in completed tasks.

The Problem

When a user edited a message in a completed task:

  1. API history contains an assistant message with tool_use blocks (e.g., id=A)
  2. The user's response was removed during editing
  3. A new response flow could generate different tool IDs (id=B)
  4. The tool_result used id=B, but the API expected id=A
  5. This caused the 400 error: unexpected tool_use_id found in tool_result blocks

The Solution

Added getValidatedToolUseId() function in presentAssistantMessage.ts that:

  1. Detects edit scenarios by checking if the last message in API history is an assistant message (waiting for tool_result) vs a user message (normal flow)
  2. Finds unmatched tool_use blocks and uses their correct IDs
  3. Preserves original IDs during normal conversation flow

How it works:

  • Normal flow (last message = USER): Use current streaming ID (it will be added to history)
  • Edit flow (last message = ASSISTANT): Use ID from API history to match existing tool_use

Changes

  • Added getValidatedToolUseId() function to validate and correct tool IDs
  • Updated pushToolResult to use validated IDs
  • Handles edge cases: multiple tools, matching by name, graceful fallbacks

Testing

The fix handles:

  • ✅ Message edit scenarios
  • ✅ Task resumption/rehydration
  • ✅ Multiple tool calls in the same conversation
  • ✅ Prevents the original "400 unexpected tool_use_id" error

Important

Fixes tool_use_id mismatch error in completed task message edits by validating tool IDs in presentAssistantMessage.ts.

  • Behavior:
    • Adds getValidatedToolUseId() in presentAssistantMessage.ts to correct tool_use_id mismatches when editing messages in completed tasks.
    • Updates pushToolResult in presentAssistantMessage.ts to use validated tool IDs.
  • Functionality:
    • Detects edit scenarios by checking the last message role in API history.
    • Matches tool_use blocks by ID or name, with fallbacks for unmatched cases.
    • Ensures correct tool_use_id is used in tool_result blocks to prevent 400 errors.
  • Testing:
    • Handles message edits, task resumption, multiple tool calls, and prevents original 400 error.

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

…tasks

When a user edits a message in a completed task, the API history contains
an assistant message with tool_use blocks that need matching tool_result
responses. Previously, new tool IDs could be generated during the edit
flow, causing the error:
'unexpected tool_use_id found in tool_result blocks'

This fix adds getValidatedToolUseId() function that:
- Detects edit scenarios by checking if last message in API history is
  an assistant message (waiting for tool_result)
- Finds unmatched tool_use blocks and uses their correct IDs
- Preserves original IDs during normal conversation flow (last message
  is a user message)

The fix handles:
- Message edit scenarios
- Task resumption/rehydration
- Multiple tool calls in the same conversation
@daniel-lxs daniel-lxs requested review from cte, jr and mrubens as code owners December 3, 2025 23:32
@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Dec 3, 2025
@daniel-lxs daniel-lxs moved this from Triage to PR [Needs Review] in Roo Code Roadmap Dec 3, 2025
@dosubot dosubot bot added the bug Something isn't working label Dec 3, 2025
@roomote
Copy link
Contributor

roomote bot commented Dec 3, 2025

Rooviewer Clock   See task on Roo Cloud

Review completed. No issues found.

The implementation correctly addresses the tool_use_id mismatch issue when editing messages in completed tasks. The getValidatedToolUseId() function properly detects edit scenarios by checking the last message role in API history, matches tool_use blocks by ID or name, and handles edge cases appropriately.

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

* especially when editing messages in completed tasks.
*
* When a user edits a message in a completed task:
* 1. API history is truncated (keeping assistant's tool_use with id=A)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we keep the assistant's tool use?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this just feels like a lot of special case logic. Is there a way that we could treat it more like a checkpoint rollback following by a new user message and not need any special casing aside from what's already in place to handle that case?

@daniel-lxs daniel-lxs closed this Dec 4, 2025
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Dec 4, 2025
@github-project-automation github-project-automation bot moved this from PR [Needs Review] to Done in Roo Code Roadmap Dec 4, 2025
@daniel-lxs daniel-lxs deleted the dan/roo-37-tool-id-mismatch-when-the-task-is-completed-and-the-user branch December 4, 2025 18:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants