Skip to content

The agent that grows with you

fix(bedrock): Claude never gets a GPT turn's sealed reasoning on the Anthropic Messages path

Victor Kyriazakose34d84897abc
diff --git a/agent/anthropic_message_convert.py b/agent/anthropic_message_convert.py
index 111bd035d96..0372b0581ac 100644
--- a/agent/anthropic_message_convert.py
+++ b/agent/anthropic_message_convert.py
@@ -240,10 +240,16 @@ def _to_plain_data(value: Any, *, _depth: int = 0, _path: Optional[set] = None)
def _extract_preserved_thinking_blocks(message: dict[str, Any]) -> list[dict[str, Any]]:
- """Deep-copied thinking/redacted_thinking blocks from ``reasoning_details``."""
+ """Deep-copied thinking/redacted_thinking blocks from ``reasoning_details``; none from a turn a non-Claude
+ Bedrock Converse model stamped (a GPT -> Claude switch or fallback): Claude rejects another family's
+ sealed blobs (``Invalid `data` in `redacted_thinking` block``), and no Anthropic route signed them."""
raw_details = message.get("reasoning_details")
if not isinstance(raw_details, list):
return []
+ from agent.bedrock_adapter import reasoning_minted_outside_claude
+
+ if reasoning_minted_outside_claude(message):
+ return []
return [
copy.deepcopy(d)
for d in raw_details
diff --git a/agent/bedrock_adapter.py b/agent/bedrock_adapter.py
index 69ce78feb31..f02350c7c1a 100644
--- a/agent/bedrock_adapter.py
+++ b/agent/bedrock_adapter.py
@@ -904,6 +904,17 @@ def _converse_replay_policy(model: Optional[str]) -> str:
return "strip" if any(p in lowered for p in _CONVERSE_REPLAY_STRIP_PATTERNS) else "unsigned"
+def _minted_outside_claude(issuer: Optional[str]) -> bool:
+ return issuer is not None and _converse_replay_policy(issuer) != "signed"
+
+
+def reasoning_minted_outside_claude(msg: dict) -> bool:
+ """True when a non-Claude Converse model stamped this turn's reasoning. Claude gets none of it on either
+ Bedrock wire (Converse here, AnthropicBedrock in anthropic_message_convert): it cannot open another
+ family's sealed blobs. Unstamped turns (Claude's own AnthropicBedrock turns, older sessions) are False."""
+ return _minted_outside_claude(_issuer(msg))
+
+
def _unsigned_reasoning(block: dict) -> Optional[dict]:
reasoning = block.get("reasoningContent")
if not isinstance(reasoning, dict):
@@ -943,7 +954,7 @@ def _reasoning_filter(policy: str, issuer: Optional[str], own: bool, *, in_loop:
if own:
return _verbatim if in_loop or policy in ("unsigned", "issuer") else prior
if issuer is not None:
- return _without_reasoning if policy == "signed" and _converse_replay_policy(issuer) != "signed" else prior
+ return _without_reasoning if policy == "signed" and _minted_outside_claude(issuer) else prior
return loop if in_loop else prior
diff --git a/agent/error_classifier.py b/agent/error_classifier.py
index 8e39cdb7f6b..e9489f492c3 100644
--- a/agent/error_classifier.py
+++ b/agent/error_classifier.py
@@ -342,6 +342,15 @@ _PARAM_REJECTION_WORDS = ("not supported", "unsupported", "unknown", "unrecogniz
# Anthropic thinking-block 400 wordings (see _provider_special_cases).
_THINKING_MUTATION_WORDS = ("signature", "cannot be modified", "must remain as they were")
+
+def _is_anthropic_thinking_rejection(msg: str) -> bool:
+ """A thinking block the upstream will not verify: a signature broken by transcript mutation, or a
+ ``redacted_thinking`` blob it cannot open (another model's sealed reasoning after a /model switch or
+ fallback: ``Invalid `data` in `redacted_thinking` block``). Both heal by stripping the replayed blocks."""
+ return "thinking" in msg and (
+ any(p in msg for p in _THINKING_MUTATION_WORDS) or ("redacted_thinking" in msg and "invalid" in msg))
+
+
# Local MoA streaming adapter-shape bugs (see _moa_special_cases).
_MOA_ADAPTER_SHAPE_BUGS = (
"'types.SimpleNamespace' object is not iterable", "'types.SimpleNamespace' object has no attribute 'index'",
@@ -895,9 +904,9 @@ def _provider_special_cases(c: _Ctx) -> Optional[Verdict]:
# strips Anthropic thinking blocks and resends the same encrypted item forever.
if status == 400 and (c.code == "thinking_signature_invalid" or "thinking_signature_invalid" in msg):
return _V_INVALID_ENCRYPTED
- # Anthropic thinking-block 400s (signature mismatch after transcript
- # mutation). Not gated on provider — OpenRouter proxies Anthropic errors.
- if status == 400 and "thinking" in msg and any(p in msg for p in _THINKING_MUTATION_WORDS):
+ # Anthropic thinking-block 400s (signature mismatch after transcript mutation, or a sealed blob
+ # the upstream cannot open). Not gated on provider — OpenRouter proxies Anthropic errors.
+ if status == 400 and _is_anthropic_thinking_rejection(msg):
return _v(_R.thinking_signature)
# Anthropic long-context tier gate (429 "extra usage" + "long context").
if status == 429 and "extra usage" in msg and "long context" in msg:
diff --git a/tests/agent/test_bedrock_anthropic_foreign_reasoning.py b/tests/agent/test_bedrock_anthropic_foreign_reasoning.py
new file mode 100644
index 00000000000..ac9d00d9338
--- /dev/null
+++ b/tests/agent/test_bedrock_anthropic_foreign_reasoning.py
@@ -0,0 +1,268 @@
+"""Bedrock GPT -> Claude: another model's sealed reasoning never reaches Claude on the Anthropic Messages path.
+
+A Bedrock session on a reasoning GPT (Converse) stores GPT's sealed reasoning as ``redacted_thinking`` in
+``reasoning_details`` and stamps the turn with the issuing model (``CONVERSE_ISSUER_TYPE``). #142139 made
+Converse drop a non-Claude turn's reasoning when Claude answers. Claude on Bedrock runs on the
+AnthropicBedrock (``anthropic_messages``) path instead, which replayed the blob; Claude rejects it with
+``Invalid `data` in `redacted_thinking` block``, and that 400 used to classify as ``format_error`` and fail
+over to GPT on every turn.
+
+Two layers are pinned here: the converter drops thinking minted by a non-Claude model (stamped turns), and
+the classifier routes the rejection to the existing ``thinking_signature`` recovery (the safety net for
+unstamped turns from older sessions), so the turn stays on Claude.
+"""
+
+from __future__ import annotations
+
+import json
+import logging
+from types import SimpleNamespace
+
+import httpx
+import pytest
+
+from agent.anthropic_message_convert import convert_messages_to_anthropic
+from agent.bedrock_adapter import CONVERSE_ISSUER_TYPE
+from agent.error_classifier import FailoverReason, classify_api_error
+
+CLAUDE = "global.anthropic.claude-opus-5-5"
+GPT = "us.openai.gpt-6.1-sol"
+BEDROCK_URL = "https://bedrock-runtime.us-east-1.amazonaws.com"
+GPT_BLOB = "R1BULXNlYWxlZC1yZWFzb25pbmctYmxvYg==" # base64, as _persisted_reasoning_details stores it
+REJECTION = "messages.3.content.10: Invalid `data` in `redacted_thinking` block"
+
+
+def _gpt_turn(answer: str, *, stamped: bool = True, tool_call: dict | None = None) -> dict:
+ details = [
+ {"type": "thinking", "thinking": "gpt readable summary"},
+ {"type": "redacted_thinking", "data": GPT_BLOB},
+ ]
+ if stamped:
+ details.append({"type": CONVERSE_ISSUER_TYPE, "model": GPT})
+ turn = {"role": "assistant", "content": answer, "reasoning_details": details}
+ if tool_call:
+ turn["tool_calls"] = [tool_call]
+ return turn
+
+
+def _claude_turn(answer: str, sig: str, *, issuer: str | None = None) -> dict:
+ details = [{"type": "thinking", "thinking": "claude chain", "signature": sig}]
+ if issuer:
+ details.append({"type": CONVERSE_ISSUER_TYPE, "model": issuer})
+ return {"role": "assistant", "content": answer, "reasoning_details": details}
+
+
+def _wire_thinking(wire: list[dict]) -> list[dict]:
+ return [b for m in wire if m["role"] == "assistant" and isinstance(m["content"], list)
+ for b in m["content"] if b.get("type") in ("thinking", "redacted_thinking")]
+
+
+def _convert(history: list[dict], model: str = CLAUDE, base_url: str = BEDROCK_URL) -> list[dict]:
+ _, wire = convert_messages_to_anthropic(history, base_url=base_url, model=model)
+ return wire
+
+
+# --- Root: the converter never replays a non-Claude model's reasoning ---------------------------------------
+
+
[email protected]("base_url", [BEDROCK_URL, None], ids=["bedrock", "anthropic-native"])
+def test_gpt_stamped_reasoning_never_reaches_claude_and_claude_keeps_its_own(base_url):
+ history = [
+ {"role": "user", "content": "q1"}, _gpt_turn("a1"),
+ {"role": "user", "content": "q2"}, _claude_turn("a2", "claude-sig-2"),
+ {"role": "user", "content": "q3"},
+ ]
+ wire = _convert(history, base_url=base_url)
+
+ assert GPT_BLOB not in json.dumps(wire)
+ assert "gpt readable summary" not in json.dumps(wire)
+ assert {"type": "thinking", "thinking": "claude chain", "signature": "claude-sig-2"} in _wire_thinking(wire)
+ assert wire[1]["content"] == [{"type": "text", "text": "a1"}]
+
+
+def test_fallback_mid_tool_loop_drops_gpt_reasoning_from_the_in_flight_loop():
+ call = {"id": "call_1", "type": "function", "function": {"name": "terminal", "arguments": "{}"}}
+ history = [
+ {"role": "user", "content": "run it"}, _gpt_turn("", tool_call=call),
+ {"role": "tool", "tool_call_id": "call_1", "content": "ok"},
+ ]
+ wire = _convert(history)
+
+ assert GPT_BLOB not in json.dumps(wire)
+ assert _wire_thinking(wire) == []
+ assert [b["type"] for b in wire[1]["content"]] == ["tool_use"]
+
+
+def test_claude_stamped_converse_turn_keeps_its_signed_thinking_and_redacted_blob():
+ """A Claude turn captured on Converse (sticky stream-denied fallback) is still Claude's: it replays."""
+ turn = _claude_turn("a1", "claude-sig-1", issuer="us.anthropic.claude-sonnet-4-6")
+ turn["reasoning_details"].insert(1, {"type": "redacted_thinking", "data": "Q2xhdWRlLXJlZGFjdGVk"})
+ wire = _convert([{"role": "user", "content": "q1"}, turn, {"role": "user", "content": "q2"}])
+
+ assert _wire_thinking(wire) == [
+ {"type": "thinking", "thinking": "claude chain", "signature": "claude-sig-1"},
+ {"type": "redacted_thinking", "data": "Q2xhdWRlLXJlZGFjdGVk"},
+ ]
+
+
+def test_unstamped_redacted_thinking_still_replays():
+ """Unstamped turns keep replaying: Claude's own AnthropicBedrock turns carry no issuer record and Claude
+ mints redacted_thinking itself, so "unstamped" cannot mean "foreign". A pre-stamp GPT blob is healed by
+ the one-shot thinking_signature recovery instead (see the conversation-loop test below)."""
+ wire = _convert([{"role": "user", "content": "q1"}, _gpt_turn("a1", stamped=False), {"role": "user", "content": "q2"}])
+
+ assert {"type": "redacted_thinking", "data": GPT_BLOB} in _wire_thinking(wire)
+
+
+# --- Safety net: the rejection reaches the thinking_signature recovery ---------------------------------------
+
+
+def _anthropic_400(message: str, body: dict | None = None):
+ import anthropic
+
+ body = body if body is not None else {"message": message}
+ response = httpx.Response(400, request=httpx.Request("POST", f"{BEDROCK_URL}/model/{CLAUDE}/invoke"))
+ return anthropic.BadRequestError(f"Error code: 400 - {body}", response=response, body=body)
+
+
+ None, # Bedrock's flat {"message": ...}
+ {"type": "error", "error": {"type": "invalid_request_error", "message": REJECTION}}, # direct API
+], ids=["bedrock-body", "anthropic-body"])
+def test_invalid_redacted_thinking_data_classifies_as_thinking_signature(body):
+ result = classify_api_error(_anthropic_400(REJECTION, body), provider="bedrock", model=CLAUDE)
+
+ assert result.reason == FailoverReason.thinking_signature
+ assert result.retryable is True
+
+
[email protected](("message", "expected"), [
+ # Existing Anthropic signature wording keeps its class.
+ ("messages.1.content.0: Invalid `signature` in `thinking` block", FailoverReason.thinking_signature),
+ # OpenAI Responses stale encrypted reasoning: still beats the Anthropic rule (#70595).
+ ("thinking_signature_invalid: redacted_thinking invalid", FailoverReason.invalid_encrypted_content),
+ # Other invalid-block 400s stay format errors.
+ ("messages.1.content.0: Invalid `data` in `image` block", FailoverReason.format_error),
+])
+def test_neighbouring_400s_keep_their_class(message, expected):
+ assert classify_api_error(_anthropic_400(message), provider="bedrock", model=CLAUDE).reason == expected
+
+
+# --- Composed: real AIAgent turn loop, AnthropicBedrock client faked -----------------------------------------
+
+
+class _FakeMessages:
+ """Claude on Bedrock: rejects any request replaying GPT's sealed blob, else answers with signed thinking."""
+
+ def __init__(self, log):
+ self.log = log
+ self.always_reject = False
+
+ def create(self, **kwargs):
+ sent_blob = GPT_BLOB in json.dumps(kwargs.get("messages"), default=str)
+ signatures = [b.get("signature") for b in _wire_thinking(kwargs.get("messages") or []) if b.get("signature")]
+ self.log.append({"model": kwargs.get("model"), "sent_gpt_blob": sent_blob, "claude_signatures": signatures})
+ if sent_blob or self.always_reject:
+ raise _anthropic_400(REJECTION)
+ n = len(self.log)
+ return SimpleNamespace(
+ content=[SimpleNamespace(type="thinking", thinking=f"claude chain {n}", signature=f"claude-sig-{n}"),
+ SimpleNamespace(type="text", text=f"claude answer {n}")],
+ stop_reason="end_turn", model=CLAUDE,
+ usage=SimpleNamespace(input_tokens=10, output_tokens=5),
+ )
+
+
+class _FakeConverse:
+ """Converse client: a GPT answer here means the turn failed over (setup probes also land here)."""
+
+ def __init__(self, log):
+ self.log = log
+
+ def converse(self, **kwargs):
+ self.log.append(kwargs.get("modelId"))
+ return {"output": {"message": {"role": "assistant", "content": [{"text": "gpt answer"}]}},
+ "stopReason": "end_turn", "usage": {"inputTokens": 1, "outputTokens": 1, "totalTokens": 2}}
+
+ def converse_stream(self, **kwargs):
+ raise AssertionError("streaming is disabled in this test")
+
+
+def bedrock_claude_agent(monkeypatch):
+ import run_agent
+
+ monkeypatch.setenv("AWS_ACCESS_KEY_ID", "AKIATESTTESTTESTTEST")
+ monkeypatch.setenv("AWS_SECRET_ACCESS_KEY", "test-secret")
+ monkeypatch.setenv("AWS_REGION", "us-east-1")
+ monkeypatch.setattr("hermes_cli.plugins.discover_plugins", lambda: None)
+ monkeypatch.setattr("model_tools.get_tool_definitions", lambda **kwargs: [{
+ "type": "function",
+ "function": {"name": "t", "description": "t", "parameters": {"type": "object", "properties": {}}},
+ }])
+ monkeypatch.setattr("model_tools.check_toolset_requirements", dict)
+ monkeypatch.setattr("time.sleep", lambda *_: None)
+ log: list[dict] = []
+ converse_models: list[str] = []
+ client = SimpleNamespace(messages=_FakeMessages(log), close=lambda: None)
+ monkeypatch.setattr("agent.anthropic_adapter.build_anthropic_bedrock_client", lambda *a, **k: client)
+ monkeypatch.setattr("agent.bedrock_adapter._get_bedrock_runtime_client",
+ lambda *a, **k: _FakeConverse(converse_models))
+
+ agent = run_agent.AIAgent(
+ model=CLAUDE, provider="bedrock", base_url=BEDROCK_URL, api_mode="anthropic_messages",
+ fallback_model=[{"provider": "bedrock", "model": GPT}],
+ quiet_mode=True, skip_context_files=True, skip_memory=True, max_iterations=4,
+ )
+ agent._disable_streaming = True
+ agent._persist_session = lambda *a, **k: None
+ agent._save_trajectory = lambda *a, **k: None
+ return agent, log, converse_models
+
+
+def _gpt_history(*, stamped: bool) -> list[dict]:
+ return [
+ {"role": "user", "content": "q1"}, _gpt_turn("a1", stamped=stamped),
+ {"role": "user", "content": "q2"}, _gpt_turn("a2", stamped=stamped),
+ ]
+
+
+def test_switch_to_claude_never_sends_stamped_gpt_reasoning(bedrock_claude_agent, caplog):
+ agent, log, converse_models = bedrock_claude_agent
+ with caplog.at_level(logging.INFO):
+ result = agent.run_conversation("q3", conversation_history=_gpt_history(stamped=True))
+
+ assert result["final_response"] == "claude answer 1"
+ assert log == [{"model": CLAUDE, "sent_gpt_blob": False, "claude_signatures": []}]
+ assert GPT not in converse_models
+ assert "Fallback activated" not in caplog.text
+
+
+def test_rejected_unstamped_gpt_blob_is_stripped_and_the_turn_stays_on_claude(bedrock_claude_agent, caplog):
+ agent, log, converse_models = bedrock_claude_agent
+ with caplog.at_level(logging.INFO):
+ first = agent.run_conversation("q3", conversation_history=_gpt_history(stamped=False))
+ second = agent.run_conversation("q4", conversation_history=first["messages"])
+
+ assert "Fallback activated" not in caplog.text
+ assert GPT not in converse_models
+ assert {entry["model"] for entry in log} == {CLAUDE}
+ # One rejection, one stripped retry on Claude; the next turn keeps the blob out and replays
+ # Claude's own signed thinking from the turn that succeeded.
+ assert [entry["sent_gpt_blob"] for entry in log] == [True, False, False]
+ assert first["final_response"] == "claude answer 2"
+ assert second["final_response"] == "claude answer 3"
+ assert "claude-sig-2" in log[2]["claude_signatures"]
+
+
+def test_a_rejection_the_strip_cannot_heal_stays_bounded(bedrock_claude_agent, caplog):
+ """The strip runs once per turn (thinking_sig_retry_attempted); a repeat rejection takes the
+ ordinary retryable budget and then the fallback chain, never an endless strip-and-resend."""
+ agent, log, converse_models = bedrock_claude_agent
+ agent._anthropic_client.messages.always_reject = True
+ with caplog.at_level(logging.INFO):
+ result = agent.run_conversation("q3", conversation_history=_gpt_history(stamped=False))
+
+ assert caplog.text.count("Thinking block signature recovery") == 1
+ assert 2 <= len(log) <= 1 + agent._api_max_retries
+ assert GPT in converse_models and result["final_response"] == "gpt answer"