Files
magpie/internal/plugin/testdata
yetone 7d9165d842 gateway: tests for plugin-served Factory with Standard usage only, 99% caps and order routing (plugins#349)
TestPluginFactoryStandardOnlyCaps runs the fake plugin as factory (marked
moved, two OAuth accounts A then B, order routing, each capped at 99% with
SetAccountCap as `magpie provider account-cap factory <account> 99` does).
The windows are those Factory's plugin 0.1.38 reports with standard_only
on (limitWindows(l, true)): Standard's 5 hours and 7 days with no model
list, Droid Core's and Extra usage aside. The fake plugin gains
$FAKE_WINDOWS, a file of each account's windows read at each usage
reading, so routing reads them through the host and provider.Allowances,
not a stub.

What it holds, today's behaviour, no code change:
- A's 7 days at 99% (its 5 hours at 40%): A is not sent the request; B
  serves fake-1, fake-claude and glm-5.3 (a Droid Core model). The trace
  shows a@fake 99/99 left out with its reset. Core windows at 100% on an
  account below its Standard caps don't hold it.
- Both capped (A's 5 hours 99%, B's week 100%): 429 rate_limit_error,
  "usage cap reached: every account that serves ... - Factory (a@fake) is
  at 99% of a usage window, past its 99% cap, until <time>; Factory
  (b@fake) ...", advice naming magpie provider account-cap, Retry-After
  at the sooner reset (A's 5 hours), nothing sent to the vendor, logged
  "every account at its usage cap".
- A read again below its cap serves again; a kept reading still at 99%
  whose reset time has passed serves too.
- A conversation on A moves to B when A is capped. Once A is back: auto
  affinity with no cache worth keeping goes back to A; with B's cache
  (5000 cached tokens) it stays on B; affinity session stays on B. A new
  conversation goes to A.

Broken by hand, each red at the step it guards: capHeld returning nil
(A served while capped), allowanceOf keeping aside windows (glm-5.3 went
to B while A's Standard windows were below cap), CapHeld ignoring a
passed reset (429 where A should serve).

Run: go test -tags nogui ./internal/gateway -run
TestPluginFactoryStandardOnlyCaps -count=1 -v, ok; go test -tags nogui
./internal/gateway ./internal/plugin ./internal/provider ./internal/cli
-count=1 -timeout 45m at base ed91aa04e: all four ok (gateway 494s,
plugin 217s, provider 191s, cli 88s); rebased onto ef1a203df and the new
test run again there (below). gofmt -l clean; GOOS=linux go vet -tags
nogui and GOOS=windows go vet of ./internal/gateway clean. No full ./...
suite: tests and testdata only, and another session's full suite was
running.

Sandbox HOME, built binary, fake plugin as factory, two accounts signed in
with magpie plugin login factory (browser opener stubbed on PATH):
magpie provider account-cap factory alice@fake 99 -> "capped at 99% of
each usage window", saved as accountCaps in providers.json; --window
"7 days" 95 and default work; an unknown account and 150 are refused.

Not tried: real Factory accounts, the real plugin's own option toggle in
magpie (its own usage.test.mjs covers the switch), the Anthropic
/v1/messages route's 429 body.
2026-10-24 15:33:17 +08:00
..
providers, sign-in, tray, plugins: four of StringKe's reports on Discord — (1) Removing a provider left its editor open and listed, and a second Remove said there was no such provider: the removal had been made, but moving the agents off its models failed on one agent whose config couldn't be written, and Reseat returned that as the change's error; now such an agent is a move with its error ("still on X, not moved: …", a warning in the panel, a line in the CLI and TUI), the others are moved, and the panel, told a delete failed, re-reads the list and closes the editor when the provider is gone. (2) The sign-in waiting sheet shows the sign-in link itself, selected whole on a click, with a copy button, for a browser other than the one that opens. (3) A subscription with several accounts offers "Account in use" among the menu bar's usages, following the account the gateway goes to first. (4) The Antigravity plugin's sign-in never started: magpie called every plugin's authorize with an inputs object, the way OpenCode's CLI does, and opencode-antigravity-auth then asked its Project ID on the terminal — magpie's stdin — until the minute ran out; inputs now go only to a method with prompts, as OpenCode's TUI passes them (checked against the real package under Bun in a sandbox: the Google sign-in URL comes back at once). TestReseatUnmovable, TestTrayInUse, TestInUseOf, TestAuthorizeNoPrompts, provider-remove.test.cjs, signin-link.test.cjs and tray-inuse.test.cjs fail without the change. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass; click-scroll, old-webkit, tray-usages, plugin-signin, signin-callback, factory-sub pass.
2026-09-30 20:45:23 +08:00
providers: Max concurrent requests — over it, an agent's requests wait in line and go out in the order they came (Lemon on Discord: 希望 provider 或者插件能够支持一个参数类似 max_concurrency,如果超过上限,放到无界队列里面依序请求; Memo: ChatGPT/Codex subscriptions' risk control trips at about 5-6 requests at once) — the gateway sent every request to its provider the moment it came, so a few agents (or one agent's subagents) on one subscription could have any number out at the vendor at once, and nothing in magpie could hold them to what an account takes. A provider now has maxConcurrency (providers.json; nil or 0 is no limit, a negative one is saved as 0), and a signed-in agent's accounts keep theirs with their picks. The limit is per key or account, as the gateway already tells them apart (candidate.who(): provider#keyid, provider@user, else the provider), so a subscription pooling several accounts lets each one have that many out. Before a try is forwarded it takes a slot in that lane (internal/gateway/concurrency.go); with none free it waits in a FIFO queue with no bound, and is let in, first come first served, as one before it ends. The slot is held for the whole try (attempt reads the vendor's reply to its end or until the agent hangs up, and its body is closed when it returns) and given back once whatever happens (sync.Once), and a lane with nothing out and no one waiting is dropped. An agent that hangs up while waiting leaves the queue at once, is never sent and takes no slot (a slot granted in the same instant is handed on), and goes the way of any canceled request (499, nothing recorded). Waiting is not a failure: a request in line stays for its provider and is not moved on to a fallback or another group member, the account isn't rested, and the time it waited is told in its route's try (Try.Queued, ms). A plugin can say how many each of its accounts takes, as its own field: the auth hook's maxConcurrency, else package.json's magpie.maxConcurrency (a whole number over 0, at most 1000); the user's setting on the provider goes over it. GET /v1/magpie/concurrency (local or shared callers, as quotas) gives each lane's out, waiting and limit. The provider editor (keys, custom, signed-in accounts) has "Max concurrent requests" / 最大并发请求数 under Proxy, "超出时排队,按顺序发出;0 或留空为不限制" below it (with a plugin's own number as its placeholder), refuses anything but a whole number from 0 to 1000 before anything is posted, and an empty field saves none (the plugin's, if it has one); a save that doesn't send the field keeps what the provider had. No coloured left border, and a click in the field leaves the page where it is. TestMaxConcurrency (limit 2, five streams: never more than 2 at the vendor, 3 waiting, let in a..e in order; one canceled while waiting leaves the queue, takes no slot and never reaches the vendor; slots given back when each stream ends and when an agent hangs up mid-stream; the next one served at once) fails without the change (all five reach the vendor at once, no lane), as do TestLanesLimitLifted, TestProviderConcurrency and the updated TestPluginOwnIconAndKeyHint (the auth hook's 3, package.json's 5 when the hook's is 2.5); provider-concurrency.test.cjs (chromium and webkit, en and zh: typed and saved, 2.5 refused with nothing posted, empty saved as null, an account opening on its 5 and saving 0, a plugin's 4 as the placeholder, every string translated, no stripe, no scroll on click) passes. No vendor was asked. go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui -count=1 ./... (under a temp HOME) and go test -race on the new tests pass. provider-proxy.test.cjs's account test fails the same on HEAD without the change (its editor redrawn under it).
2026-10-02 13:38:23 +08:00