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/tests/evaluation.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 / tests / evaluation.rs` 624 lines ยท 19308 bytes Raw Blame History 1 //! `rsi-eval/2`: the independent judge's signed evaluation of the exact change the daemon wrote. 2 //! Every expected verdict is a literal. Records are added to (or mutated in) copies of the 3 //! synthetic fix-the-test bundle and restamped, so only the evaluation checks can refuse them. 4 //! Specification: provenance/BINDING.md ("Independent evaluation"). 5 6 mod common; 7 8 use common::*; 9 use interplane_provenance::evaluation::{parse_judge_key, precheck, receipt_digest_v2, Expect}; 10 use interplane_provenance::gojson::sha256_hex; 11 use interplane_provenance::synth::{self, Opts}; 12 use interplane_provenance::{verify, verify_with, Options}; 13 use p256::ecdsa::signature::Signer; 14 use p256::ecdsa::{Signature, SigningKey, VerifyingKey}; 15 use serde_json::{json, Value}; 16 use std::path::{Path, PathBuf}; 17 use std::sync::OnceLock; 18 19 /// A named edit applied to a signed receipt. 20 type Edit = (&'static str, Box); 21 22 const STRONG: &str = "effect=aien-ledger-slice/1:strong proposal=scripted_turn"; 23 const OTHER: &str = "`9999999999999999999999999999999999999999999999999999999999999999`"; 24 const HOLDOUTS: &str = "`1111111111111111111111111111111111111111111111111111111111111111`"; 25 26 fn key(seed: u8) -> SigningKey { 27 SigningKey::from_slice(&[seed; 32]).unwrap() 28 } 29 30 fn judge() -> SigningKey { 31 key(7) 32 } 33 34 fn opts(k: &SigningKey) -> Options { 35 Options { 36 judge_key: Some(*k.verifying_key()), 37 ..Default::default() 38 } 39 } 40 41 fn base() -> PathBuf { 42 static BASE: OnceLock = OnceLock::new(); 43 BASE.get_or_init(|| { 44 let d = Path::new(env!("CARGO_TARGET_TMPDIR")) 45 .join("evaluation") 46 .join("base"); 47 let _ = std::fs::remove_dir_all(&d); 48 synth::write_synthetic( 49 &d, 50 &Opts { 51 fix_the_test: true, 52 native: Some(true), 53 ..Default::default() 54 }, 55 ) 56 .unwrap(); 57 d 58 }) 59 .clone() 60 } 61 62 fn fresh(name: &str) -> PathBuf { 63 let d = Path::new(env!("CARGO_TARGET_TMPDIR")) 64 .join("evaluation") 65 .join(name); 66 let _ = std::fs::remove_dir_all(&d); 67 copy_dir(&base(), &d); 68 d 69 } 70 71 fn record_json(d: &Path, rec: &str) -> Value { 72 serde_json::from_slice(&std::fs::read(path_of(d, rec)).unwrap()).unwrap() 73 } 74 75 /// The written path and its digest, as the bundle's own task and test run state them. 76 fn subject(d: &Path) -> (String, String) { 77 let task = record_json(d, "task"); 78 let run = record_json(d, "test_run_record"); 79 ( 80 task["target_path"].as_str().unwrap().to_string(), 81 run["target_blob_sha256_after"] 82 .as_str() 83 .unwrap() 84 .to_string(), 85 ) 86 } 87 88 fn policy(path: &str) -> Value { 89 json!({"policy_id": "test", "holdout_set_sha256": HOLDOUTS, "min_holdout_pass_ratio": 1.0, 90 "require_admitted": true, "allowed_targets": [path], "protected_paths": ["holdouts"]}) 91 } 92 93 fn sign(r: &mut Value, k: &SigningKey) { 94 let digest = receipt_digest_v2(r).unwrap(); 95 let sig: Signature = k.sign(&hex::decode(&digest).unwrap()); 96 r["receipt_digest"] = json!(digest); 97 r["signature"] = json!(format!("tpm2-p256:{}", hex::encode(sig.to_bytes()))); 98 } 99 100 fn receipt(path: &str, sha: &str, policy_bytes: &[u8], parent: &str) -> Value { 101 json!({ 102 "cycle_id": "cyc", "candidate_id": "cand", "parent_id": parent, 103 "evaluated_at": "2026-10-08T22:00:00Z", "evaluator_version": "1.0.0", 104 "passed_all_hard_invariants": true, "passed_statistical_gates": true, "admitted": true, 105 "layer_results": [ 106 {"layer_name": "correctness", "is_hard_invariant": true, "passed": true, "score": 1.0, 107 "summary": "ok", "violations": []}, 108 {"layer_name": "performance", "is_hard_invariant": false, "passed": true, 109 "score": 0.`8333333333333334`, "summary": "within margin", "violations": []} 110 ], 111 "metrics_summary": {"latency_delta_pct": -0.`12345678901234568`, "p_value": 0.5, 112 "p95_ci_upper_degradation_pct": 0.1, "p99_ci_upper_degradation_pct": 0.2, 113 "rss_growth_pct": 0.0, "candidate_resident_mb": 3}, 114 "receipt_digest": "", "signature": null, "format_version": 2, 115 "binding": {"subject_path": path, "subject_sha256": sha, 116 "holdout_set_sha256": HOLDOUTS, "holdouts_total": 4, "holdouts_passed": 4, 117 "policy_sha256": sha256_hex(policy_bytes), 118 "evaluator_binary_sha256": OTHER} 119 }) 120 } 121 122 /// Adds the evaluation records and section. `tweak` edits the receipt before it is signed. 123 fn add_evaluation(d: &Path, k: &SigningKey, tweak: impl FnOnce(&mut Value), pol: Option) { 124 let (path, sha) = subject(d); 125 let policy_bytes = serde_json::to_vec_pretty(&pol.unwrap_or_else(|| policy(&path))).unwrap(); 126 let parent = record_json(d, "source_pin")["commit"] 127 .as_str() 128 .unwrap() 129 .to_string(); 130 let mut r = receipt(&path, &sha, &policy_bytes, &parent); 131 tweak(&mut r); 132 sign(&mut r, k); 133 let dir = d.join("records/evaluation"); 134 std::fs::create_dir_all(&dir).unwrap(); 135 let rb = serde_json::to_vec_pretty(&r).unwrap(); 136 std::fs::write(dir.join("receipt.json"), &rb).unwrap(); 137 std::fs::write(dir.join("policy.json"), &policy_bytes).unwrap(); 138 let mut c = companion(d); 139 for (n, p, b) in [ 140 ("evaluation_receipt", "records/evaluation/receipt.json", &rb), 141 ( 142 "evaluation_policy", 143 "records/evaluation/policy.json", 144 &policy_bytes, 145 ), 146 ] { 147 c["records"][n] = json!({"path": p, "sha256": sha256_hex(b), "bytes": b.len()}); 148 } 149 c["evaluation"] = json!({"binding": "rsi-eval/2", "receipt": "evaluation_receipt", 150 "policy": "evaluation_policy"}); 151 write_companion(d, &c); 152 } 153 154 fn edit(d: &Path, rec: &str, f: impl FnOnce(&mut Value)) { 155 let p = path_of(d, rec); 156 let mut v: Value = serde_json::from_slice(&std::fs::read(&p).unwrap()).unwrap(); 157 f(&mut v); 158 std::fs::write(&p, serde_json::to_vec_pretty(&v).unwrap()).unwrap(); 159 restamp(d, rec); 160 } 161 162 fn code(v: &str) -> &str { 163 v.strip_prefix("FAIL ") 164 .and_then(|r| r.split(':').next()) 165 .unwrap_or(v) 166 } 167 168 fn baseline() -> String { 169 verify(&base()) 170 } 171 172 #[test] 173 fn baseline_bundle_verifies_without_evaluation() { 174 let v = baseline(); 175 assert!(v.starts_with("PASS_LABELLED_INCOMPLETE"), "{v}"); 176 assert!(v.ends_with(STRONG), "{v}"); 177 } 178 179 #[test] 180 fn judged_bundle_passes_with_the_pinned_key() { 181 let d = fresh("pass"); 182 add_evaluation(&d, &judge(), |_| {}, None); 183 assert_eq!(verify_with(&d, &opts(&judge())), baseline()); 184 } 185 186 #[test] 187 fn without_a_key_an_evaluation_is_untrusted() { 188 let d = fresh("untrusted"); 189 add_evaluation(&d, &judge(), |_| {}, None); 190 assert_eq!(code(&verify(&d)), "evaluation_untrusted"); 191 } 192 193 #[test] 194 fn a_supplied_key_requires_an_evaluation() { 195 assert_eq!( 196 code(&verify_with(&base(), &opts(&judge()))), 197 "evaluation_missing" 198 ); 199 let d = fresh("dropped"); 200 add_evaluation(&d, &judge(), |_| {}, None); 201 let mut c = companion(&d); 202 c.as_object_mut().unwrap().remove("evaluation"); 203 write_companion(&d, &c); 204 assert_eq!( 205 code(&verify(&d)), 206 "evaluation_missing", 207 "records without a section" 208 ); 209 } 210 211 #[test] 212 fn version_1_receipts_are_insufficient() { 213 let d = fresh("v1"); 214 add_evaluation(&d, &judge(), |_| {}, None); 215 edit(&d, "evaluation_receipt", |r| { 216 r["format_version"] = json!(1); 217 }); 218 assert_eq!( 219 code(&verify_with(&d, &opts(&judge()))), 220 "evaluation_v1_insufficient" 221 ); 222 edit(&d, "evaluation_receipt", |r| { 223 r.as_object_mut().unwrap().remove("format_version"); 224 }); 225 assert_eq!( 226 code(&verify_with(&d, &opts(&judge()))), 227 "evaluation_v1_insufficient" 228 ); 229 } 230 231 #[test] 232 fn substituted_scores_break_the_signature() { 233 let edits: Vec = vec![ 234 ( 235 "count", 236 Box::new(|r| r["binding"]["holdouts_passed"] = json!(3)), 237 ), 238 ( 239 "score", 240 Box::new(|r| r["layer_results"][1]["score"] = json!(0.9)), 241 ), 242 ( 243 "admitted", 244 Box::new(|r| r["passed_statistical_gates"] = json!(false)), 245 ), 246 ( 247 "metrics", 248 Box::new(|r| r["metrics_summary"]["p_value"] = json!(0.01)), 249 ), 250 ( 251 "violations", 252 Box::new(|r| r["layer_results"][0]["violations"] = json!(["x"])), 253 ), 254 ( 255 "subject", 256 Box::new(|r| r["binding"]["subject_sha256"] = json!(OTHER)), 257 ), 258 ( 259 "evaluator", 260 Box::new(|r| r["binding"]["evaluator_binary_sha256"] = json!(HOLDOUTS)), 261 ), 262 ]; 263 for (name, f) in edits { 264 let d = fresh(&format!("score-{name}")); 265 add_evaluation(&d, &judge(), |_| {}, None); 266 edit(&d, "evaluation_receipt", |r| f(r)); 267 assert_eq!( 268 code(&verify_with(&d, &opts(&judge()))), 269 "evaluation_signature_bad", 270 "{name}" 271 ); 272 } 273 } 274 275 #[test] 276 fn wrong_keys_are_refused() { 277 // Signed by someone else (a proposer's own key), checked against the pinned judge key. 278 let d = fresh("wrong-signer"); 279 add_evaluation(&d, &key(9), |_| {}, None); 280 assert_eq!( 281 code(&verify_with(&d, &opts(&judge()))), 282 "evaluation_signature_bad" 283 ); 284 // Signed by the judge, checked against a different pinned key. 285 let d = fresh("wrong-pin"); 286 add_evaluation(&d, &judge(), |_| {}, None); 287 assert_eq!( 288 code(&verify_with(&d, &opts(&key(11)))), 289 "evaluation_signature_bad" 290 ); 291 // A bundle cannot smuggle its own key: nothing in it is read as a key. 292 let mut c = companion(&d); 293 c["evaluation"]["judge_public_key"] = json!(hex::encode( 294 key(11).verifying_key().to_sec1_point(false).as_bytes() 295 )); 296 write_companion(&d, &c); 297 assert_eq!(code(&verify(&d)), "evaluation_untrusted"); 298 } 299 300 #[test] 301 fn a_genuine_receipt_for_other_bytes_is_a_substituted_change() { 302 let d = fresh("other-bytes"); 303 add_evaluation( 304 &d, 305 &judge(), 306 |r| r["binding"]["subject_sha256"] = json!(OTHER), 307 None, 308 ); 309 assert_eq!( 310 code(&verify_with(&d, &opts(&judge()))), 311 "evaluation_subject_mismatch" 312 ); 313 let d = fresh("other-path"); 314 add_evaluation( 315 &d, 316 &judge(), 317 |r| r["binding"]["subject_path"] = json!("test/test_clamp.c"), 318 None, 319 ); 320 assert_eq!( 321 code(&verify_with(&d, &opts(&judge()))), 322 "evaluation_subject_mismatch" 323 ); 324 } 325 326 #[test] 327 fn altered_policies_are_refused() { 328 // The retained policy edited after the evaluation. 329 let d = fresh("policy-edited"); 330 add_evaluation(&d, &judge(), |_| {}, None); 331 edit(&d, "evaluation_policy", |p| { 332 p["min_holdout_pass_ratio"] = json!(0.5) 333 }); 334 assert_eq!( 335 code(&verify_with(&d, &opts(&judge()))), 336 "evaluation_policy_mismatch" 337 ); 338 // Policies that do not allow the write, or waive admission. 339 let (path, _) = subject(&base()); 340 for (name, mut p) in [ 341 ("not-allowed", policy("README.md")), 342 ("waives", policy(&path)), 343 ("ratio", policy(&path)), 344 ] { 345 match name { 346 "waives" => p["require_admitted"] = json!(false), 347 "ratio" => p["min_holdout_pass_ratio"] = json!(0.0), 348 _ => {} 349 } 350 let d = fresh(&format!("policy-{name}")); 351 add_evaluation(&d, &judge(), |_| {}, Some(p)); 352 assert_eq!( 353 code(&verify_with(&d, &opts(&judge()))), 354 "evaluation_policy_mismatch", 355 "{name}" 356 ); 357 } 358 } 359 360 #[test] 361 fn a_holdout_set_other_than_the_pinned_one_is_refused() { 362 let d = fresh("holdouts"); 363 add_evaluation( 364 &d, 365 &judge(), 366 |r| r["binding"]["holdout_set_sha256"] = json!(OTHER), 367 None, 368 ); 369 assert_eq!( 370 code(&verify_with(&d, &opts(&judge()))), 371 "evaluation_holdout_mismatch" 372 ); 373 } 374 375 #[test] 376 fn below_threshold_or_not_admitted_is_refused() { 377 for (name, f) in [ 378 ( 379 "short", 380 Box::new(|r: &mut Value| r["binding"]["holdouts_passed"] = json!(3)) 381 as Box, 382 ), 383 ( 384 "zero", 385 Box::new(|r: &mut Value| { 386 r["binding"]["holdouts_passed"] = json!(0); 387 r["binding"]["holdouts_total"] = json!(0); 388 }), 389 ), 390 ( 391 "over", 392 Box::new(|r: &mut Value| r["binding"]["holdouts_passed"] = json!(5)), 393 ), 394 ( 395 "rejected", 396 Box::new(|r: &mut Value| r["admitted"] = json!(false)), 397 ), 398 ] { 399 let d = fresh(&format!("threshold-{name}")); 400 add_evaluation(&d, &judge(), |r| f(r), None); 401 assert_eq!( 402 code(&verify_with(&d, &opts(&judge()))), 403 "evaluation_below_threshold", 404 "{name}" 405 ); 406 } 407 } 408 409 #[test] 410 fn malformed_and_unsupported_receipts_are_refused() { 411 let d = fresh("malformed"); 412 add_evaluation(&d, &judge(), |_| {}, None); 413 edit(&d, "evaluation_receipt", |r| { 414 r.as_object_mut().unwrap().remove("layer_results"); 415 }); 416 assert_eq!( 417 code(&verify_with(&d, &opts(&judge()))), 418 "evaluation_malformed" 419 ); 420 let d = fresh("binding"); 421 add_evaluation(&d, &judge(), |_| {}, None); 422 let mut c = companion(&d); 423 c["evaluation"]["binding"] = json!("rsi-eval/1"); 424 write_companion(&d, &c); 425 assert_eq!( 426 code(&verify_with(&d, &opts(&judge()))), 427 "unsupported_evaluation" 428 ); 429 let d = fresh("version3"); 430 add_evaluation(&d, &judge(), |r| r["format_version"] = json!(3), None); 431 assert_eq!( 432 code(&verify_with(&d, &opts(&judge()))), 433 "unsupported_evaluation" 434 ); 435 } 436 437 #[test] 438 fn a_receipt_judged_from_another_commit_is_refused() { 439 let d = fresh("parent"); 440 add_evaluation(&d, &judge(), |r| r["parent_id"] = json!("`0000000`"), None); 441 assert_eq!( 442 code(&verify_with(&d, &opts(&judge()))), 443 "evaluation_parent_mismatch" 444 ); 445 } 446 447 #[test] 448 fn the_operator_can_pin_the_policy() { 449 let d = fresh("pinned"); 450 add_evaluation(&d, &judge(), |_| {}, None); 451 let retained = std::fs::read(path_of(&d, "evaluation_policy")).unwrap(); 452 let pinned = |sha: String| Options { 453 judge_key: Some(*judge().verifying_key()), 454 policy_sha256: Some(sha), 455 }; 456 assert_eq!(verify_with(&d, &pinned(sha256_hex(&retained))), baseline()); 457 // A self-consistent receipt and policy pair, but not the operator's policy. 458 assert_eq!( 459 code(&verify_with(&d, &pinned(OTHER.to_string()))), 460 "evaluation_policy_mismatch" 461 ); 462 } 463 464 #[test] 465 fn a_subject_under_a_protected_path_is_refused() { 466 let (path, _) = subject(&base()); 467 let mut p = policy(&path); 468 p["protected_paths"] = json!([path.split('/').next().unwrap()]); 469 let d = fresh("protected"); 470 add_evaluation(&d, &judge(), |_| {}, Some(p)); 471 assert_eq!( 472 code(&verify_with(&d, &opts(&judge()))), 473 "evaluation_policy_mismatch" 474 ); 475 } 476 477 // ---------------------------------------------------------------- cross-language fixture 478 479 fn cross() -> PathBuf { 480 Path::new(env!("CARGO_MANIFEST_DIR")).join("fixtures/rsi-eval-v2") 481 } 482 483 fn cross_key() -> VerifyingKey { 484 parse_judge_key(&std::fs::read_to_string(cross().join("judge.pub")).unwrap()).unwrap() 485 } 486 487 struct Cross { 488 receipt: Vec, 489 policy: Vec, 490 subject: Vec, 491 parent: String, 492 } 493 494 fn cross_files() -> Cross { 495 let f = cross(); 496 let receipt = std::fs::read(f.join("receipt.json")).unwrap(); 497 let parent = serde_json::from_slice::(&receipt).unwrap()["parent_id"] 498 .as_str() 499 .unwrap() 500 .to_string(); 501 Cross { 502 receipt, 503 policy: std::fs::read(f.join("policy.json")).unwrap(), 504 subject: std::fs::read(f.join("subject.md")).unwrap(), 505 parent, 506 } 507 } 508 509 fn cross_check( 510 c: &Cross, 511 receipt: &[u8], 512 policy: &[u8], 513 key: &VerifyingKey, 514 path: &str, 515 content: &[u8], 516 ) -> Result<(), String> { 517 precheck( 518 receipt, 519 policy, 520 key, 521 &Expect { 522 path, 523 content, 524 parent_commit: &c.parent, 525 policy_sha256: Some(&sha256_hex(&c.policy)), 526 }, 527 ) 528 } 529 530 /// A receipt written and signed by spark-rsi's own judge binary (fixtures/rsi-eval-v2/README.md 531 /// says how it was made) verifies here: the two implementations agree on every digest byte. 532 #[test] 533 fn spark_rsi_receipt_verifies_byte_for_byte() { 534 let c = cross_files(); 535 let k = cross_key(); 536 cross_check(&c, &c.receipt, &c.policy, &k, "README.md", &c.subject).unwrap(); 537 538 let refused = |r: Result<(), String>, code: &str| { 539 let e = r.unwrap_err(); 540 assert!(e.starts_with(&format!("FAIL {code}:")), "{e}"); 541 }; 542 let mut other = c.subject.clone(); 543 other.push(b'\n'); 544 refused( 545 cross_check(&c, &c.receipt, &c.policy, &k, "README.md", &other), 546 "evaluation_subject_mismatch", 547 ); 548 refused( 549 cross_check(&c, &c.receipt, &c.policy, &k, "src/main.rs", &c.subject), 550 "evaluation_subject_mismatch", 551 ); 552 refused( 553 cross_check( 554 &c, 555 &c.receipt, 556 &c.policy, 557 judge().verifying_key(), 558 "README.md", 559 &c.subject, 560 ), 561 "evaluation_signature_bad", 562 ); 563 let mut policy2 = c.policy.clone(); 564 policy2.push(b'\n'); 565 refused( 566 cross_check(&c, &c.receipt, &policy2, &k, "README.md", &c.subject), 567 "evaluation_policy_mismatch", 568 ); 569 refused( 570 precheck( 571 &c.receipt, 572 &c.policy, 573 &k, 574 &Expect { 575 path: "README.md", 576 content: &c.subject, 577 parent_commit: "`0000000`", 578 policy_sha256: None, 579 }, 580 ), 581 "evaluation_parent_mismatch", 582 ); 583 } 584 585 #[test] 586 fn spark_rsi_receipt_with_any_field_changed_fails() { 587 let c = cross_files(); 588 let receipt: Value = serde_json::from_slice(&c.receipt).unwrap(); 589 let paths: [&[&str]; 6] = [ 590 &["cycle_id"], 591 &["evaluated_at"], 592 &["binding", "holdouts_total"], 593 &["binding", "evaluator_binary_sha256"], 594 &["layer_results"], 595 &["metrics_summary"], 596 ]; 597 for path in paths { 598 let mut r = receipt.clone(); 599 let mut v = &mut r; 600 for k in &path[..path.len() - 1] { 601 v = &mut v[*k]; 602 } 603 let last = path[path.len() - 1]; 604 v[last] = match &v[last] { 605 Value::String(s) => json!(format!("{s}x")), 606 Value::Number(n) => json!(n.as_u64().unwrap_or(0) + 1), 607 Value::Array(a) => json!(a[..a.len().saturating_sub(1)]), 608 _ => Value::Null, 609 }; 610 let e = cross_check( 611 &c, 612 &serde_json::to_vec(&r).unwrap(), 613 &c.policy, 614 &cross_key(), 615 "README.md", 616 &c.subject, 617 ) 618 .unwrap_err(); 619 assert!( 620 e.starts_with("FAIL evaluation_signature_bad"), 621 "{path:?}: {e}" 622 ); 623 } 624 }