@threenative/runtime-native 0.3.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.
@@ -25,8 +25,9 @@ export const NATIVE_ORIENTATIONS = ['landscape', 'portrait', 'sensor'];
25
25
  export const DEFAULT_IOS_CONFIG = {
26
26
  app: { id: 'com.threenative.game', name: 'ThreeNative', version: '0.1.13', build: 1 },
27
27
  display: { orientation: 'landscape', fullscreen: true, keepScreenOn: false, maxFps: 60 },
28
- window: { title: 'ThreeNative', width: 1280, height: 720, resizable: true },
28
+ window: { title: 'ThreeNative', width: 1280, height: 720, maximized: false, resizable: true },
29
29
  };
30
+ const BINARY_PLIST_MAGIC = Buffer.from('bplist00', 'ascii');
30
31
  const IOS_ORIENTATIONS = {
31
32
  landscape: ['UIInterfaceOrientationLandscapeLeft', 'UIInterfaceOrientationLandscapeRight'],
32
33
  portrait: ['UIInterfaceOrientationPortrait'],
@@ -65,6 +66,31 @@ function readConfig(configPath) {
65
66
  }
66
67
  }
67
68
 
69
+ function convertBinaryInfoPlist(path) {
70
+ const result = spawnSync('plutil', ['-convert', 'xml1', '-o', '-', path], {
71
+ encoding: 'utf8',
72
+ });
73
+ if (result.error) {
74
+ throw new Error(
75
+ `TN_IOS_INFO_PLIST_CONVERT_FAILED: plutil could not convert ${path}: ${result.error.message}`,
76
+ );
77
+ }
78
+ if (result.status !== 0 || !result.stdout?.includes('<plist')) {
79
+ throw new Error(
80
+ `TN_IOS_INFO_PLIST_CONVERT_FAILED: plutil exited with code ${result.status ?? 'unknown'} for ${path}: ${result.stderr || 'no XML output'}`,
81
+ );
82
+ }
83
+ return result.stdout;
84
+ }
85
+
86
+ function readIosInfoPlist(path, convertInfoPlist = convertBinaryInfoPlist) {
87
+ const source = readFileSync(path);
88
+ if (!source.subarray(0, BINARY_PLIST_MAGIC.length).equals(BINARY_PLIST_MAGIC)) {
89
+ return { format: 'xml', source: source.toString('utf8') };
90
+ }
91
+ return { format: 'binary', source: convertInfoPlist(path, source) };
92
+ }
93
+
68
94
  function xmlEscape(value) {
69
95
  return String(value).replaceAll('&', '&amp;').replaceAll('<', '&lt;').replaceAll('>', '&gt;').replaceAll('"', '&quot;').replaceAll("'", '&apos;');
70
96
  }
@@ -180,15 +206,17 @@ function findApp(directory) {
180
206
  return undefined;
181
207
  }
182
208
 
183
- function compileIosAssets(catalog, output, includeAppIcon = false) {
209
+ export function compileIosAssets(catalog, output, includeAppIcon = false, execute = spawnSync) {
184
210
  const iconArguments = includeAppIcon ? ['--app-icon', 'AppIcon'] : [];
185
- const result = spawnSync(
211
+ const result = execute(
186
212
  'xcrun',
187
213
  [
188
214
  'actool',
189
215
  '--compile',
190
216
  output,
191
217
  ...iconArguments,
218
+ '--output-partial-info-plist',
219
+ '/dev/null',
192
220
  '--platform',
193
221
  'iphonesimulator',
194
222
  '--minimum-deployment-target',
@@ -267,6 +295,13 @@ function writeLaunchColor(catalog, value) {
267
295
  );
268
296
  }
269
297
 
298
+ function writeAssetCatalogContents(catalog) {
299
+ writeFileSync(
300
+ join(catalog, 'Contents.json'),
301
+ `${JSON.stringify({ info: { author: 'xcode', version: 1 } }, null, 2)}\n`,
302
+ );
303
+ }
304
+
270
305
  function stageIosIcon(output, icon, compileIcon = compileIosIcon, options = {}) {
271
306
  if (!existsSync(icon) || !statSync(icon).isFile()) {
272
307
  throw new Error(`TN_CONFIG_ICON_MISSING: app.icon does not exist: ${icon}`);
@@ -279,6 +314,7 @@ function stageIosIcon(output, icon, compileIcon = compileIosIcon, options = {})
279
314
  const compiled = join(temporary, 'compiled');
280
315
  mkdirSync(appIcon, { recursive: true });
281
316
  mkdirSync(compiled, { recursive: true });
317
+ writeAssetCatalogContents(catalog);
282
318
  copyFileSync(icon, join(appIcon, 'AppIcon-1024.png'));
283
319
  const appearances = options.variants ?? {};
284
320
  const images = [
@@ -337,6 +373,8 @@ function stageIosLaunchAssets(output, backgroundColor, compileAssets = compileIo
337
373
  const catalog = join(temporary, 'Assets.xcassets');
338
374
  const compiled = join(temporary, 'compiled');
339
375
  mkdirSync(compiled, { recursive: true });
376
+ mkdirSync(catalog, { recursive: true });
377
+ writeAssetCatalogContents(catalog);
340
378
  writeLaunchColor(catalog, backgroundColor);
341
379
  compileAssets(catalog, compiled);
342
380
  const assetsCar = join(compiled, 'Assets.car');
@@ -396,6 +434,7 @@ export function stageIosSimulatorApp({
396
434
  config = undefined,
397
435
  compileIcon = compileIosIcon,
398
436
  compileAssets = compileIosAssets,
437
+ convertInfoPlist = convertBinaryInfoPlist,
399
438
  }) {
400
439
  const declared = configValue(config, orientation);
401
440
  const declaredOrientation = orientationValue(declared.display.orientation);
@@ -419,7 +458,8 @@ export function stageIosSimulatorApp({
419
458
  mkdirSync(game, { recursive: true });
420
459
  stageIosUi(ui, declared.ui?.renderer === 'web' ? 'web' : 'native', join(output, 'ui'));
421
460
  const plist = join(output, 'Info.plist');
422
- writeFileSync(plist, renderIosInfoPlist(readFileSync(plist, 'utf8'), declared));
461
+ const infoPlist = readIosInfoPlist(plist, convertInfoPlist);
462
+ writeFileSync(plist, renderIosInfoPlist(infoPlist.source, declared));
423
463
  const iosVariants = declared.app.icons?.ios ?? {};
424
464
  const icon = declared.app.icon ?? iosVariants.dark ?? iosVariants.tinted;
425
465
  if (icon !== undefined) {
@@ -463,6 +503,7 @@ export function stageIosSimulatorApp({
463
503
  assets: assetFiles.map((path) => ({ path, sha256: checksum(join(game, path)) })),
464
504
  bundleSha256: checksum(bundle),
465
505
  host: 'ios-simulator-arm64',
506
+ infoPlistFormat: infoPlist.format,
466
507
  orientation: declaredOrientation,
467
508
  appId: declared.app.id,
468
509
  appName: declared.app.name,
@@ -473,7 +473,7 @@ export function createEvidenceFixture({ platform = "android", candidateSha = "8b
473
473
  evidenceClass: "physical-device",
474
474
  },
475
475
  source: {
476
- remote: "https://github.com/jonit-dev/threenative.git",
476
+ remote: "https://github.com/ThreeNativeHQ/threenative.git",
477
477
  branch: "main",
478
478
  headSha: candidateSha,
479
479
  worktree: "clean",
@@ -6,6 +6,11 @@ import { join, relative, resolve } from 'node:path';
6
6
 
7
7
  export const PRODUCTION_EVIDENCE_VERSION = 'productionEvidenceV1';
8
8
  export const REQUIRED_LIFECYCLE_MARKERS = ['run-start', 'first-workload-frame', 'clean-end'];
9
+ export const REGRESSION_COLLECTION_PROFILE = Object.freeze({
10
+ coldStarts: 5,
11
+ durationSeconds: 30,
12
+ minFrameSamples: 1_000,
13
+ });
9
14
 
10
15
  export class ProductionEvidenceError extends Error {
11
16
  constructor(code, message, status = 'BLOCKED') {
@@ -48,6 +53,8 @@ export function evaluateFrameBudget(metrics, budget = {}) {
48
53
  const mean = finiteMetric(metrics.meanFps) ? metrics.meanFps : meanFps(metrics.frameIntervalsMs);
49
54
  const p95 = finiteMetric(metrics.p95FrameMs) ? metrics.p95FrameMs : nearestRank(metrics.frameIntervalsMs, 0.95);
50
55
  const p99 = finiteMetric(metrics.p99FrameMs) ? metrics.p99FrameMs : nearestRank(metrics.frameIntervalsMs, 0.99);
56
+ const medianFrameMs = nearestRank(metrics.frameIntervalsMs, 0.5);
57
+ if (budget.minFps !== undefined && (!finiteMetric(medianFrameMs) || Math.round(100_000 / medianFrameMs) / 100 < budget.minFps)) failures.push('TN_PROD_PERFORMANCE_BUDGET');
51
58
  const floors = metrics.oneSecondFps ?? oneSecondFrameFloors(metrics.intervals ?? []);
52
59
  const drawCalls = maximumMetric(metrics, 'drawCalls');
53
60
  const triangles = maximumMetric(metrics, 'triangles');
@@ -63,9 +70,74 @@ export function evaluateFrameBudget(metrics, budget = {}) {
63
70
  return { drawCalls, failures: [...new Set(failures)], floors, mean, p95, p99, triangles };
64
71
  }
65
72
 
73
+ /**
74
+ * Regression runs are deliberately stricter than the long-standing production report. A number
75
+ * is useful only after the workload is ready, the warmup buffer was reset, and both the monotonic
76
+ * wall clock and presented-frame clock have supplied the requested window. These checks are kept
77
+ * here so browser, desktop, Android, and iOS collectors share one fail-closed contract.
78
+ */
79
+ export function regressionCollectionCodes(input) {
80
+ if (input.execution?.profile !== 'regression') return [];
81
+ const codes = [];
82
+ const metrics = input.metrics ?? {};
83
+ const execution = input.execution ?? {};
84
+ const frameSamples = metrics.frameIntervalsMs;
85
+ const frameCount = Array.isArray(frameSamples) ? frameSamples.length : 0;
86
+ const windows = metrics.runWindows;
87
+ const completeWindows = Array.isArray(windows)
88
+ && windows.length === 1
89
+ && windows.every((window) => isRecord(window)
90
+ && Number.isInteger(window.sampleCount)
91
+ && window.sampleCount >= REGRESSION_COLLECTION_PROFILE.minFrameSamples
92
+ && finiteMetric(window.durationSeconds)
93
+ && window.durationSeconds >= REGRESSION_COLLECTION_PROFILE.durationSeconds);
94
+ const windowSampleCount = Array.isArray(windows)
95
+ ? windows.reduce((total, window) => total + (isRecord(window) && Number.isInteger(window.sampleCount) ? window.sampleCount : 0), 0)
96
+ : 0;
97
+ const duration = metrics.durationSeconds;
98
+ if (frameCount < REGRESSION_COLLECTION_PROFILE.minFrameSamples
99
+ || !completeWindows
100
+ || windowSampleCount < REGRESSION_COLLECTION_PROFILE.minFrameSamples
101
+ || !finiteMetric(duration)
102
+ || duration < REGRESSION_COLLECTION_PROFILE.durationSeconds) {
103
+ codes.push('TN_PROD_REGRESSION_WINDOW');
104
+ }
105
+ if (execution.readiness?.ready !== true || execution.readiness?.sampleReset !== true) {
106
+ codes.push('TN_PROD_READINESS_MISSING');
107
+ }
108
+ if (execution.warmupReset !== true) codes.push('TN_PROD_STARTUP_CONTAMINATION');
109
+ if (metrics.clockSource !== 'monotonic-performance'
110
+ || !strictlyIncreasing(metrics.clockSamplesMs)
111
+ || metrics.fixedTick === true) {
112
+ codes.push('TN_PROD_CLOCK_INVALID');
113
+ }
114
+ if (metrics.presentationClockSource !== 'raf-presentation'
115
+ || !strictlyIncreasing(metrics.presentationSamplesMs)) {
116
+ codes.push('TN_PROD_PRESENTATION_CLOCK_INVALID');
117
+ }
118
+ if (!Number.isInteger(execution.coldStarts)
119
+ || execution.coldStarts < REGRESSION_COLLECTION_PROFILE.coldStarts
120
+ || !Array.isArray(metrics.startupSamplesMs)
121
+ || metrics.startupSamplesMs.length < REGRESSION_COLLECTION_PROFILE.coldStarts) {
122
+ codes.push('TN_PROD_STARTUP_SAMPLES_INCOMPLETE');
123
+ }
124
+ if (metrics.motion?.moved !== true || metrics.motion?.movingObjects <= 0) {
125
+ codes.push('TN_PROD_MOTION_MISSING');
126
+ }
127
+ if (metrics.pixels?.nonBlank !== true || metrics.pixels?.changed !== true) {
128
+ codes.push('TN_PROD_PIXEL_EVIDENCE_MISSING');
129
+ }
130
+ if (String(input.target ?? '').includes('physical')
131
+ && (metrics.thermal?.complete !== true || metrics.thermal?.thermallyConfounded === true)) {
132
+ codes.push('TN_PROD_THERMAL_STATE_INVALID');
133
+ }
134
+ return [...new Set(codes)];
135
+ }
136
+
66
137
  export function evaluateProductionEvidence(input, options = {}) {
67
138
  validateProductionEvidence(input);
68
139
  const codes = new Set(input.codes ?? []);
140
+ const advisoryCodes = new Set(input.advisoryCodes ?? []);
69
141
  const markers = new Set(markerNames(input.markers));
70
142
  const diagnosticCodes = new Set([
71
143
  'TN_PROD_ANDROID_ANR',
@@ -87,14 +159,22 @@ export function evaluateProductionEvidence(input, options = {}) {
87
159
  }
88
160
  if (input.source.dirty === true && input.source.diffSha === undefined) codes.add('TN_PROD_SOURCE_DIFF_MISSING');
89
161
  const frameBudget = evaluateFrameBudget(input.metrics ?? {}, input.budget ?? {});
90
- for (const code of frameBudget.failures) codes.add(code);
162
+ const advisoryTiming = input.execution?.performanceEvaluation === 'advisory';
163
+ for (const code of frameBudget.failures) {
164
+ if (advisoryTiming) advisoryCodes.add(code);
165
+ else codes.add(code);
166
+ }
167
+ for (const code of regressionCollectionCodes(input)) codes.add(code);
91
168
  if (input.metrics?.thermal?.complete === false || input.metrics?.battery?.complete === false) codes.add('TN_PROD_RESOURCE_SAMPLES_INCOMPLETE');
92
169
  if (input.metrics?.thermal?.severeSeconds >= 60) codes.add('TN_PROD_THERMAL_BUDGET');
93
170
  if (input.metrics?.durationSeconds !== undefined && input.budget?.minDurationSeconds !== undefined && input.metrics.durationSeconds < input.budget.minDurationSeconds) codes.add('TN_PROD_MARKER_MISSING');
94
171
  const startupP95 = input.metrics?.startupP95Ms
95
172
  ?? nearestRank(input.metrics?.startupSamplesMs, 0.95)
96
173
  ?? input.metrics?.startupMs;
97
- if (input.budget?.maxStartupMs !== undefined && (startupP95 === undefined || startupP95 > input.budget.maxStartupMs)) codes.add('TN_PROD_STARTUP_BUDGET');
174
+ if (input.budget?.maxStartupMs !== undefined && (startupP95 === undefined || startupP95 > input.budget.maxStartupMs)) {
175
+ if (advisoryTiming) advisoryCodes.add('TN_PROD_STARTUP_BUDGET');
176
+ else codes.add('TN_PROD_STARTUP_BUDGET');
177
+ }
98
178
  const memory = input.metrics?.memory;
99
179
  if (memory !== undefined) {
100
180
  const firstMedian = memory.first15MedianBytes;
@@ -125,6 +205,11 @@ export function evaluateProductionEvidence(input, options = {}) {
125
205
  if (input.audioClaim === 'claimed' && typeof input.audioEvidenceSha256 !== 'string') codes.add('TN_PROD_AUDIO_EVIDENCE');
126
206
  const blockedCodes = [...codes].filter((code) => [
127
207
  'TN_PROD_MARKER_MISSING',
208
+ 'TN_PROD_REGRESSION_WINDOW',
209
+ 'TN_PROD_READINESS_MISSING',
210
+ 'TN_PROD_STARTUP_CONTAMINATION',
211
+ 'TN_PROD_CLOCK_INVALID',
212
+ 'TN_PROD_PRESENTATION_CLOCK_INVALID',
128
213
  'TN_PROD_SOURCE_SHA_MISMATCH',
129
214
  'TN_PROD_SOURCE_DIFF_MISSING',
130
215
  'TN_PROD_RESOURCE_SAMPLES_INCOMPLETE',
@@ -137,11 +222,15 @@ export function evaluateProductionEvidence(input, options = {}) {
137
222
  'TN_PROD_PLAYTEST_FAILED',
138
223
  'TN_PROD_RENDER_SAMPLES_INCOMPLETE',
139
224
  'TN_PROD_STARTUP_SAMPLES_INCOMPLETE',
225
+ 'TN_PROD_MOTION_MISSING',
226
+ 'TN_PROD_PIXEL_EVIDENCE_MISSING',
227
+ 'TN_PROD_THERMAL_STATE_INVALID',
140
228
  ].includes(code));
141
229
  const failureCodes = [...codes].filter((code) => code.startsWith('TN_PROD_') && !blockedCodes.includes(code));
142
230
  const status = blockedCodes.length > 0 ? 'BLOCKED' : failureCodes.length > 0 ? 'FAIL' : 'PASS';
143
231
  return {
144
232
  ...input,
233
+ ...(advisoryCodes.size === 0 ? {} : { advisoryCodes: [...advisoryCodes] }),
145
234
  codes: [...codes],
146
235
  metrics: {
147
236
  ...input.metrics,
@@ -203,6 +292,9 @@ export function validateProductionEvidence(value) {
203
292
  if (value.diagnosticArtifacts !== undefined && !Array.isArray(value.diagnosticArtifacts)) {
204
293
  throw new ProductionEvidenceError('TN_PROD_EVIDENCE_INVALID', 'Evidence diagnosticArtifacts must be an array when present.');
205
294
  }
295
+ if (value.advisoryCodes !== undefined && (!Array.isArray(value.advisoryCodes) || value.advisoryCodes.some((code) => typeof code !== 'string' || code.length === 0))) {
296
+ throw new ProductionEvidenceError('TN_PROD_EVIDENCE_INVALID', 'Evidence advisoryCodes must be a string array when present.');
297
+ }
206
298
  assertPrivacySafe(value);
207
299
  }
208
300
 
@@ -231,6 +323,13 @@ function finiteMetric(value) {
231
323
  return typeof value === 'number' && Number.isFinite(value);
232
324
  }
233
325
 
326
+ function strictlyIncreasing(values) {
327
+ return Array.isArray(values)
328
+ && values.length > 1
329
+ && values.every((value, index) => finiteMetric(value)
330
+ && (index === 0 || value > values[index - 1]));
331
+ }
332
+
234
333
  function maximumMetric(metrics, key) {
235
334
  if (Array.isArray(metrics.intervals) && metrics.intervals.length > 0) {
236
335
  const values = metrics.intervals.map((sample) => sample?.[key]);