Files
magpie/internal/agent/codex_openai_auth_test.go
T
yetone 72ca3d593b codex: magpie as Codex's provider keeps the Codex app's ChatGPT account while it has room, so its Work chats and projects stay
Qyburn3 on X: with magpie taking Codex over, the Codex app's "Work"
sessions don't load while the model still answers; turned off, they
come back.

Cause: [model_providers.magpie] had no requires_openai_auth, so Codex's
app-server answers account/read and getAuthStatus with no account and no
token whenever magpie is Codex's provider (magpie API, or the codex.out
takeover). ChatGPT.app 26.1021 then switches to an identity of no
account: onboarding again with no name, ChatGPT mode's projects and
chats ("Work") empty, no avatar, no Chrome control (#1824).

Now putProvider writes requires_openai_auth = true while Codex has a
ChatGPT sign-in with room (openaiAuth: codexChatGPTKnown and not
codexUsedUp), and takes it out otherwise:
- API key alone: left out. With it Codex drops image_gen (captured: 11
  tools without, 10 with), and there is no account to keep.
- No sign-in: left out (the app would ask for a sign-in).
- A sign-in the Codex app holds (codex.out, or magpie API while used
  up): left out, since the app's composer gate (authMethod chatgpt +
  allowed false, #540) would grey Send for magpie's models again. The
  takeover's note now names "its ChatGPT chats and projects" among what
  is off till the reset, beside Keep on ChatGPT.
- An unknown sign-in read leaves the table as it is.
Sync rewrites the key on any table that disagrees (authStale), an older
magpie's included, and the key joins codexAppRoute, so the Codex app's
app-server is restarted onto its account with nothing for the user to
do. The magpie API note no longer says Chrome control needs ChatGPT (en,
zh, zh-TW, ja, de); agent-wiring.md says why.

Real app (mote, ChatGPT.app 26.1021 with its codex 0.164.0-alpha.11,
sandbox CODEX_HOME, copied real ChatGPT auth, auth.json unchanged after):
- native config: the account's projects and recent chats listed.
- magpie table without the key: onboarding, "ChatGPT Work" with no
  projects, no chats. app-server: account null, authMethod null.
- magpie table with requires_openai_auth = true (the keys this commit
  writes): every project and chat back; account/read chatgpt pro,
  getAuthStatus authMethod chatgpt with a token.
- Model requests to a capturing server, with and without the key:
  Bearer is magpie's experimental_bearer_token, no ChatGPT-Account-ID,
  15 tools both ways with a ChatGPT sign-in (image_gen kept).

Tests: TestCodexMagpieProviderKeepsChatGPTAccount (ChatGPT + magpie API,
used up and back; taken over and the reset; an older magpie's table from
live config bytes; API key and no sign-in, fresh and after ChatGPT) and
TestCodexAppRouteFollowsOpenAIAuth. Broken by hand: on the old codex.go
5 of 7 subtests red; without the Sync re-derive 3 red; with the key kept
while used up 2 red; agents.go without the route key, the route test red.

Run (mote-run, at this tree on origin/main 157d575c3; the build and the
agent run again after the rebase onto e5e2aa58e, ok; 5a85a9836 after it touches redact only,
no agent
or GUI file, ok): gofmt -l clean;
go build ./...; go vet linux (-tags nogui) and windows ./..., darwin
./internal/agent; go test -tags nogui -run 'Codex|Agents|Sync|Disconnect|Route'
./internal/agent ok; GUI codex-signin-always, codex-app-out, i18n-keys
and the i18n files, Chromium and WebKit, 96/96.
Not run before the push: the full go test ./... (mote had three other
sessions' full suites at load ~100); it runs after, and any red gets an
issue. Not tried: the used-up case on the real app with the key (the
composer gate is from #540's reading of 26.1013), Windows and Linux
Codex apps.
2026-10-29 01:18:59 +08:00

140 lines
5.1 KiB
Go

package agent
import (
"strings"
"testing"
)
// Qyburn3 on X: with magpie as Codex's provider for a Codex signed in to
// ChatGPT, the Codex app (ChatGPT.app 26.1021) lost its ChatGPT account:
// app-server's account/read and getAuthStatus answer no account and no
// token for a provider without requires_openai_auth, and the app's
// ChatGPT mode showed no chats or projects ("Work" sessions gone) while
// magpie's models still answered. The table says requires_openai_auth
// while that sign-in has room, and not for one the Codex app holds (Send
// would grey again, #540), an API key (its image tool), or no sign-in (a
// login screen). Sync brings a table an older magpie wrote up to date,
// both ways.
func TestCodexMagpieProviderKeepsChatGPTAccount(t *testing.T) {
// the table as an older magpie wrote it, bytes from a live config.toml
const older = "model = \"fake/m1\"\nmodel_provider = \"magpie\"\nmodel_catalog_json = \"magpie-models.json\"\n\n" +
"[model_providers.magpie]\nname = \"magpie\"\nbase_url = \"http://127.0.0.1:3425/v1\"\nwire_api = \"responses\"\n" +
"experimental_bearer_token = \"magpie\"\nhttp_headers = { \"x-openai-actor-authorization\" = \"magpie\" }\n"
flag := func(t *testing.T, cfg string) string {
t.Helper()
i := strings.Index(cfg, "[model_providers.magpie]")
if i < 0 {
t.Fatalf("no magpie provider table:\n%s", cfg)
}
rest := cfg[i+1:]
if j := strings.Index(rest, "\n["); j >= 0 {
rest = rest[:j]
}
for _, l := range strings.Split(rest, "\n") {
if k, v, ok := strings.Cut(l, "="); ok && strings.TrimSpace(k) == "requires_openai_auth" {
return strings.TrimSpace(v)
}
}
return ""
}
t.Run("ChatGPT, magpie API", func(t *testing.T) {
home, read := codexHome(t, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`, "")
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)
}
cfg := read()
if !strings.Contains(cfg, `model_provider = "magpie"`) || flag(t, cfg) != "true" {
t.Fatalf("magpie its provider beside a ChatGPT sign-in with room: requires_openai_auth wanted:\n%s", cfg)
}
if !strings.Contains(cfg, `"x-openai-actor-authorization" = "magpie"`) || !strings.Contains(cfg, "experimental_bearer_token") {
t.Fatalf("magpie's key and actor header kept:\n%s", cfg)
}
// the account runs out: the takeover's way, no account in the app
codexUsedUp = func() bool { return true }
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if cfg := read(); flag(t, cfg) != "" {
t.Fatalf("used up: no requires_openai_auth, or Send greys (#540):\n%s", cfg)
}
codexUsedUp = func() bool { return false }
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if cfg := read(); flag(t, cfg) != "true" {
t.Fatalf("room again: requires_openai_auth back:\n%s", cfg)
}
if c := cx.Check(); c != "" {
t.Fatal(c)
}
})
t.Run("ChatGPT, taken over", func(t *testing.T) {
home, read := codexHome(t, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`, "")
cx := codex(home)
codexUsedUp = func() bool { return true }
if err := cx.Fields[0].Set("fake/m1"); err != nil {
t.Fatal(err)
}
if cfg := read(); !strings.Contains(cfg, `model_provider = "magpie"`) || flag(t, cfg) != "" {
t.Fatalf("codex.out: magpie its provider with no requires_openai_auth:\n%s", cfg)
}
// the reset: back beside the sign-in, the table kept for the
// threads started on magpie, now keeping the account too
codexUsedUp = func() bool { return false }
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if cfg := read(); strings.Contains(cfg, "model_provider =") || flag(t, cfg) != "true" {
t.Fatalf("after the reset:\n%s", cfg)
}
})
t.Run("older table synced", func(t *testing.T) {
home, read := codexHome(t, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`, older)
cx := codex(home)
if err := cx.Field("login").Set("api"); err != nil {
t.Fatal(err)
}
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if cfg := read(); flag(t, cfg) != "true" {
t.Fatalf("an older magpie's table, synced: requires_openai_auth wanted:\n%s", cfg)
}
})
for _, c := range []struct{ name, auth string }{
{"API key", `{"OPENAI_API_KEY":"sk-relay"}`},
{"no sign-in", ""},
} {
t.Run(c.name, func(t *testing.T) {
home, read := codexHome(t, c.auth, "")
cx := codex(home)
if err := cx.Fields[0].Set("fake/m1"); err != nil {
t.Fatal(err)
}
if cfg := read(); !strings.Contains(cfg, `model_provider = "magpie"`) || flag(t, cfg) != "" {
t.Fatalf("no ChatGPT account: no requires_openai_auth:\n%s", cfg)
}
})
// one written while signed in, the sign-in gone since
t.Run(c.name+" after ChatGPT", func(t *testing.T) {
withFlag := strings.Replace(older, "wire_api = \"responses\"\n", "wire_api = \"responses\"\nrequires_openai_auth = true\n", 1)
home, read := codexHome(t, c.auth, withFlag)
cx := codex(home)
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if cfg := read(); flag(t, cfg) != "" {
t.Fatalf("signed out: requires_openai_auth goes:\n%s", cfg)
}
})
}
}