|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use crate::core::eval_benchmarks::AggregateMetrics as BenchmarkAggregateMetrics; |
| 4 | + |
| 5 | +use super::super::{ |
| 6 | + EvalFixtureResult, EvalNamedMetricComparison, EvalReport, EvalSuiteResult, |
| 7 | + EvalVerificationHealth, |
| 8 | +}; |
| 9 | + |
| 10 | +pub(in super::super) fn build_suite_comparisons( |
| 11 | + current: &[EvalSuiteResult], |
| 12 | + baseline: Option<&EvalReport>, |
| 13 | +) -> Vec<EvalNamedMetricComparison> { |
| 14 | + let Some(baseline) = baseline else { |
| 15 | + return Vec::new(); |
| 16 | + }; |
| 17 | + |
| 18 | + let baseline_by_suite = baseline |
| 19 | + .suite_results |
| 20 | + .iter() |
| 21 | + .map(|suite| (suite.suite.as_str(), &suite.aggregate)) |
| 22 | + .collect::<HashMap<_, _>>(); |
| 23 | + |
| 24 | + let mut comparisons = current |
| 25 | + .iter() |
| 26 | + .filter_map(|suite| { |
| 27 | + let baseline_metrics = baseline_by_suite.get(suite.suite.as_str())?; |
| 28 | + Some(build_comparison( |
| 29 | + suite.suite.clone(), |
| 30 | + &suite.aggregate, |
| 31 | + baseline_metrics, |
| 32 | + )) |
| 33 | + }) |
| 34 | + .collect::<Vec<_>>(); |
| 35 | + comparisons.sort_by(|left, right| left.name.cmp(&right.name)); |
| 36 | + comparisons |
| 37 | +} |
| 38 | + |
| 39 | +pub(in super::super) fn build_named_breakdown_comparisons( |
| 40 | + current: &HashMap<String, BenchmarkAggregateMetrics>, |
| 41 | + baseline: Option<&HashMap<String, BenchmarkAggregateMetrics>>, |
| 42 | +) -> Vec<EvalNamedMetricComparison> { |
| 43 | + let Some(baseline) = baseline else { |
| 44 | + return Vec::new(); |
| 45 | + }; |
| 46 | + |
| 47 | + let mut comparisons = current |
| 48 | + .iter() |
| 49 | + .filter_map(|(name, current_metrics)| { |
| 50 | + baseline.get(name).map(|baseline_metrics| { |
| 51 | + build_comparison(name.clone(), current_metrics, baseline_metrics) |
| 52 | + }) |
| 53 | + }) |
| 54 | + .collect::<Vec<_>>(); |
| 55 | + comparisons.sort_by(|left, right| left.name.cmp(&right.name)); |
| 56 | + comparisons |
| 57 | +} |
| 58 | + |
| 59 | +pub(in super::super) fn build_verification_health( |
| 60 | + results: &[EvalFixtureResult], |
| 61 | +) -> Option<EvalVerificationHealth> { |
| 62 | + let warnings_total = results |
| 63 | + .iter() |
| 64 | + .map(|result| result.warnings.len()) |
| 65 | + .sum::<usize>(); |
| 66 | + if warnings_total == 0 { |
| 67 | + return None; |
| 68 | + } |
| 69 | + |
| 70 | + let mut health = EvalVerificationHealth { |
| 71 | + warnings_total, |
| 72 | + fixtures_with_warnings: results |
| 73 | + .iter() |
| 74 | + .filter(|result| !result.warnings.is_empty()) |
| 75 | + .count(), |
| 76 | + ..Default::default() |
| 77 | + }; |
| 78 | + |
| 79 | + for warning in results.iter().flat_map(|result| &result.warnings) { |
| 80 | + let lower = warning.to_ascii_lowercase(); |
| 81 | + if lower.contains("verification fail-open kept") { |
| 82 | + health.fail_open_warning_count += 1; |
| 83 | + } |
| 84 | + if lower.contains("unparseable verifier output") { |
| 85 | + health.parse_failure_count += 1; |
| 86 | + } |
| 87 | + if lower.contains("verifier request error") { |
| 88 | + health.request_failure_count += 1; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + Some(health) |
| 93 | +} |
| 94 | + |
| 95 | +fn build_comparison( |
| 96 | + name: String, |
| 97 | + current: &BenchmarkAggregateMetrics, |
| 98 | + baseline: &BenchmarkAggregateMetrics, |
| 99 | +) -> EvalNamedMetricComparison { |
| 100 | + EvalNamedMetricComparison { |
| 101 | + name, |
| 102 | + current_micro_f1: current.micro_f1, |
| 103 | + baseline_micro_f1: baseline.micro_f1, |
| 104 | + micro_f1_delta: current.micro_f1 - baseline.micro_f1, |
| 105 | + current_weighted_score: current.weighted_score, |
| 106 | + baseline_weighted_score: baseline.weighted_score, |
| 107 | + weighted_score_delta: current.weighted_score - baseline.weighted_score, |
| 108 | + current_fixture_count: current.fixture_count, |
| 109 | + baseline_fixture_count: baseline.fixture_count, |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +#[cfg(test)] |
| 114 | +mod tests { |
| 115 | + use crate::core::eval_benchmarks::AggregateMetrics; |
| 116 | + |
| 117 | + use super::*; |
| 118 | + |
| 119 | + fn metrics(micro_f1: f32, weighted_score: f32, fixture_count: usize) -> AggregateMetrics { |
| 120 | + AggregateMetrics { |
| 121 | + micro_f1, |
| 122 | + weighted_score, |
| 123 | + fixture_count, |
| 124 | + ..Default::default() |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + #[test] |
| 129 | + fn build_named_breakdown_comparisons_intersects_current_and_baseline() { |
| 130 | + let current = HashMap::from([ |
| 131 | + ("bug".to_string(), metrics(0.7, 0.72, 2)), |
| 132 | + ("security".to_string(), metrics(0.9, 0.93, 3)), |
| 133 | + ]); |
| 134 | + let baseline = HashMap::from([ |
| 135 | + ("security".to_string(), metrics(0.95, 0.96, 3)), |
| 136 | + ("style".to_string(), metrics(0.8, 0.81, 1)), |
| 137 | + ]); |
| 138 | + |
| 139 | + let comparisons = build_named_breakdown_comparisons(¤t, Some(&baseline)); |
| 140 | + |
| 141 | + assert_eq!(comparisons.len(), 1); |
| 142 | + assert_eq!(comparisons[0].name, "security"); |
| 143 | + assert!((comparisons[0].micro_f1_delta + 0.05).abs() < f32::EPSILON); |
| 144 | + } |
| 145 | + |
| 146 | + #[test] |
| 147 | + fn build_verification_health_counts_fail_open_signals() { |
| 148 | + let results = vec![ |
| 149 | + EvalFixtureResult { |
| 150 | + fixture: "suite/a".to_string(), |
| 151 | + suite: Some("suite".to_string()), |
| 152 | + passed: true, |
| 153 | + total_comments: 1, |
| 154 | + required_matches: 1, |
| 155 | + required_total: 1, |
| 156 | + benchmark_metrics: None, |
| 157 | + suite_thresholds: None, |
| 158 | + difficulty: None, |
| 159 | + metadata: None, |
| 160 | + rule_metrics: vec![], |
| 161 | + rule_summary: None, |
| 162 | + warnings: vec![ |
| 163 | + "verification fail-open kept 1 comment(s) after verifier request error: boom" |
| 164 | + .to_string(), |
| 165 | + "verification fail-open kept 1 comment(s) after unparseable verifier output" |
| 166 | + .to_string(), |
| 167 | + ], |
| 168 | + failures: vec![], |
| 169 | + }, |
| 170 | + EvalFixtureResult { |
| 171 | + fixture: "suite/b".to_string(), |
| 172 | + suite: Some("suite".to_string()), |
| 173 | + passed: true, |
| 174 | + total_comments: 1, |
| 175 | + required_matches: 1, |
| 176 | + required_total: 1, |
| 177 | + benchmark_metrics: None, |
| 178 | + suite_thresholds: None, |
| 179 | + difficulty: None, |
| 180 | + metadata: None, |
| 181 | + rule_metrics: vec![], |
| 182 | + rule_summary: None, |
| 183 | + warnings: vec![], |
| 184 | + failures: vec![], |
| 185 | + }, |
| 186 | + ]; |
| 187 | + |
| 188 | + let health = build_verification_health(&results).unwrap(); |
| 189 | + assert_eq!(health.warnings_total, 2); |
| 190 | + assert_eq!(health.fixtures_with_warnings, 1); |
| 191 | + assert_eq!(health.fail_open_warning_count, 2); |
| 192 | + assert_eq!(health.parse_failure_count, 1); |
| 193 | + assert_eq!(health.request_failure_count, 1); |
| 194 | + } |
| 195 | +} |
0 commit comments