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
//! Composite runtime that dispatches per-container to a primary + optional delegate.
//!
//! The [`CompositeRuntime`] owns a *primary* runtime (the node-native runtime —
//! e.g. `HcsRuntime` on Windows, `YoukiRuntime` on Linux, Docker elsewhere) and
//! an optional *delegate* runtime used for foreign-OS workloads (e.g. a WSL2
//! delegate on Windows that runs Linux containers). Each call is routed based
//! on the container's identity:
//!
//! * **[`Runtime::create_container`]** consults
//! [`ServiceSpec::platform`](zlayer_spec::ServiceSpec) first; when the
//! spec's OS targets the delegate we route there, otherwise primary. When
//! `platform` is `None`, a secondary **image-OS cache** (populated by
//! [`CompositeRuntime::record_image_os`] from OCI manifest inspection at
//! pull time) is consulted. If both are unknown we fall through to the
//! primary. **Strict policy (H-7):** if either source identifies the
//! workload as Linux and this node has no delegate configured, dispatch
//! returns [`AgentError::RouteToPeer`] so the scheduler can re-place the
//! workload on a Linux peer — the old permissive "fall through to primary"
//! behavior is gone.
//! * All subsequent per-container operations (start/stop/remove/logs/exec/…)
//! look up the container in an internal **dispatch cache** that records
//! which runtime created it. This guarantees the same runtime sees the
//! container for its whole lifecycle, even after daemon restarts within
//! the same process.
//! * Cross-cutting image operations (`pull_image`, `pull_image_with_policy`,
//! `list_images`, `prune_images`) fan out to both runtimes — we cannot know
//! in advance which runtime will execute a pulled image, and merged image
//! listings give users a single coherent view. `remove_image` / `tag_image`
//! try primary first and fall back to delegate.
//!
//! The dispatch cache is populated on `create_container` and cleared on
//! `remove_container`. Looking up an unknown id yields
//! [`AgentError::NotFound`], which surfaces as a clean 404 at the API layer
//! rather than silently forwarding to the wrong runtime.
use std::collections::HashMap;
use std::net::IpAddr;
use std::sync::Arc;
use std::time::Duration;
use async_trait::async_trait;
use tokio::sync::RwLock;
use zlayer_observability::logs::{LogEntry, LogStream};
use zlayer_spec::{OsKind, PullPolicy, RegistryAuth, ServiceSpec};
use crate::cgroups_stats::ContainerStats;
use crate::error::{AgentError, Result};
use crate::runtime::{
ContainerId, ContainerInspectDetails, ContainerState, ExecEventStream, ImageInfo, LogChannel,
LogChunk, LogsStream, LogsStreamOptions, OverlayAttachKind, PruneResult, Runtime, StatsSample,
StatsStream, WaitCondition, WaitOutcome,
};
/// Which underlying runtime a given container was dispatched to.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum DispatchTarget {
Primary,
Delegate,
/// The Apple-Virtualization (VZ) delegate (macOS only). Selected
/// automatically for `com.zlayer.runtime=vz` base bundles, or per-service
/// via the `com.zlayer.isolation=vz` label.
Vz,
/// The Apple-Virtualization **Linux-guest** delegate (macOS only). The
/// default Linux path on macOS: selected for Linux images, the
/// `com.zlayer.runtime=vz-linux` marker, or the
/// `com.zlayer.isolation=vz-linux` label.
VzLinux,
}
/// Routes each container to either the primary runtime or an optional delegate.
///
/// See the module-level documentation for the dispatch rules.
pub struct CompositeRuntime {
primary: Arc<dyn Runtime>,
delegate: Option<Arc<dyn Runtime>>,
/// Opt-in Apple-Virtualization delegate (macOS). Selected only when a
/// service carries `com.zlayer.isolation=vz`.
vz: Option<Arc<dyn Runtime>>,
/// Apple-Virtualization Linux-guest delegate (macOS). When present, it is
/// the default runtime for Linux images on this node; libkrun
/// (`delegate`) is then reachable only via `com.zlayer.isolation=vm`.
vz_linux: Option<Arc<dyn Runtime>>,
/// Per-container dispatch cache. Populated on `create_container`, removed
/// on `remove_container`.
dispatch: Arc<RwLock<HashMap<ContainerId, DispatchTarget>>>,
/// Image-OS cache consulted when a spec has no explicit `platform`.
/// Populated by [`CompositeRuntime::record_image_os`], which is driven
/// from [`zlayer_registry::fetch_image_os`] during `pull_image*`.
image_os: Arc<RwLock<HashMap<String, OsKind>>>,
/// Image runtime-marker cache (the `com.zlayer.runtime` manifest
/// annotation, e.g. `"vz"`). Populated from
/// [`zlayer_registry::fetch_image_runtime_marker`] during `pull_image*` so
/// `select_for` can auto-detect a VZ base bundle and prefer the VZ runtime
/// for it without requiring a per-service label.
image_runtime: Arc<RwLock<HashMap<String, String>>>,
/// Filesystem paths of the persistent blob caches that the runtimes pull
/// into, tried IN ORDER for image-OS / runtime-marker inspection. Typically:
///
/// 1. the VZ-Linux runtime's `{data_dir}/vz/linux/images/blobs.redb` (the
/// delegate that actually runs the Linux workload), and
/// 2. the primary Sandbox runtime's `{data_dir}/images/blobs.redb`.
///
/// Both stores matter because `pull_image` pulls into BOTH (primary first,
/// then VZ-Linux), and either pull may short-circuit under
/// `PullPolicy::IfNotPresent` when its rootfs already exists — leaving the
/// manifest/config in only ONE of the two caches. Inspection therefore
/// probes them in order and stops at the first store that resolves the OS,
/// LOCAL-ONLY via [`zlayer_registry::fetch_image_os_in_cache_only`] — so an
/// already-pulled Linux image is detected as Linux (and routed to VZ-Linux)
/// with NO network call, even under a Docker Hub rate-limit. For the OS
/// dispatch path there is intentionally **no** network fallback: a local
/// miss yields "OS unknown" and dispatch uses its safe macOS default rather
/// than risking a 429 (see [`CompositeRuntime::inspect_image_os`]).
os_inspect_cache_paths: Vec<std::path::PathBuf>,
}
impl CompositeRuntime {
/// Construct a new composite runtime.
///
/// `primary` handles containers whose platform matches the host node.
/// `delegate`, when present, handles foreign-OS containers (currently:
/// Linux containers on a Windows host via the WSL2 delegate runtime).
#[must_use]
pub fn new(primary: Arc<dyn Runtime>, delegate: Option<Arc<dyn Runtime>>) -> Self {
Self {
primary,
delegate,
vz: None,
vz_linux: None,
dispatch: Arc::new(RwLock::new(HashMap::new())),
image_os: Arc::new(RwLock::new(HashMap::new())),
image_runtime: Arc::new(RwLock::new(HashMap::new())),
os_inspect_cache_paths: Vec::new(),
}
}
/// Point image-OS / runtime-marker inspection at a single persistent blob
/// cache the runtimes pull into, so the OS of an already-pulled image
/// resolves from the LOCAL config blob with no network round-trip.
///
/// Convenience wrapper over [`CompositeRuntime::with_os_inspect_cache_paths`]
/// for callers that only have one store. `path` is the on-disk blob-cache
/// file (e.g. the VZ-Linux runtime's `{data_dir}/vz/linux/images/blobs.redb`).
#[must_use]
pub fn with_os_inspect_cache_path(self, path: Option<std::path::PathBuf>) -> Self {
self.with_os_inspect_cache_paths(path.into_iter().collect())
}
/// Point image-OS / runtime-marker inspection at an ORDERED list of
/// persistent blob caches the runtimes pull into.
///
/// Inspection probes each store LOCAL-ONLY (no network) in order and stops
/// at the first that resolves the image's OS / marker. This matters because
/// `pull_image` pulls into BOTH the VZ-Linux store and the primary Sandbox
/// store, and either pull may short-circuit under `PullPolicy::IfNotPresent`
/// when its rootfs already exists — leaving the manifest/config in only ONE
/// of the two caches. Probing both (VZ-Linux first, then primary) is what
/// lets a locally-cached Linux image route to VZ-Linux under a Docker Hub
/// rate-limit (see [`zlayer_registry::fetch_image_os_in_cache_only`]).
#[must_use]
pub fn with_os_inspect_cache_paths(mut self, paths: Vec<std::path::PathBuf>) -> Self {
self.os_inspect_cache_paths = paths;
self
}
/// Resolve `image`'s OS for **dispatch**, probing each configured local blob
/// cache in order, **LOCAL-ONLY — never a network call**.
///
/// This is the dispatch-population path: it runs inside `pull_image*` purely
/// to fill the image-OS cache that [`CompositeRuntime::select_for`] consults.
/// It MUST NOT touch the wire. The image's layers have already been pulled
/// and extracted by the time we get here, and the runtimes that did the pull
/// (VZ-Linux / Sandbox) wrote the manifest+config into the very blob caches
/// `os_inspect_cache_paths` points at — so the OS is knowable with zero
/// network round-trips.
///
/// The old code fell back to a network inspection (`fetch_image_os`) when no
/// local cache resolved the OS. That network call was reachable on a Docker
/// Hub 429, and a failed inspection left the cache empty → a cached Linux
/// image (e.g. `alpine`) fell through to the Seatbelt sandbox (`Primary`),
/// which cannot exec a Linux ELF (exit 127). The network fallback is gone:
/// a registry rate-limit can no longer break dispatch here. A genuine local
/// miss simply returns `Ok(None)` (dispatch then uses its safe macOS
/// fallthrough), and it never errors or blocks.
async fn inspect_image_os(
&self,
image: &str,
) -> std::result::Result<Option<OsKind>, zlayer_registry::RegistryError> {
for path in &self.os_inspect_cache_paths {
match zlayer_registry::CacheType::persistent_at(path)
.build()
.await
{
Ok(cache) => {
match zlayer_registry::fetch_image_os_in_cache_only(image, cache, None).await {
Ok(Some(os)) => return Ok(Some(os)),
Ok(None) => {
tracing::trace!(
image,
cache = %path.display(),
"image OS not resolvable from this local cache; trying next",
);
}
Err(e) => return Err(e),
}
}
Err(e) => {
tracing::debug!(
image,
cache = %path.display(),
error = %e,
"failed to open OS-inspect blob cache; trying next",
);
}
}
}
// No local cache resolved it. We deliberately do NOT fall back to a
// network inspection: a Docker Hub 429 must never reach this
// dispatch-population path (see the doc comment above). A clean local
// miss is `Ok(None)` — dispatch falls through to its safe macOS default.
Ok(None)
}
/// Resolve `image`'s `com.zlayer.runtime` marker, probing each configured
/// local blob cache in order (no network per cache) before any network call.
async fn inspect_image_runtime_marker(
&self,
image: &str,
auth: Option<&RegistryAuth>,
) -> std::result::Result<Option<String>, zlayer_registry::RegistryError> {
for path in &self.os_inspect_cache_paths {
match zlayer_registry::CacheType::persistent_at(path)
.build()
.await
{
Ok(cache) => {
match zlayer_registry::fetch_image_runtime_marker_in_cache_only(
image, cache, None,
)
.await
{
Ok(Some(marker)) => return Ok(Some(marker)),
Ok(None) => {
tracing::trace!(
image,
cache = %path.display(),
"runtime marker not resolvable from this local cache; trying next",
);
}
Err(e) => return Err(e),
}
}
Err(e) => {
tracing::debug!(
image,
cache = %path.display(),
error = %e,
"failed to open marker-inspect blob cache; trying next",
);
}
}
}
zlayer_registry::fetch_image_runtime_marker(image, auth).await
}
/// Attach an opt-in Apple-Virtualization delegate. Services labelled
/// `com.zlayer.isolation=vz` route to it; everything else is unaffected.
#[must_use]
pub fn with_vz_delegate(mut self, vz: Option<Arc<dyn Runtime>>) -> Self {
self.vz = vz;
self
}
/// Attach the Apple-Virtualization Linux-guest delegate. When present it
/// becomes the **default** runtime for Linux images on this node (libkrun
/// is then reachable only via the explicit `com.zlayer.isolation=vm`
/// label); when `None`, Linux dispatch falls back to the libkrun delegate
/// or `RouteToPeer` as before.
#[must_use]
pub fn with_vz_linux_delegate(mut self, vz_linux: Option<Arc<dyn Runtime>>) -> Self {
self.vz_linux = vz_linux;
self
}
/// Access the primary runtime (for introspection / tests).
#[must_use]
pub fn primary(&self) -> &Arc<dyn Runtime> {
&self.primary
}
/// Access the delegate runtime, if one is configured.
#[must_use]
pub fn delegate(&self) -> Option<&Arc<dyn Runtime>> {
self.delegate.as_ref()
}
/// Record that `image` is known to target operating system `os`.
///
/// Wired from [`zlayer_registry::fetch_image_os`] during `pull_image*`
/// (see [`CompositeRuntime::apply_image_os_inspection`]) so that specs
/// without an explicit `platform` still dispatch correctly.
pub(crate) async fn record_image_os(&self, image: &str, os: OsKind) {
self.image_os.write().await.insert(image.to_string(), os);
}
/// Record an image's `com.zlayer.runtime` marker (e.g. `"vz"`), used by
/// [`CompositeRuntime::select_for`] to auto-detect a runtime-specific bundle.
pub(crate) async fn record_image_runtime(&self, image: &str, marker: String) {
self.image_runtime
.write()
.await
.insert(image.to_string(), marker);
}
/// Apply a manifest runtime-marker inspection to the cache. Mirrors
/// [`CompositeRuntime::apply_image_os_inspection`]'s non-fatal contract:
/// only a present marker updates the cache; absence or error leaves it
/// untouched (dispatch falls through to the OS/platform rules).
async fn apply_image_runtime_inspection(
&self,
image: &str,
result: std::result::Result<Option<String>, zlayer_registry::RegistryError>,
) {
match result {
Ok(Some(marker)) => {
tracing::debug!(image, marker, "cached image runtime marker for dispatch");
self.record_image_runtime(image, marker).await;
}
Ok(None) => {}
Err(e) => {
tracing::trace!(
image,
error = %e,
"failed to inspect image runtime marker — dispatch unaffected",
);
}
}
}
/// Apply the result of a manifest OS inspection to the image-OS cache.
///
/// Factored out of [`Runtime::pull_image`] and
/// [`Runtime::pull_image_with_policy`] so the cache-update policy can be
/// unit-tested without depending on a live registry. The three branches
/// mirror the contract of [`zlayer_registry::fetch_image_os`]:
///
/// * `Ok(Some(os))` — populate the cache so future `create_container`
/// calls without an explicit `spec.platform` dispatch to the right
/// runtime.
/// * `Ok(None)` — the config blob had no (or an unrecognized) `os`
/// field. Leave the cache untouched; dispatch falls through to primary.
/// * `Err(_)` — transient or registry error. Log at warn and leave the
/// cache untouched. We never fail the overall `pull_image*` call on
/// inspection failure: the primary runtime's own pull already
/// succeeded, and the safe fall-through is "primary".
async fn apply_image_os_inspection(
&self,
image: &str,
result: std::result::Result<Option<OsKind>, zlayer_registry::RegistryError>,
) {
match result {
Ok(Some(os)) => {
self.record_image_os(image, os).await;
tracing::debug!(image, ?os, "cached image OS for dispatch");
}
Ok(None) => {
tracing::trace!(
image,
"image manifest has no OS field — dispatch will fall through to primary",
);
}
Err(e) => {
tracing::warn!(
image,
error = %e,
"failed to inspect image manifest OS — dispatch will fall through to primary",
);
}
}
}
/// Decide which runtime should handle a `create_container` call for `spec`.
///
/// The `service` argument is the originating service name, used to build an
/// actionable [`AgentError::RouteToPeer`] when a Linux workload lands on
/// this node without a local delegate so the scheduler can re-place it on
/// a capable peer.
///
/// Policy (H-7): Linux workloads are never silently routed to the primary
/// on nodes without a delegate. The old "permissive" fall-through (primary
/// handles everything) returned an `Unsupported` error only when
/// `spec.platform` was explicitly set, but fell through to primary for
/// specs without a platform — producing cryptic downstream errors when the
/// image-OS cache said `Linux`. We now return `RouteToPeer` in both cases.
///
/// Routing precedence, locally-known OS only (NO network call ever happens
/// here — the image-OS cache is filled local-only at pull time):
/// 1. explicit `com.zlayer.isolation` label,
/// 2. `com.zlayer.runtime` manifest marker (`vz` / `vz-linux`),
/// 3. `spec.platform.os`,
/// 4. the image-OS cache: `Linux` -> `VzLinux` (when present), `Macos` /
/// `Windows` -> `Primary`,
/// 5. FINAL fallthrough — OS genuinely unknown: on a macOS host (proxied by
/// the presence of a `vz_linux` delegate) default to `VzLinux`, because
/// almost every registry image is Linux and the Seatbelt sandbox cannot
/// exec a Linux ELF. A macOS-native rootfs never reaches this branch: it
/// resolves `os == Macos` at step 4 and routes to `Primary`.
async fn select_for(&self, service: &str, spec: &ServiceSpec) -> Result<DispatchTarget> {
// Explicit per-service isolation label wins over everything below.
// `com.zlayer.isolation=vz` -> VZ (native-macOS guest VM)
// `com.zlayer.isolation=vz-linux` -> VZ Linux-guest VM
// `com.zlayer.isolation=vm|libkrun` -> libkrun delegate (force VM)
// `com.zlayer.isolation=sandbox|seatbelt` -> Seatbelt sandbox (primary),
// opting OUT of VZ auto-detect.
if let Some(label) = spec.labels.get("com.zlayer.isolation") {
if self.vz.is_some() && label.eq_ignore_ascii_case("vz") {
return Ok(DispatchTarget::Vz);
}
if self.vz_linux.is_some() && label.eq_ignore_ascii_case("vz-linux") {
return Ok(DispatchTarget::VzLinux);
}
if label.eq_ignore_ascii_case("vm") || label.eq_ignore_ascii_case("libkrun") {
// Force the libkrun delegate. If no delegate exists the
// platform/image-OS rules below produce the appropriate
// `RouteToPeer`, so only short-circuit when one is present.
if self.delegate.is_some() {
return Ok(DispatchTarget::Delegate);
}
}
if label.eq_ignore_ascii_case("sandbox") || label.eq_ignore_ascii_case("seatbelt") {
return Ok(DispatchTarget::Primary);
}
}
// Auto-detect a VZ base bundle: when the image's manifest carries
// `com.zlayer.runtime=vz` (stamped by `zlayer vz build-base`), prefer the
// VZ runtime — it is the only runtime that can boot such a bundle. This
// is the "prefer VZ by default" behaviour: it only fires for genuine VZ
// bundles, so Seatbelt-rootfs and Linux images are unaffected.
if self.vz.is_some()
&& self
.image_runtime
.read()
.await
.get(&spec.image.name.to_string())
.is_some_and(|m| m.eq_ignore_ascii_case(zlayer_registry::ZLAYER_RUNTIME_VZ))
{
return Ok(DispatchTarget::Vz);
}
// Auto-detect a VZ Linux-guest image: when the manifest carries
// `com.zlayer.runtime=vz-linux`, prefer the VZ Linux runtime.
if self.vz_linux.is_some()
&& self
.image_runtime
.read()
.await
.get(&spec.image.name.to_string())
.is_some_and(|m| m.eq_ignore_ascii_case(zlayer_registry::ZLAYER_RUNTIME_LINUX_VZ))
{
return Ok(DispatchTarget::VzLinux);
}
if let Some(platform) = &spec.platform {
let target = match platform.os {
OsKind::Windows | OsKind::Macos => DispatchTarget::Primary,
// On macOS the VZ Linux-guest runtime is the default Linux path;
// only fall back to the libkrun delegate when it is absent.
OsKind::Linux if self.vz_linux.is_some() => DispatchTarget::VzLinux,
OsKind::Linux => DispatchTarget::Delegate,
};
if matches!(target, DispatchTarget::Delegate) && self.delegate.is_none() {
return Err(AgentError::RouteToPeer {
service: service.to_string(),
required_os: OsKind::Linux.as_oci_str().to_string(),
reason: "spec.platform.os = linux but this node has no WSL2 delegate \
configured; enable `--install-wsl yes` on this node or add a Linux \
peer to the cluster"
.to_string(),
});
}
return Ok(target);
}
if let Some(os) = self
.image_os
.read()
.await
.get(&spec.image.name.to_string())
.copied()
{
return match os {
OsKind::Linux => {
if self.vz_linux.is_some() {
// VZ Linux-guest is the default Linux path on macOS.
Ok(DispatchTarget::VzLinux)
} else if self.delegate.is_some() {
Ok(DispatchTarget::Delegate)
} else {
// No delegate and the image manifest says Linux —
// refuse at the composite layer so the scheduler can
// re-place on a Linux peer instead of the primary
// failing with a cryptic HCS error.
Err(AgentError::RouteToPeer {
service: service.to_string(),
required_os: OsKind::Linux.as_oci_str().to_string(),
reason: format!(
"image '{}' manifest reports os=linux but this node has no WSL2 \
delegate configured; enable `--install-wsl yes` on this node or \
add a Linux peer to the cluster",
spec.image.name
),
})
}
}
OsKind::Windows | OsKind::Macos => Ok(DispatchTarget::Primary),
};
}
// OS genuinely unknown (no isolation label, no runtime marker, no
// `spec.platform`, no image-OS cache hit). On a macOS host with a
// VZ-Linux delegate, default to VZ-Linux: the overwhelming majority of
// images pulled from public registries are Linux, and the Seatbelt
// sandbox (the primary) cannot exec a Linux ELF — sending an unknown
// image there is the exit-127 failure this fix exists to prevent. The
// user is fine with VZ-Linux as the default; the only hard rule is that
// a macOS-native rootfs must never go to the Linux VM, and that is
// already guaranteed above by the `image_os == Macos -> Primary` branch
// (a native bundle resolves its OS locally and never reaches here).
//
// The `vz_linux` delegate is only ever attached on a macOS host, so its
// presence is a sufficient proxy for "macOS host" — non-macOS hosts
// (Windows HCS, Linux) keep the historical primary fallthrough.
if self.vz_linux.is_some() {
return Ok(DispatchTarget::VzLinux);
}
Ok(DispatchTarget::Primary)
}
/// Look up an existing dispatch decision for `id`, or return `NotFound`.
async fn lookup(&self, id: &ContainerId) -> Result<Arc<dyn Runtime>> {
let target =
self.dispatch
.read()
.await
.get(id)
.copied()
.ok_or_else(|| AgentError::NotFound {
container: id.to_string(),
reason: "no dispatch record in CompositeRuntime".to_string(),
})?;
Ok(self.runtime_for(target).clone())
}
/// Resolve a [`DispatchTarget`] to the concrete runtime reference.
///
/// Unwrapping the delegate is safe because [`Self::select_for`] returns
/// `Err` whenever a delegate would be required but is missing, so a
/// `DispatchTarget::Delegate` can never end up in the dispatch map
/// without a delegate being present.
fn runtime_for(&self, t: DispatchTarget) -> &Arc<dyn Runtime> {
match t {
DispatchTarget::Primary => &self.primary,
DispatchTarget::Delegate => self
.delegate
.as_ref()
.expect("delegate target requires delegate to exist"),
// `select_for` only returns `Vz` when a vz delegate is present;
// fall back to primary defensively.
DispatchTarget::Vz => self.vz.as_ref().unwrap_or(&self.primary),
// `select_for` only returns `VzLinux` when a vz-linux delegate is
// present; fall back to primary defensively.
DispatchTarget::VzLinux => self.vz_linux.as_ref().unwrap_or(&self.primary),
}
}
/// Build the ordered list of backends to try for a per-container read
/// (logs / stats), owning backend first.
///
/// The container's dispatch record (recorded at `create_container`) names
/// the runtime that actually ran it, so we try that one first. The other
/// configured backends follow as a defensive fallback for the case where
/// the owning backend can answer container lifecycle calls but not a
/// particular read (e.g. the macOS `SandboxRuntime` primary implements
/// `container_logs`/`get_container_stats` but not the *streaming*
/// `logs_stream`/`stats_stream`, so it returns `Unsupported` for the
/// latter). Returns `NotFound` when the id was never dispatched.
async fn read_backends(
&self,
id: &ContainerId,
) -> Result<Vec<(&'static str, Arc<dyn Runtime>)>> {
let owner =
self.dispatch
.read()
.await
.get(id)
.copied()
.ok_or_else(|| AgentError::NotFound {
container: id.to_string(),
reason: "no dispatch record in CompositeRuntime".to_string(),
})?;
// Owning backend first, then every other configured backend (de-duped
// against the owner) so a read the owner can't serve can still be
// satisfied elsewhere instead of 500-ing.
let all: [(DispatchTarget, Option<&Arc<dyn Runtime>>); 4] = [
(DispatchTarget::Primary, Some(&self.primary)),
(DispatchTarget::Delegate, self.delegate.as_ref()),
(DispatchTarget::Vz, self.vz.as_ref()),
(DispatchTarget::VzLinux, self.vz_linux.as_ref()),
];
let label_for = |t: DispatchTarget| match t {
DispatchTarget::Primary => "primary",
DispatchTarget::Delegate => "delegate",
DispatchTarget::Vz => "vz",
DispatchTarget::VzLinux => "vz_linux",
};
let mut out: Vec<(&'static str, Arc<dyn Runtime>)> =
vec![(label_for(owner), self.runtime_for(owner).clone())];
for (target, rt) in all {
if target != owner {
if let Some(rt) = rt {
out.push((label_for(target), rt.clone()));
}
}
}
Ok(out)
}
}
/// Accumulates per-backend errors while a read fans out across the
/// owner-first fallback chain, so the *final* error reflects the right HTTP
/// status.
///
/// Every backend in the chain is tried; a backend that does not own the
/// container returns [`AgentError::NotFound`] (a *skip*, not authoritative),
/// while a backend that owns it but cannot serve this particular read returns
/// some other error (notably the `Unsupported` default for an unimplemented
/// streaming read) — a *soft miss* we fall back from. The distinction matters
/// for the final error: if **every** backend returned `NotFound`, the container
/// genuinely does not exist here and we surface `NotFound` (→ 404); if any
/// backend produced a non-`NotFound` error, that is the more informative
/// failure to report (→ 500) once no backend could serve the read.
#[derive(Default)]
struct ReadMissAccumulator {
/// The most recent non-`NotFound` error, if any backend produced one.
soft_err: Option<AgentError>,
/// The most recent `NotFound`, used only when *no* soft error occurred.
not_found: Option<AgentError>,
}
impl ReadMissAccumulator {
fn record(&mut self, e: AgentError) {
if matches!(e, AgentError::NotFound { .. }) {
self.not_found = Some(e);
} else {
self.soft_err = Some(e);
}
}
/// Resolve the accumulated misses into the final error for a read where no
/// backend succeeded. Prefers a soft error (more informative → 500) over a
/// bare `NotFound`; falls back to a synthesised `Unsupported` only if
/// nothing was recorded at all (an empty backend list, which cannot happen
/// in practice since the owner is always present).
fn into_error(self, what: &str) -> AgentError {
self.soft_err
.or(self.not_found)
.unwrap_or_else(|| AgentError::Unsupported(format!("no backend could serve {what}")))
}
}
/// Build a bounded one-shot [`LogsStream`] from a captured-log snapshot.
///
/// Used by [`CompositeRuntime::logs_stream`] when no backend offers a native
/// log stream but one can produce a `container_logs` snapshot (e.g. the macOS
/// `SandboxRuntime`). Mirrors the VZ-Linux runtime's own snapshot-to-stream
/// translation so the wire shape is identical regardless of which backend
/// served the data: honour the per-channel filters and re-attach the newline
/// the line-splitter stripped.
fn one_shot_logs_stream(entries: Vec<LogEntry>, opts: &LogsStreamOptions) -> LogsStream {
use futures_util::stream;
// Docker's default (neither stdout nor stderr explicitly requested) means
// "both"; equivalently, keep stdout unless stderr was the *only* channel
// requested, and vice-versa.
let want_stdout = opts.stdout || !opts.stderr;
let want_stderr = opts.stderr || !opts.stdout;
let timestamps = opts.timestamps;
let chunks: Vec<Result<LogChunk>> = entries
.into_iter()
.filter_map(|e| {
let channel = match e.stream {
LogStream::Stdout => LogChannel::Stdout,
LogStream::Stderr => LogChannel::Stderr,
};
let keep = match channel {
LogChannel::Stdout => want_stdout,
LogChannel::Stderr => want_stderr,
LogChannel::Stdin => false,
};
if !keep {
return None;
}
let mut bytes = e.message.into_bytes();
bytes.push(b'\n');
Some(Ok(LogChunk {
stream: channel,
bytes: bytes::Bytes::from(bytes),
timestamp: timestamps.then_some(e.timestamp),
}))
})
.collect();
Box::pin(stream::iter(chunks))
}
/// Build a bounded one-shot [`StatsStream`] from a single [`ContainerStats`]
/// snapshot.
///
/// Used by [`CompositeRuntime::stats_stream`] when no backend offers a native
/// stats stream but one can produce a `get_container_stats` snapshot. The
/// [`ContainerStats`] CPU figure is microseconds; [`StatsSample::cpu_total_ns`]
/// is nanoseconds, so we scale. `online_cpus` is unknown from this coarse
/// snapshot (the non-streaming API does not carry it) and is reported as `1`
/// so the Docker-compat CPU-percent math has a sane divisor.
fn one_shot_stats_stream(stats: &ContainerStats) -> StatsStream {
use futures_util::stream;
let sample = StatsSample {
cpu_total_ns: stats.cpu_usage_usec.saturating_mul(1_000),
cpu_system_ns: 0,
online_cpus: 1,
mem_used_bytes: stats.memory_bytes,
mem_limit_bytes: stats.memory_limit,
net_rx_bytes: 0,
net_tx_bytes: 0,
blkio_read_bytes: 0,
blkio_write_bytes: 0,
pids_current: 0,
pids_limit: None,
timestamp: chrono::Utc::now(),
};
Box::pin(stream::iter(vec![Ok(sample)]))
}
#[async_trait]
impl Runtime for CompositeRuntime {
async fn pull_image(&self, image: &str) -> Result<()> {
// Primary pull. `WrongPlatform` here means the image's OCI config
// reports an OS the primary cannot service (e.g. a Linux image on the
// Windows HCS runtime). That is a *soft* failure: the delegate's pull
// below owns the image, so we log and continue rather than failing
// the whole composite call. Any other error is a real pull failure
// and must bubble.
if let Err(e) = self.primary.pull_image(image).await {
if matches!(e, AgentError::WrongPlatform { .. }) {
tracing::debug!(
image,
error = %e,
"primary runtime cannot service image (wrong platform); delegating",
);
} else {
return Err(e);
}
}
if let Some(delegate) = &self.delegate {
if let Err(e) = delegate.pull_image(image).await {
// Foreign-OS images will reliably fail one of the two pulls
// (primary can't store a Linux image's config on Windows, or
// vice versa). That's expected — the successful side owns the
// layers we'll actually use — so we keep this at debug.
tracing::debug!(
image,
error = %e,
"delegate runtime failed to pull image (likely wrong OS); continuing with primary result",
);
}
}
// VZ + VZ-Linux delegates (macOS). The VZ-Linux runtime is the default
// execution path for Linux images on macOS and owns its OWN image store
// (`image_rootfs`); if we never pull into it, the image is absent both
// when `create_container` dispatches there AND from `list_images` /
// `inspect_image` (which is what `docker pull` verifies). Pulling here
// makes the image actually present where it runs and listable. Errors
// are non-fatal for the same wrong-OS reason as the delegate above.
for (label, rt) in [
self.vz.as_ref().map(|r| ("vz", r)),
self.vz_linux.as_ref().map(|r| ("vz_linux", r)),
]
.into_iter()
.flatten()
{
if let Err(e) = rt.pull_image(image).await {
tracing::debug!(
image,
runtime = label,
error = %e,
"vz delegate failed to pull image (likely wrong OS); continuing",
);
}
}
// Inspect the OCI manifest's `config.os` so `select_for(spec)` can
// dispatch correctly when `spec.platform` is `None`. Non-fatal: any
// failure here just means dispatch falls through to primary.
let os_result = self.inspect_image_os(image).await;
self.apply_image_os_inspection(image, os_result).await;
let marker_result = self.inspect_image_runtime_marker(image, None).await;
self.apply_image_runtime_inspection(image, marker_result)
.await;
Ok(())
}
async fn pull_image_with_policy(
&self,
image: &str,
policy: PullPolicy,
auth: Option<&RegistryAuth>,
source: zlayer_spec::SourcePolicy,
) -> Result<()> {
// See `pull_image` above for the `WrongPlatform` soft-skip rationale.
if let Err(e) = self
.primary
.pull_image_with_policy(image, policy, auth, source)
.await
{
if matches!(e, AgentError::WrongPlatform { .. }) {
tracing::debug!(
image,
error = %e,
"primary runtime cannot service image (wrong platform); delegating",
);
} else {
return Err(e);
}
}
if let Some(delegate) = &self.delegate {
if let Err(e) = delegate
.pull_image_with_policy(image, policy, auth, source)
.await
{
tracing::debug!(
image,
error = %e,
"delegate runtime failed to pull image (likely wrong OS); continuing with primary result",
);
}
}
// See `pull_image` above: the VZ-Linux runtime owns its own image store
// and is the default Linux execution path on macOS, so pull into it (and
// the opt-in VZ delegate) too. Non-fatal per-backend errors.
for (label, rt) in [
self.vz.as_ref().map(|r| ("vz", r)),
self.vz_linux.as_ref().map(|r| ("vz_linux", r)),
]
.into_iter()
.flatten()
{
if let Err(e) = rt.pull_image_with_policy(image, policy, auth, source).await {
tracing::debug!(
image,
runtime = label,
error = %e,
"vz delegate failed to pull image (likely wrong OS); continuing",
);
}
}
let os_result = self.inspect_image_os(image).await;
self.apply_image_os_inspection(image, os_result).await;
let marker_result = self.inspect_image_runtime_marker(image, auth).await;
self.apply_image_runtime_inspection(image, marker_result)
.await;
Ok(())
}
async fn create_container(&self, id: &ContainerId, spec: &ServiceSpec) -> Result<()> {
let target = self.select_for(&id.service, spec).await?;
{
let mut dispatch = self.dispatch.write().await;
dispatch.insert(id.clone(), target);
}
let rt = self.runtime_for(target).clone();
match rt.create_container(id, spec).await {
Ok(()) => Ok(()),
Err(e) => {
// Roll back the cache insert on failure so subsequent lookups
// don't find a dangling entry.
self.dispatch.write().await.remove(id);
Err(e)
}
}
}
async fn start_container(&self, id: &ContainerId) -> Result<()> {
let rt = self.lookup(id).await?;
rt.start_container(id).await
}
async fn stop_container(&self, id: &ContainerId, timeout: Duration) -> Result<()> {
let rt = self.lookup(id).await?;
rt.stop_container(id, timeout).await
}
async fn remove_container(&self, id: &ContainerId) -> Result<()> {
let rt = self.lookup(id).await?;
let res = rt.remove_container(id).await;
self.dispatch.write().await.remove(id);
res
}
async fn container_state(&self, id: &ContainerId) -> Result<ContainerState> {
let rt = self.lookup(id).await?;
rt.container_state(id).await
}
async fn container_logs(&self, id: &ContainerId, tail: usize) -> Result<Vec<LogEntry>> {
let backends = self.read_backends(id).await?;
let mut misses = ReadMissAccumulator::default();
for (label, rt) in backends {
match rt.container_logs(id, tail).await {
Ok(logs) => return Ok(logs),
Err(e) => {
tracing::warn!(
container = %id,
runtime = label,
error = %e,
"composite container_logs: backend could not serve logs; trying next backend",
);
misses.record(e);
}
}
}
Err(misses.into_error("container_logs"))
}
async fn exec(&self, id: &ContainerId, cmd: &[String]) -> Result<(i32, String, String)> {
let rt = self.lookup(id).await?;
rt.exec(id, cmd).await
}
async fn exec_with_opts(
&self,
id: &ContainerId,
opts: &crate::runtime::ExecOptions,
) -> Result<(i32, String, String)> {
// Forward to the resolved backend's `exec_with_opts` so Docker exec
// options (`--user`, `-w`, `-e`) reach the runtime that actually owns
// the container. Without this override the trait default would call
// `self.exec(opts.command)` and silently drop user/cwd/env.
let rt = self.lookup(id).await?;
rt.exec_with_opts(id, opts).await
}
async fn exec_stream(&self, id: &ContainerId, cmd: &[String]) -> Result<ExecEventStream> {
let rt = self.lookup(id).await?;
rt.exec_stream(id, cmd).await
}
async fn get_container_stats(&self, id: &ContainerId) -> Result<ContainerStats> {
let backends = self.read_backends(id).await?;
let mut misses = ReadMissAccumulator::default();
for (label, rt) in backends {
match rt.get_container_stats(id).await {
Ok(stats) => return Ok(stats),
Err(e) => {
tracing::warn!(
container = %id,
runtime = label,
error = %e,
"composite get_container_stats: backend could not serve stats; \
trying next backend",
);
misses.record(e);
}
}
}
Err(misses.into_error("get_container_stats"))
}
async fn wait_container(&self, id: &ContainerId) -> Result<i32> {
let rt = self.lookup(id).await?;
rt.wait_container(id).await
}
async fn wait_outcome(&self, id: &ContainerId) -> Result<WaitOutcome> {
let rt = self.lookup(id).await?;
rt.wait_outcome(id).await
}
async fn wait_outcome_with_condition(
&self,
id: &ContainerId,
condition: WaitCondition,
) -> Result<WaitOutcome> {
let rt = self.lookup(id).await?;
rt.wait_outcome_with_condition(id, condition).await
}
async fn rename_container(&self, id: &ContainerId, new_name: &str) -> Result<()> {
let rt = self.lookup(id).await?;
rt.rename_container(id, new_name).await
}
async fn get_logs(&self, id: &ContainerId) -> Result<Vec<LogEntry>> {
let backends = self.read_backends(id).await?;
let mut misses = ReadMissAccumulator::default();
for (label, rt) in backends {
match rt.get_logs(id).await {
Ok(logs) => return Ok(logs),
Err(e) => {
tracing::warn!(
container = %id,
runtime = label,
error = %e,
"composite get_logs: backend could not serve logs; trying next backend",
);
misses.record(e);
}
}
}
Err(misses.into_error("get_logs"))
}
async fn logs_stream(&self, id: &ContainerId, opts: LogsStreamOptions) -> Result<LogsStream> {
// Route to the backend that actually created the container. The default
// trait impl returns `Unsupported`, which surfaced as a swallowed 500 on
// `GET /containers/{id}/logs` whenever the owning backend did not
// implement streaming (e.g. the macOS `SandboxRuntime` primary, which
// implements `container_logs` but not `logs_stream`).
//
// Try each backend's `logs_stream` (owner first); on a soft miss
// (`Unsupported`/error that is not `NotFound`) fall back to the same
// backend's non-streaming `container_logs` and SYNTHESISE a one-shot
// stream from it. Only a genuine `NotFound` propagates (→ 404).
let backends = self.read_backends(id).await?;
let mut misses = ReadMissAccumulator::default();
for (label, rt) in &backends {
match rt.logs_stream(id, opts.clone()).await {
Ok(stream) => return Ok(stream),
Err(e) => {
tracing::warn!(
container = %id,
runtime = label,
error = %e,
"composite logs_stream: backend has no native log stream; \
falling back to a one-shot snapshot",
);
misses.record(e);
}
}
}
// No backend offered a native stream. Synthesise one from whichever
// backend can produce a captured-log snapshot (`container_logs`).
let tail = opts
.tail
.map_or(1000, |n| usize::try_from(n).unwrap_or(1000));
for (label, rt) in &backends {
match rt.container_logs(id, tail).await {
Ok(entries) => {
return Ok(one_shot_logs_stream(entries, &opts));
}
Err(e) => {
tracing::warn!(
container = %id,
runtime = label,
error = %e,
"composite logs_stream: backend snapshot fallback failed; trying next",
);
misses.record(e);
}
}
}
Err(misses.into_error("container logs"))
}
async fn stats_stream(&self, id: &ContainerId) -> Result<StatsStream> {
// Same rationale as `logs_stream`: forward to the owning backend so
// `GET /containers/{id}/stats` reaches the runtime that ran the
// container instead of hitting the `Unsupported` default (→ swallowed
// 500). On a soft miss, fall back to the non-streaming
// `get_container_stats` and synthesise a one-shot sample.
let backends = self.read_backends(id).await?;
let mut misses = ReadMissAccumulator::default();
for (label, rt) in &backends {
match rt.stats_stream(id).await {
Ok(stream) => return Ok(stream),
Err(e) => {
tracing::warn!(
container = %id,
runtime = label,
error = %e,
"composite stats_stream: backend has no native stats stream; \
falling back to a one-shot sample",
);
misses.record(e);
}
}
}
for (label, rt) in &backends {
match rt.get_container_stats(id).await {
Ok(stats) => return Ok(one_shot_stats_stream(&stats)),
Err(e) => {
tracing::warn!(
container = %id,
runtime = label,
error = %e,
"composite stats_stream: backend sample fallback failed; trying next",
);
misses.record(e);
}
}
}
Err(misses.into_error("container stats"))
}
async fn get_container_pid(&self, id: &ContainerId) -> Result<Option<u32>> {
let rt = self.lookup(id).await?;
rt.get_container_pid(id).await
}
fn overlay_attach_kind(&self) -> OverlayAttachKind {
// Linux workloads on macOS execute in the VZ-Linux delegate, which joins
// the overlay from inside the guest (`InGuestVsock`). Defer to it when
// present so the service layer takes the guest-managed attach path and
// calls `push_overlay_config` (routed per-container below); otherwise use
// the primary's kind. Non-VZ containers route to a runtime whose
// `push_overlay_config` is unsupported and degrade gracefully.
self.vz_linux.as_ref().map_or_else(
|| self.primary.overlay_attach_kind(),
|vz| vz.overlay_attach_kind(),
)
}
async fn push_overlay_config(
&self,
id: &ContainerId,
config: &zlayer_types::overlayd::GuestOverlayConfig,
) -> Result<()> {
let rt = self.lookup(id).await?;
rt.push_overlay_config(id, config).await
}
async fn get_container_ip(&self, id: &ContainerId) -> Result<Option<IpAddr>> {
let rt = self.lookup(id).await?;
rt.get_container_ip(id).await
}
async fn get_container_port_override(&self, id: &ContainerId) -> Result<Option<u16>> {
let rt = self.lookup(id).await?;
rt.get_container_port_override(id).await
}
#[cfg(target_os = "windows")]
async fn get_container_namespace_id(
&self,
id: &ContainerId,
) -> Result<Option<windows::core::GUID>> {
let rt = self.lookup(id).await?;
rt.get_container_namespace_id(id).await
}
async fn sync_container_volumes(&self, id: &ContainerId) -> Result<()> {
let rt = self.lookup(id).await?;
rt.sync_container_volumes(id).await
}
async fn list_images(&self) -> Result<Vec<ImageInfo>> {
// Fan out over every configured runtime and merge their image lists.
// Crucially, a *single* backend's failure must not fail the whole
// call: on macOS the `primary` (SandboxRuntime) does not implement
// `list_images` at all (it returns `Unsupported`), yet pulled Linux
// images live in the `vz_linux` delegate's store. Propagating the
// primary's error via `?` here used to surface as a 500 on
// `GET /images/json` (and, via the inspect fallback, broke every
// `docker pull` verification). Tolerate per-backend errors the same
// way we already tolerate the delegate's, and include the VZ +
// VZ-Linux delegates so their images are actually listable.
let mut out: Vec<ImageInfo> = Vec::new();
let mut seen: std::collections::HashSet<String> = std::collections::HashSet::new();
let mut any_ok = false;
let mut last_err: Option<AgentError> = None;
for (label, rt) in [
Some(("primary", &self.primary)),
self.delegate.as_ref().map(|d| ("delegate", d)),
self.vz.as_ref().map(|d| ("vz", d)),
self.vz_linux.as_ref().map(|d| ("vz_linux", d)),
]
.into_iter()
.flatten()
{
match rt.list_images().await {
Ok(images) => {
any_ok = true;
for img in images {
// De-dup by reference so an image registered in more
// than one backend isn't reported twice.
if seen.insert(img.reference.clone()) {
out.push(img);
}
}
}
Err(e) => {
tracing::debug!(
runtime = label,
error = %e,
"composite list_images: backend returned an error; skipping it",
);
last_err = Some(e);
}
}
}
// Only fail if *every* backend errored. With at least one success we
// return the merged (possibly empty) list — an empty image set is a
// valid response, not an error.
if any_ok {
Ok(out)
} else {
Err(last_err.unwrap_or_else(|| {
AgentError::Unsupported("no runtime implements list_images".into())
}))
}
}
async fn remove_image(&self, image: &str, force: bool) -> Result<()> {
match self.primary.remove_image(image, force).await {
Ok(()) => Ok(()),
Err(primary_err) => {
if let Some(delegate) = &self.delegate {
match delegate.remove_image(image, force).await {
Ok(()) => Ok(()),
Err(delegate_err) => {
tracing::debug!(
image,
%delegate_err,
"delegate remove_image also failed; returning primary error",
);
Err(primary_err)
}
}
} else {
Err(primary_err)
}
}
}
}
async fn prune_images(&self) -> Result<PruneResult> {
// Symmetric with `remove_image` / `tag_image`: a primary that does not
// implement pruning (e.g. a cache-less backend that returns
// `Unsupported`) must not 501 the whole call when a delegate exists and
// could still reclaim space. Only a primary `Unsupported` is tolerated;
// any other primary error still propagates.
let mut result = match self.primary.prune_images().await {
Ok(r) => r,
Err(AgentError::Unsupported(reason)) if self.delegate.is_some() => {
tracing::debug!(
%reason,
"primary runtime does not support prune_images; relying on delegate",
);
PruneResult::default()
}
Err(e) => return Err(e),
};
if let Some(delegate) = &self.delegate {
match delegate.prune_images().await {
Ok(extra) => {
result.deleted.extend(extra.deleted);
result.space_reclaimed =
result.space_reclaimed.saturating_add(extra.space_reclaimed);
}
Err(e) => tracing::warn!(
error = %e,
"delegate runtime prune_images failed; returning primary result only",
),
}
}
Ok(result)
}
async fn kill_container(&self, id: &ContainerId, signal: Option<&str>) -> Result<()> {
let rt = self.lookup(id).await?;
rt.kill_container(id, signal).await
}
async fn tag_image(&self, source: &str, target: &str) -> Result<()> {
match self.primary.tag_image(source, target).await {
Ok(()) => Ok(()),
Err(primary_err) => {
if let Some(delegate) = &self.delegate {
match delegate.tag_image(source, target).await {
Ok(()) => Ok(()),
Err(delegate_err) => {
tracing::debug!(
source,
target,
%delegate_err,
"delegate tag_image also failed; returning primary error",
);
Err(primary_err)
}
}
} else {
Err(primary_err)
}
}
}
}
async fn inspect_detailed(&self, id: &ContainerId) -> Result<ContainerInspectDetails> {
let rt = self.lookup(id).await?;
rt.inspect_detailed(id).await
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::cgroups_stats::ContainerStats;
use std::sync::Mutex as StdMutex;
use zlayer_spec::{ArchKind, DeploymentSpec, TargetPlatform};
/// Which runtime a mock represents. Only used for labelling invocation
/// records in tests.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Role {
Primary,
Delegate,
Vz,
VzLinux,
}
/// One recorded invocation: (runtime role, method name, container id).
type CallRecord = (Role, String, Option<ContainerId>);
/// Shared, thread-safe log of every mock call made in a single test.
type CallLog = Arc<StdMutex<Vec<CallRecord>>>;
/// Mock runtime that records every method call it receives.
///
/// This is intentionally minimal — just enough trait surface to exercise
/// the composite's dispatch logic. Every recorded call includes the role
/// (primary vs delegate), the method name, and the container id (or
/// `None` for cross-cutting image operations).
struct MockRuntime {
role: Role,
calls: CallLog,
list_images_response: Vec<ImageInfo>,
/// When set, `list_images` returns `AgentError::Unsupported(msg)`
/// instead of `list_images_response`. Models a backend (e.g. the macOS
/// `SandboxRuntime` primary) that does not implement image listing.
list_images_error: Option<String>,
pull_image_error: Option<String>,
/// When set, both `pull_image` and `pull_image_with_policy` return a
/// freshly-built [`AgentError::WrongPlatform`] using these fields
/// (`expected`, `actual`). Takes precedence over `pull_image_error`
/// so tests can simulate a wrong-platform soft skip end-to-end.
pull_image_wrong_platform: Option<(&'static str, &'static str)>,
/// When `true`, the *streaming* reads (`logs_stream` / `stats_stream`)
/// return `AgentError::Unsupported`, modelling a backend (e.g. the macOS
/// `SandboxRuntime` primary) that implements the snapshot reads
/// (`container_logs` / `get_container_stats`) but not the streaming
/// ones — exactly the case that used to surface as a swallowed 500.
stream_unsupported: bool,
/// When `true`, *every* per-container read (`container_logs`,
/// `get_logs`, `get_container_stats`, `logs_stream`, `stats_stream`)
/// returns `AgentError::NotFound`, modelling a backend that does not own
/// the container at all. The composite must NOT mask this as success,
/// and a genuine all-not-found must propagate as `NotFound` (404).
reads_not_found: bool,
/// Captured-log snapshot returned by `container_logs` / `get_logs`
/// (unless `reads_not_found`). Lets a delegate model real workload
/// output the composite's snapshot fallback should surface.
logs_response: Vec<LogEntry>,
/// When `true`, the snapshot `get_container_stats` returns
/// `AgentError::Unsupported` (a soft miss), modelling a backend that
/// owns the container but cannot report stats at all. Forces the
/// composite to fall back to another backend.
stats_snapshot_unsupported: bool,
/// `prune_images` response. `None` models a backend that does not
/// implement pruning (returns `AgentError::Unsupported`, like the trait
/// default); `Some(result)` models a backend that prunes and reports
/// the given [`PruneResult`].
prune_images_response: Option<PruneResult>,
}
impl MockRuntime {
fn new(role: Role, calls: CallLog) -> Self {
Self {
role,
calls,
list_images_response: Vec::new(),
list_images_error: None,
pull_image_error: None,
pull_image_wrong_platform: None,
stream_unsupported: false,
reads_not_found: false,
logs_response: Vec::new(),
stats_snapshot_unsupported: false,
prune_images_response: None,
}
}
/// Streaming reads return `Unsupported`; snapshot reads still work.
fn with_stream_unsupported(mut self) -> Self {
self.stream_unsupported = true;
self
}
/// Every per-container read returns `NotFound`.
fn with_reads_not_found(mut self) -> Self {
self.reads_not_found = true;
self
}
/// Set the captured-log snapshot returned by the snapshot reads.
fn with_logs(mut self, logs: Vec<LogEntry>) -> Self {
self.logs_response = logs;
self
}
/// Snapshot `get_container_stats` returns `Unsupported` (a soft miss).
fn with_stats_snapshot_unsupported(mut self) -> Self {
self.stats_snapshot_unsupported = true;
self
}
/// `prune_images` succeeds and reports the given [`PruneResult`].
fn with_prune_result(mut self, result: PruneResult) -> Self {
self.prune_images_response = Some(result);
self
}
fn build_wrong_platform_error(&self, image: &str) -> Option<AgentError> {
self.pull_image_wrong_platform
.map(|(expected, actual)| AgentError::WrongPlatform {
runtime: match self.role {
Role::Primary => "primary-mock".to_string(),
Role::Delegate => "delegate-mock".to_string(),
Role::Vz => "vz-mock".to_string(),
Role::VzLinux => "vz-linux-mock".to_string(),
},
expected: expected.to_string(),
actual: actual.to_string(),
image: image.to_string(),
})
}
fn record(&self, method: &str, id: Option<&ContainerId>) {
self.calls
.lock()
.expect("mock call-log mutex poisoned")
.push((self.role, method.to_string(), id.cloned()));
}
}
#[async_trait]
impl Runtime for MockRuntime {
async fn pull_image(&self, image: &str) -> Result<()> {
self.record("pull_image", None);
if let Some(err) = self.build_wrong_platform_error(image) {
return Err(err);
}
if let Some(msg) = &self.pull_image_error {
return Err(AgentError::Internal(msg.clone()));
}
Ok(())
}
async fn pull_image_with_policy(
&self,
image: &str,
_policy: PullPolicy,
_auth: Option<&RegistryAuth>,
_source: zlayer_spec::SourcePolicy,
) -> Result<()> {
self.record("pull_image_with_policy", None);
if let Some(err) = self.build_wrong_platform_error(image) {
return Err(err);
}
if let Some(msg) = &self.pull_image_error {
return Err(AgentError::Internal(msg.clone()));
}
Ok(())
}
async fn create_container(&self, id: &ContainerId, _spec: &ServiceSpec) -> Result<()> {
self.record("create_container", Some(id));
Ok(())
}
async fn start_container(&self, id: &ContainerId) -> Result<()> {
self.record("start_container", Some(id));
Ok(())
}
async fn stop_container(&self, id: &ContainerId, _timeout: Duration) -> Result<()> {
self.record("stop_container", Some(id));
Ok(())
}
async fn remove_container(&self, id: &ContainerId) -> Result<()> {
self.record("remove_container", Some(id));
Ok(())
}
async fn container_state(&self, id: &ContainerId) -> Result<ContainerState> {
self.record("container_state", Some(id));
Ok(ContainerState::Running)
}
async fn container_logs(&self, id: &ContainerId, _tail: usize) -> Result<Vec<LogEntry>> {
self.record("container_logs", Some(id));
if self.reads_not_found {
return Err(mock_not_found());
}
Ok(self.logs_response.clone())
}
async fn exec(&self, id: &ContainerId, _cmd: &[String]) -> Result<(i32, String, String)> {
self.record("exec", Some(id));
Ok((0, String::new(), String::new()))
}
async fn get_container_stats(&self, id: &ContainerId) -> Result<ContainerStats> {
self.record("get_container_stats", Some(id));
if self.reads_not_found {
return Err(mock_not_found());
}
if self.stats_snapshot_unsupported {
return Err(AgentError::Unsupported("mock has no snapshot stats".into()));
}
Ok(ContainerStats {
cpu_usage_usec: 1_000,
memory_bytes: 4096,
memory_limit: 8192,
timestamp: std::time::Instant::now(),
})
}
async fn wait_container(&self, id: &ContainerId) -> Result<i32> {
self.record("wait_container", Some(id));
Ok(0)
}
async fn get_logs(&self, id: &ContainerId) -> Result<Vec<LogEntry>> {
self.record("get_logs", Some(id));
if self.reads_not_found {
return Err(mock_not_found());
}
Ok(self.logs_response.clone())
}
async fn logs_stream(
&self,
id: &ContainerId,
_opts: LogsStreamOptions,
) -> Result<LogsStream> {
self.record("logs_stream", Some(id));
if self.reads_not_found {
return Err(mock_not_found());
}
if self.stream_unsupported {
return Err(AgentError::Unsupported("mock has no log stream".into()));
}
// A backend that owns a native stream replays its captured logs.
Ok(one_shot_logs_stream(
self.logs_response.clone(),
&LogsStreamOptions::default(),
))
}
async fn stats_stream(&self, id: &ContainerId) -> Result<StatsStream> {
use futures_util::stream;
self.record("stats_stream", Some(id));
if self.reads_not_found {
return Err(mock_not_found());
}
if self.stream_unsupported {
return Err(AgentError::Unsupported("mock has no stats stream".into()));
}
Ok(Box::pin(stream::iter(vec![Ok(StatsSample {
cpu_total_ns: 0,
cpu_system_ns: 0,
online_cpus: 1,
mem_used_bytes: 4096,
mem_limit_bytes: 8192,
net_rx_bytes: 0,
net_tx_bytes: 0,
blkio_read_bytes: 0,
blkio_write_bytes: 0,
pids_current: 0,
pids_limit: None,
timestamp: chrono::Utc::now(),
})])))
}
async fn get_container_pid(&self, id: &ContainerId) -> Result<Option<u32>> {
self.record("get_container_pid", Some(id));
Ok(None)
}
async fn get_container_ip(&self, id: &ContainerId) -> Result<Option<IpAddr>> {
self.record("get_container_ip", Some(id));
Ok(None)
}
async fn list_images(&self) -> Result<Vec<ImageInfo>> {
self.record("list_images", None);
if let Some(msg) = &self.list_images_error {
return Err(AgentError::Unsupported(msg.clone()));
}
Ok(self.list_images_response.clone())
}
async fn prune_images(&self) -> Result<PruneResult> {
self.record("prune_images", None);
match &self.prune_images_response {
Some(result) => Ok(result.clone()),
None => Err(AgentError::Unsupported(
"mock runtime does not support prune_images".into(),
)),
}
}
}
/// Build a [`ServiceSpec`] (with the given image name) from the minimal
/// inline YAML the existing runtime tests use, then optionally set a
/// target platform on it.
fn make_spec(image: &str, platform: Option<TargetPlatform>) -> ServiceSpec {
let yaml = format!(
r"
version: v1
deployment: test
services:
test:
rtype: service
image:
name: {image}
endpoints:
- name: http
protocol: http
port: 8080
"
);
let mut spec = serde_yaml::from_str::<DeploymentSpec>(&yaml)
.expect("valid deployment yaml")
.services
.remove("test")
.expect("service 'test' present");
spec.platform = platform;
spec
}
fn cid(service: &str, replica: u32) -> ContainerId {
ContainerId::new(service.to_string(), replica)
}
fn make_composite(with_delegate: bool) -> (CompositeRuntime, CallLog) {
let calls = Arc::new(StdMutex::new(Vec::new()));
let primary = Arc::new(MockRuntime::new(Role::Primary, Arc::clone(&calls)));
let delegate = if with_delegate {
Some(Arc::new(MockRuntime::new(Role::Delegate, Arc::clone(&calls))) as Arc<dyn Runtime>)
} else {
None
};
(
CompositeRuntime::new(primary as Arc<dyn Runtime>, delegate),
calls,
)
}
fn role_for(calls: &[CallRecord], method: &str) -> Option<Role> {
calls
.iter()
.find(|(_, m, _)| m == method)
.map(|(role, _, _)| *role)
}
/// The `NotFound` a `MockRuntime` returns when it does not own a container.
fn mock_not_found() -> AgentError {
AgentError::NotFound {
container: "mock".to_string(),
reason: "mock backend does not own this container".to_string(),
}
}
#[tokio::test]
async fn dispatch_windows_spec_goes_to_primary() {
let (rt, calls) = make_composite(true);
let id = cid("win-svc", 0);
let spec = make_spec(
"mcr.microsoft.com/windows/nanoserver:ltsc2022",
Some(TargetPlatform::new(OsKind::Windows, ArchKind::Amd64)),
);
rt.create_container(&id, &spec).await.unwrap();
rt.start_container(&id).await.unwrap();
let calls = calls.lock().unwrap();
assert_eq!(
role_for(&calls, "create_container"),
Some(Role::Primary),
"create_container should hit primary for Windows spec"
);
assert_eq!(
role_for(&calls, "start_container"),
Some(Role::Primary),
"start_container should hit primary for Windows spec"
);
}
#[tokio::test]
async fn dispatch_linux_spec_goes_to_delegate() {
let (rt, calls) = make_composite(true);
let id = cid("lin-svc", 0);
let spec = make_spec(
"docker.io/library/alpine:3.19",
Some(TargetPlatform::new(OsKind::Linux, ArchKind::Amd64)),
);
rt.create_container(&id, &spec).await.unwrap();
rt.start_container(&id).await.unwrap();
let calls = calls.lock().unwrap();
assert_eq!(
role_for(&calls, "create_container"),
Some(Role::Delegate),
"create_container should hit delegate for Linux spec"
);
assert_eq!(
role_for(&calls, "start_container"),
Some(Role::Delegate),
"start_container should hit delegate for Linux spec"
);
}
#[tokio::test]
async fn dispatch_linux_without_delegate_errors() {
// H-7 policy: a Linux spec on a node without a delegate must return
// `RouteToPeer` (not `Unsupported`, not a silent primary fall-through)
// so the scheduler can re-place the workload on a capable peer.
let (rt, _calls) = make_composite(false);
let id = cid("lin-svc", 0);
let spec = make_spec(
"docker.io/library/alpine:3.19",
Some(TargetPlatform::new(OsKind::Linux, ArchKind::Amd64)),
);
let err = rt.create_container(&id, &spec).await.unwrap_err();
match err {
AgentError::RouteToPeer {
service,
required_os,
reason,
} => {
assert_eq!(service, "lin-svc");
assert_eq!(required_os, "linux");
assert!(
reason.contains("--install-wsl") && reason.contains("Linux peer"),
"reason must name both remediations, got: {reason}"
);
}
other => panic!("expected RouteToPeer, got {other:?}"),
}
}
#[tokio::test]
async fn dispatch_linux_image_cache_without_delegate_routes_to_peer() {
// H-7 policy: even when `spec.platform` is unset, a Linux image in the
// OS cache must route to a peer instead of falling through to primary.
// This is the old permissive-fallthrough path the comment at lines
// 172-178 used to describe; the behavior is now strict.
let (rt, _calls) = make_composite(false);
let id = cid("svc", 0);
let image = "docker.io/library/nginx:1.25";
rt.record_image_os(image, OsKind::Linux).await;
let spec = make_spec(image, None);
let err = rt.create_container(&id, &spec).await.unwrap_err();
match err {
AgentError::RouteToPeer {
service,
required_os,
reason,
} => {
assert_eq!(service, "svc");
assert_eq!(required_os, "linux");
assert!(
reason.contains(image),
"reason should mention the image name, got: {reason}"
);
assert!(
reason.contains("--install-wsl") && reason.contains("Linux peer"),
"reason must name both remediations, got: {reason}"
);
}
other => panic!("expected RouteToPeer, got {other:?}"),
}
}
#[tokio::test]
async fn dispatch_macos_spec_goes_to_primary() {
let (rt, calls) = make_composite(true);
let id = cid("mac-svc", 0);
let spec = make_spec(
"ghcr.io/zlayer/macos:latest",
Some(TargetPlatform::new(OsKind::Macos, ArchKind::Arm64)),
);
rt.create_container(&id, &spec).await.unwrap();
let calls = calls.lock().unwrap();
assert_eq!(
role_for(&calls, "create_container"),
Some(Role::Primary),
"create_container should hit primary for Macos spec"
);
}
#[tokio::test]
async fn dispatch_no_platform_no_image_os_falls_through_to_primary() {
let (rt, calls) = make_composite(true);
let id = cid("svc", 0);
let spec = make_spec("docker.io/library/nginx:1.25", None);
rt.create_container(&id, &spec).await.unwrap();
let calls = calls.lock().unwrap();
assert_eq!(
role_for(&calls, "create_container"),
Some(Role::Primary),
"fall-through should pick primary when both platform and image-OS cache are unknown"
);
}
#[tokio::test]
async fn dispatch_uses_image_os_cache_when_platform_missing() {
let (rt, calls) = make_composite(true);
let id = cid("svc", 0);
let image = "docker.io/library/nginx:1.25";
rt.record_image_os(image, OsKind::Linux).await;
let spec = make_spec(image, None);
rt.create_container(&id, &spec).await.unwrap();
let calls = calls.lock().unwrap();
assert_eq!(
role_for(&calls, "create_container"),
Some(Role::Delegate),
"image-OS cache should route Linux images to the delegate"
);
}
/// Composite with primary + delegate + an attached VZ delegate, all sharing
/// one call log.
fn make_composite_with_vz() -> (CompositeRuntime, CallLog) {
let calls = Arc::new(StdMutex::new(Vec::new()));
let primary = Arc::new(MockRuntime::new(Role::Primary, Arc::clone(&calls)));
let delegate =
Arc::new(MockRuntime::new(Role::Delegate, Arc::clone(&calls))) as Arc<dyn Runtime>;
let vz = Arc::new(MockRuntime::new(Role::Vz, Arc::clone(&calls))) as Arc<dyn Runtime>;
let rt = CompositeRuntime::new(primary as Arc<dyn Runtime>, Some(delegate))
.with_vz_delegate(Some(vz));
(rt, calls)
}
#[tokio::test]
async fn dispatch_vz_bundle_annotation_auto_routes_to_vz() {
let (rt, calls) = make_composite_with_vz();
let id = cid("mac-svc", 0);
let image = "ghcr.io/org/macos-vz:sequoia";
// Simulate the manifest inspection having cached `com.zlayer.runtime=vz`.
rt.record_image_runtime(image, "vz".to_string()).await;
let spec = make_spec(image, None);
rt.create_container(&id, &spec).await.unwrap();
let calls = calls.lock().unwrap();
assert_eq!(
role_for(&calls, "create_container"),
Some(Role::Vz),
"a com.zlayer.runtime=vz bundle should auto-route to the VZ runtime"
);
}
#[tokio::test]
async fn dispatch_vz_label_forces_vz() {
let (rt, calls) = make_composite_with_vz();
let id = cid("mac-svc", 0);
let mut spec = make_spec("ghcr.io/org/whatever:1", None);
spec.labels
.insert("com.zlayer.isolation".to_string(), "vz".to_string());
rt.create_container(&id, &spec).await.unwrap();
let calls = calls.lock().unwrap();
assert_eq!(
role_for(&calls, "create_container"),
Some(Role::Vz),
"an explicit com.zlayer.isolation=vz label should force the VZ runtime"
);
}
#[tokio::test]
async fn dispatch_sandbox_label_overrides_vz_bundle() {
let (rt, calls) = make_composite_with_vz();
let id = cid("mac-svc", 0);
let image = "ghcr.io/org/macos-vz:sequoia";
rt.record_image_runtime(image, "vz".to_string()).await;
let mut spec = make_spec(image, None);
spec.labels
.insert("com.zlayer.isolation".to_string(), "sandbox".to_string());
rt.create_container(&id, &spec).await.unwrap();
let calls = calls.lock().unwrap();
assert_eq!(
role_for(&calls, "create_container"),
Some(Role::Primary),
"com.zlayer.isolation=sandbox should opt out of VZ auto-detect (force the sandbox)"
);
}
/// Composite with primary + delegate (libkrun) + a VZ Linux-guest delegate,
/// all sharing one call log. Mirrors `make_composite_with_vz`.
fn make_composite_with_vz_linux() -> (CompositeRuntime, CallLog) {
let calls = Arc::new(StdMutex::new(Vec::new()));
let primary = Arc::new(MockRuntime::new(Role::Primary, Arc::clone(&calls)));
let delegate =
Arc::new(MockRuntime::new(Role::Delegate, Arc::clone(&calls))) as Arc<dyn Runtime>;
let vz_linux =
Arc::new(MockRuntime::new(Role::VzLinux, Arc::clone(&calls))) as Arc<dyn Runtime>;
let rt = CompositeRuntime::new(primary as Arc<dyn Runtime>, Some(delegate))
.with_vz_linux_delegate(Some(vz_linux));
(rt, calls)
}
#[tokio::test]
async fn dispatch_vz_linux_label_forces_vz_linux() {
let (rt, calls) = make_composite_with_vz_linux();
let id = cid("lin-svc", 0);
let mut spec = make_spec("docker.io/library/alpine:3.19", None);
spec.labels
.insert("com.zlayer.isolation".to_string(), "vz-linux".to_string());
rt.create_container(&id, &spec).await.unwrap();
let calls = calls.lock().unwrap();
assert_eq!(
role_for(&calls, "create_container"),
Some(Role::VzLinux),
"com.zlayer.isolation=vz-linux must force the VZ Linux runtime"
);
}
#[tokio::test]
async fn dispatch_vz_linux_marker_auto_routes_to_vz_linux() {
let (rt, calls) = make_composite_with_vz_linux();
let id = cid("lin-svc", 0);
let image = "ghcr.io/org/linux-vz:bookworm";
rt.record_image_runtime(image, "vz-linux".to_string()).await;
let spec = make_spec(image, None);
rt.create_container(&id, &spec).await.unwrap();
let calls = calls.lock().unwrap();
assert_eq!(
role_for(&calls, "create_container"),
Some(Role::VzLinux),
"a com.zlayer.runtime=vz-linux marker should auto-route to the VZ Linux runtime"
);
}
#[tokio::test]
async fn dispatch_linux_platform_with_vz_linux_routes_to_vz_linux() {
let (rt, calls) = make_composite_with_vz_linux();
let id = cid("lin-svc", 0);
// platform.os = linux: with a VZ Linux delegate present this is the
// default Linux path, NOT the libkrun delegate.
let spec = make_spec(
"docker.io/library/alpine:3.19",
Some(TargetPlatform::new(OsKind::Linux, ArchKind::Arm64)),
);
rt.create_container(&id, &spec).await.unwrap();
let calls = calls.lock().unwrap();
assert_eq!(
role_for(&calls, "create_container"),
Some(Role::VzLinux),
"a Linux platform spec must default to the VZ Linux runtime when present"
);
}
#[tokio::test]
async fn dispatch_linux_image_os_with_vz_linux_routes_to_vz_linux() {
let (rt, calls) = make_composite_with_vz_linux();
let id = cid("lin-svc", 0);
let image = "docker.io/library/nginx:1.25";
rt.record_image_os(image, OsKind::Linux).await;
let spec = make_spec(image, None);
rt.create_container(&id, &spec).await.unwrap();
let calls = calls.lock().unwrap();
assert_eq!(
role_for(&calls, "create_container"),
Some(Role::VzLinux),
"a Linux image-OS cache hit must default to the VZ Linux runtime when present"
);
}
#[tokio::test]
async fn dispatch_macos_image_os_with_vz_linux_routes_to_primary() {
// A macOS-native rootfs must NEVER go to the Linux VM. Even with a
// VZ-Linux delegate present (the default Linux path), an image whose
// locally-known OS is macOS routes to the primary (Seatbelt sandbox).
let (rt, calls) = make_composite_with_vz_linux();
let id = cid("mac-svc", 0);
let image = "ghcr.io/zlayer/macos-native:latest";
rt.record_image_os(image, OsKind::Macos).await;
let spec = make_spec(image, None);
rt.create_container(&id, &spec).await.unwrap();
let calls = calls.lock().unwrap();
assert_eq!(
role_for(&calls, "create_container"),
Some(Role::Primary),
"image_os == Macos must route to primary even when VZ-Linux is the default",
);
}
#[tokio::test]
async fn dispatch_unknown_os_with_vz_linux_defaults_to_vz_linux() {
// OS genuinely unknown (no isolation label, no runtime marker, no
// platform, no image-OS cache hit) on a macOS host with a VZ-Linux
// delegate: default to VZ-Linux. Sending an unknown (overwhelmingly
// Linux) image to the Seatbelt sandbox is the exit-127 failure this fix
// exists to prevent.
let (rt, calls) = make_composite_with_vz_linux();
let id = cid("svc", 0);
let spec = make_spec("docker.io/library/whatever:latest", None);
rt.create_container(&id, &spec).await.unwrap();
let calls = calls.lock().unwrap();
assert_eq!(
role_for(&calls, "create_container"),
Some(Role::VzLinux),
"an unknown-OS image must default to VZ-Linux when the delegate is present",
);
}
#[tokio::test]
async fn dispatch_unknown_os_without_vz_linux_falls_through_to_primary() {
// The unknown-OS default to VZ-Linux is keyed on the delegate's
// presence (a proxy for "macOS host"). Without a VZ-Linux delegate the
// historical primary fallthrough is preserved for non-macOS hosts.
let (rt, calls) = make_composite(true);
let id = cid("svc", 0);
let spec = make_spec("docker.io/library/whatever:latest", None);
rt.create_container(&id, &spec).await.unwrap();
let calls = calls.lock().unwrap();
assert_eq!(
role_for(&calls, "create_container"),
Some(Role::Primary),
"without a VZ-Linux delegate an unknown-OS image keeps the primary fallthrough",
);
}
/// Seed a persistent blob cache at `path` with a manifest + config blob for
/// `image` whose config declares `os = linux`, mirroring what a real
/// VZ-Linux pull writes to `{data_dir}/vz/linux/images/blobs.redb`.
async fn seed_persistent_linux_cache(path: &std::path::Path, image: &str) {
seed_persistent_cache_with_os(path, image, "linux").await;
}
/// Like [`seed_persistent_linux_cache`] but lets the test pick the config
/// `os` value (e.g. `"darwin"` for a macOS-native bundle).
async fn seed_persistent_cache_with_os(path: &std::path::Path, image: &str, os: &str) {
let cache = zlayer_registry::CacheType::persistent_at(path)
.build()
.await
.expect("open persistent blob cache");
let config_json = serde_json::json!({
"architecture": "arm64",
"os": os,
"config": {},
});
let config_bytes = serde_json::to_vec(&config_json).unwrap();
let config_digest = zlayer_registry::compute_digest(&config_bytes);
cache.put(&config_digest, &config_bytes).await.unwrap();
let manifest = zlayer_registry::OciImageManifest {
schema_version: 2,
media_type: Some("application/vnd.oci.image.manifest.v1+json".to_string()),
artifact_type: None,
config: oci_client::manifest::OciDescriptor {
media_type: "application/vnd.oci.image.config.v1+json".to_string(),
digest: config_digest.clone(),
size: i64::try_from(config_bytes.len()).unwrap(),
urls: None,
annotations: None,
},
layers: vec![],
annotations: None,
subject: None,
};
let manifest_bytes = serde_json::to_vec(&manifest).unwrap();
let manifest_digest = zlayer_registry::compute_digest(&manifest_bytes);
cache
.put(&zlayer_registry::manifest_cache_key(image), &manifest_bytes)
.await
.unwrap();
cache
.put(
&zlayer_registry::manifest_digest_cache_key(image),
manifest_digest.as_bytes(),
)
.await
.unwrap();
}
/// End-to-end of the macOS rate-limit routing fix: a Linux image whose OS
/// lives ONLY in the local persistent blob cache (no network) must be
/// inspected at `pull_image` time and then routed to the VZ-Linux runtime
/// by `select_for` — exactly the path that breaks under a Docker Hub 429
/// when inspection goes to the wire.
#[tokio::test]
async fn pull_then_dispatch_resolves_linux_os_from_local_cache_routes_to_vz_linux() {
let tmp = tempfile::tempdir().unwrap();
let cache_path = tmp.path().join("blobs.redb");
let image = "docker.io/library/alpine:latest";
seed_persistent_linux_cache(&cache_path, image).await;
let (rt, calls) = make_composite_with_vz_linux();
let rt = rt.with_os_inspect_cache_path(Some(cache_path));
// pull_image drives the real local-first OS inspection; no network.
rt.pull_image(image).await.unwrap();
// The OS must now be cached as Linux purely from the local store.
assert_eq!(
rt.image_os.read().await.get(image).copied(),
Some(OsKind::Linux),
"pull_image must resolve Linux OS from the local persistent cache",
);
// And select_for must route the (platform-less) spec to VZ-Linux.
let id = cid("lin-svc", 0);
let spec = make_spec(image, None);
rt.create_container(&id, &spec).await.unwrap();
let calls = calls.lock().unwrap();
assert_eq!(
role_for(&calls, "create_container"),
Some(Role::VzLinux),
"a Linux image whose OS came from the local cache must route to VZ-Linux",
);
}
/// LIVE BUG #1, end-to-end: the cache is seeded under the QUALIFIED ref
/// (`docker.io/library/alpine:latest`, as the pull writes it) but the spec —
/// and therefore every `pull_image` / `inspect_image_os` / `select_for`
/// lookup — uses the BARE `alpine:latest`. With the canonical manifest-key
/// normalization, the bare-ref inspect hits the qualified-seeded cache with
/// NO network call, so the Linux image still routes to VZ-Linux.
#[tokio::test]
async fn bare_ref_spec_resolves_os_from_qualified_seeded_cache_routes_to_vz_linux() {
let tmp = tempfile::tempdir().unwrap();
let cache_path = tmp.path().join("blobs.redb");
// Seed under the QUALIFIED ref, exactly as a real pull persists it.
seed_persistent_linux_cache(&cache_path, "docker.io/library/alpine:latest").await;
let (rt, calls) = make_composite_with_vz_linux();
let rt = rt.with_os_inspect_cache_paths(vec![cache_path]);
// Everything below uses the BARE ref, exactly as the live daemon does
// (`ImageRef::Display` yields the user-original string).
let bare = "alpine:latest";
rt.pull_image(bare).await.unwrap();
assert_eq!(
rt.image_os.read().await.get(bare).copied(),
Some(OsKind::Linux),
"bare-ref inspect must resolve Linux from the qualified-seeded cache",
);
let id = cid("lin-svc", 0);
let spec = make_spec(bare, None);
rt.create_container(&id, &spec).await.unwrap();
let calls = calls.lock().unwrap();
assert_eq!(
role_for(&calls, "create_container"),
Some(Role::VzLinux),
"bare-ref Linux image routes to VZ-Linux via the canonical-key cache hit",
);
}
/// LIVE BUG #2 / multi-cache fallback: the manifest+config live ONLY in the
/// SECOND configured cache (the primary Sandbox store), because the
/// VZ-Linux pull short-circuited under `IfNotPresent`. Inspection must probe
/// the empty first cache (no network), then resolve from the second — still
/// with NO network — and route to VZ-Linux.
#[tokio::test]
async fn os_resolves_from_second_cache_when_first_is_empty() {
let tmp = tempfile::tempdir().unwrap();
let empty_cache = tmp.path().join("vz-linux-blobs.redb");
let primary_cache = tmp.path().join("primary-blobs.redb");
// Create the first cache empty (so opening it succeeds but it misses).
zlayer_registry::CacheType::persistent_at(&empty_cache)
.build()
.await
.unwrap();
// Only the SECOND cache has the image.
seed_persistent_linux_cache(&primary_cache, "docker.io/library/alpine:latest").await;
let (rt, calls) = make_composite_with_vz_linux();
let rt = rt.with_os_inspect_cache_paths(vec![empty_cache, primary_cache]);
let bare = "alpine:latest";
rt.pull_image(bare).await.unwrap();
assert_eq!(
rt.image_os.read().await.get(bare).copied(),
Some(OsKind::Linux),
"OS must resolve from the second cache after the first misses (no network)",
);
let id = cid("lin-svc", 0);
let spec = make_spec(bare, None);
rt.create_container(&id, &spec).await.unwrap();
let calls = calls.lock().unwrap();
assert_eq!(role_for(&calls, "create_container"), Some(Role::VzLinux),);
}
/// The exact LIVE bug, simulated end-to-end: a `pull_image` whose network OS
/// re-inspection WOULD 429 still leaves dispatch fully working, because the
/// image's OS is resolved purely from the local persistent blob cache the
/// runtime already populated during extract — with NO network call at all.
///
/// We model the 429 by pointing `os_inspect_cache_paths` at a real seeded
/// cache (so the local resolver succeeds) while using a synthetic
/// `*.invalid` registry host: if the dispatch-population path ever reached
/// the network it would fail to resolve, leaving the cache empty and routing
/// the Linux image to the Seatbelt primary (exit 127). It must not — the
/// local cache hit is authoritative and the image routes to VZ-Linux.
#[tokio::test]
async fn pull_with_network_429_still_dispatches_via_local_cache() {
let tmp = tempfile::tempdir().unwrap();
let cache_path = tmp.path().join("blobs.redb");
// The image ref uses a host that cannot be resolved on the wire; only
// the LOCAL cache knows its OS.
let image = "registry.invalid.example/library/alpine:latest";
seed_persistent_linux_cache(&cache_path, image).await;
let (rt, calls) = make_composite_with_vz_linux();
let rt = rt.with_os_inspect_cache_path(Some(cache_path));
// `pull_image` drives the dispatch-population inspection. Even though a
// real registry inspection of `*.invalid` would fail (our stand-in for a
// 429), the local-only path resolves Linux and the call succeeds.
rt.pull_image(image).await.unwrap();
assert_eq!(
rt.image_os.read().await.get(image).copied(),
Some(OsKind::Linux),
"OS must be resolved from the local cache with no network call",
);
// And dispatch routes the Linux image to VZ-Linux, not the primary.
let id = cid("lin-svc", 0);
let spec = make_spec(image, None);
rt.create_container(&id, &spec).await.unwrap();
let calls = calls.lock().unwrap();
assert_eq!(
role_for(&calls, "create_container"),
Some(Role::VzLinux),
"a would-be-429 pull must still route the cached Linux image to VZ-Linux",
);
}
/// Companion to the macOS-native dispatch guard, but driving the resolution
/// through the real local-cache inspection at `pull_image` time: a bundle
/// whose config declares `os = darwin` in the local cache must route to the
/// primary, never the Linux VM.
#[tokio::test]
async fn pull_then_dispatch_resolves_macos_os_from_local_cache_routes_to_primary() {
let tmp = tempfile::tempdir().unwrap();
let cache_path = tmp.path().join("blobs.redb");
let image = "ghcr.io/zlayer/macos-native:latest";
seed_persistent_cache_with_os(&cache_path, image, "darwin").await;
let (rt, calls) = make_composite_with_vz_linux();
let rt = rt.with_os_inspect_cache_path(Some(cache_path));
rt.pull_image(image).await.unwrap();
assert_eq!(
rt.image_os.read().await.get(image).copied(),
Some(OsKind::Macos),
"pull_image must resolve macOS OS from the local persistent cache",
);
let id = cid("mac-svc", 0);
let spec = make_spec(image, None);
rt.create_container(&id, &spec).await.unwrap();
let calls = calls.lock().unwrap();
assert_eq!(
role_for(&calls, "create_container"),
Some(Role::Primary),
"a macOS-native rootfs must route to primary even with VZ-Linux as default",
);
}
#[tokio::test]
async fn dispatch_vm_label_forces_libkrun_delegate() {
let (rt, calls) = make_composite_with_vz_linux();
let id = cid("lin-svc", 0);
// Even with a VZ Linux delegate as the default, an explicit
// `com.zlayer.isolation=vm` label forces the libkrun delegate.
let mut spec = make_spec(
"docker.io/library/alpine:3.19",
Some(TargetPlatform::new(OsKind::Linux, ArchKind::Arm64)),
);
spec.labels
.insert("com.zlayer.isolation".to_string(), "vm".to_string());
rt.create_container(&id, &spec).await.unwrap();
let calls = calls.lock().unwrap();
assert_eq!(
role_for(&calls, "create_container"),
Some(Role::Delegate),
"com.zlayer.isolation=vm must force the libkrun delegate even when VZ Linux is default"
);
}
#[tokio::test]
async fn dispatch_unmarked_image_with_vz_delegate_falls_through_to_primary() {
let (rt, calls) = make_composite_with_vz();
let id = cid("mac-svc", 0);
// No runtime marker, no platform, no image-OS cache: VZ must NOT capture
// ordinary images just because the delegate exists.
let spec = make_spec("ghcr.io/org/plain:1", None);
rt.create_container(&id, &spec).await.unwrap();
let calls = calls.lock().unwrap();
assert_eq!(
role_for(&calls, "create_container"),
Some(Role::Primary),
"an unmarked image must fall through to primary even when a VZ delegate is attached"
);
}
#[tokio::test]
async fn per_container_dispatch_cache_persists_through_start_stop() {
let (rt, calls) = make_composite(true);
let id = cid("win-svc", 0);
let spec = make_spec(
"mcr.microsoft.com/windows/nanoserver:ltsc2022",
Some(TargetPlatform::new(OsKind::Windows, ArchKind::Amd64)),
);
rt.create_container(&id, &spec).await.unwrap();
rt.start_container(&id).await.unwrap();
rt.stop_container(&id, Duration::from_secs(1))
.await
.unwrap();
rt.remove_container(&id).await.unwrap();
let recorded = calls.lock().unwrap().clone();
for method in [
"create_container",
"start_container",
"stop_container",
"remove_container",
] {
assert_eq!(
role_for(&recorded, method),
Some(Role::Primary),
"{method} should have dispatched to primary"
);
}
// After remove, the dispatch cache entry should be gone.
let after = rt
.start_container(&id)
.await
.expect_err("lookup after remove should fail");
assert!(
matches!(after, AgentError::NotFound { .. }),
"expected NotFound after remove, got {after:?}"
);
}
#[tokio::test]
async fn pull_image_calls_both_runtimes() {
let (rt, calls) = make_composite(true);
rt.pull_image("docker.io/library/alpine:3.19")
.await
.unwrap();
let recorded = calls.lock().unwrap();
let pull_calls: Vec<Role> = recorded
.iter()
.filter(|(_, m, _)| m == "pull_image")
.map(|(r, _, _)| *r)
.collect();
assert!(
pull_calls.contains(&Role::Primary),
"primary should have been pulled: {pull_calls:?}",
);
assert!(
pull_calls.contains(&Role::Delegate),
"delegate should have been pulled: {pull_calls:?}",
);
}
#[tokio::test]
async fn pull_image_delegate_error_does_not_fail() {
// Build the composite by hand so we can flip the delegate's
// pull_image_error before wrapping it in an Arc<dyn Runtime>.
let calls = Arc::new(StdMutex::new(Vec::new()));
let primary = Arc::new(MockRuntime::new(Role::Primary, Arc::clone(&calls)));
let mut delegate = MockRuntime::new(Role::Delegate, Arc::clone(&calls));
delegate.pull_image_error = Some("simulated delegate pull failure".to_string());
let rt = CompositeRuntime::new(
primary as Arc<dyn Runtime>,
Some(Arc::new(delegate) as Arc<dyn Runtime>),
);
// Top-level call must succeed despite the delegate error.
rt.pull_image("docker.io/library/alpine:3.19")
.await
.unwrap();
let recorded = calls.lock().unwrap();
let pull_calls: Vec<Role> = recorded
.iter()
.filter(|(_, m, _)| m == "pull_image")
.map(|(r, _, _)| *r)
.collect();
assert!(
pull_calls.contains(&Role::Primary) && pull_calls.contains(&Role::Delegate),
"both runtimes should have been called: {pull_calls:?}",
);
}
#[tokio::test]
async fn pull_image_primary_wrong_platform_does_not_fail() {
// The HCS runtime returns `AgentError::WrongPlatform` when the image's
// OCI config reports a non-Windows OS (calling `ProcessBaseImage` on a
// Linux base layer is guaranteed to fail with 0x80070003). The
// composite must treat that as a soft skip and let the delegate's
// pull own the image — the overall pull must NOT fail.
let calls = Arc::new(StdMutex::new(Vec::new()));
let mut primary = MockRuntime::new(Role::Primary, Arc::clone(&calls));
primary.pull_image_wrong_platform = Some(("windows", "linux"));
let delegate = MockRuntime::new(Role::Delegate, Arc::clone(&calls));
let rt = CompositeRuntime::new(
Arc::new(primary) as Arc<dyn Runtime>,
Some(Arc::new(delegate) as Arc<dyn Runtime>),
);
// Top-level call must succeed despite the primary's wrong-platform err.
rt.pull_image("docker.io/library/alpine:3.19")
.await
.expect("composite pull must tolerate WrongPlatform from primary");
let recorded = calls.lock().unwrap();
let pull_calls: Vec<Role> = recorded
.iter()
.filter(|(_, m, _)| m == "pull_image")
.map(|(r, _, _)| *r)
.collect();
assert!(
pull_calls.contains(&Role::Primary) && pull_calls.contains(&Role::Delegate),
"delegate must still be called when primary soft-skips: {pull_calls:?}",
);
}
#[tokio::test]
async fn pull_image_with_policy_primary_wrong_platform_does_not_fail() {
// Same contract as `pull_image_primary_wrong_platform_does_not_fail`
// but exercising the `pull_image_with_policy` entry point. The
// policy/auth path is what the daemon's create-container hot loop
// actually invokes, so it has to honour the same soft-skip rule.
let calls = Arc::new(StdMutex::new(Vec::new()));
let mut primary = MockRuntime::new(Role::Primary, Arc::clone(&calls));
primary.pull_image_wrong_platform = Some(("windows", "linux"));
let delegate = MockRuntime::new(Role::Delegate, Arc::clone(&calls));
let rt = CompositeRuntime::new(
Arc::new(primary) as Arc<dyn Runtime>,
Some(Arc::new(delegate) as Arc<dyn Runtime>),
);
rt.pull_image_with_policy(
"docker.io/library/alpine:3.19",
PullPolicy::IfNotPresent,
None,
zlayer_spec::SourcePolicy::default(),
)
.await
.expect("composite pull_image_with_policy must tolerate WrongPlatform from primary");
let recorded = calls.lock().unwrap();
let pull_calls: Vec<Role> = recorded
.iter()
.filter(|(_, m, _)| m == "pull_image_with_policy")
.map(|(r, _, _)| *r)
.collect();
assert!(
pull_calls.contains(&Role::Primary) && pull_calls.contains(&Role::Delegate),
"delegate must still be called when primary soft-skips: {pull_calls:?}",
);
}
#[tokio::test]
async fn pull_image_primary_non_wrong_platform_error_still_fails() {
// Sanity check: only `WrongPlatform` is soft-skipped. Any other error
// from the primary must still bubble up so real pull failures aren't
// silently swallowed.
let calls = Arc::new(StdMutex::new(Vec::new()));
let mut primary = MockRuntime::new(Role::Primary, Arc::clone(&calls));
primary.pull_image_error = Some("simulated real failure".to_string());
let delegate = MockRuntime::new(Role::Delegate, Arc::clone(&calls));
let rt = CompositeRuntime::new(
Arc::new(primary) as Arc<dyn Runtime>,
Some(Arc::new(delegate) as Arc<dyn Runtime>),
);
let err = rt
.pull_image("docker.io/library/alpine:3.19")
.await
.expect_err("real primary error must propagate");
assert!(
matches!(err, AgentError::Internal(_)),
"expected Internal, got {err:?}",
);
}
#[tokio::test]
async fn list_images_merges_both() {
// Hand-build so we can seed each mock's list_images_response.
let calls = Arc::new(StdMutex::new(Vec::new()));
let mut primary = MockRuntime::new(Role::Primary, Arc::clone(&calls));
primary.list_images_response = vec![ImageInfo {
reference: "primary/image:1".to_string(),
digest: None,
size_bytes: None,
}];
let mut delegate = MockRuntime::new(Role::Delegate, Arc::clone(&calls));
delegate.list_images_response = vec![ImageInfo {
reference: "delegate/image:1".to_string(),
digest: None,
size_bytes: None,
}];
let rt = CompositeRuntime::new(
Arc::new(primary) as Arc<dyn Runtime>,
Some(Arc::new(delegate) as Arc<dyn Runtime>),
);
let merged = rt.list_images().await.unwrap();
let refs: Vec<&str> = merged.iter().map(|i| i.reference.as_str()).collect();
assert!(
refs.contains(&"primary/image:1") && refs.contains(&"delegate/image:1"),
"merged list should contain both entries, got {refs:?}",
);
}
/// Regression (macOS `GET /images/json` 500): when the *primary* runtime
/// does not implement `list_images` (the `SandboxRuntime` returns
/// `Unsupported`), the composite must NOT propagate that error. It must
/// fall back to the other backends — in particular the VZ-Linux delegate
/// that actually owns pulled Linux images — and return their list. Before
/// the fix the composite used `self.primary.list_images().await?`, which
/// surfaced as a 500 and (via the inspect fallback) broke `docker pull`.
#[tokio::test]
async fn list_images_tolerates_primary_unsupported_and_uses_vz_linux() {
let calls = Arc::new(StdMutex::new(Vec::new()));
let mut primary = MockRuntime::new(Role::Primary, Arc::clone(&calls));
primary.list_images_error = Some("list_images is not supported".to_string());
let mut vz_linux = MockRuntime::new(Role::VzLinux, Arc::clone(&calls));
vz_linux.list_images_response = vec![ImageInfo {
reference: "docker.io/library/alpine:latest".to_string(),
digest: None,
size_bytes: None,
}];
let rt = CompositeRuntime::new(Arc::new(primary) as Arc<dyn Runtime>, None)
.with_vz_linux_delegate(Some(Arc::new(vz_linux) as Arc<dyn Runtime>));
let images = rt
.list_images()
.await
.expect("primary Unsupported must not fail the composite list_images");
let refs: Vec<&str> = images.iter().map(|i| i.reference.as_str()).collect();
assert_eq!(
refs,
vec!["docker.io/library/alpine:latest"],
"should return the VZ-Linux delegate's images, got {refs:?}",
);
}
/// When EVERY backend fails `list_images`, the composite surfaces an error
/// (rather than silently returning an empty list, which would mask a total
/// backend outage).
#[tokio::test]
async fn list_images_errors_only_when_all_backends_fail() {
let calls = Arc::new(StdMutex::new(Vec::new()));
let mut primary = MockRuntime::new(Role::Primary, Arc::clone(&calls));
primary.list_images_error = Some("unsupported".to_string());
let mut vz_linux = MockRuntime::new(Role::VzLinux, Arc::clone(&calls));
vz_linux.list_images_error = Some("also unsupported".to_string());
let rt = CompositeRuntime::new(Arc::new(primary) as Arc<dyn Runtime>, None)
.with_vz_linux_delegate(Some(Arc::new(vz_linux) as Arc<dyn Runtime>));
let err = rt.list_images().await.unwrap_err();
assert!(
matches!(err, AgentError::Unsupported(_)),
"all-backends-fail should surface Unsupported, got {err:?}",
);
}
/// When the PRIMARY does not implement `prune_images` (returns
/// `AgentError::Unsupported`) but a delegate does, the composite must
/// tolerate the primary miss and return the delegate's result — symmetric
/// with how `remove_image` / `tag_image` tolerate a primary failure when a
/// delegate exists. A future cache-less primary must not 501 the whole call.
#[tokio::test]
async fn prune_images_tolerates_primary_unsupported_and_uses_delegate() {
let calls = Arc::new(StdMutex::new(Vec::new()));
// Primary leaves `prune_images_response` as `None` → returns Unsupported.
let primary = MockRuntime::new(Role::Primary, Arc::clone(&calls));
let delegate =
MockRuntime::new(Role::Delegate, Arc::clone(&calls)).with_prune_result(PruneResult {
deleted: vec![
"docker.io/library/alpine:3.19".to_string(),
"docker.io/library/nginx:1.25".to_string(),
],
space_reclaimed: 4096,
});
let rt = CompositeRuntime::new(
Arc::new(primary) as Arc<dyn Runtime>,
Some(Arc::new(delegate) as Arc<dyn Runtime>),
);
let result = rt
.prune_images()
.await
.expect("primary Unsupported must not fail the composite prune_images");
assert_eq!(
result.deleted,
vec![
"docker.io/library/alpine:3.19".to_string(),
"docker.io/library/nginx:1.25".to_string(),
],
"should return the delegate's deleted images, got {:?}",
result.deleted,
);
assert_eq!(
result.space_reclaimed, 4096,
"should return the delegate's reclaimed bytes",
);
let calls = calls.lock().unwrap();
assert_eq!(
role_for(&calls, "prune_images"),
Some(Role::Primary),
"primary prune_images must still be attempted first",
);
assert!(
calls
.iter()
.any(|(role, m, _)| *role == Role::Delegate && m == "prune_images"),
"delegate prune_images must be invoked after the primary miss",
);
}
// ----------------------------------------------------------------------
// Per-container read routing (logs / stats).
//
// These guard the macOS Docker-compat `/logs` and `/stats` 500 fix: when
// the owning backend cannot serve a particular read (the primary
// `SandboxRuntime` implements snapshot reads but returns `Unsupported` for
// the *streaming* ones, or a different backend owns the container), the
// composite must route to / fall back across backends and return real data
// instead of propagating `Unsupported` as a swallowed 500. Only a genuine
// all-not-found is a 404.
// ----------------------------------------------------------------------
/// Build a `LogEntry` with the given stream + message for read tests.
fn log_entry(stream: LogStream, message: &str) -> LogEntry {
LogEntry {
timestamp: chrono::Utc::now(),
stream,
source: zlayer_observability::logs::LogSource::Container("test".to_string()),
message: message.to_string(),
service: None,
deployment: None,
}
}
/// Drain a `LogsStream` into the concatenated UTF-8 body bytes.
async fn drain_logs(stream: LogsStream) -> String {
use futures_util::StreamExt as _;
let mut out = Vec::new();
let mut s = stream;
while let Some(item) = s.next().await {
out.extend_from_slice(&item.expect("log chunk ok").bytes);
}
String::from_utf8(out).expect("utf8 log body")
}
/// Collect a `StatsStream` into a Vec of samples.
async fn drain_stats(stream: StatsStream) -> Vec<StatsSample> {
use futures_util::StreamExt as _;
let mut out = Vec::new();
let mut s = stream;
while let Some(item) = s.next().await {
out.push(item.expect("stats sample ok"));
}
out
}
/// Build a composite whose primary models the macOS `SandboxRuntime`
/// (snapshot reads work, streaming reads return `Unsupported`) and whose
/// VZ-Linux delegate owns the container with working native streams.
/// Returns (composite, call-log) with a container already dispatched to the
/// chosen owner.
async fn make_read_composite(owner: Role) -> (CompositeRuntime, ContainerId, CallLog) {
let calls = Arc::new(StdMutex::new(Vec::new()));
let logs = vec![
log_entry(LogStream::Stdout, "hello stdout"),
log_entry(LogStream::Stderr, "hello stderr"),
];
let primary = MockRuntime::new(Role::Primary, Arc::clone(&calls))
.with_stream_unsupported()
.with_logs(logs.clone());
let vz_linux = MockRuntime::new(Role::VzLinux, Arc::clone(&calls)).with_logs(logs);
let rt = CompositeRuntime::new(Arc::new(primary) as Arc<dyn Runtime>, None)
.with_vz_linux_delegate(Some(Arc::new(vz_linux) as Arc<dyn Runtime>));
let id = cid("read-svc", 0);
// Dispatch the container to the chosen owner without going through the
// (platform-dependent) `select_for` path.
let target = match owner {
Role::Primary => DispatchTarget::Primary,
Role::VzLinux => DispatchTarget::VzLinux,
other => panic!("make_read_composite supports Primary/VzLinux, not {other:?}"),
};
rt.dispatch.write().await.insert(id.clone(), target);
(rt, id, calls)
}
#[tokio::test]
async fn logs_stream_falls_back_to_snapshot_when_owner_has_no_stream() {
// Sole backend = primary (SandboxRuntime model): `logs_stream` is
// Unsupported, but `container_logs` works. With no other backend the
// composite must synthesise a stream from the snapshot rather than 500.
let calls = Arc::new(StdMutex::new(Vec::new()));
let logs = vec![
log_entry(LogStream::Stdout, "hello stdout"),
log_entry(LogStream::Stderr, "hello stderr"),
];
let primary = MockRuntime::new(Role::Primary, Arc::clone(&calls))
.with_stream_unsupported()
.with_logs(logs);
let rt = CompositeRuntime::new(Arc::new(primary) as Arc<dyn Runtime>, None);
let id = cid("read-svc", 0);
rt.dispatch
.write()
.await
.insert(id.clone(), DispatchTarget::Primary);
let stream = rt
.logs_stream(&id, LogsStreamOptions::default())
.await
.expect("logs_stream must not 500 when snapshot reads work");
let body = drain_logs(stream).await;
assert!(
body.contains("hello stdout") && body.contains("hello stderr"),
"synthesised stream must carry the captured logs, got: {body:?}",
);
}
#[tokio::test]
async fn logs_stream_routes_to_delegate_owner_native_stream() {
// Owner = VZ-Linux delegate with a working native stream; the primary's
// streaming read is Unsupported but must not be consulted first.
let (rt, id, calls) = make_read_composite(Role::VzLinux).await;
let stream = rt
.logs_stream(&id, LogsStreamOptions::default())
.await
.expect("delegate-owned logs_stream must succeed");
let body = drain_logs(stream).await;
assert!(body.contains("hello stdout"), "got: {body:?}");
let log = calls.lock().expect("call-log mutex poisoned");
assert_eq!(
role_for(&log, "logs_stream"),
Some(Role::VzLinux),
"logs_stream must hit the owning delegate first, calls: {log:?}",
);
}
#[tokio::test]
async fn get_logs_falls_back_across_backends() {
// Owner = primary; here snapshot `get_logs` works on primary directly,
// so it should succeed on the owner without ever consulting the
// delegate. (Soft-miss fallback is exercised by the stats test below.)
let (rt, id, _calls) = make_read_composite(Role::Primary).await;
let logs = rt.get_logs(&id).await.expect("get_logs must succeed");
assert_eq!(logs.len(), 2, "owner snapshot logs should be returned");
}
#[tokio::test]
async fn stats_stream_falls_back_to_snapshot_when_owner_has_no_stream() {
// Sole backend = primary (SandboxRuntime model): `stats_stream` is
// Unsupported but `get_container_stats` works. With no other backend
// offering a native stream, the composite must synthesise a single
// non-empty sample from the snapshot rather than 500.
let calls = Arc::new(StdMutex::new(Vec::new()));
let primary = MockRuntime::new(Role::Primary, Arc::clone(&calls)).with_stream_unsupported();
let rt = CompositeRuntime::new(Arc::new(primary) as Arc<dyn Runtime>, None);
let id = cid("read-svc", 0);
rt.dispatch
.write()
.await
.insert(id.clone(), DispatchTarget::Primary);
let stream = rt
.stats_stream(&id)
.await
.expect("stats_stream must not 500 when get_container_stats works");
let samples = drain_stats(stream).await;
assert_eq!(samples.len(), 1, "snapshot fallback yields one sample");
assert!(
samples[0].mem_used_bytes > 0,
"synthesised sample must carry non-zero memory, got {:?}",
samples[0],
);
assert_eq!(
samples[0].cpu_total_ns, 1_000_000,
"cpu microseconds must be scaled to nanoseconds in the synthesised sample",
);
}
#[tokio::test]
async fn get_container_stats_tolerates_owner_miss_and_uses_other_backend() {
// Owner = primary whose snapshot `get_container_stats` returns
// `Unsupported` (a soft miss); the delegate that follows in the fallback
// chain serves it. The composite must NOT propagate the owner's
// Unsupported as a 500.
let calls = Arc::new(StdMutex::new(Vec::new()));
let primary =
MockRuntime::new(Role::Primary, Arc::clone(&calls)).with_stats_snapshot_unsupported();
let vz_linux = MockRuntime::new(Role::VzLinux, Arc::clone(&calls));
let rt = CompositeRuntime::new(Arc::new(primary) as Arc<dyn Runtime>, None)
.with_vz_linux_delegate(Some(Arc::new(vz_linux) as Arc<dyn Runtime>));
let id = cid("read-svc", 0);
rt.dispatch
.write()
.await
.insert(id.clone(), DispatchTarget::Primary);
let stats = rt
.get_container_stats(&id)
.await
.expect("owner Unsupported must fall back to the delegate, not 500");
assert!(stats.memory_bytes > 0, "delegate stats should be returned");
let log = calls.lock().expect("call-log mutex poisoned");
assert!(
log.iter()
.any(|(role, method, _)| *role == Role::Primary && method == "get_container_stats"),
"primary must have been tried first, calls: {log:?}",
);
assert!(
log.iter()
.any(|(role, method, _)| *role == Role::VzLinux && method == "get_container_stats"),
"delegate must have served the fallback, calls: {log:?}",
);
}
#[tokio::test]
async fn reads_propagate_not_found_when_no_backend_owns_container() {
// Every backend returns NotFound for the dispatched container: the
// composite must surface NotFound (→ 404), NOT mask it as Unsupported
// or empty success.
let calls = Arc::new(StdMutex::new(Vec::new()));
let primary = MockRuntime::new(Role::Primary, Arc::clone(&calls)).with_reads_not_found();
let vz_linux = MockRuntime::new(Role::VzLinux, Arc::clone(&calls)).with_reads_not_found();
let rt = CompositeRuntime::new(Arc::new(primary) as Arc<dyn Runtime>, None)
.with_vz_linux_delegate(Some(Arc::new(vz_linux) as Arc<dyn Runtime>));
let id = cid("read-svc", 0);
rt.dispatch
.write()
.await
.insert(id.clone(), DispatchTarget::Primary);
// `LogsStream`/`StatsStream` are not `Debug`, so match instead of
// `unwrap_err()`.
match rt.logs_stream(&id, LogsStreamOptions::default()).await {
Err(AgentError::NotFound { .. }) => {}
other => panic!(
"all-not-found logs_stream must be NotFound (404), got {:?}",
other.err(),
),
}
match rt.stats_stream(&id).await {
Err(AgentError::NotFound { .. }) => {}
other => panic!(
"all-not-found stats_stream must be NotFound (404), got {:?}",
other.err(),
),
}
let cl_err = rt.container_logs(&id, 10).await.unwrap_err();
assert!(
matches!(cl_err, AgentError::NotFound { .. }),
"all-not-found container_logs must be NotFound (404), got {cl_err:?}",
);
}
#[tokio::test]
async fn reads_on_undispatched_container_are_not_found() {
// No dispatch record at all → NotFound (the id was never created here).
let (rt, _calls) = make_composite(false);
let id = cid("ghost", 0);
match rt.logs_stream(&id, LogsStreamOptions::default()).await {
Err(AgentError::NotFound { .. }) => {}
other => panic!(
"undispatched logs_stream must be NotFound, got {:?}",
other.err()
),
}
}
/// Regression: `pull_image` must fan out to the VZ-Linux delegate so the
/// image lands in the store where Linux containers actually execute on
/// macOS (and so it becomes listable/inspectable). Before the fix the
/// composite only pulled into `primary` + `delegate`, leaving the
/// VZ-Linux `image_rootfs` empty.
#[tokio::test]
async fn pull_image_fans_out_to_vz_linux() {
let calls = Arc::new(StdMutex::new(Vec::new()));
let primary = MockRuntime::new(Role::Primary, Arc::clone(&calls));
let vz_linux = MockRuntime::new(Role::VzLinux, Arc::clone(&calls));
let rt = CompositeRuntime::new(Arc::new(primary) as Arc<dyn Runtime>, None)
.with_vz_linux_delegate(Some(Arc::new(vz_linux) as Arc<dyn Runtime>));
rt.pull_image("docker.io/library/alpine:latest")
.await
.expect("pull should succeed");
let log = calls.lock().expect("call-log mutex poisoned");
assert!(
log.iter()
.any(|(role, method, _)| *role == Role::VzLinux && method == "pull_image"),
"pull_image must reach the VZ-Linux delegate, recorded calls: {log:?}",
);
}
#[tokio::test]
async fn dispatch_lookup_unknown_container_errors() {
let (rt, _calls) = make_composite(true);
let id = cid("ghost", 0);
let err = rt.start_container(&id).await.unwrap_err();
assert!(
matches!(err, AgentError::NotFound { .. }),
"expected NotFound for unknown container, got {err:?}"
);
}
/// Helper: read the internal image-OS cache for test assertions.
async fn cached_os(rt: &CompositeRuntime, image: &str) -> Option<OsKind> {
rt.image_os.read().await.get(image).copied()
}
#[tokio::test]
async fn apply_image_os_inspection_populates_cache_on_ok_some() {
// Contract: when `fetch_image_os` resolves to a recognized OS, the
// cache is populated so subsequent `select_for` calls for specs
// without `platform` dispatch correctly.
let (rt, _calls) = make_composite(true);
let image = "docker.io/library/alpine:3.19";
rt.apply_image_os_inspection(image, Ok(Some(OsKind::Linux)))
.await;
assert_eq!(cached_os(&rt, image).await, Some(OsKind::Linux));
}
#[tokio::test]
async fn apply_image_os_inspection_leaves_cache_untouched_on_ok_none() {
// Contract: when the manifest carries no (or an unrecognized) `os`
// field the cache is left alone. Dispatch will fall through to the
// primary on `create_container`.
let (rt, _calls) = make_composite(true);
let image = "docker.io/library/nginx:1.25";
rt.apply_image_os_inspection(image, Ok(None)).await;
assert_eq!(cached_os(&rt, image).await, None);
}
#[tokio::test]
async fn apply_image_os_inspection_leaves_cache_untouched_on_err() {
// Contract: a registry error during inspection is non-fatal and must
// not poison the cache. Dispatch falls through to primary on lookup.
let (rt, _calls) = make_composite(true);
let image = "docker.io/library/nginx:1.25";
// Pre-seed the cache so we can assert the error path doesn't
// overwrite or clear an existing entry.
rt.record_image_os(image, OsKind::Linux).await;
let err = zlayer_registry::RegistryError::NotFound {
registry: "docker.io".to_string(),
image: image.to_string(),
};
rt.apply_image_os_inspection(image, Err(err)).await;
// Cache is still whatever it was before the failed inspection.
assert_eq!(cached_os(&rt, image).await, Some(OsKind::Linux));
}
#[tokio::test]
async fn pull_image_inspection_failure_does_not_fail_pull() {
// End-to-end: even when the registry fetch fails (inevitable for the
// synthetic image refs used in unit tests), `pull_image` still
// returns `Ok`. The mock primary/delegate both succeed; the
// inspection step logs and moves on. The cache must remain empty
// because there was no successful inspection to record.
let (rt, _calls) = make_composite(true);
let image = "invalid.example.invalid/ghost:v1";
rt.pull_image(image).await.unwrap();
assert_eq!(
cached_os(&rt, image).await,
None,
"failed inspection must not populate the image-OS cache"
);
}
#[tokio::test]
async fn pull_image_with_policy_inspection_failure_does_not_fail_pull() {
// Same contract as `pull_image_inspection_failure_does_not_fail_pull`
// but exercising the policy-aware entry point.
let (rt, _calls) = make_composite(true);
let image = "invalid.example.invalid/ghost:v1";
rt.pull_image_with_policy(
image,
PullPolicy::IfNotPresent,
None,
zlayer_spec::SourcePolicy::default(),
)
.await
.unwrap();
assert_eq!(cached_os(&rt, image).await, None);
}
#[test]
fn os_kind_from_oci_str_roundtrip() {
// Guards the `as_oci_str` ↔ `from_oci_str` relationship used by the
// inspection path. If a new variant is added to `OsKind` without
// updating `from_oci_str` we want the miss here, not a silent
// "dispatch to primary" regression in production.
for os in [OsKind::Linux, OsKind::Windows, OsKind::Macos] {
assert_eq!(OsKind::from_oci_str(os.as_oci_str()), Some(os));
}
assert_eq!(OsKind::from_oci_str(""), None);
assert_eq!(OsKind::from_oci_str("freebsd"), None);
}
}