Files
magpie/docs/subsystems
yetone be7f174677 gateway: a key out of a guessed quota is asked again after one refusal, and held a minute (zoecats)
zoecats (Discord #feedback, 10-25): a local aggregator answers 429
insufficient_quota "quota exhausted" for the moment it switches
upstreams, and the agent's retry and every request after it were
answered by magpie itself, "out of its allowance: not asked again
before …", Retry-After 900. v0.1.1488 (c6768f06f) let it through only
when the user unticks the provider's new "Rests" switch; by default it
was still held (checked on origin/main 4dfd88206 below). The owner's
zero-config rule wants it to just work.

Cause: quotaHeld (#1772) answers from the rest, without asking, as soon
as every key left rests out of its allowance or credit, from the first
refusal on, for as long as the rest: 15 minutes (quotaRest) or 30
(creditRest) that magpie guessed when the vendor named no time.

Now (Rest.first, Rest.askAt, Rest.asksAt), for a rest whose length
magpie guessed (creditRest, or quotaRest with no OwnQuotaRest):
- The key is held only once it was refused so twice in a row. The first
  refusal still rests it (the order passes it over for a ready
  candidate), but the next request, the agent's retry first, asks it
  again; served, its rest is cleared as before.
- Held, it is asked again a minute on (fallbackCooldown), once, and
  held again if refused.
- A time the vendor named (resets, Retry-After, a window) or the user
  set (quotaRest, QuotaResetAt) holds it from the first refusal to
  then, as before. A first version gave every refusal one more ask;
  the full gateway suite turned it down in TestClineFreeLimitLeaves-
  OtherModels and TestZhipuRestsUntilItsReset (a named reset asked
  again), so it is limited to the guess.
  #1772's keys out for the week are asked at most once a minute each
  while requests come, not on every request.
- soonestBack, the Retry-After a 429 without one gets, says when the
  first of them is asked again: none after a first refusal, about 60s
  for a guessed hold (was 900 or 1800). That narrows #3556 for a
  guessed hold; a vendor-named or user-set one is as before.
- A rate limit was already asked again (restLast, retries within the
  request) and is unchanged; the new test holds it there, Retry-After
  0 and every key resting included.
- The "Rests" switch (LimitRestOff) stays: unticked, nothing rests at
  all.
docs/subsystems/gateway-routing.md says so.

Run (worktree at origin/main 4dfd88206 plus this change):
- Reproduced first on the old code with tests:
  TestRetryAfterALimitIsAsked (new; 429 insufficient_quota, a usage
  limit with no reset, the same with Retry-After: 0, 402, every key of
  two; plus two rate-limit cases): the five limit cases red on
  4dfd88206 ("request 2 after the refusal: 429 … not asked again before
  14:34"), green now; the rate cases green on both.
  TestHeldLimitAskedAgainInAMinute (new) red on the old code (Retry-After
  900, not asked a minute on), green now. TestAllRestingAnswersFromRest,
  TestOwnQuotaRest and TestQuotaHeldOnlyForKeysOutOfAllowance changed to
  the new rule (each key asked once more, then held; held Retry-After
  about 60s; a user-set week held from the first refusal); red on the
  old code. TestRemoteMagpieRestsTheModel (#2077) asks Claude Code once
  more before its hold: the other magpie can't read "resets 8:10pm
  (America/Los_Angeles)", so its 15 minutes there are a guess.
- Broken by hand, each red, then restored: r.first always false ->
  TestRetryAfterALimitIsAsked's five limit cases; askAt never set ->
  TestHeldLimitAskedAgainInAMinute and TestAllRestingAnswersFromRest
  (Retry-After 900).
- Sandbox HOME, a nogui build of 4dfd88206 and of this tree, `serve` on
  127.0.0.1:3481 over a Python fake answering 429 insufficient_quota
  "quota exhausted" once, then 200: old: 429, then 429 "not asked again
  before 14:41", 429 again, Retry-After 900, 1 upstream POST. New: 429
  (no Retry-After), 200, 200, 3 upstream POSTs (rerun on the final tree). The fake refusing every
  time, new: 429, 429 Retry-After 60, then held "not asked again before
  14:27" Retry-After 60, 2 upstream POSTs.
- go test -tags nogui -count=1 -timeout 45m ./internal/gateway/ at
  4dfd88206 plus this tree, through suite-lock (after the DSH run):
  ok 446.6s. The first full run, before the rule was limited to a
  guess, was red in the three tests named above.
- gofmt -l clean; go vet ./internal/gateway/ (darwin); GOOS=linux go
  vet -tags nogui ./...; GOOS=windows go vet -tags nogui
  ./internal/gateway/: ok.

Not tried: zoecats' own aggregator; a real vendor's out-of-credit key
(OpenAI insufficient_quota) to see the once-a-minute ask in use; the
GUI (no GUI file changed; the Routing page reads the same rests);
Windows box (Windows CI compiles the tests only).
2026-10-25 15:02:12 +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.