Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Wave B lane 1d: bench flip retirement (N chunk rows, plain and embedded, under the publication timeouts); retire at most 1,000 chunk rows per flip and per deferred statement
garrytanc71d1efe2511
diff --git a/scripts/bench-large-pages.ts b/scripts/bench-large-pages.tsindex cd822be9ee..f56be1c903 100644--- a/scripts/bench-large-pages.ts+++ b/scripts/bench-large-pages.ts@@ -14,11 +14,18 @@* - `concurrent_small_put`: latency of small page writes (the put_page import* core, `importFromContent`) issued one per event-loop turn, as separate* requests arrive, while the large ingest runs.+ * - `flip_retire`: wall time of `flipBodyGeneration` retiring N live chunk rows+ * (`--flip-rows`, default 5000,10000,20000,40000) in one transaction under the+ * publication timeouts (`lock_timeout` 1 s, `statement_timeout` 5 s), with+ * chunks of the bench page's average size, without and with embeddings (an+ * embedded row's retire UPDATE also writes HNSW entries). Each N gets its own+ * segmented page; `timed_out` reports a 57014 statement timeout.** Run it under a memory cap so a regression fails instead of swapping:* sudo systemd-run --scope -p MemoryMax=3G bun scripts/bench-large-pages.ts --size 100MB* sudo systemd-run --scope -p MemoryMax=3G env DATABASE_URL=postgresql://... bun scripts/bench-large-pages.ts --size 100MB --engine postgres- * Flags: --size <N>[KB|MB|GB] (default 100MB; above PAGE_MAX_BYTES, where the streamed path starts), --engine pglite|postgres (default pglite; postgres reads DATABASE_URL), --json.+ * Flags: --size <N>[KB|MB|GB] (default 100MB; above PAGE_MAX_BYTES, where the streamed path starts), --engine pglite|postgres (default pglite; postgres reads DATABASE_URL), --json,+ * --flip-rows <N,...>, --flip-only (only the `flip_retire` section).*/import { spawnSync } from 'node:child_process';import { appendFileSync, mkdtempSync, rmSync, statSync, writeFileSync } from 'node:fs';@@ -30,6 +37,7 @@ import { readPageBodyPolicy } from '../src/core/page-body/limits.ts';import { PAGE_MAX_BYTES } from '../src/core/page-size-policy.ts';import { importStreamedMarkdownFile } from '../src/core/page-body/streamed-import.ts';import { conversationBody, conversationPage } from '../test/helpers/conversation-page.ts';+import { flipInput, publishSegmentedPage, segmentText, stageGeneration } from '../test/helpers/page-body-fixtures.ts';const SLUG = 'bench/large-page';@@ -93,6 +101,53 @@ async function ingest(engine: BrainEngine, file: string) {const round = (n: number) => Math.round(n * 10) / 10;+/** Chunks per manifest occurrence and bytes per chunk text of the 100 MB bench page (29,041 chunks over 374 occurrences). */+const FLIP_CHUNKS_PER_OCCURRENCE = 78;+const FLIP_CHUNK_BYTES = 3400;++/**+ * One `flip_retire` cell: a segmented page at generation 1 whose occurrences hold `rows` live chunk rows, then the flip to+ * generation 2 that retires all of them, timed inside a transaction with the publication's lock and statement timeouts.+ */+async function flipRetire(engine: BrainEngine, rows: number, embedded: boolean) {+ const slug = `bench/flip-${rows}-${embedded ? 'embedded' : 'plain'}`;+ await engine.executeRaw('DELETE FROM pages WHERE slug=$1', [slug]);+ const texts = Array.from({ length: Math.ceil(rows / FLIP_CHUNKS_PER_OCCURRENCE) }, (_, i) => segmentText(`## Part ${i}\n\nSegment ${i} of ${slug}.\n`));+ const page = await publishSegmentedPage(engine, slug, texts);+ const samples = Array.from({ length: 64 }, (_, i) => conversationBody(FLIP_CHUNK_BYTES, { seed: 100 + i }));+ const embedding = embedded ? "('[' || array_to_string(ARRAY(SELECT round(random()::numeric, 4) FROM generate_series(1, 1024) WHERE g >= 0), ',') || ']')::vector" : 'NULL';+ for (let from = 0; from < rows; from += 5000) {+ await engine.executeRaw(`INSERT INTO content_chunks (page_id, chunk_index, chunk_text, chunk_source, occurrence_id, local_ordinal, raw_byte_start, raw_byte_end, body_generation, embedding, embedded_at)+ SELECT $1, g, ($2::text[])[1 + g % 64], 'compiled_truth', ($3::bigint[])[1 + g / ${FLIP_CHUNKS_PER_OCCURRENCE}], g % ${FLIP_CHUNKS_PER_OCCURRENCE}, 0, ${FLIP_CHUNK_BYTES}, 1,+ ${embedding}, ${embedded ? 'now()' : 'NULL'}+ FROM generate_series($4::int, $5::int) AS g`, [page.pageId, samples, page.occurrences, from, Math.min(rows, from + 5000) - 1]);+ }+ const next = [segmentText(`## Part 0\n\nReplacement of ${slug}.\n`)];+ const staged = await stageGeneration(engine, { pageId: page.pageId, slug, genFrom: 2, texts: next });+ const t0 = performance.now();+ try {+ const flip = await engine.transaction(async tx => {+ await tx.executeRaw("SELECT set_config('lock_timeout','1s',true),set_config('statement_timeout','5s',true)");+ return tx.flipBodyGeneration(flipInput(page.pageId, 1, staged.stagingId, next,+ { closeOccurrences: page.occurrences, retireChunkOccurrences: page.occurrences, retireMaxRows: rows }));+ });+ return { rows, embedded, ms: round(performance.now() - t0), retired: flip.retiredChunks, timed_out: false };+ } catch (error) {+ if ((error as { code?: unknown })?.code !== '57014' && !/statement timeout/i.test(String(error))) throw error;+ return { rows, embedded, ms: round(performance.now() - t0), retired: 0, timed_out: true };+ } finally {+ await engine.executeRaw('DELETE FROM pages WHERE slug=$1', [slug]);+ }+}++async function flipRetireCells(engine: BrainEngine) {+ const cells = [];+ for (const embedded of [false, true]) {+ for (const rows of arg('--flip-rows', '5000,10000,20000,40000').split(',').map(Number)) cells.push(await flipRetire(engine, rows, embedded));+ }+ return cells;+}+/** Child mode: ingest `--file` in a fresh process (the parent generated it) and report its peak RSS. */async function child(file: string, kind: string): Promise<void> {const dir = mkdtempSync(join(tmpdir(), 'gbrain-bench-large-'));@@ -118,6 +173,17 @@ async function main(): Promise<void> {const size = parseSize(arg('--size', '100MB'));const kind = arg('--engine', 'pglite');if (process.argv.includes('--child')) return child(arg('--child', ''), kind);+ if (process.argv.includes('--flip-only')) {+ const dir = mkdtempSync(join(tmpdir(), 'gbrain-bench-large-'));+ const engine = await openEngine(kind, dir);+ try {+ process.stdout.write(JSON.stringify({ engine: kind, flip_retire: await flipRetireCells(engine) }) + '\n');+ } finally {+ await engine.disconnect();+ rmSync(dir, { recursive: true, force: true });+ }+ return;+ }if (size <= PAGE_MAX_BYTES) throw new Error(`--size ${size} is not above PAGE_MAX_BYTES (${PAGE_MAX_BYTES}); smaller files take the single-string import.`);const dir = mkdtempSync(join(tmpdir(), 'gbrain-bench-large-'));@@ -164,6 +230,7 @@ async function main(): Promise<void> {rows: { texts: rows.texts, manifest: rows.manifest, chunks: rows.chunks },append_10kb: { ms: round(appendMs), manifest: appended.manifestAtGeneration, chunks: appended.chunksAtGeneration },concurrent_small_put: { writes: latencies.length, p50_ms: pct(0.5), p95_ms: pct(0.95), max_ms: pct(1) },+ flip_retire: await flipRetireCells(engine),};if (process.argv.includes('--json')) process.stdout.write(JSON.stringify(report) + '\n');else process.stdout.write(JSON.stringify(report, null, 2) + '\n');diff --git a/src/core/page-body/limits.ts b/src/core/page-body/limits.tsindex 233389cde5..b9654988db 100644--- a/src/core/page-body/limits.ts+++ b/src/core/page-body/limits.ts@@ -53,8 +53,13 @@ export const STAGE_BATCH_BYTES: Readonly<Record<'postgres' | 'pglite', number>>pglite: 4 * 1024 ** 2,};-/** Chunk rows the generation flip retires inside the publication transaction; above it the page publishes unsealed and a projection job retires the rest. */-export const DEFAULT_FLIP_RETIRE_MAX_ROWS = 20_000;+/**+ * Chunk rows the generation flip retires inside the publication transaction; above it the page publishes unsealed and a+ * projection job retires the rest, this many rows per statement. Retiring an embedded row cannot be a HOT update, so it+ * writes a new `idx_chunks_embedding` (HNSW) entry: `scripts/bench-large-pages.ts` measured 1,000 embedded rows at 1.4 s+ * on PGLite and 0.6 s on Postgres, 20,000 at 129 s on PGLite and over the 5 s statement timeout on Postgres.+ */+export const DEFAULT_FLIP_RETIRE_MAX_ROWS = 1_000;/*** Snapshots of an inline page over the inline cap kept per page (Wave A). Eachdiff --git a/src/core/page-body/projection.ts b/src/core/page-body/projection.tsindex ec03ecbe56..04d2d2d59e 100644--- a/src/core/page-body/projection.ts+++ b/src/core/page-body/projection.ts@@ -20,12 +20,11 @@ import { faultPoint } from '../persistence/fault-points.ts';import { sealSegmentedPage } from '../persistence/segmented-apply.ts';import { PROJECTION_RETRY_READY_SQL } from '../page-state/projections.ts';import { occurrencesWithoutChunks, stageOccurrenceChunks } from './ingest.ts';+import { DEFAULT_FLIP_RETIRE_MAX_ROWS } from './limits.ts';import type { JsonObject } from './types.ts';export interface SegmentedProjectionResult { chunks: number; retired: number; sealed: boolean }-/** Chunk rows one retirement statement marks. */-const RETIRE_BATCH_ROWS = 5_000;interface ProjectionPage {id: number; body_generation: number | string; knowledge_revision: string; text_projection_revision: string | null;@@ -52,9 +51,9 @@ async function retireStaleChunks(engine: Pick<BrainEngine, 'executeRaw' | 'kind'OR (s.body_generation<=$2 AND EXISTS (SELECT 1 FROM content_chunks n WHERE n.page_id=s.page_id AND n.occurrence_id=s.occurrence_idAND n.retired_generation IS NULL AND n.body_generation>s.body_generation AND n.body_generation<=$2)))LIMIT $4)- RETURNING c.id`, [pageId, generation, live, RETIRE_BATCH_ROWS]);+ RETURNING c.id`, [pageId, generation, live, DEFAULT_FLIP_RETIRE_MAX_ROWS]);retired += rows.length;- if (rows.length < RETIRE_BATCH_ROWS) return retired;+ if (rows.length < DEFAULT_FLIP_RETIRE_MAX_ROWS) return retired;if (engine.kind === 'pglite') await new Promise(resolve => setImmediate(resolve));}}