Files
magpie/internal/agent/codex_app_route_test.go
T
yetone d6e1116380 codex: each end of the Codex app's app-server says why, and list flaps are guarded (#4550)
70d50254c (v0.1.2387) took the model list out of the route the Codex
app's app-server is ended on. The effort the app saves on each pick
went into the catalog, the next sync rewrote it, and the route's hash
of the catalog changed, so the app-server was ended every few minutes
and the ChatGPT app read that as a crash (goto_nozomi on Discord,
ggbdpq on #4550). This adds two things on top of that.

- codexAppStep logs each end with what made the app-server stale:
  "codex: ended the Codex app's app-server [pid] for the app to start
  another: <why>". The reason is the ChatGPT account switched, the
  route changed, or the used-up takeover. Before this a successful end
  logged nothing, so a reported crash couldn't be put down to magpie or
  to the app (ggbdpq asked for it on #4550).
- TestCodexAppRouteKeepsThroughListFlaps runs in a sandbox HOME
  through SyncCatalog. A relay's list drops a model and has it back,
  the catalog is re-ranked in another order with other descriptions,
  and the catalog is caught half-written. Each must leave the route as
  it was. 70d50254c's tests cover an effort pick and a rename.

Product call, as 70d50254c made it: a list change alone never restarts
the app-server, not even once Codex has been idle for a while. The
restart is what crashes ChatGPT when an MCP server is slow, and idle
time doesn't make it safe. The new list is read at the app's next
launch, or on the Agents row's Restart (StaleCopies). Where requests
go (model_provider, openai_base_url, the magpie table's base_url,
requires_openai_auth, the used-up takeover) still restarts it, as
TestCodexAppRouteFollowsConnectAndDisconnect and
TestCodexAppRouteFollowsOpenAIAuth hold.

Run (mote):
- go vet, GOOS=linux go vet and GOOS=windows go vet of ./internal/agent
  and ./internal/provider: clean.
- go test -count=1 -timeout 45m ./internal/agent ./internal/provider on
  e484b1e93: ok. Rebased onto 5cc2eea2f (dsh in internal/agent), then
  go vet and go test -count=1 -timeout 45m ./internal/agent again, and
  -run CodexApp ./internal/provider: ok.
- gofmt -l on both packages: nothing.

Broken by hand:
- TestCodexAppRouteKeepsThroughListFlaps at 70d50254c^ (the route
  still hashed the catalog) is red three ways: a model dropped (:119),
  another order (:141) and a half-written catalog (:147) each changed
  the route.
- With the used-up reason taken out of codexAppStep,
  TestCodexAppEndSaysWhy is red ("... start another: where Codex's
  config sends it changed", want "used up").

Not tried:
- The real ChatGPT/Codex app. Nothing here changes when the app-server
  is ended, only what is logged.
- The whole ./... suite and the GUI suite. Neither package touches the
  GUI.
- Whether goto_nozomi's crashes stop on v0.1.2387. #4550 stays open
  for them to say, with the new log line asked for if any crash is
  left.
2026-10-31 19:55:12 +08:00

150 lines
5.1 KiB
Go

package agent
import (
"encoding/json"
"path/filepath"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/provider"
)
// The route the Codex app's app-server is restarted on (codex_app_server.go)
// changes when Codex is connected beside its ChatGPT sign-in and when it is
// disconnected (JiangLiuer222 on X: the app kept sending to 127.0.0.1:3425
// after Disconnect), and not when the Codex app writes a model pick of its
// own, which would restart it on every pick.
func TestCodexAppRouteFollowsConnectAndDisconnect(t *testing.T) {
home, read := codexHome(t, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`, "model = \"gpt-5.5\"\n")
cx := codex(home)
route := func() string {
t.Helper()
r, _, known := codexAppRoute()
if !known {
t.Fatal("route not known")
}
return r
}
before := route()
if _, err := cx.ConnectHow(); err != nil {
t.Fatal(err)
}
connected := route()
if connected == before {
t.Fatalf("connect left the route as it was:\n%s", read())
}
// the Codex app writes its own pick: no restart for that
if err := edit.SetTOMLTop(filepath.Join(home, ".codex", "config.toml"), edit.KV{Path: "model", Value: "gpt-6.1-sol"}); err != nil {
t.Fatal(err)
}
if route() != connected {
t.Fatalf("a model pick changed the route:\n%s", read())
}
if err := cx.Disconnect(); err != nil {
t.Fatal(err)
}
// the [model_providers.magpie] table stays, for the threads started
// on it; openai_base_url goes
if r := route(); r == connected {
t.Fatalf("disconnect left the route as it was: %q\n%s", r, read())
}
}
// requires_openai_auth on magpie's table decides whether the Codex app
// has its ChatGPT account at all (openaiAuth), and the app-server reads it
// as it starts: the route changes with it, so the app is restarted onto
// its account when Sync writes it (Qyburn3 on X).
func TestCodexAppRouteFollowsOpenAIAuth(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)
}
with, _, _ := codexAppRoute()
codexUsedUp = func() bool { return true }
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if without, _, _ := codexAppRoute(); without == with {
t.Fatalf("requires_openai_auth taken out left the route as it was:\n%s", read())
}
}
// #4550: the Codex app's app-server was restarted three times in 20
// minutes with the user doing nothing, and a slow MCP server took
// ChatGPT down with each. What magpie writes to Codex's catalog changes
// with no change of the user's: a list that fails to fetch drops its
// models and has them back at the next fetch, the order is re-ranked by
// the models Codex used last, descriptions and windows follow the
// vendor's list. None of that is where Codex sends, so none of it is the
// route, which restarts the app-server at once; and a catalog that can't
// be read now is no change either.
func TestCodexAppRouteKeepsThroughListFlaps(t *testing.T) {
home := syncHome(t)
codexCat := filepath.Join(home, ".codex", "magpie-models.json")
writeFile(t, filepath.Join(home, ".codex", "config.toml"), "model = \"relay/glm-4.6\"\nmodel_provider = \"magpie\"\nmodel_catalog_json = '"+codexCat+"'\n")
writeFile(t, codexCat, `{"models":[]}`)
save := func(models ...string) {
t.Helper()
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: models}); err != nil {
t.Fatal(err)
}
SyncCatalog()
}
route := func() string {
t.Helper()
r, _, known := codexAppRoute()
if !known {
t.Fatal("route not known")
}
return r
}
save("glm-4.6", "glm-4.5")
full := readFile(codexCat)
if !strings.Contains(full, "glm-4.5") {
t.Fatalf("catalog:\n%s", full)
}
before := route()
// the list fetched while the vendor failed: one model left
save("glm-4.6")
if strings.Contains(readFile(codexCat), "glm-4.5") {
t.Fatalf("catalog still lists the dropped model:\n%s", readFile(codexCat))
}
if r := route(); r != before {
t.Errorf("a model dropped from the list changed the route:\n%s\nto\n%s", before, r)
}
// and back at the next fetch
save("glm-4.6", "glm-4.5")
if r := route(); r != before {
t.Errorf("the model back in the list changed the route:\n%s\nto\n%s", before, r)
}
// the same models in another order, as usage re-ranks them, with
// other descriptions
var cat map[string][]map[string]any
if err := json.Unmarshal([]byte(readFile(codexCat)), &cat); err != nil {
t.Fatal(err)
}
ms := cat["models"]
slices.Reverse(ms)
for i, m := range ms {
m["priority"] = i + 1
m["description"] = "fetched again"
}
b, _ := json.Marshal(cat)
writeFile(t, codexCat, string(b))
if r := route(); r != before {
t.Errorf("the list in another order changed the route:\n%s\nto\n%s", before, r)
}
// half a file, as another writer's would be mid-write: not known,
// never a change
writeFile(t, codexCat, `{"models":[{"slug":"glm`)
if r := route(); r != before {
t.Errorf("a catalog that can't be read changed the route:\n%s\nto\n%s", before, r)
}
}