modelexpress-server 0.7.0

High-performance gRPC server for model serving and management
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
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

use crate::metrics::grpc::RpcOutcome;
use crate::metrics::registry::{DownloadMetrics, RegistryMetrics, StatusLabel};
use crate::registry::backend::ClaimOutcome;
use crate::registry::entry_key::EntryKey;
use crate::registry::state::RegistryManager;
use modelexpress_common::{
    cache::{CacheConfig, resolve_model_path},
    constants, download,
    grpc::{
        api::{ApiRequest, ApiResponse, api_service_server::ApiService},
        health::{HealthRequest, HealthResponse, health_service_server::HealthService},
        model::{
            DeleteModelRequest, DeleteModelResponse, FileChunk, ModelDownloadRequest,
            ModelFileInfo, ModelFileList, ModelFileSelector, ModelFilesRequest,
            ModelProvider as GrpcModelProvider, ModelStatusUpdate,
            model_service_server::ModelService,
        },
    },
    models::{ModelProvider, ModelStatus},
    providers::is_weight_file,
};
use std::{
    collections::HashMap,
    path::{Path, PathBuf},
    sync::{Arc, Mutex},
    time::SystemTime,
};
use tokio::io::AsyncReadExt;
use tokio_stream::wrappers::ReceiverStream;
use tonic::{Request, Response, Status};
use tracing::{debug, error, info, warn};

static START_TIME: std::sync::OnceLock<SystemTime> = std::sync::OnceLock::new();

/// Ceiling on resolving a revision with the provider. This is a metadata lookup, not a
/// download, so it should complete quickly or not at all.
const REVISION_RESOLVE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(30);

/// Get the configured cache directory for model downloads
fn get_server_cache_dir() -> Option<std::path::PathBuf> {
    // Try to get cache configuration
    if let Ok(config) = CacheConfig::discover() {
        Some(config.local_path)
    } else {
        // Fall back to environment variable
        modelexpress_common::envs::hf_hub_cache()
    }
}

/// Returns true if the model's files are present in the given cache directory. Used to
/// guard against stale `DOWNLOADED` registry records that point at a cache entry which no
/// longer exists on disk (e.g. left behind by a client-side `model clear`). When no cache
/// directory is configured we cannot verify, so we assume the files are present to preserve
/// existing behavior rather than loop re-downloading forever.
async fn model_files_present(
    cache_dir: Option<std::path::PathBuf>,
    model_name: &str,
    provider: ModelProvider,
    revision: Option<&str>,
) -> bool {
    let Some(cache_dir) = cache_dir else {
        return true;
    };
    download::get_provider(provider)
        .get_model_path_revision(model_name, cache_dir, revision)
        .await
        .is_ok()
}

/// A model download request reduced to the identity the registry and the provider need.
///
/// `revision` is always the *resolved*, immutable revision (a commit SHA for Hugging
/// Face), never the branch or tag the caller typed, so cache and lease identity cannot
/// drift when a tag moves.
#[derive(Debug, Clone)]
pub struct DownloadTarget {
    pub model_name: String,
    pub provider: ModelProvider,
    pub revision: Option<String>,
    pub ignore_weights: bool,
}

impl DownloadTarget {
    /// The single string every registry backend keys this download on.
    fn entry_key(&self) -> String {
        EntryKey::new(
            self.model_name.clone(),
            self.revision.clone(),
            self.ignore_weights,
        )
        .to_string()
    }
}

/// Resolve a requested revision to the immutable revision the download will use.
///
/// A revision the caller pinned explicitly must resolve or the request fails: silently
/// serving the default revision instead would hand back a different model than asked
/// for. For an unpinned request we fall back to whatever snapshot is already cached,
/// so a Hub outage still serves models the server has downloaded before.
#[allow(clippy::result_large_err)] // tonic::Status grew past Clippy's Rust 1.98 threshold.
async fn resolve_target_revision(
    model_name: &str,
    provider: ModelProvider,
    cache_dir: Option<PathBuf>,
    requested: Option<&str>,
) -> Result<Option<String>, Status> {
    let provider_impl = download::get_provider(provider);

    // Pinning a revision on a provider that has none is a malformed request, not a
    // revision that happens to be missing.
    if requested.is_some() && !provider_impl.supports_revisions() {
        return Err(Status::invalid_argument(format!(
            "Provider '{}' does not support pinned revisions",
            provider_impl.provider_name()
        )));
    }

    // Resolution runs before the download lease is claimed, so an unresponsive provider
    // would otherwise hold the RPC open indefinitely without any download in progress.
    let resolve = provider_impl.resolve_revision(model_name, cache_dir.clone(), requested);
    let error = match tokio::time::timeout(REVISION_RESOLVE_TIMEOUT, resolve).await {
        Ok(Ok(resolved)) => return Ok(resolved),
        Ok(Err(e)) => e,
        Err(_) => anyhow::anyhow!("timed out after {}s", REVISION_RESOLVE_TIMEOUT.as_secs()),
    };

    if requested.is_some() {
        return Err(Status::not_found(format!(
            "Failed to resolve revision for model '{model_name}': {error:#}"
        )));
    }

    warn!(
        "Failed to resolve the default revision for '{model_name}': {error:#}; \
         falling back to the cached snapshot"
    );

    match provider_impl
        .get_model_path_revision(model_name, cache_dir.unwrap_or_default(), None)
        .await
        .ok()
        .and_then(|path| {
            path.file_name()
                .and_then(|name| name.to_str())
                .map(String::from)
        }) {
        Some(cached) => Ok(Some(cached)),
        None => Err(Status::not_found(format!(
            "Failed to resolve revision for model '{model_name}': {error:#}"
        ))),
    }
}

/// Health service implementation
#[derive(Debug, Default)]
pub struct HealthServiceImpl;

#[tonic::async_trait]
impl HealthService for HealthServiceImpl {
    async fn get_health(
        &self,
        _request: Request<HealthRequest>,
    ) -> Result<Response<HealthResponse>, Status> {
        let start_time = START_TIME.get_or_init(SystemTime::now);
        let uptime = SystemTime::now()
            .duration_since(*start_time)
            .unwrap_or_default()
            .as_secs();

        let response = HealthResponse {
            version: env!("CARGO_PKG_VERSION").to_string(),
            status: "ok".to_string(),
            uptime,
        };

        Ok(Response::new(response))
    }
}

/// API service implementation
#[derive(Debug, Default)]
pub struct ApiServiceImpl;

/// Return `body` with the handler's own verdict attached.
///
/// See [`crate::metrics::grpc`]. Mirrors the helper in `p2p/service.rs`; kept
/// local to each module so a handler's outcome is stated next to the handler.
#[allow(clippy::result_large_err)] // Returns the handlers' own tonic::Status result type.
fn tagged<T>(body: T, outcome: RpcOutcome) -> Result<Response<T>, Status> {
    let mut response = Response::new(body);
    response.extensions_mut().insert(outcome);
    Ok(response)
}

#[tonic::async_trait]
impl ApiService for ApiServiceImpl {
    async fn send_request(
        &self,
        request: Request<ApiRequest>,
    ) -> Result<Response<ApiResponse>, Status> {
        let api_request = request.into_inner();
        info!("Received gRPC request: {:?}", api_request);

        // Process the request based on the action
        if api_request.action.as_str() == "ping" {
            info!("Processing ping request");
            let response_data = serde_json::json!({ "message": "pong" });
            let data_bytes = serde_json::to_vec(&response_data)
                .map_err(|e| Status::internal(format!("Serialization error: {e}")))?;

            tagged(
                ApiResponse {
                    success: true,
                    data: Some(data_bytes),
                    error: None,
                },
                RpcOutcome::Ok,
            )
        } else {
            error!("Unknown action: {}", api_request.action);
            // In band, like the P2P handlers: `Ok` on the wire with
            // `success: false` in the body, so the outcome has to be stated
            // rather than inferred from the status code.
            tagged(
                ApiResponse {
                    success: false,
                    data: None,
                    error: Some(format!("Unknown action: {}", api_request.action)),
                },
                RpcOutcome::InvalidArgument,
            )
        }
    }
}

/// Model service implementation
#[derive(Clone)]
pub struct ModelServiceImpl {
    tracker: Arc<ModelDownloadTracker>,
}

impl ModelServiceImpl {
    /// Each server owns its tracker, so multiple servers can run in one process.
    pub fn new(tracker: Arc<ModelDownloadTracker>) -> Self {
        Self { tracker }
    }
}

/// Helper function to collect all files in a model directory recursively
fn collect_model_files(
    base_path: &Path,
    current_path: &Path,
    file_selector: Option<&ModelFileSelector>,
    ignore_weights: bool,
) -> Vec<(PathBuf, u64)> {
    let mut files = Vec::new();

    if let Ok(entries) = std::fs::read_dir(current_path) {
        for entry in entries.flatten() {
            let path = entry.path();
            if path.is_file() {
                if let Ok(metadata) = std::fs::metadata(&path) {
                    // Get relative path from base_path
                    if let Ok(relative) = path.strip_prefix(base_path) {
                        // Validate that the relative path does not contain any '..' components or is absolute
                        let mut is_safe = true;
                        for comp in relative.components() {
                            use std::path::Component;
                            match comp {
                                Component::ParentDir
                                | Component::RootDir
                                | Component::Prefix(_) => {
                                    is_safe = false;
                                    break;
                                }
                                _ => {}
                            }
                        }
                        if !is_safe {
                            tracing::warn!(
                                "Skipping potentially unsafe file path: {:?} (relative: {:?})",
                                path,
                                relative
                            );
                        } else if ignore_weights
                            && is_weight_file(relative.to_string_lossy().as_ref())
                        {
                            // Weight file skipped because ignore_weights is set.
                        } else if file_selector.is_none_or(|selector| {
                            selector
                                .paths
                                .iter()
                                .any(|selector_path| Path::new(selector_path) == relative)
                        }) {
                            files.push((relative.to_path_buf(), metadata.len()));
                        }
                    }
                }
            } else if path.is_dir() {
                files.extend(collect_model_files(
                    base_path,
                    &path,
                    file_selector,
                    ignore_weights,
                ));
            }
        }
    }

    files
}

fn ensure_selected_files_exist(
    files: &[(PathBuf, u64)],
    file_selector: Option<&ModelFileSelector>,
) -> Result<(), String> {
    let Some(selector) = file_selector else {
        return Ok(());
    };

    if let Some(missing_path) = selector.paths.iter().find(|selector_path| {
        !files
            .iter()
            .any(|(path, _)| Path::new(selector_path) == path.as_path())
    }) {
        Err(format!(
            "Selected file not found in model directory: {missing_path}"
        ))
    } else {
        Ok(())
    }
}

#[tonic::async_trait]
impl ModelService for ModelServiceImpl {
    type EnsureModelDownloadedStream = ReceiverStream<Result<ModelStatusUpdate, Status>>;
    type StreamModelFilesStream = ReceiverStream<Result<FileChunk, Status>>;

    async fn ensure_model_downloaded(
        &self,
        request: Request<ModelDownloadRequest>,
    ) -> Result<Response<Self::EnsureModelDownloadedStream>, Status> {
        info!("Starting model download stream");
        let model_request = request.into_inner();
        let (tx, rx) = tokio::sync::mpsc::channel(4);

        // Convert gRPC provider to our enum
        let grpc_provider = GrpcModelProvider::try_from(model_request.provider).map_err(|_| {
            Status::invalid_argument(format!(
                "Invalid provider value: {}",
                model_request.provider
            ))
        })?;
        let provider = ModelProvider::from(grpc_provider);
        let model_name = download::canonical_model_name(&model_request.model_name, provider)
            .map_err(|e| Status::invalid_argument(e.to_string()))?;
        let ignore_weights = model_request.ignore_weights;

        // Resolve before claiming: the registry keys the download lease on the resolved
        // revision, so two revisions of one model never coalesce onto a single claim.
        let revision = resolve_target_revision(
            &model_name,
            provider,
            get_server_cache_dir(),
            model_request.revision.as_deref(),
        )
        .await?;

        let target = DownloadTarget {
            model_name: model_name.clone(),
            provider,
            revision: revision.clone(),
            ignore_weights,
        };

        // Spawn a task to handle the streaming download updates
        let tracker = self.tracker.clone();
        tokio::spawn(async move {
            // Run the full claim + wait + retry flow. `ensure_model_downloaded` sends
            // its own initial status update (based on the `ClaimOutcome` returned by the
            // registry), so we don't do a pre-check here — a pre-check would either
            // duplicate that update or, worse, emit `status=ERROR` on a model we're
            // about to retry and trip the client-lib's terminal-error bailout before
            // the retry completion broadcast arrives.
            let final_status = tracker.ensure_model_downloaded(&target, &tx).await;

            // Send final status update
            let final_update = ModelStatusUpdate {
                model_name: model_name.clone(),
                status: modelexpress_common::grpc::model::ModelStatus::from(final_status) as i32,
                message: match final_status {
                    ModelStatus::DOWNLOADED => {
                        Some("Model download completed successfully".to_string())
                    }
                    ModelStatus::ERROR => Some("Model download failed".to_string()),
                    ModelStatus::DOWNLOADING => Some("Download still in progress".to_string()),
                },
                provider: grpc_provider as i32,
                resolved_revision: revision,
            };

            let _ = tx.send(Ok(final_update)).await;
        });

        Ok(Response::new(ReceiverStream::new(rx)))
    }

    async fn stream_model_files(
        &self,
        request: Request<ModelFilesRequest>,
    ) -> Result<Response<Self::StreamModelFilesStream>, Status> {
        let files_request = request.into_inner();
        let chunk_size = if files_request.chunk_size == 0 {
            constants::DEFAULT_TRANSFER_CHUNK_SIZE
        } else {
            files_request.chunk_size as usize
        };

        // Convert gRPC provider to our enum
        let grpc_provider = GrpcModelProvider::try_from(files_request.provider).map_err(|_| {
            Status::invalid_argument(format!(
                "Invalid provider value: {}",
                files_request.provider
            ))
        })?;
        let provider = ModelProvider::from(grpc_provider);
        let model_name = download::canonical_model_name(&files_request.model_name, provider)
            .map_err(|e| Status::invalid_argument(e.to_string()))?;
        let provider_impl = download::get_provider(provider);

        info!(
            "Starting file stream for model: {} with chunk size: {} bytes",
            model_name, chunk_size
        );

        // Get the cache directory
        let cache_dir = get_server_cache_dir()
            .ok_or_else(|| Status::internal("Server cache directory not configured"))?;

        // Get the model path using the provider from the request. A requested revision
        // selects that exact snapshot instead of whichever one is newest on disk.
        let model_path = provider_impl
            .get_model_path_revision(
                &model_name,
                cache_dir.clone(),
                files_request.revision.as_deref(),
            )
            .await
            .map_err(|e| Status::not_found(format!("Model not found: {e}")))?;

        debug!("Model path resolved to: {:?}", model_path);

        let commit_hash = if provider == ModelProvider::HuggingFace {
            model_path
                .file_name()
                .and_then(|name| name.to_str())
                .map(String::from)
        } else {
            None
        };

        if provider == ModelProvider::HuggingFace && commit_hash.is_none() {
            return Err(Status::internal(
                "Resolved Hugging Face model path did not contain a revision",
            ));
        }

        let expected_model_path =
            resolve_model_path(&cache_dir, provider, &model_name, commit_hash.as_deref()).map_err(
                |e| Status::internal(format!("Failed to resolve expected cache layout: {e}")),
            )?;

        if model_path != expected_model_path {
            error!(
                "Resolved model path '{}' does not match expected cache layout '{}' for model '{}'",
                model_path.display(),
                expected_model_path.display(),
                model_name
            );
            return Err(Status::internal(
                "Resolved model path does not match expected cache layout",
            ));
        }

        // Collect all files to stream
        let files = collect_model_files(
            &model_path,
            &model_path,
            files_request.file_selector.as_ref(),
            files_request.ignore_weights,
        );
        ensure_selected_files_exist(&files, files_request.file_selector.as_ref())
            .map_err(Status::not_found)?;

        if files.is_empty() {
            return Err(Status::not_found("No files found in model directory"));
        }

        let total_files = files.len();
        info!(
            "Found {} files to stream for model {}",
            total_files, model_name
        );

        let (tx, rx) = tokio::sync::mpsc::channel(16);

        // Spawn a task to stream files
        tokio::spawn(async move {
            // Allocate buffer once and reuse across all files
            let mut buffer = vec![0u8; chunk_size];
            let mut is_first_chunk = true;

            for (file_idx, (relative_path, total_size)) in files.iter().enumerate() {
                let file_path = model_path.join(relative_path);
                let is_last_file = file_idx == total_files.saturating_sub(1);

                debug!("Streaming file: {:?} ({} bytes)", relative_path, total_size);

                // Open the file
                let file = match tokio::fs::File::open(&file_path).await {
                    Ok(f) => f,
                    Err(e) => {
                        error!("Failed to open file {:?}: {}", file_path, e);
                        let _ = tx
                            .send(Err(Status::internal(format!("Failed to open file: {e}"))))
                            .await;
                        return;
                    }
                };

                let mut reader = tokio::io::BufReader::new(file);
                let mut offset: u64 = 0;

                if *total_size == 0 {
                    let first_chunk = std::mem::replace(&mut is_first_chunk, false);
                    let chunk = FileChunk {
                        relative_path: relative_path.to_string_lossy().to_string(),
                        data: Vec::new(),
                        offset: 0,
                        total_size: 0,
                        is_last_chunk: true,
                        is_last_file,
                        commit_hash: if first_chunk {
                            commit_hash.clone()
                        } else {
                            None
                        },
                    };

                    if tx.send(Ok(chunk)).await.is_err() {
                        debug!("Client disconnected during file stream");
                        return;
                    }

                    continue;
                }

                loop {
                    let bytes_read = match reader.read(&mut buffer).await {
                        Ok(0) => break, // EOF
                        Ok(n) => n,
                        Err(e) => {
                            error!("Failed to read file {:?}: {}", file_path, e);
                            let _ = tx
                                .send(Err(Status::internal(format!("Failed to read file: {e}"))))
                                .await;
                            return;
                        }
                    };

                    let is_last_chunk = offset.saturating_add(bytes_read as u64) >= *total_size;

                    let first_chunk = std::mem::replace(&mut is_first_chunk, false);

                    let chunk = FileChunk {
                        relative_path: relative_path.to_string_lossy().to_string(),
                        data: buffer[..bytes_read].to_vec(),
                        offset,
                        total_size: *total_size,
                        is_last_chunk,
                        is_last_file: is_last_file && is_last_chunk,
                        commit_hash: if first_chunk {
                            commit_hash.clone()
                        } else {
                            None
                        },
                    };

                    if tx.send(Ok(chunk)).await.is_err() {
                        debug!("Client disconnected during file stream");
                        return;
                    }

                    offset = offset.saturating_add(bytes_read as u64);
                }
            }

            info!("File streaming completed for model");
        });

        Ok(Response::new(ReceiverStream::new(rx)))
    }

    async fn list_model_files(
        &self,
        request: Request<ModelFilesRequest>,
    ) -> Result<Response<ModelFileList>, Status> {
        let files_request = request.into_inner();

        // Convert gRPC provider to our enum
        let grpc_provider = GrpcModelProvider::try_from(files_request.provider).map_err(|_| {
            Status::invalid_argument(format!(
                "Invalid provider value: {}",
                files_request.provider
            ))
        })?;
        let provider = ModelProvider::from(grpc_provider);
        let model_name = download::canonical_model_name(&files_request.model_name, provider)
            .map_err(|e| Status::invalid_argument(e.to_string()))?;
        let provider_impl = download::get_provider(provider);

        info!("Listing files for model: {}", model_name);

        // Get the cache directory
        let cache_dir = get_server_cache_dir()
            .ok_or_else(|| Status::internal("Server cache directory not configured"))?;

        // Get the model path using the provider from the request
        let model_path = provider_impl
            .get_model_path_revision(&model_name, cache_dir, files_request.revision.as_deref())
            .await
            .map_err(|e| Status::not_found(format!("Model not found: {e}")))?;

        // Collect all files
        let files = collect_model_files(
            &model_path,
            &model_path,
            files_request.file_selector.as_ref(),
            files_request.ignore_weights,
        );
        ensure_selected_files_exist(&files, files_request.file_selector.as_ref())
            .map_err(Status::not_found)?;

        let file_infos: Vec<ModelFileInfo> = files
            .iter()
            .map(|(path, size)| ModelFileInfo {
                relative_path: path.to_string_lossy().to_string(),
                size: *size,
            })
            .collect();

        let total_size: u64 = files.iter().map(|(_, size)| size).sum();

        Ok(Response::new(ModelFileList {
            model_name,
            files: file_infos,
            total_size,
        }))
    }

    async fn delete_model(
        &self,
        request: Request<DeleteModelRequest>,
    ) -> Result<Response<DeleteModelResponse>, Status> {
        let delete_request = request.into_inner();

        let grpc_provider = GrpcModelProvider::try_from(delete_request.provider).map_err(|_| {
            Status::invalid_argument(format!(
                "Invalid provider value: {}",
                delete_request.provider
            ))
        })?;
        let provider = ModelProvider::from(grpc_provider);
        let model_name = download::canonical_model_name(&delete_request.model_name, provider)
            .map_err(|e| Status::invalid_argument(e.to_string()))?;

        // A model can hold several entries at once (one per revision, and separate ones
        // for metadata-only downloads). `model clear` means "forget this model", so drop
        // all of them rather than only the unpinned full-weight entry.
        let tracker = self.tracker.clone();
        let outcome = tracker.delete_model_entries(&model_name).await;
        let removed = outcome.removed;
        info!("Deleted {removed} registry record(s) for model '{model_name}'");

        // The response stays `success: true` -- the fallback delete was still
        // attempted and callers depend on that shape. But a registry outage and a
        // model with no entries both return zero records here, so without the tag
        // the RPC would be recorded as a plain success during an outage. That is
        // the exact failure this layer exists to prevent.
        tagged(
            DeleteModelResponse {
                success: true,
                message: Some(format!(
                    "Model '{model_name}' removed from registry ({removed} record(s))"
                )),
            },
            if outcome.degraded {
                RpcOutcome::BackendError
            } else {
                RpcOutcome::Ok
            },
        )
    }
}

/// What clearing a model's registry entries actually achieved.
///
/// `removed` alone cannot distinguish "this model had no entries" from "the
/// registry was unreachable and only the fallback key was tried" -- both are
/// zero.
pub struct DeleteEntriesOutcome {
    /// How many records were removed.
    pub removed: usize,
    /// The registry listing failed, so `removed` is a floor rather than a count.
    pub degraded: bool,
}

/// Type alias for the complex waiting channels type
type WaitingChannels =
    Arc<Mutex<HashMap<String, Vec<tokio::sync::mpsc::Sender<Result<ModelStatusUpdate, Status>>>>>>;

/// What a status update is routed by and what it reports.
///
/// Waiters are registered under the registry entry key, which carries the revision and
/// weight mode, while the update itself reports the plain model name the caller asked
/// for plus the revision it resolved to.
struct UpdateIdentity<'a> {
    entry_key: &'a str,
    model_name: &'a str,
    provider: ModelProvider,
    resolved_revision: Option<&'a str>,
}

impl UpdateIdentity<'_> {
    fn status_update(&self, status: ModelStatus, message: Option<String>) -> ModelStatusUpdate {
        ModelStatusUpdate {
            model_name: self.model_name.to_string(),
            status: modelexpress_common::grpc::model::ModelStatus::from(status) as i32,
            message,
            provider: GrpcModelProvider::from(self.provider) as i32,
            resolved_revision: self.resolved_revision.map(String::from),
        }
    }
}

/// Tracks the status of model downloads through the distributed registry backend.
#[derive(Clone)]
pub struct ModelDownloadTracker {
    /// Distributed registry (Redis today, K8s CRDs in a follow-up).
    registry: Arc<RegistryManager>,
    /// Maps model names to list of channels waiting for updates on this server replica.
    waiting_channels: WaitingChannels,
    download_metrics: DownloadMetrics,
    registry_metrics: RegistryMetrics,
}

const DOWNLOAD_LEASE_DURATION: std::time::Duration = std::time::Duration::from_secs(30);
const DOWNLOAD_HEARTBEAT_INTERVAL: tokio::time::Duration = tokio::time::Duration::from_secs(10);

impl ModelDownloadTracker {
    pub fn new(
        registry: Arc<RegistryManager>,
        download_metrics: DownloadMetrics,
        registry_metrics: RegistryMetrics,
    ) -> Self {
        Self {
            registry,
            waiting_channels: Arc::new(Mutex::new(HashMap::new())),
            download_metrics,
            registry_metrics,
        }
    }

    /// Number of models with callers currently waiting on a download.
    ///
    /// This map is never evicted wholesale, so its size is the early warning for
    /// the leak that would eventually OOM the server. A poisoned lock reports the
    /// recovered length rather than failing: a metric read must not be able to
    /// take down the download path.
    pub fn waiting_count(&self) -> usize {
        match self.waiting_channels.lock() {
            Ok(waiting) => waiting.len(),
            Err(poisoned) => poisoned.into_inner().len(),
        }
    }

    async fn touch_and_log(&self, model_name: &str) {
        if let Err(e) = self.registry.touch_model(model_name).await {
            error!("Failed to touch model {model_name}: {e}");
        }
    }

    /// Gets the status of a model from the registry, bumping `last_used_at` on hit.
    /// Returns None on lookup failure (error logged) or unknown model.
    pub async fn get_status(&self, model_name: &str) -> Option<ModelStatus> {
        match self.registry.get_status(model_name).await {
            Ok(Some(status)) => {
                self.touch_and_log(model_name).await;
                Some(status)
            }
            Ok(None) => None,
            Err(e) => {
                error!("Failed to get model status from registry: {e}");
                None
            }
        }
    }

    /// Sets the status of a model and notifies all waiting channels on this replica.
    pub async fn set_status_and_notify(
        &self,
        model_name: String,
        status: ModelStatus,
        provider: ModelProvider,
        message: Option<String>,
    ) {
        if let Err(e) = self
            .registry
            .set_status(&model_name, provider, status, message.clone())
            .await
        {
            error!("Failed to update model status in registry: {e}");
            return;
        }
        self.notify_waiters(
            UpdateIdentity {
                entry_key: &model_name,
                model_name: &model_name,
                provider,
                resolved_revision: None,
            },
            status,
            message,
        );
    }

    fn notify_waiters(
        &self,
        identity: UpdateIdentity<'_>,
        status: ModelStatus,
        message: Option<String>,
    ) {
        let mut waiting = match self.waiting_channels.lock() {
            Ok(guard) => guard,
            Err(poisoned) => {
                error!("Waiting channels mutex is poisoned, recovering");
                poisoned.into_inner()
            }
        };
        if let Some(channels) = waiting.get(identity.entry_key) {
            let update = identity.status_update(status, message);
            for channel in channels {
                let _ = channel.try_send(Ok(update.clone()));
            }
            if status == ModelStatus::DOWNLOADED || status == ModelStatus::ERROR {
                waiting.remove(identity.entry_key);
            }
        }
    }

    /// Adds a channel that wants updates on a specific model (server-replica-local).
    pub fn add_waiting_channel(
        &self,
        model_name: &str,
        tx: tokio::sync::mpsc::Sender<Result<ModelStatusUpdate, Status>>,
    ) {
        let mut waiting = match self.waiting_channels.lock() {
            Ok(guard) => guard,
            Err(poisoned) => {
                error!("Waiting channels mutex is poisoned, recovering");
                poisoned.into_inner()
            }
        };
        waiting.entry(model_name.to_string()).or_default().push(tx);
    }

    /// Deletes a model record from the registry and clears local waiters.
    /// Returns whether the registry record was actually removed.
    ///
    /// The error is still swallowed -- callers proceed to notify waiters either
    /// way -- but the outcome is reported, because a caller that records the
    /// removal as a lifecycle event must not do so when nothing was removed.
    pub async fn delete_status(&self, model_name: &str) -> bool {
        let deleted = match self.registry.delete_model(model_name).await {
            Ok(()) => true,
            Err(e) => {
                error!("Failed to delete model from registry: {e}");
                false
            }
        };
        let mut waiting = match self.waiting_channels.lock() {
            Ok(guard) => guard,
            Err(poisoned) => {
                error!("Waiting channels mutex is poisoned, recovering");
                poisoned.into_inner()
            }
        };
        waiting.remove(model_name);
        deleted
    }

    /// Deletes every registry entry belonging to `model_name`, whatever revision or
    /// weight mode each entry covers.
    ///
    /// The registry-listing failure is swallowed on purpose: the fallback still
    /// clears the common case, which is better than refusing outright. It is
    /// reported in the return value so the caller can say so, because otherwise a
    /// backend outage and a model with no entries are the same `0`.
    pub async fn delete_model_entries(&self, model_name: &str) -> DeleteEntriesOutcome {
        let records = match self.registry.get_models_by_last_used(None).await {
            Ok(records) => records,
            Err(e) => {
                error!("Failed to list registry records for '{model_name}': {e}");
                // Fall back to the unpinned full-weight key so the common case still
                // clears rather than failing outright.
                self.delete_status(model_name).await;
                return DeleteEntriesOutcome {
                    removed: 0,
                    degraded: true,
                };
            }
        };

        let mut removed: usize = 0;
        for record in records {
            if EntryKey::parse(&record.model_name).belongs_to(model_name) {
                // Deleting a record mid-download is a real exit from DOWNLOADING,
                // and the only one the claim lifecycle cannot see: once the key
                // is gone, finish_download_claim finds nothing to fence and
                // returns false, so it records no departure. Without this the
                // arrival booked by the claim is never matched and the flow
                // accounting stays permanently ahead. The status is already in
                // hand here, so this costs no extra read.
                // Only once the record is really gone. delete_model can fail and
                // is swallowed, and recording first would book a departure for an
                // entry still sitting in DOWNLOADING -- turning a backend outage
                // into a permanently understated flow count.
                let deleted = self.delete_status(&record.model_name).await;
                if deleted && record.status == ModelStatus::DOWNLOADING {
                    self.registry_metrics
                        .record_transition(StatusLabel::Downloading, StatusLabel::Absent);
                }
                removed = removed.saturating_add(1);
            }
        }
        DeleteEntriesOutcome {
            removed,
            degraded: false,
        }
    }

    /// Spawn a background task that actually downloads the model, updating the tracker on
    /// success or failure. Extracted here so the claim and retry paths share the code.
    fn spawn_download_task(&self, target: DownloadTarget, retry: bool, claim_id: String) {
        let tracker = self.clone();
        tokio::spawn(async move {
            let started = std::time::Instant::now();
            let entry_key = target.entry_key();
            let DownloadTarget {
                model_name,
                provider,
                revision,
                ignore_weights,
            } = target;
            let cache_dir = get_server_cache_dir();
            let download = download::download_model_revision(
                &model_name,
                provider,
                cache_dir,
                ignore_weights,
                revision.as_deref(),
            );
            tokio::pin!(download);
            let start = tokio::time::Instant::now()
                .checked_add(DOWNLOAD_HEARTBEAT_INTERVAL)
                .unwrap_or_else(tokio::time::Instant::now);
            let mut heartbeat = tokio::time::interval_at(start, DOWNLOAD_HEARTBEAT_INTERVAL);
            let result = loop {
                tokio::select! {
                    result = &mut download => break result,
                    _ = heartbeat.tick() => {
                        match tracker
                            .registry
                            .refresh_download_claim(
                                &entry_key,
                                provider,
                                &claim_id,
                                DOWNLOAD_LEASE_DURATION,
                            )
                            .await
                        {
                            Ok(true) => {}
                            Ok(false) => {
                                warn!(
                                    "Stopping download for {model_name}: lease ownership was lost"
                                );
                                return;
                            }
                            Err(e) => {
                                warn!("Failed to refresh download lease for {model_name}: {e}");
                            }
                        }
                    }
                }
            };

            let (status, message) = match result {
                Ok(_path) => (
                    ModelStatus::DOWNLOADED,
                    Some("Model download completed successfully".to_string()),
                ),
                Err(e) => {
                    if retry {
                        error!("Failed to download model {model_name} on retry: {e}");
                    } else {
                        error!("Failed to download model {model_name}: {e}");
                    }
                    let msg = if retry {
                        format!("Download failed on retry: {e}")
                    } else {
                        format!("Download failed: {e}")
                    };
                    (ModelStatus::ERROR, Some(msg))
                }
            };

            // Timed from the top of the task, so this is the whole download
            // rather than the registry write below it. Observed before the fence
            // check: the bytes were fetched regardless of whether this replica
            // still owns the right to publish the result.
            tracker
                .download_metrics
                .observe(StatusLabel::from(status), started.elapsed().as_secs_f64());

            match tracker
                .registry
                .finish_download_claim(&entry_key, provider, &claim_id, status, message.clone())
                .await
            {
                Ok(true) => {
                    tracker.notify_waiters(
                        UpdateIdentity {
                            entry_key: &entry_key,
                            model_name: &model_name,
                            provider,
                            resolved_revision: revision.as_deref(),
                        },
                        status,
                        message,
                    );
                }
                Ok(false) => {
                    warn!("Ignoring completion for {model_name}: lease ownership was lost");
                }
                Err(e) => {
                    error!("Failed to finish download claim for {model_name}: {e}");
                }
            }
        });
    }

    /// Initiates a download for a model and streams status updates.
    ///
    /// All registry operations key on the target's entry key rather than its model name,
    /// so a second revision (or a full-weight request behind a metadata-only one) claims
    /// its own lease instead of coalescing onto an unrelated download.
    pub async fn ensure_model_downloaded(
        &self,
        target: &DownloadTarget,
        tx: &tokio::sync::mpsc::Sender<Result<ModelStatusUpdate, Status>>,
    ) -> ModelStatus {
        let entry_key = target.entry_key();
        let entry_key = entry_key.as_str();
        let model_name = target.model_name.as_str();
        let provider = target.provider;
        let identity = UpdateIdentity {
            entry_key,
            model_name,
            provider,
            resolved_revision: target.revision.as_deref(),
        };

        // Atomically try to claim this model for download. The `ClaimOutcome` tells us
        // whether THIS replica won the claim or is observing someone else's claim —
        // status alone (`DOWNLOADING`) can't distinguish those cases across replicas.
        // A claim may report an existing `DOWNLOADED` record whose files no longer exist
        // on disk (e.g. after a client-side `model clear` that only removed local files).
        // When that happens we drop the stale record and re-claim once, so the download
        // path runs instead of returning a false success. Bounded to two attempts to
        // avoid looping if the delete or a concurrent re-claim keeps the record around.
        const MAX_CLAIM_ATTEMPTS: usize = 2;
        let mut claim_id = uuid::Uuid::new_v4().to_string();
        let mut attempt: usize = 0;
        let (status, is_owner) = loop {
            attempt = attempt.saturating_add(1);
            match self
                .registry
                .try_claim_for_download(entry_key, provider, &claim_id, DOWNLOAD_LEASE_DURATION)
                .await
            {
                // Both outcomes mean this replica owns the download; they differ
                // only in cost, which the metrics layer records separately.
                Ok(ClaimOutcome::Claimed | ClaimOutcome::TookOver) => {
                    break (ModelStatus::DOWNLOADING, true);
                }
                Ok(ClaimOutcome::AlreadyExists(existing)) => {
                    if existing == ModelStatus::DOWNLOADED
                        && attempt < MAX_CLAIM_ATTEMPTS
                        && !model_files_present(
                            get_server_cache_dir(),
                            model_name,
                            provider,
                            target.revision.as_deref(),
                        )
                        .await
                    {
                        error!(
                            "Registry reports model '{model_name}' as DOWNLOADED but its files \
                             are missing from the cache; clearing the stale record and \
                             re-downloading"
                        );
                        self.delete_status(entry_key).await;
                        continue;
                    }
                    if existing == ModelStatus::DOWNLOADED {
                        // Returning an existing downloaded model is a cache hit for LRU purposes.
                        self.touch_and_log(entry_key).await;
                    }
                    break (existing, false);
                }
                Err(e) => {
                    error!("Failed to claim model for download: {e}");
                    let error_update = identity.status_update(
                        ModelStatus::ERROR,
                        Some("Registry error occurred".to_string()),
                    );
                    let _ = tx.send(Ok(error_update)).await;
                    return ModelStatus::ERROR;
                }
            }
        };

        // If we observed a previous ERROR, attempt the ERROR -> DOWNLOADING CAS up front.
        // Only the CAS winner spawns the retry download; observers fall through to the
        // wait loop. Doing this *before* the initial stream update keeps the reported
        // status honest: after this block, the record is DOWNLOADING (the record may
        // briefly have been ERROR, but the client should wait, not bail).
        let (effective_status, is_retry_owner) = if status == ModelStatus::ERROR {
            let won = match self
                .registry
                .try_reset_error_for_retry(entry_key, provider, &claim_id, DOWNLOAD_LEASE_DURATION)
                .await
            {
                Ok(won) => won,
                Err(e) => {
                    error!("Failed to CAS status for retry: {e}");
                    let _ = tx
                        .send(Ok(identity.status_update(
                            ModelStatus::ERROR,
                            Some("Registry error occurred during retry".to_string()),
                        )))
                        .await;
                    return ModelStatus::ERROR;
                }
            };
            (ModelStatus::DOWNLOADING, won)
        } else {
            (status, false)
        };

        let update = identity.status_update(
            effective_status,
            match (status, effective_status) {
                (_, ModelStatus::DOWNLOADED) => Some("Model already downloaded".to_string()),
                (ModelStatus::ERROR, _) => Some("Previous download failed, retrying".to_string()),
                (_, ModelStatus::DOWNLOADING) => Some("Model download in progress".to_string()),
                // effective can never be ERROR: ERROR observations are CAS'd above.
                (_, ModelStatus::ERROR) => Some("Download error".to_string()),
            },
        );
        let _ = tx.send(Ok(update)).await;

        if effective_status == ModelStatus::DOWNLOADING {
            // Every caller is a waiter — whether we own the download or not, we still
            // need a channel so the completion broadcast reaches this stream.
            self.add_waiting_channel(entry_key, tx.clone());

            // Spawn the download only on the replica that won the claim (fresh
            // download) or won the ERROR-retry CAS. Everyone else waits.
            if is_owner || is_retry_owner {
                let retry = status == ModelStatus::ERROR;
                self.spawn_download_task(target.clone(), retry, claim_id);
                claim_id = uuid::Uuid::new_v4().to_string();
            }

            loop {
                tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
                match self
                    .registry
                    .try_claim_for_download(entry_key, provider, &claim_id, DOWNLOAD_LEASE_DURATION)
                    .await
                {
                    // A taken-over lease must be re-driven exactly like a fresh
                    // claim. Letting it fall through instead would leave the entry
                    // wedged in DOWNLOADING with no owner running.
                    Ok(ClaimOutcome::Claimed | ClaimOutcome::TookOver) => {
                        self.spawn_download_task(target.clone(), false, claim_id);
                        claim_id = uuid::Uuid::new_v4().to_string();
                    }
                    Ok(ClaimOutcome::AlreadyExists(current_status))
                        if current_status != ModelStatus::DOWNLOADING =>
                    {
                        return current_status;
                    }
                    Ok(ClaimOutcome::AlreadyExists(_)) => {}
                    Err(e) => warn!("Failed to poll download lease for {model_name}: {e}"),
                }
            }
        }

        effective_status
    }
}

#[cfg(test)]
#[allow(clippy::expect_used)]
mod tests {
    use super::*;
    use modelexpress_common::grpc::{api::ApiRequest, health::HealthRequest};
    use modelexpress_common::test_support::{EnvVarGuard, acquire_env_mutex};
    use tempfile::TempDir;
    use tokio_stream::StreamExt;
    use tonic::Request;

    #[tokio::test]
    async fn test_health_service() {
        let service = HealthServiceImpl;
        let request = Request::new(HealthRequest {});

        let response = service.get_health(request).await;
        assert!(response.is_ok());

        let health_response = response.expect("Health response should be ok").into_inner();
        assert_eq!(health_response.version, env!("CARGO_PKG_VERSION"));
        assert_eq!(health_response.status, "ok");
        // uptime is u64, always >= 0, so just verify it exists
        let _uptime = health_response.uptime;
    }

    /// The unknown-action path is an in-band failure and must not read as `ok`.
    #[tokio::test]
    async fn unknown_action_is_tagged_invalid_argument() {
        let service = ApiServiceImpl;
        let response = service
            .send_request(Request::new(ApiRequest {
                id: "test-id".to_string(),
                action: "not-a-real-action".to_string(),
                payload: None,
            }))
            .await
            .expect("the RPC itself succeeds");

        assert_eq!(
            response.extensions().get::<RpcOutcome>(),
            Some(&RpcOutcome::InvalidArgument)
        );
        assert!(!response.into_inner().success);
    }

    #[tokio::test]
    async fn test_api_service_ping() {
        let service = ApiServiceImpl;
        let request = Request::new(ApiRequest {
            id: "test-id".to_string(),
            action: "ping".to_string(),
            payload: None,
        });

        let response = service.send_request(request).await;
        assert!(response.is_ok());

        let api_response = response.expect("API response should be ok").into_inner();
        assert!(api_response.success);
        assert!(api_response.data.is_some());
        assert!(api_response.error.is_none());

        // Check that the response data contains "pong"
        let data_bytes = api_response.data.expect("Data should be present");
        let data: serde_json::Value =
            serde_json::from_slice(&data_bytes).expect("Data should be valid JSON");
        assert_eq!(data["message"], "pong");
    }

    #[tokio::test]
    async fn test_api_service_unknown_action() {
        let service = ApiServiceImpl;
        let request = Request::new(ApiRequest {
            id: "test-id".to_string(),
            action: "unknown-action".to_string(),
            payload: None,
        });

        let response = service.send_request(request).await;
        assert!(response.is_ok());

        let api_response = response.expect("API response should be ok").into_inner();
        assert!(!api_response.success);
        assert!(api_response.data.is_none());
        assert!(api_response.error.is_some());

        let error_message = api_response.error.expect("Error should be present");
        assert!(error_message.contains("Unknown action"));
    }

    // Tracker tests exercise the ModelDownloadTracker's interaction with a mocked
    // RegistryBackend. The full backend semantics (claim atomicity, LRU ordering, etc.) are
    // covered by the per-backend unit tests in modelexpress_server::registry and by the
    // testcontainers-based integration tests.
    fn tracker_with_mock(
        mock: crate::registry::backend::MockRegistryBackend,
    ) -> ModelDownloadTracker {
        let registry = Arc::new(RegistryManager::with_backend(Arc::new(mock)));
        ModelDownloadTracker::new(
            registry,
            DownloadMetrics::register(&mut crate::metrics::new_registry()),
            RegistryMetrics::register(&mut crate::metrics::new_registry()),
        )
    }

    /// As [`tracker_with_mock`], but hands back the Prometheus registry the metric
    /// families were registered into.
    ///
    /// [`tracker_with_mock`] registers into a temporary registry that is dropped on
    /// the spot, so nothing recorded through it can ever be encoded. Any metric
    /// assertion against a tracker built that way would be vacuous by construction.
    fn tracker_with_mock_and_registry(
        mock: crate::registry::backend::MockRegistryBackend,
    ) -> (ModelDownloadTracker, prometheus_client::registry::Registry) {
        let mut metrics_registry = crate::metrics::new_registry();
        let download_metrics = DownloadMetrics::register(&mut metrics_registry);
        let registry_metrics = RegistryMetrics::register(&mut metrics_registry);
        let registry = Arc::new(RegistryManager::with_backend(Arc::new(mock)));
        (
            ModelDownloadTracker::new(registry, download_metrics, registry_metrics),
            metrics_registry,
        )
    }

    fn encoded_metrics(registry: &prometheus_client::registry::Registry) -> String {
        crate::metrics::encode_text(registry).unwrap_or_else(|_| String::from("<encode failed>"))
    }

    /// The `downloading -> absent` series, or 0 when it was never created.
    fn departures_from_downloading(encoded: &str) -> i64 {
        encoded
            .lines()
            .find_map(|line| {
                line.strip_prefix(
                    r#"mx_registry_status_transitions_total{from="downloading",to="absent"} "#,
                )
            })
            .and_then(|value| value.trim().parse().ok())
            .unwrap_or_default()
    }

    fn record_with_status(
        model_name: &str,
        status: ModelStatus,
    ) -> crate::registry::backend::ModelRecord {
        let now = chrono::Utc::now();
        crate::registry::backend::ModelRecord {
            model_name: model_name.to_string(),
            provider: ModelProvider::HuggingFace,
            status,
            created_at: now,
            last_used_at: now,
            message: None,
        }
    }

    /// Unpinned, full-weight target: its entry key is the bare model name.
    fn test_target(model_name: &str) -> DownloadTarget {
        DownloadTarget {
            model_name: model_name.to_string(),
            provider: ModelProvider::HuggingFace,
            revision: None,
            ignore_weights: false,
        }
    }

    #[tokio::test]
    async fn test_tracker_get_status_missing_returns_none() {
        let mut mock = crate::registry::backend::MockRegistryBackend::new();
        mock.expect_get_status().once().returning(|_| Ok(None));
        // touch is NOT called when status is missing
        let tracker = tracker_with_mock(mock);
        assert!(tracker.get_status("unknown").await.is_none());
    }

    #[tokio::test]
    async fn test_tracker_get_status_hit_bumps_last_used_at() {
        let mut mock = crate::registry::backend::MockRegistryBackend::new();
        mock.expect_get_status()
            .once()
            .returning(|_| Ok(Some(ModelStatus::DOWNLOADED)));
        mock.expect_touch_model().once().returning(|_| Ok(()));
        let tracker = tracker_with_mock(mock);
        assert_eq!(tracker.get_status("m").await, Some(ModelStatus::DOWNLOADED));
    }

    #[tokio::test]
    #[allow(clippy::await_holding_lock)]
    async fn test_tracker_downloaded_cache_hit_bumps_last_used_at() {
        let env_lock = acquire_env_mutex();
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let _cache_dir_guard = EnvVarGuard::set(
            &env_lock,
            "MODEL_EXPRESS_CACHE_DIRECTORY",
            temp_dir.path().to_str().expect("Expected temp dir path"),
        );
        let _offline_guard = EnvVarGuard::set(&env_lock, "HF_HUB_OFFLINE", "1");
        let model_dir = temp_dir.path().join("models--test--model/snapshots/abc123");
        std::fs::create_dir_all(&model_dir).expect("Failed to create model dir");
        std::fs::write(model_dir.join("config.json"), b"{}").expect("Failed to write config");

        let mut mock = crate::registry::backend::MockRegistryBackend::new();
        mock.expect_try_claim_for_download()
            .with(
                mockall::predicate::eq("test/model"),
                mockall::predicate::eq(ModelProvider::HuggingFace),
                mockall::predicate::always(),
                mockall::predicate::always(),
            )
            .once()
            .returning(|_, _, _, _| Ok(ClaimOutcome::AlreadyExists(ModelStatus::DOWNLOADED)));
        mock.expect_touch_model()
            .with(mockall::predicate::eq("test/model"))
            .once()
            .returning(|_| Ok(()));
        let tracker = tracker_with_mock(mock);
        let (tx, _rx) = tokio::sync::mpsc::channel(1);

        assert_eq!(
            tracker
                .ensure_model_downloaded(&test_target("test/model"), &tx)
                .await,
            ModelStatus::DOWNLOADED
        );
    }

    /// Claim, touch, and report a cache hit for `target`, asserting the registry was
    /// keyed on the target's entry key rather than on the bare model name.
    async fn assert_claims_on_entry_key(target: DownloadTarget) {
        let expected_key = target.entry_key();
        assert_ne!(
            expected_key, target.model_name,
            "This target must not reduce to the bare model name, or the test proves nothing"
        );
        let mut mock = crate::registry::backend::MockRegistryBackend::new();
        mock.expect_try_claim_for_download()
            .with(
                mockall::predicate::eq(expected_key.clone()),
                mockall::predicate::eq(ModelProvider::HuggingFace),
                mockall::predicate::always(),
                mockall::predicate::always(),
            )
            .once()
            .returning(|_, _, _, _| Ok(ClaimOutcome::AlreadyExists(ModelStatus::DOWNLOADED)));
        mock.expect_touch_model()
            .with(mockall::predicate::eq(expected_key))
            .once()
            .returning(|_| Ok(()));

        let tracker = tracker_with_mock(mock);
        let (tx, mut rx) = tokio::sync::mpsc::channel(4);

        assert_eq!(
            tracker.ensure_model_downloaded(&target, &tx).await,
            ModelStatus::DOWNLOADED
        );

        // Clients see the model name they asked for, not the internal registry key.
        let update = rx
            .recv()
            .await
            .expect("Expected a status update")
            .expect("Expected an ok status update");
        assert_eq!(update.model_name, target.model_name);
        assert_eq!(update.resolved_revision, target.revision);
    }

    #[tokio::test]
    #[allow(clippy::await_holding_lock)]
    async fn test_tracker_scopes_the_lease_to_the_resolved_revision() {
        let env_lock = acquire_env_mutex();
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let _cache_dir_guard = EnvVarGuard::set(
            &env_lock,
            "MODEL_EXPRESS_CACHE_DIRECTORY",
            temp_dir.path().to_str().expect("Expected temp dir path"),
        );
        let _offline_guard = EnvVarGuard::set(&env_lock, "HF_HUB_OFFLINE", "1");
        let model_dir = temp_dir.path().join("models--test--model/snapshots/abc123");
        std::fs::create_dir_all(&model_dir).expect("Failed to create model dir");
        std::fs::write(model_dir.join("config.json"), b"{}").expect("Failed to write config");

        assert_claims_on_entry_key(DownloadTarget {
            revision: Some("abc123".to_string()),
            ..test_target("test/model")
        })
        .await;
    }

    #[tokio::test]
    #[allow(clippy::await_holding_lock)]
    async fn test_tracker_separates_metadata_only_from_full_weight_downloads() {
        let env_lock = acquire_env_mutex();
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let _cache_dir_guard = EnvVarGuard::set(
            &env_lock,
            "MODEL_EXPRESS_CACHE_DIRECTORY",
            temp_dir.path().to_str().expect("Expected temp dir path"),
        );
        let _offline_guard = EnvVarGuard::set(&env_lock, "HF_HUB_OFFLINE", "1");
        let model_dir = temp_dir.path().join("models--test--model/snapshots/abc123");
        std::fs::create_dir_all(&model_dir).expect("Failed to create model dir");
        std::fs::write(model_dir.join("config.json"), b"{}").expect("Failed to write config");

        // A metadata-only entry must not be the same registry record as a full-weight
        // one, or a weightless snapshot would satisfy a request that needs weights.
        assert_claims_on_entry_key(DownloadTarget {
            ignore_weights: true,
            ..test_target("test/model")
        })
        .await;
    }

    #[tokio::test]
    async fn test_tracker_set_status_notifies_waiting_channel() {
        let mut mock = crate::registry::backend::MockRegistryBackend::new();
        mock.expect_set_status()
            .once()
            .returning(|_, _, _, _| Ok(()));
        let tracker = tracker_with_mock(mock);

        let (tx, mut rx) = tokio::sync::mpsc::channel(4);
        tracker.add_waiting_channel("m", tx);

        tracker
            .set_status_and_notify(
                "m".to_string(),
                ModelStatus::DOWNLOADED,
                ModelProvider::HuggingFace,
                Some("done".to_string()),
            )
            .await;

        let update = rx.recv().await.expect("waiter should receive update");
        let update = update.expect("notify should send Ok");
        assert_eq!(update.model_name, "m");
        assert_eq!(
            update.status,
            modelexpress_common::grpc::model::ModelStatus::Downloaded as i32
        );
        assert_eq!(update.message.as_deref(), Some("done"));

        // Terminal status removes waiters.
        let waiters = tracker
            .waiting_channels
            .lock()
            .expect("waiters lock")
            .get("m")
            .map_or(0, std::vec::Vec::len);
        assert_eq!(waiters, 0);
    }

    #[tokio::test]
    async fn test_tracker_delete_status_clears_backend_and_waiters() {
        let mut mock = crate::registry::backend::MockRegistryBackend::new();
        mock.expect_delete_model().once().returning(|_| Ok(()));
        let tracker = tracker_with_mock(mock);

        let (tx, _rx) = tokio::sync::mpsc::channel(1);
        tracker.add_waiting_channel("m", tx);
        tracker.delete_status("m").await;

        let waiters = tracker
            .waiting_channels
            .lock()
            .expect("waiters lock")
            .contains_key("m");
        assert!(!waiters);
    }

    /// The bool `delete_status` returns is the sole gate on the `downloading ->
    /// absent` departure, and no caller is forced to read it -- there is no
    /// `#[must_use]` and two of the three call sites discard it. So the contract
    /// is pinned here directly rather than only through the delete-path tests
    /// below, which would still pass if the two arms were swapped in step.
    #[tokio::test]
    async fn delete_status_reports_whether_the_backend_removed_the_record() {
        let mut removed = crate::registry::backend::MockRegistryBackend::new();
        removed.expect_delete_model().once().returning(|_| Ok(()));
        assert!(
            tracker_with_mock(removed).delete_status("m").await,
            "a successful delete must report true"
        );

        let mut failed = crate::registry::backend::MockRegistryBackend::new();
        failed
            .expect_delete_model()
            .once()
            .returning(|_| Err("backend unreachable".into()));
        assert!(
            !tracker_with_mock(failed).delete_status("m").await,
            "the error is still swallowed, but it must not report a removal"
        );
    }

    /// `model clear` on an entry that is still downloading is the third exit from
    /// `DOWNLOADING`, and the only one the claim lifecycle cannot observe for
    /// itself: once the record is gone `finish_download_claim` finds nothing to
    /// fence and records no departure, so the deleting side has to book it.
    /// Without this the arrival booked by the claim is never matched and the flow
    /// accounting stays permanently ahead by one per deleted download.
    #[tokio::test]
    async fn deleting_a_downloading_entry_books_a_departure() {
        let mut mock = crate::registry::backend::MockRegistryBackend::new();
        mock.expect_get_models_by_last_used()
            .once()
            .returning(|_| Ok(vec![record_with_status("m", ModelStatus::DOWNLOADING)]));
        mock.expect_delete_model().once().returning(|_| Ok(()));

        let (tracker, metrics_registry) = tracker_with_mock_and_registry(mock);
        let outcome = tracker.delete_model_entries("m").await;
        assert_eq!(outcome.removed, 1);
        assert!(!outcome.degraded);

        let encoded = encoded_metrics(&metrics_registry);
        assert_eq!(
            departures_from_downloading(&encoded),
            1,
            "a deleted download must not stay counted as in flight: {encoded}"
        );
    }

    /// `delete_model` failing is swallowed so waiters are still notified, which is
    /// exactly why the departure has to be gated on the outcome. Booking it anyway
    /// would report a departure for an entry still sitting in `DOWNLOADING` and
    /// leave the flow count permanently short during a backend outage.
    #[tokio::test]
    async fn a_failed_delete_books_no_departure() {
        let mut mock = crate::registry::backend::MockRegistryBackend::new();
        mock.expect_get_models_by_last_used()
            .once()
            .returning(|_| Ok(vec![record_with_status("m", ModelStatus::DOWNLOADING)]));
        mock.expect_delete_model()
            .once()
            .returning(|_| Err("backend unreachable".into()));

        let (tracker, metrics_registry) = tracker_with_mock_and_registry(mock);
        tracker.delete_model_entries("m").await;

        let encoded = encoded_metrics(&metrics_registry);
        assert_eq!(
            departures_from_downloading(&encoded),
            0,
            "a swallowed delete failure must not book a departure: {encoded}"
        );
    }

    /// Ordinary cache eviction clears `DOWNLOADED` entries. Those never occupied
    /// the in-flight level, so counting them as departures would drive the
    /// derivation negative on the most common delete there is.
    #[tokio::test]
    async fn deleting_a_downloaded_entry_books_no_departure() {
        let mut mock = crate::registry::backend::MockRegistryBackend::new();
        mock.expect_get_models_by_last_used()
            .once()
            .returning(|_| Ok(vec![record_with_status("m", ModelStatus::DOWNLOADED)]));
        mock.expect_delete_model().once().returning(|_| Ok(()));

        let (tracker, metrics_registry) = tracker_with_mock_and_registry(mock);
        let outcome = tracker.delete_model_entries("m").await;
        assert_eq!(outcome.removed, 1, "the record is still removed");

        let encoded = encoded_metrics(&metrics_registry);
        assert_eq!(
            departures_from_downloading(&encoded),
            0,
            "a completed model was never in flight: {encoded}"
        );
    }

    /// `waiting_count` is the sole input to `mx_state_entries{map="download_waiters"}`,
    /// the gauge registered as "the OOM early warning". Stuck-at-zero is the failure
    /// mode that matters: the map leaks, the server heads for OOM, and the metric
    /// built to warn about it reads flat the whole way. The refresh task's own test
    /// substitutes a stub closure, so nothing else executes this function.
    #[tokio::test]
    async fn waiting_count_tracks_the_waiter_map() {
        let mut mock = crate::registry::backend::MockRegistryBackend::new();
        mock.expect_delete_model().once().returning(|_| Ok(()));
        let tracker = tracker_with_mock(mock);
        assert_eq!(tracker.waiting_count(), 0);

        let (tx_a, _rx_a) = tokio::sync::mpsc::channel(1);
        tracker.add_waiting_channel("a", tx_a);
        let (tx_b, _rx_b) = tokio::sync::mpsc::channel(1);
        tracker.add_waiting_channel("b", tx_b);
        // Keyed by model, so a second waiter on "a" does not add an entry.
        let (tx_a2, _rx_a2) = tokio::sync::mpsc::channel(1);
        tracker.add_waiting_channel("a", tx_a2);
        assert_eq!(tracker.waiting_count(), 2);

        tracker.delete_status("a").await;
        assert_eq!(tracker.waiting_count(), 1);
    }

    /// The download histogram registers and exports whether or not anything ever
    /// observes into it, so an empty series is the deceptive failure mode: the
    /// scrape looks healthy and simply reports zero downloads. This drives the
    /// real task to its terminal status and checks a sample landed.
    ///
    /// `finish_download_claim` returns `Ok(false)` on purpose -- this replica was
    /// fenced. The bytes were still fetched, so the observation has to happen
    /// before the fence check; returning `Ok(true)` here would leave that ordering
    /// unpinned.
    ///
    /// No network: the NGC provider rejects a model name that is not
    /// `org/name/version` before it opens a client or touches the filesystem, so
    /// the download future resolves to `Err` immediately and the task takes the
    /// `ERROR` outcome.
    #[tokio::test]
    async fn a_finished_download_is_observed_into_the_histogram_even_when_fenced() {
        let (done_tx, done_rx) = tokio::sync::oneshot::channel();
        let done_tx = Mutex::new(Some(done_tx));

        let mut mock = crate::registry::backend::MockRegistryBackend::new();
        mock.expect_finish_download_claim()
            .once()
            .returning(move |_, _, _, status, _| {
                assert_eq!(status, ModelStatus::ERROR);
                if let Ok(mut slot) = done_tx.lock()
                    && let Some(tx) = slot.take()
                {
                    let _ = tx.send(());
                }
                // Fenced: someone else owns the claim now.
                Ok(false)
            });

        let (tracker, metrics_registry) = tracker_with_mock_and_registry(mock);
        tracker.spawn_download_task(
            DownloadTarget {
                model_name: "not-a-valid-ngc-artifact".to_string(),
                provider: ModelProvider::Ngc,
                revision: None,
                ignore_weights: false,
            },
            false,
            "claim-1".to_string(),
        );
        done_rx.await.expect("the download task reaches the finish");

        let encoded = encoded_metrics(&metrics_registry);
        assert!(
            encoded.contains(r#"mx_download_seconds_count{outcome="error"} 1"#),
            "a failed download must be observed under its terminal status: {encoded}"
        );
    }

    #[tokio::test]
    async fn test_tracker_error_status_clears_waiters() {
        let mut mock = crate::registry::backend::MockRegistryBackend::new();
        mock.expect_set_status()
            .once()
            .returning(|_, _, _, _| Ok(()));
        let tracker = tracker_with_mock(mock);
        let (tx, _rx) = tokio::sync::mpsc::channel(1);
        tracker.add_waiting_channel("m", tx);
        tracker
            .set_status_and_notify(
                "m".to_string(),
                ModelStatus::ERROR,
                ModelProvider::HuggingFace,
                Some("fail".to_string()),
            )
            .await;
        let waiters = tracker
            .waiting_channels
            .lock()
            .expect("waiters lock")
            .get("m")
            .map_or(0, std::vec::Vec::len);
        assert_eq!(waiters, 0, "ERROR is terminal, waiters must be cleared");
    }

    #[tokio::test]
    async fn test_tracker_downloading_status_keeps_waiters() {
        let mut mock = crate::registry::backend::MockRegistryBackend::new();
        mock.expect_set_status()
            .once()
            .returning(|_, _, _, _| Ok(()));
        let tracker = tracker_with_mock(mock);
        let (tx, _rx) = tokio::sync::mpsc::channel(1);
        tracker.add_waiting_channel("m", tx);
        tracker
            .set_status_and_notify(
                "m".to_string(),
                ModelStatus::DOWNLOADING,
                ModelProvider::HuggingFace,
                None,
            )
            .await;
        let waiters = tracker
            .waiting_channels
            .lock()
            .expect("waiters lock")
            .get("m")
            .map_or(0, std::vec::Vec::len);
        assert_eq!(
            waiters, 1,
            "DOWNLOADING is non-terminal, waiter must remain"
        );
    }

    #[tokio::test]
    async fn test_tracker_set_status_swallows_backend_error() {
        let mut mock = crate::registry::backend::MockRegistryBackend::new();
        mock.expect_set_status()
            .once()
            .returning(|_, _, _, _| Err("redis down".into()));
        let tracker = tracker_with_mock(mock);
        let (tx, mut rx) = tokio::sync::mpsc::channel(1);
        tracker.add_waiting_channel("m", tx);
        // Error is logged but set_status_and_notify returns ()
        tracker
            .set_status_and_notify(
                "m".to_string(),
                ModelStatus::DOWNLOADED,
                ModelProvider::HuggingFace,
                None,
            )
            .await;
        // Nothing should be notified on the channel because set_status failed early.
        assert!(
            rx.try_recv().is_err(),
            "waiter shouldn't receive on backend error"
        );
    }

    /// Model service for the file-serving tests, which don't touch the tracker. A
    /// no-expectation mock backend keeps them off the `memory-backend` feature.
    fn test_model_service() -> ModelServiceImpl {
        let registry = Arc::new(RegistryManager::with_backend(Arc::new(
            crate::registry::backend::MockRegistryBackend::new(),
        )));
        ModelServiceImpl::new(Arc::new(ModelDownloadTracker::new(
            registry,
            DownloadMetrics::register(&mut crate::metrics::new_registry()),
            RegistryMetrics::register(&mut crate::metrics::new_registry()),
        )))
    }

    #[test]
    fn test_collect_model_files_empty_dir() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let files = collect_model_files(temp_dir.path(), temp_dir.path(), None, false);
        assert!(files.is_empty());
    }

    #[test]
    fn test_collect_model_files_with_files() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");

        // Create some test files
        let file1_path = temp_dir.path().join("config.json");
        std::fs::write(&file1_path, r#"{"test": "data"}"#).expect("Failed to write file1");

        let file2_path = temp_dir.path().join("model.bin");
        std::fs::write(&file2_path, vec![0u8; 100]).expect("Failed to write file2");

        let files = collect_model_files(temp_dir.path(), temp_dir.path(), None, false);

        assert_eq!(files.len(), 2);

        // Check file sizes
        let total_size: u64 = files.iter().map(|(_, size)| size).sum();
        assert!(total_size > 0);

        // Check that relative paths are correct
        let paths: Vec<_> = files
            .iter()
            .map(|(p, _)| p.to_string_lossy().to_string())
            .collect();
        assert!(paths.contains(&"config.json".to_string()));
        assert!(paths.contains(&"model.bin".to_string()));
    }

    #[test]
    fn test_collect_model_files_ignore_weights() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");

        // Mix of non-weight and weight files, including a nested weight shard.
        std::fs::write(temp_dir.path().join("config.json"), "{}").expect("write config");
        std::fs::write(temp_dir.path().join("tokenizer.json"), "{}").expect("write tokenizer");
        std::fs::write(temp_dir.path().join("model.safetensors"), vec![0u8; 10])
            .expect("write safetensors");
        std::fs::write(temp_dir.path().join("pytorch_model.bin"), vec![0u8; 10])
            .expect("write bin");
        let subdir = temp_dir.path().join("subdir");
        std::fs::create_dir(&subdir).expect("create subdir");
        std::fs::write(
            subdir.join("model-00001-of-00002.safetensors"),
            vec![0u8; 10],
        )
        .expect("write nested shard");

        // ignore_weights = false streams everything.
        let all = collect_model_files(temp_dir.path(), temp_dir.path(), None, false);
        assert_eq!(all.len(), 5);

        // ignore_weights = true keeps only the non-weight files (config + tokenizer),
        // dropping weight shards at any depth.
        let no_weights = collect_model_files(temp_dir.path(), temp_dir.path(), None, true);
        let names: Vec<String> = no_weights
            .iter()
            .map(|(p, _)| p.to_string_lossy().to_string())
            .collect();
        assert_eq!(
            no_weights.len(),
            2,
            "expected only non-weight files: {names:?}"
        );
        assert!(names.contains(&"config.json".to_string()));
        assert!(names.contains(&"tokenizer.json".to_string()));
        assert!(
            names
                .iter()
                .all(|n| !n.ends_with(".safetensors") && !n.ends_with(".bin")),
            "weight files must be excluded: {names:?}"
        );
    }

    #[test]
    fn test_collect_model_files_nested() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");

        // Create nested directory structure
        let subdir = temp_dir.path().join("subdir");
        std::fs::create_dir(&subdir).expect("Failed to create subdir");

        let file1_path = temp_dir.path().join("root_file.txt");
        std::fs::write(&file1_path, "root content").expect("Failed to write file1");

        let file2_path = subdir.join("nested_file.txt");
        std::fs::write(&file2_path, "nested content").expect("Failed to write file2");

        let files = collect_model_files(temp_dir.path(), temp_dir.path(), None, false);

        assert_eq!(files.len(), 2);

        // Check that nested path is correct
        let paths: Vec<_> = files
            .iter()
            .map(|(p, _)| p.to_string_lossy().to_string())
            .collect();
        assert!(paths.iter().any(|p| p.contains("nested_file")));
    }

    #[test]
    fn test_collect_model_files_with_selector_filters_exact_paths() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let subdir = temp_dir.path().join("subdir");
        std::fs::create_dir(&subdir).expect("Failed to create subdir");
        std::fs::write(temp_dir.path().join("config.json"), "{}").expect("Failed to write config");
        std::fs::write(temp_dir.path().join("model.bin"), vec![0u8; 100])
            .expect("Failed to write model");
        std::fs::write(temp_dir.path().join("ignored.txt"), "ignore")
            .expect("Failed to write ignored");
        std::fs::write(subdir.join("nested.txt"), "nested").expect("Failed to write nested");

        let selector = ModelFileSelector {
            paths: vec!["config.json".to_string(), "subdir/nested.txt".to_string()],
        };
        let files = collect_model_files(temp_dir.path(), temp_dir.path(), Some(&selector), false);

        let mut paths: Vec<_> = files
            .iter()
            .map(|(p, _)| p.to_string_lossy().to_string())
            .collect();
        paths.sort();
        assert_eq!(
            paths,
            vec!["config.json".to_string(), "subdir/nested.txt".to_string()]
        );
    }

    #[test]
    fn test_collect_model_files_with_selector_empty_and_nonmatching_paths() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        std::fs::write(temp_dir.path().join("config.json"), "{}").expect("Failed to write config");

        let empty_selector = ModelFileSelector { paths: vec![] };
        assert!(
            collect_model_files(
                temp_dir.path(),
                temp_dir.path(),
                Some(&empty_selector),
                false
            )
            .is_empty()
        );

        let nonmatching_selector = ModelFileSelector {
            paths: vec!["missing.json".to_string(), "../config.json".to_string()],
        };
        assert!(
            collect_model_files(
                temp_dir.path(),
                temp_dir.path(),
                Some(&nonmatching_selector),
                false
            )
            .is_empty()
        );
    }

    #[tokio::test]
    #[allow(clippy::await_holding_lock)]
    async fn test_list_model_files_hf_honors_file_selector() {
        let env_lock = acquire_env_mutex();
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let _cache_dir_guard = EnvVarGuard::set(
            &env_lock,
            "MODEL_EXPRESS_CACHE_DIRECTORY",
            temp_dir.path().to_str().expect("Expected temp dir path"),
        );
        let _offline_guard = EnvVarGuard::set(&env_lock, "HF_HUB_OFFLINE", "1");

        let model_dir = temp_dir.path().join("models--test--model/snapshots/abc123");
        std::fs::create_dir_all(model_dir.join("subdir")).expect("Failed to create model dir");
        std::fs::write(model_dir.join("config.json"), br#"{"model":"test"}"#)
            .expect("Failed to write config");
        std::fs::write(model_dir.join("model.bin"), vec![0u8; 100]).expect("Failed to write model");
        std::fs::write(model_dir.join("subdir/nested.txt"), b"nested")
            .expect("Failed to write nested");

        let service = test_model_service();
        let request = Request::new(ModelFilesRequest {
            model_name: "test/model".to_string(),
            provider: modelexpress_common::grpc::model::ModelProvider::HuggingFace as i32,
            chunk_size: 0,
            file_selector: Some(ModelFileSelector {
                paths: vec!["config.json".to_string(), "subdir/nested.txt".to_string()],
            }),
            ignore_weights: false,
            revision: None,
        });

        let response = service
            .list_model_files(request)
            .await
            .expect("Expected file list")
            .into_inner();
        let mut paths: Vec<_> = response
            .files
            .iter()
            .map(|file| file.relative_path.clone())
            .collect();
        paths.sort();

        assert_eq!(
            paths,
            vec!["config.json".to_string(), "subdir/nested.txt".to_string()]
        );
        assert_eq!(
            response.total_size,
            br#"{"model":"test"}"#.len() as u64 + b"nested".len() as u64
        );
    }

    #[tokio::test]
    #[allow(clippy::await_holding_lock)]
    async fn test_model_files_present_reflects_disk_state() {
        let env_lock = acquire_env_mutex();
        let _offline_guard = EnvVarGuard::set(&env_lock, "HF_HUB_OFFLINE", "1");
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let cache_dir = temp_dir.path().to_path_buf();

        // No files on disk: a stale DOWNLOADED record must not be honored.
        assert!(
            !model_files_present(
                Some(cache_dir.clone()),
                "test/model",
                ModelProvider::HuggingFace,
                None
            )
            .await
        );

        // Once the snapshot exists, the cache hit is real.
        let model_dir = cache_dir.join("models--test--model/snapshots/abc123");
        std::fs::create_dir_all(&model_dir).expect("Failed to create model dir");
        std::fs::write(model_dir.join("config.json"), b"{}").expect("Failed to write config");
        assert!(
            model_files_present(
                Some(cache_dir),
                "test/model",
                ModelProvider::HuggingFace,
                None
            )
            .await
        );
    }

    #[tokio::test]
    async fn test_model_files_present_assumes_present_without_cache_dir() {
        // With no configured cache directory we cannot verify, so we must not force a
        // re-download loop: assume the files are present.
        assert!(model_files_present(None, "test/model", ModelProvider::HuggingFace, None).await);
    }

    #[tokio::test]
    #[allow(clippy::await_holding_lock)]
    async fn test_stream_model_files_hf_honors_file_selector() {
        let env_lock = acquire_env_mutex();
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let _cache_dir_guard = EnvVarGuard::set(
            &env_lock,
            "MODEL_EXPRESS_CACHE_DIRECTORY",
            temp_dir.path().to_str().expect("Expected temp dir path"),
        );
        let _offline_guard = EnvVarGuard::set(&env_lock, "HF_HUB_OFFLINE", "1");

        let model_dir = temp_dir.path().join("models--test--model/snapshots/abc123");
        std::fs::create_dir_all(&model_dir).expect("Failed to create model dir");
        std::fs::write(model_dir.join("config.json"), br#"{"model":"test"}"#)
            .expect("Failed to write config");
        std::fs::write(model_dir.join("model.bin"), vec![0u8; 100]).expect("Failed to write model");

        let service = test_model_service();
        let request = Request::new(ModelFilesRequest {
            model_name: "test/model".to_string(),
            provider: modelexpress_common::grpc::model::ModelProvider::HuggingFace as i32,
            chunk_size: 1024,
            file_selector: Some(ModelFileSelector {
                paths: vec!["config.json".to_string()],
            }),
            ignore_weights: false,
            revision: None,
        });

        let response = service
            .stream_model_files(request)
            .await
            .expect("Expected stream response");
        let chunks: Vec<_> = response
            .into_inner()
            .map(|chunk| chunk.expect("Expected chunk"))
            .collect()
            .await;

        assert_eq!(chunks.len(), 1);
        assert_eq!(chunks[0].relative_path, "config.json");
        assert_eq!(chunks[0].commit_hash.as_deref(), Some("abc123"));
        assert!(chunks[0].is_last_file);
    }

    #[tokio::test]
    #[allow(clippy::await_holding_lock)]
    async fn test_stream_model_files_serves_the_requested_revision() {
        let env_lock = acquire_env_mutex();
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let _cache_dir_guard = EnvVarGuard::set(
            &env_lock,
            "MODEL_EXPRESS_CACHE_DIRECTORY",
            temp_dir.path().to_str().expect("Expected temp dir path"),
        );
        let _offline_guard = EnvVarGuard::set(&env_lock, "HF_HUB_OFFLINE", "1");

        // Two revisions cached side by side. Without a requested revision the newest
        // snapshot wins, so asking for the older one proves the request is honored.
        let write_snapshot = |commit: &str, body: &[u8]| {
            let model_dir = temp_dir
                .path()
                .join("models--test--model/snapshots")
                .join(commit);
            std::fs::create_dir_all(&model_dir).expect("Failed to create model dir");
            std::fs::write(model_dir.join("config.json"), body).expect("Failed to write config");
        };
        write_snapshot("older111", b"old");
        // Snapshot ordering is by directory timestamp, so the two have to be distinct.
        tokio::time::sleep(tokio::time::Duration::from_millis(1100)).await;
        write_snapshot("newer222", b"new");

        let stream_revision = async |revision: Option<&str>| {
            let request = Request::new(ModelFilesRequest {
                model_name: "test/model".to_string(),
                provider: modelexpress_common::grpc::model::ModelProvider::HuggingFace as i32,
                chunk_size: 1024,
                file_selector: None,
                ignore_weights: false,
                revision: revision.map(String::from),
            });
            let chunks: Vec<_> = test_model_service()
                .stream_model_files(request)
                .await
                .expect("Expected stream response")
                .into_inner()
                .map(|chunk| chunk.expect("Expected chunk"))
                .collect()
                .await;
            assert_eq!(chunks.len(), 1);
            (chunks[0].commit_hash.clone(), chunks[0].data.clone())
        };

        assert_eq!(
            stream_revision(Some("older111")).await,
            (Some("older111".to_string()), b"old".to_vec())
        );
        assert_eq!(
            stream_revision(None).await,
            (Some("newer222".to_string()), b"new".to_vec()),
            "Without a revision the newest snapshot is served, so the pinned request above \
             really did select a different one"
        );
    }

    #[tokio::test]
    async fn test_ensure_model_downloaded_rejects_a_revision_on_a_provider_without_revisions() {
        let service = test_model_service();
        let request = Request::new(ModelDownloadRequest {
            model_name: "gs://test-bucket/org/model/rev-1".to_string(),
            provider: modelexpress_common::grpc::model::ModelProvider::Gcs as i32,
            ignore_weights: false,
            revision: Some("v1.0".to_string()),
        });

        let status = service
            .ensure_model_downloaded(request)
            .await
            .map(|_| ())
            .expect_err("A provider without revisions must reject a pinned revision");
        assert_eq!(status.code(), tonic::Code::InvalidArgument);
        assert!(
            status
                .message()
                .contains("does not support pinned revisions"),
            "Unexpected message: {}",
            status.message()
        );
    }

    #[tokio::test]
    #[allow(clippy::await_holding_lock)]
    async fn test_stream_model_files_rejects_an_uncached_revision() {
        let env_lock = acquire_env_mutex();
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let _cache_dir_guard = EnvVarGuard::set(
            &env_lock,
            "MODEL_EXPRESS_CACHE_DIRECTORY",
            temp_dir.path().to_str().expect("Expected temp dir path"),
        );
        let _offline_guard = EnvVarGuard::set(&env_lock, "HF_HUB_OFFLINE", "1");

        let model_dir = temp_dir.path().join("models--test--model/snapshots/abc123");
        std::fs::create_dir_all(&model_dir).expect("Failed to create model dir");
        std::fs::write(model_dir.join("config.json"), b"{}").expect("Failed to write config");

        let service = test_model_service();
        let request = Request::new(ModelFilesRequest {
            model_name: "test/model".to_string(),
            provider: modelexpress_common::grpc::model::ModelProvider::HuggingFace as i32,
            chunk_size: 1024,
            file_selector: None,
            ignore_weights: false,
            revision: Some("not-cached".to_string()),
        });

        let status = service
            .stream_model_files(request)
            .await
            .map(|_| ())
            .expect_err("An uncached revision must not fall back to another snapshot");
        assert_eq!(status.code(), tonic::Code::NotFound);
    }

    #[tokio::test]
    #[allow(clippy::await_holding_lock)]
    async fn test_stream_model_files_hf_returns_not_found_for_missing_selector_path() {
        let env_lock = acquire_env_mutex();
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let _cache_dir_guard = EnvVarGuard::set(
            &env_lock,
            "MODEL_EXPRESS_CACHE_DIRECTORY",
            temp_dir.path().to_str().expect("Expected temp dir path"),
        );
        let _offline_guard = EnvVarGuard::set(&env_lock, "HF_HUB_OFFLINE", "1");

        let model_dir = temp_dir.path().join("models--test--model/snapshots/abc123");
        std::fs::create_dir_all(&model_dir).expect("Failed to create model dir");
        std::fs::write(model_dir.join("config.json"), br#"{"model":"test"}"#)
            .expect("Failed to write config");

        let service = test_model_service();
        let request = Request::new(ModelFilesRequest {
            model_name: "test/model".to_string(),
            provider: modelexpress_common::grpc::model::ModelProvider::HuggingFace as i32,
            chunk_size: 1024,
            file_selector: Some(ModelFileSelector {
                paths: vec!["config.json".to_string(), "missing.json".to_string()],
            }),
            ignore_weights: false,
            revision: None,
        });

        let result = service.stream_model_files(request).await;
        let status = result.expect_err("Expected not found");
        assert_eq!(status.code(), tonic::Code::NotFound);
        assert_eq!(
            status.message(),
            "Selected file not found in model directory: missing.json"
        );
    }

    #[tokio::test]
    async fn test_list_model_files_not_found() {
        let service = test_model_service();

        let request = Request::new(ModelFilesRequest {
            model_name: "non-existent-model-12345".to_string(),
            provider: modelexpress_common::grpc::model::ModelProvider::HuggingFace as i32,
            chunk_size: 0,
            file_selector: None,
            ignore_weights: false,
            revision: None,
        });

        let result = service.list_model_files(request).await;
        assert!(result.is_err());
        let status = result.expect_err("Should return error");
        assert_eq!(status.code(), tonic::Code::NotFound);
    }

    #[tokio::test]
    async fn test_stream_model_files_not_found() {
        let service = test_model_service();

        let request = Request::new(ModelFilesRequest {
            model_name: "non-existent-model-12345".to_string(),
            provider: modelexpress_common::grpc::model::ModelProvider::HuggingFace as i32,
            chunk_size: 1024,
            file_selector: None,
            ignore_weights: false,
            revision: None,
        });

        let result = service.stream_model_files(request).await;
        assert!(result.is_err());
        let status = result.expect_err("Should return error");
        assert_eq!(status.code(), tonic::Code::NotFound);
    }

    #[tokio::test]
    async fn test_ensure_model_downloaded_rejects_invalid_provider() {
        let service = test_model_service();

        let request = Request::new(ModelDownloadRequest {
            model_name: "test/model".to_string(),
            provider: 99,
            ignore_weights: false,
            revision: None,
        });

        let result = service.ensure_model_downloaded(request).await;
        assert!(result.is_err());
        let status = result.expect_err("Should return error");
        assert_eq!(status.code(), tonic::Code::InvalidArgument);
        assert!(status.message().contains("Invalid provider value"));
    }

    #[tokio::test]
    async fn test_list_model_files_rejects_invalid_provider() {
        let service = test_model_service();

        let request = Request::new(ModelFilesRequest {
            model_name: "test/model".to_string(),
            provider: 99,
            chunk_size: 0,
            file_selector: None,
            ignore_weights: false,
            revision: None,
        });

        let result = service.list_model_files(request).await;
        assert!(result.is_err());
        let status = result.expect_err("Should return error");
        assert_eq!(status.code(), tonic::Code::InvalidArgument);
        assert!(status.message().contains("Invalid provider value"));
    }

    #[tokio::test]
    async fn test_stream_model_files_rejects_invalid_provider() {
        let service = test_model_service();

        let request = Request::new(ModelFilesRequest {
            model_name: "test/model".to_string(),
            provider: 99,
            chunk_size: 1024,
            file_selector: None,
            ignore_weights: false,
            revision: None,
        });

        let result = service.stream_model_files(request).await;
        assert!(result.is_err());
        let status = result.expect_err("Should return error");
        assert_eq!(status.code(), tonic::Code::InvalidArgument);
        assert!(status.message().contains("Invalid provider value"));
    }

    #[tokio::test]
    #[allow(clippy::await_holding_lock)]
    async fn test_stream_model_files_hf_first_chunk_includes_commit_hash() {
        let env_lock = acquire_env_mutex();
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let _cache_dir_guard = EnvVarGuard::set(
            &env_lock,
            "MODEL_EXPRESS_CACHE_DIRECTORY",
            temp_dir.path().to_str().expect("Expected temp dir path"),
        );
        let _offline_guard = EnvVarGuard::set(&env_lock, "HF_HUB_OFFLINE", "1");

        let model_dir = temp_dir.path().join("models--test--model/snapshots/abc123");
        std::fs::create_dir_all(&model_dir).expect("Failed to create model dir");
        std::fs::write(model_dir.join("config.json"), br#"{"model":"test"}"#)
            .expect("Failed to write model file");

        let service = test_model_service();
        let request = Request::new(ModelFilesRequest {
            model_name: "test/model".to_string(),
            provider: modelexpress_common::grpc::model::ModelProvider::HuggingFace as i32,
            chunk_size: 1024,
            file_selector: None,
            ignore_weights: false,
            revision: None,
        });

        let response = service
            .stream_model_files(request)
            .await
            .expect("Expected stream response");
        let mut stream = response.into_inner();
        let first_chunk = stream
            .next()
            .await
            .expect("Expected stream item")
            .expect("Expected first chunk");

        assert_eq!(first_chunk.relative_path, "config.json");
        assert_eq!(first_chunk.commit_hash.as_deref(), Some("abc123"));
        assert!(first_chunk.is_last_chunk);
        assert!(first_chunk.is_last_file);
    }

    #[tokio::test]
    #[allow(clippy::await_holding_lock)]
    async fn test_stream_model_files_hf_emits_chunk_for_zero_byte_file() {
        let env_lock = acquire_env_mutex();
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let _cache_dir_guard = EnvVarGuard::set(
            &env_lock,
            "MODEL_EXPRESS_CACHE_DIRECTORY",
            temp_dir.path().to_str().expect("Expected temp dir path"),
        );
        let _offline_guard = EnvVarGuard::set(&env_lock, "HF_HUB_OFFLINE", "1");

        let model_dir = temp_dir.path().join("models--test--model/snapshots/abc123");
        std::fs::create_dir_all(&model_dir).expect("Failed to create model dir");
        std::fs::write(model_dir.join("empty.bin"), []).expect("Failed to write empty file");

        let service = test_model_service();
        let request = Request::new(ModelFilesRequest {
            model_name: "test/model".to_string(),
            provider: modelexpress_common::grpc::model::ModelProvider::HuggingFace as i32,
            chunk_size: 1024,
            file_selector: None,
            ignore_weights: false,
            revision: None,
        });

        let response = service
            .stream_model_files(request)
            .await
            .expect("Expected stream response");
        let mut stream = response.into_inner();
        let first_chunk = stream
            .next()
            .await
            .expect("Expected stream item")
            .expect("Expected first chunk");

        assert_eq!(first_chunk.relative_path, "empty.bin");
        assert_eq!(first_chunk.total_size, 0);
        assert_eq!(first_chunk.data.len(), 0);
        assert_eq!(first_chunk.offset, 0);
        assert_eq!(first_chunk.commit_hash.as_deref(), Some("abc123"));
        assert!(first_chunk.is_last_chunk);
        assert!(first_chunk.is_last_file);
    }
}