interplane-mv
INTERPLANE: open interoperability protocol between AI models and agent runtimes. Models express intent. Runtimes retain authority.
Brought by drakestapleton
interplane-mv / rust / crates / interplane-conformance / tests / schemas.rs
| 1 | //! Every envelope, result, decision, capability request, catalog and selection the reference |
| 2 | //! implementation consumes or produces is validated against `spec/schemas/*.json`. |
| 3 | use std::path::PathBuf; |
| 4 | |
| 5 | use interplane_conformance::{load_fixtures, run_case}; |
| 6 | use interplane_core::*; |
| 7 | use interplane_crossaxis::{ |
| 8 | coerce_arguments, expand, measure_tokens, openai_tools_renderer, select, BenchTotals, |
| 9 | EstimatedTokens, |
| 10 | }; |
| 11 | use interplane_crossveil::{mock_mapping_table, MockRuntime, RuntimeAuthority}; |
| 12 | use interplane_lenshift::{DialectRegistry, ParseContext}; |
| 13 | use jsonschema::{Resource, Validator}; |
| 14 | use serde_json::{json, Value}; |
| 15 | |
| 16 | fn root() -> PathBuf { |
| 17 | PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../..") |
| 18 | } |
| 19 | |
| 20 | fn validator(name: &str) -> Validator { |
| 21 | let dir = root().join("spec/schemas"); |
| 22 | let mut resources = vec![]; |
| 23 | for e in std::fs::read_dir(&dir).unwrap() { |
| 24 | let p = e.unwrap().path(); |
| 25 | let v: Value = serde_json::from_str(&std::fs::read_to_string(&p).unwrap()).unwrap(); |
| 26 | let id = v["$id"].as_str().unwrap().to_string(); |
| 27 | resources.push((id, Resource::from_contents(v).unwrap())); |
| 28 | } |
| 29 | let main: Value = serde_json::from_str( |
| 30 | &std::fs::read_to_string(dir.join(format!("{name}.schema.json"))).unwrap(), |
| 31 | ) |
| 32 | .unwrap(); |
| 33 | jsonschema::options() |
| 34 | .with_resources(resources.into_iter()) |
| 35 | .build(&main) |
| 36 | .expect("schema compiles") |
| 37 | } |
| 38 | |
| 39 | fn check(v: &Validator, what: &str, inst: &Value) { |
| 40 | let errs: Vec<String> = v |
| 41 | .iter_errors(inst) |
| 42 | .map(|e| format!("{e} at {}", e.instance_path)) |
| 43 | .collect(); |
| 44 | assert!( |
| 45 | errs.is_empty(), |
| 46 | "{what} violates its schema: {errs:?}\n{inst}" |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | fn fixtures() -> Vec<Value> { |
| 51 | load_fixtures(&root().join("conformance/fixtures")).expect("fixtures") |
| 52 | } |
| 53 | |
| 54 | #[test] |
| 55 | fn fixture_envelopes_validate() { |
| 56 | let env = validator("envelope"); |
| 57 | let mut n = 0; |
| 58 | for fx in fixtures() { |
| 59 | for step in fx["steps"].as_array().unwrap() { |
| 60 | if let Some(e) = step.get("envelope") { |
| 61 | if e["interplane_version"] == "0.1" { |
| 62 | check(&env, &format!("{} envelope", fx["case"]), e); |
| 63 | n += 1; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | assert!(n >= 3); |
| 69 | } |
| 70 | |
| 71 | #[test] |
| 72 | fn produced_messages_validate() { |
| 73 | let (envs, res, dec, cap) = ( |
| 74 | validator("envelope"), |
| 75 | validator("result"), |
| 76 | validator("decision"), |
| 77 | validator("capability"), |
| 78 | ); |
| 79 | let reg = DialectRegistry::with_defaults(); |
| 80 | let table = mock_mapping_table(); |
| 81 | let rt = MockRuntime::new(); |
| 82 | let catalog = rt.catalog(); |
| 83 | let mut results = 0; |
| 84 | for fx in fixtures() { |
| 85 | let run = run_case(&fx); |
| 86 | for r in &run.results { |
| 87 | if r["request_id"].is_null() { |
| 88 | continue; // CORE.md allows a null request_id before an id exists; the schema requires a string. |
| 89 | } |
| 90 | check(&res, &format!("{} result", run.case), r); |
| 91 | if r["decision"].is_object() { |
| 92 | check(&dec, &format!("{} decision", run.case), &r["decision"]); |
| 93 | } |
| 94 | results += 1; |
| 95 | } |
| 96 | for step in fx["steps"].as_array().unwrap() { |
| 97 | let Some(d) = step["dialect"].as_str() else { |
| 98 | continue; |
| 99 | }; |
| 100 | let Ok(dialect) = reg.get(d) else { continue }; |
| 101 | let ctx = ParseContext { |
| 102 | trace_id: "t".into(), |
| 103 | turn: 0, |
| 104 | model: step["model"].as_str().unwrap_or("").into(), |
| 105 | }; |
| 106 | for (i, intent) in dialect |
| 107 | .parse(&step["input"], &ctx) |
| 108 | .intents |
| 109 | .iter() |
| 110 | .enumerate() |
| 111 | { |
| 112 | let iv = serde_json::to_value(intent).unwrap(); |
| 113 | let e = Envelope::new( |
| 114 | &format!("m-{i}"), |
| 115 | "t", |
| 116 | "2026-01-01T00:00:00Z", |
| 117 | Party::new("model", "x"), |
| 118 | Party::new("runtime", "mock"), |
| 119 | iv, |
| 120 | ); |
| 121 | check( |
| 122 | &envs, |
| 123 | &format!("{} wrapped intent", run.case), |
| 124 | &serde_json::to_value(&e).unwrap(), |
| 125 | ); |
| 126 | if let Ok(mut c) = table.map(intent) { |
| 127 | if let Some(d) = catalog.capabilities.iter().find(|x| x.name == c.capability) { |
| 128 | coerce_arguments(&mut c, d); |
| 129 | } |
| 130 | check( |
| 131 | &cap, |
| 132 | &format!("{} capability_request", run.case), |
| 133 | &serde_json::to_value(&c).unwrap(), |
| 134 | ); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | assert!(results >= 25); |
| 140 | } |
| 141 | |
| 142 | #[test] |
| 143 | fn catalog_selection_and_events_validate() { |
| 144 | let rt = MockRuntime::new(); |
| 145 | let catalog = rt.catalog(); |
| 146 | check( |
| 147 | &validator("catalog"), |
| 148 | "catalog", |
| 149 | &serde_json::to_value(&catalog).unwrap(), |
| 150 | ); |
| 151 | let (sel, _) = select( |
| 152 | &catalog, |
| 153 | &["filesystem".to_string()], |
| 154 | Some(3), |
| 155 | &["send_email".to_string()], |
| 156 | ); |
| 157 | check( |
| 158 | &validator("selection"), |
| 159 | "selection", |
| 160 | &serde_json::to_value(&sel).unwrap(), |
| 161 | ); |
| 162 | let ev = Event { |
| 163 | kind: EventMsgKind, |
| 164 | event: EventKind::ToolDecision, |
| 165 | seq: 1, |
| 166 | request_id: Some("r".into()), |
| 167 | turn: Some(0), |
| 168 | payload: None, |
| 169 | extensions: Default::default(), |
| 170 | }; |
| 171 | check( |
| 172 | &validator("event"), |
| 173 | "event", |
| 174 | &serde_json::to_value(&ev).unwrap(), |
| 175 | ); |
| 176 | // negative control: the validator really rejects. |
| 177 | let bad = json!({"kind": "result", "request_id": "r", "status": "denied"}); |
| 178 | assert!( |
| 179 | !validator("result").is_valid(&bad), |
| 180 | "non-ok result without error must fail" |
| 181 | ); |
| 182 | } |
| 183 | |
| 184 | /// Selection receipts with a token `measure` block and with expansions validate, and the schema |
| 185 | /// refuses a malformed refusal entry and an unknown token unit (ported from the retired Python |
| 186 | /// test `test_expansion_receipts_and_measure_validate`). |
| 187 | #[test] |
| 188 | fn expansion_receipts_and_measure_validate() { |
| 189 | let v = validator("selection"); |
| 190 | let errors = |inst: &Value| v.iter_errors(inst).count(); |
| 191 | let catalog = MockRuntime::new().catalog(); |
| 192 | let (mut sel, caps) = select( |
| 193 | &catalog, |
| 194 | &["email".to_string()], |
| 195 | Some(3), |
| 196 | &["fail_tool".to_string()], |
| 197 | ); |
| 198 | check(&v, "selection", &serde_json::to_value(&sel).unwrap()); |
| 199 | let msgs = json!([{"role": "user", "content": "hi"}]); |
| 200 | measure_tokens( |
| 201 | &mut sel, |
| 202 | &catalog, |
| 203 | &caps, |
| 204 | openai_tools_renderer, |
| 205 | Some((&EstimatedTokens, &msgs, BenchTotals::default())), |
| 206 | ); |
| 207 | let measured = serde_json::to_value(&sel).unwrap(); |
| 208 | assert!(measured["measure"]["tokens"].is_object(), "{measured}"); |
| 209 | check(&v, "measured selection", &measured); |
| 210 | let out = expand( |
| 211 | &sel, |
| 212 | &catalog, |
| 213 | &json!({"kind": "requested_excluded", "name": "read_file"}), |
| 214 | None, |
| 215 | None, |
| 216 | ) |
| 217 | .unwrap(); |
| 218 | let out = expand( |
| 219 | &out, |
| 220 | &catalog, |
| 221 | &json!({"kind": "discovery_hit", "names": ["web_fetch", "nope"]}), |
| 222 | None, |
| 223 | None, |
| 224 | ) |
| 225 | .unwrap(); |
| 226 | let expanded = serde_json::to_value(&out).unwrap(); |
| 227 | check(&v, "expanded selection", &expanded); |
| 228 | let fixtures = root().join("conformance/fixtures"); |
| 229 | for e in std::fs::read_dir(&fixtures).unwrap() { |
| 230 | let p = e.unwrap().path(); |
| 231 | let name = p.file_name().unwrap().to_string_lossy().to_string(); |
| 232 | if !(name.starts_with("24-") || name.starts_with("25-")) { |
| 233 | continue; |
| 234 | } |
| 235 | let f: Value = serde_json::from_str(&std::fs::read_to_string(&p).unwrap()).unwrap(); |
| 236 | for s in f["expected"]["selections"].as_array().unwrap() { |
| 237 | check(&v, &name, s); |
| 238 | } |
| 239 | } |
| 240 | let mut bad = expanded.clone(); |
| 241 | bad["expansions"][0]["refused"] = json!([{"name": "x"}]); |
| 242 | assert!(errors(&bad) > 0, "malformed refused entry accepted"); |
| 243 | let mut bad = measured; |
| 244 | bad["measure"]["tokens"]["unit"] = json!("tokens"); |
| 245 | assert!(errors(&bad) > 0, "unknown token unit accepted"); |
| 246 | } |