reasonkit-core 0.1.8

The Reasoning Engine — Auditable Reasoning for Production AI | Rust-Native | Turn Prompts into Protocols
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
//! # VIBE Benchmarking System
//!
//! Comprehensive benchmarking suite for VIBE protocol validation performance
//! and quality assessment across different platforms and scenarios.

use super::*;
use crate::vibe::scoring::TrendDirection;
use crate::vibe::validation::{Severity, VIBEError};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::time::Instant;
use tokio::sync::RwLock;

/// Benchmark suite for VIBE validation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchmarkSuite {
    /// Suite identifier
    pub suite_id: Uuid,

    /// Suite name and description
    pub name: String,
    pub description: String,

    /// Benchmark scenarios
    pub scenarios: Vec<BenchmarkScenario>,

    /// Suite configuration
    pub config: BenchmarkConfig,

    /// Historical results
    pub results: Vec<BenchmarkResult>,
}

/// Individual benchmark scenario
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchmarkScenario {
    /// Scenario identifier
    pub scenario_id: Uuid,

    /// Scenario metadata
    pub name: String,
    pub description: String,
    pub category: BenchmarkCategory,

    /// Protocol to benchmark
    pub protocol: BenchmarkProtocol,

    /// Target platforms for validation
    pub target_platforms: Vec<Platform>,

    /// Performance thresholds
    pub performance_thresholds: PerformanceThresholds,

    /// Expected outcomes
    pub expected_outcomes: ExpectedOutcomes,
}

/// Protocol specifically designed for benchmarking
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchmarkProtocol {
    /// Protocol content
    pub content: String,

    /// Protocol type
    pub protocol_type: ProtocolType,

    /// Complexity level
    pub complexity: ProtocolComplexity,

    /// Known characteristics
    pub characteristics: ProtocolCharacteristics,
}

/// Complexity levels for benchmark protocols
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ProtocolComplexity {
    Simple,
    Moderate,
    Complex,
    VeryComplex,
}

/// Protocol characteristics for benchmarking
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProtocolCharacteristics {
    pub has_multiple_platforms: bool,
    pub has_security_requirements: bool,
    pub has_performance_requirements: bool,
    pub has_accessibility_requirements: bool,
    pub has_integration_requirements: bool,
    pub estimated_validation_time_ms: u64,
}

/// Benchmark categories
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BenchmarkCategory {
    Performance,
    Accuracy,
    CrossPlatform,
    Regression,
    Stress,
}

/// Performance thresholds for validation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceThresholds {
    pub max_validation_time_ms: u64,
    pub max_memory_usage_mb: u64,
    pub min_score_threshold: f32,
    pub max_error_rate_percent: f32,
}

/// Expected outcomes for validation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExpectedOutcomes {
    pub expected_score_range: (f32, f32),
    pub expected_issues_count: (usize, usize),
    pub expected_platform_scores: HashMap<Platform, f32>,
    pub required_validations: Vec<Platform>,
}

/// Benchmark configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchmarkConfig {
    /// Number of iterations for each scenario
    pub iterations: usize,

    /// Parallel execution settings
    pub parallel_execution: bool,
    pub max_concurrent_validations: usize,

    /// Warm-up iterations (discarded from results)
    pub warmup_iterations: usize,

    /// Statistical confidence level
    pub confidence_level: f32,

    /// Enable detailed profiling
    pub enable_profiling: bool,

    /// Custom scoring criteria
    pub scoring_criteria: Option<ValidationCriteria>,
}

/// Benchmark execution result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchmarkResult {
    /// Result identifier
    pub result_id: Uuid,

    /// Execution timestamp
    pub timestamp: chrono::DateTime<chrono::Utc>,

    /// Scenario that was executed
    pub scenario_id: Uuid,

    /// Platform results
    pub platform_results: HashMap<Platform, PlatformBenchmarkResult>,

    /// Overall metrics
    pub overall_metrics: OverallBenchmarkMetrics,

    /// Statistical analysis
    pub statistics: BenchmarkStatistics,

    /// Pass/fail status
    pub passed: bool,
    pub failure_reason: Option<String>,
}

/// Platform-specific benchmark result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlatformBenchmarkResult {
    /// Platform validation score
    pub score: f32,

    /// Validation time
    pub validation_time_ms: u64,

    /// Memory usage
    pub memory_usage_mb: u64,

    /// CPU usage
    pub cpu_usage_percent: f32,

    /// Issues found
    pub issues_count: usize,

    /// Recommendations count
    pub recommendations_count: usize,
}

/// Overall benchmark metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OverallBenchmarkMetrics {
    pub total_validation_time_ms: u64,
    pub average_score: f32,
    pub score_variance: f32,
    pub total_issues_found: usize,
    pub platforms_passed: usize,
    pub platforms_failed: usize,
}

/// Benchmark statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchmarkStatistics {
    pub mean_validation_time_ms: f32,
    pub std_dev_validation_time_ms: f32,
    pub min_validation_time_ms: u64,
    pub max_validation_time_ms: u64,
    pub percentile_95_ms: u64,
    pub percentile_99_ms: u64,
    pub throughput_validations_per_second: f32,
}

/// Performance metrics for the entire benchmarking system
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct PerformanceMetrics {
    /// Total benchmarks executed
    pub total_benchmarks: u64,

    /// Average benchmark duration
    pub average_duration_ms: f32,

    /// Fastest benchmark
    pub fastest_benchmark_ms: u64,

    /// Slowest benchmark
    pub slowest_benchmark_ms: u64,

    /// Success rate
    pub success_rate_percent: f32,

    /// Platform performance distribution
    pub platform_distribution: HashMap<Platform, f32>,

    /// Error patterns
    pub error_patterns: HashMap<String, u32>,

    /// Optimization opportunities
    pub optimization_opportunities: Vec<String>,
}

/// Benchmark execution engine
pub struct BenchmarkEngine {
    /// VIBE engine for validation
    vibe_engine: super::validation::VIBEEngine,

    /// Performance metrics
    metrics: Arc<RwLock<PerformanceMetrics>>,

    /// Historical benchmark data
    benchmark_history: Arc<RwLock<BenchmarkHistory>>,
}

/// Historical benchmark data for trend analysis
#[derive(Debug, Default)]
pub struct BenchmarkHistory {
    pub results: Vec<BenchmarkResult>,
    pub trend_analysis: Option<BenchmarkTrendAnalysis>,
}

/// Benchmark trend analysis
#[derive(Debug, Clone)]
pub struct BenchmarkTrendAnalysis {
    pub performance_trend: PerformanceTrend,
    pub accuracy_trend: AccuracyTrend,
    pub regression_detection: RegressionDetection,
}

/// Performance trend over time
#[derive(Debug, Clone)]
pub struct PerformanceTrend {
    pub direction: TrendDirection,
    pub slope: f32,
    pub correlation: f32,
}

/// Accuracy trend analysis
#[derive(Debug, Clone)]
pub struct AccuracyTrend {
    pub accuracy_improvement: f32,
    pub false_positive_rate: f32,
    pub false_negative_rate: f32,
}

/// Regression detection results
#[derive(Debug, Clone)]
pub struct RegressionDetection {
    pub regressions_detected: u32,
    pub regression_threshold: f32,
    pub regression_details: Vec<RegressionDetail>,
}

/// Individual regression detail
#[derive(Debug, Clone)]
pub struct RegressionDetail {
    pub scenario_id: Uuid,
    pub platform: Platform,
    pub regression_severity: Severity,
    pub performance_drop_percent: f32,
}

impl BenchmarkEngine {
    /// Create new benchmark engine
    pub fn new(vibe_engine: super::validation::VIBEEngine) -> Self {
        Self {
            vibe_engine,
            metrics: Arc::new(RwLock::new(PerformanceMetrics::default())),
            benchmark_history: Arc::new(RwLock::new(BenchmarkHistory::default())),
        }
    }

    /// Execute a complete benchmark suite
    pub async fn execute_suite(
        &self,
        suite: &BenchmarkSuite,
        config: &ValidationConfig,
    ) -> Result<BenchmarkResult, VIBEError> {
        let start_time = Instant::now();

        // Validate suite configuration
        self.validate_suite_config(suite)?;

        // Execute warm-up iterations
        if suite.config.warmup_iterations > 0 {
            self.execute_warmup(suite, config).await?;
        }

        // Execute benchmark iterations
        let mut all_results = Vec::new();

        for iteration in 0..suite.config.iterations {
            tracing::info!(
                "Executing benchmark iteration {}/{}",
                iteration + 1,
                suite.config.iterations
            );

            let iteration_results = self.execute_iteration(suite, config).await?;
            all_results.extend(iteration_results);
        }

        // Aggregate results
        let result = self.aggregate_benchmark_results(suite, all_results, start_time)?;

        // Update metrics and history
        self.update_benchmark_metrics(&result).await?;
        self.store_benchmark_result(&result).await?;

        // Perform regression analysis
        if let Some(regression) = self.detect_regressions(&result).await? {
            tracing::warn!("Regression detected: {:?}", regression);
        }

        Ok(result)
    }

    /// Execute a single benchmark scenario
    pub async fn execute_scenario(
        &self,
        scenario: &BenchmarkScenario,
        config: &ValidationConfig,
    ) -> Result<BenchmarkResult, VIBEError> {
        let _start_time = Instant::now();

        // Validate scenario
        self.validate_scenario(scenario)?;

        // Execute validation for target platforms
        let mut platform_results = HashMap::new();

        for platform in &scenario.target_platforms {
            let platform_start = Instant::now();

            // Create platform-specific validation config
            let mut platform_config = config.clone();
            platform_config.target_platforms = vec![*platform];

            // Execute validation
            let validation_result = self
                .vibe_engine
                .validate_protocol(&scenario.protocol.content, platform_config)
                .await?;

            let validation_time = platform_start.elapsed();

            platform_results.insert(
                *platform,
                PlatformBenchmarkResult {
                    score: validation_result.overall_score,
                    validation_time_ms: validation_time.as_millis() as u64,
                    memory_usage_mb: self.estimate_memory_usage(&validation_result),
                    cpu_usage_percent: self.estimate_cpu_usage(&validation_result),
                    issues_count: validation_result.issues.len(),
                    recommendations_count: validation_result.recommendations.len(),
                },
            );
        }

        // Calculate overall metrics
        let overall_metrics = self.calculate_overall_metrics(&platform_results)?;

        // Calculate statistics
        let statistics = self.calculate_statistics(&platform_results)?;

        // Determine pass/fail status
        let (passed, failure_reason) = self.evaluate_scenario_outcome(
            &scenario.expected_outcomes,
            &overall_metrics,
            &platform_results,
        )?;

        let result = BenchmarkResult {
            result_id: Uuid::new_v4(),
            timestamp: chrono::Utc::now(),
            scenario_id: scenario.scenario_id,
            platform_results,
            overall_metrics,
            statistics,
            passed,
            failure_reason,
        };

        Ok(result)
    }

    /// Execute warm-up iterations to stabilize performance
    async fn execute_warmup(
        &self,
        suite: &BenchmarkSuite,
        config: &ValidationConfig,
    ) -> Result<(), VIBEError> {
        for _ in 0..suite.config.warmup_iterations {
            for scenario in &suite.scenarios {
                // Quick validation without storing results
                let mut warmup_config = config.clone();
                warmup_config.target_platforms = scenario.target_platforms.clone();

                let _ = self
                    .vibe_engine
                    .validate_protocol(&scenario.protocol.content, warmup_config)
                    .await;
            }
        }

        Ok(())
    }

    /// Execute a single iteration of all scenarios
    async fn execute_iteration(
        &self,
        suite: &BenchmarkSuite,
        config: &ValidationConfig,
    ) -> Result<Vec<BenchmarkResult>, VIBEError> {
        let mut results = Vec::new();

        for scenario in &suite.scenarios {
            let result = self.execute_scenario(scenario, config).await?;
            results.push(result);
        }

        Ok(results)
    }

    /// Aggregate multiple benchmark results
    fn aggregate_benchmark_results(
        &self,
        suite: &BenchmarkSuite,
        all_results: Vec<BenchmarkResult>,
        start_time: Instant,
    ) -> Result<BenchmarkResult, VIBEError> {
        let total_time = start_time.elapsed();

        // Combine all platform results
        let mut combined_platform_results = HashMap::new();
        let mut all_overall_metrics = Vec::new();

        for result in &all_results {
            for (platform, platform_result) in &result.platform_results {
                combined_platform_results
                    .entry(*platform)
                    .or_insert_with(Vec::new)
                    .push(platform_result.clone());
            }
            all_overall_metrics.push(result.overall_metrics.clone());
        }

        // Calculate average metrics across all iterations
        let average_score = all_results
            .iter()
            .map(|r| r.overall_metrics.average_score)
            .sum::<f32>()
            / all_results.len() as f32;

        let total_issues = all_results
            .iter()
            .map(|r| r.overall_metrics.total_issues_found)
            .sum::<usize>();

        let passed_scenarios = all_results.iter().filter(|r| r.passed).count();
        let failed_scenarios = all_results.len() - passed_scenarios;

        // Combine statistics
        let combined_statistics = self.combine_statistics(&all_results)?;

        // Determine overall pass/fail
        let overall_passed = if suite.scenarios.is_empty() {
            false
        } else {
            (passed_scenarios as f32 / suite.scenarios.len() as f32) >= 0.8 // 80% pass rate
        };

        let overall_failure_reason = if !overall_passed {
            Some(format!(
                "Only {}/{} scenarios passed",
                passed_scenarios,
                suite.scenarios.len()
            ))
        } else {
            None
        };

        Ok(BenchmarkResult {
            result_id: Uuid::new_v4(),
            timestamp: chrono::Utc::now(),
            scenario_id: suite.suite_id, // Use suite ID for aggregated result
            platform_results: combined_platform_results
                .into_iter()
                .map(|(platform, results)| {
                    let avg_score =
                        results.iter().map(|r| r.score).sum::<f32>() / results.len() as f32;
                    let avg_time = results.iter().map(|r| r.validation_time_ms).sum::<u64>()
                        / results.len() as u64;

                    (
                        platform,
                        PlatformBenchmarkResult {
                            score: avg_score,
                            validation_time_ms: avg_time,
                            memory_usage_mb: results.iter().map(|r| r.memory_usage_mb).sum::<u64>()
                                / results.len() as u64,
                            cpu_usage_percent: results
                                .iter()
                                .map(|r| r.cpu_usage_percent)
                                .sum::<f32>()
                                / results.len() as f32,
                            issues_count: results.iter().map(|r| r.issues_count).sum::<usize>()
                                / results.len(),
                            recommendations_count: results
                                .iter()
                                .map(|r| r.recommendations_count)
                                .sum::<usize>()
                                / results.len(),
                        },
                    )
                })
                .collect(),
            overall_metrics: OverallBenchmarkMetrics {
                total_validation_time_ms: total_time.as_millis() as u64,
                average_score,
                score_variance: self.calculate_score_variance(&all_results),
                total_issues_found: total_issues / all_results.len(),
                platforms_passed: passed_scenarios,
                platforms_failed: failed_scenarios,
            },
            statistics: combined_statistics,
            passed: overall_passed,
            failure_reason: overall_failure_reason,
        })
    }

    /// Validate suite configuration
    fn validate_suite_config(&self, suite: &BenchmarkSuite) -> Result<(), VIBEError> {
        if suite.scenarios.is_empty() {
            return Err(VIBEError::BenchmarkError(
                "Benchmark suite has no scenarios".to_string(),
            ));
        }

        if suite.config.iterations == 0 {
            return Err(VIBEError::BenchmarkError(
                "Benchmark iterations must be greater than 0".to_string(),
            ));
        }

        if suite.config.warmup_iterations >= suite.config.iterations {
            return Err(VIBEError::BenchmarkError(
                "Warm-up iterations must be less than total iterations".to_string(),
            ));
        }

        Ok(())
    }

    /// Validate individual scenario
    fn validate_scenario(&self, scenario: &BenchmarkScenario) -> Result<(), VIBEError> {
        if scenario.protocol.content.trim().is_empty() {
            return Err(VIBEError::BenchmarkError(
                "Scenario protocol content is empty".to_string(),
            ));
        }

        if scenario.target_platforms.is_empty() {
            return Err(VIBEError::BenchmarkError(
                "Scenario has no target platforms".to_string(),
            ));
        }

        // Validate expected outcomes consistency
        for platform in &scenario.expected_outcomes.required_validations {
            if !scenario.target_platforms.contains(platform) {
                return Err(VIBEError::BenchmarkError(format!(
                    "Platform {:?} required in outcomes but not in target platforms",
                    platform
                )));
            }
        }

        Ok(())
    }

    /// Calculate overall metrics from platform results
    fn calculate_overall_metrics(
        &self,
        platform_results: &HashMap<Platform, PlatformBenchmarkResult>,
    ) -> Result<OverallBenchmarkMetrics, VIBEError> {
        let scores: Vec<f32> = platform_results.values().map(|r| r.score).collect();
        let average_score = scores.iter().sum::<f32>() / scores.len() as f32;

        let score_variance = self.calculate_variance(&scores);

        let total_issues = platform_results.values().map(|r| r.issues_count).sum();

        let validation_times: Vec<u64> = platform_results
            .values()
            .map(|r| r.validation_time_ms)
            .collect();
        let total_time = validation_times.iter().sum::<u64>();

        let passed_platforms = platform_results
            .values()
            .filter(|r| r.score >= 70.0)
            .count();

        Ok(OverallBenchmarkMetrics {
            total_validation_time_ms: total_time,
            average_score,
            score_variance,
            total_issues_found: total_issues,
            platforms_passed: passed_platforms,
            platforms_failed: platform_results.len() - passed_platforms,
        })
    }

    /// Calculate statistics from platform results
    fn calculate_statistics(
        &self,
        platform_results: &HashMap<Platform, PlatformBenchmarkResult>,
    ) -> Result<BenchmarkStatistics, VIBEError> {
        let times: Vec<u64> = platform_results
            .values()
            .map(|r| r.validation_time_ms)
            .collect();

        if times.is_empty() {
            return Err(VIBEError::BenchmarkError(
                "No platform results for statistics".to_string(),
            ));
        }

        let mean_time = times.iter().sum::<u64>() as f32 / times.len() as f32;
        let variance =
            self.calculate_variance(&times.iter().map(|&t| t as f32).collect::<Vec<_>>());
        let std_dev = variance.sqrt();

        let mut sorted_times = times.clone();
        sorted_times.sort();

        let min_time = sorted_times.first().copied().unwrap_or(0);
        let max_time = sorted_times.last().copied().unwrap_or(0);

        let percentile_95 = self.calculate_percentile(&sorted_times, 95.0);
        let percentile_99 = self.calculate_percentile(&sorted_times, 99.0);

        let throughput = if mean_time > 0.0 {
            1000.0 / mean_time
        } else {
            0.0
        };

        Ok(BenchmarkStatistics {
            mean_validation_time_ms: mean_time,
            std_dev_validation_time_ms: std_dev,
            min_validation_time_ms: min_time,
            max_validation_time_ms: max_time,
            percentile_95_ms: percentile_95,
            percentile_99_ms: percentile_99,
            throughput_validations_per_second: throughput,
        })
    }

    /// Combine statistics from multiple results
    fn combine_statistics(
        &self,
        results: &[BenchmarkResult],
    ) -> Result<BenchmarkStatistics, VIBEError> {
        if results.is_empty() {
            return Err(VIBEError::BenchmarkError(
                "No results to combine".to_string(),
            ));
        }

        // Combine all validation times
        let all_times: Vec<f32> = results
            .iter()
            .flat_map(|r| {
                r.platform_results
                    .values()
                    .map(|pr| pr.validation_time_ms as f32)
                    .collect::<Vec<_>>()
            })
            .collect();

        if all_times.is_empty() {
            return Err(VIBEError::BenchmarkError(
                "No validation times found".to_string(),
            ));
        }

        let mean_time = all_times.iter().sum::<f32>() / all_times.len() as f32;
        let variance = self.calculate_variance(&all_times);
        let std_dev = variance.sqrt();

        let min_time = all_times.iter().fold(f32::INFINITY, |a, &b| a.min(b)) as u64;
        let max_time = all_times.iter().fold(0.0f32, |a, &b| a.max(b)) as u64;

        let mut sorted_times = all_times.iter().map(|&t| t as u64).collect::<Vec<_>>();
        sorted_times.sort();

        let percentile_95 = self.calculate_percentile(&sorted_times, 95.0);
        let percentile_99 = self.calculate_percentile(&sorted_times, 99.0);

        let throughput = if mean_time > 0.0 {
            1000.0 / mean_time
        } else {
            0.0
        };

        Ok(BenchmarkStatistics {
            mean_validation_time_ms: mean_time,
            std_dev_validation_time_ms: std_dev,
            min_validation_time_ms: min_time,
            max_validation_time_ms: max_time,
            percentile_95_ms: percentile_95,
            percentile_99_ms: percentile_99,
            throughput_validations_per_second: throughput,
        })
    }

    /// Evaluate if scenario meets expected outcomes
    fn evaluate_scenario_outcome(
        &self,
        expected: &ExpectedOutcomes,
        metrics: &OverallBenchmarkMetrics,
        platform_results: &HashMap<Platform, PlatformBenchmarkResult>,
    ) -> Result<(bool, Option<String>), VIBEError> {
        let (min_expected, max_expected) = expected.expected_score_range;
        let (min_issues, max_issues) = expected.expected_issues_count;

        // Check score range
        if metrics.average_score < min_expected || metrics.average_score > max_expected {
            return Ok((
                false,
                Some(format!(
                    "Score {:.1} outside expected range {:.1}-{:.1}",
                    metrics.average_score, min_expected, max_expected
                )),
            ));
        }

        // Check issues count
        if metrics.total_issues_found < min_issues || metrics.total_issues_found > max_issues {
            return Ok((
                false,
                Some(format!(
                    "Issues count {} outside expected range {}-{}",
                    metrics.total_issues_found, min_issues, max_issues
                )),
            ));
        }

        // Check platform scores
        for (platform, expected_score) in &expected.expected_platform_scores {
            if let Some(result) = platform_results.get(platform) {
                let score_diff = (result.score - expected_score).abs();
                if score_diff > 10.0 {
                    // 10 point tolerance
                    return Ok((
                        false,
                        Some(format!(
                            "Platform {:?} score {:.1} differs from expected {:.1}",
                            platform, result.score, expected_score
                        )),
                    ));
                }
            }
        }

        // Check required validations
        for platform in &expected.required_validations {
            if !platform_results.contains_key(platform) {
                return Ok((
                    false,
                    Some(format!(
                        "Required platform {:?} validation missing",
                        platform
                    )),
                ));
            }
        }

        Ok((true, None))
    }

    /// Calculate variance for a set of values
    fn calculate_variance(&self, values: &[f32]) -> f32 {
        if values.len() < 2 {
            return 0.0;
        }

        let mean: f32 = values.iter().sum::<f32>() / values.len() as f32;
        values
            .iter()
            .map(|&value| (value - mean).powi(2))
            .sum::<f32>()
            / (values.len() - 1) as f32
    }

    /// Calculate score variance across results
    fn calculate_score_variance(&self, results: &[BenchmarkResult]) -> f32 {
        let scores: Vec<f32> = results
            .iter()
            .map(|r| r.overall_metrics.average_score)
            .collect();
        self.calculate_variance(&scores)
    }

    /// Calculate percentile from sorted data
    fn calculate_percentile(&self, sorted_data: &[u64], percentile: f32) -> u64 {
        if sorted_data.is_empty() {
            return 0;
        }

        let index = ((percentile / 100.0) * (sorted_data.len() - 1) as f32).round() as usize;
        sorted_data[index.min(sorted_data.len() - 1)]
    }

    /// Estimate memory usage from validation result
    fn estimate_memory_usage(&self, _result: &super::validation::ValidationResult) -> u64 {
        // Simplified memory estimation based on result complexity
        150 // Base memory usage in MB
    }

    /// Estimate CPU usage from validation result
    fn estimate_cpu_usage(&self, _result: &super::validation::ValidationResult) -> f32 {
        // Simplified CPU estimation
        30.0 // Base CPU usage percentage
    }

    /// Update benchmark metrics
    async fn update_benchmark_metrics(&self, result: &BenchmarkResult) -> Result<(), VIBEError> {
        let mut metrics = self.metrics.write().await;

        metrics.total_benchmarks += 1;

        // Update average duration
        let current_avg = metrics.average_duration_ms;
        let new_avg = if metrics.total_benchmarks == 1 {
            result.overall_metrics.total_validation_time_ms as f32
        } else {
            (current_avg * (metrics.total_benchmarks - 1) as f32
                + result.overall_metrics.total_validation_time_ms as f32)
                / metrics.total_benchmarks as f32
        };
        metrics.average_duration_ms = new_avg;

        // Update min/max times
        if metrics.fastest_benchmark_ms == 0
            || result.overall_metrics.total_validation_time_ms < metrics.fastest_benchmark_ms
        {
            metrics.fastest_benchmark_ms = result.overall_metrics.total_validation_time_ms;
        }

        if result.overall_metrics.total_validation_time_ms > metrics.slowest_benchmark_ms {
            metrics.slowest_benchmark_ms = result.overall_metrics.total_validation_time_ms;
        }

        // Update success rate
        let _success_count = if result.passed { 1 } else { 0 };
        let current_success_rate = metrics.success_rate_percent;
        let new_success_rate = if metrics.total_benchmarks == 1 {
            if result.passed {
                100.0
            } else {
                0.0
            }
        } else {
            (current_success_rate * (metrics.total_benchmarks - 1) as f32
                + if result.passed { 100.0 } else { 0.0 })
                / metrics.total_benchmarks as f32
        };
        metrics.success_rate_percent = new_success_rate;

        Ok(())
    }

    /// Store benchmark result in history
    async fn store_benchmark_result(&self, result: &BenchmarkResult) -> Result<(), VIBEError> {
        let mut history = self.benchmark_history.write().await;
        history.results.push(result.clone());

        // Keep only last 1000 results
        if history.results.len() > 1000 {
            let overflow = history.results.len() - 1000;
            history.results.drain(0..overflow);
        }

        Ok(())
    }

    /// Detect regressions in benchmark results
    async fn detect_regressions(
        &self,
        _current_result: &BenchmarkResult,
    ) -> Result<Option<RegressionDetection>, VIBEError> {
        // Simplified regression detection
        // In a real implementation, this would compare with historical baselines

        let history = self.benchmark_history.read().await;
        if history.results.len() < 10 {
            return Ok(None); // Need sufficient history
        }

        // Compare with recent average (last 5 results)
        let recent_results = &history.results[history.results.len().saturating_sub(5)..];
        let recent_avg_time: f32 = recent_results
            .iter()
            .map(|r| r.overall_metrics.total_validation_time_ms as f32)
            .sum::<f32>()
            / recent_results.len() as f32;

        let current_time = _current_result.overall_metrics.total_validation_time_ms as f32;

        // Check for significant regression (20% slower)
        if current_time > recent_avg_time * 1.2 {
            return Ok(Some(RegressionDetection {
                regressions_detected: 1,
                regression_threshold: 20.0,
                regression_details: vec![RegressionDetail {
                    scenario_id: _current_result.scenario_id,
                    platform: Platform::Web, // Simplified
                    regression_severity: Severity::High,
                    performance_drop_percent: ((current_time - recent_avg_time) / recent_avg_time)
                        * 100.0,
                }],
            }));
        }

        Ok(None)
    }
}

impl Default for BenchmarkEngine {
    fn default() -> Self {
        let vibe_engine = super::validation::VIBEEngine::new();
        Self::new(vibe_engine)
    }
}

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

    #[tokio::test]
    async fn test_benchmark_engine_creation() {
        let vibe_engine = super::validation::VIBEEngine::new();
        let engine = BenchmarkEngine::new(vibe_engine);
        assert!(engine.vibe_engine.get_statistics().await.is_ok());
    }

    #[test]
    fn test_scenario_validation() {
        let vibe_engine = super::validation::VIBEEngine::new();
        let engine = BenchmarkEngine::new(vibe_engine);

        let scenario = BenchmarkScenario {
            scenario_id: Uuid::new_v4(),
            name: "Test Scenario".to_string(),
            description: "A test benchmark scenario".to_string(),
            category: BenchmarkCategory::Performance,
            protocol: BenchmarkProtocol {
                content: "Test protocol content".to_string(),
                protocol_type: ProtocolType::ThinkToolChain,
                complexity: ProtocolComplexity::Simple,
                characteristics: ProtocolCharacteristics {
                    has_multiple_platforms: false,
                    has_security_requirements: false,
                    has_performance_requirements: false,
                    has_accessibility_requirements: false,
                    has_integration_requirements: false,
                    estimated_validation_time_ms: 1000,
                },
            },
            target_platforms: vec![Platform::Web],
            performance_thresholds: PerformanceThresholds {
                max_validation_time_ms: 5000,
                max_memory_usage_mb: 1000,
                min_score_threshold: 70.0,
                max_error_rate_percent: 5.0,
            },
            expected_outcomes: ExpectedOutcomes {
                expected_score_range: (60.0, 90.0),
                expected_issues_count: (0, 5),
                expected_platform_scores: HashMap::new(),
                required_validations: vec![Platform::Web],
            },
        };

        assert!(engine.validate_scenario(&scenario).is_ok());

        // Test empty protocol
        let mut invalid_scenario = scenario.clone();
        invalid_scenario.protocol.content = "".to_string();
        assert!(engine.validate_scenario(&invalid_scenario).is_err());
    }

    #[test]
    fn test_statistics_calculation() {
        let vibe_engine = super::validation::VIBEEngine::new();
        let engine = BenchmarkEngine::new(vibe_engine);

        let mut platform_results = HashMap::new();
        platform_results.insert(
            Platform::Web,
            PlatformBenchmarkResult {
                score: 80.0,
                validation_time_ms: 1000,
                memory_usage_mb: 100,
                cpu_usage_percent: 25.0,
                issues_count: 2,
                recommendations_count: 3,
            },
        );
        platform_results.insert(
            Platform::Backend,
            PlatformBenchmarkResult {
                score: 75.0,
                validation_time_ms: 1200,
                memory_usage_mb: 150,
                cpu_usage_percent: 30.0,
                issues_count: 3,
                recommendations_count: 2,
            },
        );

        let metrics = engine.calculate_overall_metrics(&platform_results).unwrap();
        assert!(metrics.average_score > 0.0);
        assert!(metrics.total_validation_time_ms > 0);

        let stats = engine.calculate_statistics(&platform_results).unwrap();
        assert!(stats.mean_validation_time_ms > 0.0);
        assert!(stats.throughput_validations_per_second > 0.0);
    }
}