Skip to content

feat: add previous account switch - #133

Merged
Loongphy merged 4 commits into
mainfrom
feat/previous-account-switch
Jun 1, 2026
Merged

feat: add previous account switch#133
Loongphy merged 4 commits into
mainfrom
feat/previous-account-switch

Conversation

@Loongphy

@Loongphy Loongphy commented Jun 1, 2026

Copy link
Copy Markdown
Owner

Summary

  • Add cd - style previous-account switching via codex-auth - and codex-auth switch -.
  • Persist previous_active_account_key in registry schema 4 and normalize schema 4 files missing it.
  • Preserve previous-account state during same-account switches and active-account removal replacement; clear it only when the recorded previous account is removed.

Validation

  • HOME=/tmp/previous-account-switch zig build run -- list
  • HOME=/tmp/previous-account-switch zig build test
  • git diff --check

Risks

  • Registry persistence now writes previous_active_account_key while staying on schema_version = 4 because this schema has not shipped yet.
  • Previous switch depends on recorded account keys; it fails without mutation when the recorded previous account is missing.

PR Loop Status

  • Latest push creates the feature branch and initial PR.
  • CI status pending.
  • No review comments have been addressed yet.
  • No comments intentionally left unresolved.

@pkg-pr-new

pkg-pr-new Bot commented Jun 1, 2026

Copy link
Copy Markdown

Open in StackBlitz

@loongphy/codex-auth-darwin-arm64

npx https://pkg.pr.new/@loongphy/codex-auth-darwin-arm64@7079dcc

@loongphy/codex-auth-darwin-x64

npx https://pkg.pr.new/@loongphy/codex-auth-darwin-x64@7079dcc

@loongphy/codex-auth-linux-arm64

npx https://pkg.pr.new/@loongphy/codex-auth-linux-arm64@7079dcc

@loongphy/codex-auth-linux-x64

npx https://pkg.pr.new/@loongphy/codex-auth-linux-x64@7079dcc

@loongphy/codex-auth-win32-arm64

npx https://pkg.pr.new/@loongphy/codex-auth-win32-arm64@7079dcc

@loongphy/codex-auth-win32-x64

npx https://pkg.pr.new/@loongphy/codex-auth-win32-x64@7079dcc

@loongphy/codex-auth

npx https://pkg.pr.new/@loongphy/codex-auth@7079dcc

commit: 7079dcc

@greptile-apps

greptile-apps Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds cd - style previous-account switching via codex-auth - and codex-auth switch -, backed by a new previous_active_account_key field persisted in the schema-4 registry. The implementation centralises state updates through a new setActiveAccountKey overload and a sibling setActiveAccountKeyPreservingPrevious function used in drift-correction and removal-replacement paths so that automatic registry syncs never clobber the user's explicitly recorded previous account.

  • New SwitchTarget union replaces the flat query: ?[]u8 field in SwitchOptions, and the parser cleanly maps lone - at both the top-level and switch sub-command to .previous, rejecting flag combinations at parse time.
  • Registry persistence adds previous_active_account_key to schema 4 (no version bump), and currentLayoutNeedsRewrite triggers a one-time normalisation rewrite for older schema-4 files that pre-date this field.
  • Remove paths are updated to preserve the previous pointer when the removed account is not the recorded previous, and to clear it explicitly when it is.

Confidence Score: 5/5

Safe to merge — all three changed code paths (explicit switch, auto-replacement after remove, drift correction) consistently use the correct setter variant, and the one-time normalisation rewrite for existing schema-4 files terminates correctly after the field is written.

The centralisation of previous-account tracking in setActiveAccountKey is correct: same-account activations are a no-op for the previous field, drift-correction calls the Preserving variant so user-initiated history is never silently overwritten, and the removal path explicitly clears previous only when the removed account matches it. Memory ownership in handleSwitchPrevious is sound — the key is duped before activateAccountByKey frees the original through setActiveAccountKey. The schema normalisation trigger correctly distinguishes Zig null (key absent) from JSON null (key present-but-null), so subsequent loads do not re-trigger the rewrite. Test coverage is thorough, including the active==previous edge case after removal.

No files require special attention.

Important Files Changed

Filename Overview
src/workflows/switch.zig Refactored handleSwitch into three sub-functions (query/previous/picker); new handleSwitchPrevious correctly dupes the previous key before passing it to activateAccountByKey to avoid use-after-free.
src/registry/account_ops.zig New setActiveAccountKey updates previous_active_account_key on every intentional switch; setActiveAccountKeyPreservingPrevious retained for drift-correction and removal-replacement paths; removeAccounts correctly clears previous only when the removed account matches it.
src/registry/storage.zig currentLayoutNeedsRewrite correctly triggers a one-time normalisation rewrite for schema-4 files missing previous_active_account_key; JSON null vs Zig-null semantics handled correctly so the rewrite does not loop.
src/cli/commands/switch.zig SwitchTarget union cleanly replaces the optional query field; lone - distinguished from flag prefixes; end-of-parse check rejects combinations with --live/--api/--skip-api.
src/workflows/active_auth.zig reconcileActiveAuthAfterRemove now calls the Preserving variants so the user's recorded previous account is not overwritten by auto-replacement.
tests/cli_integration_test.zig Comprehensive integration tests cover round-trip alternation, no-previous failure, missing-previous-account failure, active-removal replacement preserving previous, and active==previous edge case.
tests/registry_test.zig Unit tests cover save/load round-trip, same-account idempotency, schema-4 normalisation rewrite, drift-correction preserving previous, and remove-clears-previous-key.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["CLI args parsed"] --> B{"opts.target"}
    B -- ".query" --> C["handleSwitchQuery"]
    B -- ".previous" --> D["handleSwitchPrevious"]
    B -- ".picker" --> E["picker / live flow"]
    D --> F{"previous_active_account_key?"}
    F -- "null" --> G["error: no previous account"]
    F -- "has key" --> H{"key in accounts list?"}
    H -- "no" --> I["error: previous account unavailable"]
    H -- "yes" --> J{"active == previous?"}
    J -- "yes" --> G
    J -- "no" --> K["activateAccountByKey -> setActiveAccountKey"]
    K --> L["saveRegistry -> printSwitchedAccount"]
Loading

Reviews (3): Last reviewed commit: "fix: reject previous switch to active ac..." | Re-trigger Greptile

Comment thread src/workflows/switch.zig Outdated
@Loongphy
Loongphy merged commit 2ef8259 into main Jun 1, 2026
13 checks passed
@Loongphy
Loongphy deleted the feat/previous-account-switch branch June 1, 2026 08:10
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.

1 participant