diff --git a/docs/subsystems/provider-plugins.md b/docs/subsystems/provider-plugins.md index fafd6d7f2..f18f675f2 100644 --- a/docs/subsystems/provider-plugins.md +++ b/docs/subsystems/provider-plugins.md @@ -132,7 +132,7 @@ The editor's "Move to plugin" action calls `provider.Adopt`. With accounts, adop Installing the plugin independently can also hand over ownership through `provider.HandOver`. The package must be enabled, meet the mover's minimum version, and serve the expected provider. After add, update, or upgrade, the GUI calls `HandOver(ctx, false)`, which can adopt a built-in with no accounts. Existing built-in accounts wait for `HandOver(ctx, true)` in gateway maintenance. -`KeepRetiringMoved` first runs after 20 seconds, then hourly. Each cycle maintains moved plugin versions, calls `MoveRetiring`, then performs automatic handover. `MoveRetiring` moves every subscription with a mover (`retiring`, which is `MovableIDs`) that has built-in accounts, waiting six hours after a failed attempt. The built-ins are being retired (the owner, 10-16: 陆续把内置Provider下线掉), so a `back` record is moved again too; a `removed` one is left until the user installs the plugin. Handover turns a `removed` record whose plugin is installed and current back into `plugin`, preserves a plugin already signed in under its separate `-plugin` id and skips existing migration records except a `failed` record old enough to retry. +`KeepRetiringMoved` first runs after 20 seconds, then hourly, or sooner when a retired built-in's failed move is due (`nextRetiringRun`). Each cycle maintains moved plugin versions, calls `MoveRetiring`, then performs automatic handover. `MoveRetiring` moves every subscription with a mover (`retiring`, which is `MovableIDs`) that has built-in accounts. A failed attempt waits six hours (`moveRetry`), or ten minutes when the built-in is retired (`retiredRetry`, `retryWait`), since nothing serves those accounts until they move: a move that failed because npm was out of reach or the plugin wasn't installed yet is tried again soon. The built-ins are being retired (the owner, 10-16: 陆续把内置Provider下线掉), so a `back` record is moved again too; a `removed` one is left until the user installs the plugin. Handover turns a `removed` record whose plugin is installed and current back into `plugin`, preserves a plugin already signed in under its separate `-plugin` id and skips existing migration records except a `failed` record old enough to retry. There is no move back: `provider/moveback` and `magpie plugins move-back` answer that the built-ins are retired, and removing or turning off the plugin doesn't move anything back either. Turned off, the plugin keeps the subscription `plugin`, off with it. Removed (`RemovePlugin`), the subscription is recorded `removed`, its accounts kept in the plugin's auth store, since `plugin.Remove` leaves sign-ins until the user signs out. The Plugins page and `magpie plugins rm`/`off` say the subscription stops working and its accounts are kept. Rolling back a failed move (`putBack`, through each mover's `back` and `settle`) is the only path that writes to the built-in's logins. diff --git a/internal/provider/migrate.go b/internal/provider/migrate.go index 1f79e2920..7634f79bf 100644 --- a/internal/provider/migrate.go +++ b/internal/provider/migrate.go @@ -67,8 +67,26 @@ const ( // 10-16: 陆续把内置Provider下线掉). A tests' list stands in for it. var retiring = MovableIDs -// moveRetry is how long a failed move waits before it is tried again. -const moveRetry = 6 * time.Hour +// moveRetry is how long a failed move waits before it is tried again, +// while the built-in carries on. One whose built-in is retired waits +// retiredRetry: nothing serves its accounts until it moves, so a move that +// failed for a moment (npm out of reach, the plugin not installed yet) is +// tried again soon, not hours later. +const ( + moveRetry = 6 * time.Hour + retiredRetry = 10 * time.Minute +) + +// retryWait is how long a failed move of id waits before it is tried again. +func retryWait(id string) time.Duration { + if BuiltinRetired(id) { + return retiredRetry + } + return moveRetry +} + +// retryDue says whether a failed move of id, last tried at, is due again. +func retryDue(id string, at time.Time) bool { return time.Since(at) >= retryWait(id) } // Migration is where one built-in's move stands. type Migration struct { @@ -949,7 +967,7 @@ func handBack(ctx context.Context, id string, mv *mover, accts []movedAccount, k } // MoveRetiring moves the built-ins being retired that have accounts, at -// start-up: once, or again moveRetry after one failed. One an older magpie +// start-up: once, or again retryWait after one failed. One an older magpie // moved back is moved again: there is no way back, as the built-in is // going. One whose plugin the user removed waits for them to install it. func MoveRetiring(ctx context.Context) map[string]error { @@ -965,7 +983,7 @@ func MoveRetiring(ctx context.Context) map[string]error { continue case ok && m.State == MoveRemoved: continue - case ok && m.State == MoveFailed && time.Since(m.At) < moveRetry: + case ok && m.State == MoveFailed && !retryDue(id, m.At): continue } if accts, err := movers[id].out(); err != nil || len(accts) == 0 { @@ -988,7 +1006,7 @@ func MoveRetiring(ctx context.Context) map[string]error { // in use as -plugin's and the id changing would leave agents asking // for a provider gone; the add sheet lists it once regardless. One moved // whose plugin the user removed is the plugin's again once they install it, -// its accounts still with the plugin, and a failed move waits moveRetry. No +// its accounts still with the plugin, and a failed move waits retryWait. No // plugin is installed for it: the user's own install is what says to. func HandOver(ctx context.Context, accounts bool) map[string]error { if settings.AccountDiscoveryOff() { @@ -1008,7 +1026,7 @@ func HandOver(ctx context.Context, accounts bool) map[string]error { } continue } - if ok && (m.State != MoveFailed || time.Since(m.At) < moveRetry) { + if ok && (m.State != MoveFailed || !retryDue(id, m.At)) { continue } if !pluginReady(mv) { @@ -1067,8 +1085,9 @@ func keepMovedCurrent(ctx context.Context) { // KeepRetiringMoved moves the built-ins being retired, and hands over // those whose plugin the user installed (HandOver), run by the magpie // serving the gateway (one magpie, never two at once): a little after it -// starts, then every hour, so a failed move is tried again once moveRetry -// has gone. It also keeps each moved built-in's plugin up to date. +// starts, then every hour, so a failed move is tried again once retryWait +// has gone; a retired built-in's failed move brings the next run forward +// to when it is due. It also keeps each moved built-in's plugin up to date. func KeepRetiringMoved(ctx context.Context) { t := time.NewTimer(20 * time.Second) defer t.Stop() @@ -1092,10 +1111,23 @@ func KeepRetiringMoved(ctx context.Context) { log.Printf("moved %s to its plugin, %s", id, MovePackage(id)) } } - t.Reset(time.Hour) + t.Reset(nextRetiringRun()) } } +// nextRetiringRun is how long KeepRetiringMoved waits before it runs +// again: an hour, or less when a retired built-in's failed move is due +// sooner. +func nextRetiringRun() time.Duration { + next := time.Hour + for _, id := range MovableIDs() { + if m, ok := MigrationOf(id); ok && m.State == MoveFailed && BuiltinRetired(id) { + next = min(next, max(time.Until(m.At.Add(retiredRetry)), time.Minute)) + } + } + return next +} + // MovedOnto are the built-ins moved onto the plugin named (its spec or // package name). func MovedOnto(name string) []string { diff --git a/internal/provider/migrate_test.go b/internal/provider/migrate_test.go index 1c2fd69d9..fb811646e 100644 --- a/internal/provider/migrate_test.go +++ b/internal/provider/migrate_test.go @@ -211,7 +211,8 @@ func TestMoveToPlugin(t *testing.T) { } // A move cut short (magpie quit mid-way) is put back before it is tried -// again, and a failed one waits before magpie tries it by itself again. +// again, and a failed one waits before magpie tries it by itself again: +// hours while the built-in serves its accounts, minutes once it is retired. func TestMoveCutShort(t *testing.T) { bun := testenv.Bun(t) claudeHome(t) @@ -265,9 +266,29 @@ func TestMoveCutShort(t *testing.T) { if errs := MoveRetiring(ctx); len(errs) != 0 || Moved("fakeco") { t.Fatalf("tried again at once: %v", errs) } - _ = setMigration("fakeco", func(m *Migration) { m.At = time.Now().Add(-moveRetry - time.Minute) }) + // past retiredRetry, still within moveRetry: the built-in serves its + // accounts yet, so it waits the hours + _ = setMigration("fakeco", func(m *Migration) { m.At = time.Now().Add(-retiredRetry - time.Minute) }) + if errs := MoveRetiring(ctx); len(errs) != 0 || Moved("fakeco") { + t.Fatalf("a built-in still serving it tried again within moveRetry: %v", errs) + } + if d := nextRetiringRun(); d != time.Hour { + t.Fatalf("the next run with no retired built-in failed: %s, not an hour", d) + } + // retired, nothing serves its accounts: tried again after retiredRetry, + // the run brought forward to it + retiredBuiltins["fakeco"] = true + t.Cleanup(func() { delete(retiredBuiltins, "fakeco") }) + _ = setMigration("fakeco", func(m *Migration) { m.At = time.Now() }) + if d := nextRetiringRun(); d > retiredRetry || d < retiredRetry-time.Minute { + t.Fatalf("the next run after a retired built-in's failed move: %s, not %s", d, retiredRetry) + } + if errs := MoveRetiring(ctx); len(errs) != 0 || Moved("fakeco") { + t.Fatalf("a retired built-in tried again at once: %v", errs) + } + _ = setMigration("fakeco", func(m *Migration) { m.At = time.Now().Add(-retiredRetry - time.Minute) }) if errs := MoveRetiring(ctx); errs["fakeco"] != nil || !Moved("fakeco") { - t.Fatalf("after moveRetry: %v", errs) + t.Fatalf("a retired built-in after retiredRetry: %v", errs) } // a magpie that stopped between the record and logins.json: tidied loginsMu.Lock()