interplane-mv
INTERPLANE: open interoperability protocol between AI models and agent runtimes. Models express intent. Runtimes retain authority.
Brought by drakestapleton
interplane-mv / provenance / tests / real_chain.rs
| 1 | //! The first real chain: one WALDO-trained tiny model, one AIEN daemon load, a tool call generated |
| 2 | //! by AIEN's own generation path on that loaded model, parsed by the INTERPLANE `aien_legacy` |
| 3 | //! dialect, one approved write through the INTERPLANE AIEN adapter, and the daemon's own ledger |
| 4 | //! records, including the daemon's OWN record of the generation. Every byte under `fixtures/real-waldo-aien-chain` comes from that one run (CPU |
| 5 | //! evidence). These tests alter a copy and require the verifier to refuse. |
| 6 | mod common; |
| 7 | use common::*; |
| 8 | use interplane_provenance::gojson::sha256_hex; |
| 9 | use interplane_provenance::verify; |
| 10 | use serde_json::{json, Value}; |
| 11 | use std::path::Path; |
| 12 | |
| 13 | const REAL: &str = "fixtures/real-waldo-aien-chain"; |
| 14 | const OK: &str = |
| 15 | "PASS complete effect=aien-ledger-slice/1:strong proposal=model_generation/2 candidate=none"; |
| 16 | |
| 17 | fn edit_json(d: &Path, rec: &str, f: impl FnOnce(&mut Value)) { |
| 18 | let p = path_of(d, rec); |
| 19 | let mut v: Value = serde_json::from_slice(&std::fs::read(&p).unwrap()).unwrap(); |
| 20 | f(&mut v); |
| 21 | std::fs::write(&p, serde_json::to_vec_pretty(&v).unwrap()).unwrap(); |
| 22 | restamp(d, rec); |
| 23 | } |
| 24 | |
| 25 | /// Edit the JSON carried inside a ComposeRecordView's `text` string. |
| 26 | fn edit_text(d: &Path, rec: &str, f: impl FnOnce(&mut Value)) { |
| 27 | edit_json(d, rec, |v| { |
| 28 | let mut t: Value = serde_json::from_str(v["text"].as_str().unwrap()).unwrap(); |
| 29 | f(&mut t); |
| 30 | v["text"] = json!(serde_json::to_string(&t).unwrap()); |
| 31 | }); |
| 32 | } |
| 33 | |
| 34 | fn refused(d: &Path, why: &str) { |
| 35 | let v = verify(d); |
| 36 | assert!(v.starts_with("FAIL "), "{why}: {v}"); |
| 37 | } |
| 38 | |
| 39 | #[test] |
| 40 | fn real_chain_is_complete_because_every_link_is_verified_from_retained_bytes() { |
| 41 | assert_eq!(verify(Path::new(REAL)), OK); |
| 42 | let c = companion(Path::new(REAL)); |
| 43 | assert_eq!(c["completeness"]["state"], "complete"); |
| 44 | assert_eq!(c["completeness"]["missing"], json!([])); |
| 45 | } |
| 46 | |
| 47 | #[test] |
| 48 | fn real_chain_records_are_real_daemon_output() { |
| 49 | let c = companion(Path::new(REAL)); |
| 50 | assert_eq!(c["fixture"]["class"], "real"); |
| 51 | assert_eq!(c["aien"]["candidate_id"], Value::Null); |
| 52 | let log = std::fs::read_to_string(path_of(Path::new(REAL), "aien_load_log")).unwrap(); |
| 53 | assert!(log.contains("CPU-reference"), "CPU evidence only"); |
| 54 | assert!( |
| 55 | log.contains("Tokenizer: chatml"), |
| 56 | "the loader accepted the export's chat template" |
| 57 | ); |
| 58 | assert!( |
| 59 | log.contains("Warm-up: 1 token in"), |
| 60 | "the loaded model really ran inference" |
| 61 | ); |
| 62 | // The generation record is the daemon's: an `effect` note carrying the marker, written before |
| 63 | // the approved write, and the id the daemon returned in TurnFinished is the retained one. |
| 64 | let r: Value = serde_json::from_slice( |
| 65 | &std::fs::read(path_of(Path::new(REAL), "ledger_generation")).unwrap(), |
| 66 | ) |
| 67 | .unwrap(); |
| 68 | let g: Value = serde_json::from_str(r["text"].as_str().unwrap()).unwrap(); |
| 69 | assert_eq!( |
| 70 | (g["generation"].clone(), g["v"].clone()), |
| 71 | (json!(1), json!(1)) |
| 72 | ); |
| 73 | assert_eq!(r["note"], "effect"); |
| 74 | assert_eq!(r["verified"], true); |
| 75 | let t: Value = |
| 76 | serde_json::from_slice(&std::fs::read(path_of(Path::new(REAL), "model_turn")).unwrap()) |
| 77 | .unwrap(); |
| 78 | assert_eq!(t["generation_record_id"], r["id"]); |
| 79 | assert_eq!( |
| 80 | t["generation_record_source"], |
| 81 | "TurnFinished.generation_record" |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | #[test] |
| 86 | fn a_scripted_turn_is_never_complete() { |
| 87 | // Same real bytes, but declared as a scripted turn: the model link is missing. |
| 88 | let d = scratch(REAL, "real_scripted"); |
| 89 | let mut c = companion(&d); |
| 90 | c["effect"]["proposal_origin"] = json!("scripted_turn"); |
| 91 | c["completeness"] = |
| 92 | json!({"state": "incomplete", "missing": ["link:model_turn"], "reason": "scripted"}); |
| 93 | write_companion(&d, &c); |
| 94 | assert_eq!( |
| 95 | verify(&d), |
| 96 | "PASS_LABELLED_INCOMPLETE missing=link:model_turn effect=aien-ledger-slice/1:strong proposal=scripted_turn" |
| 97 | ); |
| 98 | // Declaring it complete is refused, whatever the bytes say. |
| 99 | c["completeness"] = json!({"state": "complete", "missing": []}); |
| 100 | write_companion(&d, &c); |
| 101 | assert_eq!( |
| 102 | verify(&d), |
| 103 | "FAIL unlabelled_missing: link:model_turn (declared complete)" |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | #[test] |
| 108 | fn the_driver_asserted_generation_origin_is_superseded() { |
| 109 | let d = scratch(REAL, "real_origin_v1"); |
| 110 | let mut c = companion(&d); |
| 111 | c["effect"]["proposal_origin"] = json!("model_generation/1"); |
| 112 | write_companion(&d, &c); |
| 113 | let v = verify(&d); |
| 114 | assert!( |
| 115 | v.starts_with("FAIL unsupported_binding: effect.proposal_origin=model_generation/1"), |
| 116 | "{v}" |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | #[test] |
| 121 | fn proposal_origin_must_be_stated_and_unknown_origins_are_refused() { |
| 122 | let d = scratch(REAL, "real_origin_absent"); |
| 123 | let mut c = companion(&d); |
| 124 | c["effect"] |
| 125 | .as_object_mut() |
| 126 | .unwrap() |
| 127 | .remove("proposal_origin"); |
| 128 | c["completeness"] = json!({"state": "complete", "missing": []}); |
| 129 | write_companion(&d, &c); |
| 130 | assert_eq!( |
| 131 | verify(&d), |
| 132 | "FAIL malformed_companion: effect.proposal_origin" |
| 133 | ); |
| 134 | c["effect"]["proposal_origin"] = json!("model_turn"); |
| 135 | write_companion(&d, &c); |
| 136 | assert_eq!( |
| 137 | verify(&d), |
| 138 | "FAIL unsupported_binding: effect.proposal_origin=model_turn" |
| 139 | ); |
| 140 | } |
| 141 | |
| 142 | #[test] |
| 143 | fn the_generated_text_must_be_the_text_the_dialect_parsed() { |
| 144 | // The model turn is changed to a different but valid call; the daemon's record still carries |
| 145 | // the digest of what the model returned. |
| 146 | let d = scratch(REAL, "real_turn_text"); |
| 147 | edit_json(&d, "model_turn", |t| { |
| 148 | let s = t["input"] |
| 149 | .as_str() |
| 150 | .unwrap() |
| 151 | .replace("summary.txt", "other.txt"); |
| 152 | t["input"] = json!(s); |
| 153 | }); |
| 154 | assert_eq!( |
| 155 | verify(&d), |
| 156 | "FAIL binding_mismatch: generation record output_text_sha256 != sha256(model_turn.input)" |
| 157 | ); |
| 158 | } |
| 159 | |
| 160 | #[test] |
| 161 | fn a_consistently_rewritten_turn_and_record_still_has_to_match_the_trace_request() { |
| 162 | let d = scratch(REAL, "real_turn_rewrite"); |
| 163 | let new = |s: &str| s.replace("summary.txt", "other.txt"); |
| 164 | edit_json(&d, "model_turn", |t| { |
| 165 | t["input"] = json!(new(t["input"].as_str().unwrap())) |
| 166 | }); |
| 167 | let text = |d: &Path| { |
| 168 | serde_json::from_slice::<Value>(&std::fs::read(path_of(d, "model_turn")).unwrap()).unwrap() |
| 169 | ["input"] |
| 170 | .as_str() |
| 171 | .unwrap() |
| 172 | .to_string() |
| 173 | }; |
| 174 | let digest = sha256_hex(text(&d).as_bytes()); |
| 175 | edit_text(&d, "ledger_generation", |g| { |
| 176 | g["output_text_sha256"] = json!(digest) |
| 177 | }); |
| 178 | assert_eq!( |
| 179 | verify(&d), |
| 180 | "FAIL binding_mismatch: model_turn arguments vs trace request" |
| 181 | ); |
| 182 | } |
| 183 | |
| 184 | #[test] |
| 185 | fn a_record_for_another_model_or_tokenizer_is_refused() { |
| 186 | let d = scratch(REAL, "real_gen_model"); |
| 187 | edit_text(&d, "ledger_generation", |g| { |
| 188 | g["model_sha256"] = json!("1".repeat(64)) |
| 189 | }); |
| 190 | assert_eq!( |
| 191 | verify(&d), |
| 192 | "FAIL binding_mismatch: generation record model_sha256 != export_weights" |
| 193 | ); |
| 194 | let d = scratch(REAL, "real_gen_tokenizer"); |
| 195 | edit_text(&d, "ledger_generation", |g| { |
| 196 | g["tokenizer_sha256"] = json!("2".repeat(64)) |
| 197 | }); |
| 198 | assert_eq!( |
| 199 | verify(&d), |
| 200 | "FAIL binding_mismatch: generation record tokenizer_sha256 != export_tokenizer" |
| 201 | ); |
| 202 | } |
| 203 | |
| 204 | #[test] |
| 205 | fn a_record_from_another_daemon_is_refused() { |
| 206 | let d = scratch(REAL, "real_gen_pid"); |
| 207 | edit_text(&d, "ledger_generation", |g| g["daemon"]["pid"] = json!(1)); |
| 208 | assert_eq!( |
| 209 | verify(&d), |
| 210 | "FAIL binding_mismatch: generation record daemon pid/start vs daemon_run" |
| 211 | ); |
| 212 | let d = scratch(REAL, "real_gen_start"); |
| 213 | edit_text(&d, "ledger_generation", |g| { |
| 214 | g["daemon"]["start_ticks"] = json!(1) |
| 215 | }); |
| 216 | assert_eq!( |
| 217 | verify(&d), |
| 218 | "FAIL binding_mismatch: generation record daemon pid/start vs daemon_run" |
| 219 | ); |
| 220 | } |
| 221 | |
| 222 | #[test] |
| 223 | fn a_missing_generation_record_is_an_absent_claim_not_a_pass() { |
| 224 | // Not retained at all: the manifest has no entry. |
| 225 | let d = scratch(REAL, "real_gen_missing"); |
| 226 | let mut c = companion(&d); |
| 227 | c["records"] |
| 228 | .as_object_mut() |
| 229 | .unwrap() |
| 230 | .remove("ledger_generation"); |
| 231 | write_companion(&d, &c); |
| 232 | assert_eq!(verify(&d), "FAIL missing_record_entry: ledger_generation"); |
| 233 | // Listed but the bytes are gone. |
| 234 | let d = scratch(REAL, "real_gen_deleted"); |
| 235 | std::fs::remove_file(path_of(&d, "ledger_generation")).unwrap(); |
| 236 | let v = verify(&d); |
| 237 | assert!(v.starts_with("FAIL "), "{v}"); |
| 238 | // The daemon returned no id: the run cannot claim a record. |
| 239 | let d = scratch(REAL, "real_gen_no_id"); |
| 240 | edit_json(&d, "model_turn", |t| { |
| 241 | t.as_object_mut().unwrap().remove("generation_record_id"); |
| 242 | }); |
| 243 | assert_eq!( |
| 244 | verify(&d), |
| 245 | "FAIL binding_mismatch: model_turn.generation_record_id != ledger_generation.id" |
| 246 | ); |
| 247 | } |
| 248 | |
| 249 | #[test] |
| 250 | fn a_generation_record_that_is_not_the_one_the_daemon_returned_is_refused() { |
| 251 | let d = scratch(REAL, "real_gen_other_id"); |
| 252 | edit_json(&d, "ledger_generation", |r| r["id"] = json!(3)); |
| 253 | assert_eq!( |
| 254 | verify(&d), |
| 255 | "FAIL binding_mismatch: model_turn.generation_record_id != ledger_generation.id" |
| 256 | ); |
| 257 | } |
| 258 | |
| 259 | #[test] |
| 260 | fn a_generation_record_must_precede_the_effect() { |
| 261 | let d = scratch(REAL, "real_gen_late"); |
| 262 | edit_json(&d, "ledger_generation", |r| r["id"] = json!(99)); |
| 263 | edit_json(&d, "model_turn", |t| t["generation_record_id"] = json!(99)); |
| 264 | assert_eq!( |
| 265 | verify(&d), |
| 266 | "FAIL binding_mismatch: generation record does not precede the effect's replay claim" |
| 267 | ); |
| 268 | } |
| 269 | |
| 270 | #[test] |
| 271 | fn a_malformed_generation_record_is_refused() { |
| 272 | type Case = (&'static str, fn(&mut Value), &'static str); |
| 273 | let cases: [Case; 6] = [ |
| 274 | ( |
| 275 | "unverified", |
| 276 | |r| r["verified"] = json!(false), |
| 277 | "ledger_generation.verified", |
| 278 | ), |
| 279 | ( |
| 280 | "wrong_note", |
| 281 | |r| r["note"] = json!("observation"), |
| 282 | "ledger_generation.note", |
| 283 | ), |
| 284 | ( |
| 285 | "links", |
| 286 | |r| r["links"] = json!([1, 0, 0, 0]), |
| 287 | "ledger_generation.links (must be empty)", |
| 288 | ), |
| 289 | ( |
| 290 | "not_json", |
| 291 | |r| r["text"] = json!("generation"), |
| 292 | "ledger_generation.text (not one JSON object)", |
| 293 | ), |
| 294 | ( |
| 295 | "trailing", |
| 296 | |r| { |
| 297 | let t = format!("{} {{}}", r["text"].as_str().unwrap()); |
| 298 | r["text"] = json!(t) |
| 299 | }, |
| 300 | "ledger_generation.text (not one JSON object)", |
| 301 | ), |
| 302 | ( |
| 303 | "array", |
| 304 | |r| r["text"] = json!("[1]"), |
| 305 | "ledger_generation.text (not one JSON object)", |
| 306 | ), |
| 307 | ]; |
| 308 | for (name, f, msg) in cases { |
| 309 | let d = scratch(REAL, &format!("real_gen_bad_{name}")); |
| 310 | edit_json(&d, "ledger_generation", f); |
| 311 | assert_eq!( |
| 312 | verify(&d), |
| 313 | format!("FAIL binding_mismatch: {msg}"), |
| 314 | "{name}" |
| 315 | ); |
| 316 | } |
| 317 | type Edit = fn(&mut Value); |
| 318 | let text_cases: [(&str, Edit, &str); 6] = [ |
| 319 | ( |
| 320 | "marker", |
| 321 | |g| g["generation"] = json!(2), |
| 322 | "ledger_generation.generation/v marker", |
| 323 | ), |
| 324 | ( |
| 325 | "marker_absent", |
| 326 | |g| { |
| 327 | g.as_object_mut().unwrap().remove("generation"); |
| 328 | }, |
| 329 | "ledger_generation.generation/v marker", |
| 330 | ), |
| 331 | ( |
| 332 | "version", |
| 333 | |g| g["v"] = json!(2), |
| 334 | "ledger_generation.generation/v marker", |
| 335 | ), |
| 336 | ( |
| 337 | "aborted", |
| 338 | |g| g["finish_reason"] = json!("aborted"), |
| 339 | "generation record finish_reason=aborted", |
| 340 | ), |
| 341 | ( |
| 342 | "no_tokens", |
| 343 | |g| g["output_tokens"] = json!(0), |
| 344 | "generation record output_tokens", |
| 345 | ), |
| 346 | ( |
| 347 | "ids_digest", |
| 348 | |g| g["prompt_ids_sha256"] = json!("zz"), |
| 349 | "generation record prompt_ids_sha256", |
| 350 | ), |
| 351 | ]; |
| 352 | for (name, f, msg) in text_cases { |
| 353 | let d = scratch(REAL, &format!("real_gen_badtext_{name}")); |
| 354 | edit_text(&d, "ledger_generation", f); |
| 355 | assert_eq!( |
| 356 | verify(&d), |
| 357 | format!("FAIL binding_mismatch: {msg}"), |
| 358 | "{name}" |
| 359 | ); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | #[test] |
| 364 | fn an_altered_generation_record_is_refused_whatever_field_changes() { |
| 365 | for (k, v) in [ |
| 366 | ("output_text_sha256", json!("3".repeat(64))), |
| 367 | ("model_sha256", json!("4".repeat(64))), |
| 368 | ("tokenizer_sha256", json!("5".repeat(64))), |
| 369 | ("finish_reason", json!("preempted")), |
| 370 | ] { |
| 371 | let d = scratch(REAL, &format!("real_gen_alter_{k}")); |
| 372 | edit_text(&d, "ledger_generation", |g| g[k] = v.clone()); |
| 373 | refused(&d, k); |
| 374 | } |
| 375 | // The caller-asserted ids are recorded, never trusted: changing them changes no verdict. |
| 376 | let d = scratch(REAL, "real_gen_claimed_ids"); |
| 377 | edit_text(&d, "ledger_generation", |g| g["request_id"] = json!(1)); |
| 378 | assert_eq!(verify(&d), OK); |
| 379 | } |
| 380 | |
| 381 | #[test] |
| 382 | fn an_altered_ledger_record_of_each_type_is_refused() { |
| 383 | type Edit = fn(&mut Value); |
| 384 | let cases: [(&str, Edit); 5] = [ |
| 385 | ("ledger_claim", |t| t["request_id"] = json!("w2")), |
| 386 | ("ledger_committed", |t| t["claim"] = json!(999)), |
| 387 | ("ledger_grant", |t| t["path"] = json!("other.txt")), |
| 388 | ("ledger_intent", |t| t["path"] = json!("other.txt")), |
| 389 | ("ledger_ack", |t| t["state"] = json!("FAILED")), |
| 390 | ]; |
| 391 | for (rec, f) in cases { |
| 392 | let d = scratch(REAL, &format!("real_alter_{rec}")); |
| 393 | edit_text(&d, rec, f); |
| 394 | refused(&d, rec); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | #[test] |
| 399 | fn an_altered_daemon_identity_trace_or_weights_is_refused() { |
| 400 | let d = scratch(REAL, "real_pid"); |
| 401 | edit_json(&d, "daemon_run", |v| v["pid"] = json!(1)); |
| 402 | refused(&d, "daemon pid"); |
| 403 | let d = scratch(REAL, "real_trace_content"); |
| 404 | edit_json(&d, "interplane_trace", |v| { |
| 405 | v[0]["payload"]["arguments"]["content"] = json!("something else\n") |
| 406 | }); |
| 407 | refused(&d, "trace content"); |
| 408 | let d = scratch(REAL, "real_model_swap"); |
| 409 | let p = path_of(&d, "export_weights"); |
| 410 | let mut b = std::fs::read(&p).unwrap(); |
| 411 | let n = b.len() - 1; |
| 412 | b[n] ^= 1; |
| 413 | std::fs::write(&p, b).unwrap(); |
| 414 | restamp(&d, "export_weights"); |
| 415 | refused(&d, "weights"); |
| 416 | } |