Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix: a remote write strips every leading forgotten: prefix from a planted marker and refuses if the parser still reads one

garrytan2730257500a9
diff --git a/src/core/facts/withdrawal.ts b/src/core/facts/withdrawal.ts
index c66bdda848..25c2058692 100644
--- a/src/core/facts/withdrawal.ts
+++ b/src/core/facts/withdrawal.ts
@@ -221,8 +221,13 @@ export function remoteWithdrawalMarkersDropped(body: string, stored: readonly st
if (parsed.warnings.length) throw opError('invalid_params',
'A remote write cannot add a forget marker (a struck Facts row with context "forgotten: ..."), and this Facts table does not parse cleanly enough to drop it.',
'Remove the marker (call forget_fact to forget a claim), fix the Facts table, then submit the content with a new request_id.');
- return replaceOrInsertFactsFence(body, renderFactsTable(parsed.facts.map(row => planted(row)
- ? { ...row, forgotten: false, context: row.context?.replace(/^\s*forgotten\s*:\s*/i, '') || undefined } : row)));
+ const rewritten = replaceOrInsertFactsFence(body, renderFactsTable(parsed.facts.map(row => planted(row)
+ ? { ...row, forgotten: false, context: row.context?.replace(/^(\s*forgotten\s*:\s*)+/i, '') || undefined } : row)));
+ // Fail closed: the rewritten table is read by the parser a rebuild uses, and any marker it still finds refuses the write.
+ if (parseFactsFence(rewritten).facts.some(planted)) throw opError('invalid_params',
+ 'A remote write cannot add a forget marker (a struck Facts row with context "forgotten: ..."), and this one could not be dropped.',
+ 'Remove the marker (call forget_fact to forget a claim), then submit the content with a new request_id.');
+ return rewritten;
}
// Fingerprint each incoming claim once. Inside the join condition the planner
diff --git a/test/g4-forget-fence-edit.test.ts b/test/g4-forget-fence-edit.test.ts
index cbd215a5bd..9066160247 100644
--- a/test/g4-forget-fence-edit.test.ts
+++ b/test/g4-forget-fence-edit.test.ts
@@ -22,6 +22,7 @@ import { operationsByName } from '../src/core/operations.ts';
import { installFaultHook } from '../src/core/persistence/fault-points.ts';
import { performManagedSync } from '../src/core/persistence/sync-run.ts';
import { isFactWithdrawn } from '../src/core/facts/withdrawal.ts';
+import { parseFactsFence } from '../src/core/facts-fence.ts';
import { durableGitRepo, git } from './helpers/git-publication.ts';
import { managedBrain } from './helpers/managed-brain.ts';
import { requirePostgresTestDatabase, testBackends } from './helpers/test-backends.ts';
@@ -235,6 +236,9 @@ for (const backend of testBackends()) describe(`[${backend}] G4: forget is a fen
const copy = mkdtempSync(join(tmpdir(), 'gbrain-g4-remote-rebuild-'));
const marked = (claim: string) => `| 1 | ~~${claim}~~ | fact | 1.0 | world | medium | 2026-01-01 | ${today} | test | forgotten: no longer true |`;
const REMOTE = 'Remote planted claim g4plant', KEPT = 'Owner forgotten claim g4kept';
+ // Repeated, mixed-case and spaced prefixes: each still parses as a marker once a single prefix is removed.
+ const variants = ['forgotten: forgotten: planted', 'Forgotten : FORGOTTEN: planted', ' forgotten :forgotten:\tforgotten : planted']
+ .map((context, i) => ({ claim: `Remote variant claim g4variant${i}`, row: `| ${i + 1} | ~~Remote variant claim g4variant${i}~~ | fact | 1.0 | world | medium | 2026-01-01 | ${today} | test | ${context} |` }));
try {
await managedBrain(async ({ engine, ctx, root }) => {
const clientId = `client-${randomUUID().slice(0, 8)}`;
@@ -243,18 +247,21 @@ for (const backend of testBackends()) describe(`[${backend}] G4: forget is a fen
expect(await call(ctx, 'put_page', { slug: 'notes/kept', content: note('Kept', [marked(KEPT)]) })).toMatchObject({ state: 'committed' });
expect(await ledger(engine, KEPT)).toEqual(['notes/kept']);
expect(await call(remote, 'put_page', { slug: 'notes/remote', content: note('Remote', [marked(REMOTE)]) })).toMatchObject({ state: 'committed' });
+ expect(await call(remote, 'put_page', { slug: 'notes/variants', content: note('Variants', variants.map(v => v.row)) })).toMatchObject({ state: 'committed' });
const kept = await engine.readPageSnapshot('notes/kept', { sourceId: 'default' });
expect(await call(remote, 'put_page', { slug: 'notes/kept', expected_revision: kept!.revision,
content: note('Kept', [marked(KEPT), `| 2 | ${OTHER} | fact | 1.0 | world | medium | 2026-02-01 | | test | |`]) })).toMatchObject({ state: 'committed' });
const { settleManagedGitEffects } = await import('./helpers/managed-brain.ts');
await settleManagedGitEffects(engine);
expect(readFileSync(join(root, 'notes/remote.md'), 'utf8')).not.toContain('forgotten:');
+ expect(parseFactsFence(readFileSync(join(root, 'notes/variants.md'), 'utf8')).facts.filter(row => row.forgotten)).toEqual([]);
expect(readFileSync(join(root, 'notes/kept.md'), 'utf8')).toContain(`| ~~${KEPT}~~ |`);
execFileSync('git', ['clone', '-q', root, join(copy, 'repo')]);
}, { databaseUrl, setup });
await managedBrain(async ({ engine, ctx }) => {
expect((await performManagedSync(engine, { sourceId: 'default', noPull: true, noEmbed: true, noExtract: true })).status).toBe('first_sync');
expect(await ledger(engine, REMOTE)).toEqual([]);
+ for (const v of variants) expect(await ledger(engine, v.claim)).toEqual([]);
expect(await ledger(engine, KEPT)).toEqual(['notes/kept']);
expect(await call(ctx, 'remember', { fact: REMOTE, provenance: 'test', visibility: 'world', entity: 'notes/remote' })).toMatchObject({ state: 'committed' });
}, { databaseUrl, setup: ({ root }) => { rmSync(root, { recursive: true, force: true }); execFileSync('git', ['clone', '-q', join(copy, 'repo'), root]); } });