prodigy 0.4.4

Turn ad-hoc Claude sessions into reproducible development pipelines with parallel AI agents
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//! Performance regression detection system for Prodigy benchmarks
//! Tracks benchmark baselines and detects performance degradation

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;

/// Benchmark result with statistical information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchmarkResult {
    pub name: String,
    pub group: String,
    pub mean_ns: f64,
    pub median_ns: f64,
    pub std_dev_ns: f64,
    pub min_ns: f64,
    pub max_ns: f64,
    pub iterations: u64,
    pub timestamp: chrono::DateTime<chrono::Utc>,
}

/// Baseline storage for benchmark results
#[derive(Debug, Serialize, Deserialize)]
pub struct BenchmarkBaseline {
    pub version: String,
    pub commit: String,
    pub timestamp: chrono::DateTime<chrono::Utc>,
    pub results: HashMap<String, BenchmarkResult>,
}

/// Regression detection configuration
#[derive(Debug, Clone)]
pub struct RegressionConfig {
    /// Percentage threshold for detecting regression (e.g., 5.0 for 5%)
    pub threshold_percent: f64,
    /// Number of standard deviations for statistical significance
    pub std_dev_multiplier: f64,
    /// Minimum number of iterations for valid comparison
    pub min_iterations: u64,
    /// Path to store baseline files
    pub baseline_dir: PathBuf,
}

impl Default for RegressionConfig {
    fn default() -> Self {
        Self {
            threshold_percent: 5.0,
            std_dev_multiplier: 2.0,
            min_iterations: 30,
            baseline_dir: PathBuf::from(".benchmark-baselines"),
        }
    }
}

/// Performance regression detector
pub struct RegressionDetector {
    config: RegressionConfig,
    current_baseline: Option<BenchmarkBaseline>,
}

impl RegressionDetector {
    pub fn new(config: RegressionConfig) -> Self {
        Self {
            config,
            current_baseline: None,
        }
    }

    /// Load baseline from file
    pub fn load_baseline(&mut self, commit: &str) -> anyhow::Result<()> {
        let baseline_path = self.baseline_path(commit);
        if baseline_path.exists() {
            let content = fs::read_to_string(&baseline_path)?;
            self.current_baseline = Some(serde_json::from_str(&content)?);
        }
        Ok(())
    }

    /// Save baseline to file
    pub fn save_baseline(&self, baseline: &BenchmarkBaseline) -> anyhow::Result<()> {
        fs::create_dir_all(&self.config.baseline_dir)?;
        let baseline_path = self.baseline_path(&baseline.commit);
        let content = serde_json::to_string_pretty(baseline)?;
        fs::write(baseline_path, content)?;
        Ok(())
    }

    /// Compare current results against baseline
    pub fn detect_regressions(&self, current: &BenchmarkBaseline) -> Vec<RegressionReport> {
        let Some(baseline) = &self.current_baseline else {
            return vec![];
        };

        let mut reports = Vec::new();

        for (name, current_result) in &current.results {
            if let Some(baseline_result) = baseline.results.get(name) {
                if let Some(report) = self.analyze_regression(baseline_result, current_result) {
                    reports.push(report);
                }
            }
        }

        reports
    }

    /// Analyze individual benchmark for regression
    fn analyze_regression(
        &self,
        baseline: &BenchmarkResult,
        current: &BenchmarkResult,
    ) -> Option<RegressionReport> {
        // Check minimum iterations
        if current.iterations < self.config.min_iterations {
            return None;
        }

        let percent_change = ((current.mean_ns - baseline.mean_ns) / baseline.mean_ns) * 100.0;

        // Calculate statistical significance
        let combined_std_dev = (baseline.std_dev_ns.powi(2) + current.std_dev_ns.powi(2)).sqrt();
        let z_score = (current.mean_ns - baseline.mean_ns).abs() / combined_std_dev;
        let is_significant = z_score > self.config.std_dev_multiplier;

        // Detect regression
        if percent_change > self.config.threshold_percent && is_significant {
            return Some(RegressionReport {
                benchmark_name: current.name.clone(),
                baseline_mean_ns: baseline.mean_ns,
                current_mean_ns: current.mean_ns,
                percent_change,
                is_regression: true,
                is_significant,
                z_score,
            });
        }

        // Detect improvement (negative regression)
        if percent_change < -self.config.threshold_percent && is_significant {
            return Some(RegressionReport {
                benchmark_name: current.name.clone(),
                baseline_mean_ns: baseline.mean_ns,
                current_mean_ns: current.mean_ns,
                percent_change,
                is_regression: false,
                is_significant,
                z_score,
            });
        }

        None
    }

    /// Get path for baseline file
    fn baseline_path(&self, commit: &str) -> PathBuf {
        self.config
            .baseline_dir
            .join(format!("baseline-{}.json", &commit[..8.min(commit.len())]))
    }

    /// Generate trend analysis across multiple commits
    pub fn analyze_trends(&mut self, commits: &[String]) -> TrendAnalysis {
        let mut trends = HashMap::new();

        for commit in commits {
            if let Ok(()) = self.load_baseline(commit) {
                if let Some(baseline) = &self.current_baseline {
                    for (name, result) in &baseline.results {
                        trends
                            .entry(name.clone())
                            .or_insert_with(Vec::new)
                            .push(TrendPoint {
                                commit: commit.clone(),
                                timestamp: result.timestamp,
                                mean_ns: result.mean_ns,
                            });
                    }
                }
            }
        }

        TrendAnalysis { trends }
    }
}

/// Regression detection report
#[derive(Debug, Clone, Serialize)]
pub struct RegressionReport {
    pub benchmark_name: String,
    pub baseline_mean_ns: f64,
    pub current_mean_ns: f64,
    pub percent_change: f64,
    pub is_regression: bool,
    pub is_significant: bool,
    pub z_score: f64,
}

impl RegressionReport {
    /// Format report for display
    pub fn format(&self) -> String {
        let change_type = if self.is_regression {
            "REGRESSION"
        } else {
            "IMPROVEMENT"
        };

        format!(
            "{}: {} - {:.2}% {} (baseline: {:.2}ns, current: {:.2}ns, z-score: {:.2})",
            change_type,
            self.benchmark_name,
            self.percent_change.abs(),
            if self.is_regression {
                "slower"
            } else {
                "faster"
            },
            self.baseline_mean_ns,
            self.current_mean_ns,
            self.z_score
        )
    }
}

/// Trend analysis across multiple commits
#[derive(Debug, Serialize)]
pub struct TrendAnalysis {
    pub trends: HashMap<String, Vec<TrendPoint>>,
}

#[derive(Debug, Clone, Serialize)]
pub struct TrendPoint {
    pub commit: String,
    pub timestamp: chrono::DateTime<chrono::Utc>,
    pub mean_ns: f64,
}

/// CI integration for automated regression detection
pub struct CIIntegration {
    detector: RegressionDetector,
}

impl Default for CIIntegration {
    fn default() -> Self {
        Self::new()
    }
}

impl CIIntegration {
    pub fn new() -> Self {
        Self {
            detector: RegressionDetector::new(RegressionConfig::default()),
        }
    }

    /// Run regression detection in CI pipeline
    pub fn run_ci_check(&mut self, current_results: BenchmarkBaseline) -> anyhow::Result<bool> {
        // Get base commit (e.g., from environment variable or git)
        let base_commit = std::env::var("BASE_COMMIT").unwrap_or_else(|_| "main".to_string());

        // Load baseline for comparison
        self.detector.load_baseline(&base_commit)?;

        // Detect regressions
        let regressions = self.detector.detect_regressions(&current_results);

        // Report results
        if !regressions.is_empty() {
            println!("\n⚠️  Performance Regressions Detected:\n");
            for report in &regressions {
                if report.is_regression {
                    println!("{}", report.format());
                } else {
                    println!("{}", report.format());
                }
            }

            // Fail CI if there are actual regressions
            let has_regressions = regressions.iter().any(|r| r.is_regression);
            if has_regressions {
                println!("\n❌ CI Failed: Performance regressions detected");
                return Ok(false);
            }
        } else {
            println!("\n✅ No performance regressions detected");
        }

        // Save current results as potential new baseline
        if std::env::var("UPDATE_BASELINE").is_ok() {
            self.detector.save_baseline(&current_results)?;
            println!("📊 Baseline updated for commit: {}", current_results.commit);
        }

        Ok(true)
    }

    /// Generate performance report
    pub fn generate_report(&mut self, commits: Vec<String>) -> String {
        let trends = self.detector.analyze_trends(&commits);

        let mut report = String::from("# Performance Trend Report\n\n");

        for (benchmark, points) in &trends.trends {
            report.push_str(&format!("## {}\n\n", benchmark));
            report.push_str("| Commit | Timestamp | Mean (ns) | Change |\n");
            report.push_str("|--------|-----------|-----------|--------|\n");

            let mut prev_mean = None;
            for point in points {
                let change = if let Some(prev) = prev_mean {
                    let pct = ((point.mean_ns - prev) / prev) * 100.0;
                    format!("{:+.2}%", pct)
                } else {
                    "baseline".to_string()
                };

                report.push_str(&format!(
                    "| {} | {} | {:.2} | {} |\n",
                    &point.commit[..8.min(point.commit.len())],
                    point.timestamp.format("%Y-%m-%d"),
                    point.mean_ns,
                    change
                ));

                prev_mean = Some(point.mean_ns);
            }
            report.push('\n');
        }

        report
    }
}

#[cfg(test)]
mod tests {
    #[allow(unused_imports)]
    use super::*;

    #[test]
    fn test_regression_detection() {
        let config = RegressionConfig {
            threshold_percent: 5.0,
            std_dev_multiplier: 2.0,
            min_iterations: 30,
            baseline_dir: PathBuf::from("/tmp/bench-test"),
        };

        let detector = RegressionDetector::new(config);

        let baseline = BenchmarkResult {
            name: "test_bench".to_string(),
            group: "test".to_string(),
            mean_ns: 1000.0,
            median_ns: 990.0,
            std_dev_ns: 50.0,
            min_ns: 900.0,
            max_ns: 1100.0,
            iterations: 100,
            timestamp: chrono::Utc::now(),
        };

        let current = BenchmarkResult {
            name: "test_bench".to_string(),
            group: "test".to_string(),
            mean_ns: 1100.0, // 10% slower
            median_ns: 1090.0,
            std_dev_ns: 55.0,
            min_ns: 1000.0,
            max_ns: 1200.0,
            iterations: 100,
            timestamp: chrono::Utc::now(),
        };

        let report = detector.analyze_regression(&baseline, &current);
        assert!(report.is_some());

        let report = report.unwrap();
        assert!(report.is_regression);
        assert!(report.percent_change > 5.0);
    }

    #[test]
    fn test_improvement_detection() {
        let config = RegressionConfig::default();
        let detector = RegressionDetector::new(config);

        let baseline = BenchmarkResult {
            name: "test_bench".to_string(),
            group: "test".to_string(),
            mean_ns: 1000.0,
            median_ns: 990.0,
            std_dev_ns: 50.0,
            min_ns: 900.0,
            max_ns: 1100.0,
            iterations: 100,
            timestamp: chrono::Utc::now(),
        };

        let current = BenchmarkResult {
            name: "test_bench".to_string(),
            group: "test".to_string(),
            mean_ns: 900.0, // 10% faster
            median_ns: 890.0,
            std_dev_ns: 45.0,
            min_ns: 800.0,
            max_ns: 1000.0,
            iterations: 100,
            timestamp: chrono::Utc::now(),
        };

        let report = detector.analyze_regression(&baseline, &current);
        assert!(report.is_some());

        let report = report.unwrap();
        assert!(!report.is_regression);
        assert!(report.percent_change < -5.0);
    }
}

// Criterion benchmark setup
use criterion::{criterion_group, criterion_main, Criterion};

fn bench_regression_detection(c: &mut Criterion) {
    c.bench_function("regression_detection", |b| {
        let config = RegressionConfig::default();
        let detector = RegressionDetector::new(config);

        let baseline = BenchmarkResult {
            name: "test_benchmark".to_string(),
            group: "test_group".to_string(),
            mean_ns: 1000.0,
            median_ns: 950.0,
            std_dev_ns: 50.0,
            min_ns: 900.0,
            max_ns: 1100.0,
            iterations: 100,
            timestamp: chrono::Utc::now(),
        };

        let current = BenchmarkResult {
            name: "test_benchmark".to_string(),
            group: "test_group".to_string(),
            mean_ns: 1100.0,
            median_ns: 1050.0,
            std_dev_ns: 55.0,
            min_ns: 990.0,
            max_ns: 1210.0,
            iterations: 100,
            timestamp: chrono::Utc::now(),
        };

        b.iter(|| detector.analyze_regression(&baseline, &current));
    });
}

criterion_group!(benches, bench_regression_detection);
criterion_main!(benches);