codex: a change to the model list alone no longer ends the Codex app's app-server

The Codex app crashed and relaunched every few minutes (indekkusu141209
on X, #4550: 08:06, 08:12, 08:26).

Cause. KeepCodexAppOnItsProvider ends the app's app-server whenever
codexAppRoute changes. Since ca8a6f346 (lucis0116 on X) the route held a
hash of the bytes in the catalog that model_catalog_json names. The
ChatGPT app takes an ended app-server as a crash. Those bytes change far
more often than where requests go:

- Each model's default_reasoning_level follows config.toml's
  model_reasoning_effort (codexcat.ModelDefault, Creat.wu on Discord).
- The Codex app writes that key on every effort pick, and on a switch to
  a model with other levels. So does magpie's settle() when a pick moves
  the effort.
- The next SyncCatalog, set off by things like a plugin token refresh or
  a providers.json save, rewrote the catalog. 3s later the route had
  changed, and the app-server was ended once idle.

The route doc's own promise, "nothing the Codex app writes itself is in
route", was broken through the catalog.

The repro was in a sandbox HOME, not the live ~/.codex. A throwaway
gateway test diffed CodexDesktopCatalog's bytes:
- Two passes with nothing changed: same bytes.
- A model pick: same bytes.
- models_cache.json refetched with a new fetched_at/etag: same bytes.
- model_reasoning_effort low -> high: different bytes.
The new agent tests below show the same for magpie-models.json, and the
old route moved with it.

Change. The route is where requests go, and only that:
model_provider, openai_base_url, magpie's base_url and
requires_openai_auth, and AppOut. model_catalog_json and the catalog's
content are out, and codexCatalogTag (codex_app_route.go) is gone. A
list-only change (a provider renamed, a model added, an effort picked)
restarts nothing. The app reads the new list at its next launch. The
Agents row already shows a Codex app on an older list, with its Restart
(StaleCopies). Its digest compares slugs and visibility, so an effort
pick doesn't flag it. No new UI.

The effort in the catalog is kept. It is the Creat.wu fix, and with the
restart decoupled, a rewrite of a file the app reads only at start costs
nothing.

Trade-off: a provider renamed or a model added in magpie shows in a
running Codex app only after it is reopened, or after the Agents row's
Restart. Before this commit that happened on its own, which was
lucis0116's ask.

Siblings, all read:
- RestartCodexApp (Agents row), RestartSwitchedCodexApp (switch notice,
  46aa7e6b8) and RestartCodexAppWhenIdle (uninstall) are user-started or
  a real route change. None reads the catalog. Unchanged.
- KeepCodexDaemonCurrent restarts the CLI's managed daemon only with no
  codex session on it, on the slug digest. That is not the desktop app.
  Unchanged.
- No other agent hashes its catalog into a restart.
- zhangjian94cn's fix/4243-codex-lifecycle-boundary takes away all of
  magpie's restarts. That goes further than this commit, and no code was
  taken from it. #4243 (aresxu8866-hue) asked for the catalog to count
  only when it is magpie's, which this commit supersedes.

Tests:
- TestCodexEffortPickKeepsTheAppRunning (new): the reporter's case. An
  effort saved as the app saves it, then SyncCatalog. The catalog
  changes and the route does not.
- TestCodexAppRouteIgnoresARename replaces
  TestCodexAppRouteFollowsTheCatalog, whose guarantee is now reversed.
- Broken by hand: both new tests run on the old codexAppRoute are red
  ("an effort picked in the Codex app changed its route", "a provider
  renamed changed the Codex app's route"). Green with the change.

Run at this head (5b0bb1349's tree, rebased on e90a635a9), on mote:
- go test -tags nogui -count=1 -timeout 45m ./internal/agent
  ./internal/provider: ok, ok. The same before the rebase.
- gofmt -l .: nothing.
- go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows
  go vet ./..., go build ./...: ok. That run was before the rebase; vet
  on agent and provider was run again after it.

Not run: the full go test ./... suite. Another session's full suite held
mote, and the owner's rule is to ship without holding for it; it follows
after the push. Not tried: a real ChatGPT/Codex desktop app. Nothing on
the ChatGPT side was changed or measured (the MCP handshake ggbdpq
traced). docs/subsystems/agent-wiring.md was updated.
This commit is contained in:
yetone
2026-10-31 19:39:38 +08:00
parent b9e5bc6cb3
commit 70d50254cb
6 changed files with 68 additions and 88 deletions
File diff suppressed because one or more lines are too long
+12 -13
View File
@@ -70,9 +70,17 @@ func init() {
// codexAppRoute is where Codex's config sends it, as the app-server reads
// it at its start: the keys magpie writes for that, never one the Codex
// app writes itself (model); and whether magpie is Codex's provider for
// its used-up ChatGPT account (AppOut). Not known when Codex's config
// can't be read.
// app writes itself (model, model_reasoning_effort); and whether magpie
// is Codex's provider for its used-up ChatGPT account (AppOut). Not known
// when Codex's config can't be read.
//
// The model list (model_catalog_json, the file and what it holds) is not
// in it. A change of route ends the app's app-server, and the ChatGPT app
// takes that as a crash and relaunches. The catalog follows magpie's names
// and the effort new sessions start at, which the app saves on each pick,
// so a route that hashed it ended the app every few minutes
// (indekkusu141209 on X, #4550). A new list is read at the app's next
// launch, and the Agents row offers a Restart for it (StaleCopies).
func codexAppRoute() (route string, out, known bool) {
// asked every 3s for as long as magpie runs: the config looked at once
// for its keys, not once for each (#1665)
@@ -84,7 +92,7 @@ func codexAppRoute() (route string, out, known bool) {
}
out = a.AppOut != nil && a.AppOut()
var b strings.Builder
for _, k := range []string{"model_provider", "openai_base_url", "model_catalog_json"} {
for _, k := range []string{"model_provider", "openai_base_url"} {
v, _ := edit.GetTOMLTop(a.Path, k)
b.WriteString(k + "=" + v + "\n")
}
@@ -94,15 +102,6 @@ func codexAppRoute() (route string, out, known bool) {
// has its ChatGPT account at all (openaiAuth in codex.go): read as the
// app-server starts, like the rest
b.WriteString("magpie.requires_openai_auth=" + t["requires_openai_auth"] + "\n")
// and the list in that catalog, which the app-server also reads only
// as it starts: a provider or group renamed, or a model added, left
// the app on the names and models it started with (lucis0116 on X)
if c, _ := edit.GetTOMLTop(a.Path, "model_catalog_json"); c != "" {
if !filepath.IsAbs(c) {
c = filepath.Join(filepath.Dir(a.Path), c)
}
b.WriteString("catalog=" + codexCatalogTag(c) + "\n")
}
if out {
b.WriteString("out\n")
}
-45
View File
@@ -1,45 +0,0 @@
package agent
import (
"crypto/sha256"
"encoding/hex"
"os"
"sync"
"time"
)
// codexCatalogTag stands for what the catalog file at path holds now, for
// codexAppRoute: a hash of its bytes, "" when it can't be read. The route
// is looked at every few seconds (provider.KeepCodexAppOnItsProvider) and
// the catalog holds each model's instructions, a few MB with many models,
// so the file is read again only when its size or time changed. A rewrite
// to the same bytes (a Connect writes it again) is the same tag, and
// restarts nothing.
func codexCatalogTag(path string) string {
st, err := os.Stat(path)
if err != nil {
return ""
}
c := &codexCatalogSeen
c.Lock()
defer c.Unlock()
if c.path == path && c.size == st.Size() && c.mod.Equal(st.ModTime()) {
return c.tag
}
b, err := os.ReadFile(path)
if err != nil {
return ""
}
sum := sha256.Sum256(b)
c.path, c.size, c.mod, c.tag = path, st.Size(), st.ModTime(), hex.EncodeToString(sum[:12])
return c.tag
}
// codexCatalogSeen is the last catalog codexCatalogTag read.
var codexCatalogSeen struct {
sync.Mutex
path string
size int64
mod time.Time
tag string
}
+34
View File
@@ -100,6 +100,40 @@ func TestCodexNewSessionEffortLastsModelSwitch(t *testing.T) {
}
}
// An effort picked in the Codex app keeps the app running. The app saves
// it as model_reasoning_effort, the catalog's default_reasoning_level
// follows it (above), and the next sync rewrote the catalog. The route the
// app's app-server is ended on (codex_app_server.go) hashed the catalog's
// bytes, so each pick ended it a few minutes later, and the ChatGPT app
// read that as a crash and relaunched (indekkusu141209 on X, #4550). The
// route is where requests go, never the list: the app reads a new list at
// its next launch.
func TestCodexEffortPickKeepsTheAppRunning(t *testing.T) {
home, read := codexHome(t, "", "model_reasoning_effort = \"xhigh\"\n")
effortModels(t)
cx := codex(home)
if err := cx.Fields[0].Set("volc/all-five"); err != nil {
t.Fatal(err)
}
cat := filepath.Join(home, ".codex", "magpie-models.json")
listed, _ := os.ReadFile(cat)
before, _, known := codexAppRoute()
if !known {
t.Fatal("route not known")
}
// as the Codex app saves a pick of its own
if err := edit.SetTOMLTop(filepath.Join(home, ".codex", "config.toml"), edit.KV{Path: "model_reasoning_effort", Value: "low"}); err != nil {
t.Fatal(err)
}
SyncCatalog()
if now, _ := os.ReadFile(cat); string(now) == string(listed) {
t.Fatalf("the effort didn't reach the catalog, so this tests nothing:\n%s", read())
}
if r, _, _ := codexAppRoute(); r != before {
t.Fatalf("an effort picked in the Codex app changed its route: its app-server is ended for it\n%s", read())
}
}
// Grok Build: a model picked without the chosen level starts at its
// nearest, not at Grok's default.
func TestGrokNewSessionEffortLastsModelSwitch(t *testing.T) {
+9 -24
View File
@@ -70,26 +70,19 @@ func TestRenameGroupMovesTheAgentsOnIt(t *testing.T) {
}
}
// The Codex app's app-server reads its model catalog only as it starts,
// so the route it is restarted on (codex_app_server.go) follows what the
// catalog holds: a provider's name changed in magpie reached the file, and
// the app kept showing the old one (lucis0116 on X). The same bytes
// written again are no change.
func TestCodexAppRouteFollowsTheCatalog(t *testing.T) {
// A change to the list alone never restarts the Codex app's app-server:
// a provider renamed rewrites the catalog, and the app shows the new name
// at its next launch (TestCodexEffortPickKeepsTheAppRunning says why).
func TestCodexAppRouteIgnoresARename(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":[]}`)
SyncCatalog()
route := func() string {
t.Helper()
r, _, known := codexAppRoute()
if !known {
t.Fatal("route not known")
}
return r
before, _, known := codexAppRoute()
if !known {
t.Fatal("route not known")
}
before := route()
if !strings.Contains(readFile(codexCat), "GLM-4.6 · Relay") {
t.Fatalf("catalog:\n%s", readFile(codexCat))
}
@@ -100,15 +93,7 @@ func TestCodexAppRouteFollowsTheCatalog(t *testing.T) {
if !strings.Contains(readFile(codexCat), "GLM-4.6 · Renamed") {
t.Fatalf("catalog not rewritten:\n%s", readFile(codexCat))
}
renamed := route()
if renamed == before {
t.Fatal("a provider renamed left the Codex app's route as it was: its app-server keeps the old names")
}
// written again as it is (a Connect does): no restart for that
if err := writeOwnList(codexCat, []byte(readFile(codexCat))); err != nil {
t.Fatal(err)
}
if route() != renamed {
t.Error("the same catalog written again changed the route")
if r, _, _ := codexAppRoute(); r != before {
t.Fatal("a provider renamed changed the Codex app's route: its app-server is ended for a list")
}
}
+12 -5
View File
@@ -40,15 +40,22 @@ import (
// sending to 127.0.0.1:3425 after Codex was disconnected (JiangLiuer222 on
// X), so each such change restarts it the same way.
//
// A change to the model list alone never does: the app takes an
// app-server's end as a crash and relaunches, and the list changes far
// more often than the route (an effort picked in the app, a provider
// renamed), which ended the app every few minutes (#4550). The app reads
// a new list at its next launch, or on the Agents row's Restart.
//
// The app-server is ended as a clean exit (proc.End): the app reads any
// other exit as a crash and shows it.
// CodexAppRoute is set by the agent package: what in Codex's config the
// app-server reads as it starts and magpie writes (its provider, base URL
// and model list: route, compared, never parsed), and whether magpie is
// Codex's provider for its used-up ChatGPT account (out, AppOut); known
// false when Codex's config can't be read. Nothing the Codex app writes
// itself (its model pick) is in route, or a pick would restart it.
// app-server reads as it starts and magpie writes (its provider and base
// URL: route, compared, never parsed), and whether magpie is Codex's
// provider for its used-up ChatGPT account (out, AppOut); known false
// when Codex's config can't be read. Nothing the Codex app writes itself
// (its model or effort pick) and not the model list is in route, or a
// pick would restart it.
var CodexAppRoute func() (route string, out, known bool)
// CodexNoAccount is set by the agent package: whether the provider Codex's