Commit Graph
18 Commits
Author SHA1 Message Date
yetone f7cf17290c codex: the app's logs and sessions are read at each file's own time, not the Windows folder listing's
bitssly on X (tweet 2118722838249234609, v0.1.3177, Store Codex
26.1029.7803.0, app-server 0.166.0-alpha.6): when Codex's usage ran out
the Codex app flickered, showed "Hosted AppServerManager identity is not
ready" (fatal_error_broadcasted) and had to be quit and opened again,
four times in a day.

The page is the app's own bug after its app-server is ended (#4550), and
magpie ends it on purpose when it becomes Codex's provider for the
used-up account. 4f624241f reads the app's log for the page and reopens
the app, but on Windows it never saw it: codexLogFiles kept only files
whose fs.DirEntry Info() time was after the end, and on Windows that is
the folder listing's time, which NTFS brings up to date only as a handle
on the file is closed. The app keeps its day's log open, so the file the
page went to listed hours old and was skipped. On the Windows box the
Store app's own log listed 10:27Z (os.ReadDir and Get-ChildItem) while
the app wrote to it at 15:22Z (os.Lstat, File.GetLastWriteTimeUtc).

The same listing time judged Codex's session files, which the
app-server holds open: codexTurnOpen skipped a turn running in a thread
opened over 30 minutes before, and codexSessionsQuietFor counted such a
session quiet, so on Windows the app-server could be ended under a
running turn.

- agent/codex_app_crash.go: codexLogFiles reads os.Lstat per file.
- provider/codex_app_server.go: sessionFileInfo (os.Lstat) for
  codexTurnOpen and codexSessionsQuietFor; a file gone between the
  listing and the stat is skipped.
- The other readers that judge a file another program may hold open by
  its listing time (sessions, claude_out_soon, subscription_usage,
  claude_subscription's idle sweep) are #5549. magpie's first look after
  its own restart ending an app-server that already started under the
  takeover (one of bitssly's four pages came a minute after an
  auto-update) is #5550.

Run:
- Windows box (win-run), Store Codex 26.1029.7803.0 installed there:
  the two new tests red on the old code
  (TestCodexAppCrashedReadsAFileTheAppHoldsOpen: crashed false;
  TestCodexSessionHeldOpenReadAtItsOwnTime: "not open"), green with the
  change; go test -run 'TestCodexApp|TestCodexSession|TestCodexTurn'
  ./internal/agent ./internal/provider ok. A Go probe on the box read
  the live Store log at listing 15:22:00Z, own 15:32:02Z.
- Broken by hand on Windows: codexSessionsQuietFor back on e.Info()
  makes TestCodexSessionHeldOpenReadAtItsOwnTime red ("quiet"); the
  test checks quiet before the turn, since reading the file closes a
  handle and Windows then brings the listing up to date.
- mote at this head on origin/main 80ae904c2: go test ./... -count=1
  -timeout 45m: 86 packages ok, red only in internal/gui
  TestMainWindowCorners (#3949, open, AppKit on mote's macOS 27).
- Rebased onto origin/main f9a88c4fb (1fd0bb967 update, f9a88c4fb wsl
  touched agent and provider): at this head go test ./internal/agent
  ./internal/provider ./internal/netproxy ./internal/update
  ./internal/plugin ./internal/pii ok on mote, GOOS=windows go vet of
  agent and provider ok.
- go vet ./... (darwin), GOOS=windows go vet ./... ok; GOOS=linux go
  vet ./internal/agent ./internal/provider ok; gofmt -l clean.
- On macOS and Linux the listing already carries the file's own time,
  so the tests pass there on old and new code alike.

Not tried: a real used-up ChatGPT account reaching the page and magpie
reopening the Store app (the reopen itself is #4550's, unchanged).
2026-11-07 00:07:00 +08:00
yetone 4f624241f7 codex: the ChatGPT app left on "hit a snag" after an app-server restart is reopened when idle (#4550)
ysm119 on Discord: each Restart of Codex in magpie left the ChatGPT
desktop app on "ChatGPT 遇到了问题 / 请重试" till it was quit and opened
again. magpie ends the app's own app-server cleanly (code 0); a
signed-in ChatGPT 26.1021 then fails "Hosted AppServerManager identity
is not ready" on the one it starts, logs fatal_error_broadcasted and
shows the page. That is the app's bug; magpie now follows it up.

- provider/codex_app_crash.go: for 60s after every end magpie makes
  (the watcher's step, RestartCodexApp, RestartSwitchedCodexApp) the
  app's logs are read for a whole fatal_error_broadcasted line stamped
  after the end. On one, with no request through magpie since or in
  flight and no Codex turn opened since, the app is quit and opened
  once. Otherwise it is left as it is and logged. Where it can't be
  reopened (Linux, an app that didn't quit) the Agents row says
  "The Codex app hit an error · quit it and open it again" (appSnag)
  until the app-servers seen then are gone or the app is closed.
  Unreadable logs reopen nothing.
- agent/codex_app_crash.go: the logs as the app's file logger writes
  them (macOS ~/Library/Logs/com.openai.codex, Windows
  %LOCALAPPDATA%\Codex\Logs and the Store package's LocalCache, Linux
  $XDG_STATE_HOME/codex/logs; YYYY/MM/DD folders, tail-read, half
  lines dropped). The relaunch is the Agents row's: on macOS
  ChatGPT.app found by its bundle id, quit as ⌘Q does and opened once
  it has ended, never forced; on Windows codexAppReopen.
- GUI line in en, zh, zh-TW, ja, de; docs/subsystems/agent-wiring.md.

Run on mote (macOS) at this head on origin/main 08982805e:
- go vet ./internal/agent ./internal/provider ./internal/gui (darwin
  and GOOS=windows), GOOS=linux go vet ./internal/agent
  ./internal/provider; go build ./...; gofmt -l clean.
- go test ./internal/provider ok; go test -run
  'TestCodexApp|Restart|Reopen' ./internal/agent ok.
- go test ./internal/gui: red only in TestMainWindowCorners (#3949) and
  TestPanelHiddenByWailsMarksPageOutOfSight (#5322), AppKit window tests
  red on mote's macOS 27 and filed before this change.
- win-run and wsl-run: go test -run TestCodexApp ./internal/agent
  ./internal/provider ok (the macOS relaunch test skips on Windows, as
  TestWorkBuddySidecarOutlivesQuitOnMac does, #4115).
- node --test codex-app-snag, codex-app-out, codex-app-back, i18n-keys,
  i18n-en-no-cjk: chromium and webkit, every language, 980 and 440px.
  i18n-keys' "a plugin's words are its own" stays red for "View routing"
  (#5327/#5332, red on main since 73c98ebc2); this change added a word
  there and took it out again.
- Core script budget: under it, not raised.

Broken by hand, each red then restored:
- codexAppCrashStep's call removed: 4 provider tests red.
- the watch not armed in RestartSwitchedCodexApp:
  TestCodexAppRestartsWatchForTheErrorPage red; in RestartCodexApp:
  TestCodexAppRowRestartWatchesForTheErrorPage red.
- the turn and request checks off: TestCodexAppInUseAfterItsErrorPageIsLeft red.
- the snag not set: TestCodexAppSnagSaidTillTheAppRestarts red.
- the stamp's >= since check dropped, or a half-written last line kept:
  TestCodexAppCrashedReadsItsOwnLogs red.
- a wrong Store package path: TestCodexAppCrashedReadsEachSystemsFolder red.

Not tried: a real signed-in ChatGPT app reaching the identity-not-ready
page, and a real quit and reopen of the user's ChatGPT.app (the relaunch
is tested with faked process listing, quit and open in a sandbox HOME).
The log line and folders are read from ChatGPT 26.1021's
file-based-logger and the reporters' logs; a later app that renames
them reopens nothing, as before this change.
2026-11-05 17:24:53 +08:00
yetone 099d2b4612 agents: Codex back on ChatGPT after the reset says to restart its app, with a Restart
A bug report by mail (11-04): after the ChatGPT allowance reset, Codex
was back on its account, but the Codex app still showed no account and
no usage. The app's app-server was the one it started while magpie
served Codex, and the app reads account/read only as that starts. With
"Restart the Codex app when magpie switches its account back" off (the
default, #4550), codexAppStep leaves it running, and nothing said so;
the takeover's hover even said "It all comes back on its own at the
reset". On Windows StaleCopies lists only the daemon, so no hint showed
there either.

- provider: codexAppStep remembers the app-servers it left running when
  magpie gave Codex its account back (c.out && !out, not magpie's own
  account switch, which codexAppLeft already says). CodexAppBack() is
  true till they are gone (rechecked every 30s; a failed listing keeps
  it), a route change clears it, and RestartSwitchedCodexApp clears it.
- gui: /api/state's Codex row carries appBack (not for a WSL Codex).
  The Agents row says "Back on ChatGPT · restart the Codex app to see its
  account" with a Restart, and the row opened says why, with the same
  Restart; it posts codex/daemon/restart-app, as the Providers page's
  "still signed in as" Restart does. The takeover text now says the
  account and usage come back once the app-server restarts, and that
  this row offers it.
- i18n: zh, zh-TW, ja, de for every new and changed string.

Run (on mote unless named; rebased on origin/main d892dd6a2, then 8b902e287 (checkin only; vet and CodexApp/Checkin tests ok there), the gui budget/i18n Go tests, CodexApp and i18n-keys, codex-app-back, codex-app-out ran again at this head: ok, 148 of 148):
- go test ./internal/provider -run CodexApp: ok; also ok on Windows
  (win-run) and Linux (wsl-run, with go vet ./internal/provider).
- go test ./internal/gui: one red, TestMainWindowCorners/macOS_26_SDK,
  which is #3949 (macOS 27 on mote), nothing else.
- GOOS=windows go vet ./internal/provider ./internal/gui: ok. Linux vet
  of internal/gui needs gtk4/webkitgtk dev packages the WSL distro
  lacks, so it was not run.
- gofmt -l internal/: nothing.
- GUI: every codex-*, agent*, ag-* and i18n* file (86 files), both
  engines: 1484 of 1486. The two reds are codex-auto-reset webkit de
  1100/420, red the same alone at origin/main e0ee9265e; #4790 (noted
  there that macOS has it too). The new codex-app-back.test.cjs: 20 of
  20, every language, both engines, 980 and 440 px, line and row opened.
- Broken by hand: leftBack forced false, TestCodexAppBackSaidTillItRestarts
  went red ("the app left on the takeover's app-server isn't said");
  restored.

Not tried: a real Codex app through a real reset; the state machine is
driven with stubbed app-server listings.
2026-11-05 01:45:17 +08:00
yetone 1af868a153 provider: a Codex app left on a used-up account restarts with Restart the app off too (atom0964)
v0.1.2826 (1dead5dc7) added Restart the Codex app when magpie switches
its account, off by default, so that atom0964's ChatGPT app stopped
relaunching at every switch magpie made between two Plus accounts. Its
guard in codexAppStep left the app-server running for every change
magpie made by itself, and that covered too much:

- the takeover in, where magpie becomes Codex's provider for its used-up
  account;
- the first look on a used-up account;
- magpie's switch off an account at 98% (SwitchWhenSpent).

The Codex app greys Send for every model while its signed-in account's
rate limit says not allowed, and reads the account only as its
app-server starts (ChatGPT.app 26.1013, 8b0132b3d). Left running, the
app stayed on the used-up account with Send grey, so neither magpie's
fallback nor the next account could serve it until the user restarted
it by hand. With the default off, that broke the takeover for every
user with a fallback.

Now those three restart the app-server tick or not (`needed` in
codexAppStep: `into || c.switched && c.spent`). The tick covers only
what the app works without: the switch back once the first account has
room, and Codex given its account back at the reset. The label, tooltip
and toasts say "switches its account back" in en, zh, zh-TW, ja and de.
The tooltip adds that a used-up account restarts the app anyway.
SwitchWhenSpent's stale-app log, RestartApp's comment and
agent-wiring.md say the same.

atom0964 will still see one restart when an account runs out. Send
needs it. The restarts at the switch back and at the reset are gone.

Run at the pushed head (on mote, rebased on c0d4f17a3):
- gofmt -l clean; GOOS=linux (-tags nogui) and GOOS=windows go vet of
  provider and gui clean.
- go test -count=1 -timeout 45m ./internal/provider ./internal/davsync
  ./internal/backup: ok. ./internal/gui -run "Budget|I18n|Restart": ok
  (the core script budget holds with the longer strings, no raise).
- GUI (chromium + webkit, narrow and wide, en/zh/ja/de): restart-app,
  switch-only, i18n-keys, codex-accounts-via: 76/76.

Broken by hand: codexAppStep's guard without `!needed`:
TestCodexAppLeftRunningForMagpiesOwnChanges ("magpie switched off the
used-up account: ended [], want [60]") and
TestCodexAppFirstLookOnAUsedUpAccountRestarts go red.

Not tried: a real ChatGPT desktop app on a real used-up account (the
app-server end is stubbed); Windows, where an end is followed by
CodexAppReopen.
2026-11-03 16:55:23 +08:00
yetone 1dead5dc7b provider: magpie's own Codex account switches leave the Codex app running unless Restart the app is ticked (atom0964)
atom0964 on Discord (#help) has two ChatGPT Plus accounts in magpie, with
Codex always signed in to the first and Only switch Codex's sign-in both
off, on Smart routing. The Codex desktop app kept restarting by itself.
They tried several combinations of the ticks and it still did.

Why: with both off, SwitchWhenSpent signs Codex in to the next account at
98% and back to the first below 90%. Each switch was noted for the Codex
app (codexAppSpentSwitch), and KeepCodexAppOnItsProvider ended its
app-server once no turn ran; the ChatGPT app shows that as an error and
relaunches (#4550). With Codex kept on the first account there is no
switch, but the used-up takeover (magpie made Codex's provider for a
used-up account and given back once it has room; f37742e14 only limits
it to when another account can serve, which two accounts always can)
changed Codex's route, and that ended it too. So every combination
restarted the app.

What changed:
- Provider.RestartApp (restartApp), off by default, set by
  provider/restartapp (SetRestartApp, Codex only). This computer's own,
  like KeepLogin: Mirror keeps it and davsync's hash leaves it out.
- switchLoginBy(agent, user, byMagpie): SwitchWhenSpent marks its
  switch magpie's own (codexApp.auto). In codexAppStep, a change that is
  magpie's own (its switch, the takeover in or out, the first look at an
  app on a used-up account) ends nothing unless RestartApp is on; it
  logs why and the app follows once restarted. The switch notice's
  Restart, the Agents row's Restart, and uninstall are unchanged.
- Still restarted: a switch the user makes (Switch, the Agents row, the
  CLI), a Connect or Disconnect, failover written or taken out (any
  route change while the takeover state is unchanged), and a user's
  switch pending when magpie's comes.
- GUI: under Codex's accounts, with more than one on (or the tick set),
  "Restart the Codex app when magpie switches its account", with a tip
  and toasts in en, zh, zh-TW, ja and de. app.js paid for it with five
  account-row comments cut; TestCoreScriptBudget is not raised.
- docs/subsystems/agent-wiring.md says which changes restart the app.

Known edge: a takeover flip in the same 3s tick as a Connect is taken
as the Connect's (restarted), since the route changed by the user.

Run at the pushed head (on mote, rebased on b967b1620; then rebased on 2a19043af, a Library change touching none of these files, with the core budget checked again: 160 bytes under):
- gofmt -l clean; GOOS=linux (-tags nogui) and GOOS=windows go vet of
  provider, gui, davsync clean.
- go test -count=1 -timeout 45m ./internal/provider ./internal/davsync
  ./internal/backup: ok. ./internal/gui: ok except
  TestMainWindowCorners/macOS_26_SDK, red alone on origin/main 694f4a6d6
  too (#3949). ./internal/cli: ok except TestEveryRouteHasACommandOrIsNamed
  on GET /api/context/reply (#5026).
- GUI (chromium + webkit, 1100 and 420 wide, en/zh/ja/de):
  restart-app (new), switch-only, claude-switch-only, keep-login,
  codex-accounts-via, account-arrange, i18n-keys: 110/110 and 94/94.
  account-arrange's webkit case went red once in a 5-file run on an
  aborted /api/lanes request; it passes alone on origin/main and at
  this head.

Broken by hand:
- codexAppStep's guard turned off: TestCodexAppLeftRunningForMagpiesOwnChanges
  and TestCodexAppFirstLookLeftRunning go red (ended [60]).
- SwitchWhenSpent marking its switch as the user's: TestCodexSwitchedWhenUsedUp
  goes red ("magpie's own switch is counted the user's").

Not tried: a real ChatGPT desktop app on a real used-up account (the
app-server end is stubbed as before); Windows, where the app is quit and
reopened (CodexAppReopen runs only after an end, so leaving it running
skips it too); no CLI command for the tick (switch-only has none either).
2026-11-03 16:34:30 +08:00
yetone 9f641daeab codex: a turn stuck on a used-up account no longer holds the Codex app's Send grey after a switch by hand or magpie's takeover (xiaoq6285)
xiaoq6285 on Discord (v0.1.2700): Codex on an official ChatGPT account
ran out of quota, and after switching to another account or provider in
magpie, the Codex app's Send button stayed grey. magpie has no chat
composer of its own; the button is the Codex app's, which greys Send for
every model while the app-server it runs is on a ChatGPT account the app
holds. magpie lifts that by restarting the app's app-server
(codexAppStep), but never while a Codex turn is open in its sessions, for
up to codexTurnWindow (30m). When quota runs out mid-turn the turn is
left open, so the restart, and the grey, waited.

cc81ad4ea let a quiet stuck turn be restarted past (codexTurnStuckAfter,
2m) only after SwitchWhenSpent's own switch. Two siblings that leave the
app-server on the same used-up account still waited the 30 minutes:
- a switch by hand (Accounts, CLI) off an account last read held:
  noteCodexSwitch now marks it spent when the account's reading in hand
  says Held (codexHeldKnown: cache only, no I/O; a failed read or none
  is not held);
- magpie becoming Codex's provider for the used-up account (codex.out):
  codexAppStep marks the change into out spent. The way back at the
  reset still waits for any open turn, as a real one may run there.
A turn still writing its session or sending through magpie in the last
2 minutes holds the restart as before.

Tests, both red on the old code:
- TestCodexAppServerRestartedPastATurnStuckWhenMagpieTakesOver
- TestCodexHandSwitchOffAHeldAccountIsSpent
docs/subsystems/agent-wiring.md says so.

Run at this head on mote:
- go test -tags nogui ./internal/provider: ok (151s), at this head
  after the rebase on 52195b6b0 (it changed provider_test.go and two
  more provider files, so provider was run again).
- Before the rebase (on 07f4d46c5): provider ok, agent ok, gui ok;
  gateway red on TestCodexPoolSaysTheClientsVersion alone (#4807, red in
  a full package run, passes alone; -count=5 alone ok at this change);
  cli red on TestEveryRouteHasACommandOrIsNamed (#4826), red the same
  on origin/main 52195b6b0 just fetched.
- gofmt -l clean; GOOS=linux go vet -tags nogui, GOOS=windows go vet
  and go vet of ./internal/provider ./internal/agent ok at this head.
- Not run: the full go test ./... (agent, gateway, gui, cli and provider
  are what reach this code). No GUI change, so no GUI suite.

Broken by hand, each red, then restored:
- spent not set on the takeover (c.spent && into): the takeover test
  failed "a turn stuck on the used-up account as magpie took over:
  ended [], want [60]".
- noteCodexSwitch never marking spent: the hand-switch test failed
  "switched off out@x: switched true, spent false, want true, true".

Not tried: the real Codex app with an account actually out of quota
(no such account to hand), and Windows/Linux runs of the package (the
change is OS-independent logic; GOOS vet run). Not confirmed with the
reporter that their case is a turn left open; their screenshot wasn't
seen here.
2026-11-02 18:31:11 +08:00
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
yetone 70d50254cb 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.
2026-10-31 19:39:38 +08:00
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
yetone cc81ad4ea1 codex: a Windows Codex app that starts no app-server after a switch is reopened (蛋仔 on Discord)
After magpie switched Codex's account, the Windows Codex app answered
every send with "Codex app-server process is not available" until it
was quit and opened again. The report said the app doesn't reconnect
after a clean exit. That is half right. The app (MSIX OpenAI.Codex
26.1021, an Electron ChatGPT.exe that spawns codex.exe app-server over
stdio) does start another, but only once the old one's stdout reaches
EOF and its exit has fired. On Windows a child of the app-server (an MCP
server, a tool command) inherits that pipe handle. A child left running
keeps the pipe open, so the app never sees the close, starts none, and
its send() throws. Reproduced on the Windows box with the real codex.exe
0.164 app-server plus an MCP server: stdout ended only when the MCP
server did, and at once when the tree was ended.

magpie took "the old pid is gone" as done in each place it restarts the
app-server. None of them checked that a new one came:
- codexAppStep (the switch watcher) now records the pids it ended and
  waits codexAppBackWait (30s) for another app-server. If none comes and
  the app still runs (CodexAppOpen), it quits and opens the app as the
  Agents row's Restart does (CodexAppReopen: restartWin by the
  OpenAI.Codex_2p2nqsd0c76g0!App AUMID, Windows only). Where it can't,
  the row says to (CodexAppStuck).
- RestartCodexApp (macOS Restart), RestartCodexAppWhenIdle (uninstall)
  and the switch notice's Restart (RestartSwitchedCodexApp, 46aa7e6b8)
  wait for a new app-server too, and reopen or say so the same way.
  RestartSwitchedCodexApp's own 20s codexAppBackWait went into the
  shared 30s one.
- recheckLeft no longer clears the "still on the old account" record
  while the app is open with no app-server.
- A turn stuck open when quota ran out mid-task (蛋仔's follow-up) held
  the restart for up to codexTurnWindow (30m), since an open turn in
  Codex's sessions meant "a task runs". A switch off a used-up account
  (SwitchWhenSpent) now marks it spent (codexAppSpentSwitch). After such
  a switch, a turn that has written no session file and sent nothing
  through magpie for codexTurnStuckAfter (2m) counts as stuck, and the
  app-server is restarted past it. Its next request would be refused on
  that account anyway. A tool still running in that turn is ended with
  it. A switch for any other reason waits for the turn as before.
macOS is never reopened unasked (a reopen opens a new window,
yoshi037070, 10-27). WSL needs nothing: WSL Codex has no desktop app, and
the Windows app's app-server runs on the Windows side.

Run (at this head unless said):
- mote: go test -timeout 45m ./... at 4cf444bc4, this change on
  origin/main 807048470. It was rebased on dca3e2d71 after (5 commits,
  none in codex_app*/codex_switch/codex_daemon), and the -run below was
  run again at the pushed head. Result: every package ok except gateway and gui, whose reds aren't this
  change's (it touches neither package). gateway: TestAntigravityTurn*
  x4 and TestCodexSpentGrokFailingIsTold, red the same on origin/main
  46aa7e6b8 (#4362), and TestUnroutedUnknownHoldAnsweredSooner, which
  passes alone 3 of 3 at this head (filed #4377). gui:
  TestMainWindowCorners (#3949).
- mote: provider and agent -run 'CodexApp|CodexDaemon|RestartCodex|
  RestartSwitched|CodexSwitched|CodexSessionsQuiet|Switch' ok; gofmt -l clean; GOOS=windows, linux,
  darwin go vet ./internal/agent ./internal/provider ok.
- win-run, the same -run: everything of mine passes. Two tests are red,
  TestCodexDaemonEnvIsTheTerminals and TestWorkBuddyRestartOnMac, the
  same on origin/main (#4115).
- Broken by hand, each red, then restored: the step's no-app-server
  branch, RestartCodexApp's not-back check, uninstall's reopen,
  recheckLeft's open check (4 provider tests); RestartSwitchedCodexApp
  without the reopen (TestRestartSwitchedCodexApp "reopened 0"); the
  codexWinApp matcher widened to \WindowsApps\OpenAI.
  (TestCodexAppReopenedOnWindows). The stuck-turn bypass taken out
  ("a turn stuck on the used-up account: ended []"), the spent mark not
  set (TestCodexSwitchedWhenUsedUp), and the bypass without its spent
  and quiet checks ("a turn open after a switch not for spend: ended
  [60]").
- Real Codex app on Windows (xiaomi-gpu, OpenAI.Codex 26.1021.3705.0,
  from a throwaway test, not committed, run as a scheduled task in the
  console session):
  - RestartCodexAppWhenIdle ended the real app-server, and the app
    started another within ~0.6s. codexAppBack saw it (left false).
  - codexAppReopen quit ChatGPT.exe 43832 and opened the app by its
    AUMID in 3.7s (main now 70060). A new app-server came 9.6s after.

Not tried:
- The stuck state itself in the real app: an MCP child really holding
  the app-server's stdout inside the Codex app. It was reproduced with
  codex.exe outside the app, and the reopen was run on a working app.
- A signed-in real account switch on Windows.
- The quota-mid-task hang with a real used-up account. Why the app sat
  on the turn wasn't reproduced. A plain usage_limit_reached refusal
  closes the turn (task_complete, as dd9828f2b found), so this covers a
  turn that stays open, whatever holds it.
- Whether a reopen loses an unsent draft.
- The macOS path with a real stuck app (it says to quit and reopen;
  nothing is reopened there).
2026-10-30 13:42:48 +08:00
yetone 46aa7e6b89 providers: the Codex app's switch notice has a Restart (nb5p_hQ on X)
After an account switch, Codex's accounts say the Codex app is still on
the account before. A user asked for a button that does the restart for
them. The notice now has Restart: magpie's own dialog asks first, then
magpie ends the app's own app-server (POST /api/codex/daemon/restart-app,
provider.RestartSwitchedCodexApp), waits for it to be gone and for the
app to start another, which reads auth.json and the new account. The
window stays, as the owner chose on 10-27 (quit and reopen opened a new
window). It ends the app-server now even mid-turn, after the confirm says
a running turn stops.

The app-server is ended as a clean exit (proc.End), never killed: the app
shows a killed app-server as a crash, so there is no force fallback. Each
failure says so in the notice, in the reader's language: the app isn't
running, the app-server couldn't be ended, the app didn't start another
within 20s, or the listing failed ("couldn't tell", not success).

Only the Codex desktop app gets the button (macOS ChatGPT.app/Codex.app,
the Windows Codex app). CLI agents in a terminal get none: restarting
them would lose the user's session. The text stays as it was.

app.js is 457 bytes smaller than main (comments cut), no budget raise.
The button and the confirm and four notices are in zh, zh-TW, ja and de.

Run:
- mote (darwin): gofmt -l clean; go vet agent, provider, gui on darwin
  and GOOS=windows; GOOS=linux go vet agent and provider (gui needs
  WebKitGTK headers, which neither mote nor magpie-desktop has).
- mote: go test provider -run 'TestRestartSwitchedCodexApp|TestCodexApp|
  TestRestartCodexApp' ok; agent TestRestartSwitchedCodexAppSays,
  TestEveryNotice ok; gui TestCoreScriptBudget etc. ok.
- win-run: the same provider tests ok, with real helper processes ended
  by proc.End (TerminateProcess) and a new one started.
- mote GUI: codex-app-stale + i18n-keys, chromium and webkit, en zh
  zh-TW ja de, 900 and 480px: 54/54.
- Broken by hand: TestRestartSwitchedCodexApp and ...Real go red when
  the wait for a new app-server always holds ("none back: <nil>").
- Real app: on mote, the real ChatGPT.app 26.1021 opened under the
  test's sandbox HOME (not signed in), its app-server 5232 ended, the
  app started 5901 within 3s, no diagnostic report; the app was quit.
  (A throwaway test, not committed.)

Not tried: a signed-in Codex app switching between two real accounts
(whether its window shows the new account's limits after this); the real
Windows Codex app; the Restart clicked in the real magpie GUI.
2026-10-30 12:35:18 +08:00
yetone 51aa4c0ef3 codex: a turn left open by a gone app-server doesn't hold an account switch back (danzi002)
蛋仔 (danzi002 on Discord, 10-29 and 10-30) switched the Codex app's
used-up account in magpie, auth.json changed, and the app stayed grey
until it was quit and opened: "如果打开了 codex 再不点击任何会话的时候,
我手动切换账号它会重启,如果点了会话再切换账号就重启不了". dd9828f2b
made a switch restart every one of the app's own app-servers once no
Codex turn runs. This one never ran.

Why. Codex reads auth.json once; a 401 reloads it only for the same
account id (codex-rs/login/src/auth/manager.rs, reload_if_account_id_
matches), so only a new app-server takes up a switched account.
codexAppStep waits while any session file written to in the last 30
minutes ends in task_started. Tried with the real codex
0.164.0-alpha.11.1 (ChatGPT.app's), sandbox HOME/CODEX_HOME, a fake
Responses server holding a turn open:
- SIGTERM or stdin closed mid-turn: turn_aborted is written.
- SIGKILL (force quit, crash): the file ends in task_started for good.
- thread/resume in a new app-server, which is what opening the thread
  in the app does, appends thread_settings_applied and moves the mtime
  each time, nothing else changed.
- a 429 usage_limit_reached ends the turn with task_complete, so being
  out of credit doesn't leave one open.
So a thread whose turn a killed app-server left open reads as running
for as long as the user keeps opening it, and the switch waits forever.

Change. proc.Started(pid) reads when a process started (darwin
kern.proc.pid P_starttime, Linux /proc/<pid>/stat starttime + btime,
Windows GetProcessTimes; ErrNoStart elsewhere). codexAppStep and
RestartCodexAppWhenIdle pass the oldest start of the app-servers they
would end; an open turn whose task_started line's timestamp is more
than 2s before it can't be running in them and no longer counts. A
start or a timestamp that can't be read counts the turn as running, as
before. A turn started in a running app-server still holds it: no turn
is cut short.

Run (mote-run unless said):
- gofmt -l internal/proc internal/provider: clean.
- go vet ./internal/proc ./internal/provider on darwin, GOOS=linux
  -tags nogui, GOOS=windows: ok.
- go test -count=1 -timeout 45m ./internal/proc ./internal/provider:
  ok (6.2s, 110s).
- wsl-run (magpie-desktop) and win-run (xiaomi-gpu): TestStarted,
  TestCodexTurnOpen, TestCodexApp*: ok.
- The real killed-turn rollout above, copied into a temp test: open
  with since unknown, not open with since after the kill, open with
  since before the turn.
Broken by hand:
- codexAppStep passing a zero since (the old behaviour):
  TestCodexAppSwitchPastATurnItsServerNeverRan red ("ended [], want
  [60]").
- sessionTurnOpen ignoring since: TestCodexTurnOpen red too.
Not tried: the reporter's own app on a real account switch; whether
their app-server was in fact killed (force quit or crash) is not known.
A restarted app-server with the thread left open was not watched
ungreying Send in the real app.
2026-10-30 01:22:45 +08:00
yetone dd9828f2b8 codex: an account switch restarts every one of the Codex app's own app-servers, and a reply that says the account is out switches at once (danzi002 on Discord)
danzi002 (Discord, Codex app on magpie API, several ChatGPT Plus accounts):
when the Plus account runs out the app greys Send; magpie signs Codex in to
the next account, but the app stays grey until it is quit and opened, and
only then is on the next account.

Cause: at the switch, noteCodexSwitch handed codexAppStep a single pid,
the first app-server-looking process (findCodexApp) of a 5-second ps
listing. That can be another program's `app-server --listen stdio://`
(ps lists by pid; on the owner's Mac Miyi Helper's runs beside the app's
own right now), which codexAppStep then drops as not the app's own, so
nothing is ended; and a ps slower than 5s dropped the switch for good.
Now the switch only marks codexApp.switched, and codexAppStep lists the
app's own app-servers on Codex's home with the same test and 30s bound a
route change uses, again at the next look if the listing fails, and ends
each once no Codex turn runs. findCodexApp (the "still on the old
account" notice) skips --listen ones too.

Also: a reply that says the active account is out (CodexMayBeUsedUp,
already reading the account at once) now runs SwitchWhenSpent too, so
the switch no longer waits for the account loop's next look, up to five
minutes when the last reading was under 90%. The app sends nothing once
it is grey, so no later request tells magpie.

Checked: a usage_limit_reached turn does not hold the restart back. Real
codex 0.164.0-alpha.11.1 (ChatGPT.app's bundled one) `codex exec` against
a fake 429 usage_limit_reached server in a sandbox CODEX_HOME writes
task_started then task_complete, so codexTurnOpen counts it ended.

Run (at 9f36d768d + this change, on mote):
- TestCodexAppSwitchRestartsTheAppsOwnServers (new): another program's
  --listen app-server listed first, own one ended; ps failing at the
  switch and at the first look, both own ones ended at the next.
- TestCodexReadNowSwitchesAtOnce (new): active account at 100%, the
  other at 20%: codexReadNow signs Codex in to the other.
- Real processes in a sandbox HOME (temporary test, not committed): a
  fake codex built at …/Helper.app/…/codex `app-server --listen stdio://`
  started first and one at ChatGPT.app/…/CodexCLI.app/…/codex
  `app-server --analytics-default-enabled`, real ps, real SwitchLogin
  rewriting the sandbox auth.json, real proc.End: the app's own one
  exits; on origin/main nothing is ended and it still runs.
- go test ./internal/provider: ok. go test -run Codex ./internal/gateway: ok.
- gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows ok
  (provider, gateway).

Broken by hand: on origin/main's codex_app_server.go, codex_daemon.go and
codex_app_out.go both new tests are red ("switched beside another
program's app-server: ended [], want [221]"; "Codex left on
new@example.com").

Not tried: the real Codex app with two real Plus accounts running out
(danzi002's setup is unknown to me: whether something else runs a
--listen app-server there, or ps was slow). Whether a restarted
app-server alone ungreys Send on the new account was tried for route
changes on ChatGPT.app 26.1013, not for an account switch.
2026-10-29 18:55:21 +08:00
yetone 96a703a591 uninstall: magpie uninstall and Settings › Uninstall magpie put every agent back before magpie's files go
Jevik_AI on X (Windows, portable magpie) deleted magpie's folders by hand.
~/.codex/config.toml kept openai_base_url on 127.0.0.1:3425, and every
Codex turn then failed with a 502, with nothing left that knew why.
Deleting magpie's files never puts an agent back, so there is now an
uninstall that does.

- internal/uninstall: Run goes in this order. System removes the
  background service, opening at login, the Start menu shortcut
  (shortcut.Remove, with its App Paths key), the magpie:// links and the
  privacy model's helper. Agents ends Codex's account failover, then runs
  each connected agent's own Disconnect, or its offline disconnect for an
  app that is closed. That is the same code as the Agents page, so a value
  the user changed by hand since Connect stays theirs (e7f21dba5 for
  Codex). It then ports Codex's rollouts and restarts the Codex app's
  app-server (provider.RestartCodexAppWhenIdle, which says whether one was left on
  the old config). Data deletes magpie's folders, but keeps the Library's
  folder while it holds skills, servers or hooks the user's agents use.
  The executable is left where the user put it.
- `magpie uninstall [--yes]`: refuses while another magpie runs, and
  says which one. On a terminal it lists what goes and asks [y/N]; with
  no terminal, only --yes does it.
- Settings › About › Uninstall magpie: POST /api/uninstall runs System
  and Agents. If anything failed, nothing else is removed and the sheet
  offers Try again. Otherwise POST /api/uninstall/finish starts a
  detached `magpie uninstall --data-after <pid>` (Setsid on unix,
  DETACHED_PROCESS on Windows) and the app quits; the files go once it
  has ended, since WebView2 holds its folder open until then. A Codex app
  still on the old config is named before quitting. magpie web refuses
  both routes. Strings go through t() in all five languages.
- --data-after refuses a pid that is no process (0, negative, its own):
  kill(0) counts as alive, and a run given 0 waited out its 2 minutes.

Not done, and why:
- No restore on quit. Quitting keeps agents on magpie by design: the
  service or the next start serves them.
- No Apps & Features entry or Windows uninstall hook. A portable copy
  registers nothing with Windows, and an uninstaller that ran without
  magpie's own disconnect would leave the configs as they are. The
  `# magpie:` note above each line magpie writes (previous commit) is the
  recovery path for a copy deleted by hand.

Run at this head (db75663a0's tree, on 5d24bc6cc), on mote:
- gofmt -l clean; go vet ./... on darwin (-tags nogui), GOOS=windows and
  GOOS=linux -tags nogui: ok.
- go test -tags nogui -count=1 -timeout 45m on ., internal/uninstall,
  cli, gui (TestCoreScriptBudget included), provider, shortcut, edit,
  agent and proc: all ok.
- GUI at this head: mote was at load 100-120 with other sessions' GUI
  runs. Chromium crashed (SIGBUS) mid-file in settings-search and
  settings-sections after their en cases passed, WebKit failed to launch
  within 180s in s3-sync, settings-search and settings-sections, and
  fonts and title-bar-tools went red on unstable elements (#2984 has
  fonts under load). uninstall-settings had not reported when this was
  pushed.
- At the earlier base b3580c72c, on this machine: the full suite
  (-timeout 45m), red only on #3884, #3632 (both since fixed on main) and
  the core budget, which main's later move of Web search out (#4133) has
  room for now. GUI: 104 Settings and i18n files in chromium and webkit,
  uninstall-settings and i18n-keys green in both; the reds were #3797,
  #3876 and the load reds above.

Tried for real, in a sandbox HOME: `magpie uninstall` end to end with
Codex connected and a sandbox Codex app running (config.toml back
without magpie's lines, magpie's data gone, the exe kept, the app's
app-server restarted onto the restored config); codex-cli 0.162.1
reading the noted config.toml. Other agents were covered by tests only.
Not tried: the GUI's Uninstall in a real app window, a real
--data-after run waiting on a live app's pid, and the Windows box.

Broken by hand: --data-after's pid guard removed ->
TestUninstallDataAfterNeedsAProcess red (0, -1 and its own pid
accepted); restored, green.

At the pushed head (rebased on 51f66fe8b; the uninstall call adapted to
EndCodexFailover's (bool, error) on the way), on mote:
- gofmt clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows ok;
  go build ./... ok.
- go test -timeout 45m: internal/uninstall, edit, shortcut, cli, agent,
  provider ok; internal/gui red only on TestMainWindowCorners/macOS_26_SDK
  (#3949, macOS 27).
- GUI: uninstall-settings and i18n-keys, 30 pass 0 fail.
- WSL (magpie-desktop, CGO_ENABLED=0): uninstall, edit, shortcut,
  provider ok; agent red on TestGrokInstalled, TestOmpDetectedWithPiDir,
  TestZedAppPath, the same at origin/main 51f66fe8b (#4033, #3548).
2026-10-29 17:47:30 +08:00
yetone 791e27ec34 agent: the Codex app's Restart restarts only its app-server, its window stays
yoshi037070 (Discord, 10-27): when magpie restarted Codex, a new Codex
window opened. The switch paths (Connect, Disconnect, failover) already end
only the app's own app-server (codexAppStep, proc.End: SIGTERM on macOS,
exit 0 on Windows), and the app starts another and keeps its window. The
Agents row's Restart beside a Codex app left on the old list did not: on
macOS ReopenCodexApp ran osascript "quit" and then `open` on the bundle,
and that open is what brought up a new window.

Now ReopenCodexApp keeps its check (only a bundle the row lists as left on
the old list) and calls provider.RestartCodexApp, which ends each process
whose command line starts with <bundle>/Contents/ and is the app's own
app-server (isCodexAppOwnServer: an app-server, not --listen), waits up to
10s for them to be gone, and errs when none ran. The app, its window and
another program's --listen server are left alone. The confirm says only the
app-server restarts and the window stays; the status says "Codex app-server
restarted" (zh, zh-TW, ja, de). app.js stays under its budget, with no raise (1,310,458 of 1,310,460
after rebasing on 97332140a): the "asked first" comment went and
appReopen's shrank. The
agent-wiring page says the same.

The model picker: the app's restart recovery doesn't refetch its models
query; it refetches on the next window focus once the list is over 5
minutes old (staleTime), or when the picker is mounted again.

Run (rebased on origin/main, at this commit's tree):
- go test ./internal/agent: ok (280s).
- go test ./internal/provider -run 'Codex|Restart': ok. Before the rebase,
  the whole package with -skip TestUnreadLoginsWriteErrorsKeepCredentials
  (#3632 crashes the binary) was ok except TestFetchAsksASharedBaseOnce and
  its two siblings (#3854).
- go test ./internal/gui -run TestCoreScriptBudget: ok.
- GUI: agent-stale-copies in chromium and webkit, 5 languages, 980/440px:
  80 pass. i18n-keys: its only red is #3897 ("Runs as"). Before the
  rebase codex-app-stale passed too.
- gofmt -l clean; go vet darwin and windows for agent, provider, gui;
  linux for agent and provider (gui's linux vet needs wails' cgo here).
- Real app: a clone of ChatGPT.app 26.1021.31345 (APFS clone in a sandbox
  HOME, so the bundle is unique to it) run under that HOME. The real
  RestartCodexApp ended its app-server 32922 in 3.1s; the same main
  process (31894) started app-server 34143, and its one window (2273016)
  stayed the only window. The user's own ChatGPT was untouched.

Broken by hand:
- reopenApp back to appQuit + appOpen: TestCodexAppReopen red ("did quit
  …, open …").
- isCodexAppServer in place of isCodexAppOwnServer: TestRestartCodexApp…
  red (ended [2 3], the --listen one too).
- the bundle prefix dropped: red (ended [2 4], another bundle's).

Not tried: the picker refreshing in a signed-in app (the sandbox app isn't
signed in); Windows has no such Restart (the row's Restart for the Codex
app is macOS only; Windows switches already end codex.exe with exit 0 and
the app keeps its window, 3aead9c62).
2026-10-28 00:20:30 +08:00
yetone a312bf3e38 codex: switching the ChatGPT account restarts the Codex app's app-server once no turn runs
zhibukaixinyao (Discord #help, 10-27) asked when Windows gets the
"restart Codex" step: the Windows GUI has no Restart button, so after an
account switch the Codex app kept sending on, and showing the limits of,
the account it started with until the user quit and reopened it.

noteCodexSwitch now hands the app-server pid it recorded as left on the
old account to codexApp's stale set, the same set a route change fills.
KeepCodexAppOnItsProvider's step ends it once Codex is idle and no turn is
open, and the app starts another that reads auth.json afresh. This covers
a switch made in the gateway's process: the GUI's switch and
SwitchWhenSpent. Together with 3aead9c62 (the Windows app's app-server is
found at all) this needs no restart on Windows, macOS or Linux.

Run (at 9bfe68048 + this change):
- TestCodexAppServerRestartedOnAccountSwitch (new): nothing ended with no
  switch, nothing while a turn is open, pid ended once idle after the
  switch and only once, CodexAppStale "" afterwards.
- go test -race -count=3 -run TestCodexApp ./internal/provider: ok.
- go test ./internal/provider ./internal/agent ./internal/cli (skipping
  #3632 and #3854's tests): provider ok, cli ok; agent red only on
  TestFolderVarsAreListed, red on main since e4552cca9 (#3884).
- gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows ok.

Broken by hand: with the codexAppAccountLeft call removed the new test is
red ("switched, idle: ended [], want [60]").

Not tried: a real account switch on the Windows box (the box's Codex app
isn't signed in to two ChatGPT accounts). A switch made from the
CLI runs in its own process and still only sets the notice; the running
gateway doesn't hear of it.
2026-10-27 18:03:58 +08:00
yetone df83749218 codex: Disconnect and Connect restart the Codex app's own app-server, so the app stops sending to magpie without a restart
JiangLiuer222 on X: after Disconnect the Codex app kept sending to
127.0.0.1:3425, now and then. The app's app-server reads config.toml only
as it starts, so one started while Codex was connected kept the gateway's
base URL until the user quit the app.

Now the gateway's watcher (KeepCodexAppOnItsProvider, from 8b0132b3d)
ends the app's own app-server whenever what it reads at start and magpie
writes changes: model_provider, openai_base_url, model_catalog_json, the
magpie table's base_url and the used-up takeover (codexAppRoute, through
the provider.CodexAppRoute hook, which replaces CodexAppOnMagpie). Same
guards as before: never while a Codex request is in flight or ended in
the last 10s, never with a turn open, only on magpie's Codex home, a
clean exit. At its first look only a used-up account's app-servers are
ended, as before.

Also checked: after Disconnect nothing writes the base URL back. Failover
and the stale-model rewire skip a disconnected Codex, and the takeover
paths in Sync need Codex joined or on magpie.

Run at this tree (on origin/main 32ea16c5a), private GOCACHE, sandbox HOME:
- gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows
  (internal/agent, internal/provider); go build darwin, windows, linux
  nogui.
- go test -count=1 -run Codex ./internal/provider ./internal/agent: ok
  (at 47175a68f); -run CodexApp again at 32ea16c5a: ok.
- Broken by hand: comparing only the takeover flag (the old condition)
  made TestCodexAppServerRestartedOnDisconnect red ("disconnected, idle:
  ended [], want [60]").
- Real app: ChatGPT.app 26.1013 under a sandbox HOME (CFFIXED_USER_HOME),
  this build's magpie serve on 3492, Codex connected to a custom
  provider by magpie codex fakeai/fake-1. magpie codex default: the
  app's app-server 76497 ended within 5s, the app started 77343 and
  logged "Codex CLI process exited classifiedAsExpected=true code=0";
  window kept. Connect again: 77343 -> 79138. The owner's own app-server
  (1902, HOME=~) was untouched.

Not tried: Windows and Linux real apps, a turn running during the
switch (unit-tested only), a signed-in sandbox app (it was signed out,
so no turn was sent to see the new route used).
2026-10-27 16:33:29 +08:00
yetone 8b0132b3d2 codex: the Codex app sends again on every system as soon as magpie takes a used-up ChatGPT account over, and gets it back at the reset
Ethereal499 and Isdavexxx on X, MasterYi on Discord, #540. The Codex app greys
Send for every model, magpie's too, while the ChatGPT account it is signed in
to is used up. Its composer's gate (ChatGPT.app 26.1013) reads authMethod
"chatgpt" plus rate limit allowed=false, and it asks account/read only when its
app-server starts. ef37b6fb7 (v0.1.1381) could fail in practice:
- (a) The reopen was macOS-only, ran once per time out, only from the GUI
  process. A Windows or Linux user, a magpie without its window, or a second
  time out stayed greyed.
- Switch-back: the app stayed on magpie (authMethod null, no cloud tasks or
  Log out) after the reset. Nothing restarted it.
- (c) Detection lag: the account was read every 5 minutes.
- The notice sat on the Providers page, not on Codex's Agents row.

Now:
- The gateway process (`provider.KeepCodexAppOnItsProvider`, every 3s, on
  every OS) ends the Codex app's own app-server each time `Agent.AppOut`
  flips, both ways. The app starts another at once and reads account/read
  afresh. Its window, threads and drafts stay.
- It never ends an app-server while a Codex request is in flight through
  magpie or ended in the last 10s (`Server.CodexBusy`, Codex's requests only),
  or while a session under the Codex home has a turn open (`codexTurnOpen`).
- It ends only the app's own app-server. That one runs on stdio; one another
  program starts with `--listen` is left alone (Miyi Helper runs one on this
  Mac). It must also run on magpie's Codex home (`proc.EnvOf`: ps -E on macOS,
  /proc on Linux; Windows can't tell).
- The exit is clean (`proc.End`: SIGTERM, or TerminateProcess with code 0 on
  Windows). The app shows any other exit as a crash.
- A listing that fails is taken up again at the next look. One that outlives
  3 ends, or is denied, makes the row say to quit and reopen.
- The active account is read every minute while it is held or a window is at
  90% or more (`codexLookEvery`).
- Codex's Agents row says "On magpie until <reset> · ChatGPT allowance used
  up". Hovering it, or opening the row, says what is off until then (cloud
  tasks, the usage meter, Log out) and that it all comes back on its own. In
  en, zh, zh-TW, ja and de.
- Removed: gui's keepCodexAppSending/codexAppOut, provider's episode
  bookkeeping, and the Providers-page notice.

Not changed, by design:
- A Codex own model that no magpie account serves stays as it is: magpie
  can't serve that turn.
- Account discovery off and "Only switch Codex's sign-in" (login=chatgpt) are
  the user's own choices, and are left alone.

Full suite green at cff1a9686 (on origin/main 6ce0f14b9), below. At
4541b3e97 (rebased on 430e3050d, which touched gateway.go, app.js and
i18n.js beside this) ran: gofmt -l, go build ./..., go vet on darwin,
linux (-tags nogui ./...) and windows; go test -run Codex ./internal/provider
./internal/agent ok; GUI codex-app-out (22 ok) and codex-app-stale (20 ok),
chromium and webkit. Pushed rebased on b92cb2f59 (two more commits,
neither in these files), built, not rerun.

Run at this commit (cff1a9686, on origin/main 6ce0f14b9), with a private
GOCACHE (another session ran go clean -cache mid-run):
- gofmt -l: clean. go build ./...: ok.
- go vet: darwin (touched packages); GOOS=linux -tags nogui ./...;
  GOOS=windows (touched packages).
- go test -count=1 -timeout 45m: internal/proc, internal/provider,
  internal/agent, internal/gateway, internal/gui all ok, skipping four
  tests red on main without this change, each already filed:
  TestUnreadLoginsWriteErrorsKeepCredentials (#3632, a synctest crash
  since 091153bf) and TestFetchAsksOneListOnceForTwoBasesOfIt,
  TestFetchUnionsProtocolListsWithoutChat, TestFetchAsksASharedBaseOnce
  (#3854, since 6eb510f78). All three of the latter fail the same at
  origin/main 6ce0f14b9 and at 353519f55 without this commit.
- GUI at cff1a9686, chromium and webkit, 231 cases ok: codex-app-out (5 languages, 980 and 440px),
  i18n-keys, gui-de/ja/zh-tw, codex-app-stale, codex-accounts-via,
  agent-stale-copies, agent-app-restart, agent-failover, agent-connect,
  agent-layout, agent-expand-steady, agent-connected-unfolded,
  agent-notice-lang and codex-signin-always.
- Windows box (cross-built test binaries): proc and provider's Codex app
  tests pass. TestEndExitsClean passes there and goes red with exit code 1.
- Broken by hand, each went red:
  - Codex-only busy: TestCodexBusy.
  - The 90% cadence: TestCodexLookEvery.
  - The retry after a failed listing: TestCodexAppServerUnknown.
  - The turn guard: TestCodexAppServerRestartedEachWay.
  - The --listen exclusion: TestCodexAppOwnServer.
  - Code 0 on Windows: TestEndExitsClean.
  - The row's notice: codex-app-out.test.cjs, 4 of 4.
- Real app: ChatGPT.app 26.1013 in a sandbox HOME/CODEX_HOME, with this
  build serving on 3492/3493. Codex was put on magpie as Sync does for a held
  account (model_provider = "magpie", codex.out in stash.json). OpenAI's usage
  reading to the app was never faked.
  - The first listing ran out at load ~200, so the timeout is now 30s. The
    next look ended the app-server, and the app started another 12s later.
    Its profile went from "Pro · Usage · Log out" to "magpie" with no reopen,
    and /api/state carried appOut.
  - Undoing it ended the app-server again, and the profile came back to Pro,
    Usage and Log out.
  - The owner's own ChatGPT app (its app-server on ~/.codex) was never
    touched.
- One webkit codex-app-out run at load 291 had one red subtest, name lost.
  It passed 3 of 3 alone and in two full webkit runs.

Not tried:
- The Windows Store app (not installed on the box), and a real Codex app
  on Linux.
- A real used-up account.
- A turn running in the real app while the switch happens (unit-tested
  only).
2026-10-27 15:57:41 +08:00