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
//! Interleaved Thinking System for MiniMax M2
//!
//! Implements M2's Interleaved Thinking Protocol for multi-step reasoning
//! with cross-validation and enhanced thinking patterns.

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

/// Interleaved thinking step with cross-validation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InterleavedStep {
    pub step_id: String,
    pub description: String,
    pub reasoning_chain: Vec<ReasoningNode>,
    pub cross_validation_passed: bool,
    pub confidence: f64,
    pub validation_results: Vec<ValidationResult>,
    pub dependencies: Vec<String>,
    pub estimated_duration_ms: u32,
    pub actual_duration_ms: Option<u32>,
}

/// Individual reasoning node in the chain
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReasoningNode {
    pub node_id: String,
    pub content: String,
    pub reasoning_type: ReasoningType,
    pub confidence: f64,
    pub supporting_evidence: Vec<Evidence>,
    pub next_steps: Vec<String>,
}

/// Types of reasoning operations
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum ReasoningType {
    Deductive,
    Inductive,
    Abductive,
    Analogical,
    FirstPrinciples,
    Counterfactual,
    Synthesis,
    Analysis,
    Evaluation,
    Creative,
}

/// Evidence supporting a reasoning step
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Evidence {
    pub evidence_id: String,
    pub source: EvidenceSource,
    pub reliability: f64,
    pub relevance: f64,
    pub content: String,
}

/// Source of evidence
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum EvidenceSource {
    External(String),
    Internal(InternalSource),
    Synthetic(SyntheticSource),
}

/// Internal knowledge source
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum InternalSource {
    PreviousStep(String),
    CachedResult(String),
    UserInput,
    SystemContext,
}

/// Synthetic evidence from reasoning
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SyntheticSource {
    LogicalInference,
    PatternRecognition,
    Heuristic,
    Assumption,
}

/// Cross-validation result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidationResult {
    pub validation_id: String,
    pub validation_type: ValidationType,
    pub passed: bool,
    pub confidence_score: f64,
    pub details: String,
    pub recommendations: Vec<String>,
}

/// Types of validation checks
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ValidationType {
    LogicalConsistency,
    FactCheck,
    Coherence,
    Completeness,
    Plausibility,
    CrossReference,
    ConstraintCompliance,
}

/// Cross-validation engine
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CrossValidation {
    pub validation_id: String,
    pub primary_step: String,
    pub validating_steps: Vec<String>,
    pub validation_rules: Vec<ValidationRule>,
    pub result: CrossValidationResult,
}

/// Result of cross-validation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CrossValidationResult {
    pub overall_passed: bool,
    pub confidence_score: f64,
    pub discrepancies: Vec<Discrepancy>,
    pub agreement_metrics: AgreementMetrics,
}

/// Discrepancy found during validation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Discrepancy {
    pub discrepancy_id: String,
    pub step_involved: String,
    pub description: String,
    pub severity: DiscrepancySeverity,
    pub suggested_resolution: Option<String>,
}

/// Severity of discrepancy
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DiscrepancySeverity {
    Low,
    Medium,
    High,
    Critical,
}

/// Agreement metrics between validating steps
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgreementMetrics {
    pub semantic_similarity: f64,
    pub logical_consistency: f64,
    pub factual_alignment: f64,
    pub confidence_convergence: f64,
}

/// Thinking pattern definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThinkingPattern {
    pub pattern_id: String,
    pub name: String,
    pub description: String,
    pub pattern_type: PatternType,
    pub steps: Vec<PatternStep>,
    pub validation_rules: Vec<ValidationRule>,
    pub optimization_params: OptimizationParameters,
}

/// Types of thinking patterns
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PatternType {
    Linear,
    Branching,
    Iterative,
    Parallel,
    Hierarchical,
    Cyclical,
    Adaptive,
}

/// Individual step in a thinking pattern
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PatternStep {
    pub step_id: String,
    pub step_type: PatternStepType,
    pub description: String,
    pub prerequisites: Vec<String>,
    pub outputs: Vec<StepOutput>,
    pub validation_criteria: Vec<ValidationCriterion>,
}

/// Types of pattern steps
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PatternStepType {
    InputProcessing,
    Reasoning,
    Validation,
    Synthesis,
    Decision,
    OutputGeneration,
    Analysis,
    Evaluation,
}

/// Output specification for a pattern step
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StepOutput {
    pub output_id: String,
    pub content_type: OutputContentType,
    pub format: OutputFormat,
    pub validation_required: bool,
}

/// Content types for outputs
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum OutputContentType {
    Text,
    Structured,
    Numerical,
    Logical,
    Creative,
    Analytical,
}

/// Output format specifications
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum OutputFormat {
    PlainText,
    JSON,
    YAML,
    Markdown,
    Table,
    Graph,
}

/// Validation criterion for step outputs
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidationCriterion {
    pub criterion_id: String,
    pub check_type: CheckType,
    pub threshold: f64,
    pub description: String,
}

/// Types of validation checks
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CheckType {
    MinimumLength,
    MaximumLength,
    ConfidenceThreshold,
    LogicalConsistency,
    FactualAccuracy,
    Completeness,
}

/// Multi-step reasoning engine
pub struct MultiStepReasoning {
    pub reasoning_id: String,
    pub pattern: ThinkingPattern,
    pub current_step_index: usize,
    pub completed_steps: Vec<InterleavedStep>,
    pub pending_steps: Vec<PatternStep>,
    pub cross_validations: Vec<CrossValidation>,
    pub state: ReasoningState,
}

/// Current state of reasoning process
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReasoningState {
    pub status: ReasoningStatus,
    pub progress: f64,
    pub confidence: f64,
    pub errors: Vec<ReasoningError>,
    pub warnings: Vec<ReasoningWarning>,
}

/// Status of reasoning process
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ReasoningStatus {
    Initializing,
    InProgress,
    Validating,
    Completing,
    Completed,
    Failed,
    Paused,
}

/// Error during reasoning process
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReasoningError {
    pub error_id: String,
    pub step_id: Option<String>,
    pub error_type: ErrorType,
    pub message: String,
    pub severity: ErrorSeverity,
    pub suggested_action: Option<String>,
}

/// Error types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ErrorType {
    ValidationFailure,
    DependencyNotMet,
    Timeout,
    ResourceExhaustion,
    LogicError,
    DataInconsistency,
}

/// Error severity levels
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ErrorSeverity {
    Info,
    Warning,
    Error,
    Critical,
}

/// Warning during reasoning process
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReasoningWarning {
    pub warning_id: String,
    pub step_id: Option<String>,
    pub message: String,
    pub impact: WarningImpact,
}

/// Impact of warning
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum WarningImpact {
    Low,
    Medium,
    High,
}

/// Validation rule for patterns
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidationRule {
    pub rule_id: String,
    pub rule_type: RuleType,
    pub parameters: HashMap<String, serde_json::Value>,
    pub description: String,
}

/// Types of validation rules
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RuleType {
    SequenceConstraint,
    ResourceLimit,
    QualityThreshold,
    DependencyCheck,
    ConsistencyRule,
}

/// Optimization parameters for thinking patterns
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptimizationParameters {
    pub max_iterations: Option<u32>,
    pub confidence_threshold: f64,
    pub time_limit_ms: Option<u32>,
    pub token_limit: Option<u32>,
    pub parallelization_level: u32,
}

/// Interleaved thinking protocol
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InterleavedProtocol {
    pub protocol_id: String,
    pub name: String,
    pub version: String,
    pub description: String,
    pub patterns: Vec<ThinkingPattern>,
    pub default_pattern: String,
    pub optimization_config: ProtocolOptimization,
}

/// Protocol-level optimization settings
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProtocolOptimization {
    pub auto_validation: bool,
    pub cross_validation_enabled: bool,
    pub parallel_processing: bool,
    pub adaptive_patterns: bool,
    pub performance_target: f64,
    pub cost_optimization: bool,
}

/// Result of interleaved thinking process
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InterleavedResult {
    pub result_id: String,
    pub protocol_id: String,
    pub pattern_used: String,
    pub status: InterleavedStatus,
    pub final_confidence: f64,
    pub steps_completed: Vec<InterleavedStep>,
    pub cross_validations_performed: Vec<CrossValidation>,
    pub performance_metrics: InterleavedPerformance,
    pub recommendations: Vec<String>,
}

/// Status of interleaved thinking result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum InterleavedStatus {
    Success,
    PartialSuccess,
    ValidationFailed,
    Timeout,
    ResourceExceeded,
    Incomplete,
}

/// Performance metrics for interleaved thinking
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InterleavedPerformance {
    pub total_time_ms: u64,
    pub token_count: u32,
    pub steps_executed: u32,
    pub validations_passed: u32,
    pub validations_failed: u32,
    pub cross_validation_score: f64,
    pub efficiency_score: f64,
}

impl MultiStepReasoning {
    /// Create new multi-step reasoning engine
    pub fn new(pattern: ThinkingPattern) -> Self {
        Self {
            reasoning_id: uuid::Uuid::new_v4().to_string(),
            pattern,
            current_step_index: 0,
            completed_steps: Vec::new(),
            pending_steps: Vec::new(),
            cross_validations: Vec::new(),
            state: ReasoningState {
                status: ReasoningStatus::Initializing,
                progress: 0.0,
                confidence: 0.0,
                errors: Vec::new(),
                warnings: Vec::new(),
            },
        }
    }

    /// Execute the complete reasoning process
    pub async fn execute(&mut self, input: &str) -> Result<InterleavedResult, crate::error::Error> {
        self.state.status = ReasoningStatus::InProgress;

        // Initialize pattern steps
        self.pending_steps = self.pattern.steps.clone();

        // Execute steps with interleaved validation
        while !self.pending_steps.is_empty() && self.current_step_index < self.pattern.steps.len() {
            let current_step = &self.pattern.steps[self.current_step_index];

            // Execute step
            let step_result = self.execute_step(current_step, input).await?;
            self.completed_steps.push(step_result);

            // Perform cross-validation if enabled
            if self.should_cross_validate() {
                self.perform_cross_validation().await?;
            }

            self.current_step_index += 1;
            self.update_progress();
        }

        // Final validation and synthesis
        let final_result = self.synthesize_results().await?;
        self.state.status = ReasoningStatus::Completed;

        Ok(final_result)
    }

    /// Execute individual step
    async fn execute_step(
        &self,
        step: &PatternStep,
        input: &str,
    ) -> Result<InterleavedStep, crate::error::Error> {
        let start_time = std::time::Instant::now();

        // Check prerequisites
        for prereq in &step.prerequisites {
            if !self.has_completed_step(prereq) {
                return Err(crate::error::Error::Validation(format!(
                    "Prerequisite step '{}' not completed",
                    prereq
                )));
            }
        }

        // Generate reasoning nodes based on step type
        let reasoning_chain = self.generate_reasoning_chain(step, input).await?;

        // Validate step output
        let validation_results = self.validate_step_output(step, &reasoning_chain).await?;

        let actual_duration = start_time.elapsed().as_millis() as u32;

        Ok(InterleavedStep {
            step_id: step.step_id.clone(),
            description: step.description.clone(),
            reasoning_chain,
            cross_validation_passed: validation_results.iter().all(|r| r.passed),
            confidence: self.calculate_step_confidence(&validation_results),
            validation_results,
            dependencies: step.prerequisites.clone(),
            estimated_duration_ms: 100, // Default estimate
            actual_duration_ms: Some(actual_duration),
        })
    }

    /// Generate reasoning chain for a step
    async fn generate_reasoning_chain(
        &self,
        step: &PatternStep,
        input: &str,
    ) -> Result<Vec<ReasoningNode>, crate::error::Error> {
        let mut chain = Vec::new();

        match step.step_type {
            PatternStepType::InputProcessing => {
                chain.push(ReasoningNode {
                    node_id: uuid::Uuid::new_v4().to_string(),
                    content: format!("Processing input: {}", input),
                    reasoning_type: ReasoningType::Analysis,
                    confidence: 0.9,
                    supporting_evidence: vec![],
                    next_steps: vec![],
                });
            }
            PatternStepType::Reasoning => {
                // Generate reasoning based on input and step context
                let node = ReasoningNode {
                    node_id: uuid::Uuid::new_v4().to_string(),
                    content: self.generate_reasoning_content(step, input).await?,
                    reasoning_type: ReasoningType::Deductive,
                    confidence: 0.8,
                    supporting_evidence: vec![],
                    next_steps: vec![],
                };
                chain.push(node);
            }
            PatternStepType::Validation => {
                chain.push(ReasoningNode {
                    node_id: uuid::Uuid::new_v4().to_string(),
                    content: "Performing validation checks".to_string(),
                    reasoning_type: ReasoningType::Evaluation,
                    confidence: 0.85,
                    supporting_evidence: vec![],
                    next_steps: vec![],
                });
            }
            _ => {
                chain.push(ReasoningNode {
                    node_id: uuid::Uuid::new_v4().to_string(),
                    content: format!("Executing {} step", step.step_type.clone() as i32),
                    reasoning_type: ReasoningType::Analysis,
                    confidence: 0.7,
                    supporting_evidence: vec![],
                    next_steps: vec![],
                });
            }
        }

        Ok(chain)
    }

    /// Helper methods
    fn should_cross_validate(&self) -> bool {
        self.pattern.optimization_params.parallelization_level > 1
    }

    fn has_completed_step(&self, step_id: &str) -> bool {
        self.completed_steps
            .iter()
            .any(|step| step.step_id == step_id)
    }

    fn calculate_step_confidence(&self, validations: &[ValidationResult]) -> f64 {
        if validations.is_empty() {
            return 0.7; // Default confidence
        }

        validations.iter().map(|v| v.confidence_score).sum::<f64>() / validations.len() as f64
    }

    async fn generate_reasoning_content(
        &self,
        step: &PatternStep,
        input: &str,
    ) -> Result<String, crate::error::Error> {
        // This would integrate with actual reasoning logic
        Ok(format!(
            "Reasoning for step '{}' based on input: {}",
            step.step_id, input
        ))
    }

    async fn validate_step_output(
        &self,
        step: &PatternStep,
        chain: &[ReasoningNode],
    ) -> Result<Vec<ValidationResult>, crate::error::Error> {
        let mut validations = Vec::new();

        for criterion in &step.validation_criteria {
            let validation = match criterion.check_type {
                CheckType::MinimumLength => {
                    let content = chain
                        .iter()
                        .map(|n| n.content.as_str())
                        .collect::<Vec<_>>()
                        .join(" ");
                    let passed = content.len() >= (criterion.threshold as usize);
                    ValidationResult {
                        validation_id: uuid::Uuid::new_v4().to_string(),
                        validation_type: ValidationType::Completeness,
                        passed,
                        confidence_score: if passed { 0.9 } else { 0.3 },
                        details: format!(
                            "Content length: {} vs threshold: {}",
                            content.len(),
                            criterion.threshold
                        ),
                        recommendations: if !passed {
                            vec!["Increase detail level".to_string()]
                        } else {
                            vec![]
                        },
                    }
                }
                CheckType::ConfidenceThreshold => {
                    let avg_confidence =
                        chain.iter().map(|n| n.confidence).sum::<f64>() / chain.len() as f64;
                    let passed = avg_confidence >= criterion.threshold;
                    ValidationResult {
                        validation_id: uuid::Uuid::new_v4().to_string(),
                        validation_type: ValidationType::Coherence,
                        passed,
                        confidence_score: avg_confidence,
                        details: format!(
                            "Average confidence: {} vs threshold: {}",
                            avg_confidence, criterion.threshold
                        ),
                        recommendations: if !passed {
                            vec!["Improve confidence through better evidence".to_string()]
                        } else {
                            vec![]
                        },
                    }
                }
                _ => ValidationResult {
                    validation_id: uuid::Uuid::new_v4().to_string(),
                    validation_type: ValidationType::Completeness,
                    passed: true,
                    confidence_score: 0.8,
                    details: "Default validation passed".to_string(),
                    recommendations: vec![],
                },
            };
            validations.push(validation);
        }

        Ok(validations)
    }

    async fn perform_cross_validation(&mut self) -> Result<(), crate::error::Error> {
        // Implement cross-validation logic
        // This would compare results across multiple reasoning paths
        Ok(())
    }

    fn update_progress(&mut self) {
        self.state.progress =
            (self.completed_steps.len() as f64 / self.pattern.steps.len() as f64) * 100.0;

        // Update overall confidence
        if !self.completed_steps.is_empty() {
            self.state.confidence = self
                .completed_steps
                .iter()
                .map(|step| step.confidence)
                .sum::<f64>()
                / self.completed_steps.len() as f64;
        }
    }

    async fn synthesize_results(&self) -> Result<InterleavedResult, crate::error::Error> {
        let total_time = self
            .completed_steps
            .iter()
            .filter_map(|step| step.actual_duration_ms)
            .sum::<u32>() as u64;

        let validations_passed = self
            .completed_steps
            .iter()
            .flat_map(|step| &step.validation_results)
            .filter(|v| v.passed)
            .count() as u32;

        let validations_failed = self
            .completed_steps
            .iter()
            .flat_map(|step| &step.validation_results)
            .filter(|v| !v.passed)
            .count() as u32;

        let cross_validation_score = if !self.cross_validations.is_empty() {
            self.cross_validations
                .iter()
                .map(|cv| cv.result.confidence_score)
                .sum::<f64>()
                / self.cross_validations.len() as f64
        } else {
            0.8 // Default if no cross-validations
        };

        Ok(InterleavedResult {
            result_id: uuid::Uuid::new_v4().to_string(),
            protocol_id: self.pattern.pattern_id.clone(),
            pattern_used: self.pattern.name.clone(),
            status: if validations_failed > 0 {
                InterleavedStatus::PartialSuccess
            } else {
                InterleavedStatus::Success
            },
            final_confidence: self.state.confidence,
            steps_completed: self.completed_steps.clone(),
            cross_validations_performed: self.cross_validations.clone(),
            performance_metrics: InterleavedPerformance {
                total_time_ms: total_time,
                token_count: (total_time / 10) as u32, // Rough estimate
                steps_executed: self.completed_steps.len() as u32,
                validations_passed,
                validations_failed,
                cross_validation_score,
                efficiency_score: self.calculate_efficiency_score(
                    total_time,
                    validations_passed,
                    validations_failed,
                ),
            },
            recommendations: self.generate_recommendations(),
        })
    }

    fn calculate_efficiency_score(&self, time_ms: u64, passed: u32, failed: u32) -> f64 {
        let total_validations = passed + failed;
        if total_validations == 0 {
            return 0.5;
        }

        let success_rate = passed as f64 / total_validations as f64;
        let time_factor = (5000.0 / time_ms as f64).min(2.0); // Target 5s execution

        (success_rate * 0.7 + (time_factor - 1.0) * 0.3).clamp(0.0, 1.0)
    }

    fn generate_recommendations(&self) -> Vec<String> {
        let mut recommendations = Vec::new();

        if self.state.confidence < 0.8 {
            recommendations
                .push("Consider additional validation steps to improve confidence".to_string());
        }

        if self.completed_steps.len() < self.pattern.steps.len() {
            recommendations.push("Some pattern steps were not completed".to_string());
        }

        if let Some(error) = self.state.errors.first() {
            recommendations.push(format!("Address error: {}", error.message));
        }

        recommendations
    }
}

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

    #[test]
    fn test_interleaved_step_creation() {
        let step = InterleavedStep {
            step_id: "test_step".to_string(),
            description: "Test step".to_string(),
            reasoning_chain: vec![],
            cross_validation_passed: false,
            confidence: 0.8,
            validation_results: vec![],
            dependencies: vec![],
            estimated_duration_ms: 100,
            actual_duration_ms: None,
        };

        assert_eq!(step.step_id, "test_step");
        assert_eq!(step.confidence, 0.8);
    }

    #[test]
    fn test_thinking_pattern_creation() {
        let pattern = ThinkingPattern {
            pattern_id: "test_pattern".to_string(),
            name: "Test Pattern".to_string(),
            description: "A test thinking pattern".to_string(),
            pattern_type: PatternType::Linear,
            steps: vec![],
            validation_rules: vec![],
            optimization_params: OptimizationParameters {
                max_iterations: Some(10),
                confidence_threshold: 0.8,
                time_limit_ms: Some(5000),
                token_limit: Some(2000),
                parallelization_level: 1,
            },
        };

        assert_eq!(pattern.pattern_id, "test_pattern");
        assert_eq!(pattern.optimization_params.confidence_threshold, 0.8);
    }
}