lattice 0.2.0

A markdown predicate linter and backlink reconciler, shipped as an LSP server.
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
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2026 Two Wells <contact@twowells.dev>

//! Link graph validation.
//!
//! Checks forward links and backlink consistency across the workspace:
//! target existence, predicate vocabulary, predicate policy compliance,
//! and frontmatter backlink reconciliation.

use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::path::{Path, PathBuf};

use crate::block::{self, HeadingId, LinkKind};
use crate::config::{Config, ConnectivityPolicy, FragmentAlgorithm, PredicatePolicy};
use crate::span::Span;
use crate::workspace::Workspace;

/// Diagnostic severity level.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Severity {
    /// Fatal issue that must be fixed.
    Error,
    /// Non-fatal issue worth addressing.
    Warning,
    /// Informational note.
    Info,
    /// Suggestion — lowest severity.
    Hint,
}

/// A diagnostic produced by validation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Diagnostic {
    /// Workspace-relative path of the file containing the issue.
    pub file: PathBuf,
    /// 1-based line number.
    pub line: usize,
    /// Severity of the diagnostic.
    pub severity: Severity,
    /// Human-readable description of the issue.
    pub message: String,
    /// Byte span of the offending text, when a precise location is known.
    ///
    /// `Some` yields a precise LSP range; `None` falls back to a whole-line
    /// range anchored on `line` (used by line-level diagnostics such as
    /// backlinks). `line` stays authoritative for sorting and the CLI's
    /// `path:line:` output regardless.
    pub span: Option<Span>,
}

/// Validate forward links across all files in the workspace.
///
/// Checks each intra-project and non-markdown link for:
/// - Target file existence.
/// - Predicate membership in the configured vocabulary.
/// - Predicate policy compliance (optional vs required).
/// - Fragment resolution against headings in the target document.
pub fn validate_forward_links(workspace: &Workspace) -> Vec<Diagnostic> {
    let config = workspace.config();
    let mut diagnostics = Vec::new();

    for (file_path, file_data) in workspace.files() {
        for link in &file_data.links {
            match &link.kind {
                LinkKind::External { .. } => {}

                LinkKind::IntraDocument { fragment } => {
                    // Same-document anchor (`[…](#heading)`): resolve against
                    // this file's own headings, exactly as a cross-file
                    // fragment resolves against its target's. A dangling
                    // in-page anchor is as broken as a dangling cross-file one
                    // (issue 021).
                    check_fragment(
                        workspace,
                        config,
                        file_path,
                        link.line,
                        link.span,
                        file_path,
                        fragment,
                        &mut diagnostics,
                    );
                }

                LinkKind::NonMarkdown { target } => {
                    check_target_exists(
                        workspace,
                        file_path,
                        link.line,
                        link.span,
                        target,
                        &mut diagnostics,
                    );
                }

                LinkKind::IntraProject {
                    target,
                    fragment,
                    predicate,
                    explicit_predicate,
                } => {
                    check_target_exists(
                        workspace,
                        file_path,
                        link.line,
                        link.span,
                        target,
                        &mut diagnostics,
                    );
                    check_predicate(
                        config,
                        file_path,
                        link.line,
                        link.span,
                        predicate,
                        *explicit_predicate,
                        &mut diagnostics,
                    );
                    if let Some(frag) = fragment {
                        check_fragment(
                            workspace,
                            config,
                            file_path,
                            link.line,
                            link.span,
                            target,
                            frag,
                            &mut diagnostics,
                        );
                    }
                }
            }
        }
    }

    diagnostics.sort_by(|a, b| a.file.cmp(&b.file).then(a.line.cmp(&b.line)));
    diagnostics
}

/// Check that a link target exists as a file in the workspace or on disk.
fn check_target_exists(
    workspace: &Workspace,
    source: &Path,
    line: usize,
    span: Span,
    target: &Path,
    diagnostics: &mut Vec<Diagnostic>,
) {
    let is_markdown = target.extension().is_some_and(|ext| ext == "md");

    let exists = if is_markdown {
        workspace.file(target).is_some()
    } else {
        workspace.root().join(target).is_file()
    };

    if !exists {
        diagnostics.push(Diagnostic {
            file: source.to_path_buf(),
            line,
            severity: Severity::Error,
            message: format!("link target does not exist: {}", target.display()),
            span: Some(span),
        });
    }
}

/// Check predicate validity and policy compliance.
fn check_predicate(
    config: &Config,
    source: &Path,
    line: usize,
    span: Span,
    predicate: &str,
    explicit: bool,
    diagnostics: &mut Vec<Diagnostic>,
) {
    if explicit {
        // Decision 008: a forward link may name either member of a pair —
        // a known forward predicate or a known inverse. Only a predicate in
        // neither direction is an error.
        if !config.is_known_predicate(predicate) {
            let known: Vec<&str> = config.predicates.keys().map(String::as_str).collect();
            diagnostics.push(Diagnostic {
                file: source.to_path_buf(),
                line,
                severity: Severity::Error,
                message: format!(
                    "unknown predicate '{predicate}': choose from [{}]",
                    known.join(", ")
                ),
                span: Some(span),
            });
        }
    } else {
        match config.policy.predicates {
            PredicatePolicy::Optional => {
                diagnostics.push(Diagnostic {
                    file: source.to_path_buf(),
                    line,
                    severity: Severity::Info,
                    message: "link has no explicit predicate (defaulting to 'references')"
                        .to_string(),
                    span: Some(span),
                });
            }
            PredicatePolicy::Required => {
                let known: Vec<&str> = config.predicates.keys().map(String::as_str).collect();
                diagnostics.push(Diagnostic {
                    file: source.to_path_buf(),
                    line,
                    severity: Severity::Error,
                    message: format!("link missing predicate: choose from [{}]", known.join(", ")),
                    span: Some(span),
                });
            }
        }
    }
}

/// Validate backlink consistency across the workspace.
///
/// Computes expected backlinks from forward links, diffs against actual
/// frontmatter, and emits warnings for missing or stale backlinks.
/// Returns an empty list when `backlinks = false` in the policy.
pub fn validate_backlinks(workspace: &Workspace) -> Vec<Diagnostic> {
    if !workspace.config().policy.backlinks {
        return Vec::new();
    }

    let expected = build_expected_backlinks(workspace);
    let mut diagnostics = Vec::new();

    check_missing_backlinks(workspace, &expected, &mut diagnostics);
    check_stale_backlinks(workspace, &expected, &mut diagnostics);

    diagnostics.sort_by(|a, b| a.file.cmp(&b.file).then(a.line.cmp(&b.line)));
    diagnostics
}

/// Location of the forward link in a source document that creates a backlink
/// expectation.
///
/// Carried so a missing-backlink diagnostic can anchor on the *source* link
/// (the present artifact) rather than on the target's absent frontmatter
/// entry. When a source links to the same target more than once with the same
/// predicate, the earliest link is kept.
#[derive(Debug, Clone, Copy)]
struct ExpectedSource {
    /// 1-based line of the forward link in the source document.
    line: usize,
    /// Byte span of the forward link in the source document.
    span: Span,
}

/// Map from a target file to its expected backlinks.
///
/// `target file → { backlink label → { source file → forward-link location } }`.
/// The backlink label is the *opposite* member of the forward link's predicate
/// pair (decision 008), so an inverse-predicate link derives the forward label.
/// All paths are workspace-relative.
type ExpectedBacklinks = HashMap<PathBuf, HashMap<String, BTreeMap<PathBuf, ExpectedSource>>>;

/// Build expected backlinks from all forward links in the workspace.
///
/// Returns a map keyed by target file. Each source carries the position of
/// the forward link that created the expectation, so missing-backlink
/// diagnostics can anchor on the source.
fn build_expected_backlinks(workspace: &Workspace) -> ExpectedBacklinks {
    let config = workspace.config();
    let mut expected: ExpectedBacklinks = HashMap::new();

    for (source_path, file_data) in workspace.files() {
        for link in &file_data.links {
            if let LinkKind::IntraProject {
                target, predicate, ..
            } = &link.kind
            {
                // Skip broken targets and unknown predicates — forward validation handles those.
                if workspace.file(target).is_none() {
                    continue;
                }
                // Key by the opposite member of the pair (decision 008): an
                // inverse-predicate link (`"superseded_by"`) derives the
                // forward label (`"supersedes"`) on its target.
                let Some(opposite) = config.opposite_of(predicate) else {
                    continue;
                };

                expected
                    .entry(target.clone())
                    .or_default()
                    .entry(opposite.to_string())
                    .or_default()
                    .entry(source_path.clone())
                    .and_modify(|loc| {
                        // Keep the earliest link when a source links a target
                        // more than once under the same predicate.
                        if link.line < loc.line {
                            *loc = ExpectedSource {
                                line: link.line,
                                span: link.span,
                            };
                        }
                    })
                    .or_insert(ExpectedSource {
                        line: link.line,
                        span: link.span,
                    });
            }
        }
    }

    expected
}

/// Resolve a backlink path (file-relative) to a workspace-relative path.
///
/// Backlink paths in frontmatter are relative to the file containing them,
/// just like forward link targets. This joins the containing file's parent
/// directory with the backlink path and normalizes the result. Shared with the
/// LSP navigation handlers so that path resolution stays identical to the
/// consistency check.
pub fn resolve_backlink_path(containing_file: &Path, backlink_path: &str) -> PathBuf {
    let dir = containing_file.parent().unwrap_or_else(|| Path::new(""));
    block::normalize_path(&dir.join(backlink_path))
}

/// Compute the relative path from one file to another.
///
/// Both paths must be workspace-relative. Returns the path you would write
/// in `from`'s frontmatter to reference `to` — relative to `from`'s
/// parent directory.
fn file_relative(from: &Path, to: &Path) -> PathBuf {
    let from_dir = from.parent().unwrap_or_else(|| Path::new(""));
    let from_parts: Vec<_> = from_dir.components().collect();
    let to_parts: Vec<_> = to.components().collect();

    let common = from_parts
        .iter()
        .zip(&to_parts)
        .take_while(|(a, b)| a == b)
        .count();

    let mut result = PathBuf::new();
    for _ in common..from_parts.len() {
        result.push("..");
    }
    for part in &to_parts[common..] {
        result.push(part);
    }
    result
}

/// Emit warnings for expected backlinks missing from frontmatter.
///
/// The diagnostic anchors on the *source* document at the forward link — the
/// present artifact — rather than on the target's absent frontmatter entry.
/// This puts the warning on the file an agent is editing when it adds the
/// link, giving it an entry point to walk the graph to the target. The fix
/// still belongs in the target's frontmatter; the message names it.
///
/// An obligation is satisfied by *either* a frontmatter backlink *or* a
/// reciprocal forward link from the target back to the source carrying the
/// matching paired predicate (decision 008). A warning fires only when
/// neither exists.
fn check_missing_backlinks(
    workspace: &Workspace,
    expected: &ExpectedBacklinks,
    diagnostics: &mut Vec<Diagnostic>,
) {
    for (target_path, expected_backlinks) in expected {
        let actual = workspace
            .file(target_path)
            .and_then(|f| f.frontmatter.as_ref())
            .map(|fm| &fm.backlinks);

        for (backlink_key, expected_sources) in expected_backlinks {
            let actual_sources: BTreeSet<PathBuf> = actual
                .and_then(|a| a.get(backlink_key.as_str()))
                .map(|paths| {
                    paths
                        .iter()
                        .map(|p| resolve_backlink_path(target_path, p))
                        .collect()
                })
                .unwrap_or_default();

            for (source, loc) in expected_sources {
                // Satisfied by a materialized frontmatter backlink...
                if actual_sources.contains(source) {
                    continue;
                }
                // ...or by a reciprocal forward link from the target back to
                // the source under the same label (decision 008). The floor is
                // met from both ends, so no frontmatter copy is required.
                if has_reciprocal_forward_link(workspace, target_path, source, backlink_key) {
                    continue;
                }
                let rel = file_relative(source, target_path);
                diagnostics.push(Diagnostic {
                    file: source.clone(),
                    line: loc.line,
                    severity: Severity::Warning,
                    message: format!("expected backlink `{backlink_key}` in `{}`", rel.display()),
                    span: Some(loc.span),
                });
            }
        }
    }
}

/// Returns `true` if `target` has a forward link to `source` carrying
/// `predicate` — a reciprocal forward link that satisfies a backlink
/// obligation without a frontmatter entry (decision 008).
///
/// The obligation's label is already in the target's own forward direction
/// (it is `opposite_of` the source's link predicate), so the reciprocal link
/// satisfies it exactly when its predicate equals that label.
fn has_reciprocal_forward_link(
    workspace: &Workspace,
    target: &Path,
    source: &Path,
    predicate: &str,
) -> bool {
    let Some(target_data) = workspace.file(target) else {
        return false;
    };
    target_data.links.iter().any(|link| {
        matches!(
            &link.kind,
            LinkKind::IntraProject { target: t, predicate: p, .. }
                if t == source && p == predicate
        )
    })
}

/// Emit warnings for frontmatter backlinks with no corresponding forward link.
fn check_stale_backlinks(
    workspace: &Workspace,
    expected: &ExpectedBacklinks,
    diagnostics: &mut Vec<Diagnostic>,
) {
    for (file_path, file_data) in workspace.files() {
        let Some(fm) = &file_data.frontmatter else {
            continue;
        };

        for (backlink_key, sources) in &fm.backlinks {
            let expected_sources = expected
                .get(file_path)
                .and_then(|e| e.get(backlink_key.as_str()));

            for source_str in sources {
                let resolved = resolve_backlink_path(file_path, source_str);
                let is_expected = expected_sources.is_some_and(|set| set.contains_key(&resolved));

                if !is_expected {
                    diagnostics.push(Diagnostic {
                        file: file_path.clone(),
                        line: fm.start_line,
                        severity: Severity::Warning,
                        message: format!(
                            "backlink `{backlink_key}` from `{source_str}` has no corresponding forward link"
                        ),
                        span: None,
                    });
                }
            }
        }
    }
}

/// Check that a fragment resolves to a heading in the target document.
///
/// Explicit `{#id}` anchors are checked first (exact match). For computed
/// slugs, the algorithm policy determines which slugs are considered.
/// Skips the check when the target file does not exist (forward link
/// validation handles that case). The HTML "top of document" idioms — an
/// empty fragment (`#`) and `#top` (ASCII case-insensitive) — are always
/// valid and never flagged.
#[allow(
    clippy::too_many_arguments,
    reason = "validation context parameters are distinct concerns"
)]
fn check_fragment(
    workspace: &Workspace,
    config: &Config,
    source: &Path,
    line: usize,
    span: Span,
    target: &Path,
    fragment: &str,
    diagnostics: &mut Vec<Diagnostic>,
) {
    let Some(target_data) = workspace.file(target) else {
        return;
    };

    // `#` (empty) and `#top` (ASCII case-insensitive) are the HTML
    // "top of document" idioms: a renderer scrolls to the top regardless of
    // headings (a real `top` heading, if present, just takes precedence). Both
    // are valid, so never flag them — for same-document and cross-file links
    // alike (issue 021).
    if fragment.is_empty() || fragment.eq_ignore_ascii_case("top") {
        return;
    }

    let algorithm = config.policy.fragments;
    let headings = &target_data.headings;

    let found_heading = headings.iter().any(|heading| match &heading.id {
        HeadingId::Explicit(id) => id == fragment,
        HeadingId::Computed {
            github,
            gitlab,
            vscode,
        } => match algorithm {
            Some(FragmentAlgorithm::Github) => github == fragment,
            Some(FragmentAlgorithm::Gitlab) => gitlab == fragment,
            Some(FragmentAlgorithm::Vscode) => vscode == fragment,
            None => github == fragment || gitlab == fragment || vscode == fragment,
        },
    });

    // A fragment also resolves against an explicit raw-HTML anchor target —
    // `<a id="x"></a>` or `<a name="x">` — defined anywhere in the document.
    // Such anchors are link targets, not link sources, and `#x` resolves to
    // them exactly as it does to a heading slug (issue 025).
    let found = found_heading
        || target_data
            .anchors
            .iter()
            .any(|anchor| anchor.id == fragment);

    if !found {
        diagnostics.push(Diagnostic {
            file: source.to_path_buf(),
            line,
            severity: Severity::Error,
            message: format!(
                "fragment `#{}` not found in `{}`",
                fragment,
                target.display()
            ),
            span: Some(span),
        });
    }
}

/// Validate graph connectivity (topology) across the workspace.
///
/// Flags isolated documents per the configured [`ConnectivityPolicy`]:
///
/// - `no-orphans` — any non-root document with no intra-project edge.
/// - `no-islands` — any non-root document outside a root's connected
///   component (edges undirected).
/// - `reachable` — any non-root document not forward-reachable from a root.
///
/// Returns an empty list when `connectivity = "off"` (the default). The
/// diagnostic anchors on the isolated document itself (line 1), mirroring the
/// missing-backlink-on-target convention.
///
/// Edges are built from valid intra-project markdown forward links (target
/// must exist; self-loops excluded). A link counts as an edge regardless of
/// predicate validity — connectivity asks whether documents reference each
/// other, which is orthogonal to the predicate vocabulary; an unknown
/// predicate is a separate diagnostic. Roots are exempt at every level. When
/// no configured root resolves to an indexed file, `no-islands` and
/// `reachable` emit nothing (they have no anchor to traverse from), while
/// `no-orphans` still runs (it uses roots only for exemption).
pub fn validate_connectivity(workspace: &Workspace) -> Vec<Diagnostic> {
    let level = workspace.config().policy.connectivity;
    if level == ConnectivityPolicy::Off {
        return Vec::new();
    }

    let files = workspace.files();

    // Build forward (directed) and undirected adjacency over valid
    // intra-project markdown edges in a single pass.
    let mut forward: HashMap<&Path, BTreeSet<&Path>> = HashMap::new();
    let mut undirected: HashMap<&Path, BTreeSet<&Path>> = HashMap::new();
    for (source, file_data) in files {
        let src = source.as_path();
        for link in &file_data.links {
            let LinkKind::IntraProject { target, .. } = &link.kind else {
                continue;
            };
            // Skip broken targets (forward validation handles those) and
            // self-loops (a document linking to itself does not connect it to
            // any *other* document).
            let Some((target_key, _)) = files.get_key_value(target) else {
                continue;
            };
            let dst = target_key.as_path();
            if dst == src {
                continue;
            }
            forward.entry(src).or_default().insert(dst);
            undirected.entry(src).or_default().insert(dst);
            undirected.entry(dst).or_default().insert(src);
        }
    }

    // Resolve configured roots to indexed files (normalized).
    let root_set: BTreeSet<&Path> = workspace
        .config()
        .policy
        .roots
        .iter()
        .filter_map(|r| {
            files
                .get_key_value(&block::normalize_path(r))
                .map(|(k, _)| k.as_path())
        })
        .collect();

    let mut diagnostics = Vec::new();
    let flag = |node: &Path, message: &str, diags: &mut Vec<Diagnostic>| {
        diags.push(Diagnostic {
            file: node.to_path_buf(),
            line: 1,
            severity: Severity::Warning,
            message: message.to_string(),
            span: None,
        });
    };

    match level {
        ConnectivityPolicy::Off => {}
        ConnectivityPolicy::NoOrphans => {
            for path in files.keys() {
                let node = path.as_path();
                if root_set.contains(node) {
                    continue;
                }
                if undirected.get(node).is_none_or(BTreeSet::is_empty) {
                    flag(
                        node,
                        "orphaned document: no links to or from any other document",
                        &mut diagnostics,
                    );
                }
            }
        }
        ConnectivityPolicy::NoIslands | ConnectivityPolicy::Reachable => {
            if root_set.is_empty() {
                tracing::debug!(
                    "connectivity: no configured root resolves to an indexed file; skipping {level:?}"
                );
                return Vec::new();
            }
            let (adjacency, message) = if level == ConnectivityPolicy::NoIslands {
                (
                    &undirected,
                    "isolated document: not connected to the project graph",
                )
            } else {
                (
                    &forward,
                    "unreachable document: not reachable from any root by forward links",
                )
            };
            let visited = flood(&root_set, adjacency);
            for path in files.keys() {
                let node = path.as_path();
                if root_set.contains(node) {
                    continue;
                }
                if !visited.contains(node) {
                    flag(node, message, &mut diagnostics);
                }
            }
        }
    }

    diagnostics.sort_by(|a, b| a.file.cmp(&b.file).then(a.line.cmp(&b.line)));
    diagnostics
}

/// Flood-fill the set of nodes reachable from `roots` over `adjacency`.
///
/// Iterative DFS; pass undirected adjacency for `no-islands` (component
/// membership) or forward adjacency for `reachable` (directed navigability).
fn flood<'a>(
    roots: &BTreeSet<&'a Path>,
    adjacency: &HashMap<&'a Path, BTreeSet<&'a Path>>,
) -> BTreeSet<&'a Path> {
    let mut visited: BTreeSet<&Path> = BTreeSet::new();
    let mut stack: Vec<&Path> = roots.iter().copied().collect();
    while let Some(node) = stack.pop() {
        if !visited.insert(node) {
            continue;
        }
        if let Some(neighbors) = adjacency.get(node) {
            for &next in neighbors {
                if !visited.contains(next) {
                    stack.push(next);
                }
            }
        }
    }
    visited
}

/// Collect all diagnostics for the workspace.
///
/// Runs every validation check (forward links, backlinks, connectivity, bare
/// paths), collects unknown backlink predicate errors from frontmatter, and
/// includes frontmatter parse errors. Returns diagnostics sorted by
/// file then line number.
pub fn collect_all(workspace: &Workspace) -> Vec<Diagnostic> {
    if !workspace.has_config() {
        return Vec::new();
    }

    let mut diagnostics = Vec::new();
    diagnostics.extend(validate_forward_links(workspace));
    diagnostics.extend(validate_backlinks(workspace));
    diagnostics.extend(validate_connectivity(workspace));
    // Note: bare paths are emitted by the structural diagnostics layer
    // unconditionally — not duplicated here.

    for (path, file_data) in workspace.files() {
        for bd in &file_data.backlink_diagnostics {
            diagnostics.push(Diagnostic {
                file: path.clone(),
                line: bd.line,
                severity: Severity::Error,
                message: format!("unknown backlink predicate `{}`", bd.predicate),
                span: None,
            });
        }
        // Note: parse_diagnostics (frontmatter errors) are emitted by the
        // structural diagnostics layer unconditionally — not duplicated here.
    }

    diagnostics.sort_by(|a, b| a.file.cmp(&b.file).then(a.line.cmp(&b.line)));
    diagnostics
}

#[cfg(test)]
#[allow(
    clippy::expect_used,
    clippy::panic,
    reason = "tests use expect and panic for clarity"
)]
mod tests {
    use std::fs;

    use tempfile::TempDir;

    use super::*;
    use crate::workspace::Workspace;

    /// Create a workspace with the given files and scan it.
    fn setup_workspace(files: &[(&str, &str)]) -> (TempDir, Workspace) {
        let dir = TempDir::new().expect("create temp dir");
        // Create a .git directory so the workspace root is found.
        fs::create_dir(dir.path().join(".git")).expect("create .git");

        for (path, content) in files {
            let full = dir.path().join(path);
            if let Some(parent) = full.parent() {
                fs::create_dir_all(parent).expect("create parent dirs");
            }
            fs::write(&full, content).expect("write file");
        }

        let ws = Workspace::scan(dir.path()).expect("scan workspace");
        (dir, ws)
    }

    #[test]
    fn valid_link_with_known_predicate() {
        let (_dir, ws) = setup_workspace(&[
            ("index.md", r#"[other](other.md "references")"#),
            ("other.md", "# Other\n"),
        ]);

        let diags = validate_forward_links(&ws);
        assert!(
            diags.iter().all(|d| d.severity != Severity::Error),
            "no errors for valid link: {diags:?}"
        );
    }

    #[test]
    fn broken_link_target() {
        let (_dir, ws) =
            setup_workspace(&[("index.md", r#"[missing](nonexistent.md "references")"#)]);

        let diags = validate_forward_links(&ws);
        let errors: Vec<_> = diags
            .iter()
            .filter(|d| d.severity == Severity::Error)
            .collect();

        assert_eq!(errors.len(), 1, "one error for broken link");
        assert!(
            errors[0].message.contains("does not exist"),
            "message mentions non-existence: {}",
            errors[0].message
        );
        assert!(
            errors[0].message.contains("nonexistent.md"),
            "message includes target path: {}",
            errors[0].message
        );
    }

    #[test]
    fn unknown_predicate() {
        let (_dir, ws) = setup_workspace(&[
            ("index.md", r#"[other](other.md "invented_predicate")"#),
            ("other.md", "# Other\n"),
        ]);

        let diags = validate_forward_links(&ws);
        let errors: Vec<_> = diags
            .iter()
            .filter(|d| d.severity == Severity::Error)
            .collect();

        assert_eq!(errors.len(), 1, "one error for unknown predicate");
        assert!(
            errors[0].message.contains("unknown predicate"),
            "message mentions unknown predicate: {}",
            errors[0].message
        );
        assert!(
            errors[0].message.contains("invented_predicate"),
            "message includes the bad predicate: {}",
            errors[0].message
        );
    }

    #[test]
    fn missing_predicate_optional_policy() {
        let (_dir, ws) =
            setup_workspace(&[("index.md", "[other](other.md)"), ("other.md", "# Other\n")]);

        let diags = validate_forward_links(&ws);

        let infos: Vec<_> = diags
            .iter()
            .filter(|d| d.severity == Severity::Info)
            .collect();
        let has_errors = diags.iter().any(|d| d.severity == Severity::Error);

        assert_eq!(infos.len(), 1, "one info for implicit predicate");
        assert!(!has_errors, "no errors under optional policy");
        assert!(
            infos[0].message.contains("no explicit predicate"),
            "message describes missing predicate: {}",
            infos[0].message
        );
    }

    #[test]
    fn missing_predicate_required_policy() {
        let config_toml = "\
[policy]
predicates = \"required\"
";
        let (_dir, ws) = setup_workspace(&[
            (".lattice.toml", config_toml),
            ("index.md", "[other](other.md)"),
            ("other.md", "# Other\n"),
        ]);

        let diags = validate_forward_links(&ws);
        let errors: Vec<_> = diags
            .iter()
            .filter(|d| d.severity == Severity::Error)
            .collect();

        assert_eq!(errors.len(), 1, "one error for missing predicate");
        assert!(
            errors[0].message.contains("missing predicate"),
            "message describes missing predicate: {}",
            errors[0].message
        );
    }

    #[test]
    fn external_links_skipped() {
        let (_dir, ws) = setup_workspace(&[(
            "index.md",
            "[ext](https://example.com) [mail](mailto:a@b.com)",
        )]);

        let diags = validate_forward_links(&ws);
        assert!(diags.is_empty(), "no diagnostics for external links");
    }

    #[test]
    fn non_markdown_target_exists() {
        let (_dir, ws) = setup_workspace(&[
            ("index.md", "[diagram](arch.png)"),
            ("arch.png", "fake png"),
        ]);

        let diags = validate_forward_links(&ws);
        assert!(
            diags.iter().all(|d| d.severity != Severity::Error),
            "no errors when non-markdown target exists"
        );
    }

    #[test]
    fn non_markdown_target_missing() {
        let (_dir, ws) = setup_workspace(&[("index.md", "[diagram](missing.png)")]);

        let diags = validate_forward_links(&ws);
        let errors: Vec<_> = diags
            .iter()
            .filter(|d| d.severity == Severity::Error)
            .collect();

        assert_eq!(errors.len(), 1, "one error for missing non-markdown target");
        assert!(
            errors[0].message.contains("does not exist"),
            "message mentions non-existence: {}",
            errors[0].message
        );
    }

    // Issue 028: narrowing the dark-matter *hints* to `.md` must not touch
    // link-existence validation — a broken `[x](main.rs)` link still errors.
    #[test]
    fn broken_non_markdown_link_still_errors_after_md_narrowing() {
        let (_dir, ws) = setup_workspace(&[("index.md", "[code](main.rs)")]);

        let diags = validate_forward_links(&ws);
        let errors: Vec<_> = diags
            .iter()
            .filter(|d| d.severity == Severity::Error)
            .collect();

        assert_eq!(
            errors.len(),
            1,
            "a broken non-`.md` link still errors: {diags:?}"
        );
        assert!(
            errors[0].message.contains("does not exist"),
            "message mentions non-existence: {}",
            errors[0].message
        );
    }

    #[test]
    fn diagnostics_sorted_by_file_and_line() {
        let (_dir, ws) = setup_workspace(&[
            (
                "b.md",
                r#"[x](missing1.md "references")
[y](missing2.md "references")"#,
            ),
            ("a.md", r#"[z](missing3.md "references")"#),
        ]);

        let diags = validate_forward_links(&ws);
        let error_files: Vec<_> = diags
            .iter()
            .filter(|d| d.severity == Severity::Error)
            .map(|d| (d.file.clone(), d.line))
            .collect();

        for window in error_files.windows(2) {
            assert!(
                window[0] <= window[1],
                "diagnostics should be sorted: {:?} should come before {:?}",
                window[0],
                window[1]
            );
        }
    }

    #[test]
    fn link_in_subdirectory() {
        let (_dir, ws) = setup_workspace(&[
            ("docs/guide.md", r#"[ref](../README.md "references")"#),
            ("README.md", "# README\n"),
        ]);

        let diags = validate_forward_links(&ws);
        assert!(
            diags.iter().all(|d| d.severity != Severity::Error),
            "no errors for valid cross-directory link: {diags:?}"
        );
    }

    // --- Root-relative `/` resolution (issue 028) ---

    #[test]
    fn root_relative_link_resolves_at_workspace_root() {
        // GitHub resolves `/README.md` against the repository root, not the
        // filesystem root or the source file's directory. From a deeply nested
        // file, `[x](/README.md)` must therefore find `<root>/README.md`.
        let (_dir, ws) = setup_workspace(&[
            ("a/b/c.md", r#"[x](/README.md "references")"#),
            ("README.md", "# README\n"),
        ]);

        let diags = validate_forward_links(&ws);
        assert!(
            diags.iter().all(|d| d.severity != Severity::Error),
            "root-relative `/README.md` from a nested file resolves at the workspace root: {diags:?}"
        );
    }

    #[test]
    fn root_relative_link_to_missing_target_errors() {
        // A root-relative target that does not exist still errors — now
        // reported against the root-resolved path.
        let (_dir, ws) = setup_workspace(&[("a/b/c.md", r#"[x](/nope.md "references")"#)]);

        let diags = validate_forward_links(&ws);
        let errors: Vec<_> = diags
            .iter()
            .filter(|d| d.severity == Severity::Error)
            .collect();

        assert_eq!(
            errors.len(),
            1,
            "a missing root-relative target still errors: {diags:?}"
        );
        assert!(
            errors[0].message.contains("does not exist"),
            "message reports the missing target: {}",
            errors[0].message
        );
    }

    #[test]
    fn root_relative_resolution_independent_of_nesting_depth() {
        // The same `/README.md` resolves identically from the root and from a
        // deep subdirectory — root-anchoring is depth-independent.
        let (_dir, ws) = setup_workspace(&[
            ("root.md", r#"[x](/README.md "references")"#),
            ("a/b/c/d/deep.md", r#"[x](/README.md "references")"#),
            ("README.md", "# README\n"),
        ]);

        let diags = validate_forward_links(&ws);
        assert!(
            diags.iter().all(|d| d.severity != Severity::Error),
            "root-relative `/README.md` resolves the same at every depth: {diags:?}"
        );
    }

    #[test]
    fn root_relative_non_markdown_link_resolves_at_root() {
        // A root-relative non-markdown target resolves at the workspace root on
        // disk too (and cannot escape it), so an existing `<root>/logo.png`
        // referenced as `/logo.png` from a nested file does not error.
        let (_dir, ws) =
            setup_workspace(&[("a/b/c.md", "[logo](/logo.png)"), ("logo.png", "fake png")]);

        let diags = validate_forward_links(&ws);
        assert!(
            diags.iter().all(|d| d.severity != Severity::Error),
            "root-relative non-markdown target resolves at the workspace root: {diags:?}"
        );
    }

    #[test]
    fn protocol_relative_and_http_links_stay_external() {
        // `http(s)://` and protocol-relative `//host/...` are URLs, never
        // workspace paths: they must classify as external and produce no
        // target-existence error.
        let (_dir, ws) = setup_workspace(&[(
            "a/b/c.md",
            "[h](https://example.com/README.md) \
             [p](//cdn.example.com/lib.md) \
             [q](http://example.com/x.md)",
        )]);

        let diags = validate_forward_links(&ws);
        assert!(
            diags.is_empty(),
            "external and protocol-relative links are not workspace paths: {diags:?}"
        );
    }

    // --- Backlink validation ---

    #[test]
    fn backlink_present_no_warning() {
        let target = "\
---
backlinks:
  referenced_by:
    - index.md
---
# Target
";
        let (_dir, ws) = setup_workspace(&[
            ("index.md", r#"[target](target.md "references")"#),
            ("target.md", target),
        ]);

        let diags = validate_backlinks(&ws);
        assert!(
            diags.is_empty(),
            "no warnings when backlink is present: {diags:?}"
        );
    }

    #[test]
    fn missing_backlink_warning() {
        let (_dir, ws) = setup_workspace(&[
            ("index.md", r#"[target](target.md "supersedes")"#),
            ("target.md", "# Target\n"),
        ]);

        let diags = validate_backlinks(&ws);
        let warnings: Vec<_> = diags
            .iter()
            .filter(|d| d.severity == Severity::Warning)
            .collect();

        assert_eq!(warnings.len(), 1, "one warning for missing backlink");
        assert!(
            warnings[0].message.contains("superseded_by"),
            "message names the inverse predicate: {}",
            warnings[0].message
        );
        assert!(
            warnings[0].message.contains("target.md"),
            "message names the target file the backlink belongs in: {}",
            warnings[0].message
        );
        assert_eq!(
            warnings[0].file,
            Path::new("index.md"),
            "diagnostic anchors on the source (the forward link), not the target"
        );
    }

    #[test]
    fn missing_backlink_anchors_on_source_link_line() {
        // The forward link sits on line 3 of the source. The missing-backlink
        // diagnostic must point there (with a span), not at the target.
        let (_dir, ws) = setup_workspace(&[
            (
                "index.md",
                "# Index\n\n[target](target.md \"supersedes\")\n",
            ),
            ("target.md", "# Target\n"),
        ]);

        let diags = validate_backlinks(&ws);
        let warnings: Vec<_> = diags
            .iter()
            .filter(|d| d.severity == Severity::Warning)
            .collect();

        assert_eq!(warnings.len(), 1, "one warning for missing backlink");
        assert_eq!(
            warnings[0].file,
            Path::new("index.md"),
            "diagnostic anchors on the source file"
        );
        assert_eq!(
            warnings[0].line, 3,
            "diagnostic anchors on the forward-link line, not line 1"
        );
        assert!(
            warnings[0].span.is_some(),
            "diagnostic carries the forward link's span"
        );
    }

    #[test]
    fn stale_backlink_warning() {
        let target = "\
---
backlinks:
  superseded_by:
    - ghost.md
---
# Target
";
        let (_dir, ws) = setup_workspace(&[("target.md", target)]);

        let diags = validate_backlinks(&ws);
        let warnings: Vec<_> = diags
            .iter()
            .filter(|d| d.severity == Severity::Warning)
            .collect();

        assert_eq!(warnings.len(), 1, "one warning for stale backlink");
        assert!(
            warnings[0]
                .message
                .contains("no corresponding forward link"),
            "message describes staleness: {}",
            warnings[0].message
        );
        assert!(
            warnings[0].message.contains("ghost.md"),
            "message names the stale source: {}",
            warnings[0].message
        );
    }

    #[test]
    fn default_predicate_generates_referenced_by_backlink() {
        let (_dir, ws) = setup_workspace(&[
            ("index.md", "[target](target.md)"),
            ("target.md", "# Target\n"),
        ]);

        let diags = validate_backlinks(&ws);
        let warnings: Vec<_> = diags
            .iter()
            .filter(|d| d.severity == Severity::Warning)
            .collect();

        assert_eq!(warnings.len(), 1, "one warning for missing backlink");
        assert!(
            warnings[0].message.contains("referenced_by"),
            "implicit references produces referenced_by backlink: {}",
            warnings[0].message
        );
    }

    // --- Symmetric predicates (decision 008) ---

    #[test]
    fn inverse_predicate_forward_link_accepted() {
        // A forward link may use the inverse member of a pair — no
        // unknown-predicate error.
        let (_dir, ws) =
            setup_workspace(&[("a.md", r#"[b](b.md "superseded_by")"#), ("b.md", "# B\n")]);

        let diags = validate_forward_links(&ws);
        assert!(
            diags.iter().all(|d| d.severity != Severity::Error),
            "inverse-predicate forward link should not error: {diags:?}"
        );
    }

    #[test]
    fn inverse_predicate_derives_forward_label() {
        // `a` is superseded_by `b`, so `b` supersedes `a`: the derived
        // backlink on `b` is keyed by the *forward* label `supersedes`.
        let (_dir, ws) =
            setup_workspace(&[("a.md", r#"[b](b.md "superseded_by")"#), ("b.md", "# B\n")]);

        let diags = validate_backlinks(&ws);
        let warnings: Vec<_> = diags
            .iter()
            .filter(|d| d.severity == Severity::Warning)
            .collect();

        assert_eq!(warnings.len(), 1, "one missing-backlink warning: {diags:?}");
        assert!(
            warnings[0].message.contains("supersedes"),
            "derived label is the forward `supersedes`, not the inverse: {}",
            warnings[0].message
        );
    }

    #[test]
    fn reciprocal_forward_links_need_no_frontmatter() {
        // A→B `superseded_by` and B→A `supersedes` express the same edge from
        // both ends. The floor is met without any frontmatter backlink.
        let (_dir, ws) = setup_workspace(&[
            ("a.md", r#"[b](b.md "superseded_by")"#),
            ("b.md", r#"[a](a.md "supersedes")"#),
        ]);

        assert!(
            validate_backlinks(&ws).is_empty(),
            "reciprocal forward links require no backlinks: {:?}",
            validate_backlinks(&ws)
        );
        assert!(
            validate_forward_links(&ws)
                .iter()
                .all(|d| d.severity != Severity::Error),
            "reciprocal forward links produce no errors"
        );
    }

    #[test]
    fn removing_reciprocal_reverts_to_missing_warning() {
        // Drop B's reciprocal link: A→B `superseded_by` again needs a
        // `supersedes` backlink on B (now neither frontmatter nor reciprocal).
        let (_dir, ws) =
            setup_workspace(&[("a.md", r#"[b](b.md "superseded_by")"#), ("b.md", "# B\n")]);

        let warnings: Vec<_> = validate_backlinks(&ws)
            .into_iter()
            .filter(|d| d.severity == Severity::Warning)
            .collect();

        assert_eq!(warnings.len(), 1, "the obligation reverts to a warning");
        assert!(
            warnings[0].message.contains("supersedes"),
            "warning names the unmet `supersedes` backlink: {}",
            warnings[0].message
        );
    }

    #[test]
    fn forward_label_backlink_key_validates_as_known() {
        // A frontmatter backlink keyed by a forward predicate is accepted —
        // it materializes the edge that B's `superseded_by` link authored.
        let (_dir, ws) = setup_workspace(&[
            (
                "b.md",
                "---\nbacklinks:\n  supersedes:\n    - a.md\n---\n# B\n",
            ),
            ("a.md", r#"[b](b.md "superseded_by")"#),
        ]);

        let errors: Vec<_> = collect_all(&ws)
            .into_iter()
            .filter(|d| d.severity == Severity::Error)
            .collect();

        assert!(
            errors.is_empty(),
            "forward-label backlink key is known, not an error: {errors:?}"
        );
    }

    #[test]
    fn reciprocal_link_plus_frontmatter_is_not_warned() {
        // Over-specification — both reciprocal forward links *and* both
        // frontmatter backlinks — is redundant but consistent: no warning.
        let (_dir, ws) = setup_workspace(&[
            (
                "a.md",
                "---\nbacklinks:\n  superseded_by:\n    - b.md\n---\n[b](b.md \"superseded_by\")\n",
            ),
            (
                "b.md",
                "---\nbacklinks:\n  supersedes:\n    - a.md\n---\n[a](a.md \"supersedes\")\n",
            ),
        ]);

        assert!(
            validate_backlinks(&ws).is_empty(),
            "redundant-but-consistent edge is not warned: {:?}",
            validate_backlinks(&ws)
        );
    }

    #[test]
    fn backlinks_disabled_skips_checking() {
        let config_toml = "\
[policy]
backlinks = false
";
        let (_dir, ws) = setup_workspace(&[
            (".lattice.toml", config_toml),
            ("index.md", r#"[target](target.md "supersedes")"#),
            ("target.md", "# Target\n"),
        ]);

        let diags = validate_backlinks(&ws);
        assert!(
            diags.is_empty(),
            "no diagnostics when backlinks disabled: {diags:?}"
        );
    }

    #[test]
    fn multiple_backlinks_from_different_files() {
        let target = "\
---
backlinks:
  superseded_by:
    - a.md
---
# Target
";
        let (_dir, ws) = setup_workspace(&[
            ("a.md", r#"[target](target.md "supersedes")"#),
            ("b.md", r#"[target](target.md "supersedes")"#),
            ("target.md", target),
        ]);

        let diags = validate_backlinks(&ws);
        let warnings: Vec<_> = diags
            .iter()
            .filter(|d| d.severity == Severity::Warning)
            .collect();

        assert_eq!(
            warnings.len(),
            1,
            "one missing backlink (a.md present, b.md missing): {warnings:?}"
        );
        assert_eq!(
            warnings[0].file,
            Path::new("b.md"),
            "warning anchors on the source with the missing backlink (b.md): {warnings:?}"
        );
        assert!(
            warnings[0].message.contains("target.md"),
            "message names the target the backlink belongs in: {}",
            warnings[0].message
        );
    }

    #[test]
    fn cross_directory_backlink_to_root() {
        let target = "\
---
backlinks:
  referenced_by:
    - docs/guide.md
---
# README
";
        let (_dir, ws) = setup_workspace(&[
            ("docs/guide.md", r#"[readme](../README.md "references")"#),
            ("README.md", target),
        ]);

        let diags = validate_backlinks(&ws);
        assert!(
            diags.is_empty(),
            "no warnings for correct cross-directory backlink: {diags:?}"
        );
    }

    #[test]
    fn cross_directory_backlink_to_subdir() {
        let target = "\
---
backlinks:
  referenced_by:
    - ../index.md
---
# API
";
        let (_dir, ws) = setup_workspace(&[
            ("index.md", r#"[api](docs/api.md "references")"#),
            ("docs/api.md", target),
        ]);

        let diags = validate_backlinks(&ws);
        assert!(
            diags.is_empty(),
            "no warnings when backlink uses file-relative path: {diags:?}"
        );
    }

    #[test]
    fn same_directory_backlink() {
        let target = "\
---
backlinks:
  superseded_by:
    - guide.md
---
# API
";
        let (_dir, ws) = setup_workspace(&[
            ("docs/guide.md", r#"[api](api.md "supersedes")"#),
            ("docs/api.md", target),
        ]);

        let diags = validate_backlinks(&ws);
        assert!(
            diags.is_empty(),
            "no warnings when same-directory backlink uses bare filename: {diags:?}"
        );
    }

    #[test]
    fn missing_backlink_message_shows_file_relative_path() {
        // Source in a subdirectory links up to a root target. The message
        // (now on the source) names the target relative to the source, so the
        // file-relative `..` rendering must survive.
        let (_dir, ws) = setup_workspace(&[
            ("docs/guide.md", r#"[readme](../README.md "supersedes")"#),
            ("README.md", "# README\n"),
        ]);

        let diags = validate_backlinks(&ws);
        let warnings: Vec<_> = diags
            .iter()
            .filter(|d| d.severity == Severity::Warning)
            .collect();

        assert_eq!(warnings.len(), 1, "one warning for missing backlink");
        assert_eq!(
            warnings[0].file,
            Path::new("docs/guide.md"),
            "diagnostic anchors on the source"
        );
        assert!(
            warnings[0].message.contains("../README.md"),
            "message shows file-relative path to the target, not workspace-relative: {}",
            warnings[0].message
        );
    }

    #[test]
    fn broken_forward_link_does_not_expect_backlink() {
        let (_dir, ws) =
            setup_workspace(&[("index.md", r#"[missing](nonexistent.md "supersedes")"#)]);

        let diags = validate_backlinks(&ws);
        assert!(
            diags.is_empty(),
            "no backlink warnings for broken forward links: {diags:?}"
        );
    }

    #[test]
    fn unknown_predicate_does_not_expect_backlink() {
        let (_dir, ws) = setup_workspace(&[
            ("index.md", r#"[target](target.md "invented")"#),
            ("target.md", "# Target\n"),
        ]);

        let diags = validate_backlinks(&ws);
        assert!(
            diags.is_empty(),
            "no backlink warnings for unknown predicates: {diags:?}"
        );
    }

    // --- Fragment validation ---

    #[test]
    fn fragment_matches_explicit_anchor() {
        let (_dir, ws) = setup_workspace(&[
            ("index.md", r#"[context](target.md#my-anchor "references")"#),
            ("target.md", "## Context {#my-anchor}\n"),
        ]);

        let diags = validate_forward_links(&ws);
        assert!(
            diags.is_empty(),
            "no errors when fragment matches explicit anchor: {diags:?}"
        );
    }

    #[test]
    fn fragment_matches_computed_slug() {
        let (_dir, ws) = setup_workspace(&[
            (
                "index.md",
                r#"[gs](target.md#getting-started "references")"#,
            ),
            ("target.md", "## Getting Started\n"),
        ]);

        let diags = validate_forward_links(&ws);
        assert!(
            diags.is_empty(),
            "no errors when fragment matches computed slug: {diags:?}"
        );
    }

    #[test]
    fn fragment_not_found_produces_error() {
        let (_dir, ws) = setup_workspace(&[
            ("index.md", r#"[ref](target.md#nonexistent "references")"#),
            ("target.md", "## Introduction\n"),
        ]);

        let diags = validate_forward_links(&ws);
        let errors: Vec<_> = diags
            .iter()
            .filter(|d| d.severity == Severity::Error)
            .collect();

        assert_eq!(errors.len(), 1, "one error for unresolved fragment");
        assert!(
            errors[0].message.contains("#nonexistent"),
            "message includes the fragment: {}",
            errors[0].message
        );
        assert!(
            errors[0].message.contains("target.md"),
            "message includes the target file: {}",
            errors[0].message
        );
    }

    #[test]
    fn fragment_pinned_to_github_rejects_gitlab_only_slug() {
        // "Héllo" → github slug "héllo", gitlab slug "hllo"
        let config_toml = "\
[policy]
fragments = \"github\"
";
        let (_dir, ws) = setup_workspace(&[
            (".lattice.toml", config_toml),
            ("index.md", r#"[ref](target.md#hllo "references")"#),
            ("target.md", "## Héllo\n"),
        ]);

        let diags = validate_forward_links(&ws);
        let errors: Vec<_> = diags
            .iter()
            .filter(|d| d.severity == Severity::Error)
            .collect();

        assert_eq!(
            errors.len(),
            1,
            "gitlab-only slug rejected when pinned to github"
        );
        assert!(
            errors[0].message.contains("#hllo"),
            "message includes the fragment: {}",
            errors[0].message
        );
    }

    #[test]
    fn fragment_pinned_to_github_accepts_github_slug() {
        // "Héllo" → github slug "héllo"
        let config_toml = "\
[policy]
fragments = \"github\"
";
        let (_dir, ws) = setup_workspace(&[
            (".lattice.toml", config_toml),
            ("index.md", r#"[ref](target.md#héllo "references")"#),
            ("target.md", "## Héllo\n"),
        ]);

        let diags = validate_forward_links(&ws);
        assert!(
            diags.is_empty(),
            "github slug accepted when pinned to github: {diags:?}"
        );
    }

    #[test]
    fn fragment_pinned_to_gitlab_accepts_gitlab_slug() {
        // "Héllo" → gitlab slug "hllo"
        let config_toml = "\
[policy]
fragments = \"gitlab\"
";
        let (_dir, ws) = setup_workspace(&[
            (".lattice.toml", config_toml),
            ("index.md", r#"[ref](target.md#hllo "references")"#),
            ("target.md", "## Héllo\n"),
        ]);

        let diags = validate_forward_links(&ws);
        assert!(
            diags.is_empty(),
            "gitlab slug accepted when pinned to gitlab: {diags:?}"
        );
    }

    #[test]
    fn fragment_unpinned_accepts_any_algorithm() {
        // "Héllo" → github "héllo", gitlab "hllo", vscode "héllo"
        // With no pinned algorithm, "hllo" (gitlab-only) should still pass.
        let (_dir, ws) = setup_workspace(&[
            ("index.md", r#"[ref](target.md#hllo "references")"#),
            ("target.md", "## Héllo\n"),
        ]);

        let diags = validate_forward_links(&ws);
        assert!(
            diags.is_empty(),
            "gitlab-only slug accepted when no algorithm pinned: {diags:?}"
        );
    }

    #[test]
    fn fragment_on_broken_target_skipped() {
        let (_dir, ws) =
            setup_workspace(&[("index.md", r#"[ref](missing.md#heading "references")"#)]);

        let diags = validate_forward_links(&ws);
        let fragment_errors: Vec<_> = diags
            .iter()
            .filter(|d| d.severity == Severity::Error && d.message.contains("fragment"))
            .collect();

        assert!(
            fragment_errors.is_empty(),
            "no fragment errors for broken targets: {fragment_errors:?}"
        );
    }

    #[test]
    fn fragment_on_file_with_no_headings() {
        let (_dir, ws) = setup_workspace(&[
            ("index.md", r#"[ref](target.md#something "references")"#),
            ("target.md", "No headings here.\n"),
        ]);

        let diags = validate_forward_links(&ws);
        let errors: Vec<_> = diags
            .iter()
            .filter(|d| d.severity == Severity::Error)
            .collect();

        assert_eq!(errors.len(), 1, "error when target has no headings at all");
        assert!(
            errors[0].message.contains("#something"),
            "message includes the fragment: {}",
            errors[0].message
        );
    }

    #[test]
    fn same_document_anchor_resolves_to_own_heading() {
        // `[…](#slug)` resolves against the source file's own headings.
        let (_dir, ws) = setup_workspace(&[(
            "index.md",
            "[top](#getting-started)\n\n## Getting Started\n",
        )]);

        let diags = validate_forward_links(&ws);
        assert!(
            diags.is_empty(),
            "no errors when same-doc anchor matches an own heading: {diags:?}"
        );
    }

    #[test]
    fn same_document_anchor_resolves_to_explicit_id() {
        let (_dir, ws) = setup_workspace(&[(
            "index.md",
            "[go](#custom-id)\n\n## Some Heading {#custom-id}\n",
        )]);

        let diags = validate_forward_links(&ws);
        assert!(
            diags.is_empty(),
            "no errors when same-doc anchor matches an explicit {{#id}}: {diags:?}"
        );
    }

    #[test]
    fn same_document_anchor_not_found_produces_error() {
        // Issue 021: a dangling in-page anchor must error, like a cross-file one.
        let (_dir, ws) =
            setup_workspace(&[("index.md", "[broken](#nonexistent)\n\n## Introduction\n")]);

        let diags = validate_forward_links(&ws);
        let errors: Vec<_> = diags
            .iter()
            .filter(|d| d.severity == Severity::Error)
            .collect();

        assert_eq!(
            errors.len(),
            1,
            "one error for an unresolved same-doc anchor: {errors:?}"
        );
        assert!(
            errors[0].message.contains("#nonexistent"),
            "message includes the fragment: {}",
            errors[0].message
        );
    }

    #[test]
    fn same_document_anchor_resolves_to_explicit_html_id() {
        // Issue 025: `[x](#a)` resolves to a preceding `<a id="a"></a>` target,
        // not just a heading slug.
        let (_dir, ws) = setup_workspace(&[(
            "index.md",
            "[go](#explicit-anchor)\n\n<a id=\"explicit-anchor\"></a>\n\n## Real Heading\n",
        )]);

        let diags = validate_forward_links(&ws);
        assert!(
            diags.is_empty(),
            "no errors when same-doc anchor matches an explicit `<a id>`: {diags:?}"
        );
    }

    #[test]
    fn same_document_anchor_resolves_to_explicit_html_name() {
        // Issue 025: `<a name="a">` is equally a valid `#a` target.
        let (_dir, ws) = setup_workspace(&[(
            "index.md",
            "[go](#name-anchor)\n\n<a name=\"name-anchor\"></a>\n\n## Real Heading\n",
        )]);

        let diags = validate_forward_links(&ws);
        assert!(
            diags.is_empty(),
            "no errors when same-doc anchor matches an explicit `<a name>`: {diags:?}"
        );
    }

    #[test]
    fn same_document_anchor_resolves_to_element_id() {
        // Issue 025 (broadened to GitHub parity): `[x](#d)` resolves against any
        // element bearing `id="d"` — `<div id>` and `<section id>`, not only
        // `<a id>`.
        let (_dir, ws) = setup_workspace(&[(
            "index.md",
            "[a](#div-anchor)\n\
             [b](#section-anchor)\n\n\
             <div id=\"div-anchor\">\n\nbody\n\n</div>\n\n\
             <section id=\"section-anchor\">\n\nmore\n\n</section>\n\n\
             ## Real Heading\n",
        )]);

        let diags = validate_forward_links(&ws);
        assert!(
            diags.is_empty(),
            "no errors when same-doc anchor matches an element `id`: {diags:?}"
        );
    }

    #[test]
    fn same_document_html_anchor_does_not_mask_missing_fragment() {
        // Issue 025 repro: explicit `<a id>`/`<a name>` and a heading slug all
        // resolve, while a genuinely missing `#does-not-exist` still errors —
        // harvesting anchors must not over-suppress the control.
        let (_dir, ws) = setup_workspace(&[(
            "index.md",
            "- [a](#explicit-anchor)\n\
             - [b](#real-heading)\n\
             - [c](#name-anchor)\n\
             - [d](#does-not-exist)\n\n\
             <a id=\"explicit-anchor\"></a>\n\n\
             ## Real Heading\n\n\
             <a name=\"name-anchor\"></a>\n\n\
             Body text.\n",
        )]);

        let diags = validate_forward_links(&ws);
        let errors: Vec<_> = diags
            .iter()
            .filter(|d| d.severity == Severity::Error)
            .collect();
        assert_eq!(
            errors.len(),
            1,
            "only the genuinely missing fragment errors: {errors:?}"
        );
        assert!(
            errors[0].message.contains("#does-not-exist"),
            "the one error names the missing fragment: {}",
            errors[0].message
        );
    }

    #[test]
    fn same_document_anchor_resolves_to_mid_paragraph_inline_id() {
        // Issue 026: `[x](#s)` resolves against a non-`<a>` `id` that appears
        // mid-paragraph as inline raw HTML — the gap issue 025 left open. The
        // block-level `<div id>` resolves as before, the mid-paragraph
        // `<span id>` now resolves too, and a genuinely missing `#z` still
        // errors with exactly one diagnostic.
        let (_dir, ws) = setup_workspace(&[(
            "index.md",
            "- [a](#block-anchor)\n\
             - [b](#inline-anchor)\n\
             - [c](#does-not-exist)\n\n\
             <div id=\"block-anchor\"></div>\n\n\
             Paragraph with an <span id=\"inline-anchor\"></span> inline target.\n",
        )]);

        let diags = validate_forward_links(&ws);
        let errors: Vec<_> = diags
            .iter()
            .filter(|d| d.severity == Severity::Error)
            .collect();
        assert_eq!(
            errors.len(),
            1,
            "only the genuinely missing fragment errors; block and mid-paragraph \
             inline ids both resolve: {errors:?}"
        );
        assert!(
            errors[0].message.contains("#does-not-exist"),
            "the one error names the missing fragment: {}",
            errors[0].message
        );
    }

    #[test]
    fn same_document_top_idioms_are_valid() {
        // `#` and `#top` (any case) are back-to-top idioms — never flagged,
        // even with no matching heading (issue 021).
        let (_dir, ws) = setup_workspace(&[(
            "index.md",
            "[a](#) [b](#top) [c](#Top) [d](#TOP)\n\n## Intro\n",
        )]);

        let diags = validate_forward_links(&ws);
        assert!(
            diags.is_empty(),
            "back-to-top idioms must not be flagged: {diags:?}"
        );
    }

    #[test]
    fn cross_file_top_idioms_are_valid() {
        // The exemption is symmetric: a cross-file `#`/`#top` is valid too.
        let (_dir, ws) = setup_workspace(&[
            ("index.md", "[a](other.md#) [b](other.md#top)\n"),
            ("other.md", "## Other Heading\n"),
        ]);

        let diags = validate_forward_links(&ws);
        let errors: Vec<_> = diags
            .iter()
            .filter(|d| d.severity == Severity::Error)
            .collect();
        assert!(
            errors.is_empty(),
            "cross-file back-to-top idioms must not error: {errors:?}"
        );
    }

    #[test]
    fn same_document_real_top_heading_still_resolves() {
        // `#top` pointing at an actual `## Top` heading is fine either way.
        let (_dir, ws) = setup_workspace(&[("index.md", "[a](#top)\n\n## Top\n")]);

        let diags = validate_forward_links(&ws);
        assert!(
            diags.is_empty(),
            "a real `top` heading resolves cleanly: {diags:?}"
        );
    }

    #[test]
    fn explicit_anchor_takes_priority_over_slug() {
        // Heading has explicit anchor that differs from the computed slug.
        let (_dir, ws) = setup_workspace(&[
            ("index.md", r#"[ref](target.md#custom-id "references")"#),
            ("target.md", "## Getting Started {#custom-id}\n"),
        ]);

        let diags = validate_forward_links(&ws);
        assert!(diags.is_empty(), "explicit anchor matched: {diags:?}");
    }

    // --- Bare path validation ---

    // --- collect_all opt-in gate ---

    #[test]
    fn collect_all_empty_without_config() {
        let (_dir, ws) = setup_workspace(&[("index.md", r#"[missing](gone.md "references")"#)]);

        let diags = collect_all(&ws);
        assert!(
            diags.is_empty(),
            "collect_all should return empty without .lattice.toml: {diags:?}"
        );
    }

    #[test]
    fn collect_all_runs_with_config() {
        let (_dir, ws) = setup_workspace(&[
            (".lattice.toml", ""),
            ("index.md", r#"[missing](gone.md "references")"#),
        ]);

        let diags = collect_all(&ws);
        assert!(
            !diags.is_empty(),
            "collect_all should produce diagnostics with .lattice.toml"
        );
    }

    // --- Connectivity validation (issue 018) ---

    /// Collect the workspace-relative paths flagged by connectivity.
    fn flagged_files(ws: &Workspace) -> Vec<String> {
        let mut files: Vec<String> = validate_connectivity(ws)
            .into_iter()
            .map(|d| d.file.display().to_string())
            .collect();
        files.sort();
        files
    }

    #[test]
    fn connectivity_off_by_default() {
        // .lattice.toml present but no connectivity setting → no topology checks.
        let (_dir, ws) = setup_workspace(&[
            (".lattice.toml", "[policy]\nbacklinks = true\n"),
            ("orphan.md", "# Orphan\n"),
        ]);

        assert!(
            validate_connectivity(&ws).is_empty(),
            "connectivity defaults off: {:?}",
            validate_connectivity(&ws)
        );
    }

    #[test]
    fn no_orphans_flags_degree_zero_document() {
        let (_dir, ws) = setup_workspace(&[
            (".lattice.toml", "[policy]\nconnectivity = \"no-orphans\"\n"),
            ("index.md", r#"[a](a.md "references")"#),
            ("a.md", "# A\n"),
            ("orphan.md", "# Orphan\n"),
        ]);

        let diags = validate_connectivity(&ws);
        assert_eq!(diags.len(), 1, "only the orphan is flagged: {diags:?}");
        assert_eq!(
            diags[0].file,
            PathBuf::from("orphan.md"),
            "orphan.md flagged"
        );
        assert_eq!(diags[0].line, 1, "anchored on line 1");
        assert_eq!(
            diags[0].severity,
            Severity::Warning,
            "connectivity is a warning"
        );
        assert!(
            diags[0].message.contains("orphaned document"),
            "message names the orphan: {}",
            diags[0].message
        );
    }

    #[test]
    fn no_orphans_exempts_default_root_readme() {
        // A lone README (the default root) links nowhere but is never
        // self-flagged — a single-document workspace stays clean.
        let (_dir, ws) = setup_workspace(&[
            (".lattice.toml", "[policy]\nconnectivity = \"no-orphans\"\n"),
            ("README.md", "# Readme\n"),
        ]);

        assert!(
            validate_connectivity(&ws).is_empty(),
            "the root README is exempt: {:?}",
            validate_connectivity(&ws)
        );
    }

    #[test]
    fn no_orphans_ignores_self_loop() {
        // A document linking only to itself has no edge to any *other* document.
        let (_dir, ws) = setup_workspace(&[
            (".lattice.toml", "[policy]\nconnectivity = \"no-orphans\"\n"),
            ("selfish.md", r#"[me](selfish.md "references")"#),
        ]);

        assert_eq!(
            flagged_files(&ws),
            vec!["selfish.md".to_string()],
            "self-loop does not connect a document"
        );
    }

    #[test]
    fn no_islands_flags_disconnected_cluster() {
        let (_dir, ws) = setup_workspace(&[
            (".lattice.toml", "[policy]\nconnectivity = \"no-islands\"\n"),
            ("README.md", r#"[a](a.md "references")"#),
            ("a.md", "# A\n"),
            ("island1.md", r#"[two](island2.md "references")"#),
            ("island2.md", "# Island 2\n"),
        ]);

        assert_eq!(
            flagged_files(&ws),
            vec!["island1.md".to_string(), "island2.md".to_string()],
            "both island members flagged, root component clean"
        );
        let diags = validate_connectivity(&ws);
        assert!(
            diags[0].message.contains("isolated document"),
            "no-islands message: {}",
            diags[0].message
        );
    }

    #[test]
    fn reachable_flags_inbound_only_deadend() {
        // `lonely` links into the root component but nothing forward-navigates
        // to it from any root — connected, but unreachable.
        let (_dir, ws) = setup_workspace(&[
            (".lattice.toml", "[policy]\nconnectivity = \"reachable\"\n"),
            ("README.md", r#"[a](a.md "references")"#),
            ("a.md", "# A\n"),
            ("lonely.md", r#"[home](README.md "references")"#),
        ]);

        assert_eq!(
            flagged_files(&ws),
            vec!["lonely.md".to_string()],
            "inbound-only dead-end is unreachable"
        );
        let diags = validate_connectivity(&ws);
        assert!(
            diags[0].message.contains("unreachable document"),
            "reachable message: {}",
            diags[0].message
        );
    }

    #[test]
    fn no_islands_passes_inbound_only_deadend() {
        // The same graph as `reachable_flags_inbound_only_deadend`: `lonely`
        // shares an (undirected) edge with the root, so no-islands accepts it.
        // Demonstrates the strict superset `no-islands ⊆ reachable`.
        let (_dir, ws) = setup_workspace(&[
            (".lattice.toml", "[policy]\nconnectivity = \"no-islands\"\n"),
            ("README.md", r#"[a](a.md "references")"#),
            ("a.md", "# A\n"),
            ("lonely.md", r#"[home](README.md "references")"#),
        ]);

        assert!(
            validate_connectivity(&ws).is_empty(),
            "no-islands accepts the inbound-only dead-end: {:?}",
            validate_connectivity(&ws)
        );
    }

    #[test]
    fn reachable_uses_custom_roots() {
        let (_dir, ws) = setup_workspace(&[
            (
                ".lattice.toml",
                "[policy]\nconnectivity = \"reachable\"\nroots = [\"docs/home.md\"]\n",
            ),
            ("docs/home.md", r#"[a](../a.md "references")"#),
            ("a.md", "# A\n"),
            ("stray.md", "# Stray\n"),
        ]);

        assert_eq!(
            flagged_files(&ws),
            vec!["stray.md".to_string()],
            "traversal starts from the configured root, reaches a.md"
        );
    }

    #[test]
    fn reachable_without_resolvable_root_emits_nothing() {
        // Default root README.md is absent, so `reachable` has no anchor and
        // stays silent rather than flagging every document.
        let (_dir, ws) = setup_workspace(&[
            (".lattice.toml", "[policy]\nconnectivity = \"reachable\"\n"),
            ("a.md", "# A\n"),
            ("b.md", "# B\n"),
        ]);

        assert!(
            validate_connectivity(&ws).is_empty(),
            "no root → no reachable diagnostics: {:?}",
            validate_connectivity(&ws)
        );
    }
}