Skip to content
TypeScript

fix(server): SSH hosts no longer keep every old 300 MB runtime after desktop updates (#23113)

shivamff525478e236
diff --git a/apps/server/src/cloud/selfUpdate.test.ts b/apps/server/src/cloud/selfUpdate.test.ts
index ad7c6d4a353..352c89d06f0 100644
--- a/apps/server/src/cloud/selfUpdate.test.ts
+++ b/apps/server/src/cloud/selfUpdate.test.ts
@@ -9,9 +9,11 @@ import * as FileSystem from "effect/FileSystem";
import * as Fiber from "effect/Fiber";
import * as Path from "effect/Path";
import * as Stream from "effect/Stream";
+import * as TestClock from "effect/testing/TestClock";
import { HttpClient, HttpClientResponse } from "effect/http";
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
+import packageJson from "../../package.json" with { type: "json" };
import * as ServerConfig from "../config.ts";
import * as DesktopAppUpdate from "../desktopUpdate/DesktopAppUpdate.ts";
import * as ProcessRunner from "../processRunner.ts";
@@ -386,3 +388,89 @@ it.layer(NodeServices.layer)("server self update", (it) => {
}),
);
});
+
+it.layer(NodeServices.layer)("pruning runtimes on start", (it) => {
+ const staleVersion = "0.0.1";
+ const otherProcesses = ProcessRunner.ProcessRunner.of({
+ run: () =>
+ Effect.succeed({
+ stdout: "p42\nftxt\nn/bin/sh\n",
+ stderr: "",
+ code: ChildProcessSpawner.ExitCode(0),
+ timedOut: false,
+ stdoutTruncated: false,
+ stderrTruncated: false,
+ stdoutInvalidUtf8: false,
+ stderrInvalidUtf8: false,
+ }),
+ });
+
+ const pruneOnStart = Effect.fn("test.prune_on_start")(function* (input: {
+ readonly executable: "own runtime" | "elsewhere";
+ readonly managed?: boolean;
+ readonly symlinkedHome?: boolean;
+ }) {
+ const fs = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-prune-on-start-" });
+ const realHome = path.join(tempDir, "home");
+ const baseDir = input.symlinkedHome ? path.join(tempDir, "linked-home") : realHome;
+ const versionsDir = path.join(baseDir, "runtime", "versions");
+ yield* TestClock.setTime(24 * 60 * 60 * 1000);
+ for (const version of [staleVersion, packageJson.version]) {
+ const directory = path.join(realHome, "runtime", "versions", version);
+ yield* fs.makeDirectory(directory, { recursive: true });
+ yield* fs.writeFileString(path.join(directory, "t3"), "");
+ const sentinel = path.join(directory, ".install-complete");
+ yield* fs.writeFileString(sentinel, `${version}\n`);
+ yield* fs.utimes(sentinel, 0, 0);
+ }
+ if (input.symlinkedHome) yield* fs.symlink(realHome, baseDir);
+ // Like `process.execPath`, the running runtime is reported by its real path.
+ const executablePath =
+ input.executable === "own runtime"
+ ? yield* fs.realPath(path.join(versionsDir, packageJson.version, "t3"))
+ : "/usr/local/bin/node";
+ const removed = yield* ServerSelfUpdate.pruneRuntimesOnStart({
+ baseDir,
+ managed: input.managed ?? false,
+ }).pipe(
+ Effect.provideService(HostProcess.ExecutablePath, executablePath),
+ Effect.provideService(HostProcess.Platform, "darwin"),
+ Effect.provideService(HostProcess.Environment, {}),
+ Effect.provideService(ProcessRunner.ProcessRunner, otherProcesses),
+ );
+ return { removed, remaining: (yield* fs.readDirectory(versionsDir)).toSorted() };
+ });
+
+ // The SSH launcher runs `t3 serve` straight from the pinned runtime, with no boot service.
+ it.effect("prunes for a server started from its home's pinned runtime", () =>
+ Effect.gen(function* () {
+ const { removed, remaining } = yield* pruneOnStart({ executable: "own runtime" });
+ expect(removed).toEqual([staleVersion]);
+ expect(remaining).toEqual([packageJson.version]);
+ }),
+ );
+
+ it.effect("recognizes its runtime through a symlinked T3 home", () =>
+ Effect.gen(function* () {
+ const { removed } = yield* pruneOnStart({ executable: "own runtime", symlinkedHome: true });
+ expect(removed).toEqual([staleVersion]);
+ }),
+ );
+
+ it.effect("prunes for a boot-service server wherever it runs from", () =>
+ Effect.gen(function* () {
+ const { removed } = yield* pruneOnStart({ executable: "elsewhere", managed: true });
+ expect(removed).toEqual([staleVersion]);
+ }),
+ );
+
+ it.effect("leaves runtimes alone for an unmanaged server started elsewhere", () =>
+ Effect.gen(function* () {
+ const { removed, remaining } = yield* pruneOnStart({ executable: "elsewhere" });
+ expect(removed).toEqual([]);
+ expect(remaining).toEqual([staleVersion, packageJson.version].toSorted());
+ }),
+ );
+});
diff --git a/apps/server/src/cloud/selfUpdate.ts b/apps/server/src/cloud/selfUpdate.ts
index 10c2a7991e7..ef717c34bb5 100644
--- a/apps/server/src/cloud/selfUpdate.ts
+++ b/apps/server/src/cloud/selfUpdate.ts
@@ -33,6 +33,7 @@ import {
ensurePinnedRuntimeInstalled,
pinnedRuntimeCommand,
PinnedRuntimeInstallError,
+ pinnedRuntimePaths,
PinnedRuntimePreflightBlockedError,
prunePinnedRuntimes,
} from "./pinnedRuntime.ts";
@@ -173,6 +174,34 @@ export const withRunningThreadContinuation = Effect.fn(
});
});
+/**
+ * A server starting from the runtime its launcher settled on means runtimes left by earlier
+ * updates can go. The boot service says so through its IPC context. The SSH launcher and the
+ * install script's `t3` start this home's pinned runtime directly, without one.
+ */
+export const pruneRuntimesOnStart = Effect.fn("cloud.server_self_update.prune_on_start")(
+ function* (input: { readonly baseDir: string; readonly managed: boolean }) {
+ const fs = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const platform = yield* HostProcess.Platform;
+ const executablePath = yield* HostProcess.ExecutablePath;
+ const { entryPath } = pinnedRuntimePaths(path, input.baseDir, packageJson.version, platform);
+ // The executable path is resolved, so it names the real path of a symlinked T3 home.
+ const realEntryPath = yield* fs.realPath(entryPath).pipe(Effect.orElseSucceed(() => entryPath));
+ if (!input.managed && executablePath !== entryPath && executablePath !== realEntryPath) {
+ return [];
+ }
+ const removed = yield* prunePinnedRuntimes({
+ baseDir: input.baseDir,
+ keep: [packageJson.version],
+ });
+ if (removed.length > 0) {
+ yield* Effect.logInfo("Removed unused t3 runtimes.", { versions: removed });
+ }
+ return removed;
+ },
+);
+
export const make = Effect.fn("cloud.server_self_update.make")(function* () {
const serverConfig = yield* ServerConfig.ServerConfig;
const desktopAppUpdate = yield* DesktopAppUpdate.DesktopAppUpdate;
@@ -188,18 +217,9 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* () {
);
const inFlight = yield* Ref.make(false);
- // A managed server starting means its launcher has settled on a version, so runtimes left by
- // earlier updates can go.
- if (launcher.managed) {
- yield* prunePinnedRuntimes({ baseDir: serverConfig.baseDir, keep: [packageJson.version] }).pipe(
- Effect.tap((removed) =>
- removed.length === 0
- ? Effect.void
- : Effect.logInfo("Removed unused t3 runtimes.", { versions: removed }),
- ),
- Effect.forkScoped,
- );
- }
+ yield* pruneRuntimesOnStart({ baseDir: serverConfig.baseDir, managed: launcher.managed }).pipe(
+ Effect.forkScoped,
+ );
const capability: ServerSelfUpdateCapability | null =
serverConfig.mode === "desktop" ? "desktop-managed" : launcher.managed ? "boot-service" : null;