The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(fallback): lift a reopened Codex quota window before gating the primary restore
xxxigmc5a68f0777cb
diff --git a/agent/agent_runtime_helpers.py b/agent/agent_runtime_helpers.pyindex 44df8210976..fff3ac10acd 100644--- a/agent/agent_runtime_helpers.py+++ b/agent/agent_runtime_helpers.py@@ -1288,6 +1288,28 @@ def _revert_credential_rotation(agent) -> None:agent._credential_pool_revert_id = None+def _primary_quota_reopened_early(agent, primary_provider, primary_model, matches_primary, load_primary_pool) -> bool:+ """True when a Codex quota window that the primary pool still has benched has reopened.++ A Codex 429 benches the entry, and arms ``_rate_limited_until``, until a ``resets_at`` that can be+ days out (weekly window). The window can reopen sooner (redeemed reset, top-up, plan change); the+ pool's throttled usage probe notices, but only on ``select()``, which a session pinned to its+ fallback never reaches. Fails closed: any doubt leaves both cooldowns in force.+ """+ if primary_provider != "openai-codex":+ return False+ try:+ pool = getattr(agent, "_credential_pool", None)+ if pool is None or not matches_primary(pool):+ pool = load_primary_pool()+ model = primary_model or None+ benched_until = getattr(pool, "next_available_at", lambda **_kwargs: None)(model=model)+ return bool(benched_until is not None and benched_until > time.time() and pool.lift_reopened_cooldowns(model=model))+ except Exception:+ logger.debug("Early quota-reopen check failed; keeping the cooldown", exc_info=True)+ return False++def restore_primary_runtime(agent) -> bool:"""Restore the primary runtime at the start of a new turn so fallback stays turn-scoped(long-lived CLI agents and the gateway's cached agents)."""@@ -1302,8 +1324,6 @@ def restore_primary_runtime(agent) -> bool:# leaves _fallback_index >= len(_fallback_chain) while _fallback_activated stays False. The next turn# skips this block entirely, stranding the index and silently blocking all future fallback attempts for# the session. Fixes #20465.- if getattr(agent, "_rate_limited_until", 0) > time.monotonic():- return False # primary still in rate-limit cooldown, stay on fallbackrt = agent._primary_runtimeprimary_provider = str((rt or {}).get("provider") or "").strip().lower()primary_model = str((rt or {}).get("model") or "").strip()@@ -1327,6 +1347,10 @@ def restore_primary_runtime(agent) -> bool:key = resolve_runtime_pool_key(primary_provider, primary_runtime_base_url)loaded = load_pool(key) if key else Nonereturn loaded if loaded is not None and _matches_primary(loaded) else None+ if _primary_quota_reopened_early(agent, primary_provider, primary_model, _matches_primary, _load_primary_pool):+ agent._rate_limited_until = 0+ if getattr(agent, "_rate_limited_until", 0) > time.monotonic():+ return False # primary still in rate-limit cooldown, stay on fallbackblocked, prefetched_pool, prefetched = _primary_reset_gate_blocks(agent, rt, primary_provider, primary_runtime_base_url, _matches_primary, _load_primary_pool)diff --git a/agent/credential_pool.py b/agent/credential_pool.pyindex d082468c55b..4ad5d303ee7 100644--- a/agent/credential_pool.py+++ b/agent/credential_pool.py@@ -1048,6 +1048,17 @@ class CredentialPool(CredentialPoolAdminMixin, CredentialPoolModelCooldownMixin)available, _pending = self._available_entries(model=model)return bool(available)+ def lift_reopened_cooldowns(self, *, model: Optional[str] = None) -> bool:+ """Clear cooldowns that have elapsed or (Codex) reopened early, then report availability.++ The lift ``select()`` performs, without leasing an entry: ``has_available`` and+ ``next_available_at`` never run the early-reopen probe, so a caller that only gates on+ them (the fallback restore) waits out the whole declared reset window.+ """+ with self._lock:+ available, _pending = self._available_entries(clear_expired=True, model=model)+ return bool(available)+def next_available_at(self, *, model: Optional[str] = None) -> Optional[float]:"""Earliest epoch time (seconds) any entry re-enters rotation.