The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 22:15 UTC
fix(server): migrate compatible legacy service install state (#19449)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
James C committed
Nov 11, 2026 at 21:22 UTC
074b452d29e7939bf54dcd7a39e31ae6b3d40c60
3 files changed
+144
-3
apps/server/src/cloud/serviceProtocol.ts
+18
@@ -203,6 +203,24 @@ export function parseServiceState(value: string): ServiceState | undefined {
203
}
204
}
205
206
+export function parseLegacyServiceState(value: string) {
207
+ try {
208
+ const parsed: unknown = JSON.parse(value);
209
+ if (
210
+ !isRecord(parsed) ||
211
+ Object.keys(parsed).length !== 2 ||
212
+ parsed.protocol !== 2 ||
213
+ typeof parsed.activeVersion !== "string" ||
214
+ !isExactServiceVersion(parsed.activeVersion)
215
+ ) {
216
+ return undefined;
217
+ }
218
+ return { protocol: 2, activeVersion: parsed.activeVersion } as const;
219
+ } catch {
220
+ return undefined;
221
+ }
222
+}
223
+
224
export function serviceStateHasPendingUpdate(value: string): boolean {
225
try {
226
const parsed: unknown = JSON.parse(value);
apps/server/src/serviceLauncher.test.ts
+107
-1
@@ -7,7 +7,7 @@ import * as NodeFSP from "node:fs/promises";
7
import * as NodeSqlite from "node:sqlite";
8
9
import * as NodeServices from "@effect/platform-node/NodeServices";
10
-import { assert, it } from "@effect/vitest";
10
+import { assert, expect, it } from "@effect/vitest";
11
import { SERVER_EXIT_CODE_STATE_DIR_OWNED } from "@t3tools/contracts";
12
import * as HostProcess from "@t3tools/shared/HostProcess";
13
import * as Effect from "effect/Effect";
@@ -19,6 +19,7 @@ import { renderBootServicePlist, renderBootServiceUnit } from "./cloud/bootServi
19
import {
20
databaseBackupBlocker,
21
Launcher,
22
+ main,
23
readServiceEnvironment,
24
type BackupSpaceProbe,
25
readServiceState,
@@ -139,6 +140,15 @@ const writeFakeRuntime = (
140
return entryPath;
141
});
142
143
+const runLauncher = async (root: string) => {
144
+ vi.stubEnv("T3CODE_HOME", root);
145
+ try {
146
+ await main();
147
+ } finally {
148
+ vi.unstubAllEnvs();
149
+ }
150
+};
151
+
152
const fakeServiceUnit = (
153
fs: FileSystem.FileSystem,
154
path: Path.Path,
@@ -212,6 +222,102 @@ it.layer(NodeServices.layer)("service state persistence", (it) => {
222
}),
223
);
224
225
+ it.effect("adopts a legacy install record without rewriting it", () =>
226
+ Effect.gen(function* () {
227
+ const fs = yield* FileSystem.FileSystem;
228
+ const path = yield* Path.Path;
229
+ const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-launcher-migrate-" });
230
+ const statePath = path.join(root, "runtime", "service-state.json");
231
+ const activeVersion = "0.0.43-nightly.20260919.1962+build.1";
232
+ const versionDir = path.join(root, "runtime", "versions", activeVersion);
233
+ yield* writeFakeRuntime(
234
+ fs,
235
+ path,
236
+ versionDir,
237
+ `
238
+import { writeFileSync } from "node:fs";
239
+writeFileSync(new URL("./started", import.meta.url), "");
240
+process.exit(0);
241
+`,
242
+ );
243
+ const legacyContents = `{ "protocol": 2, "activeVersion": "${activeVersion}" }`;
244
+ yield* fs.writeFileString(statePath, legacyContents);
245
+ const restartPending = path.join(root, "runtime", ".restart-pending");
246
+ yield* fs.writeFileString(restartPending, `${activeVersion}\n`);
247
+
248
+ yield* Effect.promise(() =>
249
+ expect(runLauncher(root)).rejects.toThrow("Active child exited unexpectedly (0)."),
250
+ );
251
+
252
+ assert.isTrue(yield* fs.exists(path.join(versionDir, "started")));
253
+ assert.isFalse(yield* fs.exists(restartPending));
254
+ assert.equal(yield* fs.readFileString(statePath), legacyContents);
255
+ }),
256
+ );
257
+
258
+ it.effect("refuses non-install legacy documents without rewriting or spawning", () =>
259
+ Effect.gen(function* () {
260
+ const fs = yield* FileSystem.FileSystem;
261
+ const path = yield* Path.Path;
262
+ const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-launcher-reject-" });
263
+ const statePath = path.join(root, "runtime", "service-state.json");
264
+ yield* writeFakeRuntime(
265
+ fs,
266
+ path,
267
+ path.join(root, "runtime", "versions", "1.0.0"),
268
+ "process.exit(0);\n",
269
+ );
270
+ const legacy = { protocol: 2, activeVersion: "1.0.0" };
271
+ const childCount = spawnedChildren.length;
272
+ for (const contents of [
273
+ JSON.stringify({
274
+ ...legacy,
275
+ update: {
276
+ id: "u",
277
+ fromVersion: "1.0.0",
278
+ targetVersion: "1.1.0",
279
+ dbPath: "/tmp/state.sqlite",
280
+ status: "pending",
281
+ },
282
+ }),
283
+ JSON.stringify({ ...legacy, update: null }),
284
+ JSON.stringify({ ...legacy, extra: true }),
285
+ JSON.stringify({ ...legacy, protocol: 1 }),
286
+ JSON.stringify({ ...legacy, protocol: 5 }),
287
+ JSON.stringify({ ...legacy, activeVersion: "latest" }),
288
+ "{ invalid JSON",
289
+ ]) {
290
+ yield* fs.writeFileString(statePath, contents);
291
+ yield* Effect.promise(() =>
292
+ expect(runLauncher(root)).rejects.toThrow("Service state is invalid or unsupported."),
293
+ );
294
+ assert.equal(yield* fs.readFileString(statePath), contents);
295
+ assert.equal(spawnedChildren.length, childCount);
296
+ }
297
+ }),
298
+ );
299
+
300
+ it.effect("refuses a legacy state whose runtime is missing without rewriting or spawning", () =>
301
+ Effect.gen(function* () {
302
+ const fs = yield* FileSystem.FileSystem;
303
+ const path = yield* Path.Path;
304
+ const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-launcher-layout-" });
305
+ const statePath = path.join(root, "runtime", "service-state.json");
306
+ const contents = '{ "protocol": 2, "activeVersion": "1.0.0" }';
307
+ yield* fs.makeDirectory(path.dirname(statePath), { recursive: true });
308
+ yield* fs.writeFileString(statePath, contents);
309
+ const childCount = spawnedChildren.length;
310
+
311
+ yield* Effect.promise(() =>
312
+ expect(runLauncher(root)).rejects.toThrow(
313
+ "Selected t3@1.0.0 runtime is missing or incomplete.",
314
+ ),
315
+ );
316
+ assert.equal(yield* fs.readFileString(statePath), contents);
317
+ assert.equal(spawnedChildren.length, childCount);
318
+ }),
319
+ );
320
+
321
it.effect("a fresh launcher clears a restart deferred by t3 update", () =>
322
Effect.gen(function* () {
323
const fs = yield* FileSystem.FileSystem;
apps/server/src/serviceLauncher.ts
+19
-2
@@ -21,6 +21,7 @@ import {
21
compareExactServiceVersions,
22
decodeServiceLauncherChildMessage,
23
isExactServiceVersion,
24
+ parseLegacyServiceState,
25
parseServiceState,
26
serviceUnitBaseDirOf,
27
SERVICE_ENV_FILE,
@@ -514,11 +515,19 @@ async function discardDatabaseBackup(baseDir: string, updateId: string): Promise
515
516
export async function readServiceState(filePath: string): Promise<ServiceState> {
517
const contents = await NodeFSP.readFile(filePath, "utf8");
517
- const state = parseServiceState(contents);
518
+ const state = parseServiceState(contents) ?? adoptLegacyServiceState(contents);
519
if (state === undefined) throw new Error("Service state is invalid or unsupported.");
520
return state;
521
}
522
523
+// An older coordinator can write protocol 2 beside a newer runtime. Adopt
524
+// only its install record, in memory: rewriting it outside the selection lock
525
+// could replace state a concurrent install wrote after this read.
526
+function adoptLegacyServiceState(contents: string): ServiceState | undefined {
527
+ const legacy = parseLegacyServiceState(contents);
528
+ return legacy && { protocol: SERVICE_LAUNCHER_PROTOCOL, activeVersion: legacy.activeVersion };
529
+}
530
+
531
export async function writeServiceState(filePath: string, state: ServiceState): Promise<void> {
532
await NodeFSP.mkdir(NodePath.dirname(filePath), { recursive: true, mode: 0o700 });
533
await replaceFileDurably(filePath, `${JSON.stringify(state, null, 2)}\n`);
@@ -1106,7 +1115,15 @@ export async function main(): Promise<void> {
1115
}
1116
const environment = await readServiceEnvironment(baseDir);
1117
const statePath = NodePath.join(baseDir, "runtime", SERVICE_STATE_FILE);
1109
- const state = await readServiceState(statePath);
1118
+ const contents = await NodeFSP.readFile(statePath, "utf8");
1119
+ let state = parseServiceState(contents);
1120
+ if (state === undefined) {
1121
+ state = adoptLegacyServiceState(contents);
1122
+ if (state === undefined) throw new Error("Service state is invalid or unsupported.");
1123
+ if (!(await runtimeExists(baseDir, state.activeVersion))) {
1124
+ throw new Error(`Selected t3@${state.activeVersion} runtime is missing or incomplete.`);
1125
+ }
1126
+ }
1127
const serviceUnit = serviceUnitFromEnvironment();
1128
await new Launcher(
1129
baseDir,