llmman 0.1.689

Run any agent on any model, models stored as OCI images
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
//! Signature trust policy: which references must be signed, and by whom.
//!
//! The cryptography lives in the Go shim (go-shim/sigstore.go, reached
//! through [`crate::ffi::verify`]); this module decides *when* to invoke
//! it and *what a negative answer means*, which is a policy question.
//!
//! Policy comes from the `[verify]` section of `llmman.conf` (see
//! [`crate::config`] for the locations), merged with later files
//! winning:
//!
//! ```toml
//! [verify]
//! default = "off"                  # for references no rule matches
//!
//! [[verify.trust]]
//! pattern = "docker.io/myorg/**"
//! keys    = ["keys/myorg.pub"]     # relative to this file's directory
//! mode    = "enforce"              # off | warn | enforce
//! ```
//!
//! The default is `off`, not `warn`, on purpose: with no configured trust
//! roots there is nothing any model could be checked against, so `warn`
//! would print an unsigned-model warning on every pull that the user can
//! do nothing about — a reliable way to teach people to ignore the one
//! warning that eventually matters.
//!
//! `LLMMAN_VERIFY=off|warn|enforce` overrides the mode (not the keys) for
//! every reference, which is the useful knob in CI.

use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::sync::OnceLock;

use anyhow::{anyhow, bail, Context};

use crate::config::VerifyConf;
use crate::storage::oci::{Descriptor, OciStore};

/// Longest accepted `pattern`. Nothing real comes close; the bound just
/// keeps a pathological one uninteresting.
const MAX_PATTERN_LEN: usize = 256;

/// What a reference that does not verify should cause.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Mode {
    /// Don't check at all.
    #[default]
    Off,
    /// Check, report the outcome, proceed either way.
    Warn,
    /// Check, and refuse to proceed unless a trusted key signed it.
    Enforce,
}

impl Mode {
    fn parse(s: &str) -> anyhow::Result<Self> {
        match s.trim().to_ascii_lowercase().as_str() {
            "off" | "false" | "0" | "no" => Ok(Mode::Off),
            "warn" | "warning" => Ok(Mode::Warn),
            "enforce" | "require" | "required" | "true" | "1" | "yes" => Ok(Mode::Enforce),
            other => Err(anyhow!(
                "unknown verification mode {other:?} (expected \"off\", \"warn\", or \"enforce\")"
            )),
        }
    }
}

impl std::fmt::Display for Mode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            Mode::Off => "off",
            Mode::Warn => "warn",
            Mode::Enforce => "enforce",
        })
    }
}

// ---------------------------------------------------------------------------
// Config
// ---------------------------------------------------------------------------

#[derive(Debug, Clone)]
struct Rule {
    pattern: String,
    keys: Vec<PathBuf>,
    mode: Mode,
    /// Glob over the signature's claimed repository; `None` keeps the
    /// shim's exact equality with the pulled repository.
    claims: Option<String>,
    /// Keyless: the OIDC issuer the signing certificate must name.
    issuer: Option<String>,
    /// Keyless: the SAN the signing certificate must carry.
    subject: Option<String>,
}

#[derive(Debug, Default)]
pub struct Policy {
    /// `None` where no file set `default`. Kept optional so an explicit
    /// `default = "off"` in a higher-priority file can override a lower
    /// one's `warn`, which "later files win" requires and a plain
    /// `Mode::Off` could not express.
    default_mode: Option<Mode>,
    /// `LLMMAN_VERIFY`, overriding every rule's mode. Resolved once at
    /// load, so an unparsable value fails there rather than being
    /// re-reported (or worse, ignored) on every decision.
    forced_mode: Option<Mode>,
    /// `layers`: whether a cold load re-hashes the stored manifest and
    /// layer blobs. Optional for the same "later files win" reason as
    /// `default_mode`. The signature rules never set it: a signed model
    /// whose blob was overwritten on disk is exactly what this catches.
    layers_mode: Option<Mode>,
    /// `LLMMAN_VERIFY_LAYERS`, overriding `layers_mode`; resolved once
    /// at load like `forced_mode`.
    forced_layers_mode: Option<Mode>,
    /// Ascending precedence: the *last* matching rule wins.
    rules: Vec<Rule>,
    /// `trusted_root`: the Sigstore TrustedRoot keyless rules are
    /// checked against, already resolved against its file's directory.
    /// Optional for the same "later files win" reason as `default_mode`.
    trusted_root: Option<PathBuf>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Decision {
    pub mode: Mode,
    pub keys: Vec<PathBuf>,
    pub claims: Option<String>,
    /// Keyless identity, exclusive with `keys`: a rule names one or the
    /// other.
    pub issuer: Option<String>,
    pub subject: Option<String>,
    /// Policy-wide, copied into every decision so the verifier has it
    /// next to the identity it applies to.
    pub trusted_root: Option<PathBuf>,
}

impl Decision {
    /// A keyless rule: at least one of `issuer` and `subject` is set.
    pub fn has_identity(&self) -> bool {
        self.issuer.is_some() || self.subject.is_some()
    }

    /// The keyless rule as the shim reads it, or `None` for a key-only
    /// decision.
    pub fn identity(&self) -> Option<crate::ffi::Identity> {
        self.has_identity().then(|| crate::ffi::Identity {
            issuer: self.issuer.clone(),
            subject: self.subject.clone(),
            trusted_root: self
                .trusted_root
                .as_ref()
                .map(|p| p.to_string_lossy().into_owned()),
        })
    }
}

impl Policy {
    /// One file's `[verify]` section. `dir` is that file's own
    /// directory, which relative key paths resolve against, so a policy
    /// can ship with its keys.
    fn from_conf(conf: &VerifyConf, dir: &Path) -> anyhow::Result<Self> {
        let default_mode = conf
            .default
            .as_deref()
            .map(|s| Mode::parse(s).context("`default`"))
            .transpose()?;
        let layers_mode = conf
            .layers
            .as_deref()
            .map(|s| Mode::parse(s).context("`layers`"))
            .transpose()?;

        let mut rules = Vec::with_capacity(conf.trust.len());
        for entry in &conf.trust {
            validate_pattern("pattern", &entry.pattern)?;
            if let Some(claims) = &entry.claims {
                validate_pattern("claims", claims)?;
            }
            validate_identity(entry)?;
            let mode = match entry.mode.as_deref() {
                Some(s) => Mode::parse(s)
                    .with_context(|| format!("[[verify.trust]] pattern {:?}", entry.pattern))?,
                // Keys but no mode means "check this, tell me, don't
                // break my build".
                None => Mode::Warn,
            };
            rules.push(Rule {
                pattern: entry.pattern.clone(),
                keys: entry
                    .keys
                    .iter()
                    .map(|k| resolve_key_path(k, dir))
                    .collect(),
                mode,
                claims: entry.claims.clone(),
                issuer: entry.issuer.clone(),
                subject: entry.subject.clone(),
            });
        }
        Ok(Policy {
            default_mode,
            forced_mode: None,
            layers_mode,
            forced_layers_mode: None,
            rules,
            trusted_root: conf
                .trusted_root
                .as_deref()
                .map(|raw| resolve_key_path(raw, dir)),
        })
    }

    /// A `[verify]` section on its own, for tests that care about the
    /// policy and not the file it came from.
    #[cfg(test)]
    fn parse(text: &str, dir: &Path) -> anyhow::Result<Self> {
        Self::from_conf(&toml::from_str(text).context("parse TOML")?, dir)
    }

    /// The mode and trusted keys that apply to `reference`.
    ///
    /// Only the winning rule's keys are used; they are not accumulated
    /// across matching rules. A narrower rule must be able to *replace*
    /// a broad one's key set, or "trust the org key everywhere except
    /// here" would be inexpressible.
    pub fn decide(&self, reference: &str) -> Decision {
        let repo = canonical_repository(repository_of(reference));
        let winner = self
            .rules
            .iter()
            .rev()
            .find(|rule| glob_match(&rule.pattern, &repo));
        let mut decision = match winner {
            Some(rule) => Decision {
                mode: rule.mode,
                keys: rule.keys.clone(),
                claims: rule.claims.clone(),
                issuer: rule.issuer.clone(),
                subject: rule.subject.clone(),
                trusted_root: self.trusted_root.clone(),
            },
            None => Decision {
                mode: self.default_mode.unwrap_or_default(),
                keys: Vec::new(),
                claims: None,
                issuer: None,
                subject: None,
                trusted_root: self.trusted_root.clone(),
            },
        };
        if let Some(forced) = self.forced_mode {
            decision.mode = forced;
        }
        decision
    }

    /// Fold a later (higher-priority) file into this one: its explicit
    /// `default`, `layers` and `trusted_root` win, its rules append after ours so they
    /// take precedence in [`Policy::decide`]. The environment overrides
    /// are not file state and are left untouched.
    fn merge(&mut self, later: Policy) {
        if let Some(mode) = later.default_mode {
            self.default_mode = Some(mode);
        }
        if let Some(mode) = later.layers_mode {
            self.layers_mode = Some(mode);
        }
        if let Some(root) = later.trusted_root {
            self.trusted_root = Some(root);
        }
        self.rules.extend(later.rules);
    }

    /// The layer-hashing mode: `LLMMAN_VERIFY_LAYERS`, else the merged
    /// `[verify] layers`, else `Off`.
    fn layers_mode(&self) -> Mode {
        self.forced_layers_mode
            .or(self.layers_mode)
            .unwrap_or_default()
    }

    /// Load and merge every config file present, cached for the process.
    ///
    /// A file that cannot be read or parsed is fatal here, not skipped:
    /// it could be the one demanding enforcement, and treating it like
    /// an absent file would silently downgrade to `off`. This is why
    /// [`crate::config::files`] reports a parse failure rather than
    /// deciding what it means — the other two readers of that file can
    /// carry on without it, and this one cannot.
    pub fn load() -> anyhow::Result<&'static Policy> {
        static CACHE: OnceLock<Result<Policy, String>> = OnceLock::new();
        CACHE
            .get_or_init(|| load_uncached().map_err(|e| format!("{e:#}")))
            .as_ref()
            .map_err(|e| anyhow!("{e}"))
    }
}

fn load_uncached() -> anyhow::Result<Policy> {
    let mut merged = Policy::default();
    for file in crate::config::files().map_err(|e| anyhow!("{e}"))? {
        let policy = Policy::from_conf(&file.conf.verify, &file.dir)
            .with_context(|| format!("read {}", file.path.display()))?;
        merged.merge(policy);
    }
    merged.forced_mode = mode_override()?;
    merged.forced_layers_mode = layers_mode_override()?;
    Ok(merged)
}

/// The mode for hashing stored layers on a cold load: `LLMMAN_VERIFY_LAYERS`,
/// else the merged `[verify] layers`, else `Off`. The signature
/// `default` and trust rules play no part.
pub fn layers_mode() -> anyhow::Result<Mode> {
    Ok(Policy::load()?.layers_mode())
}

/// Rejects a glob that cannot match anything. `field` names the
/// `[[verify.trust]]` key being checked (`pattern` or `claims`) so the
/// error points at the right line. Globs are compared against the
/// repository, so one carrying a tag or digest would simply never fire,
/// silently leaving a rule the author believes is enforcing something.
/// Same reasoning as rejecting an unknown `mode`.
fn validate_pattern(field: &str, pattern: &str) -> anyhow::Result<()> {
    if pattern.is_empty() {
        bail!("a [[verify.trust]] entry has an empty `{field}`");
    }
    if pattern.len() > MAX_PATTERN_LEN {
        bail!("[[verify.trust]] {field} is longer than {MAX_PATTERN_LEN} bytes: {pattern:?}");
    }
    if pattern.contains('@') {
        bail!(
            "[[verify.trust]] {field} {pattern:?} contains a digest; globs match the \
             repository only"
        );
    }
    let last = pattern.rsplit('/').next().unwrap_or(pattern);
    if last.contains(':') {
        bail!(
            "[[verify.trust]] {field} {pattern:?} contains a tag; globs match the \
             repository only"
        );
    }
    Ok(())
}

/// A rule trusts either public keys or a keyless identity (`issuer`,
/// `subject`), never both: the two are checked by different machinery
/// and a rule naming both would leave open whether one match suffices
/// or both are required. Refused at load, like an unknown `mode`, so
/// the author learns of it before a pull is let through.
fn validate_identity(entry: &crate::config::TrustConf) -> anyhow::Result<()> {
    let has_identity = entry.issuer.is_some() || entry.subject.is_some();
    if has_identity && !entry.keys.is_empty() {
        bail!(
            "[[verify.trust]] pattern {:?} sets both `keys` and an identity (`issuer` or \
             `subject`); a rule trusts one or the other",
            entry.pattern
        );
    }
    for (field, value) in [("issuer", &entry.issuer), ("subject", &entry.subject)] {
        if value.as_deref().is_some_and(|v| v.trim().is_empty()) {
            bail!(
                "[[verify.trust]] pattern {:?} has an empty `{field}`",
                entry.pattern
            );
        }
    }
    // One half alone is too wide: an issuer without a subject accepts
    // every identity that issuer signs for, a subject without an issuer
    // accepts the same name from any issuer.
    if has_identity && (entry.issuer.is_none() || entry.subject.is_none()) {
        bail!(
            "[[verify.trust]] pattern {:?} names an identity: both `issuer` and `subject` \
             are required",
            entry.pattern
        );
    }
    Ok(())
}

/// `LLMMAN_VERIFY`, if set.
///
/// An unparsable value is an error, not a warning. Most of the time this
/// is read inside the daemon, whose stderr is a log file — so warning
/// and carrying on would turn `LLMMAN_VERIFY=enfroce` into a silent
/// downgrade to whatever the config files happened to say, which is the
/// exact failure this whole module refuses elsewhere.
fn mode_override() -> anyhow::Result<Option<Mode>> {
    parse_override(
        std::env::var("LLMMAN_VERIFY").ok().as_deref(),
        "LLMMAN_VERIFY",
    )
}

/// `LLMMAN_VERIFY_LAYERS`, if set. Same reasoning as [`mode_override`]:
/// a typo must fail the load, not downgrade it.
fn layers_mode_override() -> anyhow::Result<Option<Mode>> {
    parse_override(
        std::env::var("LLMMAN_VERIFY_LAYERS").ok().as_deref(),
        "LLMMAN_VERIFY_LAYERS",
    )
}

/// Split out so it is testable without touching the process environment;
/// `var_name` names the variable in the error.
fn parse_override(raw: Option<&str>, var_name: &'static str) -> anyhow::Result<Option<Mode>> {
    match raw.map(str::trim) {
        None | Some("") => Ok(None),
        Some(raw) => Mode::parse(raw).map(Some).context(var_name),
    }
}

/// Expand `~` and make a relative key path absolute against the config
/// file's own directory.
fn resolve_key_path(raw: &str, dir: &Path) -> PathBuf {
    let trimmed = raw.trim();
    if let Some(rest) = trimmed.strip_prefix("~/") {
        if let Some(home) = dirs::home_dir() {
            return home.join(rest);
        }
    }
    let path = PathBuf::from(trimmed);
    if path.is_absolute() {
        path
    } else {
        dir.join(path)
    }
}

// ---------------------------------------------------------------------------
// Reference matching
// ---------------------------------------------------------------------------

/// The repository part of a reference: host/namespace/name, tag and
/// digest removed. Patterns match this, because a trust rule is about
/// who publishes a model and tags move.
///
/// Mirrors `repositoryOf` in go-shim/sigstore.go, including the rule that
/// a ":" only starts a tag if it comes after the last "/".
fn repository_of(reference: &str) -> &str {
    let mut r = reference;
    if let Some(i) = r.rfind('@') {
        r = &r[..i];
    }
    let last_slash = r.rfind('/').map(|i| i as isize).unwrap_or(-1);
    if let Some(i) = r.rfind(':') {
        if (i as isize) > last_slash {
            r = &r[..i];
        }
    }
    r
}

/// Normalizes a repository to its one fully-qualified spelling, so every
/// way of writing a Docker Hub reference matches the same pattern:
/// `gemma4`, `ai/gemma4`, `docker.io/ai/gemma4` and
/// `index.docker.io/ai/gemma4` all become `docker.io/...`.
///
/// Applied to the *reference* only, never to a pattern. Normalizing a
/// pattern would rewrite what the author wrote: `docker.io/**` would lose
/// its host and become `**`, quietly widening a Docker Hub rule to every
/// registry — so a `mode = "off"` rule for Hub would disable verification
/// everywhere, and an `enforce` one would demand the Hub key from quay.io.
///
/// This normalizes *up* where go-shim/sigstore.go's `canonicalRepository`
/// folds *down*. They are doing different jobs — this one produces a
/// string to glob against, that one compares two references for equality
/// — and each is internally consistent, which is all either needs. What
/// they must agree on is which spellings are the same repository, and
/// `canonical_repository_agrees_with_the_go_side_on_what_is_equal` pins
/// that.
fn canonical_repository(repo: &str) -> String {
    for alias in ["index.docker.io", "registry-1.docker.io"] {
        if repo == alias {
            return "docker.io".to_string();
        }
        if let Some(rest) = repo.strip_prefix(&format!("{alias}/")) {
            return canonical_repository(&format!("docker.io/{rest}"));
        }
    }
    let (first, rest) = match repo.split_once('/') {
        Some((first, rest)) => (first, Some(rest)),
        None => (repo, None),
    };
    // Docker's own rule for telling a registry host from a namespace.
    let has_host = first.contains('.') || first.contains(':') || first == "localhost";
    if !has_host {
        return match rest {
            Some(rest) => format!("docker.io/{first}/{rest}"),
            None => format!("docker.io/library/{first}"),
        };
    }
    match repo.strip_prefix("docker.io/") {
        // An official image: "docker.io/ubuntu" is "docker.io/library/ubuntu".
        Some(path) if !path.contains('/') => format!("docker.io/library/{path}"),
        _ => repo.to_string(),
    }
}

/// Glob match where `*` stays within one path segment and `**` crosses
/// them — the semantics of `.gitignore` and shell globstar. So
/// `docker.io/org/*` matches `docker.io/org/model` but not
/// `docker.io/org/team/model`, and `docker.io/org/**` matches both.
///
/// Byte-wise and case-sensitive; registry references are lowercase by
/// grammar (`shortnames::parse_registry_ref` enforces it).
fn glob_match(pattern: &str, text: &str) -> bool {
    // Memoized on (pattern index, text index). Without it, a pattern of
    // repeated `*a` branches is exponential in its length, so a valid
    // config file could stall every policy decision.
    let mut seen = HashSet::new();
    glob_bytes(pattern.as_bytes(), text.as_bytes(), &mut seen)
}

fn glob_bytes(p: &[u8], t: &[u8], seen: &mut HashSet<(usize, usize)>) -> bool {
    if !seen.insert((p.len(), t.len())) {
        return false; // this state already failed
    }
    match p.first() {
        None => t.is_empty(),
        Some(b'*') => {
            if p.get(1) == Some(&b'*') {
                let rest = &p[2..];
                (0..=t.len()).any(|i| glob_bytes(rest, &t[i..], seen))
            } else {
                let rest = &p[1..];
                for i in 0..=t.len() {
                    if glob_bytes(rest, &t[i..], seen) {
                        return true;
                    }
                    // Having tried leaving the separator to the rest of
                    // the pattern, a single `*` may not swallow it.
                    if t.get(i) == Some(&b'/') {
                        break;
                    }
                }
                false
            }
        }
        Some(&c) => match t.first() {
            Some(&d) if c == d => glob_bytes(&p[1..], &t[1..], seen),
            _ => false,
        },
    }
}

// ---------------------------------------------------------------------------
// Applying the policy
// ---------------------------------------------------------------------------

/// What actually happened when a reference was checked.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Outcome {
    /// Policy said `off`; nothing was checked.
    Skipped,
    /// A trusted key signed this exact manifest.
    Verified,
    /// Checked and not verified, under `warn`. The caller proceeds and
    /// must surface the accompanying notice.
    Unverified,
}

/// A completed check, plus what the user needs to be told about it.
///
/// Notices are returned rather than printed: `cmd::transfer` can print,
/// but a pull runs inside the daemon, whose stderr is a log file nobody
/// watches, so its notices are relayed over the progress stream instead.
#[derive(Debug, Clone)]
#[must_use = "a verification notice that is never surfaced is not a warning"]
pub struct Verdict {
    pub outcome: Outcome,
    pub notices: Vec<String>,
    /// The mode this verdict was reached under, so [`Verdict::escalate`]
    /// can treat a caller's own follow-up failure the way the policy
    /// would. Private: a caller that could set it to `Off` could silence
    /// its own escalation.
    mode: Mode,
    /// The digest a trusted key actually signed. `Some` only when
    /// `outcome` is `Verified`, and it comes from the verifier's own
    /// report rather than from a separate resolve — so a caller
    /// comparing against it is comparing against what was checked.
    pub digest: Option<String>,
}

impl Verdict {
    fn new(outcome: Outcome, mode: Mode) -> Self {
        Self {
            outcome,
            notices: Vec::new(),
            mode,
            digest: None,
        }
    }

    fn with(outcome: Outcome, mode: Mode, notice: String) -> Self {
        Self {
            outcome,
            notices: vec![notice],
            mode,
            digest: None,
        }
    }

    /// Reports `msg` the way `mode` says to: fatal under `enforce`,
    /// a notice under `warn`, silent when nothing was being checked.
    pub fn escalate(&mut self, msg: String) -> anyhow::Result<()> {
        match self.mode {
            Mode::Enforce => bail!("{msg}"),
            Mode::Warn => {
                self.notices.push(format!("warning: {msg}"));
                Ok(())
            }
            Mode::Off => Ok(()),
        }
    }

    /// Prints every notice, for a caller that is the foreground process.
    pub fn report(&self) {
        for notice in &self.notices {
            eprintln!("[llmman] {notice}");
        }
    }
}

/// A refusal that says nothing about the model itself — the registry was
/// unreachable, a key unreadable, no keys configured. Under `enforce` it
/// still refuses, but a caller holding a local copy must not treat it as
/// a verdict *against* that copy.
///
/// Wraps the real cause and displays it verbatim, so tagging an error
/// this way adds a marker without adding words to what the user reads.
#[derive(Debug)]
pub struct Indeterminate(anyhow::Error);

impl std::fmt::Display for Indeterminate {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:#}", self.0)
    }
}

impl std::error::Error for Indeterminate {}

/// Whether `err` from [`check`] means "could not find out" rather than
/// "definitely not signed by a trusted key".
pub fn is_indeterminate(err: &anyhow::Error) -> bool {
    err.chain().any(|e| e.is::<Indeterminate>())
}

/// Runs the policy for `reference` at `digest`.
///
/// `digest` is the manifest digest to check, or `None` to let the shim
/// resolve it. Pass it when known: it saves a round trip and closes the
/// window in which a tag could move between verification and use.
///
/// Returns `Err` only when the policy says the caller must not proceed.
/// "Unsigned" and "signed by a stranger" are ordinary outcomes under
/// `warn`, reported rather than raised.
pub fn check(reference: &str, digest: Option<&str>) -> anyhow::Result<Verdict> {
    // A policy that won't load is indeterminate, not a verdict: a
    // malformed config file says nothing about the model, and a caller
    // holding a local copy must not delete it over one.
    let policy = Policy::load().map_err(|e| anyhow!(Indeterminate(e)))?;
    check_with(reference, digest, &policy.decide(reference))
}

fn check_with(
    reference: &str,
    digest: Option<&str>,
    decision: &Decision,
) -> anyhow::Result<Verdict> {
    if decision.mode == Mode::Off {
        return Ok(Verdict::new(Outcome::Skipped, decision.mode));
    }
    if decision.keys.is_empty() && !decision.has_identity() {
        // `LLMMAN_VERIFY=warn|enforce` with no policy file, or a rule
        // naming no keys. Nothing to check against.
        let msg = format!(
            "verification is set to {} for {reference}, but no trusted public keys are configured for it \
             (add a [[verify.trust]] rule with `keys` to llmman.conf)",
            decision.mode
        );
        if decision.mode == Mode::Enforce {
            return Err(anyhow!(Indeterminate(anyhow!(msg))));
        }
        return Ok(Verdict::with(
            Outcome::Unverified,
            decision.mode,
            format!("warning: {msg}"),
        ));
    }
    if decision.has_identity() && decision.trusted_root.is_none() {
        // A keyless rule with nothing to anchor the certificate chain or
        // the log entry to. This is a broken configuration, not an
        // unsigned model, so it is never reported as "not signed".
        let msg = format!(
            "cannot verify {reference}: the matching [[verify.trust]] rule names an identity \
             but no `trusted_root` is configured under [verify]"
        );
        if decision.mode == Mode::Enforce {
            return Err(anyhow!(Indeterminate(anyhow!(msg))));
        }
        return Ok(Verdict::with(
            Outcome::Unverified,
            decision.mode,
            format!("warning: {msg}"),
        ));
    }

    let keys: Vec<String> = decision
        .keys
        .iter()
        .map(|p| p.to_string_lossy().into_owned())
        .collect();

    // An unset `claims` crosses the FFI as the empty string, the same
    // convention `digest` uses; the shim then requires identity equality.
    let report = match crate::ffi::verify(
        reference,
        digest.unwrap_or(""),
        &keys,
        decision.claims.as_deref().unwrap_or(""),
        decision.identity().as_ref(),
    ) {
        Ok(report) => report,
        // No answer could be reached (unreadable key, unreachable
        // registry). Under `enforce` that is indistinguishable from
        // "not verified" and must fail closed.
        Err(e) => {
            if decision.mode == Mode::Enforce {
                return Err(anyhow!(Indeterminate(e)).context(format!("cannot verify {reference}")));
            }
            return Ok(Verdict::with(
                Outcome::Unverified,
                decision.mode,
                format!("warning: cannot verify {reference}: {e:#}"),
            ));
        }
    };

    if report.verified {
        let signers: Vec<&str> = report.matches.iter().map(|m| m.signer.as_str()).collect();
        let mut verdict = Verdict::with(
            Outcome::Verified,
            decision.mode,
            format!(
                "verified signature on {reference} ({}) with {}",
                short_digest(&report.digest),
                signers.join(", ")
            ),
        );
        verdict.digest = Some(report.digest);
        return Ok(verdict);
    }

    let detail = if report.reason.is_empty() {
        "no trusted key accepted a signature for it".to_string()
    } else {
        report.reason.clone()
    };
    let msg = format!(
        "{reference} ({}) is not signed by a trusted key: {detail}",
        short_digest(&report.digest)
    );
    if decision.mode == Mode::Enforce {
        bail!("{msg}");
    }
    Ok(Verdict::with(
        Outcome::Unverified,
        decision.mode,
        format!("warning: {msg}"),
    ))
}

/// `reference` rewritten to name `digest` explicitly, so a subsequent
/// resolve cannot land on anything else.
///
/// Verification resolves a tag to decide whether to trust it; whatever
/// acts on that decision has to be pinned to the same manifest, or the
/// tag can move in between and the check applies to bytes nobody used.
pub fn pin_to_digest(reference: &str, digest: &str) -> String {
    format!("{}@{digest}", repository_of(reference))
}

/// Whether `reference` names an OCI registry, decided without touching
/// the network.
///
/// `crate::hf::classify` answers this better — but only by probing
/// `/v2/`, and a probe that *fails* classifies a real registry reference
/// as HuggingFace. Deciding policy on that would take a stored model out
/// of the policy's reach exactly when the network is being interfered
/// with, which is fail-open. This errs the other way: anything not
/// recognizably one of the non-registry forms is treated as a registry,
/// so an unknown host gets checked rather than waved through.
pub fn is_registry_reference(reference: &str) -> bool {
    if crate::sources::handles(reference) {
        return false;
    }
    if ["hf://", "huggingface://"]
        .iter()
        .any(|p| reference.starts_with(p))
    {
        return false;
    }
    let host = reference.split('/').next().unwrap_or(reference);
    !crate::hf::is_known_hf_host(host)
}

/// Whether any checking would happen for `reference` at all. Lets a
/// caller skip work on the overwhelmingly common no-policy path without
/// duplicating the decision.
pub fn is_enabled_for(reference: &str) -> anyhow::Result<bool> {
    Ok(Policy::load()?.decide(reference).mode != Mode::Off)
}

/// First 12 hex characters of a digest, matching the Go shim's output.
fn short_digest(digest: &str) -> String {
    let hex = digest.split_once(':').map(|(_, h)| h).unwrap_or(digest);
    hex.chars().take(12).collect()
}

/// Whether two digest strings name the same content. Case-insensitive,
/// matching `OciStore::find`, so a spelling difference between the
/// registry's answer and the store's cannot look like a moved tag.
fn digests_match(a: &str, b: &str) -> bool {
    a.eq_ignore_ascii_case(b)
}

/// Why the stored blob for `desc` does not hash to the digest it
/// records, or `None` when it does. A malformed digest (`blob_path`
/// refuses anything but `sha256:` and 64 hex digits) or an unreadable
/// blob is a mismatch with its cause kept, not an error: the descriptor
/// came from a blob this check exists to distrust.
///
/// Only a regular file is hashed. The store writes nothing else under
/// `blobs/`, so a symlink (to `/dev/zero`, to another blob), a
/// directory, a FIFO or a device there is a replacement, reported as
/// such rather than followed or read: a symlink to a blob with the
/// right bytes would hash clean while the backend opens whatever it
/// points at by then, and a FIFO or `/dev/zero` would block the load.
/// The blob is opened first and the file type read from the open
/// handle, so there is no window between a check by path and the
/// open; `open_blob` refuses to follow a symlink on unix (`O_NOFOLLOW`)
/// and does not block on a FIFO (`O_NONBLOCK`), and on Windows opens
/// the reparse point itself (`FILE_FLAG_OPEN_REPARSE_POINT`), whose
/// handle reports a symlink and is refused here. On any other platform
/// the open follows a symlink, and the handle's type is the target's,
/// so a symlink to a regular file hashes as that file there.
fn blob_mismatch(store: &OciStore, desc: &Descriptor) -> Option<String> {
    let path = match store.blob_path(&desc.digest) {
        Ok(path) => path,
        Err(e) => return Some(format!("{e:#}")),
    };
    let mut file = match open_blob(&path) {
        Ok(file) => file,
        // Refused already; the stat only names the cause for the
        // notice (a symlink is `ELOOP`, a directory on Windows a
        // permission error) and is not what decided it.
        Err(e) => {
            return Some(match std::fs::symlink_metadata(&path) {
                Ok(meta) if !meta.file_type().is_file() => {
                    format!("{} is not a regular file: {e}", path.display())
                }
                _ => format!("open {}: {e}", path.display()),
            })
        }
    };
    match file.metadata() {
        Ok(meta) if meta.file_type().is_file() => {}
        Ok(_) => return Some(format!("{} is not a regular file", path.display())),
        Err(e) => return Some(format!("stat {}: {e}", path.display())),
    }
    match crate::fsutil::sha256_of_reader(&mut file) {
        Ok(hex) if digests_match(&format!("sha256:{hex}"), &desc.digest) => None,
        Ok(hex) => Some(format!("hashes to sha256:{hex}")),
        Err(e) => Some(format!("hash {}: {e}", path.display())),
    }
}

/// Opens a stored blob for hashing. On unix the final path component
/// is not followed if it is a symlink (`open(2)` fails with `ELOOP`)
/// and a FIFO opens at once instead of waiting for a writer; the
/// `O_NONBLOCK` is a no-op for reads from a regular file. The caller
/// checks the handle's file type before reading, so a FIFO is reported
/// rather than read.
#[cfg(unix)]
fn open_blob(path: &Path) -> std::io::Result<std::fs::File> {
    use std::os::unix::fs::OpenOptionsExt as _;
    std::fs::OpenOptions::new()
        .read(true)
        .custom_flags(libc::O_NOFOLLOW | libc::O_NONBLOCK)
        .open(path)
}

/// On Windows the final path component is opened as itself when it is
/// a reparse point (a symlink or junction), so the handle's metadata is
/// the link's rather than the target's and the caller's file type check
/// refuses it. A symlink to a directory opens too and is refused the
/// same way.
#[cfg(windows)]
fn open_blob(path: &Path) -> std::io::Result<std::fs::File> {
    use std::os::windows::fs::OpenOptionsExt as _;
    // `FILE_FLAG_OPEN_REPARSE_POINT` from `CreateFileW`; spelled here so
    // the crate does not depend on `windows-sys` for one constant.
    const FILE_FLAG_OPEN_REPARSE_POINT: u32 = 0x0020_0000;
    std::fs::OpenOptions::new()
        .read(true)
        .custom_flags(FILE_FLAG_OPEN_REPARSE_POINT)
        .open(path)
}

#[cfg(not(any(unix, windows)))]
fn open_blob(path: &Path) -> std::io::Result<std::fs::File> {
    std::fs::File::open(path)
}

// ---------------------------------------------------------------------------
// Pull-time enforcement
// ---------------------------------------------------------------------------

/// Enforces the policy across a pull, in two halves: [`check`] before any
/// layer is fetched, so a rejected model costs one manifest lookup rather
/// than a multi-gigabyte download, and [`confirm`] afterwards, so a tag
/// repointed in between cannot slip past. Neither is useful alone.
///
/// [`check`]: PullGuard::check
/// [`confirm`]: PullGuard::confirm
#[must_use = "a checked pull must be confirmed against the digest it actually stored"]
pub struct PullGuard {
    reference: String,
    mode: Mode,
    /// The digest a trusted key signed, straight from the verifier's
    /// report. `None` means nothing was verified, and `confirm` has
    /// nothing to compare against.
    verified_digest: Option<String>,
    notices: Vec<String>,
}

impl PullGuard {
    /// Runs the policy before the pull starts. `Err` if it forbids
    /// proceeding.
    pub fn check(reference: &str) -> anyhow::Result<Self> {
        let decision = Policy::load()?.decide(reference);
        let mode = decision.mode;
        if mode == Mode::Off {
            return Ok(Self {
                reference: reference.to_owned(),
                mode,
                verified_digest: None,
                notices: Vec::new(),
            });
        }
        // Resolving here only saves the shim a round trip, so its failure
        // is not fatal (the verdict carries the digest either way) unless
        // the registry says there is nothing to verify.
        let resolved = match crate::ffi::resolved_digest_of(reference) {
            Err(e) if crate::storage::oci::is_source_not_found(&e) => return Err(e),
            resolved => resolved.ok(),
        };
        let verdict = check_with(reference, resolved.as_deref(), &decision)?;

        // Under `enforce`, a pass with nothing to confirm against would
        // silently drop the second half of the guard.
        if mode == Mode::Enforce && verdict.outcome == Outcome::Verified && verdict.digest.is_none()
        {
            bail!("{reference} verified but reported no digest to confirm against");
        }
        Ok(Self {
            reference: reference.to_owned(),
            mode,
            verified_digest: verdict.digest,
            notices: verdict.notices,
        })
    }

    /// Confirms `stored` — the digest the pull landed on — is the digest
    /// that was verified, and returns the notices from both halves.
    ///
    /// A mismatch means the tag moved mid-pull: under `enforce` the
    /// bytes now in the store were never verified, whatever was verified
    /// a moment ago.
    pub fn confirm(mut self, stored: &str) -> anyhow::Result<Vec<String>> {
        let Some(verified) = self.verified_digest.take() else {
            return Ok(self.notices);
        };
        if digests_match(&verified, stored) {
            return Ok(self.notices);
        }
        let msg = format!(
            "{} changed while being pulled: verified {}, stored {}",
            self.reference,
            short_digest(&verified),
            short_digest(stored)
        );
        if self.mode == Mode::Enforce {
            bail!("{msg}");
        }
        self.notices.push(format!("warning: {msg}"));
        Ok(self.notices)
    }
}

// ---------------------------------------------------------------------------
// Signing keys
// ---------------------------------------------------------------------------

/// Passphrase for a `--sign-key`, from `LLMMAN_SIGN_PASSWORD` or
/// cosign's own `COSIGN_PASSWORD`. Ignored for an unencrypted PEM.
pub fn signing_password() -> String {
    std::env::var("LLMMAN_SIGN_PASSWORD")
        .or_else(|_| std::env::var("COSIGN_PASSWORD"))
        .unwrap_or_default()
}

/// Checks a `--sign-key` is readable and returns its absolute path.
///
/// Opened, not stat-ed, so a directory or a bad mode fails here rather
/// than after a multi-gigabyte upload. Absolute because the daemon
/// resolves it in its own working directory, not the client's.
pub fn check_signing_key(path: &str) -> anyhow::Result<PathBuf> {
    let meta =
        std::fs::metadata(path).with_context(|| format!("cannot read signing key {path}"))?;
    if !meta.is_file() {
        bail!("signing key {path} is not a file");
    }
    std::fs::File::open(path).with_context(|| format!("cannot read signing key {path}"))?;
    dunce::canonicalize(path).with_context(|| format!("resolve signing key {path}"))
}

/// Every distinct trusted key the policy names, for `llmman verify` to
/// fall back on when no `--key` was given and no rule matched.
pub fn all_configured_keys() -> anyhow::Result<Vec<PathBuf>> {
    let mut seen = HashSet::new();
    let mut keys: Vec<PathBuf> = Policy::load()?
        .rules
        .iter()
        .flat_map(|r| r.keys.iter())
        .filter(|k| seen.insert((*k).clone()))
        .cloned()
        .collect();
    keys.sort();
    Ok(keys)
}

/// Exposed for `Policy::decide` callers that already hold a reference.
pub fn decide(reference: &str) -> anyhow::Result<Decision> {
    Ok(Policy::load()?.decide(reference))
}

// ---------------------------------------------------------------------------
// Layer digests at load
// ---------------------------------------------------------------------------

/// Re-hashes the stored manifest, its config blob and every layer blob
/// it lists under `reference`, comparing each SHA-256 with the digest
/// the descriptor records. `mode` comes from [`layers_mode`]; callers
/// resolve it first so `Off` never opens the store. `Warn` returns one
/// notice per mismatch; `Enforce` fails on the first batch of
/// mismatches. A blob that cannot be opened or whose digest is malformed
/// counts as a mismatch, with the cause in the text: the backend would
/// fail on it anyway, and a missing file is as much a change as a
/// rewritten one. A manifest whose own bytes mismatch is reported alone:
/// the descriptors it lists are not trusted, so they are not followed.
///
/// Nothing is removed from the store on failure, unlike `reject_stored`
/// for a signature verdict: the blob may be the only copy the operator
/// has, and the operator asked only to be told. No registry is consulted.
pub fn verify_layers(
    mode: Mode,
    store: &OciStore,
    reference: &str,
    desc: &Descriptor,
) -> anyhow::Result<Vec<String>> {
    if mode == Mode::Off {
        return Ok(Vec::new());
    }
    let mismatched: Vec<(Descriptor, String)> = match blob_mismatch(store, desc) {
        Some(why) => vec![(desc.clone(), why)],
        None => {
            let manifest = store.read_manifest(&desc.digest)?;
            std::iter::once(&manifest.config)
                .chain(manifest.layers.iter())
                .filter_map(|d| blob_mismatch(store, d).map(|why| (d.clone(), why)))
                .collect()
        }
    };
    if mismatched.is_empty() {
        return Ok(Vec::new());
    }
    if mode == Mode::Enforce {
        let list: Vec<String> = mismatched
            .iter()
            .map(|(d, why)| format!("{} ({}): {why}", d.digest, d.media_type))
            .collect();
        bail!(
            "{reference}: stored layers do not match their recorded digests: {}",
            list.join(", ")
        );
    }
    Ok(mismatched
        .iter()
        .map(|(d, why)| {
            format!(
                "warning: {reference}: layer {} ({}) does not match its stored digest: {why}",
                d.digest, d.media_type
            )
        })
        .collect())
}

/// `reference` pinned to the manifest `desc` names: `<name>@<digest>`,
/// with any digest the reference already carried replaced. A caller
/// that verified `desc` loads through this spelling, and
/// `OciStore::find` returns a descriptor for it only when the stored
/// digest matches, so a tag repointed between the check and the load
/// (or a store written by another process in between) is "not found"
/// rather than loaded unverified.
///
/// An absolute path or a URI is returned unchanged: `split_ref_digest`
/// treats an `@` in such a name as part of the name, so appending a
/// digest would make the store look up a reference it does not hold.
/// Such a reference is therefore loaded by name, not by digest: if its
/// pointer is rewritten between the check and the load, the load opens
/// whatever it now names. Only registry-style references are pinned.
pub fn pinned_reference(reference: &str, desc: &Descriptor) -> String {
    let (name, _) = crate::storage::split_ref_digest(reference);
    let pinned = format!("{name}@{}", desc.digest);
    if crate::storage::split_ref_digest(&pinned).1 == Some(desc.digest.as_str()) {
        pinned
    } else {
        reference.to_owned()
    }
}

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

    fn policy(text: &str) -> Policy {
        Policy::parse(text, Path::new("/etc/llmman")).expect("parse policy")
    }

    /// The other tests hand `Policy` a `[verify]` section on its own.
    /// This one goes the way the daemon does, through a whole
    /// `llmman.conf`, so the two cannot drift into a policy that parses
    /// in tests and is silently absent in production.
    #[test]
    fn a_nested_verify_section_of_a_whole_llmman_conf_becomes_a_policy() {
        let conf = crate::config::parse(
            r#"
            [aliases]
            gemma4 = "docker.io/ai/gemma4"

            [providers.openai]
            api_key = "sk-x"

            [verify]
            default = "warn"

            [[verify.trust]]
            pattern = "docker.io/myorg/**"
            keys    = ["keys/myorg.pub"]
            mode    = "enforce"
            "#,
        )
        .expect("valid llmman.conf");

        let policy =
            Policy::from_conf(&conf.verify, Path::new("/etc/llmman")).expect("valid policy");

        // The rule applies where it matches, keys resolved against the
        // file's own directory...
        let decision = policy.decide("docker.io/myorg/model:v1");
        assert_eq!(decision.mode, Mode::Enforce);
        assert_eq!(
            decision.keys,
            vec![PathBuf::from("/etc/llmman/keys/myorg.pub")]
        );
        // ...and `default` covers everything it does not.
        assert_eq!(policy.decide("docker.io/other/model").mode, Mode::Warn);
    }

    #[test]
    fn repository_of_strips_tags_and_digests() {
        assert_eq!(
            repository_of("docker.io/org/model:v1"),
            "docker.io/org/model"
        );
        assert_eq!(repository_of("docker.io/org/model"), "docker.io/org/model");
        assert_eq!(
            repository_of("docker.io/org/model@sha256:abc"),
            "docker.io/org/model"
        );
        assert_eq!(
            repository_of("docker.io/org/model:v1@sha256:abc"),
            "docker.io/org/model"
        );
    }

    #[test]
    fn repository_of_keeps_a_registry_port() {
        // The colon precedes the last slash, so it is a port, not a tag.
        assert_eq!(
            repository_of("registry.example.com:5000/org/model:v1"),
            "registry.example.com:5000/org/model"
        );
    }

    #[test]
    fn canonical_repository_agrees_with_the_go_side_on_what_is_equal() {
        // Different normal forms (this one qualifies, Go's strips), but
        // they must fold the same spellings together.
        let want = canonical_repository("docker.io/ai/gemma4");
        assert_eq!(want, "docker.io/ai/gemma4");
        for spelling in [
            "index.docker.io/ai/gemma4",
            "registry-1.docker.io/ai/gemma4",
            "ai/gemma4",
        ] {
            assert_eq!(canonical_repository(spelling), want, "{spelling}");
        }
        // Official images, both spellings.
        assert_eq!(
            canonical_repository("ubuntu"),
            canonical_repository("docker.io/ubuntu")
        );
        assert_eq!(
            canonical_repository("ubuntu"),
            canonical_repository("docker.io/library/ubuntu")
        );
        // A bare host with no path still folds — the alias check must
        // not require a trailing "/" to fire.
        assert_eq!(
            canonical_repository("index.docker.io"),
            canonical_repository("docker.io")
        );
        // Other hosts are left exactly alone.
        assert_eq!(canonical_repository("quay.io/org/m"), "quay.io/org/m");
        assert_eq!(
            canonical_repository("registry.example.com:5000/org/m"),
            "registry.example.com:5000/org/m"
        );
    }

    #[test]
    fn a_docker_hub_rule_does_not_leak_onto_other_registries() {
        // Regression: canonicalizing the *pattern* stripped its host, so
        // `docker.io/**` became `**` and matched every registry — an
        // `off` rule for Hub would have disabled verification globally.
        let p = policy(
            r#"
            [[trust]]
            pattern = "docker.io/**"
            keys = ["/hub.pub"]
            mode = "enforce"
        "#,
        );
        assert_eq!(p.decide("docker.io/ai/gemma4:latest").mode, Mode::Enforce);
        for elsewhere in [
            "quay.io/other/model:v1",
            "registry.internal.example.com/team/secret:v1",
            "ghcr.io/a/b",
        ] {
            assert_eq!(p.decide(elsewhere).mode, Mode::Off, "{elsewhere}");
        }
    }

    #[test]
    fn a_docker_hub_rule_matches_every_spelling() {
        let p = policy(
            r#"
            [[trust]]
            pattern = "docker.io/ai/*"
            keys = ["/k.pub"]
            mode = "enforce"
        "#,
        );
        for reference in [
            "docker.io/ai/gemma4:latest",
            "index.docker.io/ai/gemma4:latest",
            "ai/gemma4",
        ] {
            assert_eq!(p.decide(reference).mode, Mode::Enforce, "{reference}");
        }
    }

    // -- glob ------------------------------------------------------------

    #[test]
    fn glob_matches_literals_exactly() {
        assert!(glob_match("docker.io/org/model", "docker.io/org/model"));
        assert!(!glob_match("docker.io/org/model", "docker.io/org/model2"));
    }

    #[test]
    fn single_star_stays_within_one_path_segment() {
        assert!(glob_match("docker.io/org/*", "docker.io/org/model"));
        assert!(glob_match("docker.io/*/model", "docker.io/org/model"));
        assert!(!glob_match("docker.io/org/*", "docker.io/org/team/model"));
    }

    #[test]
    fn double_star_crosses_path_segments() {
        assert!(glob_match("docker.io/**", "docker.io/org/model"));
        assert!(glob_match("docker.io/**", "docker.io/org/team/model"));
        assert!(glob_match("docker.io/**/model", "docker.io/org/team/model"));
        assert!(!glob_match("quay.io/**", "docker.io/org/model"));
    }

    #[test]
    fn glob_is_anchored_at_both_ends() {
        assert!(glob_match("docker.io/org/model*", "docker.io/org/model"));
        assert!(!glob_match("docker.io", "docker.io/org/model"));
    }

    #[test]
    fn glob_does_not_blow_up_on_a_pathological_pattern() {
        // The trailing "b" is what makes this the bad case: every one of
        // the 24 `*a` branches has to be explored before the match can
        // be ruled out. Unmemoized that is exponential and this test
        // does not finish; the assertion is really just the clock.
        let pattern = "*a".repeat(24) + "b";
        let text = "a".repeat(64);
        assert!(!glob_match(&pattern, &text));
        // Still correct on the matching side.
        assert!(glob_match(&("*a".repeat(24)), &text));
    }

    // -- rule selection --------------------------------------------------

    #[test]
    fn no_config_means_off() {
        assert_eq!(
            Policy::default().decide("docker.io/org/model:v1").mode,
            Mode::Off
        );
    }

    #[test]
    fn unmatched_reference_falls_back_to_the_default_mode() {
        let p = policy(
            r#"
            default = "warn"
            [[trust]]
            pattern = "docker.io/org/**"
            keys = ["/k.pub"]
            mode = "enforce"
        "#,
        );
        assert_eq!(p.decide("quay.io/other/model:v1").mode, Mode::Warn);
        assert!(p.decide("quay.io/other/model:v1").keys.is_empty());
    }

    #[test]
    fn the_last_matching_rule_wins_and_replaces_its_keys() {
        let p = policy(
            r#"
            [[trust]]
            pattern = "docker.io/**"
            keys = ["/broad.pub"]
            mode = "enforce"

            [[trust]]
            pattern = "docker.io/org/experimental"
            keys = ["/narrow.pub"]
            mode = "warn"
        "#,
        );
        let broad = p.decide("docker.io/org/model:v1");
        assert_eq!(broad.mode, Mode::Enforce);
        assert_eq!(broad.keys, vec![PathBuf::from("/broad.pub")]);

        let narrow = p.decide("docker.io/org/experimental:v1");
        assert_eq!(narrow.mode, Mode::Warn);
        // Replaced, not added to, so "trust the org key everywhere
        // except here" is expressible.
        assert_eq!(narrow.keys, vec![PathBuf::from("/narrow.pub")]);
    }

    #[test]
    fn a_rule_matches_regardless_of_tag() {
        let p = policy(
            r#"
            [[trust]]
            pattern = "docker.io/org/model"
            keys = ["/k.pub"]
            mode = "enforce"
        "#,
        );
        for reference in [
            "docker.io/org/model",
            "docker.io/org/model:v1",
            "docker.io/org/model@sha256:abc",
        ] {
            assert_eq!(p.decide(reference).mode, Mode::Enforce, "{reference}");
        }
    }

    // -- parsing ---------------------------------------------------------

    #[test]
    fn a_rule_without_an_explicit_mode_warns() {
        let p = policy("[[trust]]\npattern = \"docker.io/org/**\"\nkeys = [\"/k.pub\"]\n");
        assert_eq!(p.decide("docker.io/org/model").mode, Mode::Warn);
    }

    #[test]
    fn an_explicit_default_off_overrides_a_lower_priority_file() {
        // "later files win" has to include downgrades, or a per-user
        // file could never relax a system one.
        let lower = policy("default = \"warn\"\n");
        let upper = policy("default = \"off\"\n");
        assert_eq!(lower.default_mode, Some(Mode::Warn));
        assert_eq!(upper.default_mode, Some(Mode::Off));
        // A file that says nothing must not override either.
        assert_eq!(policy("[[trust]]\npattern=\"a/b\"\n").default_mode, None);
    }

    #[test]
    fn mode_spellings_are_accepted_case_insensitively() {
        assert_eq!(Mode::parse("Off").unwrap(), Mode::Off);
        assert_eq!(Mode::parse("  WARN ").unwrap(), Mode::Warn);
        assert_eq!(Mode::parse("require").unwrap(), Mode::Enforce);
        assert!(Mode::parse("maybe").is_err());
    }

    #[test]
    fn relative_key_paths_resolve_against_the_config_file() {
        let p = Policy::parse(
            "[[trust]]\npattern = \"docker.io/org/**\"\nkeys = [\"keys/org.pub\", \"/abs/other.pub\"]\n",
            Path::new("/etc/llmman"),
        )
        .unwrap();
        assert_eq!(
            p.decide("docker.io/org/model").keys,
            vec![
                PathBuf::from("/etc/llmman/keys/org.pub"),
                PathBuf::from("/abs/other.pub"),
            ]
        );
    }

    #[test]
    fn an_unusable_pattern_is_rejected_rather_than_silently_never_matching() {
        for pattern in [
            "",
            // A tag or digest can never match, since patterns are
            // compared against the repository.
            "docker.io/org/model:v1",
            "docker.io/org/model@sha256:abc",
        ] {
            assert!(
                validate_pattern("pattern", pattern).is_err(),
                "accepted {pattern:?}"
            );
        }
        assert!(validate_pattern("pattern", &"a".repeat(MAX_PATTERN_LEN + 1)).is_err());
        // A port is not a tag.
        assert!(validate_pattern("pattern", "registry.example.com:5000/org/*").is_ok());
    }

    /// `claims` is a glob like `pattern` and is held to the same rules.
    /// An empty one is an error rather than "unset": the shim reads an
    /// empty string as "no claims", so letting it through would turn an
    /// author's typo into exact equality without a word.
    #[test]
    fn an_unusable_claims_glob_is_rejected_and_the_error_names_claims() {
        for claims in ["\"\"", &format!("{:?}", "a".repeat(MAX_PATTERN_LEN + 1))] {
            let err = Policy::parse(
                &format!("[[trust]]\npattern=\"a/b\"\nclaims={claims}\n"),
                Path::new("/x"),
            )
            .unwrap_err();
            let msg = err.to_string();
            assert!(msg.contains("claims"), "{err:#}");
            assert!(!msg.contains("pattern"), "{err:#}");
        }
    }

    #[test]
    fn decide_carries_the_winning_rules_claims() {
        let p = policy(
            "[[trust]]\npattern=\"mirror.corp/**\"\nkeys=[\"k.pub\"]\n\
             claims=\"docker.io/myorg/**\"\n",
        );
        let decision = p.decide("mirror.corp/myorg/name:v1");
        assert_eq!(decision.claims.as_deref(), Some("docker.io/myorg/**"));
        // Outside every pattern the default applies, and the default has
        // no claims to make.
        let outside = p.decide("quay.io/myorg/name:v1");
        assert_eq!(outside.claims, None);
        assert!(outside.keys.is_empty());
    }

    #[test]
    fn an_identity_rule_without_keys_is_valid_and_decide_carries_it() {
        let p = policy(
            "trusted_root = \"sigstore/root.json\"\n\
             [[trust]]\npattern=\"ghcr.io/org/**\"\n\
             issuer=\"https://token.actions.githubusercontent.com\"\n\
             subject=\"https://github.com/org/models/.github/workflows/sign.yml@refs/heads/main\"\n",
        );
        let decision = p.decide("ghcr.io/org/model:v1");
        assert!(decision.keys.is_empty());
        assert!(decision.has_identity());
        assert_eq!(
            decision.issuer.as_deref(),
            Some("https://token.actions.githubusercontent.com")
        );
        assert_eq!(
            decision.subject.as_deref(),
            Some("https://github.com/org/models/.github/workflows/sign.yml@refs/heads/main")
        );
        // Relative like `keys`, against the file's own directory.
        assert_eq!(
            decision.trusted_root.as_deref(),
            Some(Path::new("/etc/llmman/sigstore/root.json"))
        );
        // The root is policy-wide: a reference no rule matches gets it too.
        let outside = p.decide("quay.io/other/model:v1");
        assert!(!outside.has_identity());
        assert_eq!(
            outside.trusted_root.as_deref(),
            Some(Path::new("/etc/llmman/sigstore/root.json"))
        );
    }

    /// One half alone is too wide: an issuer without a subject would
    /// accept every identity that issuer signs for, and a subject
    /// without an issuer the same name from any issuer.
    #[test]
    fn an_identity_needs_both_issuer_and_subject() {
        for half in ["issuer=\"https://issuer\"", "subject=\"me@example.com\""] {
            let err = Policy::parse(
                &format!("[[trust]]\npattern=\"a/b\"\n{half}\n"),
                Path::new("/x"),
            )
            .unwrap_err();
            assert!(
                format!("{err:#}").contains("both `issuer` and `subject`"),
                "{half}: {err:#}"
            );
        }
    }

    #[test]
    fn keys_and_an_identity_on_one_rule_is_a_config_error() {
        for identity in ["issuer=\"https://issuer\"", "subject=\"me@example.com\""] {
            let err = Policy::parse(
                &format!("[[trust]]\npattern=\"a/b\"\nkeys=[\"k.pub\"]\n{identity}\n"),
                Path::new("/x"),
            )
            .unwrap_err();
            let msg = err.to_string();
            assert!(msg.contains("both `keys` and an identity"), "{err:#}");
            assert!(msg.contains("\"a/b\""), "{err:#}");
        }
        let err = Policy::parse(
            "[[trust]]\npattern=\"a/b\"\nissuer=\" \"\n",
            Path::new("/x"),
        )
        .unwrap_err();
        assert!(err.to_string().contains("empty `issuer`"), "{err:#}");
    }

    #[test]
    fn a_later_files_trusted_root_wins_and_a_silent_one_keeps_it() {
        let mut merged = Policy::default();
        merged.merge(Policy::parse("trusted_root = \"a.json\"", Path::new("/one")).unwrap());
        merged.merge(Policy::parse("trusted_root = \"/abs/b.json\"", Path::new("/two")).unwrap());
        assert_eq!(
            merged.trusted_root.as_deref(),
            Some(Path::new("/abs/b.json"))
        );
        merged.merge(Policy::parse("default = \"warn\"", Path::new("/three")).unwrap());
        assert_eq!(
            merged.trusted_root.as_deref(),
            Some(Path::new("/abs/b.json"))
        );
    }

    #[test]
    fn an_unknown_mode_is_rejected_rather_than_defaulted() {
        assert!(Policy::parse(
            "[[trust]]\npattern=\"a/b\"\nmode=\"enfroce\"\n",
            Path::new("/x")
        )
        .is_err());
        assert!(Policy::parse("default = \"nope\"\n", Path::new("/x")).is_err());
    }

    // -- check_with ------------------------------------------------------

    #[test]
    fn off_skips_without_calling_the_verifier() {
        let decision = Decision {
            mode: Mode::Off,
            keys: vec![PathBuf::from("/k.pub")],
            claims: None,
            issuer: None,
            subject: None,
            trusted_root: None,
        };
        let verdict = check_with("docker.io/org/model:v1", None, &decision).unwrap();
        assert_eq!(verdict.outcome, Outcome::Skipped);
        assert!(verdict.notices.is_empty());
    }

    #[test]
    fn enforce_without_keys_fails_closed() {
        let decision = Decision {
            mode: Mode::Enforce,
            keys: vec![],
            claims: None,
            issuer: None,
            subject: None,
            trusted_root: None,
        };
        let err = check_with("docker.io/org/model:v1", None, &decision).unwrap_err();
        assert!(
            err.to_string().contains("no trusted public keys"),
            "{err:#}"
        );
    }

    #[test]
    fn warn_without_keys_reports_and_continues() {
        let decision = Decision {
            mode: Mode::Warn,
            keys: vec![],
            claims: None,
            issuer: None,
            subject: None,
            trusted_root: None,
        };
        let verdict = check_with("docker.io/org/model:v1", None, &decision).unwrap();
        assert_eq!(verdict.outcome, Outcome::Unverified);
        // `warn` must produce something the caller can actually show.
        assert_eq!(verdict.notices.len(), 1);
        assert!(verdict.notices[0].contains("no trusted public keys"));
    }

    #[test]
    fn an_identity_without_a_trusted_root_is_indeterminate_not_unsigned() {
        let mut decision = Decision {
            mode: Mode::Enforce,
            keys: vec![],
            claims: None,
            issuer: Some("https://issuer".into()),
            subject: None,
            trusted_root: None,
        };
        let err = check_with("docker.io/org/model:v1", None, &decision).unwrap_err();
        assert!(is_indeterminate(&err), "{err:#}");
        let msg = format!("{err:#}");
        assert!(msg.contains("trusted_root"), "{msg}");
        assert!(!msg.contains("no trusted public keys"), "{msg}");
        assert!(!msg.contains("not signed"), "{msg}");

        decision.mode = Mode::Warn;
        let verdict = check_with("docker.io/org/model:v1", None, &decision).unwrap();
        assert_eq!(verdict.outcome, Outcome::Unverified);
        assert!(verdict.notices[0].contains("trusted_root"));
    }

    // -- PullGuard -------------------------------------------------------

    fn guard(mode: Mode, verified: Option<&str>) -> PullGuard {
        PullGuard {
            reference: "docker.io/org/model:v1".into(),
            mode,
            verified_digest: verified.map(str::to_owned),
            notices: Vec::new(),
        }
    }

    #[test]
    fn confirm_is_a_no_op_when_nothing_was_verified() {
        let mut g = guard(Mode::Warn, None);
        g.notices.push("warning: something".into());
        assert_eq!(g.confirm("sha256:whatever").unwrap().len(), 1);
    }

    #[test]
    fn confirm_accepts_the_digest_that_was_verified() {
        assert!(guard(Mode::Enforce, Some("sha256:aaa"))
            .confirm("sha256:aaa")
            .is_ok());
    }

    #[test]
    fn confirm_ignores_digest_case() {
        // OciStore::find compares case-insensitively; a spelling
        // difference must not look like a moved tag.
        assert!(guard(Mode::Enforce, Some("sha256:ABC"))
            .confirm("sha256:abc")
            .is_ok());
    }

    #[test]
    fn confirm_rejects_a_tag_that_moved_mid_pull_under_enforce() {
        let err = guard(Mode::Enforce, Some("sha256:aaa"))
            .confirm("sha256:bbb")
            .unwrap_err();
        assert!(
            err.to_string().contains("changed while being pulled"),
            "{err:#}"
        );
    }

    #[test]
    fn confirm_only_warns_about_a_moved_tag_under_warn() {
        let notices = guard(Mode::Warn, Some("sha256:aaa"))
            .confirm("sha256:bbb")
            .unwrap();
        assert_eq!(notices.len(), 1);
        assert!(notices[0].contains("changed while being pulled"));
    }

    #[test]
    fn layers_mode_parses_merges_and_stays_apart_from_the_signature_policy() {
        let dir = Path::new("/etc/llmman");
        let warn = Policy::parse("layers = \"warn\"", dir).unwrap();
        assert_eq!(warn.layers_mode, Some(Mode::Warn));

        // Later file wins, and an explicit off can override an enforce.
        let mut merged = Policy::default();
        for text in ["layers = \"enforce\"", "layers = \"off\""] {
            merged.merge(Policy::parse(text, dir).unwrap());
        }
        assert_eq!(merged.layers_mode, Some(Mode::Off));
        assert_eq!(merged.layers_mode(), Mode::Off);

        // A file that is silent on `layers` leaves the earlier answer.
        merged.merge(Policy::parse("default = \"warn\"", dir).unwrap());
        assert_eq!(merged.layers_mode(), Mode::Off);

        // The environment beats the files.
        merged.forced_layers_mode =
            parse_override(Some("enforce"), "LLMMAN_VERIFY_LAYERS").unwrap();
        assert_eq!(merged.layers_mode(), Mode::Enforce);

        // Enforcing signatures does not switch layer hashing on.
        let signed = Policy::parse(
            "default = \"enforce\"\n[[trust]]\npattern = \"docker.io/org/**\"\nkeys = [\"k.pub\"]",
            dir,
        )
        .unwrap();
        assert_eq!(signed.default_mode, Some(Mode::Enforce));
        assert_eq!(signed.layers_mode, None);
        assert_eq!(signed.layers_mode.unwrap_or_default(), Mode::Off);

        let err = Policy::parse("layers = \"bogus\"", dir).unwrap_err();
        assert!(err.to_string().contains("`layers`"), "{err:#}");
    }

    #[test]
    fn an_invalid_llmman_verify_layers_is_an_error_like_llmman_verify() {
        let err = parse_override(Some("bogus"), "LLMMAN_VERIFY_LAYERS")
            .expect_err("a typo must not be ignored");
        assert!(err.to_string().contains("LLMMAN_VERIFY_LAYERS"), "{err:#}");
        assert_eq!(parse_override(None, "LLMMAN_VERIFY_LAYERS").unwrap(), None);
        assert_eq!(
            parse_override(Some("  "), "LLMMAN_VERIFY_LAYERS").unwrap(),
            None
        );
        assert_eq!(
            parse_override(Some("enforce"), "LLMMAN_VERIFY_LAYERS").unwrap(),
            Some(Mode::Enforce)
        );
    }

    #[test]
    fn an_invalid_llmman_verify_is_an_error_not_a_silent_downgrade() {
        // Read inside the daemon, whose stderr nobody watches, so
        // warning and carrying on would turn a typo into a downgrade.
        let err = parse_override(Some("enfroce"), "LLMMAN_VERIFY")
            .expect_err("a typo'd LLMMAN_VERIFY must not be ignored");
        assert!(err.to_string().contains("LLMMAN_VERIFY"), "{err:#}");

        assert_eq!(parse_override(None, "LLMMAN_VERIFY").unwrap(), None);
        assert_eq!(parse_override(Some("  "), "LLMMAN_VERIFY").unwrap(), None);
        assert_eq!(
            parse_override(Some("enforce"), "LLMMAN_VERIFY").unwrap(),
            Some(Mode::Enforce)
        );
    }

    #[test]
    fn escalate_follows_the_mode_it_was_decided_under() {
        // `cmd::transfer` confirms the digest it actually transferred
        // and must treat a mismatch the way the policy would.
        let mut warn = Verdict::new(Outcome::Verified, Mode::Warn);
        assert!(warn.escalate("moved".into()).is_ok());
        assert_eq!(warn.notices, vec!["warning: moved".to_string()]);

        let mut enforce = Verdict::new(Outcome::Verified, Mode::Enforce);
        assert!(enforce.escalate("moved".into()).is_err());

        let mut off = Verdict::new(Outcome::Skipped, Mode::Off);
        assert!(off.escalate("moved".into()).is_ok());
        assert!(off.notices.is_empty());
    }

    #[test]
    fn check_signing_key_rejects_what_cannot_be_signed_with() {
        let dir = std::env::temp_dir().join(format!("llmman-key-test-{}", std::process::id()));
        let _ = std::fs::create_dir_all(&dir);
        assert!(
            check_signing_key(dir.to_str().unwrap()).is_err(),
            "a directory"
        );
        assert!(
            check_signing_key(&dir.join("absent").to_string_lossy()).is_err(),
            "a missing file"
        );

        let key = dir.join("k.pem");
        std::fs::write(&key, b"not really a key").unwrap();
        // Returns an absolute path: the daemon resolves it in its own
        // working directory, not the client's.
        let resolved = check_signing_key(key.to_str().unwrap()).unwrap();
        assert!(resolved.is_absolute());
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn pin_to_digest_replaces_a_tag_rather_than_appending_to_it() {
        let d = "sha256:abc";
        assert_eq!(
            pin_to_digest("docker.io/org/m:v1", d),
            "docker.io/org/m@sha256:abc"
        );
        assert_eq!(
            pin_to_digest("docker.io/org/m", d),
            "docker.io/org/m@sha256:abc"
        );
        // An already-pinned reference is repinned, not doubled up.
        assert_eq!(
            pin_to_digest("docker.io/org/m@sha256:old", d),
            "docker.io/org/m@sha256:abc"
        );
        // A registry port survives.
        assert_eq!(
            pin_to_digest("registry.example.com:5000/org/m:v1", d),
            "registry.example.com:5000/org/m@sha256:abc"
        );
    }

    #[test]
    fn an_unknown_config_key_is_rejected_rather_than_ignored() {
        // `defualt` or `[[turst]]` would otherwise load as an off
        // policy — the same silent downgrade a typo'd mode would be.
        for text in [
            "defualt = \"enforce\"\n",
            "[[turst]]\npattern = \"docker.io/**\"\n",
            "[[trust]]\npattern = \"docker.io/**\"\nkyes = [\"/k.pub\"]\n",
        ] {
            assert!(
                Policy::parse(text, Path::new("/etc/llmman")).is_err(),
                "accepted {text:?}"
            );
        }
    }

    #[test]
    fn is_registry_reference_fails_closed_on_an_unknown_host() {
        // The whole point: an unreachable or hostile network must not be
        // able to reclassify a registry reference out of the policy's
        // reach, so anything not recognizably non-registry counts.
        for reference in [
            "docker.io/org/model:v1",
            "quay.io/org/model",
            "registry.internal.example.com:5000/team/m",
            // Not a registry at all, but unknowable without a probe —
            // treated as one, so it gets checked and refused rather
            // than silently skipped.
            "some-unknown-host.example/org/m",
        ] {
            assert!(is_registry_reference(reference), "{reference}");
        }
        for reference in [
            "hf.co/org/model",
            "huggingface.co/org/model",
            "modelscope.cn/org/model",
            "hf://org/model",
            "huggingface://org/model",
            "ms://org/model",
            "ngc://org/model",
            "s3://bucket/key",
            "gs://bucket/key",
            "/local/path/to/model",
        ] {
            assert!(!is_registry_reference(reference), "{reference}");
        }
    }

    #[test]
    fn short_digest_abbreviates_like_the_shim_does() {
        assert_eq!(short_digest("sha256:0123456789abcdef0123"), "0123456789ab");
        assert_eq!(short_digest("abc"), "abc");
    }

    // -----------------------------------------------------------------
    // Layer digests at load
    // -----------------------------------------------------------------

    const GGUF_MEDIA_TYPE: &str = "application/vnd.docker.ai.gguf.v3";

    /// A store holding one model: a config blob, one GGUF layer with the
    /// bytes `GGUFaaaa`, and a tagged manifest. Returns the store, the
    /// manifest descriptor and the layer descriptor.
    /// A store in a fresh temp dir, removed when the test ends, as the
    /// key tests above do with theirs.
    struct TempStore {
        store: OciStore,
        dir: std::path::PathBuf,
    }

    impl std::ops::Deref for TempStore {
        type Target = OciStore;
        fn deref(&self) -> &OciStore {
            &self.store
        }
    }

    impl Drop for TempStore {
        fn drop(&mut self) {
            let _ = std::fs::remove_dir_all(&self.dir);
        }
    }

    fn store_with_gguf_layer(name: &str) -> (TempStore, Descriptor, Descriptor) {
        let dir = std::env::temp_dir().join(format!(
            "llmman-verify-layers-{name}-{}-{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        let store = OciStore::open(&dir).unwrap();
        let config = store
            .write_blob("application/vnd.cncf.model.config.v1+json", b"{}")
            .unwrap();
        let layer = store.write_blob(GGUF_MEDIA_TYPE, b"GGUFaaaa").unwrap();
        let manifest = crate::storage::oci::Manifest {
            schema_version: 2,
            media_type: "application/vnd.oci.image.manifest.v1+json".into(),
            artifact_type: None,
            config,
            layers: vec![layer.clone()],
            annotations: None,
        };
        let desc = store.write_manifest(&manifest).unwrap();
        store.tag(desc.clone(), "example.com/ai/model:q4").unwrap();
        let desc = store.find("example.com/ai/model:q4").unwrap();
        (TempStore { store, dir }, desc, layer)
    }

    #[test]
    fn an_untouched_store_passes_layer_verification_under_enforce() {
        let (store, desc, _) = store_with_gguf_layer("clean");
        let notices = verify_layers(Mode::Enforce, &store, "model:q4", &desc).unwrap();
        assert!(notices.is_empty(), "{notices:?}");
    }

    #[test]
    fn a_same_size_overwrite_fails_the_load_under_enforce_and_deletes_nothing() {
        let (store, desc, layer) = store_with_gguf_layer("tamper-enforce");
        let path = store.blob_path(&layer.digest).unwrap();
        std::fs::write(&path, b"GGUFbbbb").unwrap();

        let err = verify_layers(Mode::Enforce, &store, "model:q4", &desc).unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("model:q4"), "{msg}");
        assert!(msg.contains(&layer.digest), "{msg}");
        assert!(msg.contains(GGUF_MEDIA_TYPE), "{msg}");
        assert!(path.is_file(), "the tampered blob must stay in the store");
        assert!(
            store.find("example.com/ai/model:q4").is_ok(),
            "the reference must stay in the store"
        );
    }

    #[test]
    fn a_same_size_overwrite_is_one_notice_under_warn() {
        let (store, desc, layer) = store_with_gguf_layer("tamper-warn");
        let path = store.blob_path(&layer.digest).unwrap();
        std::fs::write(&path, b"GGUFbbbb").unwrap();

        let notices = verify_layers(Mode::Warn, &store, "model:q4", &desc).unwrap();
        assert_eq!(notices.len(), 1, "{notices:?}");
        assert!(
            notices[0].starts_with("warning: model:q4: layer "),
            "{notices:?}"
        );
        assert!(notices[0].contains(&layer.digest), "{notices:?}");
        assert!(path.is_file());
    }

    #[test]
    #[cfg(unix)]
    fn a_blob_replaced_by_a_symlink_to_the_right_bytes_is_a_mismatch() {
        let (store, desc, layer) = store_with_gguf_layer("symlink");
        let path = store.blob_path(&layer.digest).unwrap();
        let target = store.dir.join("elsewhere.gguf");
        std::fs::rename(&path, &target).unwrap();
        std::os::unix::fs::symlink(&target, &path).unwrap();
        assert_eq!(std::fs::read(&path).unwrap(), b"GGUFaaaa");

        let notices = verify_layers(Mode::Warn, &store, "model:q4", &desc).unwrap();
        assert_eq!(notices.len(), 1, "{notices:?}");
        assert!(notices[0].contains(&layer.digest), "{notices:?}");
        assert!(notices[0].contains("regular file"), "{notices:?}");
        let err = verify_layers(Mode::Enforce, &store, "model:q4", &desc).unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains(&layer.digest), "{msg}");
        assert!(msg.contains("regular file"), "{msg}");
        assert!(path.symlink_metadata().unwrap().file_type().is_symlink());
    }

    /// Creating a symlink needs `SeCreateSymbolicLinkPrivilege` or
    /// developer mode; when the runner has neither the test returns
    /// early, and the shared file type check is covered by the unix test
    /// and the directory test below.
    #[test]
    #[cfg(windows)]
    fn a_blob_replaced_by_a_symlink_to_the_right_bytes_is_a_mismatch_on_windows() {
        let (store, desc, layer) = store_with_gguf_layer("symlink");
        let path = store.blob_path(&layer.digest).unwrap();
        let target = store.dir.join("elsewhere.gguf");
        std::fs::rename(&path, &target).unwrap();
        if let Err(e) = std::os::windows::fs::symlink_file(&target, &path) {
            eprintln!("skipping: cannot create a symlink on this runner: {e}");
            return;
        }
        assert_eq!(std::fs::read(&path).unwrap(), b"GGUFaaaa");

        let notices = verify_layers(Mode::Warn, &store, "model:q4", &desc).unwrap();
        assert_eq!(notices.len(), 1, "{notices:?}");
        assert!(notices[0].contains(&layer.digest), "{notices:?}");
        assert!(notices[0].contains("regular file"), "{notices:?}");
        let err = verify_layers(Mode::Enforce, &store, "model:q4", &desc).unwrap_err();
        assert!(err.to_string().contains("regular file"), "{err}");
        assert!(path.symlink_metadata().unwrap().file_type().is_symlink());
    }

    #[test]
    fn a_blob_replaced_by_a_directory_is_a_mismatch() {
        let (store, desc, layer) = store_with_gguf_layer("directory");
        let path = store.blob_path(&layer.digest).unwrap();
        std::fs::remove_file(&path).unwrap();
        std::fs::create_dir(&path).unwrap();

        let notices = verify_layers(Mode::Warn, &store, "model:q4", &desc).unwrap();
        assert_eq!(notices.len(), 1, "{notices:?}");
        assert!(notices[0].contains(&layer.digest), "{notices:?}");
        assert!(notices[0].contains("regular file"), "{notices:?}");
        let err = verify_layers(Mode::Enforce, &store, "model:q4", &desc).unwrap_err();
        assert!(err.to_string().contains("regular file"), "{err}");
    }

    #[test]
    #[cfg(unix)]
    fn a_blob_replaced_by_a_fifo_is_a_mismatch_and_is_not_read() {
        let (store, desc, layer) = store_with_gguf_layer("fifo");
        let path = store.blob_path(&layer.digest).unwrap();
        std::fs::remove_file(&path).unwrap();
        let c_path = std::ffi::CString::new(path.as_os_str().as_encoded_bytes()).unwrap();
        // SAFETY: `c_path` is a valid NUL-terminated string for the call.
        assert_eq!(unsafe { libc::mkfifo(c_path.as_ptr(), 0o600) }, 0);
        {
            use std::os::unix::fs::FileTypeExt as _;
            assert!(path.symlink_metadata().unwrap().file_type().is_fifo());
        }

        // No writer ever opens the FIFO: a blocking open or read hangs
        // the test, so returning at all is the assertion.
        let notices = verify_layers(Mode::Warn, &store, "model:q4", &desc).unwrap();
        assert_eq!(notices.len(), 1, "{notices:?}");
        assert!(notices[0].contains(&layer.digest), "{notices:?}");
        assert!(notices[0].contains("regular file"), "{notices:?}");
        let err = verify_layers(Mode::Enforce, &store, "model:q4", &desc).unwrap_err();
        assert!(err.to_string().contains("regular file"), "{err}");
    }

    #[test]
    fn pinned_reference_names_the_verified_digest_and_the_store_honours_it() {
        let (store, desc, _) = store_with_gguf_layer("pinned");
        let digest = desc.digest.clone();
        assert_eq!(
            pinned_reference("example.com/ai/model:q4", &desc),
            format!("example.com/ai/model:q4@{digest}")
        );
        // A digest already in the reference is replaced, not stacked.
        assert_eq!(
            pinned_reference("example.com/ai/model@sha256:0000", &desc),
            format!("example.com/ai/model@{digest}")
        );
        assert_eq!(
            pinned_reference("example.com/ai/model:q4@sha256:0000", &desc),
            format!("example.com/ai/model:q4@{digest}")
        );
        // A `/` after the `@` is not a digest separator, and a path or
        // URI cannot carry a digest at all, so it is left alone.
        assert_eq!(
            pinned_reference("example.com/ai/m@x/model:q4", &desc),
            format!("example.com/ai/m@x/model:q4@{digest}")
        );
        assert_eq!(
            pinned_reference("/models/model.gguf", &desc),
            "/models/model.gguf"
        );
        assert_eq!(
            pinned_reference("s3://bucket/model", &desc),
            "s3://bucket/model"
        );

        // The store resolves the pinned spelling to the same manifest...
        let pinned = pinned_reference("example.com/ai/model:q4", &desc);
        assert_eq!(store.find(&pinned).unwrap().digest, digest);
        // ...and refuses it once the tag points elsewhere.
        let other = store
            .write_blob("application/vnd.oci.image.manifest.v1+json", b"{}")
            .unwrap();
        store.tag(other, "example.com/ai/model:q4").unwrap();
        assert_ne!(
            store.find("example.com/ai/model:q4").unwrap().digest,
            digest
        );
        assert!(
            store.find(&pinned).is_err(),
            "a repointed tag must not resolve"
        );
    }

    #[test]
    fn an_opaque_reference_is_not_pinned_and_loads_by_name() {
        // The callers `find` once, then `verify_layers` and
        // `pinned_reference`, and the resolvers `find` the result again.
        // For a path or URI the result is the same name, so a pointer
        // rewritten in between is what the second `find` returns: the
        // documented limit of the pinning, asserted so it does not change
        // silently.
        let (store, desc, _) = store_with_gguf_layer("opaque");
        let digest = desc.digest.clone();
        store.tag(desc.clone(), "/models/model.gguf").unwrap();
        let desc = store.find("/models/model.gguf").unwrap();
        assert_eq!(desc.digest, digest);
        verify_layers(Mode::Enforce, &store, "/models/model.gguf", &desc).unwrap();
        let pinned = pinned_reference("/models/model.gguf", &desc);
        assert_eq!(pinned, "/models/model.gguf");

        let other = store
            .write_blob("application/vnd.oci.image.manifest.v1+json", b"{}")
            .unwrap();
        store.tag(other.clone(), "/models/model.gguf").unwrap();
        assert_eq!(
            store.find(&pinned).unwrap().digest,
            other.digest,
            "an opaque reference resolves to whatever it now names"
        );
    }

    #[test]
    fn a_missing_layer_blob_is_a_mismatch() {
        let (store, desc, layer) = store_with_gguf_layer("missing");
        std::fs::remove_file(store.blob_path(&layer.digest).unwrap()).unwrap();

        let notices = verify_layers(Mode::Warn, &store, "model:q4", &desc).unwrap();
        assert_eq!(notices.len(), 1, "{notices:?}");
        assert!(notices[0].contains(&layer.digest), "{notices:?}");
        // The I/O cause travels with the notice, not just "mismatch".
        assert!(notices[0].contains("open "), "{notices:?}");
        let err = verify_layers(Mode::Enforce, &store, "model:q4", &desc).unwrap_err();
        assert!(err.to_string().contains(&layer.digest), "{err}");
        assert!(err.to_string().contains("open "), "{err}");
    }

    #[test]
    fn an_overwritten_config_blob_is_a_mismatch() {
        let (store, desc, layer) = store_with_gguf_layer("config");
        let config = store.read_manifest(&desc.digest).unwrap().config;
        std::fs::write(store.blob_path(&config.digest).unwrap(), b"[]").unwrap();

        let notices = verify_layers(Mode::Warn, &store, "model:q4", &desc).unwrap();
        assert_eq!(notices.len(), 1, "{notices:?}");
        assert!(notices[0].contains(&config.digest), "{notices:?}");
        assert!(!notices[0].contains(&layer.digest), "{notices:?}");
        let err = verify_layers(Mode::Enforce, &store, "model:q4", &desc).unwrap_err();
        assert!(err.to_string().contains(&config.digest), "{err}");
    }

    #[test]
    fn every_mismatch_is_reported_when_the_config_and_a_layer_both_changed() {
        let (store, desc, layer) = store_with_gguf_layer("two");
        let config = store.read_manifest(&desc.digest).unwrap().config;
        std::fs::write(store.blob_path(&config.digest).unwrap(), b"[]").unwrap();
        std::fs::write(store.blob_path(&layer.digest).unwrap(), b"GGUFbbbb").unwrap();

        let notices = verify_layers(Mode::Warn, &store, "model:q4", &desc).unwrap();
        assert_eq!(notices.len(), 2, "{notices:?}");
        assert!(
            notices.iter().any(|n| n.contains(&config.digest)),
            "{notices:?}"
        );
        assert!(
            notices.iter().any(|n| n.contains(&layer.digest)),
            "{notices:?}"
        );
        let err = verify_layers(Mode::Enforce, &store, "model:q4", &desc).unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains(&config.digest), "{msg}");
        assert!(msg.contains(&layer.digest), "{msg}");
    }

    #[test]
    fn a_malformed_layer_digest_in_an_intact_manifest_is_a_mismatch_for_that_layer() {
        // The manifest itself hashes correctly (it was written through the
        // store), so its descriptors are followed; the malformed one must
        // be reported with its cause rather than aborting the check.
        let (store, desc, layer) = store_with_gguf_layer("malformed-layer");
        let mut manifest = store.read_manifest(&desc.digest).unwrap();
        manifest.layers.push(Descriptor {
            media_type: GGUF_MEDIA_TYPE.into(),
            digest: "sha256:/dev/zero".into(),
            size: 0,
            annotations: None,
        });
        let desc = store.write_manifest(&manifest).unwrap();

        let notices = verify_layers(Mode::Warn, &store, "model:q4", &desc).unwrap();
        assert_eq!(notices.len(), 1, "{notices:?}");
        assert!(notices[0].contains("invalid digest"), "{notices:?}");
        assert!(!notices[0].contains(&layer.digest), "{notices:?}");
        let err = verify_layers(Mode::Enforce, &store, "model:q4", &desc).unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("invalid digest"), "{msg}");
        assert!(!msg.contains(&layer.digest), "{msg}");
    }

    /// Overwrites the manifest blob in place with a copy whose only layer
    /// digest is `digest`, leaving the manifest's own descriptor stale.
    fn rewrite_manifest_layer_digest(store: &OciStore, desc: &Descriptor, digest: &str) {
        let mut manifest = store.read_manifest(&desc.digest).unwrap();
        manifest.layers[0].digest = digest.to_string();
        std::fs::write(
            store.blob_path(&desc.digest).unwrap(),
            serde_json::to_vec(&manifest).unwrap(),
        )
        .unwrap();
    }

    #[test]
    fn a_rewritten_manifest_with_an_absolute_layer_digest_stops_at_the_manifest() {
        let (store, desc, layer) = store_with_gguf_layer("absolute");
        rewrite_manifest_layer_digest(&store, &desc, "sha256:/dev/zero");

        let notices = verify_layers(Mode::Warn, &store, "model:q4", &desc).unwrap();
        assert_eq!(notices.len(), 1, "{notices:?}");
        assert!(notices[0].contains(&desc.digest), "{notices:?}");
        assert!(!notices[0].contains(&layer.digest), "{notices:?}");
        let err = verify_layers(Mode::Enforce, &store, "model:q4", &desc).unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains(&desc.digest), "{msg}");
        assert!(!msg.contains("/dev/zero"), "{msg}");
    }

    #[test]
    fn a_rewritten_manifest_with_a_traversing_layer_digest_stops_at_the_manifest() {
        let (store, desc, _) = store_with_gguf_layer("traversal");
        rewrite_manifest_layer_digest(&store, &desc, "sha256:../../../etc/shadow");

        let notices = verify_layers(Mode::Warn, &store, "model:q4", &desc).unwrap();
        assert_eq!(notices.len(), 1, "{notices:?}");
        assert!(notices[0].contains(&desc.digest), "{notices:?}");
        assert!(!notices[0].contains(".."), "{notices:?}");
    }

    #[test]
    fn a_malformed_layer_digest_never_becomes_a_store_path() {
        let (store, _, _) = store_with_gguf_layer("malformed");
        for digest in [
            "sha256:/dev/zero",
            "sha256:../../../etc/shadow",
            "sha512:0000000000000000000000000000000000000000000000000000000000000000",
            "sha256:abc",
            "sha256:zz00000000000000000000000000000000000000000000000000000000000000",
            "nocolon",
        ] {
            assert!(store.blob_path(digest).is_err(), "{digest}");
        }
    }

    #[test]
    fn off_opens_nothing_and_returns_empty() {
        let (store, desc, layer) = store_with_gguf_layer("off");
        std::fs::remove_file(store.blob_path(&layer.digest).unwrap()).unwrap();
        // The manifest blob goes too: a read of it would surface as Err.
        std::fs::remove_file(store.blob_path(&desc.digest).unwrap()).unwrap();

        let notices = verify_layers(Mode::Off, &store, "model:q4", &desc).unwrap();
        assert!(notices.is_empty(), "{notices:?}");
    }
}