runs: per-story state, prisoners and boutique interleaved

This commit is contained in:
2026-11-11 18:25:09 +00:00
parent 1dcdc917d2
commit 57e8718ffa
18 changed files with 157 additions and 28 deletions
+63 -10
View File
@@ -7,7 +7,8 @@
// canon/ is a generated view (never edit by hand)
//
// commands:
// init create the database
// runs list runs and their fill levels
// init create the run's database
// render write canon/ring-a.md and canon/ring-b.md
// apply <manifest.json> apply a housekeeper manifest (validated, transactional)
// insert <ring> <type> "Label" "detail" GM-authoritative insert
@@ -16,6 +17,8 @@
// dump canonical dump to dumps/memory.sql
// (also auto-written by every mutation)
// restore [--force] rebuild the db from dumps/memory.sql
// commands take --run <name>; the default comes from config.json {"run": "..."}.
//
// status counts, caps, and what's sinking
import { DatabaseSync } from 'node:sqlite';
@@ -24,11 +27,9 @@ import path from 'node:path';
import { fileURLToPath } from 'node:url';
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const DB_PATH = path.join(ROOT, 'db', 'memory.db');
const SCHEMA_PATH = path.join(ROOT, 'db', 'schema.sql');
const CANON_DIR = path.join(ROOT, 'canon');
const LOG_DIR = path.join(ROOT, 'log');
const DUMPS_DIR = path.join(ROOT, 'dumps');
const CONFIG_PATH = path.join(ROOT, 'config.json');
const RUNS_DIR = path.join(ROOT, 'runs');
// dials — retune freely
const CAPS = { a: 500, b: 3000 };
@@ -38,6 +39,38 @@ const RINGS = ['a', 'b']; // renderable; 'cold' is beyond the models
const fail = (msg) => { console.error(`error: ${msg}`); process.exit(1); };
const rel = (p) => path.relative(ROOT, p).replaceAll('\\', '/');
// run resolution: --run <name> beats config.json {"run": "..."}
const argv = process.argv.slice(2);
const runIdx = argv.indexOf('--run');
let runOverride = null;
if (runIdx !== -1) {
runOverride = argv[runIdx + 1] ?? null;
argv.splice(runIdx, 2);
}
const [cmd, ...args] = argv;
function resolveRun() {
if (runOverride) return runOverride;
if (fs.existsSync(CONFIG_PATH)) {
try {
const r = JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf8')).run;
if (r) return r;
} catch {}
}
return null;
}
const RUN = cmd === 'runs' ? null : resolveRun();
if (cmd !== 'runs' && (!RUN || !/^[a-z0-9-]+$/.test(RUN))) {
fail('no run specified — use --run <name> or {"run":"name"} in config.json');
}
const RUN_ROOT = path.join(RUNS_DIR, RUN ?? '');
const DB_PATH = path.join(RUN_ROOT, 'db', 'memory.db');
const CANON_DIR = path.join(RUN_ROOT, 'canon');
const LOG_DIR = path.join(RUN_ROOT, 'log');
const DUMPS_DIR = path.join(RUN_ROOT, 'dumps');
function openDb() {
if (!fs.existsSync(DB_PATH)) {
fail(`no database at ${rel(DB_PATH)} — run: node tool/memory.mjs init`);
@@ -298,7 +331,7 @@ function cmdApply(manifestPath) {
fs.mkdirSync(LOG_DIR, { recursive: true });
fs.writeFileSync(logFile, renderLog(mf, applied, overflow));
console.log(`applied ${mf.window}: ` + [
console.log(`applied [${RUN}] ${mf.window}: ` + [
applied.insert.length && `${applied.insert.length} inserted`,
applied.touch.length && `${applied.touch.length} touched`,
applied.promote.length && `${applied.promote.length} promoted`,
@@ -315,11 +348,30 @@ function cmdApply(manifestPath) {
// --- simple commands ---------------------------------------------------------
function cmdInit() {
fs.mkdirSync(path.join(ROOT, 'db'), { recursive: true });
for (const d of ['db', 'log', 'manifests']) {
const dir = path.join(RUN_ROOT, d);
fs.mkdirSync(dir, { recursive: true });
const keep = path.join(dir, '.gitkeep');
if (!fs.existsSync(keep)) fs.writeFileSync(keep, '');
}
const db = new DatabaseSync(DB_PATH);
db.exec(fs.readFileSync(SCHEMA_PATH, 'utf8'));
console.log(`initialized ${rel(DB_PATH)}`);
console.log(`dump: ${rel(dumpDb(db))}`);
console.log(`initialized run '${RUN}': ${rel(DB_PATH)}`);
dumpDb(db);
renderAll(db);
}
function cmdRuns() {
if (!fs.existsSync(RUNS_DIR)) { console.log('(no runs)'); return; }
for (const d of fs.readdirSync(RUNS_DIR, { withFileTypes: true })) {
if (!d.isDirectory()) continue;
const dbPath = path.join(RUNS_DIR, d.name, 'db', 'memory.db');
if (!fs.existsSync(dbPath)) { console.log(`${d.name}: (not initialized)`); continue; }
const db = new DatabaseSync(dbPath);
db.exec(fs.readFileSync(SCHEMA_PATH, 'utf8'));
const n = (ring) => db.prepare('SELECT COUNT(*) AS n FROM entries WHERE ring = ?').get(ring).n;
console.log(`${d.name}: a ${n('a')}/${CAPS.a} · b ${n('b')}/${CAPS.b} · cold ${n('cold')}`);
}
}
function today() { return new Date().toISOString().slice(0, 10); }
@@ -388,6 +440,7 @@ function cmdRestore(force = false) {
function cmdStatus() {
const db = openDb();
console.log(`run: ${RUN}`);
for (const ring of ['a', 'b', 'cold']) {
const n = db.prepare('SELECT COUNT(*) AS n FROM entries WHERE ring = ?').get(ring).n;
const cap = CAPS[ring] ?? '∞';
@@ -403,8 +456,8 @@ function cmdStatus() {
// --- dispatch ----------------------------------------------------------------
const [cmd, ...args] = process.argv.slice(2);
switch (cmd) {
case 'runs': cmdRuns(); break;
case 'init': cmdInit(); break;
case 'render': renderAll(openDb()); break;
case 'apply': args[0] ? cmdApply(args[0]) : fail('usage: apply <manifest.json>'); break;