mif-rh 0.7.0

Compiled ontology resolution/review engine for research-harness-template corpora
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
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
//! On-demand ontology vendoring (rht ADR-0012).
//!
//! Fetches domain ontology packs from the canonical registry, sha256-verifies
//! and pins them, catalogs them, discovers new registry entries, and checks
//! for local drift.
//!
//! Ports rht's `scripts/fetch-ontology.sh`, the ontology-catalog section of
//! `scripts/sync-packs.sh`, `scripts/check-ontology-lock.sh`, and
//! `scripts/sync-registry-ontologies.sh` to the compiled engine (Epic
//! research-harness-template#276, Story #277).

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

use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

use crate::error::MifRhError;
use crate::ontology_pack::parse_pack;

/// The canonical ontology registry's default base URL.
pub const DEFAULT_REGISTRY_SOURCE: &str = "https://mif-spec.dev/ontologies";

/// Ontology ids that are always committed base layers, never vendored, and
/// implicitly core when cataloged (mirrors `sync-packs.sh`'s `CORE_IDS`).
const CORE_IDS: [&str; 3] = ["mif-base", "mif-generic", "shared-traits"];

/// One registry index entry: a domain ontology's pinned version, sha256,
/// vendored file name, and `extends` ancestry.
#[derive(Debug, Clone, Deserialize)]
pub struct IndexEntry {
    /// The ontology's canonical version.
    pub version: String,
    /// The expected sha256 of the vendored file.
    pub sha256: String,
    /// The bare filename to fetch (must not contain a path separator).
    pub file: String,
    /// Ontology ids this ontology directly extends.
    #[serde(default)]
    pub extends: Vec<String>,
}

/// The canonical registry's `index.json`: every known domain ontology,
/// keyed by id.
#[derive(Debug, Clone, Deserialize)]
pub struct RegistryIndex {
    /// The indexed ontologies.
    pub ontologies: BTreeMap<String, IndexEntry>,
}

/// One pinned lock entry: a vendored ontology's version and verified
/// sha256.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LockEntry {
    /// The vendored version.
    pub version: String,
    /// The verified sha256 of the vendored file.
    pub sha256: String,
}

/// `ontologies.lock.json`: the registry source, its trust-pinned index
/// sha256 (trust-on-first-use), and every vendored ontology's pin.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LockFile {
    /// The lock file's schema tag.
    #[serde(default = "lock_schema")]
    pub schema: String,
    /// The registry source this lock was populated from.
    #[serde(default)]
    pub source: String,
    /// The pinned registry index sha256, once one has been fetched.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub index_sha256: Option<String>,
    /// Every vendored ontology's pin, keyed by id.
    #[serde(default)]
    pub ontologies: BTreeMap<String, LockEntry>,
}

fn lock_schema() -> String {
    "mif-ontology-lock/v1".to_string()
}

impl Default for LockFile {
    fn default() -> Self {
        Self {
            schema: lock_schema(),
            source: String::new(),
            index_sha256: None,
            ontologies: BTreeMap::new(),
        }
    }
}

impl LockFile {
    /// Loads `ontologies.lock.json` at `path`, or an empty default lock if
    /// the file does not exist yet (on-demand vendoring not yet adopted).
    ///
    /// # Errors
    ///
    /// Returns [`MifRhError::Io`] if `path` exists but cannot be read, or
    /// [`MifRhError::Json`] if it is not valid JSON.
    pub fn load_or_default(path: &Path) -> Result<Self, MifRhError> {
        if !path.exists() {
            return Ok(Self::default());
        }
        let contents = std::fs::read_to_string(path).map_err(|source| MifRhError::Io {
            path: path.display().to_string(),
            source,
        })?;
        serde_json::from_str(&contents).map_err(|source| MifRhError::Json {
            path: path.display().to_string(),
            source,
        })
    }
}

/// Resolves the ontology registry source.
///
/// Mirrors rht's own precedence: an explicit override (e.g. the
/// `MIF_ONTOLOGY_SOURCE` env var, read by the CLI layer), then a
/// `.ontologies.source` marker file in `root`, then the canonical default.
#[must_use]
pub fn resolve_source(root: &Path, source_override: Option<&str>) -> String {
    if let Some(src) = source_override
        && !src.is_empty()
    {
        return trim_trailing_slash(src);
    }
    if let Ok(contents) = std::fs::read_to_string(root.join(".ontologies.source"))
        && let Some(first_line) = contents.lines().next()
        && !first_line.is_empty()
    {
        return trim_trailing_slash(first_line);
    }
    DEFAULT_REGISTRY_SOURCE.to_string()
}

fn trim_trailing_slash(source: &str) -> String {
    source.strip_suffix('/').unwrap_or(source).to_string()
}

fn is_http(source: &str) -> bool {
    source.starts_with("http://") || source.starts_with("https://")
}

/// Fetches one relative path from `source` (a local directory, `file://`
/// URL, or an http(s) base URL), returning its raw bytes.
fn fetch_raw(source: &str, relpath: &str) -> Result<Vec<u8>, MifRhError> {
    if is_http(source) {
        let url = format!("{source}/{relpath}");
        let mut response = ureq::get(&url)
            .call()
            .map_err(|err| MifRhError::RegistryFetch {
                registry_source: source.to_string(),
                detail: err.to_string(),
            })?;
        response
            .body_mut()
            .read_to_vec()
            .map_err(|err| MifRhError::RegistryFetch {
                registry_source: source.to_string(),
                detail: err.to_string(),
            })
    } else {
        let base = source.strip_prefix("file://").unwrap_or(source);
        let path = Path::new(base).join(relpath);
        std::fs::read(&path).map_err(|err| MifRhError::RegistryFetch {
            registry_source: source.to_string(),
            detail: format!("cannot read {}: {err}", path.display()),
        })
    }
}

/// Hex-encodes `bytes`' sha256 digest, lowercase, matching `sha256sum`'s
/// output format.
#[must_use]
fn sha256_hex(bytes: &[u8]) -> String {
    use std::fmt::Write as _;

    let mut hasher = Sha256::new();
    hasher.update(bytes);
    hasher
        .finalize()
        .iter()
        .fold(String::new(), |mut hex, byte| {
            let _ = write!(hex, "{byte:02x}");
            hex
        })
}

/// Whether `id` is already satisfied by a committed base layer under
/// `root/schemas/ontologies/<id>/` — never fetched, regardless of the
/// registry.
fn is_committed_base(root: &Path, id: &str) -> bool {
    root.join("schemas/ontologies").join(id).is_dir()
}

/// A bare, lowercase slug (matches `harness.config.schema.json`'s own
/// ontology id pattern) — rejects anything that could escape
/// `packs/ontologies/<id>/` once vendored.
fn is_wellformed_id(id: &str) -> bool {
    let mut chars = id.chars();
    let Some(first) = chars.next() else {
        return false;
    };
    first.is_ascii_lowercase()
        && chars.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-')
}

/// Whether `file` (an index entry's attacker-controlled `file` field) is
/// safe to join onto a filesystem path: exactly one normal path component
/// (a bare filename). Checking only for `/` and `..` would still let a
/// Windows-style separator (`..\..\etc`) or an absolute path (`C:\...`)
/// through unchecked.
fn is_bare_filename(file: &str) -> bool {
    // `Path::components()` alone is insufficient: a trailing slash (e.g.
    // "foo/") still yields a single `Normal` component with nothing after
    // it, so it would otherwise pass as if it were the bare filename "foo".
    // Rejecting any '/' directly closes that gap without relying on
    // component parsing to catch it.
    let mut components = std::path::Path::new(file).components();
    matches!(components.next(), Some(std::path::Component::Normal(_)))
        && components.next().is_none()
        && !file.contains('\\')
        && !file.contains('/')
}

/// Resolves `requested`'s `extends` closure against `index` (breadth-first,
/// skipping committed base layers), returning the domain layers that must
/// actually be fetched, in discovery order.
///
/// # Errors
///
/// Returns [`MifRhError::OntologyNotInRegistry`] if a requested or ancestor
/// id has no index entry.
fn resolve_fetch_set(
    root: &Path,
    index: &RegistryIndex,
    requested: &[String],
) -> Result<Vec<String>, MifRhError> {
    let mut seen: HashSet<String> = HashSet::new();
    let mut queue: Vec<String> = Vec::new();
    for id in requested {
        if seen.insert(id.clone()) {
            queue.push(id.clone());
        }
    }
    let mut fetch_list: Vec<String> = Vec::new();
    let mut cursor = 0;
    while cursor < queue.len() {
        let id = queue[cursor].clone();
        cursor += 1;
        // Every id reaching this point — whether directly requested or
        // discovered via a registry entry's `extends` — ends up joined onto
        // a filesystem path in `fetch()` (`packs_dir.join(id)`). A
        // compromised registry could otherwise smuggle a path-traversal id
        // (e.g. `../../../etc`) through `extends`, which is fully
        // attacker-controlled index content, the same threat model as the
        // `entry.file` bare-filename check below.
        if !is_wellformed_id(&id) {
            return Err(MifRhError::MalformedOntologyId { id });
        }
        if is_committed_base(root, &id) {
            continue;
        }
        let entry = index
            .ontologies
            .get(&id)
            .ok_or_else(|| MifRhError::OntologyNotInRegistry { id: id.clone() })?;
        if !fetch_list.contains(&id) {
            fetch_list.push(id.clone());
        }
        for ancestor in &entry.extends {
            if seen.insert(ancestor.clone()) {
                queue.push(ancestor.clone());
            }
        }
    }
    Ok(fetch_list)
}

/// One ontology vendored by [`fetch`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VendoredOntology {
    /// The vendored ontology's id.
    pub id: String,
    /// The vendored version.
    pub version: String,
}

/// One ontology [`fetch`] left untouched at its pinned version.
///
/// Its `ontologies.lock.json` entry differs from the registry's current
/// version, and `refresh` was not set (research-harness-template#270: a
/// pinned corpus must not silently advance underneath an already-stamped
/// finding set).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PinnedSkipped {
    /// The ontology's id.
    pub id: String,
    /// The version currently pinned in `ontologies.lock.json`.
    pub locked_version: String,
    /// The version the registry currently offers.
    pub registry_version: String,
}

/// The outcome of a [`fetch`] call.
#[derive(Debug, Clone, Default)]
pub struct FetchReport {
    /// Every ontology newly vendored or refreshed by this call
    /// (already-vendored, unchanged ontologies are not re-listed).
    pub vendored: Vec<VendoredOntology>,
    /// Every ontology left at its pinned version because the registry has
    /// moved past it and `refresh` was not set. Never populated on a first
    /// fetch of an id (nothing pinned yet to hold).
    pub pinned_skipped: Vec<PinnedSkipped>,
}

/// Whether `id` is already pinned in `lock` at a version different from
/// `entry`'s (the registry's current one) and `refresh` was not set — the
/// rht#270 drift `fetch` must hold rather than silently advance past.
fn pinned_below_registry(
    refresh: bool,
    lock: &LockFile,
    id: &str,
    entry: &IndexEntry,
) -> Option<PinnedSkipped> {
    if refresh {
        return None;
    }
    let locked = lock.ontologies.get(id)?;
    if locked.version == entry.version {
        return None;
    }
    Some(PinnedSkipped {
        id: id.to_string(),
        locked_version: locked.version.clone(),
        registry_version: entry.version.clone(),
    })
}

/// Vendors `ids` and their `extends` closure from the registry.
///
/// Fetches from `source`'s registry index into
/// `root/packs/ontologies/<id>/`, sha256-verifying every fetched file
/// against the index (fail-closed) and pinning the result in
/// `root/ontologies.lock.json`. A committed base layer under
/// `root/schemas/ontologies/<id>/` is always satisfied locally and never
/// fetched.
///
/// Trust-on-first-use, then pin: the index's own sha256 is recorded in the
/// lock on first fetch from a given `source`. A later fetch from the SAME
/// source whose index sha256 has changed is refused — the trust root
/// moved — unless the lock's `index_sha256` is cleared to re-pin
/// deliberately.
///
/// An id already present in `ontologies.lock.json` whose pinned version
/// differs from the registry's current one is left untouched unless
/// `refresh` is set — the lock IS the version pin (rht#270): a corpus must
/// not have an ontology's schema silently advance underneath its
/// already-stamped findings just because the registry published a newer
/// version. Skipped ids are reported in
/// [`FetchReport::pinned_skipped`], never silently dropped. An id with no
/// existing lock entry (a first-time fetch) is unaffected by `refresh` and
/// always vendors at the registry's current version.
///
/// # Errors
///
/// Returns [`MifRhError::RegistryFetch`] if the index or a file cannot be
/// read, [`MifRhError::RegistryIndexInvalid`] if the index is not valid
/// JSON, [`MifRhError::OntologyNotInRegistry`] if a requested or ancestor id
/// has no index entry, [`MifRhError::IndexPinMismatch`] if the source's
/// index sha256 no longer matches the lock's pinned value,
/// [`MifRhError::UnsafeIndexPath`] if the index names an unsafe file path,
/// [`MifRhError::ChecksumMismatch`] if a fetched file's sha256 does not
/// match the index, or [`MifRhError::Io`] if a vendored file cannot be
/// written or the lock cannot be saved.
pub fn fetch(
    root: &Path,
    source: &str,
    ids: &[String],
    refresh: bool,
) -> Result<FetchReport, MifRhError> {
    let index_bytes = fetch_raw(source, "index.json")?;
    let index_sha256 = sha256_hex(&index_bytes);
    let index: RegistryIndex =
        serde_json::from_slice(&index_bytes).map_err(|err| MifRhError::RegistryIndexInvalid {
            registry_source: source.to_string(),
            detail: err.to_string(),
        })?;

    let lock_path = root.join("ontologies.lock.json");
    let mut lock = LockFile::load_or_default(&lock_path)?;
    if let Some(pinned) = &lock.index_sha256
        && lock.source == source
        && *pinned != index_sha256
    {
        return Err(MifRhError::IndexPinMismatch {
            registry_source: source.to_string(),
            pinned: pinned.clone(),
            got: index_sha256,
        });
    }

    let fetch_list = resolve_fetch_set(root, &index, ids)?;
    let mut vendored = Vec::new();
    let mut pinned_skipped = Vec::new();
    let packs_dir = root.join("packs/ontologies");
    // Set before the loop (not after) and persisted incrementally per id
    // below: if a later id in this same request fails
    // (checksum/unsafe-path/IO), the ontologies successfully vendored
    // earlier in the loop must still end up pinned in the lock — otherwise
    // they sit on disk, sha256-verified once, permanently invisible to
    // `lock_check` (which only walks `lock.ontologies`, never the packs
    // directory).
    lock.index_sha256 = Some(index_sha256);
    lock.source = source.to_string();
    for id in &fetch_list {
        // Present in `index` by construction: `resolve_fetch_set` already
        // resolved every entry in `fetch_list` through a successful lookup.
        let Some(entry) = index.ontologies.get(id) else {
            continue;
        };
        if let Some(skip) = pinned_below_registry(refresh, &lock, id, entry) {
            pinned_skipped.push(skip);
            continue;
        }
        // `entry.file` is later joined onto a filesystem path, so it must
        // be a bare filename, never a path that could escape the intended
        // directory.
        if !is_bare_filename(&entry.file) {
            return Err(MifRhError::UnsafeIndexPath {
                id: id.clone(),
                file: entry.file.clone(),
            });
        }
        let bytes = fetch_raw(source, &entry.file)?;
        let got = sha256_hex(&bytes);
        if got != entry.sha256 {
            return Err(MifRhError::ChecksumMismatch {
                id: id.clone(),
                file: entry.file.clone(),
                expected: entry.sha256.clone(),
                got,
            });
        }
        let dest_dir = packs_dir.join(id);
        let out_yaml = dest_dir.join(format!("{id}.ontology.yaml"));
        // Parse BEFORE writing to disk: a parse failure must never leave a
        // malformed/wrong-shape YAML behind under packs/ontologies/ (that
        // would violate the fail-closed "no partial vendoring on error"
        // contract and confuse a later `lock_check`/`load_packs_via_catalog`
        // scan that finds the file but doesn't know it was never vendored).
        let pack = parse_pack(
            &String::from_utf8_lossy(&bytes),
            &out_yaml.display().to_string(),
        )?;
        std::fs::create_dir_all(&dest_dir).map_err(|source| MifRhError::Io {
            path: dest_dir.display().to_string(),
            source,
        })?;
        std::fs::write(&out_yaml, &bytes).map_err(|source| MifRhError::Io {
            path: out_yaml.display().to_string(),
            source,
        })?;
        let sidecar = serde_json::json!({
            "name": id,
            "version": entry.version,
            "kind": "ontology",
            "description": pack.description.as_deref().unwrap_or(""),
            "provides": {"ontologies": [id]},
        });
        crate::write_json_atomic(&dest_dir.join("ontology.pack.json"), &sidecar)?;

        lock.ontologies.insert(
            id.clone(),
            LockEntry {
                version: entry.version.clone(),
                sha256: entry.sha256.clone(),
            },
        );
        crate::write_json_atomic(&lock_path, &lock)?;
        vendored.push(VendoredOntology {
            id: id.clone(),
            version: entry.version.clone(),
        });
    }
    if vendored.is_empty() {
        // No per-id write happened above to persist the mutated
        // index_sha256/source: either fetch_list was empty (every requested
        // id was a committed base layer), or every entry in it was pinned
        // and skipped. Either way the pin/trust-root mutations made before
        // the loop still need to land on disk.
        crate::write_json_atomic(&lock_path, &lock)?;
    }

    Ok(FetchReport {
        vendored,
        pinned_skipped,
    })
}

/// Reads `harness.config.json`'s `.ontologies[]` (JSON, since `sync_registry`
/// must round-trip every OTHER top-level field of that file untouched).
fn load_config_json(path: &Path) -> Result<serde_json::Value, MifRhError> {
    if !path.exists() {
        return Err(MifRhError::ConfigMissing {
            path: path.display().to_string(),
        });
    }
    let contents = std::fs::read_to_string(path).map_err(|source| MifRhError::Io {
        path: path.display().to_string(),
        source,
    })?;
    serde_json::from_str(&contents).map_err(|source| MifRhError::Json {
        path: path.display().to_string(),
        source,
    })
}

fn ontologies_array(config: &serde_json::Value) -> &[serde_json::Value] {
    config
        .get("ontologies")
        .and_then(serde_json::Value::as_array)
        .map_or(&[][..], Vec::as_slice)
}

fn enabled_ontology_ids(config: &serde_json::Value) -> Vec<String> {
    ontologies_array(config)
        .iter()
        .filter(|entry| {
            entry
                .get("enabled")
                .and_then(serde_json::Value::as_bool)
                .unwrap_or(false)
        })
        .filter_map(|entry| entry.get("id").and_then(serde_json::Value::as_str))
        .map(str::to_string)
        .collect()
}

fn known_ontology_ids(config: &serde_json::Value) -> HashSet<String> {
    ontologies_array(config)
        .iter()
        .filter_map(|entry| entry.get("id").and_then(serde_json::Value::as_str))
        .map(str::to_string)
        .collect()
}

/// One cataloged ontology entry written by [`sync_catalog`], matching
/// `sync-packs.sh`'s own `{id, version, source, core}` shape.
#[derive(Debug, Clone, Serialize)]
struct CatalogOntologyEntry {
    id: String,
    version: String,
    source: String,
    core: bool,
}

/// The outcome of a [`sync_catalog`] call.
#[derive(Debug, Clone, Default)]
pub struct CatalogSyncReport {
    /// How many ontologies (core + enabled) were cataloged.
    pub cataloged: usize,
}

/// Rebuilds the ontology-catalog section of rht's catalog sidecar.
///
/// Writes `.claude/enabled-packs.json`'s `.ontologies` key: every committed
/// base layer under `root/schemas/ontologies/` (core, if its id is one of
/// `mif-base`/`mif-generic`/`shared-traits`) plus every ontology enabled in
/// `harness.config.json` that is vendored under
/// `root/packs/ontologies/<id>/`.
///
/// Merges into whatever else already lives in `sidecar_path` (the general
/// pack/plugin-enablement half of `sync-packs.sh` writes other keys to the
/// same file) rather than overwriting it.
///
/// # Errors
///
/// Returns [`MifRhError::ConfigMissing`]/[`MifRhError::Json`] if
/// `config_path` cannot be read, [`MifRhError::Io`] if `root`'s ontology
/// directories cannot be read, [`MifRhError::OntologyPackYaml`] if a pack
/// fails to parse, and [`MifRhError::Io`]/[`MifRhError::JsonSerialize`] if
/// `sidecar_path` cannot be written.
pub fn sync_catalog(
    root: &Path,
    config_path: &Path,
    sidecar_path: &Path,
) -> Result<CatalogSyncReport, MifRhError> {
    let config = load_config_json(config_path)?;
    let mut entries = Vec::new();

    let core_dir = root.join("schemas/ontologies");
    if core_dir.is_dir() {
        let dir_entries = std::fs::read_dir(&core_dir).map_err(|source| MifRhError::Io {
            path: core_dir.display().to_string(),
            source,
        })?;
        let mut subdirs: Vec<_> = dir_entries
            .filter_map(Result::ok)
            .map(|entry| entry.path())
            .filter(|path| path.is_dir())
            .collect();
        subdirs.sort();
        for subdir in subdirs {
            for pack_path in yaml_files_in(&subdir)? {
                let display = pack_path.display().to_string();
                let contents =
                    std::fs::read_to_string(&pack_path).map_err(|source| MifRhError::Io {
                        path: display.clone(),
                        source,
                    })?;
                let pack = parse_pack(&contents, &display)?;
                let core = CORE_IDS.contains(&pack.id.as_str());
                entries.push(CatalogOntologyEntry {
                    id: pack.id,
                    version: pack.version,
                    source: repo_relative(root, &pack_path),
                    core,
                });
            }
        }
    }

    let mut enabled: Vec<String> = enabled_ontology_ids(&config);
    enabled.sort();
    for id in enabled {
        let pack_path = root
            .join("packs/ontologies")
            .join(&id)
            .join(format!("{id}.ontology.yaml"));
        if !pack_path.is_file() {
            continue;
        }
        let display = pack_path.display().to_string();
        let contents = std::fs::read_to_string(&pack_path).map_err(|source| MifRhError::Io {
            path: display.clone(),
            source,
        })?;
        let pack = parse_pack(&contents, &display)?;
        entries.push(CatalogOntologyEntry {
            id: pack.id,
            version: pack.version,
            source: repo_relative(root, &pack_path),
            core: false,
        });
    }

    let mut sidecar = if sidecar_path.exists() {
        load_config_json(sidecar_path)?
    } else {
        serde_json::json!({})
    };
    let cataloged = entries.len();
    if let Some(object) = sidecar.as_object_mut() {
        object.insert(
            "ontologies".to_string(),
            serde_json::to_value(&entries).map_err(|source| MifRhError::JsonSerialize {
                path: sidecar_path.display().to_string(),
                source,
            })?,
        );
    }
    if let Some(parent) = sidecar_path.parent()
        && !parent.as_os_str().is_empty()
    {
        std::fs::create_dir_all(parent).map_err(|source| MifRhError::Io {
            path: parent.display().to_string(),
            source,
        })?;
    }
    crate::write_json_atomic(sidecar_path, &sidecar)?;

    Ok(CatalogSyncReport { cataloged })
}

fn yaml_files_in(dir: &Path) -> Result<Vec<std::path::PathBuf>, MifRhError> {
    let entries = std::fs::read_dir(dir).map_err(|source| MifRhError::Io {
        path: dir.display().to_string(),
        source,
    })?;
    let mut files: Vec<_> = entries
        .filter_map(Result::ok)
        .map(|entry| entry.path())
        .filter(|path| {
            path.extension()
                .and_then(|ext| ext.to_str())
                .is_some_and(|ext| ext == "yaml" || ext == "yml")
        })
        .collect();
    files.sort();
    Ok(files)
}

fn repo_relative(root: &Path, path: &Path) -> String {
    path.strip_prefix(root)
        .unwrap_or(path)
        .display()
        .to_string()
}

/// The outcome of a [`sync_registry`] call.
#[derive(Debug, Clone, Default)]
pub struct RegistrySyncReport {
    /// Registry ontology ids discovered and newly added (enabled by
    /// default) to `harness.config.json`.
    pub discovered: Vec<String>,
    /// The subsequent vendor pass over every now-enabled ontology.
    pub fetch: FetchReport,
}

/// Discovers domain ontologies newly published to the registry.
///
/// Finds ontologies in `source`'s registry that `config_path` has never
/// heard of (not merely disabled — absent from `.ontologies[]` entirely),
/// adds each with `enabled: true` (this harness's default posture), then
/// vendors and catalogs everything currently enabled.
///
/// # Errors
///
/// Returns [`MifRhError::ConfigMissing`]/[`MifRhError::Json`] if
/// `config_path` cannot be read, [`MifRhError::RegistryFetch`]/
/// [`MifRhError::RegistryIndexInvalid`] if the registry index cannot be
/// read or parsed, [`MifRhError::MalformedOntologyId`] if the index
/// declares a malformed id, [`MifRhError::ConfigMalformed`] if
/// `config_path`'s `.ontologies` exists but is not an array, or any error
/// [`fetch`]/[`sync_catalog`] can return.
pub fn sync_registry(
    root: &Path,
    config_path: &Path,
    sidecar_path: &Path,
    source: &str,
) -> Result<RegistrySyncReport, MifRhError> {
    // Validate the config's shape BEFORE any network/source activity — a
    // malformed config must fail fast and offline, matching
    // `sync-registry-ontologies.sh`'s own original `jq -e '.ontologies |
    // type == "array"'` guard, which ran before it ever touched the
    // registry.
    let mut config = load_config_json(config_path)?;
    if !config
        .get("ontologies")
        .is_some_and(serde_json::Value::is_array)
    {
        return Err(MifRhError::ConfigMalformed {
            path: config_path.display().to_string(),
            detail: ".ontologies is missing or not an array".to_string(),
        });
    }

    let index_bytes = fetch_raw(source, "index.json")?;
    let index: RegistryIndex =
        serde_json::from_slice(&index_bytes).map_err(|err| MifRhError::RegistryIndexInvalid {
            registry_source: source.to_string(),
            detail: err.to_string(),
        })?;
    // Check the trust-on-first-use pin BEFORE mutating harness.config.json
    // below: `fetch()` (called later in this function) re-fetches and
    // re-checks the same index independently, but if that later check
    // fails, config.json must not have already been durably rewritten to
    // enable ontologies discovered from an index whose trust root moved.
    let lock = LockFile::load_or_default(&root.join("ontologies.lock.json"))?;
    if let Some(pinned) = &lock.index_sha256
        && lock.source == source
        && *pinned != sha256_hex(&index_bytes)
    {
        return Err(MifRhError::IndexPinMismatch {
            registry_source: source.to_string(),
            pinned: pinned.clone(),
            got: sha256_hex(&index_bytes),
        });
    }

    let known = known_ontology_ids(&config);
    let mut discovered: Vec<String> = index
        .ontologies
        .keys()
        .filter(|id| !is_committed_base(root, id) && !known.contains(*id))
        .cloned()
        .collect();
    discovered.sort();
    for id in &discovered {
        if !is_wellformed_id(id) {
            return Err(MifRhError::MalformedOntologyId { id: id.clone() });
        }
    }

    if !discovered.is_empty() {
        let object = config
            .as_object_mut()
            .ok_or_else(|| MifRhError::ConfigMalformed {
                path: config_path.display().to_string(),
                detail: "top-level document is not a JSON object".to_string(),
            })?;
        let array = object
            .entry("ontologies")
            .or_insert_with(|| serde_json::Value::Array(Vec::new()))
            .as_array_mut()
            .ok_or_else(|| MifRhError::ConfigMalformed {
                path: config_path.display().to_string(),
                detail: ".ontologies exists but is not an array".to_string(),
            })?;
        for id in &discovered {
            array.push(serde_json::json!({"id": id, "enabled": true}));
        }
        crate::write_json_atomic(config_path, &config)?;
    }

    let enabled = enabled_ontology_ids(&config);
    // `refresh: false` — sync-registry's job is discovering NEWLY published
    // ids and enabling them, not silently advancing ids already pinned in
    // ontologies.lock.json (rht#270). A newly-discovered id has no lock
    // entry yet, so it always vendors regardless of this flag.
    let fetch_report = fetch(root, source, &enabled, false)?;
    sync_catalog(root, config_path, sidecar_path)?;

    Ok(RegistrySyncReport {
        discovered,
        fetch: fetch_report,
    })
}

/// One drifted vendored ontology: its id, its pinned sha256, and the
/// sha256 actually found on disk.
#[derive(Debug, Clone)]
pub struct DriftEntry {
    /// The drifted ontology's id.
    pub id: String,
    /// The pinned (expected) sha256.
    pub pinned: String,
    /// The sha256 actually computed from the file on disk.
    pub got: String,
}

/// The outcome of a [`lock_check`] call.
#[derive(Debug, Clone, Default)]
pub struct LockCheckReport {
    /// Enabled ontology ids with no pin in `ontologies.lock.json`.
    pub missing_pins: Vec<String>,
    /// Enabled, pinned ontology ids with no vendored file on disk.
    pub not_vendored: Vec<String>,
    /// Pinned, vendored ontologies whose on-disk sha256 no longer matches
    /// the pin.
    pub drift: Vec<DriftEntry>,
    /// How many vendored ontologies were checked and matched their pin.
    pub checked: usize,
}

impl LockCheckReport {
    /// Whether every check passed: no missing pins, no un-vendored enabled
    /// ontologies, and no drift.
    #[must_use]
    pub const fn ok(&self) -> bool {
        self.missing_pins.is_empty() && self.not_vendored.is_empty() && self.drift.is_empty()
    }
}

/// Proves vendored domain ontologies match `root/ontologies.lock.json`.
///
/// Checks coverage (every enabled domain ontology is pinned and vendored)
/// and integrity (every vendored ontology present on disk hashes to its
/// pinned sha256). A missing lock file is not an error — on-demand
/// vendoring has not been adopted in the clone, so there is nothing to
/// verify.
///
/// # Errors
///
/// Returns [`MifRhError::ConfigMissing`]/[`MifRhError::Json`] if
/// `config_path` cannot be read, or [`MifRhError::Io`]/[`MifRhError::Json`]
/// if the lock file exists but cannot be read/parsed.
pub fn lock_check(root: &Path, config_path: &Path) -> Result<LockCheckReport, MifRhError> {
    let lock_path = root.join("ontologies.lock.json");
    if !lock_path.exists() {
        return Ok(LockCheckReport::default());
    }
    let lock = LockFile::load_or_default(&lock_path)?;
    let config = load_config_json(config_path)?;

    let mut report = LockCheckReport::default();
    // A BTreeSet, not a HashSet: `missing_pins` is built by iterating this
    // set, and its order should stay deterministic rather than depend on
    // hash iteration order.
    let enabled: BTreeSet<String> = enabled_ontology_ids(&config).into_iter().collect();

    for id in &enabled {
        if is_committed_base(root, id) {
            continue;
        }
        if !lock.ontologies.contains_key(id) {
            report.missing_pins.push(id.clone());
        }
    }

    for (id, entry) in &lock.ontologies {
        let yaml = root
            .join("packs/ontologies")
            .join(id)
            .join(format!("{id}.ontology.yaml"));
        if !yaml.is_file() {
            if enabled.contains(id) {
                report.not_vendored.push(id.clone());
            }
            continue;
        }
        let bytes = std::fs::read(&yaml).map_err(|source| MifRhError::Io {
            path: yaml.display().to_string(),
            source,
        })?;
        let got = sha256_hex(&bytes);
        if got == entry.sha256 {
            report.checked += 1;
        } else {
            report.drift.push(DriftEntry {
                id: id.clone(),
                pinned: entry.sha256.clone(),
                got,
            });
        }
    }

    Ok(report)
}

/// One `required` field the registry's advanced schema newly demands for an
/// entity type both the vendored and registry packs declare.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NewlyRequiredField {
    /// The entity type the field was added to.
    pub entity_type: String,
    /// The newly required field's name.
    pub field: String,
}

/// A stamped finding missing a newly required field.
///
/// The breaking-change warning [`check_pin_safety`] exists to surface,
/// distinct from `fetch`'s plain version-drift warning.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PinSafetyGap {
    /// The topic the finding belongs to.
    pub topic: String,
    /// The finding's id.
    pub finding_id: String,
    /// The finding's stamped entity type.
    pub entity_type: String,
    /// The newly required field the finding's `entity` payload lacks.
    pub field: String,
}

/// One pinned, drifted ontology's pin-safety analysis.
#[derive(Debug, Clone, Default)]
pub struct PinSafetyReport {
    /// The ontology's id.
    pub id: String,
    /// The version currently pinned in `ontologies.lock.json`.
    pub locked_version: String,
    /// The version the registry currently offers.
    pub registry_version: String,
    /// Whether the diff was actually performed. `false` when the vendored
    /// pack could not be read from disk to diff against — in that case
    /// `newly_required`/`gaps` are empty because nothing was analyzed, NOT
    /// because the drift was found safe. Callers must check this before
    /// treating an empty `gaps` as a clean bill of health.
    pub analyzed: bool,
    /// Fields the registry's advanced schema newly requires, relative to
    /// the vendored version. Always empty when `analyzed` is `false`.
    pub newly_required: Vec<NewlyRequiredField>,
    /// Stamped findings missing a newly required field. Always empty when
    /// `newly_required` is empty.
    pub gaps: Vec<PinSafetyGap>,
}

/// The `required` array of an entity type's `schema` object, as strings.
///
/// # Errors
///
/// Returns [`MifRhError::EntityTypeSchemaInvalid`] if `required` is present
/// but is not an array of strings. A missing `required` key is not an
/// error — it means the entity type declares no required fields — but a
/// PRESENT, malformed one must fail closed rather than silently default to
/// an empty list: for a pin-safety check, treating a malformed schema as
/// "nothing required" would produce a false "safe" verdict, the
/// worst-case failure mode for this specific check.
fn required_fields(
    entity_type: &str,
    schema: &serde_json::Value,
) -> Result<Vec<String>, MifRhError> {
    let Some(required) = schema.get("required") else {
        return Ok(Vec::new());
    };
    let Some(values) = required.as_array() else {
        return Err(MifRhError::EntityTypeSchemaInvalid {
            entity_type: entity_type.to_string(),
            detail: "schema.required is present but is not an array".to_string(),
        });
    };
    values
        .iter()
        .map(|value| {
            value
                .as_str()
                .map(str::to_string)
                .ok_or_else(|| MifRhError::EntityTypeSchemaInvalid {
                    entity_type: entity_type.to_string(),
                    detail: format!("schema.required contains a non-string element: {value}"),
                })
        })
        .collect()
}

/// The `required` fields `new`'s schema adds for an entity type present in
/// both `old` and `new`. An entity type absent from `old` is skipped
/// entirely: it is wholly new, so no existing stamped finding could have
/// been typed with it, and nothing about it can be "newly" required.
///
/// # Errors
///
/// Returns [`MifRhError::EntityTypeSchemaInvalid`] if either pack's
/// `schema.required` is malformed; see [`required_fields`].
fn diff_newly_required(
    old: &crate::ontology_pack::OntologyPack,
    new: &crate::ontology_pack::OntologyPack,
) -> Result<Vec<NewlyRequiredField>, MifRhError> {
    let mut out = Vec::new();
    for new_type in &new.entity_types {
        let Some(old_type) = old.entity_types.iter().find(|t| t.name == new_type.name) else {
            continue;
        };
        let old_required: HashSet<String> = required_fields(&old_type.name, &old_type.schema)?
            .into_iter()
            .collect();
        let mut seen_new = HashSet::new();
        for field in required_fields(&new_type.name, &new_type.schema)? {
            if !old_required.contains(&field) && seen_new.insert(field.clone()) {
                out.push(NewlyRequiredField {
                    entity_type: new_type.name.clone(),
                    field,
                });
            }
        }
    }
    Ok(out)
}

/// Cross-references `newly_required` against every stamped finding (basis
/// declared/resolved and valid — the same predicate `collect_topic_samples`
/// uses) across `topics` whose `resolved_ontology` names `ontology_id`,
/// reporting one [`PinSafetyGap`] per finding whose `entity` payload lacks a
/// newly required field.
fn find_pin_safety_gaps(
    reports_dir: &Path,
    topics: &[String],
    ontology_id: &str,
    newly_required: &[NewlyRequiredField],
) -> Result<Vec<PinSafetyGap>, MifRhError> {
    let mut gaps = Vec::new();
    for topic in topics {
        let map_path = reports_dir.join(topic).join("ontology-map.json");
        let contents = match std::fs::read_to_string(&map_path) {
            Ok(contents) => contents,
            Err(source) if source.kind() == std::io::ErrorKind::NotFound => continue,
            Err(source) => {
                return Err(MifRhError::Io {
                    path: map_path.display().to_string(),
                    source,
                });
            },
        };
        let records: Vec<crate::resolve::MapRecord> =
            serde_json::from_str(&contents).map_err(|source| MifRhError::Json {
                path: map_path.display().to_string(),
                source,
            })?;
        // Indexed once per topic rather than linearly scanned per finding
        // file below (O(records) once vs. O(records) per finding).
        let records_by_finding_id: std::collections::HashMap<&str, &crate::resolve::MapRecord> =
            records.iter().map(|r| (r.finding_id.as_str(), r)).collect();

        let findings_dir = reports_dir.join(topic).join("findings");
        if !findings_dir.is_dir() {
            continue;
        }
        for file in crate::review::list_finding_files(&findings_dir)? {
            let Ok(finding) = crate::finding::Finding::load(&file) else {
                continue; // gap findings are review's concern, not this one's
            };
            let Some(record) = records_by_finding_id.get(finding.id.as_str()).copied() else {
                continue;
            };
            let stamped = record.valid
                && matches!(
                    record.basis,
                    crate::resolve::Basis::Declared | crate::resolve::Basis::Resolved
                );
            if !stamped {
                continue;
            }
            let bound_to_this_ontology = record
                .resolved_ontology
                .as_deref()
                .and_then(|resolved| resolved.split('@').next())
                == Some(ontology_id);
            if !bound_to_this_ontology {
                continue;
            }
            let Some(entity_type) = record.entity_type.as_deref() else {
                continue;
            };
            gaps.extend(
                newly_required
                    .iter()
                    .filter(|nrf| nrf.entity_type == entity_type)
                    .filter(|nrf| {
                        finding
                            .entity
                            .as_ref()
                            .and_then(|entity| entity.get(&nrf.field))
                            .is_none_or(serde_json::Value::is_null)
                    })
                    .map(|nrf| PinSafetyGap {
                        topic: topic.clone(),
                        finding_id: finding.id.clone(),
                        entity_type: entity_type.to_string(),
                        field: nrf.field.clone(),
                    }),
            );
        }
    }
    Ok(gaps)
}

/// Guards against trusting `lock`'s per-id version pins when it was
/// established against a different registry source than `source`.
///
/// Unlike `fetch`, [`check_pin_safety`] never re-pins the lock — it is
/// read-only — so it must never treat `lock.ontologies`' per-id pins as
/// meaningful against a source other than the one the lock itself trusts.
/// `fetch` legitimately adopts a new source on first use (writing
/// `lock.source = source`); a read-only check has no such adoption step,
/// so a mismatch here can only mean the caller is checking pin safety
/// against the wrong registry.
///
/// # Errors
///
/// Returns [`MifRhError::LockSourceMismatch`] if `lock.source` is set and
/// differs from `source`, or if `lock.source` is empty while
/// `lock.ontologies` already holds pins (a legacy or hand-edited lock file
/// with pins but no recorded trust root — `LockFile`'s `#[serde(default)]`
/// on `source` would otherwise let it through as if empty meant "nothing
/// pinned yet").
fn check_lock_source(lock: &LockFile, source: &str) -> Result<(), MifRhError> {
    if lock.source.is_empty() {
        if lock.ontologies.is_empty() {
            return Ok(());
        }
        return Err(MifRhError::LockSourceMismatch {
            lock_source: "<unset>".to_string(),
            requested_source: source.to_string(),
        });
    }
    if lock.source != source {
        return Err(MifRhError::LockSourceMismatch {
            lock_source: lock.source.clone(),
            requested_source: source.to_string(),
        });
    }
    Ok(())
}

/// Fetches a drifted id's registry file and parses it into an
/// [`OntologyPack`], applying the same integrity checks `fetch` enforces on
/// this exact attacker-controlled path: `entry.file` must be a bare
/// filename (never a path that could escape the intended directory), the
/// downloaded bytes must match the index's own pinned sha256, and the bytes
/// must be valid UTF-8 (fail closed rather than `from_utf8_lossy`, since the
/// schema diffed by the caller must be exactly the checksum-verified bytes,
/// not a lossy substitution).
///
/// # Errors
///
/// Returns [`MifRhError::UnsafeIndexPath`] if `entry.file` is not a bare
/// filename, [`MifRhError::RegistryFetch`] if the file cannot be fetched,
/// [`MifRhError::ChecksumMismatch`] if its sha256 does not match `entry`,
/// [`MifRhError::OntologyPackNotUtf8`] if the bytes are not valid UTF-8, or
/// [`MifRhError::OntologyPackYaml`] if the pack YAML is malformed.
fn fetch_and_parse_registry_pack(
    source: &str,
    id: &str,
    entry: &IndexEntry,
) -> Result<crate::ontology_pack::OntologyPack, MifRhError> {
    if !is_bare_filename(&entry.file) {
        return Err(MifRhError::UnsafeIndexPath {
            id: id.to_string(),
            file: entry.file.clone(),
        });
    }
    let new_bytes = fetch_raw(source, &entry.file)?;
    let got = sha256_hex(&new_bytes);
    if got != entry.sha256 {
        return Err(MifRhError::ChecksumMismatch {
            id: id.to_string(),
            file: entry.file.clone(),
            expected: entry.sha256.clone(),
            got,
        });
    }
    let new_text = String::from_utf8(new_bytes).map_err(|_| MifRhError::OntologyPackNotUtf8 {
        id: id.to_string(),
        file: entry.file.clone(),
    })?;
    parse_pack(&new_text, &format!("{source}/{}", entry.file))
}

/// Validates and dedupes `ids` (preserving first-seen order), then filters
/// to only those actually pinned in `lock.ontologies` — the ids
/// [`check_pin_safety`] has anything to say about.
///
/// # Errors
///
/// Returns [`MifRhError::MalformedOntologyId`] if a requested id is not a
/// bare, lowercase slug.
fn pinned_requested_ids(lock: &LockFile, ids: &[String]) -> Result<Vec<String>, MifRhError> {
    let mut seen = HashSet::new();
    let mut pinned = Vec::new();
    for id in ids {
        // `id` is joined onto a filesystem path later (`old_path`), so it
        // must be validated the same way `resolve_fetch_set` validates
        // every id (including directly-requested ones) before any use —
        // a caller-supplied id is just as untrusted a path component as
        // one discovered via the registry's `extends` chain.
        if !is_wellformed_id(id) {
            return Err(MifRhError::MalformedOntologyId { id: id.clone() });
        }
        if seen.insert(id.as_str()) && lock.ontologies.contains_key(id) {
            pinned.push(id.clone());
        }
    }
    Ok(pinned)
}

/// Warns only when a pinned ontology's advanced schema actually breaks an
/// already-stamped finding.
///
/// For each of `ids` currently pinned in `root/ontologies.lock.json` whose
/// locked version differs from the registry's current one, diffs the
/// vendored (old) and registry (new) schema's `entity_types[].schema.required`
/// lists, and cross-references any newly required field against `topics`'
/// already-stamped findings — the narrower, smarter follow-up to `fetch`'s
/// plain version-drift warning (research-harness-template#270's proposed
/// fix #2; tracked as mif-rs#61).
///
/// An id with no lock entry, or already at the registry's current version,
/// produces no report entry — nothing pinned, or nothing drifted, to
/// analyze. An id whose vendored pack cannot be read from disk (e.g.
/// deleted since the pin was recorded) produces a report entry with empty
/// `newly_required`/`gaps`: the version drift is still real, but nothing
/// can be diffed against.
///
/// Read-only: never touches `ontologies.lock.json`, never vendors, never
/// advances a pin. Downloads the registry's current file for a drifted id
/// purely to diff it — unlike `fetch`, which never re-fetches a
/// pinned-skipped id's file at all — applying the same integrity checks
/// `fetch` applies to that same attacker-controlled path (index-pin
/// verification, bare-filename validation, sha256 verification against the
/// index) before trusting any fetched byte.
///
/// # Errors
///
/// Returns [`MifRhError::MalformedOntologyId`] if a requested id is not a
/// bare, lowercase slug, [`MifRhError::LockSourceMismatch`] if
/// `ontologies.lock.json` is pinned to a different source than `source`,
/// [`MifRhError::RegistryFetch`] if the registry index or a drifted id's
/// file cannot be fetched, [`MifRhError::RegistryIndexInvalid`] if the
/// index is not valid JSON, [`MifRhError::IndexPinMismatch`] if the
/// source's index sha256 no longer matches the lock's pinned value,
/// [`MifRhError::OntologyNotInRegistry`] if a requested id has no index
/// entry, [`MifRhError::UnsafeIndexPath`] if the index names an unsafe
/// file path, [`MifRhError::ChecksumMismatch`] if a fetched file's sha256
/// does not match the index, [`MifRhError::OntologyPackNotUtf8`] if a
/// checksum-verified fetched file is not valid UTF-8,
/// [`MifRhError::OntologyPackYaml`] if the vendored or registry pack YAML
/// is malformed, or [`MifRhError::Json`] if a topic's `ontology-map.json`
/// exists but fails to parse. A topic whose `ontology-map.json` does not
/// exist yet (e.g. review has never run for it) contributes no gaps for
/// that topic rather than erroring — the same treatment an individual
/// finding file that cannot be read or parsed gets (the same treatment
/// `collect_topic_samples` gives an unreadable finding) — a gap-analysis
/// finding is review's concern, not this function's.
pub fn check_pin_safety(
    root: &Path,
    source: &str,
    reports_dir: &Path,
    topics: &[String],
    ids: &[String],
) -> Result<Vec<PinSafetyReport>, MifRhError> {
    let lock = LockFile::load_or_default(&root.join("ontologies.lock.json"))?;
    // Validated and filtered to actually-pinned ids before any network/lock-source
    // work: nothing to analyze (and nothing to fail closed about) if none are pinned.
    let pinned_ids = pinned_requested_ids(&lock, ids)?;
    if pinned_ids.is_empty() {
        return Ok(Vec::new());
    }
    check_lock_source(&lock, source)?;
    let index_bytes = fetch_raw(source, "index.json")?;
    let index_sha256 = sha256_hex(&index_bytes);
    let index: RegistryIndex =
        serde_json::from_slice(&index_bytes).map_err(|err| MifRhError::RegistryIndexInvalid {
            registry_source: source.to_string(),
            detail: err.to_string(),
        })?;
    // Same trust-on-first-use check `fetch` enforces before trusting index
    // content (`vendor.rs`'s module doc: the index is "fully
    // attacker-controlled"). Read-only here — never re-pins, only refuses
    // to proceed on a mismatch.
    if let Some(pinned) = &lock.index_sha256
        && lock.source == source
        && *pinned != index_sha256
    {
        return Err(MifRhError::IndexPinMismatch {
            registry_source: source.to_string(),
            pinned: pinned.clone(),
            got: index_sha256,
        });
    }

    let mut reports = Vec::new();
    for id in &pinned_ids {
        // Guaranteed present: pinned_requested_ids already filtered to ids in lock.ontologies.
        let locked = &lock.ontologies[id];
        let entry = index
            .ontologies
            .get(id)
            .ok_or_else(|| MifRhError::OntologyNotInRegistry { id: id.clone() })?;
        if locked.version == entry.version {
            continue; // not drifted, nothing to check
        }

        let old_path = root
            .join("packs/ontologies")
            .join(id)
            .join(format!("{id}.ontology.yaml"));
        let old_pack = match std::fs::read_to_string(&old_path) {
            Ok(yaml) => Some(parse_pack(&yaml, &old_path.display().to_string())?),
            Err(source) if source.kind() == std::io::ErrorKind::NotFound => None,
            Err(source) => {
                return Err(MifRhError::Io {
                    path: old_path.display().to_string(),
                    source,
                });
            },
        };
        let Some(old_pack) = old_pack else {
            reports.push(PinSafetyReport {
                id: id.clone(),
                locked_version: locked.version.clone(),
                registry_version: entry.version.clone(),
                analyzed: false,
                newly_required: Vec::new(),
                gaps: Vec::new(),
            });
            continue;
        };

        let new_pack = fetch_and_parse_registry_pack(source, id, entry)?;

        let newly_required = diff_newly_required(&old_pack, &new_pack)?;
        let gaps = if newly_required.is_empty() {
            Vec::new()
        } else {
            find_pin_safety_gaps(reports_dir, topics, id, &newly_required)?
        };

        reports.push(PinSafetyReport {
            id: id.clone(),
            locked_version: locked.version.clone(),
            registry_version: entry.version.clone(),
            analyzed: true,
            newly_required,
            gaps,
        });
    }
    Ok(reports)
}

#[cfg(test)]
mod tests {
    use std::fs;

    use super::{
        DEFAULT_REGISTRY_SOURCE, diff_newly_required, fetch, is_bare_filename, is_wellformed_id,
        lock_check, resolve_source, sync_catalog, sync_registry,
    };
    use crate::ontology_pack::parse_pack;

    const EDU_INDEX: &str = r#"{
        "ontologies": {
            "edu-fixture": {
                "version": "0.1.0",
                "sha256": "REPLACED",
                "file": "edu-fixture.ontology.yaml",
                "extends": ["mif-base"]
            }
        }
    }"#;

    const EDU_YAML: &str = "ontology:\n  id: edu-fixture\n  version: \"0.1.0\"\n  description: \"An edu fixture\"\n  extends: [mif-base]\nentity_types: []\n";

    fn sha256_of(bytes: &[u8]) -> String {
        super::sha256_hex(bytes)
    }

    /// Writes a local-directory registry source (index.json + one ontology
    /// file) under `dir/registry/`, with the index's sha256 filled in for
    /// real, and returns the source path as a string.
    fn write_local_registry(dir: &std::path::Path) -> String {
        let registry = dir.join("registry");
        fs::create_dir_all(&registry).unwrap();
        let sha = sha256_of(EDU_YAML.as_bytes());
        let index = EDU_INDEX.replace("REPLACED", &sha);
        fs::write(registry.join("index.json"), index).unwrap();
        fs::write(registry.join("edu-fixture.ontology.yaml"), EDU_YAML).unwrap();
        registry.display().to_string()
    }

    fn write_base_layer(root: &std::path::Path) {
        fs::create_dir_all(root.join("schemas/ontologies/mif-base")).unwrap();
        fs::write(
            root.join("schemas/ontologies/mif-base/mif-base.ontology.yaml"),
            "ontology:\n  id: mif-base\n  version: \"1.0.0\"\nentity_types: []\n",
        )
        .unwrap();
    }

    #[test]
    fn resolve_source_defaults_to_the_canonical_registry() {
        let dir = tempfile::tempdir().unwrap();
        assert_eq!(resolve_source(dir.path(), None), DEFAULT_REGISTRY_SOURCE);
    }

    #[test]
    fn resolve_source_prefers_an_explicit_override() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join(".ontologies.source"), "/marker/path\n").unwrap();
        assert_eq!(resolve_source(dir.path(), Some("/override/")), "/override");
    }

    #[test]
    fn resolve_source_falls_back_to_the_marker_file() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join(".ontologies.source"), "/marker/path/\n").unwrap();
        assert_eq!(resolve_source(dir.path(), None), "/marker/path");
    }

    #[test]
    fn is_wellformed_id_accepts_bare_lowercase_slugs_only() {
        assert!(is_wellformed_id("clinical-trials"));
        assert!(is_wellformed_id("edu2"));
        assert!(!is_wellformed_id(""));
        assert!(!is_wellformed_id("../etc"));
        assert!(!is_wellformed_id("Clinical"));
        assert!(!is_wellformed_id("has space"));
    }

    #[test]
    fn is_bare_filename_rejects_a_trailing_slash() {
        // Path::components() alone treats "foo/" as a single Normal("foo")
        // component with nothing after it, so it would otherwise pass as
        // if it were the bare filename "foo" — this must be rejected.
        assert!(!is_bare_filename("foo/"));
        assert!(!is_bare_filename("foo/bar"));
        assert!(!is_bare_filename("/foo"));
        assert!(!is_bare_filename("foo\\bar"));
        assert!(is_bare_filename("edu-fixture.ontology.yaml"));
    }

    #[test]
    fn fetch_vendors_a_verified_ontology_and_skips_committed_bases() {
        let dir = tempfile::tempdir().unwrap();
        write_base_layer(dir.path());
        let source = write_local_registry(dir.path());

        let report = fetch(dir.path(), &source, &["edu-fixture".to_string()], false).unwrap();

        assert_eq!(report.vendored.len(), 1);
        assert_eq!(report.vendored[0].id, "edu-fixture");
        let vendored_yaml = dir
            .path()
            .join("packs/ontologies/edu-fixture/edu-fixture.ontology.yaml");
        assert!(vendored_yaml.is_file());
        let lock: super::LockFile = serde_json::from_str(
            &fs::read_to_string(dir.path().join("ontologies.lock.json")).unwrap(),
        )
        .unwrap();
        assert!(lock.ontologies.contains_key("edu-fixture"));
        assert!(lock.index_sha256.is_some());
        // mif-base is a committed base layer: it must never be vendored,
        // even though edu-fixture's extends names it.
        assert!(!dir.path().join("packs/ontologies/mif-base").exists());
    }

    /// Writes a registry at `version` for `edu-fixture` (distinct content
    /// from `write_local_registry`'s 0.1.0, so its sha256 genuinely
    /// differs), then seeds `ontologies.lock.json` claiming `edu-fixture`
    /// is already pinned at `locked_version` — an already-index-trusted
    /// registry (the index sha256 in the lock matches the one on disk) that
    /// has moved past a per-id pin, the exact drift `fetch`'s pin-respecting
    /// check (rht#270) must catch. Returns the registry source path.
    fn seed_registry_ahead_of_a_pinned_lock(
        dir: &std::path::Path,
        registry_version: &str,
        locked_version: &str,
    ) -> String {
        let registry = dir.join("registry");
        fs::create_dir_all(&registry).unwrap();
        let yaml = format!(
            "ontology:\n  id: edu-fixture\n  version: \"{registry_version}\"\n  description: \
             \"An edu fixture at {registry_version}\"\n  extends: [mif-base]\nentity_types: \
             []\n"
        );
        let sha = sha256_of(yaml.as_bytes());
        let index = format!(
            r#"{{"ontologies":{{"edu-fixture":{{"version":"{registry_version}","sha256":"{sha}","file":"edu-fixture.ontology.yaml","extends":["mif-base"]}}}}}}"#
        );
        fs::write(registry.join("index.json"), &index).unwrap();
        fs::write(registry.join("edu-fixture.ontology.yaml"), yaml).unwrap();

        let source = registry.display().to_string();
        let lock = serde_json::json!({
            "schema": "mif-ontology-lock/v1",
            "source": source,
            "index_sha256": sha256_of(index.as_bytes()),
            "ontologies": {
                "edu-fixture": {"version": locked_version, "sha256": "irrelevant-for-this-test"}
            }
        });
        fs::write(
            dir.join("ontologies.lock.json"),
            serde_json::to_string(&lock).unwrap(),
        )
        .unwrap();
        source
    }

    #[test]
    fn fetch_leaves_a_pinned_ontology_untouched_when_the_registry_advances_without_refresh() {
        let dir = tempfile::tempdir().unwrap();
        write_base_layer(dir.path());
        let source = seed_registry_ahead_of_a_pinned_lock(dir.path(), "0.2.0", "0.1.0");

        let report = fetch(dir.path(), &source, &["edu-fixture".to_string()], false).unwrap();

        assert!(report.vendored.is_empty());
        assert_eq!(report.pinned_skipped.len(), 1);
        assert_eq!(report.pinned_skipped[0].id, "edu-fixture");
        assert_eq!(report.pinned_skipped[0].locked_version, "0.1.0");
        assert_eq!(report.pinned_skipped[0].registry_version, "0.2.0");

        let lock: super::LockFile = serde_json::from_str(
            &fs::read_to_string(dir.path().join("ontologies.lock.json")).unwrap(),
        )
        .unwrap();
        assert_eq!(lock.ontologies["edu-fixture"].version, "0.1.0");
        assert!(
            !dir.path()
                .join("packs/ontologies/edu-fixture/edu-fixture.ontology.yaml")
                .exists(),
            "a pinned-and-skipped id must never be written to disk"
        );
    }

    #[test]
    fn fetch_refresh_advances_a_pinned_ontology_to_the_registry_version() {
        let dir = tempfile::tempdir().unwrap();
        write_base_layer(dir.path());
        let source = seed_registry_ahead_of_a_pinned_lock(dir.path(), "0.2.0", "0.1.0");

        let report = fetch(dir.path(), &source, &["edu-fixture".to_string()], true).unwrap();

        assert!(report.pinned_skipped.is_empty());
        assert_eq!(report.vendored.len(), 1);
        assert_eq!(report.vendored[0].version, "0.2.0");

        let lock: super::LockFile = serde_json::from_str(
            &fs::read_to_string(dir.path().join("ontologies.lock.json")).unwrap(),
        )
        .unwrap();
        assert_eq!(lock.ontologies["edu-fixture"].version, "0.2.0");
        assert!(
            dir.path()
                .join("packs/ontologies/edu-fixture/edu-fixture.ontology.yaml")
                .exists()
        );
    }

    #[test]
    fn fetch_persists_a_cleared_index_pin_even_when_every_id_is_pinned_skipped() {
        // Regression: when every requested id is pinned-and-skipped, no
        // per-id write happens in the loop, so the index_sha256/source
        // mutated before the loop must still be persisted by the
        // fallback write after it — otherwise a deliberate re-pin (clear
        // index_sha256, re-fetch) silently fails to record the new trust
        // root whenever it also happens to hit only already-pinned ids.
        let dir = tempfile::tempdir().unwrap();
        write_base_layer(dir.path());
        let source = seed_registry_ahead_of_a_pinned_lock(dir.path(), "0.2.0", "0.1.0");

        // Clear index_sha256, simulating a deliberate re-pin in progress.
        let mut lock: serde_json::Value = serde_json::from_str(
            &fs::read_to_string(dir.path().join("ontologies.lock.json")).unwrap(),
        )
        .unwrap();
        lock.as_object_mut().unwrap().remove("index_sha256");
        fs::write(
            dir.path().join("ontologies.lock.json"),
            serde_json::to_string(&lock).unwrap(),
        )
        .unwrap();

        let report = fetch(dir.path(), &source, &["edu-fixture".to_string()], false).unwrap();
        assert!(report.vendored.is_empty());
        assert_eq!(report.pinned_skipped.len(), 1);

        let after: super::LockFile = serde_json::from_str(
            &fs::read_to_string(dir.path().join("ontologies.lock.json")).unwrap(),
        )
        .unwrap();
        assert!(
            after.index_sha256.is_some(),
            "the re-pinned index_sha256 must be persisted even though every \
             requested id was pinned-skipped"
        );
    }

    #[test]
    fn fetch_fails_closed_on_a_checksum_mismatch() {
        let dir = tempfile::tempdir().unwrap();
        write_base_layer(dir.path());
        let registry = dir.path().join("registry");
        fs::create_dir_all(&registry).unwrap();
        let index = EDU_INDEX.replace(
            "REPLACED",
            "0000000000000000000000000000000000000000000000000000000000000000",
        );
        fs::write(registry.join("index.json"), index).unwrap();
        fs::write(registry.join("edu-fixture.ontology.yaml"), EDU_YAML).unwrap();

        let error = fetch(
            dir.path(),
            &registry.display().to_string(),
            &["edu-fixture".to_string()],
            false,
        )
        .unwrap_err();
        assert!(matches!(error, super::MifRhError::ChecksumMismatch { .. }));
        assert!(!dir.path().join("packs/ontologies/edu-fixture").exists());
    }

    #[test]
    fn fetch_rejects_a_windows_style_or_absolute_index_file_path() {
        let dir = tempfile::tempdir().unwrap();
        write_base_layer(dir.path());
        let registry = dir.path().join("registry");
        fs::create_dir_all(&registry).unwrap();
        for unsafe_file in [
            "..\\..\\etc\\passwd",
            "C:\\Windows\\evil.yaml",
            "/etc/passwd",
        ] {
            // Substitute via a JSON-escaped literal (not a raw string replace)
            // so a literal backslash in `unsafe_file` doesn't itself produce
            // invalid JSON.
            let index = EDU_INDEX
                .replace("REPLACED", &sha256_of(EDU_YAML.as_bytes()))
                .replace(
                    "\"edu-fixture.ontology.yaml\"",
                    &serde_json::to_string(unsafe_file).unwrap(),
                );
            fs::write(registry.join("index.json"), index).unwrap();

            let error = fetch(
                dir.path(),
                &registry.display().to_string(),
                &["edu-fixture".to_string()],
                false,
            )
            .unwrap_err();
            assert!(
                matches!(error, super::MifRhError::UnsafeIndexPath { .. }),
                "expected UnsafeIndexPath for {unsafe_file:?}, got {error:?}"
            );
        }
    }

    #[test]
    fn fetch_leaves_no_file_behind_when_the_vendored_yaml_fails_to_parse() {
        let dir = tempfile::tempdir().unwrap();
        write_base_layer(dir.path());
        let registry = dir.path().join("registry");
        fs::create_dir_all(&registry).unwrap();
        let malformed = b"not: [valid, yaml, ontology shape";
        let index = EDU_INDEX.replace("REPLACED", &sha256_of(malformed));
        fs::write(registry.join("index.json"), index).unwrap();
        fs::write(registry.join("edu-fixture.ontology.yaml"), malformed).unwrap();

        let error = fetch(
            dir.path(),
            &registry.display().to_string(),
            &["edu-fixture".to_string()],
            false,
        )
        .unwrap_err();

        assert!(
            matches!(error, super::MifRhError::OntologyPackYaml { .. }),
            "{error:?}"
        );
        assert!(!dir.path().join("packs/ontologies/edu-fixture").exists());
    }

    #[test]
    fn fetch_refuses_a_moved_trust_root_for_the_same_source() {
        let dir = tempfile::tempdir().unwrap();
        write_base_layer(dir.path());
        let source = write_local_registry(dir.path());
        fetch(dir.path(), &source, &["edu-fixture".to_string()], false).unwrap();

        // The same source's index.json now hashes differently.
        fs::write(
            std::path::Path::new(&source).join("index.json"),
            EDU_INDEX
                .replace("REPLACED", &sha256_of(EDU_YAML.as_bytes()))
                .replace('0', "1"),
        )
        .unwrap();

        let error = fetch(dir.path(), &source, &["edu-fixture".to_string()], false).unwrap_err();
        assert!(matches!(error, super::MifRhError::IndexPinMismatch { .. }));
    }

    #[test]
    fn fetch_reports_an_id_absent_from_the_registry() {
        let dir = tempfile::tempdir().unwrap();
        let source = write_local_registry(dir.path());

        let error = fetch(dir.path(), &source, &["nonexistent-id".to_string()], false).unwrap_err();
        assert!(matches!(
            error,
            super::MifRhError::OntologyNotInRegistry { .. }
        ));
    }

    #[test]
    fn fetch_rejects_a_path_traversal_id_reached_via_an_extends_ancestor() {
        // A compromised registry can name anything in an `extends` array —
        // it is fully attacker-controlled index content, just like a
        // requested id. A malformed ancestor id must be rejected before it
        // is ever joined onto a filesystem path, not just at discovery time
        // in `sync_registry`.
        let dir = tempfile::tempdir().unwrap();
        let registry = dir.path().join("registry");
        fs::create_dir_all(&registry).unwrap();
        let sha = sha256_of(EDU_YAML.as_bytes());
        let malicious_index = format!(
            r#"{{"ontologies":{{"edu-fixture":{{"version":"0.1.0","sha256":"{sha}","file":"edu-fixture.ontology.yaml","extends":["../../../etc"]}}}}}}"#
        );
        fs::write(registry.join("index.json"), malicious_index).unwrap();
        fs::write(registry.join("edu-fixture.ontology.yaml"), EDU_YAML).unwrap();

        let error = fetch(
            dir.path(),
            &registry.display().to_string(),
            &["edu-fixture".to_string()],
            false,
        )
        .unwrap_err();
        assert!(matches!(
            error,
            super::MifRhError::MalformedOntologyId { .. }
        ));
        assert!(!dir.path().join("packs/ontologies/edu-fixture").exists());
    }

    #[test]
    fn a_failure_partway_through_a_batch_still_pins_the_ontologies_already_vendored() {
        let dir = tempfile::tempdir().unwrap();
        let registry = dir.path().join("registry");
        fs::create_dir_all(&registry).unwrap();
        let good_sha = sha256_of(EDU_YAML.as_bytes());
        let index = format!(
            r#"{{"ontologies":{{
                "edu-fixture":{{"version":"0.1.0","sha256":"{good_sha}","file":"edu-fixture.ontology.yaml","extends":[]}},
                "bad-fixture":{{"version":"0.1.0","sha256":"0000000000000000000000000000000000000000000000000000000000000000","file":"edu-fixture.ontology.yaml","extends":[]}}
            }}}}"#
        );
        fs::write(registry.join("index.json"), index).unwrap();
        fs::write(registry.join("edu-fixture.ontology.yaml"), EDU_YAML).unwrap();

        let source = registry.display().to_string();
        let error = fetch(
            dir.path(),
            &source,
            &["edu-fixture".to_string(), "bad-fixture".to_string()],
            false,
        )
        .unwrap_err();
        assert!(matches!(error, super::MifRhError::ChecksumMismatch { .. }));

        // edu-fixture was vendored successfully before bad-fixture failed;
        // it must be pinned in the lock, not left as an untracked,
        // unverifiable file on disk.
        let lock: super::LockFile = serde_json::from_str(
            &fs::read_to_string(dir.path().join("ontologies.lock.json")).unwrap(),
        )
        .unwrap();
        assert!(lock.ontologies.contains_key("edu-fixture"));
        assert!(dir.path().join("packs/ontologies/edu-fixture").is_dir());
    }

    #[test]
    fn lock_check_passes_cleanly_with_no_lock_file() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(
            dir.path().join("harness.config.json"),
            r#"{"ontologies":[]}"#,
        )
        .unwrap();
        let report = lock_check(dir.path(), &dir.path().join("harness.config.json")).unwrap();
        assert!(report.ok());
    }

    #[test]
    fn lock_check_flags_a_missing_pin_for_an_enabled_ontology() {
        let dir = tempfile::tempdir().unwrap();
        let config_path = dir.path().join("harness.config.json");
        fs::write(
            &config_path,
            r#"{"ontologies":[{"id":"edu-fixture","enabled":true}]}"#,
        )
        .unwrap();
        fs::write(
            dir.path().join("ontologies.lock.json"),
            r#"{"schema":"mif-ontology-lock/v1","source":"x","ontologies":{}}"#,
        )
        .unwrap();

        let report = lock_check(dir.path(), &config_path).unwrap();
        assert!(!report.ok());
        assert_eq!(report.missing_pins, ["edu-fixture"]);
    }

    #[test]
    fn lock_check_flags_drift_when_a_vendored_file_no_longer_matches_its_pin() {
        let dir = tempfile::tempdir().unwrap();
        let config_path = dir.path().join("harness.config.json");
        fs::write(&config_path, r#"{"ontologies":[]}"#).unwrap();
        fs::create_dir_all(dir.path().join("packs/ontologies/edu-fixture")).unwrap();
        fs::write(
            dir.path()
                .join("packs/ontologies/edu-fixture/edu-fixture.ontology.yaml"),
            "edited locally",
        )
        .unwrap();
        fs::write(
            dir.path().join("ontologies.lock.json"),
            r#"{"schema":"mif-ontology-lock/v1","source":"x","ontologies":{"edu-fixture":{"version":"0.1.0","sha256":"deadbeef"}}}"#,
        )
        .unwrap();

        let report = lock_check(dir.path(), &config_path).unwrap();
        assert!(!report.ok());
        assert_eq!(report.drift.len(), 1);
        assert_eq!(report.drift[0].id, "edu-fixture");
    }

    #[test]
    fn sync_catalog_catalogs_core_and_enabled_ontologies() {
        let dir = tempfile::tempdir().unwrap();
        write_base_layer(dir.path());
        let config_path = dir.path().join("harness.config.json");
        fs::write(
            &config_path,
            r#"{"ontologies":[{"id":"edu-fixture","enabled":true}]}"#,
        )
        .unwrap();
        fs::create_dir_all(dir.path().join("packs/ontologies/edu-fixture")).unwrap();
        fs::write(
            dir.path()
                .join("packs/ontologies/edu-fixture/edu-fixture.ontology.yaml"),
            EDU_YAML,
        )
        .unwrap();
        let sidecar_path = dir.path().join("enabled-packs.json");
        fs::write(&sidecar_path, r#"{"enabledPlugins":["x"]}"#).unwrap();

        let report = sync_catalog(dir.path(), &config_path, &sidecar_path).unwrap();
        assert_eq!(report.cataloged, 2);

        let sidecar: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&sidecar_path).unwrap()).unwrap();
        // The unrelated key the general pack-sync half owns must survive.
        assert_eq!(sidecar["enabledPlugins"], serde_json::json!(["x"]));
        let ontologies = sidecar["ontologies"].as_array().unwrap();
        assert!(
            ontologies
                .iter()
                .any(|e| e["id"] == "mif-base" && e["core"] == true)
        );
        assert!(
            ontologies
                .iter()
                .any(|e| e["id"] == "edu-fixture" && e["core"] == false)
        );
    }

    #[test]
    fn sync_catalog_creates_the_sidecars_parent_directory_when_missing() {
        let dir = tempfile::tempdir().unwrap();
        let config_path = dir.path().join("harness.config.json");
        fs::write(&config_path, r#"{"ontologies":[]}"#).unwrap();
        // No `.claude/` directory exists yet — a fresh clone before any
        // sync has ever run.
        let sidecar_path = dir.path().join(".claude/enabled-packs.json");

        let report = sync_catalog(dir.path(), &config_path, &sidecar_path).unwrap();
        assert_eq!(report.cataloged, 0);
        assert!(sidecar_path.is_file());
    }

    #[test]
    fn sync_registry_discovers_and_enables_a_new_registry_ontology_and_preserves_other_config_fields()
     {
        let dir = tempfile::tempdir().unwrap();
        write_base_layer(dir.path());
        let source = write_local_registry(dir.path());
        let config_path = dir.path().join("harness.config.json");
        fs::write(
            &config_path,
            r#"{"version":"1.2.3","topics":[{"id":"t","ontologies":[]}],"ontologies":[]}"#,
        )
        .unwrap();
        let sidecar_path = dir.path().join("enabled-packs.json");

        let report = sync_registry(dir.path(), &config_path, &sidecar_path, &source).unwrap();

        assert_eq!(report.discovered, ["edu-fixture"]);
        assert_eq!(report.fetch.vendored.len(), 1);

        let config: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&config_path).unwrap()).unwrap();
        // Untouched top-level fields must survive the read-modify-write.
        assert_eq!(config["version"], "1.2.3");
        assert_eq!(config["topics"][0]["id"], "t");
        assert_eq!(config["ontologies"][0]["id"], "edu-fixture");
        assert_eq!(config["ontologies"][0]["enabled"], true);
    }

    #[test]
    fn sync_registry_is_idempotent_when_nothing_new_is_published() {
        let dir = tempfile::tempdir().unwrap();
        write_base_layer(dir.path());
        let source = write_local_registry(dir.path());
        let config_path = dir.path().join("harness.config.json");
        fs::write(
            &config_path,
            r#"{"ontologies":[{"id":"edu-fixture","enabled":true}]}"#,
        )
        .unwrap();
        let sidecar_path = dir.path().join("enabled-packs.json");

        let report = sync_registry(dir.path(), &config_path, &sidecar_path, &source).unwrap();
        assert!(report.discovered.is_empty());
    }

    #[test]
    fn sync_registry_rejects_a_non_array_ontologies_field_without_touching_the_network() {
        // A deliberately unreachable source: if the config-shape check ran
        // AFTER fetching the index (as it once did), this would hang or
        // fail on a network error instead of failing closed immediately on
        // the malformed config, matching
        // `sync-registry-ontologies.sh`'s own original ordering (config
        // validated before any registry access).
        let dir = tempfile::tempdir().unwrap();
        let config_path = dir.path().join("harness.config.json");
        fs::write(&config_path, r#"{"ontologies":"not-an-array"}"#).unwrap();
        let sidecar_path = dir.path().join("enabled-packs.json");

        let error = sync_registry(
            dir.path(),
            &config_path,
            &sidecar_path,
            "http://127.0.0.1.invalid/unreachable",
        )
        .unwrap_err();
        assert!(matches!(error, super::MifRhError::ConfigMalformed { .. }));
    }

    #[test]
    fn sync_registry_rejects_invalid_json_config_without_touching_the_network() {
        let dir = tempfile::tempdir().unwrap();
        let config_path = dir.path().join("harness.config.json");
        fs::write(&config_path, "not json at all").unwrap();
        let sidecar_path = dir.path().join("enabled-packs.json");

        let error = sync_registry(
            dir.path(),
            &config_path,
            &sidecar_path,
            "http://127.0.0.1.invalid/unreachable",
        )
        .unwrap_err();
        assert!(matches!(error, super::MifRhError::Json { .. }));
    }

    /// Seeds a full pin-safety fixture: a vendored (old) pack on disk at
    /// `locked_version`, a registry (new) pack at `registry_version`
    /// (`title` entity type's `schema.required` growing from `[name]` to
    /// `[name, isbn]`), and a lock pinned at `locked_version`. Returns the
    /// registry source path.
    fn seed_pin_safety_fixture(
        dir: &std::path::Path,
        locked_version: &str,
        registry_version: &str,
    ) -> String {
        let old_yaml = format!(
            "ontology:\n  id: edu-fixture\n  version: \"{locked_version}\"\n  extends: \
             [mif-base]\nentity_types:\n  - name: title\n    schema:\n      required: \
             [name]\n      properties:\n        name: {{type: string}}\n"
        );
        fs::create_dir_all(dir.join("packs/ontologies/edu-fixture")).unwrap();
        fs::write(
            dir.join("packs/ontologies/edu-fixture/edu-fixture.ontology.yaml"),
            &old_yaml,
        )
        .unwrap();

        let registry = dir.join("registry");
        fs::create_dir_all(&registry).unwrap();
        let new_yaml = format!(
            "ontology:\n  id: edu-fixture\n  version: \"{registry_version}\"\n  extends: \
             [mif-base]\nentity_types:\n  - name: title\n    schema:\n      required: [name, \
             isbn]\n      properties:\n        name: {{type: string}}\n        isbn: {{type: \
             string}}\n"
        );
        let sha = sha256_of(new_yaml.as_bytes());
        let index = format!(
            r#"{{"ontologies":{{"edu-fixture":{{"version":"{registry_version}","sha256":"{sha}","file":"edu-fixture.ontology.yaml","extends":["mif-base"]}}}}}}"#
        );
        fs::write(registry.join("index.json"), &index).unwrap();
        fs::write(registry.join("edu-fixture.ontology.yaml"), &new_yaml).unwrap();

        let source = registry.display().to_string();
        let lock = serde_json::json!({
            "schema": "mif-ontology-lock/v1",
            "source": source,
            "index_sha256": sha256_of(index.as_bytes()),
            "ontologies": {
                "edu-fixture": {"version": locked_version, "sha256": "irrelevant-for-this-test"}
            }
        });
        fs::write(
            dir.join("ontologies.lock.json"),
            serde_json::to_string(&lock).unwrap(),
        )
        .unwrap();
        source
    }

    /// Writes one topic's `ontology-map.json` (one stamped `title` record
    /// resolved against `edu-fixture@{locked_version}`) and a matching
    /// finding file whose `entity` payload is `entity_json`.
    fn write_stamped_finding(
        reports_dir: &std::path::Path,
        topic: &str,
        locked_version: &str,
        entity_json: &str,
    ) {
        let topic_dir = reports_dir.join(topic);
        fs::create_dir_all(topic_dir.join("findings")).unwrap();
        fs::write(
            topic_dir.join("ontology-map.json"),
            format!(
                r#"[{{"finding_id":"f-1","entity_type":"title","resolved_ontology":"edu-fixture@{locked_version}","basis":"resolved","valid":true}}]"#
            ),
        )
        .unwrap();
        fs::write(
            topic_dir.join("findings/f-1.json"),
            format!(r#"{{"@id":"f-1","entity":{entity_json}}}"#),
        )
        .unwrap();
    }

    #[test]
    fn diff_newly_required_dedupes_a_field_repeated_in_the_new_schema() {
        let old_yaml = "ontology:\n  id: edu-fixture\n  version: \"0.1.0\"\n  extends: \
                        [mif-base]\nentity_types:\n  - name: title\n    schema:\n      required: \
                        [name]\n      properties:\n        name: {type: string}\n";
        let new_yaml = "ontology:\n  id: edu-fixture\n  version: \"0.2.0\"\n  extends: \
                        [mif-base]\nentity_types:\n  - name: title\n    schema:\n      required: \
                        [name, isbn, isbn]\n      properties:\n        name: {type: string}\n        \
                        isbn: {type: string}\n";
        let old_pack = parse_pack(old_yaml, "old").unwrap();
        let new_pack = parse_pack(new_yaml, "new").unwrap();

        let newly_required = diff_newly_required(&old_pack, &new_pack).unwrap();

        assert_eq!(
            newly_required.len(),
            1,
            "a required field repeated in the new schema must be reported once, not once per repetition"
        );
        assert_eq!(newly_required[0].entity_type, "title");
        assert_eq!(newly_required[0].field, "isbn");
    }

    #[test]
    fn check_pin_safety_reports_nothing_for_an_id_with_no_lock_entry() {
        let dir = tempfile::tempdir().unwrap();
        let source = seed_pin_safety_fixture(dir.path(), "0.1.0", "0.2.0");
        fs::remove_file(dir.path().join("ontologies.lock.json")).unwrap();

        let reports = super::check_pin_safety(
            dir.path(),
            &source,
            &dir.path().join("reports"),
            &[],
            &["edu-fixture".to_string()],
        )
        .unwrap();
        assert!(reports.is_empty());
    }

    #[test]
    fn check_pin_safety_reports_nothing_when_not_drifted() {
        let dir = tempfile::tempdir().unwrap();
        let source = seed_pin_safety_fixture(dir.path(), "0.2.0", "0.2.0");

        let reports = super::check_pin_safety(
            dir.path(),
            &source,
            &dir.path().join("reports"),
            &[],
            &["edu-fixture".to_string()],
        )
        .unwrap();
        assert!(reports.is_empty());
    }

    #[test]
    fn check_pin_safety_dedupes_a_repeated_id() {
        let dir = tempfile::tempdir().unwrap();
        let source = seed_pin_safety_fixture(dir.path(), "0.1.0", "0.2.0");

        let reports = super::check_pin_safety(
            dir.path(),
            &source,
            &dir.path().join("reports"),
            &[],
            &["edu-fixture".to_string(), "edu-fixture".to_string()],
        )
        .unwrap();

        assert_eq!(
            reports.len(),
            1,
            "a duplicate id in the request must be analyzed once, not once per repetition"
        );
    }

    #[test]
    fn check_pin_safety_flags_a_newly_required_field_missing_from_a_stamped_finding() {
        let dir = tempfile::tempdir().unwrap();
        let source = seed_pin_safety_fixture(dir.path(), "0.1.0", "0.2.0");
        let reports_dir = dir.path().join("reports");
        write_stamped_finding(&reports_dir, "edu", "0.1.0", r#"{"name":"Algebra I"}"#);

        let reports = super::check_pin_safety(
            dir.path(),
            &source,
            &reports_dir,
            &["edu".to_string()],
            &["edu-fixture".to_string()],
        )
        .unwrap();

        assert_eq!(reports.len(), 1);
        let report = &reports[0];
        assert_eq!(report.locked_version, "0.1.0");
        assert_eq!(report.registry_version, "0.2.0");
        assert_eq!(report.newly_required.len(), 1);
        assert_eq!(report.newly_required[0].entity_type, "title");
        assert_eq!(report.newly_required[0].field, "isbn");
        assert_eq!(report.gaps.len(), 1);
        assert_eq!(report.gaps[0].finding_id, "f-1");
        assert_eq!(report.gaps[0].topic, "edu");
        assert_eq!(report.gaps[0].field, "isbn");
    }

    #[test]
    fn check_pin_safety_reports_no_gap_when_the_stamped_finding_already_has_the_field() {
        let dir = tempfile::tempdir().unwrap();
        let source = seed_pin_safety_fixture(dir.path(), "0.1.0", "0.2.0");
        let reports_dir = dir.path().join("reports");
        write_stamped_finding(
            &reports_dir,
            "edu",
            "0.1.0",
            r#"{"name":"Algebra I","isbn":"978-0-13-000000-0"}"#,
        );

        let reports = super::check_pin_safety(
            dir.path(),
            &source,
            &reports_dir,
            &["edu".to_string()],
            &["edu-fixture".to_string()],
        )
        .unwrap();

        assert_eq!(reports.len(), 1);
        assert_eq!(reports[0].newly_required.len(), 1);
        assert!(
            reports[0].gaps.is_empty(),
            "a finding that already carries the newly required field must not be flagged"
        );
    }

    #[test]
    fn check_pin_safety_reports_no_extra_warning_when_nothing_newly_required() {
        let dir = tempfile::tempdir().unwrap();
        // Same required list on both sides: no newly required field.
        let old_yaml = "ontology:\n  id: edu-fixture\n  version: \"0.1.0\"\n  extends: \
                         [mif-base]\nentity_types:\n  - name: title\n    schema:\n      \
                         required: [name]\n";
        fs::create_dir_all(dir.path().join("packs/ontologies/edu-fixture")).unwrap();
        fs::write(
            dir.path()
                .join("packs/ontologies/edu-fixture/edu-fixture.ontology.yaml"),
            old_yaml,
        )
        .unwrap();
        let registry = dir.path().join("registry");
        fs::create_dir_all(&registry).unwrap();
        let new_yaml = "ontology:\n  id: edu-fixture\n  version: \"0.2.0\"\n  extends: \
                         [mif-base]\nentity_types:\n  - name: title\n    schema:\n      \
                         required: [name]\n";
        let sha = sha256_of(new_yaml.as_bytes());
        let index = format!(
            r#"{{"ontologies":{{"edu-fixture":{{"version":"0.2.0","sha256":"{sha}","file":"edu-fixture.ontology.yaml","extends":["mif-base"]}}}}}}"#
        );
        fs::write(registry.join("index.json"), &index).unwrap();
        fs::write(registry.join("edu-fixture.ontology.yaml"), new_yaml).unwrap();
        let source = registry.display().to_string();
        let lock = serde_json::json!({
            "schema": "mif-ontology-lock/v1",
            "source": source,
            "index_sha256": sha256_of(index.as_bytes()),
            "ontologies": {"edu-fixture": {"version": "0.1.0", "sha256": "irrelevant"}}
        });
        fs::write(
            dir.path().join("ontologies.lock.json"),
            serde_json::to_string(&lock).unwrap(),
        )
        .unwrap();

        let reports = super::check_pin_safety(
            dir.path(),
            &source,
            &dir.path().join("reports"),
            &[],
            &["edu-fixture".to_string()],
        )
        .unwrap();

        assert_eq!(reports.len(), 1);
        assert!(reports[0].newly_required.is_empty());
        assert!(reports[0].gaps.is_empty());
    }

    #[test]
    fn check_pin_safety_reports_empty_analysis_when_the_vendored_pack_is_missing_from_disk() {
        let dir = tempfile::tempdir().unwrap();
        let source = seed_pin_safety_fixture(dir.path(), "0.1.0", "0.2.0");
        fs::remove_file(
            dir.path()
                .join("packs/ontologies/edu-fixture/edu-fixture.ontology.yaml"),
        )
        .unwrap();

        let reports = super::check_pin_safety(
            dir.path(),
            &source,
            &dir.path().join("reports"),
            &[],
            &["edu-fixture".to_string()],
        )
        .unwrap();

        assert_eq!(reports.len(), 1);
        assert_eq!(reports[0].locked_version, "0.1.0");
        assert_eq!(reports[0].registry_version, "0.2.0");
        assert!(!reports[0].analyzed);
        assert!(reports[0].newly_required.is_empty());
        assert!(reports[0].gaps.is_empty());
    }

    #[test]
    fn check_pin_safety_reports_an_unrelated_ontology_id_error_for_an_unregistered_id() {
        let dir = tempfile::tempdir().unwrap();
        let source = seed_pin_safety_fixture(dir.path(), "0.1.0", "0.2.0");
        // Pin an id the registry doesn't know about.
        let mut lock: serde_json::Value = serde_json::from_str(
            &fs::read_to_string(dir.path().join("ontologies.lock.json")).unwrap(),
        )
        .unwrap();
        lock["ontologies"]["ghost-ontology"] =
            serde_json::json!({"version": "0.1.0", "sha256": "irrelevant"});
        fs::write(
            dir.path().join("ontologies.lock.json"),
            serde_json::to_string(&lock).unwrap(),
        )
        .unwrap();

        let error = super::check_pin_safety(
            dir.path(),
            &source,
            &dir.path().join("reports"),
            &[],
            &["ghost-ontology".to_string()],
        )
        .unwrap_err();
        assert!(matches!(
            error,
            super::MifRhError::OntologyNotInRegistry { .. }
        ));
    }

    #[test]
    fn check_pin_safety_fails_closed_on_a_checksum_mismatch() {
        let dir = tempfile::tempdir().unwrap();
        let source = seed_pin_safety_fixture(dir.path(), "0.1.0", "0.2.0");
        // Corrupt the registry's declared sha256 for the drifted id's file.
        let index_path = dir.path().join("registry/index.json");
        let mut index: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&index_path).unwrap()).unwrap();
        index["ontologies"]["edu-fixture"]["sha256"] =
            serde_json::json!("0000000000000000000000000000000000000000000000000000000000000000");
        fs::write(&index_path, serde_json::to_string(&index).unwrap()).unwrap();
        // Re-write the lock's index_sha256 to match the corrupted index, so
        // this test isolates the file-checksum check from the index-pin
        // check.
        let lock_path = dir.path().join("ontologies.lock.json");
        let mut lock: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&lock_path).unwrap()).unwrap();
        lock["index_sha256"] =
            serde_json::json!(sha256_of(fs::read(&index_path).unwrap().as_slice()));
        fs::write(&lock_path, serde_json::to_string(&lock).unwrap()).unwrap();

        let error = super::check_pin_safety(
            dir.path(),
            &source,
            &dir.path().join("reports"),
            &[],
            &["edu-fixture".to_string()],
        )
        .unwrap_err();
        assert!(matches!(error, super::MifRhError::ChecksumMismatch { .. }));
    }

    #[test]
    fn check_pin_safety_fails_closed_on_a_lock_source_mismatch() {
        let dir = tempfile::tempdir().unwrap();
        let source = seed_pin_safety_fixture(dir.path(), "0.1.0", "0.2.0");
        // Point the lock at a different source than the one this call
        // checks against — its per-id pins were established there, not
        // against `source`, so they must not be trusted here.
        let lock_path = dir.path().join("ontologies.lock.json");
        let mut lock: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&lock_path).unwrap()).unwrap();
        lock["source"] = serde_json::json!("https://a-different-registry.test/ontologies");
        fs::write(&lock_path, serde_json::to_string(&lock).unwrap()).unwrap();

        let error = super::check_pin_safety(
            dir.path(),
            &source,
            &dir.path().join("reports"),
            &[],
            &["edu-fixture".to_string()],
        )
        .unwrap_err();
        assert!(matches!(
            error,
            super::MifRhError::LockSourceMismatch { .. }
        ));
    }

    #[test]
    fn check_pin_safety_fails_closed_on_a_lock_with_pins_but_no_recorded_source() {
        let dir = tempfile::tempdir().unwrap();
        let source = seed_pin_safety_fixture(dir.path(), "0.1.0", "0.2.0");
        // A legacy/hand-edited lock: pins present, source empty. Must not
        // be trusted against any caller-supplied source.
        let lock_path = dir.path().join("ontologies.lock.json");
        let mut lock: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&lock_path).unwrap()).unwrap();
        lock["source"] = serde_json::json!("");
        fs::write(&lock_path, serde_json::to_string(&lock).unwrap()).unwrap();

        let error = super::check_pin_safety(
            dir.path(),
            &source,
            &dir.path().join("reports"),
            &[],
            &["edu-fixture".to_string()],
        )
        .unwrap_err();
        assert!(matches!(
            error,
            super::MifRhError::LockSourceMismatch { .. }
        ));
    }

    #[test]
    fn check_pin_safety_short_circuits_before_any_fetch_when_nothing_is_pinned() {
        let dir = tempfile::tempdir().unwrap();
        // No ontologies.lock.json at all, and an unreachable source: if the
        // short-circuit didn't happen before fetching the registry index,
        // this would fail with a RegistryFetch error instead of returning
        // an empty result.
        let reports = super::check_pin_safety(
            dir.path(),
            "http://127.0.0.1.invalid/unreachable",
            &dir.path().join("reports"),
            &[],
            &["not-pinned-anywhere".to_string()],
        )
        .unwrap();
        assert!(reports.is_empty());
    }

    #[test]
    fn check_pin_safety_fails_closed_on_non_utf8_registry_bytes() {
        let dir = tempfile::tempdir().unwrap();
        let source = seed_pin_safety_fixture(dir.path(), "0.1.0", "0.2.0");
        // Replace the registry's (checksum-verified) file with bytes that
        // are not valid UTF-8; the index and lock are updated to match so
        // the failure isolates the UTF-8 check from the checksum check.
        let registry_file = dir.path().join("registry/edu-fixture.ontology.yaml");
        let invalid_utf8: &[u8] = &[0xff, 0xfe, 0xfd];
        fs::write(&registry_file, invalid_utf8).unwrap();
        let index_path = dir.path().join("registry/index.json");
        let mut index: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&index_path).unwrap()).unwrap();
        index["ontologies"]["edu-fixture"]["sha256"] = serde_json::json!(sha256_of(invalid_utf8));
        fs::write(&index_path, serde_json::to_string(&index).unwrap()).unwrap();
        let lock_path = dir.path().join("ontologies.lock.json");
        let mut lock: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&lock_path).unwrap()).unwrap();
        lock["index_sha256"] =
            serde_json::json!(sha256_of(fs::read(&index_path).unwrap().as_slice()));
        fs::write(&lock_path, serde_json::to_string(&lock).unwrap()).unwrap();

        let error = super::check_pin_safety(
            dir.path(),
            &source,
            &dir.path().join("reports"),
            &[],
            &["edu-fixture".to_string()],
        )
        .unwrap_err();
        assert!(matches!(
            error,
            super::MifRhError::OntologyPackNotUtf8 { .. }
        ));
    }

    #[test]
    fn check_pin_safety_rejects_an_unsafe_index_path() {
        let dir = tempfile::tempdir().unwrap();
        let source = seed_pin_safety_fixture(dir.path(), "0.1.0", "0.2.0");
        let index_path = dir.path().join("registry/index.json");
        let mut index: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&index_path).unwrap()).unwrap();
        index["ontologies"]["edu-fixture"]["file"] = serde_json::json!("../../../../etc/passwd");
        fs::write(&index_path, serde_json::to_string(&index).unwrap()).unwrap();
        let lock_path = dir.path().join("ontologies.lock.json");
        let mut lock: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&lock_path).unwrap()).unwrap();
        lock["index_sha256"] =
            serde_json::json!(sha256_of(fs::read(&index_path).unwrap().as_slice()));
        fs::write(&lock_path, serde_json::to_string(&lock).unwrap()).unwrap();

        let error = super::check_pin_safety(
            dir.path(),
            &source,
            &dir.path().join("reports"),
            &[],
            &["edu-fixture".to_string()],
        )
        .unwrap_err();
        assert!(matches!(error, super::MifRhError::UnsafeIndexPath { .. }));
    }

    #[test]
    fn check_pin_safety_refuses_a_registry_index_that_no_longer_matches_the_pinned_trust_root() {
        let dir = tempfile::tempdir().unwrap();
        let source = seed_pin_safety_fixture(dir.path(), "0.1.0", "0.2.0");
        // seed_pin_safety_fixture already pins the real index sha256;
        // corrupt it to simulate a swapped/tampered registry source.
        let lock_path = dir.path().join("ontologies.lock.json");
        let mut lock: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&lock_path).unwrap()).unwrap();
        lock["index_sha256"] = serde_json::json!("not-the-real-index-sha256");
        fs::write(&lock_path, serde_json::to_string(&lock).unwrap()).unwrap();

        let error = super::check_pin_safety(
            dir.path(),
            &source,
            &dir.path().join("reports"),
            &[],
            &["edu-fixture".to_string()],
        )
        .unwrap_err();
        assert!(matches!(error, super::MifRhError::IndexPinMismatch { .. }));
    }

    #[test]
    fn check_pin_safety_rejects_a_malformed_id_before_touching_the_filesystem() {
        let dir = tempfile::tempdir().unwrap();
        let source = seed_pin_safety_fixture(dir.path(), "0.1.0", "0.2.0");

        let error = super::check_pin_safety(
            dir.path(),
            &source,
            &dir.path().join("reports"),
            &[],
            &["../../../../etc/passwd".to_string()],
        )
        .unwrap_err();
        assert!(matches!(
            error,
            super::MifRhError::MalformedOntologyId { .. }
        ));
    }

    #[test]
    fn check_pin_safety_fails_closed_on_a_malformed_schema_required_field() {
        let dir = tempfile::tempdir().unwrap();
        let old_yaml = "ontology:\n  id: edu-fixture\n  version: \"0.1.0\"\n  extends: \
                         [mif-base]\nentity_types:\n  - name: title\n    schema:\n      \
                         required: [name]\n      properties:\n        name: {type: string}\n";
        fs::create_dir_all(dir.path().join("packs/ontologies/edu-fixture")).unwrap();
        fs::write(
            dir.path()
                .join("packs/ontologies/edu-fixture/edu-fixture.ontology.yaml"),
            old_yaml,
        )
        .unwrap();

        // `required` is present but is a string, not an array: malformed.
        let registry = dir.path().join("registry");
        fs::create_dir_all(&registry).unwrap();
        let new_yaml = "ontology:\n  id: edu-fixture\n  version: \"0.2.0\"\n  extends: \
                         [mif-base]\nentity_types:\n  - name: title\n    schema:\n      \
                         required: \"name\"\n      properties:\n        name: {type: string}\n";
        let sha = sha256_of(new_yaml.as_bytes());
        let index = format!(
            r#"{{"ontologies":{{"edu-fixture":{{"version":"0.2.0","sha256":"{sha}","file":"edu-fixture.ontology.yaml","extends":["mif-base"]}}}}}}"#
        );
        fs::write(registry.join("index.json"), &index).unwrap();
        fs::write(registry.join("edu-fixture.ontology.yaml"), new_yaml).unwrap();

        let source = registry.display().to_string();
        let lock = serde_json::json!({
            "schema": "mif-ontology-lock/v1",
            "source": source,
            "index_sha256": sha256_of(index.as_bytes()),
            "ontologies": {
                "edu-fixture": {"version": "0.1.0", "sha256": "irrelevant-for-this-test"}
            }
        });
        fs::write(
            dir.path().join("ontologies.lock.json"),
            serde_json::to_string(&lock).unwrap(),
        )
        .unwrap();

        let error = super::check_pin_safety(
            dir.path(),
            &source,
            &dir.path().join("reports"),
            &[],
            &["edu-fixture".to_string()],
        )
        .unwrap_err();
        assert!(matches!(
            error,
            super::MifRhError::EntityTypeSchemaInvalid { .. }
        ));
    }
}