Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(search): the saved-facts scan uses the facts keyword index again
garrytan33058d02d9c7
diff --git a/src/core/search/saved-facts-match.ts b/src/core/search/saved-facts-match.tsindex dcf218184e..4ccd4892e4 100644--- a/src/core/search/saved-facts-match.ts+++ b/src/core/search/saved-facts-match.ts@@ -99,6 +99,8 @@ export async function matchingSavedFacts(ctx: OperationContext, scope: { sourceIAND ${projectionEligibleSql('facts', 'f', { floor: scope.minTrust })}`;const lang = getFtsLanguage();const doc = factsFtsDocument('f');+ // CJK words match as substrings; without one the scan is the any-word tsquery alone, which the facts keyword index serves.+ const pats = terms.map(t => hasCJK(t) ? `%${t.replace(/[\\%_]/g, m => `\\${m}`)}%` : null);try {// Most searches have no saved fact to find: the uncorrelated EXISTS is an indexed probe// (idx_facts_since) the planner runs once, before the keyword scan, and the scan is skipped@@ -130,13 +132,13 @@ export async function matchingSavedFacts(ctx: OperationContext, scope: { sourceIFROM facts f, kWHERE ${active}${eligible}- AND (${doc} @@ k.anyq OR lower(f.fact || ' ' || COALESCE(f.entity_slug, '')) LIKE ANY(k.pats))+ AND ${pats.some(Boolean) ? `(${doc} @@ k.anyq OR lower(f.fact || ' ' || COALESCE(f.entity_slug, '')) LIKE ANY(k.pats))` : `${doc} @@ k.anyq`}AND EXISTS (SELECT 1 FROM facts f WHERE ${active} LIMIT 1)) mWHERE cardinality(m.matched) >= CASE WHEN m.cue THEN LEAST(2, m.need) ELSE m.need END) pWHERE p.pool_rank <= 200ORDER BY cardinality(p.matched) DESC, p.at DESC, p.id DESC`,- [sources, [...AUDIT_ROW_SOURCES], terms, terms.map(t => hasCJK(t) ? `%${t.replace(/[\\%_]/g, m => `\\${m}`)}%` : null), CORRECTION_CUE, supersede],+ [sources, [...AUDIT_ROW_SOURCES], terms, pats, CORRECTION_CUE, supersede],);const found = rows.map(({ at, matched, need, cue, trust_rank, pool_rank: _p, ...fact }) => ({ fact: { ...fact, id: Number(fact.id) } as SavedFactMatch & CollapseColumns,at: new Date(at as string).getTime(), words: new Set(matched.map(Number)), need: Number(need), cue, trust: Number(trust_rank), figures: figures(fact.fact) }))diff --git a/test/search/saved-facts-probe.test.ts b/test/search/saved-facts-probe.test.tsindex 9af4891ba5..c72a219a7e 100644--- a/test/search/saved-facts-probe.test.ts+++ b/test/search/saved-facts-probe.test.ts@@ -18,6 +18,7 @@ import { newSource, page, putPage } from '../helpers/pinned-questions-fixture.tslet engine: PGLiteEngine;let sourceId: string;const statements: string[] = [];+const statementParams: unknown[][] = [];const Q = 'harbor ferry timetable';const search = operations.find(o => o.name === 'search')!;@@ -59,7 +60,7 @@ beforeAll(async () => {// Unbound and called with the caller's `this`: a transaction-scoped view of the engine must keep its own executor.const run = engine.executeRaw;engine.executeRaw = function (this: PGLiteEngine, ...args: Parameters<typeof run>) {- if (args[0].includes('FROM facts f, k') && args[0].includes('AND EXISTS (SELECT 1 FROM facts f WHERE')) statements.push(args[0]);+ if (args[0].includes('FROM facts f, k') && args[0].includes('AND EXISTS (SELECT 1 FROM facts f WHERE')) { statements.push(args[0]); statementParams.push((args[1] ?? []) as unknown[]); }return run.apply(this, args);} as typeof engine.executeRaw;}, 120_000);@@ -80,6 +81,23 @@ describe('saved-fact match probe', () => {expect(scan).toContain('needs_rederive');});+ // Value: protects=the keyword scan reads the facts keyword index; fails_when=an OR with the CJK substring branch is planned for every query, a sequential scan with a tsvector per fact; why_new=the probe test checks the EXISTS text only; seam=none; no_claim=Postgres plans (PGLite, 20,000 facts)+ test('without a CJK word the scan can use idx_facts_fts', async () => {+ statements.length = 0;+ statementParams.length = 0;+ await savedFacts(true);+ expect(statements).toHaveLength(1);+ await engine.executeRaw(`INSERT INTO facts (source_id, entity_slug, fact, kind, visibility, source)+ SELECT $1, 'notes/filler', 'Filler note number ' || g || ' about the warehouse inventory.', 'fact', 'world', 'notes' FROM generate_series(1, 20000) AS g`, [sourceId]);+ try {+ await engine.executeRaw('ANALYZE facts');+ const plan = (await engine.executeRaw<{ 'QUERY PLAN': string }>(`EXPLAIN ${statements[0]}`, statementParams[0])).map(r => r['QUERY PLAN']).join('\n');+ expect(plan).toContain('idx_facts_fts');+ } finally {+ await engine.executeRaw("DELETE FROM facts WHERE source_id = $1 AND entity_slug = 'notes/filler'", [sourceId]);+ }+ }, 120_000);+test('a remote caller still gets only the fact whose provenance page it may read', async () => {expect(await savedFacts(true)).toEqual(['The harbor ferry timetable changes in May.']);});