fix(app): keep plugin update findings until the settled list agrees (#64525)

Co-authored-by: James Long <james@anoma.ly>
This commit is contained in:
opencode-agent[bot]
2026-11-17 16:32:59 -05:00
committed by GitHub
Unverified
parent 7988f65ab8
commit cb41d3da12
4 changed files with 156 additions and 68 deletions
@@ -256,6 +256,7 @@ function PluginUpdateButton(props: { plugin: PluginInfo; updates?: ExtensionPlug
<Button
size="small"
variant="ghost"
class="shrink-0 whitespace-nowrap"
disabled={current().updating}
aria-busy={current().updating}
aria-label={language.t("settings.plugins.update.label", { name: pluginLabel(props.plugin) })}
@@ -1,4 +1,4 @@
import type { McpServer } from "@opencode/client/promise"
import type { McpServer, PluginInfo } from "@opencode/client/promise"
import {
Component,
For,
@@ -90,7 +90,7 @@ export const SettingsExtensions: Component<{
const plugins = createExtensionLoad({
key: () => serverSdk.scope,
task: (_, signal) => serverSdk.api.plugin.list(undefined, { signal }).then((result) => result.data),
task: (_, signal) => serverSdk.api.plugin.list(undefined, { signal }),
})
// Plugin registries are per Location, so every `plugin.updated` names one; the default Location's names its resolved
@@ -100,7 +100,7 @@ export const SettingsExtensions: Component<{
const pluginList = createMemo(() => {
const current = plugins.view()
return current.status === "ready" ? current.value : undefined
return current.status === "ready" ? current.value.data : undefined
})
const [updateState, setUpdateState] = createSignal(createPluginUpdateState())
@@ -130,11 +130,7 @@ export const SettingsExtensions: Component<{
dispatchUpdate({
type: "check.succeeded",
attempt,
outdated: Object.fromEntries(
result.data.flatMap((plugin) =>
plugin.source.type === "package" ? [[plugin.source.target, plugin.source.outdated === true]] : [],
),
),
outdated: packageFlags(result.data),
}),
(error) => dispatchUpdate({ type: "check.failed", attempt, message: formatServerError(error, language.t) }),
)
@@ -155,7 +151,30 @@ export const SettingsExtensions: Component<{
{ defer: true },
),
)
createEffect(on(pluginList, (list) => list && dispatchUpdate({ type: "list.loaded" }), { defer: true }))
const packageFlags = (list: readonly PluginInfo[]) =>
Object.fromEntries(
list.flatMap((plugin) =>
plugin.source.type === "package" ? [[plugin.source.target, plugin.source.outdated === true]] : [],
),
)
createEffect(
on(
() => plugins.view(),
(current) => {
if (current.status !== "ready") return
dispatchUpdate({
type: "list.loaded",
ready: current.value.ready !== false,
outdated: packageFlags(current.value.data),
})
},
{ defer: true },
),
)
const hasPackages = () => pluginList()?.some((plugin) => plugin.source.type === "package") ?? false
onCleanup(() => dispatchUpdate({ type: "disposed" }))
const pluginUpdates = {
@@ -322,7 +341,7 @@ export const SettingsExtensions: Component<{
<Tabs.Content value="plugins">
<div class="settings-section">
<div class="flex items-center justify-between">
<div class="flex flex-wrap items-center justify-between gap-2">
<span class="settings-extension-heading text-13-medium">
{language.t("settings.extensions.availableAll")}
</span>
@@ -330,17 +349,20 @@ export const SettingsExtensions: Component<{
<span class="text-13-regular text-v2-text-text-muted">
{language.t("settings.extensions.manageConfig")}
</span>
<Button
size="small"
variant="ghost-muted"
disabled={pluginUpdateChecking(updateState())}
aria-busy={pluginUpdateChecking(updateState())}
onClick={() => dispatchUpdate({ type: "check.requested" })}
>
{language.t(
pluginUpdateChecking(updateState()) ? "settings.plugins.checking" : "settings.plugins.check",
)}
</Button>
<Show when={hasPackages()}>
<Button
size="small"
variant="ghost-muted"
class="shrink-0 whitespace-nowrap"
disabled={pluginUpdateChecking(updateState())}
aria-busy={pluginUpdateChecking(updateState())}
onClick={() => dispatchUpdate({ type: "check.requested" })}
>
{language.t(
pluginUpdateChecking(updateState()) ? "settings.plugins.checking" : "settings.plugins.check",
)}
</Button>
</Show>
</div>
</div>
{/* The desktop's own server also lists the plugins main runs, so its rows stay while the list loads. */}
@@ -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<typeof pluginUpdateView>[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 },
])
})
@@ -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<Record<string, boolean>>
readonly findings: Readonly<Record<string, PluginUpdateFinding>>
readonly updating: readonly RunningPluginUpdate[]
}
| {
readonly status: "checking"
readonly attempt: number
readonly nextId: number
readonly outdated: Readonly<Record<string, boolean>>
readonly findings: Readonly<Record<string, PluginUpdateFinding>>
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<Record<string, boolean>> }
/** 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"