Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix: remember linking an unattributed fact refuses a malformed memory/unattributed fence instead of dropping its rows

garrytanb774df8aa5a3
diff --git a/src/core/facts/fence-fact-append.ts b/src/core/facts/fence-fact-append.ts
index a61d96a36c..246f0f5c1c 100644
--- a/src/core/facts/fence-fact-append.ts
+++ b/src/core/facts/fence-fact-append.ts
@@ -38,6 +38,7 @@ import type { CompanionPage, PreparedMutation } from '../persistence/coordinator
import type { WriteRequest } from '../persistence/model.ts';
import { normalizeTargetFences } from '../fence-repair/import-step.ts';
import { refuseUnparsedRewrite } from '../fence-repair/refusal.ts';
+import { assertFenceRewritable } from '../fence-shared.ts';
import { pageFencesNormalized } from '../fence-repair/report.ts';
import { withPendingFenceRows } from '../eligibility/fence-overlay.ts';
import { withPageTierKept } from '../trust/fence-append.ts';
@@ -197,11 +198,15 @@ export async function prepareFenceRowRetirement(engine: BrainEngine, row: WriteR
if (!snapshot || snapshot.page.deleted_at) return null;
if (segmentedOverCeiling(snapshot.page)) throw pageBodyOverCeilingError(snapshot.page, `${row.operation} facts fence write`);
const base = await materializedPage(engine, snapshot.page);
- const fenced = { compiled_truth: base.compiled_truth, timeline: base.timeline ?? '' };
+ // #6188 / #6385 R12, as the append: the stored fence is normalized first and a residual refuses typed instead of losing the rows it skipped.
+ const target = await normalizeTargetFences(engine, { sourceId: row.source_id, slug, kind: 'facts', page: base });
+ const fenced = { compiled_truth: target.page.compiled_truth, timeline: target.page.timeline ?? '' };
const section = (['compiled_truth', 'timeline'] as const).find(key => parseFactsFence(fenced[key]).facts.some(f => f.rowNum === retire.rowNum && f.active));
if (!section) return null;
const until = retire.validUntil ? formatFenceDate(retire.validUntil) : undefined;
- const rows = parseFactsFence(fenced[section]).facts.map(f => f.rowNum === retire.rowNum
+ const parsed = parseFactsFence(fenced[section]);
+ refuseUnparsedRewrite(target.page, slug, row.source_id, () => assertFenceRewritable('facts', parsed));
+ const rows = parsed.facts.map(f => f.rowNum === retire.rowNum
? { ...f, active: false, context: [retire.note, f.context?.trim()].filter(Boolean).join(' | '), ...(until ? { validUntil: until } : {}) } : f);
const content = serializePageToMarkdown({ ...base, ...fenced, [section]: replaceOrInsertFactsFence(fenced[section], renderFactsTable(rows)) }, snapshot.tags);
const prepared = await preparePageMutation(engine, { ...row, slug, page_id: snapshot.page.id,
diff --git a/test/fence-rewrite-refuses-malformed.test.ts b/test/fence-rewrite-refuses-malformed.test.ts
index d2fda5e96d..a5bce79fba 100644
--- a/test/fence-rewrite-refuses-malformed.test.ts
+++ b/test/fence-rewrite-refuses-malformed.test.ts
@@ -233,6 +233,23 @@ describe('coordinated writers refuse typed and keep the stored page', () => {
expect(await stored(slug)).toBe(body);
}));
+ test('remember linking a fact saved without an entity refuses typed and keeps a malformed memory/unattributed row', () => withEnv({ GBRAIN_HOME: home }, async () => {
+ const entity = 'people/linker-example';
+ const claim = 'Keeps a rooftop beehive retirezq7';
+ await op('put_page').handler(ctx(), { slug: entity, content: page('Linker', 'No facts yet.') });
+ const unlinked = await op('remember').handler(ctx(), { fact: claim, provenance: 'chat', visibility: 'world', infer_entity: false }) as Record<string, any>;
+ expect(unlinked.entity_slug).toBeNull();
+ const body = (await stored('memory/unattributed')).replace(FE, `${BAD}\n${FE}`);
+ await engine.transaction(tx => withCoordinatedWrite(tx, ['default'], () => tx.executeRaw(
+ 'UPDATE pages SET compiled_truth=$3 WHERE source_id=$1 AND slug=$2', ['default', 'memory/unattributed', body]), TEST_WRITE_ATTRIBUTION));
+ const error = await refusal(() => op('remember').handler(ctx(), { fact: claim, provenance: 'chat', visibility: 'world', entity, replaces: unlinked.id })).catch(e => e);
+ expect(await stored('memory/unattributed')).toContain('Sentinelrowzq9');
+ expect({ detail: error.detail, reason: error.reason }).toEqual({ detail: 'invalid_fence', reason: 'target_fence_malformed' });
+ expect(await stored('memory/unattributed')).toBe(body);
+ expect(await stored(entity)).not.toContain(claim);
+ expect(await engine.executeRaw('SELECT expired_at FROM facts WHERE id=$1', [Number(unlinked.id)])).toEqual([{ expired_at: null }]);
+ }));
+
test('managed phantom redirect: a fence that does not parse on either side is drift and both pages stay as they were', () => withEnv({ GBRAIN_HOME: home, GBRAIN_AUDIT_DIR: join(home, 'audit') }, async () => {
const cleanCanonical = `# Seed\n\nSynthetic prose.\n\n${facts(GOOD)}\n`;
const brokenCanonical = `# Seed\n\nSynthetic prose.\n\n${facts(GOOD, BAD)}\n`;