Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(search): a corrected saved fact follows its corrector in the reply (#7938)
garrytanbc6abeb95c7e
diff --git a/src/core/ops/search.ts b/src/core/ops/search.tsindex 57a2a84bc5..aba58b6d97 100644--- a/src/core/ops/search.ts+++ b/src/core/ops/search.ts@@ -459,10 +459,21 @@ async function buildRetrievalResponseMeta(}// #8042: a lookup failure withholds saved_facts (degraded stage below) rather than return third-party text unquoted.const quotedFacts = await quoteFacts(ctx.engine, visible, ctx.remote !== false).catch(() => null);- const savedFacts = (quotedFacts ?? []).map(f => {+ const labelled = (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 });});+ // A corrected fact follows its corrector, as the matcher orders them: the reply's "corrected by a newer saved fact+ // above" stays true, and its budget, which cuts from the end, never keeps a corrected fact without its corrector.+ const savedFacts: typeof labelled = [];+ const placed = new Set(factRowIds);+ const correctorOf = (f: object) => (f as { corrected_by?: number }).corrected_by;+ for (let rest = labelled; rest.length;) {+ const ready = rest.filter(f => correctorOf(f) === undefined || placed.has(correctorOf(f)!));+ const batch = ready.length ? ready : rest;+ for (const f of batch) { savedFacts.push(f); placed.add(f.id); }+ rest = rest.filter(f => !batch.includes(f));+ }const heldTotal = [...heldBySource.values()].reduce((sum, n) => sum + n, 0);const embedding = embeddingHeld?.total ? embeddingHeld : null;const coverage: RetrievalCoverage | undefined = pending || heldTotal || embedding ? {diff --git a/test/search-saved-facts-match.test.ts b/test/search-saved-facts-match.test.tsindex 4dc4d06810..b544e5032f 100644--- a/test/search-saved-facts-match.test.ts+++ b/test/search-saved-facts-match.test.ts@@ -433,3 +433,25 @@ describe('search.saved_facts_supersede: entity facts and the five-fact cap', ()}});});++// Value: protects=a corrected fact always follows its correction in the reply (#7938 item 12); fails_when=a labelled entity fact sits above its corrector, so "corrected by a newer saved fact above" is false and the reply budget, which cuts from the end, can keep the label without the corrector; why_new=entity facts lead the list and no test placed a labelled entity fact; seam=none; no_claim=the order of facts nothing corrects+describe('search.saved_facts_supersede: a corrected fact follows its correction', () => {+ test('a labelled entity fact comes after the matched fact correcting it, in _meta and in the reply text', async () => {+ await engine.setConfig(SAVED_FACTS_SUPERSEDE_KEY, 'true');+ try {+ await importFromContent(engine, 'companies/ostravel', serializeMarkdown({}, 'Ostravel is a customer account.', '', { type: 'company', title: 'Ostravel', tags: [] }), { noEmbed: true, forceRechunk: true });+ const old = await remember('Ostravel renews at 52 seats.', '2025-06-01T09:00:00Z');+ const fix = await remember('Correction: Ostravel is at 46 seats, ignore the 52.', '2025-06-02T09:00:00Z');+ await engine.executeRaw("UPDATE facts SET entity_slug = CASE WHEN id = $1 THEN 'companies/ostravel' ELSE NULL END WHERE id = ANY($2::int[])", [old, [old, fix]]);+ const { facts, text } = await search('Ostravel renewal seats');+ expect(facts.find(x => x.id === old)?.corrected_by).toBe(fix);+ expect(order(facts, fix)).toBeGreaterThanOrEqual(0);+ expect(order(facts, fix)).toBeLessThan(order(facts, old));+ const block = text.slice(text.indexOf('Saved facts (remember)'));+ expect(block.indexOf('Correction: Ostravel is at 46 seats')).toBeGreaterThan(0);+ expect(block.indexOf('Correction: Ostravel is at 46 seats')).toBeLessThan(block.indexOf('Ostravel renews at 52 seats.'));+ } finally {+ await engine.setConfig(SAVED_FACTS_SUPERSEDE_KEY, 'false');+ }+ });+});