Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix: find, redact and sweep journal requests whose stored change reason quotes a purged claim in its folded spacing
garrytan90106fc199a9
diff --git a/src/core/facts/purge-verify.ts b/src/core/facts/purge-verify.tsindex a6394aa2d8..c01f5995b1 100644--- a/src/core/facts/purge-verify.ts+++ b/src/core/facts/purge-verify.ts@@ -514,8 +514,8 @@ export async function verifyFactPurge(engine: BrainEngine, input: { target: Purg[src, ...claimNeedles(claim), reasonNeedle(claim)], plan.versions, removed.page_versions, { ...prose, items: (input.prose ?? []).slice(0, 20) });stores[stores.length - 1] = await withSegmentedProse(engine, stores[stores.length - 1]!, src, [claim], deadline);await swept('persistence_requests', `SELECT 1 FROM persistence_requests WHERE source_id=$1 AND (${['intent', 'outcome', 'error_detail', 'error_message']- .map(c => `strpos(lower(COALESCE(${c}::text,'')),$2)>0 OR strpos(lower(COALESCE(${c}::text,'')),$3)>0`).join(' OR ')})`,- [src, needle, JSON.stringify(claim).slice(1, -1).toLowerCase()], plan.requests.length, removed.persistence_requests);+ .map(c => `strpos(lower(COALESCE(${c}::text,'')),$2)>0 OR strpos(lower(COALESCE(${c}::text,'')),$3)>0 OR strpos(lower(COALESCE(${c}::text,'')),$4)>0`).join(' OR ')})`,+ [src, needle, JSON.stringify(claim).slice(1, -1).toLowerCase(), JSON.stringify(reasonNeedle(claim)).slice(1, -1)], plan.requests.length, removed.persistence_requests);if (!dryRun && !await relationExists(engine, 'query_cache')) stores.push(missingCache('query_cache'));else await swept('query_cache', 'SELECT 1 FROM query_cache WHERE $1::text IS NOT NULL', [src], 0, removed.query_cache);const refs = plan.factIds.map(String);diff --git a/src/core/facts/purge.ts b/src/core/facts/purge.tsindex 6c2390e113..59dd576886 100644--- a/src/core/facts/purge.ts+++ b/src/core/facts/purge.ts@@ -187,7 +187,7 @@ export async function planFactPurge(engine: BrainEngine, target: PurgeTarget, alconst { stagings, held } = await stagingsHoldingClaim(engine, target.source_id, target.fact);// #6511 [R5]: a statement timeout of this unindexable substring search is deterministic on a large journal, not contention: the typed code, not a retry.const requests = await faultPoint('purge:carrying_lookup', { sourceId: target.source_id, operation: PURGE_OPERATION })- .then(() => findRequestsCarrying(engine, target.source_id, target.fact)).catch(error => {+ .then(() => findRequestsCarrying(engine, target.source_id, target.fact, reasonNeedle(target.fact))).catch(error => {if ((error as { code?: unknown } | null)?.code !== '57014') throw error;throw opError('purge_lookup_timeout', 'This purge could not finish; nothing was purged. Do not assume the requested content was erased.',`The lookup of historical write requests carrying fact ${target.id} in source ${target.source_id} exceeded its statement budget while the purge was planned, so no plan was made and nothing was removed, tombstoned or redacted. `@@ -514,7 +514,7 @@ export async function submitPurgeFactMutation(ctx: OperationContext, params: Recawait assertNoPendingPublication(tx, p.sourceId, plan.pages, admitted.id);const effectReasons = await redactGitEffectReasons(tx, p.sourceId, current.fact);const reasonTombstones = await recordReasonTombstones(tx, { sourceId: p.sourceId, claim: current.fact, rawId: p.rawId, plan: trailers });- const intents = await redactRequestIntents(tx, plan.requests, admitted.id, { needles: [current.fact] });+ const intents = await redactRequestIntents(tx, plan.requests, admitted.id, { needles: [current.fact, reasonNeedle(current.fact)] });const ledgerPresent = await sourceHasBeliefEvents(tx, p.sourceId);const streams = ledgerPresent ? await factPurgeStreams(tx, p.sourceId, plan) : [];await tx.lockPageKeys([...new Set([...plan.pages.map(page => page.slug), ...streams])].map(slug => ({ sourceId: p.sourceId, slug })));diff --git a/src/core/persistence/intent-redaction.ts b/src/core/persistence/intent-redaction.tsindex 4be521f9de..20a25afd68 100644--- a/src/core/persistence/intent-redaction.ts+++ b/src/core/persistence/intent-redaction.ts@@ -197,17 +197,18 @@ export async function recheckRequestsCarrying(tx: Pick<BrainEngine, 'executeRaw'}/**- * Requests in `sourceId` whose stored intent, outcome or error text carries `claim`, bounded, compacted- * ones included (compaction keeps the outcome). Owner-only; never returns the text.+ * Requests in `sourceId` whose stored intent, outcome or error text carries `claim`, or `reason` (the claim folded+ * as a stored change reason is), bounded, compacted ones included (compaction keeps the outcome). Owner-only; never+ * returns the text.*/-export async function findRequestsCarrying(engine: BrainEngine, sourceId: string, claim: string, limit = 10_000): Promise<IntentRedactionTarget[]> {+export async function findRequestsCarrying(engine: BrainEngine, sourceId: string, claim: string, reason = claim, limit = 10_000): Promise<IntentRedactionTarget[]> {const needle = claim.toLowerCase();// The claim as it appears inside a JSON string (quotes and backslashes escaped), lowercased.const jsonNeedle = JSON.stringify(claim).slice(1, -1).toLowerCase();- const carries = (column: string) => `(${column} IS NOT NULL AND (strpos(lower(${column}::text),$2)>0 OR strpos(lower(${column}::text),$3)>0))`;+ const carries = (column: string) => `(${column} IS NOT NULL AND (strpos(lower(${column}::text),$2)>0 OR strpos(lower(${column}::text),$3)>0 OR strpos(lower(${column}::text),$4)>0))`;return engine.executeRaw<IntentRedactionTarget>(`SELECT id::text AS id,principal_kind,principal_id FROM persistence_requestsWHERE source_id=$1 AND (${carries('intent')} OR ${carries('outcome')} OR ${carries('error_detail')} OR ${carries('error_message')})- ORDER BY sequence LIMIT $4`, [sourceId, needle, jsonNeedle, limit]);+ ORDER BY sequence LIMIT $5`, [sourceId, needle, jsonNeedle, JSON.stringify(reason).slice(1, -1).toLowerCase(), limit]);}/** Replace every JSON string that carries one of `needles` (case-insensitive) with "[purged]"; structure and other values stay. */diff --git a/test/reason-trailers-gbra217.test.ts b/test/reason-trailers-gbra217.test.tsindex 121e6be8c4..bd1398d45f 100644--- a/test/reason-trailers-gbra217.test.ts+++ b/test/reason-trailers-gbra217.test.ts@@ -563,6 +563,29 @@ describe('A7: a claim whose spacing a stored reason folded', () => {expect(after!.data).toEqual({ version: 2, commit_line: '[purged]' });const swept = await verifyFactPurge(engine, { target, plan, dryRun: false, removed: {} });expect(swept.stores.find(store => store.store === 'page_versions')).toMatchObject({ status: 'not_present' });++ const slug = `policies/folded-example-${n}`;+ await run(engine, 'default', 'put_page', { slug, content: page('Edition one.'), wait_ms: 30_000 });+ const revision = String((await run(engine, 'default', 'get_page', { slug, include_content: true })).revision);+ const second = await run(engine, 'default', 'put_page', { slug, content: page('Edition two.'), expected_revision: revision,+ reason: `Board asked because ${claim} leaked.`, wait_ms: 30_000 });+ expect((second.outcome ?? second).reason_recorded).toMatchObject({ reason: `Board asked because ${folded} leaked.`, git: 'no_repository' });+ const [held] = await engine.executeRaw<PurgeTarget>(`INSERT INTO facts(source_id,fact,entity_slug,visibility,source,source_markdown_slug,row_num)+ VALUES ('default',$1,NULL,'world','test',NULL,1) RETURNING id,source_id,visibility,fact,entity_slug,source_markdown_slug,+ gbrain_fact_fingerprint(fact) AS fact_hash,embedding::text AS embedding,embedding_model`, [claim]);+ const heldTarget = { ...held!, id: Number(held!.id) };+ const heldPlan = await planFactPurge(engine, heldTarget, false);+ expect(heldPlan.requests).toHaveLength(1);+ const unswept = await verifyFactPurge(engine, { target: heldTarget, plan: heldPlan, dryRun: false, removed: {} });+ expect(unswept.stores.find(store => store.store === 'persistence_requests')).toMatchObject({ remaining: 1 });+ const repoless = await purge(engine, 'default', heldTarget.id);+ expect(repoless.state).toBe('committed');+ expect(repoless.stores.find((store: { store: string }) => store.store === 'persistence_requests')).toMatchObject({ status: 'deleted' });+ const requests = await engine.executeRaw<{ intent: Record<string, unknown> | null; outcome: Record<string, any> | null }>(+ `SELECT intent, outcome FROM persistence_requests WHERE source_id = 'default' AND slug = $1 AND (outcome->'reason_recorded') IS NOT NULL`, [slug]);+ expect(requests.map(row => [row.intent?.reason ?? null, row.outcome?.reason_recorded?.reason])).toEqual([[null, '[purged]']]);+ expect(await engine.executeRaw(`SELECT 1 FROM persistence_requests WHERE source_id = 'default'+ AND (strpos(lower(COALESCE(intent::text, '')), $1) > 0 OR strpos(lower(COALESCE(outcome::text, '')), $1) > 0)`, [folded.toLowerCase()])).toHaveLength(0);}}), 120_000);});