|
| 1 | +//! AI/ML component analysis passes (Issue #91). |
| 2 | +//! |
| 3 | +//! Provides architecture-level safety analysis for AI/ML components using |
| 4 | +//! the `AI_ML` property set. Eight checks aligned with ISO/PAS 8800: |
| 5 | +//! |
| 6 | +//! | Pass | Severity | What it checks | |
| 7 | +//! |-------------------------|----------|-------------------------------------------------------| |
| 8 | +//! | `ai_inference_deadline` | Error | Inference latency fits within AADL deadline | |
| 9 | +//! | `ai_fallback_coverage` | Warning | Every AI thread has Fallback_Strategy | |
| 10 | +//! | `ai_fallback_timing` | Error | Fallback latency fits within remaining deadline budget | |
| 11 | +//! | `ai_ood_coverage` | Warning | Confidence_Threshold requires OOD_Detection_Enabled | |
| 12 | +//! | `ai_model_deployment` | Error | AI process bound to processor with sufficient compute | |
| 13 | +//! | `ai_redundancy` | Warning | Redundant_Model fallback has second model on diff proc | |
| 14 | +//! |
| 15 | +//! Checks 7–8 from Issue #91 (drift monitoring, training provenance) were |
| 16 | +//! evaluated and dropped: they are documentation linting, not architecture |
| 17 | +//! analysis. If needed, extend `completeness.rs` or add a separate lint pass. |
| 18 | +
|
| 19 | +use spar_hir_def::instance::SystemInstance; |
| 20 | +use spar_hir_def::item_tree::ComponentCategory; |
| 21 | + |
| 22 | +use crate::property_accessors::{ |
| 23 | + get_ai_ml_bool, get_ai_ml_string, get_confidence_threshold, get_fallback_latency, |
| 24 | + get_inference_latency_range, get_processor_binding, get_timing_property, is_ai_ml_component, |
| 25 | +}; |
| 26 | +use crate::{Analysis, AnalysisDiagnostic, Severity, component_path}; |
| 27 | + |
| 28 | +/// AI/ML safety analysis — all eight checks in one pass. |
| 29 | +pub struct AiMlAnalysis; |
| 30 | + |
| 31 | +impl Analysis for AiMlAnalysis { |
| 32 | + fn name(&self) -> &str { |
| 33 | + "ai_ml" |
| 34 | + } |
| 35 | + |
| 36 | + fn analyze(&self, instance: &SystemInstance) -> Vec<AnalysisDiagnostic> { |
| 37 | + let mut diags = Vec::new(); |
| 38 | + |
| 39 | + for (comp_idx, comp) in instance.all_components() { |
| 40 | + let props = instance.properties_for(comp_idx); |
| 41 | + |
| 42 | + if !is_ai_ml_component(props) { |
| 43 | + continue; |
| 44 | + } |
| 45 | + |
| 46 | + let path = component_path(instance, comp_idx); |
| 47 | + |
| 48 | + // Thread-level checks (inference, fallback, OOD) |
| 49 | + if comp.category == ComponentCategory::Thread { |
| 50 | + check_inference_deadline(props, &path, &mut diags); |
| 51 | + check_fallback_coverage(props, &path, &comp.name, &mut diags); |
| 52 | + check_fallback_timing(props, &path, &mut diags); |
| 53 | + check_ood_coverage(props, &path, &comp.name, &mut diags); |
| 54 | + } |
| 55 | + |
| 56 | + // Process-level checks (model deployment) |
| 57 | + if comp.category == ComponentCategory::Process { |
| 58 | + check_model_deployment(props, &path, &comp.name, &mut diags); |
| 59 | + } |
| 60 | + |
| 61 | + // Redundancy check: thread or process with Redundant_Model fallback |
| 62 | + check_redundancy(instance, comp_idx, props, &path, &comp.name, &mut diags); |
| 63 | + } |
| 64 | + |
| 65 | + diags |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// ── Check 1: Inference deadline ──────────────────────────────────── |
| 70 | + |
| 71 | +fn check_inference_deadline( |
| 72 | + props: &spar_hir_def::properties::PropertyMap, |
| 73 | + path: &[String], |
| 74 | + diags: &mut Vec<AnalysisDiagnostic>, |
| 75 | +) { |
| 76 | + let Some((_, worst_case_ps)) = get_inference_latency_range(props) else { |
| 77 | + return; |
| 78 | + }; |
| 79 | + let Some(deadline_ps) = get_timing_property(props, "Deadline") else { |
| 80 | + // AI thread with inference latency but no deadline — flag it |
| 81 | + diags.push(AnalysisDiagnostic { |
| 82 | + severity: Severity::Warning, |
| 83 | + message: "AI/ML thread has Inference_Latency but no Deadline property; \ |
| 84 | + cannot verify timing safety" |
| 85 | + .to_string(), |
| 86 | + path: path.to_vec(), |
| 87 | + analysis: "ai_ml".to_string(), |
| 88 | + }); |
| 89 | + return; |
| 90 | + }; |
| 91 | + |
| 92 | + if worst_case_ps > deadline_ps { |
| 93 | + let worst_ms = worst_case_ps as f64 / 1_000_000_000.0; |
| 94 | + let deadline_ms = deadline_ps as f64 / 1_000_000_000.0; |
| 95 | + diags.push(AnalysisDiagnostic { |
| 96 | + severity: Severity::Error, |
| 97 | + message: format!( |
| 98 | + "AI/ML inference worst-case latency ({worst_ms:.1} ms) exceeds \ |
| 99 | + thread deadline ({deadline_ms:.1} ms)" |
| 100 | + ), |
| 101 | + path: path.to_vec(), |
| 102 | + analysis: "ai_ml".to_string(), |
| 103 | + }); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +// ── Check 2: Fallback coverage ───────────────────────────────────── |
| 108 | + |
| 109 | +fn check_fallback_coverage( |
| 110 | + props: &spar_hir_def::properties::PropertyMap, |
| 111 | + path: &[String], |
| 112 | + name: &Name, |
| 113 | + diags: &mut Vec<AnalysisDiagnostic>, |
| 114 | +) { |
| 115 | + if get_ai_ml_string(props, "Fallback_Strategy").is_none() { |
| 116 | + diags.push(AnalysisDiagnostic { |
| 117 | + severity: Severity::Warning, |
| 118 | + message: format!( |
| 119 | + "AI/ML thread '{}' has no Fallback_Strategy defined; \ |
| 120 | + ISO/PAS 8800 recommends fallback measures for AI elements", |
| 121 | + name |
| 122 | + ), |
| 123 | + path: path.to_vec(), |
| 124 | + analysis: "ai_ml".to_string(), |
| 125 | + }); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +// ── Check 3: Fallback timing ─────────────────────────────────────── |
| 130 | + |
| 131 | +fn check_fallback_timing( |
| 132 | + props: &spar_hir_def::properties::PropertyMap, |
| 133 | + path: &[String], |
| 134 | + diags: &mut Vec<AnalysisDiagnostic>, |
| 135 | +) { |
| 136 | + let Some(fallback_ps) = get_fallback_latency(props) else { |
| 137 | + return; |
| 138 | + }; |
| 139 | + let Some(deadline_ps) = get_timing_property(props, "Deadline") else { |
| 140 | + return; |
| 141 | + }; |
| 142 | + let Some((_, worst_inference_ps)) = get_inference_latency_range(props) else { |
| 143 | + return; |
| 144 | + }; |
| 145 | + |
| 146 | + // In worst case: inference runs to deadline, then fallback must complete. |
| 147 | + // Total = worst_inference + fallback must fit in deadline. |
| 148 | + // More precisely: if inference fails at worst case, the fallback must |
| 149 | + // complete within the remaining budget. |
| 150 | + let total_ps = worst_inference_ps.saturating_add(fallback_ps); |
| 151 | + if total_ps > deadline_ps { |
| 152 | + let total_ms = total_ps as f64 / 1_000_000_000.0; |
| 153 | + let deadline_ms = deadline_ps as f64 / 1_000_000_000.0; |
| 154 | + let fallback_ms = fallback_ps as f64 / 1_000_000_000.0; |
| 155 | + diags.push(AnalysisDiagnostic { |
| 156 | + severity: Severity::Error, |
| 157 | + message: format!( |
| 158 | + "AI/ML worst-case inference + fallback ({total_ms:.1} ms) exceeds \ |
| 159 | + deadline ({deadline_ms:.1} ms); fallback latency is {fallback_ms:.1} ms" |
| 160 | + ), |
| 161 | + path: path.to_vec(), |
| 162 | + analysis: "ai_ml".to_string(), |
| 163 | + }); |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +// ── Check 4: OOD detection coverage ──────────────────────────────── |
| 168 | + |
| 169 | +fn check_ood_coverage( |
| 170 | + props: &spar_hir_def::properties::PropertyMap, |
| 171 | + path: &[String], |
| 172 | + name: &Name, |
| 173 | + diags: &mut Vec<AnalysisDiagnostic>, |
| 174 | +) { |
| 175 | + // If a confidence threshold is set, OOD detection should be enabled |
| 176 | + if get_confidence_threshold(props).is_some() { |
| 177 | + let ood_enabled = get_ai_ml_bool(props, "OOD_Detection_Enabled").unwrap_or(false); |
| 178 | + if !ood_enabled { |
| 179 | + diags.push(AnalysisDiagnostic { |
| 180 | + severity: Severity::Warning, |
| 181 | + message: format!( |
| 182 | + "AI/ML thread '{}' has Confidence_Threshold but OOD_Detection_Enabled \ |
| 183 | + is not set; out-of-distribution inputs may produce silently wrong results", |
| 184 | + name |
| 185 | + ), |
| 186 | + path: path.to_vec(), |
| 187 | + analysis: "ai_ml".to_string(), |
| 188 | + }); |
| 189 | + } |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +// ── Check 5: Model deployment ────────────────────────────────────── |
| 194 | + |
| 195 | +fn check_model_deployment( |
| 196 | + props: &spar_hir_def::properties::PropertyMap, |
| 197 | + path: &[String], |
| 198 | + name: &Name, |
| 199 | + diags: &mut Vec<AnalysisDiagnostic>, |
| 200 | +) { |
| 201 | + // Every AI process should be bound to a processor |
| 202 | + if get_ai_ml_string(props, "Model_Format").is_some() && get_processor_binding(props).is_none() { |
| 203 | + diags.push(AnalysisDiagnostic { |
| 204 | + severity: Severity::Error, |
| 205 | + message: format!( |
| 206 | + "AI/ML process '{}' has Model_Format but no Actual_Processor_Binding; \ |
| 207 | + cannot verify compute capacity for inference", |
| 208 | + name |
| 209 | + ), |
| 210 | + path: path.to_vec(), |
| 211 | + analysis: "ai_ml".to_string(), |
| 212 | + }); |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +// ── Check 6: Redundancy ──────────────────────────────────────────── |
| 217 | + |
| 218 | +fn check_redundancy( |
| 219 | + instance: &SystemInstance, |
| 220 | + comp_idx: ComponentInstanceIdx, |
| 221 | + props: &spar_hir_def::properties::PropertyMap, |
| 222 | + path: &[String], |
| 223 | + name: &Name, |
| 224 | + diags: &mut Vec<AnalysisDiagnostic>, |
| 225 | +) { |
| 226 | + let Some(strategy) = get_ai_ml_string(props, "Fallback_Strategy") else { |
| 227 | + return; |
| 228 | + }; |
| 229 | + if !strategy.eq_ignore_ascii_case("Redundant_Model") { |
| 230 | + return; |
| 231 | + } |
| 232 | + |
| 233 | + // Check that a sibling AI component exists on a different processor |
| 234 | + let my_binding = get_processor_binding(props); |
| 235 | + let parent = instance.component(comp_idx).parent; |
| 236 | + let Some(parent_idx) = parent else { return }; |
| 237 | + |
| 238 | + let siblings = &instance.component(parent_idx).children; |
| 239 | + let has_redundant_peer = siblings.iter().any(|&sib_idx| { |
| 240 | + if sib_idx == comp_idx { |
| 241 | + return false; |
| 242 | + } |
| 243 | + let sib_props = instance.properties_for(sib_idx); |
| 244 | + if !is_ai_ml_component(sib_props) { |
| 245 | + return false; |
| 246 | + } |
| 247 | + let sib_binding = get_processor_binding(sib_props); |
| 248 | + // Must be on a different processor |
| 249 | + match (&my_binding, &sib_binding) { |
| 250 | + (Some(mine), Some(theirs)) => !mine.eq_ignore_ascii_case(theirs), |
| 251 | + _ => false, |
| 252 | + } |
| 253 | + }); |
| 254 | + |
| 255 | + if !has_redundant_peer { |
| 256 | + diags.push(AnalysisDiagnostic { |
| 257 | + severity: Severity::Warning, |
| 258 | + message: format!( |
| 259 | + "AI/ML component '{}' uses Redundant_Model fallback but no sibling \ |
| 260 | + AI component found on a different processor", |
| 261 | + name |
| 262 | + ), |
| 263 | + path: path.to_vec(), |
| 264 | + analysis: "ai_ml".to_string(), |
| 265 | + }); |
| 266 | + } |
| 267 | +} |
| 268 | + |
| 269 | +// ── Private helpers ──────────────────────────────────────────────── |
| 270 | + |
| 271 | +use spar_hir_def::instance::ComponentInstanceIdx; |
| 272 | +use spar_hir_def::name::Name; |
| 273 | + |
| 274 | +#[cfg(test)] |
| 275 | +mod tests; |
0 commit comments