Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

feat(purge): sweep trust_proposals rows that target or relate to a purged fact or take (#5575 CEO-22)

garrytan11e403ca5732
diff --git a/src/core/deletion-inventory.ts b/src/core/deletion-inventory.ts
index 605a4a4d85..be89ff7c22 100644
--- a/src/core/deletion-inventory.ts
+++ b/src/core/deletion-inventory.ts
@@ -156,7 +156,7 @@ const ROWS: readonly Row[] = [
['takes', 'swept', 'take', 'claim kind holder since_date until_date source resolved_unit resolved_source resolved_by embedding resolved_quality embedding_model embedded_text_hash write_principal_kind write_principal_id last_write_principal_kind last_write_principal_id trust_tier write_origin'],
['think_ab_results', 'out_of_scope', 'probed_reported', 'source_id wave_version question baseline_answer with_calibration_answer preferred model_id notes', 'question baseline_answer with_calibration_answer'],
['timeline_entries', 'out_of_scope', 'probed_reported', 'source summary detail write_principal_kind write_principal_id last_write_principal_kind last_write_principal_id trust_tier write_origin', 'summary detail'],
- ['trust_proposals', 'out_of_scope', 'probed_reported', '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', 'before_state after_state'],
+ ['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'],
];
diff --git a/src/core/facts/purge.ts b/src/core/facts/purge.ts
index 88443863de..d131f0d367 100644
--- a/src/core/facts/purge.ts
+++ b/src/core/facts/purge.ts
@@ -293,7 +293,9 @@ export async function submitPurgeFactMutation(ctx: OperationContext, params: Rec
const ids = plan.factIds;
const review = (await tx.executeRaw(`DELETE FROM decide_review_queue WHERE source_id=$1 AND (a_ref=ANY($2::text[]) OR b_ref=ANY($2::text[])) RETURNING 1`, [p.sourceId, ids.map(String)])).length
+ (await tx.executeRaw(`DELETE FROM decide_review_proposals WHERE source_id=$1 AND (a_ref=ANY($2::text[]) OR b_ref=ANY($2::text[])) RETURNING 1`, [p.sourceId, ids.map(String)])).length
- + (await tx.executeRaw(`DELETE FROM decide_proposals WHERE source_id=$1 AND (new_fact_id=ANY($2::bigint[]) OR old_fact_id=ANY($2::bigint[])) RETURNING 1`, [p.sourceId, ids])).length;
+ + (await tx.executeRaw(`DELETE FROM decide_proposals WHERE source_id=$1 AND (new_fact_id=ANY($2::bigint[]) OR old_fact_id=ANY($2::bigint[])) RETURNING 1`, [p.sourceId, ids])).length
+ + (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;
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`,
diff --git a/test/purge-fact.test.ts b/test/purge-fact.test.ts
index de21d7d691..ce59a5e395 100644
--- a/test/purge-fact.test.ts
+++ b/test/purge-fact.test.ts
@@ -126,6 +126,23 @@ describe('purge_fact', () => {
}
}), 60_000);
+ test('trust proposals targeting or relating to the purged fact are swept with it', async () => withEnv({ GBRAIN_HOME: home }, async () => {
+ for (const engine of engines) {
+ const slug = 'people/trustprop-example';
+ const id = await seedPage(engine, slug);
+ const [other] = await engine.executeRaw<{ id: number }>('SELECT id FROM facts WHERE entity_slug=$1 AND id<>$2 ORDER BY id LIMIT 1', [slug, id]);
+ const insert = (target: number, related: number | null) => engine.executeRaw(`INSERT INTO trust_proposals (action, source_id, target_table, target_id, related_table, related_id, before_state, proposer)
+ VALUES ('supersede_fact', 'default', 'facts', $1, $2, $3, $4::text::jsonb, 'test') RETURNING id`,
+ [target, related === null ? null : 'facts', related, JSON.stringify({ fact: CLAIM })]);
+ await insert(id, Number(other.id));
+ await insert(Number(other.id), id);
+ const done = await confirmedPurge(engine, id);
+ expect(done.state).toBe('committed');
+ expect(await engine.executeRaw('SELECT 1 FROM trust_proposals WHERE target_id=$1 OR related_id=$1', [id])).toHaveLength(0);
+ expect(await engine.executeRaw('SELECT 1 FROM trust_proposals WHERE strpos(before_state::text,$1)>0', [CLAIM])).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';