fix(agent): sanitize messages after orphan user repair#4009
fix(agent): sanitize messages after orphan user repair#4009drag88 wants to merge 1 commit intoopenclaw:mainfrom
Conversation
When repairing orphaned trailing user messages, buildSessionContext() was called but messages bypassed the sanitization pipeline. This could cause tool_result blocks without matching tool_use blocks, leading to Anthropic API "unexpected tool_use_id" errors. Apply full sanitization (sanitizeSessionHistory, validateTurns, limitHistoryTurns) to rebuilt messages.
Additional Comments (1)
This block duplicates the earlier sanitize/validate/limit logic at Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! Prompt To Fix With AIThis is a comment left during a code review.
Path: src/agents/pi-embedded-runner/run/attempt.ts
Line: 714:733
Comment:
[P2] Consider reusing the existing sanitization/validation/limit pipeline to avoid drift.
This block duplicates the earlier sanitize/validate/limit logic at `attempt.ts:502-525`, so future edits (e.g., adding a new validation step or recording cacheTrace stages) could update one path but not the other. A small local helper (or extracting the existing pipeline into a function) would keep both call sites in sync.
<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>
How can I resolve this? If you propose a fix, please make it concise. |
bfc1ccb to
f92900f
Compare
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
|
This pull request has been automatically marked as stale due to inactivity. |
|
Closing due to inactivity. |
Summary
When repairing orphaned trailing user messages,
buildSessionContext()was called but messages bypassed the sanitization pipeline. This could causetool_resultblocks without matchingtool_useblocks, leading to Anthropic API rejecting requests with "unexpected tool_use_id" errors.The fix: Apply the full sanitization pipeline (
sanitizeSessionHistory,validateGeminiTurns,validateAnthropicTurns,limitHistoryTurns) to rebuilt messages, matching the behavior of the initial sanitization at lines 502-524.Test Plan
npm run lint)AI-Assisted PR 🤖
buildSessionContext()after repairing orphaned user messages, they go through the same sanitization pipeline as the initial message processing, preventing tool_use/tool_result pairing mismatchesContext
The bug was discovered when a WhatsApp bot session had an aborted request that created a branch in the conversation tree. When linearized, a
toolResultwas included without its correspondingtool_useblock, causing the Anthropic API to reject the request.Greptile Overview
Greptile Summary
This change fixes a subtle session-repair edge case in
runEmbeddedAttempt()where rebuilding session context after repairing an orphaned trailing user message would bypass the normal transcript sanitization/validation pipeline. The new code runs rebuilt messages throughsanitizeSessionHistory(), optional turn validators (validateGeminiTurns/validateAnthropicTurns), andlimitHistoryTurns()before callingactiveSession.agent.replaceMessages(), aligning behavior with the initial session sanitization done earlier in the function.Overall this should prevent malformed tool_use/tool_result pairings (e.g., orphaned
tool_resultblocks) from reaching provider APIs like Anthropic and causing request rejections ("unexpected tool_use_id").Confidence Score: 4/5
(2/5) Greptile learns from your feedback when you react with thumbs up/down!