From 472909914821f64c653c6ccf1466ff6e261ed85c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Mon, 16 Nov 2026 06:41:16 +0000 Subject: [PATCH] fix(parsers): past 10,000 identical elements every id is still unique (#7919) --- packages/parsers/src/hfIdAssignment.ts | 24 ++++++++---------------- packages/parsers/src/hfIds.test.ts | 8 ++++++++ 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/packages/parsers/src/hfIdAssignment.ts b/packages/parsers/src/hfIdAssignment.ts index 72152e839d..b692a77934 100644 --- a/packages/parsers/src/hfIdAssignment.ts +++ b/packages/parsers/src/hfIdAssignment.ts @@ -72,6 +72,12 @@ function contentKey(el: Element): string { * twin added or removed), so a save carrying an id served before the edit misses or hits the other * twin until the watcher reloads the preview. */ +// Past 10,000 twins the 4-char window is crowded: widen the id with the dup counter, still checked against the set. +function dupId(key: string, dup: number): string { + if (dup > 10000) return `hf-${(fnv1a(key) >>> 0).toString(36)}-${dup}`; + return toHfId(fnv1a(dup ? `${key}#${dup}` : key)); +} + // WIRE CONTRACT: id minting is content-keyed (FNV1a of tag, sorted attrs, own text). R7's // preview route relies on mintHfId producing identical ids across mint contexts // (served copy vs. the source a save parses) — see preview.test.ts @@ -84,22 +90,8 @@ export function mintHfId(el: Element, assigned: Set): string { const twins = lastDup.get(assigned) ?? new Map(); lastDup.set(assigned, twins); let dup = twins.get(key) ?? 0; - let id = - dup > 10000 - ? `hf-${(fnv1a(key) >>> 0).toString(36)}-${dup}` - : toHfId(fnv1a(dup ? `${key}#${dup}` : key)); - while (dup <= 10000 && assigned.has(id)) { - dup += 1; - // Graceful fallback instead of a hard throw: rehashing only fails to find a - // free 4-char slot in a pathological document (~1.6M identical elements). - // Rather than crash the whole parse, widen the id with the dup counter — - // still deterministic and unique, just longer than the 4-char norm. - if (dup > 10000) { - id = `hf-${(fnv1a(key) >>> 0).toString(36)}-${dup}`; - break; - } - id = toHfId(fnv1a(`${key}#${dup}`)); - } + let id = dupId(key, dup); + while (assigned.has(id)) id = dupId(key, ++dup); twins.set(key, dup); assigned.add(id); return id; diff --git a/packages/parsers/src/hfIds.test.ts b/packages/parsers/src/hfIds.test.ts index ae4b2bdc4d..b71456ce78 100644 --- a/packages/parsers/src/hfIds.test.ts +++ b/packages/parsers/src/hfIds.test.ts @@ -245,6 +245,14 @@ describe("ensureHfIds", () => { expect(lookups).toBeLessThan(3 * twins); }); + it("past 10,000 identical elements, every id is still unique", () => { + const { document } = parseHTML(`

same

`); + const el = document.querySelector("p") as Element; + const assigned = new Set(); + for (let i = 0; i < 10050; i += 1) mintHfId(el, assigned); + expect(assigned.size).toBe(10050); + }); + // Post-persist stability: once data-hf-id is written back to source, edits // don't drift the id because the attribute is already present and pinned. it("pinned id survives text edit after first persist", () => {