gamedata_blobs: fix unshipped count, index type checks, and atomic JSON #2258

Merged
leonida/mandy
kirigayakazima merged 2 commits from fix/gamedata-blobs-safety into main 2026-11-29 14:09:21 +00:00

Pull request successfully merged and closed

kirigayakazima commented 2026-11-26 10:51:26 +00:00

Claimed in the coordination issue as: GAMEDATA-BLOBS-UNSHIPPED-OVERLAP-INDEX-TYPES-AND-ATOMIC-JSON (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7189816855)

What and why

  • Fix unshipped overlap double-subtraction in cross_reference_sgd5() (tools/gamedata_blobs.py) so preload-list entries present in both db0 and db1 are counted accurately via sum(1 for s in stems if s not in db0_set and s not in db1_set) instead of p["entries"] - len(in_db0) - len(in_db1).
  • Tighten check_carve_index() to convert UnicodeDecodeError and RecursionError from json.load() into single-line ValueError diagnostics and require type(row.get("off")) is int and type(row.get("stored")) is int (non-bool, >= 0).
  • Avoid double-parsing index.json across main() and read_payloads(..., _checked_index=is_dir), reject trailing / or \ --json directory paths in check_report_path() and non-file --shaders upfront before read_payloads(), and catch RecursionError cleanly in main().
  • Write --json output atomically via _write_json_atomic() (staging next to os.path.realpath(out_path), trailing LF \n, os.fsync(), preserving permissions with published_mode(), publishing via replace_retry.replace(), and unlinking .blobs-*.tmp files on failure).
  • Add GamedataBlobsSafetyTests to tests/test_gamedata_blobs.py (without overlapping #1269's lines) covering overlap unshipped, bool/negative/str index types, RecursionError, single index.json parse, early CLI validation, and atomic JSON write/failure rollback.

Tested

  • Patched: python -I tests/test_gamedata_blobs.py -v -> Ran 14 tests in 1.178s (OK).
  • Baseline check (upstream/main a73a57e1 with updated tests/test_gamedata_blobs.py copied into detached worktree): Ran 14 tests in 1.119s (FAILED (failures=10)).
  • Repo gates: python -I tests/test_ci_registration.py (Ran 16 tests in 5.560s, OK), python -I tests/test_text_write_line_endings.py (Ran 3 tests in 13.499s, OK), python -I scripts/check_no_game_data.py (exit 0), python -I -m compileall -q tools/gamedata_blobs.py tests/test_gamedata_blobs.py (exit 0), git diff --check (exit 0).

GPUs tested: n/a (synthetic carve/RPF/SGD5 fixtures and atomic JSON output tests)

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.
Claimed in the coordination issue as: GAMEDATA-BLOBS-UNSHIPPED-OVERLAP-INDEX-TYPES-AND-ATOMIC-JSON (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7189816855) ### What and why - Fix `unshipped` overlap double-subtraction in `cross_reference_sgd5()` (`tools/gamedata_blobs.py`) so preload-list entries present in both `db0` and `db1` are counted accurately via `sum(1 for s in stems if s not in db0_set and s not in db1_set)` instead of `p["entries"] - len(in_db0) - len(in_db1)`. - Tighten `check_carve_index()` to convert `UnicodeDecodeError` and `RecursionError` from `json.load()` into single-line `ValueError` diagnostics and require `type(row.get("off")) is int` and `type(row.get("stored")) is int` (non-`bool`, `>= 0`). - Avoid double-parsing `index.json` across `main()` and `read_payloads(..., _checked_index=is_dir)`, reject trailing `/` or `\` `--json` directory paths in `check_report_path()` and non-file `--shaders` upfront before `read_payloads()`, and catch `RecursionError` cleanly in `main()`. - Write `--json` output atomically via `_write_json_atomic()` (staging next to `os.path.realpath(out_path)`, trailing LF `\n`, `os.fsync()`, preserving permissions with `published_mode()`, publishing via `replace_retry.replace()`, and unlinking `.blobs-*.tmp` files on failure). - Add `GamedataBlobsSafetyTests` to `tests/test_gamedata_blobs.py` (without overlapping `#1269`'s lines) covering overlap `unshipped`, `bool`/negative/`str` index types, `RecursionError`, single `index.json` parse, early CLI validation, and atomic JSON write/failure rollback. ### Tested - Patched: `python -I tests/test_gamedata_blobs.py -v` -> `Ran 14 tests in 1.178s` (`OK`). - Baseline check (`upstream/main` `a73a57e1` with updated `tests/test_gamedata_blobs.py` copied into detached worktree): `Ran 14 tests in 1.119s` (`FAILED (failures=10)`). - Repo gates: `python -I tests/test_ci_registration.py` (`Ran 16 tests in 5.560s`, `OK`), `python -I tests/test_text_write_line_endings.py` (`Ran 3 tests in 13.499s`, `OK`), `python -I scripts/check_no_game_data.py` (exit 0), `python -I -m compileall -q tools/gamedata_blobs.py tests/test_gamedata_blobs.py` (exit 0), `git diff --check` (exit 0). GPUs tested: n/a (synthetic carve/RPF/SGD5 fixtures and atomic JSON output tests) 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. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed.
lixonik reviewed 2026-11-26 10:58:27 +00:00
lixonik left a comment
No description provided.
Windows 11: 14/14 Python tests passed. Two additional checks on a synthetic carve reproduced a missed output collision and a second `index.json` parse. Details inline. AI-assisted.
tiguan26 approved these changes 2026-11-27 04:36:00 +00:00
tiguan26 left a comment

Approving d1545dfde.

The unshipped-count fix is real: entries - len(in_db0) - len(in_db1) subtracted a stem present in both databases twice (it could go negative); counting stems in neither set is right. Index rows now need nonnegative integer off/stored, bad UTF-8 is reported, the index is not checked twice, and the JSON is written atomically.

Merged onto main d1dff7949 in one batch with 22 other tool PRs of this series (no conflicts), on Linux with Python 3.13. tests/test_gamedata_blobs.py: 14 passed (main: 10). AI-assisted (Claude Code, Claude Opus 5.5).

Approving d1545dfde. The unshipped-count fix is real: `entries - len(in_db0) - len(in_db1)` subtracted a stem present in both databases twice (it could go negative); counting stems in neither set is right. Index rows now need nonnegative integer `off`/`stored`, bad UTF-8 is reported, the index is not checked twice, and the JSON is written atomically. Merged onto `main` `d1dff7949` in one batch with 22 other tool PRs of this series (no conflicts), on Linux with Python 3.13. `tests/test_gamedata_blobs.py`: 14 passed (main: 10). AI-assisted (Claude Code, Claude Opus 5.5).
kirigayakazima reviewed 2026-11-27 12:07:06 +00:00
kirigayakazima reviewed 2026-11-27 12:07:09 +00:00
lixonik reviewed 2026-11-27 14:23:23 +00:00
lixonik left a comment
No description provided.
Re-checked at `843a265f4` (merged onto `main` `ec91d695c`). Both earlier points are cleanly addressed: - `check_report_path()` now rejects `.bk2` and `.bin` carve-input aliases matching `DirectorySource.read()`. - `_LoadedDirectorySource` reuses the validated sorted rows from `check_carve_index()` so `index.json` is parsed once, covered by the `json.load` spy test. Verified locally on Windows 11 (Python 3): - `tests/test_gamedata_blobs.py`: 15/15 passed - `scripts/check_no_game_data.py` and `git diff --check`: clean No remaining findings. AI-assisted.
sodove commented 2026-11-28 09:15:35 +00:00

#2258 | head 843a265f47d63dec939e7f69ac1f26ebde1ae193 | python3 -I tests/test_gamedata_blobs.py -v | red: 15 tests, 11 assertion failures | green: 15 passed | Red → green

Baseline assertions failed on summary counts, invalid index diagnostics and output handling.

Pinned main 4938673668d807bf365b61284bf22b1ff8d02cc4, merged into the PR head. Ubuntu 26.04.1 (WSL2), Python 3.14.4. Full suite: not run. AI-assisted: OpenAI Codex. Batch claim.

#2258 | head `843a265f47d63dec939e7f69ac1f26ebde1ae193` | `python3 -I tests/test_gamedata_blobs.py -v` | red: 15 tests, 11 assertion failures | green: 15 passed | **Red → green** Baseline assertions failed on summary counts, invalid index diagnostics and output handling. Pinned main `4938673668d807bf365b61284bf22b1ff8d02cc4`, merged into the PR head. Ubuntu 26.04.1 (WSL2), Python 3.14.4. Full suite: not run. AI-assisted: OpenAI Codex. [Batch claim](https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7236982945).
zmasi commented 2026-11-28 22:08:32 +00:00

Native Windows 11 NTFS Verification (843a265f47)

Verified 843a265f47d63dec939e7f69ac1f26ebde1ae193 merged cleanly onto origin/main (c7753ed98a56677f10e34078b37eab0540e190ec, git merge-tree --write-tree rc=0) on native Windows 11 (26200) + NTFS:

  • Red (origin/main c7753ed98a): python -I tests/test_gamedata_blobs.py failed 11/15 (FAILED (failures=11)).
  • Green (origin/main + 843a265f47):
    • python -I tests/test_gamedata_blobs.py: 15/15 OK (overlap-safe unshipped count, strict int row type/sign checks, RecursionError handling, single index.json parse, .bk2 input-alias refusal, and pure-LF atomic JSON output).
    • Live unmocked Windows 11 NTFS stat.S_IREAD probe: Ran gamedata_blobs.main([carve, "--json", blobs.json]) against a read-only blobs.json (stat.S_IREAD): exited rc=1, left 0 .blobs-*.tmp staged files, and preserved blobs.json byte-for-byte.

AI-assisted.

### Native Windows 11 NTFS Verification (`843a265f47`) Verified `843a265f47d63dec939e7f69ac1f26ebde1ae193` merged cleanly onto `origin/main` (`c7753ed98a56677f10e34078b37eab0540e190ec`, `git merge-tree --write-tree` `rc=0`) on native **Windows 11 (`26200`) + NTFS**: - **Red (`origin/main` `c7753ed98a`)**: `python -I tests/test_gamedata_blobs.py` failed **`11/15`** (`FAILED (failures=11)`). - **Green (`origin/main + 843a265f47`)**: - `python -I tests/test_gamedata_blobs.py`: **`15/15 OK`** (overlap-safe `unshipped` count, strict `int` row type/sign checks, `RecursionError` handling, single `index.json` parse, `.bk2` input-alias refusal, and pure-LF atomic JSON output). - **Live unmocked Windows 11 NTFS `stat.S_IREAD` probe**: Ran `gamedata_blobs.main([carve, "--json", blobs.json])` against a read-only `blobs.json` (`stat.S_IREAD`): exited `rc=1`, left **`0` `.blobs-*.tmp` staged files**, and preserved `blobs.json` byte-for-byte. AI-assisted.
timbot103 reviewed 2026-11-29 04:27:13 +00:00
timbot103 left a comment
No description provided.
Static audit of `843a265f4` against the base code. No builds, tests, or GPU runs were performed for this review. The `unshipped` calculation counts each preload entry absent from both databases. Both index fields require exact nonnegative integers, which excludes booleans, and the validated rows are reused after one parse. The writer stages beside the resolved destination, flushes and syncs before `os.replace`, and pins LF in the form recognized by `tests/test_text_write_line_endings.py`. The added tests cover overlap, malformed index rows, recursion, a single parse, early argument checks, a carve-input alias, and rollback on dump and replace errors. - **Major** — `tools/gamedata_blobs.py:430` can replace a read-only report in a writable POSIX directory or turn an existing FIFO into a regular file. Preserve the nontruncating writability check for regular files and handle special destinations before the rename, as in `tools/bink_index.py:364`. - **Major** — `tools/gamedata_blobs.py:414` turns the invalid POSIX destination `regular-file/../keep.json` into `keep.json` and can overwrite it. Validate the unreduced path before resolving it so an existing non-directory component still produces `ENOTDIR`. - **Minor** — `tools/gamedata_blobs.py:433` skips unlinking the stage if cleanup `chmod` fails. Attempt unlink independently and cover that failure path in the rollback test. - **Minor** — `tools/gamedata_blobs.py:394` rejects a literal trailing backslash in a valid POSIX filename. Check separators for the active platform and update the test at `tests/test_gamedata_blobs.py:438`. AI-assisted.
MoonEyes commented 2026-11-29 14:19:07 +00:00

Merged at head 843a265. Tested by others on this head: sodove (WSL, Python 3.14) and zmasi (Windows 11 NTFS): tests/test_gamedata_blobs.py 11/15 fail on main, 15/15 pass with the PR. No GPU involved. AI-assisted: yes.

Merged at head 843a265. Tested by others on this head: sodove (WSL, Python 3.14) and zmasi (Windows 11 NTFS): `tests/test_gamedata_blobs.py` 11/15 fail on main, 15/15 pass with the PR. No GPU involved. AI-assisted: yes.
Sign in to join this conversation.
No description provided.