Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
perf(search): linear-time compiled-truth guarantee, allocation-free Jaccard in dedup (#5652)
garrytan2dedf4e11031
diff --git a/src/core/search/dedup.ts b/src/core/search/dedup.tsindex fd16a5d655..5d4d94d9e3 100644--- a/src/core/search/dedup.ts+++ b/src/core/search/dedup.ts@@ -117,9 +117,13 @@ function dedupByTextSimilarity(results: SearchResult[], threshold: number): Searlet tooSimilar = false;for (const kWords of samePageKept) {- const intersection = new Set([...rWords].filter(w => kWords.has(w)));- const union = new Set([...rWords, ...kWords]);- const jaccard = intersection.size / union.size;+ // |A ∩ B| by iterating the smaller set; |A ∪ B| = |A| + |B| - |A ∩ B|.+ // Same value as building intersection/union Sets, without allocating.+ const [smaller, larger] = rWords.size <= kWords.size ? [rWords, kWords] : [kWords, rWords];+ let intersectionSize = 0;+ for (const w of smaller) if (larger.has(w)) intersectionSize++;+ const unionSize = rWords.size + kWords.size - intersectionSize;+ const jaccard = unionSize === 0 ? 0 : intersectionSize / unionSize;if (jaccard > threshold) {tooSimilar = true;@@ -187,33 +191,30 @@ function capPerPage(results: SearchResult[], maxPerPage: number): SearchResult[]* away matching evidence (a 0.90 hit replaced by a 0.10 summary chunk).*/function guaranteeCompiledTruth(results: SearchResult[], preDedup: SearchResult[]): SearchResult[] {- // Group results by composite page key (source_id, slug).- const byPage = new Map<string, SearchResult[]>();+ // Per composite page key (source_id, slug): does the kept set already hold+ // a compiled_truth chunk? Insertion order = first appearance in results.+ const hasTruth = new Map<string, boolean>();for (const r of results) {const k = pageKey(r);- const existing = byPage.get(k) || [];- existing.push(r);- byPage.set(k, existing);+ hasTruth.set(k, hasTruth.get(k) === true || r.chunk_source === 'compiled_truth');}- const output = [...results];-- for (const [key, pageChunks] of byPage) {- const hasCompiledTruth = pageChunks.some(c => c.chunk_source === 'compiled_truth');- if (hasCompiledTruth) continue;-- // Find the best compiled_truth chunk from pre-dedup input for this- // (source_id, slug) combination. Pre-v0.17 single-source match was- // "r.slug === slug"; now it's the composite key so two same-slug- // pages in different sources don't mistakenly swap chunks across.- const candidate = preDedup- .filter(r => pageKey(r) === key && r.chunk_source === 'compiled_truth')- .sort((a, b) => b.score - a.score)[0];-- if (!candidate) continue;-- output.push(candidate);+ // One pre-dedup scan: the best compiled_truth chunk for each page that+ // lacks one (first-seen max wins on ties). The composite key keeps two+ // same-slug pages in different sources from borrowing each other's chunk.+ const best = new Map<string, SearchResult>();+ for (const r of preDedup) {+ if (r.chunk_source !== 'compiled_truth') continue;+ const k = pageKey(r);+ if (hasTruth.get(k) !== false) continue;+ const cur = best.get(k);+ if (cur === undefined || r.score > cur.score) best.set(k, r);}+ const output = [...results];+ for (const [k, truth] of hasTruth) {+ const candidate = truth ? undefined : best.get(k);+ if (candidate) output.push(candidate);+ }return output;}diff --git a/test/dedup.test.ts b/test/dedup.test.tsindex 492a1fd178..f543c018b8 100644--- a/test/dedup.test.ts+++ b/test/dedup.test.ts@@ -126,6 +126,19 @@ describe('compiled truth guarantee', () => {expect(hasCompiledTruth).toBe(true);});+ test('adds the highest-scoring compiled_truth chunk; first seen wins a tie', () => {+ const timeline = (id: number, score: number) => makeResult({+ slug: 'a', chunk_id: id, score, chunk_source: 'timeline', chunk_text: `timeline entry number ${id}`,+ });+ const truth = (id: number, score: number) => makeResult({+ slug: 'a', chunk_id: id, score, chunk_source: 'compiled_truth', chunk_text: `compiled truth variant ${id}`,+ });+ const best = dedupResults([timeline(1, 0.9), timeline(2, 0.8), timeline(3, 0.7), truth(4, 0.2), truth(5, 0.4), truth(6, 0.3)]);+ expect(best.map(r => r.chunk_id)).toEqual([1, 2, 5]);+ const tie = dedupResults([timeline(1, 0.9), timeline(2, 0.8), timeline(3, 0.7), truth(7, 0.3), truth(8, 0.3)]);+ expect(tie.map(r => r.chunk_id)).toEqual([1, 2, 7]);+ });+test('does not swap when page already has compiled_truth', () => {const results = [makeResult({ slug: 'a', chunk_id: 1, score: 0.9, chunk_source: 'compiled_truth', chunk_text: 'compiled assessment' }),