The best way to control Claude Code, Codex and more from any computer or phone
fix(desktop): an update download fetches the newest release, not the last check's (#37476)
Julius Marminge committed
Nov 15, 2026 at 21:05 UTC
83f35dac5c296224362d5bff4e6fa2523e88d45e
5 files changed
+304
-21
apps/desktop/src/app/DesktopStartupRecovery.test.ts
+72
@@ -211,3 +211,75 @@ it.effect.each([
211
}),
212
).pipe(Effect.provide(DesktopState.layer)),
213
);
214
+
215
+it.effect("installs the build the update action downloaded, not the one it offered", () =>
216
+ Effect.scoped(
217
+ Effect.gen(function* () {
218
+ const installed = yield* Deferred.make<string>();
219
+ const shown = yield* Deferred.make<void>();
220
+ let navigate: ((event: { preventDefault: () => void }, url: string) => void) | undefined;
221
+ const recovery = {
222
+ isDestroyed: () => false,
223
+ loadURL: () => Promise.resolve(),
224
+ once: () => {},
225
+ webContents: {
226
+ setWindowOpenHandler: () => {},
227
+ on: (_event: string, listener: typeof navigate) => {
228
+ navigate = listener;
229
+ Deferred.doneUnsafe(shown, Effect.void);
230
+ },
231
+ },
232
+ } as unknown as Electron.BrowserWindow;
233
+ const offered = updateState({ status: "available", availableVersion: "0.1.4" });
234
+ const state = yield* DesktopState.DesktopState;
235
+ yield* listen.pipe(
236
+ Effect.provide(
237
+ Layer.mergeAll(
238
+ FileSystem.layerNoop({ readFileString: () => Effect.succeed("0.1.3") }),
239
+ Layer.succeed(DesktopEnvironment.DesktopEnvironment, {
240
+ appVersion: "0.1.3",
241
+ stateDir: "/task/state",
242
+ logDir: "/task/logs",
243
+ path: { join: (...parts: string[]) => parts.join("/") },
244
+ } as unknown as DesktopEnvironment.DesktopEnvironment["Service"]),
245
+ Layer.succeed(ElectronApp.ElectronApp, {
246
+ on: () => Effect.void,
247
+ } as unknown as ElectronApp.ElectronApp["Service"]),
248
+ Layer.succeed(
249
+ ElectronShell.ElectronShell,
250
+ {} as ElectronShell.ElectronShell["Service"],
251
+ ),
252
+ Layer.succeed(ElectronTheme.ElectronTheme, {
253
+ shouldUseDarkColors: Effect.succeed(false),
254
+ } as ElectronTheme.ElectronTheme["Service"]),
255
+ Layer.succeed(ElectronWindow.ElectronWindow, {
256
+ main: Effect.succeedNone,
257
+ create: () => Effect.succeed(recovery),
258
+ } as unknown as ElectronWindow.ElectronWindow["Service"]),
259
+ Layer.succeed(DesktopUpdates.DesktopUpdates, {
260
+ subscribe: Effect.succeed({ latest: offered, changes: Stream.empty }),
261
+ check: () => Effect.void,
262
+ getState: Effect.succeed(offered),
263
+ // A release published since the offer: the download fetches it instead.
264
+ download: Effect.succeed({
265
+ accepted: true,
266
+ completed: true,
267
+ state: updateState({ status: "downloaded", downloadedVersion: "0.1.5" }),
268
+ }),
269
+ installPrepared: (version: string) => Deferred.succeed(installed, version),
270
+ } as unknown as DesktopUpdates.DesktopUpdates["Service"]),
271
+ ),
272
+ ),
273
+ );
274
+ yield* SubscriptionRef.set(state.primaryBackendStartup, {
275
+ _tag: "Failing",
276
+ reason: "Server failed before readiness",
277
+ managedPolicy: false,
278
+ output: "Failure details",
279
+ });
280
+ yield* Deferred.await(shown);
281
+ navigate!({ preventDefault: () => {} }, "https://startup-recovery.invalid/update");
282
+ expect(yield* Deferred.await(installed)).toBe("0.1.5");
283
+ }),
284
+ ).pipe(Effect.provide(DesktopState.layer)),
285
+);
apps/desktop/src/app/DesktopStartupRecovery.ts
+6
-3
@@ -212,12 +212,15 @@ export const listen: Effect.Effect<
212
switch (action) {
213
case "update": {
214
if (current._tag !== "Update") return;
215
- const state = yield* updates.getState;
216
- if (state.downloadedVersion === null) {
215
+ let { downloadedVersion } = yield* updates.getState;
216
+ if (downloadedVersion === null) {
217
+ // The download re-checks first, so it can fetch a newer build than the one offered.
218
const download = yield* updates.download;
219
if (!download.completed) return;
220
+ downloadedVersion = download.state.downloadedVersion;
221
}
220
- yield* updates.installPrepared(current.version);
222
+ if (downloadedVersion === null) return;
223
+ yield* updates.installPrepared(downloadedVersion);
224
return;
225
}
226
case "reinstall":
apps/desktop/src/updates/DesktopRemoteUpdates.test.ts
+6
-6
@@ -733,16 +733,15 @@ describe("DesktopRemoteUpdates", () => {
733
it.effect("waits for a background check before installing a prepared update", () => {
734
const backgroundCheckStarted = Deferred.makeUnsafe<void>();
735
const releaseBackgroundCheck = Deferred.makeUnsafe<void>();
736
- let checks = 0;
736
+ let blockCheck = false;
737
const harness = makeHarness({
738
- checkForUpdates: Effect.suspend(() => {
739
- checks += 1;
740
- return checks === 2
738
+ checkForUpdates: Effect.suspend(() =>
739
+ blockCheck
740
? Deferred.succeed(backgroundCheckStarted, undefined).pipe(
741
Effect.andThen(Deferred.await(releaseBackgroundCheck)),
742
)
744
- : Effect.void;
745
- }),
743
+ : Effect.void,
744
+ ),
745
});
746
747
return runRemoteUpdatesTest(harness, ({ requests, commits }) =>
@@ -755,6 +754,7 @@ describe("DesktopRemoteUpdates", () => {
754
harness.emit("update-downloaded", { version: "1.2.4" });
755
yield* settle;
756
757
+ blockCheck = true;
758
const backgroundCheck = yield* updates.check("poll").pipe(Effect.forkChild);
759
yield* Deferred.await(backgroundCheckStarted);
760
assert.isTrue(yield* updates.isActionActive);
apps/desktop/src/updates/DesktopUpdates.test.ts
+144
-2
@@ -344,10 +344,11 @@ describe("DesktopUpdates", () => {
344
345
const installFiber = yield* updates.install.pipe(Effect.forkScoped);
346
yield* Deferred.await(installStarted);
347
+ const checksBefore = harness.checkCount();
348
349
const checkResult = yield* updates.check("manual");
350
assert.isFalse(checkResult.checked);
350
- assert.equal(harness.checkCount(), 0);
351
+ assert.equal(harness.checkCount(), checksBefore);
352
353
yield* Deferred.succeed(releaseInstall, undefined);
354
const installResult = yield* Fiber.join(installFiber);
@@ -358,7 +359,9 @@ describe("DesktopUpdates", () => {
359
);
360
361
it.effect("preserves a queued installer after a background updater error", () => {
361
- const harness = makeHarness();
362
+ const harness = makeHarness({
363
+ checkForUpdates: Effect.sync(() => harness.emit("update-not-available")),
364
+ });
365
366
return Effect.scoped(
367
Effect.gen(function* () {
@@ -494,6 +497,145 @@ describe("DesktopUpdates", () => {
497
).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
498
});
499
500
+ it.effect("downloads the newest release published since the last check", () => {
501
+ // electron-updater downloads the release its last checkForUpdates found.
502
+ let published = "1.2.4";
503
+ let lastChecked: string | undefined;
504
+ const harness = makeHarness({
505
+ checkForUpdates: Effect.sync(() => {
506
+ lastChecked = published;
507
+ harness.emit("update-available", { version: published });
508
+ }),
509
+ downloadUpdate: Effect.sync(() => {
510
+ harness.emit("update-downloaded", { version: lastChecked });
511
+ }),
512
+ });
513
+
514
+ return Effect.scoped(
515
+ Effect.gen(function* () {
516
+ const updates = yield* DesktopUpdates.DesktopUpdates;
517
+ yield* updates.configure;
518
+ yield* updates.check("poll");
519
+ yield* flushCallbacks;
520
+ assert.equal((yield* updates.getState).availableVersion, "1.2.4");
521
+
522
+ published = "1.2.5";
523
+ const result = yield* updates.download;
524
+ yield* flushCallbacks;
525
+ assert.isTrue(result.completed);
526
+ assert.equal((yield* updates.getState).downloadedVersion, "1.2.5");
527
+ }),
528
+ ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
529
+ });
530
+
531
+ it.effect("installs a release published after the retained download", () => {
532
+ let published = "1.2.4";
533
+ let lastChecked: string | undefined;
534
+ let installedVersion: string | null | undefined;
535
+ const harness = makeHarness({
536
+ checkForUpdates: Effect.sync(() => {
537
+ lastChecked = published;
538
+ harness.emit("update-available", { version: published });
539
+ }),
540
+ downloadUpdate: Effect.sync(() => {
541
+ harness.emit("update-downloaded", { version: lastChecked });
542
+ }),
543
+ quitAndInstall: Effect.sync(() => {
544
+ installedVersion = harness.sentStates.at(-1)?.downloadedVersion;
545
+ }),
546
+ });
547
+
548
+ return Effect.scoped(
549
+ Effect.gen(function* () {
550
+ const updates = yield* DesktopUpdates.DesktopUpdates;
551
+ yield* updates.configure;
552
+ yield* updates.check("poll");
553
+ yield* flushCallbacks;
554
+ yield* updates.download;
555
+ yield* flushCallbacks;
556
+ assert.equal((yield* updates.getState).downloadedVersion, "1.2.4");
557
+
558
+ published = "1.2.5";
559
+ const checksBefore = harness.checkCount();
560
+ const result = yield* updates.installPrepared("1.2.4");
561
+ assert.isTrue(result.accepted);
562
+ // A second check before downloading could find a stale feed's older release.
563
+ assert.equal(harness.checkCount(), checksBefore + 1);
564
+ assert.equal(harness.quitAndInstalls(), 1);
565
+ assert.equal(installedVersion, "1.2.5");
566
+ }),
567
+ ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
568
+ });
569
+
570
+ it.effect("downloads the known offer when the check before downloading fails", () => {
571
+ let checks = 0;
572
+ const harness = makeHarness({
573
+ checkForUpdates: Effect.suspend(() => {
574
+ checks += 1;
575
+ if (checks > 1) {
576
+ return Effect.fail(
577
+ new ElectronUpdater.ElectronUpdaterCheckForUpdatesError({
578
+ channel: null,
579
+ cause: new Error("offline"),
580
+ }),
581
+ );
582
+ }
583
+ harness.emit("update-available", { version: "1.2.4" });
584
+ return Effect.void;
585
+ }),
586
+ downloadUpdate: Effect.sync(() => {
587
+ harness.emit("update-downloaded", { version: "1.2.4" });
588
+ }),
589
+ });
590
+
591
+ return Effect.scoped(
592
+ Effect.gen(function* () {
593
+ const updates = yield* DesktopUpdates.DesktopUpdates;
594
+ yield* updates.configure;
595
+ yield* updates.check("poll");
596
+ yield* flushCallbacks;
597
+
598
+ const result = yield* updates.download;
599
+ yield* flushCallbacks;
600
+ assert.isTrue(result.completed);
601
+ assert.equal((yield* updates.getState).downloadedVersion, "1.2.4");
602
+ // Remote requests treat a published error after their own check as final.
603
+ assert.notInclude(
604
+ harness.sentStates.map((state) => state.status),
605
+ "error",
606
+ );
607
+ }),
608
+ ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
609
+ });
610
+
611
+ it.effect("installs the retained download when a stale feed offers an older release", () => {
612
+ let published = "1.2.5";
613
+ let installedVersion: string | null | undefined;
614
+ const harness = makeHarness({
615
+ checkForUpdates: Effect.sync(() => harness.emit("update-available", { version: published })),
616
+ downloadUpdate: Effect.sync(() => harness.emit("update-downloaded", { version: "1.2.5" })),
617
+ quitAndInstall: Effect.sync(() => {
618
+ installedVersion = harness.sentStates.at(-1)?.downloadedVersion;
619
+ }),
620
+ });
621
+
622
+ return Effect.scoped(
623
+ Effect.gen(function* () {
624
+ const updates = yield* DesktopUpdates.DesktopUpdates;
625
+ yield* updates.configure;
626
+ yield* updates.check("poll");
627
+ yield* updates.download;
628
+ yield* flushCallbacks;
629
+
630
+ published = "1.2.4";
631
+ const result = yield* updates.installPrepared("1.2.5");
632
+ assert.isTrue(result.accepted);
633
+ assert.equal(harness.downloadCount(), 1);
634
+ assert.equal(installedVersion, "1.2.5");
635
+ }),
636
+ ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
637
+ });
638
+
639
it.effect("restores download state and permits retry after interruption", () =>
640
Effect.gen(function* () {
641
const actionStarted = yield* Deferred.make<void>();
apps/desktop/src/updates/DesktopUpdates.ts
+76
-10
@@ -557,6 +557,17 @@ export const make = Effect.gen(function* () {
557
});
558
});
559
560
+ // The flag is shared updater state, so callers flip it only while holding a reservation.
561
+ const runUpdaterCheck = Effect.fn("desktop.updates.runUpdaterCheck")(function* (
562
+ allowDowngrade: boolean,
563
+ ) {
564
+ const restoreDowngrade = yield* electronUpdater.allowDowngrade;
565
+ if (allowDowngrade) yield* electronUpdater.setAllowDowngrade(true);
566
+ return yield* electronUpdater.checkForUpdates.pipe(
567
+ Effect.ensuring(electronUpdater.setAllowDowngrade(restoreDowngrade).pipe(Effect.ignore)),
568
+ );
569
+ });
570
+
571
const checkForUpdates = Effect.fn("desktop.updates.checkForUpdates")(function* (
572
reason: string,
573
actionReservation: "acquire" | "held" = "acquire",
@@ -586,11 +597,7 @@ export const make = Effect.gen(function* () {
597
}
598
yield* logUpdaterInfo("checking for updates", { reason });
599
589
- // The flag is shared updater state, so it flips only while this check holds the reservation.
590
- const restoreDowngrade = yield* electronUpdater.allowDowngrade;
591
- if (allowDowngrade) yield* electronUpdater.setAllowDowngrade(true);
592
- return yield* electronUpdater.checkForUpdates.pipe(
593
- Effect.ensuring(electronUpdater.setAllowDowngrade(restoreDowngrade).pipe(Effect.ignore)),
600
+ return yield* runUpdaterCheck(allowDowngrade).pipe(
601
Effect.as(true),
602
Effect.catchTags({
603
ElectronUpdaterCheckForUpdatesError: Effect.fn(
@@ -619,7 +626,10 @@ export const make = Effect.gen(function* () {
626
);
627
});
628
622
- const downloadAvailableUpdate = Effect.gen(function* () {
629
+ // Install refreshes before downloading, and a second check could pick a stale, older release.
630
+ const downloadAvailableUpdate = Effect.fn("desktop.updates.downloadAvailableUpdate")(function* (
631
+ refresh: boolean,
632
+ ) {
633
const admission = yield* Effect.scoped(
634
admitAfterCheck(
635
Effect.gen(function* () {
@@ -643,8 +653,29 @@ export const make = Effect.gen(function* () {
653
return { accepted: false, completed: false };
654
}
655
646
- const state = yield* Ref.get(updateStateRef);
656
+ const offered = yield* Ref.get(updateStateRef);
657
+ let state = offered;
658
return yield* Effect.gen(function* () {
659
+ if (refresh) {
660
+ // electron-updater downloads what its last check found, which can be hours old.
661
+ yield* logUpdaterInfo("checking for updates", { reason: "download" });
662
+ yield* runUpdaterCheck(
663
+ offered.channel !== desktopUpdateChannelOf(environment.appVersion),
664
+ ).pipe(
665
+ // A failed check keeps electron-updater's earlier result, so the known offer still downloads.
666
+ Effect.catchTags({
667
+ ElectronUpdaterCheckForUpdatesError: (error) =>
668
+ logUpdaterWarning(error.message, {
669
+ errorTag: error._tag,
670
+ cause: describeUpdateErrorCause(error.cause),
671
+ }),
672
+ }),
673
+ );
674
+ }
675
+ state = yield* Ref.get(updateStateRef);
676
+ if (state.status !== "available") {
677
+ return { accepted: true, completed: false };
678
+ }
679
yield* setState(reduceDesktopUpdateStateOnDownloadStart(state));
680
yield* electronUpdater.setDisableDifferentialDownload(
681
isArm64HostRunningIntelBuild(environment.runtimeInfo),
@@ -693,7 +724,7 @@ export const make = Effect.gen(function* () {
724
}),
725
Effect.ensuring(finishUpdateAction("download")),
726
);
696
- }).pipe(Effect.withSpan("desktop.updates.downloadAvailableUpdate"));
727
+ });
728
729
const updateRestartMarkerDir = environment.path.join(environment.baseDir, "runtime");
730
const updateRestartMarkerPath = environment.path.join(
@@ -948,7 +979,42 @@ export const make = Effect.gen(function* () {
979
state: yield* Ref.get(updateStateRef),
980
};
981
}
951
- const result = yield* installDownloadedUpdate(expectedVersion);
982
+ let installVersion = expectedVersion;
983
+ // A download can be hours old: a release published since replaces it before restarting.
984
+ const retained = yield* Ref.get(updateStateRef);
985
+ if (
986
+ retained.downloadedVersion !== null &&
987
+ (expectedVersion === undefined || retained.downloadedVersion === expectedVersion) &&
988
+ (yield* checkForUpdates(
989
+ "install",
990
+ "acquire",
991
+ retained.channel !== desktopUpdateChannelOf(environment.appVersion),
992
+ )) &&
993
+ (yield* Ref.get(updateStateRef)).status === "available"
994
+ ) {
995
+ const refreshed = yield* Ref.get(updateStateRef);
996
+ // electron-updater compares a stale feed with the running build, not the retained download.
997
+ if (
998
+ refreshed.availableVersion === null ||
999
+ compareSemverVersions(refreshed.availableVersion, retained.downloadedVersion) <= 0
1000
+ ) {
1001
+ yield* setState(retained);
1002
+ } else {
1003
+ const download = yield* downloadAvailableUpdate(false);
1004
+ if (!download.completed) {
1005
+ return {
1006
+ accepted: download.accepted,
1007
+ completed: false,
1008
+ failed: download.accepted,
1009
+ state: yield* Ref.get(updateStateRef),
1010
+ };
1011
+ }
1012
+ if (expectedVersion !== undefined) {
1013
+ installVersion = (yield* Ref.get(updateStateRef)).downloadedVersion ?? expectedVersion;
1014
+ }
1015
+ }
1016
+ }
1017
+ const result = yield* installDownloadedUpdate(installVersion);
1018
return {
1019
accepted: result.accepted,
1020
completed: result.completed,
@@ -1357,7 +1423,7 @@ export const make = Effect.gen(function* () {
1423
};
1424
}),
1425
download: Effect.gen(function* () {
1360
- const result = yield* downloadAvailableUpdate;
1426
+ const result = yield* downloadAvailableUpdate(true);
1427
return {
1428
accepted: result.accepted,
1429
completed: result.completed,