Skip to content

Commit 8320570

Browse files
fix(server): runtime pruning keeps launchers, units, and hand-started servers
Review found runtimes that pruning could delete while still needed: - a server started through the `t3` symlink shows as `t3 serve` in ps; `t3 update` now keeps the foreground server's version - a stopped service's unit still names its launcher runtime; `t3 update` passes the unit text as a reference - a service cannot see a custom T3CODE_INSTALL_BIN_DIR; the install scripts now record launcher paths in runtime/launchers Also: a missing or failing ps skips pruning instead of erroring, a stuck .pruned- leftover no longer blocks later prunes, deletes run outside the install lock, and the shim helpers are shared with update/uninstall. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4d04e10 commit 8320570

8 files changed

Lines changed: 225 additions & 114 deletions

File tree

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ import * as Schema from "effect/Schema";
1414
import { Command, Flag, GlobalFlag, Prompt } from "effect/cli";
1515

1616
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";
1822
import { projectLocationFlags, resolveCliAuthConfig } from "./config.ts";
1923
import * as CliService from "./service.ts";
20-
import { findWindowsShim, launcherOwnsVersionsDir, resolveLauncherPath } from "./update.ts";
24+
import { findWindowsShim, resolveLauncherPath } from "./update.ts";
2125

2226
export class CliUninstallError extends Schema.TaggedError<CliUninstallError>()(
2327
"CliUninstallError",
@@ -51,7 +55,7 @@ export const findOwnedLauncher = Effect.fn("cli.uninstall.find_launcher")(functi
5155
const shimPath = yield* findWindowsShim(input.launchedAs);
5256
if (shimPath === undefined) return undefined;
5357
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;
5559
return target !== undefined && launcherOwnsVersionsDir(path, input.versionsDir, target)
5660
? shimPath
5761
: undefined;

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ import packageJson from "../../package.json" with { type: "json" };
2222
import * as BootService from "../cloud/bootService.ts";
2323
import {
2424
ensurePinnedRuntimeInstalled,
25+
launcherOwnsVersionsDir,
2526
pinnedRuntimeCommand,
2627
PinnedRuntimeInstallError,
2728
pinnedRuntimePaths,
2829
prunePinnedRuntimes,
30+
windowsShimTarget,
2931
} from "../cloud/pinnedRuntime.ts";
3032
import { compareExactServiceVersions, isExactServiceVersion } from "../cloud/serviceProtocol.ts";
3133
import * as ProcessRunner from "../processRunner.ts";
@@ -88,15 +90,6 @@ export const resolveNewestVersion = Effect.fn("cli.update.resolve_newest")(funct
8890
return yield* new CliUpdateError({ reason: `No published ${channel} release was found.` });
8991
});
9092

91-
export function launcherOwnsVersionsDir(
92-
path: Path.Path,
93-
versionsDir: string,
94-
candidate: string,
95-
): boolean {
96-
const relative = path.relative(versionsDir, path.resolve(candidate));
97-
return relative.length > 0 && !relative.startsWith("..") && !path.isAbsolute(relative);
98-
}
99-
10093
/**
10194
* Only a launcher that already points into this home's `runtime/versions` tree is touched; a
10295
* plain copy of the executable, or a launcher for some other install, is left alone.
@@ -117,7 +110,7 @@ export const repointLauncher = Effect.fn("cli.update.repoint_launcher")(function
117110
const shimPath = yield* findWindowsShim(input.launchedAs);
118111
if (shimPath === undefined) return Option.none<string>();
119112
const current = yield* fs.readFileString(shimPath).pipe(Effect.option);
120-
const quoted = Option.isSome(current) ? /^"([^"]+)"/m.exec(current.value)?.[1] : undefined;
113+
const quoted = Option.isSome(current) ? windowsShimTarget(current.value) : undefined;
121114
if (quoted === undefined || !ownsTarget(quoted)) return Option.none<string>();
122115
yield* fs
123116
.writeFileString(shimPath, `@echo off\r\n"${input.targetEntryPath}" %*`)
@@ -189,7 +182,7 @@ export const findWindowsShim = Effect.fn("cli.update.find_windows_shim")(functio
189182
const shimPath = path.join(directory, "t3.cmd");
190183
const contents = yield* fs.readFileString(shimPath).pipe(Effect.option);
191184
if (Option.isNone(contents)) continue;
192-
const target = /^"([^"]+)"/m.exec(contents.value)?.[1];
185+
const target = windowsShimTarget(contents.value);
193186
if (
194187
target !== undefined &&
195188
path.resolve(target).toLowerCase() === path.resolve(executablePath).toLowerCase()
@@ -524,12 +517,17 @@ const runUpdate = Effect.fn("cli.update.run")(function* (input: {
524517

525518
// A handed-off service install rewrites the unit later, while the old launcher may be stopped.
526519
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);
527522
yield* prunePinnedRuntimes({
528523
baseDir: input.baseDir,
529-
keep: [currentVersion, targetVersion],
530-
}).pipe(
531-
Effect.catch((cause) => Effect.logDebug("Could not prune unused t3 runtimes.", { cause })),
532-
);
524+
keep: [
525+
currentVersion,
526+
targetVersion,
527+
...(foreground?.serverVersion === undefined ? [] : [foreground.serverVersion]),
528+
],
529+
references: Option.toArray(unit),
530+
});
533531
}
534532

535533
progress.success(`Installed T3 Code ${targetVersion}`);

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

Lines changed: 67 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -574,78 +574,114 @@ it.layer(NodeServices.layer)("prunePinnedRuntimes", (it) => {
574574
}),
575575
});
576576

577+
const setup = Effect.fn(function* (versions: ReadonlyArray<string>) {
578+
const fs = yield* FileSystem.FileSystem;
579+
const path = yield* Path.Path;
580+
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-prune-" });
581+
const versionsDir = path.join(baseDir, "runtime", "versions");
582+
const install = Effect.fn(function* (version: string, complete = true) {
583+
const directory = path.join(versionsDir, version);
584+
yield* fs.makeDirectory(directory, { recursive: true });
585+
yield* fs.writeFileString(path.join(directory, "t3"), "");
586+
if (complete) {
587+
yield* fs.writeFileString(path.join(directory, ".install-complete"), `${version}\n`);
588+
}
589+
});
590+
for (const version of versions) yield* install(version);
591+
return { baseDir, versionsDir, install };
592+
});
593+
577594
it.effect("removes only completed runtimes nothing references", () =>
578595
Effect.gen(function* () {
579596
const fs = yield* FileSystem.FileSystem;
580597
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-
}
598+
const { baseDir, versionsDir, install } = yield* setup([
599+
"1.0.0",
600+
"1.1.0",
601+
"1.2.0",
602+
"1.2.5",
603+
"1.3.0",
604+
"1.3.5",
605+
"1.4.0",
606+
"1.5.0",
607+
"2.0.0",
608+
"2.1.0",
609+
]);
596610
yield* install("1.0.1", false);
597611
yield* install(".staging-abc", false);
598-
yield* install(".pruned-0.9.0");
612+
yield* install(".pruned-0.8.0");
613+
yield* install("other", false);
614+
yield* fs.symlink(path.join(versionsDir, "other"), path.join(versionsDir, "0.9.0"));
615+
const binDir = path.join(baseDir, "bin");
616+
const customBinDir = path.join(baseDir, "custom-bin");
599617
yield* fs.makeDirectory(binDir);
618+
yield* fs.makeDirectory(customBinDir);
600619
yield* fs.symlink(path.join(versionsDir, "1.2.0", "t3"), path.join(binDir, "t3"));
620+
yield* fs.symlink(path.join(versionsDir, "1.2.5", "t3"), path.join(customBinDir, "t3"));
621+
yield* fs.writeFileString(
622+
path.join(baseDir, "runtime", "launchers"),
623+
`${path.join(customBinDir, "t3")}\n`,
624+
);
601625
yield* fs.writeFileString(
602626
path.join(baseDir, "runtime", "service-state.json"),
603627
JSON.stringify({
604628
protocol: SERVICE_LAUNCHER_PROTOCOL,
605-
activeVersion: "1.5.0",
606-
update: { id: "u", fromVersion: "1.4.0", targetVersion: "1.5.0", status: "committed" },
629+
activeVersion: "1.4.0",
630+
update: { id: "u", fromVersion: "1.4.0", targetVersion: "1.5.0", status: "rolled-back" },
607631
}),
608632
);
609633

610-
const removed = yield* prunePinnedRuntimes({ baseDir, keep: ["1.5.0"] }).pipe(
634+
const removed = yield* prunePinnedRuntimes({
635+
baseDir,
636+
keep: ["2.0.0"],
637+
references: [`ExecStart=${path.join(versionsDir, "1.3.5", "t3")} __service-launcher`],
638+
}).pipe(
611639
Effect.provideService(
612640
ProcessRunner.ProcessRunner,
613-
psRunner(`${path.join(versionsDir, "1.3.0", "t3")} __service-launcher\n`),
641+
psRunner(`${path.join(versionsDir, "1.3.0", "t3")} serve\n`),
614642
),
615643
Effect.provideService(HostProcess.Platform, "linux"),
616644
Effect.provideService(HostProcess.Environment, { T3CODE_INSTALL_BIN_DIR: binDir }),
617645
);
618646

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"]);
647+
// 1.2.0 is the default launcher, 1.2.5 a recorded one, 1.3.0 running, 1.3.5 in the unit,
648+
// 1.4.0 active, 1.5.0 the rolled-back target, 2.0.0 the caller's and 2.1.0 newer.
649+
assert.deepEqual(removed.toSorted(), ["0.8.0", "1.0.0", "1.1.0"]);
622650
assert.deepEqual((yield* fs.readDirectory(versionsDir)).toSorted(), [
623651
".staging-abc",
652+
"0.9.0",
624653
"1.0.1",
625654
"1.2.0",
655+
"1.2.5",
626656
"1.3.0",
657+
"1.3.5",
627658
"1.4.0",
628659
"1.5.0",
629-
"1.6.0",
660+
"2.0.0",
661+
"2.1.0",
662+
"other",
630663
]);
631664
}),
632665
);
633666

634-
it.effect("keeps everything when the process list is unavailable", () =>
667+
it.effect.each([
668+
{ name: "the process list is unavailable", state: undefined, ps: "" },
669+
{ name: "the service state is unreadable", state: "{", ps: "init\n" },
670+
])("keeps everything when $name", ({ state, ps }) =>
635671
Effect.gen(function* () {
636672
const fs = yield* FileSystem.FileSystem;
637673
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");
674+
const { baseDir, versionsDir } = yield* setup(["1.0.0"]);
675+
if (state !== undefined) {
676+
yield* fs.writeFileString(path.join(baseDir, "runtime", "service-state.json"), state);
677+
}
642678

643679
const removed = yield* prunePinnedRuntimes({ baseDir, keep: ["2.0.0"] }).pipe(
644-
Effect.provideService(ProcessRunner.ProcessRunner, psRunner("")),
680+
Effect.provideService(ProcessRunner.ProcessRunner, psRunner(ps)),
645681
Effect.provideService(HostProcess.Environment, { T3CODE_INSTALL_BIN_DIR: baseDir }),
646682
);
647683
assert.deepEqual(removed, []);
648-
assert.isTrue(yield* fs.exists(versionDir));
684+
assert.deepEqual(yield* fs.readDirectory(versionsDir), ["1.0.0"]);
649685
}),
650686
);
651687
});

0 commit comments

Comments
 (0)