matra 0.3.0

NLP library: UDPipe structured parse, base text metrics, summarization, keyphrase extraction. Rust core with Python bindings.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
//! model2vec adapter: static sentence embeddings behind the [`Embedder`] port.
//!
//! Loads the published model2vec artifact format (an embedding matrix in
//! safetensors, a tokenizer.json, a config.json) and embeds by table
//! gather, mean pooling, and optional L2 normalization. No matmul, no
//! kernel dispatch, so vectors are bit-identical across targets, which is
//! the property that won this adapter the first slot (RFC-0010).
//!
//! This is the ONLY file that imports `safetensors` and `tokenizers`
//! (boundary rule 4 analog). The inference semantics replicate the Python
//! reference implementation, pinned by the parity and bit-identity
//! fixtures in this file's tests, and the pinned reference model's own
//! digest is pinned again in `spec/tests/semantic/reference-model.json`
//! for every crust's conformance runner.

use std::fs;
use std::path::Path;
#[cfg(not(target_arch = "wasm32"))]
use std::path::PathBuf;
#[cfg(not(target_arch = "wasm32"))]
use std::sync::atomic::{AtomicU64, Ordering};
#[cfg(not(target_arch = "wasm32"))]
use std::time::{Duration, SystemTime};

use safetensors::SafeTensors;
use safetensors::tensor::Dtype;
use sha2::{Digest, Sha256};
use tokenizers::Tokenizer;

#[cfg(not(target_arch = "wasm32"))]
use crate::config::Config;
use crate::digest::hex;
use crate::domain::{self, Embedding, Error};
use crate::embed::Embedder;

/// The reference implementation's default token cap per text. Applied
/// after unknown-token removal; also drives the pre-tokenization
/// character truncation (`max_tokens * median_token_length` bytes).
const DEFAULT_MAX_TOKENS: usize = 512;

/// The three artifact filenames, in the order the identity digest covers
/// them. Every path in this file is built from this array, so the digest
/// and the loader can never disagree about which file is which.
const ARTIFACT_FILES: [&str; 3] = ["model.safetensors", "tokenizer.json", "config.json"];

/// SHA-256 over the three artifact files of the pinned potion-base-8M
/// release, concatenated in [`ARTIFACT_FILES`] order. The same value
/// `spec/tests/semantic/reference-model.json` carries as `artifact_hash`,
/// and the same value [`Model2Vec::model_hash`] reports once loaded. A
/// loaded model whose digest equals it is the pinned model, whichever
/// constructor loaded it, so its identity names the model and licence.
///
/// Refresh this and [`POTION_BASE_8M_URLS`] together with
/// `scripts/fetch-embedding-hash.sh <revision>`.
const POTION_BASE_8M_SHA256: &str =
    "81c3592150873b1c5a8c4262850f795bff4fd568fbde80ac69889d087f16a0b4";

/// Download URLs for the pinned release, one per [`ARTIFACT_FILES`]
/// entry in the same order. The path segment after `resolve/` is a git
/// commit on the Hugging Face repository rather than a branch, so the
/// URL names one immutable tree; `main` would move under the pin. The
/// digest is what actually decides whether the bytes are trusted, and
/// the revision is what makes a mismatch a bug report rather than an
/// upstream edit.
///
/// Refresh with `scripts/fetch-embedding-hash.sh <revision>`.
#[cfg(not(target_arch = "wasm32"))]
const POTION_BASE_8M_URLS: [&str; 3] = [
    "https://huggingface.co/minishlab/potion-base-8M/resolve/bf8b056651a2c21b8d2565580b8569da283cab23/model.safetensors",
    "https://huggingface.co/minishlab/potion-base-8M/resolve/bf8b056651a2c21b8d2565580b8569da283cab23/tokenizer.json",
    "https://huggingface.co/minishlab/potion-base-8M/resolve/bf8b056651a2c21b8d2565580b8569da283cab23/config.json",
];

/// The pinned model's name, which a download notice names it by and
/// [`Model2Vec`]'s identity carries when the pinned set is what loaded.
const POTION_BASE_8M_NAME: &str = "potion-base-8M";

/// The pinned release's three artifacts together, in bytes: 30,236,760
/// for `model.safetensors`, 683,666 for `tokenizer.json` and 202 for
/// `config.json`. Only the download notice reads it, so a first run can
/// say how much is coming before it comes; what decides whether the
/// bytes are trusted is the digest.
#[cfg(not(target_arch = "wasm32"))]
const POTION_BASE_8M_BYTES: u64 = 30_920_628;

/// The pinned model's license, as its model card states it at the pinned
/// revision: `license: mit` in the front matter of
/// <https://huggingface.co/minishlab/potion-base-8M/blob/bf8b056651a2c21b8d2565580b8569da283cab23/README.md>.
/// The repository publishes no license file of its own. matra's code is
/// MIT too, but the model is a separate work with its own terms, so the
/// download notice carries them as the parsing model's does. Beside the
/// URLs because a new revision means checking both.
const POTION_BASE_8M_LICENSE: &str = "MIT";

/// Where the text of [`POTION_BASE_8M_LICENSE`] is published.
#[cfg(not(target_arch = "wasm32"))]
const POTION_BASE_8M_LICENSE_URL: &str = "https://opensource.org/license/mit";

/// The pinned release as one record, so the provisioning path takes the
/// pin whole.
#[cfg(not(target_arch = "wasm32"))]
const POTION_BASE_8M: Pin<'static> = Pin {
    name: POTION_BASE_8M_NAME,
    bytes: POTION_BASE_8M_BYTES,
    digest: POTION_BASE_8M_SHA256,
    urls: POTION_BASE_8M_URLS,
    license: POTION_BASE_8M_LICENSE,
    license_url: POTION_BASE_8M_LICENSE_URL,
};

/// Where [`Model2Vec::potion_base_8m`] keeps the pinned set inside a
/// models directory: the subdirectory named for the model, the one
/// [`Model2Vec::from_config`] reads when `models.embedding` is as shipped.
#[cfg(not(target_arch = "wasm32"))]
fn potion_dir(models_dir: &Path) -> PathBuf {
    models_dir.join(POTION_BASE_8M_NAME)
}

/// A pinned artifact set: what it is called, how many bytes it is, the
/// digest it must satisfy, where its three files come from in
/// [`ARTIFACT_FILES`] order, and the terms it is published under. A test
/// pins a fixture of its own through the same shape, as `nlp/udpipe.rs`
/// does with its own `Pin`.
#[cfg(not(target_arch = "wasm32"))]
#[derive(Debug, Clone, Copy)]
struct Pin<'a> {
    name: &'a str,
    bytes: u64,
    digest: &'a str,
    urls: [&'a str; 3],
    license: &'a str,
    license_url: &'a str,
}

/// Ceiling on any one downloaded artifact, past which
/// [`Error::InputTooLarge`] has `what` set to `"embedding_model_download"`:
/// [`limits::MAX_DOWNLOAD_BYTES`](crate::domain::limits::MAX_DOWNLOAD_BYTES).
/// The largest of the three pinned artifacts, `model.safetensors`, is
/// 30,236,760 bytes.
#[cfg(not(target_arch = "wasm32"))]
use crate::domain::limits::MAX_DOWNLOAD_BYTES as MAX_ARTIFACT_BYTES;

/// Ceiling on one artifact fetch, DNS lookup through the last byte of
/// the body. The largest artifact in the pinned release is about 30 MB,
/// which needs the whole of this budget only on a link under 1 Mbit/s,
/// so the margin is generous for a slow connection and still finite: a
/// stalled or black-holed transfer fails instead of holding the caller
/// for as long as the socket stays open.
#[cfg(not(target_arch = "wasm32"))]
const FETCH_TIMEOUT: Duration = Duration::from_secs(300);

/// Ceiling on establishing the connection, socket and TLS handshake
/// included. Separate from [`FETCH_TIMEOUT`] so an unreachable host
/// fails in seconds rather than consuming the whole transfer budget.
#[cfg(not(target_arch = "wasm32"))]
const CONNECT_TIMEOUT: Duration = Duration::from_secs(30);

/// Prefix of the temporary an artifact lands through. Shared by the
/// creation and the reclaim, so the sweep cannot look for a name the
/// writer does not use.
#[cfg(not(target_arch = "wasm32"))]
const TEMP_FILE_PREFIX: &str = ".tmp.";

/// Age past which a temporary cannot belong to a live provisioning call,
/// so it is a leftover and the next call reclaims it.
///
/// Twice [`FETCH_TIMEOUT`], the same rule and the same number
/// `nlp/udpipe.rs` uses. All three artifacts are fetched and verified
/// before any of them is written, so a temporary's modification time is
/// within milliseconds of the write that follows it, and anything older
/// than ten minutes is an orphan from a killed process rather than a
/// peer. The margin is what protects a concurrent cold start: racing
/// processes see each other's temporaries as seconds old.
#[cfg(not(target_arch = "wasm32"))]
const STALE_TEMP_AGE: Duration = Duration::from_secs(600);

/// A loaded static embedding model in the model2vec artifact format.
///
/// Construct with [`Model2Vec::from_dir`]. Each artifact's bytes are
/// read once, hashed, and parsed from that same buffer (the read-then-
/// consume discipline `read_and_verify` established for UDPipe; nothing
/// re-reads disk between hash and load). The digest over all three
/// files is the model identity that provenance-carrying result types
/// report.
pub struct Model2Vec {
    /// Row-major `[vocab, dim]` embedding matrix, converted to f32.
    matrix: Vec<f32>,
    dim: usize,
    /// Optional per-token-id scale factors (tensor `"weights"`).
    weights: Option<Vec<f32>>,
    /// Optional token-id remap (tensor `"mapping"`): row = mapping[id].
    mapping: Option<Vec<usize>>,
    tokenizer: Tokenizer,
    /// Token id of the tokenizer's unknown token, dropped before pooling.
    unk_id: Option<u32>,
    /// Median byte length of vocabulary keys; drives character truncation.
    median_token_length: usize,
    /// `normalize` from config.json (default true): L2-normalize outputs.
    normalize: bool,
    /// SHA-256 over matrix, tokenizer, and config bytes, lowercase hex.
    model_hash: String,
}

impl Model2Vec {
    /// Load a model from a directory holding `model.safetensors`,
    /// `tokenizer.json`, and `config.json`.
    ///
    /// No network is touched, ever, whatever the directory holds:
    /// this constructor loads what the caller supplied and nothing else.
    /// [`Model2Vec::potion_base_8m`] is the one that provisions.
    ///
    /// Paths are trusted as given, matching the `Udpipe::from_path`
    /// precedent for caller-supplied model locations: no size cap (the
    /// artifacts are the model, and a huge one fails in safetensors
    /// validation, not in an OOM loop) and no symlink rejection.
    ///
    /// # Errors
    ///
    /// [`Error::ModelNotFound`] if a file is absent, [`Error::Io`] if one
    /// cannot be read, [`Error::ModelInvalid`] if the artifact does not
    /// parse, uses an embedding dtype other than f32, or panics the
    /// loader internally (the panic is caught at this boundary).
    pub fn from_dir(dir: impl AsRef<Path>) -> domain::Result<Self> {
        let dir = dir.as_ref();
        let [model_path, tokenizer_path, config_path] = ARTIFACT_FILES.map(|f| dir.join(f));
        for p in [&model_path, &tokenizer_path, &config_path] {
            if !p.exists() {
                return Err(Error::ModelNotFound(p.clone()));
            }
        }
        let model_bytes = fs::read(&model_path).map_err(|e| io_at("read", &model_path, &e))?;
        let tokenizer_bytes =
            fs::read(&tokenizer_path).map_err(|e| io_at("read", &tokenizer_path, &e))?;
        let config_bytes = fs::read(&config_path).map_err(|e| io_at("read", &config_path, &e))?;
        catch_embed_panic(|| Self::from_bytes(&model_bytes, &tokenizer_bytes, &config_bytes))
    }

    /// Load the pinned reference model from the models directory
    /// `models_dir`, downloading it first if it is not already there.
    ///
    /// `models_dir` is the directory `MATRA_MODEL_DIR` and
    /// [`Config::with_model_dir`] name, the one that holds every model:
    /// the three files live in its `potion-base-8M` subdirectory, which
    /// is where [`Model2Vec::from_config`] looks for them when the model
    /// directory is `models_dir` and `models.embedding` is as shipped.
    /// In the steps below, `dir` is that subdirectory.
    ///
    /// The bytes are trusted only if the SHA-256 over all three artifact
    /// files, in the order `model.safetensors`, `tokenizer.json`,
    /// `config.json`, equals a constant compiled into this file. That is the UDPipe discipline applied to the
    /// embedding model, and it is why "downloads a model" is not the
    /// same claim as "reaches the network for whatever is there":
    /// exactly one artifact set can ever load through this constructor,
    /// and it is named in the source. RFC-0010 decision 6 is amended to
    /// say so.
    ///
    /// The sequence, and the rule it turns on: this constructor only
    /// ever removes files it downloaded itself.
    ///
    /// 1. All three files present and matching the digest: the model
    ///    parses from those same buffers, and nothing is fetched.
    /// 2. All three present and not matching, or only some of them
    ///    present: [`Error::ModelInvalid`] naming `dir`. Nothing is
    ///    downloaded and nothing is removed. The three filenames belong
    ///    to the model2vec artifact format rather than to this one
    ///    model, so a caller who put their own artifacts there would
    ///    otherwise lose them, and a partial set has no provenance worth
    ///    trusting either.
    /// 3. None of the three present: all three are downloaded into
    ///    memory and checked against the pinned digest there. Only a set
    ///    that matched is written, as one transaction: each artifact
    ///    lands through a temporary file in `dir` (created if needed),
    ///    and the renames onto the artifact names run only once all
    ///    three temporaries are written. So no reader ever sees a
    ///    partial artifact, a partial set, or bytes that failed the pin.
    ///    A failure anywhere in the sequence removes every file the call
    ///    made and returns the error. A download is capped at 64 MiB.
    /// 4. A set that fails the digest is downloaded once more, and a
    ///    second mismatch returns [`Error::ModelInvalid`]. Neither
    ///    attempt wrote anything, so there is nothing to remove.
    ///
    /// So a caller either gets the pinned model or gets an error. A
    /// half-verified or partially-written model is never loaded, a
    /// failed download leaves nothing on disk for a later call to pick
    /// up, and bytes the provisioner did not write are never its to
    /// delete. [`Model2Vec::from_dir`] loads a directory of your own
    /// without any of this.
    ///
    /// **No TOCTOU window.** The bytes that satisfy the digest are the
    /// bytes that get parsed; nothing re-reads the directory between
    /// verify and load.
    ///
    /// `models_dir` is used as given; a `~` in it is not expanded.
    /// [`Model2Vec::from_config`] resolves the configured directory
    /// instead, and [`Model2Vec::from_dir`] loads three files from the
    /// directory that holds them.
    ///
    /// ```no_run
    /// use matra::config::Config;
    /// use matra::embed::model2vec::Model2Vec;
    ///
    /// // A directory the caller owns, because provisioning trusts what
    /// // it finds there; a shared one such as `/tmp` is writable by
    /// // anybody on the machine.
    /// let cfg = Config::resolve()?;
    /// // The files land in `<model_dir>/potion-base-8M/`.
    /// let model = Model2Vec::potion_base_8m(cfg.model_dir())?;
    /// # Ok::<(), matra::domain::Error>(())
    /// ```
    ///
    /// # Errors
    ///
    /// [`Error::Io`] if `dir` cannot be created, read, or written, and
    /// if a download fails at the transport or answers with a non-2xx
    /// status: the message names the URL, and the `io::ErrorKind`
    /// separates a timeout from an unreachable host.
    /// [`Error::InputTooLarge`] with `what` set to
    /// `"embedding_model_download"` if a response exceeds the 64 MiB cap.
    /// [`Error::ModelInvalid`] if `dir` already holds artifacts that are
    /// not the pinned set, if the digest still mismatches after one
    /// re-download, or if the verified bytes do not parse.
    #[cfg(not(target_arch = "wasm32"))]
    pub fn potion_base_8m(models_dir: impl AsRef<Path>) -> domain::Result<Self> {
        Self::potion_base_8m_with(&potion_dir(models_dir.as_ref()), fetch_capped, &mut |_| {})
    }

    /// [`Model2Vec::potion_base_8m`] over the directory a [`Config`]
    /// resolves: the model directory joined with the configured
    /// embedding model name.
    ///
    /// With the model directory and the embedding model name as shipped,
    /// this is `Model2Vec::potion_base_8m(cfg.model_dir())`. It exists so
    /// a caller with no opinion about where models live does not have to
    /// invent one, and so `models.embedding` is read where it applies.
    ///
    /// ```no_run
    /// use matra::embed::model2vec::Model2Vec;
    ///
    /// let cfg = matra::config::Config::resolve()?;
    /// let model = Model2Vec::from_config(&cfg)?;
    /// # Ok::<(), matra::domain::Error>(())
    /// ```
    ///
    /// # Errors
    ///
    /// Whatever [`Model2Vec::potion_base_8m`] returns.
    #[cfg(not(target_arch = "wasm32"))]
    pub fn from_config(cfg: &Config) -> domain::Result<Self> {
        Self::potion_base_8m_with(
            &cfg.model_dir().join(cfg.embedding_model()),
            fetch_capped,
            &mut |_| {},
        )
    }

    /// [`Model2Vec::from_config`], with `notice` called once before each
    /// fetch of the pinned set and not at all when the set is already on
    /// disk: [`Engine::from_config_with_notice`](crate::Engine::from_config_with_notice)
    /// for the embedding model.
    ///
    /// A first run downloads 30.9 MB across three files, and the library
    /// writes to no terminal, so the facts travel out as a
    /// [`domain::ProvisionNotice`] naming the model, its size, where it
    /// is going and its license, and the caller decides the wording. The
    /// three files are one notice, because they are fetched and verified
    /// as one set; a set that fails its digest is fetched once more, with
    /// a second notice.
    ///
    /// ```no_run
    /// use matra::embed::model2vec::Model2Vec;
    ///
    /// let cfg = matra::config::Config::resolve()?;
    /// let model = Model2Vec::from_config_with_notice(&cfg, |n| {
    ///     eprintln!("fetching {} into {}", n.artifact, n.destination.display());
    /// })?;
    /// # Ok::<(), matra::domain::Error>(())
    /// ```
    ///
    /// # Errors
    ///
    /// Whatever [`Model2Vec::potion_base_8m`] returns.
    #[cfg(not(target_arch = "wasm32"))]
    pub fn from_config_with_notice(
        cfg: &Config,
        mut notice: impl FnMut(&domain::ProvisionNotice),
    ) -> domain::Result<Self> {
        Self::potion_base_8m_with(
            &cfg.model_dir().join(cfg.embedding_model()),
            fetch_capped,
            &mut notice,
        )
    }

    /// [`Model2Vec::potion_base_8m`] with the fetcher and the notice as
    /// arguments. The public constructors pass [`fetch_capped`]; tests
    /// pass a closure, so the download path is exercised without a
    /// network.
    #[cfg(not(target_arch = "wasm32"))]
    fn potion_base_8m_with<F>(
        dir: &Path,
        fetch: F,
        notice: &mut dyn FnMut(&domain::ProvisionNotice),
    ) -> domain::Result<Self>
    where
        F: Fn(&str) -> domain::Result<Vec<u8>>,
    {
        Self::provision(dir, &POTION_BASE_8M, notice, &fetch)
    }

    /// The provisioning sequence [`Model2Vec::potion_base_8m`] documents,
    /// with what makes it "potion-base-8M" as arguments: the pin (its
    /// digest, URLs, size and license), the notice, and the fetcher.
    ///
    /// The pin is a parameter rather than a constant read from scope so
    /// that a test can pin a three-file fixture of its own. A test that
    /// could vary only the fetcher would be testing the fetcher; the
    /// behavior worth pinning is what the digest decides.
    ///
    /// Every path out of here leaves `dir` holding either the full pinned
    /// set or none of the three names. The set is fetched into memory
    /// and checked against the pin there ([`fetch_verified`]), so a
    /// mismatch is refused before the directory is touched at all, and
    /// [`install_artifacts`] is one transaction, so a write that fails
    /// partway leaves nothing behind. The alternative is a directory
    /// left holding one artifact, which every later call refuses as a
    /// partial set: a transient timeout would become a permanent failure
    /// that only a hand deletion clears.
    #[cfg(not(target_arch = "wasm32"))]
    fn provision<F>(
        dir: &Path,
        pin: &Pin<'_>,
        notice: &mut dyn FnMut(&domain::ProvisionNotice),
        fetch: &F,
    ) -> domain::Result<Self>
    where
        F: Fn(&str) -> domain::Result<Vec<u8>>,
    {
        let expected_digest = pin.digest;
        // Before the download rather than after it, so a directory that
        // cannot be created fails in milliseconds instead of after
        // 30 MB has travelled. `nlp/udpipe.rs` orders it the same way.
        fs::create_dir_all(dir).map_err(|e| io_at("create the model directory", dir, &e))?;
        let paths: [PathBuf; 3] = ARTIFACT_FILES.map(|f| dir.join(f));
        let present = paths.iter().filter(|p| p.exists()).count();

        // Files this call did not write are never files it removes. The
        // three names are the artifact format's, not this model's, so a
        // caller who pointed the configured embedding name at a
        // directory holding their own model has a full set sitting here.
        if present == ARTIFACT_FILES.len() {
            return match read_verify_load(&paths, expected_digest)? {
                Some(model) => Ok(model),
                None => Err(not_the_pinned_model(
                    dir,
                    "the three artifacts already there are not the pinned reference model",
                    expected_digest,
                )),
            };
        }
        if present > 0 {
            return Err(not_the_pinned_model(
                dir,
                "only part of an artifact set is there, and a partial set carries no provenance",
                expected_digest,
            ));
        }

        // None of the three names is taken, so the directory is this
        // call's to fill, and what lands in it is this call's to remove.
        // The set is verified in memory first, so nothing that failed
        // the pin ever carries an artifact's name, not even for the
        // length of a rename, and a mismatch costs the directory
        // nothing.
        let artifacts = fetch_verified(dir, pin, notice, fetch)?;
        install_artifacts(dir, &paths, &artifacts)?;
        // The bytes that satisfied the digest are the bytes that parse.
        // Nothing re-reads the directory between the two.
        catch_embed_panic(|| Model2Vec::from_bytes(&artifacts[0], &artifacts[1], &artifacts[2]))
    }

    /// Parse a model from in-memory artifact bytes. The identity hash is
    /// computed here over the same buffers that get parsed, so it always
    /// matches what was loaded.
    fn from_bytes(
        model_bytes: &[u8],
        tokenizer_bytes: &[u8],
        config_bytes: &[u8],
    ) -> domain::Result<Self> {
        // All three artifacts determine the output vectors (the tokenizer
        // decides which rows pool, config's normalize flips the geometry),
        // so the identity covers all three, in this order.
        let mut hasher = Sha256::new();
        hasher.update(model_bytes);
        hasher.update(tokenizer_bytes);
        hasher.update(config_bytes);
        let model_hash = hex(&hasher.finalize());

        let tensors = SafeTensors::deserialize(model_bytes)
            .map_err(|e| Error::ModelInvalid(format!("safetensors: {e}")))?;
        let names = tensors.names();
        let emb_name = ["embeddings", "0", "embedding.weight"]
            .into_iter()
            .find(|n| names.iter().any(|t| t == n))
            .ok_or_else(|| {
                Error::ModelInvalid(
                    "no embeddings tensor (tried: embeddings, 0, embedding.weight)".into(),
                )
            })?;
        let emb = tensors
            .tensor(emb_name)
            .map_err(|e| Error::ModelInvalid(format!("embeddings tensor: {e}")))?;
        if emb.shape().len() != 2 {
            return Err(Error::ModelInvalid(format!(
                "embeddings tensor must be 2-dimensional, got shape {:?}",
                emb.shape()
            )));
        }
        let (vocab, dim) = (emb.shape()[0], emb.shape()[1]);
        if vocab == 0 || dim == 0 {
            return Err(Error::ModelInvalid(format!(
                "embeddings tensor has a zero dimension: shape {vocab}x{dim}"
            )));
        }
        let matrix = f32_tensor(emb.dtype(), emb.data(), "embeddings")?;
        if matrix.len() != vocab * dim {
            return Err(Error::ModelInvalid(format!(
                "embeddings tensor data length {} does not match shape {}x{}",
                matrix.len(),
                vocab,
                dim
            )));
        }

        let weights = match tensors.tensor("weights") {
            Ok(t) => Some(f32_tensor(t.dtype(), t.data(), "weights")?),
            Err(_) => None,
        };
        let mapping = match tensors.tensor("mapping") {
            Ok(t) => Some(index_tensor(t.dtype(), t.data())?),
            Err(_) => None,
        };
        // Malformation is loud at load, not silent at embed. Every mapping
        // entry must be a valid matrix row, and a weights table must cover
        // the id space it is indexed by (token ids, so the mapping's
        // length when one exists, the matrix vocab otherwise). The Python
        // reference tolerates short weights by defaulting to 1.0; a
        // half-scaled artifact is a defect, not a model, so matra rejects
        // it (resilience floor over quiet parity on malformed input).
        if let Some(map) = &mapping
            && let Some(&bad) = map.iter().find(|&&row| row >= vocab)
        {
            return Err(Error::ModelInvalid(format!(
                "mapping entry {bad} outside matrix vocab {vocab}"
            )));
        }
        if let Some(w) = &weights {
            let id_space = mapping.as_ref().map_or(vocab, Vec::len);
            if w.len() < id_space {
                return Err(Error::ModelInvalid(format!(
                    "weights tensor length {} does not cover the token id space {id_space}",
                    w.len()
                )));
            }
        }

        let tokenizer = Tokenizer::from_bytes(tokenizer_bytes)
            .map_err(|e| Error::ModelInvalid(format!("tokenizer: {e}")))?;

        // The unknown token lives in tokenizer.json under the model key;
        // the tokenizers API exposes no direct accessor, so read it from
        // the same JSON the tokenizer was built from. WordPiece/BPE store
        // it as a string (`unk_token`); Unigram stores an index
        // (`unk_id`). Missing both means no unk filtering, which is the
        // reference behavior for such tokenizers.
        let tokenizer_json: serde_json::Value = serde_json::from_slice(tokenizer_bytes)
            .map_err(|e| Error::ModelInvalid(format!("tokenizer.json: {e}")))?;
        let model_obj = tokenizer_json.get("model");
        let unk_id = model_obj
            .and_then(|m| m.get("unk_token"))
            .and_then(|u| u.as_str())
            .and_then(|s| tokenizer.token_to_id(s))
            .or_else(|| {
                model_obj
                    .and_then(|m| m.get("unk_id"))
                    .and_then(serde_json::Value::as_u64)
                    .and_then(|v| u32::try_from(v).ok())
            });

        let mut lengths: Vec<usize> = tokenizer
            .get_vocab(false)
            .keys()
            .map(|token| token.len())
            .collect();
        lengths.sort_unstable();
        // Upper middle on even-length vocabularies, matching the Rust
        // reference; only shifts the truncation heuristic on long inputs.
        let median_token_length = lengths.get(lengths.len() / 2).copied().unwrap_or(1).max(1);

        let config: serde_json::Value = serde_json::from_slice(config_bytes)
            .map_err(|e| Error::ModelInvalid(format!("config.json: {e}")))?;
        let normalize = config
            .get("normalize")
            .and_then(serde_json::Value::as_bool)
            .unwrap_or(true);

        Ok(Self {
            matrix,
            dim,
            weights,
            mapping,
            tokenizer,
            unk_id,
            median_token_length,
            normalize,
            model_hash,
        })
    }

    /// SHA-256 over the three artifact files (matrix, tokenizer, config,
    /// in that order), lowercase hex. The model identity for
    /// provenance-carrying result types; all three files determine the
    /// output vectors, so all three are covered.
    #[must_use]
    pub fn model_hash(&self) -> &str {
        &self.model_hash
    }

    /// Number of dimensions of every vector this model produces.
    #[must_use]
    pub fn dimensions(&self) -> usize {
        self.dim
    }

    /// Truncate to at most `max_bytes` bytes on a char boundary, the
    /// reference implementation's cheap pre-tokenization cap.
    fn truncate_str(text: &str, max_bytes: usize) -> &str {
        if text.len() <= max_bytes {
            return text;
        }
        let mut end = max_bytes;
        while end > 0 && !text.is_char_boundary(end) {
            end -= 1;
        }
        &text[..end]
    }

    /// Embed one text: truncate, tokenize without special tokens, drop
    /// unknown tokens, cap at [`DEFAULT_MAX_TOKENS`], gather rows
    /// (through the mapping remap when present), scale by per-token
    /// weights when present, mean-pool, and L2-normalize if configured.
    /// An empty token list yields the zero vector, matching the
    /// reference.
    fn embed_one(&self, text: &str) -> domain::Result<Embedding> {
        let truncated = Self::truncate_str(
            text,
            DEFAULT_MAX_TOKENS.saturating_mul(self.median_token_length),
        );
        let encoding = self
            .tokenizer
            .encode_fast(truncated, false)
            .map_err(|e| Error::ModelInvalid(format!("tokenize: {e}")))?;
        let mut ids: Vec<u32> = encoding.get_ids().to_vec();
        if let Some(unk) = self.unk_id {
            ids.retain(|&id| id != unk);
        }
        ids.truncate(DEFAULT_MAX_TOKENS);

        let vocab = self.matrix.len() / self.dim;
        let mut sum = vec![0.0f32; self.dim];
        let mut count = 0usize;
        for &id in &ids {
            let row = match &self.mapping {
                Some(map) => match map.get(id as usize) {
                    Some(&mapped) => mapped,
                    None => {
                        return Err(Error::ModelInvalid(format!(
                            "token id {id} outside mapping (len {})",
                            map.len()
                        )));
                    }
                },
                None => id as usize,
            };
            if row >= vocab {
                return Err(Error::ModelInvalid(format!(
                    "token id {id} maps to row {row} outside vocab {vocab}"
                )));
            }
            // Indexed by original token id, per the reference; load-time
            // validation makes a miss unreachable, so 1.0 is defensive,
            // not a silent tolerance.
            let scale = self
                .weights
                .as_ref()
                .and_then(|w| w.get(id as usize))
                .copied()
                .unwrap_or(1.0);
            let base = row * self.dim;
            for (s, v) in sum.iter_mut().zip(&self.matrix[base..base + self.dim]) {
                *s += v * scale;
            }
            count += 1;
        }
        let denom = count.max(1) as f32;
        for s in &mut sum {
            *s /= denom;
        }
        if self.normalize {
            let norm = sum.iter().map(|v| v * v).sum::<f32>().sqrt().max(1e-12);
            for s in &mut sum {
                *s /= norm;
            }
        }
        Ok(Embedding(sum))
    }
}

/// The component name a [`Model2Vec`] reports in its identity.
const EMBEDDER_NAME: &str = "model2vec";

impl Embedder for Model2Vec {
    fn embed(&self, texts: &[&str]) -> domain::Result<Vec<Embedding>> {
        catch_embed_panic(|| texts.iter().map(|t| self.embed_one(t)).collect())
    }

    /// `model2vec`, with [`Model2Vec::model_hash`] as the digest. The
    /// model's name and licence are known only when the digest is the
    /// pinned reference model's, however it was loaded; a set of the
    /// caller's own leaves both `None`, as a parser model matra does not
    /// pin does.
    fn identity(&self) -> domain::Identity {
        let pinned = self.model_hash.eq_ignore_ascii_case(POTION_BASE_8M_SHA256);
        domain::Identity::new(
            EMBEDDER_NAME,
            pinned.then(|| POTION_BASE_8M_NAME.to_string()),
            Some(self.model_hash.clone()),
            pinned.then(|| POTION_BASE_8M_LICENSE.to_string()),
        )
    }
}

/// Read the three artifacts once, hash those buffers, and parse the model
/// from the same buffers when the digest matches.
///
/// Returns `Ok(None)` on a digest mismatch, which is the caller's signal
/// to remove and retry. Verification happens before parsing rather than
/// after, and not only for the obvious reason: if the parse ran first, a
/// corrupt artifact would fail as [`Error::ModelInvalid`] from
/// safetensors and never reach the retry at all.
///
/// [`Model2Vec::from_bytes`] hashes the same three buffers again to
/// compute the model identity. That is one duplicated pass over 30 MB,
/// kept because the two hashes answer different questions: this one is a
/// gate on bytes that are not yet trusted, the other is a property of a
/// model that loaded.
#[cfg(not(target_arch = "wasm32"))]
fn read_verify_load(
    paths: &[PathBuf; 3],
    expected_digest: &str,
) -> domain::Result<Option<Model2Vec>> {
    let mut artifacts: [Vec<u8>; 3] = Default::default();
    for (slot, path) in artifacts.iter_mut().zip(paths) {
        *slot = fs::read(path).map_err(|e| io_at("read", path, &e))?;
    }

    if !digest_of(&artifacts).eq_ignore_ascii_case(expected_digest) {
        return Ok(None);
    }

    catch_embed_panic(|| Model2Vec::from_bytes(&artifacts[0], &artifacts[1], &artifacts[2]))
        .map(Some)
}

/// The pin's subject: SHA-256 over the three artifacts, concatenated in
/// [`ARTIFACT_FILES`] order.
///
/// One digest over the set rather than three, because a model is the
/// set: a tokenizer from one release beside a matrix from another is not
/// a model anybody pinned. One function so the disk path and the
/// download path cannot disagree about what the pin covers.
#[cfg(not(target_arch = "wasm32"))]
fn digest_of(artifacts: &[Vec<u8>; 3]) -> String {
    let mut hasher = Sha256::new();
    for bytes in artifacts {
        hasher.update(bytes);
    }
    hex(&hasher.finalize())
}

/// The refusal for a directory the provisioner did not fill itself.
///
/// It names the directory, the reason, and every way out, because the
/// caller reached a constructor that provisions and was told no: either
/// those files are a model of their own (load them with
/// [`Model2Vec::from_dir`]), or they are a stale cache (remove them), or
/// the directory is the wrong one (point the configuration elsewhere).
#[cfg(not(target_arch = "wasm32"))]
fn not_the_pinned_model(dir: &Path, reason: &str, expected_digest: &str) -> Error {
    Error::ModelInvalid(format!(
        "{}: {reason} (expected the three-file SHA-256 {expected_digest}). \
         Nothing was downloaded and nothing was removed. Load a model of your own with \
         Model2Vec::from_dir, remove the files there to provision the pinned model again, \
         or point MATRA_MODEL_DIR or the configured embedding model name at another \
         directory.",
        dir.display()
    ))
}

/// A filesystem failure that names the operation and the path.
///
/// The counterpart of the function of the same name in `nlp/udpipe.rs`,
/// and it exists for the same reason: `io error: No space left on device
/// (os error 28)` was the whole message an embedding install produced,
/// with the path sitting in a variable one line away. The sentence is
/// written once, in `IoContext::cannot` in the domain, which also carries
/// the path as a detail; this wrapper only keeps the call sites short.
fn io_at(operation: &str, path: &Path, error: &std::io::Error) -> Error {
    Error::Io(crate::domain::IoContext::cannot(operation, path, error))
}

/// Remove the named paths, ignoring failures. A file that is already
/// gone is the state this wants; one that cannot be removed surfaces at
/// the next verify, which is the loud place for it.
///
/// The one cleanup in this file: [`Transaction`] unwinds a failed
/// install with it. A mismatched download needs no cleanup, because the
/// digest is checked before anything is written.
#[cfg(not(target_arch = "wasm32"))]
fn remove_artifacts(paths: &[PathBuf]) {
    for path in paths {
        let _ = fs::remove_file(path);
    }
}

/// The paths one download created, removed together unless every step of
/// that download succeeded.
///
/// A download is all three artifacts or none of them. The three names
/// appearing one at a time would leave a window in which the directory
/// holds a partial set, and a partial set is what
/// [`Model2Vec::provision`] refuses on the next call: a fetch that timed
/// out on the second artifact would otherwise wedge the directory until
/// somebody deleted files by hand. So every fetch and every write happens
/// first, into temporaries, and the renames start only once nothing is
/// left that can fail on a fetch.
///
/// Cleanup runs from `Drop`, so a panic between the first write and the
/// commit removes what this call made, exactly as an error does. It only
/// ever names paths this call created: a temporary whose open failed was
/// never registered, so a file somebody else put at that name is left
/// exactly as it was found.
#[cfg(not(target_arch = "wasm32"))]
struct Transaction {
    /// Temporaries this call opened.
    temps: Vec<PathBuf>,
    /// Artifacts this call renamed into place.
    landed: Vec<PathBuf>,
    committed: bool,
}

#[cfg(not(target_arch = "wasm32"))]
impl Transaction {
    fn new() -> Transaction {
        Transaction {
            temps: Vec::new(),
            landed: Vec::new(),
            committed: false,
        }
    }

    /// Write `bytes` to `tmp`, which must not already exist.
    ///
    /// The temporary is opened with `create_new`, which is `O_EXCL`: a
    /// path that already exists fails the open, symlink or not. A name
    /// in a directory somebody else can write to is otherwise a place to
    /// plant a symlink, and a plain write would follow it and put 30 MB
    /// through to whatever it points at. Failing is the right answer and
    /// the message names the path. A leftover from a killed process is
    /// no longer how this is normally met: [`reclaim_stale_temps`]
    /// clears one that is too old to be live, and [`temp_stamp`] keeps
    /// two calls from choosing one name.
    fn write_temp(&mut self, tmp: &Path, bytes: &[u8]) -> domain::Result<()> {
        use std::io::Write;

        let mut file = fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(tmp)
            .map_err(|e| {
                domain::IoContext::at_path(
                    e.kind(),
                    tmp,
                    format!(
                        "cannot create the temporary file {}: {e} (a leftover from a killed \
                         process, or one planted there; remove it and try again)",
                        tmp.display()
                    ),
                )
            })?;
        // Registered only once the file is this call's own, so a path
        // that was already there is never this call's to remove.
        self.temps.push(tmp.to_path_buf());
        file.write_all(bytes)
            .map_err(|e| io_at("write the artifact to", tmp, &e))?;
        Ok(())
    }

    /// Move one written temporary onto its artifact name.
    ///
    /// `std::fs::rename` is atomic on one filesystem (POSIX `rename(2)`,
    /// Windows `MoveFileExW` with `MOVEFILE_REPLACE_EXISTING`), so no
    /// reader sees a half-written artifact, and two processes
    /// provisioning into the same directory cannot interleave: the
    /// temporary name is that call's alone ([`temp_stamp`]) and the
    /// rename is the only operation that touches the final path.
    fn rename_into_place(&mut self, tmp: &Path, final_path: &Path) -> domain::Result<()> {
        fs::rename(tmp, final_path)
            .map_err(|e| io_at("move the artifact into place at", final_path, &e))?;
        self.landed.push(final_path.to_path_buf());
        Ok(())
    }

    /// All three artifacts are in place. Nothing is removed from here on.
    fn commit(&mut self) {
        self.committed = true;
    }
}

#[cfg(not(target_arch = "wasm32"))]
impl Drop for Transaction {
    fn drop(&mut self) {
        if self.committed {
            return;
        }
        remove_artifacts(&self.temps);
        remove_artifacts(&self.landed);
    }
}

/// Fetch all three artifacts into memory and return them only if they
/// are the pinned set.
///
/// Two attempts. The likely cause of a first mismatch is a truncated or
/// intercepted transfer rather than a changed upstream, and one retry
/// costs a bounded read; a second failure is not a transient, so it
/// returns [`Error::ModelInvalid`]. Nothing has been written at any
/// point, so a mismatch leaves the model directory exactly as it was.
///
/// The digest covers the three artifacts as a set, so a mismatch
/// condemns the set: the retry refetches all three rather than the one
/// that might have drifted.
///
/// `notice` is called before each round, so a caller can say what is
/// coming before the wait rather than after it. One notice per round,
/// not per file: the three are one set, fetched and verified together.
#[cfg(not(target_arch = "wasm32"))]
fn fetch_verified<F>(
    dir: &Path,
    pin: &Pin<'_>,
    notice: &mut dyn FnMut(&domain::ProvisionNotice),
    fetch: &F,
) -> domain::Result<[Vec<u8>; 3]>
where
    F: Fn(&str) -> domain::Result<Vec<u8>>,
{
    let expected_digest = pin.digest;
    for _ in 0..2 {
        notice(&domain::ProvisionNotice {
            artifact: pin.name.to_string(),
            bytes: pin.bytes,
            destination: dir.to_path_buf(),
            license: pin.license.to_string(),
            license_url: pin.license_url.to_string(),
        });
        let artifacts = fetch_all(&pin.urls, fetch)?;
        if digest_of(&artifacts).eq_ignore_ascii_case(expected_digest) {
            return Ok(artifacts);
        }
    }
    Err(Error::ModelInvalid(format!(
        "artifact digest mismatch after re-download in {}: expected {expected_digest}",
        dir.display()
    )))
}

/// One round of three fetches, held in memory.
///
/// The size cap is enforced here rather than inside the fetcher, at the
/// one place every artifact passes through: the fetcher is replaceable
/// and the bound is not.
#[cfg(not(target_arch = "wasm32"))]
fn fetch_all<F>(urls: &[&str; 3], fetch: &F) -> domain::Result<[Vec<u8>; 3]>
where
    F: Fn(&str) -> domain::Result<Vec<u8>>,
{
    let mut artifacts: [Vec<u8>; 3] = Default::default();
    for (slot, url) in artifacts.iter_mut().zip(urls) {
        let bytes = fetch(url)?;
        if bytes.len() > MAX_ARTIFACT_BYTES {
            return Err(Error::InputTooLarge {
                limit: MAX_ARTIFACT_BYTES,
                actual: bytes.len(),
                what: "embedding_model_download",
            });
        }
        *slot = bytes;
    }
    Ok(artifacts)
}

/// Put three verified artifacts in place, all of them or none.
///
/// One transaction: each lands in a temporary in `dir` first, and only
/// when all three are written does a rename put any of them under an
/// artifact name, so the set becomes visible as a set. A failure at any
/// point (a temporary that cannot be opened, a write, a rename) removes
/// every temporary and every artifact this call had already renamed,
/// then returns the error, so the directory is left as this call found
/// it.
///
/// The bytes arriving here have already satisfied the pin. Until the
/// review of #77 the order was the other way round: all three were
/// written and renamed onto the artifact names and the digest was
/// consulted only afterwards, so unverified bytes reached the model
/// directory under their real names.
///
/// This is deliberately a second implementation of the temp-then-rename
/// pattern `nlp/udpipe.rs` uses rather than a shared helper. The two
/// adapters share no module by design, and a utility module both imported
/// would put a third file into the wiring to save twenty lines.
#[cfg(not(target_arch = "wasm32"))]
fn install_artifacts(
    dir: &Path,
    paths: &[PathBuf; 3],
    artifacts: &[Vec<u8>; 3],
) -> domain::Result<()> {
    reclaim_stale_temps(dir, SystemTime::now());

    let stamp = temp_stamp();
    let temps: [PathBuf; 3] =
        ARTIFACT_FILES.map(|name| dir.join(format!("{TEMP_FILE_PREFIX}{name}.{stamp}")));
    let mut transaction = Transaction::new();

    for (tmp, bytes) in temps.iter().zip(artifacts) {
        transaction.write_temp(tmp, bytes)?;
    }

    // Past this line nothing can fail on a fetch or a write, so every
    // rename runs against bytes already on disk.
    for (tmp, final_path) in temps.iter().zip(paths) {
        transaction.rename_into_place(tmp, final_path)?;
    }
    transaction.commit();
    Ok(())
}

/// A name fragment no call in flight will pick twice: the process id,
/// the wall clock in nanoseconds, and a counter for two calls in one
/// process that read the same nanosecond.
///
/// The process id alone was the previous answer and it is not unique. In
/// a container every process is pid 1, so two cold starts sharing a
/// bind-mounted model directory choose the same temporary names, and
/// `create_new` refuses the second one. `nlp/udpipe.rs` names its
/// temporary directory the same way and for the same reason.
#[cfg(not(target_arch = "wasm32"))]
fn temp_stamp() -> String {
    static COUNTER: AtomicU64 = AtomicU64::new(0);
    let nanos = SystemTime::now()
        .duration_since(SystemTime::UNIX_EPOCH)
        .map_or(0, |d| d.as_nanos() as u64);
    format!(
        "{}.{nanos:x}.{:x}",
        std::process::id(),
        COUNTER.fetch_add(1, Ordering::Relaxed)
    )
}

/// Remove every temporary in `dir` too old to belong to a running
/// provisioning call.
///
/// `now` is a parameter so the rule is testable without touching
/// modification times on disk. A file whose modification time is in the
/// future (a clock that moved, a copied tree) is left alone: an
/// unexplained timestamp is a reason not to delete, not a reason to.
///
/// Without this, a run killed between the first write and the last
/// rename left a temporary that `create_new` would refuse for ever
/// after. The name carried the process id, and in a container every
/// process is pid 1, so one interrupt wedged every later embedding
/// provision on that machine until somebody deleted the file by hand.
/// `nlp/udpipe.rs` reclaims its own temporaries by the same rule and the
/// same threshold.
///
/// Failures are ignored throughout. Reclaiming a leftover is tidying,
/// and a file that cannot be listed or removed must not turn a working
/// download into an error.
#[cfg(not(target_arch = "wasm32"))]
fn reclaim_stale_temps(dir: &Path, now: SystemTime) {
    let Ok(entries) = fs::read_dir(dir) else {
        return;
    };
    for entry in entries.flatten() {
        let name = entry.file_name();
        let Some(name) = name.to_str() else { continue };
        if !name.starts_with(TEMP_FILE_PREFIX) {
            continue;
        }
        // `DirEntry::metadata` does not follow a symlink, and
        // `remove_file` removes the link rather than its target, so a
        // planted link is reclaimed as itself or not at all.
        let stale = entry
            .metadata()
            .and_then(|m| m.modified())
            .ok()
            .and_then(|modified| now.duration_since(modified).ok())
            .is_some_and(|age| age >= STALE_TEMP_AGE);
        if stale {
            let _ = fs::remove_file(entry.path());
        }
    }
}

/// Fetch one URL into memory, reading at most one byte past
/// [`MAX_ARTIFACT_BYTES`].
///
/// Stopping one byte over is what lets the caller tell "at the cap" from
/// "over it" while keeping the read bounded: an endless or misdirected
/// response costs 64 MiB and a rejection, not the machine's memory. The
/// consequence is that the `actual` on the resulting
/// [`Error::InputTooLarge`] is the bound that was breached rather than
/// the response's full length, which is not knowable without reading it.
///
/// `ureq` treats a non-2xx status as an error by default, so an HTML
/// error page never reaches the digest.
///
/// The call is bounded in time as well as in size: [`FETCH_TIMEOUT`] end
/// to end and [`CONNECT_TIMEOUT`] on the connection, because `ureq` sets
/// no timeout of its own and a socket that accepts and then says nothing
/// would otherwise block the caller for as long as it stays open.
#[cfg(not(target_arch = "wasm32"))]
fn fetch_capped(url: &str) -> domain::Result<Vec<u8>> {
    use std::io::Read;

    let response = download_agent()
        .get(url)
        .call()
        .map_err(|e| transport_failure(url, &e))?;
    let mut bytes = Vec::new();
    response
        .into_body()
        .into_reader()
        .take(MAX_ARTIFACT_BYTES as u64 + 1)
        .read_to_end(&mut bytes)
        .map_err(|e| body_failure(url, e))?;
    Ok(bytes)
}

/// The client [`fetch_capped`] downloads through.
///
/// A function rather than an inline builder so a test can assert the
/// configuration without a network. `https_only` is the part worth
/// asserting: `ureq` follows up to ten redirects by default, so without
/// it an `https` URL that redirects to `http` is fetched in cleartext.
/// The pinned digest means that cannot change which bytes load, so this
/// is confidentiality rather than integrity, but a redirect is not the
/// user's decision to make and the pinned URLs are all `https`.
#[cfg(not(target_arch = "wasm32"))]
fn download_agent() -> ureq::Agent {
    ureq::Agent::config_builder()
        .timeout_global(Some(FETCH_TIMEOUT))
        .timeout_connect(Some(CONNECT_TIMEOUT))
        .https_only(true)
        .build()
        .into()
}

/// Map a failure part-way through reading the response body.
///
/// The body reader hands back an `io::Error`, and `ureq` builds that one
/// with `Error::into_io`, which returns the inner error only for
/// `Error::Io` and wraps everything else, `Timeout` included, in
/// `io::Error::other`. Reading `kind()` straight off it therefore gives
/// `Other`, so a fetch that ran past [`FETCH_TIMEOUT`] mid-transfer
/// reported `Other` where `site/content/reference/errors.md` and RFC-0015
/// both promise `TimedOut`. `From<io::Error> for ureq::Error` unwraps
/// the wrapped error again, which recovers the kind and also gives a
/// mid-stream certificate rejection the sentence [`download_message`]
/// writes. An `io::Error` that was never a `ureq::Error` comes back as
/// `Error::Io` and keeps its own kind.
#[cfg(not(target_arch = "wasm32"))]
fn body_failure(url: &str, error: std::io::Error) -> Error {
    transport_failure(url, &ureq::Error::from(error))
}

/// Map a `ureq` failure to [`Error::Io`].
///
/// A download that never arrives is a transport failure, not an invalid
/// model: [`Error::ModelInvalid`] stays reserved for bytes that did
/// arrive and then failed the digest or the parser. A non-2xx status
/// belongs on this side too, because what came back was a server's
/// answer about the request rather than a model. The `io::ErrorKind` is
/// preserved where `ureq` knows it, so a caller can tell a timeout from
/// an unreachable host without reading the message, and the failure is
/// marked retryable (EPR-0006): nothing was written, so the same call can
/// be made again. A rejected certificate is the exception, since its
/// message names a remedy no retry supplies.
#[cfg(not(target_arch = "wasm32"))]
fn transport_failure(url: &str, error: &ureq::Error) -> Error {
    use std::io::ErrorKind;

    let kind = match error {
        ureq::Error::Io(e) => e.kind(),
        ureq::Error::Timeout(_) => ErrorKind::TimedOut,
        ureq::Error::ConnectionFailed | ureq::Error::HostNotFound => ErrorKind::NotConnected,
        _ => ErrorKind::Other,
    };
    let retryable = !is_certificate_rejection(&error.to_string(), error);
    Error::Io(crate::domain::IoContext::download(
        kind,
        url,
        download_message(url, error),
        retryable,
    ))
}

/// What the user reads when an artifact download fails.
///
/// A rejected certificate gets a sentence rather than a `Debug`
/// rendering of a `rustls` enum. matra verifies TLS against root
/// certificates compiled into the binary and never reads the system
/// trust store, which is why it needs no `ca-certificates` package and
/// also why installing a proxy's CA there changes nothing. That is one
/// fact about matra rather than one about this adapter, so both
/// provisioning paths say it (RFC-0015); the wording differs only in the
/// way out, because this model is a directory of three artifacts rather
/// than one file. The raw failure is kept at the end, because a bug
/// report needs it.
#[cfg(not(target_arch = "wasm32"))]
fn download_message(url: &str, error: &ureq::Error) -> String {
    let detail = error.to_string();
    if !is_certificate_rejection(&detail, error) {
        return format!("download {url}: {detail}");
    }
    format!(
        "download {url}: the TLS certificate offered for {} was rejected. matra verifies \
         TLS against root certificates compiled into it and never reads the system trust \
         store, so a proxy that re-signs TLS cannot be trusted by installing its CA. \
         Fetch the artifacts by hand into a directory of your own and load it with \
         Model2Vec::from_dir instead. Underlying failure: {detail}",
        host_of(url),
    )
}

/// Whether a transport failure is the peer's certificate being refused.
///
/// `ureq` surfaces a `rustls` handshake failure as `Error::Io` wrapping
/// an `io::Error` whose message is the `rustls` error, so the variant
/// alone does not say. The rendered text does.
#[cfg(not(target_arch = "wasm32"))]
fn is_certificate_rejection(detail: &str, error: &ureq::Error) -> bool {
    if matches!(error, ureq::Error::Tls(_)) {
        return true;
    }
    let lowered = detail.to_ascii_lowercase();
    lowered.contains("certificate") || lowered.contains("rustls")
}

/// The host in an absolute URL, or the whole URL when it has no
/// recognisable authority. Enough for a message; not a URL parser.
#[cfg(not(target_arch = "wasm32"))]
fn host_of(url: &str) -> &str {
    let after_scheme = url.split_once("://").map_or(url, |(_, rest)| rest);
    let authority = after_scheme
        .split(['/', '?', '#'])
        .next()
        .unwrap_or(after_scheme);
    let host = authority.rsplit_once('@').map_or(authority, |(_, h)| h);
    host.split(':').next().unwrap_or(host)
}

/// Convert a tensor's raw little-endian bytes to f32. Only f32 sources
/// are supported; f16 and i8 artifacts exist in the wild and return
/// [`Error::ModelInvalid`] with the dtype named, so the gap is loud
/// rather than silently wrong.
fn f32_tensor(dtype: Dtype, data: &[u8], name: &str) -> domain::Result<Vec<f32>> {
    match dtype {
        Dtype::F32 => Ok(data
            .as_chunks::<4>()
            .0
            .iter()
            .map(|b| f32::from_le_bytes(*b))
            .collect()),
        other => Err(Error::ModelInvalid(format!(
            "{name} tensor dtype {other:?} not supported (f32 only in this build)"
        ))),
    }
}

/// Convert an i32/i64 index tensor to usize rows, rejecting negatives.
fn index_tensor(dtype: Dtype, data: &[u8]) -> domain::Result<Vec<usize>> {
    let to_usize = |v: i64| -> domain::Result<usize> {
        usize::try_from(v).map_err(|_| Error::ModelInvalid(format!("negative mapping index {v}")))
    };
    match dtype {
        Dtype::I32 => data
            .as_chunks::<4>()
            .0
            .iter()
            .map(|b| to_usize(i64::from(i32::from_le_bytes(*b))))
            .collect(),
        Dtype::I64 => data
            .as_chunks::<8>()
            .0
            .iter()
            .map(|b| to_usize(i64::from_le_bytes(*b)))
            .collect(),
        other => Err(Error::ModelInvalid(format!(
            "mapping tensor dtype {other:?} not supported (i32/i64 only)"
        ))),
    }
}

/// Run a closure that may panic inside the safetensors or tokenizers
/// parsing paths, converting any panic to [`Error::ModelInvalid`]. Both
/// crates are pure Rust, so a panic here is a Rust panic on malformed
/// input, not a C abort; the boundary exists because library code must
/// not panic (the UDPipe precedent, same shape).
fn catch_embed_panic<F, T>(f: F) -> domain::Result<T>
where
    F: FnOnce() -> domain::Result<T>,
{
    use std::panic::{AssertUnwindSafe, catch_unwind};
    match catch_unwind(AssertUnwindSafe(f)) {
        Ok(result) => result,
        Err(payload) => {
            let message = payload
                .downcast_ref::<&'static str>()
                .map(|s| (*s).to_string())
                .or_else(|| payload.downcast_ref::<String>().cloned())
                .unwrap_or_else(|| "model2vec panic (no message captured)".to_string());
            Err(Error::ModelInvalid(format!(
                "model2vec panicked: {message}"
            )))
        }
    }
}

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

    /// The normalized tiny model, shared with the parity module.
    pub(super) fn write_model_normalized(dir: &Path) {
        write_model(dir, None, r#"{"normalize": true}"#);
    }
    use safetensors::tensor::TensorView;
    use std::collections::HashMap;

    /// Vocab: [UNK]=0, hello=1, world=2. Row 0 is poison so any test
    /// that pools it fails loudly.
    const MATRIX: [[f32; 4]; 3] = [
        [9.0, 9.0, 9.0, 9.0],
        [1.0, 0.0, 2.0, 0.0],
        [0.0, 2.0, 0.0, 4.0],
    ];

    const TOKENIZER_JSON: &str = r###"{
        "version": "1.0",
        "truncation": null,
        "padding": null,
        "added_tokens": [],
        "normalizer": {"type": "BertNormalizer", "clean_text": true,
            "handle_chinese_chars": true, "strip_accents": null,
            "lowercase": true},
        "pre_tokenizer": {"type": "BertPreTokenizer"},
        "post_processor": null,
        "decoder": null,
        "model": {"type": "WordPiece", "unk_token": "[UNK]",
            "continuing_subword_prefix": "##",
            "max_input_chars_per_word": 100,
            "vocab": {"[UNK]": 0, "hello": 1, "world": 2}}
    }"###;

    fn tensor_bytes(rows: &[[f32; 4]]) -> Vec<u8> {
        rows.iter()
            .flatten()
            .flat_map(|v| v.to_le_bytes())
            .collect()
    }

    fn write_model_full(
        dir: &Path,
        weights: Option<&[f32]>,
        mapping: Option<&[i64]>,
        config: &str,
    ) {
        let emb = tensor_bytes(&MATRIX);
        let mut tensors = HashMap::new();
        tensors.insert(
            "embeddings".to_string(),
            TensorView::new(Dtype::F32, vec![3, 4], &emb).unwrap(),
        );
        let wbytes: Vec<u8>;
        if let Some(w) = weights {
            wbytes = w.iter().flat_map(|v| v.to_le_bytes()).collect();
            tensors.insert(
                "weights".to_string(),
                TensorView::new(Dtype::F32, vec![w.len()], &wbytes).unwrap(),
            );
        }
        let mbytes: Vec<u8>;
        if let Some(m) = mapping {
            mbytes = m.iter().flat_map(|v| v.to_le_bytes()).collect();
            tensors.insert(
                "mapping".to_string(),
                TensorView::new(Dtype::I64, vec![m.len()], &mbytes).unwrap(),
            );
        }
        let bytes = safetensors::serialize(&tensors, None).unwrap();
        fs::write(dir.join("model.safetensors"), bytes).unwrap();
        fs::write(dir.join("tokenizer.json"), TOKENIZER_JSON).unwrap();
        fs::write(dir.join("config.json"), config).unwrap();
    }

    fn write_model(dir: &Path, weights: Option<&[f32]>, config: &str) {
        write_model_full(dir, weights, None, config);
    }

    fn load(weights: Option<&[f32]>, config: &str) -> Model2Vec {
        let dir = tempfile::tempdir().unwrap();
        write_model(dir.path(), weights, config);
        Model2Vec::from_dir(dir.path()).unwrap()
    }

    fn load_full(weights: Option<&[f32]>, mapping: Option<&[i64]>, config: &str) -> Model2Vec {
        let dir = tempfile::tempdir().unwrap();
        write_model_full(dir.path(), weights, mapping, config);
        Model2Vec::from_dir(dir.path()).unwrap()
    }

    #[test]
    fn mean_pool_is_exact() {
        let m = load(None, r#"{"normalize": false}"#);
        let out = m.embed(&["hello world"]).unwrap();
        assert_eq!(out.len(), 1);
        assert_eq!(out[0].0, vec![0.5, 1.0, 1.0, 2.0]);
    }

    #[test]
    fn normalization_is_exact() {
        // norm of [0.5, 1, 1, 2] is sqrt(6.25) = 2.5, exact in f32.
        let m = load(None, r#"{"normalize": true}"#);
        let out = m.embed(&["hello world"]).unwrap();
        assert_eq!(out[0].0, vec![0.2, 0.4, 0.4, 0.8]);
    }

    #[test]
    fn normalize_defaults_to_true_when_absent() {
        let m = load(None, "{}");
        let out = m.embed(&["hello world"]).unwrap();
        assert_eq!(out[0].0, vec![0.2, 0.4, 0.4, 0.8]);
    }

    #[test]
    fn unknown_tokens_are_dropped_not_pooled() {
        let m = load(None, r#"{"normalize": false}"#);
        // "xyzzy" tokenizes to [UNK]; only "hello" (row 1) remains.
        let out = m.embed(&["hello xyzzy"]).unwrap();
        assert_eq!(out[0].0, vec![1.0, 0.0, 2.0, 0.0]);
    }

    #[test]
    fn per_token_weights_scale_before_pooling() {
        let m = load(Some(&[1.0, 2.0, 0.5]), r#"{"normalize": false}"#);
        // hello scaled by 2 -> [2,0,4,0]; world by 0.5 -> [0,1,0,2].
        let out = m.embed(&["hello world"]).unwrap();
        assert_eq!(out[0].0, vec![1.0, 0.5, 2.0, 1.0]);
    }

    #[test]
    fn empty_and_all_unknown_input_yield_zero_vectors() {
        let m = load(None, r#"{"normalize": true}"#);
        let out = m.embed(&["", "xyzzy qwerty"]).unwrap();
        assert_eq!(out.len(), 2);
        assert_eq!(out[0].0, vec![0.0; 4]);
        assert_eq!(out[1].0, vec![0.0; 4]);
    }

    #[test]
    fn contract_length_and_dimension_hold() {
        let m = load(None, "{}");
        let texts = ["hello", "world", "hello world hello", ""];
        let out = m.embed(&texts).unwrap();
        assert_eq!(out.len(), texts.len());
        assert!(out.iter().all(|e| e.0.len() == m.dimensions()));
    }

    #[test]
    fn model_hash_covers_all_three_artifact_files() {
        let dir = tempfile::tempdir().unwrap();
        write_model(dir.path(), None, "{}");
        let mut hasher = Sha256::new();
        for f in ["model.safetensors", "tokenizer.json", "config.json"] {
            hasher.update(fs::read(dir.path().join(f)).unwrap());
        }
        let expected = hex(&hasher.finalize());
        let m = Model2Vec::from_dir(dir.path()).unwrap();
        assert_eq!(m.model_hash(), expected);

        // A config-only change is a different geometry, so a different
        // identity, which is the point of covering all three files.
        let dir2 = tempfile::tempdir().unwrap();
        write_model(dir2.path(), None, r#"{"normalize": false}"#);
        let m2 = Model2Vec::from_dir(dir2.path()).unwrap();
        assert_ne!(m.model_hash(), m2.model_hash());
    }

    /// The identity is the parser's shape: a model of the caller's own
    /// carries its digest and names neither a model nor a licence, since
    /// matra knows neither. Only the pinned digest earns both, which the
    /// ignored reference-model test checks in `tests/semantic_conformance.rs`.
    #[test]
    fn an_unpinned_model_reports_its_digest_and_nothing_it_cannot_know() {
        let dir = tempfile::tempdir().unwrap();
        write_model(dir.path(), None, "{}");
        let m = Model2Vec::from_dir(dir.path()).unwrap();
        assert_eq!(
            m.identity(),
            domain::Identity::new("model2vec", None, Some(m.model_hash().to_string()), None)
        );
    }

    #[test]
    fn mapping_remaps_token_ids_to_rows() {
        // Swap hello and world: id 1 -> row 2, id 2 -> row 1.
        let m = load_full(None, Some(&[0, 2, 1]), r#"{"normalize": false}"#);
        let out = m.embed(&["hello"]).unwrap();
        assert_eq!(out[0].0, vec![0.0, 2.0, 0.0, 4.0]);
    }

    #[test]
    fn weights_index_by_original_token_id_not_mapped_row() {
        // Mapping swaps rows; weights[1]=2.0 must scale token id 1
        // (hello, now gathering row 2), pinning the reference semantics.
        let m = load_full(
            Some(&[1.0, 2.0, 1.0]),
            Some(&[0, 2, 1]),
            r#"{"normalize": false}"#,
        );
        let out = m.embed(&["hello"]).unwrap();
        assert_eq!(out[0].0, vec![0.0, 4.0, 0.0, 8.0]);
    }

    #[test]
    fn mapping_entry_outside_vocab_is_rejected_at_load() {
        let dir = tempfile::tempdir().unwrap();
        write_model_full(dir.path(), None, Some(&[0, 3, 1]), "{}");
        match Model2Vec::from_dir(dir.path()) {
            Err(Error::ModelInvalid(msg)) => assert!(msg.contains("mapping entry")),
            other => panic!("expected ModelInvalid, got {:?}", other.map(|_| ())),
        }
    }

    #[test]
    fn short_weights_tensor_is_rejected_at_load() {
        let dir = tempfile::tempdir().unwrap();
        write_model(dir.path(), Some(&[1.0, 2.0]), "{}");
        match Model2Vec::from_dir(dir.path()) {
            Err(Error::ModelInvalid(msg)) => assert!(msg.contains("weights tensor length")),
            other => panic!("expected ModelInvalid, got {:?}", other.map(|_| ())),
        }
    }

    #[test]
    fn zero_dimension_matrix_is_rejected_at_load() {
        let dir = tempfile::tempdir().unwrap();
        let mut tensors = HashMap::new();
        let empty: Vec<u8> = Vec::new();
        tensors.insert(
            "embeddings".to_string(),
            TensorView::new(Dtype::F32, vec![3, 0], &empty).unwrap(),
        );
        let bytes = safetensors::serialize(&tensors, None).unwrap();
        fs::write(dir.path().join("model.safetensors"), bytes).unwrap();
        fs::write(dir.path().join("tokenizer.json"), TOKENIZER_JSON).unwrap();
        fs::write(dir.path().join("config.json"), "{}").unwrap();
        match Model2Vec::from_dir(dir.path()) {
            Err(Error::ModelInvalid(msg)) => assert!(msg.contains("zero dimension")),
            other => panic!("expected ModelInvalid, got {:?}", other.map(|_| ())),
        }
    }

    #[test]
    fn missing_file_is_model_not_found() {
        let dir = tempfile::tempdir().unwrap();
        match Model2Vec::from_dir(dir.path()) {
            Err(Error::ModelNotFound(p)) => {
                assert!(p.ends_with("model.safetensors"));
            }
            Err(other) => panic!("expected ModelNotFound, got {other:?}"),
            Ok(_) => panic!("expected ModelNotFound, got a loaded model"),
        }
    }

    #[test]
    fn corrupt_safetensors_is_model_invalid_not_panic() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("model.safetensors"), b"garbage").unwrap();
        fs::write(dir.path().join("tokenizer.json"), TOKENIZER_JSON).unwrap();
        fs::write(dir.path().join("config.json"), "{}").unwrap();
        match Model2Vec::from_dir(dir.path()) {
            Err(Error::ModelInvalid(_)) => {}
            Err(other) => panic!("expected ModelInvalid, got {other:?}"),
            Ok(_) => panic!("expected ModelInvalid, got a loaded model"),
        }
    }

    #[test]
    fn truncation_respects_char_boundaries() {
        // A multi-byte char straddling the cut must not split.
        let text = "hé";
        let t = Model2Vec::truncate_str(text, 2);
        assert_eq!(t, "h");
        assert_eq!(Model2Vec::truncate_str(text, 3), "hé");
    }
}

#[cfg(test)]
mod parity {
    //! Parity against the Python reference implementation (model2vec
    //! 0.9.0). Expected vectors were produced by running
    //! `StaticModel.from_pretrained` + `encode` on the exact artifact
    //! `tests::write_model` constructs (same matrix, tokenizer, config,
    //! normalize=true). The reference computes in float64, so the
    //! comparison is tolerance-based here; bit-exactness is the
    //! Rust-across-targets property, asserted separately in `tests`.
    use super::tests::write_model_normalized;
    use super::*;

    const TOLERANCE: f32 = 1e-6;

    #[test]
    fn matches_python_reference_on_pinned_inputs() {
        let dir = tempfile::tempdir().unwrap();
        write_model_normalized(dir.path());
        let m = Model2Vec::from_dir(dir.path()).unwrap();

        let cases: [(&str, [f64; 4]); 6] = [
            ("hello world", [0.2, 0.4, 0.4, 0.8]),
            ("hello", [0.4472135954999579, 0.0, 0.8944271909999159, 0.0]),
            // xyzzy is [UNK], dropped: identical to "hello" alone.
            (
                "hello xyzzy",
                [0.4472135954999579, 0.0, 0.8944271909999159, 0.0],
            ),
            ("", [0.0, 0.0, 0.0, 0.0]),
            ("xyzzy qwerty", [0.0, 0.0, 0.0, 0.0]),
            // Exercises the BertNormalizer lowercase path.
            (
                "HELLO World hello",
                [
                    0.31622776601683794,
                    0.31622776601683794,
                    0.6324555320336759,
                    0.6324555320336759,
                ],
            ),
        ];
        let texts: Vec<&str> = cases.iter().map(|(t, _)| *t).collect();
        let out = m.embed(&texts).unwrap();
        for ((text, expected), got) in cases.iter().zip(&out) {
            for (i, (e, g)) in expected.iter().zip(&got.0).enumerate() {
                let diff = (*e as f32 - g).abs();
                assert!(
                    diff <= TOLERANCE,
                    "{text:?} dim {i}: python {e} vs rust {g} (diff {diff})"
                );
            }
        }
    }
}

#[cfg(test)]
mod bit_parity {
    //! The property the static adapter buys (RFC-0010): identical bits on
    //! every target. These constants were produced once on aarch64; the
    //! CI matrix runs this test on x86_64 and aarch64, so a target whose
    //! arithmetic dispatches differently fails loudly here.
    use super::tests::write_model_normalized;
    use super::*;

    #[test]
    fn vectors_are_bit_identical_across_targets() {
        let dir = tempfile::tempdir().unwrap();
        write_model_normalized(dir.path());
        let m = Model2Vec::from_dir(dir.path()).unwrap();

        let cases: [(&str, [u32; 4]); 3] = [
            (
                "hello world",
                [0x3e4c_cccd, 0x3ecc_cccd, 0x3ecc_cccd, 0x3f4c_cccd],
            ),
            (
                "hello",
                [0x3ee4_f92e, 0x0000_0000, 0x3f64_f92e, 0x0000_0000],
            ),
            (
                "HELLO World hello",
                [0x3ea1_e89c, 0x3ea1_e89c, 0x3f21_e89c, 0x3f21_e89c],
            ),
        ];
        let texts: Vec<&str> = cases.iter().map(|(t, _)| *t).collect();
        let out = m.embed(&texts).unwrap();
        for ((text, expected), got) in cases.iter().zip(&out) {
            let bits: Vec<u32> = got.0.iter().map(|v| v.to_bits()).collect();
            assert_eq!(&bits[..], expected, "bit drift on {text:?}");
        }
    }
}

#[cfg(test)]
#[cfg(not(target_arch = "wasm32"))]
mod provisioning {
    //! The pinned-download path (EP-0010 M4), exercised with the fetcher as
    //! an argument so no test here touches the network or needs the
    //! 30 MB reference artifact. The fixture is the same tiny three-file
    //! model the rest of this file's tests use, and its digest is
    //! computed here rather than pinned.
    use std::cell::RefCell;

    use super::tests::write_model_normalized;
    use super::*;

    /// Stand-ins for [`POTION_BASE_8M_URLS`]. Nothing dereferences them;
    /// they are the keys the test fetcher is asked for, and asserting on
    /// them is how a test shows which artifacts were requested.
    const URLS: [&str; 3] = ["test://model", "test://tokenizer", "test://config"];

    /// The three artifacts as bytes, plus the digest over them in
    /// [`ARTIFACT_FILES`] order.
    struct Fixture {
        bytes: [Vec<u8>; 3],
        digest: String,
    }

    impl Fixture {
        fn new() -> Fixture {
            let dir = tempfile::tempdir().unwrap();
            write_model_normalized(dir.path());
            let bytes = ARTIFACT_FILES.map(|name| fs::read(dir.path().join(name)).unwrap());
            let mut hasher = Sha256::new();
            for buf in &bytes {
                hasher.update(buf);
            }
            Fixture {
                bytes,
                digest: hex(&hasher.finalize()),
            }
        }

        fn write_into(&self, dir: &Path) {
            for (name, buf) in ARTIFACT_FILES.iter().zip(&self.bytes) {
                fs::write(dir.join(name), buf).unwrap();
            }
        }
    }

    /// What a fetcher hands back for every request.
    enum Serves {
        /// The fixture bytes, so a download heals the directory.
        TheModel,
        /// Bytes that parse as nothing, so the digest can never match.
        Garbage,
        /// Garbage for the first round of three requests and the fixture
        /// for every one after it: a corrupt download that heals on the
        /// retry, with the corruption in bytes this call produced.
        GarbageThenTheModel,
        /// One byte past the cap.
        TooMuch,
        /// The fixture for the first request and a transport failure for
        /// every one after it: a download that dies partway through.
        TheFirstThenAFailure,
        /// The fixture for the first two requests and one byte past the
        /// cap for the third: the cap firing on the last of the set.
        TooMuchOnTheLast,
        /// Nothing: being called at all is the failure.
        Nothing,
    }

    /// A fetcher that records every URL it was asked for.
    struct Fetcher<'a> {
        fixture: &'a Fixture,
        serves: Serves,
        log: RefCell<Vec<String>>,
    }

    impl<'a> Fetcher<'a> {
        fn new(fixture: &'a Fixture, serves: Serves) -> Fetcher<'a> {
            Fetcher {
                fixture,
                serves,
                log: RefCell::new(Vec::new()),
            }
        }

        fn fetch(&self, url: &str) -> domain::Result<Vec<u8>> {
            self.log.borrow_mut().push(url.to_string());
            let index = URLS.iter().position(|u| *u == url).expect("known url");
            let round = self.log.borrow().len();
            match self.serves {
                Serves::TheModel => Ok(self.fixture.bytes[index].clone()),
                Serves::Garbage => Ok(b"not a model".to_vec()),
                Serves::GarbageThenTheModel => {
                    if round <= ARTIFACT_FILES.len() {
                        Ok(b"not a model".to_vec())
                    } else {
                        Ok(self.fixture.bytes[index].clone())
                    }
                }
                Serves::TooMuch => Ok(vec![0u8; MAX_ARTIFACT_BYTES + 1]),
                Serves::TheFirstThenAFailure => {
                    if round == 1 {
                        Ok(self.fixture.bytes[index].clone())
                    } else {
                        Err(Error::Io(std::io::Error::new(
                            std::io::ErrorKind::TimedOut,
                            format!("download {url}: the transfer stalled"),
                        )))
                    }
                }
                Serves::TooMuchOnTheLast => {
                    if round < ARTIFACT_FILES.len() {
                        Ok(self.fixture.bytes[index].clone())
                    } else {
                        Ok(vec![0u8; MAX_ARTIFACT_BYTES + 1])
                    }
                }
                Serves::Nothing => {
                    panic!("the fetcher was called for {url}, and should not have been")
                }
            }
        }

        fn calls(&self) -> Vec<String> {
            self.log.borrow().clone()
        }
    }

    const FIXTURE_NAME: &str = "fixture-model";
    const FIXTURE_LICENSE: &str = "Fixture License 1.0";
    const FIXTURE_LICENSE_URL: &str = "https://licenses.example/fixture";

    /// The fixture's pin: its digest, the stand-in URLs, its size, and a
    /// license of its own, so a notice can be told apart from the real
    /// pin's.
    fn pin_of(fixture: &Fixture) -> Pin<'_> {
        Pin {
            name: FIXTURE_NAME,
            bytes: fixture.bytes.iter().map(|b| b.len() as u64).sum(),
            digest: &fixture.digest,
            urls: URLS,
            license: FIXTURE_LICENSE,
            license_url: FIXTURE_LICENSE_URL,
        }
    }

    fn provision(
        dir: &Path,
        fixture: &Fixture,
        fetcher: &Fetcher<'_>,
    ) -> domain::Result<Model2Vec> {
        provision_noting(dir, fixture, fetcher, &mut |_| {})
    }

    fn provision_noting(
        dir: &Path,
        fixture: &Fixture,
        fetcher: &Fetcher<'_>,
        notice: &mut dyn FnMut(&domain::ProvisionNotice),
    ) -> domain::Result<Model2Vec> {
        Model2Vec::provision(dir, &pin_of(fixture), notice, &|url| fetcher.fetch(url))
    }

    /// `Result::unwrap_err` would need `Model2Vec: Debug`, and a loaded
    /// model has nothing worth printing (a 30 MB matrix and a tokenizer),
    /// so the surface stays as it is and the tests unwrap by hand.
    fn expect_err(result: domain::Result<Model2Vec>) -> Error {
        match result {
            Ok(_) => panic!("expected an error, got a loaded model"),
            Err(e) => e,
        }
    }

    fn present(dir: &Path) -> Vec<&'static str> {
        ARTIFACT_FILES
            .into_iter()
            .filter(|name| dir.join(name).exists())
            .collect()
    }

    /// The temporaries left in `dir`. Every artifact lands through one,
    /// and none of them outlives the call that opened it, so anything
    /// this returns is a leak. A directory that was never created counts
    /// as empty.
    fn temporaries(dir: &Path) -> Vec<String> {
        let Ok(entries) = fs::read_dir(dir) else {
            return Vec::new();
        };
        entries
            .map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned())
            .filter(|name| name.starts_with(".tmp."))
            .collect()
    }

    #[test]
    fn a_matching_digest_loads_and_nothing_is_fetched() {
        let fixture = Fixture::new();
        let dir = tempfile::tempdir().unwrap();
        fixture.write_into(dir.path());

        let fetcher = Fetcher::new(&fixture, Serves::Nothing);
        let model = provision(dir.path(), &fixture, &fetcher).unwrap();

        assert_eq!(model.model_hash(), fixture.digest);
        assert!(fetcher.calls().is_empty());
    }

    /// A first set that fails the digest is refetched, and the heal is
    /// invisible to the directory: the mismatching bytes were never
    /// written, so there is nothing to take back.
    #[test]
    fn a_corrupted_download_is_refetched_and_the_second_set_lands() {
        let fixture = Fixture::new();
        // Empty, so every byte that ends up in it came from this call.
        let dir = tempfile::tempdir().unwrap();

        let fetcher = Fetcher::new(&fixture, Serves::GarbageThenTheModel);
        let model = provision(dir.path(), &fixture, &fetcher).unwrap();

        // Two rounds of three. All three come back each time, not just
        // the one that drifted: the digest covers the set, so the set is
        // what gets replaced.
        assert_eq!(fetcher.calls(), [URLS, URLS].concat());
        assert_eq!(model.model_hash(), fixture.digest);
        assert_eq!(temporaries(dir.path()), Vec::<String>::new());
    }

    #[test]
    fn a_second_mismatch_is_model_invalid_and_leaves_no_files() {
        let fixture = Fixture::new();
        let dir = tempfile::tempdir().unwrap();

        let fetcher = Fetcher::new(&fixture, Serves::Garbage);
        let err = expect_err(provision(dir.path(), &fixture, &fetcher));

        assert!(
            matches!(err, Error::ModelInvalid(ref m) if m.contains(&fixture.digest)),
            "expected a digest mismatch, got {err:?}"
        );
        // Exactly one retry, so two rounds of three, and nothing left
        // behind for a later call to find.
        assert_eq!(fetcher.calls().len(), 2 * ARTIFACT_FILES.len());
        assert_eq!(present(dir.path()), Vec::<&str>::new());
    }

    #[test]
    fn an_oversized_download_is_input_too_large() {
        let fixture = Fixture::new();
        let dir = tempfile::tempdir().unwrap();

        let fetcher = Fetcher::new(&fixture, Serves::TooMuch);
        let err = expect_err(provision(dir.path(), &fixture, &fetcher));

        match err {
            Error::InputTooLarge {
                limit,
                actual,
                what,
            } => {
                assert_eq!(limit, MAX_ARTIFACT_BYTES);
                assert_eq!(actual, MAX_ARTIFACT_BYTES + 1);
                assert_eq!(what, "embedding_model_download");
            }
            other => panic!("expected InputTooLarge, got {other:?}"),
        }
        // The cap fires before anything is written.
        assert_eq!(present(dir.path()), Vec::<&str>::new());
    }

    /// Regression: a rejected certificate reads as a sentence naming
    /// the host, on this path as well as the UDPipe one. Someone behind
    /// a TLS-intercepting proxy meets both.
    #[test]
    fn a_rejected_certificate_reads_as_a_sentence() {
        let error = ureq::Error::Io(std::io::Error::other(
            "invalid peer certificate: Other(OtherError(CaUsedAsEndEntity))",
        ));
        let message = download_message(POTION_BASE_8M_URLS[0], &error);

        assert!(message.contains("huggingface.co"), "{message}");
        assert!(message.contains("system trust store"), "{message}");
        assert!(message.contains("from_dir"), "{message}");
        assert!(message.contains("CaUsedAsEndEntity"), "{message}");
        let mapped = transport_failure(POTION_BASE_8M_URLS[0], &error);
        assert_eq!(mapped.kind(), "io");
        assert_eq!(
            mapped.details().get("retryable"),
            Some(&domain::DetailValue::Bool(false)),
            "the message says to load by hand, so a retry is not offered"
        );
    }

    /// A download that did not arrive says so as data (EPR-0006): its
    /// URL, and that the same call may succeed when made again, since a
    /// failed fetch writes nothing.
    #[test]
    fn a_transport_failure_is_retryable_and_names_its_url() {
        for error in [
            ureq::Error::HostNotFound,
            ureq::Error::Timeout(ureq::Timeout::Connect),
            ureq::Error::StatusCode(502),
        ] {
            let details = transport_failure(POTION_BASE_8M_URLS[0], &error).details();
            assert_eq!(
                details.get("url"),
                Some(&domain::DetailValue::Text(
                    POTION_BASE_8M_URLS[0].to_string()
                )),
                "{error}"
            );
            assert_eq!(
                details.get("retryable"),
                Some(&domain::DetailValue::Bool(true)),
                "{error}"
            );
        }
    }

    #[test]
    fn a_url_yields_the_host_a_message_should_name() {
        assert_eq!(host_of(POTION_BASE_8M_URLS[0]), "huggingface.co");
        assert_eq!(host_of("https://example.org:8443/a?b=c"), "example.org");
    }

    /// A download is all three artifacts or none of them. Leaving the
    /// first one behind would be the worst of both: every later call
    /// refuses a partial set, so one timed-out transfer would wedge the
    /// directory until somebody deleted files by hand.
    #[test]
    fn a_fetch_that_fails_partway_leaves_no_artifacts() {
        let fixture = Fixture::new();
        let dir = tempfile::tempdir().unwrap();

        let fetcher = Fetcher::new(&fixture, Serves::TheFirstThenAFailure);
        let err = expect_err(provision(dir.path(), &fixture, &fetcher));

        // The fetcher's own error, not a digest verdict on bytes that
        // never arrived.
        match err {
            Error::Io(e) => assert_eq!(e.kind(), std::io::ErrorKind::TimedOut),
            other => panic!("expected the fetcher's Io error, got {other:?}"),
        }
        // A transport failure is not retried, so the second URL is the
        // last one asked for, and the first artifact does not survive it.
        assert_eq!(fetcher.calls(), URLS[..2].to_vec());
        assert_eq!(present(dir.path()), Vec::<&str>::new());
        assert_eq!(temporaries(dir.path()), Vec::<String>::new());
    }

    /// The cap firing on the last of the three is the same transaction:
    /// the two that already arrived are temporaries rather than
    /// artifacts, and they go with it.
    #[test]
    fn an_oversized_last_artifact_leaves_no_artifacts() {
        let fixture = Fixture::new();
        let dir = tempfile::tempdir().unwrap();

        let fetcher = Fetcher::new(&fixture, Serves::TooMuchOnTheLast);
        let err = expect_err(provision(dir.path(), &fixture, &fetcher));

        match err {
            Error::InputTooLarge {
                limit,
                actual,
                what,
            } => {
                assert_eq!(limit, MAX_ARTIFACT_BYTES);
                assert_eq!(actual, MAX_ARTIFACT_BYTES + 1);
                assert_eq!(what, "embedding_model_download");
            }
            other => panic!("expected InputTooLarge, got {other:?}"),
        }
        assert_eq!(fetcher.calls(), URLS.to_vec());
        assert_eq!(present(dir.path()), Vec::<&str>::new());
        assert_eq!(temporaries(dir.path()), Vec::<String>::new());
    }

    /// The names are the artifact format's, not this model's, so a full
    /// set that does not match the pin is somebody else's model. The
    /// provisioner says so and leaves every byte where it found it.
    #[test]
    fn pre_existing_mismatching_files_are_refused_and_untouched() {
        let fixture = Fixture::new();
        let dir = tempfile::tempdir().unwrap();
        fixture.write_into(dir.path());
        let mine: &[u8] = br#"{"normalize": false}"#;
        fs::write(dir.path().join("config.json"), mine).unwrap();

        let fetcher = Fetcher::new(&fixture, Serves::Nothing);
        let err = expect_err(provision(dir.path(), &fixture, &fetcher));

        let dir_name = dir.path().display().to_string();
        assert!(
            matches!(err, Error::ModelInvalid(ref m) if m.contains(&dir_name)),
            "expected the directory to be named, got {err:?}"
        );
        assert_eq!(present(dir.path()).len(), ARTIFACT_FILES.len());
        assert_eq!(fs::read(dir.path().join("config.json")).unwrap(), mine);
        assert_eq!(
            fs::read(dir.path().join("model.safetensors")).unwrap(),
            fixture.bytes[0]
        );
        assert_eq!(
            fs::read(dir.path().join("tokenizer.json")).unwrap(),
            fixture.bytes[1]
        );
    }

    /// A partial set has no provenance: it could be an interrupted
    /// download of ours or the start of somebody else's model. Filling
    /// the gap would mix two artifact sets under one digest, so the
    /// provisioner refuses this the same way and touches nothing.
    #[test]
    fn a_partial_directory_is_refused_and_untouched() {
        let fixture = Fixture::new();
        let dir = tempfile::tempdir().unwrap();
        fixture.write_into(dir.path());
        fs::remove_file(dir.path().join("tokenizer.json")).unwrap();

        let fetcher = Fetcher::new(&fixture, Serves::Nothing);
        let err = expect_err(provision(dir.path(), &fixture, &fetcher));

        let dir_name = dir.path().display().to_string();
        assert!(
            matches!(err, Error::ModelInvalid(ref m) if m.contains(&dir_name)),
            "expected the directory to be named, got {err:?}"
        );
        assert_eq!(
            present(dir.path()),
            vec!["model.safetensors", "config.json"]
        );
        assert_eq!(
            fs::read(dir.path().join("model.safetensors")).unwrap(),
            fixture.bytes[0]
        );
        assert_eq!(
            fs::read(dir.path().join("config.json")).unwrap(),
            fixture.bytes[2]
        );
    }

    /// A temporary in a directory somebody else can write to is a place
    /// to plant a symlink, and a plain write would follow it and put
    /// 30 MB through to whatever it points at. `create_new` is `O_EXCL`,
    /// so the open fails instead.
    ///
    /// This drives [`Transaction::write_temp`] rather than a whole
    /// provision, because the name now carries a per-call stamp and a
    /// test cannot predict it. Predicting the name was never the
    /// guarantee; `O_EXCL` is.
    #[cfg(unix)]
    #[test]
    fn a_planted_temporary_is_not_written_through() {
        let victim_bytes: &[u8] = b"the caller's bytes";
        let dir = tempfile::tempdir().unwrap();
        let victim = dir.path().join("victim");
        fs::write(&victim, victim_bytes).unwrap();

        let planted = dir.path().join(".tmp.model.safetensors.planted");
        std::os::unix::fs::symlink(&victim, &planted).unwrap();

        let mut transaction = Transaction::new();
        let err = transaction
            .write_temp(&planted, b"thirty megabytes")
            .expect_err("a path already there fails the open");

        match err {
            Error::Io(e) => {
                assert_eq!(e.kind(), std::io::ErrorKind::AlreadyExists);
                assert!(e.to_string().contains(".tmp.model.safetensors"), "{e}");
            }
            other => panic!("expected Io(AlreadyExists), got {other:?}"),
        }
        assert_eq!(fs::read(&victim).unwrap(), victim_bytes);

        // The plant was never this call's, so unwinding leaves it and
        // its target alone.
        drop(transaction);
        assert!(fs::symlink_metadata(&planted).is_ok());
        assert_eq!(fs::read(&victim).unwrap(), victim_bytes);
    }

    /// Regression (review of #77): not one byte of a download is on disk
    /// while the rest of the set is still in flight, and nothing is
    /// written at all until the whole set has satisfied the pin. Before
    /// this, each artifact was written to a temporary as it arrived and
    /// all three were renamed onto the artifact names before the digest
    /// was consulted, so unverified bytes reached the model directory
    /// under their real names.
    ///
    /// The fetcher is the observer: it photographs the directory on
    /// every call, which is the only place from which the ordering is
    /// visible from outside.
    #[test]
    fn nothing_is_written_until_the_whole_set_has_satisfied_the_pin() {
        let fixture = Fixture::new();
        let dir = tempfile::tempdir().unwrap();
        let seen: RefCell<Vec<(Vec<&'static str>, Vec<String>)>> = RefCell::new(Vec::new());

        let model = Model2Vec::provision(dir.path(), &pin_of(&fixture), &mut |_| {}, &|url| {
            seen.borrow_mut()
                .push((present(dir.path()), temporaries(dir.path())));
            let index = URLS.iter().position(|u| *u == url).expect("known url");
            Ok(fixture.bytes[index].clone())
        })
        .unwrap();

        assert_eq!(model.model_hash(), fixture.digest);
        assert_eq!(seen.borrow().len(), ARTIFACT_FILES.len());
        for (artifacts, temps) in seen.borrow().iter() {
            assert!(
                artifacts.is_empty(),
                "an artifact name existed mid-download: {artifacts:?}"
            );
            assert!(
                temps.is_empty(),
                "a temporary existed mid-download: {temps:?}"
            );
        }
        assert_eq!(present(dir.path()).len(), ARTIFACT_FILES.len());
        assert_eq!(temporaries(dir.path()), Vec::<String>::new());
    }

    /// The directory is created before the download rather than after
    /// it, so an unwritable one fails in milliseconds instead of after
    /// 30 MB has travelled. `Serves::Nothing` makes being asked for a
    /// byte the failure.
    ///
    /// Regression (report H6, unfixed on this adapter until the second
    /// review of #77): the failure names the operation and the path.
    /// `io error: No space left on device (os error 28)` was the whole
    /// message an install produced, with the directory one line away in
    /// a variable, while `site/content/reference/errors.md` said both
    /// adapters carry the operation and the path.
    #[test]
    fn a_model_directory_that_cannot_be_created_fails_before_the_download() {
        let fixture = Fixture::new();
        let parent = tempfile::tempdir().unwrap();
        // A file where a directory would have to be, so `create_dir_all`
        // cannot succeed under it for root either.
        let blocker = parent.path().join("not-a-directory");
        fs::write(&blocker, b"x").unwrap();
        let dir = blocker.join("models");

        let fetcher = Fetcher::new(&fixture, Serves::Nothing);
        let err = expect_err(provision(&dir, &fixture, &fetcher));

        assert!(
            matches!(err, Error::Io(_)),
            "expected an Io failure, got {err:?}"
        );
        assert!(fetcher.calls().is_empty());
        let message = err.to_string();
        assert!(
            message.contains("create the model directory"),
            "names the operation: {message}"
        );
        assert!(
            message.contains(&dir.display().to_string()),
            "names the path: {message}"
        );
        assert_eq!(
            err.details().get("path"),
            Some(&domain::DetailValue::Text(dir.display().to_string())),
            "the path is data, not only words in the message"
        );
    }

    /// Regression (report H6): a read that fails names the operation and
    /// the path too, on the constructor that loads a caller's own
    /// directory. A directory sitting where an artifact file belongs
    /// exists, so it passes the presence check and fails the read.
    #[test]
    fn an_artifact_that_cannot_be_read_names_the_operation_and_the_path() {
        let dir = tempfile::tempdir().unwrap();
        for name in ARTIFACT_FILES {
            fs::write(dir.path().join(name), b"x").unwrap();
        }
        let blocked = dir.path().join(ARTIFACT_FILES[0]);
        fs::remove_file(&blocked).unwrap();
        fs::create_dir(&blocked).unwrap();

        let err = expect_err(Model2Vec::from_dir(dir.path()));

        assert_eq!(err.kind(), "io");
        let message = err.to_string();
        assert!(
            message.contains("cannot read"),
            "names the operation: {message}"
        );
        assert!(
            message.contains(&blocked.display().to_string()),
            "names the path: {message}"
        );
    }

    /// Regression (review of #77): a temporary left by a killed process
    /// is reclaimed once it is too old to belong to a live call. Without
    /// this there was no reclaim at all, and `create_new` refused that
    /// name for ever. In a container every process is pid 1, so one
    /// interrupt wedged every later embedding provision on the machine
    /// until somebody deleted the file by hand.
    #[test]
    fn an_aged_orphan_temporary_is_reclaimed() {
        let dir = tempfile::tempdir().unwrap();
        let orphan = dir.path().join(".tmp.model.safetensors.424242.0");
        fs::write(&orphan, b"half an artifact").unwrap();

        // The clock is a parameter, so the rule is exercised without
        // touching modification times on disk.
        reclaim_stale_temps(dir.path(), SystemTime::now());
        assert!(orphan.exists(), "nothing is old yet");

        reclaim_stale_temps(
            dir.path(),
            SystemTime::now() + STALE_TEMP_AGE + Duration::from_secs(1),
        );
        assert!(!orphan.exists(), "an aged leftover is reclaimed");
    }

    /// The constraint on that sweep: a temporary from a live peer is
    /// seconds old, and a container hands every process the same pid, so
    /// nothing may be removed for looking like this call's own.
    #[test]
    fn a_fresh_peer_temporary_survives_a_provision() {
        let fixture = Fixture::new();
        let dir = tempfile::tempdir().unwrap();
        let peer = dir
            .path()
            .join(format!(".tmp.model.safetensors.{}", std::process::id()));
        fs::write(&peer, b"in flight").unwrap();

        let fetcher = Fetcher::new(&fixture, Serves::TheModel);
        let model = provision(dir.path(), &fixture, &fetcher).unwrap();

        assert_eq!(model.model_hash(), fixture.digest);
        assert!(peer.exists(), "a fresh peer is not this call's to remove");
        assert_eq!(fs::read(&peer).unwrap(), b"in flight");
        assert_eq!(present(dir.path()).len(), ARTIFACT_FILES.len());
    }

    /// A modification time in the future is not a reason to delete. An
    /// unexplained timestamp is a reason not to.
    #[test]
    fn a_temporary_from_the_future_is_left_alone() {
        let dir = tempfile::tempdir().unwrap();
        let odd = dir.path().join(".tmp.config.json.7.0");
        fs::write(&odd, b"scratch").unwrap();

        reclaim_stale_temps(dir.path(), SystemTime::now() - Duration::from_secs(3600));
        assert!(odd.exists());
    }

    /// The sweep only ever looks at its own names. A hand-placed model
    /// in the same directory is untouched however old it looks.
    #[test]
    fn the_sweep_only_matches_its_own_temporary_names() {
        let fixture = Fixture::new();
        let dir = tempfile::tempdir().unwrap();
        fixture.write_into(dir.path());
        let temp = dir.path().join(".tmp.config.json.7.0");
        fs::write(&temp, b"scratch").unwrap();

        reclaim_stale_temps(
            dir.path(),
            SystemTime::now() + STALE_TEMP_AGE + Duration::from_secs(1),
        );

        assert_eq!(present(dir.path()).len(), ARTIFACT_FILES.len());
        assert!(!temp.exists());
    }

    /// The name that keeps two calls apart. A process id is not enough:
    /// in a container it is 1 for everybody.
    #[test]
    fn two_temporary_names_in_one_process_differ() {
        assert_ne!(temp_stamp(), temp_stamp());
    }

    /// Regression (review of #77): a timeout while reading the body
    /// reports `TimedOut`, which is what `site/content/reference/errors.md`
    /// and RFC-0015's decision table both promise. `ureq`'s body reader
    /// builds its `io::Error` with `Error::into_io`, which wraps
    /// everything that is not already an `io::Error` in
    /// `io::Error::other`, so reading `kind()` off it gave `Other`.
    #[test]
    fn a_timeout_while_reading_the_body_is_reported_as_a_timeout() {
        let from_ureq = std::io::Error::other(ureq::Error::Timeout(ureq::Timeout::Global));
        assert_eq!(
            from_ureq.kind(),
            std::io::ErrorKind::Other,
            "the wrapping this test exists to undo"
        );

        match body_failure(POTION_BASE_8M_URLS[0], from_ureq) {
            Error::Io(e) => {
                assert_eq!(e.kind(), std::io::ErrorKind::TimedOut);
                assert!(e.to_string().contains("huggingface.co"), "{e}");
            }
            other => panic!("expected Io(TimedOut), got {other:?}"),
        }
    }

    /// An `io::Error` that never was a `ureq::Error` keeps its own kind,
    /// so the unwrapping costs nothing on the ordinary path.
    #[test]
    fn a_plain_body_read_failure_keeps_its_own_kind() {
        let raw = std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "peer disconnected");

        match body_failure(POTION_BASE_8M_URLS[0], raw) {
            Error::Io(e) => assert_eq!(e.kind(), std::io::ErrorKind::UnexpectedEof),
            other => panic!("expected Io(UnexpectedEof), got {other:?}"),
        }
    }

    /// `ureq` follows up to ten redirects, so without `https_only` an
    /// `https` URL that redirects to `http` is fetched in cleartext. The
    /// digest pin keeps that from changing which bytes load, so this is
    /// confidentiality only, and it is still not the redirect's call.
    #[test]
    fn the_download_agent_refuses_to_leave_https() {
        let agent = download_agent();
        assert!(agent.config().https_only());
        assert!(
            agent.config().max_redirects() > 0,
            "the redirect following that makes https_only load-bearing"
        );
    }

    /// The pin lives in two places by necessity: this file compiles it
    /// into the loader, and the conformance fixture states it for every
    /// crust's runner. Two copies that nothing compares are two copies
    /// that drift.
    #[test]
    fn the_pinned_digest_matches_the_conformance_fixture() {
        let path =
            Path::new(env!("CARGO_MANIFEST_DIR")).join("spec/tests/semantic/reference-model.json");
        let raw = fs::read_to_string(&path)
            .unwrap_or_else(|e| panic!("cannot read {}: {e}", path.display()));
        let fixture: serde_json::Value = serde_json::from_str(&raw)
            .unwrap_or_else(|e| panic!("malformed fixture {}: {e}", path.display()));
        let pinned = fixture["model"]["artifact_hash"]
            .as_str()
            .expect("model.artifact_hash is a string");

        assert_eq!(
            pinned,
            POTION_BASE_8M_SHA256,
            "{} pins a different artifact digest than src/embed/model2vec.rs; \
             refresh both with scripts/fetch-embedding-hash.sh",
            path.display()
        );
    }

    /// Regression (EPR-0006, defect 11): the first provision of the
    /// embedding model was silent for a 30.9 MB download. The notice
    /// fires once before the set is fetched, naming the model, its size,
    /// where it is going and its license.
    #[test]
    fn a_fetch_is_announced_once_for_the_set() {
        let fixture = Fixture::new();
        let dir = tempfile::tempdir().unwrap();
        let fetcher = Fetcher::new(&fixture, Serves::TheModel);
        let mut notices = Vec::new();
        provision_noting(dir.path(), &fixture, &fetcher, &mut |n| {
            notices.push(n.clone())
        })
        .unwrap();

        assert_eq!(notices.len(), 1, "one notice for the set of three files");
        let notice = &notices[0];
        assert_eq!(notice.artifact, FIXTURE_NAME);
        assert_eq!(
            notice.bytes,
            fixture.bytes.iter().map(|b| b.len() as u64).sum::<u64>()
        );
        assert_eq!(notice.destination, dir.path());
        assert_eq!(notice.license, FIXTURE_LICENSE);
        assert_eq!(notice.license_url, FIXTURE_LICENSE_URL);
    }

    /// A set already on disk is not fetched, so nothing is announced; a
    /// set that fails its digest is fetched again, and each round is
    /// announced.
    #[test]
    fn the_notice_follows_the_fetches() {
        let fixture = Fixture::new();
        let dir = tempfile::tempdir().unwrap();
        let fetcher = Fetcher::new(&fixture, Serves::TheModel);
        provision(dir.path(), &fixture, &fetcher).unwrap();

        let mut notices = 0;
        let nothing = Fetcher::new(&fixture, Serves::Nothing);
        provision_noting(dir.path(), &fixture, &nothing, &mut |_| notices += 1).unwrap();
        assert_eq!(notices, 0, "a warm start announces nothing");

        let fresh = tempfile::tempdir().unwrap();
        let flaky = Fetcher::new(&fixture, Serves::GarbageThenTheModel);
        provision_noting(fresh.path(), &fixture, &flaky, &mut |_| notices += 1).unwrap();
        assert_eq!(
            notices, 2,
            "one notice per round, and the first round failed"
        );
    }

    /// The pinned record says what the notice will tell a user: the model
    /// by name, the three files' total size, and the license its model
    /// card states.
    #[test]
    fn the_pin_carries_its_name_size_and_license() {
        assert_eq!(POTION_BASE_8M.name, "potion-base-8M");
        assert_eq!(POTION_BASE_8M.bytes, 30_236_760 + 683_666 + 202);
        assert_eq!(POTION_BASE_8M.license, "MIT");
        assert_eq!(POTION_BASE_8M.digest, POTION_BASE_8M_SHA256);
        assert_eq!(POTION_BASE_8M.urls, POTION_BASE_8M_URLS);
    }

    /// Regression: `potion_base_8m(dir)` put the three files at the root
    /// of `dir`, where `MATRA_MODEL_DIR` and `Config::with_model_dir`
    /// mean the directory that holds every model, so a caller who passed
    /// the model directory got a second copy beside the parsing model.
    /// `dir` is the models directory, and the set lands where
    /// `from_config` looks for it.
    #[test]
    fn potion_base_8m_takes_the_models_directory_from_config_reads() {
        let models = Path::new("/srv/matra/models");
        let cfg = Config::from_sources(|key| (key == "HOME").then(|| "/home/u".to_string()), None)
            .unwrap()
            .with_model_dir(models);
        assert_eq!(
            potion_dir(models),
            cfg.model_dir().join(cfg.embedding_model())
        );
        assert_eq!(potion_dir(models), models.join("potion-base-8M"));
    }

    #[test]
    fn a_missing_directory_is_created_and_filled() {
        let fixture = Fixture::new();
        let parent = tempfile::tempdir().unwrap();
        let dir = parent.path().join("models").join("potion-base-8M");

        let fetcher = Fetcher::new(&fixture, Serves::TheModel);
        let model = provision(&dir, &fixture, &fetcher).unwrap();

        assert_eq!(model.model_hash(), fixture.digest);
        assert_eq!(present(&dir).len(), 3);
        // The temporary each artifact landed through is gone.
        assert_eq!(temporaries(&dir), Vec::<String>::new());
    }

    #[test]
    fn from_dir_never_downloads() {
        let fixture = Fixture::new();
        let fetcher = Fetcher::new(&fixture, Serves::TheModel);

        // The fetcher is live: provisioning an empty directory calls it.
        let provisioned = tempfile::tempdir().unwrap();
        provision(provisioned.path(), &fixture, &fetcher).unwrap();
        assert_eq!(fetcher.calls().len(), 3);

        // `from_dir` has no fetcher to call, and an empty directory is
        // where the difference shows: it reports what is missing instead
        // of going to get it.
        let empty = tempfile::tempdir().unwrap();
        let err = expect_err(Model2Vec::from_dir(empty.path()));
        assert!(
            matches!(err, Error::ModelNotFound(ref p) if p.ends_with("model.safetensors")),
            "expected ModelNotFound, got {err:?}"
        );
        assert_eq!(fetcher.calls().len(), 3);
        assert_eq!(present(empty.path()), Vec::<&str>::new());
    }
}