anomalyco / anomalyco/opencode

compaction sends mutated thinking block -> permanent 400 retry loop

Open
#39,291 2 comments 0 reactions 1 assignee View on GitHub

@kitlangton is already working on this.

Since Jul 28, 2026.

Dominant language
TypeScript
Stars
209k
Forks
27.5k
PR merge metrics
PR metrics pending

Description

Bug: compaction sends mutated thinking block, causes permanent retry loop (400 invalid_request_error)

opencode version: 1.16.2
Provider: anthropic, model claude-sonnet-5, extended thinking enabled (budget_tokens 8000)
Agent: compaction

What happened

During context compaction, opencode rebuilds the request history and reuses a prior assistant
turn's thinking block, but the block's thinking text field comes back empty ("thinking":"")
while the signature is preserved from the original. Anthropic's API rejects this because thinking
blocks in the last assistant message must be sent back byte-identical to how they were returned.

Because compaction retries automatically and rebuilds the same mutated payload every time, the
session gets stuck in a permanent retry loop — same 400 error, dozens of times over roughly an
hour, never recovering. Only a fresh session avoids it.

Error (from Anthropic API, relayed by opencode)
AI_APICallError
messages.N.content.1: `thinking` or `redacted_thinking` blocks in the latest assistant message
cannot be modified. These blocks must remain as they were in the original response.

N varied across retries (7, 1, 5, 3, ...) — consistent with the mutated block shifting position
as compaction repeatedly re-summarizes a growing/changing history.

Repro conditions
  • Long-running session that triggers auto-compaction with extended thinking enabled.
  • Compaction request's latest assistant message contains a thinking block whose thinking text
    is "" but signature is non-empty (from the original response) — i.e. the block was stripped/
    edited before resending.
Expected

Compaction should either preserve thinking blocks completely unmodified when replaying them, or
strip them entirely (not send thinking type block with empty text + stale signature) when
building the summarization request.

Additional observation

Traffic in the failing session routed through http://127.0.0.1:4097/v1/messages — a local proxy
in front of the Anthropic API — worth checking whether the proxy or opencode itself is the one
truncating the thinking block content before the request leaves the client.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.