Skip to content

Commit 3ecd968

Browse files
scuttletobyclaude
andcommitted
Auto-update (server): background service updates itself when quiet
A boot-service server checks its release track hourly and, when the automaticUpdates setting is on, runs the existing self-update once no thread, background task, terminal command or quiet command objects. A WorkAdmission gate holds new work from the last check to the restart, and the restarted server starts the held work once the launcher commits. This squashes the reviewed rounds 1-3 (9df6fa4..350656a) and rebases them on fork main. Round 4 changes: - Settings take upstream pingdotgg#17585/pingdotgg#17586's name and place: automaticUpdates (was autoUpdateServer), plus the fork-only automaticUpdatesQuietCommand (was autoUpdateQuietCommand). A file with the old names is renamed in memory on load (cloud/autoUpdateSettings.ts); no schema migration. - The desktop install-on-quit half moved out of this PR; it waits for upstream pingdotgg#17588. - Rebase fixes for fork main: worker.runOnce(claimLimits), launcher.prepareTrial({ baseDir }), the launch layer's event sink. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 9cdbb73 commit 3ecd968

18 files changed

Lines changed: 3583 additions & 17 deletions

‎apps/server/src/cloud/autoUpdate.test.ts‎

Lines changed: 866 additions & 0 deletions
Large diffs are not rendered by default.

‎apps/server/src/cloud/autoUpdate.ts‎

Lines changed: 830 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2+
import * as NodeServices from "@effect/platform-node/NodeServices";
3+
import { assert, it } from "@effect/vitest";
4+
import * as Effect from "effect/Effect";
5+
import * as FileSystem from "effect/FileSystem";
6+
import * as Layer from "effect/Layer";
7+
8+
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
9+
import * as ServerConfig from "../config.ts";
10+
import * as SqlitePersistence from "../persistence/Sqlite.ts";
11+
import * as ManagedPolicy from "../policy/ManagedPolicy.ts";
12+
import * as ServerSettingsModule from "../serverSettings.ts";
13+
import { renameRetiredAutoUpdateKeys } from "./autoUpdateSettings.ts";
14+
15+
// One settings service on a fresh temp base dir per test, as serverSettings.test.ts builds it.
16+
const layerServerSettings = () =>
17+
ServerSettingsModule.layer.pipe(
18+
Layer.provide(ServerSecretStore.layer),
19+
Layer.provide(ManagedPolicy.layerTest()),
20+
Layer.provideMerge(Layer.fresh(SqlitePersistence.layerMemory)),
21+
Layer.provideMerge(
22+
Layer.fresh(
23+
ServerConfig.layerTest(process.cwd(), { prefix: "t3code-auto-update-settings-test-" }),
24+
),
25+
),
26+
);
27+
28+
/** Loads a settings file with these contents (none when undefined) and returns what the server reads. */
29+
const loadSettingsFile = (contents: string | undefined) =>
30+
Effect.gen(function* () {
31+
const config = yield* ServerConfig.ServerConfig;
32+
const fs = yield* FileSystem.FileSystem;
33+
if (contents !== undefined) yield* fs.writeFileString(config.settingsPath, contents);
34+
const service = yield* ServerSettingsModule.ServerSettingsService;
35+
return yield* service.getSettings;
36+
});
37+
38+
it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))(
39+
"automatic update settings saved under the old names",
40+
(it) => {
41+
it.effect("loads an old file's choice under the new names, then saves only those", () =>
42+
Effect.gen(function* () {
43+
const settings = yield* loadSettingsFile(
44+
JSON.stringify({
45+
autoUpdateServer: true,
46+
autoUpdateQuietCommand: "/usr/local/bin/quiet",
47+
enableAgentBrowserAccess: false,
48+
}),
49+
);
50+
assert.isTrue(settings.automaticUpdates);
51+
assert.equal(settings.automaticUpdatesQuietCommand, "/usr/local/bin/quiet");
52+
assert.isFalse(settings.enableAgentBrowserAccess);
53+
54+
// Any write saves the new names; the old keys are gone, so a later load reads the same.
55+
const service = yield* ServerSettingsModule.ServerSettingsService;
56+
yield* service.updateSettings({ enableAgentBrowserAccess: true });
57+
const config = yield* ServerConfig.ServerConfig;
58+
const fs = yield* FileSystem.FileSystem;
59+
const persisted = JSON.parse(yield* fs.readFileString(config.settingsPath));
60+
assert.isTrue(persisted.automaticUpdates);
61+
assert.equal(persisted.automaticUpdatesQuietCommand, "/usr/local/bin/quiet");
62+
assert.notProperty(persisted, "autoUpdateServer");
63+
assert.notProperty(persisted, "autoUpdateQuietCommand");
64+
}).pipe(Effect.provide(layerServerSettings())),
65+
);
66+
67+
it.effect("keeps a new name already in the file over the old one", () =>
68+
Effect.gen(function* () {
69+
const settings = yield* loadSettingsFile(
70+
JSON.stringify({
71+
autoUpdateServer: true,
72+
automaticUpdates: false,
73+
autoUpdateQuietCommand: "/old/quiet",
74+
automaticUpdatesQuietCommand: "/new/quiet",
75+
}),
76+
);
77+
assert.isFalse(settings.automaticUpdates);
78+
assert.equal(settings.automaticUpdatesQuietCommand, "/new/quiet");
79+
}).pipe(Effect.provide(layerServerSettings())),
80+
);
81+
82+
it.effect("drops an old value of the wrong type without resetting the other settings", () =>
83+
Effect.gen(function* () {
84+
const settings = yield* loadSettingsFile(
85+
JSON.stringify({
86+
autoUpdateServer: "yes",
87+
autoUpdateQuietCommand: 7,
88+
enableAgentBrowserAccess: false,
89+
}),
90+
);
91+
assert.isFalse(settings.automaticUpdates);
92+
assert.equal(settings.automaticUpdatesQuietCommand, "");
93+
assert.isFalse(settings.enableAgentBrowserAccess);
94+
}).pipe(Effect.provide(layerServerSettings())),
95+
);
96+
97+
it.effect("stays off with no settings file", () =>
98+
Effect.gen(function* () {
99+
const settings = yield* loadSettingsFile(undefined);
100+
assert.isFalse(settings.automaticUpdates);
101+
assert.equal(settings.automaticUpdatesQuietCommand, "");
102+
}).pipe(Effect.provide(layerServerSettings())),
103+
);
104+
105+
it.effect("stays off with an empty settings object", () =>
106+
Effect.gen(function* () {
107+
const settings = yield* loadSettingsFile("{}");
108+
assert.isFalse(settings.automaticUpdates);
109+
assert.equal(settings.automaticUpdatesQuietCommand, "");
110+
}).pipe(Effect.provide(layerServerSettings())),
111+
);
112+
113+
it.effect("stays off when the file was cut off half-way through an old key", () =>
114+
Effect.gen(function* () {
115+
const settings = yield* loadSettingsFile('{ "autoUpdateServer": tr');
116+
assert.isFalse(settings.automaticUpdates);
117+
assert.equal(settings.automaticUpdatesQuietCommand, "");
118+
}).pipe(Effect.provide(layerServerSettings())),
119+
);
120+
},
121+
);
122+
123+
it("passes through what is not a settings object, and an object without old keys unchanged", () => {
124+
for (const value of [null, 3, "text", [true]]) {
125+
assert.equal(renameRetiredAutoUpdateKeys(value), value);
126+
}
127+
const current = { automaticUpdates: true };
128+
assert.strictEqual(renameRetiredAutoUpdateKeys(current), current);
129+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Fork: settings saved before the automatic-update keys took upstream's names still say
2+
// `autoUpdateServer` / `autoUpdateQuietCommand`. Copy them to the new names before decoding, so the
3+
// choice loads without a schema migration; the next write saves only the new names. Delete this
4+
// file and its call in serverSettings.ts when upstream's updater replaces ours.
5+
6+
const RETIRED_KEYS = [
7+
{ retired: "autoUpdateServer", current: "automaticUpdates", type: "boolean" },
8+
{ retired: "autoUpdateQuietCommand", current: "automaticUpdatesQuietCommand", type: "string" },
9+
] as const;
10+
11+
/**
12+
* Copies each retired key's value to its current name. A current key already in the file wins.
13+
* A retired value of the wrong type is not copied: it would fail the whole file's decode, and a
14+
* failed decode resets every setting to its default.
15+
*/
16+
export function renameRetiredAutoUpdateKeys(parsed: unknown): unknown {
17+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return parsed;
18+
const stored = parsed as Readonly<Record<string, unknown>>;
19+
let renamed: Record<string, unknown> | undefined;
20+
for (const { retired, current, type } of RETIRED_KEYS) {
21+
const value = stored[retired];
22+
if (stored[current] !== undefined || typeof value !== type) continue;
23+
renamed = { ...(renamed ?? stored), [current]: value };
24+
}
25+
return renamed ?? parsed;
26+
}

‎apps/server/src/cloud/selfUpdate.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ export class ServerSelfUpdate extends Context.Service<
7272
requestId: string,
7373
onHandoffAccepted?: () => Effect.Effect<void>,
7474
) => Effect.Effect<never, ServerSelfUpdateError>;
75+
/** The launcher accepted a handoff but this process is still running past it: allow the
76+
* next update (the button's or an automatic one) to start. */
77+
readonly resetAfterDroppedHandoff: Effect.Effect<void>;
7578
}
7679
>()("t3/cloud/selfUpdate/ServerSelfUpdate") {}
7780

@@ -349,6 +352,7 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* () {
349352

350353
return ServerSelfUpdate.of({
351354
update,
355+
resetAfterDroppedHandoff: Ref.set(inFlight, false),
352356
commitDesktopUpdate: Effect.fn("ServerSelfUpdate.commitDesktopUpdate")(
353357
function (requestId, onHandoffAccepted) {
354358
return desktopAppUpdate.commit(requestId, onHandoffAccepted);

‎apps/server/src/orchestration-v2/EffectWorker.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import * as ThreadTitleRegenerationService from "./ThreadTitleRegenerationServic
2828
import * as ThreadManagementService from "./ThreadManagementService.ts";
2929
import * as ServerSettings from "../serverSettings.ts";
3030
import { continueRestartedRun } from "./RestartContinuation.ts";
31+
import { WorkAdmission } from "../workAdmission.ts";
3132

3233
const isProviderTurnControlError = Schema.is(ProviderTurnControlService.ProviderTurnControlError);
3334

@@ -948,6 +949,9 @@ export const runDaemonWithOptions = (options: OrchestrationEffectDaemonOptions =
948949
Effect.scoped(
949950
Effect.gen(function* () {
950951
const worker = yield* OrchestrationEffectWorkerV2;
952+
const admission = yield* WorkAdmission;
953+
// Held effects stay pending, unclaimed: startup recovery decides what they become.
954+
yield* admission.register("outbox");
951955
const requestedConcurrency = options.concurrency ?? DEFAULT_EFFECT_WORKER_CONCURRENCY;
952956
const concurrency = Number.isFinite(requestedConcurrency)
953957
? Math.max(1, Math.floor(requestedConcurrency))
@@ -962,7 +966,7 @@ export const runDaemonWithOptions = (options: OrchestrationEffectDaemonOptions =
962966
// Notifications wake promptly; availableAt persists retry timing, and liveness polling recovers missed wakeups.
963967
const runWorker = Effect.gen(function* () {
964968
while (true) {
965-
const outcome = yield* worker.runOnce(claimLimits).pipe(
969+
const outcome = yield* admission.admit(worker.runOnce(claimLimits)).pipe(
966970
Effect.map((worked) => (worked ? ("worked" as const) : ("idle" as const))),
967971
Effect.catchCause((cause) =>
968972
Effect.logWarning("Orchestration effect worker failed", cause).pipe(

0 commit comments

Comments
 (0)