From ed981cc5efca431531c849c419c322415030b855 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 08:54:00 +0100 Subject: [PATCH 1/2] Keep known model availability in partial Codex usage responses Architecture-Review: unchanged; the parser distinguishes unknown entries while retaining the existing exact-model policy. Co-Authored-By: Codex --- scripts/ai-accounts/ai-accounts | 12 +++++++----- tests/test_ai_accounts.py | 3 ++- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/scripts/ai-accounts/ai-accounts b/scripts/ai-accounts/ai-accounts index 75828558..9caa2bfc 100755 --- a/scripts/ai-accounts/ai-accounts +++ b/scripts/ai-accounts/ai-accounts @@ -629,13 +629,15 @@ def check_codex(name: str) -> dict: model_usage = data.get("model_usage") if not isinstance(model_usage, dict) or not model_usage: return {**result, "error": "usage response has no model usage"} - if any(not isinstance(v, dict) or not isinstance(v.get("available"), bool) - for v in model_usage.values()): - return {**result, "error": "usage response has incomplete model usage"} - models = {m: v["available"] for m, v in model_usage.items()} + # Entries without a bool `available` are unknown, not a logged-out account: skip them and + # take the tier from the entries that do report availability (response shape drifts). + models = {m: v["available"] for m, v in model_usage.items() + if isinstance(v, dict) and isinstance(v.get("available"), bool)} + unknown = sorted(m for m in model_usage if m not in models) usable = [available for available in models.values() if available] tier = 0 if not ok or (models and not usable) else (2 if all(models.values()) else 1) - return {**result, "tier": tier, "models": models} + extra = {"models_unknown": unknown} if unknown else {} + return {**result, "tier": tier, "models": models, **extra} def select(provider: str, results: dict[str, dict], current: str | None) -> str | None: diff --git a/tests/test_ai_accounts.py b/tests/test_ai_accounts.py index 9db4ab49..0c53c6f3 100644 --- a/tests/test_ai_accounts.py +++ b/tests/test_ai_accounts.py @@ -397,9 +397,10 @@ def test_codex_json_and_text_share_each_existing_window(helper, monkeypatch, cap return week = {'used_percent': 96, 'limit_window_seconds': 604800, 'reset_at': 1794384770} short = {'used_percent': 99, 'limit_window_seconds': 18000, 'reset_at': short_reset} if short_reset else None - data = {'rate_limit': {'primary_window': short or week, 'secondary_window': week if short else None}, 'model_usage': {'m': {'available': True}}} + data = {'rate_limit': {'primary_window': short or week, 'secondary_window': week if short else None}, 'model_usage': {'m': {'available': True}, 'partial': {}}} monkeypatch.setattr(helper, '_http', lambda req: (200, {}, json.dumps(data).encode())) result = helper.check_codex('c2') + assert result['models'] == {'m': True} and result['models_unknown'] == ['partial'] assert result['next_reset'] == 1794384770 status = {'checked_at': 10000, 'providers': {'codex': {'active': 'c2', 'accounts': {'c2': result}}}} helper.write_private(helper.STATE / 'status.json', json.dumps(status)) From a45739690d2279f09f3346dbcf49c1cace6743c2 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 08:56:03 +0100 Subject: [PATCH 2/2] Keep all-unknown Codex usage in the unknown tier Co-Authored-By: Codex --- scripts/ai-accounts/ai-accounts | 4 +++- tests/test_ai_accounts.py | 12 +++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/scripts/ai-accounts/ai-accounts b/scripts/ai-accounts/ai-accounts index 9caa2bfc..c7aeaca2 100755 --- a/scripts/ai-accounts/ai-accounts +++ b/scripts/ai-accounts/ai-accounts @@ -634,8 +634,10 @@ def check_codex(name: str) -> dict: models = {m: v["available"] for m, v in model_usage.items() if isinstance(v, dict) and isinstance(v.get("available"), bool)} unknown = sorted(m for m in model_usage if m not in models) + if not models: + return {**result, "error": "usage response has no known model availability", "models_unknown": unknown} usable = [available for available in models.values() if available] - tier = 0 if not ok or (models and not usable) else (2 if all(models.values()) else 1) + tier = 0 if not ok or not usable else (2 if all(models.values()) else 1) extra = {"models_unknown": unknown} if unknown else {} return {**result, "tier": tier, "models": models, **extra} diff --git a/tests/test_ai_accounts.py b/tests/test_ai_accounts.py index 0c53c6f3..2d3037a6 100644 --- a/tests/test_ai_accounts.py +++ b/tests/test_ai_accounts.py @@ -384,7 +384,7 @@ def http(req): assert ('fallback' in output and 'HTTP 403' in output) is (api == 403) -@pytest.mark.parametrize('short_reset', [None, 1793912400, 'offline']) +@pytest.mark.parametrize('short_reset', [None, 1793912400, 'offline', 'all-unknown']) def test_codex_json_and_text_share_each_existing_window(helper, monkeypatch, capsys, short_reset): helper.write_private(helper.codex_home('c2') / 'auth.json', '{"tokens":{"access_token":"fake"}}') monkeypatch.setattr(helper, 'now', lambda: 10000) @@ -396,11 +396,17 @@ def test_codex_json_and_text_share_each_existing_window(helper, monkeypatch, cap assert 'unknown (network: offline)' in capsys.readouterr().out return week = {'used_percent': 96, 'limit_window_seconds': 604800, 'reset_at': 1794384770} - short = {'used_percent': 99, 'limit_window_seconds': 18000, 'reset_at': short_reset} if short_reset else None + short = {'used_percent': 99, 'limit_window_seconds': 18000, 'reset_at': short_reset} if isinstance(short_reset, int) else None data = {'rate_limit': {'primary_window': short or week, 'secondary_window': week if short else None}, 'model_usage': {'m': {'available': True}, 'partial': {}}} + if short_reset == 'all-unknown': + del data['model_usage']['m'] monkeypatch.setattr(helper, '_http', lambda req: (200, {}, json.dumps(data).encode())) result = helper.check_codex('c2') - assert result['models'] == {'m': True} and result['models_unknown'] == ['partial'] + assert result['models_unknown'] == ['partial'] + if short_reset == 'all-unknown': + assert result['tier'] == -1 and result['error'] == 'usage response has no known model availability' + return + assert result['models'] == {'m': True} assert result['next_reset'] == 1794384770 status = {'checked_at': 10000, 'providers': {'codex': {'active': 'c2', 'accounts': {'c2': result}}}} helper.write_private(helper.STATE / 'status.json', json.dumps(status))