Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix: restore the still valid copy of a claim before one whose validity had lapsed
garrytane8b315073239
diff --git a/src/core/repair/unwithdrawn-facts.ts b/src/core/repair/unwithdrawn-facts.tsindex c68f77e56b..b070653932 100644--- a/src/core/repair/unwithdrawn-facts.ts+++ b/src/core/repair/unwithdrawn-facts.ts@@ -141,10 +141,12 @@ export async function classifyUnwithdrawnFacts(db: BrainEngine, sourceIds: strin}const managed = await managedPersistenceEnabled(db);const base = rows.map(row => classify(row, evidence.get(`${row.source_id}\u0000${row.slug}`), managed));- // One copy of a claim comes back per page, entity and speaker, the narrowest visibility first, so no wider copy returns- // beside it; copies from different known speakers are not duplicates (facts/attribution.ts), so each comes back.+ // One copy of a claim comes back per page, entity and speaker, the narrowest visibility first, then a still valid copy+ // before one whose validity had lapsed, so no wider copy returns beside it; copies from different known speakers are+ // not duplicates (facts/attribution.ts), so each comes back.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 lapsed = (i: number) => rows[i]!.preimage.valid_until != null && Date.parse(rows[i]!.preimage.valid_until!) <= Date.now();+ const rank = (i: number) => (rows[i]!.visibility === 'world' ? 4 : 0) + (lapsed(i) ? 2 : 0) + (base[i]![0] === 'evidenced' ? 0 : 1);const chosen = new Map<string, number[]>();const order = rows.map((_, i) => i).filter(i => base[i]![0] !== 'excluded').sort((a, b) => rank(a) - rank(b) || a - b);const restored = new Set<number>();diff --git a/test/repair-unwithdrawn-facts-8819.test.ts b/test/repair-unwithdrawn-facts-8819.test.tsindex 8a99cc32c3..033a634156 100644--- a/test/repair-unwithdrawn-facts-8819.test.ts+++ b/test/repair-unwithdrawn-facts-8819.test.ts@@ -362,4 +362,21 @@ describe('gbrain repair unwithdrawn-facts (#8819) coverage pass', () => {});}, 120_000);}++ for (const { backend, databaseUrl } of pathologicalArms()) {+ test(`[${backend}] of two copies of one claim, the still valid copy is restored, not the one whose validity had lapsed`, async () => {+ await managedPage(databaseUrl, async (engine, sourceId) => {+ const ids = { lapsed: await factRow(engine, sourceId, 2, 'Alice plays chess'), valid: await factRow(engine, sourceId, 3, 'Alice plays chess') };+ await engine.executeRaw("UPDATE facts SET valid_until='2020-01-01' WHERE id=$1", [ids.lapsed]);+ await prefixRetire(engine, sourceId, [ids.lapsed, ids.valid]);+ return ids;+ }, async (engine, sourceId, { lapsed, valid }) => {+ const preview = await repair(engine, sourceId);+ expect(preview.listing!.map(row => [row.item, row.class])).toEqual([+ [`${sourceId}:${SLUG}#${lapsed}`, 'excluded:duplicate_candidate'], [`${sourceId}:${SLUG}#${valid}`, 'evidenced']]);+ expect((await repair(engine, sourceId, { expect: hashOf(preview) })).applied).toBe(1);+ expect(await state(engine, [lapsed, valid])).toEqual([[lapsed, null, false], [valid, 3, true]]);+ });+ }, 120_000);+ }});