xdxr_bvh: handle singular TLAS transforms, cyclic/short BVHs, and case validation (XDXR-BVH-SINGULAR-CYCLES-AND-BOUNDS) #1469

Open
github-bot wants to merge 1 commit from github-bot/gh-1469 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 00:55 UTC · original
IlyassRS3:fix/xdxr-bvh-cycles-singular-and-bounds → main · +311 −68 · 2 files · 1 commits · open

Claimed in the coordination issue as: XDXR-BVH-SINGULAR-CYCLES-AND-BOUNDS (#14)

Summary

Hardens tools/xdxr_bvh.py (CPU reference encoder, validator, walker, and GPU build checker — zero other open PRs touch this file):

  1. Singular / non-finite TLAS transform parity (build_tlas, brute_force): Per docs/bvh.md §7a and runtime/src/xdxr_build.comp (MODE_INSTANCES), an instance with a singular transform gets a zero inverse and an empty world box even when its BLAS address is non-null. Previously build_tlas() and brute_force() raised ValueError("singular or non-finite instance transform") on a singular transform with a real BLAS. Both now treat singular/non-finite instances as inactive (while invert_affine() itself continues to raise ValueError).
  2. Pointer and 40-bit BLAS address validation (ptr, build_tlas):
    • ptr(offset, kind) now rejects booleans, negative offsets, and kind outside 0..7 (previously ptr(-64, BOX16) returned -4 and ptr(64, 8) silently overflowed into the unit index).
    • build_tlas() validates blas_va is a non-bool integer in 0..2^40-1 and 256-byte aligned (matching docs/bvh.md §7a: stored as blas_va >> 8 in u32), raising ValueError instead of crashing inside struct.pack_into with struct.error.
  3. Header offsetToParents alignment check (validate): Rejects offsetToParents % 4 != 0 (docs/bvh.md §3.1 confirms 4-byte alignment across all 272,864/272,864 streams), while preserving the existing console negative-offset and unstored-entry handling.
  4. Cycle and bounds guards across walkers (box16_rounding, node_census, pair_rotations, _trace_blas, trace):
    • box16_rounding() checks header/node bounds, rejects non-triangle/non-box node types, and tracks active recursion frames (visiting) so cyclic BVHs report a cycle error instead of crashing with RecursionError.
    • node_census() and pair_rotations() guard header/node bounds and track visited pointers so cyclic BVHs terminate instead of hanging in an infinite loop.
    • _trace_blas() and trace() track visited pointers per traversal and raise ValueError("cycle in BVH ...") instead of looping forever.
  5. Case loading and check-build resilience (_load_case, check_build):
    • Uses os.path.join for Windows path safety and decodes case JSON with utf-8-sig so UTF-8 BOM files load cleanly.
    • Rejects bool values for instance["blas"], address, mask, bias, and rays, validates rays >= 1, 12-element transforms, and triangle schemas with CaseError naming the offending .json.
    • When a dumped .bin is truncated (< 64 bytes) or fails traversal (IndexError/ValueError, e.g., cyclic BVH or mismatched instance root), check_build() records it as a case problem (exit 1) instead of aborting early with exit 2 (ERROR: a case in ... does not describe a build) or hanging.

Verification

  • AI-assisted: yes
  • Host / hardware: macOS (Apple M5, arm64, CPU-only, no GPU, no game files), Python 3.14
  • Tests run:
    • python3 -I tests/test_xdxr_bvh.py → 60/60 OK (0.824s)
    • python3 -m compileall -q make_game.py tools tests → exit 0
    • python3 -I scripts/check_no_game_data.py tools/xdxr_bvh.py tests/test_xdxr_bvh.py → exit 0
    • python3 -I tests/test_text_write_line_endings.py → 3/3 OK
    • python3 -I tests/test_ci_registration.py → 16/16 OK
  • Mutation proof against unpatched upstream/main (b4b7756e8): copied tests/test_xdxr_bvh.py onto a detached worktree of upstream/main; all 8 new/extended test cases failed (Layout.test_pointer_encoding: failures=5; ClosestHit.test_instance_record: failures=1, errors=2; ClosestHit.test_singular_transform_with_valid_blas_is_inactive_in_tlas_and_brute_force: errors=1; Validate.test_misaligned_offset_to_parents_is_rejected: failures=1; Validate.test_cyclic_and_short_buffers_terminate_cleanly_across_walkers: errors=1 (RecursionError); Cli.test_check_build_accepts_utf8_bom_case_json: failures=1; Cli.test_check_build_rejects_bool_indices_and_bad_transform_or_rays: failures=4; Cli.test_check_build_corrupt_truncated_or_cyclic_bin_exits_1_not_2: infinite loop timeout > 3s).
**@⁠IlyassRS3** on GitHub · 2026-11-25 00:55 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1469) `IlyassRS3:fix/xdxr-bvh-cycles-singular-and-bounds` → `main` · +311 −68 · 2 files · 1 commits · open Claimed in the coordination issue as: `XDXR-BVH-SINGULAR-CYCLES-AND-BOUNDS` (#14) ### Summary Hardens `tools/xdxr_bvh.py` (CPU reference encoder, validator, walker, and GPU build checker — zero other open PRs touch this file): 1. **Singular / non-finite TLAS transform parity (`build_tlas`, `brute_force`)**: Per `docs/bvh.md` §7a and `runtime/src/xdxr_build.comp` (`MODE_INSTANCES`), an instance with a singular transform gets a zero inverse and an empty world box even when its BLAS address is non-null. Previously `build_tlas()` and `brute_force()` raised `ValueError("singular or non-finite instance transform")` on a singular transform with a real BLAS. Both now treat singular/non-finite instances as inactive (while `invert_affine()` itself continues to raise `ValueError`). 2. **Pointer and 40-bit BLAS address validation (`ptr`, `build_tlas`)**: - `ptr(offset, kind)` now rejects booleans, negative offsets, and `kind` outside `0..7` (previously `ptr(-64, BOX16)` returned `-4` and `ptr(64, 8)` silently overflowed into the unit index). - `build_tlas()` validates `blas_va` is a non-bool integer in `0..2^40-1` and 256-byte aligned (matching `docs/bvh.md` §7a: stored as `blas_va >> 8` in `u32`), raising `ValueError` instead of crashing inside `struct.pack_into` with `struct.error`. 3. **Header `offsetToParents` alignment check (`validate`)**: Rejects `offsetToParents % 4 != 0` (`docs/bvh.md` §3.1 confirms 4-byte alignment across all 272,864/272,864 streams), while preserving the existing console negative-offset and unstored-entry handling. 4. **Cycle and bounds guards across walkers (`box16_rounding`, `node_census`, `pair_rotations`, `_trace_blas`, `trace`)**: - `box16_rounding()` checks header/node bounds, rejects non-triangle/non-box node types, and tracks active recursion frames (`visiting`) so cyclic BVHs report a cycle error instead of crashing with `RecursionError`. - `node_census()` and `pair_rotations()` guard header/node bounds and track `visited` pointers so cyclic BVHs terminate instead of hanging in an infinite loop. - `_trace_blas()` and `trace()` track `visited` pointers per traversal and raise `ValueError("cycle in BVH ...")` instead of looping forever. 5. **Case loading and `check-build` resilience (`_load_case`, `check_build`)**: - Uses `os.path.join` for Windows path safety and decodes case JSON with `utf-8-sig` so UTF-8 BOM files load cleanly. - Rejects `bool` values for `instance["blas"]`, `address`, `mask`, `bias`, and `rays`, validates `rays >= 1`, 12-element transforms, and triangle schemas with `CaseError` naming the offending `.json`. - When a dumped `.bin` is truncated (< 64 bytes) or fails traversal (`IndexError`/`ValueError`, e.g., cyclic BVH or mismatched instance root), `check_build()` records it as a case problem (exit `1`) instead of aborting early with exit `2` (`ERROR: a case in ... does not describe a build`) or hanging. ### Verification - **AI-assisted**: yes - **Host / hardware**: macOS (Apple M5, `arm64`, CPU-only, no GPU, no game files), Python 3.14 - **Tests run**: - `python3 -I tests/test_xdxr_bvh.py` → `60/60 OK` (`0.824s`) - `python3 -m compileall -q make_game.py tools tests` → exit `0` - `python3 -I scripts/check_no_game_data.py tools/xdxr_bvh.py tests/test_xdxr_bvh.py` → exit `0` - `python3 -I tests/test_text_write_line_endings.py` → `3/3 OK` - `python3 -I tests/test_ci_registration.py` → `16/16 OK` - **Mutation proof against unpatched `upstream/main` (`b4b7756e8`)**: copied `tests/test_xdxr_bvh.py` onto a detached worktree of `upstream/main`; all 8 new/extended test cases failed (`Layout.test_pointer_encoding`: `failures=5`; `ClosestHit.test_instance_record`: `failures=1, errors=2`; `ClosestHit.test_singular_transform_with_valid_blas_is_inactive_in_tlas_and_brute_force`: `errors=1`; `Validate.test_misaligned_offset_to_parents_is_rejected`: `failures=1`; `Validate.test_cyclic_and_short_buffers_terminate_cleanly_across_walkers`: `errors=1` (`RecursionError`); `Cli.test_check_build_accepts_utf8_bom_case_json`: `failures=1`; `Cli.test_check_build_rejects_bool_indices_and_bad_transform_or_rays`: `failures=4`; `Cli.test_check_build_corrupt_truncated_or_cyclic_bin_exits_1_not_2`: infinite loop timeout > 3s).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:52:35 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 00:55 UTC · original · reviewed

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.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 00:55 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1469#pullrequestreview-6049899798) · **reviewed** 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.
Author
Owner

@⁠whoalin1 on GitHub · 2026-11-25 07:34 UTC · original

Independent verification (CPU/tools-only, native Windows, Python 3.11.9 — stdlib-only tools so representative): diff reviewed line-by-line, all claimed fixes present and covered by the new tests; neighboring suites green. Details in the verification batch log. AI-assisted verification.

**@⁠whoalin1** on GitHub · 2026-11-25 07:34 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1469#issuecomment-7156019010) Independent verification (CPU/tools-only, native Windows, Python 3.11.9 — stdlib-only tools so representative): diff reviewed line-by-line, all claimed fixes present and covered by the new tests; neighboring suites green. Details in the verification batch log. AI-assisted verification.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1469/head:github-bot/gh-1469'
git switch 'github-bot/gh-1469'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1469'
git switch 'github-bot/gh-1469'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1469'
git switch 'github-bot/gh-1469'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1469'
git switch 'main'
git merge --squash 'github-bot/gh-1469'
git switch 'main'
git merge --ff-only 'github-bot/gh-1469'
git switch 'main'
git merge 'github-bot/gh-1469'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.