diff --git a/compliance-agent/src/pipeline/orchestrator.rs b/compliance-agent/src/pipeline/orchestrator.rs index d064d55..84c77b8 100644 --- a/compliance-agent/src/pipeline/orchestrator.rs +++ b/compliance-agent/src/pipeline/orchestrator.rs @@ -844,28 +844,19 @@ impl PipelineOrchestrator { return Ok(0); } - let mut all_findings = Vec::new(); + // Each source's findings next to the tree they were found in: the + // control mapping below reads the flagged regions from it. + let mut analysed = Vec::with_capacity(sources.len()); let mut all_sbom: Vec = Vec::new(); let mut sbom_seen = std::collections::BTreeSet::new(); for a in &sources { let Some(path) = ingest_set.get(&a.id).and_then(|ia| ia.working_path.clone()) else { continue; }; - let mut source_findings = crate::pipeline::plc::analyze_tree(&path, target_id); - // Control mapping for the PLC path (run_plc_scan is separate from - // run_pipeline, which does its own mapping). PLC findings carry - // file_path/line/cwe, so the semantic pass reads each region under this - // source's `path` and stamps master-control refs. The LUT + grounded - // surface passes are code-pattern / CRA-specific and don't apply to - // IEC 61131-3 control logic, so only the semantic pass runs here. - crate::controls::semantic_stamp_findings( - &self.config, - self.llm.clone(), - &path, - &mut source_findings, - ) - .await; - all_findings.extend(source_findings); + analysed.push(( + path.clone(), + crate::pipeline::plc::analyze_tree(&path, target_id), + )); // Control-application SBOM: CODESYS libraries + runtime from a // `.projectarchive` (uploaded, or committed in the working tree). let archive = a @@ -902,6 +893,7 @@ impl PipelineOrchestrator { } } } + let all_findings = self.map_plc_findings(scan_run_id, analysed).await; tracing::info!( target_id, artifacts = sources.len(), @@ -1602,6 +1594,40 @@ impl PipelineOrchestrator { .await; } + /// Control mapping for the PLC path (`run_plc_scan` is separate from + /// `run_pipeline`, which does its own). PLC findings carry + /// file_path/line/cwe, so the semantic pass reads each region under its + /// source's tree and stamps master-control refs. The LUT and grounded + /// passes are code-pattern / CRA-specific and don't apply to IEC 61131-3 + /// control logic, so only the semantic pass runs. It asks the LLM about + /// each finding and can take minutes, so it is a phase of its own. + async fn map_plc_findings( + &self, + scan_run_id: &str, + analysed: Vec<(std::path::PathBuf, Vec)>, + ) -> Vec { + let mut all_findings = Vec::new(); + let map = self.config.breakpilot.semantic_mapping + && analysed.iter().any(|(_, findings)| !findings.is_empty()); + if map { + self.update_phase(scan_run_id, ScanPhase::SemanticControlMapping) + .await; + } + for (path, mut findings) in analysed { + if map { + crate::controls::semantic_stamp_findings( + &self.config, + self.llm.clone(), + &path, + &mut findings, + ) + .await; + } + all_findings.extend(findings); + } + all_findings + } + pub(super) async fn update_phase(&self, scan_run_id: &str, phase: ScanPhase) { let Ok(phase) = mongodb::bson::to_bson(&phase) else { return; @@ -1688,6 +1714,9 @@ fn planned_phases( if plc || ics { if plc { phases.push(ScanPhase::PlcAnalysis); + if config.breakpilot.semantic_mapping { + phases.push(ScanPhase::SemanticControlMapping); + } if config.plc_runtime.enabled && target.live_url().is_none() { phases.push(ScanPhase::PlcProvision); } @@ -1792,10 +1821,24 @@ mod tests { )); let mut config = AgentConfig::default(); config.plc_runtime.enabled = false; + config.breakpilot.semantic_mapping = false; + let plan = build_scan_plan(&target); - let phases = planned_phases(&target, &build_scan_plan(&target), &config); + assert_eq!( + planned_phases(&target, &plan, &config), + [ScanPhase::Classification, ScanPhase::PlcAnalysis] + ); - assert_eq!(phases, [ScanPhase::Classification, ScanPhase::PlcAnalysis]); + // The LLM control mapping is a phase of its own, so a long one shows. + config.breakpilot.semantic_mapping = true; + assert_eq!( + planned_phases(&target, &plan, &config), + [ + ScanPhase::Classification, + ScanPhase::PlcAnalysis, + ScanPhase::SemanticControlMapping + ] + ); } #[test]