@wildshard/engine 0.1.22 → 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 (179) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/index.html +17 -4
  3. package/lint/engine-sides.json +9 -0
  4. package/lint/sim-players.json +2 -13
  5. package/package.json +36 -3
  6. package/scripts/apfs-clone.mjs +5 -2
  7. package/scripts/app-build/catalogue.ts +39 -8
  8. package/scripts/app-build/serviceWorker.ts +4 -1
  9. package/scripts/app-build/splitOutput.mjs +33 -11
  10. package/scripts/app-build/titlePreloads.mjs +8 -5
  11. package/scripts/bake-road-surfaces.entry.ts +18 -0
  12. package/scripts/bake-road-surfaces.mjs +44 -0
  13. package/scripts/changelog.mjs +2 -1
  14. package/scripts/ci-pack.mjs +3 -3
  15. package/scripts/engine-ci-shard.mjs +11 -0
  16. package/scripts/fight-page-census.mjs +131 -0
  17. package/scripts/goldens/board.py +44 -11
  18. package/scripts/goldens/capture.mjs +18 -6
  19. package/scripts/goldens/native-residency.d.mts +2 -0
  20. package/scripts/goldens/native-residency.mjs +31 -4
  21. package/scripts/grid-road-lag.mjs +209 -0
  22. package/scripts/keep-copy.mjs +172 -0
  23. package/scripts/land.d.mts +4 -1
  24. package/scripts/land.mjs +121 -9
  25. package/scripts/lane-live.mjs +136 -0
  26. package/scripts/leftovers-report.mjs +47 -0
  27. package/scripts/loading-card-proof.mjs +42 -9
  28. package/scripts/loading-flow/benchmax.mjs +59 -7
  29. package/scripts/loading-flow/stalls.d.mts +2 -0
  30. package/scripts/loading-flow/stalls.mjs +13 -0
  31. package/scripts/map-hash.mjs +1 -1
  32. package/scripts/mmo-bots.mjs +5 -3
  33. package/scripts/mmo-grid-proof.mjs +74 -0
  34. package/scripts/mmo-seat-proof.mjs +43 -0
  35. package/scripts/nightly-heads.ts +13 -13
  36. package/scripts/nightly-model.ts +26 -12
  37. package/scripts/nightly-tag.mjs +2 -4
  38. package/scripts/nightly.proof.ts +18 -5
  39. package/scripts/nightly.ts +13 -8
  40. package/scripts/no-astc-boot.mjs +77 -0
  41. package/scripts/page-release/content.mjs +19 -10
  42. package/scripts/page-release/flow.d.mts +3 -2
  43. package/scripts/page-release/flow.mjs +31 -21
  44. package/scripts/page-release/packs.d.mts +5 -0
  45. package/scripts/page-release/packs.mjs +13 -3
  46. package/scripts/page-release/preview.d.mts +2 -0
  47. package/scripts/page-release/preview.mjs +74 -0
  48. package/scripts/page-release.mjs +13 -5
  49. package/scripts/physics-route.json +6 -6
  50. package/scripts/pins-reuse.mjs +1 -0
  51. package/scripts/pins-staging.ts +54 -0
  52. package/scripts/pins.ts +5 -0
  53. package/scripts/ring3.proof.ts +71 -0
  54. package/scripts/ring3.ts +100 -0
  55. package/scripts/road-drive-proof.mjs +269 -0
  56. package/scripts/road-memory-probe.mjs +129 -0
  57. package/scripts/scratch-dir.mjs +33 -14
  58. package/scripts/scratch-sweep.mjs +15 -23
  59. package/scripts/server-release/github.mjs +59 -26
  60. package/scripts/server-release/items.d.mts +7 -0
  61. package/scripts/server-release/items.mjs +28 -0
  62. package/scripts/server-release/publish.mjs +24 -2
  63. package/scripts/server-release/sign.mjs +8 -4
  64. package/scripts/side-parity.mjs +1 -1
  65. package/scripts/test-products.json +1 -0
  66. package/scripts/test_prebuilt_output.py +3 -1
  67. package/scripts/tsconfig.json +2 -0
  68. package/scripts/turn-in-shots.mjs +83 -0
  69. package/scripts/vitest-bench-observe-proof.py +168 -0
  70. package/scripts/vitest-bench.py +122 -77
  71. package/scripts/vitest-ci-files.ts +5 -1
  72. package/scripts/vps/rebuild.sh +3 -1
  73. package/scripts/vps/wildshard-config +106 -1
  74. package/scripts/warm-native-chrome.mjs +9 -3
  75. package/scripts/worker-client-fights.mjs +9 -3
  76. package/scripts/worktree-reap.mjs +36 -140
  77. package/scripts/worktree.mjs +92 -12
  78. package/scripts/zone-gate-proof.mjs +93 -0
  79. package/src/client/engine/audio/audioLog.ts +7 -2
  80. package/src/client/engine/boot/diagnostics.ts +69 -1
  81. package/src/client/engine/boot/sw.ts +3 -1
  82. package/src/client/engine/core/ktx2.ts +33 -10
  83. package/src/client/engine/core/ktx2Zstd.ts +2 -1
  84. package/src/client/engine/models/place.ts +7 -5
  85. package/src/client/engine/pwa/sw.js +83 -10
  86. package/src/client/engine/render/dataLook.ts +14 -7
  87. package/src/client/engine/render/lookLight.ts +64 -20
  88. package/src/client/engine/render/precompile.ts +6 -0
  89. package/src/client/engine/render/tiers.ts +9 -0
  90. package/src/client/engine/ui/Loading.ts +6 -8
  91. package/src/client/engine/ui/lookLightOption.ts +1 -1
  92. package/src/client/engine/ui/playerReload.ts +5 -5
  93. package/src/client/game/audio/cueDirector.ts +2 -1
  94. package/src/client/game/boot/bytes.generated.ts +4 -1
  95. package/src/client/game/boot/versions.generated.ts +8 -5
  96. package/src/client/game/boss/arenaRoom.ts +4 -3
  97. package/src/client/game/boss/roamingProjection.ts +1 -1
  98. package/src/client/game/devScene/hook.ts +1 -1
  99. package/src/client/game/devScene/liveLook.ts +1 -1
  100. package/src/client/game/grid/cellClip.ts +70 -6
  101. package/src/client/game/grid/dataCellKit.ts +9 -1
  102. package/src/client/game/grid/gateCard.css +9 -0
  103. package/src/client/game/grid/gateCard.ts +67 -0
  104. package/src/client/game/grid/live.ts +17 -3
  105. package/src/client/game/grid/liveSession.ts +15 -5
  106. package/src/client/game/grid/neighbourLife.ts +10 -1
  107. package/src/client/game/grid/neighbourPanels.ts +29 -2
  108. package/src/client/game/grid/pageEntry.ts +21 -0
  109. package/src/client/game/grid/products.ts +21 -7
  110. package/src/client/game/grid/regionalWorld.ts +5 -0
  111. package/src/client/game/grid/roadCompressedSurfaces.ts +69 -0
  112. package/src/client/game/grid/roadLook.ts +5 -73
  113. package/src/client/game/grid/roadLookPlatform.ts +11 -6
  114. package/src/client/game/grid/roadSurfaceMaps.ts +80 -0
  115. package/src/client/game/grid/roadTilePool.ts +4 -0
  116. package/src/client/game/grid/session.ts +58 -16
  117. package/src/client/game/grid/softWallLook.ts +6 -1
  118. package/src/client/game/plugin/ui.ts +3 -0
  119. package/src/client/game/population/signals.ts +1 -1
  120. package/src/client/game/quest/chapterDirector.ts +1 -0
  121. package/src/client/game/session/play.ts +3 -0
  122. package/src/client/game/shardfile/clientBossTelegraph.ts +4 -3
  123. package/src/client/game/shardfile/clientSource.ts +5 -2
  124. package/src/client/game/shardfile/clientWorker.ts +25 -8
  125. package/src/client/game/shardfile/look.ts +1 -1
  126. package/src/client/game/shardfile/runtimeProduct.ts +9 -5
  127. package/src/client/game/shardfile/slabLibrary.ts +37 -0
  128. package/src/client/game/shardfile/slabRanges.ts +145 -0
  129. package/src/client/game/shardfile/wirePage.ts +5 -2
  130. package/src/client/game/shardfile/worldSceneTextures.ts +15 -9
  131. package/src/client/host/online/shell.ts +2 -2
  132. package/src/client/host/standalone/shell.ts +4 -1
  133. package/src/cook/game/cookWorker.ts +8 -5
  134. package/src/cook/game/tierTextures.ts +5 -3
  135. package/src/server/engine/net/simWorld.ts +3 -4
  136. package/src/server/game/grid/cellHome.ts +58 -0
  137. package/src/server/game/grid/simulation.ts +32 -8
  138. package/src/server/game/plugin/mover.ts +18 -14
  139. package/src/server/game/plugin/simulation.ts +2 -0
  140. package/src/server/game/shardfile/dataRespawn.ts +47 -0
  141. package/src/server/game/shardfile/declaredKitItems.ts +31 -7
  142. package/src/server/host/bake/headlessWorker.ts +3 -3
  143. package/src/server/host/dedicated/cellWorker.ts +4 -2
  144. package/src/server/host/dedicated/main.ts +7 -4
  145. package/src/server/host/dedicated/packReader.ts +15 -2
  146. package/src/server/host/dedicated/playerStore.ts +1 -1
  147. package/src/server/host/headlessRuntime.ts +11 -6
  148. package/src/server/host/standalone/cellWorld.ts +20 -6
  149. package/src/server/host/standalone/gridServer.ts +20 -6
  150. package/src/server/host/standalone/roadWorld.ts +36 -0
  151. package/src/server/host/standalone/serverHost.ts +2 -2
  152. package/src/shared/engine/entities/AnimalSim.ts +10 -0
  153. package/src/shared/engine/physics/gridBorders.ts +15 -1
  154. package/src/shared/engine/physics/transferWalls.ts +9 -4
  155. package/src/shared/engine/sim/snapshot.ts +5 -5
  156. package/src/shared/game/grid/allocator.ts +8 -1
  157. package/src/shared/game/grid/gateCount.ts +27 -0
  158. package/src/shared/game/grid/zoneWalls.ts +64 -0
  159. package/src/shared/game/ledger.ts +13 -4
  160. package/src/shared/game/loot/platformCatalogue.ts +14 -0
  161. package/src/shared/game/quest/ledger.ts +3 -3
  162. package/src/shared/game/shardfile/items.ts +9 -1
  163. package/src/shared/game/strings.ts +2 -0
  164. package/src/shared/host/headlessJourney.ts +3 -3
  165. package/src/shared/host/headlessTape.ts +7 -6
  166. package/src/shared/host/releaseRows.ts +4 -1
  167. package/src/shared/host/standalone/harnessPlace.ts +4 -0
  168. package/src/test/engine/simState.ts +134 -0
  169. package/src/test/game/builtProduct.ts +6 -0
  170. package/src/test/game/headless/admittedProductCache.ts +61 -0
  171. package/src/test/game/headless/checkedReplayPipeline.ts +21 -0
  172. package/src/test/game/headless/nativeBounds.ts +27 -0
  173. package/src/test/game/headless/processUsage.ts +22 -0
  174. package/src/test/game/headless/productSession.ts +39 -0
  175. package/src/test/game/headless/ropeBodies.ts +60 -0
  176. package/src/test/game/headless/worker.ts +34 -0
  177. package/src/test/game/headlessNative.ts +114 -0
  178. package/src/test/game/headlessWorker.ts +43 -0
  179. package/src/test/tsconfig.game.json +3 -0
@@ -1,4 +1,4 @@
1
- // Privileged workflow entry: only trusted main-push CI and main-history version tags.
1
+ // Privileged workflow entry: release-branch CI and reachable version tags only.
2
2
  import { createHash } from 'node:crypto';
3
3
  import { execFileSync } from 'node:child_process';
4
4
  import { appendFileSync, readFileSync } from 'node:fs';
@@ -8,7 +8,7 @@ import { setTimeout as sleep } from 'node:timers/promises';
8
8
  import * as v from 'valibot';
9
9
  import { COMMIT } from './sign.mjs';
10
10
 
11
- const REPOSITORY = 'project-wildshard/engine', VERSION = /^v\d+\.\d+\.\d+$/u;
11
+ const REPOSITORY = 'project-wildshard/engine', VERSION = /^v\d+\.\d+\.\d+$/u, RELEASE_BRANCH = /^release\/v\d+\.\d+\.\d+$/u;
12
12
  const Run = v.object({ id: v.number(), head_sha: v.string(), head_branch: v.nullable(v.string()), event: v.string(), status: v.string(), conclusion: v.nullable(v.string()) });
13
13
  const Release = v.object({ id: v.number(), upload_url: v.string(), assets: v.array(v.object({ name: v.string(), browser_download_url: v.string() })) });
14
14
  class GitHubError extends Error {
@@ -27,7 +27,7 @@ async function api(path, options = {}) {
27
27
  /** @param {string[]} args @returns {string} */
28
28
  const git = args => execFileSync('git', args, { encoding: 'utf8' }).trim();
29
29
 
30
- /** No fork or PR event is allowed to reach a signing job. @returns {{sha: string, channel: string, tag: string, align?: true} | null} */
30
+ /** No fork or PR event is allowed to reach a signing job. @returns {{sha: string, channel: string, tag: string, branches: string[], align?: true, legacy?: true} | null} */
31
31
  function candidate() {
32
32
  if (process.env.GITHUB_REPOSITORY !== REPOSITORY) return null;
33
33
  const eventName = process.env.GITHUB_EVENT_NAME;
@@ -36,53 +36,83 @@ function candidate() {
36
36
  const sha = process.env.ALIGN_BUILD ?? '';
37
37
  if (process.env.GITHUB_REF !== 'refs/heads/main') throw new Error('Staging alignment runs from main only');
38
38
  if (!COMMIT.test(sha)) throw new Error('Staging alignment needs a full 40-character build SHA');
39
- return { sha, channel: 'staging', tag: `server-staging-${sha}`, align: true };
39
+ const branches = git(['for-each-ref', '--format=%(refname:short)', '--contains', sha, 'refs/remotes/origin/release/']).split('\n')
40
+ .map(ref => ref.replace(/^origin\//u, '')).filter(ref => RELEASE_BRANCH.test(ref) && git(['rev-parse', `refs/remotes/origin/${ref}^{commit}`]) === sha);
41
+ if (branches.length === 0) {
42
+ const prepared = publishedMainRelease(sha);
43
+ if (prepared === null) throw new Error('Staging alignment needs a published release-branch build');
44
+ return { sha, channel: 'staging', tag: `server-staging-${sha}`, branches: [`release/${prepared}`, 'main'], align: true, legacy: true };
45
+ }
46
+ return { sha, channel: 'staging', tag: `server-staging-${sha}`, branches, align: true };
40
47
  }
41
48
  if (eventName === 'workflow_run') {
42
49
  const event = v.parse(v.object({ workflow_run: v.object({ ...Run.entries, head_repository: v.object({ full_name: v.string() }) }) }), JSON.parse(readFileSync(process.env.GITHUB_EVENT_PATH ?? '', 'utf8'))).workflow_run;
43
- if (event.event !== 'push' || event.head_branch !== 'main' || event.head_repository.full_name !== REPOSITORY || event.conclusion !== 'success') return null;
50
+ if (event.event !== 'push' || !RELEASE_BRANCH.test(event.head_branch ?? '') || event.head_repository.full_name !== REPOSITORY || event.conclusion !== 'success') return null;
44
51
  if (!COMMIT.test(event.head_sha)) throw new Error('Invalid green source SHA');
45
- return { sha: event.head_sha, channel: 'staging', tag: `server-staging-${event.head_sha}` };
52
+ return { sha: event.head_sha, channel: 'staging', tag: `server-staging-${event.head_sha}`, branches: [event.head_branch ?? ''] };
46
53
  }
47
54
  if (eventName === 'push') {
48
55
  const tag = process.env.GITHUB_REF_NAME ?? '';
49
56
  if (process.env.GITHUB_REF_TYPE !== 'tag' || !VERSION.test(tag)) return null;
50
57
  const sha = git(['rev-parse', '--verify', `refs/tags/${tag}^{commit}`]);
51
58
  if (!COMMIT.test(sha)) throw new Error('Invalid tagged source SHA');
52
- return { sha, channel: 'production', tag };
59
+ return { sha, channel: 'production', tag, branches: [`release/${tag}`, 'main'] };
53
60
  }
54
61
  return null;
55
62
  }
56
63
 
57
- /** Exact-SHA success of both normal push workflows; a PR result cannot stand in.
58
- * @param {string} sha @returns {Promise<'green' | 'pending' | 'red'>} */
59
- async function checks(sha) {
60
- let pending = false;
61
- for (const workflow of ['ci.yml']) {
62
- const value = await api(`actions/workflows/${workflow}/runs?head_sha=${sha}&event=push&branch=main&per_page=100`);
63
- const rows = v.parse(v.object({ workflow_runs: v.array(Run) }), value).workflow_runs
64
- .filter(run => run.head_sha === sha && run.head_branch === 'main' && run.event === 'push').sort((a, b) => b.id - a.id);
65
- const run = rows[0];
66
- if (run === undefined || run.status !== 'completed') pending = true;
67
- else if (run.conclusion !== 'success') return 'red';
68
- }
69
- return pending ? 'pending' : 'green';
64
+ /** Whether this published ref contains the source. Missing release refs are expected after merge-back.
65
+ * @param {string} sha @param {string} branch */
66
+ function reachable(sha, branch) {
67
+ try { git(['merge-base', '--is-ancestor', sha, `refs/remotes/origin/${branch}`]); return true; }
68
+ catch { return false; }
69
+ }
70
+
71
+ /** Let a release already prepared on main finish through explicit alignment.
72
+ * Ordinary main CI events never enter this path. @param {string} sha @returns {string | null} */
73
+ function publishedMainRelease(sha) {
74
+ if (!reachable(sha, 'main')) return null;
75
+ try {
76
+ const tag = /^chore\(release\): (v\d+\.\d+\.\d+)$/u.exec(git(['log', '-1', '--format=%s', sha]))?.[1];
77
+ return tag !== undefined && JSON.parse(git(['show', `${sha}:package.json`])).version === tag.slice(1) ? tag : null;
78
+ } catch { return null; }
79
+ }
80
+
81
+ /** Exact-SHA push CI on an admitted branch; PRs and plan branches cannot authorize a release.
82
+ * @param {string} sha @param {string[]} branches @returns {Promise<'green' | 'pending' | 'red'>} */
83
+ async function checks(sha, branches) {
84
+ const value = await api(`actions/workflows/ci.yml/runs?head_sha=${sha}&event=push&per_page=100`);
85
+ const rows = v.parse(v.object({ workflow_runs: v.array(Run) }), value).workflow_runs
86
+ .filter(run => run.head_sha === sha && branches.includes(run.head_branch ?? '') && run.event === 'push').sort((a, b) => b.id - a.id);
87
+ const latest = branches.flatMap(branch => { const row = rows.find(run => run.head_branch === branch); return row === undefined ? [] : [row]; });
88
+ if (latest.some(run => run.status === 'completed' && run.conclusion === 'success')) return 'green';
89
+ if (latest.length === 0 || latest.some(run => run.status !== 'completed')) return 'pending';
90
+ return 'red';
70
91
  }
71
92
 
72
93
  /** @returns {Promise<void>} */
73
94
  async function select() {
74
95
  const source = candidate(); if (source === null) return;
75
- git(['merge-base', '--is-ancestor', source.sha, 'origin/main']);
96
+ if (source.channel === 'staging' && source.legacy !== true && !source.branches.some(branch => {
97
+ try { return git(['rev-parse', '--verify', `refs/remotes/origin/${branch}^{commit}`]) === source.sha; } catch { return false; }
98
+ })) {
99
+ if (source.align === true) throw new Error('Staging alignment needs the current release-branch head');
100
+ return; // A delayed CI event must not publish an older release-branch head.
101
+ }
102
+ const published = source.branches.some(branch => reachable(source.sha, branch));
103
+ if (!published) throw new Error('Release source is unreachable from its published release branch or main');
104
+ // A completed release keeps its exact-SHA branch proof after merge-back deletes the branch.
105
+ const proofBranches = source.channel === 'production' || source.legacy === true ? source.branches : source.branches.filter(branch => reachable(source.sha, branch));
76
106
  // A late workflow_run from before this producer existed is not a release candidate.
77
107
  if (execFileSync('git', ['ls-tree', '--name-only', source.sha, 'scripts/server-release/publish.mjs'], { encoding: 'utf8' }).trim() === '') return;
78
108
  const deadline = Date.now() + 40 * 60_000;
79
109
  for (;;) {
80
- const state = await checks(source.sha);
110
+ const state = await checks(source.sha, proofBranches);
81
111
  if (state === 'green') break;
82
- if (source.align === true) throw new Error(`Build ${source.sha} is not green on main CI and Ring 1`);
83
- if (state === 'red') { if (source.channel === 'staging') return; throw new Error('Tagged commit has a failed main CI workflow'); }
112
+ if (source.align === true) throw new Error(`Build ${source.sha} is not green on release-branch CI and Ring 1`);
113
+ if (state === 'red') { if (source.channel === 'staging') return; throw new Error('Tagged commit has a failed release CI workflow'); }
84
114
  if (source.channel === 'staging') return; // The other workflow's completion will trigger again.
85
- if (Date.now() >= deadline) throw new Error('Timed out waiting for tagged main CI');
115
+ if (Date.now() >= deadline) throw new Error('Timed out waiting for tagged release CI');
86
116
  await sleep(20_000);
87
117
  }
88
118
  const output = process.env.GITHUB_OUTPUT; if (output === undefined) throw new Error('Missing GITHUB_OUTPUT');
@@ -102,7 +132,10 @@ async function verifyReleaseTag(tag, sha) {
102
132
  async function asset(tag, sha, archive, channel) {
103
133
  if (!COMMIT.test(sha) || !(VERSION.test(tag) && channel === 'production' || tag === `server-staging-${sha}` && channel === 'staging')) throw new Error('Invalid server release tag');
104
134
  if (git(['rev-parse', 'HEAD']) !== sha) throw new Error('Bundle checkout differs from release source');
105
- git(['merge-base', '--is-ancestor', sha, 'origin/main']);
135
+ const releaseBranches = channel === 'production' ? [`release/${tag}`] : git(['for-each-ref', '--format=%(refname:short)', '--contains', sha, 'refs/remotes/origin/release/']).split('\n').map(ref => ref.replace(/^origin\//u, '')).filter(ref => RELEASE_BRANCH.test(ref));
136
+ const legacy = channel === 'staging' && publishedMainRelease(sha) !== null;
137
+ if ((channel === 'production' || legacy ? [...releaseBranches, 'main'] : releaseBranches).every(branch => !reachable(sha, branch))) throw new Error('Server asset source is unreachable from release history');
138
+ if (channel === 'staging' && !legacy && !releaseBranches.some(branch => git(['rev-parse', `refs/remotes/origin/${branch}^{commit}`]) === sha)) throw new Error('Staging asset must name the current release-branch head');
106
139
  /** @type {v.InferOutput<typeof Release>} */
107
140
  let release;
108
141
  try { release = v.parse(Release, await api(`releases/tags/${encodeURIComponent(tag)}`)); }
@@ -0,0 +1,7 @@
1
+ import type { S3Client } from '@aws-sdk/client-s3';
2
+ import type { ReleaseFile } from '../../src/shared/host/releaseRows';
3
+
4
+ type ItemReference = NonNullable<ReleaseFile['items']>;
5
+ export function itemDigest(body: Uint8Array): string;
6
+ export function itemBytesMatch(body: Uint8Array, reference: ItemReference): boolean;
7
+ export function copyItemCatalogue(client: S3Client, bucket: string, read?: typeof fetch): Promise<ItemReference>;
@@ -0,0 +1,28 @@
1
+ // Q37: only publication contacts api-server. The running game reads this immutable R2 copy.
2
+ import {createHash} from 'node:crypto';
3
+ import {GetObjectCommand,PutObjectCommand,S3ServiceException} from '@aws-sdk/client-s3';
4
+ import {MAX_ITEM_CATALOGUE_BYTES,platformItemCatalogueBytes} from '../../src/shared/game/loot/platformCatalogue.ts';
5
+
6
+ /** @typedef {NonNullable<import('../../src/shared/host/releaseRows.ts').ReleaseFile['items']>} ItemReference */
7
+ /** @param {Uint8Array} body */
8
+ export const itemDigest=body=>createHash('sha256').update(body).digest('hex');
9
+ /** @param {Uint8Array} body @param {ItemReference} reference */
10
+ export const itemBytesMatch=(body,reference)=>body.byteLength===reference.bytes&&itemDigest(body)===reference.sha256;
11
+
12
+ /** Copy admitted, canonical API bytes before signing any channel. A hash-named object is never overwritten.
13
+ * @param {import('@aws-sdk/client-s3').S3Client} client @param {string} bucket @param {typeof fetch} [read] @returns {Promise<ItemReference>} */
14
+ export async function copyItemCatalogue(client,bucket,read=fetch){
15
+ const response=await read('https://api.wildshard.io/api/items.json',{cache:'no-store',signal:AbortSignal.timeout(30_000)});
16
+ if(!response.ok)throw new Error(`Item catalogue answered ${response.status}`);
17
+ const text=await response.text();
18
+ if(new TextEncoder().encode(text).byteLength>MAX_ITEM_CATALOGUE_BYTES)throw new Error('Item catalogue is too large');
19
+ const body=platformItemCatalogueBytes(JSON.parse(text)),sha256=itemDigest(body),reference={path:`assets/catalogue/items-${sha256}.json`,sha256,bytes:body.byteLength};
20
+ try{await client.send(new PutObjectCommand({Bucket:bucket,Key:reference.path,Body:body,ContentType:'application/json',CacheControl:'public,max-age=31536000,immutable',IfNoneMatch:'*'}));}
21
+ catch(error){
22
+ if(!(error instanceof S3ServiceException&&error.$metadata.httpStatusCode===412))throw error;
23
+ const existing=await client.send(new GetObjectCommand({Bucket:bucket,Key:reference.path}));
24
+ const kept=await existing.Body?.transformToByteArray();
25
+ if(kept===undefined||!itemBytesMatch(kept,reference))throw new Error('Existing item catalogue hash mismatch',{cause:error});
26
+ }
27
+ return reference;
28
+ }
@@ -8,6 +8,7 @@ import { GetObjectCommand, HeadObjectCommand, PutObjectCommand, S3Client, S3Serv
8
8
  import * as v from 'valibot';
9
9
  import { readServerPackBook, serverPackBookPath } from '../../src/shared/game/shardfile/serverPack.ts';
10
10
  import { bundleDescriptor, COMMIT, DEFAULT_SERVER_SHARDS, publication, readPublication, releasePublicKey, releaseShards, signRelease } from './sign.mjs';
11
+ import { copyItemCatalogue, itemBytesMatch } from './items.mjs';
11
12
 
12
13
  /** @typedef {import('../../src/shared/host/releaseRows.ts').ReleaseFile} ReleaseFile */
13
14
  /** @typedef {{client: S3Client, bucket: string, candidate: ReleaseFile, privateKey: string, publicKey: string, isAncestor: (older: string, newer: string) => boolean,
@@ -22,6 +23,12 @@ const missing = error => error instanceof S3ServiceException && (error.name ===
22
23
  * no book strands every check behind it. Null when the book and every part the release's shards and grid need are in the bucket.
23
24
  * @param {S3Client} client @param {string} bucket @param {ReleaseFile} release @returns {Promise<string | null>} */
24
25
  export async function serverPacksReady(client, bucket, release) {
26
+ if (release.items !== undefined) {
27
+ let body;
28
+ try { body = await (await client.send(new GetObjectCommand({ Bucket: bucket, Key: release.items.path }))).Body?.transformToByteArray(); }
29
+ catch (error) { if (missing(error)) return `no item catalogue at ${release.items.path}`; throw error; }
30
+ if (body === undefined || !itemBytesMatch(body, release.items)) return `item catalogue hash mismatch at ${release.items.path}`;
31
+ }
25
32
  const key = serverPackBookPath(release.engineBuild);
26
33
  let text;
27
34
  try { text = await (await client.send(new GetObjectCommand({ Bucket: bucket, Key: key }))).Body?.transformToString(); }
@@ -99,6 +106,20 @@ function isAncestor(older, newer) {
99
106
  return result.status === 0;
100
107
  }
101
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
+
102
123
  /** CLI runs only after CI has uploaded the exact immutable asset. @returns {Promise<void>} */
103
124
  async function main() {
104
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 },
@@ -107,7 +128,7 @@ async function main() {
107
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.
108
129
  // `--align` (staging only) is a person naming a build, never older than the live one.
109
130
  const align = values.align, head = execFileSync('git', ['rev-parse', 'HEAD'], { encoding: 'utf8' }).trim();
110
- 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');
111
132
  if (align && values.channel !== 'staging') throw new Error('--align is staging only');
112
133
  /** @param {string} path @returns {string} */
113
134
  const sourceFile = path => execFileSync('git', ['show', `${values.build ?? ''}:${path}`], { encoding: 'utf8' });
@@ -126,7 +147,8 @@ async function main() {
126
147
  const client = new S3Client({ region: 'auto', endpoint: `https://${account}.r2.cloudflarestorage.com`, credentials });
127
148
  try {
128
149
  const packsReady = (/** @type {ReleaseFile} */ release) => serverPacksReady(client, bucket, release);
129
- const result = await publishChannel({ client, bucket, candidate, privateKey, publicKey, isAncestor, align, packsReady });
150
+ const items = await copyItemCatalogue(client, bucket);
151
+ const result = await publishChannel({ client, bucket, candidate: { ...candidate, items }, privateKey, publicKey, isAncestor, align, packsReady });
130
152
  writeFileSync(values.out, `${result.signed}\n`);
131
153
  console.log(JSON.stringify({ status: result.status, channel: result.release.channel, serial: result.release.serial, engineBuild: result.release.engineBuild, object: `releases/${result.release.channel}.json`,
132
154
  ...result.reason === undefined ? {} : { held: candidate.engineBuild, reason: result.reason } }));
@@ -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');
@@ -169,7 +169,7 @@ export function sideParityDiff(paged, server, at, allow = {}, { slack = 1, same
169
169
  if (best !== null) { used.add(best); return; }
170
170
  const shape = all.find(other => close(row, other));
171
171
  const inside = (outer, inner) => [0, 1, 2].every(i => inner[i] >= outer[i] - slack && inner[i + 3] <= outer[i + 3] + slack);
172
- if (shape === undefined && side === 'page' && !row.sensor && !row.dynamic && all.some(other => other.material === 'edge' && !other.sensor && inside(other.box, row.box))) return;
172
+ if (shape === undefined && side === 'page' && !row.sensor && !row.dynamic && all.some(other => other.material === 'edge' && !other.sensor && !Object.hasOwn(allow, allowKey(other)) && inside(other.box, row.box))) return;
173
173
  if (shape === undefined) { note(`${side}|${matchKey(row)}|${row.dynamic}`, row.box, (count, box) => side === 'page'
174
174
  ? `${describe(row)}: ${count} page collider(s) with no server match, e.g. [${box.join(', ')}]`
175
175
  : `${describe(row)}: ${count} server collider(s) near the page's player with no page match, e.g. [${box.join(', ')}]`); return; }
@@ -1,4 +1,5 @@
1
1
  {
2
+ "test/admitted-product-cache.test.ts": [],
2
3
  "test/net-link-play.test.ts": ["driftwood-isle"],
3
4
  "test/engine-rule-successors.test.ts": ["pine-hollow", "nalati-grasslands", "driftwood-isle"],
4
5
  "test/engine-rule-successors-titan.test.ts": ["nalati-grasslands"],
@@ -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'})
@@ -36,6 +36,7 @@
36
36
  }
37
37
  ],
38
38
  "include": [
39
+ "../.agents/hooks/active-hours.mjs",
39
40
  "**/*.mjs",
40
41
  "**/*.ts",
41
42
  "**/*.mts",
@@ -551,6 +552,7 @@
551
552
  "webkit-mem-reading.mjs",
552
553
  "webkit-render-smoke.mjs",
553
554
  "worker-client-fights.mjs",
555
+ "fight-page-census.mjs",
554
556
  "worker-client-moments.mjs",
555
557
  "worker-client-play.mjs",
556
558
  "worker-client-proof.mjs",
@@ -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()