Skip to content

Commit 5280dbc

Browse files
noreplyclaude
andcommitted
Auto-update fix round 1: hold new work until restart, upstream activity, desktop record
Server: - WorkAdmission gate in front of outbox claims and worktree preparation. The last check at the self-update's installing stage reads the setting, runs the quiet hook, closes the gate, counts admitted work, then reads thread state. The gate stays closed until exit; it opens again on a rejected handoff or when the launcher drops the pending record. - Activity comes from upstream's runtime recovery projections and derivePendingBackgroundWork, so every provider's background work counts, plus unheld queued runs and running terminal commands. - A failed launcher record backs off once instead of skipping forever; an unreadable state file stops the update with a warning; the Update button's update running first retries after a minute. - makeAutoUpdater takes its dependencies, and the loop is tested against a fake server. Desktop: - Arming install-on-quit writes the install record, so an install that did not apply is reported; that version is not armed again automatically. - Install-on-quit is switched off when the setting is off or the track changes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 9df6fa4 commit 5280dbc

10 files changed

Lines changed: 1288 additions & 304 deletions

File tree

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import { assert, describe, it } from "@effect/vitest";
2+
import * as Effect from "effect/Effect";
3+
import * as Layer from "effect/Layer";
4+
import * as TestClock from "effect/testing/TestClock";
5+
6+
import * as DesktopUpdates from "./DesktopUpdates.ts";
7+
import { flushCallbacks, makeHarness, type UpdatesHarnessOptions } from "./updatesTestHarness.ts";
8+
9+
// The first background check finds 1.2.4 and downloading it finishes; later checks find nothing new.
10+
const autoUpdateHarness = (options: UpdatesHarnessOptions = {}) => {
11+
let checks = 0;
12+
const harness = makeHarness({
13+
autoUpdate: true,
14+
beforeSetUpdateChannel: Effect.void,
15+
checkForUpdates: Effect.sync(() => {
16+
checks += 1;
17+
if (checks === 1) harness.emit("update-available", { version: "1.2.4" });
18+
}),
19+
downloadUpdate: Effect.sync(() => {
20+
harness.emit("update-downloaded", { version: "1.2.4" });
21+
}),
22+
...options,
23+
});
24+
return harness;
25+
};
26+
27+
const records = (harness: ReturnType<typeof makeHarness>) =>
28+
[...harness.installRecords.values()].map((record) => JSON.parse(record));
29+
30+
const run = <E>(
31+
harness: ReturnType<typeof makeHarness>,
32+
body: (updates: DesktopUpdates.DesktopUpdates["Service"]) => Effect.Effect<void, E>,
33+
) =>
34+
Effect.scoped(
35+
Effect.gen(function* () {
36+
const updates = yield* DesktopUpdates.DesktopUpdates;
37+
yield* updates.configure;
38+
yield* body(updates);
39+
}),
40+
).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
41+
42+
const passStartupCheck = Effect.gen(function* () {
43+
yield* TestClock.adjust("16 seconds");
44+
yield* flushCallbacks;
45+
});
46+
47+
describe("DesktopUpdates with the autoUpdate setting", () => {
48+
it.effect("records an install on quit like the Update button does", () => {
49+
const harness = autoUpdateHarness();
50+
return run(harness, () =>
51+
Effect.gen(function* () {
52+
yield* passStartupCheck;
53+
assert.deepEqual(harness.autoInstallOnAppQuit.at(-1), true);
54+
// The record is what lets the next start report an install that did not apply.
55+
assert.deepEqual(records(harness), [{ fromVersion: "1.2.3", targetVersion: "1.2.4" }]);
56+
}),
57+
);
58+
});
59+
60+
it.effect("leaves a version that did not apply to the Update button", () => {
61+
const harness = autoUpdateHarness({
62+
installRecord: JSON.stringify({ fromVersion: "1.2.3", targetVersion: "1.2.4" }),
63+
});
64+
return run(harness, (updates) =>
65+
Effect.gen(function* () {
66+
assert.equal((yield* updates.getState).notAppliedVersion, "1.2.4");
67+
yield* passStartupCheck;
68+
assert.equal(harness.downloadCount(), 0);
69+
assert.notInclude(harness.autoInstallOnAppQuit, true);
70+
assert.equal((yield* updates.getState).notAppliedVersion, "1.2.4");
71+
}),
72+
);
73+
});
74+
75+
it.effect("stops installing on quit when the setting is turned off", () => {
76+
const harness = autoUpdateHarness();
77+
return run(harness, () =>
78+
Effect.gen(function* () {
79+
yield* passStartupCheck;
80+
assert.deepEqual(harness.autoInstallOnAppQuit.at(-1), true);
81+
harness.setAutoUpdate(false);
82+
yield* TestClock.adjust("5 minutes");
83+
yield* flushCallbacks;
84+
assert.deepEqual(harness.autoInstallOnAppQuit.at(-1), false);
85+
assert.deepEqual(records(harness), []);
86+
}),
87+
);
88+
});
89+
90+
it.effect("stops installing on quit when the track changes", () => {
91+
const harness = autoUpdateHarness();
92+
return run(harness, (updates) =>
93+
Effect.gen(function* () {
94+
yield* passStartupCheck;
95+
assert.deepEqual(harness.autoInstallOnAppQuit.at(-1), true);
96+
yield* updates.setChannel("nightly");
97+
assert.deepEqual(harness.autoInstallOnAppQuit.at(-1), false);
98+
assert.deepEqual(records(harness), []);
99+
}),
100+
);
101+
});
102+
103+
it.effect("never arms install-on-quit without the setting", () => {
104+
const harness = autoUpdateHarness({ autoUpdate: false });
105+
return run(harness, () =>
106+
Effect.gen(function* () {
107+
yield* passStartupCheck;
108+
assert.equal(harness.downloadCount(), 0);
109+
assert.notInclude(harness.autoInstallOnAppQuit, true);
110+
}),
111+
);
112+
});
113+
});

‎apps/desktop/src/updates/DesktopUpdates.ts‎

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,9 @@ export const make = Effect.gen(function* () {
388388
const updaterConfiguredRef = yield* Ref.make(false);
389389
const lastLoggedDownloadMilestoneRef = yield* Ref.make(-1);
390390
const notAppliedVersionRef = yield* Ref.make(Option.none<string>());
391+
// Whether the autoUpdate setting armed the updater to install a download when the app quits.
392+
const autoInstallArmedRef = yield* Ref.make(false);
393+
const autoInstallSkipLoggedRef = yield* Ref.make(Option.none<string>());
391394
// The release a track switch's database check cleared. A build crosses to another track only
392395
// through such a switch, and never below that release: version order alone cannot tell whether
393396
// a Stable knows a Nightly's migrations.
@@ -889,17 +892,44 @@ export const make = Effect.gen(function* () {
889892
};
890893
});
891894

895+
// Install-on-quit stops when the setting is off or the track changes: a download from a track
896+
// the app no longer follows must not install.
897+
const disarmAutoInstall = Effect.gen(function* () {
898+
if (!(yield* Ref.getAndSet(autoInstallArmedRef, false))) return;
899+
yield* electronUpdater.setAutoInstallOnAppQuit(false);
900+
yield* removeInstallRecord;
901+
yield* logUpdaterInfo("update no longer installs when the app quits");
902+
});
903+
892904
// With the autoUpdate setting, a background check that finds a release downloads it, and the
893-
// updater installs it when the app quits. Only a download that stayed offerable is installed.
905+
// updater installs it when the app quits. The install is recorded like the Update button's, so
906+
// one that did not apply is reported at the next start, and that version is then left to the
907+
// Update button instead of being tried again on every quit.
894908
const autoDownloadAvailableUpdate = Effect.gen(function* () {
895909
const settings = yield* desktopSettings.get;
896-
if (settings.autoUpdate !== true) return;
897-
if ((yield* Ref.get(updateStateRef)).status !== "available") return;
910+
if (settings.autoUpdate !== true) {
911+
yield* disarmAutoInstall;
912+
return;
913+
}
914+
const current = yield* Ref.get(updateStateRef);
915+
if (current.status !== "available") return;
916+
const notApplied = yield* Ref.get(notAppliedVersionRef);
917+
if (Option.isSome(notApplied) && current.availableVersion === notApplied.value) {
918+
const logged = yield* Ref.getAndSet(autoInstallSkipLoggedRef, notApplied);
919+
if (!Option.isSome(logged) || logged.value !== notApplied.value) {
920+
yield* logUpdaterWarning("not installing automatically: this version did not apply", {
921+
version: notApplied.value,
922+
});
923+
}
924+
return;
925+
}
898926
yield* logUpdaterInfo("downloading update automatically");
899927
const result = yield* downloadAvailableUpdate;
900928
const state = yield* Ref.get(updateStateRef);
901929
if (result.completed && state.status === "downloaded" && state.downloadedVersion !== null) {
930+
yield* writeInstallRecord(state.downloadedVersion);
902931
yield* electronUpdater.setAutoInstallOnAppQuit(true);
932+
yield* Ref.set(autoInstallArmedRef, true);
903933
yield* logUpdaterInfo("update installs when the app quits", {
904934
version: state.downloadedVersion,
905935
});
@@ -1258,6 +1288,7 @@ export const make = Effect.gen(function* () {
12581288
return state;
12591289
}
12601290

1291+
yield* disarmAutoInstall;
12611292
yield* desktopSettings
12621293
.setUpdateChannel(nextChannel)
12631294
.pipe(

‎apps/desktop/src/updates/updatesTestHarness.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ export interface UpdatesHarnessOptions {
4343
readonly savedChannel?: DesktopAppSettings.DesktopSettings["updateChannel"];
4444
/** Adds a WSL backend beside the primary, as on a Windows host with WSL enabled. */
4545
readonly wslBackend?: boolean;
46+
/** Starts with the autoUpdate setting; `setAutoUpdate` changes it later. */
47+
readonly autoUpdate?: boolean;
4648
}
4749

4850
export function makeHarness(options: UpdatesHarnessOptions = {}) {
@@ -56,6 +58,7 @@ export function makeHarness(options: UpdatesHarnessOptions = {}) {
5658
const listeners = new Map<string, Set<(...args: readonly unknown[]) => void>>();
5759
const sentStates: DesktopUpdateState[] = [];
5860
const installSteps: string[] = [];
61+
const autoInstallOnAppQuit: boolean[] = [];
5962

6063
const addListener = (eventName: string, listener: (...args: readonly unknown[]) => void) => {
6164
const eventListeners = listeners.get(eventName) ?? new Set();
@@ -80,7 +83,10 @@ export function makeHarness(options: UpdatesHarnessOptions = {}) {
8083
feedUrls.push(options);
8184
}),
8285
setAutoDownload: () => Effect.void,
83-
setAutoInstallOnAppQuit: () => Effect.void,
86+
setAutoInstallOnAppQuit: (value) =>
87+
Effect.sync(() => {
88+
autoInstallOnAppQuit.push(value);
89+
}),
8490
setChannel: () => Effect.void,
8591
setAllowPrerelease: () => Effect.void,
8692
allowDowngrade: Effect.sync(() => allowDowngrade),
@@ -201,10 +207,14 @@ export function makeHarness(options: UpdatesHarnessOptions = {}) {
201207
...(options.savedChannel === undefined
202208
? {}
203209
: { updateChannel: options.savedChannel, updateChannelConfiguredByUser: true }),
210+
...(options.autoUpdate === undefined ? {} : { autoUpdate: options.autoUpdate }),
204211
};
205212
const setUpdateChannelError = options.setUpdateChannelError;
206213
const layerSettings =
207-
setUpdateChannelError || options.beforeSetUpdateChannel || options.savedChannel
214+
setUpdateChannelError ||
215+
options.beforeSetUpdateChannel ||
216+
options.savedChannel ||
217+
options.autoUpdate !== undefined
208218
? Layer.succeed(DesktopAppSettings.DesktopAppSettings, {
209219
get: Effect.sync(() => testSettings),
210220
load: Effect.sync(() => testSettings),
@@ -297,6 +307,11 @@ export function makeHarness(options: UpdatesHarnessOptions = {}) {
297307
checkCount: () => checkCount,
298308
quitAndInstalls: () => quitAndInstallCount,
299309
installSteps,
310+
/** Every value the updater's install-on-quit flag was set to, in order. */
311+
autoInstallOnAppQuit,
312+
setAutoUpdate: (value: boolean) => {
313+
testSettings = { ...testSettings, autoUpdate: value };
314+
},
300315
updateRestartMarkers,
301316
installRecords,
302317
previousInstallRecord: () => previousInstallRecord,

0 commit comments

Comments
 (0)