Skip to content

fix: skip tool calls from aborted assistant messages in transcript repair#4476

Closed
kira-ariaki wants to merge 1 commit intoopenclaw:mainfrom
kira-ariaki:fix/skip-aborted-tool-calls-in-transcript-repair
Closed

fix: skip tool calls from aborted assistant messages in transcript repair#4476
kira-ariaki wants to merge 1 commit intoopenclaw:mainfrom
kira-ariaki:fix/skip-aborted-tool-calls-in-transcript-repair

Conversation

@kira-ariaki
Copy link
Contributor

@kira-ariaki kira-ariaki commented Jan 30, 2026

Summary

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.

Changes

  • Modified repairToolUseResultPairing() in src/agents/session-transcript-repair.ts to skip tool call extraction for stopReason === 'error' || 'aborted' assistant messages
  • Added 7 comprehensive unit tests covering error/aborted scenarios

Root Cause

Two safety mechanisms interacted badly:

  1. session-tool-result-guard inserts synthetic toolResult for orphaned tool calls
  2. transformMessages() drops error/aborted assistant messages but not their corresponding toolResult

This created orphaned tool_result entries that referenced non-existent tool_use blocks.

Testing

All tests in session-transcript-repair.test.ts pass, including new tests for both error and aborted stop reasons.

Fixes

@Glucksberg
Copy link
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
@kira-ariaki kira-ariaki force-pushed the fix/skip-aborted-tool-calls-in-transcript-repair branch from c993906 to 1f2102a Compare February 8, 2026 18:30
@kira-ariaki
Copy link
Contributor Author

Thanks @Glucksberg for the duplicate flag. I've updated this PR to match the scope of #4844:

Changes:

  • ✅ Expanded coverage to handle both error and aborted stop reasons
  • ✅ Added comprehensive unit test for error case (now 7 total tests)
  • ✅ Updated commit message to reference all 5 related issues

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.

@sebslight
Copy link
Member

Closing as duplicate of #4844. If this is incorrect, comment and we can reopen.

@sebslight sebslight closed this Feb 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants