Server-run engine: every call answers, and a crashing load gets its own worker

A load whose networks did not all check out could hang its session for good, a
worker could spin on a job it could never run, a session whose load crashed its
worker never got a worker of its own, and an unload followed at once by a load
(a language change) lost the session's place or never answered. Fixed:

- Worker: a load call is checked whole before it touches anything. One that
  names no network, or one twice, is refused (ERR) and every realm stays as it
  was. A network without a save now answers err (broken) like one whose save
  does not load, and the others load. A network marked loading with no load
  under way answers NO_REALM instead of waiting for ever.
- Worker: it goes on only while something ran. If a job were ever still
  waiting when nothing could run, it is answered (ERR, logged) rather than left
  pending, and the worker no longer busy-loops on it.
- Engine: a session is on its worker from the moment its load is sent. A worker
  exit counts against the sessions whose load was under way (else against every
  session it held), so a save that crashes its worker gets a worker of its own
  after two exits and is given up (GAVE_UP) after a third, while the sessions
  that only shared the worker lose their realms but not their place in the pool.
  Loads under way count for placement.
- Engine: an unload no longer drops the placement of a session whose next load
  is under way, so an unload and a load sent back to back stay on one worker; a
  worker of its own is retired only when no call is pending on it (a pending
  call there used to never settle).
- Engine: a message that cannot be sent rejects (ERR) and leaves nothing pending.
- Realm: a what-if puts UI.hotMap back too (teSlop's trial left its own).
- Realm: a network in a language this server has no dictionary for plays in
  English, as the page does, instead of loading broken.

Tests (tests/mp-sim-engine.test.js): a malformed load changes nothing and every
call of the session answers within 3 s, with the workers idle after; an unload
and a load back to back keep the session on its worker; a session whose load
takes its worker down twice gets a worker of its own, then GAVE_UP, while the
bystander is never blamed; an ask leaves UI.hotMap as it was; a network in an
unknown language loads in English. Each fails on the engine before this change
(the worker's alone: the hang; the engine's alone: the placement and the exits;
the realm's alone: teSlop's hotMap and the language).

The two drafts no test played are played now (tests/mp-sim-acts.test.js): a
premium border crossing into Hong Kong (buildLink with cnPrem) and the AI's own
script deployed as it wrote it while it drops most of the traffic (teDeploy with
slop, on a small network with one capped transit port), each compared byte for
byte with the page's clicks, and both read in the DRAFTS check (nothing is left
unplayed).

Runs, each file alone under an 8G cap: mp-sim-engine 16/16 (11.6 s),
mp-sim-contract 8/8, mp-sim-acts 5 + 1 todo (10.9 s), mp-sim-determinism 6/6
(53.4 s).
This commit is contained in:
jvanvik 2026-11-09 20:35:18 +01:00
parent c5c7ea9508
commit 72f31e809b
6 changed files with 204 additions and 45 deletions

View file

@ -47,6 +47,17 @@ function pieCo() {
P.ev("ACT.buildPop({city:'sin'});ACT.pieFound();ACT.pieFactory();ACT.piePad({city:'sin'});S.pie.stock=400;closeModal();S.rng=99;uidN=1000;S=migrate(JSON.parse(JSON.stringify(S)));compute();");
return P;
}
// A small network with one transit port, Auto-TE and a script slot: a cap on that transit drops most of its traffic
// (teDeploy's other path, when the AI's script is the one deployed).
function oneUp() {
const P = page();
P.ev("ACT.start({});if(UI.pendGame)ACT.pitchSkip();S.cash=1e9;S.lvl=6;for(const g of GOALS)S.done[g.id]=0;ACT.launchBB({city:'fra'});S.pops.fra.bb.d=3000");
P.ev("ACT.buyTransit({city:'fra',net:NETS.filter(n=>sellsAt(n,'fra')&&n.price)[0].id,tier:1});ACT.coreUp();ACT.coreUp();S.te={on:true,day:S.day,script:''};closeModal();S.rng=99;uidN=1000;S=migrate(JSON.parse(JSON.stringify(S)));compute();");
P.cap = `cap transit ${P.ev('NETS.find(n=>n.id===S.transit[0].net).name')} at 5 gbps`;
return P;
}
// A Hong Kong and a Shanghai POP and a mainland license: a link between them crosses the border, and may cross it premium.
const CROSS = "S.cnLic={state:'ok'};for(const c of ['hkg','sha'])if(!S.pops[c])S.pops[c]={day:0,bb:null,boost:0,down:0,routers:1,gen:false};S=migrate(JSON.parse(JSON.stringify(S)));compute();";
const loadInto = async (code, nid, P) => checked((await E.load(code, [{ nid, env: { s: P.ev('JSON.stringify(S)'), uidN: +P.ev('uidN'), rt: null }, lang: 'en', prefs: {} }]))[0]);
test('two-click buttons: the first click arms SRV_ARM\'s key (or waits for ok), and confirmed:true on the server is both clicks', async (t) => {
@ -112,9 +123,8 @@ test('repayLoan: armed by its id, repaid by the second click (a network small en
/* The UI values an act reads, recorded while it runs (its redraw, after(), and windows left out: they read the page's
own state to draw it). Every page-held value it reads must be one of its DRAFTS; each of its DRAFTS is read on one of
the paths played here, except the few named with the reason they are not played. */
the paths played here. */
const PAGE_HELD = [...new Set([...Object.keys(A.DRAFT_TYPES), 'lmode', 'dfMode', 'drag', 'snap', 'hover', 'vp', 'nf', 'dtab'])];
const UNPLAYED = { cnPrem: 'buildLink reads it only on a link into Hong Kong across the border', slop: 'teDeploy reads it only when the script cost a tenth of the traffic' };
test('DRAFTS: the page-held UI values each act reads, all of them', (t) => {
const P = rich();
t.after(() => P.close());
@ -128,11 +138,13 @@ test('DRAFTS: the page-held UI values each act reads, all of them', (t) => {
['buildLink', `openLinkModal('fra','${near}')`, { tier: 1 }],
['buildLink', `openLinkModal('${sib[0]}','${sib[1]}');UI.route='same';`, { tier: 1 }],
['buildLink', `openLinkModal('lon','nyc');UI.via='land';`, { tier: 1 }],
['buildLink', CROSS + "openLinkModal('hkg','sha');ACT.cnPrem({v:1});", { tier: 1 }],
['orderDf', "UI.pending={a:'fra',b:'muc'};UI.dfSrc='lease';UI.dfSys='lite';", {}],
['orderDf', "UI.pending={a:'fra',b:'muc'};UI.dfSrc='build';UI.dfSys='lite';", {}],
['orderCable', "UI.pending={a:'lon',b:'nyc'};UI.seaDesign='express';UI.seaterminal=true;UI.seaengineering=false;", {}],
['decomGo', "UI.decom=new Set(['bet']);", {}],
['teDeploy', "S.te={on:true,day:S.day,script:''};UI.slop=undefined;", { script: 'prefer cog at fra' }],
(() => { const Q = oneUp(); return ['teDeploy', `UI.slop=${JSON.stringify(Q.cap)};`, { script: Q.cap }, Q]; })(),
['pieLaunch', 'UI.pieIns=true;UI.pieLas=false;', { rk: 'falcon', cr: 'short' }, pieCo()],
];
const reads = {};
@ -151,7 +163,7 @@ test('DRAFTS: the page-held UI values each act reads, all of them', (t) => {
}
for (const act of Object.keys(A.DRAFTS)) {
assert.ok(reads[act], `${act} was played`);
for (const k of A.DRAFTS[act]) if (!UNPLAYED[k]) assert.ok(reads[act].has(k), `${act} reads ${k}`);
for (const k of A.DRAFTS[act]) assert.ok(reads[act].has(k), `${act} reads ${k}`);
}
assert.deepEqual(P.errors, []);
});
@ -160,7 +172,7 @@ test('drafts and texts: the server\'s run of each draft and text act leaves the
const P = rich();
t.after(() => P.close());
const { ev } = P;
ev("S.te={on:true,day:S.day,script:''};closeModal();UI.sel=null;compute();");
ev("S.te={on:true,day:S.day,script:''};closeModal();UI.sel=null;compute();" + CROSS);
const code = 'DRAFT5', nid = 'drft0001';
await loadInto(code, nid, P);
const near = ev("Object.keys(CITY).filter(c=>!S.pops[c]&&homeOk(c)).sort((a,b)=>kmBetween('ams',a)-kmBetween('ams',b))[0]");
@ -168,6 +180,7 @@ test('drafts and texts: the server\'s run of each draft and text act leaves the
// [act, args, what the page did before the click (its drafts), the drafts sent]
const plays = [
['buildLink', { tier: 1 }, `openLinkModal('fra','${near}')`, () => pick(ev, ['pending', 'route', 'via', 'cnPrem'])],
['buildLink', { tier: 1 }, "openLinkModal('hkg','sha');ACT.cnPrem({v:1});", () => pick(ev, ['pending', 'route', 'via', 'cnPrem'])],
['orderDf', {}, "UI.pending={a:'fra',b:'muc'};UI.dfSrc='lease';UI.dfSys='lite';", () => pick(ev, ['pending', 'dfSrc', 'dfSys'])],
['orderCable', {}, "UI.pending={a:'lon',b:'nyc'};UI.seaDesign='express';UI.seaterminal=true;UI.seaengineering=false;", () => pick(ev, ['pending', 'seaDesign', 'seaterminal', 'seaengineering'])],
['decomGo', {}, "UI.decom=new Set(['bet']);", () => ({ decom: JSON.parse(ev('JSON.stringify([...UI.decom])')) })],
@ -177,6 +190,7 @@ test('drafts and texts: the server\'s run of each draft and text act leaves the
['renameGo', { name: 'Tubular Bells' }, '', null],
];
await play(code, nid, ev, plays);
assert.equal(ev("S.links.filter(l=>l.premium).length"), 1, 'the crossing into Hong Kong went premium');
// A what-if with its text: the window, and the network as it was.
const K = checked(await E.ask(code, nid, msg('teCheck', { script })));
assert.match(K.modal.h, /term/);
@ -191,6 +205,13 @@ test('drafts and texts: the server\'s run of each draft and text act leaves the
['pieLaunch', { rk: 'falcon', cr: 'short' }, 'UI.pieIns=true;UI.pieLas=false;', () => pick(Q.ev, ['pieIns', 'pieLas'])],
]);
assert.deepEqual(Q.errors, []);
// The AI's script, deployed as it wrote it, drops most of the traffic: its own apology, not the fat-finger one.
const T = oneUp();
t.after(() => T.close());
await loadInto('SL0PPY', 'slop0001', T);
await play('SL0PPY', 'slop0001', T.ev, [['teDeploy', { script: T.cap }, `UI.slop=${JSON.stringify(T.cap)};`, () => ({ slop: true })]]);
assert.equal(T.ev("S.log.slice(0,3).some(x=>/AI apologises/.test(x.t))"), true, 'the slop path');
assert.deepEqual(T.errors, []);
});
async function play(code, nid, ev, plays) {
for (const [act, args, before, ui] of plays) {