Skip to content

The agent that grows with you

fix(agent): fail over from codex app-server quota errors (#71642 re-port)

kingrubic8535b7f1964a
diff --git a/agent/conversation_loop.py b/agent/conversation_loop.py
index 2343c74547f..79303c09110 100644
--- a/agent/conversation_loop.py
+++ b/agent/conversation_loop.py
@@ -1519,11 +1519,18 @@ def _run_conversation_turn(
# Opt-in runtime: api_mode == codex_app_server hands the whole turn to the codex
# app-server subprocess (see agent/transports/codex_app_server_session.py).
if agent.api_mode == "codex_app_server":
- return agent._run_codex_app_server_turn(
+ 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,
)
+ from agent.turn_recovery import activate_codex_app_server_fallback
+ if not activate_codex_app_server_fallback(agent, codex_result):
+ return codex_result
+ # Fallback activation rewrote provider/model/api_mode: retry this same user turn on the generic
+ # loop below, keeping codex's projected rows and its failed API call in the turn's accounting.
+ s.api_call_count = int(codex_result.get("api_calls") or 0)
+ s.active_system_prompt = _sync_failover_system_message(agent, None, s.active_system_prompt)
while (s.api_call_count < agent.max_iterations and agent.iteration_budget.remaining > 0) or agent._budget_grace_call:
if _run_phase(begin_iteration, agent, s).action == "break":
diff --git a/agent/turn_recovery.py b/agent/turn_recovery.py
index f8fb9357788..3f0f45e7410 100644
--- a/agent/turn_recovery.py
+++ b/agent/turn_recovery.py
@@ -1440,14 +1440,25 @@ def _eager_fallback_status(classified: Any, is_upstream: bool, is_transport_fail
return "⚠️ Rate limited — switching to fallback provider..."
+_CODEX_APP_SERVER_FALLBACK_REASONS = frozenset({
+ FailoverReason.billing, FailoverReason.rate_limit, FailoverReason.upstream_rate_limit,
+})
+
+
def activate_codex_app_server_fallback(agent: Any, result: Dict[str, Any]) -> bool:
"""The codex app-server runtime reports a failed turn as ``result["error"]`` text instead of
raising, so the generic classify -> ``fallback_providers`` chain never saw it (#71633).
Classify that text; on a billing / rate-limit verdict activate the configured fallback and
return True so the caller re-runs the same user turn on the generic loop."""
- if result.get("interrupted"):
+ error = result.get("error")
+ if not error or result.get("interrupted") or not agent._has_pending_fallback():
+ return False
+ from agent.error_classifier import classify_api_error
+ classified = classify_api_error(
+ RuntimeError(str(error)), provider=getattr(agent, "provider", "") or "", model=getattr(agent, "model", "") or "",
+ )
+ if classified.reason not in _CODEX_APP_SERVER_FALLBACK_REASONS:
return False
- classified = None # re-port of #71642 (@kingrubic) restores the classification hunks
agent._buffer_diagnostic_status(
_eager_fallback_status(classified, classified.reason == FailoverReason.upstream_rate_limit, False))
return bool(agent._try_activate_fallback(reason=classified.reason))