multiuser: token identity, invites + bootstrap, owner-gated runs, fork, user packages; spectator spa; utc house rule

This commit is contained in:
2026-11-14 20:01:15 +00:00
parent 9f85adc3c6
commit 711634cc38
15 changed files with 1134 additions and 105 deletions
+115 -28
View File
@@ -16,6 +16,9 @@
// init the db, render the views.
// From then on the run dir is the only
// truth; the package is never read again.
// save-package "display name" save this run's craft as a new user
// package (state/packages/, published to
// all; pc never rides; --owner stamps)
// init create the run's database and stubs
// (the low-level primitive under create)
// seed [--force] [file] one-time pre-story migration; applies
@@ -57,6 +60,7 @@ import { fileURLToPath } from 'node:url';
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const SCHEMA_PATH = path.join(ROOT, 'db', 'schema.sql');
const PACKAGES_DIR = path.join(ROOT, 'packages');
const STATE_PACKAGES_DIR = path.join(ROOT, 'state', 'packages');
const RUNS_DIR = path.join(ROOT, 'runs');
// dials — retune freely
@@ -83,6 +87,12 @@ const QUIET = argv.includes('--quiet');
for (let i = argv.indexOf('--quiet'); i !== -1; i = argv.indexOf('--quiet')) argv.splice(i, 1);
const AS_JSON = argv.includes('--json');
for (let i = argv.indexOf('--json'); i !== -1; i = argv.indexOf('--json')) argv.splice(i, 1);
const ownerIdx = argv.indexOf('--owner');
let ownerOverride = null;
if (ownerIdx !== -1) {
ownerOverride = argv[ownerIdx + 1] ?? null;
argv.splice(ownerIdx, 2);
}
const [cmd, ...args] = argv;
// a run is an instance under runs/, created from a package by `create`;
@@ -555,11 +565,14 @@ function cmdInit() {
// --- packages and runs --------------------------------------------------------
//
// A package is a preset: authored content only, no state. A run is an
// instance: the package copied wholesale (the shared prompts included —
// battery included, editable per run), plus all state. Creation is the
// one time a package is ever read; afterwards the run dir is the only
// truth. run.json records the lineage and an optional model override.
// A package is a preset: authored content only, no state. Packages
// come from two roots: system (repo, authored, read-only) and user
// (state/packages, saved from a run, published to everyone). A run is
// an instance: the package copied wholesale (the shared prompts
// included — battery included, editable per run), plus all state.
// Creation is the one time a package is ever read; afterwards the run
// dir is the only truth. run.json records the lineage, the owner
// (multiuser), and an optional model override.
const PACKAGE_FILES = ['backdrop.md', 'turn-0.md', 'character.md', 'seed.json', 'settings.json', 'gm.md'];
// battery included, copied per run and then the run's to edit: the craft
@@ -569,27 +582,57 @@ const PACKAGE_FILES = ['backdrop.md', 'turn-0.md', 'character.md', 'seed.json',
// substrate facts)
const SHARED_PROMPTS = ['handbook.md'];
function cmdPackages() {
if (!fs.existsSync(PACKAGES_DIR)) { if (AS_JSON) console.log('[]'); else console.log('(no packages)'); return; }
const list = [];
for (const d of fs.readdirSync(PACKAGES_DIR, { withFileTypes: true })) {
if (!d.isDirectory()) continue;
const p = path.join(PACKAGES_DIR, d.name);
const has = (f) => fs.existsSync(path.join(p, f));
let settings = {};
try { settings = JSON.parse(fs.readFileSync(path.join(p, 'settings.json'), 'utf8')); } catch {}
list.push({
name: d.name,
display: settings.name ?? d.name,
pc: settings.pc ?? null,
records: settings.records === true,
turn0: has('turn-0.md'), seed: has('seed.json'),
complete: has('backdrop.md') && has('character.md'),
});
// resolve a package name against both roots; the repo wins a collision
function packageDir(name) {
if (!/^[a-z0-9-]+$/.test(name ?? '')) return null;
if (fs.existsSync(path.join(PACKAGES_DIR, name))) return path.join(PACKAGES_DIR, name);
if (fs.existsSync(path.join(STATE_PACKAGES_DIR, name))) return path.join(STATE_PACKAGES_DIR, name);
return null;
}
// the package meta of a user package (owner, created); system packages
// have none — their origin is the repo itself
function readPackageMeta(dir) {
try {
return JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8'));
} catch {
return null;
}
}
function packageEntry(name, dir, origin) {
const has = (f) => fs.existsSync(path.join(dir, f));
let settings = {};
try { settings = JSON.parse(fs.readFileSync(path.join(dir, 'settings.json'), 'utf8')); } catch {}
const meta = origin === 'user' ? readPackageMeta(dir) : null;
return {
name,
origin,
...(origin === 'user' ? { owner: meta?.owner ?? null } : {}),
display: settings.name ?? name,
pc: settings.pc ?? null,
records: settings.records === true,
turn0: has('turn-0.md'), seed: has('seed.json'),
complete: has('backdrop.md') && has('character.md'),
};
}
function cmdPackages() {
const list = [];
const scan = (root, origin) => {
if (!fs.existsSync(root)) return;
for (const d of fs.readdirSync(root, { withFileTypes: true })) {
if (!d.isDirectory()) continue;
list.push(packageEntry(d.name, path.join(root, d.name), origin));
}
};
scan(PACKAGES_DIR, 'system');
scan(STATE_PACKAGES_DIR, 'user');
list.sort((a, b) => a.name.localeCompare(b.name));
if (AS_JSON) { console.log(JSON.stringify(list)); return; }
if (!list.length) { console.log('(no packages)'); return; }
for (const x of list) {
console.log(`${x.name}: ${x.display} — records ${x.records ? 'on' : 'off'} · turn-0 ${x.turn0 ? 'yes' : 'no'} · seed ${x.seed ? 'yes' : 'no'} · backdrop+card ${x.complete ? 'ok' : 'MISSING'}`);
console.log(`${x.name}: ${x.display} [${x.origin}] — records ${x.records ? 'on' : 'off'} · turn-0 ${x.turn0 ? 'yes' : 'no'} · seed ${x.seed ? 'yes' : 'no'} · backdrop+card ${x.complete ? 'ok' : 'MISSING'}`);
}
}
@@ -614,13 +657,13 @@ function cmdCreate(pkgName, pc, displayName, model) {
if (!pkgName || !/^[a-z0-9-]+$/.test(pkgName) || !pc || !String(pc).trim()) {
fail('usage: create <package> <pc-name> ["package display name"] ["model"]');
}
const pkgDir = path.join(PACKAGES_DIR, pkgName);
if (!fs.existsSync(pkgDir)) fail(`no such package: ${pkgName} — see: packages`);
const pkgDir = packageDir(pkgName);
if (!pkgDir) fail(`no such package: ${pkgName} — see: packages`);
pc = String(pc).trim();
// different runs of the same package require different pc names
if (fs.existsSync(RUNS_DIR)) {
for (const d of fs.readdirSync(RUNS_DIR, { withFileTypes: true })) {
if (!d.isDirectory()) continue;
if (!d.isDirectory() || d.name.startsWith('.')) continue; // .staging-* is never a run
const rd = path.join(RUNS_DIR, d.name);
let meta = null;
try { meta = JSON.parse(fs.readFileSync(path.join(rd, 'run.json'), 'utf8')); } catch { continue; }
@@ -651,6 +694,7 @@ function cmdCreate(pkgName, pc, displayName, model) {
fs.writeFileSync(path.join(runDir, 'settings.json'), JSON.stringify(settings, null, 2) + '\n');
const meta = { package: pkgName, created: today() };
if (model) meta.model = model;
if (ownerOverride) meta.owner = ownerOverride;
fs.writeFileSync(path.join(runDir, 'run.json'), JSON.stringify(meta, null, 2) + '\n');
for (const d of ['db', 'canon', 'dumps', 'log', 'manifests', 'sessions']) {
@@ -671,19 +715,25 @@ function cmdRuns() {
const list = [];
let found = 0;
for (const d of fs.readdirSync(RUNS_DIR, { withFileTypes: true })) {
if (!d.isDirectory()) continue;
if (!d.isDirectory() || d.name.startsWith('.')) continue; // .staging-* is never a run
const runDir = path.join(RUNS_DIR, d.name);
if (!fs.existsSync(path.join(runDir, 'run.json'))) continue; // a run is marked by run.json
found++;
let pkg = '?';
let model = null;
let owner = null;
let created = null;
let forked = null;
try {
const meta = JSON.parse(fs.readFileSync(path.join(runDir, 'run.json'), 'utf8'));
pkg = meta.package ?? '?';
model = meta.model ?? null;
owner = meta.owner ?? null;
created = meta.created ?? null;
forked = meta.forked ?? null;
} catch {}
const st = readRunSettings(runDir);
const base = { id: d.name, package: pkg, model, pc: st.pc ?? null, display: runDisplay(pkg, st) };
const base = { id: d.name, package: pkg, model, owner, created, forked, pc: st.pc ?? null, display: runDisplay(pkg, st) };
const dbPath = path.join(runDir, 'db', 'memory.db');
if (!fs.existsSync(dbPath)) { list.push({ ...base, initialized: false }); continue; }
const db = new DatabaseSync(dbPath);
@@ -701,6 +751,42 @@ function cmdRuns() {
function today() { return new Date().toISOString().slice(0, 10); }
// save the run's craft as a new user package: the same files create
// copies (plus handbook), published to every user of the deployment —
// gm.md included, because saving is the act of publishing. The saved
// package carries no pc (the next create asks for one); the display
// name is the save's one decision. The id is anonymous hex, like a
// run's: no facts, nothing to rename.
function cmdSavePackage(displayName) {
const name = String(displayName ?? '').trim();
if (!name) fail('usage: save-package "display name" (run required via --run)');
let id;
for (;;) {
id = randomBytes(4).toString('hex');
if (!fs.existsSync(path.join(STATE_PACKAGES_DIR, id)) && !fs.existsSync(path.join(PACKAGES_DIR, id))) break;
}
const dst = path.join(STATE_PACKAGES_DIR, id);
fs.mkdirSync(dst, { recursive: true });
for (const f of [...PACKAGE_FILES, ...SHARED_PROMPTS]) {
const src = path.join(RUN_ROOT, f);
if (fs.existsSync(src)) fs.copyFileSync(src, path.join(dst, f));
}
const settingsPath = path.join(dst, 'settings.json');
let settings = {};
try { settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8')); } catch {}
settings.name = name;
delete settings.pc; // per-run identity never rides a package
fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2) + '\n');
fs.writeFileSync(path.join(dst, 'package.json'), JSON.stringify({
owner: ownerOverride, created: today(),
}, null, 2) + '\n');
if (AS_JSON) {
console.log(JSON.stringify({ id, name, origin: 'user', owner: ownerOverride }));
return;
}
console.log(`saved package '${id}' — ${name}: ${rel(dst)}`);
}
function gmOp(ring, type, label, detail) {
const db = openDb();
if (!RINGS.includes(ring) || !TYPES.includes(type)) {
@@ -991,6 +1077,7 @@ switch (cmd) {
case 'packages': cmdPackages(); break;
case 'runs': cmdRuns(); break;
case 'create': cmdCreate(args[0], args[1], args[2], args[3]); break;
case 'save-package': cmdSavePackage(args[0]); break;
case 'seed': cmdSeed(FORCE, args[0]); break;
case 'init': cmdInit(); break;
case 'render': renderAll(openDb()); break;