Files
magpie/docs/subsystems
yetone 803d7747e7 gateway: an account rate limited till a time its vendor named is not asked again before it (hsjzg58958 on X)
hsjzg58958 on X: a vendor limited an account for 300 seconds, and magpie
asked it again about every half a second as the agent retried, until the
vendor flagged two accounts.

Cause: a candidate resting on a 429 was still "tried all the same" when
it was the last one left (restLast/probeOne), and the last one left was
never rested at all, so every client retry went upstream. Codex on its
own sign-in is relayed by codexUpstreamOn outside the candidate loop and
had no hold at all.

Fix:
- timedHeld/vendorTimed (fallback.go): when every candidate left rests on
  a 429 whose time the vendor named (Retry-After, anthropic-ratelimit-*-
  reset, x-ratelimit-reset-*, resets_in_seconds/resets_at), none is asked;
  the agent gets 429 with Retry-After = when the first is back. A ChatGPT
  account out of its allowance is answered as the backend's
  usage_limit_reached with its x-codex-* windows (Rest.out/outHeader).
- The last one left now rests too on such a 429 (vendorSaidWhen); a 429
  naming no time rests nobody, as before.
- codexHolds (codex_held.go): Codex's own relayed sign-in is held till the
  vendor's time; its retries are answered locally with the stored body,
  x-codex-* headers and Retry-After. Lifted by restarted() for quota holds.
- Not held: LimitRestOff accounts (the user's setting), auto-reset weeks,
  rests "there" (answered by the vendor's own hold), Codex's API key.

Run:
- New TestRateLimitedKeysHeldUntilTheirReset (4 signals x 1 and 2 keys)
  and TestRateLimitedCodexAccountsHeldUntilTheirReset (3 refusals x 1 and
  2 accounts): red at fc9ce34d8 (each key/account asked on every retry),
  green after.
- Broken by hand: held branch off -> every case red (4 extra hits);
  last-one rest off -> 1-key cases red, Retry-After lost; codexHeld off ->
  1-account Codex cases red.
- mote: go test ./internal/gateway -run 'TestRateLimited|TestAllResting|
  TestQuotaHeld|TestGroupSpent|TestCodex|TestRetryAfter|TestHeldLimit'
  ok; TestCodexLeavingModelCompactsLocally updated (the used-up account is
  now asked once, then held) and ok.
- gofmt -l clean; go vet, GOOS=linux -tags nogui and GOOS=windows go vet
  and build ok.
- mote, whole internal/gateway package at the rebased head 46199a6ae
  (on 80161247a): ok but for TestStrictAffinityPassesTheErrorOn, which
  asserted the bug itself ("resting now, it is still the one asked"). A
  strict conversation's account resting till ChatGPT's named reset now
  gets its own usage-limit 429 from the rest, with nobody asked; updated,
  and with TestRateLimited|TestCodexLeavingModel|TestCodexEveryAccountOut
  ok at this head.
- The full ./... suite was queued behind six other sessions' suites on
  mote's lock and not run before the push; queued again at this head.

Not tried: a real vendor account limited live (no account was rate
limited to test on); Codex API-key relay is not held (OpenAI's platform
headers are Codex's own to read); plugin providers are held only through
the shared candidate loop; a held Codex passthrough body's
resets_in_seconds is stale by the time elapsed (Retry-After is exact).
2026-11-10 00:58:01 +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
One skill, magpie-skills, listing the Library's skills in place of each agent's own Skill 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.