Skip to content

The agent that grows with you

fix(fallback): one early-reopen restore per probe interval, not one per turn

kshitijk4poora3512a527fc6
diff --git a/agent/agent_runtime_helpers.py b/agent/agent_runtime_helpers.py
index fff3ac10acd..5859c1ca209 100644
--- a/agent/agent_runtime_helpers.py
+++ b/agent/agent_runtime_helpers.py
@@ -1295,16 +1295,28 @@ def _primary_quota_reopened_early(agent, primary_provider, primary_model, matche
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.
+
+ At most one lift per probe interval per agent: the probe caches its verdict and nothing clears
+ it on the next 429, so a "restored" answer the endpoint got wrong would otherwise restore,
+ fail, and fall back again on every turn of the cached window.
"""
if primary_provider != "openai-codex":
return False
+ from hermes_cli.auth_codex import CODEX_QUOTA_PROBE_MIN_INTERVAL_SECONDS
+ if time.monotonic() - getattr(agent, "_codex_reopen_checked_at", float("-inf")) < CODEX_QUOTA_PROBE_MIN_INTERVAL_SECONDS:
+ return False
try:
pool = getattr(agent, "_credential_pool", None)
if pool is None or not matches_primary(pool):
pool = load_primary_pool()
+ if pool is None:
+ return False
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))
+ benched_until = pool.next_available_at(model=model)
+ if benched_until is None or benched_until <= time.time():
+ return False
+ agent._codex_reopen_checked_at = time.monotonic()
+ return pool.lift_reopened_cooldowns(model=model)
except Exception:
logger.debug("Early quota-reopen check failed; keeping the cooldown", exc_info=True)
return False
diff --git a/tests/agent/test_fallback_restore_codex_early_reopen.py b/tests/agent/test_fallback_restore_codex_early_reopen.py
index 98a89af72ac..b558581c48b 100644
--- a/tests/agent/test_fallback_restore_codex_early_reopen.py
+++ b/tests/agent/test_fallback_restore_codex_early_reopen.py
@@ -50,13 +50,18 @@ def _fallback_agent():
)
agent.client = MagicMock()
agent._primary_runtime = {**agent._primary_runtime, "provider": "openai-codex", "base_url": CODEX_URL}
+ agent._swap_credential = MagicMock()
+ _pin_to_fallback(agent)
+ return agent
+
+
+def _pin_to_fallback(agent):
+ agent._fallback_index = 0
client = MagicMock(api_key="fallback-key-1234", base_url="https://openrouter.ai/api/v1")
with patch("agent.auxiliary_client.resolve_provider_client", return_value=(client, None)):
assert agent._try_activate_fallback() is True
# The weekly reset the provider declared, armed by _arm_rate_limit_cooldown.
agent._rate_limited_until = time.monotonic() + 3 * 86400
- agent._swap_credential = MagicMock()
- return agent
def _restore(agent, quota_restored):
@@ -83,3 +88,18 @@ def test_stays_on_fallback_while_quota_is_still_spent(tmp_path, monkeypatch):
assert _restore(agent, quota_restored=False) is False
assert agent._fallback_activated is True
+
+
+def test_a_wrong_restored_verdict_does_not_flip_back_every_turn(tmp_path, monkeypatch):
+ """The probe caches "restored" for its interval and a fresh 429 does not clear it."""
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ _write_exhausted_codex_pool(tmp_path, reset_in_seconds=3 * 86400)
+ agent = _fallback_agent()
+ assert _restore(agent, quota_restored=True) is True
+
+ # The primary 429s again: the pool re-benches the entry and the session falls back.
+ _write_exhausted_codex_pool(tmp_path, reset_in_seconds=3 * 86400)
+ _pin_to_fallback(agent)
+
+ assert _restore(agent, quota_restored=True) is False
+ assert agent._fallback_activated is True