Files
magpie/internal/agent/codex_desktop_test.go
T
yetone add927e604 codex: the Codex app beside the ChatGPT sign-in lists magpie's models too
laddieyao (Discord #general, 10-27): with Codex joined to magpie beside
its ChatGPT sign-in, the Codex desktop app's model picker showed OpenAI's
GPT models alone, while the Codex CLI listed magpie's. The /models
request does reach magpie's gateway and its answer holds magpie's
models. ChatGPT.app 26.1013's picker then narrows app-server model/list
to the models a Statsig config (107580212, use_hidden_models /
available_models) names, while Codex is on its built-in provider, unless
Codex reads its list from a file (model_catalog_json,
hasConfiguredModelCatalog) or names a provider of its own. The CLI has
no such filter.

Where the Codex app is installed (codexReaders has an "app"), and only
joined/beside the sign-in (not magpie as Codex's provider, not account
failover alone), magpie writes the list it hands Codex at
/backend-api/codex/models to ~/.codex/magpie-desktop-models.json and
names it with model_catalog_json. Codex is still on its built-in
provider. With a catalog named Codex asks for no list, so magpie asks
the backend itself, with Codex's live sign-in (provider.CodexLiveList):
its entries go in verbatim, base_instructions and all, merged as the
gateway merges them (codexMerged, factored out of codexModels: picked,
hidden, window, V1, AutoReview edits), magpie's models after them.

- A list not known (not asked yet, or the backend failing) is unknown:
  the file as it was stays, never an empty list; a key naming a file
  that is gone goes, as Codex loads no config then.
- Written via temp+rename at Join, set, settle and every Sync (so a
  change of magpie's models, picks or hidden models rewrites it, as
  SyncCatalog runs on them); the backend list is asked again hourly
  (5 min after a failure) and a change syncs again.
- A model_catalog_json of the user's own stands; none is written beside.
- magpie API, the app gone, or unwire take the key and the file out;
  unroute never restores the desk file as the user's catalog.
- connectinfo counts the file, so a running app on an old list reads
  as needing a restart.
- CLI-only machines: unchanged, the live /models path as before.

Run:
- Real account, sandbox HOME (copy of auth.json, chatgpt Pro; live
  ~/.codex untouched): Join wrote model_catalog_json and the file with
  the real backend's 10 entries (gpt-6.1-sol ... codex-auto-review, each
  with base_instructions) plus relay/claude-opus-5.5 and
  relay/claude-sonnet-4-5. Then the real app-server bundled in
  ChatGPT.app (codex 0.163.0-alpha.10.2, CODEX_HOME sandbox, base URL
  pointed at a dead port) model/list returned all 12, relay models
  visible, and config/read showed model_catalog_json set, which is what
  the app's picker checks to skip its filter.
- New tests: TestCodexDesktopListBesideSignIn, ...CLIOnly,
  ...KeepsTheirs (internal/agent), TestCodexDesktopCatalog
  (internal/gateway).
- Broken by hand, each went red: deskList a no-op (all three agent
  tests), the user's-own branch removed (KeepsTheirs), the gone-file
  branch removed (KeepsTheirs), the keep-last-good rule broken in
  codexDeskRefresh (TestCodexDesktopCatalog).
- go test -tags nogui -timeout 45m ./internal/agent ./internal/gateway
  ./internal/provider ./internal/cli at bc0a99bb6 (on 68bbcc01c):
  agent, gateway, cli ok; provider red on TestUnreadLogins...
  (synctest crash, #3632) and three TestFetch* tests (#3854), both red
  the same at origin/main 68bbcc01c / 5a3292354 run alone, both filed.
- Rebased over e7f21dba5 (Disconnect keeps a hand-set catalog): the
  conflict in unroute merged (the desk key counts as no catalog of the
  user's), and -run 'CodexDesktop|Disconnect|Unwire|Catalog'
  ./internal/agent ok. The packages' run at the final head could not
  build: the machine's disk was full (other sessions).
- gofmt -l clean; go vet ./... on darwin, GOOS=linux, GOOS=windows ok.

Not tried: clicking the real desktop app's picker (the app-server
answer and config were checked, not the GUI); the Windows Store app
(its readers get no version but count as the app; not run there); a
backend list change rewriting the file while the app runs (the app
needs reopening to read a new file, which connectinfo's restart notice
covers).
2026-10-27 18:38:27 +08:00

213 lines
7.1 KiB
Go

package agent
import (
"os"
"path/filepath"
"strings"
"testing"
)
// codexAppReads has the Codex app (and a CLI) read every config when app
// is true, the CLI alone when it is false; put back when the test ends.
func codexAppReads(t *testing.T, app *bool) {
t.Helper()
was := codexReaders
t.Cleanup(func() { codexReaders = was })
codexReaders = func(place) []CodexReader {
out := []CodexReader{{Kind: "cli", Version: "0.163.0"}}
if *app {
out = append(out, CodexReader{Kind: "app", Version: "0.163.0"})
}
return out
}
}
// deskListIs has CodexDesktopList answer list, or not know it while list
// is nil.
func deskListIs(t *testing.T, list *string) {
t.Helper()
was := CodexDesktopList
t.Cleanup(func() { CodexDesktopList = was })
CodexDesktopList = func() ([]byte, bool) {
if list == nil || *list == "" {
return nil, false
}
return []byte(*list), true
}
}
// laddieyao on Discord: Codex joined beside its ChatGPT sign-in, the Codex
// app's picker showed GPT alone while the CLI listed magpie's models. The
// app shows only the models of Codex's list a flag of OpenAI's names,
// unless Codex reads its list from a file: where the app is installed,
// magpie writes the list it hands Codex there and names it; the CLI alone
// keeps the list the gateway hands it.
func TestCodexDesktopListBesideSignIn(t *testing.T) {
home, read := codexHome(t, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`, `model = "gpt-5.5"`+"\n")
dir := filepath.Join(home, ".codex")
desk := filepath.Join(dir, "magpie-desktop-models.json")
app := true
codexAppReads(t, &app)
list := `{"models":[{"slug":"gpt-6-sol","base_instructions":"theirs"},{"slug":"fake/m1"}]}`
deskListIs(t, &list)
cx := codex(home)
if ok, err := cx.Join(); err != nil || !ok {
t.Fatalf("join: %v %v", ok, err)
}
cfg := read()
if !strings.Contains(cfg, `model_catalog_json = "`) || !strings.Contains(cfg, "magpie-desktop-models.json") {
t.Fatalf("joined with the Codex app here, no list named for it:\n%s", cfg)
}
if strings.Contains(cfg, "model_provider =") {
t.Fatalf("Codex's provider changed:\n%s", cfg)
}
if b, err := os.ReadFile(desk); err != nil || string(b) != list {
t.Fatalf("the app's list: %q %v", b, err)
}
// connected beside the sign-in, as before: not magpie as its provider
if !cx.Joined() || cx.AppOut() {
t.Fatalf("joined %v, app out %v\n%s", cx.Joined(), cx.AppOut(), cfg)
}
if msg := cx.Check(); msg != "" {
t.Fatalf("check: %s", msg)
}
// magpie's models changed: written again at the next sync
list = `{"models":[{"slug":"gpt-6-sol","base_instructions":"theirs"},{"slug":"fake/m2"}]}`
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if b, _ := os.ReadFile(desk); string(b) != list {
t.Fatalf("not written again: %s", b)
}
// a list not known now (the backend not answering) is unknown: the
// file as it was stays, never an empty one
was := list
list = ""
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if b, _ := os.ReadFile(desk); string(b) != was || !strings.Contains(read(), "magpie-desktop-models.json") {
t.Fatalf("unknown list: %s\n%s", b, read())
}
list = was
// a magpie model picked beside the sign-in: still Codex's provider,
// the list still the app's
if err := cx.Fields[0].Set("fake/m1"); err != nil {
t.Fatal(err)
}
if cfg := read(); !cx.Beside() || !strings.Contains(cfg, "magpie-desktop-models.json") || strings.Contains(cfg, "model_provider =") {
t.Fatalf("beside: %v\n%s", cx.Beside(), cfg)
}
// magpie API: magpie's own catalog, as magpie is Codex's provider, and
// the app's list gone
if err := cx.Field("login").Set("api"); err != nil {
t.Fatal(err)
}
if cfg := read(); !strings.Contains(cfg, `model_provider = "magpie"`) || strings.Contains(cfg, "magpie-desktop-models.json") {
t.Fatalf("api:\n%s", cfg)
}
if _, err := os.Stat(desk); !os.IsNotExist(err) {
t.Fatalf("api: the app's list is still there: %v", err)
}
if err := cx.Field("login").Set(""); err != nil {
t.Fatal(err)
}
if cfg := read(); !strings.Contains(cfg, "magpie-desktop-models.json") || strings.Contains(cfg, "model_provider =") {
t.Fatalf("back beside the sign-in:\n%s", cfg)
}
// disconnected: the key and the file go
if err := cx.Unwire(); err != nil {
t.Fatal(err)
}
if cfg := read(); strings.Contains(cfg, "model_catalog_json") {
t.Fatalf("unwired:\n%s", cfg)
}
if _, err := os.Stat(desk); !os.IsNotExist(err) {
t.Fatalf("unwired: the app's list is still there: %v", err)
}
}
// The CLI alone asks the gateway for its list, which follows each change
// as it is made: no file is written for it, and one written while the
// Codex app was installed goes once it isn't.
func TestCodexDesktopListCLIOnly(t *testing.T) {
home, read := codexHome(t, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`, `model = "gpt-5.5"`+"\n")
desk := filepath.Join(home, ".codex", "magpie-desktop-models.json")
app := false
codexAppReads(t, &app)
list := `{"models":[{"slug":"gpt-6-sol"}]}`
deskListIs(t, &list)
cx := codex(home)
if ok, err := cx.Join(); err != nil || !ok {
t.Fatalf("join: %v %v", ok, err)
}
if cfg := read(); strings.Contains(cfg, "model_catalog_json") {
t.Fatalf("CLI alone:\n%s", cfg)
}
if _, err := os.Stat(desk); !os.IsNotExist(err) {
t.Fatalf("CLI alone: a list written: %v", err)
}
app = true
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if cfg := read(); !strings.Contains(cfg, "magpie-desktop-models.json") {
t.Fatalf("the app installed:\n%s", cfg)
}
app = false
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if cfg := read(); strings.Contains(cfg, "model_catalog_json") {
t.Fatalf("the app gone:\n%s", cfg)
}
if _, err := os.Stat(desk); !os.IsNotExist(err) {
t.Fatalf("the app gone: the list stays: %v", err)
}
}
// A model_catalog_json the user set while joined is theirs: it stands, and
// no list of magpie's is written beside it. And a key naming magpie's list
// with the file gone (removed by hand) goes, as Codex loads no config then,
// while the list isn't known to write it again.
func TestCodexDesktopListKeepsTheirs(t *testing.T) {
home, read := codexHome(t, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`, `model = "gpt-5.5"`+"\n")
dir := filepath.Join(home, ".codex")
desk := filepath.Join(dir, "magpie-desktop-models.json")
app := true
codexAppReads(t, &app)
list := `{"models":[{"slug":"gpt-6-sol"}]}`
deskListIs(t, &list)
cx := codex(home)
if ok, err := cx.Join(); err != nil || !ok {
t.Fatalf("join: %v %v", ok, err)
}
os.Remove(desk)
list = ""
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if cfg := read(); strings.Contains(cfg, "model_catalog_json") {
t.Fatalf("named with the file gone:\n%s", cfg)
}
list = `{"models":[{"slug":"gpt-6-sol"}]}`
cfg := strings.Replace(read(), `model = "gpt-5.5"`, `model = "gpt-5.5"`+"\n"+`model_catalog_json = "mine.json"`, 1)
os.WriteFile(filepath.Join(dir, "mine.json"), []byte(`{"models":[]}`), 0o644)
os.WriteFile(filepath.Join(dir, "config.toml"), []byte(cfg), 0o644)
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if cfg := read(); !strings.Contains(cfg, `model_catalog_json = "mine.json"`) {
t.Fatalf("theirs:\n%s", cfg)
}
if _, err := os.Stat(desk); !os.IsNotExist(err) {
t.Fatalf("written beside theirs: %v", err)
}
}