Skip to content

The agent that grows with you

fix(codex): replay only the newest sealed reasoning item on Azure Foundry

Erosika2de15babf946
diff --git a/agent/transports/codex.py b/agent/transports/codex.py
index 1c4a87dbfad..b472d1c6708 100644
--- a/agent/transports/codex.py
+++ b/agent/transports/codex.py
@@ -405,6 +405,28 @@ def _is_post_tool_replay(messages: Optional[list[dict[str, Any]]]) -> bool:
return False
+def _newest_reasoning_only(messages: list[dict[str, Any]]) -> list[dict[str, Any]]:
+ """Copy of ``messages`` keeping ``codex_reasoning_items`` only on the newest assistant row that has any.
+ Foundry rejects a request that replays encrypted reasoning from more than one prior response (HTTP 400
+ "Conflicting authenticated continuation identities", #105369). ``compaction`` checkpoints stay everywhere."""
+ out: list[dict[str, Any]] = []
+ newest_kept = False
+ for msg in reversed(messages):
+ items = msg.get("codex_reasoning_items") if isinstance(msg, dict) and msg.get("role") == "assistant" else None
+ if isinstance(items, list) and any(isinstance(i, dict) and i.get("type") != "compaction" for i in items):
+ if newest_kept:
+ checkpoints = [i for i in items if isinstance(i, dict) and i.get("type") == "compaction"]
+ msg = dict(msg)
+ if checkpoints:
+ msg["codex_reasoning_items"] = checkpoints
+ else:
+ msg.pop("codex_reasoning_items")
+ newest_kept = True
+ out.append(msg)
+ out.reverse()
+ return out
+
+
def _native_compaction_active(context_management: Any) -> bool:
"""True only when the caller's eligibility gate produced a non-empty payload.
@@ -532,10 +554,13 @@ class ResponsesApiTransport(ProviderTransport):
is_github_responses = params.get("is_github_responses") is True
is_codex_backend = params.get("is_codex_backend") is True
is_xai_responses = params.get("is_xai_responses") is True
+ is_azure_foundry = _is_azure_foundry_responses(params)
# Foundry 400s on encrypted-reasoning replay only in the post-tool follow-up turn.
replay_encrypted_reasoning = bool(params.get("replay_encrypted_reasoning", True)) and not (
- _is_azure_foundry_responses(params) and _is_post_tool_replay(payload_messages)
+ is_azure_foundry and _is_post_tool_replay(payload_messages)
)
+ if is_azure_foundry and replay_encrypted_reasoning:
+ payload_messages = _newest_reasoning_only(payload_messages)
# One predicate decides whether context_management goes out AND whether the converter may replay a checkpoint.
context_management = params.get("context_management")
native_compaction_active = _native_compaction_active(context_management)
diff --git a/tests/agent/transports/test_codex_transport.py b/tests/agent/transports/test_codex_transport.py
index 4da859ccfad..4517132ef90 100644
--- a/tests/agent/transports/test_codex_transport.py
+++ b/tests/agent/transports/test_codex_transport.py
@@ -383,6 +383,75 @@ class TestCodexBuildKwargs:
assert "function_call_output" in item_types
assert kw.get("include") == ["reasoning.encrypted_content"]
+ @classmethod
+ def _two_turn_messages(cls):
+ """Two answered turns, each assistant row sealed by its own response, then a fresh user message."""
+ return [
+ {"role": "user", "content": "first"},
+ {
+ "role": "assistant",
+ "content": "one",
+ "codex_reasoning_items": [{"type": "reasoning", "encrypted_content": "sealed-1", "summary": []}],
+ },
+ {"role": "user", "content": "second"},
+ {
+ "role": "assistant",
+ "content": "two",
+ "codex_reasoning_items": [{"type": "reasoning", "encrypted_content": "sealed-2", "summary": []}],
+ },
+ {"role": "user", "content": "third"},
+ ]
+
+ def test_azure_foundry_new_turn_replays_only_newest_reasoning(self, transport):
+ """Two sealed prior responses on the wire is the 400 "Conflicting authenticated
+ continuation identities" shape (#105369); one is accepted."""
+ kw = transport.build_kwargs(
+ model="gpt-6-astra",
+ messages=self._two_turn_messages(),
+ tools=[],
+ provider="azure-foundry",
+ base_url="https://placeholder.openai.azure.com/openai/v1",
+ replay_encrypted_reasoning=True,
+ )
+ reasoning = [item for item in kw["input"] if item.get("type") == "reasoning"]
+ assert [item["encrypted_content"] for item in reasoning] == ["sealed-2"]
+ assert kw.get("include") == ["reasoning.encrypted_content"]
+ assistant_text = [item for item in kw["input"] if item.get("role") == "assistant"]
+ assert len(assistant_text) == 2
+
+ def test_default_responses_new_turn_replays_all_reasoning(self, transport):
+ """Non-Azure Responses endpoints keep cross-turn reasoning replay."""
+ kw = transport.build_kwargs(
+ model="gpt-5.4", messages=self._two_turn_messages(), tools=[], replay_encrypted_reasoning=True,
+ )
+ reasoning = [item for item in kw["input"] if item.get("type") == "reasoning"]
+ assert [item["encrypted_content"] for item in reasoning] == ["sealed-1", "sealed-2"]
+
+ def test_azure_foundry_newest_reasoning_pruning_leaves_canonical_messages_untouched(self, transport):
+ messages = self._two_turn_messages()
+ transport.build_kwargs(
+ model="gpt-6-astra", messages=messages, tools=[], provider="azure-foundry",
+ base_url="https://placeholder.openai.azure.com/openai/v1", replay_encrypted_reasoning=True,
+ )
+ assert messages[1]["codex_reasoning_items"][0]["encrypted_content"] == "sealed-1"
+ assert messages[3]["codex_reasoning_items"][0]["encrypted_content"] == "sealed-2"
+
+ def test_newest_reasoning_only_keeps_compaction_checkpoints(self):
+ from agent.transports.codex import _newest_reasoning_only
+
+ checkpoint = {"type": "compaction", "encrypted_content": "ckpt", "summary": []}
+ messages = [
+ {"role": "user", "content": "first"},
+ {"role": "assistant", "content": "one", "codex_reasoning_items": [checkpoint, self._reasoning_item()]},
+ {"role": "user", "content": "second"},
+ {"role": "assistant", "content": "two", "codex_reasoning_items": [self._reasoning_item()]},
+ {"role": "user", "content": "third"},
+ ]
+ pruned = _newest_reasoning_only(messages)
+ assert pruned[1]["codex_reasoning_items"] == [checkpoint]
+ assert pruned[3]["codex_reasoning_items"] == [self._reasoning_item()]
+ assert len(messages[1]["codex_reasoning_items"]) == 2
+
def test_azure_foundry_post_tool_replay_suppresses_reasoning_items(self, transport):
"""The rejected payload shape drops reasoning, keeps tool continuity."""
kw = transport.build_kwargs(