Skip to content

Bug: Synthetic tool result causes invalid transcript after terminated tool call #2087

@khopilot

Description

@khopilot

Description

When a tool call is interrupted/terminated mid-execution, clawdbot inserts a "synthetic error result" to repair the transcript. However, when the session is later replayed for API calls, the tool_use/tool_result pairing becomes misaligned, causing Anthropic API to reject the request.

Error Message

LLM request rejected: messages.228.content.1: unexpected tool_use_id found in tool_result blocks: toolu_01TKLcHJJkbDgtJEvitNxEFg. Each tool_result block must have a corresponding tool_use block in the previous message.

Steps to Reproduce

  1. Start a conversation with an agent via Telegram
  2. Trigger a long-running tool call (e.g., exec command)
  3. Interrupt/terminate the agent mid-tool-call (send "stop" or similar)
  4. Continue the conversation
  5. Eventually the session becomes corrupted and all requests fail

Session Analysis

Looking at the corrupted session JSONL:

Line 255: assistant message with toolCall id="toolu_01TKLcHJJkbDgtJEvitNxEFg", stopReason="error", errorMessage="terminated"
Line 256: synthetic toolResult with matching toolCallId, content="[clawdbot] missing tool result..."
Line 259+: API errors referencing the same tool_use_id

The synthetic result IS in the session file directly after the assistant message, but when repairToolUseResultPairing() runs in-memory before API calls, the pairing is not correctly reconstructed.

Root Cause Hypothesis

The issue is in src/agents/session-transcript-repair.js:

  • The synthetic result is inserted into JSONL at the correct position
  • But when building API messages from session entries, the message indexing differs
  • The repair logic may be dropping the synthetic result as an "orphan" in edge cases

Environment

  • Clawdbot version: 2026.1.24-3
  • Provider: Anthropic (Claude Opus 4.5)
  • Channel: Telegram
  • Compaction mode: safeguard

Workaround

Delete the corrupted session file and restart:

rm ~/.clawdbot/agents/main/sessions/<session-id>.jsonl
systemctl --user restart clawdbot-gateway

Reported by: Nicolas Delrieu

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions