Files
magpie/docs/subsystems
yetone 3f1f1882ac claude: magpie's model names never stay in a Claude Code switched off it (roswellcsy on X)
roswellcsy on X (v0.1.2846). Claude Code was switched off magpie and on
its own sign-in, with several Claude accounts and one made first. Every
session said "There's an issue with the selected model
(claude/claude-opus-5-5)", while settings.json's model was the bare
claude-opus-5-5.

Cause: a name of magpie's left in Claude Code's env.
- The env's ANTHROPIC_MODEL outranks settings.json's model (Claude Code
  2.1.316: Wg(), `i.ANTHROPIC_MODEL||settings.model`). The tier variables
  stand in for opus/sonnet/haiku. On Anthropic's endpoint a
  provider/model name is a 404, shown as that message.
- How it got there and kept coming back:
  - Connecting a Claude Code that is not routed stashes its env models as
    "the user's own" (claude.env.<VAR>), magpie's names included.
  - Disconnect (unroute) then writes them back, again unchecked.
  - Once one is there, every connect and switch-off keeps it.
  - The bare model is the user's own, from connect.was or untouched.
- Nothing took the name out afterwards:
  - unroute returned at once when not routed;
  - Sync only did desktopOnly;
  - Disconnect is a no-op once Wired() is false (a bare model with no
    gateway URL).
- Sibling: wiring at an older gateway address (magpie's key) was left in
  place by Disconnect, while set("") already took it out.

Fix (internal/agent/claude.go):
- claudeMagpieModel(v): v is a catalog ref, read through ClaudeRead and
  with any :effort or [1m] taken off.
- It is never stashed as the user's own, and never given back from a
  stash (one an older magpie wrote included).
- dropStray: with no ANTHROPIC_BASE_URL (Anthropic's own endpoint), every
  claudeModelEnv key and `model` holding one of magpie's names is taken
  out. ANTHROPIC_MODEL, SMALL_FAST, SUBAGENT, the four tiers and the
  auto mode model are covered.
  - It runs from Sync when not routed, so SyncCatalog repairs users
    already in this state at the next start.
  - It also runs from unroute when not routed (Disconnect, the default
    model, a non-magpie model).
  - An endpoint of the user's own may name its models so, and those
    stay.
- unroute, when not routed: wiring at an older gateway address
  (keyOurs) goes whole, as set("") takes it.
- docs/subsystems/agent-wiring.md says so.

Test: TestClaudeOffLeavesNoMagpieModel, in a sandbox HOME. Each subtest
reads settings.json as Claude Code starts on it (ANTHROPIC_MODEL over
model, the tiers, small fast, subagent, auto mode).
- start: the stray env, then SyncCatalog;
- connect + Disconnect twice;
- a stash an older magpie wrote;
- the user's own models come back;
- an older gateway address;
- another endpoint's names are left alone.

Red on the old code: start, connect+Disconnect and older-address fail
(red-old.log). "connected and switched off again" reproduces the report
exactly: model "claude-opus-5-5" with env ANTHROPIC_MODEL
"relay/claude-opus-5-5".

Broken by hand, each through mote-run:
- dropStray out of Sync: start red.
- Both stash guards out: connect+Disconnect red.
- The restore guard alone out: older-stash red.
- keyOurs cleanup off: older-address red.
- With the stash guard alone out, the restore guard still holds, so it
  stays green.

Run on mote (mote-run, at d8501848a plus this change; the doc edit came
after the run):
- go test ./internal/agent/ ./internal/provider/ -count=1 -timeout 45m:
  ok agent (200s), ok provider (136s).
- The new test: 7 of 7 PASS (the parent and 6 subtests).
- GOOS=windows go vet ./...: ok.
- GOOS=linux go vet -tags nogui ./...: ok. Without the tag it stops in
  Wails' cgo-only Linux files when cross-compiling, which this change
  doesn't touch. GOOS=linux go vet ./internal/agent/ ./internal/provider/
  is ok too.
- gofmt -l: clean.

Not tried:
- A real Claude Code run against Anthropic. The 404 path was read from
  the 2.1.316 bundle, not hit.
- The user's literal provider id claude/: the test uses a relay
  provider. isMagpie resolves both the same way.
- WSL's Claude Code goes through the same claudeIn and wasn't run.
- A Claude Desktop Code tab with a provider/model in settings.json's
  model and no base URL would also lose it at Sync. Desktop writes
  mythos-magpie-<n> ids there, not provider/model ones.
2026-11-03 19:18:50 +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.