mxc-sdk 1.1.0

Importable library for starting MXC sandboxes in-process, with streaming stdio.
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Container image resolution shared by the one-shot runner and the state-aware daemon.

use std::fmt::Write;
use std::ptr;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

use crate::mxc_common::logger::Logger;
use crate::mxc_common::mxc_error::ApiFailure;
use crate::mxc_common::string_util::{to_wide, CoTaskMemPWSTR};

use crate::wslc_common::container_steps::{
    self, cstr_bytes, sdk_step_error, sdk_step_error_with_context, validate_store_access,
    StoredImage,
};
use crate::wslc_common::error::{sdk_api_failure, WslcError, WslcStepError};
use crate::wslc_common::registry_policy;
use crate::wslc_common::sdk_init;
use crate::wslc_common::store_slots;
use crate::wslc_common::wslc_bindings::*;

/// How long a single pull may run before it is abandoned.
const PULL_TIMEOUT: Duration = Duration::from_secs(540);

/// Env override (positive whole seconds) for [`PULL_TIMEOUT`]. Lets a test drive
/// the abort path without waiting out the production budget.
const PULL_TIMEOUT_ENV: &str = "MXC_WSLC_PULL_TIMEOUT_SECS";

/// Gap kept between a daemon pull's budget and the deadline its client is
/// waiting on.
///
/// A pull that outlived that deadline would surface as an abandoned container
/// rather than a failed provision the caller can act on.
const PULL_HEADROOM: Duration = Duration::from_secs(60);

/// Gap between progress lines while a pull is running.
const PULL_REPORT_INTERVAL: Duration = Duration::from_secs(10);

/// How long to wait for an abandoned pull before giving up on its session.
const PULL_DRAIN_TIMEOUT: Duration = Duration::from_secs(30);

/// `E_ABORT` — returned from the progress callback to stop a pull, and reported
/// back by the SDK as the pull's own result.
const E_ABORT: HRESULT = 0x8000_4004u32 as HRESULT;

/// How long a pull may run, honouring `PULL_TIMEOUT_ENV`.
pub fn pull_budget() -> Duration {
    std::env::var(PULL_TIMEOUT_ENV)
        .ok()
        .as_deref()
        .and_then(|s| s.trim().parse::<u64>().ok())
        .filter(|&secs| secs > 0)
        .map(Duration::from_secs)
        .unwrap_or(PULL_TIMEOUT)
}

/// How long a pull the daemon is waiting on may run.
///
/// Capped under the client's response deadline, so raising
/// `PULL_TIMEOUT_ENV` past that gap takes effect only once the client
/// deadline is raised with it.
pub fn daemon_pull_budget() -> Duration {
    capped_for_daemon(
        pull_budget(),
        crate::wslc_common::daemon_client::call_timeout(),
    )
}

/// A pull's budget, held under the deadline its client is waiting on.
fn capped_for_daemon(requested: Duration, call_timeout: Duration) -> Duration {
    requested.min(pull_budget_ceiling(call_timeout))
}

/// The most a daemon pull may take for its failure to still reach the client.
fn pull_budget_ceiling(call_timeout: Duration) -> Duration {
    call_timeout
        .checked_sub(PULL_HEADROOM)
        .filter(|d| !d.is_zero())
        .unwrap_or(call_timeout)
}

/// Shared with the SDK's progress callback for the duration of one pull.
struct PullWatch {
    deadline: Instant,
    started: Instant,
    /// Milliseconds since `started` at the last progress line, so the callback
    /// can rate-limit itself without a lock.
    last_report_ms: AtomicU64,
    image: String,
}

/// Called by the SDK as a pull advances; returns [`E_ABORT`] once the deadline
/// has passed, which stops the transfer.
///
/// # Safety
/// `context` must be the `*const PullWatch` handed to `WslcPullSessionImage`,
/// and must outlive the call.
unsafe extern "C" fn pull_progress(
    progress: *const WslcImageProgressMessage,
    context: PVOID,
) -> HRESULT {
    // This runs on an SDK thread across the C ABI, where an unwind would be
    // undefined behaviour. Reporting allocates, so the whole body is guarded.
    let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        let Some(watch) = (context as *const PullWatch).as_ref() else {
            return S_OK;
        };
        let now = Instant::now();
        if now >= watch.deadline {
            return E_ABORT;
        }

        let elapsed_ms = now.duration_since(watch.started).as_millis() as u64;
        let last = watch.last_report_ms.load(Ordering::Relaxed);
        if elapsed_ms.saturating_sub(last) >= PULL_REPORT_INTERVAL.as_millis() as u64
            && watch
                .last_report_ms
                .compare_exchange(last, elapsed_ms, Ordering::Relaxed, Ordering::Relaxed)
                .is_ok()
        {
            let detail = progress.as_ref().map(|p| p.detail);
            let transferred = match detail {
                Some(d) if d.totalBytes > 0 => format!(
                    " {}/{} MiB",
                    d.currentBytes / 1_048_576,
                    d.totalBytes / 1_048_576
                ),
                _ => String::new(),
            };
            eprintln!(
                "[WSLC] Pulling '{}' — {}s elapsed{}",
                watch.image,
                elapsed_ms / 1000,
                transferred
            );
        }
        S_OK
    }));
    outcome.unwrap_or(S_OK)
}

/// Pull `image` from its registry into the session's local image cache.
///
/// # Safety
/// `sdk` must hold valid function pointers and `session` must be a live handle.
pub unsafe fn pull_image(
    sdk: &WslcSdk,
    session: WslcSession,
    image: &str,
    store: &StoreHint,
    log_prefix: &str,
    logger: &mut Logger,
) -> Result<(), WslcStepError> {
    let budget = pull_budget();
    let _ = writeln!(
        logger,
        "{} Pulling image '{}' (up to {}s)",
        log_prefix,
        image,
        budget.as_secs()
    );

    let uri_cstr = cstr_bytes("image", image)?;
    let started = Instant::now();

    // A budget too large for the clock to represent falls back to the default,
    // which is bounded rather than instantly expired.
    let deadline = started
        .checked_add(budget)
        .unwrap_or_else(|| started + PULL_TIMEOUT);

    // Stationary for the whole call: the SDK holds this pointer across it.
    let watch = PullWatch {
        deadline,
        started,
        last_report_ms: AtomicU64::new(0),
        image: image.to_string(),
    };
    let pull_opts = WslcPullImageOptions {
        uri: uri_cstr.as_ptr() as PCSTR,
        progressCallback: Some(pull_progress),
        progressCallbackContext: &watch as *const PullWatch as PVOID,
        registryAuth: ptr::null(),
    };

    let mut pull_err = CoTaskMemPWSTR::null();
    let hr = sdk.WslcPullSessionImage(session, &pull_opts, pull_err.as_mut_ptr());
    if hr != S_OK {
        return Err(pull_failure(
            image,
            store,
            hr,
            &pull_err.to_string_lossy(),
            budget,
        ));
    }

    let _ = writeln!(logger, "{} Image '{}' pulled", log_prefix, image);
    Ok(())
}

/// Off-thread pulls that have not returned, across every session in this
/// process.
///
/// The session outlives the daemon's worker only if nothing is still using it,
/// so teardown consults this before releasing the handle.
static PULLS_IN_FLIGHT: AtomicUsize = AtomicUsize::new(0);

/// Counts one pull against a caller's own counter for as long as it runs.
struct PullCount(Arc<AtomicUsize>);

impl PullCount {
    /// Enter the count, if the caller asked for one.
    fn enter(counter: Option<Arc<AtomicUsize>>) -> Option<Self> {
        counter.map(|counter| {
            counter.fetch_add(1, Ordering::SeqCst);
            Self(counter)
        })
    }
}

impl Drop for PullCount {
    fn drop(&mut self) {
        self.0.fetch_sub(1, Ordering::SeqCst);
    }
}

/// Everything an off-thread pull needs, in a form that can cross a thread.
struct PullJob {
    sdk: *const WslcSdk,
    session: WslcSession,
    image: String,
    store: StoreHint,
}

// SAFETY: the SDK is apartment-affine, not thread-affine. Both this thread and
// the spawned one join the MTA, where a handle may be used from any member
// thread. Measured against a live 31s pull: 298 concurrent `WslcListSessionImages`
// calls on the same session all returned inside 29ms, so the SDK neither rejects
// nor serializes the overlap.
unsafe impl Send for PullJob {}

/// Run a pull on a thread of its own, handing the outcome to `on_done`.
///
/// Returns as soon as the thread starts; `on_done` runs on the pull thread.
///
/// Nothing here enforces [`pull_budget`]: the SDK reports progress only as
/// response chunks arrive and exposes no cancellation handle, so a transfer
/// stalled before its next chunk cannot be stopped from outside. The caller
/// arms its own deadline and stops waiting; this thread ends when the SDK's own
/// network timeouts fire.
///
/// # Safety
/// `sdk` must hold valid function pointers and `session` must be a live handle,
/// both outliving the pull -- see [`wait_for_pulls_in_flight`].
pub unsafe fn start_pull<F>(
    sdk: &WslcSdk,
    session: WslcSession,
    image: &str,
    store: &StoreHint,
    log_prefix: &str,
    on_done: F,
) -> Result<(), WslcStepError>
where
    F: FnOnce(Result<(), WslcStepError>) + Send + 'static,
{
    // SAFETY: forwarded unchanged; this shares `start_pull_counted`'s contract.
    unsafe { start_pull_counted(sdk, session, image, store, log_prefix, None, on_done) }
}

/// [`start_pull`], additionally counting the pull against `counter` until it has
/// reported.
///
/// A caller holding several sessions needs to know whether a pull is still
/// using *this* session, which the process-wide count cannot answer. `counter`
/// is kept alongside `PULLS_IN_FLIGHT`, never in place of it.
///
/// `counter` is released only once `on_done` has returned, so a caller that
/// observes it at zero can rely on the report having been delivered.
///
/// # Safety
/// `sdk` must hold valid function pointers and `session` must be a live handle,
/// both outliving the pull.
pub unsafe fn start_pull_counted<F>(
    sdk: &WslcSdk,
    session: WslcSession,
    image: &str,
    store: &StoreHint,
    log_prefix: &str,
    counter: Option<Arc<AtomicUsize>>,
    on_done: F,
) -> Result<(), WslcStepError>
where
    F: FnOnce(Result<(), WslcStepError>) + Send + 'static,
{
    let job = PullJob {
        sdk: sdk as *const WslcSdk,
        session,
        image: image.to_string(),
        store: store.clone(),
    };
    let prefix = log_prefix.to_string();

    // Entered before the spawn so a spawn failure unwinds it, rather than
    // leaving a count for a thread that never started.
    let counted = PullCount::enter(counter);
    PULLS_IN_FLIGHT.fetch_add(1, Ordering::SeqCst);
    let spawned = std::thread::Builder::new()
        .name("wslc-pull".to_string())
        .spawn(move || {
            let job = job;

            // Outlives `on_done` below, so a caller that sees this counter at
            // zero knows the report has already been handed over.
            let _counted = counted;
            let apartment = PullApartment::enter();
            let outcome = match apartment {
                Err(e) => Err(e),
                Ok(_apartment) => {
                    let mut log = Logger::new(crate::mxc_common::logger::Mode::Console);

                    // SAFETY: the caller keeps both alive for as long as this runs.
                    unsafe {
                        pull_image(
                            &*job.sdk,
                            job.session,
                            &job.image,
                            &job.store,
                            &prefix,
                            &mut log,
                        )
                    }
                }
            };
            PULLS_IN_FLIGHT.fetch_sub(1, Ordering::SeqCst);
            on_done(outcome);
        });

    if let Err(e) = spawned {
        PULLS_IN_FLIGHT.fetch_sub(1, Ordering::SeqCst);
        return Err(WslcError::Host(format!(
            "could not start a thread to pull WSLC image '{image}': {e}"
        ))
        .into_step_error());
    }
    Ok(())
}

/// Block until no off-thread pull is outstanding, or `budget` expires.
///
/// Reports whether the wait drained. An abandoned pull is still using the
/// session it was handed, so releasing that handle while one is outstanding
/// would free memory the SDK holds.
pub fn wait_for_pulls_in_flight(budget: Duration) -> bool {
    let deadline = Instant::now() + budget;
    while PULLS_IN_FLIGHT.load(Ordering::SeqCst) > 0 {
        if Instant::now() >= deadline {
            return false;
        }
        std::thread::sleep(Duration::from_millis(50));
    }
    true
}

/// Keeps a pull thread in the COM MTA for the SDK call.
struct PullApartment;

impl PullApartment {
    fn enter() -> Result<Self, WslcStepError> {
        use windows::Win32::System::Com::{CoInitializeEx, COINIT_MULTITHREADED};

        // SAFETY: called once on a freshly spawned thread; `Drop` below pairs it.
        let hr = unsafe { CoInitializeEx(None, COINIT_MULTITHREADED) };
        if hr.is_err() {
            return Err(sdk_step_error_with_context(
                "CoInitializeEx",
                "could not join the COM apartment for a WSLC pull",
                hr.0,
                "",
            ));
        }
        Ok(Self)
    }
}

impl Drop for PullApartment {
    fn drop(&mut self) {
        use windows::Win32::System::Com::CoUninitialize;

        // SAFETY: pairs the `CoInitializeEx` that produced this guard.
        unsafe { CoUninitialize() };
    }
}

/// Report a pull the caller stopped waiting for.
pub fn pull_deadline_expired(image: &str, budget: Duration) -> WslcStepError {
    WslcError::Host(format!(
        "WSLC image '{}' did not finish pulling within {}s. The transfer was \
         abandoned and this sandbox was not created. Retry, raise {} (a \
         state-aware run needs {} raised past it as well), or warm the cache \
         from a machine that can reach the registry.",
        image,
        budget.as_secs(),
        PULL_TIMEOUT_ENV,
        crate::wslc_common::daemon_client::CALL_TIMEOUT_ENV,
    ))
    .into_step_error()
    .with_api_failure(ApiFailure::new("WslcPullSessionImage"))
}

/// Classify a `WslcPullSessionImage` failure and render it for the caller.
fn pull_failure(
    image: &str,
    store: &StoreHint,
    hr: HRESULT,
    sdk_msg: &str,
    budget: Duration,
) -> WslcStepError {
    let sanitized = sanitize_sdk_message(sdk_msg);
    let detail = if sanitized.is_empty() {
        format!("HRESULT 0x{:08X}", hr as u32)
    } else {
        format!("{} (HRESULT 0x{:08X})", sanitized, hr as u32)
    };

    match hr {
        // Our own progress callback stopped this, so the registry is not at
        // fault and the SDK's text describes the abort rather than a cause.
        E_ABORT => WslcError::Host(format!(
            "WSLC image '{}' did not finish pulling within {}s and was stopped. \
             Retry, raise the budget with {}, or warm the cache from a machine \
             that can reach the registry with: wxc-exec.exe --setup-wslc --image {}{}.",
            image,
            budget.as_secs(),
            PULL_TIMEOUT_ENV,
            image,
            store.warm_instruction(),
        )),
        WSLC_E_IMAGE_NOT_FOUND => WslcError::Rejected(format!(
            "WSLC image '{}' could not be pulled: {}. Check the image name and tag. \
             For a private registry, MXC cannot supply credentials — set \
             wslc.imageTarPath to a local tar, or import the image out of band.",
            image, detail
        )),
        WSLC_E_REGISTRY_BLOCKED_BY_POLICY => WslcError::Rejected(format!(
            "WSLC image '{}' could not be pulled: {}. Administrative policy on this \
             host blocks the registry. Use a permitted registry, or set \
             wslc.imageTarPath to a local tar.",
            image, detail
        )),
        // The cause is not one this code recognises, so the remedy is stated as
        // a possibility rather than a diagnosis: a full disk and a corrupt
        // download reach here too, and neither is fixed by restoring network.
        _ => WslcError::Host(format!(
            "WSLC image '{}' could not be pulled: {}. If the registry was unreachable, \
             retry once it is available. Otherwise set wslc.imageTarPath to a local tar, \
             or warm this cache from a machine that can reach the registry with: \
             wxc-exec.exe --setup-wslc --image {}{}. \
             See docs/backends/wslc/wsl-container-getting-started.md.",
            image,
            detail,
            image,
            store.warm_instruction(),
        )),
    }
    .into_step_error()
    .with_api_failure(sdk_api_failure("WslcPullSessionImage", hr))
}

/// Longest run of registry-supplied text kept in a user-facing message.
const MAX_SDK_MESSAGE: usize = 400;

/// Make a registry's error text safe to print and to carry over the daemon pipe.
///
/// The string reaches here from whatever server the image reference named, so
/// it can carry terminal escapes or run arbitrarily long.
fn sanitize_sdk_message(sdk_msg: &str) -> String {
    let cleaned: String = sdk_msg
        .chars()
        .map(|c| if c.is_control() { ' ' } else { c })
        .collect();
    let cleaned = cleaned.trim();
    match cleaned.char_indices().nth(MAX_SDK_MESSAGE) {
        None => cleaned.to_string(),
        Some((cut, _)) => format!("{}…", &cleaned[..cut]),
    }
}

/// Which store a resolution is reading, so a failure can name the command that
/// warms *that* store.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum StoreHint {
    /// The create-and-run store, carrying any `wslc.storagePath` override.
    CreateAndRun(Option<String>),

    /// Store slot `index` of the container-lifecycle pool.
    DaemonSlot(usize),
}

impl StoreHint {
    /// The store directory this names.
    fn store_path(&self) -> String {
        match self {
            StoreHint::CreateAndRun(Some(path)) => path.clone(),
            StoreHint::CreateAndRun(None) => std::env::temp_dir()
                .join("mxc-wslc-sessions")
                .to_string_lossy()
                .to_string(),
            StoreHint::DaemonSlot(index) => {
                store_slots::slot_path(*index).to_string_lossy().to_string()
            }
        }
    }

    /// The arguments that point `--setup-wslc` at this store, naming the
    /// directory when the arguments leave it to be re-resolved from the
    /// environment of whoever runs them.
    fn warm_instruction(&self) -> String {
        match self {
            StoreHint::CreateAndRun(Some(path)) => format!(" --storage-path \"{}\"", path),
            StoreHint::CreateAndRun(None) => format!(" (which fills {})", self.store_path()),
            StoreHint::DaemonSlot(index) => format!(
                " --wslc-daemon-slots {} (which fills {})",
                index + 1,
                self.store_path()
            ),
        }
    }
}

/// Detected tar file format for image import.
enum TarFormat {
    /// Docker image archive from `docker save` (contains `manifest.json`).
    DockerSave,
    /// Rootfs filesystem tar from `docker export` (contains Linux root directories).
    Rootfs,
    /// Unrecognized format — not a valid tar or missing expected entries.
    Unknown,
}

/// Detect the format of a tar file by scanning its entries in a single pass.
///
/// - `manifest.json` present → Docker image archive (`docker save`)
/// - Top-level Linux directories (`bin`, `etc`, `usr`, etc.) → rootfs (`docker export`)
/// - Neither found after a successful scan → `TarFormat::Unknown`
/// - Open/read/parse failures → propagated as `std::io::Error`
fn detect_tar_format(path: &str) -> std::io::Result<TarFormat> {
    let file = std::fs::File::open(path)?;
    let mut archive = tar::Archive::new(file);
    let entries = archive.entries()?;

    const ROOTFS_MARKERS: &[&str] = &["bin", "etc", "usr", "lib", "sbin", "var"];
    let mut has_rootfs_dirs = false;

    for entry in entries {
        let entry = entry?;
        let entry_path = entry.path().map_err(|e| {
            std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                format!("failed to read tar entry path: {}", e),
            )
        })?;

        // Docker-save archives have `manifest.json` at the top level.
        // Match only the root entry — not nested `manifest.json` files
        // (e.g., from NPM packages). Handles both `manifest.json` and
        // `./manifest.json` (common tar prefix).
        let normalized: std::path::PathBuf = entry_path
            .components()
            .filter(|c| !matches!(c, std::path::Component::CurDir))
            .collect();
        if normalized.as_os_str() == "manifest.json" {
            return Ok(TarFormat::DockerSave);
        }

        if !has_rootfs_dirs {
            // Skip a leading `.` component that is commonly present
            // in tar archives (e.g., `./bin/...`).
            let first_component = entry_path
                .components()
                .find_map(|component| match component {
                    std::path::Component::CurDir => None,
                    other => Some(other),
                });

            if let Some(first) = first_component {
                let first_str = first.as_os_str().to_string_lossy();
                if ROOTFS_MARKERS
                    .iter()
                    .any(|marker| *marker == first_str.as_ref())
                {
                    has_rootfs_dirs = true;
                }
            }
        }
    }

    if has_rootfs_dirs {
        Ok(TarFormat::Rootfs)
    } else {
        Ok(TarFormat::Unknown)
    }
}

/// Import a container image from a local tar file.
///
/// Supports both rootfs tars (`docker export`) and Docker image archives
/// (`docker save`). The format is auto-detected via `detect_tar_format`.
pub(crate) unsafe fn import_image_from_tar(
    sdk: &WslcSdk,
    session: WslcSession,
    image_name: &str,
    tar_path: &str,
    logger: &mut Logger,
) -> Result<(), WslcStepError> {
    let path = std::path::Path::new(tar_path);
    if !path.exists() {
        return Err(WslcError::Rejected(format!(
            "Image tar file not found: '{}'. Provide a valid rootfs tar \
             (via 'docker export') or Docker image archive (via 'docker save').",
            tar_path
        ))
        .into_step_error());
    }

    // Resolve to absolute path, following symlinks. Fall back to the
    // original path if canonicalization fails (e.g., permissions).
    let canonical = std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf());
    let tar_path = canonical.to_string_lossy();

    let tar_format = match detect_tar_format(&tar_path) {
        Ok(fmt) => fmt,
        Err(e) => {
            return Err(WslcError::Rejected(format!(
                "Failed to read tar file '{}': {}",
                tar_path, e
            ))
            .into_step_error());
        }
    };
    let wide_path: Vec<u16> = to_wide(&tar_path);

    match tar_format {
        TarFormat::DockerSave => {
            let _ = writeln!(
                logger,
                "[WSLC] Loading Docker image archive from tar: {}",
                tar_path
            );
            let load_opts = WslcLoadImageOptions {
                progressCallback: None,
                progressCallbackContext: ptr::null_mut(),
            };
            let mut err_msg = CoTaskMemPWSTR::null();
            let hr = sdk.WslcLoadSessionImageFromFile(
                session,
                wide_path.as_ptr() as PCWSTR,
                &load_opts,
                err_msg.as_mut_ptr(),
            );
            if hr != S_OK {
                let msg = err_msg.to_string_lossy();
                return Err(sdk_step_error_with_context(
                    "WslcLoadSessionImageFromFile",
                    &format!("Failed to load Docker image archive from '{}'", tar_path),
                    hr,
                    &msg,
                ));
            }
            let _ = writeln!(
                logger,
                "[WSLC] Docker image archive loaded successfully from tar"
            );
            let _ = writeln!(
                logger,
                "[WSLC] Note: container will use image '{}' — ensure this \
                 matches the tag inside the Docker archive",
                image_name
            );
        }
        TarFormat::Rootfs => {
            let _ = writeln!(
                logger,
                "[WSLC] Importing rootfs image '{}' from tar: {}",
                image_name, tar_path
            );
            let name_cstr = format!("{}\0", image_name);
            let import_opts = WslcImportImageOptions {
                progressCallback: None,
                progressCallbackContext: ptr::null_mut(),
            };
            let mut err_msg = CoTaskMemPWSTR::null();
            let hr = sdk.WslcImportSessionImageFromFile(
                session,
                name_cstr.as_bytes().as_ptr() as PCSTR,
                wide_path.as_ptr() as PCWSTR,
                &import_opts,
                err_msg.as_mut_ptr(),
            );
            if hr != S_OK {
                let msg = err_msg.to_string_lossy();
                return Err(sdk_step_error_with_context(
                    "WslcImportSessionImageFromFile",
                    &format!("Failed to import image '{}' from tar", image_name),
                    hr,
                    &msg,
                ));
            }
            let _ = writeln!(
                logger,
                "[WSLC] Image '{}' imported successfully from tar",
                image_name
            );
        }
        TarFormat::Unknown => {
            return Err(WslcError::Rejected(format!(
                "Unrecognized tar format: '{}'. Provide a rootfs tar \
                 (via 'docker export') or a Docker image archive (via 'docker save').",
                tar_path
            ))
            .into_step_error());
        }
    }

    Ok(())
}

/// Whether a cache miss may reach the image's registry.
///
/// A sandbox that declares no egress gets no pull: the fetch would run on the
/// host's network before the container exists, so the declared posture could
/// not constrain it.
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum RegistryAccess {
    Allowed,
    Denied,
}

/// What a caller must still do to make an image available.
pub enum ImageStep {
    /// Nothing: the cache already held it, or a tar supplied it.
    Ready,

    /// It has to come from its registry; see [`start_pull`].
    Pull,
}

/// Decide how `image` will be made available, doing everything that does not
/// reach the network.
///
/// Split from the pull so a caller can park between the two; [`resolve_image`]
/// joins them back together.
///
/// # Safety
/// `sdk` must hold valid function pointers and `session` must be a live handle.
#[allow(clippy::too_many_arguments)]
pub unsafe fn begin_resolve(
    sdk: &WslcSdk,
    session: WslcSession,
    image: &str,
    image_tar_path: Option<&str>,
    store: &StoreHint,
    registry: RegistryAccess,
    log_prefix: &str,
    logger: &mut Logger,
) -> Result<ImageStep, WslcStepError> {
    let cached = list_cached_images(sdk, session)?;
    let matched = cached.iter().find(|entry| entry.satisfies(image));
    let policy = registry_policy::get_policy();

    match select_action(
        matched.is_some(),
        image_tar_path.is_some(),
        registry,
        &policy,
        image,
    ) {
        ImageAction::UseCache => {
            let digest = matched.map(|e| e.digest_hex()).unwrap_or_default();
            if image_tar_path.is_some() {
                let _ = writeln!(
                    logger,
                    "{} Image '{}' already cached ({}), skipping tar import",
                    log_prefix, image, digest
                );
            } else {
                let _ = writeln!(
                    logger,
                    "{} Image '{}' found ({})",
                    log_prefix, image, digest
                );
            }
            Ok(ImageStep::Ready)
        }
        ImageAction::ImportTar => {
            import_image_from_tar(
                sdk,
                session,
                image,
                image_tar_path.expect("tar action implies a tar path"),
                logger,
            )?;
            Ok(ImageStep::Ready)
        }
        ImageAction::Pull => Ok(ImageStep::Pull),
        ImageAction::RefuseByPolicy => {
            Err(WslcError::Rejected(policy.refusal(image)).into_step_error())
        }
        ImageAction::RefuseNoEgress => Err(no_egress_refusal(image, store)),
    }
}

fn no_egress_refusal(image: &str, store: &StoreHint) -> WslcStepError {
    if let StoreHint::DaemonSlot(index) = store {
        return WslcError::Rejected(format!(
            "WSLC isolated provision refused: daemon image store slot {index} at '{}' is cold \
             for image '{image}'. No container was created. Release the session or let it \
             expire before warming this store with wxc-exec.exe --setup-wslc --image {image}{}. \
             Alternatively, import a local tar with wslc.provision.imageTarPath or allow egress.",
            store.store_path(),
            store.warm_instruction(),
        ))
        .into_step_error();
    }
    WslcError::Rejected(format!(
        "WSLC image '{}' is not cached, and this sandbox's egress default is deny. \
         Pulling it would reach the registry on the host's network, before the \
         container exists and outside the policy the request declares. Warm the \
         cache first with wxc-exec.exe --setup-wslc --image {}{}, set \
         wslc.imageTarPath to a local tar, or allow egress. \
         See docs/backends/wslc/wsl-container-getting-started.md.",
        image,
        image,
        store.warm_instruction(),
    ))
    .into_step_error()
}

/// Record which content a completed pull produced.
///
/// A tag is mutable, so the digest is the only record of what this run
/// executed. A reference pinning a digest reports nothing, since the store's
/// digest is not the one it pinned.
///
/// # Safety
/// `sdk` must hold valid function pointers and `session` must be a live handle.
pub unsafe fn report_pulled_digest(
    sdk: &WslcSdk,
    session: WslcSession,
    image: &str,
    log_prefix: &str,
    logger: &mut Logger,
) -> Result<(), WslcStepError> {
    if let Some(entry) = list_cached_images(sdk, session)?
        .iter()
        .find(|entry| entry.satisfies(image))
    {
        let _ = writeln!(
            logger,
            "{} Image '{}' resolved to sha256:{}",
            log_prefix,
            image,
            entry.digest_hex()
        );
    }
    Ok(())
}

/// Ensure `image` is in the session's local cache: import it from
/// `image_tar_path` when one is supplied, otherwise pull it from its registry.
///
/// Pulls on the calling thread. A caller that serves other work from this
/// thread should drive [`begin_resolve`] and [`start_pull`] instead.
///
/// A pull that outlives its budget is abandoned rather than ended, so the
/// caller must drain [`wait_for_pulls_in_flight`] before releasing `session`.
///
/// # Safety
/// `sdk` must hold valid function pointers and `session` must be a live handle.
#[allow(clippy::too_many_arguments)]
pub unsafe fn resolve_image(
    sdk: &WslcSdk,
    session: WslcSession,
    image: &str,
    image_tar_path: Option<&str>,
    store: &StoreHint,
    registry: RegistryAccess,
    log_prefix: &str,
    logger: &mut Logger,
) -> Result<(), WslcStepError> {
    match begin_resolve(
        sdk,
        session,
        image,
        image_tar_path,
        store,
        registry,
        log_prefix,
        logger,
    )? {
        ImageStep::Ready => Ok(()),
        ImageStep::Pull => {
            // Waiting on the pull thread is what bounds this; the in-callback
            // deadline cannot fire on a registry that sends no further chunk.
            let budget = pull_budget();
            let (done, finished) = std::sync::mpsc::channel();
            start_pull(sdk, session, image, store, log_prefix, move |outcome| {
                let _ = done.send(outcome);
            })?;

            match finished.recv_timeout(budget) {
                Ok(outcome) => outcome?,
                Err(_) => return Err(pull_deadline_expired(image, budget)),
            }
            report_pulled_digest(sdk, session, image, log_prefix, logger)
        }
    }
}

impl StoredImage {
    /// Whether this entry satisfies a request for `requested`.
    ///
    /// A reference pinning a digest never does. The store reports the config
    /// digest while the reference carries the manifest digest, and the SDK
    /// exposes no mapping between them, so honouring the pin from the cache is
    /// not possible — matching on the repository name would silently serve
    /// whatever was fetched first, which is the opposite of pinning. Such a
    /// reference goes to the registry every time, which is slower and correct.
    fn satisfies(&self, requested: &str) -> bool {
        digest_of(requested).is_none() && names_same_image(&self.name, requested)
    }

    /// The stored content digest, as lowercase hex.
    fn digest_hex(&self) -> String {
        use std::fmt::Write as _;
        self.sha256.iter().fold(String::new(), |mut out, b| {
            let _ = write!(out, "{:02x}", b);
            out
        })
    }
}

/// The `sha256:<hex>` a reference pins, if it carries one.
fn digest_of(reference: &str) -> Option<&str> {
    reference
        .rsplit_once('@')
        .and_then(|(_, digest)| digest.strip_prefix("sha256:"))
}

/// Read the session's image store.
///
/// # Safety
/// `sdk` must hold valid function pointers and `session` must be a live handle.
unsafe fn list_cached_images(
    sdk: &WslcSdk,
    session: WslcSession,
) -> Result<Vec<StoredImage>, WslcStepError> {
    container_steps::list_stored_images(sdk, session)
        .map_err(|hr| sdk_step_error("WslcListSessionImages", hr, ""))
}

/// How `resolve_image` satisfies the request once the cache has been consulted.
#[derive(Debug, PartialEq, Eq)]
enum ImageAction {
    UseCache,
    ImportTar,
    Pull,
    RefuseNoEgress,

    /// Administrative policy does not permit this image's registry.
    RefuseByPolicy,
}

/// Choose between the cache, the caller's tar, and the registry.
fn select_action(
    cached: bool,
    has_tar: bool,
    registry: RegistryAccess,
    policy: &registry_policy::RegistryPolicy,
    image: &str,
) -> ImageAction {
    match (cached, has_tar, registry) {
        (true, _, _) => ImageAction::UseCache,
        (false, true, _) => ImageAction::ImportTar,
        (false, false, RegistryAccess::Denied) => ImageAction::RefuseNoEgress,
        (false, false, RegistryAccess::Allowed) if !policy.permits(image) => {
            ImageAction::RefuseByPolicy
        }
        (false, false, RegistryAccess::Allowed) => ImageAction::Pull,
    }
}

/// Whether a stored image name denotes the image `requested`.
///
/// A reference with no tag means `:latest`, which the store spells out, so
/// comparing the two literally would miss and re-pull on every run.
fn names_same_image(stored: &str, requested: &str) -> bool {
    stored == requested || stored == with_implicit_tag(requested)
}

/// `name` with the `:latest` the registry implies when a reference omits a tag.
fn with_implicit_tag(name: &str) -> String {
    // A colon in the final path segment is the tag; one before a `/` is a
    // registry port, as in `localhost:5000/img`. A digest pins its own content
    // and never takes an implicit tag.
    let last_segment = name.rsplit('/').next().unwrap_or(name);
    if last_segment.contains(':') || name.contains('@') {
        name.to_string()
    } else {
        format!("{}:latest", name)
    }
}

/// Warm the create-and-run store's image cache with `images`, and the first
/// `daemon_slots` container-lifecycle store slots with them too, backing
/// `wxc-exec.exe --setup-wslc`.
///
/// Each store is opened once and filled with every image, so warming M images
/// across N slots costs N+1 sessions rather than M×(N+1).
///
/// The lifecycle daemon leases its store from a separate pool, so warming the
/// create-and-run store does not warm the daemon's. A slot that has never been
/// leased holds no images at all, and an uncached image under the default
/// deny-egress posture is refused before the VM is reached.
///
/// # Safety
/// Must be called once per process before any other WSLC SDK functions
/// (it initialises COM via `init_and_load_sdk`).
pub unsafe fn setup_pull_image(
    images: &[&str],
    storage_path: Option<&str>,
    daemon_slots: usize,
    logger: &mut Logger,
) -> Result<(), String> {
    let sdk = match sdk_init::init_and_load_sdk(logger) {
        Ok(s) => s,
        Err(resp) => return Err(resp.error_message),
    };

    let mut targets = vec![StoreHint::CreateAndRun(storage_path.map(str::to_string))];
    targets.extend((0..daemon_slots).map(StoreHint::DaemonSlot));

    warm_each(&targets, |target| {
        if let StoreHint::DaemonSlot(index) = target {
            let slot = store_slots::slot_path(*index);
            std::fs::create_dir_all(&slot).map_err(|e| {
                format!(
                    "could not create the WSLc store slot at {}: {e}",
                    slot.display()
                )
            })?;
        }
        warm_store(sdk, images, target, logger)
    })
}

/// Warm every target, reporting what failed rather than stopping at the first.
///
/// The create-and-run store and the lifecycle slots are independent, so one
/// being held says nothing about whether the others can be filled.
fn warm_each(
    targets: &[StoreHint],
    mut warm: impl FnMut(&StoreHint) -> Result<(), String>,
) -> Result<(), String> {
    let mut failures = Vec::new();

    for target in targets {
        if let Err(e) = warm(target) {
            failures.push(match target {
                StoreHint::CreateAndRun(_) => e,
                StoreHint::DaemonSlot(index) => format!("daemon store slot {index}: {e}"),
            });
        }
    }

    if failures.is_empty() {
        Ok(())
    } else {
        Err(failures.join("; "))
    }
}

/// What one warm attempt leaves behind.
enum PullStep {
    /// The image landed, or failed in a way that leaves the session usable.
    Continue(Result<(), String>),

    /// The pull was abandoned and may still be writing into the session, so
    /// nothing further may be pulled on it.
    Abandoned(String),
}

/// Pull every image on one session, stopping only once the session is no longer
/// usable.
fn pull_each(images: &[&str], mut pull: impl FnMut(&str) -> PullStep) -> Result<(), String> {
    let mut failures = Vec::new();

    for image in images {
        match pull(image) {
            PullStep::Continue(Ok(())) => {}
            PullStep::Continue(Err(e)) => failures.push(e),
            PullStep::Abandoned(e) => {
                failures.push(e);
                break;
            }
        }
    }

    if failures.is_empty() {
        Ok(())
    } else {
        Err(failures.join("; "))
    }
}

/// Pull `images` into the store `store` names, on one session.
///
/// # Safety
/// `sdk` must hold valid function pointers and COM must be initialised on this
/// thread.
unsafe fn warm_store(
    sdk: &'static WslcSdk,
    images: &[&str],
    store: &StoreHint,
    logger: &mut Logger,
) -> Result<(), String> {
    let storage_path_str = store.store_path();
    let session_name: Vec<u16> = to_wide("mxc-setup-wslc");
    let storage_path_wide: Vec<u16> = to_wide(&storage_path_str);

    let mut settings = std::mem::zeroed::<WslcSessionSettings>();
    let hr = sdk.WslcInitSessionSettings(
        session_name.as_ptr(),
        storage_path_wide.as_ptr(),
        &mut settings,
    );
    if hr != S_OK {
        return Err(format!(
            "WslcInitSessionSettings failed (HRESULT 0x{:08X})",
            hr as u32
        ));
    }

    let mut session: WslcSession = ptr::null_mut();
    let mut create_err = CoTaskMemPWSTR::null();
    let hr = sdk.WslcCreateSession(&mut settings, &mut session, create_err.as_mut_ptr());
    if hr != S_OK {
        return Err(format!(
            "WslcCreateSession failed (HRESULT 0x{:08X}): {}",
            hr as u32,
            create_err.to_string_lossy()
        ));
    }
    let session_guard = WslcSessionGuard::from_raw(
        session,
        sdk.terminate_session_fn(),
        sdk.release_session_fn(),
    );

    // A slot admits one live session, and a conflict is invisible until the
    // store is touched, so a held slot would otherwise surface partway through
    // the pull.
    if matches!(store, StoreHint::DaemonSlot(_)) {
        validate_store_access(sdk, session, &storage_path_str)
            .map_err(|resp| resp.error_message)?;
    }

    let _ = writeln!(logger, "[WSLC setup] Target store: {}", storage_path_str);

    // Taken when a pull is abandoned, so the session is leaked rather than
    // released.
    let mut session_guard = Some(session_guard);

    pull_each(images, |image| {
        let budget = pull_budget();
        let (done, finished) = std::sync::mpsc::channel();
        if let Err(resp) = start_pull(sdk, session, image, store, "[WSLC setup]", move |outcome| {
            let _ = done.send(outcome);
        }) {
            return PullStep::Continue(Err(resp.error_message));
        }

        match finished.recv_timeout(budget) {
            Ok(outcome) => PullStep::Continue(outcome.map_err(|resp| resp.error_message)),
            Err(_) => {
                if !wait_for_pulls_in_flight(PULL_DRAIN_TIMEOUT) {
                    if let Some(guard) = session_guard.take() {
                        std::mem::forget(guard);
                    }
                }
                PullStep::Abandoned(pull_deadline_expired(image, budget).error_message)
            }
        }
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::mxc_common::models::FailurePhase;
    use std::io::Write;

    /// `E_FAIL`, which the SDK returns when it cannot reach the registry at all.
    const E_FAIL: HRESULT = -2147467259;

    /// `pull_failure` at the production budget, which most cases do not care about.
    fn failure(image: &str, store: &StoreHint, hr: HRESULT, sdk_msg: &str) -> WslcStepError {
        pull_failure(image, store, hr, sdk_msg, PULL_TIMEOUT)
    }

    #[test]
    fn a_pull_the_caller_gave_up_on_says_no_sandbox_was_created() {
        let resp = pull_deadline_expired("alpine:latest", Duration::from_secs(90));
        assert!(
            resp.error_message.contains("within 90s"),
            "the budget that was exceeded has to be in the message: {}",
            resp.error_message
        );
        assert!(
            resp.error_message.contains(PULL_TIMEOUT_ENV),
            "the operator needs the knob that raises it"
        );
        assert!(
            resp.error_message.contains("was not created"),
            "the caller has to know no sandbox is waiting for it: {}",
            resp.error_message
        );
        // Retrying can succeed, so this is not a rejection.
        assert_eq!(resp.failure_phase, FailurePhase::LaunchFailed);
    }

    /// Teardown consults this before releasing the session, so an idle daemon
    /// must not be delayed by it.
    #[test]
    fn draining_returns_at_once_when_no_pull_is_outstanding() {
        let start = Instant::now();
        assert!(wait_for_pulls_in_flight(Duration::from_secs(30)));
        assert!(start.elapsed() < Duration::from_secs(1));
    }

    #[test]
    fn draining_reports_failure_once_the_budget_expires() {
        PULLS_IN_FLIGHT.fetch_add(1, Ordering::SeqCst);
        let drained = wait_for_pulls_in_flight(Duration::from_millis(150));
        PULLS_IN_FLIGHT.fetch_sub(1, Ordering::SeqCst);
        assert!(
            !drained,
            "an outstanding pull must be reported, not waited out silently"
        );
    }

    #[test]
    fn an_abort_is_reported_as_a_budget_overrun_not_a_registry_failure() {
        let resp = pull_failure(
            "alpine:latest",
            &StoreHint::CreateAndRun(None),
            E_ABORT,
            "operation aborted",
            Duration::from_secs(90),
        );
        assert!(
            resp.error_message.contains("within 90s"),
            "the budget that was exceeded has to be in the message: {}",
            resp.error_message
        );
        assert!(
            resp.error_message.contains(PULL_TIMEOUT_ENV),
            "the operator needs the knob that raises it"
        );
        assert!(
            !resp.error_message.contains("registry was unreachable"),
            "we stopped this, so the registry must not be blamed: {}",
            resp.error_message
        );
        // Raising the budget or retrying can succeed, so this is not a rejection.
        assert_eq!(resp.failure_phase, FailurePhase::LaunchFailed);
    }

    #[test]
    fn the_pull_budget_is_under_the_daemon_response_deadline() {
        // A pull that outlives the client's wait produces a container whose id
        // never reaches anyone.
        assert!(
            PULL_TIMEOUT < Duration::from_secs(600),
            "PULL_TIMEOUT must stay below the daemon client's CALL_TIMEOUT"
        );
    }

    fn cached(name: &str) -> StoredImage {
        StoredImage {
            name: name.to_string(),
            sha256: [0u8; 32],
        }
    }

    #[test]
    fn a_digest_reference_never_satisfies_from_cache() {
        // The store reports a config digest; the reference carries a manifest
        // digest. Matching on the repository alone would serve whatever was
        // fetched first, which defeats the pin.
        let entry = cached("busybox");
        assert!(!entry.satisfies("busybox@sha256:ab33eacc"));
        assert!(entry.satisfies("busybox"));
    }

    /// A store holding several tags of one repository must not attribute one
    /// tag's digest to another.
    #[test]
    fn a_pulled_digest_is_reported_against_the_exact_tag() {
        let other_tag = cached("busybox:1.35");
        assert!(!other_tag.satisfies("busybox:latest"));
        assert!(!other_tag.satisfies("busybox"));

        let pulled = cached("busybox:latest");
        assert!(pulled.satisfies("busybox:latest"));
        assert!(pulled.satisfies("busybox"));
    }

    /// The store reports a config digest, which is not the manifest digest a
    /// reference pins, so a pinned reference gets no audit line rather than a
    /// wrong one.
    #[test]
    fn a_pinned_reference_reports_no_digest() {
        let entry = cached("busybox");
        assert!(!entry.satisfies("busybox@sha256:ab33eacc"));
    }

    /// The budget has to leave the caller time to receive the failure, or a
    /// slow pull becomes an abandoned container instead of a failed provision.
    #[test]
    fn the_budget_ceiling_stays_under_the_callers_deadline() {
        let ceiling = pull_budget_ceiling(Duration::from_secs(600));
        assert!(ceiling < Duration::from_secs(600));
        assert_eq!(ceiling, Duration::from_secs(540));
    }

    /// A caller deadline shorter than the headroom leaves nothing to subtract;
    /// the pull gets the whole window rather than a zero budget that would
    /// abort every pull immediately.
    #[test]
    fn a_short_caller_deadline_does_not_collapse_the_budget() {
        for secs in [1u64, 30, 60] {
            let ceiling = pull_budget_ceiling(Duration::from_secs(secs));
            assert_eq!(
                ceiling,
                Duration::from_secs(secs),
                "caller deadline {secs}s"
            );
        }
    }

    /// The default budget already sits at the ceiling, so capping every caller
    /// would leave the advertised override able to lower the budget but never
    /// raise it. Only a pull a client is waiting on needs the cap.
    #[test]
    fn only_a_daemon_pull_is_capped_by_the_client_deadline() {
        let raised = Duration::from_secs(900);
        assert_eq!(
            capped_for_daemon(raised, Duration::from_secs(600)),
            Duration::from_secs(540)
        );
        assert_eq!(
            capped_for_daemon(raised, Duration::from_secs(1200)),
            raised,
            "a client deadline raised with it must let the budget through"
        );
    }

    /// Raising only the pull budget on the daemon path does nothing, so the
    /// message has to name the deadline that also has to move.
    #[test]
    fn the_timeout_message_names_both_knobs() {
        let resp = pull_deadline_expired("alpine:latest", Duration::from_secs(90));
        assert!(resp.error_message.contains(PULL_TIMEOUT_ENV));
        assert!(
            resp.error_message
                .contains(crate::wslc_common::daemon_client::CALL_TIMEOUT_ENV),
            "a state-aware operator needs the coupled deadline: {}",
            resp.error_message
        );
        let api_failure = resp.api_failure.as_deref().expect("pull operation");
        assert_eq!(api_failure.operation, "WslcPullSessionImage");
        assert!(api_failure.native_code.is_none());
    }

    #[test]
    fn a_digest_is_rendered_as_lowercase_hex() {
        let mut sha = [0u8; 32];
        sha[0] = 0x0e;
        sha[31] = 0xff;
        let hex = StoredImage {
            name: "x".to_string(),
            sha256: sha,
        }
        .digest_hex();
        assert_eq!(hex.len(), 64, "32 bytes render as 64 hex digits");
        assert!(hex.starts_with("0e"), "leading zero nibble is kept: {hex}");
        assert!(hex.ends_with("ff"));
    }

    /// Policy is only consulted for a pull, so the other branches are decided
    /// against an unmanaged machine.
    fn unmanaged() -> registry_policy::RegistryPolicy {
        registry_policy::RegistryPolicy::Unmanaged
    }

    #[test]
    fn a_cached_image_is_used_whatever_else_was_offered() {
        for has_tar in [false, true] {
            for registry in [RegistryAccess::Allowed, RegistryAccess::Denied] {
                assert_eq!(
                    select_action(true, has_tar, registry, &unmanaged(), "alpine:latest"),
                    ImageAction::UseCache
                );
            }
        }
    }

    #[test]
    fn a_tar_is_imported_rather_than_pulled() {
        // A local tar needs no registry, so an isolated sandbox keeps working.
        assert_eq!(
            select_action(false, true, RegistryAccess::Denied, &unmanaged(), "a:1"),
            ImageAction::ImportTar
        );
        assert_eq!(
            select_action(false, true, RegistryAccess::Allowed, &unmanaged(), "a:1"),
            ImageAction::ImportTar
        );
    }

    #[test]
    fn a_miss_pulls_only_when_egress_is_allowed() {
        assert_eq!(
            select_action(false, false, RegistryAccess::Allowed, &unmanaged(), "a:1"),
            ImageAction::Pull
        );
        assert_eq!(
            select_action(false, false, RegistryAccess::Denied, &unmanaged(), "a:1"),
            ImageAction::RefuseNoEgress
        );
    }

    #[test]
    fn a_registry_outside_the_allowlist_is_refused() {
        let policy = registry_policy::RegistryPolicy::Allowed(vec!["ghcr.io".to_string()]);
        assert_eq!(
            select_action(
                false,
                false,
                RegistryAccess::Allowed,
                &policy,
                "docker.io/library/alpine:3"
            ),
            ImageAction::RefuseByPolicy
        );
        assert_eq!(
            select_action(
                false,
                false,
                RegistryAccess::Allowed,
                &policy,
                "ghcr.io/owner/img:1"
            ),
            ImageAction::Pull
        );
    }

    /// A cached image and a local tar reach no registry, so policy must not
    /// block work that was never going to leave the machine.
    #[test]
    fn policy_does_not_block_the_cache_or_a_tar() {
        let policy = registry_policy::RegistryPolicy::Unreadable;
        assert_eq!(
            select_action(true, false, RegistryAccess::Allowed, &policy, "a:1"),
            ImageAction::UseCache
        );
        assert_eq!(
            select_action(false, true, RegistryAccess::Allowed, &policy, "a:1"),
            ImageAction::ImportTar
        );
    }

    /// An unreadable policy is a deployment an administrator meant to restrict.
    #[test]
    fn an_unreadable_policy_refuses_a_pull() {
        assert_eq!(
            select_action(
                false,
                false,
                RegistryAccess::Allowed,
                &registry_policy::RegistryPolicy::Unreadable,
                "a:1"
            ),
            ImageAction::RefuseByPolicy
        );
    }

    #[test]
    fn an_untagged_reference_matches_the_stored_latest_tag() {
        assert!(names_same_image("busybox:latest", "busybox"));
        assert!(names_same_image("busybox:latest", "busybox:latest"));
        assert!(names_same_image(
            "ghcr.io/owner/img:latest",
            "ghcr.io/owner/img"
        ));
    }

    #[test]
    fn a_registry_port_is_not_mistaken_for_a_tag() {
        assert_eq!(
            with_implicit_tag("localhost:5000/img"),
            "localhost:5000/img:latest"
        );
        assert!(names_same_image(
            "localhost:5000/img:latest",
            "localhost:5000/img"
        ));
    }

    #[test]
    fn an_explicit_tag_or_digest_is_left_alone() {
        assert_eq!(with_implicit_tag("busybox:1.36"), "busybox:1.36");
        assert_eq!(
            with_implicit_tag("busybox@sha256:abc"),
            "busybox@sha256:abc"
        );
        assert!(!names_same_image("busybox:latest", "busybox:1.36"));
    }

    #[test]
    fn a_different_image_never_matches() {
        assert!(!names_same_image("alpine:latest", "busybox"));
        assert!(!names_same_image("busyboxer:latest", "busybox"));
    }

    #[test]
    fn a_refused_reference_is_not_worth_retrying() {
        let resp = failure(
            "ghcr.io/nope:1",
            &StoreHint::CreateAndRun(None),
            WSLC_E_IMAGE_NOT_FOUND,
            "denied",
        );
        assert_eq!(resp.failure_phase, FailurePhase::Rejected);
        let api_failure = resp.api_failure.as_deref().expect("pull operation");
        assert_eq!(api_failure.operation, "WslcPullSessionImage");
        assert_eq!(api_failure.native_code.as_deref(), Some("0x80040601"));
    }

    #[test]
    fn a_blocked_registry_is_not_worth_retrying() {
        let resp = failure(
            "ghcr.io/nope:1",
            &StoreHint::CreateAndRun(None),
            WSLC_E_REGISTRY_BLOCKED_BY_POLICY,
            "blocked",
        );
        assert_eq!(resp.failure_phase, FailurePhase::Rejected);
    }

    #[test]
    fn an_unreachable_registry_is_retryable() {
        let resp = failure(
            "alpine:latest",
            &StoreHint::CreateAndRun(None),
            E_FAIL,
            "no such host",
        );
        assert_eq!(resp.failure_phase, FailurePhase::LaunchFailed);
    }

    #[test]
    fn an_unreachable_registry_says_how_to_work_offline() {
        let resp = failure(
            "alpine:latest",
            &StoreHint::CreateAndRun(None),
            E_FAIL,
            "no such host",
        );
        assert!(resp.error_message.contains("retry once it is available"));
        assert!(resp.error_message.contains("imageTarPath"));
    }

    #[test]
    fn an_unrecognised_failure_does_not_diagnose_the_network() {
        // A full disk reaches this arm too, so the wording offers the network
        // as one possibility rather than asserting it.
        let resp = failure(
            "alpine:latest",
            &StoreHint::CreateAndRun(None),
            E_FAIL,
            "no space left on device",
        );
        assert!(
            resp.error_message
                .contains("If the registry was unreachable"),
            "unknown causes must be stated conditionally: {}",
            resp.error_message
        );
    }

    #[test]
    fn registry_text_cannot_smuggle_control_characters() {
        let hostile = "denied\u{1b}[2Jcleared\u{7}\nnext";
        let resp = failure(
            "alpine:latest",
            &StoreHint::CreateAndRun(None),
            E_FAIL,
            hostile,
        );
        assert!(!resp.error_message.contains('\u{1b}'));
        assert!(!resp.error_message.contains('\u{7}'));
        assert!(!resp.error_message.contains('\n'));
        assert!(resp.error_message.contains("cleared"));
    }

    #[test]
    fn registry_text_is_capped() {
        let flood = "x".repeat(5_000);
        let resp = failure(
            "alpine:latest",
            &StoreHint::CreateAndRun(None),
            E_FAIL,
            &flood,
        );
        assert!(resp.error_message.contains('…'));
        assert!(
            resp.error_message.len() < 1_500,
            "a hostile registry must not dictate the message length, got {}",
            resp.error_message.len()
        );
    }

    #[test]
    fn a_multibyte_registry_message_is_cut_on_a_character_boundary() {
        // Slicing by byte offset here would panic mid-character.
        let flood = "é".repeat(5_000);
        let resp = failure(
            "alpine:latest",
            &StoreHint::CreateAndRun(None),
            E_FAIL,
            &flood,
        );
        assert!(resp.error_message.contains('…'));
    }

    #[test]
    fn an_unreachable_registry_does_not_prescribe_the_pull_that_just_failed() {
        // `--setup-wslc` shares `pull_image`, so it fails the same way here.
        let resp = failure(
            "alpine:latest",
            &StoreHint::CreateAndRun(None),
            E_FAIL,
            "no such host",
        );
        let advice = &resp.error_message;
        let setup = advice
            .find("--setup-wslc")
            .expect("offers the warm-cache route");
        let elsewhere = advice
            .find("from a machine that can reach the registry")
            .expect("qualifies where that route has to run");
        assert!(
            elsewhere < setup,
            "the other-machine qualifier must precede the command: {advice}"
        );
    }

    #[test]
    fn an_overridden_store_travels_with_the_suggested_command() {
        let resp = failure(
            "alpine:latest",
            &StoreHint::CreateAndRun(Some(r"C:\store".to_string())),
            E_FAIL,
            "down",
        );
        assert!(resp.error_message.contains(r#"--storage-path "C:\store""#));
    }

    #[test]
    fn a_default_store_names_its_directory_without_a_path_argument() {
        let resp = failure(
            "alpine:latest",
            &StoreHint::CreateAndRun(None),
            E_FAIL,
            "down",
        );
        assert!(!resp.error_message.contains("--storage-path"));
        assert!(
            resp.error_message
                .contains(&StoreHint::CreateAndRun(None).store_path()),
            "a reader whose TEMP differs warms a different store unless the \
             default is named: {}",
            resp.error_message
        );
    }

    #[test]
    fn a_lifecycle_store_is_warmed_by_slot_rather_than_by_path() {
        let resp = failure("alpine:latest", &StoreHint::DaemonSlot(0), E_FAIL, "down");

        assert!(
            resp.error_message.contains("--wslc-daemon-slots 1"),
            "the daemon leases its own store, so --storage-path would warm the \
             wrong one: {}",
            resp.error_message
        );
        assert!(!resp.error_message.contains("--storage-path"));
    }

    #[test]
    fn an_uncached_image_without_egress_names_the_store_it_needs_warmed() {
        let create_and_run = no_egress_refusal("alpine:latest", &StoreHint::CreateAndRun(None));
        assert!(!create_and_run.error_message.contains("--wslc-daemon-slots"));

        let lifecycle = no_egress_refusal("alpine:latest", &StoreHint::DaemonSlot(0));

        assert!(
            lifecycle.error_message.contains("--wslc-daemon-slots 1"),
            "a lifecycle caller sent to warm the create-and-run store retries \
             against the same cold slot: {}",
            lifecycle.error_message
        );
    }

    #[test]
    fn an_isolated_provision_names_the_selected_cold_store_and_how_to_warm_it() {
        let refusal = no_egress_refusal("alpine:latest", &StoreHint::DaemonSlot(1));
        let message = &refusal.error_message;
        assert_eq!(
            refusal.failure_phase,
            crate::mxc_common::models::FailurePhase::Rejected
        );
        assert!(message.contains("daemon image store slot 1"));
        assert!(message.contains(store_slots::slot_path(1).to_string_lossy().as_ref()));
        assert!(message.contains("cold for image 'alpine:latest'"));
        assert!(message.contains("No container was created"));
        assert!(message.contains("--wslc-daemon-slots 2"));
        assert!(message.contains("wslc.provision.imageTarPath"));
    }

    #[test]
    fn a_higher_slot_asks_for_enough_slots_to_reach_it() {
        let instruction = StoreHint::DaemonSlot(2).warm_instruction();
        assert!(
            instruction.starts_with(" --wslc-daemon-slots 3"),
            "warming is counted from slot 0, so reaching slot 2 takes 3 slots: \
             {instruction}"
        );
    }

    #[test]
    fn a_slot_remediation_names_the_store_it_fills() {
        let slot = StoreHint::DaemonSlot(1);
        let instruction = slot.warm_instruction();

        assert!(
            instruction.contains(&slot.store_path()),
            "--wslc-daemon-slots re-resolves the pool from the environment of \
             whoever runs it, so a remediation that does not name the store can \
             warm a different one: {instruction}"
        );
    }

    #[test]
    fn a_busy_create_and_run_store_still_lets_the_slots_warm() {
        let targets = [
            StoreHint::CreateAndRun(None),
            StoreHint::DaemonSlot(0),
            StoreHint::DaemonSlot(1),
        ];
        let mut warmed = Vec::new();

        let outcome = warm_each(&targets, |target| match target {
            StoreHint::CreateAndRun(_) => Err("store is held".to_string()),
            StoreHint::DaemonSlot(index) => {
                warmed.push(*index);
                Ok(())
            }
        });

        assert_eq!(
            warmed,
            [0, 1],
            "the two stores are independent, so a held create-and-run store \
             must not leave a free lifecycle slot cold"
        );
        assert_eq!(
            outcome,
            Err("store is held".to_string()),
            "an unreachable store is still a failure, not a silent skip"
        );
    }

    #[test]
    fn every_store_that_failed_is_named() {
        let targets = [StoreHint::CreateAndRun(None), StoreHint::DaemonSlot(1)];

        let outcome = warm_each(&targets, |target| match target {
            StoreHint::CreateAndRun(_) => Err("temp store is held".to_string()),
            StoreHint::DaemonSlot(_) => Err("slot is held".to_string()),
        });

        assert_eq!(
            outcome,
            Err("temp store is held; daemon store slot 1: slot is held".to_string())
        );
    }

    #[test]
    fn warming_every_store_reports_success() {
        let targets = [StoreHint::CreateAndRun(None), StoreHint::DaemonSlot(0)];

        assert_eq!(warm_each(&targets, |_| Ok(())), Ok(()));
    }

    #[test]
    fn one_image_failing_still_pulls_the_rest() {
        let mut attempted = Vec::new();

        let outcome = pull_each(&["alpine:latest", "nope:1", "python:3"], |image| {
            attempted.push(image.to_string());
            if image == "nope:1" {
                PullStep::Continue(Err("not found".to_string()))
            } else {
                PullStep::Continue(Ok(()))
            }
        });

        assert_eq!(attempted, ["alpine:latest", "nope:1", "python:3"]);
        assert_eq!(outcome, Err("not found".to_string()));
    }

    #[test]
    fn an_abandoned_pull_stops_the_rest() {
        let mut attempted = Vec::new();

        let outcome = pull_each(&["alpine:latest", "slow:1", "python:3"], |image| {
            attempted.push(image.to_string());
            if image == "slow:1" {
                PullStep::Abandoned("gave up".to_string())
            } else {
                PullStep::Continue(Ok(()))
            }
        });

        assert_eq!(
            attempted,
            ["alpine:latest", "slow:1"],
            "an abandoned pull may still be writing into the session, so nothing \
             further may be pulled on it"
        );
        assert_eq!(outcome, Err("gave up".to_string()));
    }

    #[test]
    fn pulling_every_image_reports_success() {
        assert_eq!(
            pull_each(&["alpine:latest", "python:3"], |_| PullStep::Continue(Ok(
                ()
            ))),
            Ok(())
        );
    }

    #[test]
    fn every_rejection_names_a_way_forward() {
        for hr in [WSLC_E_IMAGE_NOT_FOUND, WSLC_E_REGISTRY_BLOCKED_BY_POLICY] {
            let resp = failure(
                "ghcr.io/nope:1",
                &StoreHint::CreateAndRun(None),
                hr,
                "denied",
            );
            assert!(
                resp.error_message.contains("imageTarPath"),
                "a rejection that only diagnoses leaves the caller stuck: {}",
                resp.error_message
            );
        }
    }

    #[test]
    fn a_failure_without_an_sdk_message_still_reports_the_code() {
        let resp = failure(
            "alpine:latest",
            &StoreHint::CreateAndRun(None),
            WSLC_E_IMAGE_NOT_FOUND,
            "",
        );
        assert!(resp.error_message.contains("0x80040601"));
    }

    /// Create a temporary tar file from in-memory entries and return its path.
    fn build_test_tar(entries: &[(&str, &[u8])]) -> tempfile::NamedTempFile {
        let file = tempfile::NamedTempFile::new().unwrap();
        let mut ar = tar::Builder::new(file.as_file());
        for (path, data) in entries {
            let mut header = tar::Header::new_gnu();
            header.set_size(data.len() as u64);
            header.set_cksum();
            ar.append_data(&mut header, path, *data).unwrap();
        }
        ar.into_inner().unwrap().flush().unwrap();
        file
    }

    #[test]
    fn detect_docker_save_tar() {
        let file = build_test_tar(&[("manifest.json", b"{}")]);
        let result = detect_tar_format(file.path().to_str().unwrap());
        assert!(matches!(result, Ok(TarFormat::DockerSave)));
    }

    #[test]
    fn detect_docker_save_tar_with_dot_prefix() {
        let file = build_test_tar(&[("./manifest.json", b"{}")]);
        let result = detect_tar_format(file.path().to_str().unwrap());
        assert!(matches!(result, Ok(TarFormat::DockerSave)));
    }

    #[test]
    fn detect_rootfs_tar() {
        let file = build_test_tar(&[("bin/sh", b""), ("etc/passwd", b"")]);
        let result = detect_tar_format(file.path().to_str().unwrap());
        assert!(matches!(result, Ok(TarFormat::Rootfs)));
    }

    #[test]
    fn detect_rootfs_tar_with_dot_prefix() {
        let file = build_test_tar(&[("./bin/sh", b""), ("./etc/passwd", b"")]);
        let result = detect_tar_format(file.path().to_str().unwrap());
        assert!(matches!(result, Ok(TarFormat::Rootfs)));
    }

    #[test]
    fn detect_unknown_tar() {
        let file = build_test_tar(&[("random/file.txt", b"hello")]);
        let result = detect_tar_format(file.path().to_str().unwrap());
        assert!(matches!(result, Ok(TarFormat::Unknown)));
    }

    #[test]
    fn detect_empty_tar() {
        let file = build_test_tar(&[]);
        let result = detect_tar_format(file.path().to_str().unwrap());
        assert!(matches!(result, Ok(TarFormat::Unknown)));
    }

    #[test]
    fn nested_manifest_json_is_not_docker_save() {
        let file = build_test_tar(&[("app/manifest.json", b"{}")]);
        let result = detect_tar_format(file.path().to_str().unwrap());
        assert!(!matches!(result, Ok(TarFormat::DockerSave)));
    }

    #[test]
    fn docker_save_takes_priority_over_rootfs_markers() {
        let file = build_test_tar(&[
            ("bin/sh", b""),
            ("etc/passwd", b""),
            ("manifest.json", b"{}"),
        ]);
        let result = detect_tar_format(file.path().to_str().unwrap());
        assert!(matches!(result, Ok(TarFormat::DockerSave)));
    }

    #[test]
    fn nonexistent_file_returns_error() {
        let result = detect_tar_format("/nonexistent/path.tar");
        assert!(result.is_err());
    }
}