Skip to content

The agent that grows with you

test(auth): fold the quota-probe rotation regression into the probe suite

kshitijk4poorf1df745d71cb
diff --git a/tests/agent/test_credential_pool_codex_quota_probe_rotation.py b/tests/agent/test_credential_pool_codex_quota_probe_rotation.py
deleted file mode 100644
index c7244d55586..00000000000
--- a/tests/agent/test_credential_pool_codex_quota_probe_rotation.py
+++ /dev/null
@@ -1,97 +0,0 @@
-"""The Codex quota probe's pre-probe token rotation must survive the cooldown clear.
-
-An exhausted Codex row skips the proactive refresh chain, so by the time the selection-path quota
-probe runs its access token has expired: the probe spends the single-use refresh token first and
-adopts the rotated pair into the pool. When the probe then reports the quota restored, the cooldown
-clear must build on that rotated row — not on the caller's pre-probe copy — or the consumed refresh
-token goes back into the pool and the next refresh replays it (``refresh_token_reused``).
-"""
-from __future__ import annotations
-
-import base64
-import json
-import time
-
-import pytest
-
-import hermes_cli.auth as auth_mod
-import hermes_cli.auth_codex as auth_codex
-from agent.credential_pool import load_pool
-
-
-def _jwt(exp: float) -> str:
- def seg(obj: dict) -> str:
- return base64.urlsafe_b64encode(json.dumps(obj).encode()).rstrip(b"=").decode()
-
- payload = {"exp": int(exp), "sub": "user-q", "https://api.openai.com/auth": {"chatgpt_account_id": "acct-q"}}
- return f"{seg({'alg': 'none'})}.{seg(payload)}.sig"
-
-
-def home(tmp_path, monkeypatch):
- home = tmp_path / "hermes"
- monkeypatch.setenv("HERMES_HOME", str(home))
- auth_mod._codex_quota_probe_cache.clear()
- yield home
- auth_mod._codex_quota_probe_cache.clear()
-
-
-def _write_exhausted_row(home, now: float) -> None:
- home.mkdir(parents=True, exist_ok=True)
- (home / "auth.json").write_text(json.dumps({
- "version": 1,
- "providers": {},
- "credential_pool": {"openai-codex": [{
- "id": "cred-quota", "label": "quota-frozen", "auth_type": "oauth", "priority": 0,
- "source": "manual:device_code",
- "access_token": _jwt(now - 7200), # expired while the cooldown ran
- "refresh_token": "rt-0",
- "last_status": "exhausted", "last_status_at": now - 3600,
- "last_error_code": 429, "last_error_reason": "usage_limit_reached",
- "last_error_message": "The usage limit has been reached",
- "last_error_reset_at": now + 6 * 24 * 3600,
- }]},
- }), encoding="utf-8")
-
-
-def _rotating_token_endpoint(monkeypatch, now: float) -> list:
- """Single-use refresh tokens: rt-N mints rt-(N+1); a spent token is rejected."""
- sent: list = []
- spent: set = set()
-
- def refresh(access_token, refresh_token, **kw):
- sent.append(refresh_token)
- if refresh_token in spent:
- raise RuntimeError("refresh_token_reused")
- spent.add(refresh_token)
- n = int(refresh_token.rsplit("-", 1)[1]) + 1
- return {"access_token": _jwt(now + 3600 * n), "refresh_token": f"rt-{n}", "last_refresh": "now"}
-
- monkeypatch.setattr(auth_codex, "refresh_codex_oauth_pure", refresh)
- monkeypatch.setattr(auth_mod, "refresh_codex_oauth_pure", refresh)
- return sent
-
-
-def test_quota_restored_selection_keeps_probe_rotated_refresh_token(home, monkeypatch):
- now = time.time()
- _write_exhausted_row(home, now)
- sent = _rotating_token_endpoint(monkeypatch, now)
- monkeypatch.setattr(auth_mod, "_probe_codex_quota_restored", lambda token, **kw: True)
- monkeypatch.setattr(auth_codex, "_probe_codex_quota_restored", lambda token, **kw: True)
-
- pool = load_pool("openai-codex")
- selected = pool.select()
-
- assert sent == ["rt-0"] # the probe's pre-probe refresh; the spent rt-0 is not replayed
- assert selected is not None and selected.last_status == "ok"
- assert selected.refresh_token == "rt-1"
- assert pool._find(lambda e: e.id == "cred-quota").refresh_token == "rt-1"
- disk = json.loads((home / "auth.json").read_text(encoding="utf-8"))
- assert disk["credential_pool"]["openai-codex"][0]["refresh_token"] == "rt-1"
-
- refreshed = pool.try_refresh_current()
-
- assert sent == ["rt-0", "rt-1"] # rt-0 is never replayed
- assert refreshed is not None and refreshed.refresh_token == "rt-2"
- disk = json.loads((home / "auth.json").read_text(encoding="utf-8"))
- assert disk["credential_pool"]["openai-codex"][0]["refresh_token"] == "rt-2"
diff --git a/tests/hermes_cli/test_auth_codex_quota_probe.py b/tests/hermes_cli/test_auth_codex_quota_probe.py
index 5aa8decb600..fab7d2bed6d 100644
--- a/tests/hermes_cli/test_auth_codex_quota_probe.py
+++ b/tests/hermes_cli/test_auth_codex_quota_probe.py
@@ -356,6 +356,7 @@ def _fake_refresh(monkeypatch, fresh_token, calls):
return {"access_token": fresh_token, "refresh_token": "rf-new", "last_refresh": "now"}
monkeypatch.setattr(auth_codex, "refresh_codex_oauth_pure", _refresh)
+ monkeypatch.setattr(auth_mod, "refresh_codex_oauth_pure", _refresh) # the pool's refresh path
def test_resolver_refreshes_expired_token_before_probe(tmp_path, monkeypatch):
@@ -414,6 +415,31 @@ def test_pool_selection_refreshes_expired_token_before_probe(tmp_path, monkeypat
assert disk["providers"]["openai-codex"]["tokens"]["refresh_token"] == "rf-new"
+def test_pool_selection_keeps_probe_rotated_tokens_when_quota_restored(tmp_path, monkeypatch):
+ """The restored twin of the control above: the cooldown clear must build on the row the
+ probe rotated, not on the selection loop's pre-probe copy, or the consumed refresh token
+ goes back into the pool and the same select() replays it (``refresh_token_reused``)."""
+ now = time.time()
+ hermes_home = tmp_path / "hermes"
+ store = _expired_jwt_pool_store(now)
+ store["credential_pool"]["openai-codex"][0]["source"] = "manual:device_code" # no singleton re-sync
+ _write_auth_store(hermes_home, store)
+ monkeypatch.setenv("HERMES_HOME", str(hermes_home))
+ fresh = _jwt({"exp": now + 3600})
+ refresh_calls: list = []
+ _fake_refresh(monkeypatch, fresh, refresh_calls)
+ http_calls = _patch_expiry_aware_httpx(monkeypatch, _StubResponse(200, _usage_payload(0.0, 0.0)))
+ from agent.credential_pool import load_pool
+
+ selected = load_pool("openai-codex").select()
+
+ assert refresh_calls == ["rf-old"]
+ assert [c["headers"]["Authorization"] for c in http_calls] == [f"Bearer {fresh}"]
+ assert (selected.access_token, selected.refresh_token, selected.last_status) == (fresh, "rf-new", "ok")
+ disk = json.loads((hermes_home / "auth.json").read_text())
+ assert disk["credential_pool"]["openai-codex"][0]["refresh_token"] == "rf-new"
+
+
def test_pool_selection_throttles_failing_pre_probe_refresh(tmp_path, monkeypatch):
"""Regression control: a frozen entry whose refresh keeps failing (revoked grant, network
down) must not POST to the token endpoint on every selection — at most one attempt per