Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
test(fences): legacy sync normalization and fence_issues, put_pages report, company UC3 fence message (#6188)
garrytan8fc8d53988ad
diff --git a/src/core/company-brain/profile.ts b/src/core/company-brain/profile.tsindex 52f839f4e6..589593ae5a 100644--- a/src/core/company-brain/profile.ts+++ b/src/core/company-brain/profile.ts@@ -116,7 +116,8 @@ export async function importCompanyBrainFile(engine: BrainEngine, filePath: striif (digest(canonical(parsed, parsed.tags)) !== digest(canonical(ready.parsedPage, tags))) {// #6188 (UC3): a fence Tier 1 would normalize is named by location; the repository commit is the only fix.const fences = ready.result.fences_normalized ?? [];- throw opError('source_writeback_required', 'Canonical preparation requires a source-content correction; this profile never writes repository files.',+ throw opError('source_writeback_required', fences.length ? `Canonical preparation would normalize a facts or takes fence (${describeFixes(fences)}); this profile never writes repository files.`+ : 'Canonical preparation requires a source-content correction; this profile never writes repository files.',fences.length ? `${path} has a facts or takes fence gbrain would normalize (${describeFixes(fences)}), and this profile never edits repository files. Fix the fence in the repository and commit, then resume with ${resumeCommand(sourceId)}.`: `${path} would change when published canonically (for example normalized frontmatter or tags), and this profile never edits repository files. Review the needed correction with the user, commit it, then resume with ${resumeCommand(sourceId)}.`);}diff --git a/src/core/persistence/sync-prepare.ts b/src/core/persistence/sync-prepare.tsindex 003ed8e7c6..ef6da2f984 100644--- a/src/core/persistence/sync-prepare.ts+++ b/src/core/persistence/sync-prepare.ts@@ -197,7 +197,9 @@ async function settledSyncScreen(engine: BrainEngine, input: SyncImportScreenInp/** UC3: a company-brain source never rewrites repository files; a fence Tier 1 would normalize is named by location. */function companyWritebackRefusal(row: WriteRequest, p: SyncIntent, fenceFixes: readonly FenceFix[]): OperationError {- return syncPublicationRefusal('source_writeback_required', 'Canonical preparation requires a source-content correction; this profile never writes repository files.', row, p,+ const message = fenceFixes.length ? `Canonical preparation would normalize a facts or takes fence (${describeFixes(fenceFixes)}); this profile never writes repository files.`+ : 'Canonical preparation requires a source-content correction; this profile never writes repository files.';+ return syncPublicationRefusal('source_writeback_required', message, row, p,fenceFixes.length ? `${p.sourcePath} has a facts or takes fence gbrain would normalize (${describeFixes(fenceFixes)}), and a company-brain source never rewrites repository files; fix the fence in the repository and commit.`: `The file of ${row.slug} needs a canonical correction, and a company-brain source never rewrites repository files; correct it in the repository and commit.`);}diff --git a/src/core/persistence/verb-errors.ts b/src/core/persistence/verb-errors.tsindex d7aed3eb1c..73fc36b13b 100644--- a/src/core/persistence/verb-errors.ts+++ b/src/core/persistence/verb-errors.ts@@ -102,6 +102,9 @@ export function writeFailureDiagnostic(code: string, message?: string | null): {if (fence) return { reason: 'invalid_fence', message: message!,suggestion: `Edit ${fenceWhere(fence)} in the file as the message says and commit the change; never edit the frontmatter for it. `+ 'A managed sync with sync.holds=hold holds such a file instead of blocking; under sync.holds=fail and on company-brain sources it blocks until the file is fixed.' };+ // #6188 (UC3): a company-brain source names the fence correction it will not write; the repository commit is the fix.+ if (code === 'source_writeback_required' && message?.startsWith('Canonical preparation would normalize a facts or takes fence (')) return { reason: code, message,+ suggestion: 'A company-brain source never rewrites repository files: fix the named fence in the repository, commit it, and resume the sync.' };const replaces = code === 'invalid_params' ? REPLACES_REFUSAL.exec(message ?? '') : null;if (replaces) return { reason: code, message: message!, suggestion: REPLACES_SUGGESTION[replaces[1]!]! };return { reason: isWriteErrorCode(code) ? code : 'storage_error', message: 'The write did not commit. Inspect its durable request on the source host.',diff --git a/test/fence-write-verbs.test.ts b/test/fence-write-verbs.test.tsindex e7bffa5d71..1af914c6fc 100644--- a/test/fence-write-verbs.test.ts+++ b/test/fence-write-verbs.test.ts@@ -138,6 +138,19 @@ describe('put_page', () => {}));});+test('put_pages reports one batch-level fences_normalized and one notice for the pages Tier 1 rewrote', () => withEnv(env, async () => {+ const notices: Notice[] = [];+ const result = await op('put_pages').handler(ctx({ notices }), { request_id: crypto.randomUUID(), pages: [+ { slug: 'people/batch-fixable', content: page('Batch fixable', factsFence(fact(0, 'Zero row claim'))) },+ { slug: 'people/batch-clean', content: page('Batch clean', factsFence(fact(1, 'Clean claim'))) },+ ] }) as Record<string, any>;+ expect(result.state).toBe('committed');+ expect(result.fences_normalized).toMatchObject({ count: 1, by_class: { renumber: 1 } });+ expect(result.pages.map((p: Record<string, unknown>) => !!p.fences_normalized)).toEqual([true, false]);+ expect(notices.filter(n => n.code === 'fence_normalized')).toHaveLength(1);+ expect(parseFactsFence(await stored('people/batch-fixable')).facts.map(f => f.rowNum)).toEqual([1]);+}));+describe('append verbs normalize their target fence in the same write (D20)', () => {test('remember on a page whose facts fence lost its end marker saves the fact and reports close_fence', () => withEnv(env, async () => {const slug = 'people/remember-target';diff --git a/test/persistence-sync-company.serial.test.ts b/test/persistence-sync-company.serial.test.tsindex ccb53ba7ca..5b44190261 100644--- a/test/persistence-sync-company.serial.test.ts+++ b/test/persistence-sync-company.serial.test.ts@@ -196,3 +196,22 @@ check('a company source with a malformed fence blocks with the typed fence refusexpect(await engine.executeRaw("SELECT 1 FROM op_checkpoints WHERE op='sync-hold' AND fingerprint LIKE $1", [`${f.sourceId}:%`])).toEqual([]);await disposePersistenceConsumer(engine);});++// #6188 (UC3): a company-profile source never rewrites repository files, so a fence Tier 1 could fix refuses+// source_writeback_required naming the fence location (never a value), and nothing is held or rewritten.+check('a company source with a fixable fence refuses source_writeback_required naming the fence, and nothing is held or rewritten', async engine => {+ const fence = '<!--- gbrain:facts:begin -->\n| # | claim | kind | confidence | visibility | notability | valid_from | valid_until | source | context |\n|---|---|---|---|---|---|---|---|---|---|\n'+ + '| 1 | Sentinelcompanyzq8 claim | partnership | 1.0 | private | medium | 2026-01-01 | | chat | |\n<!--- gbrain:facts:end -->\n';+ const f = await input(engine, true, { 'customers/fixable.md': `---\ntype: customer\ntitle: Fixable Account\n---\n# Fixable Account\nA synthetic account.\n\n${fence}` });+ const connected = await connectCompanyBrain(engine, f);+ expect(connected.ok).toBe(false);+ const failed = await engine.executeRaw<{ error_code: string; error_message: string }>("SELECT error_code,error_message FROM persistence_requests WHERE source_id=$1 AND state IN ('failed','conflict')", [f.sourceId]);+ expect(failed.map(r => r.error_code)).toEqual(['source_writeback_required']);+ expect(await engine.executeRaw("SELECT 1 FROM op_checkpoints WHERE op='sync-hold' AND fingerprint LIKE $1", [`${f.sourceId}:%`])).toEqual([]);+ const blocked = await performSync(engine, { sourceId: f.sourceId });+ expect(blocked).toMatchObject({ status: 'blocked_by_failures' });+ expect(failed[0]!.error_message).toBe('Canonical preparation would normalize a facts or takes fence (kind_map row 1 column kind (facts, body)); this profile never writes repository files.');+ expect(blocked.managedWrite?.message ?? '').toContain('kind_map row 1 column kind (facts, body)');+ for (const text of [JSON.stringify(failed), JSON.stringify(blocked)]) { expect(text).not.toContain('Sentinelcompanyzq8'); expect(text).not.toContain('partnership'); }+ await disposePersistenceConsumer(engine);+});diff --git a/test/sync-legacy-holds.test.ts b/test/sync-legacy-holds.test.tsindex f224482f3a..acd326bffd 100644--- a/test/sync-legacy-holds.test.ts+++ b/test/sync-legacy-holds.test.ts@@ -323,3 +323,21 @@ test('#6188 (T5): legacy sync imports a page with a malformed fence as before an// Stored as written: the legacy path never refuses or rewrites a fence.expect((await engine.getPage('notes/fenced', { sourceId: id }))?.compiled_truth).toContain('| 2 | Another take | sentinelkindzq7 |');}));++test('#6188 (T5): legacy sync stores a fixable fence normalized (the file is untouched) and reports a residual one as fence_issues', async () => eachEngine(async () => {+ const facts = '<!--- gbrain:facts:begin -->\n| # | claim | kind | confidence | visibility | notability | valid_from | valid_until | source | context |\n|---|---|---|---|---|---|---|---|---|---|\n'+ + '| 1 | Sentinellegacyzq8 claim | partnership | 1.0 | private | medium | 2026-01-01 | | chat | |\n<!--- gbrain:facts:end -->\n';+ const residual = '<!--- gbrain:takes:begin -->\n| # | claim | kind | who | weight | since | source |\n|---|---|---|---|---|---|---|\n'+ + '| 1 | Synthetic take | sentinelkindzq7 | brain | 0.5 | 2026-01 | chat |\n<!--- gbrain:takes:end -->\n';+ const fixableFile = `---\ntitle: Fixable\n---\nA synthetic page.\n\n${facts}`;+ const { id, root } = await source({ 'notes/fixable.md': fixableFile, 'notes/residual.md': `---\ntitle: Residual\n---\nA synthetic page.\n\n${residual}`, 'notes/ok.md': good('Ok') });+ const result = await sync(id, root);+ expect(result.status).toBe('first_sync');+ expect(await holds(id)).toEqual([]);+ expect(result.fences_normalized).toMatchObject({ count: 1, by_class: { kind_map: 1 }, sample_paths: ['notes/fixable.md'] });+ expect(result.fence_issues).toMatchObject({ files: 1, sample: [{ path: 'notes/residual.md', fence_issues: [{ fence: 'takes', row: 1, column: 'kind', class: 'takes_kind_unsupported' }] }] });+ expect((await engine.getPage('notes/fixable', { sourceId: id }))?.compiled_truth).toContain('| fact |');+ expect(execFileSync('cat', [join(root, 'notes/fixable.md')], { encoding: 'utf8' })).toBe(fixableFile);+ const out = JSON.stringify(syncHoldJsonFields(result)) + printed(result);+ for (const secret of ['Sentinellegacyzq8', 'partnership', 'sentinelkindzq7']) expect(out).not.toContain(secret);+}));