Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

GBRA-161 PR 4: write-attribution and canonical-writer inventories, purge SQL golden (#7065)

garrytan60d9f8e4260b
diff --git a/docs/architecture/canonical-writers.tsv b/docs/architecture/canonical-writers.tsv
index 717bbd6d1b..6fc00fc40d 100644
--- a/docs/architecture/canonical-writers.tsv
+++ b/docs/architecture/canonical-writers.tsv
@@ -6,6 +6,7 @@ src/core/persistence/derived-retirement-hold.ts 2 contract retireDerivedPages ve
src/core/generations/adapters/page.ts 1 contract GBRA-161: importPageItem writes a derived page item through importFromContent inside the generation activation's maintenanceTransaction (publish.ts), behind the canonical table trigger; managed brains keep the coordinator path.
src/core/generations/adapters/atoms.ts 1 contract GBRA-161: the unmanaged atom completion flip (source_hash) runs in the generation activation's maintenanceTransaction after every atom and provenance edge was written and before the items are rechecked live; managed brains keep the coordinator path.
src/core/generations/adapters/chronicle-events.ts 4 contract GBRA-161 PR 3: the Life Chronicle event adapter writes event pages and their depth timeline projection rows, and removes a retired event's projection row, only inside publishGeneration's maintenanceTransaction or the coordinated managed_generation_activate/_restore request, under the depth and event page keys; managed event pages keep the managed_maintenance_chronicle_event request.
+src/core/generations/managed-retire.ts 1 coordinator GBRA-161 PR 4: a managed synthesis generation retires each matched earlier output through one managed_generation_retire request, whose preparer runs the versioned page delete and stamps retired_by/retired_at inside the coordinated write, refusing unless the item is pending on the live generation its activation made
src/core/persistence/links-maintenance.ts 1 coordinator Managed extract --stale and post-sync extraction add only the coordinator's unrecorded canonical timeline tuples inside a revision-bound withCoordinatedWrite transaction that also replaces the page's derived links and stamps its watermark.
src/core/extract-takes-from-pages.ts 0 coordinator Managed bootstrap submits one revision-checked put_page per source page so its complete takes fence and projected rows commit together; unmanaged writes retain the md-first helper.
src/core/persistence/origin-guard.ts 2 engine_guard #6948 bindOrigin records a page origin (and drops an origin_twin marker) only after assertOriginUnclaimed holds the origin advisory lock inside the caller's engine.transaction(); callers are the guarded publication applies and repairs.
diff --git a/docs/architecture/system-of-record.md b/docs/architecture/system-of-record.md
index 486637ca2a..ebfe58960d 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/write-single.ts` (2): the legacy single-fact insert and its supersession link each run in `maintenanceTransaction`; managed brains publish through the fact request.
- `src/core/generations/adapters/atoms.ts` (1): the unmanaged atom completion stamp (`source_hash`) runs inside the generation activation's `maintenanceTransaction` (`publish.ts`, GBRA-161).
- `src/core/generations/adapters/chronicle-events.ts` (3): Life Chronicle event writes, their timeline projection rows and the retirement of dropped events run inside the generation activation's `maintenanceTransaction` (`publish.ts`) on unmanaged brains and inside the coordinated `managed_generation_activate` / `managed_generation_restore` requests on managed brains (GBRA-161 PR 3); a restore runs in the restore runner's (`repair.ts`).
+- `src/core/generations/managed-retire.ts` (1): the retirement stamp (`retired_by`, `retired_at`) of one synthesis output a managed generation retires runs inside its `managed_generation_retire` request's coordinated write, after the versioned page delete (GBRA-161 PR 4).
- `src/core/import-file.ts` (12): direct markdown, code and image imports, the moved-file rename (`updateSlug`) and the #3694 legacy-hash re-stamp run in `maintenanceTransaction`; managed imports go through the page request preparer.
- `src/core/minions/handlers/ingest-capture.ts` (1): the capture ingest tombstone soft delete runs in `maintenanceTransaction`.
- `src/core/ops/extraction.ts` (2): extraction review promote (frontmatter) and reject (soft delete) each run in `maintenanceTransaction`.
diff --git a/test/fixtures/goldens/sql-text/pages.json b/test/fixtures/goldens/sql-text/pages.json
index 2d48b61ecf..f8d1bb03dc 100644
--- a/test/fixtures/goldens/sql-text/pages.json
+++ b/test/fixtures/goldens/sql-text/pages.json
@@ -789,8 +789,8 @@
"params": [
"number"
],
- "sql": "DELETE FROM pages WHERE deleted_at IS NOT NULL AND deleted_at < now() - ($1 || ' hours')::interval -- #6729: the old page of an open rename-identity downgrade waits for its disposition (identityDowngradePurgeHold). AND NOT EXISTS (SELECT 1 FROM op_checkpoints dg WHERE dg.op = 'sync-identity-downgrade' AND dg.fingerprint = pages.id::text AND dg.completed_keys->0->>'disposition' IS NULL AND pages.deleted_at = (dg.completed_keys->0->>'old_deleted_at')::timestamptz) -- #7013: a restorable re-extraction retirement waits out its hold (atomRetirementPurgeHold). AND NOT (pages.type='atom' AND pages.frontmatter->>'retired_by'='managed-reextract' AND pages.deleted_at > now() - interval '30 days' AND pages.id IN (SELECT held.id FROM (WITH retired AS ( SELECT p.id, p.source_id, p.slug, CASE WHEN NULLIF(p.frontmatter->>'source_slug','') IS NOT NULL THEN 'slug:'||(p.frontmatter->>'source_slug') WHEN NULLIF(p.frontmatter->>'source_path','') IS NOT NULL THEN 'path:'||(p.frontmatter->>'source_path') END AS okey, NULLIF(p.frontmatter->>'source_slug','') AS origin_slug, COALESCE(NULLIF(p.frontmatter->>'source_slug',''), p.frontmatter->>'source_path') AS origin, regexp_replace(COALESCE(p.frontmatter->>'source_hash',''),'^pending:','') AS hash, p.frontmatter->>'retired_at' AS retired_at FROM pages p WHERE p.type='atom' AND p.deleted_at IS NOT NULL AND p.frontmatter->>'retired_by'='managed-reextract' AND NULLIF(p.frontmatter->>'imported_from','') IS NULL AND (true) ), latest AS ( SELECT DISTINCT ON (source_id, okey) source_id, okey, hash FROM retired WHERE okey IS NOT NULL ORDER BY source_id, okey, retired_at DESC NULLS LAST, id DESC ), live AS ( SELECT p.source_id, CASE WHEN NULLIF(p.frontmatter->>'source_slug','') IS NOT NULL THEN 'slug:'||(p.frontmatter->>'source_slug') WHEN NULLIF(p.frontmatter->>'source_path','') IS NOT NULL THEN 'path:'||(p.frontmatter->>'source_path') END AS okey, array_agg(DISTINCT regexp_replace(COALESCE(p.frontmatter->>'source_hash',''),'^pending:','')) AS hashes FROM pages p WHERE p.type='atom' AND p.deleted_at IS NULL AND NULLIF(p.frontmatter->>'imported_from','') IS NULL AND (true) GROUP BY 1, 2 ) SELECT r.id, r.source_id, r.slug, CASE WHEN r.origin_slug IS NOT NULL THEN 'page' ELSE 'transcript' END AS origin_kind, r.origin, o.id AS origin_page_id, r.hash, r.retired_at, (r.okey IS NOT NULL AND (r.origin_slug IS NULL OR o.id IS NOT NULL)) AS origin_live, (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, (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_generation FROM retired r LEFT JOIN pages o ON o.source_id=r.source_id AND o.slug=r.origin_slug AND o.deleted_at IS NULL LEFT JOIN live lv ON lv.source_id=r.source_id AND lv.okey=r.okey LEFT JOIN latest l ON l.source_id=r.source_id AND l.okey=r.okey) held WHERE held.restorable)) -- GBRA-161: every derived retirement waits out its hold (derivedRetirementPurgeHold). AND NOT (COALESCE(pages.frontmatter->>'retired_by','') IN ('extract-atoms','managed-reextract','stale-atoms','life-chronicle','generation-restore') AND pages.deleted_at > now() - interval '1 day' * (SELECT COALESCE(MAX(CASE WHEN c.value ~ '^[0-9]{1,3}$' THEN CASE WHEN c.value::int BETWEEN 3 AND 365 THEN c.value::int END END), 30) FROM config c WHERE c.key = 'generations.restore_days')) RETURNING slug, source_id",
- "textSha256": "b23c99bbcf6f35b181a39bf1c0b1e5d90c95c1965323861db55f59d60f63caf0"
+ "sql": "DELETE FROM pages WHERE deleted_at IS NOT NULL AND deleted_at < now() - ($1 || ' hours')::interval -- #6729: the old page of an open rename-identity downgrade waits for its disposition (identityDowngradePurgeHold). AND NOT EXISTS (SELECT 1 FROM op_checkpoints dg WHERE dg.op = 'sync-identity-downgrade' AND dg.fingerprint = pages.id::text AND dg.completed_keys->0->>'disposition' IS NULL AND pages.deleted_at = (dg.completed_keys->0->>'old_deleted_at')::timestamptz) -- #7013: a restorable re-extraction retirement waits out its hold (atomRetirementPurgeHold). AND NOT (pages.type='atom' AND pages.frontmatter->>'retired_by'='managed-reextract' AND pages.deleted_at > now() - interval '30 days' AND pages.id IN (SELECT held.id FROM (WITH retired AS ( SELECT p.id, p.source_id, p.slug, CASE WHEN NULLIF(p.frontmatter->>'source_slug','') IS NOT NULL THEN 'slug:'||(p.frontmatter->>'source_slug') WHEN NULLIF(p.frontmatter->>'source_path','') IS NOT NULL THEN 'path:'||(p.frontmatter->>'source_path') END AS okey, NULLIF(p.frontmatter->>'source_slug','') AS origin_slug, COALESCE(NULLIF(p.frontmatter->>'source_slug',''), p.frontmatter->>'source_path') AS origin, regexp_replace(COALESCE(p.frontmatter->>'source_hash',''),'^pending:','') AS hash, p.frontmatter->>'retired_at' AS retired_at FROM pages p WHERE p.type='atom' AND p.deleted_at IS NOT NULL AND p.frontmatter->>'retired_by'='managed-reextract' AND NULLIF(p.frontmatter->>'imported_from','') IS NULL AND (true) ), latest AS ( SELECT DISTINCT ON (source_id, okey) source_id, okey, hash FROM retired WHERE okey IS NOT NULL ORDER BY source_id, okey, retired_at DESC NULLS LAST, id DESC ), live AS ( SELECT p.source_id, CASE WHEN NULLIF(p.frontmatter->>'source_slug','') IS NOT NULL THEN 'slug:'||(p.frontmatter->>'source_slug') WHEN NULLIF(p.frontmatter->>'source_path','') IS NOT NULL THEN 'path:'||(p.frontmatter->>'source_path') END AS okey, array_agg(DISTINCT regexp_replace(COALESCE(p.frontmatter->>'source_hash',''),'^pending:','')) AS hashes FROM pages p WHERE p.type='atom' AND p.deleted_at IS NULL AND NULLIF(p.frontmatter->>'imported_from','') IS NULL AND (true) GROUP BY 1, 2 ) SELECT r.id, r.source_id, r.slug, CASE WHEN r.origin_slug IS NOT NULL THEN 'page' ELSE 'transcript' END AS origin_kind, r.origin, o.id AS origin_page_id, r.hash, r.retired_at, (r.okey IS NOT NULL AND (r.origin_slug IS NULL OR o.id IS NOT NULL)) AS origin_live, (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, (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_generation FROM retired r LEFT JOIN pages o ON o.source_id=r.source_id AND o.slug=r.origin_slug AND o.deleted_at IS NULL LEFT JOIN live lv ON lv.source_id=r.source_id AND lv.okey=r.okey LEFT JOIN latest l ON l.source_id=r.source_id AND l.okey=r.okey) held WHERE held.restorable)) -- GBRA-161: every derived retirement waits out its hold (derivedRetirementPurgeHold). AND NOT (COALESCE(pages.frontmatter->>'retired_by','') IN ('extract-atoms','managed-reextract','stale-atoms','life-chronicle','generation-restore','synthesis-resynthesis','synthesis-cleanup') AND pages.deleted_at > now() - interval '1 day' * (SELECT COALESCE(MAX(CASE WHEN c.value ~ '^[0-9]{1,3}$' THEN CASE WHEN c.value::int BETWEEN 3 AND 365 THEN c.value::int END END), 30) FROM config c WHERE c.key = 'generations.restore_days')) RETURNING slug, source_id",
+ "textSha256": "9bd0a9e9c1d4a2f2e6984038737f5d74b708c92465293d31d5c894aa2a5ab2f2"
},
{
"event": "commit",
@@ -803,8 +803,8 @@
"params": [
"number"
],
- "sql": "SELECT slug, deleted_at FROM pages WHERE deleted_at IS NOT NULL AND deleted_at < now() - ($1 || ' hours')::interval AND NOT EXISTS (SELECT 1 FROM op_checkpoints dg WHERE dg.op = 'sync-identity-downgrade' AND dg.fingerprint = pages.id::text AND dg.completed_keys->0->>'disposition' IS NULL AND pages.deleted_at = (dg.completed_keys->0->>'old_deleted_at')::timestamptz) AND NOT (pages.type='atom' AND pages.frontmatter->>'retired_by'='managed-reextract' AND pages.deleted_at > now() - interval '30 days' AND pages.id IN (SELECT held.id FROM (WITH retired AS ( SELECT p.id, p.source_id, p.slug, CASE WHEN NULLIF(p.frontmatter->>'source_slug','') IS NOT NULL THEN 'slug:'||(p.frontmatter->>'source_slug') WHEN NULLIF(p.frontmatter->>'source_path','') IS NOT NULL THEN 'path:'||(p.frontmatter->>'source_path') END AS okey, NULLIF(p.frontmatter->>'source_slug','') AS origin_slug, COALESCE(NULLIF(p.frontmatter->>'source_slug',''), p.frontmatter->>'source_path') AS origin, regexp_replace(COALESCE(p.frontmatter->>'source_hash',''),'^pending:','') AS hash, p.frontmatter->>'retired_at' AS retired_at FROM pages p WHERE p.type='atom' AND p.deleted_at IS NOT NULL AND p.frontmatter->>'retired_by'='managed-reextract' AND NULLIF(p.frontmatter->>'imported_from','') IS NULL AND (true) ), latest AS ( SELECT DISTINCT ON (source_id, okey) source_id, okey, hash FROM retired WHERE okey IS NOT NULL ORDER BY source_id, okey, retired_at DESC NULLS LAST, id DESC ), live AS ( SELECT p.source_id, CASE WHEN NULLIF(p.frontmatter->>'source_slug','') IS NOT NULL THEN 'slug:'||(p.frontmatter->>'source_slug') WHEN NULLIF(p.frontmatter->>'source_path','') IS NOT NULL THEN 'path:'||(p.frontmatter->>'source_path') END AS okey, array_agg(DISTINCT regexp_replace(COALESCE(p.frontmatter->>'source_hash',''),'^pending:','')) AS hashes FROM pages p WHERE p.type='atom' AND p.deleted_at IS NULL AND NULLIF(p.frontmatter->>'imported_from','') IS NULL AND (true) GROUP BY 1, 2 ) SELECT r.id, r.source_id, r.slug, CASE WHEN r.origin_slug IS NOT NULL THEN 'page' ELSE 'transcript' END AS origin_kind, r.origin, o.id AS origin_page_id, r.hash, r.retired_at, (r.okey IS NOT NULL AND (r.origin_slug IS NULL OR o.id IS NOT NULL)) AS origin_live, (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, (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_generation FROM retired r LEFT JOIN pages o ON o.source_id=r.source_id AND o.slug=r.origin_slug AND o.deleted_at IS NULL LEFT JOIN live lv ON lv.source_id=r.source_id AND lv.okey=r.okey LEFT JOIN latest l ON l.source_id=r.source_id AND l.okey=r.okey) held WHERE held.restorable)) AND NOT (COALESCE(pages.frontmatter->>'retired_by','') IN ('extract-atoms','managed-reextract','stale-atoms','life-chronicle','generation-restore') AND pages.deleted_at > now() - interval '1 day' * (SELECT COALESCE(MAX(CASE WHEN c.value ~ '^[0-9]{1,3}$' THEN CASE WHEN c.value::int BETWEEN 3 AND 365 THEN c.value::int END END), 30) FROM config c WHERE c.key = 'generations.restore_days')) ORDER BY deleted_at ASC, slug ASC",
- "textSha256": "9e33c1110b060594c5e908aff9ed93346811af360db69a8a7843455c8ac4e764"
+ "sql": "SELECT slug, deleted_at FROM pages WHERE deleted_at IS NOT NULL AND deleted_at < now() - ($1 || ' hours')::interval AND NOT EXISTS (SELECT 1 FROM op_checkpoints dg WHERE dg.op = 'sync-identity-downgrade' AND dg.fingerprint = pages.id::text AND dg.completed_keys->0->>'disposition' IS NULL AND pages.deleted_at = (dg.completed_keys->0->>'old_deleted_at')::timestamptz) AND NOT (pages.type='atom' AND pages.frontmatter->>'retired_by'='managed-reextract' AND pages.deleted_at > now() - interval '30 days' AND pages.id IN (SELECT held.id FROM (WITH retired AS ( SELECT p.id, p.source_id, p.slug, CASE WHEN NULLIF(p.frontmatter->>'source_slug','') IS NOT NULL THEN 'slug:'||(p.frontmatter->>'source_slug') WHEN NULLIF(p.frontmatter->>'source_path','') IS NOT NULL THEN 'path:'||(p.frontmatter->>'source_path') END AS okey, NULLIF(p.frontmatter->>'source_slug','') AS origin_slug, COALESCE(NULLIF(p.frontmatter->>'source_slug',''), p.frontmatter->>'source_path') AS origin, regexp_replace(COALESCE(p.frontmatter->>'source_hash',''),'^pending:','') AS hash, p.frontmatter->>'retired_at' AS retired_at FROM pages p WHERE p.type='atom' AND p.deleted_at IS NOT NULL AND p.frontmatter->>'retired_by'='managed-reextract' AND NULLIF(p.frontmatter->>'imported_from','') IS NULL AND (true) ), latest AS ( SELECT DISTINCT ON (source_id, okey) source_id, okey, hash FROM retired WHERE okey IS NOT NULL ORDER BY source_id, okey, retired_at DESC NULLS LAST, id DESC ), live AS ( SELECT p.source_id, CASE WHEN NULLIF(p.frontmatter->>'source_slug','') IS NOT NULL THEN 'slug:'||(p.frontmatter->>'source_slug') WHEN NULLIF(p.frontmatter->>'source_path','') IS NOT NULL THEN 'path:'||(p.frontmatter->>'source_path') END AS okey, array_agg(DISTINCT regexp_replace(COALESCE(p.frontmatter->>'source_hash',''),'^pending:','')) AS hashes FROM pages p WHERE p.type='atom' AND p.deleted_at IS NULL AND NULLIF(p.frontmatter->>'imported_from','') IS NULL AND (true) GROUP BY 1, 2 ) SELECT r.id, r.source_id, r.slug, CASE WHEN r.origin_slug IS NOT NULL THEN 'page' ELSE 'transcript' END AS origin_kind, r.origin, o.id AS origin_page_id, r.hash, r.retired_at, (r.okey IS NOT NULL AND (r.origin_slug IS NULL OR o.id IS NOT NULL)) AS origin_live, (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, (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_generation FROM retired r LEFT JOIN pages o ON o.source_id=r.source_id AND o.slug=r.origin_slug AND o.deleted_at IS NULL LEFT JOIN live lv ON lv.source_id=r.source_id AND lv.okey=r.okey LEFT JOIN latest l ON l.source_id=r.source_id AND l.okey=r.okey) held WHERE held.restorable)) AND NOT (COALESCE(pages.frontmatter->>'retired_by','') IN ('extract-atoms','managed-reextract','stale-atoms','life-chronicle','generation-restore','synthesis-resynthesis','synthesis-cleanup') AND pages.deleted_at > now() - interval '1 day' * (SELECT COALESCE(MAX(CASE WHEN c.value ~ '^[0-9]{1,3}$' THEN CASE WHEN c.value::int BETWEEN 3 AND 365 THEN c.value::int END END), 30) FROM config c WHERE c.key = 'generations.restore_days')) ORDER BY deleted_at ASC, slug ASC",
+ "textSha256": "ba89292c8dfd0f6d0b8db2bcbc171271baf490a5150e48fa783cdd42fbbff79b"
}
]
},