Skip to content

The agent that grows with you

refactor(auxiliary): move the Codex aux response normalizer into a sibling

kshitijk4poora94ef2fea574
diff --git a/agent/auxiliary_client.py b/agent/auxiliary_client.py
index 54fda40aa68..9e5fcdca726 100644
--- a/agent/auxiliary_client.py
+++ b/agent/auxiliary_client.py
@@ -1128,52 +1128,6 @@ def _scoped_key_env(name: str) -> str:
return (os.getenv(name) or "").strip()
-# Codex Responses → chat.completions adapter, so aux consumers need no changes.
-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 _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.
- output = [
- SimpleNamespace(**item) if isinstance(item, dict) else item
- for item in (getattr(final, "output", None) or [])
- ]
- normalized_final = SimpleNamespace(
- output=output or [SimpleNamespace(type="message", content=[])],
- output_text=getattr(final, "output_text", None),
- status=getattr(final, "status", None),
- 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, recover_leaked_tool_call=False,
- )
- # Aux consumers speak Chat Completions: "length" activates their existing
- # partial-summary rejection/fallback, whereas Codex's "incomplete" does not.
- # Any provider-incomplete response (token cap or content_filter) is a partial no aux consumer
- # may commit; completed tool calls stay "tool_calls" so dispatchers (e.g. MCP sampling) still run them.
- if finish_reason != "tool_calls" and (
- finish_reason == "incomplete" or _lower_or_none(normalized_final.status) == "incomplete"
- ):
- finish_reason = "length"
- text_parts = [message.content] if message.content else []
- tool_calls_raw = message.tool_calls
- usage = None
- resp_usage = getattr(final, "usage", None)
- if resp_usage:
- def _u(key: str) -> int:
- return getattr(resp_usage, key, 0) or (resp_usage.get(key, 0) if isinstance(resp_usage, dict) else 0)
- usage = SimpleNamespace(
- prompt_tokens=_u("input_tokens"), completion_tokens=_u("output_tokens"),
- total_tokens=_u("total_tokens"))
- return text_parts, tool_calls_raw, usage, finish_reason
-
-
def _attempt_stream_socket(stream: Any) -> Any:
"""The raw socket under an SDK event stream (``stream.response`` is the ``httpx.Response``;
httpcore publishes its connection as the ``network_stream`` extension), or None."""
@@ -1637,6 +1591,8 @@ class _CodexCompletionsAdapter:
guard.release_stream(event_stream)
if final is None:
raise RuntimeError("Codex auxiliary Responses stream did not return a final response")
+ from agent.auxiliary_codex_response import _parse_codex_final_response
+
text_parts, tool_calls_raw, usage, finish_reason = _parse_codex_final_response(
final, issuer_kind=issuer_kind, issuer_model=issuer_model,
)
diff --git a/agent/auxiliary_codex_response.py b/agent/auxiliary_codex_response.py
new file mode 100644
index 00000000000..12adf529cf7
--- /dev/null
+++ b/agent/auxiliary_codex_response.py
@@ -0,0 +1,55 @@
+"""Codex Responses → chat.completions normalization for auxiliary callers.
+
+Aux consumers speak Chat Completions, so a Responses result is mapped onto
+``(text_parts, tool_calls, usage, finish_reason)`` through the main loop's shared
+normalizer, keeping its phase and completion gates.
+"""
+
+from types import SimpleNamespace
+from typing import Any, List, Optional, Tuple
+
+
+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 _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.
+ output = [
+ SimpleNamespace(**item) if isinstance(item, dict) else item
+ for item in (getattr(final, "output", None) or [])
+ ]
+ normalized_final = SimpleNamespace(
+ output=output or [SimpleNamespace(type="message", content=[])],
+ output_text=getattr(final, "output_text", None),
+ status=getattr(final, "status", None),
+ 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, recover_leaked_tool_call=False,
+ )
+ # Aux consumers speak Chat Completions: "length" activates their existing
+ # partial-summary rejection/fallback, whereas Codex's "incomplete" does not.
+ # Any provider-incomplete response (token cap or content_filter) is a partial no aux consumer
+ # may commit; completed tool calls stay "tool_calls" so dispatchers (e.g. MCP sampling) still run them.
+ if finish_reason != "tool_calls" and (
+ finish_reason == "incomplete" or _lower_or_none(normalized_final.status) == "incomplete"
+ ):
+ finish_reason = "length"
+ text_parts = [message.content] if message.content else []
+ tool_calls_raw = message.tool_calls
+ usage = None
+ resp_usage = getattr(final, "usage", None)
+ if resp_usage:
+ def _u(key: str) -> int:
+ return getattr(resp_usage, key, 0) or (resp_usage.get(key, 0) if isinstance(resp_usage, dict) else 0)
+ usage = SimpleNamespace(
+ prompt_tokens=_u("input_tokens"), completion_tokens=_u("output_tokens"),
+ total_tokens=_u("total_tokens"))
+ return text_parts, tool_calls_raw, usage, finish_reason
+