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
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
//! # Code Analyzer Module
//!
//! Performs deep analysis of parsed AST across multiple programming languages.
//! Leverages MiniMax M2's SWE-bench excellence for superior code understanding.
//!
//! ## Analysis Capabilities
//!
//! - **Quality Metrics**: Maintainability, complexity, testability
//! - **Pattern Detection**: Design patterns, anti-patterns, code smells
//! - **Bug Detection**: Logic errors, security vulnerabilities, performance issues
//! - **Optimization**: Performance, memory, and maintainability improvements
//! - **Cross-Language Insights**: Language-specific best practices and patterns

use crate::code_intelligence::*;
use crate::error::Error;
use crate::thinktool::ThinkToolExecutor;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use tracing::{info, instrument, warn};

/// Main code analyzer
pub struct CodeAnalyzer {
    /// Quality assessors
    quality_assessors: HashMap<ProgrammingLanguage, Box<dyn QualityAssessor + Send + Sync>>,

    /// Pattern detectors
    pattern_detectors: HashMap<ProgrammingLanguage, Box<dyn PatternDetector + Send + Sync>>,

    /// Bug detectors
    bug_detectors: HashMap<ProgrammingLanguage, Box<dyn BugDetector + Send + Sync>>,

    /// ThinkTool executor for advanced reasoning
    #[allow(dead_code)]
    thinktool_executor: Arc<ThinkToolExecutor>,
}

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

/// Code analysis engine
impl CodeAnalyzer {
    /// Create new code analyzer
    pub fn new() -> Self {
        let mut quality_assessors = HashMap::new();
        let mut pattern_detectors = HashMap::new();
        let mut bug_detectors = HashMap::new();

        // Initialize language-specific analyzers
        quality_assessors.insert(
            ProgrammingLanguage::Rust,
            Box::new(RustQualityAssessor::new()) as Box<dyn QualityAssessor + Send + Sync>,
        );
        quality_assessors.insert(
            ProgrammingLanguage::Java,
            Box::new(JavaQualityAssessor::new()) as Box<dyn QualityAssessor + Send + Sync>,
        );
        quality_assessors.insert(
            ProgrammingLanguage::Python,
            Box::new(PythonQualityAssessor::new()) as Box<dyn QualityAssessor + Send + Sync>,
        );

        pattern_detectors.insert(
            ProgrammingLanguage::Rust,
            Box::new(RustPatternDetector::new()) as Box<dyn PatternDetector + Send + Sync>,
        );
        pattern_detectors.insert(
            ProgrammingLanguage::Java,
            Box::new(JavaPatternDetector::new()) as Box<dyn PatternDetector + Send + Sync>,
        );
        pattern_detectors.insert(
            ProgrammingLanguage::Python,
            Box::new(PythonPatternDetector::new()) as Box<dyn PatternDetector + Send + Sync>,
        );

        bug_detectors.insert(
            ProgrammingLanguage::Rust,
            Box::new(RustBugDetector::new()) as Box<dyn BugDetector + Send + Sync>,
        );
        bug_detectors.insert(
            ProgrammingLanguage::Java,
            Box::new(JavaBugDetector::new()) as Box<dyn BugDetector + Send + Sync>,
        );
        bug_detectors.insert(
            ProgrammingLanguage::Python,
            Box::new(PythonBugDetector::new()) as Box<dyn BugDetector + Send + Sync>,
        );

        let thinktool_executor = Arc::new(ThinkToolExecutor::new());

        Self {
            quality_assessors,
            pattern_detectors,
            bug_detectors,
            thinktool_executor,
        }
    }

    /// Perform comprehensive code analysis
    #[instrument(skip(self, ast, code))]
    pub fn analyze(
        &self,
        ast: &UnifiedAST,
        language: ProgrammingLanguage,
        code: &str,
        context: Option<CodeAnalysisContext>,
    ) -> Result<CodeAnalysisResult, Error> {
        info!("Performing comprehensive analysis for {:?} code", language);

        // Perform quality assessment
        let quality_metrics = self.assess_quality(ast, language, context.as_ref())?;

        // Detect patterns and anti-patterns
        let _patterns = self.detect_patterns(ast, language)?;

        // Detect bugs and issues
        let bug_findings = self.detect_bugs(ast, language, code)?;

        // Generate optimization suggestions
        let optimization_suggestions =
            self.generate_optimizations(ast, language, &quality_metrics, &bug_findings)?;

        // Generate test suggestions
        let test_suggestions = self.generate_test_suggestions(ast, language, &quality_metrics)?;

        // Calculate performance metrics
        let performance_metrics =
            self.calculate_performance_metrics(ast, language, &quality_metrics)?;

        // Generate cross-language insights
        let cross_language_insights = self.generate_cross_language_insights(ast, language)?;

        // Calculate overall complexity score
        let complexity_score = self.calculate_complexity_score(ast, &quality_metrics);

        Ok(CodeAnalysisResult {
            language,
            complexity_score,
            quality_metrics,
            optimization_suggestions,
            bug_findings,
            test_suggestions,
            performance_metrics,
            cross_language_insights,
        })
    }

    /// Assess code quality
    fn assess_quality(
        &self,
        ast: &UnifiedAST,
        language: ProgrammingLanguage,
        context: Option<&CodeAnalysisContext>,
    ) -> Result<CodeQualityMetrics, Error> {
        if let Some(assessor) = self.quality_assessors.get(&language) {
            assessor.assess_quality(ast, context)
        } else {
            // Fallback to generic quality assessment
            self.assess_generic_quality(ast, context)
        }
    }

    /// Generic quality assessment fallback
    fn assess_generic_quality(
        &self,
        ast: &UnifiedAST,
        _context: Option<&CodeAnalysisContext>,
    ) -> Result<CodeQualityMetrics, Error> {
        let complexity = &ast.complexity_metrics;

        // Calculate maintainability index (simplified McCabe's method)
        let maintainability_index = self.calculate_maintainability_index(complexity);

        // Calculate documentation quality
        let documentation_quality = self.calculate_documentation_quality(ast);

        // Calculate testability score
        let testability_score = self.calculate_testability_score(ast);

        // Calculate security score
        let security_score = self.calculate_security_score(ast);

        // Calculate performance score
        let performance_score = self.calculate_performance_score(complexity);

        Ok(CodeQualityMetrics {
            maintainability_index,
            cyclomatic_complexity: complexity.cyclomatic_complexity,
            code_coverage_potential: self.calculate_coverage_potential(ast),
            documentation_quality,
            testability_score,
            security_score,
            performance_score,
        })
    }

    /// Detect patterns and anti-patterns
    fn detect_patterns(
        &self,
        ast: &UnifiedAST,
        language: ProgrammingLanguage,
    ) -> Result<Vec<PatternFinding>, Error> {
        if let Some(detector) = self.pattern_detectors.get(&language) {
            detector.detect_patterns(ast)
        } else {
            // Fallback to generic pattern detection
            self.detect_generic_patterns(ast)
        }
    }

    /// Generic pattern detection fallback
    fn detect_generic_patterns(&self, ast: &UnifiedAST) -> Result<Vec<PatternFinding>, Error> {
        let mut patterns = Vec::new();

        // Detect common anti-patterns
        if ast.complexity_metrics.cyclomatic_complexity > 10.0 {
            patterns.push(PatternFinding {
                pattern_type: PatternType::AntiPattern,
                name: "High Cyclomatic Complexity".to_string(),
                description: "Function has high cyclomatic complexity, consider refactoring"
                    .to_string(),
                severity: PatternSeverity::Medium,
                location: None,
            });
        }

        if ast.complexity_metrics.nesting_depth > 4 {
            patterns.push(PatternFinding {
                pattern_type: PatternType::AntiPattern,
                name: "Deep Nesting".to_string(),
                description: "Code has deep nesting levels, consider flattening".to_string(),
                severity: PatternSeverity::Medium,
                location: None,
            });
        }

        Ok(patterns)
    }

    /// Detect bugs and issues
    fn detect_bugs(
        &self,
        ast: &UnifiedAST,
        language: ProgrammingLanguage,
        code: &str,
    ) -> Result<Vec<BugFinding>, Error> {
        if let Some(detector) = self.bug_detectors.get(&language) {
            detector.detect_bugs(ast, code)
        } else {
            // Fallback to generic bug detection
            self.detect_generic_bugs(ast, code)
        }
    }

    /// Generic bug detection fallback
    fn detect_generic_bugs(&self, ast: &UnifiedAST, code: &str) -> Result<Vec<BugFinding>, Error> {
        let mut bugs = Vec::new();

        // Check for common security issues
        if code.contains("eval(") || code.contains("exec(") {
            bugs.push(BugFinding {
                severity: BugSeverity::High,
                category: BugCategory::Security,
                description: "Use of eval/exec can lead to code injection vulnerabilities"
                    .to_string(),
                location: CodeLocation {
                    file_path: "unknown".to_string(),
                    line_number: None,
                    column_number: None,
                    function_name: None,
                },
                confidence: 0.9,
                suggested_fix: Some(
                    "Use safer alternatives like JSON parsing or input validation".to_string(),
                ),
            });
        }

        // Check for potential null pointer issues
        if ast.variables.iter().any(|v| v.data_type.is_none()) {
            bugs.push(BugFinding {
                severity: BugSeverity::Medium,
                category: BugCategory::Type,
                description:
                    "Variables without explicit type declarations may cause runtime errors"
                        .to_string(),
                location: CodeLocation {
                    file_path: "unknown".to_string(),
                    line_number: None,
                    column_number: None,
                    function_name: None,
                },
                confidence: 0.7,
                suggested_fix: Some(
                    "Add explicit type declarations for better type safety".to_string(),
                ),
            });
        }

        Ok(bugs)
    }

    /// Generate optimization suggestions
    fn generate_optimizations(
        &self,
        _ast: &UnifiedAST,
        _language: ProgrammingLanguage,
        quality_metrics: &CodeQualityMetrics,
        bug_findings: &[BugFinding],
    ) -> Result<Vec<OptimizationSuggestion>, Error> {
        let mut suggestions = Vec::new();

        // Performance optimizations
        if quality_metrics.performance_score < 0.7 {
            suggestions.push(OptimizationSuggestion {
                category: OptimizationCategory::Performance,
                priority: SuggestionPriority::High,
                description: "Optimize performance-critical sections".to_string(),
                impact: 0.8,
                effort: 0.6,
                code_example: Some("Consider algorithmic optimizations or caching".to_string()),
            });
        }

        // Security optimizations
        let security_bugs = bug_findings
            .iter()
            .filter(|b| b.category == BugCategory::Security)
            .count();
        if security_bugs > 0 {
            suggestions.push(OptimizationSuggestion {
                category: OptimizationCategory::Security,
                priority: SuggestionPriority::Critical,
                description: format!("Address {} security vulnerabilities", security_bugs),
                impact: 0.9,
                effort: 0.7,
                code_example: Some(
                    "Implement input validation and secure coding practices".to_string(),
                ),
            });
        }

        // Maintainability optimizations
        if quality_metrics.maintainability_index < 0.7 {
            suggestions.push(OptimizationSuggestion {
                category: OptimizationCategory::Maintainability,
                priority: SuggestionPriority::Medium,
                description: "Improve code maintainability through refactoring".to_string(),
                impact: 0.6,
                effort: 0.4,
                code_example: Some(
                    "Extract methods, reduce complexity, improve naming".to_string(),
                ),
            });
        }

        // Testing optimizations
        if quality_metrics.testability_score < 0.6 {
            suggestions.push(OptimizationSuggestion {
                category: OptimizationCategory::Testing,
                priority: SuggestionPriority::High,
                description: "Improve code testability".to_string(),
                impact: 0.7,
                effort: 0.3,
                code_example: Some(
                    "Use dependency injection, reduce coupling, increase cohesion".to_string(),
                ),
            });
        }

        Ok(suggestions)
    }

    /// Generate test suggestions
    fn generate_test_suggestions(
        &self,
        ast: &UnifiedAST,
        _language: ProgrammingLanguage,
        quality_metrics: &CodeQualityMetrics,
    ) -> Result<Vec<TestSuggestion>, Error> {
        let mut suggestions = Vec::new();

        // Generate test suggestions based on functions
        for function in &ast.functions {
            suggestions.push(TestSuggestion {
                test_type: TestType::Unit,
                description: format!("Unit tests for function '{}'", function.name),
                target_function: Some(function.name.clone()),
                coverage_area: "Function behavior".to_string(),
                priority: SuggestionPriority::High,
            });

            // Add edge case testing for complex functions
            if function.complexity.cyclomatic_complexity > 5.0 {
                suggestions.push(TestSuggestion {
                    test_type: TestType::EdgeCase,
                    description: format!("Edge case tests for function '{}'", function.name),
                    target_function: Some(function.name.clone()),
                    coverage_area: "Boundary conditions".to_string(),
                    priority: SuggestionPriority::Medium,
                });
            }
        }

        // Add integration test suggestions
        if !ast.classes.is_empty() {
            suggestions.push(TestSuggestion {
                test_type: TestType::Integration,
                description: "Integration tests for class interactions".to_string(),
                target_function: None,
                coverage_area: "Class integration".to_string(),
                priority: SuggestionPriority::Medium,
            });
        }

        // Add performance test suggestions for complex code
        if quality_metrics.cyclomatic_complexity > 8.0 {
            suggestions.push(TestSuggestion {
                test_type: TestType::Performance,
                description: "Performance tests for high-complexity code".to_string(),
                target_function: None,
                coverage_area: "Performance characteristics".to_string(),
                priority: SuggestionPriority::Low,
            });
        }

        Ok(suggestions)
    }

    /// Calculate performance metrics
    fn calculate_performance_metrics(
        &self,
        ast: &UnifiedAST,
        _language: ProgrammingLanguage,
        _quality_metrics: &CodeQualityMetrics,
    ) -> Result<PerformanceMetrics, Error> {
        let complexity = &ast.complexity_metrics;

        // Estimate execution time based on complexity
        let execution_time_estimate =
            complexity.cyclomatic_complexity * complexity.lines_of_code as f64 / 100.0;

        // Estimate memory usage
        let memory_usage_estimate = complexity.lines_of_code as f64 * 0.1; // KB per line estimate

        // Calculate CPU intensity
        let cpu_intensity = (complexity.cyclomatic_complexity / 10.0).min(1.0);

        // Calculate scalability score
        let scalability_score = (1.0 - (complexity.cyclomatic_complexity / 20.0)).max(0.0);

        // Identify bottlenecks
        let mut bottlenecks = Vec::new();

        if complexity.cyclomatic_complexity > 10.0 {
            bottlenecks.push(PerformanceBottleneck {
                location: CodeLocation {
                    file_path: "unknown".to_string(),
                    line_number: None,
                    column_number: None,
                    function_name: None,
                },
                impact: 0.8,
                description: "High cyclomatic complexity may cause performance issues".to_string(),
                potential_optimization: "Consider algorithmic optimization or code simplification"
                    .to_string(),
            });
        }

        if complexity.nesting_depth > 4 {
            bottlenecks.push(PerformanceBottleneck {
                location: CodeLocation {
                    file_path: "unknown".to_string(),
                    line_number: None,
                    column_number: None,
                    function_name: None,
                },
                impact: 0.6,
                description: "Deep nesting can impact performance".to_string(),
                potential_optimization: "Flatten nested structures or use early returns"
                    .to_string(),
            });
        }

        Ok(PerformanceMetrics {
            execution_time_estimate,
            memory_usage_estimate,
            cpu_intensity,
            scalability_score,
            bottlenecks,
        })
    }

    /// Generate cross-language insights
    fn generate_cross_language_insights(
        &self,
        _ast: &UnifiedAST,
        language: ProgrammingLanguage,
    ) -> Result<Vec<CrossLanguageInsight>, Error> {
        let mut insights = Vec::new();

        // Add language-specific insights
        match language {
            ProgrammingLanguage::Rust => {
                insights.push(CrossLanguageInsight {
                    source_language: language,
                    target_language: ProgrammingLanguage::Java,
                    insight_type: CrossLanguageInsightType::BestPractice,
                    description:
                        "Rust's ownership model prevents memory leaks that Java avoids through GC"
                            .to_string(),
                    potential_improvements: vec![
                        "Consider RAII patterns in other languages".to_string()
                    ],
                });
            }
            ProgrammingLanguage::Java => {
                insights.push(CrossLanguageInsight {
                    source_language: language,
                    target_language: ProgrammingLanguage::Python,
                    insight_type: CrossLanguageInsightType::Pattern,
                    description: "Java's interface patterns can improve Python code organization"
                        .to_string(),
                    potential_improvements: vec!["Use abstract base classes in Python".to_string()],
                });
            }
            _ => {}
        }

        Ok(insights)
    }

    /// Calculate overall complexity score
    fn calculate_complexity_score(
        &self,
        ast: &UnifiedAST,
        quality_metrics: &CodeQualityMetrics,
    ) -> f64 {
        // Weighted complexity score
        let cyclomatic_weight = 0.3;
        let cognitive_weight = 0.2;
        let nesting_weight = 0.2;
        let maintainability_weight = 0.3;

        let cyclomatic_score =
            (20.0 - ast.complexity_metrics.cyclomatic_complexity).max(0.0) / 20.0;
        let cognitive_score = (20.0 - ast.complexity_metrics.cognitive_complexity).max(0.0) / 20.0;
        let nesting_score = (5.0 - ast.complexity_metrics.nesting_depth as f64).max(0.0) / 5.0;
        let maintainability_score = quality_metrics.maintainability_index;

        cyclomatic_score * cyclomatic_weight
            + cognitive_score * cognitive_weight
            + nesting_score * nesting_weight
            + maintainability_score * maintainability_weight
    }

    // Helper methods for quality metrics calculation
    fn calculate_maintainability_index(&self, complexity: &ComplexityMetrics) -> f64 {
        // Simplified maintainability index calculation
        let halstead = &complexity.halstead_complexity;
        let volume_score = if halstead.volume > 0.0 {
            (171.0
                - 5.2 * halstead.volume.log2()
                - 0.23 * complexity.cyclomatic_complexity
                - 16.2 * (complexity.lines_of_code as f64).log2())
            .max(0.0)
        } else {
            100.0
        };

        (volume_score / 171.0).min(1.0)
    }

    fn calculate_documentation_quality(&self, ast: &UnifiedAST) -> f64 {
        let total_functions = ast.functions.len() + ast.classes.len();
        if total_functions == 0 {
            return 1.0;
        }

        let documented_items = ast
            .comments
            .iter()
            .filter(|c| c.comment_type == CommentType::Documentation)
            .count();
        (documented_items as f64 / total_functions as f64).min(1.0)
    }

    fn calculate_testability_score(&self, ast: &UnifiedAST) -> f64 {
        let mut score = 1.0;

        // Penalize high complexity
        score -= (ast.complexity_metrics.cyclomatic_complexity / 20.0).min(0.5);

        // Penalize deep nesting
        score -= (ast.complexity_metrics.nesting_depth as f64 / 10.0).min(0.3);

        // Reward good function size
        let avg_function_lines = if !ast.functions.is_empty() {
            ast.functions
                .iter()
                .map(|f| f.complexity.lines_of_code)
                .sum::<u32>() as f64
                / ast.functions.len() as f64
        } else {
            0.0
        };

        if avg_function_lines > 50.0 {
            score -= 0.2;
        }

        score.clamp(0.0f64, 1.0)
    }

    fn calculate_security_score(&self, ast: &UnifiedAST) -> f64 {
        let mut score: f64 = 1.0;

        // Check for potential security issues in AST
        let security_keywords = ["eval", "exec", "system", "shell_exec"];
        let code_text = format!(
            "{} {} {} {}",
            ast.functions
                .iter()
                .map(|f| f.name.as_str())
                .collect::<Vec<_>>()
                .join(" "),
            ast.variables
                .iter()
                .map(|v| v.name.as_str())
                .collect::<Vec<_>>()
                .join(" "),
            ast.classes
                .iter()
                .map(|c| c.name.as_str())
                .collect::<Vec<_>>()
                .join(" "),
            ast.comments
                .iter()
                .map(|c| c.content.as_str())
                .collect::<Vec<_>>()
                .join(" ")
        );

        for keyword in &security_keywords {
            if code_text.contains(keyword) {
                score -= 0.1;
            }
        }

        score.clamp(0.0f64, 1.0)
    }

    fn calculate_performance_score(&self, complexity: &ComplexityMetrics) -> f64 {
        let mut score: f64 = 1.0;

        // Penalize high complexity
        score -= (complexity.cyclomatic_complexity / 20.0).min(0.4);

        // Penalize high nesting
        score -= (complexity.nesting_depth as f64 / 10.0).min(0.3);

        // Penalize large functions
        if complexity.lines_of_code > 100 {
            score -= 0.2;
        }

        score.clamp(0.0f64, 1.0)
    }

    fn calculate_coverage_potential(&self, ast: &UnifiedAST) -> f64 {
        let total_functions = ast.functions.len() + ast.classes.len();
        if total_functions == 0 {
            return 0.0;
        }

        // Functions with parameters are generally more testable
        let testable_functions = ast
            .functions
            .iter()
            .filter(|f| !f.parameters.is_empty())
            .count();
        (testable_functions as f64 / total_functions as f64).min(1.0)
    }
}

/// Pattern finding result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PatternFinding {
    pub pattern_type: PatternType,
    pub name: String,
    pub description: String,
    pub severity: PatternSeverity,
    pub location: Option<CodeLocation>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PatternType {
    DesignPattern,
    AntiPattern,
    CodeSmell,
    BestPractice,
}

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

// ============================================================================
// LANGUAGE-SPECIFIC QUALITY ASSESSORS
// ============================================================================

/// Quality assessor trait
#[async_trait]
pub trait QualityAssessor: Send + Sync {
    fn assess_quality(
        &self,
        ast: &UnifiedAST,
        context: Option<&CodeAnalysisContext>,
    ) -> Result<CodeQualityMetrics, Error>;
}

/// Pattern detector trait
#[async_trait]
pub trait PatternDetector: Send + Sync {
    fn detect_patterns(&self, ast: &UnifiedAST) -> Result<Vec<PatternFinding>, Error>;
}

/// Bug detector trait
#[async_trait]
pub trait BugDetector: Send + Sync {
    fn detect_bugs(&self, ast: &UnifiedAST, code: &str) -> Result<Vec<BugFinding>, Error>;
}

// Rust-specific implementations
pub struct RustQualityAssessor;

impl RustQualityAssessor {
    pub fn new() -> Self {
        Self
    }
}

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

#[async_trait]
impl QualityAssessor for RustQualityAssessor {
    fn assess_quality(
        &self,
        ast: &UnifiedAST,
        _context: Option<&CodeAnalysisContext>,
    ) -> Result<CodeQualityMetrics, Error> {
        // Rust-specific quality assessment
        let mut metrics = CodeQualityMetrics::default();

        // Check for Rust-specific patterns
        let has_error_handling = ast
            .functions
            .iter()
            .any(|f| f.body.iter().any(|s| s.content.contains("Result")));
        let has_ownership_patterns = ast.variables.iter().any(|v| !v.is_mutable);

        if has_error_handling {
            metrics.security_score += 0.1;
        }

        if has_ownership_patterns {
            metrics.performance_score += 0.1;
            metrics.maintainability_index += 0.1;
        }

        // Rust typically has good memory safety
        metrics.security_score = (metrics.security_score + 0.2).min(1.0);

        Ok(metrics)
    }
}

pub struct RustPatternDetector;

impl RustPatternDetector {
    pub fn new() -> Self {
        Self
    }
}

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

#[async_trait]
impl PatternDetector for RustPatternDetector {
    fn detect_patterns(&self, ast: &UnifiedAST) -> Result<Vec<PatternFinding>, Error> {
        let mut patterns = Vec::new();

        // Check for Rust-specific patterns
        for function in &ast.functions {
            if function.body.iter().any(|s| s.content.contains("unwrap()")) {
                patterns.push(PatternFinding {
                    pattern_type: PatternType::CodeSmell,
                    name: "Unchecked unwrap()".to_string(),
                    description: "Consider using proper error handling instead of unwrap()"
                        .to_string(),
                    severity: PatternSeverity::Medium,
                    location: Some(CodeLocation {
                        file_path: "unknown".to_string(),
                        line_number: Some(function.line_number),
                        column_number: None,
                        function_name: Some(function.name.clone()),
                    }),
                });
            }
        }

        Ok(patterns)
    }
}

pub struct RustBugDetector;

impl RustBugDetector {
    pub fn new() -> Self {
        Self
    }
}

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

#[async_trait]
impl BugDetector for RustBugDetector {
    fn detect_bugs(&self, _ast: &UnifiedAST, code: &str) -> Result<Vec<BugFinding>, Error> {
        let mut bugs = Vec::new();

        // Rust-specific bug detection
        if code.contains("unsafe ") {
            bugs.push(BugFinding {
                severity: BugSeverity::Medium,
                category: BugCategory::Memory,
                description: "Unsafe code block detected - ensure proper safety checks".to_string(),
                location: CodeLocation {
                    file_path: "unknown".to_string(),
                    line_number: None,
                    column_number: None,
                    function_name: None,
                },
                confidence: 0.8,
                suggested_fix: Some(
                    "Add safety invariants and consider if unsafe is truly necessary".to_string(),
                ),
            });
        }

        Ok(bugs)
    }
}

// Java-specific implementations
pub struct JavaQualityAssessor;

impl JavaQualityAssessor {
    pub fn new() -> Self {
        Self
    }
}

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

#[async_trait]
impl QualityAssessor for JavaQualityAssessor {
    fn assess_quality(
        &self,
        ast: &UnifiedAST,
        _context: Option<&CodeAnalysisContext>,
    ) -> Result<CodeQualityMetrics, Error> {
        // Java-specific quality assessment
        let mut metrics = CodeQualityMetrics::default();

        // Check for OOP patterns
        let has_inheritance = ast.classes.iter().any(|c| !c.inheritance.is_empty());
        let has_interfaces = ast.classes.iter().any(|c| !c.interfaces.is_empty());

        if has_inheritance {
            metrics.maintainability_index += 0.1;
        }

        if has_interfaces {
            metrics.testability_score += 0.1;
        }

        Ok(metrics)
    }
}

pub struct JavaPatternDetector;

impl JavaPatternDetector {
    pub fn new() -> Self {
        Self
    }
}

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

#[async_trait]
impl PatternDetector for JavaPatternDetector {
    fn detect_patterns(&self, ast: &UnifiedAST) -> Result<Vec<PatternFinding>, Error> {
        let mut patterns = Vec::new();

        // Check for Java-specific patterns
        for class in &ast.classes {
            if class.methods.len() > 20 {
                patterns.push(PatternFinding {
                    pattern_type: PatternType::AntiPattern,
                    name: "God Class".to_string(),
                    description: format!(
                        "Class '{}' has too many methods ({})",
                        class.name,
                        class.methods.len()
                    ),
                    severity: PatternSeverity::High,
                    location: Some(CodeLocation {
                        file_path: "unknown".to_string(),
                        line_number: Some(class.line_number),
                        column_number: None,
                        function_name: None,
                    }),
                });
            }
        }

        Ok(patterns)
    }
}

pub struct JavaBugDetector;

impl JavaBugDetector {
    pub fn new() -> Self {
        Self
    }
}

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

#[async_trait]
impl BugDetector for JavaBugDetector {
    fn detect_bugs(&self, _ast: &UnifiedAST, code: &str) -> Result<Vec<BugFinding>, Error> {
        let mut bugs = Vec::new();

        // Java-specific bug detection
        if code.contains("System.out.println") {
            bugs.push(BugFinding {
                severity: BugSeverity::Low,
                category: BugCategory::Logic,
                description: "Debug print statement detected in production code".to_string(),
                location: CodeLocation {
                    file_path: "unknown".to_string(),
                    line_number: None,
                    column_number: None,
                    function_name: None,
                },
                confidence: 0.9,
                suggested_fix: Some(
                    "Use proper logging framework instead of System.out.println".to_string(),
                ),
            });
        }

        Ok(bugs)
    }
}

// Python-specific implementations
pub struct PythonQualityAssessor;

impl PythonQualityAssessor {
    pub fn new() -> Self {
        Self
    }
}

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

#[async_trait]
impl QualityAssessor for PythonQualityAssessor {
    fn assess_quality(
        &self,
        ast: &UnifiedAST,
        _context: Option<&CodeAnalysisContext>,
    ) -> Result<CodeQualityMetrics, Error> {
        // Python-specific quality assessment
        let mut metrics = CodeQualityMetrics::default();

        // Check for Pythonic patterns
        let has_list_comprehensions = ast
            .functions
            .iter()
            .any(|f| f.body.iter().any(|s| s.content.contains("[")));
        let has_context_managers = ast
            .functions
            .iter()
            .any(|f| f.body.iter().any(|s| s.content.contains("with ")));

        if has_list_comprehensions {
            metrics.performance_score += 0.1;
        }

        if has_context_managers {
            metrics.security_score += 0.1;
            metrics.maintainability_index += 0.1;
        }

        Ok(metrics)
    }
}

pub struct PythonPatternDetector;

impl PythonPatternDetector {
    pub fn new() -> Self {
        Self
    }
}

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

#[async_trait]
impl PatternDetector for PythonPatternDetector {
    fn detect_patterns(&self, ast: &UnifiedAST) -> Result<Vec<PatternFinding>, Error> {
        let mut patterns = Vec::new();

        // Check for Python-specific patterns
        for function in &ast.functions {
            if function.body.len() > 50 {
                patterns.push(PatternFinding {
                    pattern_type: PatternType::CodeSmell,
                    name: "Long Function".to_string(),
                    description: format!(
                        "Function '{}' is very long ({} statements)",
                        function.name,
                        function.body.len()
                    ),
                    severity: PatternSeverity::Medium,
                    location: Some(CodeLocation {
                        file_path: "unknown".to_string(),
                        line_number: Some(function.line_number),
                        column_number: None,
                        function_name: Some(function.name.clone()),
                    }),
                });
            }
        }

        Ok(patterns)
    }
}

pub struct PythonBugDetector;

impl PythonBugDetector {
    pub fn new() -> Self {
        Self
    }
}

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

#[async_trait]
impl BugDetector for PythonBugDetector {
    fn detect_bugs(&self, _ast: &UnifiedAST, code: &str) -> Result<Vec<BugFinding>, Error> {
        let mut bugs = Vec::new();

        // Python-specific bug detection
        if code.contains("except:") {
            bugs.push(BugFinding {
                severity: BugSeverity::High,
                category: BugCategory::Logic,
                description: "Bare except clause detected - catch specific exceptions".to_string(),
                location: CodeLocation {
                    file_path: "unknown".to_string(),
                    line_number: None,
                    column_number: None,
                    function_name: None,
                },
                confidence: 0.9,
                suggested_fix: Some("Specify the exceptions you want to catch".to_string()),
            });
        }

        Ok(bugs)
    }
}

impl Default for CodeQualityMetrics {
    fn default() -> Self {
        Self {
            maintainability_index: 0.8,
            cyclomatic_complexity: 1.0,
            code_coverage_potential: 0.8,
            documentation_quality: 0.7,
            testability_score: 0.8,
            security_score: 0.9,
            performance_score: 0.8,
        }
    }
}

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

    #[test]
    fn test_code_analyzer_creation() {
        let analyzer = CodeAnalyzer::new();
        assert!(!analyzer.quality_assessors.is_empty());
        assert!(!analyzer.pattern_detectors.is_empty());
        assert!(!analyzer.bug_detectors.is_empty());
    }

    #[test]
    fn test_complexity_score_calculation() {
        let analyzer = CodeAnalyzer::new();
        let ast = UnifiedAST {
            language: ProgrammingLanguage::Rust,
            functions: vec![],
            classes: vec![],
            variables: vec![],
            imports: vec![],
            comments: vec![],
            complexity_metrics: ComplexityMetrics {
                cyclomatic_complexity: 5.0,
                cognitive_complexity: 6.0,
                nesting_depth: 2,
                lines_of_code: 20,
                halstead_complexity: HalsteadMetrics {
                    operators: HashMap::new(),
                    operands: HashMap::new(),
                    vocabulary: 10,
                    length: 20,
                    volume: 86.4,
                    difficulty: 5.0,
                },
            },
        };

        let quality_metrics = CodeQualityMetrics {
            maintainability_index: 0.8,
            cyclomatic_complexity: 5.0,
            code_coverage_potential: 0.7,
            documentation_quality: 0.6,
            testability_score: 0.8,
            security_score: 0.9,
            performance_score: 0.8,
        };

        let complexity_score = analyzer.calculate_complexity_score(&ast, &quality_metrics);
        assert!((0.0..=1.0).contains(&complexity_score));
    }
}