Skip to content

Commit 4d04e10

Browse files
fix(server): updates no longer keep every old runtime on disk
Every update published a new runtime under runtime/versions and nothing removed the old ones. Managed servers now prune on start and `t3 update` prunes when it finishes, keeping anything the service state, the t3 shell launcher, a running process, or a newer install still needs. Fixes #8345 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 774c6c1 commit 4d04e10

4 files changed

Lines changed: 267 additions & 0 deletions

File tree

‎apps/server/src/cli/update.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
pinnedRuntimeCommand,
2626
PinnedRuntimeInstallError,
2727
pinnedRuntimePaths,
28+
prunePinnedRuntimes,
2829
} from "../cloud/pinnedRuntime.ts";
2930
import { compareExactServiceVersions, isExactServiceVersion } from "../cloud/serviceProtocol.ts";
3031
import * as ProcessRunner from "../processRunner.ts";
@@ -521,6 +522,16 @@ const runUpdate = Effect.fn("cli.update.run")(function* (input: {
521522
);
522523
}
523524

525+
// A handed-off service install rewrites the unit later, while the old launcher may be stopped.
526+
if (servicePlan?.handedOff !== true) {
527+
yield* prunePinnedRuntimes({
528+
baseDir: input.baseDir,
529+
keep: [currentVersion, targetVersion],
530+
}).pipe(
531+
Effect.catch((cause) => Effect.logDebug("Could not prune unused t3 runtimes.", { cause })),
532+
);
533+
}
534+
524535
progress.success(`Installed T3 Code ${targetVersion}`);
525536
if (Option.isSome(repointed)) {
526537
yield* Console.log(" Run t3 to get started.\n");

‎apps/server/src/cloud/pinnedRuntime.test.ts‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@ import * as TestClock from "effect/testing/TestClock";
1010
import { HttpClient, HttpClientResponse } from "effect/http";
1111
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
1212

13+
import * as HostProcess from "@t3tools/shared/HostProcess";
14+
1315
import * as ProcessRunner from "../processRunner.ts";
1416
import {
1517
ensurePinnedRuntimeInstalled,
1618
pinnedRuntimeCommand,
1719
pinnedRuntimePaths,
1820
PinnedRuntimeInstallError,
1921
type PinnedRuntimeProgress,
22+
prunePinnedRuntimes,
2023
} from "./pinnedRuntime.ts";
24+
import { SERVICE_LAUNCHER_PROTOCOL } from "./serviceProtocol.ts";
2125

2226
const version = "1.2.3";
2327
const archiveName = `t3-${version}-linux-x64.tar.gz`;
@@ -553,3 +557,95 @@ it.layer(NodeServices.layer)("ensurePinnedRuntimeInstalled", (it) => {
553557
}),
554558
);
555559
});
560+
561+
it.layer(NodeServices.layer)("prunePinnedRuntimes", (it) => {
562+
const psRunner = (stdout: string) =>
563+
ProcessRunner.ProcessRunner.of({
564+
run: () =>
565+
Effect.succeed({
566+
stdout,
567+
stderr: "",
568+
code: ChildProcessSpawner.ExitCode(0),
569+
timedOut: false,
570+
stdoutTruncated: false,
571+
stderrTruncated: false,
572+
stdoutInvalidUtf8: false,
573+
stderrInvalidUtf8: false,
574+
}),
575+
});
576+
577+
it.effect("removes only completed runtimes nothing references", () =>
578+
Effect.gen(function* () {
579+
const fs = yield* FileSystem.FileSystem;
580+
const path = yield* Path.Path;
581+
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-prune-" });
582+
const binDir = path.join(baseDir, "bin");
583+
const versionsDir = path.join(baseDir, "runtime", "versions");
584+
const install = (version: string, complete = true) =>
585+
Effect.gen(function* () {
586+
const directory = path.join(versionsDir, version);
587+
yield* fs.makeDirectory(directory, { recursive: true });
588+
yield* fs.writeFileString(path.join(directory, "t3"), "");
589+
if (complete) {
590+
yield* fs.writeFileString(path.join(directory, ".install-complete"), `${version}\n`);
591+
}
592+
});
593+
for (const version of ["1.0.0", "1.1.0", "1.2.0", "1.3.0", "1.4.0", "1.5.0", "1.6.0"]) {
594+
yield* install(version);
595+
}
596+
yield* install("1.0.1", false);
597+
yield* install(".staging-abc", false);
598+
yield* install(".pruned-0.9.0");
599+
yield* fs.makeDirectory(binDir);
600+
yield* fs.symlink(path.join(versionsDir, "1.2.0", "t3"), path.join(binDir, "t3"));
601+
yield* fs.writeFileString(
602+
path.join(baseDir, "runtime", "service-state.json"),
603+
JSON.stringify({
604+
protocol: SERVICE_LAUNCHER_PROTOCOL,
605+
activeVersion: "1.5.0",
606+
update: { id: "u", fromVersion: "1.4.0", targetVersion: "1.5.0", status: "committed" },
607+
}),
608+
);
609+
610+
const removed = yield* prunePinnedRuntimes({ baseDir, keep: ["1.5.0"] }).pipe(
611+
Effect.provideService(
612+
ProcessRunner.ProcessRunner,
613+
psRunner(`${path.join(versionsDir, "1.3.0", "t3")} __service-launcher\n`),
614+
),
615+
Effect.provideService(HostProcess.Platform, "linux"),
616+
Effect.provideService(HostProcess.Environment, { T3CODE_INSTALL_BIN_DIR: binDir }),
617+
);
618+
619+
// 1.2.0 is the shell launcher, 1.3.0 a running launcher, 1.4.0 the rollback, 1.5.0 active,
620+
// 1.6.0 newer than anything kept; 1.0.1 never completed.
621+
assert.deepEqual(removed, ["1.0.0", "1.1.0"]);
622+
assert.deepEqual((yield* fs.readDirectory(versionsDir)).toSorted(), [
623+
".staging-abc",
624+
"1.0.1",
625+
"1.2.0",
626+
"1.3.0",
627+
"1.4.0",
628+
"1.5.0",
629+
"1.6.0",
630+
]);
631+
}),
632+
);
633+
634+
it.effect("keeps everything when the process list is unavailable", () =>
635+
Effect.gen(function* () {
636+
const fs = yield* FileSystem.FileSystem;
637+
const path = yield* Path.Path;
638+
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-prune-ps-" });
639+
const versionDir = path.join(baseDir, "runtime", "versions", "1.0.0");
640+
yield* fs.makeDirectory(versionDir, { recursive: true });
641+
yield* fs.writeFileString(path.join(versionDir, ".install-complete"), "1.0.0\n");
642+
643+
const removed = yield* prunePinnedRuntimes({ baseDir, keep: ["2.0.0"] }).pipe(
644+
Effect.provideService(ProcessRunner.ProcessRunner, psRunner("")),
645+
Effect.provideService(HostProcess.Environment, { T3CODE_INSTALL_BIN_DIR: baseDir }),
646+
);
647+
assert.deepEqual(removed, []);
648+
assert.isTrue(yield* fs.exists(versionDir));
649+
}),
650+
);
651+
});

‎apps/server/src/cloud/pinnedRuntime.ts‎

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,15 @@ import {
1919
parseChecksums,
2020
} from "@t3tools/shared/cliRelease";
2121

22+
import * as HostProcess from "@t3tools/shared/HostProcess";
23+
2224
import * as ProcessRunner from "../processRunner.ts";
25+
import {
26+
compareExactServiceVersions,
27+
isExactServiceVersion,
28+
parseServiceState,
29+
SERVICE_STATE_FILE,
30+
} from "./serviceProtocol.ts";
2331

2432
const PINNED_RUNTIME_DIR = "runtime";
2533
const PINNED_RUNTIME_INSTALL_TIMEOUT = Duration.minutes(10);
@@ -364,3 +372,139 @@ const installPinnedRuntime = Effect.fn("cloud.pinned_runtime.ensure_installed")(
364372

365373
export const ensurePinnedRuntimeInstalled = (input: PinnedRuntimeInstallInput) =>
366374
pinnedRuntimeInstallLock.withPermit(installPinnedRuntime(input));
375+
376+
const PINNED_RUNTIME_PRUNED_PREFIX = ".pruned-";
377+
378+
/** Versions the `t3` shims written by the install scripts and `t3 update` point at. */
379+
const shellLauncherVersions = Effect.fn("cloud.pinned_runtime.shell_launcher_versions")(function* (
380+
versionsDir: string,
381+
) {
382+
const fs = yield* FileSystem.FileSystem;
383+
const path = yield* Path.Path;
384+
const platform = yield* HostProcess.Platform;
385+
const environment = yield* HostProcess.Environment;
386+
const home = yield* HostProcess.HomeDirectory;
387+
const directories = [
388+
environment["T3CODE_INSTALL_BIN_DIR"] || path.join(home, ".local", "bin"),
389+
...(environment["PATH"] ?? environment["Path"] ?? "").split(platform === "win32" ? ";" : ":"),
390+
].filter((directory) => directory.trim().length > 0);
391+
const versions = new Set<string>();
392+
for (const directory of directories) {
393+
const target =
394+
platform === "win32"
395+
? yield* fs.readFileString(path.join(directory, "t3.cmd")).pipe(
396+
Effect.map((contents) => /^"([^"]+)"/m.exec(contents)?.[1]),
397+
Effect.orElseSucceed(() => undefined),
398+
)
399+
: yield* fs.readLink(path.join(directory, "t3")).pipe(
400+
Effect.map((link) => path.resolve(directory, link)),
401+
Effect.orElseSucceed(() => undefined),
402+
);
403+
if (target === undefined) continue;
404+
const relative = path.relative(versionsDir, path.resolve(target));
405+
if (relative.startsWith("..") || path.isAbsolute(relative)) continue;
406+
const version = relative.split(path.sep)[0];
407+
if (version) versions.add(version);
408+
}
409+
return versions;
410+
});
411+
412+
const runningCommandLines = Effect.fn("cloud.pinned_runtime.running_command_lines")(function* () {
413+
const runner = yield* ProcessRunner.ProcessRunner;
414+
const platform = yield* HostProcess.Platform;
415+
const result = yield* runner.run({
416+
...(platform === "win32"
417+
? {
418+
command: "powershell.exe",
419+
args: [
420+
"-NoProfile",
421+
"-NonInteractive",
422+
"-Command",
423+
"Get-CimInstance Win32_Process | Select-Object -ExpandProperty CommandLine",
424+
],
425+
}
426+
: { command: "ps", args: ["-ax", "-o", "command="] }),
427+
timeout: Duration.seconds(10),
428+
maxOutputBytes: 16 * 1024 * 1024,
429+
});
430+
return result.code === 0 && result.stdout.length > 0 ? Option.some(result.stdout) : Option.none();
431+
});
432+
433+
/**
434+
* Every update publishes a new immutable runtime, so without pruning they pile up. Removes
435+
* completed runtimes nothing can start again and keeps: the caller's versions, everything the
436+
* service state references (active, rollback, pending), the `t3` shell launcher's target, any
437+
* runtime a running process uses (an older service launcher keeps serving newer children), and
438+
* anything newer than the caller's versions, which another process may be installing. Staging
439+
* directories, symlinks, and unexpected entries are never touched.
440+
*/
441+
export const prunePinnedRuntimes = Effect.fn("cloud.pinned_runtime.prune")(function* (input: {
442+
readonly baseDir: string;
443+
readonly keep: ReadonlyArray<string>;
444+
}) {
445+
const fs = yield* FileSystem.FileSystem;
446+
const path = yield* Path.Path;
447+
const versionsDir = pinnedRuntimeVersionsDir(path, input.baseDir);
448+
const entries = yield* fs.readDirectory(versionsDir).pipe(Effect.orElseSucceed(() => []));
449+
450+
// An interrupted prune has already renamed its runtime out of the way.
451+
for (const entry of entries) {
452+
if (entry.startsWith(PINNED_RUNTIME_PRUNED_PREFIX)) {
453+
yield* fs.remove(path.join(versionsDir, entry), { recursive: true, force: true });
454+
}
455+
}
456+
457+
const completed: Array<string> = [];
458+
for (const version of entries) {
459+
if (!isExactServiceVersion(version)) continue;
460+
const versionDir = path.join(versionsDir, version);
461+
const isLink = yield* fs.readLink(versionDir).pipe(Effect.isSuccess);
462+
const sentinel = yield* fs
463+
.readFileString(path.join(versionDir, ".install-complete"))
464+
.pipe(Effect.option);
465+
if (!isLink && Option.isSome(sentinel) && sentinel.value.trim() === version) {
466+
completed.push(version);
467+
}
468+
}
469+
if (completed.length === 0) return [];
470+
471+
const keep = new Set(input.keep);
472+
const stateText = yield* fs
473+
.readFileString(path.join(input.baseDir, "runtime", SERVICE_STATE_FILE))
474+
.pipe(Effect.option);
475+
if (Option.isSome(stateText)) {
476+
const state = parseServiceState(stateText.value);
477+
// State this version cannot read may reference runtimes in ways it does not know.
478+
if (state === undefined) return [];
479+
keep.add(state.activeVersion);
480+
if (state.update !== undefined) {
481+
keep.add(state.update.fromVersion);
482+
keep.add(state.update.targetVersion);
483+
}
484+
}
485+
for (const version of yield* shellLauncherVersions(versionsDir)) keep.add(version);
486+
const commandLines = yield* runningCommandLines();
487+
// Without a process list, a runtime in use would look unused.
488+
if (Option.isNone(commandLines)) return [];
489+
490+
const removable = completed.filter(
491+
(version) =>
492+
!keep.has(version) &&
493+
input.keep.every((kept) => compareExactServiceVersions(version, kept) < 0) &&
494+
!commandLines.value.includes(path.join(versionsDir, version) + path.sep),
495+
);
496+
const removed: Array<string> = [];
497+
for (const version of removable) {
498+
const pruned = path.join(versionsDir, `${PINNED_RUNTIME_PRUNED_PREFIX}${version}`);
499+
// Renaming first means an interrupted removal never leaves a partial tree that still carries
500+
// its sentinel and would pass for a complete install.
501+
yield* fs.rename(path.join(versionsDir, version), pruned).pipe(
502+
Effect.andThen(fs.remove(pruned, { recursive: true, force: true })),
503+
Effect.andThen(Effect.sync(() => removed.push(version))),
504+
Effect.catch((cause) =>
505+
Effect.logWarning("Could not remove an unused t3 runtime.", { version, cause }),
506+
),
507+
);
508+
}
509+
return removed;
510+
}, pinnedRuntimeInstallLock.withPermit);

‎apps/server/src/cloud/selfUpdate.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { HttpClient } from "effect/http";
2525

2626
import { CLI_RELEASE_BASE_URL_ENV } from "@t3tools/shared/cliRelease";
2727

28+
import packageJson from "../../package.json" with { type: "json" };
2829
import * as ServerConfig from "../config.ts";
2930
import * as DesktopAppUpdate from "../desktopUpdate/DesktopAppUpdate.ts";
3031
import * as ProcessRunner from "../processRunner.ts";
@@ -33,6 +34,7 @@ import {
3334
pinnedRuntimeCommand,
3435
PinnedRuntimeInstallError,
3536
PinnedRuntimePreflightBlockedError,
37+
prunePinnedRuntimes,
3638
} from "./pinnedRuntime.ts";
3739
import { decodeServicePreflightResult } from "./servicePreflight.ts";
3840
import * as ServiceLauncherClient from "./serviceLauncherClient.ts";
@@ -186,6 +188,20 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* () {
186188
);
187189
const inFlight = yield* Ref.make(false);
188190

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.catch((cause) => Effect.logWarning("Could not prune unused t3 runtimes.", { cause })),
201+
Effect.forkScoped,
202+
);
203+
}
204+
189205
const capability: ServerSelfUpdateCapability | null =
190206
serverConfig.mode === "desktop" ? "desktop-managed" : launcher.managed ? "boot-service" : null;
191207
const failWith = (reason: string, cause?: unknown) =>

0 commit comments

Comments
 (0)