The best way to control Claude Code, Codex and more from any computer or phone

main
ts 947 lines 38.2 KB
Raw
1 // @effect-diagnostics nodeBuiltinImport:off - node:path.win32 stands in for a Windows host in one test.
2 import * as NodeServices from "@effect/platform-node/NodeServices";
3 import { assert, it } from "@effect/vitest";
4 import * as Deferred from "effect/Deferred";
5 import * as Duration from "effect/Duration";
6 import * as Effect from "effect/Effect";
7 import * as FileSystem from "effect/FileSystem";
8 import * as Fiber from "effect/Fiber";
9 import * as Path from "effect/Path";
10 import * as Stream from "effect/Stream";
11 import * as NodePath from "node:path";
12 import * as TestClock from "effect/testing/TestClock";
13 import { HttpClient, HttpClientError, HttpClientResponse } from "effect/http";
14 import * as ChildProcess from "effect/process/ChildProcess";
15 import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
16
17 import * as HostProcess from "@t3tools/shared/HostProcess";
18
19 import * as ProcessRunner from "../processRunner.ts";
20 import {
21 isVersionExecutable,
22 ensurePinnedRuntimeInstalled,
23 isUnpublishedRelease,
24 pinnedRuntimeCommand,
25 pinnedRuntimePaths,
26 PinnedRuntimeInstallError,
27 type PinnedRuntimeProgress,
28 prunePinnedRuntimes,
29 } from "./pinnedRuntime.ts";
30 import { SERVICE_LAUNCHER_PROTOCOL } from "./serviceProtocol.ts";
31
32 const version = "1.2.3";
33 const archiveName = `t3-${version}-linux-x64.tar.gz`;
34 const archiveBytes = new TextEncoder().encode("not really a tarball");
35 const archiveHex = (bytes: Uint8Array) =>
36 Effect.promise(() => crypto.subtle.digest("SHA-256", bytes)).pipe(
37 Effect.map((digest) =>
38 Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, "0")).join(""),
39 ),
40 );
41 const validChecksums = archiveHex(archiveBytes).pipe(
42 Effect.map((hex) => `${hex} ${archiveName}\n`),
43 );
44 const releaseHttpClient = (checksums: string, requests: string[] = []) =>
45 HttpClient.make((request) => {
46 requests.push(request.url);
47 const body = new URL(request.url).pathname.endsWith("/SHA256SUMS") ? checksums : archiveBytes;
48 return Effect.succeed(HttpClientResponse.fromWeb(request, new Response(body)));
49 });
50 const extractingRunner = (fs: FileSystem.FileSystem, path: Path.Path, commands: string[] = []) =>
51 ProcessRunner.ProcessRunner.of({
52 run: (input) =>
53 Effect.gen(function* () {
54 commands.push(input.command);
55 const targetIndex = input.args.indexOf("-C");
56 const stagingDir = input.args[targetIndex + 1];
57 if (input.command !== "tar" || stagingDir === undefined) {
58 return yield* Effect.die(`unexpected command ${input.command}`);
59 }
60 yield* fs.writeFileString(path.join(stagingDir, "t3"), "#!/bin/sh\n").pipe(Effect.orDie);
61 return {
62 stdout: "",
63 stderr: "",
64 code: ChildProcessSpawner.ExitCode(0),
65 timedOut: false,
66 stdoutTruncated: false,
67 stderrTruncated: false,
68 stdoutInvalidUtf8: false,
69 stderrInvalidUtf8: false,
70 };
71 }),
72 });
73
74 it.layer(NodeServices.layer)("ensurePinnedRuntimeInstalled", (it) => {
75 it.effect("installs the verified release archive as the runtime executable", () =>
76 Effect.gen(function* () {
77 const fs = yield* FileSystem.FileSystem;
78 const path = yield* Path.Path;
79 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-archive-" });
80 const requests: string[] = [];
81 const commands: string[] = [];
82 const paths = yield* ensurePinnedRuntimeInstalled({
83 baseDir,
84 version,
85 fs,
86 path,
87 platform: "linux",
88 arch: "x64",
89 httpClient: releaseHttpClient(yield* validChecksums, requests),
90 releaseBaseUrl: "https://releases.example/download",
91 runner: extractingRunner(fs, path, commands),
92 validate: (staging) =>
93 fs.exists(staging.entryPath).pipe(
94 Effect.flatMap((exists) => (exists ? Effect.void : Effect.die("missing runtime"))),
95 Effect.orDie,
96 ),
97 });
98 assert.equal(paths.entryPath, path.join(paths.versionDir, "t3"));
99 assert.deepEqual(pinnedRuntimeCommand(paths), { command: paths.entryPath, args: [] });
100 assert.deepEqual(requests, [
101 `https://releases.example/download/v${version}/SHA256SUMS`,
102 `https://releases.example/download/v${version}/${archiveName}`,
103 ]);
104 assert.deepEqual(commands, ["tar"]);
105 assert.equal(yield* fs.readFileString(paths.sentinelPath), `${version}\n`);
106 assert.isFalse(yield* fs.exists(path.join(paths.versionDir, "t3-runtime-archive")));
107 }),
108 );
109
110 it.effect("cleans up an interrupted download without reporting verification or extraction", () =>
111 Effect.gen(function* () {
112 const fs = yield* FileSystem.FileSystem;
113 const path = yield* Path.Path;
114 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-progress-failed-" });
115 const checksums = yield* validChecksums;
116 const progress: PinnedRuntimeProgress[] = [];
117 let cancelled = false;
118 const client = HttpClient.make((request) =>
119 Effect.succeed(
120 HttpClientResponse.fromWeb(
121 request,
122 request.url.endsWith("/SHA256SUMS")
123 ? new Response(checksums)
124 : new Response(
125 new ReadableStream({
126 start(controller) {
127 controller.enqueue(archiveBytes.slice(0, 4));
128 },
129 cancel() {
130 cancelled = true;
131 },
132 }),
133 ),
134 ),
135 ),
136 );
137 const firstChunk = yield* Deferred.make<void>();
138 const install = yield* ensurePinnedRuntimeInstalled({
139 baseDir,
140 version,
141 fs,
142 path,
143 platform: "linux",
144 arch: "x64",
145 httpClient: client,
146 runner: extractingRunner(fs, path),
147 validate: () => Effect.die("must not validate an interrupted archive"),
148 onProgress: (event) => {
149 progress.push(event);
150 if (event.stage === "download" && event.received === 4)
151 Deferred.doneUnsafe(firstChunk, Effect.void);
152 },
153 }).pipe(Effect.forkScoped);
154 yield* Deferred.await(firstChunk);
155 yield* Fiber.interrupt(install);
156 assert.deepEqual(progress.at(-1), { stage: "download", received: 4, total: undefined });
157 assert.isTrue(progress.every((event) => event.stage === "download"));
158 assert.isTrue(cancelled);
159 assert.deepEqual(yield* fs.readDirectory(path.join(baseDir, "runtime", "versions")), []);
160 }),
161 );
162
163 it.effect("reports a release that lists an archive it does not serve as unpublished", () =>
164 Effect.gen(function* () {
165 const fs = yield* FileSystem.FileSystem;
166 const path = yield* Path.Path;
167 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-archive-missing-" });
168 const checksums = yield* validChecksums;
169 const failsWith = (status: number) =>
170 ensurePinnedRuntimeInstalled({
171 baseDir,
172 version,
173 fs,
174 path,
175 platform: "linux",
176 arch: "x64",
177 httpClient: HttpClient.make((request) =>
178 Effect.succeed(
179 HttpClientResponse.fromWeb(
180 request,
181 request.url.endsWith("/SHA256SUMS")
182 ? new Response(checksums)
183 : new Response("", { status }),
184 ),
185 ),
186 ),
187 // "404" in the mirror's path must not read a server error as a missing release.
188 releaseBaseUrl: "https://updates.example.com/404/t3code",
189 runner: extractingRunner(fs, path),
190 validate: () => Effect.die("must not validate a missing archive"),
191 }).pipe(Effect.flip);
192 assert.isTrue(isUnpublishedRelease(yield* failsWith(404)));
193 assert.isFalse(isUnpublishedRelease(yield* failsWith(403)));
194 }),
195 );
196
197 it.effect("retries a release download that GitHub answers with a gateway timeout", () =>
198 Effect.gen(function* () {
199 const fs = yield* FileSystem.FileSystem;
200 const path = yield* Path.Path;
201 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-retry-" });
202 const checksums = yield* validChecksums;
203 const requests: string[] = [];
204 const gatewayTimeoutServed = yield* Deferred.make<void>();
205 const install = yield* ensurePinnedRuntimeInstalled({
206 baseDir,
207 version,
208 fs,
209 path,
210 platform: "linux",
211 arch: "x64",
212 httpClient: HttpClient.make((request) => {
213 requests.push(request.url);
214 const isChecksums = request.url.endsWith("/SHA256SUMS");
215 if (requests.length === 1) {
216 Deferred.doneUnsafe(gatewayTimeoutServed, Effect.void);
217 return Effect.succeed(
218 HttpClientResponse.fromWeb(request, new Response("", { status: 504 })),
219 );
220 }
221 return Effect.succeed(
222 HttpClientResponse.fromWeb(
223 request,
224 new Response(isChecksums ? checksums : archiveBytes),
225 ),
226 );
227 }),
228 runner: extractingRunner(fs, path),
229 validate: () => Effect.void,
230 }).pipe(Effect.forkScoped);
231 yield* Deferred.await(gatewayTimeoutServed);
232 yield* TestClock.adjust(Duration.seconds(2));
233 const paths = yield* Fiber.join(install);
234 assert.equal(yield* fs.readFileString(paths.sentinelPath), `${version}\n`);
235 assert.equal(requests.filter((url) => url.endsWith("/SHA256SUMS")).length, 2);
236 }),
237 );
238
239 it.effect.each([
240 { name: "body fails after the host answered", first: "body fails", honorsRange: true },
241 {
242 name: "body stalls and its resume ends early",
243 first: "body stalls",
244 honorsRange: true,
245 shortResume: true,
246 },
247 { name: "body stalls on a host that ignores Range", first: "body stalls", honorsRange: false },
248 { name: "response headers stall", first: "headers stall", honorsRange: true },
249 {
250 name: "whole body arrives and then stalls",
251 first: "body stalls",
252 honorsRange: true,
253 wholeArchive: true,
254 },
255 ] as const)("resumes a release archive download when its $name", (row) =>
256 Effect.gen(function* () {
257 const { first, honorsRange } = row;
258 const shortResume = "shortResume" in row;
259 const fs = yield* FileSystem.FileSystem;
260 const path = yield* Path.Path;
261 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-retry-body-" });
262 const checksums = yield* validChecksums;
263 const ranges: Array<string | undefined> = [];
264 const checksumRanges: Array<string | undefined> = [];
265 const progress: PinnedRuntimeProgress[] = [];
266 const checksumsRequested = yield* Deferred.make<void>();
267 const archiveRequested = yield* Deferred.make<void>();
268 const shortResumeRequested = yield* Deferred.make<void>();
269 const total = archiveBytes.length;
270 const heldBytes = "wholeArchive" in row ? archiveBytes : archiveBytes.slice(0, 4);
271 const install = yield* ensurePinnedRuntimeInstalled({
272 baseDir,
273 version,
274 fs,
275 path,
276 platform: "linux",
277 arch: "x64",
278 httpClient: HttpClient.make((request) => {
279 if (request.url.endsWith("/SHA256SUMS")) {
280 checksumRanges.push(request.headers["range"]);
281 const response = HttpClientResponse.fromWeb(request, new Response(checksums));
282 if (checksumRanges.length > 1) return Effect.succeed(response);
283 Deferred.doneUnsafe(checksumsRequested, Effect.void);
284 // The checksums fail once after a prefix, which their retry must not resume from.
285 const failingBody = Stream.concat(
286 Stream.make(new TextEncoder().encode(checksums).slice(0, 8)),
287 Stream.fail(
288 new HttpClientError.HttpClientError({
289 reason: new HttpClientError.DecodeError({ request, response }),
290 }),
291 ),
292 );
293 return Effect.sleep("1 second").pipe(
294 Effect.as<HttpClientResponse.HttpClientResponse>(
295 Object.create(response, { stream: { value: failingBody } }),
296 ),
297 );
298 }
299 const range = request.headers["range"];
300 ranges.push(range);
301 if (ranges.length > 1) {
302 const start = honorsRange ? Number(/^bytes=(\d+)-$/.exec(range ?? "")?.[1] ?? 0) : 0;
303 // A short resume stops one byte early; the next retry asks for that byte.
304 const end = shortResume && ranges.length === 2 ? total - 1 : total;
305 const response = HttpClientResponse.fromWeb(
306 request,
307 start >= total
308 ? new Response("", {
309 status: 416,
310 headers: { "content-range": `bytes */${total}` },
311 })
312 : start > 0
313 ? new Response(archiveBytes.slice(start, end), {
314 status: 206,
315 headers: { "content-range": `bytes ${start}-${end - 1}/${total}` },
316 })
317 : new Response(archiveBytes, { headers: { "content-length": `${total}` } }),
318 );
319 if (end === total) return Effect.succeed(response);
320 Deferred.doneUnsafe(shortResumeRequested, Effect.void);
321 // An Effect stream keeps the body, and so the retry wait after it, on the test clock.
322 const body = Stream.make(archiveBytes.slice(start, end));
323 return Effect.sleep("1 second").pipe(
324 Effect.as<HttpClientResponse.HttpClientResponse>(
325 Object.create(response, { stream: { value: body } }),
326 ),
327 );
328 }
329 Deferred.doneUnsafe(archiveRequested, Effect.void);
330 if (first === "headers stall") return Effect.never;
331 const response = HttpClientResponse.fromWeb(
332 request,
333 new Response(archiveBytes, { headers: { "content-length": `${total}` } }),
334 );
335 const failingBody = Stream.concat(
336 Stream.make(heldBytes),
337 first === "body stalls"
338 ? Stream.never
339 : Stream.fail(
340 new HttpClientError.HttpClientError({
341 reason: new HttpClientError.DecodeError({ request, response }),
342 }),
343 ),
344 );
345 // TestClock follows the work a woken sleep starts, so one adjust reaches the retry wait.
346 return Effect.sleep("1 second").pipe(
347 Effect.as<HttpClientResponse.HttpClientResponse>(
348 Object.create(response, { stream: { value: failingBody } }),
349 ),
350 );
351 }),
352 runner: extractingRunner(fs, path),
353 validate: () => Effect.void,
354 onProgress: (event) => progress.push(event),
355 }).pipe(Effect.forkScoped);
356 yield* Deferred.await(checksumsRequested);
357 yield* TestClock.adjust(Duration.seconds(3));
358 yield* Deferred.await(archiveRequested);
359 // The one-minute stall bound, then the first two-second retry backoff.
360 yield* TestClock.adjust(Duration.seconds(first === "body fails" ? 3 : 63));
361 if (shortResume) {
362 yield* Deferred.await(shortResumeRequested);
363 // Its one-second delay, then the second, four-second backoff.
364 yield* TestClock.adjust(Duration.seconds(5));
365 }
366 const paths = yield* Fiber.join(install);
367 assert.deepEqual(checksumRanges, [undefined, undefined]);
368 assert.deepEqual(ranges, [
369 undefined,
370 first === "headers stall" ? undefined : `bytes=${heldBytes.length}-`,
371 ...(shortResume ? [`bytes=${total - 1}-`] : []),
372 ]);
373 assert.equal(yield* fs.readFileString(paths.sentinelPath), `${version}\n`);
374 assert.deepEqual(
375 progress.findLast((event) => event.stage === "download"),
376 {
377 stage: "download",
378 received: total,
379 total,
380 },
381 );
382 }),
383 );
384
385 it.effect("fails at once when the release host cannot be reached", () =>
386 Effect.gen(function* () {
387 const fs = yield* FileSystem.FileSystem;
388 const path = yield* Path.Path;
389 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-offline-" });
390 let requests = 0;
391 const error = yield* ensurePinnedRuntimeInstalled({
392 baseDir,
393 version,
394 fs,
395 path,
396 platform: "linux",
397 arch: "x64",
398 httpClient: HttpClient.make((request) => {
399 requests += 1;
400 return Effect.fail(
401 new HttpClientError.HttpClientError({
402 reason: new HttpClientError.TransportError({ request }),
403 }),
404 );
405 }),
406 runner: extractingRunner(fs, path),
407 validate: () => Effect.die("must not validate without a download"),
408 }).pipe(Effect.flip);
409 assert.instanceOf(error, PinnedRuntimeInstallError);
410 assert.equal(requests, 1);
411 }),
412 );
413
414 it.effect("refuses an archive whose checksum does not match the release", () =>
415 Effect.gen(function* () {
416 const fs = yield* FileSystem.FileSystem;
417 const path = yield* Path.Path;
418 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-archive-bad-" });
419 const commands: string[] = [];
420 const error = yield* ensurePinnedRuntimeInstalled({
421 baseDir,
422 version,
423 fs,
424 path,
425 platform: "linux",
426 arch: "x64",
427 httpClient: releaseHttpClient(`${"0".repeat(64)} ${archiveName}\n`),
428 runner: extractingRunner(fs, path, commands),
429 validate: () => Effect.die("must not validate an unverified archive"),
430 }).pipe(Effect.flip);
431 assert.instanceOf(error, PinnedRuntimeInstallError);
432 assert.equal(error.step, "verifying the t3 release archive checksum");
433 assert.deepEqual(commands, []);
434 assert.deepEqual(yield* fs.readDirectory(path.join(baseDir, "runtime", "versions")), []);
435 }),
436 );
437
438 it.effect("validates a staging tree before atomically publishing it", () =>
439 Effect.gen(function* () {
440 const fs = yield* FileSystem.FileSystem;
441 const path = yield* Path.Path;
442 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-runtime-test-" });
443 const finalPaths = pinnedRuntimePaths(path, baseDir, version, "linux");
444 let validatedDirectory = "";
445
446 const installed = yield* ensurePinnedRuntimeInstalled({
447 baseDir,
448 version,
449 fs,
450 path,
451 platform: "linux",
452 arch: "x64",
453 httpClient: releaseHttpClient(yield* validChecksums),
454 runner: extractingRunner(fs, path),
455 validate: (staging) =>
456 Effect.gen(function* () {
457 validatedDirectory = staging.versionDir;
458 assert.isFalse(yield* fs.exists(finalPaths.versionDir));
459 assert.isTrue(yield* fs.exists(staging.entryPath));
460 }).pipe(Effect.orDie),
461 });
462
463 assert.notEqual(validatedDirectory, finalPaths.versionDir);
464 assert.deepEqual(installed, finalPaths);
465 assert.isTrue(yield* fs.exists(finalPaths.entryPath));
466 assert.equal(yield* fs.readFileString(finalPaths.sentinelPath), `${version}\n`);
467 }),
468 );
469
470 it.effect("removes staging and leaves no final runtime when validation fails", () =>
471 Effect.gen(function* () {
472 const fs = yield* FileSystem.FileSystem;
473 const path = yield* Path.Path;
474 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-runtime-test-" });
475 const finalPaths = pinnedRuntimePaths(path, baseDir, version, "linux");
476
477 yield* ensurePinnedRuntimeInstalled({
478 baseDir,
479 version,
480 fs,
481 path,
482 platform: "linux",
483 arch: "x64",
484 httpClient: releaseHttpClient(yield* validChecksums),
485 runner: extractingRunner(fs, path),
486 validate: () =>
487 Effect.fail(new PinnedRuntimeInstallError({ step: "validating the staged runtime" })),
488 }).pipe(Effect.flip);
489
490 assert.isFalse(yield* fs.exists(finalPaths.versionDir));
491 assert.deepEqual(
492 (yield* fs.readDirectory(path.dirname(finalPaths.versionDir))).filter((entry) =>
493 entry.startsWith(".staging-"),
494 ),
495 [],
496 );
497 }),
498 );
499
500 it.effect("replaces an incomplete pinned runtime", () =>
501 Effect.gen(function* () {
502 const fs = yield* FileSystem.FileSystem;
503 const path = yield* Path.Path;
504 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-runtime-repair-" });
505 const finalPaths = pinnedRuntimePaths(path, baseDir, version, "linux");
506 yield* fs.makeDirectory(finalPaths.versionDir, { recursive: true });
507 yield* fs.writeFileString(path.join(finalPaths.versionDir, "partial"), "incomplete\n");
508
509 yield* ensurePinnedRuntimeInstalled({
510 baseDir,
511 version,
512 fs,
513 path,
514 platform: "linux",
515 arch: "x64",
516 httpClient: releaseHttpClient(yield* validChecksums),
517 runner: extractingRunner(fs, path),
518 validate: () => Effect.void,
519 });
520
521 assert.isFalse(yield* fs.exists(path.join(finalPaths.versionDir, "partial")));
522 assert.isTrue(yield* fs.exists(finalPaths.entryPath));
523 }),
524 );
525
526 it.effect("reuses a cached runtime under a mirror only while the mirror lists it", () =>
527 Effect.gen(function* () {
528 const fs = yield* FileSystem.FileSystem;
529 const path = yield* Path.Path;
530 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-runtime-mirror-" });
531 const cached = pinnedRuntimePaths(path, baseDir, version, "linux");
532 yield* fs.makeDirectory(path.dirname(cached.entryPath), { recursive: true });
533 yield* fs.writeFileString(cached.entryPath, "#!/bin/sh\n");
534 yield* fs.writeFileString(cached.sentinelPath, `${version}\n`);
535 const install = (checksums: string, requests: string[]) =>
536 ensurePinnedRuntimeInstalled({
537 baseDir,
538 version,
539 fs,
540 path,
541 platform: "linux",
542 arch: "x64",
543 httpClient: releaseHttpClient(checksums, requests),
544 // A signed mirror URL keeps its query on every file it serves.
545 releaseBaseUrl: "https://updates.example.com/t3code/?token=abc",
546 requirePublishedRelease: true,
547 runner: extractingRunner(fs, path),
548 validate: () => Effect.void,
549 });
550
551 const withdrawn = yield* install("", []).pipe(Effect.flip);
552 assert.include(withdrawn.message, `finding ${archiveName} in the t3 release checksums`);
553 const requests: string[] = [];
554 assert.equal((yield* install(yield* validChecksums, requests)).entryPath, cached.entryPath);
555 // Listed, so the cached runtime is used without downloading it again.
556 assert.deepEqual(requests, [
557 `https://updates.example.com/t3code/v${version}/SHA256SUMS?token=abc`,
558 ]);
559 }),
560 );
561
562 it.effect("preserves a completed runtime when validation fails", () =>
563 Effect.gen(function* () {
564 const fs = yield* FileSystem.FileSystem;
565 const path = yield* Path.Path;
566 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-runtime-repair-" });
567 const finalPaths = pinnedRuntimePaths(path, baseDir, version, "linux");
568 yield* fs.makeDirectory(path.dirname(finalPaths.entryPath), { recursive: true });
569 yield* fs.writeFileString(finalPaths.entryPath, "broken\n");
570 yield* fs.writeFileString(finalPaths.sentinelPath, `${version}\n`);
571
572 let validations = 0;
573 const requests: string[] = [];
574 yield* ensurePinnedRuntimeInstalled({
575 baseDir,
576 version,
577 fs,
578 path,
579 platform: "linux",
580 arch: "x64",
581 httpClient: releaseHttpClient(yield* validChecksums, requests),
582 runner: extractingRunner(fs, path),
583 validate: (paths) =>
584 Effect.gen(function* () {
585 validations += 1;
586 const source = yield* fs.readFileString(paths.entryPath).pipe(Effect.orDie);
587 if (source === "broken\n") {
588 return yield* new PinnedRuntimeInstallError({ step: "validating the runtime" });
589 }
590 }),
591 }).pipe(Effect.flip);
592
593 assert.equal(validations, 1);
594 assert.deepEqual(requests, []);
595 assert.equal(yield* fs.readFileString(finalPaths.entryPath), "broken\n");
596 }),
597 );
598 });
599
600 it("isVersionExecutable accepts only <version>/t3, never another drive or program", () => {
601 const win32 = NodePath.win32 as unknown as Path.Path;
602 assert.isTrue(isVersionExecutable(win32, "C:\\t3\\versions", "C:\\t3\\versions\\1.0.0\\t3.exe"));
603 assert.isFalse(isVersionExecutable(win32, "C:\\t3\\versions", "D:\\t3.exe"));
604 assert.isFalse(
605 isVersionExecutable(win32, "C:\\t3\\versions", "C:\\t3\\versions\\1.0.0\\node.exe"),
606 );
607 });
608
609 it.layer(NodeServices.layer)("prunePinnedRuntimes", (it) => {
610 const lsofRunner = (stdout: string) =>
611 ProcessRunner.ProcessRunner.of({
612 run: () =>
613 Effect.succeed({
614 stdout,
615 stderr: "",
616 code: ChildProcessSpawner.ExitCode(0),
617 timedOut: false,
618 stdoutTruncated: false,
619 stderrTruncated: false,
620 stdoutInvalidUtf8: false,
621 stderrInvalidUtf8: false,
622 }),
623 });
624
625 const setup = Effect.fn(function* (versions: ReadonlyArray<string>) {
626 const fs = yield* FileSystem.FileSystem;
627 const path = yield* Path.Path;
628 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-prune-" });
629 const versionsDir = path.join(baseDir, "runtime", "versions");
630 const now = Duration.toMillis(Duration.days(1));
631 yield* TestClock.setTime(now);
632 const install = Effect.fn(function* (
633 version: string,
634 completed: "long ago" | "just now" | "never" = "long ago",
635 ) {
636 const directory = path.join(versionsDir, version);
637 yield* fs.makeDirectory(directory, { recursive: true });
638 yield* fs.writeFileString(path.join(directory, "t3"), "");
639 if (completed === "never") return;
640 const sentinel = path.join(directory, ".install-complete");
641 yield* fs.writeFileString(sentinel, `${version}\n`);
642 const at = completed === "just now" ? now : 0;
643 yield* fs.utimes(sentinel, at, at);
644 });
645 for (const version of versions) yield* install(version);
646 return { baseDir, versionsDir, install };
647 });
648
649 it.effect("removes only completed runtimes nothing references", () =>
650 Effect.gen(function* () {
651 const fs = yield* FileSystem.FileSystem;
652 const path = yield* Path.Path;
653 const { baseDir, versionsDir, install } = yield* setup([
654 "1.0.0",
655 "1.1.0",
656 "1.2.0",
657 "1.2.5",
658 "1.3.0",
659 "1.3.5",
660 "1.4.0",
661 "1.5.0",
662 "2.0.0",
663 "2.1.0",
664 ]);
665 // A downgrade another process installed but has not pointed anything at yet.
666 yield* install("0.5.0", "just now");
667 yield* install("1.0.1", "never");
668 yield* install(".staging-abc", "never");
669 yield* install(".pruned-0.8.0");
670 yield* install("other", "never");
671 yield* fs.symlink(path.join(versionsDir, "other"), path.join(versionsDir, "0.9.0"));
672 const binDir = path.join(baseDir, "bin");
673 const customBinDir = path.join(baseDir, "custom-bin");
674 yield* fs.makeDirectory(binDir);
675 yield* fs.makeDirectory(customBinDir);
676 // Chained: the launcher reaches its runtime through a second link.
677 yield* fs.symlink(path.join(versionsDir, "1.2.0", "t3"), path.join(baseDir, "alias-t3"));
678 yield* fs.symlink(path.join(baseDir, "alias-t3"), path.join(binDir, "t3"));
679 yield* fs.symlink(path.join(versionsDir, "1.2.5", "t3"), path.join(customBinDir, "t3"));
680 yield* fs.writeFileString(
681 path.join(baseDir, "runtime", "launchers"),
682 `${path.join(customBinDir, "t3")}\n`,
683 );
684 yield* fs.writeFileString(
685 path.join(baseDir, "runtime", "service-state.json"),
686 JSON.stringify({
687 protocol: SERVICE_LAUNCHER_PROTOCOL,
688 activeVersion: "1.4.0",
689 update: { id: "u", fromVersion: "1.4.0", targetVersion: "1.5.0", status: "rolled-back" },
690 }),
691 );
692
693 const removed = yield* prunePinnedRuntimes({
694 baseDir,
695 keep: ["2.0.0"],
696 references: [`ExecStart=${path.join(versionsDir, "1.3.5", "t3")} __service-launcher`],
697 }).pipe(
698 Effect.provideService(
699 ProcessRunner.ProcessRunner,
700 lsofRunner(`p42\nftxt\nn${path.join(versionsDir, "1.3.0", "t3")}\n`),
701 ),
702 Effect.provideService(HostProcess.Platform, "darwin"),
703 Effect.provideService(HostProcess.Environment, { T3CODE_INSTALL_BIN_DIR: binDir }),
704 );
705
706 // 1.2.0 is the default launcher, 1.2.5 a recorded one, 1.3.0 running, 1.3.5 in the unit,
707 // 1.4.0 active, 1.5.0 the rolled-back target, 2.0.0 the caller's, 0.5.0 just installed.
708 // Being newer than the caller's version does not keep 2.1.0.
709 assert.deepEqual(removed.toSorted(), ["0.8.0", "1.0.0", "1.1.0", "2.1.0"]);
710 // The launcher found on PATH is remembered for a service that cannot see it.
711 assert.equal(
712 yield* fs.readFileString(path.join(baseDir, "runtime", "launchers")),
713 `${path.join(customBinDir, "t3")}\n\n${path.join(binDir, "t3")}\n`,
714 );
715 assert.deepEqual((yield* fs.readDirectory(versionsDir)).toSorted(), [
716 ".staging-abc",
717 "0.5.0",
718 "0.9.0",
719 "1.0.1",
720 "1.2.0",
721 "1.2.5",
722 "1.3.0",
723 "1.3.5",
724 "1.4.0",
725 "1.5.0",
726 "2.0.0",
727 "other",
728 ]);
729 }),
730 );
731
732 it.effect.each([
733 { name: "its directory is on PATH", onPath: true, recorded: false, linkedHome: false },
734 {
735 name: "an earlier prune recorded its entry as a launcher",
736 onPath: false,
737 recorded: true,
738 linkedHome: false,
739 },
740 {
741 name: "its real directory in a symlinked home is on PATH",
742 onPath: true,
743 recorded: false,
744 linkedHome: true,
745 },
746 ])("removes a runtime only its own entry names when $name", ({ onPath, recorded, linkedHome }) =>
747 Effect.gen(function* () {
748 const fs = yield* FileSystem.FileSystem;
749 const path = yield* Path.Path;
750 const { baseDir, versionsDir } = yield* setup(["1.0.0", "1.1.0"]);
751 const binDir = path.join(baseDir, "bin");
752 yield* fs.makeDirectory(binDir);
753 yield* fs.symlink(path.join(versionsDir, "1.1.0", "t3"), path.join(binDir, "t3"));
754 const ownDir = path.join(versionsDir, "1.0.0");
755 if (recorded) {
756 yield* fs.writeFileString(
757 path.join(baseDir, "runtime", "launchers"),
758 `${path.join(ownDir, "t3")}\n`,
759 );
760 }
761
762 const home = linkedHome
763 ? path.join(yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-link-" }), "home")
764 : baseDir;
765 if (linkedHome) yield* fs.symlink(baseDir, home);
766
767 // The launch agent puts the running runtime's own directory on PATH.
768 const removed = yield* prunePinnedRuntimes({ baseDir: home, keep: ["2.0.0"] }).pipe(
769 Effect.provideService(
770 ProcessRunner.ProcessRunner,
771 lsofRunner("p1\nftxt\nn/sbin/launchd\n"),
772 ),
773 Effect.provideService(HostProcess.Platform, "darwin"),
774 Effect.provideService(HostProcess.Environment, {
775 T3CODE_INSTALL_BIN_DIR: binDir,
776 PATH: onPath ? `${ownDir}:/usr/bin:/bin` : "/usr/bin:/bin",
777 }),
778 );
779 assert.deepEqual(removed, ["1.0.0"]);
780 }),
781 );
782
783 it.effect.each([
784 { name: "a launcher inside the versions directory", via: "bin" as const },
785 { name: "a PATH directory linked to its version directory", via: "linked dir" as const },
786 {
787 name: "a PATH directory in the versions directory linked to its version directory",
788 via: "linked versions dir" as const,
789 },
790 ])("keeps a runtime $name names", ({ via }) =>
791 Effect.gen(function* () {
792 const fs = yield* FileSystem.FileSystem;
793 const path = yield* Path.Path;
794 const { baseDir, versionsDir } = yield* setup(["1.0.0", "1.1.0"]);
795 let pathDirs = "/usr/bin:/bin";
796 if (via === "bin") {
797 // The installer accepts any bin directory, including one under the versions directory.
798 const binDir = path.join(versionsDir, "bin");
799 yield* fs.makeDirectory(binDir);
800 yield* fs.symlink(path.join(versionsDir, "1.0.0", "t3"), path.join(binDir, "t3"));
801 yield* fs.writeFileString(
802 path.join(baseDir, "runtime", "launchers"),
803 `${path.join(binDir, "t3")}\n`,
804 );
805 } else {
806 const linkedDir = path.join(via === "linked dir" ? baseDir : versionsDir, "current");
807 yield* fs.symlink(path.join(versionsDir, "1.0.0"), linkedDir);
808 pathDirs = `${linkedDir}:${pathDirs}`;
809 }
810
811 const removed = yield* prunePinnedRuntimes({ baseDir, keep: ["2.0.0"] }).pipe(
812 Effect.provideService(
813 ProcessRunner.ProcessRunner,
814 lsofRunner("p1\nftxt\nn/sbin/launchd\n"),
815 ),
816 Effect.provideService(HostProcess.Platform, "darwin"),
817 Effect.provideService(HostProcess.Environment, { PATH: pathDirs }),
818 );
819 assert.deepEqual(removed, ["1.1.0"]);
820 }),
821 );
822
823 it.effect.each([
824 // An interrupted append cut this record short.
825 { name: "an unterminated launcher record", recorded: "/opt/t", cut: "" },
826 // Another process's append is cut short after prune reads the records.
827 { name: "a record cut short after the read", recorded: "/opt/t3\n", cut: "/opt/t" },
828 ])("remembers a launcher found on PATH after $name", ({ recorded, cut }) =>
829 Effect.gen(function* () {
830 const fs = yield* FileSystem.FileSystem;
831 const path = yield* Path.Path;
832 const { baseDir, versionsDir } = yield* setup(["1.0.0", "1.1.0"]);
833 const binDir = path.join(baseDir, "custom-bin");
834 yield* fs.makeDirectory(binDir);
835 yield* fs.symlink(path.join(versionsDir, "1.0.0", "t3"), path.join(binDir, "t3"));
836 const launchersFile = path.join(baseDir, "runtime", "launchers");
837 yield* fs.writeFileString(launchersFile, recorded);
838 const prune = (environment: Record<string, string>) =>
839 prunePinnedRuntimes({ baseDir, keep: [] }).pipe(
840 Effect.provideService(ProcessRunner.ProcessRunner, lsofRunner("p1\nftxt\nn/bin/sh\n")),
841 Effect.provideService(HostProcess.Platform, "darwin"),
842 Effect.provideService(HostProcess.Environment, environment),
843 Effect.provideService(HostProcess.HomeDirectory, baseDir),
844 );
845
846 yield* prune({ PATH: binDir }).pipe(
847 Effect.provideService(FileSystem.FileSystem, {
848 ...fs,
849 readFileString: (file, encoding) =>
850 fs
851 .readFileString(file, encoding)
852 .pipe(
853 Effect.tap(() =>
854 file === launchersFile
855 ? fs.writeFileString(file, cut, { flag: "a" })
856 : Effect.void,
857 ),
858 ),
859 }),
860 );
861 // A service that cannot see the interactive PATH.
862 const removed = yield* prune({});
863
864 assert.deepEqual(removed, []);
865 assert.isTrue(yield* fs.exists(path.join(versionsDir, "1.0.0", "t3")));
866 }),
867 );
868
869 it.effect("matches a Windows shim whatever its casing", () =>
870 Effect.gen(function* () {
871 const fs = yield* FileSystem.FileSystem;
872 const path = yield* Path.Path;
873 const { baseDir, versionsDir } = yield* setup(["1.0.0", "1.1.0"]);
874 const shim = path.join(baseDir, "t3.cmd");
875 yield* fs.writeFileString(
876 shim,
877 `@echo off\r\n"${path.join(versionsDir, "1.0.0", "t3.exe").toUpperCase()}" %*`,
878 );
879 yield* fs.writeFileString(path.join(baseDir, "runtime", "launchers"), `${shim}\n`);
880
881 const removed = yield* prunePinnedRuntimes({ baseDir, keep: ["2.0.0"] }).pipe(
882 Effect.provideService(
883 ProcessRunner.ProcessRunner,
884 lsofRunner("C:\\Windows\\explorer.exe\n"),
885 ),
886 Effect.provideService(HostProcess.Platform, "win32"),
887 Effect.provideService(HostProcess.Environment, {}),
888 );
889 assert.deepEqual(removed, ["1.1.0"]);
890 }),
891 );
892
893 it.effect.each([
894 { name: "the process list is unavailable", state: undefined, lsof: "" },
895 { name: "the service state is unparseable", state: "{", lsof: "p1\nftxt\nn/sbin/launchd\n" },
896 {
897 name: "the service state cannot be read",
898 state: null,
899 lsof: "p1\nftxt\nn/sbin/launchd\n",
900 },
901 ])("keeps everything when $name", ({ state, lsof }) =>
902 Effect.gen(function* () {
903 const fs = yield* FileSystem.FileSystem;
904 const path = yield* Path.Path;
905 const { baseDir, versionsDir } = yield* setup(["1.0.0"]);
906 const statePath = path.join(baseDir, "runtime", "service-state.json");
907 if (state === null) yield* fs.makeDirectory(statePath);
908 else if (state !== undefined) yield* fs.writeFileString(statePath, state);
909
910 const removed = yield* prunePinnedRuntimes({ baseDir, keep: ["2.0.0"] }).pipe(
911 Effect.provideService(ProcessRunner.ProcessRunner, lsofRunner(lsof)),
912 Effect.provideService(HostProcess.Platform, "darwin"),
913 Effect.provideService(HostProcess.Environment, { T3CODE_INSTALL_BIN_DIR: baseDir }),
914 );
915 assert.deepEqual(removed, []);
916 assert.deepEqual(yield* fs.readDirectory(versionsDir), ["1.0.0"]);
917 }),
918 );
919
920 it.effect.skipIf(HostProcess.Platform.defaultValue() !== "linux")(
921 "keeps a runtime a process runs through a symlink",
922 () =>
923 Effect.gen(function* () {
924 const fs = yield* FileSystem.FileSystem;
925 const path = yield* Path.Path;
926 const { baseDir, versionsDir } = yield* setup(["1.0.0", "1.1.0"]);
927 const entry = path.join(versionsDir, "1.0.0", "t3");
928 // Multicall coreutils (Ubuntu's uutils) refuse to run renamed, so copy Node instead.
929 yield* fs.copyFile(process.execPath, entry);
930 yield* fs.chmod(entry, 0o755);
931 const link = path.join(baseDir, "t3");
932 yield* fs.symlink(entry, link);
933 // Its argv names only the link, as with `t3 serve` run from PATH. Removing the link after
934 // spawning leaves the process's executable as the only reference.
935 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
936 yield* spawner.spawn(ChildProcess.make(link, ["-e", "setTimeout(() => {}, 30_000)"]));
937 yield* fs.remove(link);
938
939 const removed = yield* prunePinnedRuntimes({ baseDir, keep: ["2.0.0"] }).pipe(
940 Effect.provideService(HostProcess.Platform, "linux"),
941 Effect.provideService(HostProcess.Environment, { T3CODE_INSTALL_BIN_DIR: baseDir }),
942 Effect.provide(ProcessRunner.layer),
943 );
944 assert.deepEqual(removed, ["1.1.0"]);
945 }),
946 );
947 });