Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Merge remote-tracking branch 'origin/capy/belief-ledger-w1' into capy/belief-w1-purge
garrytan6ae3b1a52273
diff --cc src/core/belief/backfill.tsindex b82e153357,db82581d2a..c4434286ba--- a/src/core/belief/backfill.ts+++ b/src/core/belief/backfill.ts@@@ -16,13 -16,12 +16,16 @@@* `purge_epoch` it read; the append runs under the page key, refuses when the* epoch moved (the page is prepared again from current, redacted versions),* and rechecks tombstones through `appendBeliefEvents`. Events of a key a- * purge hid through derivation (`derived.hidden`) are appended hidden.+ * purge hid through derivation (`derived.hidden`) are appended hidden, and a+ * carrying field a purge registered from a retained version that quoted the+ * claim (`registerVersionCarriers`) is appended as the redaction marker. A+ * version whose own frontmatter is private publishes private events, whatever+ * the page's visibility is now.- * Publication ids are derived from the page lifetime and version, so a rerun- * appends nothing twice.+ * Publication ids are derived from the page lifetime and the version row; a+ * lifetime that already has its summary is skipped, so a rerun appends+ * nothing even after versions were pruned. The lifetime is resolved under the+ * page key, and the append refuses when the page was renamed or replaced+ * since preparation.*/import type { BrainEngine } from '../engine.ts';import { parseFactsFence } from '../facts-fence.ts';@@@ -39,9 -38,9 +42,9 @@@ import { coverageIntervals } from './coexport interface BackfillPageResult { slug: string; versions: number; events: number; complete: boolean; reasons: string[] }export interface BackfillReport { source_id: string; since: string | null; pages: BackfillPageResult[]; events: number; retried: number }-interface Body { revision: string | null; source_time: string; text: string | null }+interface Body { revision: string | null; source_time: string; text: string | null; private: boolean }interface Keyed { kind: 'fact' | 'take'; key: string; value: Record<string, unknown>; vhash: string; visibility: string; claimFp: string; validFrom: string | null; validUntil: string | null }- interface Prepared { epoch: number; incarnation: string; lifetime: string; pageId: number; versions: Body[]; drafts: BeliefEventDraft[][]; summary: BeliefEventDraft; complete: boolean; reasons: string[] }+ interface Prepared { epoch: number; incarnation: string; lifetime: string; pageId: number; versions: Body[]; drafts: BeliefEventDraft[][]; publications: string[]; summary: BeliefEventDraft; complete: boolean; reasons: string[] }const MAX_EPOCH_RETRIES = 3;const toUuid = (hex: string) => `${hex.slice(0, 8)}-${hex.slice(8, 12)}-4${hex.slice(13, 16)}-a${hex.slice(17, 20)}-${hex.slice(20, 32)}`;@@@ -96,10 -95,12 +99,13 @@@ async function preparePage(engine: Braip.body_storage = 'segmented' AS large, p.compiled_truth, p.timeline, (${privatePagesFilterFragment('p')}) AS visibleFROM pages p WHERE p.source_id = $1 AND p.slug = $2`, [sourceId, slug]);if (!page) return null;- const lifetime = (await resolveLifetimes(engine, [{ sourceId, incarnation: source.incarnation, slug, pageId: Number(page.id) }])).get(Number(page.id))!;+ const lifetime = await engine.transaction(async tx => {+ await tx.lockPageKeys([{ sourceId, slug }]);+ return (await resolveLifetimes(tx, [{ sourceId, incarnation: source.incarnation, slug, pageId: Number(page.id) }])).get(Number(page.id))!;+ });- const versions = await engine.executeRaw<{ id: number; snapshot_at: string; revision: string | null; compiled_truth: string | null; timeline: string | null; segmented: boolean }>(`SELECT v.id,- v.snapshot_at, v.knowledge_revision::text AS revision, v.compiled_truth, v.timeline, v.body_storage = 'segmented' AS segmented+ const versions = await engine.executeRaw<{ id: number; snapshot_at: string; revision: string | null; compiled_truth: string | null; timeline: string | null; segmented: boolean; private: boolean }>(`SELECT v.id,+ v.snapshot_at, v.knowledge_revision::text AS revision, v.compiled_truth, v.timeline, v.body_storage = 'segmented' AS segmented,+ (COALESCE(v.frontmatter->>'visibility', '') = 'private' OR COALESCE(v.frontmatter ? 'pinned_question', false)) AS privateFROM page_versions v WHERE v.page_id = $1 ORDER BY v.snapshot_at, v.id`, [page.id]);if (new Date(page.created_at).getTime() > Date.parse(since)) return null;const before = versions.filter(v => new Date(v.snapshot_at).getTime() <= Date.parse(since));