Repository navigation
Description
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:
- Existing threads retain their previous account affinity after a manual account change.
- During startup, an explicitly selected account can be replaced while quota data is still unknown.
- A single transient 5xx can soft-avoid the selected account immediately, even when
upstreamFailoverThresholdis 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:
- Thread T is bound to account A.
- The user selects account B in the dashboard.
- A new request from T continues using A.
- 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
softAvoidUntiland clear affinity only whenconsecutiveFailures >= 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
activeCodexAccountIdleft 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
github-actions commentedon Jul 26, 2026
Automated translation bookkeeping — detected language: English.
github-actions commentedon Jul 26, 2026
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.
Ingwannu commentedon Jul 26, 2026
Confirmed against current
dev(9d1bb146): the three routing behaviors are real.PUT /api/codex-auth/activepersists 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.softAvoidUntiland drop the failing thread's affinity from the first failure, beforeupstreamFailoverThresholdis reached.I will harden the existing Pool contract by:
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
devPR and complete verification here before closing.Ingwannu commentedon Jul 26, 2026
Fixed and merged into
devvia PR #506 (2d5d4916).The landed Pool behavior now matches the immediate-selection contract:
appliesImmediately: true;consecutiveFailuresreachesupstreamFailoverThreshold;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.