Files
magpie/docs/subsystems
yetone 3b4983710a gateway: a usage cap holds with sessions in parallel (yalifesign on X)
yalifesign runs 4 or 5 sessions on one ChatGPT Plus account with a 90%
cap and capStop/capCareful off, and the five-hour window went to 100%
and Codex's own 429. Caps from 99 down to 90 all overshot.

Cause: a Codex reply reports usage (codex.rate_limits, x-codex-*) as
its turn begins, without that turn's own spend. The near-cap gate
(435f04151) took one turn at a time only once a reading was inside the
5-point band. It neither counted nor waited for the turns already out
on the account, and after waiting it read usage again only when the
reading hadn't changed, which a pre-turn reading always changed. Five
sessions start turns on readings outside the band that lag every turn
in flight, and they pile up past the cap.

Fix, in serve's walk and cap_near.go:
- turnSpends learns what one turn spends per account (the rise in used
  over the turns returned since it last moved, at most 2x the band) and
  Server.nearCapOf judges "near" on the reading plus that spend for
  every turn out or returned since the reading.
- Near the cap, after taking the near slot, a turn waits until no other
  turn is out on the account (lanes.out, lanes.idle; a lane slot handed
  to this request is its own and not waited for). With other
  candidates it moves behind them first, as before.
- nearCapAgain reads usage again (bounded by carefulWait) when a turn on
  the account came back after the last good read (lanes.ended vs
  UsageRead), and still when it waited and the reading is unchanged.
The gate is generic over the account, so Claude accounts and plugin
accounts get the same hold. capStop and capCareful are read as before;
nothing changes a value the user set. A key's usage cap has no
near-cap gate at all, before and after this: #6928.

Tests:
- TestCapHoldsUnderParallelSessions: a fake Codex that reports usage
  before each turn and adds 1.5 points after it, 5 sessions x 40 turns
  from 60% to a 90% cap. Fails above cap + one turn (91.5%).
- TestNearCapReadsAgainAfterATurnCameBack, TestNearCapWaitsForTurnsAlreadyOut,
  TestTurnSpendsAhead, TestLaneIdleLeavesItsOwnSlot.
- TestNearCapWaitsThenHolds now has the usage read after the first turn.

Run:
- origin/main adfa196db with the two gateway tests (-count=10, mote):
  TestCapHoldsUnderParallelSessions red 9 of 10 (91.5 to 97.5%),
  TestNearCapReadsAgainAfterATurnCameBack red 10 of 10.
- With the fix: the parallel test 20 of 20 at 90.0%, earlier 39 of 40
  at 90.0% and 1 at 91.5%; near-cap, lanes, key slot and careful tests
  -count=3 ok.
- Broken by hand: no spend estimate, 8 of 20 red (93.0, 94.5%); no
  re-read after a returned turn, ReadsAgainAfterATurnCameBack red 10 of
  10; no wait for turns out, WaitsForTurnsAlreadyOut red on every run.
- go test ./... -timeout 45m on mote at 884bcadb6 (base ff88ec264): 103
  ok, no FAIL. Rebased on 618bfc1a9 (kimi, agent and gui only), then
  internal/gateway, internal/agent and internal/gui again: gateway, agent and gui ok at bb23d1b69.
- gofmt -l clean; GOOS=windows go vet ./... ok; go vet ./... on Linux
  (wsl-run, magpie-desktop): ./internal/gateway/... and ./internal/provider/... ok; the whole tree stops on
  gtk4/webkitgtk-6.0 missing in that distro. GOOS=linux vet from macOS
  stops in Wails' cgo files, as on main.
- No GUI change, so no GUI suite.

Not tried: a real Codex account at its cap with parallel sessions (the
fake reproduces the reported shape, not OpenAI's exact accounting); the
spend estimate on a window that renews mid-run beyond the unit test;
key caps (#6928).
2026-11-17 14:54:11 +08:00
..
2026-11-10 03:25:44 +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
Memory shared by every agent: the memory MCP server, the digest in instructions, the Memory tab Memory
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.