Skip to content

Compaction-triggered tool_use/tool_result mismatch (variant of #1685) #4739

@Gamblegrift

Description

@Gamblegrift

Description

We hit the tool_use/tool_result mismatch bug (#1685) through a compaction-specific path that isn't fully covered by the existing reports.

Our Reproduction

  1. Agent (Elliot/forge) ran a heavy session: 855 messages, 218K tokens — lots of image analysis, screenshots, file writes
  2. Safeguard compaction fired at the token limit (218,109 tokens)
  3. Compaction pruned older messages including some tool_use blocks
  4. Agent continued working after compaction — more tool calls from 15:20-15:26
  5. At 15:26:30, agent called write tool → result was a synthetic error: [clawdbot] missing tool result in session history; inserted synthetic error result for transcript repair.
  6. The synthetic result's tool_use_id (toolu_01ApYXdC1pESwmh5Tek2bMTq) referenced a tool_use block that had been pruned during compaction
  7. Every subsequent API call failed permanently

Key Difference from #1826/#2087

Those issues focus on interrupted/terminated tool calls (stopReason: "error"). Our case is different:

  • The tool call was not interrupted — the agent was actively working
  • The corruption came from compaction separating paired messages — the tool_use was in older history that got summarized away, but the tool_result survived in recent messages
  • The compaction summary itself already noted a prior repair: "Tool Failures: image: [clawdbot] missing tool result in session history"

Suggested Fix (extends #1826's proposal)

In addition to skipping errored assistant messages, repairToolUseResultPairing() should also:

  1. Validate that referenced tool_use blocks exist in the active context before inserting synthetic results
  2. Drop orphaned tool_results whose tool_use was compacted away (rather than keeping them)
  3. Post-compaction: re-validate tool_use/tool_result pairing integrity

Environment

  • Clawdbot version: 2026.1.24-3
  • Provider: Anthropic (Claude Opus 4.5)
  • Channel: Discord (multi-agent setup, forge agent)
  • Compaction mode: safeguard
  • Context pruning: cache-ttl, 1h TTL

Workaround

Deleted the corrupted session JSONL + cleared sessions.json, restarted gateway.

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