Skip to content

Commit daecb2d

Browse files
vanceingallsclaude
andcommitted
fix(core): graceful collision fallback + body-scoped pin seed (R1 review)
Addresses review on #1269 (Miguel P3 + Rames): - mintHfId no longer throws on the pathological collision cap; it widens the id with the dup counter instead, so a degenerate document can never crash the whole parse call. - Seed the `assigned` set from `body.querySelectorAll("[data-hf-id]")` (was the whole document) to match the mint walk's scope — a stray head id can't bump a body element off its hash. - Document the 4-char id-space rationale in toHfId. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
1 parent af243a6 commit daecb2d

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

‎packages/core/src/parsers/hfIds.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ function fnv1a(str: string): number {
2323
return h >>> 0;
2424
}
2525

26+
// 4 base-36 chars · 36^4 ≈ 1.68M ids per document. Birthday-paradox collision
27+
// ≈ N²/(2·36^4): well under 1% per document after dup rehash at realistic
28+
// clip-model sizes (≤ a few hundred elements). The dup-rehash in mintHfId
29+
// resolves the rare collision; width is deliberately small for readable ids.
2630
function toHfId(hash: number): string {
2731
const s = (hash >>> 0).toString(36);
2832
// Use suffix (most-avalanched bits) for better distribution within the 4-char window.
@@ -55,8 +59,15 @@ export function mintHfId(el: Element, assigned: Set<string>): string {
5559
let id = toHfId(fnv1a(key));
5660
let dup = 0;
5761
while (assigned.has(id)) {
58-
if (dup > 1000) throw new Error("ensureHfIds: hash collision limit exceeded");
5962
dup += 1;
63+
// Graceful fallback instead of a hard throw: rehashing only fails to find a
64+
// free 4-char slot in a pathological document (~1.6M identical elements).
65+
// Rather than crash the whole parse, widen the id with the dup counter —
66+
// still deterministic and unique, just longer than the 4-char norm.
67+
if (dup > 10000) {
68+
id = `hf-${(fnv1a(key) >>> 0).toString(36)}-${dup}`;
69+
break;
70+
}
6071
id = toHfId(fnv1a(`${key}#${dup}`));
6172
}
6273
assigned.add(id);
@@ -76,7 +87,9 @@ export function ensureHfIds(html: string): string {
7687

7788
const assigned = new Set<string>();
7889
// Seed with already-present ids (pin) so fresh mints never collide with them.
79-
for (const el of Array.from(document.querySelectorAll("[data-hf-id]"))) {
90+
// Scope to <body> to match the mint walk below — a stray data-hf-id in <head>
91+
// must not pin an id into the set that a body element would then be bumped off.
92+
for (const el of Array.from(body.querySelectorAll("[data-hf-id]"))) {
8093
const existing = el.getAttribute("data-hf-id");
8194
if (existing) assigned.add(existing);
8295
}

0 commit comments

Comments
 (0)