fix(sessions): truncate at incomplete tool calls instead of synthetic results#3565
fix(sessions): truncate at incomplete tool calls instead of synthetic results#3565kiranjd wants to merge 1 commit intoopenclaw:mainfrom
Conversation
… synthetic results When session history contains an assistant message with tool calls but missing results (e.g., due to branching, interruption, or crash), the API rejects the request. Previously, we tried to repair this by inserting synthetic error results, but this approach was fragile and could lead to confusing conversation states. New approach: truncate history before the first incomplete tool call sequence. This is simpler and always produces valid history: - Agent continues working (just with less context) - No confusing synthetic error results - User may need to repeat their last request The truncation is logged with details about which tool call IDs were missing, helping track and diagnose the root cause. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Additional Comments (2)
This is especially likely when the missing results are actually “orphaned on another branch” rather than truly absent. Consider checking for tool results only within the linearized path/after the call turn (or the same span the reordering logic already scans) instead of globally. Prompt To Fix With AIThis is a comment left during a code review.
Path: src/agents/session-transcript-repair.ts
Line: 80:83
Comment:
[P1] Truncation check can false-positive due to global toolResult scan
`findFirstIncompleteToolCallIndex` precomputes `allResultIds` by scanning *all* tool results anywhere in the transcript (`indexAllToolResultIds`). That means an assistant tool call will be considered “complete” if a matching `toolResult` exists later on a different branch/turn, even if it won’t be included after linearization (the exact failure mode described in the PR). In that scenario, this function won’t truncate and the provider will still reject the request.
This is especially likely when the missing results are actually “orphaned on another branch” rather than truly absent. Consider checking for tool results only within the linearized path/after the call turn (or the same span the reordering logic already scans) instead of globally.
How can I resolve this? If you propose a fix, please make it concise.
The truncation warning logs Prompt To Fix With AIThis is a comment left during a code review.
Path: src/agents/pi-embedded-runner/google.ts
Line: 341:348
Comment:
[P2] Log may include untrusted tool call IDs and get very large
The truncation warning logs `missingToolCallIds.join(", ")` inline. If tool call IDs can be influenced by user/session contents (or just be numerous), this can produce noisy/oversized logs and make parsing harder. Consider logging them as structured data (array field) and/or truncating the list/count (e.g., first N ids + total) to keep logs bounded.
How can I resolve this? If you propose a fix, please make it concise. |
bfc1ccb to
f92900f
Compare
Summary
When session history contains an assistant message with tool calls but missing results (due to branching, interruption, or crash), the API rejects the request with errors like:
This change makes agents recover automatically instead of getting stuck.
What users experience
Before (broken)
After (fixed)
Technical details
Root cause
Session files use a tree structure with branching. When linearizing the tree for API calls:
Previous approach (fragile)
Problems:
New approach (simple)
Code changes
session-transcript-repair.ts: NewfindFirstIncompleteToolCallIndex()function, truncation logicgoogle.ts: Log when truncation occurs for monitoringAlternatives considered
Testing
🤖 Generated with Claude Code
Greptile Overview
Greptile Summary
This PR changes session transcript recovery to avoid provider rejections when an assistant tool-call turn lacks matching tool results. Instead of inserting synthetic error tool results,
repairToolUseResultPairingnow detects the first assistant message with missing tool results and truncates history before it, then reorders any in-span tool results and drops duplicates/orphans.sanitizeSessionHistoryis updated to use the new repair report and log when truncation occurs, and tests were updated to cover the new truncation behavior.Confidence Score: 3/5
findFirstIncompleteToolCallIndextreats any matching toolResult anywhere in the transcript as satisfying a tool call, which can prevent truncation when results exist off-path (the described root cause). Logging change is low risk.Context used:
dashboard- CLAUDE.md (source)dashboard- AGENTS.md (source)