Skip to content

awc_opus: probe the new-file mode once per folder, check codec 0 sizes (ASSET-355) - #4399

Open
AirKyzzZ wants to merge 3 commits into
ItsNotRexed:mainfrom
AirKyzzZ:fix/awc-opus-probe-once-and-pcm-size
Open

AirKyzzZ wants to merge 3 commits into
ItsNotRexed:mainfrom
AirKyzzZ:fix/awc-opus-probe-once-and-pcm-size

Conversation

@AirKyzzZ

Copy link
Copy Markdown

Claimed in the coordination issue as: ASSET-355 (claim)

What and why

Two small fixes in tools/awc_opus.py, one commit each:

  • published_mode() fell back to new_file_mode(dir) for every new output, which creates and deletes a probe file each time. That's one per output, about 1.6M over the corpus. The mode is now cached per output folder inside awc_opus.py, and the shared file_mode.py is untouched. On APFS, 5,000 writes took 1.31 s before and 0.80 s after; NTFS with Defender is probably worse.
  • Codec 0 (PCM) wrote the data tag without the size check codec 2 has. 401 data bytes for 100 stereo frames gave a WAV with 100.25 frames and an odd chunk without the RIFF pad byte. A codec-0 stream whose size isn't samples * channels * 2 now takes the same "not handled" path as codec 2.

Worth checking by someone with their package: the codec-0 check isn't confirmed against the 19 real codec-0 streams yet. The WAV count should stay at 246. If a real stream carries padding, it would now be refused. I'll run it on my own copy when it's ready, unless someone gets there first.

Tested

macOS 27.0.1 (Apple M3 Pro), Python 3.14:

  • python -I tests/test_awc_opus.py: 49 OK. The probe test counts the probe files actually opened (5 on main, 1 here). The codec-0 test uses a synthetic bank with 401, 399 and 400-byte streams (3 WAVs on main, 1 here).
  • test_audio_wav, test_awc_manifest, test_awc_shorts, test_opus_tags, test_file_mode, test_pool_workers and the repo-wide guards OK.

Synthetic banks only. AI-assisted (Claude Code).

Checklist

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

Signed-off-by: Maxime Mansiet <AirKyzzZ@users.noreply.github.com>
…nnels * 2

Signed-off-by: Maxime Mansiet <AirKyzzZ@users.noreply.github.com>

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran this at 69485269 on Windows 11: python -I tests/test_awc_opus.py -> 49 tests, OK; git diff --check clean.

A codec 0 stream whose data size is not samples * channels * 2 is now refused with the expected size, and the new-file probe runs once per folder; the existing audio tests pass. No blocking issue.

@abilliontokens

Copy link
Copy Markdown
Contributor

Tested 6948526 on Windows 11, Python 3.12: pass.

  • python -I tests/test_awc_opus.py: 49 tests OK (the two new ones included, no skips).
  • python -m compileall -q tools/awc_opus.py tests/test_awc_opus.py: clean.
  • Code read, both changes check out:
    • write_file() for a new file now takes the folder's mode via @functools.cached folder_file_mode(parent) (new_file_mode), instead of calling published_mode(out_path) — which had to probe per output file (the new test asserts exactly one .probe open for five new outputs). Existing files keep published_mode(out_path), and the cache is per directory string, so per-process caching under ProcessPoolExecutor stays correct.
    • The codec-0 PCM branch now requires size == samples * channels * 2 before writing a .wav; a stream that fails it falls through to other instead of silently producing a wrong-length WAV. Consistent with the per-channel samples convention the stereo tests document (samples × channels × 2), and the new test covers both sides of the gate (401/399 → other, 400 → pcm).

Hand-built banks only, no game data. AI-assisted (omp orchestrator).

@tupacfan04

Copy link
Copy Markdown
Contributor

PASS (6948526), mergeable, no conflicts. tests/test_awc_opus.py 49/49, 85 adjacent tests across file_mode, pool_workers, awc_manifest, awc_shorts and audio_wav, compileall clean. Negative control on 03b09a08: the probe test fails 5 probes vs 1, and the codec test exports 3 WAVs vs 1.

A refusing folder fails only that bank (tools/awc_opus.py:436) with later banks still exported, and the CLI exits 1 (:454) - unchanged from main, so it avoids aborting early without pretending success. The codec-0 exact-size rule matches the warning in tools/awc_manifest.py:80.

Minor: the per-folder probe cache is unbounded (one int per folder, so small), and the padding risk is unverified without game data.

Approve. AI-assisted review, no game files, no GPU.

@bobcatchris

Copy link
Copy Markdown
Contributor

Heads-up, non-blocking: write_file() has the same read-only temp cleanup failure that tupacfan04 found in #4344. At current head 69485269 I injected an os.replace failure with a read-only destination (Windows, CPython 3.12.0, synthetic bytes): the old bytes survive, but the staged file has inherited the destination's mode and .awc-*.tmp is left behind because it cannot be unlinked. This PR changes the mode-probe line but not the except path, so the bug remains. I deliberately excluded awc_opus from my sibling fix #4516 because this PR owns the function; if you want it here, the one-line fix is to os.chmod(tmp_path, 0o600) inside the existing suppress(OSError) before os.remove(tmp_path), with a regression that checks the reset and no leftovers.

…d replace

Signed-off-by: Maxime Mansiet <AirKyzzZ@users.noreply.github.com>
@AirKyzzZ

Copy link
Copy Markdown
Author

@bobcatchris thanks for leaving it to this PR. Done in a2fdf317: on a failed write, write_file() now sets the staged file to 0o600 (errors ignored) before removing it, so a staging file that inherited a read-only mode no longer stays behind on Windows.

Regression test_failed_replace_over_a_read_only_file_leaves_no_staging_file in tests/test_awc_opus.py. It injects an os.replace failure over a 0o444 destination and expects only the old file afterwards, with its old bytes. Since POSIX unlinks read-only files anyway, the test wraps os.remove to refuse a non-writable file the way Windows does, so it fails without the fix on every host:

  • Windows 10, Python 3.14: OK at a2fdf317; with the previous awc_opus.py the new test fails.
  • macOS 27.0.1, Python 3.14: same (OK / fails without the fix). test_audio_stage_resume and the repo-wide guards OK.

@kapitche

Copy link
Copy Markdown
Contributor

Tested a2fdf31 merged with main 98fa503 on Linux (Python 3.12.16): pass - tests/test_awc_opus.py 50 tests OK; negative control: tools/awc_opus.py restored from main with the PR test kept -> FAILED (failures=3, e.g. a leftover .awc-*.tmp listed, counts 5 != 1).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants