From e7844942700d681d11aafce53435f2596fd801aa Mon Sep 17 00:00:00 2001 From: le king fu Date: Sat, 24 Oct 2026 14:41:21 -0400 Subject: [PATCH] fix(sync): read a missing Retry-After as null, not 0 Number(null) and Number('') are 0: a 429 without a Retry-After header made initialMerge resend the same batch three times with no wait instead of waiting 60 s. The header is now read by parseRetryAfter (syncRules.ts, tested under node), and the smoke guard locks its use in postBatch. Found by the autopilot self-review of PR #159. Refs #151 Co-Authored-By: Claude Opus 5.5 --- src/services/syncClient.ts | 4 ++-- src/services/syncRules.ts | 12 ++++++++++++ tests/smoke.test.cjs | 2 ++ tests/syncRules.test.mjs | 12 ++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/services/syncClient.ts b/src/services/syncClient.ts index 20b5144..45c0e7e 100644 --- a/src/services/syncClient.ts +++ b/src/services/syncClient.ts @@ -20,6 +20,7 @@ import { MAX_PUSH_BATCHES, classifyOutbox, isSynced, + parseRetryAfter, sendInBatches, type PostResponse, } from '@/src/services/syncRules'; @@ -70,8 +71,7 @@ async function postBatch(batch: SyncOperation[], headers: Record }); if (!res.ok) { console.warn(`[sync] push failed with status ${res.status}`); - const header = Number(res.headers.get('Retry-After')); - return { status: res.status, retryAfter: Number.isFinite(header) && header >= 0 ? header : null, body: null }; + return { status: res.status, retryAfter: parseRetryAfter(res.headers.get('Retry-After')), body: null }; } const body = await res.json().catch(() => null); return { status: res.status, retryAfter: null, body }; diff --git a/src/services/syncRules.ts b/src/services/syncRules.ts index 2f1e893..2cbc045 100644 --- a/src/services/syncRules.ts +++ b/src/services/syncRules.ts @@ -346,6 +346,18 @@ export function classifyOutbox( return { markIds, keptRefusals, droppedRefusals: dropped.length, droppedByType }; } +/** + * Retry-After header in seconds, or null when absent, empty, negative or not + * a number of seconds (an HTTP date included). Number(null) and Number('') + * are 0, hence the explicit checks: a missing header must mean null (wait + * RETRY_AFTER_MAX_S), not 0. + */ +export function parseRetryAfter(header: string | null | undefined): number | null { + if (header == null || header.trim() === '') return null; + const seconds = Number(header); + return Number.isFinite(seconds) && seconds >= 0 ? seconds : null; +} + /** Response of one POST, as seen by sendInBatches. */ export interface PostResponse { status: number; diff --git a/tests/smoke.test.cjs b/tests/smoke.test.cjs index b01c88a..c848ab8 100644 --- a/tests/smoke.test.cjs +++ b/tests/smoke.test.cjs @@ -209,6 +209,8 @@ check('syncClient.ts passes the #151 push bounds to sendInBatches', () => { assert.match(merge, /size: BATCH_SIZE,/); assert.match(body('pushChanges'), /\.orderBy\(asc\(syncOutbox\.createdAt\)\)/); assert.match(body('pushChanges'), /classifyOutbox\(unsynced, results,/); + // Number(null) is 0: a missing Retry-After must go through parseRetryAfter + assert.match(src, /retryAfter: parseRetryAfter\(res\.headers\.get\('Retry-After'\)\)/); }); // --- Build toolchain guards --- diff --git a/tests/syncRules.test.mjs b/tests/syncRules.test.mjs index c128a42..65adad1 100644 --- a/tests/syncRules.test.mjs +++ b/tests/syncRules.test.mjs @@ -26,6 +26,7 @@ import { RETRY_AFTER_MAX_S, MAX_429_RETRIES, parseResults, + parseRetryAfter, isSynced, classifyOutbox, sendInBatches, @@ -379,6 +380,17 @@ test('parseResults: the results array, or null for another shape', () => { assert.equal(parseResults({ results: [{ idempotencyKey: 'a', status: 'applied' }, null] }), null); }); +test('parseRetryAfter: seconds, or null when absent, empty, negative or a date', () => { + assert.equal(parseRetryAfter('7'), 7); + assert.equal(parseRetryAfter('0'), 0); + assert.equal(parseRetryAfter(null), null); + assert.equal(parseRetryAfter(undefined), null); + assert.equal(parseRetryAfter(''), null); + assert.equal(parseRetryAfter(' '), null); + assert.equal(parseRetryAfter('-5'), null); + assert.equal(parseRetryAfter('Wed, 21 Oct 2026 07:28:00 GMT'), null); +}); + // Fake transport: one response (or thrown error) per call, in order. function transport(responses) { const calls = [];