Skip to content

Session corruption from orphaned tool_result blocks survives restart (overload/timeout interruption) #58608

@funkylazer

Description

@funkylazer

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.jsrepairToolUseResultPairing() 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:

  1. If session loading itself fails on malformed data before the repair code runs, the session is unrecoverable without manual intervention
  2. Compaction running while a tool call is in flight could produce a compacted history with broken tool_use/tool_result pairing
  3. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions