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

Open
opened 2026-11-26 21:19:58 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2075
Author: @KushGene
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/hashdict-atomic-json-path-guard


📝 Commits (2)

  • 1ca436c tools/hashdict: write --out atomically, use shared report_paths guard, and accept UTF-8 BOM in --lookup (HASHDICT-ATOMIC-JSON-AND-PATH-GUARD)
  • 48b2141 tests/test_hashdict: check directory entries instead of alias existence on folding volumes

📊 Changes

2 files changed (+144 additions, -17 deletions)

View changed files

📝 tests/test_hashdict.py (+87 -0)
📝 tools/hashdict.py (+57 -17)

📄 Description

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)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/2075 **Author:** [@KushGene](https://github.com/KushGene) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/hashdict-atomic-json-path-guard` --- ### 📝 Commits (2) - [`1ca436c`](https://github.com/ubiquitarianism/leonida/commit/1ca436c86d890ce70d81ada5ac8a01861fcce9bc) tools/hashdict: write --out atomically, use shared report_paths guard, and accept UTF-8 BOM in --lookup (HASHDICT-ATOMIC-JSON-AND-PATH-GUARD) - [`48b2141`](https://github.com/ubiquitarianism/leonida/commit/48b2141a29defc80b3806eaac8c7fa803574c8a6) tests/test_hashdict: check directory entries instead of alias existence on folding volumes ### 📊 Changes **2 files changed** (+144 additions, -17 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_hashdict.py` (+87 -0) 📝 `tools/hashdict.py` (+57 -17) </details> ### 📄 Description 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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#2081
No description provided.