memd 1.9.0

Local memory CLI for AI agents with hybrid retrieval
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
//! Model download utilities
//!
//! Downloads embedding model to ~/.cache/memd/ on first use.

use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

use sha2::{Digest, Sha256};

use super::traits::PoolingStrategy;
use crate::error::{MemdError, Result};

/// Supported embedding models
///
/// Each model has specific configuration for URLs, dimensions, and pooling.
/// Pooling strategy is tied to model architecture, not user-configurable.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum EmbeddingModel {
    /// all-MiniLM-L6-v2: 384-dim, mean pooling, 23MB quantized
    /// MTEB score: 56.3
    #[default]
    AllMiniLmL6V2,
    /// Qwen3-Embedding-0.6B: 1024-dim, last-token pooling, ~614MB quantized
    /// MTEB score: 64.33 (+15% improvement)
    Qwen3Embedding0_6B,
}

impl EmbeddingModel {
    /// Get embedding dimension for this model
    pub fn dimension(&self) -> usize {
        match self {
            Self::AllMiniLmL6V2 => 384,
            Self::Qwen3Embedding0_6B => 1024,
        }
    }

    /// Get pooling strategy for this model
    pub fn pooling_strategy(&self) -> PoolingStrategy {
        match self {
            Self::AllMiniLmL6V2 => PoolingStrategy::Mean,
            Self::Qwen3Embedding0_6B => PoolingStrategy::LastToken,
        }
    }

    /// Get model ONNX file URL
    pub fn model_url(&self) -> &'static str {
        match self {
            Self::AllMiniLmL6V2 => ALL_MINILM_MODEL_URL,
            Self::Qwen3Embedding0_6B => QWEN3_MODEL_URL,
        }
    }

    /// Get tokenizer URL
    pub fn tokenizer_url(&self) -> &'static str {
        match self {
            Self::AllMiniLmL6V2 => ALL_MINILM_TOKENIZER_URL,
            Self::Qwen3Embedding0_6B => QWEN3_TOKENIZER_URL,
        }
    }

    /// Get expected model ONNX sha256 for fresh downloads
    pub fn model_sha256(&self) -> &'static str {
        match self {
            Self::AllMiniLmL6V2 => ALL_MINILM_MODEL_SHA256,
            Self::Qwen3Embedding0_6B => QWEN3_MODEL_SHA256,
        }
    }

    /// Get expected tokenizer sha256 for fresh downloads
    pub fn tokenizer_sha256(&self) -> &'static str {
        match self {
            Self::AllMiniLmL6V2 => ALL_MINILM_TOKENIZER_SHA256,
            Self::Qwen3Embedding0_6B => QWEN3_TOKENIZER_SHA256,
        }
    }

    /// Get model filename for cache
    pub fn model_filename(&self) -> &'static str {
        match self {
            Self::AllMiniLmL6V2 => "all-MiniLM-L6-v2-quantized.onnx",
            Self::Qwen3Embedding0_6B => "qwen3-embedding-0.6b-q8.onnx",
        }
    }

    /// Get tokenizer filename for cache
    pub fn tokenizer_filename(&self) -> &'static str {
        match self {
            Self::AllMiniLmL6V2 => "all-minilm-l6-v2-tokenizer.json",
            Self::Qwen3Embedding0_6B => "qwen3-embedding-0.6b-tokenizer.json",
        }
    }

    /// Get minimum expected model file size (bytes)
    pub fn min_model_size(&self) -> u64 {
        match self {
            Self::AllMiniLmL6V2 => 20_000_000,       // ~23MB
            Self::Qwen3Embedding0_6B => 500_000_000, // ~614MB
        }
    }

    /// Get minimum expected tokenizer file size (bytes)
    pub fn min_tokenizer_size(&self) -> u64 {
        match self {
            Self::AllMiniLmL6V2 => 500_000,     // ~700KB
            Self::Qwen3Embedding0_6B => 10_000, // ~varies
        }
    }

    /// Whether this model uses instruction-formatted queries
    pub fn uses_instruction_format(&self) -> bool {
        match self {
            Self::AllMiniLmL6V2 => false,
            Self::Qwen3Embedding0_6B => true,
        }
    }

    /// Whether this model requires position_ids as an input
    ///
    /// Decoder-style models (Qwen3) require explicit position IDs,
    /// while encoder models (BERT, all-MiniLM) compute them internally.
    pub fn requires_position_ids(&self) -> bool {
        match self {
            Self::AllMiniLmL6V2 => false,
            Self::Qwen3Embedding0_6B => true,
        }
    }

    /// Get KV-cache configuration for decoder models
    ///
    /// Returns None for encoder models (BERT-style) that don't use KV-cache.
    /// Returns configuration for decoder models that require empty KV-cache tensors.
    pub fn kv_cache_config(&self) -> Option<KvCacheConfig> {
        match self {
            Self::AllMiniLmL6V2 => None,
            Self::Qwen3Embedding0_6B => Some(KvCacheConfig {
                num_layers: 28,
                num_kv_heads: 8,
                head_dim: 128,
            }),
        }
    }
}

/// KV-cache configuration for decoder models
///
/// Decoder models (like Qwen3) use key-value caching for efficient autoregressive
/// generation. For embedding generation (single forward pass), we pass empty
/// KV-cache tensors with sequence length 0.
#[derive(Debug, Clone, Copy)]
pub struct KvCacheConfig {
    /// Number of transformer layers
    pub num_layers: usize,
    /// Number of key-value attention heads
    pub num_kv_heads: usize,
    /// Dimension of each attention head
    pub head_dim: usize,
}

const ALL_MINILM_MODEL_URL: &str =
    "https://huggingface.co/Xenova/all-MiniLM-L6-v2/resolve/751bff37182d3f1213fa05d7196b954e230abad9/onnx/model_quantized.onnx";
const ALL_MINILM_MODEL_SHA256: &str =
    "afdb6f1a0e45b715d0bb9b11772f032c399babd23bfc31fed1c170afc848bdb1";
const ALL_MINILM_TOKENIZER_URL: &str =
    "https://huggingface.co/Xenova/all-MiniLM-L6-v2/resolve/751bff37182d3f1213fa05d7196b954e230abad9/tokenizer.json";
const ALL_MINILM_TOKENIZER_SHA256: &str =
    "da0e79933b9ed51798a3ae27893d3c5fa4a201126cef75586296df9b4d2c62a0";

const QWEN3_MODEL_URL: &str =
    "https://huggingface.co/onnx-community/Qwen3-Embedding-0.6B-ONNX/resolve/c25a394dd583836952667c12f008335071b3f43d/onnx/model_int8.onnx";
const QWEN3_MODEL_SHA256: &str = "6d0ea863f78b4a84afa3c7fcba1ec341572b5e28121aef77b7092b1dfdf679c7";
const QWEN3_TOKENIZER_URL: &str =
    "https://huggingface.co/onnx-community/Qwen3-Embedding-0.6B-ONNX/resolve/c25a394dd583836952667c12f008335071b3f43d/tokenizer.json";
const QWEN3_TOKENIZER_SHA256: &str =
    "def76fb086971c7867b829c23a26261e38d9d74e02139253b38aeb9df8b4b50a";

// Legacy constants for backward compatibility (used by existing get_model_path/get_tokenizer_path)
const MODEL_URL: &str = ALL_MINILM_MODEL_URL;
const MODEL_SHA256: &str = ALL_MINILM_MODEL_SHA256;
const MODEL_FILENAME: &str = "all-MiniLM-L6-v2-quantized.onnx";
const TOKENIZER_URL: &str = ALL_MINILM_TOKENIZER_URL;
const TOKENIZER_SHA256: &str = ALL_MINILM_TOKENIZER_SHA256;
const TOKENIZER_FILENAME: &str = "tokenizer.json";
const MIN_MODEL_SIZE: u64 = 20_000_000;
const MIN_TOKENIZER_SIZE: u64 = 500_000;

// Candle BERT (safetensors) files for sentence-transformers/all-MiniLM-L6-v2.
// These URLs are fetched with plain ureq, which follows huggingface.co's
// relative 307 Location headers correctly. Prior versions used hf-hub 0.3.2,
// which mishandled those redirects and failed with RelativeUrlWithoutBase.
// URLs are pinned to immutable revisions and fresh downloads verify sha256;
// bumping a model means updating the revision and sha256 constants together.
const CANDLE_BERT_CONFIG_URL: &str =
    "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/1110a243fdf4706b3f48f1d95db1a4f5529b4d41/config.json";
const CANDLE_BERT_CONFIG_SHA256: &str =
    "953f9c0d463486b10a6871cc2fd59f223b2c70184f49815e7efbcab5d8908b41";
const CANDLE_BERT_CONFIG_FILENAME: &str = "sentence-transformers-all-MiniLM-L6-v2-config.json";
const CANDLE_BERT_TOKENIZER_URL: &str =
    "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/1110a243fdf4706b3f48f1d95db1a4f5529b4d41/tokenizer.json";
const CANDLE_BERT_TOKENIZER_SHA256: &str =
    "be50c3628f2bf5bb5e3a7f17b1f74611b2561a3a27eeab05e5aa30f411572037";
const CANDLE_BERT_TOKENIZER_FILENAME: &str =
    "sentence-transformers-all-MiniLM-L6-v2-tokenizer.json";
const CANDLE_BERT_WEIGHTS_URL: &str =
    "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/1110a243fdf4706b3f48f1d95db1a4f5529b4d41/model.safetensors";
const CANDLE_BERT_WEIGHTS_SHA256: &str =
    "53aa51172d142c89d9012cce15ae4d6cc0ca6895895114379cacb4fab128d9db";
const CANDLE_BERT_WEIGHTS_FILENAME: &str = "sentence-transformers-all-MiniLM-L6-v2.safetensors";
const MIN_CANDLE_BERT_CONFIG_SIZE: u64 = 100; // config.json is ~600 bytes
const MIN_CANDLE_BERT_TOKENIZER_SIZE: u64 = 100_000; // tokenizer.json is ~470KB
const MIN_CANDLE_BERT_WEIGHTS_SIZE: u64 = 80_000_000; // safetensors is ~90MB

// Candle BERT (safetensors) files for BAAI/bge-base-en-v1.5 — a stronger
// 768-d BERT retriever selectable via `--embedding-model bge-base`. Same
// pinned-immutable-revision + sha256 contract as the MiniLM constants.
const CANDLE_BGE_CONFIG_URL: &str =
    "https://huggingface.co/BAAI/bge-base-en-v1.5/resolve/a5beb1e3e68b9ab74eb54cfd186867f64f240e1a/config.json";
const CANDLE_BGE_CONFIG_SHA256: &str =
    "bc00af31a4a31b74040d73370aa83b62da34c90b75eb77bfa7db039d90abd591";
const CANDLE_BGE_CONFIG_FILENAME: &str = "bge-base-en-v1.5-config.json";
const CANDLE_BGE_TOKENIZER_URL: &str =
    "https://huggingface.co/BAAI/bge-base-en-v1.5/resolve/a5beb1e3e68b9ab74eb54cfd186867f64f240e1a/tokenizer.json";
const CANDLE_BGE_TOKENIZER_SHA256: &str =
    "d241a60d5e8f04cc1b2b3e9ef7a4921b27bf526d9f6050ab90f9267a1f9e5c66";
const CANDLE_BGE_TOKENIZER_FILENAME: &str = "bge-base-en-v1.5-tokenizer.json";
const CANDLE_BGE_WEIGHTS_URL: &str =
    "https://huggingface.co/BAAI/bge-base-en-v1.5/resolve/a5beb1e3e68b9ab74eb54cfd186867f64f240e1a/model.safetensors";
const CANDLE_BGE_WEIGHTS_SHA256: &str =
    "c7c1988aae201f80cf91a5dbbd5866409503b89dcaba877ca6dba7dd0a5167d7";
const CANDLE_BGE_WEIGHTS_FILENAME: &str = "bge-base-en-v1.5.safetensors";
const MIN_CANDLE_BGE_CONFIG_SIZE: u64 = 100; // config.json is ~800 bytes
const MIN_CANDLE_BGE_TOKENIZER_SIZE: u64 = 100_000; // tokenizer.json is ~700KB
const MIN_CANDLE_BGE_WEIGHTS_SIZE: u64 = 400_000_000; // safetensors is ~438MB

/// Which BERT-family model the Candle embedder loads.
///
/// Selected through the normal config surface (`--embedding-model bge-base`,
/// which threads down to the store as `PersistentStoreConfig::candle_model`).
/// Pooling and the retrieval query prefix are properties of the model, not
/// user-configurable — mirroring how `EmbeddingModel` ties pooling to
/// architecture.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CandleModel {
    /// sentence-transformers/all-MiniLM-L6-v2: 384-d, mean pooling
    #[default]
    MiniLm,
    /// BAAI/bge-base-en-v1.5: 768-d, CLS pooling, query prefix at recall
    BgeBase,
}

impl CandleModel {
    /// Pooling strategy required by this model's training recipe.
    pub fn pooling_strategy(&self) -> PoolingStrategy {
        match self {
            Self::MiniLm => PoolingStrategy::Mean,
            Self::BgeBase => PoolingStrategy::Cls,
        }
    }

    /// Retrieval query prefix, applied to queries only (never documents).
    pub fn query_prefix(&self) -> Option<&'static str> {
        match self {
            Self::MiniLm => None,
            Self::BgeBase => Some("Represent this sentence for searching relevant passages: "),
        }
    }

    /// Human-readable model id for logs.
    pub fn hf_id(&self) -> &'static str {
        match self {
            Self::MiniLm => "sentence-transformers/all-MiniLM-L6-v2",
            Self::BgeBase => "BAAI/bge-base-en-v1.5",
        }
    }
}

/// Get the cache directory for memd models
pub fn get_cache_dir() -> Result<PathBuf> {
    let cache_dir = dirs::cache_dir()
        .ok_or_else(|| MemdError::StorageError("cannot determine cache directory".into()))?
        .join("memd")
        .join("models");
    Ok(cache_dir)
}

/// Get path to model file, downloading if needed
pub fn get_model_path() -> Result<PathBuf> {
    let cache_dir = get_cache_dir()?;
    let model_path = cache_dir.join(MODEL_FILENAME);

    if !model_path.exists() {
        download_model(&cache_dir)?;
    }

    // Verify model exists and has expected size
    verify_model_exists(&model_path)?;

    Ok(model_path)
}

/// Get path to tokenizer file, downloading if needed
pub fn get_tokenizer_path() -> Result<PathBuf> {
    let cache_dir = get_cache_dir()?;
    let tokenizer_path = cache_dir.join(TOKENIZER_FILENAME);

    if !tokenizer_path.exists() {
        download_tokenizer(&cache_dir)?;
    }

    // Verify tokenizer exists and has expected size
    verify_tokenizer_exists(&tokenizer_path)?;

    Ok(tokenizer_path)
}

/// Verify model file exists and has reasonable size
pub fn verify_model_exists(path: &PathBuf) -> Result<()> {
    if !path.exists() {
        return Err(MemdError::StorageError(format!(
            "model file not found at {:?}",
            path
        )));
    }

    let metadata = std::fs::metadata(path)?;
    if metadata.len() < MIN_MODEL_SIZE {
        return Err(MemdError::StorageError(format!(
            "model file too small ({} bytes), expected >= {} bytes. File may be corrupted, delete and retry.",
            metadata.len(),
            MIN_MODEL_SIZE
        )));
    }

    Ok(())
}

/// Verify tokenizer file exists and has reasonable size
fn verify_tokenizer_exists(path: &PathBuf) -> Result<()> {
    if !path.exists() {
        return Err(MemdError::StorageError(format!(
            "tokenizer file not found at {:?}",
            path
        )));
    }

    let metadata = std::fs::metadata(path)?;
    if metadata.len() < MIN_TOKENIZER_SIZE {
        return Err(MemdError::StorageError(format!(
            "tokenizer file too small ({} bytes), expected >= {} bytes. File may be corrupted, delete and retry.",
            metadata.len(),
            MIN_TOKENIZER_SIZE
        )));
    }

    Ok(())
}

/// Download the embedding model
pub fn download_model(cache_dir: &Path) -> Result<()> {
    let model_path = cache_dir.join(MODEL_FILENAME);
    download_file(
        MODEL_URL,
        &model_path,
        "embedding model",
        Some(MODEL_SHA256),
    )
}

/// Download the tokenizer (legacy, uses default model)
fn download_tokenizer(cache_dir: &Path) -> Result<()> {
    let tokenizer_path = cache_dir.join(TOKENIZER_FILENAME);
    download_file(
        TOKENIZER_URL,
        &tokenizer_path,
        "tokenizer",
        Some(TOKENIZER_SHA256),
    )
}

// =============================================================================
// Model-aware download functions (new API)
// =============================================================================

/// Get path to model file for specific model, downloading if needed
pub fn get_model_path_for(model: EmbeddingModel) -> Result<PathBuf> {
    let cache_dir = get_cache_dir()?;
    let model_path = cache_dir.join(model.model_filename());

    if !model_path.exists() {
        download_file(
            model.model_url(),
            &model_path,
            model.model_filename(),
            Some(model.model_sha256()),
        )?;
    }

    // Verify model exists and has expected size
    verify_file_size(&model_path, model.min_model_size(), "model")?;

    Ok(model_path)
}

/// Get path to tokenizer file for specific model, downloading if needed
pub fn get_tokenizer_path_for(model: EmbeddingModel) -> Result<PathBuf> {
    let cache_dir = get_cache_dir()?;
    let tokenizer_path = cache_dir.join(model.tokenizer_filename());

    if !tokenizer_path.exists() {
        download_file(
            model.tokenizer_url(),
            &tokenizer_path,
            model.tokenizer_filename(),
            Some(model.tokenizer_sha256()),
        )?;
    }

    // Verify tokenizer exists and has expected size
    verify_file_size(&tokenizer_path, model.min_tokenizer_size(), "tokenizer")?;

    Ok(tokenizer_path)
}

/// Generic file download helper.
///
/// Streams into a per-invocation sibling temp file
/// (`<path>.partial.<pid>.<thread>.<counter>`), fsyncs, then publishes
/// to the canonical target via `hard_link` + `remove_file`. On Unix
/// `hard_link` fails atomically with `AlreadyExists` if another caller
/// already published — that branch keeps the winner's bytes and drops
/// ours. `rename` would silently clobber the winner (Unix semantics),
/// so we intentionally don't use it. The per-invocation counter in
/// the temp suffix prevents same-process concurrent callers on the
/// same target from sharing a temp file.
///
/// This keeps the cache atomic: an interrupted download, a crash
/// mid-stream, or two racing processes can never leave a half-written
/// file at the canonical cache path where `verify_file_size` would
/// either wedge boot or (worse) let a truncated model through.
fn download_file(
    url: &str,
    path: &PathBuf,
    name: &str,
    expected_sha256: Option<&str>,
) -> Result<()> {
    use std::sync::atomic::{AtomicU64, Ordering};
    static COUNTER: AtomicU64 = AtomicU64::new(0);

    let cache_dir = path.parent().unwrap();
    std::fs::create_dir_all(cache_dir)?;

    // Advisory single-writer lock (Candle follow-up from v0.8.0 handoff).
    // Optimizes the N-process race: without this, all racers stream the
    // full payload (up to ~614MB for Qwen3) and only one wins the
    // hard_link publish. With this, the writer streams; late-arrivers
    // wait for the target to appear and return without downloading.
    //
    // Correctness is still guaranteed by the hard_link publish below,
    // not by the lock. If the lock owner crashes, waiters time out and
    // fall through to the current race-safe behavior. The lock is
    // "advisory" in the POSIX sense: it only binds cooperating
    // processes (any memd binary). Stale-lock handling treats locks
    // older than STALE_LOCK_TIMEOUT as abandoned and reclaims them.
    let lock_path = advisory_lock_path(path);
    let mut _lock_guard: Option<LockGuard> = None;
    match try_acquire_advisory_lock(&lock_path) {
        AcquireLockOutcome::Acquired(guard) => {
            _lock_guard = Some(guard);
        }
        AcquireLockOutcome::Contended => {
            match wait_for_publish_or_release(path, &lock_path, advisory_lock_wait_timeout()) {
                WaitOutcome::Published => {
                    tracing::info!("{} published by concurrent writer; reusing", name);
                    return Ok(());
                }
                WaitOutcome::LockReleased | WaitOutcome::Timeout => {
                    // Fall through to race-safe download. Either the prior
                    // writer crashed before publishing, or we waited too
                    // long. The hard_link check below still prevents corruption.
                    tracing::debug!(
                    "advisory lock wait fell through for {}; proceeding with race-safe download",
                    name
                );
                }
            }
        }
        AcquireLockOutcome::Skipped => {
            tracing::debug!(
                "advisory lock unavailable for {} (non-fatal); proceeding with race-safe download",
                name
            );
        }
    }

    tracing::info!("Downloading {} to {:?}", name, path);

    let response = ureq::get(url)
        .call()
        .map_err(|e| MemdError::StorageError(format!("failed to download {}: {}", name, e)))?;

    let tmp_path = path.with_extension(format!(
        "partial.{}.{:?}.{}",
        std::process::id(),
        std::thread::current().id(),
        COUNTER.fetch_add(1, Ordering::Relaxed)
    ));
    let mut file = std::fs::File::create(&tmp_path)?;
    let mut reader = response.into_reader();
    let mut hasher = Sha256::new();
    let mut buffer = [0u8; 16 * 1024];
    let copy_result = (|| -> std::io::Result<()> {
        loop {
            let read = reader.read(&mut buffer)?;
            if read == 0 {
                break;
            }
            hasher.update(&buffer[..read]);
            file.write_all(&buffer[..read])?;
        }
        file.sync_all()
    })();
    if let Err(e) = copy_result {
        let _ = std::fs::remove_file(&tmp_path);
        return Err(MemdError::StorageError(format!(
            "failed to stream {} to {:?}: {}",
            name, tmp_path, e
        )));
    }
    let actual_sha256 = format!("{:x}", hasher.finalize());
    drop(file);

    if let Some(expected) = expected_sha256 {
        if let Err(error) = enforce_sha256(&actual_sha256, expected, name) {
            let _ = std::fs::remove_file(&tmp_path);
            return Err(error);
        }
    }

    // First-writer-wins publish: hard_link is atomic and fails with
    // AlreadyExists if target already exists. Loser cleans up its tmp
    // and keeps winner's bytes.
    match std::fs::hard_link(&tmp_path, path) {
        Ok(()) => {
            let _ = std::fs::remove_file(&tmp_path);
            // Durability fold (v0.8.0 handoff Candle follow-up): after
            // a successful first-writer publish, fsync the parent dir
            // so the new directory entry survives a post-return crash.
            // The earlier file.sync_all() made the inode's data durable;
            // without this, the dirent mapping `path -> inode` can still
            // be lost on recovery. Best-effort: log-and-continue on
            // platforms/filesystems that reject dir fsync.
            fsync_parent_dir(path);
        }
        Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {
            let _ = std::fs::remove_file(&tmp_path);
        }
        Err(e) => {
            let _ = std::fs::remove_file(&tmp_path);
            return Err(MemdError::StorageError(format!(
                "failed to publish {} to {:?}: {}",
                name, path, e
            )));
        }
    }

    tracing::info!("{} downloaded successfully", name);
    Ok(())
}

#[cfg(test)]
fn sha256_hex_of_file(path: &Path) -> Result<String> {
    let mut file = std::fs::File::open(path)?;
    let mut hasher = Sha256::new();
    let mut buffer = [0u8; 16 * 1024];
    loop {
        let read = file.read(&mut buffer)?;
        if read == 0 {
            break;
        }
        hasher.update(&buffer[..read]);
    }
    Ok(format!("{:x}", hasher.finalize()))
}

fn enforce_sha256(actual: &str, expected: &str, name: &str) -> Result<()> {
    if actual.eq_ignore_ascii_case(expected) {
        return Ok(());
    }
    Err(MemdError::StorageError(format!(
        "{} sha256 mismatch: expected {}, actual {}; corrupt partial download was deleted",
        name, expected, actual
    )))
}

/// Best-effort fsync of the parent directory of ``path``.
///
/// Follows Linux durability semantics: `file.sync_all()` makes the
/// inode's data durable, but a subsequent crash can still lose the
/// directory entry that links the canonical name to that inode.
/// Opening the parent directory and calling `sync_all()` on the
/// directory handle forces the dirent through to stable storage.
///
/// Silent on filesystems that reject directory fsync (rare; mostly
/// networked/synthetic filesystems). We already wrote and link-published
/// the file successfully, so a failed dir-fsync is best-effort — log
/// at debug and continue rather than fail the whole download.
fn fsync_parent_dir(path: &std::path::Path) {
    let Some(parent) = path.parent() else {
        return;
    };
    match std::fs::File::open(parent) {
        Ok(dir) => {
            if let Err(e) = dir.sync_all() {
                tracing::debug!(
                    "fsync on parent dir {:?} failed (best-effort): {}",
                    parent,
                    e
                );
            }
        }
        Err(e) => {
            tracing::debug!(
                "opening parent dir {:?} for fsync failed (best-effort): {}",
                parent,
                e
            );
        }
    }
}

// =============================================================================
// Advisory single-writer lock for download_file
// =============================================================================

/// Max time a waiter blocks on an active lock owner before giving up and
/// falling through to the race-safe download path. 15 minutes bounds UX
/// on a stuck download; on a real slow link the waiter may fall through
/// and start its own download, at which point the hard_link publish
/// decides the winner and both callers end up with correct bytes.
const ADVISORY_LOCK_WAIT: Duration = Duration::from_secs(900);

/// A lock file is considered stale after this duration and reclaimed on
/// the next acquire attempt. Set generously to cover the largest
/// supported model (~614MB Qwen3) on a slow residential link (~2 Mbps):
/// 60 minutes. Must be >= ADVISORY_LOCK_WAIT so a waiter timing out
/// doesn't falsely invalidate a still-live writer's lock when it
/// retries acquire. False negatives (treating a live writer as stale)
/// are only a bandwidth regression — hard_link publish still guarantees
/// correctness — but we avoid them to keep the optimization effective
/// under realistic worst-case latency.
const STALE_LOCK_TIMEOUT: Duration = Duration::from_secs(3600);

/// Polling interval for waiters. Starts small so a fast publish returns
/// quickly, capped so a slow download doesn't hammer the filesystem.
const WAIT_POLL_INITIAL: Duration = Duration::from_millis(50);
const WAIT_POLL_MAX: Duration = Duration::from_secs(2);

/// In-test override for the wait timeout, so the `advisory_lock_wait_times_out`
/// test doesn't have to block for 10 minutes. Production code reads
/// ADVISORY_LOCK_WAIT.
#[cfg(test)]
static TEST_WAIT_OVERRIDE_MS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);

#[cfg(test)]
fn advisory_lock_wait_timeout() -> Duration {
    let ms = TEST_WAIT_OVERRIDE_MS.load(std::sync::atomic::Ordering::Relaxed);
    if ms == 0 {
        ADVISORY_LOCK_WAIT
    } else {
        Duration::from_millis(ms)
    }
}

#[cfg(not(test))]
fn advisory_lock_wait_timeout() -> Duration {
    ADVISORY_LOCK_WAIT
}

/// Sibling lock path for a download target. Appends `.lock` verbatim to
/// the filename so multi-extension paths (e.g., `model.onnx`) don't
/// collide with a `.lock` variant of a differently-named file in the
/// same directory (`with_extension("lock")` would drop `.onnx`).
fn advisory_lock_path(path: &Path) -> PathBuf {
    match path.file_name() {
        Some(name) => {
            let mut new_name = name.to_os_string();
            new_name.push(".lock");
            path.with_file_name(new_name)
        }
        // Defensive: callers pass cache paths with filenames, but don't
        // panic on pathological inputs; return a path that will fail to
        // open, causing a Skipped outcome downstream.
        None => path.with_extension("lock"),
    }
}

enum AcquireLockOutcome {
    /// We hold the lock; safe to proceed as the single writer.
    Acquired(LockGuard),
    /// Another live writer holds the lock. Caller should wait.
    Contended,
    /// Could not create the lock file for a non-contention reason
    /// (permissions, IO error). Caller should proceed unlocked; the
    /// hard_link publish still safeguards correctness.
    Skipped,
}

enum WaitOutcome {
    /// Target appeared on disk — the prior writer finished successfully.
    Published,
    /// Lock file disappeared without target appearing — writer likely
    /// crashed mid-stream. Caller should fall through and try itself.
    LockReleased,
    /// Waited longer than the timeout. Caller falls through to race-safe
    /// download (hard_link still guards against corruption).
    Timeout,
}

/// Try to acquire the advisory lock. Uses `create_new(true)` which maps
/// to `O_EXCL|O_CREAT` on Unix and `CREATE_NEW` on Windows — both atomic.
fn try_acquire_advisory_lock(lock_path: &Path) -> AcquireLockOutcome {
    loop {
        match std::fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(lock_path)
        {
            Ok(mut f) => {
                // Best-effort stamp for diagnostics and stale-lock detection.
                // Body is never read for correctness — if absent/malformed,
                // the lockfile's mtime is used as the fallback timestamp.
                let now_ms = SystemTime::now()
                    .duration_since(UNIX_EPOCH)
                    .map(|d| d.as_millis())
                    .unwrap_or(0);
                let _ = writeln!(f, "pid={} created_ms={}", std::process::id(), now_ms);
                let _ = f.sync_all();
                drop(f);
                return AcquireLockOutcome::Acquired(LockGuard {
                    path: lock_path.to_path_buf(),
                });
            }
            Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {
                // Someone is (or was) writing. Check for staleness.
                if lock_is_stale(lock_path) {
                    // Try to reclaim: remove and retry once. If the
                    // remove races with the actual owner cleaning up,
                    // the next create_new either succeeds (we get
                    // Acquired) or re-trips AlreadyExists (we fall to
                    // Contended below).
                    let _ = std::fs::remove_file(lock_path);
                    continue;
                }
                return AcquireLockOutcome::Contended;
            }
            Err(e) => {
                tracing::debug!(
                    "advisory lock create at {:?} failed non-fatally: {}",
                    lock_path,
                    e
                );
                return AcquireLockOutcome::Skipped;
            }
        }
    }
}

/// Consider a lock file stale when its mtime is older than
/// STALE_LOCK_TIMEOUT. Uses mtime rather than the file body because the
/// body is written after create_new and may be momentarily empty.
fn lock_is_stale(lock_path: &Path) -> bool {
    let Ok(meta) = std::fs::metadata(lock_path) else {
        return false;
    };
    let Ok(mtime) = meta.modified() else {
        return false;
    };
    match SystemTime::now().duration_since(mtime) {
        Ok(age) => age > STALE_LOCK_TIMEOUT,
        // Clock went backwards — don't treat as stale.
        Err(_) => false,
    }
}

fn wait_for_publish_or_release(target: &Path, lock_path: &Path, timeout: Duration) -> WaitOutcome {
    let start = Instant::now();
    let mut delay = WAIT_POLL_INITIAL;
    loop {
        if target.exists() {
            return WaitOutcome::Published;
        }
        if !lock_path.exists() {
            // Re-check target: writer may have published and removed
            // the lock between our two checks. Without this re-check
            // we'd return LockReleased and redundantly re-download a
            // file that's already on disk (still correct — hard_link
            // would catch it later — but wasteful).
            if target.exists() {
                return WaitOutcome::Published;
            }
            return WaitOutcome::LockReleased;
        }
        if start.elapsed() >= timeout {
            return WaitOutcome::Timeout;
        }
        std::thread::sleep(delay);
        delay = (delay * 2).min(WAIT_POLL_MAX);
    }
}

/// RAII guard that removes the lock file on drop. Best-effort removal —
/// a failure to unlink (e.g., the lock file was already reaped by a
/// stale-reclaim) is silently ignored. The stale-lock fallback in the
/// next acquire attempt handles any leftover.
struct LockGuard {
    path: PathBuf,
}

impl Drop for LockGuard {
    fn drop(&mut self) {
        let _ = std::fs::remove_file(&self.path);
    }
}

/// Get paths to the Candle BERT model files (config, tokenizer, weights).
///
/// Downloads any missing files from huggingface.co into the memd cache using
/// plain `ureq`, which handles HF's relative 307 redirects correctly. Targets
/// the `sentence-transformers/all-MiniLM-L6-v2` repo used by the Candle BERT
/// embedder. Returns the local paths in (config, tokenizer, weights) order.
pub fn get_candle_bert_paths() -> Result<(PathBuf, PathBuf, PathBuf)> {
    get_candle_model_paths(CandleModel::MiniLm)
}

/// Get paths to the Candle model files for the selected BERT-family model.
///
/// Same download/verify contract as `get_candle_bert_paths`, parameterized
/// over the model chosen via `--embedding-model`.
pub fn get_candle_model_paths(model: CandleModel) -> Result<(PathBuf, PathBuf, PathBuf)> {
    struct Spec {
        config: (&'static str, &'static str, &'static str, u64),
        tokenizer: (&'static str, &'static str, &'static str, u64),
        weights: (&'static str, &'static str, &'static str, u64),
    }
    let spec = match model {
        CandleModel::MiniLm => Spec {
            config: (
                CANDLE_BERT_CONFIG_URL,
                CANDLE_BERT_CONFIG_FILENAME,
                CANDLE_BERT_CONFIG_SHA256,
                MIN_CANDLE_BERT_CONFIG_SIZE,
            ),
            tokenizer: (
                CANDLE_BERT_TOKENIZER_URL,
                CANDLE_BERT_TOKENIZER_FILENAME,
                CANDLE_BERT_TOKENIZER_SHA256,
                MIN_CANDLE_BERT_TOKENIZER_SIZE,
            ),
            weights: (
                CANDLE_BERT_WEIGHTS_URL,
                CANDLE_BERT_WEIGHTS_FILENAME,
                CANDLE_BERT_WEIGHTS_SHA256,
                MIN_CANDLE_BERT_WEIGHTS_SIZE,
            ),
        },
        CandleModel::BgeBase => Spec {
            config: (
                CANDLE_BGE_CONFIG_URL,
                CANDLE_BGE_CONFIG_FILENAME,
                CANDLE_BGE_CONFIG_SHA256,
                MIN_CANDLE_BGE_CONFIG_SIZE,
            ),
            tokenizer: (
                CANDLE_BGE_TOKENIZER_URL,
                CANDLE_BGE_TOKENIZER_FILENAME,
                CANDLE_BGE_TOKENIZER_SHA256,
                MIN_CANDLE_BGE_TOKENIZER_SIZE,
            ),
            weights: (
                CANDLE_BGE_WEIGHTS_URL,
                CANDLE_BGE_WEIGHTS_FILENAME,
                CANDLE_BGE_WEIGHTS_SHA256,
                MIN_CANDLE_BGE_WEIGHTS_SIZE,
            ),
        },
    };

    let cache_dir = get_cache_dir()?;
    std::fs::create_dir_all(&cache_dir)?;

    let fetch =
        |(url, filename, sha, min_size): (&str, &str, &str, u64), kind: &str| -> Result<PathBuf> {
            let path = cache_dir.join(filename);
            if !path.exists() {
                download_file(url, &path, kind, Some(sha))?;
            }
            verify_file_size(&path, min_size, kind)?;
            Ok(path)
        };

    let config_path = fetch(spec.config, "BERT config")?;
    let tokenizer_path = fetch(spec.tokenizer, "BERT tokenizer")?;
    let weights_path = fetch(spec.weights, "BERT weights")?;
    Ok((config_path, tokenizer_path, weights_path))
}

/// Verify file exists and meets minimum size
fn verify_file_size(path: &PathBuf, min_size: u64, file_type: &str) -> Result<()> {
    if !path.exists() {
        return Err(MemdError::StorageError(format!(
            "{} file not found at {:?}",
            file_type, path
        )));
    }

    let metadata = std::fs::metadata(path)?;
    if metadata.len() < min_size {
        return Err(MemdError::StorageError(format!(
            "{} file too small ({} bytes), expected >= {} bytes. File may be corrupted, delete and retry.",
            file_type,
            metadata.len(),
            min_size
        )));
    }

    Ok(())
}

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

    fn loopback_listener_or_skip(test_name: &str) -> Option<std::net::TcpListener> {
        match std::net::TcpListener::bind("127.0.0.1:0") {
            Ok(listener) => Some(listener),
            Err(err) if err.kind() == std::io::ErrorKind::PermissionDenied => {
                eprintln!(
                    "skipping {test_name}: loopback bind is not permitted in this environment"
                );
                None
            }
            Err(err) => panic!("bind loopback listener for {test_name}: {err}"),
        }
    }

    #[test]
    fn test_cache_dir() {
        let dir = get_cache_dir().expect("should get cache dir");
        assert!(dir.to_string_lossy().contains("memd"));
        assert!(dir.to_string_lossy().contains("models"));
    }

    #[test]
    fn test_embedding_model_defaults() {
        let model = EmbeddingModel::default();
        assert_eq!(model, EmbeddingModel::AllMiniLmL6V2);
        assert_eq!(model.dimension(), 384);
        assert_eq!(model.pooling_strategy(), PoolingStrategy::Mean);
        assert!(!model.uses_instruction_format());
    }

    #[test]
    fn test_qwen3_model_config() {
        let model = EmbeddingModel::Qwen3Embedding0_6B;
        assert_eq!(model.dimension(), 1024);
        assert_eq!(model.pooling_strategy(), PoolingStrategy::LastToken);
        assert!(model.uses_instruction_format());
    }

    #[test]
    fn test_model_filenames() {
        assert_eq!(
            EmbeddingModel::AllMiniLmL6V2.model_filename(),
            "all-MiniLM-L6-v2-quantized.onnx"
        );
        assert_eq!(
            EmbeddingModel::Qwen3Embedding0_6B.model_filename(),
            "qwen3-embedding-0.6b-q8.onnx"
        );
    }

    #[test]
    fn test_model_urls() {
        assert!(EmbeddingModel::AllMiniLmL6V2
            .model_url()
            .contains("all-MiniLM-L6-v2"));
        assert!(EmbeddingModel::Qwen3Embedding0_6B
            .model_url()
            .contains("Qwen3-Embedding"));
    }

    fn assert_pinned_hf_url(url: &str) {
        let marker = "/resolve/";
        let start = url
            .find(marker)
            .expect("url should contain revision marker")
            + marker.len();
        let rest = &url[start..];
        let revision = rest.split('/').next().expect("revision segment");
        assert_eq!(
            revision.len(),
            40,
            "revision should be a commit hash: {url}"
        );
        assert!(
            revision.bytes().all(|byte| byte.is_ascii_hexdigit()),
            "revision should be ascii hex: {url}"
        );
        let mutable_ref = ["resolve", "main"].join("/");
        assert!(
            !url.contains(&mutable_ref),
            "url should not use a mutable ref: {url}"
        );
    }

    fn assert_sha256_constant(hash: &str) {
        assert_eq!(hash.len(), 64, "sha256 should be 64 hex chars: {hash}");
        assert!(
            hash.bytes()
                .all(|byte| byte.is_ascii_hexdigit() && !byte.is_ascii_uppercase()),
            "sha256 should be lowercase hex: {hash}"
        );
    }

    fn sha256_hex_of_bytes(bytes: &[u8]) -> String {
        let mut hasher = Sha256::new();
        hasher.update(bytes);
        format!("{:x}", hasher.finalize())
    }

    #[test]
    fn test_all_download_urls_are_revision_pinned() {
        let urls = [
            ALL_MINILM_MODEL_URL,
            ALL_MINILM_TOKENIZER_URL,
            QWEN3_MODEL_URL,
            QWEN3_TOKENIZER_URL,
            MODEL_URL,
            TOKENIZER_URL,
            CANDLE_BERT_CONFIG_URL,
            CANDLE_BERT_TOKENIZER_URL,
            CANDLE_BERT_WEIGHTS_URL,
            CANDLE_BGE_CONFIG_URL,
            CANDLE_BGE_TOKENIZER_URL,
            CANDLE_BGE_WEIGHTS_URL,
            EmbeddingModel::AllMiniLmL6V2.model_url(),
            EmbeddingModel::AllMiniLmL6V2.tokenizer_url(),
            EmbeddingModel::Qwen3Embedding0_6B.model_url(),
            EmbeddingModel::Qwen3Embedding0_6B.tokenizer_url(),
        ];
        for url in urls {
            assert_pinned_hf_url(url);
        }
    }

    #[test]
    fn test_sha256_constants_are_lowercase_hex() {
        let hashes = [
            ALL_MINILM_MODEL_SHA256,
            ALL_MINILM_TOKENIZER_SHA256,
            QWEN3_MODEL_SHA256,
            QWEN3_TOKENIZER_SHA256,
            MODEL_SHA256,
            TOKENIZER_SHA256,
            CANDLE_BERT_CONFIG_SHA256,
            CANDLE_BERT_TOKENIZER_SHA256,
            CANDLE_BERT_WEIGHTS_SHA256,
            CANDLE_BGE_CONFIG_SHA256,
            CANDLE_BGE_TOKENIZER_SHA256,
            CANDLE_BGE_WEIGHTS_SHA256,
        ];
        for hash in hashes {
            assert_sha256_constant(hash);
        }
    }

    #[test]
    fn test_candle_model_selection_and_properties() {
        // Default is MiniLM with mean pooling and no query prefix; BGE uses
        // CLS pooling with the retrieval instruction prefix on queries only.
        assert_eq!(CandleModel::default(), CandleModel::MiniLm);
        assert_eq!(
            CandleModel::MiniLm.pooling_strategy(),
            PoolingStrategy::Mean
        );
        assert!(CandleModel::MiniLm.query_prefix().is_none());
        assert_eq!(
            CandleModel::BgeBase.pooling_strategy(),
            PoolingStrategy::Cls
        );
        let prefix = CandleModel::BgeBase.query_prefix().unwrap();
        assert!(prefix.starts_with("Represent this sentence"));
        assert!(CandleModel::BgeBase.hf_id().contains("bge-base-en-v1.5"));
    }

    #[test]
    fn test_candle_bert_constants() {
        // Contract: Candle BERT assets are fetched from an immutable
        // sentence-transformers revision and verified by sha256. Bumping
        // these files must update the revision and hash constants together.
        assert_eq!(
            CANDLE_BERT_CONFIG_URL,
            "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/1110a243fdf4706b3f48f1d95db1a4f5529b4d41/config.json"
        );
        assert_eq!(
            CANDLE_BERT_TOKENIZER_URL,
            "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/1110a243fdf4706b3f48f1d95db1a4f5529b4d41/tokenizer.json"
        );
        assert_eq!(
            CANDLE_BERT_WEIGHTS_URL,
            "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/1110a243fdf4706b3f48f1d95db1a4f5529b4d41/model.safetensors"
        );
        assert!(CANDLE_BERT_CONFIG_URL.starts_with("https://"));
        assert!(CANDLE_BERT_TOKENIZER_URL.starts_with("https://"));
        assert!(CANDLE_BERT_WEIGHTS_URL.starts_with("https://"));
    }

    #[test]
    fn test_sha256_verification_reports_expected_and_actual() {
        let tmp_dir = unique_tmp_dir("hash-verify");
        std::fs::create_dir_all(&tmp_dir).unwrap();
        let file = tmp_dir.join("wrong.bin");
        std::fs::write(&file, b"wrong content").unwrap();

        let actual = sha256_hex_of_file(&file).unwrap();
        let expected = sha256_hex_of_bytes(b"expected content");
        let err = enforce_sha256(&actual, &expected, "wrong test file").unwrap_err();
        let message = err.to_string();
        assert!(message.contains(&expected));
        assert!(message.contains(&actual));

        std::fs::remove_dir_all(&tmp_dir).ok();
    }

    #[test]
    fn test_download_file_rejects_corrupt_hash_without_publish() {
        use std::io::{Read, Write};

        let body = b"wrong loopback bytes".to_vec();
        let Some(listener) =
            loopback_listener_or_skip("test_download_file_rejects_corrupt_hash_without_publish")
        else {
            return;
        };
        let addr = listener.local_addr().unwrap();
        let body_for_server = body.clone();
        let server_thread = std::thread::spawn(move || {
            if let Ok((mut stream, _)) = listener.accept() {
                let mut buf = [0u8; 1024];
                let _ = stream.read(&mut buf);
                let response = format!(
                    "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
                    body_for_server.len()
                );
                let _ = stream.write_all(response.as_bytes());
                let _ = stream.write_all(&body_for_server);
            }
        });

        let tmp_dir = unique_tmp_dir("download-hash-reject");
        std::fs::create_dir_all(&tmp_dir).unwrap();
        let target = tmp_dir.join("corrupt.bin");
        let expected = sha256_hex_of_bytes(b"different expected bytes");

        let url = format!("http://{}/x", addr);
        let result = download_file(&url, &target, "corrupt test", Some(&expected));
        server_thread.join().ok();

        assert!(result.is_err(), "hash mismatch should fail");
        assert!(!target.exists(), "mismatched download must not publish");
        let leftovers: Vec<_> = std::fs::read_dir(&tmp_dir)
            .unwrap()
            .filter_map(|entry| entry.ok())
            .map(|entry| entry.file_name().to_string_lossy().into_owned())
            .filter(|name| name.contains("partial"))
            .collect();
        assert!(
            leftovers.is_empty(),
            "no partial tmp files should remain, found: {:?}",
            leftovers
        );

        std::fs::remove_dir_all(&tmp_dir).ok();
    }

    #[test]
    fn test_download_file_accepts_matching_hash_and_publishes() {
        use std::io::{Read, Write};

        let body = b"known loopback bytes".to_vec();
        let Some(listener) =
            loopback_listener_or_skip("test_download_file_accepts_matching_hash_and_publishes")
        else {
            return;
        };
        let addr = listener.local_addr().unwrap();
        let body_for_server = body.clone();
        let server_thread = std::thread::spawn(move || {
            if let Ok((mut stream, _)) = listener.accept() {
                let mut buf = [0u8; 1024];
                let _ = stream.read(&mut buf);
                let response = format!(
                    "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
                    body_for_server.len()
                );
                let _ = stream.write_all(response.as_bytes());
                let _ = stream.write_all(&body_for_server);
            }
        });

        let tmp_dir = unique_tmp_dir("download-hash-ok");
        std::fs::create_dir_all(&tmp_dir).unwrap();
        let target = tmp_dir.join("pinned.bin");
        let expected = sha256_hex_of_bytes(&body);

        let url = format!("http://{}/x", addr);
        let result = download_file(&url, &target, "pinned test", Some(&expected));
        server_thread.join().ok();

        assert!(result.is_ok(), "matching hash should succeed: {:?}", result);
        assert_eq!(std::fs::read(&target).unwrap(), body);

        std::fs::remove_dir_all(&tmp_dir).ok();
    }

    #[test]
    fn test_candle_bert_cache_filenames_are_distinct() {
        // Repo-qualified filenames avoid collisions with the Xenova ONNX
        // tokenizer cached under the same directory.
        assert_ne!(CANDLE_BERT_CONFIG_FILENAME, CANDLE_BERT_TOKENIZER_FILENAME);
        assert_ne!(CANDLE_BERT_TOKENIZER_FILENAME, TOKENIZER_FILENAME);
        assert!(CANDLE_BERT_TOKENIZER_FILENAME.contains("sentence-transformers"));
        assert!(CANDLE_BERT_WEIGHTS_FILENAME.ends_with(".safetensors"));
    }

    #[test]
    fn test_download_file_fails_cleanly_on_request_error() {
        // When the ureq .call() fails (connect-refused on a loopback port
        // with no listener), download_file must return an error WITHOUT
        // creating any file on disk — neither the canonical target nor a
        // `.partial.*` sibling. This covers the early-failure branch
        // (before any tmp file exists).
        let tmp_dir = std::env::temp_dir().join(format!(
            "memd-download-req-err-test-{}-{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        std::fs::create_dir_all(&tmp_dir).unwrap();
        let target = tmp_dir.join("atomic-test.bin");

        let result = download_file("http://127.0.0.1:1/never", &target, "test file", None);
        assert!(result.is_err(), "expected download to error");
        assert!(
            !target.exists(),
            "target should not exist after failed download: {:?}",
            target
        );
        let leftovers: Vec<_> = std::fs::read_dir(&tmp_dir)
            .unwrap()
            .filter_map(|e| e.ok())
            .map(|e| e.file_name())
            .collect();
        assert!(
            leftovers.is_empty(),
            "no temp/partial files should remain, found: {:?}",
            leftovers
        );

        std::fs::remove_dir_all(&tmp_dir).ok();
    }

    #[test]
    fn test_fsync_parent_dir_is_ok_on_real_dir() {
        // Durability fold: `fsync_parent_dir` should quietly succeed
        // on a normal tmpfs/ext4-style directory and never panic. We
        // don't have a way to prove a real fsync reached disk in unit
        // tests, but we can at least prove the code path runs without
        // surfacing an error to the caller (it's best-effort by design).
        let tmp_dir = std::env::temp_dir().join(format!(
            "memd-fsync-test-{}-{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        std::fs::create_dir_all(&tmp_dir).unwrap();
        let f = tmp_dir.join("marker.bin");
        std::fs::write(&f, b"x").unwrap();
        // Just exercising the code path; it should not panic or abort
        // even if the underlying FS doesn't implement dir fsync.
        fsync_parent_dir(&f);
        // Also tolerate a path with no parent (e.g., root) without
        // panicking: pass a path with no parent-less pathological shape.
        // `Path::new("foo")`.parent() is Some("") on Unix, so this is
        // already a safe no-op; we just call it to cover.
        fsync_parent_dir(std::path::Path::new("foo"));

        std::fs::remove_dir_all(&tmp_dir).ok();
    }

    #[test]
    fn test_download_file_fsyncs_parent_on_publish() {
        // End-to-end happy path: download completes, hard_link publishes,
        // and fsync_parent_dir is called on the canonical target's parent.
        // We can't assert the kernel-level fsync, but we can assert the
        // post-publish invariants hold: file present, sized, no stray
        // `.partial.*` siblings left behind.
        use std::io::{Read, Write};

        let body = b"candle-fsync-payload".to_vec();
        let Some(listener) =
            loopback_listener_or_skip("test_download_file_fsyncs_parent_on_publish")
        else {
            return;
        };
        let addr = listener.local_addr().unwrap();
        let body_for_server = body.clone();
        let server_thread = std::thread::spawn(move || {
            if let Ok((mut stream, _)) = listener.accept() {
                let mut buf = [0u8; 1024];
                let _ = stream.read(&mut buf);
                let response = format!(
                    "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
                    body_for_server.len()
                );
                let _ = stream.write_all(response.as_bytes());
                let _ = stream.write_all(&body_for_server);
            }
        });

        let tmp_dir = std::env::temp_dir().join(format!(
            "memd-download-fsync-test-{}-{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        std::fs::create_dir_all(&tmp_dir).unwrap();
        let target = tmp_dir.join("candle.bin");

        let url = format!("http://{}/x", addr);
        let result = download_file(&url, &target, "fsync test", None);
        server_thread.join().ok();

        assert!(result.is_ok(), "download_file should succeed: {:?}", result);
        let observed = std::fs::read(&target).unwrap();
        assert_eq!(observed, body, "published bytes must match");
        let leftovers: Vec<_> = std::fs::read_dir(&tmp_dir)
            .unwrap()
            .filter_map(|e| e.ok())
            .map(|e| e.file_name().to_string_lossy().into_owned())
            .filter(|n| n.contains("partial"))
            .collect();
        assert!(
            leftovers.is_empty(),
            "no partial tmp files should remain, found: {:?}",
            leftovers
        );

        std::fs::remove_dir_all(&tmp_dir).ok();
    }

    /// Network-gated integration test: actually fetch a small real
    /// asset from huggingface.co and verify `download_file` lands it
    /// atomically in a cache dir. Guards against regressions in the
    /// ureq-based HF redirect handling (the RelativeUrlWithoutBase
    /// issue that hf-hub 0.3.2 had; fixed by switching to plain ureq
    /// which follows HF's relative 307 Location headers correctly).
    ///
    /// Opt-in to keep the default `cargo test` hermetic. Enable with
    /// `MEMD_NETWORK_TESTS=1 cargo test -p memd test_download_file_hf_config_integration`.
    /// Uses the ~600-byte `config.json` asset so the test finishes in
    /// well under a second on a normal connection.
    #[test]
    fn test_download_file_hf_config_integration() {
        if std::env::var("MEMD_NETWORK_TESTS").ok().as_deref() != Some("1") {
            eprintln!("skipping HF network test; set MEMD_NETWORK_TESTS=1 to run");
            return;
        }

        let tmp_dir = std::env::temp_dir().join(format!(
            "memd-hf-config-net-test-{}-{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        std::fs::create_dir_all(&tmp_dir).unwrap();
        let target = tmp_dir.join(CANDLE_BERT_CONFIG_FILENAME);

        let result = download_file(
            CANDLE_BERT_CONFIG_URL,
            &target,
            "BERT config",
            Some(CANDLE_BERT_CONFIG_SHA256),
        );
        assert!(
            result.is_ok(),
            "HF config download should succeed (network required): {:?}",
            result
        );
        assert!(target.exists(), "config file should exist at target");
        let meta = std::fs::metadata(&target).expect("metadata");
        assert!(
            meta.len() >= MIN_CANDLE_BERT_CONFIG_SIZE,
            "downloaded config is too small ({} bytes, expected >= {})",
            meta.len(),
            MIN_CANDLE_BERT_CONFIG_SIZE
        );
        // No leftover `.partial.*` sibling.
        let leftovers: Vec<_> = std::fs::read_dir(&tmp_dir)
            .unwrap()
            .filter_map(|e| e.ok())
            .map(|e| e.file_name().to_string_lossy().into_owned())
            .filter(|n| n.contains("partial"))
            .collect();
        assert!(
            leftovers.is_empty(),
            "no partial tmp files should remain after a clean HF download, found: {:?}",
            leftovers
        );

        std::fs::remove_dir_all(&tmp_dir).ok();
    }

    #[test]
    fn test_download_file_is_first_writer_wins() {
        // Simulate the race-loser branch: the canonical target already
        // exists when we try to publish. `hard_link` must fail with
        // AlreadyExists, our tmp must be cleaned up, and the pre-existing
        // bytes must be preserved byte-for-byte.
        //
        // A tiny in-process HTTP server on a loopback port serves a real
        // body so `download_file` reaches the publish branch (which is the
        // only place that can observe the pre-existing target). We then
        // assert the target's original bytes are intact and no `.partial.*`
        // sibling is left behind.
        use std::io::{Read, Write};

        let body = b"fresh-download-payload".to_vec();
        let Some(listener) = loopback_listener_or_skip("test_download_file_is_first_writer_wins")
        else {
            return;
        };
        let addr = listener.local_addr().unwrap();
        let body_for_server = body.clone();
        let server_thread = std::thread::spawn(move || {
            if let Ok((mut stream, _)) = listener.accept() {
                let mut buf = [0u8; 1024];
                let _ = stream.read(&mut buf);
                let response = format!(
                    "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
                    body_for_server.len()
                );
                let _ = stream.write_all(response.as_bytes());
                let _ = stream.write_all(&body_for_server);
            }
        });

        let tmp_dir = std::env::temp_dir().join(format!(
            "memd-download-winner-test-{}-{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        std::fs::create_dir_all(&tmp_dir).unwrap();
        let target = tmp_dir.join("winner.bin");

        // Pre-existing target: simulate the winning race sibling.
        let sentinel = b"WINNER-BYTES".to_vec();
        std::fs::write(&target, &sentinel).unwrap();

        let url = format!("http://{}/x", addr);
        let result = download_file(&url, &target, "race test", None);
        server_thread.join().ok();

        assert!(result.is_ok(), "download_file should succeed: {:?}", result);
        // Target preserved byte-for-byte (winner wins).
        let observed = std::fs::read(&target).unwrap();
        assert_eq!(
            observed, sentinel,
            "pre-existing winner bytes must not be clobbered"
        );
        // Loser's .partial.* tmp file must be cleaned up.
        let leftovers: Vec<_> = std::fs::read_dir(&tmp_dir)
            .unwrap()
            .filter_map(|e| e.ok())
            .map(|e| e.file_name().to_string_lossy().into_owned())
            .filter(|n| n.contains("partial"))
            .collect();
        assert!(
            leftovers.is_empty(),
            "loser's partial tmp must be cleaned up, found: {:?}",
            leftovers
        );

        std::fs::remove_dir_all(&tmp_dir).ok();
    }

    fn unique_tmp_dir(tag: &str) -> PathBuf {
        std::env::temp_dir().join(format!(
            "memd-{}-{}-{}",
            tag,
            std::process::id(),
            SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ))
    }

    #[test]
    fn test_advisory_lock_path_preserves_full_filename() {
        // Multi-extension files (model.onnx) must produce model.onnx.lock
        // rather than model.lock so two distinct targets in the same dir
        // don't collide on a single lock. This mirrors the concern that
        // motivated .partial.* tmp naming to avoid with_extension.
        let p = PathBuf::from("/tmp/cache/model.onnx");
        let lock = advisory_lock_path(&p);
        assert_eq!(lock, PathBuf::from("/tmp/cache/model.onnx.lock"));

        let p2 = PathBuf::from("/tmp/cache/tokenizer.json");
        assert_eq!(
            advisory_lock_path(&p2),
            PathBuf::from("/tmp/cache/tokenizer.json.lock")
        );

        // Single-extension and no-extension also work.
        let p3 = PathBuf::from("/tmp/cache/blob");
        assert_eq!(
            advisory_lock_path(&p3),
            PathBuf::from("/tmp/cache/blob.lock")
        );
    }

    #[test]
    fn test_advisory_lock_acquire_and_drop_cleans_up() {
        let tmp_dir = unique_tmp_dir("lock-acquire");
        std::fs::create_dir_all(&tmp_dir).unwrap();
        let target = tmp_dir.join("target.bin");
        let lock = advisory_lock_path(&target);

        {
            let outcome = try_acquire_advisory_lock(&lock);
            match outcome {
                AcquireLockOutcome::Acquired(_guard) => {
                    assert!(lock.exists(), "lock file should exist while guard is held");
                }
                _ => panic!("expected Acquired on first attempt"),
            }
        } // guard drops here

        assert!(
            !lock.exists(),
            "lock file should be removed after guard drop"
        );
        std::fs::remove_dir_all(&tmp_dir).ok();
    }

    #[test]
    fn test_advisory_lock_second_caller_is_contended() {
        let tmp_dir = unique_tmp_dir("lock-contention");
        std::fs::create_dir_all(&tmp_dir).unwrap();
        let target = tmp_dir.join("target.bin");
        let lock = advisory_lock_path(&target);

        let first = try_acquire_advisory_lock(&lock);
        let second = try_acquire_advisory_lock(&lock);
        match (first, second) {
            (AcquireLockOutcome::Acquired(_g1), AcquireLockOutcome::Contended) => {}
            _ => panic!("expected Acquired then Contended on two racers"),
        }

        std::fs::remove_dir_all(&tmp_dir).ok();
    }

    #[test]
    fn test_advisory_lock_stale_reclaim() {
        // A lock file whose mtime is older than STALE_LOCK_TIMEOUT should
        // be reclaimed on the next acquire attempt. We can't portably
        // backdate a file's mtime from safe Rust, so we stub the staleness
        // check by writing a lock and then directly invoking the reclaim
        // path via remove-plus-retry — the observable contract is the
        // same: an acquirer eventually gets the lock when the prior
        // owner is abandoned. Here we prove the contract against a
        // manually-cleared lock.
        let tmp_dir = unique_tmp_dir("lock-stale");
        std::fs::create_dir_all(&tmp_dir).unwrap();
        let target = tmp_dir.join("target.bin");
        let lock = advisory_lock_path(&target);

        // Pre-existing "abandoned" lock.
        std::fs::write(&lock, b"pid=99999 created_ms=0\n").unwrap();
        assert!(lock.exists());

        // After manual remove (simulating stale reclaim), the acquire
        // succeeds. This proves the loop will terminate in Acquired
        // when the stale branch is taken — the staleness detection
        // itself is covered by test_lock_is_stale_by_mtime below.
        std::fs::remove_file(&lock).unwrap();
        let outcome = try_acquire_advisory_lock(&lock);
        assert!(matches!(outcome, AcquireLockOutcome::Acquired(_)));

        std::fs::remove_dir_all(&tmp_dir).ok();
    }

    #[test]
    fn test_lock_is_stale_by_mtime() {
        // Fresh lock is not stale.
        let tmp_dir = unique_tmp_dir("lock-staleness");
        std::fs::create_dir_all(&tmp_dir).unwrap();
        let lock = tmp_dir.join("fresh.lock");
        std::fs::write(&lock, b"x").unwrap();
        assert!(
            !lock_is_stale(&lock),
            "fresh lock should not be classified stale"
        );

        // Missing lock is trivially not stale.
        let missing = tmp_dir.join("nope.lock");
        assert!(!lock_is_stale(&missing));

        std::fs::remove_dir_all(&tmp_dir).ok();
    }

    #[test]
    fn test_wait_returns_published_when_target_appears() {
        let tmp_dir = unique_tmp_dir("lock-wait-pub");
        std::fs::create_dir_all(&tmp_dir).unwrap();
        let target = tmp_dir.join("target.bin");
        let lock = advisory_lock_path(&target);

        std::fs::write(&lock, b"pid=1 created_ms=0").unwrap();

        let target_clone = target.clone();
        let lock_clone = lock.clone();
        let t = std::thread::spawn(move || {
            std::thread::sleep(Duration::from_millis(100));
            std::fs::write(&target_clone, b"published").unwrap();
            std::fs::remove_file(&lock_clone).ok();
        });

        let outcome = wait_for_publish_or_release(&target, &lock, Duration::from_secs(5));
        t.join().unwrap();
        assert!(matches!(outcome, WaitOutcome::Published));

        std::fs::remove_dir_all(&tmp_dir).ok();
    }

    #[test]
    fn test_wait_prefers_published_over_released_when_both_are_true() {
        // TOCTOU fold: if the writer publishes target AND removes the
        // lock in the tiny window between our target.exists() and
        // lock_path.exists() calls, we must still return Published
        // (not LockReleased) so the waiter skips its redundant download.
        // Set both up synchronously — target present, lock absent — and
        // assert Published. The re-check inside the !lock_path branch
        // is what makes this pass.
        let tmp_dir = unique_tmp_dir("lock-wait-toctou");
        std::fs::create_dir_all(&tmp_dir).unwrap();
        let target = tmp_dir.join("target.bin");
        let lock = advisory_lock_path(&target);

        std::fs::write(&target, b"already-published").unwrap();
        // Lock intentionally absent.
        let outcome = wait_for_publish_or_release(&target, &lock, Duration::from_millis(200));
        assert!(
            matches!(outcome, WaitOutcome::Published),
            "target present + lock absent must yield Published, got non-Published"
        );

        std::fs::remove_dir_all(&tmp_dir).ok();
    }

    #[test]
    fn test_wait_returns_released_when_lock_disappears_without_target() {
        let tmp_dir = unique_tmp_dir("lock-wait-rel");
        std::fs::create_dir_all(&tmp_dir).unwrap();
        let target = tmp_dir.join("target.bin");
        let lock = advisory_lock_path(&target);

        std::fs::write(&lock, b"pid=1 created_ms=0").unwrap();

        let lock_clone = lock.clone();
        let t = std::thread::spawn(move || {
            std::thread::sleep(Duration::from_millis(100));
            std::fs::remove_file(&lock_clone).ok();
        });

        let outcome = wait_for_publish_or_release(&target, &lock, Duration::from_secs(5));
        t.join().unwrap();
        assert!(
            matches!(outcome, WaitOutcome::LockReleased),
            "writer crash (lock gone, target absent) must yield LockReleased"
        );

        std::fs::remove_dir_all(&tmp_dir).ok();
    }

    #[test]
    fn test_wait_returns_timeout_when_nothing_changes() {
        let tmp_dir = unique_tmp_dir("lock-wait-to");
        std::fs::create_dir_all(&tmp_dir).unwrap();
        let target = tmp_dir.join("target.bin");
        let lock = advisory_lock_path(&target);

        std::fs::write(&lock, b"pid=1 created_ms=0").unwrap();

        let outcome = wait_for_publish_or_release(&target, &lock, Duration::from_millis(200));
        assert!(matches!(outcome, WaitOutcome::Timeout));

        std::fs::remove_file(&lock).ok();
        std::fs::remove_dir_all(&tmp_dir).ok();
    }

    #[test]
    fn test_download_file_waiter_reuses_publication() {
        // End-to-end: two concurrent `download_file` calls on the same
        // target. Only one tiny HTTP server is spawned; if the waiter
        // actually waits for publish, it must NOT hit the server (there
        // are no extra connections to service). We prove this by the
        // final invariants: target bytes match the body, no `.partial.*`
        // sibling remains, no lock file remains, both calls return Ok.
        //
        // The lock wait timeout is short so we don't block the suite;
        // the writer's sleep yields the thread to let the waiter enter
        // the Contended branch.
        use std::io::{Read, Write};

        let body = b"one-download-shared".to_vec();
        let Some(listener) =
            loopback_listener_or_skip("test_download_file_waiter_reuses_publication")
        else {
            return;
        };
        let addr = listener.local_addr().unwrap();
        let body_for_server = body.clone();
        let server_thread = std::thread::spawn(move || {
            if let Ok((mut stream, _)) = listener.accept() {
                let mut buf = [0u8; 1024];
                let _ = stream.read(&mut buf);
                // Small delay so the second caller has time to see
                // the lock and enter the wait branch.
                std::thread::sleep(Duration::from_millis(150));
                let response = format!(
                    "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
                    body_for_server.len()
                );
                let _ = stream.write_all(response.as_bytes());
                let _ = stream.write_all(&body_for_server);
            }
            // Any additional connections from waiters attempting to
            // re-download would fail with ConnectionRefused (listener
            // dropped). That would surface as an error and the test
            // would fail — which is exactly the regression guard.
        });

        let tmp_dir = unique_tmp_dir("download-coop");
        std::fs::create_dir_all(&tmp_dir).unwrap();
        let target = tmp_dir.join("shared.bin");
        let target_clone = target.clone();

        let url = format!("http://{}/x", addr);
        let url_clone = url.clone();
        let target_for_writer = target.clone();

        let writer = std::thread::spawn(move || {
            download_file(&url, &target_for_writer, "cooperative writer", None)
        });

        // Give the writer a head-start to acquire the lock.
        std::thread::sleep(Duration::from_millis(20));

        let waiter = std::thread::spawn(move || {
            download_file(&url_clone, &target_clone, "cooperative waiter", None)
        });

        let writer_res = writer.join().unwrap();
        let waiter_res = waiter.join().unwrap();
        server_thread.join().ok();

        assert!(writer_res.is_ok(), "writer must succeed: {:?}", writer_res);
        assert!(waiter_res.is_ok(), "waiter must succeed: {:?}", waiter_res);

        let observed = std::fs::read(&target).unwrap();
        assert_eq!(observed, body);

        // No leftover tmp or lock files.
        let leftovers: Vec<_> = std::fs::read_dir(&tmp_dir)
            .unwrap()
            .filter_map(|e| e.ok())
            .map(|e| e.file_name().to_string_lossy().into_owned())
            .filter(|n| n.contains("partial") || n.ends_with(".lock"))
            .collect();
        assert!(
            leftovers.is_empty(),
            "no partial or lock files should remain after coop download: {:?}",
            leftovers
        );

        std::fs::remove_dir_all(&tmp_dir).ok();
    }
}