Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

bench: land scripts/bench/sync-freshness.ts commit-to-searchable harness from gbra133-latency (GBRA-133 PR 2b.6)

garrytan749411bcc652
diff --git a/scripts/bench/sync-freshness-lib.ts b/scripts/bench/sync-freshness-lib.ts
new file mode 100644
index 0000000000..554399e8f0
--- /dev/null
+++ b/scripts/bench/sync-freshness-lib.ts
@@ -0,0 +1,286 @@
+/**
+ * Helpers for scripts/bench/sync-freshness.ts (GBRA-133 lane A): the synthetic
+ * corpus, commit generators, probes (in-process engine and MCP stdio), child
+ * processes with per-line arrival timestamps, and the statistics. Everything
+ * binds loopback and runs keyless.
+ */
+import { appendFileSync, cpSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
+import { dirname, join } from 'node:path';
+import { execFileSync } from 'node:child_process';
+import { cpus, totalmem } from 'node:os';
+
+export const now = () => performance.timeOrigin + performance.now();
+export const round1 = (n: number) => Math.round(n * 10) / 10;
+export const sleep = (ms: number) => new Promise<void>(r => setTimeout(r, ms));
+
+/** Linear-interpolated percentile. */
+export function pct(xs: number[], p: number): number | null {
+ if (!xs.length) return null;
+ const s = [...xs].sort((a, b) => a - b);
+ const rank = (p / 100) * (s.length - 1);
+ const lo = Math.floor(rank), hi = Math.ceil(rank);
+ return round1(s[lo]! + (s[hi]! - s[lo]!) * (rank - lo));
+}
+export interface Stats { n: number; p50: number | null; p95: number | null; max: number | null; min: number | null; mean: number | null; cv: number | null }
+export function stats(xs: number[]): Stats {
+ const n = xs.length;
+ if (!n) return { n, p50: null, p95: null, max: null, min: null, mean: null, cv: null };
+ const mean = xs.reduce((a, b) => a + b, 0) / n;
+ const sd = n > 1 ? Math.sqrt(xs.reduce((a, b) => a + (b - mean) ** 2, 0) / (n - 1)) : 0;
+ return { n, p50: pct(xs, 50), p95: pct(xs, 95), max: round1(Math.max(...xs)), min: round1(Math.min(...xs)), mean: round1(mean), cv: mean ? Math.round((sd / mean) * 1000) / 1000 : null };
+}
+
+export function git(root: string, ...args: string[]): string {
+ return execFileSync('git', ['-C', root, ...args], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], maxBuffer: 256 * 1024 * 1024 }).trim();
+}
+export function commit(root: string, message: string, paths: string[] | 'all'): string {
+ if (paths === 'all') git(root, 'add', '-A');
+ else for (let i = 0; i < paths.length; i += 500) git(root, 'add', '-A', '--', ...paths.slice(i, i + 500));
+ git(root, '-c', 'user.name=Bench Example', '-c', '[email protected]', 'commit', '-qm', message);
+ return git(root, 'rev-parse', 'HEAD');
+}
+
+export function machineInfo(): Record<string, unknown> {
+ let kernel = '';
+ try { kernel = execFileSync('uname', ['-r'], { encoding: 'utf8' }).trim(); } catch { /* best effort */ }
+ return { cores: cpus().length, cpu_model: cpus()[0]?.model ?? 'unknown', ram_gb: Math.round(totalmem() / 2 ** 30), kernel, bun: Bun.version };
+}
+
+// Synthetic corpus: deterministic pages, 1000 per directory.
+const WORDS = ['ledger', 'harbor', 'quartz', 'meadow', 'signal', 'lantern', 'orbit', 'canvas', 'summit', 'thicket', 'ember', 'falcon',
+ 'garnet', 'hollow', 'island', 'juniper', 'kestrel', 'linden', 'marble', 'nectar', 'oracle', 'pepper', 'quiver', 'russet'];
+export const pagePath = (i: number) => 'notes/d' + String(Math.floor(i / 1000)).padStart(3, '0') + '/n-' + String(i).padStart(6, '0') + '.md';
+export const addedPath = (n: number) => 'notes/added/a-' + String(n).padStart(6, '0') + '.md';
+export const slugOf = (path: string) => path.replace(/\.md$/, '');
+export function pageContent(i: number, status = 'baseline'): string {
+ const w = (k: number) => WORDS[(i * 7 + k * 13) % WORDS.length];
+ const body = Array.from({ length: 6 }, (_, p) => 'Paragraph ' + p + ' of note ' + i + ' covers the ' + w(p) + ' ' + w(p + 1) + ' plan, the ' + w(p + 2) + ' follow-up and a decision about the ' + w(p + 3) + ' review.').join('\n\n');
+ return '---\ntitle: Note ' + i + '\ntype: note\ntags: [bench, ' + w(0) + ']\n---\n# Note ' + i + '\n\nStatus: ' + status + '\n\n' + body + '\n\nSee [[' + slugOf(pagePath((i + 1) % 1000)) + ']].\n';
+}
+export function writeCorpus(root: string, pages: number): void {
+ for (let i = 0; i < pages; i++) {
+ const p = join(root, pagePath(i));
+ if (i % 1000 === 0) mkdirSync(dirname(p), { recursive: true });
+ writeFileSync(p, pageContent(i));
+ }
+ writeFileSync(join(root, 'README.md'), '# Bench brain (synthetic)\n');
+}
+/** Lowercase letters only, so every tokenizer keeps it as one word. */
+export function token(): string {
+ const a = 'abcdefghijklmnopqrstuvwxyz';
+ let t = 'zq';
+ for (let i = 0; i < 10; i++) t += a[Math.floor(Math.random() * 26)];
+ return t;
+}
+
+// Corpus state between runs: which pages are deleted, how many were added.
+export interface CorpusState { pages: number; deleted: number[]; added: number[]; nextAdd: number; cursor: number }
+export function readState(dir: string): CorpusState { return JSON.parse(readFileSync(join(dir, 'corpus-state.json'), 'utf8')); }
+export function writeState(dir: string, s: CorpusState): void { writeFileSync(join(dir, 'corpus-state.json'), JSON.stringify(s)); }
+
+export type CommitKind = 'single' | 'batch' | 'backlog';
+export interface Change { kind: CommitKind; token: string; sha: string; t_commit: number; t_commit_start: number; edited: string[]; added: string[]; deleted: string[] }
+
+/** Writes and commits one change. single: 1 edit; batch: 50 edits + 5 adds + 5 deletes; backlog: 1000 edits. */
+export function makeChange(root: string, dir: string, kind: CommitKind): Change {
+ const st = readState(dir);
+ const dead = new Set(st.deleted);
+ const tok = token();
+ const nEdit = kind === 'single' ? 1 : kind === 'batch' ? 50 : 1000;
+ const nAdd = kind === 'batch' ? 5 : 0;
+ const nDel = kind === 'batch' ? 5 : 0;
+ const seen = new Set<number>();
+ const pick = (): number => {
+ for (;;) { const i = st.cursor; st.cursor = (st.cursor + 7919) % st.pages; if (!dead.has(i) && !seen.has(i)) { seen.add(i); return i; } }
+ };
+ const editIdx = Array.from({ length: nEdit }, pick);
+ const delIdx = Array.from({ length: nDel }, pick);
+ const edited = editIdx.map(pagePath);
+ const added: string[] = [];
+ const deleted = delIdx.map(pagePath);
+ const t_commit_start = now();
+ for (const i of editIdx) writeFileSync(join(root, pagePath(i)), pageContent(i, 'edited ' + tok));
+ for (let k = 0; k < nAdd; k++) {
+ const n = st.nextAdd++;
+ const p = addedPath(n);
+ mkdirSync(dirname(join(root, p)), { recursive: true });
+ writeFileSync(join(root, p), '---\ntitle: Added ' + n + '\ntype: note\n---\n# Added ' + n + '\n\nStatus: added ' + tok + '\n\nA new bench note.\n');
+ added.push(p); st.added.push(n);
+ }
+ for (const p of deleted) rmSync(join(root, p));
+ for (const i of delIdx) st.deleted.push(i);
+ const sha = commit(root, 'bench ' + kind + ' ' + tok, [...edited, ...added, ...deleted]);
+ const t_commit = now();
+ writeState(dir, st);
+ return { kind, token: tok, sha, t_commit, t_commit_start, edited, added, deleted };
+}
+
+// Child processes with per-line arrival timestamps.
+export interface Line { t: number; stream: 'out' | 'err'; text: string }
+export interface Child { proc: ReturnType<typeof Bun.spawn>; lines: Line[]; exited: Promise<number>; onLine: (fn: (l: Line) => void) => void; exitAt: number | null; startedAt: number; stdoutRaw: () => string; write: (s: string) => void; kill: (sig?: NodeJS.Signals) => void }
+
+export function spawnChild(cmd: string[], opts: { cwd: string; env: Record<string, string | undefined>; logFile?: string; stdin?: boolean }): Child {
+ const startedAt = now();
+ const proc = Bun.spawn(cmd, { cwd: opts.cwd, env: opts.env as Record<string, string>, stdin: opts.stdin ? 'pipe' : 'ignore', stdout: 'pipe', stderr: 'pipe' });
+ const lines: Line[] = [];
+ const listeners: Array<(l: Line) => void> = [];
+ let raw = '';
+ const emit = (line: Line) => {
+ lines.push(line);
+ if (opts.logFile) appendFileSync(opts.logFile, new Date(line.t).toISOString() + ' ' + line.stream + ' ' + line.text + '\n');
+ for (const fn of listeners) fn(line);
+ };
+ const pump = async (stream: ReadableStream<Uint8Array>, name: 'out' | 'err') => {
+ const dec = new TextDecoder();
+ let buf = '';
+ for await (const chunk of stream) {
+ const t = now();
+ const text = dec.decode(chunk, { stream: true });
+ if (name === 'out') raw += text;
+ buf += text;
+ let nl: number;
+ while ((nl = buf.indexOf('\n')) >= 0) { emit({ t, stream: name, text: buf.slice(0, nl) }); buf = buf.slice(nl + 1); }
+ }
+ if (buf) emit({ t: now(), stream: name, text: buf });
+ };
+ const child: Child = {
+ proc, lines, exitAt: null, startedAt,
+ exited: Promise.all([pump(proc.stdout as ReadableStream<Uint8Array>, 'out'), pump(proc.stderr as ReadableStream<Uint8Array>, 'err'), proc.exited])
+ .then(([, , code]) => { child.exitAt = now(); return code as number; }),
+ onLine: fn => { listeners.push(fn); },
+ stdoutRaw: () => raw,
+ write: s => { const w = proc.stdin as unknown as { write: (s: string) => void; flush?: () => void }; w.write(s); w.flush?.(); },
+ kill: (sig = 'SIGTERM') => { try { proc.kill(sig); } catch { /* gone */ } },
+ };
+ return child;
+}
+export function waitLine(child: Child, re: RegExp, timeoutMs: number, fromT = 0): Promise<Line | null> {
+ const hit = child.lines.find(l => l.t >= fromT && re.test(l.text));
+ if (hit) return Promise.resolve(hit);
+ return new Promise(resolve => {
+ let done = false;
+ const finish = (v: Line | null) => { if (!done) { done = true; clearTimeout(timer); resolve(v); } };
+ const timer = setTimeout(() => finish(null), timeoutMs);
+ child.onLine(l => { if (l.t >= fromT && re.test(l.text)) finish(l); });
+ void child.exited.then(() => setTimeout(() => finish(null), 50));
+ });
+}
+
+// MCP stdio client over a gbrain serve child.
+export class McpClient {
+ private id = 0;
+ private pending = new Map<number, (v: { result?: any; error?: any }) => void>();
+ constructor(private child: Child) {
+ child.onLine(l => {
+ if (l.stream !== 'out' || !l.text.startsWith('{')) return;
+ try {
+ const msg = JSON.parse(l.text) as { id?: number; result?: unknown; error?: unknown };
+ const cb = typeof msg.id === 'number' ? this.pending.get(msg.id) : undefined;
+ if (cb) { this.pending.delete(msg.id!); cb(msg); }
+ } catch { /* not a JSON-RPC line */ }
+ });
+ }
+ request(method: string, params: unknown, timeoutMs = 30_000): Promise<{ result?: any; error?: any }> {
+ const id = ++this.id;
+ return new Promise((resolve, reject) => {
+ const timer = setTimeout(() => { this.pending.delete(id); reject(new Error('mcp ' + method + ' timed out')); }, timeoutMs);
+ this.pending.set(id, v => { clearTimeout(timer); resolve(v); });
+ this.child.write(JSON.stringify({ jsonrpc: '2.0', id, method, params }) + '\n');
+ });
+ }
+ async init(): Promise<void> {
+ const r = await this.request('initialize', { protocolVersion: '2025-06-18', capabilities: {}, clientInfo: { name: 'sync-freshness-bench', version: '1' } }, 120_000);
+ if (r.error) throw new Error('mcp initialize failed: ' + JSON.stringify(r.error));
+ this.child.write(JSON.stringify({ jsonrpc: '2.0', method: 'notifications/initialized' }) + '\n');
+ }
+ async call(name: string, args: Record<string, unknown>): Promise<string> {
+ const r = await this.request('tools/call', { name, arguments: args });
+ if (r.error) return 'ERROR ' + JSON.stringify(r.error);
+ return (r.result?.content ?? []).map((c: { text?: string }) => c.text ?? '').join('\n');
+ }
+}
+
+// Probes: has the change become visible?
+export interface ProbeResult { getOk: boolean; searchHits: number; searchOk: boolean; partial: boolean; embedded?: boolean }
+export interface Prober { check(c: Change): Promise<ProbeResult>; close(): Promise<void>; kind: string }
+
+export const expectedHits = (c: Change) => c.edited.length + c.added.length;
+/** get_page targets: the last edited path in path order, the last added page, and the first deleted page (must be gone). */
+export function getTargets(c: Change): { present: string[]; gone: string[] } {
+ return { present: [[...c.edited].sort().at(-1)!, ...(c.added.length ? [c.added.at(-1)!] : [])], gone: c.deleted.slice(0, 1) };
+}
+
+export async function engineProber(gbrainRoot: string, databaseUrl: string, embed = false): Promise<Prober> {
+ const { PostgresEngine } = await import(join(gbrainRoot, 'src/core/postgres-engine.ts'));
+ const engine = new PostgresEngine();
+ await engine.connect({ database_url: databaseUrl, poolSize: 2 });
+ return {
+ kind: 'engine',
+ async check(c) {
+ const { present, gone } = getTargets(c);
+ const want = expectedHits(c);
+ let getOk = true;
+ for (const path of present) {
+ const p = await engine.getPage(slugOf(path), { sourceId: 'bench' }).catch(() => null);
+ if (!p || !String(p.compiled_truth ?? '').includes(c.token)) { getOk = false; break; }
+ }
+ if (getOk) for (const path of gone) if (await engine.getPage(slugOf(path), { sourceId: 'bench' }).catch(() => null)) { getOk = false; break; }
+ const hits = await engine.searchKeyword(c.token, { limit: 100, sourceId: 'bench' }).catch(() => []);
+ const slugs = new Set((hits as Array<{ slug: string }>).map(h => h.slug));
+ let embedded: boolean | undefined;
+ if (embed) {
+ const [{ total, done }] = await engine.executeRaw("SELECT count(*)::int AS total, count(*) FILTER (WHERE c.embedding IS NOT NULL)::int AS done FROM content_chunks c JOIN pages p ON p.id=c.page_id WHERE p.source_id='bench' AND p.deleted_at IS NULL AND c.chunk_text LIKE $1", ['%' + c.token + '%']);
+ embedded = total >= Math.min(want, 1) && done === total && total > 0;
+ }
+ if (want <= 100) return { getOk, searchHits: slugs.size, searchOk: slugs.size >= want, partial: slugs.size > 0, embedded };
+ const [{ n }] = await engine.executeRaw("SELECT count(*)::int AS n FROM content_chunks c JOIN pages p ON p.id=c.page_id WHERE p.source_id='bench' AND p.deleted_at IS NULL AND c.chunk_text LIKE $1", ['%' + c.token + '%']);
+ return { getOk, searchHits: n, searchOk: slugs.size >= 100 && n >= want, partial: slugs.size > 0 };
+ },
+ async close() { await engine.disconnect(); },
+ };
+}
+
+export function mcpProber(client: McpClient): Prober {
+ return {
+ kind: 'mcp',
+ async check(c) {
+ const { present, gone } = getTargets(c);
+ const want = expectedHits(c);
+ let getOk = true;
+ for (const path of present) {
+ const text = await client.call('get_page', { slug: slugOf(path), source_id: 'bench' }).catch(e => String(e));
+ if (!text.includes(c.token)) { getOk = false; break; }
+ }
+ if (getOk) for (const path of gone) {
+ const text = await client.call('get_page', { slug: slugOf(path), source_id: 'bench' }).catch(e => String(e));
+ if (text.includes('Status: baseline')) { getOk = false; break; }
+ }
+ const text = await client.call('search', { query: c.token, limit: 100, source_id: 'bench', match: 'keyword' }).catch(e => String(e));
+ const slugs = new Set([...text.matchAll(/notes\/(?:d\d{3}\/n-\d{6}|added\/a-\d{6})/g)].map(m => m[0]));
+ return { getOk, searchHits: slugs.size, searchOk: slugs.size >= Math.min(want, 100), partial: slugs.size > 0 };
+ },
+ async close() { /* the serve owns it */ },
+ };
+}
+
+export interface Visibility { t_get: number | null; t_search: number | null; t_first_hit: number | null; t_embedded?: number | null; probes: number; probe_ms_mean: number }
+/** Polls until both get_page and search see the change, or the deadline. */
+export async function pollVisible(prober: Prober, c: Change, deadline: number, everyMs: number): Promise<Visibility> {
+ let t_get: number | null = null, t_search: number | null = null, t_first: number | null = null, t_emb: number | null = null;
+ let probes = 0, probeMs = 0, wantEmb = false;
+ while (now() < deadline && (t_get === null || t_search === null || (wantEmb && t_emb === null))) {
+ const t0 = now();
+ const r = await prober.check(c);
+ const t1 = now();
+ probes++; probeMs += t1 - t0;
+ if (r.getOk && t_get === null) t_get = t1;
+ if (r.searchOk && t_search === null) t_search = t1;
+ if (r.partial && t_first === null) t_first = t1;
+ if (r.embedded !== undefined) { wantEmb = true; if (r.embedded && t_emb === null) t_emb = t1; }
+ if (t_get === null || t_search === null || (wantEmb && t_emb === null)) await sleep(Math.max(0, everyMs - (now() - t0)));
+ }
+ return { t_get, t_search, t_first_hit: t_first, t_embedded: wantEmb ? t_emb : undefined, probes, probe_ms_mean: probes ? round1(probeMs / probes) : 0 };
+}
+
+export function copyDir(from: string, to: string): void { rmSync(to, { recursive: true, force: true }); cpSync(from, to, { recursive: true }); }
+export { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync };
diff --git a/scripts/bench/sync-freshness-matrix.sh b/scripts/bench/sync-freshness-matrix.sh
new file mode 100755
index 0000000000..2e0ac32283
--- /dev/null
+++ b/scripts/bench/sync-freshness-matrix.sh
@@ -0,0 +1,72 @@
+#!/usr/bin/env bash
+# GBRA-133 lane A: the cell sets behind docs of scripts/bench/sync-freshness.ts.
+# scripts/bench/sync-freshness-matrix.sh <core|rtt|pollers|extras|embed|big|backlog|noop|pr4b> [work-dir] [pr4b-checkout]
+# Cells run one at a time (they share the CPU). Each writes <work-dir>/results/<cell>.json;
+# render the table with: bun scripts/bench/sync-freshness.ts --report <work-dir>/results
+set -uo pipefail
+SET=${1:?set}; W=${2:-$PWD/.context/bench/sync-freshness}; PR4B=${3:-}
+B="bun $(dirname "$0")/sync-freshness.ts --work-dir $W"
+run() { echo "== $*" >&2; $B "$@" 2>&1 | grep -E '^\[freshness' | grep -E 'wrote|ERROR|never|cell ' >&2 || true; }
+case "$SET" in
+ core)
+ for P in 1000 10000; do for E in postgres pglite; do for M in classic managed; do
+ run --engine $E --mode $M --pages $P --commit single --trigger manual --reps 20
+ run --engine $E --mode $M --pages $P --commit batch --trigger manual --reps 10
+ if [ $M = managed ]; then
+ run --engine $E --mode $M --pages $P --commit single --trigger serve --reps 20
+ run --engine $E --mode $M --pages $P --commit batch --trigger serve --reps 10
+ else
+ run --engine $E --mode $M --pages $P --commit single --trigger serve --reps 1 --timeout 60
+ fi
+ done; done; done ;;
+ rtt)
+ for M in classic managed; do
+ run --engine postgres --mode $M --pages 10000 --commit single --trigger manual --reps 20 --rtt 57
+ run --engine postgres --mode $M --pages 10000 --commit batch --trigger manual --reps 10 --rtt 57
+ done
+ run --engine postgres --mode managed --pages 10000 --commit single --trigger serve --reps 20 --rtt 57
+ run --engine postgres --mode managed --pages 10000 --commit batch --trigger serve --reps 10 --rtt 57 ;;
+ pollers)
+ run --engine postgres --mode managed --pages 10000 --commit single --trigger jobs --reps 10
+ run --engine postgres --mode classic --pages 10000 --commit single --trigger jobs --reps 10
+ run --engine postgres --mode classic --pages 10000 --commit single --trigger watch --interval 60 --reps 6
+ run --engine postgres --mode managed --pages 10000 --commit single --trigger watch --interval 60 --reps 6
+ run --engine postgres --mode managed --pages 10000 --commit single --trigger autopilot --reps 4 ;;
+ extras)
+ run --engine postgres --mode managed --pages 10000 --commit single --trigger serve --scenario during-sync --reps 5
+ run --engine pglite --mode managed --pages 10000 --commit single --trigger serve --scenario during-sync --reps 5 ;;
+ embed)
+ for M in classic managed; do for L in 200 2000; do
+ run --engine postgres --mode $M --pages 1000 --commit single --trigger manual --reps 5 --embed stub --embed-latency-ms $L --timeout 120
+ done; done
+ run --engine postgres --mode managed --pages 1000 --commit single --trigger serve --reps 5 --embed stub --embed-latency-ms 200 --timeout 120
+ run --engine postgres --mode managed --pages 1000 --commit single --trigger serve --reps 5 --embed stub --embed-latency-ms 2000 --timeout 120 ;;
+ big)
+ for E in postgres pglite; do for M in classic managed; do
+ run --engine $E --mode $M --pages 100000 --commit single --trigger manual --reps 10
+ [ $M = managed ] && run --engine $E --mode $M --pages 100000 --commit single --trigger serve --reps 10
+ done; done
+ for M in classic managed; do run --engine postgres --mode $M --pages 100000 --commit single --trigger manual --reps 10 --rtt 57; done
+ run --engine postgres --mode managed --pages 100000 --commit single --trigger serve --reps 10 --rtt 57 ;;
+ backlog)
+ for P in 10000 100000; do for E in postgres pglite; do for M in classic managed; do
+ run --engine $E --mode $M --pages $P --commit backlog --trigger manual --reps 2
+ done; done; done ;;
+ noop)
+ for P in 10000 100000; do
+ run --engine postgres --mode managed --pages $P --commit noop --trigger manual --reps 10 --trace
+ run --engine pglite --mode managed --pages $P --commit noop --trigger manual --reps 10
+ done
+ run --engine postgres --mode managed --pages 10000 --commit noop --trigger manual --reps 10 --trace --rtt 57 ;;
+ pr4b)
+ : "${PR4B:?pr4b checkout path}"
+ for ROOT in "$(cd "$(dirname "$0")/../.." && pwd)" "$PR4B"; do
+ run --gbrain-root "$ROOT" --engine postgres --mode managed --pages 10000 --commit single --trigger manual --reps 20
+ run --gbrain-root "$ROOT" --engine postgres --mode managed --pages 10000 --commit single --trigger serve --reps 20
+ run --gbrain-root "$ROOT" --engine postgres --mode managed --pages 10000 --commit single --trigger manual --reps 20 --rtt 57
+ run --gbrain-root "$ROOT" --engine postgres --mode managed --pages 10000 --commit single --trigger serve --reps 20 --rtt 57
+ run --gbrain-root "$ROOT" --engine pglite --mode managed --pages 10000 --commit single --trigger manual --reps 20
+ run --gbrain-root "$ROOT" --engine pglite --mode managed --pages 10000 --commit single --trigger serve --reps 20
+ done ;;
+ *) echo "unknown set $SET" >&2; exit 2 ;;
+esac
diff --git a/scripts/bench/sync-freshness.ts b/scripts/bench/sync-freshness.ts
new file mode 100644
index 0000000000..a85a61c28e
--- /dev/null
+++ b/scripts/bench/sync-freshness.ts
@@ -0,0 +1,466 @@
+#!/usr/bin/env bun
+/**
+ * GBRA-133 lane A: commit-to-searchable latency bench. Opt-in, never run in CI.
+ *
+ * bun scripts/bench/sync-freshness.ts --engine pglite|postgres --mode classic|managed --pages N
+ * --commit single|batch|backlog --trigger serve|autopilot|jobs|watch|manual --reps N [--json]
+ * [--rtt MS] [--interval S] [--timeout S] [--scenario during-sync] [--gbrain-root <checkout>]
+ * [--work-dir <dir>] [--label <name>] [--rebuild] [--probe-ms 100]
+ * bun scripts/bench/sync-freshness.ts --report <results dir> # markdown table of every cell JSON
+ *
+ * Builds (once, then reuses) a synthetic git brain of N pages for the engine,
+ * mode and gbrain checkout: classic import first, then for managed the real
+ * "gbrain sources writer claim/activate" CLI (a managed brain of the same size
+ * is cloned from the classic one). Per rep it commits a change with a fresh
+ * token, fires or waits for the trigger, and polls until get_page returns the
+ * new content and a keyword search returns every changed page.
+ *
+ * Probes: Postgres reads through an in-process engine on a direct (un-proxied)
+ * connection every --probe-ms; PGLite with a serve reads through that serve's
+ * own MCP stdio (get_page, search); PGLite without a serve cannot be read while
+ * the syncing process holds the single-writer lock, so a manual sync is timed
+ * to its completion line and verified after exit, and resident pollers are
+ * derived (see the report). Every child line is timestamped on arrival, which
+ * gives the phase breakdown (boot, discovery/freeze, publish, tail).
+ * Postgres runs in Docker (gbrain-bench-pg) with toxiproxy for --rtt, the same
+ * harness as scripts/bench/managed-sync-catchup.ts. Keyless: provider keys are
+ * stripped from every child.
+ */
+import { join, resolve } from 'node:path';
+import { execFileSync } from 'node:child_process';
+import { readdirSync } from 'node:fs';
+import postgres from '#postgres';
+import { startHarness, measureRtt, startEmbeddingStub, readTrace, groupStatements, classify, type Harness } from './managed-sync-catchup-lib.ts';
+import {
+ copyDir, engineProber, existsSync, git, machineInfo, makeChange, mcpProber, McpClient, mkdirSync, now, pollVisible, readFileSync, rmSync, round1,
+ sleep, spawnChild, stats, waitLine, writeCorpus, writeFileSync, writeState, getTargets, slugOf, expectedHits,
+ type Change, type Child, type CommitKind, type Prober, type Line, type Stats,
+} from './sync-freshness-lib.ts';
+
+const REPO = resolve(import.meta.dir, '../..');
+const argv = process.argv.slice(2);
+const flag = (name: string, fallback: string) => { const i = argv.indexOf('--' + name); return i >= 0 && argv[i + 1] ? argv[i + 1]! : fallback; };
+const has = (name: string) => argv.includes('--' + name);
+
+if (has('report')) { report(flag('report', '.').split(',').map(d => resolve(d)).join(',')); process.exit(0); }
+
+const ENGINE = flag('engine', 'pglite') as 'pglite' | 'postgres';
+const MODE = flag('mode', 'classic') as 'classic' | 'managed';
+const PAGES = Number(flag('pages', '1000'));
+const COMMIT = flag('commit', 'single') as CommitKind | 'noop';
+const TRIGGER = flag('trigger', 'manual') as 'serve' | 'autopilot' | 'jobs' | 'watch' | 'manual';
+const REPS = Number(flag('reps', '10'));
+const RTT = Number(flag('rtt', '0'));
+const SCENARIO = flag('scenario', 'plain');
+const PROBE_MS = Number(flag('probe-ms', '100'));
+const GROOT = resolve(flag('gbrain-root', REPO));
+const WORK = resolve(flag('work-dir', join(REPO, '.context/bench/sync-freshness')));
+const CLI = join(GROOT, 'src/cli.ts');
+/** The product revision (last commit touching src/), so bench-only commits keep reusing the same brains. */
+const REV = git(GROOT, 'log', '-1', '--format=%h', '--abbrev=8', '--', 'src', 'package.json');
+const LABEL = flag('label', git(GROOT, 'rev-parse', '--abbrev-ref', 'HEAD') + '@' + REV);
+const INTERVAL = Number(flag('interval', TRIGGER === 'autopilot' ? '300' : '60'));
+const TIMEOUT_S = Number(flag('timeout', String(TRIGGER === 'autopilot' ? INTERVAL * 2 + 600 : TRIGGER === 'watch' ? INTERVAL + 300 : COMMIT === 'backlog' ? 3600 : 300)));
+const JSON_OUT = has('json');
+const EMBED = flag('embed', 'off') as 'off' | 'stub';
+const EMBED_LATENCY_MS = Number(flag('embed-latency-ms', '200'));
+let stubUrl = '';
+const TRACE = has('trace');
+if (COMMIT === 'noop' && TRIGGER !== 'manual') { console.error('--commit noop measures a manual sync with nothing to do; use --trigger manual'); process.exit(2); }
+if (!['pglite', 'postgres'].includes(ENGINE) || !['classic', 'managed'].includes(MODE) || !['single', 'batch', 'backlog', 'noop'].includes(COMMIT)
+ || !['serve', 'autopilot', 'jobs', 'watch', 'manual'].includes(TRIGGER) || !(PAGES > 0) || !(REPS > 0)) {
+ console.error('usage: see the header of scripts/bench/sync-freshness.ts'); process.exit(2);
+}
+if (RTT && ENGINE !== 'postgres') { console.error('--rtt applies to --engine postgres'); process.exit(2); }
+const log = (m: string) => console.error('[freshness ' + new Date().toISOString().slice(11, 19) + '] ' + m);
+
+const brainKey = (mode: string) => ENGINE + '-' + mode + '-' + PAGES + (EMBED === 'stub' ? '-embed' : '') + '-' + REV;
+const brainDir = (mode: string) => join(WORK, 'brains', brainKey(mode));
+const dbName = (mode: string) => ('gbf_' + brainKey(mode)).replace(/\W/g, '_').toLowerCase();
+const DIR = brainDir(MODE);
+const HOME = join(DIR, 'home');
+const ROOT = join(DIR, 'repo');
+
+let harness: Harness | null = null;
+const directUrl = (mode = MODE) => harness!.directUrl(dbName(mode));
+
+function childEnv(extra: Record<string, string | undefined> = {}): Record<string, string | undefined> {
+ const env: Record<string, string | undefined> = { ...process.env };
+ for (const k of Object.keys(env)) if (/_API_KEY$|_API_TOKEN$|^DATABASE_URL$|^GBRAIN_DATABASE_URL$|^OPENAI_BASE_URL$|^GBRAIN_HOME$/.test(k)) delete env[k];
+ return { ...env, GBRAIN_HOME: HOME, GBRAIN_NO_BANNER: '1', GBRAIN_BACKUP_CHECK: '0', GBRAIN_SKILLS_DIR: join(HOME, 'skills'), NO_COLOR: '1',
+ ...(EMBED === 'stub' ? { OPENAI_API_KEY: 'sk-bench-stub-not-a-key', OPENAI_BASE_URL: stubUrl } : {}), ...extra };
+}
+/** Async on purpose: the embedding stub answers from this process's event loop. */
+async function gb(args: string[], opts: { home?: string; allowFail?: boolean; timeoutMs?: number } = {}): Promise<{ code: number; stdout: string; stderr: string; json: any }> {
+ const p = Bun.spawn([process.execPath, CLI, ...args], { cwd: opts.home ?? HOME, env: childEnv(opts.home ? { GBRAIN_HOME: opts.home, GBRAIN_SKILLS_DIR: join(opts.home, 'skills') } : {}) as Record<string, string>,
+ stdin: 'ignore', stdout: 'pipe', stderr: 'pipe', timeout: opts.timeoutMs ?? 4 * 3600_000 });
+ const [stdout, stderr, exitCode] = await Promise.all([new Response(p.stdout).text(), new Response(p.stderr).text(), p.exited]);
+ const r = { exitCode };
+ let json: any = null;
+ try { json = JSON.parse(stdout); } catch { const last = stdout.trim().split('\n').reverse().find(l => l.startsWith('{')); try { json = last ? JSON.parse(last) : null; } catch { json = null; } }
+ if (r.exitCode !== 0 && !opts.allowFail) throw new Error('gbrain ' + args.join(' ') + ' exited ' + r.exitCode + '\n' + stdout.slice(-1500) + '\n' + stderr.slice(-1500));
+ return { code: r.exitCode ?? -1, stdout, stderr, json };
+}
+function writeConfig(home: string, mode: string, url?: string): void {
+ mkdirSync(join(home, '.gbrain'), { recursive: true });
+ mkdirSync(join(home, 'skills'), { recursive: true });
+ if (!existsSync(join(home, 'skills', 'RESOLVER.md'))) writeFileSync(join(home, 'skills', 'RESOLVER.md'), '# Bench fixture skills\n');
+ const file = join(home, '.gbrain', 'config.json');
+ const prior = existsSync(file) ? JSON.parse(readFileSync(file, 'utf8')) : {};
+ const base = ENGINE === 'postgres' ? { engine: 'postgres', database_url: url ?? harness!.directUrl(dbName(mode)) } : { engine: 'pglite', database_path: join(home, '.gbrain', 'brain.pglite') };
+ const embedding = EMBED === 'stub' ? { embedding_disabled: false, embedding_model: 'openai:text-embedding-3-small', embedding_dimensions: 1536 } : { embedding_disabled: true };
+ writeFileSync(file, JSON.stringify({ ...prior, ...base, ...embedding, self_upgrade: { mode: 'off' } }, null, 2) + '\n');
+}
+async function admin<T>(fn: (sql: ReturnType<typeof postgres>) => Promise<T>, db = 'postgres'): Promise<T> {
+ const sql = postgres(harness!.directUrl(db), { max: 1, onnotice: () => {} });
+ try { return await fn(sql); } finally { await sql.end(); }
+}
+async function dbExists(db: string): Promise<boolean> { return admin(async sql => (await sql.unsafe('SELECT 1 FROM pg_database WHERE datname=$1', [db])).length > 0); }
+/** A cached brain counts only while its database still exists: a recreated gbrain-bench-pg container (machine restart) loses every database. */
+async function brainBuilt(mode: 'classic' | 'managed'): Promise<boolean> {
+ if (!existsSync(join(brainDir(mode), 'ready.json'))) return false;
+ if (ENGINE !== 'postgres' || await dbExists(dbName(mode))) return true;
+ log('cached ' + brainKey(mode) + ' has no database ' + dbName(mode) + '; rebuilding');
+ return false;
+}
+
+async function syncUntilDone(home: string, label: string): Promise<{ ms: number; runs: number }> {
+ const t = now();
+ for (let i = 0; ; i++) {
+ const r = await gb(['sync', '--source', 'bench', '--no-pull', ...(EMBED === 'stub' ? [] : ['--no-embed']), '--json'], { home, allowFail: true });
+ const status = r.json?.sync_status ?? r.json?.outcome;
+ log(label + ' sync pass ' + (i + 1) + ': ' + status + ' (' + round1((now() - t) / 1000) + ' s)');
+ if (['synced', 'first_sync', 'up_to_date'].includes(status)) return { ms: now() - t, runs: i + 1 };
+ if (i > 200 || (r.code !== 0 && status !== 'partial')) throw new Error(label + ' sync did not converge: ' + r.stdout.slice(-1500) + r.stderr.slice(-1500));
+ }
+}
+
+/**
+ * A full import leaves the mention-link pass to later (links_extraction_lag), and a managed sync then spends its tail on
+ * that backlog. Cells measure steady state, so the build clears it the way the sync's own hint says.
+ */
+async function settleLinks(home: string): Promise<{ ms: number; runs: number; last: string }> {
+ const t = now();
+ let last = '';
+ for (let i = 0; i < 20; i++) {
+ const r = await gb(['extract', '--stale', '--catch-up', '--source-id', 'bench', '--brain', 'host'], { home, allowFail: true });
+ last = (r.stdout + r.stderr).trim().split('\n').slice(-3).join(' | ').slice(0, 400);
+ log('links settle pass ' + (i + 1) + ' (' + round1((now() - t) / 1000) + ' s): ' + last);
+ if (r.code !== 0 || !/Actionable: [1-9]\d* page\(s\) still need|[1-9]\d* page\(s\) due for the mention pass/.test(r.stdout + r.stderr)) return { ms: round1(now() - t), runs: i + 1, last };
+ }
+ return { ms: round1(now() - t), runs: 20, last };
+}
+
+async function activate(home: string): Promise<number> {
+ const t = now();
+ let state = (await gb(['sources', 'writer', 'status', '--json'], { home })).json.admin_state as string;
+ await gb(['sources', 'writer', 'claim', 'bench', '--path', join(home, '..', 'repo'), '--admin-intent', 'writer_claim', '--expected-state', state, '--json'], { home });
+ state = (await gb(['sources', 'writer', 'status', '--json'], { home })).json.admin_state as string;
+ const a = await gb(['sources', 'writer', 'activate', '--confirm-quiesced', '--admin-intent', 'writer_activate', '--expected-state', state, '--json'], { home });
+ if (a.json?.enabled !== true) throw new Error('activation did not enable managed persistence: ' + a.stdout.slice(-1500));
+ return now() - t;
+}
+
+/** Builds the brain for (engine, mode, pages, checkout) unless it is already built. */
+async function ensureBrain(mode: 'classic' | 'managed'): Promise<Record<string, unknown>> {
+ const dir = brainDir(mode), home = join(dir, 'home'), root = join(dir, 'repo');
+ const ready = join(dir, 'ready.json');
+ if (!has('rebuild') && await brainBuilt(mode)) {
+ const prior = JSON.parse(readFileSync(ready, 'utf8'));
+ if (prior.links_settled) return prior;
+ writeConfig(home, mode);
+ prior.links_settled = await settleLinks(home);
+ writeFileSync(ready, JSON.stringify(prior, null, 2));
+ return prior;
+ }
+ rmSync(dir, { recursive: true, force: true });
+ mkdirSync(dir, { recursive: true });
+ const build: Record<string, unknown> = { engine: ENGINE, mode, pages: PAGES, rev: REV, built_at: new Date().toISOString() };
+ if (mode === 'managed' && await brainBuilt('classic')) {
+ log('cloning classic ' + brainKey('classic') + ' for managed');
+ const t = now();
+ copyDir(join(brainDir('classic'), 'repo'), root);
+ writeFileSync(join(dir, 'corpus-state.json'), readFileSync(join(brainDir('classic'), 'corpus-state.json')));
+ if (ENGINE === 'postgres') {
+ await admin(sql => sql.unsafe('SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname=$1 AND pid<>pg_backend_pid()', [dbName('classic')]));
+ if (await dbExists(dbName(mode))) await admin(sql => sql.unsafe('DROP DATABASE ' + dbName(mode) + ' WITH (FORCE)'));
+ await admin(sql => sql.unsafe('CREATE DATABASE ' + dbName(mode) + ' TEMPLATE ' + dbName('classic')));
+ writeConfig(home, mode);
+ await admin(sql => sql.unsafe("UPDATE sources SET local_path=$1 WHERE id='bench'", [root]), dbName(mode));
+ } else {
+ mkdirSync(join(home, '.gbrain'), { recursive: true });
+ for (const f of readdirSync(join(brainDir('classic'), 'home', '.gbrain'))) if (f.startsWith('brain.pglite') && !f.includes('owner')) copyDir(join(brainDir('classic'), 'home', '.gbrain', f), join(home, '.gbrain', f));
+ writeConfig(home, mode);
+ await withPglite(home, e => e.executeRaw("UPDATE sources SET local_path=$1 WHERE id='bench'", [root]));
+ }
+ build.cloned_from = brainKey('classic');
+ build.clone_ms = round1(now() - t);
+ } else {
+ log('building ' + brainKey(mode) + ': writing ' + PAGES + ' pages');
+ let t = now();
+ mkdirSync(root, { recursive: true });
+ git(root, 'init', '-q');
+ writeCorpus(root, PAGES);
+ git(root, 'add', '-A');
+ git(root, '-c', 'user.name=Bench Example', '-c', '[email protected]', 'commit', '-qm', 'corpus');
+ writeState(dir, { pages: PAGES, deleted: [], added: [], nextAdd: 0, cursor: 0 });
+ build.corpus_ms = round1(now() - t);
+ if (ENGINE === 'postgres') {
+ if (await dbExists(dbName(mode))) await admin(sql => sql.unsafe('DROP DATABASE ' + dbName(mode) + ' WITH (FORCE)'));
+ await admin(sql => sql.unsafe('CREATE DATABASE ' + dbName(mode)));
+ }
+ writeConfig(home, mode);
+ t = now();
+ await gb(['sources', 'add', 'bench', '--path', root, '--no-federated'], { home });
+ build.sources_add_ms = round1(now() - t);
+ const s = await syncUntilDone(home, 'initial import');
+ build.initial_import_ms = round1(s.ms);
+ build.initial_import_pages_per_s = round1(PAGES / (s.ms / 1000));
+ }
+ if (mode === 'classic') build.links_settled = await settleLinks(home);
+ if (mode === 'managed') {
+ build.activate_ms = round1(await activate(home));
+ build.first_managed_sync = await syncUntilDone(home, 'post-activation');
+ build.links_settled = await settleLinks(home);
+ }
+ writeFileSync(ready, JSON.stringify(build, null, 2));
+ return build;
+}
+
+async function withPglite<T>(home: string, fn: (engine: any) => Promise<T>): Promise<T> {
+ const prior = process.env.GBRAIN_HOME;
+ process.env.GBRAIN_HOME = home;
+ const { PGLiteEngine } = await import(join(GROOT, 'src/core/pglite-engine.ts'));
+ const engine = new PGLiteEngine();
+ await engine.connect({ engine: 'pglite', database_path: join(home, '.gbrain', 'brain.pglite') });
+ try { return await fn(engine); } finally { await engine.disconnect(); if (prior === undefined) delete process.env.GBRAIN_HOME; else process.env.GBRAIN_HOME = prior; }
+}
+async function pgliteVerify(c: Change): Promise<{ getOk: boolean; searchOk: boolean; hits: number }> {
+ return withPglite(HOME, async engine => {
+ const { present, gone } = getTargets(c);
+ let getOk = true;
+ for (const p of present) { const page = await engine.getPage(slugOf(p), { sourceId: 'bench' }); if (!page || !String(page.compiled_truth).includes(c.token)) getOk = false; }
+ for (const p of gone) if (await engine.getPage(slugOf(p), { sourceId: 'bench' })) getOk = false;
+ const hits = new Set((await engine.searchKeyword(c.token, { limit: 100, sourceId: 'bench' })).map((h: { slug: string }) => h.slug)).size;
+ const [{ n }] = await engine.executeRaw("SELECT count(*)::int AS n FROM pages WHERE source_id='bench' AND deleted_at IS NULL AND compiled_truth LIKE $1", ['%' + c.token + '%']);
+ return { getOk, searchOk: hits >= Math.min(100, expectedHits(c)) && n >= expectedHits(c), hits, pages_with_token: n };
+ });
+}
+
+// ---------------------------------------------------------------------------
+// Phase extraction from timestamped child lines.
+// ---------------------------------------------------------------------------
+const PHASE_RES: Array<[string, RegExp]> = [
+ ['first_line', /./],
+ ['frozen', /entries frozen|managed catch-up:/],
+ ['first_progress', /\d+\/\d+ processed/],
+ ['done', /^Synced [0-9a-f]+\.\.|^First sync complete|Managed sync synced|^\[\d\d:\d\d:\d\d\] Synced:|Already up to date|up to date/],
+ ['serve_job_started', /on-commit: .*started managed sync job/],
+ ['serve_resuming', /on-commit: .*resuming/],
+ ['cycle', /\[cycle\]|"event":"cycle"/],
+];
+function phaseMarks(lines: Line[], from: number, to: number): Record<string, number> {
+ const out: Record<string, number> = {};
+ for (const l of lines) {
+ if (l.t < from || l.t > to) continue;
+ const gp = /\[gbrain phase\] (\S+) done (\d+)ms/.exec(l.text);
+ if (gp && out['gp:' + gp[1]] === undefined) out['gp:' + gp[1]] = Number(gp[2]);
+ for (const [name, re] of PHASE_RES) if (out[name] === undefined && (name !== 'first_line' || l.stream === 'err') && re.test(l.text.replace(/^\[\d\d:\d\d:\d\d\] (?=\[)/, ''))) out[name] = round1(l.t - from);
+ }
+ return out;
+}
+
+// ---------------------------------------------------------------------------
+// One cell.
+// ---------------------------------------------------------------------------
+interface Rep { rep: number; sha: string; token: string; files: number; get_ms: number | null; search_ms: number | null; first_hit_ms: number | null; commit_git_ms: number;
+ source: string; phases: Record<string, number>; probes?: number; probe_ms_mean?: number; exit_ms?: number | null; verified?: unknown; note?: string; error?: string }
+
+async function main(): Promise<void> {
+ mkdirSync(WORK, { recursive: true });
+ if (ENGINE === 'postgres') harness = await startHarness({ pgPort: 55432, proxyPort: Number(flag('proxy-port', String(56000 + (process.pid % 1000)))), apiPort: 58474, keep: true });
+ const stub = EMBED === 'stub' ? await startEmbeddingStub({ latencyMs: EMBED_LATENCY_MS, maxConcurrent: 8, dims: 1536 }) : null;
+ if (stub) stubUrl = stub.url;
+ if (MODE === 'managed' && !(await brainBuilt('classic')) && PAGES >= 10000) await ensureBrain('classic');
+ const build = await ensureBrain(MODE);
+ const brainUrl = ENGINE === 'postgres' ? (RTT ? harness!.proxyUrl(dbName(MODE)) : directUrl()) : undefined;
+ writeConfig(HOME, MODE, brainUrl);
+ let measuredRtt: number | null = null;
+ if (ENGINE === 'postgres') { await harness!.setRtt(RTT); measuredRtt = await measureRtt(brainUrl!); }
+ const cellId = [ENGINE, MODE, PAGES, COMMIT, TRIGGER, RTT ? 'rtt' + RTT : '', SCENARIO !== 'plain' ? SCENARIO : '', EMBED === 'stub' ? 'embed' + EMBED_LATENCY_MS : '', LABEL.replace(/[^\w.-]/g, '_')].filter(Boolean).join('-');
+ const outDir = join(WORK, 'results');
+ mkdirSync(join(outDir, 'logs'), { recursive: true });
+ const logFile = join(outDir, 'logs', cellId + '-' + Date.now() + '.log');
+ log('cell ' + cellId + ' (' + REPS + ' reps)');
+
+ const reps: Rep[] = [];
+ const notes: string[] = [];
+ let resident: Child | null = null;
+ let prober: Prober | null = ENGINE === 'postgres' ? await engineProber(GROOT, directUrl(), EMBED === 'stub') : null;
+
+ if (TRIGGER === 'serve') {
+ await gb(['config', 'set', 'sync.on_commit', 'true']);
+ resident = spawnChild([process.execPath, CLI, 'serve'], { cwd: HOME, env: childEnv(), logFile, stdin: true });
+ const mcp = new McpClient(resident);
+ await mcp.init();
+ if (!prober) prober = mcpProber(mcp);
+ const enabled = await waitLine(resident, /on-commit: enabled/, 45_000);
+ notes.push(enabled ? 'serve on-commit watcher enabled ' + round1(enabled.t - resident.startedAt) + ' ms after spawn' : 'serve printed no on-commit enabled line within 45 s');
+ await sleep(4000);
+ } else if (TRIGGER === 'watch') {
+ resident = spawnChild([process.execPath, CLI, 'sync', '--watch', '--interval', String(INTERVAL), '--source', 'bench', '--no-pull'], { cwd: HOME, env: childEnv(), logFile });
+ await waitLine(resident, /Watching for changes/, 120_000);
+ if (ENGINE === 'pglite') notes.push('PGLite: the watch process holds the single-writer lock, so nothing else can read the brain; probes cannot run (derive from interval + manual sync cost).');
+ } else if (TRIGGER === 'autopilot') {
+ resident = spawnChild([process.execPath, CLI, 'autopilot', '--repo', ROOT, '--interval', String(INTERVAL), '--json'], { cwd: HOME, env: childEnv(), logFile });
+ await waitLine(resident, /cycle|Autopilot starting/, 120_000);
+ } else if (TRIGGER === 'jobs') {
+ if (ENGINE !== 'postgres') { notes.push('PGLite has no resident jobs worker ("gbrain jobs work" drains the queue and exits).'); }
+ else {
+ resident = spawnChild([process.execPath, CLI, 'jobs', 'work'], { cwd: HOME, env: childEnv(), logFile });
+ await sleep(5000);
+ }
+ }
+
+ if (TRIGGER === 'jobs' && prober && resident) {
+ const c = makeChange(ROOT, DIR, 'single');
+ const v = await pollVisible(prober, c, now() + 30_000, 500);
+ notes.push('jobs worker alone (no submit): ' + (v.t_search === null ? 'nothing synced a single-file commit within 30 s; the worker has no autonomous sync trigger' : 'the commit became visible after ' + round1(v.t_search - c.t_commit) + ' ms without a submit'));
+ await gb(['jobs', 'submit', 'sync', '--params', JSON.stringify({ sourceId: 'bench', noPull: true })], { allowFail: true });
+ await pollVisible(prober, c, now() + 120_000, 200);
+ }
+
+ if (COMMIT === 'noop') {
+ await gb(['sync', '--source', 'bench', '--no-pull', '--json'], { allowFail: true });
+ notes.push('noop: HEAD equals the last synced commit (one unrecorded sync converged it first); each rep is one manual sync with nothing to do, timed spawn to exit');
+ for (let rep = 0; rep < REPS; rep++) {
+ await sleep(300);
+ const trace = TRACE && ENGINE === 'postgres' ? join(outDir, 'logs', cellId + '-rep' + rep + '-sql.jsonl') : undefined;
+ if (trace) rmSync(trace, { force: true });
+ const child = spawnChild([process.execPath, CLI, 'sync', '--source', 'bench', '--no-pull', '--json'], { cwd: HOME, env: childEnv(trace ? { GBRAIN_SQL_TRACE: trace, GBRAIN_SQL_TRACE_LABEL: 'noop-sync' } : {}), logFile });
+ const code = await child.exited;
+ const r: Rep = { rep, sha: git(ROOT, 'rev-parse', '--short=12', 'HEAD'), token: '', files: 0, get_ms: null, search_ms: null, first_hit_ms: null, commit_git_ms: 0, source: 'exit', phases: {} };
+ r.exit_ms = round1(child.exitAt! - child.startedAt);
+ for (const [k, v] of Object.entries(phaseMarks(child.lines, child.startedAt, now()))) r.phases[k] = v;
+ const out = child.stdoutRaw();
+ try { const j = JSON.parse(out.slice(out.indexOf('{'))); r.phases.sync_status = j.sync_status; } catch { /* not json */ }
+ if (code !== 0) r.error = 'sync exited ' + code;
+ if (trace && existsSync(trace)) {
+ const recs = readTrace(trace);
+ const byClass: Record<string, { n: number; ms: number }> = {};
+ for (const x of recs) { const c = byClass[classify(x.sql)] ??= { n: 0, ms: 0 }; c.n++; c.ms += x.ms; }
+ r.phases.db_round_trips = recs.length;
+ r.phases.db_ms = round1(recs.reduce((a, x) => a + x.ms, 0));
+ if (recs.length) r.phases.db_span_ms = round1(Math.max(...recs.map(x => x.t + x.ms)) - Math.min(...recs.map(x => x.t)));
+ (r as Rep & { sql?: unknown }).sql = { by_class: Object.fromEntries(Object.entries(byClass).map(([k, v]) => [k, { n: v.n, ms: round1(v.ms) }])), top: groupStatements(recs, 1, 100000).sort((a, b) => b.total_ms - a.total_ms).slice(0, 12).map(g => ({ sql: g.sql, count: g.count, total_ms: g.total_ms })) };
+ }
+ reps.push(r);
+ log('noop rep ' + (rep + 1) + '/' + REPS + ': exit ' + r.exit_ms + ' ms ' + JSON.stringify(r.phases) + (r.error ? ' ERROR ' + r.error : ''));
+ }
+ }
+
+ try {
+ for (let rep = 0; rep < (COMMIT === 'noop' ? 0 : REPS); rep++) {
+ if (TRIGGER === 'watch' || TRIGGER === 'autopilot') await sleep(Math.random() * INTERVAL * 1000);
+ else await sleep(TRIGGER === 'serve' ? 3500 : 300);
+ let pre: Change | null = null;
+ if (SCENARIO === 'during-sync') {
+ pre = makeChange(ROOT, DIR, 'batch');
+ if (TRIGGER === 'serve' && resident) await waitLine(resident, /started managed sync job/, 60_000, pre.t_commit);
+ await sleep(Number(flag('during-delay-ms', '500')));
+ }
+ const c = makeChange(ROOT, DIR, COMMIT as CommitKind);
+ const deadline = c.t_commit + TIMEOUT_S * 1000;
+ const r: Rep = { rep, sha: c.sha.slice(0, 12), token: c.token, files: c.edited.length + c.added.length + c.deleted.length, get_ms: null, search_ms: null, first_hit_ms: null,
+ commit_git_ms: round1(c.t_commit - c.t_commit_start), source: '', phases: {} };
+ let child: Child | null = resident;
+ if (TRIGGER === 'manual') {
+ child = spawnChild([process.execPath, CLI, 'sync', '--source', 'bench', '--no-pull', '--json'], { cwd: HOME, env: childEnv(), logFile });
+ } else if (TRIGGER === 'jobs' && ENGINE === 'postgres') {
+ const t = now();
+ const sub = await gb(['jobs', 'submit', 'sync', '--params', JSON.stringify({ sourceId: 'bench', noPull: true })], { allowFail: true });
+ r.phases.submit_cli_ms = round1(now() - t);
+ r.phases.submit_after_commit_ms = round1(t - c.t_commit);
+ if (sub.code !== 0) r.error = 'jobs submit failed: ' + (sub.stdout + sub.stderr).slice(-400);
+ }
+ if (prober) {
+ const v = await pollVisible(prober, c, deadline, PROBE_MS);
+ r.source = prober.kind;
+ r.get_ms = v.t_get === null ? null : round1(v.t_get - c.t_commit);
+ r.search_ms = v.t_search === null ? null : round1(v.t_search - c.t_commit);
+ r.first_hit_ms = v.t_first_hit === null ? null : round1(v.t_first_hit - c.t_commit);
+ r.probes = v.probes; r.probe_ms_mean = v.probe_ms_mean;
+ if (EMBED === 'stub') r.phases.embedded_ms = v.t_embedded == null ? -1 : round1(v.t_embedded - c.t_commit);
+ if (TRIGGER === 'manual' && child) { await child.exited; r.exit_ms = round1(child.exitAt! - c.t_commit); }
+ } else if (TRIGGER === 'manual' && child) {
+ await child.exited;
+ r.exit_ms = round1(child.exitAt! - c.t_commit);
+ const done = child.lines.find(l => PHASE_RES[3]![1].test(l.text));
+ r.source = 'done-line';
+ r.get_ms = r.search_ms = done ? round1(done.t - c.t_commit) : null;
+ const total = expectedHits(c) + c.deleted.length;
+ const processed = child.lines.find(l => new RegExp('\\b' + total + '/' + total + ' processed').test(l.text));
+ if (processed) r.phases.all_processed_line_ms = round1(processed.t - c.t_commit);
+ r.verified = await pgliteVerify(c);
+ if (!(r.verified as { getOk: boolean }).getOk) r.error = 'PGLite verification after exit did not see the change';
+ } else {
+ r.source = 'none';
+ r.note = 'no reader available while the trigger process holds the PGLite lock';
+ if (resident) await waitLine(resident, PHASE_RES[3]![1], TIMEOUT_S * 1000, c.t_commit).then(l => { if (l) r.phases.done_line_ms = round1(l.t - c.t_commit); });
+ }
+ if (child && child === resident && r.search_ms !== null) await waitLine(child, PHASE_RES[3]![1], 5000, c.t_commit);
+ if (child) {
+ const base = TRIGGER === 'manual' ? child.startedAt : c.t_commit;
+ const marks = phaseMarks(child.lines, base, now());
+ for (const [k, v] of Object.entries(marks)) r.phases[k] = v;
+ if (TRIGGER === 'manual') r.phases.spawn_after_commit_ms = round1(child.startedAt - c.t_commit);
+ if (TRIGGER === 'manual') { const out = child.stdoutRaw(); try { const j = JSON.parse(out.slice(out.indexOf('{'))); r.phases.sync_status = j.sync_status; r.phases.written = j.drain?.written ?? j.added + j.modified; } catch { /* not json */ } }
+ }
+ if (pre) r.note = 'committed while the sync of a 60-file batch (' + pre.sha.slice(0, 8) + ') was running';
+ reps.push(r);
+ log('rep ' + (rep + 1) + '/' + REPS + ': get ' + r.get_ms + ' ms, search ' + r.search_ms + ' ms' + (r.exit_ms ? ', exit ' + r.exit_ms + ' ms' : '') + ' ' + JSON.stringify(r.phases) + (r.error ? ' ERROR ' + r.error : ''));
+ if (r.search_ms === null && r.get_ms === null && r.source !== 'none' && TRIGGER !== 'serve') { notes.push('rep ' + rep + ' never became visible within ' + TIMEOUT_S + ' s'); }
+ if (r.search_ms === null && MODE === 'classic' && TRIGGER === 'serve') { notes.push('classic brain: serve never synced the commit within ' + TIMEOUT_S + ' s (the on-commit watcher lists managed sources only)'); break; }
+ }
+ } finally {
+ if (resident) { resident.kill('SIGTERM'); await Promise.race([resident.exited, sleep(40_000)]); resident.kill('SIGKILL'); }
+ if (TRIGGER === 'serve') await gb(['config', 'set', 'sync.on_commit', 'false'], { allowFail: true });
+ await prober?.close();
+ if (harness) { await harness.setRtt(0); harness.stop(); }
+ if (stub) { notes.push('embedding stub: ' + JSON.stringify(stub.stats) + ', ' + EMBED_LATENCY_MS + ' ms per call'); stub.close(); }
+ }
+
+ const ok = (k: 'get_ms' | 'search_ms') => reps.map(r => r[k]).filter((x): x is number => typeof x === 'number');
+ const phaseKeys = [...new Set(reps.flatMap(r => Object.keys(r.phases)))].filter(k => reps.some(r => typeof r.phases[k] === 'number'));
+ const phaseStats: Record<string, Stats> = {};
+ for (const k of phaseKeys) phaseStats[k] = stats(reps.map(r => r.phases[k]).filter((x): x is number => typeof x === 'number'));
+ const result = {
+ schema: 'gbrain-sync-freshness/1', cell: cellId, label: LABEL, gbrain_rev: REV, gbrain_root: GROOT, at: new Date().toISOString(),
+ params: { engine: ENGINE, mode: MODE, pages: PAGES, commit: COMMIT, trigger: TRIGGER, reps: REPS, rtt_ms: RTT, measured_rtt_ms: measuredRtt, interval_s: ['watch', 'autopilot'].includes(TRIGGER) ? INTERVAL : null,
+ scenario: SCENARIO, probe_ms: PROBE_MS, timeout_s: TIMEOUT_S, keyless: EMBED === 'off', embed: EMBED === 'stub' ? { provider: 'loopback OpenAI stub', latency_ms: EMBED_LATENCY_MS } : null },
+ machine: machineInfo(), build, notes: [...new Set(notes)],
+ get_page: stats(ok('get_ms')), search: stats(ok('search_ms')), exit: stats(reps.map(r => r.exit_ms).filter((x): x is number => typeof x === 'number')),
+ phases: phaseStats, failures: reps.filter(r => r.error || (r.source !== 'none' && r.source !== 'exit' && r.search_ms === null)).length, reps, log: logFile,
+ };
+ const file = join(outDir, cellId + '.json');
+ writeFileSync(file, JSON.stringify(result, null, 2) + '\n');
+ if (JSON_OUT) console.log(JSON.stringify(result));
+ log('wrote ' + file + ': search p50 ' + result.search.p50 + ' p95 ' + result.search.p95 + ' max ' + result.search.max + ' cv ' + result.search.cv + ' (n=' + result.search.n + ')');
+}
+
+function report(dirs: string): void {
+ const rows = dirs.split(',').flatMap(dir => readdirSync(dir).filter(f => f.endsWith('.json')).map(f => JSON.parse(readFileSync(join(dir, f), 'utf8')))).filter(r => r.schema === 'gbrain-sync-freshness/1')
+ .sort((a, b) => a.cell.localeCompare(b.cell));
+ const s = (x: Stats | undefined) => x && x.n ? (x.p50 !== null ? (x.p50 / 1000).toFixed(2) : '-') + ' / ' + (x.p95 !== null ? (x.p95 / 1000).toFixed(2) : '-') + ' / ' + (x.max !== null ? (x.max / 1000).toFixed(2) : '-') : '-';
+ const ph = (r: any) => Object.entries(r.phases as Record<string, Stats>).filter(([k, v]) => v.n && v.p50 !== null && !['written', 'spawn_after_commit_ms', 'submit_cli_ms'].includes(k)).map(([k, v]) => k + ' ' + (v.p50! / 1000).toFixed(2)).join(', ');
+ console.log('| cell | engine | mode | pages | commit | trigger | rtt | n | get_page p50/p95/max s | search p50/p95/max s | search cv | exit p50/p95/max s | phases (p50 s) | gbrain |');
+ console.log('|---|---|---|---|---|---|---|---|---|---|---|---|---|---|');
+ for (const r of rows) {
+ const p = r.params;
+ console.log('| ' + [r.cell, p.engine, p.mode, p.pages, p.commit + (p.scenario !== 'plain' ? ' (' + p.scenario + ')' : ''), p.trigger + (p.interval_s ? ' ' + p.interval_s + 's' : ''), p.rtt_ms ? p.rtt_ms + ' ms' : '0',
+ r.search.n + '/' + p.reps, s(r.get_page), s(r.search), r.search.cv ?? '-', s(r.exit), ph(r), r.gbrain_rev].join(' | ') + ' |');
+ }
+}
+
+await main();
+process.exit(0);