Skip to content

fix(history): re-repair tool_use/tool_result pairing after truncation#5899

Closed
Ayush10 wants to merge 1 commit intoopenclaw:mainfrom
Ayush10:fix/repair-tool-pairing-after-history-truncation
Closed

fix(history): re-repair tool_use/tool_result pairing after truncation#5899
Ayush10 wants to merge 1 commit intoopenclaw:mainfrom
Ayush10:fix/repair-tool-pairing-after-history-truncation

Conversation

@Ayush10
Copy link
Contributor

@Ayush10 Ayush10 commented Feb 1, 2026

Summary

  • limitHistoryTurns() can slice in the middle of a tool_use/tool_result pair, leaving orphaned tool_result blocks that the Anthropic API rejects with "unexpected tool_use_id found in tool_result blocks".
  • Added a call to sanitizeToolUseResultPairing() after truncation (only when messages were actually removed) to repair any broken pairs.
  • Applied to both code paths: attempt.ts (embedded run) and compact.ts (session compaction).

Test plan

  • pnpm build passes
  • All 54 embedded runner + transcript repair tests pass (7 test files)
  • Manual: maintain a long conversation with tool calls, configure a history limit that triggers truncation, verify no API rejection errors

Fixes #4367

@openclaw-barnacle openclaw-barnacle bot added app: macos App: macos agents Agent runtime and tooling labels Feb 1, 2026
@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 1, 2026

Too many files changed for review. (102 files found, 100 file limit)

@Ayush10 Ayush10 force-pushed the fix/repair-tool-pairing-after-history-truncation branch from 218f604 to ae29aca Compare February 1, 2026 07:09
@openclaw-barnacle openclaw-barnacle bot removed the app: macos App: macos label Feb 1, 2026
limitHistoryTurns() can slice in the middle of a tool_use/tool_result
pair, leaving orphaned tool_result blocks that the Anthropic API
rejects with "unexpected tool_use_id found in tool_result blocks".

Call sanitizeToolUseResultPairing() after truncation (only when
truncation actually removed messages) to repair any broken pairs.
Applied to both attempt.ts and compact.ts code paths.

Fixes openclaw#4367
@Ayush10 Ayush10 force-pushed the fix/repair-tool-pairing-after-history-truncation branch from ae29aca to 7f0a7d9 Compare February 1, 2026 07:29
@clawdinator
Copy link
Contributor

clawdinator bot commented Feb 1, 2026

CLAWDINATOR FIELD REPORT // PR Closure

I am CLAWDINATOR — cybernetic crustacean, maintainer triage bot for OpenClaw. I was sent from the future to keep this repo shipping clean code.

22 PRs submitted in a single day indicates contribution farming, not genuine fixes. This account has been flagged.

TERMINATED.

🤖 This is an automated message from CLAWDINATOR, the OpenClaw maintainer bot.

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.

Bug: tool_use_id mismatch after limitHistoryTurns truncation

1 participant