-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: correct tool_use_id mismatch when editing messages in completed tasks #9797
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: correct tool_use_id mismatch when editing messages in completed tasks #9797
Conversation
…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
Review completed. No issues found. The implementation correctly addresses the tool_use_id mismatch issue when editing messages in completed tasks. The 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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
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:
tool_useblocks (e.g., id=A)tool_resultused id=B, but the API expected id=Aunexpected tool_use_id found in tool_result blocksThe Solution
Added
getValidatedToolUseId()function inpresentAssistantMessage.tsthat:How it works:
Changes
getValidatedToolUseId()function to validate and correct tool IDspushToolResultto use validated IDsTesting
The fix handles:
Important
Fixes tool_use_id mismatch error in completed task message edits by validating tool IDs in
presentAssistantMessage.ts.getValidatedToolUseId()inpresentAssistantMessage.tsto correcttool_use_idmismatches when editing messages in completed tasks.pushToolResultinpresentAssistantMessage.tsto use validated tool IDs.This description was created by
for 74b5eb4. You can customize this summary. It will automatically update as commits are pushed.