@devries-cameron20 / t3code Mirror · read-only / commits / 2b28b46daa
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 22, 22:00 UTC

fix(server): updates no longer keep every old runtime on disk (#22253)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 20, 2026 at 09:59 UTC 2b28b46daa10d419bb79613139e046a9324b86b0
10 files changed +563 -26
apps/server/src/cli/uninstall.ts
+7 -3
@@ -14,10 +14,14 @@ import * as Schema from "effect/Schema";
14 import { Command, Flag, GlobalFlag, Prompt } from "effect/cli";
15
16 import * as BootService from "../cloud/bootService.ts";
17 -import { pinnedRuntimeVersionsDir } from "../cloud/pinnedRuntime.ts";
17 +import {
18 + launcherOwnsVersionsDir,
19 + pinnedRuntimeVersionsDir,
20 + windowsShimTarget,
21 +} from "../cloud/pinnedRuntime.ts";
22 import { projectLocationFlags, resolveCliAuthConfig } from "./config.ts";
23 import * as CliService from "./service.ts";
20 -import { findWindowsShim, launcherOwnsVersionsDir, resolveLauncherPath } from "./update.ts";
24 +import { findWindowsShim, resolveLauncherPath } from "./update.ts";
25
26 export class CliUninstallError extends Schema.TaggedError<CliUninstallError>()(
27 "CliUninstallError",
@@ -51,7 +55,7 @@ export const findOwnedLauncher = Effect.fn("cli.uninstall.find_launcher")(functi
55 const shimPath = yield* findWindowsShim(input.launchedAs);
56 if (shimPath === undefined) return undefined;
57 const contents = yield* fs.readFileString(shimPath).pipe(Effect.option);
54 - const target = Option.isSome(contents) ? /^"([^"]+)"/m.exec(contents.value)?.[1] : undefined;
58 + const target = Option.isSome(contents) ? windowsShimTarget(contents.value) : undefined;
59 return target !== undefined && launcherOwnsVersionsDir(path, input.versionsDir, target)
60 ? shimPath
61 : undefined;
apps/server/src/cli/update.ts
+16 -11
@@ -22,9 +22,12 @@ import packageJson from "../../package.json" with { type: "json" };
22 import * as BootService from "../cloud/bootService.ts";
23 import {
24 ensurePinnedRuntimeInstalled,
25 + launcherOwnsVersionsDir,
26 pinnedRuntimeCommand,
27 PinnedRuntimeInstallError,
28 pinnedRuntimePaths,
29 + prunePinnedRuntimes,
30 + windowsShimTarget,
31 } from "../cloud/pinnedRuntime.ts";
32 import { compareExactServiceVersions, isExactServiceVersion } from "../cloud/serviceProtocol.ts";
33 import * as ProcessRunner from "../processRunner.ts";
@@ -87,15 +90,6 @@ export const resolveNewestVersion = Effect.fn("cli.update.resolve_newest")(funct
90 return yield* new CliUpdateError({ reason: `No published ${channel} release was found.` });
91 });
92
90 -export function launcherOwnsVersionsDir(
91 - path: Path.Path,
92 - versionsDir: string,
93 - candidate: string,
94 -): boolean {
95 - const relative = path.relative(versionsDir, path.resolve(candidate));
96 - return relative.length > 0 && !relative.startsWith("..") && !path.isAbsolute(relative);
97 -}
98 -
93 /**
94 * 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.
@@ -116,7 +110,7 @@ export const repointLauncher = Effect.fn("cli.update.repoint_launcher")(function
110 const shimPath = yield* findWindowsShim(input.launchedAs);
111 if (shimPath === undefined) return Option.none<string>();
112 const current = yield* fs.readFileString(shimPath).pipe(Effect.option);
119 - const quoted = Option.isSome(current) ? /^"([^"]+)"/m.exec(current.value)?.[1] : undefined;
113 + const quoted = Option.isSome(current) ? windowsShimTarget(current.value) : undefined;
114 if (quoted === undefined || !ownsTarget(quoted)) return Option.none<string>();
115 yield* fs
116 .writeFileString(shimPath, `@echo off\r\n"${input.targetEntryPath}" %*`)
@@ -188,7 +182,7 @@ export const findWindowsShim = Effect.fn("cli.update.find_windows_shim")(functio
182 const shimPath = path.join(directory, "t3.cmd");
183 const contents = yield* fs.readFileString(shimPath).pipe(Effect.option);
184 if (Option.isNone(contents)) continue;
191 - const target = /^"([^"]+)"/m.exec(contents.value)?.[1];
185 + const target = windowsShimTarget(contents.value);
186 if (
187 target !== undefined &&
188 path.resolve(target).toLowerCase() === path.resolve(executablePath).toLowerCase()
@@ -521,6 +515,17 @@ const runUpdate = Effect.fn("cli.update.run")(function* (input: {
515 );
516 }
517
518 + // A handed-off service install rewrites the unit later, while the old launcher may be stopped.
519 + if (servicePlan?.handedOff !== true) {
520 + // A stopped service's unit still names the launcher runtime it will start.
521 + const unit = yield* fs.readFileString(status.unitPath).pipe(Effect.option);
522 + yield* prunePinnedRuntimes({
523 + baseDir: input.baseDir,
524 + keep: [currentVersion, targetVersion],
525 + references: Option.toArray(Option.flatMapNullishOr(unit, BootService.bootServiceProgramOf)),
526 + });
527 + }
528 +
529 progress.success(`Installed T3 Code ${targetVersion}`);
530 if (Option.isSome(repointed)) {
531 yield* Console.log(" Run t3 to get started.\n");
apps/server/src/cloud/bootService.test.ts
+17
@@ -64,6 +64,23 @@ it("reads the served T3 home back out of a rendered unit or plist", () => {
64 expect(BootService.bootServiceBaseDirOf("[Service]\nExecStart=/x\n")).toBeUndefined();
65 });
66
67 +it.each([
68 + ["a unit", '/home/theo/T3 "Data"/100%\\x', BootService.renderBootServiceUnit],
69 + [
70 + "a plist",
71 + "/Users/theo/a&b <c>",
72 + (plan: BootService.BootServicePlan) =>
73 + BootService.renderBootServicePlist(plan, { homeDir: "/Users/theo", environmentPath: "" }),
74 + ],
75 +] as const)("reads the launcher back out of %s", (_, baseDir, render) => {
76 + const program = `${baseDir}/runtime/versions/1.2.3/t3`;
77 + expect(
78 + BootService.bootServiceProgramOf(
79 + render({ program: [program, "__service-launcher"], baseDir, logPath: "/log", unitPath: "" }),
80 + ),
81 + ).toBe(program);
82 +});
83 +
84 it("survives the kernel OOM-killing a greedy agent child", () => {
85 const unit = BootService.renderBootServiceUnit(linuxPlan);
86
apps/server/src/cloud/bootService.ts
+21 -12
@@ -53,21 +53,30 @@ function quoteSystemdValue(value: string): string {
53 : escaped;
54 }
55
56 +const unquoteSystemdValue = (raw: string) =>
57 + (raw.startsWith('"') && raw.endsWith('"')
58 + ? raw.slice(1, -1).replaceAll('\\"', '"').replaceAll("\\\\", "\\")
59 + : raw
60 + ).replaceAll("%%", "%");
61 +
62 +const unescapeXmlText = (value: string) =>
63 + value.replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll("&amp;", "&");
64 +
65 export function bootServiceBaseDirOf(contents: string): string | undefined {
66 const systemd = /^Environment=T3CODE_HOME=(.*)$/m.exec(contents)?.[1];
58 - if (systemd !== undefined) {
59 - const raw = systemd.trim();
60 - const unquoted =
61 - raw.startsWith('"') && raw.endsWith('"')
62 - ? raw.slice(1, -1).replaceAll('\\"', '"').replaceAll("\\\\", "\\")
63 - : raw;
64 - return unquoted.replaceAll("%%", "%");
65 - }
67 + if (systemd !== undefined) return unquoteSystemdValue(systemd.trim());
68 const plist = /<key>T3CODE_HOME<\/key>\s*<string>([^<]*)<\/string>/.exec(contents)?.[1];
67 - if (plist !== undefined) {
68 - return plist.replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll("&amp;", "&");
69 - }
70 - return undefined;
69 + return plist === undefined ? undefined : unescapeXmlText(plist);
70 +}
71 +
72 +/** The executable an installed unit or plist starts: the launcher's runtime. */
73 +export function bootServiceProgramOf(contents: string): string | undefined {
74 + const systemd = /^ExecStart=("(?:\\.|[^"\\])*"|\S+)/m.exec(contents)?.[1];
75 + if (systemd !== undefined) return unquoteSystemdValue(systemd);
76 + const plist = /<key>ProgramArguments<\/key>\s*<array>\s*<string>([^<]*)<\/string>/.exec(
77 + contents,
78 + )?.[1];
79 + return plist === undefined ? undefined : unescapeXmlText(plist);
80 }
81
82 export interface BootServicePlan {
apps/server/src/cloud/pinnedRuntime.test.ts
+207
@@ -8,8 +8,11 @@ import * as Fiber from "effect/Fiber";
8 import * as Path from "effect/Path";
9 import * as TestClock from "effect/testing/TestClock";
10 import { HttpClient, HttpClientResponse } from "effect/http";
11 +import * as ChildProcess from "effect/process/ChildProcess";
12 import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
13
14 +import * as HostProcess from "@t3tools/shared/HostProcess";
15 +
16 import * as ProcessRunner from "../processRunner.ts";
17 import {
18 ensurePinnedRuntimeInstalled,
@@ -17,7 +20,9 @@ import {
20 pinnedRuntimePaths,
21 PinnedRuntimeInstallError,
22 type PinnedRuntimeProgress,
23 + prunePinnedRuntimes,
24 } from "./pinnedRuntime.ts";
25 +import { SERVICE_LAUNCHER_PROTOCOL } from "./serviceProtocol.ts";
26
27 const version = "1.2.3";
28 const archiveName = `t3-${version}-linux-x64.tar.gz`;
@@ -553,3 +558,205 @@ it.layer(NodeServices.layer)("ensurePinnedRuntimeInstalled", (it) => {
558 }),
559 );
560 });
561 +
562 +it.layer(NodeServices.layer)("prunePinnedRuntimes", (it) => {
563 + const lsofRunner = (stdout: string) =>
564 + ProcessRunner.ProcessRunner.of({
565 + run: () =>
566 + Effect.succeed({
567 + stdout,
568 + stderr: "",
569 + code: ChildProcessSpawner.ExitCode(0),
570 + timedOut: false,
571 + stdoutTruncated: false,
572 + stderrTruncated: false,
573 + stdoutInvalidUtf8: false,
574 + stderrInvalidUtf8: false,
575 + }),
576 + });
577 +
578 + const setup = Effect.fn(function* (versions: ReadonlyArray<string>) {
579 + const fs = yield* FileSystem.FileSystem;
580 + const path = yield* Path.Path;
581 + const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-prune-" });
582 + const versionsDir = path.join(baseDir, "runtime", "versions");
583 + const now = Duration.toMillis(Duration.days(1));
584 + yield* TestClock.setTime(now);
585 + const install = Effect.fn(function* (
586 + version: string,
587 + completed: "long ago" | "just now" | "never" = "long ago",
588 + ) {
589 + const directory = path.join(versionsDir, version);
590 + yield* fs.makeDirectory(directory, { recursive: true });
591 + yield* fs.writeFileString(path.join(directory, "t3"), "");
592 + if (completed === "never") return;
593 + const sentinel = path.join(directory, ".install-complete");
594 + yield* fs.writeFileString(sentinel, `${version}\n`);
595 + const at = completed === "just now" ? now : 0;
596 + yield* fs.utimes(sentinel, at, at);
597 + });
598 + for (const version of versions) yield* install(version);
599 + return { baseDir, versionsDir, install };
600 + });
601 +
602 + it.effect("removes only completed runtimes nothing references", () =>
603 + Effect.gen(function* () {
604 + const fs = yield* FileSystem.FileSystem;
605 + const path = yield* Path.Path;
606 + const { baseDir, versionsDir, install } = yield* setup([
607 + "1.0.0",
608 + "1.1.0",
609 + "1.2.0",
610 + "1.2.5",
611 + "1.3.0",
612 + "1.3.5",
613 + "1.4.0",
614 + "1.5.0",
615 + "2.0.0",
616 + "2.1.0",
617 + ]);
618 + // A downgrade another process installed but has not pointed anything at yet.
619 + yield* install("0.5.0", "just now");
620 + yield* install("1.0.1", "never");
621 + yield* install(".staging-abc", "never");
622 + yield* install(".pruned-0.8.0");
623 + yield* install("other", "never");
624 + yield* fs.symlink(path.join(versionsDir, "other"), path.join(versionsDir, "0.9.0"));
625 + const binDir = path.join(baseDir, "bin");
626 + const customBinDir = path.join(baseDir, "custom-bin");
627 + yield* fs.makeDirectory(binDir);
628 + yield* fs.makeDirectory(customBinDir);
629 + // Chained: the launcher reaches its runtime through a second link.
630 + yield* fs.symlink(path.join(versionsDir, "1.2.0", "t3"), path.join(baseDir, "alias-t3"));
631 + yield* fs.symlink(path.join(baseDir, "alias-t3"), path.join(binDir, "t3"));
632 + yield* fs.symlink(path.join(versionsDir, "1.2.5", "t3"), path.join(customBinDir, "t3"));
633 + yield* fs.writeFileString(
634 + path.join(baseDir, "runtime", "launchers"),
635 + `${path.join(customBinDir, "t3")}\n`,
636 + );
637 + yield* fs.writeFileString(
638 + path.join(baseDir, "runtime", "service-state.json"),
639 + JSON.stringify({
640 + protocol: SERVICE_LAUNCHER_PROTOCOL,
641 + activeVersion: "1.4.0",
642 + update: { id: "u", fromVersion: "1.4.0", targetVersion: "1.5.0", status: "rolled-back" },
643 + }),
644 + );
645 +
646 + const removed = yield* prunePinnedRuntimes({
647 + baseDir,
648 + keep: ["2.0.0"],
649 + references: [`ExecStart=${path.join(versionsDir, "1.3.5", "t3")} __service-launcher`],
650 + }).pipe(
651 + Effect.provideService(
652 + ProcessRunner.ProcessRunner,
653 + lsofRunner(`p42\nftxt\nn${path.join(versionsDir, "1.3.0", "t3")}\n`),
654 + ),
655 + Effect.provideService(HostProcess.Platform, "darwin"),
656 + Effect.provideService(HostProcess.Environment, { T3CODE_INSTALL_BIN_DIR: binDir }),
657 + );
658 +
659 + // 1.2.0 is the default launcher, 1.2.5 a recorded one, 1.3.0 running, 1.3.5 in the unit,
660 + // 1.4.0 active, 1.5.0 the rolled-back target, 2.0.0 the caller's, 0.5.0 just installed.
661 + // Being newer than the caller's version does not keep 2.1.0.
662 + assert.deepEqual(removed.toSorted(), ["0.8.0", "1.0.0", "1.1.0", "2.1.0"]);
663 + // The launcher found on PATH is remembered for a service that cannot see it.
664 + assert.equal(
665 + yield* fs.readFileString(path.join(baseDir, "runtime", "launchers")),
666 + `${path.join(customBinDir, "t3")}\n${path.join(binDir, "t3")}\n`,
667 + );
668 + assert.deepEqual((yield* fs.readDirectory(versionsDir)).toSorted(), [
669 + ".staging-abc",
670 + "0.5.0",
671 + "0.9.0",
672 + "1.0.1",
673 + "1.2.0",
674 + "1.2.5",
675 + "1.3.0",
676 + "1.3.5",
677 + "1.4.0",
678 + "1.5.0",
679 + "2.0.0",
680 + "other",
681 + ]);
682 + }),
683 + );
684 +
685 + it.effect("matches a Windows shim whatever its casing", () =>
686 + Effect.gen(function* () {
687 + const fs = yield* FileSystem.FileSystem;
688 + const path = yield* Path.Path;
689 + const { baseDir, versionsDir } = yield* setup(["1.0.0", "1.1.0"]);
690 + const shim = path.join(baseDir, "t3.cmd");
691 + yield* fs.writeFileString(
692 + shim,
693 + `@echo off\r\n"${path.join(versionsDir, "1.0.0", "t3.exe").toUpperCase()}" %*`,
694 + );
695 + yield* fs.writeFileString(path.join(baseDir, "runtime", "launchers"), `${shim}\n`);
696 +
697 + const removed = yield* prunePinnedRuntimes({ baseDir, keep: ["2.0.0"] }).pipe(
698 + Effect.provideService(
699 + ProcessRunner.ProcessRunner,
700 + lsofRunner("C:\\Windows\\explorer.exe\n"),
701 + ),
702 + Effect.provideService(HostProcess.Platform, "win32"),
703 + Effect.provideService(HostProcess.Environment, {}),
704 + );
705 + assert.deepEqual(removed, ["1.1.0"]);
706 + }),
707 + );
708 +
709 + it.effect.each([
710 + { name: "the process list is unavailable", state: undefined, lsof: "" },
711 + { name: "the service state is unparseable", state: "{", lsof: "p1\nftxt\nn/sbin/launchd\n" },
712 + {
713 + name: "the service state cannot be read",
714 + state: null,
715 + lsof: "p1\nftxt\nn/sbin/launchd\n",
716 + },
717 + ])("keeps everything when $name", ({ state, lsof }) =>
718 + Effect.gen(function* () {
719 + const fs = yield* FileSystem.FileSystem;
720 + const path = yield* Path.Path;
721 + const { baseDir, versionsDir } = yield* setup(["1.0.0"]);
722 + const statePath = path.join(baseDir, "runtime", "service-state.json");
723 + if (state === null) yield* fs.makeDirectory(statePath);
724 + else if (state !== undefined) yield* fs.writeFileString(statePath, state);
725 +
726 + const removed = yield* prunePinnedRuntimes({ baseDir, keep: ["2.0.0"] }).pipe(
727 + Effect.provideService(ProcessRunner.ProcessRunner, lsofRunner(lsof)),
728 + Effect.provideService(HostProcess.Platform, "darwin"),
729 + Effect.provideService(HostProcess.Environment, { T3CODE_INSTALL_BIN_DIR: baseDir }),
730 + );
731 + assert.deepEqual(removed, []);
732 + assert.deepEqual(yield* fs.readDirectory(versionsDir), ["1.0.0"]);
733 + }),
734 + );
735 +
736 + it.effect.skipIf(HostProcess.Platform.defaultValue() !== "linux")(
737 + "keeps a runtime a process runs through a symlink",
738 + () =>
739 + Effect.gen(function* () {
740 + const fs = yield* FileSystem.FileSystem;
741 + const path = yield* Path.Path;
742 + const { baseDir, versionsDir } = yield* setup(["1.0.0", "1.1.0"]);
743 + const entry = path.join(versionsDir, "1.0.0", "t3");
744 + yield* fs.copyFile("/bin/sleep", entry);
745 + yield* fs.chmod(entry, 0o755);
746 + const link = path.join(baseDir, "t3");
747 + yield* fs.symlink(entry, link);
748 + // Its argv names only the link, as with `t3 serve` run from PATH. Removing the link after
749 + // spawning leaves the process's executable as the only reference.
750 + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
751 + yield* spawner.spawn(ChildProcess.make(link, ["30"]));
752 + yield* fs.remove(link);
753 +
754 + const removed = yield* prunePinnedRuntimes({ baseDir, keep: ["2.0.0"] }).pipe(
755 + Effect.provideService(HostProcess.Platform, "linux"),
756 + Effect.provideService(HostProcess.Environment, { T3CODE_INSTALL_BIN_DIR: baseDir }),
757 + Effect.provide(ProcessRunner.layer),
758 + );
759 + assert.deepEqual(removed, ["1.1.0"]);
760 + }),
761 + );
762 +});
apps/server/src/cloud/pinnedRuntime.ts
+267
@@ -1,4 +1,5 @@
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";
@@ -19,7 +20,10 @@ import {
20 parseChecksums,
21 } from "@t3tools/shared/cliRelease";
22
23 +import * as HostProcess from "@t3tools/shared/HostProcess";
24 +
25 import * as ProcessRunner from "../processRunner.ts";
26 +import { isExactServiceVersion, parseServiceState, SERVICE_STATE_FILE } from "./serviceProtocol.ts";
27
28 const PINNED_RUNTIME_DIR = "runtime";
29 const PINNED_RUNTIME_INSTALL_TIMEOUT = Duration.minutes(10);
@@ -364,3 +368,266 @@ const installPinnedRuntime = Effect.fn("cloud.pinned_runtime.ensure_installed")(
368
369 export const ensurePinnedRuntimeInstalled = (input: PinnedRuntimeInstallInput) =>
370 pinnedRuntimeInstallLock.withPermit(installPinnedRuntime(input));
371 +
372 +const PINNED_RUNTIME_PRUNED_PREFIX = ".pruned-";
373 +/**
374 + * Another process may have just published a runtime it has not yet pointed anything at: an
375 + * update, a downgrade, or the install script between unpacking and linking. The install lock only
376 + * covers this process, so recently completed runtimes are left for a later prune.
377 + */
378 +const PINNED_RUNTIME_PRUNE_GRACE = Duration.hours(1);
379 +/**
380 + * One `t3` launcher path per line, appended by the install scripts and by any prune that finds a
381 + * launcher on PATH. A service does not see the installer's `T3CODE_INSTALL_BIN_DIR` or the
382 + * interactive PATH, so this is how it finds a custom one, including installs that predate the file.
383 + */
384 +const PINNED_RUNTIME_LAUNCHERS_FILE = "launchers";
385 +
386 +export function launcherOwnsVersionsDir(
387 + path: Path.Path,
388 + versionsDir: string,
389 + candidate: string,
390 +): boolean {
391 + const relative = path.relative(versionsDir, path.resolve(candidate));
392 + return relative.length > 0 && !relative.startsWith("..") && !path.isAbsolute(relative);
393 +}
394 +
395 +/** The executable a `t3.cmd` shim written by the install script or `t3 update` runs. */
396 +export const windowsShimTarget = (contents: string) => /^"([^"]+)"/m.exec(contents)?.[1];
397 +
398 +/** Where the `t3` launchers written by the install scripts and `t3 update` point. */
399 +const shellLauncherTargets = Effect.fn("cloud.pinned_runtime.shell_launcher_targets")(function* (
400 + baseDir: string,
401 +) {
402 + const fs = yield* FileSystem.FileSystem;
403 + const path = yield* Path.Path;
404 + const platform = yield* HostProcess.Platform;
405 + const environment = yield* HostProcess.Environment;
406 + const home = yield* HostProcess.HomeDirectory;
407 + const launcherName = platform === "win32" ? "t3.cmd" : "t3";
408 + const launchersFile = path.join(baseDir, PINNED_RUNTIME_DIR, PINNED_RUNTIME_LAUNCHERS_FILE);
409 + const recorded = (yield* fs.readFileString(launchersFile).pipe(Effect.orElseSucceed(() => "")))
410 + .split(/\r?\n/)
411 + .filter((launcher) => launcher.trim().length > 0);
412 + const found = [
413 + environment["T3CODE_INSTALL_BIN_DIR"] || path.join(home, ".local", "bin"),
414 + ...(environment["PATH"] ?? environment["Path"] ?? "").split(platform === "win32" ? ";" : ":"),
415 + ]
416 + .filter((directory) => path.isAbsolute(directory))
417 + .map((directory) => path.join(directory, launcherName));
418 + const versionsDir = pinnedRuntimeVersionsDir(path, baseDir);
419 + const realVersionsDir = yield* fs
420 + .realPath(versionsDir)
421 + .pipe(Effect.orElseSucceed(() => versionsDir));
422 + const unrecorded: Array<string> = [];
423 + const targets: Array<string> = [];
424 + for (const launcher of [...recorded, ...found]) {
425 + const target =
426 + platform === "win32"
427 + ? yield* fs.readFileString(launcher).pipe(
428 + Effect.map(windowsShimTarget),
429 + Effect.orElseSucceed(() => undefined),
430 + )
431 + : // Fully resolved, through chained links and a symlinked T3 home alike.
432 + yield* fs.realPath(launcher).pipe(Effect.orElseSucceed(() => undefined));
433 + if (target === undefined) continue;
434 + targets.push(target);
435 + if (
436 + (launcherOwnsVersionsDir(path, versionsDir, target) ||
437 + launcherOwnsVersionsDir(path, realVersionsDir, target)) &&
438 + !recorded.includes(launcher) &&
439 + !unrecorded.includes(launcher)
440 + ) {
441 + unrecorded.push(launcher);
442 + }
443 + }
444 + if (unrecorded.length > 0) {
445 + yield* fs
446 + .writeFileString(launchersFile, unrecorded.map((launcher) => `${launcher}\n`).join(""), {
447 + flag: "a",
448 + })
449 + .pipe(Effect.ignore);
450 + }
451 + return targets;
452 +});
453 +
454 +/**
455 + * Resolved executables rather than command lines: a server started through the `t3` symlink
456 + * shows up as `t3 serve`, possibly from another T3 home that runs this home's runtimes. Windows
457 + * has no symlink launcher, and its `t3.cmd` shim runs the absolute path, so command lines do there.
458 + */
459 +const runningExecutables = Effect.fn("cloud.pinned_runtime.running_executables")(function* () {
460 + const fs = yield* FileSystem.FileSystem;
461 + const runner = yield* ProcessRunner.ProcessRunner;
462 + const platform = yield* HostProcess.Platform;
463 + if (platform === "linux") {
464 + const pids = yield* fs.readDirectory("/proc").pipe(Effect.option);
465 + if (Option.isNone(pids)) return Option.none<string>();
466 + const executables: Array<string> = [];
467 + for (const pid of pids.value) {
468 + if (!/^\d+$/.test(pid)) continue;
469 + // Unreadable ones belong to other users or are this user's non-dumpable daemons (systemd
470 + // --user, ssh-agent). Neither runs a t3 runtime, and bailing on them would never prune.
471 + const executable = yield* fs.readLink(`/proc/${pid}/exe`).pipe(Effect.option);
472 + if (Option.isSome(executable)) executables.push(executable.value);
473 + }
474 + return executables.length === 0 ? Option.none<string>() : Option.some(executables.join("\n"));
475 + }
476 + const result = yield* runner
477 + .run({
478 + ...(platform === "win32"
479 + ? {
480 + command: "powershell.exe",
481 + args: [
482 + "-NoProfile",
483 + "-NonInteractive",
484 + "-Command",
485 + "Get-CimInstance Win32_Process | Select-Object -ExpandProperty CommandLine",
486 + ],
487 + }
488 + : { command: "lsof", args: ["-nP", "-w", "-d", "txt", "-F", "n"] }),
489 + timeout: Duration.seconds(10),
490 + maxOutputBytes: 16 * 1024 * 1024,
491 + })
492 + .pipe(Effect.option);
493 + return Option.isSome(result) && result.value.code === 0 && result.value.stdout.length > 0
494 + ? Option.some(result.value.stdout)
495 + : Option.none<string>();
496 +});
497 +
498 +const selectUnusedRuntimes = Effect.fn("cloud.pinned_runtime.select_unused")(function* (input: {
499 + readonly baseDir: string;
500 + readonly keep: ReadonlyArray<string>;
501 + readonly references: ReadonlyArray<string>;
502 +}) {
503 + const fs = yield* FileSystem.FileSystem;
504 + const path = yield* Path.Path;
505 + const versionsDir = pinnedRuntimeVersionsDir(path, input.baseDir);
506 + const entries = yield* fs.readDirectory(versionsDir).pipe(Effect.orElseSucceed(() => []));
507 + // An interrupted prune has already renamed its runtime out of the way.
508 + const pruned = new Set(
509 + entries
510 + .filter((entry) => entry.startsWith(PINNED_RUNTIME_PRUNED_PREFIX))
511 + .map((entry) => path.join(versionsDir, entry)),
512 + );
513 +
514 + const settledBefore =
515 + (yield* Clock.currentTimeMillis) - Duration.toMillis(PINNED_RUNTIME_PRUNE_GRACE);
516 + const completed: Array<string> = [];
517 + for (const version of entries) {
518 + if (!isExactServiceVersion(version)) continue;
519 + const versionDir = path.join(versionsDir, version);
520 + const sentinelPath = path.join(versionDir, ".install-complete");
521 + const isLink = yield* fs.readLink(versionDir).pipe(Effect.isSuccess);
522 + const sentinel = yield* fs.readFileString(sentinelPath).pipe(Effect.option);
523 + const completedAt = yield* fs.stat(sentinelPath).pipe(
524 + Effect.map((info) => Option.getOrUndefined(info.mtime)?.getTime()),
525 + Effect.orElseSucceed(() => undefined),
526 + );
527 + if (
528 + !isLink &&
529 + Option.isSome(sentinel) &&
530 + sentinel.value.trim() === version &&
531 + completedAt !== undefined &&
532 + completedAt < settledBefore
533 + ) {
534 + completed.push(version);
535 + }
536 + }
537 + if (completed.length === 0) return pruned;
538 +
539 + const keep = new Set(input.keep);
540 + const stateText = yield* fs
541 + .readFileString(path.join(input.baseDir, PINNED_RUNTIME_DIR, SERVICE_STATE_FILE))
542 + .pipe(
543 + Effect.asSome,
544 + Effect.catch((error) =>
545 + Effect.succeed(error.reason._tag === "NotFound" ? Option.none<string>() : undefined),
546 + ),
547 + );
548 + // State this version cannot read may reference runtimes in ways it does not know.
549 + if (stateText === undefined) return pruned;
550 + if (Option.isSome(stateText)) {
551 + const state = parseServiceState(stateText.value);
552 + if (state === undefined) return pruned;
553 + keep.add(state.activeVersion);
554 + if (state.update !== undefined) {
555 + keep.add(state.update.fromVersion);
556 + keep.add(state.update.targetVersion);
557 + }
558 + }
559 + const executables = yield* runningExecutables();
560 + // Without a process list, a runtime in use would look unused.
561 + if (Option.isNone(executables)) return pruned;
562 + const platform = yield* HostProcess.Platform;
563 + // Windows paths are case-insensitive, and shims and process lists need not agree on case.
564 + const foldCase = (value: string) => (platform === "win32" ? value.toLowerCase() : value);
565 + const references = [
566 + executables.value,
567 + ...(yield* shellLauncherTargets(input.baseDir)),
568 + ...input.references,
569 + ].map(foldCase);
570 + // Process executables are resolved, so they name the real path of a symlinked T3 home.
571 + const realVersionsDir = yield* fs
572 + .realPath(versionsDir)
573 + .pipe(Effect.orElseSucceed(() => versionsDir));
574 +
575 + for (const version of completed) {
576 + const versionDir = path.join(versionsDir, version);
577 + const prefixes = [versionDir, path.join(realVersionsDir, version)].map((dir) =>
578 + foldCase(dir + path.sep),
579 + );
580 + if (
581 + keep.has(version) ||
582 + references.some((reference) => prefixes.some((prefix) => reference.includes(prefix)))
583 + ) {
584 + continue;
585 + }
586 + const target = path.join(versionsDir, `${PINNED_RUNTIME_PRUNED_PREFIX}${version}`);
587 + // Renaming first means an interrupted removal never leaves a partial tree that still carries
588 + // its sentinel and would pass for a complete install.
589 + yield* fs.remove(target, { recursive: true, force: true }).pipe(
590 + Effect.andThen(fs.rename(versionDir, target)),
591 + Effect.andThen(Effect.sync(() => pruned.add(target))),
592 + Effect.catch((cause) =>
593 + Effect.logWarning("Could not remove an unused t3 runtime.", { version, cause }),
594 + ),
595 + );
596 + }
597 + return pruned;
598 +});
599 +
600 +/**
601 + * Every update publishes a new immutable runtime, so without pruning they pile up. Removes
602 + * completed runtimes nothing can start again and keeps: the caller's versions, everything the
603 + * service state references (active, rollback, pending), and runtimes that a running process
604 + * executes, a `t3` launcher points at, or `references` (such as the service unit) name, plus
605 + * anything published within the last hour. Staging directories, symlinks, and unexpected entries
606 + * are never touched. Returns the versions removed.
607 + */
608 +export const prunePinnedRuntimes = Effect.fn("cloud.pinned_runtime.prune")(function* (input: {
609 + readonly baseDir: string;
610 + readonly keep: ReadonlyArray<string>;
611 + readonly references?: ReadonlyArray<string>;
612 +}) {
613 + const fs = yield* FileSystem.FileSystem;
614 + const path = yield* Path.Path;
615 + // Only selection and the renames race an install; deleting renamed trees can take a while.
616 + const pruned = yield* pinnedRuntimeInstallLock.withPermit(
617 + selectUnusedRuntimes({ ...input, references: input.references ?? [] }),
618 + );
619 + const removed: Array<string> = [];
620 + for (const target of pruned) {
621 + yield* fs.remove(target, { recursive: true, force: true }).pipe(
622 + Effect.andThen(
623 + Effect.sync(() =>
624 + removed.push(path.basename(target).slice(PINNED_RUNTIME_PRUNED_PREFIX.length)),
625 + ),
626 + ),
627 + Effect.catch((cause) =>
628 + Effect.logWarning("Could not remove an unused t3 runtime.", { target, cause }),
629 + ),
630 + );
631 + }
632 + return removed;
633 +});
apps/server/src/cloud/selfUpdate.ts
+15
@@ -25,6 +25,7 @@ import { HttpClient } from "effect/http";
25
26 import { CLI_RELEASE_BASE_URL_ENV } from "@t3tools/shared/cliRelease";
27
28 +import packageJson from "../../package.json" with { type: "json" };
29 import * as ServerConfig from "../config.ts";
30 import * as DesktopAppUpdate from "../desktopUpdate/DesktopAppUpdate.ts";
31 import * as ProcessRunner from "../processRunner.ts";
@@ -33,6 +34,7 @@ import {
34 pinnedRuntimeCommand,
35 PinnedRuntimeInstallError,
36 PinnedRuntimePreflightBlockedError,
37 + prunePinnedRuntimes,
38 } from "./pinnedRuntime.ts";
39 import { decodeServicePreflightResult } from "./servicePreflight.ts";
40 import * as ServiceLauncherClient from "./serviceLauncherClient.ts";
@@ -186,6 +188,19 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* () {
188 );
189 const inFlight = yield* Ref.make(false);
190
191 + // A managed server starting means its launcher has settled on a version, so runtimes left by
192 + // earlier updates can go.
193 + if (launcher.managed) {
194 + yield* prunePinnedRuntimes({ baseDir: serverConfig.baseDir, keep: [packageJson.version] }).pipe(
195 + Effect.tap((removed) =>
196 + removed.length === 0
197 + ? Effect.void
198 + : Effect.logInfo("Removed unused t3 runtimes.", { versions: removed }),
199 + ),
200 + Effect.forkScoped,
201 + );
202 + }
203 +
204 const capability: ServerSelfUpdateCapability | null =
205 serverConfig.mode === "desktop" ? "desktop-managed" : launcher.managed ? "boot-service" : null;
206 const failWith = (reason: string, cause?: unknown) =>
scripts/install.ps1
+6
@@ -215,6 +215,12 @@ $shim = Join-Path $binDir "t3.cmd"
215 # UTF-8 without a BOM: cmd.exe reads the shim as-is, and ASCII would corrupt
216 # non-ASCII characters in the user's home path.
217 [System.IO.File]::WriteAllText($shim, "@echo off`r`n`"$(Join-Path $targetDir 't3.exe')`" %*", (New-Object System.Text.UTF8Encoding $false))
218 +# Pruning keeps what this shim points at; a background service cannot see $binDir otherwise.
219 +$launchers = Join-Path $t3Home "runtime\launchers"
220 +$shimPath = (Resolve-Path $shim).Path
221 +if (-not ((Test-Path $launchers) -and ((Get-Content $launchers) -contains $shimPath))) {
222 + [System.IO.File]::AppendAllText($launchers, "$shimPath`r`n", (New-Object System.Text.UTF8Encoding $false))
223 +}
224 if ($interactive) { [Console]::Error.Write("`r$esc[2K") }
225 [Console]::Error.WriteLine(" ${green}Installed T3 Code $version$reset`n")
226 if (($env:PATH -split ";") -notcontains $binDir) {
scripts/install.sh
+4
@@ -219,6 +219,10 @@ fi
219 step "Setting up the t3 command..."
220 mkdir -p "$bin_dir"
221 ln -sfn "${target_dir}/t3" "${bin_dir}/t3"
222 +# Pruning keeps what this launcher points at; a background service cannot see bin_dir otherwise.
223 +launcher="$(CDPATH='' cd -P -- "$bin_dir" && pwd)/t3"
224 +launchers="${t3_home}/runtime/launchers"
225 +grep -qxF "$launcher" "$launchers" 2>/dev/null || printf '%s\n' "$launcher" >> "$launchers"
226 if "$interactive"; then printf '\r\033[2K' >&2; fi
227 printf ' %sInstalled T3 Code %s%s\n\n' "$green" "$version" "$reset" >&2
228 case ":${PATH}:" in
scripts/install.test.ts
+3
@@ -102,6 +102,9 @@ describe.skipIf(HostProcess.Platform.defaultValue() !== "linux")("installer term
102 }).trim(),
103 ).toBe("t3 v1.2.3");
104 expect(await NodeFSP.readdir(versions)).toEqual([version]);
105 + expect(
106 + await NodeFSP.readFile(NodePath.join(root, "home/runtime/launchers"), "utf8"),
107 + ).toBe(`${NodePath.join(root, "bin/t3")}\n`);
108 }
109 } finally {
110 if (child.exitCode === null) child.kill();