diff --git a/.github/scripts/pr-review.mjs b/.github/scripts/pr-review.mjs index 6fa855b..668a363 100644 --- a/.github/scripts/pr-review.mjs +++ b/.github/scripts/pr-review.mjs @@ -43,22 +43,31 @@ class HttpError extends Error { } } -async function githubRequest(url, token, body) { +export async function githubRequest(url, token, body, fetchRequest = fetch) { + const method = body === undefined ? "GET" : "POST"; let response; - try { - response = await fetch(url, { - method: body === undefined ? "GET" : "POST", - headers: { - Authorization: "Bearer " + token, - Accept: "application/vnd.github+json", - "Content-Type": "application/json", - }, - body: body === undefined ? undefined : JSON.stringify(body), - redirect: "error", - signal: AbortSignal.timeout(30_000), - }); - } catch (error) { - throw new Error("GitHub request failed (" + error.name + ")"); + for (let attempt = 0; ; attempt++) { + try { + response = await fetchRequest(url, { + method, + headers: { + Authorization: "Bearer " + token, + Accept: "application/vnd.github+json", + "Content-Type": "application/json", + // A synchronous OCR run can outlive idle HTTP connections in Node’s pool. + Connection: "close", + }, + body: body === undefined ? undefined : JSON.stringify(body), + redirect: "manual", + signal: AbortSignal.timeout(30_000), + }); + break; + } catch (error) { + if (method === "GET" && attempt === 0) continue; + const code = error.cause?.code ?? error.name; + const detail = /^(?:UND_ERR_[A-Z_]+|E[A-Z]+|TypeError|TimeoutError)$/.test(code) ? code : "network error"; + throw new Error("GitHub " + method + " " + new URL(url).pathname + " failed (" + detail + ")"); + } } if (!response.ok) throw new HttpError(response.status); try { diff --git a/.github/scripts/pr-review.test.mjs b/.github/scripts/pr-review.test.mjs index dd05655..bc11116 100644 --- a/.github/scripts/pr-review.test.mjs +++ b/.github/scripts/pr-review.test.mjs @@ -12,6 +12,7 @@ import { diffAnchors, effectiveDiffStats, extractPreviousReview, + githubRequest, hasSkipCIToken, normalizeLLMBaseURL, parseOCRJSON, @@ -40,6 +41,23 @@ async function tempDirectory(t, label) { return resolve(directory); } +test("GitHub uses fresh connections and retries reads without replaying review posts", async () => { + const url = "https://api.github.com/repos/test/example/pulls/1"; + const failure = new TypeError("do not disclose", { cause: { code: "UND_ERR_SOCKET" } }); + let requests = 0; + const fetchRequest = async (_url, options) => { + assert.equal(options.headers.Connection, "close"); + assert.equal(options.redirect, "manual"); + if (++requests === 1) throw failure; + return new Response("{}", { status: 200 }); + }; + await githubRequest(url, "key", undefined, fetchRequest); + assert.equal(requests, 2); + requests = 0; + await assert.rejects(githubRequest(url, "key", {}, fetchRequest), /POST .*UND_ERR_SOCKET/); + assert.equal(requests, 1); +}); + test("background stays within the OCR Unicode limit and protects reserved delimiters", () => { const background = backgroundText({ pull: { title: "Feature ", body: "" + "🚀".repeat(9000), head: { sha: A }, base: { sha: B } },