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/rust/crates/interplane-core/src/validate.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 / rust / crates / interplane-core / src / validate.rs` 84 lines ยท 2874 bytes Raw Blame History 1 //! Structural envelope validation: what the schemas express. Never authorization. 2 use crate::types::{Envelope, ErrorCode, Party, Payload}; 3 use crate::version::ProtocolVersion; 4 use regex::Regex; 5 use serde_json::Value; 6 use std::sync::OnceLock; 7 8 const KINDS: [&str; 8] = [ 9 "tool_request", 10 "capability_request", 11 "decision", 12 "result", 13 "event", 14 "catalog", 15 "selection", 16 "probe_report", 17 ]; 18 const PARTY_KINDS: [&str; 5] = ["model", "runtime", "adapter", "operator", "other"]; 19 20 fn id_re() -> &'static Regex { 21 static R: OnceLock = OnceLock::new(); 22 R.get_or_init(|| Regex::new(r"^[A-Za-z0-9._:-]{1,128}$").expect("regex")) 23 } 24 fn ts_re() -> &'static Regex { 25 static R: OnceLock = OnceLock::new(); 26 R.get_or_init(|| { 27 Regex::new(r"^\d{4}-\d{2}-\d{2}[Tt]\d{2}:\d{2}:\d{2}(\.\d+)?([Zz]|[+-]\d{2}:\d{2})$") 28 .expect("regex") 29 }) 30 } 31 32 /// `common.schema.json#/$defs/Id`. 33 pub fn is_valid_id(s: &str) -> bool { 34 id_re().is_match(s) 35 } 36 37 fn party_ok(p: &Party) -> bool { 38 PARTY_KINDS.contains(&p.kind.as_str()) && !p.id.is_empty() && p.id.chars().count() <= 256 39 } 40 41 /// Validate a parsed envelope. Checks, in order: version (`unsupported_version` before anything 42 /// else is looked at), ids, timestamp, parties, payload kind and payload shape. 43 pub fn validate_envelope(e: &Envelope) -> Result<(), ErrorCode> { 44 ProtocolVersion::parse(&e.interplane_version)?.check_major()?; 45 if !is_valid_id(&e.message_id) || !is_valid_id(&e.trace_id) { 46 return Err(ErrorCode::MalformedEnvelope); 47 } 48 if let Some(p) = &e.parent_id { 49 if !is_valid_id(p) { 50 return Err(ErrorCode::MalformedEnvelope); 51 } 52 } 53 if !ts_re().is_match(&e.timestamp) || !party_ok(&e.source) || !party_ok(&e.destination) { 54 return Err(ErrorCode::MalformedEnvelope); 55 } 56 let kind = e 57 .payload 58 .get("kind") 59 .and_then(Value::as_str) 60 .ok_or(ErrorCode::MalformedEnvelope)?; 61 if !KINDS.contains(&kind) { 62 return Err(ErrorCode::MalformedEnvelope); 63 } 64 let payload = Payload::from_value(&e.payload)?; 65 if let Payload::ToolRequest(t) = &payload { 66 if !is_valid_id(&t.request_id) { 67 return Err(ErrorCode::MalformedEnvelope); 68 } 69 } 70 Ok(()) 71 } 72 73 /// Validate raw JSON as an envelope. The version is checked first, even if the rest is malformed. 74 pub fn validate_envelope_value(v: &Value) -> Result { 75 let obj = v.as_object().ok_or(ErrorCode::MalformedEnvelope)?; 76 match obj.get("interplane_version").and_then(Value::as_str) { 77 Some(s) => ProtocolVersion::parse(s)?.check_major()?, 78 None => return Err(ErrorCode::MalformedEnvelope), 79 } 80 let e: Envelope = 81 serde_json::from_value(v.clone()).map_err(|_| ErrorCode::MalformedEnvelope)?; 82 validate_envelope(&e)?; 83 Ok(e) 84 }