@wildshard/engine 0.1.20 → 0.1.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/lint/engine-sides.json +4 -0
  3. package/lint/shardfile-boundaries.mjs +4 -2
  4. package/lint/wildshard-plugin.js +1 -1
  5. package/package.json +3 -1
  6. package/scripts/audio-download-proof.mjs +1 -1
  7. package/scripts/chaos-proxy-core.mjs +14 -1
  8. package/scripts/chaos-proxy-proof.mjs +22 -0
  9. package/scripts/check-spelled-paths.mjs +6 -3
  10. package/scripts/committed-fast-checks.mjs +2 -2
  11. package/scripts/failmode-cases.d.mts +8 -0
  12. package/scripts/failmode-cases.mjs +97 -0
  13. package/scripts/failmode-proof.mjs +556 -0
  14. package/scripts/grid-crossing-nightly.mjs +5 -4
  15. package/scripts/grid-crossing-report.mjs +5 -2
  16. package/scripts/grid-crossing-tour.mjs +25 -8
  17. package/scripts/grid-crossing-walk-proof.mjs +3 -3
  18. package/scripts/hover-speed-probe.mjs +136 -0
  19. package/scripts/mmo-bots.mjs +7 -3
  20. package/scripts/net-lag-proof.mjs +4 -3
  21. package/scripts/online-rig.mjs +11 -9
  22. package/scripts/parity/boot-smoke.mjs +1 -1
  23. package/scripts/release-cook.mjs +113 -0
  24. package/scripts/test-products.json +0 -21
  25. package/scripts/vps/cook-follow.d.mts +6 -0
  26. package/scripts/vps/cook-follow.mjs +45 -0
  27. package/scripts/vps/harden-apply.sh +10 -1
  28. package/scripts/vps/rebuild.sh +120 -26
  29. package/scripts/vps/wildshard-apply.mjs +5 -1
  30. package/scripts/vps/wildshard-config +117 -3
  31. package/src/client/app/entry.ts +2 -3
  32. package/src/client/engine/boot/contentRead.ts +78 -9
  33. package/src/client/engine/boot/downloadGiveUp.ts +34 -0
  34. package/src/client/engine/boot/gpuFiles.ts +3 -3
  35. package/src/client/engine/boot/leaveAfterPaint.ts +4 -1
  36. package/src/client/engine/combat/EquipmentService.ts +2 -0
  37. package/src/client/engine/core/Game.ts +6 -2
  38. package/src/client/engine/core/shadowLayer.ts +18 -0
  39. package/src/client/engine/explore/pick.ts +13 -1
  40. package/src/client/engine/net/predictor.ts +12 -4
  41. package/src/client/engine/player/TouchControls.ts +3 -2
  42. package/src/client/engine/render/families/sceneSurface.ts +3 -1
  43. package/src/client/engine/telemetry/browserErrors.ts +3 -1
  44. package/src/client/engine/ui/ErrorModal.ts +1 -1
  45. package/src/client/engine/ui/Loading.ts +14 -2
  46. package/src/client/engine/ui/downloadFailed.ts +54 -0
  47. package/src/client/engine/ui/errorScreen.ts +51 -9
  48. package/src/client/engine/ui/hoverBoost.ts +31 -4
  49. package/src/client/engine/ui/styles/touch.css +6 -0
  50. package/src/client/game/boot/spawnDownload.ts +56 -7
  51. package/src/client/game/grid/copyPrompts.ts +3 -4
  52. package/src/client/game/grid/dataCellKit.ts +91 -0
  53. package/src/client/game/grid/enteredEquipment.ts +11 -2
  54. package/src/client/game/grid/live.ts +11 -301
  55. package/src/client/game/grid/liveSession.ts +33 -10
  56. package/src/client/game/grid/neighbourLife.ts +50 -32
  57. package/src/client/game/grid/reveal.ts +7 -2
  58. package/src/client/game/grid/session.ts +6 -70
  59. package/src/client/game/grid/shardfileFar.ts +13 -2
  60. package/src/client/game/plugin/weaponFamily.ts +6 -0
  61. package/src/client/game/shardfile/clientSource.ts +7 -3
  62. package/src/client/game/shardfile/clientWorker.ts +17 -7
  63. package/src/client/game/shardfile/groundMerge.ts +5 -3
  64. package/src/client/host/online/linkScreens.ts +12 -6
  65. package/src/client/host/online/reconnectStrip.ts +3 -2
  66. package/src/client/host/online/release.ts +87 -16
  67. package/src/client/host/online/releaseScreens.ts +108 -0
  68. package/src/client/host/online/shell.ts +46 -12
  69. package/src/client/host/online/voice.ts +45 -1
  70. package/src/client/host/online/voiceUi.ts +24 -6
  71. package/src/server/engine/sim/playerSim.ts +1 -1
  72. package/src/server/game/grid/room.ts +2 -38
  73. package/src/server/game/grid/simulation.ts +12 -6
  74. package/src/server/game/shardfile/declaredKitItems.ts +8 -2
  75. package/src/server/host/dedicated/cellHost.ts +9 -2
  76. package/src/server/host/dedicated/cellProtocol.ts +1 -1
  77. package/src/server/host/dedicated/cellWorker.ts +2 -2
  78. package/src/server/host/dedicated/dedicatedRoom.ts +22 -4
  79. package/src/server/host/dedicated/gridCells.ts +5 -3
  80. package/src/server/host/dedicated/main.ts +6 -4
  81. package/src/server/host/dedicated/roadRoom.ts +3 -0
  82. package/src/server/host/dedicated/workerTable.ts +0 -10
  83. package/src/server/host/dedicated/world.ts +39 -5
  84. package/src/server/host/standalone/cellWorld.ts +1 -1
  85. package/src/server/host/standalone/gridServer.ts +1 -1
  86. package/src/shared/engine/boot/loadingWaits.ts +10 -2
  87. package/src/shared/engine/core/config.ts +3 -0
  88. package/src/shared/engine/player/hoverSpeed.ts +3 -3
  89. package/src/shared/engine/saves/shape.ts +17 -2
  90. package/src/shared/engine/strings.ts +22 -1
  91. package/src/shared/game/grid/placementRows.ts +4 -3
  92. package/src/shared/game/grid/rules.ts +12 -0
  93. package/src/client/game/grid/cellLoading.ts +0 -110
  94. package/src/client/game/grid/cellScreens.ts +0 -48
@@ -0,0 +1,556 @@
1
+ #!/usr/bin/env node
2
+ // FM3 (meta docs/plans/MMO-FAILURE-MODES-PLAN.md): the failure proof. Real faults, real screens, on this Mac only. The rig of
3
+ // online-rig.mjs (the built server, a signed release file on loopback) sits behind FM2's chaos proxy (chaos-proxy-core.mjs), and a
4
+ // headless browser at 402x874 plays a build stamped with the proxy's release address. Each case (scripts/failmode-cases.mjs) gets a fresh
5
+ // browser context: a play case enters Driftwood, then the Node driver arms the fault through the proxy's control (never page script);
6
+ // a boot or load case arms it before navigation. The case passes only when the injection is seen (a proxy match, or the browser's own
7
+ // offline/freeze event), the catalogued screen (`data-failmode`) shows with primary copy free of raw errors, and the page recovers to a
8
+ // real frame (or the named terminal page) once the fault is healed. Every case is recorded in `<work>/failmodes/receipt.json`, with
9
+ // its screenshot under `<work>/failmodes/shots/`.
10
+ //
11
+ // Exit 0: every selected case passed. Exit 1: a screen is missing or wrong, a dependency row isn't built, or the engine can't produce
12
+ // the fault (all named in the receipt). Exit 2: the harness failed (setup, an injection that matched nothing, cleanup). `--baseline`
13
+ // runs every case either way; it never changes the verdict. These are Ring 2 proofs: real 35 s/120 s clocks, minutes per engine.
14
+ // Never in pre-push or CI (FM11 owns the nightly).
15
+ //
16
+ // The server lets 10 connects a minute in per address and every page here is 127.0.0.1, so play cases wait out the minute between
17
+ // them (`--cooldown=<s>`, 60) rather than lifting the cap; a long outage is held by refused upgrades that never reach the server.
18
+ //
19
+ // node scripts/failmode-proof.mjs --prepare <work> a fixture key (<work>/key.json, 0600) and a Driftwood build stamped with the
20
+ // proxy's release address (<work>/dist); prints both paths
21
+ // scripts/browser-lane.sh node scripts/failmode-proof.mjs <dist> <work> <key.json> <pg bin|memory:> <pg data>
22
+ // [--engine=chromium|webkit] [--scenario=<id,...>|all] [--baseline] [--cooldown=<s>]
23
+ // Ports: the rig from WS_RIG_PORTS (18856 here: Postgres 18856, world 18857, pages 18858, release 18859), the proxy's pages on 18851 and
24
+ // its world door on 18852. The fixture release is signed by the owned key only, channel production on loopback (staging for mode 46).
25
+ import { spawnSync } from 'node:child_process';
26
+ import { generateKeyPairSync } from 'node:crypto';
27
+ import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
28
+ import { join, resolve as absolute } from 'node:path';
29
+ import { chromium, devices, webkit } from 'playwright';
30
+ import { openChaosProxy } from './chaos-proxy-core.mjs';
31
+ import { KEYCHAIN_ARGS } from './chromium-keychain.mjs';
32
+ import { saveFixture } from './debug-settings.mjs';
33
+ import { FLOOR_GRID_ENTER } from './frame-floor-navigation.mjs';
34
+ import { rawLeak, selectCases, verdict } from './failmode-cases.mjs';
35
+
36
+ process.env.WS_RIG_PORTS ??= '18856';
37
+ const ROOT = absolute(import.meta.dirname, '..'), PROXY_PAGE = 18851, PROXY_WORLD = 18852, SHARD = 'driftwood-isle';
38
+ const PAGE_ORIGIN = `http://127.0.0.1:${String(PROXY_PAGE)}`, RELEASE_URL = `${PAGE_ORIGIN}/__release.json`, SOCKET = `ws://127.0.0.1:${String(PROXY_WORLD)}/`;
39
+ const args = process.argv.slice(2), positional = args.filter(value => !value.startsWith('--'));
40
+ /** @param {string} name @param {string} fallback */
41
+ const flag = (name, fallback) => args.find(value => value.startsWith(`--${name}=`))?.slice(name.length + 3) ?? fallback;
42
+ /** @param {number} ms */
43
+ const sleep = ms => new Promise(resolve => { setTimeout(resolve, ms); });
44
+
45
+ if (args[0] === '--prepare') {
46
+ const named = args[1];
47
+ if (named === undefined || named.startsWith('--')) throw new Error('--prepare <work>');
48
+ const work = absolute(named);
49
+ mkdirSync(work, { recursive: true });
50
+ const keyFile = join(work, 'key.json');
51
+ if (!existsSync(keyFile)) {
52
+ const pair = generateKeyPairSync('ed25519');
53
+ writeFileSync(keyFile, JSON.stringify({ public: pair.publicKey.export({ type: 'spki', format: 'der' }).toString('hex'), private: pair.privateKey.export({ type: 'pkcs8', format: 'pem' }) }), { mode: 0o600, flag: 'wx' });
54
+ }
55
+ const key = JSON.parse(readFileSync(keyFile, 'utf8')), owner = `failmode-${String(process.pid)}`;
56
+ const built = spawnSync('bash', ['scripts/serve-build.sh', '--shard', SHARD, '--name', 'failmode', '--hours', '1'], { cwd: ROOT, encoding: 'utf8',
57
+ env: { ...process.env, SERVE_OWNER: owner, WILDSHARD_RELEASE_URL: RELEASE_URL, WILDSHARD_RELEASE_KEY: key.public }, stdio: ['ignore', 'pipe', 'pipe'] });
58
+ const out = /→ (\S+)/u.exec(built.stderr)?.[1], port = /127\.0\.0\.1:(\d+)/u.exec(built.stdout)?.[1];
59
+ try {
60
+ if (built.status !== 0 || out === undefined) throw new Error(`the build failed: ${built.stderr.slice(-2000)}`);
61
+ const dist = join(work, 'dist');
62
+ rmSync(dist, { recursive: true, force: true });
63
+ if (spawnSync('cp', ['-cR', out, dist], { stdio: 'inherit' }).status !== 0) throw new Error('copying the build failed');
64
+ if (!readFileSync(join(dist, 'index.html'), 'utf8').includes(RELEASE_URL)) throw new Error('the build is not stamped with the proxy release address');
65
+ console.log(JSON.stringify({ dist, keyFile }));
66
+ } finally {
67
+ if (port !== undefined) spawnSync('bash', ['scripts/serve-build.sh', 'stop', port], { cwd: ROOT, env: { ...process.env, SERVE_OWNER: owner }, stdio: 'ignore' });
68
+ }
69
+ process.exit(0);
70
+ }
71
+
72
+ const [dist = '', work = '', keyFile = '', pgBin = '', pgData = ''] = positional;
73
+ if (pgData === '' && pgBin !== 'memory:') throw new Error('failmode-proof <dist> <work> <key.json> <pg bin|memory:> <pg data>');
74
+ const engine = flag('engine', 'chromium'), baseline = args.includes('--baseline'), cooldownMs = Number(flag('cooldown', '60')) * 1000;
75
+ if (engine !== 'chromium' && engine !== 'webkit') throw new Error('--engine=chromium|webkit');
76
+ // `--refusal=screen|injection` proves strict mode refuses: a drop that must show a screen no build has (exit 1), and a fault armed on
77
+ // a route the page never reads, so it matches nothing (exit 2). Neither is a catalogue case.
78
+ const refusal = flag('refusal', '');
79
+ if (!['', 'screen', 'injection'].includes(refusal)) throw new Error('--refusal=screen|injection');
80
+ const drop = selectCases('drop')[0];
81
+ const cases = refusal === '' ? selectCases(flag('scenario', 'all')) : drop === undefined ? [] : [refusal === 'screen' ? { ...drop, id: 'drop', screen: 'no-such-screen', mode: 'R1' } : { ...drop, id: 'unmatched', run: 'unmatched', mode: 'R2' }], out = join(absolute(work), 'failmodes'), shots = join(out, 'shots');
82
+ if (!readFileSync(join(dist, 'index.html'), 'utf8').includes(RELEASE_URL)) throw new Error(`${dist} is not stamped with ${RELEASE_URL}: build it with --prepare`);
83
+ rmSync(out, { recursive: true, force: true }); mkdirSync(shots, { recursive: true });
84
+ const sha = spawnSync('git', ['rev-parse', 'HEAD'], { cwd: ROOT, encoding: 'utf8' }).stdout.trim();
85
+
86
+ /** @typedef {import('./failmode-cases.mjs').Case} Case */
87
+ /** @typedef {import('./failmode-cases.mjs').Outcome} Outcome */
88
+ /** @typedef {{ page: import('playwright').Page, context: import('playwright').BrowserContext, errors: string[] }} Entered */
89
+ /** @typedef {{ outcome: Outcome, reason?: string, seen?: Seen | null, injection?: unknown, recovery?: string, shot?: string, notes?: unknown }} Result */
90
+ /** @typedef {{ ids: string, ms: number, copy: string, leak: string | null }} Seen */
91
+
92
+ const { openRig } = await import('./online-rig.mjs');
93
+ const rig = await openRig({ dist, work: join(absolute(work), 'rig'), keyFile, pgBin, pgData, shard: SHARD, socket: SOCKET, origins: [PAGE_ORIGIN] });
94
+ let proxy = /** @type {Awaited<ReturnType<typeof openChaosProxy>> | null} */ (null), launched = /** @type {import('playwright').Browser | null} */ (null);
95
+ const rows = /** @type {(Result & { mode: string, id: string, owner: string, screen: string, inject: string, ms: number })[]} */ ([]);
96
+ let harnessFault = /** @type {string | null} */ (null);
97
+ try {
98
+ rmSync(join(absolute(work), 'proxy'), { recursive: true, force: true });
99
+ proxy = await openChaosProxy({ work: join(absolute(work), 'proxy'), pageUpstream: rig.base, releaseUpstream: `http://127.0.0.1:${String(Number(process.env.WS_RIG_PORTS) + 3)}`, worldUpstream: `http://127.0.0.1:${String(Number(process.env.WS_RIG_PORTS) + 1)}`, pagePort: PROXY_PAGE, worldPort: PROXY_WORLD });
100
+ const token = readFileSync(proxy.ready.tokenFile, 'utf8');
101
+ /** The proxy's state, or its rules replaced. @param {{ scenario: string, rules: object[] }} [body] */
102
+ const control = async body => {
103
+ const response = await fetch(proxy?.ready.controlUrl ?? '', { method: body === undefined ? 'GET' : 'POST', headers: { authorization: `Bearer ${token}`, 'content-type': 'application/json' }, ...body === undefined ? {} : { body: JSON.stringify(body) } });
104
+ if (!response.ok) throw new Error(`chaos control ${String(response.status)}`);
105
+ return /** @type {{ counters: { upgrades: number, matches: Record<string, number> }, events: object[], activeConnections: number }} */ (await response.json());
106
+ };
107
+ /** @param {string} scenario @param {object[]} rules */
108
+ const arm = (scenario, rules) => control({ scenario, rules });
109
+ const heal = () => control({ scenario: 'healthy', rules: [] });
110
+ /** matches on a route since a snapshot @param {Awaited<ReturnType<typeof control>>} before @param {string} route */
111
+ const matchedSince = async (before, route) => ((await control()).counters.matches[route] ?? 0) - (before.counters.matches[route] ?? 0);
112
+
113
+ launched = await (engine === 'webkit' ? webkit : chromium).launch({ headless: true, args: engine === 'webkit' ? [] : [...KEYCHAIN_ARGS, '--use-angle=metal', '--ignore-gpu-blocklist'] });
114
+ const { defaultBrowserType: _engine, ...phone } = devices['iPhone 16 Pro'];
115
+ const URL_PLAY = `${PAGE_ORIGIN}/?chunk=${SHARD}&mute=1&nolock=1&skipintro=1`;
116
+ /** A fresh device: its own context, Developer on, its errors and visibility changes kept. @param {() => void} [init] */
117
+ const fresh = async init => {
118
+ const context = await /** @type {import('playwright').Browser} */ (launched).newContext(phone), errors = /** @type {string[]} */ ([]);
119
+ await saveFixture(context, { scope: 'device', key: 'devMode', data: true });
120
+ await context.addInitScript(() => { const seen = /** @type {string[]} */ ([]); Object.defineProperty(globalThis, '__fmVisibility', { value: seen }); document.addEventListener('visibilitychange', () => { seen.push(document.visibilityState); }); });
121
+ if (init !== undefined) await context.addInitScript(init);
122
+ const page = await context.newPage();
123
+ page.on('pageerror', error => { errors.push(error.message.slice(0, 240)); });
124
+ page.on('console', line => { if (line.type() === 'error') errors.push(line.text().slice(0, 240)); if (process.env.FM_TRACE === '1') console.log(` [page ${line.type()}] ${line.text().slice(0, 300)}`); });
125
+ page.on('response', response => { if (process.env.FM_TRACE === '1' && response.status() >= 400) console.log(` [http ${String(response.status())}] ${response.url().slice(0, 200)}`); });
126
+ page.on('framenavigated', frame => { if (frame === page.mainFrame() && process.env.FM_TRACE === '1') console.log(` [nav] ${frame.url()}`); });
127
+ return { page, context, errors };
128
+ };
129
+ /** Through the Name box (if it asks) into the world, to 30 frames. @param {Entered} entered @param {number} [timeout] */
130
+ const intoWorld = async (entered, timeout = 150_000) => {
131
+ const { page } = entered, until = Date.now() + timeout;
132
+ for (;;) {
133
+ if (await page.locator('#ws-name-box input').isVisible().catch(() => false)) {
134
+ await page.locator('#ws-name-box input').fill(`fm${String(Date.now() % 100_000)}`);
135
+ await page.locator('#ws-name-box button[type=submit]').click();
136
+ }
137
+ if (await page.evaluate(() => typeof window.__workerClient === 'function' && window.__wildshard?.world?.player !== undefined).catch(() => false)) break;
138
+ if (Date.now() > until) {
139
+ const marks = await page.evaluate(() => ({ marks: Object.entries(document.documentElement.dataset).map(([key, value]) => `${key}=${value ?? ''}`).join(' '), sw: navigator.serviceWorker?.controller?.state ?? 'none', url: location.href, seen: [...document.querySelectorAll('[data-failmode]')].map(node => node instanceof HTMLElement ? node.dataset.failmode : '') })).catch(() => null);
140
+ throw new Error(`the page never reached the world (${JSON.stringify({ marks, errors: entered.errors.slice(-4) }).slice(0, 900)})`);
141
+ }
142
+ await sleep(250);
143
+ }
144
+ await page.evaluate(FLOOR_GRID_ENTER);
145
+ await page.waitForFunction(() => (/** @type {NonNullable<typeof window.__workerClient>} */ (window.__workerClient))().frames > 30, undefined, { timeout: Math.max(5000, until - Date.now()) });
146
+ };
147
+ /** The frames the page has heard. @param {import('playwright').Page} page */
148
+ const frames = page => page.evaluate(() => (typeof window.__workerClient === 'function' ? window.__workerClient().frames : -1)).catch(() => -1);
149
+ /** Every visible catalogued screen's ids. @param {import('playwright').Page} page */
150
+ const visibleIds = page => page.evaluate(() => [...document.querySelectorAll('[data-failmode]')].filter(node => node instanceof HTMLElement && !node.hidden && getComputedStyle(node).display !== 'none' && getComputedStyle(node).visibility !== 'hidden' && node.getBoundingClientRect().width > 0).map(node => (node instanceof HTMLElement ? node.dataset.failmode : undefined) ?? '')).catch(() => /** @type {string[]} */ ([]));
151
+ /** Waits for the screen `id`; what shows, how fast, and its primary copy (a Details/Copy diagnostics part is not primary).
152
+ * @param {import('playwright').Page} page @param {string} id @param {number} timeout @param {number} [from] */
153
+ const screen = async (page, id, timeout, from = Date.now()) => {
154
+ const until = Date.now() + timeout;
155
+ while (Date.now() < until) {
156
+ const found = await page.evaluate(wanted => {
157
+ for (const node of document.querySelectorAll('[data-failmode]')) {
158
+ if (!(node instanceof HTMLElement) || !(node.dataset.failmode ?? '').split(/\s+/u).includes(wanted)) continue;
159
+ const style = getComputedStyle(node), box = node.getBoundingClientRect();
160
+ if (node.hidden || style.display === 'none' || style.visibility === 'hidden' || box.width === 0 || box.height === 0) continue;
161
+ const copy = /** @type {HTMLElement} */ (node.cloneNode(true));
162
+ for (const part of copy.querySelectorAll('details, [data-details], .details, button')) part.remove();
163
+ return { ids: node.dataset.failmode ?? '', copy: (copy.textContent ?? '').replaceAll(/\s+/gu, ' ').trim().slice(0, 300) };
164
+ }
165
+ return null;
166
+ }, id).catch(() => null);
167
+ if (found !== null) return { ...found, ms: Date.now() - from, leak: rawLeak(found.copy) };
168
+ await sleep(200);
169
+ }
170
+ return null;
171
+ };
172
+ /** What a player reads now, for a case whose screen never came. @param {import('playwright').Page} page */
173
+ const onScreen = async page => ({ visible: await visibleIds(page), text: (await page.evaluate(() => document.body.textContent).catch(() => '') ?? '').replaceAll(/\s+/gu, ' ').slice(0, 300) });
174
+ /** A shot named for the case. @param {import('playwright').Page} page @param {string} id @param {string} what */
175
+ const shoot = async (page, id, what) => { const file = `${id.replace('/', '-')}-${what}.png`; await page.screenshot({ path: join(shots, file) }).catch(() => undefined); return file; };
176
+ /** Back in play: frames move on and no card or page is up (a notice may be). @param {import('playwright').Page} page @param {number} timeout */
177
+ const recovered = async (page, timeout) => {
178
+ const until = Date.now() + timeout, start = await frames(page);
179
+ while (Date.now() < until) {
180
+ const now = await frames(page), up = await visibleIds(page);
181
+ if (now > start + 10 && up.every(ids => /\b(?:reconnected|slow|lossy|staging)\b/u.test(ids))) return true;
182
+ await sleep(300);
183
+ }
184
+ return false;
185
+ };
186
+ /** The screen's verdict once seen. @param {Seen | null} seen @param {Case} item @param {import('playwright').Page} page @returns {Promise<Result>} */
187
+ const judged = async (seen, item, page) => {
188
+ if (seen === null) return { outcome: 'ui-fail', reason: `no ${item.screen} screen`, seen: null, notes: await onScreen(page) };
189
+ if (seen.leak !== null) return { outcome: 'ui-fail', reason: `primary copy leaks "${seen.leak}"`, seen };
190
+ return { outcome: 'pass', seen };
191
+ };
192
+ /** Clicks a button on the case's screen. @param {import('playwright').Page} page @param {string} id @param {string} label */
193
+ const press = (page, id, label) => page.locator(`[data-failmode~="${id}"] button`, { hasText: label }).first().click({ timeout: 5000 });
194
+ /** WebKit's setOffline (Playwright) leaves loopback sockets up, so a WebKit page "offline" kept playing on the rig (opus-fm1's mode 1).
195
+ * There the proxy cuts the world door too while the browser is offline: a live link drops and new upgrades are refused. Labelled in
196
+ * the receipt as `loopbackCut`; Chromium needs none (its offline mode closes the socket itself). @param {boolean} live */
197
+ const cutLoopback = async live => {
198
+ if (engine !== 'webkit') return false;
199
+ if (live) {
200
+ const before = await control(); await arm('webkit-offline', [{ route: 'socket', kind: 'drop-after', afterMs: 0 }]);
201
+ for (let tries = 0; tries < 40 && await matchedSince(before, 'socket') === 0; tries++) await sleep(100);
202
+ }
203
+ await arm('webkit-offline-held', [{ route: 'socket', kind: 'refuse-upgrade', code: 503 }]);
204
+ return true;
205
+ };
206
+
207
+ /** @type {Record<string, (item: Case) => Promise<Result>>} */
208
+ const runners = {
209
+ // a fault on the world's socket, held, then healed
210
+ socketDown: async item => {
211
+ const entered = await fresh(); const { page } = entered;
212
+ try {
213
+ await page.goto(URL_PLAY); await intoWorld(entered);
214
+ const before = await control(), started = Date.now();
215
+ /** @type {Record<string, { first: object, hold: object | null, holdMs: number, wait: number, button?: string, reloads?: boolean }>} */
216
+ const plans = {
217
+ drop: { first: { route: 'socket', kind: 'drop-after', afterMs: 0 }, hold: { route: 'socket', kind: 'refuse-upgrade', code: 503 }, holdMs: 2500, wait: 4000 },
218
+ reset: { first: { route: 'socket', kind: 'drop-after', afterMs: 0 }, hold: { route: 'socket', kind: 'refuse-upgrade', code: 503 }, holdMs: 2500, wait: 4000 },
219
+ reconnected: { first: { route: 'socket', kind: 'drop-after', afterMs: 0 }, hold: { route: 'socket', kind: 'refuse-upgrade', code: 503 }, holdMs: 2000, wait: 15_000 },
220
+ 'half-open': { first: { route: 'socket', kind: 'half-open' }, hold: null, holdMs: 2500, wait: 4000 },
221
+ restart: { first: { route: 'socket', kind: 'close', code: 4005, reason: 'restart' }, hold: null, holdMs: 4000, wait: 4000 },
222
+ resuming: { first: { route: 'socket', kind: 'drop-after', afterMs: 0 }, hold: { route: 'socket', kind: 'refuse-upgrade', code: 503 }, holdMs: 37_000, wait: 40_000 },
223
+ lost: { first: { route: 'socket', kind: 'drop-after', afterMs: 0 }, hold: { route: 'socket', kind: 'refuse-upgrade', code: 503 }, holdMs: 122_000, wait: 126_000, button: 'Try again' },
224
+ full: { first: { route: 'socket', kind: 'close', code: 4002, reason: 'full' }, hold: null, holdMs: 3000, wait: 6000 },
225
+ refused: { first: { route: 'socket', kind: 'close', code: 4001, reason: 'refused' }, hold: null, holdMs: 3000, wait: 6000, button: 'Play again', reloads: true },
226
+ 'version-skew': { first: { route: 'socket', kind: 'close', code: 4000, reason: 'reload' }, hold: null, holdMs: 15_000, wait: 20_000, button: 'Reload', reloads: true },
227
+ };
228
+ const plan = plans[item.id];
229
+ if (plan === undefined) throw new Error(`socketDown has no plan for ${item.id}`);
230
+ await arm(item.id, [plan.first]);
231
+ for (let tries = 0; tries < 40 && await matchedSince(before, 'socket') === 0; tries++) await sleep(100);
232
+ const injected = await matchedSince(before, 'socket');
233
+ if (injected === 0) return { outcome: 'harness-fail', reason: 'the socket fault matched nothing' };
234
+ if (plan.hold !== null) await arm(`${item.id}-held`, [plan.hold]);
235
+ const seenWhileDown = item.id === 'reconnected' ? null : await screen(page, item.screen, plan.wait, started);
236
+ const shot = seenWhileDown === null ? '' : await shoot(page, item.id, 'down');
237
+ await sleep(Math.max(0, plan.holdMs - (Date.now() - started)));
238
+ const injection = { socketMatches: await matchedSince(before, 'socket'), downMs: Date.now() - started };
239
+ await heal();
240
+ if (item.id === 'reconnected') {
241
+ const chip = await screen(page, 'reconnected', plan.wait, Date.now());
242
+ const chipShot = chip === null ? undefined : await shoot(page, item.id, 'chip');
243
+ const gone = chip !== null && await page.locator('[data-failmode~="reconnected"]').waitFor({ state: 'detached', timeout: 6000 }).then(() => true, () => false);
244
+ const verdictRow = await judged(chip, item, page);
245
+ return { ...verdictRow, ...verdictRow.outcome === 'pass' && !gone ? { outcome: 'ui-fail', reason: 'the Reconnected notice never went' } : {}, injection, ...chipShot === undefined ? {} : { shot: chipShot }, recovery: gone ? 'chip gone by itself' : 'chip stayed' };
246
+ }
247
+ const result = await judged(seenWhileDown, item, page);
248
+ if (result.outcome !== 'pass') return { ...result, injection, shot };
249
+ if (plan.button !== undefined) {
250
+ const reloading = plan.reloads === true ? page.waitForEvent('load', { timeout: 30_000 }).catch(() => null) : null;
251
+ await press(page, item.screen, plan.button).catch(() => undefined);
252
+ if (reloading !== null) { await reloading; await intoWorld(entered, 90_000).catch(() => undefined); }
253
+ }
254
+ const back = await recovered(page, item.id === 'full' ? 45_000 : 30_000);
255
+ return back ? { ...result, injection, shot, recovery: 'a real frame, no card' } : { outcome: 'ui-fail', reason: 'no recovery after heal', seen: seenWhileDown, injection, shot, after: await shoot(page, item.id, 'after'), url: page.url(), notes: await onScreen(page) };
256
+ } finally { await heal(); await entered.context.close(); }
257
+ },
258
+ // FM4: Send while the link is down holds one visible line, sent once on the way back
259
+ heldChat: async item => {
260
+ const entered = await fresh(); const { page } = entered;
261
+ try {
262
+ await page.goto(URL_PLAY); await intoWorld(entered);
263
+ const before = await control();
264
+ await arm('reset', [{ route: 'socket', kind: 'drop-after', afterMs: 0 }]);
265
+ for (let tries = 0; tries < 40 && await matchedSince(before, 'socket') === 0; tries++) await sleep(100);
266
+ if (await matchedSince(before, 'socket') === 0) return { outcome: 'harness-fail', reason: 'the socket fault matched nothing' };
267
+ await arm('reset-held', [{ route: 'socket', kind: 'refuse-upgrade', code: 503 }]);
268
+ await screen(page, 'drop', 4000);
269
+ const line = `held ${String(Date.now() % 10_000)}`;
270
+ await page.locator('#ws-chat input').fill(line); await page.locator('#ws-chat input').press('Enter');
271
+ const seen = await screen(page, item.screen, 3000), shot = await shoot(page, item.id, 'held');
272
+ await sleep(1500); await heal();
273
+ const result = await judged(seen, item, page);
274
+ const sent = await page.locator('#ws-chat', { hasText: line }).waitFor({ timeout: 15_000 }).then(() => true, () => false);
275
+ await sleep(2000);
276
+ const copies = await page.evaluate(text => [...document.querySelectorAll('#ws-chat [data-lines] > *')].filter(node => (node.textContent ?? '').includes(text)).length, line);
277
+ if (result.outcome !== 'pass') return { ...result, shot };
278
+ return sent && copies === 1 ? { ...result, shot, recovery: 'the held line sent once' } : { outcome: 'ui-fail', reason: `held line sent ${String(copies)} times`, seen, shot };
279
+ } finally { await heal(); await entered.context.close(); }
280
+ },
281
+ // a second tab of the same device takes the seat; Play here takes it back
282
+ replaced: async item => {
283
+ const entered = await fresh(); const { page, context } = entered;
284
+ try {
285
+ await page.goto(URL_PLAY); await intoWorld(entered);
286
+ const second = await context.newPage(), started = Date.now();
287
+ await second.goto(URL_PLAY);
288
+ const seen = await screen(page, item.screen, 120_000, started), shot = seen === null ? undefined : await shoot(page, item.id, 'card');
289
+ const result = await judged(seen, item, page);
290
+ await second.close();
291
+ if (result.outcome !== 'pass') return { ...result, ...shot === undefined ? {} : { shot } };
292
+ const reloading = page.waitForEvent('load', { timeout: 30_000 }).catch(() => null);
293
+ await press(page, item.screen, 'Play here'); await reloading; await intoWorld(entered, 90_000).catch(() => undefined);
294
+ const back = await recovered(page, 30_000);
295
+ return back ? { ...result, ...shot === undefined ? {} : { shot }, injection: { secondTab: true }, recovery: 'Play here: seated' } : { outcome: 'ui-fail', reason: 'Play here never seated', seen, notes: await onScreen(page) };
296
+ } finally { await entered.context.close(); }
297
+ },
298
+ // --refusal=injection: a fault on a route this page never reads matches nothing, and that is a harness failure, never a pass
299
+ unmatched: async () => {
300
+ const entered = await fresh(); const { page } = entered;
301
+ try {
302
+ await page.goto(URL_PLAY); await intoWorld(entered);
303
+ const before = await control(); await arm('unmatched', [{ route: 'doctor', kind: 'status', code: 503 }]);
304
+ await sleep(5000);
305
+ const matches = await matchedSince(before, 'doctor');
306
+ return matches === 0 ? { outcome: 'harness-fail', reason: 'the doctor fault matched nothing in 5 s', injection: { matches } } : { outcome: 'pass', injection: { matches } };
307
+ } finally { await heal(); await entered.context.close(); }
308
+ },
309
+ // FM8 mode 14: the doctor's page doesn't answer its preflight; the world page stays, with a card
310
+ doctorHttp: async item => {
311
+ const entered = await fresh(); const { page } = entered;
312
+ try {
313
+ await page.goto(URL_PLAY); await intoWorld(entered);
314
+ const before = await control(), here = page.url(), started = Date.now();
315
+ await arm(item.id, [{ route: 'doctor', kind: 'status', code: 503 }]);
316
+ await page.evaluate(() => { document.dispatchEvent(new CustomEvent('ws:doctor', { detail: 'link' })); });
317
+ const seen = await screen(page, item.screen, 15_000, started), shot = await shoot(page, item.id, seen === null ? 'missing' : 'card');
318
+ const injection = { doctorMatches: await matchedSince(before, 'doctor'), stayed: page.url() === here };
319
+ await heal();
320
+ if (injection.doctorMatches === 0) return { outcome: 'harness-fail', reason: 'the doctor fault matched nothing', injection, notes: await onScreen(page) };
321
+ const result = await judged(seen, item, page);
322
+ if (result.outcome !== 'pass') return { ...result, injection, shot };
323
+ if (!injection.stayed) return { outcome: 'ui-fail', reason: 'the page left for a doctor that did not answer', seen, injection, shot };
324
+ await Promise.all([page.waitForURL(/\/doctor\//u, { timeout: 20_000 }), press(page, item.screen, 'Try again')]).catch(() => undefined);
325
+ return /\/doctor\//u.test(page.url()) ? { ...result, injection, shot, recovery: 'healed, Try again: the doctor opened' } : { outcome: 'ui-fail', reason: 'Try again never opened the doctor', seen, injection, shot };
326
+ } finally { await heal(); await entered.context.close(); }
327
+ },
328
+ // FM4b: the browser goes offline in the world
329
+ offlinePlay: async item => {
330
+ const entered = await fresh(); const { page, context } = entered;
331
+ try {
332
+ await page.goto(URL_PLAY); await intoWorld(entered);
333
+ const started = Date.now(); await context.setOffline(true);
334
+ const loopbackCut = await cutLoopback(true), injection = { setOffline: true, ...loopbackCut ? { loopbackCut } : {} };
335
+ const seen = await screen(page, item.screen, 8000, started), shot = seen === null ? undefined : await shoot(page, item.id, 'card');
336
+ await sleep(2000); await context.setOffline(false); if (loopbackCut) await heal();
337
+ const result = await judged(seen, item, page);
338
+ const back = result.outcome === 'pass' && await recovered(page, 30_000);
339
+ return result.outcome !== 'pass' ? { ...result, injection } : back ? { ...result, ...shot === undefined ? {} : { shot }, injection, recovery: 'online: a frame, no card' } : { outcome: 'ui-fail', reason: 'no recovery online', seen, notes: await onScreen(page) };
340
+ } finally { await context.setOffline(false).catch(() => undefined); await heal().catch(() => undefined); await entered.context.close(); }
341
+ },
342
+ // J298: a world the device holds loads offline and says so
343
+ offlineLaunch: async item => {
344
+ const entered = await fresh(); const { page, context } = entered;
345
+ try {
346
+ await page.goto(URL_PLAY); await intoWorld(entered);
347
+ // a first visit's worker takes control from the next load (no clients.claim on a world page): a second online visit is the warm one.
348
+ // Visits open the play address: the page drops its query once it starts, so a reload would land on the title
349
+ const controlling = () => page.waitForFunction(() => navigator.serviceWorker?.controller !== null && navigator.serviceWorker?.controller !== undefined, undefined, { timeout: 20_000 }).then(() => true, () => false);
350
+ if (!await controlling()) { await page.waitForFunction(async () => (await navigator.serviceWorker?.getRegistration())?.active !== undefined, undefined, { timeout: 60_000 }).catch(() => undefined); await page.goto(URL_PLAY); await intoWorld(entered); }
351
+ const controlled = await controlling();
352
+ if (!controlled) return { outcome: 'unsupported', reason: `${engine}: no controlling service worker after the warm visit, so nothing can launch offline` };
353
+ await sleep(3000);
354
+ await context.setOffline(true);
355
+ const loopbackCut = await cutLoopback(false), injection = { warmed: true, setOffline: true, ...loopbackCut ? { loopbackCut } : {} };
356
+ const started = Date.now(); await page.goto(URL_PLAY).catch(() => undefined);
357
+ await intoWorld(entered, 60_000).catch(() => undefined);
358
+ const seen = await screen(page, item.screen, 15_000, started), shot = seen === null ? await shoot(page, item.id, 'missing') : await shoot(page, item.id, 'card');
359
+ await context.setOffline(false); if (loopbackCut) await heal();
360
+ const result = await judged(seen, item, page);
361
+ if (result.outcome !== 'pass') return { ...result, shot, injection };
362
+ return await recovered(page, 45_000) ? { ...result, shot, injection, recovery: 'online: a frame, no card' } : { outcome: 'ui-fail', reason: 'no recovery online', seen, shot, notes: await onScreen(page) };
363
+ } finally { await context.setOffline(false).catch(() => undefined); await heal().catch(() => undefined); await entered.context.close(); }
364
+ },
365
+ // the page frozen (CDP), with or without a visibility change
366
+ freeze: async item => {
367
+ if (engine !== 'chromium') return { outcome: 'unsupported', reason: 'WebKit has no lifecycle freeze; a native-Safari Ring 2 proof is owed' };
368
+ const entered = await fresh(); const { page, context } = entered;
369
+ try {
370
+ await page.goto(URL_PLAY); await intoWorld(entered);
371
+ const cdp = await context.newCDPSession(page), awayMs = item.id === 'background-short' ? 4000 : 35_000, started = Date.now();
372
+ await cdp.send('Page.setWebLifecycleState', { state: 'frozen' });
373
+ await sleep(awayMs);
374
+ await cdp.send('Page.setWebLifecycleState', { state: 'active' });
375
+ const visibility = await page.evaluate(() => /** @type {string[]} */ (/** @type {any} */ (globalThis).__fmVisibility));
376
+ const seen = await screen(page, item.screen, 8000, Date.now()), shot = await shoot(page, item.id, seen === null ? 'missing' : 'card');
377
+ const injection = { frozenMs: Date.now() - started, visibility };
378
+ if (seen === null && item.id !== 'sleep-wake' && !visibility.includes('hidden'))
379
+ return { outcome: 'unsupported', reason: 'headless Chromium kept the page visible through the freeze; a real hidden/visible return (native-Safari Ring 2) is owed', injection, shot, notes: await onScreen(page) };
380
+ const result = await judged(seen, item, page);
381
+ if (result.outcome !== 'pass') return { ...result, injection, shot };
382
+ return await recovered(page, 30_000) ? { ...result, injection, shot, recovery: 'seated again' } : { outcome: 'ui-fail', reason: 'no recovery after the return', seen, injection, shot, notes: await onScreen(page) };
383
+ } finally { await entered.context.close(); }
384
+ },
385
+ // FM4b: slow and unstable links (measured RTT; the proxy holds frames)
386
+ linkQuality: async item => {
387
+ const entered = await fresh(); const { page } = entered;
388
+ try {
389
+ await page.goto(URL_PLAY); await intoWorld(entered);
390
+ const before = await control(), started = Date.now();
391
+ // Mode 22 (opus-of18): the proxy keeps a socket's frames in order, so per-frame jitter on a 60 Hz stream queues every frame behind
392
+ // the slowest and the pong RTT sits flat near the jitter's top (seen: no screen under ±200 ms). An unstable link is one whose
393
+ // delay swings: the driver moves the downstream delay between 20 ms and 420 ms every 2.5 s, so the last five pongs span over 250 ms.
394
+ const waving = { on: item.id === 'lossy' };
395
+ const wave = (async () => { for (let high = true; waving.on; high = !high) { await arm(item.id, [{ route: 'socket', kind: 'delay-frames', direction: 'down', ms: high ? 420 : 20, jitterMs: 0, seed: 7 }]); await sleep(2500); } })();
396
+ if (item.id === 'slow') await arm(item.id, [{ route: 'socket', kind: 'delay-frames', direction: 'both', ms: 160, jitterMs: 0, seed: 7 }]);
397
+ const seen = await screen(page, item.screen, 15_000, started).finally(() => { waving.on = false; }), shot = seen === null ? undefined : await shoot(page, item.id, 'chip');
398
+ await wave;
399
+ const injection = { socketMatches: await matchedSince(before, 'socket') };
400
+ await heal();
401
+ if (injection.socketMatches === 0) return { outcome: 'harness-fail', reason: 'the delay matched no frame', injection };
402
+ const result = await judged(seen, item, page);
403
+ if (result.outcome !== 'pass') return { ...result, injection };
404
+ const cleared = await page.locator(`[data-failmode~="${item.screen}"]`).waitFor({ state: 'detached', timeout: 20_000 }).then(() => true, () => false);
405
+ return cleared ? { ...result, injection, ...shot === undefined ? {} : { shot }, recovery: 'cleared after good seconds' } : { outcome: 'ui-fail', reason: 'the chip never cleared', seen, injection };
406
+ } finally { await heal(); await entered.context.close(); }
407
+ },
408
+ // FM5: the release file fails before the page can dial
409
+ releaseFault: async item => {
410
+ const signed = await (await fetch(`http://127.0.0.1:${String(Number(process.env.WS_RIG_PORTS) + 3)}/release.json`)).text();
411
+ const badSignature = JSON.stringify({ ...JSON.parse(signed), signature: '00'.repeat(64) });
412
+ /** @type {Record<string, object>} */
413
+ const rules = {
414
+ 'release-5xx': { route: 'release', kind: 'status', code: 503 },
415
+ 'release-429': { route: 'release', kind: 'status', code: 429, retryAfter: '3' },
416
+ 'release-404': { route: 'release', kind: 'status', code: 404 },
417
+ 'captive-portal': { route: 'release', kind: 'body', text: '<!doctype html><title>Wi-Fi sign in</title><form>Sign in to this network</form>', contentType: 'text/html' },
418
+ 'release-unsigned': { route: 'release', kind: 'body', text: badSignature, contentType: 'application/json' },
419
+ };
420
+ const rule = rules[item.id];
421
+ if (rule === undefined) throw new Error(`releaseFault has no rule for ${item.id}`);
422
+ const entered = await fresh(); const { page } = entered;
423
+ try {
424
+ const before = await control(); await arm(item.id, [rule]);
425
+ const started = Date.now(); await page.goto(URL_PLAY);
426
+ // the Name box comes before the read on a page with no seat yet
427
+ const seen = await (async () => { const until = Date.now() + 30_000; while (Date.now() < until) { if (await page.locator('#ws-name-box input').isVisible().catch(() => false)) { await page.locator('#ws-name-box input').fill('fmboot'); await page.locator('#ws-name-box button[type=submit]').click(); } const found = await screen(page, item.screen, 500, started); if (found !== null) return found; } return null; })();
428
+ const shot = await shoot(page, item.id, seen === null ? 'missing' : 'page');
429
+ const injection = { releaseMatches: await matchedSince(before, 'release'), upgrades: (await control()).counters.upgrades - before.counters.upgrades };
430
+ if (injection.releaseMatches === 0) return { outcome: 'harness-fail', reason: 'the release fault matched nothing', injection, notes: await onScreen(page) };
431
+ if (item.id === 'release-unsigned' && injection.upgrades > 0) return { outcome: 'ui-fail', reason: 'the page dialled on a release it could not verify', seen, injection, shot };
432
+ const result = await judged(seen, item, page);
433
+ await heal();
434
+ if (result.outcome !== 'pass') return { ...result, injection, shot };
435
+ await press(page, item.screen, 'Try').catch(() => undefined);
436
+ await intoWorld(entered, 60_000).catch(() => undefined);
437
+ return await recovered(page, 30_000) ? { ...result, injection, shot, recovery: 'healed: the world loads' } : { outcome: 'ui-fail', reason: 'no recovery after heal', seen, injection, shot, notes: await onScreen(page) };
438
+ } finally { await heal(); await entered.context.close(); }
439
+ },
440
+ // FM6: packs and the spawn book fail on the loading card
441
+ packsFault: async item => {
442
+ /** @type {Record<string, object>} */
443
+ const rules = {
444
+ 'packs-5xx': { route: 'packs', kind: 'status', code: 503 },
445
+ 'packs-429': { route: 'packs', kind: 'status', code: 429, retryAfter: '3' },
446
+ 'book-5xx': { route: 'book', kind: 'status', code: 503 },
447
+ 'download-failed': { route: 'packs', kind: 'status', code: 503 },
448
+ 'pack-broken': { route: 'packs', kind: 'truncate', bytes: 64 },
449
+ };
450
+ const rule = rules[item.id];
451
+ if (rule === undefined) throw new Error(`packsFault has no rule for ${item.id}`);
452
+ const entered = await fresh(); const { page } = entered, route = item.id === 'book-5xx' ? 'book' : 'packs';
453
+ try {
454
+ const before = await control(); await arm(item.id, [rule]);
455
+ const started = Date.now(); await page.goto(URL_PLAY);
456
+ const seen = await (async () => { const until = Date.now() + (item.id === 'download-failed' ? 120_000 : 40_000); while (Date.now() < until) { if (await page.locator('#ws-name-box input').isVisible().catch(() => false)) { await page.locator('#ws-name-box input').fill('fmload'); await page.locator('#ws-name-box button[type=submit]').click(); } const found = await screen(page, item.screen, 500, started); if (found !== null) return found; } return null; })();
457
+ const shot = await shoot(page, item.id, seen === null ? 'missing' : 'line');
458
+ const injection = { matches: await matchedSince(before, route) };
459
+ await heal();
460
+ if (injection.matches === 0) return { outcome: 'harness-fail', reason: `the ${route} fault matched nothing (the proxy learns pack URLs from the manifests it forwards)`, injection, notes: await onScreen(page) };
461
+ const result = await judged(seen, item, page);
462
+ if (result.outcome !== 'pass') return { ...result, injection, shot };
463
+ // the budgeted retries were spent before the line showed: healed, the card's Retry is the player's way on
464
+ // mode 36's page covers the card: its Try again is the same RETRY
465
+ const retry = item.id === 'download-failed' ? page.locator('[data-failmode~="download-failed"] button', { hasText: 'Try again' }).first() : page.locator('.ws-load-retry:visible').first();
466
+ const pressed = await retry.click({ timeout: 5000 }).then(() => true, () => false);
467
+ await intoWorld(entered, 90_000).catch(() => undefined);
468
+ return await recovered(page, 30_000) ? { ...result, injection, shot, recovery: pressed ? 'heal, Retry: the world loads' : 'heal: the world loads' } : { outcome: 'ui-fail', reason: 'no recovery after heal', seen, injection, shot, notes: await onScreen(page) };
469
+ } finally { await heal(); await entered.context.close(); }
470
+ },
471
+ // FM6: the Cache API refuses with QuotaExceededError (an injection at the public boundary, labelled; never a full disk)
472
+ storageFull: async item => {
473
+ const entered = await fresh(() => { Cache.prototype.put = () => Promise.reject(new DOMException('failmode-proof injected quota refusal', 'QuotaExceededError')); });
474
+ const { page } = entered;
475
+ try {
476
+ const started = Date.now(); await page.goto(URL_PLAY);
477
+ await intoWorld(entered, 90_000).catch(() => undefined);
478
+ const seen = await screen(page, item.screen, 10_000, started), shot = await shoot(page, item.id, seen === null ? 'missing' : 'line');
479
+ const result = await judged(seen, item, page), playing = await recovered(page, 15_000);
480
+ const injection = { cachePut: 'QuotaExceededError (page realm only; the service worker is not injected)' };
481
+ if (result.outcome !== 'pass') return { ...result, injection, shot };
482
+ return playing ? { ...result, injection, shot, recovery: 'plays on from memory' } : { outcome: 'ui-fail', reason: 'the world never played', seen, injection, shot };
483
+ } finally { await entered.context.close(); }
484
+ },
485
+ // FM4b mode 42: the device clock 2 min ahead; the Doctor measures it against the page's host
486
+ clockSkew: async item => {
487
+ const entered = await fresh(() => {
488
+ const Real = Date, skew = () => /** @type {number} */ (/** @type {any} */ (globalThis).__fmSkew ?? 120_000);
489
+ class Skewed extends Real { constructor(/** @type {any[]} */ ...given) { if (given.length === 0) super(Real.now() + skew()); else super(.../** @type {[any]} */ (given)); } static now() { return Real.now() + skew(); } }
490
+ globalThis.Date = /** @type {DateConstructor} */ (/** @type {unknown} */ (Skewed));
491
+ });
492
+ const { page } = entered;
493
+ try {
494
+ await page.goto(URL_PLAY); await intoWorld(entered);
495
+ const started = Date.now();
496
+ await Promise.all([page.waitForURL(/\/doctor\//u, { timeout: 20_000 }), page.evaluate(() => { document.dispatchEvent(new CustomEvent('ws:doctor', { detail: 'link' })); })]);
497
+ const seen = await screen(page, item.screen, 45_000, started), shot = await shoot(page, item.id, seen === null ? 'missing' : 'doctor');
498
+ const result = await judged(seen, item, page), injection = { pageClockAheadMs: 120_000 };
499
+ if (result.outcome !== 'pass') return { ...result, injection, shot };
500
+ await page.evaluate(() => { /** @type {any} */ (globalThis).__fmSkew = 0; });
501
+ await page.locator('button', { hasText: 'Run again' }).first().click();
502
+ const cleared = await page.locator(`[data-failmode~="${item.screen}"]`).waitFor({ state: 'detached', timeout: 45_000 }).then(() => true, () => false);
503
+ return cleared ? { ...result, injection, shot, recovery: 'Run again with the clock right: no warning' } : { outcome: 'ui-fail', reason: 'the warning stayed after Run again with the clock right', seen, injection, shot };
504
+ } finally { await entered.context.close(); }
505
+ },
506
+ // FM5 mode 46: a release signed for staging (loopback fixture only)
507
+ staging: async item => {
508
+ rig.publish(2, rig.build, 'staging');
509
+ const entered = await fresh(); const { page } = entered;
510
+ try {
511
+ await page.goto(URL_PLAY); await intoWorld(entered);
512
+ const seen = await screen(page, item.screen, 10_000), shot = await shoot(page, item.id, seen === null ? 'missing' : 'notice');
513
+ const result = await judged(seen, item, page);
514
+ return { ...result, shot, injection: { channel: 'staging', loopback: true }, ...result.outcome === 'pass' ? { recovery: 'stays for the session' } : {} };
515
+ } finally { rig.publish(3, rig.build, 'production'); await entered.context.close(); }
516
+ },
517
+ };
518
+
519
+ let lastPlay = 0;
520
+ for (const item of cases) {
521
+ const started = Date.now();
522
+ /** @type {Result} */
523
+ let result;
524
+ if (item.blocked !== undefined) result = { outcome: 'blocked-dependency', reason: item.blocked };
525
+ else if (item.chromiumOnly === true && engine !== 'chromium') result = { outcome: 'unsupported', reason: `${engine} cannot produce this lifecycle fault; a native-Safari Ring 2 proof is owed` };
526
+ else {
527
+ const runner = runners[item.run ?? ''];
528
+ if (runner === undefined) throw new Error(`no runner ${String(item.run)} for ${item.id}`);
529
+ // the server's 10 connects a minute per address: play cases wait it out, never lift it
530
+ if (item.phase !== 'boot' || item.id === 'offline-launch') { const wait = lastPlay + cooldownMs - Date.now(); if (wait > 0) { console.log(`.. ${item.id}: waiting ${String(Math.round(wait / 1000))} s for the connect window`); await sleep(wait); } }
531
+ try { result = await runner(item); } catch (error) { result = { outcome: 'harness-fail', reason: String(error).slice(0, 400) }; }
532
+ lastPlay = Date.now();
533
+ }
534
+ const row = { mode: item.mode, id: item.id, owner: item.owner, screen: item.screen, inject: item.inject, ms: Date.now() - started, ...result };
535
+ rows.push(row);
536
+ console.log(`${result.outcome.padEnd(18)} ${item.mode.padEnd(4)} ${item.id}${result.reason === undefined ? '' : `: ${result.reason}`}`);
537
+ if (result.outcome === 'harness-fail' && !baseline) break;
538
+ }
539
+ } catch (error) {
540
+ harnessFault = String(error).slice(0, 600);
541
+ console.log(`harness-fail: ${harnessFault}`);
542
+ } finally {
543
+ const cleanup = /** @type {string[]} */ ([]);
544
+ await launched?.close().catch((/** @type {unknown} */ error) => { cleanup.push(`browser: ${String(error)}`); });
545
+ await proxy?.close().catch((/** @type {unknown} */ error) => { cleanup.push(`proxy: ${String(error)}`); });
546
+ await rig.close().catch((/** @type {unknown} */ error) => { cleanup.push(`rig: ${String(error)}`); });
547
+ if (cleanup.length > 0) harnessFault = `${harnessFault ?? ''} cleanup: ${cleanup.join('; ')}`.trim();
548
+ }
549
+ const code = harnessFault !== null ? 2 : verdict(rows);
550
+ /** @type {Record<string, number>} */
551
+ const counts = {};
552
+ for (const row of rows) counts[row.outcome] = (counts[row.outcome] ?? 0) + 1;
553
+ writeFileSync(join(out, 'receipt.json'), JSON.stringify({ schema: 'failmode-proof/1', sha, engine, browser: engine, baseline, at: new Date().toISOString(), pages: PAGE_ORIGIN, socket: SOCKET,
554
+ saves: pgBin === 'memory:' ? 'memory (no persistence case selected reads them)' : 'owned Postgres', harnessFault, counts, exit: code, cases: rows }, null, 1));
555
+ console.log(`${JSON.stringify(counts)} -> ${join(out, 'receipt.json')}`);
556
+ process.exit(code);
@@ -8,16 +8,17 @@
8
8
  // `test/grid-crossing-ratchet.json` is red. Every run appends one line to ~/.cache/wildshard/lane-receipts.log (`grid-nightly DONE` or
9
9
  // `grid-nightly RED`, naming the red pairs), which the coordinator reads to open a QA-FEEDBACK row. State (the last serial and night) is
10
10
  // <root>/state.json.
11
- // node scripts/grid-crossing-nightly.mjs [--root=<dir>] [--force=release|nightly] [--dry-run]
11
+ // node scripts/grid-crossing-nightly.mjs [--root=<dir>] [--force=release|nightly] [--dry-run] [--ratchet=<file>]
12
+ // --ratchet hands the report another ratchet file (the seeded break that proves a worse pair turns the run red).
12
13
  // --root defaults to ~/.cache/wildshard/plan-artifacts/grid/nightly. --dry-run prints what is due and runs nothing.
13
14
  import { spawnSync } from 'node:child_process';
14
15
  import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
15
16
  import { homedir } from 'node:os';
16
- import { join } from 'node:path';
17
+ import { join, resolve } from 'node:path';
17
18
 
18
19
  /** @param {string} name @param {string} fallback */
19
20
  const flag = (name, fallback) => process.argv.find(arg => arg.startsWith(`--${name}=`))?.slice(name.length + 3) ?? fallback;
20
- const root = flag('root', join(homedir(), '.cache/wildshard/plan-artifacts/grid/nightly')), force = flag('force', ''), dry = process.argv.includes('--dry-run');
21
+ const root = flag('root', join(homedir(), '.cache/wildshard/plan-artifacts/grid/nightly')), force = flag('force', ''), dry = process.argv.includes('--dry-run'), ratchet = flag('ratchet', '') === '' ? '' : resolve(flag('ratchet', ''));
21
22
  const RECEIPTS = join(homedir(), '.cache/wildshard/lane-receipts.log'), STATE = join(root, 'state.json'), here = join(import.meta.dirname, '..');
22
23
  mkdirSync(root, { recursive: true });
23
24
  /** @type {{ serial: number | null, night: string | null }} */
@@ -46,7 +47,7 @@ for (const kind of due) {
46
47
  ? run(['scripts/grid-crossing-qa.mjs', '--base=https://staging.wildshard.io', `--out=${join(dir, 'walks')}`])
47
48
  : run(['scripts/grid-crossing-tour.mjs', '--base=https://preview.wildshard.io', `--out=${join(dir, 'tour')}`, '--lane=grid-nightly']);
48
49
  const runs = kind === 'release' ? join(dir, 'walks/rows.json') : join(dir, 'tour');
49
- const report = spawnSync('node', ['scripts/grid-crossing-report.mjs', join(dir, 'report'), `--runs=${runs}`], { cwd: here, encoding: 'utf8' });
50
+ const report = spawnSync('node', ['scripts/grid-crossing-report.mjs', join(dir, 'report'), `--runs=${runs}`, ...(ratchet === '' ? [] : [`--ratchet=${ratchet}`])], { cwd: here, encoding: 'utf8' });
50
51
  writeFileSync(join(dir, 'report.log'), `${report.stdout}${report.stderr}`);
51
52
  const worse = report.stdout.split('\n').filter(line => line.startsWith('WORSE ')).slice(0, 8).join('; ');
52
53
  const ok = report.status === 0;