Skip to content

The agent that grows with you

refactor: inline the pre-stream transport-cause check in run_codex_stream

teknium1ca94646b7eb1
diff --git a/agent/codex_runtime.py b/agent/codex_runtime.py
index 6416993efbb..79f079b138d 100644
--- a/agent/codex_runtime.py
+++ b/agent/codex_runtime.py
@@ -57,27 +57,6 @@ def _codex_request_failure_details(error: BaseException) -> tuple[int | None, st
return request_body_bytes, " <- ".join(exception_classes)
-def _has_transport_error_cause(error: BaseException) -> bool:
- """True when any link of the exception chain is an httpx transport error.
-
- The OpenAI SDK wraps pre-stream connect/receive failures (``ReadError``,
- ``RemoteProtocolError``) in ``APIConnectionError``; the wrapped cause is
- the retryable signal. A watchdog retirement surfaces as ``TimeoutError``
- instead, so it never matches here.
- """
- import httpx as _httpx
-
- current: BaseException | None = error
- seen: set[int] = set()
- while current is not None and id(current) not in seen and len(seen) < 8:
- seen.add(id(current))
- if isinstance(current, _httpx.TransportError):
- return True
- implicit_chain = current.__cause__ is None and not current.__suppress_context__
- current = current.__context__ if implicit_chain else current.__cause__
- return False
-
-
def _coerce_usage_int(value: Any) -> int:
if isinstance(value, bool):
return 0
@@ -1027,13 +1006,12 @@ def run_codex_stream(agent, api_kwargs: dict, client: Any = None, on_first_delta
return event_stream.final_response
raise
except _APIConnectionError as exc:
+ # The SDK wraps every connect/receive failure (``raise APIConnectionError from err``), so the
+ # raw ``transport_errors`` branch above never sees a pre-stream failure. Before the stream
+ # opened nothing is billed, so one fresh physical request is safe (#103673); once the writer
+ # token is claimed the inference may already be billed, so mid-stream failures still raise.
if (attempt < max_stream_retries and writer_token["value"] is None
- and _has_transport_error_cause(exc)):
- # Pre-stream SDK connect failure (#103673): the request was
- # serialized but the stream never opened, so nothing was
- # billed and a fresh physical request is safe. Mid-stream
- # failures keep the old behavior (raise) to avoid
- # duplicating an already-billed inference.
+ and isinstance(exc.__cause__, _httpx.TransportError)):
logger.debug(
"Codex Responses pre-stream connect failed (attempt %s/%s); retrying. %s error=%s",
attempt + 1, max_stream_retries + 1, agent._client_log_context(), exc,