Repository navigation
Conversation
…ssage mutations After the initial sanitizeToolUseResultPairing() call during session setup, several operations mutate activeSession.messages without re-running the pairing repair (orphan user-message removal, history image pruning). If any of these steps orphans a tool_use block, the corrupted transcript is sent to the Anthropic API which rejects it permanently, breaking the session. Add a final sanitizeToolUseResultPairing() pass after all late-stage message mutations and before the prompt is sent to the LLM.
tonyzorin
marked this pull request as draft
March 6, 2026 11:03
Contributor
Greptile SummaryThis PR adds a final
Confidence Score: 4/5
Last reviewed commit: 4629b79 |
tonyzorin
marked this pull request as ready for review
March 6, 2026 11:08
|
This pull request has been automatically marked as stale due to inactivity. |
|
Closing due to inactivity. |
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
Describe the problem and fix in 2–5 bullets:
sanitizeToolUseResultPairing()call at session setup (~line 1333 inattempt.ts), several operations mutateactiveSession.messagesviareplaceMessages()without re-running the pairing repair: orphaned user-message removal and history image pruning (pruneProcessedHistoryImages).tool_useblock (one without a matchingtool_resultin the next message), the corrupted transcript is sent to the Anthropic API which rejects it withinvalid_request_error. The error is then persisted in the session transcript, permanently breaking the session — every subsequent request fails with the same error.sanitizeToolUseResultPairing()pass after all late-stage message mutations and before the prompt is sent to the LLM.Change Type (select all)
Scope (select all touched areas)
Linked Issue/PR
User-visible / Behavior Changes
None. The change is a defensive no-op when tool_use/tool_result pairing is already correct (returns the same array reference). Users affected by the bug will no longer have their sessions permanently broken.
Security Impact (required)
NoNoNoNoNoRepro + Verification
Environment
Steps
tool_use/tool_resultpair gets orphaned during late-stage message manipulation (orphan removal or image pruning)messages.N: tool_use ids were found without tool_result blocks immediately after: <id>Expected
Session continues working normally after compaction/pruning.
Actual
Session is permanently broken with
invalid_request_erroron every request.Evidence
dist/pi-embedded-CtM2Mrrj.json a live gateway (v2026.3.2) and confirmed the fix resolved the issue for a session that had been broken for 2 days.pnpm build && pnpm check && pnpm testpass on the source: 829 test files, 6823 tests, 0 failures.Human Verification (required)
What you personally verified (not just CI), and how:
call_68354307orphan) resumed working immediately.replaceMessagescall).Compatibility / Migration
YesNoNoFailure Recovery (if this breaks)
src/agents/pi-embedded-runner/run/attempt.tsreplaceMessagescalls (would show as log entries if verbose logging is enabled).Risks and Mitigations
sanitizeToolUseResultPairing()call adds a small amount of processing per run.