Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/storage/engine-bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ const SUPPORTED_TARGETS = [
"win32-x64", "win32-arm64",
];

const BOOTSTRAP_TIMEOUT_MS = 10_000;
// Whole-download budget for the ~11 MB engine tarball. Must cover a multi-MB
// registry fetch over a non-trivial network (corporate proxy / slow link);
// 10 s locked Node<=21 machines into a 6 h legacy-only cooldown after one slow
// boot (#3338). Aligned with the updater's own tarball budget (src/update.ts).
export const BOOTSTRAP_TIMEOUT_MS = 60_000;
export const COOLDOWN_MS = 6 * 60 * 60 * 1000;

type BootstrapStatus =
Expand Down Expand Up @@ -94,6 +98,13 @@ export function resetEngineBootstrapForTests(): void {
inFlight = null;
}

let bootstrapTimeoutOverrideMs: number | null = null;

/** Test seam: override the whole-download timeout (null = production value). */
export function setBootstrapTimeoutMsForTests(ms: number | null): void {
bootstrapTimeoutOverrideMs = ms;
}

function activeSpec(): EngineBootstrapSpec {
return specOverride ?? DEFAULT_ENGINE_SPEC;
}
Expand Down Expand Up @@ -288,7 +299,7 @@ async function downloadAndInstall(spec: EngineBootstrapSpec, target: string, res
// @types/node types RequestInit.dispatcher as its internal Dispatcher;
// cast once here (no `as any`), same pattern as src/update.ts fetchWithEgress.
const res = await fetch(url, {
signal: AbortSignal.timeout(BOOTSTRAP_TIMEOUT_MS),
signal: AbortSignal.timeout(bootstrapTimeoutOverrideMs ?? BOOTSTRAP_TIMEOUT_MS),
...(dispatcher ? { dispatcher } : {}),
} as RequestInit);
if (!res.ok || !res.body) throw new Error(`registry returned HTTP ${res.status} for ${url}`);
Expand Down
78 changes: 74 additions & 4 deletions tests/storage-engine-bootstrap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ import {
setSqliteProbeOverridesForTests,
} from "../src/storage/driver.ts";
import {
BOOTSTRAP_TIMEOUT_MS,
ensureSqliteEngine,
extractTgzToDir,
platformTargetKey,
resetEngineBootstrapForTests,
setBootstrapTimeoutMsForTests,
setEngineBootstrapSpecForTests,
setPlatformKeyForTests,
type BootstrapOutcome,
Expand Down Expand Up @@ -142,7 +144,7 @@ interface MockRegistry {
close(): Promise<void>;
}

async function startMockRegistry(body: Buffer, status = 200, version = "13.0.3"): Promise<MockRegistry> {
async function startMockRegistry(body: Buffer, status = 200, version = "13.0.3", delayMs = 0): Promise<MockRegistry> {
let count = 0;
const server = http.createServer((req, res) => {
count++;
Expand All @@ -151,15 +153,27 @@ async function startMockRegistry(body: Buffer, status = 200, version = "13.0.3")
res.end();
return;
}
res.writeHead(status, { "content-type": "application/octet-stream" });
res.end(body);
const send = (): void => {
if (res.writableEnded || res.destroyed) return;
try {
res.writeHead(status, { "content-type": "application/octet-stream" });
res.end(body);
} catch {
// client aborted the download before we got here — nothing to serve
}
};
if (delayMs > 0) setTimeout(send, delayMs);
else send();
});
await new Promise<void>((r) => server.listen(0, "127.0.0.1", r));
const addr = server.address() as AddressInfo;
return {
base: `http://127.0.0.1:${addr.port}`,
requests: () => count,
close: () => new Promise<void>((r) => server.close(() => r())),
close: () => new Promise<void>((r) => {
(server as unknown as { closeAllConnections?: () => void }).closeAllConnections?.();
server.close(() => r());
}),
};
}

Expand Down Expand Up @@ -188,6 +202,7 @@ function resetAllSeams(): void {
clearSelfDownloadedEngineForTests();
setEngineBootstrapSpecForTests(null);
setPlatformKeyForTests(null);
setBootstrapTimeoutMsForTests(null);
resetEngineBootstrapForTests();
}

Expand Down Expand Up @@ -431,6 +446,61 @@ test("bootstrap: stale unusable cache dir is replaced instead of looping the coo
}
});

test("bootstrap: production download budget comfortably covers an ~11 MB registry fetch (#3338)", () => {
assert.ok(BOOTSTRAP_TIMEOUT_MS >= 30_000,
`BOOTSTRAP_TIMEOUT_MS=${BOOTSTRAP_TIMEOUT_MS} too tight for an ~11 MB download over a non-trivial network (was 10 s, see #3338)`);
});

test("bootstrap: a download that outlasts its timeout budget aborts fail-safe and arms the cooldown (#3338)", async () => {
const cachePrev = isolateCache();
let reg: MockRegistry | null = null;
try {
resetAllSeams();
const tgz = fakePackageTgz();
setEngineBootstrapSpecForTests({ version: "13.0.3", sha512Base64: sha512B64(tgz) });
setSqliteProbeOverridesForTests({ builtin: false, host: false });
const delayMs = 800;
setBootstrapTimeoutMsForTests(Math.floor(delayMs / 5));
reg = await startMockRegistry(tgz, 200, "13.0.3", delayMs);
await withRegistry(reg.base, async () => {
const out = await ensureSqliteEngine({ env: { ...UNIFIED_ENV } });
assert.equal(out.status, "failed", `expected timeout failure, got ${JSON.stringify(out)}`);
assert.match(out.detail ?? "", /abort|time/i, "failure reason names the abort/timeout");
assert.ok(!fs.existsSync(path.join(cachePrev, "billion-context", "sqlite", "better-sqlite3-13.0.3")), "aborted download must not publish a cache dir");
assert.ok(fs.existsSync(path.join(cachePrev, "billion-context", "sqlite", ".bootstrap-failed.json")), "aborted download arms the cooldown");
});
} finally {
await reg?.close();
resetAllSeams();
restoreCache(cachePrev);
rmrf(cachePrev);
}
});

test("bootstrap: a slow-but-completing download succeeds once the budget covers it (#3338)", async () => {
const cachePrev = isolateCache();
let reg: MockRegistry | null = null;
try {
resetAllSeams();
const tgz = fakePackageTgz();
setEngineBootstrapSpecForTests({ version: "13.0.3", sha512Base64: sha512B64(tgz) });
setSqliteProbeOverridesForTests({ builtin: false, host: false });
const delayMs = 400;
setBootstrapTimeoutMsForTests(delayMs * 10);
reg = await startMockRegistry(tgz, 200, "13.0.3", delayMs);
await withRegistry(reg.base, async () => {
const out = await ensureSqliteEngine({ env: { ...UNIFIED_ENV } });
assert.equal(out.status, "downloaded", `expected slow download to succeed, got ${JSON.stringify(out)}`);
assert.ok(fs.existsSync(path.join(cachePrev, "billion-context", "sqlite", "better-sqlite3-13.0.3", "package.json")), "slow download materialized");
});
} finally {
await reg?.close();
resetAllSeams();
restoreCache(cachePrev);
rmrf(cachePrev);
}
});

test("bootstrap: offline (connection refused) degrades fail-safe with cooldown, never throws", async () => {
const cachePrev = isolateCache();
try {
Expand Down
Loading