tools/ds_permute_probe: write --json report atomically (carry of #2000) #3090

Merged
MoonEyes merged 4 commits from upstream-pr/3090 into main 2026-12-02 13:26:02 +00:00
MoonEyes commented 2026-12-02 10:30:25 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T10:30:25Z.


Claimed in the coordination issue as: DS-PERMUTE-PROBE-ATOMIC-JSON (the original author's item ID, claimed at https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7170145014)

Depends on: #2000

What and why

Carry of #2000 (Ch1mpleo), unchanged since 2026-11-25. Branch = main + the author's head (commit kept) + conflict resolution; I added no commit of my own. #2000 conflicts with main in tests/test_ds_permute_probe.py (add/add: main's test_oracle_closed_on_success_and_failure landed where the PR adds its tests), and the review on #2000 asks for a rebase.

The change: write_json_report in tools/ds_permute_probe.py now writes the --json report to a temporary file in the same directory (UTF-8, LF line endings) and moves it into place with os.replace, removing the temporary file on failure. A failed write therefore leaves an existing report untouched instead of truncating it. The PR also adapts the OSError test and adds test_atomic_json_write_and_replace_failure_preserves_existing.

Conflict resolution: kept both sides' tests in tests/test_ds_permute_probe.py. tools/ds_permute_probe.py merged without conflict. No other review defect was raised on #2000 (the only review asks for a rebase), so there is no review-fix commit.

Tested

Windows 11, Python 3.14.0, no GPU, no game files, on main a917c54d6 plus this branch.

  • python -I tests/test_ds_permute_probe.py: Ran 9 tests, OK.
  • Red: with tools/ds_permute_probe.py restored to main's version and the branch's tests kept, the same command gives FAILED (failures=1, errors=1): test_atomic_json_write_and_replace_failure_preserves_existing fails and test_write_json_report_and_main_catch_oserror_cleanly errors. Restored afterwards, working tree clean.
  • python -I scripts/check_no_game_data.py tools/ds_permute_probe.py tests/test_ds_permute_probe.py: exit 0.

Not run: the full suite, CI.

GPUs tested: n/a (unit tests only)

AI-assisted: yes. Models: Claude Opus 5.5 in Claude Code (carry, conflict resolution, fixes, tests); separate read-only reviewer agent: Claude Opus 5.5.

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)

Supersedes #2000 once merged; the original stays open until a maintainer decides.

🤖 Generated with Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5604607252 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3090). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-02T10:30:25Z. --- Claimed in the coordination issue as: DS-PERMUTE-PROBE-ATOMIC-JSON (the original author's item ID, claimed at https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7170145014) Depends on: #2000 ### What and why Carry of #2000 (Ch1mpleo), unchanged since 2026-11-25. Branch = main + the author's head (commit kept) + conflict resolution; I added no commit of my own. #2000 conflicts with main in `tests/test_ds_permute_probe.py` (add/add: main's `test_oracle_closed_on_success_and_failure` landed where the PR adds its tests), and the review on #2000 asks for a rebase. The change: `write_json_report` in `tools/ds_permute_probe.py` now writes the `--json` report to a temporary file in the same directory (UTF-8, LF line endings) and moves it into place with `os.replace`, removing the temporary file on failure. A failed write therefore leaves an existing report untouched instead of truncating it. The PR also adapts the OSError test and adds `test_atomic_json_write_and_replace_failure_preserves_existing`. Conflict resolution: kept both sides' tests in `tests/test_ds_permute_probe.py`. `tools/ds_permute_probe.py` merged without conflict. No other review defect was raised on #2000 (the only review asks for a rebase), so there is no review-fix commit. ### Tested Windows 11, Python 3.14.0, no GPU, no game files, on main a917c54d6 plus this branch. - `python -I tests/test_ds_permute_probe.py`: Ran 9 tests, OK. - Red: with `tools/ds_permute_probe.py` restored to main's version and the branch's tests kept, the same command gives `FAILED (failures=1, errors=1)`: `test_atomic_json_write_and_replace_failure_preserves_existing` fails and `test_write_json_report_and_main_catch_oserror_cleanly` errors. Restored afterwards, working tree clean. - `python -I scripts/check_no_game_data.py tools/ds_permute_probe.py tests/test_ds_permute_probe.py`: exit 0. Not run: the full suite, CI. GPUs tested: n/a (unit tests only) AI-assisted: yes. Models: Claude Opus 5.5 in Claude Code (carry, conflict resolution, fixes, tests); separate read-only reviewer agent: Claude Opus 5.5. ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a: no translator change) Supersedes #2000 once merged; the original stays open until a maintainer decides. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Kal0s-design commented 2026-12-02 10:58:31 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-12-02T10:58:31Z.


Windows 11, Python 3.13, head 582a4dfaf merged into current main: the merge is clean. python -I tests/test_ds_permute_probe.py: 9 tests, OK.

One Windows behaviour changes, and it's worth knowing before merge. I wrote a report over an existing one while another handle in the same process held it open for reading, the way a viewer or editor that keeps the file open does. Python's open() on Windows doesn't share delete access.

main's write_json_report this PR
return None (written) cannot write --json ...: Access is denied
report afterwards the new content the old content, untouched
files left in the folder report.json report.json (the temporary file was removed)

So the PR does what it says: a failed write keeps the old report and leaves nothing behind. On Windows, though, "report open somewhere" becomes a failure where it used to overwrite. os.replace can't replace a file held open without FILE_SHARE_DELETE, while the old truncating open(path, "w") could. That's a fair trade for atomicity, and the message is clear. Maybe say it in the tool's help, or retry once.

GPUs tested: n/a (CPU unit tests)

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7343246178 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3090#issuecomment-7343246178). Originally posted by [Kal0s-design](https://github.com/Kal0s-design) on 2026-12-02T10:58:31Z. --- Windows 11, Python 3.13, head `582a4dfaf` merged into current `main`: the merge is clean. `python -I tests/test_ds_permute_probe.py`: 9 tests, OK. One Windows behaviour changes, and it's worth knowing before merge. I wrote a report over an existing one while another handle in the same process held it open for reading, the way a viewer or editor that keeps the file open does. Python's `open()` on Windows doesn't share delete access. | | `main`'s `write_json_report` | this PR | |---|---|---| | return | `None` (written) | `cannot write --json ...: Access is denied` | | report afterwards | the new content | the old content, untouched | | files left in the folder | `report.json` | `report.json` (the temporary file was removed) | So the PR does what it says: a failed write keeps the old report and leaves nothing behind. On Windows, though, "report open somewhere" becomes a failure where it used to overwrite. `os.replace` can't replace a file held open without `FILE_SHARE_DELETE`, while the old truncating `open(path, "w")` could. That's a fair trade for atomicity, and the message is clear. Maybe say it in the tool's help, or retry once. GPUs tested: n/a (CPU unit tests) AI-assisted: yes (Claude Code).
MoonEyes commented 2026-12-02 11:15:28 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T11:15:28Z.


Thanks for measuring the Windows case. Addressed in 70caa0dce: the rename now goes through replace_retry, as the other atomic writers in tools/ do, so a short hold without share-delete (scanner, indexer, sync client) is ridden out for up to 5 s. A hold that lasts, like your open handle, still fails, keeps the old report and leaves no temporary file, and the message now adds "is the report open in another program? close it and run again". Two tests cover a hold that clears after one try and one that does not; both fail with the previous tool. python -I tests/test_ds_permute_probe.py: 11 tests OK.

AI-assisted (Claude Code, Claude Opus 5.5)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7343654898 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3090#issuecomment-7343654898). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-02T11:15:28Z. --- Thanks for measuring the Windows case. Addressed in 70caa0dce: the rename now goes through `replace_retry`, as the other atomic writers in `tools/` do, so a short hold without share-delete (scanner, indexer, sync client) is ridden out for up to 5 s. A hold that lasts, like your open handle, still fails, keeps the old report and leaves no temporary file, and the message now adds "is the report open in another program? close it and run again". Two tests cover a hold that clears after one try and one that does not; both fail with the previous tool. `python -I tests/test_ds_permute_probe.py`: 11 tests OK. AI-assisted (Claude Code, Claude Opus 5.5)
enlight3d commented 2026-12-02 13:06:44 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by enlight3d on 2026-12-02T13:06:44Z.


RUN (independent run on Windows, #3090 head 70caa0dce, merged locally into main 23a51c494 with git merge --no-ff, no conflicts. CI on the head: change-scope, check, linux-python-and-viewer, no-game-data and semantic-tests all pass.)

  • History: #2000's head 24ff6697c (Ch1mpleo) is an ancestor of the head through the merge 33d5f8602, under its author's name. The body still says "I added no commit of my own", but the head is 70caa0dce, the carrier's replace_retry commit made after Kal0s-design's comment. The counts below are for that head.
  • python -I tests/test_ds_permute_probe.py -v: main 8 tests OK; merge 11 tests OK. python -m pytest on the same file (the only one that names ds_permute_probe) gives the same counts: main 8 passed; merge 11 passed.
  • Red: main's tools/ds_permute_probe.py with the head's test file gives 11 tests, 2 failures and 2 errors:
    • The two errors: main has no tempfile or replace_retry to patch.
    • test_atomic_json_write_and_replace_failure_preserves_existing fails: main reports no error, because it writes in place.
    • test_write_json_report_rides_out_a_short_windows_hold fails (0 != 2 replace calls).
  • Native probe on NTFS, with a real second handle opened with Python's open() for reading (no share-delete), held while write_json_report rewrites the report:
    • main: the write succeeds while the handle is open and the report gets the new content. That is the in-place truncation Kal0s-design measured.
    • Merge, handle held throughout: after 5.0 s of retries it returns "cannot write --json ...: Access is denied (is the report open in another program? close it and run again)". The old report is unchanged and no temporary file is left.
    • Merge, handle closed after 1 s: the write succeeds after 1.1 s with the new content. So replace_retry rides out a short hold for real, not only under the mock.
  • Mutations of the merge's tools/ds_permute_probe.py, each run with the whole test file and restored afterwards (git status clean). 4 of 5 are killed:
    • replace_retry replaced by a plain os.replace: killed (2 tests).
    • Temporary file not removed on failure: killed (2 tests).
    • Hint about the open report dropped: killed.
    • CRLF line endings: killed by main's LF test.
    • Survives: the temporary file created in the system temp folder (dir=None) instead of next to the report. On the same volume the rename still works, but across volumes os.replace would fail. The tests' folders sit on the system temp volume, so nothing pins "next to the report".

Not covered here: Linux and macOS (no winerror there, so the hint and the retry do not apply), and hw_oracle runs (mocked, as in the existing tests).

Verdict: PASS. The report is now replaced atomically, a short Windows hold is ridden out and a long one is named, which I checked natively and not only under mocks. Red on main and green on the merge hold, and the author's commit is kept. The body's "no commit of my own" is out of date for this head.

Machine: Windows 11 Pro 26300 (NTFS), i9-13900K, 64 GB, CPython 3.13.12 (CI uses 3.12); unit tests only, no GPU or game files used.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7346366750 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3090#issuecomment-7346366750). Originally posted by [enlight3d](https://github.com/enlight3d) on 2026-12-02T13:06:44Z. --- RUN (independent run on Windows, #3090 head 70caa0dce, merged locally into `main` 23a51c494 with `git merge --no-ff`, no conflicts. CI on the head: change-scope, check, linux-python-and-viewer, no-game-data and semantic-tests all pass.) - History: #2000's head 24ff6697c (Ch1mpleo) is an ancestor of the head through the merge 33d5f8602, under its author's name. The body still says "I added no commit of my own", but the head is 70caa0dce, the carrier's `replace_retry` commit made after Kal0s-design's comment. The counts below are for that head. - `python -I tests/test_ds_permute_probe.py -v`: main 8 tests OK; merge 11 tests OK. `python -m pytest` on the same file (the only one that names `ds_permute_probe`) gives the same counts: main 8 passed; merge 11 passed. - Red: `main`'s `tools/ds_permute_probe.py` with the head's test file gives 11 tests, 2 failures and 2 errors: - The two errors: `main` has no `tempfile` or `replace_retry` to patch. - `test_atomic_json_write_and_replace_failure_preserves_existing` fails: `main` reports no error, because it writes in place. - `test_write_json_report_rides_out_a_short_windows_hold` fails (`0 != 2` replace calls). - Native probe on NTFS, with a real second handle opened with Python's `open()` for reading (no share-delete), held while `write_json_report` rewrites the report: - `main`: the write succeeds while the handle is open and the report gets the new content. That is the in-place truncation Kal0s-design measured. - Merge, handle held throughout: after 5.0 s of retries it returns "cannot write --json ...: Access is denied (is the report open in another program? close it and run again)". The old report is unchanged and no temporary file is left. - Merge, handle closed after 1 s: the write succeeds after 1.1 s with the new content. So `replace_retry` rides out a short hold for real, not only under the mock. - Mutations of the merge's `tools/ds_permute_probe.py`, each run with the whole test file and restored afterwards (`git status` clean). 4 of 5 are killed: - `replace_retry` replaced by a plain `os.replace`: killed (2 tests). - Temporary file not removed on failure: killed (2 tests). - Hint about the open report dropped: killed. - CRLF line endings: killed by `main`'s LF test. - Survives: the temporary file created in the system temp folder (`dir=None`) instead of next to the report. On the same volume the rename still works, but across volumes `os.replace` would fail. The tests' folders sit on the system temp volume, so nothing pins "next to the report". Not covered here: Linux and macOS (no `winerror` there, so the hint and the retry do not apply), and hw_oracle runs (mocked, as in the existing tests). Verdict: PASS. The report is now replaced atomically, a short Windows hold is ridden out and a long one is named, which I checked natively and not only under mocks. Red on `main` and green on the merge hold, and the author's commit is kept. The body's "no commit of my own" is out of date for this head. Machine: Windows 11 Pro 26300 (NTFS), i9-13900K, 64 GB, CPython 3.13.12 (CI uses 3.12); unit tests only, no GPU or game files used.
MoonEyes commented 2026-12-02 13:13:27 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T13:13:27Z.


Merging. Head 70caa0dce merges cleanly with main bf7886746 and does not force a rebase of #2566. Independent check on head 70caa0dce: enlight3d's red/green on main + this head (2 failures and 2 errors with main's tool, green 11/11, native NTFS probe with a real open handle: main overwrites in place, the merge returns the error with the hint after 5 s and keeps the old report; 4 of 5 mutations killed), comment 7346366750; Ch1mpleo's commit from #2000 is kept; CI green incl. no-game-data. Merged by MoonEyes.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7346539287 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3090#issuecomment-7346539287). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-02T13:13:27Z. --- Merging. Head `70caa0dce` merges cleanly with `main` `bf7886746` and does not force a rebase of #2566. Independent check on head 70caa0dce: enlight3d's red/green on main + this head (2 failures and 2 errors with main's tool, green 11/11, native NTFS probe with a real open handle: main overwrites in place, the merge returns the error with the hint after 5 s and keeps the old report; 4 of 5 mutations killed), comment 7346366750; Ch1mpleo's commit from #2000 is kept; CI green incl. no-game-data. Merged by MoonEyes. AI-assisted (Claude Code).
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
ProjectLeonida/mandy!3090
No description provided.