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
//! # Multi-Language Code Intelligence Enhancement
//!
//! Leverages MiniMax M2's exceptional 9+ language mastery and superior 72.5% SWE-bench performance
//! to provide advanced code understanding, optimization, and analysis across multiple programming languages.
//!
//! ## Core Features
//!
//! - **Multi-Language Mastery**: Rust (primary), Java, Golang, C++, Kotlin, Objective-C, TypeScript, JavaScript, Python
//! - **SWE-bench Excellence**: 72.5% SWE-bench Multilingual score performance
//! - **Real-world Coding Tasks**: Test case generation, code optimization, code review
//! - **Cross-Framework Compatibility**: Claude Code, Cline, Kilo Code, Droid, Roo Code, BlackBox AI
//! - **Rust-First Enhancement**: Optimized for ReasonKit's Rust-based architecture
//! - **Interleaved Thinking Protocol**: Integration with M2's advanced reasoning capabilities

pub mod analyzer;
pub mod bug_detector;
pub mod m2_integration;
pub mod optimizer;
pub mod parser;
pub mod test_generator;

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use tracing::info;

pub use analyzer::CodeAnalyzer;
pub use bug_detector::{BugAnalysisContext, BugDetectorEngine, EnhancedBugFinding};
pub use m2_integration::{M2CodeInsight, M2CodeIntelligenceConnector, M2InsightType};
pub use optimizer::CodeOptimizer;
pub use parser::*;
pub use test_generator::{TestGeneratorEngine, TestSuggestion};

/// Programming languages supported by the system
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ProgrammingLanguage {
    Rust,
    Java,
    Golang,
    Cpp,
    Kotlin,
    ObjectiveC,
    TypeScript,
    JavaScript,
    Python,
}

/// Main code intelligence engine that orchestrates all components
pub struct CodeIntelligenceEngine {
    parser: CodeParser,
    analyzer: CodeAnalyzer,
    optimizer: CodeOptimizer,
    bug_detector: BugDetectorEngine,
    test_generator: TestGeneratorEngine,
    m2_integration: M2CodeIntelligenceConnector,
}

impl CodeIntelligenceEngine {
    /// Create new code intelligence engine
    pub async fn new() -> Result<Self, crate::error::Error> {
        let parser = CodeParser::new();
        let analyzer = CodeAnalyzer::new();
        let optimizer = CodeOptimizer::new();
        let bug_detector = BugDetectorEngine::new();
        let test_generator = TestGeneratorEngine::new();
        let m2_integration = M2CodeIntelligenceConnector::new().await?;

        Ok(Self {
            parser,
            analyzer,
            optimizer,
            bug_detector,
            test_generator,
            m2_integration,
        })
    }

    /// Perform comprehensive code analysis
    pub async fn analyze_code(
        &mut self,
        code: &str,
        language: ProgrammingLanguage,
        context: Option<CodeAnalysisContext>,
    ) -> Result<ComprehensiveCodeAnalysis, crate::error::Error> {
        info!(
            "Starting comprehensive code analysis for {:?} ({} chars)",
            language,
            code.len()
        );

        // Parse code into AST
        let ast = self.parser.parse(code, language).await?;

        // Perform static analysis
        let analysis_result = self
            .analyzer
            .analyze(&ast, language, code, context.clone())?;

        // Detect bugs and issues
        let bug_findings = self
            .bug_detector
            .detect_bugs_comprehensive(&ast, language, code, &BugAnalysisContext::default())
            .await?;
        let bug_findings: Vec<BugFinding> = bug_findings.into_iter().map(|f| f.bug).collect();

        // Generate optimization suggestions
        let optimizations = self
            .optimizer
            .generate_optimizations(&ast, language, code, &analysis_result)
            .await?;

        // Generate test cases
        let test_suggestions = self
            .test_generator
            .generate_tests(&ast, language, &analysis_result)
            .await?;

        // Integrate with M2 for advanced reasoning
        let m2_insights = self
            .m2_integration
            .enhance_analysis(&analysis_result, &ast, language)
            .await?;

        // Calculate overall score
        let overall_score = self.calculate_overall_score(&analysis_result, &bug_findings);

        Ok(ComprehensiveCodeAnalysis {
            language,
            ast,
            analysis_result,
            bug_findings: bug_findings.clone(),
            optimization_suggestions: optimizations,
            test_suggestions,
            m2_insights,
            overall_score,
        })
    }

    /// Calculate overall code quality score
    fn calculate_overall_score(&self, analysis: &CodeAnalysisResult, bugs: &[BugFinding]) -> f64 {
        let quality_score = analysis.complexity_score;
        let bug_penalty = bugs
            .iter()
            .map(|b| b.severity.as_penalty_score())
            .sum::<f64>();
        (quality_score - bug_penalty).clamp(0.0, 1.0)
    }
}

/// Comprehensive code analysis result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComprehensiveCodeAnalysis {
    pub language: ProgrammingLanguage,
    pub ast: UnifiedAST,
    pub analysis_result: CodeAnalysisResult,
    pub bug_findings: Vec<BugFinding>,
    pub optimization_suggestions: Vec<OptimizationSuggestion>,
    pub test_suggestions: Vec<TestSuggestion>,
    pub m2_insights: Vec<M2CodeInsight>,
    pub overall_score: f64,
}

/// Code analysis result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CodeAnalysisResult {
    pub language: ProgrammingLanguage,
    pub complexity_score: f64,
    pub quality_metrics: CodeQualityMetrics,
    pub optimization_suggestions: Vec<OptimizationSuggestion>,
    pub bug_findings: Vec<BugFinding>,
    pub test_suggestions: Vec<TestSuggestion>,
    pub performance_metrics: PerformanceMetrics,
    pub cross_language_insights: Vec<CrossLanguageInsight>,
}

/// Bug finding result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BugFinding {
    pub severity: BugSeverity,
    pub category: BugCategory,
    pub description: String,
    pub location: CodeLocation,
    pub confidence: f64,
    pub suggested_fix: Option<String>,
}

/// Optimization suggestion
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptimizationSuggestion {
    pub category: OptimizationCategory,
    pub priority: SuggestionPriority,
    pub description: String,
    pub impact: f64,
    pub effort: f64,
    pub code_example: Option<String>,
}

/// Test suite produced by generators
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TestSuite {
    pub language: ProgrammingLanguage,
    pub framework: Option<TestFramework>,
    pub test_cases: Vec<TestCase>,
}

/// Code analysis context
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CodeAnalysisContext {
    pub file_path: Option<String>,
    pub project_type: Option<String>,
    pub performance_critical: bool,
    pub security_critical: bool,
    pub target_language: Option<ProgrammingLanguage>,
}

/// Extension trait for bug severity
trait BugSeverityExt {
    fn as_penalty_score(&self) -> f64;
}

impl BugSeverityExt for BugSeverity {
    fn as_penalty_score(&self) -> f64 {
        match self {
            BugSeverity::Critical => 0.3,
            BugSeverity::High => 0.2,
            BugSeverity::Medium => 0.1,
            BugSeverity::Low => 0.05,
            BugSeverity::Info => 0.0,
        }
    }
}

/// Main code parser
pub struct CodeParser {
    language_parsers: HashMap<ProgrammingLanguage, Arc<dyn LanguageParser + Send + Sync>>,
}

impl CodeParser {
    pub fn new() -> Self {
        let mut language_parsers: HashMap<
            ProgrammingLanguage,
            Arc<dyn LanguageParser + Send + Sync>,
        > = HashMap::new();

        // Initialize language-specific parsers
        language_parsers.insert(ProgrammingLanguage::Rust, Arc::new(RustParser::new()));
        language_parsers.insert(ProgrammingLanguage::Java, Arc::new(JavaParser::new()));
        language_parsers.insert(ProgrammingLanguage::Golang, Arc::new(GolangParser::new()));
        language_parsers.insert(ProgrammingLanguage::Cpp, Arc::new(CppParser::new()));
        language_parsers.insert(ProgrammingLanguage::Kotlin, Arc::new(KotlinParser::new()));
        language_parsers.insert(
            ProgrammingLanguage::ObjectiveC,
            Arc::new(ObjectiveCParser::new()),
        );
        language_parsers.insert(
            ProgrammingLanguage::TypeScript,
            Arc::new(TypeScriptParser::new()),
        );
        language_parsers.insert(
            ProgrammingLanguage::JavaScript,
            Arc::new(JavaScriptParser::new()),
        );
        language_parsers.insert(ProgrammingLanguage::Python, Arc::new(PythonParser::new()));

        Self { language_parsers }
    }

    pub async fn parse(
        &self,
        code: &str,
        language: ProgrammingLanguage,
    ) -> Result<UnifiedAST, crate::error::Error> {
        if let Some(parser) = self.language_parsers.get(&language) {
            parser.parse(code).await
        } else {
            Err(crate::error::Error::CodeIntelligence(format!(
                "Language {:?} is not supported",
                language
            )))
        }
    }

    /// Auto-detect programming language from code content
    pub async fn detect_language(
        &self,
        code: &str,
    ) -> Result<ProgrammingLanguage, crate::error::Error> {
        let mut best_language = ProgrammingLanguage::Rust;
        let mut best_score = 0.0;

        for (language, parser) in &self.language_parsers {
            let score = parser.detect_language(code);
            if score > best_score {
                best_score = score;
                best_language = *language;
            }
        }

        if best_score >= 0.3 {
            Ok(best_language)
        } else {
            Err(crate::error::Error::CodeIntelligence(
                "Failed to detect programming language".to_string(),
            ))
        }
    }
}

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

/// Error types for code intelligence
#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("Language {0:?} is not supported")]
    LanguageNotSupported(ProgrammingLanguage),
    #[error("Failed to detect programming language")]
    LanguageDetectionFailed,
    #[error("Parse error: {0}")]
    ParseError(String),
    #[error("Analysis error: {0}")]
    AnalysisError(String),
    #[error("M2 integration error: {0}")]
    M2IntegrationError(String),
}

/// Code quality metrics structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CodeQualityMetrics {
    pub maintainability_index: f64,
    pub cyclomatic_complexity: f64,
    pub code_coverage_potential: f64,
    pub documentation_quality: f64,
    pub testability_score: f64,
    pub security_score: f64,
    pub performance_score: f64,
}

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

/// Bug categories
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BugCategory {
    Security,
    Logic,
    Type,
    Memory,
    Performance,
    Accessibility,
    Concurrency,
    NullPointer,
    ResourceLeak,
    Deadlock,
    RaceCondition,
}

/// Code location information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CodeLocation {
    pub file_path: String,
    pub line_number: Option<u32>,
    pub column_number: Option<u32>,
    pub function_name: Option<String>,
}

/// Optimization categories
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum OptimizationCategory {
    Performance,
    Memory,
    Security,
    Maintainability,
    Testing,
}

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

/// Test types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TestType {
    Unit,
    Integration,
    EdgeCase,
    Performance,
    Security,
    Property,
}

/// Test frameworks
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TestFramework {
    JUnit,
    TestNG,
    PyTest,
    Jest,
    Mocha,
    NUnit,
    GoTest,
    RustTest,
    SwiftXCTest,
    KotlinTest,
}

/// Additional cross-language types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CrossLanguageInsight {
    pub source_language: ProgrammingLanguage,
    pub target_language: ProgrammingLanguage,
    pub insight_type: CrossLanguageInsightType,
    pub description: String,
    pub potential_improvements: Vec<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CrossLanguageInsightType {
    BestPractice,
    Pattern,
    Performance,
    Security,
}

/// Performance metrics structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceMetrics {
    pub execution_time_estimate: f64,
    pub memory_usage_estimate: f64,
    pub cpu_intensity: f64,
    pub scalability_score: f64,
    pub bottlenecks: Vec<PerformanceBottleneck>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceBottleneck {
    pub location: CodeLocation,
    pub impact: f64,
    pub description: String,
    pub potential_optimization: String,
}

/// Cross-language analysis result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CrossLanguageAnalysisResult {
    pub language_analyses: HashMap<ProgrammingLanguage, CodeAnalysisResult>,
    pub common_patterns: Vec<CrossLanguagePattern>,
    pub language_comparisons: Vec<LanguageComparison>,
    pub best_practices: Vec<CrossLanguageBestPractice>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CrossLanguagePattern {
    pub pattern_name: String,
    pub languages: Vec<ProgrammingLanguage>,
    pub description: String,
    pub implementations: HashMap<ProgrammingLanguage, String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LanguageComparison {
    pub language1: ProgrammingLanguage,
    pub language2: ProgrammingLanguage,
    pub similarities: Vec<String>,
    pub differences: Vec<String>,
    pub recommendations: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CrossLanguageBestPractice {
    pub practice_name: String,
    pub applicable_languages: Vec<ProgrammingLanguage>,
    pub description: String,
    pub examples: HashMap<ProgrammingLanguage, String>,
    pub benefits: Vec<String>,
}

/// Test case structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TestCase {
    pub test_type: TestType,
    pub description: String,
    pub test_code: String,
    pub target_function: Option<String>,
    pub dependencies: Vec<String>,
    pub expected_outcomes: Vec<String>,
}

/// Code optimization result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CodeOptimizationResult {
    pub original_code: String,
    pub optimized_code: String,
    pub improvements: Vec<OptimizationImprovement>,
    pub performance_gain_estimate: f64,
    pub maintainability_impact: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptimizationImprovement {
    pub category: OptimizationCategory,
    pub description: String,
    pub impact_score: f64,
    pub code_changes: Vec<String>,
}

/// Code change structure for optimizations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CodeChange {
    pub location: CodeLocation,
    pub original_code: String,
    pub new_code: String,
    pub description: String,
}

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

    #[tokio::test]
    async fn test_code_intelligence_engine_creation() {
        let engine = CodeIntelligenceEngine::new().await;
        assert!(engine.is_ok());
    }

    #[tokio::test]
    async fn test_rust_code_analysis() {
        let mut engine = CodeIntelligenceEngine::new().await.unwrap();
        let rust_code = r#"
        fn main() {
            let mut x = 42;
            if x > 0 {
                println!("Positive: {}", x);
            }
        }
        "#;

        let result = engine
            .analyze_code(rust_code, ProgrammingLanguage::Rust, None)
            .await;
        assert!(result.is_ok());
        let analysis = result.unwrap();
        assert_eq!(analysis.language, ProgrammingLanguage::Rust);
        assert!(analysis.overall_score >= 0.0 && analysis.overall_score <= 1.0);
    }

    #[tokio::test]
    async fn test_language_detection() {
        let parser = CodeParser::new();

        let rust_code = "fn main() { let x = 42; }";
        let detected = parser.detect_language(rust_code).await;
        assert!(detected.is_ok());
        assert_eq!(detected.unwrap(), ProgrammingLanguage::Rust);
    }
}