Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix: record the purge's fact expiry as written by maintenance

garrytand19ab85db454
diff --git a/docs/architecture/system-of-record.md b/docs/architecture/system-of-record.md
index c16e814c2d..c55b48cd41 100644
--- a/docs/architecture/system-of-record.md
+++ b/docs/architecture/system-of-record.md
@@ -260,6 +260,7 @@ or a physical projection that leaves attribution untouched):
- `src/core/facts/fence-fact-append.ts` (1): `prepareFenceFactAppend` inserts the facts row of a fence row the request's page file holds, in its caller's coordinated publication after the page row (remember and its callers).
- `src/core/facts/fence-write.ts` (1): the markdown-first fence reconcile insert runs in `maintenanceTransaction` (its page body mirror goes through `facts/file-body-guard.ts`).
- `src/core/facts/file-body-guard.ts` (2): the #6982 checked body mirror (`replaceStoredBody`: the page_versions row and the body refresh, under the page row lock) runs in its caller's `maintenanceTransaction` (fence write, unfenced-facts fence, phantom redirect, forget, proposal supersede).
+- `src/core/facts/gone-page-facts.ts` (2): a retention purge expires the purged pages' facts in its delete transaction, in `maintenanceTransaction` (the bulk purge and the per-tombstone purge of a source the belief ledger records; `purgeDeletedPagesCoordinated`).
- `src/core/facts/forget.ts` (4): the legacy fence expiry, withdrawal and strike-through each run in `maintenanceTransaction` (nested inside a caller's coordinated write, they keep its actor).
- `src/core/facts/ontology-rekey.ts` (1): B-10's duplicate-observation drop runs in its caller's transaction immediately before the caller's re-key: the rename's `maintenanceTransaction`, the `relink_facts` publication, the phantom redirect's fact move and `repair facts-rehome`'s move; every caller is attributed.
- `src/core/facts/proposal-supersede.ts` (4): unmanaged `decide` proposal accept and undo run in one `maintenanceTransaction` each; managed brains publish a `decide_proposal` request.
diff --git a/src/core/persistence/purge-deleted.ts b/src/core/persistence/purge-deleted.ts
index 081606f3ad..53ffc9ba07 100644
--- a/src/core/persistence/purge-deleted.ts
+++ b/src/core/persistence/purge-deleted.ts
@@ -19,6 +19,7 @@ import { clearPurgePark, isParked, notePurgeSourceWriter, PURGE_PARK_LIMIT, purg
import { ItemRun, type ItemFailure } from '../item-failure.ts';
import { readUnreviewedSyncTombstones, syncDeleteHeld } from './sync-delete-tombstones.ts';
import { expireGonePageFacts } from '../facts/gone-page-facts.ts';
+import { maintenanceTransaction } from './attribution.ts';
/** A tombstone that moved under a concurrent writer; the next run retries it. */
const DEFERRAL_CODES = new Set(['revision_conflict', 'page_not_found', 'page_identity_changed']);
@@ -127,7 +128,7 @@ async function expireRecordedTombstones(engine: BrainEngine, olderThanHours: num
}
try {
const publicationId = randomUUID(), report = { appended: 0 };
- const deleted = await engine.transaction(async tx => {
+ const deleted = await maintenanceTransaction(engine, async tx => {
const slugs = [...new Set([row.slug, ...await linkStreamSlugs(tx, row.source_id, row.id)])].sort();
await tx.lockPageKeys(slugs.map(slug => ({ sourceId: row.source_id, slug })));
return withBeliefBatch(tx, { sourceId: row.source_id, slugs, publicationId, op: 'purge_deleted_pages', hardDeleteEvent: 'page.expired', report },
@@ -298,7 +299,7 @@ export async function purgeDeletedPagesCoordinated(engine: BrainEngine, olderTha
const kept = opts.automatic ? await readUnreviewedSyncTombstones(engine, olderThanHours) : [];
const recorded = await expireRecordedTombstones(engine, olderThanHours, opts.retryParked === true, new Set(kept.map(row => row.id)));
const exclude = [...recorded.exclude, ...kept.map(row => row.id)];
- const result = await engine.purgeDeletedPages(olderThanHours, exclude.length ? { excludeIds: exclude } : undefined);
+ const result = await maintenanceTransaction(engine, tx => tx.purgeDeletedPages(olderThanHours, exclude.length ? { excludeIds: exclude } : undefined));
const held = kept.filter(row => row.expired);
return { slugs: [...recorded.slugs, ...result.slugs], count: recorded.slugs.length + result.count, blocked: [...recorded.blocked, ...held.map(row => syncDeleteHeld(row, olderThanHours))],
deferred: 0, failed: recorded.failed, inactive: 0, parked: recorded.parked, capacity_deferred: 0, held: held.length };
diff --git a/test/purge-expires-page-facts.test.ts b/test/purge-expires-page-facts.test.ts
index 103215d2e4..58ab5c8e4e 100644
--- a/test/purge-expires-page-facts.test.ts
+++ b/test/purge-expires-page-facts.test.ts
@@ -8,7 +8,7 @@
* purged page, and facts about other pages.
* 2. Fails when: a purge deletes the page row and leaves its facts with expired_at NULL (facts have no foreign key to
* pages), on the bulk purge (purgeDeletedPages) or the belief-ledger purge of a recorded source
- * (expireRecordedTombstones); or the fix deletes a fact row instead of expiring it. The managed purge, which already
+ * (expireRecordedTombstones); or the fix deletes a fact row instead of expiring it, or expires it with no write attribution. The managed purge, which already
* sweeps the facts filed on the page, is pinned alongside.
* 3. Real seams: writes and reads dispatched through MCP dispatch, remote reads with a minted token verified by the
* OAuth provider; a repo-less source, an unmanaged source the belief ledger records, and a managed brain; embeddings stubbed.
@@ -113,6 +113,8 @@ async function purgedPageScenario(engine: BrainEngine, source: string, purge: ()
...(opts.sweepsFiled ? [] : [{ fact: expect.stringContaining('canaryobservation'), expired: false }]),
];
expect(await facts()).toEqual(expected);
+ // The expiry is attributed to the maintenance principal, like every other fact write.
+ expect(await engine.executeRaw('SELECT fact FROM facts WHERE source_id = $1 AND expired_at IS NOT NULL AND last_write_principal_kind IS NULL', [source])).toEqual([]);
// The soft-deleted holder comes back with its fact about the purged page.
await write('restore_page', { slug: SHELVED, expected_revision: await shelvedRevision() });
expect((await facts()).find(f => f.fact.includes('canaryshelvedholder'))?.expired).toBe(false);
@@ -146,7 +148,7 @@ for (const kind of testBackends()) {
else { const pglite = new PGLiteEngine(); await pglite.connect({ engine: 'pglite' } as never); await pglite.initSchema(); engine = pglite; close = () => pglite.disconnect(); }
try {
await engine.executeRaw("INSERT INTO sources (id, name) VALUES ('source-a', 'source-a') ON CONFLICT (id) DO NOTHING");
- await purgedPageScenario(engine, 'source-a', async () => (await engine.purgeDeletedPages(72)).slugs, { remoteReader: true });
+ await purgedPageScenario(engine, 'source-a', async () => (await purgeDeletedPagesCoordinated(engine, 72)).slugs, { remoteReader: true });
} finally { await close(); }
}, 240_000);