import { spawn } from "node:child_process"; import { mkdtemp, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { dirname, join, resolve } from "node:path"; import { DatabaseSync } from "node:sqlite"; import { fileURLToPath } from "node:url"; import { MemoryStore } from "../src/store.ts"; const repositoryRoot = resolve( dirname(fileURLToPath(import.meta.url)), "../../..", ); /** * Seed a realistic disposable knowledge store: multi-table schema, journal * history, size samples, one zombie table, one hot table. Never touches the * user's real database. */ async function seed(path: string): Promise { const store = await MemoryStore.open(path); const db = new DatabaseSync(path); try { db.exec(` CREATE TABLE facts(entity TEXT NOT NULL, attribute TEXT NOT NULL, value TEXT NOT NULL, observed_at TEXT NOT NULL, source TEXT, project TEXT); CREATE TABLE events(kind TEXT NOT NULL, subject TEXT NOT NULL, payload TEXT, occurred_at TEXT NOT NULL); CREATE TABLE edges(from_id TEXT NOT NULL, kind TEXT NOT NULL, to_id TEXT NOT NULL); CREATE TABLE deploy_log(stage TEXT, message TEXT, at TEXT); CREATE TABLE old_experiments(topic TEXT, note TEXT); CREATE INDEX idx_facts_entity ON facts(entity); CREATE VIRTUAL TABLE facts_fts USING fts5(value, content='facts'); `); const fact = db.prepare("INSERT INTO facts VALUES(?,?,?,?,?,?)"); const entities = ["pi-ext", "niri", "onyx", "termux"]; const attributes = ["build", "deploy", "caveat", "config"]; for (let index = 0; index < 40; index++) fact.run( entities[index % 4] ?? "pi-ext", attributes[index % 4] ?? "build", `Fact ${index}: builds require Node 26 and mise tasks.`, "2026-10-28", "synthetic preview seed", index % 2 ? "/home/me/Workspace/pi-ext" : "", ); const event = db.prepare("INSERT INTO events VALUES(?,?,?,?)"); for (let index = 0; index < 120; index++) event.run("deploy", "luci", `run ${index} ok`, "2026-10-29"); const edge = db.prepare("INSERT INTO edges VALUES(?,?,?)"); for (let index = 0; index < 30; index++) edge.run("pi-ext", "uses", "niri"); const log = db.prepare("INSERT INTO deploy_log VALUES(?,?,?)"); for (let index = 0; index < 400; index++) log.run("build", `tsc pass ${index}`, "2026-10-29"); db.prepare( "INSERT INTO old_experiments VALUES('grafton','superseded')", ).run(); // Journal: a believable mix of reads, writes, ddl, one error, one undo. const insert = db.prepare( "INSERT INTO _journal(ts,actor,mode,readonly,sql,params,rowsReturned,rowsWritten,durationMs,ok,tablesTouched,schemaChanged) VALUES(?,?,?,?,?,?,?,?,?,1,?,?)", ); const now = Date.now(); const journalRows: Array<{ mode: string; rowsReturned: number; rowsWritten: number; sql: string; schemaChanged: boolean; }> = [ { mode: "ddl", rowsReturned: 0, rowsWritten: 0, sql: "CREATE TABLE facts(...)", schemaChanged: true, }, { mode: "ddl", rowsReturned: 0, rowsWritten: 0, sql: "CREATE TABLE events(...)", schemaChanged: true, }, { mode: "write", rowsReturned: 0, rowsWritten: 40, sql: "INSERT INTO facts ...", schemaChanged: false, }, { mode: "read", rowsReturned: 12, rowsWritten: 0, sql: "SELECT * FROM facts WHERE project=''", schemaChanged: false, }, { mode: "read", rowsReturned: 30, rowsWritten: 0, sql: "SELECT * FROM facts WHERE entity='niri'", schemaChanged: false, }, { mode: "write", rowsReturned: 0, rowsWritten: 120, sql: "INSERT INTO events ...", schemaChanged: false, }, { mode: "read", rowsReturned: 1, rowsWritten: 0, sql: "SELECT value FROM facts_fts WHERE value MATCH 'node'", schemaChanged: false, }, { mode: "read", rowsReturned: 0, rowsWritten: 0, sql: "SELECT * FROM deploy_log", schemaChanged: false, }, { mode: "write", rowsReturned: 0, rowsWritten: 30, sql: "INSERT INTO edges ...", schemaChanged: false, }, { mode: "read", rowsReturned: 8, rowsWritten: 0, sql: "SELECT * FROM edges", schemaChanged: false, }, { mode: "read", rowsReturned: 3, rowsWritten: 0, sql: "SELECT * FROM facts", schemaChanged: false, }, { mode: "write", rowsReturned: 0, rowsWritten: 1, sql: "DELETE FROM deploy_log WHERE stage='x'", schemaChanged: false, }, ]; for (const [index, entry] of journalRows.entries()) { insert.run( new Date(now - (journalRows.length - index) * 900_000).toISOString(), index % 4 === 3 ? "human" : "agent", entry.mode, entry.mode === "read" ? 1 : 0, entry.sql, "[]", entry.rowsReturned, entry.rowsWritten, 4 + index, JSON.stringify([]), entry.schemaChanged ? 1 : 0, ); } db.prepare( "INSERT INTO _journal(ts,actor,mode,readonly,sql,params,durationMs,ok,error,tablesTouched) VALUES(?,?,?,?,?,?,?,?,?,?)", ).run( new Date(now - 600_000).toISOString(), "agent", "read", 1, "SELECT * FROM factz", "[]", 3, 0, "No table 'factz'. Existing tables: facts, events… see _schema_docs", "[]", ); store.sampleSizes(); } finally { db.close(); store.close(); } } async function main(): Promise { const cwd = await mkdtemp(join(tmpdir(), "pi-memory-viewer-")); const database = join(cwd, "memory.sqlite"); await seed(database); const distMemory = join(repositoryRoot, "dist", "memory"); await writeFile(join(cwd, "README.txt"), "Disposable viewer preview data.\n"); const child = spawn( "pi", [ "--no-extensions", "--no-skills", "--no-context-files", "--no-prompt-templates", "--no-themes", "--no-mcp", "--extension", distMemory, "--tools", "memory", ], { cwd, env: { ...process.env, PI_MEMORY_DATABASE_PATH: database, PI_MEMORY_DEBUG: "0", PI_TELEMETRY: "0", }, stdio: "inherit", }, ); await new Promise((resolveExit, reject) => { child.once("error", reject); child.once("exit", (code) => resolveExit(code)); }); } void main().catch((error: unknown) => { process.stderr.write( `${error instanceof Error ? error.message : String(error)}\n`, ); process.exitCode = 1; });