Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions scripts/ai-accounts/ai-accounts
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: An exhausted account with all-unknown models is reported as unknown (tier -1), not unusable (tier 0)

When ok is False (rate limit reached or utilization ≥ MARGIN), the response already shows the account can't be used, whatever its model availability. The new early return still gives tier -1, so select treats it as unknown instead of known-exhausted. The existing 'no model usage' path (line 631) does the same, so the behaviour is consistent and not a bug. If you want the known exhaustion to count, return tier: 0 here when not ok.

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:
Expand Down
13 changes: 10 additions & 3 deletions tests/test_ai_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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': {}}}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: The partial-entry case is added to a window-sharing test instead of its own focused parser test

The new assertion sits inside test_codex_json_and_text_share_each_existing_window, which is about quota windows. The test never checks the resulting tier, or that model_availability reports an unknown model as not known. It also has no case where every entry is unknown, which is the case that regressed. Add a small dedicated test with three cases: a mix of known and unknown entries (tier 2 or 1 as expected, plus models_unknown), all entries unknown (tier -1 with an error), and model_availability('codex', name, 'partial') returning known=False.

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'

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: All-unknown case doesn't check that quota windows are kept

The fix in check_codex (scripts/ai-accounts/ai-accounts:637-638) is correct. Before it, when every model entry was unknown, models was empty, all({}) was True, and an account with available quota got tier 2, so routing could pick an account whose model availability was unknown. Now it returns tier -1 and still includes **result (windows, util, next_reset). The new 'all-unknown' test branch only checks tier, error and models_unknown, then returns. It never checks windows or next_reset. The acceptance criteria require quota windows to be preserved, so a future change that returns a bare error dict on this path would still pass. Smallest fix: before the early return, add assert result['next_reset'] == 1794384770 and result['windows'] and 'models' not in result.

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))
Expand Down
Loading