You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Context pruning can split tool_use/tool_result pairs, causing API validation errors #46319
limitHistoryTurns() — slices messages to keep only the last N user turns
replaceMessages() — feeds the result to the agent
Context pruning extension fires on the context event (soft-trims / hard-clears tool result content in-place, does NOT remove messages)
The primary issue:limitHistoryTurns() (step 3) slices the message array on user-turn boundaries after repair has already run. This slice can cut between an assistant tool_use message and its tool_result, orphaning one side. There is no repair step after limitHistoryTurns.
Note: The context pruning extension itself (pruner.js) is actually safe — it only replaces tool result content with placeholders/trimmed versions, never removes messages from the array. The transcript-sanitize extension also exists with repair logic, but doesn't appear to be registered in buildEmbeddedExtensionPaths().
Symptoms
Sessions crash with Anthropic API errors about unexpected tool_result blocks (no matching tool_use) or orphaned tool_use blocks without a corresponding tool_result
Affected sessions become unrecoverable without manual intervention (compaction or session reset)
Most likely to trigger in long-running DM sessions with dmHistoryLimit configured
Suggested Fixes
Move repair after limitHistoryTurns: Run repairToolUseResultPairing() (or at minimum a lightweight orphan check) afterlimitHistoryTurns() slices the array — this is the simplest targeted fix.
Make limitHistoryTurns tool-pair-aware: When counting user turns for the slice point, ensure the cut doesn't land between an assistant+toolCall and its toolResult. If it would, include the full tool exchange.
Register transcript-sanitize extension: The transcript-sanitize.js extension already exists and runs repairToolUseResultPairing on the context event — registering it in buildEmbeddedExtensionPaths() would add a safety net that catches any orphans right before the API call.
Current main already applies a second tool-use/tool-result repair pass immediately after limitHistoryTurns(), which directly addresses the reported truncation bug. The shared repair helper also drops free-floating orphan toolResult entries, and the same post-truncation safeguard is present in both normal embedded runs and compaction flows.
Shared repair drops orphan tool results:repairToolUseResultPairing documents strict-provider rejection for displaced tool results and drops any free-floating toolResult message that is not directly attached to a matching assistant tool-call turn. (src/agents/session-transcript-repair.ts:420, 564f820efa2b)
Compaction path has same safeguard: The compaction pipeline also reruns sanitizeToolUseResultPairing(...) after limitHistoryTurns(), so the same orphaning bug is covered there too. (src/agents/pi-embedded-runner/compact.ts:954, 564f820efa2b)
Original slicer is still naive, but wrapped by repair:limitHistoryTurns() still slices purely on user-turn boundaries via messages.slice(lastUserIndex), which matches the issue’s root cause analysis; current main fixes that by repairing after the slice instead of making the slicer pair-aware. (src/agents/pi-embedded-runner/history.ts:17, c13836804058)
Repair behavior is covered by tests:session-transcript-repair.test.ts includes coverage that orphan tool results are dropped, which is the key expected behavior after post-limit repair runs. (src/agents/session-transcript-repair.test.ts:126, 564f820efa2b)
So I’m closing this as already implemented rather than keeping a duplicate issue open.
Activity
steipete commentedon Apr 25, 2026
Closing this as implemented after Codex review.
Current
mainalready applies a second tool-use/tool-result repair pass immediately afterlimitHistoryTurns(), which directly addresses the reported truncation bug. The shared repair helper also drops free-floating orphantoolResultentries, and the same post-truncation safeguard is present in both normal embedded runs and compaction flows.What I checked:
run/attempt.tsnow truncates history, then immediately rerunssanitizeToolUseResultPairing(...)with an inline comment explaining thatlimitHistoryTurnscan orphan tool results. (src/agents/pi-embedded-runner/run/attempt.ts:1631, 564f820efa2b)repairToolUseResultPairingdocuments strict-provider rejection for displaced tool results and drops any free-floatingtoolResultmessage that is not directly attached to a matching assistant tool-call turn. (src/agents/session-transcript-repair.ts:420, 564f820efa2b)sanitizeToolUseResultPairing(...)afterlimitHistoryTurns(), so the same orphaning bug is covered there too. (src/agents/pi-embedded-runner/compact.ts:954, 564f820efa2b)limitHistoryTurns()still slices purely on user-turn boundaries viamessages.slice(lastUserIndex), which matches the issue’s root cause analysis; current main fixes that by repairing after the slice instead of making the slicer pair-aware. (src/agents/pi-embedded-runner/history.ts:17, c13836804058)session-transcript-repair.test.tsincludes coverage that orphan tool results are dropped, which is the key expected behavior after post-limit repair runs. (src/agents/session-transcript-repair.test.ts:126, 564f820efa2b)So I’m closing this as already implemented rather than keeping a duplicate issue open.
Review notes: reviewed against c13836804058; fix evidence: commit 564f820efa2b.