Skip to content

recipes: reuse stable source hashes during reach validation - #875

Merged
juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q38-t0-speed
Oct 27, 2026
Merged

juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q38-t0-speed

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

contrib-check --tier t0의 입력 검증이 같은 소스 파일을 반복해서 읽고 해시하던 비용을 줄입니다.

What and why

Validating a recorded reach key walks the source corpus and hashes every .rs, .toml, .py and Cargo.lock file, and the same files were read and hashed again on every later validation in the same process. This reuses the existing HashCache for those reads, so a file whose size and modification time are unchanged is hashed once per process. A rewrite with the same size and modification time still invalidates its consumers, and recently modified files are hashed again.

Release notes: internal, the t0 check tier finishes sooner; no behavior change.

Code shape

  • No second copy. No new hashing code: _file and _linked in tools/bloomery/reach_cache.py now take the tree's existing HashCache instead of reading and hashing on their own. Every caller was updated.
  • Model code holds only architecture facts. No model code changes.
  • Dead code is gone. The unconditional read-and-hash in _file is replaced, not kept beside the cached one.
  • No silent failure. The corpus is still walked in full and symlinked source directories are still followed.
  • Comments state what is true now.
  • No relaxed gate. No gate, pin or check list changed.
  • cargo fmt and cargo clippy are clean on what I touched; the lint count did not go up. There is no Rust change in this PR.

Duplication found or removed (path:line): none.

What I ran

New test: test_repeated_key_validation_reuses_stable_source_hashes in tools/recipes-key-test.py. It computes a key twice, checks that the second validation does not reopen an unchanged source file, then rewrites that file with the same size and modification time and checks that the key changes.

python3 tools/recipes-key-test.py --self-test
Ran 26 tests in 59.817s

Putting the old unconditional read back in _file makes the new test fail.

I timed the full t0 tier, bash tools/contrib-check.sh --tier t0, with the changed files taken from the base and from the head in turn, main then head then main then head, after one untimed warm-up run:

main: DONE tier=t0 total=37 red=0 wall=239s
head: DONE tier=t0 total=37 red=0 wall=212s
main: DONE tier=t0 total=37 red=0 wall=237s
head: DONE tier=t0 total=37 red=0 wall=210s
Metric Base Head Difference Ranges overlap
t0 wall time, median of two runs 238 s 211 s -27 s (-11.3%) no

The timing base is upstream c21e3b81eb7f3ede965e5ac68f325b8a59cb2e05, with this patch on it as the head. The branch was later rebased onto 11b3f4f3049e9a56b8ad61ea92caf9e0c6aed97e. The patch is unchanged by the rebase (git range-diff shows =), and I did not rerun the timing.

I did not run the GPU gates, since this change touches no GPU or Rust code.

Hardware

The runs used one machine:

host: Linux 7.0.0-38-generic x86_64, AMD Ryzen 9 9950X 16-Core Processor
toolchain: rustc 1.99.1 (58cf8f9c3 2026-10-13) (1.99.1); RUSTFLAGS: -C target-cpu=native

AI help

AI 코딩 에이전트가 코드를 작성한 후 설명을 썼고, 테스트를 마쳤습니다. diff 리뷰와 PR 초안 리뷰는 제가 직접 했습니다.

@github-actions github-actions Bot added documentation Improvements or additions to documentation tools Tools, recipes, CI and the toolchain: tools/, justfile, .github/, Cargo labels Oct 27, 2026

@github-actions github-actions Bot 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.

Verdict: CLEAN: the change reuses the existing HashCache for source hashes, and I found no input where a stale hash can be served.

Non-blocking

⚠ A file that keeps changing now raises RecipeError, not "source changed": tools/bloomery/reach_cache.py:51. The old _bytes raised OSError when a file moved during the read, and current() turned that into False (the "request a fresh key" path). HashCache.sha goes through hash_stable, which raises RecipeError after 3 tries, and current() catches only OSError, ValueError. The error escapes from key_inputs and validate, and its text says "something writes $BLOOMERY_DATA", which is wrong for a source file. This needs a file rewritten three times in a row during one read, so it is rare. Direction: in _file, catch RecipeError and re-raise it as OSError, or accept the new error and say so in the PR body.

⚠ _corpus resets a field of another module's object: tools/bloomery/reach_cache.py:61. cache.t0_ns = time.time_ns() is correct (it keeps the 2 s racy window current in a long-lived process), but nothing says why. Add one line of why, for example "the racy window is measured from this walk, not from tree creation".

Checked

  • Duplicates: 0 new functions; _file and _linked take the existing HashCache (recipes.py:9152). No dryer pairs (no Rust change; the tools did not run).
  • Staleness path: the cache key is (size, mtime, ctime, inode, device). A same-size, same-mtime rewrite changes ctime, so it is re-read. Files newer than t0_ns - RACY_NS are never cached. The digest is the same sha256 over raw bytes as before, so the key value does not change.
  • Silent failure: 4 sites followed (_file, _linked, _corpus, _holds). A missing or broken file still raises OSError from os.stat, as before. The one change is the ⚠ above.
  • Tests: the new test protects HashCache.sha being used in _file. Mutant "unconditional read in _file" turns the assert not reads red. Mutant "drop ctime from ident" turns the after != before assert red, because the utime call restores only mtime. Mutant "_holds uncached" is covered only if the file is an observed read; the corpus walk covers it anyway.
  • Gates: no pin, band or check list moved.
  • Performance: tool code, not a decode or prefill hot path. The 2-run A/B in the PR body is thin but matches the claim.
  • Escape catalog: no pattern matched (no shell changes).

Notes

  • No Rust or GPU code changed, so gpu.json has no card evidence.
  • The timing ran on the pre-rebase base; the PR body says the patch is unchanged (range-diff =).

🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)

@juliankang4

Copy link
Copy Markdown
Collaborator Author

The review is done and the contribution check passed, so I'm merging this.

@juliankang4
juliankang4 merged commit 12dee75 into midagedev:main Oct 27, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation tools Tools, recipes, CI and the toolchain: tools/, justfile, .github/, Cargo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant