fix: repair tool_use/tool_result pairings after history truncation#4387
Closed
spiceoogway wants to merge 3 commits intoopenclaw:mainfrom
Closed
fix: repair tool_use/tool_result pairings after history truncation#4387spiceoogway wants to merge 3 commits intoopenclaw:mainfrom
spiceoogway wants to merge 3 commits intoopenclaw:mainfrom
Conversation
…ixes openclaw#4367) The message processing pipeline had a synchronization bug where limitHistoryTurns() truncated conversation history AFTER repairToolUseResultPairing() had already fixed tool_use/tool_result pairings. This could split assistant messages (with tool_use) from their corresponding tool_result blocks, creating orphaned tool_result blocks that the Anthropic API rejects. This fix calls sanitizeToolUseResultPairing() AFTER limitHistoryTurns() to repair any pairings broken by truncation, ensuring the transcript remains valid before being sent to the LLM API. Changes: - Added import for sanitizeToolUseResultPairing from session-transcript-repair.js - Call sanitizeToolUseResultPairing() on the limited message array - Updated variable name from 'limited' to 'repaired' for clarity
480fd42 to
8324201
Compare
Contributor
Author
|
Rebased on latest main to fix stale lockfile failures (moltbot→openclaw rename). The remaining CI failures are pre-existing on main:
None of these are caused by this PR's changes. |
…runcation Add tests for PR openclaw#4387 covering three critical scenarios: 1. Orphaned tool_use (tool call without result after truncation) - verifies synthetic error result insertion 2. Orphaned tool_result (result without call after truncation) - verifies orphan dropping 3. Normal history (well-formed transcript) - verifies no-op behavior Also tests edge cases: - Multiple orphaned tool_use blocks - Mixed scenario with some results present, some missing All tests pass. Fixes openclaw#4367
Contributor
Author
|
Closing as duplicate of #4736, which is a superset of this fix — it applies the same sanitizeToolUseResultPairing() call after limitHistoryTurns in attempt.ts, plus also fixes compact.ts and re-runs validateGeminiTurns/validateAnthropicTurns. Both address the same root cause (broken tool_use/tool_result pairing after history truncation). |
spiceoogway
added a commit
to spiceoogway/openclaw
that referenced
this pull request
Jan 31, 2026
…runcation Add tests for PR openclaw#4387 covering three critical scenarios: 1. Orphaned tool_use (tool call without result after truncation) - verifies synthetic error result insertion 2. Orphaned tool_result (result without call after truncation) - verifies orphan dropping 3. Normal history (well-formed transcript) - verifies no-op behavior Also tests edge cases: - Multiple orphaned tool_use blocks - Mixed scenario with some results present, some missing All tests pass. Fixes openclaw#4367
4 tasks
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 #4367 - tool_use_id mismatch error after limitHistoryTurns truncation
Problem
The message processing pipeline had a synchronization bug:
sanitizeSessionHistory()callsrepairToolUseResultPairing()to fix tool_use/tool_result pairingslimitHistoryTurns()runs AFTER the repair, truncating conversation historytool_use) and itstool_result, creating orphanedtool_resultblocksunexpected tool_use_id found in tool_result blocksSolution
Call
sanitizeToolUseResultPairing()AFTERlimitHistoryTurns()to repair any pairings broken by truncation.Changes
sanitizeToolUseResultPairingfromsession-transcript-repair.jssanitizeToolUseResultPairing()on the limited message arraylimitedtorepairedfor clarityTesting
pnpm lint)pnpm format)npm run build)Files Modified
src/agents/pi-embedded-runner/run/attempt.ts