Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
plugin: a plugin stuck on its vendor no longer holds up the plugins loaded or listed with it
yetonefed0055da86f
diff --git a/docs/subsystems/provider-plugins.md b/docs/subsystems/provider-plugins.mdindex cb5d65804..b707c455f 100644--- a/docs/subsystems/provider-plugins.md+++ b/docs/subsystems/provider-plugins.md@@ -124,6 +124,10 @@ How the store handles picks and names:- Re-adding a plugin under a new version or spec keeps its picks.- Remove, off, options and prefer find a plugin by its exact spec first, then by package name (`indexOf` in `store.go`). This way, acting on one plugin never reaches another plugin that has the same package name.+## A plugin that is slow or stuck++The host loads every plugin side by side (`loadPlugins` in [`host.js`](../../internal/plugin/host.js)) and gives each one `pluginLoadWait` (20s, [`host.go`](../../internal/plugin/host.go)) for its import and its plugin function, and each config hook as long. A plugin still loading then is told as not loaded ("the plugin's load didn't finish in 20s") and the host answers `init` with the rest; what it returns later is dropped until the host starts again. Hooks are still kept in the list's order. A listing (`providers`) asks every provider side by side too, and gives each models hook `pluginListWait` (20s, inside `refreshDeadline`'s 30s): one that hasn't answered is listed as fallen back with that as its `listError`, so magpie keeps the list it had, and that answer isn't kept for requests (`keepInfo`). Before, one plugin whose sign-in kept failing held every plugin after it until `init`'s two-minute deadline failed the whole host, and one stuck models hook failed the whole list (shualaitianduai on Discord). `TestStuckPluginLoadHoldsNoOther` and `TestStuckModelListHoldsNoOther` hold both.+## How ownership changesMigration records use five states:diff --git a/internal/plugin/host.go b/internal/plugin/host.goindex e037d29d5..3530cb04e 100644--- a/internal/plugin/host.go+++ b/internal/plugin/host.go@@ -998,6 +998,8 @@ func startOn(ctx context.Context, bun string) (*host, bool, error) {"prefer": l.Prefer,"options": settings.Load().PluginOptions,"plugins": items,+ "loadMillis": pluginLoadWait.Milliseconds(),+ "listMillis": pluginListWait.Milliseconds(),}, &res)if err != nil {crashed := !h.alive()@@ -1014,6 +1016,16 @@ func startOn(ctx context.Context, bun string) (*host, bool, error) {return h, false, nil}+// pluginLoadWait bounds one plugin's load in the host, and+// pluginListWait one provider's model list in a listing: a plugin stuck on+// its vendor (a sign-in refused and tried again) is given up on by itself,+// within init's and refreshDeadline's own bounds, rather than holding up+// every plugin loaded or listed with it. Tests shorten them.+var (+ pluginLoadWait = 20 * time.Second+ pluginListWait = 20 * time.Second+)+// drainStderr logs the child's stderr a line at a time. A line over 4 MiB// stops the scanner; the rest is read and dropped, so the child is never left// blocked on a pipe nobody reads, and the log says from where.diff --git a/internal/plugin/host.js b/internal/plugin/host.jsindex 600d35800..9ac5337d9 100644--- a/internal/plugin/host.js+++ b/internal/plugin/host.js@@ -313,6 +313,28 @@ const renewing = new Map() // account → its sign-in's renewal under wayconst unrenewed = new Map() // account → its last renewal that failed: {at, secret, gone}let config = { provider: {} } // what the plugins' config hooks made of it+// loadWait bounds one plugin's load (its import and its plugin function)+// and one config hook; listWait bounds one provider's models hook in a+// listing. A plugin stuck on its vendor (a sign-in answered 401 and tried+// again, a vendor that never answers) is then given up on alone: the+// plugins load side by side and every other one is listed meanwhile,+// rather than all waiting behind it until magpie's own deadline fails the+// whole host or the whole list. magpie sets both at init.+let loadWait = 20 * 1000+let listWait = 20 * 1000++// late is what a step that took longer than ms gives instead of its own+// result: an error saying so. The step goes on by itself; what it gives+// later is dropped.+class Late extends Error {}+function inTime(step, ms, what) {+ let timer+ const late = new Promise((_, no) => {+ timer = setTimeout(() => no(Object.assign(new Late(`${what} didn't finish in ${Math.round(ms / 1000)}s`), { plain: true })), ms)+ })+ return Promise.race([Promise.resolve().then(step), late]).finally(() => clearTimeout(timer))+}+// ---- a provider's own proxy ---------------------------------------------------// A request made for a provider or an account with a proxy of its own@@ -1217,41 +1239,56 @@ async function loadPlugins(list) {loaded.push(r.error ? { spec: r.spec, error: r.error } : { spec: r.spec })}}- for (const p of list) {+ // each plugin loads by itself, side by side, so one that is slow or+ // stuck holds up none of the others; what they add is kept in the+ // list's order all the same, as a later plugin's hooks win+ const one = async (p, isPi) => {+ let gateway+ if (p.gateway) {+ gateway = await import(pathToFileURL(p.gateway).href)+ if (typeof gateway.onRequest !== "function") throw new Error("magpie.hooks exports no onRequest")+ }+ let fns = []+ // A gateway-only package needs no OpenCode entry. Mixed packages+ // still load their provider hooks through the usual entry rules.+ const pkg = readJSON(path.join(p.target, "package.json"))+ const onlyGateway = isPi || (p.gateway && (p.gateway === p.target || (!pkg?.main && !pkg?.exports && !INDEX_FILES.some((f) => fs.existsSync(path.join(p.target, f))))))+ for (const file of onlyGateway ? [] : entries(p.target)) {+ fns = servers(await import(pathToFileURL(file).href))+ if (fns.length) break+ }+ if (fns.length === 0 && !gateway) throw new Error("exports no OpenCode plugin function")+ const got = []+ for (const fn of fns) got.push({ spec: p.spec, target: p.target, hooks: (await fn(input, p.options)) ?? {} })+ return { got, gateway }+ }+ const results = await Promise.all(list.map((p) => {const isPi = pis.includes(p)- if (isPi && !p.gateway) continue- try {- let gateway- if (p.gateway) {- gateway = await import(pathToFileURL(p.gateway).href)- if (typeof gateway.onRequest !== "function") throw new Error("magpie.hooks exports no onRequest")- }- let fns = []- // A gateway-only package needs no OpenCode entry. Mixed packages- // still load their provider hooks through the usual entry rules.- const pkg = readJSON(path.join(p.target, "package.json"))- const onlyGateway = isPi || (p.gateway && (p.gateway === p.target || (!pkg?.main && !pkg?.exports && !INDEX_FILES.some((f) => fs.existsSync(path.join(p.target, f))))))- for (const file of onlyGateway ? [] : entries(p.target)) {- fns = servers(await import(pathToFileURL(file).href))- if (fns.length) break- }- if (fns.length === 0 && !gateway) throw new Error("exports no OpenCode plugin function")- for (const fn of fns) hooks.push({ spec: p.spec, target: p.target, hooks: (await fn(input, p.options)) ?? {} })- if (gateway) gatewayHooks.push({ spec: p.spec, options: p.options ?? {}, onRequest: gateway.onRequest,- onResponse: gateway.onResponse,- state: { calls: 0, failures: 0, totalMillis: 0, lastOutcome: "idle", lastBytes: 0 } })- if (!isPi) loaded.push({ spec: p.spec })- } catch (e) {+ if (isPi && !p.gateway) return null+ return inTime(() => one(p, isPi), loadWait, "the plugin's load").then((r) => ({ p, isPi, ...r }), (e) => ({ p, isPi, e }))+ }))+ for (const r of results) {+ if (!r) continue+ const { p, isPi } = r+ if (r.e) {+ const e = r.econst error = e?.plain ? e.message : String(e?.stack ?? e)- if (isPi) loaded.find((r) => r.spec === p.spec).error = error+ if (isPi) loaded.find((x) => x.spec === p.spec).error = errorelse loaded.push({ spec: p.spec, error })+ continue}+ for (const x of r.got) hooks.push(x)+ if (r.gateway) gatewayHooks.push({ spec: p.spec, options: p.options ?? {}, onRequest: r.gateway.onRequest,+ onResponse: r.gateway.onResponse,+ state: { calls: 0, failures: 0, totalMillis: 0, lastOutcome: "idle", lastBytes: 0 } })+ if (!isPi) loaded.push({ spec: p.spec })}config = { provider: {}, ...structuredClone(userConfig) }for (const h of hooks) {if (typeof h.hooks.config !== "function") continuetry {- await h.hooks.config(config)+ // one at a time, as each may build on the last, but none for long+ await inTime(() => h.hooks.config(config), loadWait, "config hook")} catch (e) {send({ event: "log", level: "error", message: `${h.spec}: config hook: ${e?.message ?? e}` })}@@ -1670,7 +1707,7 @@ function fromModelsDev(p, m) {// info is what OpenCode knows of provider id, its models as the plugin// lists them for account key (the first when none); strict fails where the// plugin's list does, rather than keeping the list it was given.-async function info(id, key, strict) {+async function info(id, key, strict, limit) {const md = mdev()[id]const cfg = config.provider?.[id]const out = {@@ -1732,7 +1769,8 @@ async function info(id, key, strict) {try {const given = JSON.parse(JSON.stringify(out))const l = { tried: false, lastOk: false }- const next = await listing.run(l, () => inScope(id, k, () => ph.models(given, { auth: authCopy(k) })))+ const ask = () => listing.run(l, () => inScope(id, k, () => ph.models(given, { auth: authCopy(k) })))+ const next = limit > 0 ? await inTime(ask, limit, "the plugin's model list") : await ask()// a hook that asked its vendor, got no list and gave back the one it// was given fell back: magpie keeps the list it had, as a built-in// whose fetch failed keeps the one it fetched last; so does one that@@ -1744,6 +1782,9 @@ async function info(id, key, strict) {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 }]))} catch (e) {+ // a listing that gave up waiting: what the hook gives later is no+ // list a request should be built from+ if (e instanceof Late) out.late = true// an error the models hook throws may say what it means for the// sign-in, as an answer's X-Magpie-Sign-In does: a built-in whose// model list the vendor refused marked the account@@ -1782,7 +1823,7 @@ async function infoOf(id, key) {// keepInfo keeps p, info(id, key) just built by a listing, for the// account's requests.function keepInfo(key, p, gen) {- if (gen === infoGen && !p.failed) infos.set(key, { gen, md: mdev(), info: p })+ if (gen === infoGen && !p.failed && !p.late) infos.set(key, { gen, md: mdev(), info: p })}// shown is a plugin's string as magpie shows it: trimmed and at most n@@ -1900,23 +1941,24 @@ function tiersOf(v) {// built-in fetches each account's list through the account's.async function providers({ proxies } = {}) {const stored = readAuth()- const out = []- for (const [id, a] of auths()) {+ // every provider is listed side by side, each models hook given+ // listWait: one whose vendor is stuck holds up no other's row+ const row = async ([id, a]) => {const keys = accountsOf(stored, id)const through = (k) => proxies?.[id]?.[k ?? keys[0] ?? ""] ?? proxies?.[id]?.[""] ?? ""const gen = infoGen- const p = await via.run(through(), () => info(id))+ const p = await via.run(through(), () => info(id, undefined, false, listWait))keepInfo(keys[0] ?? id, p, gen)// each account's own models, as the built-ins read each account's: a// plan may serve fewer, or others, than the first account's. One that// can't be read is taken to have them all, or the ones it was last// told to have, as a built-in account whose fetch failed keeps its own.- const own = await Promise.all(keys.slice(1).map((k) => via.run(through(k), () => info(id, k, true)).then((q) => (keepInfo(k, q, gen), q)).catch((e) => ({ failed: true, listError: e?.message ?? String(e) }))))+ const own = await Promise.all(keys.slice(1).map((k) => via.run(through(k), () => info(id, k, true, listWait)).then((q) => (keepInfo(k, q, gen), q)).catch((e) => ({ failed: true, listError: e?.message ?? String(e) }))))const models = { ...p.models }for (const q of own) for (const [k, m] of Object.entries(q?.models ?? {})) models[k] ??= mconst ids = (q) => Object.values(q.models).filter((m) => m.status !== "deprecated").map((m) => m.id)const first = stored[keys[0]]- out.push({+ return {id,spec: a.spec,name: p.name,@@ -1982,9 +2024,9 @@ async function providers({ proxies } = {}) {// words, which magpie may search with for other modelssearches: m.searches === true,})),- })+ }}- return out+ return Promise.all([...auths()].map(row))}// ---- signing in --------------------------------------------------------------@@ -2584,6 +2626,8 @@ const handlers = {userConfig = p.config ?? {}prefer = p.prefer ?? {}chosen = p.options ?? {}+ if (p.loadMillis > 0) loadWait = p.loadMillis+ if (p.listMillis > 0) listWait = p.listMillisawait loadPlugins(p.plugins ?? [])return { plugins: loaded.map(withProviders) }},diff --git a/internal/plugin/plugin_stuck_test.go b/internal/plugin/plugin_stuck_test.gonew file mode 100644index 000000000..9dca665fe--- /dev/null+++ b/internal/plugin/plugin_stuck_test.go@@ -0,0 +1,103 @@+package plugin++import (+ "context"+ "path/filepath"+ "strings"+ "testing"+ "time"+)++// stuckWaits shortens how long the host waits on one plugin's load and on+// one provider's model list, for the test.+func stuckWaits(t *testing.T) {+ t.Helper()+ load, list := pluginLoadWait, pluginListWait+ pluginLoadWait, pluginListWait = 2*time.Second, 2*time.Second+ t.Cleanup(func() { pluginLoadWait, pluginListWait = load, list })+}++// addStuck adds the stuck plugin ahead of the fake one, so the host meets+// it first, as a plugin earlier in the user's list.+func addStuck(t *testing.T, ctx context.Context) {+ t.Helper()+ for _, p := range []string{"testdata/stuck/index.js", "testdata/fake/index.js"} {+ abs, _ := filepath.Abs(p)+ if _, err := Add(ctx, abs); err != nil {+ t.Fatal(err)+ }+ }+}++// A plugin whose load never finishes, as one whose sign-in is refused and+// tried again, holds up none of the plugins after it: they load and are+// listed, and it is told as not loaded, saying why (shualaitianduai on+// Discord). Before, the host loaded the plugins one after another and+// answered init only when all were in, so every plugin waited until+// magpie's two-minute deadline failed the whole host.+func TestStuckPluginLoadHoldsNoOther(t *testing.T) {+ sandbox(t)+ stuckWaits(t)+ t.Setenv("STUCK_LOAD", "1")+ ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)+ defer cancel()+ addStuck(t, ctx)++ ls, err := Plugins(ctx)+ if err != nil || len(ls) != 2 {+ t.Fatalf("Plugins = %+v, %v", ls, err)+ }+ for _, l := range ls {+ stuck := strings.Contains(l.Spec, "stuck")+ if stuck && !strings.Contains(l.Error, "didn't finish") {+ t.Errorf("the stuck plugin says %q, not that it didn't finish loading", l.Error)+ }+ if !stuck && l.Error != "" {+ t.Errorf("the plugin after the stuck one didn't load: %s", l.Error)+ }+ }+ ps, err := Providers(ctx)+ if err != nil || len(ps) != 1 || ps[0].ID != "fakeco" || len(ps[0].Models) == 0 {+ t.Fatalf("Providers = %+v, %v; want fakeco with its models", ps, err)+ }+}++// A provider whose models hook never answers holds up no other provider's+// listing: the list comes back within the host's wait, the other provider+// with its models and the stuck one with a list error saying it gave up.+// Before, the host listed the providers one after another, and the whole+// list waited on the stuck one past refreshDeadline.+func TestStuckModelListHoldsNoOther(t *testing.T) {+ sandbox(t)+ stuckWaits(t)+ t.Setenv("STUCK_MODELS", "1")+ ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)+ defer cancel()+ addStuck(t, ctx)+ for _, id := range []string{"stuckco", "fakeco"} {+ if _, err := APIKey(ctx, id, 0, nil, "k-"+id, NewAccount); err != nil {+ t.Fatal(err)+ }+ }++ lctx, lcancel := context.WithTimeout(ctx, 15*time.Second)+ defer lcancel()+ ps, err := Providers(lctx)+ if err != nil || len(ps) != 2 {+ t.Fatalf("Providers = %+v, %v", ps, err)+ }+ for _, p := range ps {+ switch p.ID {+ case "fakeco":+ if p.FellBack || len(p.Models) == 0 {+ t.Errorf("fakeco listed fellBack=%v with %d models, %q", p.FellBack, len(p.Models), p.ListError)+ }+ case "stuckco":+ if !p.FellBack || !strings.Contains(p.ListError, "didn't finish") {+ t.Errorf("stuckco listed fellBack=%v saying %q", p.FellBack, p.ListError)+ }+ default:+ t.Errorf("listed %q", p.ID)+ }+ }+}diff --git a/internal/plugin/testdata/stuck/index.js b/internal/plugin/testdata/stuck/index.jsnew file mode 100644index 000000000..32a043a42--- /dev/null+++ b/internal/plugin/testdata/stuck/index.js@@ -0,0 +1,32 @@+// A plugin whose vendor never lets it through, as one whose sign-in is+// answered 401 and tried again for ever: with $STUCK_LOAD set its plugin+// function never returns, and with $STUCK_MODELS set its models hook+// never does.+const never = () => new Promise(() => {})++export const StuckPlugin = async () => {+ if (process.env.STUCK_LOAD) await never()+ return {+ config: async (cfg) => {+ cfg.provider = cfg.provider ?? {}+ cfg.provider.stuckco = {+ name: "StuckCo",+ npm: "@ai-sdk/openai-compatible",+ api: "https://stuck.invalid/v1",+ models: { "stuck-1": { name: "Stuck One", limit: { context: 1000, output: 100 } } },+ }+ },+ provider: {+ id: "stuckco",+ async models(provider) {+ if (process.env.STUCK_MODELS) await never()+ return provider.models+ },+ },+ auth: {+ provider: "stuckco",+ methods: [{ type: "api", label: "API key" }],+ loader: async () => ({ baseURL: "https://stuck.invalid/v1" }),+ },+ }+}