Skip to content

Orphaned tool_result after mid-stream assistant error causes permanent session breakage #5497

@brezgis

Description

@brezgis

Summary

When an assistant message errors mid-stream during a tool_use call (e.g., web_fetch timing out), the transcript repair logic can produce an orphaned tool_result block with no matching tool_use in the preceding assistant message. This causes every subsequent API request to fail with a 400 error, permanently breaking the session until manual JSONL surgery is performed.

Environment

Clawdbot version: 2026.1.24-3
Provider: Anthropic (claude-opus-4-5)
Channel: Discord
OS: Ubuntu Linux (Hetzner VPS)

Steps to Reproduce

Agent calls web_fetch (or any tool) during a response
The API call errors mid-stream (stopReason: "error")
The assistant message with the tool_use block is partially persisted to the session JSONL
Clawdbot's transcript repair injects a synthetic tool_result for the errored call
On next turn, the Anthropic API sees the tool_result referencing toolu_XXXX but the corresponding tool_use block is missing from the assistant message (because it was only partially written)
API returns 400: unexpected tool_use_id found in tool_result blocks
Every subsequent message hits the same 400 — the session is permanently broken

Expected Behavior

The transcript hygiene / sanitization logic should:
Detect orphaned tool_result blocks (tool_result with no matching tool_use in the preceding assistant message)
Either remove the orphaned tool_result, or inject a synthetic tool_use to match it
Prevent the session from entering an unrecoverable state

Actual Behavior

The session enters a permanent error loop. The only recovery is manual truncation of the session JSONL file.

Workaround

Locate the session file at ~/.clawdbot/agents/main/sessions/.jsonl, find the line number of the broken entry, and truncate:

cp session.jsonl session.jsonl.bak
head -n session.jsonl.bak > session.jsonl

Notes

The transcript hygiene docs indicate that Anthropic provider policy includes "tool result pairing repair and synthetic tool results" — but this appears to only handle the case of missing tool_results (tool_use without a result), not the reverse case of missing tool_use (orphaned tool_result). The reverse case should be covered too.

This issue was written collaboratively with my Clawdbot agent (Claude). The bug was encountered and manually debugged by a human; the writeup was drafted by the agent based on the diagnosis.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions