Skip to content

Post-compaction message serialization drops tool_result blocks → permanent session death #40416

Description

@BooRy95

Bug Report

Version: 2026.3.7 (updated from 3.2 earlier today)
Platform: macOS (Darwin 25.3.0, arm64), Node v25.7.0
Model Provider: Anthropic (Claude Opus 4.6)

Summary

After a hook-triggered compaction, the API message reconstruction drops tool_result blocks while preserving their corresponding tool_use blocks. This creates a permanently malformed conversation history — every subsequent API call fails with:

messages.97: `tool_use` ids were found without `tool_result` blocks immediately after: call70362952. Each `tool_use` block must have a corresponding `tool_result` block in the next message.

The session becomes permanently unrecoverable. The only fix is /new or /reset, which loses the entire conversation.

Reproduction

This happened twice in one day on the same agent, both times after heavy sessions (~180K tokens) triggered hook-based compaction.

Sequence of events:

  1. Session reaches high token count (~180K tokens before compaction)
  2. Hook-triggered compaction fires (fromHook: true in compaction entry)
  3. Compaction completes — JSONL data is intact (both tool_use and tool_result entries are present and correctly linked via toolCallId)
  4. Next API request to Anthropic fails: the reconstructed messages array includes the tool_use block but drops its matching tool_result
  5. Every subsequent message fails with the same error — session is permanently dead
  6. From the user's perspective: ~1 hour of Discord messages "disappear" and the bot starts posting error messages

JSONL forensics:

  • Line 529: Assistant message with tool_use blocks: memory_search(call_70362952) and session_status(call_03536860)
  • Line 530: toolResult with toolCallId: call_70362952 (memory_search result) ✅ exists
  • Line 531: toolResult with toolCallId: call_03536860 (session_status result) ✅ exists
  • Line 523: Compaction event (firstKeptEntryId: 36559378, tokensBefore: 180355)
  • Line 535: First failed assistant response — empty content, stopReason: "error"

The data integrity is fine. The bug is in how the kept entries are serialized into the Anthropic API messages array after compaction.

Suspected Cause

The 2026.3.7 release includes postCompactionSections — a new feature that modifies what gets injected after compaction. This is the exact code path where the tool_use/tool_result pairing breaks. Both occurrences today happened after updating to 2026.3.7; this was not observed on 2026.3.2.

Impact

High / Disruptive:

  • Complete session loss (all conversation context gone)
  • No graceful recovery — must /new or /reset
  • User-visible: messages "disappear" from Discord timeline
  • Happened 2x in one day on a single agent
  • Any session that reaches compaction threshold is at risk

Environment

  • Agent: big-brain (primary agent, Opus 4.6)
  • Channel: Discord
  • Compaction trigger: hook-based (automatic, not manual)
  • Session JSONL preserved as .reset file for inspection

Workaround

None currently. /new or /reset is the only recovery, which loses all session context.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions