the access round: spectator mode removed and reads made story-only -- the table, raw session, carryover, files, status, and the notepad are owner-only; the story is the one public read (any signed-in user, fork button on it that copies the run that made the story, never the view); #28's human-side open read overturned with the ruling, gm.md stays never model-facing; non-owners land in the story view (play/setup redirect on the 403, home cards link there directly), the story view names its owner and stands alone on the story response, which now carries display/owner/ownerName; the table gains a raw tab -- the whole log as it rode the wire (thinking, every variant block, every reminder copy, full tool results, rides inline) next to the curated projection, live sessions only, sticky across turn re-renders; --debug mode for easy testing: every request is a fixed superuser that bypasses the users db entirely, no invite dance, loopback-bound regardless of config (found live: the SPA boot only asked the server who it was when a token existed, so tokenless browsers never learned they were the debug user -- boot now always asks); lint rule 10: no laptop-visible facts anywhere and all commits always from Mute -- the username and home paths are derived at runtime so the checker never contains them, scanned over the tracked tree, every historical blob, and commit messages, identity enforced on author and committer on every ref plus git config; the queue split: resolved questions relocated to DECISIONS.md verbatim by number (day's words kept, reversals annotate never rewrite, the agenda/reminder trio gets its own section), QUESTIONS is open items only (stale OPENs resolved on the way: #10 by the 2026-11-13 pairing default, #11 by the raw tab); smoke reworked to the story-only battery -- ash 403s on every run surface, 200 on the story alone, the ownerless-run check rides the runs list; all twelve QUESTIONS-number citations re-pointed at DECISIONS; smoke ALL PASS in a worktree (111 checks), two-user browser pass (the non-owner's whole run surface is a 403 detail and a 200 story, back-to-table hidden, owner views intact), raw-tab browser pass (curated table leak-free, raw shows the whole variants block, tabs vanish on dormancy), typecheck green on both programs, check-repo clean
This commit is contained in:
+81
-34
@@ -2,12 +2,18 @@
|
||||
//
|
||||
// Synchronous JSON over HTTP; the SPA and curl are both just clients.
|
||||
// Multiuser: bearer-token identity (users.mjs), ownership gating on
|
||||
// every mutation and model call, fork for everyone else. Boot-rebuild:
|
||||
// every mutation, model call, and run read (the story is the one
|
||||
// public surface), fork for everyone. Boot-rebuild:
|
||||
// every run whose latest session was not housekept gets its open
|
||||
// session back from the jsonl -- the log is the truth, the context is a
|
||||
// view.
|
||||
//
|
||||
// usage: node server/main.mjs [--mock]
|
||||
// Debug mode (--debug): every request is one fixed superuser that
|
||||
// bypasses the user database entirely -- no invite dance, no tokens.
|
||||
// Loopback-bound whatever the config says. Testing only, never a real
|
||||
// deployment.
|
||||
//
|
||||
// usage: node server/main.mjs [--mock] [--debug]
|
||||
/** @typedef {import('./shapes.mjs').Ctx} Ctx */
|
||||
/** @typedef {import('./users.mjs').UserRow} UserRow */
|
||||
/** A request as a handler sees it: path params, the parsed JSON body
|
||||
@@ -31,6 +37,7 @@ import { forkRun } from './fork.mjs';
|
||||
|
||||
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
||||
const MOCK = process.argv.includes('--mock');
|
||||
const DEBUG = process.argv.includes('--debug'); // identity bypass: everyone is the superuser
|
||||
|
||||
const CONFIG_PATH = path.join(ROOT, 'server.json');
|
||||
if (!fs.existsSync(CONFIG_PATH)) {
|
||||
@@ -63,7 +70,10 @@ ctx.enqueue = (runId, fn) => {
|
||||
|
||||
// --- bootstrap: the fresh-deployment superuser invite ---------------------------
|
||||
|
||||
const boot = users.bootstrap();
|
||||
if (DEBUG) {
|
||||
console.warn('debug mode: every request is the superuser -- no invites, no tokens; testing only, never a real deployment');
|
||||
}
|
||||
const boot = DEBUG ? null : users.bootstrap(); // debug skips the invite dance entirely
|
||||
if (boot?.code) {
|
||||
console.log(`fresh deployment -- the superuser invite (also at ${path.relative(ROOT, users.bootstrapFile)}):`);
|
||||
console.log(` ${boot.code}`);
|
||||
@@ -182,6 +192,18 @@ function serveStatic(pathname, res) {
|
||||
// signed-in user: the runs and packages of this deployment are shared
|
||||
// among invitees, not with the world.
|
||||
|
||||
// debug mode: the fixed identity. authenticate() returns it for every
|
||||
// request -- no token, no lookup; users.mjs is bypassed entirely, and
|
||||
// these wrappers make the id resolve as an owner through the same
|
||||
// ownership code (a user the db never heard of).
|
||||
const DEBUG_ID = 'debug';
|
||||
/** @type {UserRow} */
|
||||
const DEBUG_USER = { id: DEBUG_ID, token_hash: '', name: 'Debug', superuser: 1, created: '' };
|
||||
/** @param {?string|undefined} uid @returns {boolean} */
|
||||
const userExists = (uid) => (DEBUG && uid === DEBUG_ID) || users.exists(uid ?? null);
|
||||
/** @param {?string|undefined} uid @returns {?string} */
|
||||
const userName = (uid) => (DEBUG && uid === DEBUG_ID ? 'Debug' : users.userName(uid ?? null));
|
||||
|
||||
route('GET', '/api/session', async ({ user }) => ({
|
||||
// the public shape only -- the stored row (token hash) never leaves
|
||||
user: user ? { id: user.id, name: user.name, superuser: user.superuser === 1 } : null,
|
||||
@@ -206,7 +228,7 @@ route('GET', '/api/invites', async ({ user }) => {
|
||||
id: i.id,
|
||||
kind: i.kind,
|
||||
created_at: i.created_at,
|
||||
state: i.claimed_by == null ? 'unclaimed' : `claimed by ${users.userName(i.claimed_by) ?? i.claimed_by}`,
|
||||
state: i.claimed_by == null ? 'unclaimed' : `claimed by ${userName(i.claimed_by) ?? i.claimed_by}`,
|
||||
}));
|
||||
return { invites: list };
|
||||
});
|
||||
@@ -260,11 +282,13 @@ function requireUser(user) {
|
||||
// of who owned a run, and a dead owner id (revocation) resolves to
|
||||
// absent. The API never exposes a dangling id.
|
||||
/** @param {?string|undefined} owner @returns {?string} */
|
||||
const resolveOwner = (owner) => (owner != null && users.exists(owner) ? owner : null);
|
||||
const resolveOwner = (owner) => (owner != null && userExists(owner) ? owner : null);
|
||||
|
||||
// ownership: every GM action -- every mutation, every model call -- is
|
||||
// the owner's alone. Ownerless runs (pre-user or revoked owner) are
|
||||
// one uniform state: readable by all, forkable, never re-claimed.
|
||||
// ownership: every GM action -- every mutation, every model call --
|
||||
// and the whole working surface (table, raw session, carryover,
|
||||
// files, status, notepad) is the owner's alone; the story is the one
|
||||
// public read. Ownerless runs (pre-user or revoked owner) are one
|
||||
// uniform state: story-readable, forkable, never re-claimed.
|
||||
/**
|
||||
* @param {?UserRow} user @param {string} runId
|
||||
* @returns {asserts user is UserRow}
|
||||
@@ -277,7 +301,7 @@ function requireOwner(user, runId) {
|
||||
throw new HttpError(403, `run '${runId}' is ownerless -- fork it to make it yours`);
|
||||
}
|
||||
if (meta.owner !== user.id) {
|
||||
throw new HttpError(403, `run '${runId}' is ${users.userName(meta.owner) ?? 'another user'}'s -- fork it to continue as yours`);
|
||||
throw new HttpError(403, `run '${runId}' is ${userName(meta.owner) ?? 'another user'}'s -- fork it to continue as yours`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -296,7 +320,7 @@ route('GET', '/api/packages', async () => {
|
||||
for (const p of pkgs) {
|
||||
if (p.origin === 'user') {
|
||||
p.owner = resolveOwner(p.owner);
|
||||
p.ownerName = p.owner == null ? null : users.userName(p.owner);
|
||||
p.ownerName = p.owner == null ? null : userName(p.owner);
|
||||
}
|
||||
}
|
||||
return pkgs;
|
||||
@@ -340,7 +364,7 @@ route('GET', '/api/runs', async () => {
|
||||
r.session = s?.n ?? null;
|
||||
r.pendingAsk = s?.pendingAsk?.question ?? null;
|
||||
r.owner = resolveOwner(r.owner);
|
||||
r.ownerName = r.owner == null ? null : users.userName(r.owner);
|
||||
r.ownerName = r.owner == null ? null : userName(r.owner);
|
||||
}
|
||||
return runs;
|
||||
});
|
||||
@@ -370,9 +394,9 @@ route('GET', '/api/names', async () => {
|
||||
return { name: pool[Math.floor(Math.random() * pool.length)] };
|
||||
});
|
||||
|
||||
route('GET', '/api/runs/:id', async ({ params }) => {
|
||||
route('GET', '/api/runs/:id', async ({ params, user }) => {
|
||||
const id = params.id;
|
||||
bundle.requireRun(id);
|
||||
requireOwner(user, id); // the table's face: panes, meter, phase -- the owner's
|
||||
const st = clerkJson(id, '--json', 'report');
|
||||
const s = ctx.sessions.get(id);
|
||||
const meta = bundle.runMeta(id);
|
||||
@@ -387,7 +411,7 @@ route('GET', '/api/runs/:id', async ({ params }) => {
|
||||
created: meta?.created ?? null,
|
||||
forked: meta?.forked ?? null,
|
||||
owner,
|
||||
ownerName: owner == null ? null : users.userName(owner),
|
||||
ownerName: owner == null ? null : userName(owner),
|
||||
models: meta?.models ?? {},
|
||||
modelDefaults: ctx.cfg.models ?? {},
|
||||
},
|
||||
@@ -453,8 +477,8 @@ function fileSurface(runId) {
|
||||
return { n, phase, files: spec };
|
||||
}
|
||||
|
||||
route('GET', '/api/runs/:id/files', async ({ params }) => {
|
||||
bundle.requireRun(params.id);
|
||||
route('GET', '/api/runs/:id/files', async ({ params, user }) => {
|
||||
requireOwner(user, params.id); // the craft is working material
|
||||
const surf = fileSurface(params.id);
|
||||
/** @type {Record<string, { mode: string, exists: boolean, content?: string }>} */
|
||||
const files = {};
|
||||
@@ -539,10 +563,13 @@ route('POST', '/api/runs/:id/model', async ({ params, body, user }) => {
|
||||
return { ok: true, models: meta.models ?? {}, defaults: ctx.cfg.models ?? {} };
|
||||
});
|
||||
|
||||
// the GM's notepad: the run's gm.md -- always editable in the SPA,
|
||||
// never in any bundle, never sent to any model (no assembly path
|
||||
// reads it). Seeded from the package at creation, the GM's to keep.
|
||||
route('GET', '/api/runs/:id/notes', async ({ params }) => {
|
||||
// the GM's notepad: the run's gm.md -- the owner's alone (#28's
|
||||
// human-side open read overturned 2026-11-15), never in any bundle,
|
||||
// never sent to any model (no assembly path reads it -- the model
|
||||
// boundary stands). Seeded from the package at creation, the GM's to
|
||||
// keep.
|
||||
route('GET', '/api/runs/:id/notes', async ({ params, user }) => {
|
||||
requireOwner(user, params.id);
|
||||
const dir = bundle.requireRun(params.id);
|
||||
const file = path.join(dir, 'gm.md');
|
||||
return { content: fs.existsSync(file) ? fs.readFileSync(file, 'utf8') : '' };
|
||||
@@ -550,7 +577,7 @@ route('GET', '/api/runs/:id/notes', async ({ params }) => {
|
||||
|
||||
route('PUT', '/api/runs/:id/notes', async ({ params, body, user }) => {
|
||||
const dir = bundle.requireRun(params.id);
|
||||
requireOwner(user, params.id); // spectators read the pad, the owner writes it
|
||||
requireOwner(user, params.id); // the pad is owner-only in both directions
|
||||
if (typeof body.content !== 'string') throw new HttpError(400, 'content (a string) is required -- empty clears the pad');
|
||||
fs.writeFileSync(path.join(dir, 'gm.md'), body.content);
|
||||
return { ok: true };
|
||||
@@ -591,7 +618,8 @@ route('POST', '/api/runs/:id/sessions', async ({ params, body, user }) => {
|
||||
});
|
||||
});
|
||||
|
||||
route('GET', '/api/runs/:id/session', async ({ params }) => {
|
||||
route('GET', '/api/runs/:id/session', async ({ params, user }) => {
|
||||
requireOwner(user, params.id); // the raw audit surface
|
||||
const s = ctx.sessions.get(params.id);
|
||||
if (!s) throw new HttpError(404, `no live session for '${params.id}'`);
|
||||
return {
|
||||
@@ -607,8 +635,8 @@ route('GET', '/api/runs/:id/session', async ({ params }) => {
|
||||
// read-only -- the review window's preview of the next contwin. Same
|
||||
// projection as the table (the picked variant only, thinking stays in
|
||||
// the raw log), plus the ending that decides the opening flow.
|
||||
route('GET', '/api/runs/:id/carryover', async ({ params }) => {
|
||||
bundle.requireRun(params.id);
|
||||
route('GET', '/api/runs/:id/carryover', async ({ params, user }) => {
|
||||
requireOwner(user, params.id); // the table's dormant stand-in
|
||||
const live = ctx.sessions.get(params.id);
|
||||
if (live) throw new HttpError(409, `session ${live.n} is live -- the table is the surface`);
|
||||
const latest = session.latestSession(params.id);
|
||||
@@ -652,9 +680,11 @@ route('GET', '/api/runs/:id/carryover', async ({ params }) => {
|
||||
// what the GM needs at the table. Same envelope as /session,
|
||||
// different policy over the same log: thinking never crosses the
|
||||
// wire, variant turns carry only the picked variant. The raw
|
||||
// /session stays whole as the audit surface; during dormancy the
|
||||
// /session stays whole as the audit surface (the table's raw tab
|
||||
// in the SPA); during dormancy the
|
||||
// /carryover endpoint is the table's stand-in.
|
||||
route('GET', '/api/runs/:id/table', async ({ params }) => {
|
||||
route('GET', '/api/runs/:id/table', async ({ params, user }) => {
|
||||
requireOwner(user, params.id); // the table is the owner's working surface
|
||||
const s = ctx.sessions.get(params.id);
|
||||
if (!s) throw new HttpError(404, `no live session for '${params.id}'`);
|
||||
const eff = effectiveContents(s.lines);
|
||||
@@ -692,7 +722,9 @@ route('GET', '/api/runs/:id/table', async ({ params }) => {
|
||||
// omission. A dormant session's carryover is the story's live edge:
|
||||
// its raw lines project like any log's, at the chronological end --
|
||||
// GM edits during dormancy show, because the carryover is the current
|
||||
// truth of where the story stands.
|
||||
// truth of where the story stands. The public reading surface: any
|
||||
// signed-in user; the table and everything else in the run is the
|
||||
// owner's.
|
||||
route('GET', '/api/runs/:id/story', async ({ params }) => {
|
||||
bundle.requireRun(params.id);
|
||||
const rides = /<(status|agenda|conflict)>[\s\S]*?<\/\1>/g;
|
||||
@@ -741,7 +773,12 @@ route('GET', '/api/runs/:id/story', async ({ params }) => {
|
||||
}
|
||||
if (turns.length) sessions.push({ session: n, turns });
|
||||
}
|
||||
return { run: params.id, sessions };
|
||||
// the public face rides along: what the run is called and whose it
|
||||
// is -- the story view stands alone (the detail it used to lean on
|
||||
// is owner-only now)
|
||||
const meta = bundle.runMeta(params.id);
|
||||
const owner = resolveOwner(meta?.owner);
|
||||
return { run: params.id, display: bundle.runDisplay(params.id), owner, ownerName: owner == null ? null : userName(owner), sessions };
|
||||
});
|
||||
|
||||
route('POST', '/api/runs/:id/turn', async ({ params, body, user }) => {
|
||||
@@ -768,8 +805,8 @@ route('POST', '/api/runs/:id/fork', async ({ params, user }) => {
|
||||
return ctx.enqueue(params.id, async () => forkRun(ctx, params.id, u));
|
||||
});
|
||||
|
||||
route('GET', '/api/runs/:id/status', async ({ params }) => {
|
||||
bundle.requireRun(params.id);
|
||||
route('GET', '/api/runs/:id/status', async ({ params, user }) => {
|
||||
requireOwner(user, params.id); // status and agenda are machinery
|
||||
return { status: clerkJson(params.id, 'status'), agenda: clerkJson(params.id, 'agenda') };
|
||||
});
|
||||
|
||||
@@ -858,9 +895,15 @@ route('POST', '/api/runs/:id/housekeeping/replay', async ({ params, body, user }
|
||||
return { ok: true, parsed: true, session: n, file: `manifests/${n}.replay.json`, manifest: mf };
|
||||
});
|
||||
|
||||
route('GET', '/api/runs/:id/housekeeping', async ({ params }) => housekeeping.jobState(ctx, params.id));
|
||||
route('GET', '/api/runs/:id/housekeeping', async ({ params, user }) => {
|
||||
requireOwner(user, params.id); // the job state is machinery
|
||||
return housekeeping.jobState(ctx, params.id);
|
||||
});
|
||||
|
||||
route('GET', '/api/runs/:id/housekeeping/log', async ({ params }) => housekeeping.latestLog(params.id));
|
||||
route('GET', '/api/runs/:id/housekeeping/log', async ({ params, user }) => {
|
||||
requireOwner(user, params.id); // the apply log is machinery
|
||||
return housekeeping.latestLog(params.id);
|
||||
});
|
||||
|
||||
route('POST', '/api/runs/:id/housekeeping/apply', async ({ params, body, user }) => {
|
||||
// the recovery path: apply a (hand-edited) manifest directly -- the
|
||||
@@ -892,6 +935,7 @@ route('POST', '/api/runs/:id/housekeeping/apply', async ({ params, body, user })
|
||||
// bearer-token identity (users.mjs); null when absent or unknown
|
||||
/** @param {import('node:http').IncomingMessage} req @returns {?UserRow} */
|
||||
function authenticate(req) {
|
||||
if (DEBUG) return DEBUG_USER; // debug: everyone is the superuser
|
||||
const m = /^Bearer\s+(.+)$/i.exec(req.headers.authorization ?? '');
|
||||
return m ? users.getUserByToken(m[1].trim()) : null;
|
||||
}
|
||||
@@ -928,7 +972,10 @@ const server = http.createServer(async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
const [host, port] = String(cfg.bind ?? '127.0.0.1:7350').split(':');
|
||||
// debug never leaves this machine: whatever the config binds, the
|
||||
// debug host is loopback (the port stays configurable)
|
||||
const [cfgHost, port] = String(cfg.bind ?? '127.0.0.1:7350').split(':');
|
||||
const host = DEBUG ? '127.0.0.1' : cfgHost;
|
||||
server.listen(Number(port ?? 7350), host, () => {
|
||||
console.log(`Tiny Worlds server: http://${host}:${port} | ${MOCK ? 'MOCK model' : cfg.models?.player} | ${ctx.sessions.size} session(s) rebuilt`);
|
||||
console.log(`Tiny Worlds server: http://${host}:${port} | ${MOCK ? 'MOCK model' : cfg.models?.player} | ${ctx.sessions.size} session(s) rebuilt${DEBUG ? ' | DEBUG: auth bypassed' : ''}`);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user