docs: the host's own renewal backoff and held refused lists (JZGiVvkSOA75376 on X)

Run for this stack (6e2dcc4a1, 16dcfa504, this) at this tree, on mote:
go vet (darwin), GOOS=linux -tags nogui and GOOS=windows go vet of
internal/plugin and internal/provider: ok; go test -count=1 of
internal/plugin (127s) and internal/provider (150s): ok. gofmt -l clean.
Sandbox repro (HOME in the worktree, magpie-ima-auth 0.3.0 and
opencode-zed-auth installed, two dead accounts each, `magpie serve` for
150s) on Windows (xiaomi-gpu) through win-run: ima "token renewal
failed" 10 lines on origin/main 1b078d404, 6 with this stack; both
counts include a host restart for a Bun update, which starts the
backoff again. On mac (mote) with `magpie web` and a page switching
Providers/Usage: 12 on origin/main.
Not reproduced: the user's ~80 tries a minute and ~5 MB/s. Neither the
headless gateway nor the web GUI here asked that often, and each try
here moved KBs (ima's refresh, Zed's 401, npm documents of 13-43 KB),
so no per-try download cap was added. The Windows tray app's own polling
was not tried.
Not tried: real ima or Zed accounts; the GUI suite (no GUI file changed).
This commit is contained in:
yetone
2026-11-10 09:45:03 +08:00
parent 16dcfa504a
commit 9430c53093
+2
View File
@@ -207,6 +207,8 @@ An OAuth account keeps a short-lived access token beside its refresh token, and
A plugin's `auth.refresh` renews a sign-in only when something uses the account (a request, its models, its usage, its check-in) within its `refreshLead` of `expires`. An account left idle past its token's life lapsed, and for a vendor whose session dies with its token the user had to sign in again: OrcaTerm's WeChat token lives two hours, and its refresh after that answers `SESSION_EXPIRED` for good (#5080, orinsid, [magpie-community/plugins#675](https://github.com/magpie-community/plugins/issues/675)). So the magpie serving the gateway renews each such sign-in as it comes due (`KeepPluginsSignedIn` in [`plugin_keepalive.go`](../../internal/provider/plugin_keepalive.go), started beside `KeepPluginsListed`). It reads the due times from plugin-auth.json (`plugin.RenewAt`: `expires` less the lead the host reckons, `Provider.RenewLead`, listed with `Renews`), asks nothing and starts no host until one is due, and sleeps until the next, at most five minutes. A due sign-in is renewed through the host's own renewal (`keepSignedIn` → `renewal` in [`host.js`](../../internal/plugin/host.js)), the one a request makes: one at a time per account whoever asks, a request arriving meanwhile waiting for the same one, under the lock file every magpie process renews under, through the account's proxy. Only accounts switched on and not marked, of a plugin provider not switched off, hidden (`leftAlone`) or being moved, are kept. A renewal that fails marks nothing: it is held back a minute, doubled each time to half an hour, for that sign-in only; only the plugin's `signIn: "expired"` marks the account, as at a request. A plugin whose renewal costs the account something, or whose refresh token another client holds too, says `auth.keepAlive: false` and is renewed only when used. No plugin in magpie-community/plugins says so as of this change: their refreshes cost no quota, those that rotate a refresh token (Cline, Factory, Nous, SenseNova, CodeArts, Qoder, MiniMax) already rotate it at use, and the ones reading another app's sign-in (kimi-work's reference accounts, Raccoon's desktop) never refresh it. `TestKeepSignedInRenewsIdleSignIns` in [`plugin_keepalive_test.go`](../../internal/plugin/plugin_keepalive_test.go) runs the renewal fixture in a real host; `TestKeeperAsksOnlyWhatIsDue`, `TestKeeperBacksOffAFailedRenewal`, `TestKeeperHoldsWhatTheHostFindsNotDue` and `TestKeptSignInsAreTheOnesSwitchedOn` in [`plugin_keepalive_test.go`](../../internal/provider/plugin_keepalive_test.go) cover the loop and which accounts it keeps.
Inside the host, a sign-in whose renewal failed isn't renewed again by whatever uses it next: `renewal` in [`host.js`](../../internal/plugin/host.js) holds it 30 seconds (`RENEW_RETRY`), doubled with each failure in a row of that sign-in up to 30 minutes (`RENEW_RETRY_MOST`, `retryAfter`, `failedRenewal`); one the plugin said `signIn: "expired"` of isn't tried again at all, and a new sign-in starts afresh. A refresh that gives nothing back while the sign-in is due counts as a failure: magpie-ima-auth 0.3.0 (third-party) catches ima's "sign in again", warns and returns undefined, and the host asked it again on every request, models listing and usage read, ~80 times a minute for two dead accounts (JZGiVvkSOA75376 on X). A models hook that throws with `signIn: "expired"` is held the same way for that sign-in (`refusedLists`, keyed by its access, refresh and key): the listing falls back with the same error and the hook isn't asked until the backoff has passed, which stopped two dead Zed sign-ins being refused by Zed ~30 times a minute. `TestPluginRenewsSignIns` (its `r-none` account), `TestRenewRetryBacksOff` and `TestRefusedListIsNotAskedAgain` in [`internal/plugin`](../../internal/plugin) cover them.
An account signed out while one of its hooks runs stays signed out. A hook that saves a sign-in through `client.auth.set` (a models list spending a rotating refresh token, as the fake plugin's `rot-` sign-ins do) saves nothing once the account it was called for is gone from plugin-auth.json: `inScope` in [`host.js`](../../internal/plugin/host.js) notes whether the account was signed in when the call began (`had`), and `auth.set` checks it under the file's lock, as `renewLocked` already did for a renewal. A sign-in's own steps (`signingIn`) are not held to it, since their account is new. Before this, a listing that started before a sign-out, or before a failed move took its accounts back out of the plugin (`putBack`), wrote the account in again, and the plugin kept an account of a move that failed (#5126, #5119). `TestSignOutOutlastsRunningHook` in [`signout_hook_test.go`](../../internal/plugin/signout_hook_test.go) holds the hook (`FAKE_MODELS_HOLD`) across the sign-out in a real host.
## A model's free and rate