@wildshard/engine 0.1.23 → 0.1.24

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 (99) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/index.html +17 -4
  3. package/lint/engine-sides.json +7 -0
  4. package/package.json +29 -3
  5. package/scripts/app-build/titlePreloads.mjs +8 -5
  6. package/scripts/bake-road-surfaces.entry.ts +18 -0
  7. package/scripts/bake-road-surfaces.mjs +44 -0
  8. package/scripts/changelog.mjs +2 -1
  9. package/scripts/goldens/capture.mjs +18 -6
  10. package/scripts/goldens/native-residency.d.mts +1 -0
  11. package/scripts/goldens/native-residency.mjs +14 -1
  12. package/scripts/grid-road-lag.mjs +209 -0
  13. package/scripts/keep-copy.mjs +64 -12
  14. package/scripts/land.d.mts +4 -1
  15. package/scripts/land.mjs +85 -3
  16. package/scripts/loading-flow/benchmax.mjs +2 -0
  17. package/scripts/map-hash.mjs +1 -1
  18. package/scripts/nightly-tag.mjs +2 -4
  19. package/scripts/page-release/content.mjs +19 -10
  20. package/scripts/page-release/flow.d.mts +3 -2
  21. package/scripts/page-release/flow.mjs +31 -21
  22. package/scripts/page-release/packs.d.mts +5 -0
  23. package/scripts/page-release/packs.mjs +13 -3
  24. package/scripts/page-release/preview.d.mts +2 -0
  25. package/scripts/page-release/preview.mjs +74 -0
  26. package/scripts/page-release.mjs +13 -5
  27. package/scripts/road-drive-proof.mjs +269 -0
  28. package/scripts/road-memory-probe.mjs +129 -0
  29. package/scripts/scratch-dir.mjs +4 -4
  30. package/scripts/server-release/github.mjs +59 -26
  31. package/scripts/server-release/publish.mjs +15 -1
  32. package/scripts/server-release/sign.mjs +8 -4
  33. package/scripts/test_prebuilt_output.py +3 -1
  34. package/scripts/turn-in-shots.mjs +83 -0
  35. package/scripts/vitest-bench-observe-proof.py +168 -0
  36. package/scripts/vitest-bench.py +122 -77
  37. package/scripts/vps/rebuild.sh +3 -1
  38. package/scripts/worktree-reap.mjs +21 -9
  39. package/scripts/worktree.mjs +1 -1
  40. package/scripts/zone-gate-proof.mjs +93 -0
  41. package/src/client/engine/boot/sw.ts +3 -1
  42. package/src/client/engine/core/ktx2.ts +30 -9
  43. package/src/client/engine/models/place.ts +7 -5
  44. package/src/client/engine/pwa/sw.js +57 -2
  45. package/src/client/engine/render/precompile.ts +6 -0
  46. package/src/client/engine/render/tiers.ts +9 -0
  47. package/src/client/engine/ui/playerReload.ts +5 -5
  48. package/src/client/game/boot/bytes.generated.ts +4 -1
  49. package/src/client/game/boot/versions.generated.ts +8 -5
  50. package/src/client/game/grid/cellClip.ts +70 -6
  51. package/src/client/game/grid/gateCard.css +9 -0
  52. package/src/client/game/grid/gateCard.ts +67 -0
  53. package/src/client/game/grid/live.ts +17 -3
  54. package/src/client/game/grid/liveSession.ts +15 -5
  55. package/src/client/game/grid/neighbourLife.ts +10 -1
  56. package/src/client/game/grid/neighbourPanels.ts +29 -2
  57. package/src/client/game/grid/pageEntry.ts +21 -0
  58. package/src/client/game/grid/regionalWorld.ts +5 -0
  59. package/src/client/game/grid/roadCompressedSurfaces.ts +69 -0
  60. package/src/client/game/grid/roadLook.ts +5 -73
  61. package/src/client/game/grid/roadLookPlatform.ts +11 -6
  62. package/src/client/game/grid/roadSurfaceMaps.ts +80 -0
  63. package/src/client/game/grid/roadTilePool.ts +4 -0
  64. package/src/client/game/grid/session.ts +52 -12
  65. package/src/client/game/grid/softWallLook.ts +6 -1
  66. package/src/client/game/plugin/ui.ts +3 -0
  67. package/src/client/game/session/play.ts +3 -0
  68. package/src/client/game/shardfile/clientSource.ts +5 -2
  69. package/src/client/game/shardfile/clientWorker.ts +3 -2
  70. package/src/client/game/shardfile/slabLibrary.ts +37 -0
  71. package/src/client/game/shardfile/slabRanges.ts +145 -0
  72. package/src/client/host/online/shell.ts +2 -2
  73. package/src/client/host/standalone/shell.ts +4 -1
  74. package/src/server/game/grid/simulation.ts +27 -4
  75. package/src/server/game/plugin/simulation.ts +2 -0
  76. package/src/server/game/shardfile/dataRespawn.ts +47 -0
  77. package/src/server/host/standalone/cellWorld.ts +1 -1
  78. package/src/server/host/standalone/gridServer.ts +18 -6
  79. package/src/server/host/standalone/roadWorld.ts +30 -0
  80. package/src/server/host/standalone/serverHost.ts +2 -2
  81. package/src/shared/engine/entities/AnimalSim.ts +10 -0
  82. package/src/shared/engine/physics/transferWalls.ts +9 -4
  83. package/src/shared/game/grid/allocator.ts +8 -1
  84. package/src/shared/game/grid/gateCount.ts +27 -0
  85. package/src/shared/game/grid/zoneWalls.ts +64 -0
  86. package/src/shared/game/strings.ts +2 -0
  87. package/src/shared/host/standalone/harnessPlace.ts +4 -0
  88. package/src/test/engine/simState.ts +134 -0
  89. package/src/test/game/builtProduct.ts +6 -0
  90. package/src/test/game/headless/admittedProductCache.ts +61 -0
  91. package/src/test/game/headless/checkedReplayPipeline.ts +21 -0
  92. package/src/test/game/headless/nativeBounds.ts +27 -0
  93. package/src/test/game/headless/processUsage.ts +22 -0
  94. package/src/test/game/headless/productSession.ts +39 -0
  95. package/src/test/game/headless/ropeBodies.ts +60 -0
  96. package/src/test/game/headless/worker.ts +34 -0
  97. package/src/test/game/headlessNative.ts +114 -0
  98. package/src/test/game/headlessWorker.ts +43 -0
  99. package/src/test/tsconfig.game.json +3 -0
@@ -106,6 +106,20 @@ function isAncestor(older, newer) {
106
106
  return result.status === 0;
107
107
  }
108
108
 
109
+ /** Main publishes policy; release refs may own a newer source tree.
110
+ * @param {string} build @param {string} head @param {string | undefined} channel @param {string | undefined} tag */
111
+ function releaseHistoryContains(build, head, channel, tag) {
112
+ if (isAncestor(build, head)) return true;
113
+ const refs = execFileSync('git', ['for-each-ref', '--format=%(refname:short) %(objectname)', 'refs/remotes/origin/release/'], { encoding: 'utf8' }).trim().split('\n')
114
+ .map(line => line.split(' ')).filter(([ref = '']) => /^origin\/release\/v\d+\.\d+\.\d+$/u.test(ref));
115
+ if (channel === 'staging') return tag === `server-staging-${build}` && refs.some(([, tip]) => tip === build);
116
+ if (channel !== 'production' || tag === undefined || !/^v\d+\.\d+\.\d+$/u.test(tag)) return false;
117
+ try {
118
+ if (execFileSync('git', ['rev-parse', '--verify', `refs/tags/${tag}^{commit}`], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim() !== build) return false;
119
+ } catch { return false; }
120
+ return refs.some(([ref, tip]) => ref === `origin/release/${tag}` && tip !== undefined && isAncestor(build, tip));
121
+ }
122
+
109
123
  /** CLI runs only after CI has uploaded the exact immutable asset. @returns {Promise<void>} */
110
124
  async function main() {
111
125
  const { values } = parseArgs({ options: { channel: { type: 'string' }, build: { type: 'string' }, tag: { type: 'string' }, bundle: { type: 'string' }, out: { type: 'string' }, check: { type: 'boolean', default: false },
@@ -114,7 +128,7 @@ async function main() {
114
128
  // The build's epoch and pins are read from that commit; the workflow builds its bundle in its own tree and checks the tag's asset.
115
129
  // `--align` (staging only) is a person naming a build, never older than the live one.
116
130
  const align = values.align, head = execFileSync('git', ['rev-parse', 'HEAD'], { encoding: 'utf8' }).trim();
117
- if (values.build === undefined || !COMMIT.test(values.build) || !isAncestor(values.build, head)) throw new Error('The build must be a full commit on the checked-out main');
131
+ if (values.build === undefined || !COMMIT.test(values.build) || !releaseHistoryContains(values.build, head, values.channel, values.tag)) throw new Error('The build must be a full commit in published main or release history');
118
132
  if (align && values.channel !== 'staging') throw new Error('--align is staging only');
119
133
  /** @param {string} path @returns {string} */
120
134
  const sourceFile = path => execFileSync('git', ['show', `${values.build ?? ''}:${path}`], { encoding: 'utf8' });
@@ -11,15 +11,19 @@ export { DEFAULT_SERVER_SHARDS, servedSlug } from './served-slug.mjs';
11
11
  /** @typedef {import('../../src/shared/host/releaseRows.ts').ReleaseFile} ReleaseFile */
12
12
  export const COMMIT = /^[0-9a-f]{40}$/u;
13
13
 
14
- /** CI publications always name their immutable bundle and public socket.
14
+ /** Online publications name their immutable bundle and socket; preview is a signed solo deployment.
15
15
  * @param {unknown} value @returns {ReleaseFile} */
16
16
  export function publication(value) {
17
17
  const row = v.parse(ReleaseSchema, value);
18
- if (row.channel === 'preview' || !Number.isSafeInteger(row.serial) || row.serial < 1 || !COMMIT.test(row.engineBuild)
18
+ if (!Number.isSafeInteger(row.serial) || row.serial < 1 || !COMMIT.test(row.engineBuild)
19
19
  || !Number.isSafeInteger(row.saveEpoch) || row.saveEpoch < 1 || row.shards.length === 0
20
20
  || row.shards.some(shard => !/^[a-z0-9_-]+$/u.test(shard.slug) || !Number.isSafeInteger(shard.revision))
21
- || new Set(row.shards.map(shard => shard.slug)).size !== row.shards.length
22
- || row.bundle === undefined || !/^[\w][\w.+-]*\/[\w][\w.+-]*\.tar\.gz$/u.test(row.bundle.name)
21
+ || new Set(row.shards.map(shard => shard.slug)).size !== row.shards.length) throw new Error('Invalid release publication');
22
+ if (row.channel === 'preview') {
23
+ if (row.socket !== undefined || row.bundle !== undefined) throw new Error('Preview publication must be solo');
24
+ return row;
25
+ }
26
+ if (row.bundle === undefined || !/^[\w][\w.+-]*\/[\w][\w.+-]*\.tar\.gz$/u.test(row.bundle.name)
23
27
  || row.socket === undefined) throw new Error('Invalid server publication');
24
28
  const socket = new URL(row.socket);
25
29
  if (socket.protocol !== 'wss:' || socket.username !== '' || socket.password !== '' || socket.hash !== '') throw new Error('A public wss socket is required');
@@ -21,7 +21,9 @@ class PrebuiltTests(unittest.TestCase):
21
21
  def setUp(self):
22
22
  Path('tmp').mkdir(exist_ok=True)
23
23
  self.temporary = tempfile.TemporaryDirectory(dir='tmp')
24
- self.root = Path(self.temporary.name)
24
+ # Python versions returning a relative temporary path must still make
25
+ # the external-directory symlink target an actual absolute owner.
26
+ self.root = Path(self.temporary.name).resolve()
25
27
  self.source, self.target, self.packet = [self.root / name for name in ('source', 'target', 'packet')]
26
28
  self.target.mkdir()
27
29
  version = json.dumps({'build': SHA[:9] + '-fixture'})
@@ -0,0 +1,83 @@
1
+ #!/usr/bin/env node
2
+ // ZONE-ENTRY ZE7: every cell's four turn-ins, seen from the road. From a home entered by Shard Select, the harness pose stands the
3
+ // walker on the road's centre line at each other cell's four entryways (north, east, south, west), facing into the cell, and takes
4
+ // one shot: what a player on the road sees of the 24 placeholders (no grey or snow-white stand-in within sight of the road).
5
+ // scripts/browser-lane.sh node scripts/turn-in-shots.mjs --base=<preview or loopback url> --out=<dir> [--home=driftwood]
6
+ // [--engine=chromium|webkit] [--device='iPhone 15 Pro'] [--viewport=2992x1434@2] [--cells=slug,slug] [--settle=3000]
7
+ // Writes <out>/<engine>[-phone]-<slug>-<side>.jpg and turn-ins.json. Preview or a loopback page only (the harness pose).
8
+ import { mkdirSync, writeFileSync } from 'node:fs';
9
+ import { join, resolve as absolute } from 'node:path';
10
+ import { chromium, devices, webkit } from 'playwright';
11
+ import { KEYCHAIN_ARGS } from './chromium-keychain.mjs';
12
+ import { saveFixture } from './debug-settings.mjs';
13
+
14
+ /** @param {string} name @param {string} fallback */
15
+ const flag = (name, fallback) => process.argv.find(arg => arg.startsWith(`--${name}=`))?.slice(name.length + 3) ?? fallback;
16
+ const base = flag('base', '').replace(/\/$/u, ''), out = absolute(flag('out', '')), engineName = flag('engine', 'chromium'), device = flag('device', '');
17
+ const settle = Number(flag('settle', '3000')), only = flag('cells', '').split(',').filter(Boolean);
18
+ if (!/^https:\/\/preview\.wildshard\.io$|^http:\/\/127\.0\.0\.1:\d+$/u.test(base) || out === absolute('')) throw new Error('usage: turn-in-shots.mjs --base=<preview or loopback url> --out=<dir> [--home=driftwood] [--engine=chromium|webkit] [--device=...]');
19
+ const HOMES = /** @type {Record<string, { slug: string, card: string }>} */ ({ driftwood: { slug: 'driftwood-isle', card: 'Driftwood Isle' }, nalati: { slug: 'nalati-grasslands', card: 'Nalati Grasslands' } });
20
+ const homeName = flag('home', 'driftwood'), home = HOMES[homeName];
21
+ if (home === undefined) throw new Error(`No home ${homeName}: ${Object.keys(HOMES).join(', ')}`);
22
+ const desk = (/^(\d+)x(\d+)@([\d.]+)$/u.exec(flag('viewport', '2992x1434@2')) ?? [0, '2992', '1434', '2']).slice(1).map(Number);
23
+ const sleep = (/** @type {number} */ ms) => new Promise(resolve => { setTimeout(resolve, ms); });
24
+ /** The cell's half size and the gap's half (road and both strips): the road's centre line runs HALF + GAP_HALF off a cell's centre. */
25
+ const HALF = 250, GAP_HALF = 31.25;
26
+ const SIDES = /** @type {const} */ ([['north', 0, 1], ['east', 1, 0], ['south', 0, -1], ['west', -1, 0]]);
27
+ mkdirSync(out, { recursive: true });
28
+
29
+ const tag = `${engineName}${device === '' ? '' : '-phone'}`;
30
+ const result = { base, home: home.slug, engine: engineName, device, at: new Date().toISOString(), build: '', shots: /** @type {Record<string, unknown>[]} */ ([]), errors: /** @type {string[]} */ ([]), note: '' };
31
+ const browser = await (engineName === 'webkit' ? webkit : chromium).launch({ headless: true, args: engineName === 'chromium' ? [...KEYCHAIN_ARGS, '--mute-audio', '--use-angle=metal', '--ignore-gpu-blocklist'] : [] });
32
+ let code = 1;
33
+ try {
34
+ result.build = await fetch(`${base}/version.json`).then(async found => String((await found.json()).build)).catch(() => '');
35
+ const phone = device === '' ? undefined : devices[device];
36
+ if (device !== '' && phone === undefined) throw new Error(`No Playwright device ${device}`);
37
+ const context = await browser.newContext(phone ?? { viewport: { width: desk[0] ?? 2992, height: desk[1] ?? 1434 }, deviceScaleFactor: desk[2] ?? 2 }), page = await context.newPage();
38
+ page.on('pageerror', error => { result.errors.push(error.message.slice(0, 240)); });
39
+ await saveFixture(context, { scope: 'device', key: 'firstPlayDone', data: true });
40
+ await saveFixture(context, { scope: 'device', key: 'devMode', data: true });
41
+ await saveFixture(context, { scope: 'global', key: 'settings', merge: true, data: { volume: 0 } });
42
+ await page.goto(`${base}/?mute=1&nolock=1`, { waitUntil: 'load' });
43
+ await page.locator('.ws-main-select').click({ timeout: 120_000 });
44
+ if (await page.locator('#ws-name-box input').waitFor({ timeout: 30_000 }).then(() => true).catch(() => false)) { await page.locator('#ws-name-box input').fill('turnins'); await page.locator('#ws-name-box button[type=submit]').click(); }
45
+ await page.locator('.ws-menu-card').first().waitFor({ state: 'visible', timeout: 30_000 });
46
+ const index = await page.evaluate(card => [...document.querySelectorAll('.ws-menu-card')].findIndex(row => (row.textContent ?? '').includes(card)), home.card);
47
+ if (index < 0) throw new Error(`No ${home.card} card`);
48
+ await page.locator('.ws-menu-dots i').nth(index).click();
49
+ await page.locator('.ws-menu-play').click();
50
+ await page.waitForFunction(() => /** @type {any} */ (window).__wildshard?.shard?.grid?.state?.().live?.live?.gameplayReady === true, undefined, { timeout: 180_000, polling: 100 });
51
+ await sleep(2000);
52
+ await page.getByText('DONE', { exact: true }).click({ timeout: 2000 }).catch(() => undefined);
53
+ const grid = await page.evaluate(() => {
54
+ const W = /** @type {any} */ (window), st = W.__wildshard.shard.grid.state(), wf = st.live.live.worldFeet, p = W.__wildshard.world.player.position;
55
+ return { origin: { x: wf.x - p.x, z: wf.z - p.z }, cells: /** @type {{ slug: string, cell: [number, number] }[]} */ (st.cells).map(row => ({ slug: row.slug, cell: row.cell })) };
56
+ });
57
+ const own = grid.cells.find(row => row.slug === home.slug);
58
+ if (own === undefined) throw new Error(`${home.slug} is not on the grid`);
59
+ const pitch = own.cell[0] !== 0 ? grid.origin.x / own.cell[0] : own.cell[1] !== 0 ? grid.origin.z / own.cell[1] : 562.5;
60
+ for (const cell of grid.cells) {
61
+ if (cell.slug === home.slug || (only.length > 0 && !only.includes(cell.slug))) continue;
62
+ for (const [side, sx, sz] of SIDES) {
63
+ // home-frame feet on the road's centre line in front of this entryway, facing the cell's centre
64
+ const cx = cell.cell[0] * pitch - grid.origin.x, cz = cell.cell[1] * pitch - grid.origin.z;
65
+ const x = cx + sx * (HALF + GAP_HALF), z = cz + sz * (HALF + GAP_HALF), yaw = Math.atan2(x - cx, z - cz);
66
+ await page.evaluate(([px, pz, py]) => { const p = /** @type {any} */ (window).__wildshard.world.player; p.spawn(px, pz, py, 1.5); p.link?.place?.(p.position.x, p.position.y, p.position.z, py); }, [x, z, yaw]);
67
+ await sleep(settle);
68
+ await page.evaluate(py => { /** @type {any} */ (window).__wildshard.world.player.yaw = py; }, yaw);
69
+ await sleep(300);
70
+ const file = `${tag}-${cell.slug}-${side}.jpg`;
71
+ await page.screenshot({ path: join(out, file), type: 'jpeg', quality: 70 });
72
+ const seen = await page.evaluate(() => { const st = /** @type {any} */ (window).__wildshard.shard.grid.state(); return { inside: st.inside ?? null, card: st.cellLoading ?? null, mb: st.playingMB ?? null }; });
73
+ result.shots.push({ cell: cell.slug, side, file, ...seen });
74
+ console.log(`shot ${cell.slug} ${side}`);
75
+ }
76
+ }
77
+ code = 0;
78
+ } catch (error) { result.note = String(error).slice(0, 400); console.log(`FAIL ${result.note}`); }
79
+ finally {
80
+ writeFileSync(join(out, `${tag}-turn-ins.json`), `${JSON.stringify(result, null, 2)}\n`);
81
+ await browser.close();
82
+ }
83
+ process.exit(code);
@@ -0,0 +1,168 @@
1
+ #!/usr/bin/env python3
2
+ """TS12a outer-edge proof: real gate argv, measured descendants and an unsignalled orphan."""
3
+ import argparse
4
+ import importlib.util
5
+ import json
6
+ from pathlib import Path
7
+ import re
8
+ import shutil
9
+ import subprocess
10
+ import sys
11
+ import tempfile
12
+ import time
13
+
14
+ ROOT = Path(__file__).resolve().parents[1]
15
+ sys.dont_write_bytecode = True
16
+
17
+
18
+ def module(name, path):
19
+ spec = importlib.util.spec_from_file_location(name, path)
20
+ loaded = importlib.util.module_from_spec(spec)
21
+ spec.loader.exec_module(loaded)
22
+ return loaded
23
+
24
+
25
+ bench = module('bench', ROOT / 'scripts/vitest-bench.py')
26
+ controls = module('controls', ROOT / 'scripts/cpu-census-proof.py')
27
+
28
+
29
+ def cli(work, *options):
30
+ return [sys.executable, str(ROOT / 'scripts/vitest-bench.py'), 'observe',
31
+ '--out', str(work / 'summary.json'), '--interval', '.05', '--seconds', '6',
32
+ '--drain-seconds', '.2', *options]
33
+
34
+
35
+ def first_sample(work):
36
+ reports = list(work.glob('summary.*.jsonl'))
37
+ return any('"type":"sample"' in report.read_text() for report in reports)
38
+
39
+
40
+ def finish(observer, work, expected):
41
+ stdout, stderr = observer.communicate(timeout=8)
42
+ assert observer.returncode == expected, (stdout, stderr)
43
+ data = json.loads((work / 'summary.json').read_text())
44
+ assert data['schema'] == 'vitest-gate-census/2' and len(data['observations']) == 1, data
45
+ row = data['observations'][0]
46
+ assert row['gateKind'] == 'unverified' and row['exactGateCpu'] is None, row
47
+ assert 'cpuSeconds' not in row and 'userSeconds' not in row and 'systemSeconds' not in row, row
48
+ assert row['samples'] >= 2 and row['peakSummedRssMB'] > 0, row
49
+ assert (ROOT / row['rawReport']).is_file()
50
+ return row
51
+
52
+
53
+ def descendants(work, leak):
54
+ work.mkdir()
55
+ root = subprocess.Popen([sys.executable, str(ROOT / 'scripts/cpu-census-proof.py'),
56
+ '--fixture', str(work), str(int(leak))], cwd=ROOT,
57
+ stdout=subprocess.PIPE, stderr=subprocess.PIPE, start_new_session=True)
58
+ observer = None
59
+ try:
60
+ controls.wait_for(lambda: (work / 'ready').exists())
61
+ children = json.loads((work / 'ready').read_text())
62
+ observer = subprocess.Popen(cli(work, '--pid', str(root.pid)), cwd=ROOT,
63
+ stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
64
+ controls.wait_for(lambda: first_sample(work))
65
+ (work / 'go').touch()
66
+ # The real gate parent reaps its owner. Retaining a zombie would make
67
+ # the recorder correctly report an unreadable, uncertain identity.
68
+ assert root.wait(timeout=4) == 0
69
+ row = finish(observer, work, 2 if leak else 0)
70
+ if leak:
71
+ assert row['reason'] == 'live observed descendants after root exit', row
72
+ assert not row['allObservedChildrenDrained'], row
73
+ assert any(child['pid'] == children[0] for child in row['remaining']), row
74
+ (work / 'after-observer').touch()
75
+ controls.wait_for(lambda: (work / 'writer-survived').exists() and (work / 'writer-cpu').exists())
76
+ return {'orphanReported': True, 'writerNotSignalled': True}
77
+ assert row['allObservedChildrenDrained'] and not row['remaining'], row
78
+ assert row['processesByKind'].get('python', 0) >= 3, row
79
+ busy_cpu = int((work / 'busy-cpu').read_text()) / 1e9
80
+ assert row['sampledCpuSecondsLowerBound'] >= busy_cpu * .6, row
81
+ assert row['sampledCpuSecondsLowerBound'] < busy_cpu + .5, row
82
+ return {'sampledCpuLowerBound': row['sampledCpuSecondsLowerBound'],
83
+ 'busyCpuIndependent': busy_cpu, 'drained': True, 'exactTotalAbsent': True}
84
+ finally:
85
+ (work / 'go').touch()
86
+ (work / 'after-observer').touch()
87
+ if observer is not None and observer.poll() is None:
88
+ observer.terminate()
89
+ observer.wait(timeout=2)
90
+ if root.poll() is None:
91
+ root.terminate()
92
+ root.wait(timeout=2)
93
+
94
+
95
+ def discovery(work):
96
+ (work / '.githooks').mkdir(parents=True)
97
+ script = work / '.githooks/push-checks.mjs'
98
+ stop = work / 'stop'
99
+ script.write_text("import {existsSync,writeFileSync} from 'node:fs';\n"
100
+ "writeFileSync(process.argv[2],'ready');\n"
101
+ "setInterval(()=>{if(existsSync(process.argv[3]))process.exit(0);},10);\n")
102
+ node = shutil.which('node')
103
+ owners, observer = [], None
104
+ try:
105
+ # Older than both Node roots, with the old regex embedded in its argv.
106
+ owners.append(subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(10)',
107
+ 'node', '.githooks/push-checks.mjs'], cwd=work))
108
+ for entry, label in [(str(script), 'absolute'), ('.githooks/push-checks.mjs', 'relative')]:
109
+ owners.append(subprocess.Popen([node, entry, str(work / label), str(stop)], cwd=work))
110
+ controls.wait_for(lambda: (work / 'absolute').exists() and (work / 'relative').exists())
111
+ table = subprocess.check_output(['ps', '-p', ','.join(str(owner.pid) for owner in owners),
112
+ '-o', 'pid=,uid=,command='], text=True)
113
+ roots = bench.gate_candidates(table, re.compile(bench.GATE_ROOT))
114
+ assert roots == sorted(owner.pid for owner in owners[1:]), (roots, table)
115
+ try:
116
+ bench.choose_gate(roots)
117
+ except SystemExit as error:
118
+ assert 'ambiguous' in str(error)
119
+ else:
120
+ raise AssertionError('ambiguous real gate owners must refuse')
121
+ missing = subprocess.run(cli(work, '--root', '^TS12a-impossible-root$', '--wait-seconds', '.05'),
122
+ capture_output=True, text=True, timeout=2)
123
+ assert missing.returncode != 0 and 'discovery deadline' in missing.stderr, missing
124
+ legacy = work / 'summary.json'
125
+ legacy.write_text('{"schema":1,"gates":[]}')
126
+ incompatible = subprocess.run(cli(work, '--pid', str(owners[1].pid)),
127
+ capture_output=True, text=True, timeout=2)
128
+ assert incompatible.returncode != 0 and 'incompatible census summary' in incompatible.stderr
129
+ legacy.unlink()
130
+ # Exercise bounded discovery through the CLI on the real absolute Node root.
131
+ pattern = '^' + re.escape(node) + r'\s+' + re.escape(str(script)) + r'(?:\s|$)'
132
+ observer = subprocess.Popen(cli(work, '--root', pattern, '--wait-seconds', '.5'), cwd=ROOT,
133
+ stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
134
+ controls.wait_for(lambda: first_sample(work))
135
+ stop.touch()
136
+ for owner in owners[1:]:
137
+ assert owner.wait(timeout=2) == 0
138
+ row = finish(observer, work, 0)
139
+ assert row['root']['pid'] == owners[1].pid and row['allObservedChildrenDrained'], row
140
+ return {'relativeAndAbsoluteRoots': True, 'wrapperExcluded': True, 'ambiguousRefused': True,
141
+ 'missingBounded': True, 'legacySummaryRefused': True, 'actualCliDiscovery': True}
142
+ finally:
143
+ stop.touch()
144
+ if observer is not None and observer.poll() is None:
145
+ observer.terminate()
146
+ observer.wait(timeout=2)
147
+ for owner in owners:
148
+ if owner.poll() is None:
149
+ owner.terminate()
150
+ owner.wait(timeout=2)
151
+
152
+
153
+ def main():
154
+ parser = argparse.ArgumentParser(description=__doc__)
155
+ parser.add_argument('--scratch', type=Path, required=True)
156
+ args = parser.parse_args()
157
+ args.scratch.mkdir(parents=True, exist_ok=True)
158
+ began = time.monotonic()
159
+ with tempfile.TemporaryDirectory(dir=args.scratch) as temporary:
160
+ work = Path(temporary)
161
+ result = {'discovery': discovery(work / 'discovery'),
162
+ 'normal': descendants(work / 'normal', False),
163
+ 'orphan': descendants(work / 'orphan', True)}
164
+ print(json.dumps({**result, 'seconds': time.monotonic() - began}))
165
+
166
+
167
+ if __name__ == '__main__':
168
+ main()
@@ -9,7 +9,8 @@ macOS and Linux includes every reaped descendant), the largest single-process ma
9
9
  python3 scripts/vitest-bench.py run [--out tmp/vitest-bench/results.json] [--slots 2] [FILE ...]
10
10
  python3 scripts/vitest-bench.py table [--in tmp/vitest-bench/results.json] [--top 30]
11
11
  python3 scripts/vitest-bench.py pack [--in ...] [--workers 4,5,6,10]
12
- python3 scripts/vitest-bench.py observe [--count 3] (passive census of real push gates)
12
+ python3 scripts/vitest-bench.py observe --out tmp/YOUR-LANE/census.json [--pid PID | --count 3]
13
+ (passive identity-aware census; sampled CPU lower bounds, not exact full-gate totals)
13
14
 
14
15
  `run` resumes: files already in the results JSON at the same commit are skipped. The bench is the machine's
15
16
  lowest-priority caller. It starts a file only while no full-test lease is held or queued, nobody else waits for
@@ -682,84 +683,114 @@ def command_pack(args):
682
683
  'packedWallSeconds': {n: round(pack(net, n), 1) for n in args.workers}}, indent=1))
683
684
 
684
685
 
685
- def cpu_seconds(text):
686
- """ps time text ([D-]H:MM:SS.ss or M:SS.ss) to seconds."""
687
- days, _, text = text.rpartition('-')
688
- parts = [float(part) for part in text.split(':')]
689
- seconds = sum(value * 60 ** index for index, value in enumerate(reversed(parts)))
690
- return seconds + (int(days) * 86400 if days else 0)
686
+ # Match the executable and its entry script, not a wrapper that mentions the gate.
687
+ # --pid remains available for launchers/options whose argv ps cannot disambiguate.
688
+ GATE_ROOT = r'^(?:\S*/)?node(?:24)?(?:\s+--[^\s]+)*\s+(?:\S*/)?\.githooks/push-checks\.mjs(?:\s|$)'
691
689
 
692
690
 
693
- def command_observe(args):
694
- """Passively census one real gate: every process under a root whose command matches --root, sampled with ps.
695
- Records per-process peak cumulative user/total CPU (a process living under one interval is missed), peak summed
696
- RSS, peak process count, and CPU parallelism over time. Runs nothing itself."""
697
- log = lambda message: print(time.strftime('%H:%M:%S'), message, flush=True)
691
+ def gate_candidates(output, pattern):
692
+ roots = []
693
+ for line in output.splitlines():
694
+ parts = line.split(None, 2)
695
+ if len(parts) == 3 and parts[0].isdigit() and parts[1].isdigit():
696
+ pid, uid = map(int, parts[:2])
697
+ if pid > 1 and pid != os.getpid() and uid == os.getuid() and pattern.search(parts[2]):
698
+ roots.append(pid)
699
+ return sorted(set(roots))
700
+
701
+
702
+ def choose_gate(roots):
703
+ if len(roots) > 1:
704
+ raise SystemExit('ambiguous gate roots; give --pid for the intended owner')
705
+ return roots[0] if roots else None
706
+
707
+
708
+ def observe_root(args):
709
+ if args.pid is not None:
710
+ return args.pid
698
711
  pattern = re.compile(args.root)
699
- gates = 0
700
- while gates < args.count:
701
- roots = []
702
- while not roots:
703
- out = subprocess.run(['ps', '-axo', 'pid=,command='], capture_output=True, text=True).stdout
704
- roots = [int(line.split(None, 1)[0]) for line in out.splitlines() if pattern.search(line) and 'vitest-bench' not in line]
705
- if not roots:
706
- time.sleep(args.interval)
707
- root = min(roots)
708
- log(f'observing gate root pid {root}')
709
- began, seen, timeline, peak_rss, peak_procs = time.monotonic(), {}, [], 0, 0
710
- while True:
711
- out = subprocess.run(['ps', '-axo', 'pid=,ppid=,utime=,time=,rss=,comm='], capture_output=True, text=True).stdout
712
- table = {}
713
- for line in out.splitlines():
714
- parts = line.split(None, 5)
715
- if len(parts) == 6:
716
- table[int(parts[0])] = (int(parts[1]), cpu_seconds(parts[2]), cpu_seconds(parts[3]), int(parts[4]) * 1024, parts[5])
717
- if root not in table:
718
- break
719
- children = collections.defaultdict(list)
720
- for pid, row in table.items():
721
- children[row[0]].append(pid)
722
- tree, stack = [], [root]
723
- while stack:
724
- pid = stack.pop()
725
- tree.append(pid)
726
- stack.extend(children.get(pid, ()))
727
- for pid in tree:
728
- _, user, total, _, comm = table[pid]
729
- old = seen.get(pid, (0, 0, comm))
730
- seen[pid] = (max(old[0], user), max(old[1], total), comm)
731
- peak_rss = max(peak_rss, sum(table[pid][3] for pid in tree))
732
- peak_procs = max(peak_procs, len(tree))
733
- timeline.append([round(time.monotonic() - began, 2), round(sum(v[1] for v in seen.values()), 2),
734
- len(tree), round(sum(table[pid][3] for pid in tree) / 2**20)])
735
- time.sleep(args.interval)
736
- wall = time.monotonic() - began
737
- user = sum(v[0] for v in seen.values())
738
- total = sum(v[1] for v in seen.values())
739
- by_kind = collections.Counter()
740
- cpu_by_kind = collections.Counter()
741
- for _, cpu, comm in seen.values():
742
- by_kind[kind_of(comm)] += 1
743
- cpu_by_kind[kind_of(comm)] += cpu
744
- # Busiest 10 s window of CPU parallelism.
745
- peak_parallel = 0.0
746
- for index, (at, cpu, _, _) in enumerate(timeline):
747
- for later in timeline[index:]:
748
- if later[0] - at >= 10:
749
- peak_parallel = max(peak_parallel, (later[1] - cpu) / (later[0] - at))
750
- break
751
- row = {'root': root, 'observedAt': int(time.time()), 'wallSeconds': round(wall, 1), 'userSeconds': round(user, 1),
752
- 'systemSeconds': round(total - user, 1), 'cpuSeconds': round(total, 1),
753
- 'averageParallelism': round(total / wall, 2) if wall else None, 'peak10sParallelism': round(peak_parallel, 2),
754
- 'peakTreeRssMB': round(peak_rss / 2**20), 'peakProcesses': peak_procs, 'processes': len(seen),
755
- 'processesByKind': dict(by_kind), 'cpuSecondsByKind': {k: round(v, 1) for k, v in cpu_by_kind.most_common()},
756
- 'loadAtEnd': round(os.getloadavg()[0], 1), 'timeline': timeline[::max(1, len(timeline) // 400)]}
757
- data = load(args.out) or {'schema': 1, 'gates': []}
758
- data['gates'].append(row)
712
+ until = time.monotonic() + args.wait_seconds
713
+ while True:
714
+ try:
715
+ output = subprocess.run(['ps', '-axo', 'pid=,uid=,command='], check=True,
716
+ capture_output=True, text=True,
717
+ timeout=max(.05, min(5, until - time.monotonic()))).stdout
718
+ except subprocess.TimeoutExpired:
719
+ raise SystemExit('process inventory exceeded discovery deadline; give --pid or retry later')
720
+ root = choose_gate(gate_candidates(output, pattern))
721
+ if root is not None:
722
+ return root
723
+ if time.monotonic() >= until:
724
+ raise SystemExit('no matching gate before discovery deadline; give --pid or retry later')
725
+ time.sleep(min(args.interval, max(0, until - time.monotonic())))
726
+
727
+
728
+ def census_summary(report, status):
729
+ start, end, cpu, cpu_by_kind, samples = None, None, 0, collections.Counter(), 0
730
+ with report.open() as reader:
731
+ for line in reader:
732
+ row = json.loads(line)
733
+ if row['type'] == 'start':
734
+ start = row
735
+ elif row['type'] == 'sample':
736
+ samples += 1
737
+ for process in row['processes']:
738
+ delta = process['cpuDeltaNsLowerBound'] / 1e9
739
+ cpu += delta
740
+ cpu_by_kind[kind_of(process['name'])] += delta
741
+ elif row['type'] == 'end':
742
+ end = row
743
+ if start is None or end is None:
744
+ raise SystemExit(f'incomplete census; raw observation retained at {report}')
745
+ kinds = collections.Counter(kind_of(process['name']) for process in end['observedProcesses'])
746
+ return {'root': start['root'], 'startedAtMs': start['startedAtMs'],
747
+ 'gateKind': 'unverified', 'rawReport': os.path.relpath(report, ROOT),
748
+ 'observationWallSeconds': end['seconds'], 'sampledCpuSecondsLowerBound': cpu,
749
+ 'averageObservedCpuCoresLowerBound': cpu / end['seconds'] if end['seconds'] else None,
750
+ 'peakIntervalCpuCoresLowerBound': end['peak']['intervalCpuCoresLowerBound'],
751
+ 'peakSummedRssMB': end['peak']['summedRssBytes'] / 2**20,
752
+ 'peakProcesses': end['peak']['processes'], 'processes': len(end['observedProcesses']),
753
+ 'processesByKind': dict(kinds), 'sampledCpuSecondsByKindLowerBound': dict(cpu_by_kind),
754
+ 'remaining': end['remaining'], 'reason': end['reason'], 'incompleteReads': end['incompleteReads'],
755
+ 'allObservedChildrenDrained': status == 0 and end['rootMeasured'] and not end['remaining']
756
+ and end['reason'] == 'observed tree drained',
757
+ 'samples': samples, 'observerCpuSeconds': end['observerCpuNs'] / 1e9,
758
+ 'limits': start['limits'],
759
+ 'exactGateCpu': None,
760
+ 'qualification': 'Not an exact full-gate total: attach full/split provenance and reaped-tree '
761
+ 'user/system rusage separately. A sampled empty tree does not prove '
762
+ 'that every short-lived descendant was observed.'}
763
+
764
+
765
+ def command_observe(args):
766
+ """Observe existing owners through the identity-aware recorder; never launch or signal a gate."""
767
+ schema = 'vitest-gate-census/2'
768
+ if args.out.exists():
769
+ data = json.loads(args.out.read_text())
770
+ if data.get('schema') != schema or not isinstance(data.get('observations'), list):
771
+ raise SystemExit('incompatible census summary; use a new --out path for version 2 observations')
772
+ else:
773
+ data = {'schema': schema, 'observations': []}
774
+ args.out.parent.mkdir(parents=True, exist_ok=True)
775
+ for _ in range(args.count):
776
+ root = observe_root(args)
777
+ report = args.out.with_name(f'{args.out.stem}.{time.time_ns()}.{root}.jsonl')
778
+ command = [sys.executable, str(ROOT / 'scripts/cpu-census.py'), '--pid', str(root),
779
+ '--report', str(report), '--interval', str(args.interval), '--seconds', str(args.seconds),
780
+ '--drain-seconds', str(args.drain_seconds)]
781
+ print(f'observing root pid {root}; raw report {report}', flush=True)
782
+ result = subprocess.run(command, capture_output=True, text=True,
783
+ timeout=args.seconds + args.drain_seconds + 15)
784
+ if not report.exists():
785
+ raise SystemExit(result.stderr.strip() or 'census refused before observation')
786
+ row = census_summary(report, result.returncode)
787
+ data['observations'].append(row)
759
788
  save(args.out, data)
760
- log(f"gate {root}: wall {row['wallSeconds']}s cpu {row['cpuSeconds']}s (user {row['userSeconds']}, sys {row['systemSeconds']}) "
761
- f"avg parallelism {row['averageParallelism']} peak10s {row['peak10sParallelism']} rss {row['peakTreeRssMB']}MB")
762
- gates += 1
789
+ print(json.dumps({'root': root, 'sampledCpuSecondsLowerBound': row['sampledCpuSecondsLowerBound'],
790
+ 'allObservedChildrenDrained': row['allObservedChildrenDrained'],
791
+ 'reason': row['reason']}), flush=True)
792
+ if result.returncode != 0:
793
+ raise SystemExit(result.returncode)
763
794
 
764
795
 
765
796
  def main():
@@ -788,11 +819,25 @@ def main():
788
819
  packer.add_argument('--workers', type=lambda text: [int(n) for n in text.split(',')], default=[2, 4, 5, 6, 8, 10])
789
820
  packer.add_argument('--overhead', type=float, default=0.0, help='per-process startup seconds to subtract')
790
821
  observe = sub.add_parser('observe')
791
- observe.add_argument('--root', default=r'node \.githooks/push-checks\.mjs')
792
- observe.add_argument('--out', default=str(ROOT / 'tmp' / 'vitest-bench' / 'gates.json'))
822
+ roots = observe.add_mutually_exclusive_group()
823
+ roots.add_argument('--root', default=GATE_ROOT, help='anchored process-command regex; refuses ambiguous roots')
824
+ roots.add_argument('--pid', type=int, help='existing owner PID; observation never launches or signals it')
825
+ observe.add_argument('--out', type=Path, required=True, help='version-2 summary in your owned repository scratch')
826
+ observe.add_argument('--wait-seconds', type=float, default=120, help='discovery deadline, at most 120 seconds')
827
+ observe.add_argument('--seconds', type=float, default=1200, help='observation deadline, at most 3600 seconds')
828
+ observe.add_argument('--drain-seconds', type=float, default=10)
793
829
  observe.add_argument('--interval', type=float, default=0.5)
794
830
  observe.add_argument('--count', type=int, default=1)
795
831
  args = parser.parse_args()
832
+ if args.command == 'observe':
833
+ if not .05 <= args.interval <= 10 or not 0 <= args.wait_seconds <= 120 or not 1 <= args.seconds <= 3600 or not 0 <= args.drain_seconds <= 30 or args.count < 1:
834
+ parser.error('give bounded observation/discovery intervals and a positive count')
835
+ if args.pid is not None and (args.pid <= 1 or args.pid == os.getpid() or args.count != 1):
836
+ parser.error('--pid needs one existing owner and --count=1')
837
+ try:
838
+ re.compile(args.root)
839
+ except re.error as error:
840
+ parser.error(f'invalid --root expression: {error}')
796
841
  {'run': command_run, 'table': command_table, 'pack': command_pack, 'observe': command_observe}[args.command](args)
797
842
 
798
843
 
@@ -488,6 +488,8 @@ have_cook_source() {
488
488
  && [ "$(cook_commit)" = "$COOK_BUILD" ] \
489
489
  && [ "$(git -c safe.directory="$COOK_SRC" -C "$COOK_SRC" rev-parse "$COOK_REF^{commit}" 2>/dev/null)" = "$COOK_BUILD" ]
490
490
  }
491
+ # pnpm 10 versions the store (`<store-dir>/v10`) and keeps the global virtual store at `<that>/links`, which
492
+ # scripts/assert-store-layout.mjs checks; `$COOK_PNPM_STORE/links` failed it, so v0.1.23's cook move rolled back.
491
493
  make_cook_source() {
492
494
  install -d -m 755 -o root -g root "$(dirname "$COOK_SRC")"
493
495
  if [ ! -d "$COOK_SRC/.git" ]; then
@@ -498,7 +500,7 @@ make_cook_source() {
498
500
  if [ "$(git -c safe.directory="$COOK_SRC" -C "$COOK_SRC" rev-parse "$COOK_REF^{commit}" 2>/dev/null)" != "$COOK_BUILD" ]; then say stop "The signed release's $COOK_REF does not resolve to $COOK_BUILD"; exit 1; fi
499
501
  rm -f "$COOK_SRC/.wildshard-installed"
500
502
  git -c safe.directory="$COOK_SRC" -C "$COOK_SRC" checkout --quiet --force --detach "$COOK_BUILD"
501
- (cd "$COOK_SRC" && org_git env CI=1 HOME=/root COREPACK_ENABLE_DOWNLOAD_PROMPT=0 corepack "$PNPM" install --frozen-lockfile --ignore-scripts --config.confirmModulesPurge=false --config.ci=false --store-dir "$COOK_PNPM_STORE" --virtual-store-dir "$COOK_PNPM_STORE/links")
503
+ (cd "$COOK_SRC" && org_git env CI=1 HOME=/root COREPACK_ENABLE_DOWNLOAD_PROMPT=0 corepack "$PNPM" install --frozen-lockfile --ignore-scripts --config.confirmModulesPurge=false --config.ci=false --store-dir "$COOK_PNPM_STORE" --virtual-store-dir "$COOK_PNPM_STORE/v10/links")
502
504
  (cd "$COOK_SRC" && "$NODE" scripts/assert-store-layout.mjs)
503
505
  printf '%s %s\n' "$COOK_REF" "$COOK_BUILD" > "$COOK_SRC/.wildshard-installed"
504
506
  chmod -R go-w "$COOK_SRC"