Files
magpie/docs/subsystems
yetone d5bc9f0009 gateway: a plain 403 never rests an account until its window, and two accounts' alike 403 on one request rest neither (plugins#243)
zygs1083-dotcom on plugins#243: Factory answered 403 to a Claude Code
2.1.292 request (a changed instructions wrapper factory 0.1.33 didn't
adapt), and the accounts went into cooldown.

On ab7ec8c65 two things made that worse than one failed request:

- restAfterMarked's default branch rested any failure of an account
  whose cached reading was full (c.full: 98% under Smart) until the
  window reset, held to 8 days, by "window". A plain 403 says nothing
  of the window, so an account read at 98% sat out its week over it.
  A 401 or 403 no longer infers a window rest: one that names a quota
  is failQuota already (usedUpWords), and one that doesn't goes on the
  backoff ladder. Every other failure at 98% still rests by window.
- The candidate loop rested each account in turn for the same request's
  403, so one request walked the whole provider into backoff. Now a
  plain 403 (plainForbidden: failOther, not a model not served) from a
  second key or account of the same provider, model and reasoning in
  one request is the request refused: that one doesn't rest, the
  earlier mate's rest and counted failures are taken back (unforbid,
  only when the rest is still the one it took), its trace try loses
  its Rest, and the remaining mates are asked last (matesLast). The
  Routing page already words a failed try with no rest as "doesn't
  rest for it", so no new strings. One account's plain 403 alone still
  rests it as before.

docs/subsystems/gateway-routing.md says both, with the tests.

Tests (internal/gateway/plain403_rest_test.go):
- TestPlain403DoesNotRestUntilTheWindow: an account at 98% gets a plain
  403 and a plain 401: rests by backoff; a 403 saying used up still
  rests 39h by window; a 502 at 98% still by window. On ab7ec8c65's
  routing.go (the file swapped back) it fails: "a plain 403 at 98%
  rests 39h0m0s by window (other)".
- TestPlain403OnTwoMatesRestsNeither: three Codex accounts through the
  gateway against a fake ChatGPT backend: one 403 alone rests that
  account; two alike with a third answering, and two alike with nobody
  left, rest nobody and leave no failure counted. Broken by hand twice:
  the mate branch disabled (both alike cases red, "codex@me@example.com
  resting true, want false"), and unforbid leaving the counts (both red,
  "still counts 1 failures").

Run at ab7ec8c65 (macOS, go1.26.8, sandbox HOME), both commits in the
tree: gofmt, three vets clean; set -o pipefail; go test -timeout 45m
./...: exit 0, 64 packages ok, no FAIL (load ~200).

Rebased onto b641868eb (origin/main, which moved gateway.go and
routing.go: b749cffbb's reply-room retry on a 400, b641868eb's
heldClockIn; neither touches the 403 path) and run again at a33699df6
(this pair's tree), macOS, go1.26.8, sandbox HOME: gofmt -l none; go
vet ./... (darwin), GOOS=linux go vet -tags nogui ./..., GOOS=windows
go vet ./...: all clean. go test -count=1 -timeout 45m
./internal/provider: ok (809s). go test -run
'TestPlain403|TestOrderedKeepsAnAccountAt98|TestPromptRefusalRestsNobody|ReplyFitsTheWindowLeft|TestHeldClock|Rest|Forbid|403'
./internal/gateway: ok (110s). go test -count=1 -timeout 45m
./internal/gateway: ok (1762s, load ~300). A full ./... at a33699df6 was started
and stopped after 46 minutes with one package done (load 320-420 from
other sessions, disk at 8.7G free); the earlier full run of the same
two commits on ab7ec8c65 was exit 0, 64 packages ok.

Not tried: no real Factory account or request; the 403 is a fake
backend's.
2026-10-19 22:17:39 +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.