Skip to content

The agent that grows with you

fix(codex): drop a replayed message id when its reasoning id was stripped

teknium1801590af2d6c
diff --git a/agent/codex_responses_adapter.py b/agent/codex_responses_adapter.py
index 4a0156fcdf2..dec3d7a4453 100644
--- a/agent/codex_responses_adapter.py
+++ b/agent/codex_responses_adapter.py
@@ -398,11 +398,24 @@ def _replay_reasoning_items(
return replayed
+def _turn_has_encrypted_reasoning(msg: Dict[str, Any]) -> bool:
+ """True when the stored assistant turn produced an encrypted ``reasoning`` item alongside its message."""
+ return any(isinstance(ri, dict) and ri.get("encrypted_content") for ri in _as_list(msg.get("codex_reasoning_items")))
+
+
def _replay_message_items(
msg: Dict[str, Any], *, is_github_responses: bool, current_issuer_kind: Optional[str] = None,
) -> List[Dict[str, Any]]:
- """Replay exact assistant message items (id/phase) for prefix-cache hits."""
+ """Replay exact assistant message items (id/phase) for prefix-cache hits.
+
+ A ``msg_*`` id minted in the same response as a ``reasoning`` item is bound to that item's ``rs_*`` id,
+ which ``_replay_reasoning_items`` always strips (store=False). Replaying the message id alone is a
+ deterministic HTTP 400 ("provided without its required 'reasoning' item", #97427/#97442), so the message
+ id is dropped whenever its turn carried encrypted reasoning — replayed, suppressed or foreign-issuer —
+ and the message goes out as content/status/phase only. Reasoning-free turns keep their id.
+ """
replayed: List[Dict[str, Any]] = []
+ linked_to_reasoning = _turn_has_encrypted_reasoning(msg)
for raw_item in _as_list(msg.get("codex_message_items")):
if not (isinstance(raw_item, dict) and raw_item.get("type") == "message" and raw_item.get("role") == "assistant"):
continue
@@ -412,6 +425,8 @@ def _replay_message_items(
if isinstance(part, dict) and str(part.get("type") or "").strip() in _OUTPUT_TEXT_TYPES
]
if content:
+ if linked_to_reasoning and raw_item.get("id"):
+ raw_item = {k: v for k, v in raw_item.items() if k != "id"}
replayed.append(_assistant_message_item(
raw_item, content, is_github_responses=is_github_responses, current_issuer_kind=current_issuer_kind,
))
diff --git a/tests/agent/test_codex_responses_adapter.py b/tests/agent/test_codex_responses_adapter.py
index f4ff99bab01..d0cf7655d06 100644
--- a/tests/agent/test_codex_responses_adapter.py
+++ b/tests/agent/test_codex_responses_adapter.py
@@ -612,6 +612,30 @@ def test_chat_messages_to_responses_input_drops_foreign_id_for_codex_backend():
assert xai_message["id"] == _FOREIGN_ITEM_ID
+def test_message_id_is_dropped_when_its_turn_replays_reasoning_without_id():
+ """#97427/#97442: a ``msg_*`` id bound to a stripped ``rs_*`` id is an orphan the API rejects with 400;
+ the message survives as content/status/phase. A reasoning-free turn keeps its id (prefix-cache affinity)."""
+ def _turn(text, *, reasoning):
+ msg = {
+ "role": "assistant",
+ "content": text,
+ "codex_message_items": [{
+ "type": "message", "role": "assistant", "status": "completed", "id": f"msg_{text}",
+ "phase": "final_answer", "content": [{"type": "output_text", "text": text}],
+ }],
+ }
+ if reasoning:
+ msg["codex_reasoning_items"] = [{"type": "reasoning", "id": "rs_1", "encrypted_content": "BLOB", "summary": []}]
+ return msg
+
+ items = _chat_messages_to_responses_input([_turn("linked", reasoning=True), _turn("alone", reasoning=False)])
+
+ reasoning, linked, alone = (i for i in items if i.get("type") in {"reasoning", "message"})
+ assert "id" not in reasoning and "id" not in linked
+ assert linked["phase"] == "final_answer" and linked["content"] == [{"type": "output_text", "text": "linked"}]
+ assert alone["id"] == "msg_alone"
+
+
def _reasoning_history(item):
return [
{"role": "assistant", "content": "done", "codex_reasoning_items": [item]},