psin_psch: fix PMAP exact_end, schema checks, head handling, and carve-dir/atomic JSON I/O (PSIN-PSCH-VALIDATION-AND-IO-HARDENING) #1523

Merged
IlyassRS3 merged 1 commit from upstream-pr/1523 into main 2026-11-28 14:45:15 +00:00
IlyassRS3 commented 2026-11-25 03:23:41 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T03:23:41Z.


Summary

Fixes several concrete bugs and hardens validation and report I/O in tools/psin_psch.py:

  • PMAP exact_end calculation (decode_pmap): computes the data-area end from max(b["offset"] + b["length"] for b in blocks) instead of row-order blocks[-1], so valid PMAP tables with a trailing empty block or out-of-order final row are no longer falsely rejected.
  • Self-referencing descriptor & duplicate hash detection (check_psch_schema): rejects self-referencing PSCH_TYPE_ARRAY (elem_idx == idx) and PSCH_TYPE_MAP (k_idx == idx / v_idx == idx) descriptors and reports duplicate struct or enum hashes in PSCH instead of silently overwriting earlier entries.
  • Safe head row filtering (_has_psin_head, scan_target): handles None/non-string head fields in carve index.json rows without raising an uncaught AttributeError, and matches uppercase "5053494E" prefixes as well as lowercase.
  • Complete --carve-dir & pre-parse overwrite protection (check_report_path, main): checks .bin payloads inside --carve-dir via gm.check_report_path (not only index.json), and runs the initial check_report_path guard before reading index.json so --json <dir>/index.json still exits 2 even when index.json is corrupt.
  • Atomic JSON report writing (_write_json, main): stages --json output in a sibling temp file with newline="\n", trailing LF, allow_nan=False, file_mode.published_mode, and replace_retry.replace, preserving pre-existing files on failure.

Coordination

Testing

  • python3 -I tests/test_psin_psch.py -v — 9/9 OK (including 5 new regression tests)
  • python3 -I tests/test_missing_input_exit_codes.py — 2/2 OK
  • python3 -I tests/test_text_write_line_endings.py — 3/3 OK
  • python3 -I scripts/check_no_game_data.py tools/psin_psch.py tests/test_psin_psch.py — clean
  • python3 -m compileall -q tools/psin_psch.py tests/test_psin_psch.py — clean
  • Mutation check against unpatched main:tools/psin_psch.py: 5/5 new tests fail (FAILED (failures=3, errors=2)).

Checklist

  • AI-assisted: yes
  • GPUs tested: none required (pure standard-library CPU container/CLI tests, zero game files)
  • No game data, keys, or online services included
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5484552015 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1523). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T03:23:41Z. --- ## Summary Fixes several concrete bugs and hardens validation and report I/O in `tools/psin_psch.py`: - **PMAP `exact_end` calculation** (`decode_pmap`): computes the data-area end from `max(b["offset"] + b["length"] for b in blocks)` instead of row-order `blocks[-1]`, so valid PMAP tables with a trailing empty block or out-of-order final row are no longer falsely rejected. - **Self-referencing descriptor & duplicate hash detection** (`check_psch_schema`): rejects self-referencing `PSCH_TYPE_ARRAY` (`elem_idx == idx`) and `PSCH_TYPE_MAP` (`k_idx == idx` / `v_idx == idx`) descriptors and reports duplicate struct or enum hashes in `PSCH` instead of silently overwriting earlier entries. - **Safe `head` row filtering** (`_has_psin_head`, `scan_target`): handles `None`/non-string `head` fields in carve `index.json` rows without raising an uncaught `AttributeError`, and matches uppercase `"5053494E"` prefixes as well as lowercase. - **Complete `--carve-dir` & pre-parse overwrite protection** (`check_report_path`, `main`): checks `.bin` payloads inside `--carve-dir` via `gm.check_report_path` (not only `index.json`), and runs the initial `check_report_path` guard before reading `index.json` so `--json <dir>/index.json` still exits 2 even when `index.json` is corrupt. - **Atomic JSON report writing** (`_write_json`, `main`): stages `--json` output in a sibling temp file with `newline="\n"`, trailing LF, `allow_nan=False`, `file_mode.published_mode`, and `replace_retry.replace`, preserving pre-existing files on failure. ## Coordination - Claimed in the coordination issue as: `PSIN-PSCH-VALIDATION-AND-IO-HARDENING` (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7151838167) - Depends on: none ## Testing - `python3 -I tests/test_psin_psch.py -v` — 9/9 OK (including 5 new regression tests) - `python3 -I tests/test_missing_input_exit_codes.py` — 2/2 OK - `python3 -I tests/test_text_write_line_endings.py` — 3/3 OK - `python3 -I scripts/check_no_game_data.py tools/psin_psch.py tests/test_psin_psch.py` — clean - `python3 -m compileall -q tools/psin_psch.py tests/test_psin_psch.py` — clean - Mutation check against unpatched `main:tools/psin_psch.py`: 5/5 new tests fail (`FAILED (failures=3, errors=2)`). ## Checklist - AI-assisted: yes - GPUs tested: none required (pure standard-library CPU container/CLI tests, zero game files) - No game data, keys, or online services included
copilot-pull-request-reviewer[bot] commented 2026-11-25 03:23:54 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T03:23:54Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6051393929 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1523#pullrequestreview-6051393929). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T03:23:54Z. **Review:** COMMENTED --- 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.
ytsodacan commented 2026-11-25 03:34:01 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by ytsodacan on 2026-11-25T03:34:01Z.


[KEEP]

  • Scoped to tools/psin_psch.py and tests/test_psin_psch.py; the PMAP exact_end fix (use max block end instead of the last row), duplicate struct/enum hash checks and atomic JSON writes look reasonable, with new tests for each.
  • Only no-game-data and change-scope checks are reported and both were still pending, so there is no green CI signal yet. Merge once they pass.
  • This is a broad bundle (PMAP, schema checks, head handling, carve-dir guard, atomic I/O) in one PR; splitting would ease review but is not required.
  • No conflicts and no overlapping open PR found (#873, the related earlier change, is already merged).
<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7152033873 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1523#issuecomment-7152033873). Originally posted by [ytsodacan](https://github.com/ytsodacan) on 2026-11-25T03:34:01Z. --- **[KEEP]** - Scoped to `tools/psin_psch.py` and `tests/test_psin_psch.py`; the PMAP `exact_end` fix (use max block end instead of the last row), duplicate struct/enum hash checks and atomic JSON writes look reasonable, with new tests for each. - Only `no-game-data` and `change-scope` checks are reported and both were still pending, so there is no green CI signal yet. Merge once they pass. - This is a broad bundle (PMAP, schema checks, head handling, carve-dir guard, atomic I/O) in one PR; splitting would ease review but is not required. - No conflicts and no overlapping open PR found (#873, the related earlier change, is already merged).
whoalin1 commented 2026-11-25 07:53:22 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by whoalin1 on 2026-11-25T07:53:22Z.


Independent verification (native Windows, Python 3.11.9 — stdlib-only, representative): diff vs merge-base reviewed; PMAP exact_end via max(block end), duplicate/self-ref schema checks, lowercase head match, check_report_path rows coverage, atomic _write_json all present; tests 9/9+2/2+3/3, mutation reproduces FAILED(3,2). compileall + check_no_game_data clean. AI-assisted verification.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7156365508 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1523#issuecomment-7156365508). Originally posted by [whoalin1](https://github.com/whoalin1) on 2026-11-25T07:53:22Z. --- Independent verification (native Windows, Python 3.11.9 — stdlib-only, representative): diff vs merge-base reviewed; PMAP exact_end via max(block end), duplicate/self-ref schema checks, lowercase head match, check_report_path rows coverage, atomic _write_json all present; tests 9/9+2/2+3/3, mutation reproduces FAILED(3,2). compileall + check_no_game_data clean. AI-assisted verification.
MoonEyes commented 2026-11-28 14:45:11 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T14:45:11Z.


Red/green: pytest tests/test_psin_psch.py fails on main (5 failed, 4 passed) and passes on head 64b23f4f17 merged with main (9 passed).
Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main d48a957bb in GitHub Actions: no new failure.
AI-assisted (Claude Code, Claude Opus 5.5)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7240904554 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1523#issuecomment-7240904554). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T14:45:11Z. --- Red/green: `pytest tests/test_psin_psch.py` fails on main (5 failed, 4 passed) and passes on head 64b23f4f17ca merged with main (9 passed). Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main d48a957bb in GitHub Actions: no new failure. AI-assisted (Claude Code, Claude Opus 5.5)
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
ProjectLeonida/mandy!1523
No description provided.