aien-protocols-mv
Canonical specifications, wire protocols, and schemas for the AIEN sovereign ecosystem
Brought by drakestapleton
aien-protocols-mv / crates / aien-pi-spark-task / tests / tool_call.rs
| 1 | //! Tool-call tasks (SPEC.md section 11) against the shared vectors. |
| 2 | //! |
| 3 | //! Reads the same files the C reference checker reads, byte for byte: |
| 4 | //! `vectors/interplane-tool-request.json` (the INTERPLANE canonical request), |
| 5 | //! `vectors/ok-tool-call-request.pst`, and every recorded session in |
| 6 | //! `vectors/sessions/expected.txt` whose name contains `tool`. For each |
| 7 | //! session the Rust reader must print the same verdict line as |
| 8 | //! `pst_verify --session`. |
| 9 | //! |
| 10 | //! Scope: this reader checks the single-message rules ([`decode`]) and the |
| 11 | //! tool-call rules ([`TaskBody`]). The other session rules of section 10 |
| 12 | //! (side, subject, ordinals, context, the order table) are checked by the C |
| 13 | //! checker only; the tool vectors are built so that none of them decides the |
| 14 | //! verdict. |
| 15 | |
| 16 | use std::collections::HashMap; |
| 17 | use std::path::PathBuf; |
| 18 | |
| 19 | use aien_pi_spark_task::{ |
| 20 | decode, payload_digest, Refusal, TaskBody, ToolCallHead, TYPE_TOOL_CALL_CHUNK, |
| 21 | }; |
| 22 | |
| 23 | /// The digest interplane-core printed for the carried request at |
| 24 | /// aien-dev/interplane-mv 83df0f074b2c572473cd535c89f767df48933f4a. |
| 25 | const INTERPLANE_DIGEST: &str = |
| 26 | "sha256:78dbef7ee689b20ad9ae530fd7fa8417fb671778667b27f910484156776990d6"; |
| 27 | |
| 28 | fn vectors_dir() -> PathBuf { |
| 29 | PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../specs/pi-spark-task/vectors") |
| 30 | } |
| 31 | |
| 32 | fn read(name: &str) -> Vec<u8> { |
| 33 | std::fs::read(vectors_dir().join(name)).unwrap_or_else(|e| panic!("read {name}: {e}")) |
| 34 | } |
| 35 | |
| 36 | #[test] |
| 37 | fn the_head_names_the_carried_interplane_request() { |
| 38 | let canonical = read("interplane-tool-request.json"); |
| 39 | let bytes = read("ok-tool-call-request.pst"); |
| 40 | let msg = decode(&bytes).expect("ok-tool-call-request.pst is accepted"); |
| 41 | let head = ToolCallHead::parse(msg.payload).expect("a 36-byte head"); |
| 42 | assert_eq!(head.interplane_digest_string(), INTERPLANE_DIGEST); |
| 43 | assert_eq!(payload_digest(&canonical), head.interplane_digest); |
| 44 | assert_eq!(head.canonical_len as usize, canonical.len()); |
| 45 | } |
| 46 | |
| 47 | /// One recorded session (section 10.4 container), checked for the |
| 48 | /// single-message and tool-call rules. Also returns the canonical bytes of |
| 49 | /// every tool call that completed. |
| 50 | fn tool_session(b: &[u8]) -> (String, Vec<Vec<u8>>) { |
| 51 | let mut tasks: HashMap<[u8; 16], TaskBody> = HashMap::new(); |
| 52 | let mut done = Vec::new(); |
| 53 | assert_eq!(&b[..4], b"PSS1", "recording magic"); |
| 54 | let mut o = 4; |
| 55 | let mut step = 0usize; |
| 56 | let refuse = |r: Refusal, step: usize| format!("refuse {} step={step}", r.code()); |
| 57 | while o < b.len() { |
| 58 | step += 1; |
| 59 | let side = b[o]; |
| 60 | let len = usize::from(u16::from_be_bytes([b[o + 1], b[o + 2]])); |
| 61 | let rec = &b[o + 3..o + 3 + len]; |
| 62 | o += 3 + len; |
| 63 | let msg = match decode(rec) { |
| 64 | Ok(m) => m, |
| 65 | Err(r) => return (refuse(r, step), done), |
| 66 | }; |
| 67 | let id = msg.header.task_id; |
| 68 | if let Some(body) = TaskBody::open(&msg) { |
| 69 | match body { |
| 70 | Ok(body) => { |
| 71 | tasks.entry(id).or_insert(body); |
| 72 | } |
| 73 | Err(r) => return (refuse(r, step), done), |
| 74 | } |
| 75 | continue; |
| 76 | } |
| 77 | let Some(task) = tasks.get_mut(&id) else { |
| 78 | panic!("step {step}: a tool vector names an unknown task"); |
| 79 | }; |
| 80 | if msg.header.msg_type == TYPE_TOOL_CALL_CHUNK { |
| 81 | match task.chunk(msg.payload) { |
| 82 | Ok(true) => { |
| 83 | if let TaskBody::ToolCall(call) = task { |
| 84 | done.push(call.canonical_bytes().expect("verified").to_vec()); |
| 85 | } |
| 86 | } |
| 87 | Ok(false) => {} |
| 88 | Err(r) => return (refuse(r, step), done), |
| 89 | } |
| 90 | } else if side == 2 { |
| 91 | if let Err(r) = task.check_spark(msg.header.msg_type) { |
| 92 | return (refuse(r, step), done); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | (format!("ok steps={step} tasks={}", tasks.len()), done) |
| 97 | } |
| 98 | |
| 99 | #[test] |
| 100 | fn every_tool_session_matches_the_c_checker() { |
| 101 | let canonical = read("interplane-tool-request.json"); |
| 102 | let list = String::from_utf8(read("sessions/expected.txt")).expect("utf-8"); |
| 103 | let mut checked = 0usize; |
| 104 | let mut carried = 0usize; |
| 105 | let mut failures = Vec::new(); |
| 106 | for line in list.lines() { |
| 107 | if line.is_empty() || line.starts_with('#') { |
| 108 | continue; |
| 109 | } |
| 110 | let (name, want) = line.split_once(' ').expect("file name and verdict"); |
| 111 | if !name.contains("tool") { |
| 112 | continue; |
| 113 | } |
| 114 | let (got, done) = tool_session(&read(&format!("sessions/{name}"))); |
| 115 | if got != want { |
| 116 | failures.push(format!("{name}\n expected: {want}\n got: {got}")); |
| 117 | } |
| 118 | for bytes in done { |
| 119 | // Carried unchanged: the joined chunks are the INTERPLANE bytes. |
| 120 | if bytes != canonical { |
| 121 | failures.push(format!( |
| 122 | "{name}: joined bytes differ from the INTERPLANE request" |
| 123 | )); |
| 124 | } |
| 125 | carried += 1; |
| 126 | } |
| 127 | checked += 1; |
| 128 | } |
| 129 | assert!( |
| 130 | failures.is_empty(), |
| 131 | "{} of {checked} tool sessions differ:\n{}", |
| 132 | failures.len(), |
| 133 | failures.join("\n") |
| 134 | ); |
| 135 | assert!( |
| 136 | checked >= 9, |
| 137 | "expected at least 9 tool sessions, found {checked}" |
| 138 | ); |
| 139 | assert!(carried >= 1, "no tool session carried the whole request"); |
| 140 | } |