From 172bd38515d0d894970ee3bd5c5e9aa7137189f7 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 22 Nov 2026 11:24:09 +0000 Subject: [PATCH 1/3] test(server): runtime prune leaves a committed update's old runtime and abandoned staging Failing repro for #40805. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/cloud/pinnedRuntime.test.ts | 40 +++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/apps/server/src/cloud/pinnedRuntime.test.ts b/apps/server/src/cloud/pinnedRuntime.test.ts index 7e4d69482eebf..aff51fc8dd77f 100644 --- a/apps/server/src/cloud/pinnedRuntime.test.ts +++ b/apps/server/src/cloud/pinnedRuntime.test.ts @@ -729,6 +729,46 @@ it.layer(NodeServices.layer)("prunePinnedRuntimes", (it) => { }), ); + it.effect("removes the runtime a committed update replaced", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const { baseDir } = yield* setup(["1.0.0", "1.1.0"]); + yield* fs.writeFileString( + path.join(baseDir, "runtime", "service-state.json"), + JSON.stringify({ + protocol: SERVICE_LAUNCHER_PROTOCOL, + activeVersion: "1.1.0", + update: { id: "u", fromVersion: "1.0.0", targetVersion: "1.1.0", status: "committed" }, + }), + ); + + const removed = yield* prunePinnedRuntimes({ baseDir, keep: ["1.1.0"] }).pipe( + Effect.provideService(ProcessRunner.ProcessRunner, lsofRunner("p1\nftxt\nn/bin/sh\n")), + Effect.provideService(HostProcess.Platform, "darwin"), + Effect.provideService(HostProcess.Environment, {}), + ); + assert.deepEqual(removed, ["1.0.0"]); + }), + ); + + it.effect("removes a staging directory an interrupted install abandoned long ago", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const { baseDir, versionsDir, install } = yield* setup(["1.0.0"]); + yield* install(".staging-abc", "never"); + yield* fs.utimes(path.join(versionsDir, ".staging-abc"), 0, 0); + + yield* prunePinnedRuntimes({ baseDir, keep: ["1.0.0"] }).pipe( + Effect.provideService(ProcessRunner.ProcessRunner, lsofRunner("p1\nftxt\nn/bin/sh\n")), + Effect.provideService(HostProcess.Platform, "darwin"), + Effect.provideService(HostProcess.Environment, {}), + ); + assert.deepEqual(yield* fs.readDirectory(versionsDir), ["1.0.0"]); + }), + ); + it.effect.each([ { name: "its directory is on PATH", onPath: true, recorded: false, linkedHome: false }, { From 1383e7ed43bcc4a14c5f4e0f8024bb1140b3541f Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 22 Nov 2026 11:27:59 +0000 Subject: [PATCH 2/3] fix(server): runtime prune removes the runtime a committed update replaced and abandoned staging Fixes pingdotgg/t3code#40805 Co-Authored-By: Claude Opus 5.5 --- apps/server/src/cloud/pinnedRuntime.ts | 29 +++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/apps/server/src/cloud/pinnedRuntime.ts b/apps/server/src/cloud/pinnedRuntime.ts index 08180f80a67ab..40fda58bfab67 100644 --- a/apps/server/src/cloud/pinnedRuntime.ts +++ b/apps/server/src/cloud/pinnedRuntime.ts @@ -416,7 +416,7 @@ const installPinnedRuntime = Effect.fn("cloud.pinned_runtime.ensure_installed")( const stagingDir = yield* fs .makeTempDirectory({ directory: versionsDir, - prefix: ".staging-", + prefix: PINNED_RUNTIME_STAGING_PREFIX, }) .pipe( Effect.mapError( @@ -485,6 +485,7 @@ const installPinnedRuntime = Effect.fn("cloud.pinned_runtime.ensure_installed")( export const ensurePinnedRuntimeInstalled = (input: PinnedRuntimeInstallInput) => pinnedRuntimeInstallLock.withPermit(installPinnedRuntime(input)); +const PINNED_RUNTIME_STAGING_PREFIX = ".staging-"; const PINNED_RUNTIME_PRUNED_PREFIX = ".pruned-"; /** * Another process may have just published a runtime it has not yet pointed anything at: an @@ -817,6 +818,16 @@ const selectUnusedRuntimes = Effect.fn("cloud.pinned_runtime.select_unused")(fun (yield* Clock.currentTimeMillis) - Duration.toMillis(PINNED_RUNTIME_PRUNE_GRACE); const completed: Array = []; for (const version of entries) { + if (version.startsWith(PINNED_RUNTIME_STAGING_PREFIX)) { + // A crashed or interrupted install leaves its staging directory behind. + const stagingDir = path.join(versionsDir, version); + const modifiedAt = yield* fs.stat(stagingDir).pipe( + Effect.map((info) => Option.getOrUndefined(info.mtime)?.getTime()), + Effect.orElseSucceed(() => undefined), + ); + if (modifiedAt !== undefined && modifiedAt < settledBefore) pruned.add(stagingDir); + continue; + } if (!isExactServiceVersion(version)) continue; const versionDir = path.join(versionsDir, version); const sentinelPath = path.join(versionDir, ".install-complete"); @@ -854,7 +865,8 @@ const selectUnusedRuntimes = Effect.fn("cloud.pinned_runtime.select_unused")(fun if (state === undefined) return pruned; keep.add(state.activeVersion); if (state.update !== undefined) { - keep.add(state.update.fromVersion); + // A committed update never returns to the runtime it replaced. + if (state.update.status !== "committed") keep.add(state.update.fromVersion); keep.add(state.update.targetVersion); } } @@ -901,8 +913,8 @@ const selectUnusedRuntimes = Effect.fn("cloud.pinned_runtime.select_unused")(fun /** * Keep runtimes named by callers, service state, running processes, launchers, or references, - * and anything published within an hour. Only prune completed runtimes; leave staging - * directories, symlinks, and unexpected entries untouched. + * and anything published within an hour. Only prune completed runtimes and staging directories + * untouched for an hour; leave symlinks and unexpected entries untouched. */ export const prunePinnedRuntimes = Effect.fn("cloud.pinned_runtime.prune")(function* (input: { readonly baseDir: string; @@ -917,11 +929,14 @@ export const prunePinnedRuntimes = Effect.fn("cloud.pinned_runtime.prune")(funct ); const removed: Array = []; for (const target of pruned) { + const name = path.basename(target); yield* fs.remove(target, { recursive: true, force: true }).pipe( Effect.andThen( - Effect.sync(() => - removed.push(path.basename(target).slice(PINNED_RUNTIME_PRUNED_PREFIX.length)), - ), + Effect.sync(() => { + if (name.startsWith(PINNED_RUNTIME_PRUNED_PREFIX)) { + removed.push(name.slice(PINNED_RUNTIME_PRUNED_PREFIX.length)); + } + }), ), Effect.catch((cause) => Effect.logWarning("Could not remove an unused t3 runtime.", { target, cause }), From 196ae4a45db1e8c486e5872c9ee7ec24996284e9 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 22 Nov 2026 11:35:58 +0000 Subject: [PATCH 3/3] fix(server): stage a runtime install only after its download finishes An install in another process could take hours to download into memory while its empty staging directory aged past the prune's one-hour grace. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/cloud/pinnedRuntime.test.ts | 2 ++ apps/server/src/cloud/pinnedRuntime.ts | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/apps/server/src/cloud/pinnedRuntime.test.ts b/apps/server/src/cloud/pinnedRuntime.test.ts index aff51fc8dd77f..28a7d720c525f 100644 --- a/apps/server/src/cloud/pinnedRuntime.test.ts +++ b/apps/server/src/cloud/pinnedRuntime.test.ts @@ -152,6 +152,8 @@ it.layer(NodeServices.layer)("ensurePinnedRuntimeInstalled", (it) => { }, }).pipe(Effect.forkScoped); yield* Deferred.await(firstChunk); + // Nothing is staged mid-download, so an age-based prune elsewhere cannot remove it. + assert.deepEqual(yield* fs.readDirectory(path.join(baseDir, "runtime", "versions")), []); yield* Fiber.interrupt(install); assert.deepEqual(progress.at(-1), { stage: "download", received: 4, total: undefined }); assert.isTrue(progress.every((event) => event.stage === "download")); diff --git a/apps/server/src/cloud/pinnedRuntime.ts b/apps/server/src/cloud/pinnedRuntime.ts index 40fda58bfab67..13c8f251c93b6 100644 --- a/apps/server/src/cloud/pinnedRuntime.ts +++ b/apps/server/src/cloud/pinnedRuntime.ts @@ -260,11 +260,9 @@ const fetchReleaseAsset = Effect.fn("cloud.pinned_runtime.fetch_release_asset")( ); }); -const installFromArchive = Effect.fn("cloud.pinned_runtime.install_archive")(function* ( +const downloadArchive = Effect.fn("cloud.pinned_runtime.download_archive")(function* ( input: PinnedRuntimeInstallInput, - stagingDir: string, ) { - const { fs, path } = input; const platformKey = cliArchivePlatformKey(input.platform, input.arch); if (platformKey === undefined) { return yield* new PinnedRuntimeInstallError({ @@ -292,7 +290,15 @@ const installFromArchive = Effect.fn("cloud.pinned_runtime.install_archive")(fun step: "verifying the t3 release archive checksum", }); } + return archive; +}); +const installFromArchive = Effect.fn("cloud.pinned_runtime.install_archive")(function* ( + input: PinnedRuntimeInstallInput, + stagingDir: string, + archive: Uint8Array, +) { + const { fs, path } = input; const archivePath = path.join(stagingDir, PINNED_RUNTIME_ARCHIVE_FILE); yield* fs .writeFile(archivePath, archive) @@ -413,6 +419,9 @@ const installPinnedRuntime = Effect.fn("cloud.pinned_runtime.ensure_installed")( }), ), ); + // Downloads are buffered in memory and can take hours; staging only once they finish keeps a + // staging directory's age a sign of abandonment for prunes in other processes. + const archive = yield* downloadArchive(input); const stagingDir = yield* fs .makeTempDirectory({ directory: versionsDir, @@ -434,7 +443,7 @@ const installPinnedRuntime = Effect.fn("cloud.pinned_runtime.ensure_installed")( }; return yield* Effect.gen(function* () { - yield* installFromArchive(input, stagingDir); + yield* installFromArchive(input, stagingDir, archive); input.onProgress?.({ stage: "validate" }); yield* input.validate(stagingPaths);