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 / model_turn_link.rs
| 1 | //! `model-turn/2`: the retained model turn names the exact request and response bytes, the model |
| 2 | //! and weights digests, the backend and the times, and each is compared with what the bundle |
| 3 | //! retains. The base is the real daemon-generated chain (`fixtures/real-waldo-aien-chain`) upgraded |
| 4 | //! in a copy with the new request record and fields, so the committed fixture is never rewritten. |
| 5 | //! Every expected verdict is a literal. A turn that does not chain never yields `PASS`: it is |
| 6 | //! `FAIL`, or, when the bundle honestly says the turn is not a model's, the INCOMPLETE label. |
| 7 | mod common; |
| 8 | use common::*; |
| 9 | use interplane_provenance::gojson::sha256_hex; |
| 10 | use interplane_provenance::verify; |
| 11 | use serde_json::{json, Value}; |
| 12 | use std::path::Path; |
| 13 | |
| 14 | const REAL: &str = "fixtures/real-waldo-aien-chain"; |
| 15 | const COMPLETE: &str = |
| 16 | "PASS complete effect=aien-ledger-slice/1:strong proposal=model_generation/2 candidate=none"; |
| 17 | const REQUEST: &[u8] = |
| 18 | b"[{\"role\":\"user\",\"content\":\"Save the meeting summary to summary.txt.\"}]"; |
| 19 | |
| 20 | fn edit_json(d: &Path, rec: &str, f: impl FnOnce(&mut Value)) { |
| 21 | let p = path_of(d, rec); |
| 22 | let mut v: Value = serde_json::from_slice(&std::fs::read(&p).unwrap()).unwrap(); |
| 23 | f(&mut v); |
| 24 | std::fs::write(&p, serde_json::to_vec_pretty(&v).unwrap()).unwrap(); |
| 25 | restamp(d, rec); |
| 26 | } |
| 27 | |
| 28 | /// A copy of the real chain whose model turn is a `model-turn/2` record with a retained request. |
| 29 | fn v2(name: &str) -> std::path::PathBuf { |
| 30 | let d = scratch(REAL, name); |
| 31 | std::fs::create_dir_all(d.join("records/model")).unwrap(); |
| 32 | std::fs::write(d.join("records/model/request.json"), REQUEST).unwrap(); |
| 33 | let mut c = companion(&d); |
| 34 | c["records"]["model_request"] = json!({"path": "records/model/request.json", |
| 35 | "sha256": sha256_hex(REQUEST), "bytes": REQUEST.len()}); |
| 36 | let weights = c["records"]["export_weights"]["sha256"].clone(); |
| 37 | write_companion(&d, &c); |
| 38 | let log = std::fs::read_to_string(path_of(&d, "aien_load_log")).unwrap(); |
| 39 | let backend = log |
| 40 | .lines() |
| 41 | .find(|l| l.contains("Backend:")) |
| 42 | .unwrap() |
| 43 | .trim() |
| 44 | .to_string(); |
| 45 | edit_json(&d, "model_turn", |t| { |
| 46 | let input = t["input"].as_str().unwrap().to_string(); |
| 47 | t["record"] = json!("model-turn/2"); |
| 48 | t["model_id"] = json!("toolcall-tiny"); |
| 49 | t["request_record"] = json!("model_request"); |
| 50 | t["request_sha256"] = json!(sha256_hex(REQUEST)); |
| 51 | t["response_sha256"] = json!(sha256_hex(input.as_bytes())); |
| 52 | t["weights_sha256"] = weights; |
| 53 | t["backend"] = json!(backend); |
| 54 | }); |
| 55 | d |
| 56 | } |
| 57 | |
| 58 | fn failed(d: &Path, code: &str, why: &str) { |
| 59 | let v = verify(d); |
| 60 | assert!( |
| 61 | v.starts_with(&format!("FAIL {code}:")) && !v.starts_with("PASS"), |
| 62 | "{why}: {v}" |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | #[test] |
| 67 | fn a_model_turn_that_chains_by_digest_is_complete() { |
| 68 | let d = v2("mt-ok"); |
| 69 | assert_eq!(verify(&d), COMPLETE); |
| 70 | } |
| 71 | |
| 72 | #[test] |
| 73 | fn request_bytes_changed_after_the_digest_was_named_are_refused() { |
| 74 | let d = v2("mt-req"); |
| 75 | std::fs::write( |
| 76 | d.join("records/model/request.json"), |
| 77 | b"[{\"role\":\"user\",\"content\":\"other\"}]", |
| 78 | ) |
| 79 | .unwrap(); |
| 80 | restamp(&d, "model_request"); |
| 81 | failed( |
| 82 | &d, |
| 83 | "binding_mismatch", |
| 84 | "request bytes differ from request_sha256", |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | #[test] |
| 89 | fn a_request_that_is_not_a_messages_array_is_refused() { |
| 90 | let d = v2("mt-req-shape"); |
| 91 | std::fs::write(d.join("records/model/request.json"), b"{}").unwrap(); |
| 92 | restamp(&d, "model_request"); |
| 93 | edit_json(&d, "model_turn", |t| { |
| 94 | t["request_sha256"] = json!(sha256_hex(b"{}")) |
| 95 | }); |
| 96 | failed(&d, "binding_mismatch", "request is not a messages array"); |
| 97 | } |
| 98 | |
| 99 | #[test] |
| 100 | fn a_request_record_that_is_not_listed_is_refused() { |
| 101 | let d = v2("mt-req-unlisted"); |
| 102 | let mut c = companion(&d); |
| 103 | c["records"] |
| 104 | .as_object_mut() |
| 105 | .unwrap() |
| 106 | .remove("model_request"); |
| 107 | write_companion(&d, &c); |
| 108 | failed( |
| 109 | &d, |
| 110 | "missing_record_entry", |
| 111 | "request record dropped from the manifest", |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | #[test] |
| 116 | fn a_response_that_is_not_the_named_response_is_refused() { |
| 117 | let d = v2("mt-resp"); |
| 118 | edit_json(&d, "model_turn", |t| { |
| 119 | t["response_sha256"] = json!(sha256_hex(b"other")) |
| 120 | }); |
| 121 | failed(&d, "binding_mismatch", "response_sha256 differs"); |
| 122 | } |
| 123 | |
| 124 | #[test] |
| 125 | fn weights_digest_not_the_exported_file_is_refused() { |
| 126 | let d = v2("mt-weights"); |
| 127 | edit_json(&d, "model_turn", |t| { |
| 128 | t["weights_sha256"] = json!(sha256_hex(b"other weights")) |
| 129 | }); |
| 130 | failed(&d, "binding_mismatch", "weights_sha256 differs"); |
| 131 | } |
| 132 | |
| 133 | #[test] |
| 134 | fn a_backend_the_load_log_does_not_name_is_refused() { |
| 135 | let d = v2("mt-backend"); |
| 136 | edit_json(&d, "model_turn", |t| t["backend"] = json!("Backend: CUDA")); |
| 137 | failed(&d, "binding_mismatch", "backend not in the log"); |
| 138 | } |
| 139 | |
| 140 | #[test] |
| 141 | fn times_out_of_order_or_after_the_request_are_refused() { |
| 142 | let d = v2("mt-time-order"); |
| 143 | edit_json(&d, "model_turn", |t| { |
| 144 | t["stream_started"] = json!("2026-10-08T02:00:00Z"); |
| 145 | t["stream_finished"] = json!("2026-10-08T01:00:00Z"); |
| 146 | }); |
| 147 | failed(&d, "binding_mismatch", "finished before started"); |
| 148 | let d = v2("mt-time-late"); |
| 149 | edit_json(&d, "model_turn", |t| { |
| 150 | t["stream_started"] = json!("2026-10-08T03:00:00Z"); |
| 151 | t["stream_finished"] = json!("2026-10-08T03:00:01Z"); |
| 152 | }); |
| 153 | failed( |
| 154 | &d, |
| 155 | "binding_mismatch", |
| 156 | "generation finished after the request", |
| 157 | ); |
| 158 | } |
| 159 | |
| 160 | #[test] |
| 161 | fn a_turn_for_other_bytes_does_not_chain_to_the_effect() { |
| 162 | // The response text is replaced by a different tool call and every digest is restamped to |
| 163 | // match: the daemon's generation record and the trace request still name the old text. |
| 164 | let d = v2("mt-other-text"); |
| 165 | edit_json(&d, "model_turn", |t| { |
| 166 | let other = "<tool_call>\n{\"name\": \"write_file\", \"arguments\": {\"path\": \"summary.txt\", \"content\": \"other\\n\"}}\n</tool_call><|im_end|>\n"; |
| 167 | t["input"] = json!(other); |
| 168 | t["response_sha256"] = json!(sha256_hex(other.as_bytes())); |
| 169 | }); |
| 170 | failed( |
| 171 | &d, |
| 172 | "binding_mismatch", |
| 173 | "text differs from the daemon's generation record", |
| 174 | ); |
| 175 | } |
| 176 | |
| 177 | #[test] |
| 178 | fn a_missing_model_turn_is_a_refusal_and_a_scripted_one_is_labelled_incomplete() { |
| 179 | // Declared as a model's, not retained: refused. |
| 180 | let d = v2("mt-dropped"); |
| 181 | let mut c = companion(&d); |
| 182 | c["records"].as_object_mut().unwrap().remove("model_turn"); |
| 183 | write_companion(&d, &c); |
| 184 | assert!(verify(&d).starts_with("FAIL "), "{}", verify(&d)); |
| 185 | // The honest downgrade: the same bundle says the turn was scripted. Never PASS complete. |
| 186 | let d = v2("mt-scripted"); |
| 187 | let mut c = companion(&d); |
| 188 | c["effect"]["proposal_origin"] = json!("scripted_turn"); |
| 189 | c["completeness"] = json!({"state": "incomplete", "missing": ["link:model_turn"]}); |
| 190 | write_companion(&d, &c); |
| 191 | assert_eq!( |
| 192 | verify(&d), |
| 193 | "PASS_LABELLED_INCOMPLETE missing=link:model_turn effect=aien-ledger-slice/1:strong proposal=scripted_turn" |
| 194 | ); |
| 195 | // A scripted turn that claims completeness is refused. |
| 196 | let mut c = companion(&d); |
| 197 | c["completeness"] = json!({"state": "complete", "missing": []}); |
| 198 | write_companion(&d, &c); |
| 199 | failed(&d, "unlabelled_missing", "scripted but declared complete"); |
| 200 | } |
| 201 | |
| 202 | #[test] |
| 203 | fn the_version_one_turn_keeps_its_old_checks_and_is_not_read_as_having_the_new_links() { |
| 204 | let d = scratch(REAL, "mt-v1"); |
| 205 | assert_eq!(verify(&d), COMPLETE); |
| 206 | let t: Value = |
| 207 | serde_json::from_slice(&std::fs::read(path_of(&d, "model_turn")).unwrap()).unwrap(); |
| 208 | assert!(t.get("record").is_none() && t.get("request_sha256").is_none()); |
| 209 | } |