shader: extract main(argv), atomic HLSL writes, and directory/struct/zlib guards in shader_profile_audit with CLI suite #2546

Merged
Ammmae0x merged 3 commits from upstream-pr/2554 into main 2026-11-25 21:08:21 +00:00
Ammmae0x commented 2026-11-25 20:38:43 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-25T20:38:43Z.


Change

Harden tools/shader_profile_audit.py CLI argument handling, output writes, and error guards, and add a standalone pure-Python CLI/mutation test suite (tests/shader_profile_audit_cli_check.py, registered as shader_profile_audit_cli_check in CMakeLists.txt):

  • Update main(argv: Sequence[str] | None = None) -> int to forward argv to parser.parse_args(argv) and return 0, invoking raise SystemExit(main()) under if __name__ == "__main__".
  • Defer accepted HLSL and s9-slot0-read-probe.hlsl writes until scanning finishes, validate every destination against the archive path (_same_path() via resolve() and samefile()) and existing directories, and write atomically via tempfile.TemporaryDirectory(dir=destination.parent) + os.replace() so failures leave zero partial HLSL files or temporary directories.
  • Reject directory archive paths and --source-dir regular files or archive collisions upfront before scanning.
  • Catch (OSError, ValueError, struct.error, zlib.error) cleanly via parser.exit(1, f"Shader profile audit: {error}\n") with zero raw tracebacks while preserving JSON stdout and scan() behavior.

AI-assisted.

Verification

Verified on commit 91a483bf0e36aceed10a38818551f811c315b906 (rebased onto upstream/main 5f51daaa) using out\pe-env\Scripts\python.exe -B:

  • out\pe-env\Scripts\python.exe -B tests/shader_profile_audit_cli_check.py -v -> exit 0 (Ran 8 tests in 10.674s, OK)
  • out\pe-env\Scripts\python.exe -B tests/shader_profile_audit_check.py -v -> exit 0 (Ran 3 tests in 3.025s, OK)
  • out\pe-env\Scripts\python.exe -B tests/shader_census_check.py -v -> exit 0 (Ran 9 tests in 0.979s, OK)
  • out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py -> exit 0 (form coverage checks passed)
  • out\pe-env\Scripts\python.exe -B tests/docs_links_check.py -> exit 0 (Self-test passed. Checked 19 Markdown files and 483 links.)
  • out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py -> exit 0 (Self-test passed. Checked 1783 tracked source, script and build/CI files: no bidirectional controls.)
  • Spot mutation check: 11/11 targeted mutations in tools/shader_profile_audit.py (parse_args(argv), _same_path resolve()/samefile(), --source-dir archive collision & non-directory checks, per-destination archive collision, upfront args.archive.is_dir(), struct.error & zlib.error guards, atomic staging write, return 0) caused tests/shader_profile_audit_cli_check.py to fail (exit 1).

Evidence and follow-up

  • New fixtures in tests/shader_profile_audit_cli_check.py reuse the synthetic in-memory DXBC/DXIL/PSV0/RPF builders and stub disassembler from tests/shader_profile_audit_check.py under the repository MIT License.
  • Negative controls cover missing/directory archives, --source-dir regular files and archive collisions (relative alias, non-existent resolve(), and hardlink samefile()), output-file archive collision (s9-slot0-read-probe.hlsl), os.replace() OSError leaving zero .hlsl files or temp dirs, mid-scan struct.error/zlib.error leaving zero partial files or tracebacks, and Python 3.14 argparse-compatible usage errors.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6613292782 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2554). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-25T20:38:43Z. --- ## Change Harden `tools/shader_profile_audit.py` CLI argument handling, output writes, and error guards, and add a standalone pure-Python CLI/mutation test suite (`tests/shader_profile_audit_cli_check.py`, registered as `shader_profile_audit_cli_check` in `CMakeLists.txt`): - Update `main(argv: Sequence[str] | None = None) -> int` to forward `argv` to `parser.parse_args(argv)` and return `0`, invoking `raise SystemExit(main())` under `if __name__ == "__main__"`. - Defer accepted HLSL and `s9-slot0-read-probe.hlsl` writes until scanning finishes, validate every destination against the archive path (`_same_path()` via `resolve()` and `samefile()`) and existing directories, and write atomically via `tempfile.TemporaryDirectory(dir=destination.parent)` + `os.replace()` so failures leave zero partial HLSL files or temporary directories. - Reject directory archive paths and `--source-dir` regular files or archive collisions upfront before scanning. - Catch `(OSError, ValueError, struct.error, zlib.error)` cleanly via `parser.exit(1, f"Shader profile audit: {error}\n")` with zero raw tracebacks while preserving JSON stdout and `scan()` behavior. AI-assisted. ## Verification Verified on commit `91a483bf0e36aceed10a38818551f811c315b906` (rebased onto `upstream/main` `5f51daaa`) using `out\pe-env\Scripts\python.exe -B`: - `out\pe-env\Scripts\python.exe -B tests/shader_profile_audit_cli_check.py -v` -> exit 0 (`Ran 8 tests in 10.674s`, `OK`) - `out\pe-env\Scripts\python.exe -B tests/shader_profile_audit_check.py -v` -> exit 0 (`Ran 3 tests in 3.025s`, `OK`) - `out\pe-env\Scripts\python.exe -B tests/shader_census_check.py -v` -> exit 0 (`Ran 9 tests in 0.979s`, `OK`) - `out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py` -> exit 0 (`form coverage checks passed`) - `out\pe-env\Scripts\python.exe -B tests/docs_links_check.py` -> exit 0 (`Self-test passed. Checked 19 Markdown files and 483 links.`) - `out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py` -> exit 0 (`Self-test passed. Checked 1783 tracked source, script and build/CI files: no bidirectional controls.`) - Spot mutation check: 11/11 targeted mutations in `tools/shader_profile_audit.py` (`parse_args(argv)`, `_same_path` `resolve()`/`samefile()`, `--source-dir` archive collision & non-directory checks, per-destination archive collision, upfront `args.archive.is_dir()`, `struct.error` & `zlib.error` guards, atomic staging write, `return 0`) caused `tests/shader_profile_audit_cli_check.py` to fail (exit 1). ## Evidence and follow-up - New fixtures in `tests/shader_profile_audit_cli_check.py` reuse the synthetic in-memory DXBC/DXIL/PSV0/RPF builders and stub disassembler from `tests/shader_profile_audit_check.py` under the repository MIT License. - Negative controls cover missing/directory archives, `--source-dir` regular files and archive collisions (relative alias, non-existent `resolve()`, and hardlink `samefile()`), output-file archive collision (`s9-slot0-read-probe.hlsl`), `os.replace()` `OSError` leaving zero `.hlsl` files or temp dirs, mid-scan `struct.error`/`zlib.error` leaving zero partial files or tracebacks, and Python 3.14 `argparse`-compatible usage errors. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets.
tedin7 commented 2026-11-25 21:01:26 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T20:49:06Z.

Review: COMMENTED


Review of head 91a483bf (CI is green; the branch is 8 commits behind main).

Finding: one new test depends on directory listing order

  • tests/shader_profile_audit_cli_check.py:114 compares [p.name for p in path.parent.iterdir()] with a sorted list. iterdir() order is filesystem-specific. On Linux it fails: ['s9-slot0-read-probe.hlsl', 'point-mesh', 'store', 'general'] != ['general', 'point-mesh', 's9-slot0-read-probe.hlsl', 'store']. It only passes where the listing happens to come back sorted (NTFS).
  • Fix: wrap the left side in sorted(...). The behavior under test (no staging directories left behind, exactly these entries) stays the same.
  • The suite is registered in CMake but runs through the Windows CTest, so CI does not see this. A Linux run of the Python checks would.

What else I checked (all fine)

  • main(argv) forwards argv and returns 0; scan() behavior and JSON stdout are unchanged.
  • HLSL and probe writes are deferred until the scan ends and are replaced atomically; directory/collision checks run before any write.
  • struct.error and zlib.error now exit with a clean message.
  • Mutation: removing the probe-file write makes the suite fail (2 failures instead of 1), so that path is guarded.
  • No other open PR touches shader_profile_audit (#1850 is merged and is a different change).

Commands run locally (Linux, PR worktree)

  • python3 -B tests/shader_profile_audit_cli_check.py: 7 of 8 pass, 1 fails (above)
  • git diff --check origin/main...HEAD: clean
  • python3 tests/docs_links_check.py, python3 tests/bidi_controls_check.py: passed

Not merging until the test is made order-independent.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6064789621 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2554#pullrequestreview-6064789621). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T20:49:06Z. **Review:** COMMENTED --- Review of head 91a483bf (CI is green; the branch is 8 commits behind main). Finding: one new test depends on directory listing order - `tests/shader_profile_audit_cli_check.py:114` compares `[p.name for p in path.parent.iterdir()]` with a sorted list. `iterdir()` order is filesystem-specific. On Linux it fails: `['s9-slot0-read-probe.hlsl', 'point-mesh', 'store', 'general'] != ['general', 'point-mesh', 's9-slot0-read-probe.hlsl', 'store']`. It only passes where the listing happens to come back sorted (NTFS). - Fix: wrap the left side in `sorted(...)`. The behavior under test (no staging directories left behind, exactly these entries) stays the same. - The suite is registered in CMake but runs through the Windows CTest, so CI does not see this. A Linux run of the Python checks would. What else I checked (all fine) - `main(argv)` forwards `argv` and returns 0; `scan()` behavior and JSON stdout are unchanged. - HLSL and probe writes are deferred until the scan ends and are replaced atomically; directory/collision checks run before any write. - `struct.error` and `zlib.error` now exit with a clean message. - Mutation: removing the probe-file write makes the suite fail (2 failures instead of 1), so that path is guarded. - No other open PR touches `shader_profile_audit` (#1850 is merged and is a different change). Commands run locally (Linux, PR worktree) - `python3 -B tests/shader_profile_audit_cli_check.py`: 7 of 8 pass, 1 fails (above) - `git diff --check origin/main...HEAD`: clean - `python3 tests/docs_links_check.py`, `python3 tests/bidi_controls_check.py`: passed Not merging until the test is made order-independent. AI-assisted; commands listed were executed locally.
Ammmae0x commented 2026-11-25 21:01:35 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-25T20:58:58Z.


Addressed in 3957ecc5:

  • Wrapped path.parent.iterdir() in sorted(p.name for p in path.parent.iterdir()) at tests/shader_profile_audit_cli_check.py:114 so the directory-listing assertion is independent of filesystem entry order.

Verified on 3957ecc5:

  • out/pe-env/Scripts/python.exe -B tests/shader_profile_audit_cli_check.py -v: 8/8 OK (also verified with Path.iterdir() monkeypatched to reverse order).
  • out/pe-env/Scripts/python.exe -B tests/shader_profile_audit_check.py -v: 3/3 OK; tests/shader_census_check.py -v: 9/9 OK.
  • ctest --preset dev -R "^shader_profile_audit" --output-on-failure: 2/2 passed (13.86 s).
  • ctest --preset release -R "^shader_profile_audit" --output-on-failure: 2/2 passed (15.34 s).
  • tests/docs_links_check.py (19 files / 483 links) and tests/bidi_controls_check.py (1783 files): passed.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7174450406 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2554#issuecomment-7174450406). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-25T20:58:58Z. --- Addressed in `3957ecc5`: - Wrapped `path.parent.iterdir()` in `sorted(p.name for p in path.parent.iterdir())` at `tests/shader_profile_audit_cli_check.py:114` so the directory-listing assertion is independent of filesystem entry order. Verified on `3957ecc5`: - `out/pe-env/Scripts/python.exe -B tests/shader_profile_audit_cli_check.py -v`: 8/8 OK (also verified with `Path.iterdir()` monkeypatched to reverse order). - `out/pe-env/Scripts/python.exe -B tests/shader_profile_audit_check.py -v`: 3/3 OK; `tests/shader_census_check.py -v`: 9/9 OK. - `ctest --preset dev -R "^shader_profile_audit" --output-on-failure`: 2/2 passed (13.86 s). - `ctest --preset release -R "^shader_profile_audit" --output-on-failure`: 2/2 passed (15.34 s). - `tests/docs_links_check.py` (19 files / 483 links) and `tests/bidi_controls_check.py` (1783 files): passed. AI-assisted.
upstream-sync closed this pull request 2026-11-25 22:02:01 +00:00
tedin7 commented 2026-11-25 22:02:02 +00:00 (Migrated from github.com)

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

Review: COMMENTED


Review of head 3957ecc5 (re-review after the author's follow-up; only change since 91a483bf is tests/shader_profile_audit_cli_check.py:114, now sorted(p.name for p in path.parent.iterdir())).

Previous finding is fixed: the listing comparison no longer depends on directory order.

What I checked on this head (Linux, clean worktree):

  • python3 -I -B tests/shader_profile_audit_cli_check.py -> Ran 8 tests, OK
  • python3 tests/docs_links_check.py -> ok; python3 tests/bidi_controls_check.py -> ok
  • git diff --check origin/main...HEAD -> clean
  • Mutation: replace os.replace(staged, destination) with a direct write_text (tools/shader_profile_audit.py:230) -> suite FAILS (2 failures). Good.
  • Mutation: drop the archive-collision check (tools/shader_profile_audit.py:297) -> suite FAILS (1 failure). Good.
  • Mutation: disable destination.exists() and destination.is_dir() (tools/shader_profile_audit.py:299) -> suite still passes. Minor: the "output path is a directory" message is not pinned by a test (the write still fails cleanly through the OSError handler). Not a blocker; a test asserting the exact message would pin it.

Scope: Python tool + a pure-Python suite + CMake registration, no shader lowering, no startup-path claim. No other open or merged PR covers this slice.

The suite is registered in CTest, so the Windows run is the remaining gate; I will queue it on the updated head.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6064935472 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2554#pullrequestreview-6064935472). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T21:02:11Z. **Review:** COMMENTED --- Review of head 3957ecc5 (re-review after the author's follow-up; only change since 91a483bf is `tests/shader_profile_audit_cli_check.py:114`, now `sorted(p.name for p in path.parent.iterdir())`). Previous finding is fixed: the listing comparison no longer depends on directory order. What I checked on this head (Linux, clean worktree): - `python3 -I -B tests/shader_profile_audit_cli_check.py` -> `Ran 8 tests`, `OK` - `python3 tests/docs_links_check.py` -> ok; `python3 tests/bidi_controls_check.py` -> ok - `git diff --check origin/main...HEAD` -> clean - Mutation: replace `os.replace(staged, destination)` with a direct `write_text` (`tools/shader_profile_audit.py:230`) -> suite FAILS (2 failures). Good. - Mutation: drop the archive-collision check (`tools/shader_profile_audit.py:297`) -> suite FAILS (1 failure). Good. - Mutation: disable `destination.exists() and destination.is_dir()` (`tools/shader_profile_audit.py:299`) -> suite still passes. Minor: the "output path is a directory" message is not pinned by a test (the write still fails cleanly through the `OSError` handler). Not a blocker; a test asserting the exact message would pin it. Scope: Python tool + a pure-Python suite + CMake registration, no shader lowering, no startup-path claim. No other open or merged PR covers this slice. The suite is registered in CTest, so the Windows run is the remaining gate; I will queue it on the updated head. AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-25 22:02:16 +00:00 (Migrated from github.com)

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


Win2025 WARP receipt for head 1275e912 after updating with main (run limited to the PR's tests: --regex shader_profile_audit).

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

  • Debug (--preset dev):
    • Build (cmake --build --preset dev -- -j 2): 27.00s (configure 1.86s)
    • Test (ctest --preset dev -R 'shader_profile_audit' --parallel 4 --no-tests=error --output-on-failure): 2/2 passed (3.45s: shader_profile_audit_check 1.63s, shader_profile_audit_cli_check 3.33s)
  • Release /O2 (--preset release):
    • Build (cmake --build --preset release -- -j 2): 62.00s (configure 1.87s)
    • Test (ctest --preset release -R 'shader_profile_audit' --parallel 4 --no-tests=error --output-on-failure): 2/2 passed (2.92s: shader_profile_audit_check 1.37s, shader_profile_audit_cli_check 2.80s)
  • Windows Python checks: tests/shader_census_check.py ok (1.1s)
  • sccache: 30/46 hits; wall 103.7s

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7174650148 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2554#issuecomment-7174650148). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T21:07:45Z. --- Win2025 WARP receipt for head 1275e912 after updating with main (run limited to the PR's tests: `--regex shader_profile_audit`). Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit `1275e91206ee2009e0d00441a7984c70426b6359`: - **Debug (`--preset dev`)**: - Build (`cmake --build --preset dev -- -j 2`): `27.00s` (configure `1.86s`) - Test (`ctest --preset dev -R 'shader_profile_audit' --parallel 4 --no-tests=error --output-on-failure`): **2/2 passed** (`3.45s`: `shader_profile_audit_check` `1.63s`, `shader_profile_audit_cli_check` `3.33s`) - **Release `/O2` (`--preset release`)**: - Build (`cmake --build --preset release -- -j 2`): `62.00s` (configure `1.87s`) - Test (`ctest --preset release -R 'shader_profile_audit' --parallel 4 --no-tests=error --output-on-failure`): **2/2 passed** (`2.92s`: `shader_profile_audit_check` `1.37s`, `shader_profile_audit_cli_check` `2.80s`) - **Windows Python checks**: `tests/shader_census_check.py` ok (`1.1s`) - `sccache`: 30/46 hits; wall `103.7s` 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!2546
No description provided.