boxlite-cli 0.10.5

Command-line interface for BoxLite container runtime
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
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
//! `boxlite serve` — long-running REST API server.
//!
//! Holds a single BoxliteRuntime and exposes the full REST API
//! over HTTP so that `Boxlite.rest()` clients can connect.

mod handlers;
mod types;

use std::collections::HashMap;
use std::sync::Arc;
use std::time::Instant;

use axum::extract::{Request, State};
use axum::http::Method;
use axum::http::StatusCode;
use axum::http::header::AUTHORIZATION;
use axum::middleware::{self, Next};
use axum::response::{IntoResponse, Response};
use axum::routing::{get, post, put};
use axum::{Json, Router};
use clap::Args;
use futures::StreamExt;
use tokio::sync::RwLock;
use tower_http::catch_panic::CatchPanicLayer;
use tower_http::request_id::{MakeRequestUuid, PropagateRequestIdLayer, SetRequestIdLayer};

use boxlite::runtime::options::{InboundNetworkConfig, NetworkMode, OutboundNetworkConfig};
use boxlite::{
    BoxCommand, BoxInfo, BoxOptions, BoxStatus, BoxliteRuntime, ExecStdin, Execution, LiteBox,
    NetworkSpec, RootfsSpec,
};

use crate::cli::GlobalFlags;
use crate::defaults::{LOCAL_SERVE_HOST, LOCAL_SERVE_PORT};

use self::types::{BoxResponse, CreateBoxRequest, ErrorBody, ErrorDetail, ExecRequest};

fn parse_api_key(raw: &str) -> Result<String, String> {
    if raw.is_empty() {
        Err("API key must not be empty".to_string())
    } else {
        Ok(raw.to_string())
    }
}

// ============================================================================
// CLI Args
// ============================================================================

#[derive(Args, Debug)]
pub struct ServeArgs {
    /// Port to listen on. Defaults to `LOCAL_SERVE_PORT`.
    #[arg(long, default_value_t = LOCAL_SERVE_PORT)]
    pub port: u16,

    /// Host/address to bind to. Defaults to `LOCAL_SERVE_HOST`.
    #[arg(long, default_value_t = LOCAL_SERVE_HOST.to_string())]
    pub host: String,

    /// Optional expected API key. When set, every route except
    /// `GET /v1/config` requires `Authorization: Bearer <this>` (constant-time
    /// match) and returns 401 otherwise. Unset = permissive (accepts any/no
    /// bearer) — the zero-config local-dev default.
    #[arg(
        long,
        env = "BOXLITE_SERVE_API_KEY",
        hide_env_values = true,
        value_parser = parse_api_key
    )]
    pub api_key: Option<String>,
}

// ============================================================================
// Shared State
// ============================================================================

struct AppState {
    runtime: BoxliteRuntime,
    /// Cached box handles (box_id -> Arc<LiteBox>).
    boxes: RwLock<HashMap<String, Arc<LiteBox>>>,
    /// Active executions (execution_id -> ActiveExecution). Holds an
    /// `Arc` so attach sessions can drop the map lock before doing
    /// long-running WS pumping while keeping the exec alive.
    executions: RwLock<HashMap<String, Arc<ActiveExecution>>>,
    /// Optional expected API key (`--api-key` / `$BOXLITE_SERVE_API_KEY`).
    /// `None` ⇒ permissive (no auth enforced).
    api_key: Option<String>,
    /// Lifecycle deadlines per box, in seconds.
    ///
    /// Held here rather than in `BoxOptions` because the engine accepts neither
    /// on a local runtime, which is what `serve` drives:
    ///
    /// - `auto_stop` is refused outright — `reject_local_unsupported_options`
    ///   (`runtime/rt_impl.rs:1743`) returns Unsupported, because a local
    ///   runtime has no sweeper of its own.
    /// - `auto_delete` is accepted but means something else: `removes_on_stop()`
    ///   is `effective_auto_delete() > 0`, so passing a deadline through makes
    ///   the engine delete the box the instant it stops — the opposite of
    ///   waiting — and forces it non-detached.
    ///
    /// `serve` is the sweeper, so it keeps the policy and acts on it.
    ///
    /// In memory only: a restart forgets every deadline, and a box created
    /// before it is never swept.
    lifecycle: RwLock<HashMap<String, LifecyclePolicy>>,
    /// Last observed user activity per box, for AutoStop.
    ///
    /// `serve` holds the `$BOXLITE_HOME` lock for its whole life, so nothing
    /// else can touch these boxes while it runs and this map is a complete
    /// record. A box it has not seen is seeded at the current tick rather than
    /// measured against `BoxInfo.last_updated`, which tracks state transitions
    /// and would report a long-running busy box as idle since boot.
    last_activity: RwLock<HashMap<String, Instant>>,
}

/// The deadlines `serve` enforces for one box. `0` disables either.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(in crate::commands::serve) struct LifecyclePolicy {
    pub auto_stop: u32,
    pub auto_delete: u32,
}

impl LifecyclePolicy {
    fn is_empty(&self) -> bool {
        self.auto_stop == 0 && self.auto_delete == 0
    }
}

impl AppState {
    /// Remember a box's deadlines. A policy with neither set is not stored.
    pub(in crate::commands::serve) async fn set_lifecycle(
        &self,
        box_id: &str,
        policy: LifecyclePolicy,
    ) {
        if policy.is_empty() {
            return;
        }
        self.lifecycle
            .write()
            .await
            .insert(box_id.to_string(), policy);
    }

    /// The box's deadlines, or an all-zero policy when it has none.
    pub(in crate::commands::serve) async fn lifecycle_of(&self, box_id: &str) -> LifecyclePolicy {
        self.lifecycle
            .read()
            .await
            .get(box_id)
            .copied()
            .unwrap_or_default()
    }

    /// Render a box for the wire, with the deadlines this server holds.
    ///
    /// The single place a `BoxResponse` is built, because `BoxInfo` cannot
    /// carry these two fields here: `build_box_options` withholds both from the
    /// runtime, so `BoxInfo` reports the zeroes it was handed. Reading them off
    /// `info` would answer with no deadline on a box the sweep is enforcing one
    /// for. A box with no stored policy reports none, so a clone or an import
    /// does not inherit the deadline of the box it came from.
    pub(in crate::commands::serve) async fn box_response(&self, info: &BoxInfo) -> BoxResponse {
        let mut resp = box_info_to_response(info);
        // `auto_resume` is not overlaid: it *is* forwarded to the runtime, so
        // `info` already carries the caller's value back.
        let policy = self.lifecycle_of(info.id.as_ref()).await;
        resp.auto_stop = policy.auto_stop;
        resp.auto_delete = policy.auto_delete;
        resp
    }

    /// Mark a box as used right now, resetting its AutoStop window.
    ///
    /// The single write site for the idle clock, so the request middleware and
    /// the sweep agree on what "used" means.
    pub(in crate::commands::serve) async fn record_box_activity(&self, box_id: &str) {
        self.last_activity
            .write()
            .await
            .insert(box_id.to_string(), Instant::now());
    }

    /// Forget everything held about a box that is gone.
    ///
    /// Without this both maps grow for the life of the process, one entry per
    /// box ever deleted.
    pub(in crate::commands::serve) async fn forget_box(&self, box_id: &str) {
        self.lifecycle.write().await.remove(box_id);
        self.last_activity.write().await.remove(box_id);
    }

    /// Drop idle clocks for spellings that name no box.
    ///
    /// `forget_box` covers boxes this server deleted, but not ids that were
    /// never boxes: `record_activity` stamps the clock straight off the request
    /// path, before any handler can reject an unknown id, so a client looping
    /// over made-up ids would otherwise grow the map for the life of the
    /// process — and `serve` binds `0.0.0.0` and may run with no API key.
    ///
    /// Only `last_activity` is pruned. It is the map unauthenticated input can
    /// grow, and re-seeding a live box costs nothing but a fresh window. The
    /// `lifecycle` map is written only by `create_box`, always with a canonical
    /// id, so pruning it would buy nothing and would race: a box created after
    /// the caller's `list_info` snapshot would have its deadline dropped and
    /// never re-added.
    pub(in crate::commands::serve) async fn retain_known_boxes(
        &self,
        live: &[(String, Option<String>)],
    ) {
        self.last_activity
            .write()
            .await
            .retain(|spelling, _| box_named_by(spelling, live).is_some());
    }
}

/// Which stdio session an [`ActiveExecution`] fronts.
///
/// Both kinds live in the same `executions` registry, keyed by execution
/// id — but only an exec session can be *addressed* by that id: the main
/// session's id is the container id, which the guest assigns and `BoxInfo`
/// does not carry, so a client on `/boxes/{id}/attach` can only name the
/// box. Marking the kind at insert time is what lets `find_main_session`
/// recognize an already-open main session from the box id alone.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(in crate::commands::serve) enum SessionKind {
    /// A tenant exec — `POST /boxes/{box_id}/exec`.
    Exec,
    /// The box's main command session: the container's init (docker
    /// semantics — `run IMAGE COMMAND` runs COMMAND *as* init). The guest
    /// registers it under `execution_id == container_id`.
    Main,
}

/// Response header on the `/boxes/{box_id}/attach` upgrade carrying the
/// main session's execution id (the container id).
///
/// The client cannot know that id up front, but it needs one for every
/// *other* thing an `Execution` does — signal, resize, kill, status probe,
/// reconnect — all of which are addressed by execution id. Handing it back
/// on the 101 makes the main session an ordinary session from that point
/// on: no parallel control path, no second-class `Execution`. The client
/// half of this contract is `RestBox::attach` in
/// `src/boxlite/src/rest/litebox.rs`, which pins the same header name.
pub(in crate::commands::serve) const MAIN_SESSION_ID_HEADER: &str = "x-boxlite-execution-id";

/// Server-side state for one execution. The underlying `Execution`'s
/// stdout/stderr are consumed once at creation and tee'd into broadcast
/// channels so any number of attach sessions (over time) can subscribe.
/// The `Execution` itself is kept in the map so `wait()`, `kill()`,
/// `signal()`, `resize_tty()` and reattach all work.
pub(in crate::commands::serve) struct ActiveExecution {
    box_id: String,
    kind: SessionKind,
    execution: Execution,
    /// Stdin sink owned by the WS `/attach` session.
    stdin: tokio::sync::Mutex<Option<ExecStdin>>,
    /// Backlog-aware broadcast tees. Late subscribers see the backlog
    /// snapshot on subscribe, then live data — matching the Go runner's
    /// streamBus pattern.
    stdout_bus: Arc<BacklogBroadcast>,
    stderr_bus: Arc<BacklogBroadcast>,
    /// Single-attach + reaper state, all under one Mutex.
    attach: tokio::sync::Mutex<AttachState>,
    /// Whether the exec has been seen to complete (Done fired). Set by
    /// the wait task; checked by the reaper to skip already-exited execs.
    done: std::sync::atomic::AtomicBool,
    /// Watch-channel mirror of `done` for async observers. The wait task
    /// flips this to `true` after `Execution::wait()` returns; SSE and WS
    /// handlers `select!` on `done_rx.changed()` so they break out of their
    /// loops the instant the process completes (rather than waiting for the
    /// broadcast channel's receivers to see `Closed`, which they never do
    /// because `ActiveExecution` owns the master Senders for its lifetime).
    /// Pattern: Vector `RepairState` watch::channel<EnumState>
    /// (src/sinks/redis/sink.rs:130-135); ours is binary so bool suffices.
    done_tx: tokio::sync::watch::Sender<bool>,
    /// Final exit code, populated once Done fires. Read by the WS attach
    /// handler to send the `{"type":"exit", "exit_code":N}` text frame.
    exit_code: std::sync::atomic::AtomicI32,
    /// Stamped when the wait task fires. Used by the retention check so
    /// execs that ran longer than `COMPLETED_RETENTION_GRACE` are not
    /// evicted immediately on exit.
    done_at: std::sync::Mutex<Option<Instant>>,
    /// Used by the reaper to enforce the 24 h hard cap.
    created_at: Instant,
}

struct AttachState {
    connected: bool,
    /// Wall-clock instant when the single-attach slot last went idle.
    /// Initialized to the exec's creation time so a client that never
    /// calls `/attach` still escalates through SIGHUP→SIGTERM→SIGKILL
    /// at the reconnect_grace boundary. Cleared on successful
    /// `mark_connected()`, re-stamped on `mark_disconnected()`.
    last_disconnect_at: Option<Instant>,
    signaled_hup: bool,
    signaled_term: bool,
    /// Set by the reaper's final escalation (SIGKILL path). Once true,
    /// `mark_connected()` rejects so a late attach can't race the kill.
    reaping_kill: bool,
    /// True while the reaper is delivering a cooperative signal (HUP/TERM).
    /// `mark_connected()` rejects while set, closing the TOCTOU gap between
    /// `try_escalate_*` releasing the lock and `signal()` reaching the
    /// process. Cleared by `finish_escalation()` after delivery.
    escalating: bool,
}

/// Bounded buffer size for the stdout/stderr broadcast channels.
/// 256 chunks at ~4 KB each = ~1 MB of slack for a transiently slow
/// subscriber before it sees `RecvError::Lagged`.
const ATTACH_BROADCAST_CAPACITY: usize = 256;

/// Byte-capped backlog retained for replay on late (re)attach.
/// Matches the Go runner's `streamBusBacklogCap` (256 KiB).
const BACKLOG_BYTE_CAP: usize = 256 * 1024;

/// Broadcast sender with a bounded byte backlog for replay on subscribe.
///
/// Pattern mirrors Go runner's `streamBus` — `send()` appends to a
/// byte-capped backlog AND fans out via broadcast; `subscribe()` replays
/// the backlog snapshot then switches to live broadcast.
struct BacklogBroadcast {
    tx: tokio::sync::broadcast::Sender<Vec<u8>>,
    state: std::sync::Mutex<BacklogState>,
    cap: usize,
}

struct BacklogState {
    backlog: std::collections::VecDeque<Vec<u8>>,
    total_bytes: usize,
}

impl BacklogBroadcast {
    fn new(capacity: usize, backlog_cap: usize) -> Self {
        let (tx, _) = tokio::sync::broadcast::channel(capacity);
        Self {
            tx,
            state: std::sync::Mutex::new(BacklogState {
                backlog: std::collections::VecDeque::new(),
                total_bytes: 0,
            }),
            cap: backlog_cap,
        }
    }

    fn send(&self, data: Vec<u8>) {
        let mut state = self.state.lock().unwrap();
        state.total_bytes += data.len();
        state.backlog.push_back(data.clone());
        // Always retain at least the most recent chunk so a late subscriber
        // sees something, even if a single chunk exceeds the byte cap.
        while state.total_bytes > self.cap && state.backlog.len() > 1 {
            if let Some(old) = state.backlog.pop_front() {
                state.total_bytes -= old.len();
            } else {
                break;
            }
        }
        // Broadcast under the same lock so subscribe() can't snapshot
        // the backlog AND receive the same chunk from the live channel.
        let _ = self.tx.send(data);
    }

    /// Subscribe with atomic backlog replay. The returned receiver
    /// yields the backlog snapshot first, then live broadcasts — no
    /// gap, no interleaving. Both the backlog snapshot and the
    /// broadcast subscribe happen under the state lock, which `send()`
    /// also holds through its `tx.send()`, preventing duplicates.
    fn subscribe(&self) -> BacklogReceiver {
        let state = self.state.lock().unwrap();
        let replay: std::collections::VecDeque<Vec<u8>> = state.backlog.iter().cloned().collect();
        let rx = self.tx.subscribe();
        BacklogReceiver { replay, rx }
    }
}

/// Receiver that yields backlog chunks first, then live broadcast.
/// Created by `BacklogBroadcast::subscribe()`.
struct BacklogReceiver {
    replay: std::collections::VecDeque<Vec<u8>>,
    rx: tokio::sync::broadcast::Receiver<Vec<u8>>,
}

impl BacklogReceiver {
    async fn recv(&mut self) -> Result<Vec<u8>, tokio::sync::broadcast::error::RecvError> {
        if let Some(chunk) = self.replay.pop_front() {
            return Ok(chunk);
        }
        self.rx.recv().await
    }

    fn try_recv(&mut self) -> Result<Vec<u8>, tokio::sync::broadcast::error::TryRecvError> {
        if let Some(chunk) = self.replay.pop_front() {
            return Ok(chunk);
        }
        self.rx.try_recv()
    }
}

impl ActiveExecution {
    fn new(
        box_id: String,
        kind: SessionKind,
        mut execution: Execution,
        stdin: Option<ExecStdin>,
    ) -> Arc<Self> {
        let stdout = execution.stdout();
        let stderr = execution.stderr();

        let stdout_bus = Arc::new(BacklogBroadcast::new(
            ATTACH_BROADCAST_CAPACITY,
            BACKLOG_BYTE_CAP,
        ));
        let stderr_bus = Arc::new(BacklogBroadcast::new(
            ATTACH_BROADCAST_CAPACITY,
            BACKLOG_BYTE_CAP,
        ));
        let (done_tx, _) = tokio::sync::watch::channel(false);

        let now = Instant::now();
        let active = Arc::new(Self {
            box_id,
            kind,
            execution,
            stdin: tokio::sync::Mutex::new(stdin),
            stdout_bus: stdout_bus.clone(),
            stderr_bus: stderr_bus.clone(),
            attach: tokio::sync::Mutex::new(AttachState {
                connected: false,
                last_disconnect_at: Some(now),
                signaled_hup: false,
                signaled_term: false,
                reaping_kill: false,
                escalating: false,
            }),
            done: std::sync::atomic::AtomicBool::new(false),
            done_tx,
            exit_code: std::sync::atomic::AtomicI32::new(-1),
            done_at: std::sync::Mutex::new(None),
            created_at: now,
        });

        // Spawn pumps that read the (single-consumer) Stream half and
        // fan out via the backlog-aware broadcast. Unlike raw broadcast,
        // BacklogBroadcast retains recent output so late subscribers
        // see the backlog on subscribe.
        let stdout_handle = if let Some(mut out) = stdout {
            let bus = stdout_bus;
            Some(tokio::spawn(async move {
                while let Some(line) = out.next().await {
                    bus.send(line.into_bytes());
                }
            }))
        } else {
            None
        };
        let stderr_handle = if let Some(mut err) = stderr {
            let bus = stderr_bus;
            Some(tokio::spawn(async move {
                while let Some(line) = err.next().await {
                    bus.send(line.into_bytes());
                }
            }))
        } else {
            None
        };

        // Wait task: records exit code + flips done. Barriers the pump
        // tasks so all output is broadcast before done_tx fires.
        {
            let active = Arc::clone(&active);
            tokio::spawn(async move {
                if let Ok(result) = active.execution.wait().await {
                    active
                        .exit_code
                        .store(result.exit_code, std::sync::atomic::Ordering::SeqCst);
                }
                if let Some(h) = stdout_handle {
                    let _ = h.await;
                }
                if let Some(h) = stderr_handle {
                    let _ = h.await;
                }
                *active.done_at.lock().unwrap() = Some(Instant::now());
                active.done.store(true, std::sync::atomic::Ordering::SeqCst);
                let _ = active.done_tx.send(true);
            });
        }

        active
    }

    pub(in crate::commands::serve) fn box_id(&self) -> &str {
        &self.box_id
    }

    pub(in crate::commands::serve) fn kind(&self) -> SessionKind {
        self.kind
    }

    pub(in crate::commands::serve) fn stdout_bus(&self) -> &BacklogBroadcast {
        &self.stdout_bus
    }

    pub(in crate::commands::serve) fn stderr_bus(&self) -> &BacklogBroadcast {
        &self.stderr_bus
    }

    pub(in crate::commands::serve) fn stdin(&self) -> &tokio::sync::Mutex<Option<ExecStdin>> {
        &self.stdin
    }

    pub(in crate::commands::serve) fn execution(&self) -> &Execution {
        &self.execution
    }

    pub(in crate::commands::serve) fn is_done(&self) -> bool {
        self.done.load(std::sync::atomic::Ordering::SeqCst)
    }

    /// Subscribe to the "process is done" watch channel. Callers select
    /// on `rx.changed()` to wake the instant the wait task fires.
    pub(in crate::commands::serve) fn done_rx(&self) -> tokio::sync::watch::Receiver<bool> {
        self.done_tx.subscribe()
    }

    pub(in crate::commands::serve) fn exit_code(&self) -> i32 {
        self.exit_code.load(std::sync::atomic::Ordering::SeqCst)
    }

    pub(in crate::commands::serve) fn created_at(&self) -> Instant {
        self.created_at
    }

    /// Whether a completed execution should still be retained in the map.
    /// Used by the reaper and tests.
    pub(in crate::commands::serve) fn should_retain(&self, now: Instant) -> bool {
        if !self.is_done() {
            return true;
        }
        let done_at = self.done_at.lock().unwrap();
        match *done_at {
            Some(at) => now.duration_since(at) <= COMPLETED_RETENTION_GRACE,
            None => true,
        }
    }

    /// Attempt to claim the single-attach slot. Returns true on success;
    /// false if another client is already attached OR the reaper has
    /// claimed a terminal kill. Resets escalation flags on success so a
    /// fresh disconnect starts a fresh reap clock.
    pub(in crate::commands::serve) async fn mark_connected(&self) -> bool {
        let mut s = self.attach.lock().await;
        if s.connected || s.reaping_kill || s.escalating {
            return false;
        }
        s.connected = true;
        s.last_disconnect_at = None;
        s.signaled_hup = false;
        s.signaled_term = false;
        true
    }

    pub(in crate::commands::serve) async fn mark_disconnected(&self) {
        let mut s = self.attach.lock().await;
        s.connected = false;
        s.last_disconnect_at = Some(Instant::now());
    }

    /// Set the terminal reaping flag so mark_connected() rejects.
    /// Used by the hard-cap kill path which bypasses the escalation
    /// state machine.
    pub(in crate::commands::serve) async fn mark_reaping_kill(&self) {
        let mut s = self.attach.lock().await;
        s.reaping_kill = true;
    }

    async fn is_reaping_kill(&self) -> bool {
        let s = self.attach.lock().await;
        s.reaping_kill
    }

    /// Reaper: atomically try to escalate to SIGHUP. Sets `escalating`
    /// to block concurrent `mark_connected()` during signal delivery.
    /// Returns `true` if the transition was taken; `false` if skipped.
    async fn try_escalate_hup(&self, now: Instant, reconnect_grace: std::time::Duration) -> bool {
        let mut s = self.attach.lock().await;
        if s.connected || s.signaled_hup || s.reaping_kill || s.escalating {
            return false;
        }
        let Some(disc) = s.last_disconnect_at else {
            return false;
        };
        if now.duration_since(disc) <= reconnect_grace {
            return false;
        }
        s.signaled_hup = true;
        s.escalating = true;
        s.last_disconnect_at = Some(now);
        true
    }

    /// Reaper: atomically try to escalate to SIGTERM.
    async fn try_escalate_term(&self, now: Instant, shutdown_grace: std::time::Duration) -> bool {
        let mut s = self.attach.lock().await;
        if s.connected || !s.signaled_hup || s.signaled_term || s.reaping_kill || s.escalating {
            return false;
        }
        let Some(disc) = s.last_disconnect_at else {
            return false;
        };
        if now.duration_since(disc) <= shutdown_grace {
            return false;
        }
        s.signaled_term = true;
        s.escalating = true;
        s.last_disconnect_at = Some(now);
        true
    }

    /// Reaper: atomically try to escalate to SIGKILL. Once this returns
    /// `true`, `mark_connected()` will reject — the exec is doomed.
    async fn try_escalate_kill(&self, now: Instant, shutdown_grace: std::time::Duration) -> bool {
        let mut s = self.attach.lock().await;
        if s.connected || !s.signaled_term || s.reaping_kill {
            return false;
        }
        let Some(disc) = s.last_disconnect_at else {
            return false;
        };
        if now.duration_since(disc) <= shutdown_grace {
            return false;
        }
        s.reaping_kill = true;
        true
    }

    /// Clear the `escalating` flag after successful signal delivery.
    async fn finish_escalation(&self) {
        let mut s = self.attach.lock().await;
        s.escalating = false;
    }

    /// Atomically mark the exec as doomed AND clear escalating. Used when
    /// signal delivery fails during escalation — ensures no gap between
    /// clearing escalating and setting reaping_kill where mark_connected
    /// could slip through.
    async fn escalation_failed_mark_doomed(&self) {
        let mut s = self.attach.lock().await;
        s.escalating = false;
        s.reaping_kill = true;
    }
}

// ============================================================================
// Phase 5.7 — Orphan reaper
// ============================================================================

const REAPER_TICK: std::time::Duration = std::time::Duration::from_secs(30);
const REAPER_SIGNAL_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);
/// Budget for one runtime call on the lifecycle sweep.
///
/// Stopping or removing a box is heavier than signalling an exec, so it gets a
/// wider budget than `REAPER_SIGNAL_TIMEOUT` — but it is still bounded: the
/// sweep shares its task with orphan-exec reaping, so an unbounded await on a
/// wedged microVM would stall that too. A call that times out leaves the
/// deadline unmet and the next tick retries it.
///
/// This bounds each call, not the pass: boxes are swept one at a time, as the
/// orphan reaper walks its own candidates, so a tick can still run long.
const LIFECYCLE_OP_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(20);
const DEFAULT_RECONNECT_GRACE: std::time::Duration = std::time::Duration::from_secs(300);
const DEFAULT_SHUTDOWN_GRACE: std::time::Duration = std::time::Duration::from_secs(30);
const DEFAULT_MAX_SESSION_LIFETIME: std::time::Duration = std::time::Duration::from_secs(24 * 3600);
const COMPLETED_RETENTION_GRACE: std::time::Duration = std::time::Duration::from_secs(5 * 60);

/// Minimal duration parser: accepts `<n>s`, `<n>m`, `<n>h`, or a bare
/// integer interpreted as seconds. Mirrors Go's `time.ParseDuration` for
/// the cases we actually use. Returns `fallback` on any error or unset
/// env var, logging a warning so operators don't silently inherit the
/// default.
fn resolve_duration(var: &str, fallback: std::time::Duration) -> std::time::Duration {
    let raw = match std::env::var(var) {
        Ok(s) if !s.is_empty() => s,
        _ => return fallback,
    };
    let parsed = if let Some(rest) = raw.strip_suffix('h') {
        rest.parse::<u64>()
            .ok()
            .map(|n| std::time::Duration::from_secs(n * 3600))
    } else if let Some(rest) = raw.strip_suffix('m') {
        rest.parse::<u64>()
            .ok()
            .map(|n| std::time::Duration::from_secs(n * 60))
    } else if let Some(rest) = raw.strip_suffix('s') {
        rest.parse::<u64>().ok().map(std::time::Duration::from_secs)
    } else {
        raw.parse::<u64>().ok().map(std::time::Duration::from_secs)
    };
    match parsed {
        Some(d) => d,
        None => {
            tracing::warn!(env = var, value = %raw,
                "invalid duration env var (use Ns/Nm/Nh), using default");
            fallback
        }
    }
}

/// What the sweep should do with one box.
#[derive(Debug, PartialEq, Eq)]
enum LifecycleAction {
    Leave,
    Stop,
    Delete,
}

/// Decide one box's fate from its policy and observed clocks alone.
///
/// Split out from the sweep so the rule is testable without a VM, a runtime or
/// a clock. `0` disables either deadline, matching the wire contract.
fn decide_lifecycle(
    status: BoxStatus,
    auto_stop_secs: u32,
    auto_delete_secs: u32,
    idle: std::time::Duration,
    since_stop: std::time::Duration,
) -> LifecycleAction {
    // A running box is stopped once it has been idle for the whole window.
    if status == BoxStatus::Running
        && auto_stop_secs > 0
        && idle >= std::time::Duration::from_secs(u64::from(auto_stop_secs))
    {
        return LifecycleAction::Stop;
    }

    // A box that has come to rest is deleted once it has been at rest for the
    // whole window, measured from that transition rather than from last use: a
    // box that was busy right up to its stop must still age out on schedule.
    //
    // `Failed` counts as at rest — it will never run again on its own, so
    // excluding it would leak exactly the boxes nobody goes back to clean up.
    let at_rest = matches!(status, BoxStatus::Stopped | BoxStatus::Failed);
    if at_rest
        && auto_delete_secs > 0
        && since_stop >= std::time::Duration::from_secs(u64::from(auto_delete_secs))
    {
        return LifecycleAction::Delete;
    }

    LifecycleAction::Leave
}

/// How long a box has been idle, seeding the clock if this `serve` has not seen
/// it before.
///
/// The read guard must be released before the write lock is taken. Holding it
/// across the `.write().await` — which is what a `match` on the guard does,
/// since the scrutinee temporary outlives the arms — self-deadlocks on tokio's
/// `RwLock` and hangs the whole reaper loop, orphan-exec reaping included.
/// The clock is keyed by whatever spelling the caller used, so every stamp that
/// resolves to this box counts and the newest wins. `boxes` is the live set,
/// needed because resolving a prefix depends on what else exists. A box seen for
/// the first time is seeded under its canonical id.
async fn idle_or_seed(
    last_activity: &RwLock<HashMap<String, Instant>>,
    box_id: &str,
    boxes: &[(String, Option<String>)],
    now: Instant,
) -> std::time::Duration {
    let stamped = {
        let clocks = last_activity.read().await;
        clocks
            .iter()
            .filter(|(spelling, _)| box_named_by(spelling, boxes) == Some(box_id))
            .map(|(_, stamped)| *stamped)
            .max()
    };
    match stamped {
        Some(stamped) => now.saturating_duration_since(stamped),
        None => {
            last_activity.write().await.insert(box_id.to_string(), now);
            std::time::Duration::ZERO
        }
    }
}

/// The box a client's spelling resolves to, or `None` if it names none.
///
/// Mirrors `BoxManager::lookup_box` (litebox/manager.rs:102): an exact id, then
/// an exact name, then a prefix — and a prefix matching more than one box
/// resolves to nothing there, so it resolves to nothing here. The empty
/// spelling is the one divergence, rejected here rather than treated as a
/// prefix of everything; `activity_box_id` never yields one.
///
/// Resolving rather than enumerating, because prefixes cannot be listed: there
/// are as many as the id is long. Without this, `boxlite --url … exec 01HJK4`
/// would stamp a key the sweep never reads and its box would be AutoStopped
/// mid-use.
///
/// Rejecting the ambiguous prefix is what keeps that from becoming a lever.
/// `record_activity` stamps the raw path segment before any handler can reject
/// it, and `serve` may run with no API key — so if a one-character prefix
/// counted for every box it matched, `POST /v1/boxes/a/exec` would hold all of
/// them open, and 62 such requests a tick would pin the whole server.
fn box_named_by<'a>(spelling: &str, boxes: &'a [(String, Option<String>)]) -> Option<&'a str> {
    if spelling.is_empty() {
        return None;
    }
    if let Some((id, _)) = boxes.iter().find(|(id, _)| id == spelling) {
        return Some(id);
    }
    if let Some((id, _)) = boxes
        .iter()
        .find(|(_, name)| name.as_deref() == Some(spelling))
    {
        return Some(id);
    }
    let mut prefixed = boxes.iter().filter(|(id, _)| id.starts_with(spelling));
    match (prefixed.next(), prefixed.next()) {
        (Some((id, _)), None) => Some(id),
        _ => None,
    }
}

/// Boxes with work in flight, which AutoStop must not interrupt.
///
/// A tenant exec counts: `record_activity` stamps once per HTTP request, so a
/// long-running `POST /exec` never re-stamps and the box would look idle.
///
/// A `Main` session does not. It is the box's own init, it lives as long as the
/// box runs, and `run_reap_once` never evicts it — so counting it would pin
/// every started box and AutoStop could never fire at all. An idle box whose
/// init is still running is exactly what AutoStop exists to stop; an
/// *interactive* main session is kept alive by its data frames re-stamping the
/// clock instead.
fn busy_box_ids(
    executions: &HashMap<String, Arc<ActiveExecution>>,
) -> std::collections::HashSet<String> {
    executions
        .values()
        .filter(|execution| execution.kind() != SessionKind::Main && !execution.is_done())
        .map(|execution| execution.box_id.clone())
        .collect()
}

/// One lifecycle pass: stop idle boxes, delete boxes that have been at rest
/// long enough. Runs on the same tick as the orphan-exec reaper.
async fn run_lifecycle_once(state: &AppState, now: Instant) {
    let boxes = match tokio::time::timeout(LIFECYCLE_OP_TIMEOUT, state.runtime.list_info()).await {
        Ok(Ok(boxes)) => boxes,
        Ok(Err(error)) => {
            tracing::warn!(%error, "lifecycle sweep could not list boxes");
            return;
        }
        Err(_) => {
            tracing::warn!("lifecycle sweep timed out listing boxes");
            return;
        }
    };

    let busy = busy_box_ids(&*state.executions.read().await);

    // The live set every recorded spelling is resolved against. A client may
    // address a box as its canonical id, its name, or an unambiguous id prefix,
    // and the middleware stamps whichever was written — so `box_named_by`
    // resolves a spelling to the box it names, rather than trying to list a
    // box's spellings, which is impossible for prefixes. Resolving a prefix
    // needs the whole set, since ambiguity is a property of it.
    let live: Vec<(String, Option<String>)> = boxes
        .iter()
        .map(|info| (info.id.to_string(), info.name.clone()))
        .collect();
    state.retain_known_boxes(&live).await;

    let now_utc = chrono::Utc::now();
    for info in boxes {
        let box_id = info.id.to_string();
        // The policy lives here, not on `BoxInfo`: neither deadline is handed
        // to the runtime, so neither comes back from it.
        let policy = state.lifecycle_of(&box_id).await;
        if policy.is_empty() {
            continue;
        }
        if busy
            .iter()
            .any(|spelling| box_named_by(spelling, &live) == Some(box_id.as_str()))
        {
            continue;
        }

        let idle = idle_or_seed(&state.last_activity, &box_id, &live, now).await;
        // The delete deadline uses `last_updated`: for a box at rest that
        // transition *is* the stop, which is the anchor AutoDelete wants.
        let since_stop = (now_utc - info.last_updated)
            .to_std()
            .unwrap_or(std::time::Duration::ZERO);

        match decide_lifecycle(
            info.status,
            policy.auto_stop,
            policy.auto_delete,
            idle,
            since_stop,
        ) {
            LifecycleAction::Leave => {}
            LifecycleAction::Stop => {
                tracing::info!(
                    box_id = %box_id,
                    idle_secs = idle.as_secs(),
                    auto_stop = policy.auto_stop,
                    "AutoStop deadline reached, stopping box"
                );
                match tokio::time::timeout(LIFECYCLE_OP_TIMEOUT, state.runtime.get(&box_id)).await {
                    Ok(Ok(Some(bx))) => {
                        match tokio::time::timeout(LIFECYCLE_OP_TIMEOUT, bx.stop()).await {
                            Ok(Ok(())) => {}
                            Ok(Err(error)) => {
                                tracing::warn!(box_id = %box_id, %error, "AutoStop failed to stop box");
                            }
                            Err(_) => {
                                tracing::warn!(box_id = %box_id, "AutoStop timed out stopping box");
                            }
                        }
                    }
                    Ok(Ok(None)) => {}
                    Ok(Err(error)) => {
                        tracing::warn!(box_id = %box_id, %error, "AutoStop could not fetch box")
                    }
                    Err(_) => {
                        tracing::warn!(box_id = %box_id, "AutoStop timed out fetching box")
                    }
                }
            }
            LifecycleAction::Delete => {
                tracing::info!(
                    box_id = %box_id,
                    at_rest_secs = since_stop.as_secs(),
                    auto_delete = policy.auto_delete,
                    "AutoDelete deadline reached, removing box"
                );
                // Evict the cached handle first, as `remove_box` does: a removed
                // box is never fetched again, so nothing else would drop it.
                state.boxes.write().await.remove(&box_id);
                match tokio::time::timeout(
                    LIFECYCLE_OP_TIMEOUT,
                    state.runtime.remove(&box_id, false),
                )
                .await
                {
                    Ok(Ok(())) => state.forget_box(&box_id).await,
                    Ok(Err(error)) => {
                        tracing::warn!(box_id = %box_id, %error, "AutoDelete failed to remove box");
                    }
                    Err(_) => {
                        tracing::warn!(box_id = %box_id, "AutoDelete timed out removing box");
                    }
                }
            }
        }
    }
}

async fn reaper_loop(state: Arc<AppState>) {
    let reconnect_grace = resolve_duration("BOXLITE_RECONNECT_GRACE", DEFAULT_RECONNECT_GRACE);
    let shutdown_grace = resolve_duration("BOXLITE_SHUTDOWN_GRACE", DEFAULT_SHUTDOWN_GRACE);
    let max_lifetime =
        resolve_duration("BOXLITE_MAX_SESSION_LIFETIME", DEFAULT_MAX_SESSION_LIFETIME);

    let mut ticker = tokio::time::interval(REAPER_TICK);
    ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
    loop {
        ticker.tick().await;
        let now = Instant::now();
        run_reap_once(&state, now, reconnect_grace, shutdown_grace, max_lifetime).await;
        run_lifecycle_once(&state, now).await;
    }
}

async fn run_reap_once(
    state: &AppState,
    now: Instant,
    reconnect_grace: std::time::Duration,
    shutdown_grace: std::time::Duration,
    max_lifetime: std::time::Duration,
) {
    let candidates: Vec<(String, Arc<ActiveExecution>)> = {
        let map = state.executions.read().await;
        map.iter()
            .map(|(k, v)| (k.clone(), Arc::clone(v)))
            .collect()
    };

    for (id, active) in candidates {
        // Done check first — a completed exec is always handled by the
        // retention path, even if it exceeds the lifetime cap. This
        // avoids starving done-eviction when try_kill_and_evict keeps
        // failing for an already-exited process.
        if active.is_done() {
            if !active.should_retain(now) {
                // Compare-and-remove, for the same reason as `try_kill_and_evict`:
                // a restarted box re-registers its main session under the same
                // container id, and this snapshot may already be stale.
                evict_if_same(state, &id, &active).await;
            }
            continue;
        }

        // A main session is never an orphan and never stale: it is the box's
        // init, so killing it powers the VM off and destroys the box. A client
        // walking away from `docker attach` does not stop the container, and
        // neither may this; nor may the lifetime cap, which exists to bound
        // *exec* sessions, not the workload they run beside.
        //
        // Every branch below signals or kills, so Main stops here. The
        // done-eviction above still applies to it — a main session whose init
        // has exited really is finished, and evicting a dead entry kills
        // nothing.
        if active.kind() == SessionKind::Main {
            continue;
        }

        if now.duration_since(active.created_at()) > max_lifetime {
            active.mark_reaping_kill().await;
            tracing::warn!(exec_id = %id, "session lifetime cap reached, killing");
            try_kill_and_evict(state, &id, &active).await;
            continue;
        }
        // Retry kill for entries already marked doomed by a prior tick
        // or a failed DELETE handler.
        if active.is_reaping_kill().await {
            try_kill_and_evict(state, &id, &active).await;
            continue;
        }
        if active.try_escalate_hup(now, reconnect_grace).await {
            let sig_result =
                tokio::time::timeout(REAPER_SIGNAL_TIMEOUT, active.execution().signal(1)).await;
            if matches!(sig_result, Ok(Ok(()))) {
                active.finish_escalation().await;
            } else {
                tracing::warn!(exec_id = %id, "SIGHUP delivery failed or timed out, killing");
                active.escalation_failed_mark_doomed().await;
                try_kill_and_evict(state, &id, &active).await;
            }
        } else if active.try_escalate_term(now, shutdown_grace).await {
            let sig_result =
                tokio::time::timeout(REAPER_SIGNAL_TIMEOUT, active.execution().signal(15)).await;
            if matches!(sig_result, Ok(Ok(()))) {
                active.finish_escalation().await;
            } else {
                tracing::warn!(exec_id = %id, "SIGTERM delivery failed or timed out, killing");
                active.escalation_failed_mark_doomed().await;
                try_kill_and_evict(state, &id, &active).await;
            }
        } else if active.try_escalate_kill(now, shutdown_grace).await {
            tracing::warn!(exec_id = %id, "orphan exec did not exit after SIGTERM, killing");
            try_kill_and_evict(state, &id, &active).await;
        }
    }
}

/// Kill and remove from the map. Only evicts on kill success; on failure
/// the entry stays with `reaping_kill=true` so the next reaper tick retries.
async fn try_kill_and_evict(state: &AppState, id: &str, active: &Arc<ActiveExecution>) {
    let result = tokio::time::timeout(REAPER_SIGNAL_TIMEOUT, active.execution().kill()).await;
    match result {
        Ok(Ok(())) => {
            evict_if_same(state, id, active).await;
        }
        Ok(Err(e)) => {
            tracing::warn!(exec_id = %id, err = %e, "kill failed, will retry next tick");
        }
        Err(_) => {
            tracing::warn!(exec_id = %id, "kill timed out, will retry next tick");
        }
    }
}

/// Remove `id` only if it still maps to the session we decided to reap.
///
/// The reaper works from a snapshot taken under an earlier read lock and awaits
/// in between (a kill can block for `REAPER_SIGNAL_TIMEOUT`), so by the time it
/// evicts, the key may have been rebound. That is not hypothetical for the main
/// session: its id *is* the container id, which is fixed at box creation, so a
/// box that restarts re-registers a brand-new session under the very same key.
/// A bare `remove(id)` would then delete the live session out from under its
/// client — and the client could not recover, because the guest refuses a second
/// `Attach` on a session that already has one.
async fn evict_if_same(state: &AppState, id: &str, doomed: &Arc<ActiveExecution>) {
    let mut map = state.executions.write().await;
    if let Some(current) = map.get(id)
        && Arc::ptr_eq(current, doomed)
    {
        map.remove(id);
    }
}

// ============================================================================
// Conversions
// ============================================================================

fn box_info_to_response(info: &BoxInfo) -> BoxResponse {
    BoxResponse {
        box_id: info.id.to_string(),
        name: info.name.clone(),
        status: info.status.as_str().to_string(),
        created_at: info.created_at.to_rfc3339(),
        updated_at: info.last_updated.to_rfc3339(),
        pid: info.pid,
        image: info.image.clone(),
        cpus: info.cpus,
        memory_mib: info.memory_mib,
        labels: info.labels.clone(),
        auto_stop: info.auto_stop,
        auto_delete: info.auto_delete,
        auto_resume: info.auto_resume,
        exit_code: info.exit_code,
    }
}

fn volume_info_to_response(info: &boxlite::runtime::types::VolumeInfo) -> types::VolumeResponse {
    types::VolumeResponse {
        id: info.id.clone(),
        name: info.name.clone(),
        created_at: info.created_at.to_rfc3339(),
        size_bytes: info.size_bytes,
    }
}

fn build_box_options(req: &CreateBoxRequest) -> Result<BoxOptions, boxlite::BoxliteError> {
    let rootfs = if let Some(ref path) = req.rootfs_path {
        RootfsSpec::RootfsPath(path.clone())
    } else {
        RootfsSpec::Image(req.image.clone().unwrap_or_else(|| "alpine:latest".into()))
    };

    let env: Vec<(String, String)> = req
        .env
        .as_ref()
        .map(|m| m.iter().map(|(k, v)| (k.clone(), v.clone())).collect())
        .unwrap_or_default();

    let (network, inbound_network) = match &req.network {
        Some(network) => {
            if network.uses_legacy_fields()
                && (network.outbound.is_some() || network.inbound.is_some())
            {
                return Err(boxlite::BoxliteError::InvalidArgument(
                    "network must use either nested outbound/inbound fields or legacy flat fields, not both"
                        .into(),
                ));
            }

            let (mode, allow_net) = match &network.outbound {
                Some(outbound) => (
                    outbound.mode.parse::<NetworkMode>()?,
                    outbound.allow_net.clone(),
                ),
                None => (
                    network
                        .legacy
                        .mode
                        .as_deref()
                        .unwrap_or("enabled")
                        .parse::<NetworkMode>()?,
                    network.legacy.allow_net.clone().unwrap_or_default(),
                ),
            };
            let (inbound_mode, inbound_allow_net) = match &network.inbound {
                Some(inbound) => (
                    inbound.mode.parse::<NetworkMode>()?,
                    inbound.allow_net.clone(),
                ),
                None => (NetworkMode::Disabled, Vec::new()),
            };
            (
                NetworkSpec::try_from(OutboundNetworkConfig { mode, allow_net })?,
                NetworkSpec::try_from(InboundNetworkConfig {
                    mode: inbound_mode,
                    allow_net: inbound_allow_net,
                })?,
            )
        }
        None => (NetworkSpec::default(), NetworkSpec::disabled()),
    };

    // SecurityOptions is deliberately NOT client-configurable over
    // REST: sandbox security is the operator's policy. The server
    // always uses `AdvancedBoxOptions::default()` for new boxes, so
    // the default-flip (jailer + seccomp on for Linux/macOS) applies
    // uniformly. Operators who want a different policy run the
    // server with a different default; clients cannot relax it.

    if let Some(volumes) = &req.volumes
        && !volumes.is_empty()
    {
        return Err(boxlite::BoxliteError::InvalidArgument(
            "managed volumes are not supported by boxlite serve".into(),
        ));
    }

    // An empty name or value can never substitute anything. Reject at the
    // boundary so this server agrees with the Cloud API's IsNotEmpty and the
    // runner's per-element `dive` required validation on what a secret is.
    if let Some(secrets) = &req.secrets
        && secrets
            .iter()
            .any(|s| s.name.is_empty() || s.value.is_empty())
    {
        return Err(boxlite::BoxliteError::InvalidArgument(
            "secret name and value must be non-empty".into(),
        ));
    }

    // Map secrets onto the core `Secret` type and apply the placeholder
    // default. The local runtime does not synthesize `<BOXLITE_SECRET:{name}>`
    // for an empty placeholder (unlike the Go SDK), so defaulting here is what
    // keeps a placeholder-less secret from silently injecting an empty env var
    // and no MITM substitution — the same failure class POL-303 fixes on Cloud.
    let secrets: Vec<boxlite::runtime::options::Secret> = req
        .secrets
        .as_ref()
        .map(|ss| {
            ss.iter()
                .map(|s| boxlite::runtime::options::Secret {
                    name: s.name.clone(),
                    value: s.value.clone(),
                    hosts: s.hosts.clone(),
                    placeholder: s
                        .placeholder
                        .clone()
                        .filter(|p| !p.is_empty())
                        .unwrap_or_else(|| format!("<BOXLITE_SECRET:{}>", s.name)),
                })
                .collect()
        })
        .unwrap_or_default();

    Ok(BoxOptions {
        rootfs,
        cpus: req.cpus,
        memory_mib: req.memory_mib,
        disk_size_gb: req.disk_size_gb,
        working_dir: req.working_dir.clone(),
        env,
        secrets,
        network,
        inbound_network,
        entrypoint: req.entrypoint.clone(),
        cmd: req.cmd.clone(),
        user: req.user.clone(),
        tty: req.tty.unwrap_or(false),
        advanced: {
            let mut advanced = boxlite::AdvancedBoxOptions::default();
            let capabilities = req.advanced.capabilities.as_ref().map(|capabilities| {
                boxlite::ContainerCapabilities {
                    add: capabilities.add.clone(),
                    drop: capabilities.drop.clone(),
                }
            });
            advanced.set_capabilities(capabilities)?;
            advanced
        },
        // Neither deadline is forwarded; `serve` holds both and sweeps them.
        //
        // `auto_stop` cannot be forwarded at all: the local runtime this server
        // drives refuses a non-zero value outright
        // (`reject_local_unsupported_options`, runtime/rt_impl.rs:1743), so a
        // create carrying one would 400 before anything could enforce it.
        //
        // `auto_delete` would be accepted but reinterpreted: `removes_on_stop()`
        // is `effective_auto_delete() > 0`, so the engine would delete the box
        // the moment it stopped rather than after the delay, and force it
        // non-detached. `create_box` records both in `AppState`.
        auto_stop: None,
        auto_delete: Some(0),
        auto_resume: req.auto_resume,
        // Boxes made over the wire outlive the request that made them.
        detach: req.detach.unwrap_or(true),
        ..Default::default()
    })
}

fn build_box_command(req: &ExecRequest) -> Result<BoxCommand, boxlite::BoxliteError> {
    let mut cmd = BoxCommand::new(&req.command).args(req.args.iter().map(String::as_str));

    if let Some(ref env_map) = req.env {
        for (k, v) in env_map {
            cmd = cmd.env(k, v);
        }
    }
    if let Some(ref wd) = req.working_dir {
        cmd = cmd.working_dir(wd);
    }
    if req.tty {
        cmd = cmd.tty(true);
    }
    if let Some(secs) = req.timeout_seconds {
        cmd = cmd.timeout_seconds(secs)?;
    }
    Ok(cmd)
}

// ============================================================================
// Error Helpers
// ============================================================================

/// Build a JSON error response with the canonical wire envelope.
///
/// `error_type` and `code` are caller-supplied because some sites
/// (auth middleware, handler timeout, schema-validation rejection) emit
/// errors that don't correspond to a `BoxliteError` variant. For
/// `BoxliteError` paths use [`error_from_boxlite`] instead — it dispatches
/// to the single source of truth in `BoxliteError::http()`.
fn error_response(
    status: StatusCode,
    message: impl Into<String>,
    error_type: &str,
    code: &str,
) -> Response {
    let body = ErrorBody {
        error: ErrorDetail {
            message: message.into(),
            error_type: error_type.to_string(),
            code: code.to_string(),
            request_id: None,
        },
    };
    (status, Json(body)).into_response()
}

/// Map a `BoxliteError` to its canonical HTTP response. Delegates the
/// (status, type, code) decision to `BoxliteError::http()` so the mapping
/// is exhaustive at compile time — adding a new variant becomes a build
/// error in `errors.rs`, never a silent 500.
fn error_from_boxlite(err: &boxlite::BoxliteError) -> Response {
    let (code, etype, ecode) = err.http();
    let status = StatusCode::from_u16(code)
        .expect("BoxliteError::http() must return a valid HTTP status code");
    error_response(status, err.to_string(), etype, ecode)
}

/// Panic handler for [`CatchPanicLayer`]. Turns a handler panic into a
/// `500 InternalError internal` response with our wire envelope —
/// otherwise axum's default returns an empty `500 Internal Server Error`
/// with no body, breaking the client's status-table 500-vs-Network
/// distinction.
fn handle_panic(err: Box<dyn std::any::Any + Send + 'static>) -> Response {
    let detail = err
        .downcast_ref::<&'static str>()
        .map(|s| s.to_string())
        .or_else(|| err.downcast_ref::<String>().cloned())
        .unwrap_or_else(|| "panic in handler".to_string());
    tracing::error!(panic = %detail, "handler panicked");
    error_response(
        StatusCode::INTERNAL_SERVER_ERROR,
        format!("internal error: {}", detail),
        "InternalError",
        "internal",
    )
}

/// Pure auth decision (unit-tested). `true` = allow. `expected == None` ⇒
/// permissive (no key configured). `GET /v1/config` is always public
/// (pre-auth capability discovery). Otherwise the presented bearer must
/// match `expected` (constant-time).
fn auth_allows(expected: Option<&str>, path: &str, bearer: Option<&str>) -> bool {
    let Some(expected) = expected else {
        return true;
    };
    if path == "/v1/config" {
        return true;
    }
    match bearer {
        Some(tok) => constant_time_eq(tok.as_bytes(), expected.as_bytes()),
        None => false,
    }
}

/// Whether a request path is user activity on a box, and if so which box.
///
/// Box-scoped paths count by default. Defaulting to "counts" means a newly
/// added box operation cannot silently become invisible to the idle clock and
/// get its box stopped mid-flight.
///
/// The exclusions are the paths a poller hits on a timer, which must never be
/// able to hold a box open forever:
///
/// - `/metrics`, scraped on a schedule;
/// - a bare `GET`/`HEAD` on the box, which reads metadata rather than using the
///   box — a client watching `status` once a second would otherwise reset the
///   idle window before every sweep and AutoStop would never fire;
/// - `DELETE`, which is removing the box, not using it.
///
/// `/stop` is excluded because it cannot matter: AutoStop ignores a box that is
/// already stopped, and AutoDelete measures from `last_updated`, not this clock.
fn activity_box_id<'a>(method: &Method, path: &'a str) -> Option<&'a str> {
    let rest = path.strip_prefix("/v1/boxes/")?;
    let (box_id, tail) = match rest.split_once('/') {
        Some((box_id, tail)) => (box_id, tail),
        None => (rest, ""),
    };
    if box_id.is_empty() || matches!(tail, "metrics" | "stop") {
        return None;
    }
    // A read of the box resource itself is metadata, not use.
    if tail.is_empty() && matches!(*method, Method::GET | Method::HEAD | Method::DELETE) {
        return None;
    }
    Some(box_id)
}

/// Stamp the idle clock for the box a request names, before it runs.
async fn record_activity(State(state): State<Arc<AppState>>, req: Request, next: Next) -> Response {
    if let Some(segment) = activity_box_id(req.method(), req.uri().path()) {
        // Decoded before it is stamped: the request path is raw, while the
        // sweep resolves it through `box_named_by`, against names that arrive
        // decoded. Any
        // escaped spelling would file the box's activity under a key the sweep
        // never queries, and the box would be AutoStopped while in continuous
        // use. A client may escape an unreserved character in a perfectly legal
        // name (`my%2Dbox` for `my-box`), so this does not rest on the contract's
        // `name` pattern being unenforced here — which it is.
        let box_id = percent_encoding::percent_decode_str(segment).decode_utf8_lossy();
        state.record_box_activity(&box_id).await;
    }
    next.run(req).await
}

/// Auth middleware: thin axum adapter over [`auth_allows`]. 401 in the
/// standard error shape when denied.
async fn require_api_key(State(state): State<Arc<AppState>>, req: Request, next: Next) -> Response {
    let bearer = req
        .headers()
        .get(AUTHORIZATION)
        .and_then(|v| v.to_str().ok())
        .and_then(|v| {
            v.strip_prefix("Bearer ")
                .or_else(|| v.strip_prefix("bearer "))
        });
    if auth_allows(state.api_key.as_deref(), req.uri().path(), bearer) {
        next.run(req).await
    } else {
        error_response(
            StatusCode::UNAUTHORIZED,
            "invalid or missing API key",
            "AuthError",
            "unauthenticated",
        )
    }
}

/// Length-checked constant-time byte compare — avoids a timing oracle on the
/// configured token without pulling in a crate.
fn constant_time_eq(a: &[u8], b: &[u8]) -> bool {
    if a.len() != b.len() {
        return false;
    }
    let mut diff = 0u8;
    for (x, y) in a.iter().zip(b.iter()) {
        diff |= x ^ y;
    }
    diff == 0
}

// ============================================================================
// Box Handle Cache Helper
// ============================================================================

/// Whether an operation that would wake this box is allowed to proceed.
///
/// The flag governs *resuming* — bringing back a box that ran and was stopped,
/// which is what AutoStop leaves behind. Only `Stopped` is that state.
///
/// Every other state is left alone deliberately. `Running`/`Paused` are already
/// up, so no wake is involved. A `Configured` box has never run, and its first
/// boot is not a resume: gating it would break `run --url`, which attaches
/// before it starts the box, so `run --no-auto-resume` would refuse itself.
/// `Failed` and `Stopping` are not states a caller asked to come back from.
fn autoresume_allows(status: BoxStatus, auto_resume: bool) -> bool {
    status != BoxStatus::Stopped || auto_resume
}

/// Resolve a box for an operation that drives the guest, refusing to wake it
/// implicitly when the caller disabled AutoResume.
///
/// Only the operations that would actually boot the box use this — exec, files,
/// attach. `start`/`stop`, snapshots, clone and export resolve through
/// `get_or_fetch_box`: they either are the explicit lifecycle call or work off
/// disk, and gating them would answer a metadata read with advice to start a box
/// the caller never asked to run. Metrics boots too but never resumes, so it has
/// its own resolver: see `get_box_for_metrics`.
#[allow(clippy::result_large_err)]
async fn get_or_resume_box(state: &AppState, box_id: &str) -> Result<Arc<LiteBox>, Response> {
    // `info()` reads persisted state and does not boot anything, so asking it
    // first cannot itself perform the wake we may be about to refuse.
    let info = match state.runtime.get_info(box_id).await {
        Ok(Some(info)) => info,
        Ok(None) => {
            return Err(error_response(
                StatusCode::NOT_FOUND,
                format!("box not found: {box_id}"),
                "NotFoundError",
                "not_found",
            ));
        }
        Err(e) => return Err(error_from_boxlite(&e)),
    };

    if !autoresume_allows(info.status, info.auto_resume) {
        return Err(error_response(
            StatusCode::CONFLICT,
            format!(
                "box {box_id} is {:?} and has AutoResume disabled; start it explicitly first",
                info.status
            ),
            "ConflictError",
            "conflict",
        ));
    }

    get_or_fetch_box(state, box_id).await
}

/// Resolve a box for a metrics scrape, refusing to wake one the sweep would put
/// straight back to sleep.
///
/// `serve` must not wake a box for an operation it does not count as use, and
/// metrics is the only such operation: `LiteBox::metrics()` boots a box that is
/// not running, while `activity_box_id` deliberately does not stamp the idle
/// clock for a scrape. Together, on a box with an AutoStop deadline, they are a
/// loop — the scrape wakes the box, the next tick finds it idle and stops it,
/// the scrape after that wakes it again, each wake re-running the image's init.
/// That is also what the guide has always promised: monitoring must not be able
/// to start a box, or to hold one open.
///
/// Only a box the sweep would stop is refused. One with no AutoStop deadline
/// keeps the historical boot-on-scrape: nothing would put it back to sleep, so
/// there is no loop to break and no reason to change what a scrape did before.
#[allow(clippy::result_large_err)]
async fn get_box_for_metrics(state: &AppState, box_id: &str) -> Result<Arc<LiteBox>, Response> {
    // `get_info` reads persisted state and boots nothing, so asking it first
    // cannot itself perform the wake we may be about to refuse.
    let info = match state.runtime.get_info(box_id).await {
        Ok(Some(info)) => info,
        Ok(None) => {
            return Err(error_response(
                StatusCode::NOT_FOUND,
                format!("box not found: {box_id}"),
                "NotFoundError",
                "not_found",
            ));
        }
        Err(e) => return Err(error_from_boxlite(&e)),
    };

    // The deadline is looked up by the box's own id, never by the spelling the
    // request used. The path segment may be a user-defined name (the wire
    // contract says "the identifier, or a user-defined name"), while `create_box`
    // only ever files the policy under the canonical id — so keying off the
    // segment would read no deadline for `/v1/boxes/{name}/metrics` and wake the
    // very box this exists to leave asleep. The sweep resolves the same aliasing
    // from the other side, in `box_named_by`.
    let policy = state.lifecycle_of(info.id.as_ref()).await;
    if metrics_would_thrash(info.status, policy.auto_stop) {
        let status = info.status;
        return Err(error_response(
            StatusCode::CONFLICT,
            format!(
                "box {box_id} is {status:?} and AutoStop is enabled; a metrics scrape does not \
                 start a box, start it explicitly first"
            ),
            "ConflictError",
            "conflict",
        ));
    }

    get_or_fetch_box(state, box_id).await
}

/// Whether serving metrics would wake a box the sweep would then stop again.
fn metrics_would_thrash(status: BoxStatus, auto_stop_secs: u32) -> bool {
    status != BoxStatus::Running && auto_stop_secs > 0
}

#[allow(clippy::result_large_err)]
async fn get_or_fetch_box(state: &AppState, box_id: &str) -> Result<Arc<LiteBox>, Response> {
    // Check cache first.
    //
    // A cached handle is only good while its box is up. A box can now stop
    // *itself* — its main command exits and the guest powers the VM off — and
    // such a handle is spent: it holds the dead VM and can never boot another.
    // The runtime's own cache is invalidated by the exit watcher, but this one is
    // ours, and nothing was clearing it. Serving from it would answer every later
    // `/exec`, `/files` and `/attach` on that box with a corpse, forever.
    //
    // So a non-Running box gets a fresh handle, which *can* boot it. That is the
    // auto-restart the cloud depends on: its reaper stops idle boxes and the next
    // SDK call is expected to bring them back.
    let cached = state.boxes.read().await.get(box_id).cloned();
    if let Some(cached) = cached {
        match cached.info().await {
            Ok(info) if info.status.is_active() => return Ok(Arc::clone(&cached)),
            Ok(_) => {}
            Err(e) => return Err(error_from_boxlite(&e)),
        }
        let mut boxes = state.boxes.write().await;
        if boxes
            .get(box_id)
            .is_some_and(|current| Arc::ptr_eq(current, &cached))
        {
            boxes.remove(box_id);
        }
    }

    // Fetch from runtime
    match state.runtime.get(box_id).await {
        Ok(Some(b)) => {
            let id = b
                .info()
                .await
                .map_err(|e| error_from_boxlite(&e))?
                .id
                .to_string();
            let arc = Arc::new(b);
            state.boxes.write().await.insert(id, Arc::clone(&arc));
            Ok(arc)
        }
        Ok(None) => Err(error_response(
            StatusCode::NOT_FOUND,
            format!("box not found: {box_id}"),
            "NotFoundError",
            "not_found",
        )),
        Err(e) => Err(error_from_boxlite(&e)),
    }
}

// ============================================================================
// Main Session (container init)
// ============================================================================

/// Find a box's already-open main session in the registry.
///
/// Linear, because the registry is keyed by execution id and the main
/// session's id (the container id) is exactly what the caller doesn't
/// know — see [`SessionKind`]. The registry holds one entry per live
/// session on this server, so the scan is bounded by that, and it only
/// runs on attach.
fn find_main_session(
    executions: &HashMap<String, Arc<ActiveExecution>>,
    box_id: &str,
) -> Option<Arc<ActiveExecution>> {
    executions
        .values()
        // `is_done()` matters here in a way it never did for execs. A main
        // session's id is the container id, which is fixed at box creation and
        // is therefore the *same across reboots* — so a finished session from
        // the previous run would still match this box, and an attach after a
        // restart would be handed the old VM's dead stream, its stale backlog
        // and its stale exit code. An exec cannot collide that way: it gets a
        // fresh id every time.
        .find(|active| {
            active.box_id() == box_id && active.kind() == SessionKind::Main && !active.is_done()
        })
        .cloned()
}

/// Return the box's main session, calling `open` to create it only if the
/// box does not have one yet.
///
/// `open` runs at most once per box while the session is registered: the
/// guest binds init's stdout/stderr to the *first* `Attach` RPC and
/// answers later ones with `already_exists` (guest `ExecState::attach`),
/// so a second `LiteBox::attach()` would hand back a permanently silent
/// `Execution` — and, whichever one won the registry, leave a client
/// attached to a dead stream. The caller therefore holds the registry
/// write lock across this whole function, which is what makes the
/// check-then-open atomic against a concurrent first attach.
async fn register_main_session<F, Fut>(
    executions: &mut HashMap<String, Arc<ActiveExecution>>,
    box_id: &str,
    open: F,
) -> Result<Arc<ActiveExecution>, boxlite::BoxliteError>
where
    F: FnOnce() -> Fut,
    Fut: std::future::Future<Output = Result<Execution, boxlite::BoxliteError>>,
{
    if let Some(active) = find_main_session(executions, box_id) {
        return Ok(active);
    }

    let mut execution = open().await?;
    let stdin = execution.stdin();
    let exec_id = execution.id().clone();
    let active = ActiveExecution::new(box_id.to_string(), SessionKind::Main, execution, stdin);
    executions.insert(exec_id, Arc::clone(&active));
    Ok(active)
}

/// Get the box's main session for `GET /boxes/{box_id}/attach`, opening it
/// on the first attach.
///
/// A cold box is now booted *inside* the registry write lock, which the earlier
/// design deliberately avoided. It has to be: the client must be attached before
/// the main command runs (create → attach → start), so the attach is no longer
/// separable from the boot, and the check-then-open has to stay atomic — the
/// guest refuses a second Attach on one session, so a racing client would end up
/// holding a permanently silent `Execution`. The cost is stated plainly at the
/// call site and it is not small: a cold boot pulls the image, which is
/// unbounded, and everything else queues behind it.
///
/// It deliberately does NOT call `start()` first. `start()` on a *Stopped* box
/// runs the restart pipeline, and the box's init is the user's main command —
/// so attaching to a finished job would silently run the job again. `attach()`
/// already does the right thing for every status by itself: it boots a
/// `Configured` box (which has never run) and refuses a `Stopped` one.
#[allow(clippy::result_large_err)]
async fn get_or_attach_main_session(
    state: &AppState,
    box_id: &str,
) -> Result<Arc<ActiveExecution>, Response> {
    if let Some(active) = find_main_session(&*state.executions.read().await, box_id) {
        return Ok(active);
    }

    let litebox = get_or_resume_box(state, box_id).await?;

    // Attaching boots the box (creating its container) and subscribes to the main
    // command's session, but does *not* run init — `POST /start` does. So a client
    // mid `run --url` is registered here, on the stream, before it starts the box:
    // docker's create → attach → start, split across the two calls it makes.
    //
    // This runs under the registry write lock, and booting a cold box pulls its
    // image, which is unbounded, so every other `/exec`, `/attach` and the reaper
    // wait behind it. The lock buys the atomic check-then-open that stops two
    // clients opening two guest streams for one session — the guest refuses a
    // second Attach, so the loser would get a permanently silent Execution. A
    // per-box open lock would scope that to the same box; worth doing, not here.
    let mut executions = state.executions.write().await;
    register_main_session(&mut executions, box_id, || async {
        litebox.attach(boxlite::AttachOptions::main()).await
    })
    .await
    .map_err(|e| error_from_boxlite(&e))
}

// ============================================================================
// Router
// ============================================================================

fn build_router(state: Arc<AppState>) -> Router {
    use handlers::{advanced, boxes, config, executions, files, me, metrics, snapshots, volumes};

    Router::new()
        // Identity (no tenant prefix)
        .route("/v1/me", get(me::get_me))
        .route("/v1/config", get(config::get_config))
        // Runtime metrics
        .route("/v1/metrics", get(metrics::runtime_metrics))
        // Named volumes
        .route(
            "/v1/volumes",
            post(volumes::create_volume).get(volumes::list_volumes),
        )
        .route(
            "/v1/volumes/{id}",
            get(volumes::get_volume).delete(volumes::remove_volume),
        )
        // Box CRUD (import first — static path before param path)
        .route("/v1/boxes/import", post(advanced::import_box))
        .route(
            "/v1/boxes",
            post(boxes::create_box).get(boxes::list_boxes),
        )
        .route(
            "/v1/boxes/{box_id}",
            get(boxes::get_box)
                .delete(boxes::remove_box)
                .head(boxes::head_box),
        )
        // Box lifecycle
        .route(
            "/v1/boxes/{box_id}/start",
            post(boxes::start_box),
        )
        .route(
            "/v1/boxes/{box_id}/stop",
            post(boxes::stop_box),
        )
        // Box metrics
        .route(
            "/v1/boxes/{box_id}/metrics",
            get(metrics::box_metrics),
        )
        // Main command session (container init) — docker's
        // `POST /containers/{id}/attach`, distinct from exec-attach below.
        .route(
            "/v1/boxes/{box_id}/attach",
            get(executions::attach_box),
        )
        // Execution
        .route(
            "/v1/boxes/{box_id}/exec",
            post(executions::start_execution),
        )
        .route(
            "/v1/boxes/{box_id}/executions/{exec_id}",
            get(executions::get_execution).delete(executions::kill_execution),
        )
        .route(
            "/v1/boxes/{box_id}/executions/{exec_id}/attach",
            get(executions::attach_execution),
        )
        .route(
            "/v1/boxes/{box_id}/executions/{exec_id}/signal",
            post(executions::send_signal),
        )
        .route(
            "/v1/boxes/{box_id}/executions/{exec_id}/resize",
            post(executions::resize_tty),
        )
        // Files
        .route(
            "/v1/boxes/{box_id}/files",
            put(files::upload_files).get(files::download_files),
        )
        // Snapshots
        .route(
            "/v1/boxes/{box_id}/snapshots",
            post(snapshots::create_snapshot).get(snapshots::list_snapshots),
        )
        .route(
            "/v1/boxes/{box_id}/snapshots/{name}",
            get(snapshots::get_snapshot).delete(snapshots::delete_snapshot),
        )
        .route(
            "/v1/boxes/{box_id}/snapshots/{name}/restore",
            post(snapshots::restore_snapshot),
        )
        // Clone & export
        .route(
            "/v1/boxes/{box_id}/clone",
            post(advanced::clone_box),
        )
        .route(
            "/v1/boxes/{box_id}/export",
            post(advanced::export_box),
        )
        // Applied before `require_api_key`, which therefore wraps it: an
        // unauthenticated request must not be able to hold someone else's box
        // open by resetting its idle clock.
        .layer(middleware::from_fn_with_state(
            state.clone(),
            record_activity,
        ))
        .layer(middleware::from_fn_with_state(
            state.clone(),
            require_api_key,
        ))
        // Middleware stack (outermost first, applied in reverse):
        // 1. SetRequestIdLayer — read X-Request-Id from request, or mint
        //    a UUID. Stored in request extensions for downstream handlers
        //    and tracing spans.
        // 2. PropagateRequestIdLayer — copy the request-id onto the
        //    response headers so clients can correlate to server logs.
        // 3. CatchPanicLayer — handler panic ⇒ 500 with our envelope.
        //    Without this, axum returns an empty 500 which the client
        //    mis-classifies as a proxy/Network error.
        //
        // Skipped (intentionally): TimeoutLayer. boxlite handlers have
        // operation-specific timeouts (signal/kill use 10s, image pulls
        // can legitimately take minutes). A global request timeout would
        // break long-running ops.
        .layer(CatchPanicLayer::custom(handle_panic))
        .layer(PropagateRequestIdLayer::x_request_id())
        .layer(SetRequestIdLayer::x_request_id(MakeRequestUuid))
        .with_state(state)
}

// ============================================================================
// Entry Point
// ============================================================================

pub async fn execute(args: ServeArgs, global: &GlobalFlags) -> anyhow::Result<()> {
    // A server owns an embedded runtime. Client connection settings must not
    // turn it into a proxy merely because a credential profile or REST URL is
    // present in the environment.
    let options = global.resolve_runtime_options()?;
    let runtime = global.create_runtime_with_options(options)?;

    let state = Arc::new(AppState {
        runtime,
        boxes: RwLock::new(HashMap::new()),
        executions: RwLock::new(HashMap::new()),
        api_key: args.api_key.clone(),
        lifecycle: RwLock::new(HashMap::new()),
        last_activity: RwLock::new(HashMap::new()),
    });

    // Phase 5.7: spawn the orphan reaper. Same escalation policy as the
    // Go runner — 5min SIGHUP, +30s SIGTERM, +30s SIGKILL, 24h cap.
    tokio::spawn(reaper_loop(Arc::clone(&state)));

    let app = build_router(state.clone());
    let listener = tokio::net::TcpListener::bind((args.host.as_str(), args.port)).await?;
    let addr = listener.local_addr()?;

    tracing::info!("boxlite serve listening on {}", addr);
    eprintln!("BoxLite REST API server listening on http://{addr}");

    // Graceful shutdown on ctrl-c
    let shutdown_state = state.clone();
    axum::serve(listener, app)
        .with_graceful_shutdown(async move {
            let _ = tokio::signal::ctrl_c().await;
            tracing::info!("shutting down...");
            eprintln!("\nShutting down...");
            let _ = shutdown_state.runtime.shutdown(Some(10)).await;
        })
        .await?;

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use boxlite::runtime::options::NetworkSpec;
    use std::time::Duration;

    // --- API-key auth decision (pure; no runtime/network needed) ---

    #[test]
    fn auth_allows_permissive_when_no_key() {
        assert!(auth_allows(None, "/v1/boxes", None));
        assert!(auth_allows(None, "/v1/me", Some("anything")));
    }

    #[test]
    fn auth_allows_config_public_even_with_key() {
        assert!(auth_allows(Some("k"), "/v1/config", None));
    }

    #[test]
    fn auth_allows_requires_exact_bearer_when_key_set() {
        assert!(auth_allows(Some("k"), "/v1/me", Some("k")));
        assert!(!auth_allows(Some("k"), "/v1/me", Some("wrong")));
        assert!(!auth_allows(Some("k"), "/v1/me", None));
        assert!(!auth_allows(Some("k"), "/v1/boxes", Some("")));
    }

    #[test]
    fn constant_time_eq_basic() {
        assert!(constant_time_eq(b"abc", b"abc"));
        assert!(!constant_time_eq(b"abc", b"abd"));
        assert!(!constant_time_eq(b"abc", b"abcd"));
        assert!(constant_time_eq(b"", b""));
    }

    #[test]
    fn build_box_command_rejects_invalid_timeout_seconds() {
        for seconds in [-1.0, f64::NAN, f64::INFINITY] {
            let req = ExecRequest {
                command: "true".to_string(),
                args: Vec::new(),
                stdin: None,
                env: None,
                timeout_seconds: Some(seconds),
                working_dir: None,
                tty: false,
            };

            let err = build_box_command(&req).expect_err("invalid timeout should fail");
            assert!(
                matches!(err, boxlite::BoxliteError::InvalidArgument(ref msg) if msg.contains("timeout_seconds")),
                "unexpected error for {seconds:?}: {err}"
            );
        }
    }

    // ============================================================
    // REST `security` wire contract: server-owned only.
    //
    // The REST surface deliberately exposes no knob for clients to
    // pick a security preset or override `SecurityOptions`. Combined
    // with `#[serde(deny_unknown_fields)]` on `CreateBoxRequest`,
    // any client attempt to send `security` / `security_settings`
    // is rejected at deserialize time (i.e. 400 from the API)
    // rather than silently relaxing the server's policy.
    // ============================================================

    /// `-t` has to survive the wire, or a REST `run -it` silently gets pipes.
    ///
    /// The terminal belongs to the container's init, so it is decided at
    /// *create* and nothing downstream can add it. The client only sends the
    /// field when asked (the server rejects unknown fields), so both shapes
    /// must work: present-and-true, and absent.
    #[test]
    fn build_box_options_carries_tty_from_the_wire() {
        let with_tty: super::types::CreateBoxRequest =
            serde_json::from_str(r#"{"image": "alpine:latest", "tty": true}"#)
                .expect("body with tty must deserialize");
        assert!(
            build_box_options(&with_tty).expect("build").tty,
            "a REST client asking for a terminal must get one"
        );

        let without: super::types::CreateBoxRequest =
            serde_json::from_str(r#"{"image": "alpine:latest"}"#).expect("body must deserialize");
        assert!(
            !build_box_options(&without).expect("build").tty,
            "no tty asked for, none granted"
        );
    }

    #[test]
    fn build_box_options_carries_secrets_from_the_wire() {
        let req: super::types::CreateBoxRequest = serde_json::from_str(
            r#"{"image":"alpine:latest","secrets":[{"name":"openai","value":"sk-test","hosts":["api.openai.com"]}]}"#,
        )
        .expect("body with secrets must deserialize");

        let opts = build_box_options(&req).expect("build with secrets");
        assert_eq!(opts.secrets.len(), 1, "one secret in, one secret out");
        let secret = &opts.secrets[0];
        assert_eq!(secret.name, "openai");
        assert_eq!(secret.value, "sk-test");
        assert_eq!(secret.hosts, vec!["api.openai.com"]);
        // Placeholder omitted on the wire: serve applies the same default the
        // Go SDK does, so a placeholder-less secret still substitutes.
        assert_eq!(secret.placeholder, "<BOXLITE_SECRET:openai>");
    }

    #[test]
    fn build_box_options_preserves_explicit_secret_placeholder() {
        let req: super::types::CreateBoxRequest = serde_json::from_str(
            r#"{"image":"alpine:latest","secrets":[{"name":"httpbin","value":"v","placeholder":"<MY_TOKEN>"}]}"#,
        )
        .expect("body with explicit placeholder must deserialize");

        let opts = build_box_options(&req).expect("build");
        assert_eq!(
            opts.secrets[0].placeholder, "<MY_TOKEN>",
            "caller placeholder wins"
        );
    }

    #[test]
    fn build_box_options_defaults_an_empty_secret_placeholder() {
        let req: super::types::CreateBoxRequest = serde_json::from_str(
            r#"{"image":"alpine:latest","secrets":[{"name":"openai","value":"v","placeholder":""}]}"#,
        )
        .expect("body with empty placeholder must deserialize");

        let opts = build_box_options(&req).expect("build");
        assert_eq!(
            opts.secrets[0].placeholder, "<BOXLITE_SECRET:openai>",
            "an empty placeholder is as absent as an omitted one"
        );
    }

    #[test]
    fn build_box_options_rejects_an_empty_secret_name_or_value() {
        for secrets in [
            r#"[{"name":"","value":"v"}]"#,
            r#"[{"name":"n","value":""}]"#,
        ] {
            let req: super::types::CreateBoxRequest = serde_json::from_str(&format!(
                r#"{{"image":"alpine:latest","secrets":{secrets}}}"#
            ))
            .expect("body with secrets must deserialize");

            let err = build_box_options(&req).expect_err("empty secret fields must be rejected");
            assert!(
                matches!(err, boxlite::BoxliteError::InvalidArgument(ref msg) if msg.contains("non-empty")),
                "unexpected error: {err}"
            );
        }
    }

    #[test]
    fn build_box_options_rejects_nonempty_volumes() {
        let req: super::types::CreateBoxRequest = serde_json::from_str(
            r#"{"image":"alpine:latest","volumes":[{"managed_volume":"v1","guest_path":"/data"}]}"#,
        )
        .expect("body with volumes must deserialize (accepted, then rejected)");

        let err = build_box_options(&req).expect_err("non-empty volumes must be rejected");
        assert!(
            matches!(err, boxlite::BoxliteError::InvalidArgument(ref msg) if msg.contains("managed volumes")),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn build_box_options_carries_container_capabilities_from_the_wire() {
        let req: super::types::CreateBoxRequest = serde_json::from_str(
            r#"{"image":"alpine:latest","advanced":{"capabilities":{"add":["SYS_ADMIN"],"drop":["CAP_NET_RAW"]}}}"#,
        )
        .expect("capability request must deserialize");

        let opts = build_box_options(&req).expect("build capability options");
        let capabilities = opts.advanced.capabilities().expect("capabilities set");
        assert_eq!(capabilities.add, vec!["SYS_ADMIN"]);
        assert_eq!(capabilities.drop, vec!["CAP_NET_RAW"]);
    }

    /// A request that never mentions `advanced`/`capabilities` at all must
    /// resolve to `None` (unspecified), not an explicit empty policy — that
    /// distinction is what a privileged request needs, and what an older
    /// archive importer needs (`archive_version_for_options` keys off it).
    #[test]
    fn build_box_options_leaves_capabilities_unspecified_when_the_wire_omits_them() {
        let req: super::types::CreateBoxRequest =
            serde_json::from_str(r#"{"image":"alpine:latest"}"#)
                .expect("ordinary request must deserialize");

        let opts = build_box_options(&req).expect("build ordinary options");
        assert!(
            opts.advanced.capabilities().is_none(),
            "omitting capabilities on the wire must not become an explicit empty policy"
        );
    }

    #[test]
    fn build_box_options_carries_lifecycle_policy_and_uses_compatible_detach_default() {
        let req: super::types::CreateBoxRequest = serde_json::from_str(
            r#"{"auto_stop": 900, "auto_delete": 3600, "auto_resume": false}"#,
        )
        .expect("lifecycle body must deserialize");
        let opts = build_box_options(&req).expect("build lifecycle options");
        assert_eq!(
            opts.auto_stop, None,
            "the local runtime refuses a non-zero auto_stop outright, so \
             forwarding it would 400 the create"
        );
        assert_eq!(opts.auto_resume, Some(false));
        assert_eq!(
            opts.auto_delete,
            Some(0),
            "the deadline must not reach the runtime, which would read any \
             non-zero value as remove-on-stop and delete the box at its stop"
        );
        assert!(
            opts.detach,
            "withholding the deadline is what keeps a deadlined box detachable"
        );

        let persistent: super::types::CreateBoxRequest =
            serde_json::from_str(r#"{"auto_delete": 0}"#).expect("body must deserialize");
        assert!(
            build_box_options(&persistent).expect("build").detach,
            "persistent boxes keep the serve API's historical detached default"
        );
    }

    fn lifecycle_state() -> AppState {
        let runtime = BoxliteRuntime::rest(boxlite::BoxliteRestOptions::new(
            "http://127.0.0.1:1".to_string(),
        ))
        .expect("rest runtime");
        AppState {
            runtime,
            boxes: RwLock::new(HashMap::new()),
            executions: RwLock::new(HashMap::new()),
            api_key: None,
            lifecycle: RwLock::new(HashMap::new()),
            last_activity: RwLock::new(HashMap::new()),
        }
    }

    /// A `BoxInfo` shaped exactly as the runtime hands one back for a box
    /// created through `serve`: `build_box_options` withheld both deadlines, so
    /// the runtime recorded zeroes for them.
    fn info_as_the_runtime_reports_it(status: &str) -> BoxInfo {
        serde_json::from_value(serde_json::json!({
            "id": "01HJK4TNRPQSXYZ8WM6NCVT9R5",
            "name": "swept",
            "status": status,
            "created_at": "2026-01-01T00:00:00Z",
            "last_updated": "2026-01-01T00:00:00Z",
            "pid": null,
            "image": "alpine:latest",
            "cpus": 1,
            "memory_mib": 512,
            "labels": {},
            "auto_stop": 0,
            "auto_delete": 0,
            "auto_resume": true,
            "health_status": { "state": "None", "failures": 0, "last_check": null },
            "exit_code": null,
        }))
        .expect("box info")
    }

    /// Every box response must report the deadlines `serve` holds, not the
    /// zeroes it handed the runtime.
    ///
    /// `create_box` patching its own response is not enough: a client reading
    /// the box back — `GET`, the list, or the response to its own `stop` —
    /// would be told no deadline exists on a box the sweep is enforcing one
    /// for, and an SDK would copy that straight into its own `BoxInfo`.
    #[tokio::test]
    async fn a_box_response_reports_the_deadlines_serve_holds() {
        let state = lifecycle_state();
        let info = info_as_the_runtime_reports_it("stopped");
        assert_eq!(
            (info.auto_stop, info.auto_delete),
            (0, 0),
            "the runtime has no deadline to report; the whole point is that serve holds it"
        );

        state
            .set_lifecycle(
                info.id.as_ref(),
                LifecyclePolicy {
                    auto_stop: 900,
                    auto_delete: 604_800,
                },
            )
            .await;

        let resp = state.box_response(&info).await;
        assert_eq!(resp.auto_stop, 900, "AutoStop must survive the round trip");
        assert_eq!(
            resp.auto_delete, 604_800,
            "AutoDelete must survive the round trip"
        );
        assert!(
            resp.auto_resume,
            "auto_resume is forwarded to the runtime, so it is read off the info"
        );
    }

    /// A box `serve` holds no policy for reports no deadline — a clone or an
    /// import must not inherit the source box's.
    #[tokio::test]
    async fn a_box_with_no_stored_policy_reports_no_deadline() {
        let state = lifecycle_state();
        let resp = state
            .box_response(&info_as_the_runtime_reports_it("running"))
            .await;
        assert_eq!((resp.auto_stop, resp.auto_delete), (0, 0));
    }

    /// A metrics scrape must not wake a box the sweep would stop again.
    ///
    /// `LiteBox::metrics()` boots a box that is not running, and a scrape never
    /// stamps the idle clock — so on a box with an AutoStop deadline the two
    /// are a loop: scrape wakes, next tick stops, next scrape wakes. A box with
    /// no AutoStop deadline has nothing to put it back to sleep, so it keeps
    /// the historical boot-on-scrape.
    #[test]
    fn metrics_refuses_to_wake_only_a_box_the_sweep_would_stop() {
        for status in [
            BoxStatus::Unknown,
            BoxStatus::Configured,
            BoxStatus::Stopping,
            BoxStatus::Stopped,
            BoxStatus::Paused,
            BoxStatus::Failed,
        ] {
            assert!(
                metrics_would_thrash(status, 900),
                "{status:?} is not running, so serving metrics would boot it into the sweep"
            );
            assert!(
                !metrics_would_thrash(status, 0),
                "{status:?} has no AutoStop deadline, so nothing would stop it again"
            );
        }

        assert!(
            !metrics_would_thrash(BoxStatus::Running, 900),
            "a running box is not booted by a scrape, so there is no wake to refuse"
        );
        assert!(!metrics_would_thrash(BoxStatus::Running, 0));
    }

    /// The engine used to reject an invalid pair for us; now that neither
    /// deadline is forwarded, nothing behind this handler would. The runtime
    /// here points at a dead port on purpose: a 400 must come back without the
    /// request ever reaching `runtime.create`.
    #[tokio::test]
    async fn an_invalid_lifecycle_pair_is_refused_before_the_box_is_created() {
        let req: types::CreateBoxRequest =
            serde_json::from_str(r#"{"auto_stop": 3600, "auto_delete": 60}"#)
                .expect("body must deserialize");

        let response = handlers::boxes::create_box(
            axum::extract::State(Arc::new(lifecycle_state())),
            axum::Json(req),
        )
        .await;

        assert_eq!(
            response.status(),
            StatusCode::BAD_REQUEST,
            "auto_delete <= auto_stop is forbidden by the contract"
        );
    }

    #[tokio::test]
    async fn only_a_tenant_exec_marks_a_box_busy() {
        // Main is the box's own init: it lives as long as the box and
        // `run_reap_once` never evicts it, so counting it would pin every
        // started box and AutoStop could never fire. A tenant exec must count,
        // or a long `POST /exec` looks idle and is killed mid-run.
        let (main_exec, _main_ch) = stub_execution("cid-main");
        let (tenant, _tenant_ch) = stub_execution("cid-exec");

        let mut executions: HashMap<String, Arc<ActiveExecution>> = HashMap::new();
        executions.insert(
            "main".to_string(),
            ActiveExecution::new("box-main".to_string(), SessionKind::Main, main_exec, None),
        );
        executions.insert(
            "exec".to_string(),
            ActiveExecution::new("box-exec".to_string(), SessionKind::Exec, tenant, None),
        );

        let busy = busy_box_ids(&executions);

        assert!(busy.contains("box-exec"), "a live tenant exec is work");
        assert!(
            !busy.contains("box-main"),
            "counting Main would make AutoStop unreachable for every started box"
        );

        // `ActiveExecution::new` spawns a pump and a wait task per session, and
        // they stay parked for as long as the stub's channels are open — which
        // has to be past `busy_box_ids`, since a completed execution is not
        // busy. Closing the channels lets `wait()` return; waiting on the
        // resulting `done` signal is what makes those tasks finish before the
        // test does, rather than being left running for nextest to find.
        // Subscribed before the drop on purpose: a `watch` receiver takes the
        // current value as already seen, so one created after the session
        // finished would wait for a second change that never comes.
        let mut dones: Vec<_> = executions.values().map(|active| active.done_rx()).collect();
        drop((_main_ch, _tenant_ch));
        for done in &mut dones {
            assert!(
                tokio::time::timeout(std::time::Duration::from_secs(5), done.changed())
                    .await
                    .is_ok(),
                "a session whose channels are closed must finish"
            );
        }
    }

    /// Every spelling the runtime resolves must count as use — including an id
    /// prefix, which is the one that cannot be enumerated.
    ///
    /// `BoxManager::lookup_box` accepts an exact id, a name, or any
    /// unambiguous `id.starts_with(..)` prefix, and `RestBox` puts the caller's
    /// spelling straight into the path. A sweep that listed a box's spellings
    /// would have to produce every prefix — there are as many as the id is long
    /// — so it matches instead. Getting this wrong AutoStops a box that
    /// `boxlite --url … exec 01HJK4` is actively driving.
    #[test]
    fn a_spelling_resolves_the_way_the_runtime_resolves_it() {
        let id = "01HJK4TNRPQSXYZ8WM6NCVT9R5";
        let other = "01HJK9ZZZZZZZZZZZZZZZZZZZZ";
        let one = [(id.to_string(), Some("web".to_string()))];
        let two = [
            (id.to_string(), Some("web".to_string())),
            (other.to_string(), None),
        ];

        assert_eq!(box_named_by(id, &one), Some(id), "exact id");
        assert_eq!(box_named_by("web", &one), Some(id), "exact name");
        assert_eq!(box_named_by("01HJK4", &two), Some(id), "unique prefix");

        assert_eq!(box_named_by("", &one), None, "an empty spelling names none");
        assert_eq!(box_named_by("nope", &one), None, "names no box");
        assert_eq!(
            box_named_by("01HJK4TNRPQSXYZ8WM6NCVT9R5X", &one),
            None,
            "longer than the id is not a prefix of it"
        );

        // The one that matters: `BoxManager::lookup_box` errors on a prefix that
        // matches more than one box, so it must resolve to nothing here too.
        // Counting it would let one unauthenticated request stamp every box it
        // matched — `record_activity` stamps before any handler can reject it.
        assert_eq!(
            box_named_by("01HJK", &two),
            None,
            "an ambiguous prefix names no box"
        );
        assert_eq!(
            box_named_by("0", &two),
            None,
            "nor does a one-character one"
        );
    }

    /// A box driven by an id prefix must not look idle.
    ///
    /// The end-to-end companion to the truth table above: this is the spelling
    /// the sweep's old alias list could not represent at all.
    #[tokio::test]
    async fn a_box_driven_by_an_id_prefix_is_not_idle_under_its_full_id() {
        let map: RwLock<HashMap<String, Instant>> = RwLock::new(HashMap::new());
        let now = Instant::now();
        map.write().await.insert("01HJK4".to_string(), now);

        let idle = tokio::time::timeout(
            std::time::Duration::from_secs(3),
            idle_or_seed(
                &map,
                "01HJK4TNRPQSXYZ8WM6NCVT9R5",
                &[("01HJK4TNRPQSXYZ8WM6NCVT9R5".to_string(), None)],
                now + std::time::Duration::from_secs(30),
            ),
        )
        .await
        .expect("reading must not deadlock");

        assert_eq!(
            idle,
            std::time::Duration::from_secs(30),
            "a prefix-addressed request is use of the box it resolves to"
        );
    }

    /// ...and pruning must not throw that stamp away either.
    #[tokio::test]
    async fn pruning_keeps_a_stamp_made_under_an_id_prefix() {
        let state = lifecycle_state();
        state.record_box_activity("01HJK4").await;

        state
            .retain_known_boxes(&[("01HJK4TNRPQSXYZ8WM6NCVT9R5".to_string(), None)])
            .await;

        assert!(
            state.last_activity.read().await.contains_key("01HJK4"),
            "pruning dropped a stamp that names a live box"
        );
    }

    #[tokio::test]
    async fn a_box_driven_by_name_is_not_idle_under_its_id() {
        // The middleware stamps the raw URL segment, and the contract lets that
        // be a name. Reading only the canonical id loses every request made by
        // name, and AutoStop then stops a box in continuous use.
        let map: RwLock<HashMap<String, Instant>> = RwLock::new(HashMap::new());
        let now = Instant::now();
        map.write().await.insert("web".to_string(), now);

        let idle = tokio::time::timeout(
            std::time::Duration::from_secs(3),
            idle_or_seed(
                &map,
                "bx-abc123",
                &[("bx-abc123".to_string(), Some("web".to_string()))],
                now + std::time::Duration::from_secs(30),
            ),
        )
        .await
        .expect("reading must not deadlock");

        assert_eq!(idle, std::time::Duration::from_secs(30));
    }

    #[tokio::test]
    async fn the_idle_clock_drops_spellings_that_name_no_box() {
        // `record_activity` stamps before any handler can reject an unknown id,
        // and serve may run with no API key, so this is unauthenticated input.
        let state = lifecycle_state();
        state.record_box_activity("real").await;
        state.record_box_activity("never-existed").await;

        state
            .retain_known_boxes(&[("real".to_string(), None)])
            .await;

        let clocks = state.last_activity.read().await;
        assert!(clocks.contains_key("real"));
        assert!(
            !clocks.contains_key("never-existed"),
            "an id no box answers to must not hold a clock forever"
        );
    }

    #[tokio::test]
    async fn pruning_never_drops_a_deadline() {
        // `lifecycle` is written by create_box after its box may already be
        // missing from a caller's list_info snapshot. Pruning it against that
        // snapshot would drop the deadline permanently and silently.
        let state = lifecycle_state();
        state
            .set_lifecycle(
                "fresh",
                LifecyclePolicy {
                    auto_stop: 60,
                    auto_delete: 0,
                },
            )
            .await;

        state.retain_known_boxes(&[]).await;

        assert_eq!(state.lifecycle_of("fresh").await.auto_stop, 60);
    }

    // --- AutoResume gate ---

    /// The whole truth table, so inverting or dropping the rule fails here.
    #[test]
    fn autoresume_gates_only_a_stopped_box() {
        // `Stopped` is what AutoStop leaves behind, so it is the one state a
        // caller can be resumed *from*.
        assert!(autoresume_allows(BoxStatus::Stopped, true));
        assert!(
            !autoresume_allows(BoxStatus::Stopped, false),
            "a stopped box must refuse an implicit wake"
        );

        // Everything else is allowed whatever the flag says. Running/Paused are
        // already up. `Configured` has never run, and its first boot is not a
        // resume: gating it would make `run --url --no-auto-resume` refuse
        // itself, since `run` attaches before it starts the box.
        for status in [
            BoxStatus::Running,
            BoxStatus::Paused,
            BoxStatus::Configured,
            BoxStatus::Failed,
            BoxStatus::Stopping,
            BoxStatus::Unknown,
        ] {
            for auto_resume in [true, false] {
                assert!(
                    autoresume_allows(status, auto_resume),
                    "{status:?} with auto_resume={auto_resume} is not a resume"
                );
            }
        }
    }

    // --- Lifecycle sweep ---

    #[test]
    fn a_box_is_stopped_only_after_a_full_idle_window() {
        use std::time::Duration;
        let below = decide_lifecycle(
            BoxStatus::Running,
            60,
            0,
            Duration::from_secs(59),
            Duration::ZERO,
        );
        assert_eq!(below, LifecycleAction::Leave);

        // `>=`, so the boundary tick acts rather than waiting another 30s.
        let at = decide_lifecycle(
            BoxStatus::Running,
            60,
            0,
            Duration::from_secs(60),
            Duration::ZERO,
        );
        assert_eq!(at, LifecycleAction::Stop);
    }

    #[test]
    fn a_zero_window_disables_its_deadline() {
        use std::time::Duration;
        let huge = Duration::from_secs(86_400);
        assert_eq!(
            decide_lifecycle(BoxStatus::Running, 0, 0, huge, huge),
            LifecycleAction::Leave
        );
        assert_eq!(
            decide_lifecycle(BoxStatus::Stopped, 0, 0, huge, huge),
            LifecycleAction::Leave
        );
    }

    #[test]
    fn a_box_at_rest_is_deleted_on_its_own_deadline() {
        use std::time::Duration;
        // Anchored on the stop, not on last use: idle is zero here because the
        // box was busy right up to the moment it stopped, and it must still go.
        for status in [BoxStatus::Stopped, BoxStatus::Failed] {
            assert_eq!(
                decide_lifecycle(status, 0, 60, Duration::ZERO, Duration::from_secs(60)),
                LifecycleAction::Delete,
                "status {status:?}"
            );
        }
    }

    #[test]
    fn a_running_box_is_never_deleted_by_the_delete_deadline() {
        use std::time::Duration;
        // AutoDelete waits for the box to come to rest; a long-lived running
        // box must not be removed out from under its user.
        assert_eq!(
            decide_lifecycle(
                BoxStatus::Running,
                0,
                60,
                Duration::ZERO,
                Duration::from_secs(86_400)
            ),
            LifecycleAction::Leave
        );
    }

    #[test]
    fn box_scoped_paths_count_as_activity_except_the_pollers() {
        let post = Method::POST;
        assert_eq!(activity_box_id(&post, "/v1/boxes/abc/exec"), Some("abc"));
        assert_eq!(activity_box_id(&post, "/v1/boxes/abc/files"), Some("abc"));
        assert_eq!(activity_box_id(&post, "/v1/boxes/abc/attach"), Some("abc"));
        // `/start` counts: it is the one operation whose whole purpose is to
        // make a box usable again, so it must reset the window.
        assert_eq!(activity_box_id(&post, "/v1/boxes/abc/start"), Some("abc"));
        // A read of a sub-resource is still use of the box.
        assert_eq!(
            activity_box_id(&Method::GET, "/v1/boxes/abc/files"),
            Some("abc")
        );

        // Pollers must never hold a box open. A client watching `status` once a
        // second would otherwise reset the window before every sweep.
        assert_eq!(activity_box_id(&Method::GET, "/v1/boxes/abc"), None);
        assert_eq!(activity_box_id(&Method::HEAD, "/v1/boxes/abc"), None);
        assert_eq!(activity_box_id(&Method::DELETE, "/v1/boxes/abc"), None);
        assert_eq!(activity_box_id(&post, "/v1/boxes/abc/metrics"), None);
        assert_eq!(activity_box_id(&post, "/v1/boxes/abc/stop"), None);
        assert_eq!(activity_box_id(&post, "/v1/metrics"), None);
        assert_eq!(activity_box_id(&post, "/v1/boxes"), None);
    }

    #[tokio::test]
    async fn an_unseen_box_starts_its_window_now_rather_than_looking_idle() {
        // A box this `serve` has never seen reads as freshly used, not as idle
        // since boot — otherwise the first tick after a restart stops every
        // long-running box whose window is shorter than its uptime.
        //
        // Timeout-wrapped because the seeding path deadlocks rather than
        // returning a wrong answer if the read guard is held across the write
        // lock, and a hanging test stalls CI instead of failing it.
        let map: RwLock<HashMap<String, Instant>> = RwLock::new(HashMap::new());
        let now = Instant::now();
        let budget = std::time::Duration::from_secs(3);

        let first = tokio::time::timeout(
            budget,
            idle_or_seed(&map, "b", &[("b".to_string(), None)], now),
        )
        .await
        .expect("seeding must not deadlock");
        assert_eq!(first, std::time::Duration::ZERO);

        let later = now + std::time::Duration::from_secs(600);
        let second = tokio::time::timeout(
            budget,
            idle_or_seed(&map, "b", &[("b".to_string(), None)], later),
        )
        .await
        .expect("reading a seeded box must not deadlock");
        assert_eq!(second, std::time::Duration::from_secs(600));
    }

    #[test]
    fn build_box_options_omitted_inbound_defaults_to_disabled() {
        // A request without inbound is private, whether `network` is absent,
        // uses the legacy flat shape (which predates the split), or nests
        // only outbound.
        for network in [
            None,
            Some(r#"{"mode": "enabled"}"#),
            Some(r#"{"outbound": {"mode": "enabled"}}"#),
        ] {
            let body = match network {
                Some(network) => format!(r#"{{"image": "alpine:latest", "network": {network}}}"#),
                None => r#"{"image": "alpine:latest"}"#.to_string(),
            };
            let req: super::types::CreateBoxRequest =
                serde_json::from_str(&body).expect("body must deserialize");
            let opts = build_box_options(&req).expect("build");
            assert!(
                matches!(opts.inbound_network, NetworkSpec::Disabled),
                "network {network:?} must default inbound to disabled"
            );
        }
    }

    #[test]
    fn build_box_options_accepts_nested_network_spec() {
        let req: super::types::CreateBoxRequest = serde_json::from_str(
            r#"{
                "image": "alpine:latest",
                "network": {
                    "outbound": {
                        "mode": "enabled",
                        "allow_net": ["api.openai.com"]
                    },
                    "inbound": {
                        "mode": "disabled"
                    }
                }
            }"#,
        )
        .expect("nested network body must deserialize");
        let opts = build_box_options(&req).expect("build");
        match opts.network {
            NetworkSpec::Enabled { allow_net } => {
                assert_eq!(allow_net, vec!["api.openai.com"]);
            }
            NetworkSpec::Disabled => panic!("network should be enabled"),
        }
        assert!(matches!(opts.inbound_network, NetworkSpec::Disabled));
    }

    #[test]
    fn build_box_options_rejects_mixed_legacy_and_nested_network_spec() {
        let req: super::types::CreateBoxRequest = serde_json::from_str(
            r#"{
                "image": "alpine:latest",
                "network": {
                    "mode": "enabled",
                    "outbound": {
                        "mode": "enabled",
                        "allow_net": ["api.openai.com"]
                    }
                }
            }"#,
        )
        .expect("mixed network body still deserializes for compatibility validation");
        let err = build_box_options(&req).expect_err("mixed network body must fail");
        assert!(
            matches!(err, boxlite::BoxliteError::InvalidArgument(ref msg) if msg.contains("either nested")),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn build_box_options_rejects_empty_legacy_allow_net_mixed_with_nested_network_spec() {
        let req: super::types::CreateBoxRequest = serde_json::from_str(
            r#"{
                "image": "alpine:latest",
                "network": {
                    "allow_net": [],
                    "outbound": {
                        "mode": "enabled"
                    }
                }
            }"#,
        )
        .expect("mixed network body still deserializes for compatibility validation");
        let err = build_box_options(&req).expect_err("mixed network body must fail");
        assert!(
            matches!(err, boxlite::BoxliteError::InvalidArgument(ref msg) if msg.contains("either nested")),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn build_box_options_empty_body_lands_on_server_default_security() {
        // Bog-standard REST body. Server resolves security from its
        // own default; on Linux/macOS that's jailer-on (the standard
        // preset, post-flip).
        let json = r#"{"image": "alpine:latest"}"#;
        let req: super::types::CreateBoxRequest =
            serde_json::from_str(json).expect("body must deserialize");
        let opts = build_box_options(&req).expect("build_box_options");
        #[cfg(any(target_os = "linux", target_os = "macos"))]
        assert!(
            opts.advanced.security.jailer_enabled,
            "server default must be sandbox-on after the flip"
        );
    }

    #[test]
    fn create_box_request_rejects_client_supplied_security_preset() {
        // A malicious or careless client sends `security: "development"`
        // hoping to disable the jailer. `deny_unknown_fields` turns
        // that into a hard deserialize error, which the REST layer
        // surfaces as a 400 — there is no quiet fall-through.
        let json = r#"{"image": "alpine:latest", "security": "development"}"#;
        let msg = match serde_json::from_str::<super::types::CreateBoxRequest>(json) {
            Ok(_) => panic!("`security` must be rejected at deserialize"),
            Err(e) => e.to_string(),
        };
        assert!(
            msg.contains("unknown field") && msg.contains("security"),
            "expected deny-unknown-fields rejection mentioning `security`; got {msg}"
        );
    }

    #[test]
    fn create_box_request_rejects_client_supplied_security_settings() {
        // Same shape as the previous test but with a `security_settings`
        // struct. Also blocked at deserialize.
        let json = r#"{
            "image": "alpine:latest",
            "security_settings": {
                "jailer_enabled":  false,
                "seccomp_enabled": false,
                "uid": null,
                "gid": null,
                "new_pid_ns": false,
                "new_net_ns": false,
                "chroot_base": "/srv/boxlite",
                "chroot_enabled": false,
                "close_fds": false,
                "sanitize_env": false,
                "env_allowlist": [],
                "resource_limits": {},
                "sandbox_profile": null,
                "network_enabled": true
            }
        }"#;
        let msg = match serde_json::from_str::<super::types::CreateBoxRequest>(json) {
            Ok(_) => panic!("`security_settings` must be rejected at deserialize"),
            Err(e) => e.to_string(),
        };
        assert!(
            msg.contains("unknown field") && msg.contains("security_settings"),
            "expected deny-unknown-fields rejection mentioning `security_settings`; got {msg}"
        );
    }

    fn uploaded_v3_archive(box_options: serde_json::Value) -> Vec<u8> {
        let backing_path = b"/server/path/must-not-be-read";
        let mut disk = vec![0_u8; 1024];
        disk[0..4].copy_from_slice(&0x5146_49fbu32.to_be_bytes());
        disk[4..8].copy_from_slice(&3_u32.to_be_bytes());
        disk[8..16].copy_from_slice(&512_u64.to_be_bytes());
        disk[16..20].copy_from_slice(&(backing_path.len() as u32).to_be_bytes());
        disk[512..512 + backing_path.len()].copy_from_slice(backing_path);

        let manifest = serde_json::json!({
            "version": 3,
            "box_name": null,
            "image": "alpine:latest",
            "box_options": box_options,
            "guest_disk_checksum": "",
            "container_disk_checksum": "",
            "exported_at": "2026-07-26T00:00:00Z"
        });
        let manifest = serde_json::to_vec(&manifest).expect("serialize manifest");
        let mut archive = tar::Builder::new(Vec::new());

        for (name, bytes) in [
            ("manifest.json", manifest.as_slice()),
            ("disk.qcow2", disk.as_slice()),
        ] {
            let mut header = tar::Header::new_gnu();
            header.set_mode(0o600);
            header.set_size(bytes.len() as u64);
            header.set_cksum();
            archive
                .append_data(&mut header, name, bytes)
                .expect("append archive entry");
        }

        archive.finish().expect("finish archive");
        archive.into_inner().expect("archive bytes")
    }

    async fn upload_v3_archive(
        state: Arc<AppState>,
        box_options: serde_json::Value,
    ) -> (StatusCode, serde_json::Value) {
        let response = handlers::advanced::import_box(
            State(state),
            axum::extract::Query(types::ImportQuery { name: None }),
            axum::body::Bytes::from(uploaded_v3_archive(box_options)),
        )
        .await;
        let status = response.status();
        let response_body = axum::body::to_bytes(response.into_body(), usize::MAX)
            .await
            .expect("response body");
        let body = serde_json::from_slice(&response_body).expect("JSON response");
        (status, body)
    }

    async fn assert_uploaded_archive_rejected_before_provisioning(
        box_options: serde_json::Value,
        expected_message: &str,
    ) {
        let home = tempfile::tempdir().expect("runtime home");
        let runtime = BoxliteRuntime::new_for_test(boxlite::BoxliteOptions {
            home_dir: home.path().join("boxlite"),
            ..Default::default()
        })
        .expect("local runtime");
        let state = Arc::new(AppState {
            runtime: runtime.clone(),
            boxes: RwLock::new(HashMap::new()),
            executions: RwLock::new(HashMap::new()),
            api_key: None,
            lifecycle: RwLock::new(HashMap::new()),
            last_activity: RwLock::new(HashMap::new()),
        });
        let (control_status, control_error) =
            upload_v3_archive(state.clone(), serde_json::json!({})).await;
        assert_eq!(control_status, StatusCode::CONFLICT);
        assert_eq!(control_error["error"]["type"], "InvalidStateError");
        assert_eq!(control_error["error"]["code"], "invalid_state");
        assert!(
            control_error["error"]["message"]
                .as_str()
                .is_some_and(|message| message.contains("backing file reference")),
            "control upload must reach disk validation: {control_error}"
        );

        let (status, error) = upload_v3_archive(state, box_options).await;
        assert_eq!(status, StatusCode::BAD_REQUEST);
        assert_eq!(error["error"]["type"], "UnsupportedError");
        assert_eq!(error["error"]["code"], "unsupported");
        assert!(
            error["error"]["message"]
                .as_str()
                .is_some_and(|message| message.contains(expected_message))
        );
        assert!(
            runtime.list_info().await.expect("list boxes").is_empty(),
            "rejected upload must not provision a box"
        );
        runtime.shutdown(Some(1)).await.expect("shutdown runtime");
    }

    #[tokio::test]
    async fn serve_import_rejects_nested_virtualization_archive_before_provisioning() {
        assert_uploaded_archive_rejected_before_provisioning(
            serde_json::json!({"advanced": {"nested_virtualization": true}}),
            "nested virtualization",
        )
        .await;
    }

    #[tokio::test]
    async fn serve_import_rejects_host_volume_archive_before_provisioning() {
        assert_uploaded_archive_rejected_before_provisioning(
            serde_json::json!({
                "volumes": [{
                    "host_path": "/",
                    "guest_path": "/host",
                    "read_only": false
                }]
            }),
            "volume mounts",
        )
        .await;
    }

    /// The managed-volume shape is refused by the same gate, and reaches it
    /// through the same deserialization — an archive naming someone else's
    /// volume must not provision a box either.
    #[tokio::test]
    async fn serve_import_rejects_managed_volume_archive_before_provisioning() {
        assert_uploaded_archive_rejected_before_provisioning(
            serde_json::json!({
                "volumes": [{
                    "managed_volume": "someone-elses-data",
                    "guest_path": "/data",
                    "read_only": false
                }]
            }),
            "volume mounts",
        )
        .await;
    }

    /// Build an `ActiveExecution` backed by a stub `Execution` whose
    /// stdout/stderr/result channels we control from the test.
    fn make_test_active() -> (
        Arc<ActiveExecution>,
        tokio::sync::mpsc::UnboundedSender<String>, // stdout driver
        tokio::sync::mpsc::UnboundedSender<String>, // stderr driver
        tokio::sync::mpsc::UnboundedSender<boxlite::ExecResult>, // result driver
    ) {
        let (exec, stdout_tx, stderr_tx, _stdin_rx, result_tx) =
            boxlite::Execution::stub("test-exec");
        let active = ActiveExecution::new("test-box".to_string(), SessionKind::Exec, exec, None);
        (active, stdout_tx, stderr_tx, result_tx)
    }

    // ---------------------------------------------------------------
    // Main session (container init) — `GET /boxes/{id}/attach`
    // ---------------------------------------------------------------

    /// Channel handles that keep a stub `Execution` alive. Dropping them
    /// closes the result channel, which the wait task reads as "process
    /// exited"; a main session under test must stay running.
    #[allow(dead_code)]
    struct StubChannels(
        tokio::sync::mpsc::UnboundedSender<String>,
        tokio::sync::mpsc::UnboundedSender<String>,
        tokio::sync::mpsc::UnboundedReceiver<Vec<u8>>,
        tokio::sync::mpsc::UnboundedSender<boxlite::ExecResult>,
    );

    fn stub_execution(id: &str) -> (Execution, StubChannels) {
        let (exec, stdout_tx, stderr_tx, stdin_rx, result_tx) = boxlite::Execution::stub(id);
        (
            exec,
            StubChannels(stdout_tx, stderr_tx, stdin_rx, result_tx),
        )
    }

    // A second `GET /boxes/{id}/attach` must reuse the registered main
    // session, never open a second one. The guest binds init's stdout to
    // the first Attach RPC and rejects later ones, so a second
    // `LiteBox::attach()` would return an Execution that never streams —
    // and would hand a second client a live attach slot. The registry
    // entry is the record that the session is already open, so the second
    // call must find it and get refused by `mark_connected()` (the 409).
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn second_attach_reuses_main_session_and_never_reopens_it() {
        let mut executions: HashMap<String, Arc<ActiveExecution>> = HashMap::new();
        let opens = Arc::new(std::sync::atomic::AtomicUsize::new(0));

        // A tenant exec on the same box must not be mistaken for the main
        // session — it is addressed by its own id and has its own slot.
        let (tenant, _tenant_channels) = stub_execution("exec-1");
        executions.insert(
            "exec-1".to_string(),
            ActiveExecution::new("box1".to_string(), SessionKind::Exec, tenant, None),
        );

        let (init, _init_channels) = stub_execution("container-1");
        let mut init = Some(init);
        let first = register_main_session(&mut executions, "box1", || {
            opens.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
            let init = init.take().expect("opener runs once");
            async move { Ok(init) }
        })
        .await
        .expect("first attach opens the main session");

        // Second attach: the opener here would produce a *different*
        // session. If it ever runs, we have opened a second guest stream.
        let (decoy, _decoy_channels) = stub_execution("container-DECOY");
        let mut decoy = Some(decoy);
        let second = register_main_session(&mut executions, "box1", || {
            opens.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
            let decoy = decoy.take().expect("opener runs once");
            async move { Ok(decoy) }
        })
        .await
        .expect("second attach resolves the main session");

        assert_eq!(
            opens.load(std::sync::atomic::Ordering::SeqCst),
            1,
            "the second attach must reuse the registered main session, not open another one",
        );
        assert!(
            Arc::ptr_eq(&first, &second),
            "both attaches must resolve to the same ActiveExecution",
        );
        assert_eq!(
            second.execution().id(),
            "container-1",
            "the main session keeps the container id it was opened with",
        );
        assert!(
            executions.contains_key("container-1"),
            "the main session is registered under its container id, alongside tenant execs: {:?}",
            executions.keys().collect::<Vec<_>>(),
        );

        // The single-attach claim is what turns the reused entry into a
        // 409 for the second client, exactly as for an exec session.
        assert!(first.mark_connected().await, "first client claims the slot");
        assert!(
            !second.mark_connected().await,
            "a second client on an attached main session must be refused (409)",
        );
    }

    // The main session is found by box id — the container id is not
    // knowable to the caller. Exec sessions and other boxes' sessions must
    // never answer that lookup.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn find_main_session_matches_only_the_boxs_init_session() {
        let mut executions: HashMap<String, Arc<ActiveExecution>> = HashMap::new();

        let (exec, _exec_channels) = stub_execution("exec-1");
        executions.insert(
            "exec-1".to_string(),
            ActiveExecution::new("box1".to_string(), SessionKind::Exec, exec, None),
        );
        let (other_init, _other_channels) = stub_execution("container-2");
        executions.insert(
            "container-2".to_string(),
            ActiveExecution::new("box2".to_string(), SessionKind::Main, other_init, None),
        );

        assert!(
            find_main_session(&executions, "box1").is_none(),
            "box1 has only an exec session — attaching must open its main session, not adopt the exec",
        );

        let (init, _init_channels) = stub_execution("container-1");
        executions.insert(
            "container-1".to_string(),
            ActiveExecution::new("box1".to_string(), SessionKind::Main, init, None),
        );

        let found = find_main_session(&executions, "box1").expect("box1 main session");
        assert_eq!(
            found.execution().id(),
            "container-1",
            "must not return box2's main session",
        );
    }

    const STUB_BOX_ID: &str = "01HJK4TNRPQSXYZ8WM6NCVT9R5";
    /// The same box's user-defined name. The wire contract lets a client address
    /// a box by either, so both spellings belong in these tests.
    const STUB_BOX_NAME: &str = "swept-box";

    /// A running stub upstream, plus counters for the writes it is asked to make.
    struct StubUpstream {
        url: String,
        task: tokio::task::JoinHandle<()>,
        stops: Arc<std::sync::atomic::AtomicUsize>,
        deletes: Arc<std::sync::atomic::AtomicUsize>,
        execs: Arc<std::sync::atomic::AtomicUsize>,
    }

    impl StubUpstream {
        fn stops(&self) -> usize {
            self.stops.load(std::sync::atomic::Ordering::SeqCst)
        }

        fn deletes(&self) -> usize {
            self.deletes.load(std::sync::atomic::Ordering::SeqCst)
        }

        /// How many times the guest was actually asked to run something.
        ///
        /// A gate that let a request through is only distinguishable from one
        /// the request never reached by whether the runtime call happened.
        fn execs(&self) -> usize {
            self.execs.load(std::sync::atomic::Ordering::SeqCst)
        }
    }

    /// An upstream that answers the box reads with one canned box, so a handler
    /// can be driven end to end without a VM.
    ///
    /// The box it reports is shaped like one created through `serve`: both
    /// deadlines are zero, because `build_box_options` withheld them from the
    /// runtime. `auto_resume` is a parameter because the resolvers disagree
    /// about it — one consults it, one consults the held AutoStop deadline —
    /// and that disagreement is what lets a test tell them apart.
    async fn stub_upstream(status: &'static str, auto_resume: bool) -> StubUpstream {
        fn canned_box(status: &str, auto_resume: bool) -> serde_json::Value {
            serde_json::json!({
                "box_id": STUB_BOX_ID,
                "name": STUB_BOX_NAME,
                "status": status,
                "created_at": "2026-01-01T00:00:00Z",
                "updated_at": "2026-01-01T00:00:00Z",
                "pid": null,
                "image": "alpine:latest",
                "cpus": 1,
                "memory_mib": 512,
                "auto_stop": 0,
                "auto_delete": 0,
                "auto_resume": auto_resume,
            })
        }

        let deletes = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let stops = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let execs = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let app = axum::Router::new()
            .route(
                "/v1/boxes/{id}/exec",
                axum::routing::post({
                    let execs = Arc::clone(&execs);
                    move |axum::extract::Path(id): axum::extract::Path<String>| {
                        let execs = Arc::clone(&execs);
                        async move {
                            if id == STUB_BOX_ID || id == STUB_BOX_NAME {
                                execs.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                            }
                            // The reply shape does not matter: the counter is the
                            // whole point, and every caller here is proving only
                            // that the request got this far.
                            StatusCode::INTERNAL_SERVER_ERROR.into_response()
                        }
                    }
                }),
            )
            .route(
                "/v1/boxes",
                axum::routing::get(move || async move {
                    Json(serde_json::json!({ "boxes": [canned_box(status, auto_resume)] }))
                })
                // Creation reports the box back with both deadlines at zero,
                // exactly as the real runtime does once `build_box_options` has
                // withheld them — so a test can tell a stored policy from an
                // echoed one.
                .post(move |_body: Json<serde_json::Value>| async move {
                    (StatusCode::CREATED, Json(canned_box(status, auto_resume)))
                }),
            )
            .route(
                "/v1/boxes/{id}/stop",
                axum::routing::post({
                    let stops = Arc::clone(&stops);
                    move |axum::extract::Path(id): axum::extract::Path<String>| {
                        let stops = Arc::clone(&stops);
                        async move {
                            // Gated on the id like the sibling delete, so the
                            // counter says which box was stopped and not merely
                            // that something was.
                            if id == STUB_BOX_ID || id == STUB_BOX_NAME {
                                stops.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                                Json(canned_box("stopped", auto_resume)).into_response()
                            } else {
                                StatusCode::NOT_FOUND.into_response()
                            }
                        }
                    }
                }),
            )
            .route(
                "/v1/boxes/{id}",
                axum::routing::get(
                    // Answers to either spelling with the same box, reporting its
                    // canonical id — which is what the real runtime does, since
                    // it resolves a name before returning info. A stub that
                    // echoed the requested spelling back as `box_id` would hide
                    // exactly the id-vs-name bugs these tests exist to catch.
                    move |axum::extract::Path(id): axum::extract::Path<String>| async move {
                        if id == STUB_BOX_ID || id == STUB_BOX_NAME {
                            Json(canned_box(status, auto_resume)).into_response()
                        } else {
                            StatusCode::NOT_FOUND.into_response()
                        }
                    },
                )
                .delete({
                    let deletes = Arc::clone(&deletes);
                    move |axum::extract::Path(id): axum::extract::Path<String>| {
                        let deletes = Arc::clone(&deletes);
                        async move {
                            if id == STUB_BOX_ID || id == STUB_BOX_NAME {
                                deletes.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                                StatusCode::NO_CONTENT.into_response()
                            } else {
                                StatusCode::NOT_FOUND.into_response()
                            }
                        }
                    }
                }),
            );
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let port = listener.local_addr().unwrap().port();
        let task = tokio::spawn(async move {
            let _ = axum::serve(listener, app).await;
        });
        StubUpstream {
            url: format!("http://127.0.0.1:{port}"),
            task,
            stops,
            deletes,
            execs,
        }
    }

    /// Serve `state` on an ephemeral port and return its base URL.
    async fn serve_router(state: Arc<AppState>) -> (String, tokio::task::JoinHandle<()>) {
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let port = listener.local_addr().unwrap().port();
        let handle = tokio::spawn(async move {
            let _ = axum::serve(listener, build_router(state)).await;
        });
        (format!("http://127.0.0.1:{port}"), handle)
    }

    /// Reading a box back over HTTP must report the deadlines `serve` holds.
    ///
    /// `box_response` being correct is not enough — the defect this guards is a
    /// handler that does not call it. Rust lets these handlers reach
    /// `box_info_to_response` directly from their parent module, so rewiring one
    /// back to it compiles clean; only driving the real route catches it.
    ///
    /// Both read shapes are driven: the single value and the list's loop. The
    /// remaining sites (`start`, `stop`, `clone`, `import`) build their response
    /// from the same single value, but only after an operation a stub upstream
    /// cannot stand in for.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn reading_a_box_over_http_reports_the_deadlines_serve_holds() {
        let upstream = stub_upstream("stopped", true).await;
        let state = Arc::new(AppState {
            runtime: BoxliteRuntime::rest(boxlite::BoxliteRestOptions::new(upstream.url.clone()))
                .expect("rest runtime"),
            boxes: RwLock::new(HashMap::new()),
            executions: RwLock::new(HashMap::new()),
            api_key: None,
            lifecycle: RwLock::new(HashMap::new()),
            last_activity: RwLock::new(HashMap::new()),
        });
        state
            .set_lifecycle(
                STUB_BOX_ID,
                LifecyclePolicy {
                    auto_stop: 900,
                    auto_delete: 604_800,
                },
            )
            .await;
        let (base, server) = serve_router(Arc::clone(&state)).await;
        let client = reqwest::Client::new();

        let one: serde_json::Value = client
            .get(format!("{base}/v1/boxes/{STUB_BOX_ID}"))
            .send()
            .await
            .expect("request must reach the server")
            .json()
            .await
            .expect("a box body");
        assert_eq!(
            one["auto_stop"], 900,
            "GET must report the AutoStop deadline serve is sweeping, not the runtime's zero"
        );
        assert_eq!(
            one["auto_delete"], 604_800,
            "GET must report the AutoDelete deadline serve is sweeping, not the runtime's zero"
        );

        let listed: serde_json::Value = client
            .get(format!("{base}/v1/boxes"))
            .send()
            .await
            .expect("request must reach the server")
            .json()
            .await
            .expect("a list body");
        assert_eq!(
            listed["boxes"][0]["auto_stop"], 900,
            "the list must report the same deadlines the single read does"
        );
        assert_eq!(listed["boxes"][0]["auto_delete"], 604_800);

        server.abort();
        upstream.task.abort();
    }

    /// Removing a box by name must forget the deadline filed under its id.
    ///
    /// `retain_known_boxes` prunes the idle clock but deliberately never touches
    /// the deadline map, so an entry missed here is missed for the life of the
    /// process. Driven by name specifically: forgetting the spelling the request
    /// used is what leaves the id-keyed entry behind.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn removing_a_box_by_name_forgets_the_deadline_filed_under_its_id() {
        let upstream = stub_upstream("stopped", true).await;
        let state = Arc::new(AppState {
            runtime: BoxliteRuntime::rest(boxlite::BoxliteRestOptions::new(upstream.url.clone()))
                .expect("rest runtime"),
            boxes: RwLock::new(HashMap::new()),
            executions: RwLock::new(HashMap::new()),
            api_key: None,
            lifecycle: RwLock::new(HashMap::new()),
            last_activity: RwLock::new(HashMap::new()),
        });
        state
            .set_lifecycle(
                STUB_BOX_ID,
                LifecyclePolicy {
                    auto_stop: 900,
                    auto_delete: 604_800,
                },
            )
            .await;
        let (base, server) = serve_router(Arc::clone(&state)).await;

        let response = reqwest::Client::new()
            .delete(format!("{base}/v1/boxes/{STUB_BOX_NAME}"))
            .send()
            .await
            .expect("request must reach the server");
        assert_eq!(
            response.status(),
            StatusCode::NO_CONTENT,
            "the box must actually have been removed, or the forget is untested"
        );

        assert_eq!(
            upstream.deletes(),
            1,
            "the box must have been removed upstream, not just forgotten locally"
        );
        assert!(
            !state.lifecycle.read().await.contains_key(STUB_BOX_ID),
            "the deadline outlived the box it belonged to"
        );
        server.abort();
        upstream.task.abort();
    }

    /// The sweep itself must reach the runtime, not just decide correctly.
    ///
    /// `decide_lifecycle`, the busy set, the aliasing and the idle seeding are
    /// each covered in isolation; this is the loop that wires them to
    /// `list_info` and `remove`. Without it, every part could be right while
    /// nothing is ever actually swept.
    ///
    /// This drives the AutoDelete arm; `the_sweep_stops_a_box_idle_past_its_
    /// window` drives the Stop arm. `last_updated` on the canned box is long
    /// past, so the deadline below is comfortably elapsed.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn the_sweep_removes_a_box_whose_delete_deadline_has_passed() {
        let upstream = stub_upstream("stopped", true).await;
        let state = AppState {
            runtime: BoxliteRuntime::rest(boxlite::BoxliteRestOptions::new(upstream.url.clone()))
                .expect("rest runtime"),
            boxes: RwLock::new(HashMap::new()),
            executions: RwLock::new(HashMap::new()),
            api_key: None,
            lifecycle: RwLock::new(HashMap::new()),
            last_activity: RwLock::new(HashMap::new()),
        };
        state
            .set_lifecycle(
                STUB_BOX_ID,
                LifecyclePolicy {
                    auto_stop: 0,
                    auto_delete: 60,
                },
            )
            .await;

        run_lifecycle_once(&state, Instant::now()).await;

        assert_eq!(
            upstream.deletes(),
            1,
            "a box at rest past its AutoDelete window must actually be removed"
        );
        assert!(
            !state.lifecycle.read().await.contains_key(STUB_BOX_ID),
            "a removed box must not keep its deadline"
        );
        upstream.task.abort();
    }

    /// Creating a box over the wire must actually file its deadlines.
    ///
    /// `create_box`'s `set_lifecycle` is the only production write to the
    /// lifecycle map — every other caller is a test seeding it by hand. Delete
    /// that one line and the whole feature goes inert while the suite stays
    /// green: no box acquires a deadline, every read reports zero, and the sweep
    /// never acts. Nothing else here can catch that, so this drives the real
    /// route and checks both the response and the map behind it.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn creating_a_box_over_http_files_the_deadlines_it_asked_for() {
        let upstream = stub_upstream("running", true).await;
        let state = Arc::new(AppState {
            runtime: BoxliteRuntime::rest(boxlite::BoxliteRestOptions::new(upstream.url.clone()))
                .expect("rest runtime"),
            boxes: RwLock::new(HashMap::new()),
            executions: RwLock::new(HashMap::new()),
            api_key: None,
            lifecycle: RwLock::new(HashMap::new()),
            last_activity: RwLock::new(HashMap::new()),
        });
        let (base, server) = serve_router(Arc::clone(&state)).await;

        let created: serde_json::Value = reqwest::Client::new()
            .post(format!("{base}/v1/boxes"))
            .json(&serde_json::json!({
                "image": "alpine:latest",
                "auto_stop": 900,
                "auto_delete": 604_800,
            }))
            .send()
            .await
            .expect("request must reach the server")
            .json()
            .await
            .expect("a box body");

        assert_eq!(
            created["auto_stop"], 900,
            "the create response must echo the policy, not the runtime's zero"
        );
        assert_eq!(created["auto_delete"], 604_800);
        assert_eq!(
            state.lifecycle.read().await.get(STUB_BOX_ID).copied(),
            Some(LifecyclePolicy {
                auto_stop: 900,
                auto_delete: 604_800,
            }),
            "the sweep reads this map; a policy that never lands here is never enforced"
        );

        server.abort();
        upstream.task.abort();
    }

    /// A box addressed by a name that needs escaping must still count as used.
    ///
    /// The idle clock is written from the raw request path and read from the
    /// decoded `BoxInfo.name`, so without decoding at the write site the two
    /// spellings never meet and a box in continuous use is swept as idle. The
    /// contract's `name` pattern forbids a name that needs escaping, but `serve`
    /// does not enforce it, so such a name reaches this code.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn activity_on_an_escaped_name_lands_under_the_name_the_sweep_reads() {
        let upstream = stub_upstream("running", true).await;
        let state = Arc::new(AppState {
            runtime: BoxliteRuntime::rest(boxlite::BoxliteRestOptions::new(upstream.url.clone()))
                .expect("rest runtime"),
            boxes: RwLock::new(HashMap::new()),
            executions: RwLock::new(HashMap::new()),
            api_key: None,
            lifecycle: RwLock::new(HashMap::new()),
            last_activity: RwLock::new(HashMap::new()),
        });
        let (base, server) = serve_router(Arc::clone(&state)).await;

        let _ = reqwest::Client::new()
            .post(format!("{base}/v1/boxes/my%20box/exec"))
            .json(&serde_json::json!({"command": "echo"}))
            .send()
            .await
            .expect("request must reach the server");

        // A legal name, escaped anyway. Percent-encoding an unreserved character
        // is allowed, so this case does not depend on the contract's `name`
        // pattern going unenforced — it would still arrive if that gap closed.
        let _ = reqwest::Client::new()
            .post(format!("{base}/v1/boxes/swept%2Dbox/exec"))
            .json(&serde_json::json!({"command": "echo"}))
            .send()
            .await
            .expect("request must reach the server");

        let clocks = state.last_activity.read().await;
        assert!(
            clocks.contains_key("my box"),
            "the stamp must be filed under the decoded name the sweep looks for, \
             not the escaped spelling: {:?}",
            clocks.keys().collect::<Vec<_>>()
        );
        assert!(
            clocks.contains_key(STUB_BOX_NAME),
            "an escaped unreserved character must decode too: {:?}",
            clocks.keys().collect::<Vec<_>>()
        );

        drop(clocks);
        server.abort();
        upstream.task.abort();
    }

    /// AutoResume must be enforced by the routes, not merely decided correctly.
    ///
    /// `autoresume_allows` is exhaustively unit-tested, but that says nothing
    /// about whether any handler consults it: `get_or_resume_box` and
    /// `get_or_fetch_box` have identical signatures, so rewiring a route to the
    /// ungated one compiles clean and every predicate test stays green. Only
    /// driving the route catches it. The body is asserted too, because a 409
    /// alone would not distinguish this gate from the metrics one.
    ///
    /// All four call sites are driven, because each handler resolves for itself
    /// and rewiring any one of them alone is invisible to the others' tests:
    /// `exec`, both halves of `files` (upload and download are separate
    /// handlers on one route), and `attach`.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn a_stopped_box_with_autoresume_off_refuses_the_routes_that_would_wake_it() {
        let upstream = stub_upstream("stopped", false).await;
        let state = Arc::new(AppState {
            runtime: BoxliteRuntime::rest(boxlite::BoxliteRestOptions::new(upstream.url.clone()))
                .expect("rest runtime"),
            boxes: RwLock::new(HashMap::new()),
            executions: RwLock::new(HashMap::new()),
            api_key: None,
            lifecycle: RwLock::new(HashMap::new()),
            last_activity: RwLock::new(HashMap::new()),
        });
        let (base, server) = serve_router(Arc::clone(&state)).await;
        let client = reqwest::Client::new();

        let exec = client
            .post(format!("{base}/v1/boxes/{STUB_BOX_ID}/exec"))
            .json(&serde_json::json!({"command": "echo"}))
            .send()
            .await
            .expect("request must reach the server");
        assert_eq!(
            exec.status().as_u16(),
            409,
            "exec must not implicitly wake a box whose caller disabled AutoResume"
        );
        let body = exec.text().await.expect("an error body");
        assert!(
            body.contains("has AutoResume disabled"),
            "the refusal must be the AutoResume gate, not another 409: {body}"
        );

        let download = client
            .get(format!("{base}/v1/boxes/{STUB_BOX_ID}/files?path=/tmp"))
            .send()
            .await
            .expect("request must reach the server");
        assert_eq!(
            download.status().as_u16(),
            409,
            "reading files must be gated too; it drives the guest just as exec does"
        );
        assert!(
            download
                .text()
                .await
                .expect("an error body")
                .contains("has AutoResume disabled"),
            "the refusal must be the AutoResume gate, not another 409"
        );

        let upload = client
            .put(format!("{base}/v1/boxes/{STUB_BOX_ID}/files?path=/tmp/x"))
            .body(Vec::new())
            .send()
            .await
            .expect("request must reach the server");
        assert_eq!(
            upload.status().as_u16(),
            409,
            "writing files is a separate handler and resolves for itself"
        );
        assert!(
            upload
                .text()
                .await
                .expect("an error body")
                .contains("has AutoResume disabled"),
            "the refusal must be the AutoResume gate, not another 409"
        );

        // Attach goes through a fourth resolver, and its `WebSocketUpgrade`
        // extractor rejects a plain GET before the handler runs — so the refusal
        // is only observable through a real handshake.
        let ws_url = base.replacen("http://", "ws://", 1);
        let refused =
            tokio_tungstenite::connect_async(format!("{ws_url}/v1/boxes/{STUB_BOX_ID}/attach"))
                .await
                .expect_err("the handshake must be refused, not completed");
        match refused {
            tokio_tungstenite::tungstenite::Error::Http(response) => {
                assert_eq!(
                    response.status().as_u16(),
                    409,
                    "attach must not implicitly wake the box either"
                );
                // tungstenite fills this from whatever it had already buffered
                // past the response headers, so in principle a split write
                // would leave it empty. Asserted unconditionally anyway: the
                // body does arrive over loopback, and tolerating an empty one
                // would quietly reduce this to a status-only check the day it
                // stopped. A flake here is worth more than a silent downgrade.
                let body = String::from_utf8_lossy(response.body().as_deref().unwrap_or_default())
                    .into_owned();
                assert!(
                    body.contains("has AutoResume disabled"),
                    "the refusal must be the AutoResume gate, not the already-attached 409: {body}"
                );
            }
            other => panic!("expected an HTTP refusal, got {other:?}"),
        }

        server.abort();
        upstream.task.abort();
    }

    /// ...and must not refuse when the caller left AutoResume on.
    ///
    /// Without this the test above passes just as well against a route that
    /// answers 409 unconditionally.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn a_stopped_box_with_autoresume_on_is_not_refused() {
        let upstream = stub_upstream("stopped", true).await;
        let state = Arc::new(AppState {
            runtime: BoxliteRuntime::rest(boxlite::BoxliteRestOptions::new(upstream.url.clone()))
                .expect("rest runtime"),
            boxes: RwLock::new(HashMap::new()),
            executions: RwLock::new(HashMap::new()),
            api_key: None,
            lifecycle: RwLock::new(HashMap::new()),
            last_activity: RwLock::new(HashMap::new()),
        });
        let (base, server) = serve_router(Arc::clone(&state)).await;

        let exec = reqwest::Client::new()
            .post(format!("{base}/v1/boxes/{STUB_BOX_ID}/exec"))
            .json(&serde_json::json!({"command": "echo"}))
            .send()
            .await
            .expect("request must reach the server");
        assert_ne!(
            exec.status().as_u16(),
            409,
            "AutoResume was enabled; the wake must not have been refused"
        );
        // A non-409 alone would also be satisfied by a request that never
        // reached the gate at all. What proves it was let *through* is that the
        // runtime went on to ask the guest to run something.
        assert_eq!(
            upstream.execs(),
            1,
            "the request must have reached the guest, not merely avoided a 409"
        );

        server.abort();
        upstream.task.abort();
    }

    /// The Stop arm, likewise driven end to end.
    ///
    /// This one goes through `runtime.get` and `LiteBox::stop` rather than a
    /// bare delete, so it covers the longer of the two paths out of
    /// `decide_lifecycle`. The box has never been seen by this server, so
    /// `idle_or_seed` would normally seed it at zero and spare it — the clock is
    /// pre-stamped far enough back to put it past its window.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn the_sweep_stops_a_box_idle_past_its_window() {
        let upstream = stub_upstream("running", true).await;
        let state = AppState {
            runtime: BoxliteRuntime::rest(boxlite::BoxliteRestOptions::new(upstream.url.clone()))
                .expect("rest runtime"),
            boxes: RwLock::new(HashMap::new()),
            executions: RwLock::new(HashMap::new()),
            api_key: None,
            lifecycle: RwLock::new(HashMap::new()),
            last_activity: RwLock::new(HashMap::new()),
        };
        state
            .set_lifecycle(
                STUB_BOX_ID,
                LifecyclePolicy {
                    auto_stop: 60,
                    auto_delete: 0,
                },
            )
            .await;
        let now = Instant::now();
        state
            .last_activity
            .write()
            .await
            .insert(STUB_BOX_ID.to_string(), now - Duration::from_secs(600));

        run_lifecycle_once(&state, now).await;

        assert_eq!(
            upstream.stops(),
            1,
            "a running box idle past its AutoStop window must actually be stopped"
        );
        upstream.task.abort();
    }

    /// ...and must leave a box alone that is still inside its window.
    ///
    /// Without this the test above passes just as well against a sweep that
    /// stops unconditionally.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn the_sweep_leaves_a_box_still_inside_its_idle_window() {
        let upstream = stub_upstream("running", true).await;
        let state = AppState {
            runtime: BoxliteRuntime::rest(boxlite::BoxliteRestOptions::new(upstream.url.clone()))
                .expect("rest runtime"),
            boxes: RwLock::new(HashMap::new()),
            executions: RwLock::new(HashMap::new()),
            api_key: None,
            lifecycle: RwLock::new(HashMap::new()),
            last_activity: RwLock::new(HashMap::new()),
        };
        state
            .set_lifecycle(
                STUB_BOX_ID,
                LifecyclePolicy {
                    auto_stop: 600,
                    auto_delete: 0,
                },
            )
            .await;
        let now = Instant::now();
        state
            .last_activity
            .write()
            .await
            .insert(STUB_BOX_ID.to_string(), now - Duration::from_secs(60));

        run_lifecycle_once(&state, now).await;

        assert_eq!(
            upstream.stops(),
            0,
            "the sweep stopped a box that had been used inside its window"
        );
        upstream.task.abort();
    }

    /// ...and must leave a box alone whose delete deadline has not passed.
    ///
    /// Without this the delete test passes just as well against a sweep that
    /// removes unconditionally.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn the_sweep_leaves_a_box_whose_deadline_has_not_passed() {
        let upstream = stub_upstream("stopped", true).await;
        let state = AppState {
            runtime: BoxliteRuntime::rest(boxlite::BoxliteRestOptions::new(upstream.url.clone()))
                .expect("rest runtime"),
            boxes: RwLock::new(HashMap::new()),
            executions: RwLock::new(HashMap::new()),
            api_key: None,
            lifecycle: RwLock::new(HashMap::new()),
            last_activity: RwLock::new(HashMap::new()),
        };
        // The canned box's `last_updated` is a fixed date in the past, so its
        // time at rest grows as the calendar does. A century-long window
        // out-reaches that for the life of this test.
        state
            .set_lifecycle(
                STUB_BOX_ID,
                LifecyclePolicy {
                    auto_stop: 0,
                    auto_delete: 60 * 60 * 24 * 365 * 100,
                },
            )
            .await;

        run_lifecycle_once(&state, Instant::now()).await;

        assert_eq!(
            upstream.deletes(),
            0,
            "the sweep removed a box that was still inside its AutoDelete window"
        );
        assert!(
            state.lifecycle.read().await.contains_key(STUB_BOX_ID),
            "a box that was not removed must keep its deadline"
        );
        upstream.task.abort();
    }

    /// A metrics scrape must be refused rather than wake a box the sweep would
    /// stop again — checked through the real route, not the predicate.
    ///
    /// The stub box has `auto_resume: true`, so the AutoResume resolver would
    /// let this through; only the metrics resolver refuses it. That is exactly
    /// the rewiring that would silently restore the thrash loop.
    ///
    /// Both spellings are driven. The deadline is filed under the box's id, but
    /// the path segment may be a user-defined name — so a lookup keyed off the
    /// segment reads no deadline for the name and wakes the box, and only the
    /// name half of this test can see that.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn scraping_metrics_will_not_wake_a_box_the_sweep_would_stop() {
        let upstream = stub_upstream("stopped", true).await;
        let state = Arc::new(AppState {
            runtime: BoxliteRuntime::rest(boxlite::BoxliteRestOptions::new(upstream.url.clone()))
                .expect("rest runtime"),
            boxes: RwLock::new(HashMap::new()),
            executions: RwLock::new(HashMap::new()),
            api_key: None,
            lifecycle: RwLock::new(HashMap::new()),
            last_activity: RwLock::new(HashMap::new()),
        });
        state
            .set_lifecycle(
                STUB_BOX_ID,
                LifecyclePolicy {
                    auto_stop: 900,
                    auto_delete: 604_800,
                },
            )
            .await;
        let (base, server) = serve_router(Arc::clone(&state)).await;

        for spelling in [STUB_BOX_ID, STUB_BOX_NAME] {
            let response = reqwest::Client::new()
                .get(format!("{base}/v1/boxes/{spelling}/metrics"))
                .send()
                .await
                .expect("request must reach the server");
            assert_eq!(
                response.status().as_u16(),
                409,
                "a scrape addressed by {spelling:?} must be refused, not answered by booting the box"
            );
            let body = response.text().await.expect("an error body");
            assert!(
                body.contains("a metrics scrape does not start a box"),
                "the refusal must be the metrics rule, not the AutoResume gate: {body}"
            );

            assert!(
                !state.last_activity.read().await.contains_key(spelling),
                "a scrape must not stamp the idle clock either"
            );
        }
        server.abort();
        upstream.task.abort();
    }

    /// An unauthenticated request must not stamp a box's idle clock.
    ///
    /// This rests entirely on layer order: axum applies `.layer()` calls in
    /// reverse, so `require_api_key` is added last to end up outermost and
    /// short-circuit before `record_activity` runs. Swapping the two adjacent
    /// calls inverts it silently, and any caller could then hold someone else's
    /// box open past its AutoStop window.
    #[tokio::test]
    async fn a_rejected_request_does_not_stamp_the_idle_clock() {
        let runtime = BoxliteRuntime::rest(boxlite::BoxliteRestOptions::new(
            "http://127.0.0.1:1".to_string(),
        ))
        .expect("rest runtime");
        let state = Arc::new(AppState {
            runtime,
            boxes: RwLock::new(HashMap::new()),
            executions: RwLock::new(HashMap::new()),
            api_key: Some("expected-key".to_string()),
            lifecycle: RwLock::new(HashMap::new()),
            last_activity: RwLock::new(HashMap::new()),
        });

        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let port = listener.local_addr().unwrap().port();
        let server = {
            let state = Arc::clone(&state);
            tokio::spawn(async move {
                let _ = axum::serve(listener, build_router(state)).await;
            })
        };

        let unauthorized = reqwest::Client::new()
            .post(format!("http://127.0.0.1:{port}/v1/boxes/victim/exec"))
            .json(&serde_json::json!({"command": "echo"}))
            .send()
            .await
            .expect("request must reach the server");
        assert_eq!(unauthorized.status().as_u16(), 401);

        assert!(
            !state.last_activity.read().await.contains_key("victim"),
            "a 401'd request must not have reset the box's idle window"
        );
        server.abort();
    }

    // The container-attach route must be registered at the path the client
    // builds (`RestBox::attach` → `/v1/boxes/{id}/attach`). A method it
    // does not serve proves the path matched (405); an adjacent path that
    // was never registered proves the opposite (404). Neither reaches a
    // handler, so no box or VM is touched.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn container_attach_route_is_registered() {
        // A REST-backed runtime keeps AppState cheap: no local runtime
        // dirs, no embedded-runtime extraction. Routing is decided before
        // any handler runs, so the runtime is never called.
        let runtime = BoxliteRuntime::rest(boxlite::BoxliteRestOptions::new(
            "http://127.0.0.1:1".to_string(),
        ))
        .expect("rest runtime");
        let state = Arc::new(AppState {
            runtime,
            boxes: RwLock::new(HashMap::new()),
            executions: RwLock::new(HashMap::new()),
            api_key: None,
            lifecycle: RwLock::new(HashMap::new()),
            last_activity: RwLock::new(HashMap::new()),
        });

        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let port = listener.local_addr().unwrap().port();
        let server = tokio::spawn(async move {
            let _ = axum::serve(listener, build_router(state)).await;
        });

        let http = reqwest::Client::new();
        let attached = http
            .post(format!("http://127.0.0.1:{port}/v1/boxes/box1/attach"))
            .send()
            .await
            .expect("POST /attach");
        assert_eq!(
            attached.status().as_u16(),
            405,
            "GET /v1/boxes/{{box_id}}/attach must be registered (405 = path matched, method did not)",
        );

        // A `/ports` discovery route existed earlier on this branch and was
        // withdrawn: the REST surface reports bindings on the box resource.
        let withdrawn_ports = http
            .get(format!("http://127.0.0.1:{port}/v1/boxes/box1/ports"))
            .send()
            .await
            .expect("GET withdrawn /ports route");
        assert_eq!(
            withdrawn_ports.status().as_u16(),
            404,
            "the withdrawn /ports discovery route must not come back",
        );

        let unrouted = http
            .get(format!("http://127.0.0.1:{port}/v1/boxes/box1/attach/nope"))
            .send()
            .await
            .expect("GET unregistered path");
        assert_eq!(
            unrouted.status().as_u16(),
            404,
            "control: an unregistered path must 404, so the 405 above is meaningful",
        );

        server.abort();
    }

    // ---------------------------------------------------------------
    // Finding 1: late subscriber misses pre-attach output
    // ---------------------------------------------------------------
    //
    // ActiveExecution pumps stdout through a tokio::sync::broadcast
    // sender. broadcast::subscribe() only delivers messages sent AFTER
    // the subscribe call. A client that calls GET /attach after output
    // has already been produced loses that output.
    //
    // This exercises the real ActiveExecution: we push lines through
    // the stub, let the pump broadcast them, then subscribe and check
    // whether the late subscriber sees the earlier lines.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn late_attach_subscriber_must_see_prior_output() {
        let (active, stdout_tx, _stderr_tx, _result_tx) = make_test_active();

        // Push 5 lines through the stub's stdout channel. The pump
        // task inside ActiveExecution::new reads these and broadcasts
        // them.
        for i in 1..=5 {
            stdout_tx.send(format!("line-{i}\n")).unwrap();
        }
        // Give the pump task a tick to broadcast all 5 chunks.
        tokio::time::sleep(Duration::from_millis(50)).await;

        // NOW subscribe — this is what run_attach_session does when a
        // client connects to /attach after the exec already produced
        // output.
        let mut rx = active.stdout_bus().subscribe();

        // Push one more line AFTER the subscribe so we can prove the
        // channel is alive.
        stdout_tx.send("line-6\n".to_string()).unwrap();
        tokio::time::sleep(Duration::from_millis(20)).await;

        let mut received = Vec::new();
        while let Ok(Ok(data)) = tokio::time::timeout(Duration::from_millis(50), rx.recv()).await {
            received.push(String::from_utf8(data).unwrap());
        }

        // MUST FAIL on unfixed code: received has only ["line-6\n"].
        // The 5 pre-subscribe lines are lost because broadcast has no
        // backlog replay.
        assert!(
            received.len() >= 6,
            "late subscriber must see pre-subscribe output; \
             got {} line(s): {:?}  (expected >= 6, including the 5 pre-attach lines)",
            received.len(),
            received,
        );
    }

    // ---------------------------------------------------------------
    // Finding 2: final stdout chunk lost on fast process exit
    // ---------------------------------------------------------------
    //
    // The architecture has TWO independent spawned tasks:
    //   (A) stdout pump: reads ExecStdout stream → broadcasts via stdout_bus
    //   (B) wait task: calls execution.wait() → stores exit_code → fires done_tx
    //
    // If (B) fires done_tx BEFORE (A) has broadcast the last chunk,
    // the WS writer's try_recv() drain misses it.
    //
    // Rather than racing the scheduler, we test the structural defect
    // directly: done_tx can fire while the pump's broadcast channel
    // still has unconsumed source items in the ExecStdout mpsc.
    // A correct implementation would barrier the pump's completion
    // before firing done_tx.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn done_signal_must_wait_for_pump_completion() {
        let (active, stdout_tx, stderr_tx, result_tx) = make_test_active();

        // Subscribe BEFORE any data is pushed — the subscriber will
        // receive all broadcast chunks. No sleep-based polling.
        let mut rx = active.stdout_bus().subscribe();
        let mut done_rx = active.done_rx();

        // Push output, then signal exit immediately. The pump task
        // must read from ExecStdout and broadcast BEFORE done fires.
        stdout_tx.send("final-line\n".to_string()).unwrap();
        drop(stdout_tx);
        drop(stderr_tx);
        result_tx
            .send(boxlite::ExecResult {
                exit_code: 0,
                error_message: None,
            })
            .unwrap();

        // Wait for the done signal.
        let _ = tokio::time::timeout(Duration::from_secs(2), done_rx.changed()).await;

        // After done fires, the pump barrier guarantees all output has
        // been broadcast. Drain with try_recv — no sleep needed.
        let mut all = Vec::new();
        while let Ok(bytes) = rx.try_recv() {
            all.push(String::from_utf8(bytes).unwrap());
        }

        assert!(
            all.iter().any(|s| s.contains("final-line")),
            "after done_rx fires, all output must have been broadcast; \
             got: {:?}",
            all,
        );
    }

    // ---------------------------------------------------------------
    // Finding 3: reaper immediately evicts completed execs
    // ---------------------------------------------------------------
    //
    // run_reap_once removes is_done() execs on the very next tick.
    // The Go runner retains them for 5 minutes. A client that polls
    // GET /executions/{id} shortly after exit gets 404.
    //
    // We can't construct a full AppState without BoxliteRuntime, so
    // we build the executions map directly and call run_reap_once.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn reaper_retains_completed_exec_for_grace_period() {
        let (active, stdout_tx, stderr_tx, result_tx) = make_test_active();

        // Signal exit so is_done() flips true. Drop BOTH stream senders
        // so the pump tasks exit and the wait task's barrier completes.
        drop(stdout_tx);
        drop(stderr_tx);
        result_tx
            .send(boxlite::ExecResult {
                exit_code: 42,
                error_message: None,
            })
            .unwrap();
        for _ in 0..20 {
            if active.is_done() {
                break;
            }
            tokio::time::sleep(Duration::from_millis(50)).await;
        }
        assert!(active.is_done(), "precondition: exec must be done");

        // The exec was just created so created_at is recent — the
        // production should_retain() must return true.
        let now = Instant::now();
        assert!(
            active.should_retain(now),
            "recently-completed exec must be retained (grace period = {:?})",
            COMPLETED_RETENTION_GRACE,
        );

        // Conversely, a time far in the future should NOT retain.
        let far_future = now + COMPLETED_RETENTION_GRACE + Duration::from_secs(1);
        assert!(
            !active.should_retain(far_future),
            "exec past the retention grace must not be retained",
        );
    }

    /// A finished main session must not be handed to a restarted box.
    ///
    /// Unlike an exec — which gets a fresh id every time — the main session's id
    /// is the container id, fixed at box creation and therefore identical across
    /// reboots. So the previous run's dead session still matches this box, and
    /// without an `is_done()` filter a post-restart attach would be given the
    /// old VM's stream, its stale backlog and its stale exit code, while the new
    /// boot's init session was never registered at all.
    #[tokio::test]
    async fn find_main_session_skips_a_finished_one_so_a_restart_gets_a_new_session() {
        let (exec, channels) = stub_execution("cid-main");
        let finished = ActiveExecution::new("box1".to_string(), SessionKind::Main, exec, None);

        let mut executions = HashMap::new();
        executions.insert("cid-main".to_string(), Arc::clone(&finished));

        assert!(
            find_main_session(&executions, "box1").is_some(),
            "precondition: a live main session is found"
        );

        // End it, exactly as init exiting would: send the result and drop the
        // stream senders so the pumps finish.
        let StubChannels(stdout_tx, stderr_tx, _stdin_rx, result_tx) = channels;
        drop(stdout_tx);
        drop(stderr_tx);
        result_tx
            .send(boxlite::ExecResult {
                exit_code: 0,
                error_message: None,
            })
            .unwrap();
        for _ in 0..40 {
            if finished.is_done() {
                break;
            }
            tokio::time::sleep(Duration::from_millis(50)).await;
        }
        assert!(finished.is_done(), "precondition: the main session ended");

        assert!(
            find_main_session(&executions, "box1").is_none(),
            "a finished main session must not be reused — the restarted box needs a new one"
        );
    }

    /// The reaper must never reap the box's main session.
    ///
    /// That session is the container's init, so killing it powers the VM off
    /// and takes the whole box with it. Registering it in `state.executions` is
    /// what makes `/boxes/{id}/attach` work — but that map is the one the
    /// reaper walks, which put the user's box on the orphan-escalation path
    /// (SIGHUP → SIGTERM → SIGKILL → evict) the moment their client
    /// disconnected, and under the 24h lifetime cap even while still attached.
    /// Detaching from `docker attach` does not stop a container; nor may this.
    #[tokio::test]
    async fn reaper_never_reaps_the_boxs_main_session() {
        let runtime = BoxliteRuntime::rest(boxlite::BoxliteRestOptions::new(
            "http://127.0.0.1:1".to_string(),
        ))
        .expect("rest runtime");
        let state = AppState {
            runtime,
            boxes: RwLock::new(HashMap::new()),
            executions: RwLock::new(HashMap::new()),
            api_key: None,
            lifecycle: RwLock::new(HashMap::new()),
            last_activity: RwLock::new(HashMap::new()),
        };

        // The box's main command, and an ordinary exec running beside it.
        let (init, _init_channels) = stub_execution("cid-main");
        let main = Arc::new(ActiveExecution::new(
            "box1".to_string(),
            SessionKind::Main,
            init,
            None,
        ));
        let (tenant_exec, _tenant_channels) = stub_execution("exec-1");
        let tenant = Arc::new(ActiveExecution::new(
            "box1".to_string(),
            SessionKind::Exec,
            tenant_exec,
            None,
        ));
        {
            let mut map = state.executions.write().await;
            map.insert("cid-main".to_string(), Arc::clone(&main));
            map.insert("exec-1".to_string(), Arc::clone(&tenant));
        }

        // Neither was ever attached, and we reap from far enough in the future
        // that the lifetime cap has long since passed — the harshest state the
        // reaper knows.
        let doomsday = Instant::now() + Duration::from_secs(48 * 60 * 60);
        run_reap_once(
            &state,
            doomsday,
            Duration::from_secs(1),
            Duration::from_secs(1),
            Duration::from_secs(60),
        )
        .await;

        let surviving = state.executions.read().await;
        assert!(
            surviving.contains_key("cid-main"),
            "the box's main session must survive the reaper — reaping it kills init and destroys the box"
        );
        assert!(
            !main.is_reaping_kill().await,
            "the main session must never even be marked for kill"
        );

        // Control: the exec beside it *is* reaped under the same tick, so this
        // proves the Main guard rather than a reaper that happens to be inert.
        assert!(
            !surviving.contains_key("exec-1"),
            "an orphaned exec past the lifetime cap must still be killed and evicted"
        );
    }
}