The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(codex): run transform_llm_output on codex app-server replies
Teknium53b086aee8dd
diff --git a/agent/codex_runtime.py b/agent/codex_runtime.pyindex 11ad5243c45..04e8e8b6a7e 100644--- a/agent/codex_runtime.py+++ b/agent/codex_runtime.py@@ -722,8 +722,24 @@ def _finish_codex_turn(agent, turn, messages: list[dict[str, Any]], *, original_return usage_result+def _transform_codex_reply(agent, turn, turn_id: Any) -> dict[str, Any]:+ """Run ``transform_llm_output`` on a finished codex reply before its rows are persisted, as+ ``finish_text_response`` does on the generic loop: the user and the stored row get the rewrite."""+ if turn.interrupted or turn.error is not None or not turn.final_text:+ return {"final_response": turn.final_text}+ from agent.turn_finalizer import apply_llm_output_transform+ final, transformed, pre_transform = apply_llm_output_transform(agent, turn.final_text, turn_id=turn_id)+ if transformed:+ for message in reversed(turn.projected_messages or []):+ if message.get("role") == "assistant" and message.get("content") == pre_transform:+ message["content"] = final+ break+ return {"final_response": final, "response_transformed": transformed, "pre_transform_response": pre_transform}++def run_codex_app_server_turn(agent, *, user_message: str, original_user_message: Any, messages: list[dict[str, Any]],- effective_task_id: str, should_review_memory: bool = False) -> dict[str, Any]:+ effective_task_id: str, should_review_memory: bool = False,+ turn_id: Any = None) -> dict[str, Any]:"""Hand the turn to a ``codex app-server`` subprocess and project its events into ``messages``.Returns the chat_completions result shape. The user message is ALREADY in ``messages`` — never append it again."""# Defense in depth for compression.checkpoint_required: agent init refuses the combination, but@@ -753,6 +769,7 @@ def run_codex_app_server_turn(agent, *, user_message: str, original_user_messageif getattr(turn, "should_retire", False):logger.warning("codex app-server session retired (turn error: %s)", turn.error)_close_codex_session(agent)+ reply = _transform_codex_reply(agent, turn, turn_id)# The binding is published only once the transcript it belongs to is durable, and never for a# retired thread (the next agent would only resume into the same wedge).if _persist_projected_messages(agent, turn, messages) and not getattr(turn, "should_retire", False):@@ -763,8 +780,7 @@ def run_codex_app_server_turn(agent, *, user_message: str, original_user_messagereturn _turn_result(interrupt, messages, api_calls=1, completed=not turn.interrupted and turn.error is None, error=turn.error,# We flushed the projected rows ourselves (agent_persisted); the gateway must skip its own DB write.- final_response=turn.final_text, agent_persisted=True, codex_thread_id=turn.thread_id, codex_turn_id=turn.turn_id,- **usage_result,+ agent_persisted=True, codex_thread_id=turn.thread_id, codex_turn_id=turn.turn_id, **reply, **usage_result,)diff --git a/agent/conversation_loop.py b/agent/conversation_loop.pyindex 72ea3119e64..b6bf751b73c 100644--- a/agent/conversation_loop.py+++ b/agent/conversation_loop.py@@ -1609,7 +1609,7 @@ def _codex_app_server_turn(agent: Any, s: Any) -> Optional[dict[str, Any]]:codex_result = agent._run_codex_app_server_turn(user_message=s.user_message, original_user_message=s.original_user_message,messages=s.messages, effective_task_id=s.effective_task_id,- should_review_memory=s._should_review_memory,+ should_review_memory=s._should_review_memory, turn_id=s.turn_id,)from agent.turn_recovery import activate_codex_app_server_fallbackif not activate_codex_app_server_fallback(agent, codex_result):diff --git a/tests/agent/test_codex_app_server_transform.py b/tests/agent/test_codex_app_server_transform.pynew file mode 100644index 00000000000..f9b118542f8--- /dev/null+++ b/tests/agent/test_codex_app_server_transform.py@@ -0,0 +1,49 @@+"""The codex app-server runtime runs ``transform_llm_output`` like the generic loop.++That runtime returns before ``finalize_turn``, so a rewriting plugin (redaction) must be applied+to the codex reply before its rows are persisted: the user (who, with ``hold_stream``, saw nothing+stream) receives the rewrite, and the stored assistant row is the rewrite too.+"""++import os+from pathlib import Path+from types import SimpleNamespace+from unittest.mock import MagicMock++from agent.codex_runtime import run_codex_app_server_turn+from tests.fakes.redactor_plugin import RAW, REWRITE, redactor_home+++def _codex_agent(final_text: str):+ agent = MagicMock()+ agent._codex_session = MagicMock() # a seeded session: no subprocess spawn+ agent._codex_session.run_turn.return_value = SimpleNamespace(+ interrupted=False, error=None, thread_id="thread-1", turn_id="turn-1", tool_iterations=0,+ projected_messages=[{"role": "assistant", "content": final_text}], final_text=final_text,+ should_retire=False)+ agent._codex_session_prompt = None+ agent.tool_progress_callback = None+ agent._iters_since_skill = agent._skill_nudge_interval = 0+ agent.valid_tool_names = set()+ agent._session_db = None+ agent._llm_output_transform = None+ agent.session_id = "sess-codex"+ agent.platform = "cli"+ return agent+++def test_codex_reply_is_rewritten_before_it_is_returned_or_stored():+ from hermes_cli import plugins++ redactor_home(Path(os.environ["HERMES_HOME"]), "http://127.0.0.1:9/v1", hold=True)+ plugins._reset_plugin_managers_for_tests()+ try:+ result = run_codex_app_server_turn(+ _codex_agent(RAW), user_message="config?", original_user_message="config?",+ messages=[{"role": "user", "content": "config?"}], effective_task_id="task-1", turn_id="t1")+ finally:+ plugins._reset_plugin_managers_for_tests()++ assert result["final_response"] == REWRITE+ assert result["response_transformed"] is True and result["pre_transform_response"] == RAW+ assert [m["content"] for m in result["messages"] if m["role"] == "assistant"] == [REWRITE]