selfware 0.6.4

Your personal AI workshop — software you own, software that lasts
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
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
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
//! Verification Gates - Automatic validation after every code change
//!
//! Implements the "never proceed on assumptions" protocol:
//! 1. Speculate: Agent proposes an edit
//! 2. Validate: Harness runs checks automatically
//! 3. Feedback: Agent sees results immediately
//! 4. Commit: Only on green, or explicit override

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use std::time::Instant;
use tokio::process::Command;

use crate::tools::cargo::{parse_cargo_json_messages, CompilerError, Severity};

/// Captured result of a reaped verification command (see `run_reaped`).
struct ReapedOutput {
    success: bool,
    stdout: Vec<u8>,
    stderr: Vec<u8>,
}

/// Run a verification command with a timeout, capturing output and reaping the
/// ENTIRE process group on timeout. A hung `cargo check` (or the `rustc`
/// children it spawns) would otherwise stall the agent forever and leave
/// orphaned processes holding `target/` locks — a self-reinforcing stall for
/// unattended runs.
async fn run_reaped(
    program: &str,
    args: &[&str],
    cwd: &Path,
    timeout_secs: u64,
) -> Result<ReapedOutput> {
    use tokio::io::AsyncReadExt;

    let mut cmd = Command::new(program);
    cmd.args(args).current_dir(cwd);
    #[cfg(unix)]
    cmd.process_group(0);
    cmd.stdin(std::process::Stdio::null());
    cmd.stdout(std::process::Stdio::piped());
    cmd.stderr(std::process::Stdio::piped());

    let mut child = cmd
        .spawn()
        .with_context(|| format!("Failed to spawn {}", program))?;
    let pid = child.id();

    let mut so = child.stdout.take();
    let mut se = child.stderr.take();
    let so_task = tokio::spawn(async move {
        let mut b = Vec::new();
        if let Some(ref mut s) = so {
            let _ = s.read_to_end(&mut b).await;
        }
        b
    });
    let se_task = tokio::spawn(async move {
        let mut b = Vec::new();
        if let Some(ref mut s) = se {
            let _ = s.read_to_end(&mut b).await;
        }
        b
    });

    let timeout = tokio::time::Duration::from_secs(timeout_secs.max(1));
    let (success, timed_out) = match tokio::time::timeout(timeout, child.wait()).await {
        Ok(Ok(status)) => (status.success(), false),
        Ok(Err(e)) => return Err(e).with_context(|| format!("{} wait failed", program)),
        Err(_) => {
            #[cfg(unix)]
            if let Some(p) = pid {
                use nix::sys::signal::{killpg, Signal};
                use nix::unistd::Pid;
                let _ = killpg(Pid::from_raw(p as i32), Signal::SIGKILL);
            }
            let _ = child.kill().await;
            let _ = child.wait().await;
            (false, true)
        }
    };

    let stdout = so_task.await.unwrap_or_default();
    let mut stderr = se_task.await.unwrap_or_default();
    if timed_out {
        stderr.extend_from_slice(
            format!(
                "\n[selfware] {} timed out after {}s and its process group was killed.\n",
                program, timeout_secs
            )
            .as_bytes(),
        );
    }

    Ok(ReapedOutput {
        success,
        stdout,
        stderr,
    })
}

/// Verification result for a single check
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CheckResult {
    pub check_type: CheckType,
    pub passed: bool,
    pub duration_ms: u64,
    pub output: String,
    pub errors: Vec<VerificationError>,
    pub warnings: Vec<String>,
    pub suggestions: Vec<String>,
}

/// Types of verification checks
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CheckType {
    /// Rust type checking (cargo check)
    TypeCheck,
    /// Run tests (cargo test)
    Test,
    /// Linting (cargo clippy)
    Lint,
    /// Formatting check (cargo fmt --check)
    Format,
    /// Custom command
    Custom,
}

impl CheckType {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::TypeCheck => "type_check",
            Self::Test => "test",
            Self::Lint => "lint",
            Self::Format => "format",
            Self::Custom => "custom",
        }
    }
}

/// A verification error with location info
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VerificationError {
    pub file: String,
    pub line: Option<u32>,
    pub column: Option<u32>,
    pub message: String,
    pub code: Option<String>,
    pub severity: ErrorSeverity,
    pub suggestion: Option<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ErrorSeverity {
    Error,
    Warning,
    Note,
    Help,
}

/// Detected repository language for verification dispatch.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RepoLanguage {
    Python,
    JavaScript,
    TypeScript,
    Java,
    CSharp,
    Cpp,
    Sql,
    Go,
    Swift,
    Rust,
    Unknown,
}

impl RepoLanguage {
    /// File extensions associated with this language.
    pub fn extensions(&self) -> &'static [&'static str] {
        match self {
            Self::Python => &[".py"],
            Self::JavaScript => &[".js", ".jsx"],
            Self::TypeScript => &[".ts", ".tsx"],
            Self::Java => &[".java"],
            Self::CSharp => &[".cs"],
            Self::Cpp => &[".c", ".cc", ".cpp", ".cxx", ".h", ".hh", ".hpp"],
            Self::Sql => &[".sql"],
            Self::Go => &[".go"],
            Self::Swift => &[".swift"],
            Self::Rust => &[".rs"],
            Self::Unknown => &[],
        }
    }

    /// Convert from a file extension.
    pub fn from_extension(ext: &str) -> Option<Self> {
        match ext {
            ".py" => Some(Self::Python),
            ".js" | ".jsx" => Some(Self::JavaScript),
            ".ts" | ".tsx" => Some(Self::TypeScript),
            ".java" => Some(Self::Java),
            ".cs" => Some(Self::CSharp),
            ".c" | ".cc" | ".cpp" | ".cxx" | ".h" | ".hh" | ".hpp" => Some(Self::Cpp),
            ".sql" => Some(Self::Sql),
            ".go" => Some(Self::Go),
            ".swift" => Some(Self::Swift),
            ".rs" => Some(Self::Rust),
            _ => None,
        }
    }

    /// Convert from a manifest file name.
    pub fn from_manifest(name: &str) -> Option<Self> {
        match name {
            "setup.py" | "pyproject.toml" | "requirements.txt" => Some(Self::Python),
            "package.json" => Some(Self::JavaScript), // may be upgraded to TypeScript
            "tsconfig.json" => Some(Self::TypeScript),
            "pom.xml" | "build.gradle" | "build.gradle.kts" => Some(Self::Java),
            "Package.swift" => Some(Self::Swift),
            "go.mod" => Some(Self::Go),
            "Cargo.toml" => Some(Self::Rust),
            _ => None,
        }
    }
}

impl std::fmt::Display for RepoLanguage {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Python => write!(f, "python"),
            Self::JavaScript => write!(f, "javascript"),
            Self::TypeScript => write!(f, "typescript"),
            Self::Java => write!(f, "java"),
            Self::CSharp => write!(f, "csharp"),
            Self::Cpp => write!(f, "cpp"),
            Self::Sql => write!(f, "sql"),
            Self::Go => write!(f, "go"),
            Self::Swift => write!(f, "swift"),
            Self::Rust => write!(f, "rust"),
            Self::Unknown => write!(f, "unknown"),
        }
    }
}

/// Per-language verification settings.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LanguageCheckSet {
    #[serde(default = "crate::config::types::default_true")]
    pub syntax: bool,
    #[serde(default = "crate::config::types::default_true")]
    pub format: bool,
    #[serde(default = "crate::config::types::default_true")]
    pub lint: bool,
    #[serde(default = "crate::config::types::default_true")]
    pub test: bool,
}

impl Default for LanguageCheckSet {
    fn default() -> Self {
        Self {
            syntax: true,
            format: true,
            lint: true,
            test: true,
        }
    }
}

/// Complete verification report after a change
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VerificationReport {
    pub triggered_by: String,
    pub timestamp: chrono::DateTime<chrono::Utc>,
    pub total_duration_ms: u64,
    pub checks: Vec<CheckResult>,
    pub overall_passed: bool,
    pub affected_files: Vec<String>,
    pub side_effects: Vec<SideEffect>,
    pub suggested_next_steps: Vec<String>,
}

/// Side effects detected from the change
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SideEffect {
    pub effect_type: SideEffectType,
    pub description: String,
    pub files: Vec<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SideEffectType {
    FileCreated,
    FileModified,
    FileDeleted,
    DependencyAdded,
    DependencyRemoved,
    TestAdded,
    TestRemoved,
}

/// Configuration for verification gates
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VerificationConfig {
    /// Run type check after every file edit
    pub check_on_edit: bool,
    /// Run tests after every file edit
    pub test_on_edit: bool,
    /// Run clippy after every file edit
    pub lint_on_edit: bool,
    /// Run format check after every file edit
    pub format_on_edit: bool,
    /// Only run checks on affected files (faster but less thorough)
    pub incremental: bool,
    /// Timeout for each check
    pub check_timeout_secs: u64,
    /// Continue running other checks if one fails
    pub continue_on_failure: bool,
    /// Files/patterns to exclude from verification
    pub exclude_patterns: Vec<String>,
    /// Custom verification commands
    pub custom_checks: Vec<CustomCheck>,
    /// Optional SWE-bench official test command. When set, it is run after every
    /// file edit/write in addition to the normal per-language checks.
    #[serde(default)]
    pub post_edit_test_command: Option<String>,
    /// Per-language verification settings. When a language is absent,
    /// all checks default to enabled.
    #[serde(default)]
    pub language_settings: std::collections::HashMap<RepoLanguage, LanguageCheckSet>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CustomCheck {
    pub name: String,
    pub command: String,
    pub args: Vec<String>,
    pub run_on: Vec<String>, // File patterns that trigger this check
}

impl Default for VerificationConfig {
    fn default() -> Self {
        Self {
            check_on_edit: true,
            test_on_edit: false, // Tests can be slow, opt-in
            lint_on_edit: false, // Clippy can be slow, opt-in
            format_on_edit: true,
            incremental: true,
            check_timeout_secs: 60,
            continue_on_failure: true,
            exclude_patterns: vec![
                "*.md".to_string(),
                "*.txt".to_string(),
                "*.json".to_string(),
                "*.toml".to_string(),
            ],
            custom_checks: vec![],
            post_edit_test_command: None,
            language_settings: std::collections::HashMap::new(),
        }
    }
}

impl VerificationConfig {
    /// Fast mode: only type check
    pub fn fast() -> Self {
        Self {
            check_on_edit: true,
            test_on_edit: false,
            lint_on_edit: false,
            format_on_edit: false,
            ..Default::default()
        }
    }

    /// Thorough mode: all checks
    pub fn thorough() -> Self {
        Self {
            check_on_edit: true,
            test_on_edit: true,
            lint_on_edit: true,
            format_on_edit: true,
            ..Default::default()
        }
    }
}

/// The verification gate - runs checks and reports results
pub struct VerificationGate {
    config: VerificationConfig,
    project_root: PathBuf,
    last_results: Option<VerificationReport>,
    /// Cache of file hashes to detect changes and skip redundant verification
    file_hash_cache: std::collections::HashMap<String, u64>,
    /// Last verification timestamp for cache TTL
    last_verification_time: Option<std::time::Instant>,
    /// Optional hint from the SWE-bench dataset (e.g. "python").
    repo_language_hint: Option<String>,
    /// Cached inferred language to avoid re-scanning the repo.
    inferred_language_cache: Option<RepoLanguage>,
}

impl VerificationGate {
    pub fn new(project_root: impl AsRef<Path>, config: VerificationConfig) -> Self {
        Self {
            config,
            project_root: project_root.as_ref().to_path_buf(),
            last_results: None,
            file_hash_cache: std::collections::HashMap::new(),
            last_verification_time: None,
            repo_language_hint: None,
            inferred_language_cache: None,
        }
    }

    /// Set a language hint (e.g. from SWE-bench Pro dataset).
    /// Invalidates any cached inference so the hint takes effect.
    pub fn set_repo_language_hint(&mut self, hint: impl Into<String>) {
        self.repo_language_hint = Some(hint.into());
        self.inferred_language_cache = None;
    }

    /// Set the optional command to run automatically after every file edit/write.
    pub fn set_post_edit_test_command(&mut self, command: Option<String>) {
        self.config.post_edit_test_command = command;
    }

    /// Compute hash for a file's content
    fn compute_file_hash(&self, path: &str) -> Option<u64> {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::Hasher;
        use std::io::Read;

        let full_path = self.project_root.join(path);
        let mut file = std::fs::File::open(full_path).ok()?;
        let mut contents = Vec::new();
        file.read_to_end(&mut contents).ok()?;

        let mut hasher = DefaultHasher::new();
        hasher.write(&contents);
        Some(hasher.finish())
    }

    /// Check if files have changed since last verification
    fn have_files_changed(&self, files: &[String]) -> bool {
        // If no previous verification, files are considered changed
        if self.file_hash_cache.is_empty() {
            return true;
        }

        for file in files {
            let current_hash = match self.compute_file_hash(file) {
                Some(h) => h,
                None => return true, // Can't read file, assume changed
            };

            match self.file_hash_cache.get(file) {
                Some(cached_hash) if *cached_hash == current_hash => {
                    // File unchanged
                    continue;
                }
                _ => {
                    // File changed or not in cache
                    return true;
                }
            }
        }

        false // All files unchanged
    }

    /// Update file hash cache with current hashes
    fn update_file_cache(&mut self, files: &[String]) {
        for file in files {
            if let Some(hash) = self.compute_file_hash(file) {
                self.file_hash_cache.insert(file.clone(), hash);
            }
        }
    }

    /// Run verification after a file change
    pub async fn verify_change(
        &mut self,
        changed_files: &[String],
        trigger: &str,
    ) -> Result<VerificationReport> {
        let start = Instant::now();
        let mut checks = Vec::new();
        let mut suggested_next_steps = Vec::new();
        let mut overall_passed = true;

        // Filter out excluded files
        let files_to_check: Vec<_> = changed_files
            .iter()
            .filter(|f| !self.is_excluded(f))
            .cloned()
            .collect();

        if files_to_check.is_empty() {
            return Ok(VerificationReport {
                triggered_by: trigger.to_string(),
                timestamp: chrono::Utc::now(),
                total_duration_ms: 0,
                checks: vec![],
                overall_passed: true,
                affected_files: changed_files.to_vec(),
                side_effects: vec![],
                suggested_next_steps: vec![
                    "No code files changed, verification skipped".to_string()
                ],
            });
        }

        // Check if files have actually changed (cache optimization)
        if !self.have_files_changed(&files_to_check) {
            // Return cached result if available
            if let Some(ref last_report) = self.last_results {
                if last_report.overall_passed {
                    return Ok(VerificationReport {
                        triggered_by: format!("{} (cached)", trigger),
                        timestamp: chrono::Utc::now(),
                        total_duration_ms: 0,
                        checks: last_report.checks.clone(),
                        overall_passed: true,
                        affected_files: changed_files.to_vec(),
                        side_effects: vec![],
                        suggested_next_steps: vec![
                            "Files unchanged - using cached verification results".to_string(),
                        ],
                    });
                }
            }
        }

        // Group changed files by language
        let mut files_by_lang: std::collections::HashMap<RepoLanguage, Vec<String>> =
            std::collections::HashMap::new();
        for file in &files_to_check {
            if let Some(ext) = Path::new(file).extension().and_then(|e| e.to_str()) {
                let ext = format!(".{}", ext);
                if let Some(lang) = RepoLanguage::from_extension(&ext) {
                    files_by_lang.entry(lang).or_default().push(file.clone());
                }
            }
        }

        // Run cheap syntax checks first for all touched languages
        if self.config.check_on_edit {
            for (lang, files) in &files_by_lang {
                let settings = self.get_language_settings(*lang);
                if settings.syntax {
                    let result = self.run_cheap_syntax_check(*lang, files).await?;
                    if !result.passed {
                        suggested_next_steps
                            .push(format!("Fix {} syntax errors before proceeding", lang));
                    }
                    checks.push(result);
                }
            }
        }

        // Run optional post-edit test command (e.g., SWE-bench official tests)
        if let Some(ref cmd) = self.config.post_edit_test_command {
            let check_start = Instant::now();
            let timeout_secs = self.config.check_timeout_secs.max(60);
            let timeout_duration = tokio::time::Duration::from_secs(timeout_secs);

            let mut parsed = shlex::split(cmd).unwrap_or_default();
            let (passed, output, duration_ms) = if parsed.is_empty() {
                (
                    false,
                    format!("Empty or unparseable post-edit test command: {}", cmd),
                    check_start.elapsed().as_millis() as u64,
                )
            } else {
                let program = parsed.remove(0);
                let command_future = Command::new(&program)
                    .args(&parsed)
                    .current_dir(&self.project_root)
                    .output();

                match tokio::time::timeout(timeout_duration, command_future).await {
                    Ok(Ok(output)) => {
                        let stdout = String::from_utf8_lossy(&output.stdout);
                        let stderr = String::from_utf8_lossy(&output.stderr);
                        let combined = if stderr.is_empty() {
                            stdout.to_string()
                        } else {
                            format!("{}\n{}", stdout, stderr)
                        };
                        (
                            output.status.success(),
                            truncate_str(&combined, 4000),
                            check_start.elapsed().as_millis() as u64,
                        )
                    }
                    Ok(Err(e)) => (
                        false,
                        format!("Failed to run post-edit test command '{}': {}", cmd, e),
                        check_start.elapsed().as_millis() as u64,
                    ),
                    Err(_) => (
                        false,
                        format!(
                            "Post-edit test command '{}' timed out after {} seconds",
                            cmd, timeout_secs
                        ),
                        timeout_duration.as_millis() as u64,
                    ),
                }
            };

            if !passed {
                overall_passed = false;
                suggested_next_steps.push(format!(
                    "The post-edit test command failed: {}. Fix the failing test before completing.",
                    cmd
                ));
            }
            checks.push(CheckResult {
                check_type: CheckType::Test,
                passed,
                duration_ms,
                output,
                errors: vec![],
                warnings: vec![],
                suggestions: vec![],
            });
        }

        // Early exit if syntax checks failed and continue_on_failure is false
        if !self.config.continue_on_failure && checks.iter().any(|c| !c.passed) {
            let overall_passed = false;
            let total_duration = start.elapsed().as_millis() as u64;
            let side_effects = self.detect_side_effects(&files_to_check).await;
            let report = VerificationReport {
                triggered_by: trigger.to_string(),
                timestamp: chrono::Utc::now(),
                total_duration_ms: total_duration,
                checks,
                overall_passed,
                affected_files: files_to_check,
                side_effects,
                suggested_next_steps,
            };
            self.last_results = Some(report.clone());
            self.update_file_cache(&report.affected_files);
            self.last_verification_time = Some(std::time::Instant::now());
            return Ok(report);
        }

        // Detect if any Rust files changed
        let rust_files_changed = files_to_check.iter().any(|f| f.ends_with(".rs"));

        if rust_files_changed {
            // Run type check
            if self.config.check_on_edit {
                let result = self.run_cargo_check().await?;
                if !result.passed {
                    suggested_next_steps.push("Fix type errors before proceeding".to_string());
                }
                checks.push(result);
            }

            // Run format check
            if self.config.format_on_edit {
                let result = self.run_cargo_fmt_check().await?;
                if !result.passed {
                    suggested_next_steps.push("Run cargo fmt to fix formatting".to_string());
                }
                checks.push(result);
            }

            // Run tests (if enabled)
            if self.config.test_on_edit {
                let result = self.run_cargo_test().await?;
                if !result.passed {
                    suggested_next_steps.push("Fix failing tests".to_string());
                }
                checks.push(result);
            }

            // Run clippy (if enabled)
            if self.config.lint_on_edit {
                let result = self.run_cargo_clippy().await?;
                if !result.passed {
                    suggested_next_steps.push("Address clippy warnings".to_string());
                }
                checks.push(result);
            }
        }

        // Targeted tests for non-Rust languages
        if self.config.test_on_edit {
            for (lang, files) in &files_by_lang {
                if *lang == RepoLanguage::Rust {
                    continue;
                }
                let settings = self.get_language_settings(*lang);
                if settings.test {
                    let result = self.run_targeted_test(*lang, files).await?;
                    if !result.passed {
                        suggested_next_steps.push(format!("Fix failing {} tests", lang));
                    }
                    checks.push(result);
                }
            }
        }

        // Multi-language QA: dispatch to language_qa runners for non-Rust files
        let has_non_rust = files_by_lang.keys().any(|l| *l != RepoLanguage::Rust);
        if has_non_rust {
            use crate::testing::language_qa::{run_go_qa, run_node_qa, run_python_qa, QaLanguage};

            let detected_lang = QaLanguage::detect(&self.project_root);
            let timeout = self.config.check_timeout_secs;

            let has_python = files_by_lang.contains_key(&RepoLanguage::Python);
            let has_js = files_by_lang.contains_key(&RepoLanguage::JavaScript)
                || files_by_lang.contains_key(&RepoLanguage::TypeScript);
            let has_go = files_by_lang.contains_key(&RepoLanguage::Go);

            let qa_results = match detected_lang {
                QaLanguage::Python if has_python => {
                    run_python_qa(&self.project_root, timeout).await
                }
                QaLanguage::Node if has_js => run_node_qa(&self.project_root, timeout).await,
                QaLanguage::Go if has_go => run_go_qa(&self.project_root, timeout).await,
                _ => Vec::new(),
            };

            for qa_stage in qa_results {
                let check = Self::qa_stage_to_check_result(qa_stage);
                if !check.passed {
                    suggested_next_steps.push(format!(
                        "Fix {} {} errors",
                        detected_lang,
                        check.check_type.as_str()
                    ));
                }
                checks.push(check);
            }
        }

        // Run custom checks
        for custom in &self.config.custom_checks {
            if self.should_run_custom_check(custom, &files_to_check) {
                let result = self.run_custom_check(custom).await?;
                checks.push(result);
            }
        }

        overall_passed = overall_passed && checks.iter().all(|c| c.passed);
        let total_duration = start.elapsed().as_millis() as u64;

        // Detect side effects
        let side_effects = self.detect_side_effects(&files_to_check).await;

        // Add suggestions based on results
        if overall_passed && suggested_next_steps.is_empty() {
            suggested_next_steps.push("All checks passed - safe to proceed".to_string());
        }

        let report = VerificationReport {
            triggered_by: trigger.to_string(),
            timestamp: chrono::Utc::now(),
            total_duration_ms: total_duration,
            checks,
            overall_passed,
            affected_files: files_to_check,
            side_effects,
            suggested_next_steps,
        };

        self.last_results = Some(report.clone());

        // Update file hash cache for future change detection
        self.update_file_cache(&report.affected_files);
        self.last_verification_time = Some(std::time::Instant::now());

        Ok(report)
    }

    /// Quick verification - just type check
    pub async fn quick_verify(&mut self, _changed_files: &[String]) -> Result<bool> {
        let result = self.run_cargo_check().await?;
        Ok(result.passed)
    }

    /// Full verification - all checks
    pub async fn full_verify(&mut self) -> Result<VerificationReport> {
        // Do NOT delegate with an empty file list: verify_change early-returns
        // overall_passed=true with zero checks for empty input, so a "full
        // verification" would pass vacuously without running anything. Run the
        // crate-wide checks the config enables.
        let start = Instant::now();
        let mut checks = Vec::new();
        if self.config.check_on_edit {
            checks.push(self.run_cargo_check().await?);
        }
        if self.config.lint_on_edit {
            checks.push(self.run_cargo_clippy().await?);
        }
        if self.config.test_on_edit {
            checks.push(self.run_cargo_test().await?);
        }
        let overall_passed = checks.iter().all(|c| c.passed);
        let suggested_next_steps = if checks.is_empty() {
            vec!["No verification checks enabled in config".to_string()]
        } else {
            vec![]
        };
        Ok(VerificationReport {
            triggered_by: "full_verification".to_string(),
            timestamp: chrono::Utc::now(),
            total_duration_ms: start.elapsed().as_millis() as u64,
            overall_passed,
            affected_files: vec![],
            side_effects: vec![],
            checks,
            suggested_next_steps,
        })
    }

    /// Run cargo check
    async fn run_cargo_check(&self) -> Result<CheckResult> {
        let start = Instant::now();

        let output = run_reaped(
            "cargo",
            &["check", "--message-format=json"],
            &self.project_root,
            self.config.check_timeout_secs,
        )
        .await?;

        let duration = start.elapsed().as_millis() as u64;
        let stdout = String::from_utf8_lossy(&output.stdout);
        let stderr = String::from_utf8_lossy(&output.stderr);

        let (errors, warnings) = parse_cargo_json_output(&stdout);

        Ok(CheckResult {
            check_type: CheckType::TypeCheck,
            passed: output.success,
            duration_ms: duration,
            output: if output.success {
                "Type check passed".to_string()
            } else {
                stderr.to_string()
            },
            errors,
            warnings: warnings.iter().map(|e| e.message.clone()).collect(),
            suggestions: vec![],
        })
    }

    /// Run cargo fmt --check
    async fn run_cargo_fmt_check(&self) -> Result<CheckResult> {
        let start = Instant::now();

        let output = run_reaped(
            "cargo",
            &["fmt", "--check"],
            &self.project_root,
            self.config.check_timeout_secs,
        )
        .await?;

        let duration = start.elapsed().as_millis() as u64;
        let stdout = String::from_utf8_lossy(&output.stdout);

        Ok(CheckResult {
            check_type: CheckType::Format,
            passed: output.success,
            duration_ms: duration,
            output: if output.success {
                "Formatting check passed".to_string()
            } else {
                stdout.to_string()
            },
            errors: vec![],
            warnings: vec![],
            suggestions: if !output.success {
                vec!["Run `cargo fmt` to fix formatting".to_string()]
            } else {
                vec![]
            },
        })
    }

    /// Run cargo test with timeout to prevent getting stuck
    async fn run_cargo_test(&self) -> Result<CheckResult> {
        let start = Instant::now();

        // Apply timeout from config (default 5 minutes)
        let timeout_secs = self.config.check_timeout_secs.max(60); // At least 60 seconds
        let timeout_duration = tokio::time::Duration::from_secs(timeout_secs);

        let command_future = Command::new("cargo")
            .args(["test", "--no-fail-fast"])
            .current_dir(&self.project_root)
            .output();

        let output = match tokio::time::timeout(timeout_duration, command_future).await {
            Ok(Ok(output)) => output,
            Ok(Err(e)) => return Err(e.into()),
            Err(_) => {
                // Timeout - return a graceful error
                return Ok(CheckResult {
                    check_type: CheckType::Test,
                    passed: false,
                    duration_ms: timeout_duration.as_millis() as u64,
                    output: format!("Tests timed out after {} seconds", timeout_secs),
                    errors: vec![VerificationError {
                        file: "N/A".to_string(),
                        line: None,
                        column: None,
                        message: format!("cargo test exceeded {}s timeout", timeout_secs),
                        code: Some("TIMEOUT".to_string()),
                        severity: ErrorSeverity::Error,
                        suggestion: Some("Tests took too long. Run manually with `cargo test` or increase check_timeout_secs in config".to_string()),
                    }],
                    warnings: vec!["Tests were cancelled due to timeout. Press Ctrl+C to exit if stuck.".to_string()],
                    suggestions: vec!["Consider running tests manually or increasing timeout".to_string()],
                });
            }
        };

        let duration = start.elapsed().as_millis() as u64;
        let stdout = String::from_utf8_lossy(&output.stdout);
        let stderr = String::from_utf8_lossy(&output.stderr);

        // Parse test failures from output
        let errors = parse_test_failures(&stdout, &stderr);

        Ok(CheckResult {
            check_type: CheckType::Test,
            passed: output.status.success(),
            duration_ms: duration,
            output: format!("{}\n{}", stdout, stderr),
            errors,
            warnings: vec![],
            suggestions: vec![],
        })
    }

    /// Run cargo clippy
    async fn run_cargo_clippy(&self) -> Result<CheckResult> {
        let start = Instant::now();

        let output = run_reaped(
            "cargo",
            &["clippy", "--message-format=json", "--", "-D", "warnings"],
            &self.project_root,
            self.config.check_timeout_secs,
        )
        .await?;

        let duration = start.elapsed().as_millis() as u64;
        let stdout = String::from_utf8_lossy(&output.stdout);
        let stderr = String::from_utf8_lossy(&output.stderr);

        let (errors, warnings) = parse_cargo_json_output(&stdout);

        Ok(CheckResult {
            check_type: CheckType::Lint,
            passed: output.success,
            duration_ms: duration,
            output: stderr.to_string(),
            errors,
            warnings: warnings.iter().map(|e| e.message.clone()).collect(),
            suggestions: vec![],
        })
    }

    /// Run a custom check
    async fn run_custom_check(&self, check: &CustomCheck) -> Result<CheckResult> {
        let start = Instant::now();

        let args_ref: Vec<&str> = check.args.iter().map(|s| s.as_str()).collect();
        let output = run_reaped(
            &check.command,
            &args_ref,
            &self.project_root,
            self.config.check_timeout_secs,
        )
        .await?;

        let duration = start.elapsed().as_millis() as u64;
        let stdout = String::from_utf8_lossy(&output.stdout);
        let stderr = String::from_utf8_lossy(&output.stderr);

        Ok(CheckResult {
            check_type: CheckType::Custom,
            passed: output.success,
            duration_ms: duration,
            output: format!("{}\n{}", stdout, stderr),
            errors: vec![],
            warnings: vec![],
            suggestions: vec![],
        })
    }

    /// Check if a file should be excluded from verification
    pub fn is_excluded(&self, file: &str) -> bool {
        for pattern in &self.config.exclude_patterns {
            if let Ok(glob) = glob::Pattern::new(pattern) {
                if glob.matches(file) {
                    return true;
                }
            }
        }
        false
    }

    /// Check if a custom check should run based on changed files
    fn should_run_custom_check(&self, check: &CustomCheck, files: &[String]) -> bool {
        if check.run_on.is_empty() {
            return true;
        }
        for pattern in &check.run_on {
            if let Ok(glob) = glob::Pattern::new(pattern) {
                if files.iter().any(|f| glob.matches(f)) {
                    return true;
                }
            }
        }
        false
    }

    /// Convert a QA stage result from language_qa into a CheckResult.
    fn qa_stage_to_check_result(stage: crate::testing::qa_profiles::QaStageResult) -> CheckResult {
        use crate::testing::qa_profiles::QaStage;
        let check_type = match stage.stage {
            QaStage::Syntax | QaStage::TypeCheck => CheckType::TypeCheck,
            QaStage::Format => CheckType::Format,
            QaStage::Lint => CheckType::Lint,
            QaStage::Test => CheckType::Test,
            QaStage::Security => CheckType::Custom,
        };
        CheckResult {
            check_type,
            passed: stage.passed,
            duration_ms: stage.duration_ms,
            output: stage.output,
            errors: vec![],
            warnings: vec![],
            suggestions: vec![],
        }
    }

    /// Detect side effects from file changes
    async fn detect_side_effects(&self, files: &[String]) -> Vec<SideEffect> {
        let mut effects = Vec::new();

        for file in files {
            // Check if it's a new file
            let path = self.project_root.join(file);
            if path.exists() {
                effects.push(SideEffect {
                    effect_type: SideEffectType::FileModified,
                    description: format!("Modified: {}", file),
                    files: vec![file.clone()],
                });
            }

            // Check for test files (language-agnostic)
            if file.contains("test")
                || file.contains("_test.rs")
                || file.contains("_test.py")
                || file.contains("_test.go")
                || file.contains(".test.js")
                || file.contains(".test.ts")
            {
                effects.push(SideEffect {
                    effect_type: SideEffectType::TestAdded,
                    description: "Test file modified".to_string(),
                    files: vec![file.clone()],
                });
            }
        }

        // Check manifest files for dependency changes
        if files.iter().any(|f| f.ends_with("Cargo.toml")) {
            effects.push(SideEffect {
                effect_type: SideEffectType::DependencyAdded,
                description: "Cargo.toml modified - dependencies may have changed".to_string(),
                files: vec!["Cargo.toml".to_string()],
            });
        }
        if files.iter().any(|f| f.ends_with("package.json")) {
            effects.push(SideEffect {
                effect_type: SideEffectType::DependencyAdded,
                description: "package.json modified - dependencies may have changed".to_string(),
                files: vec!["package.json".to_string()],
            });
        }
        if files.iter().any(|f| f.ends_with("go.mod")) {
            effects.push(SideEffect {
                effect_type: SideEffectType::DependencyAdded,
                description: "go.mod modified - dependencies may have changed".to_string(),
                files: vec!["go.mod".to_string()],
            });
        }
        if files
            .iter()
            .any(|f| f.ends_with("requirements.txt") || f.ends_with("pyproject.toml"))
        {
            effects.push(SideEffect {
                effect_type: SideEffectType::DependencyAdded,
                description: "Python manifest modified - dependencies may have changed".to_string(),
                files: files
                    .iter()
                    .filter(|f| f.ends_with("requirements.txt") || f.ends_with("pyproject.toml"))
                    .cloned()
                    .collect(),
            });
        }

        effects
    }

    /// Get the last verification results
    pub fn last_results(&self) -> Option<&VerificationReport> {
        self.last_results.as_ref()
    }

    /// Infer the primary repository language using multiple signals:
    /// 1. SWE-bench `repo_language` hint (if set)
    /// 2. Manifest files (Cargo.toml, package.json, go.mod, etc.)
    /// 3. File extensions in the repo
    ///
    /// Result is cached per workdir.
    #[cfg(test)]
    fn infer_repo_language(&mut self) -> RepoLanguage {
        if let Some(cached) = self.inferred_language_cache {
            return cached;
        }

        // 1. Use dataset hint if available
        if let Some(ref hint) = self.repo_language_hint {
            let lang = match hint.to_lowercase().as_str() {
                "rust" => RepoLanguage::Rust,
                "python" => RepoLanguage::Python,
                "javascript" | "js" => RepoLanguage::JavaScript,
                "typescript" | "ts" => RepoLanguage::TypeScript,
                "java" => RepoLanguage::Java,
                "csharp" | "c#" => RepoLanguage::CSharp,
                "cpp" | "c++" | "c" => RepoLanguage::Cpp,
                "sql" => RepoLanguage::Sql,
                "go" | "golang" => RepoLanguage::Go,
                "swift" => RepoLanguage::Swift,
                _ => RepoLanguage::Unknown,
            };
            if lang != RepoLanguage::Unknown {
                self.inferred_language_cache = Some(lang);
                return lang;
            }
        }

        // 2. Check for manifest files
        let manifests = [
            ("Cargo.toml", RepoLanguage::Rust),
            ("go.mod", RepoLanguage::Go),
            ("Package.swift", RepoLanguage::Swift),
            ("pom.xml", RepoLanguage::Java),
            ("build.gradle", RepoLanguage::Java),
            ("build.gradle.kts", RepoLanguage::Java),
            ("tsconfig.json", RepoLanguage::TypeScript),
            ("package.json", RepoLanguage::JavaScript),
            ("setup.py", RepoLanguage::Python),
            ("pyproject.toml", RepoLanguage::Python),
            ("requirements.txt", RepoLanguage::Python),
        ];
        for (file, lang) in &manifests {
            if self.project_root.join(file).exists() {
                if *lang == RepoLanguage::JavaScript
                    && self.project_root.join("tsconfig.json").exists()
                {
                    self.inferred_language_cache = Some(RepoLanguage::TypeScript);
                    return RepoLanguage::TypeScript;
                }
                self.inferred_language_cache = Some(*lang);
                return *lang;
            }
        }

        // 3. Fall back to file extension counting
        let counts = scan_repo_extensions(&self.project_root, 100);
        let best = counts
            .iter()
            .max_by_key(|(_, count)| *count)
            .map(|(lang, _)| *lang);
        let result = best.unwrap_or(RepoLanguage::Unknown);
        self.inferred_language_cache = Some(result);
        result
    }

    /// Retrieve per-language settings, falling back to defaults.
    fn get_language_settings(&self, lang: RepoLanguage) -> LanguageCheckSet {
        self.config
            .language_settings
            .get(&lang)
            .cloned()
            .unwrap_or_default()
    }

    /// Run a cheap syntax check on ONLY the touched files.
    async fn run_cheap_syntax_check(
        &self,
        lang: RepoLanguage,
        files: &[String],
    ) -> Result<CheckResult> {
        let start = Instant::now();
        let full_paths: Vec<_> = files.iter().map(|f| self.project_root.join(f)).collect();

        let (program, args): (&str, Vec<String>) = match lang {
            RepoLanguage::Python => {
                let mut a = vec!["-m".to_string(), "py_compile".to_string()];
                for p in &full_paths {
                    a.push(p.to_string_lossy().to_string());
                }
                ("python3", a)
            }
            RepoLanguage::JavaScript => {
                if let Some(p) = full_paths.first() {
                    (
                        "node",
                        vec!["--check".to_string(), p.to_string_lossy().to_string()],
                    )
                } else {
                    return Ok(CheckResult {
                        check_type: CheckType::TypeCheck,
                        passed: true,
                        duration_ms: 0,
                        output: "No JS files to check".to_string(),
                        errors: vec![],
                        warnings: vec![],
                        suggestions: vec![],
                    });
                }
            }
            RepoLanguage::TypeScript => {
                let mut a = vec!["tsc".to_string(), "--noEmit".to_string()];
                for p in &full_paths {
                    a.push(p.to_string_lossy().to_string());
                }
                ("npx", a)
            }
            RepoLanguage::Java => {
                let mut a = vec![
                    "-Xlint:none".to_string(),
                    "-d".to_string(),
                    "/tmp".to_string(),
                ];
                for p in &full_paths {
                    a.push(p.to_string_lossy().to_string());
                }
                ("javac", a)
            }
            RepoLanguage::CSharp => {
                if self.project_root.join("global.json").exists()
                    || self
                        .project_root
                        .read_dir()
                        .ok()
                        .into_iter()
                        .flatten()
                        .flatten()
                        .any(|e| {
                            e.path()
                                .extension()
                                .and_then(|x| x.to_str())
                                .is_some_and(|x| matches!(x, "sln" | "csproj"))
                        })
                {
                    ("dotnet", vec!["build".to_string(), "--nologo".to_string()])
                } else {
                    let mut a = vec![
                        "-target:library".to_string(),
                        "-out:/tmp/selfware-csharp-check.dll".to_string(),
                    ];
                    for p in &full_paths {
                        a.push(p.to_string_lossy().to_string());
                    }
                    ("csc", a)
                }
            }
            RepoLanguage::Cpp => {
                if self.project_root.join("CMakeLists.txt").exists() {
                    ("cmake", vec!["--build".to_string(), ".".to_string()])
                } else if let Some(p) = full_paths.first() {
                    let compiler = if p
                        .extension()
                        .and_then(|e| e.to_str())
                        .is_some_and(|e| matches!(e, "c"))
                    {
                        "cc"
                    } else {
                        "c++"
                    };
                    (
                        compiler,
                        vec!["-fsyntax-only".to_string(), p.to_string_lossy().to_string()],
                    )
                } else {
                    return Ok(CheckResult {
                        check_type: CheckType::TypeCheck,
                        passed: true,
                        duration_ms: 0,
                        output: "No C/C++ files to check".to_string(),
                        errors: vec![],
                        warnings: vec![],
                        suggestions: vec![],
                    });
                }
            }
            RepoLanguage::Sql => {
                if self.command_exists("sqlfluff").await {
                    let mut a = vec![
                        "lint".to_string(),
                        "--dialect".to_string(),
                        "ansi".to_string(),
                    ];
                    for p in &full_paths {
                        a.push(p.to_string_lossy().to_string());
                    }
                    ("sqlfluff", a)
                } else {
                    return Ok(CheckResult {
                        check_type: CheckType::TypeCheck,
                        passed: true,
                        duration_ms: 0,
                        output: "sqlfluff not installed; SQL syntax check skipped".to_string(),
                        errors: vec![],
                        warnings: vec![
                            "SQL syntax check skipped: sqlfluff not installed".to_string()
                        ],
                        suggestions: vec![],
                    });
                }
            }
            RepoLanguage::Go => {
                let mut a = vec!["-l".to_string()];
                for p in &full_paths {
                    a.push(p.to_string_lossy().to_string());
                }
                ("gofmt", a)
            }
            RepoLanguage::Swift => {
                if self.project_root.join("Package.swift").exists() {
                    ("swift", vec!["build".to_string()])
                } else if let Some(p) = full_paths.first() {
                    (
                        "swiftc",
                        vec!["-parse".to_string(), p.to_string_lossy().to_string()],
                    )
                } else {
                    return Ok(CheckResult {
                        check_type: CheckType::TypeCheck,
                        passed: true,
                        duration_ms: 0,
                        output: "No Swift files to check".to_string(),
                        errors: vec![],
                        warnings: vec![],
                        suggestions: vec![],
                    });
                }
            }
            RepoLanguage::Rust => {
                let mut a = vec!["--check".to_string()];
                for p in &full_paths {
                    a.push(p.to_string_lossy().to_string());
                }
                ("rustfmt", a)
            }
            RepoLanguage::Unknown => {
                return Ok(CheckResult {
                    check_type: CheckType::TypeCheck,
                    passed: true,
                    duration_ms: 0,
                    output: "Unknown language, skipping syntax check".to_string(),
                    errors: vec![],
                    warnings: vec![],
                    suggestions: vec![],
                });
            }
        };

        if !matches!(program, "sh") && !self.command_exists(program).await {
            return Ok(CheckResult {
                check_type: CheckType::TypeCheck,
                passed: false,
                duration_ms: 0,
                output: format!(
                    "{} syntax check could not run: `{}` not found",
                    lang, program
                ),
                errors: vec![VerificationError {
                    file: files.first().cloned().unwrap_or_default(),
                    line: None,
                    column: None,
                    message: format!("{} verifier `{}` not found", lang, program),
                    code: Some("VERIFIER_NOT_FOUND".to_string()),
                    severity: ErrorSeverity::Error,
                    suggestion: Some(format!(
                        "Install `{}` or run a project-specific verifier",
                        program
                    )),
                }],
                warnings: vec![],
                suggestions: vec![format!(
                    "Run a project-specific {} verification command manually",
                    lang
                )],
            });
        }

        let output = Command::new(program)
            .args(&args)
            .current_dir(&self.project_root)
            .output()
            .await
            .context(format!("Failed to run {} syntax check", lang))?;

        let duration = start.elapsed().as_millis() as u64;
        let stdout = String::from_utf8_lossy(&output.stdout);
        let stderr = String::from_utf8_lossy(&output.stderr);
        let combined = if stderr.is_empty() {
            stdout.to_string()
        } else {
            format!("{}\n{}", stdout, stderr)
        };

        Ok(CheckResult {
            check_type: CheckType::TypeCheck,
            passed: output.status.success(),
            duration_ms: duration,
            output: if output.status.success() {
                format!("{} syntax check passed", lang)
            } else {
                combined
            },
            errors: if output.status.success() {
                vec![]
            } else {
                vec![VerificationError {
                    file: files.first().cloned().unwrap_or_default(),
                    line: None,
                    column: None,
                    message: format!("{} syntax error", lang),
                    code: None,
                    severity: ErrorSeverity::Error,
                    suggestion: Some(format!("Check {} syntax and fix errors", lang)),
                }]
            },
            warnings: vec![],
            suggestions: if output.status.success() {
                vec![]
            } else {
                vec![format!("Fix {} syntax errors before running tests", lang)]
            },
        })
    }

    /// Infer the appropriate test command for the repository.
    async fn infer_test_command(&self, lang: RepoLanguage) -> Option<(String, Vec<String>)> {
        match lang {
            RepoLanguage::Rust => Some((
                "cargo".to_string(),
                vec!["test".to_string(), "--no-fail-fast".to_string()],
            )),
            RepoLanguage::Python => {
                if self.project_root.join("pytest.ini").exists()
                    || self.has_pyproject_pytest()
                    || self.command_exists("pytest").await
                {
                    Some((
                        "pytest".to_string(),
                        vec!["--quiet".to_string(), "--tb=short".to_string()],
                    ))
                } else {
                    Some((
                        "python3".to_string(),
                        vec![
                            "-m".to_string(),
                            "unittest".to_string(),
                            "discover".to_string(),
                            "-s".to_string(),
                            ".".to_string(),
                            "-q".to_string(),
                        ],
                    ))
                }
            }
            RepoLanguage::JavaScript | RepoLanguage::TypeScript => {
                let pm = self.detect_package_manager();
                Some((pm.to_string(), vec!["test".to_string()]))
            }
            RepoLanguage::Java => {
                if self.project_root.join("pom.xml").exists() {
                    Some(("mvn".to_string(), vec!["test".to_string()]))
                } else if self.project_root.join("build.gradle").exists()
                    || self.project_root.join("build.gradle.kts").exists()
                {
                    Some(("gradle".to_string(), vec!["test".to_string()]))
                } else {
                    None
                }
            }
            RepoLanguage::CSharp => Some(("dotnet".to_string(), vec!["test".to_string()])),
            RepoLanguage::Cpp => {
                if self.project_root.join("CMakeLists.txt").exists() {
                    Some(("ctest".to_string(), vec!["--output-on-failure".to_string()]))
                } else {
                    Some(("make".to_string(), vec!["test".to_string()]))
                }
            }
            RepoLanguage::Sql => {
                if self.command_exists("sqlfluff").await {
                    Some((
                        "sqlfluff".to_string(),
                        vec![
                            "lint".to_string(),
                            "--dialect".to_string(),
                            "ansi".to_string(),
                        ],
                    ))
                } else {
                    None
                }
            }
            RepoLanguage::Go => Some((
                "go".to_string(),
                vec!["test".to_string(), "-v".to_string(), "./...".to_string()],
            )),
            RepoLanguage::Swift => Some(("swift".to_string(), vec!["test".to_string()])),
            RepoLanguage::Unknown => None,
        }
    }

    /// Run targeted tests for a specific language.
    async fn run_targeted_test(
        &self,
        lang: RepoLanguage,
        changed_files: &[String],
    ) -> Result<CheckResult> {
        let start = Instant::now();
        let timeout_secs = self.config.check_timeout_secs.max(60);

        let Some((program, mut args)) = self.infer_test_command(lang).await else {
            return Ok(CheckResult {
                check_type: CheckType::Test,
                passed: true,
                duration_ms: 0,
                output: "No test command inferred for unknown language".to_string(),
                errors: vec![],
                warnings: vec![],
                suggestions: vec![],
            });
        };

        // If specific test files were touched, target them when possible
        if lang == RepoLanguage::Python {
            let test_files: Vec<_> = changed_files
                .iter()
                .filter(|f| f.contains("test") && f.ends_with(".py"))
                .cloned()
                .collect();
            if !test_files.is_empty() {
                args.extend(test_files);
            }
        }

        let command_future = Command::new(&program)
            .args(&args)
            .current_dir(&self.project_root)
            .output();

        let output = match tokio::time::timeout(
            tokio::time::Duration::from_secs(timeout_secs),
            command_future,
        )
        .await
        {
            Ok(Ok(output)) => output,
            Ok(Err(e)) => return Err(e.into()),
            Err(_) => {
                return Ok(CheckResult {
                    check_type: CheckType::Test,
                    passed: false,
                    duration_ms: timeout_secs * 1000,
                    output: format!("Tests timed out after {} seconds", timeout_secs),
                    errors: vec![VerificationError {
                        file: "N/A".to_string(),
                        line: None,
                        column: None,
                        message: format!("{} test exceeded {}s timeout", lang, timeout_secs),
                        code: Some("TIMEOUT".to_string()),
                        severity: ErrorSeverity::Error,
                        suggestion: Some(
                            "Tests took too long. Run manually or increase check_timeout_secs in config"
                                .to_string(),
                        ),
                    }],
                    warnings: vec![],
                    suggestions: vec![],
                });
            }
        };

        let duration = start.elapsed().as_millis() as u64;
        let stdout = String::from_utf8_lossy(&output.stdout);
        let stderr = String::from_utf8_lossy(&output.stderr);

        Ok(CheckResult {
            check_type: CheckType::Test,
            passed: output.status.success(),
            duration_ms: duration,
            output: format!("{}\n{}", stdout, stderr),
            errors: vec![],
            warnings: vec![],
            suggestions: vec![],
        })
    }

    async fn command_exists(&self, cmd: &str) -> bool {
        match Command::new("which").arg(cmd).output().await {
            Ok(output) => output.status.success(),
            Err(_) => false,
        }
    }

    fn has_pyproject_pytest(&self) -> bool {
        let path = self.project_root.join("pyproject.toml");
        if !path.exists() {
            return false;
        }
        if let Ok(content) = std::fs::read_to_string(&path) {
            content.contains("[tool.pytest") || content.contains("[tool:pytest")
        } else {
            false
        }
    }

    fn detect_package_manager(&self) -> &'static str {
        if self.project_root.join("pnpm-lock.yaml").exists() {
            "pnpm"
        } else if self.project_root.join("yarn.lock").exists() {
            "yarn"
        } else {
            "npm"
        }
    }
}

/// Scan a repo for file extensions and count languages.
#[cfg(test)]
fn scan_repo_extensions(
    root: &Path,
    max_files: usize,
) -> std::collections::HashMap<RepoLanguage, usize> {
    let mut counts = std::collections::HashMap::new();
    let mut stack = vec![root.to_path_buf()];
    let mut checked = 0usize;

    while let Some(dir) = stack.pop() {
        if let Ok(entries) = std::fs::read_dir(&dir) {
            for entry in entries.flatten() {
                if checked >= max_files {
                    return counts;
                }
                if let Ok(meta) = entry.metadata() {
                    if meta.is_file() {
                        if let Some(ext) = entry.path().extension().and_then(|e| e.to_str()) {
                            let ext = format!(".{}", ext);
                            if let Some(lang) = RepoLanguage::from_extension(&ext) {
                                *counts.entry(lang).or_insert(0) += 1;
                            }
                        }
                        checked += 1;
                    } else if meta.is_dir() {
                        let name = entry.file_name();
                        let name = name.to_string_lossy();
                        if !name.starts_with('.')
                            && name != "target"
                            && name != "node_modules"
                            && name != "__pycache__"
                            && name != "vendor"
                        {
                            stack.push(entry.path());
                        }
                    }
                }
            }
        }
    }
    counts
}

/// Convert a CompilerError from cargo module to VerificationError
fn compiler_error_to_verification_error(ce: &CompilerError) -> VerificationError {
    VerificationError {
        file: ce.file.clone(),
        line: if ce.line > 0 { Some(ce.line) } else { None },
        column: if ce.column > 0 { Some(ce.column) } else { None },
        message: ce.message.clone(),
        code: ce.code.clone(),
        severity: match ce.severity {
            Severity::Error => ErrorSeverity::Error,
            Severity::Warning => ErrorSeverity::Warning,
            Severity::Note => ErrorSeverity::Note,
            Severity::Help => ErrorSeverity::Help,
        },
        suggestion: ce.suggestion.clone(),
    }
}

/// Parse cargo JSON output into errors and warnings
/// Uses shared parsing logic from crate::tools::cargo
fn parse_cargo_json_output(output: &str) -> (Vec<VerificationError>, Vec<VerificationError>) {
    let (cargo_errors, cargo_warnings) = parse_cargo_json_messages(output);

    let errors = cargo_errors
        .iter()
        .map(compiler_error_to_verification_error)
        .collect();
    let warnings = cargo_warnings
        .iter()
        .map(compiler_error_to_verification_error)
        .collect();

    (errors, warnings)
}

/// Parse test failures from cargo test output
fn parse_test_failures(stdout: &str, stderr: &str) -> Vec<VerificationError> {
    let mut errors = Vec::new();

    // Look for FAILED tests
    for line in stdout.lines().chain(stderr.lines()) {
        if line.contains("FAILED") && line.contains("test ") {
            let test_name = line
                .split("test ")
                .nth(1)
                .and_then(|s| s.split(" ...").next())
                .unwrap_or("unknown");

            errors.push(VerificationError {
                file: String::new(),
                line: None,
                column: None,
                message: format!("Test failed: {}", test_name),
                code: None,
                severity: ErrorSeverity::Error,
                suggestion: Some("Check test output for details".to_string()),
            });
        }

        // Look for panic messages
        if line.contains("panicked at") {
            errors.push(VerificationError {
                file: String::new(),
                line: None,
                column: None,
                message: line.to_string(),
                code: None,
                severity: ErrorSeverity::Error,
                suggestion: None,
            });
        }
    }

    errors
}

/// Format a verification report for display
impl std::fmt::Display for VerificationReport {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "\n╔══════════════════════════════════════════╗")?;
        writeln!(f, "║         VERIFICATION REPORT              ║")?;
        writeln!(f, "╠══════════════════════════════════════════╣")?;
        writeln!(
            f,
            "║ Trigger: {:<30} ║",
            truncate_str(&self.triggered_by, 30)
        )?;
        writeln!(
            f,
            "║ Status: {:<31} ║",
            if self.overall_passed {
                "✓ PASSED"
            } else {
                "✗ FAILED"
            }
        )?;
        writeln!(
            f,
            "║ Duration: {:<29} ║",
            format!("{}ms", self.total_duration_ms)
        )?;
        writeln!(f, "╠══════════════════════════════════════════╣")?;

        for check in &self.checks {
            let status = if check.passed { "" } else { "" };
            writeln!(
                f,
                "{} {}: {}ms",
                status,
                check.check_type.as_str(),
                check.duration_ms
            )?;

            for error in &check.errors {
                writeln!(
                    f,
                    "║   └─ {}: {}",
                    error.file,
                    truncate_str(&error.message, 30)
                )?;
            }
        }

        if !self.suggested_next_steps.is_empty() {
            writeln!(f, "╠══════════════════════════════════════════╣")?;
            writeln!(f, "║ Suggested next steps:                    ║")?;
            for step in &self.suggested_next_steps {
                writeln!(f, "║   • {}", truncate_str(step, 36))?;
            }
        }

        writeln!(f, "╚══════════════════════════════════════════╝")?;
        Ok(())
    }
}

fn truncate_str(s: &str, max_len: usize) -> String {
    if s.len() <= max_len {
        s.to_string()
    } else {
        format!("{}...", &s[..max_len.saturating_sub(3)])
    }
}

#[cfg(test)]
#[path = "../../tests/unit/testing/verification/verification_test.rs"]
mod tests;