bvh_geometry_check: catch NaN leaf bounds, infinite-bound override tolerance blow-up, invalid --geom, and strict JSON (BVH-GEOMETRY-CHECK-NAN-INF-GEOM) #1429

Merged
IlyassRS3 merged 1 commit from upstream-pr/1429 into main 2026-11-28 17:56:33 +00:00
IlyassRS3 commented 2026-11-25 00:06:19 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T00:06:19Z.


Item ID: BVH-GEOMETRY-CHECK-NAN-INF-GEOM
AI-assisted: yes

Summary

Fixes four soundness and input-validation bugs in tools/bvh_geometry_check.py:

  1. Non-finite (NaN/±inf) leaf and scene bounds silently passed check_bounds():
    • When a leaf's vertices produced NaN bounds, max([box[a] - b[a], ...]) > OUTSIDE_TOLERANCE * extent evaluated to False, and Python's min(union[a], NaN) / max(union[a + 3], NaN) dropped NaN once another finite leaf populated union — falsely reporting leaves_outside=0, bounds_gap=0.0, consistent=True.
    • check_bounds() now rejects non-finite mapped sceneBounds corners and counts any leaf with non-finite bounds as outside (setting bounds_gap = math.inf, consistent = False).
  2. check_overrides() tolerance blew up to +inf on infinite child bounds:
    • Line 98 checked if math.isfinite(value) without checking math.isfinite(b[comp]). When a child bound was ±inf (e.g. a BOX16 bound overflowing half-float range) and the stored override value was finite, tol became +inf, turning lo - tol <= value <= hi + tol into -inf <= value <= +inf and accepting overrides wildly outside [lo, hi].
    • Added _ulp_tol(value, ref) so each bound comparison (b[comp], lo, hi) applies ULP tolerance only when both operands are finite.
  3. Strict --geom VB[,IB] validation and early --json directory check:
    • Rejects malformed --geom specs (len(parts) > 2 or empty VB path such as --geom vb,ib,extra, --geom ,ib, --geom "") with exit status 2 instead of silently dropping extra tokens or opening "".
    • Rejects --json <existing_dir> before reading/rehydrating.
  4. Strict RFC 8259 --json output (allow_nan=False):
    • Non-finite float fields (such as bounds_gap = inf on inconsistent non-finite inputs) are serialized as JSON null with allow_nan=False rather than emitting non-standard NaN/Infinity tokens.

Verification (macOS Apple M5, arm64, Python 3.14; no game files)

  • python3 -m compileall -q make_game.py tools tests — OK
  • python3 -I scripts/check_no_game_data.py tools/bvh_geometry_check.py tests/test_bvh_geometry_check.py — OK
  • python3 -I tests/test_bvh_geometry_check.py -v — 12/12 OK (5 new regression tests)
  • All 11 tests/test_bvh_*.py suites — 214/214 OK
  • python3 -I tests/test_text_write_line_endings.py — 3/3 OK
  • python3 -I tests/test_ci_registration.py — 16/16 OK
  • Mutation proof (running the new tests/test_bvh_geometry_check.py against unpatched upstream/main 1154976d4): Ran 12 tests — FAILED (failures=5) — all 5 new regression tests fail without this fix.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5482519556 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1429). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T00:06:19Z. --- Item ID: `BVH-GEOMETRY-CHECK-NAN-INF-GEOM` AI-assisted: yes ### Summary Fixes four soundness and input-validation bugs in `tools/bvh_geometry_check.py`: 1. **Non-finite (`NaN`/`±inf`) leaf and scene bounds silently passed `check_bounds()`**: - When a leaf's vertices produced `NaN` bounds, `max([box[a] - b[a], ...]) > OUTSIDE_TOLERANCE * extent` evaluated to `False`, and Python's `min(union[a], NaN)` / `max(union[a + 3], NaN)` dropped `NaN` once another finite leaf populated `union` — falsely reporting `leaves_outside=0, bounds_gap=0.0, consistent=True`. - `check_bounds()` now rejects non-finite mapped `sceneBounds` corners and counts any leaf with non-finite bounds as outside (setting `bounds_gap = math.inf`, `consistent = False`). 2. **`check_overrides()` tolerance blew up to `+inf` on infinite child bounds**: - Line 98 checked `if math.isfinite(value)` without checking `math.isfinite(b[comp])`. When a child bound was `±inf` (e.g. a BOX16 bound overflowing half-float range) and the stored override `value` was finite, `tol` became `+inf`, turning `lo - tol <= value <= hi + tol` into `-inf <= value <= +inf` and accepting overrides wildly outside `[lo, hi]`. - Added `_ulp_tol(value, ref)` so each bound comparison (`b[comp]`, `lo`, `hi`) applies ULP tolerance only when both operands are finite. 3. **Strict `--geom VB[,IB]` validation and early `--json` directory check**: - Rejects malformed `--geom` specs (`len(parts) > 2` or empty VB path such as `--geom vb,ib,extra`, `--geom ,ib`, `--geom ""`) with exit status 2 instead of silently dropping extra tokens or opening `""`. - Rejects `--json <existing_dir>` before reading/rehydrating. 4. **Strict RFC 8259 `--json` output (`allow_nan=False`)**: - Non-finite float fields (such as `bounds_gap = inf` on inconsistent non-finite inputs) are serialized as JSON `null` with `allow_nan=False` rather than emitting non-standard `NaN`/`Infinity` tokens. ### Verification (macOS Apple M5, arm64, Python 3.14; no game files) - `python3 -m compileall -q make_game.py tools tests` — OK - `python3 -I scripts/check_no_game_data.py tools/bvh_geometry_check.py tests/test_bvh_geometry_check.py` — OK - `python3 -I tests/test_bvh_geometry_check.py -v` — 12/12 OK (5 new regression tests) - All 11 `tests/test_bvh_*.py` suites — 214/214 OK - `python3 -I tests/test_text_write_line_endings.py` — 3/3 OK - `python3 -I tests/test_ci_registration.py` — 16/16 OK - **Mutation proof** (running the new `tests/test_bvh_geometry_check.py` against unpatched `upstream/main` `1154976d4`): `Ran 12 tests — FAILED (failures=5)` — all 5 new regression tests fail without this fix.
copilot-pull-request-reviewer[bot] commented 2026-11-25 00:06:30 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T00:06:30Z.

Review: COMMENTED


Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6049483324 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1429#pullrequestreview-6049483324). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T00:06:30Z. **Review:** COMMENTED --- Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
GeorgiyDemo commented 2026-11-25 00:20:59 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by GeorgiyDemo on 2026-11-25T00:20:59Z.


Tested e5f742680 on Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), against main 364ab4eab (Python tool only, no GPU):

  • python3 -I tests/test_bvh_geometry_check.py: 12/12 OK at this head (synthetic BVH inputs, no game data).
  • The same file on main fails 5 of 12: test_nan_leaf_bounds_fails_consistency_even_when_other_leaf_covers_bounds, test_nonfinite_scene_bounds_fails_consistency, test_infinite_child_bound_does_not_bypass_override_range_check, test_invalid_geom_spec_and_json_directory_rejected, test_json_report_strict_rfc8259_with_nonfinite_gap — each of the claimed areas has a failing test on main.
  • Did not run the tool on real BVH dumps, only the synthetic tests.

AI-assisted; synthetic fixtures only, no game data.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7148416202 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1429#issuecomment-7148416202). Originally posted by [GeorgiyDemo](https://github.com/GeorgiyDemo) on 2026-11-25T00:20:59Z. --- Tested `e5f742680` on Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), against `main` `364ab4eab` (Python tool only, no GPU): - `python3 -I tests/test_bvh_geometry_check.py`: **12/12 OK** at this head (synthetic BVH inputs, no game data). - The same file on `main` fails **5 of 12**: `test_nan_leaf_bounds_fails_consistency_even_when_other_leaf_covers_bounds`, `test_nonfinite_scene_bounds_fails_consistency`, `test_infinite_child_bound_does_not_bypass_override_range_check`, `test_invalid_geom_spec_and_json_directory_rejected`, `test_json_report_strict_rfc8259_with_nonfinite_gap` — each of the claimed areas has a failing test on `main`. - Did not run the tool on real BVH dumps, only the synthetic tests. AI-assisted; synthetic fixtures only, no game data.
MoonEyes commented 2026-11-28 17:56:30 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T17:56:30Z.


Red/green: tests/test_bvh_geometry_check.py fails 5 assertions on main (NaN leaf bounds, non-finite scene bounds, infinite override tolerance, invalid --geom/--json dir, strict JSON) and passes with the PR.
Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main cb7a239cc in GitHub Actions: no new failure.
AI-assisted (Claude Code, Claude Opus 5.5)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7243405964 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1429#issuecomment-7243405964). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T17:56:30Z. --- Red/green: tests/test_bvh_geometry_check.py fails 5 assertions on main (NaN leaf bounds, non-finite scene bounds, infinite override tolerance, invalid --geom/--json dir, strict JSON) and passes with the PR. Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main cb7a239cc in GitHub Actions: no new failure. AI-assisted (Claude Code, Claude Opus 5.5)
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!1429
No description provided.