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
//! # VIBE Validation Engine
//!
//! Core validation engine implementing MiniMax M2's "Agent-as-a-Verifier" paradigm
//! for comprehensive protocol validation across multiple platforms.

use super::validation_config::ValidationConfig;
use super::*;
use futures::future::join_all;
use std::sync::Arc;
use std::time::Instant;
use tokio::sync::{RwLock, Semaphore};

/// The main VIBE validation engine that orchestrates protocol validation
/// across multiple platforms using the "Agent-as-a-Verifier" paradigm.
pub struct VIBEEngine {
    /// Internal state protected by RwLock for concurrent access
    state: Arc<RwLock<EngineState>>,

    /// Validation adapters for different platforms
    adapters: HashMap<Platform, Arc<dyn PlatformValidator>>,

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

    /// Concurrent validation limit
    concurrent_limit: usize,

    /// Validation cache for performance optimization
    cache: Arc<RwLock<ValidationCache>>,
}

/// Internal engine state
#[derive(Debug, Clone)]
struct EngineState {
    /// Total protocols validated
    protocols_validated: u64,

    /// Platform usage statistics
    platform_stats: HashMap<Platform, u64>,

    /// Recent validation results for trend analysis
    recent_results: VecDeque<ValidationResult>,

    /// System health metrics
    health_metrics: HealthMetrics,
}

/// Combined validation result across platforms.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidationResult {
    pub overall_score: f32,
    pub platform_scores: HashMap<Platform, f32>,
    pub confidence_interval: Option<ConfidenceInterval>,
    pub status: ValidationStatus,
    pub detailed_results: HashMap<Platform, PlatformValidationResult>,
    pub validation_time_ms: u64,
    pub issues: Vec<ValidationIssue>,
    pub recommendations: Vec<Recommendation>,
    pub timestamp: chrono::DateTime<chrono::Utc>,
    pub protocol_id: Uuid,
}

/// Health metrics for monitoring system performance
#[derive(Debug, Clone)]
#[allow(dead_code)]
struct HealthMetrics {
    pub cpu_usage_percent: f32,
    pub memory_usage_mb: u64,
    pub disk_usage_mb: u64,
    pub network_requests_per_minute: u32,
    pub validation_success_rate: f32,
}

/// Performance metrics tracking
#[derive(Debug, Default, Clone)]
pub struct PerformanceMetrics {
    pub average_validation_time_ms: u64,
    pub fastest_validation_ms: u64,
    pub slowest_validation_ms: u64,
    pub protocols_per_minute: f32,
    pub platform_distribution: HashMap<Platform, f32>,
    pub error_rate_percent: f32,
}

/// Validation cache for performance optimization
#[derive(Debug, Default)]
struct ValidationCache {
    /// Cache of recent validation results
    results: HashMap<Uuid, CachedValidation>,

    /// Platform-specific performance data
    platform_data: HashMap<Platform, PlatformPerformance>,

    /// Cache statistics
    hit_rate_percent: f32,
    total_requests: u64,
    cache_hits: u64,
}

/// Cached validation result
#[derive(Debug, Clone)]
struct CachedValidation {
    result: ValidationResult,
    timestamp: chrono::DateTime<chrono::Utc>,
    ttl_seconds: u64,
}

/// Platform-specific performance data
#[derive(Debug, Clone)]
#[allow(dead_code)]
struct PlatformPerformance {
    pub average_response_time_ms: u64,
    pub success_rate: f32,
    pub error_patterns: HashMap<String, u32>,
    pub optimization_suggestions: Vec<String>,
}

impl VIBEEngine {
    /// Create a new VIBE validation engine
    pub fn new() -> Self {
        let mut engine = Self {
            state: Arc::new(RwLock::new(EngineState {
                protocols_validated: 0,
                platform_stats: HashMap::new(),
                recent_results: VecDeque::new(),
                health_metrics: HealthMetrics {
                    cpu_usage_percent: 0.0,
                    memory_usage_mb: 0,
                    disk_usage_mb: 0,
                    network_requests_per_minute: 0,
                    validation_success_rate: 1.0,
                },
            })),
            adapters: HashMap::new(),
            metrics: Arc::new(RwLock::new(PerformanceMetrics::default())),
            concurrent_limit: 10,
            cache: Arc::new(RwLock::new(ValidationCache::default())),
        };

        // Initialize default platform adapters
        engine.initialize_default_adapters();

        engine
    }

    /// Create VIBE engine with custom configuration
    pub fn with_config(
        concurrent_limit: usize,
        custom_adapters: HashMap<Platform, Arc<dyn PlatformValidator>>,
    ) -> Self {
        let mut engine = Self {
            state: Arc::new(RwLock::new(EngineState {
                protocols_validated: 0,
                platform_stats: HashMap::new(),
                recent_results: VecDeque::new(),
                health_metrics: HealthMetrics {
                    cpu_usage_percent: 0.0,
                    memory_usage_mb: 0,
                    disk_usage_mb: 0,
                    network_requests_per_minute: 0,
                    validation_success_rate: 1.0,
                },
            })),
            adapters: custom_adapters,
            metrics: Arc::new(RwLock::new(PerformanceMetrics::default())),
            concurrent_limit,
            cache: Arc::new(RwLock::new(ValidationCache::default())),
        };

        // Add any missing default adapters
        engine.initialize_default_adapters();

        engine
    }

    /// Initialize default platform validators
    fn initialize_default_adapters(&mut self) {
        self.adapters.entry(Platform::Web).or_insert_with(|| {
            Arc::new(super::platforms::WebValidator::new()) as Arc<dyn PlatformValidator>
        });

        self.adapters
            .entry(Platform::Simulation)
            .or_insert_with(|| {
                Arc::new(super::platforms::SimulationValidator::new()) as Arc<dyn PlatformValidator>
            });

        self.adapters.entry(Platform::Android).or_insert_with(|| {
            Arc::new(super::platforms::AndroidValidator::new()) as Arc<dyn PlatformValidator>
        });

        self.adapters.entry(Platform::IOS).or_insert_with(|| {
            Arc::new(super::platforms::IOSValidator::new()) as Arc<dyn PlatformValidator>
        });

        self.adapters.entry(Platform::Backend).or_insert_with(|| {
            Arc::new(super::platforms::BackendValidator::new()) as Arc<dyn PlatformValidator>
        });
    }

    /// Validate a protocol across specified platforms using Agent-as-Verifier paradigm
    pub async fn validate_protocol(
        &self,
        protocol_content: &str,
        config: ValidationConfig,
    ) -> Result<ValidationResult> {
        let start_time = Instant::now();

        // Generate protocol ID for caching and tracking
        let protocol_id = self.generate_protocol_id(protocol_content);

        // Check cache for recent validation
        if let Some(cached_result) = self.get_cached_validation(&protocol_id).await? {
            tracing::info!(
                "Using cached validation result for protocol {}",
                protocol_id
            );
            return Ok(cached_result);
        }

        // Validate protocol content
        self.validate_protocol_content(protocol_content)?;

        // Create validation semaphore for concurrent platform validation
        let semaphore = Arc::new(Semaphore::new(self.concurrent_limit));

        // Spawn validation tasks for each platform
        let mut validation_tasks = Vec::new();

        for platform in &config.target_platforms {
            if let Some(adapter) = self.adapters.get(platform) {
                let adapter = Arc::clone(adapter);
                let semaphore_clone = semaphore.clone();
                let config_clone = config.clone();
                let protocol_content = protocol_content.to_string();
                let platform = *platform;

                let task = tokio::spawn(async move {
                    let _permit = semaphore_clone.acquire().await.unwrap();
                    adapter
                        .validate_protocol(&protocol_content, &config_clone, platform)
                        .await
                });

                validation_tasks.push(task);
            } else {
                tracing::warn!("No validator adapter found for platform {:?}", platform);
            }
        }

        // Wait for all platform validations to complete
        let join_results = join_all(validation_tasks).await;
        let mut platform_results = Vec::with_capacity(join_results.len());
        for join_result in join_results {
            match join_result {
                Ok(Ok(result)) => platform_results.push(result),
                Ok(Err(e)) => return Err(e),
                Err(e) => {
                    return Err(VIBEError::ValidationError(format!(
                        "Platform worker task failed: {e}"
                    )));
                }
            }
        }

        // Aggregate results using Agent-as-Verifier logic
        let aggregated_result = self
            .aggregate_platform_results(platform_results, &config, start_time)
            .await?;

        // Update statistics and cache
        self.update_statistics(&aggregated_result).await?;
        self.cache_validation_result(&protocol_id, &aggregated_result)
            .await?;

        Ok(aggregated_result)
    }

    /// Validate protocol content for basic requirements
    fn validate_protocol_content(&self, content: &str) -> Result<()> {
        if content.trim().is_empty() {
            return Err(VIBEError::InvalidProtocol(
                "Protocol content is empty".to_string(),
            ));
        }

        if content.len() > 100_000 {
            return Err(VIBEError::InvalidProtocol(
                "Protocol content exceeds maximum size".to_string(),
            ));
        }

        // Check for basic protocol structure
        if !content.contains("protocol") && !content.contains("Protocol") {
            tracing::warn!("Protocol content may not follow standard structure");
        }

        Ok(())
    }

    /// Aggregate results from multiple platform validators
    async fn aggregate_platform_results(
        &self,
        platform_results: Vec<PlatformValidationResult>,
        config: &ValidationConfig,
        start_time: Instant,
    ) -> Result<ValidationResult> {
        let validation_duration = start_time.elapsed();

        // Collect scores from all platforms
        let mut score_values = Vec::new();
        let mut platform_scores: HashMap<Platform, f32> = HashMap::new();
        let mut detailed_results = HashMap::new();
        let mut overall_issues = Vec::new();

        for result in platform_results {
            score_values.push(result.score);
            platform_scores.insert(result.platform, result.score);
            detailed_results.insert(result.platform, result.clone());

            // Collect issues for overall analysis
            if !result.issues.is_empty() {
                overall_issues.extend(result.issues);
            }
        }

        // Calculate overall VIBE score (weighted average with confidence intervals)
        let overall_score =
            self.calculate_overall_score(&score_values, &config.validation_criteria)?;

        // Generate confidence interval based on platform consensus
        let confidence_interval = Some(self.calculate_confidence_interval(&score_values)?);

        // Determine overall validation status
        let status = if overall_score >= config.minimum_score {
            ValidationStatus::Passed
        } else {
            ValidationStatus::Failed
        };

        // Generate recommendations based on validation results
        let recommendations = self.generate_recommendations(&detailed_results, overall_score)?;

        // Create validation result
        let result = ValidationResult {
            overall_score,
            platform_scores,
            confidence_interval,
            status,
            detailed_results,
            validation_time_ms: validation_duration.as_millis() as u64,
            issues: overall_issues,
            recommendations,
            timestamp: chrono::Utc::now(),
            protocol_id: self.generate_protocol_id("aggregated"),
        };

        Ok(result)
    }

    /// Calculate overall VIBE score using weighted aggregation
    fn calculate_overall_score(
        &self,
        scores: &[f32],
        criteria: &ValidationCriteria,
    ) -> Result<f32> {
        if scores.is_empty() {
            return Err(VIBEError::NoValidations(
                "No platform scores available".to_string(),
            ));
        }

        // Base weights for different platforms
        let platform_weights = match scores.len() {
            1 => vec![1.0],
            2 => vec![0.4, 0.6],
            3 => vec![0.3, 0.4, 0.3],
            4 => vec![0.25, 0.25, 0.25, 0.25],
            _ => vec![0.2, 0.2, 0.2, 0.2, 0.2], // 5 platforms
        };

        // Calculate weighted average
        let mut weighted_sum = 0.0;
        let mut total_weight = 0.0;

        for (i, &score) in scores.iter().enumerate() {
            if i < platform_weights.len() {
                let weight = platform_weights[i];
                weighted_sum += score * weight;
                total_weight += weight;
            }
        }

        let base_score = weighted_sum / total_weight;

        // Apply criteria adjustments
        let mut adjusted_score = base_score;

        // Adjust for logical consistency requirement
        if criteria.logical_consistency && base_score < 70.0 {
            adjusted_score *= 0.9; // Penalty for poor logical consistency
        }

        // Adjust for practical applicability requirement
        if criteria.practical_applicability && base_score < 60.0 {
            adjusted_score *= 0.85; // Strong penalty for impractical protocols
        }

        // Adjust for platform compatibility
        if criteria.platform_compatibility && scores.len() < 3 {
            adjusted_score *= 0.95; // Slight penalty for limited platform coverage
        }

        // Ensure score stays within valid range
        Ok(adjusted_score.clamp(0.0, 100.0))
    }

    /// Calculate confidence interval based on platform score variance
    fn calculate_confidence_interval(&self, platform_scores: &[f32]) -> Result<ConfidenceInterval> {
        if platform_scores.is_empty() {
            return Err(VIBEError::NoValidations(
                "No platform scores for confidence calculation".to_string(),
            ));
        }

        let mean: f32 = platform_scores.iter().sum::<f32>() / platform_scores.len() as f32;
        let variance: f32 = platform_scores
            .iter()
            .map(|&score| (score - mean).powi(2))
            .sum::<f32>()
            / platform_scores.len() as f32;
        let std_dev = variance.sqrt();

        // 95% confidence interval
        let margin_of_error = 1.96 * std_dev / (platform_scores.len() as f32).sqrt();

        Ok(ConfidenceInterval {
            lower: (mean - margin_of_error).max(0.0),
            upper: (mean + margin_of_error).min(100.0),
            confidence_level: 0.95,
            sample_size: platform_scores.len(),
        })
    }

    /// Generate actionable recommendations based on validation results
    fn generate_recommendations(
        &self,
        detailed_results: &HashMap<Platform, PlatformValidationResult>,
        overall_score: f32,
    ) -> Result<Vec<Recommendation>> {
        let mut recommendations = Vec::new();

        // Overall performance recommendations
        if overall_score < 60.0 {
            recommendations.push(Recommendation {
                priority: Priority::High,
                category: RecommendationCategory::Overall,
                title: "Major Protocol Revision Needed".to_string(),
                description: "Protocol requires significant improvements across multiple areas."
                    .to_string(),
                actionable_steps: vec![
                    "Review protocol logic flow".to_string(),
                    "Strengthen validation criteria".to_string(),
                    "Improve error handling".to_string(),
                ],
            });
        } else if overall_score < 80.0 {
            recommendations.push(Recommendation {
                priority: Priority::Medium,
                category: RecommendationCategory::Overall,
                title: "Protocol Enhancement Recommended".to_string(),
                description: "Protocol is functional but could benefit from targeted improvements."
                    .to_string(),
                actionable_steps: vec![
                    "Optimize performance-critical sections".to_string(),
                    "Add comprehensive error handling".to_string(),
                    "Enhance documentation".to_string(),
                ],
            });
        }

        // Platform-specific recommendations
        for (platform, result) in detailed_results {
            if result.score < 60.0 {
                recommendations.push(Recommendation {
                    priority: Priority::High,
                    category: RecommendationCategory::PlatformSpecific,
                    title: format!("{} Platform Issues Detected", platform),
                    description: format!(
                        "Protocol scored {} on {} platform with {} issues detected.",
                        result.score,
                        platform,
                        result.issues.len()
                    ),
                    actionable_steps: self
                        .generate_platform_specific_steps(platform, &result.issues)?,
                });
            }
        }

        // Performance recommendations
        let avg_response_time = detailed_results
            .values()
            .map(|r| r.performance_metrics.average_response_time_ms)
            .sum::<u64>()
            / detailed_results.len() as u64;

        if avg_response_time > 2000 {
            recommendations.push(Recommendation {
                priority: Priority::Medium,
                category: RecommendationCategory::Performance,
                title: "Performance Optimization Needed".to_string(),
                description: format!(
                    "Average response time ({})ms exceeds recommended threshold (2000ms).",
                    avg_response_time
                ),
                actionable_steps: vec![
                    "Optimize algorithm complexity".to_string(),
                    "Implement caching strategies".to_string(),
                    "Reduce network requests".to_string(),
                ],
            });
        }

        Ok(recommendations)
    }

    /// Generate platform-specific improvement steps
    fn generate_platform_specific_steps(
        &self,
        platform: &Platform,
        issues: &[ValidationIssue],
    ) -> Result<Vec<String>> {
        let mut steps = Vec::new();

        match platform {
            Platform::Web => {
                steps.push("Optimize UI/UX design patterns".to_string());
                steps.push("Implement responsive design".to_string());
                steps.push("Add accessibility features".to_string());
            }
            Platform::Simulation => {
                steps.push("Review logic flow completeness".to_string());
                steps.push("Validate state management".to_string());
                steps.push("Test edge cases".to_string());
            }
            Platform::Android => {
                steps.push("Optimize for Android Material Design".to_string());
                steps.push("Implement touch gesture handling".to_string());
                steps.push("Test on multiple screen densities".to_string());
            }
            Platform::IOS => {
                steps.push("Follow iOS Human Interface Guidelines".to_string());
                steps.push("Implement native iOS patterns".to_string());
                steps.push("Optimize for different iOS versions".to_string());
            }
            Platform::Backend => {
                steps.push("Strengthen API security".to_string());
                steps.push("Implement proper error handling".to_string());
                steps.push("Optimize database queries".to_string());
            }
        }

        // Add issue-specific steps
        for issue in issues {
            match issue.severity {
                Severity::Critical => steps.push(format!(
                    "URGENT: Address critical issue - {}",
                    issue.description
                )),
                Severity::High => steps.push(format!(
                    "HIGH: Fix high-priority issue - {}",
                    issue.description
                )),
                Severity::Medium => steps.push(format!(
                    "MEDIUM: Address medium-priority issue - {}",
                    issue.description
                )),
                Severity::Low => {
                    steps.push(format!("LOW: Consider improving - {}", issue.description))
                }
            }
        }

        Ok(steps)
    }

    /// Update internal statistics after validation
    async fn update_statistics(&self, result: &ValidationResult) -> Result<()> {
        let mut state = self.state.write().await;

        // Update protocol count
        state.protocols_validated += 1;

        // Update platform statistics
        for platform in result.detailed_results.keys() {
            *state.platform_stats.entry(*platform).or_insert(0) += 1;
        }

        // Add to recent results (keep last 100)
        state.recent_results.push_back(result.clone());
        if state.recent_results.len() > 100 {
            state.recent_results.pop_front();
        }

        // Update health metrics
        let success = result.status == ValidationStatus::Passed;
        let current_success_rate = state.health_metrics.validation_success_rate;
        let _total_validations = state.protocols_validated as f32;

        // Calculate new success rate with exponential moving average
        let alpha = 0.1; // Smoothing factor
        let new_success_rate = if success {
            current_success_rate * (1.0 - alpha) + alpha * 1.0
        } else {
            current_success_rate * (1.0 - alpha) + alpha * 0.0
        };

        state.health_metrics.validation_success_rate = new_success_rate;

        // Update performance metrics
        let mut metrics = self.metrics.write().await;
        let validation_time = result.validation_time_ms;

        // Update timing metrics
        if metrics.fastest_validation_ms == 0 || validation_time < metrics.fastest_validation_ms {
            metrics.fastest_validation_ms = validation_time;
        }
        if validation_time > metrics.slowest_validation_ms {
            metrics.slowest_validation_ms = validation_time;
        }

        // Update average (exponential moving average)
        let alpha = 0.2;
        if metrics.average_validation_time_ms == 0 {
            metrics.average_validation_time_ms = validation_time;
        } else {
            metrics.average_validation_time_ms =
                (metrics.average_validation_time_ms as f32 * (1.0 - alpha)
                    + validation_time as f32 * alpha) as u64;
        }

        Ok(())
    }

    /// Cache validation result for performance
    async fn cache_validation_result(
        &self,
        protocol_id: &Uuid,
        result: &ValidationResult,
    ) -> Result<()> {
        let mut cache = self.cache.write().await;

        let cached = CachedValidation {
            result: result.clone(),
            timestamp: chrono::Utc::now(),
            ttl_seconds: 3600, // 1 hour TTL
        };

        cache.results.insert(*protocol_id, cached);

        // Clean expired entries
        let now = chrono::Utc::now();
        cache.results.retain(|_, cached_result| {
            now.signed_duration_since(cached_result.timestamp)
                .num_seconds()
                < cached_result.ttl_seconds as i64
        });

        Ok(())
    }

    /// Retrieve cached validation result
    async fn get_cached_validation(&self, protocol_id: &Uuid) -> Result<Option<ValidationResult>> {
        let cache = self.cache.read().await;

        if let Some(cached) = cache.results.get(protocol_id) {
            let now = chrono::Utc::now();
            let age_seconds = now.signed_duration_since(cached.timestamp).num_seconds();

            if age_seconds < cached.ttl_seconds as i64 {
                // Update cache statistics
                let mut cache_mut = self.cache.write().await;
                cache_mut.cache_hits += 1;
                cache_mut.total_requests += 1;
                cache_mut.hit_rate_percent =
                    (cache_mut.cache_hits as f32 / cache_mut.total_requests as f32) * 100.0;

                return Ok(Some(cached.result.clone()));
            }
        }

        Ok(None)
    }

    /// Generate protocol ID for caching and tracking
    fn generate_protocol_id(&self, content: &str) -> Uuid {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();
        content.hash(&mut hasher);
        Uuid::from_u128(hasher.finish() as u128)
    }

    /// Get current engine statistics
    pub async fn get_statistics(&self) -> Result<VIBEStats> {
        let state = self.state.read().await;
        let _metrics = self.metrics.read().await;
        let _cache = self.cache.read().await;

        // Calculate validation trends from recent results
        let validation_trends: Vec<ScoreTrend> = state
            .recent_results
            .iter()
            .map(|result| {
                // Determine platform distribution from detailed results
                let platform = if !result.detailed_results.is_empty() {
                    result
                        .detailed_results
                        .keys()
                        .next()
                        .copied()
                        .unwrap_or(Platform::Web)
                } else {
                    Platform::Web
                };

                ScoreTrend {
                    timestamp: result.timestamp,
                    score: result.overall_score,
                    platform,
                }
            })
            .collect();

        Ok(VIBEStats {
            total_validations: state.protocols_validated,
            average_score: if !validation_trends.is_empty() {
                validation_trends.iter().map(|t| t.score).sum::<f32>()
                    / validation_trends.len() as f32
            } else {
                0.0
            },
            success_rate: state.health_metrics.validation_success_rate,
            platform_distribution: state
                .platform_stats
                .iter()
                .map(|(platform, count)| (*platform, *count as u32))
                .collect(),
            validation_trends,
        })
    }

    /// Add custom platform validator
    pub fn add_platform_validator(
        &mut self,
        platform: Platform,
        validator: Arc<dyn PlatformValidator>,
    ) {
        self.adapters.insert(platform, validator);
    }

    /// Remove platform validator
    pub fn remove_platform_validator(
        &mut self,
        platform: &Platform,
    ) -> Option<Arc<dyn PlatformValidator>> {
        self.adapters.remove(platform)
    }

    /// Clear validation cache
    pub async fn clear_cache(&self) -> Result<()> {
        let mut cache = self.cache.write().await;
        cache.results.clear();
        cache.platform_data.clear();
        Ok(())
    }
}

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

/// VIBE-specific errors
/// Result type for VIBE operations.
pub type Result<T> = std::result::Result<T, VIBEError>;

#[derive(Debug, thiserror::Error)]
pub enum VIBEError {
    #[error("Invalid protocol: {0}")]
    InvalidProtocol(String),

    #[error("Validation failed: {0}")]
    ValidationError(String),

    #[error("No validations performed: {0}")]
    NoValidations(String),

    #[error("Platform error: {0}")]
    PlatformError(String),

    #[error("Cache error: {0}")]
    CacheError(String),

    #[error("Configuration error: {0}")]
    ConfigurationError(String),

    #[error("Performance error: {0}")]
    PerformanceError(String),

    #[error("Internal error: {0}")]
    Internal(String),

    #[error("Adapter error: {0}")]
    AdapterError(String),

    #[error("Benchmark error: {0}")]
    BenchmarkError(String),

    #[error("Scoring error: {0}")]
    ScoringError(String),

    #[error("Proof ledger error: {0}")]
    ProofLedgerError(#[from] crate::verification::proof_ledger::ProofLedgerError),

    #[error("ReasonKit error: {0}")]
    ReasonKitError(#[from] crate::error::Error),
}

/// Validation status enumeration
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ValidationStatus {
    Passed,
    Failed,
    Warning,
    Pending,
}

/// Confidence interval for validation scores
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfidenceInterval {
    pub lower: f32,
    pub upper: f32,
    pub confidence_level: f32,
    pub sample_size: usize,
}

/// Recommendation for protocol improvement
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Recommendation {
    pub priority: Priority,
    pub category: RecommendationCategory,
    pub title: String,
    pub description: String,
    pub actionable_steps: Vec<String>,
}

/// Priority levels for recommendations
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Priority {
    Low,
    Medium,
    High,
    Critical,
}

/// Recommendation categories
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RecommendationCategory {
    Overall,
    PlatformSpecific,
    Performance,
    Security,
    UserExperience,
    Logic,
}

/// Validation issue detected during platform validation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidationIssue {
    pub platform: Platform,
    pub severity: Severity,
    pub category: IssueCategory,
    pub description: String,
    pub location: Option<String>,
    pub suggestion: Option<String>,
}

/// Issue severity levels
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Severity {
    Low,
    Medium,
    High,
    Critical,
}

/// Issue categories for classification
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum IssueCategory {
    LogicError,
    PerformanceIssue,
    SecurityVulnerability,
    UIUXIssue,
    CompatibilityProblem,
    ResourceUsage,
    ErrorHandling,
    Documentation,
}

/// Result from individual platform validation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlatformValidationResult {
    pub platform: Platform,
    pub score: f32,
    pub status: ValidationStatus,
    pub issues: Vec<ValidationIssue>,
    pub performance_metrics: PlatformPerformanceMetrics,
    pub recommendations: Vec<String>,
}

/// Platform-specific performance metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlatformPerformanceMetrics {
    pub average_response_time_ms: u64,
    pub memory_usage_mb: u64,
    pub cpu_usage_percent: f32,
    pub error_rate_percent: f32,
    pub throughput_requests_per_second: f32,
}

use std::collections::VecDeque;

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

    #[tokio::test]
    async fn test_vibe_engine_creation() {
        let engine = VIBEEngine::new();
        let stats = engine.get_statistics().await.unwrap();
        assert_eq!(stats.total_validations, 0);
    }

    #[test]
    fn test_protocol_id_generation() {
        let engine = VIBEEngine::new();
        let id1 = engine.generate_protocol_id("test protocol");
        let id2 = engine.generate_protocol_id("test protocol");
        assert_eq!(id1, id2); // Same content should generate same ID

        let id3 = engine.generate_protocol_id("different protocol");
        assert_ne!(id1, id3); // Different content should generate different ID
    }

    #[test]
    fn test_overall_score_calculation() {
        let engine = VIBEEngine::new();
        let criteria = ValidationCriteria {
            logical_consistency: true,
            practical_applicability: true,
            platform_compatibility: true,
            performance_requirements: false,
            security_considerations: false,
            user_experience: false,
            custom_metrics: HashMap::new(),
        };

        let scores = vec![80.0, 75.0, 90.0];
        let score = engine.calculate_overall_score(&scores, &criteria).unwrap();

        assert!(score >= 0.0);
        assert!(score <= 100.0);
        assert!(score < 90.0); // Should be adjusted by criteria
    }
}