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

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

📋 Pull Request Information

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

Base: main ← Head: fix/op-reach-index-and-report-guards


📝 Commits (1)

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

📊 Changes

2 files changed (+281 additions, -12 deletions)

View changed files

📝 tests/test_op_reach.py (+145 -0)
📝 tools/op_reach.py (+136 -12)

📄 Description

Claimed in the coordination issue as: OP-REACH-INDEX-AND-REPORT-GUARDS — verified 0 open PRs and 0 prior claims touching tools/op_reach.py (unable to post a comment on #14 because GitHub disabled commenting once #14 reached its 2,500-comment cap, and no successor "Coordinating who works on what" issue is open yet)

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

🔄 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/1556 **Author:** [@Vemast](https://github.com/Vemast) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/op-reach-index-and-report-guards` --- ### 📝 Commits (1) - [`a237f51`](https://github.com/ubiquitarianism/leonida/commit/a237f51ea4f9e83618b0afdb243bad13eff23143) op_reach: harden index parsing, report_paths guards, and atomic JSON I/O (OP-REACH-INDEX-AND-REPORT-GUARDS) ### 📊 Changes **2 files changed** (+281 additions, -12 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_op_reach.py` (+145 -0) 📝 `tools/op_reach.py` (+136 -12) </details> ### 📄 Description Claimed in the coordination issue as: OP-REACH-INDEX-AND-REPORT-GUARDS — verified 0 open PRs and 0 prior claims touching `tools/op_reach.py` (unable to post a comment on `#14` because GitHub disabled commenting once `#14` reached its 2,500-comment cap, and no successor `"Coordinating who works on what"` issue is open yet) 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.) --- <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#1581
No description provided.