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 <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-09-24 13:41:45 +09:00
co-authored by Claude Opus 5.5
parent 1b9e2c5ec6
commit 1d7ca9dcb8
5 changed files with 77 additions and 25 deletions
@@ -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<BrowserRpcTransportFailureCode>([
"NETWORK_UNREACHABLE",
@@ -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) ||
@@ -68,6 +68,7 @@ export function createCursorPaginationRuntime<Value>(dependencies: Readonly<{
return Object.freeze({
async loadAll(context) {
const items: Value[] = [];
let pageBytes = 0;
const cursors = new Set<string>();
let cursor: string | null = null;
let snapshot: string | null | undefined;
@@ -108,16 +109,21 @@ export function createCursorPaginationRuntime<Value>(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)) {
@@ -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(
@@ -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<number>(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);
});
});
/**