adrs-core 0.11.0

Core library for managing Architecture Decision Records
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
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
//! ADR linting using mdbook-lint rules.
//!
//! This module provides unified linting for ADRs, combining per-file validation
//! (title format, required sections, date format) with repository-level checks
//! (sequential numbering, duplicate detection, broken links).

use crate::{Adr, Repository, Result};
use globset::Glob;
use mdbook_lint_core::Document;
use mdbook_lint_core::rule::{CollectionRule, Rule};
use mdbook_lint_rulesets::adr::{
    Adr001, Adr002, Adr003, Adr004, Adr005, Adr006, Adr007, Adr008, Adr009, Adr010, Adr011, Adr012,
    Adr013, Adr014, Adr015, Adr016, Adr017, AdrFormat,
};
use std::collections::HashSet;
use std::path::PathBuf;

/// Rule IDs and rule names that are *always* produced without a path.
///
/// These are the upstream collection rules whose only source is
/// `check_repository`'s `CollectionRule::check_collection` loop (`path: None`
/// there -- see the comment on that loop). `ADR013` / `adr-valid-adr-links` is
/// deliberately excluded: that rule id is also used by the frontmatter
/// broken-link check above the collection-rule loop, which *does* set a path,
/// so a `[[doctor.ignore_path]]` entry naming it can fire for that source even
/// though it can never match the collection-rule source.
///
/// Used to warn when a `[[doctor.ignore_path]]` entry names a rule that can
/// never be suppressed by path (issue #365).
const ALWAYS_PATHLESS_RULES: &[&str] = &[
    "ADR010",
    "adr-superseded-has-replacement",
    "ADR011",
    "adr-sequential-numbering",
    "ADR012",
    "adr-no-duplicate-numbers",
];

/// Severity level for lint issues.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum IssueSeverity {
    /// Informational message.
    Info,
    /// Warning that should be addressed.
    Warning,
    /// Error that needs to be fixed.
    Error,
}

impl std::fmt::Display for IssueSeverity {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            IssueSeverity::Info => write!(f, "info"),
            IssueSeverity::Warning => write!(f, "warning"),
            IssueSeverity::Error => write!(f, "error"),
        }
    }
}

impl From<mdbook_lint_core::Severity> for IssueSeverity {
    fn from(severity: mdbook_lint_core::Severity) -> Self {
        match severity {
            mdbook_lint_core::Severity::Error => IssueSeverity::Error,
            mdbook_lint_core::Severity::Warning => IssueSeverity::Warning,
            mdbook_lint_core::Severity::Info => IssueSeverity::Info,
        }
    }
}

/// A unified issue type for both per-file lint violations and repository-level diagnostics.
#[derive(Debug, Clone)]
pub struct Issue {
    /// The rule that produced this issue (e.g., "ADR001", "adr-title-format").
    pub rule_id: String,
    /// Human-readable rule name.
    pub rule_name: String,
    /// The severity of this issue.
    pub severity: IssueSeverity,
    /// A human-readable message describing the issue.
    pub message: String,
    /// The path to the affected file, if applicable.
    pub path: Option<PathBuf>,
    /// Line number (1-based), if applicable.
    pub line: Option<usize>,
    /// Column number (1-based), if applicable.
    pub column: Option<usize>,
    /// The ADR number, if applicable.
    pub adr_number: Option<u32>,
    /// Related ADR numbers (for issues involving multiple ADRs).
    pub related_adrs: Vec<u32>,
}

impl Issue {
    /// Create a new issue from an mdbook-lint violation.
    fn from_violation(
        violation: mdbook_lint_core::Violation,
        path: Option<PathBuf>,
        adr_number: Option<u32>,
    ) -> Self {
        Self {
            rule_id: violation.rule_id,
            rule_name: violation.rule_name,
            severity: violation.severity.into(),
            message: violation.message,
            path,
            line: Some(violation.line),
            column: Some(violation.column),
            adr_number,
            related_adrs: Vec::new(),
        }
    }
}

/// Results from linting.
#[derive(Debug, Default)]
pub struct LintReport {
    /// All issues found.
    pub issues: Vec<Issue>,
}

impl LintReport {
    /// Create a new empty report.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add an issue to the report.
    pub fn add(&mut self, issue: Issue) {
        self.issues.push(issue);
    }

    /// Check if there are any errors.
    pub fn has_errors(&self) -> bool {
        self.issues
            .iter()
            .any(|i| i.severity == IssueSeverity::Error)
    }

    /// Check if there are any warnings.
    pub fn has_warnings(&self) -> bool {
        self.issues
            .iter()
            .any(|i| i.severity == IssueSeverity::Warning)
    }

    /// Check if the report is clean (no warnings or errors).
    pub fn is_clean(&self) -> bool {
        !self.has_errors() && !self.has_warnings()
    }

    /// Get the count of issues by severity.
    pub fn count_by_severity(&self, severity: IssueSeverity) -> usize {
        self.issues
            .iter()
            .filter(|i| i.severity == severity)
            .count()
    }

    /// Sort issues by severity (errors first), then by path, then by line.
    pub fn sort(&mut self) {
        self.issues.sort_by(|a, b| {
            b.severity
                .cmp(&a.severity)
                .then_with(|| a.path.cmp(&b.path))
                .then_with(|| a.line.cmp(&b.line))
        });
    }
}

/// Detect an ADR document's format from its section headings.
///
/// The mdbook-lint ADR rules auto-detect format from YAML frontmatter alone,
/// treating every frontmatter document as MADR. That misclassifies a
/// frontmatter-backed Nygard ADR (as produced by `adrs --ng init`), which then
/// fails the MADR section rules (#348). This detects the format the way a reader
/// would: from the headings actually present.
///
/// - A MADR-specific H2 (`## Context and Problem Statement`, `## Decision
///   Outcome`, or `## Considered Options`) means [`AdrFormat::Madr4`].
/// - Otherwise a Nygard H2 (`## Context` or `## Decision`) means
///   [`AdrFormat::Nygard`].
/// - With neither present, fall back to [`AdrFormat::Auto`] so the rules apply
///   their own frontmatter-based heuristic unchanged.
fn detect_adr_format(content: &str) -> AdrFormat {
    let mut has_nygard = false;

    for line in content.lines() {
        let Some(heading) = line.strip_prefix("## ") else {
            continue;
        };
        let heading = heading.trim();

        if heading.eq_ignore_ascii_case("Context and Problem Statement")
            || heading.eq_ignore_ascii_case("Decision Outcome")
            || heading.eq_ignore_ascii_case("Considered Options")
        {
            return AdrFormat::Madr4;
        }

        if heading.eq_ignore_ascii_case("Context") || heading.eq_ignore_ascii_case("Decision") {
            has_nygard = true;
        }
    }

    if has_nygard {
        AdrFormat::Nygard
    } else {
        AdrFormat::Auto
    }
}

/// Lint a single ADR file.
///
/// Runs all per-file lint rules against the ADR content.
pub fn lint_adr(adr: &Adr) -> Result<LintReport> {
    let mut report = LintReport::new();

    // Get the file content
    let Some(path) = &adr.path else {
        return Ok(report); // No path, nothing to lint
    };

    let content = std::fs::read_to_string(path)?;

    // Create mdbook-lint Document
    let doc = match Document::new(content, path.clone()) {
        Ok(d) => d,
        Err(e) => {
            report.add(Issue {
                rule_id: "parse-error".to_string(),
                rule_name: "parse-error".to_string(),
                severity: IssueSeverity::Error,
                message: format!("Failed to parse document: {e}"),
                path: Some(path.clone()),
                line: None,
                column: None,
                adr_number: Some(adr.number),
                related_adrs: Vec::new(),
            });
            return Ok(report);
        }
    };

    // Detect the document's actual format from its section headings, not from
    // the mere presence of YAML frontmatter. The mdbook-lint ADR rules default
    // to `AdrFormat::Auto`, which classifies any frontmatter document as MADR
    // (see #348: `adrs --ng init` writes frontmatter + Nygard headings, which
    // Auto then flags as missing the MADR `## Context and Problem Statement` /
    // `## Decision Outcome` sections). Pinning the format-sensitive rules to the
    // format we detect from the headings keeps a frontmatter-backed Nygard ADR
    // valid while still validating genuine MADR documents.
    let format = detect_adr_format(&doc.content);

    // Run all single-document rules
    let rules: Vec<Box<dyn Rule>> = vec![
        Box::new(Adr001::default()),
        Box::new(Adr002::default()),
        Box::new(Adr003::default()),
        Box::new(Adr004::with_format(format)),
        Box::new(Adr005::with_format(format)),
        Box::new(Adr006::with_format(format)),
        Box::new(Adr007::default()),
        Box::new(Adr008::default()),
        Box::new(Adr009::default()),
        Box::new(Adr014::default()),
        Box::new(Adr015::default()),
        Box::new(Adr016::default()),
        Box::new(Adr017::with_format(format)),
    ];

    for rule in rules {
        match rule.check(&doc) {
            Ok(violations) => {
                for violation in violations {
                    report.add(Issue::from_violation(
                        violation,
                        Some(path.clone()),
                        Some(adr.number),
                    ));
                }
            }
            Err(e) => {
                report.add(Issue {
                    rule_id: rule.id().to_string(),
                    rule_name: rule.name().to_string(),
                    severity: IssueSeverity::Error,
                    message: format!("Rule failed: {e}"),
                    path: Some(path.clone()),
                    line: None,
                    column: None,
                    adr_number: Some(adr.number),
                    related_adrs: Vec::new(),
                });
            }
        }
    }

    Ok(report)
}

/// Lint all ADRs in a repository (per-file checks only).
pub fn lint_all(repo: &Repository) -> Result<LintReport> {
    let mut report = LintReport::new();
    let adrs = repo.list()?;

    for adr in &adrs {
        let adr_report = lint_adr(adr)?;
        report.issues.extend(adr_report.issues);
    }

    report.sort();
    Ok(report)
}

/// Run repository-level checks (collection rules).
///
/// These checks analyze the ADR set as a whole:
/// - Sequential numbering (ADR011)
/// - Duplicate numbers (ADR012)
/// - Broken links (ADR013)
/// - Superseded ADRs have replacements (ADR010)
/// - Asymmetric links (`asymmetric-link`)
pub fn check_repository(repo: &Repository) -> Result<LintReport> {
    let mut report = LintReport::new();
    let adrs = repo.list()?;

    // Build documents for collection rules
    let mut documents = Vec::new();
    for adr in &adrs {
        if let Some(path) = &adr.path {
            let content = std::fs::read_to_string(path)?;
            if let Ok(doc) = Document::new(content, path.clone()) {
                documents.push(doc);
            }
        }
    }

    // Resolve frontmatter `links[].target` against the ADR numbers actually
    // present in the repository. This is distinct from the upstream ADR013
    // rule below, which only checks markdown link *filenames* in the
    // rendered body and has no notion of frontmatter (#355). A frontmatter
    // link is a structured reference to an ADR number, so an unresolvable
    // target is unambiguously broken and reported at `Error` rather than the
    // upstream rule's `Warning`.
    //
    // Gated to nextgen mode: in compatible mode, `adr.links` is populated by
    // parsing legacy body syntax like `Supersedes [1. Title](0001-title.md)`
    // (see parse.rs), not frontmatter, and that path is already covered by
    // the upstream markdown-filename check below. Checking it again here
    // would change compatible-mode severity/exit-code behavior, which is out
    // of scope for this fix.
    //
    // Track the prefix of the message the upstream ADR013 rule would emit for
    // each broken link so its warning can be suppressed once our error covers
    // the same broken link. Nygard-family templates render a link both in
    // frontmatter and as a body markdown link, so without this a single
    // broken link would otherwise produce two ADR013 issues for the same
    // record.
    //
    // The prefix stops at the target's zero-padded number rather than
    // spelling out a whole filename, because the body filename varies: an
    // unresolvable target renders as the `{:04}-....md` fallback (see
    // template.rs's `resolve_link_titles`), but a link rendered while its
    // target still existed keeps that target's real filename, which is the
    // case #355 was found through. Anchoring on the record's path and the
    // padded number matches both without matching a different record or a
    // different target.
    let mut broken_link_fragments: Vec<String> = Vec::new();

    if repo.config().is_next_gen() {
        let existing_numbers: std::collections::HashSet<u32> =
            adrs.iter().map(|a| a.number).collect();

        for adr in &adrs {
            for link in &adr.links {
                if existing_numbers.contains(&link.target) {
                    continue;
                }

                let path = adr.path.clone().unwrap_or_default();
                broken_link_fragments.push(format!(
                    "{}: Link to '{:04}",
                    path.display(),
                    link.target
                ));

                report.add(Issue {
                    rule_id: "ADR013".to_string(),
                    rule_name: "adr-valid-adr-links".to_string(),
                    severity: IssueSeverity::Error,
                    message: format!(
                        "ADR {} '{}' links to non-existent ADR {}",
                        adr.number, adr.title, link.target
                    ),
                    path: adr.path.clone(),
                    line: None,
                    column: None,
                    adr_number: Some(adr.number),
                    related_adrs: Vec::new(),
                });
            }
        }
    }

    // Check that every link is reciprocated (#357). `adrs link` maintains
    // both halves of a relationship by construction, so an asymmetric pair
    // is evidence that something outside the tool edited the record -- a
    // hand edit, a renumber, a badly resolved merge. For each link A -> B,
    // require that B carries some link back to A.
    //
    // The back-link is matched by target only, not by `LinkKind::reverse()`'s
    // exact kind. `adrs link` accepts an explicit `reverse_kind` override
    // (see commands/link.rs), so requiring the derived kind exactly would
    // flag the tool's own supported output as corruption; asking only "does
    // B link back to A" agrees with the tool by construction.
    //
    // Links whose target does not exist are skipped: that is a broken link,
    // already reported as ADR013 above (frontmatter) or below (markdown
    // body). Reporting it again as asymmetry would give two diagnostics for
    // one problem.
    //
    // Unlike the frontmatter check above, this is not gated to nextgen mode:
    // `adr.links` is populated in compatible mode by parsing legacy body
    // syntax (see parse.rs) and in nextgen mode from frontmatter, and there
    // is no upstream rule here to conflict with.
    let adrs_by_number: std::collections::HashMap<u32, &Adr> =
        adrs.iter().map(|a| (a.number, a)).collect();

    for adr in &adrs {
        for link in &adr.links {
            let Some(target_adr) = adrs_by_number.get(&link.target) else {
                continue;
            };

            let has_back_link = target_adr
                .links
                .iter()
                .any(|back| back.target == adr.number);

            if !has_back_link {
                report.add(Issue {
                    rule_id: "asymmetric-link".to_string(),
                    rule_name: "adr-asymmetric-link".to_string(),
                    severity: IssueSeverity::Warning,
                    message: format!(
                        "ADR {} '{}' links to ADR {} as '{}' but ADR {} has no link back to ADR {}",
                        adr.number, adr.title, link.target, link.kind, link.target, adr.number
                    ),
                    path: adr.path.clone(),
                    line: None,
                    column: None,
                    adr_number: Some(adr.number),
                    related_adrs: Vec::new(),
                });
            }
        }
    }

    // Run collection rules
    let collection_rules: Vec<Box<dyn CollectionRule>> = vec![
        Box::new(Adr010),
        Box::new(Adr011),
        Box::new(Adr012),
        Box::new(Adr013),
    ];

    for rule in collection_rules {
        match rule.check_collection(&documents) {
            Ok(violations) => {
                for violation in violations {
                    // Suppress the upstream markdown-filename ADR013 warning
                    // when the frontmatter check above already reported the
                    // same broken link as an error (see comment above).
                    if violation.rule_id == "ADR013"
                        && broken_link_fragments
                            .iter()
                            .any(|fragment| violation.message.contains(fragment.as_str()))
                    {
                        continue;
                    }

                    // Collection rule violations may have path in the message
                    // We need to parse it out or handle it differently
                    report.add(Issue {
                        rule_id: rule.id().to_string(),
                        rule_name: rule.name().to_string(),
                        severity: violation.severity.into(),
                        message: violation.message,
                        path: None, // Collection rules may span multiple files
                        line: if violation.line > 0 {
                            Some(violation.line)
                        } else {
                            None
                        },
                        column: if violation.column > 0 {
                            Some(violation.column)
                        } else {
                            None
                        },
                        adr_number: None,
                        related_adrs: Vec::new(),
                    });
                }
            }
            Err(e) => {
                report.add(Issue {
                    rule_id: rule.id().to_string(),
                    rule_name: rule.name().to_string(),
                    severity: IssueSeverity::Error,
                    message: format!("Rule failed: {e}"),
                    path: None,
                    line: None,
                    column: None,
                    adr_number: None,
                    related_adrs: Vec::new(),
                });
            }
        }
    }

    report.sort();
    Ok(report)
}

/// A compiled `[[doctor.ignore_path]]` entry, ready to match against issue paths.
struct CompiledIgnorePath {
    matcher: globset::GlobMatcher,
    rules: HashSet<String>,
}

/// Run all checks and filter out issues matching ignored rule IDs/names,
/// repository-wide or path-scoped.
///
/// Repository-wide ignores are `repo.config().doctor.ignore` unioned with
/// `extra_ignore` (e.g. CLI `--ignore` flags for a single invocation).
/// Path-scoped ignores are `repo.config().doctor.ignore_path`: each entry
/// suppresses its `rules` only for issues whose path, relative to the
/// repository root with separators normalized to `/`, matches its `glob`.
/// Both forms match rules case-insensitively against `Issue.rule_id` and
/// `Issue.rule_name` (issue #365).
///
/// Returns the filtered report, the count of issues that were suppressed
/// (repository-wide and path-scoped combined, each issue counted once even
/// if it matched both), and any config warnings: an invalid glob that could
/// not be compiled, or a `[[doctor.ignore_path]]` entry naming a rule that
/// only ever produces path-less diagnostics and so can never be suppressed
/// this way.
pub fn check_all_filtered(
    repo: &Repository,
    extra_ignore: &[String],
) -> Result<(LintReport, usize, Vec<String>)> {
    let mut report = LintReport::new();
    let mut warnings = Vec::new();

    // Use list_with_errors to capture parse failures
    let (adrs, parse_errors) = repo.list_with_errors()?;

    // Report parse errors as lint issues
    for (path, error) in &parse_errors {
        report.add(Issue {
            rule_id: "parse-error".to_string(),
            rule_name: "adr-parse-error".to_string(),
            severity: IssueSeverity::Error,
            message: format!("Failed to parse ADR: {error}"),
            path: Some(path.clone()),
            line: None,
            column: None,
            adr_number: None,
            related_adrs: Vec::new(),
        });
    }

    // Run per-file lint on successfully parsed ADRs
    for adr in &adrs {
        let adr_report = lint_adr(adr)?;
        report.issues.extend(adr_report.issues);
    }

    // Run repository-level checks (these still use repo.list() internally,
    // which is fine — they only need successfully parsed ADRs)
    let repo_report = check_repository(repo)?;
    report.issues.extend(repo_report.issues);

    report.sort();

    let ignore_set: HashSet<String> = repo
        .config()
        .doctor
        .ignore
        .iter()
        .chain(extra_ignore.iter())
        .map(|s| s.to_lowercase())
        .collect();

    // Compile each `[[doctor.ignore_path]]` entry. An invalid glob is a
    // config error in the same family as #363 -- the user believes an
    // exemption is active when it is not -- so it is reported as a warning
    // rather than silently dropped, and the rest of the config still loads
    // and applies (see `warn_unknown_config_keys` in the CLI for the same
    // non-fatal-warning convention).
    let mut compiled_ignore_paths: Vec<CompiledIgnorePath> = Vec::new();
    for entry in &repo.config().doctor.ignore_path {
        match Glob::new(&entry.glob) {
            Ok(glob) => compiled_ignore_paths.push(CompiledIgnorePath {
                matcher: glob.compile_matcher(),
                rules: entry.rules.iter().map(|s| s.to_lowercase()).collect(),
            }),
            Err(e) => warnings.push(format!(
                "[[doctor.ignore_path]] glob '{}' is invalid and will not be applied: {e}",
                entry.glob
            )),
        }

        for rule in &entry.rules {
            if ALWAYS_PATHLESS_RULES
                .iter()
                .any(|pathless| pathless.eq_ignore_ascii_case(rule))
            {
                warnings.push(format!(
                    "[[doctor.ignore_path]] entry for glob '{}' names rule '{}', which only ever produces diagnostics without a path; this exemption can never suppress it",
                    entry.glob, rule
                ));
            }
        }
    }

    if ignore_set.is_empty() && compiled_ignore_paths.is_empty() {
        return Ok((report, 0, warnings));
    }

    let root = repo.root();
    let before = report.issues.len();
    report.issues.retain(|issue| {
        if ignore_set.contains(&issue.rule_id.to_lowercase())
            || ignore_set.contains(&issue.rule_name.to_lowercase())
        {
            return false;
        }

        let Some(path) = &issue.path else {
            return true;
        };

        let relative = path.strip_prefix(root).unwrap_or(path);
        let relative_str = relative.to_string_lossy().replace('\\', "/");

        let rule_id = issue.rule_id.to_lowercase();
        let rule_name = issue.rule_name.to_lowercase();

        !compiled_ignore_paths.iter().any(|entry| {
            entry.matcher.is_match(&relative_str)
                && (entry.rules.contains(&rule_id) || entry.rules.contains(&rule_name))
        })
    });
    let suppressed = before - report.issues.len();

    Ok((report, suppressed, warnings))
}

/// Run all checks: per-file lint + repository-level checks.
///
/// Also reports files that look like ADRs (digit-prefixed `.md` files in the
/// ADR directory) but could not be parsed (e.g., invalid YAML frontmatter).
pub fn check_all(repo: &Repository) -> Result<LintReport> {
    check_all_filtered(repo, &[]).map(|(report, _, _)| report)
}

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

    #[test]
    fn test_issue_severity_ordering() {
        assert!(IssueSeverity::Error > IssueSeverity::Warning);
        assert!(IssueSeverity::Warning > IssueSeverity::Info);
    }

    #[test]
    fn test_lint_report_empty() {
        let report = LintReport::new();
        assert!(report.is_clean());
        assert!(!report.has_errors());
        assert!(!report.has_warnings());
    }

    #[test]
    fn test_lint_report_with_issues() {
        let mut report = LintReport::new();
        report.add(Issue {
            rule_id: "ADR001".to_string(),
            rule_name: "adr-title-format".to_string(),
            severity: IssueSeverity::Error,
            message: "Title format invalid".to_string(),
            path: Some(PathBuf::from("0001-test.md")),
            line: Some(1),
            column: Some(1),
            adr_number: Some(1),
            related_adrs: Vec::new(),
        });

        assert!(report.has_errors());
        assert!(!report.is_clean());
        assert_eq!(report.count_by_severity(IssueSeverity::Error), 1);
    }

    #[test]
    fn test_lint_valid_nygard_adr() {
        // Uses the actual ADR #0001 text produced by `adrs init`. The word "described"
        // previously triggered an ADR014 false positive (fixed in mdbook-lint-rulesets 0.14.3).
        let content = format!(
            r#"# 1. Record architecture decisions

Date: 2024-03-04

## Status

Accepted

## Context

{}

## Decision

{}

## Consequences

{}
"#,
            crate::init_adr::CONTEXT,
            crate::init_adr::DECISION,
            crate::init_adr::CONSEQUENCES,
        );
        let temp_dir = tempfile::tempdir().unwrap();
        let path = temp_dir
            .path()
            .join("adr")
            .join("0001-record-architecture-decisions.md");
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(&path, content).unwrap();

        let mut adr = Adr::new(1, "Record architecture decisions");
        adr.path = Some(path);

        let report = lint_adr(&adr).unwrap();

        // Print any issues for debugging
        for issue in &report.issues {
            println!(
                "{}: {} ({}:{})",
                issue.rule_id,
                issue.message,
                issue.line.unwrap_or(0),
                issue.column.unwrap_or(0)
            );
        }

        assert!(report.is_clean(), "Expected no issues for valid Nygard ADR");
    }

    #[test]
    fn test_lint_invalid_adr_missing_status() {
        let content = r#"# 1. Test decision

Date: 2024-03-04

## Context

Some context.

## Decision

Some decision.

## Consequences

Some consequences.
"#;
        let temp_dir = tempfile::tempdir().unwrap();
        let path = temp_dir.path().join("adr").join("0001-test-decision.md");
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(&path, content).unwrap();

        let mut adr = Adr::new(1, "Test decision");
        adr.path = Some(path);

        let report = lint_adr(&adr).unwrap();

        // Should have at least one issue (missing status)
        assert!(
            !report.is_clean(),
            "Expected issues for ADR missing status section"
        );
        assert!(
            report.issues.iter().any(|i| i.rule_id == "ADR002"),
            "Expected ADR002 (missing status) violation"
        );
    }

    #[test]
    fn test_nygard_bare_minimal_template_passes_doctor() {
        // Regression for #330: a file produced by the Nygard bare-minimal
        // template must not trip any doctor error (it previously emitted no
        // `Date:` line and failed with ADR003). Empty-section ADR014 warnings
        // are inherent to the variant and are not errors.
        use crate::{Adr, Config, Repository, Template, TemplateFormat, TemplateVariant};

        let temp = tempfile::tempdir().unwrap();
        let repo = Repository::init(temp.path(), None, false).unwrap();

        let template =
            Template::builtin_with_variant(TemplateFormat::Nygard, TemplateVariant::BareMinimal);
        let adr = Adr::new(2, "Bare minimal regression");
        let rendered = template
            .render(&adr, &Config::default(), &std::collections::HashMap::new())
            .unwrap();
        let path = repo.adr_path().join("0002-bare-minimal-regression.md");
        std::fs::write(&path, rendered).unwrap();

        let report = check_all(&repo).unwrap();
        let file_errors: Vec<_> = report
            .issues
            .iter()
            .filter(|i| i.severity == IssueSeverity::Error)
            .filter(|i| {
                i.path
                    .as_ref()
                    .is_some_and(|p| p.to_string_lossy().contains("0002-bare-minimal-regression"))
            })
            .collect();
        assert!(
            file_errors.is_empty(),
            "nygard bare-minimal output should have no doctor errors, got: {file_errors:?}"
        );
    }

    #[test]
    fn test_detect_adr_format_from_headings() {
        // Frontmatter + Nygard headings is Nygard, not MADR (#348).
        assert_eq!(
            detect_adr_format("---\nstatus: accepted\n---\n\n## Context\n\n## Decision\n"),
            AdrFormat::Nygard
        );
        // MADR-specific headings win regardless of frontmatter.
        assert_eq!(
            detect_adr_format(
                "---\nstatus: accepted\n---\n\n## Context and Problem Statement\n\n## Decision Outcome\n"
            ),
            AdrFormat::Madr4
        );
        // Plain Nygard (no frontmatter) is Nygard.
        assert_eq!(
            detect_adr_format("# 1. Title\n\nDate: 2024-03-04\n\n## Context\n"),
            AdrFormat::Nygard
        );
        // Neither heading set present: defer to the rules' own heuristic.
        assert_eq!(
            detect_adr_format("---\nstatus: accepted\n---\n\n# Title only\n"),
            AdrFormat::Auto
        );
    }

    #[test]
    fn test_ng_init_repo_passes_doctor() {
        // Regression for #348: `adrs --ng init` writes ADR #0001 with YAML
        // frontmatter and Nygard headings. The mdbook-lint ADR rules auto-detect
        // format from frontmatter alone and flagged it as MADR, demanding
        // `## Context and Problem Statement` / `## Decision Outcome` (ADR004/005).
        // A freshly initialized next-gen repository must pass doctor unchanged.
        use crate::Repository;

        let temp = tempfile::tempdir().unwrap();
        let repo = Repository::init(temp.path(), None, true).unwrap();

        let report = check_all(&repo).unwrap();
        let errors: Vec<_> = report
            .issues
            .iter()
            .filter(|i| i.severity == IssueSeverity::Error)
            .collect();
        assert!(
            errors.is_empty(),
            "freshly `--ng init`ed repo should pass doctor, got: {errors:?}"
        );
    }

    #[test]
    fn test_frontmatter_nygard_adr_not_flagged_for_madr_sections() {
        // #348: frontmatter presence alone must not trigger the MADR section
        // rules on a document that uses Nygard headings.
        let content = "---\nnumber: 1\ntitle: Record architecture decisions\ndate: 2024-03-04\nstatus: accepted\n---\n\n## Context\n\nSome context.\n\n## Decision\n\nSome decision.\n\n## Consequences\n\nSome consequences.\n";
        let temp_dir = tempfile::tempdir().unwrap();
        let path = temp_dir
            .path()
            .join("adr")
            .join("0001-record-architecture-decisions.md");
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(&path, content).unwrap();

        let mut adr = Adr::new(1, "Record architecture decisions");
        adr.path = Some(path);

        let report = lint_adr(&adr).unwrap();
        assert!(
            !report
                .issues
                .iter()
                .any(|i| i.rule_id == "ADR004" || i.rule_id == "ADR005"),
            "frontmatter+Nygard ADR must not trip MADR section rules, got: {:?}",
            report.issues
        );
    }

    #[test]
    fn test_genuine_madr_missing_decision_outcome_still_flagged() {
        // Detection must not weaken validation of real MADR documents: a MADR
        // ADR (MADR headings) that omits `## Decision Outcome` still gets ADR005.
        let content = "---\nnumber: 1\ntitle: Use Postgres\ndate: 2024-03-04\nstatus: accepted\n---\n\n## Context and Problem Statement\n\nWhich database?\n\n## Considered Options\n\n* Postgres\n* MySQL\n";
        let temp_dir = tempfile::tempdir().unwrap();
        let path = temp_dir.path().join("adr").join("0001-use-postgres.md");
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(&path, content).unwrap();

        let mut adr = Adr::new(1, "Use Postgres");
        adr.path = Some(path);

        let report = lint_adr(&adr).unwrap();
        assert!(
            report.issues.iter().any(|i| i.rule_id == "ADR005"),
            "MADR ADR missing '## Decision Outcome' must still trip ADR005, got: {:?}",
            report.issues
        );
    }

    #[test]
    fn test_check_all_reports_parse_errors() {
        use crate::Repository;

        let temp = tempfile::tempdir().unwrap();
        let repo = Repository::init(temp.path(), None, true).unwrap();

        // Write an ADR with invalid YAML (bad date)
        let bad_content =
            "---\nnumber: 2\nstatus: accepted\ndate: not-a-date\n---\n\n# 2. Bad Date\n";
        std::fs::write(repo.adr_path().join("0002-bad-date.md"), bad_content).unwrap();

        let report = check_all(&repo).unwrap();

        let parse_errors: Vec<_> = report
            .issues
            .iter()
            .filter(|i| i.rule_id == "parse-error")
            .collect();

        assert_eq!(parse_errors.len(), 1, "should report 1 parse error");
        assert_eq!(parse_errors[0].severity, IssueSeverity::Error);
        assert!(
            parse_errors[0]
                .path
                .as_ref()
                .unwrap()
                .to_string_lossy()
                .contains("0002-bad-date.md")
        );
    }

    #[test]
    fn test_check_all_no_parse_errors_for_string_decision_makers() {
        use crate::Repository;

        let temp = tempfile::tempdir().unwrap();
        let repo = Repository::init(temp.path(), None, true).unwrap();

        // Issue #216: decision-makers as string should not cause a parse error
        let content = "---\nnumber: 2\nstatus: accepted\ndate: 2026-03-18\ndecision-makers: alice\n---\n\n# 2. Test\n\n## Context\n\nContext.\n\n## Decision\n\nDecision.\n\n## Consequences\n\nConsequences.\n";
        std::fs::write(repo.adr_path().join("0002-test.md"), content).unwrap();

        let report = check_all(&repo).unwrap();

        let parse_errors: Vec<_> = report
            .issues
            .iter()
            .filter(|i| i.rule_id == "parse-error")
            .collect();

        assert!(
            parse_errors.is_empty(),
            "string decision-makers should not cause parse error, got: {:?}",
            parse_errors.iter().map(|i| &i.message).collect::<Vec<_>>()
        );
    }
    // ========== check_repository collection rules (issue #239) ==========

    fn make_nygard_adr(number: u32, title: &str, status: &str, links: &str) -> String {
        format!(
            "# {}. {}\n\nDate: 2024-01-01\n\n## Status\n\n{}{}\n## Context\n\nSome context.\n\n## Decision\n\nA decision.\n\n## Consequences\n\nSome consequences.\n",
            number, title, status, links
        )
    }

    #[test]
    fn test_check_repository_broken_link_adr013() {
        use crate::Repository;

        let temp = tempfile::tempdir().unwrap();
        // init creates ADR #1 automatically
        let repo = Repository::init(temp.path(), None, false).unwrap();
        let adr_dir = repo.adr_path();

        // ADR 2 links to nonexistent ADR 99
        std::fs::write(
            adr_dir.join("0002-second.md"),
            make_nygard_adr(
                2,
                "Second",
                "Accepted",
                "\n\nSupersedes [99. Unknown](0099-unknown.md)\n",
            ),
        )
        .unwrap();

        let report = check_repository(&repo).unwrap();

        // Should have an ADR013 (broken links) issue
        let has_adr013 = report.issues.iter().any(|i| i.rule_id == "ADR013");
        assert!(
            has_adr013,
            "Expected ADR013 broken-link issue, got: {:?}",
            report.issues.iter().map(|i| &i.rule_id).collect::<Vec<_>>()
        );
    }

    fn make_frontmatter_adr(number: u32, title: &str, status: &str, links_yaml: &str) -> String {
        format!(
            "---\nnumber: {}\ntitle: {}\ndate: 2024-01-01\nstatus: {}\n{}---\n\n## Context\n\nSome context.\n\n## Decision\n\nA decision.\n\n## Consequences\n\nSome consequences.\n",
            number, title, status, links_yaml
        )
    }

    #[test]
    fn test_check_repository_frontmatter_broken_link_adr013_error() {
        use crate::Repository;

        let temp = tempfile::tempdir().unwrap();
        // nextgen mode: links live in YAML frontmatter (issue #355 repro)
        let repo = Repository::init(temp.path(), None, true).unwrap();
        let adr_dir = repo.adr_path();

        // ADR 2 has a frontmatter link to nonexistent ADR 99
        std::fs::write(
            adr_dir.join("0002-second.md"),
            make_frontmatter_adr(
                2,
                "Second",
                "proposed",
                "links:\n  - target: 99\n    kind: relatesto\n",
            ),
        )
        .unwrap();

        let report = check_repository(&repo).unwrap();

        let broken = report
            .issues
            .iter()
            .find(|i| i.rule_id == "ADR013" && i.severity == IssueSeverity::Error);
        assert!(
            broken.is_some(),
            "Expected ADR013 error for frontmatter link to non-existent ADR 99, got: {:?}",
            report
                .issues
                .iter()
                .map(|i| (&i.rule_id, i.severity, &i.message))
                .collect::<Vec<_>>()
        );
        let issue = broken.unwrap();
        assert_eq!(issue.adr_number, Some(2));
        assert!(
            issue.path.is_some(),
            "expected a file location on the issue"
        );
        assert!(
            issue.message.contains("links to non-existent ADR 99"),
            "unexpected message: {}",
            issue.message
        );
        assert!(
            report.has_errors(),
            "a broken frontmatter link must make the report (and doctor's exit code) nonzero"
        );
    }

    #[test]
    fn test_check_repository_frontmatter_link_to_existing_adr_no_issue() {
        use crate::Repository;

        let temp = tempfile::tempdir().unwrap();
        let repo = Repository::init(temp.path(), None, true).unwrap();
        let adr_dir = repo.adr_path();

        // init() creates ADR #1; link ADR 2 to it.
        std::fs::write(
            adr_dir.join("0002-second.md"),
            make_frontmatter_adr(
                2,
                "Second",
                "proposed",
                "links:\n  - target: 1\n    kind: relatesto\n",
            ),
        )
        .unwrap();

        let report = check_repository(&repo).unwrap();

        assert!(
            !report.issues.iter().any(|i| i.rule_id == "ADR013"),
            "link to an existing ADR should not produce ADR013, got: {:?}",
            report.issues.iter().map(|i| &i.message).collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_check_repository_dedups_frontmatter_and_body_broken_link() {
        use crate::Repository;

        let temp = tempfile::tempdir().unwrap();
        let repo = Repository::init(temp.path(), None, true).unwrap();
        let adr_dir = repo.adr_path();

        // A Nygard-format nextgen render puts a broken link's target in both
        // the frontmatter `links:` block and a body markdown link, with the
        // same unresolved fallback filename (see template.rs's
        // `resolve_link_titles`, which falls back to `{:04}-....md` for a
        // target it can't find). One broken link should report once.
        let content = "---\nnumber: 2\ntitle: Second\ndate: 2024-01-01\nstatus: proposed\nlinks:\n  - target: 99\n    kind: relatesto\n---\n\n# 2. Second\n\nDate: 2024-01-01\n\n## Status\n\nProposed\n\nRelates to [99. ...](0099-....md)\n\n## Context\n\nSome context.\n\n## Decision\n\nA decision.\n\n## Consequences\n\nSome consequences.\n";
        std::fs::write(adr_dir.join("0002-second.md"), content).unwrap();

        let report = check_repository(&repo).unwrap();

        let adr013_issues: Vec<_> = report
            .issues
            .iter()
            .filter(|i| i.rule_id == "ADR013")
            .collect();
        assert_eq!(
            adr013_issues.len(),
            1,
            "a broken link present in both frontmatter and body should report once, got: {:?}",
            adr013_issues
                .iter()
                .map(|i| (&i.severity, &i.message))
                .collect::<Vec<_>>()
        );
        assert_eq!(adr013_issues[0].severity, IssueSeverity::Error);
    }

    #[test]
    fn test_check_repository_dedups_frontmatter_and_body_link_with_real_filename() {
        use crate::Repository;

        let temp = tempfile::tempdir().unwrap();
        let repo = Repository::init(temp.path(), None, true).unwrap();
        let adr_dir = repo.adr_path();

        // A link that was rendered while its target still existed carries the
        // target's real filename, not the `{:04}-....md` unresolved fallback.
        // This is the case #355 was found through: renumbering left a stale
        // target behind. It is still one broken link and must report once.
        let content = "---\nnumber: 2\ntitle: Second\ndate: 2024-01-01\nstatus: proposed\nlinks:\n  - target: 99\n    kind: relatesto\n---\n\n# 2. Second\n\nDate: 2024-01-01\n\n## Status\n\nProposed\n\nRelates to [99. Old Title](0099-old-title.md)\n\n## Context\n\nSome context.\n\n## Decision\n\nA decision.\n\n## Consequences\n\nSome consequences.\n";
        std::fs::write(adr_dir.join("0002-second.md"), content).unwrap();

        let report = check_repository(&repo).unwrap();

        let adr013_issues: Vec<_> = report
            .issues
            .iter()
            .filter(|i| i.rule_id == "ADR013")
            .collect();
        assert_eq!(
            adr013_issues.len(),
            1,
            "a broken link whose body filename resolved should report once, got: {:?}",
            adr013_issues
                .iter()
                .map(|i| (&i.severity, &i.message))
                .collect::<Vec<_>>()
        );
        assert_eq!(adr013_issues[0].severity, IssueSeverity::Error);
    }

    // ========== check_repository asymmetric-link rule (issue #357) ==========

    #[test]
    fn test_check_repository_asymmetric_link_half_deleted_one_warning() {
        use crate::Repository;

        let temp = tempfile::tempdir().unwrap();
        // Compatible mode, matching the issue's own reproduction. `init`
        // creates ADR #1 with no links -- the "half-deleted" state: its
        // `Superseded by` line was removed by hand, leaving ADR #2's
        // `Supersedes` link with nothing pointing back (issue #357 case 1).
        let repo = Repository::init(temp.path(), None, false).unwrap();
        let adr_dir = repo.adr_path();

        std::fs::write(
            adr_dir.join("0002-second.md"),
            make_nygard_adr(
                2,
                "Second",
                "Accepted",
                "\n\nSupersedes [1. Record architecture decisions](0001-record-architecture-decisions.md)\n",
            ),
        )
        .unwrap();

        let report = check_repository(&repo).unwrap();

        let warnings: Vec<_> = report
            .issues
            .iter()
            .filter(|i| i.rule_id == "asymmetric-link")
            .collect();
        assert_eq!(
            warnings.len(),
            1,
            "expected exactly one asymmetric-link warning, got: {:?}",
            report
                .issues
                .iter()
                .map(|i| (&i.rule_id, &i.message))
                .collect::<Vec<_>>()
        );
        assert_eq!(warnings[0].rule_name, "adr-asymmetric-link");
        assert_eq!(warnings[0].severity, IssueSeverity::Warning);
        assert_eq!(warnings[0].adr_number, Some(2));
        assert!(
            warnings[0].message.contains("ADR 2")
                && warnings[0].message.contains("ADR 1")
                && warnings[0].message.contains("no link back to ADR 2"),
            "unexpected message: {}",
            warnings[0].message
        );
    }

    #[test]
    fn test_check_repository_asymmetric_link_mis_targeted_two_warnings() {
        use crate::Repository;

        let temp = tempfile::tempdir().unwrap();
        // Nextgen mode (frontmatter links) -- covers the both-modes requirement.
        let repo = Repository::init(temp.path(), None, true).unwrap();
        let adr_dir = repo.adr_path();

        // ADR 1's reverse link is repointed at ADR 3, which exists but has no
        // relationship to either. ADR 2 still claims to supersede ADR 1
        // (issue #357 case 2). Both halves are independently broken, so this
        // must produce two warnings, not one.
        std::fs::write(
            adr_dir.join("0001-record-architecture-decisions.md"),
            make_frontmatter_adr(
                1,
                "Record architecture decisions",
                "superseded",
                "links:\n  - target: 3\n    kind: supersededby\n",
            ),
        )
        .unwrap();
        std::fs::write(
            adr_dir.join("0002-second.md"),
            make_frontmatter_adr(
                2,
                "Second",
                "proposed",
                "links:\n  - target: 1\n    kind: supersedes\n",
            ),
        )
        .unwrap();
        std::fs::write(
            adr_dir.join("0003-third.md"),
            make_frontmatter_adr(3, "Third", "proposed", ""),
        )
        .unwrap();

        let report = check_repository(&repo).unwrap();

        let warnings: Vec<_> = report
            .issues
            .iter()
            .filter(|i| i.rule_id == "asymmetric-link")
            .collect();
        assert_eq!(
            warnings.len(),
            2,
            "expected two asymmetric-link warnings, one per independently broken half, got: {:?}",
            report
                .issues
                .iter()
                .map(|i| (&i.rule_id, &i.message))
                .collect::<Vec<_>>()
        );

        let adr_numbers: Vec<_> = warnings.iter().filter_map(|i| i.adr_number).collect();
        assert!(
            adr_numbers.contains(&1) && adr_numbers.contains(&2),
            "expected warnings naming both ADR 1 and ADR 2, got: {adr_numbers:?}"
        );
        assert!(
            warnings
                .iter()
                .all(|i| i.severity == IssueSeverity::Warning)
        );
    }

    #[test]
    fn test_check_repository_symmetric_link_via_repository_link_no_warning() {
        use crate::{LinkKind, Repository};

        let temp = tempfile::tempdir().unwrap();
        let repo = Repository::init(temp.path(), None, true).unwrap();
        let adr_dir = repo.adr_path();

        // ADR 2, so `repo.link` has two existing ADRs to connect.
        std::fs::write(
            adr_dir.join("0002-second.md"),
            make_frontmatter_adr(2, "Second", "proposed", ""),
        )
        .unwrap();

        // Built through the tool's own API, not by hand, so this test breaks
        // if the rule and `adrs link` ever disagree about what counts as
        // symmetric.
        repo.link(2, 1, LinkKind::Supersedes, LinkKind::SupersededBy)
            .unwrap();

        let report = check_repository(&repo).unwrap();

        assert!(
            !report.issues.iter().any(|i| i.rule_id == "asymmetric-link"),
            "a pair built through Repository::link must not be flagged, got: {:?}",
            report
                .issues
                .iter()
                .map(|i| (&i.rule_id, &i.message))
                .collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_check_repository_non_derived_reverse_kind_no_warning() {
        use crate::{LinkKind, Repository};

        let temp = tempfile::tempdir().unwrap();
        let repo = Repository::init(temp.path(), None, true).unwrap();
        let adr_dir = repo.adr_path();

        std::fs::write(
            adr_dir.join("0002-second.md"),
            make_frontmatter_adr(2, "Second", "proposed", ""),
        )
        .unwrap();

        // `adrs link` accepts an explicit `reverse_kind` override
        // (commands/link.rs), so a pair whose reverse kind is not
        // `LinkKind::Supersedes.reverse()` (`SupersededBy`) is still valid
        // tool output, not corruption. Here ADR 1's link back to ADR 2 is
        // `RelatesTo` rather than the derived `SupersededBy`.
        repo.link(2, 1, LinkKind::Supersedes, LinkKind::RelatesTo)
            .unwrap();

        let report = check_repository(&repo).unwrap();

        assert!(
            !report.issues.iter().any(|i| i.rule_id == "asymmetric-link"),
            "a non-derived but present reverse link must not be flagged, got: {:?}",
            report
                .issues
                .iter()
                .map(|i| (&i.rule_id, &i.message))
                .collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_check_repository_broken_link_no_asymmetric_link_warning() {
        use crate::Repository;

        let temp = tempfile::tempdir().unwrap();
        // init creates ADR #1 automatically.
        let repo = Repository::init(temp.path(), None, false).unwrap();
        let adr_dir = repo.adr_path();

        // ADR 2 links to nonexistent ADR 99: a broken link, not an
        // asymmetric one. It must be reported once, as ADR013, and not
        // again as asymmetric-link.
        std::fs::write(
            adr_dir.join("0002-second.md"),
            make_nygard_adr(
                2,
                "Second",
                "Accepted",
                "\n\nSupersedes [99. Unknown](0099-unknown.md)\n",
            ),
        )
        .unwrap();

        let report = check_repository(&repo).unwrap();

        assert!(
            report.issues.iter().any(|i| i.rule_id == "ADR013"),
            "expected the broken-link diagnostic to still fire"
        );
        assert!(
            !report.issues.iter().any(|i| i.rule_id == "asymmetric-link"),
            "a link to a nonexistent ADR must not also be flagged as asymmetric, got: {:?}",
            report
                .issues
                .iter()
                .map(|i| (&i.rule_id, &i.message))
                .collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_check_repository_symmetric_relates_to_no_warning() {
        use crate::{LinkKind, Repository};

        let temp = tempfile::tempdir().unwrap();
        let repo = Repository::init(temp.path(), None, false).unwrap();
        let adr_dir = repo.adr_path();

        std::fs::write(
            adr_dir.join("0002-second.md"),
            make_nygard_adr(2, "Second", "Accepted", ""),
        )
        .unwrap();

        // `LinkKind::RelatesTo.reverse()` is `RelatesTo` itself, so a
        // symmetric `RelatesTo` pair is the default `repo.link` output.
        repo.link(2, 1, LinkKind::RelatesTo, LinkKind::RelatesTo)
            .unwrap();

        let report = check_repository(&repo).unwrap();

        assert!(
            !report.issues.iter().any(|i| i.rule_id == "asymmetric-link"),
            "a symmetric RelatesTo pair must not be flagged, got: {:?}",
            report
                .issues
                .iter()
                .map(|i| (&i.rule_id, &i.message))
                .collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_check_repository_one_way_relates_to_one_warning() {
        use crate::Repository;

        let temp = tempfile::tempdir().unwrap();
        // init creates ADR #1 with no links.
        let repo = Repository::init(temp.path(), None, false).unwrap();
        let adr_dir = repo.adr_path();

        // ADR 2 relates to ADR 1, but ADR 1 has no reciprocal link -- a
        // one-way RelatesTo is a plausible hand-written relationship, and
        // should be a single warning, not silence.
        std::fs::write(
            adr_dir.join("0002-second.md"),
            make_nygard_adr(
                2,
                "Second",
                "Accepted",
                "\n\nRelates to [1. Record architecture decisions](0001-record-architecture-decisions.md)\n",
            ),
        )
        .unwrap();

        let report = check_repository(&repo).unwrap();

        let warnings: Vec<_> = report
            .issues
            .iter()
            .filter(|i| i.rule_id == "asymmetric-link")
            .collect();
        assert_eq!(
            warnings.len(),
            1,
            "expected exactly one asymmetric-link warning for a one-way RelatesTo, got: {:?}",
            report
                .issues
                .iter()
                .map(|i| (&i.rule_id, &i.message))
                .collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_check_repository_sequential_gap_adr011() {
        use crate::Repository;

        let temp = tempfile::tempdir().unwrap();
        // init creates ADR #1 automatically; write #2 and #4 to create a gap at #3
        let repo = Repository::init(temp.path(), None, false).unwrap();
        let adr_dir = repo.adr_path();

        // ADRs 1, 2, 4 -- gap at 3
        std::fs::write(
            adr_dir.join("0002-second.md"),
            make_nygard_adr(2, "Second", "Accepted", ""),
        )
        .unwrap();
        std::fs::write(
            adr_dir.join("0004-fourth.md"),
            make_nygard_adr(4, "Fourth", "Accepted", ""),
        )
        .unwrap();

        let report = check_repository(&repo).unwrap();

        // Should have an ADR011 (sequential gap) issue
        let has_adr011 = report.issues.iter().any(|i| i.rule_id == "ADR011");
        assert!(
            has_adr011,
            "Expected ADR011 sequential-gap issue, got: {:?}",
            report.issues.iter().map(|i| &i.rule_id).collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_check_repository_clean_repo_has_no_issues() {
        use crate::Repository;

        let temp = tempfile::tempdir().unwrap();
        let repo = Repository::init(temp.path(), None, false).unwrap();
        let adr_dir = repo.adr_path();

        // Repository::init creates ADR #1 automatically -- use #2 and #3 to avoid duplicate
        std::fs::write(
            adr_dir.join("0002-second.md"),
            make_nygard_adr(2, "Second", "Accepted", ""),
        )
        .unwrap();
        std::fs::write(
            adr_dir.join("0003-third.md"),
            make_nygard_adr(3, "Third", "Proposed", ""),
        )
        .unwrap();

        let report = check_repository(&repo).unwrap();

        let collection_rule_ids = ["ADR010", "ADR011", "ADR012", "ADR013"];
        let collection_issues: Vec<_> = report
            .issues
            .iter()
            .filter(|i| collection_rule_ids.contains(&i.rule_id.as_str()))
            .collect();

        assert!(
            collection_issues.is_empty(),
            "Clean repo should have no collection-rule issues, got: {:?}",
            collection_issues
                .iter()
                .map(|i| format!("{}: {}", i.rule_id, i.message))
                .collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_check_all_combines_lint_and_repository_checks() {
        use crate::Repository;

        let temp = tempfile::tempdir().unwrap();
        let repo = Repository::init(temp.path(), None, false).unwrap();
        let adr_dir = repo.adr_path();

        // Create a valid ADR so check_all has something to process
        std::fs::write(
            adr_dir.join("0001-first.md"),
            make_nygard_adr(1, "First", "Accepted", ""),
        )
        .unwrap();

        // check_all should succeed and return a report
        let report = check_all(&repo).unwrap();

        // With a valid sequential repo, no collection-rule violations
        let adr011 = report
            .issues
            .iter()
            .filter(|i| i.rule_id == "ADR011")
            .count();
        assert_eq!(
            adr011, 0,
            "Single valid ADR should have no sequential-gap issue"
        );
    }

    // ========== check_all_filtered / [doctor].ignore (issue #316) ==========

    #[test]
    fn test_check_all_filtered_suppresses_ignored_rule() {
        use crate::Repository;

        let temp = tempfile::tempdir().unwrap();
        // init creates ADR #1 automatically; write #2 and #4 to create a gap at #3,
        // which trips ADR011 (Warning severity, confirmed via
        // test_check_repository_sequential_gap_adr011).
        let repo = Repository::init(temp.path(), None, false).unwrap();
        let adr_dir = repo.adr_path();
        std::fs::write(
            adr_dir.join("0002-second.md"),
            make_nygard_adr(2, "Second", "Accepted", ""),
        )
        .unwrap();
        std::fs::write(
            adr_dir.join("0004-fourth.md"),
            make_nygard_adr(4, "Fourth", "Accepted", ""),
        )
        .unwrap();

        // Unfiltered: check_repository still reports ADR011.
        let unfiltered = check_repository(&repo).unwrap();
        let unfiltered_adr011 = unfiltered
            .issues
            .iter()
            .filter(|i| i.rule_id == "ADR011")
            .count();
        assert!(
            unfiltered_adr011 > 0,
            "expected check_repository to report ADR011 before filtering"
        );

        // Write adrs.toml with a lowercase ignore entry, then re-open the repository
        // so the config is loaded from disk (Repository::init keeps the in-memory
        // config it built at creation time).
        std::fs::write(
            temp.path().join("adrs.toml"),
            "adr_dir = \"doc/adr\"\n\n[doctor]\nignore = [\"adr011\"]\n",
        )
        .unwrap();
        let repo = Repository::open(temp.path()).unwrap();
        assert_eq!(repo.config().doctor.ignore, vec!["adr011".to_string()]);

        // check_all (and check_all_filtered) should no longer contain ADR011,
        // proving case-insensitive matching against the real rule_id "ADR011".
        let filtered = check_all(&repo).unwrap();
        let filtered_adr011 = filtered
            .issues
            .iter()
            .filter(|i| i.rule_id == "ADR011")
            .count();
        assert_eq!(
            filtered_adr011, 0,
            "check_all should suppress ADR011 issues per [doctor].ignore"
        );

        // check_repository (unfiltered) should still report ADR011 -- filtering
        // is check_all-level only.
        let still_unfiltered = check_repository(&repo).unwrap();
        assert!(
            still_unfiltered
                .issues
                .iter()
                .any(|i| i.rule_id == "ADR011"),
            "check_repository should remain unfiltered"
        );
    }

    #[test]
    fn test_check_all_filtered_returns_suppressed_count() {
        use crate::Repository;

        let temp = tempfile::tempdir().unwrap();
        let repo = Repository::init(temp.path(), None, false).unwrap();
        let adr_dir = repo.adr_path();
        std::fs::write(
            adr_dir.join("0002-second.md"),
            make_nygard_adr(2, "Second", "Accepted", ""),
        )
        .unwrap();
        std::fs::write(
            adr_dir.join("0004-fourth.md"),
            make_nygard_adr(4, "Fourth", "Accepted", ""),
        )
        .unwrap();

        let unfiltered = check_all(&repo).unwrap();
        let unfiltered_adr011 = unfiltered
            .issues
            .iter()
            .filter(|i| i.rule_id == "ADR011")
            .count();
        assert!(unfiltered_adr011 > 0);

        std::fs::write(
            temp.path().join("adrs.toml"),
            "adr_dir = \"doc/adr\"\n\n[doctor]\nignore = [\"ADR011\"]\n",
        )
        .unwrap();
        let repo = Repository::open(temp.path()).unwrap();

        let (filtered, suppressed_count, warnings) = check_all_filtered(&repo, &[]).unwrap();
        assert_eq!(suppressed_count, unfiltered_adr011);
        assert!(
            filtered.issues.iter().all(|i| i.rule_id != "ADR011"),
            "filtered report should not contain ADR011"
        );
        assert!(
            warnings.is_empty(),
            "no [[doctor.ignore_path]] entries configured, expected no warnings"
        );
    }

    // ========== check_all_filtered / [[doctor.ignore_path]] (issue #365) ==========

    /// An ADR with an explicit Consequences body, so a test can trigger
    /// ADR014's placeholder-text check on demand.
    fn make_nygard_adr_with_consequences(
        number: u32,
        title: &str,
        status: &str,
        consequences: &str,
    ) -> String {
        format!(
            "# {number}. {title}\n\nDate: 2024-01-01\n\n## Status\n\n{status}\n\n## Context\n\nSome context.\n\n## Decision\n\nA decision.\n\n## Consequences\n\n{consequences}\n"
        )
    }

    #[test]
    fn test_check_all_filtered_scoped_ignore_suppresses_on_matching_record_only() {
        // The headline test (#365): a scoped ignore suppresses ADR014 on the
        // record it names and leaves ADR014 firing on a different record that
        // trips the same placeholder-text check.
        use crate::Repository;

        let temp = tempfile::tempdir().unwrap();
        let repo = Repository::init(temp.path(), None, false).unwrap();
        let adr_dir = repo.adr_path();

        // ADR 1 (created by init) already has real content; add two more ADRs
        // that both trip ADR014 via placeholder text in Consequences.
        std::fs::write(
            adr_dir.join("0002-second.md"),
            make_nygard_adr_with_consequences(2, "Second", "Accepted", "TBD"),
        )
        .unwrap();
        std::fs::write(
            adr_dir.join("0003-third.md"),
            make_nygard_adr_with_consequences(3, "Third", "Accepted", "TBD"),
        )
        .unwrap();

        // Unfiltered: both records trip ADR014.
        let (unfiltered, _, _) = check_all_filtered(&repo, &[]).unwrap();
        let unfiltered_adr014_paths: Vec<_> = unfiltered
            .issues
            .iter()
            .filter(|i| i.rule_id == "ADR014")
            .filter_map(|i| i.path.clone())
            .collect();
        assert!(
            unfiltered_adr014_paths
                .iter()
                .any(|p| p.ends_with("0002-second.md")),
            "expected ADR014 on 0002-second.md before scoping, got: {unfiltered_adr014_paths:?}"
        );
        assert!(
            unfiltered_adr014_paths
                .iter()
                .any(|p| p.ends_with("0003-third.md")),
            "expected ADR014 on 0003-third.md before scoping, got: {unfiltered_adr014_paths:?}"
        );

        // Scope the exemption to 0002-second.md only.
        std::fs::write(
            temp.path().join("adrs.toml"),
            "adr_dir = \"doc/adr\"\n\n[[doctor.ignore_path]]\nglob = \"doc/adr/0002-*.md\"\nrules = [\"ADR014\"]\n",
        )
        .unwrap();
        let repo = Repository::open(temp.path()).unwrap();

        let (filtered, suppressed_count, warnings) = check_all_filtered(&repo, &[]).unwrap();
        assert!(
            warnings.is_empty(),
            "expected no warnings, got {warnings:?}"
        );
        assert!(suppressed_count > 0);

        let filtered_adr014_paths: Vec<_> = filtered
            .issues
            .iter()
            .filter(|i| i.rule_id == "ADR014")
            .filter_map(|i| i.path.clone())
            .collect();
        assert!(
            !filtered_adr014_paths
                .iter()
                .any(|p| p.ends_with("0002-second.md")),
            "0002-second.md's ADR014 should be suppressed, got: {filtered_adr014_paths:?}"
        );
        assert!(
            filtered_adr014_paths
                .iter()
                .any(|p| p.ends_with("0003-third.md")),
            "0003-third.md's ADR014 should still fire, got: {filtered_adr014_paths:?}"
        );
    }

    #[test]
    fn test_check_all_filtered_scoped_ignore_double_star_matches_subdirectory() {
        // `**` must span the intermediate directory components of a nested
        // `adr_dir` (e.g. "docs/architecture/decisions", the shape used in
        // #363's own reproduction), not just match within a single directory.
        // `Repository::list` only reads ADR files directly inside `adr_dir`
        // (`max_depth(1)`), so the subdirectory being spanned here is
        // `adr_dir` itself relative to the repository root, not a
        // subdirectory of `adr_dir`.
        use crate::Repository;

        let temp = tempfile::tempdir().unwrap();
        let repo = Repository::init(
            temp.path(),
            Some(PathBuf::from("docs/architecture/decisions")),
            false,
        )
        .unwrap();
        let adr_dir = repo.adr_path();
        std::fs::write(
            adr_dir.join("0002-second.md"),
            make_nygard_adr_with_consequences(2, "Second", "Accepted", "TBD"),
        )
        .unwrap();

        std::fs::write(
            temp.path().join("adrs.toml"),
            "adr_dir = \"docs/architecture/decisions\"\n\n[[doctor.ignore_path]]\nglob = \"**/0002-*.md\"\nrules = [\"ADR014\"]\n",
        )
        .unwrap();
        let repo = Repository::open(temp.path()).unwrap();

        let (filtered, suppressed_count, warnings) = check_all_filtered(&repo, &[]).unwrap();
        assert!(
            warnings.is_empty(),
            "expected no warnings, got {warnings:?}"
        );
        assert!(
            suppressed_count > 0,
            "expected the ** glob to match the nested record"
        );
        assert!(
            !filtered.issues.iter().any(|i| i.rule_id == "ADR014"
                && i.path
                    .as_ref()
                    .is_some_and(|p| p.ends_with("0002-second.md"))),
            "nested record's ADR014 should be suppressed by the ** glob"
        );
    }

    #[test]
    fn test_check_all_filtered_scoped_ignore_matching_nothing_suppresses_nothing() {
        use crate::Repository;

        let temp = tempfile::tempdir().unwrap();
        let repo = Repository::init(temp.path(), None, false).unwrap();
        let adr_dir = repo.adr_path();
        std::fs::write(
            adr_dir.join("0002-second.md"),
            make_nygard_adr_with_consequences(2, "Second", "Accepted", "TBD"),
        )
        .unwrap();

        std::fs::write(
            temp.path().join("adrs.toml"),
            "adr_dir = \"doc/adr\"\n\n[[doctor.ignore_path]]\nglob = \"doc/adr/9999-*.md\"\nrules = [\"ADR014\"]\n",
        )
        .unwrap();
        let repo = Repository::open(temp.path()).unwrap();

        let (filtered, suppressed_count, warnings) = check_all_filtered(&repo, &[]).unwrap();
        assert!(
            warnings.is_empty(),
            "expected no warnings, got {warnings:?}"
        );
        assert_eq!(
            suppressed_count, 0,
            "a glob matching nothing should suppress nothing"
        );
        assert!(
            filtered.issues.iter().any(|i| i.rule_id == "ADR014"),
            "ADR014 should still fire since the glob did not match"
        );
    }

    #[test]
    fn test_check_all_filtered_scoped_and_repo_wide_ignores_compose() {
        use crate::Repository;

        let temp = tempfile::tempdir().unwrap();
        let repo = Repository::init(temp.path(), None, false).unwrap();
        let adr_dir = repo.adr_path();
        // 0002 trips ADR014 (scoped away below); 0003/0005 create a numbering
        // gap that trips ADR011 (suppressed repository-wide below).
        std::fs::write(
            adr_dir.join("0002-second.md"),
            make_nygard_adr_with_consequences(2, "Second", "Accepted", "TBD"),
        )
        .unwrap();
        std::fs::write(
            adr_dir.join("0003-third.md"),
            make_nygard_adr(3, "Third", "Accepted", ""),
        )
        .unwrap();
        std::fs::write(
            adr_dir.join("0005-fifth.md"),
            make_nygard_adr(5, "Fifth", "Accepted", ""),
        )
        .unwrap();

        let (unfiltered, _, _) =
            check_all_filtered(&Repository::open(temp.path()).unwrap(), &[]).unwrap();
        let unfiltered_adr014 = unfiltered
            .issues
            .iter()
            .filter(|i| i.rule_id == "ADR014")
            .count();
        let unfiltered_adr011 = unfiltered
            .issues
            .iter()
            .filter(|i| i.rule_id == "ADR011")
            .count();
        assert!(unfiltered_adr014 > 0);
        assert!(unfiltered_adr011 > 0);

        std::fs::write(
            temp.path().join("adrs.toml"),
            "adr_dir = \"doc/adr\"\n\n[doctor]\nignore = [\"ADR011\"]\n\n[[doctor.ignore_path]]\nglob = \"doc/adr/0002-*.md\"\nrules = [\"ADR014\"]\n",
        )
        .unwrap();
        let repo = Repository::open(temp.path()).unwrap();

        let (filtered, suppressed_count, warnings) = check_all_filtered(&repo, &[]).unwrap();
        assert!(
            warnings.is_empty(),
            "expected no warnings, got {warnings:?}"
        );
        assert_eq!(
            suppressed_count,
            unfiltered_adr014 + unfiltered_adr011,
            "both the scoped and repository-wide ignores should count, with no double counting"
        );
        assert!(!filtered.issues.iter().any(|i| i.rule_id == "ADR014"));
        assert!(!filtered.issues.iter().any(|i| i.rule_id == "ADR011"));
    }

    #[test]
    fn test_check_all_filtered_scoped_ignore_matches_by_rule_name() {
        use crate::Repository;

        let temp = tempfile::tempdir().unwrap();
        let repo = Repository::init(temp.path(), None, false).unwrap();
        let adr_dir = repo.adr_path();
        std::fs::write(
            adr_dir.join("0002-second.md"),
            make_nygard_adr_with_consequences(2, "Second", "Accepted", "TBD"),
        )
        .unwrap();

        // Confirm ADR014's rule name before relying on it below.
        let (unfiltered, _, _) =
            check_all_filtered(&Repository::open(temp.path()).unwrap(), &[]).unwrap();
        let rule_name = unfiltered
            .issues
            .iter()
            .find(|i| i.rule_id == "ADR014")
            .map(|i| i.rule_name.clone())
            .expect("expected an ADR014 issue");

        std::fs::write(
            temp.path().join("adrs.toml"),
            format!(
                "adr_dir = \"doc/adr\"\n\n[[doctor.ignore_path]]\nglob = \"doc/adr/0002-*.md\"\nrules = [\"{rule_name}\"]\n"
            ),
        )
        .unwrap();
        let repo = Repository::open(temp.path()).unwrap();

        let (filtered, suppressed_count, warnings) = check_all_filtered(&repo, &[]).unwrap();
        assert!(
            warnings.is_empty(),
            "expected no warnings, got {warnings:?}"
        );
        assert!(suppressed_count > 0);
        assert!(!filtered.issues.iter().any(|i| i.rule_id == "ADR014"));
    }

    #[test]
    fn test_check_all_filtered_invalid_glob_warns_and_config_still_loads() {
        use crate::Repository;

        let temp = tempfile::tempdir().unwrap();
        let repo = Repository::init(temp.path(), None, false).unwrap();
        let adr_dir = repo.adr_path();
        std::fs::write(
            adr_dir.join("0002-second.md"),
            make_nygard_adr_with_consequences(2, "Second", "Accepted", "TBD"),
        )
        .unwrap();

        // '[' with no closing ']' is an invalid glob pattern.
        std::fs::write(
            temp.path().join("adrs.toml"),
            "adr_dir = \"doc/adr\"\n\n[[doctor.ignore_path]]\nglob = \"doc/adr/[0002-*.md\"\nrules = [\"ADR014\"]\n",
        )
        .unwrap();
        let repo = Repository::open(temp.path()).unwrap();

        let (filtered, suppressed_count, warnings) = check_all_filtered(&repo, &[]).unwrap();
        assert_eq!(
            suppressed_count, 0,
            "an invalid glob must not suppress anything"
        );
        assert!(
            filtered.issues.iter().any(|i| i.rule_id == "ADR014"),
            "ADR014 should still fire since the invalid glob was not applied"
        );
        assert!(
            warnings.iter().any(|w| w.contains("doc/adr/[0002-*.md")),
            "expected a warning naming the invalid glob, got: {warnings:?}"
        );
    }

    #[test]
    fn test_check_all_filtered_ignore_path_naming_collection_rule_warns() {
        use crate::Repository;

        let temp = tempfile::tempdir().unwrap();
        let repo = Repository::init(temp.path(), None, false).unwrap();
        let adr_dir = repo.adr_path();
        std::fs::write(
            adr_dir.join("0002-second.md"),
            make_nygard_adr(2, "Second", "Accepted", ""),
        )
        .unwrap();

        // ADR011 (sequential numbering) is an upstream collection rule and
        // never carries a path, so this exemption can never fire.
        std::fs::write(
            temp.path().join("adrs.toml"),
            "adr_dir = \"doc/adr\"\n\n[[doctor.ignore_path]]\nglob = \"doc/adr/0002-*.md\"\nrules = [\"ADR011\"]\n",
        )
        .unwrap();
        let repo = Repository::open(temp.path()).unwrap();

        let (_filtered, _suppressed_count, warnings) = check_all_filtered(&repo, &[]).unwrap();
        assert!(
            warnings.iter().any(|w| w.contains("ADR011")),
            "expected a warning naming the rule that can never fire, got: {warnings:?}"
        );
    }
}