You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bug: limitHistoryTurns breaks tool_use/tool_result pairs after sanitization #38370
When sessions grow large (170K+ tokens), the message windowing code can break tool_use/tool_result pairs, causing the Claude API to reject requests.
Error Message
LLM request rejected: messages.434.content.1: unexpected tool_use_id found in tool_result blocks: toolu_01Vq6m4qpujG9jQqHVSseEBV. Each tool_result block must have a corresponding tool_use block in the previous message.
Root Cause
limitHistoryTurns() runs aftersanitizeToolUseResultPairing(), which can break previously-repaired pairs.
constprior=awaitsanitizeSessionHistory({...});// repairs tool pairsconstvalidatedGemini=validateGeminiTurns(prior);constvalidated=validateAnthropicTurns(validatedGemini);constlimited=limitHistoryTurns(validated, ...);// CAN BREAK PAIRS AGAIN
Why It Breaks
limitHistoryTurns counts user turns from the end and slices at user turn boundaries. If the cut point lands between an assistant message containing tool_use and the following user message with tool_result, the API rejects it.
Proposed Fix
Option A: Quick Fix
Call sanitizeToolUseResultPairing again after limitHistoryTurns:
constlimited=limitHistoryTurns(validated, ...);constrepaired=sanitizeToolUseResultPairing(limited);// ADD THIS
Option B: Proper Fix
Make limitHistoryTurns tool-pair-aware so it never cuts inside a tool transaction (between tool_use and its matching tool_result).
Reproduction
Long session with many tool calls (170K+ tokens, 400+ messages)
Session approaches context limit
limitHistoryTurns trims messages
Cut happens between tool_use (assistant) and tool_result (user)
API rejects
Notes
The memoryFlush feature exacerbates this by injecting tool calls at the compaction boundary
This affects ANY tool, not just specific ones
Workaround: Clear session with /new, but this loses context
Current main already repairs tool/result pairing after limitHistoryTurns in both embedded-runner paths, and the repair helper drops orphaned toolResult messages. The reported failure mode is therefore already implemented/fixed on main, and the same code is present in release v2026.4.22.
What I checked:
Run attempt re-repairs after truncation:limitHistoryTurns(...) is immediately followed by sanitizeToolUseResultPairing(...), with an inline comment calling out orphaned tool_result blocks after truncation. (src/agents/pi-embedded-runner/run/attempt.ts:1615)
Compaction path re-repairs after truncation: The compaction flow applies the same post-limitHistoryTurns repair step, again explicitly covering orphaned tool/result pairs. (src/agents/pi-embedded-runner/compact.ts:953)
Repair helper drops orphan tool results:repairToolUseResultPairing removes free-floating toolResult messages when they are not directly attached to a matching assistant tool-call turn, which is the exact rejection path from the report. (src/agents/session-transcript-repair.ts:456)
Regression coverage for orphan tool results: Tests assert that orphan tool results are dropped when no matching tool call remains. (src/agents/session-transcript-repair.test.ts:126)
Release tag contains the fix: The v2026.4.22 tag already contains the post-limitHistoryTurnssanitizeToolUseResultPairing(...) calls in both source paths. (00bd2cf7a376)
So I’m closing this as already implemented rather than keeping a duplicate issue open.
Activity
fix(agents): prevent limitHistoryTurns from orphaning toolResult mess…
openclaw-barnacle commentedon Apr 21, 2026
This issue has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.
steipete commentedon Apr 24, 2026
Closing this as implemented after Codex review.
Current
mainalready repairs tool/result pairing afterlimitHistoryTurnsin both embedded-runner paths, and the repair helper drops orphanedtoolResultmessages. The reported failure mode is therefore already implemented/fixed onmain, and the same code is present in releasev2026.4.22.What I checked:
limitHistoryTurns(...)is immediately followed bysanitizeToolUseResultPairing(...), with an inline comment calling out orphanedtool_resultblocks after truncation. (src/agents/pi-embedded-runner/run/attempt.ts:1615)limitHistoryTurnsrepair step, again explicitly covering orphaned tool/result pairs. (src/agents/pi-embedded-runner/compact.ts:953)repairToolUseResultPairingremoves free-floatingtoolResultmessages when they are not directly attached to a matching assistant tool-call turn, which is the exact rejection path from the report. (src/agents/session-transcript-repair.ts:456)src/agents/session-transcript-repair.test.ts:126)v2026.4.22tag already contains the post-limitHistoryTurnssanitizeToolUseResultPairing(...)calls in both source paths. (00bd2cf7a376)So I’m closing this as already implemented rather than keeping a duplicate issue open.
Review notes: reviewed against 76a4c167f7ef; fix evidence: release v2026.4.22, commit 00bd2cf7a376.