Files
magpie/internal/provider/codex_app_noaccount_test.go
T
yetone d99e213f5b codex: an account switch restarts nothing while Codex's provider takes no ChatGPT account
danzi002 on Discord: with requires_openai_auth = false on Codex's provider,
switching the Codex account in magpie still restarted the Codex app's
app-server and said the app and daemon were left on the old account.

Codex's provider with no requires_openai_auth = true goes by no ChatGPT
account: codex-rs ModelProviderInfo::account_state answers no account, and
resolve_provider_auth sends the table's key, never auth.json. So which
account auth.json holds is nothing a running Codex uses.

- provider.CodexNoAccount (set to agent.codexNoAccount): the config's top
  model_provider is not openai, has a [model_providers] table, and the table
  doesn't say requires_openai_auth = true. A profile, the built-in provider,
  no table or an unreadable config count as false (may use the account).
- codexAppStep forgets a switch (and a spent switch) while it holds, so the
  app-server is left running; a route change still restarts it.
- CodexAppStale / CodexDaemonStale say nothing, and `magpie accounts`'
  switch line says nothing needs restarting.
- Kept: route-change restarts (model_provider, openai_base_url,
  model_catalog_json, base_url, catalog hash: read only as the app-server
  starts) and the daemon's model-list restarts; and switch restarts when
  magpie writes requires_openai_auth = true (ChatGPT sign-in with room).

Run (mote-run; full suite at this change on e0ad48641):
- go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet
  ./..., go build for darwin/linux(nogui)/windows: ok.
- go test -tags nogui -timeout 45m ./...: 81 packages ok, no FAIL.
- New tests: TestCodexNoAccountSwitchRestartsNothing (provider),
  TestCodexNoAccountFollowsRequiresOpenAIAuth and
  TestCodexNoAccountOfEachConfig (agent).
- Broken by hand: codexNoAccount returning false in provider made
  TestCodexNoAccountSwitchRestartsNothing red (ended the app-servers);
  agent's codexNoAccount returning false made both agent tests red.
- Real codex-cli 0.165.0 in a sandbox CODEX_HOME with a provider table at
  requires_openai_auth = false: app-server account/read and getAuthStatus
  answered no account before and after auth.json was switched from a@x to
  b@x, and `codex exec` against a loopback capture server sent
  `Authorization: Bearer <the table's key>` with no chatgpt-account-id for
  both accounts.

After rebasing onto origin/main (2 commits: gateway desktop probe,
usage purposes): go vet ./... and go test of internal/agent, provider,
cli, usage and gateway at the pushed head: ok.

Not tried: the real Codex desktop app (macOS or the Windows MSIX) through
a switch; a daemon with live sessions through a switch; the GUI suite
(no GUI asset changed; the switch notice reads CodexAppStale).
2026-10-30 21:01:09 +08:00

88 lines
3.0 KiB
Go

package provider
import (
"context"
"slices"
"testing"
"time"
"github.com/yetone/magpie/internal/proc"
)
// With Codex on a provider that takes no ChatGPT account (magpie's table
// with requires_openai_auth = false, danzi002 on Discord: "requires_openai_auth
// = false 的时候就不应该重启 codex 了"), the app-server sends the
// provider's own key and reads no account at all, so switching the account
// in auth.json changes nothing it goes by: neither a switch nor a switch
// off a spent account restarts the Codex app's app-server, and neither the
// app nor the background daemon is said to be left on the old account. A
// route change still restarts it, and with the provider taking the account
// again a switch does too.
func TestCodexNoAccountSwitchRestartsNothing(t *testing.T) {
signIn(t)
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", t.TempDir())
s := stubCodexApp(t)
old, oldHome, oldNo := listProcesses, processCodexHome, CodexNoAccount
t.Cleanup(func() {
listProcesses, processCodexHome, CodexNoAccount = old, oldHome, oldNo
DismissCodexDaemon()
DismissCodexApp()
})
processCodexHome = func(int) (string, bool) { return "", false }
listProcesses = func(context.Context) ([]proc.Process, error) {
ps := []proc.Process{{PID: 9, Args: "codex app-server --managed-daemon"}}
for _, p := range s.running {
ps = append(ps, proc.Process{PID: p, Args: codexWindowsAppServer})
}
return ps, nil
}
noAccount := true
CodexNoAccount = func() bool { return noAccount }
now := time.Now()
idle := func() (int, time.Time) { return 0, now.Add(-time.Minute) }
step := func() []int { return codexAppStep(context.Background(), now, idle) }
recheck := func() {
codexDaemonMu.Lock()
codexDaemonChecked = time.Now()
codexDaemonMu.Unlock()
}
s.route, s.running = "magpie,noauth", []int{60}
if got := step(); got != nil {
t.Fatalf("nothing changed: ended %v", got)
}
noteCodexSwitch("a@x", "b@x")
recheck()
for range 3 {
if got := step(); got != nil {
t.Fatalf("switched on a provider that takes no account: ended %v", got)
}
}
if got := CodexAppStale(); got != "" {
t.Fatalf("the Codex app said to be left on %q", got)
}
if got := CodexDaemonStale(); got != "" {
t.Fatalf("the daemon said to be left on %q", got)
}
codexAppSpentSwitch()
if got := step(); got != nil {
t.Fatalf("switched off a spent account on a provider that takes no account: ended %v", got)
}
// where Codex sends its requests changed: restarted as ever
s.route = "magpie,auth"
if got := step(); !slices.Equal(got, []int{60}) {
t.Fatalf("route changed: ended %v, want [60]", got)
}
// on a provider that takes the account a switch restarts it
noAccount = false
noteCodexSwitch("b@x", "c@x")
recheck()
if got := CodexAppStale(); got != "b@x" {
t.Fatalf("on a provider that takes the account: app left on %q, want b@x", got)
}
if got := step(); !slices.Equal(got, []int{100}) {
t.Fatalf("switched on a provider that takes the account: ended %v, want [100]", got)
}
}