Skip to content

Instantly share code, notes, and snippets.

@piotrmasior
piotrmasior / fix-orphaned-tool-result.md
Created February 7, 2026 22:25
Fix: Orphaned tool_result after aborted assistant messages — proposal

Fix: Orphaned tool_result After Aborted Assistant Messages

The Bug (TL;DR)

When an assistant message is aborted mid-stream (e.g., after context compaction at 200k tokens), the toolCall blocks with partialJson get persisted to the session JSONL. OpenClaw's repair mechanism inserts synthetic tool_result entries for these. But at API call time, transformMessages() drops the aborted assistant entirely (stopReason === "aborted") while keeping the synthetic tool_result — creating orphaned results that make every subsequent API call fail with unexpected tool_use_id.

Root Cause Chain

Stream abort (pi-ai/anthropic.js)