Skip to content

Commit 077a1f1

Browse files
fix(server): a cached runtime installs under a mirror only while the mirror lists it
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4b208a3 commit 077a1f1

4 files changed

Lines changed: 75 additions & 19 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@ export const runUpdate = Effect.fn("cli.update.run")(function* (input: {
499499
platform,
500500
arch,
501501
releaseBaseUrl: mirror?.feedUrl ?? (environment[CLI_RELEASE_BASE_URL_ENV]?.trim() || undefined),
502+
requirePublishedRelease: mirror !== undefined,
502503
validate: (paths) =>
503504
runner
504505
.run({

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,39 @@ it.layer(NodeServices.layer)("ensurePinnedRuntimeInstalled", (it) => {
267267
}),
268268
);
269269

270+
it.effect("reuses a cached runtime under a mirror only while the mirror lists it", () =>
271+
Effect.gen(function* () {
272+
const fs = yield* FileSystem.FileSystem;
273+
const path = yield* Path.Path;
274+
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-runtime-mirror-" });
275+
const cached = pinnedRuntimePaths(path, baseDir, version, "linux");
276+
yield* fs.makeDirectory(path.dirname(cached.entryPath), { recursive: true });
277+
yield* fs.writeFileString(cached.entryPath, "#!/bin/sh\n");
278+
yield* fs.writeFileString(cached.sentinelPath, `${version}\n`);
279+
const install = (checksums: string, requests: string[]) =>
280+
ensurePinnedRuntimeInstalled({
281+
baseDir,
282+
version,
283+
fs,
284+
path,
285+
platform: "linux",
286+
arch: "x64",
287+
httpClient: releaseHttpClient(checksums, requests),
288+
releaseBaseUrl: "https://updates.example.com/t3code",
289+
requirePublishedRelease: true,
290+
runner: extractingRunner(fs, path),
291+
validate: () => Effect.void,
292+
});
293+
294+
const withdrawn = yield* install("", []).pipe(Effect.flip);
295+
assert.include(withdrawn.message, `finding ${archiveName} in the t3 release checksums`);
296+
const requests: string[] = [];
297+
assert.equal((yield* install(yield* validChecksums, requests)).entryPath, cached.entryPath);
298+
// Listed, so the cached runtime is used without downloading it again.
299+
assert.deepEqual(requests, [`https://updates.example.com/t3code/v${version}/SHA256SUMS`]);
300+
}),
301+
);
302+
270303
it.effect("preserves a completed runtime when validation fails", () =>
271304
Effect.gen(function* () {
272305
const fs = yield* FileSystem.FileSystem;

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

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
cliArchivePlatformKey,
1818
cliArchiveTarCommand,
1919
cliReleaseDownloadBaseUrl,
20+
type CliArchivePlatformKey,
2021
parseChecksums,
2122
} from "@t3tools/shared/cliRelease";
2223

@@ -111,6 +112,8 @@ interface PinnedRuntimeInstallInput {
111112
readonly arch: string;
112113
readonly httpClient: HttpClient.HttpClient;
113114
readonly releaseBaseUrl?: string | undefined;
115+
/** Reuse a cached runtime only while the release source still lists it, as a policy mirror does. */
116+
readonly requirePublishedRelease?: boolean;
114117
readonly onProgress?: (progress: PinnedRuntimeProgress) => void;
115118
}
116119

@@ -178,25 +181,8 @@ const installFromArchive = Effect.fn("cloud.pinned_runtime.install_archive")(fun
178181
});
179182
}
180183
const httpClient = input.httpClient;
181-
const baseUrl = cliReleaseDownloadBaseUrl(input.version, input.releaseBaseUrl);
182-
const fileName = cliArchiveFileName(input.version, platformKey);
183-
184184
input.onProgress?.({ stage: "download", received: 0, total: undefined });
185-
const checksums = parseChecksums(
186-
new TextDecoder().decode(
187-
yield* fetchReleaseAsset(
188-
httpClient,
189-
`${baseUrl}/${CLI_RELEASE_CHECKSUMS_FILE}`,
190-
"downloading the t3 release checksums",
191-
),
192-
),
193-
);
194-
const expected = checksums.get(fileName);
195-
if (expected === undefined) {
196-
return yield* new PinnedRuntimeInstallError({
197-
step: `finding ${fileName} in the t3 release checksums`,
198-
});
199-
}
185+
const { baseUrl, fileName, expected } = yield* publishedArchive(input, platformKey);
200186
const archive = yield* fetchReleaseAsset(
201187
httpClient,
202188
`${baseUrl}/${fileName}`,
@@ -248,6 +234,31 @@ const installFromArchive = Effect.fn("cloud.pinned_runtime.install_archive")(fun
248234
yield* fs.remove(archivePath, { force: true }).pipe(Effect.ignore);
249235
});
250236

237+
/** The release's archive for this machine and its checksum; fails when the release lists none. */
238+
const publishedArchive = Effect.fn("cloud.pinned_runtime.published_archive")(function* (
239+
input: PinnedRuntimeInstallInput,
240+
platformKey: CliArchivePlatformKey,
241+
) {
242+
const baseUrl = cliReleaseDownloadBaseUrl(input.version, input.releaseBaseUrl);
243+
const fileName = cliArchiveFileName(input.version, platformKey);
244+
const checksums = parseChecksums(
245+
new TextDecoder().decode(
246+
yield* fetchReleaseAsset(
247+
input.httpClient,
248+
`${baseUrl}/${CLI_RELEASE_CHECKSUMS_FILE}`,
249+
"downloading the t3 release checksums",
250+
),
251+
),
252+
);
253+
const expected = checksums.get(fileName);
254+
if (expected === undefined) {
255+
return yield* new PinnedRuntimeInstallError({
256+
step: `finding ${fileName} in the t3 release checksums`,
257+
});
258+
}
259+
return { baseUrl, fileName, expected };
260+
});
261+
251262
const installPinnedRuntime = Effect.fn("cloud.pinned_runtime.ensure_installed")(function* (
252263
input: PinnedRuntimeInstallInput,
253264
) {
@@ -265,6 +276,15 @@ const installPinnedRuntime = Effect.fn("cloud.pinned_runtime.ensure_installed")(
265276
const alreadyPinned =
266277
entryExists && Option.isSome(sentinel) && sentinel.value.trim() === input.version;
267278
if (alreadyPinned) {
279+
if (input.requirePublishedRelease === true) {
280+
const platformKey = cliArchivePlatformKey(input.platform, input.arch);
281+
if (platformKey === undefined) {
282+
return yield* new PinnedRuntimeInstallError({
283+
step: `selecting a t3 release archive for ${input.platform}-${input.arch}`,
284+
});
285+
}
286+
yield* publishedArchive(input, platformKey);
287+
}
268288
input.onProgress?.({ stage: "cached" });
269289
yield* input.validate(paths);
270290
return paths;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* () {
132132
const platform = yield* HostProcess.Platform;
133133
const arch = yield* HostProcess.Architecture;
134134
const httpClient = yield* HttpClient.HttpClient;
135+
const mirror = managedUpdateMirror(policy.document);
135136
const releaseBaseUrl =
136-
managedUpdateMirror(policy.document)?.feedUrl ??
137+
mirror?.feedUrl ??
137138
Option.getOrUndefined(yield* Config.String(CLI_RELEASE_BASE_URL_ENV).pipe(Config.option));
138139
const inFlight = yield* Ref.make(false);
139140

@@ -209,6 +210,7 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* () {
209210
platform,
210211
arch,
211212
releaseBaseUrl,
213+
requirePublishedRelease: mirror !== undefined,
212214
validate: (runtime) =>
213215
runner
214216
.run({

0 commit comments

Comments
 (0)