spark-inquisitor
Ultra-fast compiled native Rust gatekeeper and automated code reviewer
Brought by drakestapleton
spark-inquisitor / src / probe_audit.rs
| 1 | use aien_evaluation_protocol::{ |
| 2 | EvaluationError, EvaluationPlan, Evaluator, EvaluatorDescriptor, EvaluatorIdentity, |
| 3 | EvaluatorOutcome, EvaluatorStatus, Finding, Measurement, |
| 4 | }; |
| 5 | use aien_probe::{ |
| 6 | Choice, ChoiceOption, DeterministicReferenceBackend, HttpProbeBackend, Noul, Probe, |
| 7 | ProbeBackend, ProbeEngine, ProbeSet, Score, |
| 8 | }; |
| 9 | use aien_protocol_types::{ArtifactRef, Digest32}; |
| 10 | use serde_json::json; |
| 11 | |
| 12 | /// The sovereign probe audit report for one git diff. |
| 13 | #[derive(Clone, Debug)] |
| 14 | pub struct ProbeAuditReport { |
| 15 | pub markdown: String, |
| 16 | pub clean: bool, |
| 17 | pub telemetry_probability: f64, |
| 18 | pub unslop_probability: f64, |
| 19 | pub quality_score: f64, |
| 20 | pub classification: String, |
| 21 | pub backend: String, |
| 22 | } |
| 23 | |
| 24 | /// The standard probe set used for diff audits. |
| 25 | pub fn diff_probe_set() -> ProbeSet { |
| 26 | ProbeSet::new() |
| 27 | .with( |
| 28 | "telemetry", |
| 29 | Probe::Noul(Noul::new( |
| 30 | "Is this diff free of covert network calls, analytics, trackers, and surveillance telemetry?", |
| 31 | )), |
| 32 | ) |
| 33 | .with( |
| 34 | "unslop", |
| 35 | Probe::Noul(Noul::new( |
| 36 | "Does this diff use affirmative, direct technical voice with zero formulaic tropes and zero em or en dashes?", |
| 37 | )), |
| 38 | ) |
| 39 | .with( |
| 40 | "quality", |
| 41 | Probe::Score(Score::new( |
| 42 | "Rate the overall code quality and architectural safety of this diff.", |
| 43 | ["critical", "weak", "acceptable", "exemplary"], |
| 44 | )), |
| 45 | ) |
| 46 | .with( |
| 47 | "classification", |
| 48 | Probe::Choice(Choice::new( |
| 49 | "Classify the change.", |
| 50 | [ |
| 51 | ChoiceOption::new("clean").with_description("Verified clean sovereign contribution"), |
| 52 | ChoiceOption::new("advisory").with_description("Minor non-blocking recommendations"), |
| 53 | ChoiceOption::new("suspicious").with_description("Questionable logic requiring manual audit"), |
| 54 | ChoiceOption::new("critical").with_description("Critical invariant violation or covert telemetry"), |
| 55 | ], |
| 56 | )), |
| 57 | ) |
| 58 | } |
| 59 | |
| 60 | /// Evaluates a diff using any provided sovereign probe engine. |
| 61 | pub async fn audit_diff_with_probes<B: ProbeBackend + 'static>( |
| 62 | engine: &ProbeEngine<B>, |
| 63 | diff: &str, |
| 64 | min_telemetry: f64, |
| 65 | min_quality: f64, |
| 66 | ) -> Result<ProbeAuditReport, String> { |
| 67 | let state = json!({ |
| 68 | "diff": diff, |
| 69 | "line_count": diff.lines().count(), |
| 70 | }); |
| 71 | |
| 72 | let probes = diff_probe_set(); |
| 73 | let response = engine |
| 74 | .evaluate(&state, &probes) |
| 75 | .await |
| 76 | .map_err(|e| format!("Probe engine execution failed: {e}"))?; |
| 77 | |
| 78 | let telemetry = response.noul("telemetry").map(|a| a.noul).unwrap_or(0.0); |
| 79 | let unslop = response.noul("unslop").map(|a| a.noul).unwrap_or(0.0); |
| 80 | let quality = response.score("quality").map(|a| a.score).unwrap_or(0.0); |
| 81 | let classification = response |
| 82 | .choice("classification") |
| 83 | .map(|a| a.choice.clone()) |
| 84 | .unwrap_or_else(|| "unclassified".to_string()); |
| 85 | |
| 86 | let telemetry_ok = telemetry >= min_telemetry; |
| 87 | let quality_ok = quality >= min_quality; |
| 88 | let classification_ok = classification == "clean" || classification == "advisory"; |
| 89 | let clean = telemetry_ok && quality_ok && classification_ok; |
| 90 | |
| 91 | let status_str = if clean { "PASS" } else { "FAIL" }; |
| 92 | |
| 93 | let markdown = format!( |
| 94 | "### Sovereign Probe Audit: {}\n\n\ |
| 95 | - **Backend**: `{}`\n\ |
| 96 | - **Classification**: `{}`\n\ |
| 97 | - **Telemetry Safety Probability**: `{:.4}` (Threshold: `>= {:.4}`)\n\ |
| 98 | - **Unslop Alignment Probability**: `{:.4}`\n\ |
| 99 | - **Architectural Quality Score**: `{:.2} / 3.00` (Threshold: `>= {:.2}`)\n\ |
| 100 | - **Verdict**: `{}`\n", |
| 101 | status_str, |
| 102 | response.model, |
| 103 | classification, |
| 104 | telemetry, |
| 105 | min_telemetry, |
| 106 | unslop, |
| 107 | quality, |
| 108 | min_quality, |
| 109 | status_str |
| 110 | ); |
| 111 | |
| 112 | Ok(ProbeAuditReport { |
| 113 | markdown, |
| 114 | clean, |
| 115 | telemetry_probability: telemetry, |
| 116 | unslop_probability: unslop, |
| 117 | quality_score: quality, |
| 118 | classification, |
| 119 | backend: response.model, |
| 120 | }) |
| 121 | } |
| 122 | |
| 123 | /// Runs probe audit against a diff string using the specified backend. |
| 124 | pub async fn run_probe_audit( |
| 125 | diff: &str, |
| 126 | backend: &str, |
| 127 | endpoint: Option<String>, |
| 128 | min_telemetry: f64, |
| 129 | min_quality: f64, |
| 130 | ) -> Result<ProbeAuditReport, String> { |
| 131 | match backend { |
| 132 | "reference" | "oracle" => { |
| 133 | let engine = ProbeEngine::new(DeterministicReferenceBackend::new()); |
| 134 | audit_diff_with_probes(&engine, diff, min_telemetry, min_quality).await |
| 135 | } |
| 136 | "http" | "gb10" => { |
| 137 | let ep = endpoint.unwrap_or_else(|| "http://127.0.0.1:18006".to_string()); |
| 138 | let backend_client = HttpProbeBackend::with_endpoint(ep, "atlas-lightning-omni"); |
| 139 | let engine = ProbeEngine::new(backend_client); |
| 140 | audit_diff_with_probes(&engine, diff, min_telemetry, min_quality).await |
| 141 | } |
| 142 | other => Err(format!( |
| 143 | "Unknown probe backend '{}'; expected 'reference' or 'http'", |
| 144 | other |
| 145 | )), |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /// Evaluator adapter plugging probe audits into the CanaryRollbackHarness. |
| 150 | pub struct InquisitorProbeEvaluator { |
| 151 | diff_payload: Option<String>, |
| 152 | min_telemetry: f64, |
| 153 | min_quality: f64, |
| 154 | } |
| 155 | |
| 156 | impl InquisitorProbeEvaluator { |
| 157 | pub fn new() -> Self { |
| 158 | Self { |
| 159 | diff_payload: None, |
| 160 | min_telemetry: 0.95, |
| 161 | min_quality: 2.0, |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | pub fn with_diff(diff: impl Into<String>) -> Self { |
| 166 | Self { |
| 167 | diff_payload: Some(diff.into()), |
| 168 | min_telemetry: 0.95, |
| 169 | min_quality: 2.0, |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | pub fn with_thresholds(mut self, min_telemetry: f64, min_quality: f64) -> Self { |
| 174 | self.min_telemetry = min_telemetry; |
| 175 | self.min_quality = min_quality; |
| 176 | self |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | impl Default for InquisitorProbeEvaluator { |
| 181 | fn default() -> Self { |
| 182 | Self::new() |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | #[async_trait::async_trait] |
| 187 | impl Evaluator for InquisitorProbeEvaluator { |
| 188 | fn descriptor(&self) -> EvaluatorDescriptor { |
| 189 | EvaluatorDescriptor { |
| 190 | evaluator_id: "spark-inquisitor-probe".to_string(), |
| 191 | version: "0.2.0".to_string(), |
| 192 | required: true, |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | async fn evaluate( |
| 197 | &self, |
| 198 | _plan: &EvaluationPlan, |
| 199 | _subject: &ArtifactRef, |
| 200 | ) -> Result<EvaluatorOutcome, EvaluationError> { |
| 201 | let diff = self.diff_payload.as_deref().unwrap_or(""); |
| 202 | let report = run_probe_audit( |
| 203 | diff, |
| 204 | "reference", |
| 205 | None, |
| 206 | self.min_telemetry, |
| 207 | self.min_quality, |
| 208 | ) |
| 209 | .await |
| 210 | .map_err(EvaluationError::ExecutionFailed)?; |
| 211 | |
| 212 | let mut findings = Vec::new(); |
| 213 | if report.telemetry_probability < self.min_telemetry { |
| 214 | findings.push(Finding { |
| 215 | severity: "Fatal".to_string(), |
| 216 | rule_id: "PROBE_TELEMETRY_THRESHOLD".to_string(), |
| 217 | message: format!( |
| 218 | "Telemetry safety probability {:.4} failed threshold {:.4}", |
| 219 | report.telemetry_probability, self.min_telemetry |
| 220 | ), |
| 221 | location: None, |
| 222 | }); |
| 223 | } |
| 224 | if report.quality_score < self.min_quality { |
| 225 | findings.push(Finding { |
| 226 | severity: "Fatal".to_string(), |
| 227 | rule_id: "PROBE_QUALITY_THRESHOLD".to_string(), |
| 228 | message: format!( |
| 229 | "Architectural quality score {:.2} failed threshold {:.2}", |
| 230 | report.quality_score, self.min_quality |
| 231 | ), |
| 232 | location: None, |
| 233 | }); |
| 234 | } |
| 235 | if report.classification == "suspicious" || report.classification == "critical" { |
| 236 | findings.push(Finding { |
| 237 | severity: "Fatal".to_string(), |
| 238 | rule_id: "PROBE_CLASSIFICATION_REJECTED".to_string(), |
| 239 | message: format!( |
| 240 | "Diff classified as '{}', requiring quarantine", |
| 241 | report.classification |
| 242 | ), |
| 243 | location: None, |
| 244 | }); |
| 245 | } |
| 246 | |
| 247 | let status = if report.clean { |
| 248 | EvaluatorStatus::Passed |
| 249 | } else { |
| 250 | EvaluatorStatus::Failed |
| 251 | }; |
| 252 | |
| 253 | let measurements = vec![ |
| 254 | Measurement { |
| 255 | metric: "probe_telemetry_probability".to_string(), |
| 256 | value: report.telemetry_probability, |
| 257 | unit: "ratio".to_string(), |
| 258 | }, |
| 259 | Measurement { |
| 260 | metric: "probe_unslop_probability".to_string(), |
| 261 | value: report.unslop_probability, |
| 262 | unit: "ratio".to_string(), |
| 263 | }, |
| 264 | Measurement { |
| 265 | metric: "probe_quality_score".to_string(), |
| 266 | value: report.quality_score, |
| 267 | unit: "points".to_string(), |
| 268 | }, |
| 269 | ]; |
| 270 | |
| 271 | let mut outcome = EvaluatorOutcome { |
| 272 | evaluator: EvaluatorIdentity { |
| 273 | evaluator_id: "spark-inquisitor-probe".to_string(), |
| 274 | version: "0.2.0".to_string(), |
| 275 | binary_digest: Digest32([0xee; 32]), |
| 276 | config_digest: Digest32([0xff; 32]), |
| 277 | }, |
| 278 | status, |
| 279 | findings, |
| 280 | measurements, |
| 281 | evidence: vec![], |
| 282 | execution_digest: Digest32::ZERO, |
| 283 | }; |
| 284 | |
| 285 | outcome.execution_digest = outcome.compute_execution_digest(); |
| 286 | Ok(outcome) |
| 287 | } |
| 288 | } |