bcpack_tool: single-pass profile, history overwrite guard, and atomic output (BCPACK-TOOL-HARDENING) #1422

Merged
IlyassRS3 merged 1 commit from upstream-pr/1422 into main 2026-11-28 20:15:13 +00:00
IlyassRS3 commented 2026-11-24 23:51:12 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-24T23:51:12Z.


Item ID: BCPACK-TOOL-HARDENING
AI-assisted: yes

Summary

Fixes five bugs in tools/bcpack_tool.py:

  1. Eliminate double rANS decode in profile_stream(): profile_stream() previously called inspect_stream() and then called bcpack.decode_lanes() a second time on the same input. Extracted _inspect_decoded_lanes() so both inspect_stream() and profile_stream() decode the bitstream exactly once.
  2. Refuse overwriting --history in decode_file(): decode_file() guarded in_path via same_report_path, but allowed out_path to overwrite a --history JSON file (directly or via symlink/hardlink/case-folded alias) or the per-codec <history_dir>/<codec>.json file read by bcpack_tables_bptc. Both are now protected via same_report_path (while preserving dict history support for Python API callers).
  3. Fix --history error attribution for directories with a trailing slash: os.path.basename("/path/to/hist_dir/") returns "", and "" not in str(e) was always False, silently dropping filename attribution on json.JSONDecodeError. _history_paths() normalizes the directory path and resolves to the concrete <codec>.json file when present so errors always name the failing history file.
  4. Atomic output publishing & early directory/symlink-loop checks: decode_file() now rejects symlink cycles (ELOOP) and existing directories (IsADirectoryError) before reading/decoding, preserves symlinks by resolving os.path.realpath(out_path) (matching tools/glb_inspect.py), stages regular-file writes through tempfile.NamedTemporaryFile + file_mode.published_mode + os.replace, and cleans up staged files on failure so existing output bytes are never truncated on partial write errors.
  5. Handle bcpack.BcpackUnsupportedCodec and redirected stdout cleanly in CLI: main() now catches bcpack.BcpackUnsupportedCodec (NotImplementedError) alongside (ValueError, OSError) so it emits one ERROR: line and exits 2 instead of printing a traceback, and guards sys.stdout.reconfigure(errors="replace").

Verification (macOS Apple M5, arm64, Python 3.14; no game files)

  • python3 -m compileall -q make_game.py tools tests — OK
  • python3 -I scripts/check_no_game_data.py tools/bcpack_tool.py tests/test_bcpack_tool.py — OK
  • python3 -I tests/test_bcpack_tool.py -v — 28/28 OK (10 new regression tests)
  • python3 -I tests/test_bcpack.py — 20/20 OK
  • python3 -I tests/test_bcpack_tables.py — 34/34 OK
  • python3 -I tests/test_bcpack_tables_bptc.py — 21/21 OK
  • python3 -I tests/test_text_write_line_endings.py — 3/3 OK
  • python3 -I tests/test_ci_registration.py — 16/16 OK
  • Mutation proof (running the new tests/test_bcpack_tool.py against unpatched upstream/main 364ab4eab): FAILED (failures=6, errors=1) — catches double decode_lanes, overwriting --history file / symlink / <dir>/bc1.json, dropped trailing-slash history attribution, non-atomic overwrite, and uncaught BcpackUnsupportedCodec.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5482351273 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1422). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-24T23:51:12Z. --- Item ID: `BCPACK-TOOL-HARDENING` AI-assisted: yes ### Summary Fixes five bugs in `tools/bcpack_tool.py`: 1. **Eliminate double rANS decode in `profile_stream()`**: `profile_stream()` previously called `inspect_stream()` and then called `bcpack.decode_lanes()` a second time on the same input. Extracted `_inspect_decoded_lanes()` so both `inspect_stream()` and `profile_stream()` decode the bitstream exactly once. 2. **Refuse overwriting `--history` in `decode_file()`**: `decode_file()` guarded `in_path` via `same_report_path`, but allowed `out_path` to overwrite a `--history` JSON file (directly or via symlink/hardlink/case-folded alias) or the per-codec `<history_dir>/<codec>.json` file read by `bcpack_tables_bptc`. Both are now protected via `same_report_path` (while preserving `dict` history support for Python API callers). 3. **Fix `--history` error attribution for directories with a trailing slash**: `os.path.basename("/path/to/hist_dir/")` returns `""`, and `"" not in str(e)` was always `False`, silently dropping filename attribution on `json.JSONDecodeError`. `_history_paths()` normalizes the directory path and resolves to the concrete `<codec>.json` file when present so errors always name the failing history file. 4. **Atomic output publishing & early directory/symlink-loop checks**: `decode_file()` now rejects symlink cycles (`ELOOP`) and existing directories (`IsADirectoryError`) before reading/decoding, preserves symlinks by resolving `os.path.realpath(out_path)` (matching `tools/glb_inspect.py`), stages regular-file writes through `tempfile.NamedTemporaryFile` + `file_mode.published_mode` + `os.replace`, and cleans up staged files on failure so existing output bytes are never truncated on partial write errors. 5. **Handle `bcpack.BcpackUnsupportedCodec` and redirected stdout cleanly in CLI**: `main()` now catches `bcpack.BcpackUnsupportedCodec` (`NotImplementedError`) alongside `(ValueError, OSError)` so it emits one `ERROR:` line and exits 2 instead of printing a traceback, and guards `sys.stdout.reconfigure(errors="replace")`. ### Verification (macOS Apple M5, arm64, Python 3.14; no game files) - `python3 -m compileall -q make_game.py tools tests` — OK - `python3 -I scripts/check_no_game_data.py tools/bcpack_tool.py tests/test_bcpack_tool.py` — OK - `python3 -I tests/test_bcpack_tool.py -v` — 28/28 OK (10 new regression tests) - `python3 -I tests/test_bcpack.py` — 20/20 OK - `python3 -I tests/test_bcpack_tables.py` — 34/34 OK - `python3 -I tests/test_bcpack_tables_bptc.py` — 21/21 OK - `python3 -I tests/test_text_write_line_endings.py` — 3/3 OK - `python3 -I tests/test_ci_registration.py` — 16/16 OK - **Mutation proof** (running the new `tests/test_bcpack_tool.py` against unpatched `upstream/main` `364ab4eab`): `FAILED (failures=6, errors=1)` — catches double `decode_lanes`, overwriting `--history` file / symlink / `<dir>/bc1.json`, dropped trailing-slash history attribution, non-atomic overwrite, and uncaught `BcpackUnsupportedCodec`.
copilot-pull-request-reviewer[bot] commented 2026-11-24 23:51:27 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-24T23:51:27Z.

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:6049324159 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1422#pullrequestreview-6049324159). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-24T23:51:27Z. **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.
MoonEyes commented 2026-11-28 20:15:09 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T20:15:09Z.


Red/green: On main test_bcpack_tool.py has 5 failures and 1 error (an unsupported codec raises a traceback instead of exiting with code 2), and with the PR 28 tests pass (4 skipped) on Windows 11 at OreoFR's (RTX 4070).
Independent tester (OreoFR, RTX 4070, Windows 11): PR head merged with the other PRs of its batch on main c1ecf04ed, full ctest run (485 tests, tests that launch the game excluded): no new failure compared with main.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7245148438 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1422#issuecomment-7245148438). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T20:15:09Z. --- Red/green: On main test_bcpack_tool.py has 5 failures and 1 error (an unsupported codec raises a traceback instead of exiting with code 2), and with the PR 28 tests pass (4 skipped) on Windows 11 at OreoFR's (RTX 4070). Independent tester (OreoFR, RTX 4070, Windows 11): PR head merged with the other PRs of its batch on main c1ecf04ed, full ctest run (485 tests, tests that launch the game excluded): no new failure compared with main.
OreoFR commented 2026-11-28 21:50:19 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-28T21:50:19Z.


Verified by OreoFR

  • Base: main 3408969a7 + PR head e1fd5654e
  • Test: python -I tests/test_bcpack_tool.py
  • Red on main: 5 FAIL + 1 ERROR (BcpackUnsupportedCodec traceback instead of exit 2): the missing function itself
  • Green with the PR: 28 OK (4 skipped)
  • Verdict: READY
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7246310417 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1422#issuecomment-7246310417). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-28T21:50:19Z. --- **Verified by OreoFR** - Base: main `3408969a7` + PR head `e1fd5654e` - Test: `python -I tests/test_bcpack_tool.py` - Red on main: 5 FAIL + 1 ERROR (BcpackUnsupportedCodec traceback instead of exit 2): the missing function itself - Green with the PR: 28 OK (4 skipped) - Verdict: READY - Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 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!1422
No description provided.