Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
belief ledger lane A: a short purged claim quotes a field only as a word in spaced scripts (#39)
garrytane3a40fa77160
diff --git a/src/core/belief/purge.ts b/src/core/belief/purge.tsindex 9ee4ce7e2f..377538dbef 100644--- a/src/core/belief/purge.ts+++ b/src/core/belief/purge.ts@@ -70,9 +70,25 @@ export function claimForms(claims: readonly string[]): string[] {return [...out];}+/** Claims shorter than this quote a field only as a word (#39); longer claims match as substrings. */+const SHORT_CLAIM = 8;+/** Letters and digits of scripts written with spaces between words: only there does a glued neighbour mean another word. */+const SPACED_WORD = /[\p{Script=Latin}\p{Script=Greek}\p{Script=Cyrillic}\p{Script=Armenian}\p{Script=Georgian}\p{N}]/u;+const glued = (edge: string | undefined, next: string | undefined) => !!edge && !!next && SPACED_WORD.test(edge) && SPACED_WORD.test(next);++/**+ * Whether a field quotes one of the forms. A short claim must not be glued to a letter or digit on either side+ * (`ceo` does not quote `herbaceous`); punctuation, markup and scripts without word spaces (CJK, Thai) still match.+ */export const quotes = (text: string, forms: readonly string[]) => {const lower = text.toLowerCase();- return forms.some(f => lower.includes(f));+ return forms.some(f => {+ if (f.length >= SHORT_CLAIM) return lower.includes(f);+ for (let at = lower.indexOf(f); at >= 0; at = lower.indexOf(f, at + 1)) {+ if (!glued(f[0], lower[at - 1]) && !glued(f[f.length - 1], lower[at + f.length])) return true;+ }+ return false;+ });};async function registerRedaction(tx: Pick<BrainEngine, 'executeRaw'>, sourceId: string, key: string, field: string, request: string, hash: string): Promise<void> {diff --git a/test/belief-ledger-purge-review.test.ts b/test/belief-ledger-purge-review.test.tsindex 849dcb40d6..d7d95f70b5 100644--- a/test/belief-ledger-purge-review.test.ts+++ b/test/belief-ledger-purge-review.test.ts@@ -13,7 +13,7 @@ import { withBeliefBatch } from '../src/core/belief/ledger.ts';import { backfillBeliefHistory } from '../src/core/belief/backfill.ts';import { coverageAt, coverageIntervals } from '../src/core/belief/coverage-report.ts';import { recordDerivationInputs } from '../src/core/facts/derivation-inputs.ts';-import { redactCarryingFields, registerLiveCarriers } from '../src/core/belief/purge.ts';+import { claimForms, quotes, redactCarryingFields, registerLiveCarriers } from '../src/core/belief/purge.ts';import { REDACTION_MARKER } from '../src/core/belief/keys.ts';import { withCoordinatedWrite } from '../src/core/persistence/context.ts';import { submitRememberMutation } from '../src/core/persistence/memory-mutations.ts';@@ -187,6 +187,23 @@ for (const engineCase of testBackends().map(beliefEngineCase)) {expect(r).toEqual({ quoted: true, prior: [], value: 0 });}, 240_000);+ test('#39: a short purged claim redacts the fields that quote it as a word, not every field containing its letters', async () => {+ const r = await collect<Record<string, unknown>>(report => brain(async b => {+ await b.put('notes/short-example', beliefPage({ title: 'Short Example', facts: ['CEO'] }));+ const acme = (confidence: string) => beliefPage({ facts: [{ claim: 'Acme sells widgets', context: 'herbaceous borders sell well', confidence },+ { claim: 'Acme hired staff', context: 'the _CEO_ approved it', confidence }, { claim: 'Acme has a board', context: 'board of the CEO-office', confidence }] });+ await b.put('companies/acme-example', acme('1.0'));+ await b.put('companies/acme-example', acme('0.8'));+ await purgePage(b, 'notes/short-example');+ const contexts = Object.fromEntries((await b.events()).filter(e => e.slug === 'companies/acme-example' && e.subject_kind === 'fact')+ .map(e => [e.value?.fact, /^\[redacted: purge [0-9a-f-]+\]$/.test(String(e.value?.context)) ? 'redacted' : e.value?.context]));+ report(contexts);+ }));+ expect(r).toEqual({ 'Acme sells widgets': 'herbaceous borders sell well', 'Acme hired staff': 'redacted', 'Acme has a board': 'redacted' });+ const cases = [['CEO', 'a CEO.'], ['CEO', 'x_ceo_y'], ['CEO', 'ceo2'], ['東京', '東京都に住む'], ['Door code is 4417', 'door code is 44170'], ['ИП', 'ИПотека']];+ expect(cases.map(([claim, text]) => quotes(text!, claimForms([claim!])))).toEqual([true, true, false, true, true, false]);+ }, 240_000);+test('#14: a hard delete (purge, managed and unmanaged retention) records every cascaded inbound link as link.retracted in its own stream', async () => {const run = (managed: boolean, remove: (b: BeliefBrain, slug: string) => Promise<unknown>) => collect<string[]>(report => brain(async b => {await b.put('people/target-example', beliefPage({ type: 'person', title: 'Target Example' }));