Skip to content

fix: skip synthetic tool results for aborted/errored messages#11729

Closed
rodbland2021 wants to merge 1 commit intoopenclaw:mainfrom
rodbland2021:fix/aborted-tool-call-synthetic-results
Closed

fix: skip synthetic tool results for aborted/errored messages#11729
rodbland2021 wants to merge 1 commit intoopenclaw:mainfrom
rodbland2021:fix/aborted-tool-call-synthetic-results

Conversation

@rodbland2021
Copy link
Contributor

Summary

Fixes session corruption by skipping tool call extraction from assistant messages with stopReason: "aborted" or stopReason: "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 synthetic tool_result blocks for them.

This causes the Anthropic API to reject the entire session with:

HTTP 400: unexpected tool_use_id found in tool_result blocks

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 when stopReason === "aborted" or stopReason === "error":

const stopReason = (msg as { stopReason?: unknown }).stopReason;
if (stopReason === "aborted" || stopReason === "error") {
  return [];
}

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

  • High: Eliminates the most common source of session corruption
  • Low Risk: Small, focused change with comprehensive test coverage
  • Backwards Compatible: Only affects error handling, no API changes

Related Issues

Fixes #6788

Follow-up Work

This PR addresses the primary issue (aborted/errored messages). Future PRs could address:

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>
@openclaw-barnacle openclaw-barnacle bot added the agents Agent runtime and tooling label Feb 8, 2026
@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 8, 2026

Greptile encountered an error while reviewing this PR. Please reach out to support@greptile.com for assistance.

@rodbland2021
Copy link
Contributor Author

Note: Duplicate PRs Exist

I 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:
Pick one PR to merge (ideally the cleanest implementation with tests) and close the duplicates.

Why this PR might be preferred:

  • ✅ Uses existing test suite (tests already written, now passing)
  • ✅ Minimal change (4 lines + comments)
  • ✅ Handles both aborted and error stopReasons
  • ✅ Well-documented with clear commit message

Happy to close this if an earlier PR is selected, or coordinate with other contributors to consolidate efforts.

@rodbland2021
Copy link
Contributor Author

Closing this PR as it's a duplicate of #6831 (submitted 6 days earlier by @zerone0x).

Why #6831 is better:

  • ✅ Nearly identical fix but submitted first (Feb 2 vs Feb 8)
  • ✅ More complete - fixes both session-transcript-repair.ts AND session-tool-result-guard.ts
  • ✅ Better tested - adds 2 new tests vs using existing tests only
  • ✅ Greptile review: 5/5 confidence score

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.

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.

Transcript repair creates orphaned tool_result for aborted tool calls

1 participant