commitbee 0.6.0

AI-powered commit message generator using tree-sitter semantic analysis and local LLMs
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
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
// SPDX-FileCopyrightText: 2026 Sephyi <me@sephy.io>
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial

//! Evaluation harness for pipeline quality testing.
//!
//! Runs fixture diffs through the context builder, commit type inference,
//! and sanitizer pipeline, then compares against expected output snapshots.
//! Feature-gated behind `eval`.
//!
//! ## Fixture format
//!
//! Each fixture lives in `tests/fixtures/eval/<name>/`:
//! - `metadata.toml` — required: assertions and expectations
//! - `diff.patch` — required: unified diff
//! - `symbols.toml` — optional: injected `CodeSymbol` data for AST testing
//! - `response.json` — optional: canned LLM response for sanitizer testing
//! - `config.toml` — optional: config overrides
//! - `expected.txt` — optional: expected first line of sanitized message

use std::path::{Path, PathBuf};
use std::sync::Arc;

use console::style;
use serde::Deserialize;

use crate::config::Config;
use crate::domain::{
    ChangeStatus, CodeSymbol, DiffStats, FileCategory, FileChange, StagedChanges, SymbolKind,
};
use crate::error::{Error, Result};
use crate::services::context::ContextBuilder;
use crate::services::sanitizer::CommitSanitizer;

/// Metadata describing a single evaluation fixture.
#[derive(Debug, Deserialize)]
pub struct FixtureMetadata {
    pub name: String,
    pub description: String,
    #[serde(default)]
    #[allow(dead_code)]
    pub language: Option<String>,
    #[serde(default)]
    #[allow(dead_code)]
    pub category: Option<String>,
    pub expected_type: String,
    #[serde(default)]
    pub expected_scope: Option<String>,

    /// Evidence flag assertions (all optional).
    #[serde(default)]
    pub evidence: Option<EvidenceExpectation>,

    /// Prompt content assertions.
    #[serde(default)]
    pub prompt: Option<PromptExpectation>,

    /// Cross-file connection assertions.
    #[serde(default)]
    pub connections: Option<ConnectionsExpectation>,

    /// Subject line assertions.
    #[serde(default)]
    pub subject: Option<SubjectExpectation>,

    /// Breaking change assertions.
    #[serde(default)]
    pub breaking: Option<BreakingExpectation>,
}

/// Expected evidence flags from the context builder.
#[derive(Debug, Default, Deserialize)]
pub struct EvidenceExpectation {
    #[serde(default)]
    pub is_mechanical: Option<bool>,
    #[serde(default)]
    pub has_bug_evidence: Option<bool>,
    #[serde(default)]
    pub has_new_public_api: Option<bool>,
    #[serde(default)]
    pub public_api_removed_count: Option<usize>,
    #[serde(default)]
    pub is_dependency_only: Option<bool>,
}

/// Expected prompt content patterns.
#[derive(Debug, Default, Deserialize)]
pub struct PromptExpectation {
    #[serde(default)]
    pub must_contain: Vec<String>,
    #[serde(default)]
    pub must_not_contain: Vec<String>,
}

/// Expected cross-file connection assertions.
#[derive(Debug, Default, Deserialize)]
pub struct ConnectionsExpectation {
    #[serde(default)]
    pub expected_count: Option<usize>,
    #[serde(default)]
    pub min_count: Option<usize>,
    #[serde(default)]
    pub must_contain: Vec<String>,
}

/// Expected subject line properties.
#[derive(Debug, Default, Deserialize)]
pub struct SubjectExpectation {
    #[serde(default)]
    pub must_contain: Vec<String>,
    #[serde(default)]
    pub must_not_contain: Vec<String>,
    #[serde(default)]
    pub max_length: Option<usize>,
}

/// Expected breaking change behavior.
#[derive(Debug, Default, Deserialize)]
pub struct BreakingExpectation {
    /// Whether breaking_change metadata signals are expected.
    #[serde(default)]
    pub expected: Option<bool>,
}

/// A single symbol definition from `symbols.toml`.
#[derive(Debug, Deserialize)]
struct SymbolDef {
    kind: String,
    name: String,
    file: String,
    #[serde(default = "default_line")]
    line: usize,
    #[serde(default = "default_line")]
    end_line: usize,
    #[serde(default)]
    is_public: bool,
    #[serde(default)]
    is_added: bool,
    #[serde(default)]
    is_whitespace_only: Option<bool>,
    #[serde(default)]
    signature: Option<String>,
    #[serde(default)]
    parent_scope: Option<String>,
}

fn default_line() -> usize {
    1
}

/// Container for deserializing `symbols.toml`.
#[derive(Debug, Deserialize)]
struct SymbolsFile {
    #[serde(default)]
    symbols: Vec<SymbolDef>,
}

/// Individual assertion failure.
#[derive(Debug, Clone)]
pub struct AssertionFailure {
    pub category: String,
    pub message: String,
}

impl std::fmt::Display for AssertionFailure {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "[{}] {}", self.category, self.message)
    }
}

/// Result of running one fixture through the pipeline.
#[derive(Debug)]
pub struct EvalResult {
    pub fixture_name: String,
    pub description: String,
    // Type inference check
    pub expected_type: String,
    pub actual_type: String,
    pub type_passed: bool,
    // Scope check (only if expected_scope is set)
    pub expected_scope: Option<String>,
    pub actual_scope: Option<String>,
    pub scope_passed: bool,
    // Prompt assembly check
    pub prompt_assembled: bool,
    // Sanitizer check (only if response.json exists)
    pub sanitizer_result: Option<SanitizerCheck>,
    // Expected message check (only if expected.txt exists)
    pub message_check: Option<MessageCheck>,
    // New assertion failures (evidence, prompt, connections, subject, breaking)
    pub assertion_failures: Vec<AssertionFailure>,
    // Overall
    pub error: Option<String>,
}

#[derive(Debug)]
pub struct SanitizerCheck {
    pub passed: bool,
    pub actual_message: Option<String>,
    pub error: Option<String>,
}

#[derive(Debug)]
pub struct MessageCheck {
    pub expected_first_line: String,
    pub actual_first_line: Option<String>,
    pub passed: bool,
}

impl EvalResult {
    pub fn passed(&self) -> bool {
        self.type_passed
            && self.scope_passed
            && self.prompt_assembled
            && self.error.is_none()
            && self.assertion_failures.is_empty()
            && self.sanitizer_result.as_ref().is_none_or(|s| s.passed)
            && self.message_check.as_ref().is_none_or(|m| m.passed)
    }
}

/// Evaluation runner that processes fixture directories.
pub struct EvalRunner {
    fixtures_dir: PathBuf,
    filter: Option<String>,
}

impl EvalRunner {
    #[must_use]
    pub fn new(fixtures_dir: PathBuf, filter: Option<String>) -> Self {
        Self {
            fixtures_dir,
            filter,
        }
    }

    /// Run all fixtures and return results without calling `process::exit`.
    ///
    /// Suitable for integration tests where panicking on failure is preferred
    /// over terminating the process.
    #[allow(dead_code)]
    pub fn run_sync(&self) -> Result<Vec<EvalResult>> {
        if !self.fixtures_dir.exists() {
            return Err(Error::Config(format!(
                "Fixtures directory not found: {}",
                self.fixtures_dir.display()
            )));
        }

        let fixtures = self.discover_fixtures()?;
        let mut results = Vec::new();
        for fixture_dir in &fixtures {
            results.push(self.run_fixture(fixture_dir));
        }
        Ok(results)
    }

    pub async fn run(&self) -> Result<()> {
        if !self.fixtures_dir.exists() {
            return Err(Error::Config(format!(
                "Fixtures directory not found: {}",
                self.fixtures_dir.display()
            )));
        }

        let fixtures = self.discover_fixtures()?;

        if fixtures.is_empty() {
            eprintln!(
                "{} No fixtures found in {}",
                style("warning:").yellow().bold(),
                self.fixtures_dir.display()
            );
            return Ok(());
        }

        eprintln!(
            "{} Running {} evaluation fixture(s)...\n",
            style("eval:").cyan().bold(),
            fixtures.len()
        );

        let mut results = Vec::new();
        for fixture_dir in &fixtures {
            let result = self.run_fixture(fixture_dir);
            results.push(result);
        }

        self.print_results(&results);

        // Print aggregate summary
        let summary = EvalSummary::from_results(&results);
        eprintln!("{}", summary.format_report());

        if summary.total_failed > 0 {
            eprintln!(
                "{} {} fixture(s) failed",
                style("FAIL").red().bold(),
                summary.total_failed,
            );
            std::process::exit(1);
        }

        eprintln!(
            "{} All {} fixture(s) passed",
            style("PASS").green().bold(),
            summary.total_passed,
        );

        Ok(())
    }

    /// Discover fixture subdirectories, optionally filtered by pattern.
    fn discover_fixtures(&self) -> Result<Vec<PathBuf>> {
        let mut fixtures = Vec::new();

        let entries = std::fs::read_dir(&self.fixtures_dir).map_err(|e| {
            Error::Config(format!(
                "Cannot read fixtures directory {}: {}",
                self.fixtures_dir.display(),
                e
            ))
        })?;

        for entry in entries {
            let entry = entry?;
            let path = entry.path();

            if !path.is_dir() {
                continue;
            }

            // Must contain metadata.toml
            if !path.join("metadata.toml").exists() {
                continue;
            }

            // Apply filter if set
            if let Some(ref filter) = self.filter {
                let dir_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
                if !dir_name.contains(filter.as_str()) {
                    continue;
                }
            }

            fixtures.push(path);
        }

        fixtures.sort();
        Ok(fixtures)
    }

    /// Run a single fixture through the pipeline.
    fn run_fixture(&self, fixture_dir: &Path) -> EvalResult {
        let dir_name = fixture_dir
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("unknown")
            .to_string();

        // Load metadata
        let metadata = match self.load_metadata(fixture_dir) {
            Ok(m) => m,
            Err(e) => {
                return EvalResult {
                    fixture_name: dir_name,
                    description: String::new(),
                    expected_type: String::new(),
                    actual_type: String::new(),
                    type_passed: false,
                    expected_scope: None,
                    actual_scope: None,
                    scope_passed: false,
                    prompt_assembled: false,
                    sanitizer_result: None,
                    message_check: None,
                    assertion_failures: Vec::new(),
                    error: Some(format!("Failed to load metadata: {}", e)),
                };
            }
        };

        // Load diff
        let diff_content = match std::fs::read_to_string(fixture_dir.join("diff.patch")) {
            Ok(d) => d,
            Err(e) => {
                return EvalResult {
                    fixture_name: metadata.name,
                    description: metadata.description,
                    expected_type: metadata.expected_type,
                    actual_type: String::new(),
                    type_passed: false,
                    expected_scope: metadata.expected_scope,
                    actual_scope: None,
                    scope_passed: false,
                    prompt_assembled: false,
                    sanitizer_result: None,
                    message_check: None,
                    assertion_failures: Vec::new(),
                    error: Some(format!("Failed to load diff.patch: {}", e)),
                };
            }
        };

        // Load optional config overrides
        let config = self.load_config(fixture_dir);

        // Parse diff into StagedChanges
        let changes = Self::parse_diff_to_changes(&diff_content);

        // Load optional symbols from symbols.toml
        let symbols = self.load_symbols(fixture_dir);

        // Run context builder with injected symbols
        let context = ContextBuilder::build(&changes, &symbols, &[], &config);

        // Check type inference
        let actual_type = context.suggested_type.as_str().to_string();
        let type_passed = actual_type.eq_ignore_ascii_case(&metadata.expected_type);

        // Check scope inference
        let actual_scope = context.suggested_scope.clone();
        let scope_passed = match &metadata.expected_scope {
            Some(expected) if expected == "optional" => true, // Any scope is fine
            Some(expected) => actual_scope.as_deref() == Some(expected.as_str()),
            None => true, // No expectation
        };

        // Check prompt assembly
        let prompt = context.to_prompt();
        let prompt_assembled = !prompt.is_empty() && prompt.contains("SUMMARY:");

        // Run new assertion checks
        let mut assertion_failures = Vec::new();
        Self::check_evidence(&metadata, &context, &mut assertion_failures);
        Self::check_prompt_content(&metadata, &prompt, &mut assertion_failures);
        Self::check_connections(&metadata, &context, &mut assertion_failures);
        Self::check_breaking(&metadata, &context, &mut assertion_failures);

        // Check sanitizer if response.json exists
        let sanitizer_result = self.check_sanitizer(fixture_dir, &config);

        // Check subject assertions against sanitized message
        Self::check_subject(&metadata, &sanitizer_result, &mut assertion_failures);

        // Check expected message if expected.txt exists
        let message_check = self.check_expected_message(fixture_dir, &sanitizer_result);

        EvalResult {
            fixture_name: metadata.name,
            description: metadata.description,
            expected_type: metadata.expected_type,
            actual_type,
            type_passed,
            expected_scope: metadata.expected_scope,
            actual_scope,
            scope_passed,
            prompt_assembled,
            sanitizer_result,
            message_check,
            assertion_failures,
            error: None,
        }
    }

    fn load_metadata(&self, fixture_dir: &Path) -> Result<FixtureMetadata> {
        let content = std::fs::read_to_string(fixture_dir.join("metadata.toml"))
            .map_err(|e| Error::Config(format!("Cannot read metadata.toml: {}", e)))?;
        toml::from_str(&content).map_err(|e| Error::Config(format!("Invalid metadata.toml: {}", e)))
    }

    fn load_config(&self, fixture_dir: &Path) -> Config {
        let config_path = fixture_dir.join("config.toml");
        if config_path.exists()
            && let Ok(content) = std::fs::read_to_string(&config_path)
            && let Ok(config) = toml::from_str::<Config>(&content)
        {
            return config;
        }
        Config::default()
    }

    /// Load symbols from `symbols.toml` if it exists, converting to `Vec<CodeSymbol>`.
    fn load_symbols(&self, fixture_dir: &Path) -> Vec<CodeSymbol> {
        let symbols_path = fixture_dir.join("symbols.toml");
        if !symbols_path.exists() {
            return Vec::new();
        }

        let content = match std::fs::read_to_string(&symbols_path) {
            Ok(c) => c,
            Err(_) => return Vec::new(),
        };

        let symbols_file: SymbolsFile = match toml::from_str(&content) {
            Ok(s) => s,
            Err(e) => {
                eprintln!(
                    "{} Failed to parse symbols.toml: {}",
                    style("warning:").yellow().bold(),
                    e
                );
                return Vec::new();
            }
        };

        symbols_file
            .symbols
            .into_iter()
            .map(|def| CodeSymbol {
                kind: parse_symbol_kind(&def.kind),
                name: def.name,
                file: PathBuf::from(def.file),
                line: def.line,
                end_line: def.end_line,
                is_public: def.is_public,
                is_added: def.is_added,
                is_whitespace_only: def.is_whitespace_only,
                span_change_kind: None,
                signature: def.signature,
                parent_scope: def.parent_scope,
            })
            .collect()
    }

    /// Check evidence flag assertions.
    fn check_evidence(
        metadata: &FixtureMetadata,
        context: &crate::domain::PromptContext,
        failures: &mut Vec<AssertionFailure>,
    ) {
        let Some(ref evidence) = metadata.evidence else {
            return;
        };

        if let Some(expected) = evidence.is_mechanical
            && context.is_mechanical != expected
        {
            failures.push(AssertionFailure {
                category: "evidence".to_string(),
                message: format!(
                    "is_mechanical: expected={}, actual={}",
                    expected, context.is_mechanical
                ),
            });
        }

        if let Some(expected) = evidence.has_bug_evidence
            && context.has_bug_evidence != expected
        {
            failures.push(AssertionFailure {
                category: "evidence".to_string(),
                message: format!(
                    "has_bug_evidence: expected={}, actual={}",
                    expected, context.has_bug_evidence
                ),
            });
        }

        if let Some(expected) = evidence.has_new_public_api
            && context.has_new_public_api != expected
        {
            failures.push(AssertionFailure {
                category: "evidence".to_string(),
                message: format!(
                    "has_new_public_api: expected={}, actual={}",
                    expected, context.has_new_public_api
                ),
            });
        }

        if let Some(expected) = evidence.public_api_removed_count
            && context.public_api_removed_count != expected
        {
            failures.push(AssertionFailure {
                category: "evidence".to_string(),
                message: format!(
                    "public_api_removed_count: expected={}, actual={}",
                    expected, context.public_api_removed_count
                ),
            });
        }

        if let Some(expected) = evidence.is_dependency_only
            && context.is_dependency_only != expected
        {
            failures.push(AssertionFailure {
                category: "evidence".to_string(),
                message: format!(
                    "is_dependency_only: expected={}, actual={}",
                    expected, context.is_dependency_only
                ),
            });
        }
    }

    /// Check prompt content assertions (must_contain / must_not_contain).
    fn check_prompt_content(
        metadata: &FixtureMetadata,
        prompt: &str,
        failures: &mut Vec<AssertionFailure>,
    ) {
        let Some(ref prompt_exp) = metadata.prompt else {
            return;
        };

        for pattern in &prompt_exp.must_contain {
            if !prompt.contains(pattern.as_str()) {
                failures.push(AssertionFailure {
                    category: "prompt".to_string(),
                    message: format!("must_contain not found: \"{}\"", pattern),
                });
            }
        }

        for pattern in &prompt_exp.must_not_contain {
            if prompt.contains(pattern.as_str()) {
                failures.push(AssertionFailure {
                    category: "prompt".to_string(),
                    message: format!("must_not_contain was found: \"{}\"", pattern),
                });
            }
        }
    }

    /// Check cross-file connection assertions.
    fn check_connections(
        metadata: &FixtureMetadata,
        context: &crate::domain::PromptContext,
        failures: &mut Vec<AssertionFailure>,
    ) {
        let Some(ref conn_exp) = metadata.connections else {
            return;
        };

        if let Some(expected_count) = conn_exp.expected_count
            && context.connections.len() != expected_count
        {
            failures.push(AssertionFailure {
                category: "connections".to_string(),
                message: format!(
                    "expected_count={}, actual={}",
                    expected_count,
                    context.connections.len()
                ),
            });
        }

        if let Some(min_count) = conn_exp.min_count
            && context.connections.len() < min_count
        {
            failures.push(AssertionFailure {
                category: "connections".to_string(),
                message: format!(
                    "min_count={}, actual={}",
                    min_count,
                    context.connections.len()
                ),
            });
        }

        let connections_text = context.connections.join(" ");
        for pattern in &conn_exp.must_contain {
            if !connections_text.contains(pattern.as_str()) {
                failures.push(AssertionFailure {
                    category: "connections".to_string(),
                    message: format!(
                        "must_contain not found: \"{}\"\n  actual connections: {:?}",
                        pattern, context.connections
                    ),
                });
            }
        }
    }

    /// Check breaking change assertions.
    fn check_breaking(
        metadata: &FixtureMetadata,
        context: &crate::domain::PromptContext,
        failures: &mut Vec<AssertionFailure>,
    ) {
        let Some(ref breaking_exp) = metadata.breaking else {
            return;
        };

        if let Some(expected) = breaking_exp.expected {
            let has_breaking_signals = !context.metadata_breaking_signals.is_empty()
                || context.public_api_removed_count > 0;

            if expected && !has_breaking_signals {
                failures.push(AssertionFailure {
                    category: "breaking".to_string(),
                    message: "expected breaking signals, but none detected".to_string(),
                });
            } else if !expected && has_breaking_signals {
                failures.push(AssertionFailure {
                    category: "breaking".to_string(),
                    message: format!(
                        "expected no breaking signals, but found: metadata_signals={:?}, public_api_removed={}",
                        context.metadata_breaking_signals, context.public_api_removed_count
                    ),
                });
            }
        }
    }

    /// Check subject line assertions against the sanitized message.
    fn check_subject(
        metadata: &FixtureMetadata,
        sanitizer_result: &Option<SanitizerCheck>,
        failures: &mut Vec<AssertionFailure>,
    ) {
        let Some(ref subject_exp) = metadata.subject else {
            return;
        };

        // Subject checks only apply if we have a sanitized message
        let Some(sanitizer) = sanitizer_result else {
            return;
        };
        let Some(ref message) = sanitizer.actual_message else {
            return;
        };

        // Extract subject (first line, after "type(scope): " prefix)
        let first_line = message.lines().next().unwrap_or("");

        // The subject is after ": " in conventional commits
        let subject = first_line
            .find(": ")
            .map(|i| &first_line[i + 2..])
            .unwrap_or(first_line);

        for pattern in &subject_exp.must_contain {
            let lower_subject = subject.to_lowercase();
            let lower_pattern = pattern.to_lowercase();
            if !lower_subject.contains(&lower_pattern) {
                failures.push(AssertionFailure {
                    category: "subject".to_string(),
                    message: format!(
                        "must_contain not found: \"{}\" in subject \"{}\"",
                        pattern, subject
                    ),
                });
            }
        }

        for pattern in &subject_exp.must_not_contain {
            let lower_subject = subject.to_lowercase();
            let lower_pattern = pattern.to_lowercase();
            if lower_subject.contains(&lower_pattern) {
                failures.push(AssertionFailure {
                    category: "subject".to_string(),
                    message: format!(
                        "must_not_contain was found: \"{}\" in subject \"{}\"",
                        pattern, subject
                    ),
                });
            }
        }

        if let Some(max_len) = subject_exp.max_length
            && first_line.len() > max_len
        {
            failures.push(AssertionFailure {
                category: "subject".to_string(),
                message: format!(
                    "first line length {} exceeds max_length {}",
                    first_line.len(),
                    max_len
                ),
            });
        }
    }

    fn check_sanitizer(&self, fixture_dir: &Path, config: &Config) -> Option<SanitizerCheck> {
        let response_path = fixture_dir.join("response.json");
        if !response_path.exists() {
            return None;
        }

        let raw_response = match std::fs::read_to_string(&response_path) {
            Ok(r) => r,
            Err(e) => {
                return Some(SanitizerCheck {
                    passed: false,
                    actual_message: None,
                    error: Some(format!("Failed to read response.json: {}", e)),
                });
            }
        };

        match CommitSanitizer::sanitize(&raw_response, &config.format) {
            Ok(message) => Some(SanitizerCheck {
                passed: true,
                actual_message: Some(message),
                error: None,
            }),
            Err(e) => Some(SanitizerCheck {
                passed: false,
                actual_message: None,
                error: Some(format!("Sanitizer failed: {}", e)),
            }),
        }
    }

    fn check_expected_message(
        &self,
        fixture_dir: &Path,
        sanitizer_result: &Option<SanitizerCheck>,
    ) -> Option<MessageCheck> {
        let expected_path = fixture_dir.join("expected.txt");
        if !expected_path.exists() {
            return None;
        }

        let expected_content = match std::fs::read_to_string(&expected_path) {
            Ok(c) => c,
            Err(_) => return None,
        };

        let expected_first_line = expected_content
            .lines()
            .next()
            .unwrap_or("")
            .trim()
            .to_string();

        let actual_first_line = sanitizer_result
            .as_ref()
            .and_then(|s| s.actual_message.as_ref())
            .and_then(|m| m.lines().next())
            .map(|l| l.to_string());

        let passed = actual_first_line
            .as_deref()
            .is_some_and(|actual| actual == expected_first_line);

        Some(MessageCheck {
            expected_first_line,
            actual_first_line,
            passed,
        })
    }

    /// Parse a unified diff into `StagedChanges` for evaluation.
    ///
    /// This is a simplified parser that extracts file paths, status,
    /// and line counts from a standard unified diff format.
    fn parse_diff_to_changes(diff_content: &str) -> StagedChanges {
        let mut files: Vec<FileChange> = Vec::new();
        let mut current_path: Option<PathBuf> = None;
        let mut current_diff = String::new();
        let mut additions: usize = 0;
        let mut deletions: usize = 0;
        let mut is_new_file = false;
        let mut is_deleted_file = false;

        for line in diff_content.lines() {
            if line.starts_with("diff --git") {
                // Flush previous file
                if let Some(path) = current_path.take() {
                    let status = if is_new_file {
                        ChangeStatus::Added
                    } else if is_deleted_file {
                        ChangeStatus::Deleted
                    } else {
                        ChangeStatus::Modified
                    };

                    files.push(FileChange {
                        category: FileCategory::from_path(&path),
                        path,
                        status,
                        diff: Arc::from(current_diff.as_str()),
                        additions,
                        deletions,
                        is_binary: false,
                        old_path: None,
                        rename_similarity: None,
                    });
                }

                // Parse new file path from "diff --git a/path b/path"
                current_path = line.split(" b/").nth(1).map(|p| PathBuf::from(p.trim()));
                current_diff = String::new();
                additions = 0;
                deletions = 0;
                is_new_file = false;
                is_deleted_file = false;
            } else if line.starts_with("new file mode") {
                is_new_file = true;
            } else if line.starts_with("deleted file mode") {
                is_deleted_file = true;
            } else if line.starts_with('+') && !line.starts_with("+++") {
                additions += 1;
                current_diff.push_str(line);
                current_diff.push('\n');
            } else if line.starts_with('-') && !line.starts_with("---") {
                deletions += 1;
                current_diff.push_str(line);
                current_diff.push('\n');
            } else if line.starts_with(' ') || line.starts_with("@@") {
                current_diff.push_str(line);
                current_diff.push('\n');
            }
        }

        // Flush last file
        if let Some(path) = current_path {
            let status = if is_new_file {
                ChangeStatus::Added
            } else if is_deleted_file {
                ChangeStatus::Deleted
            } else {
                ChangeStatus::Modified
            };

            files.push(FileChange {
                category: FileCategory::from_path(&path),
                path,
                status,
                diff: Arc::from(current_diff.as_str()),
                additions,
                deletions,
                is_binary: false,
                old_path: None,
                rename_similarity: None,
            });
        }

        let stats = DiffStats {
            files_changed: files.len(),
            insertions: files.iter().map(|f| f.additions).sum(),
            deletions: files.iter().map(|f| f.deletions).sum(),
        };

        StagedChanges { files, stats }
    }

    fn print_results(&self, results: &[EvalResult]) {
        for result in results {
            let status = if result.passed() {
                style("PASS").green().bold()
            } else {
                style("FAIL").red().bold()
            };

            eprintln!(
                "  [{}] {}{}",
                status, result.fixture_name, result.description
            );

            // Type inference
            let type_icon = if result.type_passed { "ok" } else { "MISMATCH" };
            eprintln!(
                "    Type: expected={}, actual={} [{}]",
                result.expected_type, result.actual_type, type_icon
            );

            // Scope inference (if expected)
            if let Some(ref expected_scope) = result.expected_scope {
                let scope_icon = if result.scope_passed {
                    "ok"
                } else {
                    "MISMATCH"
                };
                eprintln!(
                    "    Scope: expected={}, actual={} [{}]",
                    expected_scope,
                    result.actual_scope.as_deref().unwrap_or("none"),
                    scope_icon
                );
            }

            // Prompt assembly
            let prompt_icon = if result.prompt_assembled {
                "ok"
            } else {
                "FAIL"
            };
            eprintln!("    Prompt: [{}]", prompt_icon);

            // Assertion failures
            for failure in &result.assertion_failures {
                eprintln!("    {} {}", style("FAIL").red(), failure);
            }

            // Sanitizer check
            if let Some(ref sanitizer) = result.sanitizer_result {
                let san_icon = if sanitizer.passed { "ok" } else { "FAIL" };
                eprintln!("    Sanitizer: [{}]", san_icon);
                if let Some(ref msg) = sanitizer.actual_message {
                    let first_line = msg.lines().next().unwrap_or("");
                    eprintln!("      Output: {}", first_line);
                }
                if let Some(ref err) = sanitizer.error {
                    eprintln!("      Error: {}", err);
                }
            }

            // Message check
            if let Some(ref msg_check) = result.message_check {
                let msg_icon = if msg_check.passed { "ok" } else { "MISMATCH" };
                eprintln!("    Message: [{}]", msg_icon);
                if !msg_check.passed {
                    eprintln!("      Expected: {}", msg_check.expected_first_line);
                    eprintln!(
                        "      Actual:   {}",
                        msg_check.actual_first_line.as_deref().unwrap_or("(none)")
                    );
                }
            }

            // Error
            if let Some(ref err) = result.error {
                eprintln!("    Error: {}", err);
            }

            eprintln!();
        }
    }
}

/// Aggregate evaluation summary with per-type accuracy breakdown.
#[derive(Debug)]
pub struct EvalSummary {
    pub total_fixtures: usize,
    pub total_passed: usize,
    pub total_failed: usize,
    /// Per-type accuracy: (type_name, passed, total).
    pub per_type: Vec<(String, usize, usize)>,
}

impl EvalSummary {
    /// Build a summary from eval results.
    #[must_use]
    pub fn from_results(results: &[EvalResult]) -> Self {
        let total_fixtures = results.len();
        let total_passed = results.iter().filter(|r| r.passed()).count();
        let total_failed = total_fixtures - total_passed;

        // Group by expected_type
        let mut type_map: std::collections::BTreeMap<String, (usize, usize)> =
            std::collections::BTreeMap::new();

        for result in results {
            let key = result.expected_type.to_lowercase();
            if key.is_empty() {
                continue;
            }
            let entry = type_map.entry(key).or_insert((0, 0));
            entry.1 += 1; // total
            if result.passed() {
                entry.0 += 1; // passed
            }
        }

        let per_type: Vec<(String, usize, usize)> = type_map
            .into_iter()
            .map(|(k, (passed, total))| (k, passed, total))
            .collect();

        Self {
            total_fixtures,
            total_passed,
            total_failed,
            per_type,
        }
    }

    /// Format the summary as a human-readable report.
    #[must_use]
    pub fn format_report(&self) -> String {
        let mut report = String::new();

        report.push_str("=== Eval Summary ===\n\n");

        // Per-type breakdown
        report.push_str("Per-type accuracy:\n");
        for (type_name, passed, total) in &self.per_type {
            let pct = if *total > 0 {
                (*passed as f64 / *total as f64) * 100.0
            } else {
                0.0
            };
            report.push_str(&format!(
                "  {}: {}/{} ({:.0}%)\n",
                type_name, passed, total, pct
            ));
        }

        // Overall score
        let overall_pct = if self.total_fixtures > 0 {
            (self.total_passed as f64 / self.total_fixtures as f64) * 100.0
        } else {
            0.0
        };
        report.push_str(&format!(
            "\nOverall: {}/{} ({:.1}%)\n",
            self.total_passed, self.total_fixtures, overall_pct
        ));

        report
    }
}

/// Parse a symbol kind string from TOML into `SymbolKind`.
fn parse_symbol_kind(kind: &str) -> SymbolKind {
    match kind.to_lowercase().as_str() {
        "function" | "fn" => SymbolKind::Function,
        "method" => SymbolKind::Method,
        "struct" => SymbolKind::Struct,
        "enum" => SymbolKind::Enum,
        "trait" => SymbolKind::Trait,
        "impl" => SymbolKind::Impl,
        "class" => SymbolKind::Class,
        "interface" => SymbolKind::Interface,
        "const" | "constant" => SymbolKind::Const,
        "type" => SymbolKind::Type,
        _ => SymbolKind::Function, // Default fallback
    }
}