Server-run play, part 2: the engine

The engine that runs every network of a server session (server-sim design,
section 4): each network in its own vm realm, realms in a small pool of
worker threads, a promise API for the session host. Nothing uses it yet;
the plain build and the game are unchanged (no file under src/).

- scripts/lib/mp-sim-realm.js: one network in a DONT_CONTEXTIFY realm
  running the plain page's game (never the MP page's client folder), with
  recording DOM stand-ins, no-op timers that count, and the srv... calls:
  load from a checkpoint (route memory seeded, then routed), begin a new
  network, marks and restores, the day as a page runs it, the action
  pipeline with drafts, arming, the sanitize gate and rollback, what-ifs
  that put S, R, the dice and uidN back, pulls with top-level key diffs,
  slim R and flow records on demand, fx captured and bounded, saves, cards.
- scripts/lib/mp-sim-worker.js: realms per session in one worker, days one
  network at a time with other messages between them, loads and unloads
  waiting for a session's day, time limits; a throw goes back to the mark
  in place, a timeout gets a fresh realm (new epoch) carrying the old R, a
  day is retried once and the network broken after a second failure.
- scripts/lib/mp-sim-engine.js: createEngine() with the fake engine's API,
  placement by realm count, every pending call rejected when its worker
  exits, a worker of its own after two exits in an hour; snapWith() joins
  the host's clock, roster and since to a realm's snapshot.
- Contract: a pulled snapshot is the realm's half (the host adds clock,
  roster, since); haptic fx carry milliseconds; the fake engine follows,
  with close() and snapWith(). The button crawl moved to tests/lib.
- Tests: realm vs jsdom byte for byte over 60 scenic big-network days with
  actions; restore; retry after a throw or a timeout; realm order; a timer
  audit and the gate over a sweep of sent buttons (SIM_LONG=1: a year and
  every button); captures, rollbacks, asks, selection reports, ordering,
  bounds, pollution, worker exits; the act tables against the game.

The gate found two keys the game writes in a shape a reload repairs (buyTE's
S.te, mkLeave's S.monkey): game fixes for the integrator, kept visible as a
todo test and a list in the audit.

Rebased onto the 3.3.0 merge (61e31c5): the engine itself needed no change. A
realm still routes in the game's own code (no route worker, no saves), the
route cache that now keeps list orders from day to day leaves a checkpoint and a
retry the same, and the capacity market and moved customers play in a realm as
on a page, under the strict gate. The tests needed three things: an old save's
first days earn the new traffic badges, which the unwatched network's snapshot
keeps; the restore test compares JSON text with sorted keys instead of a
deep-equal of two networks (a diff of gigabytes when they differ); and the crawl
opens the windows that move a customer, with a new test playing the market's and
the path moves' acts, 40 days of sales and a checkpoint against the page.
This commit is contained in:
jvanvik 2026-11-07 11:21:23 +01:00
parent 61e31c53d3
commit 065c922287
12 changed files with 1906 additions and 29 deletions

28
tests/lib/mp-sim-crawl.js Normal file
View file

@ -0,0 +1,28 @@
// Every button and drop-down a rich game shows, as the page would send it: [act, args]. Runs in a jsdom page of the
// game (tests/harness.js load()) on whatever network it holds: city panels and their tabs, link panels, the left
// lists, every dashboard tab, the bank, the sheets the big buttons open, the link window, the windows that move a
// customer to another path, and armed two-click buttons.
// Used by the envelope check (tests/mp-sim-contract.test.js) and the engine's timer audit and gate
// (tests/mp-sim-determinism.test.js), which send them to a server realm.
'use strict';
function crawl(ev) {
ev(`window.__btns=[];window.__grab=()=>{for(const el of document.querySelectorAll('[data-act],[data-chg]')){const act=el.dataset.act||el.dataset.chg;let a={};try{a=el.dataset.args?JSON.parse(el.dataset.args):{};}catch(e){a={bad:el.dataset.args};}
if(el.dataset.chg){a={...a,v:el.value}}__btns.push([act,a]);}};`);
const views = [
"for(const c of ['fra','ams','nyc','sin','syd']){for(const tab of ['cust','up','peer','infra','links']){UI.sel={type:'city',id:c};UI.tab=tab;renderPanel();__grab();}}",
"for(const l of S.links.slice(0,40)){UI.sel={type:'link',id:l.id};renderPanel();__grab();}",
"UI.leftOpen=true;for(const k of ['goals','deals','noc','nets']){UI.left=k;renderLeft();__grab();}",
"$('dash').hidden=false;for(const k of DTABS){UI.dtab=k;renderDash();__grab();}$('dash').hidden=true;",
'ACT.bank();__grab();ACT.bankClose();',
"for(const [a,x] of [['opsOpen',{}],['utilities',{city:'fra'}],['bstdOpen',{}],['pniAllOpen',{net:null}],['pniAllOpen',{net:'cog'}],['salesOpen',{}],['routingOpen',{}],['ip4Open',{}],['models',{}],['decomOpen',{}],['teEdit',{}]]){try{ACT[a](x);}catch(e){}__grab();closeModal();}",
"openLinkModal('fra','nyc');__grab();closeModal();openLinkModal('fra','ams');__grab();closeModal();",
// Moving a customer to another path (3.3.0): its buttons are only in that window.
'for(const b of S.biz.filter(b=>pathKind(b)).slice(0,6)){ACT.pathOpen({id:b.id});__grab();closeModal();}',
"UI.confirm=null;for(const t of S.transit.slice(0,3)){UI.sel={type:'city',id:t.city};UI.tab='up';UI.confirm=t.id;renderPanel();__grab();}UI.confirm=null;",
];
for (const v of views) ev(v);
return JSON.parse(ev('JSON.stringify(__btns)'));
}
module.exports = { crawl };

View file

@ -10,11 +10,14 @@
// pause?, err?, retried?, ms}}}
// engine.act(code, nid, msg) → {t:'actDone', nid, epoch, rid, outcome, why?, v, upd} (msg: the request body, 5.8)
// engine.ask(code, nid, msg) → {t:'askDone', nid, rid, modal:{h, c}, ui?}
// engine.pull(code, nid, {snap, views}) → {t:'pulled', nid, epoch, v, upd?, snap?, subs?} (views: [{link} | {peer}])
// engine.pull(code, nid, {snap, views}) → {t:'pulled', nid, epoch, v, upd?, snap?, subs?} (views: [{link} | {peer}]);
// snap is the realm's half (t, epoch, v, now, S, r, keep): engine.snapWith(snap, {clock,
// roster, since}) makes the stream event
// engine.sub(code, nid, {views}) → {t:'subbed', nid, epoch, v, subs}
// engine.prefs(code, nid, {prefs}) → undefined; engine.greet(code, nid) → a pulled
// engine.save(code, nid) → {t:'saved', nid, txt, uidN, rt, day, lost}; engine.card(code, nid) → {t:'card', nid, card}
// engine.unload(code, nid?) → {t:'unloaded', code, nid?}; engine.stats() → {workers, realms, sessions} (not a promise)
// engine.unload(code, nid?) → {t:'unloaded', code, nid?}; engine.stats() → {workers, realms, sessions} (not a promise);
// engine.close() → stops it (every pending call rejects)
//
// For the tests of the host: engine.calls (every call, [method, ...args]); engine.outcome = (msg) => outcome to answer
// acts with ('applied' unless set); engine.lose(code, nid) / engine.pause(code, nid) (the next day ends bankrupt / asks
@ -31,6 +34,7 @@
// (retried:true, no err).
'use strict';
const { fixture } = require('./mp-sim-shape');
const { snapWith } = require('../../scripts/lib/mp-sim-engine');
function createFakeEngine(opts = {}) {
const F = { snap: fixture('snap'), day: fixture('upd-day'), act: fixture('upd-act'), sub: fixture('sub'), roster: fixture('roster') };
@ -69,9 +73,10 @@ function createFakeEngine(opts = {}) {
const s = { ...base.s, day: r.day };
return JSON.stringify({ ...base, from, v, now: Date.now(), day: r.day, s, ...extra });
};
// The realm's half of a snapshot: the host adds clock, roster and since with snapWith().
const snapOf = (code, nid, r) => {
const S = { ...F.snap.S, day: r.day, lost: r.lost, mp: { ...F.snap.S.mp, code, pid: nid } };
return JSON.stringify({ ...F.snap, epoch: r.epoch, v: r.v, now: Date.now(), S, keep: r.keep.splice(0) });
return JSON.stringify({ t: 'snap', epoch: r.epoch, v: r.v, now: Date.now(), S, r: F.snap.r, keep: r.keep.splice(0) });
};
const subsOf = (views = []) => {
const o = {};
@ -165,6 +170,8 @@ function createFakeEngine(opts = {}) {
return answer('unload', () => { for (const k of [...realms.keys()]) if (k.startsWith(code + '/') && (nid === undefined || k === key(code, nid))) realms.delete(k); return { t: 'unloaded', code, ...(nid ? { nid } : {}) }; }, code, nid);
},
stats() { return { workers: 1, realms: realms.size, sessions: new Set([...realms.keys()].map((k) => k.split('/')[0])).size }; },
close() { engine.exit(); return Promise.resolve(); },
snapWith,
// Test controls.
lose(code, nid) { realm(code, nid).loseNext = true; },
pause(code, nid) { realm(code, nid).pause = true; },

85
tests/lib/mp-sim-play.js Normal file
View file

@ -0,0 +1,85 @@
// What the engine's tests share (tests/mp-sim-engine.test.js, mp-sim-determinism.test.js, mp-sim-acts.test.js): the
// networks they play, the requests a page sends, and the page's side of a comparison, run in jsdom by the click path.
//
// page({save}) a jsdom page holding a network as the server's realm holds it (pinned clock, seeded): {ev, …}
// bigScenic() the big fixture as a server network (session marks, dice, its L2 customers on scenic routes),
// as live S text; the route-memory problem only shows with scenic circuits (design 4.7, 11)
// msg(act, args, more) an act or ask request as a page sends it (envelope-checked, ctx clamped)
// ctxOf(ev) the page's ctx: its selection, tab and left list, a desktop viewport
// begin(nid, o) a new network's begin entry for engine.load()
// RSIG R with its lazy parts read out, attacks by id (as tests/route-worker.test.js reads it)
// checked(r) r, after checkWorker (and checkEvent of the upd/snap inside) found nothing
// differs(a, b), canon(s) the first differing top-level keys of two JSON texts; a JSON text with its keys sorted
'use strict';
const fs = require('node:fs');
const path = require('node:path');
const assert = require('node:assert/strict');
const { load } = require('../harness');
const { lit } = require('../../scripts/lib/js-literal');
const { simId } = require('../../scripts/lib/page');
const A = require('../../scripts/lib/mp-sim-acts');
const { checkWorker, checkEvent } = require('./mp-sim-shape');
const CODE = 'K7Q2XD', PID = 'abcd1234', DEV = '9f8e7d6c', T0 = 1.7e12, STEP = 2500;
const MP = { code: CODE, pid: PID, asn: 201234, seed: 424242, preset: 'oncall', color: '#3a86ff', title: 'Friday' };
const TEST = { seed: 4242, now: T0, step: STEP };
// Time limits for engines whose tests inject no timeout: a busy machine (every test file at once) must not time out a
// big network's day or action that takes 40 ms when it is quiet. The tests of timeouts use the engine's own limits.
const ROOMY = { day: 20000, act: 20000, ask: 20000, pull: 20000, save: 20000, mark: 20000, small: 20000 };
let SIM;
const sim = () => SIM || (SIM = simId());
const BIG = path.join(__dirname, '..', 'fixtures', 'big-network.json');
// A jsdom page whose clock and dice are the realm's (TEST): a day there is __t+=STEP; dayTick(); and the day's end render.
function page({ save, uidN = 1000 } = {}) {
const h = load({ seed: TEST.seed });
h.ev(`window.__t=${T0};Date.now=()=>__t;UI.started=true;UI.pitched=true;UI.speed=0;closeModal();`);
if (save) h.ev(`S=migrate(JSON.parse(${lit(save)}));uidN=${uidN};R=emptyR();compute();`);
h.day = () => h.ev('__t+=' + STEP + ';dayTick();UI.ticking=true;renderHUD();UI.ticking=false;');
h.close = () => h.dom.window.close();
return h;
}
let BIGS = null;
function bigScenic() {
if (BIGS) return BIGS;
const h = load({ seed: TEST.seed });
h.ev(`S=migrate(JSON.parse(${lit(fs.readFileSync(BIG, 'utf8'))}));S.rng=${MP.seed};S.mp={...${JSON.stringify(MP)},seq:0,sim:1};
for(const b of S.biz)if(BIZ_T[b.type].l2)b.route='scenic';`);
BIGS = h.ev('JSON.stringify(migrate(JSON.parse(JSON.stringify(S))))');
h.dom.window.close();
return BIGS;
}
let n = 0;
function msg(act, args = {}, more = {}) {
const body = JSON.parse(JSON.stringify({ rid: 'r' + (++n).toString(36).padStart(8, '0'), dev: DEV, sim: sim(), act, args, ...more }));
const r = A.checkEnvelope(A.ASK.includes(act) ? 'ask' : 'act', body, { sim: sim() });
assert.ok(r.ok, `${act}: the page would not send this (${r.error})`);
return body;
}
const ctxOf = (ev, o = {}) => ({ sel: JSON.parse(ev('JSON.stringify(UI.sel)')), tab: ev('UI.tab'), left: ev('UI.left'), modal: false, w: 1024, h: 768, coarse: false, ...o });
const begin = (nid, o = {}) => ({ nid, begin: { name: 'Packet Pals', home: 'fra', preset: 'oncall', asn: MP.asn, mp: { ...MP, pid: nid }, tut: false, ...o }, lang: 'en', prefs: {} });
const RSIG = "(()=>{const o={};for(const k in R)o[k]=R[k];o.ef={};for(const k in R.ef)o.ef[k]=R.ef[k];o.fl=(R.fl||[]).map(f=>[f.c,f.n,f.junk,f.rem,f.gt,f.gi,f.gp,f.gh,f.ti,f.ii]);return JSON.stringify(o,(k,v)=>k==='attack'?(v&&v.id):k==='ports'?undefined:v);})()";
function checked(r) {
const p = checkWorker(r);
assert.deepEqual(p, [], `${r && r.t}: ${p.join('; ')}`);
if (r && typeof r.upd === 'string') assert.deepEqual(checkEvent(JSON.parse(r.upd)), [], 'the update');
return r;
}
// The first top-level keys whose JSON differs, with a glimpse of each side: for a failing comparison's message.
function differs(a, b) {
const A2 = JSON.parse(a), B2 = JSON.parse(b);
return Object.keys({ ...A2, ...B2 }).filter((k) => JSON.stringify(A2[k]) !== JSON.stringify(B2[k]))
.slice(0, 4).map((k) => `${k}: ${String(JSON.stringify(A2[k])).slice(0, 300)} ≠ ${String(JSON.stringify(B2[k])).slice(0, 300)}`).join('\n');
}
// JSON text with every object's keys sorted (arrays keep their order): two networks that differ only in key order are
// the same text. Compare these strings, never the parsed objects with a deep-equal.
const sortK = (v) => (Array.isArray(v) ? v.map(sortK) : v && typeof v === 'object' ? Object.keys(v).sort().reduce((o, k) => ((o[k] = sortK(v[k])), o), {}) : v);
const canon = (s) => JSON.stringify(sortK(JSON.parse(s)));
module.exports = { CODE, PID, DEV, MP, T0, STEP, TEST, ROOMY, sim, page, bigScenic, msg, ctxOf, begin, RSIG, checked, differs, canon };

View file

@ -83,7 +83,9 @@ function checkFx(f, w = 'fx', out = []) {
const bad = () => out.push(`${w}: bad ${k} payload ${JSON.stringify(p).slice(0, 80)}`);
switch (k) {
case 'toast': fields(p, { h: str, k: [str, 'opt'], a: [listOf((x) => obj(x) && str(x.l) && str(x.act)), 'opt'], o: [(v) => v === null || obj(v), 'opt'], tick: [bool, 'opt'] }, `${w}.toast`, out); break;
case 'sfx': case 'haptic': if (!str(p)) bad(); break;
case 'sfx': if (!str(p)) bad(); break;
// The game's haptic(ms) takes how long to buzz.
case 'haptic': if (!(num(p) && p >= 0)) bad(); break;
case 'floater': if (!Array.isArray(p) || p.length !== 4 || !num(p[0]) || !num(p[1]) || !str(p[2]) || !str(p[3])) bad(); break;
case 'track': if (!Array.isArray(p) || p.length !== 2 || !str(p[0]) || !(p[1] === undefined || p[1] === null || bool(p[1]))) bad(); break;
case 'pause': if (p !== 1) bad(); break;
@ -111,14 +113,18 @@ const ROSTER = { t: (v) => v === 'roster', code: (v) => CODE.test(v), title: str
feed: listOf((f, w, out) => { fields(f, { k: str, pid: str, name: str, at: nat, text: [str, 'opt'] }, w, out); return true; }),
on: listOf((v) => PID.test(v)) };
/* A snapshot is made in two halves: the realm's (the network: S, r, keep, at its epoch and version) and the host's
(clock, roster, since: the session and this stream). A worker's pulled.snap is the realm's half as text; the engine's
snapWith() joins the host's to it (scripts/lib/mp-sim-engine.js). */
const SNAP_REALM = { t: str, epoch: (v) => EPOCH.test(v), v: nat, now: nat, S: liveS, r: slimR,
keep: (v, ww, o) => { if (!Array.isArray(v) || v.length > 32) return false; v.forEach((f, i) => { checkFx(f, `${ww}[${i}]`, o); if (f[0] !== 'badge' && f[0] !== 'modal') o.push(`${ww}[${i}]: keep holds only badge and modal`); }); return true; } };
const SNAP_HOST = { clock: (v, ww, o) => { fields(v, CLOCK, ww, o); return true; }, roster: (v, ww, o) => { fields(v, ROSTER, ww, o); return true; },
since: (v, ww, o) => { fields(v, { days: nat, again: nat }, ww, o); return true; } };
function checkEvent(e) {
const out = [];
if (!obj(e) || !str(e.t)) return ['an event is an object with t'];
const w = e.t;
if (e.t === 'snap') fields(e, { t: str, epoch: (v) => EPOCH.test(v), v: nat, now: nat, S: liveS, r: slimR,
clock: (v, ww, o) => { fields(v, CLOCK, ww, o); return true; }, roster: (v, ww, o) => { fields(v, ROSTER, ww, o); return true; },
since: (v, ww, o) => { fields(v, { days: nat, again: nat }, ww, o); return true; },
keep: (v, ww, o) => { if (!Array.isArray(v) || v.length > 32) return false; v.forEach((f, i) => { checkFx(f, `${ww}[${i}]`, o); if (f[0] !== 'badge' && f[0] !== 'modal') o.push(`${ww}[${i}]: keep holds only badge and modal`); }); return true; } }, w, out);
if (e.t === 'snap') fields(e, { ...SNAP_REALM, ...SNAP_HOST }, w, out);
else if (e.t === 'upd') {
fields(e, { t: str, from: nat, v: nat, now: nat, day: nat, s: obj, del: [listOf(str), 'opt'], r: [slimR, 'opt'], fx: [fxList, 'opt'], acts: [listOf(actEntry), 'opt'] }, w, out);
if (nat(e.v) && nat(e.from) && e.v !== e.from + 1) out.push('upd: v is from + 1 (only a pull makes a version)');
@ -135,6 +141,8 @@ function checkEvent(e) {
// Worker → host replies (4.2). Big payloads (an update, a snapshot) are JSON text, ready to write to every stream.
const json = (t) => (v, w, out) => { if (!str(v)) return false; let e; try { e = JSON.parse(v); } catch (x) { return false; } for (const p of checkEvent(e)) out.push(`${w}: ${p}`); if (e.t !== t) out.push(`${w}: not a ${t}`); return true; };
// The realm's half of a snapshot (above): the host's three fields come later, so here they are unknown.
const snapPart = (v, w, out) => { if (!str(v)) return false; let e; try { e = JSON.parse(v); } catch (x) { return false; } if (!obj(e) || e.t !== 'snap') { out.push(`${w}: not a snap`); return true; } fields(e, SNAP_REALM, w, out); return true; };
const subs = mapOf((k) => /^(link|peer):.{1,40}$/.test(k), (v) => obj(v));
const epoch = (v) => EPOCH.test(v);
const WORKER = {
@ -144,7 +152,7 @@ const WORKER = {
dayDone: { t: str, code: (v) => CODE.test(v), d: nat, nets: mapOf((k) => PID.test(k), (v, w, out) => { fields(v, { day: nat, lost: bool, pause: [bool, 'opt'], err: [str, 'opt'], retried: [bool, 'opt'], ms: (x) => num(x) && x >= 0 }, w, out); return true; }) },
actDone: { t: str, nid: (v) => PID.test(v), epoch, rid: (v) => RID.test(v), outcome: (v) => OUTCOMES.includes(v), why: [str, 'opt'], v: nat, upd: [(v, w, out) => v === null || json('upd')(v, w, out), 'opt'] },
askDone: { t: str, nid: (v) => PID.test(v), rid: (v) => RID.test(v), modal: [(v) => obj(v) && str(v.h) && str(v.c), 'opt'], ui: [obj, 'opt'], err: [str, 'opt'] },
pulled: { t: str, nid: (v) => PID.test(v), epoch, v: nat, upd: [json('upd'), 'opt'], snap: [json('snap'), 'opt'], subs: [subs, 'opt'] },
pulled: { t: str, nid: (v) => PID.test(v), epoch, v: nat, upd: [json('upd'), 'opt'], snap: [snapPart, 'opt'], subs: [subs, 'opt'] },
subbed: { t: str, nid: (v) => PID.test(v), epoch, v: nat, subs },
saved: { t: str, nid: (v) => PID.test(v), txt: str, uidN: nat, rt: obj, day: nat, lost: bool },
card: { t: str, nid: (v) => PID.test(v), card: (v) => v === null || obj(v) },
@ -157,7 +165,7 @@ function checkWorker(m) {
fields(m, WORKER[m.t], m.t, out);
if (m.t === 'loaded' && m.err === undefined) for (const k of ['epoch', 'day', 'lost']) if (m[k] === undefined) out.push(`loaded.${k}: missing`);
if (m.t === 'loaded' && m.err !== undefined && m.saved !== undefined) out.push('loaded: a network that did not load has nothing saved');
if (m.t === 'pulled' && m.snap !== undefined && str(m.snap)) { try { const e = JSON.parse(m.snap); if (e.epoch !== m.epoch) out.push('pulled: the snapshot\'s epoch is not the realm\'s'); } catch (x) { /* reported above */ } }
if (m.t === 'pulled' && m.snap !== undefined && str(m.snap)) { try { const e = JSON.parse(m.snap); if (e.epoch !== m.epoch) out.push('pulled: the snapshot\'s epoch is not the realm\'s'); if (e.v !== m.v) out.push('pulled: the snapshot is not at the version pulled'); } catch (x) { /* reported above */ } }
return out;
}