Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/interplane-mv/blob/main/provenance/src/testrun.rs # interplane-mv INTERPLANE: open interoperability protocol between AI models and agent runtimes. Models express intent. Runtimes retain authority. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `interplane-mv / provenance / src / testrun.rs` 242 lines ยท 9378 bytes Raw Blame History 1 //! `vac-test-run/1`: the fix-the-test slice's extra evidence, and the `aien.native` claim. 2 //! Specification: provenance/BINDING.md ("Test run", "Source pin", "Native claim"). 3 //! 4 //! The daemon only writes the file; it does not run tests. The test run is HARNESS evidence: the 5 //! harness ran the task's test command before the write and after the daemon's ack, and retained 6 //! what it saw. This module checks that the retained records agree with each other and with the 7 //! daemon's own grant and ack. It cannot tell a harness export from a hand-written file (unsigned). 8 //! It reads and compares; it authorizes and repairs nothing. 9 10 use super::{fail, s, Archive, Fail}; 11 use crate::ledger; 12 use serde_json::Value; 13 use std::collections::BTreeSet; 14 15 pub const BINDING: &str = "vac-test-run/1"; 16 pub const MISSING_NATIVE: &str = "link:native"; 17 const RUN_KIND: &str = "vac-test-run"; 18 const PIN_KIND: &str = "vac-source-pin"; 19 20 fn is_hex64(x: &str) -> bool { 21 x.len() == 64 && x.bytes().all(|b| b.is_ascii_hexdigit()) 22 } 23 24 fn is_hex40(x: &str) -> bool { 25 x.len() == 40 && x.bytes().all(|b| b.is_ascii_hexdigit()) 26 } 27 28 fn native_of(m: &Value) -> Option<&Value> { 29 m.get("aien") 30 .and_then(|a| a.get("native")) 31 .filter(|n| !n.is_null()) 32 } 33 34 /// Missing links implied by a slice bundle: with a `test_run` section, the daemon must have 35 /// reported a native compose library (`aien.native.claimed: true`), else `link:native` is missing. 36 pub fn implied_missing(m: &Value) -> BTreeSet { 37 let mut out = BTreeSet::new(); 38 if m.get("test_run").is_some() { 39 let claimed = native_of(m).and_then(|n| n.get("claimed")) == Some(&Value::Bool(true)); 40 if !claimed { 41 out.insert(MISSING_NATIVE.to_string()); 42 } 43 } 44 out 45 } 46 47 /// True when the bundle carries a test run or the records of one (a task, a source pin). 48 pub fn claims_test_run(a: &Archive, m: &Value) -> bool { 49 m.get("test_run").is_some_and(|v| !v.is_null()) 50 || ["task", "source_pin", "test_run_record"] 51 .iter() 52 .any(|n| a.has(n)) 53 } 54 55 fn rec_json(a: &Archive, name: &str, code: &str) -> Result { 56 if !a.has(name) { 57 return fail(code, format!("record {name} absent")); 58 } 59 match a.json(name)? { 60 Some(v) => Ok(v), 61 None => fail(code, format!("record {name} not retained")), 62 } 63 } 64 65 /// Checks for the slice sections. `ledger_checked` is true when the effect link was the strong 66 /// ledger binding (whose checks already ran). 67 pub fn check(a: &Archive, m: &Value, ledger_checked: bool) -> Result<(), Fail> { 68 let section = m.get("test_run").filter(|v| !v.is_null()); 69 // A bundle that carries a task or a source pin claims a fix: the test run must be there. 70 let claims_fix = ["task", "source_pin", "test_run_record"] 71 .iter() 72 .any(|n| a.has(n)); 73 let Some(sec) = section else { 74 if claims_fix { 75 return fail( 76 "test_run_missing", 77 "task/source_pin/test_run_record present but no test_run section", 78 ); 79 } 80 return check_native(a, m); 81 }; 82 if !ledger_checked { 83 return fail( 84 "malformed_companion", 85 "test_run requires effect.binding aien-ledger-slice/1", 86 ); 87 } 88 if s(sec, &["binding"]) != Some(BINDING) { 89 return fail( 90 "unsupported_test_run", 91 format!( 92 "test_run.binding={}", 93 s(sec, &["binding"]).unwrap_or("absent") 94 ), 95 ); 96 } 97 let Some(rec_name) = s(sec, &["record"]) else { 98 return fail("test_run_missing", "test_run.record"); 99 }; 100 let run = rec_json(a, rec_name, "test_run_missing")?; 101 if s(&run, &["kind"]) != Some(RUN_KIND) || run.get("v").and_then(Value::as_u64) != Some(1) { 102 return fail("unsupported_test_run", "record kind/v"); 103 } 104 let task = rec_json(a, "task", "test_run_missing")?; 105 let facts = ledger::facts(a)?; 106 107 // Task scope: the task names the one file it allows to change, and the grant must write that 108 // file. Without it a run that edited the test instead of the code would still verify. 109 match s(&task, &["target_path"]) { 110 Some(t) if t == facts.path => {} 111 Some(t) => { 112 return fail( 113 "task_scope_mismatch", 114 format!("task.target_path={t} vs ledger_grant.path={}", facts.path), 115 ) 116 } 117 None => { 118 return fail( 119 "task_scope_mismatch", 120 "task.target_path absent: the task does not say which file may change", 121 ) 122 } 123 } 124 let pin = rec_json(a, "source_pin", "source_pin_mismatch")?; 125 126 // Source pin: the commit the task starts from, and the exact target bytes the grant replaced. 127 let pin_bad = |what: &str| -> Result<(), Fail> { fail("source_pin_mismatch", what) }; 128 if s(&pin, &["kind"]) != Some(PIN_KIND) { 129 return pin_bad("source_pin.kind"); 130 } 131 if s(&pin, &["target_path"]) != Some(facts.path.as_str()) { 132 return pin_bad("source_pin.target_path vs ledger_grant.path"); 133 } 134 match ( 135 s(&pin, &["target_blob_sha256"]), 136 facts.prior_sha256.as_deref(), 137 ) { 138 (Some(p), Some(g)) if is_hex64(p) && p == g => {} 139 _ => return pin_bad("source_pin.target_blob_sha256 vs ledger_grant.prior_sha256"), 140 } 141 if s(&pin, &["commit"]).is_none() || s(&pin, &["commit"]) != s(&task, &["repo_commit"]) { 142 return pin_bad("source_pin.commit vs task.repo_commit"); 143 } 144 145 // The test run. 146 let bad = |what: &str| -> Result<(), Fail> { fail("test_run_mismatch", what) }; 147 let tid = s(&task, &["task_id"]); 148 if tid.is_none() || s(sec, &["task_id"]) != tid || s(&run, &["task_id"]) != tid { 149 return bad("task_id (section, record and task must agree)"); 150 } 151 if run.get("argv") != task.get("test_cmd") || !run.get("argv").is_some_and(Value::is_array) { 152 return bad("argv vs task.test_cmd"); 153 } 154 match run.get("exit_code").and_then(Value::as_i64) { 155 Some(0) => {} 156 Some(c) => return bad(&format!("exit_code={c} (a fix must pass: exit 0)")), 157 None => return bad("exit_code"), 158 } 159 match run.get("test_exit_before").and_then(Value::as_i64) { 160 Some(0) => return bad("test_exit_before=0 (the test already passed: nothing was fixed)"), 161 Some(_) => {} 162 None => return bad("test_exit_before"), 163 } 164 match s(&run, &["target_blob_sha256_after"]) { 165 Some(b) if is_hex64(b) && b == facts.disk_sha256 && b == facts.content_sha256 => {} 166 _ => return bad("target_blob_sha256_after vs ledger_ack.disk_sha256"), 167 } 168 for (field, record) in [ 169 ("stdout_sha256", "test_stdout"), 170 ("stderr_sha256", "test_stderr"), 171 ] { 172 let Some(d) = s(&run, &[field]).filter(|d| is_hex64(d)) else { 173 return bad(field); 174 }; 175 // A claimed digest must be backed by retained bytes that hash to it. A missing entry, or 176 // one labelled not retained, would otherwise let any 64-hex value pass. 177 if !a.has(record) { 178 return bad(&format!( 179 "{record} record absent: {field} has nothing to match" 180 )); 181 } 182 match a.bytes(record)? { 183 None => return bad(&format!("{record} not retained: {field} cannot be checked")), 184 Some(_) if a.sha(record)? != d => return bad(&format!("{field} vs retained {record}")), 185 Some(_) => {} 186 } 187 } 188 check_native(a, m) 189 } 190 191 /// `aien.native`: the claim that the daemon's compose library was the native one, checked against 192 /// the retained `ComposeRecall` report (`compose_recall`). 193 fn check_native(a: &Archive, m: &Value) -> Result<(), Fail> { 194 let Some(n) = native_of(m) else { 195 return Ok(()); 196 }; 197 let (Some(claimed), Some(sha)) = ( 198 n.get("claimed").and_then(Value::as_bool), 199 s(n, &["omega_sha"]), 200 ) else { 201 return fail("malformed_companion", "aien.native.claimed/omega_sha"); 202 }; 203 let recall = if a.has("compose_recall") { 204 a.json("compose_recall")? 205 } else { 206 None 207 }; 208 let recall_native = recall 209 .as_ref() 210 .and_then(|r| r.get("compose_native")) 211 .and_then(Value::as_bool); 212 let recall_sha = recall.as_ref().and_then(|r| s(r, &["omega_sha"])); 213 if claimed { 214 match (recall_native, recall_sha) { 215 (Some(true), Some(r)) if is_hex40(r) && r == sha && is_hex40(sha) => {} 216 (None, _) => { 217 return fail( 218 "native_claim_contradicted", 219 "claimed native but no compose_recall report with compose_native", 220 ) 221 } 222 (Some(false), _) => { 223 return fail( 224 "native_claim_contradicted", 225 "claimed native but compose_recall.compose_native=false", 226 ) 227 } 228 _ => { 229 return fail( 230 "native_claim_contradicted", 231 "aien.native.omega_sha vs compose_recall.omega_sha", 232 ) 233 } 234 } 235 } else if recall_native == Some(true) { 236 return fail( 237 "native_claim_contradicted", 238 "claimed:false but compose_recall.compose_native=true", 239 ); 240 } 241 Ok(()) 242 }