Skip to content

Orphan tool_result causes API rejection after terminated request #5518

@IDLEcreative

Description

@IDLEcreative

Bug Description

When an assistant message with a tool_use block is terminated/aborted mid-execution (stopReason: "error", errorMessage: "terminated"), the transcript repair logic inserts a synthetic tool_result for the orphaned tool call. However, when building the API payload, the terminated assistant message appears to be excluded while the synthetic tool_result is still included, causing an API rejection:

400 {"type":"error","error":{"type":"invalid_request_error","message":"messages.108.content.1: unexpected `tool_use_id` found in `tool_result` blocks: toolu_014PfKm8xZYg6Q6ZnotRoM54. Each `tool_result` block must have a corresponding `tool_use` block in the previous message."}}

Steps to Reproduce

  1. Start a conversation that uses tools
  2. Terminate/abort a request mid-tool-call (e.g., kill the process, network interruption)
  3. The session file now has an assistant message with stopReason: "error" containing a tool_use block
  4. The repair logic in session-transcript-repair.js inserts a synthetic tool_result
  5. On next request, the API rejects due to orphan tool_result

Expected Behavior

The repair logic should either:

  • Not insert synthetic tool_results for assistant messages that will be excluded from the API payload (those with stopReason: "error")
  • OR ensure the terminated assistant message (with its tool_use) is included when its synthetic tool_result is included

Workaround

Clear the session file:

rm ~/.clawdbot/agents/main/sessions/*.jsonl

Environment

  • clawdbot version: (latest as of 2026-01-31)
  • Provider: Anthropic
  • Model: claude-opus-4-5

Relevant Code

The repair logic is in dist/agents/session-transcript-repair.js - the repairToolUseResultPairing function correctly handles orphan tool_results but doesn't account for the case where the parent assistant message is excluded due to error status.

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