the debug round: server.json deleted, the bind hardcoded to loopback (RP_PORT for the port), the debug superuser token-gated behind a printed, persisted token, and the context meter reading the discovered per-model contwin
The rulings (developer, 2026-11-19, in conversation): a deployment basically always needs a fronting proxy, so the listen address is code -- loopback, no override (an op who wants otherwise owns their own socat); the port rides the RP_PORT env var (the RP_BASE twin the test scripts use, default 7350, garbage refused loud); server.json is deleted entirely, file and reader both, because it held no meaningful configuration once the bind died -- only the dead bind and the declared contwin; and contwin is provider/model dependent, never a deployment global, so the meter must DISCOVER it, not read a declaration.
The meter: the run detail's context block now derives its window from the registry -- modelState's resolved player spec, findModel, the model's input ceiling -- null when the provider states none or no pick stands, pct computed against that. A dividend named in review: pct is uncapped, so a mid-run re-pick onto a smaller model shows the meter over 100% BEFORE the first send refuses (the endpoint's own context-length 400 is the designed refusal -- no pre-request check, nothing truncates, the failed call lands its req event, the composer keeps the line per the standing rule).
Debug mode reworked around its real purpose (skip the invite dance on a fresh server, mocks in particular): the old shape -- every request the superuser, no auth at all -- left any debug server that somehow reached a network fully open. Now one fixed superuser behind a real bearer token: minted into state/debug-token.txt (the bootstrap-invite precedent -- a raw secret on disk so a restart never invalidates what the tester already pasted; the developer ruled persist), printed at every boot, and the claim screen gains the paste path ("or paste a token": store it, verify through the public /session, land or banner). The paste path is generic surface, not debug-only: tokens are returned once and never listed, so a pasted saved token is the only browser-migration path that existed to build. The public /session carries a debug flag so the claim hint says "paste the token the server printed at boot" instead of pointing at the bootstrap file debug never mints. Debug browser passes now exercise the real auth path (the 401, the u: attribution line) instead of bypassing it.
En-route mock fidelity fix the smoke earned: the mock path never set s.usage (no provider call happens), so every usage and context surface was null under the smoke -- the first cut of the new gauge assertion failed on exactly that. The mock now carries a synthetic usage (the assembled view's chars over 4), riding the msg events like the real path's, so the detail's usage and the context gauge are smoke-assertable: contwin 524288 straight from the mock's registry input ceiling, pct computed.
Verified: typecheck green both programs; smoke ALL PASS (+1, the discovered-gauge assertion); restart + reminder checks green (5 runs again); a browser pass on --mock --debug end to end -- the wrong token banners "token rejected" and stays on claim, the printed token lands as Debug the op, the stored token survives a server restart (the persistence proof), zero console errors; check-repo clean (it caught the round's own "window" in the new README passage -- contwin is the word); the dev machine's server.json deleted and the debug server restarted on the real state (Chloe and Irene rebuilt, the token minted into the real state/). Docs: README's deployment and debug passages, the config passage, and the layout tree (the server.example.json lines gone with the file); DICTIONARY's meter and contwin entries (the config key died with the file -- the dated rename record keeps its day's words); AGENTS's server entry and the two server.json gotchas; HANDOFF's round record and the push-queue count (now eight). The developer's operator-queue line ("revisit the debug mode, hardcode loopback") is struck by them in the same commit. packages/detention stays untracked WIP, untouched.
This commit is contained in:
+50
-37
@@ -8,10 +8,11 @@
|
||||
// session back from the jsonl -- the log is the truth, the context is a
|
||||
// view.
|
||||
//
|
||||
// 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.
|
||||
// Debug mode (--debug): one fixed superuser ("debug") that bypasses
|
||||
// the user database entirely -- no invite dance. The bearer token
|
||||
// that signs it in is minted into state/debug-token.txt and printed
|
||||
// at every boot. Loopback-bound by code, debug or not. Testing only,
|
||||
// never a real deployment.
|
||||
//
|
||||
// usage: node server/main.mjs [--mock] [--debug]
|
||||
/** @typedef {import('./shapes.mjs').Ctx} Ctx */
|
||||
@@ -41,24 +42,14 @@ 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
|
||||
|
||||
// server.json is the static, non-secret remainder: bind and contwin.
|
||||
// Optional (developer, 2026-11-18) -- the provider entities live in
|
||||
// state/config.json (server-written, SPA-managed). Nothing here gates
|
||||
// the boot: a fresh deployment with no config at all comes up
|
||||
// serving, mints the superuser invite, and shows the global
|
||||
// no-providers banner -- every consumer of providers treats "none"
|
||||
// as a loud 409, never a crash.
|
||||
/** @type {import('./shapes.mjs').ServerCfg} */
|
||||
let cfg = {};
|
||||
const SERVER_JSON = path.join(ROOT, 'server.json');
|
||||
if (fs.existsSync(SERVER_JSON)) {
|
||||
try {
|
||||
cfg = JSON.parse(fs.readFileSync(SERVER_JSON, 'utf8'));
|
||||
} catch {
|
||||
console.error('server.json does not parse -- fix it or remove it (the provider entities live in state/config.json)');
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
// There is no server.json (developer, 2026-11-19: it held no
|
||||
// meaningful configuration once the bind died) -- the bind is code
|
||||
// (loopback always) and the port rides the RP_PORT env var. The
|
||||
// provider entities live in state/config.json (server-written,
|
||||
// SPA-managed). Nothing gates the boot: a fresh deployment with no
|
||||
// config at all comes up serving, mints the superuser invite, and
|
||||
// shows the global no-providers banner -- every consumer of
|
||||
// providers treats "none" as a loud 409, never a crash.
|
||||
let providerCfg = providers.readConfig();
|
||||
// the self-heal rule (developer, 2026-11-18), stated broadly: an error that
|
||||
// can be mechanically fixed to a reasonable degree without requiring
|
||||
@@ -86,7 +77,7 @@ if (!providerCfg.providers.length) {
|
||||
const providersMap = new Map(providerCfg.providers.map((p) => [p.slug, makeProvider(p)]));
|
||||
// locks/enqueue are attached just below; the cast is the two-step that
|
||||
// lets the literal precede them
|
||||
const ctxBase = { mock: MOCK, cfg, providerCfg, providers: providersMap, sessions: new Map(), jobs: new Map(), runMeta: bundle.runMeta };
|
||||
const ctxBase = { mock: MOCK, providerCfg, providers: providersMap, sessions: new Map(), jobs: new Map(), runMeta: bundle.runMeta };
|
||||
/** @type {Ctx} */
|
||||
const ctx = /** @type {Ctx} */ (ctxBase);
|
||||
|
||||
@@ -119,7 +110,7 @@ ctx.enqueue = (runId, fn) => {
|
||||
// --- bootstrap: the fresh-deployment superuser invite ---------------------------
|
||||
|
||||
if (DEBUG) {
|
||||
console.warn('debug mode: every request is the superuser -- no invites, no tokens; testing only, never a real deployment');
|
||||
console.warn('debug mode: one fixed superuser behind the printed token -- no invites, no users db; testing only, never a real deployment');
|
||||
}
|
||||
const boot = DEBUG ? null : users.bootstrap(); // debug skips the invite dance entirely
|
||||
if (boot?.code) {
|
||||
@@ -256,6 +247,7 @@ 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,
|
||||
fresh: users.userCount() === 0,
|
||||
debug: DEBUG, // the claim screen's hint: no invite exists here
|
||||
}), { public: true });
|
||||
|
||||
route('POST', '/api/claim', async ({ body }) => {
|
||||
@@ -762,6 +754,7 @@ route('GET', '/api/runs/:id', async ({ params, user }) => {
|
||||
const latest = session.latestSession(id);
|
||||
const sm = latest == null ? {} : session.sessionMeta(id, latest);
|
||||
const phase = s ? 'live' : (sm.phase ?? null);
|
||||
const rh = roleHealth(id);
|
||||
return {
|
||||
run: {
|
||||
id,
|
||||
@@ -773,7 +766,7 @@ route('GET', '/api/runs/:id', async ({ params, user }) => {
|
||||
models: meta?.models ?? {},
|
||||
// the invalidation surface: each resolved spec's health, so the
|
||||
// table can carry the note before a send ever 409s
|
||||
modelState: roleHealth(id),
|
||||
modelState: rh,
|
||||
// the providers' default specs -- the create flow's pre-select,
|
||||
// and the fallback a pick-less role would inherit
|
||||
providerDefaults: providerDefaults(),
|
||||
@@ -793,11 +786,16 @@ route('GET', '/api/runs/:id', async ({ params, user }) => {
|
||||
events: st.events,
|
||||
pendingAsk: s?.pendingAsk?.question ?? null,
|
||||
usage: s?.usage ?? null,
|
||||
context: s?.usage ? {
|
||||
used: s.usage.promptTokens,
|
||||
contwin: cfg.contwin ?? null,
|
||||
pct: cfg.contwin ? Math.round((100 * s.usage.promptTokens) / cfg.contwin) : null,
|
||||
} : null,
|
||||
// the discovered window (developer, 2026-11-19): contwin is
|
||||
// model-dependent, never deployment-declared -- the run's player
|
||||
// model's input ceiling from the registry, null when the provider
|
||||
// states none or no pick stands
|
||||
context: (() => {
|
||||
if (!s?.usage) return null;
|
||||
const spec = rh.player.spec;
|
||||
const win = spec ? discovery.findModel(spec.provider, spec.model)?.input ?? null : null;
|
||||
return { used: s.usage.promptTokens, contwin: win, pct: win ? Math.round((100 * s.usage.promptTokens) / win) : null };
|
||||
})(),
|
||||
};
|
||||
});
|
||||
|
||||
@@ -1418,8 +1416,12 @@ 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 ?? '');
|
||||
if (DEBUG) {
|
||||
// debug: the one fixed superuser, token-gated -- the token lives
|
||||
// in state/debug-token.txt and is printed at every boot
|
||||
return m && m[1].trim() === users.debugToken() ? DEBUG_USER : null;
|
||||
}
|
||||
return m ? users.getUserByToken(m[1].trim()) : null;
|
||||
}
|
||||
|
||||
@@ -1455,12 +1457,23 @@ const server = http.createServer(async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// 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, () => {
|
||||
// the bind is code, not config (developer, 2026-11-19): loopback
|
||||
// always -- a deployment fronts the server with a proxy, and anything
|
||||
// else is the op's socat. The port rides RP_PORT (the RP_BASE twin
|
||||
// the test scripts use), default 7350, garbage refused loud.
|
||||
const PORT = (() => {
|
||||
const raw = process.env.RP_PORT;
|
||||
if (raw == null || raw === '') return 7350;
|
||||
const n = Number(raw);
|
||||
if (!Number.isInteger(n) || n < 1 || n > 65535) {
|
||||
console.error(`RP_PORT '${raw}' is not a port -- fix or unset it`);
|
||||
process.exit(1);
|
||||
}
|
||||
return n;
|
||||
})();
|
||||
server.listen(PORT, '127.0.0.1', () => {
|
||||
const providerCount = ctx.providerCfg.providers.length;
|
||||
const discovered = Object.keys(discovery.registry()?.providers ?? {}).length;
|
||||
console.log(`Tiny Worlds server: http://${host}:${port} | ${MOCK ? 'MOCK model' : `${providerCount} provider(s), ${discovered} discovered`} | ${ctx.sessions.size} session(s) rebuilt${DEBUG ? ' | DEBUG: auth bypassed' : ''}`);
|
||||
console.log(`Tiny Worlds server: http://127.0.0.1:${PORT} | ${MOCK ? 'MOCK model' : `${providerCount} provider(s), ${discovered} discovered`} | ${ctx.sessions.size} session(s) rebuilt${DEBUG ? ' | DEBUG' : ''}`);
|
||||
if (DEBUG) console.log(`debug token (paste it into the sign-in box): ${users.debugToken()}`);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user