tools/awc_markers: guard --out paths with report_paths and write JSON atomically (AWC-MARKERS-ATOMIC-OUT-AND-PATH-GUARD) #2078

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

@⁠KushGene on GitHub · 2026-11-25 19:40 UTC · original
KushGene:fix/awc-markers-atomic-out-guard → main · +206 −12 · 2 files · 2 commits · open

Claimed in the coordination issue as: AWC-MARKERS-ATOMIC-OUT-AND-PATH-GUARD (#1608)

What and why

Hardens tools/awc_markers.py --out handling without touching iter_banks() or _read() (keeping zero overlap with #1159):

  • Early --out validation & input overwrite guard: Refuses empty/whitespace-only and directory --out paths before scanning, and checks discovered inputs with report_paths.same_report_path (catching case-only aliases on case-folding volumes even when --out does not exist yet).
  • Atomic JSON writes & error handling: _write_json_atomic creates missing parent directories, writes via a sibling temporary file (tempfile.mkstemp(..., prefix=".awc_markers-", suffix=".json")), preserves permissions with file_mode.published_mode, swaps via replace_retry.replace, cleans up the temp file on failure, and catches OSError to print a clean awc_markers: error: cannot write ... line and exit 1 instead of raising a traceback or truncating an existing report. Output formatting (indent=2, trailing \n, LF) is unchanged.
  • Regression tests: Added 4 tests in OutputSafetyTest in tests/test_awc_markers.py.

Tested

  • Confirmed all 4 new OutputSafetyTest methods in tests/test_awc_markers.py failed against main (e18b59a5b) before the fix (--out "" / " " exited 0 silently, directory --out and missing parent raised tracebacks, --out overwriting input exited 0, and failed write left existing report broken).
  • python3 -I tests/test_awc_markers.py: 15/15 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/awc_markers.py tests/test_awc_markers.py: exit 0.
  • python3 -m compileall -q make_game.py tools tests: exit 0.

GPUs tested: n/a (CPU-only Python tool and synthetic 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:40 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2078) `KushGene:fix/awc-markers-atomic-out-guard` → `main` · +206 −12 · 2 files · 2 commits · open Claimed in the coordination issue as: AWC-MARKERS-ATOMIC-OUT-AND-PATH-GUARD (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608) ### What and why Hardens [`tools/awc_markers.py`](tools/awc_markers.py) `--out` handling without touching `iter_banks()` or `_read()` (keeping zero overlap with #1159): - **Early `--out` validation & input overwrite guard:** Refuses empty/whitespace-only and directory `--out` paths before scanning, and checks discovered inputs with `report_paths.same_report_path` (catching case-only aliases on case-folding volumes even when `--out` does not exist yet). - **Atomic JSON writes & error handling:** `_write_json_atomic` creates missing parent directories, writes via a sibling temporary file (`tempfile.mkstemp(..., prefix=".awc_markers-", suffix=".json")`), preserves permissions with `file_mode.published_mode`, swaps via `replace_retry.replace`, cleans up the temp file on failure, and catches `OSError` to print a clean `awc_markers: error: cannot write ...` line and exit `1` instead of raising a traceback or truncating an existing report. Output formatting (`indent=2`, trailing `\n`, LF) is unchanged. - **Regression tests:** Added 4 tests in `OutputSafetyTest` in [`tests/test_awc_markers.py`](tests/test_awc_markers.py). ### Tested - Confirmed all 4 new `OutputSafetyTest` methods in `tests/test_awc_markers.py` failed against `main` (`e18b59a5b`) before the fix (`--out ""` / `" "` exited 0 silently, directory `--out` and missing parent raised tracebacks, `--out` overwriting input exited 0, and failed write left existing report broken). - `python3 -I tests/test_awc_markers.py`: 15/15 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/awc_markers.py tests/test_awc_markers.py`: exit 0. - `python3 -m compileall -q make_game.py tools tests`: exit 0. GPUs tested: n/a (CPU-only Python tool and synthetic 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:17:04 +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_awc_markers.py: 1 failed, 14 passed, 8 subtests passed. Reproduced twice.
  • Failing test: OutputSafetyTest::test_out_refuses_input_file_and_case_only_alias_on_folding_volume, at self.assertFalse(os.path.exists(alias)) (AssertionError: True is not false).
  • Cause: NTFS is case-insensitive, so banks/A.AWC already "exists" because banks/a.awc exists. The tool correctly refused with exit code 2 and "overwrite", and the input file was unchanged. Only the final existence check assumes a case-sensitive host filesystem. Checking that the input bytes are unchanged (already done next), or skipping the exists check when os.path.exists(alias) was already true before the call, would make it pass on Windows.

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

**@⁠DeadlyChirp** on GitHub · 2026-11-26 10:02 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2078#issuecomment-7188832376) · 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_awc_markers.py`: 1 failed, 14 passed, 8 subtests passed. Reproduced twice. - Failing test: `OutputSafetyTest::test_out_refuses_input_file_and_case_only_alias_on_folding_volume`, at `self.assertFalse(os.path.exists(alias))` (`AssertionError: True is not false`). - Cause: NTFS is case-insensitive, so `banks/A.AWC` already "exists" because `banks/a.awc` exists. The tool correctly refused with exit code 2 and "overwrite", and the input file was unchanged. Only the final existence check assumes a case-sensitive host filesystem. Checking that the input bytes are unchanged (already done next), or skipping the `exists` check when `os.path.exists(alias)` was already true before the call, would make it pass on Windows. (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 58bda6a72 by checking os.listdir(banks) == ["a.awc"] 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/2078#issuecomment-7189097847) Thanks for testing on Windows, @⁠DeadlyChirp! Fixed in 58bda6a72 by checking `os.listdir(banks) == ["a.awc"]` 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/2078/head:github-bot/gh-2078'
git switch 'github-bot/gh-2078'

Merge

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