scripts/corpus_watch: strip BOM JSON, check --out/--game collisions, validate args, enforce UTF-8 #2256

Merged
kirigayakazima merged 2 commits from upstream-pr/2256 into main 2026-11-29 03:38:09 +00:00
kirigayakazima commented 2026-11-26 10:43:07 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kirigayakazima on 2026-11-26T10:43:07Z.


Claimed in the coordination issue as: CORPUS-WATCH-CLI-ENCODING-COLLISION-AND-BOM-SAFETY (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7189698488)

What and why

  • Read dxbc/index.json, spirv/index.json, and --results JSONL with encoding="utf-8-sig" in scripts/corpus_watch.py and validate parsed container root types (list/dict for carved index, dict for spirv/index.json).
  • Extend outputs_collide() (keeping 4-argument default compatibility) to check --out and --game alongside work, report, results, and log.
  • Update main(argv=None) to reject non-finite or negative --interval, non-positive --jobs, and directory/trailing-separator --results or --log paths via ap.error(...) before tool checks or running make_game.py.
  • Ensure parent directories for --results and --log are created once prerequisites pass, and pass encoding="utf-8", errors="replace" to both subprocess.run() invocations.
  • Expand tests/test_corpus_watch.py with tests covering BOM indexes, malformed roots, --out/--game collision checks, CLI argument validation, nested parent folder creation, and subprocess encoding.

Tested

  • Patched: python -I tests/test_corpus_watch.py -v -> Ran 7 tests in 0.149s (OK).
  • Baseline check (upstream/main a73a57e1 with updated tests/test_corpus_watch.py copied into detached worktree): Ran 7 tests in 0.101s (FAILED (errors=10)).
  • Repo gates: python -I tests/test_ci_registration.py (Ran 16 tests in 5.514s, OK), python -I tests/test_text_write_line_endings.py (Ran 3 tests in 13.118s, OK), python -I scripts/check_no_game_data.py (exit 0), python -I -m compileall -q (exit 0), git diff --check (exit 0).

GPUs tested: n/a (mocked subprocess calls and corpus-counting unit 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.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5508522217 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2256). Originally posted by [kirigayakazima](https://github.com/kirigayakazima) on 2026-11-26T10:43:07Z. --- Claimed in the coordination issue as: CORPUS-WATCH-CLI-ENCODING-COLLISION-AND-BOM-SAFETY (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7189698488) ### What and why - Read `dxbc/index.json`, `spirv/index.json`, and `--results` JSONL with `encoding="utf-8-sig"` in `scripts/corpus_watch.py` and validate parsed container root types (`list`/`dict` for carved index, `dict` for `spirv/index.json`). - Extend `outputs_collide()` (keeping 4-argument default compatibility) to check `--out` and `--game` alongside `work`, `report`, `results`, and `log`. - Update `main(argv=None)` to reject non-finite or negative `--interval`, non-positive `--jobs`, and directory/trailing-separator `--results` or `--log` paths via `ap.error(...)` before tool checks or running `make_game.py`. - Ensure parent directories for `--results` and `--log` are created once prerequisites pass, and pass `encoding="utf-8", errors="replace"` to both `subprocess.run()` invocations. - Expand `tests/test_corpus_watch.py` with tests covering BOM indexes, malformed roots, `--out`/`--game` collision checks, CLI argument validation, nested parent folder creation, and subprocess encoding. ### Tested - Patched: `python -I tests/test_corpus_watch.py -v` -> `Ran 7 tests in 0.149s` (`OK`). - Baseline check (`upstream/main` `a73a57e1` with updated `tests/test_corpus_watch.py` copied into detached worktree): `Ran 7 tests in 0.101s` (`FAILED (errors=10)`). - Repo gates: `python -I tests/test_ci_registration.py` (`Ran 16 tests in 5.514s`, `OK`), `python -I tests/test_text_write_line_endings.py` (`Ran 3 tests in 13.118s`, `OK`), `python -I scripts/check_no_game_data.py` (exit 0), `python -I -m compileall -q` (exit 0), `git diff --check` (exit 0). GPUs tested: n/a (mocked subprocess calls and corpus-counting unit 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 commented 2026-11-26 10:50:44 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-26T10:50:44Z.

Review: COMMENTED


Windows 11: 7/7 Python tests passed. A synthetic end-to-end CLI check with child commands stubbed reproduced an input index being modified before the command fails. Details inline.

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6072803842 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2256#pullrequestreview-6072803842). Originally posted by [lixonik](https://github.com/lixonik) on 2026-11-26T10:50:44Z. **Review:** COMMENTED --- Windows 11: 7/7 Python tests passed. A synthetic end-to-end CLI check with child commands stubbed reproduced an input index being modified before the command fails. Details inline. AI-assisted.
lixonik commented 2026-11-26 10:50:44 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-26T10:50:44Z.

Code review location: scripts/corpus_watch.py; line 86

@@ -71,20 +76,22 @@ def corpus_counts(work):
             "validated": validated, "per_stage": per_stage}
 
 
-def outputs_collide(work, report, results, log):
+def outputs_collide(work, report, results, log, game=None, out=None):
     """Refuse an output that names an input or another output."""
-    outputs = [p for p in (report, results, log) if p]
+    outputs = [p for p in (report, results, log, out) if p]
     for i, first in enumerate(outputs):
         for second in outputs[i + 1:]:
             if same_report_path(first, second):
                 return True
-    for out in outputs:
-        if same_report_path(out, work):
-            return True
+    inputs = [p for p in (work, game) if p]

[P2] Include the corpus indexes consumed by the watcher in the output collision check.

For --once --results <work>/shaders/dxbc/index.json, this guard accepts the path. With a synthetic DXBC index containing [{"stage":"vs"}], the watcher counts the index and appends its JSONL result to the same file. A subsequent json.load() fails with Extra data, and the watcher itself fails while reading the original list as a results record. This also applies to the SPIR-V index and legacy index paths. Can the guard reject results/log aliases of the actual index files before it writes anything?

<!-- greyfinch-sync:ubiquitarianism/leonida:review-comment:4707283617 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2256#discussion_r4707283617). Originally posted by [lixonik](https://github.com/lixonik) on 2026-11-26T10:50:44Z. **Code review location:** `scripts/corpus_watch.py`; line 86 ~~~~diff @@ -71,20 +76,22 @@ def corpus_counts(work): "validated": validated, "per_stage": per_stage} -def outputs_collide(work, report, results, log): +def outputs_collide(work, report, results, log, game=None, out=None): """Refuse an output that names an input or another output.""" - outputs = [p for p in (report, results, log) if p] + outputs = [p for p in (report, results, log, out) if p] for i, first in enumerate(outputs): for second in outputs[i + 1:]: if same_report_path(first, second): return True - for out in outputs: - if same_report_path(out, work): - return True + inputs = [p for p in (work, game) if p] ~~~~ --- [P2] Include the corpus indexes consumed by the watcher in the output collision check. For `--once --results <work>/shaders/dxbc/index.json`, this guard accepts the path. With a synthetic DXBC index containing `[{"stage":"vs"}]`, the watcher counts the index and appends its JSONL result to the same file. A subsequent `json.load()` fails with `Extra data`, and the watcher itself fails while reading the original list as a results record. This also applies to the SPIR-V index and legacy index paths. Can the guard reject results/log aliases of the actual index files before it writes anything?
tiguan26 commented 2026-11-27 04:35:42 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by tiguan26 on 2026-11-27T04:35:42Z.

Review: APPROVED


Approving fa4d7ae12.

Outputs are now checked against the game folder as well as the work folder (and --out among them), --interval/--jobs/--results/--log are validated, the JSON inputs must be the expected shapes, and llvm-mc output is decoded as UTF-8.

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_corpus_watch.py: 7 passed (main: 4). AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6083534505 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2256#pullrequestreview-6083534505). Originally posted by [tiguan26](https://github.com/tiguan26) on 2026-11-27T04:35:42Z. **Review:** APPROVED --- Approving fa4d7ae12. Outputs are now checked against the game folder as well as the work folder (and `--out` among them), `--interval`/`--jobs`/`--results`/`--log` are validated, the JSON inputs must be the expected shapes, and llvm-mc output is decoded as UTF-8. 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_corpus_watch.py`: 7 passed (main: 4). AI-assisted (Claude Code, Claude Opus 5.5).
kirigayakazima commented 2026-11-27 12:07:12 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kirigayakazima on 2026-11-27T12:07:12Z.

Code review location: scripts/corpus_watch.py; line 86

@@ -71,20 +76,22 @@ def corpus_counts(work):
             "validated": validated, "per_stage": per_stage}
 
 
-def outputs_collide(work, report, results, log):
+def outputs_collide(work, report, results, log, game=None, out=None):
     """Refuse an output that names an input or another output."""
-    outputs = [p for p in (report, results, log) if p]
+    outputs = [p for p in (report, results, log, out) if p]
     for i, first in enumerate(outputs):
         for second in outputs[i + 1:]:
             if same_report_path(first, second):
                 return True
-    for out in outputs:
-        if same_report_path(out, work):
-            return True
+    inputs = [p for p in (work, game) if p]

Fixed: outputs_collide() now also guards work's corpus index paths (shaders/{dxbc,spirv}/index.json and legacy {dxbc,spirv}/index.json) so --results/--log cannot alias them. Added regression tests in tests/test_corpus_watch.py (8 passed + 5 repo checks).

<!-- greyfinch-sync:ubiquitarianism/leonida:review-comment:4719170666 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2256#discussion_r4719170666). Originally posted by [kirigayakazima](https://github.com/kirigayakazima) on 2026-11-27T12:07:12Z. **Code review location:** `scripts/corpus_watch.py`; line 86 ~~~~diff @@ -71,20 +76,22 @@ def corpus_counts(work): "validated": validated, "per_stage": per_stage} -def outputs_collide(work, report, results, log): +def outputs_collide(work, report, results, log, game=None, out=None): """Refuse an output that names an input or another output.""" - outputs = [p for p in (report, results, log) if p] + outputs = [p for p in (report, results, log, out) if p] for i, first in enumerate(outputs): for second in outputs[i + 1:]: if same_report_path(first, second): return True - for out in outputs: - if same_report_path(out, work): - return True + inputs = [p for p in (work, game) if p] ~~~~ --- Fixed: `outputs_collide()` now also guards `work`'s corpus index paths (`shaders/{dxbc,spirv}/index.json` and legacy `{dxbc,spirv}/index.json`) so `--results`/`--log` cannot alias them. Added regression tests in `tests/test_corpus_watch.py` (8 passed + 5 repo checks).
lixonik commented 2026-11-27 14:23:24 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-27T14:23:24Z.

Review: COMMENTED


Re-checked at 199c8c17e (merged onto main ec91d695c).

Extending outputs_collide() to include work's shaders/{dxbc,spirv}/index.json and legacy {dxbc,spirv}/index.json paths (with dedicated collision tests) cleanly covers the overlap with --report.

Verified locally on Windows 11 (Python 3):

  • tests/test_corpus_watch.py: 8/8 passed
  • scripts/check_no_game_data.py and git diff --check: clean

No remaining findings.

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6089570886 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2256#pullrequestreview-6089570886). Originally posted by [lixonik](https://github.com/lixonik) on 2026-11-27T14:23:24Z. **Review:** COMMENTED --- Re-checked at `199c8c17e` (merged onto `main` `ec91d695c`). Extending `outputs_collide()` to include `work`'s `shaders/{dxbc,spirv}/index.json` and legacy `{dxbc,spirv}/index.json` paths (with dedicated collision tests) cleanly covers the overlap with `--report`. Verified locally on Windows 11 (Python 3): - `tests/test_corpus_watch.py`: 8/8 passed - `scripts/check_no_game_data.py` and `git diff --check`: clean No remaining findings. AI-assisted.
sodove commented 2026-11-28 09:15:26 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by sodove on 2026-11-28T09:15:26Z.


#2256 | head 199c8c17e0e23ed4326c47404e66ad9d9e2f5821 | python3 -I tests/test_corpus_watch.py -v | red: 8 tests, 15 errors | green: 8 passed | Exceptions on main → pass

Baseline errors included BOM decoding and incompatible call signatures. The merged tests exercised those paths successfully. Failure and error counts include subtest events.

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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7237120514 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2256#issuecomment-7237120514). Originally posted by [sodove](https://github.com/sodove) on 2026-11-28T09:15:26Z. --- #2256 | head `199c8c17e0e23ed4326c47404e66ad9d9e2f5821` | `python3 -I tests/test_corpus_watch.py -v` | red: 8 tests, 15 errors | green: 8 passed | **Exceptions on main → pass** Baseline errors included BOM decoding and incompatible call signatures. The merged tests exercised those paths successfully. Failure and error counts include subtest events. 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:14:13 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T22:14:13Z.


Native Windows 11 NTFS Verification (199c8c17e0)

Verified 199c8c17e0e23ed4326c47404e66ad9d9e2f5821 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_corpus_watch.py failed (FAILED (errors=15) across 8 tests).
  • Green (origin/main + 199c8c17e0):
    • python -I tests/test_corpus_watch.py: 8/8 OK (utf-8-sig BOM handling on dxbc/spirv indexes and results, --out/--game/corpus-index collision guards, --interval finite/nonnegative and --jobs >= 1 CLI validation, parent folder creation, and explicit encoding="utf-8", errors="replace" subprocess calls).
    • Live Windows 11 NTFS hardlink probe: Created an NTFS hard link (os.link) to shaders\dxbc\index.json and confirmed corpus_watch.outputs_collide rejects it (True).

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7246592307 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2256#issuecomment-7246592307). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T22:14:13Z. --- ### Native Windows 11 NTFS Verification (`199c8c17e0`) Verified `199c8c17e0e23ed4326c47404e66ad9d9e2f5821` 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_corpus_watch.py` failed (`FAILED (errors=15)` across `8` tests). - **Green (`origin/main + 199c8c17e0`)**: - `python -I tests/test_corpus_watch.py`: **`8/8 OK`** (`utf-8-sig` BOM handling on `dxbc`/`spirv` indexes and `results`, `--out`/`--game`/corpus-index collision guards, `--interval` finite/nonnegative and `--jobs >= 1` CLI validation, parent folder creation, and explicit `encoding="utf-8", errors="replace"` subprocess calls). - **Live Windows 11 NTFS hardlink probe**: Created an NTFS hard link (`os.link`) to `shaders\dxbc\index.json` and confirmed `corpus_watch.outputs_collide` rejects it (`True`). AI-assisted.
OreoFR commented 2026-11-29 03:19:47 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-29T03:19:47Z.


Verified by OreoFR on macOS 27.2.1 (26B131), Apple M5, APFS (case-insensitive), Python 3.12.16, main c8bf0e7b6, PR head 199c8c17e : red 15 errors / green 8 OK

python3.12 -I tests/test_corpus_watch.py (8 tests):

  • Merge: clean (git merge-tree).
  • Red (main + only this PR's test file): 15 errors (subtests included) in test_cli_validation_parent_creation_and_subprocess_encoding (interval nan / inf / -1, jobs 0 / -2, results/log pointing at a directory; main's main() takes no argv yet), test_outputs_collide_checks_out_and_game, test_outputs_collide_rejects_work_corpus_indexes (4 index.json paths), test_utf8_bom_indexes_and_malformed_root_types.
  • Green (main + git merge --no-ff): 8/8 OK, skipped 0.
  • (MEASURED n=3) red stable.
  • Neighbours: only this test imports the script; test_tool_startup same psin_dialogue failure on main and merge, unrelated.
  • Diff: BOM accepted and the JSON root must be a list/object; outputs_collide now also compares out vs game and the 4 work-dir index.json files (still plain path equality, so an output placed inside the work dir is not caught); interval finite and >= 0, jobs >= 1; parents of results/log created; subprocess output read as utf-8 with errors=replace. No game data.

Verdict: READY

AI-assisted: Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7249981480 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2256#issuecomment-7249981480). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-29T03:19:47Z. --- Verified by OreoFR on macOS 27.2.1 (26B131), Apple M5, APFS (case-insensitive), Python 3.12.16, main c8bf0e7b6, PR head 199c8c17e : red 15 errors / green 8 OK `python3.12 -I tests/test_corpus_watch.py` (8 tests): - Merge: clean (`git merge-tree`). - **Red** (main + only this PR's test file): 15 errors (subtests included) in `test_cli_validation_parent_creation_and_subprocess_encoding` (interval nan / inf / -1, jobs 0 / -2, results/log pointing at a directory; main's `main()` takes no argv yet), `test_outputs_collide_checks_out_and_game`, `test_outputs_collide_rejects_work_corpus_indexes` (4 index.json paths), `test_utf8_bom_indexes_and_malformed_root_types`. - **Green** (main + `git merge --no-ff`): 8/8 OK, skipped 0. - (MEASURED n=3) red stable. - Neighbours: only this test imports the script; `test_tool_startup` same `psin_dialogue` failure on main and merge, unrelated. - Diff: BOM accepted and the JSON root must be a list/object; `outputs_collide` now also compares out vs game and the 4 work-dir index.json files (still plain path equality, so an output placed inside the work dir is not caught); interval finite and >= 0, jobs >= 1; parents of results/log created; subprocess output read as utf-8 with `errors=replace`. No game data. **Verdict: READY** AI-assisted: Claude Code
DadaCap commented 2026-11-29 03:38:06 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by DadaCap on 2026-11-29T03:38:06Z.


Merging, thank you @kirigayakazima! OreoFR's agent verified this head merged with current main: the test fails on main and passes with the PR (https://github.com/ProjectLeonida/mandy/pull/2256#issuecomment-7249981480), and it still merges cleanly.

AI-assisted (Claude Code, Claude Opus 5.5), on behalf of DadaCap.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7250167213 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2256#issuecomment-7250167213). Originally posted by [DadaCap](https://github.com/DadaCap) on 2026-11-29T03:38:06Z. --- Merging, thank you @kirigayakazima! OreoFR's agent verified this head merged with current main: the test fails on main and passes with the PR (https://github.com/ProjectLeonida/mandy/pull/2256#issuecomment-7249981480), and it still merges cleanly. AI-assisted (Claude Code, Claude Opus 5.5), on behalf of DadaCap.
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!2256
No description provided.