Skip to content

Make manual Codex account selection immediate and prevent unexpected pool rotation #504

Closed

Description

@str0203

Summary

Manual account selection in the Codex account pool currently behaves as “use this account for the next session”, but the implementation can still route traffic to other pool accounts in ways that are difficult for users to predict.

I reproduced three independent causes:

  1. Existing threads retain their previous account affinity after a manual account change.
  2. During startup, an explicitly selected account can be replaced while quota data is still unknown.
  3. A single transient 5xx can soft-avoid the selected account immediately, even when upstreamFailoverThreshold is configured as 3.

Together, these behaviors make quota usage appear to “leak” across the pool after a user explicitly selected one account.

This is related to #425, but the requested behavior is different. #425 proposes opt-in account-qualified model namespaces with strict per-conversation fail-closed routing. This issue proposes improving the existing global Pool selection while preserving quota-aware rotation and failover.

Environment

  • opencodex: 2.7.40
  • Codex runtime: 0.146.0-alpha.3.1
  • Platform: macOS
  • OpenAI provider mode: Pool
  • Auto-switch threshold: 80%
  • Upstream failover threshold: 3
  • Multiple ChatGPT Plus accounts in the pool

Current behavior

1. Manual selection only affects future sessions

PUT /api/codex-auth/active updates activeCodexAccountId, but does not clear the runtime thread-affinity map.

As a result:

  1. Thread T is bound to account A.
  2. The user selects account B in the dashboard.
  3. A new request from T continues using A.
  4. Only a new thread uses B.

The dashboard wording (“Set as Next Session”) describes this behavior, but it is surprising when the user expects the selected account to become the active pool account.

2. Unknown quota can override an explicit selection

At startup, quota priming is asynchronous. Before quota data arrives, the selected account receives the unknown usage score.

The current deadlock guard can rotate among unknown candidates, including replacing a manually selected pool account with the main login.

Observed log:

[codex-routing] quota unknown for active "<selected-account>"; rotating to "__main__" (all candidates unknown, threshold=80)

Unknown quota is not evidence that the account reached the configured threshold, so this rotation overrides the user's choice without a real quota signal.

3. One transient 5xx can spread traffic across the pool

The transient failure path creates softAvoidUntil immediately whenever failover is enabled.

That makes isCodexAccountSelectable false after the first transient 5xx, so the next request can use another account before consecutiveFailures reaches upstreamFailoverThreshold.

Observed routing timeline:

05:39 selected pool account -> 502
05:41 main account          -> requests begin
05:52 main account          -> 502
05:57 main account          -> 502
05:57 another pool account  -> requests begin

This occurred while the selected account was far below the 80% quota threshold.

Expected behavior

When a user manually selects an account in Pool mode:

  • Clear existing thread-account affinity.
  • Apply the selected account to the next request from existing and new threads.
  • Requests already in flight may retain their captured authentication context.
  • Keep using the selected account while its known usage remains below the auto-switch threshold.
  • If quota is unknown, keep the selected account until quota data becomes available.
  • For transient connection errors and 5xx responses, keep the selected account until failures reach upstreamFailoverThreshold.
  • Continue to rotate immediately for hard conditions:
    • 401/403 authentication failure
    • 429 quota cooldown
    • the configured quota threshold is reached and a lower-usage account is available

This preserves automatic rotation while making manual selection authoritative and understandable.

Suggested implementation

Manual account selection

In PUT /api/codex-auth/active:

  • Persist activeCodexAccountId.
  • Clear the complete thread-affinity map.
  • Clear transient failure/soft-avoid state for the manually selected account.
  • Preserve hard 429 cooldown and reauthentication state.
  • Return an explicit field such as appliesImmediately: true.

Unknown quota

In quota auto-switching:

  • Do not rotate when the selected account's quota is unknown.
  • Keep the selected account and wait for quota priming or the next upstream quota response.
  • Only compare accounts after the selected account has a real usage score.

Transient failures

  • Count transient failures first.
  • Create softAvoidUntil and clear affinity only when consecutiveFailures >= upstreamFailoverThreshold.
  • A successful terminal response should reset the transient streak.
  • Keep the current immediate handling for credential and quota failures.

Dashboard and CLI

Replace wording such as:

Set as Next Session
Next Session

with wording such as:

Select Account
Selected

The confirmation should explain that the choice applies to the next request from existing and new threads.

Auditability

Add an account-switch history to the dashboard and request logs:

  • previous account
  • selected account
  • timestamp
  • reason: manual, quota threshold, 429 cooldown, authentication failure, or transient-failure threshold

This would make unexpected quota movement diagnosable without exposing account PII.

Local validation

A local proof-of-concept was tested with the following behavior:

  • An existing test thread was first bound to account A.
  • Changing only activeCodexAccountId left the thread on A, reproducing the current behavior.
  • Clearing thread affinity during manual selection caused the same existing thread's next resolution to use account B.
  • Unknown quota retained the selected account.
  • The first and second transient 502 did not soft-avoid the account.
  • The third consecutive transient 502 enabled failover.
  • 401/403, 429, and quota-threshold switching were left unchanged.
  • Bun builds of the CLI and library entry points completed successfully.

Acceptance criteria

  • Manual account selection applies to the next request from existing and new threads.
  • Existing thread affinity is cleared when the user changes the selected account.
  • Unknown quota never overrides an explicit selection.
  • Transient failures respect upstreamFailoverThreshold.
  • 401/403 and 429 still trigger immediate safety handling.
  • Quota-based rotation still works at the configured threshold.
  • UI and CLI wording reflect immediate account selection.
  • Tests cover manual selection, startup with unknown quota, transient failure thresholds, hard cooldown, and authentication failure.

Activity

  1. github-actions commented on Jul 26, 2026

    @github-actions
    Contributor

    Automated translation bookkeeping — detected language: English.

  2. github-actions commented on Jul 26, 2026

    @github-actions
    Contributor

    Possibly related issues:

    Reason: The new issue discusses improving manual account selection in Codex while the related issue 425 proposes a different approach to account management, focusing on exposing accounts as model namespaces.

    Detected automatically via GitHub Models.

  3. Ingwannu commented on Jul 26, 2026

    @Ingwannu
    Owner

    Confirmed against current dev (9d1bb146): the three routing behaviors are real.

    • PUT /api/codex-auth/active persists the selected id but does not clear the thread-affinity map or the selected account's transient health state.
    • applyQuotaAutoSwitch() explicitly rotates an unknown active account to another unknown candidate.
    • transient outcomes create softAvoidUntil and drop the failing thread's affinity from the first failure, before upstreamFailoverThreshold is reached.

    I will harden the existing Pool contract by:

    1. making a manual selection immediately authoritative for subsequent requests, clearing all existing affinity plus only the selected account's transient soft-avoid/failure streak while preserving hard quota cooldown and reauth state;
    2. keeping the explicitly active account while its quota is unknown;
    3. applying transient soft-avoid and affinity clearing only once the configured consecutive-failure threshold is reached, while leaving 401/403 and 429 immediate handling unchanged;
    4. updating the API response, dashboard wording, CLI-visible behavior/docs, and focused routing/API/UI regressions.

    The proposed account-switch history is useful but is a separate observability feature, not required to fix the three release-blocking routing defects, so I will not bundle it into this hardening PR. I will link the dev PR and complete verification here before closing.

  4. Ingwannu commented on Jul 26, 2026

    @Ingwannu
    Owner

    Fixed and merged into dev via PR #506 (2d5d4916).

    The landed Pool behavior now matches the immediate-selection contract:

    • manual selection clears all existing thread affinity and the selected account's transient failure/soft-avoid state, while preserving hard 429 cooldown and reauthentication state;
    • the API returns appliesImmediately: true;
    • an explicitly active account remains selected while its quota is unknown instead of rotating on a guessed score;
    • transient 5xx/connect/timeout outcomes only create soft-avoid and clear affinity once consecutiveFailures reaches upstreamFailoverThreshold;
    • 401/403 quarantine, 429 cooldown, and known quota-threshold rotation remain immediate/unchanged;
    • GUI and CLI now say Selected / Select Account, with localized dashboard and reference documentation describing next-request semantics.

    Verification: 199 focused backend/CLI tests, 13 focused GUI tests, TypeScript typecheck, GUI ESLint and production build, 131-page docs build, CodeRabbit, React Doctor, npm-global install checks, and the complete Ubuntu/macOS/Windows CI matrix all passed. The final review queue had no unresolved threads.

    The requested account-switch history remains a separate observability enhancement and was intentionally not bundled into this release-hardening fix.

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