Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Keep a forgotten owned-memory fact retired, restore one copy of a claim at its narrowest visibility, and skip claims the fence states
garrytan4c49b5e980c7
diff --git a/docs/guides/repair.md b/docs/guides/repair.mdindex 83feec03e0..c5ec4aa105 100644--- a/docs/guides/repair.md+++ b/docs/guides/repair.md@@ -2171,7 +2171,7 @@ facts are left to [Extractor facts](#extractor-facts).| --- | --- | --- || `evidenced` | No fence of the page ever held the fact: no facts-fence generation recorded it, and neither the page nor any stored version of it carries it at the same row number, claim and visibility. | By `--apply --expect <hash>` || `ambiguous` | The page's version history has a gap (pruned versions, or an import that consumed several commits), or it has more stored versions than one preview reads, so an earlier fence holding the fact cannot be ruled out. | Only with `--include-ambiguous` and that preview's hash |-| `excluded:<reason>` | Page missing or deleted, superseded, withdrawn (`forget`), changed since the retirement, held by a fence (`fence_held`: an edit of the fence removed it, a genuine withdrawal), an active fact with the same text and entity already on the page at any visibility, an earlier candidate with the same text and entity, or an unmanaged brain (`unmanaged_reconcile`). | Never |+| `excluded:<reason>` | Page missing or deleted, superseded, withdrawn (`forget`, including a forget on the member or scope memory page it sits on), changed since the retirement, held by a fence (`fence_held`: an edit of the fence removed it, a genuine withdrawal), a claim the page's fence states (`fence_states_claim`), an active fact with the same text and entity already on the page at any visibility, another candidate with the same text and entity at the same or a narrower visibility (`duplicate_candidate`; a private copy comes back before a world one), or an unmanaged brain (`unmanaged_reconcile`). | Never |The preview binds each page's revision. If the page was written after thepreview, the apply restores nothing for it and reportsdiff --git a/src/core/repair/unwithdrawn-facts.ts b/src/core/repair/unwithdrawn-facts.tsindex c3f38b51d5..c123826e5c 100644--- a/src/core/repair/unwithdrawn-facts.ts+++ b/src/core/repair/unwithdrawn-facts.ts@@ -12,9 +12,11 @@* retirement left it: expired at that generation's instant, row number cleared, same claim and visibility, not* superseded, and not a conversation-extractor row (`repair extractor-facts` owns those). Each is classified:* - excluded (listed, never restored): the page is missing or deleted; the fact was superseded or withdrawn- * (`fact_withdrawals`); a fence held it (a fact_fence generation recorded it, or the page or a stored version of it- * carries it at the same row number, claim and visibility: a genuine withdrawal); an active fact with the same- * text and entity is on the page, at any visibility; another candidate with the same text is restored instead; or+ * (`fact_withdrawals`, for its entity, every subject or the owned memory page it sits on); a fence held it (a+ * fact_fence generation recorded it, or the page or a stored version of it carries it at the same row number, claim+ * and visibility: a genuine withdrawal); the page's fence states the claim; an active fact with the same text and+ * entity is on the page, at any visibility; another candidate with the same text and entity, at the same or a+ * narrower visibility, is restored instead; or* the brain is unmanaged, whose extract_facts reconcile retires a row its fence lacks as drift and would retire a* restored row again.* - ambiguous (restored only with `--include-ambiguous` and that preview's hash): the page's version history has a@@ -43,6 +45,7 @@ import { clearApprovedSet, loadApprovedSet, previewChangedError, previewHash, saimport { maintenanceTransaction } from '../persistence/attribution.ts';import { FACT_FENCE_KIND } from '../generations/fence.ts';import { factsFenceHeld, pageFenceRowKeys } from '../generations/fence-evidence.ts';+import { withdrawalCoversFactSql } from '../access/member-facts.ts';import type { FactPreimage } from '../generations/adapters/fact-rows.ts';import { afterCursor, type RepairHandler, type RepairItem, type RepairItemOutcome, type RepairListing, type RepairPlan, type RepairScope } from './core.ts';@@ -108,7 +111,7 @@ export async function classifyUnwithdrawnFacts(db: BrainEngine, sourceIds: strinp.id AS page_id, p.deleted_at IS NOT NULL AS page_deleted, p.version_history_gap_at IS NOT NULL AS history_gap,EXISTS (SELECT 1 FROM fact_withdrawals w WHERE w.source_id = f.source_id AND w.visibility = f.visibilityAND w.fact_hash IN (gbrain_fact_fingerprint(f.fact), gbrain_fact_fingerprint_v1(f.fact))- AND (w.subject = '*' OR w.subject = f.entity_slug)) AS withdrawn,+ AND ${withdrawalCoversFactSql('w', 'f')}) AS withdrawn,EXISTS (SELECT 1 FROM facts a WHERE a.source_id = f.source_id AND a.source_markdown_slug = r.slugAND a.expired_at IS NULL AND gbrain_fact_fingerprint(a.fact) = gbrain_fact_fingerprint(f.fact)AND a.entity_slug IS NOT DISTINCT FROM f.entity_slug) AS active_duplicate@@ -119,7 +122,7 @@ export async function classifyUnwithdrawnFacts(db: BrainEngine, sourceIds: strinAND COALESCE(f.source, '') NOT LIKE 'cli:extract-conversation-facts%'ORDER BY r.source_id, r.slug, f.id`, [sourceIds, opts.slug ?? null, FACT_FENCE_KIND]);// A fence that held the row (a generation, the page, a stored version) means the row was withdrawn by an edit of it.- const evidence = new Map<string, { held: Set<number>; complete: boolean }>();+ const evidence = new Map<string, { held: Set<number>; complete: boolean; claims: Set<string> }>();for (const row of rows) {const key = `${row.source_id}\u0000${row.slug}`;if (evidence.has(key) || row.page_id === null) continue;@@ -127,15 +130,22 @@ export async function classifyUnwithdrawnFacts(db: BrainEngine, sourceIds: strinconst [current] = await db.executeRaw<{ id: number; compiled_truth: string | null; timeline: string | null; body_storage: string | null }>(`SELECT id, CASE WHEN body_storage = 'segmented' THEN NULL ELSE compiled_truth END AS compiled_truth,CASE WHEN body_storage = 'segmented' THEN NULL ELSE timeline END AS timeline, body_storage FROM pages WHERE id = $1`, [row.page_id]);- evidence.set(key, await factsFenceHeld(db, { sourceId: row.source_id, slug: row.slug,- prior: current ? await pageFenceRowKeys(db, current, row.slug, row.source_id) : null,- rows: page.map(other => ({ id: Number(other.id), row_num: Number(other.preimage.row_num), fact: other.preimage.fact, visibility: other.preimage.visibility })) }));+ const prior = current ? await pageFenceRowKeys(db, current, row.slug, row.source_id) : null;+ const held = await factsFenceHeld(db, { sourceId: row.source_id, slug: row.slug, prior,+ rows: page.map(other => ({ id: Number(other.id), row_num: Number(other.preimage.row_num), fact: other.preimage.fact, visibility: other.preimage.visibility })) });+ evidence.set(key, { ...held, claims: new Set([...(prior ?? [])].map(rowKey => rowKey.split('\u0000')[1]!)) });}const managed = await managedPersistenceEnabled(db);const base = rows.map(row => classify(row, evidence.get(`${row.source_id}\u0000${row.slug}`), managed));- const key = (row: CandidateRow) => JSON.stringify([row.source_id, row.slug, row.visibility, row.entity_slug, row.fingerprint]);+ // One copy of a claim comes back per page and entity, the narrowest visibility first, so no wider copy returns beside it.+ const key = (row: CandidateRow) => JSON.stringify([row.source_id, row.slug, row.entity_slug, row.fingerprint]);+ const rank = (i: number) => (rows[i]!.visibility === 'world' ? 2 : 0) + (base[i]![0] === 'evidenced' ? 0 : 1);const representative = new Map<string, number>();- for (const klass of ['evidenced', 'ambiguous'] as const) rows.forEach((row, i) => { if (base[i]![0] === klass && !representative.has(key(row))) representative.set(key(row), i); });+ rows.forEach((row, i) => {+ if (base[i]![0] === 'excluded') return;+ const current = representative.get(key(row));+ if (current === undefined || rank(i) < rank(current)) representative.set(key(row), i);+ });return rows.map((row, i) => {const duplicate = base[i]![0] !== 'excluded' && representative.get(key(row)) !== i;const [klass, reason] = duplicate ? ['excluded' as const, 'duplicate_candidate'] : base[i]!;@@ -144,12 +154,13 @@ export async function classifyUnwithdrawnFacts(db: BrainEngine, sourceIds: strin});}-function classify(row: CandidateRow, evidence: { held: Set<number>; complete: boolean } | undefined, managed: boolean): [UnwithdrawnFactClass, string] {+function classify(row: CandidateRow, evidence: { held: Set<number>; complete: boolean; claims: Set<string> } | undefined, managed: boolean): [UnwithdrawnFactClass, string] {if (row.page_id === null || row.page_deleted || !evidence) return ['excluded', 'page_missing'];if (row.superseded) return ['excluded', 'superseded'];if (row.withdrawn) return ['excluded', 'withdrawn'];if (row.expired_at !== row.retired_at) return ['excluded', 'changed_since_retirement'];if (evidence.held.has(Number(row.id))) return ['excluded', 'fence_held'];+ if (evidence.claims.has(row.fact)) return ['excluded', 'fence_states_claim'];if (row.active_duplicate) return ['excluded', 'active_duplicate'];if (!managed) return ['excluded', 'unmanaged_reconcile'];if (row.history_gap) return ['ambiguous', 'version_history_gap'];@@ -163,6 +174,7 @@ const DETAILS: Record<string, string> = {withdrawn: 'its claim was withdrawn (forget)',changed_since_retirement: 'it changed after the generation retired it',fence_held: 'a fence of the page held it, so an edit of the fence withdrew it',+ fence_states_claim: "the page's fence states this claim, so its next write would retire a restored copy again",active_duplicate: 'an active fact with the same text and entity is already on the page',duplicate_candidate: 'another candidate of the page with the same text and entity is restored instead',unmanaged_reconcile: "this brain is unmanaged: its extract_facts reconcile treats a numbered row the page's fence lacks as drift and would retire a restored row again",diff --git a/test/repair-unwithdrawn-facts-8819.test.ts b/test/repair-unwithdrawn-facts-8819.test.tsindex ea62ca7eb7..616c29f560 100644--- a/test/repair-unwithdrawn-facts-8819.test.ts+++ b/test/repair-unwithdrawn-facts-8819.test.ts@@ -34,17 +34,17 @@ async function repair(engine: BrainEngine, sourceId: string, opts: { expect?: st}const hashOf = (result: RepairResult) => result.apply_command.match(/--expect ([0-9a-f]+)/)![1]!;-async function factRow(engine: BrainEngine, sourceId: string, rowNum: number, fact: string, source = 'mcp:put_page'): Promise<number> {+async function factRow(engine: BrainEngine, sourceId: string, rowNum: number, fact: string, source = 'mcp:put_page', page = SLUG): Promise<number> {const [row] = await engine.executeRaw<{ id: number }>(`INSERT INTO facts (source_id, entity_slug, fact, kind, visibility, source, row_num, source_markdown_slug)- VALUES ($1, $2, $3, 'fact', 'private', $4, $5, $2) RETURNING id`, [sourceId, SLUG, fact, source, rowNum]);+ VALUES ($1, $2, $3, 'fact', 'private', $4, $5, $6) RETURNING id`, [sourceId, SLUG, fact, source, rowNum, page]);return Number(row!.id);}/** What the pre-fix projection did on a page's first fact generation: record it, retiring `ids` with their before-states, and expire them. */-async function prefixRetire(engine: BrainEngine, sourceId: string, ids: number[]): Promise<void> {+async function prefixRetire(engine: BrainEngine, sourceId: string, ids: number[], slug = SLUG): Promise<void> {await engine.transaction(async tx => {- const page = (await tx.readPageSnapshot(SLUG, { sourceId }))!.page;- await publishFenceGeneration(tx, { kind: FACT_FENCE_KIND, sourceId, slug: SLUG, read: readFenceBlocks(page, 'facts'),+ const page = (await tx.readPageSnapshot(slug, { sourceId }))!.page;+ await publishFenceGeneration(tx, { kind: FACT_FENCE_KIND, sourceId, slug, read: readFenceBlocks(page, 'facts'),items: factFenceItems([{ row_num: 1, fact: 'Alice founded acme-example', visibility: 'private' }]),retire: (await factPreimages(tx, sourceId, ids)).map(factRetireItem), writer: 'projection' });await tx.executeRaw('UPDATE facts SET expired_at=now(), row_num=NULL WHERE source_id=$1 AND id=ANY($2::bigint[])', [sourceId, ids]);@@ -76,10 +76,10 @@ async function seed(s: ManagedSource): Promise<{ chess: number; lisbon: number;* `run` gets what it returned.*/async function managedPage<T>(databaseUrl: string | undefined, seed: (engine: BrainEngine, sourceId: string) => Promise<T>,- run: (engine: BrainEngine, sourceId: string, seeded: T) => Promise<void>, segmented = false): Promise<void> {+ run: (engine: BrainEngine, sourceId: string, seeded: T) => Promise<void>, segmented = false, fence = FOUNDED): Promise<void> {let seeded: T;await managedSource(databaseUrl, async s => {- await s.put(SLUG, PERSON('Alice Example', FACTS_FENCE(FOUNDED)));+ await s.put(SLUG, PERSON('Alice Example', FACTS_FENCE(fence)));await s.engine.executeRaw("DELETE FROM derived_generations WHERE source_id=$1 AND kind='fact_fence'", [s.sourceId]);await s.engine.executeRaw('DELETE FROM facts WHERE source_id=$1 AND source_markdown_slug=$2', [s.sourceId, SLUG]);if (segmented) await s.engine.executeRaw(`INSERT INTO page_versions (page_id, compiled_truth, timeline, body_storage, body_generation)@@ -272,4 +272,55 @@ describe('gbrain repair unwithdrawn-facts (#8819) coverage pass', () => {}}, 120_000);}++ for (const { backend, databaseUrl } of pathologicalArms()) {+ test(`[${backend}] a fact forgotten on its owned memory page stays retired`, async () => {+ const MEMBER = 'memory/members/alice-example';+ let ids: { chess: number; lisbon: number };+ await managedSource(databaseUrl, async s => {+ await s.put(MEMBER, PERSON('Alice Example', FACTS_FENCE(FOUNDED)));+ await s.engine.executeRaw("DELETE FROM derived_generations WHERE source_id=$1 AND kind='fact_fence'", [s.sourceId]);+ await s.engine.executeRaw('DELETE FROM facts WHERE source_id=$1 AND source_markdown_slug=$2', [s.sourceId, MEMBER]);+ await factRow(s.engine, s.sourceId, 1, 'Alice founded acme-example', 'chat', MEMBER);+ ids = { chess: await factRow(s.engine, s.sourceId, 2, 'Alice plays chess', 'mcp:put_page', MEMBER),+ lisbon: await factRow(s.engine, s.sourceId, 3, 'Alice lives in Lisbon', 'mcp:put_page', MEMBER) };+ await prefixRetire(s.engine, s.sourceId, [ids.chess, ids.lisbon], MEMBER);+ await s.engine.executeRaw(`INSERT INTO fact_withdrawals (source_id, visibility, subject, fact_hash)+ SELECT source_id, visibility, source_markdown_slug, gbrain_fact_fingerprint(fact) FROM facts WHERE id=$1`, [ids.chess]);+ }, async ({ engine, sourceId }) => {+ const preview = await repair(engine, sourceId);+ expect(preview.listing!.map(row => [row.item, row.class])).toEqual([+ [`${sourceId}:${MEMBER}#${ids.chess}`, 'excluded:withdrawn'], [`${sourceId}:${MEMBER}#${ids.lisbon}`, 'evidenced']]);+ expect((await repair(engine, sourceId, { expect: hashOf(preview) })).applied).toBe(1);+ expect(await state(engine, [ids.chess, ids.lisbon])).toEqual([[ids.chess, null, false], [ids.lisbon, 3, true]]);+ });+ }, 120_000);+ }++ for (const { backend, databaseUrl } of pathologicalArms()) {+ test(`[${backend}] private and world copies of one claim: only the private copy is restored`, async () => {+ await managedPage(databaseUrl, async (engine, sourceId) => {+ const ids = { world: await factRow(engine, sourceId, 2, 'Alice plays chess'), priv: await factRow(engine, sourceId, 3, 'Alice plays chess') };+ await engine.executeRaw("UPDATE facts SET visibility='world' WHERE id=$1", [ids.world]);+ await prefixRetire(engine, sourceId, [ids.world, ids.priv]);+ return ids;+ }, async (engine, sourceId, { world, priv }) => {+ const preview = await repair(engine, sourceId);+ expect(preview.listing!.map(row => [row.item, row.class])).toEqual([+ [`${sourceId}:${SLUG}#${world}`, 'excluded:duplicate_candidate'], [`${sourceId}:${SLUG}#${priv}`, 'evidenced']]);+ expect((await repair(engine, sourceId, { expect: hashOf(preview) })).applied).toBe(1);+ expect(await state(engine, [world, priv])).toEqual([[world, null, false], [priv, 3, true]]);+ });+ }, 120_000);+ }++ for (const { backend, databaseUrl } of pathologicalArms()) {+ test(`[${backend}] a claim the page's fence states at another row is not restored, since the next write would retire it again`, async () => {+ await managedPage(databaseUrl, retiredChess, async (engine, sourceId, chess) => {+ const preview = await repair(engine, sourceId);+ expect(preview.listing!.map(row => row.class)).toEqual(['excluded:fence_states_claim']);+ expect(await state(engine, [chess])).toEqual([[chess, null, false]]);+ }, false, [FOUNDED, ROW(4, 'Alice plays chess')].join('\n'));+ }, 120_000);+ }});