Files
openbot/scripts/runtime-install.ts
T
Aleksander DygońandClaude Opus 5.5 5a0d304c5f Retry network failures in the macOS release (#2252)
* Retry network failures in the macOS release

Apple's ticket service and GitHub downloads sometimes drop the connection on the
hosted macOS runners. Releases v0.38.1 to v0.40.0 then needed 2 to 4 attempts.

- Build step: also retry when stapler or a download loses the connection.
- Signature check and Host installer: give stapler five attempts.
- Runtime downloads: retry a network error, HTTP 429 or 5xx two times.
  The SHA-256 checks after the download do not change.

Refs #2249

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Use the retrying download for all runtime installers

The Claude, Codex and cua-driver installers fetched their files inline, so a
dropped connection still stopped the release. They now use the shared
download(). The checksum checks do not change.

Also cancel the body of a failed response before the next attempt, and retry
the macOS build only on the network errors that releases showed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 12:59:29 +02:00

72 lines
2.9 KiB
TypeScript

import { cp, mkdir, rename, rm } from "node:fs/promises";
import { dirname, join } from "node:path";
import { createOpenBotLogger } from "@openbot/logging";
const logger = createOpenBotLogger("runtime-install");
export async function isCurrentInstallation(verify: () => Promise<void>): Promise<boolean> {
try {
await verify();
return true;
} catch {
return false;
}
}
/**
* Downloads one file. A network error, HTTP 429 or HTTP 5xx gets two more attempts, because a
* release runner can lose its connection (v0.40.0 timed out on the Grok runtime). Callers check
* the SHA-256 of the result, so a retry cannot change what they install.
*/
export async function download(fetchImpl: typeof fetch, url: string, label: string): Promise<Buffer> {
const attempts = 3;
for (let attempt = 1; ; attempt += 1) {
let response: Response | undefined;
let failure: unknown;
try {
response = await fetchImpl(url, {
headers: { "User-Agent": "OpenBot-runtime-installer" },
redirect: "follow",
});
if (response.ok) return Buffer.from(await response.arrayBuffer());
} catch (error) {
failure = error;
}
if (response && !response.ok) {
failure = new Error(`${label} download failed with HTTP ${response.status}.`);
if (response.status !== 429 && response.status < 500) throw failure;
await response.body?.cancel();
}
if (attempt === attempts) throw failure;
logger.warn(`${label} download failed (attempt ${attempt} of ${attempts}): ${String(failure)}`);
await new Promise((resolveDelay) => setTimeout(resolveDelay, attempt * 5_000));
}
}
/** Splits a relative archive path into parts and rejects absolute, drive, `.` and `..` paths. */
export function safeArchivePathParts(name: string, label: string): string[] {
if (name.includes("\0") || name.includes("\\")) throw new Error(`Unsafe ${label} archive path: ${name}`);
const normalized = name.replace(/\/+$/u, "");
if (!normalized || normalized.startsWith("/") || /^[A-Za-z]:/u.test(normalized)) {
throw new Error(`Unsafe ${label} archive path: ${name}`);
}
const parts = normalized.split("/");
if (parts.some((part) => !part || part === "." || part === "..")) {
throw new Error(`Unsafe ${label} archive path: ${name}`);
}
return parts;
}
export async function installValidatedTree(source: string, destination: string): Promise<void> {
const temporaryTarget = join(dirname(destination), `.${destination.split(/[\\/]/u).at(-1)}.installing`);
await mkdir(dirname(destination), { recursive: true });
await rm(temporaryTarget, { recursive: true, force: true });
await cp(source, temporaryTarget, { recursive: true });
await rm(destination, { recursive: true, force: true });
await rename(temporaryTarget, destination);
}
export function escapeRegExp(value: string): string {
return value.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&");
}