fix(cache): validate translator build stamp, prune stale shaders/blobs on upgrade, and add clean-cache CLI #578

Open
github-bot wants to merge 2 commits from github-bot/gh-578 into main AGit
Owner

@⁠iMarllun on GitHub · 2026-12-06 03:11 UTC · original
iMarllun:fix/shader-cache-validation → main · +758 −0 · 7 files · 2 commits · open

Summary

Improves shader/pipeline cache validation and prevents stale cache buildup across translator updates:

  1. Translator build stamp validation & automatic stale cache pruning (crates/larp-gpu/src/shader_cache.rs):

    • Writes cache/shaders/translator.stamp containing the active shader_cache::build() BLAKE3 digest (which already covers larp-shader sources, shader_cache.rs, iew_heap_limit, iew_title_limit, host_uses_wave32, translator LARP_* switches, and shader_opt).
    • On startup, if ranslator.stamp is missing or does not match the current translator build digest, atomically moves stale cache/shaders/<00..ff> buckets and sibling cache/pipelines/blobs* directories aside (.stale-*) and deletes them on a background thread so startup is not blocked and gigabytes of unreachable shaders/blobs from previous builds do not accumulate on disk.
    • Any leftover .stale-* directories from an interrupted run are also cleaned up automatically.
    • Keeps created-pipelines.bin and quad-pipelines.txt intact by default for fast pre-warming; setting LARP_CLEAN_PIPELINES_ON_UPGRADE=1 also invalidates them on translator build changes.
  2. Safe manual cache reset CLI (larp.exe clean-cache [--all]):

    • Added larp clean-cache in pps/larp/src/main.rs to delete cache/shaders and cache/pipelines/blobs* (and with --all, also created-pipelines.bin and quad-pipelines.txt) without touching data/ (saves and settings.ini).
  3. Docs & Tests:

    • Documented larp.exe clean-cache [--all] and LARP_CLEAN_PIPELINES_ON_UPGRADE in docs/environment-variables.md.
    • Added unit test shader_cache::sync_cache_stamp_prunes_stale_buckets_and_blobs_on_mismatch.

Validation

  • cargo test -p larp-gpu --lib shader_cache: sync_cache_stamp_prunes_stale_buckets_and_blobs_on_mismatch passed (0 failed).
  • cargo check --release -p larp: clean, 0 new warnings.
**@⁠iMarllun** on GitHub · 2026-12-06 03:11 UTC · [original](https://github.com/gamingwithhomie/larp/pull/578) `iMarllun:fix/shader-cache-validation` → `main` · +758 −0 · 7 files · 2 commits · open ## Summary Improves shader/pipeline cache validation and prevents stale cache buildup across translator updates: 1. **Translator build stamp validation & automatic stale cache pruning (crates/larp-gpu/src/shader_cache.rs)**: - Writes cache/shaders/translator.stamp containing the active shader_cache::build() BLAKE3 digest (which already covers larp-shader sources, shader_cache.rs, iew_heap_limit, iew_title_limit, host_uses_wave32, translator LARP_* switches, and shader_opt). - On startup, if ranslator.stamp is missing or does not match the current translator build digest, atomically moves stale cache/shaders/<00..ff> buckets and sibling cache/pipelines/blobs* directories aside (.stale-*) and deletes them on a background thread so startup is not blocked and gigabytes of unreachable shaders/blobs from previous builds do not accumulate on disk. - Any leftover .stale-* directories from an interrupted run are also cleaned up automatically. - Keeps created-pipelines.bin and quad-pipelines.txt intact by default for fast pre-warming; setting LARP_CLEAN_PIPELINES_ON_UPGRADE=1 also invalidates them on translator build changes. 2. **Safe manual cache reset CLI (larp.exe clean-cache [--all])**: - Added larp clean-cache in pps/larp/src/main.rs to delete cache/shaders and cache/pipelines/blobs* (and with --all, also created-pipelines.bin and quad-pipelines.txt) without touching data/ (saves and settings.ini). 3. **Docs & Tests**: - Documented larp.exe clean-cache [--all] and LARP_CLEAN_PIPELINES_ON_UPGRADE in docs/environment-variables.md. - Added unit test shader_cache::sync_cache_stamp_prunes_stale_buckets_and_blobs_on_mismatch. ## Validation - cargo test -p larp-gpu --lib shader_cache: sync_cache_stamp_prunes_stale_buckets_and_blobs_on_mismatch passed (0 failed). - cargo check --release -p larp: clean, 0 new warnings.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-06 03:17:13 +00:00
Author
Owner

@⁠christopear on GitHub · 2026-12-06 03:15 UTC · original

I’d address the deletion boundaries before merging. I exercised the cleanup functions from 605c3c3 on disposable directories in a native Rust harness. The normal separate data directory survived. Two misconfiguration cases are worth guarding because the command promises to leave saves and settings untouched.

  • With the configured data directory under the shaders directory, plain clean-cache deleted a dummy settings.ini and printed that the data directory was untouched.
  • In a fixture with a .larp-root marker, Config::cache_subdir rejected the shaders path. The manual cleaner accepted the same location and removed it.

Could both entry points preflight all targets before the first mutation, resolve aliases, reject overlap with persistent data and enforce the existing source-tree exclusion? I’d also narrow automatic deletion to demonstrably owned entries. A directory called pipelines/.stale-notes disappeared even with a matching stamp. pipelines/blobs-backup disappeared on a missing or changed stamp.

Repeated cold starts on stamp write failure. I blocked the temporary stamp filename with a directory, changed the digest from A to B, then created a fresh blob and removed the obstruction. A second sync with the same B digest deleted the fresh blob again. The first pass had discarded the old generation and silently kept stamp A. Consider a recoverable transition with errors surfaced, or making bulk pruning explicit. A changed translator digest alone also doesn't establish that every host PSO blob is obsolete.

Interaction if #530 lands. After a successful one-recipe hints import, clean-cache --all removed the catalog but left imported-hints.txt. The next import skipped and the catalog stayed absent. Removing the completion marker restored the recipe. The opt-in catalog prune reproduced this too. Plain cleanup correctly retained the recipe. Invalidate import completion whenever the corresponding catalog is reset.

I used the extracted cleanup bodies with a supplied Config, synchronous deletion for deterministic checks, and #530’s import/cache code with synthetic shader bytes. Six runs covering both catalog settings. No game files, Windows junction tests or concurrent processes involved.

**@⁠christopear** on GitHub · 2026-12-06 03:15 UTC · [original](https://github.com/gamingwithhomie/larp/pull/578#issuecomment-7450551589) I’d address the deletion boundaries before merging. I exercised the cleanup functions from `605c3c3` on disposable directories in a native Rust harness. The normal separate data directory survived. Two misconfiguration cases are worth guarding because the command promises to leave saves and settings untouched. * With the configured data directory under the shaders directory, plain `clean-cache` deleted a dummy settings.ini and printed that the data directory was untouched. * In a fixture with a .larp-root marker, Config::cache_subdir rejected the shaders path. The manual cleaner accepted the same location and removed it. Could both entry points preflight all targets before the first mutation, resolve aliases, reject overlap with persistent data and enforce the existing source-tree exclusion? I’d also narrow automatic deletion to demonstrably owned entries. A directory called `pipelines/.stale-notes` disappeared even with a matching stamp. `pipelines/blobs-backup` disappeared on a missing or changed stamp. **Repeated cold starts on stamp write failure.** I blocked the temporary stamp filename with a directory, changed the digest from A to B, then created a fresh blob and removed the obstruction. A second sync with the same B digest deleted the fresh blob again. The first pass had discarded the old generation and silently kept stamp A. Consider a recoverable transition with errors surfaced, or making bulk pruning explicit. A changed translator digest alone also doesn't establish that every host PSO blob is obsolete. **Interaction if #530 lands.** After a successful one-recipe hints import, `clean-cache --all` removed the catalog but left imported-hints.txt. The next import skipped and the catalog stayed absent. Removing the completion marker restored the recipe. The opt-in catalog prune reproduced this too. Plain cleanup correctly retained the recipe. Invalidate import completion whenever the corresponding catalog is reset. I used the extracted cleanup bodies with a supplied Config, synchronous deletion for deterministic checks, and #530’s import/cache code with synthetic shader bytes. Six runs covering both catalog settings. No game files, Windows junction tests or concurrent processes involved.
Author
Owner

@⁠iMarllun on GitHub · 2026-12-06 03:28 UTC · original

Thanks for the thorough review @⁠christopear — addressed every point in bed22b:

  1. Byte-identical shader_cache.rs: moved all stamp validation and cleanup into a dedicated module crates/larp-gpu/src/cache_clean.rs (invoked once from pipeline.rs right after probe_view_limit()) so crates/larp-gpu/src/shader_cache.rs is completely untouched and upgrading does not cause an avoidable cold shader cache.
  2. Preflight before any mutation: both sync_stamp() and clean_cache() share preflight_targets(), which resolves paths/aliases (including ..), enforces larp_core::ensure_outside_repo() (.larp-root exclusion), rejects any overlap with data_dir in either direction, and refuses symlinks / Windows reparse points before touching any file.
  3. Strictly owned matchers & narrower automatic pruning:
    • Stale staging dirs must match .larp-stale-- (never touches .stale-notes).
    • Blob dirs must match exact lobs, lobs-pix, lobs-<16-hex>, or lobs-windows-v1-<64-hex>-<16-hex>-(plain|pix) (never touches lobs-backup).
    • Automatic upgrade pruning on translator mismatch now only prunes provably unreachable cache/shaders/<00..ff> buckets by default; owned pipelines/blobs* and catalog files are only pruned via explicit larp clean-cache or opt-in LARP_CLEAN_PIPELINES_ON_UPGRADE=1.
  4. No repeated cold starts on stamp write failure: commit_stamp() now commits the new ranslator.stamp atomically before discarding old buckets; if committing the stamp fails (e.g. blocked path or read-only dir), it returns an error without deleting any cache entries.
  5. PR #530 compatibility: whenever created-pipelines.bin is reset (clean-cache --all or LARP_CLEAN_PIPELINES_ON_UPGRADE=1), imported-hints.txt is removed alongside it so hints re-import cleanly.

Re-verified with 7 unit tests in cargo test -p larp-gpu --lib cache_clean (all passing) covering .larp-root, data_dir overlap (including .. aliases), unowned dirs, blocked stamp commits, and imported-hints.txt, plus cargo check --release -p larp (0 new warnings).

**@⁠iMarllun** on GitHub · 2026-12-06 03:28 UTC · [original](https://github.com/gamingwithhomie/larp/pull/578#issuecomment-7450731501) Thanks for the thorough review @⁠christopear — addressed every point in bed22b: 1. **Byte-identical shader_cache.rs**: moved all stamp validation and cleanup into a dedicated module crates/larp-gpu/src/cache_clean.rs (invoked once from pipeline.rs right after probe_view_limit()) so crates/larp-gpu/src/shader_cache.rs is completely untouched and upgrading does not cause an avoidable cold shader cache. 2. **Preflight before any mutation**: both sync_stamp() and clean_cache() share preflight_targets(), which resolves paths/aliases (including ..), enforces larp_core::ensure_outside_repo() (.larp-root exclusion), rejects any overlap with data_dir in either direction, and refuses symlinks / Windows reparse points before touching any file. 3. **Strictly owned matchers & narrower automatic pruning**: - Stale staging dirs must match .larp-stale-<pid>-<nanos> (never touches .stale-notes). - Blob dirs must match exact lobs, lobs-pix, lobs-<16-hex>, or lobs-windows-v1-<64-hex>-<16-hex>-(plain|pix) (never touches lobs-backup). - Automatic upgrade pruning on translator mismatch now only prunes provably unreachable cache/shaders/<00..ff> buckets by default; owned pipelines/blobs* and catalog files are only pruned via explicit larp clean-cache or opt-in LARP_CLEAN_PIPELINES_ON_UPGRADE=1. 4. **No repeated cold starts on stamp write failure**: commit_stamp() now commits the new ranslator.stamp atomically **before** discarding old buckets; if committing the stamp fails (e.g. blocked path or read-only dir), it returns an error without deleting any cache entries. 5. **PR #530 compatibility**: whenever created-pipelines.bin is reset (clean-cache --all or LARP_CLEAN_PIPELINES_ON_UPGRADE=1), imported-hints.txt is removed alongside it so hints re-import cleanly. Re-verified with 7 unit tests in cargo test -p larp-gpu --lib cache_clean (all passing) covering .larp-root, data_dir overlap (including .. aliases), unowned dirs, blocked stamp commits, and imported-hints.txt, plus cargo check --release -p larp (0 new warnings).
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/578/head:github-bot/gh-578'
git switch 'github-bot/gh-578'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-578'
git switch 'github-bot/gh-578'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-578'
git switch 'github-bot/gh-578'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-578'
git switch 'main'
git merge --squash 'github-bot/gh-578'
git switch 'main'
git merge --ff-only 'github-bot/gh-578'
git switch 'main'
git merge 'github-bot/gh-578'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.