Server-run engine: review fixes

The review of track T2 found these; each has a test in tests/mp-sim-*.

- Built-ins: an arg, array entry or draft that is '__proto__', 'constructor'
  or 'prototype' is refused by the envelope check and again by the realm
  (free text may still say them). After every realm call the trampoline
  checks the realm's built-ins against what they were at load; a realm
  whose built-ins changed answers {srvErr, polluted} and is replaced from
  the worker's mark like one that timed out. S as one JSON per key is a
  null-prototype object, so the epilogue's for…in never reads a prototype.
- A realm that cannot be built again is never used again: rebuild() tries
  once more without the packed R, and when that fails too the network is
  dropped (NO_REALM), and the engine tells the host (opts.onLost).
- A mark that fails or runs out of time changes nothing: it clears the old
  mark first, the realm is built again from its own save, never from an
  older mark, and the reply names the mark. A day rebuilds only from its
  own attempt's mark.
- A day whose alone or nets is not a list is refused whole (ERR); a throw
  in a load or day step answers that call, never ends the worker.
- An exit counts against the session the worker was running (the worker
  says so before each job and step), else as before. Workers of their own
  are capped (opts.maxOwn, 4): past that a session is given up.
- A worker that exits before it is ready is started again after a pause
  that doubles; after 5 in a row every call answers SIM_DOWN.
- The game text is read and compiled once, in createEngine: a src/ that
  does not compile throws there, and every worker, also one started after
  an exit, runs the game engine.simId names.
- A damaged route memory never breaks a save that loads (routed from
  nothing, logged), and only a server network loads (S.mp.sim, Normal).
- Routing memory (UI.rk, UI.rkR) is kept by the in-place restore and
  carried in rt (rk), so a reloaded or rolled-back network skips the same
  routings as the one that kept going.
- A fresh realm takes over the fx of the one it replaces (the mark's after
  a failed call, all of them after a read): badges, windows and toasts
  waiting for a page are not lost to a timeout.
- Windows: the stand-in elements answer querySelector from what was
  written (a [data-x] marker) and have a working classList, never the
  inert proxy that said every window is open. A request's ctx may name the
  open window, ctx.win = {k, ui} (mp-sim-acts.js WINDOWS: bstd, routing,
  charter, ops, pie), with the view state only the page holds (the
  intern's POP, the pie window's tab and POP), checked in the realm. This
  is an addition to the contract of design 5.8 that the page (T4) sends.
  The stand-in is hidden again after an act.
- xpBuild's via is a TEXT arg (256): a six-span express fits.
- actDone, askDone and pulled carry ms, the worker time of the call;
  engine.mem() and engine.has(code, nid) are public; prefs() passes a
  realm's err through; after close() every call rejects CLOSED.
- The fake engine fails as the engine does (NO_REALM, ERR, a load without
  a save, a day of an unknown session or naming an unloaded network,
  GAVE_UP, SIM_DOWN, CLOSED, onLost), its greet says its one-shots once and
  never 'told'; a test runs the same edge calls on both.
- SIM_DRAFT is built from DRAFT_TYPES.
This commit is contained in:
jvanvik 2026-11-09 22:16:52 +01:00
parent 3b8c331556
commit 9597d89079
11 changed files with 879 additions and 211 deletions

View file

@ -16,6 +16,8 @@ const path = require('node:path');
const SIM = simId();
const env = (act, args = {}, more = {}) => ({ rid: 'a1b2c3d4e5', dev: '9f8e7d6c', sim: SIM, act, args, ...more });
// A checkpoint as the host hands it to load(): the save text (the canned network on that day).
const sv = (day) => JSON.stringify({ ...fixture('snap').S, day });
test('every ACT is classified once: SEND, ASK, VOTE, LOCAL or PUZZLE; SEND is the agent\'s game commands, adjusted', (t) => {
// The development page of a multiplayer build: every ACT there is, the server-play client's (98-server-play) too.
@ -94,8 +96,26 @@ test('the envelope check refuses what section 10 refuses', () => {
[env('pniAll', { net: null, tier: 1 }), 'a quote act without its quote'], [env('utilityCaches', { city: 'fra', quote: 5 }), 'a key quote as a number'],
[env('bstdApply', { q: 5 }), 'the build standard without its plan'], [env('bstdApply', { quote: 5 }), 'a plan key as a number'],
[env('teDeploy', { script: 'x'.repeat(4001) }), 'a script over 4000'], [env('metricSet', { id: 'l1', v: '1'.repeat(17) }), 'a metric over 16'],
// Values that name a built-in: written through as a key (S.pops[city]) they reach a prototype.
[env('launchBB', { city: '__proto__' }), '__proto__ as a city'], [env('addGen', { city: 'constructor' }), 'constructor as a city'], [env('buildPop', { city: 'prototype' }), 'prototype as a city'],
[env('buildPop', { city: ['fra', '__proto__'] }), '__proto__ in an array'], [env('buildLink', { tier: 1 }, { ui: { pending: { a: '__proto__', b: 'ams' } } }), '__proto__ in a draft'],
[env('decomGo', {}, { ui: { decom: ['fra', 'constructor'] } }), 'constructor among the cities to close'], [env('orderDf', {}, { ui: { dfSys: 'constructor' } }), 'constructor as a system'],
// The open window: one the server knows by name, with only its own view state.
[env('opsTpl', { k: 'bb' }, { ctx: { modal: true, win: { k: 'nope' } } }), 'a window that is none'], [env('opsTpl', { k: 'bb' }, { ctx: { win: 'ops' } }), 'a window as text'],
[env('opsTpl', { k: 'bb' }, { ctx: { win: { k: 'ops', ui: { pieTab: 'co' } } } }), 'another window\'s view state'], [env('opsTpl', { k: 'bb' }, { ctx: { win: { k: 'ops', x: 1 } } }), 'a window with more'],
[env('opsTpl', { k: 'bb' }, { ctx: { win: { k: 'ops', ui: { opsCity: '__proto__' } } } }), '__proto__ as the intern\'s POP'], [env('opsTpl', { k: 'bb' }, { ctx: { win: { k: 'ops', ui: { opsCity: { a: 1 } } } } }), 'a POP as an object'],
[env('opsTpl', { k: 'bb' }, { ctx: { win: { k: 'constructor' } } }), 'constructor as a window'],
]) no('act', b, why);
assert.equal(ok('act', env('teDeploy', { script: 'x'.repeat(4000) })).ok, true, 'a long script within its cap');
// Free text may say them: a network named for a word.
assert.equal(ok('act', env('renameGo', { name: 'constructor' })).ok, true, 'a name is no key');
// An express of six spans: six link ids as the game mints them (10 or 11 characters each), joined by commas.
const ids = Array.from({ length: 6 }, (x, i) => 'i' + (2e7 + i).toString(36).slice(-5) + (12345 + i));
assert.equal(ids.join(',').length > 64, true);
assert.equal(ok('act', env('xpBuild', { via: ids.join(',') })).ok, true, 'the longest express a player can build');
for (const win of [{ k: 'ops', ui: { opsCity: 'zrh' } }, { k: 'pie', ui: { pieTab: 'co', pieCity: 'fra' } }, { k: 'routing' }, { k: 'bstd' }, { k: 'charter' }, { k: 'pie', ui: { pieTab: null } }]) {
assert.equal(ok('act', env('opsTpl', { k: 'bb' }, { ctx: { modal: true, win } })).ok, true, JSON.stringify(win));
}
// A viewport outside what the layout tells apart is clamped, not refused: a 5120-px monitor, a desktop zoomed out.
for (const [w, h, cw, ch] of [[5120, 1440, A.VIEW_MAX, 1440], [100, 50, A.VIEW_MIN, A.VIEW_MIN], [390, 844, 390, 844]]) {
const b = env('buildPop', { city: 'fra' }, { ctx: { w, h } });
@ -163,7 +183,7 @@ test('the canned state is the game\'s own: migrate() keeps the snapshot and ever
test('the fake engine answers every call in the shapes of section 4.2', async () => {
const e = createFakeEngine(), code = 'K7Q2XD', a = 'abcd1234', b = 'efgh5678';
const all = [];
const L = await e.load(code, [{ nid: a, begin: { name: 'Packet Pals', home: 'fra', preset: 'oncall', asn: 201234, mp: {}, tut: false }, lang: 'en', prefs: {} }, { nid: b, env: { day: 40 }, lang: 'de', prefs: {} }]);
const L = await e.load(code, [{ nid: a, begin: { name: 'Packet Pals', home: 'fra', preset: 'oncall', asn: 201234, mp: {}, tut: false }, lang: 'en', prefs: {} }, { nid: b, env: { s: sv(40) }, lang: 'de', prefs: {} }]);
assert.equal(L.length, 2); assert.ok(L[0].saved, 'a new network is checkpointed at once');
assert.ok(L[0].epoch && L[1].epoch && L[0].epoch !== L[1].epoch, 'each realm load has its own epoch');
all.push(...L);
@ -215,7 +235,7 @@ test('the fake engine answers every call in the shapes of section 4.2', async ()
});
test('the fake engine fails the way the engine does: a network that does not load, a day that fails twice or once', async () => {
const a = 'abcd1234', b = 'efgh5678', c = 'ijkl9012', net = (nid) => ({ nid, env: { day: 5 }, lang: 'en', prefs: {} });
const a = 'abcd1234', b = 'efgh5678', c = 'ijkl9012', net = (nid) => ({ nid, env: { s: sv(5) }, lang: 'en', prefs: {} });
// A save that does not load (4.7): that network is broken, the others load.
const e = createFakeEngine(), code = 'K7Q2XD';
e.breakNext(code, b);
@ -254,6 +274,27 @@ test('the fake engine fails the way the engine does: a network that does not loa
assert.match(checkWorker({ t: 'pulled', nid: a, v: 1 }).join(), /epoch: missing/);
assert.match(checkWorker({ t: 'loaded', nid: a, err: 'x', saved: { t: 'saved' } }).join(), /nothing saved/);
assert.deepEqual(checkFx(['toast', { h: 'one argument', tick: false }]), [], 'toast(html): k and a may be left out');
// Refused the way the engine refuses, with its codes (tests/mp-sim-engine.test.js holds the two to each other).
const lost = [], X = createFakeEngine({ onLost: (cd, nids, why) => lost.push([cd, ...nids, why].join(' ')) });
await assert.rejects(X.load(code, []), (x) => x.code === 'ERR');
await assert.rejects(X.load(code, [net(a), net(a)]), (x) => x.code === 'ERR' && /twice/.test(x.message));
const [nl] = await X.load(code, [{ nid: a, env: { day: 5 }, lang: 'en', prefs: {} }]);
assert.equal(nl.err, 'no save to load', 'a checkpoint without its save text');
await assert.rejects(X.act(code, a, env('buildPop', { city: 'par' })), (x) => x.code === 'NO_REALM');
await assert.rejects(X.day('N0SVCH', { d: 1, nets: [a] }), (x) => x.code === 'NO_REALM');
await X.load(code, [net(a)]);
assert.deepEqual(Object.keys((await X.day(code, { d: 1, nets: [a, b] })).nets), [a], 'a network that is not loaded is left out');
const G1 = await X.greet(code, a), G2 = await X.greet(code, a);
assert.deepEqual([typeof G1.upd, G2.upd], ['string', undefined], 'the one-shots of a greet are said once');
X.failNext('load', 'gaveUp');
await assert.rejects(X.load('Q9W8E7', [net(c)]), (x) => x.code === 'GAVE_UP');
// A worker that dies takes every realm, also of a session with nothing pending: its next call answers NO_REALM.
X.exit();
assert.deepEqual(lost, [`${code} ${a} exit`]);
await assert.rejects(X.pull(code, a, {}), (x) => x.code === 'NO_REALM');
await X.close();
await assert.rejects(X.load(code, [net(a)]), (x) => x.code === 'CLOSED');
await assert.rejects(X.pull(code, a, {}), (x) => x.code === 'CLOSED');
});
test('the harness: load({mp:true}) is a stamped MP page, and fakeFetch streams events the page reads through fetch', async (t) => {