diff --git a/docs/subsystems/agent-wiring.md b/docs/subsystems/agent-wiring.md index a1557b89a..60700396c 100644 --- a/docs/subsystems/agent-wiring.md +++ b/docs/subsystems/agent-wiring.md @@ -99,7 +99,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 (`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`). Nor does it move when 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`). Every turn would be refused there too, and the move only hid the account, its usage and its reset from the app's menu (down to "magpie") and ended the app's app-server at the limit and again at the reset, which the ChatGPT app shows as an error and relaunches (#4550; xiaoq6285 on Discord, one Plus account in the Windows app; `TestCodexOwnModelOneSpentAccountStaysOnItsSignIn`, `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 waits for the user, though, unless they ticked Restart the Codex app when magpie switches its account (`Provider.RestartApp`, `provider/restartapp`, off by default, this computer's own like KeepLogin): the used-up takeover in and out, the first look, and its own account switches (`SwitchWhenSpent` → `switchLoginBy(…, true)`, `codexApp.auto`). 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. atom0964 on Discord had both sign-in ticks off and the ChatGPT app relaunched at every switch between two Plus accounts (`TestCodexAppLeftRunningForMagpiesOwnChanges`, `TestCodexAppFirstLookLeftRunning`, `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 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 (`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`). Nor does it move when 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`). Every turn would be refused there too, and the move only hid the account, its usage and its reset from the app's menu (down to "magpie") and ended the app's app-server at the limit and again at the reset, which the ChatGPT app shows as an error and relaunches (#4550; xiaoq6285 on Discord, one Plus account in the Windows app; `TestCodexOwnModelOneSpentAccountStaysOnItsSignIn`, `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). 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 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/gui/assets/app.js b/internal/gui/assets/app.js index e072c53e8..566610c6d 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -13281,9 +13281,9 @@ function renderAccounts(a, p) { list.append(el("div", "switch-only", only)); } if (p && cx && (several || p.restartApp)) { - const [re, cb] = tick(t("Restart the {agent} app when magpie switches its account", ag), !!p.restartApp); - re.title = t("Off, the {agent} app stays on its account until you restart it.", ag); - cb.onchange = () => accountAction("provider/restartapp", { id: p.id, restartApp: cb.checked }, t(cb.checked ? "magpie restarts the {agent} app when it switches accounts" : "magpie leaves the {agent} app running when it switches accounts", ag)); + const [re, cb] = tick(t("Restart the {agent} app when magpie switches its account back", ag), !!p.restartApp); + re.title = t("Off, the {agent} app stays on its account until you restart it. A used-up account restarts it anyway.", ag); + cb.onchange = () => accountAction("provider/restartapp", { id: p.id, restartApp: cb.checked }, t(cb.checked ? "magpie restarts the {agent} app when it switches accounts back" : "magpie leaves the {agent} app running when it switches accounts back", ag)); list.append(el("div", "restart-app", re)); } // how a pool ends at its caps (plugins#349) diff --git a/internal/gui/assets/i18n.js b/internal/gui/assets/i18n.js index 0ba67747c..595ecaa9f 100644 --- a/internal/gui/assets/i18n.js +++ b/internal/gui/assets/i18n.js @@ -1810,10 +1810,10 @@ const I18N = { "Keep {agent} signed in to the first account": "{agent} 始终登录首选账号", "Keep {agent} signed in to": "{agent} 始终登录", "Only switch {agent}'s sign-in": "只切换 {agent} 的登录", - "Restart the {agent} app when magpie switches its account": "magpie 切换账号时自动重启 {agent} 应用", - "Off, the {agent} app stays on its account until you restart it.": "关闭时,{agent} 应用会停留在原来的账号上,直到你重启它。", - "magpie restarts the {agent} app when it switches accounts": "magpie 切换账号时会重启 {agent} 应用", - "magpie leaves the {agent} app running when it switches accounts": "magpie 切换账号时不再重启 {agent} 应用", + "Restart the {agent} app when magpie switches its account back": "magpie 切回原账号时自动重启 {agent} 应用", + "Off, the {agent} app stays on its account until you restart it. A used-up account restarts it anyway.": "关闭时,{agent} 应用会停留在原来的账号上,直到你重启它。账号额度用完时仍会重启。", + "magpie restarts the {agent} app when it switches accounts back": "magpie 切回原账号时会重启 {agent} 应用", + "magpie leaves the {agent} app running when it switches accounts back": "magpie 切回原账号时不再重启 {agent} 应用", "Switch accounts only": "仅切换账号", "Switch accounts only: {agent} is signed in as {user}. Pick another of its accounts to sign {agent} in to it, without connecting it to magpie.": "仅切换账号:{agent} 当前登录的是 {user}。选另一个账号,即可把 {agent} 切换过去,无需接入 magpie。", "Not signed in": "未登录", @@ -6332,10 +6332,10 @@ const I18N = { "Keep {agent} signed in to the first account": "{agent} 始終登入首選帳號", "Keep {agent} signed in to": "{agent} 始終登入", "Only switch {agent}'s sign-in": "只切換 {agent} 的登入", - "Restart the {agent} app when magpie switches its account": "magpie 切換帳號時自動重新啟動 {agent} 應用程式", - "Off, the {agent} app stays on its account until you restart it.": "關閉時,{agent} 應用程式會停留在原來的帳號上,直到你重新啟動它。", - "magpie restarts the {agent} app when it switches accounts": "magpie 切換帳號時會重新啟動 {agent} 應用程式", - "magpie leaves the {agent} app running when it switches accounts": "magpie 切換帳號時不再重新啟動 {agent} 應用程式", + "Restart the {agent} app when magpie switches its account back": "magpie 切回原帳號時自動重新啟動 {agent} 應用程式", + "Off, the {agent} app stays on its account until you restart it. A used-up account restarts it anyway.": "關閉時,{agent} 應用程式會停留在原來的帳號上,直到你重新啟動它。帳號額度用完時仍會重新啟動。", + "magpie restarts the {agent} app when it switches accounts back": "magpie 切回原帳號時會重新啟動 {agent} 應用程式", + "magpie leaves the {agent} app running when it switches accounts back": "magpie 切回原帳號時不再重新啟動 {agent} 應用程式", "Switch accounts only": "僅切換帳號", "Switch accounts only: {agent} is signed in as {user}. Pick another of its accounts to sign {agent} in to it, without connecting it to magpie.": "僅切換帳號:{agent} 目前登入的是 {user}。選另一個帳號,即可把 {agent} 切換過去,無需接入 magpie。", "Not signed in": "未登入", @@ -10830,10 +10830,10 @@ const I18N = { "Keep {agent} signed in to the first account": "{agent} を最初のアカウントにサインインしたままにする", "Keep {agent} signed in to": "{agent} のサインイン先を固定:", "Only switch {agent}'s sign-in": "{agent} のサインインだけを切り替える", - "Restart the {agent} app when magpie switches its account": "magpie がアカウントを切り替えたら {agent} アプリを再起動する", - "Off, the {agent} app stays on its account until you restart it.": "オフのとき、{agent} アプリは再起動するまで元のアカウントのままです。", - "magpie restarts the {agent} app when it switches accounts": "magpie はアカウントを切り替えると {agent} アプリを再起動します", - "magpie leaves the {agent} app running when it switches accounts": "magpie はアカウントを切り替えても {agent} アプリを再起動しません", + "Restart the {agent} app when magpie switches its account back": "magpie が元のアカウントに戻したら {agent} アプリを再起動する", + "Off, the {agent} app stays on its account until you restart it. A used-up account restarts it anyway.": "オフのとき、{agent} アプリは再起動するまで元のアカウントのままです。利用枠を使い切ったときは再起動します。", + "magpie restarts the {agent} app when it switches accounts back": "magpie は元のアカウントに戻すと {agent} アプリを再起動します", + "magpie leaves the {agent} app running when it switches accounts back": "magpie は元のアカウントに戻しても {agent} アプリを再起動しません", "Switch accounts only": "アカウント切り替えのみ", "Switch accounts only: {agent} is signed in as {user}. Pick another of its accounts to sign {agent} in to it, without connecting it to magpie.": "アカウント切り替えのみ:{agent} は {user} としてサインイン中です。別のアカウントを選ぶと、{agent} を magpie に接続せずにそのアカウントへ切り替えます。", "Not signed in": "未サインイン", @@ -15359,10 +15359,10 @@ const I18N = { "Keep {agent} signed in to the first account": "{agent} beim ersten Konto angemeldet lassen", "Keep {agent} signed in to": "{agent} angemeldet lassen bei", "Only switch {agent}'s sign-in": "Nur die Anmeldung von {agent} wechseln", - "Restart the {agent} app when magpie switches its account": "{agent}-App neu starten, wenn magpie das Konto wechselt", - "Off, the {agent} app stays on its account until you restart it.": "Aus bleibt die {agent}-App beim bisherigen Konto, bis du sie neu startest.", - "magpie restarts the {agent} app when it switches accounts": "magpie startet die {agent}-App neu, wenn es das Konto wechselt", - "magpie leaves the {agent} app running when it switches accounts": "magpie lässt die {agent}-App laufen, wenn es das Konto wechselt", + "Restart the {agent} app when magpie switches its account back": "{agent}-App neu starten, wenn magpie zum Konto zurückwechselt", + "Off, the {agent} app stays on its account until you restart it. A used-up account restarts it anyway.": "Aus bleibt die {agent}-App beim bisherigen Konto, bis du sie neu startest. Ein aufgebrauchtes Konto startet sie trotzdem neu.", + "magpie restarts the {agent} app when it switches accounts back": "magpie startet die {agent}-App neu, wenn es zum Konto zurückwechselt", + "magpie leaves the {agent} app running when it switches accounts back": "magpie lässt die {agent}-App laufen, wenn es zum Konto zurückwechselt", "Switch accounts only": "Nur Konten wechseln", "Switch accounts only: {agent} is signed in as {user}. Pick another of its accounts to sign {agent} in to it, without connecting it to magpie.": "Nur Konten wechseln: {agent} ist als {user} angemeldet. Wähle ein anderes seiner Konten, um {agent} dort anzumelden, ohne es mit magpie zu verbinden.", "Not signed in": "Nicht angemeldet", diff --git a/internal/gui/tests/restart-app.test.cjs b/internal/gui/tests/restart-app.test.cjs index 238ce5b08..1eb6514ff 100644 --- a/internal/gui/tests/restart-app.test.cjs +++ b/internal/gui/tests/restart-app.test.cjs @@ -53,16 +53,16 @@ function serve(lang, first, posts) { } const label = { - en: "Restart the Codex app when magpie switches its account", - zh: "magpie 切换账号时自动重启 Codex 应用", - ja: "magpie がアカウントを切り替えたら Codex アプリを再起動する", - de: "Codex-App neu starten, wenn magpie das Konto wechselt", + en: "Restart the Codex app when magpie switches its account back", + zh: "magpie 切回原账号时自动重启 Codex 应用", + ja: "magpie が元のアカウントに戻したら Codex アプリを再起動する", + de: "Codex-App neu starten, wenn magpie zum Konto zurückwechselt", }; const strings = [ - "Restart the {agent} app when magpie switches its account", - "Off, the {agent} app stays on its account until you restart it.", - "magpie restarts the {agent} app when it switches accounts", - "magpie leaves the {agent} app running when it switches accounts", + "Restart the {agent} app when magpie switches its account back", + "Off, the {agent} app stays on its account until you restart it. A used-up account restarts it anyway.", + "magpie restarts the {agent} app when it switches accounts back", + "magpie leaves the {agent} app running when it switches accounts back", ]; for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { diff --git a/internal/provider/codex_app_server.go b/internal/provider/codex_app_server.go index 2fc195ac2..0f1e871ec 100644 --- a/internal/provider/codex_app_server.go +++ b/internal/provider/codex_app_server.go @@ -318,9 +318,9 @@ func codexAppAccountSwitched() { codexAppSwitched(false, false) } func codexAppSpentSwitch() { codexAppSwitched(true, false) } // codexAppSwitched notes a switch of Codex's account: spent, off one used -// up; byMagpie, made by magpie itself (SwitchWhenSpent), which restarts -// the app only with RestartApp on. One the user made among them restarts -// it all the same. +// up; byMagpie, made by magpie itself (SwitchWhenSpent). magpie's switch +// back restarts the app only with RestartApp on; its switch off a spent +// account, and one the user made among them, restart it all the same. func codexAppSwitched(spent, byMagpie bool) { c := &codexApp c.Lock() @@ -382,18 +382,25 @@ func codexAppStep(ctx context.Context, now time.Time, busy func() (int, time.Tim // quota ran out mid-turn would hold their restart, and the app's grey // Send, for up to codexTurnWindow (xiaoq6285 on Discord) into := changed && out && (!c.seen || !c.out) - // what changed magpie changed by itself: the used-up takeover in or - // out, and its own account switches. The ChatGPT app shows the end of - // its app-server as an error and relaunches (#4550), so that waits for - // the user to restart 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. + // what changed magpie changed by itself and the app works without: + // the switch back once the account has room, and the takeover out at + // the reset. The ChatGPT app shows the end of its app-server as an + // error and relaunches (#4550), so that waits for the user to restart + // 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 + // 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 - if changed && !byHand && !codexRestartsApp() { - why := "magpie switched Codex's ChatGPT account" + needed := into || c.switched && c.spent + if changed && !byHand && !needed && !codexRestartsApp() { + why := "magpie switched Codex's ChatGPT account back" if !c.switched { - why = "magpie became Codex's provider for its used-up account, or gave it back" + why = "magpie gave Codex back its ChatGPT account after the reset" } log.Printf("codex: %s; the Codex app's app-server is left running (Restart the Codex app to follow is off), and the app follows once it is restarted", why) c.switched, c.auto = false, false diff --git a/internal/provider/codex_app_server_test.go b/internal/provider/codex_app_server_test.go index b2d01e9e1..fbd2d7fb9 100644 --- a/internal/provider/codex_app_server_test.go +++ b/internal/provider/codex_app_server_test.go @@ -1079,12 +1079,16 @@ func TestCodexSessionsQuietFor(t *testing.T) { } // With both of Codex's sign-in ticks off magpie switches Codex's ChatGPT -// account by itself (SwitchWhenSpent, and back at the reset), and becomes -// Codex's provider for a used-up account and gives it back. Ending the -// Codex app's app-server for any of them makes the ChatGPT app show an -// error and relaunch (#4550), which atom0964 on Discord could not turn -// off. Those leave the app running unless the user ticked RestartApp. A -// switch the user makes, and a Connect or Disconnect, still restart it. +// account by itself (SwitchWhenSpent, and back once the first has room), +// and becomes Codex's provider for a used-up account and gives it back. +// Ending the Codex app's app-server makes the ChatGPT app show an error +// and relaunch (#4550), which atom0964 on Discord could not turn off. The +// changes the app works without (the switch back, the account given back +// at the reset) leave it running unless the user ticked RestartApp. Those +// that leave it on a used-up account restart it either way: the app +// greys Send there for every model and reads the account only as its +// app-server starts (8b0132b3d), so magpie's fallback could not serve it. +// A switch the user makes, and a Connect or Disconnect, restart it too. func TestCodexAppLeftRunningForMagpiesOwnChanges(t *testing.T) { signIn(t) s := stubCodexApp(t) @@ -1101,61 +1105,60 @@ func TestCodexAppLeftRunningForMagpiesOwnChanges(t *testing.T) { } } } + ends := func(what string, want int) { + t.Helper() + if got := step(); !slices.Equal(got, []int{want}) { + t.Fatalf("%s: ended %v, want [%d]", what, got, want) + } + none("after " + what) + } s.running = []int{60} none("nothing changed") codexAppSwitched(true, true) - none("magpie switched off the used-up account") + ends("magpie switched off the used-up account", 60) codexAppSwitched(false, true) - none("magpie switched back at the reset") + none("magpie switched back once the account had room") s.on = true - none("magpie became Codex's provider for the used-up account") + ends("magpie became Codex's provider for the used-up account", 100) s.on = false - none("magpie gave Codex back its account") + none("magpie gave Codex back its account at the reset") // a switch the user made, alone or after one of magpie's codexAppSwitched(false, true) codexAppAccountSwitched() - if got := step(); !slices.Equal(got, []int{60}) { - t.Fatalf("switched by the user after magpie: ended %v, want [60]", got) - } - none("the new app-server read the account as it started") + ends("switched by the user after magpie", 101) codexAppAccountSwitched() codexAppSwitched(false, true) - if got := step(); !slices.Equal(got, []int{100}) { - t.Fatalf("switched by the user, then magpie: ended %v, want [100]", got) - } + ends("switched by the user, then magpie", 102) // a Connect: where Codex's config sends it changed, not by the takeover s.route = "connected" - if got := step(); !slices.Equal(got, []int{101}) { - t.Fatalf("connected: ended %v, want [101]", got) - } - none("after the connect") + ends("connected", 103) - // ticked: magpie's own changes restart it as before + // ticked: magpie's changes the app works without restart it as well restarts = true - codexAppSwitched(true, true) - if got := step(); !slices.Equal(got, []int{102}) { - t.Fatalf("RestartApp on, magpie switched: ended %v, want [102]", got) - } + codexAppSwitched(false, true) + ends("RestartApp on, magpie switched back", 104) s.on = true - if got := step(); !slices.Equal(got, []int{103}) { - t.Fatalf("RestartApp on, magpie took over: ended %v, want [103]", got) - } + ends("RestartApp on, magpie took over", 105) + s.on = false + ends("RestartApp on, magpie gave the account back", 106) } -// At magpie's first look, an app opened before it on a used-up account -// was restarted at once; that is magpie's own change too, and waits for -// RestartApp. -func TestCodexAppFirstLookLeftRunning(t *testing.T) { +// At magpie's first look, an app opened before it on a used-up account is +// restarted at once with RestartApp off too: left on that account it greys +// Send for every model, magpie's included. +func TestCodexAppFirstLookOnAUsedUpAccountRestarts(t *testing.T) { signIn(t) s := stubCodexApp(t) codexRestartsApp = func() bool { return false } now := time.Now() s.running, s.on = []int{60}, true - for range 2 { - if got := codexAppStep(context.Background(), now, func() (int, time.Time) { return 0, now.Add(-time.Minute) }); got != nil { - t.Fatalf("first look on the used-up account: ended %v", got) - } + idle := func() (int, time.Time) { return 0, now.Add(-time.Minute) } + if got := codexAppStep(context.Background(), now, idle); !slices.Equal(got, []int{60}) { + t.Fatalf("first look on the used-up account: ended %v, want [60]", got) + } + if got := codexAppStep(context.Background(), now, idle); got != nil { + t.Fatalf("after the first look: ended %v", got) } } diff --git a/internal/provider/codex_switch.go b/internal/provider/codex_switch.go index 83c875d8b..246032642 100644 --- a/internal/provider/codex_switch.go +++ b/internal/provider/codex_switch.go @@ -488,10 +488,12 @@ func SwitchWhenSpent(ctx context.Context, agent string) (string, error) { } if agent == "codex" { if was := CodexAppStale(); was != "" { - if codexRestartsApp() { + // a switch off a spent account restarts it either way: left on + // that account, the app greys Send for every model + if codexRestartsApp() || !back { log.Printf("codex: the Codex app is still signed in to %s, and shows its limits, until its app-server is restarted once no Codex turn runs", was) } else { - log.Printf("codex: the Codex app is still signed in to %s, and shows its limits, until it is restarted: magpie restarts it for a switch of its own only with \"Restart the Codex app to follow\" on", was) + log.Printf("codex: the Codex app is still signed in to %s, and shows its limits, until it is restarted: magpie restarts it for a switch back only with \"Restart the Codex app to follow\" on", was) } } } diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 132412b2e..696e8fe77 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -167,14 +167,16 @@ type Provider struct { // request on them (KeptToAgent; myduso on Discord). SwitchOnly bool `json:"switchOnly,omitempty"` // RestartApp, on Codex's subscription, has magpie restart the Codex - // app's own app-server when it changes Codex's account by itself: a - // switch off an account running out (SwitchWhenSpent) or back, and - // becoming Codex's provider for a used-up account or giving it back. - // The ChatGPT app shows an ended app-server as an error and relaunches - // (#4550), so it is off unless the user turns it on (atom0964 on - // Discord); the app then stays on its account until it is restarted, - // from the switch notice or by hand. A switch the user makes, and a - // Connect or Disconnect, restart it either way. + // app's own app-server for a change it makes by itself that the app + // works without: the switch back once the account it moved off has + // room (SwitchWhenSpent), and giving Codex its account back at the + // reset. The ChatGPT app shows an ended app-server as an error and + // relaunches (#4550), so it is off unless the user turns it on + // (atom0964 on Discord); the app then stays on its account until it + // is restarted, from the switch notice or by hand. A switch the user + // makes, a Connect or Disconnect, and whatever leaves the app on a + // used-up account (a switch off it, becoming its provider for it) + // restart it either way: the app greys Send there (8b0132b3d). RestartApp bool `json:"restartApp,omitempty"` // MaxConcurrency is how many requests may be out at the vendor at once