diff --git a/scripts/ai-accounts/ai-accounts b/scripts/ai-accounts/ai-accounts index 75828558..c7aeaca2 100755 --- a/scripts/ai-accounts/ai-accounts +++ b/scripts/ai-accounts/ai-accounts @@ -629,13 +629,17 @@ 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) + 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) - return {**result, "tier": tier, "models": models} + 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} 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..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,10 +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 - data = {'rate_limit': {'primary_window': short or week, 'secondary_window': week if short else None}, 'model_usage': {'m': {'available': True}}} + 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_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))