From 1d7ca9dcb8b3d1167eae7bf30ce687a38843135f Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 24 Sep 2026 13:33:44 +0900 Subject: [PATCH] fix(query-cache): measure pagination per page, reject bare W/" ETag, drop dead RPC helper (Q-6.3-12) - cursor pagination re-serialized every accumulated item on each page (O(pages x total)) and used items.push(...page.items), which throws RangeError for a large page, so loadAll rejected instead of returning a Result. Each page is now measured once and summed (errs high by at most one byte per page), the item count is checked before copying, and items are appended one by one. - conditional-validator-store accepted `W/"` as a weak ETag because its opening quote also satisfied endsWith('"'). The closing quote must now come after the opening one. - browser-rpc-runtime's validTransportFailure had no callers, duplicated decodeTransportFailure and read properties directly, which RPC-RR-04 forbids. Deleted (approved for this template). Verified: new tests failed RED (18 serialized items instead of 6 because the accumulated array was passed each time, RangeError for a 1,000,000 item page, `W/"` installed) and pass now. All lane gates pass except the known removal-fixture test. Co-Authored-By: Claude Opus 5.5 --- .../browser-rpc/browser-rpc-runtime.ts | 16 ----- .../conditional-validator-store.ts | 11 ++-- .../query-cache/cursor-pagination-runtime.ts | 12 +++- .../unit/conditional-validator-store.test.ts | 2 + tests/unit/cursor-pagination-runtime.test.ts | 61 +++++++++++++++++++ 5 files changed, 77 insertions(+), 25 deletions(-) diff --git a/src/adapters/browser-rpc/browser-rpc-runtime.ts b/src/adapters/browser-rpc/browser-rpc-runtime.ts index da99e532..5842a154 100644 --- a/src/adapters/browser-rpc/browser-rpc-runtime.ts +++ b/src/adapters/browser-rpc/browser-rpc-runtime.ts @@ -1489,22 +1489,6 @@ function decodeTransportFailure( }); } -function validTransportFailure( - failure: unknown, -): failure is BrowserRpcTransportFailure { - if (!failure || typeof failure !== "object" || !("code" in failure)) { - return false; - } - const candidate = failure as BrowserRpcTransportFailure; - return ( - TRANSPORT_FAILURE_CODES.has(candidate.code) && - (candidate.retryAfterMs === undefined || - (Number.isSafeInteger(candidate.retryAfterMs) && - candidate.retryAfterMs >= 0 && - candidate.retryAfterMs <= BROWSER_RPC_HARD_LIMITS.maxRetryAfterMs)) - ); -} - const TRANSPORT_FAILURE_CODES = new Set([ "NETWORK_UNREACHABLE", diff --git a/src/adapters/query-cache/conditional-validator-store.ts b/src/adapters/query-cache/conditional-validator-store.ts index 81e51004..2522b1f2 100644 --- a/src/adapters/query-cache/conditional-validator-store.ts +++ b/src/adapters/query-cache/conditional-validator-store.ts @@ -138,12 +138,11 @@ export function createConditionalValidatorStore( function isSafeEntityTag(value: string): boolean { if (value.length < 3 || value.length > 256) return false; - const opaque = value.startsWith('W/"') - ? value.slice(3, -1) - : value.startsWith('"') - ? value.slice(1, -1) - : null; - if (opaque === null || !value.endsWith('"')) return false; + const start = value.startsWith('W/"') ? 3 : value.startsWith('"') ? 1 : -1; + // Q-6.3-12. The closing quote must follow the opening one: a bare `W/"` + // ends with its opening quote and is unclosed. + if (start < 0 || value.length <= start || !value.endsWith('"')) return false; + const opaque = value.slice(start, -1); return [...opaque].every((character) => { const code = character.codePointAt(0) ?? 0; return code === 0x21 || (code >= 0x23 && code <= 0x7e) || diff --git a/src/adapters/query-cache/cursor-pagination-runtime.ts b/src/adapters/query-cache/cursor-pagination-runtime.ts index 4c74b1ca..f355a831 100644 --- a/src/adapters/query-cache/cursor-pagination-runtime.ts +++ b/src/adapters/query-cache/cursor-pagination-runtime.ts @@ -68,6 +68,7 @@ export function createCursorPaginationRuntime(dependencies: Readonly<{ return Object.freeze({ async loadAll(context) { const items: Value[] = []; + let pageBytes = 0; const cursors = new Set(); let cursor: string | null = null; let snapshot: string | null | undefined; @@ -108,16 +109,21 @@ export function createCursorPaginationRuntime(dependencies: Readonly<{ "PAGINATION_SNAPSHOT_CHANGED", ); } - items.push(...page.items); + // Q-6.3-12. Each page is measured once (the sum errs high by at most + // one byte per page) and appended item by item: re-serializing the + // accumulated items was O(pages × total), and `push(...items)` + // throws a RangeError for a large page. + pageBytes += estimatedBytes(page.items); if ( - items.length > profile.maxTotalItems || - estimatedBytes(items) > profile.maxEstimatedBytes + items.length + page.items.length > profile.maxTotalItems || + pageBytes > profile.maxEstimatedBytes ) { return failure( "RESULT_LIMIT_EXCEEDED", "PAGINATION_RESULT_LIMIT", ); } + for (const item of page.items) items.push(item); if (!page.hasMore) return { ok: true, value: Object.freeze(items) }; const nextCursor: string | null = page.nextCursor; if (!nextCursor || cursors.has(nextCursor)) { diff --git a/tests/unit/conditional-validator-store.test.ts b/tests/unit/conditional-validator-store.test.ts index 9d4ccc62..df657455 100644 --- a/tests/unit/conditional-validator-store.test.ts +++ b/tests/unit/conditional-validator-store.test.ts @@ -79,6 +79,8 @@ describe("conditional validator CAS sidecar", () => { scope: firstScope.snapshot, }; expect(store.install(first, "raw-etag", 1)).toBe(false); + // Q-6.3-12. The opening quote of `W/"` is not also its closing quote. + expect(store.install(first, 'W/"', 1)).toBe(false); expect(store.install(first, 'W/"etag-a"', 1)).toBe(true); expect( store.install( diff --git a/tests/unit/cursor-pagination-runtime.test.ts b/tests/unit/cursor-pagination-runtime.test.ts index eeb6fce6..ebb938ab 100644 --- a/tests/unit/cursor-pagination-runtime.test.ts +++ b/tests/unit/cursor-pagination-runtime.test.ts @@ -157,6 +157,67 @@ describe("bounded cursor pagination runtime", () => { error: { code: "PAGINATION_SNAPSHOT_CHANGED" }, }); }); + + // Q-6.3-12. The byte estimate serialized every accumulated item again on + // each page, and `push(...items)` spreads a page into call arguments. + it("serializes each page once", async () => { + const pages = [["a", "b"], ["c", "d"], ["e", "f"]]; + const stringify = vi.spyOn(JSON, "stringify"); + try { + const runtime = createCursorPaginationRuntime({ + definitionId: "LIST_ALL", + profile: { ...profile, maxTotalItems: 6 }, + loadPage: async (cursor) => { + const index = cursor === null ? 0 : Number(cursor); + const last = index === pages.length - 1; + return { + ok: true, + value: { + items: pages[index] ?? [], + nextCursor: last ? null : String(index + 1), + hasMore: !last, + snapshotToken: null, + }, + }; + }, + }); + const result = await runtime.loadAll({}); + const serializedItems = stringify.mock.calls + .map(([value]) => (Array.isArray(value) ? value.length : 0)) + .reduce((sum, length) => sum + length, 0); + stringify.mockRestore(); + expect(result).toEqual({ + ok: true, + value: ["a", "b", "c", "d", "e", "f"], + }); + expect(serializedItems).toBe(6); + } finally { + stringify.mockRestore(); + } + }); + + it("accepts a page larger than the call argument limit", async () => { + const large = new Array(1_000_000).fill(0); + const bulk = createCursorPaginationRuntime({ + definitionId: "LIST_ALL", + profile: { + ...profile, + maxTotalItems: large.length, + maxEstimatedBytes: 4 * large.length, + }, + loadPage: async () => ({ + ok: true, + value: { + items: large, + nextCursor: null, + hasMore: false, + snapshotToken: null, + }, + }), + }); + const result = await bulk.loadAll({}); + expect(result.ok && result.value.length).toBe(large.length); + }); }); /**