Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

feat(purge): sweep write-gate holds carrying the claim and gate receipts naming purged rows (#5575 CEO-22); attribution inventory lists the trust writers

garrytan0c1da965f02e
diff --git a/docs/architecture/system-of-record.md b/docs/architecture/system-of-record.md
index 90d086a029..bf1df4d791 100644
--- a/docs/architecture/system-of-record.md
+++ b/docs/architecture/system-of-record.md
@@ -235,8 +235,8 @@ or a physical projection that leaves attribution untouched):
- `src/core/embed-facts.ts` (1): embedding columns only (physical projection).
- `src/core/embedding-dim-check.ts` (1): embedding columns only (physical projection).
- `src/core/extract/receipt-writer.ts` (1): the extraction receipt page runs in `maintenanceTransaction`.
-- `src/core/facts/conversation-publication.ts` (3): the managed conversation-facts preparer clears each member page's prior rows and inserts its frozen entry (or a blocked outcome row) inside the coordinator publication of its admitted batch request; the unmanaged cleanup runs the same clear in `maintenanceTransaction`.
- `src/core/facts/backstop.ts` (3): the facts backstop's DB-only fallbacks insert each fact in `maintenanceTransaction`; managed brains publish through the facts request preparer.
+- `src/core/facts/conversation-publication.ts` (3): the managed conversation-facts preparer clears each member page's prior rows and inserts its frozen entry (or a blocked outcome row) inside the coordinator publication of its admitted batch request; the unmanaged cleanup runs the same clear in `maintenanceTransaction`.
- `src/core/facts/derivation-inputs.ts` (3): `hideDerivedRows` expires derived facts, deactivates derived takes and soft-deletes derived pages inside the purge's coordinated transaction, attributed to the purge request.
- `src/core/facts/fence-write.ts` (2): the markdown-first fence reconcile insert and its page body mirror each run in `maintenanceTransaction`.
- `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).
@@ -270,13 +270,13 @@ or a physical projection that leaves attribution untouched):
- `src/core/persistence/sync-prepare.ts` (4): managed sync request preparer.
- `src/core/persistence/takes-prepare.ts` (3): takes request preparer.
- `src/core/repair/captured-facts.ts` (1): a request on managed brains; the unmanaged captured-facts expiry runs in `maintenanceTransaction`.
-- `src/core/repair/ontology-facts.ts` (1): a coordinated database-only write (`coordinatedDatabaseWrite`, the `ontology_propose` path) on managed brains; the unmanaged ontology-facts restore runs in `maintenanceTransaction`.
- `src/core/repair/conversation-labels.ts` (4): a request on managed brains; the unmanaged label retirement runs in `maintenanceTransaction`.
- `src/core/repair/extractor-facts.ts` (1): a request on managed brains; the unmanaged extractor-facts restore runs in `maintenanceTransaction`.
- `src/core/repair/frontmatter.ts` (1): a `managed_file_repair` request on managed sources; the unmanaged rename runs in `maintenanceTransaction`.
+- `src/core/repair/ontology-facts.ts` (1): a coordinated database-only write (`coordinatedDatabaseWrite`, the `ontology_propose` path) on managed brains; the unmanaged ontology-facts restore runs in `maintenanceTransaction`.
+- `src/core/repair/stale-atoms.ts` (3): a request on managed brains; the unmanaged retirement runs in `maintenanceTransaction`.
- `src/core/repair/take-supersession.ts` (1): `gbrain repair take-supersession` reprojection: a receipted `managed_maintenance_take_reproject` request on managed brains, `maintenanceTransaction` on unmanaged ones; fence edits go through a revision-bound `put_page`.
- `src/core/repair/timeline-comments.ts` (1): `gbrain repair timeline-comments` row cleanup: `withCoordinatedWrite` under the page key on managed brains, `maintenanceTransaction` otherwise.
-- `src/core/repair/stale-atoms.ts` (3): a request on managed brains; the unmanaged retirement runs in `maintenanceTransaction`.
- `src/core/schema-pack/page-to-alias.ts` (1): the page-to-alias conversion soft-deletes each converted page in `maintenanceTransaction`.
- `src/core/schema-pack/page-to-link.ts` (1): the page-to-link conversion soft-deletes each converted page in `maintenanceTransaction`.
- `src/core/schema-pack/retype.ts` (1): each bounded retype batch runs in `maintenanceTransaction`.
@@ -286,6 +286,9 @@ or a physical projection that leaves attribution untouched):
- `src/core/think/index.ts` (1): the saved `think` result page runs in `maintenanceTransaction`.
- `src/core/timeline-dedup-repair.ts` (1): runs only from schema migration v139, before migration v193 adds the attribution columns, so it has no actor to record.
- `src/core/timeline-write-through.ts` (2): the timeline write-through's page row splice and entry insert run in one `maintenanceTransaction`.
+- `src/core/trust/fence-guard.ts` (2): the guarded fence re-projection detaches or moves higher-tier fact rows inside the page's coordinated publication.
+- `src/core/trust/owner-actions.ts` (1): an owner release inserts the held fact in a coordinated (managed) or attributed (unmanaged) transaction at user_confirmed.
+- `src/core/trust/supersede-handlers.ts` (2): owner accept/undo of a trust proposal re-tiers the new fact inside the checked supersede's transaction.
<!-- write-attribution-covered:end -->
Unattributed (on unmanaged brains these write with `NULL` attribution). These
diff --git a/src/core/deletion-inventory.ts b/src/core/deletion-inventory.ts
index 1e71550e6a..0e011add5e 100644
--- a/src/core/deletion-inventory.ts
+++ b/src/core/deletion-inventory.ts
@@ -159,7 +159,7 @@ const ROWS: readonly Row[] = [
['trust_allow_rules', 'out_of_scope', 'no_memory_text', 'source_id uri_prefix reason_family created_by removed_by reason'],
['trust_proposals', 'swept', 'review_rows', 'action source_id target_table related_table before_state after_state proposer proposer_principal_kind proposer_principal_id status decided_principal_kind decided_principal_id'],
['wanted_links', 'out_of_scope', 'probed_reported', 'source_id producer ref_kind target_source_id target_ref link_type context', 'context'],
- ['write_gate_holds', 'out_of_scope', 'probed_reported', 'kind source_id slug fingerprint payload write_origin tier reason_families reasons request_id status decided_by', 'payload'],
+ ['write_gate_holds', 'swept', 'review_rows', 'kind source_id slug fingerprint payload write_origin tier reason_families reasons request_id status decided_by'],
['write_gate_receipts', 'out_of_scope', 'text_free_ledger', 'target_table target_id source_id content_hash tier verdict reason_families reasons request_id'],
];
diff --git a/src/core/facts/purge.ts b/src/core/facts/purge.ts
index d131f0d367..20c8eaf175 100644
--- a/src/core/facts/purge.ts
+++ b/src/core/facts/purge.ts
@@ -297,6 +297,12 @@ export async function submitPurgeFactMutation(ctx: OperationContext, params: Rec
+ (await tx.executeRaw(`DELETE FROM trust_proposals WHERE source_id=$1 AND ((target_table='facts' AND target_id=ANY($2::bigint[])) OR (related_table='facts' AND related_id=ANY($2::bigint[]))
OR (target_table='takes' AND target_id=ANY($3::bigint[])) OR (related_table='takes' AND related_id=ANY($3::bigint[]))) RETURNING 1`, [p.sourceId, ids, plan.takes.map(t => t.id)])).length;
const loops = (await tx.executeRaw('DELETE FROM open_loops WHERE source_id=$1 AND fact_id=ANY($2::bigint[]) RETURNING 1', [p.sourceId, ids])).length;
+ // #5575: held writes carrying the claim (fact text or take claim) and the gate receipts naming purged rows or those holds.
+ const holdIds = (await tx.executeRaw<{ id: string }>(`DELETE FROM write_gate_holds WHERE source_id=$1
+ AND gbrain_fact_fingerprint(COALESCE(payload->>'fact', payload->>'claim', ''))=$2 RETURNING id::text AS id`, [p.sourceId, current.fact_hash])).map(r => r.id);
+ const gateReceipts = (await tx.executeRaw(`DELETE FROM write_gate_receipts WHERE (target_table='facts' AND target_id=ANY($1::text[]))
+ OR (target_table='takes' AND target_id=ANY($2::text[])) OR (target_table='write_gate_holds' AND target_id=ANY($3::text[])) RETURNING 1`,
+ [ids.map(String), plan.takes.map(t => String(t.id)), holdIds])).length;
const proposals = (await tx.executeRaw('DELETE FROM take_proposals WHERE source_id=$1 AND gbrain_fact_fingerprint(claim_text)=$2 RETURNING 1', [p.sourceId, current.fact_hash])).length;
const notices = (await tx.executeRaw(`UPDATE core_edit_notices SET base_text=NULL WHERE source_id=$1 AND strpos(lower(base_text),$2)>0 RETURNING 1`,
[p.sourceId, current.fact.toLowerCase()])).length;
@@ -318,6 +324,7 @@ export async function submitPurgeFactMutation(ctx: OperationContext, params: Rec
targets: pages.map(page => ({ slug: page.slug, page_id: Number(page.id), revision: page.knowledge_revision })) })]);
counts = { facts, takes, chunks, page_versions: versions.redacted, pages: rewritten.length, persistence_requests: intents,
query_cache: cache, decide_review: review, open_loops: loops, take_proposals: proposals, core_edit_notices: notices,
+ write_gate_holds: holdIds.length, write_gate_receipts: gateReceipts,
derived_hidden: Object.values(derived.hidden).reduce((a, b) => a + b, 0) };
return completeWrite(tx, admitted, 'committed', { purge: { fact_id: p.id, hash8: token, fact_hash: current.fact_hash, subject: plan.subject,
visibility: current.visibility, removed: counts, derived: { ...derived, truncated: plan.derived.truncated }, pages: rewritten.length },
diff --git a/test/purge-fact.test.ts b/test/purge-fact.test.ts
index ce59a5e395..8feca6e336 100644
--- a/test/purge-fact.test.ts
+++ b/test/purge-fact.test.ts
@@ -143,6 +143,25 @@ describe('purge_fact', () => {
}
}), 60_000);
+ test('held writes carrying the claim and gate receipts naming the purged fact or those holds are swept', async () => withEnv({ GBRAIN_HOME: home }, async () => {
+ for (const engine of engines) {
+ const slug = 'people/heldclaim-example';
+ const id = await seedPage(engine, slug);
+ const [hold] = await engine.executeRaw<{ id: string }>(`INSERT INTO write_gate_holds (kind, source_id, slug, fingerprint, detector_version, tier, reason_families, reasons, detector_error, payload)
+ VALUES ('fact', 'default', $1, 'fp-heldclaim', 1, 'external_untrusted', ARRAY['override']::text[], ARRAY[]::text[], false, $2::text::jsonb) RETURNING id::text AS id`,
+ [slug, JSON.stringify({ fact: CLAIM })]);
+ const receipt = (table: string, target: string, hash: string) => engine.executeRaw(`INSERT INTO write_gate_receipts (target_table, target_id, source_id, content_hash, tier, detector_version, verdict, reason_families, reasons, detector_error)
+ VALUES ($1, $2, 'default', $3, 'agent_written', 1, 'flag', ARRAY['override']::text[], ARRAY[]::text[], false)`, [table, target, hash]);
+ await receipt('facts', String(id), 'h-fact');
+ await receipt('write_gate_holds', hold!.id, 'h-hold');
+ const done = await confirmedPurge(engine, id);
+ expect(done.state).toBe('committed');
+ expect(done.purge.removed).toMatchObject({ write_gate_holds: 1, write_gate_receipts: 2 });
+ expect(await engine.executeRaw(`SELECT 1 FROM write_gate_holds WHERE strpos(payload::text, $1) > 0`, [CLAIM])).toHaveLength(0);
+ expect(await engine.executeRaw(`SELECT 1 FROM write_gate_receipts WHERE (target_table='facts' AND target_id=$1) OR (target_table='write_gate_holds' AND target_id=$2)`, [String(id), hold!.id])).toHaveLength(0);
+ }
+ }), 60_000);
+
test('no resurrection: stale re-import, re-extraction and direct inserts are refused or dropped', async () => withEnv({ GBRAIN_HOME: home }, async () => {
for (const engine of engines) {
const slug = 'people/bob-example';