Skip to content

Commit 9df6fa4

Browse files
noreplyclaude
andcommitted
Auto-update: server updates itself when quiet, desktop installs on quit
Opt-in settings, both off by default: - Server `autoUpdateServer`: in boot-service mode, check the release source hourly; when a newer release on the same track exists, wait until no turn, approval, link request or background task is live (and an optional `autoUpdateQuietCommand` exits 0), then run the existing self-update. Skips the version the launcher rolled back; gives up a wait after 2 hours and never forces. - Desktop `autoUpdate`: download an available update after each check and install it when the app quits. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 24e7747 commit 9df6fa4

6 files changed

Lines changed: 656 additions & 0 deletions

File tree

‎apps/desktop/src/settings/DesktopAppSettings.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ export interface DesktopSettings {
3939
readonly wslDistro: string | null;
4040
// Requires restart because the pool chooses its primary spec at layer initialization.
4141
readonly wslOnly: boolean;
42+
/** Download new releases by themselves and install them when the app quits. Absent means off. */
43+
readonly autoUpdate?: boolean;
4244
}
4345

4446
export interface DesktopSettingsChange {
@@ -102,6 +104,7 @@ const DesktopSettingsDocument = Schema.Struct({
102104
wslMode: Schema.optionalKey(Schema.Literals(["local", "wsl"])),
103105
wslDistro: Schema.optionalKey(Schema.NullOr(Schema.String)),
104106
wslOnly: Schema.optionalKey(Schema.Boolean),
107+
autoUpdate: Schema.optionalKey(Schema.Boolean),
105108
});
106109

107110
type DesktopSettingsDocument = typeof DesktopSettingsDocument.Type;
@@ -233,6 +236,7 @@ function normalizeDesktopSettingsDocument(
233236
wslBackendEnabled,
234237
wslDistro: normalizeWslDistro(parsed.wslDistro),
235238
wslOnly: parsed.wslOnly === true,
239+
...(parsed.autoUpdate === true ? { autoUpdate: true } : {}),
236240
};
237241
}
238242

@@ -279,6 +283,9 @@ function toDesktopSettingsDocument(
279283
if (settings.wslOnly !== defaults.wslOnly) {
280284
document.wslOnly = settings.wslOnly;
281285
}
286+
if (settings.autoUpdate === true) {
287+
document.autoUpdate = true;
288+
}
282289

283290
return document;
284291
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,9 +889,27 @@ export const make = Effect.gen(function* () {
889889
};
890890
});
891891

892+
// 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.
894+
const autoDownloadAvailableUpdate = Effect.gen(function* () {
895+
const settings = yield* desktopSettings.get;
896+
if (settings.autoUpdate !== true) return;
897+
if ((yield* Ref.get(updateStateRef)).status !== "available") return;
898+
yield* logUpdaterInfo("downloading update automatically");
899+
const result = yield* downloadAvailableUpdate;
900+
const state = yield* Ref.get(updateStateRef);
901+
if (result.completed && state.status === "downloaded" && state.downloadedVersion !== null) {
902+
yield* electronUpdater.setAutoInstallOnAppQuit(true);
903+
yield* logUpdaterInfo("update installs when the app quits", {
904+
version: state.downloadedVersion,
905+
});
906+
}
907+
}).pipe(Effect.withSpan("desktop.updates.autoDownload"));
908+
892909
const startUpdatePollers: Effect.Effect<void, never, Scope.Scope> = Effect.gen(function* () {
893910
yield* Effect.sleep(AUTO_UPDATE_STARTUP_DELAY).pipe(
894911
Effect.andThen(checkForUpdates("startup")),
912+
Effect.andThen(autoDownloadAvailableUpdate),
895913
Effect.catchCause((cause) => {
896914
if (Cause.hasInterruptsOnly(cause)) {
897915
return Effect.void;
@@ -907,6 +925,7 @@ export const make = Effect.gen(function* () {
907925
);
908926
yield* Effect.sleep(AUTO_UPDATE_POLL_INTERVAL).pipe(
909927
Effect.andThen(checkForUpdates("poll")),
928+
Effect.andThen(autoDownloadAvailableUpdate),
910929
Effect.forever,
911930
Effect.catchCause((cause) => {
912931
if (Cause.hasInterruptsOnly(cause)) {
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
import { describe, expect, it } from "@effect/vitest";
2+
import * as Effect from "effect/Effect";
3+
import * as SqlClient from "effect/sql/SqlClient";
4+
5+
import * as SqlitePersistence from "../persistence/Sqlite.ts";
6+
import {
7+
activityReason,
8+
afterCheck,
9+
afterFailure,
10+
afterProbe,
11+
AUTO_UPDATE_CHECK_INTERVAL_MS,
12+
AUTO_UPDATE_FAILURE_BACKOFF_MS,
13+
AUTO_UPDATE_FIRST_CHECK_DELAY_MS,
14+
AUTO_UPDATE_MAX_QUIET_WAIT_MS,
15+
AUTO_UPDATE_QUIET_POLL_MS,
16+
hookReason,
17+
initialAutoUpdateState,
18+
nextAutoUpdateStep,
19+
offerableVersion,
20+
readActivity,
21+
} from "./autoUpdate.ts";
22+
23+
const quiet = { activeRuns: 0, pendingRequests: 0, waitingLinkRequests: 0, backgroundTasks: 0 };
24+
25+
describe("offerableVersion", () => {
26+
it.each([
27+
{ current: "0.1.9-fork.1", candidate: "0.1.9-fork.2", expected: "0.1.9-fork.2" },
28+
{ current: "0.1.9-fork.3", candidate: "0.1.10-fork.1", expected: "0.1.10-fork.1" },
29+
{ current: "0.1.9", candidate: "0.1.10", expected: "0.1.10" },
30+
{ current: "0.1.9-fork.2", candidate: "0.1.9-fork.2", expected: undefined },
31+
{ current: "0.1.9-fork.2", candidate: "0.1.9-fork.1", expected: undefined },
32+
{ current: "0.1.9", candidate: "0.1.10-nightly.20261112.1", expected: undefined },
33+
{ current: "0.1.9", candidate: "latest", expected: undefined },
34+
{ current: "0.1.9", candidate: undefined, expected: undefined },
35+
])("$current -> $candidate", ({ current, candidate, expected }) => {
36+
expect(offerableVersion({ current, candidate, rolledBackVersion: undefined })).toBe(expected);
37+
});
38+
39+
it("skips the release the launcher rolled back, but not a newer one", () => {
40+
expect(
41+
offerableVersion({
42+
current: "0.1.9-fork.1",
43+
candidate: "0.1.9-fork.2",
44+
rolledBackVersion: "0.1.9-fork.2",
45+
}),
46+
).toBeUndefined();
47+
expect(
48+
offerableVersion({
49+
current: "0.1.9-fork.1",
50+
candidate: "0.1.9-fork.3",
51+
rolledBackVersion: "0.1.9-fork.2",
52+
}),
53+
).toBe("0.1.9-fork.3");
54+
});
55+
});
56+
57+
describe("activityReason", () => {
58+
it("is null only when nothing is working", () => {
59+
expect(activityReason(quiet)).toBeNull();
60+
expect(activityReason({ ...quiet, activeRuns: 1 })).toBe("1 running turn");
61+
expect(activityReason({ ...quiet, pendingRequests: 2, backgroundTasks: 1 })).toBe(
62+
"2 pending approval or questions, 1 background task",
63+
);
64+
expect(activityReason({ ...quiet, waitingLinkRequests: 1 })).toBe("1 pending link request");
65+
});
66+
});
67+
68+
describe("hookReason", () => {
69+
it("allows only a clean exit", () => {
70+
expect(hookReason({ code: 0, stdout: "ok", stderr: "", timedOut: false })).toBeNull();
71+
expect(
72+
hookReason({ code: 1, stdout: "\n deploy running \nmore", stderr: "", timedOut: false }),
73+
).toBe("the quiet hook said not now: deploy running");
74+
expect(hookReason({ code: 3, stdout: "", stderr: "busy", timedOut: false })).toBe(
75+
"the quiet hook said not now: busy",
76+
);
77+
expect(hookReason({ code: 1, stdout: "", stderr: "", timedOut: false })).toBe(
78+
"the quiet hook said not now",
79+
);
80+
expect(hookReason({ code: null, stdout: "", stderr: "", timedOut: true })).toBe(
81+
"the quiet hook did not answer in time",
82+
);
83+
expect(hookReason({ error: "ProcessSpawnError" })).toBe(
84+
"the quiet hook could not run (ProcessSpawnError)",
85+
);
86+
});
87+
});
88+
89+
describe("auto-update schedule", () => {
90+
const start = 1_000_000;
91+
92+
it("checks first after the startup delay, then hourly when nothing is newer", () => {
93+
const state = initialAutoUpdateState(start);
94+
expect(nextAutoUpdateStep(state, start)).toEqual({ type: "none" });
95+
const due = start + AUTO_UPDATE_FIRST_CHECK_DELAY_MS;
96+
expect(nextAutoUpdateStep(state, due)).toEqual({ type: "check" });
97+
const after = afterCheck(due, undefined);
98+
expect(after).toEqual({ phase: "idle", nextCheckAt: due + AUTO_UPDATE_CHECK_INTERVAL_MS });
99+
});
100+
101+
it("waits for quiet once a minute, then updates", () => {
102+
const waiting = afterCheck(start, "0.1.9-fork.2");
103+
expect(nextAutoUpdateStep(waiting, start)).toEqual({ type: "probe", version: "0.1.9-fork.2" });
104+
if (waiting.phase !== "waiting") throw new Error("expected waiting");
105+
const busy = afterProbe(waiting, start, "1 running turn");
106+
expect(busy.outcome).toBe("wait");
107+
expect(nextAutoUpdateStep(busy.state, start + 1)).toEqual({ type: "none" });
108+
expect(nextAutoUpdateStep(busy.state, start + AUTO_UPDATE_QUIET_POLL_MS)).toEqual({
109+
type: "probe",
110+
version: "0.1.9-fork.2",
111+
});
112+
if (busy.state.phase !== "waiting") throw new Error("expected waiting");
113+
expect(afterProbe(busy.state, start + AUTO_UPDATE_QUIET_POLL_MS, null).outcome).toBe("update");
114+
});
115+
116+
it("never forces: a bounded wait gives up until the next check", () => {
117+
const waiting = afterCheck(start, "0.1.9-fork.2");
118+
if (waiting.phase !== "waiting") throw new Error("expected waiting");
119+
const late = start + AUTO_UPDATE_MAX_QUIET_WAIT_MS;
120+
const gaveUp = afterProbe(waiting, late, "1 running turn");
121+
expect(gaveUp.outcome).toBe("give-up");
122+
expect(gaveUp.state).toEqual({
123+
phase: "idle",
124+
nextCheckAt: late + AUTO_UPDATE_CHECK_INTERVAL_MS,
125+
});
126+
// Quiet at the bound still updates.
127+
expect(afterProbe(waiting, late, null).outcome).toBe("update");
128+
});
129+
130+
it("backs off after a failed install, longer than a normal check", () => {
131+
const failed = afterFailure(start);
132+
expect(failed).toEqual({ phase: "idle", nextCheckAt: start + AUTO_UPDATE_FAILURE_BACKOFF_MS });
133+
expect(AUTO_UPDATE_FAILURE_BACKOFF_MS).toBeGreaterThan(AUTO_UPDATE_CHECK_INTERVAL_MS);
134+
expect(nextAutoUpdateStep(failed, start + AUTO_UPDATE_CHECK_INTERVAL_MS)).toEqual({
135+
type: "none",
136+
});
137+
expect(nextAutoUpdateStep(failed, start + AUTO_UPDATE_FAILURE_BACKOFF_MS)).toEqual({
138+
type: "check",
139+
});
140+
});
141+
});
142+
143+
describe("readActivity", () => {
144+
it.effect("counts live work on threads that still exist", () =>
145+
Effect.gen(function* () {
146+
const sql = yield* SqlClient.SqlClient;
147+
expect(yield* readActivity(sql)).toEqual(quiet);
148+
149+
const now = "2026-11-12T00:00:00.000Z";
150+
const thread = (id: string, deletedAt: string | null) => sql`
151+
INSERT INTO orchestration_v2_projection_threads
152+
(thread_id, project_id, title, default_provider, runtime_mode, interaction_mode,
153+
created_at, updated_at, deleted_at, payload_json)
154+
VALUES (${id}, 'project', 'title', 'codex', 'full-access', 'default',
155+
${now}, ${now}, ${deletedAt}, '{}')
156+
`;
157+
yield* thread("live", null);
158+
yield* thread("gone", now);
159+
const run = (id: string, threadId: string, ordinal: number, status: string) => sql`
160+
INSERT INTO orchestration_v2_projection_runs
161+
(run_id, thread_id, ordinal, provider, status, requested_at, payload_json)
162+
VALUES (${id}, ${threadId}, ${ordinal}, 'codex', ${status}, ${now}, '{}')
163+
`;
164+
yield* run("queued", "live", 1, "queued");
165+
yield* run("done", "live", 2, "completed");
166+
yield* run("ghost", "gone", 1, "running");
167+
expect(yield* readActivity(sql)).toEqual(quiet);
168+
169+
yield* run("running", "live", 3, "running");
170+
yield* sql`
171+
INSERT INTO orchestration_v2_projection_runtime_requests
172+
(runtime_request_id, thread_id, node_id, kind, status, created_at, payload_json)
173+
VALUES ('request', 'live', 'node', 'approval', 'pending', ${now}, '{}')
174+
`;
175+
yield* sql`
176+
INSERT INTO orchestration_v2_projection_turn_items
177+
(turn_item_id, thread_id, ordinal, type, status, updated_at, payload_json)
178+
VALUES ('link', 'live', 1, 'link_request', 'waiting', ${now}, '{}')
179+
`;
180+
yield* sql`
181+
INSERT INTO orchestration_v2_projection_provider_threads
182+
(provider_thread_id, thread_id, provider, status, updated_at, payload_json)
183+
VALUES ('provider', 'live', 'codex', 'idle', ${now},
184+
'{"pendingBackgroundTasks":[{"id":"a"},{"id":"b"}]}')
185+
`;
186+
expect(yield* readActivity(sql)).toEqual({
187+
activeRuns: 1,
188+
pendingRequests: 1,
189+
waitingLinkRequests: 1,
190+
backgroundTasks: 2,
191+
});
192+
}).pipe(Effect.provide(SqlitePersistence.layerMemory)),
193+
);
194+
});

0 commit comments

Comments
 (0)