The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(codex): quota probe and /usage pool paths use the pool route base
kshitijk4poor5e2d55ca9c65
diff --git a/agent/account_usage.py b/agent/account_usage.pyindex e5989e26b92..477c99d3a1f 100644--- a/agent/account_usage.py+++ b/agent/account_usage.py@@ -10,6 +10,7 @@ import httpxfrom agent.anthropic_credentials import _is_oauth_token, resolve_anthropic_tokenfrom hermes_cli.auth import AuthError, _read_codex_tokens, resolve_codex_runtime_credentials+from hermes_cli.auth_codex import _codex_pool_route_base_urlfrom hermes_cli.runtime_provider import resolve_runtime_providerfrom hermes_time import safe_strftime@@ -333,7 +334,7 @@ def _resolve_codex_usage_credentials(entry = load_pool("openai-codex").try_refresh_matching(api_key_hint=explicit_key)if entry is None:raise RuntimeError("Could not refresh the Codex credential this session runs on")- return entry.runtime_api_key, str(entry.runtime_base_url or base_url or "").strip(), None+ return entry.runtime_api_key, _codex_pool_route_base_url(entry.runtime_base_url or base_url), None# Only AuthError is caught so tier 3 can run: a broad except would mask a transient refresh/network failure# and hand back a DIFFERENT pool account's usage; such errors must propagate to the fail-open outer guard.# account_id is best-effort: a partial singleton store must not sink a usable credential.@@ -362,7 +363,8 @@ def _resolve_codex_usage_credentials(entry = load_pool("openai-codex").select()if entry is None:raise RuntimeError("No available openai-codex credential in credential pool")- return entry.runtime_api_key, str(entry.runtime_base_url or base_url or "").strip(), None+ # Pool rows keep the canonical URL; a gateway key must go to its route host, not chatgpt.com (#121486).+ return entry.runtime_api_key, _codex_pool_route_base_url(entry.runtime_base_url or base_url), Nonedef _codex_banked_resets(payload: dict) -> int:diff --git a/agent/credential_pool.py b/agent/credential_pool.pyindex 7d7ed8202e7..86661b3332d 100644--- a/agent/credential_pool.py+++ b/agent/credential_pool.py@@ -1944,7 +1944,10 @@ class CredentialPool(CredentialPoolAdminMixin, CredentialPoolModelCooldownMixin))self._sync_device_code_entry_to_auth_store(entry)token = entry.access_token or token- return bool(auth_mod._probe_codex_quota_restored(token, base_url=entry.base_url))+ # The row keeps the canonical URL; a gateway key belongs to its route host (#121486).+ from hermes_cli.auth_codex import _codex_pool_route_base_url+ return bool(auth_mod._probe_codex_quota_restored(+ token, base_url=_codex_pool_route_base_url(entry.base_url)))except Exception:logger.debug("Codex quota-restored probe failed", exc_info=True)return Falsediff --git a/hermes_cli/auth_codex.py b/hermes_cli/auth_codex.pyindex c141dd8d057..cc6c0e9f2f1 100644--- a/hermes_cli/auth_codex.py+++ b/hermes_cli/auth_codex.py@@ -841,7 +841,8 @@ def _probe_codex_pool_entry_quota_restored(entry: Dict[str, Any]) -> Optional[bologger.debug("Failed to persist refreshed Codex pool tokens", exc_info=True)if not token:return None- return _probe_codex_quota_restored(token, base_url=entry.get("base_url"))+ # The row keeps the canonical URL; a gateway key belongs to its route host (#121486).+ return _probe_codex_quota_restored(token, base_url=_codex_pool_route_base_url(entry.get("base_url")))def clear_codex_pool_quota_cooldowns(access_token: Optional[str] = None) -> int:diff --git a/tests/hermes_cli/test_codex_credential_host_binding.py b/tests/hermes_cli/test_codex_credential_host_binding.pyindex 458c4e82d9d..9d9bf4ff823 100644--- a/tests/hermes_cli/test_codex_credential_host_binding.py+++ b/tests/hermes_cli/test_codex_credential_host_binding.py@@ -311,6 +311,133 @@ def test_image_request_goes_to_the_pool_entry_gateway(monkeypatch, key):assert seen[0][1] == f"Bearer {key}"+# ── quota-restored probe + /usage (pool rows keep the canonical ChatGPT URL) ──────────────+++class _UsageRecorder:+ """``httpx.Client`` stand-in recording every Authorization-bearing GET."""++ def __init__(self, seen):+ self.seen = seen++ def __enter__(self):+ return self++ def __exit__(self, *exc):+ return False++ def get(self, url, headers=None, **_kw):+ self.seen.append((str(url), (headers or {}).get("Authorization", "")))+ return SimpleNamespace(status_code=200, json=lambda: {+ "rate_limit": {"primary_window": {"used_percent": 10}}}, headers={})+++def _exhausted_jwt_pool(home: Path) -> None:+ """A JWT-shaped gateway key in a rate-limited pool row that still carries the canonical URL."""+ import time+ now = time.time()+ (home / "auth.json").write_text(json.dumps({+ "version": 1, "providers": {},+ "credential_pool": {"openai-codex": [{+ "id": "gw", "label": "gateway", "auth_type": "oauth", "priority": 0,+ "source": "device_code", "access_token": JWT, "base_url": CHATGPT,+ "last_status": "exhausted", "last_status_at": now, "last_error_code": 429,+ "last_error_reason": "usage_limit_reached", "last_error_message": "The usage limit has been reached",+ "last_error_reset_at": now + 3 * 24 * 3600,+ }]},+ }))+++def usage_probe_http(monkeypatch):+ from hermes_cli import auth as auth_mod+ from hermes_cli import auth_codex+ seen = []+ auth_mod._codex_quota_probe_cache.clear()+ monkeypatch.setattr(auth_codex, "_codex_http_client", lambda **kw: _UsageRecorder(seen))+ yield seen+ auth_mod._codex_quota_probe_cache.clear()+++def test_quota_restored_probe_of_a_persisted_entry_asks_the_gateway(monkeypatch, usage_probe_http):+ home = _home(monkeypatch)+ _write_config(home, base_url=GW)+ _exhausted_jwt_pool(home)+ from hermes_cli.auth_codex import _probe_codex_pool_entry_quota_restored++ entry = json.loads((home / "auth.json").read_text())["credential_pool"]["openai-codex"][0]+ assert _probe_codex_pool_entry_quota_restored(entry) is True++ assert _authorized_hosts(usage_probe_http) == {"codex-gw.example"}+++def test_pool_selection_quota_probe_asks_the_gateway(monkeypatch, usage_probe_http):+ home = _home(monkeypatch)+ _write_config(home, base_url=GW)+ _exhausted_jwt_pool(home)+ from agent.credential_pool import load_pool++ pool = load_pool("openai-codex")+ (entry,) = pool.entries()+ assert pool._codex_quota_restored_upstream(entry) is True++ assert _authorized_hosts(usage_probe_http) == {"codex-gw.example"}+++def test_quota_restored_probe_direct_chatgpt_positive_control(monkeypatch, usage_probe_http):+ home = _home(monkeypatch)+ _write_config(home)+ _exhausted_jwt_pool(home)+ from hermes_cli.auth_codex import _probe_codex_pool_entry_quota_restored++ entry = json.loads((home / "auth.json").read_text())["credential_pool"]["openai-codex"][0]+ _probe_codex_pool_entry_quota_restored(entry)++ assert _authorized_hosts(usage_probe_http) == {"chatgpt.com"}+++def test_usage_pool_fallback_tier_sends_gateway_key_to_the_gateway(monkeypatch):+ """``/usage`` tier 3 (runtime resolver raised): the pooled key is paired with its route."""+ from agent import account_usage++ home = _home(monkeypatch)+ _write_config(home, base_url=GW)+ _write_pool(home, OPAQUE)+ seen = []++ def _no_runtime(**_kw):+ raise account_usage.AuthError("no creds", provider="openai-codex", code="codex_auth_missing")++ monkeypatch.setattr(account_usage, "resolve_codex_runtime_credentials", _no_runtime)+ monkeypatch.setattr(account_usage.httpx, "Client", lambda **kw: _UsageRecorder(seen))++ account_usage.fetch_account_usage("openai-codex")++ assert _authorized_hosts(seen) == {"codex-gw.example"}+ assert seen[0][1] == f"Bearer {OPAQUE}"+++def test_usage_forced_refresh_keeps_the_refreshed_pool_key_on_the_gateway(monkeypatch):+ """The 401 retry refreshes the live agent's own pool entry; the row's canonical URL must not+ pull the refreshed gateway key over to chatgpt.com."""+ from agent import account_usage++ home = _home(monkeypatch)+ _write_config(home, base_url=GW)+ monkeypatch.setattr(account_usage, "_read_codex_tokens", lambda: {"tokens": {}})++ class Pool:+ def try_refresh_matching(self, api_key_hint=None, credential_id=None):+ return SimpleNamespace(runtime_api_key="fresh-gw-key", runtime_base_url=CHATGPT)++ monkeypatch.setattr("agent.credential_pool.load_pool", lambda provider: Pool())++ token, base_url, _acct = account_usage._resolve_codex_usage_credentials(+ GW, "stale-gw-key", force_refresh=True)++ assert (token, base_url) == ("fresh-gw-key", GW)++def test_route_fallback_reads_the_profile_scoped_override_not_a_sibling_process_env(monkeypatch):"""If route resolution itself fails, the fallback still honours only the routed profile's``HERMES_CODEX_BASE_URL`` — never a multiplexed sibling's process env."""