Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(import): store the parse-normal body after restoring hidden fact rows (#6545)

time-attack4cbfa55ee89c
diff --git a/src/core/import-file.ts b/src/core/import-file.ts
index 9c32cd3d1f..068f5f67e7 100644
--- a/src/core/import-file.ts
+++ b/src/core/import-file.ts
@@ -446,6 +446,10 @@ export async function importFromContent(
const { assertPreparedFactWithdrawals, preserveWithdrawnFenceRows } = await import('./facts/withdrawal.ts');
parsed.compiled_truth = await preserveWithdrawnFenceRows(engine, sourceId ?? 'default', parsed.compiled_truth, slug);
parsed.timeline = await preserveWithdrawnFenceRows(engine, sourceId ?? 'default', parsed.timeline, slug);
+ // #6545: the rewrites above run after parseMarkdown trimmed the body (a fence insert appends "\n"); store the
+ // parse-normal form of the file this row serializes to, so the canonical file still matches its own row.
+ parsed.compiled_truth = parsed.compiled_truth.trim();
+ parsed.timeline = parsed.timeline.trim();
// #1035: absence of an explicit frontmatter `type:` on an EXISTING page
// means "preserve the stored type", not "re-infer". Pre-fix, a round-trip
diff --git a/src/core/persistence/protected-takes.ts b/src/core/persistence/protected-takes.ts
index baca6dd954..0055838913 100644
--- a/src/core/persistence/protected-takes.ts
+++ b/src/core/persistence/protected-takes.ts
@@ -26,5 +26,5 @@ export function preserveProtectedTakes(incoming:string,stored:string):string {
if(!before.read) throw opError('invalid_params','This page quotes a takes fence inside markdown code, so a remote replacement cannot preserve it.',
'Ask the user to replace this page with a local write (gbrain put on the brain host), or send the page with only the prose you mean to change through a scoped edit.',
{ why: 'Remote reads hide the quoted fence; re-appending it at the end of the page would move it out of the code block and turn the example into a live takes fence.' });
- return `${incoming.trimEnd()}\n\n${before.text}\n`;
+ return `${incoming.trimEnd()}\n\n${before.text}`;
}
diff --git a/test/import-hidden-fact-restore-6545.test.ts b/test/import-hidden-fact-restore-6545.test.ts
new file mode 100644
index 0000000000..3728b21606
--- /dev/null
+++ b/test/import-hidden-fact-restore-6545.test.ts
@@ -0,0 +1,59 @@
+// #6545: a remote put_page that restores privacy-hidden fact rows stored a
+// body ending in "\n" (the fence insert appends one after parsing), while
+// the canonical file it wrote reparses trimmed. The DB row then never
+// matched its own file, so every later coordinated write on the page was
+// refused `source_changed` until the next sync reimported it.
+import { expect, test } from 'bun:test';
+import { readFileSync } from 'node:fs';
+import { join } from 'node:path';
+import { parseMarkdown } from '../src/core/markdown.ts';
+import { FACTS_FENCE_BEGIN, FACTS_FENCE_END, parseFactsFence } from '../src/core/facts-fence.ts';
+import { operations } from '../src/core/operations.ts';
+import type { OperationContext } from '../src/core/ops/contract.ts';
+import { preserveProtectedTakes } from '../src/core/persistence/protected-takes.ts';
+import { managedBrain } from './helpers/managed-brain.ts';
+
+const SLUG = 'projects/x';
+const PRIVATE_FENCE = `# X
+
+Body.
+
+## Facts
+
+${FACTS_FENCE_BEGIN}
+| # | claim | kind | confidence | visibility | notability | valid_from | valid_until | source | context |
+|---|-------|------|------------|------------|------------|------------|-------------|--------|---------|
+| 1 | Secret claim | fact | 1.0 | private | low | | | test | |
+${FACTS_FENCE_END}
+`;
+
+const op = (name: string) => operations.find(o => o.name === name)!;
+
+async function put(ctx: OperationContext, content: string) {
+ const current = await ctx.engine.readPageSnapshot(SLUG, { sourceId: 'default' });
+ return op('put_page').handler(ctx, { slug: SLUG, content, ...(current ? { expected_revision: current.revision } : {}) });
+}
+
+test('#6545 remote put_page restoring a hidden fact row stores the parse-normal body and later writes succeed', async () => {
+ await managedBrain(async ({ engine, ctx, root }) => {
+ await put(ctx, `---\ntype: project\ntitle: X\n---\n\n${PRIVATE_FENCE}`);
+ const remote = { ...ctx, remote: true } as OperationContext;
+ await put(remote, `---\ntype: project\ntitle: X\n---\n\n# X\n\nBody edited.\n\n<!-- timeline -->\n\n## Timeline\n\n- **2026-10-12** | s — e\n`);
+
+ const page = await engine.getPage(SLUG, { sourceId: 'default' });
+ expect(parseFactsFence(page!.compiled_truth).facts.map(f => f.claim)).toEqual(['Secret claim']);
+ const file = parseMarkdown(readFileSync(join(root, `${SLUG}.md`), 'utf8'), `${SLUG}.md`);
+ await op('add_timeline_entry').handler(remote, { slug: SLUG, date: '2026-10-13', summary: 'follow-up' });
+ const after = await engine.getPage(SLUG, { sourceId: 'default' });
+ expect(after!.timeline).toContain('follow-up');
+ expect(page!.compiled_truth).toBe(file.compiled_truth);
+ expect(page!.timeline).toBe(file.timeline);
+ });
+}, 60_000);
+
+test('#6545 preserveProtectedTakes returns trimEnd-normal output', () => {
+ const next = '# X\n\nBody.';
+ const current = `# X\n\n<!--- gbrain:takes:begin -->\n| # | claim | kind | who | weight | since | source |\n|---|---|---|---|---|---|---|\n| 1 | t | take | people/a | 0.5 | 2026-01 | s |\n<!--- gbrain:takes:end -->\n`;
+ const out = preserveProtectedTakes(next, current);
+ expect(out).toBe(out.trimEnd());
+});