Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

test(graduation): cached 1k/10k fixture wrapper over buildHistoryFixture

garrytanfbd95037cf75
diff --git a/scripts/persistence/graduation-fixture.ts b/scripts/persistence/graduation-fixture.ts
new file mode 100644
index 0000000000..58e41a7e09
--- /dev/null
+++ b/scripts/persistence/graduation-fixture.ts
@@ -0,0 +1,305 @@
+/**
+ * Graduation fixtures: on-disk PGLite brains with real persistence history,
+ * ready for `gbrain migrate --to postgres`.
+ *
+ * Kinds:
+ * - `history`: a thin wrapper over `buildHistoryFixture` (`history-fixture.ts`,
+ * 1..10,000 pages). After the build every content chunk gets a
+ * deterministic vector, so the round trip carries embeddings while the
+ * brain stays keyless.
+ * - `legacy`: the independent hand-built fixture in
+ * `test/fixtures/graduation/legacy-brain.ts` with its expected.json.
+ *
+ * A fixture directory holds `brain.pglite` (the data dir), `home` (its
+ * GBRAIN_HOME with config.json, host identity, writer credentials and managed
+ * root registry), `root` (the Git checkouts) and `fixture.json` (build
+ * outputs, the report). Builds run in a child process with provider keys
+ * removed. Each build is cached as a tarball keyed by the fixture source files'
+ * sha256, the schema version and the build options (seed included); a cache
+ * hit extracts the tarball and re-homes it: absolute paths in the database,
+ * the managed-root registry and the owner stamps are rewritten for the new
+ * directory, and the stamps take the new directories' device, inode and birth
+ * time. The 10k history build takes about 18 minutes, so CI builds it once per
+ * run and every 10k test restores it.
+ *
+ * bun scripts/persistence/graduation-fixture.ts --kind history --pages 1000 --seed 1 --out <dir> [--json]
+ * bun scripts/persistence/graduation-fixture.ts --kind legacy --out <dir>
+ *
+ * Options: --sources N (default 4), --worktrees N (default 2), --cache-dir DIR
+ * (default $GBRAIN_GRADUATION_FIXTURE_CACHE or ~/.cache/gbrain-graduation-fixtures),
+ * --no-cache. Synthetic data only; bearer tokens in fixture.json are valid
+ * only inside the scratch brain.
+ */
+import assert from 'node:assert/strict';
+import { createHash } from 'node:crypto';
+import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, statSync, writeFileSync } from 'node:fs';
+import { homedir } from 'node:os';
+import { basename, dirname, join, resolve } from 'node:path';
+
+export type GraduationFixtureKind = 'history' | 'legacy';
+export interface GraduationFixtureOptions {
+ kind: GraduationFixtureKind;
+ /** history only (1..10,000). */
+ pages?: number;
+ seed?: number;
+ sources?: number;
+ worktrees?: number;
+ /** Destination directory; must not exist or be empty. */
+ dir: string;
+ cacheDir?: string | null;
+ /** Child build timeout; the 10k history build needs about 20 minutes. */
+ timeoutMs?: number;
+}
+export interface GraduationFixtureReport {
+ kind: GraduationFixtureKind;
+ pages: number;
+ /** On-disk bytes of the PGLite data dir, and the sum of pg_total_relation_size over public tables. */
+ bytes: { data_dir: number; relations: number };
+ chunks: number;
+ versions: number;
+ requests: number;
+ effects: number;
+ embeddings: { chunks: number; facts: number; takes: number };
+ facts: number;
+ withdrawals: number;
+ build_ms: number;
+}
+export interface GraduationFixture {
+ kind: GraduationFixtureKind;
+ key: string;
+ dir: string;
+ dataDir: string;
+ home: string;
+ root: string;
+ cached: boolean;
+ report: GraduationFixtureReport;
+ /** Build outputs: history fixture ids and synthetic tokens, or the legacy fixture's ids. */
+ outputs: Record<string, unknown>;
+}
+
+const REPO = resolve(import.meta.dir, '..', '..');
+const KEY_FILES = ['scripts/persistence/graduation-fixture.ts', 'scripts/persistence/history-fixture.ts', 'scripts/persistence/ops.ts',
+ 'test/fixtures/graduation/legacy-brain.ts', 'test/fixtures/graduation/expected.json'];
+const PROVIDER_KEYS = /^(OPENAI|ANTHROPIC|VOYAGE|GEMINI|GOOGLE|TYPESAFE|JEV_TYPESAFE|OPENROUTER|GROQ|MISTRAL|COHERE|DEEPSEEK|XAI)_/;
+
+function normalized(opts: GraduationFixtureOptions) {
+ return opts.kind === 'legacy' ? { kind: 'legacy' as const }
+ : { kind: 'history' as const, pages: opts.pages ?? 1000, seed: opts.seed ?? 1, sources: opts.sources ?? 4, worktrees: opts.worktrees ?? 2 };
+}
+
+/** sha256 over the fixture source files, the schema version and the build options. */
+export async function graduationFixtureKey(opts: GraduationFixtureOptions): Promise<string> {
+ const { LATEST_VERSION } = await import('../../src/core/migrate.ts');
+ const hash = createHash('sha256');
+ for (const file of KEY_FILES) hash.update(file).update(readFileSync(join(REPO, file)));
+ hash.update(JSON.stringify({ schema: LATEST_VERSION, ...normalized(opts) }));
+ return hash.digest('hex').slice(0, 32);
+}
+
+function defaultCacheDir(): string {
+ return process.env.GBRAIN_GRADUATION_FIXTURE_CACHE || join(homedir(), '.cache', 'gbrain-graduation-fixtures');
+}
+
+function run(argv: string[], opts: { cwd?: string; env?: Record<string, string | undefined>; timeoutMs?: number } = {}): string {
+ const result = Bun.spawnSync(argv, { cwd: opts.cwd, env: opts.env as Record<string, string>, stdout: 'pipe', stderr: 'pipe', timeout: opts.timeoutMs });
+ if (result.exitCode !== 0) throw new Error(`${argv.slice(0, 3).join(' ')} exited ${result.exitCode}${result.signalCode ? ` (${result.signalCode})` : ''}\n${result.stderr.toString().slice(-4000)}`);
+ return result.stdout.toString();
+}
+
+/** Prepare a fixture in `opts.dir`, from the cache when its key matches, otherwise by building it in a child process. */
+export async function prepareGraduationFixture(opts: GraduationFixtureOptions): Promise<GraduationFixture> {
+ const dir = resolve(opts.dir);
+ if (existsSync(dir)) assert.equal(readdirSync(dir).length, 0, `graduation fixture: ${dir} must be empty`);
+ mkdirSync(dir, { recursive: true });
+ const key = await graduationFixtureKey(opts);
+ const cacheDir = opts.cacheDir === null ? null : resolve(opts.cacheDir ?? defaultCacheDir());
+ const tarball = cacheDir ? join(cacheDir, `${opts.kind}-${key}.tar.gz`) : null;
+ if (tarball && existsSync(tarball)) {
+ run(['tar', '-xpzf', tarball, '-C', dir]);
+ await rehomeFixture(dir);
+ return { ...readMeta(dir), cached: true };
+ }
+ const env: Record<string, string | undefined> = Object.fromEntries(Object.entries(process.env).filter(([name]) => !PROVIDER_KEYS.test(name)));
+ delete env.DATABASE_URL; delete env.GBRAIN_DATABASE_URL;
+ env.GBRAIN_HOME = join(dir, 'home');
+ env.GBRAIN_NO_SNAPSHOT = '1';
+ run([process.execPath, '--no-env-file', import.meta.path, '--build-child', JSON.stringify({ ...normalized(opts), key, dir })],
+ { cwd: REPO, env, timeoutMs: opts.timeoutMs ?? 30 * 60_000 });
+ if (tarball) {
+ mkdirSync(cacheDir!, { recursive: true });
+ const staged = `${tarball}.${process.pid}.tmp`;
+ run(['tar', '-cpzf', staged, '-C', dir, '.']);
+ renameSync(staged, tarball);
+ }
+ return { ...readMeta(dir), cached: false };
+}
+
+function readMeta(dir: string): Omit<GraduationFixture, 'cached'> {
+ const meta = JSON.parse(readFileSync(join(dir, 'fixture.json'), 'utf8'));
+ return { kind: meta.kind, key: meta.key, dir, dataDir: join(dir, 'brain.pglite'), home: join(dir, 'home'), root: join(dir, 'root'),
+ report: meta.report, outputs: meta.outputs };
+}
+
+const sha = (text: string) => createHash('sha256').update(text).digest('hex');
+
+function walk(dir: string, visit: (path: string) => void): void {
+ for (const entry of readdirSync(dir, { withFileTypes: true })) {
+ const path = join(dir, entry.name);
+ if (entry.isDirectory()) { if (entry.name !== '.git' && entry.name !== 'brain.pglite') walk(path, visit); }
+ else if (entry.isFile()) visit(path);
+ }
+}
+
+/**
+ * Rewrite a restored fixture for its new directory: database paths, small
+ * JSON identity files (and their path-hash file names), then re-stamp every
+ * owner stamp and reservation with the new directories' device, inode and
+ * birth time. Only the synthetic fixture's own private files are touched.
+ */
+export async function rehomeFixture(dir: string): Promise<void> {
+ const metaPath = join(dir, 'fixture.json');
+ const meta = JSON.parse(readFileSync(metaPath, 'utf8'));
+ const from: string = meta.builtAt;
+ if (from === dir) return;
+ const swap = (text: string) => text.split(from).join(dir);
+ const files: string[] = [];
+ walk(dir, path => { if (path.endsWith('.json') && statSync(path).size <= 65_536) files.push(path); });
+ files.push(join(dir, 'brain.pglite', '.gbrain-managed'));
+ const oldPaths = new Set<string>();
+ for (const file of files) {
+ if (!existsSync(file)) continue;
+ const text = readFileSync(file, 'utf8');
+ for (const match of text.matchAll(/"(?:root|local_path)":"([^"]+)"/g)) oldPaths.add(match[1]);
+ if (text.includes(from)) writeFileSync(file, swap(text), { mode: statSync(file).mode & 0o777 });
+ }
+ const renamed = new Map([...oldPaths].map(path => [sha(path), sha(swap(path))]));
+ for (const file of files) {
+ if (!existsSync(file)) continue;
+ const name = basename(file).replace(/[a-f0-9]{64}/, hash => renamed.get(hash) ?? hash);
+ if (name !== basename(file)) renameSync(file, join(dirname(file), name));
+ }
+ walk(dir, path => {
+ const name = basename(path);
+ if (name !== '.gbrain-owner.json' && !/^\.gbrain-owner-[a-f0-9]{64}\.json$/.test(name)) return;
+ const value = JSON.parse(readFileSync(path, 'utf8'));
+ const info = statSync(value.root, { bigint: true });
+ if (name === '.gbrain-owner.json') Object.assign(value, { device: info.dev.toString(), inode: info.ino.toString(), birth: info.birthtimeNs.toString() });
+ else if (value.initialInode !== null) Object.assign(value, { initialDevice: info.dev.toString(), initialInode: info.ino.toString(), initialBirth: info.birthtimeNs.toString() });
+ writeFileSync(path, JSON.stringify(value), { mode: 0o600 });
+ });
+ const { PGLiteEngine } = await import('../../src/core/pglite-engine.ts');
+ const engine = new PGLiteEngine();
+ await engine.connect({ database_path: join(dir, 'brain.pglite') });
+ try {
+ await engine.transaction(async tx => {
+ await tx.executeRaw("SELECT set_config('gbrain.topology_change','on',true)");
+ await tx.executeRaw('UPDATE sources SET local_path=replace(local_path,$1,$2) WHERE local_path LIKE $3', [from, dir, `${from}%`]);
+ await tx.executeRaw(`UPDATE persistence_host_bindings SET local_path=replace(local_path,$1,$2),coordination_path=replace(coordination_path,$1,$2)
+ WHERE local_path LIKE $3 OR coordination_path LIKE $3`, [from, dir, `${from}%`]);
+ });
+ } finally { await engine.disconnect(); }
+ meta.builtAt = dir;
+ writeFileSync(metaPath, JSON.stringify(meta, null, 2));
+}
+
+function dirBytes(path: string): number {
+ let total = 0;
+ for (const entry of readdirSync(path, { withFileTypes: true })) {
+ const child = join(path, entry.name);
+ total += entry.isDirectory() ? dirBytes(child) : statSync(child).size;
+ }
+ return total;
+}
+
+/** Bytes, chunks, versions, requests and embeddings of a closed fixture brain (opens it read-only in practice). */
+async function reportFor(engine: import('../../src/core/engine.ts').BrainEngine, kind: GraduationFixtureKind, dataDir: string, buildMs: number): Promise<GraduationFixtureReport> {
+ const [row] = await engine.executeRaw<Record<string, number>>(`SELECT
+ (SELECT count(*) FROM pages)::int AS pages, (SELECT count(*) FROM content_chunks)::int AS chunks,
+ (SELECT count(*) FROM page_versions)::int AS versions, (SELECT count(*) FROM persistence_requests)::int AS requests,
+ (SELECT count(*) FROM persistence_effects)::int AS effects, (SELECT count(*) FROM facts)::int AS facts,
+ (SELECT count(*) FROM fact_withdrawals)::int AS withdrawals,
+ (SELECT count(*) FROM content_chunks WHERE embedding IS NOT NULL)::int AS chunk_embeddings,
+ (SELECT count(*) FROM facts WHERE embedding IS NOT NULL)::int AS fact_embeddings,
+ (SELECT count(*) FROM takes WHERE embedding IS NOT NULL)::int AS take_embeddings,
+ (SELECT sum(pg_total_relation_size(c.oid)) FROM pg_class c JOIN pg_namespace n ON n.oid=c.relnamespace
+ WHERE n.nspname='public' AND c.relkind='r')::bigint AS relations`);
+ return { kind, pages: Number(row.pages), bytes: { data_dir: dirBytes(dataDir), relations: Number(row.relations) },
+ chunks: Number(row.chunks), versions: Number(row.versions), requests: Number(row.requests), effects: Number(row.effects),
+ embeddings: { chunks: Number(row.chunk_embeddings), facts: Number(row.fact_embeddings), takes: Number(row.take_embeddings) },
+ facts: Number(row.facts), withdrawals: Number(row.withdrawals), build_ms: buildMs };
+}
+
+/** Child-process build: GBRAIN_HOME is already `<dir>/home`. */
+async function buildChild(spec: { kind: GraduationFixtureKind; key: string; dir: string; pages?: number; seed?: number; sources?: number; worktrees?: number }): Promise<void> {
+ assert.equal(process.env.GBRAIN_HOME, join(spec.dir, 'home'), 'graduation fixture: the child must run under the fixture home');
+ const { PGLiteEngine } = await import('../../src/core/pglite-engine.ts');
+ const { saveConfig } = await import('../../src/core/config.ts');
+ const { disposePersistenceConsumer } = await import('../../src/core/persistence/service.ts');
+ const dataDir = join(spec.dir, 'brain.pglite');
+ const root = join(spec.dir, 'root');
+ mkdirSync(root, { recursive: true });
+ saveConfig({ engine: 'pglite', database_path: dataDir, embedding_disabled: true } as Parameters<typeof saveConfig>[0]);
+ const engine = new PGLiteEngine();
+ await engine.connect({ database_path: dataDir });
+ const started = performance.now();
+ let outputs: Record<string, unknown>;
+ try {
+ await engine.initSchema();
+ if (spec.kind === 'legacy') {
+ const { buildLegacyBrain } = await import('../../test/fixtures/graduation/legacy-brain.ts');
+ const built = await buildLegacyBrain(engine, { root });
+ outputs = { ...built, queued: undefined, queuedAdmissionPath: join(root, 'queued-admission.json') };
+ } else {
+ const { buildHistoryFixture } = await import('./history-fixture.ts');
+ const built = await buildHistoryFixture(engine, { pages: spec.pages!, seed: spec.seed!, sources: spec.sources!, worktrees: spec.worktrees!, root });
+ const [dims] = await engine.executeRaw<{ t: string }>(`SELECT format_type(atttypid, atttypmod) AS t FROM pg_attribute
+ WHERE attrelid='content_chunks'::regclass AND attname='embedding'`);
+ const width = Number(/\((\d+)\)/.exec(dims.t)![1]);
+ // Deterministic per-chunk vectors (no provider): sin waves seeded by chunk id, at full float precision.
+ await engine.executeRaw(`UPDATE content_chunks c SET embedding=v.vec::vector, embedded_at='2026-01-01T00:00:00Z', model='fixture:graduation'
+ FROM (SELECT id, ('[' || string_agg((sin((g + 1) * (id + 0.37)) / 3)::real::text, ',' ORDER BY g) || ']') AS vec
+ FROM content_chunks, generate_series(0, $1 - 1) g GROUP BY id) v WHERE v.id=c.id`, [width]);
+ outputs = { seed: built.seed, sources: built.sources, remotes: built.remotes, accessTokenId: built.accessTokenId,
+ oauthClientId: built.oauthClientId, queuedRequestId: built.queuedRequestId, delayedEffectId: built.delayedEffectId,
+ counts: built.counts, digest: built.digest, embeddingWidth: width };
+ }
+ } finally {
+ await disposePersistenceConsumer(engine);
+ }
+ const buildMs = Math.round(performance.now() - started);
+ const report = await reportFor(engine, spec.kind, dataDir, buildMs);
+ await engine.disconnect();
+ report.bytes.data_dir = dirBytes(dataDir);
+ writeFileSync(join(spec.dir, 'fixture.json'), JSON.stringify({ kind: spec.kind, key: spec.key, builtAt: spec.dir, options: spec, report, outputs }, null, 2));
+}
+
+function parseArgs(argv: string[]): { opts: GraduationFixtureOptions; json: boolean } {
+ const value = (flag: string) => { const i = argv.indexOf(flag); return i >= 0 ? argv[i + 1] : undefined; };
+ const kind = (value('--kind') ?? 'history') as GraduationFixtureKind;
+ if (kind !== 'history' && kind !== 'legacy') throw new Error(`--kind must be history or legacy, got ${kind}`);
+ const out = value('--out');
+ if (!out) throw new Error('--out <dir> is required');
+ const num = (flag: string) => value(flag) === undefined ? undefined : Number(value(flag));
+ return { json: argv.includes('--json'), opts: { kind, dir: out, pages: num('--pages'), seed: num('--seed'), sources: num('--sources'),
+ worktrees: num('--worktrees'), cacheDir: argv.includes('--no-cache') ? null : value('--cache-dir') } };
+}
+
+if (import.meta.main) {
+ const argv = process.argv.slice(2);
+ if (argv[0] === '--build-child') {
+ await buildChild(JSON.parse(argv[1]));
+ } else {
+ let parsed: ReturnType<typeof parseArgs>;
+ try { parsed = parseArgs(argv); } catch (error) { console.error(String((error as Error).message)); process.exit(2); }
+ const fixture = await prepareGraduationFixture(parsed.opts);
+ if (parsed.json) console.log(JSON.stringify({ ...fixture, outputs: undefined }, null, 2));
+ else {
+ const r = fixture.report;
+ console.log(`${fixture.kind} fixture at ${fixture.dir} (${fixture.cached ? 'restored from cache' : `built in ${r.build_ms} ms`}, key ${fixture.key})`);
+ console.log(` pages ${r.pages} chunks ${r.chunks} versions ${r.versions} requests ${r.requests} effects ${r.effects} facts ${r.facts} withdrawals ${r.withdrawals}`);
+ console.log(` embeddings: chunks ${r.embeddings.chunks} facts ${r.embeddings.facts} takes ${r.embeddings.takes}`);
+ console.log(` bytes: data dir ${r.bytes.data_dir} relations ${r.bytes.relations}`);
+ }
+ }
+}
+
diff --git a/test/fixtures/graduation/expected.json b/test/fixtures/graduation/expected.json
index c55ad75f6c..5feedf3c2a 100644
--- a/test/fixtures/graduation/expected.json
+++ b/test/fixtures/graduation/expected.json
@@ -83,6 +83,7 @@
"local_writers": { "live": 2, "revoked": 1 },
"bytes": { "raw_data_min": 2097152, "files_metadata_min": 262144, "minion_data_min": 524288, "frontmatter_min": 65536 },
"cycle_locks": 1,
+ "embeddings": { "chunks": 1, "facts": 1 },
"brain": { "enabled": true }
},
"target": {
diff --git a/test/fixtures/graduation/legacy-brain.ts b/test/fixtures/graduation/legacy-brain.ts
index 82d875cb99..f0199d3e6b 100644
--- a/test/fixtures/graduation/legacy-brain.ts
+++ b/test/fixtures/graduation/legacy-brain.ts
@@ -104,6 +104,7 @@ export interface LegacyExpected {
local_writers: { live: number; revoked: number };
bytes: { raw_data_min: number; files_metadata_min: number; minion_data_min: number; frontmatter_min: number };
cycle_locks: number;
+ embeddings: { chunks: number; facts: number };
brain: { enabled: boolean };
};
target: {
@@ -217,9 +218,6 @@ export async function buildLegacyBrain(engine: BrainEngine, { root }: { root: st
await sql(`INSERT INTO tags(page_id,tag) VALUES($1,'company'),($1,'Legacy'),($1,'legacy')`, [acme]);
await sql(`INSERT INTO timeline_entries(page_id,date,source,summary,detail) VALUES($1,'2025-10-01','legacy','Founded','Acme-example was founded.'),
($1,'2026-01-10','legacy','Seed round','Raised from fund-a.')`, [acme]);
- await sql(`INSERT INTO content_chunks(page_id,chunk_index,chunk_text,chunk_source,embedding,model,token_count,embedded_at)
- VALUES($1,0,'Acme-example builds widgets.','compiled_truth',$2::vector,'fixture:legacy',6,'2025-11-02T00:00:00Z')`,
- [acme, fixtureVector(chunkDims, 1)]);
// Takes: row 1 superseded by row 2 (supersession is by row number, no FK).
await sql(`INSERT INTO takes(page_id,row_num,claim,kind,holder,weight,superseded_by,active,created_at,updated_at)
@@ -313,6 +311,10 @@ export async function buildLegacyBrain(engine: BrainEngine, { root }: { root: st
await sql(`UPDATE persistence_effects SET state='running',attempts=1,error_code=NULL,execution_token=$2::uuid,claim_expires_at='2026-03-10T00:05:00Z',
outcome=NULL,next_attempt_at='2026-03-10T00:00:00Z' WHERE id=$1`, [effectIds.orphanRunning, LEGACY_IDS.orphanExecutionToken]);
+ // The consumer re-chunked every page; the Acme chunk gets the legacy vector (vectors must round-trip as text).
+ const embedded = await engine.executeRaw(`UPDATE content_chunks SET embedding=$2::vector,model='fixture:legacy',embedded_at='2025-11-02T00:00:00Z'
+ WHERE page_id=$1 AND chunk_index=0 RETURNING id`, [acme, fixtureVector(chunkDims, 1)]);
+ assert.equal(embedded.length, 1, 'legacy fixture: the Acme page has no chunk to embed');
// Git effects settle on the consumer's timing; pin them to the state a crash right after publication leaves.
await sql(`UPDATE persistence_effects SET state='queued',attempts=0,error_code=NULL,execution_token=NULL,claim_expires_at=NULL,
next_attempt_at='2026-03-10T00:00:00Z' WHERE kind='git'`);
@@ -458,6 +460,9 @@ export async function legacySourceMismatches(engine: BrainEngine, expected = loa
if (!(Number(bytes[key]) >= s.bytes[`${key}_min`])) m.push(`bytes ${key}: ${bytes[key]} < ${s.bytes[`${key}_min`]}`);
}
compare(m, 'cycle locks', await count(engine, 'gbrain_cycle_locks'), s.cycle_locks);
+ const [vectors] = await engine.executeRaw<{ chunks: number; facts: number }>(`SELECT
+ (SELECT count(*) FROM content_chunks WHERE embedding IS NOT NULL)::int AS chunks, (SELECT count(*) FROM facts WHERE embedding IS NOT NULL)::int AS facts`);
+ compare(m, 'embeddings', { chunks: Number(vectors.chunks), facts: Number(vectors.facts) }, s.embeddings);
const [brain] = await engine.executeRaw<{ enabled: boolean }>('SELECT enabled FROM persistence_brain WHERE singleton=1');
compare(m, 'brain', brain, s.brain);
return m;
diff --git a/test/scripts/graduation-fixture.test.ts b/test/scripts/graduation-fixture.test.ts
new file mode 100644
index 0000000000..789e756bc7
--- /dev/null
+++ b/test/scripts/graduation-fixture.test.ts
@@ -0,0 +1,63 @@
+/**
+ * The graduation fixture cache restores a working managed brain at a new path.
+ *
+ * Protects: `scripts/persistence/graduation-fixture.ts`, which the 1k/10k
+ * graduation round trips and the crash suite use to avoid rebuilding history
+ * (the 10k build takes about 18 minutes). A restored tarball has new inodes
+ * and a new directory, so every owner stamp, reservation, managed-root record
+ * and database path must be re-homed or the first managed write refuses with
+ * a physical-root error.
+ * Regression it catches: a new path-bearing identity file or column that the
+ * re-home misses, a cache key that ignores the fixture sources, or a report
+ * that stops counting chunks, versions, requests or embeddings.
+ * Not covered elsewhere: no other test restores a cached fixture. No production seam.
+ */
+import { afterAll, describe, expect, test } from 'bun:test';
+import { mkdtempSync, readdirSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { PGLiteEngine } from '../../src/core/pglite-engine.ts';
+import { assertPhysicalRoot } from '../../src/core/persistence/physical-root.ts';
+import { graduationFixtureKey, prepareGraduationFixture } from '../../scripts/persistence/graduation-fixture.ts';
+import { legacySourceMismatches } from '../fixtures/graduation/legacy-brain.ts';
+import { withEnv } from '../helpers/with-env.ts';
+
+const scratch = mkdtempSync(join(tmpdir(), 'gbrain-graduation-fixture-'));
+afterAll(() => rmSync(scratch, { recursive: true, force: true }));
+
+describe('graduation fixture cache', () => {
+ test('a cache hit re-homes the legacy brain so it verifies and its managed roots hold', async () => {
+ const cacheDir = join(scratch, 'cache');
+ const built = await prepareGraduationFixture({ kind: 'legacy', dir: join(scratch, 'built'), cacheDir });
+ expect(built.cached).toBe(false);
+ expect(readdirSync(cacheDir).filter(name => name.endsWith('.tar.gz'))).toEqual([`legacy-${built.key}.tar.gz`]);
+ const restored = await prepareGraduationFixture({ kind: 'legacy', dir: join(scratch, 'restored'), cacheDir });
+ expect(restored.cached).toBe(true);
+ expect(restored.report).toEqual(built.report);
+ expect(restored.report).toMatchObject({ pages: 9, chunks: 7, versions: 2, requests: 4, effects: 6, embeddings: { chunks: 1, facts: 1, takes: 0 } });
+
+ await withEnv({ GBRAIN_HOME: restored.home }, async () => {
+ const engine = new PGLiteEngine();
+ await engine.connect({ database_path: restored.dataDir });
+ try {
+ expect(await legacySourceMismatches(engine)).toEqual([]);
+ const bindings = await engine.executeRaw<{ worktree_id: string; local_path: string; coordination_path: string }>(
+ 'SELECT worktree_id,local_path,coordination_path FROM persistence_host_bindings');
+ expect(bindings.length).toBe(1);
+ for (const binding of bindings) {
+ expect(binding.local_path.startsWith(restored.dir)).toBe(true);
+ expect(binding.coordination_path.startsWith(restored.home)).toBe(true);
+ expect(() => assertPhysicalRoot(binding.local_path, { worktreeId: binding.worktree_id, coordinationPath: binding.coordination_path })).not.toThrow();
+ }
+ } finally { await engine.disconnect(); }
+ });
+ }, 180_000);
+
+ test('the cache key changes with the seed and the page count', async () => {
+ const base = { kind: 'history' as const, dir: scratch, pages: 1000, seed: 1 };
+ const key = await graduationFixtureKey(base);
+ expect(await graduationFixtureKey({ ...base, seed: 2 })).not.toBe(key);
+ expect(await graduationFixtureKey({ ...base, pages: 10_000 })).not.toBe(key);
+ expect(await graduationFixtureKey({ ...base, dir: join(scratch, 'elsewhere') })).toBe(key);
+ });
+});