From 10cac9ab5d5c2d2553e4721b87d6cea798ace7e5 Mon Sep 17 00:00:00 2001 From: Kit Langton Date: Thu, 17 Sep 2026 22:33:43 -0700 Subject: [PATCH] fix(tui): show update installation progress (#49676) --- packages/cli/src/commands/handlers/default.ts | 17 ++++- packages/cli/src/services/updater.ts | 5 +- packages/tui/src/component/dialog-update.tsx | 16 +++-- .../tui/src/context/update-notification.tsx | 9 ++- .../tui/test/component/dialog-update.test.tsx | 62 +++++++++++++++++++ 5 files changed, 98 insertions(+), 11 deletions(-) create mode 100644 packages/tui/test/component/dialog-update.test.tsx diff --git a/packages/cli/src/commands/handlers/default.ts b/packages/cli/src/commands/handlers/default.ts index cc47db98eed..8bf7410615f 100644 --- a/packages/cli/src/commands/handlers/default.ts +++ b/packages/cli/src/commands/handlers/default.ts @@ -47,7 +47,14 @@ export default Runtime.handler(Commands, (input) => ), ) const updater = yield* Updater.Service - const update = yield* updater.run().pipe(Effect.forkScoped) + let installing: string | undefined + const updateListeners = new Set<(version: string) => void>() + const update = yield* updater + .run((version) => { + installing = version + updateListeners.forEach((notify) => notify(version)) + }) + .pipe(Effect.ensuring(Effect.sync(() => (installing = undefined))), Effect.forkScoped) preflight.loading() const config = yield* Config.Service const npm = yield* Npm.Service @@ -92,7 +99,13 @@ export default Runtime.handler(Commands, (input) => ), { signal }, ), - check: (signal) => runPromise(Fiber.join(update).pipe(Effect.flatMap(() => updater.check())), { signal }), + check: (signal, notify) => { + if (installing) notify(installing) + updateListeners.add(notify) + return runPromise(Fiber.join(update).pipe(Effect.flatMap(() => updater.check())), { signal }).finally(() => + updateListeners.delete(notify), + ) + }, apply: (version) => runPromise(updater.apply(version)), }, packages: { diff --git a/packages/cli/src/services/updater.ts b/packages/cli/src/services/updater.ts index 530929c7e93..b1a85db1fd8 100644 --- a/packages/cli/src/services/updater.ts +++ b/packages/cli/src/services/updater.ts @@ -13,7 +13,7 @@ export type RunResult = { readonly type: "available" | "installed"; readonly ver export type CheckResult = RunResult | { readonly type: "unavailable"; readonly message: string } export interface Interface { - readonly run: () => Effect.Effect + readonly run: (onInstall?: (version: string) => void) => Effect.Effect readonly check: () => Effect.Effect readonly apply: (version: string) => Effect.Effect readonly method: () => Effect.Effect @@ -275,10 +275,11 @@ const make = Effect.gen(function* () { }) const run = Effect.fn("cli.updater.run")( - function* () { + function* (onInstall: (version: string) => void = () => {}) { const result = yield* inspect() if (!result) return undefined if (result.policy === "notify") return { type: "available" as const, version: result.version } + onInstall(result.version) if (!(yield* install(result.version))) return yield* Effect.fail(new Error("Installation method not found")) return { type: "installed" as const, version: result.version } }, diff --git a/packages/tui/src/component/dialog-update.tsx b/packages/tui/src/component/dialog-update.tsx index 83f7ccda363..e8c6618b661 100644 --- a/packages/tui/src/component/dialog-update.tsx +++ b/packages/tui/src/component/dialog-update.tsx @@ -32,12 +32,14 @@ export function DialogUpdate(props: { }), ) const state = createMemo(() => { + const message = error() + if (message) return { type: "check-failed" as const, message } + const current = props.state() + if (current?.type === "installing") return current if (check.loading) return { type: "checking" as const } const unavailable = check() if (unavailable) return { type: "unavailable" as const, message: unavailable } - const message = error() - if (message) return { type: "check-failed" as const, message } - return props.state() ?? { type: "current" as const } + return current ?? { type: "current" as const } }) const buttons = createMemo(() => { const type = state().type @@ -87,9 +89,11 @@ export function DialogUpdate(props: { - {state().type === "available" || state().type === "installing" || state().type === "failed" - ? "Update available" - : "Update"} + {state().type === "installing" + ? "Updating OpenCode" + : state().type === "available" || state().type === "failed" + ? "Update available" + : "Update"} dialog.clear()}> esc diff --git a/packages/tui/src/context/update-notification.tsx b/packages/tui/src/context/update-notification.tsx index be2b9155355..d4190681623 100644 --- a/packages/tui/src/context/update-notification.tsx +++ b/packages/tui/src/context/update-notification.tsx @@ -20,6 +20,7 @@ export type UpdateSource = { readonly subscribe: (notify: (notice: ClientNotice) => void, signal: AbortSignal) => Promise readonly check: ( signal: AbortSignal, + onInstall: (version: string) => void, ) => Promise readonly apply: (version: string) => Promise } @@ -74,7 +75,13 @@ export const { use: useUpdateNotification, provider: UpdateNotificationProvider const check = async (signal: AbortSignal) => { const updater = props.updater if (!updater || state()?.type === "installing") return - const result = await updater.check(signal) + const result = await updater + .check(signal, (version) => { + if (!signal.aborted) setState({ type: "installing", version }) + }) + .finally(() => { + if (state()?.type === "installing") setState(undefined) + }) if (signal.aborted) return if (result?.type === "unavailable") return result.message setState(result) diff --git a/packages/tui/test/component/dialog-update.test.tsx b/packages/tui/test/component/dialog-update.test.tsx new file mode 100644 index 00000000000..066933e0362 --- /dev/null +++ b/packages/tui/test/component/dialog-update.test.tsx @@ -0,0 +1,62 @@ +/** @jsxImportSource @opentui/solid */ +import { testRender } from "@opentui/solid" +import { expect, test } from "bun:test" +import { createSignal } from "solid-js" +import { DialogUpdate } from "../../src/component/dialog-update" +import { ConfigProvider } from "../../src/config" +import type { UpdateState } from "../../src/context/update-notification" +import { Keymap } from "../../src/context/keymap" +import { ThemeProvider } from "../../src/context/theme" +import { DialogProvider } from "../../src/ui/dialog" +import { ToastProvider } from "../../src/ui/toast" +import { emptyThemeSource, tmpdir } from "../fixture/fixture" +import { TestTuiContexts } from "../fixture/tui-environment" +import { createTuiResolvedConfig } from "../fixture/tui-runtime" + +test("installation progress replaces checking while the update job is still pending", async () => { + await using temporary = await tmpdir() + const [state, setState] = createSignal() + const pending = Promise.withResolvers() + const app = await testRender( + () => ( + + + + + + + pending.promise} + state={state} + skip={() => {}} + install={() => Promise.resolve()} + restart={() => {}} + /> + + + + + + + ), + { width: 80, height: 24, kittyKeyboard: true }, + ) + + try { + app.renderer.start() + await app.waitForFrame((frame) => frame.includes("Checking for updates")) + setState({ type: "installing", version: "2.0.0" }) + await app.waitForFrame( + (frame) => + frame.includes("Updating OpenCode") && + frame.includes("Installing OpenCode 2.0.0") && + !frame.includes("Checking"), + ) + expect(app.captureCharFrame()).not.toContain("Skip") + pending.reject(new Error("Update service unavailable")) + await app.waitForFrame((frame) => frame.includes("Update service unavailable") && !frame.includes("Installing")) + } finally { + pending.resolve(undefined) + app.renderer.destroy() + } +})