fix: skip synthetic tool results for aborted/errored messages#11729
fix: skip synthetic tool results for aborted/errored messages#11729rodbland2021 wants to merge 1 commit intoopenclaw:mainfrom
Conversation
Prevents session corruption by skipping tool call extraction from assistant messages with stopReason "aborted" or "error". When users interrupt requests mid-stream or errors occur, tool_use blocks may be incomplete/malformed. Generating synthetic tool_result blocks for these causes Anthropic API to reject with: "unexpected tool_use_id found in tool_result blocks" This fix eliminates the most common source of session corruption. Fixes openclaw#6788 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
|
Greptile encountered an error while reviewing this PR. Please reach out to support@greptile.com for assistance. |
Note: Duplicate PRs ExistI discovered there are 10+ open PRs addressing this same issue (oldest from Jan 28). Issue #6788 was closed on Feb 3 as "COMPLETED" but no PR was merged. Related PRs:
Recommendation for Maintainers: Why this PR might be preferred:
Happy to close this if an earlier PR is selected, or coordinate with other contributors to consolidate efforts. |
|
Closing this PR as it's a duplicate of #6831 (submitted 6 days earlier by @zerone0x). Why #6831 is better:
I should have searched for existing PRs before submitting. Apologies for adding to the duplicate pile! To maintainers: Please consider merging #6831 - it appears to be the cleanest solution among the 10+ duplicate PRs addressing this issue. |
Summary
Fixes session corruption by skipping tool call extraction from assistant messages with
stopReason: "aborted"orstopReason: "error".Problem
When users interrupt requests mid-stream or errors occur during LLM responses, tool_use blocks may be incomplete or malformed. The current
extractToolCallsFromAssistant()function extracts these incomplete tool calls and generates synthetictool_resultblocks for them.This causes the Anthropic API to reject the entire session with:
This triggers auth profile cooldowns and can cascade through all available profiles, making the gateway unresponsive.
Solution
Added a check at the beginning of
extractToolCallsFromAssistant()to skip tool call extraction whenstopReason === "aborted"orstopReason === "error":Testing
All existing tests pass (10/10). The test suite already included tests for this exact scenario:
skips tool call extraction for assistant messages with stopReason 'error'(line 117)skips tool call extraction for assistant messages with stopReason 'aborted'(line 140)drops orphan tool results that follow an aborted assistant message(line 180)These tests were previously failing but now pass with this fix.
Impact
Related Issues
Fixes #6788
Follow-up Work
This PR addresses the primary issue (aborted/errored messages). Future PRs could address: