Merge perf/6-routing-design: routing keeps its structural tables and runs as small functions, about half the CPU, identical results (347 fuzzed games)

# Conflicts:
#	CHANGELOG.md
This commit is contained in:
Frederic Jung 2026-11-01 21:57:08 +01:00
commit add3d67e48
No known key found for this signature in database
12 changed files with 717 additions and 59 deletions

View file

@ -12,6 +12,7 @@ All notable changes to this project are documented here. The project follows
- Working out where traffic goes takes about a fifth less work on big networks, with exactly the same result.
- The game no longer freezes while it routes each day's traffic on big networks. A background thread of the game does the routing, so the map, panels and buttons keep responding while days go by; what the day brings is exactly what it was before. Clicks that change your network still route right away.
- A plug-in optic that is too short for your subsea cable no longer sends you to submarine terminals and optical engineering, which cannot stretch it (#253). The hint now names the optics that do reach, such as the Sienna WaveMagic 1.6T for Perth to Cape Town. Optics the upgrades can stretch still get the advice.
- Big networks use about half the battery they did: each day's routing takes half the processor time, and reuses what it worked out the day before when only demand has changed. Where traffic goes does not change.
## [2.4.0] — 2026-11-01

View file

@ -140,6 +140,20 @@ It runs every day and after every action, so it is built for big networks: the g
indices (`CIDX`), each exit is asked once per network what it can reach, and `R.ef` (what crosses each
link) is only put together for a link when something reads it.
What follows from the network's shape alone is kept from one call to the next (`29-route-cache.js`): the shortest
paths from every POP, and who reaches whom with each network's exit lists. Each has a key made of everything it is
built from (the live graph with its weights; every exit with what it reaches and how it is preferred, RouteScript and
the events that move routes), so most days, when only demand changes, they are reused, and any other change rebuilds
them. Only indices are kept: exits, lists and paths are made fresh every call, because routing writes on them.
`tests/route-cache.test.js` checks the keys, and the equivalence tests route with and without the kept tables.
`scripts/route-fuzz.js` plays hundreds of random seeded games on main's page and this checkout's at once, comparing all
of R and S after every move and day, with the kept tables and without (`tests/route-fuzz.test.js` plays a few).
Each loop of `compute()` runs in a function of its own (the stages are closures, the busiest is `rankPass`;
the money is `computeMoney()`). V8 optimises a small function quickly and keeps it; `compute()` as one 33 KB function
never stayed optimised: it ran in baseline code, boxing every number, and was compiled again and again in the background
only to be thrown away, which cost more CPU than the routing itself. Keep it that way: a new loop goes in a function.
Served share per city (`sfOf`) drives eyeball revenue and customer mood.
## Routing in a worker

167
scripts/lib/fuzz-page.js Normal file
View file

@ -0,0 +1,167 @@
// Runs inside a game page (scripts/route-fuzz.js loads two: main's and this checkout's), next to scripts/lib/mcp-page.js.
// The driver picks every move with its own seeded random numbers and sends the same calls to both pages, so the pages
// stay in lockstep; this file only offers what a player could do now (buttons on screen, POPs, cities) and digests what
// routing gave, as strings the driver compares.
(() => {
const DENY = new Set(['animSet', 'cont', 'exportSave', 'importGo', 'importPick', 'install', 'langSet', 'menu', 'mute', 'newgame', 'phonePlay', 'phoneSpeed',
'prefSet', 'pzShare', 'resetAsk', 'settings', 'settingsClose', 'share', 'shareDl', 'shareGo', 'slotBack', 'slotDel', 'slotDelGo', 'slotExport', 'slotMenu',
'slotRetry', 'slotSpace', 'slotSwitch', 'slotTake', 'soundSet', 'speed', 'start', 'toTitle', 'traceCopy', 'updateLater', 'updateNow', 'vpSet', 'wsShare',
// Leave the game or open what is not this game: the title screen, menus of other modes, AI helpers that fetch.
'closePanel', 'puzzleMenu', 'puzzleDoor', 'scnMenu', 'startScn', 'startWeekly', 'pzStart', 'pzArStart', 'arMenu', 'teSlop', 'help', 'helpClose', 'keepPlaying',
'logOpen', 'pitchSkip', 'pitchGo', 'feedback', 'tutSkip', 'tutNext']);
const sig = (b) => b.act + ' ' + JSON.stringify(b.args);
const btns = (root) => (root ? [...root.querySelectorAll('[data-act]')] : []).filter((b) => !b.disabled && !DENY.has(b.dataset.act) && typeof ACT[b.dataset.act] === 'function')
.map((b) => { let args = {}; try { args = JSON.parse(b.dataset.args || '{}'); } catch (e) {} return { act: b.dataset.act, args }; });
const modalOpen = () => !$('modal').hidden;
const errs = [];
const call = (act, args) => {
try { const r = ACT[act](args || {}); if (r && typeof r.then === 'function') r.catch((e) => errs.push(act + ': ' + e.message)); return ''; } catch (e) { return act + ': ' + (e && e.message); }
};
const fixClock = (t) => { Date.now = () => t; };
// ---------- starts ----------
const homes = () => CITIES.filter((c) => homeOk(c.id)).map((c) => c.id);
// Home cities with rules of their own: China, Korea, Russia, Australia, the Philippines, islands and far places.
const special = () => CITIES.filter((c) => homeOk(c.id) && (c.cn || c.kr || c.ru || ['AU', 'PH', 'NZ', 'JP', 'ID', 'IS', 'FJ', 'PF', 'MU', 'MV', 'CL', 'ZA', 'BR', 'IN', 'SG', 'HK', 'TW', 'KR', 'CN'].includes(c.cc))).map((c) => c.id);
const lists = () => ({ homes: homes(), special: special(), scn: SCN.map((s) => s.id), pz: PZ.map((p) => p.id), ar: AR_RUNS.map((r) => r.id) });
const settle = () => { UI.started = true; UI.pitched = true; UI.speed = 0; if (UI.pendGame) ACT.pitchSkip(); closeModal(); if (S && !pzOn()) compute(); renderAll(); };
const start = ({ kind, id, city, preset, seed, name }) => {
if (window.__reseed) window.__reseed(seed);
fixClock(1.7e12 + seed * 1000);
UI.pitched = true; UI.pendGame = null;
if (pzOn()) pzLeave();
if (kind === 'normal') beginGame(name, city, { mode: 'normal', preset });
else if (kind === 'sandbox') beginGame(name, city, { mode: 'sandbox', rules: { chaos: preset } });
else if (kind === 'scenario') { const sc = SCN.find((x) => x.id === id); beginGame(name, sc.city, { scn: sc }); }
else if (kind === 'weekly') { const wk = weekly(); beginGame(name, wk.city, { scn: wk, seed: wk.seed }); }
else if (kind === 'puzzle') pzBegin(pzDef(id));
else if (kind === 'architect') arBegin(arRunDef(id));
// A saved network (the big fixture): loaded and migrated as an import is, with its incidents and events still on.
else if (kind === 'save') { S = migrate(JSON.parse(window.__fzSave)); }
settle();
return { mode: S.mode, home: S.home, puzzle: pzOn() };
};
// ---------- what a player could do now ----------
// The nearest cities, and those a cable lands from here (subsea seats, IRUs and own cables start there).
const near = (c, n) => CITIES.filter((x) => x.id !== c).map((x) => [x.id, kmBetween(c, x.id)]).sort((a, b) => a[1] - b[1]).slice(0, n).map((x) => x[0])
.concat(CABLE_PAIRS.filter((p) => p.a === c || p.b === c).slice(0, 4).map((p) => (p.a === c ? p.b : p.a)));
const state = () => {
const pops = Object.keys(S.pops), nb = new Set();
for (const p of pops.slice(0, 12)) for (const c of near(p, 8)) if (!S.pops[c]) nb.add(c);
return { pops, near: [...nb], cities: CITIES.length, links: S.links.map((l) => l.id), day: S.day, puzzle: pzOn(), modal: modalOpen(), lost: !!S.lost,
clock: !!rule('clock'), core: S.core ? S.core.lv : 0, nets: NET_IDS.length, transit: S.transit.map((t) => t.id), pni: S.pni.map((p) => p.id), biz: S.biz.map((b) => b.id), offers: (S.offers || []).map((o) => o.id) };
};
// The buttons of one place on screen, after opening it.
const screen = (w) => {
if (w.where === 'modal') return btns($('modal'));
if (w.where === 'city') { UI.sel = { type: 'city', id: w.city }; UI.tab = w.tab; renderAll(); return btns($('panel')); }
if (w.where === 'link') { if (!S.links.some((l) => l.id === w.id)) return []; UI.sel = { type: 'link', id: w.id }; renderAll(); return btns($('panel')); }
if (w.where === 'dash') { $('dash').hidden = false; UI.dtab = w.tab; renderDash(); const b = btns($('dash')); return b; }
if (w.where === 'left') { UI.left = w.tab; UI.leftOpen = true; renderLeft(); return btns($('leftBody')); }
if (w.where === 'openLink') { if (!S.pops[w.a]) return []; openLinkModal(w.a, w.b); return modalOpen() ? btns($('modal')) : []; }
return [];
};
const press = (b) => { const e = call(b.act, b.args); return e; };
const close = () => { if (modalOpen()) closeModal(); $('dash').hidden = true; };
// ---------- things a player does with no button handy here ----------
const RS = ['spill at 80%', 'spill at 60%', 'protect customers', 'blackhole ddos'];
const script = (rules) => {
const L = [];
for (const r of rules) {
const [k, a, b] = r;
const net = NETS[a % NETS.length], t = S.transit.length ? NET[S.transit[a % S.transit.length].net] : net;
const pops = Object.keys(S.pops), l = S.links.length ? S.links[a % S.links.length] : null;
if (k === 0) L.push(RS[a % RS.length]);
else if (k === 1) L.push(`${b % 2 ? 'prefer' : 'avoid'} ${['pni', 'ix', 'transit'][b % 3]} for ${net.name.toLowerCase()}`);
else if (k === 2) L.push(`cap transit ${t.name.toLowerCase()} at ${10 + (b % 50)} gbps`);
else if (k === 3) L.push(`${b % 2 ? 'prefer' : 'avoid'} transit ${t.name.toLowerCase()}`);
else if (k === 4) L.push(`prepend transit ${t.name.toLowerCase()} ${1 + (b % 3)}`);
else if (k === 5 && pops.length) L.push(`drain ${CITY[pops[a % pops.length]].name.toLowerCase()}`);
else if (k === 6 && l) L.push(`avoid link ${CITY[l.a].name.toLowerCase()}-${CITY[l.b].name.toLowerCase()}`);
}
return L.join('\n');
};
const poke = (p) => {
const [k, a, b] = p;
if (k === 'te') { if (!S.te) return 'no te'; S.te.script = script(a); S.te.on = !!b; S.core = S.core || { lv: 0, run: 0 }; after(); return S.te.script; }
if (k === 'teOff') { if (S.te) { S.te.on = false; after(); } return ''; }
if (k === 'core') { if (S.core) { const v = a % (S.core.lv + 1); return call('coreSet', { v }); } return ''; }
if (k === 'fail') { S.nextFail = S.day + 1; return ''; }
if (k === 'event') { S.nextEvent = S.day + 1; return ''; }
if (k === 'cash') { S.cash += a; after(); return ''; }
if (k === 'lvl') { S.lvl = Math.min(LEVELS.length, (S.lvl || 0) + 1); after(); return ''; }
// Events that move routes, as the game's own events set them.
if (k === 'ev') {
const n = NETS[a % NETS.length].id;
if (b === 0) S.ev.leak = { net: n, until: S.day + 3 };
else if (b === 1) S.ev.rpki = { nets: [n, NETS[(a * 7) % NETS.length].id], until: S.day + 3 };
else if (b === 2) S.ev.depeer = S.day + 3;
else if (b === 3) { const x = Object.keys(S.ixp); if (x.length) { S.ev.ixdown = S.ev.ixdown || {}; S.ev.ixdown[x[a % x.length]] = S.day + 2; } }
else if (b === 4 && S.links.length) S.links[a % S.links.length].cut = S.day + 2;
else if (b === 5 && S.transit.length) S.transit[a % S.transit.length].down = S.day + 2;
// After an update brings networks: the courtesy route to them (sanitize() sets it on load).
else if (b === 7) S.grace = { until: S.day + 4, nets: [n, NETS[(a * 13) % NETS.length].id, NETS[(a * 31) % NETS.length].id], fresh: 0 };
else if (b === 8) S.krExit = S.day + 2;
else if (b === 6) { const p = Object.keys(S.pops); if (p.length > 1) S.pops[p[1 + (a % (p.length - 1))]].down = S.day + 2; }
after(); return '';
}
return 'unknown poke';
};
/* Actions on what the network already has, with arguments a button somewhere would give (some sit in collapsed
lists, or on screens the walker rarely reaches): one list of calls, the driver picks one. */
const direct = () => {
const o = [], add = (act, args) => { if (typeof ACT[act] === 'function') o.push({ act, args }); };
const pops = Object.keys(S.pops), PR = ['high', 'low', 'normal'];
for (const t of S.transit) { add('setPref', { kind: 't', id: t.id, v: PR[(t.id.length + S.day) % 3] }); add('setCarry', { id: t.id, on: !t.carry }); add('lagT', { id: t.id, d: (S.day % 3) ? 1 : -1 }); add('cancelTransit', { id: t.id, ok: true }); add('togglePrem', { id: t.id }); add('cleanPipe', { id: t.id }); }
for (const p of S.pni) { add('setPref', { kind: 'p', id: p.id, v: PR[(p.id.length + S.day) % 3] }); add('lagP', { id: p.id, d: (S.day % 3) ? 1 : -1 }); add('cancelPni', { id: p.id, ok: true }); add('sacrif', { id: p.id }); }
for (const x of Object.keys(S.ixp)) { add('setPref', { kind: 'ix', id: x, v: PR[(x.length + S.day) % 3] }); add('lagIx', { city: x, d: (S.day % 3) ? 1 : -1 }); add('peerAll', { city: x }); add('rs', { city: x }); add('cancelIx', { city: x, sure: true, ok: true });
for (const n of ixMembers(x).slice(0, 30)) add(ixSessions(x).has(n) ? 'unpeer' : 'peer', { city: x, net: n }); }
for (const c of pops) {
for (const x of ixIdsIn(c)) if (S.ixp[x] === undefined) add('buyIx', { city: x, tier: S.day % 3 });
for (const n of NETS) if (n.pops.includes(c)) { if (S.day % 2 === 0 || n.cache) add('pni', { city: c, net: n.id, tier: n.id.length % 3 }); if (n.cache) add('cache', { city: c, net: n.id }); }
for (const n of NETS) if (sellsAt(n, c) && (n.id.length + S.day) % 4 === 0) add('buyTransit', { city: c, net: n.id, tier: (S.day >> 2) % 3 });
add('addRouter', { city: c }); add('upRouter', { city: c, g: 1 + (S.day % 4) }); add('foundIx', { city: c }); add('launchBB', { city: c }); add('addSite', { city: c });
}
for (const c of S.caches) add('rmCache', { id: c.id });
for (const l of S.links) { add('setStandby', { id: l.id, on: !l.standby }); add('setMetric', { id: l.id, v: ['backup', 'avoid', 'prefer', 'normal'][(l.id.length + S.day) % 4] }); add('upLink', { id: l.id, tier: (S.day % 4) }); add('rmLink', { id: l.id }); add('addParallel', { id: l.id }); }
for (const b of S.offers || []) add('accept', { id: b.id });
for (const b of S.biz) add('dropBiz', { id: b.id });
for (const l of S.loanOffers || []) add('takeLoan', { id: l.id });
for (const l of S.loans || []) add('repayLoan', { id: l.id });
return o;
};
// ---------- time ----------
const day = () => { if (S.lost || !rule('clock')) return 'no clock'; if (modalOpen()) closeModal(); try { dayTick(); } catch (e) { return 'dayTick: ' + e.message; } return ''; };
// ---------- digests ----------
// Everything routing produced: R in full (with every link's R.ef), the flows (R.fl), and what the PNI lists read.
const routing = () => {
if (!R) return 'noR';
const r = JSON.stringify(R), fl = R.fl ? JSON.stringify(R.fl) : '';
let pf = '';
if (typeof pniFlows === 'function' && S && !pzOn()) for (const c of Object.keys(S.pops).slice(0, 4)) try { pf += c + JSON.stringify(pniFlows(c)); } catch (e) { pf += c + 'E' + e.message; }
return r + '\n' + fl + '\n' + pf;
};
const parts = () => { const o = {}; if (!R) return o; for (const k of Object.keys(R)) o[k] = JSON.stringify(R[k]); o.fl = R.fl ? JSON.stringify(R.fl) : ''; return o; };
const sDigest = () => JSON.stringify(S);
const sParts = () => { const o = {}; for (const k of Object.keys(S)) o[k] = JSON.stringify(S[k]); return o; };
/* After a move: what the move routed (a), routing again from the same state (b: on this checkout's page the kept
tables are used), and routing from that state with the cache off (c: RC.on=false where there is an RC). b and c
route from the same previous R; c's R is kept, as a plain compute would. */
const check = () => {
if (!S || pzOn() && !R) return { a: routing() };
const a = routing(), prior = R, keep = UI.rkR === prior;
compute(); const b = routing();
R = prior; const rc = typeof RC !== 'undefined';
if (rc) RC.on = false;
try { compute(); } finally { if (rc) RC.on = true; }
const c = routing();
if (keep) UI.rkR = R;
return { a, b, c };
};
window.__fz = { direct, lists, start, state, screen, press, close, poke, day, routing, parts, sDigest, sParts, check, errs, modalOpen };
})();

188
scripts/lib/route-fuzz.js Normal file
View file

@ -0,0 +1,188 @@
// Differential games (scripts/route-fuzz.js, tests/route-fuzz.test.js): two game pages in one process, a base page
// (main's) and this checkout's, play the same seeded game move for move. After every move and every day both must
// have routed identically (all of R, R.fl, R.ef, the PNI lists), and this checkout must route the same with the
// routing cache (RC) on and off. The whole game state S must match too, so the two games stay in lockstep.
// Every move is picked here, from what the base page offers right now, with the game's seed: the seed alone
// replays a game.
const fs = require('node:fs');
const path = require('node:path');
const { load } = require('../../tests/harness');
const FUZZ_JS = fs.readFileSync(path.join(__dirname, 'fuzz-page.js'), 'utf8');
function openPage(file, label) {
const g = load({ seed: 1, file });
g.ev(FUZZ_JS);
g.label = label;
g.call = (fn, ...args) => {
g.dom.window.__fzA = JSON.parse(JSON.stringify(args));
const out = g.ev(`JSON.stringify(__fz.${fn}(...window.__fzA))`);
return out === undefined ? undefined : JSON.parse(out);
};
return g;
}
// mulberry32: the driver's own numbers, apart from the pages' Math.random.
function rng(seed) {
let a = seed >>> 0;
const f = () => { a = (a + 0x6D2B79F5) >>> 0; let t = a; t = Math.imul(t ^ (t >>> 15), t | 1); t ^= t + Math.imul(t ^ (t >>> 7), t | 61); return ((t ^ (t >>> 14)) >>> 0) / 4294967296; };
f.int = (n) => Math.floor(f() * n);
f.pick = (a) => a[f.int(a.length)];
// A button by its action's weight (w), then any button of that action: 38 transit sellers count as one choice.
f.wp = (a, w) => { const g = new Map(); for (const x of a) { const l = g.get(x.act); if (l) l.push(x); else g.set(x.act, [x]); }
const ks = [...g.keys()]; let s = 0; for (const k of ks) s += w(g.get(k)[0]); let t = f() * s;
for (const k of ks) { t -= w(g.get(k)[0]); if (t < 0) return f.pick(g.get(k)); } return f.pick(g.get(ks[ks.length - 1])); };
f.w = (o) => { let s = 0; for (const k in o) s += o[k]; let x = f() * s; for (const k in o) { x -= o[k]; if (x < 0) return k; } return Object.keys(o)[0]; };
return f;
}
// The first key of two {key: json} maps that differs, with a short look at where.
function firstDiff(A, B) {
const keys = [...new Set([...Object.keys(A), ...Object.keys(B)])];
const out = [];
for (const k of keys) {
if (A[k] === B[k]) continue;
const a = A[k] || '', b = B[k] || '';
let i = 0; while (i < a.length && a[i] === b[i]) i++;
out.push({ key: k, at: i, base: a.slice(Math.max(0, i - 80), i + 120), head: b.slice(Math.max(0, i - 80), i + 120) });
if (out.length >= 4) break;
}
return out;
}
const DTABS = ['overview', 'cust', 'conn', 'traffic', 'infra', 'lease'];
const TABS = ['cust', 'up', 'peer', 'links'];
const LEFT = ['nets', 'noc', 'deals', 'goals'];
const PRESETS = ['freeze', 'oncall', 'bgp'];
// How often a button is pressed against the others on screen: what changes the network often, looking around rarely.
const BUILD = /^(buy\w*|pni\w*All|pni|peer|peerAll|unpeer|cache|cacheSite|rmCache|buildLink|buildPop|launchBB|accept|decline|dropBiz|takeLoan|repayLoan|lag[TPI]x?|setPref|setCarry|setStandby|setMetric|togglePrem|cleanPipe|sacrif|rs|foundIx|myIxUp|addRouter|rmRouter|upRouter|addSite|rmSite|siteSet|decomToggle|rmLink|upLink|addParallel|cancel\w*|upVxc|buyNni|rmNni|buildScrub|scrubUp|rmScrub|addGen|regen|addCh|rmCh|addPair|upSys|spares|layShip|crews|fixInc|seaPackage|subUpgrade|orderCable|orderDf|ownCab|opsHire|opsToggle|opsParam|opsCap|opsPop|opsTpl|opsNewPop|popStdSet|coreSet|teRecipe|dkOptOut|dkStay|utility\w*|listFiber|unlistFiber|leaseSwap|custGo|confGo|confSponsor|seatBuy|charter|cnPrem|applyCn|ad|ddosAuto|pz(Pop|RmPop|Link|AddPar|Tier|RmLink|RtAdd|RtRm|RtModel|Route|Via|Div)|setRoute|route|via|metricSet)$/;
const UIACT = /^(tab|dashTab|leftTab|\w*Sort|\w*Filter|pniGrp|trace\w*|focusCity|findGo|selLink|dashLink|logAll|tailTog|listMore|netFocus|netFilter|meetGo|phoneTab|dashClose|connF|connCity|connNet|connFor|custF|custCity|opsOpen|decomOpen|linkCheck)$/;
const DW = { launchBB: 4, accept: 4, buyTransit: 3, pni: 3, buyIx: 3, setPref: 4, cache: 2, peerAll: 2, setCarry: 2, takeLoan: 2 };
const weight = (b) => (BUILD.test(b.act) ? 8 : UIACT.test(b.act) ? 0.3 : 1);
/* One game. pages: {base, head}. Returns {seed, start, moves, days, log, div} where div is the first divergence (or null).
opts: moves (count, default 50 to 200 by seed), maxDays per day-move, onMove(i, move) */
function playGame(pages, seed, opts = {}) {
const { base, head } = pages;
const r = rng(seed * 2654435761);
const both = (fn, ...a) => [base.call(fn, ...a), head.call(fn, ...a)];
const L = base.call('lists');
const log = [], acts = {};
let div = null, days = 0;
const note = (m) => { log.push(m); };
const fail = (where, info) => { if (!div) div = { seed, where, move: log.length, last: log.slice(-6), ...info }; };
const same = (where, a, b) => { if (JSON.stringify(a) !== JSON.stringify(b)) fail(where, { base: a, head: b }); };
// Compare everything: routing on both pages (a: the move's, b: again, c: cache off) and the game state.
const compare = (where) => {
const [x, y] = both('check');
for (const k of ['a', 'b', 'c']) if (x[k] !== y[k]) { fail(where + ': routing (' + k + ') base vs head', { diff: firstDiff(base.call('parts'), head.call('parts')) }); return false; }
if (y.b !== y.c) { fail(where + ': head cached vs uncached', { diff: 'R from the kept tables differs from a fresh route' }); return false; }
const [s1, s2] = both('sDigest');
if (s1 !== s2) { fail(where + ': game state S', { diff: firstDiff(base.call('sParts'), head.call('sParts')) }); return false; }
return true;
};
// The start.
const kind = opts.save ? 'save' : r.w({ normal: 44, sandbox: 14, scenario: 20, weekly: 3, puzzle: 11, architect: 8 });
if (opts.save) for (const p of [base, head]) p.dom.window.__fzSave = opts.save;
const home = r() < 0.4 ? r.pick(L.special) : r.pick(L.homes);
const st = { kind, seed: (seed % 2147483646) + 1, name: 'Fuzz ' + seed, city: home,
preset: kind === 'sandbox' ? r.pick(['none', 'calm', 'normal', 'spicy']) : r.pick(PRESETS),
id: kind === 'scenario' ? r.pick(L.scn) : kind === 'puzzle' ? r.pick(L.pz) : kind === 'architect' ? r.pick(L.ar) : undefined };
const [s0, s1] = both('start', st);
same('start', s0, s1);
note('start ' + JSON.stringify(st));
if (!div) compare('start');
// A screen's buttons on both pages; the same list or a divergence.
const screen = (w) => { const [a, b] = both('screen', w); same('buttons of ' + JSON.stringify(w), a, b); return a || []; };
const press = (b, where) => { acts[b.act] = (acts[b.act] || 0) + 1; const [e1, e2] = both('press', b); same('press ' + where, e1, e2); note(where + ' ' + b.act + JSON.stringify(b.args) + (e1 ? ' !' + e1 : '')); };
// Answer whatever window opened, a few levels deep, then close it.
const modal = (bias) => {
for (let k = 0; k < 4 && !div; k++) {
if (!base.call('modalOpen')) return;
let bs = screen({ where: 'modal' });
if (bias) { const pref = bs.filter((b) => bias.test(b.act)); if (pref.length && r() < 0.7) bs = pref; }
const real = bs.filter((b) => !/^(closeModal|x)$/.test(b.act));
if (!real.length || r() < 0.12) break;
press(r.wp(real, weight), ' modal');
if (!div) compare('modal button');
}
both('close');
};
const N = opts.moves || 50 + (seed % 151);
for (let i = 0; i < N && !div; i++) {
const s = base.call('state');
if (s.lost) { note('bankrupt'); break; }
const mv = r.w(s.puzzle ? { city: 40, openLink: 30, link: 15, modal: 5 } : { city: 22, direct: 22, openLink: 13, link: 5, dash: 5, left: 3, poke: 8, day: s.clock ? 20 : 0, build: 4 });
if (opts.onMove) opts.onMove(i, mv);
if (mv === 'city' || mv === 'build') {
const city = mv === 'build' || !s.pops.length || r() < 0.3 ? (s.near.length ? r.pick(s.near) : r.pick(L.homes)) : r.pick(s.pops);
const tab = r.pick(TABS);
let bs = screen({ where: 'city', city, tab });
if (mv === 'build') { const p = bs.filter((b) => /^(buildPop|launchBB|opsNewPop|popStdSet)$/.test(b.act)); if (p.length) bs = p; }
if (bs.length) press(r.wp(bs, weight), `city ${city}/${tab}`);
if (!div) compare('city button');
modal();
} else if (mv === 'openLink') {
if (!s.pops.length) continue;
const a = r.pick(s.pops), b = r() < 0.75 && s.near.length ? r.pick(s.near) : r() < 0.5 && s.pops.length > 1 ? r.pick(s.pops) : r.pick(L.homes);
const bs = screen({ where: 'openLink', a, b });
note(`openLink ${a}-${b} (${bs.length})`);
if (bs.length) {
// Pick a route or a kind first sometimes (dark fiber, subsea seat, own cable...), then build.
const opt = bs.filter((x) => !/^(buildLink|closeModal)$/.test(x.act));
if (opt.length && r() < 0.4) { press(r.pick(opt), ' link option'); if (!div) compare('link option'); }
modal(/^(buildLink|pzLink|seaPackage|layShip|buyVxc|dfSys|seaDesign)$/);
} else both('close');
if (!div) compare('openLink');
} else if (mv === 'link') {
if (!s.links.length) continue;
const id = r.pick(s.links);
const bs = screen({ where: 'link', id });
if (bs.length) press(r.wp(bs, weight), 'link ' + id);
if (!div) compare('link button');
modal();
} else if (mv === 'dash' || mv === 'left') {
const w = mv === 'dash' ? { where: 'dash', tab: r.pick(DTABS) } : { where: 'left', tab: r.pick(LEFT) };
const bs = screen(w);
if (bs.length) press(r.wp(bs, weight), mv + ' ' + w.tab);
both('close');
if (!div) compare(mv + ' button');
modal();
} else if (mv === 'direct') {
const [a, b] = both('direct');
same('direct calls', a, b);
if (a.length) press(r.wp(a, (x) => DW[x.act] || (/^(cancel|rm|drop|unpeer)/.test(x.act) ? 0.4 : 1)), 'direct');
if (!div) compare('direct');
modal();
} else if (mv === 'modal') {
modal();
} else if (mv === 'poke') {
const k = r.w({ te: 3, teOff: 1, core: 2, fail: 2, event: 3, cash: 3, lvl: 2, ev: 4 });
let p;
if (k === 'te') { const rules = []; for (let j = 0, n = 1 + r.int(4); j < n; j++) rules.push([r.int(7), r.int(1000), r.int(1000)]); p = [k, rules, r() < 0.85]; }
else p = [k, k === 'cash' ? r.int(3e7) : r.int(1e6), r.int(9)];
const [e1, e2] = both('poke', p);
same('poke', e1, e2);
note('poke ' + JSON.stringify(p));
if (!div) compare('poke ' + k);
} else if (mv === 'day') {
const n = 1 + r.int(opts.maxDays || 12);
note('days ' + n);
for (let d = 0; d < n && !div; d++) {
const [e1, e2] = both('day');
same('day', e1, e2);
days++;
if (!div) compare('day ' + d);
// A window that opened over the day (an offer, a loan, a crisis) gets an answer.
if (!div && base.call('modalOpen')) modal();
}
}
}
return { seed, start: st, moves: log.length, days, log, div, acts, errors: [...base.errors, ...head.errors] };
}
module.exports = { openPage, playGame, rng };

84
scripts/route-fuzz.js Normal file
View file

@ -0,0 +1,84 @@
#!/usr/bin/env node
// Many random games, played move for move on main's page and on this checkout's, compared after every move and day
// (scripts/lib/route-fuzz.js). For refactors that promise identical routing.
//
// node scripts/route-fuzz.js 300 games against origin/main
// node scripts/route-fuzz.js --games 50 --from 1000 games with seeds 1000..1049
// node scripts/route-fuzz.js --seed 1234 -v one game, every move printed (to replay a divergence)
// node scripts/route-fuzz.js --save tests/fixtures/big-network.json --games 10 --moves 30 from a big network
// node scripts/route-fuzz.js --jobs 2 --base OLD.html --out fuzz.json
// Exit code 1 when any game diverged; each divergence is printed with its seed and its last moves.
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const { fork } = require('node:child_process');
const args = process.argv.slice(2);
const opt = (k, d) => { const i = args.indexOf(k); return i >= 0 ? args[i + 1] : d; };
const has = (k) => args.includes(k);
function child() {
const { openPage, playGame } = require('./lib/route-fuzz');
const base = openPage(opt('--base'), 'base'), head = openPage(opt('--head'), 'head');
process.on('message', (m) => {
if (m.stop) process.exit(0);
const t = Date.now();
let res;
try { res = playGame({ base, head }, m.seed, { moves: m.moves, save: m.save }); } catch (e) { res = { seed: m.seed, div: { where: 'driver crashed', error: String(e.stack || e) }, log: [] }; }
process.send({ seed: m.seed, ms: Date.now() - t, start: res.start, moves: res.moves, days: res.days, div: res.div, acts: res.acts, log: m.verbose || res.div ? res.log : null, errors: res.errors });
});
process.send({ ready: true });
}
function parent() {
const page = require('./lib/page');
const tmp = (n) => path.join(os.tmpdir(), `tubes-fuzz-${process.pid}-${n}.html`);
const BASE = opt('--base', null) || (() => { const f = tmp('base'); fs.writeFileSync(f, page.assembleRef(opt('--base-ref', 'origin/main'))); return f; })();
const HEAD = opt('--head', null) || (() => { const f = tmp('head'); fs.writeFileSync(f, page.assemble().html); return f; })();
const one = opt('--seed', null);
const from = +opt('--from', 1), games = one ? 1 : +opt('--games', 300);
const seeds = one ? [+one] : Array.from({ length: games }, (_, i) => from + i);
const jobs = Math.max(1, Math.min(+opt('--jobs', Math.max(1, os.cpus().length)), seeds.length));
const verbose = has('-v');
const moves = opt('--moves', null) ? +opt('--moves') : undefined;
// --save FILE: every game starts from this save (a plain one, or an export from the menu) instead of a new network.
const RAW = opt('--save', null) ? JSON.parse(fs.readFileSync(opt('--save'), 'utf8')) : null;
const save = RAW ? JSON.stringify(RAW.app === 'series-of-tubes' && RAW.S ? RAW.S : RAW) : undefined;
const results = [];
let next = 0, live = 0;
const t0 = Date.now();
const done = () => {
const div = results.filter((r) => r.div), tot = (k) => results.reduce((a, r) => a + (r[k] || 0), 0);
const kinds = {};
for (const r of results) if (r.start) { const k = r.start.kind + (r.start.kind === 'normal' || r.start.kind === 'sandbox' ? ':' + r.start.preset : ''); kinds[k] = (kinds[k] || 0) + 1; }
console.log(`\n${results.length} games, ${tot('moves')} moves, ${tot('days')} days in ${((Date.now() - t0) / 1000).toFixed(0)} s. Starts: ${JSON.stringify(kinds)}`);
const acts = {};for (const r of results) for (const k in r.acts || {}) acts[k] = (acts[k] || 0) + r.acts[k];
if (has('--acts')) console.log('Actions pressed: ' + Object.entries(acts).sort((a, b) => b[1] - a[1]).map(([k, n]) => k + ' ' + n).join(', '));
const errs = [...new Set(results.flatMap((r) => r.errors || []))];
if (errs.length) console.log('Page errors:\n' + errs.slice(0, 10).map((e) => ' ' + e.slice(0, 200)).join('\n'));
if (div.length) {
console.log(`\n${div.length} games diverged:`);
for (const r of div) console.log(JSON.stringify({ seed: r.seed, start: r.start, ...r.div }, null, 1).slice(0, 4000));
} else console.log('No divergence: identical routing and game state in every game, cached and uncached.');
const out = opt('--out', null);
if (out) fs.writeFileSync(out, JSON.stringify(results, null, 1));
process.exit(div.length ? 1 : 0);
};
for (let j = 0; j < jobs; j++) {
const c = fork(__filename, ['--child', '--base', BASE, '--head', HEAD], { stdio: ['ignore', 'inherit', 'inherit', 'ipc'] });
live++;
const feed = () => { if (next < seeds.length) c.send({ seed: seeds[next++], moves, verbose, save }); else c.send({ stop: true }); };
c.on('message', (m) => {
if (!m.ready) {
results.push(m);
const tag = m.div ? 'DIVERGED at ' + m.div.where : 'ok';
console.log(`seed ${m.seed} ${m.start ? m.start.kind + ' ' + (m.start.id || m.start.preset) + ' ' + m.start.city : ''}: ${m.moves} moves, ${m.days} days, ${(m.ms / 1000).toFixed(1)} s, ${tag}`);
if (verbose && m.log) console.log(m.log.map((l) => ' ' + l).join('\n'));
}
feed();
});
c.on('exit', () => { if (--live === 0) done(); });
}
}
if (has('--child')) child(); else parent();

View file

@ -15,3 +15,5 @@ function dijkstra(adj,src,wf,skip,stop){const n=adj.length,D=new Float64Array(n)
return {D,P};}
// The edges of a dijkstra() path back from city index B to A, and the cities on it (B first), as indices.
function walkI(P,A,B){const path=[],ni=[B];let v=B;while(v!==A){const l=P[v];path.push(l);v=l.ai===v?l.bi:l.ai;ni.push(v);}return {path,ni};}
// The same for compute()'s sender, with the marks it keeps on a path (full, its latency, its far-exit traffic) there from the start: one object shape.
function walkP(P,A,B){const path=[],ni=[B];let v=B;while(v!==A){const l=P[v];path.push(l);v=l.ai===v?l.bi:l.ai;ni.push(v);}return {path,ni,dead:0,ms:-1,fg:0};}

View file

@ -0,0 +1,52 @@
/* What compute() works out from the network's shape alone, kept from one call to the next: most days only demand moves,
and on a big network these are a third of the work. Each part has a key made of everything it is built from, and any
other key rebuilds it, so it is never stale:
- the graph (RC.gk): the POPs, the remote cities and every edge with its ends and weight, in the order compute() made
them. Kept: the shortest-path tables from every POP, ALONE and the islands (COMP).
- the exits (RC.xk): the graph key, RouteScript, the events that change who reaches whom, and every exit in order with
its kind, id, network, city, preference, rank, carry and prepend marks and what it can reach. Kept: okBy, what is
reachable, and each network's exit lists by rank (BRJ, with the lists networks share) with DSEL and RSET.
Only numbers are kept (exit and edge indices): compute() makes fresh exits, lists and paths from them every call,
since routing writes on those. RC.on=false (the equivalence tests) always rebuilds. */
const RC={on:true,gk:null,g:null,xk:null,x:null};
// The graph key: EA lists the edges in the order compute() made them (so adj, and the order Dijkstra settles ties, follow).
function rcGraphKey(pops,remote,EA){let k=pops.join(',')+'|'+remote.join(',');for(let i=0;i<EA.length;i++){const e=EA[i];k+='|'+e.ai+','+e.bi+','+e.w;}return k;}
// Shortest paths from every POP, from the cache or worked out (and kept when key is not null). P arrays hold today's edges.
function rcGraph(key,pops,adj,EA,NC,wW){const SPD={},SPP={},SDi=[],SPi=[],G=key!==null&&RC.gk===key?RC.g:null;
if(G){for(const s of pops){const i=CIDX[s],PI=G.PI[i],P=new Array(NC);for(let x=0;x<NC;x++){const k=PI[x];if(k>=0)P[x]=EA[k];}SDi[i]=SPD[s]=G.D[i];SPi[i]=SPP[s]=P;}
return {SPD,SPP,SDi,SPi,ALONE:G.ALONE,COMP:G.COMP,NCOMP:G.NCOMP};}
// ALONE[i]: no other city lies within 1e-6 of POP i (so only its own exits can be at distance 0 from it).
const ALONE=new Uint8Array(NC),PIs=[];
for(const s of pops){const i=CIDX[s],r=dijkstra(adj,i,wW);SDi[i]=SPD[s]=r.D;SPi[i]=SPP[s]=r.P;ALONE[i]=1;for(let x=0;x<NC;x++)if(x!==i&&r.D[x]<1e-6)ALONE[i]=0;
if(key!==null){const PI=PIs[i]=new Int32Array(NC).fill(-1);for(let x=0;x<NC;x++)if(r.P[x])PI[x]=r.P[x].i;}}
// COMP[i]: the island city i is on (-1: no POP reaches it). Links run both ways, so reaching is mutual.
const COMP=new Int32Array(NC).fill(-1);let NCOMP=0;for(const s of pops){const i=CIDX[s];if(COMP[i]>=0)continue;const D=SDi[i],k=NCOMP++;for(let x=0;x<NC;x++)if(D[x]<Infinity)COMP[x]=k;}
if(key!==null){RC.gk=key;RC.g={D:SDi.slice(),PI:PIs,ALONE,COMP,NCOMP};RC.xk=null;RC.x=null;}
return {SPD,SPP,SDi,SPi,ALONE,COMP,NCOMP};}
/* The exits key. What an exit reaches: a transit port every network its flags allow (the flags are in the key), a PNI its
network's cone (fixed at load), a cache its network, an exchange (or the courtesy route) its sessions with their cones,
each marked when it is reached only through a cone (^). */
function rcExitKey(gk,E,flags){let k=gk+'#'+flags;
for(let i=0;i<E.length;i++){const e=E[i];k+='\n'+e.k+'|'+e.id+'|'+e.net+'|'+e.city+'|'+e.pref+'|'+e.rank+'|'+(e.carry?1:0)+(e.prep?1:0)+'|';
if(e.k==='ix'){const d=e.dir;e.nets.forEach(n=>{k+=n+(d&&!d.has(n)?'^,':',');});}else if(e.k==='c')k+=e.nets[0];}
return k;}
// The exits' lists, with a sort key: an exit's index in E.
const rcIx=(L,E)=>{const a=new Int32Array(L.length);for(let i=0;i<L.length;i++)a[i]=L[i].xi;return a;};
const rcEx=(a,E)=>{const L=new Array(a.length);for(let i=0;i<a.length;i++)L[i]=E[a[i]];return L;};
// Each list knows whether it holds a preferred exit (hi) and its exits by city (at), in list order.
function exIdx(L){L.hi=L.some(e=>e.hp===0);L.at=new Map();for(const e of L){const h=L.at.get(e.ci);if(h)h.push(e);else L.at.set(e.ci,[e]);}}
// What reaching networks gave R (taken right after compute() worked it out, before anything else adds to R).
function rcReachTake(R2){return {reachable:{...R2.reachable},nt:{...R2.nt},cone:{...R2.cone},reach:R2.reach,reachNT:R2.reachNT};}
function rcReachPut(X,R2){const r=X.reach;Object.assign(R2.reachable,r.reachable);Object.assign(R2.nt,r.nt);Object.assign(R2.cone,r.cone);R2.reach=r.reach;R2.reachNT=r.reachNT;}
// Keep what was built (on a miss): okBy, the exit lists (each shared list once), each network's lists by rank, DSEL, RSET.
function rcExitKeep(key,E,okBy,reach,LIST,BRJ,DSEL,RSET){const ok=[];for(let j=0;j<NET_IDS.length;j++)ok[j]=rcIx(okBy[NET_IDS[j]],E);
const id=new Map(),lists=[];for(const V of LIST.values()){id.set(V,lists.length);
if(V.cs){const cs=[];for(let k=0;k<V.cs.length;k++)if(V.cs[k])cs[k]=rcIx(V.cs[k],E);lists.push({cs,a:null});}else lists.push({cs:null,a:rcIx(V,E)});}
const brj=BRJ.map(m=>{const o=[];for(const r in m)o.push(r,id.get(m[r]));return o;});
RC.xk=key;RC.x={ok,reach,lists,brj,DSEL,RSET:[...RSET]};}
// Fresh lists for today's exits from a kept build.
function rcExitLists(X,E,BRJ,DSEL,RSET){const V=X.lists.map(d=>{if(d.cs){const cs={cs:[]};for(let k=0;k<d.cs.length;k++)if(d.cs[k]){const s=rcEx(d.cs[k],E);exIdx(s);cs.cs[k]=s;}return cs;}
const L=rcEx(d.a,E);exIdx(L);return L;});
for(let j=0;j<X.brj.length;j++){const o=X.brj[j],m={};for(let k=0;k<o.length;k+=2)m[o[k]]=V[o[k+1]];BRJ[j]=m;}
for(const [r,a] of X.DSEL)DSEL.set(r,a);for(const r of X.RSET)RSET.add(r);}
function rcOkBy(X,E){const okBy={};for(let j=0;j<NET_IDS.length;j++)okBy[NET_IDS[j]]=rcEx(X.ok[j],E);return okBy;}

View file

@ -19,58 +19,70 @@ function efBuild(R2,L,ACN,ACD,NKN,NC,NN){const efg={};for(const [id,i,t] of L){l
function flDefine(R2,c){const {NF,FREM,FCI,FNJ,FCA,FBZ,FGT,FGI,FGP,FGH,FTI,FII,E,CNB,NCN}=c;let fl=null;Object.defineProperty(R2,'flc',{value:c});
Object.defineProperty(R2,'fl',{get(){if(!fl){fl=new Array(NF);for(let i=0;i<NF;i++){const ci=FCI[i],j=FNJ[i];
fl[i]={c:CITIES[ci].id,n:NET_IDS[j],rem:FREM[i],cache:FCA[i]===1,junk:false,bz:FBZ[i]===1,ci,cn:CNB[ci],ncn:NCN[j],nj:j,nr:j,gt:FGT[i],gi:FGI[i],gp:FGP[i],gh:FGH[i],ti:FTI[i]<0?null:E[FTI[i]].id,ii:FII[i]<0?null:E[FII[i]].id};}}return fl;}});}
/* Every exit has the same fields in the same order, whatever its kind, so the routing loop reads them from one object shape
(exits of six shapes made each property read there a slow lookup). */
function mkEx(k,id,city,cap,rank,pref,lab,ok,nets,net){return {k,net,id,city,lab,cap,used:0,rank,pref,ok,nets,dir:null,clean:false,prem:false,carry:false,prep:false,sac:false,pnc:false,
ek:'',J:0,byC:null,desc:null,dup:false,told:false,hp:1,ci:0,cn:false,vis:0,ts:-1,xi:0,kx:EXK.indexOf(k),fac:undefined};}
// The exit kinds by number (mkEx's kx), for what routing adds up per kind in typed arrays.
const EXK=['t','ix','p','c'];
function compute(){RW.gen++;
const day=S.day, R2=emptyR(), pops=Object.keys(S.pops).filter(popUp), NC=CITIES.length;
// The graph works on city indices: adj[i] lists [neighbour, edge] in S.links order.
const remote=remoteCities(),adj=new Array(NC); pops.concat(remote).forEach(p=>adj[CIDX[p]]=[]);
const edges={},TRX=teRules(),live=[];let NE=0;R2.bizDem={};R2.bizSrv={};
const edges={},EA=[],TRX=teRules(),live=[];let NE=0;R2.bizDem={};R2.bizSrv={};
for(const l of S.links){const ai=CIDX[l.a],bi=CIDX[l.b];if(linkDown(l)||!adj[ai]||!adj[bi])continue;live.push(l);const key=l.a<l.b?l.a+'|'+l.b:l.b+'|'+l.a;let e=edges[key];
// The metric belongs to the city pair (a preset or a custom one); links on standby add capacity that fills only after the others (#122).
if(!e){e=edges[key]={id:key,a:l.a,b:l.b,ai,bi,i:NE++,km:l.km,cap:0,scap:0,w:Infinity,lat:Infinity,slat:Infinity,links:[],res:0,ld:0,tr:null,pl:0,f:TRX&&TRX.avoid.has(key)?60:1};adj[ai].push([bi,e]);adj[bi].push([ai,e]);}
if(!e){e=edges[key]={id:key,a:l.a,b:l.b,ai,bi,i:NE++,km:l.km,cap:0,scap:0,w:Infinity,lat:Infinity,slat:Infinity,links:[],res:0,ld:0,tr:null,pl:0,jk:0,f:TRX&&TRX.avoid.has(key)?60:1};EA.push(e);adj[ai].push([bi,e]);adj[bi].push([ai,e]);}
const c=capOf(l),lt=linkLat(l);e.cap+=c;e.links.push(l);e.w=Math.min(e.w,pairW(key,l.km)*e.f);if(l.standby){e.scap+=c;e.slat=Math.min(e.slat,lt);}else e.lat=Math.min(e.lat,lt);}
for(const k in edges){const e=edges[k];if(e.lat===Infinity)e.lat=e.slat;}
// Shortest paths from every POP: SPD[pop] the distance to each city index (Infinity: no path), SPP[pop] the edge it arrives by.
// ALONE[i]: no other city lies within 1e-6 of POP i (so only its own exits can be at distance 0 from it).
const SPD={},SPP={},SDi=[],SPi=[],ALONE=new Uint8Array(NC),wW=e=>e.w,wLat=e=>e.lat+0.05;
for(const s of pops){const i=CIDX[s],r=dijkstra(adj,i,wW);SDi[i]=SPD[s]=r.D;SPi[i]=SPP[s]=r.P;ALONE[i]=1;for(let x=0;x<NC;x++)if(x!==i&&r.D[x]<1e-6)ALONE[i]=0;}
// COMP[i]: the island city i is on (-1: no POP reaches it). Links run both ways, so reaching is mutual.
const COMP=new Int32Array(NC).fill(-1);let NCOMP=0;for(const s of pops){const i=CIDX[s];if(COMP[i]>=0)continue;const D=SDi[i],k=NCOMP++;for(let x=0;x<NC;x++)if(D[x]<Infinity)COMP[x]=k;}
const E=[], ev=S.ev;
// Shortest paths from every POP: SPD[pop] the distance to each city index (Infinity: no path), SPP[pop] the edge it arrives by;
// ALONE and the islands (COMP). Kept between calls while the graph is the same (29-route-cache.js).
const wW=e=>e.w,wLat=e=>e.lat+0.05,RCON=RC.on,GK=RCON?rcGraphKey(pops,remote,EA):null,
{SPD,SPP,SDi,SPi,ALONE,COMP,NCOMP}=rcGraph(GK,pops,adj,EA,NC,wW);
const ev=S.ev;
const depeer=ev.depeer>day||scnRule('spat'), leak=ev.leak&&ev.leak.until>day?ev.leak.net:null;
// Sender pays (Gangnam Style Peering): no carrier hands traffic to the Korean incumbents for you, only a paid PNI does.
const snd=scnRule('sender');
// Stages with loops run as functions of their own (see rankPass below), here the exits.
const E=(()=>{const E=[];
for(const t of S.transit){if(!popUp(t.city)||t.down>day||!liveN(t.city,t.site,t.n))continue;
// AS-path prepending (#96) moves part of the port to a last-resort exit of its own.
const cap=Math.min(liveCap(PORT[t.tier].g,t.city,t.site,t.n)*lagF(t),TRX&&TRX.cap[t.net]!==undefined?TRX.cap[t.net]:Infinity),pf=TRX&&TRX.prep[t.net]?PREPEND_F[TRX.prep[t.net]]:0;
const ex={k:'t',net:t.net,clean:!!t.clean,prem:premOn(t),pref:t.pref||'normal',carry:!!t.carry,lab:labOf(N_('common.net_transit'),'net',NET[t.net].name),id:t.id,city:t.city,cap:cap*(1-pf),used:0,rank:3,ok:n=>!(depeer&&t.net==='cog'&&n==='ggl')&&n!==leak&&!(snd&&NET[n].kr)};
E.push(ex);if(pf)E.push({...ex,cap:cap*pf,prep:true});}
const ex=mkEx('t',t.id,t.city,cap*(1-pf),3,t.pref||'normal',labOf(N_('common.net_transit'),'net',NET[t.net].name),n=>!(depeer&&t.net==='cog'&&n==='ggl')&&n!==leak&&!(snd&&NET[n].kr),null,t.net);
ex.clean=!!t.clean;ex.prem=premOn(t);ex.carry=!!t.carry;E.push(ex);if(pf){const x=Object.assign(mkEx(),ex);x.cap=cap*pf;x.prep=true;E.push(x);}}
// `nets`: every network ok() can say yes to, so it need not be asked about the other hundred.
for(const c in S.ixp){const ct=ixCity(c);if(!(popUp(ct)||remote.includes(ct))||(S.ev.ixdown&&S.ev.ixdown[c]>day)||!(S.myIx[c]?myIxLive(c):liveN(ct,(S.ixSite||{})[c],(S.ixn||{})[c])))continue;const set=ixSessions(c);R2.sessions+=set.size;
// A peer announces its customers too (#85): the session reaches its cone, after the networks peering directly.
let all=set;for(const id of set)if(DOWN[id].length){if(all===set)all=new Set(set);for(const x of DOWN[id])all.add(x);}
E.push({k:'ix',pref:(S.ixPref&&S.ixPref[c])||'normal',lab:labOf(N_('common.ix_peering'),'ix',ixName(c)),id:c,city:ct,cap:ixCap(c),used:0,rank:2,ok:n=>all.has(n),nets:all,dir:all===set?null:set});}
for(const p of S.pni){if(!pniOn(p)||!popUp(p.city)||!liveN(p.city,p.site,p.n))continue;R2.sessions++;E.push({k:'p',sac:!!p.sacrif,pnc:!!NET[p.net].cn,pref:p.pref||'normal',lab:labOf(N_('common.pni_with_net'),'net',NET[p.net].name),id:p.id,city:p.city,cap:liveCap(pniG(p),p.city,p.site,p.n)*lagF(p)*(p.sacrif?0.5:1),used:0,rank:1,net:p.net,ok:n=>CONE_S[p.net].has(n),nets:CONE[p.net]});}
for(const c of S.caches){if(!popUp(c.city)||siteDown(c.city,c.site==='b'?'b':'a'))continue;E.push({k:'c',pref:'normal',lab:labOf(N_('common.name_cache'),'name',NET[c.net].cache.name),id:c.id,city:c.city,cap:cacheG(c),used:0,rank:0,ok:n=>n===c.net,nets:[c.net]});}
const x=mkEx('ix',c,ct,ixCap(c),2,(S.ixPref&&S.ixPref[c])||'normal',labOf(N_('common.ix_peering'),'ix',ixName(c)),n=>all.has(n),all);x.dir=all===set?null:set;E.push(x);}
for(const p of S.pni){if(!pniOn(p)||!popUp(p.city)||!liveN(p.city,p.site,p.n))continue;R2.sessions++;
const x=mkEx('p',p.id,p.city,liveCap(pniG(p),p.city,p.site,p.n)*lagF(p)*(p.sacrif?0.5:1),1,p.pref||'normal',labOf(N_('common.pni_with_net'),'net',NET[p.net].name),n=>CONE_S[p.net].has(n),CONE[p.net],p.net);x.sac=!!p.sacrif;x.pnc=!!NET[p.net].cn;E.push(x);}
for(const c of S.caches){if(!popUp(c.city)||siteDown(c.city,c.site==='b'?'b':'a'))continue;E.push(mkEx('c',c.id,c.city,cacheG(c),0,'normal',labOf(N_('common.name_cache'),'name',NET[c.net].cache.name),n=>n===c.net,[c.net]));}
// After an update adds networks, a free courtesy route reaches the new ones you have no other way to (#46).
R2.graceNets=0;if(S.grace&&S.grace.until>day){const gs=new Set(S.grace.nets.filter(n=>NET[n]&&!E.some(e=>e.k!=='c'&&e.ok(n)))),hc=popUp(S.home)?S.home:pops.find(popUp);
R2.graceNets=gs.size;if(gs.size&&hc)E.push({k:'ix',pref:'normal',lab:tr('compute.courtesy_route_to_new_networks'),id:'~grace',city:hc,cap:1e9,used:0,rank:2,ok:n=>gs.has(n),nets:gs});}
{const rp=ev.rpki&&ev.rpki.until>day?new Set(ev.rpki.nets):null;if(rp)for(const e of E){if(e.k==='c')continue;const o=e.ok;e.ok=n=>!rp.has(n)&&o(n);}}
R2.graceNets=gs.size;if(gs.size&&hc)E.push(mkEx('ix','~grace',hc,1e9,2,'normal',tr('compute.courtesy_route_to_new_networks'),n=>gs.has(n),gs));}
return E;})();
const rp=ev.rpki&&ev.rpki.until>day?new Set(ev.rpki.nets):null;if(rp)for(const e of E){if(e.k==='c')continue;const o=e.ok;e.ok=n=>!rp.has(n)&&o(n);}
// Who reaches whom and each network's exit lists, kept while the exits are the same (XC, 29-route-cache.js).
const XK=RCON?rcExitKey(GK,E,(depeer?1:0)+'|'+leak+'|'+(snd?1:0)+'|'+(rp?ev.rpki.nets.join(','):'')+'|'+(TRX?S.te.script:'')):null,XC=XK!==null&&RC.xk===XK?RC.x:null;
// coneOf: does exit e reach network n only through a peer's customer cone (#85)?
const coneOf=(e,n)=>e.k==='p'?e.net!==n:!!e.dir&&!e.dir.has(n);
// ok() cannot change during one compute, so ask each exit once per network, not once per flow and rank.
// okBy[net] lists the exits that reach it in E's order, like E.filter(e=>e.ok(net)).
const okBy={};for(const id of NET_IDS)okBy[id]=[];
const okBy=XC?rcOkBy(XC,E):{};if(!XC)for(const id of NET_IDS)okBy[id]=[];
const okAdd=(e,n)=>{const L=okBy[n];if(L&&L[L.length-1]!==e&&e.ok(n))L.push(e);};
// TSL: a slot per transit port id, for what it carries to each network (R.tNet, added up in TNA).
const ekN={},TSL=new Map();for(const e of E){e.ek=e.k+':'+e.id;ekN[e.ek]=(ekN[e.ek]||0)+1;}
const ekN={},TSL=new Map();(()=>{for(const e of E){e.ek=e.k+':'+e.id;ekN[e.ek]=(ekN[e.ek]||0)+1;}
// dup: shares its key with another exit (a hand-edited save), so R.egDesc shows whichever pushed last.
for(const e of E){e.J=0;e.byC=null;e.desc=null;e.dup=ekN[e.ek]>1;e.told=false;e.hp=e.pref==='high'?0:1;e.ci=CIDX[e.city];e.cn=cnCity(e.city);e.vis=0;e.ts=-1;if(e.k==='t'){let t=TSL.get(e.id);if(t===undefined)TSL.set(e.id,t=TSL.size);e.ts=t;}
for(let i=0;i<E.length;i++){const e=E[i];e.xi=i;e.J=0;e.byC=null;e.desc=null;e.dup=ekN[e.ek]>1;e.told=false;e.hp=e.pref==='high'?0:1;e.ci=CIDX[e.city];e.cn=cnCity(e.city);e.vis=0;e.ts=-1;if(e.k==='t'){let t=TSL.get(e.id);if(t===undefined)TSL.set(e.id,t=TSL.size);e.ts=t;}
if(XC)continue;
// Index loops or forEach (same order): for…of here made an iterator result per exit and network.
const ns=e.nets||NET_IDS;if(Array.isArray(ns)){for(let q=0;q<ns.length;q++)okAdd(e,ns[q]);}else ns.forEach(n=>okAdd(e,n));}
for(const n of NETS){let any=false,nt=false;for(const e of okBy[n.id])if(e.rank>0){any=true;if(e.k!=='t'){nt=true;break;}}R2.reachable[n.id]=any;if(any)R2.reach+=NSH[n.id];if(nt){R2.nt[n.id]=1;R2.reachNT+=NSH[n.id];
if(XC)rcReachPut(XC,R2);else for(const n of NETS){let any=false,nt=false;for(const e of okBy[n.id])if(e.rank>0){any=true;if(e.k!=='t'){nt=true;break;}}R2.reachable[n.id]=any;if(any)R2.reach+=NSH[n.id];if(nt){R2.nt[n.id]=1;R2.reachNT+=NSH[n.id];
// Reached without transit only through the customers of networks you peer with (#85). R.cone[id]: the peer that
// carries it, the first PNI's network or else the nearest upstream that is a member of an exchange you are on.
if(!okBy[n.id].some(e=>(e.k==='p'||e.k==='ix')&&!coneOf(e,n.id)))R2.cone[n.id]=coneBy(okBy[n.id],n.id);}}
if(!okBy[n.id].some(e=>(e.k==='p'||e.k==='ix')&&!coneOf(e,n.id)))R2.cone[n.id]=coneBy(okBy[n.id],n.id);}}})();
const RCH=XK!==null&&!XC?rcReachTake(R2):null;
const cacheNets=new Set(S.caches.filter(c=>popUp(c.city)).map(c=>c.net));
const viral=(ev.viral>day?1.25:1)*(ev.php&&ev.php>day?0.7:1);
/* Flows are columns, not objects (tens of thousands of them on a big network): flow F starts in city FCI[F] for network
@ -80,7 +92,7 @@ function compute(){RW.gen++;
const bizBw={};for(const b of S.biz){const T=BIZ_T[b.type];if(!T.l2&&!T.fiber&&(!T.event||eventLive(b))){const v=bizBw[b.city];bizBw[b.city]=(v===undefined?0:v)+b.bw;}}
// Traffic shares by network index, and their sums, for each share table (and with Russia's throttle).
const shArr=new Map(),shSum=new Map(),DEMTO=new Float64Array(NN),loc=scnRule('local');
for(const c of Object.keys(S.pops)){
(()=>{for(const c of Object.keys(S.pops)){
const p=S.pops[c],ci=CIDX[c],cn=cnCity(c),SH=cn?NSH_CN:ruCity(c)?NSH_RU:CITY[c].kr&&S.krExit?NSH_KRX:NSH;const eye=(p.bb?p.bb.d:0)*viral*(cn&&ev.golden>day?1.4:1);const biz=bizBw[c]??0;const d0=eye+biz;
// Goggle video is throttled in Russia (#70): people there get half of it, so they ask for half. It is not
// traffic you failed to deliver, and it does not cross your links only to be thrown away.
@ -97,7 +109,7 @@ function compute(){RW.gen++;
for(let q=0;q<(sp?2:1);q++){const x=sp?(q?a*(1-bf):a*bf):a,bz=sp&&!q;if(x<=0)continue;if(cn2){flow(ci,j,x*0.85,true,bz);flow(ci,j,x*0.15,false,bz);}
else flow(ci,j,x,false,bz);}}
R2.bizDem[c]=biz*d/(d0||1);R2.bizSrv[c]=0;
}
}})();
if(Object.keys(S.pops).length)for(let j=0;j<NN;j++)R2.demTo[NETS[j].id]=DEMTO[j];
// What each flow got by kind of exit (FGT transit, FGI exchange, FGP PNI elsewhere, FGH PNI with that network here) and
// the last transit and exchange exit it used (FTI, FII: an index into E, -1 for none). Fresh each call: R.fl reads them.
@ -144,7 +156,7 @@ function compute(){RW.gen++;
if(old&&old.every(r=>r.keys.every(k=>edges[k]))){const prev=old.map(r=>{const path=r.keys.map(k=>edges[k]);return {path,ni:r.ns.map(c=>CIDX[c]),share:r.share};});
if(prev.reduce((x,r)=>x+cost(r),0)<=pick2.reduce((x,r)=>x+cost(r),0)*1.1)out=prev;}
R2.l2route[key]=out.map(r=>({keys:r.path.map(e=>e.id),ns:cityIds(r.ni),share:r.share}));return out;};
for(const b of S.biz){if(!BIZ_T[b.type].l2)continue;
(()=>{for(const b of S.biz){if(!BIZ_T[b.type].l2)continue;
// An SD-WAN is hub-and-spoke: one circuit from the first site to each of the others.
const pairs=b.sites?b.sites.slice(1).map(x=>[b.sites[0],x]):[[b.city,b.city2]];let minF=1,maxLat=0,tot=0,noPath=false;
for(const [A,B] of pairs){let amt=0;
@ -158,7 +170,7 @@ function compute(){RW.gen++;
minF=Math.min(minF,amt/b.bw);tot+=amt;}
R2.l2lat[b.id]=noPath?Infinity:maxLat;R2.l2[b.id]=b.bw*minF;R2.egT.l2=(R2.egT.l2||0)+tot;
// Same answer as diversePaths(), which would rebuild today's live links for every customer.
if((b.reqs||[]).includes('diverse')){const k=b.city+'>'+b.city2;R2.div[b.id]=divMemo[k]??(divMemo[k]=disjointI(b.city,b.city2));}}
if((b.reqs||[]).includes('diverse')){const k=b.city+'>'+b.city2;R2.div[b.id]=divMemo[k]??(divMemo[k]=disjointI(b.city,b.city2));}}})();
/* Floods ride the backbone ahead of everyone's traffic (25-ddos-flood.js). haul: up to g from city A to POP B over the
shortest path, as far as links and routers have room. */
const msOf=(A,B)=>{const Bi=CIDX[B],Ai=CIDX[A];return SPD[B]&&SPD[B][Ai]<Infinity?walkI(SPP[B],Bi,Ai).path.reduce((n,e)=>n+e.lat,0):0;};
@ -172,7 +184,7 @@ function compute(){RW.gen++;
if(c0===false)return null;if(c0&&c0.path.every(l=>l.res>1e-3)&&c0.ni.every(i=>!HAS[i]||NRES[i]>1e-3))return c0;
const {D,P}=dijkstra(adj,src,wW,(v,e)=>e.res<=1e-3||(HAS[v]&&NRES[v]<=1e-3)||(CORE&&OUT[v]&&v!==dst),dst);
if(D[dst]===Infinity||D[dst]>REACH*base+60){teCache.set(key,false);return null;}
const Q=walkI(P,src,dst);teCache.set(key,Q);return Q;};
const Q=walkP(P,src,dst);teCache.set(key,Q);return Q;};
/* A network in a peer's customer cone, reached through that peer (#85): after every network you peer with directly
(its AS path is one hop longer) and before transit. */
const base=(e,n)=>e.pref==='low'?4:e.rank>0&&e.rank<3&&coneOf(e,n)?CONE_RANK:e.rank;
@ -190,16 +202,20 @@ function compute(){RW.gen++;
// Each network's exits by the rank they route at (RouteScript can move an exit per network), in E's order.
// Each list also knows whether it holds a preferred exit (hi) and its exits by city (at), in list order.
// With several islands (POPs no backbone joins), each list is split by island: a flow only scans exits it can reach.
const idx=L=>{L.hi=L.some(e=>e.hp===0);L.at=new Map();for(const e of L){const h=L.at.get(e.ci);if(h)h.push(e);else L.at.set(e.ci,[e]);}};
// Networks with the same exits at a rank share one list (LIST, by content), so what one flow learns serves them all.
const LIST=new Map();for(let i=0;i<E.length;i++)E[i].xi=i;
const byRank={},BRJ=[],RSET=new Set();for(let j=0;j<NET_IDS.length;j++){const id=NET_IDS[j],m=BRJ[j]=byRank[id]={},dm=dirOf(id);for(const e of okBy[id]){const r=TR?eff(e,id):base(e,id);RSET.add(r);(m[r]||(m[r]=[])).push(e);
// A kept build (XC) gives the same lists, made fresh for today's exits.
const LIST=new Map(),idx=exIdx,byRank={},BRJ=[],RSET=new Set();if(XC)rcExitLists(XC,E,BRJ,DSEL,RSET);else(()=>{for(let j=0;j<NET_IDS.length;j++){const id=NET_IDS[j],m=BRJ[j]=byRank[id]={},dm=dirOf(id);for(const e of okBy[id]){const r=TR?eff(e,id):base(e,id);RSET.add(r);(m[r]||(m[r]=[])).push(e);
if(dm&&r===3&&e.k==='t'){const t=dm.get(e.net);if(t){const d=DIRECT_RANK[t][e.carry?0:1];let ds=DSEL.get(d);if(!ds)DSEL.set(d,ds=new Uint8Array(NET_IDS.length));ds[j]=1;RSET.add(d);(m[d]||(m[d]=[])).push(e);}}}
for(const r in m){const L=m[r],key=L.map(e=>e.xi).join(','),o=LIST.get(key);if(o){m[r]=o;continue;}
let V=L;if(NCOMP>1){const cs=[];for(const e of L){const k=COMP[e.ci];if(k>=0)(cs[k]||(cs[k]=[])).push(e);}for(const s of cs)if(s)idx(s);V={cs};}else idx(L);LIST.set(key,V);m[r]=V;}}
if(RCH)rcExitKeep(XK,E,okBy,RCH,LIST,BRJ,DSEL,RSET);})();
// The flow being routed, and from its POP: distances, arriving edges and the paths walked so far, by city index.
// F: its index; fci, fnj: its city and network index.
let F=0,fci=0,fnj=0,Dv=null,Pv=null,PP=null,vis=0;const walked=[],G=[],REST=[],NEG=[];
let F=0,fci=0,fnj=0,Dv=null,Pv=null,PP=null,vis=0;const walked=[],G=[],REST=[],NEG=[];let GN=0;
/* What each kind of exit served, in all (EGT) and per network (NEA, by network index and kind): numbers in typed arrays,
where a sum by key would box every new value. EGO and NEO keep the order each first got traffic, which is the order
R.egT and R.netEg list them in; both are written back after routing. */
const EGT=new Float64Array(4),EGO=[],NEA=new Float64Array(NN*4),NEO=[];
// An exit the flow can still try: not visited yet, not full when the flow started, reachable, and not on a saturated path.
const usable=e=>e.vis!==vis&&e.used<e.cap&&Dv[e.ci]<Infinity&&!gone(e),byDist=(a,b)=>a.hp-b.hp||Dv[a.ci]-Dv[b.ci];
// Push up to `want` along one path to exit e; returns what fitted. Index loops: for…of allocates an iterator until optimised. Capacity only shrinks while routing, so a
@ -220,8 +236,8 @@ function compute(){RW.gen++;
// Backhaul to transit: users notice the round trip to the exit. Up to 20 ms (about 1,500 km) is free, then
// it gets worse, and past 50 ms broadband and internet services count as bad service (#…). Under sender pays,
// Korean users notice any exit far away, transit or not, and much sooner.
if(path.length&&(e.k==='t'||snd&&CITIES[ci].kr)){let ms=P.ms;if(ms===undefined){ms=0;for(let k=0;k<PN;k++)ms+=path[k].lat;P.ms=ms;}const rtt=2*ms,kl=snd&&CITIES[ci].kr;
if(rtt>(kl?KR_LAT_OK:LAT_OK)){LATP[ci]+=ok*(kl?krLatLoss(rtt):latLoss(rtt));if(!P.fg){P.fg=0;FARL.push(P,ci,e.ci);}P.fg+=ok;}}R2.egT[e.k]=(R2.egT[e.k]||0)+ok;const ne=NEG[nr]||(NEG[nr]=R2.netEg[NET_IDS[nr]]={c:0,p:0,ix:0,t:0});ne[e.k]+=ok;
if(path.length&&(e.k==='t'||snd&&CITIES[ci].kr)){let ms=P.ms;if(ms<0){ms=0;for(let k=0;k<PN;k++)ms+=path[k].lat;P.ms=ms;}const rtt=2*ms,kl=snd&&CITIES[ci].kr;
if(rtt>(kl?KR_LAT_OK:LAT_OK)){LATP[ci]+=ok*(kl?krLatLoss(rtt):latLoss(rtt));if(!P.fg){P.fg=0;FARL.push(P,ci,e.ci);}P.fg+=ok;}}const kx=e.kx;if(EGT[kx]===0&&!EGO.includes(kx))EGO.push(kx);EGT[kx]+=ok;if(!NEG[nr]){NEG[nr]=R2.netEg[NET_IDS[nr]]={c:0,p:0,ix:0,t:0};NEO.push(nr);}NEA[nr*4+kx]+=ok;
// Where each flow left (the PNI list asks what a PNI here would take over, #175).
if(e.J>0){const m=e.byC||(e.byC=new Map()),v=m.get(ci);if(v){v[0]+=ok;if(bz)v[1]+=ok;}else m.set(ci,[ok,bz?ok:0]);}
if(e.k==='t'){FGT[F]+=ok;FTI[F]=e.xi;}else if(e.k==='ix'){FGI[F]+=ok;FII[F]=e.xi;}else if(e.k==='p'){if(e.ci===ci&&e.net===NET_IDS[nr])FGH[F]+=ok;else FGP[F]+=ok;}}
@ -231,7 +247,7 @@ function compute(){RW.gen++;
// with free capacity, as long as they are no more than twice as long.
const send=(e,want)=>{
let room=Math.min(want,FREM[F],e.cap-e.used);if(room<=1e-6)return;
const P=PP[e.ci]||(PP[e.ci]=walkI(Pv,fci,e.ci));if(!P.dead)room-=push(e,P,room,TE&&SPILL<1?1-SPILL:0);
const P=PP[e.ci]||(PP[e.ci]=walkP(Pv,fci,e.ci));if(!P.dead)room-=push(e,P,room,TE&&SPILL<1?1-SPILL:0);
if(TE&&room>1e-6&&FREM[F]>1e-6&&e.used<e.cap&&e.ci!==fci)for(let k=0;k<2&&room>1e-6;k++){const P=altPath(fci,e.ci,Dv[e.ci]);if(!P)break;const m=push(e,P,Math.min(room,e.cap-e.used));if(m<=1e-6)break;room-=m;R2.teMoved+=m;}
};
// Exits with the same preference and the same IGP distance form an ECMP group: spread the flow over
@ -246,7 +262,8 @@ function compute(){RW.gen++;
for(let k=i0;k<i1;k++){if(FREM[F]<=1e-6)break;if(!gone(g[k]))send(g[k],FREM[F]);}};
// Stable, in place: short lists by insertion (no garbage), long ones by the built-in sort. Both are stable with the
// same comparison, so the order is the same either way.
const sortD=a=>{const n=a.length;if(n>24){a.sort(byDist);return;}for(let i=1;i<n;i++){const x=a[i];let j=i-1;while(j>=0&&byDist(a[j],x)>0){a[j+1]=a[j];j--;}a[j+1]=x;}};
// n: sort only a[0..n) (the group G holds GN exits).
const sortD=(a,n=a.length)=>{if(n>24){if(n===a.length)a.sort(byDist);else{const t=a.slice(0,n).sort(byDist);for(let i=0;i<n;i++)a[i]=t[i];}return;}for(let i=1;i<n;i++){const x=a[i];let j=i-1;while(j>=0&&byDist(a[j],x)>0){a[j+1]=a[j];j--;}a[j+1]=x;}};
// The direct ranks (25-direct-egress.js) have short lists, one carrier's ports, so they take the plain way: the usable exits
// nearest first, tried in groups of equal distance, as the reference loop in the tests does. A direct rank only concerns the
// networks that have an exit there, and nothing from a POP where Prefer has the say; a port that is not set to carry only takes
@ -261,11 +278,11 @@ function compute(){RW.gen++;
else for(let li=0;li<lst.length;li++)if(usable(lst[li]))REST.push(lst[li]);
sortD(REST);
for(let i=0;i<REST.length&&FREM[F]>1e-6;){let j=i+1;while(j<REST.length&&REST[j].hp===REST[i].hp&&Math.abs(Dv[REST[j].ci]-Dv[REST[i].ci])<1e-6)j++;sendGroup(REST,i,j);i=j;}}};
// The tables of a flow's city (distances, paths) are set up only once it has an exit list at this rank.
for(const pass of PASSES)for(const rank of RANKS){
if(!RSET.has(rank))continue;
if(DSEL.has(rank)){directPass(rank,pass);continue;}
// Index loops in here: plain for…of allocates an iterator per loop until V8 has optimised compute().
/* One rank of one pass over every flow. Each hot loop of compute() sits in a function of its own: V8 optimises a small
function quickly and keeps it, while compute() as a whole is too big to optimise cheaply (it would run unoptimised,
boxing every number, and be compiled again and again in the background). The tables of a flow's city (distances,
paths) are set up only once it has an exit list at this rank. */
const rankPass=(rank,pass)=>{
for(F=0;F<NF;F++){
if(FREM[F]<=1e-6||(rank===0&&FCA[F]===0)||(pass!==0&&FBZ[F]!==(pass===1?1:0)))continue;
fnj=FNJ[F];let lst=BRJ[fnj][rank];if(!lst)continue;fci=FCI[F];if(lst.cs){lst=lst.cs[COMP[fci]];if(!lst)continue;}
@ -274,10 +291,11 @@ function compute(){RW.gen++;
exits gives. Exits only fill up once tried, so the usable ones stay the same until then. Nearly every flow
fits in its first group: find that one with a scan (nothing is visited yet), and sort only what is left
when it does not fit. Most often the group is the POP's own exits: with no preferred exit on the list
and no other city at distance 0, the usable ones here are exactly the first group, already in order. */
G.length=0;
if(!lst.hi&&ALONE[fci]){const here=lst.at.get(fci);if(here)for(let h=0;h<here.length;h++){const e=here[h];if(e.used<e.cap&&!gone(e))G.push(e);}}
if(!G.length){
and no other city at distance 0, the usable ones here are exactly the first group, already in order.
The group is G[0..GN): emptying an array (length=0) gives back its storage, and the next push allocates it again. */
GN=0;
if(!lst.hi&&ALONE[fci]){const here=lst.at.get(fci);if(here)for(let h=0;h<here.length;h++){const e=here[h];if(e.used<e.cap&&!gone(e))G[GN++]=e;}}
if(!GN){
// One scan: the best (preference, distance) so far and, in list order, the exits within 1e-6 of it. The best
// distance only goes down, so dropping the ones it leaves behind gives exactly the exits a second scan would.
// Exits that are full stay full for this pass: when a quarter of a list is, it is rewritten without them.
@ -285,24 +303,30 @@ function compute(){RW.gen++;
them still have room no other exit can have become better, and the scan would find the same group (an
empty one, no usable exit, stays empty). */
const gc=lst.gc||(lst.gc=[]),c=gc[fci];
if(c!==undefined&&allRoom(c)){if(!c.length)continue;for(let k=0;k<c.length;k++)G.push(c[k]);}else{
if(c!==undefined&&allRoom(c)){if(!c.length)continue;for(let k=0;k<c.length;k++)G[GN++]=c[k];}else{
let bh=2,bd=Infinity,full=0;
for(let li=0,LN=lst.length;li<LN;li++){const e=lst[li];if(e.used>=e.cap||gone(e)){full++;continue;}const d=Dv[e.ci];if(d===Infinity)continue;const h=e.hp;
if(h<bh){bh=h;bd=d;G.length=0;G.push(e);}
else if(h===bh){if(d<bd){bd=d;let k=0;for(let gi=0;gi<G.length;gi++){const x=G[gi];if(Dv[x.ci]-d<1e-6)G[k++]=x;}G.length=k;G.push(e);}else if(d-bd<1e-6)G.push(e);}}
if(h<bh){bh=h;bd=d;GN=0;G[GN++]=e;}
else if(h===bh){if(d<bd){bd=d;let k=0;for(let gi=0;gi<GN;gi++){const x=G[gi];if(Dv[x.ci]-d<1e-6)G[k++]=x;}GN=k;G[GN++]=e;}else if(d-bd<1e-6)G[GN++]=e;}}
if(full*4>=lst.length&&full){let k=0;for(let li=0;li<lst.length;li++){const e=lst[li];if(e.used<e.cap)lst[k++]=e;}lst.length=k;}
if(bh>1){gc[fci]=NOG;continue;}
if(G.length>1)sortD(G);gc[fci]=G.slice();}}
sendGroup(G,0,G.length);
if(GN>1)sortD(G,GN);gc[fci]=G.slice(0,GN);}}
sendGroup(G,0,GN);
if(FREM[F]<=1e-6)continue;
REST.length=0;for(let li=0;li<lst.length;li++)if(usable(lst[li]))REST.push(lst[li]);sortD(REST);
for(let i=0;i<REST.length&&FREM[F]>1e-6;){
let j=i+1;while(j<REST.length&&REST[j].hp===REST[i].hp&&Math.abs(Dv[REST[j].ci]-Dv[REST[i].ci])<1e-6)j++;
sendGroup(REST,i,j);i=j;
}
}
}};
for(const pass of PASSES)for(const rank of RANKS){
if(!RSET.has(rank))continue;
if(DSEL.has(rank)){directPass(rank,pass);continue;}
rankPass(rank,pass);
}
for(const i of NORD)R2.nodeLoad[CITIES[i].id]=NLOAD[i];
for(const k of EGO)R2.egT[EXK[k]]=EGT[k];for(const j of NEO){const ne=NEG[j];for(let k=0;k<4;k++)ne[EXK[k]]=NEA[j*4+k];}
(()=>{
if(DD){let rem=0,jf=0;for(let i=0;i<NF;i++)if(FREM[i]>1e-6)rem+=FREM[i];if(JK)for(const k in edges){const e=edges[k];if(e.res<=1e-6&&e.jk)jf+=e.jk;}
ddosAfter(DD,{E,R2,squeeze:Math.min(rem,jf),
lost:(ci,x,y,e)=>{SRV[ci]-=x;R2.served-=x;const c=CITIES[ci].id;if(y)R2.bizSrv[c]-=y;R2.egT[e.k]-=x;if(e.rank<=2)R2.peered-=x;}});}
@ -318,8 +342,14 @@ function compute(){RW.gen++;
for(const k in edges){const e=edges[k],ld=e.ld||0,ac=e.cap-e.scap,a=Math.min(ld,ac),sp=ld-a;for(const l of e.links){const c=capOf(l);R2.load[l.id]=l.standby?(e.scap>0?sp*c/e.scap:0):(ac>0?a*c/ac:0);}}
for(const e of TORD){const m=R2.tNet[e.id]={},b=e.ts*NN;for(const j of TNK[e.ts])m[NET_IDS[j]]=TNA[b+j];}
R2.peerShare=R2.served>0?R2.peered/R2.served:0;
for(const e of E){if(e.k==='t')R2.useT[e.id]=(R2.useT[e.id]||0)+e.used;else if(e.k==='ix')R2.useIx[e.id]=e.used;else if(e.k==='p')R2.usePni[e.id]=e.used;else R2.useCache[e.id]=e.used;}
// money
for(const e of E){if(e.k==='t')R2.useT[e.id]=(R2.useT[e.id]||0)+e.used;else if(e.k==='ix')R2.useIx[e.id]=e.used;else if(e.k==='p')R2.usePni[e.id]=e.used;else R2.useCache[e.id]=e.used;}})();
computeMoney(R2);
// R.fl: from this call's own columns, made into objects only when something asks.
flDefine(R2,{NF,FREM,FCI,FNJ,FCA,FBZ,FGT,FGI,FGP,FGH,FTI,FII,E,CNB,NCN});
R=R2;UI.hotMap=hotByCity();
}
// What the network costs and earns per month, from what compute() routed.
function computeMoney(R2){
const C=R2.cost,V=R2.rev,pm=S.priceMul;
C.pops=Object.entries(S.pops).reduce((a,[c,p])=>a+popMo(c)+((p.routers||1)-1)*ROUTER_MO+cageMo(p.routers)+(p.routers||1)*RG[p.rgen||0].mo+(p.gen?GEN_MO:0),0);
C.optical=S.links.filter(l=>l.kind==='df').reduce((a,l)=>a+(l.ready>S.day?0:ampCount(l.km)*AMP_MO*(l.own&&isRdm(l.sys)&&!l.sub?Math.max(1,l.pairs|0):1)+(l.regen|0)*REGEN_MO)+(l.spares?Math.round(150+(l.ch||[]).length*80):0),0);
@ -338,7 +368,4 @@ function compute(){RW.gen++;
C.revshare=(S.loans||[]).reduce((a,l)=>a+(BANKS[l.bank].equity||l.bal>0?l.share||0:0),0)*(V.eyeballs+V.business+V.ix);
R2.costMo=Object.values(C).reduce((a,b)=>a+b,0);
R2.revMo=V.eyeballs+V.business+V.ix+V.grant;
// R.fl: from this call's own columns, made into objects only when something asks.
flDefine(R2,{NF,FREM,FCI,FNJ,FCA,FBZ,FGT,FGI,FGP,FGH,FTI,FII,E,CNB,NCN});
R=R2;UI.hotMap=hotByCity();
}

87
tests/route-cache.test.js Normal file
View file

@ -0,0 +1,87 @@
// What compute() keeps between calls (src/game/80-simulation/29-route-cache.js): the shortest paths and the exit lists,
// rebuilt only when their key changes. These tests make sure every kind of change that can matter changes the key (a
// stale table would route yesterday's network), that demand alone does not (or nothing is gained), and that routing
// with the kept tables is exactly routing without them.
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const { load } = require('./harness');
const SAVE = fs.readFileSync(path.join(__dirname, 'fixtures', 'big-network.json'), 'utf8');
function open() {
const h = load({ seed: 777 });
h.ev(`S=migrate(JSON.parse(${JSON.stringify(SAVE)}));UI.started=true;UI.pitched=true;UI.speed=0;closeModal();S.nextFail=S.day+999;S.nextEvent=S.day+999;compute();compute();`);
return h;
}
// Applies a change, routes once with the kept tables and once without (from the same yesterday), and says which keys moved.
function step(ev, change) {
ev(change);
return JSON.parse(ev(`(()=>{const gk=RC.gk,xk=RC.xk,prior=R;compute();const a=JSON.stringify(R)+JSON.stringify(R.fl);
const g=RC.gk!==gk,x=RC.xk!==xk;R=prior;RC.on=false;try{compute();}finally{RC.on=true;}const b=JSON.stringify(R)+JSON.stringify(R.fl);
return JSON.stringify({g,x,same:a===b,len:a.length});})()`));
}
test('a change to links, POPs, ports, peering, events or RouteScript rebuilds what routing keeps; demand alone does not', () => {
const { ev, errors } = open();
assert.ok(ev('RC.gk!==null&&RC.xk!==null'), 'routing keeps its tables after a compute');
// [what changes, the graph key moves, the exits key moves]
const changes = [
// Demand and capacity only: nothing structural, the kept tables are used.
['for(const c in S.pops)if(S.pops[c].bb)S.pops[c].bb.d*=1.07', false, false],
['S.biz.forEach(b=>b.bw*=1.1)', false, false],
["S.links.find(l=>l.kind==='wave').tier=1", false, false],
['S.transit[0].tier=Math.max(0,S.transit[0].tier-1)', false, false],
['S.day++', false, false],
// The graph: a cut, a link gone, a link back, a metric, a POP down and up again, RouteScript's avoid.
['S.links[3].cut=S.day+3', true, true],
['S.links[3].cut=0', true, true],
['window.__l=S.links.splice(7,1)[0]', true, true],
['S.links.push(__l)', true, true],
["S.pairMet=S.pairMet||{};{const l=S.links[10];S.pairMet[l.a<l.b?l.a+'|'+l.b:l.b+'|'+l.a]='avoid';}", true, true],
['S.pops.sea.down=S.day+3', true, true],
['S.pops.sea.down=0', true, true],
// The exits: transit ports, PNIs, caches, exchanges, sessions.
["S.transit[2].pref='low'", false, true],
["S.transit[2].pref='high'", false, true],
['S.transit[5].carry=true', false, true],
['S.transit[4].down=S.day+3', false, true],
['S.transit.splice(1,1)', false, true],
["S.pni[0].pref='high'", false, true],
['S.pni.splice(10,25)', false, true],
['S.caches.splice(0,30)', false, true],
["S.ixPref={};S.ixPref[Object.keys(S.ixp)[1]]='low'", false, true],
['S.ev.ixdown={};S.ev.ixdown[Object.keys(S.ixp)[2]]=S.day+3', false, true],
['delete S.rs[Object.keys(S.rs)[0]]', false, true],
['delete S.ixp[Object.keys(S.ixp)[3]]', false, true],
// Events that change who reaches whom, and RouteScript.
["S.ev.rpki={nets:['ggl','nfx'],until:S.day+3}", false, true],
['S.ev.depeer=S.day+3', false, true],
["S.ev.leak={net:'fbk',until:S.day+3}", false, true],
["S.te={on:true,day:0,script:'prefer transit '+NET[S.transit[0].net].name+'\\nspill at 80%'};S.core={lv:2,run:2,legacy:true}", false, true],
["S.te.script+='\\nprefer pni for goggle'", false, true],
// avoid link: the edge's weight, so the graph key moves too.
["{const l=S.links.find(x=>!linkDown(x));S.te.script+='\\navoid link '+CITY[l.a].name.toLowerCase()+'-'+CITY[l.b].name.toLowerCase();}", true, true],
];
for (const [change, g, x] of changes) {
const r = step(ev, change);
assert.ok(r.same, `${change}: routing with the kept tables is routing without them`);
if (g !== null) assert.equal(r.g, g, `${change}: the graph key ${g ? 'moves' : 'stays'}`);
assert.equal(r.x, x, `${change}: the exits key ${x ? 'moves' : 'stays'}`);
assert.ok(r.len > 1e5, 'a big network was routed');
}
assert.deepEqual(errors, []);
});
test('days in a row reuse the kept tables and route like a fresh start every day', () => {
const { ev, errors } = open();
for (let d = 0; d < 4; d++) {
ev('dayTick()');
const r = step(ev, '');
assert.ok(r.same, `day ${d}: the same routing with and without the kept tables`);
assert.ok(!r.g && !r.x, `day ${d}: a day of demand only keeps the tables`);
}
assert.deepEqual(errors, []);
});

30
tests/route-fuzz.test.js Normal file
View file

@ -0,0 +1,30 @@
// A few seeded random games (scripts/lib/route-fuzz.js), played move for move on main's page and on this checkout's:
// after every move and day both route identically and keep the same game state, and this checkout routes the same with
// the routing cache (RC) on and off. scripts/route-fuzz.js plays hundreds of these; here a handful, so CI stays quick.
// Without git's origin/main (a bare tarball), the base is this checkout's own page: the cache on/off check still runs.
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const page = require('../scripts/lib/page');
const { openPage, playGame } = require('../scripts/lib/route-fuzz');
function pages() {
const head = path.join(os.tmpdir(), `tubes-fuzz-test-${process.pid}-head.html`);
fs.writeFileSync(head, page.assemble().html);
let base = head;
try { const b = path.join(os.tmpdir(), `tubes-fuzz-test-${process.pid}-base.html`); fs.writeFileSync(b, page.assembleRef('origin/main')); base = b; } catch (e) {}
return { base: openPage(base, 'base'), head: openPage(head, 'head') };
}
test('random games route the same as main, with and without the routing cache, move for move', () => {
const P = pages();
// Seeds that start a normal game, a sandbox, a scenario and a puzzle, with transit, exchanges, PNIs and RouteScript.
for (const seed of [20, 23, 27, 26]) {
const g = playGame(P, seed, { moves: 35 });
assert.equal(g.div, null, `seed ${seed} diverged: ${JSON.stringify(g.div, null, 1)}`);
assert.ok(g.moves >= 35, `seed ${seed} played its moves`);
assert.deepEqual(g.errors, [], `seed ${seed}: no page errors`);
}
});

View file

@ -26,7 +26,10 @@ function network(ev) {
const start = source.indexOf(' for(const pass of PASSES)for(const rank of RANKS){');
const end = source.indexOf(' for(const i of NORD)', start);
assert.ok(start >= 0 && end > start, 'allocation loop boundaries must exist');
ev(`window.referenceCompute=eval('('+${JSON.stringify(source.slice(0,start)+referenceAllocation+source.slice(end))}+')')`);
// The reference also builds every table afresh, so the comparison covers what routing keeps between calls.
assert.ok(source.includes('RCON=RC.on'), 'compute() can be told not to keep its tables');
const ref = (source.slice(0,start)+referenceAllocation+source.slice(end)).replace('RCON=RC.on', 'RCON=false');
ev(`window.referenceCompute=eval('('+${JSON.stringify(ref)}+')')`);
}
test('indexed routing matches the simple allocation loop across network changes', () => {

View file

@ -273,21 +273,24 @@ function plainCompute(ev) {
const src = ev('compute.toString()');
let plain = src
.replace('if(!lst.hi&&ALONE[fci])', 'if(false)')
.replace('if(!G.length){\n // One scan', 'if(false){\n // One scan')
.replace('if(!GN){\n // One scan', 'if(false){\n // One scan')
.replace('const ns=e.nets||NET_IDS;', 'const ns=NET_IDS;')
.replace('if(P.dead)return 0;', '').replaceAll('if(r<=1e-6){P.dead=1;return 0;}', '')
.replace('if(!P.dead)room-=push(', 'room-=push(').replace(/const gone=e=>\{[^\n]*\};/, 'const gone=e=>false;');
.replace('if(!P.dead)room-=push(', 'room-=push(').replace(/const gone=e=>\{[^\n]*\};/, 'const gone=e=>false;')
// ...and without what routing keeps between calls (29-route-cache.js): the plain path builds everything afresh.
.replace('RCON=RC.on', 'RCON=false');
// ...and the group split and sorting written the plain way, with array copies, as before they were made allocation-free
const sg = plain.indexOf('const ROOM=[],sendGroup='), sd = plain.indexOf('const sortD=');
assert.ok(sg > 0 && sd > sg, 'found the group sender and the sort');
plain = plain.slice(0, sg) + `const ROOM=[],sendGroup=(g0,i0=0,i1=g0.length)=>{const g=g0.slice(i0,i1);for(const e of g)e.vis=vis;
if(g.length>1){const room=g.map(e=>Math.max(0,e.cap-e.used)),tot=room.reduce((a,b)=>a+b,0),want=FREM[F];if(tot>1e-6)g.forEach((e,k)=>send(e,want*room[k]/tot));}
for(const e of g){if(FREM[F]<=1e-6)break;send(e,FREM[F]);}};\n ` + plain.slice(sd).replace(/^const sortD=a=>\{[^\n]*\};/, 'const sortD=a=>a.sort(byDist);');
for(const e of g){if(FREM[F]<=1e-6)break;send(e,FREM[F]);}};\n ` + plain.slice(sd).replace(/^const sortD=\(a,n=a\.length\)=>\{[^\n]*\};/, 'const sortD=(a,n=a.length)=>{const t=a.slice(0,n).sort(byDist);for(let i=0;i<n;i++)a[i]=t[i];};');
for (const [a, b] of [[src, plain]]) assert.notEqual(a, b);
assert.equal((plain.match(/if\(false\)/g) || []).length, 2, 'both shortcuts were found and switched off');
assert.ok(!plain.includes('e.nets||NET_IDS'), 'the exit index was switched off');
assert.ok(!plain.includes('P.dead'), 'the full-path mark was switched off');
assert.ok(plain.includes('const sortD=a=>a.sort(byDist);') && plain.includes('g.forEach('), 'the plain group split and sort are in');
assert.ok(plain.includes('RCON=false') && !plain.includes('RC.on'), 'the kept routing tables were switched off');
assert.ok(plain.includes('const sortD=(a,n=a.length)=>{const t=a.slice(0,n).sort(byDist);') && plain.includes('g.forEach('), 'the plain group split and sort are in');
ev('window.__plain=' + plain.replace(/^function compute/, 'function'));
}