Skip to content

fix: restart the Codex app-server daemon after a credential switch - #105

Merged
xjoker merged 1 commit into
xjoker:devfrom
newbdez33:newbdez33/codex-switch-app-server
Sep 30, 2026
Merged

xjoker merged 1 commit into
xjoker:devfrom
newbdez33:newbdez33/codex-switch-app-server

Conversation

@newbdez33

Copy link
Copy Markdown
Contributor

Problem

Codex CLI 0.157 (released 2026-09-25, "automatic background-server startup for eligible interactive sessions") attaches every interactive codex session to a shared local app-server daemon (codex app-server --listen unix:// --managed-daemon). That daemon builds one AuthManager at startup and caches auth.json in memory. Nothing watches the file; reload() runs only after an in-app login, and the 401 recovery path reloads from disk only when the account id matches (reload_if_account_id_matches in codex-rs/login/src/auth/manager.rs).

So after codex-switch use other, the live auth.json changes but every new codex session still runs on the previous account until the daemon is restarted. codex-switch launch is broken the same way: the launched session joins the daemon and never reads the staged file. codex exec and codex --no-daemon run in process and are unaffected.

Behavior

  • use <alias>, use (auto-select), the TUI u switch, and the login paths that activate credentials (login, login <alias> re-auth when that profile is current, TUI add/re-login) now run codex app-server daemon restart after the live file is written.
  • The restart only happens when codex app-server daemon version reports {"status":"running"}. A stopped daemon or a Codex without the daemon subcommand is left alone, because restart would otherwise start a daemon nobody asked for.
  • The live file is hashed before and after the write; an identical file (re-selecting the profile that is already live, e.g. a scheduled use) skips the restart, so sessions are not interrupted on every run.
  • A refused restart is a warning on stderr with the manual command; the exit status stays 0 because the switch itself has happened. --json stdout is unchanged (the message goes to stderr).
  • Sessions attached to the daemon reconnect to it on the new account (Codex's TUI reconnect rejoins existing threads); a turn that was in progress is interrupted. This is stated in the message and the docs.
  • launch keeps its temporary staging instead of touching the daemon: a ChatGPT launch prepends --no-daemon when codex --help lists it (0.156+). An argv that already contains --no-daemon, --remote, or agents is passed through unchanged. Provider launches are untouched.

Boundary and trade-offs

  • New module src/app_server.rs owns the probe/restart pair; profile.rs stays free of process spawning. The command and TUI layers call it after each live-auth write. replace_profile_auth_and_live_if_current now returns whether the live file was replaced so re-login only restarts when it actually changed the live account.
  • Restart, not RPC: the app-server protocol has no "reload credentials" request, so a restart is the only way to make the daemon read the new file.
  • A ChatGPT launch now spawns codex --help once before staging (support probe). Launched sessions run embedded, so they do not appear in codex agents.
  • Not addressed: provider launches use an isolated CODEX_HOME, where a 0.157+ Codex may auto-start its own managed daemon per run home. Separate concern; noted for follow-up.

Tests

Failing before the fix, passing after:

  • tests/test_launch_passthrough.rs (fake codex on PATH records argv): use_restarts_a_running_app_server_daemon, use_leaves_a_stopped_app_server_daemon_alone, use_does_not_restart_the_daemon_when_the_live_auth_is_unchanged, use_reports_a_failed_daemon_restart_without_failing_the_switch, json_use_keeps_the_daemon_report_off_stdout, launch_chatgpt_runs_codex_without_the_shared_daemon_when_supported, launch_chatgpt_keeps_argv_for_a_codex_without_no_daemon, launch_chatgpt_leaves_an_explicit_server_choice_alone.
  • Unit: src/app_server.rs (scripted version/restart outputs, exit codes, spawn errors, messages, unchanged-file gate) and src/launch.rs (embedded_codex_argv).

Verification run (macOS, Rust 1.97.0)

cargo fmt --check
cargo clippy --all-targets -- -D warnings -A clippy::question_mark
cargo test --all        # 625 unit + 161 integration, all pass
bash -n scripts/install.sh
  • cargo clippy --all-targets -- -D warnings fails on current dev before this change: clippy::question_mark at src/tui/app.rs:913 (provider launch picker) under Rust 1.97. Not touched here.
  • cargo audit is not installed on this machine; not run.
  • Not verified: Linux and Windows jobs (CI), a real OAuth login path, and a real restart against a live account. The detection half was verified on a machine running Codex CLI 0.158.0 with a managed 0.159.0 daemon (codex app-server daemon version → {"status":"running",…}; the TUI processes hold sockets to the daemon and the daemon holds their rollout files), and codex --no-daemon exec … / codex --no-daemon resume … parse while codex exec --no-daemon does not, which is why the flag is prepended.

Docs

Feature-Guide, Command-Reference (automation contract), Configuration unchanged, FAQ, Getting-Started, Troubleshooting (new section with the manual command), Architecture-Overview, Chinese-Guide, and CHANGELOG (Unreleased).

Codex CLI 0.157 (2026-09-25) attaches interactive sessions to a shared
local app-server daemon. The daemon loads auth.json once and re-reads it
only for the account it already holds (the 401 recovery reloads on a
matching account id and nothing watches the file), so `use` and `login`
replaced the file while every new `codex` session kept the previous
account.

`use`, the TUI switch, and the login paths that activate credentials now
run `codex app-server daemon restart` when the live file changed and
`codex app-server daemon version` reports a running daemon. A stopped
daemon or an older Codex is left alone, a failed restart is a warning
because the switch itself has happened, and re-selecting the profile
that is already live skips the restart so a scheduled `use` does not
interrupt sessions on every run.

`launch` keeps its temporary staging: a ChatGPT launch adds `--no-daemon`
when the installed Codex lists it, so the launched session reads the
staged file instead of joining the daemon. An argv that already picks
`--no-daemon`, `--remote`, or `agents` is passed through unchanged.
xjoker added a commit that referenced this pull request Sep 30, 2026
fix: restart the Codex app-server daemon after a credential switch
@xjoker
xjoker merged commit 7e57076 into xjoker:dev Sep 30, 2026
@xjoker

xjoker commented Sep 30, 2026

Copy link
Copy Markdown
Owner

Thanks for the thorough fix and write-up! Merged into dev (merge commit 803dca6). I resolved conflicts in the fake codex test script (kept the new probe handling plus the completion marker already on dev), CHANGELOG, Command-Reference, and Chinese-Guide.

I added a follow-up on top (815a2e8):

  • Timeout: each codex app-server daemon version/restart call is now bounded to 15 seconds. A daemon that stops answering is reported as a failed restart with the manual command, instead of hanging use or the TUI switch.
  • Opt-out: the new use.restart_app_server setting (default true, also toggleable in the TUI Settings tab). With false, the daemon is left alone and a note shows codex app-server daemon restart. This addresses the concern in Profile switch succeeds but new Codex CLI keeps the previous account via shared daemon (0.157) #104 that a restart interrupts turns in progress in other sessions.

Full cargo fmt --check, cargo clippy --all-targets -- -D warnings, and cargo test --all pass on Windows. It will ship in the next release.

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.

2 participants