Commit Graph
7 Commits
Author SHA1 Message Date
yetone f5c956b848 plugin: a 401 on an access token its refresh token renews makes it due, not the sign-in gone (plugins#618)
jinpy666 (magpie-community/plugins#618): with Copilot plugin 0.1.0, the
two Copilot slots that carried no traffic said "sign-in has expired —
sign in again" within a day or two, while their GitHub OAuth token still
traded at /copilot_internal/v2/token. A plugin's bare 401 (no
X-Magpie-Sign-In) marked the account whatever the cause, and a mark is
cleared only by a success or a usage read that says so, which an idle
slot never gets.

The host now judges such a 401 against the sign-in as the request found
it (staleAccess in host.js). For an OAuth sign-in with a refresh token
whose access token is still the stored one and not due yet, it makes the
token due (expires 1, under the auth lock, only while the access token is
unchanged), drops the account's loader and says "kept": the gateway
fails the request over as before, and the next request renews the token
through the plugin's loader (or magpie's auth.refresh) and goes through.
Still marked: an access token renewed for this request and turned away,
one that was due already and not renewed, an account with no refresh
token, and a plugin saying "expired" (a refused refresh). A sign-in that
can't be read when the 401 comes is unknown: kept, not marked. A marked
account stays routable (pluginAlsoOn doesn't filter marks), so a slot
marked before this is renewed on its next request and the success clears
it. Copilot isn't a mover; its plugin 0.1.1 (plugins 53d4813, on the dist
branch; never on npm) says "expired" for the exchange's own 401 and
"renewed" for a quota read GitHub answers, which clears old marks.

TestPluginStaleAccessRenewsNotLapses (new, fake plugin + local vendor,
no network): idle account kept and due after the 401, renewed and 200
on the next request; a renewed token refused is marked; a refused
refresh (signIn "expired") is marked; plugin-auth.json unreadable at the
401 marks nothing; a marked account is still routed. TestPluginFailover's
"gone" case asserted one 401 marks; it now asserts no mark after that
401 and the mark once the account's renewed token is refused too.
docs/subsystems/provider-plugins.md has the rule.

Run at this commit's tree (mote, rebased on 541102eba):
- go test -count=1 -timeout 45m ./internal/plugin/ ./internal/provider/
  ./internal/gateway/: plugin ok, provider ok; gateway red on
  TestCodexPoolSaysTheClientsVersion (#4807) and
  TestUnroutedHeldReportSaved (#4300), both filed, full-run-only; both
  pass alone here and at 541102eba.
- GOOS=linux and GOOS=windows go vet of the three packages: ok.
- gofmt -l: clean.
Broken by hand: host.js at the old code, the new test is red ("idle@fake
was marked for a dropped access token its refresh token renews"); the
unknown branch returning "", red on unknown@fake.
Not tried: a real Copilot account (or any real vendor's dropped access
token); which 401 marked the reporter's slots first isn't confirmed (a
request failed over to them, most likely); a plugin that keeps a future
expires but never renews (it is marked on its second 401, not its
first); GUI unchanged, no GUI run.
2026-11-02 12:03:24 +08:00
yetone 3dc25a6168 tests: a plugin fake's vendor counts only its own test's requests (#3357)
Run 39913278700 at 65e80405b (ubuntu, Race): TestClinePluginPinsDeepSeekAndGLM
"the plugin sent 7 requests" for 6 asks, in 0.33s. #3347 was the same
class: TestPluginToldWhereTheAgentWorks got 2 for 1, in 0.66s.

Cause, as far as it is known. The extra request did not come from the
test's own route:
- not a gateway retry: retryPause is 1s, so a retried ask can't finish
  in 0.33s or 0.66s. #3347's "a first try failed with a 5xx and was
  retried" doesn't fit either run's time;
- not the plugin host: the gateway reaches the host over stdio JSON-RPC
  (no HTTP retry), and host.js fetchAs calls the loader's fetch once;
- a never-reset log of every request the cline test's vendor got showed
  exactly 12 (6 asks x 2 runs), all POST /v1/chat/completions from
  magpie/dev, with nothing in the background.
So the seventh came from outside the test. The likely sender is a late
request from another test, in this shard or another of race-shards.sh's
four processes, reaching a port the kernel handed to this test's
httptest server (the class d0ccbbebc met with a freed port). Which test
sent it is still unknown.

Fix the class: testenv.FakeVendor(t, h) serves a fake plugin's vendor
at $FAKE_BASE and sets $FAKE_RUN to a nonce made for the test. The fake,
renew and renewrace plugins send it as X-Fake-Run. A request without it
gets 421 and is logged with its method, URI, remote address, User-Agent,
Authorization and size, so the next one on CI names its sender. Every
gateway and plugin test that served the fake's vendor with
httptest.NewServer plus FAKE_BASE now uses it (10 gateway, 5 plugin
files). The cline test now counts against the asks' recorded tries, as
65e80405b did for the workdir test: it fails on a real duplicate and
logs each retry's status. No exactly-once guarantee was loosened.

Reproduction by injection, at be002aca0:
- a request to FAKE_BASE from outside the route, with the filter off:
  "the plugin sent 7 requests for 6 asks tried 6 times" (the CI shape);
  with it on, the test passes and logs the 421 line;
- the fake plugin fetching twice for glm-5.3 (a real duplicate) still
  fails: "sent 8 ... tried 6". Both breaks were restored.

Ran at be002aca0 (macOS, -tags nogui):
- gofmt -l clean; go vet ./... darwin, GOOS=linux -tags nogui, GOOS=windows ok;
- internal/plugin -race -count=20 ok (157.5s);
- the edited gateway files' tests -race -count=20 ok (93.1s);
Rebased on c8c6e7e23 (it brings TestPluginThinkingOff, on besideFake),
and ran there: gofmt -l clean; vet of the three packages for darwin,
linux nogui and windows ok; TestPluginThinkingOff, the cline and the
workdir tests -race -count=20 ok (18.3s); internal/gateway (363.6s),
internal/plugin (83.9s) and internal/testenv -timeout 45m ok, with no
421 line logged;
- a Linux container run of gateway.race in four shards like CI's,
  -test.v: ok, and no stray was seen.

Not tried: the stray was never caught in the act, here or in the
container, so the sender is a hypothesis. The count=20 run over all 142
besideFake tests hit 45m in TestPluginStreamLargeReplies (sizing, no
FAIL) and was narrowed to the edited files. No product code changed, so
the full ./... suite was not run.
2026-10-23 16:57:33 +08:00
yetone 2da8fbe488 tests: every test that runs plugins on the bun on PATH runs it once first, through testenv.Bun, so a bun that has never run here doesn't time out internal/provider's and internal/plugin's plugin tests (#1306)
65dcc573 (ggbdpq's patch 1) warmed the bun on PATH before the root
package's one-minute plugin contexts. The same lookup of the bun on PATH
is in 40 more places in internal/provider (28), internal/gateway (8),
internal/plugin (3) and internal/gui (1), and in the root package's
TestPluginListSaysClash. Many of them then open a one- or two-minute
context (internal/provider's sign-in, fetch and move tests,
internal/plugin's sandbox), and there macOS's first-exec check of a
just-installed or upgraded bun landed inside the deadline. The rest
are covered the same way, so no test has to remember which is which.

testenv.Bun now does what the root package's pathBun did: it finds the
bun on PATH (skipping the test when there is none) and runs
`bun --version` before the test opens its context, once per bun per test
binary. pathBun is gone; every `exec.LookPath("bun")` + skip block in
the tests uses testenv.Bun instead. Three tests reach for the bun some
other way and are left as they were: TestHostRunsAsMagpieBun (7ddea2c2
covers its copy), the plugin_git test's realBun and netproxy's
TestBunThroughBridge, none of which runs the plugin host under a
deadline on the PATH bun. migrate_test.go needed two `err` declarations
that the removed block used to provide.

What was run (macOS arm64, bun 1.4.2), with a wrapper bun first on PATH
whose first exec sleeps 61s (marker-gated, then execs the real bun):
- Red, old code:
  - internal/provider TestPluginSignInTakesAPastedCallback: FAIL at
    61.1s, "pluginsignin_paste_test.go:36: starting plugins: context
    deadline exceeded".
  - internal/plugin TestFakePlugin: FAIL at 62.0s, "plugin_test.go:66:
    Plugins = [], starting plugins: context deadline exceeded".
  - internal/gateway TestPluginFailover passed on the old code too (its
    calls have no deadline that short), so the gateway is not shown red.
- Green, this change, same cold wrapper: the three tests passed (61.1s,
  62.9s, 61.2s).
- go test -tags nogui -count=1 for the root package and for
  internal/testenv, gateway, gui, plugin and provider, under a temp HOME
  with GOMODCACHE, GOCACHE and GOPATH pinned: all ok.
- gofmt -l on the changed files: clean. go vet of those packages for
  darwin (-tags nogui), GOOS=linux and GOOS=windows (-tags nogui), and
  GOOS=windows without the tag: clean.

Not run: a really cold bun on a loaded machine; the sleep stands in for
the check. Not run on Linux or Windows, where the warm-up is one extra
`bun --version` per test binary.
2026-10-10 10:22:59 +08:00
forever 470d1e71a5 provider: a plugin account's usage reading begun before a request was answered no longer overrules the answer, so a 401's lapse mark isn't taken off by a clean reading already out (TestPluginFailover, about 1 run in 90) (waitingsnow) (#1176)
TestPluginFailover failed about 1 run in 90 at "a refused isn't marked".
With notePluginLapse logged, a failing run read: a background reading of
a's allowance (loginReading, after the "stream" round's StaleAllowance)
began at 31.985; the upstream answered a's request 401 at 32.001 and
notePluginSaid marked a lapsed at 32.003; the reading ended clean at
32.011 and notePluginLapse(200) took the mark off; the test read the
logins at 32.042. Users meet the same: a reading of the Usage page or of
routing's allowances that is out when a request is refused clears the
mark the refusal left, and routing goes on trying the refused account;
one that ends refused marks an account a request has just gone through
on.

What plugin accounts' sign-ins are told of is now counted under
loginsMu (pluginSignInTold, pluginSignInSaid). A request's answer, a
sign-in and the plugin's own signIn events are told now and apply as
before. pluginLoginQuota takes the count before plugin.AccountUsage, and
what its reading found applies only when nothing was told of that
account since (notePluginLapseSince), as Allowances leaves out an
account renewed while it read. A count rather than a time: Windows'
clock moves in ticks of up to 15.6ms, and a reading begun just after an
answer would read as begun with it.

TestPluginUsageReadOlderThanAnswer holds the fake plugin's usage on an
httptest server ($FAKE_USAGE, whose 401 the fake now reads as an expired
sign-in) while a request's answer is noted, both ways, and checks that
readings begun afterwards still mark and clear. Each direction fails
without the fix. TestPluginFailover now reads the allowances the
"stream" round left stale before the "gone" round: the fake's usage
doesn't ask the upstream that refuses, so a reading that asks after the
401 finds the sign-in good and rightly takes the mark off. A review
reproduced that with a delay injected before the count is taken.

Left as it is: a reading whose own renewal of the token tells "renewed"
before it asks skips the "expired" it then finds; the next 401 marks the
account. Not tried with a real plugin account.

Under a temp HOME with Go's caches pinned and bun 1.4.2: gofmt, go vet
-tags nogui ./..., builds for darwin, GOOS=linux -tags nogui and
GOOS=windows (and windows vet of provider and gateway) pass; go test
-tags nogui ./... passes; the changed provider tests and
TestPluginSaysSignIn/TestPluginFailover pass with -race -count=20; the
report's `go test -tags nogui -race -cpu 1,2,4 -count=30 -run
'^TestPluginFailover$' ./internal/gateway/` passed 3 rounds (270 runs),
where it failed 3 of about 54 runs before the fix on the same loaded
machine.
2026-10-08 06:48:41 +08:00
yetone 1fa21850a3 plugins: a plugin can say its sign-in renewed, and what a usage read means for it (要保证插件跟内置的所有逻辑都要一模一样) — a built-in whose sign-in renewed took the lapse mark off whatever the request then met, and a plugin could only clear it with a success: X-Magpie-Sign-In: renewed now clears it on any status. A usage read marked a plugin account whenever its error said to sign in again and cleared it on any clean read, where Qoder's, Kiro's and others' built-ins marked none from usage: auth.usage may now say signIn: expired, kept or renewed, taken as the header is; one that doesn't say keeps the old reading. TestPluginSaysSignIn and TestPluginUsageLapse fail without the change. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./internal/gateway ./internal/provider ./internal/plugin pass. 2026-10-01 04:46:24 +08:00
yetone c9d2015e5d plugins: a plugin says apart what its answer means for the sign-in, so it can fail with its built-in's status (当然要做,要保证插件跟内置的所有逻辑都要一模一样) — magpie took a plugin account for lapsed on a 401 alone, so a plugin had to choose between the built-in's status and its lapse mark: Factory's and MiMo's refused renewals were 401s where the built-in answered 502 and marked the account, and Qoder's 403, Zed's retried 401, Kiro's unreachable refresh and Cursor's expired plan answered other than the built-in so as not to mark one it didn't. An answer's X-Magpie-Sign-In now says: expired marks the account lapsed whatever the status, kept leaves it be; without it a 401 marks it and a success clears it, as before. The agent never sees the header. TestPluginSaysSignIn fails without the change. 2026-10-01 04:36:42 +08:00
yetone 4e63e98359 gateway: a plugin's provider with several accounts fails over as a built-in's subscription does (别忘了同时添加多个账号、设置路由组、自动故障转移、额度不够时的转移、显示额度) — a test of it end to end, through the plugin host and a fake vendor: three accounts signed in in magpie, all on; one answering a quota 429, a quota error in the stream before any content, or a 401 hands the request to the next and rests, the 401 marking it lapsed; one whose five hours are used up for a model, as the plugin's usage says, is weighed last for that model even while in use, and first for a model its window doesn't count; a routing group of the plugin's model and another provider's answers from the other when every account of the plugin is out. It fails with the account's allowance asked of any agent but the plugin's provider's. 2026-10-01 03:54:19 +08:00