zgcwkj puts magpie behind nginx on one domain and wanted the browser UI (magpie web / serve --web, 127.0.0.1:3430) and the gateway (:3425) from one port, so one location works. Now the web port hands every gateway client route to the gateway: /v1/..., /v1beta/..., /models, /chat/completions, /responses, /messages, /images, /embeddings, /rerank, /audio, /videos, /muse-code/models, /backend-api/codex/..., the search path and /mcp/... (gateway.ClientPath). Everything else stays the page's, behind webGuard as before. /api/* stays split: on the web port it is the UI's API; Ollama's /api/* and Claude Desktop's /api/hello still need the gateway port, since the two can't share the path. Security: a handed-over request goes through the gateway's whole own stack, not a copy of it. gateway.Server.Front() is lanGuard(Handler()), built once; ListenAndServe serves the same handler. When this process serves the gateway, the web port calls Front() in process; when another magpie does, toGateway reverse-proxies to gateway.URL(), keeps the Host, copies Forwarded/X-Forwarded-* back (ReverseProxy strips them), and adds X-Forwarded-For for a non-loopback peer, so remote, proxied, rebound, Origin null, *.localhost and foreign-Origin requests are judged exactly as on the gateway port. The web cookie is never a gateway key. Paths that aren't clean (/v1/../api, /v1//x, /v1/./x) are not client paths, so they never reach the gateway as one. The allow list (AllowGuard) still wraps both. Tests: - TestClientPathCoversHandler reads every mux pattern Handler registers and fails when one is neither a ClientPath nor /, /api/* or /_magpie/*, so a new gateway route has to be placed. - TestClientPathOnlyClean. - TestWebOnePortServedHere / ServedElsewhere: with no web cookie, the UI API (/api/settings) is never reached for: no Origin (GET, POST), Origin null, a foreign Origin, a rebound Host (GET, POST), through a proxy (XFF), from another machine, /v1/../api, /v1/%2e%2e/api, /v1//api, and with a gateway key. The gateway routes on the web port: local agent 200; Origin null POST 403; foreign Origin 403; rebound Host 403 (with the page's cookie too); proxy or remote without a key 401 (with the page's cookie too); with a gateway key 200; not shared 403. - TestWebAssetsAreNoClientPath: no embedded asset shadows a client path. Run (full packages at the first rebase, on origin/main0b72ccbef): - go test ./internal/gateway ./internal/gui -count=1 -timeout 45m: ok (gateway 644s, gui 193s). - Rebased again on origin/main8b460833e(upstream touched catalog, provider, redact and usage only); at this head: the three builds and the vets below again, and go test -run 'TestClientPath|TestWeb' ./internal/gateway ./internal/gui: ok. The full packages were not rerun at this head. - gofmt -l internal/gateway internal/gui: empty - go vet ./internal/gateway ./internal/gui; GOOS=linux go vet -tags nogui ./internal/gateway ./internal/gui ./internal/cli; GOOS=windows go vet ./internal/gateway ./internal/gui: ok - go build .; GOOS=linux go build -tags nogui .; GOOS=windows go build .: ok Broken by hand, each red, then restored: - no onePort (the old web handler): agent 401 not 200, Origin null 401 not 403 (ServedHere). - Front without lanGuard: rebound Host 200, proxy without a key 200. - toGateway not copying the forwarding headers: proxy without a key 200, not shared 200 (ServedElsewhere). - no X-Forwarded-For for a remote peer: another machine 200. - Host not kept: rebound Host 200. - ClientPath taking unclean paths: TestClientPathOnlyClean red (4 paths). The gui tests stay green under this break, since the gateway's mux answers /v1/../api/settings with a redirect, not the UI API; the clean check is a second wall. Real try: the built binary, `magpie serve --web=127.0.0.1:3472` under a sandbox HOME (MAGPIE_ADDR=127.0.0.1:3471, MAGPIE_NO_ACCOUNTS=1, MAGPIE_TEST_SANDBOX), then Homebrew's nginx 1.31.7 (bottle unpacked in a scratch folder, proxy_pass to 3472 with Host and X-Forwarded-For) on 127.0.0.1:3473. Direct on 3472: / 401, /?k= 303, /api/settings 401 without and 200 with the cookie, /v1/models 200, Origin null POST 403, foreign Origin 403, X-Forwarded-For 403 (not shared). Through nginx, not shared: /v1/models 403 with the "came through a proxy" reason; the UI signs in with /?k= and /api/settings is 200 with its cookie. Sharing turned on and a key added from the UI API through nginx: /v1/models 401 without a key and 401 with only the page's cookie, 200 with the key; /v1/messages and a streamed /v1/chat/completions with the key reach the gateway (404: the sandbox has no provider); /api/settings with the key 401; Origin null and foreign Origin without a key 401; /v1/../api/settings 401; /api/tags 401 (the page's API, not Ollama's). A rebound Host on either port is refused only for a request that looks like a browser's (Sec-Fetch-Site), the same as the gateway port before. Not tried: a real model reply (and so a long stream) through the one port, since the sandbox has no provider; https, WebSocket (Codex's /backend-api/codex websocket) through nginx; Windows and Linux, other than vet and build; the GUI suite (no GUI asset changed).
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 |
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
- Identify the affected subsystem and the actual runtime path, including plugin or built-in ownership.
- Compare the claimed before/after behavior with the base and head implementation. Trace relevant callers and state transitions beyond the changed lines.
- Check that changed contracts appear in the owning reference and that source links still resolve.
- 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.