Skip to content

engram: verify prefetch advice independently of row reads - #670

Merged
juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:fix-review-bot-issue-662-engram
Oct 22, 2026
Merged

juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:fix-review-bot-issue-662-engram

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

engram의 prefetch가 실제로 어떤 페이지를 커널에 요청하는지 직접 검사하는 시험을 더해서, prefetch가 빠지거나 한 페이지 어긋나도 시험이 실패하게 했습니다.

What and why

Closes #662.

The engram byte-comparison test reads rows back and compares bytes, so it does not notice when prefetch advises the wrong pages or none at all. This change adds a test that checks the madvise calls themselves: the page address, the length and the advice kind that reach the system call. It does not depend on row reads or on populate side effects.

In test builds, TableBytes::advise_pages calls a small wrapper in place of libc::madvise. The wrapper forwards its arguments to the real call and records them per thread when the call succeeds, so parallel tests stay apart. Skipping the call or changing the address, length or advice changes the record. A normal build calls libc::madvise directly and has no recording code and no storage. The new test prefetch_advises_the_requested_page_runs runs Site::prefetch and the RowTable implementation's prefetch against a real file mapping. It computes the expected pages from the mapping address, a tensor base the test chooses and the row size, and does not call page_runs, pages or row for that.

The cases are small rows, rows of exactly one page, rows larger than a page, a tensor that starts in the middle of a page, duplicate, reversed, adjacent and distant rows, and the last row. For each call the test checks MADV_WILLNEED, page alignment of address and length, the exact page set, and that runs are merged with no duplicate page. An empty request and an out-of-range request must emit no call.

The comment in crates/engram/tests/engram.rs that claimed the byte comparison catches wrong prefetch ranges now says what it covers. The test replaces page_runs_advise_each_requested_page_once, whose page-run checks, empty input and rejected rows it covers. The hw_engram_copy_rows_matches_row hardware test gets the same comment correction. The byte comparison and residency tests stay as they are. No public API, I/O path or gate changes. The test-only wrapper adds one unsafe block and one unsafe fn, each with a safety comment, and tools/unsafe-ratchet.txt records the new count (engram 12 blocks, 1 fn) with a dated reason.

Release notes: internal: engram's prefetch test checks the pages advised to the kernel, so the test catches a missing or shifted prefetch

Code shape

  • No second copy. The advice and page merging stay in TableBytes. The test adds one test-only wrapper at the shared call and one test. The expected pages are computed from file coordinates, so the test does not repeat product logic.
  • Model code holds only architecture facts. No model code changes.
  • Dead code is gone. Nothing was made unused.
  • No silent failure. The test checks that an empty or out-of-range request emits no advice, and every file and call result in it is checked.
  • Comments state what is true now. The corrected comment in engram.rs describes only what the byte comparison proves.
  • No relaxed gate. No band, tolerance or lint moves. The unsafe count rises by the one block and one function the test-only wrapper needs, with a dated pin and a safety comment.
  • cargo fmt is clean on what I touched. cargo clippy was not run here.

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

What I ran

The commit is 5a69a91d on upstream main e53cabeb, run on a Linux host with no GPU.

FAIL first. I planted seven mutants one at a time and ran the new test with cargo test -p bloomery-engram --lib tests::prefetch_advises_the_requested_page_runs -- --exact --nocapture. Each one failed at the intended assertion, with rc 101 and one failed test:

Mutant Assertion that failed Result
the madvise call is replaced by let rc = 0; prefetch must advise the requested pages rc 101
the advice is MADV_NORMAL prefetch must use WILLNEED over whole pages rc 101
Site::prefetch returns Ok(()) without advising prefetch must advise the requested pages rc 101
the RowTable implementation's prefetch returns Ok(()) without advising prefetch must advise the requested pages rc 101
TableBytes::advise passes first + self.page prefetch must advise exactly the row pages rc 101
the madvise address in advise_pages is first + self.page prefetch must advise exactly the row pages rc 101
the madvise length in advise_pages is len + self.page prefetch must advise exactly the row pages rc 101

The first row is the case the issue describes: a prefetch with no advice that the byte comparison cannot see. The fifth row's failure shows a page set off by one: left: [33539142066], right: [33539142065].

Each mutant is restored after its run, and git diff --exit-code is clean afterwards. On the unchanged code:

cargo test -p bloomery-engram -- --nocapture
test result: ok. 22 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

The engram integration tests also compile and run; the 12 existing hardware tests stay ignored.

LC_ALL=C just contrib-check --tier t0
DONE tier=t0 total=107 red=0 wall=343s

cargo fmt -p bloomery-engram and git diff --check upstream/main...HEAD are clean. The checks ran on 5a69a91d.

The existing hardware tests need the original model checkpoint on disk and stay ignored. I did not use them as evidence for this change. The new test needs no model and no GPU.

What I ran into

This does not block this change.

The crate overview in crates/engram/src/lib.rs describes the prefetch cost as one syscall per row, while TableBytes::advise and Site::prefetch issue one per distinct page run. I left the text as it is, since it is a cost description and not a test gap. To check it, read the overview next to the new test's page-run expectations.

prefetch and the populate that follows it for the same ids each sort the ids, build the runs and merge pages again. I did not measure whether that host work matters and changed nothing there.

Hardware (if the change is for a machine)

A Linux host, CPU only. No GPU, model download or timing was involved, and this change touches no kernel, launch, device copy, sync or placement path. I made no speed claim.

AI help

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

@github-actions github-actions Bot added the host Host crates: qdot, threads, engram, engram-lab, runtime, sampler, levers, decision, vision, refset label Oct 22, 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 advice test checks syscall arguments independently of row reads, and normal builds retain the direct syscall.

Checked

Duplicates: 3 new items checked; no existing recorder or equivalent syscall test found. Dryer reported 0 pairs.
Complexity: crapper flagged no functions.
Silent failure: 1 syscall-result path followed; failed advice still returns a named SiteIo error.
Tests: assertions cover missing calls, shifted addresses, wrong lengths or advice at crates/engram/src/lib.rs:700, and bypassing either prefetch entry. The PR reports seven red mutant runs and 22 passing unit tests; Rust tests were not rerun here.
Boundaries: empty and rejected requests emit no advice; exact page sets and strict run separation catch omissions, duplicates and unmerged runs. Thread-local logs isolate parallel tests.
Gates: the replaced test and increased unsafe counts have dated PIN reasons; no tolerance moved.
Performance: recording and allocation compile only in test builds.
Escape catalog: C1–C4 do not match this diff.

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

Filed outside this PR: #673

@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 new assertions check prefetch advice independently of row reads, and non-test builds preserve the existing syscall.

Checked

  • Duplicates: 3 new items checked (recorder, thread-local storage, test), none duplicated. Dryer reported 0 pairs.
  • Complexity: crapper flagged no functions. Growth is test setup and assertions; production control flow is unchanged.
  • Silent failure: 2 return paths followed. The recorder preserves the syscall result and leaves errno untouched on failure; the caller returns EngramError::SiteIo.
  • Tests: at crates/engram/src/lib.rs:700, skipping the call fails the nonempty assertion; shifting the address or extending the length by one page fails exact-page equality. Changing the advice at crates/engram/src/lib.rs:710 fails the WILLNEED assertion. Duplicate and unmerged runs fail equality or separation assertions. Empty and invalid requests assert no calls. Integration-test edits change comments only.
  • Gates: the replaced test has a dated reason. The unsafe ratchet increases by one block and one function with a dated PIN and explanation. No tolerance moves.
  • Adversarial checks: thread-local recording separates parallel tests; expected pages come from mapping coordinates without row reads; last-row ranges fit the mapping.
  • Escape catalog: no C1–C4 patterns matched.

Notes

Rust tests and mutations were not run during validation. The mutation conclusions above follow from code inspection.

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

Filed outside this PR: #675

@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 d5b8c81 into midagedev:main Oct 22, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

host Host crates: qdot, threads, engram, engram-lab, runtime, sampler, levers, decision, vision, refset

Projects

None yet

Development

Successfully merging this pull request may close these issues.

engram: byte-comparison gate does not detect incorrect prefetch ranges

1 participant