From de671719a0ba04de83103af45298c391d599ceb6 Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 17 Oct 2026 14:21:36 +0800 Subject: [PATCH] provider: a retired built-in's failed move is tried again in 10 minutes, not 6 hours A failed move waited moveRetry (6h) before KeepRetiringMoved tried it again. That was fine while the built-in carried on serving the accounts. A retired built-in serves nothing, so a move that failed for a moment (npm out of reach, or the plugin listed in plugins.json but not installed yet, which the real-account run on 85b8f6d4d hit with Command Code: the first Move failed, the second worked) left the user's subscription unserved for up to 6 hours unless they pressed Move. - retryWait(id) is retiredRetry (10 minutes) for a retired built-in, and moveRetry otherwise. MoveRetiring and HandOver both use it (retryDue). - KeepRetiringMoved's next run is brought forward to when a retired built-in's failed move is due (nextRetiringRun, at least a minute), instead of always an hour. - docs/subsystems/provider-plugins.md says so. TestMoveCutShort now also checks: - a failed move 11 minutes old waits while the built-in serves it, and the next run is an hour; - once retired, the next run is 10 minutes, it isn't tried at once, and it is moved after retiredRetry. Broken by hand, each red: - retryWait returning moveRetry for a retired built-in: "a retired built-in after retiredRetry: map[]" - nextRetiringRun always an hour: "the next run after a retired built-in's failed move: 1h0m0s, not 10m0s" Run: go test -tags nogui ./internal/provider -run TestMoveCutShort: ok. What was run for the stack, at load ~300 (it was rebased from bad4508c4 onto 1acd79cdf; the only conflicts were in docs/subsystems/gateway-routing.md, resolved to main's text with each commit's own edit, checked word by word): - At this commit's tree (bda209c1f, on 1e643ba7e; then rebased onto 1acd79cdf, where vet on all three systems and internal/gui ran clean): gofmt -l clean; go vet darwin/linux/windows clean; go test ./internal/provider ./internal/cli ./plugins/search ok; ./internal/gateway -run 'Jev|Auto|Review|Plugin|Retire|Move|Search' ok. The full suite wasn't run again on 1e643ba7e's four new commits (a provider display name, two Jev changes, auto-mode review), none in a file the stack touches but the gateway-routing doc; CI runs it before the tag. - Full suite at 3f0858b6f (on c4aeaec74): gofmt -l clean, go vet darwin/linux/windows clean, go test -tags nogui ./... -count=1 -timeout 90m: 64 ok, exit 0. - Full suite at a42a1090c (on 89521b03d) and ee288bc65 (on 717b69f78): the same, 64 ok, exit 0. Factory's own min commit (0.1.32) was dropped in the last rebase: main raised it to 0.1.33 in b5c76935b. - Full suite at cbf4626a3 (on bad4508c4): gofmt -l clean, go vet darwin/linux/windows clean, go test -tags nogui ./... -count=1 -timeout 90m: 64 ok, exit 0. At 4c6c99bbd the same run had gateway red on TestPluginStreamLargeReplies only (#2551; passes alone, 181.957s). - GUI: retired-move 4/0 in both engines. The remaining 519 files ran at 85b8f6d4d, two at a time (3864 ok, 109 not ok when this was written; 18 of the reds are browser launch timeouts). Each red file was run alone at the head, and those still red were run on origin/main bad4508c4: - red the same on main, filed: segs-glide 20/20 (#2683), routing-bodies (#2559, #2658), routing-model-rest (#2115), routing-keys-fold (#1612), routing-wb-restricted (#1905), provider-advanced-fold (#2552), sidebar (#2684), s3-sync (#2448), routing-request-metrics (#2571), rest-try-now (#2564), quota-name-translated and quota-count-wrap (#2228), lightweight (#2638), codex-predictions (#2624), gateway-key-models under load (#2648); - green alone: plugin-list-error, sessions-paging, plugin-kinds-discover, plugin-kernel, session-terminal, routing-time-went, routing-flood, routing-context-fold, routing-agent-gone, plugin-local, plugin-key-hint, name-suffix-no-names, nav-sub-unroll, model-api-gemini, model-api-preset. At ee288bc65: retired-move 4/0, routing-compaction 10/0, library-ime-scroll 18/0; title-bar-tools 4/6, red the same on main 2042123f4 (#2740: a reload cuts off sideGwRead's read). The stack's only GUI change is the unmoved line in renderMovable, shown only when /api/providers has unmoved entries. - Real accounts through the plugins (85b8f6d4d, sandbox HOME): grok, workbuddy, workbuddy-ai, cursor, zcode 200; devin, commandcode-plan, mimo-app 200 after a first timeout; qoder 401 shown as lapsed (plugins#181, the built-in had no 10605 handling either). Not tried: kiro, factory, zed (no account here). - The built-in-only WorkBuddy model ids: the plugin reads WorkBuddy's live list, which doesn't offer them. --- docs/subsystems/provider-plugins.md | 2 +- internal/provider/migrate.go | 50 +++++++++++++++++++++++------ internal/provider/migrate_test.go | 27 ++++++++++++++-- 3 files changed, 66 insertions(+), 13 deletions(-) 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()