Agents: fix orphaned toolResult after history truncation#5738
Closed
henryjrobinson wants to merge 1 commit intoopenclaw:mainfrom
Closed
Agents: fix orphaned toolResult after history truncation#5738henryjrobinson wants to merge 1 commit intoopenclaw:mainfrom
henryjrobinson wants to merge 1 commit intoopenclaw:mainfrom
Conversation
When `limitHistoryTurns()` truncates conversation history (based on `dmHistoryLimit` config), it could leave orphaned `toolResult` messages whose matching `toolCall` was in the truncated portion. This caused Anthropic API errors like: "unexpected tool_use_id found in tool_result blocks" Fix by detecting and dropping orphaned toolResult messages after truncation. Added helper functions to extract tool call IDs from assistant messages and filter out toolResults referencing missing IDs. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Comment on lines
+57
to
+63
| // Filter out toolResult messages that reference missing tool calls | ||
| return messages.filter((msg) => { | ||
| if (msg.role !== "toolResult") return true; | ||
| const id = extractToolResultId(msg); | ||
| // Keep if we can't determine the ID (defensive) or if the ID exists | ||
| return !id || availableToolCallIds.has(id); | ||
| }); |
Contributor
There was a problem hiding this comment.
[P1] dropOrphanedToolResults() currently keeps toolResult messages when it can’t extract an ID (return !id || availableToolCallIds.has(id)). If the API validation error is triggered by a mismatched/unknown tool result identifier field (or a renamed field you didn’t anticipate), this “defensive keep” path can still leave the request invalid after truncation. Consider flipping this to drop tool results with an unrecognized/missing ID when truncation happens (or at least when msg.role === "toolResult"), since a tool result without a resolvable ID can’t be matched anyway.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/pi-embedded-runner/history.ts
Line: 57:63
Comment:
[P1] `dropOrphanedToolResults()` currently *keeps* toolResult messages when it can’t extract an ID (`return !id || availableToolCallIds.has(id)`). If the API validation error is triggered by a mismatched/unknown tool result identifier field (or a renamed field you didn’t anticipate), this “defensive keep” path can still leave the request invalid after truncation. Consider flipping this to drop tool results with an unrecognized/missing ID when truncation happens (or at least when `msg.role === "toolResult"`), since a tool result without a resolvable ID can’t be matched anyway.
How can I resolve this? If you propose a fix, please make it concise.
4 tasks
Member
|
Closing as duplicate of #2557. If this is incorrect, comment and we can reopen. |
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 the "unexpected tool_use_id found in tool_result blocks" API error that occurs when
limitHistoryTurns()truncates conversation history.dmHistoryLimitconfig is set, history truncation could leave orphanedtoolResultmessages whose matchingtoolCallwas in the truncated portionChanges
history.tsto extract tool call IDs and detect orphanslimitHistoryTurns()to calldropOrphanedToolResults()after truncationTest plan
limitHistoryTurnstests passpnpm buildsucceedspnpm lintpasses🤖 Generated with Claude Code
Greptile Overview
Greptile Summary
This PR updates
limitHistoryTurns()to post-process truncated history and droptoolResultmessages whose referenced tool call no longer exists in the kept assistant messages. It adds small helper extractors inhistory.tsto collect tool call IDs from assistant content blocks and to read tool call IDs from toolResult messages, plus new unit tests that cover orphaned tool results (includingtoolUsenaming).Confidence Score: 4/5
(2/5) Greptile learns from your feedback when you react with thumbs up/down!
Context used:
dashboard- CLAUDE.md (source)dashboard- AGENTS.md (source)