Files
magpie/docs/subsystems
yetone 3ec89c5224 codex: the takeover's end waits for the chats on magpie the Codex app has open (milianxinqi on X)
milianxinqi on X, the Codex app: "Model provider mismatch. This chat uses
magpie, but your current provider is OpenAI. Switch to magpie to
continue." ("用的好好的,codex 突然就弹这个了"), with nothing disconnected.

Cause. With the ChatGPT account Codex is signed in to held (out of its
allowance and credits), magpie is Codex's provider (the codex.out
takeover, #540) and new chats start on magpie. When the account has room
again (its window's reset, a switch to another account, or the account
the user keeps Codex on, a5e3972a7), Sync ended the takeover at once:
model_provider out of config.toml, Codex back on OpenAI. The Codex app
keeps the app-server the takeover started (codexAppStep leaves it
running unless Restart the Codex app to follow is on, off by default,
#4550), with its chats loaded and each one's writer lock held, so
bringBack could move none of them and codexRetagOpenThreads never ran
(it runs only before magpie ends the app-server). Every send in an open
chat then met the mismatch, with no step of the user's.

Fix. outStays (agent/codex.go): the takeover's end in Sync, both the
out_own way back and #540's, waits while a Codex holds a chat on magpie
open or wrote to one in the last minute (codexHeldOnMagpie:
sessions.CodexThreadsBusyOnBut, T3 Code's own threads left out, as
codexRetagOpenThreads leaves them), and codexThreadsLater asks for the
Sync that ends it once they are let go; bringBack then moves them onto
OpenAI. Meanwhile magpie serves them on the account, which has room, and
the app on the takeover's app-server shows no account either way. Nothing
waits when the app-server is restarted for the change (Restart the Codex
app to follow on, provider.CodexRestartsApp) or the user turned the
takeover off (Always ChatGPT): that restart moves the open chats first.
The [model_providers.magpie] table already stays after a Disconnect, so
an old thread still opens; a deliberate Disconnect restarts the
app-server (byHand) and retags its chats.

Other paths looked at and left: Disconnect, Unwire, Join and Codex's
account switch go through the same bringBack/retag; RestoreOnQuit (off by
default) runs at quit only; the update restart hands the gateway over on
macOS and Linux and skips it. On Windows a restart runs RestoreOnQuit
when it is on: filed separately.

Tests:
- TestCodexTakeoverWaitsForTheOpenChat (new): takeover, a chat on magpie
  quiet but its lock held, account back: still magpie's provider, the
  chat on magpie, no Sync asked; lock let go: a Sync asked, which ends
  the takeover and moves the chat to openai.
- TestCodexTakeoverEndsAtOnceWhenTheAppFollows (new): with the app
  restarted for the change, nothing waits.
- TestCodexTakeoverThreadComesBackOnceQuiet (nb5p): now the takeover
  stays while the thread is written to, and ends at the later Sync.
Broken by hand: outStays' gate taken out of Sync: the new test and nb5p's
red ("Codex was put on OpenAI under it"), green with it.

Run (mica, mote over its load, at this tree on origin/main 48fffb60c):
- go test -count=1 -timeout 45m ./internal/agent ./internal/sessions
  ./internal/provider: ok (426s, 1917s, 237s).
- go vet of those on darwin, GOOS=windows, GOOS=linux: ok. gofmt -l clean.
- Rebased onto origin/main a2d402afe (provider and sessions changes
  elsewhere): go build ./..., go vet of the three, and the takeover,
  thread-move and retag tests of internal/agent and internal/sessions
  again: ok.

Not tried: the real Codex app through a real reset (needs an account
held then given room); a full ./... run (mica had another session's full
suite and GUI suite running).
2026-11-18 16:33:04 +08:00
..
2026-11-10 03:25:44 +08:00

Subsystem design references

These pages describe current responsibilities, runtime paths, state, and contracts. Start with the subsystem relevant to your task, then follow its source links to verify the implementation you change.

Subsystem Reference
Built-in subscriptions moving to plugins Provider and plugin ownership
Built-in plugins: registry, lifecycle, plugins.yml, presets, settings schemas, the window's kernel (slots, error boundary, kit) Plugin runtime
Plugins that run on the gateway's requests and replies Gateway middleware
Tools that plugins give, offered to every agent as an MCP server Plugin tools
Plugins that await context recall and observe completed replies Asynchronous gateway request hooks
Claude subscription turns across Claude Code runs Claude subscription bridge
How the gateway picks a provider, key or account, and falls back Gateway routing and fallback
Gateway Session Aggregation Gateway Session Aggregation
Connecting an agent's config to the gateway, drift, disconnect, WSL twins Agent wiring
Providers, keys, subscription accounts, refresh and allowance Providers and accounts
Instructions, MCP servers, skills and RTK written into agents Library
One MCP server, magpie, given to every agent in place of the Library's servers MCP router
Memory shared by every agent: the memory MCP server, the digest in instructions, the Memory tab Memory
One skill, magpie-skills, listing the Library's skills in place of each agent's own Skill router
Tray, panel, window, magpie web and the page's JSON API GUI app shell
Encrypted backups and setup synchronization over WebDAV, S3 or GitHub Sync and backup

A subsystem can span several packages or repositories. Its reference describes the behavior those parts provide together; it does not need to list every function.

Maintain a reference

Update the owning reference in the same PR when a change alters a documented responsibility, interface, state transition, runtime path, compatibility rule, or failure behavior. An internal refactor that preserves these contracts does not require a prose change.

Keep each fact in one reference and link to it from agent instructions or other pages. Link to source files and named symbols rather than copying implementation details that change frequently. Explain field meanings and constraints that signatures alone do not express.

For a new reference, include the responsibilities and boundaries, source of truth, main runtime paths, state transitions, important constraints, and existing verification commands. Include failure and compatibility behavior where relevant. Document current behavior separately from proposed changes; the proposal belongs in its issue or PR.

Describe a PR's semantic changes

For each affected subsystem, explain the observable change with its trigger and before/after behavior. Include changes to interfaces, state, compatibility, and failure behavior when relevant. Link the affected reference, implementation, and verification evidence. State when a refactor preserves the documented behavior.

For example, a change to plugin adoption should explain which migration state and account condition trigger it, which implementation handles requests before and after adoption, and which test exercises that transition.

This is an author-provided explanation that a reviewer verifies against the diff and surrounding code. It is not an automatically proven compatibility verdict.

Review a change

  1. Identify the affected subsystem and the actual runtime path, including plugin or built-in ownership.
  2. Compare the claimed before/after behavior with the base and head implementation. Trace relevant callers and state transitions beyond the changed lines.
  3. Check that changed contracts appear in the owning reference and that source links still resolve.
  4. Check that the cited tests or runtime evidence exercise the claimed behavior. Report checks that were not run.

A documentation update alone does not prove the implementation. Existing tests and runtime evidence remain necessary for behavior changes.