Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
GBRA-161 PR 1b: atom_retirements_restorable defers ledger-covered atoms to the ledger
garrytan7f57ddb23d20
diff --git a/src/commands/doctor/checks/atom-retirements.ts b/src/commands/doctor/checks/atom-retirements.tsindex 180b71862b..913cb47af4 100644--- a/src/commands/doctor/checks/atom-retirements.ts+++ b/src/commands/doctor/checks/atom-retirements.ts@@ -3,7 +3,9 @@* `gbrain repair atoms-restore` can bring back, by the one rule the repair and the purge hold share* (`atomRetirementsSql`). A `WAVE_CHECKS` entry: `ok` when none; `warn` with the count, the origins, the* oldest and newest retirement, the date the purge hold of the earliest one ends, and the read-only preview as fix.- * The apply needs the user's agreement on the previewed set. Read-only.+ * The apply needs the user's agreement on the previewed set. Read-only. GBRA-161 PR 1b: an atom whose retirement a live+ * generation's ledger records is reported there (`generation_retirements`, `ledger_covered` here) and not counted+ * again, so each atom warns at most once; the preview, its JSON and the apply are unchanged.*/import type { BrainEngine } from '../../../core/engine.ts';import type { Check } from '../../doctor.ts';@@ -20,17 +22,20 @@ export async function atomRetirementsRestorableCheck(engine: BrainEngine, sourcecount(DISTINCT c.origin_kind||':'||c.origin)::int AS origins, min(c.retired_at) AS oldest, max(c.retired_at) AS newest,to_char((min(p.deleted_at) + interval '${ATOM_RESTORE_HOLD_DAYS} days') AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS"Z"') AS hold_untilFROM (${atomRetirementsSql('p.source_id=ANY($1::text[])')}) c JOIN pages p ON p.id=c.id- WHERE c.restorable GROUP BY p.source_id ORDER BY p.source_id`, [sources]);+ WHERE c.restorable AND c.ledger_generation IS NULL GROUP BY p.source_id ORDER BY p.source_id`, [sources]);+ const [covered] = await engine.executeRaw<{ count: number }>(+ `SELECT count(*)::int AS count FROM (${atomRetirementsSql('p.source_id=ANY($1::text[])')}) c WHERE c.restorable AND c.ledger_generation IS NOT NULL`, [sources]);const bySource = rows.map(row => ({ ...row, count: Number(row.count), origins: Number(row.origins) }));const count = bySource.reduce((sum, row) => sum + row.count, 0);const details = { count, origins: bySource.reduce((sum, row) => sum + row.origins, 0),oldest_retired_at: bySource.map(row => row.oldest).filter(Boolean).sort()[0] ?? null,newest_retired_at: bySource.map(row => row.newest).filter(Boolean).sort().at(-1) ?? null,hold_until: bySource.map(row => row.hold_until).sort()[0] ?? null,- source_ids: bySource.map(row => row.source_id), by_source: bySource, repair: 'atoms-restore', docs: DOCS };+ source_ids: bySource.map(row => row.source_id), by_source: bySource, ledger_covered: Number(covered?.count ?? 0), repair: 'atoms-restore', docs: DOCS };if (!count) {return { name: 'atom_retirements_restorable', status: 'ok', details,- message: 'No atom retired by an empty re-extraction is waiting to be restored.' };+ message: details.ledger_covered ? `No legacy atom retirement is waiting to be restored; ${details.ledger_covered} retirement(s) the generation ledger records are reported by generation_retirements.`+ : 'No atom retired by an empty re-extraction is waiting to be restored.' };}const first = bySource[0]!;const preview = ['gbrain', 'repair', 'atoms-restore', '--source', first.source_id, '--json'];diff --git a/src/core/persistence/atom-retirement-hold.ts b/src/core/persistence/atom-retirement-hold.tsindex dcecc104c5..bd72e5fbb0 100644--- a/src/core/persistence/atom-retirement-hold.ts+++ b/src/core/persistence/atom-retirement-hold.ts@@ -11,7 +11,9 @@* generation replaced it;* - `latest`: its hash is the hash of the origin's most recently retired `managed-reextract` atom, so an older* generation that a later extraction replaced is never revived.- * `restorable` is `origin_live AND NOT replaced AND latest`. The query is set-based (each CTE reads `pages` once,+ * `restorable` is `origin_live AND NOT replaced AND latest`. GBRA-161 PR 1b: `ledger_generation` is the live atom generation+ * whose ledger records the retirement (null for a legacy stamp alone); doctor reports a covered atom once, through the+ * ledger (`generation_retirements`), and `atoms-restore` still restores it on a managed brain. The query is set-based (each CTE reads `pages` once,* joined on source and origin), so its cost grows linearly with the atoms in scope.** The purge (engine bulk delete and preview, the belief-ledger expiry, the managed selection) holds a restorable@@ -33,8 +35,8 @@ const notImported = (alias: string) => `NULLIF(${alias}.frontmatter->>'imported_/*** SQL: every re-extraction retirement whose page `p` satisfies `scope` (a predicate over the `pages` alias `p`; it* also narrows the live atoms the replacement check reads, so scope by source or by origin, never by atom), with- * columns id, source_id, slug, origin_kind, origin, origin_page_id, hash, retired_at, origin_live, replaced, latest- * and restorable.+ * columns id, source_id, slug, origin_kind, origin, origin_page_id, hash, retired_at, origin_live, replaced, latest,+ * restorable and ledger_generation.*/export function atomRetirementsSql(scope = 'true'): string {return `WITH retired AS (@@ -58,7 +60,9 @@ export function atomRetirementsSql(scope = 'true'): string {(lv.hashes IS NOT NULL AND NOT (lv.hashes <@ ARRAY[r.hash])) AS replaced,COALESCE(l.hash = r.hash, false) AS latest,(r.okey IS NOT NULL AND (r.origin_slug IS NULL OR o.id IS NOT NULL) AND NOT COALESCE(lv.hashes IS NOT NULL AND NOT (lv.hashes <@ ARRAY[r.hash]), false)- AND COALESCE(l.hash = r.hash, false)) AS restorable+ AND COALESCE(l.hash = r.hash, false)) AS restorable,+ (SELECT g.id FROM derived_generation_items i JOIN derived_generations g ON g.id=i.generation_id+ WHERE g.source_id=r.source_id AND g.kind='atom' AND g.state='live' AND i.item_key=r.slug AND i.action='retired' ORDER BY g.id DESC LIMIT 1) AS ledger_generationFROM retired rLEFT JOIN pages o ON o.source_id=r.source_id AND o.slug=r.origin_slug AND o.deleted_at IS NULLLEFT JOIN live lv ON lv.source_id=r.source_id AND lv.okey=r.okey