Skip to content

The agent that grows with you

fix(auxiliary): let the shared normalizer judge tool-call-like aux text

kshitijk4poora5cdd9ee299f
diff --git a/agent/auxiliary_client.py b/agent/auxiliary_client.py
index bf78dc34ce6..365ecc57f1c 100644
--- a/agent/auxiliary_client.py
+++ b/agent/auxiliary_client.py
@@ -1133,10 +1133,7 @@ def _parse_codex_final_response(
final: Any, *, issuer_kind: Optional[str] = None, issuer_model: Optional[str] = None,
) -> Tuple[List[str], List[Any], Any, str]:
"""Normalize Responses output without losing phase or completion state for aux callers."""
- from agent.codex_responses_adapter import (
- _INCOMPLETE_STATUSES, _SERVER_SIDE_TOOL_CALL_TYPES, _extract_responses_message_text, _leaked_tool_call_text,
- _lower_or_none, _normalize_codex_response,
- )
+ from agent.codex_responses_adapter import _lower_or_none, _normalize_codex_response
# The shared normalizer reads SDK-style items. Keep support for compatible hosts
# returning dict items, and the aux adapter's legacy empty completed response.
@@ -1151,32 +1148,17 @@ def _parse_codex_final_response(
incomplete_details=getattr(final, "incomplete_details", None),
error=getattr(final, "error", None),
)
+ # Aux has no continuation to re-elicit a leaked tool call, so tool-call-shaped text stays content
+ # and goes through the normalizer's own phase/completion gates (no clear, no WARNING).
message, finish_reason = _normalize_codex_response(
- normalized_final, issuer_kind=issuer_kind, issuer_model=issuer_model,
+ normalized_final, issuer_kind=issuer_kind, issuer_model=issuer_model, recover_leaked_tool_call=False,
)
- status = _lower_or_none(normalized_final.status)
- reason = str(_field(normalized_final.incomplete_details, "reason", "") or "").strip().lower()
- # The main loop's leaked-tool-call recovery clears the text so its continuation can re-elicit a
- # real call; aux has no continuation, so a completed answer quoting such text keeps it.
- # Only a fully completed response qualifies: any unfinished (non-server-side) item is still a partial.
- if finish_reason == "incomplete" and status == "completed" and not message.tool_calls \
- and message.codex_message_items is None and not any(
- _field(item, "type") not in _SERVER_SIDE_TOOL_CALL_TYPES
- and _lower_or_none(_field(item, "status")) in _INCOMPLETE_STATUSES
- for item in output):
- # Same gate as the normalizer: commentary/analysis is never the answer, nor is output_text alongside it.
- narration = {"commentary", "analysis"}
- phases = {_lower_or_none(_field(item, "phase")) for item in output if _field(item, "type") == "message"}
- answer = "\n".join(filter(None, (
- _extract_responses_message_text(item) for item in output
- if _field(item, "type") == "message" and _lower_or_none(_field(item, "phase")) not in narration
- ))).strip() or ("" if phases & narration else (normalized_final.output_text or "").strip())
- if answer and _leaked_tool_call_text(answer):
- message.content, finish_reason = answer, "stop"
# Aux consumers speak Chat Completions: "length" activates their existing
# partial-summary rejection/fallback, whereas Codex's "incomplete" does not.
# A final_answer phase cannot override the provider's incomplete status, but completed
# tool calls stay "tool_calls" so dispatchers (e.g. MCP sampling) still run them.
+ status = _lower_or_none(normalized_final.status)
+ reason = str(_field(normalized_final.incomplete_details, "reason", "") or "").strip().lower()
if finish_reason != "tool_calls" and (
finish_reason == "incomplete" or (status == "incomplete" and reason != "content_filter")
):
diff --git a/agent/codex_responses_adapter.py b/agent/codex_responses_adapter.py
index 94b15a1006f..6296934b740 100644
--- a/agent/codex_responses_adapter.py
+++ b/agent/codex_responses_adapter.py
@@ -1159,9 +1159,12 @@ class _OutputScan:
def _normalize_codex_response(
response: Any, *, issuer_kind: Optional[str] = None, issuer_model: Optional[str] = None,
+ recover_leaked_tool_call: bool = True,
) -> tuple[Any, str]:
"""Normalize a Responses API object to ``(assistant_message, finish_reason)``.
- ``issuer_kind`` / ``issuer_model`` are stamped onto captured reasoning items for provenance replay drops."""
+ ``issuer_kind`` / ``issuer_model`` are stamped onto captured reasoning items for provenance replay drops.
+ ``recover_leaked_tool_call=False`` is for callers with no continuation (aux): tool-call-shaped text is
+ then kept as ordinary content and judged by the same phase/completion gates as any other answer."""
response_status = _lower_or_none(getattr(response, "status", None))
incomplete_reason = str(_field(getattr(response, "incomplete_details", None), "reason", "") or "").strip().lower()
response_incomplete_content_filter = response_status == "incomplete" and incomplete_reason == "content_filter"
@@ -1194,7 +1197,9 @@ def _normalize_codex_response(
# Tool-call leak recovery: gpt-5.x sometimes emits the intended ``function_call`` as plain Harmony 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))
+ leaked_tool_call_text = bool(
+ recover_leaked_tool_call and 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 "
diff --git a/tests/agent/test_auxiliary_codex_completion_status.py b/tests/agent/test_auxiliary_codex_completion_status.py
index bb910007f3e..6121d7d4705 100644
--- a/tests/agent/test_auxiliary_codex_completion_status.py
+++ b/tests/agent/test_auxiliary_codex_completion_status.py
@@ -90,8 +90,8 @@ _CODEX_REASONING_ONLY = SimpleNamespace(
pytest.param("", "completed", [_TOOL_CALL], None, None, "tool_calls", id="tool-call"),
pytest.param("", "completed", [_message(_LEAK_LIKE_ANSWER, phase=None)], None, _LEAK_LIKE_ANSWER, "stop",
id="tool-call-like-answer"),
- pytest.param("", "completed", [_message(_LEAK_LIKE_ANSWER, phase=None), _PARTIAL_TOOL_CALL], None, None,
- "length", id="tool-call-like-partial-item"),
+ pytest.param("", "completed", [_message(_LEAK_LIKE_ANSWER, phase=None), _PARTIAL_TOOL_CALL], None,
+ _LEAK_LIKE_ANSWER, "length", id="tool-call-like-partial-item"),
# A str ``output`` is delivered only via output_text (empty output list), as streamed Codex answers can be.
pytest.param("", "completed", _LEAK_LIKE_ANSWER, None, _LEAK_LIKE_ANSWER, "stop", id="tool-call-like-output-text"),
# (items, text): output_text beside a commentary item is narration, never the rescued answer.
@@ -99,6 +99,9 @@ _CODEX_REASONING_ONLY = SimpleNamespace(
id="tool-call-like-output-text-beside-commentary"),
pytest.param("", "incomplete", [_message("PARTIAL")], "max_output_tokens", "PARTIAL", "length", id="token-cap"),
pytest.param("", "incomplete", [_TOOL_CALL], "max_output_tokens", None, "tool_calls", id="token-cap-after-tool-call"),
+ # Commentary + unphased text is incomplete whether or not the unphased text looks like a tool call.
+ pytest.param("", "completed", [_message("NOTE", phase="commentary"), _message(_LEAK_LIKE_ANSWER, phase=None)],
+ None, _LEAK_LIKE_ANSWER, "length", id="tool-call-like-beside-commentary"),
# Route-sensitive normalization: the issuer comes from the request's route classification.
pytest.param("https://chatgpt.com/backend-api/codex", "completed", [_CODEX_REASONING_ONLY], None, None, "length",
id="codex-reasoning-only"),