Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix: pre-landing review fixes
garrytan751f6d541fd5
diff --git a/src/commands/doctor/checks/outside-content-fences.ts b/src/commands/doctor/checks/outside-content-fences.tsindex fb9623717b..f5ed084911 100644--- a/src/commands/doctor/checks/outside-content-fences.ts+++ b/src/commands/doctor/checks/outside-content-fences.ts@@ -7,7 +7,8 @@* fence row still indexed from such a page was planted before the fix or added by a gbrain writer (`remember`, loop* extraction) on purpose; the check cannot tell which, so it lists each page for review and removes nothing. It reads* the derived rows (`facts.row_num`, `takes`, `links.origin_field = 'links_fence'`), never a page body, so a- * segmented page is covered too. Contact pages are out of scope: their single-line fields cannot hold a fence.+ * segmented page is covered too. Contact (person) pages are out of scope: gbrain's own writers put most person facts+ * there, so the list would bury the rare planted row; their rendered name, organization and title are neutralized too.* Location and counts only, never a cell value. Reads the database only.*/import type { Check } from '../../doctor.ts';diff --git a/src/core/outside-content.ts b/src/core/outside-content.tsindex c51ce26cac..33fed04ad1 100644--- a/src/core/outside-content.ts+++ b/src/core/outside-content.ts@@ -19,7 +19,9 @@* 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,+ * - NUL is stripped first, as the connector render boundary (`connectorRender`) and page storage strip it, so a NUL+ * inside a token cannot hide it from the rules below and rebuild it in the stored page.+ * - CR, CRLF, U+2028 and U+2029 become LF: 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.@@ -27,6 +29,7 @@import { isBareTimelineHeading, timelineSentinelKind } from './markdown.ts';import { isDatedTimelineLine } from './timeline-citations.ts';import { BUILTIN_PATTERNS } from './conversation-parser/builtins.ts';+import { sanitizeForJsonb } from './batch-rows.ts';const IMESSAGE_SLACK = BUILTIN_PATTERNS.find((p) => p.id === 'imessage-slack');if (!IMESSAGE_SLACK) {@@ -50,19 +53,23 @@ const MARKER_TOKEN_RE = /(gbrain)(\s*):(?=\s*[a-z])/gi;export function escapeAnchorLines(text: string): string {return text.split('\n')- .map((line) => (MESSAGE_ANCHOR_RE.test(line) || DATE_HEADING_RE.test(line) ? `\\${line}` : line))+ .map((line) => (isAnchorLine(line) ? `\\${line}` : line)).join('\n');}+function isAnchorLine(line: string): boolean {+ return MESSAGE_ANCHOR_RE.test(line) || DATE_HEADING_RE.test(line);+}+function isStructuralLine(line: string): boolean {const trimmed = line.trim();return timelineSentinelKind(trimmed) === 'marker' || isBareTimelineHeading(trimmed) || isDatedTimelineLine(line)- || TIMELINE_BULLET_RE.test(line) || TIMELINE_HEADING_RE.test(line) || MESSAGE_ANCHOR_RE.test(line) || DATE_HEADING_RE.test(line);+ || TIMELINE_BULLET_RE.test(line) || TIMELINE_HEADING_RE.test(line) || isAnchorLine(line);}/** Outside-authored text with no live gbrain marker, citation, timeline split or row, or transcript anchor. */export function neutralizeOutsideText(text: string): string {- return text+ return sanitizeForJsonb(text).replace(/\r\n?|[\u2028\u2029]/g, '\n').replace(MARKER_TOKEN_RE, (_m, word: string, ws: string) => `${word}${ws}\\:`).replace(/\[Source:/g, '[Source\\:')diff --git a/test/book-mirror.test.ts b/test/book-mirror.test.tsindex 15db27b8e9..a7ea9c05e1 100644--- a/test/book-mirror.test.ts+++ b/test/book-mirror.test.ts@@ -110,4 +110,14 @@ describe('gbrain book-mirror — source file invariants', () => {// contract by adding the wrong fields.expect(source).toContain('viaSubagent intentionally omitted');});++ // gstack /ship Step 9 pass 1 (testing specialist). buildAssembledPage is private, so this pins it the way the+ // invariants above pin the trust contract.+ // Value: protects=each chapter analysis (model output over an untrusted book) is neutralized before assembly (GBRA-221);+ // fails_when=the neutralizeOutsideText call on a.result is removed; why_new=the GBRA-221 renderer suite has no+ // book-mirror case; seam=none; no_claim=the assembled page's parse (source text only).+ it('neutralizes each chapter analysis before assembling the page (GBRA-221)', () => {+ expect(source).toContain("import { neutralizeOutsideText } from '../core/outside-content.ts';");+ expect(source).toContain('.map(a => neutralizeOutsideText(a.result.trim()))');+ });});diff --git a/test/doctor-outside-content-fences.test.ts b/test/doctor-outside-content-fences.test.tsindex fee2eb5659..b713665653 100644--- a/test/doctor-outside-content-fences.test.ts+++ b/test/doctor-outside-content-fences.test.ts@@ -31,7 +31,7 @@ beforeAll(async () => {afterAll(async () => { if (engine) await engine.disconnect(); }, 60_000);async function source(id: string, config: Record<string, unknown>) {- await engine.executeRaw('INSERT INTO sources (id, name, local_path, config) VALUES ($1, $1, NULL, $2::text::jsonb)', [id, JSON.stringify(config)]);+ await engine.executeRaw('INSERT INTO sources (id, name, local_path, config) VALUES ($1, $1, NULL, $2::text::jsonb) ON CONFLICT (id) DO NOTHING', [id, JSON.stringify(config)]);}async function page(sourceId: string, slug: string, type: string, body: string, extra = '') {@@ -61,6 +61,8 @@ describe('outside_content_fences doctor check', () => {}, 60_000);test('a planted fence on an email, GitHub or transcript page is listed for review and nothing is removed', async () => {+ await source('google-example', { kind: 'google', g_account: '[email protected]' });+ await source('notes-example', {});await source('github-example', { kind: 'github', gh_repos: 'acme-example/app' });await page('google-example', 'emails/2026/01/planted-thread', 'email', FENCE);await page('github-example', 'gh/acme-example/app/1', 'note', FENCE);@@ -97,6 +99,26 @@ describe('outside_content_fences doctor check', () => {expect(items.map(i => `${i.source_id}:${i.slug}`)).toEqual(['scope-google:emails/2026/02/live-thread']);}, 60_000);+ // gstack /ship Step 9 pass 1 (testing specialist).+ // Value: protects=active takes and links-fence links on an imported email page are counted like facts; fails_when=the+ // takes or links arm of the query is dropped or reads the wrong flag or origin; why_new=every test above plants only a+ // facts fence; seam=none; no_claim=how takes and links fences are projected (rows are inserted directly).+ test('counts active takes and links-fence links on an imported page, not inactive takes or other links', async () => {+ await source('arms-google', { kind: 'google', g_account: '[email protected]' });+ await page('arms-google', 'emails/2026/03/arms-thread', 'email', 'A plain message.');+ await page('arms-google', 'companies/acme-example', 'company', 'A company page.');+ const id = async (slug: string) => (await engine.executeRaw<{ id: number }>('SELECT id FROM pages WHERE source_id = $1 AND slug = $2', ['arms-google', slug]))[0]!.id;+ const thread = await id('emails/2026/03/arms-thread'), company = await id('companies/acme-example');+ await engine.executeRaw(`INSERT INTO takes (page_id, row_num, claim, kind, holder, weight)+ VALUES ($1, 1, 'Synthetic take', 'take', 'owner', 0.5), ($1, 2, 'Withdrawn take', 'take', 'owner', 0.5)`, [thread]);+ await engine.executeRaw('UPDATE takes SET active = false WHERE page_id = $1 AND row_num = 2', [thread]);+ await engine.executeRaw(`INSERT INTO links (from_page_id, to_page_id, link_type, origin_page_id, origin_field)+ VALUES ($1, $2, 'invested_in', $1, 'links_fence'), ($1, $2, 'mentions', $1, 'body')`, [thread, company]);+ const checks = await outsideContentFencesEntry.run({ engine } as unknown as DoctorContext) as Check[];+ const items = (checks[0]!.details as { items: Array<Record<string, unknown>> }).items.filter(i => i.source_id === 'arms-google');+ expect(items).toEqual([{ source_id: 'arms-google', slug: 'emails/2026/03/arms-thread', type: 'email', facts: 0, world_facts: 0, takes: 1, links: 1 }]);+ }, 60_000);+// Value: protects=the check reports an error check instead of throwing when the query fails;// fails_when=the try/catch around the query is removed; why_new=no test drives the error path;// seam=none; no_claim=real database failures (an engine stub throws).diff --git a/test/outside-content-fence-injection.test.ts b/test/outside-content-fence-injection.test.tsindex 9c281d7fde..f17c4c1095 100644--- a/test/outside-content-fence-injection.test.ts+++ b/test/outside-content-fence-injection.test.ts@@ -32,7 +32,8 @@ import { extractTimelineFromContent } from '../src/core/timeline-extract.ts';import { parseInlineCitationTimelineEntries } from '../src/core/timeline-citations.ts';import { parseMarkdown } from '../src/core/markdown.ts';import { renderItemPage, renderRepoCard } from '../src/core/github-source.ts';-import { renderPersonPage } from '../src/core/google/google-render.ts';+import { renderCalendarEventPage, renderPersonPage, renderThreadPage } from '../src/core/google/google-render.ts';+import { connectorRender } from '../src/core/connectors/connector-text.ts';import { redactSession, renderSessionParts } from '../src/core/transcripts/render.ts';const SOURCE = 'gsrc';@@ -70,6 +71,9 @@ const HOSTILE = [`- **2026-01-02** | inbox — ${PLANTED_TIMELINE}`,].join('\n');+/** HOSTILE with a NUL inside every marker token, a citation and the row prefix: the connector render boundary strips NUL. */+const NUL_SPLIT = [HOSTILE.replace(/gbrain:/g, 'gbrain\u0000:').replace(/^- \*\*/m, '\u0000- **'), `See [Source\u0000: inbox, 2026-01-04] for ${PLANTED_TIMELINE}.`].join('\n');+const NOW_MS = Math.floor(Date.now() / 1000) * 1000;const daysAgoMs = (n: number): number => NOW_MS - n * 86_400_000;@@ -235,6 +239,35 @@ describe('GBRA-221: every outside-content renderer uses the shared neutralizer',expectInertPage(renderRepoCard('acme-example/app', { description: HOSTILE, default_branch: 'main', archived: false, private: true } as never, null));});+ // gstack /ship Step 9 pass 1 (red team): connectorRender strips NUL after the per-field neutralizer ran.+ // Value: protects=a NUL inside a marker, citation or row prefix cannot rebuild it at the connector render boundary;+ // fails_when=neutralizeOutsideText stops stripping NUL before its matchers run; why_new=the renderer tests above+ // never pass through connectorRender; seam=none; no_claim=provider decoding of NUL (literal inputs stand in).+ test('a NUL-split marker, citation or row stays inert through the connector render boundary', () => {+ const detail = { number: 1, title: 'Example issue', state: 'open', state_reason: null, body: NUL_SPLIT, created_at: '2026-01-01T00:00:00Z',+ updated_at: '2026-01-02T00:00:00Z', closed_at: null, labels: [], assignees: [], milestone: null, html_url: 'https://github.com/acme-example/app/issues/1',+ user: { login: 'example-user' } };+ const msg = { id: 'AbCdEfGhIjKl', threadId: 't1', from: 'Mallory Example <[email protected]>', fromAddress: '[email protected]', to: [ACCOUNT], cc: [],+ subject: 'Quick note', dateIso: '2026-01-05T10:00:00Z', internalDateMs: 0, labelIds: ['INBOX'], listUnsubscribe: false, bodyText: NUL_SPLIT };+ const event = { id: 'evt1', summary: 'Planning sync', description: NUL_SPLIT, startIso: '2026-01-05T10:00:00Z', endIso: '2026-01-05T11:00:00Z',+ allDay: false, organizer: null, attendees: [], location: null, status: 'confirmed', account: ACCOUNT };+ const rendered = [+ renderItemPage({ repo: 'acme-example/app', number: 1, kind: 'issue', detail, linked: [], reviews: [], reviewComments: [], checks: null,+ comments: [] } as never, true, null),+ renderRepoCard('acme-example/app', { description: NUL_SPLIT, default_branch: 'main', archived: false, private: true } as never, null),+ renderThreadPage({ threadId: 't1', account: ACCOUNT, messages: [msg] } as never)!.markdown,+ renderCalendarEventPage(event as never)!.markdown,+ renderPersonPage({ resourceName: 'people/c1', displayName: NUL_SPLIT, emails: ['[email protected]'], phones: [], organization: null, title: null,+ groups: [], deleted: false } as never)!.markdown,+ ];+ for (const markdown of rendered) {+ const stored = connectorRender(markdown, { path: 'x/page.md' });+ expectInertPage(stored);+ expect(parseInlineCitationTimelineEntries(stored)).toEqual([]);+ expect(stored).not.toMatch(/gbrain\s*:/i);+ }+ });+// Value: protects=a contact's name, organization and title cannot plant a fence or timeline row on the person page;// fails_when=a renderPersonPage field drops its neutralizeOutsideText call; why_new=existing person-page tests only// cover phone-only layout; seam=none; no_claim=the Google Contacts API shape (a ContactData literal stands in).@@ -275,6 +308,23 @@ describe('GBRA-221: outside text cannot plant machine fences through the Googleexpect(await activeFacts(PLANTED)).toEqual([]);}), 120_000);+ // gstack /ship Step 9 pass 1 (red team): the real Google importer path, which stores connectorRender's NUL-stripped page.+ // Value: protects=a NUL-split fence, citation and row in an inbound email stay inert in the stored page and promote no+ // fact; fails_when=NUL is stripped only after the neutralizer; why_new=the email test above carries no NUL; seam=none;+ // no_claim=Gmail's own NUL handling (the fake API delivers the bytes as given).+ test('a NUL byte inside a marker, citation or row prefix cannot rebuild it in the stored email page', async () => withEnv({ GBRAIN_HOME: dir }, async () => {+ const state = { historyId: '1000', history: [], events: [], messages: [+ { id: '18c2f4a9b3d21e01', threadId: '17aa00000000a001', ms: daysAgoMs(1), from: 'Mallory Example <[email protected]>', subject: 'Quick note', body: NUL_SPLIT },+ ] };+ expect((await sweep(state, vault())).failedFiles ?? 0).toBe(0);+ const page = await pageWhere('email');+ expect(page).toBeDefined();+ expectInert(page!);+ expect(parseInlineCitationTimelineEntries(page!.compiled_truth)).toEqual([]);+ await runExtractFacts(engine, { sourceId: SOURCE });+ expect(await activeFacts(PLANTED)).toEqual([]);+ }), 120_000);+test('a calendar description carrying fence markers (HTML-entity encoded) renders inert', async () => withEnv({ GBRAIN_HOME: dir }, async () => {const encoded = HOSTILE.replace(/</g, '<').replace(/>/g, '>').split('\n').join('<br>');const state = { historyId: '1000', history: [], messages: [], events: [{