ruvllm 2.2.1

LLM serving runtime with Ruvector integration - Paged attention, KV cache, and SONA learning
Documentation
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
//! Multi-Perspective Critique System
//!
//! Implements a multi-perspective critique system that evaluates outputs from
//! different angles to provide comprehensive reflection. Each perspective focuses
//! on a specific quality dimension.
//!
//! ## Available Perspectives
//!
//! - **Correctness**: Verifies logical correctness and absence of errors
//! - **Completeness**: Checks if all requirements are addressed
//! - **Consistency**: Ensures internal consistency and follows conventions
//!
//! ## Architecture
//!
//! ```text
//! +-------------------+     +----------------------+
//! | Perspective trait |<----| CorrectnessChecker   |
//! | - critique()      |     +----------------------+
//! | - name()          |<----| CompletenessChecker  |
//! +-------------------+     +----------------------+
//!                      <----| ConsistencyChecker   |
//!                           +----------------------+
//!           |
//!           v
//! +-------------------+     +----------------------+
//! | CritiqueResult    |---->| UnifiedCritique      |
//! | - passed          |     | - combine results    |
//! | - score           |     | - generate summary   |
//! | - issues          |     +----------------------+
//! +-------------------+
//! ```

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

/// Configuration for perspectives
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerspectiveConfig {
    /// Weight for this perspective in combined scoring
    pub weight: f32,
    /// Minimum score to pass
    pub pass_threshold: f32,
    /// Whether to provide detailed feedback
    pub detailed_feedback: bool,
    /// Custom checks to perform
    pub custom_checks: Vec<String>,
}

impl Default for PerspectiveConfig {
    fn default() -> Self {
        Self {
            weight: 1.0,
            pass_threshold: 0.6,
            detailed_feedback: true,
            custom_checks: Vec::new(),
        }
    }
}

/// Result of a critique from one perspective
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CritiqueResult {
    /// Name of the perspective
    pub perspective_name: String,
    /// Whether the critique passed
    pub passed: bool,
    /// Score (0.0-1.0)
    pub score: f32,
    /// Summary of the critique
    pub summary: String,
    /// Specific issues found
    pub issues: Vec<CritiqueIssue>,
    /// Strengths identified
    pub strengths: Vec<String>,
    /// Time taken for critique (ms)
    pub critique_time_ms: u64,
}

impl CritiqueResult {
    /// Create a new passing critique result
    pub fn pass(perspective: impl Into<String>, score: f32, summary: impl Into<String>) -> Self {
        Self {
            perspective_name: perspective.into(),
            passed: true,
            score: score.clamp(0.0, 1.0),
            summary: summary.into(),
            issues: Vec::new(),
            strengths: Vec::new(),
            critique_time_ms: 0,
        }
    }

    /// Create a failing critique result
    pub fn fail(perspective: impl Into<String>, score: f32, summary: impl Into<String>) -> Self {
        Self {
            perspective_name: perspective.into(),
            passed: false,
            score: score.clamp(0.0, 1.0),
            summary: summary.into(),
            issues: Vec::new(),
            strengths: Vec::new(),
            critique_time_ms: 0,
        }
    }

    /// Add an issue
    pub fn with_issue(mut self, issue: CritiqueIssue) -> Self {
        self.issues.push(issue);
        self
    }

    /// Add a strength
    pub fn with_strength(mut self, strength: impl Into<String>) -> Self {
        self.strengths.push(strength.into());
        self
    }
}

/// A specific issue found during critique
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CritiqueIssue {
    /// Issue severity (0.0-1.0)
    pub severity: f32,
    /// Issue description
    pub description: String,
    /// Location (line number or section)
    pub location: Option<String>,
    /// Suggested fix
    pub suggestion: String,
    /// Category of issue
    pub category: IssueCategory,
}

impl CritiqueIssue {
    /// Create a new critique issue
    pub fn new(description: impl Into<String>, severity: f32, category: IssueCategory) -> Self {
        Self {
            severity: severity.clamp(0.0, 1.0),
            description: description.into(),
            location: None,
            suggestion: String::new(),
            category,
        }
    }

    /// Add location
    pub fn at(mut self, location: impl Into<String>) -> Self {
        self.location = Some(location.into());
        self
    }

    /// Add suggestion
    pub fn suggest(mut self, suggestion: impl Into<String>) -> Self {
        self.suggestion = suggestion.into();
        self
    }
}

/// Categories of critique issues
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum IssueCategory {
    /// Logical error
    Logic,
    /// Syntax or structural issue
    Syntax,
    /// Missing element
    Missing,
    /// Redundant element
    Redundant,
    /// Inconsistency
    Inconsistent,
    /// Style or convention violation
    Style,
    /// Security concern
    Security,
    /// Performance concern
    Performance,
    /// Documentation gap
    Documentation,
    /// Other
    Other,
}

/// Trait for perspective implementations
pub trait Perspective: Send + Sync {
    /// Get the perspective name
    fn name(&self) -> &str;

    /// Perform critique from this perspective
    fn critique(&self, output: &str, context: &ExecutionContext) -> CritiqueResult;

    /// Get the configuration
    fn config(&self) -> &PerspectiveConfig;
}

/// Correctness checker perspective
///
/// Verifies logical correctness, absence of errors, and proper functioning
pub struct CorrectnessChecker {
    config: PerspectiveConfig,
}

impl CorrectnessChecker {
    /// Create a new correctness checker
    pub fn new() -> Self {
        Self {
            config: PerspectiveConfig {
                weight: 1.2, // Higher weight for correctness
                pass_threshold: 0.7,
                detailed_feedback: true,
                custom_checks: Vec::new(),
            },
        }
    }

    /// Create with custom config
    pub fn with_config(config: PerspectiveConfig) -> Self {
        Self { config }
    }

    /// Check for error patterns in output
    fn check_for_errors(&self, output: &str) -> Vec<CritiqueIssue> {
        let mut issues = Vec::new();

        // Check for explicit error markers
        let error_patterns = [
            ("error[", "Compiler error present", IssueCategory::Syntax),
            ("Error:", "Runtime error present", IssueCategory::Logic),
            ("panic!", "Panic in code", IssueCategory::Logic),
            (
                "unwrap()",
                "Potential panic from unwrap",
                IssueCategory::Logic,
            ),
            (
                "expect()",
                "Potential panic from expect",
                IssueCategory::Logic,
            ),
            ("todo!()", "Unimplemented todo", IssueCategory::Missing),
            (
                "unimplemented!()",
                "Unimplemented code",
                IssueCategory::Missing,
            ),
            (
                "unreachable!()",
                "Unreachable code marker",
                IssueCategory::Logic,
            ),
        ];

        for (pattern, description, category) in error_patterns {
            if output.contains(pattern) {
                let count = output.matches(pattern).count();
                issues.push(
                    CritiqueIssue::new(
                        format!("{} ({} occurrence(s))", description, count),
                        if category == IssueCategory::Logic {
                            0.8
                        } else {
                            0.5
                        },
                        category,
                    )
                    .suggest(format!("Address or remove {}", pattern)),
                );
            }
        }

        // Check for unbalanced brackets (potential syntax errors)
        let open_parens = output.matches('(').count();
        let close_parens = output.matches(')').count();
        if open_parens != close_parens {
            issues.push(
                CritiqueIssue::new(
                    format!(
                        "Unbalanced parentheses: {} open, {} close",
                        open_parens, close_parens
                    ),
                    0.7,
                    IssueCategory::Syntax,
                )
                .suggest("Check for missing or extra parentheses"),
            );
        }

        let open_braces = output.matches('{').count();
        let close_braces = output.matches('}').count();
        if open_braces != close_braces {
            issues.push(
                CritiqueIssue::new(
                    format!(
                        "Unbalanced braces: {} open, {} close",
                        open_braces, close_braces
                    ),
                    0.7,
                    IssueCategory::Syntax,
                )
                .suggest("Check for missing or extra braces"),
            );
        }

        issues
    }

    /// Check for logic issues
    fn check_logic(&self, output: &str) -> Vec<CritiqueIssue> {
        let mut issues = Vec::new();

        // Check for potential infinite loops
        if output.contains("loop {") && !output.contains("break") {
            issues.push(
                CritiqueIssue::new(
                    "Potential infinite loop without break",
                    0.6,
                    IssueCategory::Logic,
                )
                .suggest("Add break condition or use while/for loop"),
            );
        }

        // Check for empty functions
        let empty_fn_pattern = "fn ";
        if output.contains(empty_fn_pattern) {
            // Simple heuristic: function with just {}
            if output.contains("{ }") || output.contains("{}") {
                issues.push(
                    CritiqueIssue::new("Empty function body detected", 0.4, IssueCategory::Missing)
                        .suggest("Implement function body or add todo!()"),
                );
            }
        }

        // Check for hardcoded values that might be problematic
        if output.contains("localhost") || output.contains("127.0.0.1") {
            issues.push(
                CritiqueIssue::new("Hardcoded localhost/IP address", 0.3, IssueCategory::Style)
                    .suggest("Consider using configuration or environment variables"),
            );
        }

        issues
    }
}

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

impl Perspective for CorrectnessChecker {
    fn name(&self) -> &str {
        "correctness"
    }

    fn critique(&self, output: &str, _context: &ExecutionContext) -> CritiqueResult {
        let start = std::time::Instant::now();

        if output.is_empty() {
            return CritiqueResult::fail(self.name(), 0.0, "Empty output").with_issue(
                CritiqueIssue::new("No output provided", 1.0, IssueCategory::Missing),
            );
        }

        let mut issues = Vec::new();
        let mut strengths = Vec::new();

        // Check for errors
        issues.extend(self.check_for_errors(output));

        // Check logic
        issues.extend(self.check_logic(output));

        // Identify strengths
        if output.contains("Result<") || output.contains("Option<") {
            strengths.push("Uses proper error handling types".to_string());
        }
        if output.contains("#[test]") {
            strengths.push("Includes tests".to_string());
        }
        if output.contains("///") || output.contains("//!") {
            strengths.push("Includes documentation".to_string());
        }

        // Calculate score
        let issue_penalty: f32 = issues.iter().map(|i| i.severity * 0.15).sum();
        let score = (1.0 - issue_penalty).clamp(0.0, 1.0);
        let passed = score >= self.config.pass_threshold;

        let summary = if passed {
            format!(
                "Code appears correct with {} minor issue(s)",
                issues.iter().filter(|i| i.severity < 0.5).count()
            )
        } else {
            format!("Found {} issue(s) affecting correctness", issues.len())
        };

        let mut result = if passed {
            CritiqueResult::pass(self.name(), score, summary)
        } else {
            CritiqueResult::fail(self.name(), score, summary)
        };

        result.issues = issues;
        result.strengths = strengths;
        result.critique_time_ms = start.elapsed().as_millis() as u64;
        result
    }

    fn config(&self) -> &PerspectiveConfig {
        &self.config
    }
}

/// Completeness checker perspective
///
/// Checks if all requirements are addressed and the output is complete
pub struct CompletenessChecker {
    config: PerspectiveConfig,
}

impl CompletenessChecker {
    /// Create a new completeness checker
    pub fn new() -> Self {
        Self {
            config: PerspectiveConfig {
                weight: 1.0,
                pass_threshold: 0.6,
                detailed_feedback: true,
                custom_checks: Vec::new(),
            },
        }
    }

    /// Create with custom config
    pub fn with_config(config: PerspectiveConfig) -> Self {
        Self { config }
    }

    /// Extract requirements from task
    fn extract_requirements(&self, task: &str) -> Vec<String> {
        let mut requirements = Vec::new();

        // Look for action verbs
        let action_words = [
            "implement",
            "create",
            "add",
            "build",
            "write",
            "define",
            "include",
            "support",
            "handle",
            "return",
            "take",
            "accept",
        ];

        for word in action_words {
            if task.to_lowercase().contains(word) {
                requirements.push(format!("Task mentions '{}' action", word));
            }
        }

        // Look for specific features mentioned
        if task.contains("error handling") || task.contains("handle error") {
            requirements.push("Error handling".to_string());
        }
        if task.contains("test") {
            requirements.push("Tests".to_string());
        }
        if task.contains("document") {
            requirements.push("Documentation".to_string());
        }
        if task.contains("async") {
            requirements.push("Async support".to_string());
        }

        requirements
    }

    /// Check if requirements are met
    fn check_requirements(&self, output: &str, requirements: &[String]) -> Vec<CritiqueIssue> {
        let mut issues = Vec::new();
        let output_lower = output.to_lowercase();

        for req in requirements {
            let req_lower = req.to_lowercase();

            // Simple keyword matching for requirement fulfillment
            let is_met = req_lower
                .split_whitespace()
                .any(|word| word.len() > 3 && output_lower.contains(word));

            if !is_met {
                issues.push(
                    CritiqueIssue::new(
                        format!("Requirement may not be addressed: {}", req),
                        0.4,
                        IssueCategory::Missing,
                    )
                    .suggest(format!("Ensure {} is implemented", req)),
                );
            }
        }

        issues
    }

    /// Check for incomplete markers
    fn check_incomplete_markers(&self, output: &str) -> Vec<CritiqueIssue> {
        let mut issues = Vec::new();

        let markers = [
            ("TODO", "Incomplete TODO item"),
            ("FIXME", "Incomplete FIXME item"),
            ("XXX", "XXX marker present"),
            ("HACK", "Temporary hack present"),
            ("...", "Ellipsis indicating incomplete"),
            ("// ...", "Code omitted marker"),
            ("/* ... */", "Code omitted block"),
        ];

        for (marker, description) in markers {
            if output.contains(marker) {
                let count = output.matches(marker).count();
                issues.push(
                    CritiqueIssue::new(
                        format!("{} ({} occurrence(s))", description, count),
                        0.5,
                        IssueCategory::Missing,
                    )
                    .suggest(format!("Complete or remove {} markers", marker)),
                );
            }
        }

        issues
    }
}

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

impl Perspective for CompletenessChecker {
    fn name(&self) -> &str {
        "completeness"
    }

    fn critique(&self, output: &str, context: &ExecutionContext) -> CritiqueResult {
        let start = std::time::Instant::now();

        if output.is_empty() {
            return CritiqueResult::fail(self.name(), 0.0, "Empty output - nothing completed")
                .with_issue(CritiqueIssue::new(
                    "No output provided",
                    1.0,
                    IssueCategory::Missing,
                ));
        }

        let mut issues = Vec::new();
        let mut strengths = Vec::new();

        // Extract and check requirements
        let requirements = self.extract_requirements(&context.task);
        issues.extend(self.check_requirements(output, &requirements));

        // Check for incomplete markers
        issues.extend(self.check_incomplete_markers(output));

        // Check output length as proxy for completeness
        let line_count = output.lines().count();
        if line_count < 5 && context.task.len() > 50 {
            issues.push(
                CritiqueIssue::new(
                    "Output may be too brief for the task complexity",
                    0.3,
                    IssueCategory::Missing,
                )
                .suggest("Consider expanding the implementation"),
            );
        }

        // Identify completeness strengths
        if !output.contains("TODO") && !output.contains("FIXME") {
            strengths.push("No incomplete TODO/FIXME markers".to_string());
        }
        if output.lines().count() > 20 {
            strengths.push("Substantial implementation provided".to_string());
        }

        // Calculate score
        let issue_penalty: f32 = issues.iter().map(|i| i.severity * 0.2).sum();
        let score = (1.0 - issue_penalty).clamp(0.0, 1.0);
        let passed = score >= self.config.pass_threshold;

        let summary = if passed {
            "Output appears complete with all major requirements addressed"
        } else {
            "Output may be incomplete - some requirements not clearly addressed"
        };

        let mut result = if passed {
            CritiqueResult::pass(self.name(), score, summary)
        } else {
            CritiqueResult::fail(self.name(), score, summary)
        };

        result.issues = issues;
        result.strengths = strengths;
        result.critique_time_ms = start.elapsed().as_millis() as u64;
        result
    }

    fn config(&self) -> &PerspectiveConfig {
        &self.config
    }
}

/// Consistency checker perspective
///
/// Ensures internal consistency and adherence to conventions
pub struct ConsistencyChecker {
    config: PerspectiveConfig,
}

impl ConsistencyChecker {
    /// Create a new consistency checker
    pub fn new() -> Self {
        Self {
            config: PerspectiveConfig {
                weight: 0.8, // Slightly lower weight
                pass_threshold: 0.5,
                detailed_feedback: true,
                custom_checks: Vec::new(),
            },
        }
    }

    /// Create with custom config
    pub fn with_config(config: PerspectiveConfig) -> Self {
        Self { config }
    }

    /// Check naming conventions
    fn check_naming(&self, output: &str) -> Vec<CritiqueIssue> {
        let mut issues = Vec::new();

        // Check for mixed naming conventions (simple heuristic)
        let _has_snake_case = output.contains("_") && output.contains("fn ");
        let has_camel_case = output
            .chars()
            .zip(output.chars().skip(1))
            .any(|(a, b)| a.is_lowercase() && b.is_uppercase());

        // In Rust, we expect snake_case for functions/variables
        if has_camel_case && output.contains("fn ") && !output.contains("trait ") {
            issues.push(
                CritiqueIssue::new(
                    "Possible camelCase usage in Rust code (should use snake_case)",
                    0.3,
                    IssueCategory::Style,
                )
                .suggest("Use snake_case for function and variable names"),
            );
        }

        issues
    }

    /// Check for consistent formatting
    fn check_formatting(&self, output: &str) -> Vec<CritiqueIssue> {
        let mut issues = Vec::new();

        // Check for inconsistent indentation
        let lines: Vec<&str> = output.lines().collect();
        let mut indent_styles = HashMap::new();

        for line in &lines {
            if line.starts_with("    ") {
                *indent_styles.entry("4spaces").or_insert(0) += 1;
            } else if line.starts_with("  ") && !line.starts_with("    ") {
                *indent_styles.entry("2spaces").or_insert(0) += 1;
            } else if line.starts_with('\t') {
                *indent_styles.entry("tabs").or_insert(0) += 1;
            }
        }

        if indent_styles.len() > 1 {
            issues.push(
                CritiqueIssue::new(
                    "Inconsistent indentation style detected",
                    0.4,
                    IssueCategory::Style,
                )
                .suggest("Use consistent indentation (4 spaces recommended for Rust)"),
            );
        }

        // Check for trailing whitespace
        let trailing_ws_count = lines.iter().filter(|l| l.ends_with(' ')).count();
        if trailing_ws_count > 0 {
            issues.push(
                CritiqueIssue::new(
                    format!("Trailing whitespace on {} line(s)", trailing_ws_count),
                    0.2,
                    IssueCategory::Style,
                )
                .suggest("Remove trailing whitespace"),
            );
        }

        issues
    }

    /// Check for internal consistency
    fn check_internal_consistency(&self, output: &str) -> Vec<CritiqueIssue> {
        let mut issues = Vec::new();

        // Check for mix of error handling styles
        let uses_result = output.contains("Result<");
        let uses_option = output.contains("Option<");
        let uses_unwrap = output.contains(".unwrap()");
        let uses_question = output.contains("?;") || output.contains("?)");

        if (uses_result || uses_option) && uses_unwrap && uses_question {
            issues.push(
                CritiqueIssue::new(
                    "Inconsistent error handling: mixing ? operator and unwrap()",
                    0.4,
                    IssueCategory::Inconsistent,
                )
                .suggest("Prefer using ? operator consistently for error propagation"),
            );
        }

        // Check for consistent visibility modifiers
        let pub_count = output.matches("pub fn").count();
        let priv_count = output.matches("fn ").count() - pub_count;

        if pub_count > 0
            && priv_count > 0
            && (pub_count as f32 / (pub_count + priv_count) as f32) < 0.3
        {
            // This is actually fine, just noting it
        }

        issues
    }
}

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

impl Perspective for ConsistencyChecker {
    fn name(&self) -> &str {
        "consistency"
    }

    fn critique(&self, output: &str, _context: &ExecutionContext) -> CritiqueResult {
        let start = std::time::Instant::now();

        if output.is_empty() {
            return CritiqueResult::fail(self.name(), 0.0, "Empty output").with_issue(
                CritiqueIssue::new(
                    "No output to check consistency",
                    1.0,
                    IssueCategory::Missing,
                ),
            );
        }

        let mut issues = Vec::new();
        let mut strengths = Vec::new();

        // Check naming conventions
        issues.extend(self.check_naming(output));

        // Check formatting
        issues.extend(self.check_formatting(output));

        // Check internal consistency
        issues.extend(self.check_internal_consistency(output));

        // Identify strengths
        if !issues
            .iter()
            .any(|i| i.category == IssueCategory::Inconsistent)
        {
            strengths.push("Consistent coding style".to_string());
        }
        if output.contains("use std::") || output.contains("use crate::") {
            strengths.push("Proper import organization".to_string());
        }

        // Calculate score
        let issue_penalty: f32 = issues.iter().map(|i| i.severity * 0.15).sum();
        let score = (1.0 - issue_penalty).clamp(0.0, 1.0);
        let passed = score >= self.config.pass_threshold;

        let summary = if passed {
            "Code follows consistent conventions and style"
        } else {
            "Inconsistencies detected in style or conventions"
        };

        let mut result = if passed {
            CritiqueResult::pass(self.name(), score, summary)
        } else {
            CritiqueResult::fail(self.name(), score, summary)
        };

        result.issues = issues;
        result.strengths = strengths;
        result.critique_time_ms = start.elapsed().as_millis() as u64;
        result
    }

    fn config(&self) -> &PerspectiveConfig {
        &self.config
    }
}

/// Unified critique combining multiple perspectives
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UnifiedCritique {
    /// Individual critique results
    pub critiques: Vec<CritiqueResult>,
    /// Overall pass/fail
    pub passed: bool,
    /// Combined score (weighted average)
    pub combined_score: f32,
    /// Overall summary
    pub summary: String,
    /// Prioritized issues (sorted by severity)
    pub prioritized_issues: Vec<CritiqueIssue>,
    /// All identified strengths
    pub strengths: Vec<String>,
    /// Total critique time
    pub total_time_ms: u64,
}

impl UnifiedCritique {
    /// Create a unified critique from multiple perspective results
    pub fn combine(critiques: Vec<CritiqueResult>, weights: &[f32]) -> Self {
        let mut total_weight = 0.0f32;
        let mut weighted_sum = 0.0f32;
        let mut all_issues = Vec::new();
        let mut all_strengths = Vec::new();
        let mut total_time = 0u64;

        for (i, critique) in critiques.iter().enumerate() {
            let weight = weights.get(i).copied().unwrap_or(1.0);
            total_weight += weight;
            weighted_sum += critique.score * weight;

            all_issues.extend(critique.issues.clone());
            all_strengths.extend(critique.strengths.clone());
            total_time += critique.critique_time_ms;
        }

        let combined_score = if total_weight > 0.0 {
            weighted_sum / total_weight
        } else {
            0.0
        };

        // Sort issues by severity
        all_issues.sort_by(|a, b| {
            b.severity
                .partial_cmp(&a.severity)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Deduplicate strengths
        all_strengths.sort();
        all_strengths.dedup();

        let pass_count = critiques.iter().filter(|c| c.passed).count();
        let passed = pass_count > critiques.len() / 2 && combined_score >= 0.6;

        let summary = if passed {
            format!(
                "Passed {}/{} perspectives with combined score {:.2}",
                pass_count,
                critiques.len(),
                combined_score
            )
        } else {
            format!(
                "Failed: only {}/{} perspectives passed, combined score {:.2}",
                pass_count,
                critiques.len(),
                combined_score
            )
        };

        Self {
            critiques,
            passed,
            combined_score,
            summary,
            prioritized_issues: all_issues,
            strengths: all_strengths,
            total_time_ms: total_time,
        }
    }

    /// Get the top N issues
    pub fn top_issues(&self, n: usize) -> Vec<&CritiqueIssue> {
        self.prioritized_issues.iter().take(n).collect()
    }

    /// Get issues by category
    pub fn issues_by_category(&self, category: IssueCategory) -> Vec<&CritiqueIssue> {
        self.prioritized_issues
            .iter()
            .filter(|i| i.category == category)
            .collect()
    }
}

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

    fn test_context() -> ExecutionContext {
        ExecutionContext::new("implement a function", AgentType::Coder, "test input")
    }

    #[test]
    fn test_critique_result_builders() {
        let pass = CritiqueResult::pass("test", 0.8, "Good job").with_strength("Clean code");
        assert!(pass.passed);
        assert!(!pass.strengths.is_empty());

        let fail = CritiqueResult::fail("test", 0.3, "Issues found")
            .with_issue(CritiqueIssue::new("Problem", 0.7, IssueCategory::Logic));
        assert!(!fail.passed);
        assert!(!fail.issues.is_empty());
    }

    #[test]
    fn test_critique_issue_builder() {
        let issue = CritiqueIssue::new("Test issue", 0.5, IssueCategory::Logic)
            .at("line 5")
            .suggest("Fix it");

        assert_eq!(issue.location, Some("line 5".to_string()));
        assert!(!issue.suggestion.is_empty());
    }

    #[test]
    fn test_correctness_checker_empty() {
        let checker = CorrectnessChecker::new();
        let context = test_context();
        let result = checker.critique("", &context);

        assert!(!result.passed);
        assert!(result.score < 0.5);
    }

    #[test]
    fn test_correctness_checker_with_errors() {
        let checker = CorrectnessChecker::new();
        let context = test_context();
        let output = r#"
            fn test() {
                panic!("error");
                todo!();
            }
        "#;

        let result = checker.critique(output, &context);
        assert!(!result.issues.is_empty());
    }

    #[test]
    fn test_correctness_checker_clean_code() {
        let checker = CorrectnessChecker::new();
        let context = test_context();
        let output = r#"
            /// Documentation
            pub fn example() -> Result<(), Error> {
                Ok(())
            }

            #[test]
            fn test_example() {
                assert!(example().is_ok());
            }
        "#;

        let result = checker.critique(output, &context);
        assert!(!result.strengths.is_empty());
    }

    #[test]
    fn test_completeness_checker_todo() {
        let checker = CompletenessChecker::new();
        let context = test_context();
        let output = "fn example() { // TODO: implement }";

        let result = checker.critique(output, &context);
        assert!(result
            .issues
            .iter()
            .any(|i| i.category == IssueCategory::Missing));
    }

    #[test]
    fn test_completeness_checker_complete() {
        let checker = CompletenessChecker::new();
        let context = ExecutionContext::new("implement function", AgentType::Coder, "input");
        let output = r#"
            pub fn implement_function() -> i32 {
                let value = 42;
                // Full implementation here
                value * 2
            }
        "#;

        let result = checker.critique(output, &context);
        assert!(result.passed || result.score > 0.5);
    }

    #[test]
    fn test_consistency_checker_mixed_indent() {
        let checker = ConsistencyChecker::new();
        let context = test_context();
        let output = "fn test() {\n    line1\n  line2\n\tline3\n}";

        let result = checker.critique(output, &context);
        assert!(result
            .issues
            .iter()
            .any(|i| i.category == IssueCategory::Style));
    }

    #[test]
    fn test_consistency_checker_clean() {
        let checker = ConsistencyChecker::new();
        let context = test_context();
        let output = r#"
use std::io;

fn clean_function() -> io::Result<()> {
    let value = 42;
    Ok(())
}
        "#;

        let result = checker.critique(output, &context);
        // Should pass or have high score
        assert!(result.score > 0.5);
    }

    #[test]
    fn test_unified_critique() {
        let correctness = CritiqueResult::pass("correctness", 0.8, "Good");
        let completeness = CritiqueResult::pass("completeness", 0.7, "Complete");
        let consistency = CritiqueResult::fail("consistency", 0.4, "Issues");

        let unified = UnifiedCritique::combine(
            vec![correctness, completeness, consistency],
            &[1.2, 1.0, 0.8],
        );

        assert!(unified.combined_score > 0.5);
        assert!(!unified.summary.is_empty());
    }

    #[test]
    fn test_unified_critique_issues_by_category() {
        let mut result = CritiqueResult::fail("test", 0.5, "Issues")
            .with_issue(CritiqueIssue::new("Logic issue", 0.7, IssueCategory::Logic))
            .with_issue(CritiqueIssue::new("Style issue", 0.3, IssueCategory::Style));

        let unified = UnifiedCritique::combine(vec![result], &[1.0]);

        let logic_issues = unified.issues_by_category(IssueCategory::Logic);
        assert_eq!(logic_issues.len(), 1);
    }

    #[test]
    fn test_perspective_trait_implementation() {
        let checker: Box<dyn Perspective> = Box::new(CorrectnessChecker::new());
        assert_eq!(checker.name(), "correctness");

        let context = test_context();
        let result = checker.critique("fn test() {}", &context);
        assert!(!result.perspective_name.is_empty());
    }
}