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

@ -7,6 +7,9 @@
// - Retry: a day that fails once (a throw, and a timeout, both after the day's DDoS step with an attack on, the networks
// busy) leaves the same network as no failure.
// - Order: three networks of a session, in two different realm orders, each the same.
// - Routing memory: a checkpoint taken between an act and the next day, and an act rolled back, keep what the game
// remembers of its last routing (UI.rk, UI.rkR): the next click that changes nothing routing reads routes in neither,
// as in the network that kept going (compute() is not always a fixed point, so a routing there could change R).
// - Market and moved customers (3.2.0, 3.3.0): the market's settings and a customer moved to another fiber or pinned
// to a path, then days of sales, against the page and under the strict gate; a checkpoint of it plays on the same.
// - Timer audit and gate: days with failures, events, the intern on every order and the sales desk on auto, then every
@ -195,6 +198,35 @@ test('retry: a day that throws, or times out, once after the DDoS step with an a
await E.unload(code);
});
test('routing memory: after a checkpoint between an act and the next day, or an act rolled back, a click that changes nothing routing reads routes in neither network', async () => {
const code = 'RKMEM0', base = 'rkbs0001', copy = 'rkcp0002';
const ev = async (nid, src) => (await E.eval(code, nid, src)).value;
checked((await E.load(code, [begin(base)]))[0]);
await day(code, 1, [base]);
assert.equal(checked(await E.act(code, base, msg('launchBB', { city: 'fra' }))).outcome, 'applied');
// The checkpoint, right after an act that routed, in a fresh realm.
const sv = checked(await E.save(code, base));
assert.equal(sv.rt.rk, true, 'the checkpoint says R is the routing of S as it is');
checked((await E.load(code, [{ nid: copy, env: { s: sv.txt, uidN: sv.uidN, rt: sv.rt }, lang: 'en', prefs: {} }]))[0]);
await ev(copy, `__srvClock(${await ev(base, 'return __srvClock()')});return 1;`);
const COUNT = '{const c=compute;compute=function(){__cc++;return c.apply(this,arguments);};}window.__cc=0;return 1;';
for (const n of [base, copy]) await ev(n, COUNT);
for (const n of [base, copy]) assert.equal(checked(await E.act(code, n, msg('renameGo', { name: 'Tubular Bells' }))).outcome, 'applied');
assert.deepEqual([await ev(base, 'return __cc'), await ev(copy, 'return __cc')], [0, 0], 'a click that changes nothing routing reads');
{ const x = await realmS(code, base), y = await realmS(code, copy); assert.ok(x === y, differs(x, y)); }
// An act that routes, then throws: back in place, what the game remembers of its routing with it.
const net = await ev(base, "return NETS.filter(n=>sellsAt(n,'fra')&&!S.transit.some(t=>t.city==='fra'&&t.net===n.id))[0].id");
await ev(base, "{const f=ACT.buyTransit;ACT.buyTransit=a=>{f(a);throw new Error('after its routing');};}return 1;");
const r = checked(await E.act(code, base, msg('buyTransit', { city: 'fra', net, tier: 1 })));
assert.equal(r.outcome, 'error');
assert.ok(await ev(base, 'return __cc') > 0, 'the act that failed had routed');
for (const n of [base, copy]) await ev(n, '__cc=0;return 1;');
for (const n of [base, copy]) checked(await E.act(code, n, msg('renameGo', { name: 'Packet Pals' })));
assert.deepEqual([await ev(base, 'return __cc'), await ev(copy, 'return __cc')], [0, 0], 'nor after an act rolled back');
{ const x = await realmS(code, base), y = await realmS(code, copy); assert.ok(x === y, differs(x, y)); }
await E.unload(code);
});
test('order: three networks of a session play the same in either realm order', async () => {
// The same three networks under ids that sort the other way round: X, Y, Z here are Z, Y, X there.
const X = { home: 'fra' }, Y = { home: 'nyc', preset: 'bgp' }, Z = { home: 'sin', preset: 'freeze' };