Skip to content

tex_headers: stage --out JSON (preserve previous on write fail) - #9234

Open
Grimkeh wants to merge 1 commit into
ItsNotRexed:mainfrom
Grimkeh:fix/tex-headers-preserve-json
Open

Grimkeh wants to merge 1 commit into
ItsNotRexed:mainfrom
Grimkeh:fix/tex-headers-preserve-json

Conversation

@Grimkeh

@Grimkeh Grimkeh commented Nov 23, 2026

Copy link
Copy Markdown

Summary

  • Stage textures_index.json / --out via temp + replace_retry so a failed publish leaves the previous complete report.
  • O_WRONLY probe (no truncate) before staging; published_mode on Unix.
  • Unit test: mocked replace_retry failure keeps prior file bytes.
  • No game files. AI-assisted.

Refs #8504

Made with Cursor

…us report

Same BUILD-589 preserve pattern as recent twins: O_WRONLY probe without truncate,
NamedTemporaryFile + replace_retry, published_mode on Unix.

Co-authored-by: Cursor <cursoragent@cursor.com>
@Matiux14

Copy link
Copy Markdown

Reviewed 17937be71 (CPU only, Windows 11, Python 3.15.2) against main 867d922f0.

  • python -m compileall -q make_game.py tools tests -> exit 0
  • python -I tests/test_tex_headers.py -> Ran 78 tests ... OK
  • python -I tests/test_tex_headers.py TestWriteJsonReportPreserve.test_out_write_failure_preserves_previous_report -v -> OK
  • Negative control: tools/tex_headers.py reverted to main with the new test kept -> AttributeError: module 'tex_headers' has no attribute 'replace_retry' -> the test pins the change.
  • python -I scripts/check_no_game_data.py tools/tex_headers.py tests/test_tex_headers.py -> exit 0
  • git diff --check main...pr-9234 -> clean

The change follows the same write_json_report staging pattern applied in #9219 (xcl_compare), plus the existing file_mode/replace_retry helpers. A mid-write failure now leaves the previous report intact; the error is still reported through the same ERROR: cannot write ... path.

AI-assisted review.

@Kiril-Donec Kiril-Donec left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent native Windows verification of 17937be71179c8a6272401616a31b3fa64644f20, merged cleanly onto current main 6312c057f: pass.

Windows 11 build 26300, CPython 3.13.5; CPU only, no game files or GPU.

  • python -I tests/test_tex_headers.py: 78 tests passed.
  • Additional synthetic CLI negative control: replace json.dump with a writer that writes a JSON prefix then raises OSError. Both revisions return 2 with one stderr line. On current main the previous report is truncated; with this PR its bytes remain unchanged and there are no .tex_headers-* files left.
  • Real Windows rename-lock recovery: hold the existing report with CreateFileW(GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE), omitting share-delete. Releasing after 200 ms lets the unchanged PR writer publish the complete expected JSON (0.317 s here); no staging file remains.
  • Keep the same native handle open for the whole retry window: exit 2 after 5.003 s, one stderr line, previous report byte-identical, no staging files left.
  • Normal successful publication matches the expected synthetic rows.
  • py_compile, changed-file/probe check_no_game_data.py, and git diff --check: pass.

No defect found in these checked paths. Non-regular destinations and POSIX permission behavior were not additionally exercised. The CLI probe mocks only scanning/the deliberate partial-write failure; the Windows handles, filesystem writes, retry and rename behavior are real.

AI-assisted (OpenAI Codex).

@RAINighty

Copy link
Copy Markdown

Tested 17937be71 merged with main 6312c057f (clean merge) on Windows 11 (10.0.26300), Python 3.13.15 in UTF-8 mode (PYTHONUTF8=1; venv with the pinned tests/requirements.txt), with llvm-mc 21.1.8 (AMDGPU) and SPIRV-Tools v2026.4 on PATH: pass.

  • tests/test_tex_headers.py with this PR: passes (Ran 78 tests in 0.963s, OK). On main: passes (Ran 77 tests in 0.729s, OK).
  • Negative control: this PR's test with main's version of tools/tex_headers.py fails (Ran 78 tests in 0.712s, FAILED (errors=1)), so the new test does catch the problem.
  • Code not reviewed beyond running it. No GPU or game files.

AI-assisted (Claude Code).

@AsgerCode

Copy link
Copy Markdown

Tested 17937be on Windows 11 (NTFS), Python 3.12.10 (CPU only, no GPU, no game files): pass

What I ran:

  • python -I tests/test_tex_headers.py: 78 tests, OK. The other test files that use tex_headers: test_tex_headers_report_paths.py 8 OK (1 skipped), test_calibrated_texture.py 2 OK (1 skipped), test_calibrated_texture_extra.py 25 OK, test_ttpf_assets.py 31 OK (3 skipped), test_ttpf_mips.py 93 OK. pytest tests/test_tex_export.py: 2 failures, which also fail on main (the dds_png preview test that open test_tex_export: use R32_UINT as the format dds_png cannot preview (red since #6758) #7239 fixes).
  • Negative control with a behaviour check: I kept the PR's tool but replaced the body of write_json_report with main's direct open(path, "w") writer. The new test fails with AssertionError: OSError not raised, so it pins the staging and not only the new import.
  • Merge with current main (867d922): clean, the PR is based on it.

The real CLI, with a synthetic carve (a/index.json with {"files": []} and one 16.bin) and an existing --out report:

Case PR main
Read-only existing --out exit 2, one ERROR: cannot write ...: Permission denied line, report kept, no .tmp left the same
json.dump writes part of the data, then raises OSError 28 exit 2, one ERROR line, report kept, no .tmp exit 2, report truncated
KeyboardInterrupt during json.dump traceback (as before), report kept, no .tmp report truncated
--out is a folder error: ... is a directory, exit 2 the same
Parent of --out is a file exit 2, one ERROR: cannot write line the same
--out is index.json, a .bin input, a hard link to index.json, or the 8.3 short name of index.json refused with output would overwrite an input, input unchanged the same
--out is the 8.3 short name of an existing report the long-name file is updated, no .tmp n/a
Normal write bytes are identical to main's json.dump(allt, f) n/a

The read-only case is safe on Windows for two reasons. The O_WRONLY probe refuses the file before any staging file exists. Also, the os.chmod(staged, published_mode(...)) call runs only when os.name != "nt", so the staging file never gets the read-only attribute. The case where a read-only staging file stays behind after a failed os.replace (fixed in tools/bvh_index.py with os.chmod(temp, 0o600) before the unlink) cannot happen here.

Nits, none blocking:

  1. The same writer (about 30 lines, only the temp prefix and the json.dump arguments differ) is in anim_index: stage JSON out so a write failure keeps the previous report #8826, gamedata_sps1: stage --json so a write failure keeps the previous report #8833, exe_checklist: stage --out so a write failure keeps the previous report #8905, d3d12_guid_sites: stage --json so a write failure keeps the previous report #9004, skin_streams: stage --json so a write failure keeps the previous report #9196, psin_psch: stage --json so a write failure keeps the previous report #9203, veh_spawn_intensities: stage --json so a write failure keeps the previous report #9210, turing_atlas: stage --out so a write failure keeps the previous report #9214, xcl_compare: stage --out so a write failure keeps the previous report #9219 and this PR. tools/ has no public helper for this. The nearest is the private bvh_index._publish(dest, write, newline), which bvh_geometry_check.py already imports. One helper next to file_mode.published_mode would give these tools one tested copy.

Overlap with open PRs that change tools/tex_headers.py:

AI-assisted (Claude Code).

@sds-228 sds-228 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The staged JSON write preserves the previous report on failure, while non-regular destinations keep the direct-write path. The focused suite passes 78/78 at head and 77/77 at base; the new test fails against base source and also fails when the old direct-write logic is substituted back in, confirming it is behaviorally discriminating. No game data or local paths are involved.

AI-assisted (OpenCode).

@sayak-brm sayak-brm left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. I read the diff, tools/file_mode.py and tools/replace_retry.py in main, and the existing --out tests.

Ran on Windows (merged onto a recent main, shared venv, scrubbed environment): python -I tests/test_tex_headers.py gives 78 tests OK. With only tools/tex_headers.py put back to main and the PR's tests kept, test_out_write_failure_preserves_previous_r... errors, so the new test does guard the change.

The staging logic looks right to me: the probe does not truncate, the temp file sits beside the resolved destination, and it is removed in the finally block.

Three small things:

  1. CONTRIBUTING.md asks for the claim ID in the pull request ("claiming: ", plus "Depends on:" where it applies). The description has neither.
  2. The new test covers the failure path only. It would be worth asserting that no .tex_headers-*.tmp file is left in the folder after the failed write.
  3. Overlap: #3572, #5981 and #7973 also change the --out handling in tools/tex_headers.py, and this PR moves the os.makedirs call out of main(). Whichever of these lands second will need a rebase in main(). Worth coordinating in the claims issue.

Review prepared with Claude Code.

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.

7 participants