@threenative/runtime-native 0.2.0 → 0.3.1

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.
@@ -3,32 +3,145 @@
3
3
  import { spawnSync } from 'node:child_process';
4
4
  import { createHash } from 'node:crypto';
5
5
  import { existsSync, mkdirSync, readFileSync, realpathSync, writeFileSync } from 'node:fs';
6
- import { basename, join, resolve } from 'node:path';
6
+ import { basename, dirname, join, resolve } from 'node:path';
7
7
  import { fileURLToPath, pathToFileURL } from 'node:url';
8
8
  import { PNG } from 'pngjs';
9
9
 
10
- const workspaceRoot = resolve(fileURLToPath(import.meta.url), '..', '..', '..', '..');
11
10
  const READY_MARKER = 'TN_NATIVE_SMOKE_READY:webgpu';
12
11
  const ASSET_MARKER = 'TN_NATIVE_STARTER_ASSETS_LOADED:texture,glb';
12
+ // Measured: a drawn starter frame has ~17,000 distinct colours, a lost capture had 5.
13
+ const UNRENDERED_FRAME_COLOR_FLOOR = 64;
14
+ // 64x64. Below this the frame is a fixture, not a capture.
15
+ const UNRENDERED_FRAME_MIN_PIXELS = 4096;
16
+ const ASSET_PIXEL_FLOOR = 100;
17
+ // The packaged proof is a cyan/magenta checkerboard pennant, and it is identified by CHANNEL
18
+ // MARGIN rather than by distance to the authored texture colour. The gate reads a lit, tonemapped
19
+ // render, and the authored cyan never reaches the screen: measured on the CI capture of run
20
+ // 34076016432, the pennant's cyan renders as rgb(86,180,189) — 103 away from the authored
21
+ // [18,220,255] — while the ocean behind it sits at 149 and the sky at 140. Any absolute-distance
22
+ // threshold wide enough to admit the asset also admits the sea, which is how a frame showing the
23
+ // pennant plainly was rejected with 798,464 of 921,600 pixels classified as proof.
24
+ //
25
+ // Magenta is the discriminator, because an ocean world contains none: 521 magenta pixels on the
26
+ // pennant, against 0 in the same frame with the pennant cropped away.
27
+ const PROOF_MAGENTA_MIN_MARGIN = 40;
28
+ // Corroborating cyan, counted only inside the magenta bounds so the background cannot supply it.
29
+ const PROOF_CYAN_MIN_MARGIN = 70;
30
+ const PROOF_CYAN_IN_BOUNDS_FLOOR = 50;
31
+ // A proof region may occupy up to a quarter of a rendered frame; a wash that reaches almost every
32
+ // part of the frame must not count as localized evidence.
33
+ const MAX_ASSET_BOUNDS_FRACTION = 0.25;
34
+ // Bounds alone cannot separate a small asset from magenta scattered thinly across the frame, since
35
+ // scattered pixels share almost the same bounding box as a solid one. Density does: the pennant
36
+ // fills 0.236 of its own bounds, scattered noise fills 0.04.
37
+ const MIN_ASSET_BOUNDS_DENSITY = 0.1;
13
38
 
14
- export function inspectStarterScreenshot(path) {
15
- if (!existsSync(path)) throw new Error(`TN_NATIVE_STARTER_SCREENSHOT_MISSING: ${path}`);
16
- const png = PNG.sync.read(readFileSync(path));
39
+ function isProofMagenta(data, offset) {
40
+ return (
41
+ data[offset + 3] > 0 &&
42
+ data[offset] > data[offset + 1] + PROOF_MAGENTA_MIN_MARGIN &&
43
+ data[offset + 2] > data[offset + 1] + PROOF_MAGENTA_MIN_MARGIN
44
+ );
45
+ }
46
+
47
+ function isProofCyan(data, offset) {
48
+ return (
49
+ data[offset + 3] > 0 &&
50
+ data[offset + 1] - data[offset] > PROOF_CYAN_MIN_MARGIN &&
51
+ data[offset + 2] - data[offset] > PROOF_CYAN_MIN_MARGIN
52
+ );
53
+ }
54
+
55
+ function countProofCyanInBounds(png, bounds) {
56
+ let cyan = 0;
57
+ for (let y = bounds.minY; y <= bounds.maxY; y += 1) {
58
+ for (let x = bounds.minX; x <= bounds.maxX; x += 1) {
59
+ if (isProofCyan(png.data, (y * png.width + x) * 4)) cyan += 1;
60
+ }
61
+ }
62
+ return cyan;
63
+ }
64
+
65
+ // Localization is a question about a capture, not about a texture, so only a capture-sized frame
66
+ // reaches this. The distribution test feeds the packaged 16x16 proof itself, where the asset
67
+ // legitimately fills the whole frame and asking where it sits has no meaning.
68
+ function assertProofIsLocalized(png, bounds, magentaAssetPixels) {
69
+ const boundsArea = (bounds.maxX - bounds.minX + 1) * (bounds.maxY - bounds.minY + 1);
70
+ const boundsFraction = boundsArea / (png.width * png.height);
71
+ if (boundsFraction > MAX_ASSET_BOUNDS_FRACTION) {
72
+ throw new Error(
73
+ `TN_NATIVE_STARTER_ASSET_NOT_LOCALIZED: magenta spans ${(boundsFraction * 100).toFixed(1)}% of the frame, which is a wash, not the proof asset.`,
74
+ );
75
+ }
76
+ const density = magentaAssetPixels / boundsArea;
77
+ if (density < MIN_ASSET_BOUNDS_DENSITY) {
78
+ throw new Error(
79
+ `TN_NATIVE_STARTER_ASSET_NOT_LOCALIZED: magenta fills only ${(density * 100).toFixed(1)}% of its own bounds, which is scatter, not the proof asset.`,
80
+ );
81
+ }
82
+ }
83
+
84
+ // One pass over the frame: the colour census the unrendered-frame floor reads, and the magenta
85
+ // pixel count with the bounds it occupies.
86
+ function scanProofMagenta(png) {
17
87
  const colors = new Set();
18
- let cyanAssetPixels = 0;
88
+ const bounds = { maxX: -1, maxY: -1, minX: png.width, minY: png.height };
89
+ let magentaAssetPixels = 0;
19
90
  for (let index = 0; index < png.data.length; index += 4) {
20
- const red = png.data[index];
21
- const green = png.data[index + 1];
22
- const blue = png.data[index + 2];
23
- const alpha = png.data[index + 3];
24
- colors.add(`${red},${green},${blue},${alpha}`);
25
- if (alpha > 0 && blue > 150 && green > 140 && blue > red * 1.4) cyanAssetPixels += 1;
91
+ colors.add(
92
+ `${png.data[index]},${png.data[index + 1]},${png.data[index + 2]},${png.data[index + 3]}`,
93
+ );
94
+ if (!isProofMagenta(png.data, index)) continue;
95
+ magentaAssetPixels += 1;
96
+ const pixel = index / 4;
97
+ const x = pixel % png.width;
98
+ const y = Math.floor(pixel / png.width);
99
+ if (x < bounds.minX) bounds.minX = x;
100
+ if (x > bounds.maxX) bounds.maxX = x;
101
+ if (y < bounds.minY) bounds.minY = y;
102
+ if (y > bounds.maxY) bounds.maxY = y;
26
103
  }
104
+ return { bounds, colors, magentaAssetPixels };
105
+ }
106
+
107
+ export function inspectStarterScreenshot(path) {
108
+ if (!existsSync(path)) throw new Error(`TN_NATIVE_STARTER_SCREENSHOT_MISSING: ${path}`);
109
+ const png = PNG.sync.read(readFileSync(path));
110
+ const { bounds, colors, magentaAssetPixels } = scanProofMagenta(png);
27
111
  if (colors.size < 2) throw new Error('TN_NATIVE_STARTER_SCREENSHOT_BLANK: one-color frame.');
28
- if (cyanAssetPixels < 100) {
29
- throw new Error(`TN_NATIVE_STARTER_ASSET_NOT_VISIBLE: found ${cyanAssetPixels} cyan proof pixels.`);
112
+ // A one-colour guard is too weak to catch the capture this gate actually loses. A rendered
113
+ // starter frame carries roughly 17k distinct colours; an intermittent CI failure captured five —
114
+ // flat background, two flat shapes, thirteen pixels of the GLB — while the run log still showed
115
+ // TN_NATIVE_STARTER_ASSETS_LOADED and "Rendered 300 frames". That frame was never drawn, and
116
+ // reporting it as a missing asset sends the reader hunting for a texture that loaded fine.
117
+ // Only meaningful at capture resolution. A 16x16 synthetic fixture — what the installed-verifier
118
+ // distribution test feeds this function — is legitimately two colours, and judging it by the
119
+ // diversity a 1280x720 render carries would reject a frame that is exactly what it claims to be.
120
+ const rendered = png.width * png.height >= UNRENDERED_FRAME_MIN_PIXELS;
121
+ if (rendered && colors.size < UNRENDERED_FRAME_COLOR_FLOOR) {
122
+ throw new Error(
123
+ `TN_NATIVE_STARTER_FRAME_NOT_RENDERED: only ${colors.size} distinct colours in ${png.width}x${png.height}. The run log may still show every marker: this is the capture, not the scene.`,
124
+ );
125
+ }
126
+ if (magentaAssetPixels < ASSET_PIXEL_FLOOR) {
127
+ throw new Error(
128
+ `TN_NATIVE_STARTER_ASSET_NOT_VISIBLE: found ${magentaAssetPixels} magenta proof pixels in a frame of ${colors.size} colours.`,
129
+ );
130
+ }
131
+ if (rendered) assertProofIsLocalized(png, bounds, magentaAssetPixels);
132
+ const cyanAssetPixels = countProofCyanInBounds(png, bounds);
133
+ if (cyanAssetPixels < PROOF_CYAN_IN_BOUNDS_FLOOR) {
134
+ throw new Error(
135
+ `TN_NATIVE_STARTER_ASSET_NOT_VISIBLE: magenta found but only ${cyanAssetPixels} cyan proof pixels inside its bounds; the checkerboard needs both colours.`,
136
+ );
30
137
  }
31
- return { colors: colors.size, cyanAssetPixels, height: png.height, width: png.width };
138
+ return {
139
+ colors: colors.size,
140
+ cyanAssetPixels,
141
+ height: png.height,
142
+ magentaAssetPixels,
143
+ width: png.width,
144
+ };
32
145
  }
33
146
 
34
147
  export function analyzeStarterLog(log, frames = 300) {
@@ -42,6 +155,13 @@ export function analyzeStarterLog(log, frames = 300) {
42
155
  for (const pattern of [/TN_NATIVE_START_FAILED/u, /validation error/iu, /TypeError:/u]) {
43
156
  if (pattern.test(log)) failures.push(`runtime log matched ${pattern}`);
44
157
  }
158
+ // The capture is only evidence if the world was on screen when it was taken. The host holds the
159
+ // screenshot until the startup gate opens and says so; a 0 here means it captured the loading
160
+ // state after waiting out its budget, which is the difference between a 17,000-colour frame and
161
+ // a five-colour one.
162
+ if (log.includes('TN_STARTUP_CAPTURE_READY:0')) {
163
+ failures.push('startup gate never opened before capture (TN_STARTUP_CAPTURE_READY:0)');
164
+ }
45
165
  return failures;
46
166
  }
47
167
 
@@ -61,9 +181,13 @@ export function verifyStarterDesktop({ frames = 300, project = process.cwd() } =
61
181
  mkdirSync(artifactDirectory, { recursive: true });
62
182
  const runtimeArgs = ['--screenshot', screenshot, '--frames', String(frames)];
63
183
  // See verify-desktop-core.mjs: `xvfb-run` hands back its own failing cleanup kill's status.
184
+ const displayHelper = join(dirname(fileURLToPath(import.meta.url)), 'xvfb.sh');
185
+ if (process.platform === 'linux' && !existsSync(displayHelper)) {
186
+ throw new Error(`TN_NATIVE_STARTER_DISPLAY_SUPPORT_MISSING: ${displayHelper}`);
187
+ }
64
188
  const command = process.platform === 'linux' ? 'sh' : artifact;
65
189
  const args = process.platform === 'linux'
66
- ? [join(workspaceRoot, 'scripts', 'xvfb.sh'), artifact, ...runtimeArgs]
190
+ ? [displayHelper, artifact, ...runtimeArgs]
67
191
  : runtimeArgs;
68
192
  const result = spawnSync(command, args, {
69
193
  cwd: projectRoot,
@@ -0,0 +1,68 @@
1
+ #!/bin/sh
2
+ # Run a command under a virtual X display and return the command's status.
3
+ #
4
+ # This copy is packaged with the installed desktop verifier. It must not resolve a helper from the
5
+ # engine checkout because the verifier is a consumer-facing command run from node_modules.
6
+ # `xvfb-run` is deliberately not used: its cleanup kill can replace a successful command status.
7
+ set -u
8
+
9
+ if [ "$#" -eq 0 ]; then
10
+ echo "scripts/xvfb.sh: usage: scripts/xvfb.sh <command> [args...]" >&2
11
+ exit 2
12
+ fi
13
+
14
+ case "$(uname -s 2>/dev/null || echo unknown)" in
15
+ Linux*) ;;
16
+ *) exec "$@" ;;
17
+ esac
18
+
19
+ if ! command -v Xvfb >/dev/null 2>&1; then
20
+ echo "scripts/xvfb.sh: Xvfb is required for headless Linux runs and is not installed." >&2
21
+ exit 2
22
+ fi
23
+
24
+ screen="${TN_XVFB_SCREEN:-1600x900x24}"
25
+ runtime="$(mktemp -d)"
26
+ display_file="$runtime/display"
27
+ : >"$display_file"
28
+
29
+ Xvfb -displayfd 3 -screen 0 "$screen" -nolisten tcp 3>"$display_file" &
30
+ xvfb_pid=$!
31
+
32
+ cleanup() {
33
+ if kill -0 "$xvfb_pid" 2>/dev/null; then
34
+ kill "$xvfb_pid" 2>/dev/null || true
35
+ wait "$xvfb_pid" 2>/dev/null || true
36
+ fi
37
+ rm -rf "$runtime"
38
+ }
39
+ trap cleanup EXIT INT TERM
40
+
41
+ # 10s was enough until a loaded two-core CI runner missed it: run 33789430714's installed-verifier
42
+ # case reported "Xvfb did not report a display within 10 seconds" while Xvfb was still alive and
43
+ # starting. The loop already exits the moment Xvfb dies, so a higher ceiling costs a healthy run
44
+ # nothing and only buys a slow one time. Tenths of a second.
45
+ display_wait_tenths=300
46
+ display=""
47
+ waited=0
48
+ while [ "$waited" -lt "$display_wait_tenths" ]; do
49
+ display="$(tr -d '[:space:]' <"$display_file")"
50
+ [ -n "$display" ] && break
51
+ if ! kill -0 "$xvfb_pid" 2>/dev/null; then
52
+ echo "scripts/xvfb.sh: Xvfb exited before it reported a display" >&2
53
+ exit 2
54
+ fi
55
+ sleep 0.1
56
+ waited=$((waited + 1))
57
+ done
58
+
59
+ if [ -z "$display" ]; then
60
+ echo "scripts/xvfb.sh: Xvfb did not report a display within $((display_wait_tenths / 10)) seconds" >&2
61
+ exit 2
62
+ fi
63
+
64
+ DISPLAY=":$display"
65
+ export DISPLAY
66
+ "$@"
67
+ status=$?
68
+ exit "$status"