Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(repair): extractor-facts dedup is per entity

garrytanc4c51946ba7d
diff --git a/src/core/repair/extractor-facts.ts b/src/core/repair/extractor-facts.ts
index 9ca5277594..6c90710a35 100644
--- a/src/core/repair/extractor-facts.ts
+++ b/src/core/repair/extractor-facts.ts
@@ -13,9 +13,9 @@
* classified, in this order:
* - excluded (listed, never restored): the page is missing or deleted; the
* fact was superseded (`superseded_by`, E-T5); its claim was withdrawn
- * (`fact_withdrawals`); an active fact with the same text already sits on
- * the page (a fence row or another extractor row); or an earlier candidate
- * of the page carries the same text.
+ * (`fact_withdrawals`); an active fact with the same text and entity already
+ * sits on the page (a fence row or another extractor row); or an earlier
+ * candidate of the page carries the same text and entity.
* - ambiguous (restored only with `--include-ambiguous` and the hash of that
* preview): an unmanaged brain (no receipts); no committed write receipt of
* the page completed at the exact expiry instant (`completed_at =
@@ -142,7 +142,8 @@ export async function classifyExtractorFacts(db: BrainEngine, sourceIds: string[
AND w.fact_hash IN (gbrain_fact_fingerprint(c.fact), gbrain_fact_fingerprint_v1(c.fact))
AND (w.subject='*' OR w.subject=c.entity_slug)) AS withdrawn,
EXISTS (SELECT 1 FROM facts a WHERE a.source_id=c.source_id AND a.visibility=c.visibility
- AND gbrain_fact_fingerprint(a.fact)=c.fingerprint AND a.source_markdown_slug=c.slug AND a.expired_at IS NULL) AS active_duplicate,
+ AND gbrain_fact_fingerprint(a.fact)=c.fingerprint AND a.source_markdown_slug=c.slug AND a.expired_at IS NULL
+ AND a.entity_slug IS NOT DISTINCT FROM c.entity_slug) AS active_duplicate,
fenced.slug IS NOT NULL AS fence_history,
r.request_id AS receipt, r.consumer_version AS receipt_consumer
FROM cand c
@@ -153,7 +154,7 @@ export async function classifyExtractorFacts(db: BrainEngine, sourceIds: string[
const seen = new Set<string>();
return rows.map(row => {
const id = Number(row.id);
- const key = JSON.stringify([row.source_id, row.slug, row.visibility, row.fingerprint]);
+ const key = JSON.stringify([row.source_id, row.slug, row.visibility, row.entity_slug, row.fingerprint]);
const [klass, reason] = classify(row, seen.has(key), opts.managed);
if (klass !== 'excluded') seen.add(key);
return { id, source_id: row.source_id, slug: row.slug, class: klass, reason, expired_at: row.expired_at,
@@ -180,8 +181,8 @@ const DETAILS: Record<string, string> = {
page_missing: 'its conversation page is missing or deleted',
superseded: 'a newer fact superseded it',
withdrawn: 'its claim was withdrawn (forget)',
- active_duplicate: 'an active fact with the same text is already on the page',
- duplicate_candidate: 'an earlier candidate of the page has the same text',
+ active_duplicate: 'an active fact with the same text and entity is already on the page',
+ duplicate_candidate: 'an earlier candidate of the page has the same text and entity',
unmanaged_brain: 'unmanaged brain: no write receipt can prove the expiry',
no_same_transaction_receipt: 'no committed write of the page completed at the exact expiry instant',
post_fix_consumer: 'expired by a write published by a fixed consumer (a fence row may have taken its position)',
diff --git a/test/repair-extractor-facts.test.ts b/test/repair-extractor-facts.test.ts
index 3502822075..528814722a 100644
--- a/test/repair-extractor-facts.test.ts
+++ b/test/repair-extractor-facts.test.ts
@@ -394,10 +394,14 @@ for (const backend of testBackends()) {
await engine.executeRaw('INSERT INTO sources(id,name) VALUES($1,$1)', [sourceId]);
const ids = await conversation(engine, 'conversations/unmanaged', ['Alice sends the deck', 'Alice books the venue'], undefined, sourceId);
const superseded = (await conversation(engine, 'conversations/unmanaged-2', ['Alice hires a designer'], undefined, sourceId))[0];
+ // The same claim about another entity on the same page is a distinct fact, not a duplicate (Codex review).
+ const [{ id: bobs }] = await engine.executeRaw<{ id: number }>(`INSERT INTO facts (source_id, entity_slug, fact, kind, visibility, source, row_num, source_markdown_slug)
+ VALUES ($1, 'people/bob-example', 'Alice sends the deck', 'commitment', 'private', $2, 3, 'conversations/unmanaged') RETURNING id::int AS id`, [sourceId, EXTRACTOR]);
+ ids.push(Number(bobs));
await engine.executeRaw("UPDATE facts SET expired_at=now(), row_num=NULL WHERE source_id=$1", [sourceId]);
await engine.executeRaw('UPDATE facts SET superseded_by=$1 WHERE id=$2', [ids[0], superseded]);
const preview = await repair(engine, home, ['--source', sourceId]);
- expect(preview.results[0].residuals).toEqual({ evidenced: 0, ambiguous: 2, excluded: 1 });
+ expect(preview.results[0].residuals).toEqual({ evidenced: 0, ambiguous: 3, excluded: 1 });
expect(preview.results[0].affected).toBe(0);
expect(preview.results[0].listing!.map(entry => entry.detail)).toContain('unmanaged brain: no write receipt can prove the expiry');
expect((await refusal(() => repair(engine, home, ['--source', sourceId, '--apply', '--expect', hashOf(preview)]))).code).toBe('preview_changed');
@@ -408,7 +412,8 @@ for (const backend of testBackends()) {
const applied = await repair(engine, home, ['--source', sourceId, '--include-ambiguous', '--apply', '--expect', hashOf(wide)]);
expect(applied.results[0].outcomes).toEqual({ restored: 1 });
expect(await factState(engine, [...ids, superseded])).toEqual([
- { id: ids[0], active: true, row_num: 1 }, { id: ids[1], active: true, row_num: 2 }, { id: superseded, active: false, row_num: null }]);
+ { id: ids[0], active: true, row_num: 1 }, { id: ids[1], active: true, row_num: 2 }, { id: superseded, active: false, row_num: null },
+ { id: ids[2], active: true, row_num: 3 }]);
expect((await repair(engine, home, ['--source', sourceId])).results[0].residuals).toEqual({ evidenced: 0, ambiguous: 0, excluded: 1 });
}, 60_000);
});