diff --git a/internal/plugin/host.js b/internal/plugin/host.js index bb91ba36c..8b7dec31f 100644 --- a/internal/plugin/host.js +++ b/internal/plugin/host.js @@ -2147,6 +2147,17 @@ function fromModelsDev(p, m) { // list is told as fallen back, which keeps the one magpie had. The // listing's caller may give up (signal), which a hook is told through // its own options' signal too (#4678). +// refusedLists is each account whose models hook said its vendor refused +// the sign-in (signIn "expired"): {sign, at, n, error}. Its list isn't +// asked again for that sign-in until retryAfter(n) has passed, as a +// renewal that failed isn't: two dead Zed sign-ins were listed, and +// refused by Zed, ~30 times a minute (JZGiVvkSOA75376 on X). Signing in +// again (another sign-in) asks at once. +const refusedLists = new Map() + +// signInOf is what tells one sign-in of an account from the next. +const signInOf = (a) => JSON.stringify([a?.access ?? "", a?.refresh ?? "", a?.key ?? ""]) + async function info(id, key, strict, limit, quiet, signal) { const md = mdev()[id] const cfg = providerConfig(id) @@ -2215,6 +2226,16 @@ async function info(id, key, strict, limit, quiet, signal) { const began = Date.now() await fresh(id, k) const all = readAuth() + const held = refusedLists.get(k) + if (held && all[k] && held.sign === signInOf(all[k]) && Date.now() - held.at < retryAfter(held.n)) { + // refused before, and nothing changed since: the vendor isn't asked + // again, and the account stays marked as it was + const r = reach.getStore() + if (r) r.refused ??= held.error + out.fellBack = true + out.listError = held.error + continue + } // the hook's own signal: its listWait deadline aborts it, and so does // the listing's caller going away, whichever comes first (#4678) const ctl = new AbortController() @@ -2235,6 +2256,7 @@ async function info(id, key, strict, limit, quiet, signal) { // show the plugin's short defaults as if they were the account's if (out.fellBack) out.listError = l.said || "the plugin couldn't get its vendor's list" out.models = Object.fromEntries(Object.entries(next ?? {}).map(([k, m]) => [k, { ...m, id: k, providerID: id }])) + refusedLists.delete(k) } catch (e) { // a listing that gave up waiting: what the hook gives later is no // list a request should be built from @@ -2249,6 +2271,11 @@ async function info(id, key, strict, limit, quiet, signal) { if (strict && !(e?.signIn === "expired" && all[k])) throw e const r = reach.getStore() if (r && e?.signIn === "expired") r.refused ??= e?.message ?? String(e) + if (e?.signIn === "expired" && all[k]) { + const was = refusedLists.get(k) + const sign = signInOf(all[k]) + refusedLists.set(k, { sign, at: Date.now(), n: was?.sign === sign ? was.n + 1 : 1, error: e?.message ?? String(e) }) + } // a hook that threw on its vendor's failure has no list to tell: // magpie keeps the one it had, as for one that gave its defaults back out.fellBack = true diff --git a/internal/plugin/models_refused_test.go b/internal/plugin/models_refused_test.go new file mode 100644 index 000000000..19f0cd048 --- /dev/null +++ b/internal/plugin/models_refused_test.go @@ -0,0 +1,59 @@ +package plugin + +import ( + "context" + "os" + "path/filepath" + "strings" + "testing" + "time" +) + +// A models hook that said its vendor refused the sign-in (signIn +// "expired") isn't asked again for that sign-in at once: two dead Zed +// sign-ins were listed, and refused by Zed, ~30 times a minute +// (JZGiVvkSOA75376 on X). A new sign-in is asked at once. +func TestRefusedListIsNotAskedAgain(t *testing.T) { + sandbox(t) + log := filepath.Join(t.TempDir(), "models.log") + t.Setenv("FAKE_MODELS_LOG", log) + asked := func() int { + b, _ := os.ReadFile(log) + return strings.Count(string(b), "asked r-models-gone") + } + ctx, cancel := context.WithTimeout(context.Background(), time.Minute) + defer cancel() + abs, _ := filepath.Abs("testdata/fake/index.js") + if _, err := Add(ctx, abs); err != nil { + t.Fatal(err) + } + k, err := Import(ctx, "fakeco", map[string]any{"type": "oauth", "refresh": "r-models-gone", "access": "a", "expires": 9e15, "accountId": "gone@fake"}) + if err != nil { + t.Fatal(err) + } + if _, err := Providers(ctx); err != nil { + t.Fatal(err) + } + first := asked() + if first == 0 { + t.Fatal("the models hook wasn't asked for the account") + } + for range 3 { + if _, err := Providers(ctx); err != nil { + t.Fatal(err) + } + } + if got := asked(); got != first { + t.Fatalf("3 more listings asked the hook of a refused sign-in %d more times, want 0", got-first) + } + // signed in again: asked at once + if err := Restore(ctx, "fakeco", k, map[string]any{"type": "oauth", "refresh": "r-models-gone", "access": "a2", "expires": 9e15, "accountId": "gone@fake"}); err != nil { + t.Fatal(err) + } + if _, err := Providers(ctx); err != nil { + t.Fatal(err) + } + if got := asked(); got <= first { + t.Fatal("a new sign-in's models weren't asked") + } +} diff --git a/internal/plugin/testdata/fake/index.js b/internal/plugin/testdata/fake/index.js index ea884fb21..248b46415 100644 --- a/internal/plugin/testdata/fake/index.js +++ b/internal/plugin/testdata/fake/index.js @@ -372,7 +372,7 @@ export const FakePlugin = async ({ client }) => ({ id: ID, models: async (p, { auth }) => { // $FAKE_MODELS_LOG: each time the hook is asked is a line there - if (process.env.FAKE_MODELS_LOG) (await import("node:fs")).appendFileSync(process.env.FAKE_MODELS_LOG, "asked\n") + if (process.env.FAKE_MODELS_LOG) (await import("node:fs")).appendFileSync(process.env.FAKE_MODELS_LOG, "asked " + (auth?.refresh ?? auth?.key ?? "-") + "\n") if (auth?.refresh === "r-dead" || auth?.key === "dead") throw new Error("the vendor refused the sign-in") if (auth?.refresh === "r-models-gone") throw Object.assign(new Error("the vendor refused the sign-in"), { signIn: "expired" }) // a sign-in past its time, said in words only, as Grok's plugin says it