fix(agents): sanitize tool pairing after compaction and history truncation#4852
Open
lailoo wants to merge 1 commit intoopenclaw:mainfrom
Open
fix(agents): sanitize tool pairing after compaction and history truncation#4852lailoo wants to merge 1 commit intoopenclaw:mainfrom
lailoo wants to merge 1 commit intoopenclaw:mainfrom
Conversation
…ation When compaction prunes older messages or limitHistoryTurns truncates history, tool_result entries can become orphaned if their matching tool_use was removed. This causes 'unexpected tool_use_id' API errors. Changes: - compaction-safeguard.ts: call sanitizeToolUseResultPairing() after pruning - history.ts: call sanitizeToolUseResultPairing() after truncation - Added 2 new tests for limitHistoryTurns tool pairing scenarios Fixes openclaw#4739, openclaw#4728, openclaw#4723, openclaw#3462, openclaw#3455, openclaw#4261, openclaw#4367, openclaw#4323, openclaw#4650
This was referenced Feb 2, 2026
Contributor
Additional Comments (1)
This test only checks that a Consider asserting the exact sequence and IDs (e.g., Prompt To Fix With AIThis is a comment left during a code review.
Path: src/agents/pi-embedded-runner.limithistoryturns.test.ts
Line: 185:205
Comment:
[P1] Test doesn’t actually assert toolCall/toolResult pairing
This test only checks that a `toolResult` remains and that the returned array length is 3, but it doesn’t verify that the retained `toolResult.toolCallId` matches a retained assistant `toolCall` (or that the `toolResult` is immediately after the `toolCall`). A regression where an orphan `toolResult` survives (or pairing order is wrong) could still pass.
Consider asserting the exact sequence and IDs (e.g., `limited[1]` is the toolCall with id `call_1` and `limited[2]` is the matching toolResult with `toolCallId === 'call_1'`).
How can I resolve this? If you propose a fix, please make it concise. |
bfc1ccb to
f92900f
Compare
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
When compaction prunes older messages or
limitHistoryTurnstruncates history,tool_resultentries can become orphaned if their matchingtool_usewas removed. This causesunexpected tool_use_idAPI errors.Changes
compaction-safeguard.ts
sanitizeToolUseResultPairing()afterpruneHistoryForContextShare()to clean up orphantool_resultentrieshistory.ts
sanitizeToolUseResultPairing()after truncating history inlimitHistoryTurns()Tests
limitHistoryTurnstool pairing scenarios:removes orphan toolResult after truncationpreserves valid tool pairs after truncationRoot Cause
Both compaction and history truncation can remove
assistantmessages containingtool_useblocks while leaving their correspondingtool_resultentries in the retained portion. This creates orphanedtool_resultentries that reference non-existenttool_useblocks.Testing
compaction-safeguard.test.tspasspi-embedded-runner.limithistoryturns.test.tspassFixes
Greptile Overview
Greptile Summary
This PR hardens transcript integrity by running
sanitizeToolUseResultPairing()after two history-shaping operations: compaction pruning (pruneHistoryForContextShare) and DM history truncation (limitHistoryTurns). This prevents “orphan”toolResultentries whose matchingtoolCall/tool_usemessages were removed, which can trigger strict-provider API errors likeunexpected tool_use_id.Changes are localized to the compaction safeguard extension and embedded runner history logic, plus two new unit tests covering tool pairing behavior during truncation.
Confidence Score: 4/5