Files
magpie/docs/subsystems
yetone b14784c03f gateway: a fallback too small for the request is asked once every account with room is out of its allowance (Kai on Discord)
Kai on Discord (0.1.1278): Claude Code to claude/claude-opus-5-5, the
Claude provider's two subscription accounts (Max, Pro) both at their
limit ("resets 5:58am", "resets Oct 18 at 3pm"), its Fallback
codex/gpt-6.1-sol. Routing said the second account's 429 had nobody left
to try, and Claude Code had the 429. The fallback was never in the
route's order.

Cause: roomy (0f398c489, 10-15) leaves out, before anyone is asked, a
candidate whose known window is no larger than the request's estimate.
A Codex account's gpt-6.1-sol is 272K (its live list's context_window);
Opus holds 1M. A Claude Code conversation past ~272K tokens left the
Codex fallback out of the candidates and the trace's order, silently,
and when both Claude accounts failed for their allowance the agent was
told to wait hours. "It worked yesterday": the conversation was shorter.
A short request on current main already fell back (the new test's first
cases pass on the old code).

Fix: roomy hands back what it left out (small, with its order rows).
When the last candidate with room fails out of its allowance or credit
(failQuota/failCredit), nothing before it failed otherwise (other ==
nil), no account is pinned, and it isn't an overflow, refusal or
turned-away prompt, those left out that aren't resting are appended and
asked, their rows added to the trace's order, and the failed one rests
as one before the last does. The too-small fallback's overflow then
reaches the agent through the existing outgrew path, to compact, after
which it holds the conversation. Any other failure (a bad key, a busy
vendor) reaches the agent as before, the small member not asked, so
dgqyushen's free-200K-first group keeps its behaviour. Streaming and
non-streaming share the loop; count_tokens and System One's decision
models don't go through roomy. The Routing page still shows a member
left out and not asked only by its absence (no GUI change).

Tests:
- TestClaudeAccountsSpentFallBackToCodex (new): fake `claude` CLI that
  answers every account with rate_limit_event rejected + "You've hit
  your limit", a fake Codex backend, Claude's Fallback codex/gpt-6.1-sol,
  Codex's window 272K. Short request streamed / not / with accounts
  already resting: Codex answers (green before and after). A ~375K-token
  request streamed and not: red on the old code (429, codex asked 0,
  order = the two Claude accounts), green now.
- TestMemberTooSmallIsTheLastResort (new): group paid(10K) then
  free(2K), a 2.2K request. paid 401: the agent gets the 401, free not
  asked. paid 429 insufficient_quota: free answers, paid rests, free in
  the order.
- TestRoomyLeavesWhatItCantJudge checks the handed-back candidates.

Broken by hand:
- the new block disabled (false &&): both stream cases of the Claude
  test red (429, codex asked 0), TestMemberTooSmallIsTheLastResort red
  (429 insufficient_quota, free 0). Restored: green.
- the quota condition forced true: TestMemberTooSmallIsTheLastResort's
  401 case red (free asked, 200). Restored: green.
- the other == nil condition removed: still green; no test has an
  earlier member failing otherwise before a quota failure.

Run:
- gofmt -l: clean. go vet ./internal/gateway (darwin), GOOS=linux go vet
  -tags nogui ./..., GOOS=windows go vet ./...: clean.
- go test -timeout 45m ./... at de671719a (load ~240-470): every package ok,
  internal/gateway included (2451s), except three reds in packages this
  doesn't touch, each already filed and each a timing bound under load:
  internal/gui TestMainWindowCorners/macOS_26_SDK killed at its deadline
  (#1936, #2645; red alone too on this loaded machine), internal/library
  TestCheckStdio/timeout_ends_the_tree (#2765; -count=3 alone: ok),
  internal/plugin TestStopLetsARenewalEnd (#2743; -count=2 alone: ok).
- After rebasing on origin/main: go test ./internal/gateway -run with the
  new and roomy/overflow tests: ok.

Not done: Routing still shows a member left out and not asked only by
its absence; a row saying "too small for this request" would be a GUI
and i18n change of its own. Not tried against Kai's real accounts.
2026-10-18 02:17:02 +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.