@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 03:37 UTC

main
ts 932 lines 36.3 KB
Raw
1 import * as Cause from "effect/Cause";
2 import * as Clock from "effect/Clock";
3 import * as Duration from "effect/Duration";
4 import * as Effect from "effect/Effect";
5 import * as Hex from "effect/encoding/Hex";
6 import * as FileSystem from "effect/FileSystem";
7 import * as Path from "effect/Path";
8 import * as Schema from "effect/Schema";
9 import * as Option from "effect/Option";
10 import * as Schedule from "effect/Schedule";
11 import * as Stream from "effect/Stream";
12 import * as Semaphore from "effect/Semaphore";
13 import { HttpClient, HttpClientError, HttpClientRequest, HttpClientResponse } from "effect/http";
14
15 import {
16 CLI_RELEASE_CHECKSUMS_FILE,
17 cliArchiveFileName,
18 cliArchivePlatformKey,
19 cliArchiveTarCommand,
20 cliReleaseAssetUrl,
21 type CliArchivePlatformKey,
22 parseChecksums,
23 } from "@t3tools/shared/cliRelease";
24
25 import * as HostProcess from "@t3tools/shared/HostProcess";
26
27 import * as ProcessRunner from "../processRunner.ts";
28 import { isExactServiceVersion, parseServiceState, SERVICE_STATE_FILE } from "./serviceProtocol.ts";
29
30 const PINNED_RUNTIME_DIR = "runtime";
31 const PINNED_RUNTIME_INSTALL_TIMEOUT = Duration.minutes(10);
32 const PINNED_RUNTIME_DOWNLOAD_STALL_TIMEOUT = Duration.minutes(1);
33 const PINNED_RUNTIME_ARCHIVE_FILE = "t3-runtime-archive";
34 // Separate layers share one install lock across this process.
35 const pinnedRuntimeInstallLock = Semaphore.makeUnsafe(1);
36
37 export interface PinnedRuntimePaths {
38 readonly versionDir: string;
39 readonly entryPath: string;
40 readonly sentinelPath: string;
41 }
42
43 export function pinnedRuntimeCommand(paths: PinnedRuntimePaths): {
44 readonly command: string;
45 readonly args: ReadonlyArray<string>;
46 } {
47 return { command: paths.entryPath, args: [] };
48 }
49
50 export function pinnedRuntimeVersionsDir(path: Path.Path, baseDir: string): string {
51 return path.join(baseDir, PINNED_RUNTIME_DIR, "versions");
52 }
53
54 export function pinnedRuntimePaths(
55 path: Path.Path,
56 baseDir: string,
57 version: string,
58 platform: NodeJS.Platform,
59 ): PinnedRuntimePaths {
60 const versionDir = path.join(pinnedRuntimeVersionsDir(path, baseDir), version);
61 return {
62 versionDir,
63 entryPath: path.join(versionDir, platform === "win32" ? "t3.exe" : "t3"),
64 sentinelPath: path.join(versionDir, ".install-complete"),
65 };
66 }
67
68 export class PinnedRuntimeInstallError extends Schema.TaggedError<PinnedRuntimeInstallError>()(
69 "PinnedRuntimeInstallError",
70 {
71 step: Schema.String,
72 exitCode: Schema.optional(Schema.Number),
73 stdoutLength: Schema.optional(Schema.Number),
74 stderrLength: Schema.optional(Schema.Number),
75 httpStatus: Schema.optional(Schema.Number),
76 downloadHost: Schema.optional(Schema.String),
77 cause: Schema.optional(Schema.Defect()),
78 },
79 ) {
80 override get message(): string {
81 return this.exitCode === undefined
82 ? `Pinned runtime install failed while ${this.step}.`
83 : `Pinned runtime install failed while ${this.step} (exit code ${this.exitCode}).`;
84 }
85 }
86
87 export class PinnedRuntimePreflightBlockedError extends Schema.TaggedError<PinnedRuntimePreflightBlockedError>()(
88 "PinnedRuntimePreflightBlockedError",
89 {
90 version: Schema.String,
91 reason: Schema.String,
92 },
93 ) {
94 override get message(): string {
95 return this.reason;
96 }
97 }
98
99 export type PinnedRuntimeProgress =
100 | { readonly stage: "download"; readonly received: number; readonly total: number | undefined }
101 | { readonly stage: "verify" | "extract" | "validate" | "cached" };
102
103 /** Tar writes the executable before native packages; only a validated install gets a sentinel. */
104
105 interface PinnedRuntimeInstallInput {
106 readonly baseDir: string;
107 readonly version: string;
108 readonly fs: FileSystem.FileSystem;
109 readonly path: Path.Path;
110 readonly runner: ProcessRunner.ProcessRunner["Service"];
111 readonly validate: (
112 paths: PinnedRuntimePaths,
113 ) => Effect.Effect<void, PinnedRuntimeInstallError | PinnedRuntimePreflightBlockedError>;
114 readonly platform: NodeJS.Platform;
115 readonly arch: string;
116 readonly httpClient: HttpClient.HttpClient;
117 readonly releaseBaseUrl?: string | undefined;
118 /** Reuse a cached runtime only while the release source still lists it, as a policy mirror does. */
119 readonly requirePublishedRelease?: boolean;
120 readonly onProgress?: (progress: PinnedRuntimeProgress) => void;
121 }
122
123 // GitHub release downloads intermittently answer 5xx during incidents; a retry usually lands.
124 const DOWNLOAD_RETRY = { times: 3, schedule: Schedule.exponential("2 seconds") } as const;
125
126 const isTransientDownloadFailure = (
127 error: HttpClientError.HttpClientError | Cause.TimeoutError,
128 ): boolean => {
129 // A slow link stalls past the bound and then recovers; the retry resumes from the bytes held.
130 if (error._tag === "TimeoutError") return true;
131 // Body bytes are read raw, so a DecodeError is a body that failed after the host answered.
132 if (error.reason._tag === "DecodeError") return true;
133 const status = error.response?.status;
134 return status !== undefined && (status >= 500 || status === 429 || status === 408);
135 };
136
137 const CONTENT_RANGE = /^bytes (\d+)-\d+\/(\d+|\*)$/;
138
139 const concatChunks = (chunks: ReadonlyArray<Uint8Array>, length: number): Uint8Array => {
140 const bytes = new Uint8Array(length);
141 let offset = 0;
142 for (const chunk of chunks) {
143 bytes.set(chunk, offset);
144 offset += chunk.byteLength;
145 }
146 return bytes;
147 };
148
149 const fetchReleaseAsset = Effect.fn("cloud.pinned_runtime.fetch_release_asset")(function* (
150 httpClient: HttpClient.HttpClient,
151 url: string,
152 step: string,
153 // Only a digest-checked asset resumes; anything else restarts at byte 0 on each retry.
154 resume: boolean,
155 onProgress?: (progress: PinnedRuntimeProgress) => void,
156 ) {
157 // Bytes from earlier attempts outlive the retry so a stalled download resumes with Range.
158 let chunks: Array<Uint8Array> = [];
159 let received = 0;
160 // Bound stalls while holding the install lock, but allow transfers that keep
161 // receiving bytes to finish regardless of their total duration.
162 return yield* Effect.suspend(() => {
163 if (!resume) {
164 chunks = [];
165 received = 0;
166 }
167 return httpClient.execute(
168 received > 0
169 ? HttpClientRequest.get(url).pipe(
170 HttpClientRequest.setHeader("range", `bytes=${received}-`),
171 )
172 : HttpClientRequest.get(url),
173 );
174 }).pipe(
175 // A resume answered 416 has nothing after the held bytes to send; the archive
176 // checksum decides whether they are the whole file.
177 Effect.filterOrElse(
178 (response) => received > 0 && response.status === 416,
179 HttpClientResponse.filterStatusOk,
180 ),
181 Effect.timeout(PINNED_RUNTIME_DOWNLOAD_STALL_TIMEOUT),
182 Effect.flatMap(
183 Effect.fn(function* (response) {
184 if (response.status === 416) return concatChunks(chunks, received);
185 const contentRange =
186 response.status === 206
187 ? CONTENT_RANGE.exec(response.headers["content-range"] ?? "")
188 : null;
189 if (response.status === 206 && Number(contentRange?.[1]) !== received) {
190 chunks = [];
191 received = 0;
192 return yield* new HttpClientError.HttpClientError({
193 reason: new HttpClientError.DecodeError({
194 request: response.request,
195 response,
196 description: "partial content does not resume the bytes already received",
197 }),
198 });
199 }
200 if (contentRange === null) {
201 chunks = [];
202 received = 0;
203 }
204 const length = Number(
205 contentRange === null ? response.headers["content-length"] : contentRange[2],
206 );
207 const total = Number.isFinite(length) && length > 0 ? length : undefined;
208 onProgress?.({ stage: "download", received, total });
209 const attemptStart = received;
210 yield* response.stream.pipe(
211 Stream.timeoutOrElse({
212 duration: PINNED_RUNTIME_DOWNLOAD_STALL_TIMEOUT,
213 orElse: () => Stream.fail(new Cause.TimeoutError()),
214 }),
215 Stream.runForEach((chunk) =>
216 Effect.sync(() => {
217 chunks.push(chunk);
218 received += chunk.byteLength;
219 onProgress?.({ stage: "download", received, total });
220 }),
221 ),
222 );
223 // A server may end a 206 before the declared total; a retry asks for the rest.
224 if (
225 resume &&
226 contentRange !== null &&
227 total !== undefined &&
228 received > attemptStart &&
229 received < total
230 ) {
231 return yield* new HttpClientError.HttpClientError({
232 reason: new HttpClientError.DecodeError({
233 request: response.request,
234 response,
235 description: "partial content ended before the declared total",
236 }),
237 });
238 }
239 if (total === undefined && received > 0) {
240 onProgress?.({ stage: "download", received, total: received });
241 }
242 return concatChunks(chunks, received);
243 }),
244 ),
245 Effect.retry({ ...DOWNLOAD_RETRY, while: isTransientDownloadFailure }),
246 Effect.mapError(
247 (cause) =>
248 new PinnedRuntimeInstallError({
249 step: cause._tag === "TimeoutError" ? `${step} (timed out)` : step,
250 httpStatus:
251 HttpClientError.isHttpClientError(cause) && cause.reason._tag === "StatusCodeError"
252 ? cause.reason.response.status
253 : undefined,
254 downloadHost: URL.parse(
255 HttpClientError.isHttpClientError(cause) ? cause.request.url : url,
256 )?.host,
257 cause,
258 }),
259 ),
260 );
261 });
262
263 const installFromArchive = Effect.fn("cloud.pinned_runtime.install_archive")(function* (
264 input: PinnedRuntimeInstallInput,
265 stagingDir: string,
266 ) {
267 const { fs, path } = input;
268 const platformKey = cliArchivePlatformKey(input.platform, input.arch);
269 if (platformKey === undefined) {
270 return yield* new PinnedRuntimeInstallError({
271 step: `selecting a t3 release archive for ${input.platform}-${input.arch}`,
272 });
273 }
274 const httpClient = input.httpClient;
275 input.onProgress?.({ stage: "download", received: 0, total: undefined });
276 const { fileName, expected } = yield* publishedArchive(input, platformKey);
277 const archive = yield* fetchReleaseAsset(
278 httpClient,
279 cliReleaseAssetUrl(input.version, fileName, input.releaseBaseUrl),
280 "downloading the t3 release archive",
281 true,
282 input.onProgress,
283 );
284 input.onProgress?.({ stage: "verify" });
285 const digest = yield* Effect.tryPromise({
286 try: () => crypto.subtle.digest("SHA-256", archive),
287 catch: (cause) =>
288 new PinnedRuntimeInstallError({ step: "verifying the t3 release archive", cause }),
289 });
290 if (Hex.encode(new Uint8Array(digest)) !== expected) {
291 return yield* new PinnedRuntimeInstallError({
292 step: "verifying the t3 release archive checksum",
293 });
294 }
295
296 const archivePath = path.join(stagingDir, PINNED_RUNTIME_ARCHIVE_FILE);
297 yield* fs
298 .writeFile(archivePath, archive)
299 .pipe(
300 Effect.mapError(
301 (cause) => new PinnedRuntimeInstallError({ step: "writing the t3 release archive", cause }),
302 ),
303 );
304 input.onProgress?.({ stage: "extract" });
305 const extractStep = "extracting the t3 release archive";
306 // Strip the archive's stem directory so the executable lands at <versionDir>/t3.
307 yield* input.runner
308 .run({
309 command: cliArchiveTarCommand(input.platform, process.env),
310 args: ["-xf", archivePath, "-C", stagingDir, "--strip-components=1"],
311 timeout: PINNED_RUNTIME_INSTALL_TIMEOUT,
312 })
313 .pipe(
314 Effect.mapError((cause) => new PinnedRuntimeInstallError({ step: extractStep, cause })),
315 Effect.filterOrFail(
316 (result) => result.code === 0,
317 (result) =>
318 new PinnedRuntimeInstallError({
319 step: extractStep,
320 exitCode: Number(result.code),
321 stdoutLength: result.stdout.length,
322 stderrLength: result.stderr.length,
323 }),
324 ),
325 );
326 yield* fs.remove(archivePath, { force: true }).pipe(Effect.ignore);
327 });
328
329 /** The release's archive for this machine and its checksum; fails when the release lists none. */
330 const publishedArchive = Effect.fn("cloud.pinned_runtime.published_archive")(function* (
331 input: PinnedRuntimeInstallInput,
332 platformKey: CliArchivePlatformKey,
333 ) {
334 const fileName = cliArchiveFileName(input.version, platformKey);
335 const checksums = parseChecksums(
336 new TextDecoder().decode(
337 yield* fetchReleaseAsset(
338 input.httpClient,
339 cliReleaseAssetUrl(input.version, CLI_RELEASE_CHECKSUMS_FILE, input.releaseBaseUrl),
340 "downloading the t3 release checksums",
341 false,
342 ),
343 ),
344 );
345 const expected = checksums.get(fileName);
346 if (expected === undefined) {
347 return yield* new PinnedRuntimeInstallError({
348 step: `finding ${fileName} in the t3 release checksums`,
349 });
350 }
351 return { fileName, expected };
352 });
353
354 const isPinnedRuntimeInstallError = Schema.is(PinnedRuntimeInstallError);
355
356 /** Whether an install failed because the release source does not publish the version for this machine. */
357 export const isUnpublishedRelease = (error: unknown): error is PinnedRuntimeInstallError =>
358 isPinnedRuntimeInstallError(error) &&
359 (error.step.startsWith("finding ") ||
360 (error.step.startsWith("downloading the t3 release") &&
361 HttpClientError.isHttpClientError(error.cause) &&
362 error.cause.response?.status === 404));
363
364 const installPinnedRuntime = Effect.fn("cloud.pinned_runtime.ensure_installed")(function* (
365 input: PinnedRuntimeInstallInput,
366 ) {
367 const { fs } = input;
368 const paths = pinnedRuntimePaths(input.path, input.baseDir, input.version, input.platform);
369 const [versionDirExists, entryExists, sentinel] = yield* Effect.all([
370 fs.exists(paths.versionDir),
371 fs.exists(paths.entryPath),
372 fs.readFileString(paths.sentinelPath).pipe(Effect.option),
373 ]).pipe(
374 Effect.mapError(
375 (cause) => new PinnedRuntimeInstallError({ step: "checking the pinned runtime", cause }),
376 ),
377 );
378 const alreadyPinned =
379 entryExists && Option.isSome(sentinel) && sentinel.value.trim() === input.version;
380 if (alreadyPinned) {
381 if (input.requirePublishedRelease === true) {
382 const platformKey = cliArchivePlatformKey(input.platform, input.arch);
383 if (platformKey === undefined) {
384 return yield* new PinnedRuntimeInstallError({
385 step: `selecting a t3 release archive for ${input.platform}-${input.arch}`,
386 });
387 }
388 yield* publishedArchive(input, platformKey);
389 }
390 input.onProgress?.({ stage: "cached" });
391 yield* input.validate(paths);
392 return paths;
393 }
394 if (versionDirExists) {
395 yield* fs.remove(paths.versionDir, { recursive: true, force: true }).pipe(
396 Effect.mapError(
397 (cause) =>
398 new PinnedRuntimeInstallError({
399 step: "removing an incomplete pinned runtime",
400 cause,
401 }),
402 ),
403 );
404 }
405
406 const versionsDir = input.path.dirname(paths.versionDir);
407 yield* fs.makeDirectory(versionsDir, { recursive: true }).pipe(
408 Effect.mapError(
409 (cause) =>
410 new PinnedRuntimeInstallError({
411 step: "preparing the pinned runtime directory",
412 cause,
413 }),
414 ),
415 );
416 const stagingDir = yield* fs
417 .makeTempDirectory({
418 directory: versionsDir,
419 prefix: ".staging-",
420 })
421 .pipe(
422 Effect.mapError(
423 (cause) =>
424 new PinnedRuntimeInstallError({
425 step: "preparing the pinned runtime directory",
426 cause,
427 }),
428 ),
429 );
430 const stagingPaths: PinnedRuntimePaths = {
431 versionDir: stagingDir,
432 entryPath: input.path.join(stagingDir, input.path.relative(paths.versionDir, paths.entryPath)),
433 sentinelPath: input.path.join(stagingDir, ".install-complete"),
434 };
435
436 return yield* Effect.gen(function* () {
437 yield* installFromArchive(input, stagingDir);
438
439 input.onProgress?.({ stage: "validate" });
440 yield* input.validate(stagingPaths);
441 yield* fs
442 .writeFileString(stagingPaths.sentinelPath, `${input.version}\n`)
443 .pipe(
444 Effect.mapError(
445 (cause) =>
446 new PinnedRuntimeInstallError({ step: "recording the completed install", cause }),
447 ),
448 );
449 const published = yield* fs.rename(stagingDir, paths.versionDir).pipe(
450 Effect.as(true),
451 Effect.catch((cause) =>
452 Effect.all([
453 fs.exists(paths.entryPath),
454 fs.readFileString(paths.sentinelPath).pipe(Effect.option),
455 ]).pipe(
456 Effect.mapError(
457 (checkCause) =>
458 new PinnedRuntimeInstallError({
459 step: "checking a concurrently published pinned runtime",
460 cause: checkCause,
461 }),
462 ),
463 Effect.flatMap(([publishedEntryExists, publishedSentinel]) =>
464 publishedEntryExists &&
465 Option.isSome(publishedSentinel) &&
466 publishedSentinel.value.trim() === input.version
467 ? Effect.succeed(false)
468 : Effect.fail(
469 new PinnedRuntimeInstallError({
470 step: "publishing the pinned runtime",
471 cause,
472 }),
473 ),
474 ),
475 ),
476 ),
477 );
478 if (!published) yield* input.validate(paths);
479 return paths;
480 }).pipe(
481 Effect.ensuring(fs.remove(stagingDir, { recursive: true, force: true }).pipe(Effect.ignore)),
482 );
483 });
484
485 export const ensurePinnedRuntimeInstalled = (input: PinnedRuntimeInstallInput) =>
486 pinnedRuntimeInstallLock.withPermit(installPinnedRuntime(input));
487
488 const PINNED_RUNTIME_PRUNED_PREFIX = ".pruned-";
489 /**
490 * Another process may have just published a runtime it has not yet pointed anything at: an
491 * update, a downgrade, or the install script between unpacking and linking. The install lock only
492 * covers this process, so recently completed runtimes are left for a later prune.
493 */
494 const PINNED_RUNTIME_PRUNE_GRACE = Duration.hours(1);
495 /**
496 * One `t3` launcher path per line, appended by the install scripts and by any prune that finds a
497 * launcher on PATH. A service does not see the installer's `T3CODE_INSTALL_BIN_DIR` or the
498 * interactive PATH, so this is how it finds a custom one, including installs that predate the file.
499 */
500 const PINNED_RUNTIME_LAUNCHERS_FILE = "launchers";
501
502 /**
503 * Starts on a fresh line whatever the file ends with when the append lands: another process's
504 * append may have been cut short since this one read the file. Readers skip the blank lines.
505 */
506 const launcherRecords = (launchers: ReadonlyArray<string>) =>
507 `\n${launchers.map((launcher) => `${launcher}\n`).join("")}`;
508
509 export function launcherOwnsVersionsDir(
510 path: Path.Path,
511 versionsDir: string,
512 candidate: string,
513 ): boolean {
514 const relative = path.relative(versionsDir, path.resolve(candidate));
515 return relative.length > 0 && !relative.startsWith("..") && !path.isAbsolute(relative);
516 }
517
518 /** Node preserves argv0's spelling; resolve bare names on PATH to find the launcher symlink. */
519 export const resolveLauncherPath = Effect.gen(function* () {
520 const path = yield* Path.Path;
521 const fs = yield* FileSystem.FileSystem;
522 const invokedAs = yield* HostProcess.InvokedAs;
523 const cwd = yield* HostProcess.WorkingDirectory;
524 const environment = yield* HostProcess.Environment;
525 const platform = yield* HostProcess.Platform;
526 if (invokedAs.includes("/") || invokedAs.includes("\\")) {
527 return path.resolve(cwd, invokedAs);
528 }
529 const delimiter = platform === "win32" ? ";" : ":";
530 for (const directory of (environment["PATH"] ?? "").split(delimiter)) {
531 if (directory.length === 0) continue;
532 const candidate = path.resolve(cwd, directory, invokedAs);
533 const info = yield* fs.stat(candidate).pipe(Effect.option);
534 // Like the shell, pass over directories and non-executable files named the same.
535 if (
536 Option.isSome(info) &&
537 info.value.type === "File" &&
538 (platform === "win32" || (info.value.mode & 0o111) !== 0)
539 ) {
540 return candidate;
541 }
542 }
543 return undefined;
544 });
545
546 /**
547 * `t3 update` repoints the launcher it was started through, which may be on neither PATH nor the
548 * default bin dir. Unrecorded, the next prune would remove the runtime it now points at.
549 */
550 export const recordPinnedRuntimeLauncher = Effect.fn("cloud.pinned_runtime.record_launcher")(
551 function* (input: { readonly baseDir: string; readonly launcher: string }) {
552 const fs = yield* FileSystem.FileSystem;
553 const path = yield* Path.Path;
554 const launchersFile = path.join(
555 input.baseDir,
556 PINNED_RUNTIME_DIR,
557 PINNED_RUNTIME_LAUNCHERS_FILE,
558 );
559 // Prune may run in another process, such as the service, whose working directory differs.
560 const launcher = path.resolve(input.launcher);
561 const recorded = yield* fs.readFileString(launchersFile).pipe(Effect.orElseSucceed(() => ""));
562 if (recorded.split(/\r?\n/).includes(launcher)) return;
563 yield* fs.writeFileString(launchersFile, launcherRecords([launcher]), { flag: "a" });
564 },
565 );
566
567 /** Whether `candidate` is a version's own executable, `<versionsDir>/<version>/t3[.exe]`. */
568 export function isVersionExecutable(
569 path: Path.Path,
570 versionsDir: string,
571 candidate: string,
572 ): boolean {
573 const relative = path.relative(versionsDir, path.resolve(candidate));
574 // On another Windows drive, relative() returns the absolute path, like `D:\t3.exe`.
575 return !path.isAbsolute(relative) && /^(?!\.\.)[^\\/]+[\\/]t3(?:\.exe)?$/i.test(relative);
576 }
577
578 /** The executable a `t3.cmd` shim written by the install script or `t3 update` runs. */
579 export const windowsShimTarget = (contents: string) => /^"([^"]+)"/m.exec(contents)?.[1];
580
581 /** Like {@link windowsShimTarget}, but only for a shim exactly as written; never a user's wrapper. */
582 export const generatedWindowsShimTarget = (contents: string) =>
583 /^@echo off\r?\n"([^"]+)" %\*\s*$/.exec(contents)?.[1];
584
585 /**
586 * The `t3` script the install scripts and `t3 update` write on macOS and Linux. Exec by absolute
587 * path: through a symlink, argv0 is the link, and the Cursor SDK looks for its helpers beside it.
588 */
589 export const posixLauncherScript = (executable: string) =>
590 `#!/bin/sh\nexec '${executable.replaceAll("'", "'\\''")}' "$@"\n`;
591
592 /** The executable a script exactly as {@link posixLauncherScript} writes it runs; never a user's wrapper. */
593 export const readPosixLauncherTarget = Effect.fn("cloud.pinned_runtime.read_posix_launcher")(
594 function* (launcher: string) {
595 const fs = yield* FileSystem.FileSystem;
596 // Stat first so a runtime binary found the same way is never read whole.
597 const info = yield* fs.stat(launcher).pipe(Effect.option);
598 if (Option.isNone(info) || info.value.type !== "File" || info.value.size > 4096n) {
599 return undefined;
600 }
601 const contents = yield* fs.readFileString(launcher).pipe(Effect.option);
602 if (Option.isNone(contents)) return undefined;
603 const quoted = /^#!\/bin\/sh\nexec '((?:[^']|'\\'')*)' "\$@"\n$/.exec(contents.value)?.[1];
604 return quoted?.replaceAll("'\\''", "'");
605 },
606 );
607
608 /**
609 * The `t3` this executable was started through on macOS or Linux: a legacy symlink into this
610 * home's runtime/versions, or, since a script execs the executable and argv0 names it, an unedited
611 * {@link posixLauncherScript} that runs it.
612 */
613 export const findPosixLauncher = Effect.fn("cloud.pinned_runtime.find_posix_launcher")(
614 function* (input: { readonly launchedAs: string; readonly versionsDir: string }) {
615 const fs = yield* FileSystem.FileSystem;
616 const path = yield* Path.Path;
617 const environment = yield* HostProcess.Environment;
618 const linkTarget = yield* fs.readLink(input.launchedAs).pipe(Effect.option);
619 if (Option.isSome(linkTarget)) {
620 const resolved = path.resolve(path.dirname(input.launchedAs), linkTarget.value);
621 return isVersionExecutable(path, input.versionsDir, resolved) ? input.launchedAs : undefined;
622 }
623 const realPath = (file: string) =>
624 fs.realPath(file).pipe(Effect.orElseSucceed(() => undefined));
625 const executable = yield* realPath(input.launchedAs);
626 const realVersionsDir = yield* realPath(input.versionsDir);
627 if (
628 executable === undefined ||
629 realVersionsDir === undefined ||
630 !isVersionExecutable(path, realVersionsDir, executable)
631 ) {
632 return undefined;
633 }
634 const recorded = yield* fs
635 .readFileString(path.join(path.dirname(input.versionsDir), "launchers"))
636 .pipe(Effect.orElseSucceed(() => ""));
637 const cwd = yield* HostProcess.WorkingDirectory;
638 const candidates = [
639 // PATH first, resolved like the shell does: it decides which t3 the user runs.
640 ...(environment["PATH"] ?? "")
641 .split(":")
642 .filter((directory) => directory.length > 0)
643 .map((directory) => path.resolve(cwd, directory, "t3")),
644 path.join(
645 environment["T3CODE_INSTALL_BIN_DIR"] ||
646 path.join(yield* HostProcess.HomeDirectory, ".local", "bin"),
647 "t3",
648 ),
649 ...recorded.split(/\r?\n/),
650 ].filter((candidate) => path.isAbsolute(candidate));
651 for (const candidate of candidates) {
652 // The script itself, so an alias that links to it keeps working.
653 const script = yield* realPath(candidate);
654 if (script === undefined) continue;
655 // Like the shell, pass over a script nobody can execute.
656 const info = yield* fs.stat(script).pipe(Effect.option);
657 if (Option.isNone(info) || (info.value.mode & 0o111) === 0) continue;
658 const target = yield* readPosixLauncherTarget(script);
659 if (target !== undefined && (yield* realPath(target)) === executable) return script;
660 }
661 return undefined;
662 },
663 );
664
665 /** Writes {@link posixLauncherScript} at `launcher` in one rename, replacing a legacy symlink rather than writing through it. */
666 export const writePosixLauncher = Effect.fn("cloud.pinned_runtime.write_posix_launcher")(
667 function* (input: {
668 readonly launcher: string;
669 readonly executable: string;
670 /** Fail instead of replacing a file that is there, even one created after the caller looked. */
671 readonly exclusive: boolean;
672 }) {
673 const fs = yield* FileSystem.FileSystem;
674 const path = yield* Path.Path;
675 const staging = yield* fs.makeTempFileScoped({
676 directory: path.dirname(input.launcher),
677 prefix: ".t3-",
678 });
679 yield* fs.writeFileString(staging, posixLauncherScript(input.executable));
680 yield* fs.chmod(staging, 0o755);
681 yield* input.exclusive ? fs.link(staging, input.launcher) : fs.rename(staging, input.launcher);
682 },
683 Effect.scoped,
684 );
685
686 /** Where the `t3` launchers written by the install scripts and `t3 update` point. */
687 const shellLauncherTargets = Effect.fn("cloud.pinned_runtime.shell_launcher_targets")(function* (
688 baseDir: string,
689 ) {
690 const fs = yield* FileSystem.FileSystem;
691 const path = yield* Path.Path;
692 const platform = yield* HostProcess.Platform;
693 const environment = yield* HostProcess.Environment;
694 const home = yield* HostProcess.HomeDirectory;
695 const launcherName = platform === "win32" ? "t3.cmd" : "t3";
696 const launchersFile = path.join(baseDir, PINNED_RUNTIME_DIR, PINNED_RUNTIME_LAUNCHERS_FILE);
697 const recorded = (yield* fs.readFileString(launchersFile).pipe(Effect.orElseSucceed(() => "")))
698 .split(/\r?\n/)
699 .filter((launcher) => launcher.trim().length > 0);
700 const found = [
701 environment["T3CODE_INSTALL_BIN_DIR"] || path.join(home, ".local", "bin"),
702 ...(environment["PATH"] ?? environment["Path"] ?? "").split(platform === "win32" ? ";" : ":"),
703 ]
704 .filter((directory) => path.isAbsolute(directory))
705 .map((directory) => path.join(directory, launcherName));
706 const versionsDir = pinnedRuntimeVersionsDir(path, baseDir);
707 const realVersionsDir = yield* fs
708 .realPath(versionsDir)
709 .pipe(Effect.orElseSucceed(() => versionsDir));
710 const inVersionsDir = (candidate: string) =>
711 launcherOwnsVersionsDir(path, versionsDir, candidate) ||
712 launcherOwnsVersionsDir(path, realVersionsDir, candidate);
713 const unrecorded: Array<string> = [];
714 const targets: Array<string> = [];
715 for (const launcher of [...recorded, ...found]) {
716 const resolved =
717 platform === "win32"
718 ? yield* fs.readFileString(launcher).pipe(
719 Effect.map(windowsShimTarget),
720 Effect.orElseSucceed(() => undefined),
721 )
722 : // Fully resolved, through chained links and a symlinked T3 home alike.
723 yield* fs.realPath(launcher).pipe(Effect.orElseSucceed(() => undefined));
724 if (resolved === undefined) continue;
725 const scriptTarget =
726 platform === "win32" ? undefined : yield* readPosixLauncherTarget(resolved);
727 const target =
728 scriptTarget === undefined
729 ? resolved
730 : yield* fs.realPath(scriptTarget).pipe(Effect.orElseSucceed(() => scriptTarget));
731 // A runtime's own entry, on the macOS service PATH or recorded by an earlier prune, is no launcher.
732 // It is named as `versions/<version>/t3`, and neither it nor its directory is a link.
733 const entryDir = path.dirname(path.resolve(launcher));
734 const parent = path.dirname(entryDir);
735 if (
736 platform !== "win32" &&
737 scriptTarget === undefined &&
738 (parent === versionsDir || parent === realVersionsDir) &&
739 target === path.join(realVersionsDir, path.basename(entryDir), path.basename(launcher))
740 ) {
741 continue;
742 }
743 targets.push(target);
744 if (inVersionsDir(target) && !recorded.includes(launcher) && !unrecorded.includes(launcher)) {
745 unrecorded.push(launcher);
746 }
747 }
748 if (unrecorded.length > 0) {
749 yield* fs
750 .writeFileString(launchersFile, launcherRecords(unrecorded), { flag: "a" })
751 .pipe(Effect.ignore);
752 }
753 return targets;
754 });
755
756 /**
757 * Resolved executables rather than command lines: a server started through the `t3` symlink
758 * shows up as `t3 serve`, possibly from another T3 home that runs this home's runtimes. Windows
759 * has no symlink launcher, and its `t3.cmd` shim runs the absolute path, so command lines do there.
760 */
761 const runningExecutables = Effect.fn("cloud.pinned_runtime.running_executables")(function* () {
762 const fs = yield* FileSystem.FileSystem;
763 const runner = yield* ProcessRunner.ProcessRunner;
764 const platform = yield* HostProcess.Platform;
765 if (platform === "linux") {
766 const pids = yield* fs.readDirectory("/proc").pipe(Effect.option);
767 if (Option.isNone(pids)) return Option.none<string>();
768 const executables: Array<string> = [];
769 for (const pid of pids.value) {
770 if (!/^\d+$/.test(pid)) continue;
771 // Unreadable ones belong to other users or are this user's non-dumpable daemons (systemd
772 // --user, ssh-agent). Neither runs a t3 runtime, and bailing on them would never prune.
773 const executable = yield* fs.readLink(`/proc/${pid}/exe`).pipe(Effect.option);
774 if (Option.isSome(executable)) executables.push(executable.value);
775 }
776 return executables.length === 0 ? Option.none<string>() : Option.some(executables.join("\n"));
777 }
778 const result = yield* runner
779 .run({
780 ...(platform === "win32"
781 ? {
782 command: "powershell.exe",
783 args: [
784 "-NoProfile",
785 "-NonInteractive",
786 "-Command",
787 "[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false); Get-CimInstance Win32_Process | Select-Object -ExpandProperty CommandLine",
788 ],
789 }
790 : { command: "lsof", args: ["-nP", "-w", "-d", "txt", "-F", "n"] }),
791 timeout: Duration.seconds(10),
792 maxOutputBytes: 16 * 1024 * 1024,
793 })
794 .pipe(Effect.option);
795 return Option.isSome(result) && result.value.code === 0 && result.value.stdout.length > 0
796 ? Option.some(result.value.stdout)
797 : Option.none<string>();
798 });
799
800 const selectUnusedRuntimes = Effect.fn("cloud.pinned_runtime.select_unused")(function* (input: {
801 readonly baseDir: string;
802 readonly keep: ReadonlyArray<string>;
803 readonly references: ReadonlyArray<string>;
804 }) {
805 const fs = yield* FileSystem.FileSystem;
806 const path = yield* Path.Path;
807 const versionsDir = pinnedRuntimeVersionsDir(path, input.baseDir);
808 const entries = yield* fs.readDirectory(versionsDir).pipe(Effect.orElseSucceed(() => []));
809 // An interrupted prune has already renamed its runtime out of the way.
810 const pruned = new Set(
811 entries
812 .filter((entry) => entry.startsWith(PINNED_RUNTIME_PRUNED_PREFIX))
813 .map((entry) => path.join(versionsDir, entry)),
814 );
815
816 const settledBefore =
817 (yield* Clock.currentTimeMillis) - Duration.toMillis(PINNED_RUNTIME_PRUNE_GRACE);
818 const completed: Array<string> = [];
819 for (const version of entries) {
820 if (!isExactServiceVersion(version)) continue;
821 const versionDir = path.join(versionsDir, version);
822 const sentinelPath = path.join(versionDir, ".install-complete");
823 const isLink = yield* fs.readLink(versionDir).pipe(Effect.isSuccess);
824 const sentinel = yield* fs.readFileString(sentinelPath).pipe(Effect.option);
825 const completedAt = yield* fs.stat(sentinelPath).pipe(
826 Effect.map((info) => Option.getOrUndefined(info.mtime)?.getTime()),
827 Effect.orElseSucceed(() => undefined),
828 );
829 if (
830 !isLink &&
831 Option.isSome(sentinel) &&
832 sentinel.value.trim() === version &&
833 completedAt !== undefined &&
834 completedAt < settledBefore
835 ) {
836 completed.push(version);
837 }
838 }
839 if (completed.length === 0) return pruned;
840
841 const keep = new Set(input.keep);
842 const stateText = yield* fs
843 .readFileString(path.join(input.baseDir, PINNED_RUNTIME_DIR, SERVICE_STATE_FILE))
844 .pipe(
845 Effect.asSome,
846 Effect.catch((error) =>
847 Effect.succeed(error.reason._tag === "NotFound" ? Option.none<string>() : undefined),
848 ),
849 );
850 // State this version cannot read may reference runtimes in ways it does not know.
851 if (stateText === undefined) return pruned;
852 if (Option.isSome(stateText)) {
853 const state = parseServiceState(stateText.value);
854 if (state === undefined) return pruned;
855 keep.add(state.activeVersion);
856 if (state.update !== undefined) {
857 keep.add(state.update.fromVersion);
858 keep.add(state.update.targetVersion);
859 }
860 }
861 const executables = yield* runningExecutables();
862 // Without a process list, a runtime in use would look unused.
863 if (Option.isNone(executables)) return pruned;
864 const platform = yield* HostProcess.Platform;
865 // Windows paths are case-insensitive, and shims and process lists need not agree on case.
866 const foldCase = (value: string) => (platform === "win32" ? value.toLowerCase() : value);
867 const references = [
868 executables.value,
869 ...(yield* shellLauncherTargets(input.baseDir)),
870 ...input.references,
871 ].map(foldCase);
872 // Process executables are resolved, so they name the real path of a symlinked T3 home.
873 const realVersionsDir = yield* fs
874 .realPath(versionsDir)
875 .pipe(Effect.orElseSucceed(() => versionsDir));
876
877 for (const version of completed) {
878 const versionDir = path.join(versionsDir, version);
879 const prefixes = [versionDir, path.join(realVersionsDir, version)].map((dir) =>
880 foldCase(dir + path.sep),
881 );
882 if (
883 keep.has(version) ||
884 references.some((reference) => prefixes.some((prefix) => reference.includes(prefix)))
885 ) {
886 continue;
887 }
888 const target = path.join(versionsDir, `${PINNED_RUNTIME_PRUNED_PREFIX}${version}`);
889 // Renaming first means an interrupted removal never leaves a partial tree that still carries
890 // its sentinel and would pass for a complete install.
891 yield* fs.remove(target, { recursive: true, force: true }).pipe(
892 Effect.andThen(fs.rename(versionDir, target)),
893 Effect.andThen(Effect.sync(() => pruned.add(target))),
894 Effect.catch((cause) =>
895 Effect.logWarning("Could not remove an unused t3 runtime.", { version, cause }),
896 ),
897 );
898 }
899 return pruned;
900 });
901
902 /**
903 * Keep runtimes named by callers, service state, running processes, launchers, or references,
904 * and anything published within an hour. Only prune completed runtimes; leave staging
905 * directories, symlinks, and unexpected entries untouched.
906 */
907 export const prunePinnedRuntimes = Effect.fn("cloud.pinned_runtime.prune")(function* (input: {
908 readonly baseDir: string;
909 readonly keep: ReadonlyArray<string>;
910 readonly references?: ReadonlyArray<string>;
911 }) {
912 const fs = yield* FileSystem.FileSystem;
913 const path = yield* Path.Path;
914 // Only selection and the renames race an install; deleting renamed trees can take a while.
915 const pruned = yield* pinnedRuntimeInstallLock.withPermit(
916 selectUnusedRuntimes({ ...input, references: input.references ?? [] }),
917 );
918 const removed: Array<string> = [];
919 for (const target of pruned) {
920 yield* fs.remove(target, { recursive: true, force: true }).pipe(
921 Effect.andThen(
922 Effect.sync(() =>
923 removed.push(path.basename(target).slice(PINNED_RUNTIME_PRUNED_PREFIX.length)),
924 ),
925 ),
926 Effect.catch((cause) =>
927 Effect.logWarning("Could not remove an unused t3 runtime.", { target, cause }),
928 ),
929 );
930 }
931 return removed;
932 });