Skip to content

Session corruption: aborted tool calls cause tool_use_id mismatch errors #12112

@Legionair-4

Description

@Legionair-4

Summary

When a request is aborted mid-tool-call, OpenClaw's session repair mechanism creates a synthetic tool result, but the API subsequently rejects the session with:

messages.X.content.Y: unexpected tool_use_id found in tool_result blocks: toolu_XXXX. Each tool_result block must have a corresponding tool_use block in the previous message.

Steps to Reproduce

  1. Start a conversation that involves tool calls
  2. Interrupt/abort a request while a tool call is in progress (network hiccup, timeout, or user interruption)
  3. The assistant message gets stopReason: "aborted" with a partial/incomplete tool call (has partialJson)
  4. OpenClaw detects the orphan and inserts a synthetic error result:
    {"role":"toolResult","toolCallId":"toolu_XXXX","content":[{"type":"text","text":"[openclaw] missing tool result in session history; inserted synthetic error result for transcript repair."}],"isError":true}
  5. On next user message, the API rejects with tool_use_id mismatch

Root Cause

When reconstructing messages to send to the API, the aborted assistant message's tool_use block is either:

  • Not included (because it's incomplete/partial), OR
  • Malformed in a way the API doesn't recognize

But the synthetic tool_result IS included, creating a mismatch.

Session Log Evidence

Line N:   assistant → stopReason: "aborted", toolCall id: toolu_01Hoo9VQsRSw7Nafu2QFt8KK (partialJson present)
Line N+1: toolResult → toolCallId: toolu_01Hoo9VQsRSw7Nafu2QFt8KK (synthetic repair)
Line N+2: user message
Line N+3: assistant → stopReason: "error", API rejects with tool_use_id mismatch

Suggested Fix

When an aborted message contains a partial/incomplete tool call:

  1. Option A: Strip BOTH the aborted tool_use AND its synthetic result before sending to API
  2. Option B: Complete/normalize the tool_use structure so it's valid before adding a result
  3. Option C: Don't insert synthetic results for aborted tool calls at all

Workaround

Manually delete/reset the corrupted session file:

rm ~/.openclaw/agents/main/sessions/<session-id>.jsonl

Environment

  • OpenClaw: 2026.2.2-3
  • Model: claude-opus-4-5 (Anthropic)
  • OS: Ubuntu (WSL2)

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