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

Open
github-bot wants to merge 2 commits from github-bot/gh-2075 into main AGit
Owner

@⁠KushGene on GitHub · 2026-11-25 19:36 UTC · original
KushGene:fix/hashdict-atomic-json-path-guard → main · +144 −17 · 2 files · 2 commits · open

Claimed in the coordination issue as: HASHDICT-ATOMIC-JSON-AND-PATH-GUARD (#1608)

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)
**@⁠KushGene** on GitHub · 2026-11-25 19:36 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2075) `KushGene:fix/hashdict-atomic-json-path-guard` → `main` · +144 −17 · 2 files · 2 commits · open Claimed in the coordination issue as: HASHDICT-ATOMIC-JSON-AND-PATH-GUARD (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608) ### 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)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:16:58 +00:00
Author
Owner

@⁠DeadlyChirp on GitHub · 2026-11-26 10:02 UTC · original · edited 2026-11-26 10:30 UTC

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.)

**@⁠DeadlyChirp** on GitHub · 2026-11-26 10:02 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2075#issuecomment-7188837142) · edited 2026-11-26 10:30 UTC 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.)
Author
Owner

@⁠KushGene on GitHub · 2026-11-26 10:14 UTC · original

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.

**@⁠KushGene** on GitHub · 2026-11-26 10:14 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2075#issuecomment-7189095394) 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.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2075/head:github-bot/gh-2075'
git switch 'github-bot/gh-2075'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2075'
git switch 'github-bot/gh-2075'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2075'
git switch 'github-bot/gh-2075'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2075'
git switch 'main'
git merge --squash 'github-bot/gh-2075'
git switch 'main'
git merge --ff-only 'github-bot/gh-2075'
git switch 'main'
git merge 'github-bot/gh-2075'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.