Skip to content

The agent that grows with you

fix(bedrock): never replay another model's sealed reasoning on Converse

Victor Kyriazakos3aaabae4b01f
diff --git a/agent/bedrock_adapter.py b/agent/bedrock_adapter.py
index 44c118755e2..f4142cb48d1 100644
--- a/agent/bedrock_adapter.py
+++ b/agent/bedrock_adapter.py
@@ -869,18 +869,21 @@ def _assistant_blocks(msg: dict, content, claude: bool = True, own: bool = False
# redacted blobs may be another model's (a /model switch from Claude) and non-Claude models
# reject them ("This model doesn't support the reasoningContent.reasoningText.signature field");
# the in-flight loop was produced by the model now answering and replays as captured.
+# A turn stamped by another model (CONVERSE_ISSUER_TYPE) never gets the in-flight verbatim replay: a fallback or
+# switch mid-loop hands the loop to a model that cannot open the previous one's sealed blobs (_reasoning_filter).
_CONVERSE_REPLAY_STRIP_PATTERNS = ("deepseek.r1", "deepseek-r1", "moonshotai.kimi-k3")
# reasoning_details record naming the model that minted a turn's Converse reasoning: its signatures are that
# model's own, so it gets them back unmodified on every later turn and after a reload (AWS: "include the text and
-# its signature unmodified"); any other model still gets the per-policy prior-turn treatment.
+# its signature unmodified"); any other model gets the per-policy prior-turn treatment, and Claude gets no
+# reasoning at all from a turn another family minted.
CONVERSE_ISSUER_TYPE = "bedrock.native_assistant"
-def _issued_by(msg: dict, model: Optional[str]) -> bool:
- return model is not None and any(isinstance(d, dict) and d.get("type") == CONVERSE_ISSUER_TYPE
- and d.get("model") == model for d in msg.get("reasoning_details") or [])
+def _issuer(msg: dict) -> Optional[str]:
+ return next((d["model"] for d in msg.get("reasoning_details") or []
+ if isinstance(d, dict) and d.get("type") == CONVERSE_ISSUER_TYPE and isinstance(d.get("model"), str)), None)
def _converse_replay_policy(model: Optional[str]) -> str:
@@ -921,6 +924,18 @@ def _filter_reasoning(blocks: list[dict], keep) -> list[dict]:
return [kept for kept in map(keep, blocks) if kept is not None]
+def _reasoning_filter(policy: str, issuer: Optional[str], own: bool, *, in_loop: bool):
+ """Which reasoning one assistant turn may replay to the model now answering. A turn stamped by ANOTHER
+ model (fallback or ``/model`` switch, including mid tool loop) loses the in-flight loop's verbatim replay:
+ sealed ``redactedContent`` opens only for its issuer, and Claude rejects another family's blobs outright
+ (``Invalid `data` in `redacted_thinking` block``), so a non-Claude turn replays no reasoning to Claude.
+ Unstamped turns (older sessions) keep the policy, and the rejection-driven resend covers them."""
+ prior, loop = _REASONING_FILTERS[policy]
+ if issuer is not None and not own:
+ return _without_reasoning if policy == "signed" and _converse_replay_policy(issuer) != "signed" else prior
+ return loop if in_loop or (own and policy == "unsigned") else prior
+
+
def convert_messages_to_converse(
messages: list[dict], model: Optional[str] = None,
) -> tuple[Optional[list[dict]], list[dict]]:
@@ -929,7 +944,6 @@ def convert_messages_to_converse(
same-role neighbours merge, placeholder user turns pad the ends. ``model`` selects the reasoning
replay policy for turns before the in-flight tool loop (None replays every carrier verbatim)."""
policy = _converse_replay_policy(model)
- prior_filter, loop_filter = _REASONING_FILTERS[policy]
# Tool results are role "tool" here, so the last role "user" message opens the in-flight tool loop.
loop_start = next((i for i in range(len(messages) - 1, -1, -1) if messages[i].get("role") == "user"), -1)
system_blocks: list[dict] = []
@@ -951,8 +965,9 @@ def convert_messages_to_converse(
append_turn("user", [{"toolResult": {
"toolUseId": msg.get("tool_call_id", ""), "content": [{"text": _safe_text(result_content)}]}}])
elif role == "assistant":
- own = _issued_by(msg, model)
- keep = loop_filter if idx >= loop_start or (own and policy == "unsigned") else prior_filter
+ issuer = _issuer(msg) if model is not None else None
+ own = issuer is not None and issuer == model
+ keep = _reasoning_filter(policy, issuer, own, in_loop=idx >= loop_start)
blocks = _filter_reasoning(_assistant_blocks(msg, content, claude=policy == "signed", own=own), keep)
append_turn("assistant", blocks or [dict(_PLACEHOLDER_BLOCK)])
elif role == "user":
diff --git a/tests/agent/test_bedrock_converse_reasoning_replay.py b/tests/agent/test_bedrock_converse_reasoning_replay.py
index 40d8173442e..f67630ad51d 100644
--- a/tests/agent/test_bedrock_converse_reasoning_replay.py
+++ b/tests/agent/test_bedrock_converse_reasoning_replay.py
@@ -128,3 +128,68 @@ def test_converse_reasoning_survives_a_reload_from_state_db(model, expect):
_, msgs = convert_messages_to_converse(history, model=model)
reasoning = [b["reasoningContent"]["reasoningText"] for b in msgs[1]["content"] if "reasoningContent" in b]
assert reasoning == [expect]
+
+
+GPT = "global.openai.gpt-6.1-sol"
+OPUS = "global.anthropic.claude-opus-5-5"
+
+
+def _bedrock_turn(model, tag, tool_id, *, reloaded):
+ """One tool turn as the agent stores it: produced by ``model`` through the real transport (which stamps it)."""
+ transport = BedrockTransport()
+ transport.build_kwargs(model=model, messages=[{"role": "user", "content": "x"}])
+ normalized = transport.normalize_response({"output": {"message": {"role": "assistant", "content": [
+ {"reasoningContent": {"reasoningText": {"text": f"think {tag}"}}},
+ {"reasoningContent": {"redactedContent": f"rsn_{tag}".encode()}},
+ {"toolUse": {"toolUseId": tool_id, "name": "read_file", "input": {"path": "a"}}},
+ ]}}, "stopReason": "tool_use", "usage": {}})
+ data = dict(normalized.provider_data)
+ if reloaded: # bedrock_content_blocks is live-only; a state.db reload keeps reasoning_details
+ data.pop("bedrock_content_blocks")
+ call = {"id": tool_id, "type": "function", "function": {"name": "read_file", "arguments": '{"path": "a"}'}}
+ return {"role": "assistant", "content": "", "tool_calls": [call], "reasoning_content": f"think {tag}", **data}
+
+
+def _session(issuer, *, reloaded):
+ """A prior tool turn, a final answer, then an in-flight tool loop: every assistant turn minted by ``issuer``."""
+ return [
+ {"role": "user", "content": "Q1"}, _bedrock_turn(issuer, "prior", "call_1", reloaded=reloaded),
+ {"role": "tool", "tool_call_id": "call_1", "content": "ok"}, {"role": "assistant", "content": "A1"},
+ {"role": "user", "content": "Q2"}, _bedrock_turn(issuer, "loop", "call_2", reloaded=reloaded),
+ {"role": "tool", "tool_call_id": "call_2", "content": "ok"},
+ ]
+
+
+def _replayed_reasoning(kwargs):
+ return [[next(iter(b["reasoningContent"])) for b in m["content"] if "reasoningContent" in b]
+ for m in kwargs["messages"] if m["role"] == "assistant" and any("toolUse" in b for b in m["content"])]
+
+
[email protected]("reloaded", [False, True])
[email protected]("issuer, target, expect", [
+ # The issuing model gets its own sealed blobs back, prior turn and in-flight loop alike.
+ (GPT, GPT, [["reasoningText", "redactedContent"]] * 2),
+ # Fallback or switch to Claude, including mid-loop: Claude cannot read another family's reasoning at all.
+ (GPT, OPUS, [[], []]),
+ # Another GPT cannot open the blob either (it hangs on it): readable text only, in the loop too.
+ (GPT, "global.openai.gpt-6-sol", [["reasoningText"]] * 2),
+])
+def test_reasoning_another_model_minted_never_replays_sealed(issuer, target, expect, reloaded):
+ """Invariant: a sealed redactedContent blob reaches only the model that minted it. Coatue field report:
+ a GPT-6.1 session fell back to Opus 5.5 after two read timeouts and every call 400'd on
+ ``Invalid `data` in `redacted_thinking` block`` because GPT's blobs were replayed verbatim."""
+ history = _session(issuer, reloaded=reloaded)
+ before = copy.deepcopy(history)
+ kwargs = BedrockTransport().build_kwargs(model=target, messages=history)
+ assert _replayed_reasoning(kwargs) == expect
+ toolus = [b["toolUse"]["toolUseId"] for m in kwargs["messages"] for b in m["content"] if "toolUse" in b]
+ assert toolus == ["call_1", "call_2"]
+ assert history == before
+
+
+def test_claude_minted_reasoning_still_replays_to_claude():
+ """Control: the gate is per issuer family, so Claude-to-Claude replay (signed thinking and redacted
+ blobs) stays verbatim, as Bedrock requires for Claude."""
+ history = _session("global.anthropic.claude-sonnet-5-5", reloaded=False)
+ kwargs = BedrockTransport().build_kwargs(model=OPUS, messages=history)
+ assert _replayed_reasoning(kwargs) == [["reasoningText", "redactedContent"]] * 2