Skip to content

feat: automatically switch accounts when quota is exhausted - #209

Open
lifan-builds wants to merge 1 commit into
Lampese:mainfrom
lifan-builds:codex/upstream-auto-switch
Open

lifan-builds wants to merge 1 commit into
Lampese:mainfrom
lifan-builds:codex/upstream-auto-switch

Conversation

@lifan-builds

@lifan-builds lifan-builds commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Closes #207.

When the active ChatGPT account exhausts its quota, this opt-in feature selects the next saved account with freshly confirmed capacity and reuses the existing close/switch/reopen flow.

Behavior

  • Check once a minute while desktop Switcher runs. Confirm exhaustion at 100% of either usage window, allow usable credits, and skip API-key accounts or unknown/failed quota responses. Walk saved accounts in order with wraparound.
  • Recheck quota and the active source before acting. Enforce the expected source under the shared native authentication lock so a queued automatic switch cannot override an intervening manual selection.
  • Close and reopen supported running desktops, using graceful closing unless force close was selected. Leave unsupported processes alone. When Codex is already closed, change only the account.
  • Expose monitoring/error status and a turn-off control. Use one in-flight check, 15-second read timeouts, and a five-minute cooldown after a switch/error. Pause while manual switch, Settings, or add-account dialogs are open. Disable background suspension on macOS 14+ for tray monitoring.

Validation

Checks rerun on this standalone branch on October 28, 2026, after merging current upstream main: pnpm build passed; pnpm test passed (28 passed, 1 platform-specific skip); cargo test --locked --manifest-path src-tauri/Cargo.toml -- --test-threads=1 passed (62 passed, 1 opt-in live test ignored); changed Rust formatting and git diff --check passed.

Five policy tests cover quota thresholds, credits, unknown/error results, saved-order wraparound, candidate failures, resets, source changes, cancellation, unsupported processes, and switch failures. The native race test verifies the source is checked after waiting for the authentication lock. Earlier mocked production-frontend scenarios covered close/switch/reopen ordering and failures, opt-in/off, cooldown, and source races in the combined implementation; this split reran builds and automated suites.

A full automatic switch against a real newly exhausted account has not been observed. Windows runtime and hidden-window timing outside macOS 14+ remain unverified. Enabling the feature authorizes automatic closing; unsaved Codex work can be lost, as stated in Settings.

Related contributions

Conversation recovery: #205 / #206. Direct re-login: #208 / #210. This branch targets upstream main independently and contains no recovery or fork-distribution code. When merging the features together, preserve both App/Settings changes, route automatic closing through recovery when enabled, and include the re-login dialog in the monitoring pause guard.

@Bl0ck154

Bl0ck154 commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

I compared this with the auto-switch feature in my fork. The quotaAutoSwitch.ts logic, its React hook, and the quota policy tests match this PR exactly. Our fork CI has passed with that code: https://github.com/Bl0ck154/codex-switcher/actions/runs/41720701434

I also tried merging this PR into current upstream main, and Git found no conflicts. GitHub doesn't show checks on this PR branch, so the standalone workflow still needs approval/running. I haven't independently verified every live close/switch/reopen scenario; the author's test notes already explain those gaps.

@lifan-builds

Copy link
Copy Markdown
Contributor Author

Thanks @Bl0ck154 for verifying this branch and running the full CI suite in your fork!

cc @emreertunc @Lampese — #209 (opt-in quota auto-switch) and #210 (direct re-login for expired saved accounts) are both small, self-contained, conflict-free against main, and verified in fork CI. Whenever you have a moment, could you approve the pending action_required workflow runs or take a look at #209 / #210? Happy to adjust anything.

@emreertunc emreertunc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[P2] Prevent automatic closing when unsupported sessions are also running

In src/lib/quotaAutoSwitch.ts:53–56, finding one reopenable desktop is enough to allow automatic switching. However, the shared close operation terminates all detected active sessions.

With one desktop and one terminal CLI session running, this closes both but only reopens the desktop. This contradicts the stated behavior of leaving unsupported processes alone.

I reproduced the policy decision with count: 2 and desktop_count: 1: it calls the switch callback and returns switched: true.

Please require every active process root to belong to a supported desktop before proceeding, and enforce this again in the native close path. Add regression coverage for mixed desktop/terminal sessions.

The reviewed head passed 62 Rust tests, 28 frontend tests, and the production frontend build; those tests do not cover this case.

Since #206 is now merged, please also sync with main, resolve the App/Settings conflicts, and preserve conversation recovery in the automatic switching flow when enabled.

@lifan-builds
lifan-builds force-pushed the codex/upstream-auto-switch branch from 1f6a89d to 79a9cd2 Compare November 19, 2026 19:23
@lifan-builds

Copy link
Copy Markdown
Contributor Author

Thanks @emreertunc — updated in 79a9cd2:

  1. Mixed desktop / CLI session guard (policy + native close path):
    • get_codex_reopen_info now also reports active_count from the same process snapshot, and checkQuotaAutoSwitch requires every active process root to belong to a supported desktop (desktop_count === activeRoots, falling back to processes.count) before proceeding.
    • kill_codex_processes accepts require_all_desktops (enabled by runAutomaticDesktopSwitch) and verifies via ensure_all_roots_supported that every active root PID was captured as a supported desktop before terminating any process, while preserving manual user-confirmed closing.
  2. Regression coverage: added unit tests in tests/quotaAutoSwitch.test.ts (count: 2, desktop_count: 1 and active_count: 2, desktop_count: 1 vs all-desktop roots) and in src-tauri/src/commands/process.rs (automatic_close_rejects_mixed_desktop_and_terminal_sessions_before_termination).
  3. Rebase & feat: recover interrupted conversations after switching accounts #206 integration: rebased onto current main (v0.2.24), resolved App/Settings/account.rs conflicts, and wired runAutomaticDesktopSwitch through prepare_codex_session_recovery / finishSessionRecovery (plus cancel_codex_session_recovery on abort) with regression coverage.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: automatically switch to an account with available quota

3 participants