From 60d27162c6cfca23688515e606c26ed60e3b69a9 Mon Sep 17 00:00:00 2001 From: Aaron Stephen Cherian <212321731+Aaron-hub-dev-maker@users.noreply.github.com> Date: Mon, 30 Nov 2026 14:18:35 +0530 Subject: [PATCH 1/5] Time out retryable refusals whose body outlives the wait deadline waitForReview returns TIMED_OUT when a 429 or 503 arrives with a complete body and its Retry-After cannot start before the deadline. When the same refusal's body is still streaming as the deadline passes, the transport reported it as HTTP 429/503 "the response is not valid UTF-8 JSON", and waitForReview rethrew it, so a slow overloaded gateway turned a timed-out wait into an error with a misleading diagnosis. At the wait deadline a 429 or 503 body read now times out as a successful body does. Other refusals, such as 403, keep their status. Plain reads outside a wait are unchanged. This matches the Python SDK's check_stream rule ("Retryable statuses time out as an expired read does"). Co-Authored-By: Claude Opus 5.5 --- src/transport.ts | 7 ++++++- tests/http.test.mjs | 47 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/transport.ts b/src/transport.ts index 53c9bd4..a0e68af 100644 --- a/src/transport.ts +++ b/src/transport.ts @@ -226,7 +226,12 @@ async function decoded( } } catch (error) { if (error instanceof ProoflineError) throw error; - if (response.ok && (signal.aborted || performance.now() >= deadline)) + // At the wait deadline a retryable refusal times out as it would after a + // complete body: its retry could not start in time. Other refusals keep + // their status. + const timedOut = + response.ok || (waitTimeout && [429, 503].includes(response.status)); + if (timedOut && (signal.aborted || performance.now() >= deadline)) throw new ProoflineError( 0, waitTimeout diff --git a/tests/http.test.mjs b/tests/http.test.mjs index c1263ee..6af27b9 100644 --- a/tests/http.test.mjs +++ b/tests/http.test.mjs @@ -1060,6 +1060,53 @@ test("refusal retries preserve response bounds and the caller deadline", async ( assert.equal(fixture.calls.length - before, 1); }); +test("a retryable refusal whose body outlives the wait deadline times out like a complete one", async (t) => { + for (const status of [429, 503, 403]) { + for (const clock of ["elapsed", "native-abort"]) { + await t.test(`${status} ${clock}`, async (t) => { + const fixture = await server(t, (_, response) => { + response.writeHead(status, { + "Content-Type": "application/problem+json", + "Retry-After": "1", + }); + // The headers and a first chunk arrive; the rest never does. + response.write('{"type":"about:blank","detail":"'); + }); + const client = new ProoflineClient({ baseUrl: fixture.baseUrl, token }); + const reviewUrl = `${fixture.baseUrl}/orgs/by-id/${orgId}/reviews/${reviewId}`; + // "native-abort" keeps the observed clock before the deadline, so only + // the request's abort signal reports that the wait deadline expired. + const observed = performance.now(); + const mocked = + clock === "native-abort" + ? t.mock.method(performance, "now", () => observed) + : undefined; + try { + const waiting = client.waitForReview(orgId, reviewId, { + timeoutMs: 80, + }); + if (status === 403) { + // A refusal that is not retried keeps its status, as before. + await assert.rejects( + waiting, + (error) => + error instanceof ProoflineError && error.status === 403, + ); + } else { + const result = await waiting; + assert.equal(result.status, "TIMED_OUT"); + assert.equal(result.review, null); + assert.equal(result.reviewUrl, reviewUrl); + } + } finally { + mocked?.mock.restore(); + } + assert.equal(fixture.calls.length, 1); + }); + } + } +}); + test("invalid UTF-8 never hides successful-body or incomplete-body failures", async (t) => { for (const status of [200, 429, 503]) { for (const mode of [ From fb16d189fcd91e7d6477092889dc6b03fd2fc99b Mon Sep 17 00:00:00 2001 From: Aaron Stephen Cherian <212321731+Aaron-hub-dev-maker@users.noreply.github.com> Date: Mon, 30 Nov 2026 15:31:03 +0530 Subject: [PATCH 2/5] Make the elapsed-clock case reach the deadline check In the "elapsed" case the stalled body was cut off by the request's abort signal, so decoded() returned through signal.aborted and the performance.now() >= deadline branch was never exercised: removing it left every case passing. The elapsed case now moves the observed clock past the wait deadline and then ends the connection while the request's abort signal is still 20 seconds away, so it fails without the clock check, and the native-abort case fails without the signal check. Co-Authored-By: Claude Opus 5.5 --- tests/http.test.mjs | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/tests/http.test.mjs b/tests/http.test.mjs index 6af27b9..24c6b0f 100644 --- a/tests/http.test.mjs +++ b/tests/http.test.mjs @@ -1064,26 +1064,36 @@ test("a retryable refusal whose body outlives the wait deadline times out like a for (const status of [429, 503, 403]) { for (const clock of ["elapsed", "native-abort"]) { await t.test(`${status} ${clock}`, async (t) => { + const real = performance.now.bind(performance); + let skew = 0; const fixture = await server(t, (_, response) => { response.writeHead(status, { "Content-Type": "application/problem+json", "Retry-After": "1", }); // The headers and a first chunk arrive; the rest never does. - response.write('{"type":"about:blank","detail":"'); + response.write('{"type":"about:blank","detail":"', () => { + if (clock !== "elapsed") return; + // The observed clock passes the wait deadline, then the read fails + // while the request's abort signal, 20 seconds away, has not fired. + setTimeout(() => { + skew = 600_000; + response.socket.end(); + }, 20); + }); }); const client = new ProoflineClient({ baseUrl: fixture.baseUrl, token }); const reviewUrl = `${fixture.baseUrl}/orgs/by-id/${orgId}/reviews/${reviewId}`; - // "native-abort" keeps the observed clock before the deadline, so only - // the request's abort signal reports that the wait deadline expired. - const observed = performance.now(); - const mocked = - clock === "native-abort" - ? t.mock.method(performance, "now", () => observed) - : undefined; + // "elapsed" reports the expiry only through the observed clock; + // "native-abort" freezes that clock before the deadline, so only the + // request's abort signal reports that the wait deadline expired. + const observed = real(); + const mocked = t.mock.method(performance, "now", () => + clock === "elapsed" ? real() + skew : observed, + ); try { const waiting = client.waitForReview(orgId, reviewId, { - timeoutMs: 80, + timeoutMs: clock === "elapsed" ? 20_000 : 80, }); if (status === 403) { // A refusal that is not retried keeps its status, as before. @@ -1099,7 +1109,7 @@ test("a retryable refusal whose body outlives the wait deadline times out like a assert.equal(result.reviewUrl, reviewUrl); } } finally { - mocked?.mock.restore(); + mocked.mock.restore(); } assert.equal(fixture.calls.length, 1); }); From fd040f573b7a90d85b49bf7408002c010d0c1c5e Mon Sep 17 00:00:00 2001 From: Aaron Stephen Cherian <212321731+Aaron-hub-dev-maker@users.noreply.github.com> Date: Mon, 30 Nov 2026 16:02:52 +0530 Subject: [PATCH 3/5] Name the expiry condition for what it decides The flag is true for every successful response, timed out or not; it decides whether an expired body read is reported as a timeout. Co-Authored-By: Claude Opus 5.5 --- src/transport.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/transport.ts b/src/transport.ts index a0e68af..dc91014 100644 --- a/src/transport.ts +++ b/src/transport.ts @@ -229,9 +229,9 @@ async function decoded( // At the wait deadline a retryable refusal times out as it would after a // complete body: its retry could not start in time. Other refusals keep // their status. - const timedOut = + const timeoutOnExpiry = response.ok || (waitTimeout && [429, 503].includes(response.status)); - if (timedOut && (signal.aborted || performance.now() >= deadline)) + if (timeoutOnExpiry && (signal.aborted || performance.now() >= deadline)) throw new ProoflineError( 0, waitTimeout From 684a8715dc36f40792f19d6f83df3ba8f171af30 Mon Sep 17 00:00:00 2001 From: Aaron Stephen Cherian <212321731+Aaron-hub-dev-maker@users.noreply.github.com> Date: Mon, 30 Nov 2026 16:26:20 +0530 Subject: [PATCH 4/5] Share the retried statuses and describe the deadline rule exactly The rule does not make a cut-off refusal behave exactly like a complete one: a complete 429/503 is still thrown when its Retry-After exceeds 60 s or the retries are spent. Say what it does instead, rename the test accordingly, and use one RETRYABLE_STATUSES list for both the retry loop and the deadline rule so they cannot drift apart. The native-abort case gets a 500 ms budget so the response headers arrive before its abort fires on a slow runner. Co-Authored-By: Claude Opus 5.5 --- src/transport.ts | 12 +++++++----- tests/http.test.mjs | 5 +++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/transport.ts b/src/transport.ts index dc91014..16cc8c1 100644 --- a/src/transport.ts +++ b/src/transport.ts @@ -5,6 +5,7 @@ const DEFAULT_BASE_URL = "https://proofline.sh"; const MAX_RESPONSE_BYTES = 8 * 1024 * 1024; const MAX_RETRIES = 3; const MAX_RETRY_SECONDS = 60; +const RETRYABLE_STATUSES = [429, 503]; const inspect = Symbol.for("nodejs.util.inspect.custom"); const secrets = new WeakMap(); @@ -226,11 +227,12 @@ async function decoded( } } catch (error) { if (error instanceof ProoflineError) throw error; - // At the wait deadline a retryable refusal times out as it would after a - // complete body: its retry could not start in time. Other refusals keep - // their status. + // A retryable refusal cut off at the wait deadline reports the wait timing + // out, as the Python SDK's check_stream does. Other refusals keep their + // status. const timeoutOnExpiry = - response.ok || (waitTimeout && [429, 503].includes(response.status)); + response.ok || + (waitTimeout && RETRYABLE_STATUSES.includes(response.status)); if (timeoutOnExpiry && (signal.aborted || performance.now() >= deadline)) throw new ProoflineError( 0, @@ -538,7 +540,7 @@ export class Transport { const delay = retryDelay(response.headers.get("Retry-After"), attempt); if ( method !== "GET" || - ![429, 503].includes(response.status) || + !RETRYABLE_STATUSES.includes(response.status) || attempt >= MAX_RETRIES || delay === undefined ) diff --git a/tests/http.test.mjs b/tests/http.test.mjs index 24c6b0f..79b758d 100644 --- a/tests/http.test.mjs +++ b/tests/http.test.mjs @@ -1060,7 +1060,7 @@ test("refusal retries preserve response bounds and the caller deadline", async ( assert.equal(fixture.calls.length - before, 1); }); -test("a retryable refusal whose body outlives the wait deadline times out like a complete one", async (t) => { +test("a retryable refusal cut off at the wait deadline times the wait out", async (t) => { for (const status of [429, 503, 403]) { for (const clock of ["elapsed", "native-abort"]) { await t.test(`${status} ${clock}`, async (t) => { @@ -1093,7 +1093,8 @@ test("a retryable refusal whose body outlives the wait deadline times out like a ); try { const waiting = client.waitForReview(orgId, reviewId, { - timeoutMs: clock === "elapsed" ? 20_000 : 80, + // Long enough for the headers to arrive before the abort fires. + timeoutMs: clock === "elapsed" ? 20_000 : 500, }); if (status === 403) { // A refusal that is not retried keeps its status, as before. From 43e6f9c98e797b7071b1c6b54f4d7ff56eb4bf3c Mon Sep 17 00:00:00 2001 From: Aaron Stephen Cherian <212321731+Aaron-hub-dev-maker@users.noreply.github.com> Date: Tue, 1 Dec 2026 11:37:04 +0530 Subject: [PATCH 5/5] Pin that a cut-off refusal times out only at the deadline The test proved that a 429 or 503 body cut off at the wait deadline times the wait out, but not that the rule stops there: making every cut-off retryable refusal in a wait time out, even long before the deadline, still passed the whole suite. Add an "interrupted" case whose body ends while the deadline is far away. Every status must keep its invalid-response error, so a change that bypasses both deadline checks now fails it. Co-Authored-By: Claude Opus 5.5 --- tests/http.test.mjs | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/tests/http.test.mjs b/tests/http.test.mjs index 79b758d..901b6a1 100644 --- a/tests/http.test.mjs +++ b/tests/http.test.mjs @@ -1060,9 +1060,9 @@ test("refusal retries preserve response bounds and the caller deadline", async ( assert.equal(fixture.calls.length - before, 1); }); -test("a retryable refusal cut off at the wait deadline times the wait out", async (t) => { +test("a retryable refusal times the wait out only when cut off at the wait deadline", async (t) => { for (const status of [429, 503, 403]) { - for (const clock of ["elapsed", "native-abort"]) { + for (const clock of ["elapsed", "native-abort", "interrupted"]) { await t.test(`${status} ${clock}`, async (t) => { const real = performance.now.bind(performance); let skew = 0; @@ -1073,11 +1073,12 @@ test("a retryable refusal cut off at the wait deadline times the wait out", asyn }); // The headers and a first chunk arrive; the rest never does. response.write('{"type":"about:blank","detail":"', () => { - if (clock !== "elapsed") return; - // The observed clock passes the wait deadline, then the read fails - // while the request's abort signal, 20 seconds away, has not fired. + if (clock === "native-abort") return; + // "elapsed": the observed clock passes the wait deadline, then the + // read fails while the request's abort signal, 20 seconds away, has + // not fired. "interrupted": the read fails long before the deadline. setTimeout(() => { - skew = 600_000; + if (clock === "elapsed") skew = 600_000; response.socket.end(); }, 20); }); @@ -1086,22 +1087,26 @@ test("a retryable refusal cut off at the wait deadline times the wait out", asyn const reviewUrl = `${fixture.baseUrl}/orgs/by-id/${orgId}/reviews/${reviewId}`; // "elapsed" reports the expiry only through the observed clock; // "native-abort" freezes that clock before the deadline, so only the - // request's abort signal reports that the wait deadline expired. + // request's abort signal reports that the wait deadline expired; + // "interrupted" ends the body while the deadline is still far away. const observed = real(); const mocked = t.mock.method(performance, "now", () => - clock === "elapsed" ? real() + skew : observed, + clock === "native-abort" ? observed : real() + skew, ); try { const waiting = client.waitForReview(orgId, reviewId, { // Long enough for the headers to arrive before the abort fires. - timeoutMs: clock === "elapsed" ? 20_000 : 500, + timeoutMs: clock === "native-abort" ? 500 : 20_000, }); - if (status === 403) { - // A refusal that is not retried keeps its status, as before. + if (status === 403 || clock === "interrupted") { + // A refusal that is not retried, or a body cut off before the + // deadline, keeps its status and error, as before. await assert.rejects( waiting, (error) => - error instanceof ProoflineError && error.status === 403, + error instanceof ProoflineError && + error.status === status && + error.type === "urn:proofline:sdk:invalid-response", ); } else { const result = await waiting;