Problem
When a session is interrupted mid-tool-call (overload, timeout, rate limit), the conversation history can end up with orphaned tool_result blocks that have no matching tool_use before them. On the next API call, Anthropic rejects the malformed history (e.g. messages.36 validation error), and the session is effectively dead until manually nuked.
This has been a recurring pattern — sessions get corrupted after just a few hundred lines of history.
Current Repair Infrastructure
Clawdbot already has solid repair logic:
session-transcript-repair.js → repairToolUseResultPairing() drops orphans, deduplicates, inserts synthetic error results
session-tool-result-guard.js → tracks pending tool calls and auto-flushes synthetics on next append
transcript-sanitize.js → extension that runs repair on every context build
sanitizeSessionHistory() in attempt.js → calls sanitizeToolUseResultPairing for Anthropic models before every API call
The Gap
The repair runs on context build (in-memory, before API call), but the raw session file on disk remains corrupted. This means:
- If session loading itself fails on malformed data before the repair code runs, the session is unrecoverable without manual intervention
- Compaction running while a tool call is in flight could produce a compacted history with broken tool_use/tool_result pairing
- The corruption persists across restarts since the session file is never rewritten with repaired data
Suggested Fix
Run repairToolUseResultPairing() (or equivalent) on session file load as well, not just on context build. If the loaded session has orphaned/displaced tool results, repair and rewrite the file immediately. This would make sessions self-healing on restart instead of requiring manual /new or /reset.
Environment
- Version: 2026.1.24-3 (latest)
- Model: Anthropic Claude (Opus/Sonnet)
- Channel: Telegram
Problem
When a session is interrupted mid-tool-call (overload, timeout, rate limit), the conversation history can end up with orphaned
tool_resultblocks that have no matchingtool_usebefore them. On the next API call, Anthropic rejects the malformed history (e.g.messages.36validation error), and the session is effectively dead until manually nuked.This has been a recurring pattern — sessions get corrupted after just a few hundred lines of history.
Current Repair Infrastructure
Clawdbot already has solid repair logic:
session-transcript-repair.js→repairToolUseResultPairing()drops orphans, deduplicates, inserts synthetic error resultssession-tool-result-guard.js→ tracks pending tool calls and auto-flushes synthetics on next appendtranscript-sanitize.js→ extension that runs repair on every context buildsanitizeSessionHistory()inattempt.js→ callssanitizeToolUseResultPairingfor Anthropic models before every API callThe Gap
The repair runs on context build (in-memory, before API call), but the raw session file on disk remains corrupted. This means:
Suggested Fix
Run
repairToolUseResultPairing()(or equivalent) on session file load as well, not just on context build. If the loaded session has orphaned/displaced tool results, repair and rewrite the file immediately. This would make sessions self-healing on restart instead of requiring manual/newor/reset.Environment