Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions packages/desktop/src/main/updater-controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { describe, expect, test } from "bun:test"
import {
createUpdaterController,
LAUNCH_WINDOW,
supportsMajor,
type MajorUpdaterBackend,
type UpdaterBackend,
type UpdaterReadyRecord,
Expand Down Expand Up @@ -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 }) })

Expand Down
26 changes: 24 additions & 2 deletions packages/desktop/src/main/updater-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown>
Expand Down Expand Up @@ -58,6 +71,7 @@ export function createUpdaterController(input: {
let installing: Promise<void> | undefined
let prompted = false
let asked = false
let failures = 0
const listeners = new Set<(state: UpdaterState) => void>()
const majorListeners = new Set<(state: MajorUpdateState) => void>()

Expand All @@ -82,15 +96,21 @@ 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()
.then(async (result) => {
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) => {
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion packages/desktop/src/main/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -159,7 +160,10 @@ export function setupAutoUpdater(stop: () => Promise<void>) {
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)
Expand Down
Loading