Skip to content

fix(codex): keep the user's API-key login when switching to a third-party provider - #577

Open
jayhemnani9910 wants to merge 1 commit into
SaladDay:mainfrom
jayhemnani9910:fix/codex-api-key-login-not-residue
Open

jayhemnani9910 wants to merge 1 commit into
SaladDay:mainfrom
jayhemnani9910:fix/codex-api-key-login-not-residue

Conversation

@jayhemnani9910

Copy link
Copy Markdown

Summary

A Codex user who logged in with codex login --with-api-key loses that login the first time they switch to a third-party provider:

~/.codex/auth.json   {"OPENAI_API_KEY":"sk-..."}
cc-switch --app codex provider list      # imports the live setup as "default"
cc-switch --app codex provider add --name Relay --base-url https://relay.example.com/v1 --api-key sk-relay --model gpt-relay
cc-switch --app codex use relay          # auth.json deleted, codex-login-stash.json stays {"logins": {}}
cc-switch --app codex use codex-official # auth.json still missing

The same thing happens with "Preserve Codex official login" turned on. A ChatGPT OAuth login in the same steps is stashed and restored as expected.

The imported default row has category custom, so prepare puts its key into the list of third-party keys. is_residue then sees the live auth.json as a key that an older version wrote for a relay. It is left out of the stash and deleted, even with preserve on. The comment on is_residue already says a key the user logged in with themselves must not count.

This change only counts a row's key when the row's config sends traffic somewhere other than built-in OpenAI: a model_provider other than openai, openai_base_url or base_url, a [model_providers.openai] override, a profile that sets one of those, or a config that does not parse. A login with no config.toml, with only settings like model = ... and [profiles.*], with model_provider = "openai", or with an unused [model_providers.*] table is now stashed and restored like an OAuth login. Keys that older versions wrote for real relay rows are still cleaned up.

Not changed: switching back to the default card itself (not an official card) still leaves auth.json empty, as before, because a non-official target never restores from the stash. Upstream cc-switch builds the residue list the same way (codex_direct.rs).

Testing

  • provider_service_switch_codex_stashes_user_api_key_login_from_imported_default: default, relay, official for five config shapes; auth.json is back with the user's key. Fails on main.
  • provider_service_switch_codex_preserve_keeps_user_api_key_login_from_imported_default: with preserve on, auth.json is untouched after the relay switch. Fails on main.
  • provider_service_switch_codex_official_clears_legacy_relay_key: a relay key left in auth.json by an older version is still removed on the switch to official, for five relay config shapes. Passes before and after.
  • cargo fmt --check and cargo clippy --all-targets show nothing new. The full cargo test run has the same failures with and without the change (permission checks on this machine's temp dirs, e.g. database init and managed config dir tests).

@jayhemnani9910
jayhemnani9910 force-pushed the fix/codex-api-key-login-not-residue branch from ee2e309 to 762dd0a Compare November 25, 2026 05:51
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