build: harden atomic report writes and path/IO error guards in verify_build with CLI suite #2578

Merged
Ammmae0x merged 3 commits from upstream-pr/2586 into main 2026-11-25 22:26:26 +00:00
Ammmae0x commented 2026-11-25 22:05:16 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-25T22:05:16Z.


Summary

Hardens tools/verify_build.py with atomic report staging via tempfile.TemporaryDirectory, deterministic sorted file discovery, upfront input/output directory validation, and clean OSError/ValueError error handling without raw tracebacks, backed by a standalone pure-Python CLI test suite registered in CTest.

Changes

  • tools/verify_build.py:
    • Replace predictable .pid.tmp staging with _write_json_atomic() using tempfile.TemporaryDirectory(dir=destination.parent) + os.replace(), creating parent directories, leaving zero temporary artifacts on failure, and preserving any pre-existing report if serialization or replacement fails.
    • Validate root.is_dir() after args.directory.resolve(strict=True) and reject existing directory targets for --output before running verify_pieces().
    • Sort root.rglob("*") in inventory() and root.glob(pattern) in manifest() for deterministic ordering across filesystems.
    • Catch OSError alongside ValueError during inventory(), verify_pieces(), and atomic report writing via parser.error(...) (exit 2, zero raw tracebacks), while preserving all existing tests/layout_chunks_check.py expectations.
  • tests/verify_build_cli_check.py:
    • Add standalone pure-Python CLI, atomic report write, sorted inventory/glob determinism, sys.argv/__main__ entrypoint, path guard, error-handling, and mutation-killing tests.
  • CMakeLists.txt:
    • Register build_verify_build_cli_check in CTest right after conditional_import_paths.

Verification

All commands executed after rebasing onto upstream/main (fb2f86ac):

  • out/pe-env/Scripts/python.exe -B tests/verify_build_cli_check.py -v
    • Observed: Ran 6 tests in 1.075s / OK (exit 0)
  • out/pe-env/Scripts/python.exe -B tests/layout_chunks_check.py
    • Observed: layout chunk summary ok (exit 0)
  • out/pe-env/Scripts/python.exe -B tests/form_coverage_check.py
    • Observed: form coverage checks passed (exit 0)
  • out/pe-env/Scripts/python.exe -B tests/docs_links_check.py
    • Observed: Self-test passed. Checked 19 Markdown files and 495 links. (exit 0)
  • out/pe-env/Scripts/python.exe -B tests/bidi_controls_check.py
    • Observed: Self-test passed. Checked 1816 tracked source, script and build/CI files: no bidirectional controls. (exit 0)
  • Minimal temp-copy mutation testing (12 adversarial mutations across sorted inventory(), root.is_dir(), output dir/inside-root guards, atomic staging/staging dir, verify_pieces OSError/ValueError, report-write OSError/ValueError, argv forwarding, and failure return code 1): 12/12 killed.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6614699971 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2586). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-25T22:05:16Z. --- ## Summary Hardens `tools/verify_build.py` with atomic report staging via `tempfile.TemporaryDirectory`, deterministic sorted file discovery, upfront input/output directory validation, and clean `OSError`/`ValueError` error handling without raw tracebacks, backed by a standalone pure-Python CLI test suite registered in CTest. ### Changes - **`tools/verify_build.py`**: - Replace predictable `.pid.tmp` staging with `_write_json_atomic()` using `tempfile.TemporaryDirectory(dir=destination.parent)` + `os.replace()`, creating parent directories, leaving zero temporary artifacts on failure, and preserving any pre-existing report if serialization or replacement fails. - Validate `root.is_dir()` after `args.directory.resolve(strict=True)` and reject existing directory targets for `--output` before running `verify_pieces()`. - Sort `root.rglob("*")` in `inventory()` and `root.glob(pattern)` in `manifest()` for deterministic ordering across filesystems. - Catch `OSError` alongside `ValueError` during `inventory()`, `verify_pieces()`, and atomic report writing via `parser.error(...)` (exit 2, zero raw tracebacks), while preserving all existing `tests/layout_chunks_check.py` expectations. - **`tests/verify_build_cli_check.py`**: - Add standalone pure-Python CLI, atomic report write, sorted inventory/glob determinism, `sys.argv`/`__main__` entrypoint, path guard, error-handling, and mutation-killing tests. - **`CMakeLists.txt`**: - Register `build_verify_build_cli_check` in CTest right after `conditional_import_paths`. ## Verification All commands executed after rebasing onto `upstream/main` (`fb2f86ac`): - `out/pe-env/Scripts/python.exe -B tests/verify_build_cli_check.py -v` - Observed: `Ran 6 tests in 1.075s` / `OK` (exit 0) - `out/pe-env/Scripts/python.exe -B tests/layout_chunks_check.py` - Observed: `layout chunk summary ok` (exit 0) - `out/pe-env/Scripts/python.exe -B tests/form_coverage_check.py` - Observed: `form coverage checks passed` (exit 0) - `out/pe-env/Scripts/python.exe -B tests/docs_links_check.py` - Observed: `Self-test passed. Checked 19 Markdown files and 495 links.` (exit 0) - `out/pe-env/Scripts/python.exe -B tests/bidi_controls_check.py` - Observed: `Self-test passed. Checked 1816 tracked source, script and build/CI files: no bidirectional controls.` (exit 0) - Minimal temp-copy mutation testing (12 adversarial mutations across sorted `inventory()`, `root.is_dir()`, output dir/inside-root guards, atomic staging/staging dir, `verify_pieces` `OSError`/`ValueError`, report-write `OSError`/`ValueError`, `argv` forwarding, and failure return code `1`): 12/12 killed. AI-assisted.
upstream-sync closed this pull request 2026-11-25 23:01:55 +00:00
tedin7 commented 2026-11-25 23:01:55 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T22:07:15Z.

Review: COMMENTED


Review of head 8d4936bc (comment only, no merge).

Code (tools/verify_build.py): reads sound. Atomic write with os.replace from a temp dir in the destination parent, early is_dir / output-is-directory checks, and OSError mapped to parser.error all look right. No sibling callsite of inventory() or manifest() was left out.

Finding (test robustness): tests/verify_build_cli_check.py:~170-190, test_inventory_and_manifest_glob_are_sorted, errors on Python 3.14.8:
TypeError: ...reversed_glob() got an unexpected keyword argument 'case_sensitive'.
Since Python 3.13, Path.rglob calls self.glob(pattern, case_sensitive=..., recurse_symlinks=...), so the mocked reversed_glob(path_obj, pattern) (and reversed_rglob) are too narrow. Fix: accept *args, **kwargs in both mocks and forward them to the real function. The test also patches Path internals and asserts the exact glob patterns, which ties it to the implementation. A real directory with out-of-order names and a sort-sensitive assertion on the report would guard the behavior more robustly.
The other 5 tests pass; tests/layout_chunks_check.py passes.

Mutations on the code under test (each reverted):

  • os.replace removed: suite FAILED (3 failures, 1 error, the error being the pre-existing 3.14 one above).
  • sorted(...) removed in inventory() and the is_dir guard disabled: suite FAILED (2 failures).
    So the new tests do catch both changes.

Other commands: git diff --check origin/main...HEAD, python3 tests/docs_links_check.py, python3 tests/bidi_controls_check.py all clean. No Windows run on this head yet and CI was pending, so not merged from my side. Please fix the mock signature, then it can be re-checked.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6065653897 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2586#pullrequestreview-6065653897). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T22:07:15Z. **Review:** COMMENTED --- Review of head 8d4936bc (comment only, no merge). Code (`tools/verify_build.py`): reads sound. Atomic write with `os.replace` from a temp dir in the destination parent, early `is_dir` / output-is-directory checks, and `OSError` mapped to `parser.error` all look right. No sibling callsite of `inventory()` or `manifest()` was left out. Finding (test robustness): `tests/verify_build_cli_check.py:~170-190`, `test_inventory_and_manifest_glob_are_sorted`, errors on Python 3.14.8: `TypeError: ...reversed_glob() got an unexpected keyword argument 'case_sensitive'`. Since Python 3.13, `Path.rglob` calls `self.glob(pattern, case_sensitive=..., recurse_symlinks=...)`, so the mocked `reversed_glob(path_obj, pattern)` (and `reversed_rglob`) are too narrow. Fix: accept `*args, **kwargs` in both mocks and forward them to the real function. The test also patches `Path` internals and asserts the exact glob patterns, which ties it to the implementation. A real directory with out-of-order names and a sort-sensitive assertion on the report would guard the behavior more robustly. The other 5 tests pass; `tests/layout_chunks_check.py` passes. Mutations on the code under test (each reverted): - `os.replace` removed: suite FAILED (3 failures, 1 error, the error being the pre-existing 3.14 one above). - `sorted(...)` removed in `inventory()` and the `is_dir` guard disabled: suite FAILED (2 failures). So the new tests do catch both changes. Other commands: `git diff --check origin/main...HEAD`, `python3 tests/docs_links_check.py`, `python3 tests/bidi_controls_check.py` all clean. No Windows run on this head yet and CI was pending, so not merged from my side. Please fix the mock signature, then it can be re-checked. AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-25 23:01:55 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T22:21:53Z.

Review: COMMENTED


Review of head 4924be40 (comment only).

Checked:

  • tools/verify_build.py: atomic write via a staging dir next to the output, os.replace; not-a-directory and output-is-directory guards; OSError guards around inventory, piece verification and report write; sorted globs for stable results. No overlap with merged #2471 / #646 / #51 (different guards).
  • tests/verify_build_cli_check.py is registered in CMake as build_verify_build_cli_check next to the sibling intake Python tests.
  • Minor, not blocking: an OSError from the second inventory(root) is reported as "cannot write report", which is slightly misleading.

Commands run (Linux, worktree at this head):

  • python3 -B tests/verify_build_cli_check.py -> 6 tests OK
  • python3 tests/docs_links_check.py -> OK; python3 tests/bidi_controls_check.py -> OK
  • git diff --check origin/main...HEAD -> clean
  • Mutation 1: replace os.replace(staged, destination) with pass -> 3 failures + 1 error
  • Mutation 2: disable the output-is-directory guard -> 1 error

Windows run and merge decision follow separately.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6065803965 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2586#pullrequestreview-6065803965). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T22:21:53Z. **Review:** COMMENTED --- Review of head 4924be40 (comment only). Checked: - `tools/verify_build.py`: atomic write via a staging dir next to the output, `os.replace`; not-a-directory and output-is-directory guards; OSError guards around inventory, piece verification and report write; sorted globs for stable results. No overlap with merged #2471 / #646 / #51 (different guards). - `tests/verify_build_cli_check.py` is registered in CMake as `build_verify_build_cli_check` next to the sibling intake Python tests. - Minor, not blocking: an OSError from the second `inventory(root)` is reported as "cannot write report", which is slightly misleading. Commands run (Linux, worktree at this head): - `python3 -B tests/verify_build_cli_check.py` -> 6 tests OK - `python3 tests/docs_links_check.py` -> OK; `python3 tests/bidi_controls_check.py` -> OK - `git diff --check origin/main...HEAD` -> clean - Mutation 1: replace `os.replace(staged, destination)` with `pass` -> 3 failures + 1 error - Mutation 2: disable the output-is-directory guard -> 1 error Windows run and merge decision follow separately. AI-assisted; commands listed were executed locally.
Ammmae0x commented 2026-11-25 23:02:04 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-25T22:20:17Z.


Addressed in commit 4924be40 (tests/verify_build_cli_check.py):

  • Updated reversed_rglob and reversed_glob in test_inventory_and_manifest_glob_are_sorted to accept and forward *args, **kwargs so Python 3.13+/3.14 Path.rglob delegation to self.glob(..., case_sensitive=..., recurse_symlinks=...) works without TypeError.
  • Removed the brittle internal seen_glob_patterns equality assertion (which also observes "*" when Path.rglob delegates to Path.glob) and added a real directory with out-of-order filenames (z_extra.bin, m/z.bin, a_extra.bin, m/a.bin) verifying both inventory() key order and the report's extra_disk_files ordering.

Local verification after 4924be40:

  • out\pe-env\Scripts\python.exe -B tests/verify_build_cli_check.py -v -> 6 tests OK
  • out\pe-env\Scripts\python.exe -B tests/layout_chunks_check.py -> ok
  • out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py -> passed
  • out\pe-env\Scripts\python.exe -B tests/docs_links_check.py -> 19 Markdown files, 495 links ok
  • out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py -> 1816 tracked files ok

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7176237007 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2586#issuecomment-7176237007). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-25T22:20:17Z. --- Addressed in commit `4924be40` (`tests/verify_build_cli_check.py`): - Updated `reversed_rglob` and `reversed_glob` in `test_inventory_and_manifest_glob_are_sorted` to accept and forward `*args, **kwargs` so Python 3.13+/3.14 `Path.rglob` delegation to `self.glob(..., case_sensitive=..., recurse_symlinks=...)` works without `TypeError`. - Removed the brittle internal `seen_glob_patterns` equality assertion (which also observes `"*"` when `Path.rglob` delegates to `Path.glob`) and added a real directory with out-of-order filenames (`z_extra.bin`, `m/z.bin`, `a_extra.bin`, `m/a.bin`) verifying both `inventory()` key order and the report's `extra_disk_files` ordering. Local verification after `4924be40`: - `out\pe-env\Scripts\python.exe -B tests/verify_build_cli_check.py -v` -> 6 tests OK - `out\pe-env\Scripts\python.exe -B tests/layout_chunks_check.py` -> ok - `out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py` -> passed - `out\pe-env\Scripts\python.exe -B tests/docs_links_check.py` -> 19 Markdown files, 495 links ok - `out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py` -> 1816 tracked files ok AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-25 23:02:05 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T22:25:40Z.


Win2025 WARP receipt for head a31f2366 after updating with main.

Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit a31f236679200ba675f612a4bcf99782c0f55c0d:

  • Debug (--preset dev):
    • Build (cmake --build --preset dev -- -j 2): 28.77s (configure 4.43s)
    • Test (ctest --preset dev -R 'build_verify_build_cli_check' --parallel 2 --no-tests=error --output-on-failure): 1/1 passed (2.33s: build_verify_build_cli_check 2.10s)
  • Release /O2 (--preset release):
    • Build (cmake --build --preset release -- -j 2): 22.47s (configure 3.22s)
    • Test (ctest --preset release -R 'build_verify_build_cli_check' --parallel 2 --no-tests=error --output-on-failure): 1/1 passed (1.93s: build_verify_build_cli_check 1.76s)
  • Windows Python checks: tests/layout_chunks_check.py ok (1.5s)
  • sccache: 194/271 hits; wall 77.8s

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7176348260 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2586#issuecomment-7176348260). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T22:25:40Z. --- Win2025 WARP receipt for head a31f2366 after updating with main. Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit `a31f236679200ba675f612a4bcf99782c0f55c0d`: - **Debug (`--preset dev`)**: - Build (`cmake --build --preset dev -- -j 2`): `28.77s` (configure `4.43s`) - Test (`ctest --preset dev -R 'build_verify_build_cli_check' --parallel 2 --no-tests=error --output-on-failure`): **1/1 passed** (`2.33s`: `build_verify_build_cli_check` `2.10s`) - **Release `/O2` (`--preset release`)**: - Build (`cmake --build --preset release -- -j 2`): `22.47s` (configure `3.22s`) - Test (`ctest --preset release -R 'build_verify_build_cli_check' --parallel 2 --no-tests=error --output-on-failure`): **1/1 passed** (`1.93s`: `build_verify_build_cli_check` `1.76s`) - **Windows Python checks**: `tests/layout_chunks_check.py` ok (`1.5s`) - `sccache`: 194/271 hits; wall `77.8s` AI-assisted; commands listed were executed locally.
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
R0Y00/Xbox2Windows!2578
No description provided.