openclaw - 💡(How to fix) Fix Session corruption from orphaned tool_result blocks survives restart (overload/timeout interruption) [1 comments, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#58608Fetched 2026-04-08 02:00:15
View on GitHub
Comments
1
Participants
1
Timeline
0
Reactions
0
Participants

Error Message

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.

  • session-transcript-repair.jsrepairToolUseResultPairing() drops orphans, deduplicates, inserts synthetic error results
RAW_BUFFERClick to expand / collapse

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

extent analysis

TL;DR

Run the repairToolUseResultPairing() function on session file load to prevent corruption and make sessions self-healing.

Guidance

  • Modify the session loading logic to run repairToolUseResultPairing() on the loaded session data to detect and fix any orphaned tool_result blocks.
  • Immediately rewrite the session file with the repaired data to ensure consistency across restarts.
  • Consider adding a check to prevent compaction from running while a tool call is in flight to avoid producing a compacted history with broken tool_use/tool_result pairing.
  • Review the transcript-sanitize.js and sanitizeSessionHistory() functions to ensure they are correctly sanitizing the session history before every API call.

Example

No explicit code example is provided, but the repairToolUseResultPairing() function in session-transcript-repair.js can be used as a reference for implementing the repair logic on session file load.

Notes

This solution assumes that the repairToolUseResultPairing() function is correctly implemented and effective in repairing corrupted session histories. Additionally, this fix may not address all possible corruption scenarios, and further testing and validation may be necessary to ensure its effectiveness.

Recommendation

Apply the suggested fix by running repairToolUseResultPairing() on session file load, as this will help prevent corruption and make sessions self-healing, reducing the need for manual intervention.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING