diff --git a/packages/desktop/src/main/updater-controller.test.ts b/packages/desktop/src/main/updater-controller.test.ts index 2d43bee9bc50..5c2d0f5ea806 100644 --- a/packages/desktop/src/main/updater-controller.test.ts +++ b/packages/desktop/src/main/updater-controller.test.ts @@ -2,6 +2,7 @@ import { describe, expect, test } from "bun:test" import { createUpdaterController, LAUNCH_WINDOW, + supportsMajor, type MajorUpdaterBackend, type UpdaterBackend, type UpdaterReadyRecord, @@ -272,6 +273,66 @@ describe("major updater", () => { expect(app.controller.major.getState()).toEqual({ status: "ready", quit: true }) }) + test("a download that keeps failing stops until the user checks for updates", async () => { + const failure = { fail: true } + const app = setupMajor({ + download: async () => { + if (failure.fail) throw new Error("sha512 checksum mismatch") + }, + }) + await app.offer() + await app.controller.check() + await app.settled() + await app.controller.check() + await app.settled() + expect(app.calls.filter((call) => call === "major download")).toHaveLength(3) + + await app.controller.check() + await app.settled() + await app.controller.check() + await app.settled() + + expect(app.calls.filter((call) => call === "major download")).toHaveLength(3) + expect(app.calls.filter((call) => call === "major check")).toHaveLength(3) + expect(app.controller.major.getState()).toEqual({ status: "unavailable" }) + expect(app.updates).not.toContain("error") + + failure.fail = false + await app.controller.checkNow() + + expect(app.controller.major.getState()).toEqual({ status: "ready", quit: true }) + }) + + test("a feed that cannot be reached is not counted as a failed download", async () => { + const feed = { down: true } + const app = setupMajor({ + feed: () => { + if (feed.down) throw new Error("net::ERR_INTERNET_DISCONNECTED") + return { isUpdateAvailable: true, updateInfo: { version: "2.2.7" } } + }, + }) + await app.offer() + for (const _ of Array.from({ length: 5 })) { + await app.controller.check() + await app.settled() + } + expect(app.calls).not.toContain("major download") + + feed.down = false + await app.controller.check() + await app.settled() + + expect(app.controller.major.getState()).toEqual({ status: "ready", quit: true }) + }) + + test("only offers OpenCode 2 where it can run", () => { + expect(supportsMajor("darwin", "12.7.6")).toBe(false) + expect(supportsMajor("darwin", "13.0")).toBe(true) + expect(supportsMajor("darwin", "27.0.1")).toBe(true) + expect(supportsMajor("win32", "10.0.19045")).toBe(true) + expect(supportsMajor("linux", "6.8.0")).toBe(true) + }) + test("stays unavailable when the feed has no OpenCode 2 release", async () => { const app = setupMajor({ feed: () => ({ isUpdateAvailable: false }) }) diff --git a/packages/desktop/src/main/updater-controller.ts b/packages/desktop/src/main/updater-controller.ts index 6d537b75890b..6398e443a5fd 100644 --- a/packages/desktop/src/main/updater-controller.ts +++ b/packages/desktop/src/main/updater-controller.ts @@ -10,6 +10,19 @@ export const LAUNCH_WINDOW = 30 * 1000 // OpenCode 1 only offers the next major release. A feed that answers with anything older is ignored. const MAJOR_VERSION = 2 +// OpenCode 2 runs on macOS 13 and later, and its feed does not say so. On an older macOS it would install and then +// not start, with OpenCode 1 gone. +const MAJOR_MACOS = 13 + +// A download that keeps failing would otherwise start again at every 10 minute check, about 300 MB each time. After +// this many failures the app waits for its next launch, or for the user to check for updates. +const MAJOR_ATTEMPTS = 3 + +export function supportsMajor(platform: string, system: string) { + if (platform !== "darwin") return true + return Number(system.split(".")[0]) >= MAJOR_MACOS +} + export type UpdaterBackend = { checkForUpdates(): Promise<{ isUpdateAvailable?: boolean; updateInfo?: { version?: string } } | null | undefined> downloadUpdate(): Promise @@ -58,6 +71,7 @@ export function createUpdaterController(input: { let installing: Promise | undefined let prompted = false let asked = false + let failures = 0 const listeners = new Set<(state: UpdaterState) => void>() const majorListeners = new Set<(state: MajorUpdateState) => void>() @@ -82,7 +96,7 @@ export function createUpdaterController(input: { // check tries again, because the user has not asked for anything yet. const prepare = () => { const backend = input.major - if (!backend || preparing || major.status !== "unavailable") return + if (!backend || preparing || major.status !== "unavailable" || failures >= MAJOR_ATTEMPTS) return transitionMajor({ status: "downloading" }) preparing = backend .checkForUpdates() @@ -90,7 +104,13 @@ export function createUpdaterController(input: { if (!result?.isUpdateAvailable || !isMajor(result.updateInfo?.version)) return false // A newer OpenCode 1 release can withdraw the offer while the feed answers. if (major.status !== "downloading") return false - await backend.downloadUpdate() + await backend.downloadUpdate().catch((error) => { + // An OpenCode 1 release that withdraws the offer cancels the download. Only a download that failed by itself + // counts. + if (major.status === "downloading") failures++ + throw error + }) + failures = 0 return true }) .catch((error) => { @@ -168,6 +188,8 @@ export function createUpdaterController(input: { // A check the user asked for. It also waits for OpenCode 2 to finish downloading, so the answer is never "up to // date" while the upgrade is still on its way. async checkNow() { + // The user asked, so a download that gave up gets another try. + failures = 0 await check() await preparing return state diff --git a/packages/desktop/src/main/updater.ts b/packages/desktop/src/main/updater.ts index 241427654488..ba2d4627a00f 100644 --- a/packages/desktop/src/main/updater.ts +++ b/packages/desktop/src/main/updater.ts @@ -3,6 +3,7 @@ import pkg, { type AppUpdater, type CancellationToken } from "electron-updater" import { CHANNEL, UPDATER_ENABLED } from "./constants" import { createUpdaterController, + supportsMajor, type MajorUpdaterBackend, type UpdaterBackend, type UpdaterReadyRecord, @@ -159,7 +160,10 @@ export function setupAutoUpdater(stop: () => Promise) { currentVersion: app.getVersion(), backend, // Only the release channel ships OpenCode 1 desktop builds. - major: UPDATER_ENABLED && CHANNEL === "prod" ? majorBackend() : undefined, + major: + UPDATER_ENABLED && CHANNEL === "prod" && supportsMajor(process.platform, process.getSystemVersion()) + ? majorBackend() + : undefined, persistence: { get() { const value = store.get(key)