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/effect_binding.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 / effect_binding.rs` 559 lines · 19034 bytes Raw Blame History 1 //! Effect binding tests: the ledger slice (`aien-ledger-slice/1`, strong) and the loose 2 //! `record_effect_receipt/1` (weaker). Every expected verdict is a literal. Each mutated archive is 3 //! internally restamped (record digests recomputed) so only the binding itself can refuse it. 4 //! Specification: provenance/BINDING.md. 5 6 mod common; 7 8 use common::*; 9 use interplane_provenance::binding::{ 10 approval_binding_bytes, compact_sorted, ApprovalFields, APPROVAL_BINDING, 11 }; 12 use interplane_provenance::gojson::sha256_hex; 13 use interplane_provenance::synth::{self, Opts}; 14 use interplane_provenance::verify; 15 use serde_json::{json, Value}; 16 use std::path::{Path, PathBuf}; 17 18 const SYN: &str = "fixtures/synthetic-full-chain"; 19 const WEAK: &str = "fixtures/synthetic-weak-receipt"; 20 const STRONG: &str = "effect=aien-ledger-slice/1:strong proposal=scripted_turn"; 21 const WEAKL: &str = "effect=record_effect_receipt/1:weak"; 22 const INCOMPLETE: &str = "PASS_LABELLED_INCOMPLETE missing=link:model_turn"; 23 24 fn fresh(name: &str, o: &Opts) -> PathBuf { 25 let d = Path::new(env!("CARGO_TARGET_TMPDIR")) 26 .join("effect") 27 .join(name); 28 let _ = std::fs::remove_dir_all(&d); 29 synth::write_synthetic(&d, o).unwrap(); 30 d 31 } 32 33 /// Change the JSON inside a record's `text` string, keep the rest of the exported view, restamp. 34 fn edit_text(d: &Path, rec: &str, f: impl FnOnce(&mut Value)) { 35 let p = path_of(d, rec); 36 let mut v: Value = serde_json::from_slice(&std::fs::read(&p).unwrap()).unwrap(); 37 let mut t: Value = serde_json::from_str(v["text"].as_str().unwrap()).unwrap(); 38 f(&mut t); 39 v["text"] = json!(t.to_string()); 40 std::fs::write(&p, serde_json::to_vec_pretty(&v).unwrap()).unwrap(); 41 restamp(d, rec); 42 } 43 44 /// Change the exported view itself (id, verified, note, ...), restamp. 45 fn edit_view(d: &Path, rec: &str, f: impl FnOnce(&mut Value)) { 46 let p = path_of(d, rec); 47 let mut v: Value = serde_json::from_slice(&std::fs::read(&p).unwrap()).unwrap(); 48 f(&mut v); 49 std::fs::write(&p, serde_json::to_vec_pretty(&v).unwrap()).unwrap(); 50 restamp(d, rec); 51 } 52 53 fn edit_json(d: &Path, rec: &str, f: impl FnOnce(&mut Value)) { 54 edit_view(d, rec, f) 55 } 56 57 /// Copy `from`'s record `rec` over `to`'s and restamp. 58 fn transplant(from: &Path, to: &Path, rec: &str) { 59 std::fs::copy(path_of(from, rec), path_of(to, rec)).unwrap(); 60 restamp(to, rec); 61 } 62 63 const LEDGER: [&str; 5] = [ 64 "ledger_claim", 65 "ledger_committed", 66 "ledger_grant", 67 "ledger_intent", 68 "ledger_ack", 69 ]; 70 71 fn with_slice_of(name: &str, base: &Opts, donor: &Opts) -> PathBuf { 72 let d = fresh(name, base); 73 let other = fresh(&format!("{name}-donor"), donor); 74 for r in LEDGER { 75 transplant(&other, &d, r); 76 } 77 d 78 } 79 80 fn asset(opts: impl FnOnce(&mut Opts)) -> Opts { 81 let mut o = Opts::default(); 82 opts(&mut o); 83 o 84 } 85 86 // ---------- labels ---------- 87 88 #[test] 89 fn ledger_slice_is_labelled_strong() { 90 assert_eq!(verify(Path::new(SYN)), format!("{INCOMPLETE} {STRONG}")); 91 } 92 93 #[test] 94 fn record_effect_receipt_v1_is_still_accepted_and_labelled_weaker() { 95 assert_eq!(verify(Path::new(WEAK)), format!("{INCOMPLETE} {WEAKL}")); 96 } 97 98 // ---------- identity: request, trace, model ---------- 99 100 #[test] 101 fn ledger_for_another_request_than_the_link_names() { 102 // Request 02 is in the trace, so the trace check passes; the daemon's records name request 01. 103 let d = scratch(SYN, "ledger_other_request"); 104 let mut c = companion(&d); 105 c["interplane"]["request_id"] = json!("call-prov-02"); 106 write_companion(&d, &c); 107 assert_eq!( 108 verify(&d), 109 "FAIL binding_mismatch: ledger_grant.request_id ledger=call-prov-01 link=call-prov-02" 110 ); 111 } 112 113 #[test] 114 fn ledger_from_another_trace() { 115 // Same request id, same arguments, same everything except the trace id. 116 let d = with_slice_of( 117 "ledger_other_trace", 118 &Opts::default(), 119 &asset(|o| o.trace_id = Some("trace-prov-02".into())), 120 ); 121 assert_eq!( 122 verify(&d), 123 "FAIL binding_mismatch: ledger_grant.trace_id ledger=trace-prov-02 link=trace-prov-01" 124 ); 125 } 126 127 #[test] 128 fn claim_for_another_trace_than_its_grant() { 129 let d = scratch(SYN, "claim_other_trace"); 130 edit_text(&d, "ledger_claim", |t| { 131 t["trace_id"] = json!("trace-prov-02") 132 }); 133 assert_eq!( 134 verify(&d), 135 "FAIL binding_mismatch: ledger_claim.trace_id ledger=trace-prov-02 link=trace-prov-01" 136 ); 137 } 138 139 #[test] 140 fn effect_from_a_different_daemon_process_than_the_one_that_loaded_the_model() { 141 let d = scratch(SYN, "other_daemon_process"); 142 edit_json(&d, "daemon_run", |r| r["pid"] = json!(4243)); 143 assert_eq!( 144 verify(&d), 145 "FAIL binding_mismatch: ledger_claim.executor.pid vs daemon_run.pid" 146 ); 147 let d = scratch(SYN, "other_daemon_start"); 148 edit_json(&d, "daemon_run", |r| r["start_ticks"] = json!(1)); 149 assert_eq!( 150 verify(&d), 151 "FAIL binding_mismatch: ledger_claim.executor.start vs daemon_run.start_ticks" 152 ); 153 } 154 155 #[test] 156 fn effect_from_a_different_executable_than_the_aien_link() { 157 let d = scratch(SYN, "other_executable"); 158 edit_json(&d, "daemon_run", |r| { 159 r["executable_sha256"] = json!("0".repeat(64)) 160 }); 161 assert_eq!( 162 verify(&d), 163 "FAIL binding_mismatch: daemon_run.executable_sha256 vs aien.executable_sha256" 164 ); 165 } 166 167 #[test] 168 fn effect_from_a_daemon_that_bound_another_socket_than_the_load_log() { 169 let d = scratch(SYN, "other_socket"); 170 edit_json(&d, "daemon_run", |r| r["socket"] = json!("/elsewhere.sock")); 171 assert_eq!( 172 verify(&d), 173 "FAIL binding_mismatch: aien_load.socket vs daemon_run.socket" 174 ); 175 } 176 177 #[test] 178 fn load_log_for_another_model_than_the_export() { 179 let d = scratch(SYN, "other_model"); 180 let p = path_of(&d, "aien_load_log"); 181 let c = companion(&d); 182 let sha = c["records"]["export_weights"]["sha256"].as_str().unwrap(); 183 let text = std::fs::read_to_string(&p).unwrap(); 184 std::fs::write(&p, text.replace(sha, &"1".repeat(64))).unwrap(); 185 restamp(&d, "aien_load_log"); 186 assert_eq!(verify(&d), "FAIL binding_mismatch: aien_load.model_sha256"); 187 } 188 189 // ---------- altered record, one test per record type ---------- 190 191 #[test] 192 fn altered_claim() { 193 let d = scratch(SYN, "alter_claim"); 194 edit_text(&d, "ledger_claim", |t| { 195 t["approval_key"] = json!("a".repeat(64)) 196 }); 197 assert_eq!( 198 verify(&d), 199 "FAIL binding_mismatch: ledger_claim.approval_key" 200 ); 201 } 202 203 #[test] 204 fn altered_committed() { 205 let d = scratch(SYN, "alter_committed"); 206 edit_text(&d, "ledger_committed", |t| { 207 t["evidence"]["cx_promotion"] = json!(999) 208 }); 209 assert_eq!( 210 verify(&d), 211 "FAIL binding_mismatch: ledger_committed.evidence.cx_promotion" 212 ); 213 } 214 215 #[test] 216 fn committed_for_another_claim() { 217 let d = scratch(SYN, "committed_other_claim"); 218 edit_text(&d, "ledger_committed", |t| t["claim"] = json!(7)); 219 assert_eq!(verify(&d), "FAIL binding_mismatch: ledger_committed.claim"); 220 } 221 222 #[test] 223 fn altered_grant() { 224 // The grant's approved content changed: the approval key recomputed from its own fields no 225 // longer equals the key the claim recorded. 226 let d = scratch(SYN, "alter_grant"); 227 edit_text(&d, "ledger_grant", |t| { 228 t["content_sha256"] = json!("b".repeat(64)) 229 }); 230 assert_eq!( 231 verify(&d), 232 "FAIL binding_mismatch: ledger_grant.approval_key (recomputed from its approval fields)" 233 ); 234 } 235 236 #[test] 237 fn altered_intent() { 238 let d = scratch(SYN, "alter_intent"); 239 edit_text(&d, "ledger_intent", |t| t["path"] = json!("notes/evil.md")); 240 assert_eq!(verify(&d), "FAIL binding_mismatch: ledger_intent.path"); 241 } 242 243 #[test] 244 fn altered_ack() { 245 let d = scratch(SYN, "alter_ack"); 246 edit_text(&d, "ledger_ack", |t| t["state"] = json!("NOT_DONE")); 247 assert_eq!(verify(&d), "FAIL effect_not_done: ledger_ack.state=NOT_DONE: the daemon read the world back and the write did not land"); 248 let d = scratch(SYN, "alter_ack_disk"); 249 edit_text(&d, "ledger_ack", |t| { 250 t["disk_sha256"] = json!("c".repeat(64)) 251 }); 252 assert_eq!(verify(&d), "FAIL binding_mismatch: ledger_ack.disk_sha256"); 253 } 254 255 #[test] 256 fn record_the_daemon_did_not_verify() { 257 let d = scratch(SYN, "unverified_grant"); 258 edit_view(&d, "ledger_grant", |v| v["verified"] = json!(false)); 259 assert_eq!(verify(&d), "FAIL binding_mismatch: ledger_grant.verified"); 260 } 261 262 #[test] 263 fn altered_record_without_restamp_is_a_digest_mismatch() { 264 let d = scratch(SYN, "alter_no_restamp"); 265 let p = path_of(&d, "ledger_ack"); 266 let mut b = std::fs::read(&p).unwrap(); 267 let n = b.len() / 2; 268 b[n] ^= 0x01; 269 std::fs::write(&p, b).unwrap(); 270 assert_eq!(verify(&d), "FAIL digest_mismatch: ledger_ack"); 271 } 272 273 #[test] 274 fn records_out_of_ledger_order() { 275 let d = scratch(SYN, "order"); 276 // The ack claims to predate its intent. 277 edit_view(&d, "ledger_ack", |v| v["id"] = json!(25)); 278 assert_eq!( 279 verify(&d), 280 "FAIL binding_mismatch: ledger record order [20, 26, 27, 28, 25]" 281 ); 282 } 283 284 #[test] 285 fn ledger_record_removed_from_the_manifest() { 286 let d = scratch(SYN, "no_ack"); 287 let mut c = companion(&d); 288 c["records"].as_object_mut().unwrap().remove("ledger_ack"); 289 write_companion(&d, &c); 290 assert_eq!(verify(&d), "FAIL effect_interrupted: ledger_intent is the last record: no ack, the write may or may not have landed"); 291 } 292 293 // ---------- INTERPLANE call against the ledger ---------- 294 295 #[test] 296 fn interplane_arguments_differ_from_what_the_daemon_approved() { 297 let d = scratch(SYN, "args_differ"); 298 edit_json(&d, "interplane_trace", |t| { 299 t[0]["payload"]["arguments"]["content"] = json!("hello world!") 300 }); 301 assert_eq!( 302 verify(&d), 303 "FAIL binding_mismatch: interplane.arguments.content request=call-prov-01" 304 ); 305 } 306 307 #[test] 308 fn interplane_result_names_other_ledger_records() { 309 let d = scratch(SYN, "result_ids"); 310 edit_json(&d, "interplane_trace", |t| { 311 t[1]["payload"]["data"]["receipt"]["grant_id"] = json!(1) 312 }); 313 assert_eq!( 314 verify(&d), 315 "FAIL binding_mismatch: interplane.result.data.receipt.grant_id request=call-prov-01" 316 ); 317 } 318 319 // ---------- identical calls ---------- 320 321 #[test] 322 fn two_identical_calls_in_one_trace_are_told_apart() { 323 let a = fresh("same_trace_call_01", &Opts::default()); 324 let b = fresh( 325 "same_trace_call_03", 326 &asset(|o| o.request_id = Some(synth::IDENTICAL_REQUEST_ID.into())), 327 ); 328 // The two calls have the same tool, arguments and result data. 329 let ta: Value = 330 serde_json::from_slice(&std::fs::read(path_of(&a, "interplane_trace")).unwrap()).unwrap(); 331 assert_eq!(ta[0]["payload"]["arguments"], ta[4]["payload"]["arguments"]); 332 assert_eq!(ta[0]["payload"]["tool"], ta[4]["payload"]["tool"],); 333 // Each verifies against its own slice ... 334 assert_eq!(verify(&a), format!("{INCOMPLETE} {STRONG}")); 335 assert_eq!(verify(&b), format!("{INCOMPLETE} {STRONG}")); 336 // ... and not against the other's. 337 let swapped = with_slice_of( 338 "same_trace_swapped", 339 &Opts::default(), 340 &asset(|o| o.request_id = Some(synth::IDENTICAL_REQUEST_ID.into())), 341 ); 342 assert_eq!( 343 verify(&swapped), 344 "FAIL binding_mismatch: ledger_grant.request_id ledger=call-prov-03 link=call-prov-01" 345 ); 346 } 347 348 #[test] 349 fn two_identical_calls_in_two_traces_are_told_apart() { 350 let a = fresh("two_traces_a", &Opts::default()); 351 let b = fresh( 352 "two_traces_b", 353 &asset(|o| o.trace_id = Some("trace-prov-02".into())), 354 ); 355 assert_eq!(verify(&a), format!("{INCOMPLETE} {STRONG}")); 356 assert_eq!(verify(&b), format!("{INCOMPLETE} {STRONG}")); 357 let swapped = with_slice_of( 358 "two_traces_swapped", 359 &asset(|o| o.trace_id = Some("trace-prov-02".into())), 360 &Opts::default(), 361 ); 362 assert_eq!( 363 verify(&swapped), 364 "FAIL binding_mismatch: ledger_grant.trace_id ledger=trace-prov-01 link=trace-prov-02" 365 ); 366 } 367 368 #[test] 369 fn weaker_receipt_cannot_tell_two_identical_calls_apart_and_says_so() { 370 let a = fresh("weak_call_01", &asset(|o| o.weak = true)); 371 let b = fresh( 372 "weak_call_03", 373 &asset(|o| { 374 o.weak = true; 375 o.request_id = Some(synth::IDENTICAL_REQUEST_ID.into()) 376 }), 377 ); 378 // Byte-identical receipts: nothing in them names the request or the trace. 379 assert_eq!( 380 std::fs::read(path_of(&a, "effect_receipt")).unwrap(), 381 std::fs::read(path_of(&b, "effect_receipt")).unwrap() 382 ); 383 transplant(&b, &a, "effect_receipt"); 384 // Call 03's receipt is accepted for call 01. The verdict carries the weaker label. 385 assert_eq!(verify(&a), format!("{INCOMPLETE} {WEAKL}")); 386 } 387 388 // ---------- canonicalization ---------- 389 390 #[test] 391 fn receipt_declared_in_the_jcs_form_is_refused() { 392 let d = scratch(WEAK, "digest_form_jcs"); 393 let mut c = companion(&d); 394 c["effect"]["digest_form"] = json!("jcs-rfc8785"); 395 write_companion(&d, &c); 396 assert_eq!( 397 verify(&d), 398 "FAIL canonicalization_mismatch: effect.digest_form=jcs-rfc8785 (producer form is serde_json.to_vec.sorted-keys/1)" 399 ); 400 let d = scratch(WEAK, "digest_form_absent"); 401 let mut c = companion(&d); 402 c["effect"].as_object_mut().unwrap().remove("digest_form"); 403 write_companion(&d, &c); 404 assert_eq!( 405 verify(&d), 406 "FAIL canonicalization_mismatch: effect.digest_form=absent (producer form is serde_json.to_vec.sorted-keys/1)" 407 ); 408 } 409 410 fn jcs(v: &Value) -> String { 411 format!( 412 "sha256:{}", 413 sha256_hex(interplane_core::canonicalize(v).as_bytes()) 414 ) 415 } 416 417 /// Rewrite the weak fixture's request arguments (trace) and put `digest(args)` in its receipt. 418 fn weak_with_args(name: &str, args: Value, digest: impl Fn(&Value) -> String) -> PathBuf { 419 let d = scratch(WEAK, name); 420 edit_json(&d, "interplane_trace", |t| { 421 t[0]["payload"]["arguments"] = args.clone() 422 }); 423 edit_json(&d, "effect_receipt", |r| { 424 r["arguments_digest"] = json!(digest(&args)) 425 }); 426 d 427 } 428 429 #[test] 430 fn receipt_hashed_in_the_jcs_form_is_refused_by_name() { 431 // JCS orders keys by UTF-16 code unit, serde_json by UTF-8 byte: the astral key sorts first 432 // in one and last in the other. A shared "sorted keys" reading would hide this. 433 let args = json!({"\u{ffff}": 1, "\u{1f600}": 2}); 434 assert_ne!( 435 jcs(&args), 436 format!("sha256:{}", sha256_hex(&compact_sorted(&args).unwrap())) 437 ); 438 let d = weak_with_args("jcs_hashed", args, jcs); 439 assert_eq!( 440 verify(&d), 441 "FAIL canonicalization_mismatch: effect_receipt.arguments_digest is the JCS form; the producer form is serde_json.to_vec.sorted-keys/1" 442 ); 443 } 444 445 #[test] 446 fn receipt_in_the_producer_form_is_accepted_where_jcs_would_differ() { 447 let args = json!({"\u{ffff}": 1, "\u{1f600}": 2}); 448 let d = weak_with_args("producer_hashed", args, |v| { 449 format!("sha256:{}", sha256_hex(&compact_sorted(v).unwrap())) 450 }); 451 assert_eq!(verify(&d), format!("{INCOMPLETE} {WEAKL}")); 452 } 453 454 #[test] 455 fn float_arguments_are_refused_not_approximated() { 456 let args = json!({"x": 1.5}); 457 let d = weak_with_args("float_args", args, |_| "sha256:00".into()); 458 assert_eq!( 459 verify(&d), 460 "FAIL canonicalization_mismatch: effect_receipt.arguments_digest: producer form serde_json.to_vec.sorted-keys/1 not reproduced: non-integer number 1.5" 461 ); 462 } 463 464 #[test] 465 fn unknown_effect_binding_is_refused() { 466 let d = scratch(SYN, "binding_v2"); 467 let mut c = companion(&d); 468 c["effect"]["binding"] = json!("aien-ledger-slice/2"); 469 c["completeness"] = json!({"state": "complete", "missing": []}); 470 write_companion(&d, &c); 471 assert_eq!( 472 verify(&d), 473 "FAIL unsupported_binding: effect.binding=aien-ledger-slice/2" 474 ); 475 let d = scratch(SYN, "binding_absent"); 476 let mut c = companion(&d); 477 c["effect"].as_object_mut().unwrap().remove("binding"); 478 c["completeness"] = json!({"state": "complete", "missing": []}); 479 write_companion(&d, &c); 480 assert_eq!(verify(&d), "FAIL malformed_companion: effect.binding"); 481 let d = scratch(SYN, "approval_binding_v1"); 482 let mut c = companion(&d); 483 c["effect"]["approval_binding"] = json!("aien.approval.v1"); 484 write_companion(&d, &c); 485 assert_eq!( 486 verify(&d), 487 "FAIL unsupported_binding: effect.approval_binding != aien.approval.v2 (got aien.approval.v1)" 488 ); 489 } 490 491 // ---------- the byte forms themselves ---------- 492 493 #[test] 494 fn compact_sorted_equals_serde_json_to_vec_for_integer_values() { 495 let v = json!({ 496 "z": [1, -2, 3000000000u64, null, true, false], 497 "a": {"b\"c": "q\\r\n\t\u{8}\u{c}\u{1}\u{1f} \u{7f} \u{2028} é \u{1f600}", "A": 18446744073709551615u64}, 498 "": "", 499 "\u{ffff}": 1, "\u{1f600}": 2 500 }); 501 assert_eq!(compact_sorted(&v).unwrap(), serde_json::to_vec(&v).unwrap()); 502 } 503 504 #[test] 505 fn approval_binding_bytes_are_the_daemons_sorted_compact_object() { 506 // sovereign-core `binding_bytes`: a serde_json map of nine strings plus "v", compact, sorted. 507 let f = ApprovalFields { 508 approval_id: "aien-approval:w1:c32f", 509 approved_proposal_sha256: "f766", 510 approver: "dr\"ake", 511 content_sha256: "7a65", 512 desk_key_id: "440f", 513 path: "dir/summary.txt", 514 request_id: "w1", 515 trace_id: "ledger", 516 workspace: "/w s/\u{e9}", 517 }; 518 let expect = json!({ 519 "approval_id": f.approval_id, "approved_proposal_sha256": f.approved_proposal_sha256, 520 "approver": f.approver, "content_sha256": f.content_sha256, "desk_key_id": f.desk_key_id, 521 "path": f.path, "request_id": f.request_id, "trace_id": f.trace_id, 522 "workspace": f.workspace, "v": APPROVAL_BINDING, 523 }); 524 assert_eq!( 525 approval_binding_bytes(&f), 526 serde_json::to_vec(&expect).unwrap() 527 ); 528 } 529 530 // ---------- evidence, never authority ---------- 531 532 #[test] 533 fn verifying_reads_and_changes_nothing() { 534 let d = scratch(SYN, "read_only"); 535 let snapshot = |d: &Path| { 536 let mut all = Vec::new(); 537 let mut stack = vec![d.to_path_buf()]; 538 while let Some(p) = stack.pop() { 539 for e in std::fs::read_dir(&p).unwrap() { 540 let e = e.unwrap(); 541 if e.file_type().unwrap().is_dir() { 542 stack.push(e.path()); 543 } else { 544 all.push((e.path(), std::fs::read(e.path()).unwrap())); 545 } 546 } 547 } 548 all.sort(); 549 all 550 }; 551 let before = snapshot(&d); 552 let verdict = verify(&d); 553 assert!(verdict.starts_with("PASS_LABELLED_INCOMPLETE"), "{verdict}"); 554 assert_eq!(snapshot(&d), before); 555 // A PASS is a statement about records. No verdict text carries a permission. 556 for word in ["allow", "grant ", "authoriz", "permit", "execute"] { 557 assert!(!verdict.to_lowercase().contains(word), "{verdict}"); 558 } 559 }