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/aien-protocols-mv/blob/main/crates/aien-probe/src/evaluator.rs # aien-protocols-mv Canonical specifications, wire protocols, and schemas for the AIEN sovereign ecosystem 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 `aien-protocols-mv / crates / aien-probe / src / evaluator.rs` 231 lines ยท 8169 bytes Raw Blame History 1 //! Adapter that plugs the probe engine into the unified Evaluator protocol. 2 //! 3 //! A [`ProbeEvaluator`] runs a [`ProbeSet`] over the evaluation subject and turns the calibrated 4 //! answers into an [`EvaluatorOutcome`]. Gates declare which answers must hold for the subject to 5 //! pass, so a [`ProbeEvaluator`] drops straight into the [`CanaryRollbackHarness`]. 6 //! 7 //! [`CanaryRollbackHarness`]: aien_evaluation_protocol::CanaryRollbackHarness 8 9 use crate::{Answer, ProbeBackend, ProbeEngine, ProbeSet}; 10 use aien_evaluation_protocol::{ 11 EvaluationError, EvaluationPlan, Evaluator, EvaluatorDescriptor, EvaluatorIdentity, 12 EvaluatorOutcome, EvaluatorStatus, Finding, Measurement, 13 }; 14 use aien_protocol_types::{ArtifactRef, Digest32}; 15 use async_trait::async_trait; 16 use serde_json::json; 17 use std::sync::Arc; 18 19 /// A requirement an answer must satisfy for the subject to pass. 20 #[derive(Clone, Debug)] 21 pub enum ProbeGate { 22 /// A [`Noul`](crate::Noul) probe whose probability must reach a minimum. 23 Noul { id: String, min: f64 }, 24 /// A [`Score`](crate::Score) probe whose position must reach a minimum. 25 Score { id: String, min: f64 }, 26 /// A [`Choice`](crate::Choice) probe whose selection must be in a set. 27 Choice { id: String, allowed: Vec }, 28 } 29 30 impl ProbeGate { 31 /// A noul gate. 32 pub fn noul(id: impl Into, min: f64) -> Self { 33 Self::Noul { id: id.into(), min } 34 } 35 36 /// A score gate. 37 pub fn score(id: impl Into, min: f64) -> Self { 38 Self::Score { id: id.into(), min } 39 } 40 41 /// A choice gate. 42 pub fn choice(id: impl Into, allowed: I) -> Self 43 where 44 I: IntoIterator, 45 S: Into, 46 { 47 Self::Choice { 48 id: id.into(), 49 allowed: allowed.into_iter().map(Into::into).collect(), 50 } 51 } 52 } 53 54 /// Configuration for a [`ProbeEvaluator`]. 55 #[derive(Clone, Debug, Default)] 56 pub struct ProbeEvaluatorConfig { 57 /// The gates a subject must satisfy. 58 pub gates: Vec, 59 } 60 61 impl ProbeEvaluatorConfig { 62 /// A configuration with no gates. 63 pub fn new() -> Self { 64 Self { gates: Vec::new() } 65 } 66 67 /// Adds a gate. 68 pub fn with_gate(mut self, gate: ProbeGate) -> Self { 69 self.gates.push(gate); 70 self 71 } 72 } 73 74 /// An [`Evaluator`] backed by the sovereign probe engine. 75 pub struct ProbeEvaluator { 76 engine: Arc>, 77 probes: ProbeSet, 78 config: ProbeEvaluatorConfig, 79 descriptor: EvaluatorDescriptor, 80 } 81 82 impl ProbeEvaluator { 83 /// Builds an evaluator over an engine, a probe set, and a gate configuration. 84 pub fn new(engine: ProbeEngine, probes: ProbeSet, config: ProbeEvaluatorConfig) -> Self { 85 Self { 86 engine: Arc::new(engine), 87 probes, 88 config, 89 descriptor: EvaluatorDescriptor { 90 evaluator_id: "aien-sovereign-probe".to_string(), 91 version: "0.1.0".to_string(), 92 required: true, 93 }, 94 } 95 } 96 97 /// Overrides the evaluator descriptor. 98 pub fn with_descriptor(mut self, descriptor: EvaluatorDescriptor) -> Self { 99 self.descriptor = descriptor; 100 self 101 } 102 103 /// The probe set the evaluator runs. 104 pub fn probes(&self) -> &ProbeSet { 105 &self.probes 106 } 107 } 108 109 #[async_trait] 110 impl Evaluator for ProbeEvaluator { 111 fn descriptor(&self) -> EvaluatorDescriptor { 112 self.descriptor.clone() 113 } 114 115 async fn evaluate( 116 &self, 117 plan: &EvaluationPlan, 118 subject: &ArtifactRef, 119 ) -> Result { 120 let state = json!({ 121 "evaluation_id": plan.evaluation_id.0.to_string(), 122 "profile": plan.profile, 123 "sandbox_profile": plan.sandbox_profile, 124 "subject": { 125 "artifact_id": subject.artifact_id.to_string(), 126 "media_type": subject.media_type, 127 "byte_size": subject.byte_size, 128 "digest": hex::encode(subject.digest.0), 129 } 130 }); 131 132 let response = self 133 .engine 134 .evaluate(&state, &self.probes) 135 .await 136 .map_err(|e| EvaluationError::ExecutionFailed(format!("probe engine failure: {e}")))?; 137 138 let mut findings = Vec::new(); 139 let mut measurements = Vec::new(); 140 141 for (id, answer) in &response.answers { 142 match answer { 143 Answer::Noul(noul) => measurements.push(Measurement { 144 metric: format!("probe_noul_{id}"), 145 value: noul.noul, 146 unit: "ratio".to_string(), 147 }), 148 Answer::Score(score) => measurements.push(Measurement { 149 metric: format!("probe_score_{id}"), 150 value: score.score, 151 unit: "points".to_string(), 152 }), 153 Answer::Choice(choice) => measurements.push(Measurement { 154 metric: format!("probe_choice_confidence_{id}"), 155 value: choice.confidence, 156 unit: "ratio".to_string(), 157 }), 158 } 159 } 160 161 for gate in &self.config.gates { 162 match gate { 163 ProbeGate::Noul { id, min } => { 164 let value = response.noul(id).map(|answer| answer.noul).unwrap_or(0.0); 165 if value < *min { 166 findings.push(Finding { 167 severity: "fatal".to_string(), 168 rule_id: format!("probe:noul:{id}"), 169 message: format!( 170 "noul probe {id} returned {value:.4}, below required {min:.4}" 171 ), 172 location: None, 173 }); 174 } 175 } 176 ProbeGate::Score { id, min } => { 177 let value = response.score(id).map(|answer| answer.score).unwrap_or(0.0); 178 if value < *min { 179 findings.push(Finding { 180 severity: "fatal".to_string(), 181 rule_id: format!("probe:score:{id}"), 182 message: format!( 183 "score probe {id} returned {value:.4}, below required {min:.4}" 184 ), 185 location: None, 186 }); 187 } 188 } 189 ProbeGate::Choice { id, allowed } => { 190 let chosen = response 191 .choice(id) 192 .map(|answer| answer.choice.clone()) 193 .unwrap_or_default(); 194 if !allowed.contains(&chosen) { 195 findings.push(Finding { 196 severity: "fatal".to_string(), 197 rule_id: format!("probe:choice:{id}"), 198 message: format!( 199 "choice probe {id} selected {chosen}, which is not in the allowed set" 200 ), 201 location: None, 202 }); 203 } 204 } 205 } 206 } 207 208 let status = if findings.is_empty() { 209 EvaluatorStatus::Passed 210 } else { 211 EvaluatorStatus::Failed 212 }; 213 214 let mut outcome = EvaluatorOutcome { 215 evaluator: EvaluatorIdentity { 216 evaluator_id: self.descriptor.evaluator_id.clone(), 217 version: self.descriptor.version.clone(), 218 binary_digest: Digest32([0u8; 32]), 219 config_digest: Digest32([0u8; 32]), 220 }, 221 status, 222 findings, 223 measurements, 224 evidence: vec![subject.clone()], 225 execution_digest: Digest32([0u8; 32]), 226 }; 227 outcome.execution_digest = outcome.compute_execution_digest(); 228 229 Ok(outcome) 230 } 231 }