From 5e88745f125c0d332c1d16ea0363860d447657f5 Mon Sep 17 00:00:00 2001 From: Hermes Agent <127238744+teknium1@users.noreply.github.com> Date: Mon, 27 Jul 2026 18:34:40 -0700 Subject: [PATCH] fix: exempt codex_responses from the empty-assistant-content pads MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Commentary-phase Codex turns persist with content:'' by design (their text is delivered via the interim assistant callback), and the Responses wire has no 'assistant must not be empty' validation — padding them broke test_run_conversation_codex_continues_after_commentary_phase_message in CI. Both the builder pad and the send-time pad now skip api_mode=codex_responses. Keying on the ACTIVE api_mode preserves the repair for codex-written sessions replayed through a strict chat-completions provider. --- agent/chat_completion_helpers.py | 13 ++++++++++++- agent/conversation_loop.py | 25 ++++++++++++++++--------- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.py index 4fd7b7b79d4d..61de1cfe4725 100644 --- a/agent/chat_completion_helpers.py +++ b/agent/chat_completion_helpers.py @@ -1392,7 +1392,18 @@ def build_assistant_message(agent, assistant_message, finish_reason: str) -> dic # the same trick the reasoning_content pad uses above (#15250, #17400). # Tool-call turns are exempt: ``content: ""`` alongside ``tool_calls`` # is accepted everywhere and normalizing it would alter cache keys. - if not _san_content and not assistant_tool_calls: + # codex_responses is exempt too: empty assistant content is a designed + # first-class state there (commentary-phase messages persist with + # content:"" while their text is delivered via the interim callback), + # and the Responses wire has no "assistant must not be empty" + # validation. A codex session later replayed through a strict + # chat-completions provider is still repaired by the send-time pad, + # which keys on the ACTIVE api_mode at replay time. + if ( + not _san_content + and not assistant_tool_calls + and getattr(agent, "api_mode", None) != "codex_responses" + ): _san_content = " " msg = { diff --git a/agent/conversation_loop.py b/agent/conversation_loop.py index ba0864f7c49e..0f02173ae2ad 100644 --- a/agent/conversation_loop.py +++ b/agent/conversation_loop.py @@ -1653,15 +1653,22 @@ def run_conversation( # turn), but BEFORE apply_anthropic_cache_control rewrites content # into list blocks. Tool-call turns are exempt: ``content: ''`` # alongside ``tool_calls`` is accepted everywhere and normalizing - # it would alter prompt-cache keys. - for am in api_messages: - if ( - am.get("role") == "assistant" - and not am.get("tool_calls") - and isinstance(am.get("content"), str) - and not am["content"].strip() - ): - am["content"] = " " + # it would alter prompt-cache keys. codex_responses is exempt: + # empty assistant content is a designed first-class state there + # (commentary-phase turns persist with content:'') and the + # Responses wire has no empty-content validation. Keying on the + # ACTIVE api_mode means a codex-written empty turn is still + # repaired the moment the session replays through a strict + # chat-completions provider. + if getattr(agent, "api_mode", None) != "codex_responses": + for am in api_messages: + if ( + am.get("role") == "assistant" + and not am.get("tool_calls") + and isinstance(am.get("content"), str) + and not am["content"].strip() + ): + am["content"] = " " # Apply Anthropic prompt caching for Claude models on native # Anthropic, OpenRouter, and third-party Anthropic-compatible