Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(content-sanity): gbrain facts/takes fences are not markup (#5822)
garrytanaccf339a3d14
diff --git a/src/core/content-sanity.ts b/src/core/content-sanity.tsindex 018043ec5c..68a820160d 100644--- a/src/core/content-sanity.ts+++ b/src/core/content-sanity.ts@@ -279,6 +279,9 @@ export interface ProseAssessment {// and excluded from the denominator entirely (Codex #2 — a code-heavy doc// must not read as high-markup). The remaining strips count toward markup.const FENCED_CODE_RE = /```[\s\S]*?```|~~~[\s\S]*?~~~/g;+// gbrain's own facts/takes fences (#5822): their table cells are the page's+// structured facts, written by gbrain itself, so they are excluded like code.+const GBRAIN_FENCE_RE = /<!--- gbrain:([a-z_-]+):begin -->[\s\S]*?<!--- gbrain:\1:end -->/g;const INLINE_CODE_RE = /`[^`\n]*`/g;const HTML_TAG_RE = /<\/?[a-z][^>]*>/gi;const MD_IMAGE_RE = /!\[[^\]]*\]\([^)]*\)/g;@@ -291,7 +294,7 @@ const MD_EMPHASIS_RE = /[*_~]{1,3}/g;const TABLE_PIPE_RE = /\|/g;/**- * Pure prose-vs-markup assessment. Strips code (excluded from the ratio),+ * Pure prose-vs-markup assessment. Strips code and gbrain facts/takes fences (excluded from the ratio),* then measures how much of the REMAINING content is markup syntax vs real* sentences. Returns a ratio in [0, 1]; high = boilerplate/nav shape.*@@ -300,8 +303,8 @@ const TABLE_PIPE_RE = /\|/g;* than catching the obvious nav-blob shape without nuking legit prose.*/export function assessProse(body: string): ProseAssessment {- // Code excluded from the denominator (Codex #2): a code doc isn't junk.- const noCode = body.replace(FENCED_CODE_RE, ' ').replace(INLINE_CODE_RE, ' ');+ // Code and gbrain-managed fences are excluded from the denominator (Codex #2): a code doc isn't junk.+ const noCode = body.replace(GBRAIN_FENCE_RE, ' ').replace(FENCED_CODE_RE, ' ').replace(INLINE_CODE_RE, ' ');const total_chars = noCode.replace(/\s+/g, '').length;if (total_chars === 0) {return { prose_chars: 0, total_chars: 0, markup_ratio: 0 };diff --git a/test/content-sanity-gbrain-fences-5822.test.ts b/test/content-sanity-gbrain-fences-5822.test.tsnew file mode 100644index 0000000000..333990d76d--- /dev/null+++ b/test/content-sanity-gbrain-fences-5822.test.ts@@ -0,0 +1,81 @@+/**+ * #5822: gbrain's own facts/takes fences do not count as markup.+ *+ * Protects: a page whose body is mostly the facts (or takes) fence gbrain+ * wrote is not flagged `markup_heavy` on import, while the same table shape+ * written as ordinary page markup still is.+ * Fails when: the prose pass measures fence rows as table markup, so the+ * fence share becomes the page's markup ratio.+ * Seams: none; in-memory PGLite for the import path, isolated GBRAIN_HOME.+ */+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';+import { mkdtempSync, rmSync } from 'node:fs';+import { join } from 'node:path';+import { tmpdir } from 'node:os';+import { assessContentSanity, assessProse } from '../src/core/content-sanity.ts';+import { FACTS_FENCE_BEGIN, FACTS_FENCE_END, renderFactsTable } from '../src/core/facts-fence.ts';+import { TAKES_FENCE_BEGIN, TAKES_FENCE_END, renderTakesFence } from '../src/core/takes-fence.ts';+import { importFromContent } from '../src/core/import-file.ts';+import { getContentFlag } from '../src/core/quarantine.ts';+import { PGLiteEngine } from '../src/core/pglite-engine.ts';+import { withEnv } from './helpers/with-env.ts';++const PROSE = '# Example Co\n\n' + 'Example Co makes kitchen tools and sells them online and through two retail partners. '.repeat(30);+const facts = renderFactsTable(Array.from({ length: 300 }, (_, i) => ({+ rowNum: i + 1, claim: `Example Co reported quarterly figure ${i + 1} in a public update about its product line`,+ kind: 'fact' as const, confidence: 0.9, visibility: 'world' as const, notability: 'medium' as const,+ source: `email/example-thread-${i + 1}`, context: 'status update about operations', active: true,+})));+const takes = renderTakesFence(Array.from({ length: 500 }, (_, i) => ({+ rowNum: i + 1, claim: `Example Co will expand retail partner ${i + 1} within the year`, kind: 'bet',+ holder: 'world', weight: 0.6, source: `notes/example-${i + 1}`, active: true,+})));+const FACTS_BODY = `${PROSE}\n\n## Facts\n\n${facts}\n`;+const TAKES_BODY = `${PROSE}\n\n## Takes\n\n${takes}\n`;++describe('prose pass', () => {+ test('the fences this test builds carry the shared marker pairs', () => {+ expect(facts).toContain(FACTS_FENCE_BEGIN);+ expect(facts).toContain(FACTS_FENCE_END);+ expect(takes).toContain(TAKES_FENCE_BEGIN);+ expect(takes).toContain(TAKES_FENCE_END);+ });++ test('a facts-fence-heavy page is not markup_heavy', () => {+ expect(Buffer.byteLength(FACTS_BODY)).toBeGreaterThan(50_000);+ expect(assessProse(FACTS_BODY).markup_ratio).toBeLessThan(0.1);+ expect(assessContentSanity({ compiled_truth: FACTS_BODY, timeline: '', title: 'Example Co' }).flag_reason).toBeNull();+ });++ test('a takes-fence-heavy page is not markup_heavy', () => {+ expect(Buffer.byteLength(TAKES_BODY)).toBeGreaterThan(50_000);+ expect(assessContentSanity({ compiled_truth: TAKES_BODY, timeline: '', title: 'Example Co' }).flag_reason).toBeNull();+ });++ test('the same table outside a gbrain fence is still markup_heavy, and bytes still count the fence', () => {+ const unfenced = FACTS_BODY.replace(FACTS_FENCE_BEGIN, '').replace(FACTS_FENCE_END, '');+ expect(assessContentSanity({ compiled_truth: unfenced, timeline: '', title: 'Example Co' }).flag_reason).toBe('markup_heavy');+ expect(assessContentSanity({ compiled_truth: FACTS_BODY, timeline: '', title: 'Example Co' }).bytes).toBe(Buffer.byteLength(FACTS_BODY));+ });+});++describe('import', () => {+ let engine: PGLiteEngine;+ let home: string;+ beforeAll(async () => {+ engine = new PGLiteEngine();+ await engine.connect({}); await engine.initSchema();+ home = mkdtempSync(join(tmpdir(), 'cs-5822-home-'));+ }, 120_000);+ afterAll(async () => { await engine.disconnect(); rmSync(home, { recursive: true, force: true }); });++ test('importing a facts-fence-heavy company page sets no content_flag', async () => {+ await withEnv({ GBRAIN_HOME: home, GBRAIN_AUDIT_DIR: home }, async () => {+ const result = await importFromContent(engine, 'companies/example-co', `---\ntype: company\ntitle: Example Co\n---\n${FACTS_BODY}`, { noEmbed: true });+ expect(result.status).toBe('imported');+ expect(result.flagged).toBeUndefined();+ const page = await engine.getPage('companies/example-co');+ expect(getContentFlag(page!.frontmatter as Record<string, unknown>)).toBeNull();+ });+ });+});