Skip to content

Compaction deletes thinkingSignature, causing Anthropic API rejection #27504

@similar-bespoke

Description

@similar-bespoke

Summary

sanitizeHistoryContentBlock() in the compaction pipeline unconditionally deletes thinkingSignature from thinking blocks. When these blocks later appear as the latest assistant message in the Anthropic API request, the API rejects with:

messages.N.content.M: `thinking` or `redacted_thinking` blocks in the latest assistant message 
cannot be modified. These blocks must remain as they were in the original response.

Steps to Reproduce

  1. Use Anthropic provider with extended thinking enabled (thinkingLevel: low or higher)
  2. Have a long-running session that accumulates enough history to trigger compaction
  3. After compaction, send a new message through the same session
  4. API rejects because the latest assistant message's thinking blocks are missing thinkingSignature

Root Cause

In pi-embedded (traced via dist), sanitizeHistoryContentBlock() contains:

if (type === "thinking") {
    if ("thinkingSignature" in entry) {
        delete entry.thinkingSignature;  // ← deletes the cryptographic signature
        truncated = true;
    }
}

The Anthropic API requires thinking blocks to be byte-identical to the original response, including the thinkingSignature field. Deleting it constitutes a modification.

Impact

  • Any session with extended thinking + enough history for compaction becomes permanently broken
  • The session cannot recover — every subsequent API call fails with the same error
  • Only fix is clearing the session entirely (deleting the JSONL)
  • Fresh sessions (e.g., WhatsApp messages that start new conversations) are unaffected

Observed Behaviour

  • Error first appeared after session accumulated 1685 events across 3 compactions
  • Error message index varies (messages.37, messages.79, messages.93) as compaction changes history length
  • Error persists across gateway restarts — the damage is in the stored session data
  • Tested on OpenClaw 2026.2.23 (b817600), Anthropic provider, claude-sonnet-4-6

Suggested Fix

Either:

  1. Preserve thinkingSignature during compaction — it's needed for API round-tripping
  2. Strip the entire thinking block instead of just the signature, if the goal is to save tokens
  3. Only strip thinkingSignature from non-latest assistant messages, since the API only validates the most recent one

Option 2 is probably safest — the thinking content is not useful after compaction, and removing the entire block avoids the signature mismatch entirely.

Environment

  • OpenClaw: 2026.2.23 (b817600)
  • Provider: Anthropic (claude-sonnet-4-6)
  • OS: macOS Darwin 25.3.0
  • Node: v22.22.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions