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 22, 09:30 UTC
fix(server): t3 update no longer deletes the runtime its launcher runs (#24289)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Victor Vogel committed
Oct 31, 2026 at 21:30 UTC
846de873a10fc65a0d2b68123ff9715551373803
4 files changed
+266
-11
apps/server/src/cli/update.test.ts
+174
-2
@@ -1,15 +1,36 @@
1
import * as NodeServices from "@effect/platform-node/NodeServices";
2
import { assert, it } from "@effect/vitest";
3
import * as HostProcess from "@t3tools/shared/HostProcess";
4
+import * as Duration from "effect/Duration";
5
import * as Effect from "effect/Effect";
6
+import * as Exit from "effect/Exit";
7
import * as FileSystem from "effect/FileSystem";
8
import * as Option from "effect/Option";
9
import * as Path from "effect/Path";
10
+import * as TestClock from "effect/testing/TestClock";
11
import { HttpClient, HttpClientResponse } from "effect/http";
12
+import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
13
import { afterEach, vi } from "vite-plus/test";
14
15
+import { prunePinnedRuntimes } from "../cloud/pinnedRuntime.ts";
16
+import * as ProcessRunner from "../processRunner.ts";
17
import { repointLauncher, resolveLauncherPath, resolveNewestVersion } from "./update.ts";
18
19
+// lsof listing one process, so prune sees a process list in which no runtime is running.
20
+const unrelatedLsof = ProcessRunner.ProcessRunner.of({
21
+ run: () =>
22
+ Effect.succeed({
23
+ stdout: "p1\nftxt\nn/usr/bin/true\n",
24
+ stderr: "",
25
+ code: ChildProcessSpawner.ExitCode(0),
26
+ timedOut: false,
27
+ stdoutTruncated: false,
28
+ stderrTruncated: false,
29
+ stdoutInvalidUtf8: false,
30
+ stderrInvalidUtf8: false,
31
+ }),
32
+});
33
+
34
afterEach(() => vi.restoreAllMocks());
35
36
it.effect("authenticates each release page until the requested channel is found", () =>
@@ -53,7 +74,7 @@ it.layer(NodeServices.layer)("t3 update launcher", (it) => {
74
75
const repointed = yield* repointLauncher({
76
launchedAs: launcher,
56
- versionsDir: path.join(root, "runtime/versions"),
77
+ baseDir: root,
78
targetEntryPath: newExe,
79
});
80
@@ -62,6 +83,157 @@ it.layer(NodeServices.layer)("t3 update launcher", (it) => {
83
}).pipe(Effect.scoped, Effect.provideService(HostProcess.Platform, "linux")),
84
);
85
86
+ it.effect.each([
87
+ { name: "no launcher records", recorded: undefined },
88
+ { name: "an empty launcher record", recorded: "" },
89
+ { name: "whole launcher records", recorded: "/opt/t3\n" },
90
+ { name: "Windows launcher records", recorded: "C:\\t3.cmd\r\n" },
91
+ // An interrupted append cut this record short.
92
+ { name: "an unterminated launcher record", recorded: "/opt/t" },
93
+ // Another process's append is cut short after t3 update reads the records.
94
+ { name: "a record cut short after the read", recorded: "/opt/t3\n", cut: "/opt/t" },
95
+ ])("keeps the runtime a repointed launcher off PATH points at with $name", ({ recorded, cut }) =>
96
+ Effect.gen(function* () {
97
+ const fs = yield* FileSystem.FileSystem;
98
+ const path = yield* Path.Path;
99
+ const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-update-" });
100
+ yield* TestClock.setTime(Duration.toMillis(Duration.days(1)));
101
+ for (const version of ["1.0.0", "2.0.0"]) {
102
+ const directory = path.join(root, "runtime/versions", version);
103
+ yield* fs.makeDirectory(directory, { recursive: true });
104
+ yield* fs.writeFileString(path.join(directory, "t3"), "");
105
+ yield* fs.writeFileString(path.join(directory, ".install-complete"), `${version}\n`);
106
+ yield* fs.utimes(path.join(directory, ".install-complete"), 0, 0);
107
+ }
108
+ if (recorded !== undefined) {
109
+ yield* fs.writeFileString(path.join(root, "runtime/launchers"), recorded);
110
+ }
111
+ // A launcher in the T3 home itself, which neither PATH nor the default bin dir reaches.
112
+ const launcher = path.join(root, "t3");
113
+ yield* fs.symlink(path.join(root, "runtime/versions/1.0.0/t3"), launcher);
114
+
115
+ const launchersFile = path.join(root, "runtime/launchers");
116
+ yield* repointLauncher({
117
+ launchedAs: launcher,
118
+ baseDir: root,
119
+ targetEntryPath: path.join(root, "runtime/versions/2.0.0/t3"),
120
+ }).pipe(
121
+ Effect.provideService(FileSystem.FileSystem, {
122
+ ...fs,
123
+ readFileString: (file, encoding) =>
124
+ fs
125
+ .readFileString(file, encoding)
126
+ .pipe(
127
+ Effect.tap(() =>
128
+ cut === undefined || file !== launchersFile
129
+ ? Effect.void
130
+ : fs.writeFileString(file, cut, { flag: "a" }),
131
+ ),
132
+ ),
133
+ }),
134
+ );
135
+ // A later server start prunes with no idea how this machine launches t3.
136
+ const removed = yield* prunePinnedRuntimes({ baseDir: root, keep: [] }).pipe(
137
+ Effect.provideService(ProcessRunner.ProcessRunner, unrelatedLsof),
138
+ Effect.provideService(HostProcess.Platform, "darwin"),
139
+ Effect.provideService(HostProcess.Environment, { PATH: "" }),
140
+ Effect.provideService(HostProcess.HomeDirectory, root),
141
+ );
142
+
143
+ assert.deepStrictEqual(removed, ["1.0.0"]);
144
+ // macOS temp dirs sit behind the /var -> /private/var symlink, so compare canonical paths.
145
+ assert.equal(
146
+ yield* fs.realPath(launcher),
147
+ yield* fs.realPath(path.join(root, "runtime/versions/2.0.0/t3")),
148
+ );
149
+ }).pipe(Effect.scoped, Effect.provideService(HostProcess.Platform, "linux")),
150
+ );
151
+
152
+ const launcherFixture = Effect.fnUntraced(function* () {
153
+ const fs = yield* FileSystem.FileSystem;
154
+ const path = yield* Path.Path;
155
+ const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-update-" });
156
+ const oldExe = path.join(root, "runtime/versions/1.0.0/t3");
157
+ const newExe = path.join(root, "runtime/versions/2.0.0/t3");
158
+ const launcher = path.join(root, "t3");
159
+ for (const file of [oldExe, newExe]) {
160
+ yield* fs.makeDirectory(path.dirname(file), { recursive: true });
161
+ yield* fs.writeFileString(file, "");
162
+ }
163
+ yield* fs.symlink(oldExe, launcher);
164
+ return { root, oldExe, newExe, launcher, launchersFile: path.join(root, "runtime/launchers") };
165
+ });
166
+
167
+ it.effect("records a relative launcher path as absolute", () =>
168
+ Effect.gen(function* () {
169
+ const fs = yield* FileSystem.FileSystem;
170
+ const path = yield* Path.Path;
171
+ const { root, newExe, launcher, launchersFile } = yield* launcherFixture();
172
+
173
+ yield* repointLauncher({
174
+ launchedAs: path.relative(path.resolve("."), launcher),
175
+ baseDir: root,
176
+ targetEntryPath: newExe,
177
+ });
178
+
179
+ assert.equal(yield* fs.readFileString(launchersFile), `\n${launcher}\n`);
180
+ }).pipe(Effect.scoped, Effect.provideService(HostProcess.Platform, "linux")),
181
+ );
182
+
183
+ it.effect("fails instead of repointing a launcher it cannot record", () =>
184
+ Effect.gen(function* () {
185
+ const fs = yield* FileSystem.FileSystem;
186
+ const { root, oldExe, newExe, launcher, launchersFile } = yield* launcherFixture();
187
+ // Appending to a directory fails the way a read-only or full disk would.
188
+ yield* fs.makeDirectory(launchersFile);
189
+
190
+ const result = yield* repointLauncher({
191
+ launchedAs: launcher,
192
+ baseDir: root,
193
+ targetEntryPath: newExe,
194
+ }).pipe(Effect.exit);
195
+
196
+ assert.isTrue(Exit.isFailure(result));
197
+ assert.equal(yield* fs.readLink(launcher), oldExe);
198
+ }).pipe(Effect.scoped, Effect.provideService(HostProcess.Platform, "linux")),
199
+ );
200
+
201
+ it.effect.each(["recording", "repointing"] as const)(
202
+ "never leaves a repointed launcher unrecorded when t3 update dies while %s",
203
+ (step) =>
204
+ Effect.gen(function* () {
205
+ const fs = yield* FileSystem.FileSystem;
206
+ const { root, newExe, launcher, launchersFile } = yield* launcherFixture();
207
+
208
+ yield* repointLauncher({
209
+ launchedAs: launcher,
210
+ baseDir: root,
211
+ targetEntryPath: newExe,
212
+ }).pipe(
213
+ Effect.provideService(FileSystem.FileSystem, {
214
+ ...fs,
215
+ writeFileString: (path, data, options) =>
216
+ step === "recording" && path === launchersFile
217
+ ? Effect.die("t3 update was killed")
218
+ : fs.writeFileString(path, data, options),
219
+ rename: (from, to) =>
220
+ step === "repointing" && to === launcher
221
+ ? Effect.die("t3 update was killed")
222
+ : fs.rename(from, to),
223
+ }),
224
+ Effect.exit,
225
+ );
226
+
227
+ const repointed = (yield* fs.readLink(launcher)) === newExe;
228
+ const recorded = (yield* fs
229
+ .readFileString(launchersFile)
230
+ .pipe(Effect.orElseSucceed(() => "")))
231
+ .split("\n")
232
+ .includes(launcher);
233
+ assert.isFalse(repointed && !recorded);
234
+ }).pipe(Effect.scoped, Effect.provideService(HostProcess.Platform, "linux")),
235
+ );
236
+
237
it.effect("leaves a plain copy or a foreign symlink alone", () =>
238
Effect.gen(function* () {
239
const fs = yield* FileSystem.FileSystem;
@@ -85,7 +257,7 @@ it.layer(NodeServices.layer)("t3 update launcher", (it) => {
257
for (const launchedAs of [copy, foreign, otherLauncher, undefined]) {
258
const repointed = yield* repointLauncher({
259
launchedAs,
88
- versionsDir: path.join(root, "runtime/versions"),
260
+ baseDir: root,
261
targetEntryPath: newExe,
262
});
263
assert.equal(repointed._tag, "None", launchedAs ?? "undefined");
apps/server/src/cli/update.ts
+16
-5
@@ -26,7 +26,9 @@ import {
26
pinnedRuntimeCommand,
27
PinnedRuntimeInstallError,
28
pinnedRuntimePaths,
29
+ pinnedRuntimeVersionsDir,
30
prunePinnedRuntimes,
31
+ recordPinnedRuntimeLauncher,
32
windowsShimTarget,
33
} from "../cloud/pinnedRuntime.ts";
34
import { compareExactServiceVersions, isExactServiceVersion } from "../cloud/serviceProtocol.ts";
@@ -92,19 +94,26 @@ export const resolveNewestVersion = Effect.fn("cli.update.resolve_newest")(funct
94
95
/**
96
* Only a launcher that already points into this home's `runtime/versions` tree is touched; a
95
- * plain copy of the executable, or a launcher for some other install, is left alone.
97
+ * plain copy of the executable, or a launcher for some other install, is left alone. It is
98
+ * recorded before it moves, so prune never meets a repointed launcher it does not know about.
99
*/
100
export const repointLauncher = Effect.fn("cli.update.repoint_launcher")(function* (input: {
101
readonly launchedAs: string | undefined;
99
- readonly versionsDir: string;
102
+ readonly baseDir: string;
103
readonly targetEntryPath: string;
104
}) {
105
const fs = yield* FileSystem.FileSystem;
106
const path = yield* Path.Path;
107
const platform = yield* HostProcess.Platform;
108
if (input.launchedAs === undefined) return Option.none<string>();
106
- const ownsTarget = (candidate: string) =>
107
- launcherOwnsVersionsDir(path, input.versionsDir, candidate);
109
+ const versionsDir = pinnedRuntimeVersionsDir(path, input.baseDir);
110
+ const ownsTarget = (candidate: string) => launcherOwnsVersionsDir(path, versionsDir, candidate);
111
+ const remember = (launcher: string) =>
112
+ recordPinnedRuntimeLauncher({ baseDir: input.baseDir, launcher }).pipe(
113
+ Effect.mapError(
114
+ () => new CliUpdateError({ reason: `Could not record the t3 launcher at ${launcher}.` }),
115
+ ),
116
+ );
117
118
if (platform === "win32") {
119
const shimPath = yield* findWindowsShim(input.launchedAs);
@@ -112,6 +121,7 @@ export const repointLauncher = Effect.fn("cli.update.repoint_launcher")(function
121
const current = yield* fs.readFileString(shimPath).pipe(Effect.option);
122
const quoted = Option.isSome(current) ? windowsShimTarget(current.value) : undefined;
123
if (quoted === undefined || !ownsTarget(quoted)) return Option.none<string>();
124
+ yield* remember(shimPath);
125
yield* fs
126
.writeFileString(shimPath, `@echo off\r\n"${input.targetEntryPath}" %*`)
127
.pipe(
@@ -126,6 +136,7 @@ export const repointLauncher = Effect.fn("cli.update.repoint_launcher")(function
136
if (Option.isNone(linkTarget)) return Option.none<string>();
137
const resolvedTarget = path.resolve(path.dirname(input.launchedAs), linkTarget.value);
138
if (!ownsTarget(resolvedTarget)) return Option.none<string>();
139
+ yield* remember(input.launchedAs);
140
const tempLink = `${input.launchedAs}.${process.pid}.tmp`;
141
yield* fs.symlink(input.targetEntryPath, tempLink).pipe(
142
Effect.andThen(fs.rename(tempLink, input.launchedAs)),
@@ -472,7 +483,7 @@ const runUpdate = Effect.fn("cli.update.run")(function* (input: {
483
const launchedAs = (yield* HostProcess.IsExecutable) ? yield* resolveLauncherPath : undefined;
484
const repointed = yield* repointLauncher({
485
launchedAs,
475
- versionsDir: path.dirname(runtime.versionDir),
486
+ baseDir: input.baseDir,
487
targetEntryPath: runtime.entryPath,
488
});
489
apps/server/src/cloud/pinnedRuntime.test.ts
+47
-1
@@ -409,7 +409,7 @@ it.layer(NodeServices.layer)("prunePinnedRuntimes", (it) => {
409
// The launcher found on PATH is remembered for a service that cannot see it.
410
assert.equal(
411
yield* fs.readFileString(path.join(baseDir, "runtime", "launchers")),
412
- `${path.join(customBinDir, "t3")}\n${path.join(binDir, "t3")}\n`,
412
+ `${path.join(customBinDir, "t3")}\n\n${path.join(binDir, "t3")}\n`,
413
);
414
assert.deepEqual((yield* fs.readDirectory(versionsDir)).toSorted(), [
415
".staging-abc",
@@ -428,6 +428,52 @@ it.layer(NodeServices.layer)("prunePinnedRuntimes", (it) => {
428
}),
429
);
430
431
+ it.effect.each([
432
+ // An interrupted append cut this record short.
433
+ { name: "an unterminated launcher record", recorded: "/opt/t", cut: "" },
434
+ // Another process's append is cut short after prune reads the records.
435
+ { name: "a record cut short after the read", recorded: "/opt/t3\n", cut: "/opt/t" },
436
+ ])("remembers a launcher found on PATH after $name", ({ recorded, cut }) =>
437
+ Effect.gen(function* () {
438
+ const fs = yield* FileSystem.FileSystem;
439
+ const path = yield* Path.Path;
440
+ const { baseDir, versionsDir } = yield* setup(["1.0.0", "1.1.0"]);
441
+ const binDir = path.join(baseDir, "custom-bin");
442
+ yield* fs.makeDirectory(binDir);
443
+ yield* fs.symlink(path.join(versionsDir, "1.0.0", "t3"), path.join(binDir, "t3"));
444
+ const launchersFile = path.join(baseDir, "runtime", "launchers");
445
+ yield* fs.writeFileString(launchersFile, recorded);
446
+ const prune = (environment: Record<string, string>) =>
447
+ prunePinnedRuntimes({ baseDir, keep: [] }).pipe(
448
+ Effect.provideService(ProcessRunner.ProcessRunner, lsofRunner("p1\nftxt\nn/bin/sh\n")),
449
+ Effect.provideService(HostProcess.Platform, "darwin"),
450
+ Effect.provideService(HostProcess.Environment, environment),
451
+ Effect.provideService(HostProcess.HomeDirectory, baseDir),
452
+ );
453
+
454
+ yield* prune({ PATH: binDir }).pipe(
455
+ Effect.provideService(FileSystem.FileSystem, {
456
+ ...fs,
457
+ readFileString: (file, encoding) =>
458
+ fs
459
+ .readFileString(file, encoding)
460
+ .pipe(
461
+ Effect.tap(() =>
462
+ file === launchersFile
463
+ ? fs.writeFileString(file, cut, { flag: "a" })
464
+ : Effect.void,
465
+ ),
466
+ ),
467
+ }),
468
+ );
469
+ // A service that cannot see the interactive PATH.
470
+ const removed = yield* prune({});
471
+
472
+ assert.deepEqual(removed, []);
473
+ assert.isTrue(yield* fs.exists(path.join(versionsDir, "1.0.0", "t3")));
474
+ }),
475
+ );
476
+
477
it.effect("matches a Windows shim whatever its casing", () =>
478
Effect.gen(function* () {
479
const fs = yield* FileSystem.FileSystem;
apps/server/src/cloud/pinnedRuntime.ts
+29
-3
@@ -377,6 +377,13 @@ const PINNED_RUNTIME_PRUNE_GRACE = Duration.hours(1);
377
*/
378
const PINNED_RUNTIME_LAUNCHERS_FILE = "launchers";
379
380
+/**
381
+ * Starts on a fresh line whatever the file ends with when the append lands: another process's
382
+ * append may have been cut short since this one read the file. Readers skip the blank lines.
383
+ */
384
+const launcherRecords = (launchers: ReadonlyArray<string>) =>
385
+ `\n${launchers.map((launcher) => `${launcher}\n`).join("")}`;
386
+
387
export function launcherOwnsVersionsDir(
388
path: Path.Path,
389
versionsDir: string,
@@ -386,6 +393,27 @@ export function launcherOwnsVersionsDir(
393
return relative.length > 0 && !relative.startsWith("..") && !path.isAbsolute(relative);
394
}
395
396
+/**
397
+ * `t3 update` repoints the launcher it was started through, which may be on neither PATH nor the
398
+ * default bin dir. Unrecorded, the next prune would remove the runtime it now points at.
399
+ */
400
+export const recordPinnedRuntimeLauncher = Effect.fn("cloud.pinned_runtime.record_launcher")(
401
+ function* (input: { readonly baseDir: string; readonly launcher: string }) {
402
+ const fs = yield* FileSystem.FileSystem;
403
+ const path = yield* Path.Path;
404
+ const launchersFile = path.join(
405
+ input.baseDir,
406
+ PINNED_RUNTIME_DIR,
407
+ PINNED_RUNTIME_LAUNCHERS_FILE,
408
+ );
409
+ // Prune may run in another process, such as the service, whose working directory differs.
410
+ const launcher = path.resolve(input.launcher);
411
+ const recorded = yield* fs.readFileString(launchersFile).pipe(Effect.orElseSucceed(() => ""));
412
+ if (recorded.split(/\r?\n/).includes(launcher)) return;
413
+ yield* fs.writeFileString(launchersFile, launcherRecords([launcher]), { flag: "a" });
414
+ },
415
+);
416
+
417
/** The executable a `t3.cmd` shim written by the install script or `t3 update` runs. */
418
export const windowsShimTarget = (contents: string) => /^"([^"]+)"/m.exec(contents)?.[1];
419
@@ -437,9 +465,7 @@ const shellLauncherTargets = Effect.fn("cloud.pinned_runtime.shell_launcher_targ
465
}
466
if (unrecorded.length > 0) {
467
yield* fs
440
- .writeFileString(launchersFile, unrecorded.map((launcher) => `${launcher}\n`).join(""), {
441
- flag: "a",
442
- })
468
+ .writeFileString(launchersFile, launcherRecords(unrecorded), { flag: "a" })
469
.pipe(Effect.ignore);
470
}
471
return targets;