fix: skip tool calls from aborted assistant messages in transcript repair#4476
Closed
kira-ariaki wants to merge 1 commit intoopenclaw:mainfrom
Closed
Conversation
This was referenced Feb 2, 2026
This was referenced Feb 8, 2026
Contributor
|
📋 Duplicate triage note: PR #4844 appears to implement the same fix. Consider consolidating efforts or closing one in favor of the other. |
…ipt repair When a streaming response is interrupted (stopReason: error or aborted), the tool call may be incomplete (indicated by partialJson in the message). The transcript repair logic was still extracting tool calls from these messages and inserting synthetic tool_result blocks for them. When sent to Anthropic API, these incomplete tool_use blocks may be invalid/malformed, causing the API to reject the request with: 'unexpected tool_use_id found in tool_result blocks' This permanently corrupts the session until manually repaired. Fix: Check for stopReason === 'error' || stopReason === 'aborted' in extractToolCallsFromAssistant and return early with an empty array, skipping synthetic result insertion for these cases. Adds comprehensive test coverage for both error and aborted scenarios. Fixes openclaw#4475, openclaw#4597, openclaw#4600, openclaw#4814, openclaw#4815
c993906 to
1f2102a
Compare
Contributor
Author
|
Thanks @Glucksberg for the duplicate flag. I've updated this PR to match the scope of #4844: Changes:
This PR now addresses:
Both PRs now have equivalent functionality and test coverage. Since this was submitted earlier (Jan 30), I've improved it rather than closing. Happy for maintainers to choose either one. |
Member
|
Closing as duplicate of #4844. If this is incorrect, comment and we can reopen. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a streaming response is interrupted (
stopReason: errororaborted), the tool call may be incomplete (indicated by partialJson in the message). The transcript repair logic was still extracting tool calls from these messages and inserting synthetictool_resultblocks for them.When sent to Anthropic API, these incomplete
tool_useblocks may be invalid/malformed, causing the API to reject the request with:unexpected tool_use_id found in tool_result blocksThis permanently corrupts the session until manually repaired.
Changes
repairToolUseResultPairing()insrc/agents/session-transcript-repair.tsto skip tool call extraction forstopReason === 'error' || 'aborted'assistant messagesRoot Cause
Two safety mechanisms interacted badly:
session-tool-result-guardinserts synthetictoolResultfor orphaned tool callstransformMessages()drops error/aborted assistant messages but not their correspondingtoolResultThis created orphaned
tool_resultentries that referenced non-existenttool_useblocks.Testing
All tests in
session-transcript-repair.test.tspass, including new tests for both error and aborted stop reasons.Fixes