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

@ -4,7 +4,9 @@
// Order. Messages run one at a time, in arrival order per network. A session's day is one task per network, in sorted
// network order, with a turn for other messages between two networks: an action for network X lands between days of
// other networks, never inside X's own day. The networks of a day are fixed when it starts. Loads, new networks and
// unloads of a session wait for its day; actions for a network that is still loading wait for it.
// unloads of a session wait for its day; actions for a network that is still loading wait for it. Every message is
// answered: a load that names no network (or one twice) is refused whole and changes nothing; a network in it that
// has no save, or whose save does not load, answers err (broken) and the others load.
//
// Failures. Every realm call has a time limit (a day 2 s, an action 1 s, a what-if 3 s, a pull or a save 1 s). Before
// a day or an action the realm is marked (its S as text, uidN, the dice, and for a day its rt: route memory and routing
@ -90,6 +92,7 @@ function createCore(opts = {}) {
function loadOne(c, n) {
const e = realms.get(key(c, n.nid));
try {
if (!n.begin && !(n.env && typeof n.env === 'object' && typeof n.env.s === 'string')) throw new Error('no save to load');
e.realm = build(n.nid, n.lang);
e.lang = e.realm.lang;
const epoch = epochOf();
@ -204,8 +207,10 @@ function createCore(opts = {}) {
return true;
}
for (let k = 0; k < i; k++) if (jobs[k].code === j.code && SESSION_OPS.has(jobs[k].t) && jobs[k].t !== 'day') return false;
// A network that is loading waits for its load; one marked loading with no load under way (none can be: a guard)
// waits for nothing, so start() answers NO_REALM.
const e = realms.get(key(j.code, j.nid));
return !e || !e.loading;
return !e || !e.loading || !(s && s.load);
}
function reply(j, r) { post({ id: j.id, ok: true, reply: r === undefined ? null : r }); }
function fail(j, x) { post({ id: j.id, ok: false, error: { message: String((x && x.message) || x), code: (x && x.code) || 'ERR' } }); }
@ -222,11 +227,17 @@ function createCore(opts = {}) {
function start(j) {
if (j.t === 'mem') { const h = v8.getHeapStatistics(), mu = process.memoryUsage(); return reply(j, { heapUsed: h.used_heap_size, heapTotal: h.total_heap_size, external: h.external_memory, rss: mu.rss, realms: realms.size }); }
if (j.t === 'load') {
const s = session(j.code);
for (const n of j.nets) {
if (!n || typeof n.nid !== 'string' || (!n.begin && !(n.env && typeof n.env.s === 'string'))) throw new Error('load: each network needs a nid and an env with its save text, or begin');
realms.set(key(j.code, n.nid), { code: j.code, nid: n.nid, lang: n.lang || 'en', prefs: n.prefs || {}, loading: true, realm: null, epoch: '', v: 0, day: 0, lost: false, broken: false, mark: null, dayMark: null });
// The whole call first, with no side effects: a malformed one is refused and leaves every realm as it was. A
// network without a save to load (or a begin) is only that network's problem: loadOne answers it broken.
const nets = Array.isArray(j.nets) ? j.nets : [], seen = new Set();
if (!nets.length) throw new Error('load: give the networks to load');
for (const n of nets) {
if (!n || typeof n !== 'object' || typeof n.nid !== 'string' || !n.nid) throw new Error('load: each network needs a nid');
if (seen.has(n.nid)) throw new Error(`load: ${n.nid.slice(0, 40)} is there twice`);
seen.add(n.nid);
}
const s = session(j.code);
for (const n of nets) realms.set(key(j.code, n.nid), { code: j.code, nid: n.nid, lang: n.lang || 'en', prefs: n.prefs || {}, loading: true, realm: null, epoch: '', v: 0, day: 0, lost: false, broken: false, mark: null, dayMark: null });
s.load = { j, i: 0, out: [] };
return;
}
@ -242,7 +253,8 @@ function createCore(opts = {}) {
return reply(j, { t: 'unloaded', code: j.code, ...(j.nid !== undefined ? { nid: j.nid } : {}) });
}
const e = realms.get(key(j.code, j.nid));
if (!e) throw Object.assign(new Error(`no realm ${key(j.code, j.nid)}`), { code: 'NO_REALM' });
if (e && e.loading) { realms.delete(key(j.code, j.nid)); log('error', `[${j.nid}] marked loading with no load under way: dropped`); }
if (!e || e.loading) throw Object.assign(new Error(`no realm ${key(j.code, j.nid)}`), { code: 'NO_REALM' });
const h = H[j.t];
if (!h) throw new Error(`unknown message ${j.t}`);
return reply(j, h(j, e));
@ -263,12 +275,20 @@ function createCore(opts = {}) {
if (D.i >= D.order.length) { s.day = null; reply(D.j, { t: 'dayDone', code: D.j.code, d: D.j.d, nets: D.out }); }
return true;
}
/* Turns: a message and a step of a session's load or day, in alternation. The worker goes on only while something ran:
what a job waits for (a load, a day, an earlier job of its session) is itself a job or a step that runs, so a pass
where nothing could run means nothing will until a message comes (handle() kicks). If a job were still waiting
then, nothing could ever free it: it is answered, not left pending, and the worker does not spin on it. */
let flip = false;
function pump() {
pumping = false;
flip = !flip;
if (flip) { if (!runJob()) runStep(); } else if (!runStep()) runJob();
if (jobs.length || [...sessions.values()].some((s) => s.load || s.day)) kick();
if (flip ? runJob() || runStep() : runStep() || runJob()) return kick();
if (!jobs.length) return;
const j = jobs.shift();
log('error', `mp-sim worker: ${j.t} for ${key(j.code, j.nid)} waited for nothing that could come; answered`);
fail(j, new Error(`${j.t} could not run`));
kick();
}
function kick() { if (!pumping) { pumping = true; setImmediate(pump); } }
return {