Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

feat(purge): writers refuse purged claims with typed purged_content; isFactWithdrawn covers purge tombstones

garrytand87aadc212c3
diff --git a/src/core/facts/single-prepare.ts b/src/core/facts/single-prepare.ts
index 21f5df0783..9d4ae34443 100644
--- a/src/core/facts/single-prepare.ts
+++ b/src/core/facts/single-prepare.ts
@@ -3,6 +3,7 @@ import { verbError } from '../ops/contract.ts';
import { isAvailable, embedOne, getEmbeddingModel } from '../ai/gateway.ts';
import { cosineSimilarity } from './classify.ts';
import { isFactWithdrawn } from './withdrawal.ts';
+import { assertFactNotPurged } from './withdrawal.ts';
import { cosineVerdict } from './capture-dedup.ts';
export type FactCandidate = FactRow & { source_markdown_slug: string | null; row_num: number | null };
@@ -24,6 +25,7 @@ export async function prepareFactEmbedding(fact: string, signal?: AbortSignal, d
return { embedding: null, embedding_model: null, degraded: true };
}
export async function assertFactNotWithdrawn(engine: BrainEngine, sourceId: string, input: SingleFactIntent): Promise<void> {
+ await assertFactNotPurged(engine, sourceId, input);
if (await isFactWithdrawn(engine, sourceId, input.visibility, input.fact, input.entity_slug)) {
throw verbError('invalid_params', 'fact_withdrawn: this exact claim was explicitly forgotten in this source and visibility.',
'Remember a corrected claim. Repeating the old claim does not restore withdrawn memory.');
diff --git a/src/core/facts/withdrawal.ts b/src/core/facts/withdrawal.ts
index a5fda431df..e0f2848d8f 100644
--- a/src/core/facts/withdrawal.ts
+++ b/src/core/facts/withdrawal.ts
@@ -1,6 +1,6 @@
import type { BrainEngine } from '../engine.ts';
import { renderFactsTable, type ParsedFact } from '../facts-fence.ts';
-import { OperationError } from '../ops/contract.ts';
+import { OperationError, opError } from '../ops/contract.ts';
import { fenceOperationError } from '../fence-repair/refusal.ts';
import { withdrawnFact, withdrawalFenceBlocks } from './withdrawal-overlay.ts';
import { ambiguousFenceClaims, discoverWithdrawalTargets, withdrawalDiscoveryFailure } from './withdrawal-discovery.ts';
@@ -173,12 +173,28 @@ export async function assertPreparedFactWithdrawals(engine: BrainEngine, sourceI
}
}
-/** Explicit remember is not an implicit restore operation for that entity. */
+/** Explicit remember is not an implicit restore operation for that entity. A purged claim counts as withdrawn for every writer. */
export async function isFactWithdrawn(
engine: BrainEngine, sourceId: string, visibility: string, claim: string, entitySlug: string | null,
): Promise<boolean> {
const rows = await engine.executeRaw(`SELECT 1 FROM fact_withdrawals
WHERE source_id=$1 AND visibility=$2 AND fact_hash IN (gbrain_fact_fingerprint($3),gbrain_fact_fingerprint_v1($3))
- AND (subject = '*' OR subject = $4::text)`, [sourceId,visibility,claim,entitySlug]);
+ AND (subject = '*' OR subject = $4::text)
+ UNION ALL SELECT 1 FROM fact_purges WHERE source_id=$1 AND visibility=$2 AND fact_hash=gbrain_fact_fingerprint($3)
+ AND (subject = '*' OR subject = $4::text) LIMIT 1`, [sourceId,visibility,claim,entitySlug]);
return rows.length > 0;
}
+
+/** #5575: the claim matches a purge tombstone (the facts guard raises the same typed code on insert). */
+export async function isFactPurged(engine: BrainEngine, sourceId: string, visibility: string, claim: string, entitySlug: string | null): Promise<boolean> {
+ const rows = await engine.executeRaw(`SELECT 1 FROM fact_purges WHERE source_id=$1 AND visibility=$2 AND fact_hash=gbrain_fact_fingerprint($3)
+ AND (subject = '*' OR subject = $4::text) LIMIT 1`, [sourceId, visibility, claim, entitySlug]);
+ return rows.length > 0;
+}
+
+/** Writers check this before isFactWithdrawn so a purged claim refuses with typed purged_content, before any provider work. */
+export async function assertFactNotPurged(engine: BrainEngine, sourceId: string, input: { visibility: string; fact: string; entity_slug: string | null }): Promise<void> {
+ if (!await isFactPurged(engine, sourceId, input.visibility, input.fact, input.entity_slug)) return;
+ throw opError('purged_content', 'purged_content: this claim was purged from this source and cannot be saved again.',
+ 'Purged content stays out of the brain. If it is still true, remember it in new words; only the owner can clear a purge tombstone.');
+}
diff --git a/test/purge-fact.test.ts b/test/purge-fact.test.ts
index 21b7ac6ae8..de21d7d691 100644
--- a/test/purge-fact.test.ts
+++ b/test/purge-fact.test.ts
@@ -233,3 +233,19 @@ describe('purge_fact', () => {
}
}), 60_000);
});
+
+describe('purged claims and writers', () => {
+ test('remember of a purged claim refuses with typed purged_content before admission work; isFactWithdrawn covers it', async () => withEnv({ GBRAIN_HOME: home }, async () => {
+ const { assertFactNotWithdrawn } = await import('../src/core/facts/single-prepare.ts');
+ const { isFactWithdrawn, isFactPurged } = await import('../src/core/facts/withdrawal.ts');
+ for (const engine of engines) {
+ const slug = 'people/gus-example';
+ const id = await seedPage(engine, slug);
+ await confirmedPurge(engine, id);
+ expect(await isFactPurged(engine, 'default', 'world', 'DOOR CODE IS 4417!', slug)).toBe(true);
+ expect(await isFactWithdrawn(engine, 'default', 'world', CLAIM, slug)).toBe(true);
+ expect(await isFactPurged(engine, 'default', 'world', CLAIM, 'people/other-example')).toBe(false);
+ expect(await codeOf(assertFactNotWithdrawn(engine, 'default', { fact: CLAIM, kind: 'fact', visibility: 'world', entity_slug: slug }))).toBe('purged_content');
+ }
+ }), 60_000);
+});