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