Files
magpie/internal/provider/plugin_keepalive.go
T
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

210 lines
7.0 KiB
Go

package provider
// Keeping the plugins' sign-ins alive while nothing uses them (#5080). A
// plugin that renews its sign-ins through magpie (auth.refresh) had each
// renewed only when something used the account: a request, its models,
// its usage or its check-in, within the plugin's refreshLead of expiry.
// 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 (orinsid, plugins#675).
//
// So the magpie serving the gateway renews each such sign-in as it comes
// due, on a timer, through the host's own renewal (plugin.KeepSignedIn):
// the one a request would make, once at a time per account whoever asks
// (a request arriving meanwhile waits for the same one) and under the
// lock file every magpie process renews under. Only the accounts switched
// on are kept, of a plugin provider not switched off, not hidden and not
// being moved; one the vendor turned away (Lapsed) waits for the user to
// sign in again. A plugin that would rather not (a renewal that costs the
// account something, or a refresh token another client holds too) says
// auth.keepAlive: false.
//
// Nothing is asked until an account is due: a look reads plugin-auth.json
// (parsed once per change, filememo) and sleeps until the next account
// comes due, no longer than keepAliveLookMost, so an idle magpie with no
// account due starts no plugin host. A renewal that fails is tried again
// later and later (keepAliveBackoff), for that sign-in only, and never
// marks the account: a renewal that couldn't reach its vendor says
// nothing about the sign-in. Only the plugin saying the vendor turned it
// away for good marks it, as a request's renewal does.
import (
"context"
"errors"
"log"
"time"
"github.com/yetone/magpie/internal/plugin"
)
var (
// keepAliveLookFirst is how long after the gateway starts the first
// look is made, once its start has listed the plugins
keepAliveLookFirst = 30 * time.Second
// keepAliveLookMost is the longest a look waits for the next: a
// sign-in made meanwhile is seen by then (the shortest refreshLead a
// plugin says is a minute, and its token is due a lead early)
keepAliveLookMost = 5 * time.Minute
// keepAliveLookLeast is the shortest: a token due already but held
// back (keepAliveBackoff) isn't looked at in a busy loop
keepAliveLookLeast = 15 * time.Second
// keepAliveRenewWait bounds one renewal: the host's own lock is held
// at most a minute (RENEW_LOCK_STALE) before another takes it over
keepAliveRenewWait = 2 * time.Minute
)
// keepAliveBackoff is how long a sign-in whose renewal failed n times in
// a row waits before it is tried again: a minute, doubled each time, at
// most half an hour. An outage of the vendor's isn't asked every look,
// and its accounts are renewed soon after it ends.
func keepAliveBackoff(n int) time.Duration {
d := time.Minute
for i := 1; i < n && d < 30*time.Minute; i++ {
d *= 2
}
return min(d, 30*time.Minute)
}
// pluginSignIn is a plugin account kept alive: its provider, its key in
// plugin-auth.json, the account as magpie lists it (for its proxy) and
// when it comes due.
type pluginSignIn struct {
ID, Key, User string
At time.Time
}
// pluginSignInsKept are the plugins' sign-ins kept alive in the
// background, each with when it is next due: of the plugins that renew
// through magpie and keep alive (plugin.Provider.Renews), the accounts
// switched on and not refused, of a provider neither switched off, hidden
// nor being moved. Tests replace it.
var pluginSignInsKept = func() []pluginSignIn {
var out []pluginSignIn
for _, pp := range heldPlugins() {
if !pp.Renews || movingNow(pp.ID) || leftAlone(PluginID(pp.ID)) {
continue
}
for _, l := range pluginLogins(pp) {
if !l.On || l.Lapsed != "" {
continue
}
if at, ok := plugin.RenewAt(pp, l.acct.Key); ok {
out = append(out, pluginSignIn{ID: pp.ID, Key: l.acct.Key, User: l.User, At: at})
}
}
}
return out
}
// keepPluginSignedIn renews one sign-in if it is due; tests replace it.
var keepPluginSignedIn = func(ctx context.Context, s pluginSignIn) (plugin.KeptSignedIn, error) {
if !plugin.Running() && !plugin.HasBun() {
return plugin.KeptSignedIn{}, errNoBun
}
return plugin.KeepSignedIn(ViaLogin(ctx, PluginID(s.ID), s.User), s.ID, s.Key)
}
var errNoBun = errors.New("no Bun to run the plugins on")
// signInKeeper keeps the plugins' sign-ins alive, a look at a time, from
// one goroutine.
type signInKeeper struct {
// held is each sign-in held back, by its key and the time it was due
// (a sign-in renewed or made again since has another due time, and
// starts afresh): its renewal failed n times in a row, or, n 0, the
// host found it not due yet
held map[signInDue]keptFailure
}
type signInDue struct {
key string
at int64
}
type keptFailure struct {
n int
until time.Time
}
// look renews each sign-in due at now, one after another, but those held
// back after a failure, and says how long until the next look.
func (k *signInKeeper) look(ctx context.Context, now time.Time) time.Duration {
next := now.Add(keepAliveLookMost)
soon := func(t time.Time) {
if t.Before(next) {
next = t
}
}
due := map[signInDue]bool{}
for _, s := range pluginSignInsKept() {
if ctx.Err() != nil {
break
}
d := signInDue{s.Key, s.At.UnixMilli()}
due[d] = true
if s.At.After(now) {
soon(s.At)
continue
}
if f, ok := k.held[d]; ok && now.Before(f.until) {
soon(f.until)
continue
}
c, cancel := context.WithTimeout(ctx, keepAliveRenewWait)
r, err := keepPluginSignedIn(c, s)
cancel()
if err == nil && r.Err != "" {
err = errors.New(r.Err)
}
hold := func(f keptFailure, d2 time.Duration) {
f.until = now.Add(d2)
if k.held == nil {
k.held = map[signInDue]keptFailure{}
}
k.held[d] = f
soon(f.until)
}
switch {
case err != nil && ctx.Err() != nil:
// magpie is stopping: nothing is known of the sign-in
case err != nil || r.Due && !r.Renewed:
f := k.held[d]
f.n++
hold(f, keepAliveBackoff(f.n))
if err != nil && !r.Waiting {
log.Printf("%s: renewing a sign-in nothing used: %v (tried again in %s)", s.ID, err, keepAliveBackoff(f.n))
}
case !r.Due:
// the host reckons it otherwise (a plugin updated to another
// lead, not listed again yet): not asked again every look
hold(keptFailure{}, keepAliveLookMost)
default:
delete(k.held, d)
}
}
// a sign-in renewed, made again or gone is forgotten
for d := range k.held {
if !due[d] {
delete(k.held, d)
}
}
return min(max(next.Sub(now), keepAliveLookLeast), keepAliveLookMost)
}
// KeepPluginsSignedIn renews the plugins' sign-ins as they come due, in
// the magpie serving the gateway, while nothing uses them (#5080).
func KeepPluginsSignedIn(ctx context.Context) {
var k signInKeeper
t := time.NewTimer(keepAliveLookFirst)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case <-t.C:
}
t.Reset(k.look(ctx, time.Now()))
}
}