fix(agents): re-run tool_use/tool_result repair after limitHistoryTurns truncation#13974
Closed
zerone0x wants to merge 2 commits intoopenclaw:mainfrom
Closed
fix(agents): re-run tool_use/tool_result repair after limitHistoryTurns truncation#13974zerone0x wants to merge 2 commits intoopenclaw:mainfrom
zerone0x wants to merge 2 commits intoopenclaw:mainfrom
Conversation
derivePromptTokens() summed input + cacheRead + cacheWrite, but cache tokens are already part of the prompt — they don't consume additional context window capacity. With Anthropic prompt caching, cacheRead can easily exceed 100K, inflating the total past the 200K context cap and triggering premature auto-compaction. Use only `input` tokens for context-window percentage and compaction thresholds. Cache metrics are preserved in NormalizedUsage for cost reporting. Fixes openclaw#13853 Co-Authored-By: Claude <[email protected]>
…ns truncation
limitHistoryTurns() can orphan tool_result blocks when it slices off the
assistant message containing the corresponding tool_use. This causes
HTTP 400 errors from the Anthropic API ("unexpected tool_use_id found in
tool_result blocks").
Re-run sanitizeToolUseResultPairing() after limitHistoryTurns() in both
the compaction and reply flows, gated on the same transcript policy flag.
Fixes openclaw#13896
Co-Authored-By: Claude <[email protected]>
TWFBusiness
added a commit
to TWFBusiness/openclaw-tw
that referenced
this pull request
Feb 11, 2026
…pstream fixes - Remove Discord, Telegram, Slack, Signal, iMessage, BlueBubbles extensions - Remove @buape/carbon, grammy, and other gateway-specific dependencies - Add session-based login system (cookie auth, rate limiting) - Add type stubs for optional deps (baileys, ciao, qrcode-terminal) - Apply upstream PRs: XSS fix (openclaw#13952), orphan tool_results (openclaw#13974), Anthropic tool ID sanitization (openclaw#13976), BM25 score fix (openclaw#14005), redaction fix (openclaw#14006), subagent timeout (openclaw#13996), context window guard (openclaw#13986), model default reset (openclaw#13993), cache token accounting (openclaw#13853) - Fix all TypeScript compilation errors from channel removal Co-Authored-By: Claude Opus 4.6 <[email protected]>
Contributor
Author
|
🤖 Closing: the fix for |
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
Fixes #13896
limitHistoryTurns()can orphantool_resultblocks when it slices off the assistant message containing the correspondingtool_use. This causes HTTP 400 errors from the Anthropic API:Root cause
The message preparation pipeline runs
sanitizeSessionHistory()(which callsrepairToolUseResultPairing()) beforelimitHistoryTurns(). WhenlimitHistoryTurns()then slices the history by user-turn count, it can remove an assistant message containing atool_useblock while keeping the correspondingtool_resultin a later user message — re-introducing orphaned tool results after the repair already ran.Fix
Re-run
sanitizeToolUseResultPairing()afterlimitHistoryTurns()in both affected code paths:compact.ts)run/attempt.ts)The repair is gated on the same
transcriptPolicy.repairToolUseResultPairingflag used by the existing pre-truncation repair, so it only runs for providers that need it (Anthropic, Google).This follows the same pattern already used in
compaction.ts:339-357whererepairToolUseResultPairing()is called after dropping message chunks.Changes
src/agents/pi-embedded-runner/compact.ts— add post-truncationsanitizeToolUseResultPairing()callsrc/agents/pi-embedded-runner/run/attempt.ts— add post-truncationsanitizeToolUseResultPairing()callsrc/agents/pi-embedded-runner.limithistoryturns.test.ts— add regression tests for the orphan scenarioTest plan
limitHistoryTurns+sanitizeToolUseResultPairingdrops orphaned tool_results🤖 Generated with Claude Code (issue-hunter-pro)
Greptile Overview
Greptile Summary
Fixes orphaned
tool_resultblocks that occur whenlimitHistoryTurns()slices off assistant messages containingtool_useblocks while keeping their correspondingtool_resultin later messages. This was causing HTTP 400 errors from the Anthropic API with "unexpected tool_use_id found in tool_result blocks" errors.The fix re-runs
sanitizeToolUseResultPairing()afterlimitHistoryTurns()in both the compaction flow (compact.ts) and main reply flow (attempt.ts). The repair is gated on thetranscriptPolicy.repairToolUseResultPairingflag, so it only runs for providers that need it (Anthropic, Google).sanitizeToolUseResultPairing()call incompact.ts:440-442sanitizeToolUseResultPairing()call inattempt.ts:567-569usage.tsthat excludes cache tokens from context-window accounting (prevents premature auto-compaction)Confidence Score: 5/5