Skip to content

[Bug] quota-unknown auto-switch rotates to __main__ first (score=100 ≥ threshold) #605

Closed as not planned

Description

@sergiobuilds

Summary

When Codex account quotas are unprimed / unknown, pool auto-switch can rotate the active account to __main__ even though:

  • the previously active pool account is still the intended one, and
  • __main__ may already be weekly-exhausted.

This breaks the expected sequential model (burn current account → switch at threshold% used → next account) and can produce cooling down / 429 storms and surprising account switches.

Environment

  • @bitkyc08/opencodex 2.7.40
  • codexAccountMode: pool
  • autoSwitchThreshold: 80 (default)
  • multiple ChatGPT Pro accounts in the pool + main login
  • Linux, proxy on 127.0.0.1:10100
  • Clients: Hermes Agent via OpenAI-compatible POST /v1/chat/completions (and general Codex traffic)

Observed logs

[codex-routing] quota unknown for active "chatgpt-XXXXXXXX"; rotating to "__main__" (all candidates unknown, threshold=80)
[codex-routing] quota unknown for active "__main__" and no eligible rotation target; staying put

After this, traffic can stick on MAIN (or thrash) instead of staying on the healthy pool account until real weekly usage hits the threshold.

Root cause (code)

File: src/codex/routing.ts

  1. Unknown usage is scored as 100 (CODEX_UNKNOWN_USAGE_SCORE = 100 in src/codex/quota.ts).
  2. In applyQuotaAutoSwitch, unknown active usage is therefore treated as ≥ threshold (80) even when the account is not actually near weekly limit.
  3. pickLowerUsageAccount cannot prefer another unknown account (100 < 100 never wins).
  4. Deadlock guard then calls pickNextUnknownAccount().
  5. getEligiblePoolAccounts() unshifts __main__ first, so eligible[0] for unknown rotation is almost always MAIN.

Relevant logic (2.7.40):

// applyQuotaAutoSwitch
const activeUsage = computeCodexUsageScore(quota, ...);
if (activeUsage < threshold) return active; // unknown=100 → false when threshold=80

// ... pickLowerUsageAccount fails among unknowns ...

if (isUnknownUsage(activeUsage)) {
  const next = pickNextUnknownAccount(config, active, now);
  // next is often __main__ because getEligiblePoolAccounts unshifts MAIN
}
// getEligiblePoolAccounts
ids.unshift(MAIN_CODEX_ACCOUNT_ID);
// pickNextUnknownAccount
return eligible.length > 0 ? eligible[0]! : null; // MAIN if present

Expected behavior

For pool + threshold auto-switch:

  1. Stay on the current active account while usage is known and < threshold.
  2. If quota is unknown, do not treat it as 100% used and do not rotate just because it is unknown. Prefer priming quotas and staying put.
  3. If unknown-rotation remains as a last resort, do not prefer MAIN by list order. Prefer non-main pool accounts first; only consider MAIN if no other eligible account exists.
  4. Weekly-exhausted MAIN should not become the catch-all target when pool accounts are healthy but unprimed.

Suggested fix

Minimal options (any one is a large improvement):

A (preferred): If isUnknownUsage(activeUsage), stay on active and rely on existing primeCodexPoolQuotas pre-route priming. Do not rotate on unknown alone.

B: Score unknown as “not over threshold” (e.g. treat unknown as 0 for threshold comparison, while still marking unknown separately).

C: If keeping unknown-rotation, change pickNextUnknownAccount to prefer id !== MAIN_CODEX_ACCOUNT_ID before falling back to MAIN; and/or stop unshifting MAIN to the front for this path.

D: Never auto-select MAIN when its last known weekly usage is ≥ threshold / exhausted (even if currently unknown in-memory).

Local workaround we applied

We patched applyQuotaAutoSwitch to stay put when active quota is unknown, and made pickNextUnknownAccount prefer non-main (defense in depth). After restart + ocx account refresh openai, sequential threshold behavior is usable again.

Happy to open a PR if you want this upstreamed.

Impact

  • Multi-account Pro pools unexpectedly jump to MAIN
  • Users see rate-limit / “cooling down” / provider failures despite other pool accounts having low weekly %
  • Breaks “one account until 80%, then next” operational model

Activity

  1. github-actions commented on Jul 28, 2026

    @github-actions
    Contributor

    Issue closed: use an issue template

    Thanks for taking the time to submit this issue. It was closed automatically because it was not opened with a recognized issue template (Bug report, Feature request, Documentation, or Provider compatibility). Freeform and API-opened issues cannot skip this check.

    • This issue does not use a recognized issue template.

    Please edit the issue to add:

    • Open a new issue with the Bug report, Feature request, Documentation, or Provider compatibility template.
    • Or edit this issue so the body uses the template section headings for the kind of report you are filing.

    See the Contributing guide for details.

    Once the report passes the automated checks, it will be reopened automatically unless a maintainer has changed its state.

  2. github-actions commented on Jul 28, 2026

    @github-actions
    Contributor

    Automated translation bookkeeping — detected language: English.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions