Files
magpie/docs/subsystems
yetone 94df239265 gateway: answers as an Ollama server, so Copilot in Visual Studio and VS Code lists magpie's models when given the gateway as its Ollama URL (wani on Discord)
Visual Studio 2026's Copilot (Agent preview, bring your own model) takes a
custom endpoint only for OpenAI and Ollama. wani's OpenAI attempt failed
with "Session was not created with authentication info or custom
provider". That error comes from the Copilot runtime in VS: the session was
created without a GitHub token or a BYOK provider config, and nothing
reaches magpie, so magpie can't fix it.

What an Ollama client needs, read from vscode-copilot-chat's
src/extension/byok/vscode-node/ollamaProvider.ts (5863f5a):
- GET /api/version, which must be at least 0.6.4, or no model is listed;
- GET /api/tags, from which it takes each model's "model";
- POST /api/show of each. A model without "capabilities" is skipped. The
  window is model_info["<general.architecture>.context_length"] ?? 32768,
  and the name is general.basename;
- chat on <url>/v1/chat/completions, streamed, with an empty
  "Authorization: Bearer ". magpie already serves that.
It never calls /api/chat. The Copilot SDK under Visual Studio's agent
treats Ollama as an OpenAI API at <url>/v1 too.

ollama.go serves these three routes on Handler's mux, so every guard
/v1/* has covers them too:
- lanGuard: LAN sharing, a gateway key, rebound Host, forwarded loopback;
- corsGuard: a foreign Origin, Origin null and *.localhost;
- a key held to some models: catalogFor/keyAllowed.

/api/show tells:
- capabilities: completion and tools, plus vision from Images and thinking
  from Thinks;
- the window, or 32768 when magpie doesn't know it, since VS Code would
  take a 0;
- general.parameter_count and magpie.embedding_length, because the ollama
  CLI 0.6.8's "ollama show" read them unchecked and panicked without them.
  The real CLI found this.
/api/chat and /api/generate are not served, so "ollama run" doesn't work.

Tests (internal/gateway/ollama_test.go; lan_test.go lists the routes):
- TestOllamaModelRoutes, TestOllamaRoutesHoldAKey, TestOllamaRoutesGuarded
  and TestOllamaClientChats each fail with 404 without the routes.
- Broken by hand, each went red:
  - tags from provider.Catalog() instead of catalogFor: HoldAKey;
  - vision dropped: ModelRoutes;
  - corsGuard skipping /api/: Guarded (200 for a foreign page, Origin
    null and a localhost subdomain);
  - an unknown window told as 0: ModelRoutes;
  - parameter_count and embedding_length left out: ModelRoutes.

Real use, in a sandbox HOME with MAGPIE_NO_ACCOUNTS=1:
- a nogui build on 127.0.0.1:3561, with a custom provider on a fake
  OpenAI upstream (python, 127.0.0.1:3562);
- the real ollama 0.6.8 CLI with OLLAMA_HOST at it: "ollama --version"
  (server 0.12.6), "ollama list" (both models) and "ollama show" all
  worked. Before the model_info fix, show panicked;
- curl replaying VS Code's sequence (version, tags, show, then a streamed
  /v1/chat/completions with an empty Bearer and VS Code's body) streamed
  the upstream's reply, and the upstream got its own key;
- on the same server, a foreign Origin, Origin null and a rebound Host all
  got 403.

Not tried:
- Visual Studio 2026 itself (no Windows VS here);
- VS Code's Copilot itself (VS Code isn't installed);
- JetBrains' Ollama client, which likely needs /api/chat.

Run at this head (macOS):
- gofmt -l .: nothing;
- go vet ./...; GOOS=linux go vet -tags nogui ./...; GOOS=windows go vet
  ./...: clean;
- go test -timeout 45m ./... at e89d6a4f9, load ~300 to 400: 59 packages
  ok. These failed:
  - internal/agent, internal/gateway and internal/library ran past 45m
    (#2544, #2870, #2853);
  - TestClaudeAccountsSpentFallBackToCodex in gateway (#2858);
  - TestSetAnswersWhileDevinAndProvidersHang in gui, at 16s against its
    bound (#1375);
  - TestGatewayHookMixedPiPackage in plugin, starting plugins after 742s
    (#2865).
  Rerun alone:
  - gui's and plugin's tests: ok (gui 35s, plugin 499s);
  - internal/agent ok (2580s), internal/library ok (1112s);
  - internal/gateway in two halves by test name, each ok (1406s and
    1030s). Whole, it timed out again at 45m while starting
    TestRouteKeepsItsFolderShort, not stuck.
No GUI file changed.
2026-10-19 22:32:37 +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
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.