Files
magpie/internal/plugin/testdata/renew-nokeep
yetone 4c0c6c02b8 plugin: keep idle plugin sign-ins alive in the background (#5080)
A plugin with auth.refresh had a sign-in renewed only when something
used the account (a request, its models, usage or check-in) within its
refreshLead of expiry. An account left idle past its token's life
lapsed. OrcaTerm's WeChat token lives two hours, and after that its
refresh answers SESSION_EXPIRED for good, so the user had to sign in
again (orinsid, magpie-community/plugins#675).

The gateway now renews each such sign-in as it comes due
(provider.KeepPluginsSignedIn, started beside KeepPluginsListed):

- Due times come from plugin-auth.json through filememo
  (plugin.RenewAt: expires less the lead the host reckons, now listed
  as Provider.Renews/RenewLead). Nothing is asked, and no host started,
  until an account is due. The loop sleeps until the next one, at most
  5 minutes, and renews them one after another.
- A due sign-in goes through the host's own renewal (host.js
  keepSignedIn, which shares renewal() with fresh()). It is one at a
  time per account whoever asks: a request arriving meanwhile waits
  for the same promise, under the cross-process lock file, through the
  account's proxy.
- Kept: accounts switched on and not marked, of a plugin provider not
  switched off, hidden (leftAlone) or being moved.
- A failed renewal marks nothing. It is held back for 1m, doubling to
  at most 30m, for that sign-in only. Only the plugin's
  signIn:"expired" marks it, as at a request.
- A plugin can opt out with auth.keepAlive: false. None in the plugins
  repo needs to: no refresh costs quota, the ones rotating single-use
  refresh tokens (Cline, Factory, Nous, SenseNova, CodeArts, Qoder,
  MiniMax) rotate at use already, and kimi-work's reference accounts
  and Raccoon's desktop reuse never refresh another app's token.

Docs: provider-plugins.md "A sign-in nothing uses"; leftAlone list in
providers-accounts.md.

Run at the pushed head, on mote (mote-run), -tags nogui:
- go test -count=1 -timeout 45m ./internal/plugin ./internal/provider
  ./internal/gateway: ok (109s, 119s, 637s), rebased on origin/main
  6cfe78635. Rebased again on b088fd0f0 (gateway.go, prompt.go
  changed): go build ./..., go vet of the three packages and the new
  tests plus TestLivePrompt*: ok.
- the new tests on Windows (win-run) and Linux (wsl-run): ok.
- GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...,
  go vet ./...: ok. gofmt -l: clean.

Broken by hand, each red, then restored:
- keepSignedIn refreshing by itself, not through renewal()/the lock:
  TestKeepSignedInRenewsIdleSignIns red (r-ok-busy renewed twice
  beside concurrent requests).
- keepAlive:false ignored: red (stayco listed as renewed).
- a network failure reported as gone: red (down: Gone true).
- Go backoff removed: TestKeeperBacksOffAFailedRenewal red.
- the On, Lapsed and leftAlone filters each dropped:
  TestKeptSignInsAreTheOnesSwitchedOn red.
The new tests don't compile on the old code (no KeepSignedIn).

Not tried: a real OrcaTerm account idling past its 2h token with the
gateway running; Windows/Linux runs of the new tests (vet only); a
magpie with many plugin accounts beyond the unit test's look.
2026-11-03 21:01:27 +08:00
..