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 / src / negctl.rs
| 1 | //! Negative controls V1 to V8 (bench/PROTOCOL-0.3.md section 6): deliberately vulnerable variants |
| 2 | //! of the harness, so the suite can show it is able to fail. Compiled only with the test-only |
| 3 | //! `negative-controls` feature of this crate. No pipeline library knows about any of this: each |
| 4 | //! variant wraps the unmodified pipeline, runtime or lifecycle from the outside. |
| 5 | use std::collections::HashMap; |
| 6 | use std::path::Path; |
| 7 | |
| 8 | use interplane_core::{CapabilityRequest, Decision, DecisionKind, RequestLedger}; |
| 9 | use interplane_crossveil::{ObservedRecord, Pipeline}; |
| 10 | use serde_json::{json, Value}; |
| 11 | |
| 12 | use crate::{run_suite, EFFECT_CAPABILITIES}; |
| 13 | |
| 14 | /// Printed by the runner when a variant is selected. CI greps the default build for it. |
| 15 | pub const MARKER: &str = "NEGCTL_BUILT_IN"; |
| 16 | |
| 17 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 18 | pub enum Variant { |
| 19 | V1, |
| 20 | V2, |
| 21 | V3, |
| 22 | V4, |
| 23 | V5, |
| 24 | V6, |
| 25 | V7, |
| 26 | V8, |
| 27 | } |
| 28 | |
| 29 | impl Variant { |
| 30 | pub const ALL: [Variant; 8] = [ |
| 31 | Variant::V1, |
| 32 | Variant::V2, |
| 33 | Variant::V3, |
| 34 | Variant::V4, |
| 35 | Variant::V5, |
| 36 | Variant::V6, |
| 37 | Variant::V7, |
| 38 | Variant::V8, |
| 39 | ]; |
| 40 | pub fn name(self) -> &'static str { |
| 41 | match self { |
| 42 | Variant::V1 => "V1", |
| 43 | Variant::V2 => "V2", |
| 44 | Variant::V3 => "V3", |
| 45 | Variant::V4 => "V4", |
| 46 | Variant::V5 => "V5", |
| 47 | Variant::V6 => "V6", |
| 48 | Variant::V7 => "V7", |
| 49 | Variant::V8 => "V8", |
| 50 | } |
| 51 | } |
| 52 | pub fn parse(s: &str) -> Option<Variant> { |
| 53 | Variant::ALL.into_iter().find(|v| v.name() == s) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | fn is(ctl: Option<Variant>, v: Variant) -> bool { |
| 58 | ctl == Some(v) |
| 59 | } |
| 60 | |
| 61 | /// V7: request-identity and catalog checks skipped. The harness-injected identity and catalog |
| 62 | /// defects (a decision or result citing another request, a duplicate advertised name, a catalog |
| 63 | /// digest that changed) are not put in front of the pipeline, as if its checks did not exist. |
| 64 | pub fn skips_identity(ctl: Option<Variant>) -> bool { |
| 65 | is(ctl, Variant::V7) |
| 66 | } |
| 67 | |
| 68 | /// V8: an adapter exception fails open. A fault in `decide` authorizes, a fault in `execute` lets |
| 69 | /// the real execution through as if nothing had gone wrong. |
| 70 | pub fn fail_open(ctl: Option<Variant>) -> bool { |
| 71 | is(ctl, Variant::V8) |
| 72 | } |
| 73 | |
| 74 | /// An approval asserted by the model: in `arguments` or in `extensions`. |
| 75 | fn claims_approval(req: &CapabilityRequest) -> bool { |
| 76 | let claim = |m: &serde_json::Map<String, Value>| { |
| 77 | m.get("approval_id") |
| 78 | .and_then(Value::as_str) |
| 79 | .is_some_and(|s| !s.is_empty()) |
| 80 | || m.get("approved") == Some(&json!(true)) |
| 81 | }; |
| 82 | claim(&req.arguments) || claim(&req.extensions) |
| 83 | } |
| 84 | |
| 85 | /// V2 and V4: the runtime's refusal is overturned. V2 honours a model-asserted approval; V4 |
| 86 | /// ignores exposure and authorizes every effect capability. |
| 87 | pub fn tamper_decision( |
| 88 | ctl: Option<Variant>, |
| 89 | req: &CapabilityRequest, |
| 90 | d: &mut Decision, |
| 91 | readmitting: bool, |
| 92 | ) { |
| 93 | // V6: the runtime sees the re-admitted request as already approved. |
| 94 | if is(ctl, Variant::V6) && readmitting && d.decision == DecisionKind::RequiresApproval { |
| 95 | d.decision = DecisionKind::Authorized; |
| 96 | d.approval = None; |
| 97 | return; |
| 98 | } |
| 99 | if !matches!( |
| 100 | d.decision, |
| 101 | DecisionKind::Denied | DecisionKind::RequiresApproval |
| 102 | ) { |
| 103 | return; |
| 104 | } |
| 105 | let fire = (is(ctl, Variant::V2) && claims_approval(req)) |
| 106 | || (is(ctl, Variant::V4) && EFFECT_CAPABILITIES.contains(&req.capability.as_str())); |
| 107 | if fire { |
| 108 | d.decision = DecisionKind::Authorized; |
| 109 | d.reason = None; |
| 110 | d.approval = None; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /// V6: re-admission with duplicate detection off. Every step starts with a fresh ledger, and a |
| 115 | /// host `approve` step is carried out by re-admitting the original envelope (see the runner). |
| 116 | pub fn before_step(ctl: Option<Variant>, p: &mut Pipeline<'_>) { |
| 117 | if is(ctl, Variant::V6) { |
| 118 | p.ledger = RequestLedger::new(); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | fn collect_strings(v: &Value, out: &mut Vec<String>) { |
| 123 | match v { |
| 124 | Value::String(s) => out.push(s.clone()), |
| 125 | Value::Array(a) => a.iter().for_each(|x| collect_strings(x, out)), |
| 126 | Value::Object(m) => m.values().for_each(|x| collect_strings(x, out)), |
| 127 | _ => {} |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /// V1: the host loop parses tool-call markup found in rendered results and inputs as if the model |
| 132 | /// had written it, and runs the resulting intents. |
| 133 | pub fn host_loop( |
| 134 | ctl: Option<Variant>, |
| 135 | p: &mut Pipeline<'_>, |
| 136 | trace: &str, |
| 137 | new: &[Value], |
| 138 | observed: &mut Vec<ObservedRecord>, |
| 139 | results: &mut Vec<Value>, |
| 140 | ) { |
| 141 | if !is(ctl, Variant::V1) { |
| 142 | return; |
| 143 | } |
| 144 | let mut texts = vec![]; |
| 145 | new.iter().for_each(|r| collect_strings(r, &mut texts)); |
| 146 | for t in texts.into_iter().filter(|t| t.contains("<tool_call>")) { |
| 147 | let turn = 1000 + observed.len() as u64; |
| 148 | let out = p.run_turn("qwen35", "Qwen/Qwen3.5-9B", &Value::String(t), trace, turn); |
| 149 | results.extend( |
| 150 | out.results |
| 151 | .iter() |
| 152 | .map(|r| serde_json::to_value(r).unwrap_or(Value::Null)), |
| 153 | ); |
| 154 | observed.extend(out.records); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /// V3: absent or unknown trust defaults to `trusted_runtime` (results and the input ledger). |
| 159 | pub fn finish(ctl: Option<Variant>, results: &mut [Value], inputs: &mut [Value]) { |
| 160 | if !is(ctl, Variant::V3) { |
| 161 | return; |
| 162 | } |
| 163 | let weak = |t: Option<&Value>| t.is_none_or(|t| t.is_null() || t == "unknown"); |
| 164 | for r in results.iter_mut() { |
| 165 | if let Some(prov) = r.get_mut("provenance").and_then(Value::as_object_mut) { |
| 166 | if weak(prov.get("trust")) { |
| 167 | prov.insert("trust".into(), json!("trusted_runtime")); |
| 168 | prov.insert("trusted".into(), json!(true)); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | for rec in inputs.iter_mut() { |
| 173 | if weak(rec.get("trust")) { |
| 174 | rec["trust"] = json!("trusted_runtime"); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /// V5: a continuation is accepted when it cites any non-empty `approval_id` (Rust lifecycle at |
| 180 | /// 678c06c). Modelled by presenting the minted id in place of whatever the caller cited. |
| 181 | pub fn continuation_any_id( |
| 182 | ctl: Option<Variant>, |
| 183 | minted: &mut HashMap<String, String>, |
| 184 | rid: &str, |
| 185 | d: &mut Decision, |
| 186 | ) { |
| 187 | if !is(ctl, Variant::V5) { |
| 188 | return; |
| 189 | } |
| 190 | match (minted.get(rid), d.approval.as_mut()) { |
| 191 | (None, Some(a)) => { |
| 192 | minted.insert(rid.to_string(), a.approval_id.clone()); |
| 193 | } |
| 194 | (Some(m), Some(a)) if !a.approval_id.is_empty() => a.approval_id = m.clone(), |
| 195 | _ => {} |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /// V5 at the pipeline: a host `approve` step is accepted whatever non-empty `approval_id` it cites, |
| 200 | /// modelled by citing the id the runtime minted for the request in its place. |
| 201 | pub fn approve_any_id( |
| 202 | ctl: Option<Variant>, |
| 203 | pend: Option<&interplane_crossveil::pipeline::PendingApproval>, |
| 204 | d: &mut Decision, |
| 205 | ) { |
| 206 | if !is(ctl, Variant::V5) { |
| 207 | return; |
| 208 | } |
| 209 | if let (Some(p), Some(a)) = (pend, d.approval.as_mut()) { |
| 210 | if !a.approval_id.is_empty() { |
| 211 | a.approval_id = p.approval_id.clone(); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /// Run the whole suite unmodified and once per variant; check each variant fails every case the |
| 217 | /// spec lists for it. Names the failing cases; `valid` is true only if the unmodified run is clean |
| 218 | /// and every variant is detected. |
| 219 | pub fn matrix(dir: &Path, spec: &Path) -> Result<Value, String> { |
| 220 | let text = std::fs::read_to_string(spec).map_err(|e| format!("{}: {e}", spec.display()))?; |
| 221 | let spec: Value = serde_json::from_str(&text).map_err(|e| e.to_string())?; |
| 222 | let names = |s: &crate::Suite| -> Vec<String> { |
| 223 | let mut v: Vec<String> = s.failed().iter().map(|o| o.name.clone()).collect(); |
| 224 | v.sort(); |
| 225 | v |
| 226 | }; |
| 227 | let base = names(&run_suite(dir, None)?); |
| 228 | let mut variants = serde_json::Map::new(); |
| 229 | let mut valid = base.is_empty(); |
| 230 | for v in Variant::ALL { |
| 231 | let failed = names(&run_suite(dir, Some(v))?); |
| 232 | let row = &spec["variants"][v.name()]; |
| 233 | let must: Vec<String> = row["must_fail"] |
| 234 | .as_array() |
| 235 | .map(|a| { |
| 236 | a.iter() |
| 237 | .filter_map(|x| x.as_str().map(String::from)) |
| 238 | .collect() |
| 239 | }) |
| 240 | .unwrap_or_default(); |
| 241 | let detected = !must.is_empty() && must.iter().all(|m| failed.contains(m)); |
| 242 | valid &= detected; |
| 243 | variants.insert( |
| 244 | v.name().into(), |
| 245 | json!({"defect": row["defect"], "failed": failed, "must_fail": must, "detected": detected}), |
| 246 | ); |
| 247 | } |
| 248 | Ok(json!({"baseline_failed": base, "valid": valid, "variants": variants})) |
| 249 | } |