Files
magpie/internal/agent/codex_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

89 lines
3.3 KiB
Go

package agent
import (
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/edit"
)
// codexNoAccount says Codex's provider takes no ChatGPT account, so an
// account switch restarts nothing (danzi002 on Discord), only for a
// provider table without requires_openai_auth = true: magpie API beside a
// ChatGPT sign-in with room has magpie write true, and the Codex app keeps
// that account, so a switch there still restarts it. The user's own false
// stays through Sync and counts.
func TestCodexNoAccountFollowsRequiresOpenAIAuth(t *testing.T) {
home, read := codexHome(t, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`, "")
path := filepath.Join(home, ".codex", "config.toml")
if codexNoAccount() {
t.Fatalf("Codex on its own ChatGPT sign-in takes no account:\n%s", read())
}
cx := codex(home)
if err := cx.Field("login").Set("api"); err != nil {
t.Fatal(err)
}
if err := cx.Fields[0].Set("fake/m1"); err != nil {
t.Fatal(err)
}
if codexNoAccount() {
t.Fatalf("magpie API with requires_openai_auth = true takes no account:\n%s", read())
}
if err := edit.SetTOMLKey(path, "model_providers."+magpieID, "requires_openai_auth", false); err != nil {
t.Fatal(err)
}
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if !codexNoAccount() {
t.Fatalf("the user's requires_openai_auth = false takes an account:\n%s", read())
}
// a profile may name another provider: not known, so not none
if err := edit.SetTOMLTop(path, edit.KV{Path: "profile", Value: "work"}); err != nil {
t.Fatal(err)
}
if codexNoAccount() {
t.Fatalf("with a profile:\n%s", read())
}
}
// The provider's table decides it, whoever's it is; the built-in openai
// provider, one with no table and a config that can't be read don't
// count.
func TestCodexNoAccountOfEachConfig(t *testing.T) {
for _, c := range []struct {
name, config string
want bool
}{
{"none", "", false},
{"openai", "model_provider = \"openai\"\n", false},
{"no table", "model_provider = \"ollama\"\n", false},
{"magpie false", "model_provider = \"magpie\"\n[model_providers.magpie]\nbase_url = \"http://127.0.0.1:3425/v1\"\nrequires_openai_auth = false\n", true},
{"magpie unset", "model_provider = \"magpie\"\n[model_providers.magpie]\nbase_url = \"http://127.0.0.1:3425/v1\"\n", true},
{"magpie true", "model_provider = \"magpie\"\n[model_providers.magpie]\nbase_url = \"http://127.0.0.1:3425/v1\"\nrequires_openai_auth = true\n", false},
{"theirs false", "model_provider = \"relay\"\n[model_providers.relay]\nbase_url = \"https://relay.example/v1\"\nrequires_openai_auth = false\n", true},
{"table only", "[model_providers.magpie]\nrequires_openai_auth = false\n", false},
} {
t.Run(c.name, func(t *testing.T) {
codexHome(t, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`, c.config)
if got := codexNoAccount(); got != c.want {
t.Fatalf("got %v, want %v", got, c.want)
}
})
}
t.Run("unreadable", func(t *testing.T) {
home, _ := codexHome(t, "", "model_provider = \"magpie\"\n[model_providers.magpie]\nrequires_openai_auth = false\n")
path := filepath.Join(home, ".codex", "config.toml")
if err := os.Remove(path); err != nil {
t.Fatal(err)
}
if err := os.Mkdir(path, 0o700); err != nil {
t.Fatal(err)
}
if codexNoAccount() {
t.Fatal("a config that can't be read takes no account")
}
})
}