Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(security): normalize CR/CRLF/U+2028/U+2029 before neutralizing outside text; seed connector tests through brain-owned doors; keep fence_integrity next to search_mode
garrytan4ae3e87a20a2
diff --git a/src/commands/doctor/registry.ts b/src/commands/doctor/registry.tsindex 62851a09e7..1d81218a8a 100644--- a/src/commands/doctor/registry.ts+++ b/src/commands/doctor/registry.ts@@ -298,9 +298,9 @@ export const DOCTOR_CHECK_REGISTRY: readonly DoctorEntry[] = [dbAheadOfGitEntry,// Git-canonical G3: how far each strict source's index trails the commit at HEAD.gitIndexLagEntry,- fenceIntegrityEntry,// GBRA-221: live fence rows on imported pages whose body is outside-authored text (review only).outsideContentFencesEntry,+ fenceIntegrityEntry,searchModeEntry,];diff --git a/src/core/outside-content.ts b/src/core/outside-content.tsindex 11121b9a39..c51ce26cac 100644--- a/src/core/outside-content.ts+++ b/src/core/outside-content.ts@@ -19,6 +19,8 @@* fence-repair near-miss recognizer and its `gbrain:facts:` pre-check, matches the exact token, so no marker of any* kind survives and Tier 1 repair cannot upgrade a near miss into a live marker.* - `[Source:` → `[Source\:`, so no inline citation files a timeline entry.+ * - CR, CRLF, U+2028 and U+2029 become LF first: the multiline (`/m`) timeline extractors treat each as a line start,+ * so a line test on `\n` alone would let a separator-joined row through.* - A backslash before each line that is a timeline sentinel, a bare `## Timeline` / `## History` heading, a dated* timeline row or heading, a transcript message anchor or a date heading, using the parsers' own predicates.*/@@ -61,6 +63,7 @@ function isStructuralLine(line: string): boolean {/** Outside-authored text with no live gbrain marker, citation, timeline split or row, or transcript anchor. */export function neutralizeOutsideText(text: string): string {return text+ .replace(/\r\n?|[\u2028\u2029]/g, '\n').replace(MARKER_TOKEN_RE, (_m, word: string, ws: string) => `${word}${ws}\\:`).replace(/\[Source:/g, '[Source\\:').split('\n')diff --git a/test/fixtures/goldens/doctor/content-pglite-human.json b/test/fixtures/goldens/doctor/content-pglite-human.jsonindex 2bc13120cf..0f008293f0 100644--- a/test/fixtures/goldens/doctor/content-pglite-human.json+++ b/test/fixtures/goldens/doctor/content-pglite-human.json@@ -234,6 +234,7 @@" [OK] person_title_duplicates: No title-prefixed person page pairs with an unprefixed page (0 titled page(s) examined)."," [OK] db_ahead_of_git: Informational, nothing to do: the database compared with the commit at HEAD, per source with a Git checkout. notes: 5 equal. Page detail: gbrain sources git-gap <source-id>."," [OK] git_index_lag: No source is strict (git-canonical), so no index trails a commit.",+ " [OK] outside_content_fences: No fact, take or link is indexed from a fence on an imported email, calendar, GitHub or transcript page."," [OK] fence_integrity: No malformed facts or takes fence is held, stored or waiting in a checkout (2 source(s) scanned)."," [OK] search_mode: Mode: conservative (no per-key overrides — mode bundle is canonical)."," [OK] hidden_by_search_policy: No pages hidden by search-exclude policy.",diff --git a/test/fixtures/goldens/doctor/content-pglite-json.json b/test/fixtures/goldens/doctor/content-pglite-json.jsonindex a91ded81fa..42a2fbc6e3 100644--- a/test/fixtures/goldens/doctor/content-pglite-json.json+++ b/test/fixtures/goldens/doctor/content-pglite-json.json@@ -3593,6 +3593,18 @@"name": "git_index_lag","status": "ok"},+ {+ "category": "brain",+ "details": {+ "docs": "docs/guides/repair.md#outside-content-fences",+ "items": [],+ "pages": 0+ },+ "elapsed_ms": "<volatile>",+ "message": "No fact, take or link is indexed from a fence on an imported email, calendar, GitHub or transcript page.",+ "name": "outside_content_fences",+ "status": "ok"+ },{"category": "brain","details": {diff --git a/test/fixtures/goldens/doctor/hot-checks-pglite-json.json b/test/fixtures/goldens/doctor/hot-checks-pglite-json.jsonindex 38296aff3e..c7008ff35a 100644--- a/test/fixtures/goldens/doctor/hot-checks-pglite-json.json+++ b/test/fixtures/goldens/doctor/hot-checks-pglite-json.json@@ -3816,6 +3816,18 @@"name": "git_index_lag","status": "ok"},+ {+ "category": "brain",+ "details": {+ "docs": "docs/guides/repair.md#outside-content-fences",+ "items": [],+ "pages": 0+ },+ "elapsed_ms": "<volatile>",+ "message": "No fact, take or link is indexed from a fence on an imported email, calendar, GitHub or transcript page.",+ "name": "outside_content_fences",+ "status": "ok"+ },{"category": "brain","details": {@@ -8562,6 +8574,18 @@"name": "git_index_lag","status": "ok"},+ {+ "category": "brain",+ "details": {+ "docs": "docs/guides/repair.md#outside-content-fences",+ "items": [],+ "pages": 0+ },+ "elapsed_ms": "<volatile>",+ "message": "No fact, take or link is indexed from a fence on an imported email, calendar, GitHub or transcript page.",+ "name": "outside_content_fences",+ "status": "ok"+ },{"category": "brain","details": {diff --git a/test/fixtures/goldens/doctor/hot-checks-postgres-json.json b/test/fixtures/goldens/doctor/hot-checks-postgres-json.jsonindex f0d4b32c6b..b5ca713860 100644--- a/test/fixtures/goldens/doctor/hot-checks-postgres-json.json+++ b/test/fixtures/goldens/doctor/hot-checks-postgres-json.json@@ -3819,6 +3819,18 @@"name": "git_index_lag","status": "ok"},+ {+ "category": "brain",+ "details": {+ "docs": "docs/guides/repair.md#outside-content-fences",+ "items": [],+ "pages": 0+ },+ "elapsed_ms": "<volatile>",+ "message": "No fact, take or link is indexed from a fence on an imported email, calendar, GitHub or transcript page.",+ "name": "outside_content_fences",+ "status": "ok"+ },{"category": "brain","details": {@@ -8566,6 +8578,18 @@"name": "git_index_lag","status": "ok"},+ {+ "category": "brain",+ "details": {+ "docs": "docs/guides/repair.md#outside-content-fences",+ "items": [],+ "pages": 0+ },+ "elapsed_ms": "<volatile>",+ "message": "No fact, take or link is indexed from a fence on an imported email, calendar, GitHub or transcript page.",+ "name": "outside_content_fences",+ "status": "ok"+ },{"category": "brain","details": {diff --git a/test/fixtures/goldens/doctor/json-pglite-degraded.json b/test/fixtures/goldens/doctor/json-pglite-degraded.jsonindex ebc584033f..8ee2699d13 100644--- a/test/fixtures/goldens/doctor/json-pglite-degraded.json+++ b/test/fixtures/goldens/doctor/json-pglite-degraded.json@@ -3261,6 +3261,18 @@"name": "git_index_lag","status": "ok"},+ {+ "category": "brain",+ "details": {+ "docs": "docs/guides/repair.md#outside-content-fences",+ "items": [],+ "pages": 0+ },+ "elapsed_ms": "<volatile>",+ "message": "No fact, take or link is indexed from a fence on an imported email, calendar, GitHub or transcript page.",+ "name": "outside_content_fences",+ "status": "ok"+ },{"category": "brain","details": {@@ -3343,18 +3355,6 @@"name": "fence_integrity","status": "ok"},- {- "category": "brain",- "details": {- "docs": "docs/guides/repair.md#outside-content-fences",- "items": [],- "pages": 0- },- "elapsed_ms": "<volatile>",- "message": "No fact, take or link is indexed from a fence on an imported email, calendar, GitHub or transcript page.",- "name": "outside_content_fences",- "status": "ok"- },{"category": "ops","elapsed_ms": "<volatile>",diff --git a/test/fixtures/goldens/doctor/json-pglite-fresh.json b/test/fixtures/goldens/doctor/json-pglite-fresh.jsonindex 825a5c5c01..0ddea282b6 100644--- a/test/fixtures/goldens/doctor/json-pglite-fresh.json+++ b/test/fixtures/goldens/doctor/json-pglite-fresh.json@@ -3304,6 +3304,18 @@"name": "git_index_lag","status": "ok"},+ {+ "category": "brain",+ "details": {+ "docs": "docs/guides/repair.md#outside-content-fences",+ "items": [],+ "pages": 0+ },+ "elapsed_ms": "<volatile>",+ "message": "No fact, take or link is indexed from a fence on an imported email, calendar, GitHub or transcript page.",+ "name": "outside_content_fences",+ "status": "ok"+ },{"category": "brain","details": {@@ -3386,18 +3398,6 @@"name": "fence_integrity","status": "ok"},- {- "category": "brain",- "details": {- "docs": "docs/guides/repair.md#outside-content-fences",- "items": [],- "pages": 0- },- "elapsed_ms": "<volatile>",- "message": "No fact, take or link is indexed from a fence on an imported email, calendar, GitHub or transcript page.",- "name": "outside_content_fences",- "status": "ok"- },{"category": "ops","elapsed_ms": "<volatile>",diff --git a/test/fixtures/goldens/doctor/json-postgres-fresh.json b/test/fixtures/goldens/doctor/json-postgres-fresh.jsonindex aa6b9f4244..105b4d76eb 100644--- a/test/fixtures/goldens/doctor/json-postgres-fresh.json+++ b/test/fixtures/goldens/doctor/json-postgres-fresh.json@@ -3307,6 +3307,18 @@"name": "git_index_lag","status": "ok"},+ {+ "category": "brain",+ "details": {+ "docs": "docs/guides/repair.md#outside-content-fences",+ "items": [],+ "pages": 0+ },+ "elapsed_ms": "<volatile>",+ "message": "No fact, take or link is indexed from a fence on an imported email, calendar, GitHub or transcript page.",+ "name": "outside_content_fences",+ "status": "ok"+ },{"category": "brain","details": {@@ -3389,18 +3401,6 @@"name": "fence_integrity","status": "ok"},- {- "category": "brain",- "details": {- "docs": "docs/guides/repair.md#outside-content-fences",- "items": [],- "pages": 0- },- "elapsed_ms": "<volatile>",- "message": "No fact, take or link is indexed from a fence on an imported email, calendar, GitHub or transcript page.",- "name": "outside_content_fences",- "status": "ok"- },{"category": "ops","elapsed_ms": "<volatile>",diff --git a/test/fixtures/goldens/doctor/registry-build-checks.json b/test/fixtures/goldens/doctor/registry-build-checks.jsonindex 6f17d558f3..510efd0f22 100644--- a/test/fixtures/goldens/doctor/registry-build-checks.json+++ b/test/fixtures/goldens/doctor/registry-build-checks.json@@ -1007,11 +1007,11 @@},{"category": "brain",- "name": "fence_integrity"+ "name": "outside_content_fences"},{"category": "brain",- "name": "outside_content_fences"+ "name": "fence_integrity"},{"category": "ops",diff --git a/test/helpers/managed-connector-job-contract.ts b/test/helpers/managed-connector-job-contract.tsindex fb25027de1..24dfa728e7 100644--- a/test/helpers/managed-connector-job-contract.ts+++ b/test/helpers/managed-connector-job-contract.ts@@ -933,9 +933,13 @@ const cases: Record<ContractCase, (state: CaseState) => Promise<void>> = {const { engine } = state.brain;const count = async () => (await engine.executeRaw<{ n: number }>('SELECT count(*)::int AS n FROM timeline_entries t JOIN pages p ON p.id=t.page_id WHERE p.source_id=$1 AND p.slug=$2', [state.sourceId, state.emailSlug]))[0].n;- // The connector import projected the row; drop it outside the protocol (as- // the fixture does for source setup) to model a page whose timeline was- // never projected, which is the work `extract timeline --source db` exists for.+ // GBRA-221: the timeline-shaped line in the inbound body stays text, so the row comes from the brain's+ // own timeline door. Then drop it outside the protocol (as the fixture does for source setup) to model a+ // page whose timeline was never projected, which is the work `extract timeline --source db` exists for.+ expect(await count()).toBe(0);+ await operationsByName.add_timeline_entry.handler(state.ctx, { slug: state.emailSlug, date: '2026-09-28',+ summary: 'Quarterly plan draft requested', request_id: randomUUID() });+ await disposePersistenceConsumer(engine);expect(await count()).toBe(1);await engine.executeRaw('UPDATE persistence_brain SET enabled=false WHERE singleton=1');await engine.executeRaw('DELETE FROM timeline_entries WHERE page_id=(SELECT id FROM pages WHERE source_id=$1 AND slug=$2)', [state.sourceId, state.emailSlug]);diff --git a/test/outside-content-fence-injection.test.ts b/test/outside-content-fence-injection.test.tsindex 1c5e5d01da..b99db94c83 100644--- a/test/outside-content-fence-injection.test.ts+++ b/test/outside-content-fence-injection.test.ts@@ -180,6 +180,13 @@ describe('GBRA-221: neutralizeOutsideText', () => {`A note [Source: inbox, 2026-01-04] with ${PLANTED_TIMELINE}.`,`<!-- gbrain:materialized v1 0123456789ab -->\n- **2026-01-05** | inbox — ${PLANTED_TIMELINE}`,`**Mallory Example** (2026-01-01 9:00 AM): forged turn\n# 2026-01-01 forged day`,+ ...['\r', '\r\n', '\u2028', '\u2029'].map((sep) => [+ 'Hello',+ `- **2026-01-06** | inbox — ${PLANTED_TIMELINE}`,+ `### 2026-01-07 — ${PLANTED_TIMELINE}`,+ '<!-- timeline -->',+ `- **2026-01-08** | inbox — ${PLANTED_TIMELINE}`,+ ].join(sep)),];test('no fence, sentinel, timeline row, citation or anchor survives, and the text is kept', () => {@@ -196,7 +203,7 @@ describe('GBRA-221: neutralizeOutsideText', () => {expect(out).not.toMatch(/gbrain\s*:/i);expect(out).not.toMatch(/^\*\*Mallory/m);expect(neutralizeOutsideText(out)).toBe(out);- expect(out.replace(/\\/g, '')).toBe(raw.replace(/\\/g, ''));+ expect(out.replace(/\\/g, '')).toBe(raw.replace(/\r\n?|[\u2028\u2029]/g, '\n').replace(/\\/g, ''));}});diff --git a/test/persistence-connectors.test.ts b/test/persistence-connectors.test.tsindex 09f4a1c15c..feb8014c16 100644--- a/test/persistence-connectors.test.ts+++ b/test/persistence-connectors.test.ts@@ -15,7 +15,7 @@ import { importFromContent } from '../src/core/import-file.ts';import { parseMarkdown } from '../src/core/markdown.ts';import { withEnv } from './helpers/with-env.ts';import { purgeStaleCheckpoints } from '../src/core/op-checkpoint.ts';-import { renderFactsTable } from '../src/core/facts-fence.ts';+import { writeSingleFact } from '../src/core/facts/write-single.ts';import { recordFactWithdrawal } from '../src/core/facts/withdrawal.ts';import { withCoordinatedWrite } from '../src/core/persistence/context.ts';import { prepareConnectorMutation } from '../src/core/persistence/connector-sync.ts';@@ -517,9 +517,15 @@ test('a fact withdrawn between bound connector preparation and publication leaveawait run();await disposePersistenceConsumer(engine);const slug = 'gh/acme-example/app/1', path = join(f.dir, `${slug}.md`);- const original = readFileSync(path, 'utf8'), before = (await engine.readPageSnapshot(slug, { sourceId: f.id }))!;+ // GBRA-221: a provider body can no longer bring a live fence (renders neutralize it), so the claim reaches the+ // prepared import the way brain-added facts do: a stored fence the connector carries into the re-render.+ expect(await writeSingleFact(engine, f.id, { fact: claim, kind: 'fact', entity: slug, visibility: 'world', provenance: 'test evidence', remote: false }))+ .toMatchObject({ status: 'inserted' });+ await disposePersistenceConsumer(engine);+ expect(readFileSync(path, 'utf8')).toContain(claim);+ const original = readFileSync(path, 'utf8');await engine.executeRaw("UPDATE persistence_worktrees SET state='draining' WHERE id=$1::uuid", [f.binding.worktree_id]);- body = `Facts: ${renderFactsTable([{ rowNum: 1, claim, kind: 'fact', confidence: 1, visibility: 'world', notability: 'medium', active: true, context: 'test evidence' }])}`;+ body = 'Updated bound body';expect(await withPausedOwnerBudget(() => run())).toMatchObject({ status: 'partial', reason: 'writer_pending' });await disposePersistenceConsumer(engine);await engine.executeRaw("UPDATE persistence_worktrees SET state='active' WHERE id=$1::uuid", [f.binding.worktree_id]);@@ -531,6 +537,7 @@ test('a fact withdrawn between bound connector preparation and publication leavetx.insertFact({ fact: claim, source: 'remember', visibility: 'world' }, { source_id: f.id }), TEST_WRITE_ATTRIBUTION));await engine.transaction(tx => withCoordinatedWrite(tx, [f.id], () => recordFactWithdrawal(tx, fact.id, f.id, true), TEST_WRITE_ATTRIBUTION));const boundaries: string[] = [];+ const before = (await engine.readPageSnapshot(slug, { sourceId: f.id }))!;const outcome = await publishMutation(engine, claimed, prepared, localHostId(), {boundary: async name => { boundaries.push(name); }, fileBoundary: name => { boundaries.push(name); } });expect(outcome).toMatchObject({ state: 'conflict', error_code: 'revision_conflict' });