tools/hashdict: write --out atomically, use shared report_paths guard, and accept UTF-8 BOM in --lookup (HASHDICT-ATOMIC-JSON-AND-PATH-GUARD) #2075

Merged
KushGene merged 2 commits from upstream-pr/2075 into main 2026-11-28 10:47:30 +00:00
KushGene commented 2026-11-25 19:36:48 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by KushGene on 2026-11-25T19:36:48Z.


Claimed in the coordination issue as: HASHDICT-ATOMIC-JSON-AND-PATH-GUARD (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7172506299)

What and why

Hardens tools/hashdict.py output handling and --lookup input decoding without changing output bytes:

  • Atomic --out writes: Writes JSON through a sibling temporary file (tempfile.mkstemp(..., prefix=".hashdict-", suffix=".json")), preserves permissions with file_mode.published_mode, and swaps via replace_retry.replace, cleaning up the temp file on failure so an interrupted or failing write no longer truncates an existing dictionary.
  • Early --out validation & shared path guard: Refuses empty/whitespace-only and directory --out paths before scanning inputs or creating parent directories, and checks --extra and index.json inputs with report_paths.same_report_path (catching case-only aliases on case-folding volumes even when --out does not exist yet).
  • UTF-8 BOM in --lookup: Opens --lookup dictionaries with utf-8-sig (matching the existing index.json reader in hashdict.py), so dictionaries saved with a BOM by Windows PowerShell 5.1 / Notepad are accepted cleanly.
  • Regression tests: Added 4 tests in tests/test_hashdict.py (test_write_failure_preserves_existing_output_and_leaves_no_temp_file, test_out_rejects_empty_and_directory_paths, test_out_refuses_case_only_input_alias_on_folding_volume, test_lookup_accepts_utf8_bom_dictionary).

Tested

  • Confirmed all 4 new test methods in tests/test_hashdict.py failed against main (e18b59a5b) before the fix (existing out truncated to b'' on json.dump error, whitespace --out accepted, case-only alias written, UTF-8 BOM rejected).
  • python3 -I tests/test_hashdict.py: 47/47 passed.
  • python3 -I tests/test_text_write_line_endings.py: 3/3 passed.
  • python3 -I tests/test_report_paths.py: 22/22 passed.
  • python3 -I scripts/check_no_game_data.py tools/hashdict.py tests/test_hashdict.py: exit 0.
  • python3 -m compileall -q make_game.py tools tests: exit 0.

GPUs tested: n/a (CPU-only Python tool and unit tests on Fedora Linux 45)

AI-assisted: yes (Antigravity)

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)
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5497889992 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2075). Originally posted by [KushGene](https://github.com/KushGene) on 2026-11-25T19:36:48Z. --- Claimed in the coordination issue as: HASHDICT-ATOMIC-JSON-AND-PATH-GUARD (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7172506299) ### What and why Hardens [`tools/hashdict.py`](tools/hashdict.py) output handling and `--lookup` input decoding without changing output bytes: - **Atomic `--out` writes:** Writes JSON through a sibling temporary file (`tempfile.mkstemp(..., prefix=".hashdict-", suffix=".json")`), preserves permissions with `file_mode.published_mode`, and swaps via `replace_retry.replace`, cleaning up the temp file on failure so an interrupted or failing write no longer truncates an existing dictionary. - **Early `--out` validation & shared path guard:** Refuses empty/whitespace-only and directory `--out` paths before scanning inputs or creating parent directories, and checks `--extra` and `index.json` inputs with `report_paths.same_report_path` (catching case-only aliases on case-folding volumes even when `--out` does not exist yet). - **UTF-8 BOM in `--lookup`:** Opens `--lookup` dictionaries with `utf-8-sig` (matching the existing `index.json` reader in `hashdict.py`), so dictionaries saved with a BOM by Windows PowerShell 5.1 / Notepad are accepted cleanly. - **Regression tests:** Added 4 tests in [`tests/test_hashdict.py`](tests/test_hashdict.py) (`test_write_failure_preserves_existing_output_and_leaves_no_temp_file`, `test_out_rejects_empty_and_directory_paths`, `test_out_refuses_case_only_input_alias_on_folding_volume`, `test_lookup_accepts_utf8_bom_dictionary`). ### Tested - Confirmed all 4 new test methods in `tests/test_hashdict.py` failed against `main` (`e18b59a5b`) before the fix (existing `out` truncated to `b''` on `json.dump` error, whitespace `--out` accepted, case-only alias written, UTF-8 BOM rejected). - `python3 -I tests/test_hashdict.py`: 47/47 passed. - `python3 -I tests/test_text_write_line_endings.py`: 3/3 passed. - `python3 -I tests/test_report_paths.py`: 22/22 passed. - `python3 -I scripts/check_no_game_data.py tools/hashdict.py tests/test_hashdict.py`: exit 0. - `python3 -m compileall -q make_game.py tools tests`: exit 0. GPUs tested: n/a (CPU-only Python tool and unit tests on Fedora Linux 45) AI-assisted: yes (Antigravity) ### 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)
DeadlyChirp commented 2026-11-26 10:02:53 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by DeadlyChirp on 2026-11-26T10:02:53Z.


Tested on Windows 11, Python 3.12.10, with this PR merged onto current upstream main.

  • tests/test_hashdict.py: 1 failed, 45 passed, 1 skipped, 22 subtests passed. Reproduced twice. The skip is the symlink case (Developer Mode is off, WinError 1314).
  • Failing test: OutputPathTest::test_out_refuses_case_only_input_alias_on_folding_volume (tests/test_hashdict.py:819, AssertionError: True is not false).
  • Cause: the same pattern as #2078. NTFS is case-insensitive, so the case-only alias path already "exists" because the input file exists, and an assertFalse(os.path.exists(alias)) style check fails even though the tool refused the write. The check needs to allow a case-insensitive host filesystem.

(AI-assisted test run; output checked by hand.)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7188837142 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2075#issuecomment-7188837142). Originally posted by [DeadlyChirp](https://github.com/DeadlyChirp) on 2026-11-26T10:02:53Z. --- Tested on Windows 11, Python 3.12.10, with this PR merged onto current upstream main. - `tests/test_hashdict.py`: 1 failed, 45 passed, 1 skipped, 22 subtests passed. Reproduced twice. The skip is the symlink case (Developer Mode is off, WinError 1314). - Failing test: `OutputPathTest::test_out_refuses_case_only_input_alias_on_folding_volume` (`tests/test_hashdict.py:819`, `AssertionError: True is not false`). - Cause: the same pattern as #2078. NTFS is case-insensitive, so the case-only alias path already "exists" because the input file exists, and an `assertFalse(os.path.exists(alias))` style check fails even though the tool refused the write. The check needs to allow a case-insensitive host filesystem. (AI-assisted test run; output checked by hand.)
KushGene commented 2026-11-26 10:14:25 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by KushGene on 2026-11-26T10:14:25Z.


Thanks for testing on Windows, @DeadlyChirp! Fixed in 48b2141a2 by checking os.listdir(os.path.join(carve, "common")) == ["index.json"] instead of assertFalse(os.path.exists(alias)) so the assertion holds on both case-sensitive and case-insensitive (case-preserving) filesystems.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7189095394 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2075#issuecomment-7189095394). Originally posted by [KushGene](https://github.com/KushGene) on 2026-11-26T10:14:25Z. --- Thanks for testing on Windows, @DeadlyChirp! Fixed in 48b2141a2 by checking `os.listdir(os.path.join(carve, "common")) == ["index.json"]` instead of `assertFalse(os.path.exists(alias))` so the assertion holds on both case-sensitive and case-insensitive (case-preserving) filesystems.
sodove commented 2026-11-28 10:12:10 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by sodove on 2026-11-28T10:12:10Z.


#2075 | head 48b2141a29defc80b3806eaac8c7fa803574c8a6 | python3 -I tests/test_hashdict.py -v | red: 47 run, 4 failures, 1 error | green: 47 passed | Red → green

Baseline checks failed on the JSON input/output cases added to this file.

Pinned main 33db04a7010e81c40a7a6a15624525aa0c4a3a58, merged into this PR head. Ubuntu 26.04.1 (WSL2), Python 3.14.4. Full suite: not run. AI-assisted: OpenAI Codex. Batch claim.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7237740931 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2075#issuecomment-7237740931). Originally posted by [sodove](https://github.com/sodove) on 2026-11-28T10:12:10Z. --- #2075 | head `48b2141a29defc80b3806eaac8c7fa803574c8a6` | `python3 -I tests/test_hashdict.py -v` | red: 47 run, 4 failures, 1 error | green: 47 passed | **Red → green** Baseline checks failed on the JSON input/output cases added to this file. Pinned main `33db04a7010e81c40a7a6a15624525aa0c4a3a58`, merged into this PR head. Ubuntu 26.04.1 (WSL2), Python 3.14.4. Full suite: not run. AI-assisted: OpenAI Codex. [Batch claim](https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7237628300).
DadaCap commented 2026-11-28 10:47:33 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by DadaCap on 2026-11-28T10:47:33Z.


Merged, thank you @KushGene!

What it does: tools/hashdict: write --out atomically, use shared report_paths guard, and accept UTF-8 BOM in --lookup (HASHDICT-ATOMIC-JSON-AND-PATH-GUARD)

How it was checked: sodove tested this PR at head 48b2141a2 on main 33db04a70: the PR's own test fails on main and passes with this PR merged in (his result is linked from the batch summary in #1608). We then re-ran the same test locally on Windows 11 and got the same verdict: on main, red (2 failures, 1 error); with the PR, green (47 OK, 1 skip).

Our count on main was 2 failures and 1 error (sodove's 4 and 1): one symlink/hard-link case is skipped on our machine; the verdict is the same.

CI: the red jobs on this PR fail the same way on main, or on the older main it was built on (the cs_dpp_cmp.asm translator fixture, since fixed on main, and the Windows runner's Visual Studio setup), so none comes from this change.

Merged one by one with 20 other PRs of the same reviewed batch, pinned to the tested head.

AI-assisted (Claude Code, Claude Opus 5.5), posted for the maintainers.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7238125094 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2075#issuecomment-7238125094). Originally posted by [DadaCap](https://github.com/DadaCap) on 2026-11-28T10:47:33Z. --- Merged, thank you @KushGene! **What it does:** tools/hashdict: write --out atomically, use shared report_paths guard, and accept UTF-8 BOM in --lookup (HASHDICT-ATOMIC-JSON-AND-PATH-GUARD) **How it was checked:** sodove tested this PR at head `48b2141a2` on main `33db04a70`: the PR's own test fails on main and passes with this PR merged in (his result is linked from the batch summary in #1608). We then re-ran the same test locally on Windows 11 and got the same verdict: on main, red (2 failures, 1 error); with the PR, green (47 OK, 1 skip). Our count on main was 2 failures and 1 error (sodove's 4 and 1): one symlink/hard-link case is skipped on our machine; the verdict is the same. **CI:** the red jobs on this PR fail the same way on main, or on the older main it was built on (the `cs_dpp_cmp.asm` translator fixture, since fixed on main, and the Windows runner's Visual Studio setup), so none comes from this change. Merged one by one with 20 other PRs of the same reviewed batch, pinned to the tested head. AI-assisted (Claude Code, Claude Opus 5.5), posted for the maintainers.
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!2075
No description provided.