aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / aien-closure / tests / declaration_codes.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! SPEC section 6.2 of aien-protocols `specs/verified-crumb/SPEC.md`: one |
| 17 | //! meaning per declaration code. One test per row of the table, each with a |
| 18 | //! negative twin that fails if the row's code is swapped for a neighbouring |
| 19 | //! code (a mutant that returns the wrong code is killed by the twin). |
| 20 | //! |
| 21 | //! Rows 5a and 5b (a VC's stored source imports against `dependencies[]`) are |
| 22 | //! covered by the unit test `step8_stored_source_imports_against_dependencies` |
| 23 | //! in `src/declare.rs`: this crate reads no VCs, the same set comparison is |
| 24 | //! what step 8 needs. |
| 25 | |
| 26 | mod common; |
| 27 | |
| 28 | use aien_closure::Code; |
| 29 | use aien_proof::evidence::{receipt_id, store_receipt, Receipt}; |
| 30 | use common::*; |
| 31 | use std::collections::BTreeSet; |
| 32 | use std::path::Path; |
| 33 | |
| 34 | fn only(root: &Path, store: &Path, want: Code) { |
| 35 | expect_only(root, store, want); |
| 36 | } |
| 37 | |
| 38 | /// Change a manifest line in place. |
| 39 | fn edit_manifest(root: &Path, krate: &str, from: &str, to: &str) { |
| 40 | let rel = format!("crates/{krate}/closure.toml"); |
| 41 | let text = read(root, &rel); |
| 42 | assert!(text.contains(from), "{from:?} not in {text}"); |
| 43 | write(root, &rel, &text.replace(from, to)); |
| 44 | } |
| 45 | |
| 46 | /// Re-qualify `krate` with an extra receipt dependency on `dep`, and declare |
| 47 | /// it in the manifest, so the only thing wrong is that `dep` is not an edge. |
| 48 | fn declare_non_edge( |
| 49 | root: &Path, |
| 50 | store: &Path, |
| 51 | ids: &std::collections::BTreeMap<String, String>, |
| 52 | krate: &str, |
| 53 | dep: &str, |
| 54 | ) { |
| 55 | let p = store.join("receipts").join(format!("{}.json", ids[krate])); |
| 56 | let mut r: Receipt = serde_json::from_str(&std::fs::read_to_string(p).unwrap()).unwrap(); |
| 57 | r.dependencies.push(ids[dep].clone()); |
| 58 | r.id = receipt_id(&r).unwrap(); |
| 59 | store_receipt(store, &r).unwrap(); |
| 60 | edit_manifest(root, krate, &ids[krate], &r.id); |
| 61 | let rel = format!("crates/{krate}/closure.toml"); |
| 62 | let text = read(root, &rel); |
| 63 | write(root, &rel, &format!("{}dep {dep} {}\n", text, ids[dep])); |
| 64 | } |
| 65 | |
| 66 | fn found(root: &Path, store: &Path) -> BTreeSet<Code> { |
| 67 | codes(root, store) |
| 68 | } |
| 69 | |
| 70 | // Row 1: an import with no pin, or a pin that is not the dependency's own receipt. |
| 71 | |
| 72 | #[test] |
| 73 | fn row1_edge_without_receipt_pin_is_not_pinned() { |
| 74 | let (root, store, ids) = seeded(&Knobs::default()); |
| 75 | edit_manifest( |
| 76 | &root, |
| 77 | "alpha", |
| 78 | &format!("dep beta {}", ids["beta"]), |
| 79 | "dep beta", |
| 80 | ); |
| 81 | only(&root, &store, Code::DependencyNotPinned); |
| 82 | } |
| 83 | |
| 84 | #[test] |
| 85 | fn row1_not_undeclared_and_not_unverified_twin() { |
| 86 | // Same situation, negative twin: a missing pin is neither an undeclared |
| 87 | // edge (the edge IS declared) nor an extra declaration. |
| 88 | let (root, store, ids) = seeded(&Knobs::default()); |
| 89 | edit_manifest( |
| 90 | &root, |
| 91 | "alpha", |
| 92 | &format!("dep beta {}", ids["beta"]), |
| 93 | "dep beta", |
| 94 | ); |
| 95 | let got = found(&root, &store); |
| 96 | assert!(!got.contains(&Code::UndeclaredImport), "{got:?}"); |
| 97 | assert!(!got.contains(&Code::UnverifiedDependency), "{got:?}"); |
| 98 | } |
| 99 | |
| 100 | #[test] |
| 101 | fn row1_pin_that_is_not_the_dependencys_receipt_is_not_pinned() { |
| 102 | let (root, store, ids) = seeded(&Knobs::default()); |
| 103 | edit_manifest( |
| 104 | &root, |
| 105 | "alpha", |
| 106 | &format!("dep beta {}", ids["beta"]), |
| 107 | &format!("dep beta {}", ids["gamma"]), |
| 108 | ); |
| 109 | only(&root, &store, Code::DependencyNotPinned); |
| 110 | } |
| 111 | |
| 112 | // Row 2: a lock line for something the component does not depend on. |
| 113 | |
| 114 | #[test] |
| 115 | fn row2_lock_line_with_no_edge_is_unverified_dependency() { |
| 116 | let (root, store, ids) = seeded(&Knobs::default()); |
| 117 | let lock = read(&root, "closure.lock"); |
| 118 | // gamma has no dependencies, so `gamma needs alpha` is a pin with no edge. |
| 119 | write( |
| 120 | &root, |
| 121 | "closure.lock", |
| 122 | &format!("{lock}needs gamma alpha {}\n", ids["alpha"]), |
| 123 | ); |
| 124 | only(&root, &store, Code::UnverifiedDependency); |
| 125 | } |
| 126 | |
| 127 | #[test] |
| 128 | fn row2_not_undeclared_and_not_not_pinned_twin() { |
| 129 | let (root, store, ids) = seeded(&Knobs::default()); |
| 130 | let lock = read(&root, "closure.lock"); |
| 131 | write( |
| 132 | &root, |
| 133 | "closure.lock", |
| 134 | &format!("{lock}needs gamma alpha {}\n", ids["alpha"]), |
| 135 | ); |
| 136 | let got = found(&root, &store); |
| 137 | assert!(!got.contains(&Code::UndeclaredImport), "{got:?}"); |
| 138 | assert!(!got.contains(&Code::DependencyNotPinned), "{got:?}"); |
| 139 | } |
| 140 | |
| 141 | #[test] |
| 142 | fn row2_wrong_id_and_missing_line_stay_not_pinned() { |
| 143 | // The mirror cases are pin problems, not extra pins: a known pair with |
| 144 | // another id, and a missing line. |
| 145 | let (root, store, ids) = seeded(&Knobs::default()); |
| 146 | let lock = read(&root, "closure.lock"); |
| 147 | write( |
| 148 | &root, |
| 149 | "closure.lock", |
| 150 | &lock.replace( |
| 151 | &format!("needs alpha gamma {}", ids["gamma"]), |
| 152 | &format!("needs alpha gamma {}", ids["alpha"]), |
| 153 | ), |
| 154 | ); |
| 155 | only(&root, &store, Code::DependencyNotPinned); |
| 156 | let dropped: String = lock |
| 157 | .lines() |
| 158 | .filter(|l| !l.starts_with("needs alpha gamma")) |
| 159 | .map(|l| format!("{l}\n")) |
| 160 | .collect(); |
| 161 | write(&root, "closure.lock", &dropped); |
| 162 | only(&root, &store, Code::DependencyNotPinned); |
| 163 | } |
| 164 | |
| 165 | #[test] |
| 166 | fn row2_extra_pin_and_missing_pin_together_report_both() { |
| 167 | let (root, store, ids) = seeded(&Knobs::default()); |
| 168 | let lock = read(&root, "closure.lock"); |
| 169 | let mut edited: String = lock |
| 170 | .lines() |
| 171 | .filter(|l| !l.starts_with("needs alpha gamma")) |
| 172 | .map(|l| format!("{l}\n")) |
| 173 | .collect(); |
| 174 | edited.push_str(&format!("needs gamma alpha {}\n", ids["alpha"])); |
| 175 | write(&root, "closure.lock", &edited); |
| 176 | assert_eq!( |
| 177 | found(&root, &store), |
| 178 | BTreeSet::from([Code::UnverifiedDependency, Code::DependencyNotPinned]) |
| 179 | ); |
| 180 | } |
| 181 | |
| 182 | // Row 3: a declared dependency that is not an actual edge. |
| 183 | |
| 184 | #[test] |
| 185 | fn row3_declared_dep_that_is_not_an_edge_is_unverified_dependency() { |
| 186 | // alpha depends on beta only; it now also declares gamma (a transitive |
| 187 | // dependency, not an edge of alpha). |
| 188 | let (root, store, ids) = seeded(&Knobs::default()); |
| 189 | declare_non_edge(&root, &store, &ids, "alpha", "gamma"); |
| 190 | only(&root, &store, Code::UnverifiedDependency); |
| 191 | } |
| 192 | |
| 193 | #[test] |
| 194 | fn row3_not_undeclared_twin() { |
| 195 | let (root, store, ids) = seeded(&Knobs::default()); |
| 196 | declare_non_edge(&root, &store, &ids, "alpha", "gamma"); |
| 197 | let got = found(&root, &store); |
| 198 | assert!(!got.contains(&Code::UndeclaredImport), "{got:?}"); |
| 199 | assert!(!got.contains(&Code::DependencyNotPinned), "{got:?}"); |
| 200 | } |
| 201 | |
| 202 | #[test] |
| 203 | fn row3_declared_dep_without_pin_that_is_not_an_edge_is_still_unverified() { |
| 204 | let (root, store, _) = seeded(&Knobs::default()); |
| 205 | let text = read(&root, "crates/alpha/closure.toml"); |
| 206 | write( |
| 207 | &root, |
| 208 | "crates/alpha/closure.toml", |
| 209 | &format!("{text}dep gamma\n"), |
| 210 | ); |
| 211 | only(&root, &store, Code::UnverifiedDependency); |
| 212 | } |
| 213 | |
| 214 | // Row 4: an actual edge the manifest does not declare. |
| 215 | |
| 216 | fn undeclared_edge() -> (std::path::PathBuf, std::path::PathBuf) { |
| 217 | let (root, store, _) = seeded(&Knobs::default()); |
| 218 | let m = read(&root, "crates/alpha/Cargo.toml"); |
| 219 | write( |
| 220 | &root, |
| 221 | "crates/alpha/Cargo.toml", |
| 222 | &m.replace( |
| 223 | "[dependencies]", |
| 224 | "[dependencies]\ngamma = { path = \"../gamma\" }", |
| 225 | ), |
| 226 | ); |
| 227 | // Re-seed against the new graph, then drop the declaration again, so the |
| 228 | // undeclared edge is the only thing wrong. |
| 229 | seed(&root, &store, &Knobs::default()); |
| 230 | let man = read(&root, "crates/alpha/closure.toml"); |
| 231 | let kept: String = man |
| 232 | .lines() |
| 233 | .filter(|l| !l.starts_with("dep gamma")) |
| 234 | .map(|l| format!("{l}\n")) |
| 235 | .collect(); |
| 236 | write(&root, "crates/alpha/closure.toml", &kept); |
| 237 | (root, store) |
| 238 | } |
| 239 | |
| 240 | #[test] |
| 241 | fn row4_actual_edge_not_declared_is_undeclared_import() { |
| 242 | let (root, store) = undeclared_edge(); |
| 243 | only(&root, &store, Code::UndeclaredImport); |
| 244 | } |
| 245 | |
| 246 | #[test] |
| 247 | fn row4_not_unverified_and_not_not_pinned_twin() { |
| 248 | let (root, store) = undeclared_edge(); |
| 249 | let got = found(&root, &store); |
| 250 | assert!(!got.contains(&Code::UnverifiedDependency), "{got:?}"); |
| 251 | assert!(!got.contains(&Code::DependencyNotPinned), "{got:?}"); |
| 252 | } |
| 253 | |
| 254 | // Rows 1 to 4 are four different codes, so a mutant that merges two of them |
| 255 | // is caught by the exit codes. |
| 256 | |
| 257 | #[test] |
| 258 | fn rows_1_to_4_map_to_four_distinct_exit_codes() { |
| 259 | let exits: BTreeSet<i32> = [ |
| 260 | Code::DependencyNotPinned, |
| 261 | Code::UnverifiedDependency, |
| 262 | Code::UndeclaredImport, |
| 263 | ] |
| 264 | .iter() |
| 265 | .map(|c| c.exit_code()) |
| 266 | .collect(); |
| 267 | assert_eq!(exits, BTreeSet::from([13, 10, 16])); |
| 268 | } |