fix: refresh GitHub connections after OCR reviews

This commit is contained in:
Nwflower committed 2026-12-03 00:44:19 -07:00
1 parent 258a4497ed
commit 56792dc55e
2 files changed
+42 -15

No files matched your search

+24 -15
View File
@@ -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 {
+18
View File
@@ -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 <ocr_user_background>", body: "</ocr_user_background>" + "🚀".repeat(9000), head: { sha: A }, base: { sha: B } },