mxc-sdk 1.0.1

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
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Owns the WSLc SDK session and containers on a single dedicated worker
//! thread.
//!
//! The WSLc SDK's `WslcSession` / `WslcContainer` / `WslcProcess` handles are
//! apartment-affine: any thread that has joined the MTA may use them. A single
//! long-lived worker thread owns the container map and every short SDK call;
//! async pipe handlers dispatch typed [`WorkerCommand`]s to it over a channel
//! and await the reply. An image pull and an `exec` both run for an unbounded
//! time, so each takes a thread of its own and posts its outcome back to the
//! worker as another command.
//!
//! State-aware topology (decided): **one** shared `WslcSession` (the WSL2
//! utility VM, booted lazily on first provision and amortised across all
//! sandboxes) and a refcounted `sandbox_id -> container` map. The session is
//! released when the last container is deprovisioned and the idle timeout
//! elapses. Its image store is leased from
//! [`mxc_sdk::wslc_common::store_slots`], which keeps the daemon off the store
//! create-and-run execution defaults to.
//!
//! Each phase drives the real WSLc SDK via the reusable steps in
//! [`mxc_sdk::wslc_common::container_steps`] and [`mxc_sdk::wslc_common::image`]: `provision`
//! ensures the session + resolves the image + creates a container with a
//! keepalive init process; `start` boots
//! it; `exec` runs a fresh `WslcCreateContainerProcess` to completion on a
//! thread of its own, streaming its stdout/stderr live to the pipe handler via
//! an [`OutputSink`]; `stop` / `deprovision` stop + delete. The completion reply
//! carries the exit code; output flows over the sink. (Client `Stdin` forwarding
//! is a later fill-in.)

use std::collections::{hash_map::Entry, HashMap};
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex, Weak};
use std::time::{Duration, Instant};

use anyhow::Result;
use tokio::sync::mpsc::error::TrySendError;
use tokio::sync::{mpsc, oneshot};

use mxc_sdk::mxc_common::logger::{Logger, Mode};
use mxc_sdk::mxc_common::models::{FailurePhase, ScriptResponse};
use mxc_sdk::wslc_common::container_steps::{self, OutStream, OutputSink, ProcessSettings};
use mxc_sdk::wslc_common::daemon_protocol::{
    DeprovisionConfig, ErrKind, ExecConfig, ExecTerminal, NetworkMode, PortMapping,
    ProvisionConfig, StartConfig, StopConfig,
};
use mxc_sdk::wslc_common::image;
use mxc_sdk::wslc_common::policy_mapping;
use mxc_sdk::wslc_common::process_env::EnvScope;
use mxc_sdk::wslc_common::store_slots::{self, StoreSlotError, StoreSlotLease, StoreSlotPool};
use mxc_sdk::wslc_common::wslc_bindings::{
    WslcContainer, WslcContainerGuard, WslcContainerNetworkingMode, WslcSdk, WslcSessionGuard,
};

/// Fixed name of the single WSL2 utility-VM session the daemon owns.
const SESSION_NAME: &str = "mxc-wslc-daemon";

/// How long teardown waits for an abandoned pull before giving up on it.
const PULL_DRAIN_TIMEOUT: Duration = Duration::from_secs(30);

/// How long teardown waits for an off-worker exec before giving up on it.
const EXEC_DRAIN_TIMEOUT: Duration = Duration::from_secs(30);

/// Block until `in_flight` reads zero, or `budget` expires.
///
/// Reports whether the wait drained. A run that has not reported back is still
/// using the container handle it was given, so releasing that handle while one
/// is outstanding would free memory the SDK holds.
fn wait_for_execs_in_flight(in_flight: &AtomicUsize, budget: Duration) -> bool {
    let deadline = Instant::now() + budget;
    while in_flight.load(Ordering::SeqCst) > 0 {
        if Instant::now() >= deadline {
            return false;
        }
        std::thread::sleep(Duration::from_millis(50));
    }
    true
}

/// Convert a step helper's `failure_phase` into a typed failure, since the wire
/// carries only a message and an [`ErrKind`].
fn sr_err(resp: ScriptResponse) -> WorkerError {
    let err = anyhow::anyhow!(resp.error_message);
    match resp.failure_phase {
        FailurePhase::BackendUnavailable => WorkerError::Unavailable(err),
        FailurePhase::Rejected => WorkerError::Rejected(err),
        _ => WorkerError::Backend(err),
    }
}

/// A typed worker failure. The control server maps [`WorkerError::kind`] onto
/// the protocol's [`ErrKind`] so clients can react (e.g. distinguish an unknown
/// sandbox from a backend fault) without string-matching the message.
#[derive(Debug)]
pub enum WorkerError {
    /// The referenced sandbox id is unknown to the daemon.
    NotProvisioned(String),

    /// The sandbox exists but has not been started.
    NotStarted(String),

    /// An exec already holds this container's single-flight slot.
    Busy(String),

    /// The host cannot run WSLc at all.
    Unavailable(anyhow::Error),

    /// The request cannot be honored as written.
    Rejected(anyhow::Error),

    /// A backend/SDK-level failure, or an internal worker/channel fault.
    Backend(anyhow::Error),
}

impl WorkerError {
    /// The protocol classification the control server returns for this error.
    pub fn kind(&self) -> ErrKind {
        match self {
            WorkerError::NotProvisioned(_) => ErrKind::NotProvisioned,
            WorkerError::NotStarted(_) => ErrKind::NotStarted,
            WorkerError::Busy(_) => ErrKind::Busy,
            WorkerError::Unavailable(_) => ErrKind::Unavailable,
            WorkerError::Rejected(_) => ErrKind::Rejected,
            WorkerError::Backend(_) => ErrKind::Backend,
        }
    }
}

impl std::fmt::Display for WorkerError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            WorkerError::NotProvisioned(id) => write!(f, "unknown sandbox {id}"),
            WorkerError::NotStarted(id) => write!(f, "sandbox {id} is not started"),
            WorkerError::Busy(id) => write!(f, "sandbox {id} already has an exec in flight"),
            WorkerError::Unavailable(e) | WorkerError::Rejected(e) | WorkerError::Backend(e) => {
                write!(f, "{e:#}")
            }
        }
    }
}

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

impl From<anyhow::Error> for WorkerError {
    fn from(e: anyhow::Error) -> Self {
        WorkerError::Backend(e)
    }
}

/// A unit of work dispatched from an async pipe handler to the WSLc worker
/// thread. Each variant carries a `oneshot` reply channel the worker fulfils.
pub enum WorkerCommand {
    Provision {
        config: ProvisionConfig,
        reply: oneshot::Sender<Result<String, WorkerError>>,
    },
    /// Anything addressed to one container. Routing every such command through
    /// a single variant is what forces it through [`Worker::dispatch`], so a new
    /// one cannot reach a container whose exec is still using its handle.
    Container(ContainerWork),
    /// Report a parked provision's pull, from the pull thread or its deadline.
    PullFinished {
        token: u64,
        outcome: Result<(), ScriptResponse>,
    },
    /// Report an off-worker exec, from its run thread.
    ExecFinished {
        sandbox_id: String,
        report: ExecReport,
    },
    /// Give up on a sandbox whose provision reply never reached a client.
    Retire {
        sandbox_id: String,
        reply: oneshot::Sender<()>,
    },
    /// Report the current live-container count (drives the idle watchdog).
    ContainerCount { reply: oneshot::Sender<usize> },
    /// Release all containers + the session and stop the worker thread.
    Shutdown { reply: oneshot::Sender<()> },
}

/// A resource an exec holds against the daemon's capacity, shared by the client
/// handler and the run so it is released only once both are done with it.
///
/// Opaque so the worker never names the control server's permit type.
pub type ExecSlotGuard = Arc<dyn Send + Sync>;

/// Everything an admitted exec needs, kept together so it travels as one value
/// from the pipe handler to the run thread.
pub struct ExecRequest {
    pub config: ExecConfig,

    /// Live-output sink the worker hands to `exec_in_container`, carrying the
    /// SDK's stdout/stderr chunks to the pipe handler as bytes arrive.
    pub sink: OutputSink,
    pub cancellation: Arc<AtomicBool>,
    pub(crate) registration: Arc<ExecRegistration>,
    pub admit: oneshot::Sender<Result<(), WorkerError>>,
    pub done: oneshot::Sender<Result<ExecTerminal, WorkerError>>,

    /// Counts this run against the daemon's exec capacity until it reports
    /// back, so a disconnecting client does not free the slot while its run
    /// thread and container process are still going.
    pub slot: Option<ExecSlotGuard>,
}

/// What an off-worker exec thread hands back, already classified.
///
/// Quarantine stays on the worker because it mutates the container map and
/// issues its own SDK delete, so an unconfirmed run reports the detail instead
/// of acting on it.
#[derive(Debug)]
pub enum ExecReport {
    Finished(Result<ExecTerminal, WorkerError>),
    Unconfirmed(String),
}

/// Everything an off-worker exec needs, in a form that can cross a thread.
struct ExecJob {
    sdk: *const WslcSdk,
    container: WslcContainer,
    config: ExecConfig,
    sink: OutputSink,
    cancellation: Arc<AtomicBool>,
}

// SAFETY: the SDK is apartment-affine, not thread-affine. Both the worker and
// the spawned thread join the MTA, where a handle may be used from any member
// thread. Measured with two containers in one session running `sleep 5` from two
// MTA threads: both processes started within 0.5ms of each other and overlapped
// for 5.1100s of a 5.1177s wall clock, both exited 0, and each process's output
// arrived only on its own capture buffer and live sink.
unsafe impl Send for ExecJob {}

/// Keeps the in-flight count accurate even if the run panics, so teardown
/// cannot be blocked forever by a thread that is already gone.
struct ExecCount(Arc<AtomicUsize>);

impl ExecCount {
    fn enter(counter: &Arc<AtomicUsize>) -> Self {
        counter.fetch_add(1, Ordering::SeqCst);
        Self(Arc::clone(counter))
    }
}

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

/// Run an exec on a thread of its own, reporting back to the worker.
///
/// Returns as soon as the thread starts. The caller keeps `sdk` loaded and the
/// container handle alive until [`WorkerCommand::ExecFinished`] lands -- see
/// [`wait_for_execs_in_flight`].
fn start_exec(
    job: ExecJob,
    worker: mpsc::UnboundedSender<WorkerCommand>,
    in_flight: &Arc<AtomicUsize>,
) -> Result<(), std::io::Error> {
    let counted = ExecCount::enter(in_flight);
    let spawned = std::thread::Builder::new()
        .name("wslc-exec".to_string())
        .spawn(move || {
            let job = job;

            // Releasing the count only after the report is queued is what lets a
            // teardown that sees zero rely on finding it.
            let _counted = counted;
            let sandbox_id = job.config.sandbox_id.clone();

            let run = std::panic::AssertUnwindSafe(|| match ComApartment::enter() {
                Err(e) => ExecReport::Finished(Err(WorkerError::Backend(e))),
                Ok(_apartment) => {
                    let mut log = Logger::new(Mode::Console);
                    run_exec(job, &mut log)
                }
            });

            // A panic leaves the process's fate unknown, which is what
            // `Unconfirmed` already means.
            let report = std::panic::catch_unwind(run).unwrap_or_else(|_| {
                ExecReport::Unconfirmed("the exec thread panicked".to_string())
            });

            let _ = worker.send(WorkerCommand::ExecFinished { sandbox_id, report });
        });

    // A spawn failure drops the guard along with the closure, so there is no
    // release to do here.
    spawned.map(|_| ())
}

/// The off-worker half of an exec: run it to completion and classify it.
fn run_exec(job: ExecJob, logger: &mut Logger) -> ExecReport {
    let ExecJob {
        sdk,
        container,
        config,
        sink,
        cancellation,
    } = job;

    // ProcessSettings::build expects `NAME=VALUE` env entries.
    let env: Vec<String> = config
        .env
        .iter()
        .map(|(k, v)| format!("{}={}", k, v))
        .collect();

    // SAFETY: `sdk` is valid and `container` is a live, started handle; the
    // worker releases neither while this run is still counted in flight.
    let outcome = unsafe {
        container_steps::exec_in_container(
            &*sdk,
            container,
            &config.script_code,
            &env,
            config.env_scope,
            &config.working_directory,
            config.timeout_ms,
            &cancellation,
            Some(sink),
            logger,
        )
    };

    let outcome = match outcome {
        Ok(outcome) => outcome,
        Err(e) => return ExecReport::Finished(Err(sr_err(e))),
    };

    match outcome.completion {
        container_steps::ProcessCompletion::TerminationUnconfirmed => ExecReport::Unconfirmed(
            outcome
                .post_launch_error
                .map(|error| error.error_message)
                .unwrap_or_else(|| "process termination could not be confirmed".to_string()),
        ),
        container_steps::ProcessCompletion::Confirmed(terminal) => {
            ExecReport::Finished(Ok(terminal))
        }
    }
}

/// A chunk of live process output streamed from the worker to the pipe handler:
/// which stream it came from and the bytes (owned, so it can cross the channel).
pub type OutputChunk = (OutStream, Vec<u8>);

/// Bound on the number of unconsumed live-output chunks buffered between the
/// SDK's I/O callback threads and the pipe handler. The channel is bounded (not
/// unbounded) so a container emitting output faster than a slow client drains it
/// cannot grow the queue without limit and OOM the persistent daemon, taking
/// down every sandbox it owns — the same hazard the capture-buffer cap guards
/// against. When the queue is full the sink does **not** block (see
/// [`enqueue_output`]): it latches a truncation flag and drops further bytes so
/// the SDK callback thread — which also delivers the process-exit callback — is
/// never parked. Stalling that thread could otherwise block exit delivery and
/// wedge teardown for every sandbox sharing the daemon.
pub(crate) const LIVE_OUTPUT_CHANNEL_CAPACITY: usize = 256;

/// Max bytes per enqueued live-output chunk. A single SDK callback can deliver
/// an arbitrarily large buffer; splitting it here bounds each queue entry's
/// allocation and keeps the resulting `Stdout`/`Stderr` frame well under the
/// protocol's `MAX_FRAME_SIZE` (a `Vec<u8>` serializes as a JSON number array,
/// ~4x expansion), so a large callback can never overflow a frame and abort the
/// stream before its terminal frame.
pub(crate) const LIVE_OUTPUT_MAX_CHUNK_BYTES: usize = 64 * 1024;

/// Enqueue an SDK output callback, splitting it into `LIVE_OUTPUT_MAX_CHUNK_BYTES`
/// pieces so each queue entry and its resulting frame stay bounded regardless of
/// the callback's buffer size.
///
/// This runs **synchronously on the SDK's I/O callback thread**, which also
/// delivers the process-exit callback. It must therefore never block: a
/// [`try_send`](mpsc::Sender::try_send) that finds the bounded queue full does
/// **not** apply backpressure (that would park this thread and could deadlock
/// exit delivery and teardown for every sandbox on the daemon). Instead it
/// latches `overflowed` and stops enqueuing; the pipe handler turns a latched
/// overflow into a terminal `Error` frame so the client sees an explicit
/// truncation rather than a silently short stream. Once latched, subsequent
/// callbacks return immediately so the client receives a clean truncated prefix
/// rather than a gapped stream. A closed receiver (client left / leak-path
/// `close()`) likewise stops enqueuing.
fn enqueue_output(
    tx: &mpsc::Sender<OutputChunk>,
    overflowed: &AtomicBool,
    kind: OutStream,
    bytes: &[u8],
) {
    if overflowed.load(Ordering::Relaxed) {
        return;
    }
    for chunk in bytes.chunks(LIVE_OUTPUT_MAX_CHUNK_BYTES) {
        match tx.try_send((kind, chunk.to_vec())) {
            Ok(()) => {}
            Err(TrySendError::Full(_)) => {
                overflowed.store(true, Ordering::Relaxed);
                return;
            }
            Err(TrySendError::Closed(_)) => return,
        }
    }
}

/// An admitted exec: the completion receiver (the run's exit code), the
/// live-output receiver the pipe handler drains into `Stdout`/`Stderr` frames,
/// and the `overflowed` latch the sink sets when it had to drop output because a
/// slow client let the bounded queue fill. The registration keeps the exec id
/// reserved until the pipe handler finishes terminal delivery. The pipe handler
/// reports a set overflow latch as a terminal `Error` frame.
#[derive(Debug)]
pub struct ExecStream {
    pub done: oneshot::Receiver<Result<ExecTerminal, WorkerError>>,
    pub output: mpsc::Receiver<OutputChunk>,
    pub overflowed: Arc<AtomicBool>,
    pub(crate) registration: Arc<ExecRegistration>,
}

pub(crate) type ActiveExecs = Arc<Mutex<HashMap<String, ActiveExec>>>;

#[derive(Debug)]
pub(crate) struct ActiveExec {
    run_token: String,
    cancellation: Weak<AtomicBool>,
}

#[derive(Debug)]
pub(crate) struct ExecRegistration {
    exec_id: String,
    run_token: String,
    active_execs: ActiveExecs,
    cancellation: Arc<AtomicBool>,
}

impl Drop for ExecRegistration {
    fn drop(&mut self) {
        let mut active_execs = self
            .active_execs
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        if active_execs.get(&self.exec_id).is_some_and(|current| {
            current.run_token == self.run_token
                && current
                    .cancellation
                    .ptr_eq(&Arc::downgrade(&self.cancellation))
        }) {
            active_execs.remove(&self.exec_id);
        }
    }
}

pub(crate) fn register_exec(
    active_execs: &ActiveExecs,
    exec_id: &str,
    run_token: &str,
    cancellation: &Arc<AtomicBool>,
) -> Result<ExecRegistration, WorkerError> {
    let active_exec = ActiveExec {
        run_token: run_token.to_string(),
        cancellation: Arc::downgrade(cancellation),
    };
    let mut active_execs_guard = active_execs
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner());
    match active_execs_guard.entry(exec_id.to_string()) {
        Entry::Occupied(mut entry) if entry.get().cancellation.upgrade().is_none() => {
            entry.insert(active_exec);
        }
        Entry::Occupied(_) => {
            return Err(WorkerError::Rejected(anyhow::anyhow!(
                "exec id {exec_id:?} is already active"
            )));
        }
        Entry::Vacant(entry) => {
            entry.insert(active_exec);
        }
    }
    drop(active_execs_guard);

    Ok(ExecRegistration {
        exec_id: exec_id.to_string(),
        run_token: run_token.to_string(),
        active_execs: Arc::clone(active_execs),
        cancellation: Arc::clone(cancellation),
    })
}

/// A cheap, clonable handle async tasks use to drive the worker thread.
#[derive(Clone)]
pub struct SessionHandle {
    tx: mpsc::UnboundedSender<WorkerCommand>,
    active_execs: ActiveExecs,
}

impl SessionHandle {
    /// The live-exec registry, so a test can observe what [`Self::cancel_exec`]
    /// reached.
    #[cfg(test)]
    pub(crate) fn active_execs(&self) -> &ActiveExecs {
        &self.active_execs
    }

    /// Provision a container, returning its minted `sandbox_id`.
    pub async fn provision(&self, config: ProvisionConfig) -> Result<String, WorkerError> {
        let (reply, rx) = oneshot::channel();
        self.send(WorkerCommand::Provision { config, reply })?;
        rx.await.map_err(worker_gone)?
    }

    /// Start a provisioned container.
    pub async fn start(&self, config: StartConfig) -> Result<(), WorkerError> {
        let (reply, rx) = oneshot::channel();
        self.send(WorkerCommand::Container(ContainerWork::Start {
            config,
            reply,
        }))?;
        rx.await.map_err(worker_gone)?
    }

    /// Admit and run a command in a started container. Awaits the worker's
    /// **admission** decision first: on rejection (unknown / not-started /
    /// already-busy sandbox) this returns the typed error *before* the caller
    /// writes any admission to the client. On admission it returns an
    /// [`ExecStream`] — the completion receiver (the run's exit code) plus the
    /// live-output receiver, which the caller drains into `Stdout`/`Stderr`
    /// frames as bytes arrive. Admission and
    /// the claim on the container are atomic on the worker thread, so no
    /// lifecycle command can invalidate the checked state before the run starts.
    pub async fn exec(
        &self,
        config: ExecConfig,
        slot: Option<ExecSlotGuard>,
    ) -> Result<ExecStream, WorkerError> {
        let (admit, admit_rx) = oneshot::channel();
        let (done, done_rx) = oneshot::channel();
        let (stream_tx, output) = mpsc::channel::<OutputChunk>(LIVE_OUTPUT_CHANNEL_CAPACITY);
        // The sink is invoked from the SDK's I/O callback threads (native OS
        // threads, never the async runtime). Those same threads deliver the
        // process-exit callback, so the sink must never block: [`enqueue_output`]
        // uses a non-blocking `try_send` and, on a full queue, latches
        // `overflowed` and drops further bytes instead of parking the thread.
        // Memory stays bounded by the channel capacity; a slow/non-reading
        // client can no longer wedge exit delivery or teardown for every sandbox
        // on the daemon. The pipe handler reports a set latch as a terminal
        // `Error` frame so truncation is explicit rather than silent.
        let overflowed = Arc::new(AtomicBool::new(false));
        let sink_overflowed = Arc::clone(&overflowed);
        let sink: OutputSink = Box::new(move |kind, bytes| {
            enqueue_output(&stream_tx, &sink_overflowed, kind, bytes);
        });
        let cancellation = Arc::new(AtomicBool::new(false));
        let registration = Arc::new(register_exec(
            &self.active_execs,
            &config.exec_id,
            &config.run_token,
            &cancellation,
        )?);
        self.send(WorkerCommand::Container(ContainerWork::Exec(ExecRequest {
            config,
            sink,
            cancellation,
            registration: Arc::clone(&registration),
            admit,
            done,
            slot,
        })))?;
        admit_rx.await.map_err(worker_gone)??;
        Ok(ExecStream {
            done: done_rx,
            output,
            overflowed,
            registration,
        })
    }

    /// Signal an admitted exec without waiting for the worker, which may not
    /// have reached the run yet.
    pub fn cancel_exec(&self, exec_id: &str, run_token: &str) {
        if let Some(cancellation) = self
            .active_execs
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .get(exec_id)
            .filter(|current| current.run_token == run_token)
            .and_then(|current| current.cancellation.upgrade())
        {
            cancellation.store(true, Ordering::Release);
        }
    }

    /// Stop a running container.
    pub async fn stop(&self, config: StopConfig) -> Result<(), WorkerError> {
        let (reply, rx) = oneshot::channel();
        self.send(WorkerCommand::Container(ContainerWork::Stop {
            config,
            reply,
        }))?;
        rx.await.map_err(worker_gone)?
    }

    /// Deprovision (delete) a container.
    pub async fn deprovision(&self, config: DeprovisionConfig) -> Result<(), WorkerError> {
        let (reply, rx) = oneshot::channel();
        self.send(WorkerCommand::Container(ContainerWork::Deprovision {
            config,
            reply,
        }))?;
        rx.await.map_err(worker_gone)?
    }

    /// Current number of live containers (0 means the daemon is idle).
    pub async fn container_count(&self) -> Result<usize, WorkerError> {
        let (reply, rx) = oneshot::channel();
        self.send(WorkerCommand::ContainerCount { reply })?;
        rx.await.map_err(worker_gone)
    }

    /// Stop counting a sandbox that no client can reach, so the idle watchdog
    /// can shut the daemon down.
    pub async fn retire(&self, sandbox_id: String) -> Result<(), WorkerError> {
        let (reply, rx) = oneshot::channel();
        self.send(WorkerCommand::Retire { sandbox_id, reply })?;
        rx.await.map_err(worker_gone)
    }

    /// Ask the worker to release everything and stop. Awaits confirmation.
    pub async fn shutdown(&self) -> Result<(), WorkerError> {
        let (reply, rx) = oneshot::channel();
        self.send(WorkerCommand::Shutdown { reply })?;
        rx.await.map_err(worker_gone)
    }

    fn send(&self, cmd: WorkerCommand) -> Result<(), WorkerError> {
        self.tx
            .send(cmd)
            .map_err(|_| WorkerError::Backend(anyhow::anyhow!("WSLc worker thread is gone")))
    }
}

fn worker_gone(_e: oneshot::error::RecvError) -> WorkerError {
    WorkerError::Backend(anyhow::anyhow!("WSLc worker dropped the reply channel"))
}

/// Per-container bookkeeping held by the worker: whether the container is
/// currently started and the live `WslcContainer` handle (kept alive across
/// phases so repeated `exec`s hit a warm container). The sandbox id is the
/// map key.
struct ContainerEntry {
    started: bool,
    quarantined: bool,

    /// Set when a sandbox's provision reply never reached a client, so nobody
    /// can deprovision it.
    ///
    /// Kept in the map rather than dropped so [`Worker::shutdown`] still stops
    /// and deletes the container.
    retired: bool,
    container: WslcContainerGuard,

    /// Keeps a quarantined sandbox counted against exec capacity, because a run
    /// whose termination was never confirmed may still hold a live process.
    exec_slot: Option<ExecSlotGuard>,
}

/// A provision waiting on its image to arrive from a registry.
struct PendingProvision {
    config: ProvisionConfig,
    reply: oneshot::Sender<Result<String, WorkerError>>,

    /// Held only so dropping this entry retires the deadline thread.
    _retire_deadline: std::sync::mpsc::Sender<()>,
}

/// A command addressed to one container, either about to run or parked behind
/// an exec that is still using that container's handle.
pub(crate) enum ContainerWork {
    /// Validate the sandbox (exists + started) and, if admitted, hand the run to
    /// a thread of its own. The two replies make admission **atomic** with the
    /// claim on the container: the worker validates, claims the container's
    /// in-flight slot, answers `admit` and starts the run thread without
    /// yielding. A later lifecycle command naming that container parks behind
    /// the claim and a later `Exec` is refused with `Busy`, so none can
    /// interleave with the run. `admit` carries the pre-run decision (so an
    /// unknown, not-started or already-busy sandbox is a pre-admission typed
    /// error, never a post-admission stream `Error`); `done` carries the run's
    /// exit code once [`WorkerCommand::ExecFinished`] lands.
    Exec(ExecRequest),
    Start {
        config: StartConfig,
        reply: oneshot::Sender<Result<(), WorkerError>>,
    },
    Stop {
        config: StopConfig,
        reply: oneshot::Sender<Result<(), WorkerError>>,
    },
    Deprovision {
        config: DeprovisionConfig,
        reply: oneshot::Sender<Result<(), WorkerError>>,
    },
}

impl ContainerWork {
    fn sandbox_id(&self) -> &str {
        match self {
            ContainerWork::Exec(request) => &request.config.sandbox_id,
            ContainerWork::Start { config, .. } => &config.sandbox_id,
            ContainerWork::Stop { config, .. } => &config.sandbox_id,
            ContainerWork::Deprovision { config, .. } => &config.sandbox_id,
        }
    }

    /// Answer this work with `message`, for a daemon that is shutting down.
    fn refuse(self, message: &str) {
        let error = WorkerError::Backend(anyhow::anyhow!("{message}"));
        match self {
            ContainerWork::Exec(request) => {
                let _ = request.admit.send(Err(error));
            }
            ContainerWork::Start { reply, .. }
            | ContainerWork::Stop { reply, .. }
            | ContainerWork::Deprovision { reply, .. } => {
                let _ = reply.send(Err(error));
            }
        }
    }
}

/// How [`Worker::begin_exec`] puts a claimed run on its own thread, injected so
/// a worker with no SDK loaded can still reach the claim.
type RunStarter = fn(
    &Worker,
    ExecConfig,
    WslcContainer,
    OutputSink,
    Arc<AtomicBool>,
    &mpsc::UnboundedSender<WorkerCommand>,
) -> Result<(), WorkerError>;

/// Hand a claimed run to its own MTA thread using the SDK this worker loaded.
fn start_run_on_sdk(
    state: &Worker,
    config: ExecConfig,
    container: WslcContainer,
    sink: OutputSink,
    cancellation: Arc<AtomicBool>,
    worker: &mpsc::UnboundedSender<WorkerCommand>,
) -> Result<(), WorkerError> {
    let Some(sdk) = state.sdk.as_ref() else {
        return Err(WorkerError::Backend(anyhow::anyhow!(
            "no active WSLc session"
        )));
    };

    let sandbox_id = config.sandbox_id.clone();
    let job = ExecJob {
        // The box's contents, not the field: teardown may take the field while
        // this run is still dereferencing the SDK.
        sdk: &**sdk as *const WslcSdk,
        container,
        config,
        sink,
        cancellation,
    };

    start_exec(job, worker.clone(), &state.execs_in_flight).map_err(|e| {
        WorkerError::Backend(anyhow::anyhow!(
            "could not start a thread to run exec on sandbox {sandbox_id}: {e}"
        ))
    })
}

/// An exec running on its own thread, and the work that parked behind it.
///
/// `container` is the handle that run is using, so nothing may delete or
/// release it until [`Worker::finish_exec`] removes this entry.
struct InFlightExec {
    container: WslcContainer,
    done: oneshot::Sender<Result<ExecTerminal, WorkerError>>,

    /// Held only so the exec id stays reserved for as long as the run lasts.
    _registration: Arc<ExecRegistration>,

    /// Held only so the run counts against exec capacity until it reports back.
    slot: Option<ExecSlotGuard>,
    parked: Vec<ContainerWork>,
}

/// The single-threaded WSLc session owner. Constructed and run entirely on the
/// worker thread. Holds the lazily-loaded SDK and the one shared session (the
/// WSL2 utility VM), plus the `sandbox_id -> container` map. The SDK/session/
/// guard handles are `!Send` raw pointers; ownership stays here, and a pull or
/// exec thread borrows one only for as long as its own in-flight counter says
/// so.
struct Worker {
    logger: Logger,
    start_run: RunStarter,

    // Field order is load-bearing on implicit drop: `containers` and `session`
    // hold handles whose Drop calls into the SDK, so they must drop before `sdk`
    // unloads `wslcsdk.dll`.
    containers: HashMap<String, ContainerEntry>,
    pending: HashMap<u64, PendingProvision>,
    exec_in_flight: HashMap<String, InFlightExec>,

    /// Runs this worker started that have not reported back. Shared with the run
    /// threads, which outlive this struct whenever teardown abandons a handle.
    execs_in_flight: Arc<AtomicUsize>,
    next_pull_token: u64,
    session: Option<WslcSessionGuard>,

    /// The store the live session holds, returned to the pool when the session
    /// is released. Teardown that leaks the session leaks this with it, since
    /// the leaked session keeps the store's VHDX mounted.
    store_lease: Option<StoreSlotLease>,
    store_slots: StoreSlotPool,

    /// Boxed so the address an off-worker pull or exec was handed survives this
    /// struct being moved or the field being taken, which teardown does to leak
    /// the SDK rather than unload it under a running thread.
    sdk: Option<Box<WslcSdk>>,
}

impl Worker {
    fn new() -> Self {
        Self {
            logger: Logger::new(Mode::Console),
            start_run: start_run_on_sdk,
            sdk: None,
            session: None,
            store_lease: None,
            store_slots: StoreSlotPool::new(),
            containers: HashMap::new(),
            pending: HashMap::new(),
            exec_in_flight: HashMap::new(),
            execs_in_flight: Arc::new(AtomicUsize::new(0)),
            next_pull_token: 0,
        }
    }

    /// Lazily load the SDK and boot the shared session on first use. Idempotent.
    fn ensure_session(&mut self) -> Result<(), WorkerError> {
        if self.sdk.is_none() {
            // SAFETY: the worker thread is already in the MTA (see `ComApartment`).
            let sdk =
                unsafe { container_steps::load_sdk_checked(&mut self.logger) }.map_err(sr_err)?;
            self.sdk = Some(Box::new(sdk));
        }
        if self.session.is_none() {
            let Self {
                store_slots,
                sdk,
                logger,
                ..
            } = self;
            let sdk = sdk.as_ref().expect("sdk loaded above");

            let (lease, session) = store_slots
                .lease_with(|store| {
                    // SAFETY: `sdk` holds valid pointers and COM is initialised.
                    unsafe {
                        container_steps::create_daemon_session(
                            sdk,
                            SESSION_NAME,
                            &store.to_string_lossy(),
                            logger,
                        )
                    }
                })
                .map_err(|e| match e {
                    StoreSlotError::Refused { source, .. } => sr_err(source),
                    pool_failure => WorkerError::Backend(anyhow::anyhow!("{pool_failure}")),
                })?;

            self.session = Some(session);
            self.store_lease = Some(lease);
        }
        Ok(())
    }

    /// Start a provision, parking it if the image has to be pulled.
    ///
    /// Returning without replying is what keeps this thread free: the pull runs
    /// elsewhere and posts [`WorkerCommand::PullFinished`] when it lands, so
    /// every other sandbox keeps being served while a registry is slow.
    fn begin_provision(
        &mut self,
        config: ProvisionConfig,
        reply: oneshot::Sender<Result<String, WorkerError>>,
        worker: &mpsc::UnboundedSender<WorkerCommand>,
    ) {
        if let Err(e) = self.ensure_session() {
            let _ = reply.send(Err(e));
            return;
        }

        let sdk = self.sdk.as_ref().expect("session ensured");
        let session = self.session.as_ref().expect("session ensured").as_raw();
        let store = image::StoreHint::DaemonSlot(
            self.store_lease
                .as_ref()
                .map_or(0, store_slots::StoreSlotLease::index),
        );

        // SAFETY: `sdk`/`session` are valid.
        let step = unsafe {
            image::begin_resolve(
                sdk,
                session,
                &config.image,
                config.image_tar_path.as_deref(),
                &store,
                match config.network {
                    NetworkMode::None => image::RegistryAccess::Denied,
                    NetworkMode::Bridged => image::RegistryAccess::Allowed,
                },
                "[WSLC][daemon]",
                &mut self.logger,
            )
        };
        let step = match step {
            Ok(step) => step,
            Err(e) => {
                let _ = reply.send(Err(sr_err(e)));
                return;
            }
        };

        match step {
            image::ImageStep::Ready => {
                let _ = reply.send(self.create_provisioned_container(&config));
            }
            image::ImageStep::Pull => {
                let token = self.next_pull_token;
                self.next_pull_token += 1;

                let done = worker.clone();
                let started = unsafe {
                    image::start_pull(
                        sdk,
                        session,
                        &config.image,
                        &store,
                        "[WSLC][daemon]",
                        move |outcome| {
                            let _ = done.send(WorkerCommand::PullFinished { token, outcome });
                        },
                    )
                };
                if let Err(e) = started {
                    let _ = reply.send(Err(sr_err(e)));
                    return;
                }

                // Nothing can stop a stalled pull from outside, so the deadline
                // is enforced by giving up on it rather than by ending it.
                let budget = image::daemon_pull_budget();
                let image_name = config.image.clone();
                let expired = worker.clone();
                let (retire_deadline, retired) = std::sync::mpsc::channel::<()>();
                let armed = std::thread::Builder::new()
                    .name("wslc-pull-deadline".to_string())
                    .spawn(move || {
                        // A pull that lands first drops the sender, so this
                        // returns rather than sleeping out the budget.
                        if retired.recv_timeout(budget)
                            != Err(std::sync::mpsc::RecvTimeoutError::Timeout)
                        {
                            return;
                        }
                        let _ = expired.send(WorkerCommand::PullFinished {
                            token,
                            outcome: Err(image::pull_deadline_expired(&image_name, budget)),
                        });
                    });

                if armed.is_err() {
                    // Parking without a deadline is how a provision waits
                    // forever, so refuse instead. The pull carries on and warms
                    // the cache for a retry.
                    let _ = reply.send(Err(WorkerError::Backend(anyhow::anyhow!(
                        "could not arm the pull deadline for image '{}'",
                        config.image
                    ))));
                    return;
                }

                self.pending.insert(
                    token,
                    PendingProvision {
                        config,
                        reply,
                        _retire_deadline: retire_deadline,
                    },
                );
            }
        }
    }

    /// Resume a parked provision once its pull reported, or its deadline did.
    ///
    /// An unknown token is the loser of that race, and has nothing left to do.
    fn finish_provision(&mut self, token: u64, outcome: Result<(), ScriptResponse>) {
        let Some(PendingProvision {
            config,
            reply,
            _retire_deadline: _,
        }) = self.pending.remove(&token)
        else {
            return;
        };

        if let Err(e) = outcome {
            let _ = reply.send(Err(sr_err(e)));
            return;
        }

        let sdk = self.sdk.as_ref().expect("session ensured");
        let session = self.session.as_ref().expect("session ensured").as_raw();

        // SAFETY: `sdk`/`session` are valid.
        if let Err(e) = unsafe {
            image::report_pulled_digest(
                sdk,
                session,
                &config.image,
                "[WSLC][daemon]",
                &mut self.logger,
            )
        } {
            let _ = reply.send(Err(sr_err(e)));
            return;
        }

        let _ = reply.send(self.create_provisioned_container(&config));
    }

    /// Everything after the image is in the store: create the container and
    /// register it under a fresh sandbox id.
    fn create_provisioned_container(
        &mut self,
        config: &ProvisionConfig,
    ) -> Result<String, WorkerError> {
        let sdk = self.sdk.as_ref().expect("session ensured");
        let session = self.session.as_ref().expect("session ensured").as_raw();

        let mounts: Vec<policy_mapping::VolumeMount> = config
            .volumes
            .iter()
            .map(|v| policy_mapping::VolumeMount {
                windows_path: v.host.clone(),
                container_path: v.container.clone(),
                read_only: v.read_only,
            })
            .collect();
        let net_mode = match config.network {
            NetworkMode::None => WslcContainerNetworkingMode::WSLC_CONTAINER_NETWORKING_MODE_NONE,
            NetworkMode::Bridged => {
                WslcContainerNetworkingMode::WSLC_CONTAINER_NETWORKING_MODE_BRIDGED
            }
        };
        let port_mappings = to_model_port_mappings(&config.port_mappings);

        // SAFETY: `sdk`/`session` are valid; every buffer the SDK stores pointers
        // into is owned by a stationary local (`keepalive`) until create returns.
        let container = unsafe {
            // Merge keeps the keepalive out of `env -i`, so a container whose
            // execs never replace an environment does not need `/usr/bin/env`
            // in its image.
            let mut keepalive = ProcessSettings::build_detached(
                sdk,
                container_steps::KEEPALIVE_SCRIPT,
                &[],
                EnvScope::Merge,
                "",
            )
            .map_err(sr_err)?;

            container_steps::create_daemon_container(
                sdk,
                session,
                &config.image,
                &mounts,
                &port_mappings,
                net_mode,
                &mut keepalive,
                &mut self.logger,
            )
            .map_err(sr_err)?
        };

        let sandbox_id = format!("wslc:{}", uuid::Uuid::new_v4().simple());
        self.containers.insert(
            sandbox_id.clone(),
            ContainerEntry {
                started: false,
                quarantined: false,
                retired: false,
                container,
                exec_slot: None,
            },
        );
        Ok(sandbox_id)
    }

    fn start(&mut self, config: StartConfig) -> Result<(), WorkerError> {
        // Existence check first, so an unknown sandbox errors without needing the
        // SDK (keeps the no-WSL unit tests self-contained).
        if !self.containers.contains_key(&config.sandbox_id) {
            return Err(WorkerError::NotProvisioned(config.sandbox_id));
        }
        let sdk = self
            .sdk
            .as_ref()
            .ok_or_else(|| anyhow::anyhow!("no active WSLc session"))?;
        let entry = self
            .containers
            .get_mut(&config.sandbox_id)
            .expect("checked above");
        if entry.quarantined {
            return Err(WorkerError::Backend(anyhow::anyhow!(
                "sandbox {} is quarantined after an exec whose termination could not be \
                 confirmed; deprovision it before reuse",
                config.sandbox_id
            )));
        }
        // SAFETY: `sdk` is valid and `entry.container` is a live handle.
        unsafe {
            container_steps::start_daemon_container(sdk, entry.container.as_raw(), &mut self.logger)
        }
        .map_err(sr_err)?;
        entry.started = true;
        Ok(())
    }

    /// Validate that a sandbox exists and is started, returning the live handle
    /// needed to run. Sole owner of the exists+started invariant: [`begin_exec`]
    /// trusts the handle it is given and never re-checks, because the worker
    /// validates and claims the container's in-flight slot without yielding, and
    /// no later command naming that container can release the handle while the
    /// claim stands.
    ///
    /// [`begin_exec`]: Worker::begin_exec
    fn validate_exec(&self, sandbox_id: &str) -> Result<WslcContainer, WorkerError> {
        match self.containers.get(sandbox_id) {
            None => Err(WorkerError::NotProvisioned(sandbox_id.to_string())),
            Some(entry) if entry.quarantined => Err(WorkerError::Backend(anyhow::anyhow!(
                "sandbox {sandbox_id} is quarantined after an exec whose termination could not \
                 be confirmed; deprovision it before reuse"
            ))),
            Some(entry) if !entry.started => Err(WorkerError::NotStarted(sandbox_id.to_string())),
            Some(entry) => Ok(entry.container.as_raw()),
        }
    }

    /// Run one container command, refusing a second exec on a container that
    /// already has one and parking lifecycle work behind the run instead.
    fn dispatch(&mut self, work: ContainerWork, worker: &mpsc::UnboundedSender<WorkerCommand>) {
        if let Some(in_flight) = self.exec_in_flight.get_mut(work.sandbox_id()) {
            match work {
                ContainerWork::Exec(request) => {
                    let ExecRequest { config, admit, .. } = request;
                    let _ = admit.send(Err(WorkerError::Busy(config.sandbox_id)));
                }
                lifecycle => in_flight.parked.push(lifecycle),
            }
            return;
        }

        match work {
            ContainerWork::Exec(request) => self.begin_exec(request, worker),
            ContainerWork::Start { config, reply } => {
                let _ = reply.send(self.start(config));
            }
            ContainerWork::Stop { config, reply } => {
                let _ = reply.send(self.stop(config));
            }
            ContainerWork::Deprovision { config, reply } => {
                let _ = reply.send(self.deprovision(config));
            }
        }
    }

    /// Admit an exec and hand the run to a thread of its own.
    ///
    /// Validation, the admission reply, the claim on the container and the
    /// spawn all happen here without yielding, so nothing can delete the
    /// validated handle before the run thread takes it.
    fn begin_exec(&mut self, request: ExecRequest, worker: &mpsc::UnboundedSender<WorkerCommand>) {
        let ExecRequest {
            config,
            sink,
            cancellation,
            registration,
            admit,
            done,
            slot,
        } = request;

        let container = match self.validate_exec(&config.sandbox_id) {
            Ok(container) => container,
            Err(e) => {
                let _ = admit.send(Err(e));
                return;
            }
        };

        if cancellation.load(Ordering::Acquire) {
            if admit.send(Ok(())).is_ok() {
                let _ = done.send(Ok(ExecTerminal::Cancelled));
            }
            return;
        }

        // Starting a run the client handler already abandoned would hold the
        // container's slot for the full timeout with nobody left to read it.
        if admit.send(Ok(())).is_err() {
            return;
        }

        let sandbox_id = config.sandbox_id.clone();
        let start_run = self.start_run;
        if let Err(e) = start_run(self, config, container, sink, cancellation, worker) {
            let _ = done.send(Err(e));
            return;
        }

        self.exec_in_flight.insert(
            sandbox_id,
            InFlightExec {
                container,
                done,
                _registration: registration,
                slot,
                parked: Vec::new(),
            },
        );
    }

    /// Retire a finished exec: answer its client, then release the work that
    /// parked behind it.
    fn finish_exec(
        &mut self,
        sandbox_id: &str,
        report: ExecReport,
        worker: &mpsc::UnboundedSender<WorkerCommand>,
    ) {
        for work in self.retire_exec(sandbox_id, report) {
            self.dispatch(work, worker);
        }
    }

    /// Answer a finished exec and hand back whatever parked behind it.
    ///
    /// Separate from [`Worker::finish_exec`] so teardown can answer the client
    /// without releasing parked work into a daemon that is shutting down.
    fn retire_exec(&mut self, sandbox_id: &str, report: ExecReport) -> Vec<ContainerWork> {
        let Some(in_flight) = self.exec_in_flight.remove(sandbox_id) else {
            return Vec::new();
        };

        let outcome = match report {
            ExecReport::Finished(outcome) => outcome,
            ExecReport::Unconfirmed(detail) => {
                Err(self.quarantine(sandbox_id, in_flight.container, &detail, in_flight.slot))
            }
        };

        if let Err(orphaned) = in_flight.done.send(outcome) {
            // The client handler is gone (e.g. its post-admission Ok write
            // failed) but the run already happened. Record the result so a
            // completed exec is never silently lost.
            self.logger.log_line(&format!(
                "exec on {sandbox_id} completed after the client disconnected; \
                 orphaned result: {orphaned:?}"
            ));
        }

        in_flight.parked
    }

    fn quarantine(
        &mut self,
        sandbox_id: &str,
        container: WslcContainer,
        detail: &str,
        slot: Option<ExecSlotGuard>,
    ) -> WorkerError {
        let delete_result = self.sdk.as_ref().map(|sdk| {
            // SAFETY: `sdk` is valid and `container` is the live handle stored
            // for `sandbox_id`.
            unsafe { container_steps::delete_daemon_container(sdk, container, &mut self.logger) }
        });

        match delete_result {
            Some(Ok(())) => {
                // Deleting the container ends anything still running inside it,
                // so this exec stops counting against capacity.
                drop(slot);
                self.containers.remove(sandbox_id);
                WorkerError::Backend(anyhow::anyhow!(
                    "exec on sandbox {sandbox_id} could not be confirmed terminated ({detail}); \
                     the container was quarantined and deleted"
                ))
            }
            Some(Err(delete_error)) => {
                if let Some(entry) = self.containers.get_mut(sandbox_id) {
                    entry.quarantined = true;
                    entry.exec_slot = slot;
                }
                WorkerError::Backend(anyhow::anyhow!(
                    "exec on sandbox {sandbox_id} could not be confirmed terminated ({detail}); \
                     quarantine deletion failed and the sandbox remains blocked until \
                     deprovision succeeds: {}",
                    delete_error.error_message
                ))
            }
            None => {
                if let Some(entry) = self.containers.get_mut(sandbox_id) {
                    entry.quarantined = true;
                    entry.exec_slot = slot;
                }
                WorkerError::Backend(anyhow::anyhow!(
                    "exec on sandbox {sandbox_id} could not be confirmed terminated ({detail}); \
                     the sandbox remains quarantined because no active WSLc SDK is available"
                ))
            }
        }
    }

    fn stop(&mut self, config: StopConfig) -> Result<(), WorkerError> {
        if !self.containers.contains_key(&config.sandbox_id) {
            return Err(WorkerError::NotProvisioned(config.sandbox_id));
        }
        let sdk = self
            .sdk
            .as_ref()
            .ok_or_else(|| anyhow::anyhow!("no active WSLc session"))?;
        let entry = self
            .containers
            .get_mut(&config.sandbox_id)
            .expect("checked above");
        // SAFETY: `sdk` is valid and `entry.container` is a live handle.
        unsafe {
            container_steps::stop_daemon_container(sdk, entry.container.as_raw(), &mut self.logger)
        }
        .map_err(sr_err)?;
        entry.started = false;
        Ok(())
    }

    /// Stop counting a sandbox that no client can reach.
    fn retire(&mut self, sandbox_id: &str) {
        if let Some(entry) = self.containers.get_mut(sandbox_id) {
            entry.retired = true;
        }
    }

    /// Containers a client could still act on.
    ///
    /// Drives the idle watchdog, which shuts the daemon down only at zero. A
    /// parked provision has no container yet, so counting it keeps the daemon
    /// alive for the client still waiting on its pull.
    fn live_container_count(&self) -> usize {
        self.containers.values().filter(|e| !e.retired).count() + self.pending.len()
    }

    fn deprovision(&mut self, config: DeprovisionConfig) -> Result<(), WorkerError> {
        let container_raw = match self.containers.get(&config.sandbox_id) {
            Some(e) => e.container.as_raw(),
            None => return Err(WorkerError::NotProvisioned(config.sandbox_id)),
        };

        if let Some(sdk) = self.sdk.as_ref() {
            // SAFETY: `sdk` is valid and `container_raw` is a live handle.
            unsafe {
                container_steps::delete_daemon_container(sdk, container_raw, &mut self.logger)
            }
            .map_err(sr_err)?;
        }

        // Delete succeeded (or no SDK loaded): drop the handle now. Keeping the
        // entry on failure above leaves it retryable.
        self.containers.remove(&config.sandbox_id);

        // The shared session (and SDK) stay loaded so a subsequent provision
        // reuses the already-booted WSL2 VM. The idle watchdog releases them via
        // `shutdown` once the container count has stayed at zero for the idle
        // timeout.
        Ok(())
    }

    /// Give up the SDK handles rather than release them, when an off-worker
    /// thread may still be using one.
    ///
    /// Reached only on an unwind, where [`Worker::shutdown`]'s drain never ran.
    fn abandon_if_borrowed(mut self, execs_in_flight: usize, pull_outstanding: bool) {
        if execs_in_flight > 0 || pull_outstanding {
            std::mem::forget(std::mem::take(&mut self.containers));
            std::mem::forget(self.session.take());
            std::mem::forget(self.store_lease.take());
            std::mem::forget(self.sdk.take());
        }

        // Only the handles are abandoned, so the reply channels close as the
        // bookkeeping drops and a waiting client observes the worker is gone.
    }

    /// Answer a command pulled off the queue during teardown.
    ///
    /// [`WorkerCommand::ExecFinished`] is the one the drain is looking for and
    /// is handled by its caller.
    fn refuse_while_shutting_down(&self, cmd: WorkerCommand) {
        const SHUTTING_DOWN: &str = "the WSLc daemon is shutting down";

        match cmd {
            WorkerCommand::Provision { reply, .. } => {
                let _ = reply.send(Err(WorkerError::Backend(anyhow::anyhow!(
                    "{SHUTTING_DOWN}"
                ))));
            }
            WorkerCommand::Container(work) => work.refuse(SHUTTING_DOWN),
            WorkerCommand::Retire { reply, .. } => {
                let _ = reply.send(());
            }
            WorkerCommand::ContainerCount { reply } => {
                let _ = reply.send(self.live_container_count());
            }
            WorkerCommand::Shutdown { reply } => {
                let _ = reply.send(());
            }

            // The provision this would resume was already answered above.
            WorkerCommand::PullFinished { .. } => {}
            WorkerCommand::ExecFinished { .. } => {}
        }
    }

    /// Release every container and the session.
    ///
    /// A parked provision is answered rather than dropped, since a dropped
    /// reply reaches its client as a bare "worker gone".
    fn shutdown(
        &mut self,
        rx: &mut mpsc::UnboundedReceiver<WorkerCommand>,
        drain_budget: Duration,
    ) {
        for (_, pending) in self.pending.drain() {
            let _ = pending.reply.send(Err(WorkerError::Backend(anyhow::anyhow!(
                "the WSLc daemon shut down while pulling image '{}'",
                pending.config.image
            ))));
        }

        // An off-worker exec is still using its container handle, so nothing
        // below may stop, delete or release one until the runs report back.
        let execs_drained = wait_for_execs_in_flight(&self.execs_in_flight, drain_budget);

        if execs_drained {
            // Every drained run queued its report before releasing its count, so
            // the reports are all in the channel now. Collecting them is what
            // lets a finished exec keep its own exit code instead of the
            // shutdown error below. Anything else found along the way is
            // answered rather than dropped, since a dropped reply reaches its
            // client as a bare "worker gone".
            while !self.exec_in_flight.is_empty() {
                let Ok(cmd) = rx.try_recv() else {
                    break;
                };
                match cmd {
                    WorkerCommand::ExecFinished { sandbox_id, report } => {
                        for work in self.retire_exec(&sandbox_id, report) {
                            work.refuse(&format!(
                                "the WSLc daemon shut down before sandbox {sandbox_id} was free"
                            ));
                        }
                    }
                    other => self.refuse_while_shutting_down(other),
                }
            }
        }

        for (sandbox_id, in_flight) in self.exec_in_flight.drain() {
            let _ = in_flight
                .done
                .send(Err(WorkerError::Backend(anyhow::anyhow!(
                    "the WSLc daemon shut down while running an exec on sandbox {sandbox_id}"
                ))));
            for work in in_flight.parked {
                work.refuse(&format!(
                    "the WSLc daemon shut down before sandbox {sandbox_id} was free"
                ));
            }
        }

        if !execs_drained {
            self.logger.log_line(
                "a WSLC exec is still running; leaking the session rather than \
                 releasing handles it is using",
            );
            std::mem::forget(std::mem::take(&mut self.containers));
            std::mem::forget(self.session.take());
            std::mem::forget(self.store_lease.take());
            std::mem::forget(self.sdk.take());
            return;
        }

        if let Some(sdk) = self.sdk.as_ref() {
            for (_, entry) in self.containers.drain() {
                // SAFETY: `sdk` is valid and `entry.container` is a live handle.
                unsafe {
                    if entry.started {
                        let _ = container_steps::stop_daemon_container(
                            sdk,
                            entry.container.as_raw(),
                            &mut self.logger,
                        );
                    }
                    let _ = container_steps::delete_daemon_container(
                        sdk,
                        entry.container.as_raw(),
                        &mut self.logger,
                    );
                }
                // entry (WslcContainerGuard) drops here, releasing the handle.
            }
        } else {
            self.containers.clear();
        }

        // An abandoned pull is still using this session, so releasing it now
        // would free memory the SDK holds. The leaked handles cost one
        // process's worth of memory until it exits.
        if !image::wait_for_pulls_in_flight(PULL_DRAIN_TIMEOUT) {
            self.logger.log_line(
                "a WSLC image pull is still running; leaking the session rather than \
                 releasing a handle it is using",
            );
            std::mem::forget(self.session.take());
            std::mem::forget(self.store_lease.take());
            std::mem::forget(self.sdk.take());
            return;
        }

        // Session guard drops before the SDK unloads the DLL.
        self.session = None;
        self.store_lease = None;
        self.sdk = None;
    }
}

/// Spawn the WSLc worker thread and return a handle to it.
///
/// The thread joins the MTA for its entire lifetime (WSLc SDK apartment
/// affinity) and processes [`WorkerCommand`]s until a [`WorkerCommand::Shutdown`]
/// is received or the command channel closes.
pub fn spawn() -> Result<SessionHandle> {
    let (tx, mut rx) = mpsc::unbounded_channel::<WorkerCommand>();
    let active_execs = Arc::new(Mutex::new(HashMap::new()));

    // A parked provision or a finished exec resumes by posting back into this
    // queue.
    let worker_tx = tx.clone();

    std::thread::Builder::new()
        .name("wslc-session-worker".to_string())
        .spawn(move || {
            #[cfg(windows)]
            let _com = match ComApartment::enter() {
                Ok(com) => com,
                Err(e) => {
                    eprintln!("[wslc-daemon] worker COM initialisation failed: {e:#}");
                    return;
                }
            };

            let mut worker = Worker::new();

            // A panic here would otherwise drop the container handles an exec
            // thread is still using, so the unwind is caught and the handles
            // are abandoned instead.
            let served = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                while let Some(cmd) = rx.blocking_recv() {
                    match cmd {
                        WorkerCommand::Provision { config, reply } => {
                            worker.begin_provision(config, reply, &worker_tx);
                        }
                        WorkerCommand::PullFinished { token, outcome } => {
                            worker.finish_provision(token, outcome);
                        }
                        WorkerCommand::Container(work) => {
                            worker.dispatch(work, &worker_tx);
                        }
                        WorkerCommand::ExecFinished { sandbox_id, report } => {
                            worker.finish_exec(&sandbox_id, report, &worker_tx);
                        }
                        WorkerCommand::Retire { sandbox_id, reply } => {
                            worker.retire(&sandbox_id);
                            let _ = reply.send(());
                        }
                        WorkerCommand::ContainerCount { reply } => {
                            let _ = reply.send(worker.live_container_count());
                        }
                        WorkerCommand::Shutdown { reply } => {
                            worker.shutdown(&mut rx, EXEC_DRAIN_TIMEOUT);
                            let _ = reply.send(());
                            break;
                        }
                    }
                }
            }));

            if served.is_err() {
                let in_flight = worker.execs_in_flight.load(Ordering::SeqCst);

                // A pull borrows the SDK and session the same way a run does.
                let pull_outstanding = !image::wait_for_pulls_in_flight(Duration::ZERO);
                worker.abandon_if_borrowed(in_flight, pull_outstanding);
            }
        })
        .map_err(|e| anyhow::anyhow!("spawn WSLc worker thread: {e}"))?;

    Ok(SessionHandle { tx, active_execs })
}

/// RAII guard that keeps the calling thread in the COM MTA for the WSLc SDK.
#[cfg(windows)]
struct ComApartment;

#[cfg(windows)]
impl ComApartment {
    fn enter() -> Result<Self> {
        use windows::Win32::System::Com::{CoInitializeEx, COINIT_MULTITHREADED};
        // SAFETY: called once at worker-thread startup. On success the matching
        // `CoUninitialize` runs in `Drop`; on failure no guard is produced, so
        // `CoUninitialize` is never called for an apartment we did not enter.
        let hr = unsafe { CoInitializeEx(None, COINIT_MULTITHREADED) };
        if hr.is_err() {
            anyhow::bail!("CoInitializeEx(MTA) failed: {hr:?}");
        }
        Ok(Self)
    }
}

#[cfg(windows)]
impl Drop for ComApartment {
    fn drop(&mut self) {
        use windows::Win32::System::Com::CoUninitialize;
        // SAFETY: balances the CoInitializeEx in `enter`, on the same thread.
        unsafe {
            CoUninitialize();
        }
    }
}

/// The daemon IPC frame carries no protocol, so every mapping is rebuilt as TCP.
fn to_model_port_mappings(
    mappings: &[PortMapping],
) -> Vec<mxc_sdk::mxc_common::models::PortMapping> {
    mappings
        .iter()
        .map(|mapping| mxc_sdk::mxc_common::models::PortMapping {
            windows_port: mapping.windows_port,
            container_port: mapping.container_port,
            protocol: "tcp".to_string(),
        })
        .collect()
}

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

    async fn count(handle: &SessionHandle) -> usize {
        handle.container_count().await.unwrap()
    }

    // ---- No-WSL unit tests (run everywhere, never touch the SDK) ----

    #[test]
    fn port_mappings_convert_to_tcp_models_in_order() {
        let converted = to_model_port_mappings(&[
            PortMapping {
                windows_port: 8080,
                container_port: 80,
            },
            PortMapping {
                windows_port: 8443,
                container_port: 443,
            },
        ]);

        assert_eq!(
            converted,
            vec![
                mxc_sdk::mxc_common::models::PortMapping {
                    windows_port: 8080,
                    container_port: 80,
                    protocol: "tcp".to_string(),
                },
                mxc_sdk::mxc_common::models::PortMapping {
                    windows_port: 8443,
                    container_port: 443,
                    protocol: "tcp".to_string(),
                },
            ]
        );
    }

    #[test]
    fn no_port_mappings_convert_to_an_empty_list() {
        assert!(to_model_port_mappings(&[]).is_empty());
    }

    /// Build a worker entry around a handle that is never dereferenced.
    fn test_entry(retired: bool) -> ContainerEntry {
        unsafe extern "C" fn release_noop(
            _: mxc_sdk::wslc_common::wslc_bindings::WslcContainer,
        ) -> i32 {
            0
        }

        // Non-null only because the guard rejects null; the value is never used
        // as a pointer.
        let sentinel = std::ptr::dangling_mut();
        ContainerEntry {
            started: false,
            quarantined: false,
            retired,
            // SAFETY: `release_noop` never dereferences the handle, so the guard
            // owns a value it can release without touching memory.
            container: unsafe { WslcContainerGuard::from_raw(sentinel, release_noop) },
            exec_slot: None,
        }
    }

    /// A parked provision has no container yet, so the watchdog would retire
    /// the daemon out from under the client still waiting on its pull.
    /// A timer that outlived its pull would sit for the rest of the budget, so
    /// sequential cache misses would pile up sleeping threads.
    #[test]
    fn a_pull_that_lands_first_retires_its_deadline_thread() {
        let (retire_deadline, retired) = std::sync::mpsc::channel::<()>();
        let woke = std::thread::spawn(move || {
            retired.recv_timeout(Duration::from_secs(30))
                == Err(std::sync::mpsc::RecvTimeoutError::Disconnected)
        });

        drop(retire_deadline);

        assert!(
            woke.join().expect("timer thread"),
            "dropping the parked provision must wake its deadline immediately"
        );
    }

    #[test]
    fn a_pending_pull_keeps_the_daemon_alive() {
        let mut worker = Worker::new();
        assert_eq!(worker.live_container_count(), 0);

        let (reply, _rx) = oneshot::channel();
        worker.pending.insert(
            7,
            PendingProvision {
                config: ProvisionConfig {
                    image: "alpine:latest".to_string(),
                    image_tar_path: None,
                    volumes: Vec::new(),
                    network: Default::default(),
                    port_mappings: Vec::new(),
                },
                reply,
                _retire_deadline: std::sync::mpsc::channel().0,
            },
        );

        assert_eq!(
            worker.live_container_count(),
            1,
            "a provision still waiting on its image must hold the daemon open"
        );
    }

    /// The pull and its deadline race, and both report.
    #[test]
    fn only_the_first_report_of_a_pull_answers_the_client() {
        let mut worker = Worker::new();
        let (reply, mut rx) = oneshot::channel();
        worker.pending.insert(
            3,
            PendingProvision {
                config: ProvisionConfig {
                    image: "alpine:latest".to_string(),
                    image_tar_path: None,
                    volumes: Vec::new(),
                    network: Default::default(),
                    port_mappings: Vec::new(),
                },
                reply,
                _retire_deadline: std::sync::mpsc::channel().0,
            },
        );

        worker.finish_provision(
            3,
            Err(image::pull_deadline_expired(
                "alpine:latest",
                PULL_DRAIN_TIMEOUT,
            )),
        );
        assert!(rx.try_recv().is_ok(), "the first report answers the client");

        // The loser of the race: nothing left to answer, and no panic.
        worker.finish_provision(3, Ok(()));
        assert_eq!(worker.live_container_count(), 0);
    }

    #[test]
    fn a_retired_sandbox_stops_holding_the_daemon_open() {
        let mut worker = Worker::new();
        worker
            .containers
            .insert("wslc:orphan".to_string(), test_entry(false));
        assert_eq!(worker.live_container_count(), 1);

        worker.retire("wslc:orphan");

        assert_eq!(
            worker.live_container_count(),
            0,
            "a retired sandbox must not keep the daemon alive"
        );
        assert!(
            worker.containers.contains_key("wslc:orphan"),
            "the handle stays so shutdown can still delete the container"
        );
    }

    #[test]
    fn retiring_one_sandbox_leaves_the_others_counted() {
        let mut worker = Worker::new();
        worker
            .containers
            .insert("wslc:orphan".to_string(), test_entry(false));
        worker
            .containers
            .insert("wslc:live".to_string(), test_entry(false));

        worker.retire("wslc:orphan");

        assert_eq!(worker.live_container_count(), 1);
    }

    /// Retirement is driven by a failed release, which can name a sandbox that
    /// is already gone.
    #[test]
    fn retiring_an_unknown_sandbox_is_a_no_op() {
        let mut worker = Worker::new();
        worker.retire("wslc:never-existed");
        assert_eq!(worker.live_container_count(), 0);
    }

    #[test]
    fn sr_err_carries_the_step_failure_phase() {
        for (phase, expected) in [
            (FailurePhase::BackendUnavailable, ErrKind::Unavailable),
            (FailurePhase::Rejected, ErrKind::Rejected),
            (FailurePhase::LaunchFailed, ErrKind::Backend),
            (FailurePhase::PostLaunchFailed, ErrKind::Backend),
            (FailurePhase::None, ErrKind::Backend),
        ] {
            let resp = ScriptResponse {
                exit_code: -1,
                error_message: "boom".to_string(),
                failure_phase: phase.clone(),
                ..Default::default()
            };
            assert_eq!(sr_err(resp).kind(), expected, "phase {phase:?}");
        }
    }

    #[test]
    fn sr_err_preserves_the_step_message() {
        let resp = ScriptResponse {
            exit_code: -1,
            error_message: "WSLc components are missing".to_string(),
            failure_phase: FailurePhase::BackendUnavailable,
            ..Default::default()
        };
        assert_eq!(sr_err(resp).to_string(), "WSLc components are missing");
    }

    #[tokio::test]
    async fn start_unknown_sandbox_errors() {
        let handle = spawn().unwrap();
        let err = handle
            .start(StartConfig {
                sandbox_id: "wslc:does-not-exist".to_string(),
            })
            .await
            .unwrap_err();
        assert!(err.to_string().contains("unknown sandbox"));
        handle.shutdown().await.unwrap();
    }

    #[tokio::test]
    async fn exec_unknown_sandbox_errors() {
        let handle = spawn().unwrap();
        let err = handle
            .exec(
                ExecConfig {
                    exec_id: "unknown-1".to_string(),
                    run_token: "run-unknown-1".to_string(),
                    sandbox_id: "wslc:does-not-exist".to_string(),
                    script_code: "echo hi".to_string(),
                    working_directory: String::new(),
                    env: Vec::new(),
                    env_scope: EnvScope::Merge,
                    timeout_ms: 0,
                },
                None,
            )
            .await
            .unwrap_err();
        assert!(err.to_string().contains("unknown sandbox"));
        handle.shutdown().await.unwrap();
    }

    #[tokio::test]
    async fn stop_unknown_sandbox_errors() {
        let handle = spawn().unwrap();
        let err = handle
            .stop(StopConfig {
                sandbox_id: "wslc:does-not-exist".to_string(),
            })
            .await
            .unwrap_err();
        assert!(err.to_string().contains("unknown sandbox"));
        handle.shutdown().await.unwrap();
    }

    #[tokio::test]
    async fn deprovision_unknown_sandbox_errors() {
        let handle = spawn().unwrap();
        let err = handle
            .deprovision(DeprovisionConfig {
                sandbox_id: "wslc:does-not-exist".to_string(),
            })
            .await
            .unwrap_err();
        assert!(err.to_string().contains("unknown sandbox"));
        assert_eq!(count(&handle).await, 0);
        handle.shutdown().await.unwrap();
    }

    #[tokio::test]
    async fn unknown_sandbox_maps_to_not_provisioned_kind() {
        let handle = spawn().unwrap();
        let err = handle
            .start(StartConfig {
                sandbox_id: "wslc:does-not-exist".to_string(),
            })
            .await
            .unwrap_err();
        assert_eq!(err.kind(), ErrKind::NotProvisioned);
        handle.shutdown().await.unwrap();
    }

    #[tokio::test]
    async fn exec_unknown_sandbox_admission_is_not_provisioned() {
        let handle = spawn().unwrap();
        let err = handle
            .exec(
                ExecConfig {
                    exec_id: "unknown-2".to_string(),
                    run_token: "run-unknown-2".to_string(),
                    sandbox_id: "wslc:does-not-exist".to_string(),
                    script_code: "echo hi".to_string(),
                    working_directory: String::new(),
                    env: Vec::new(),
                    env_scope: EnvScope::Merge,
                    timeout_ms: 0,
                },
                None,
            )
            .await
            .unwrap_err();
        assert_eq!(err.kind(), ErrKind::NotProvisioned);
        handle.shutdown().await.unwrap();
    }

    #[test]
    fn cancel_exec_marks_the_registered_run() {
        let (tx, _rx) = mpsc::unbounded_channel();
        let cancellation = Arc::new(AtomicBool::new(false));
        let active_execs = Arc::new(Mutex::new(HashMap::from([(
            "exec-1".to_string(),
            ActiveExec {
                run_token: "run-1".to_string(),
                cancellation: Arc::downgrade(&cancellation),
            },
        )])));
        let handle = SessionHandle { tx, active_execs };

        handle.cancel_exec("exec-1", "run-1");

        assert!(cancellation.load(Ordering::Acquire));
    }

    #[test]
    fn delayed_cancellation_does_not_target_reused_exec_id() {
        let (tx, _rx) = mpsc::unbounded_channel();
        let cancellation = Arc::new(AtomicBool::new(false));
        let active_execs = Arc::new(Mutex::new(HashMap::new()));
        let registration =
            register_exec(&active_execs, "exec-1", "replacement-run", &cancellation).unwrap();
        let handle = SessionHandle { tx, active_execs };

        handle.cancel_exec("exec-1", "completed-run");
        assert!(!cancellation.load(Ordering::Acquire));

        handle.cancel_exec("exec-1", "replacement-run");
        assert!(cancellation.load(Ordering::Acquire));
        drop(registration);
    }

    #[test]
    fn duplicate_live_exec_id_is_rejected_without_replacing_registration() {
        let active_execs = Arc::new(Mutex::new(HashMap::new()));
        let first = Arc::new(AtomicBool::new(false));
        let second = Arc::new(AtomicBool::new(false));
        let registration = register_exec(&active_execs, "exec-1", "run-1", &first).unwrap();

        let error = register_exec(&active_execs, "exec-1", "run-2", &second).unwrap_err();

        assert_eq!(error.kind(), ErrKind::Rejected);
        let registered = active_execs
            .lock()
            .unwrap()
            .get("exec-1")
            .and_then(|entry| entry.cancellation.upgrade())
            .unwrap();
        assert!(Arc::ptr_eq(&registered, &first));
        drop(registration);
    }

    #[test]
    fn stale_exec_id_can_be_registered_again() {
        let stale = Arc::new(AtomicBool::new(false));
        let active_execs = Arc::new(Mutex::new(HashMap::from([(
            "exec-1".to_string(),
            ActiveExec {
                run_token: "stale-run".to_string(),
                cancellation: Arc::downgrade(&stale),
            },
        )])));
        drop(stale);
        let cancellation = Arc::new(AtomicBool::new(false));

        let registration =
            register_exec(&active_execs, "exec-1", "replacement-run", &cancellation).unwrap();

        let registered = active_execs
            .lock()
            .unwrap()
            .get("exec-1")
            .and_then(|entry| entry.cancellation.upgrade())
            .unwrap();
        assert!(Arc::ptr_eq(&registered, &cancellation));
        drop(registration);
        assert!(!active_execs.lock().unwrap().contains_key("exec-1"));
    }

    #[test]
    fn older_registration_does_not_remove_replacement() {
        let active_execs = Arc::new(Mutex::new(HashMap::new()));
        let first = Arc::new(AtomicBool::new(false));
        let first_registration = register_exec(&active_execs, "exec-1", "run-1", &first).unwrap();
        let second = Arc::new(AtomicBool::new(false));
        active_execs.lock().unwrap().insert(
            "exec-1".to_string(),
            ActiveExec {
                run_token: "run-2".to_string(),
                cancellation: Arc::downgrade(&second),
            },
        );

        drop(first_registration);

        let registered = active_execs
            .lock()
            .unwrap()
            .get("exec-1")
            .and_then(|entry| entry.cancellation.upgrade())
            .unwrap();
        assert!(Arc::ptr_eq(&registered, &second));
    }

    #[test]
    fn cancelling_unknown_or_stale_exec_id_is_a_no_op() {
        let (tx, _rx) = mpsc::unbounded_channel();
        let stale = Arc::new(AtomicBool::new(false));
        let active_execs = Arc::new(Mutex::new(HashMap::from([(
            "stale".to_string(),
            ActiveExec {
                run_token: "stale-run".to_string(),
                cancellation: Arc::downgrade(&stale),
            },
        )])));
        drop(stale);
        let handle = SessionHandle { tx, active_execs };

        handle.cancel_exec("unknown", "unknown-run");
        handle.cancel_exec("stale", "stale-run");
    }

    #[tokio::test]
    async fn large_callback_split_into_frame_safe_chunks() {
        use mxc_sdk::wslc_common::daemon_protocol::{encode_frame, StreamFrame, MAX_FRAME_SIZE};

        let (tx, mut rx) = mpsc::channel::<OutputChunk>(LIVE_OUTPUT_CHANNEL_CAPACITY);
        // A single callback far larger than one chunk (and, unsplit, larger than
        // one frame once number-array-encoded): must arrive as many bounded chunks.
        let payload = vec![b'x'; LIVE_OUTPUT_MAX_CHUNK_BYTES * 3 + 7];
        let producer = tokio::task::spawn_blocking({
            let payload = payload.clone();
            move || {
                let overflowed = AtomicBool::new(false);
                enqueue_output(&tx, &overflowed, OutStream::Stdout, &payload)
            }
        });

        let mut reassembled = Vec::new();
        let mut chunks = 0usize;
        while let Some((kind, data)) = rx.recv().await {
            assert_eq!(kind, OutStream::Stdout);
            assert!(data.len() <= LIVE_OUTPUT_MAX_CHUNK_BYTES);
            let encoded = encode_frame(&StreamFrame::Stdout { data: data.clone() }).unwrap();
            assert!(encoded.len() <= MAX_FRAME_SIZE);
            reassembled.extend_from_slice(&data);
            chunks += 1;
        }
        producer.await.unwrap();
        assert_eq!(reassembled, payload);
        assert!(
            chunks >= 4,
            "expected the callback to be split, got {chunks}"
        );
    }

    /// A started container, so [`Worker::validate_exec`] admits an exec on it.
    fn started_entry() -> ContainerEntry {
        ContainerEntry {
            started: true,
            ..test_entry(false)
        }
    }

    /// A container whose handle release is observable, so a test can tell a
    /// deliberate leak from a normal drop.
    fn flagged_entry(
        release: unsafe extern "C" fn(mxc_sdk::wslc_common::wslc_bindings::WslcContainer) -> i32,
    ) -> ContainerEntry {
        let sentinel = std::ptr::dangling_mut();
        ContainerEntry {
            started: false,
            quarantined: false,
            retired: false,

            // SAFETY: `release` never dereferences the handle, so the guard owns
            // a value it can release without touching memory.
            container: unsafe { WslcContainerGuard::from_raw(sentinel, release) },
            exec_slot: None,
        }
    }

    /// An exec occupying a container's in-flight slot, with the completion
    /// receiver a test uses to observe what the worker answers.
    fn in_flight_exec(
        exec_id: &str,
    ) -> (
        InFlightExec,
        oneshot::Receiver<Result<ExecTerminal, WorkerError>>,
    ) {
        let (done, done_rx) = oneshot::channel();
        let active_execs: ActiveExecs = Arc::new(Mutex::new(HashMap::new()));
        let cancellation = Arc::new(AtomicBool::new(false));
        let registration =
            Arc::new(register_exec(&active_execs, exec_id, "run", &cancellation).unwrap());
        (
            InFlightExec {
                container: std::ptr::dangling_mut(),
                done,
                _registration: registration,
                slot: None,
                parked: Vec::new(),
            },
            done_rx,
        )
    }

    /// An exec command and the handles a test needs to observe it.
    struct TestExec {
        work: ContainerWork,
        admit: oneshot::Receiver<Result<(), WorkerError>>,
        done: oneshot::Receiver<Result<ExecTerminal, WorkerError>>,
        cancellation: Arc<AtomicBool>,
    }

    fn exec_work(sandbox_id: &str, exec_id: &str) -> TestExec {
        exec_work_with_slot(sandbox_id, exec_id, None)
    }

    fn exec_work_with_slot(
        sandbox_id: &str,
        exec_id: &str,
        slot: Option<ExecSlotGuard>,
    ) -> TestExec {
        let (admit, admit_rx) = oneshot::channel();
        let (done, done_rx) = oneshot::channel();
        let active_execs: ActiveExecs = Arc::new(Mutex::new(HashMap::new()));
        let cancellation = Arc::new(AtomicBool::new(false));
        let registration =
            Arc::new(register_exec(&active_execs, exec_id, "run", &cancellation).unwrap());
        let sink: OutputSink = Box::new(|_, _| {});
        TestExec {
            work: ContainerWork::Exec(ExecRequest {
                config: ExecConfig {
                    exec_id: exec_id.to_string(),
                    run_token: "run".to_string(),
                    sandbox_id: sandbox_id.to_string(),
                    script_code: "echo hi".to_string(),
                    working_directory: String::new(),
                    env: Vec::new(),
                    env_scope: EnvScope::Merge,
                    timeout_ms: 0,
                },
                sink,
                cancellation: Arc::clone(&cancellation),
                registration,
                admit,
                done,
                slot,
            }),
            admit: admit_rx,
            done: done_rx,
            cancellation,
        }
    }

    /// A worker holding one started container with an exec already in flight.
    fn worker_with_exec_in_flight() -> (Worker, oneshot::Receiver<Result<ExecTerminal, WorkerError>>)
    {
        let mut worker = Worker::new();
        worker
            .containers
            .insert("wslc:busy".to_string(), started_entry());
        let (in_flight, done) = in_flight_exec("running");
        worker
            .exec_in_flight
            .insert("wslc:busy".to_string(), in_flight);
        (worker, done)
    }

    /// A worker holding one started container it can start runs on without an
    /// SDK loaded.
    fn worker_that_can_run() -> Worker {
        fn start_nothing(
            _: &Worker,
            _: ExecConfig,
            _: WslcContainer,
            _: OutputSink,
            _: Arc<AtomicBool>,
            _: &mpsc::UnboundedSender<WorkerCommand>,
        ) -> Result<(), WorkerError> {
            Ok(())
        }

        let mut worker = Worker::new();
        worker.start_run = start_nothing;
        worker
            .containers
            .insert("wslc:ready".to_string(), started_entry());
        worker
    }

    fn start_work(sandbox_id: &str) -> (ContainerWork, oneshot::Receiver<Result<(), WorkerError>>) {
        let (reply, reply_rx) = oneshot::channel();
        (
            ContainerWork::Start {
                config: StartConfig {
                    sandbox_id: sandbox_id.to_string(),
                },
                reply,
            },
            reply_rx,
        )
    }

    fn stop_work(sandbox_id: &str) -> (ContainerWork, oneshot::Receiver<Result<(), WorkerError>>) {
        let (reply, reply_rx) = oneshot::channel();
        (
            ContainerWork::Stop {
                config: StopConfig {
                    sandbox_id: sandbox_id.to_string(),
                },
                reply,
            },
            reply_rx,
        )
    }

    fn deprovision_work(
        sandbox_id: &str,
    ) -> (ContainerWork, oneshot::Receiver<Result<(), WorkerError>>) {
        let (reply, reply_rx) = oneshot::channel();
        (
            ContainerWork::Deprovision {
                config: DeprovisionConfig {
                    sandbox_id: sandbox_id.to_string(),
                },
                reply,
            },
            reply_rx,
        )
    }

    /// Deleting the container would free the handle the run thread is using, so
    /// lifecycle work waits instead of being refused.
    #[test]
    fn lifecycle_work_parks_behind_an_exec_rather_than_being_refused() {
        let (tx, _rx) = mpsc::unbounded_channel();
        let (mut worker, _done) = worker_with_exec_in_flight();
        let (start, mut start_reply) = start_work("wslc:busy");
        let (stop, mut stop_reply) = stop_work("wslc:busy");
        let (deprovision, mut deprovision_reply) = deprovision_work("wslc:busy");

        worker.dispatch(start, &tx);
        worker.dispatch(stop, &tx);
        worker.dispatch(deprovision, &tx);

        assert!(start_reply.try_recv().is_err());
        assert!(stop_reply.try_recv().is_err());
        assert!(deprovision_reply.try_recv().is_err());
        assert_eq!(worker.exec_in_flight["wslc:busy"].parked.len(), 3);
    }

    #[test]
    fn deprovision_parks_behind_an_exec_using_the_same_container() {
        let (tx, _rx) = mpsc::unbounded_channel();
        let (mut worker, _done) = worker_with_exec_in_flight();
        let (work, mut reply) = deprovision_work("wslc:busy");

        worker.dispatch(work, &tx);

        assert!(
            reply.try_recv().is_err(),
            "a deprovision must not run while an exec holds the container"
        );
        assert!(
            worker.containers.contains_key("wslc:busy"),
            "the container handle must survive until the run reports back"
        );
        assert_eq!(worker.exec_in_flight["wslc:busy"].parked.len(), 1);
    }

    #[test]
    fn an_exec_does_not_park_work_for_another_container() {
        let (tx, _rx) = mpsc::unbounded_channel();
        let (mut worker, _done) = worker_with_exec_in_flight();
        worker
            .containers
            .insert("wslc:idle".to_string(), test_entry(false));
        let (work, mut reply) = deprovision_work("wslc:idle");

        worker.dispatch(work, &tx);

        assert!(
            reply.try_recv().expect("idle container answered").is_ok(),
            "a command for an idle container must not wait on another container's exec"
        );
        assert!(!worker.containers.contains_key("wslc:idle"));
    }

    #[test]
    fn finishing_an_exec_releases_the_work_parked_behind_it() {
        let (tx, _rx) = mpsc::unbounded_channel();
        let (mut worker, mut done) = worker_with_exec_in_flight();
        let (work, mut reply) = deprovision_work("wslc:busy");
        worker.dispatch(work, &tx);

        worker.finish_exec(
            "wslc:busy",
            ExecReport::Finished(Ok(ExecTerminal::Exited(0))),
            &tx,
        );

        assert_eq!(done.try_recv().unwrap().unwrap(), ExecTerminal::Exited(0));
        assert!(reply
            .try_recv()
            .expect("the parked deprovision must be answered")
            .is_ok());
        assert!(!worker.containers.contains_key("wslc:busy"));
    }

    /// A disconnected client leaves its run going, so the slot it claimed must
    /// travel to the worker and stay claimed until that run reports back.
    #[tokio::test]
    async fn an_exec_slot_outlives_the_client_that_admitted_it() {
        let (tx, mut rx) = mpsc::unbounded_channel();
        let handle = SessionHandle {
            tx,
            active_execs: Arc::new(Mutex::new(HashMap::new())),
        };
        let limiter = Arc::new(tokio::sync::Semaphore::new(1));
        let permit = Arc::clone(&limiter).try_acquire_owned().unwrap();

        let exec = tokio::spawn(async move {
            handle
                .exec(
                    ExecConfig {
                        exec_id: "slotted".to_string(),
                        run_token: "slotted-run".to_string(),
                        sandbox_id: "wslc:busy".to_string(),
                        script_code: "echo hi".to_string(),
                        working_directory: String::new(),
                        env: Vec::new(),
                        env_scope: EnvScope::Merge,
                        timeout_ms: 0,
                    },
                    Some(Arc::new(permit)),
                )
                .await
        });

        let Some(WorkerCommand::Container(ContainerWork::Exec(request))) = rx.recv().await else {
            panic!("the exec never reached the worker");
        };
        assert!(
            request.slot.is_some(),
            "the admitting client's exec slot must reach the worker"
        );

        // Stand in for the worker: claim the slot for the run, then drop the
        // client's end as a disconnect would.
        let (mut worker, _done) = worker_with_exec_in_flight();
        worker.exec_in_flight.get_mut("wslc:busy").unwrap().slot = request.slot;
        let _ = request.admit.send(Ok(()));
        exec.await.unwrap().unwrap();
        assert_eq!(
            limiter.available_permits(),
            0,
            "a client that disconnected mid-run must not free its exec slot"
        );

        let (worker_tx, _worker_rx) = mpsc::unbounded_channel();
        worker.finish_exec(
            "wslc:busy",
            ExecReport::Finished(Ok(ExecTerminal::Exited(0))),
            &worker_tx,
        );

        assert_eq!(
            limiter.available_permits(),
            1,
            "the slot must be released once the run reports back"
        );
    }

    #[test]
    fn the_worker_claims_the_slot_when_it_starts_the_run() {
        let (tx, _rx) = mpsc::unbounded_channel();
        let mut worker = worker_that_can_run();
        let limiter = Arc::new(tokio::sync::Semaphore::new(1));
        let permit = Arc::clone(&limiter).try_acquire_owned().unwrap();
        let exec = exec_work_with_slot("wslc:ready", "claimed", Some(Arc::new(permit)));

        worker.dispatch(exec.work, &tx);

        assert_eq!(
            limiter.available_permits(),
            0,
            "the started run must hold the slot its request carried"
        );

        worker.finish_exec(
            "wslc:ready",
            ExecReport::Finished(Ok(ExecTerminal::Exited(0))),
            &tx,
        );
        assert_eq!(
            limiter.available_permits(),
            1,
            "retiring the run must release the slot"
        );
    }

    #[test]
    fn parked_lifecycle_work_replays_in_the_order_it_arrived() {
        let (tx, _rx) = mpsc::unbounded_channel();
        let (mut worker, _done) = worker_with_exec_in_flight();
        let (stop, mut stop_reply) = stop_work("wslc:busy");
        let (deprovision, mut deprovision_reply) = deprovision_work("wslc:busy");
        worker.dispatch(stop, &tx);
        worker.dispatch(deprovision, &tx);

        worker.finish_exec(
            "wslc:busy",
            ExecReport::Finished(Ok(ExecTerminal::Exited(0))),
            &tx,
        );

        let stopped = stop_reply
            .try_recv()
            .expect("the parked stop must be answered");
        assert!(
            !matches!(stopped, Err(WorkerError::NotProvisioned(_))),
            "the stop must run while its container still exists, got {stopped:?}"
        );
        assert!(deprovision_reply
            .try_recv()
            .expect("the parked deprovision must be answered")
            .is_ok());
    }

    #[test]
    fn a_second_exec_on_the_same_container_is_refused_as_busy() {
        let (tx, _rx) = mpsc::unbounded_channel();
        let (mut worker, _done) = worker_with_exec_in_flight();
        let mut second = exec_work("wslc:busy", "second");

        worker.dispatch(second.work, &tx);

        let err = second
            .admit
            .try_recv()
            .expect("a refused exec must be answered through admit")
            .unwrap_err();
        assert_eq!(err.kind(), ErrKind::Busy);
        assert!(
            worker.exec_in_flight["wslc:busy"].parked.is_empty(),
            "a refused exec must not also queue behind the first"
        );
    }

    #[test]
    fn execs_on_different_containers_are_both_admitted() {
        let (tx, _rx) = mpsc::unbounded_channel();
        let (mut worker, _done) = worker_with_exec_in_flight();
        worker
            .containers
            .insert("wslc:idle".to_string(), started_entry());
        let mut other = exec_work("wslc:idle", "other");

        worker.dispatch(other.work, &tx);

        assert!(
            other
                .admit
                .try_recv()
                .expect("idle container answered")
                .is_ok(),
            "a container of its own must not inherit another container's slot"
        );
    }

    /// The guarantee a cancellation must keep: observed before the run starts,
    /// it reports cancelled rather than creating the process.
    #[test]
    fn an_exec_cancelled_before_it_starts_never_runs() {
        let (tx, _rx) = mpsc::unbounded_channel();
        let mut worker = Worker::new();
        worker
            .containers
            .insert("wslc:idle".to_string(), started_entry());
        let mut queued = exec_work("wslc:idle", "queued");
        queued.cancellation.store(true, Ordering::Release);

        worker.dispatch(queued.work, &tx);

        assert!(queued.admit.try_recv().unwrap().is_ok());
        assert_eq!(
            queued.done.try_recv().unwrap().unwrap(),
            ExecTerminal::Cancelled
        );
        assert!(
            !worker.exec_in_flight.contains_key("wslc:idle"),
            "a cancelled exec must not claim the container's slot"
        );
    }

    #[test]
    fn a_cancelled_exec_never_reaches_the_runner() {
        static STARTED: AtomicBool = AtomicBool::new(false);

        fn record_start(
            _: &Worker,
            _: ExecConfig,
            _: WslcContainer,
            _: OutputSink,
            _: Arc<AtomicBool>,
            _: &mpsc::UnboundedSender<WorkerCommand>,
        ) -> Result<(), WorkerError> {
            STARTED.store(true, Ordering::SeqCst);
            Ok(())
        }

        let (tx, _rx) = mpsc::unbounded_channel();
        let mut worker = worker_that_can_run();
        worker.start_run = record_start;
        let mut queued = exec_work("wslc:ready", "cancelled-before-start");
        queued.cancellation.store(true, Ordering::Release);

        worker.dispatch(queued.work, &tx);

        assert!(
            !STARTED.load(Ordering::SeqCst),
            "a cancelled exec must not reach the runner that creates the process"
        );
        assert_eq!(
            queued.done.try_recv().unwrap().unwrap(),
            ExecTerminal::Cancelled
        );
    }

    #[test]
    fn an_unconfirmed_exec_keeps_its_slot_claimed() {
        let (tx, _rx) = mpsc::unbounded_channel();
        let (mut worker, _done) = worker_with_exec_in_flight();
        let limiter = Arc::new(tokio::sync::Semaphore::new(1));
        let permit = Arc::clone(&limiter).try_acquire_owned().unwrap();
        worker.exec_in_flight.get_mut("wslc:busy").unwrap().slot = Some(Arc::new(permit));

        worker.finish_exec(
            "wslc:busy",
            ExecReport::Unconfirmed("the exit callback never fired".to_string()),
            &tx,
        );

        assert_eq!(
            limiter.available_permits(),
            0,
            "a quarantined sandbox whose process may still be running must keep its slot"
        );
    }

    #[test]
    fn deprovisioning_a_quarantined_sandbox_frees_its_slot() {
        let (tx, _rx) = mpsc::unbounded_channel();
        let (mut worker, _done) = worker_with_exec_in_flight();
        let limiter = Arc::new(tokio::sync::Semaphore::new(1));
        let permit = Arc::clone(&limiter).try_acquire_owned().unwrap();
        worker.exec_in_flight.get_mut("wslc:busy").unwrap().slot = Some(Arc::new(permit));
        worker.finish_exec(
            "wslc:busy",
            ExecReport::Unconfirmed("the exit callback never fired".to_string()),
            &tx,
        );
        assert_eq!(
            limiter.available_permits(),
            0,
            "the quarantine holds a slot"
        );

        worker
            .deprovision(DeprovisionConfig {
                sandbox_id: "wslc:busy".to_string(),
            })
            .unwrap();

        assert_eq!(
            limiter.available_permits(),
            1,
            "deprovisioning the quarantined sandbox must return its slot"
        );
    }

    #[test]
    fn an_unconfirmed_exec_is_quarantined_on_the_worker() {
        let (tx, _rx) = mpsc::unbounded_channel();
        let (mut worker, mut done) = worker_with_exec_in_flight();

        worker.finish_exec(
            "wslc:busy",
            ExecReport::Unconfirmed("the exit callback never fired".to_string()),
            &tx,
        );

        let err = done.try_recv().unwrap().unwrap_err();
        assert!(err.to_string().contains("quarantined"), "got {err}");
        assert!(worker.containers["wslc:busy"].quarantined);
    }

    /// A report can outlive its slot: shutdown drains the run and clears the
    /// map before the posted command is serviced.
    #[test]
    fn a_report_for_a_container_with_no_exec_in_flight_is_a_no_op() {
        let (tx, _rx) = mpsc::unbounded_channel();
        let mut worker = Worker::new();

        worker.finish_exec(
            "wslc:never-ran",
            ExecReport::Finished(Ok(ExecTerminal::Exited(0))),
            &tx,
        );

        assert_eq!(worker.live_container_count(), 0);
    }

    /// A run that has not reported back is still holding its container handle,
    /// so a teardown that gives up waiting must abandon the handles rather than
    /// release them.
    #[test]
    fn a_timed_out_shutdown_answers_clients_without_releasing_live_handles() {
        static RELEASED: AtomicBool = AtomicBool::new(false);

        unsafe extern "C" fn release(_: mxc_sdk::wslc_common::wslc_bindings::WslcContainer) -> i32 {
            RELEASED.store(true, Ordering::SeqCst);
            0
        }

        let (tx, mut rx) = mpsc::unbounded_channel();
        let mut worker = Worker::new();
        worker
            .containers
            .insert("wslc:busy".to_string(), flagged_entry(release));
        let (in_flight, mut done) = in_flight_exec("running");
        worker
            .exec_in_flight
            .insert("wslc:busy".to_string(), in_flight);
        let (work, mut parked_reply) = stop_work("wslc:busy");
        worker.dispatch(work, &tx);

        // The run never reports, so the drain can only expire.
        let _counted = ExecCount::enter(&worker.execs_in_flight);

        worker.shutdown(&mut rx, Duration::from_millis(100));

        assert!(
            !RELEASED.load(Ordering::SeqCst),
            "a container handle the run thread still holds must not be released"
        );
        assert!(
            done.try_recv()
                .expect("the running exec must be answered")
                .is_err(),
            "the client gets a typed error rather than a dropped channel"
        );
        assert!(parked_reply
            .try_recv()
            .expect("parked work must be answered")
            .is_err());
    }

    /// A command that reaches the queue ahead of the exec report must not cost
    /// the exec its result, nor be swallowed on its way past.
    #[test]
    fn shutdown_answers_a_command_queued_ahead_of_the_exec_report() {
        let (tx, mut rx) = mpsc::unbounded_channel();
        let (mut worker, mut done) = worker_with_exec_in_flight();

        let (queued, mut queued_reply) = stop_work("wslc:other");
        tx.send(WorkerCommand::Container(queued)).unwrap();
        tx.send(WorkerCommand::ExecFinished {
            sandbox_id: "wslc:busy".to_string(),
            report: ExecReport::Finished(Ok(ExecTerminal::Exited(0))),
        })
        .unwrap();

        worker.shutdown(&mut rx, EXEC_DRAIN_TIMEOUT);

        assert_eq!(
            done.try_recv()
                .expect("the finished exec must be answered")
                .unwrap(),
            ExecTerminal::Exited(0),
            "a command ahead of the report must not cost the exec its exit code"
        );
        assert!(
            queued_reply
                .try_recv()
                .expect("the queued command must be answered, not dropped")
                .is_err(),
            "a command consumed during teardown gets a typed error"
        );
    }

    /// A run that finished before teardown keeps its own exit code: the drain
    /// found its report already queued.
    #[test]
    fn shutdown_delivers_a_finished_execs_real_outcome() {
        let (tx, mut rx) = mpsc::unbounded_channel();
        let (mut worker, mut done) = worker_with_exec_in_flight();
        let (work, mut reply) = stop_work("wslc:busy");
        worker.dispatch(work, &tx);
        tx.send(WorkerCommand::ExecFinished {
            sandbox_id: "wslc:busy".to_string(),
            report: ExecReport::Finished(Ok(ExecTerminal::Exited(0))),
        })
        .unwrap();

        worker.shutdown(&mut rx, EXEC_DRAIN_TIMEOUT);

        assert_eq!(
            done.try_recv()
                .expect("the finished exec must be answered")
                .unwrap(),
            ExecTerminal::Exited(0),
            "a run that completed before teardown must keep its exit code"
        );
        assert!(
            reply
                .try_recv()
                .expect("the parked stop must be answered rather than dropped")
                .is_err(),
            "parked work is refused once the daemon is shutting down"
        );
    }

    /// The synthetic error is for a run whose report never arrived.
    #[test]
    fn shutdown_answers_an_exec_whose_report_never_arrived() {
        let (tx, mut rx) = mpsc::unbounded_channel();
        let (mut worker, mut done) = worker_with_exec_in_flight();
        let (work, mut reply) = stop_work("wslc:busy");
        worker.dispatch(work, &tx);

        worker.shutdown(&mut rx, EXEC_DRAIN_TIMEOUT);

        assert!(done
            .try_recv()
            .expect("the running exec must be answered")
            .is_err());
        assert!(reply
            .try_recv()
            .expect("the parked stop must be answered rather than dropped")
            .is_err());
    }

    /// A thread that unwinds never reaches its own release statement, so the
    /// count has to come off in `Drop` or teardown waits out the full budget.
    #[test]
    fn a_panicking_run_still_releases_its_in_flight_count() {
        let count = Arc::new(AtomicUsize::new(0));

        let panicked = std::panic::catch_unwind({
            let count = Arc::clone(&count);
            move || {
                let _counted = ExecCount::enter(&count);
                assert_eq!(count.load(Ordering::SeqCst), 1);
                panic!("the run exploded");
            }
        });

        assert!(panicked.is_err());
        assert_eq!(
            count.load(Ordering::SeqCst),
            0,
            "a panicking run must not leave itself counted forever"
        );
    }

    /// A client awaiting a reply learns the worker is gone from its channel
    /// closing, so an unwind must not take the reply channels with it.
    #[test]
    fn an_unwind_closes_the_reply_channels_it_abandons() {
        let (mut worker, mut done) = worker_with_exec_in_flight();
        let (work, mut parked_reply) = stop_work("wslc:busy");
        worker
            .exec_in_flight
            .get_mut("wslc:busy")
            .unwrap()
            .parked
            .push(work);
        let (reply, mut pending_reply) = oneshot::channel();
        worker.pending.insert(
            1,
            PendingProvision {
                config: ProvisionConfig {
                    image: "alpine:latest".to_string(),
                    image_tar_path: None,
                    volumes: Vec::new(),
                    network: Default::default(),
                    port_mappings: Vec::new(),
                },
                reply,
                _retire_deadline: std::sync::mpsc::channel().0,
            },
        );

        worker.abandon_if_borrowed(1, false);

        assert!(
            matches!(done.try_recv(), Err(oneshot::error::TryRecvError::Closed)),
            "the running exec's client must see its channel close"
        );
        assert!(
            matches!(
                parked_reply.try_recv(),
                Err(oneshot::error::TryRecvError::Closed)
            ),
            "parked work's client must see its channel close"
        );
        assert!(
            matches!(
                pending_reply.try_recv(),
                Err(oneshot::error::TryRecvError::Closed)
            ),
            "a parked provision's client must see its channel close"
        );
    }

    /// Releasing a container handle is an SDK call; a run thread still holding
    /// one must outlive the worker that owned it.
    #[test]
    fn an_unwind_with_a_run_still_counted_keeps_container_handles() {
        static RELEASED: AtomicBool = AtomicBool::new(false);

        unsafe extern "C" fn release(_: mxc_sdk::wslc_common::wslc_bindings::WslcContainer) -> i32 {
            RELEASED.store(true, Ordering::SeqCst);
            0
        }

        let mut worker = Worker::new();
        worker
            .containers
            .insert("wslc:busy".to_string(), flagged_entry(release));

        worker.abandon_if_borrowed(1, false);

        assert!(
            !RELEASED.load(Ordering::SeqCst),
            "a handle the run thread is still using must not be released"
        );
    }

    #[test]
    fn an_unwind_with_a_pull_outstanding_keeps_container_handles() {
        static RELEASED: AtomicBool = AtomicBool::new(false);

        unsafe extern "C" fn release(_: mxc_sdk::wslc_common::wslc_bindings::WslcContainer) -> i32 {
            RELEASED.store(true, Ordering::SeqCst);
            0
        }

        let mut worker = Worker::new();
        worker
            .containers
            .insert("wslc:pulling".to_string(), flagged_entry(release));

        worker.abandon_if_borrowed(0, true);

        assert!(
            !RELEASED.load(Ordering::SeqCst),
            "a pull still borrows the SDK and session, so the handles must be kept"
        );
    }

    #[test]
    fn an_unwind_with_no_run_counted_releases_container_handles() {
        static RELEASED: AtomicBool = AtomicBool::new(false);

        unsafe extern "C" fn release(_: mxc_sdk::wslc_common::wslc_bindings::WslcContainer) -> i32 {
            RELEASED.store(true, Ordering::SeqCst);
            0
        }

        let mut worker = Worker::new();
        worker
            .containers
            .insert("wslc:idle".to_string(), flagged_entry(release));

        worker.abandon_if_borrowed(0, false);

        assert!(
            RELEASED.load(Ordering::SeqCst),
            "with nothing in flight the handles must be released normally"
        );
    }

    /// Teardown asks whether *its own* handles are free, so one worker's run
    /// must not hold another's drain open.
    #[test]
    fn one_workers_run_does_not_count_against_anothers_drain() {
        let busy = Worker::new();
        let idle = Worker::new();

        let _counted = ExecCount::enter(&busy.execs_in_flight);

        assert_eq!(busy.execs_in_flight.load(Ordering::SeqCst), 1);
        assert_eq!(
            idle.execs_in_flight.load(Ordering::SeqCst),
            0,
            "a run on one worker must not be counted against another's teardown"
        );
    }

    /// Teardown must not free a container handle a run thread is still using.
    #[test]
    fn a_counted_exec_keeps_the_drain_from_reporting_clear() {
        let in_flight = AtomicUsize::new(1);

        assert!(!wait_for_execs_in_flight(
            &in_flight,
            Duration::from_millis(100)
        ));

        in_flight.store(0, Ordering::SeqCst);
        assert!(wait_for_execs_in_flight(
            &in_flight,
            Duration::from_millis(100)
        ));
    }

    /// Provision and start a sandbox on the live host, returning its id.
    async fn provisioned_and_started(handle: &SessionHandle) -> String {
        let id = handle
            .provision(ProvisionConfig {
                image: "alpine:latest".to_string(),
                image_tar_path: None,
                volumes: Vec::new(),
                network: Default::default(),
                port_mappings: Vec::new(),
            })
            .await
            .unwrap();
        handle
            .start(StartConfig {
                sandbox_id: id.clone(),
            })
            .await
            .unwrap();
        id
    }

    async fn stop_and_deprovision(handle: &SessionHandle, sandbox_id: String) {
        handle
            .stop(StopConfig {
                sandbox_id: sandbox_id.clone(),
            })
            .await
            .unwrap();
        handle
            .deprovision(DeprovisionConfig { sandbox_id })
            .await
            .unwrap();
    }

    fn sleep_exec(exec_id: &str, sandbox_id: &str, seconds: u32) -> ExecConfig {
        ExecConfig {
            exec_id: exec_id.to_string(),
            run_token: format!("{exec_id}-run"),
            sandbox_id: sandbox_id.to_string(),
            script_code: format!("sleep {seconds}"),
            working_directory: String::new(),
            env: Vec::new(),
            env_scope: EnvScope::Merge,
            timeout_ms: 30_000,
        }
    }

    /// A run that prints an epoch second either side of its sleep.
    fn stamped_sleep_exec(exec_id: &str, sandbox_id: &str, seconds: u32) -> ExecConfig {
        ExecConfig {
            script_code: format!("date +%s; sleep {seconds}; date +%s"),
            ..sleep_exec(exec_id, sandbox_id, seconds)
        }
    }

    /// Drain a stamped run's output and return the epoch seconds it reported
    /// either side of its sleep.
    async fn stamped_interval(exec: &mut ExecStream) -> (i64, i64) {
        let mut stdout = Vec::new();
        while let Some((stream, data)) = exec.output.recv().await {
            if stream == OutStream::Stdout {
                stdout.extend_from_slice(&data);
            }
        }
        let text = String::from_utf8_lossy(&stdout);
        let stamps: Vec<i64> = text
            .lines()
            .filter_map(|line| line.trim().parse::<i64>().ok())
            .collect();
        assert!(
            stamps.len() >= 2,
            "a stamped run must report a start and an end, got {text:?}"
        );
        (stamps[0], stamps[stamps.len() - 1])
    }

    // Exercises the real SDK path end to end: provision (boot VM + create
    // container) → start → exec → stop → deprovision → refcount back to 0. It
    // provisions with the default isolated posture, which refuses a registry
    // pull, so `alpine:latest` has to be cached in the store slot this worker
    // leases — slot 0 of the pool, since it holds one session. Warm it with
    // `scripts\setup-wslc.ps1 -DaemonSlots 1`. It is `#[ignore]`d and run
    // explicitly with:
    // `cargo test -p mxc-sdk --features wslc --bin wxc-wslc-daemon -- --ignored`
    #[tokio::test]
    #[ignore = "requires a WSL2 host with alpine:latest cached in daemon store slot 0"]
    async fn full_lifecycle_on_wsl_host() {
        let handle = spawn().unwrap();
        assert_eq!(count(&handle).await, 0);

        let id = handle
            .provision(ProvisionConfig {
                image: "alpine:latest".to_string(),
                image_tar_path: None,
                volumes: Vec::new(),
                network: Default::default(),
                port_mappings: Vec::new(),
            })
            .await
            .unwrap();
        assert_eq!(count(&handle).await, 1);

        handle
            .start(StartConfig {
                sandbox_id: id.clone(),
            })
            .await
            .unwrap();

        let mut exec = handle
            .exec(
                ExecConfig {
                    exec_id: "full-lifecycle".to_string(),
                    run_token: "full-lifecycle-run".to_string(),
                    sandbox_id: id.clone(),
                    script_code: "echo hi".to_string(),
                    working_directory: String::new(),
                    env: Vec::new(),
                    env_scope: EnvScope::Merge,
                    timeout_ms: 30_000,
                },
                None,
            )
            .await
            .unwrap();
        // Drain the live output stream, then await the exit code.
        let mut stdout = Vec::new();
        while let Some((kind, data)) = exec.output.recv().await {
            if kind == OutStream::Stdout {
                stdout.extend_from_slice(&data);
            }
        }
        let code = exec.done.await.unwrap().unwrap();
        assert_eq!(code, ExecTerminal::Exited(0));
        assert_eq!(String::from_utf8_lossy(&stdout).trim(), "hi");

        handle
            .stop(StopConfig {
                sandbox_id: id.clone(),
            })
            .await
            .unwrap();

        handle
            .deprovision(DeprovisionConfig { sandbox_id: id })
            .await
            .unwrap();
        assert_eq!(count(&handle).await, 0);

        handle.shutdown().await.unwrap();
    }

    /// The guarantee a cancellation must keep against the live SDK: observed
    /// before the worker reaches the run, it creates no process.
    #[tokio::test]
    #[ignore = "requires a WSL2 host with alpine:latest already in the daemon session cache"]
    async fn cancelled_queued_exec_never_starts_process() {
        let handle = spawn().unwrap();
        let id = provisioned_and_started(&handle).await;

        // Occupy the worker with a container creation, so the exec below is
        // still queued when the cancellation lands. Spawned tasks are polled in
        // order, so this provision reaches the worker first.
        let blocker_handle = handle.clone();
        let blocker = tokio::spawn(async move {
            blocker_handle
                .provision(ProvisionConfig {
                    image: "alpine:latest".to_string(),
                    image_tar_path: None,
                    volumes: Vec::new(),
                    network: Default::default(),
                    port_mappings: Vec::new(),
                })
                .await
        });

        let queued_handle = handle.clone();
        let queued_id = id.clone();
        let queued = tokio::spawn(async move {
            queued_handle
                .exec(
                    ExecConfig {
                        exec_id: "cancelled-queued".to_string(),
                        run_token: "cancelled-queued-run".to_string(),
                        sandbox_id: queued_id,
                        script_code: "touch /tmp/mxc-cancelled-queued-marker".to_string(),
                        working_directory: String::new(),
                        env: Vec::new(),
                        env_scope: EnvScope::Merge,
                        timeout_ms: 30_000,
                    },
                    None,
                )
                .await
        });

        for _ in 0..100 {
            if handle
                .active_execs
                .lock()
                .unwrap()
                .contains_key("cancelled-queued")
            {
                break;
            }
            tokio::task::yield_now().await;
        }
        assert!(
            handle
                .active_execs
                .lock()
                .unwrap()
                .contains_key("cancelled-queued"),
            "queued exec was not registered"
        );
        handle.cancel_exec("cancelled-queued", "cancelled-queued-run");

        let blocker_id = blocker.await.unwrap().unwrap();
        let queued = queued.await.unwrap().unwrap();
        assert_eq!(queued.done.await.unwrap().unwrap(), ExecTerminal::Cancelled);

        let marker_check = handle
            .exec(
                ExecConfig {
                    exec_id: "marker-check".to_string(),
                    run_token: "marker-check-run".to_string(),
                    sandbox_id: id.clone(),
                    script_code: "test ! -e /tmp/mxc-cancelled-queued-marker".to_string(),
                    working_directory: String::new(),
                    env: Vec::new(),
                    env_scope: EnvScope::Merge,
                    timeout_ms: 30_000,
                },
                None,
            )
            .await
            .unwrap();
        assert_eq!(
            marker_check.done.await.unwrap().unwrap(),
            ExecTerminal::Exited(0)
        );

        stop_and_deprovision(&handle, id).await;
        handle
            .deprovision(DeprovisionConfig {
                sandbox_id: blocker_id,
            })
            .await
            .unwrap();
        handle.shutdown().await.unwrap();
    }

    /// A second exec on a container that already has one is refused before any
    /// admission reaches the client.
    #[tokio::test]
    #[ignore = "requires a WSL2 host with alpine:latest already in the daemon session cache"]
    async fn a_second_exec_on_a_busy_container_is_refused() {
        let handle = spawn().unwrap();
        let id = provisioned_and_started(&handle).await;

        let first = handle
            .exec(sleep_exec("busy-first", &id, 3), None)
            .await
            .unwrap();

        let refused = handle
            .exec(
                ExecConfig {
                    exec_id: "busy-second".to_string(),
                    run_token: "busy-second-run".to_string(),
                    sandbox_id: id.clone(),
                    script_code: "echo hi".to_string(),
                    working_directory: String::new(),
                    env: Vec::new(),
                    env_scope: EnvScope::Merge,
                    timeout_ms: 30_000,
                },
                None,
            )
            .await
            .unwrap_err();
        assert_eq!(refused.kind(), ErrKind::Busy);

        assert_eq!(first.done.await.unwrap().unwrap(), ExecTerminal::Exited(0));

        stop_and_deprovision(&handle, id).await;
        handle.shutdown().await.unwrap();
    }

    /// Two sandboxes must run at the same time rather than one after the other.
    #[tokio::test]
    #[ignore = "requires a WSL2 host with alpine:latest already in the daemon session cache"]
    async fn execs_on_two_sandboxes_overlap() {
        let handle = spawn().unwrap();
        let first_id = provisioned_and_started(&handle).await;
        let second_id = provisioned_and_started(&handle).await;

        let mut first = handle
            .exec(stamped_sleep_exec("overlap-first", &first_id, 6), None)
            .await
            .unwrap();
        let mut second = handle
            .exec(stamped_sleep_exec("overlap-second", &second_id, 6), None)
            .await
            .unwrap();

        // Both sandboxes share one utility VM, so their clocks agree.
        let (first_start, first_end) = stamped_interval(&mut first).await;
        let (second_start, second_end) = stamped_interval(&mut second).await;
        assert_eq!(first.done.await.unwrap().unwrap(), ExecTerminal::Exited(0));
        assert_eq!(second.done.await.unwrap().unwrap(), ExecTerminal::Exited(0));

        let overlap = first_end.min(second_end) - first_start.max(second_start);
        assert!(
            overlap >= 2,
            "the two 6s runs overlapped by {overlap}s, so they were serialized: \
             first {first_start}..{first_end}, second {second_start}..{second_end}"
        );

        stop_and_deprovision(&handle, first_id).await;
        stop_and_deprovision(&handle, second_id).await;
        handle.shutdown().await.unwrap();
    }

    /// A deprovision that reaches the worker mid-run would delete the container
    /// and free the handle the run thread is using.
    #[tokio::test]
    #[ignore = "requires a WSL2 host with alpine:latest already in the daemon session cache"]
    async fn deprovision_during_an_exec_waits_for_the_run() {
        let handle = spawn().unwrap();
        let id = handle
            .provision(ProvisionConfig {
                image: "alpine:latest".to_string(),
                image_tar_path: None,
                volumes: Vec::new(),
                network: Default::default(),
                port_mappings: Vec::new(),
            })
            .await
            .unwrap();
        handle
            .start(StartConfig {
                sandbox_id: id.clone(),
            })
            .await
            .unwrap();

        let mut exec = handle
            .exec(
                ExecConfig {
                    exec_id: "deprovision-overlap".to_string(),
                    run_token: "deprovision-overlap-run".to_string(),
                    sandbox_id: id.clone(),
                    script_code: "sleep 5; echo survived".to_string(),
                    working_directory: String::new(),
                    env: Vec::new(),
                    env_scope: EnvScope::Merge,
                    timeout_ms: 30_000,
                },
                None,
            )
            .await
            .unwrap();

        // Admission has returned, so the run thread already holds the handle.
        let deprovision = tokio::spawn({
            let handle = handle.clone();
            let sandbox_id = id.clone();
            async move { handle.deprovision(DeprovisionConfig { sandbox_id }).await }
        });

        tokio::time::sleep(Duration::from_millis(500)).await;
        assert!(
            !deprovision.is_finished(),
            "deprovision must wait for the run rather than delete the container under it"
        );

        let mut stdout = Vec::new();
        while let Some((kind, data)) = exec.output.recv().await {
            if kind == OutStream::Stdout {
                stdout.extend_from_slice(&data);
            }
        }
        assert_eq!(exec.done.await.unwrap().unwrap(), ExecTerminal::Exited(0));
        assert_eq!(String::from_utf8_lossy(&stdout).trim(), "survived");

        deprovision.await.unwrap().unwrap();
        assert_eq!(count(&handle).await, 0);

        handle.shutdown().await.unwrap();
    }
}