diff --git a/docs/development/releasing.md b/docs/development/releasing.md index 5edd5c567f4..63c358ee457 100644 --- a/docs/development/releasing.md +++ b/docs/development/releasing.md @@ -40,7 +40,9 @@ Before publishing, npm lookup failures and malformed metadata stop the job. An existing version must match the qualified tarball's SHA-512 and intended `latest`/`next` dist-tag before it can be reused. Publication never overwrites an existing version or silently changes a dist-tag. After publication, confirmed -registry absence is polled every 30 seconds for a 75-minute retry window. Lookup errors and content or tag mismatches fail immediately. If +registry absence is polled every 30 seconds. A single 75-minute deadline covers +both requests and polling delays, and each request keeps its 30-second cap. +Lookup errors and content or tag mismatches fail immediately. If visibility takes longer, rerun the failed jobs in the original workflow once the package appears. A separate post-publication job with read-only repository/actions permissions verifies registry bytes and tags, then installs diff --git a/scripts/ci/npm-release.d.mts b/scripts/ci/npm-release.d.mts index 266207cbde1..4ad6b47d661 100644 --- a/scripts/ci/npm-release.d.mts +++ b/scripts/ci/npm-release.d.mts @@ -10,6 +10,7 @@ export function stableUpdateVersion( ): string; export function waitForNpmRelease( version: string, - lookup?: (version: string) => Promise, - pause?: (milliseconds: number) => Promise, + lookup?: (version: string, signal: AbortSignal) => Promise, + pause?: (milliseconds: number, signal: AbortSignal) => Promise, + signal?: AbortSignal, ): Promise>; diff --git a/scripts/ci/npm-release.mjs b/scripts/ci/npm-release.mjs index 55293115132..5de9670a86c 100644 --- a/scripts/ci/npm-release.mjs +++ b/scripts/ci/npm-release.mjs @@ -9,6 +9,10 @@ import { requireMatchingNpmRelease, } from "./npm-release-state.mjs"; +const windowMinutes = 75; +const windowMs = windowMinutes * 60_000; +const intervalMs = 30_000; + /** Read-only callers derive the channel through the canonical SemVer owner. */ export async function readNpmRelease(version, request = fetch) { return readQualifiedNpmRelease(version, releaseNpmTag(version), request); @@ -33,16 +37,29 @@ export function stableUpdateVersion(version, published) { /** Retry only confirmed absence during the existing registry propagation window. */ export async function waitForNpmRelease( version, - lookup = readNpmRelease, - pause = setTimeout, + lookup = (candidate, signal) => + readNpmRelease(candidate, (url, init) => + fetch(url, { ...init, signal: AbortSignal.any([init.signal, signal]) }), + ), + pause = (milliseconds, signal) => + setTimeout(milliseconds, undefined, { signal }), + signal = AbortSignal.timeout(windowMs), ) { - const windowMinutes = 75; - const intervalMs = 30_000; - const attempts = (windowMinutes * 60_000) / intervalMs + 1; - for (let attempt = 1; attempt <= attempts; attempt++) { - const published = await lookup(version); - if (published.status === "published") return published; - if (attempt < attempts) await pause(intervalMs); + const attempts = windowMs / intervalMs + 1; + try { + for (let attempt = 1; attempt <= attempts; attempt++) { + signal.throwIfAborted(); + const published = await lookup(version, signal); + signal.throwIfAborted(); + if (published.status === "published") return published; + if (attempt < attempts) await pause(intervalMs, signal); + } + } catch (cause) { + if (!signal.aborted) throw cause; + throw new Error( + `npm registry visibility deadline reached for rea-agents@${version} after ${windowMinutes} minutes. Re-run the failed verification after the version becomes visible.`, + { cause }, + ); } throw new Error( `rea-agents@${version} was not visible on npm after ${attempts} attempts over ${windowMinutes} minutes. Re-run the failed verification after the version becomes visible.`, diff --git a/tests/boundary/filesystem/npmRelease.test.ts b/tests/boundary/filesystem/npmRelease.test.ts index 71db5d44953..316512765db 100644 --- a/tests/boundary/filesystem/npmRelease.test.ts +++ b/tests/boundary/filesystem/npmRelease.test.ts @@ -1,13 +1,14 @@ import { createHash } from "node:crypto"; import { writeFile } from "node:fs/promises"; import { join } from "node:path"; -import { expect, it } from "vitest"; +import { expect, it, vi } from "vitest"; import { readNpmRelease, stableUpdateVersion, waitForNpmRelease, } from "../../../scripts/ci/npm-release.mjs"; import { requireMatchingNpmRelease } from "../../../scripts/ci/npm-release-state.mjs"; +import { createDeferred } from "../../fixtures/binarySession.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; const metadata = ( @@ -174,3 +175,69 @@ it.each([ readNpmRelease("6.5.0", async () => Response.json(value)), ).rejects.toThrow(); }); + +it("keeps the 75-minute visibility budget and the per-request cap", async () => { + const timeout = vi.spyOn(AbortSignal, "timeout"); + const request = vi.fn(async () => + Response.json(metadata("6.5.0", "sha512-target", { latest: "6.5.0" })), + ); + vi.stubGlobal("fetch", request); + try { + await expect(waitForNpmRelease("6.5.0")).resolves.toMatchObject({ + status: "published", + }); + expect(timeout.mock.calls).toEqual([[75 * 60_000], [30_000]]); + expect(request).toHaveBeenCalledOnce(); + } finally { + vi.unstubAllGlobals(); + timeout.mockRestore(); + } +}); + +it("interrupts an in-flight registry request at the shared deadline", async () => { + const deadline = new AbortController(); + const reason = new Error("test deadline"); + const { promise: started, resolve: markStarted } = createDeferred(); + vi.stubGlobal("fetch", (_url: string, init: RequestInit) => { + markStarted(); + const requestSignal = init.signal; + if (!requestSignal) throw new Error("missing request signal"); + return new Promise((_resolve, reject) => { + requestSignal.addEventListener( + "abort", + () => reject(requestSignal.reason), + { + once: true, + }, + ); + }); + }); + try { + const waiting = waitForNpmRelease( + "6.5.0", + undefined, + undefined, + deadline.signal, + ); + await Promise.race([started, waiting]); + deadline.abort(reason); + await expect(waiting).rejects.toMatchObject({ + message: expect.stringContaining("after 75 minutes"), + cause: reason, + }); + } finally { + vi.unstubAllGlobals(); + } +}); + +it("interrupts the polling delay without starting another lookup", async () => { + const deadline = new AbortController(); + const lookup = vi.fn(async () => { + setImmediate(() => deadline.abort()); + return { status: "absent" } as const; + }); + await expect( + waitForNpmRelease("6.5.0", lookup, undefined, deadline.signal), + ).rejects.toThrow("visibility deadline reached"); + expect(lookup).toHaveBeenCalledExactlyOnceWith("6.5.0", deadline.signal); +});