Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(search): entity facts never bring back a claim the saved-facts cap left corrected (#7938)
garrytancea863023de2
diff --git a/src/core/ops/search.ts b/src/core/ops/search.tsindex 12ef8fb51e..57a2a84bc5 100644--- a/src/core/ops/search.ts+++ b/src/core/ops/search.ts@@ -419,7 +419,7 @@ async function buildRetrievalResponseMeta(const readinessScope = { ...scope, excludePrivate, types: opts.types, excludeSlugPrefixes };// #6721: these reads depend on nothing but the inputs above, so they run at once: on a pooled Postgres// the wall time is the slowest read's round trips, not their sum. The answer-feedback write still runs last.- const [safeIndexPending, [readiness, pendingCount], entityFacts, matchedFacts, held, sourceFreshness, embeddingHeld, filterNotice] = await Promise.all([+ const [safeIndexPending, [readiness, pendingCount], entityFacts, matched, held, sourceFreshness, embeddingHeld, filterNotice] = await Promise.all([timed('safe_index', ctx.remote !== false && hasUnsealedPagesInScope(ctx, scope, excludePrivate, { types: opts.types, excludeSlugPrefixes })),timed('readiness', probeProjectionReadiness(ctx.engine, readinessScope).then(async r =>[r, r.status === 'projection_pending' ? await countPendingProjections(ctx.engine, readinessScope, relevance) : null] as const)),@@ -441,17 +441,27 @@ async function buildRetrievalResponseMeta(// A fact the facts arm already returned as a row is not repeated beside the blocks.const factRowIds = new Set((results as SearchResult[]).flatMap(r => r.fact_row ? [r.fact_row.id] : []));// #7043: a matched fact may copy an entity fact; the merged list collapses again before the response.- // An entity fact keeps the corrected_by its matched copy carries, and a label follows its corrector into the collapse.- const correctedBy = new Map(matchedFacts.flatMap(m => m.corrected_by ? [[m.id, m.corrected_by] as const] : []));- const merged = await collapseFactReads(ctx.engine, [...entityFacts.map(e => ({ ...e, corrected_by: correctedBy.get(e.id) })),- ...matchedFacts.filter(m => !entityFacts.some(e => e.id === m.id))].filter(f => !factRowIds.has(f.id)));+ const merged = await collapseFactReads(ctx.engine, [...entityFacts,+ ...matched.facts.filter(m => !entityFacts.some(e => e.id === m.id)).map(({ corrected_by: _, ...m }) => m)].filter(f => !factRowIds.has(f.id)));const shownAs = new Map([...factRowIds].map(id => [id, id]));for (const f of merged) for (const id of f.collapsed_ids ?? [f.id]) shownAs.set(id, f.id);+ // #7938: the matcher's correction decisions cover facts its cap cut, which the entity facts can bring back. A+ // corrected fact shows only labelled beside a returned corrector; dropping one can strand a fact it corrected,+ // so the drop repeats until every remaining label names a returned fact.+ const correctors = (f: { id: number; collapsed_ids?: number[] }) => (f.collapsed_ids ?? [f.id]).flatMap(id => matched.correctedBy.get(id) ?? []);+ let visible = merged;+ for (;;) {+ const kept = visible.filter(f => { const by = correctors(f); return !by.length || by.some(id => shownAs.has(id) && shownAs.get(id) !== f.id); });+ if (kept.length === visible.length) break;+ const keptIds = new Set(kept.map(f => f.id));+ for (const [id, rep] of shownAs) if (!factRowIds.has(rep) && !keptIds.has(rep)) shownAs.delete(id);+ visible = kept;+ }// #8042: a lookup failure withholds saved_facts (degraded stage below) rather than return third-party text unquoted.- const quotedFacts = await quoteFacts(ctx.engine, merged, ctx.remote !== false).catch(() => null);- const savedFacts = (quotedFacts ?? []).map(({ corrected_by, ...f }) => {- const by = corrected_by === undefined ? undefined : shownAs.get(corrected_by);- return stripCollapseColumns(by === undefined || by === f.id ? f : { ...f, corrected_by: by });+ const quotedFacts = await quoteFacts(ctx.engine, visible, ctx.remote !== false).catch(() => null);+ const savedFacts = (quotedFacts ?? []).map(f => {+ const by = correctors(f).map(id => shownAs.get(id)).find(id => id !== undefined && id !== f.id);+ return stripCollapseColumns(by === undefined ? f : { ...f, corrected_by: by });});const heldTotal = [...heldBySource.values()].reduce((sum, n) => sum + n, 0);const embedding = embeddingHeld?.total ? embeddingHeld : null;@@ -470,7 +480,7 @@ async function buildRetrievalResponseMeta(if (heldNotice) ctx.emitNotice?.(heldNotice);const degraded = [...(m?.degraded ?? [])];if (safeIndexPending) degraded.push({ stage: 'safe_index_pending' });- if (quotedFacts === null && merged.length) degraded.push({ stage: 'saved_facts_withheld' });+ if (quotedFacts === null && visible.length) degraded.push({ stage: 'saved_facts_withheld' });if (readiness.status === 'by_design') degraded.push({ stage: 'projection_by_design' });else if (readiness.status !== 'ready') {degraded.push({ stage: readiness.status === 'projection_pending' ? 'projection_pending' : 'projection_status_unknown' });diff --git a/src/core/search/saved-facts-match.ts b/src/core/search/saved-facts-match.tsindex e35dce553d..b8a9657b81 100644--- a/src/core/search/saved-facts-match.ts+++ b/src/core/search/saved-facts-match.ts@@ -28,6 +28,9 @@ export interface SavedFactMatch { id: number; fact: string; entity_slug: string/** With `search.saved_facts_supersede` on: the newer returned fact that corrects this one. */corrected_by?: number }+/** The matched facts, and every correction decision made before the five-fact cap (corrected fact id to its corrector's id). */+export interface SavedFactMatches { facts: Array<SavedFactMatch & CollapseColumns>; correctedBy: Map<number, number> }+/** Default on (set false to turn off): a newer saved fact carrying a correction cue is returned ahead of the older fact it corrects, which is labelled `corrected_by`. */export const SAVED_FACTS_SUPERSEDE_KEY = 'search.saved_facts_supersede';/** Cues that withdraw what was said, so they correct without stating a new figure. */@@ -83,14 +86,17 @@ async function savedFactsSupersedeEnabled(engine: { getConfig(key: string): Prom* correction. Nor does a cue fact about another entity, one missing every* name the older fact starts with a matched word, one of lower trust, or one* stating no figure that withdraws nothing. A corrected fact whose corrector- * the five-fact cap leaves out is dropped.+ * the five-fact cap leaves out is dropped. `correctedBy` carries the decisions+ * for facts the cap cut too, so a caller merging other reads of the same facts+ * (the entity facts) can drop or label them the same way.*/-export async function matchingSavedFacts(ctx: OperationContext, scope: { sourceId?: string; sourceIds?: string[]; minTrust?: TrustTier }, queryText: string, otherNames: string[] = []): Promise<Array<SavedFactMatch & CollapseColumns>> {+export async function matchingSavedFacts(ctx: OperationContext, scope: { sourceId?: string; sourceIds?: string[]; minTrust?: TrustTier }, queryText: string, otherNames: string[] = []): Promise<SavedFactMatches> {+ const none: SavedFactMatches = { facts: [], correctedBy: new Map() };// A query naming one of two declared names also matches facts saved under the other.if (otherNames.length) queryText += ` ${otherNames.join(' ')}`;const terms = [...new Set(queryText.toLowerCase().match(/[\p{L}\p{N}][\p{L}\p{N}'-]+/gu) ?? [])].filter(t => t.length >= 3 && !FACT_MATCH_STOPWORDS.has(t) && !QUESTION_FRAME_WORDS.has(t)).slice(0, 12);- if (terms.length === 0 || !ctx.emitResponseMeta) return [];+ if (terms.length === 0 || !ctx.emitResponseMeta) return none;const sources = scope.sourceIds?.length ? scope.sourceIds : [scope.sourceId ?? ctx.sourceId ?? 'default'];const [excludePrivate, supersede] = await Promise.all([resolveExcludePrivatePages(ctx.engine, ctx.remote), savedFactsSupersedeEnabled(ctx.engine)]);const active = `f.source_id = ANY($1::text[])@@ -173,17 +179,18 @@ export async function matchingSavedFacts(ctx: OperationContext, scope: { sourceI...admitted.filter(r => r.fact.corrected_by && correcting.has(r.fact.id)).sort((a, b) => b.at - a.at || b.fact.id - a.fact.id),...admitted.filter(r => r.fact.corrected_by && !correcting.has(r.fact.id))] : admitted;const kept = (await collapseFactReads(ctx.engine, ordered.map(r => r.fact))).slice(0, 5);- if (!supersede) return kept;+ if (!supersede) return { facts: kept, correctedBy: new Map() };// A label names the returned fact that stands for its corrector. A corrected fact whose corrector the cap or// the collapse left out is dropped rather than shown as current.- const correctedBy = new Map(ordered.map(r => [r.fact.id, r.fact.corrected_by]));+ const correctedBy = new Map(ordered.flatMap(r => r.fact.corrected_by === undefined ? [] : [[r.fact.id, r.fact.corrected_by] as const]));const shownAs = new Map(kept.flatMap(r => (r.collapsed_ids ?? [r.id]).map(id => [id, r.id] as const)));- return kept.flatMap(({ corrected_by, ...r }) => {+ const facts = kept.flatMap(({ corrected_by, ...r }) => {const by = (r.collapsed_ids ?? [r.id]).map(id => shownAs.get(correctedBy.get(id) ?? -1)).find(c => c !== undefined && c !== r.id);if (by !== undefined) return [{ ...r, corrected_by: by }];return corrected_by === undefined ? [r] : [];});+ return { facts, correctedBy };} catch {- return [];+ return none;}}diff --git a/test/search-saved-facts-match.test.ts b/test/search-saved-facts-match.test.tsindex ee13c95b02..4dc4d06810 100644--- a/test/search-saved-facts-match.test.ts+++ b/test/search-saved-facts-match.test.ts@@ -371,3 +371,65 @@ describe('search.saved_facts_supersede: what a correction must share', () => {}});});++// Value: protects=entity enrichment never shows a corrected claim as current (#7938 item 1); fails_when=the entity-facts copy of a claim the five-fact cap cut comes back unlabelled, beside its corrector or without it; why_new=the entity-copy test above keeps both facts inside the cap; seam=none; no_claim=fact rows the facts arm returns+describe('search.saved_facts_supersede: entity facts and the five-fact cap', () => {+ async function seedCapped(name: string, desks: number): Promise<{ query: string; old: number; fix: number }> {+ const slug = `companies/${name.toLowerCase()}`;+ await importFromContent(engine, slug, serializeMarkdown({}, `${name} is a customer account.`, '', { type: 'company', title: name, tags: [] }), { noEmbed: true, forceRechunk: true });+ const old = await remember(`${name} renews at 52 seats.`, '2025-08-01T09:00:00Z');+ const fix = await remember(`Correction: ${name} is at 46 seats, ignore the 52.`, '2025-08-02T09:00:00Z');+ const seeded = [old, fix];+ for (let i = 0; i < desks; i++) seeded.push(await remember(`${name} renewal seats reviewed by desk ${'abcdef'[i]}.`, '2025-08-03T09:00:00Z'));+ // Only the corrected claim is filed under the entity, so it alone comes back through the entity facts.+ await engine.executeRaw('UPDATE facts SET entity_slug = CASE WHEN id = $1 THEN $2 ELSE NULL END WHERE id = ANY($3::int[])', [old, slug, seeded]);+ return { query: `${name} renewal seats`, old, fix };+ }+ const savedFactsBlock = (text: string) => text.slice(text.indexOf('Saved facts (remember)'));++ test('a claim whose corrector the cap cut does not come back through the entity facts', async () => {+ await engine.setConfig(SAVED_FACTS_SUPERSEDE_KEY, 'true');+ try {+ const f = await seedCapped('Quarnetix', 5);+ const { facts, text } = await search(f.query);+ expect(facts.map(x => x.id)).not.toContain(f.fix);+ expect(facts.map(x => x.id)).not.toContain(f.old);+ expect(savedFactsBlock(text)).not.toContain('Quarnetix renews at 52 seats.');+ } finally {+ await engine.setConfig(SAVED_FACTS_SUPERSEDE_KEY, 'false');+ }+ });++ test('a claim the cap cut but the entity facts return is labelled with its returned corrector', async () => {+ await engine.setConfig(SAVED_FACTS_SUPERSEDE_KEY, 'true');+ try {+ const f = await seedCapped('Brisketon', 4);+ const { facts } = await search(f.query);+ expect(facts.map(x => x.id)).toContain(f.fix);+ expect(facts.find(x => x.id === f.old)?.corrected_by).toBe(f.fix);+ } finally {+ await engine.setConfig(SAVED_FACTS_SUPERSEDE_KEY, 'false');+ }+ });++ test('dropping an entity fact whose corrector the cap cut also drops the entity fact it corrects', async () => {+ await engine.setConfig(SAVED_FACTS_SUPERSEDE_KEY, 'true');+ try {+ await importFromContent(engine, 'companies/kelvanta', serializeMarkdown({}, 'Kelvanta is a customer account.', '', { type: 'company', title: 'Kelvanta', tags: [] }), { noEmbed: true, forceRechunk: true });+ const old = await remember('Kelvanta renews at 52 seats.', '2025-07-01T09:00:00Z');+ const fix = await remember('Correction: Kelvanta renewal seats are 46.', '2025-07-02T09:00:00Z');+ // Restates the old figure, so it corrects the correction but not the claim it corrected.+ const refix = await remember('Correction: Kelvanta seats are 52.', '2025-07-03T09:00:00Z');+ const desks = [];+ for (const desk of 'abcde') desks.push(await remember(`Kelvanta renewal seats reviewed by desk ${desk}.`, '2025-07-04T09:00:00Z'));+ await engine.executeRaw("UPDATE facts SET entity_slug = CASE WHEN id = ANY($1::int[]) THEN 'companies/kelvanta' ELSE NULL END WHERE id = ANY($2::int[])",+ [[old, fix], [old, fix, refix, ...desks]]);+ const { facts } = await search('Kelvanta renewal seats');+ const returned = new Set(facts.map(x => x.id));+ expect([old, fix, refix].filter(id => returned.has(id))).toEqual([]);+ for (const x of facts) if (x.corrected_by !== undefined) expect(returned.has(x.corrected_by), String(x.id)).toBe(true);+ } finally {+ await engine.setConfig(SAVED_FACTS_SUPERSEDE_KEY, 'false');+ }+ });+});