From f5839d8b45676c02b12f569343edaa6b21596dcc Mon Sep 17 00:00:00 2001 From: bompus Date: Mon, 23 Nov 2026 09:58:34 -0700 Subject: [PATCH 1/4] fix(web): Update everything sends servers the build the desktop app downloaded Servers were sent the version from the desktop app's last update check, which can be hours old, while the desktop download checks the feed again and takes the newest build. One click left a boot-service server one nightly behind the desktop app. Servers now stage and update once the local download reports the version it downloaded, and the confirmed servers move to that version. Fixes #41548 --- .../components/sidebar/SidebarUpdatePill.tsx | 7 +- .../sidebar/updateEverything.logic.test.ts | 100 +++++++++++++++++- .../sidebar/updateEverything.logic.ts | 67 +++++++++--- .../components/sidebar/useUpdateEverything.ts | 40 +++---- 4 files changed, 179 insertions(+), 35 deletions(-) diff --git a/apps/web/src/components/sidebar/SidebarUpdatePill.tsx b/apps/web/src/components/sidebar/SidebarUpdatePill.tsx index 156e89364670..ef38fa78974c 100644 --- a/apps/web/src/components/sidebar/SidebarUpdatePill.tsx +++ b/apps/web/src/components/sidebar/SidebarUpdatePill.tsx @@ -151,7 +151,7 @@ export async function downloadLocalUpdate( notifyDownloaded, reportError = toastUpdateError, }: { readonly notifyDownloaded: boolean; readonly reportError?: ReportLocalUpdateError }, -): Promise { +): Promise { try { const result = await bridge.downloadUpdate(); if (result.completed && notifyDownloaded) { @@ -161,13 +161,14 @@ export async function downloadLocalUpdate( ? getDesktopUpdateActionError(result) : null; if (actionError) reportError(m(msg("Could not download update")), actionError); - return result.completed; + // The download checks the feed again, so this can be newer than the offer it started from. + return result.completed ? getDesktopUpdateDownloadedVersion(result.state) : null; } catch (error) { reportError( m(msg("Could not start update download")), thrownErrorMessage(error, m) ?? m(msg("An unexpected error occurred.")), ); - return false; + return null; } } diff --git a/apps/web/src/components/sidebar/updateEverything.logic.test.ts b/apps/web/src/components/sidebar/updateEverything.logic.test.ts index f4b1d0053b22..1c41d574df44 100644 --- a/apps/web/src/components/sidebar/updateEverything.logic.test.ts +++ b/apps/web/src/components/sidebar/updateEverything.logic.test.ts @@ -8,7 +8,9 @@ import { collectServerUpdateTargets, describeUpdateCapsule, describeUpdateEverything, + retargetServerUpdates, runUpdateEverything, + startServerPhase, type UpdateEverythingMachine, } from "./updateEverything.logic"; @@ -344,11 +346,105 @@ describe("collectRestartingMachines", () => { }); }); +describe("retargetServerUpdates", () => { + const targets = collectServerUpdateTargets( + [machine("service", "1.0.0", "boot-service"), machine("mac", "1.0.0", "desktop-managed")], + "1.1.0", + ).targets; + + it("moves the confirmed servers to the version this app downloaded", () => { + expect( + retargetServerUpdates(targets, "1.2.0").map(({ environmentId, targetVersion }) => [ + environmentId, + targetVersion, + ]), + ).toEqual([ + ["service", "1.2.0"], + ["mac", "1.2.0"], + ]); + }); + + it("keeps the planned targets when this app downloaded nothing", () => { + expect(retargetServerUpdates(targets, null)).toBe(targets); + expect(retargetServerUpdates(targets, undefined)).toBe(targets); + }); +}); + +describe("startServerPhase", () => { + it("starts each server's clock when servers update, with its final target", () => { + const [planned] = collectServerUpdateTargets( + [machine("service", "1.0.0", "boot-service")], + "1.1.0", + ).targets; + const providersOnly = { + environmentId: EnvironmentId.make("laptop"), + label: "laptop", + updatesProviders: true, + server: null, + handedOffVersion: null, + startedAt: 0, + result: "done" as const, + failure: null, + providerFailure: null, + }; + const run = { + localVersion: "1.2.0", + phase: "servers" as const, + restartAt: null, + machines: [ + providersOnly, + { + ...providersOnly, + environmentId: planned!.environmentId, + label: "service", + updatesProviders: false, + server: planned!, + result: null, + }, + ], + }; + + const started = startServerPhase(run, retargetServerUpdates([planned!], "1.2.0"), 30 * 60_000); + + expect(started.machines[0]).toBe(providersOnly); + expect(started.machines[1]).toMatchObject({ + startedAt: 30 * 60_000, + server: { targetVersion: "1.2.0" }, + }); + }); +}); + describe("runUpdateEverything", () => { + it("stages and updates servers with the version the local download reports", async () => { + const calls: string[] = []; + let finishDownload = (_version: string) => {}; + const download = new Promise((resolve) => (finishDownload = resolve)); + const run = runUpdateEverything({ + downloadLocal: () => download, + updateProviders: async () => { + calls.push("providers"); + }, + stageServers: (version) => { + calls.push(`stage ${version}`); + return new Map(); + }, + updateServers: async (_staged, version) => { + calls.push(`servers ${version}`); + }, + }); + await Promise.resolve(); + calls.push("downloaded"); + finishDownload("1.2.0"); + const result = await run; + + expect(calls).toEqual(["providers", "downloaded", "stage 1.2.0", "servers 1.2.0"]); + expect(result).toMatchObject({ localReady: true, localVersion: "1.2.0" }); + }); + it("still updates remote machines when the local download fails", async () => { const calls: string[] = []; const result = await runUpdateEverything({ - downloadLocal: async () => false, + downloadLocal: async () => null, updateProviders: async () => { calls.push("providers"); }, @@ -396,8 +492,8 @@ describe("runUpdateEverything", () => { await run; expect(calls).toEqual([ - "stage", "providers", + "stage", "providers done", "servers", "server fast", diff --git a/apps/web/src/components/sidebar/updateEverything.logic.ts b/apps/web/src/components/sidebar/updateEverything.logic.ts index 6930549adf19..b866033764f0 100644 --- a/apps/web/src/components/sidebar/updateEverything.logic.ts +++ b/apps/web/src/components/sidebar/updateEverything.logic.ts @@ -434,35 +434,61 @@ function trackSwitchMove( return { selfUpdate }; } +/** Servers move to the version this app downloaded, which can be newer than the offer the run was + * planned from. Only the confirmed machines update; one the new version would add waits for a later run. */ +export function retargetServerUpdates( + targets: ReadonlyArray, + localVersion: string | null | undefined, +): ReadonlyArray { + if (typeof localVersion !== "string") return targets; + return targets.map((target) => + target.targetVersion === localVersion ? target : { ...target, targetVersion: localVersion }, + ); +} + export interface UpdateEverythingSteps { - readonly downloadLocal?: () => Promise; + /** Resolves to the version this app downloaded, or null when the download failed. */ + readonly downloadLocal?: () => Promise; readonly updateProviders: () => Promise; - readonly stageServers?: () => ReadonlyMap>; + readonly stageServers?: ( + localVersion: string | null | undefined, + ) => ReadonlyMap>; readonly updateServers: ( staged: ReadonlyMap>, + localVersion: string | null | undefined, ) => Promise; } /** Finish provider installs before server restarts; servers stage meanwhile and each server's - * update waits only on its own stage. Install this app last when localReady; failed downloads skip - * only that install. */ + * update waits only on its own stage. The download checks the feed again, so servers stage and + * update only once it reports the version this app will run (undefined when it downloads nothing). + * Install this app last when localReady; failed downloads skip only that install. */ export async function runUpdateEverything( steps: UpdateEverythingSteps, ): Promise<{ readonly localReady: boolean; + readonly localVersion: string | null | undefined; readonly providers: Providers; readonly servers: Servers; }> { - const staged = new Map( - Array.from(steps.stageServers?.() ?? [], ([id, stage]) => [id, stage.catch(() => undefined)]), + const localVersion = steps.downloadLocal?.() ?? Promise.resolve(undefined); + const staged = localVersion.then( + (version) => + new Map( + Array.from(steps.stageServers?.(version) ?? [], ([id, stage]) => [ + id, + stage.catch(() => undefined), + ]), + ), ); - const [localReady, remote] = await Promise.all([ - steps.downloadLocal?.() ?? Promise.resolve(true), - steps - .updateProviders() - .then(async (providers) => ({ providers, servers: await steps.updateServers(staged) })), + const [version, remote] = await Promise.all([ + localVersion, + steps.updateProviders().then(async (providers) => ({ + providers, + servers: await steps.updateServers(await staged, await localVersion), + })), ]); - return { localReady, ...remote }; + return { localReady: version !== null, localVersion: version, ...remote }; } export type UpdateRunMachineStatus = @@ -512,6 +538,23 @@ export function isServerUpdateHandedOff(state: ServerUpdateState): boolean { return state.status === "running" && state.stage === "resuming"; } +/** Servers start updating once the local download reports its version, which can be well after + * providers finish; each server's reconnect deadline counts from here, toward its final target. */ +export function startServerPhase( + run: UpdateEverythingRun, + targets: ReadonlyArray, + startedAt: number, +): UpdateEverythingRun { + const targetsById = new Map(targets.map((target) => [target.environmentId, target])); + return { + ...run, + machines: run.machines.map((machine) => { + const server = targetsById.get(machine.environmentId); + return server === undefined ? machine : { ...machine, server, startedAt }; + }), + }; +} + export function closeFinishedUpdateRun(run: UpdateEverythingRun): UpdateEverythingRun | null { return run.phase === "servers" && run.localVersion === null && diff --git a/apps/web/src/components/sidebar/useUpdateEverything.ts b/apps/web/src/components/sidebar/useUpdateEverything.ts index 87c3a43a598e..316719ef7438 100644 --- a/apps/web/src/components/sidebar/useUpdateEverything.ts +++ b/apps/web/src/components/sidebar/useUpdateEverything.ts @@ -21,7 +21,9 @@ import { collectServerUpdateTargets, collectUpdateEverythingResult, describeUpdateEverything, + retargetServerUpdates, runUpdateEverything, + startServerPhase, type UpdateRunMachine, } from "./updateEverything.logic"; import { @@ -108,7 +110,8 @@ export function useUpdateEverything( downloadLocal, installLocal, }: { - downloadLocal?: (reportError: ReportLocalUpdateError) => Promise; + /** Resolves to the version this app downloaded, or null when it could not. */ + downloadLocal?: (reportError: ReportLocalUpdateError) => Promise; installLocal?: (reportError: ReportLocalUpdateError) => Promise; }) => { const localFailures: Array = []; @@ -152,6 +155,9 @@ export function useUpdateEverything( restartAt: null, machines: Array.from(machinesById.values()), })); + // A track switch keeps the release its database check covered. + const serversFor = (version: string | null | undefined) => + channelSwitch ? servers : retargetServerUpdates(servers, version); const providerFailures = new Map(); const finishedServers: Array<{ label: string; outcome: ServerUpdateOutcome }> = []; try { @@ -179,24 +185,14 @@ export function useUpdateEverything( ); } } - const serversStartedAt = Date.now(); - setUpdateEverythingRun( - (run) => - run && { - ...run, - phase: "servers", - machines: run.machines.map((machine) => - machine.server === null ? machine : { ...machine, startedAt: serversStartedAt }, - ), - }, - ); + setUpdateEverythingRun((run) => run && { ...run, phase: "servers" }); return outcomes; }, ...(providers.machines.length > 0 ? { - stageServers: () => + stageServers: (version) => new Map( - servers + serversFor(version) .filter( (server) => machines.find((machine) => machine.environmentId === server.environmentId) @@ -212,8 +208,13 @@ export function useUpdateEverything( ), } : {}), - updateServers: async (staged) => { - const runs = servers.map(async (server) => { + updateServers: async (staged, version) => { + const targets = serversFor(version); + const serversStartedAt = Date.now(); + setUpdateEverythingRun( + (run) => run && startServerPhase(run, targets, serversStartedAt), + ); + const runs = targets.map(async (server) => { await staged.get(server.environmentId); const serverOutcome = await updateServer(server, { quiet: true }); settleUpdateRunMachine( @@ -231,7 +232,7 @@ export function useUpdateEverything( return finished; }); await Promise.all( - servers.map((server, index) => + targets.map((server, index) => waitForServerUpdateHandoff(server.environmentId, runs[index]!), ), ); @@ -250,7 +251,10 @@ export function useUpdateEverything( setUpdateEverythingRun( (run) => run && { ...run, phase: "installing", restartAt: null }, ); - saveUpdateEverythingResult({ ...result(finishedServers), localVersion }); + saveUpdateEverythingResult({ + ...result(finishedServers), + localVersion: outcome.localVersion ?? localVersion, + }); if (await installLocal(reportLocalError)) return; clearSavedUpdateEverythingResult(); } From 43ebc9140601c768f8c6ff9e041ef904f44c49a6 Mon Sep 17 00:00:00 2001 From: bompus Date: Mon, 23 Nov 2026 10:17:35 -0700 Subject: [PATCH 2/4] fix(web): servers follow the desktop download's version as soon as it starts Waiting for the whole desktop download held every server update behind it: a stalled download blocked servers and kept the update controls locked, the capsule showed no download progress, and staging lost its overlap with provider installs. The desktop download checks the feed before it starts, so the version is final once it reports downloading. Servers now take that version, or the planned one if the download settles without starting or does not start within a minute. The run shows the new version while it is known. --- .../components/desktopUpdate.logic.test.ts | 46 +++++++++++++++++++ .../web/src/components/desktopUpdate.logic.ts | 29 ++++++++++++ .../components/sidebar/SidebarUpdatePill.tsx | 24 +++++++--- .../sidebar/updateEverything.logic.test.ts | 42 +++++++++++------ .../sidebar/updateEverything.logic.ts | 38 +++++++-------- .../components/sidebar/useUpdateEverything.ts | 26 ++++++++--- 6 files changed, 160 insertions(+), 45 deletions(-) diff --git a/apps/web/src/components/desktopUpdate.logic.test.ts b/apps/web/src/components/desktopUpdate.logic.test.ts index 601527e51a20..a03361fbdf8a 100644 --- a/apps/web/src/components/desktopUpdate.logic.test.ts +++ b/apps/web/src/components/desktopUpdate.logic.test.ts @@ -9,6 +9,7 @@ import { getDesktopUpdateReleaseUrl, installDesktopUpdate, resolveDesktopUpdateButtonAction, + watchDesktopDownloadVersion, } from "./desktopUpdate.logic"; const baseState: DesktopUpdateState = { @@ -177,3 +178,48 @@ describe("canCheckForUpdate", () => { ).toBe(true); }); }); + +describe("watchDesktopDownloadVersion", () => { + function updateStates() { + const listeners = new Set<(state: DesktopUpdateState) => void>(); + return { + bridge: { + onUpdateState: (listener: (state: DesktopUpdateState) => void) => { + listeners.add(listener); + return () => listeners.delete(listener); + }, + }, + emit: (state: DesktopUpdateState) => listeners.forEach((listener) => listener(state)), + listening: () => listeners.size, + }; + } + const offered: DesktopUpdateState = { + ...baseState, + status: "available", + availableVersion: "1.1.0", + }; + + it("reports the release the download started on, not the offer before its check", async () => { + const states = updateStates(); + const version = watchDesktopDownloadVersion(states.bridge, new Promise(() => {})); + states.emit(offered); + states.emit({ ...offered, availableVersion: "1.2.0" }); + states.emit({ ...offered, status: "downloading", availableVersion: "1.2.0" }); + + await expect(version).resolves.toBe("1.2.0"); + expect(states.listening()).toBe(0); + }); + + it("gives up when the download settles without starting or never starts", async () => { + const refused = updateStates(); + await expect( + watchDesktopDownloadVersion(refused.bridge, Promise.resolve(false)), + ).resolves.toBeUndefined(); + + const stalled = updateStates(); + await expect( + watchDesktopDownloadVersion(stalled.bridge, new Promise(() => {}), 1), + ).resolves.toBeUndefined(); + expect(stalled.listening()).toBe(0); + }); +}); diff --git a/apps/web/src/components/desktopUpdate.logic.ts b/apps/web/src/components/desktopUpdate.logic.ts index 4922ee3b524b..eb1e57c71dba 100644 --- a/apps/web/src/components/desktopUpdate.logic.ts +++ b/apps/web/src/components/desktopUpdate.logic.ts @@ -249,6 +249,35 @@ export async function installDesktopUpdate( } } +/** + * The version a desktop download fetches. The download checks the feed again before it starts, so + * this can be newer than the offer it began from; it is known once the download starts. Undefined + * when the download settles without starting, or does not start within `timeoutMs`. + */ +export function watchDesktopDownloadVersion( + bridge: Pick, + download: Promise, + timeoutMs = 60_000, +): Promise { + return new Promise((resolve) => { + let settled = false; + const finish = (version: string | undefined) => { + if (settled) return; + settled = true; + unsubscribe(); + clearTimeout(timer); + resolve(version); + }; + const unsubscribe = bridge.onUpdateState((state) => { + if (state.status === "downloading" || state.status === "downloaded") { + finish(getDesktopUpdateDownloadedVersion(state) ?? undefined); + } + }); + const timer = setTimeout(() => finish(undefined), timeoutMs); + void download.finally(() => finish(undefined)); + }); +} + export function shouldToastDesktopUpdateActionResult(result: DesktopUpdateActionResult): boolean { return getDesktopUpdateActionError(result) !== null; } diff --git a/apps/web/src/components/sidebar/SidebarUpdatePill.tsx b/apps/web/src/components/sidebar/SidebarUpdatePill.tsx index ef38fa78974c..1b2655b58852 100644 --- a/apps/web/src/components/sidebar/SidebarUpdatePill.tsx +++ b/apps/web/src/components/sidebar/SidebarUpdatePill.tsx @@ -27,6 +27,7 @@ import { resolveDesktopUpdateButtonAction, shouldShowArm64IntelBuildWarning, shouldToastDesktopUpdateActionResult, + watchDesktopDownloadVersion, } from "../desktopUpdate.logic"; import { showDesktopUpdateDownloadedToast } from "../desktopUpdate.toast"; import { useCheckProviderUpdates } from "../ProviderUpdatesAction"; @@ -151,7 +152,7 @@ export async function downloadLocalUpdate( notifyDownloaded, reportError = toastUpdateError, }: { readonly notifyDownloaded: boolean; readonly reportError?: ReportLocalUpdateError }, -): Promise { +): Promise { try { const result = await bridge.downloadUpdate(); if (result.completed && notifyDownloaded) { @@ -161,14 +162,13 @@ export async function downloadLocalUpdate( ? getDesktopUpdateActionError(result) : null; if (actionError) reportError(m(msg("Could not download update")), actionError); - // The download checks the feed again, so this can be newer than the offer it started from. - return result.completed ? getDesktopUpdateDownloadedVersion(result.state) : null; + return result.completed; } catch (error) { reportError( m(msg("Could not start update download")), thrownErrorMessage(error, m) ?? m(msg("An unexpected error occurred.")), ); - return null; + return false; } } @@ -376,12 +376,24 @@ function SidebarUpdateControl() { const bridge = window.desktopBridge; setIsConfirmingEverything(false); setIsActionPending(true); + // Servers follow the build the download fetches, known once it starts. + let startServerTarget: (target: Promise) => void = () => {}; + const serverTarget = new Promise((resolve) => { + startServerTarget = resolve; + }); const result = await settlePromise(() => everything.run({ ...(bridge && action === "download" ? { - downloadLocal: (reportError) => - downloadLocalUpdate(bridge, { notifyDownloaded: false, reportError }), + downloadLocal: (reportError) => { + const download = downloadLocalUpdate(bridge, { + notifyDownloaded: false, + reportError, + }); + startServerTarget(watchDesktopDownloadVersion(bridge, download)); + return download; + }, + serverTarget, } : {}), ...(bridge && action !== "none" diff --git a/apps/web/src/components/sidebar/updateEverything.logic.test.ts b/apps/web/src/components/sidebar/updateEverything.logic.test.ts index 1c41d574df44..d50a6c731e42 100644 --- a/apps/web/src/components/sidebar/updateEverything.logic.test.ts +++ b/apps/web/src/components/sidebar/updateEverything.logic.test.ts @@ -364,8 +364,7 @@ describe("retargetServerUpdates", () => { ]); }); - it("keeps the planned targets when this app downloaded nothing", () => { - expect(retargetServerUpdates(targets, null)).toBe(targets); + it("keeps the planned targets when the download version is unknown", () => { expect(retargetServerUpdates(targets, undefined)).toBe(targets); }); }); @@ -415,36 +414,51 @@ describe("startServerPhase", () => { }); describe("runUpdateEverything", () => { - it("stages and updates servers with the version the local download reports", async () => { + it("stages and updates servers with the version the local download fetches", async () => { const calls: string[] = []; - let finishDownload = (_version: string) => {}; - const download = new Promise((resolve) => (finishDownload = resolve)); + let knowTarget = (_version: string) => {}; + let finishDownload = () => {}; + let serversStarted = () => {}; + const serversStart = new Promise((resolve) => (serversStarted = resolve)); const run = runUpdateEverything({ - downloadLocal: () => download, + downloadLocal: () => + new Promise((resolve) => (finishDownload = () => resolve(true))), + serverTarget: new Promise((resolve) => (knowTarget = resolve)), updateProviders: async () => { calls.push("providers"); }, - stageServers: (version) => { - calls.push(`stage ${version}`); + stageServers: (target) => { + calls.push(`stage ${target}`); return new Map(); }, - updateServers: async (_staged, version) => { - calls.push(`servers ${version}`); + updateServers: async (_staged, target) => { + calls.push(`servers ${target}`); + serversStarted(); }, }); await Promise.resolve(); + calls.push("download started"); + knowTarget("1.2.0"); + // Servers do not wait for the download to finish. + await serversStart; calls.push("downloaded"); - finishDownload("1.2.0"); + finishDownload(); const result = await run; - expect(calls).toEqual(["providers", "downloaded", "stage 1.2.0", "servers 1.2.0"]); - expect(result).toMatchObject({ localReady: true, localVersion: "1.2.0" }); + expect(calls).toEqual([ + "providers", + "download started", + "stage 1.2.0", + "servers 1.2.0", + "downloaded", + ]); + expect(result.localReady).toBe(true); }); it("still updates remote machines when the local download fails", async () => { const calls: string[] = []; const result = await runUpdateEverything({ - downloadLocal: async () => null, + downloadLocal: async () => false, updateProviders: async () => { calls.push("providers"); }, diff --git a/apps/web/src/components/sidebar/updateEverything.logic.ts b/apps/web/src/components/sidebar/updateEverything.logic.ts index b866033764f0..9b12f9bd8a40 100644 --- a/apps/web/src/components/sidebar/updateEverything.logic.ts +++ b/apps/web/src/components/sidebar/updateEverything.logic.ts @@ -438,57 +438,57 @@ function trackSwitchMove( * planned from. Only the confirmed machines update; one the new version would add waits for a later run. */ export function retargetServerUpdates( targets: ReadonlyArray, - localVersion: string | null | undefined, + localVersion: string | undefined, ): ReadonlyArray { - if (typeof localVersion !== "string") return targets; + if (localVersion === undefined) return targets; return targets.map((target) => target.targetVersion === localVersion ? target : { ...target, targetVersion: localVersion }, ); } export interface UpdateEverythingSteps { - /** Resolves to the version this app downloaded, or null when the download failed. */ - readonly downloadLocal?: () => Promise; + readonly downloadLocal?: () => Promise; + /** The version servers move to, once the local download knows it; undefined keeps the plan. */ + readonly serverTarget?: Promise; readonly updateProviders: () => Promise; readonly stageServers?: ( - localVersion: string | null | undefined, + serverTarget: string | undefined, ) => ReadonlyMap>; readonly updateServers: ( staged: ReadonlyMap>, - localVersion: string | null | undefined, + serverTarget: string | undefined, ) => Promise; } /** Finish provider installs before server restarts; servers stage meanwhile and each server's - * update waits only on its own stage. The download checks the feed again, so servers stage and - * update only once it reports the version this app will run (undefined when it downloads nothing). + * update waits only on its own stage. Servers stage once `serverTarget` settles, which the local + * download resolves after its own feed check, so they get the build this app downloads. * Install this app last when localReady; failed downloads skip only that install. */ export async function runUpdateEverything( steps: UpdateEverythingSteps, ): Promise<{ readonly localReady: boolean; - readonly localVersion: string | null | undefined; readonly providers: Providers; readonly servers: Servers; }> { - const localVersion = steps.downloadLocal?.() ?? Promise.resolve(undefined); - const staged = localVersion.then( - (version) => + const serverTarget = steps.serverTarget ?? Promise.resolve(undefined); + const staged = serverTarget.then( + (target) => new Map( - Array.from(steps.stageServers?.(version) ?? [], ([id, stage]) => [ + Array.from(steps.stageServers?.(target) ?? [], ([id, stage]) => [ id, stage.catch(() => undefined), ]), ), ); - const [version, remote] = await Promise.all([ - localVersion, + const [localReady, remote] = await Promise.all([ + steps.downloadLocal?.() ?? Promise.resolve(true), steps.updateProviders().then(async (providers) => ({ providers, - servers: await steps.updateServers(await staged, await localVersion), + servers: await steps.updateServers(await staged, await serverTarget), })), ]); - return { localReady: version !== null, localVersion: version, ...remote }; + return { localReady, ...remote }; } export type UpdateRunMachineStatus = @@ -538,8 +538,8 @@ export function isServerUpdateHandedOff(state: ServerUpdateState): boolean { return state.status === "running" && state.stage === "resuming"; } -/** Servers start updating once the local download reports its version, which can be well after - * providers finish; each server's reconnect deadline counts from here, toward its final target. */ +/** Servers start updating after providers finish and the local download's version is known, so + * each server's reconnect deadline counts from here, toward its final target. */ export function startServerPhase( run: UpdateEverythingRun, targets: ReadonlyArray, diff --git a/apps/web/src/components/sidebar/useUpdateEverything.ts b/apps/web/src/components/sidebar/useUpdateEverything.ts index 316719ef7438..80a0ecb6f245 100644 --- a/apps/web/src/components/sidebar/useUpdateEverything.ts +++ b/apps/web/src/components/sidebar/useUpdateEverything.ts @@ -108,10 +108,12 @@ export function useUpdateEverything( restarts, run: async ({ downloadLocal, + serverTarget, installLocal, }: { - /** Resolves to the version this app downloaded, or null when it could not. */ - downloadLocal?: (reportError: ReportLocalUpdateError) => Promise; + downloadLocal?: (reportError: ReportLocalUpdateError) => Promise; + /** The version the local download fetches, known once it starts; see runUpdateEverything. */ + serverTarget?: Promise; installLocal?: (reportError: ReportLocalUpdateError) => Promise; }) => { const localFailures: Array = []; @@ -148,6 +150,7 @@ export function useUpdateEverything( }); } const localVersion = installLocal ? localUpdateVersion : null; + let downloadedVersion = localVersion; setUpdateEverythingRun(() => ({ ...(channelSwitch ? { trackSwitch: true } : {}), localVersion, @@ -156,13 +159,24 @@ export function useUpdateEverything( machines: Array.from(machinesById.values()), })); // A track switch keeps the release its database check covered. - const serversFor = (version: string | null | undefined) => + const serversFor = (version: string | undefined) => channelSwitch ? servers : retargetServerUpdates(servers, version); const providerFailures = new Map(); const finishedServers: Array<{ label: string; outcome: ServerUpdateOutcome }> = []; try { const outcome = await runUpdateEverything({ ...(downloadLocal ? { downloadLocal: () => downloadLocal(reportLocalError) } : {}), + ...(serverTarget && !channelSwitch + ? { + serverTarget: serverTarget.then((target) => { + if (target !== undefined && localVersion !== null) { + downloadedVersion = target; + setUpdateEverythingRun((run) => run && { ...run, localVersion: target }); + } + return target; + }), + } + : {}), updateProviders: async () => { const runs = await providers.updateAll({ quiet: true }); const outcomes = runs.map((run) => { @@ -208,8 +222,8 @@ export function useUpdateEverything( ), } : {}), - updateServers: async (staged, version) => { - const targets = serversFor(version); + updateServers: async (staged, target) => { + const targets = serversFor(target); const serversStartedAt = Date.now(); setUpdateEverythingRun( (run) => run && startServerPhase(run, targets, serversStartedAt), @@ -253,7 +267,7 @@ export function useUpdateEverything( ); saveUpdateEverythingResult({ ...result(finishedServers), - localVersion: outcome.localVersion ?? localVersion, + localVersion: downloadedVersion, }); if (await installLocal(reportLocalError)) return; clearSavedUpdateEverythingResult(); From c5fea53dd9de1473b79f3fc0162df83951c3cbfb Mon Sep 17 00:00:00 2001 From: bompus Date: Mon, 23 Nov 2026 10:23:55 -0700 Subject: [PATCH 3/4] fix(web): skip server staging once providers have settled Staging overlaps provider installs. When providers settle before the download's version is known, a stage-only request would only delay the server update and hide its progress, so servers update directly. --- .../sidebar/updateEverything.logic.test.ts | 54 ++++++++++++++----- .../sidebar/updateEverything.logic.ts | 18 ++++--- 2 files changed, 52 insertions(+), 20 deletions(-) diff --git a/apps/web/src/components/sidebar/updateEverything.logic.test.ts b/apps/web/src/components/sidebar/updateEverything.logic.test.ts index d50a6c731e42..db304a1cfd49 100644 --- a/apps/web/src/components/sidebar/updateEverything.logic.test.ts +++ b/apps/web/src/components/sidebar/updateEverything.logic.test.ts @@ -417,44 +417,70 @@ describe("runUpdateEverything", () => { it("stages and updates servers with the version the local download fetches", async () => { const calls: string[] = []; let knowTarget = (_version: string) => {}; + let finishProviders = () => {}; let finishDownload = () => {}; - let serversStarted = () => {}; - const serversStart = new Promise((resolve) => (serversStarted = resolve)); + let markStaged = () => {}; + let markServersStarted = () => {}; + const staging = new Promise((resolve) => (markStaged = resolve)); + const serversStarted = new Promise((resolve) => (markServersStarted = resolve)); const run = runUpdateEverything({ downloadLocal: () => new Promise((resolve) => (finishDownload = () => resolve(true))), serverTarget: new Promise((resolve) => (knowTarget = resolve)), - updateProviders: async () => { + updateProviders: () => { calls.push("providers"); + return new Promise((resolve) => (finishProviders = resolve)); }, stageServers: (target) => { calls.push(`stage ${target}`); + markStaged(); return new Map(); }, updateServers: async (_staged, target) => { calls.push(`servers ${target}`); - serversStarted(); + markServersStarted(); }, }); - await Promise.resolve(); - calls.push("download started"); knowTarget("1.2.0"); + await staging; + finishProviders(); // Servers do not wait for the download to finish. - await serversStart; + await serversStarted; calls.push("downloaded"); finishDownload(); const result = await run; - expect(calls).toEqual([ - "providers", - "download started", - "stage 1.2.0", - "servers 1.2.0", - "downloaded", - ]); + expect(calls).toEqual(["providers", "stage 1.2.0", "servers 1.2.0", "downloaded"]); expect(result.localReady).toBe(true); }); + it("skips staging when providers finish before the download's version is known", async () => { + const calls: string[] = []; + let knowTarget = (_version: string) => {}; + let providersDone = () => {}; + const providersFinished = new Promise((resolve) => (providersDone = resolve)); + const run = runUpdateEverything({ + downloadLocal: async () => true, + serverTarget: new Promise((resolve) => (knowTarget = resolve)), + updateProviders: async () => { + calls.push("providers"); + providersDone(); + }, + stageServers: (target) => { + calls.push(`stage ${target}`); + return new Map(); + }, + updateServers: async (staged, target) => { + calls.push(`servers ${target} staged ${staged.size}`); + }, + }); + await providersFinished; + knowTarget("1.2.0"); + await run; + + expect(calls).toEqual(["providers", "servers 1.2.0 staged 0"]); + }); + it("still updates remote machines when the local download fails", async () => { const calls: string[] = []; const result = await runUpdateEverything({ diff --git a/apps/web/src/components/sidebar/updateEverything.logic.ts b/apps/web/src/components/sidebar/updateEverything.logic.ts index 9b12f9bd8a40..c35b2f5b2593 100644 --- a/apps/web/src/components/sidebar/updateEverything.logic.ts +++ b/apps/web/src/components/sidebar/updateEverything.logic.ts @@ -472,18 +472,24 @@ export async function runUpdateEverything( readonly servers: Servers; }> { const serverTarget = steps.serverTarget ?? Promise.resolve(undefined); + let providersRunning = true; + const local = steps.downloadLocal?.() ?? Promise.resolve(true); + const providers = steps.updateProviders().finally(() => { + providersRunning = false; + }); + // Staging only helps while providers install; once they settle, servers update directly. const staged = serverTarget.then( (target) => new Map( - Array.from(steps.stageServers?.(target) ?? [], ([id, stage]) => [ - id, - stage.catch(() => undefined), - ]), + Array.from( + (providersRunning ? steps.stageServers?.(target) : undefined) ?? [], + ([id, stage]) => [id, stage.catch(() => undefined)], + ), ), ); const [localReady, remote] = await Promise.all([ - steps.downloadLocal?.() ?? Promise.resolve(true), - steps.updateProviders().then(async (providers) => ({ + local, + providers.then(async (providers) => ({ providers, servers: await steps.updateServers(await staged, await serverTarget), })), From bf2b50b17c0d690a1f11e9f9723cecd78b6acb4b Mon Sep 17 00:00:00 2001 From: bompus Date: Mon, 23 Nov 2026 10:26:29 -0700 Subject: [PATCH 4/4] fix(web): servers never move below their planned target; save the downloaded version A stale feed can serve an older build than planned, which a server may already run and would refuse, so retargeting only moves servers up. The saved run result now takes the version the desktop download finished with, so the launch notice counts the app as updated even when servers kept the planned target because the download started late. --- .../components/sidebar/SidebarUpdatePill.tsx | 7 +++-- .../sidebar/updateEverything.logic.test.ts | 28 ++++++++++++++++--- .../sidebar/updateEverything.logic.ts | 16 +++++++---- .../components/sidebar/useUpdateEverything.ts | 6 ++-- 4 files changed, 43 insertions(+), 14 deletions(-) diff --git a/apps/web/src/components/sidebar/SidebarUpdatePill.tsx b/apps/web/src/components/sidebar/SidebarUpdatePill.tsx index 1b2655b58852..1094dd771b20 100644 --- a/apps/web/src/components/sidebar/SidebarUpdatePill.tsx +++ b/apps/web/src/components/sidebar/SidebarUpdatePill.tsx @@ -152,7 +152,7 @@ export async function downloadLocalUpdate( notifyDownloaded, reportError = toastUpdateError, }: { readonly notifyDownloaded: boolean; readonly reportError?: ReportLocalUpdateError }, -): Promise { +): Promise { try { const result = await bridge.downloadUpdate(); if (result.completed && notifyDownloaded) { @@ -162,13 +162,14 @@ export async function downloadLocalUpdate( ? getDesktopUpdateActionError(result) : null; if (actionError) reportError(m(msg("Could not download update")), actionError); - return result.completed; + // The download checks the feed again, so this can be newer than the offer it started from. + return result.completed ? getDesktopUpdateDownloadedVersion(result.state) : null; } catch (error) { reportError( m(msg("Could not start update download")), thrownErrorMessage(error, m) ?? m(msg("An unexpected error occurred.")), ); - return false; + return null; } } diff --git a/apps/web/src/components/sidebar/updateEverything.logic.test.ts b/apps/web/src/components/sidebar/updateEverything.logic.test.ts index db304a1cfd49..859a52fff2e8 100644 --- a/apps/web/src/components/sidebar/updateEverything.logic.test.ts +++ b/apps/web/src/components/sidebar/updateEverything.logic.test.ts @@ -364,6 +364,10 @@ describe("retargetServerUpdates", () => { ]); }); + it("never moves a server below its planned target", () => { + expect(retargetServerUpdates(targets, "1.0.5")).toEqual(targets); + }); + it("keeps the planned targets when the download version is unknown", () => { expect(retargetServerUpdates(targets, undefined)).toBe(targets); }); @@ -425,7 +429,7 @@ describe("runUpdateEverything", () => { const serversStarted = new Promise((resolve) => (markServersStarted = resolve)); const run = runUpdateEverything({ downloadLocal: () => - new Promise((resolve) => (finishDownload = () => resolve(true))), + new Promise((resolve) => (finishDownload = () => resolve("1.2.0"))), serverTarget: new Promise((resolve) => (knowTarget = resolve)), updateProviders: () => { calls.push("providers"); @@ -451,7 +455,7 @@ describe("runUpdateEverything", () => { const result = await run; expect(calls).toEqual(["providers", "stage 1.2.0", "servers 1.2.0", "downloaded"]); - expect(result.localReady).toBe(true); + expect(result).toMatchObject({ localReady: true, localVersion: "1.2.0" }); }); it("skips staging when providers finish before the download's version is known", async () => { @@ -460,7 +464,7 @@ describe("runUpdateEverything", () => { let providersDone = () => {}; const providersFinished = new Promise((resolve) => (providersDone = resolve)); const run = runUpdateEverything({ - downloadLocal: async () => true, + downloadLocal: async () => "1.2.0", serverTarget: new Promise((resolve) => (knowTarget = resolve)), updateProviders: async () => { calls.push("providers"); @@ -481,10 +485,26 @@ describe("runUpdateEverything", () => { expect(calls).toEqual(["providers", "servers 1.2.0 staged 0"]); }); + it("reports the downloaded version even when servers kept the planned target", async () => { + const targets: Array = []; + const result = await runUpdateEverything({ + downloadLocal: async () => "1.2.0", + // The download did not start in time, so servers keep their planned target. + serverTarget: Promise.resolve(undefined), + updateProviders: async () => {}, + updateServers: async (_staged, target) => { + targets.push(target); + }, + }); + + expect(targets).toEqual([undefined]); + expect(result).toMatchObject({ localReady: true, localVersion: "1.2.0" }); + }); + it("still updates remote machines when the local download fails", async () => { const calls: string[] = []; const result = await runUpdateEverything({ - downloadLocal: async () => false, + downloadLocal: async () => null, updateProviders: async () => { calls.push("providers"); }, diff --git a/apps/web/src/components/sidebar/updateEverything.logic.ts b/apps/web/src/components/sidebar/updateEverything.logic.ts index c35b2f5b2593..a5e2c240f52a 100644 --- a/apps/web/src/components/sidebar/updateEverything.logic.ts +++ b/apps/web/src/components/sidebar/updateEverything.logic.ts @@ -441,13 +441,17 @@ export function retargetServerUpdates( localVersion: string | undefined, ): ReadonlyArray { if (localVersion === undefined) return targets; + // A stale feed can serve an older build than planned, which a server may already run; only move up. return targets.map((target) => - target.targetVersion === localVersion ? target : { ...target, targetVersion: localVersion }, + compareSemverVersions(localVersion, target.targetVersion) > 0 + ? { ...target, targetVersion: localVersion } + : target, ); } export interface UpdateEverythingSteps { - readonly downloadLocal?: () => Promise; + /** Resolves to the version this app downloaded, or null when the download failed. */ + readonly downloadLocal?: () => Promise; /** The version servers move to, once the local download knows it; undefined keeps the plan. */ readonly serverTarget?: Promise; readonly updateProviders: () => Promise; @@ -468,12 +472,14 @@ export async function runUpdateEverything( steps: UpdateEverythingSteps, ): Promise<{ readonly localReady: boolean; + /** The version this app downloaded; undefined when the run downloads nothing. */ + readonly localVersion: string | undefined; readonly providers: Providers; readonly servers: Servers; }> { const serverTarget = steps.serverTarget ?? Promise.resolve(undefined); let providersRunning = true; - const local = steps.downloadLocal?.() ?? Promise.resolve(true); + const local = steps.downloadLocal?.() ?? Promise.resolve(undefined); const providers = steps.updateProviders().finally(() => { providersRunning = false; }); @@ -487,14 +493,14 @@ export async function runUpdateEverything( ), ), ); - const [localReady, remote] = await Promise.all([ + const [localVersion, remote] = await Promise.all([ local, providers.then(async (providers) => ({ providers, servers: await steps.updateServers(await staged, await serverTarget), })), ]); - return { localReady, ...remote }; + return { localReady: localVersion !== null, localVersion: localVersion ?? undefined, ...remote }; } export type UpdateRunMachineStatus = diff --git a/apps/web/src/components/sidebar/useUpdateEverything.ts b/apps/web/src/components/sidebar/useUpdateEverything.ts index 80a0ecb6f245..d73efbce28e5 100644 --- a/apps/web/src/components/sidebar/useUpdateEverything.ts +++ b/apps/web/src/components/sidebar/useUpdateEverything.ts @@ -111,7 +111,8 @@ export function useUpdateEverything( serverTarget, installLocal, }: { - downloadLocal?: (reportError: ReportLocalUpdateError) => Promise; + /** Resolves to the version this app downloaded, or null when it could not. */ + downloadLocal?: (reportError: ReportLocalUpdateError) => Promise; /** The version the local download fetches, known once it starts; see runUpdateEverything. */ serverTarget?: Promise; installLocal?: (reportError: ReportLocalUpdateError) => Promise; @@ -267,7 +268,8 @@ export function useUpdateEverything( ); saveUpdateEverythingResult({ ...result(finishedServers), - localVersion: downloadedVersion, + // The download can finish after the server target gave up waiting for it. + localVersion: outcome.localVersion ?? downloadedVersion, }); if (await installLocal(reportLocalError)) return; clearSavedUpdateEverythingResult();