From cb41d3da129426978525a5c15d94c0ee4666c925 Mon Sep 17 00:00:00 2001 From: "opencode-agent[bot]" <219766164+opencode-agent[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:32:59 -0500 Subject: [PATCH] fix(app): keep plugin update findings until the settled list agrees (#64525) Co-authored-by: James Long --- .../src/runtime/plugin/extension-plugins.tsx | 1 + .../app/src/settings/providers/extensions.tsx | 64 +++++++---- .../providers/plugin-update-machine.test.ts | 101 ++++++++++++------ .../providers/plugin-update-machine.ts | 58 +++++++--- 4 files changed, 156 insertions(+), 68 deletions(-) diff --git a/packages/app/src/runtime/plugin/extension-plugins.tsx b/packages/app/src/runtime/plugin/extension-plugins.tsx index c8b58897bdf..1f81a3ddc65 100644 --- a/packages/app/src/runtime/plugin/extension-plugins.tsx +++ b/packages/app/src/runtime/plugin/extension-plugins.tsx @@ -256,6 +256,7 @@ function PluginUpdateButton(props: { plugin: PluginInfo; updates?: ExtensionPlug + + + {/* The desktop's own server also lists the plugins main runs, so its rows stay while the list loads. */} diff --git a/packages/app/src/settings/providers/plugin-update-machine.test.ts b/packages/app/src/settings/providers/plugin-update-machine.test.ts index 0cbb1f3886c..92729b1dee0 100644 --- a/packages/app/src/settings/providers/plugin-update-machine.test.ts +++ b/packages/app/src/settings/providers/plugin-update-machine.test.ts @@ -25,7 +25,16 @@ function harness() { return { machine, effects, dispatch } } -const source = { target: "opencode-plugin-a@latest" } as const +const a = "opencode-plugin-a@latest" + +const b = "opencode-plugin-b@latest" + +/** A listed package source, which carries `outdated` only when it is true. */ +function listed(target: string, outdated: boolean): Parameters[1] { + if (!outdated) return { target } + + return { target, outdated: true } +} test("a check claims itself at once, ignores a second request, and its result overrides the listed flags", () => { const run = harness() @@ -38,76 +47,102 @@ test("a check claims itself at once, ignores a second request, and its result ov expect(pluginUpdateChecking(run.machine.state)).toBe(true) expect(run.effects).toEqual([{ type: "check", attempt: 0 }]) - run.dispatch({ type: "check.succeeded", attempt: 0, outdated: { [source.target]: true } }) + run.dispatch({ type: "check.succeeded", attempt: 0, outdated: { [a]: true } }) expect(pluginUpdateChecking(run.machine.state)).toBe(false) - expect(pluginUpdateView(run.machine.state, source)).toEqual({ outdated: true, updating: false }) - - run.dispatch({ type: "list.loaded" }) - - expect(pluginUpdateView(run.machine.state, source)).toEqual({ outdated: false, updating: false }) - expect(pluginUpdateView(run.machine.state, { ...source, outdated: true })).toEqual({ - outdated: true, - updating: false, - }) + expect(pluginUpdateView(run.machine.state, listed(a, false))).toEqual({ outdated: true, updating: false }) }) test("a failed check ends idle and reports the error", () => { const run = harness() - const message = "offline" run.dispatch({ type: "check.requested" }) - run.dispatch({ type: "check.failed", attempt: 0, message }) + run.dispatch({ type: "check.failed", attempt: 0, message: "offline" }) expect(pluginUpdateChecking(run.machine.state)).toBe(false) - expect(run.effects.at(-1)).toEqual({ type: "failed", message }) + expect(run.effects.at(-1)).toEqual({ type: "failed", message: "offline" }) }) test("an update runs once per package, then refreshes the list and hides the button", () => { const run = harness() - const outdated = { ...source, outdated: true } as const - run.dispatch({ type: "update.requested", target: source.target }) + run.dispatch({ type: "update.requested", target: a }) const updating = run.machine.state - run.dispatch({ type: "update.requested", target: source.target }) + run.dispatch({ type: "update.requested", target: a }) expect(run.machine.state).toBe(updating) - expect(pluginUpdateView(run.machine.state, outdated)).toEqual({ outdated: true, updating: true }) + expect(pluginUpdateView(run.machine.state, listed(a, true))).toEqual({ outdated: true, updating: true }) run.dispatch({ type: "update.succeeded", id: 0 }) - expect(run.effects).toEqual([{ type: "update", id: 0, target: source.target }, { type: "refresh" }]) - // The list still carries the old flag until it loads again. - expect(pluginUpdateView(run.machine.state, outdated)).toEqual({ outdated: false, updating: false }) + expect(run.effects).toEqual([{ type: "update", id: 0, target: a }, { type: "refresh" }]) + expect(pluginUpdateView(run.machine.state, listed(a, true))).toEqual({ outdated: false, updating: false }) +}) + +// The server reloads its plugins after the update returns, so the refresh can read the old flag. Before, any list load +// cleared what the update learned and the Update button came back until the next reload. +test("a list read before the server reloaded keeps an update's finding until a settled list agrees", () => { + const run = harness() + + run.dispatch({ type: "update.requested", target: a }) + run.dispatch({ type: "update.succeeded", id: 0 }) + const updated = run.machine.state + + run.dispatch({ type: "list.loaded", ready: false, outdated: { [a]: true } }) + run.dispatch({ type: "list.loaded", ready: true, outdated: { [a]: true } }) + + expect(run.machine.state).toBe(updated) + expect(pluginUpdateView(run.machine.state, listed(a, true)).outdated).toBe(false) + + run.dispatch({ type: "list.loaded", ready: true, outdated: { [a]: false } }) + + expect(run.machine.state).not.toBe(updated) + expect(pluginUpdateView(run.machine.state, listed(a, true)).outdated).toBe(true) + expect(pluginUpdateView(run.machine.state, listed(a, false)).outdated).toBe(false) +}) + +test("a check that began before an update cannot mark the updated package outdated again", () => { + const run = harness() + + run.dispatch({ type: "check.requested" }) + run.dispatch({ type: "update.requested", target: a }) + run.dispatch({ type: "update.succeeded", id: 1 }) + run.dispatch({ type: "check.succeeded", attempt: 0, outdated: { [a]: true, [b]: true } }) + + expect(pluginUpdateView(run.machine.state, listed(a, false)).outdated).toBe(false) + expect(pluginUpdateView(run.machine.state, listed(b, false)).outdated).toBe(true) + + // Nor while the update is still running. + run.dispatch({ type: "check.requested" }) + run.dispatch({ type: "update.requested", target: b }) + run.dispatch({ type: "check.succeeded", attempt: 2, outdated: { [b]: false } }) + + expect(pluginUpdateView(run.machine.state, listed(b, false))).toEqual({ outdated: true, updating: true }) }) test("a failed update keeps the package outdated and reports the error", () => { const run = harness() - const message = "npm failed" - run.dispatch({ type: "update.requested", target: source.target }) - run.dispatch({ type: "update.failed", id: 0, message }) + run.dispatch({ type: "update.requested", target: a }) + run.dispatch({ type: "update.failed", id: 0, message: "npm failed" }) - expect(pluginUpdateView(run.machine.state, { ...source, outdated: true })).toEqual({ - outdated: true, - updating: false, - }) - expect(run.effects.at(-1)).toEqual({ type: "failed", message }) + expect(pluginUpdateView(run.machine.state, listed(a, true))).toEqual({ outdated: true, updating: false }) + expect(run.effects.at(-1)).toEqual({ type: "failed", message: "npm failed" }) }) test("results from before a server change or after dispose are ignored", () => { const run = harness() run.dispatch({ type: "check.requested" }) - run.dispatch({ type: "update.requested", target: source.target }) + run.dispatch({ type: "update.requested", target: a }) run.dispatch({ type: "server.changed" }) const changed = run.machine.state - run.dispatch({ type: "check.succeeded", attempt: 0, outdated: { [source.target]: true } }) + run.dispatch({ type: "check.succeeded", attempt: 0, outdated: { [a]: true } }) run.dispatch({ type: "update.failed", id: 1, message: "late" }) expect(run.machine.state).toBe(changed) - expect(pluginUpdateView(run.machine.state, source)).toEqual({ outdated: false, updating: false }) + expect(pluginUpdateView(run.machine.state, listed(a, false))).toEqual({ outdated: false, updating: false }) run.dispatch({ type: "disposed" }) const disposed = run.machine.state @@ -116,6 +151,6 @@ test("results from before a server change or after dispose are ignored", () => { expect(run.machine.state).toBe(disposed) expect(run.effects).toEqual([ { type: "check", attempt: 0 }, - { type: "update", id: 1, target: source.target }, + { type: "update", id: 1, target: a }, ]) }) diff --git a/packages/app/src/settings/providers/plugin-update-machine.ts b/packages/app/src/settings/providers/plugin-update-machine.ts index 078cc994dee..de9d892ac43 100644 --- a/packages/app/src/settings/providers/plugin-update-machine.ts +++ b/packages/app/src/settings/providers/plugin-update-machine.ts @@ -1,22 +1,26 @@ /** One package update on its way; `id` tells it apart from an update of the same package on an earlier server. */ export type RunningPluginUpdate = { readonly id: number; readonly target: string } +/** What a check or update learned about one package; `at` is that operation's id, so an older result never wins. */ +export type PluginUpdateFinding = { readonly outdated: boolean; readonly at: number } + /** - * Checking for and installing plugin package updates from Settings › Extensions. `outdated` holds what the latest - * check or update learned per package, which is fresher than the listed plugins until the list loads again. + * Checking for and installing plugin package updates from Settings › Extensions. `findings` holds what checks and + * updates learned per package. The server reloads its plugins after both return, so a list read in between still + * carries the old flags; a finding stays until a settled list agrees with it. */ export type PluginUpdateState = | { readonly status: "idle" readonly nextId: number - readonly outdated: Readonly> + readonly findings: Readonly> readonly updating: readonly RunningPluginUpdate[] } | { readonly status: "checking" readonly attempt: number readonly nextId: number - readonly outdated: Readonly> + readonly findings: Readonly> readonly updating: readonly RunningPluginUpdate[] } | { readonly status: "disposed" } @@ -29,8 +33,11 @@ export type PluginUpdateEvent = | { readonly type: "update.requested"; readonly target: string } | { readonly type: "update.succeeded"; readonly id: number } | { readonly type: "update.failed"; readonly id: number; readonly message: string } - /** The plugin list loaded again, so its own flags are current. */ - | { readonly type: "list.loaded" } + /** + * The plugin list loaded again. `ready` is false while the server is still reloading its plugins, when the list's + * flags may predate the latest check or update; `outdated` maps each listed package to its flag. + */ + | { readonly type: "list.loaded"; readonly ready: boolean; readonly outdated: Readonly> } /** Another server: forget what this one's checks and updates learned. */ | { readonly type: "server.changed" } | { readonly type: "disposed" } @@ -51,7 +58,7 @@ const none: readonly PluginUpdateEffect[] = [] const step = (state: PluginUpdateState, effects = none): PluginUpdateTransition => ({ state, effects }) export function createPluginUpdateState(): PluginUpdateState { - return { status: "idle", nextId: 0, outdated: {}, updating: [] } + return { status: "idle", nextId: 0, findings: {}, updating: [] } } export function transitionPluginUpdate(state: PluginUpdateState, event: PluginUpdateEvent): PluginUpdateTransition { @@ -59,12 +66,22 @@ export function transitionPluginUpdate(state: PluginUpdateState, event: PluginUp if (event.type === "disposed") return step({ status: "disposed" }) - if (event.type === "server.changed") return step({ status: "idle", nextId: state.nextId, outdated: {}, updating: [] }) + if (event.type === "server.changed") return step({ status: "idle", nextId: state.nextId, findings: {}, updating: [] }) if (event.type === "list.loaded") { - if (Object.keys(state.outdated).length === 0) return step(state) + if (!event.ready) return step(state) - return step({ ...state, outdated: {} }) + const settled = Object.keys(state.findings).filter( + (target) => !(target in event.outdated) || event.outdated[target] === state.findings[target]?.outdated, + ) + + if (settled.length === 0) return step(state) + + const findings = { ...state.findings } + + for (const target of settled) delete findings[target] + + return step({ ...state, findings }) } if (event.type === "check.requested") { @@ -78,11 +95,21 @@ export function transitionPluginUpdate(state: PluginUpdateState, event: PluginUp if (event.type === "check.succeeded" || event.type === "check.failed") { if (state.status !== "checking" || state.attempt !== event.attempt) return step(state) - const idle = { status: "idle", nextId: state.nextId, outdated: state.outdated, updating: state.updating } as const + const idle = { status: "idle", nextId: state.nextId, findings: state.findings, updating: state.updating } as const if (event.type === "check.failed") return step(idle, [{ type: "failed", message: event.message }]) - return step({ ...idle, outdated: event.outdated }) + // An update that started or finished after this check began knows more about its package than the check does. + const findings = { ...state.findings } + + for (const [target, outdated] of Object.entries(event.outdated)) { + const newer = + (findings[target]?.at ?? -1) > event.attempt || state.updating.some((item) => item.target === target) + + if (!newer) findings[target] = { outdated, at: event.attempt } + } + + return step({ ...idle, findings }) } if (event.type === "update.requested") { @@ -103,7 +130,10 @@ export function transitionPluginUpdate(state: PluginUpdateState, event: PluginUp if (event.type === "update.failed") return step({ ...state, updating }, [{ type: "failed", message: event.message }]) - return step({ ...state, updating, outdated: { ...state.outdated, [running.target]: false } }, [{ type: "refresh" }]) + return step( + { ...state, updating, findings: { ...state.findings, [running.target]: { outdated: false, at: running.id } } }, + [{ type: "refresh" }], + ) } export type PluginUpdateView = { readonly outdated: boolean; readonly updating: boolean } @@ -117,7 +147,7 @@ export function pluginUpdateView( const updating = source.updating === true || state.updating.some((item) => item.target === source.target) - return { outdated: state.outdated[source.target] ?? source.outdated === true, updating } + return { outdated: state.findings[source.target]?.outdated ?? source.outdated === true, updating } } export const pluginUpdateChecking = (state: PluginUpdateState) => state.status === "checking"