From a312bf3e3884a739a8206876bb59fa940a3a65a0 Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 27 Oct 2026 18:03:08 +0800 Subject: [PATCH] codex: switching the ChatGPT account restarts the Codex app's app-server once no turn runs zhibukaixinyao (Discord #help, 10-27) asked when Windows gets the "restart Codex" step: the Windows GUI has no Restart button, so after an account switch the Codex app kept sending on, and showing the limits of, the account it started with until the user quit and reopened it. noteCodexSwitch now hands the app-server pid it recorded as left on the old account to codexApp's stale set, the same set a route change fills. KeepCodexAppOnItsProvider's step ends it once Codex is idle and no turn is open, and the app starts another that reads auth.json afresh. This covers a switch made in the gateway's process: the GUI's switch and SwitchWhenSpent. Together with 3aead9c62 (the Windows app's app-server is found at all) this needs no restart on Windows, macOS or Linux. Run (at 9bfe68048 + this change): - TestCodexAppServerRestartedOnAccountSwitch (new): nothing ended with no switch, nothing while a turn is open, pid ended once idle after the switch and only once, CodexAppStale "" afterwards. - go test -race -count=3 -run TestCodexApp ./internal/provider: ok. - go test ./internal/provider ./internal/agent ./internal/cli (skipping #3632 and #3854's tests): provider ok, cli ok; agent red only on TestFolderVarsAreListed, red on main since e4552cca9 (#3884). - gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows ok. Broken by hand: with the codexAppAccountLeft call removed the new test is red ("switched, idle: ended [], want [60]"). Not tried: a real account switch on the Windows box (the box's Codex app isn't signed in to two ChatGPT accounts). A switch made from the CLI runs in its own process and still only sets the notice; the running gateway doesn't hear of it. --- internal/provider/codex_app_server.go | 20 +++++++++ internal/provider/codex_app_server_test.go | 52 ++++++++++++++++++++++ internal/provider/codex_daemon.go | 3 ++ internal/provider/codex_switch.go | 2 +- 4 files changed, 76 insertions(+), 1 deletion(-) diff --git a/internal/provider/codex_app_server.go b/internal/provider/codex_app_server.go index 631cf87a9..42baeed0c 100644 --- a/internal/provider/codex_app_server.go +++ b/internal/provider/codex_app_server.go @@ -156,6 +156,26 @@ func CodexAppStuck() bool { return c.stuck } +// codexAppAccountLeft has the Codex app's app-server pid, left on the +// ChatGPT account Codex was switched away from (noteCodexSwitch), ended +// as a route change's are, once no Codex turn runs: the app starts +// another, which reads auth.json and the new account afresh, so the app +// shows that account's limits and sends on it with no quit and reopen +// (zhibukaixinyao on Discord: Windows has no Restart button). One +// switched away and back before then is restarted all the same, which +// costs the app nothing it shows. +func codexAppAccountLeft(pid int) { + c := &codexApp + c.Lock() + defer c.Unlock() + if c.stale == nil { + c.stale = map[int]int{} + } + if _, ok := c.stale[pid]; !ok { + c.stale[pid] = 0 + } +} + // KeepCodexAppOnItsProvider runs codexAppStep every codexAppEvery till // ctx ends. busy says how many of Codex's requests are in flight through // magpie and when the last ended. diff --git a/internal/provider/codex_app_server_test.go b/internal/provider/codex_app_server_test.go index 9ef95125c..fdab13466 100644 --- a/internal/provider/codex_app_server_test.go +++ b/internal/provider/codex_app_server_test.go @@ -404,3 +404,55 @@ func TestCodexAppOwnServer(t *testing.T) { } } } + +// Switching Codex's ChatGPT account left the Codex app on the one before +// until it was quit and opened again, and Windows has no Restart button +// for it (zhibukaixinyao on Discord): the app's app-server keeps the +// account it started with. A switch now ends that app-server once no +// Codex turn runs, as a route change does, and the app starts another on +// the new account. +func TestCodexAppServerRestartedOnAccountSwitch(t *testing.T) { + signIn(t) + t.Setenv("HOME", t.TempDir()) + t.Setenv("USERPROFILE", t.TempDir()) + s := stubCodexApp(t) + old := listProcesses + t.Cleanup(func() { + listProcesses = old + DismissCodexDaemon() + DismissCodexApp() + }) + listProcesses = func(context.Context) ([]proc.Process, error) { + var ps []proc.Process + for _, p := range s.running { + ps = append(ps, proc.Process{PID: p, Args: codexWindowsAppServer}) + } + return ps, nil + } + now := time.Now() + idle := func() (int, time.Time) { return 0, now.Add(-time.Minute) } + step := func() []int { return codexAppStep(context.Background(), now, idle) } + + s.running = []int{60} + if got := step(); got != nil { + t.Fatalf("nothing changed: ended %v", got) + } + noteCodexSwitch("a@x", "b@x") + s.turnOpen = true + if got := step(); got != nil { + t.Fatalf("a turn open in Codex's sessions: ended %v", got) + } + s.turnOpen = false + if got := step(); !slices.Equal(got, []int{60}) { + t.Fatalf("switched, idle: ended %v, want [60]", got) + } + if got := step(); got != nil { + t.Fatalf("the new app-server read the new account as it started: ended %v", got) + } + codexDaemonMu.Lock() + codexDaemonChecked = time.Time{} + codexDaemonMu.Unlock() + if got := CodexAppStale(); got != "" { + t.Fatalf("still said to be on %q after its restart", got) + } +} diff --git a/internal/provider/codex_daemon.go b/internal/provider/codex_daemon.go index a7f4b790f..0ea66fe6c 100644 --- a/internal/provider/codex_daemon.go +++ b/internal/provider/codex_daemon.go @@ -177,6 +177,9 @@ func noteCodexSwitch(from, to string) { } noteLeft(&codexDaemonLeft, findCodexDaemon(ps, codexHome()), from, to) noteLeft(&codexAppLeft, findCodexApp(ps), from, to) + if codexAppLeft.pid != 0 { + codexAppAccountLeft(codexAppLeft.pid) + } } // noteLeft updates what is known of the app-server pid, running as Codex's diff --git a/internal/provider/codex_switch.go b/internal/provider/codex_switch.go index 840dfb246..0305a6a53 100644 --- a/internal/provider/codex_switch.go +++ b/internal/provider/codex_switch.go @@ -302,7 +302,7 @@ func SwitchWhenSpent(ctx context.Context, agent string) (string, error) { } if agent == "codex" { if was := CodexAppStale(); was != "" { - log.Printf("codex: the Codex app is still signed in to %s, and shows its limits, until it is quit and opened again", was) + log.Printf("codex: the Codex app is still signed in to %s, and shows its limits, until its app-server is restarted once no Codex turn runs", was) } } // the models the agent is offered are the new account's plan's