From b3a57b3d8f742e84f09dafb008d5fca773e6e249 Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 10 Nov 2026 03:53:16 +0800 Subject: [PATCH] codex: turning the takeover off restarts the Codex app, so its ChatGPT projects come back Herzz____ on X, the Windows Codex app: with Codex through magpie the sidebar's project list is empty; they use magpie only when their subscription runs out, and want to switch between magpie and the native sign-in with the projects whole each way. What happens. When the ChatGPT account is used up, magpie becomes Codex's provider (codex.out) without requires_openai_auth: on that account the app's composer gate greys Send for every model, magpie's too (#540). Checked again in ChatGPT.app 26.1029's bundle: the gate picks the account's own snapshot (limitName null) whenever it is blocked, before any per-model additional_rate_limits, so keeping the account during the takeover would still grey Send. The app-server then answers account/read with no account and the app hides its ChatGPT projects and chats (72ca3d593). magpie never touches projects, project_roots or a thread's cwd: its thread moves only rewrite threads.model_provider (sessions/codex_provider.go), so path case or backslashes on Windows play no part. What was wrong. Turning the takeover off by hand (the takeover note's Keep on ChatGPT, the sign-in's Always ChatGPT, or Disconnect) looked the same to codexAppStep as the reset, magpie's own end of it: with Restart the Codex app to follow off (the default) the app's app-server was left running on no account, so the projects stayed gone after the user switched back to have them, until they restarted the app (the Windows app has no Restart of its own). Now Agent.AppOutOff (Always ChatGPT, or Codex not routed through magpie) reaches the step through provider.CodexTakeoverOff, asked only as out goes false, and a takeover ended so counts as the user's change: the app-server is restarted once no Codex turn runs, as for a Connect, and the Windows app reopened if none comes back (existing path). The reset itself still leaves the app running and says so (CodexAppBack); that note now names the ChatGPT projects and chats it lacks (en, zh, zh-TW, ja, de). agent-wiring.md says the same. Switching back to native is already in magpie: Agents > Codex > sign-in > Always ChatGPT (`magpie codex login chatgpt`), or Keep on ChatGPT on the takeover note; ChatGPT (the default) lets magpie take over again. Tests: TestCodexAppRestartedWhenTheTakeoverIsTurnedOff (turned off with a request in flight: held, then restarted; CodexAppBack not said; the hook asked once; the reset still left running and said); TestCodexOwnModelOutThenKeptOnChatGPT and TestCodexOwnModelGoesThroughMagpieWhileUsedUp assert AppOutOff for Keep on ChatGPT, Disconnect and the reset; codex-app-back.test.cjs checks the note names the projects in every language. Broken by hand: offByHand out of byHand, the provider test red (ended [], want [100]); the login half of AppOutOff false, the Keep on ChatGPT test red; the routed half false, the Disconnect check red. Run (mote-run at b87783fbb (this change's tree) on origin/main 547585cd9): gofmt -l clean; go build ./...; go vet linux -tags nogui ./... and windows ./..., darwin ./internal/agent ./internal/provider ./internal/gui; go test -tags nogui ./internal/provider ./internal/agent ok; GUI codex-app-back, codex-app-out, i18n-keys, i18n-en-no-cjk, gui-ja, Chromium and WebKit, every language, 155 of 155 pass. Windows (win-run on xiaomi-gpu): the provider and agent Codex app tests ok; the box's running Windows Codex app (26.1029) app-server command line has the shape TestCodexAppOwnServer already holds. Not run: a real used-up ChatGPT account (none at hand), so the restart after Keep on ChatGPT is tried with the stubbed step, not the real app; the full go test ./... waits behind mote's suite lock and runs after the push, any red filed. --- docs/subsystems/agent-wiring.md | 2 +- internal/agent/agent.go | 5 ++ internal/agent/agents.go | 7 ++ internal/agent/codex.go | 5 +- internal/agent/codex_usedup_test.go | 28 ++++++++ internal/gui/assets/app.js | 2 +- internal/gui/assets/i18n.js | 8 +-- internal/gui/tests/codex-app-back.test.cjs | 12 ++-- internal/provider/codex_app_server.go | 22 ++++++- internal/provider/codex_app_server_test.go | 77 +++++++++++++++++++++- 10 files changed, 151 insertions(+), 17 deletions(-) diff --git a/docs/subsystems/agent-wiring.md b/docs/subsystems/agent-wiring.md index 511c5d6c9..092f2ec24 100644 --- a/docs/subsystems/agent-wiring.md +++ b/docs/subsystems/agent-wiring.md @@ -108,7 +108,7 @@ something else shows up as drift. - Codex's model picker tags each of its own models by where a pick on the Agents page sends it: via magpie while routed, joined, or failing over, and direct while it is only beside its sign-in on one of magpie's models (`beside`, not joined, failover off), where such a pick takes magpie out (#940) and the GUI asks first (`TestCodexBesideOwnModelsSayDirect`). - Every other agent's own models (`ownProvider` in [`agents.go`](../../internal/agent/agents.go), the provider list read from its own auth file or config: Pi, OpenCode, Goose, Crush, Cline, Hermes, the WSL twins and the rest; omp's `ompOwnOptions`) say `Direct` with the provider's name: each is asked of that vendor on the agent's own sign-in or key, so a pick takes the agent off magpie. The picker tags them direct, lists them after magpie's, and asks before such a pick turns 「接入」 off (maxleebeta on Discord: Pi's own OpenCode Go model switched it off with nothing said; `TestOwnModelsSayTheyGoDirect`, `agent-own-direct.test.cjs`). - A line magpie writes into an agent's TOML, YAML or dotenv config gets a comment line of its own just above it, `# magpie: ; ` (`edit.Mark`, set to `agent.lineNote` by the CLI's `run` in [`mark.go`](../../internal/agent/mark.go); the edit side in [`internal/edit/mark.go`](../../internal/edit/mark.go)). Jevik_AI on X deleted magpie's folders by hand, and Codex's `config.toml` kept `openai_base_url` on the gateway with nothing in it naming magpie: every turn was a 502. Only a one-line scalar that points at the gateway or names magpie's model list gets one, and only when the write adds or changes that line, or it had a note already; a line of the user's own that magpie leaves as it is gets none. The note is never on the value's line: Grok App's `providers.rs` reads the rest of a line as the value. Every write takes the notes out first and puts them back over the lines that still need one, so a note goes with its line and a Disconnect leaves none. A YAML note that would change what the file reads as isn't added (`sameYAMLValue`). JSON has no comments and gets none. Tests set no `Mark`, so their exact bytes stay; `TestMarkedLinesEveryAgent` connects every agent with notes on and checks each gateway line has one, a second sync writes nothing, and Disconnect leaves none; `TestCodexMarkedLines`, `internal/edit`'s `TestMark*`. -- Codex signed in to ChatGPT stays beside its sign-in (`openai_base_url`) unless the user picks magpie API. magpie becomes its provider only while the Codex app holds the active account, which it sends nothing for: `/wham/usage` says it isn't allowed, and it has no credits and isn't a workspace still within its overage (`codexHeld`, read by `provider.CodexUsedUp`). With Codex switched off or removed in magpie (`leftAlone`), whose allowance magpie otherwise reads nothing of, the active account is still read for this alone while Codex takes magpie's models beside its sign-in or magpie is its provider for the held account (`Agent.HoldMatters`, through the `provider.CodexHoldMatters` hook; `codexHoldReading`): once a minute at most, with the token as Codex keeps it, never renewed or written (`codexTokenAsKept`), and nothing shown on the Usage page. Before, turning Codex off in magpie after its allowance ran out left the Codex app greying Send for magpie's models through restarts, and one already moved was put back on the held account (sherlock_23333 on X; `TestCodexHoldReadLeftAlone`, `TestCodexHoldMatters`). Where the app goes by what usage can't show (an experiment's gate, a reserve, a Team plan's overage under the reserve experiment), it counts as held: a wrong move costs ChatGPT extras, a missed one every turn. A saved reading kept through a failed read stops saying held once its used-up window has started again; a hold with no window used up stays till a fresh read. The app then loses its ChatGPT state (durable threads, remote control), so a window at 100% alone, which credits get past, doesn't move it (with no credits it does when the backend says `allowed` false, and only then: `codexAtLimit`. The app's composer gate greys Send on that alone, in ChatGPT.app 26.1029 as in 26.1021, and keeps a window of an allowed account at 1% or more, so allowed not said or allowed with a window at 100% is an account it still sends for. A used-up 5h window with the week open is held through it: the backend says allowed false there, as the real Team reply in testdata does with its 5h at 100% and its week at 63%; xiaoq6285 on Discord) (`codex.out` marks the move, and Sync undoes it once the account has room). The same holds on one of Codex's own models joined beside the sign-in (MasterYi on Discord: the Codex app greyed Send for every model on a used-up account): Sync makes magpie the provider on that model as magpie serves it (`codexOwnViaMagpie`, `codex/`), keeps the model picked in `codex.out_own`, and once the account has room puts Codex back joined on it; a model the user picked meanwhile stays theirs. With no ChatGPT account in magpie to serve it, Codex is left as it is (`TestCodexOwnModelGoesThroughMagpieWhileUsedUp`, `TestCodexOwnModelKeptOnChatGPT`). Where magpie would serve that model on the same used-up account alone (no fallback on the Codex subscription and no other ChatGPT account not known out: `provider.CodexServesWhileOut` false), every turn on it would be refused too, but the Codex app greys Send for every model while the account is out, so keeping the account's menu costs a grey Send (shamen, chenhan and xiaoq6285 on Discord). Sync then makes magpie the provider on a stand-in model of magpie's that is on no ChatGPT account (`codexOtherModel`, kept in `codex.out_stand` beside `codex.out_own`), once per held/unheld transition: on the stand-in the model is magpie's, so the branch isn't met again and a steady held state writes nothing and ends no app-server. At the reset Codex is put back joined on its own model; another model picked meanwhile stays theirs (the stand-in itself picked can't be told from staying on it, and gives way to the own model). This reverses f37742e14, which left Codex on its sign-in to keep the account's menu and usage (the app-server restarts it cost, #4550, are the two the takeover makes). With nothing else in magpie to offer (no stand-in) Codex stays on its sign-in, as no takeover could send (`TestCodexOwnModelOneSpentAccountStaysOnItsSignIn`, `TestCodexOwnModelOneSpentAccountTakenOverForOtherModels`, `TestCodexServesWhileOut`). The account is read again at once when a reply says it may be out (a `usage_limit_reached` rest, or an `x-codex-*` window heard at 100%; `provider.CodexMayBeUsedUp`, at most every 20s, the active account only), not only on the five-minute look. A turn relayed to Codex's own sign-in, the one account on (`codexUpstreamOn` with no account, the gateway's `codexRelayedAccount`), is heard the same way: its `x-codex-*` windows and `codex.rate_limits` event kept as the sign-in's usage (`heardCodexLimits`), and a `usage_limit_reached` refusal reading it again. Before, only turns served through magpie's accounts were, so with one account magpie learned the account was out only at the next look, minutes on, and the Codex app kept Send greyed until it was restarted (unsplashz on Discord; `TestCodexUsedUpRelayedAsksAtOnce`, `TestCodexRelayedWindowsHeard`). A Codex started before magpie became its provider keeps sending its own model's turns to its built-in provider; while the sign-in is known out (`provider.CodexUsedUpKnown`) and the request signs as it, those go through routing as `codex/` (`codexAccounts`), as a restarted Codex's do (`TestCodexHeldSignInServedWithoutRestart`). The Codex app greys Send for every model, magpie's too, while its `authMethod` is `chatgpt` and its rate limit says not allowed (its composer's gate in ChatGPT.app 26.1013), and reads `authMethod` from its app-server's `account/read` only as the app-server starts; with magpie the provider (`requires_openai_auth` false) that answers no account, so the gate lifts. So the gateway process restarts the app's own app-server each time magpie becomes Codex's provider for the used-up account (`Agent.AppOut`) and again when Codex is put back on the account after its reset, and likewise whenever what the app-server reads as it starts and magpie writes changes, which is where its requests go (`model_provider`, `openai_base_url` and the magpie table's `base_url`, and its `requires_openai_auth` while that table is Codex's `model_provider`: beside the sign-in the app-server reads the built-in provider's account, so the key following the account out and back is nothing it reads; `codexAppRoute`, through the `provider.CodexAppRoute` hook): a Disconnect, a Connect, failover written or taken out. The model list is not part of it: neither `model_catalog_json` nor what the catalog holds. The catalog follows magpie's names and the effort new sessions start at, which the Codex app saves on each pick, and the ChatGPT app takes an ended app-server as a crash and relaunches, so a route that hashed the catalog (from lucis0116 on X until #4550) ended the app every few minutes (indekkusu141209 on X). A new list is read at the app's next launch or on the Agents row's Restart (`StaleCopies`) A list that drops a model and has it back at the next fetch, the same list in another order, and a catalog read half-written leave the route as it was (`TestCodexEffortPickKeepsTheAppRunning`, `TestCodexAppRouteIgnoresARename`, `TestCodexAppRouteKeepsThroughListFlaps`). Before this an app-server started while Codex was connected kept sending to the gateway after Disconnect until the app was restarted (JiangLiuer222 on X). At the watcher's first look only a used-up account's app-servers are ended, since what the others read before magpie started isn't known. What magpie changes by itself that the app works without waits for the user, though, unless they ticked Restart the Codex app when magpie switches its account back (`Provider.RestartApp`, `provider/restartapp`, off by default, this computer's own like KeepLogin): its switch back once the account it moved off has room (`SwitchWhenSpent` → `switchLoginBy(…, true)`, `codexApp.auto`) and giving Codex its account back at the reset. Those leave the app-server running, log why, and the switch notice's Restart is the way to follow; a switch the user makes, a Connect, a Disconnect and failover written or taken out still restart it. So does whatever leaves the app-server on a used-up account, tick or not: the takeover in, the first look on one, and magpie's switch off one (`needed` in `codexAppStep`), since the app greys Send there and reads the account only as its app-server starts, so with the tick off a takeover at the limit left Send grey and the fallback unreachable (v0.1.2826 to its fix). An app-server left running on the account magpie switched Codex back off is restarted too once that account is read held (`restartCodexAppLeftOnSpent`, on each full look of `KeepOnAnAccountWithRoom`, once per app-server left): it keeps the account it started on (`CodexAppStale`), so when that ran out the app greyed Send for every model while the account Codex is signed in to had room and nothing else read the old one (nb5p on Discord: B first in magpie, the app still on A; `TestCodexAppLeftOnASpentAccountIsRestarted`). atom0964 on Discord had both sign-in ticks off and the ChatGPT app relaunched at every switch between two Plus accounts (`TestCodexAppLeftRunningForMagpiesOwnChanges`, `TestCodexAppFirstLookOnAUsedUpAccountRestarts`, `restart-app.test.cjs`). It runs on every system (`KeepCodexAppOnItsProvider`, every 3s; Ethereal499 and Isdavexxx on X, MasterYi on Discord, #540: the earlier once-only reopen ran on macOS from the GUI process only, so Windows, Linux, a GUI-less magpie and the switch back stayed greyed). It ends only app-servers on magpie's Codex home (`codexAppHomeIs`: the process's `CODEX_HOME`, else `HOME/.codex`, read by `proc.EnvOf`; Windows can't tell and counts every one), only once no Codex request is in flight through the gateway nor ended in the last 10s (`Server.CodexBusy`) and no session under the Codex home has a turn started and not ended (`codexTurnOpen`), and with a clean exit (`proc.End`: SIGTERM, `TerminateProcess` with code 0), since the app shows any other exit as a crash. Each end is logged with what made the app-server stale: the account switched, the route changed, or the used-up takeover (`codex: ended the Codex app's app-server [pid] for the app to start another: `), so a crash a user reports can be put down to one of them (#4550, `TestCodexAppEndSaysWhy`). The app starts another app-server at once and keeps its window, threads and drafts (tried in the real app in a sandbox `CODEX_HOME`, both ways). An app-server that outlives three ends, or may not be ended, leaves the row saying to quit and reopen the app (`CodexAppStuck`). An ended app-server's pid gone doesn't mean the app has another: the Windows app (MSIX `OpenAI.Codex`, stdio) starts one only once the old one's stdout closes, and a child it left (an MCP server, a tool command) holds that pipe open, so the app answered "Codex app-server process is not available" until it was restarted (蛋仔 on Discord). The watcher waits `codexAppBackWait` (30s) for another app-server; when none came and the app is still open (`CodexAppOpen`), it quits and opens the app as the Agents row's Restart does (`CodexAppReopen`, Windows only), else the row says to (`CodexAppStuck`). `RestartCodexApp`, uninstall's `RestartCodexAppWhenIdle` and the switch notice's Restart (`RestartSwitchedCodexApp`) wait for it the same way and reopen or say so alike, and `recheckLeft` keeps the record while the app is open with no app-server. A signed-in ChatGPT app (26.1021) can still fail on the app-server it starts next ("Hosted AppServerManager identity is not ready") and show "ChatGPT hit a snag" until it is quit and opened again (ysm119 on Discord, #4550), a fault in the app the clean end can't avoid. So for `codexAppCrashWait` (60s) after each of these ends ([`codex_app_crash.go`](../../internal/provider/codex_app_crash.go)) the app's own logs are read (`CodexAppCrashed`, [`agent/codex_app_crash.go`](../../internal/agent/codex_app_crash.go): `~/Library/Logs/com.openai.codex`, `%LOCALAPPDATA%\Codex\Logs` and the Store package's, `$XDG_STATE_HOME/codex/logs`) for a whole `fatal_error_broadcasted` line stamped after the end. When the page shows and no request has gone through magpie or been in flight since, and no Codex turn has opened since, the app is quit and opened once (`CodexAppRelaunch`: ⌘Q's quit and `open` on macOS, never forced, ChatGPT.app by its bundle id only; `CodexAppReopen` on Windows); where that can't be done (Linux, an app that didn't quit) the Agents row says to quit and reopen it (`CodexAppSnagged`, `appSnag`) until the app-servers seen then are gone or the app is closed. Logs that can't be read reopen nothing (`TestCodexAppReopenedWhenItShowsItsErrorPage`, `TestCodexAppInUseAfterItsErrorPageIsLeft`, `TestCodexAppCrashedReadsItsOwnLogs`, `TestCodexAppRelaunchOnlyTheCodexApp`, `codex-app-snag.test.cjs`). A turn left open in Codex's sessions holds the restart for up to `codexTurnWindow` (30m), except where the app-server is on a used-up account: after a switch off one (`SwitchWhenSpent` → `codexAppSpentSwitch`, or a switch by hand off an account last read held, `codexHeldKnown` in `noteCodexSwitch`) and when magpie becomes Codex's provider for one (`codexAppStep`'s `into`; xiaoq6285 on Discord: quota out, then another account or provider picked, and Send stayed grey; `TestCodexAppServerRestartedPastATurnStuckWhenMagpieTakesOver`, `TestCodexHandSwitchOffAHeldAccountIsSpent`). There a turn that has written no session file and sent nothing through magpie for `codexTurnStuckAfter` (2m) counts stuck and the app-server is restarted past it, since its next request would be refused on that account (蛋仔 on Discord: quota ran out mid-task and the app hung till restarted; `TestCodexAppServerRestartedPastATurnStuckOnTheSpentAccount`). An account switch restarts nothing, and neither the app nor the background daemon is said to be left on the old account (`CodexAppStale`, `CodexDaemonStale`, the CLI's switch line), while the provider Codex's config.toml names takes no ChatGPT account (`provider.CodexNoAccount`, `codexNoAccount` in [`agents.go`](../../internal/agent/agents.go)): a top-level `model_provider` other than `openai` whose `[model_providers]` table doesn't say `requires_openai_auth = true`, such as magpie's with the user's own `false` (danzi002 on Discord). Codex then sends the table's key and its app-server reads no account (codex-rs `account_state`, `resolve_provider_auth`); tried with codex-cli 0.165.0 in a sandbox `CODEX_HOME`: `account/read` and `getAuthStatus` said no account before and after auth.json was switched, and `codex exec` sent `Bearer` the table's key on both accounts. A route change still restarts it, and so does a switch under a profile, the built-in provider, a provider with no table or a config that can't be read (`TestCodexNoAccountSwitchRestartsNothing`, `TestCodexNoAccountOfEachConfig`, `TestCodexNoAccountFollowsRequiresOpenAIAuth`). Until the reset the app's ChatGPT side is off (cloud tasks, its usage meter, Log out); the Agents row says so with the reset's time (`appOut` in `/api/state`, `CodexOutUntil`), and it all comes back at the reset. While the active account is held or a window is at 90% or more, its usage is read every minute rather than every five (`codexLookEvery`). `TestCodexAppServerRestartedEachWay`, `TestCodexAppServerRestartedOnDisconnect`, `TestCodexAppRouteFollowsConnectAndDisconnect`, `TestCodexEffortPickKeepsTheAppRunning`, `TestCodexAppRouteIgnoresARename`, `TestCodexAppServerUnknown`, `TestCodexAppServerStuck`, `TestCodexAppServersOnHome`, `TestCodexTurnOpen`, `TestCodexBusy`, `codex-app-out.test.cjs`. +- Codex signed in to ChatGPT stays beside its sign-in (`openai_base_url`) unless the user picks magpie API. magpie becomes its provider only while the Codex app holds the active account, which it sends nothing for: `/wham/usage` says it isn't allowed, and it has no credits and isn't a workspace still within its overage (`codexHeld`, read by `provider.CodexUsedUp`). With Codex switched off or removed in magpie (`leftAlone`), whose allowance magpie otherwise reads nothing of, the active account is still read for this alone while Codex takes magpie's models beside its sign-in or magpie is its provider for the held account (`Agent.HoldMatters`, through the `provider.CodexHoldMatters` hook; `codexHoldReading`): once a minute at most, with the token as Codex keeps it, never renewed or written (`codexTokenAsKept`), and nothing shown on the Usage page. Before, turning Codex off in magpie after its allowance ran out left the Codex app greying Send for magpie's models through restarts, and one already moved was put back on the held account (sherlock_23333 on X; `TestCodexHoldReadLeftAlone`, `TestCodexHoldMatters`). Where the app goes by what usage can't show (an experiment's gate, a reserve, a Team plan's overage under the reserve experiment), it counts as held: a wrong move costs ChatGPT extras, a missed one every turn. A saved reading kept through a failed read stops saying held once its used-up window has started again; a hold with no window used up stays till a fresh read. The app then loses its ChatGPT state (durable threads, remote control), so a window at 100% alone, which credits get past, doesn't move it (with no credits it does when the backend says `allowed` false, and only then: `codexAtLimit`. The app's composer gate greys Send on that alone, in ChatGPT.app 26.1029 as in 26.1021, and keeps a window of an allowed account at 1% or more, so allowed not said or allowed with a window at 100% is an account it still sends for. A used-up 5h window with the week open is held through it: the backend says allowed false there, as the real Team reply in testdata does with its 5h at 100% and its week at 63%; xiaoq6285 on Discord) (`codex.out` marks the move, and Sync undoes it once the account has room). The same holds on one of Codex's own models joined beside the sign-in (MasterYi on Discord: the Codex app greyed Send for every model on a used-up account): Sync makes magpie the provider on that model as magpie serves it (`codexOwnViaMagpie`, `codex/`), keeps the model picked in `codex.out_own`, and once the account has room puts Codex back joined on it; a model the user picked meanwhile stays theirs. With no ChatGPT account in magpie to serve it, Codex is left as it is (`TestCodexOwnModelGoesThroughMagpieWhileUsedUp`, `TestCodexOwnModelKeptOnChatGPT`). Where magpie would serve that model on the same used-up account alone (no fallback on the Codex subscription and no other ChatGPT account not known out: `provider.CodexServesWhileOut` false), every turn on it would be refused too, but the Codex app greys Send for every model while the account is out, so keeping the account's menu costs a grey Send (shamen, chenhan and xiaoq6285 on Discord). Sync then makes magpie the provider on a stand-in model of magpie's that is on no ChatGPT account (`codexOtherModel`, kept in `codex.out_stand` beside `codex.out_own`), once per held/unheld transition: on the stand-in the model is magpie's, so the branch isn't met again and a steady held state writes nothing and ends no app-server. At the reset Codex is put back joined on its own model; another model picked meanwhile stays theirs (the stand-in itself picked can't be told from staying on it, and gives way to the own model). This reverses f37742e14, which left Codex on its sign-in to keep the account's menu and usage (the app-server restarts it cost, #4550, are the two the takeover makes). With nothing else in magpie to offer (no stand-in) Codex stays on its sign-in, as no takeover could send (`TestCodexOwnModelOneSpentAccountStaysOnItsSignIn`, `TestCodexOwnModelOneSpentAccountTakenOverForOtherModels`, `TestCodexServesWhileOut`). The account is read again at once when a reply says it may be out (a `usage_limit_reached` rest, or an `x-codex-*` window heard at 100%; `provider.CodexMayBeUsedUp`, at most every 20s, the active account only), not only on the five-minute look. A turn relayed to Codex's own sign-in, the one account on (`codexUpstreamOn` with no account, the gateway's `codexRelayedAccount`), is heard the same way: its `x-codex-*` windows and `codex.rate_limits` event kept as the sign-in's usage (`heardCodexLimits`), and a `usage_limit_reached` refusal reading it again. Before, only turns served through magpie's accounts were, so with one account magpie learned the account was out only at the next look, minutes on, and the Codex app kept Send greyed until it was restarted (unsplashz on Discord; `TestCodexUsedUpRelayedAsksAtOnce`, `TestCodexRelayedWindowsHeard`). A Codex started before magpie became its provider keeps sending its own model's turns to its built-in provider; while the sign-in is known out (`provider.CodexUsedUpKnown`) and the request signs as it, those go through routing as `codex/` (`codexAccounts`), as a restarted Codex's do (`TestCodexHeldSignInServedWithoutRestart`). The Codex app greys Send for every model, magpie's too, while its `authMethod` is `chatgpt` and its rate limit says not allowed (its composer's gate in ChatGPT.app 26.1013), and reads `authMethod` from its app-server's `account/read` only as the app-server starts; with magpie the provider (`requires_openai_auth` false) that answers no account, so the gate lifts. So the gateway process restarts the app's own app-server each time magpie becomes Codex's provider for the used-up account (`Agent.AppOut`) and again when Codex is put back on the account after its reset, and likewise whenever what the app-server reads as it starts and magpie writes changes, which is where its requests go (`model_provider`, `openai_base_url` and the magpie table's `base_url`, and its `requires_openai_auth` while that table is Codex's `model_provider`: beside the sign-in the app-server reads the built-in provider's account, so the key following the account out and back is nothing it reads; `codexAppRoute`, through the `provider.CodexAppRoute` hook): a Disconnect, a Connect, failover written or taken out. The model list is not part of it: neither `model_catalog_json` nor what the catalog holds. The catalog follows magpie's names and the effort new sessions start at, which the Codex app saves on each pick, and the ChatGPT app takes an ended app-server as a crash and relaunches, so a route that hashed the catalog (from lucis0116 on X until #4550) ended the app every few minutes (indekkusu141209 on X). A new list is read at the app's next launch or on the Agents row's Restart (`StaleCopies`) A list that drops a model and has it back at the next fetch, the same list in another order, and a catalog read half-written leave the route as it was (`TestCodexEffortPickKeepsTheAppRunning`, `TestCodexAppRouteIgnoresARename`, `TestCodexAppRouteKeepsThroughListFlaps`). Before this an app-server started while Codex was connected kept sending to the gateway after Disconnect until the app was restarted (JiangLiuer222 on X). At the watcher's first look only a used-up account's app-servers are ended, since what the others read before magpie started isn't known. What magpie changes by itself that the app works without waits for the user, though, unless they ticked Restart the Codex app when magpie switches its account back (`Provider.RestartApp`, `provider/restartapp`, off by default, this computer's own like KeepLogin): its switch back once the account it moved off has room (`SwitchWhenSpent` → `switchLoginBy(…, true)`, `codexApp.auto`) and giving Codex its account back at the reset. Those leave the app-server running, log why, and the switch notice's Restart is the way to follow; a switch the user makes, a Connect, a Disconnect and failover written or taken out still restart it, and so does the takeover turned off by the user rather than ended by the reset: its sign-in set to Always ChatGPT (the takeover note's Keep on ChatGPT) or Codex disconnected while magpie stood in (`Agent.AppOutOff` through `provider.CodexTakeoverOff`, asked only as `out` goes false). The app-server the takeover started answers `account/read` with no account, so the app kept its ChatGPT projects and chats hidden after the user turned the takeover off to have them back, and the Windows app has no Restart of its own (Herzz____ on X, `TestCodexAppRestartedWhenTheTakeoverIsTurnedOff`). So does whatever leaves the app-server on a used-up account, tick or not: the takeover in, the first look on one, and magpie's switch off one (`needed` in `codexAppStep`), since the app greys Send there and reads the account only as its app-server starts, so with the tick off a takeover at the limit left Send grey and the fallback unreachable (v0.1.2826 to its fix). An app-server left running on the account magpie switched Codex back off is restarted too once that account is read held (`restartCodexAppLeftOnSpent`, on each full look of `KeepOnAnAccountWithRoom`, once per app-server left): it keeps the account it started on (`CodexAppStale`), so when that ran out the app greyed Send for every model while the account Codex is signed in to had room and nothing else read the old one (nb5p on Discord: B first in magpie, the app still on A; `TestCodexAppLeftOnASpentAccountIsRestarted`). atom0964 on Discord had both sign-in ticks off and the ChatGPT app relaunched at every switch between two Plus accounts (`TestCodexAppLeftRunningForMagpiesOwnChanges`, `TestCodexAppFirstLookOnAUsedUpAccountRestarts`, `restart-app.test.cjs`). It runs on every system (`KeepCodexAppOnItsProvider`, every 3s; Ethereal499 and Isdavexxx on X, MasterYi on Discord, #540: the earlier once-only reopen ran on macOS from the GUI process only, so Windows, Linux, a GUI-less magpie and the switch back stayed greyed). It ends only app-servers on magpie's Codex home (`codexAppHomeIs`: the process's `CODEX_HOME`, else `HOME/.codex`, read by `proc.EnvOf`; Windows can't tell and counts every one), only once no Codex request is in flight through the gateway nor ended in the last 10s (`Server.CodexBusy`) and no session under the Codex home has a turn started and not ended (`codexTurnOpen`), and with a clean exit (`proc.End`: SIGTERM, `TerminateProcess` with code 0), since the app shows any other exit as a crash. Each end is logged with what made the app-server stale: the account switched, the route changed, or the used-up takeover (`codex: ended the Codex app's app-server [pid] for the app to start another: `), so a crash a user reports can be put down to one of them (#4550, `TestCodexAppEndSaysWhy`). The app starts another app-server at once and keeps its window, threads and drafts (tried in the real app in a sandbox `CODEX_HOME`, both ways). An app-server that outlives three ends, or may not be ended, leaves the row saying to quit and reopen the app (`CodexAppStuck`). An ended app-server's pid gone doesn't mean the app has another: the Windows app (MSIX `OpenAI.Codex`, stdio) starts one only once the old one's stdout closes, and a child it left (an MCP server, a tool command) holds that pipe open, so the app answered "Codex app-server process is not available" until it was restarted (蛋仔 on Discord). The watcher waits `codexAppBackWait` (30s) for another app-server; when none came and the app is still open (`CodexAppOpen`), it quits and opens the app as the Agents row's Restart does (`CodexAppReopen`, Windows only), else the row says to (`CodexAppStuck`). `RestartCodexApp`, uninstall's `RestartCodexAppWhenIdle` and the switch notice's Restart (`RestartSwitchedCodexApp`) wait for it the same way and reopen or say so alike, and `recheckLeft` keeps the record while the app is open with no app-server. A signed-in ChatGPT app (26.1021) can still fail on the app-server it starts next ("Hosted AppServerManager identity is not ready") and show "ChatGPT hit a snag" until it is quit and opened again (ysm119 on Discord, #4550), a fault in the app the clean end can't avoid. So for `codexAppCrashWait` (60s) after each of these ends ([`codex_app_crash.go`](../../internal/provider/codex_app_crash.go)) the app's own logs are read (`CodexAppCrashed`, [`agent/codex_app_crash.go`](../../internal/agent/codex_app_crash.go): `~/Library/Logs/com.openai.codex`, `%LOCALAPPDATA%\Codex\Logs` and the Store package's, `$XDG_STATE_HOME/codex/logs`) for a whole `fatal_error_broadcasted` line stamped after the end. When the page shows and no request has gone through magpie or been in flight since, and no Codex turn has opened since, the app is quit and opened once (`CodexAppRelaunch`: ⌘Q's quit and `open` on macOS, never forced, ChatGPT.app by its bundle id only; `CodexAppReopen` on Windows); where that can't be done (Linux, an app that didn't quit) the Agents row says to quit and reopen it (`CodexAppSnagged`, `appSnag`) until the app-servers seen then are gone or the app is closed. Logs that can't be read reopen nothing (`TestCodexAppReopenedWhenItShowsItsErrorPage`, `TestCodexAppInUseAfterItsErrorPageIsLeft`, `TestCodexAppCrashedReadsItsOwnLogs`, `TestCodexAppRelaunchOnlyTheCodexApp`, `codex-app-snag.test.cjs`). A turn left open in Codex's sessions holds the restart for up to `codexTurnWindow` (30m), except where the app-server is on a used-up account: after a switch off one (`SwitchWhenSpent` → `codexAppSpentSwitch`, or a switch by hand off an account last read held, `codexHeldKnown` in `noteCodexSwitch`) and when magpie becomes Codex's provider for one (`codexAppStep`'s `into`; xiaoq6285 on Discord: quota out, then another account or provider picked, and Send stayed grey; `TestCodexAppServerRestartedPastATurnStuckWhenMagpieTakesOver`, `TestCodexHandSwitchOffAHeldAccountIsSpent`). There a turn that has written no session file and sent nothing through magpie for `codexTurnStuckAfter` (2m) counts stuck and the app-server is restarted past it, since its next request would be refused on that account (蛋仔 on Discord: quota ran out mid-task and the app hung till restarted; `TestCodexAppServerRestartedPastATurnStuckOnTheSpentAccount`). An account switch restarts nothing, and neither the app nor the background daemon is said to be left on the old account (`CodexAppStale`, `CodexDaemonStale`, the CLI's switch line), while the provider Codex's config.toml names takes no ChatGPT account (`provider.CodexNoAccount`, `codexNoAccount` in [`agents.go`](../../internal/agent/agents.go)): a top-level `model_provider` other than `openai` whose `[model_providers]` table doesn't say `requires_openai_auth = true`, such as magpie's with the user's own `false` (danzi002 on Discord). Codex then sends the table's key and its app-server reads no account (codex-rs `account_state`, `resolve_provider_auth`); tried with codex-cli 0.165.0 in a sandbox `CODEX_HOME`: `account/read` and `getAuthStatus` said no account before and after auth.json was switched, and `codex exec` sent `Bearer` the table's key on both accounts. A route change still restarts it, and so does a switch under a profile, the built-in provider, a provider with no table or a config that can't be read (`TestCodexNoAccountSwitchRestartsNothing`, `TestCodexNoAccountOfEachConfig`, `TestCodexNoAccountFollowsRequiresOpenAIAuth`). Until the reset the app's ChatGPT side is off (cloud tasks, its usage meter, Log out); the Agents row says so with the reset's time (`appOut` in `/api/state`, `CodexOutUntil`), and it all comes back at the reset. While the active account is held or a window is at 90% or more, its usage is read every minute rather than every five (`codexLookEvery`). `TestCodexAppServerRestartedEachWay`, `TestCodexAppServerRestartedOnDisconnect`, `TestCodexAppRouteFollowsConnectAndDisconnect`, `TestCodexEffortPickKeepsTheAppRunning`, `TestCodexAppRouteIgnoresARename`, `TestCodexAppServerUnknown`, `TestCodexAppServerStuck`, `TestCodexAppServersOnHome`, `TestCodexTurnOpen`, `TestCodexBusy`, `codex-app-out.test.cjs`. - Moving a field from one of magpie's models to another (the agent's own picker) is not drift. Moving it off magpie is. - Claude Desktop applying a configLibrary profile other than magpie's is not drift when that profile is a gateway profile for this magpie's gateway (`desktopAppliedGateway` in [`claudedesktop.go`](../../internal/agent/claudedesktop.go)): Desktop's "Import configuration" adds a file exported on another computer, magpie's profile there with the user's connectors (`managedMcpServers`), as a new entry under a random id (aa86222 on Discord). magpie keeps it applied on Reconnect and through Sync, and writes in it only `inferenceGatewayApiKey` (with a missing auth scheme) when the gateway would refuse its key (`desktopKeyApplied`; magpie's own keys count as taken, `desktopKeyTaken`). A profile for another gateway, CC Switch's among them, is still drift, and Reconnect applies magpie's. The first switch-on still starts magpie's profile from an applied one (#2035). `TestClaudeDesktopImportedProfileIsOnMagpie`. - Claude Desktop 4.4212 (10-29) seals the secrets of its configLibrary profiles at rest: at every start it writes `inferenceGatewayApiKey` (and every other string secret its schema drops on export) back as `__sealed__:v1:` + base64 of `{"configLibrary":{"key":…,"value":…}}` behind `anthropic.claude.crossProfileSecret.v1:`, protected with the user's DPAPI on Windows (entropy `anthropic.claude.crossProfileSecret.v1`, claude-native's `userDpapiProtect`) and Electron's safeStorage (the Keychain) on macOS, and it sends the gateway the key as it was. `desktopOpen` ([`desktopseal.go`](../../internal/agent/desktopseal.go)) reads the key as Desktop does: on Windows it opens the seal (`desktopUnprotect`, `CryptUnprotectData`, never prompting) and judges what is in it; a seal it can't open (macOS, another user's) is unknown, so Check doesn't call it changed and `desktopKeyApplied` doesn't write over it. Before this the key read as changed after every Desktop start and Reconnect only wrote it in the clear for Desktop to seal again (a Windows user on Discord). A key in the clear that isn't one the gateway takes, a missing key and another address are still drift (`TestClaudeDesktopSealedKeyIsNotChanged`, `TestClaudeDesktopImportedSealedKeyStays`, `TestClaudeDesktopSealedKeyOpened` on Windows). diff --git a/internal/agent/agent.go b/internal/agent/agent.go index 8dd998481..054021eb3 100644 --- a/internal/agent/agent.go +++ b/internal/agent/agent.go @@ -201,6 +201,11 @@ type Agent struct { // its sign-in only as it opens, sends nothing on it till it is opened // again (MasterYi on Discord). AppOut func() bool + // AppOutOff reports the user has turned the takeover (AppOut) off: + // Codex's sign-in set to Always ChatGPT, or Codex not connected. The + // takeover ending so is the user's change, not the reset's, and the + // Codex app's app-server is restarted for it (CodexTakeoverOff). + AppOutOff func() bool // HoldMatters reports Codex takes magpie's models beside a ChatGPT // sign-in, so that magpie is its provider while the Codex app holds // the account (out), or is that already: what the app holds is read diff --git a/internal/agent/agents.go b/internal/agent/agents.go index e7a21a756..35eb92b1c 100644 --- a/internal/agent/agents.go +++ b/internal/agent/agents.go @@ -56,6 +56,13 @@ func init() { // but not when Codex's account is switched while its provider takes // no ChatGPT account: the app-server goes by none provider.CodexNoAccount = codexNoAccount + // and whether the takeover was turned off by the user rather than + // ended by the reset (Keep on ChatGPT, Always ChatGPT, a Disconnect) + provider.CodexTakeoverOff = func() bool { + home, _ := homes() + a := codex(home) + return a.AppOutOff != nil && a.AppOutOff() + } // and when none starts in place of the one ended, the app is // reopened on Windows, where its stdout was held open provider.CodexAppOpen = codexAppOpen diff --git a/internal/agent/codex.go b/internal/agent/codex.go index 4f868ef3f..d6d993c16 100644 --- a/internal/agent/codex.go +++ b/internal/agent/codex.go @@ -1939,8 +1939,9 @@ func codexIn(at place) *Agent { // mark stays for Sync to put it back beside the sign-in: the // Agents row said "its own models stay in /model" while the Codex // app was meant to be in its API state (#2267) - Joined: func() bool { return joined() && !asProvider() }, - AppOut: func() bool { return asProvider() && !api() && chatGPT() && stashShared()[at.key("codex.out")] == "1" }, + Joined: func() bool { return joined() && !asProvider() }, + AppOut: func() bool { return asProvider() && !api() && chatGPT() && stashShared()[at.key("codex.out")] == "1" }, + AppOutOff: func() bool { return stashShared()[at.key("codex.login")] == "chatgpt" || !routed() }, HoldMatters: func() bool { return stashShared()[at.key("codex.login")] == "" && chatGPT() && (viaBase() || asProvider() && stashShared()[at.key("codex.out")] == "1") diff --git a/internal/agent/codex_usedup_test.go b/internal/agent/codex_usedup_test.go index e4f4a937b..cf5da1e5d 100644 --- a/internal/agent/codex_usedup_test.go +++ b/internal/agent/codex_usedup_test.go @@ -165,6 +165,10 @@ func TestCodexOwnModelGoesThroughMagpieWhileUsedUp(t *testing.T) { if cx.AppOut() || !cx.Joined() { t.Fatalf("allowance back: AppOut %v, Joined %v", cx.AppOut(), cx.Joined()) } + // the reset ended the takeover, not the user (CodexTakeoverOff) + if cx.AppOutOff() { + t.Fatal("allowance back: the takeover reads as turned off by the user") + } // a model the user picked while out stays theirs when room comes back codexUsedUp = func() bool { return true } @@ -181,6 +185,22 @@ func TestCodexOwnModelGoesThroughMagpieWhileUsedUp(t *testing.T) { if cfg := read(); !strings.Contains(cfg, `model = "fake/m1"`) { t.Fatalf("picked while out, allowance back:\n%s", cfg) } + + // taken over again, then Codex disconnected: the user's end of it + codexUsedUp = func() bool { return true } + t.Cleanup(func() { codexUsedUp = func() bool { return false } }) + if err := cx.Sync(); err != nil { + t.Fatal(err) + } + if !cx.AppOut() || cx.AppOutOff() { + t.Fatalf("taken over again: AppOut %v, AppOutOff %v:\n%s", cx.AppOut(), cx.AppOutOff(), read()) + } + if err := cx.Disconnect(); err != nil { + t.Fatal(err) + } + if cx.AppOut() || !cx.AppOutOff() { + t.Fatalf("disconnected: AppOut %v, AppOutOff %v:\n%s", cx.AppOut(), cx.AppOutOff(), read()) + } } // One ChatGPT account, out, on one of Codex's own models joined beside the @@ -351,9 +371,17 @@ func TestCodexOwnModelOutThenKeptOnChatGPT(t *testing.T) { if !cx.AppOut() { t.Fatalf("used up: not AppOut:\n%s", read()) } + if cx.AppOutOff() { + t.Fatal("taken over: the takeover reads as turned off") + } if err := cx.Field("login").Set("chatgpt"); err != nil { t.Fatal(err) } + // the user's own end of the takeover, which restarts the Codex app's + // app-server so its ChatGPT projects come back (CodexTakeoverOff) + if !cx.AppOutOff() { + t.Fatal("kept on ChatGPT: the takeover doesn't read as turned off by the user") + } onSignIn := func(when string) { t.Helper() if cfg := read(); strings.Contains(cfg, "model_provider =") || strings.Contains(cfg, "model_catalog_json") || !strings.Contains(cfg, `model = "gpt-a"`) { diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 8521e85e7..87ef8c1fe 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -2041,7 +2041,7 @@ function appOutSaid(a) { // still on the app-server it started while magpie was its provider, which // reads the account only as it starts (a bug report by mail, 11-04) function appBackSaid(a) { - return t("magpie gave {agent} back its ChatGPT account after the reset. The {agent} app still runs on the app-server it started while magpie served it, so it shows no account, no usage and none of its ChatGPT side until that restarts. Restart does it now; its window stays.", { agent: a.name }); + return t("magpie gave {agent} back its ChatGPT account after the reset. The {agent} app still runs on the app-server it started while magpie served it, so it shows no account, no usage and none of its ChatGPT projects or chats until that restarts. Restart does it now; its window stays.", { agent: a.name }); } // appBackRestart: the Codex app's app-server restarted, so it reads its diff --git a/internal/gui/assets/i18n.js b/internal/gui/assets/i18n.js index 3cbfefbc6..ecb640193 100644 --- a/internal/gui/assets/i18n.js +++ b/internal/gui/assets/i18n.js @@ -2276,7 +2276,7 @@ const I18N = { "Till then the {agent} app's ChatGPT side is off: its ChatGPT chats and projects, browser use (its Chrome control), cloud tasks, its account, its usage meter and Log out. They come back after the reset, once the app's app-server restarts: this row offers the restart then.": "在此之前,{agent} 应用的 ChatGPT 功能暂时关闭:ChatGPT 聊天和项目、浏览器操作(Chrome 控制)、云端任务、账号、用量显示和退出登录。额度重置后,等应用的 app-server 重启就会恢复;届时这一行会提供重启按钮。", "The {agent} app hit an error · quit it and open it again": "{agent} 应用出错了 · 请退出后重新打开", "Back on ChatGPT · restart the {agent} app to see its account": "已回到 ChatGPT · 重启 {agent} 应用才能看到账号", - "magpie gave {agent} back its ChatGPT account after the reset. The {agent} app still runs on the app-server it started while magpie served it, so it shows no account, no usage and none of its ChatGPT side until that restarts. Restart does it now; its window stays.": "额度重置后,magpie 已把 ChatGPT 账号还给 {agent}。但 {agent} 应用仍在用 magpie 接管期间启动的 app-server,所以在它重启前看不到账号、用量和其他 ChatGPT 功能。点“重启”即可立即重启;窗口保持不变。", + "magpie gave {agent} back its ChatGPT account after the reset. The {agent} app still runs on the app-server it started while magpie served it, so it shows no account, no usage and none of its ChatGPT projects or chats until that restarts. Restart does it now; its window stays.": "额度重置后,magpie 已把 ChatGPT 账号还给 {agent}。但 {agent} 应用仍在用 magpie 接管期间启动的 app-server,所以在它重启前看不到账号、用量以及 ChatGPT 项目和对话。点“重启”即可立即重启;窗口保持不变。", "Only its app-server restarts, for its ChatGPT account; its window stays. A running turn stops.": "只重启它的 app-server,以读取 ChatGPT 账号;窗口保持不变。正在运行的回合会停止。", "Keep on ChatGPT (its sign-in's Always ChatGPT) leaves {agent} on its ChatGPT account instead: it shows its own limit message till the reset, and magpie no longer takes it over.": "点「保持 ChatGPT」(即登录方式里的「始终 ChatGPT」)可让 {agent} 留在它的 ChatGPT 账号上:额度重置前它会显示自己的额度用尽提示,magpie 不再接管。", "Keep on ChatGPT": "保持 ChatGPT", @@ -7070,7 +7070,7 @@ const I18N = { "Till then the {agent} app's ChatGPT side is off: its ChatGPT chats and projects, browser use (its Chrome control), cloud tasks, its account, its usage meter and Log out. They come back after the reset, once the app's app-server restarts: this row offers the restart then.": "在此之前,{agent} 應用的 ChatGPT 功能暫時關閉:ChatGPT 聊天和專案、瀏覽器操作(Chrome 控制)、雲端任務、帳號、用量顯示和登出。額度重設後,等應用的 app-server 重新啟動就會恢復;屆時這一行會提供重新啟動按鈕。", "The {agent} app hit an error · quit it and open it again": "{agent} 應用程式出錯了 · 請結束後重新開啟", "Back on ChatGPT · restart the {agent} app to see its account": "已回到 ChatGPT · 重新啟動 {agent} 應用才能看到帳號", - "magpie gave {agent} back its ChatGPT account after the reset. The {agent} app still runs on the app-server it started while magpie served it, so it shows no account, no usage and none of its ChatGPT side until that restarts. Restart does it now; its window stays.": "額度重設後,magpie 已把 ChatGPT 帳號還給 {agent}。但 {agent} 應用仍在使用 magpie 接管期間啟動的 app-server,所以在它重新啟動前看不到帳號、用量和其他 ChatGPT 功能。點「重啟」即可立即重新啟動;視窗保持不變。", + "magpie gave {agent} back its ChatGPT account after the reset. The {agent} app still runs on the app-server it started while magpie served it, so it shows no account, no usage and none of its ChatGPT projects or chats until that restarts. Restart does it now; its window stays.": "額度重設後,magpie 已把 ChatGPT 帳號還給 {agent}。但 {agent} 應用仍在使用 magpie 接管期間啟動的 app-server,所以在它重新啟動前看不到帳號、用量以及 ChatGPT 專案和對話。點「重啟」即可立即重新啟動;視窗保持不變。", "Only its app-server restarts, for its ChatGPT account; its window stays. A running turn stops.": "只重新啟動它的 app-server,以讀取 ChatGPT 帳號;視窗保持不變。正在執行的回合會停止。", "Keep on ChatGPT (its sign-in's Always ChatGPT) leaves {agent} on its ChatGPT account instead: it shows its own limit message till the reset, and magpie no longer takes it over.": "點「保持 ChatGPT」(即登入方式裡的「始終 ChatGPT」)可讓 {agent} 留在它的 ChatGPT 帳號上:額度重設前它會顯示自己的額度用盡提示,magpie 不再接管。", "Keep on ChatGPT": "保持 ChatGPT", @@ -11840,7 +11840,7 @@ const I18N = { "Till then the {agent} app's ChatGPT side is off: its ChatGPT chats and projects, browser use (its Chrome control), cloud tasks, its account, its usage meter and Log out. They come back after the reset, once the app's app-server restarts: this row offers the restart then.": "それまで {agent} アプリの ChatGPT 機能(ChatGPT のチャットとプロジェクト、ブラウザ操作(Chrome の操作)、クラウドタスク、アカウント、使用量の表示、ログアウト)はオフになります。リセット後、アプリの app-server が再起動すると戻ります。そのときこの行に再起動ボタンが出ます。", "The {agent} app hit an error · quit it and open it again": "{agent} アプリでエラーが発生しました · 終了してもう一度開いてください", "Back on ChatGPT · restart the {agent} app to see its account": "ChatGPT に戻りました · アカウントを表示するには {agent} アプリを再起動", - "magpie gave {agent} back its ChatGPT account after the reset. The {agent} app still runs on the app-server it started while magpie served it, so it shows no account, no usage and none of its ChatGPT side until that restarts. Restart does it now; its window stays.": "リセット後、magpie は {agent} に ChatGPT アカウントを戻しました。ただし {agent} アプリは magpie が代わりに応答していた間に起動した app-server のままなので、再起動するまでアカウント、使用量、その他の ChatGPT 機能は表示されません。「再起動」ですぐに再起動します。ウィンドウはそのままです。", + "magpie gave {agent} back its ChatGPT account after the reset. The {agent} app still runs on the app-server it started while magpie served it, so it shows no account, no usage and none of its ChatGPT projects or chats until that restarts. Restart does it now; its window stays.": "リセット後、magpie は {agent} に ChatGPT アカウントを戻しました。ただし {agent} アプリは magpie が代わりに応答していた間に起動した app-server のままなので、再起動するまでアカウント、使用量、ChatGPT のプロジェクトとチャットは表示されません。「再起動」ですぐに再起動します。ウィンドウはそのままです。", "Only its app-server restarts, for its ChatGPT account; its window stays. A running turn stops.": "app-server だけを再起動して ChatGPT アカウントを読み込ませます。ウィンドウはそのままです。実行中のターンは止まります。", "Keep on ChatGPT (its sign-in's Always ChatGPT) leaves {agent} on its ChatGPT account instead: it shows its own limit message till the reset, and magpie no longer takes it over.": "「ChatGPT のまま」(サインインの「常に ChatGPT」)を選ぶと {agent} は ChatGPT アカウントのままになります。リセットまではそのアプリ自身の上限メッセージが表示され、magpie は引き継ぎません。", "Keep on ChatGPT": "ChatGPT のまま", @@ -16641,7 +16641,7 @@ const I18N = { "Till then the {agent} app's ChatGPT side is off: its ChatGPT chats and projects, browser use (its Chrome control), cloud tasks, its account, its usage meter and Log out. They come back after the reset, once the app's app-server restarts: this row offers the restart then.": "Bis dahin ist die ChatGPT-Seite der {agent}-App aus: ihre ChatGPT-Chats und -Projekte, Browser-Nutzung (ihre Chrome-Steuerung), Cloud-Aufgaben, ihr Konto, ihre Nutzungsanzeige und Abmelden. Nach dem Zurücksetzen kommt sie wieder, sobald der App-Server der App neu startet: Diese Zeile bietet dann den Neustart an.", "The {agent} app hit an error · quit it and open it again": "Die {agent}-App hat einen Fehler · beende sie und öffne sie erneut", "Back on ChatGPT · restart the {agent} app to see its account": "Wieder bei ChatGPT · {agent}-App neu starten, um das Konto zu sehen", - "magpie gave {agent} back its ChatGPT account after the reset. The {agent} app still runs on the app-server it started while magpie served it, so it shows no account, no usage and none of its ChatGPT side until that restarts. Restart does it now; its window stays.": "magpie hat {agent} nach dem Zurücksetzen sein ChatGPT-Konto zurückgegeben. Die {agent}-App läuft noch auf dem App-Server, den sie startete, während magpie sie bediente, und zeigt darum kein Konto, keine Nutzung und nichts von ihrer ChatGPT-Seite, bis dieser neu startet. „Neu starten“ erledigt das jetzt; das Fenster bleibt offen.", + "magpie gave {agent} back its ChatGPT account after the reset. The {agent} app still runs on the app-server it started while magpie served it, so it shows no account, no usage and none of its ChatGPT projects or chats until that restarts. Restart does it now; its window stays.": "magpie hat {agent} nach dem Zurücksetzen sein ChatGPT-Konto zurückgegeben. Die {agent}-App läuft noch auf dem App-Server, den sie startete, während magpie sie bediente, und zeigt darum kein Konto, keine Nutzung und keine ihrer ChatGPT-Projekte oder -Chats, bis dieser neu startet. „Neu starten“ erledigt das jetzt; das Fenster bleibt offen.", "Only its app-server restarts, for its ChatGPT account; its window stays. A running turn stops.": "Nur sein App-Server startet neu, um sein ChatGPT-Konto zu lesen; das Fenster bleibt offen. Ein laufender Turn wird angehalten.", "Keep on ChatGPT (its sign-in's Always ChatGPT) leaves {agent} on its ChatGPT account instead: it shows its own limit message till the reset, and magpie no longer takes it over.": "„Bei ChatGPT bleiben“ (bei der Anmeldung „Immer ChatGPT“) lässt {agent} stattdessen bei seinem ChatGPT-Konto: Bis zum Zurücksetzen zeigt es seine eigene Limit-Meldung, und magpie übernimmt nicht mehr.", "Keep on ChatGPT": "Bei ChatGPT bleiben", diff --git a/internal/gui/tests/codex-app-back.test.cjs b/internal/gui/tests/codex-app-back.test.cjs index 3789582fb..3ead320cb 100644 --- a/internal/gui/tests/codex-app-back.test.cjs +++ b/internal/gui/tests/codex-app-back.test.cjs @@ -22,11 +22,11 @@ const codex = (appBack) => ({ }); const words = { - en: { line: "Back on ChatGPT", why: "no usage", restart: "Restart", done: "Codex app-server restarted" }, - zh: { line: "已回到 ChatGPT", why: "用量", restart: "重启", done: "Codex 的 app-server 已重启" }, - "zh-TW": { line: "已回到 ChatGPT", why: "用量", restart: "重啟", done: "Codex 的 app-server 已重新啟動" }, - ja: { line: "ChatGPT に戻りました", why: "使用量", restart: "再起動", done: "Codex の app-server を再起動しました" }, - de: { line: "Wieder bei ChatGPT", why: "keine Nutzung", restart: "Neu starten", done: "App-Server von Codex neu gestartet" }, + en: { line: "Back on ChatGPT", why: "no usage", proj: "projects", restart: "Restart", done: "Codex app-server restarted" }, + zh: { line: "已回到 ChatGPT", why: "用量", proj: "项目", restart: "重启", done: "Codex 的 app-server 已重启" }, + "zh-TW": { line: "已回到 ChatGPT", why: "用量", proj: "專案", restart: "重啟", done: "Codex 的 app-server 已重新啟動" }, + ja: { line: "ChatGPT に戻りました", why: "使用量", proj: "プロジェクト", restart: "再起動", done: "Codex の app-server を再起動しました" }, + de: { line: "Wieder bei ChatGPT", why: "keine Nutzung", proj: "Projekte", restart: "Neu starten", done: "App-Server von Codex neu gestartet" }, }; function serve(lang, posts) { @@ -91,6 +91,8 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", await said.waitFor(); const s = await said.textContent(); assert.ok(s.includes(w.why), "the row opened says why: " + s); + // what the reporter missed: the app's ChatGPT projects (Herzz____ on X) + assert.ok(s.includes(w.proj), "the row opened names the projects: " + s); assert.doesNotMatch(s, /\{\w+\}/); go = page.locator(`${row} .ag-app-back + .ag-vl button`); await page.waitForFunction(() => { diff --git a/internal/provider/codex_app_server.go b/internal/provider/codex_app_server.go index 57be4386f..90958c4f4 100644 --- a/internal/provider/codex_app_server.go +++ b/internal/provider/codex_app_server.go @@ -70,6 +70,19 @@ var CodexAppRoute func() (route string, out, known bool) // switch restarts none of them. false when it can't be told. var CodexNoAccount func() bool +// CodexTakeoverOff is set by the agent package: whether the user has +// turned off magpie's takeover of Codex's used-up account (AppOutOff): +// its sign-in set to Always ChatGPT, as the takeover note's Keep on +// ChatGPT does, or Codex not connected. The takeover ending while it says +// so is the user's change, and the Codex app's app-server is restarted +// for it as for a Connect, Restart the Codex app to follow off too: the +// one the takeover started answers account/read with no account, so the +// app shows none of its ChatGPT projects or chats till it restarts, which +// the user who turned the takeover off to have them back can't tell +// (Herzz____ on X, the Windows app's project list empty). Asked only as +// the takeover ends; false while it isn't set. +var CodexTakeoverOff func() bool + // codexNoAccount is CodexNoAccount, false while it isn't set. func codexNoAccount() bool { return CodexNoAccount != nil && CodexNoAccount() @@ -427,7 +440,11 @@ func codexAppStep(ctx context.Context, now time.Time, busy func() (int, time.Tim c := &codexApp c.Lock() switched := c.switched + wasOut := c.seen && c.out c.Unlock() + // the takeover ending now: by the user's hand (CodexTakeoverOff), or + // by the reset, which is magpie's own change + offByHand := wasOut && !out && CodexTakeoverOff != nil && CodexTakeoverOff() // an account switch is nothing to an app-server on a provider that // takes no ChatGPT account (CodexNoAccount): it is forgotten, and a // route change later restarts it all the same @@ -453,13 +470,14 @@ func codexAppStep(ctx context.Context, now time.Time, busy func() (int, time.Tim // it unless they asked magpie to (RestartApp; atom0964 on Discord: both // of Codex's sign-in ticks off, and magpie still restarted the app to // switch accounts). A switch the user made, a Connect or a Disconnect - // is theirs, and restarts it. So does whatever leaves the app-server on + // is theirs, and restarts it, and so does the takeover turned off by + // hand (CodexTakeoverOff). So does whatever leaves the app-server on // a used-up account: the takeover in, the first look on one and a // switch off one. The app greys Send for every model while that // account's rate limit says not allowed and reads the account only as // its app-server starts (8b0132b3d), so nothing magpie serves would // reach it until a restart. - byHand := c.switched && !c.auto || c.seen && c.route != route && c.out == out + byHand := c.switched && !c.auto || c.seen && c.route != route && c.out == out || offByHand needed := into || c.switched && c.spent // left on the takeover: the app-servers it started while magpie was // Codex's provider, which show no account till they restart diff --git a/internal/provider/codex_app_server_test.go b/internal/provider/codex_app_server_test.go index 6a16a3f14..017f47370 100644 --- a/internal/provider/codex_app_server_test.go +++ b/internal/provider/codex_app_server_test.go @@ -51,11 +51,12 @@ func stubCodexApp(t *testing.T) *codexAppStub { codexApp.crashSince, codexApp.crashBy, codexApp.snag, codexApp.snagPids = time.Time{}, time.Time{}, false, nil codexApp.Unlock() } - oldCrashed, oldRelaunch := CodexAppCrashed, CodexAppRelaunch + oldCrashed, oldRelaunch, oldOff := CodexAppCrashed, CodexAppRelaunch, CodexTakeoverOff + CodexTakeoverOff = nil t.Cleanup(func() { CodexAppRoute, codexAppServersOn, endCodexAppServer, codexTurnRunning = oldOn, oldList, oldEnd, oldTurn CodexAppOpen, CodexAppReopen, codexSessionsQuiet, codexRestartsApp = oldOpen, oldReopen, oldQuiet, oldRestarts - CodexAppCrashed, CodexAppRelaunch = oldCrashed, oldRelaunch + CodexAppCrashed, CodexAppRelaunch, CodexTakeoverOff = oldCrashed, oldRelaunch, oldOff reset() }) // the app's logs as tests find them: no error page, and never reopened @@ -1288,3 +1289,75 @@ func TestCodexAppBackSaidTillItRestarts(t *testing.T) { t.Fatal("a failed listing read as the app-server gone") } } + +// The takeover turned off by the user (the takeover note's Keep on +// ChatGPT, the sign-in's Always ChatGPT, a Disconnect) restarts the Codex +// app's app-server with Restart the Codex app to follow off too, once no +// Codex turn runs: the one the takeover started shows no account, so the +// app's ChatGPT projects and chats stayed gone after the user turned it +// off to have them back (Herzz____ on X, the Windows app). The reset, +// magpie's own end of it, still leaves the app running and says so +// (CodexAppBack). Whether it was turned off is asked only as the takeover +// ends, not at every look. +func TestCodexAppRestartedWhenTheTakeoverIsTurnedOff(t *testing.T) { + signIn(t) + s := stubCodexApp(t) + codexRestartsApp = func() bool { return false } + off, asked := false, 0 + CodexTakeoverOff = func() bool { asked++; return off } + now := time.Now() + idle := func() (int, time.Time) { return 0, now.Add(-time.Minute) } + step := func(busy func() (int, time.Time)) []int { return codexAppStep(context.Background(), now, busy) } + + s.running = []int{60} + for range 3 { + if got := step(idle); got != nil { + t.Fatalf("on its account: ended %v", got) + } + } + s.on = true + if got := step(idle); !slices.Equal(got, []int{60}) { + t.Fatalf("the takeover: ended %v, want [60]", got) + } + step(idle) + if asked != 0 { + t.Fatalf("asked whether the takeover is off %d times while it ran", asked) + } + + // Keep on ChatGPT, with a Codex request in flight: held till it ends + off, s.on = true, false + if got := step(func() (int, time.Time) { return 1, now.Add(-time.Minute) }); got != nil { + t.Fatalf("a Codex request in flight: ended %v", got) + } + if got := step(idle); !slices.Equal(got, []int{100}) { + t.Fatalf("the takeover turned off by hand: ended %v, want [100]", got) + } + if CodexAppBack() { + t.Fatal("restarted, and still said to be left on the takeover") + } + if asked != 1 { + t.Fatalf("asked %d times, want once as the takeover ended", asked) + } + for range 3 { + if got := step(idle); got != nil { + t.Fatalf("after: ended %v", got) + } + } + if asked != 1 { + t.Fatalf("asked %d times after the takeover ended, want once in all", asked) + } + + // taken over again, and ended by the reset: left running, and said + off = false + s.on = true + if got := step(idle); !slices.Equal(got, []int{101}) { + t.Fatalf("the takeover again: ended %v, want [101]", got) + } + s.on = false + if got := step(idle); got != nil { + t.Fatalf("the reset with Restart off: ended %v", got) + } + if !CodexAppBack() { + t.Fatal("the app left on the takeover's app-server at the reset isn't said") + } +}