ownership: player keeps own status and records; housekeeping rings-only

This commit is contained in:
2026-11-11 18:44:58 +00:00
parent b77a0de527
commit f5aa09c686
9 changed files with 229 additions and 59 deletions
+96 -24
View File
@@ -14,9 +14,14 @@
// insert <ring> <type> "Label" "detail" GM-authoritative insert
// delete <id> "reason" GM-authoritative removal to cold storage
// revive <id> [a|b] GM pulls an entry back from cold storage
// record <kind> "Label" "detail" GM files a record (if records are enabled)
// file <kind> "Label" "detail" the PC files a record at her desk
// (records-enabled runs; --gm to seed as GM)
// amend <rid> "new detail" the PC amends a record (--gm for GM fixes)
// discard <rid> "reason" the PC discards a record (--gm likewise)
// search <terms...> query the records (max 10; a miss is a miss)
// unarchive <rid> [a|b] GM pulls a record back into the rings
// set <key> "value" the PC updates her status (--gm to override)
// unset <key> the PC clears a status key
// dump canonical dump to dumps/memory.sql
// (also auto-written by every mutation)
// restore [--force] rebuild the db from dumps/memory.sql
@@ -42,7 +47,7 @@ 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": "..."}
// flags: --run <name> beats config.json {"run": "..."}; --gm attributes the op to the GM
const argv = process.argv.slice(2);
const runIdx = argv.indexOf('--run');
let runOverride = null;
@@ -50,6 +55,8 @@ if (runIdx !== -1) {
runOverride = argv[runIdx + 1] ?? null;
argv.splice(runIdx, 2);
}
const AS_GM = argv.includes('--gm');
for (let i = argv.indexOf('--gm'); i !== -1; i = argv.indexOf('--gm')) argv.splice(i, 1);
const [cmd, ...args] = argv;
function resolveRun() {
@@ -133,11 +140,32 @@ function renderRing(db, ring) {
return { file, count: rows.length };
}
function renderStatus(db) {
const rows = db.prepare('SELECT * FROM status ORDER BY updated DESC, key').all();
const lines = [
'<!-- GENERATED by tool/memory.mjs render — do not edit by hand. -->',
"<!-- The player's own current state, maintained via ((status key: value)) in OOC. -->",
'<!-- Stances are feelings; facts live in the rings. GM overrides are rare and journaled. -->',
'',
'# Status',
'',
];
if (!rows.length) lines.push('(nothing set — fresh start)');
for (const r of rows) lines.push(`- **${r.key}:** ${r.value}`);
lines.push('');
const file = path.join(CANON_DIR, 'status.md');
fs.mkdirSync(CANON_DIR, { recursive: true });
fs.writeFileSync(file, lines.join('\n'));
return { file, count: rows.length };
}
function renderAll(db) {
for (const ring of RINGS) {
const { file, count } = renderRing(db, ring);
console.log(`${rel(file)} — ${count} entries`);
}
const { file, count } = renderStatus(db);
console.log(`${rel(file)} — ${count} keys`);
}
// canonical serialization: id-ordered, one row per line, restorable SQL.
@@ -157,6 +185,9 @@ function dumpDb(db) {
for (const r of db.prepare('SELECT * FROM records ORDER BY id').all()) {
L.push(`INSERT INTO records VALUES (${r.id},${q(r.kind)},${q(r.label)},${q(r.detail)},${q(r.created)});`);
}
for (const r of db.prepare('SELECT * FROM status ORDER BY key').all()) {
L.push(`INSERT INTO status VALUES (${q(r.key)},${q(r.value)},${q(r.updated)});`);
}
for (const r of db.prepare('SELECT * FROM events ORDER BY id').all()) {
L.push(`INSERT INTO events VALUES (${r.id},${q(r.window)},${q(r.op)},${q(r.target)},${q(r.reason)});`);
}
@@ -219,7 +250,6 @@ function renderLog(mf, applied, overflow) {
section('Promotions', applied.promote, (p) => `[e${p.row.id}] **${p.row.label}** → ring A — ${p.reason}`);
section('Demotions', applied.demote, (d) => `[e${d.row.id}] **${d.row.label}** → ring B — ${d.reason}`);
section('Rewords', applied.reword, (r) => `[e${r.row.id}] **${r.row.label}** — now: ${r.detail} — ${r.reason}`);
section('Records (filed)', applied.record, (r) => `[r${r.rid}] [${r.kind}] **${r.label}**${r.detail ? ` — ${r.detail}` : ''} — ${r.reason}`);
section('Archived (rings → records)', applied.archive, (a) => `[r${a.rid}] [${a.row.type}] **${a.row.label}** ← [e${a.row.id}] — ${a.reason}`);
section('Deletions (to cold storage)', applied.delete, (d) => `[e${d.row.id}] **${d.row.label}** — ${d.reason}`);
section('Overflow (auto)', overflow, (o) => `[e${o.id}] ring ${o.from.toUpperCase()} → ${o.to === 'cold' ? 'cold storage' : 'ring ' + o.to.toUpperCase()}`);
@@ -263,8 +293,11 @@ function cmdApply(manifestPath) {
const inventions = asArray(mf.inventions, 'inventions');
const questions = asArray(mf.open_questions, 'open_questions');
if ((rec.length || arch.length) && !recordsEnabled()) {
errors.push('this character keeps no records — record/archive ops are unavailable (see the run\'s settings.json)');
if (arch.length && !recordsEnabled()) {
errors.push('this character keeps no records — archive ops are unavailable (see the run\'s settings.json)');
}
if (rec.length) {
errors.push('filing is not yours — record ops belong to the player at her desk (or the GM CLI); housekeeping is rings-only');
}
for (const [i, e] of ins.entries()) {
@@ -282,12 +315,6 @@ function cmdApply(manifestPath) {
if (row && !String(e.detail ?? '').trim()) errors.push(`reword (e${row.id}): detail is required`);
});
del.forEach((e) => ref('delete', e, null));
for (const [i, e] of rec.entries()) {
need(e.kind, `record[${i}].kind`);
need(e.label, `record[${i}].label`);
if (e.detail !== undefined && typeof e.detail !== 'string') errors.push(`record[${i}].detail must be a string`);
need(e.reason, `record[${i}].reason`);
}
arch.forEach((e) => ref('archive', e, null));
for (const [name, arr] of [['inventions', inventions], ['open_questions', questions]]) {
arr.forEach((s, i) => { if (typeof s !== 'string' || !s.trim()) errors.push(`${name}[${i}] must be a non-empty string`); });
@@ -299,7 +326,7 @@ function cmdApply(manifestPath) {
process.exit(1);
}
const applied = { insert: [], touch: [], promote: [], demote: [], reword: [], delete: [], record: [], archive: [] };
const applied = { insert: [], touch: [], promote: [], demote: [], reword: [], delete: [], archive: [] };
const overflow = [];
db.exec('BEGIN');
@@ -337,13 +364,6 @@ function cmdApply(manifestPath) {
journal(db, mf.window, 'reword', `e${id}`, e.reason);
applied.reword.push({ row: fetch(id), detail: e.detail, reason: e.reason });
}
for (const e of rec) {
const res = db.prepare('INSERT INTO records (kind, label, detail, created) VALUES (?, ?, ?, ?)')
.run(norm(e.kind), norm(e.label), norm(e.detail ?? ''), mf.date);
const rid = Number(res.lastInsertRowid);
journal(db, mf.window, 'record', `r${rid}`, e.reason);
applied.record.push({ rid, ...e });
}
for (const e of arch) {
const id = parseId(e.id);
const row = fetch(id);
@@ -390,7 +410,6 @@ function cmdApply(manifestPath) {
applied.promote.length && `${applied.promote.length} promoted`,
applied.demote.length && `${applied.demote.length} demoted`,
applied.reword.length && `${applied.reword.length} reworded`,
applied.record.length && `${applied.record.length} filed`,
applied.archive.length && `${applied.archive.length} archived`,
applied.delete.length && `${applied.delete.length} deleted`,
overflow.length && `${overflow.length} overflow`,
@@ -496,18 +515,65 @@ function cmdRestore(force = false) {
renderAll(db);
}
function cmdRecord(kind, label, detail) {
function cmdFile(kind, label, detail, reason) {
const db = openDb();
if (!recordsEnabled()) fail('this character keeps no records — nothing to file (see the run\'s settings.json)');
if (!kind?.trim() || !label?.trim()) fail('usage: record <kind> "Label" "detail"');
if (!kind?.trim() || !label?.trim()) fail('usage: file <kind> "Label" "detail"');
const res = db.prepare('INSERT INTO records (kind, label, detail, created) VALUES (?, ?, ?, ?)')
.run(norm(kind), norm(label), norm(detail ?? ''), today());
const rid = Number(res.lastInsertRowid);
journal(db, 'gm', 'record', `r${rid}`, 'GM edit');
journal(db, AS_GM ? 'gm' : 'pc', 'record', `r${rid}`, reason ?? (AS_GM ? 'GM seed' : 'player filing'));
console.log(`filed [r${rid}] [${norm(kind)}] **${norm(label)}**`);
dumpDb(db);
}
function cmdAmend(ridStr, detail) {
const db = openDb();
if (!recordsEnabled()) fail('this character keeps no records');
const rid = parseRid(ridStr);
const row = rid && db.prepare('SELECT * FROM records WHERE id = ?').get(rid);
if (!row) fail(`no such record: ${ridStr}`);
if (!detail?.trim()) fail('usage: amend <rid> "new detail"');
db.prepare('UPDATE records SET detail = ? WHERE id = ?').run(norm(detail), rid);
journal(db, AS_GM ? 'gm' : 'pc', 'amend', `r${rid}`, 'amended');
console.log(`amended [r${rid}] **${row.label}** — ${norm(detail)}`);
dumpDb(db);
}
function cmdDiscard(ridStr, reason) {
const db = openDb();
if (!recordsEnabled()) fail('this character keeps no records');
const rid = parseRid(ridStr);
const row = rid && db.prepare('SELECT * FROM records WHERE id = ?').get(rid);
if (!row) fail(`no such record: ${ridStr}`);
db.prepare('DELETE FROM records WHERE id = ?').run(rid);
journal(db, AS_GM ? 'gm' : 'pc', 'discard', `r${rid}`, reason ?? 'discarded');
console.log(`discarded [r${rid}] **${row.label}**`);
dumpDb(db);
}
function cmdSet(key, value, reason) {
const db = openDb();
if (!key?.trim() || !value?.trim()) fail('usage: set <key> "value" ["reason"]');
db.prepare('INSERT INTO status (key, value, updated) VALUES (?, ?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value, updated = excluded.updated')
.run(norm(key), norm(value), today());
journal(db, AS_GM ? 'gm' : 'pc', 'status', `s:${norm(key)}`, reason ?? (AS_GM ? 'GM override' : 'player declaration'));
console.log(`status ${norm(key)}: ${norm(value)}`);
renderAll(db);
dumpDb(db);
}
function cmdUnset(key) {
const db = openDb();
const row = db.prepare('SELECT * FROM status WHERE key = ?').get(norm(key ?? ''));
if (!row) fail(`no such status key: ${key}`);
db.prepare('DELETE FROM status WHERE key = ?').run(row.key);
journal(db, AS_GM ? 'gm' : 'pc', 'status', `s:${row.key}`, 'cleared');
console.log(`status ${row.key}: cleared`);
renderAll(db);
dumpDb(db);
}
function cmdSearch(terms) {
const db = openDb();
if (!recordsEnabled()) fail('this character keeps no records — nothing to consult');
@@ -543,6 +609,8 @@ function cmdStatus() {
console.log(`run: ${RUN}${recordsEnabled() ? '' : ' (no records)'}`);
const nr = db.prepare('SELECT COUNT(*) AS n FROM records').get().n;
console.log(`records: ${nr}`);
const sk = db.prepare('SELECT key FROM status ORDER BY updated DESC, key').all().map((r) => r.key);
console.log(`status: ${sk.length ? sk.join(', ') : '(nothing set)'}`);
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] ?? '∞';
@@ -566,7 +634,11 @@ switch (cmd) {
case 'insert': (args.length >= 3) ? gmOp(args[0], args[1], args[2], args[3] ?? '') : fail('usage: insert <a|b> <type> "Label" "detail"'); break;
case 'delete': (args.length >= 1) ? cmdDelete(args[0], args[1]) : fail('usage: delete <id> "reason"'); break;
case 'revive': (args.length >= 1) ? cmdRevive(args[0], args[1]) : fail('usage: revive <id> [a|b]'); break;
case 'record': (args.length >= 2) ? cmdRecord(args[0], args[1], args[2] ?? '') : fail('usage: record <kind> "Label" "detail"'); break;
case 'file': (args.length >= 2) ? cmdFile(args[0], args[1], args[2] ?? '', args[3]) : fail('usage: file <kind> "Label" "detail" ["reason"]'); break;
case 'amend': (args.length >= 2) ? cmdAmend(args[0], args[1]) : fail('usage: amend <rid> "new detail"'); break;
case 'discard': (args.length >= 1) ? cmdDiscard(args[0], args[1]) : fail('usage: discard <rid> "reason"'); break;
case 'set': (args.length >= 2) ? cmdSet(args[0], args[1], args[2]) : fail('usage: set <key> "value" ["reason"]'); break;
case 'unset': (args.length >= 1) ? cmdUnset(args[0]) : fail('usage: unset <key>'); break;
case 'search': cmdSearch(args); break;
case 'unarchive': (args.length >= 1) ? cmdUnarchive(args[0], args[1]) : fail('usage: unarchive <rid> [a|b]'); break;
case 'status': cmdStatus(); break;