From 26f8a6c5b04e317afd934331d10087056c273386 Mon Sep 17 00:00:00 2001 From: AIEN Atlas Date: Fri, 16 Oct 2026 02:42:23 -0500 Subject: [PATCH 1/2] fix(aien-test): a receipt is reused only for the commit it was made at (sc#517) Evidence: cargo test -p aien-test: 271 lib + 37 integration tests pass; new test a_result_is_bound_to_its_commit_and_never_reused_for_another (a README-only commit no longer gets the old commit's PASS, the gate runs again; the same commit still reuses). clippy -D warnings clean. Direct runs keyed reuse only on each gate's declared inputs, so a gate that reads undeclared files (SC-TEST reads README.md and COVENANT.md) could receive an older commit's PASS for a commit that would fail. The worker already reuses only for the same commit (#516); cache::usable now applies the same rule everywhere: a receipt made at another commit is Unusable::OtherCommit. lookup() takes the commit; run, plan (Cached section) and why pass it. Three cache tests that encoded cross-commit reuse are rewritten to the commit rule. Worker and pipeline files are untouched. Fixes #517 Co-authored-by: Drake Stapleton Co-Authored-By: Claude Fable 5.1 --- tools/aien-test/docs/LOCAL_VERIFICATION.md | 8 +- tools/aien-test/src/cache.rs | 105 +++++++++++++++------ tools/aien-test/src/plan.rs | 7 +- tools/aien-test/src/runner.rs | 2 +- 4 files changed, 88 insertions(+), 34 deletions(-) diff --git a/tools/aien-test/docs/LOCAL_VERIFICATION.md b/tools/aien-test/docs/LOCAL_VERIFICATION.md index b51f5b9c..dfb19791 100644 --- a/tools/aien-test/docs/LOCAL_VERIFICATION.md +++ b/tools/aien-test/docs/LOCAL_VERIFICATION.md @@ -255,9 +255,11 @@ The one rule: commit runs again with every sibling at the same commit; a sibling that moved runs every gate fresh. A declared sibling that cannot be made turns the cache off for the head (`no_cache`). Results are never reused across - commits in the worker: gates read files they do not declare as `inputs` - (SC-TEST reads the root `README.md` and `COVENANT.md`), so a key built - from declared inputs alone could hand a commit another commit's PASS. + commits, in the worker or in a direct `aien-test run`: gates read files + they do not declare as `inputs` (SC-TEST reads the root `README.md` and + `COVENANT.md`), so a key built from declared inputs alone could hand a + commit another commit's PASS. A receipt stands only for the commit it was + made at; `why` reports such a receipt as "made at another commit". `wt.sh remove` takes the whole run directory away (checkout and siblings) and prunes every mirror's worktree list. An older `wt.sh` that still makes diff --git a/tools/aien-test/src/cache.rs b/tools/aien-test/src/cache.rs index 6a2e00cb..317bfb74 100644 --- a/tools/aien-test/src/cache.rs +++ b/tools/aien-test/src/cache.rs @@ -26,10 +26,17 @@ //! receipts themselves: every receipt already carries `volatile.cache.key`, //! so a separate `index.jsonl` would only be a second copy that could drift. //! +//! A result is reused only for the same commit it was made at (sc#517). The +//! key covers a gate's declared inputs, but a gate may read files it did not +//! declare (SC-TEST reads README.md and COVENANT.md and searches the tree), +//! so an older commit's PASS could stand in for a commit that would fail. +//! Direct runs now follow the worker's rule: same commit, same pins, or the +//! gate runs again. Cross-commit reuse returns only once every gate's inputs +//! are proven complete (ADR 0028 cache key). +//! //! Choices where the ADR is silent or contradicts itself (recorded in the PR): -//! - A reused receipt is the source receipt's `core` with one change: its -//! `commit` is the commit being checked. The ADR says "identical core" and -//! also "records that this commit was checked"; both cannot hold, and +//! - A reused receipt is the source receipt's `core` with its `commit` set to +//! the commit being checked (the same commit, since reuse is bound to it). //! `why`, `run` and the dependency lookups all match receipts by commit. //! - Dependencies enter the key as receipt digests, resolved to the original //! fresh receipt when the dependency itself was reused. Without that step a @@ -167,6 +174,9 @@ pub enum Unusable { UncleanTree, /// HEAD moved during the run. HeadMoved, + /// It was made at another commit; a result stands only for its own + /// commit (sc#517). + OtherCommit, /// The run went past its time limit. TimedOut, /// The key written in the receipt is not what its own contents hash to. @@ -186,6 +196,9 @@ impl Unusable { Unusable::NotAnExperiment => "its verdict is not PASS or FAIL", Unusable::UncleanTree => "the tree had uncommitted changes when it ran", Unusable::HeadMoved => "HEAD moved while it ran", + Unusable::OtherCommit => { + "it was made at another commit (a result stands only for its own commit)" + } Unusable::TimedOut => "it ran past its time limit", Unusable::KeyMismatch => "its recorded key does not match its own contents", Unusable::MachineMismatch => "its machine descriptor does not match its machine digest", @@ -195,19 +208,23 @@ impl Unusable { } } -/// May `src` (which carries `key`) stand in for a run now? Fail closed: the -/// first problem found makes it unusable. +/// May `src` (which carries `key`) stand in for a run at `commit` now? Fail +/// closed: the first problem found makes it unusable. pub fn usable( src: &Stored, key: &str, index: &Index, store: &Store, env: &Value, + commit: &str, ) -> Result<(), Unusable> { let (core, vol) = (&src.value["core"], &src.value["volatile"]); if vol["cache"]["reused"] != false { return Err(Unusable::AlreadyReused); } + if src.commit() != commit { + return Err(Unusable::OtherCommit); + } if !matches!(src.verdict(), Some(Verdict::Pass | Verdict::Fail)) { return Err(Unusable::NotAnExperiment); } @@ -270,14 +287,21 @@ pub enum Lookup<'a> { Miss(Miss), } -/// The newest usable fresh receipt whose `cache.key` is `key`. -pub fn lookup<'a>(index: &'a Index, store: &Store, key: &str, env: &Value) -> Lookup<'a> { +/// The newest usable fresh receipt whose `cache.key` is `key`, made at +/// `commit`. +pub fn lookup<'a>( + index: &'a Index, + store: &Store, + key: &str, + env: &Value, + commit: &str, +) -> Lookup<'a> { let mut first_rejection: Option = None; for r in index.receipts().iter().rev() { if r.value["volatile"]["cache"]["key"] != key { continue; } - match usable(r, key, index, store, env) { + match usable(r, key, index, store, env, commit) { Ok(()) => return Lookup::Hit(r), Err(why) => { first_rejection.get_or_insert(Miss::Unusable { @@ -422,7 +446,7 @@ pub fn explain_chain( }; match key(&now, index) { Err(e) => lines.push(format!("cache: miss, cannot build the key ({e})")), - Ok(k) => match lookup(index, &store, &k, &env) { + Ok(k) => match lookup(index, &store, &k, &env, commit) { Lookup::Hit(src) => { lines.push(format!( "cache: hit, a run would reuse receipt {} ({}, made at commit {}); key {}", @@ -614,17 +638,21 @@ mod tests { } #[test] - fn reuse_across_commits_records_the_commit_that_was_checked() { + fn a_result_is_bound_to_its_commit_and_never_reused_for_another() { + // sc#517: the key covers only declared inputs, and a gate may read + // files it did not declare, so an older commit's PASS must not stand + // in for a new commit even when every declared input is unchanged. let fx = Fx::new(SCRIPT); let first = fresh_pass(&fx); let old_head = fx.head(); - // A change to a file the gate does not depend on. + // A change to a file the gate does not declare. fx.commit("README", "changed\n"); assert_ne!(fx.head(), old_head); let second = fx.run(&fx.opts()); - assert_eq!(reused_from(&second), first.receipt_digest.as_deref()); - assert_eq!(fx.runs("a"), 1); - let (mut c1, mut c2) = ( + assert_eq!(reused_from(&second), None, "no reuse across commits"); + assert_eq!(second.verdict, Verdict::Pass); + assert_eq!(fx.runs("a"), 2, "the gate ran again at the new commit"); + let (c1, c2) = ( fx.receipt(&first)["core"].clone(), fx.receipt(&second)["core"].clone(), ); @@ -632,11 +660,19 @@ mod tests { assert_eq!( c2["commit"], fx.head(), - "the reuse receipt is for this commit" + "the fresh receipt is for this commit" + ); + // The same key, so only the commit rule kept the second run fresh. + let r1 = fx.receipt(&first); + let r2 = fx.receipt(&second); + assert_eq!( + r1["volatile"]["cache"]["key"], + r2["volatile"]["cache"]["key"] ); - c1["commit"] = Value::Null; - c2["commit"] = Value::Null; - assert_eq!(c1, c2, "everything else in core is the source's"); + // Back at the new commit a third run reuses the second (same commit). + let third = fx.run(&fx.opts()); + assert_eq!(reused_from(&third), second.receipt_digest.as_deref()); + assert_eq!(fx.runs("a"), 2); } #[test] @@ -939,24 +975,29 @@ mod tests { } #[test] - fn a_change_under_a_dependency_reruns_it_and_everything_that_rests_on_it() { + fn a_new_commit_reruns_the_whole_chain_and_the_same_commit_reuses_it() { + // sc#517: a result stands only for its own commit, so any new commit + // reruns every gate, whatever its declared inputs did. Within one + // commit the chain is reused as a whole. let fx = two_gate_fixture(); run_all(&fx, &fx.opts()); - // Gate A's own input (data/ is shared by both gates here, so give B a - // different one first to see that only the chain moves). let b = gate_text("C-B", &["C-A"], &fx.counter("b"), "").replace("data/", "other/"); fx.commit("other/x.txt", "x\n"); fx.commit("tests/b.gate", &b); - run_all(&fx, &fx.opts()); - assert_eq!((fx.runs("a"), fx.runs("b")), (1, 2), "only B changed"); - fx.commit("data/in.txt", "two\n"); let after = run_all(&fx, &fx.opts()); assert!(after.values().all(|o| reused_from(o).is_none())); assert_eq!( (fx.runs("a"), fx.runs("b")), - (2, 3), - "A changed, so B reruns too" + (2, 2), + "a new commit: A reruns although its declared inputs are unchanged, and B with it" ); + let again = run_all(&fx, &fx.opts()); + assert!(again.values().all(|o| reused_from(o).is_some())); + assert_eq!((fx.runs("a"), fx.runs("b")), (2, 2), "same commit: reused"); + fx.commit("data/in.txt", "two\n"); + let after = run_all(&fx, &fx.opts()); + assert!(after.values().all(|o| reused_from(o).is_none())); + assert_eq!((fx.runs("a"), fx.runs("b")), (3, 3)); } // ---- the key itself ------------------------------------------------- @@ -1053,7 +1094,8 @@ mod tests { let store = Store::new(dir.path()); let index = Index::load(&store); let k = original["volatile"]["cache"]["key"].as_str().unwrap(); - match lookup(&index, &store, k, &runner::captured_env()) { + let commit = original["core"]["commit"].as_str().unwrap(); + match lookup(&index, &store, k, &runner::captured_env(), commit) { Lookup::Hit(_) => Err(()), Lookup::Miss(Miss::Unusable { why, .. }) => Ok(why), Lookup::Miss(Miss::NoReceipt) => panic!("the forged receipt must be found by its key"), @@ -1167,13 +1209,18 @@ mod tests { "{before}" ); let first = fresh_pass(&fx); - fx.commit("README", "changed\n"); let hit = why_lines(&fx); assert!(hit.contains("cache: hit"), "{hit}"); assert!( hit.contains(&first.receipt_digest.clone().unwrap()[..12]), "{hit}" ); + // sc#517: a new commit is a miss even when no declared input changed. + fx.commit("README", "changed\n"); + let other = why_lines(&fx); + assert!(other.contains("cache: miss"), "{other}"); + assert!(other.contains("made at another commit"), "{other}"); + assert!(!other.contains("these changed"), "{other}"); fx.commit("data/in.txt", "two\n"); let miss = why_lines(&fx); assert!(miss.contains("cache: miss"), "{miss}"); @@ -1202,7 +1249,7 @@ mod tests { fn why_for_a_dependent_follows_a_reused_dependency() { let fx = two_gate_fixture(); run_all(&fx, &fx.opts()); - fx.commit("README", "changed\n"); + // Same commit: B's dependency A is a hit, so B is one too. let g = graph::load(fx.repo.path()).unwrap(); let index = Index::load(&fx.store()); let b = g.index_of("C-B").unwrap(); diff --git a/tools/aien-test/src/plan.rs b/tools/aien-test/src/plan.rs index 7b3323b1..9bc9c36c 100644 --- a/tools/aien-test/src/plan.rs +++ b/tools/aien-test/src/plan.rs @@ -858,6 +858,11 @@ pub fn probe_cached(root: &Path, graph: &Graph, opts: &crate::runner::Options) - let env = crate::runner::captured_env(); let mut stands_on: BTreeMap> = BTreeMap::new(); let mut out = BTreeSet::new(); + // A result stands only for its own commit (sc#517); without a head + // nothing is cached. + let Ok(head) = crate::runner::head(root) else { + return out; + }; for &i in graph.order() { let node = graph.node(i); let m = &node.manifest; @@ -892,7 +897,7 @@ pub fn probe_cached(root: &Path, graph: &Graph, opts: &crate::runner::Options) - args_override: None, }; if let Ok(k) = cache::key(&now, &index) { - if let Lookup::Hit(src) = cache::lookup(&index, &store, &k, &env) { + if let Lookup::Hit(src) = cache::lookup(&index, &store, &k, &env, &head) { if src.verdict() == Some(Verdict::Pass) { reuse = Some(src.digest.clone()); out.insert(node.id.clone()); diff --git a/tools/aien-test/src/runner.rs b/tools/aien-test/src/runner.rs index 796de254..e4ba52c3 100644 --- a/tools/aien-test/src/runner.rs +++ b/tools/aien-test/src/runner.rs @@ -656,7 +656,7 @@ fn execute_gate(job: &Job<'_>, permit: Option>) -> Result