Skip to content

bvh_index and navmesh_index: stage report writes so failed runs preserve existing reports - #2684

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
tikD1337:ao/atomic-reports/bvh-navmesh
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
tikD1337:ao/atomic-reports/bvh-navmesh

Conversation

@tikD1337

Copy link
Copy Markdown
Contributor

Claim: #2516 (comment) (BUILD item: atomic report writes in tools/bvh_index.py and tools/navmesh_index.py).

What changes

write_outputs() in both tools opened the JSON and CSV reports with open(dest, "w"). That empties the previous report before the new one is serialized, so a TypeError in json.dump, a full disk or Ctrl+C left a truncated or empty file where an intact report used to be.

Each report now goes through a small _publish(dest, write, newline) helper, the same pattern as tools/rtti_scan.py:write_report:

  • it writes into a tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", dir=<dest folder>, prefix=".bvh_index-" / ".navmesh_index-", suffix=".tmp", delete=False)
  • calls os.chmod(temp, published_mode(dest)) (from tools/file_mode.py), so a replaced report keeps its bits and a new one gets the usual ones
  • then os.replace(temp, dest)
  • on any exception (BaseException, so Ctrl+C counts too) it unlinks the staging file under suppress(OSError) and re-raises; the existing report at dest is untouched

The output bytes don't change: JSON is still written with newline="\n" and CSV with newline="" and lineterminator="\n". Missing parent folders are still created (_make_parent), and the --out/--csv collision and input-overwrite checks still run before the scan.

Tests

New PublishTest in tests/test_bvh_index.py and tests/test_navmesh_index.py, all synthetic with no game data:

  • JSON serialization failure (object() in a row): the existing JSON and CSV are kept byte for byte and no .tmp is left behind
  • CSV write failure (a cell whose str() raises OSError): the existing CSV is kept
  • KeyboardInterrupt from json.dump: the existing reports are kept
  • published_mode(dest) is called for each report, and chmod is applied to a .bvh_index-*.tmp / .navmesh_index-*.tmp file in the destination folder with that mode
  • POSIX only: a replaced report keeps its 0o640 bits

Negative control: before the tool change, 3 tests failed and 1 errored in each file. For example, the interrupted run left '' where the previous JSON report was.

Ran on Windows 11, Python 3, CPU only (no GPU needed):

python -m compileall -q make_game.py tools tests      # OK
python -I tests/test_bvh_index.py                     # Ran 31 tests, OK (skipped=1: POSIX bits)
python -I tests/test_navmesh_index.py                 # Ran 26 tests, OK (skipped=1: POSIX bits)
python -I tests/test_limit_window.py                  # OK
python -I tests/test_navmesh_decode.py                # OK
python -I tests/test_report_path_case.py              # OK (skipped=1)
python -I tests/test_report_source_collision.py       # OK

The POSIX permission-bits test was skipped on this machine, so CI is its first real run.

Notes

🤖 Generated with Claude Code

…p existing reports

write_outputs opened the JSON and CSV reports with open(dest, "w"), which empties
the previous report before the new one is serialized: a TypeError in json.dump, a
full disk or Ctrl+C left a truncated file where an intact report used to be.

Each report is now written to a NamedTemporaryFile in the destination folder
(.bvh_index-*.tmp / .navmesh_index-*.tmp), given published_mode(dest) and swapped
in with os.replace. On any exception the staging file is removed and the
existing report is left untouched.

Tests: PublishTest in tests/test_bvh_index.py and tests/test_navmesh_index.py
(JSON serialization failure, CSV write failure, KeyboardInterrupt, published_mode
applied to the staged file, POSIX bits kept). All synthetic, no game data.

AI-assisted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TurboSosiska304

Copy link
Copy Markdown

Verified head a04ae22.

Environment: CachyOS, Linux 7.3.4-1-cachyos x86_64, Python 3.14.10. CPU only; AMD Ellesmere GPU present but unused.

Commands and results:

  • python3 -I tests/test_bvh_index.py -> 31 tests, OK (0.01s), nothing skipped on Linux.
  • python3 -I tests/test_navmesh_index.py -> 26 tests, OK (0.89s), 1 skipped: the case-insensitive-filesystem test (this tmpfs is case-sensitive).
    The POSIX permission-bit tests ran here (they were skipped on the author's Windows box).

The new PublishTest cases drive write_outputs with a real staging directory: json.dump TypeError, a CSV cell whose str() raises OSError, KeyboardInterrupt, chmod/published_mode call inspection (prefix, .tmp suffix, destination folder, mode), directory left with only the two reports, and 0o640 preservation. These are genuine product-path tests.

Real finding: a run with both --out and --csv is not atomic as a pair. If the CSV write fails after the JSON has been replaced, the JSON is new and the CSV is old, so the two reports disagree; the headline "failed runs keep existing reports" holds per file only. The test with the failing CSV passes because it passes out=None, which hides this case.

Minor: _publish is now a third near-copy of the same helper (rtti_scan, saves.pack); no fsync before replace.

No negative control: tools/rdna2spirv.py is untouched.

@Jadiac5

Jadiac5 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested a04ae22 on Windows 11, Python 3.12.10: pass (real archives, no difference)

  • python -I tools/bvh_index.py <archive> --out … --csv …, head and base 4947579: exit 0 in all runs; CSV byte-identical, JSON differs only in summary.elapsed_s.
  • shared_minigame000_bvh4.rpf (whole file): 2,560 streams, 1,237 BVH4 streams, all complete, 14,759,443 nodes; 12.5 s head / 12.9 s base. story000_bvh4.rpf: 4 streams, no BVH4, 0.2 s. First 2 GiB of core_bvh4.rpf: 120,915 streams, 59,331 BVH4 (59,321 complete, 10 chunked), 527,121,676 nodes, 35.1 GB inflated; 423 s head / 461 s base.
  • python -I tools/navmesh_index.py navmeshes.rpf --out … --csv … --limit 2 GiB: exit 0, CSV byte-identical, JSON differs only in elapsed_s; 32,980 streams, 9,603 matched pages; 36.1 s head / 39.0 s base.
  • The temp-file-and-replace writer (_publish, tools/bvh_index.py:355, tools/navmesh_index.py:204) held for a 77 MB JSON and a 29 MB CSV on NTFS; no .tmp file was left in any output folder.
  • Not done: an interrupted or failing write on real data (the case the PR is about), an existing read-only report, the rest of core_bvh4.rpf and navmeshes.rpf, tests (TurboSosiska304 ran them), CI.

AI-assisted (Claude Code).

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Checked a04ae22 merged onto main (clean git merge-tree) with my own Xbox Series 1.0.2.1 package (counts only), on Windows 11 / Python 3.12. navmesh_index.py navmeshes.rpf --limit 300000000 and bvh_index.py shared_minigame000_bvh4.rpf, both with --out/--csv: the CSVs are identical to main's and the JSON reports differ only in summary.elapsed_s, with the same console output. So staging the report writes leaves a successful run's reports unchanged. AI-assisted check (Claude Code), Vonsek3.

@tupacfan04

Copy link
Copy Markdown
Contributor

Tested merged onto current main on Windows 11 / Python 3.13.0 (the head is ~607 commits behind, so the merge result matters):

suite main head main + PR
test_bvh_index 26 OK 31 OK (1 skip) 31 OK (1 skip)
test_navmesh_index 21 OK 26 OK (1 skip) 27 OK (1 skip, with #2930)
test_navmesh_decode 12 OK 12 OK 14 OK

Negative control (main tools in the head tree): bvh 3 failures + 1 error, navmesh 3 failures + 1 error, matching the claim. Interrupting json.dump mid-write with an existing report: main leaves {"partial": in place (old bytes lost) for both tools; the head keeps the old bytes and leaves no .tmp.

Notes, not blockers:

  • The parent-is-file case (--out <file>/report.json) still raises an uncaught FileExistsError, but the call is inside _publish and happens before any write, so it is an error-type/UX gap rather than a lost report.
  • Atomicity is per report, not per run: the JSON publish can succeed and the CSV write then fail, leaving JSON new and CSV old (bvh_index.py:377-399, navmesh_index.py:225-236); the new tests do not cover that (the bvh CSV case passes out=None).
  • Directory-as---out is left to navmesh_decode, navmesh_index: a --out that is a folder is a usage error, not a traceback #2930.

Conflicts: none with main; only tests/test_navmesh_index.py versus #2930 (both append a class at EOF, keep both).

AI-assisted review, no game files, no GPU.

@ItsNotRexed
ItsNotRexed merged commit 50d5736 into ItsNotRexed:main Nov 22, 2026
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants