Files
magpie/docs/subsystems
yetone 6f5d48ea0f remote magpie: a moved remote is looked for again, and is sent its key only after it proves it holds it
fe61503e6 had a Remote magpie whose address didn't dial look for it on
the old address's /24 and this computer's private /24s, and sent GET
/v1/models, signed with the provider's gateway key (Authorization,
x-api-key and the provider's own headers), to every address that took
the connection at the port. On a café's or hotel's network, or with the
other computer off, anything listening there got the key in clear and,
by answering {"data":[]}, became the provider: the failed request and
every later one went to it. 18a9a2d88 turned the look off.

Now an address that takes the connection is first asked to prove it
holds the key, with nothing keyed sent:

- B POSTs {"nonce": 32 random bytes hex, "key_id": sha256(key) hex} to
  /v1/magpie/prove (provider.RemoteProvePath), no key, no provider
  headers, no proxy, no redirects (proves in remote_moved.go).
- A (gateway's remote_prove.go) finds the enabled gateway key with that
  sha256 (access.ProveSecret: constant-time compares over every key,
  the legacy settings sharing key too, then Check, so an off, expired
  or orphaned issued key has none) and answers
  HMAC-SHA256(key, "magpie-prove-v1\n" nonce "\n" address), the address
  being the one the connection reached it at (http.LocalAddrContextKey);
  404 when no key matches. A host that relays B's nonce to A elsewhere
  gets a proof for A's address, which B, checking against the address it
  dialled with hmac.Equal, refuses.
- Only after that does isRemote ask /v1/models with the key, as before.
- The route goes through lanGuard like OIDC's trade (proveAsked: POST,
  exact path, no gateway key): from another machine only while shared,
  past the allow list and rebinding check; corsGuard still judges its
  Origin, the handler refuses anything fromBrowser, and an unknown id
  from another machine counts against tooManyAuthFails. readOnlyRoutes
  lists it (it carries no key and answers none).

seekAt is lanNeighbours again: the old /24 and this computer's own
private /24s, both behind the proof.

Not found, and so safe: a remote magpie from before this route (404), one
not shared (403), one reached through a port forward, NAT or a Docker
port (it sees another local address than the one dialled), and one behind
a proxy that needs the provider's headers. Each fails as before
fe61503e6 and the user types the new address.

Tests (local httptest servers only):
- TestRemoteMagpieKeyGoesToNoNeighbour, rewritten from 18a9a2d88's "no
  address looked at": four impostors that list 200 for anything (no
  proof route; a well-formed proof under another key; a garbled proof;
  one relaying B's ask to the real magpie and handing back its proof)
  are asked only the proof, never see the key or the provider's custom
  header, and aren't saved; then the real magpie, last in the list, is
  found and saved.
- TestRemoteProof: another key, nonce, address or port is another proof;
  an IPv4-mapped IPv6 address is the same address.
- TestRemoteProveAnswersOnlyTheKeysHolder (gateway): from another
  machine while shared, the key's holder answers the right HMAC and never
  the key; an unknown id and a key turned off get 404; Origin, a Mozilla
  User-Agent and Sec-Fetch-Mode are refused; not shared, 403.
- TestRemoteMagpieMovedIsFound now uses a real gateway key on A.

Run on mote at this tree (base 18a9a2d88):
- gofmt -l internal cmd: clean
- go vet ./internal/provider ./internal/gateway ./internal/access (darwin,
  with and without -tags nogui), GOOS=windows go vet, GOOS=linux go vet
  -tags nogui: ok
- go test -tags nogui -count=1 -timeout 45m ./internal/provider
  ./internal/gateway ./internal/access: provider ok (206s), access ok,
  gateway FAIL on TestUnroutedHeldReportSaved only ("(under way)"),
  the open #4300, a file this change doesn't touch; it passes alone
  with -count=3 on mote.

Rebased onto 2000ae439 (its gateway.go change, the Antigravity format
check in models(), sits beside this one's route line). Rerun on mote at
the rebased head: gofmt clean; the same four vets ok; go test -tags
nogui -count=1 -run 'RemoteMagpie|RemoteProof|RemoteProve|LANNeighbours|
Dialable|ReadOnlyKeyEveryRoute|OIDCToken|Antigravity|KeyPage' over the
three packages: ok. The full suite was not run again.

Broken by hand, each red:
- isRemote without the proves check: the impostor with no proof route
  is sent the key and the header, is saved, and the real magpie is never
  reached.
- RemoteProof without the address: the relaying impostor passes, is
  sent the key and saved; TestRemoteProof red too.

Not tried: a real look across two computers on a LAN (A on mote, B in
WSL as fe61503e6 did); a real Windows A; IPv6 link-local addresses (not
looked at). Residual: a user-chosen weak gateway key (8+ characters)
can be guessed offline by an impostor from its sha256; magpie's own keys
are 24 random bytes.

Push rejected once; rebased onto f4068ccc2 (proc only, none of this
change's files). Rerun on mote there: gofmt, the same vets and the -run
tests above: ok.
2026-11-06 21:32:55 +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.