# Scene performance validation

A Scene that breaks the open-world performance rules is refused before it is published.

```sh
helix scene validate ./granit --perf                       # a package folder holding scene.helix-scene.json
helix scene validate ./granit --perf --tier desktop,mobile # also ask whether it would hold on mobile
helix scene validate ./granit --perf --out report.json     # write the helix.scene-perf-report/1 JSON
helix scene validate ./scene.helix-scene.json --perf --json
helix scene validate ./granit                              # without --perf: only the shared Scene v2 validator
```

Exit codes: `0` pass (warnings allowed), `1` refused (or, without `--perf`, an invalid document), `2` it could not be
judged (unreadable document, a declared file is absent, a bad `--tier`). A refusal prints every failing rule with its
code, what was measured against the limit, the subjects, and how to fix it.

`helix world scene-source publish` runs the same gate on the exact bytes it is about to upload, in dry-run and with
`--publish`, and refuses with the same report. There is no override flag. `--supports-mobile` adds the mobile tier.

## Where the rules live

The 14 rules (P01 to P14), their limits, the report schema `helix.scene-perf-report/1` and the backend re-check live
once, in `@hypersoniclabs/helix-manifest` (`src/scene-perf/**`, `docs/helix-scene-perf.md`). They are pure functions of
the Scene document and optional measurements. This CLI adds the three rules only bytes can decide, and measures the facts
that sharpen four document rules:

| Code | Rule | What the CLI reads |
|---|---|---|
| P03 | textures | Every image a GLB samples (embedded, `data:` URI, or an external Scene resource) and every texture resource: it must be KTX2 with a full mip chain, normal and data maps must be UASTC (ETC1S is for colour), longest edge at most 2,048 px (4,096 for HLOD atlases and layered library arrays). Terrain splat and normal maps are `image/png` by contract and exempt from format; their edge is still bounded. |
| P04 | geometry | A GLB over 256 KB must use `EXT_meshopt_compression` or `KHR_mesh_quantization`; every triangle primitive is indexed and can draw a triangle; every model resource is a readable GLB. |
| P12 | duplicates | Warning only: identical embedded image bytes, or identical GLB binary chunks, shipped under different resource digests. |
| facts | P02 P05 P06 P11 | Per model: triangles and draw calls from the GLB accessor counts (no geometry is decoded, so meshopt and Draco files are measured too) and the external images a model depends on. |

## Source layout

- `src/scenePerf/ktx2.ts` and `glb.ts`: header readers (the first 512 bytes of a KTX2: header, level index, descriptor, the JSON chunk of a GLB).
- `src/scenePerf/byteRules.ts`: P03, P04, P12 and the facts. Sequential, one GLB in memory at a time.
- `src/scenePerf/source.ts`: where bytes come from, a folder (traversal-safe) or the in-memory build input.
- `src/scenePerf/run.ts`: `runScenePerf` assembles the report from the manifest's document rules and the CLI byte results.
- `src/scenePerf/gate.ts`: `enforceScenePerfGate`, the publish gate. Throws `ScenePerfRefusal` (message is the full report text).
- `src/scenePerf/commands.ts`: `helix scene validate`.

The report is deterministic, so its canonical bytes depend only on the Scene and the validator. A later publication lane
seals it into the Scene Package as the `perf-report` object; the backend then calls `verifyScenePerfReport` and refuses
a Scene that arrives without a passing report.

## Granit

`helix scene validate <granit package> --perf` is the reference run (2026-10-09, desktop). It is refused on three rules and
passes the rest, and each refusal is a real property of the document:

- P05: 46 road collision cells span 1,054 to 1,140 m, over the 1,024 m cell side.
- P08: the Douglas fir prototypes keep 1,539 triangles at LOD 1 and LOD 2 (the chain does not decrease and ends above 300).
- P11: the first playable closure is 80.6 MB, over 60 MB.

P03 passes: all 745 sampled images are KTX2 with full mip chains and UASTC normal maps (the terrain PNG maps are exempt).
P12 warns about eight placements models whose geometry chunks are byte-identical under different digests. With
`--tier desktop,mobile` the mobile tier also refuses P01 (256 MiB budget over the 192 MiB cap) and P06 (297 draws over 150).
Fix the Scene (the Scene compiler lanes), never the limits.

After `helix city roads restyle-cs2` ([city-roads-restyle-cs2.md](city-roads-restyle-cs2.md)) the road refusals are gone: P05 passes (cells
clipped at 1,024 m, crowded cells split, densest 230,688 triangles), P11 passes at 48.0 MB (the near cells stream by distance and
the far layer is 1.9 MiB), P06 drops to 94 draws, and P01's memory proof to 38.6 MiB. P08 and the mobile P01 budget declaration
remain, and neither is a road property.
