The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(usage): label Codex quota windows by published duration, not response position
Simplicio, Wesley (ext)5685b4ea9e32
diff --git a/agent/account_usage.py b/agent/account_usage.pyindex ffa3a8cceb9..5be58865c90 100644--- a/agent/account_usage.py+++ b/agent/account_usage.py@@ -382,6 +382,28 @@ def _usage_windows(return windows+# Published Codex quota windows by ``limit_window_seconds``: 5h session and 7-day weekly.+_CODEX_WINDOW_LABELS_BY_SECONDS = {18000: "Session", 604800: "Weekly"}+_CODEX_WINDOW_POSITIONAL_LABELS = (("primary_window", "Session"), ("secondary_window", "Weekly"))+++def _codex_window_labels(rate_limit: dict) -> tuple[tuple[str, str], ...]:+ """Label Codex windows by their published duration, not response position (#65387).++ The usage API keys windows ``primary_window``/``secondary_window`` by position; when only the+ weekly limit is returned it occupies ``primary_window`` and the positional mapping mislabeled it+ ``Session``. Windows whose ``limit_window_seconds`` is missing or unrecognized keep the legacy+ positional label so duration-less payloads render exactly as before.+ """+ labels = []+ for key, fallback in _CODEX_WINDOW_POSITIONAL_LABELS:+ window = rate_limit.get(key) or {}+ seconds = window.get("limit_window_seconds") if isinstance(window, dict) else None+ label = _CODEX_WINDOW_LABELS_BY_SECONDS.get(int(seconds), fallback) if _is_num(seconds) else fallback+ labels.append((key, label))+ return tuple(labels)++def _plural(count: int) -> str:return "s" if count != 1 else ""@@ -403,8 +425,8 @@ def _fetch_codex_account_usage(payload = _get_json(_codex_backend_urls(resolved_base_url)[0], _codex_headers(token, account_id), timeout=15.0,)- windows = _usage_windows(payload.get("rate_limit") or {}, (("primary_window", "Session"), ("secondary_window", "Weekly")),- "used_percent", "reset_at")+ rate_limit = payload.get("rate_limit") or {}+ windows = _usage_windows(rate_limit, _codex_window_labels(rate_limit), "used_percent", "reset_at")details: list[str] = []count = _codex_banked_resets(payload)if count > 0:diff --git a/tests/agent/test_account_usage.py b/tests/agent/test_account_usage.pyindex 1705228f3ef..57bd566a4de 100644--- a/tests/agent/test_account_usage.py+++ b/tests/agent/test_account_usage.py@@ -125,6 +125,42 @@ def test_codex_usage_falls_back_to_native_credential_pool(monkeypatch, codex_usa+def _explicit_creds_snapshot(monkeypatch, payload):+ calls = []+ monkeypatch.setattr(account_usage.httpx, "Client", lambda timeout: _FakeClient(calls, payload))+ snapshot = account_usage.fetch_account_usage(+ "openai-codex", base_url="https://chatgpt.com/backend-api/codex", api_key="live-agent-token",+ )+ return snapshot, calls+++def test_codex_weekly_only_primary_window_is_labeled_weekly(monkeypatch):+ """#65387: a lone 604800s primary_window is the weekly limit, not the session one."""+ payload = {"plan_type": "pro", "rate_limit": {+ "primary_window": {"used_percent": 1, "limit_window_seconds": 604800},+ "secondary_window": None,+ }}+ snapshot, _ = _explicit_creds_snapshot(monkeypatch, payload)+ assert [(w.label, w.used_percent) for w in snapshot.windows] == [("Weekly", 1.0)]+++def test_codex_window_labels_follow_duration_with_positional_fallback(monkeypatch):+ # Swapped positions: labels must follow limit_window_seconds.+ payload = {"rate_limit": {+ "primary_window": {"used_percent": 4, "limit_window_seconds": 604800},+ "secondary_window": {"used_percent": 21, "limit_window_seconds": 18000},+ }}+ snapshot, _ = _explicit_creds_snapshot(monkeypatch, payload)+ assert [w.label for w in snapshot.windows] == ["Weekly", "Session"]+ # Missing / unrecognized durations keep the legacy positional labels.+ payload = {"rate_limit": {+ "primary_window": {"used_percent": 4},+ "secondary_window": {"used_percent": 21, "limit_window_seconds": 12345},+ }}+ snapshot, _ = _explicit_creds_snapshot(monkeypatch, payload)+ assert [w.label for w in snapshot.windows] == ["Session", "Weekly"]++def test_codex_usage_account_id_read_failure_keeps_singleton_token(monkeypatch, codex_usage_payload):"""When the resolver succeeds but the separate account_id read raises, theworking singleton token must still be used (best-effort account_id), NOT