diff --git a/data/seed/cqa-dev.db b/data/seed/cqa-dev.db index 5b2ac50..08194f4 100644 Binary files a/data/seed/cqa-dev.db and b/data/seed/cqa-dev.db differ diff --git a/docs/RUNBOOK.md b/docs/RUNBOOK.md index d2712b5..157a38c 100644 --- a/docs/RUNBOOK.md +++ b/docs/RUNBOOK.md @@ -6,7 +6,7 @@ The demo runs on one laptop (Fedora or Windows), as one process on one URL. No D 1. Install Node 24 and pnpm 10: commands for Linux, macOS and Windows are in the README ("Install prerequisites"). After installing Node, run `npm install -g pnpm@10.33.2` (the version pinned in `package.json`). 2. `pnpm install` 3. Copy `.env.example` to `.env`. Put `DATASET_IMPORT_URL` (server-only) and, once chosen, the model key there. Quote the URL if you source the file in a shell; the apps read it themselves and do not need quoting. -4. For a ready-to-inspect demo, run `pnpm seed`. It installs the committed DEV snapshot (`data/seed/`: database with 65 conversations and their evaluations, plus the 5 call recordings) without overwriting an existing database. It contains 65 completed evaluations and one failed attempt. The snapshot is the organizer-cleared DEV publication (2026-11-08). +4. For a ready-to-inspect demo, run `pnpm seed`. It installs the committed DEV snapshot (`data/seed/`: database with 65 conversations, their latest evaluations and the directory, plus the 5 call recordings) without overwriting an existing database. To refresh the snapshot from your live database run `pnpm seed:export` (DEV conversations only, latest evaluation per conversation, directory kept; the control group and run history are never exported) and commit `data/seed/`. Re-run it before pushing whenever you changed data you want on other machines. It contains 65 completed evaluations and one failed attempt. The snapshot is the organizer-cleared DEV publication (2026-11-08). 5. `pnpm start` builds everything and serves web + API at http://127.0.0.1:8787 (`PORT`/`HOST` to change). 6. Alternatively, import the data once (not needed after `pnpm seed`): `curl -X POST localhost:8787/api/datasets/import -H "content-type: application/json" -d '{"groupId":"DEV"}'` (PowerShell: `Invoke-RestMethod -Method Post -Uri http://localhost:8787/api/datasets/import -ContentType application/json -Body '{"groupId":"DEV"}'`). diff --git a/package.json b/package.json index f56e5d3..4243b8d 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "test": "pnpm -r test", "build": "pnpm -r build", "seed": "node scripts/seed.mjs", + "seed:export": "node scripts/export-seed.mjs", "prod": "node scripts/prod.mjs", "stop": "node scripts/stop.mjs", "results:check": "node scripts/export-results.mjs --check", diff --git a/scripts/export-seed.mjs b/scripts/export-seed.mjs new file mode 100644 index 0000000..47ab232 --- /dev/null +++ b/scripts/export-seed.mjs @@ -0,0 +1,70 @@ +// Rebuilds the committed DEV snapshot (data/seed/cqa-dev.db) from the live database. Usage: pnpm seed:export [--group DEV] [--db data/cqa.sqlite] +// Consistent copy via VACUUM INTO (safe while the server runs, includes WAL content), then everything outside the +// exported group is deleted from the COPY. The live database is only read. +// - conversations: the group only (the control group never leaves local storage) +// - evaluations: the latest completed one per conversation (latest failed if none completed), plus any evaluation +// that a review, appeal, crosscheck or tag points at, so no reference dangles +// - rows tied to a conversation (reviews, comments, tasks, ...) follow the conversations; people, teams, taxonomy, +// checklist versions and timeline events are kept as is +// - jobs and import/run journal events are dropped (they list ids and groups of runs, nothing to restore) +import { createRequire } from 'node:module'; +import { mkdirSync, renameSync, rmSync, statSync } from 'node:fs'; +import { dirname, join, resolve } from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const root = join(dirname(fileURLToPath(import.meta.url)), '..'); +const require = createRequire(join(root, 'apps', 'api', 'package.json')); +const Database = require('better-sqlite3'); + +const arg = (name, fallback) => { + const i = process.argv.indexOf(`--${name}`); + return i > 0 ? process.argv[i + 1] : fallback; +}; +const group = arg('group', 'DEV'); +const source = resolve(arg('db', join(root, 'data', 'cqa.sqlite'))); +const target = join(root, 'data', 'seed', 'cqa-dev.db'); +const tmp = `${target}.tmp`; + +mkdirSync(dirname(target), { recursive: true }); +rmSync(tmp, { force: true }); +const live = new Database(source, { readonly: true }); +live.prepare('VACUUM INTO ?').run(tmp); +live.close(); + +const db = new Database(tmp); +const hasTable = (t) => !!db.prepare("SELECT 1 FROM sqlite_master WHERE type='table' AND name=?").get(t); +const run = (sql, ...p) => (hasTable(sql.match(/(?:FROM|UPDATE)\s+(\w+)/i)[1]) ? db.prepare(sql).run(...p).changes : 0); +db.transaction(() => { + db.prepare('CREATE TEMP TABLE keep_conv AS SELECT id FROM conversations WHERE dataset_group_id = ?').run(group); + db.exec(`CREATE TEMP TABLE keep_eval AS + SELECT id FROM ( + SELECT id, ROW_NUMBER() OVER (PARTITION BY conversation_id ORDER BY (status = 'completed') DESC, created_at DESC, id DESC) AS rn + FROM evaluations WHERE conversation_id IN (SELECT id FROM keep_conv) + ) WHERE rn = 1`); + const pinned = ['reviews', 'appeals', 'crosschecks', 'tags'].filter(hasTable); + for (const t of pinned) db.exec(`INSERT INTO keep_eval SELECT evaluation_id FROM ${t} WHERE evaluation_id IN (SELECT id FROM evaluations WHERE conversation_id IN (SELECT id FROM keep_conv))`); + + run('DELETE FROM evaluations WHERE id NOT IN (SELECT id FROM keep_eval)'); + run('DELETE FROM conversations WHERE id NOT IN (SELECT id FROM keep_conv)'); + for (const t of ['operator_assignments', 'reviews', 'comments', 'tasks', 'crosschecks', 'tags', 'notifications']) + run(`DELETE FROM ${t} WHERE conversation_id IS NOT NULL AND conversation_id NOT IN (SELECT id FROM keep_conv)`); + run('DELETE FROM appeals WHERE evaluation_id NOT IN (SELECT id FROM evaluations)'); + run("DELETE FROM events WHERE type NOT LIKE 'directory.%' AND (conversation_id IS NULL OR conversation_id NOT IN (SELECT id FROM keep_conv))"); + run('DELETE FROM jobs'); +})(); +db.pragma('journal_mode = DELETE'); +db.exec('VACUUM'); + +const count = (t) => (hasTable(t) ? db.prepare(`SELECT COUNT(*) n FROM ${t}`).get().n : 0); +const summary = Object.fromEntries(['conversations', 'evaluations', 'checklist_versions', 'people', 'teams', 'events', 'reviews', 'appeals'].map((t) => [t, count(t)])); +const foreign = db.prepare('SELECT COUNT(*) n FROM conversations WHERE dataset_group_id <> ?').get(group).n; +db.close(); +if (foreign) { + rmSync(tmp, { force: true }); + throw new Error(`export aborted: ${foreign} conversation(s) outside group ${group}`); +} +rmSync(target, { force: true }); +rmSync(`${target}-wal`, { force: true }); +rmSync(`${target}-shm`, { force: true }); +renameSync(tmp, target); +console.log(`wrote ${target} (${(statSync(target).size / 1e6).toFixed(1)} MB) for group ${group}:`, summary);