Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Wave B PR 1: inline golden captured on master (chunker output and stored rows, switch off and on)
garrytan4a03f1b58af8
diff --git a/test/fixtures/large-pages/inline-chunks-golden.json b/test/fixtures/large-pages/inline-chunks-golden.jsonnew file mode 100644index 0000000000..053612f67c--- /dev/null+++ b/test/fixtures/large-pages/inline-chunks-golden.json@@ -0,0 +1,44 @@+{+ "chunker": {+ "golden/tiny": "eeeb8d6dfbf5d312229d9c89e68f03f3d7a9843d059cc9dec8e90e2a1a73ec52",+ "golden/empty-body": "4f53cda18c2baa0c0354bb5f9a3ecbe5ed12ab4d8e11ba873c2f11161202b945",+ "golden/no-frontmatter": "ecf41c263d7803056af769133a045999b5690093fba963629763d4599ff34dab",+ "golden/fences": "6164ba600ce00f95566ee8b31f58aed12a16d86a63d70c26ce30d0835cba1e9a",+ "golden/timeline": "3eec295c0ff24ca9b5c02a62c3aab7bfe761e1898d96d0aed26b25c99d55f954",+ "golden/crlf": "a8d79689ddbb3fc634aa3c6cf0fb13b4e40656fed44f7f74bdb054c5fea89aa8",+ "golden/long-line": "347027e4882536951c0c1e95cd972b58b40726ba7735b0e8aee319f5b2e47bdd",+ "golden/cjk": "dcb5dee8337d88f49aac435c807b9b518c45f3274a8d6058913fdcdf0c85a5c8",+ "golden/prose-100000": "7c478de6f23510db2bbc1405cdd4f5e996ed2a91587d25370936d110b05ad574",+ "golden/prose-500000": "9753c967150753293798e9996251091f4538b362c472a7886a46d442d9b4239b",+ "golden/prose-900000": "694c44befb2fab25d18a594b1b36fc3ca3ffb26a40c33c648867d105f0c70eea",+ "golden/at-threshold": "8cc07b67c54b8e527ee1698a499838532ec5dcd6c64fd511b0e0155c0d5cc7fa"+ },+ "rows": {+ "golden/tiny": "6794688d62ba7f517ca96f6539c9394641aa0ab9a299ed3ee203c763b3800247",+ "golden/empty-body": "cf3337c08204b43c89f57403ee646dfea57e7eb61bef6335e18e6f0750c88a06",+ "golden/no-frontmatter": "a8feca3bf7aa292854b0b5b123dfcb9c5ea3c16a44cb4c15fdd0f5c80d4711f4",+ "golden/fences": "0f6f3b9ae8800b7c5db6904a6d3dc21c6f05b0bdb94139e44343c08a6839c15b",+ "golden/timeline": "eede967c1d8ca20011ffb8ab8542710dce8c11bda183241faac3e17d54d080d5",+ "golden/crlf": "ce6f3d6240750001d279cd74e7095755c5663d7e660c37e63ced0f8c38037334",+ "golden/long-line": "65c1d9730ab30bf6352894465eaae026ce3b1168d1d412e8992438ce658c3ca3",+ "golden/cjk": "d1f7a9038c623bbc17b8a1fe6e08eadd856341d089ebf27ce4e4b291376412a1",+ "golden/prose-100000": "b0e26a29b4e8df0d5d4515ce658c42f2ab9db6cff0ca6a67fc146d7bafee6fc0",+ "golden/prose-500000": "bedf6011d92195ae0e0d32bb20ff51193c3d587be871337d519ec16771a2a564",+ "golden/prose-900000": "6ea0cf2282ab03f840cd837c432f479315bb944258f930ee6f56bfe0a1ce879b",+ "golden/at-threshold": "aef313469326c1321cf92e6f9c20bf597cd9b8eb419984f9582df3f262b91826"+ },+ "bodyBytes": {+ "golden/tiny": 19,+ "golden/empty-body": 0,+ "golden/no-frontmatter": 278,+ "golden/fences": 20243,+ "golden/timeline": 30173,+ "golden/crlf": 40482,+ "golden/long-line": 300205,+ "golden/cjk": 440000,+ "golden/prose-100000": 100000,+ "golden/prose-500000": 499999,+ "golden/prose-900000": 900000,+ "golden/at-threshold": 1048576+ }+}diff --git a/test/helpers/inline-page-corpus.ts b/test/helpers/inline-page-corpus.tsnew file mode 100644index 0000000000..5a676f19a6--- /dev/null+++ b/test/helpers/inline-page-corpus.ts@@ -0,0 +1,81 @@+/**+ * A deterministic corpus of inline (<= 1 MiB body) Markdown pages for the Wave B+ * inline golden: prose, headings, code fences, facts and takes fences, a+ * timeline sentinel, CJK, emoji and astral text, CRLF and long lines, from a+ * few bytes up to exactly the inline threshold. Seeded, so every run builds+ * the same bytes.+ */+import { PAGE_INLINE_MAX_BYTES } from '../../src/core/page-size-policy.ts';++export interface CorpusPage { slug: string; content: string }++function rng(seed: number): () => number {+ let s = seed >>> 0;+ return () => {+ s = (s + 0x6d2b79f5) >>> 0;+ let t = s;+ t = Math.imul(t ^ (t >>> 15), t | 1);+ t ^= t + Math.imul(t ^ (t >>> 7), t | 61);+ return ((t ^ (t >>> 14)) >>> 0) / 4294967296;+ };+}++const WORDS = ['widget', 'plan', 'acme-example', 'alice-example', 'roadmap', 'launch', 'metric', 'customer', 'pricing', 'review',+ 'décision', 'naïve', '会议', '记录', '検討', '🚀', '𝔘𝔫𝔦𝔠𝔬𝔡𝔢', 'quote"mark', 'back\\slash', 'tab\there'];++function sentence(r: () => number, words: number): string {+ const out: string[] = [];+ for (let i = 0; i < words; i++) out.push(WORDS[Math.floor(r() * WORDS.length)]!);+ return out.join(' ') + '.';+}++function block(r: () => number, i: number): string {+ const k = Math.floor(r() * 10);+ if (k === 0) return `## Section ${i}`;+ if (k === 1) return `### Detail ${i}\n\n${sentence(r, 12)}`;+ if (k === 2) return '```ts\nexport function f' + i + '(x: number): number {\n return x * ' + i + ';\n}\n```';+ if (k === 3) return `- item ${i}: ${sentence(r, 6)}\n- item ${i + 1}: ${sentence(r, 5)}`;+ if (k === 4) return `> ${sentence(r, 10)}`;+ return sentence(r, 8 + Math.floor(r() * 40));+}++/** Prose blocks until the body reaches `bytes` UTF-8 bytes, then trimmed to exactly that size on a code point. */+function body(r: () => number, bytes: number, fenced: boolean): string {+ const parts: string[] = [];+ let size = 0;+ let fences = 0;+ for (let i = 0; size < bytes; i++) {+ let b = block(r, i);+ if (b.startsWith('```') && (!fenced || ++fences > 4)) b = sentence(r, 10);+ parts.push(b);+ size += Buffer.byteLength(b) + 2;+ }+ const text = parts.join('\n\n');+ const buf = Buffer.from(text);+ let end = Math.min(bytes, buf.length);+ while (end > 0 && (buf[end]! & 0xc0) === 0x80) end--;+ return buf.subarray(0, end).toString('utf-8');+}++const FACTS = '```facts\n| fact | visibility |\n| --- | --- |\n| acme-example raised a seed | world |\n| alice-example prefers email | private |\n```';+const TAKES = '```takes\n| claim | holder | weight |\n| --- | --- | --- |\n| widget plan ships in Q3 | alice-example | 0.7 |\n```';++export function inlinePageCorpus(): CorpusPage[] {+ const pages: CorpusPage[] = [];+ const add = (slug: string, content: string) => pages.push({ slug, content });+ const r = rng(0x5eed);+ add('golden/tiny', '---\ntitle: Tiny\ntype: note\n---\nHello acme-example.\n');+ add('golden/empty-body', '---\ntitle: Empty\ntype: note\n---\n');+ add('golden/no-frontmatter', `# Heading only\n\n${sentence(r, 30)}\n`);+ add('golden/fences', `---\ntitle: Fences\ntype: concept\ntags: [b, a]\n---\n${body(r, 20_000, true)}\n\n${FACTS}\n\n${TAKES}\n`);+ add('golden/timeline', `---\ntitle: Timeline\ntype: person\n---\n${body(r, 30_000, false)}\n\n---\n\n- 2026-01-02: met alice-example\n- 2026-02-03: ${sentence(r, 12)}\n`);+ add('golden/crlf', `---\r\ntitle: Crlf\r\ntype: note\r\n---\r\n${body(r, 40_000, true).replace(/\n/g, '\r\n')}\r\n`);+ add('golden/long-line', `---\ntitle: Long line\ntype: note\n---\n${'x'.repeat(300_000)} ${sentence(r, 20)}\n`);+ add('golden/cjk', `---\ntitle: 会议记录\ntype: meeting\n---\n${'会议记录検討🚀'.repeat(20_000)}\n`);+ for (const size of [100_000, 500_000, 900_000]) add(`golden/prose-${size}`, `---\ntitle: Prose ${size}\ntype: note\n---\n${body(r, size, true)}\n`);+ const truth = body(r, PAGE_INLINE_MAX_BYTES - 64 * 1024, true);+ const timeline = `- 2026-03-01: ${body(r, 64 * 1024, false)}`;+ const timelineBytes = PAGE_INLINE_MAX_BYTES - Buffer.byteLength(truth);+ add('golden/at-threshold', `---\ntitle: At threshold\ntype: note\n---\n${truth}\n\n<!-- timeline -->\n${Buffer.from(timeline).subarray(0, timelineBytes).toString('utf-8').replace(/\uFFFD+$/, '')}`);+ return pages;+}diff --git a/test/large-pages/inline-chunks-golden.test.ts b/test/large-pages/inline-chunks-golden.test.tsnew file mode 100644index 0000000000..7c369263a7--- /dev/null+++ b/test/large-pages/inline-chunks-golden.test.ts@@ -0,0 +1,94 @@+/**+ * Wave B inline golden: pages at or under 1 MiB keep master's bytes exactly.+ *+ * 1. Protects: the inline storage path while segmented storage ships. For a+ * seeded corpus of <= 1 MiB pages (fences, facts/takes, timeline, CRLF,+ * long lines, CJK, the threshold itself) the chunker output, and the page+ * and chunk rows an import writes, hash to the digests captured on master+ * 44eb0493f, with `pages.segmented_storage` off and on.+ * 2. Fails when: any Wave B change alters inline chunking, the stored body+ * columns, content_hash, chunker_version or chunk rows of a page that+ * never crosses the inline threshold.+ * 3. Regenerate only for an intended inline change:+ * GBRAIN_TEST_UPDATE_GOLDENS=1 bun test test/large-pages/inline-chunks-golden.test.ts+ */+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';+import { createHash } from 'node:crypto';+import { existsSync, readFileSync, writeFileSync } from 'node:fs';+import { join } from 'node:path';+import { PGLiteEngine } from '../../src/core/pglite-engine.ts';+import { importFromContent } from '../../src/core/import-file.ts';+import { parseMarkdown } from '../../src/core/markdown.ts';+import { prepareMarkdownChunks } from '../../src/core/markdown-chunks.ts';+import { PAGE_INLINE_MAX_BYTES } from '../../src/core/page-size-policy.ts';+import { inlinePageCorpus } from '../helpers/inline-page-corpus.ts';++const FIXTURE = join(import.meta.dir, '..', 'fixtures', 'large-pages', 'inline-chunks-golden.json');+const UPDATE = process.env.GBRAIN_TEST_UPDATE_GOLDENS === '1';+const corpus = inlinePageCorpus();+const sha = (v: unknown) => createHash('sha256').update(JSON.stringify(v)).digest('hex');++interface Golden { chunker: Record<string, string>; rows: Record<string, string>; bodyBytes: Record<string, number> }+const golden: Golden = existsSync(FIXTURE) ? JSON.parse(readFileSync(FIXTURE, 'utf-8')) : { chunker: {}, rows: {}, bodyBytes: {} };+const observed: Golden = { chunker: {}, rows: {}, bodyBytes: {} };++let engine: PGLiteEngine;++beforeAll(async () => {+ engine = new PGLiteEngine();+ await engine.connect({});+ await engine.initSchema();+}, 120_000);++afterAll(async () => {+ await engine.disconnect();+ if (UPDATE) writeFileSync(FIXTURE, JSON.stringify(observed, null, 2) + '\n');+});++async function storedRows(slug: string) {+ const [page] = await engine.executeRaw<Record<string, unknown>>(+ 'SELECT slug, type, title, compiled_truth, timeline, frontmatter, content_hash, chunker_version, page_kind FROM pages WHERE slug = $1', [slug]);+ const chunks = await engine.executeRaw<Record<string, unknown>>(+ `SELECT c.chunk_index, c.chunk_text, c.chunk_source, c.language, c.symbol_name, c.symbol_type, c.start_line, c.end_line,+ c.projected_byte_start, c.projected_byte_end+ FROM content_chunks c JOIN pages p ON p.id = c.page_id WHERE p.slug = $1 ORDER BY c.chunk_index`, [slug]);+ return { page, chunks: chunks.map(c => ({ ...c, start_line: c.start_line ?? null, end_line: c.end_line ?? null })) };+}++describe('inline pages stay byte-identical to master', () => {+ test('the corpus spans the inline range up to the threshold', () => {+ const sizes = corpus.map(p => { const m = parseMarkdown(p.content, `${p.slug}.md`); return Buffer.byteLength(m.compiled_truth) + Buffer.byteLength(m.timeline); });+ expect(Math.max(...sizes)).toBeLessThanOrEqual(PAGE_INLINE_MAX_BYTES);+ expect(Math.max(...sizes)).toBeGreaterThan(PAGE_INLINE_MAX_BYTES - 16 * 1024);+ });++ for (const page of corpus) {+ test(`chunker output: ${page.slug}`, async () => {+ const parsed = parseMarkdown(page.content, `${page.slug}.md`);+ observed.bodyBytes[page.slug] = Buffer.byteLength(parsed.compiled_truth) + Buffer.byteLength(parsed.timeline);+ observed.chunker[page.slug] = sha(await prepareMarkdownChunks(parsed));+ if (!UPDATE) {+ expect(observed.bodyBytes[page.slug]).toBe(golden.bodyBytes[page.slug]);+ expect(observed.chunker[page.slug]).toBe(golden.chunker[page.slug]);+ }+ }, 60_000);+ }++ for (const mode of ['off', 'on'] as const) {+ describe(`import with pages.segmented_storage=${mode}`, () => {+ beforeAll(async () => {+ await engine.executeRaw('DELETE FROM pages');+ await engine.setConfig('pages.segmented_storage', mode);+ });+ for (const page of corpus) {+ test(`stored rows: ${page.slug}`, async () => {+ const result = await importFromContent(engine, page.slug, page.content, { sourceId: 'default', noEmbed: true });+ expect(result.status).toBe('imported');+ const digest = sha(await storedRows(page.slug));+ if (mode === 'off') observed.rows[page.slug] = digest;+ if (!UPDATE || mode === 'on') expect(digest).toBe(UPDATE ? observed.rows[page.slug] : golden.rows[page.slug]);+ }, 60_000);+ }+ });+ }+});