Skip to content

Auto-repair orphaned tool_use blocks in session history #19048

Description

@bharatindia123-a11y

Bug

Recurring corruption where a tool_use block gets persisted to session JSONL but its matching tool_result does not (likely due to crash/timeout mid-tool-call). This makes every subsequent API call to that session fail Anthropic's message validation:

messages.251: `tool_use` ids were found without `tool_result` blocks immediately after: call95378438

Once corrupted, the session is permanently broken — gateway restarts don't help because the history is persisted to disk. The only fix is manually deleting/moving the session JSONL file, which loses all conversation history.

Occurrences

  • 2026-02-10: Session session:agent:kalki:main:thread:986992, orphaned toolu_01XXejQsDDVRysXnYjVyJmue at messages.322
  • 2026-02-17: Session kalki-bd/9397c2ef...topic-1771220855, orphaned call95378438 at messages.251

Proposed Fix

Add a validation/repair layer that runs before sending messages to the API:

  1. On session load: Scan for tool_use blocks without a matching tool_result in the next message
  2. Auto-repair: Inject a synthetic tool_result with an error like "Tool call interrupted — session recovered"
  3. Log a warning when auto-repair kicks in so it's visible in diagnostics

This should be a lightweight pass over the message array before API submission — no need to modify the JSONL on disk.

Workaround

Move/delete the corrupted session JSONL file and restart the gateway. The agent starts a fresh session but loses all prior context.

Metadata

Metadata

Assignees

No one assigned

    Labels

    staleMarked as stale due to inactivity

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions