The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(codex): treat leaked Codex-CLI shell JSON as an incomplete turn, not a final answer
minhngoc25af5fbe9a609c5
diff --git a/agent/codex_responses_adapter.py b/agent/codex_responses_adapter.pyindex 2dda4aba48d..d28d401cbd3 100644--- a/agent/codex_responses_adapter.py+++ b/agent/codex_responses_adapter.py@@ -58,6 +58,30 @@ def _wire_model_identity(model: Any) -> Optional[str]:# Codex/Harmony tool-call serialization leaked into assistant text (no structured function_call)._TOOL_CALL_LEAK_PATTERN = re.compile(r"(?:^|[\s>|])to=functions\.[A-Za-z_][\w.]*", re.IGNORECASE)+# Codex-CLI-style shell call leaked as assistant text (``{"cmd": "..."}`` closing the message, scalar siblings only).+# Only classified as a leak when the previous line is an action lead-in ("Creating the script now.") — a bare or+# explained JSON object is a legitimate answer and must stay a final response.+_SHELL_JSON_LEAK_PATTERN = re.compile(+ r'(?:^|\n)\s*\{\s*"cmd"\s*:\s*"(?:\\.|[^"\\])*"'+ r'(?:\s*,\s*"[A-Za-z_][\w-]*"\s*:\s*(?:"(?:\\.|[^"\\])*"|true|false|null|-?\d+(?:\.\d+)?))*\s*\}\s*$',+)+_ACTION_VERBS = r"creat(?:e|ing)|writ(?:e|ing)|runn?(?:ing)?|execut(?:e|ing)|check(?:ing)?|verif(?:y|ying)|updat(?:e|ing)|install(?:ing)?|edit(?:ing)?|mak(?:e|ing)"+_SHELL_JSON_LEAK_LEADIN_PATTERN = re.compile(+ rf"^(?:sure,\s*)?(?:now\s+)?(?:let\s+me\s+|i(?:'|’)?ll\s+|i\s+will\s+|i(?:'|’)?m\s+|i\s+am\s+)?(?:{_ACTION_VERBS})\b",+ re.IGNORECASE,+)+++def _leaked_tool_call_text(text: str) -> bool:+ """True when assistant text carries a tool call the model failed to emit as a structured ``function_call``."""+ if _TOOL_CALL_LEAK_PATTERN.search(text):+ return True+ match = _SHELL_JSON_LEAK_PATTERN.search(text)+ if not match:+ return False+ lead_in = text[:match.start()].strip().splitlines()+ return bool(lead_in) and bool(_SHELL_JSON_LEAK_LEADIN_PATTERN.search(lead_in[-1].strip()))+# The Codex backend rejects literal Harmony wire tokens (``invalid_prompt: Request# blocked.``). Fullwidth bars survive format-character stripping and stay legible._HARMONY_CONTROL_TOKEN_RE = re.compile(r"<\|(start|end|channel|message|constrain|return|call)\|>")@@ -1139,8 +1163,9 @@ def _normalize_codex_response(out_text = getattr(response, "output_text", "")final_text = out_text.strip() if isinstance(out_text, str) else final_text# Tool-call leak recovery: gpt-5.x sometimes emits the intended ``function_call`` as plain Harmony text- # (``to=functions.foo {json}``). Treat as incomplete so the continuation re-elicits a real call; clear the garbage.- leaked_tool_call_text = bool(final_text and not tool_calls and _TOOL_CALL_LEAK_PATTERN.search(final_text))+ # (``to=functions.foo {json}``) or Codex-CLI shell JSON (``{"cmd": ...}``). Treat as incomplete so the+ # continuation re-elicits a real call; clear the garbage.+ leaked_tool_call_text = bool(final_text and not tool_calls and _leaked_tool_call_text(final_text))if leaked_tool_call_text:logger.warning("Codex response contains leaked tool-call text in assistant content (no structured function_call "@@ -1166,7 +1191,9 @@ def _normalize_codex_response(content=final_text, tool_calls=tool_calls,reasoning="\n\n".join(reasoning_parts).strip() if reasoning_parts else None,reasoning_content=None, reasoning_details=None,- codex_reasoning_items=scan.reasoning_items_raw or None, codex_message_items=scan.message_items_raw or None,+ codex_reasoning_items=scan.reasoning_items_raw or None,+ # Leaked text must not be replayed as a completed assistant message on the continuation.+ codex_message_items=None if leaked_tool_call_text else (scan.message_items_raw or None),)# Reasoning-only: for Codex/xAI/GitHub, status=completed means "still thinking" → incomplete so the continuation# retries. Other backends trust response.status — forcing incomplete there stalls for minutes on a final state.diff --git a/tests/agent/test_codex_responses_adapter.py b/tests/agent/test_codex_responses_adapter.pyindex 11075c433b7..add8e68596e 100644--- a/tests/agent/test_codex_responses_adapter.py+++ b/tests/agent/test_codex_responses_adapter.py@@ -824,6 +824,48 @@ def test_format_responses_error_message_only():assert _format_responses_error(err, "failed") == "Upstream model unavailable"+def _final_text_response(text):+ return SimpleNamespace(+ status="completed", incomplete_details=None, output_text=text,+ output=[SimpleNamespace(+ type="message", role="assistant", status="completed", id="msg_1",+ content=[SimpleNamespace(type="output_text", text=text)],+ )],+ )++[email protected]("text", [+ 'Creating the PowerShell script now.\n{"cmd": "mkdir -p /c/Temp && cat > /c/Temp/x.ps1 <<\'EOF\'"}',+ 'Sure, let me run the tests.\n{"cmd": "pytest -q", "workdir": "/repo", "timeout": 120}',+ "Calling tool now to=functions.terminal {\"command\": \"ls\"}",+])+def test_normalize_codex_response_treats_leaked_tool_call_text_as_incomplete(text):+ """#56920: Codex-CLI shell JSON (or Harmony ``to=functions``) leaked as assistant text is a failed tool call,+ not a final answer — classify incomplete so the continuation re-elicits a structured ``function_call``, and+ drop the message items so the leak is never replayed as a completed assistant turn."""+ assistant_message, finish_reason = _normalize_codex_response(_final_text_response(text), issuer_kind="codex_backend")++ assert finish_reason == "incomplete"+ assert assistant_message.content == ""+ assert assistant_message.tool_calls == []+ assert assistant_message.codex_message_items is None++[email protected]("text", [+ 'Here is the JSON payload the CLI expects:\n{"cmd": "mkdir -p /c/Temp"}',+ '{"cmd": "ls"}',+ 'Creating the file now.\n{"cmd": "ls"}\nDone — the file is in place.',+])+def test_normalize_codex_response_keeps_legitimate_cmd_json_answer(text):+ """#56920 false-positive guard: ``{"cmd": ...}`` without an action lead-in, or not closing the message,+ is an answer about JSON and stays a completed response with its replay items intact."""+ assistant_message, finish_reason = _normalize_codex_response(_final_text_response(text), issuer_kind="codex_backend")++ assert finish_reason == "stop"+ assert assistant_message.content == text+ assert assistant_message.codex_message_items++def test_normalize_codex_response_failed_includes_code_in_error():"""Regression: response_status == 'failed' should surface the errorcode, not just the message. Used to leak a bare 'Slow down' string