Skip to content

Commit 7fe3a46

Browse files
Merge branch 'main' into fix/writing-style-sparse-encode
2 parents 16b74ad + 2b28b46 commit 7fe3a46

13 files changed

Lines changed: 575 additions & 73 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: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +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,
29+
prunePinnedRuntimes,
30+
windowsShimTarget,
2831
} from "../cloud/pinnedRuntime.ts";
2932
import { compareExactServiceVersions, isExactServiceVersion } from "../cloud/serviceProtocol.ts";
3033
import * as ProcessRunner from "../processRunner.ts";
@@ -87,15 +90,6 @@ export const resolveNewestVersion = Effect.fn("cli.update.resolve_newest")(funct
8790
return yield* new CliUpdateError({ reason: `No published ${channel} release was found.` });
8891
});
8992

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-
9993
/**
10094
* Only a launcher that already points into this home's `runtime/versions` tree is touched; a
10195
* 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
116110
const shimPath = yield* findWindowsShim(input.launchedAs);
117111
if (shimPath === undefined) return Option.none<string>();
118112
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;
120114
if (quoted === undefined || !ownsTarget(quoted)) return Option.none<string>();
121115
yield* fs
122116
.writeFileString(shimPath, `@echo off\r\n"${input.targetEntryPath}" %*`)
@@ -188,7 +182,7 @@ export const findWindowsShim = Effect.fn("cli.update.find_windows_shim")(functio
188182
const shimPath = path.join(directory, "t3.cmd");
189183
const contents = yield* fs.readFileString(shimPath).pipe(Effect.option);
190184
if (Option.isNone(contents)) continue;
191-
const target = /^"([^"]+)"/m.exec(contents.value)?.[1];
185+
const target = windowsShimTarget(contents.value);
192186
if (
193187
target !== undefined &&
194188
path.resolve(target).toLowerCase() === path.resolve(executablePath).toLowerCase()
@@ -521,6 +515,17 @@ const runUpdate = Effect.fn("cli.update.run")(function* (input: {
521515
);
522516
}
523517

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+
524529
progress.success(`Installed T3 Code ${targetVersion}`);
525530
if (Option.isSome(repointed)) {
526531
yield* Console.log(" Run t3 to get started.\n");

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,23 @@ it("reads the served T3 home back out of a rendered unit or plist", () => {
6464
expect(BootService.bootServiceBaseDirOf("[Service]\nExecStart=/x\n")).toBeUndefined();
6565
});
6666

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+
6784
it("survives the kernel OOM-killing a greedy agent child", () => {
6885
const unit = BootService.renderBootServiceUnit(linuxPlan);
6986

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,30 @@ function quoteSystemdValue(value: string): string {
5353
: escaped;
5454
}
5555

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+
5665
export function bootServiceBaseDirOf(contents: string): string | undefined {
5766
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());
6668
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);
7180
}
7281

7382
export interface BootServicePlan {

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

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,21 @@ import * as Fiber from "effect/Fiber";
88
import * as Path from "effect/Path";
99
import * as TestClock from "effect/testing/TestClock";
1010
import { HttpClient, HttpClientResponse } from "effect/http";
11+
import * as ChildProcess from "effect/process/ChildProcess";
1112
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
1213

14+
import * as HostProcess from "@t3tools/shared/HostProcess";
15+
1316
import * as ProcessRunner from "../processRunner.ts";
1417
import {
1518
ensurePinnedRuntimeInstalled,
1619
pinnedRuntimeCommand,
1720
pinnedRuntimePaths,
1821
PinnedRuntimeInstallError,
1922
type PinnedRuntimeProgress,
23+
prunePinnedRuntimes,
2024
} from "./pinnedRuntime.ts";
25+
import { SERVICE_LAUNCHER_PROTOCOL } from "./serviceProtocol.ts";
2126

2227
const version = "1.2.3";
2328
const archiveName = `t3-${version}-linux-x64.tar.gz`;
@@ -553,3 +558,205 @@ it.layer(NodeServices.layer)("ensurePinnedRuntimeInstalled", (it) => {
553558
}),
554559
);
555560
});
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+
});

0 commit comments

Comments
 (0)