op_reach: harden index parsing, report_paths guards, and atomic JSON I/O (OP-REACH-INDEX-AND-REPORT-GUARDS) #1556

Merged
leonida/mandy
Vemast merged 1 commit from fix/op-reach-index-and-report-guards into main 2026-11-28 13:40:38 +00:00

Pull request successfully merged and closed

Vemast commented 2026-11-25 04:28:33 +00:00

Claimed in the coordination issue as: OP-REACH-INDEX-AND-REPORT-GUARDS (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7232955159)

Depends on: none

What and why

Hardens tools/op_reach.py index parsing, directory scanning, CLI output guards, and JSON output writing:

  1. Carve index parsing (stages_of):
    • Reads --index with utf-8-sig (handling UTF-8 BOM) and converts RecursionError to ValueError.
    • Normalizes file entries via os.path.basename(raw_file.replace("\\", "/")) before taking the stem (matching fssm_header.stage_index), so directory-prefixed paths in index.json resolve to listing stems.
    • Validates stage is a string (or None) and strips whitespace instead of letting a non-string stage crash census() with TypeError.
    • Treats conflicting non-empty stages for the same stem as ambiguous ("unindexed") while keeping identical duplicate rows, matching fssm_header.stage_index.
  2. Non-file entries & duplicate CLI ops (census, waves_of, report):
    • Skips subdirectories named *.s or *.fssm in census() and waves_of() instead of failing when opening them as files.
    • Deduplicates requested ops/patterns in report() (preserving order) so passing multiple encodings of one instruction (e.g. v_cndmask_b32_e32 v_cndmask_b32_e64) emits one combined base-op row.
  3. Up-front --index / --json guards and atomic JSON output (_inside_dir, _validate_json_path, _write_json_atomic):
    • Rejects directory or empty/whitespace --index and --json paths (including --json "" and NUL bytes) and non-directory --json parents up front with exit code 2.
    • Uses report_paths.same_report_path to refuse --json targets that would overwrite --index, .s / .fssm inputs (including hard-link/symlink/case-folded aliases), or write anywhere inside disasm or --fssm.
    • Creates missing nested --json parent folders and publishes strict LF JSON (allow_nan=False, trailing \n) atomically via tempfile.mkstemp, file_mode.published_mode, and replace_retry.replace, cleaning up the staged temp file on failure.
  4. Tests (tests/test_op_reach.py):
    • Adds synthetic regression tests covering BOM/prefixed/ambiguous/non-string index.json, *.s/*.fssm subdirectories, op deduplication, --index and --json overwrite/containment guards (including hard links, symlinks, and mocked macOS case-insensitive aliases), nested atomic output, and temp-file cleanup on replace_retry.replace failure.

Tested

  • python -m compileall -q tools/op_reach.py tests/test_op_reach.py — clean
  • python -I -W error tests/test_op_reach.py -v — 18 tests, OK
  • python -I tests/test_text_write_line_endings.py — 3 tests, OK
  • python -I tests/test_report_paths.py — 22 tests, OK (2 skipped on unprivileged Windows)
  • python -I scripts/check_no_game_data.py tools/op_reach.py tests/test_op_reach.py — clean

GPUs tested: n/a (CPU-only Python tool and synthetic standard-library unit tests on Windows 11)

AI-assisted: yes

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. (No translator change.)
Claimed in the coordination issue as: OP-REACH-INDEX-AND-REPORT-GUARDS (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7232955159) Depends on: none ### What and why Hardens `tools/op_reach.py` index parsing, directory scanning, CLI output guards, and JSON output writing: 1. **Carve index parsing (`stages_of`)**: - Reads `--index` with `utf-8-sig` (handling UTF-8 BOM) and converts `RecursionError` to `ValueError`. - Normalizes `file` entries via `os.path.basename(raw_file.replace("\\", "/"))` before taking the stem (matching `fssm_header.stage_index`), so directory-prefixed paths in `index.json` resolve to listing stems. - Validates `stage` is a string (or `None`) and strips whitespace instead of letting a non-string `stage` crash `census()` with `TypeError`. - Treats conflicting non-empty stages for the same stem as ambiguous (`"unindexed"`) while keeping identical duplicate rows, matching `fssm_header.stage_index`. 2. **Non-file entries & duplicate CLI ops (`census`, `waves_of`, `report`)**: - Skips subdirectories named `*.s` or `*.fssm` in `census()` and `waves_of()` instead of failing when opening them as files. - Deduplicates requested ops/patterns in `report()` (preserving order) so passing multiple encodings of one instruction (e.g. `v_cndmask_b32_e32 v_cndmask_b32_e64`) emits one combined base-op row. 3. **Up-front `--index` / `--json` guards and atomic JSON output (`_inside_dir`, `_validate_json_path`, `_write_json_atomic`)**: - Rejects directory or empty/whitespace `--index` and `--json` paths (including `--json ""` and NUL bytes) and non-directory `--json` parents up front with exit code `2`. - Uses `report_paths.same_report_path` to refuse `--json` targets that would overwrite `--index`, `.s` / `.fssm` inputs (including hard-link/symlink/case-folded aliases), or write anywhere inside `disasm` or `--fssm`. - Creates missing nested `--json` parent folders and publishes strict LF JSON (`allow_nan=False`, trailing `\n`) atomically via `tempfile.mkstemp`, `file_mode.published_mode`, and `replace_retry.replace`, cleaning up the staged temp file on failure. 4. **Tests (`tests/test_op_reach.py`)**: - Adds synthetic regression tests covering BOM/prefixed/ambiguous/non-string `index.json`, `*.s`/`*.fssm` subdirectories, op deduplication, `--index` and `--json` overwrite/containment guards (including hard links, symlinks, and mocked macOS case-insensitive aliases), nested atomic output, and temp-file cleanup on `replace_retry.replace` failure. ### Tested - `python -m compileall -q tools/op_reach.py tests/test_op_reach.py` — clean - `python -I -W error tests/test_op_reach.py -v` — 18 tests, OK - `python -I tests/test_text_write_line_endings.py` — 3 tests, OK - `python -I tests/test_report_paths.py` — 22 tests, OK (2 skipped on unprivileged Windows) - `python -I scripts/check_no_game_data.py tools/op_reach.py tests/test_op_reach.py` — clean GPUs tested: n/a (CPU-only Python tool and synthetic standard-library unit tests on Windows 11) AI-assisted: yes ### 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. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (No translator change.)
copilot-pull-request-reviewer[bot] reviewed 2026-11-25 04:28:53 +00:00
copilot-pull-request-reviewer[bot] left a comment
No description provided.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
MoonEyes commented 2026-11-28 03:02:12 +00:00

Hi @Vemast, thanks for this pull request.

CONTRIBUTING.md asks contributors to claim their work in the coordination issue ("Coordinating who works on what", currently #1608) before opening a pull request: post claiming: <ID> there, or a one-line description if the item is not listed yet, and put the ID in the pull request. I could not find a claim from you for this one (the ID OP-REACH-INDEX-AND-REPORT-GUARDS appears in the pull request, but there is no claiming: line for it).

The claim matters because "it is claimed in the coordination issue by the pull request's author" is the first item on the maintainers' checklist for a mergeable pull request, and because it lets others see that the work is taken before they write it a second time.

Could you post the claim in #1608 and add the ID to the pull request description? If you already claimed it in another form, a link to your comment is enough.

AI-assisted (Claude Code, Claude Opus 5.5).

Hi @Vemast, thanks for this pull request. CONTRIBUTING.md asks contributors to claim their work in the coordination issue ("Coordinating who works on what", currently #1608) before opening a pull request: post `claiming: <ID>` there, or a one-line description if the item is not listed yet, and put the ID in the pull request. I could not find a claim from you for this one (the ID `OP-REACH-INDEX-AND-REPORT-GUARDS` appears in the pull request, but there is no `claiming:` line for it). The claim matters because "it is claimed in the coordination issue by the pull request's author" is the first item on the maintainers' checklist for a mergeable pull request, and because it lets others see that the work is taken before they write it a second time. Could you post the claim in #1608 and add the ID to the pull request description? If you already claimed it in another form, a link to your comment is enough. AI-assisted (Claude Code, Claude Opus 5.5).
Vemast commented 2026-11-28 03:18:11 +00:00

Thanks @MoonEyes! #14 had already hit GitHub's 2,500-comment cap when this PR opened, and #1608 wasn't linked yet. I've now posted claiming: OP-REACH-INDEX-AND-REPORT-GUARDS in #1608 (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7232955159, along with done: at https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7232955325) and updated the PR description with the claim link.

AI-assisted.

Thanks @MoonEyes! `#14` had already hit GitHub's 2,500-comment cap when this PR opened, and `#1608` wasn't linked yet. I've now posted `claiming: OP-REACH-INDEX-AND-REPORT-GUARDS` in #1608 (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7232955159, along with `done:` at https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7232955325) and updated the PR description with the claim link. AI-assisted.
MoonEyes commented 2026-11-28 13:40:41 +00:00

Merged after local re-verification: this PR merged onto main fcbe97b9a without conflict, and its test file(s) (tests/test_op_reach.py) pass there. Earlier today the same tests failed on main without the PR (red/green) and a bundle with other open PRs showed no new failures. check_no_game_data.py is clean. Windows 11, Python 3.14; no GPU needed for these tests.

AI-assisted (Claude Code, Claude Opus 5.5), merged for MoonEyes.

Merged after local re-verification: this PR merged onto `main` fcbe97b9a without conflict, and its test file(s) (`tests/test_op_reach.py`) pass there. Earlier today the same tests failed on `main` without the PR (red/green) and a bundle with other open PRs showed no new failures. `check_no_game_data.py` is clean. Windows 11, Python 3.14; no GPU needed for these tests. _AI-assisted (Claude Code, Claude Opus 5.5), merged for MoonEyes._
Sign in to join this conversation.
No description provided.