Skip to content

Add one-shot automatic switch#148

Open
bjspi wants to merge 2 commits into
Loongphy:mainfrom
bjspi:codex/auto-switch-once
Open

Add one-shot automatic switch#148
bjspi wants to merge 2 commits into
Loongphy:mainfrom
bjspi:codex/auto-switch-once

Conversation

@bjspi

@bjspi bjspi commented Jun 28, 2026

Copy link
Copy Markdown

Closes #147

Adds codex-auth switch --auto for one-shot automatic switching.

Behavior:

  • requires both 5h and weekly remaining limits to be greater than 0%
  • skips the currently active account
  • chooses the inactive eligible account with the earliest 5h reset window
  • supports --api and --skip-api
  • rejects --auto with --live or explicit switch targets

Verification:

  • Build passed.
  • switch --help shows the new --auto usage.
  • Full test suite: 408/414 passed, 5 skipped, 1 failed. The remaining failure is the existing Windows PowerShell-only login launcher integration scenario, unrelated to this switch change.

@bjspi

bjspi commented Jun 28, 2026

Copy link
Copy Markdown
Author

I need it to automate switching completely without always "knowing" the exact account which has still free limits left... Hope you'll be able to merge it. thanks

@greptile-apps

greptile-apps Bot commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds codex-auth switch --auto, a one-shot non-interactive mode that picks the best eligible inactive account (both 5h and weekly limits > 0%) and switches to it without opening the picker. It also fixes a pre-existing tiebreaker bug in the live auto-switch picker where the account with the shortest weekly runway was incorrectly preferred over the one with the most remaining capacity.

  • New --auto flag: parsed in switch.zig, dispatches to handleSwitchAuto which reuses the same load/activate/save/print pipeline as the query path; correctly rejected in combination with --live or any explicit target.
  • Candidate selection (account_ops.zig): conservative eligibility (unknown usage → ineligible), tiebreaks on earliest 5h reset then longest weekly runway; accounts with API refresh errors are excluded via usage_overrides.
  • Tiebreaker bugfix (picker_auto.zig): candidate_weekly_reset < best_weekly_reset> best_weekly_reset, making the live picker consistent with the new --auto path; covered by a new unit test.

Confidence Score: 5/5

Safe to merge — the new auto-switch path is additive, well-tested, and the tiebreaker fix in the live picker is straightforward and verified by a new unit test.

All changed code paths are exercised by new unit, behavior, and integration tests. The core activate/save/print pipeline is the same pattern already used by the query path. The tiebreaker fix in picker_auto.zig is a one-line correction from < to > with a dedicated test confirming the new behaviour. No existing functionality is altered beyond the error message string that now includes --auto.

No files require special attention. The minor eligibility asymmetry between picker_auto.zig and account_ops.zig (unknown usage treated differently) is worth confirming is intentional but does not affect correctness.

Important Files Changed

Filename Overview
src/cli/picker_auto.zig Fixes the pre-existing tiebreaker regression: weekly-reset comparison changed from < to > so the account with the longer weekly runway wins ties. Now consistent with the new autoSwitchAccountIsBetter in account_ops.
src/registry/account_ops.zig Adds selectAutoSwitchAccountIndexByLimits* family; eligibility conservatively requires both 5h and weekly remaining > 0 (unknown usage → ineligible). Tiebreaker prefers earliest 5h reset, then longest weekly runway.
src/workflows/switch.zig Adds handleSwitchAuto; correctly dispatches after .previous/.query early-returns; shares the same load/activate/save/print pattern as the query path. refresh_error_name is freed but not surfaced to the user.
src/cli/commands/switch.zig Parser correctly handles all invalid --auto combinations: duplicate flag, --auto --live, and --auto with any explicit target (query or -). No allocation leaks in error paths.
tests/cli_integration_test.zig Adds a full integration scenario for switch --auto --skip-api with an eligible backup account; validates auth.json is overwritten, registry active key is updated, and previous key is recorded.
tests/workflows_core_test.zig Adds two unit tests covering candidate selection: exhausted-5h exclusion, weekly-exhausted exclusion, and the tiebreaker for accounts whose 5h windows have already reset.
src/cli/types.zig Adds auto: bool = false field to SwitchOptions. Minimal, correct change.
src/cli/output.zig Adds printNoAutoSwitchCandidateError; follows existing stderr/color conventions and includes a useful hint to run list --api.
src/workflows/preflight.zig Registers error.NoAutoSwitchCandidate as a handled CLI error so the process exits cleanly without a stack trace.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[codex-auth switch --auto] --> B{Parse flags}
    B -- "--auto + --live" --> E1[UsageError: cannot combine]
    B -- "--auto + query/dash" --> E2[UsageError: does not support --auto]
    B -- valid --> C{api_mode?}
    C -- skip_api --> D1[loadStoredSwitchSelectionDisplay]
    C -- default/force_api --> D2[loadSwitchSelectionDisplay foreground API refresh]
    D1 --> F[selectAutoSwitchAccountIndexByLimitsWithUsageOverrides]
    D2 --> F
    F -- skip active account --> G{eligible accounts?}
    G -- account has usage override/error --> H[skip]
    G -- rem_5h == 0 or rem_week == 0 --> H
    G -- both > 0 --> I[compare: earliest 5h reset tiebreak: longest weekly runway]
    I --> J{any candidate found?}
    J -- no --> E3[printNoAutoSwitchCandidateError error.NoAutoSwitchCandidate]
    J -- yes --> K[activateAccountByKey]
    K --> L[saveRegistry]
    L --> M[printSwitchedAccount]
    M --> N[exit 0]
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
flowchart TD
    A[codex-auth switch --auto] --> B{Parse flags}
    B -- "--auto + --live" --> E1[UsageError: cannot combine]
    B -- "--auto + query/dash" --> E2[UsageError: does not support --auto]
    B -- valid --> C{api_mode?}
    C -- skip_api --> D1[loadStoredSwitchSelectionDisplay]
    C -- default/force_api --> D2[loadSwitchSelectionDisplay foreground API refresh]
    D1 --> F[selectAutoSwitchAccountIndexByLimitsWithUsageOverrides]
    D2 --> F
    F -- skip active account --> G{eligible accounts?}
    G -- account has usage override/error --> H[skip]
    G -- rem_5h == 0 or rem_week == 0 --> H
    G -- both > 0 --> I[compare: earliest 5h reset tiebreak: longest weekly runway]
    I --> J{any candidate found?}
    J -- no --> E3[printNoAutoSwitchCandidateError error.NoAutoSwitchCandidate]
    J -- yes --> K[activateAccountByKey]
    K --> L[saveRegistry]
    L --> M[printSwitchedAccount]
    M --> N[exit 0]
Loading

Reviews (2): Last reviewed commit: "fix(auto-switch): prefer longer weekly r..." | Re-trigger Greptile

Comment thread src/registry/account_ops.zig
Comment thread tests/cli_integration_test.zig
…test

Address Greptile review on PR Loongphy#148:
- Flip weekly-reset tiebreaker to prefer the later reset (more remaining
  weekly runway) in both account_ops and picker_auto selection paths.
- Add unit test locking the tiebreaker direction and a shell-level
  integration test covering the full `switch --auto` activate/save/print path.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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: one-shot automatic account switch

1 participant