Skip to content

fix(amazon-bedrock): long Claude sessions get turns rejected with "thinking block bound to a different conversation" - #208704

Merged
obviyus merged 4 commits into
mainfrom
bugloop-208675
Nov 30, 2026
Merged

obviyus merged 4 commits into
mainfrom
bugloop-208675

Conversation

@obviyus

@obviyus obviyus commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Root cause: extensions/amazon-bedrock/stream.runtime.ts:1104 (:1155 in 2026.11.10) builds adaptive Claude thinking for Bedrock Converse without thinking.block_binding or the thinking-binding-controls-2026-08-01 beta, which the direct Anthropic route adds through the shared applyAnthropicThinkingBindingControls. So any replayed thinking block whose earlier history changed is rejected instead of dropped.

Fixes #208675. Thanks @michaeljgood71 for the report, the boto3 check and the patch that pointed straight at the fix.

What Problem This Solves

Fixes: long Bedrock Claude sessions with adaptive thinking get the first request of a turn rejected ("Invalid signature in thinking block. The block is bound to a different conversation") whenever earlier history changed, for example after cache-TTL pruning, compaction or a model switch. OpenClaw then retries without thinking and drops the session's earlier reasoning.

User Impact

Bedrock Claude requests with adaptive thinking now carry the same thinking-binding controls as direct Anthropic API-key requests. Bedrock drops only the stale thinking blocks, the turn succeeds on the first request, and the rest of the reasoning stays in the session. Budget-based Bedrock thinking, GovCloud (which already omits thinking.display for its older schema), and the direct Anthropic request are unchanged.

Why This Change Was Made

#136782 added the controls for direct API-key requests only and left Bedrock on reject-and-retry. The reporter showed Bedrock Converse accepts both controls and drops the stale block. The Bedrock request builder now calls the shared helper on its adaptive branch, so it follows the same rule (adaptive thinking only) and the same beta value, sent as Converse anthropic_beta. The helper is re-exported from the existing private-local provider-stream-shared SDK seam; no new helper or policy copy. Docs updated where they listed Bedrock as excluded.

Not covered: #198838 (custom provider ids on api.anthropic.com). Its cause is the separate direct-route gate isDirectAnthropicModel requiring provider id anthropic, which this Bedrock path never goes through.

Evidence

Isolated built Gateway, real openclaw agent turns in one session, au.anthropic.claude-sonnet-5-5 on amazon-bedrock pointed at a local Converse stand-in. The stand-in signs each thinking block over the exact request prefix and rejects a replayed block whose prefix changed with Bedrock's ValidationException unless both drop_block and the beta are present. Turn 1 reads four files, turns 2-4 are plain, then the contextPruning: cache-ttl TTL expires and turn 5 trims turn 1's tool results (the reporter's setting), turn 6 is plain. Two runs per side, identical:

  • main (46e0d90): turn 5 and turn 6 each rejected once (400), 4 [session-recovery] lines (retrying once without thinking blocks, stripped thinking blocks ... repaired=5, unable to repair rejected thinking replay), later requests replay 0 thinking blocks.
  • this branch: 0 rejected requests, 0 [session-recovery] lines; turn 5 replays 5 blocks (4 stale dropped by the stand-in, 1 kept), turn 6 replays 6.

Controls: a Bedrock model without adaptive thinking (Haiku 4.5, budget thinking) sends identical requests on both builds (9/9 after normalizing temp paths and timestamps). The bundled anthropic provider, through a local TLS stand-in for api.anthropic.com, sends byte-identical requests and anthropic-beta headers on both builds (4/4); built packages/ai/dist is identical. Regression: the Bedrock request-composition table fails on main (8 cases, no block_binding/anthropic_beta) and passes here; GovCloud adaptive and budget rows assert no controls. agent-tools.prefix-stability, the Anthropic binding transport parity test and the Bedrock stream, replay and plugin tests pass.

Observed proof lines: #208704 (comment)

Merge danger: two-way door. A revert restores the old request shape; nothing is stored or migrated. Blast radius: Bedrock Claude requests with adaptive thinking outside GovCloud. If a Bedrock region rejected the controls, those requests would fail rather than fall back; the reporter's boto3 Converse checks on Bedrock accepted them.

…e requests

Bedrock Converse adaptive Claude requests now carry thinking.block_binding drop_block and the thinking-binding-controls-2026-08-01 beta through the shared applyAnthropicThinkingBindingControls helper, so replayed thinking bound to an older prefix is dropped server-side instead of rejecting the turn and stripping all thinking. GovCloud keeps rejection recovery, matching its display omission.
@clawsweeper

clawsweeper Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Pull request received.

ClawSweeper review complete

ClawSweeper finished reviewing this revision.

View the workflow run.

@obviyus

obviyus commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator Author

Observed proof (isolated built Gateways, local Bedrock Converse and api.anthropic.com stand-ins, no real credentials):

# #208675 proof: Bedrock adaptive Claude requests omit thinking-binding controls

Base: 46e0d90f6717fbc0c67d15b38c23b385fb3f86f1 (origin/main, merge base), `ocx base` copy (dist-nodts).
Fix:  31fcee1579cf (branch bugloop-208675), `ocx build --dist` in the worktree (dist-nodts); the controls
      that compare request bytes also use `ocx base 31fcee1579cf` (a .git-less copy, same shape as the base copy).

Root cause: extensions/amazon-bedrock/stream.runtime.ts:1104 on main (46e0d90f6717) and :1155 on v2026.11.10:
buildAdditionalModelRequestFields returns adaptive thinking (`thinking: {type: "adaptive", display}` +
output_config) without thinking.block_binding or the thinking-binding-controls-2026-08-01 beta. The direct
Anthropic route adds both through applyAnthropicThinkingBindingControls
(packages/ai/src/providers/anthropic-thinking-replay.ts:6, called from
packages/ai/src/transports/anthropic-messages.ts:632 on main, :630 on v2026.11.10). The Bedrock exclusion
dates from #136782 (30837ae7035b), which shipped the controls for direct API-key requests only. Main has
no Bedrock fix (`git log v2026.11.10..origin/main -- extensions/amazon-bedrock` has none; no block_binding
anywhere under extensions/).

## E2E (isolated built Gateway, one long session, `openclaw agent` per turn)
Harness: proofs/208675/run.sh <tree> <label> au.anthropic.claude-sonnet-5-5 18731, CTX=40000.
Task-owned OPENCLAW_HOME/STATE_DIR/CONFIG_PATH under /tmp, --profile p208675<tree>, dummy
AWS_BEARER_TOKEN_BEDROCK, empty AWS config files, no ~/.aws, no real AWS.
Provider amazon-bedrock (bedrock-converse-stream), baseUrl http://127.0.0.1:18732 = proofs/208675/converse-standin.mjs.
Stand-in rule: every reasoning block it emits is signed over the exact request prefix (system + earlier
messages, cache points and reasoning ignored). A replayed block whose prefix changed -> HTTP 400
ValidationException "The model returned the following errors: messages.N.content.0: Invalid `signature` in
`thinking` block. The block is bound to a different conversation. Remove the block, or set
`thinking.block_binding.prefix_mismatch_behavior` to "drop_block". That setting requires the
`thinking-binding-controls-2026-08-01` value in the `anthropic_beta` field.", unless
additionalModelRequestFields has thinking.block_binding.prefix_mismatch_behavior=drop_block AND
anthropic_beta contains thinking-binding-controls-2026-08-01; then it drops the stale blocks and answers.
Session: turn 1 reads four 16000-char workspace files (read tool), turns 2-4 plain, then the
contextPruning cache-ttl TTL (5s) expires and turn 5 soft-trims turn 1's tool results (the reporter's
`contextPruning: cache-ttl` path: the prefix of every later thinking block changes), turn 6 plain.

base run 1 and run 2 (identical):
  req 1-5 status=200 (turns 1-4) thinking={"type":"adaptive","display":"summarized"} anthropic_beta=null
  req 6 status=400 verdict=rejected mismatched=[messages.3.content.0,messages.5.content.0,messages.7.content.0,messages.9.content.0]
  req 7 status=200 replayedSigned=0   (retry without thinking)
  req 8 status=400 verdict=rejected mismatched=[messages.11.content.0]   (turn 6 rejected again)
  req 9 status=200 replayedSigned=0
  [session-recovery] Anthropic thinking stream error; retrying once without thinking blocks
  [session-recovery] stripped thinking blocks after provider rejected replay: repaired=5
  [session-recovery] Anthropic thinking stream error; retrying once without thinking blocks
  [session-recovery] unable to repair rejected thinking replay: ... Session transcript changed before rewrite publication
  session-recovery lines: 4, rejected requests: 2
  turn 5 reply: replayed signed thinking checked=0 kept=0; turn 6: checked=0 kept=0

fix run 1 and run 2 (identical):
  req 1-7 status=200, every request thinking={"type":"adaptive","display":"summarized","block_binding":{"prefix_mismatch_behavior":"drop_block"}} anthropic_beta=["thinking-binding-controls-2026-08-01"]
  req 6 status=200 verdict=dropped mismatched=[messages.3,5,7,9 .content.0] (server-side drop)
  req 7 status=200 verdict=dropped (same 4 stale blocks, transcript kept them)
  session-recovery lines: 0, rejected requests: 0
  turn 5 reply: checked=5 kept=1 dropped=4; turn 6: checked=6 kept=2 dropped=4 (thinking kept in the transcript)

## Control: Bedrock model without adaptive thinking (au.anthropic.claude-haiku-4-5-20251001-v1:0, budget thinking)
run.sh base budget / run.sh fixcopy budget; compare-bedrock.mjs (normalizes temp path, minute stamps,
checkout path, the stand-in's own signatures):
  req 1-9: identical, statuses identical (200 x5, 400, 200, 400, 200)
  additionalModelRequestFields both sides = {"thinking":{"type":"enabled","budget_tokens":3072,"display":"summarized"},"anthropic_beta":["interleaved-thinking-2025-05-14"]}
  requests=9/9 all-identical=true
  (the stand-in applies its rule to any signed block, so this model is rejected the same way on both builds)

## Control: direct Anthropic provider request (bundled `anthropic`, API key, api.anthropic.com)
run-direct.sh base / fixcopy: api.anthropic.com is a local TLS stand-in (proofs/208675/anthropic-standin.mjs,
throwaway CA in proofs/208675/tls) reached through models.providers.anthropic.request.proxy explicit-proxy;
two turns in one session. compare-direct.mjs (normalizes checkout path, Source line, minute stamps):
  req 1: byte-identical bytes=32899/32899, anthropic-beta equal (…,thinking-binding-controls-2026-08-01), thinking.block_binding drop_block on both
  req 2: byte-identical  req 3: byte-identical bytes=33756/33756  req 4: byte-identical
  requests=4/4 all-identical=true
  Note: base itself is not deterministic run to run (one base run had an extra "## Conversation Context"
  system-prompt section and a system carrier); the base run shown and the fix run match byte for byte.
  Built packages/ai/dist (the Anthropic transports) is identical between the two copies: diff -rq, 155 files.

## Unit (vitest-source)
extensions/amazon-bedrock/stream.runtime.test.ts, same file on both trees:
  base 46e0d90f6717: 8 failed ("Bedrock thinking request composition > sends … policy in the final request" x7,
    "Bedrock Fable contract > sends always-adaptive high effort…"): expected block_binding + anthropic_beta, got neither.
    GovCloud-adaptive and budget rows pass on base (no controls either side).
  fix: 56 passed.
ocx test src/agents/agent-tools.prefix-stability.test.ts extensions/amazon-bedrock/stream.runtime.test.ts
  extensions/amazon-bedrock/stream.runtime.tool-replay.test.ts extensions/amazon-bedrock/index.test.ts
  packages/ai/src/anthropic-binding-transport-parity.test.ts src/plugin-sdk/api-baseline.test.ts: all passed
  (17 + 24 + 16 + 91 tests across shards).

## #198838
Not fixed by this change. Its root is the route gate isDirectAnthropicModel
(packages/ai/src/transports/anthropic-payload-policy.ts:136 on main, :131 on v2026.11.10) requiring
provider id "anthropic", which decides directApiKeyBetaHeader in both Messages adapters
(packages/ai/src/providers/anthropic.ts:221, packages/ai/src/transports/anthropic-transport-stream.ts:255).
The Bedrock route never passes through that gate; it reuses only the shared helper's adaptive policy.
Widening the direct gate to custom provider ids is a separate eligibility change (contributor PR #198842,
closed unmerged) with its own proof.

@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. impact:auth-provider Auth, provider routing, model choice, or SecretRef resolution may break. impact:session-state Session, memory, transcript, context, or agent state can drift or corrupt. labels Nov 30, 2026
@obviyus

obviyus commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator Author

Same session proof on the exact head cf7c776 (built Gateway, local Converse stand-in):

run.sh fix head au.anthropic.claude-sonnet-5-5 18761 (ocx build --dist at cf7c7769c0ae):
  req 1-7 status=200, controls on every request; req 6/7 verdict=dropped (4 stale blocks), session-recovery lines: 0, rejected requests: 0
  turn 5: checked=5 kept=1 dropped=4; turn 6: checked=6 kept=2 dropped=4

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. labels Nov 30, 2026
@obviyus
obviyus merged commit e1945e6 into main Nov 30, 2026
119 checks passed
@clawsweeper clawsweeper Bot added merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. labels Nov 30, 2026
@obviyus
obviyus deleted the bugloop-208675 branch November 30, 2026 02:25
@obviyus

obviyus commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator Author

Landed in e1945e6. Thanks @michaeljgood71 for the report and the rejection counts. On Amazon Bedrock, Claude models with adaptive thinking now send Anthropic's thinking-binding controls (the thinking-binding-controls beta and prefix_mismatch_behavior: "drop_block"), the same as the direct Anthropic provider, so long sessions no longer get their first request of each turn rejected and retried without the earlier reasoning. GovCloud keeps its previous behavior. #198838 (custom Anthropic provider ids) is a separate check and stays open.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 30, 2026
…inking block bound to a different conversation" (openclaw#208704)

Root cause: `extensions/amazon-bedrock/stream.runtime.ts:1104` (`:1155` in 2026.11.10) builds adaptive Claude thinking for Bedrock Converse without `thinking.block_binding` or the `thinking-binding-controls-2026-08-01` beta, which the direct Anthropic route adds through the shared `applyAnthropicThinkingBindingControls`. So any replayed thinking block whose earlier history changed is rejected instead of dropped.

Fixes openclaw#208675. Thanks @michaeljgood71 for the report, the boto3 check and the patch that pointed straight at the fix.

## What Problem This Solves

Fixes: long Bedrock Claude sessions with adaptive thinking get the first request of a turn rejected ("Invalid `signature` in `thinking` block. The block is bound to a different conversation") whenever earlier history changed, for example after cache-TTL pruning, compaction or a model switch. OpenClaw then retries without thinking and drops the session's earlier reasoning.

## User Impact

Bedrock Claude requests with adaptive thinking now carry the same thinking-binding controls as direct Anthropic API-key requests. Bedrock drops only the stale thinking blocks, the turn succeeds on the first request, and the rest of the reasoning stays in the session. Budget-based Bedrock thinking, GovCloud (which already omits `thinking.display` for its older schema), and the direct Anthropic request are unchanged.

## Why This Change Was Made

openclaw#136782 added the controls for direct API-key requests only and left Bedrock on reject-and-retry. The reporter showed Bedrock Converse accepts both controls and drops the stale block. The Bedrock request builder now calls the shared helper on its adaptive branch, so it follows the same rule (adaptive thinking only) and the same beta value, sent as Converse `anthropic_beta`. The helper is re-exported from the existing private-local `provider-stream-shared` SDK seam; no new helper or policy copy. Docs updated where they listed Bedrock as excluded.

Not covered: openclaw#198838 (custom provider ids on api.anthropic.com). Its cause is the separate direct-route gate `isDirectAnthropicModel` requiring provider id `anthropic`, which this Bedrock path never goes through.

## Evidence

Isolated built Gateway, real `openclaw agent` turns in one session, `au.anthropic.claude-sonnet-5-5` on `amazon-bedrock` pointed at a local Converse stand-in. The stand-in signs each thinking block over the exact request prefix and rejects a replayed block whose prefix changed with Bedrock's ValidationException unless both `drop_block` and the beta are present. Turn 1 reads four files, turns 2-4 are plain, then the `contextPruning: cache-ttl` TTL expires and turn 5 trims turn 1's tool results (the reporter's setting), turn 6 is plain. Two runs per side, identical:

- main (8e38da2): turn 5 and turn 6 each rejected once (400), 4 `[session-recovery]` lines (`retrying once without thinking blocks`, `stripped thinking blocks ... repaired=5`, `unable to repair rejected thinking replay`), later requests replay 0 thinking blocks.
- this branch: 0 rejected requests, 0 `[session-recovery]` lines; turn 5 replays 5 blocks (4 stale dropped by the stand-in, 1 kept), turn 6 replays 6.

Controls: a Bedrock model without adaptive thinking (Haiku 4.5, budget thinking) sends identical requests on both builds (9/9 after normalizing temp paths and timestamps). The bundled `anthropic` provider, through a local TLS stand-in for api.anthropic.com, sends byte-identical requests and `anthropic-beta` headers on both builds (4/4); built `packages/ai/dist` is identical. Regression: the Bedrock request-composition table fails on main (8 cases, no `block_binding`/`anthropic_beta`) and passes here; GovCloud adaptive and budget rows assert no controls. `agent-tools.prefix-stability`, the Anthropic binding transport parity test and the Bedrock stream, replay and plugin tests pass.

Observed proof lines: openclaw#208704 (comment)

Merge danger: two-way door. A revert restores the old request shape; nothing is stored or migrated. Blast radius: Bedrock Claude requests with adaptive thinking outside GovCloud. If a Bedrock region rejected the controls, those requests would fail rather than fall back; the reporter's boto3 Converse checks on Bedrock accepted them.

Co-authored-by: Ayaan Zaidi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation extensions: amazon-bedrock extensions: anthropic impact:auth-provider Auth, provider routing, model choice, or SecretRef resolution may break. impact:session-state Session, memory, transcript, context, or agent state can drift or corrupt. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

1 participant