Files
magpie/docs/subsystems
yetone 127f1e77dc gateway: a turn to another magpie goes as its new input after the turn it kept
LotusDecoder on X runs two magpies, each the other's provider. Codex says
store false, so the leg between them carried the whole conversation
every turn. Now passthrough asks a remote magpie to keep the Responses
turn (X-Magpie-Keep-Turn: magpie/<version>). A magpie that does answers
X-Magpie-Kept-Turn. This side records per route and scope a digest of
each input item as sent, the reply's output types and ids, and the
other magpie's id (remoteTurns: 16 per route and scope, 32 MB, prevTTL).
The next request that continues exactly that input and reply goes as
the items after them with previous_response_id. Anything else goes
whole: an edited history, another answer, a magpie that never said it
keeps (an older one). A refused delta is sent again whole at once, and
the route goes whole for 30 minutes.

The receiving magpie honours the header only from a magpie's hop
(keepTurnAsks): its value starts magpie/ and the User-Agent is magpie's,
or Codex's, which forwardOnce passes on for Codex turns. It keeps the
turn under the caller's prevScope and the usual caps. An id such a
caller names that isn't held gets previous_response_not_found before
any vendor is asked. withCaller takes the header off on arrival, and
X-Magpie- is never a passable request header, so no vendor gets it.

Run on mote (mote-run) at base 93d5c76c0 with this tree:
- gofmt -l internal/gateway internal/provider: clean
- GOOS=linux go vet -tags nogui ./internal/gateway: ok
- GOOS=windows go vet ./internal/gateway: ok
- go test -tags nogui -count=1 -timeout 45m ./internal/gateway:
  ok (1123.7s)
- The five new tests, plus TestRemoteMagpie*, TestPreviousResponse*,
  TestUnknownPreviousResponse and TestPrevStore: 54 passed.
- Again rebased on 538026e62, the same suite: one red,
  TestUnroutedEmptyEncodedStreamIsBounded/gzip ("TempDir RemoveAll
  cleanup: ... magpie/held: directory not empty"). It is #4775's test:
  the held report is written from the timer after the 408, so the test
  can end mid-write. Not this change; fixed in its own commit.
- At the pushed base 50aaa5726: gofmt clean, Linux and Windows vet ok,
  the related tests above (57 passes) ok.

Broken by hand, each red, then restored:
- delta never sent: SentTheNewInput, LostTurnSentWhole and the
  EditedHistory control go red.
- the header honoured from any client: KeepTurnOnlyFromAMagpie goes red
  (curl told kept).
- refusal not remembered: LostTurnSentWhole goes red (turn 3 is tried as
  a delta).
- deltas without the Kept-Turn reply: OlderRemoteMagpieSentWhole goes
  red.
- keptTurnLost check removed: KeepTurnOnlyFromAMagpie goes red (the lost
  id is relayed).
- output marks not compared: EditedHistory "other answer" goes red.
- the withCaller strip and the X-Magpie- never-pass prefix both removed:
  KeepTurnOnlyFromAMagpie goes red (vendor sent the header). Either one
  alone holds it, so removing only the strip stays green.

The new tests don't compile on the old code, since they name the new
header constants and remoteTurns.

Found on the way: a Codex turn reaches the remote magpie with Codex's
User-Agent, not magpie's, so callerFrom's existing X-Magpie-Agent/Via
reading never applies to Codex hops. Unchanged here; keepTurnAsks
accepts that shape.

Not tried:
- two real magpies over a real link, and a WebSocket-fed Codex (only
  HTTP turns in tests);
- an SSE reply over the hop (tee parses SSE through prevReply, which
  prev_response tests cover, but no remote-delta test streams);
- other platforms' test runs (Linux and Windows were vetted only).

Known gaps:
- the reply is matched by type and ids, so a client that edits the
  last answer's text (no ids) gets a delta over the kept one;
- tools and instructions are still sent every turn;
- the remote's kept turns are in memory only;
- a client that copies a hop's headers gets its store-false turns kept
  under its own scope and the caps.
2026-11-04 22:27:11 +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
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.