deepstrike-core 0.2.51

Cross-language agent runtime kernel — pure computation, zero I/O
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
//! The canonical operation driver — the plan function of [`KernelTransaction`] (spec §5.4, §6, §7.4,
//! §10.1, §10.2).
//!
//! This is the layer the migration's semantic half turns on. Everything above it is contract
//! (envelope → record → transaction); everything below it is the kernel's existing semantic
//! machinery (the scheduler's [`LoopStateMachine`], the P1 syscall gate, the P2 task table, the P3
//! context VM). The driver is the **only** place the two meet, and §5.4 fixes exactly how: every
//! wire input reduces to a P1/P2/P3 primitive, and none of them is allowed to grow a parallel
//! business state machine here.
//!
//! Three properties define it:
//!
//! 1. **No protocol adapter.** The driver reads [`NormalizedPayload`] directly and reduces it to
//!    scheduler/context primitives such as `LoopStateMachine::start`, `load_workflow`,
//!    `resolve_workflow_spawn`, and `feed`. The canonical envelope is the only wire contract.
//! 2. **`RootKind` is immutable and `ExecutionFocus` moves only on a committed transition**
//!    (§6.1.5/6.1.6, §7.4). Both live in [`CanonicalOperationDriver`], and `plan` never writes
//!    them — it *stages* the next value, and [`CanonicalOperationDriver::note_committed`] is what
//!    installs it. There is no input, host command or otherwise, that sets a focus directly.
//! 3. **A root start is one atomic input.** `ConfigureOperation` builds the engine;
//!    `StartOperation` seeds the initial context, enters the root, and publishes the first effect
//!    — a provider call for an agent root, a task spawn for a workflow root — in the *same*
//!    planned step. The historical 12+ separate accepted inputs before a first provider call
//!    (§3.3 item 20) have no equivalent path here.
//! 4. **One resolution entry, one decision per failure** (§7.9). Every pending effect — provider,
//!    tools, approval, spawn, preempt, memory, page-out, milestone — is answered through
//!    `ResolveEffect` and nothing else, and a `Failed` outcome buys exactly one policy decision:
//!    abandon, switch recovery ladder, or commit a terminal. The kernel never re-emits the same
//!    intent (DEC-5), so the historical unbounded `retry_approval` / `retry_workflow_spawn` /
//!    `retry_preempt` round trips are not expressible on this path. `ContextOverflow` is not a
//!    failure at all: it is the one *semantic* provider outcome, and it feeds the compaction ladder.
//!
//! ### What `plan` may mutate
//!
//! [`KernelTransaction::prepare`] guarantees that a non-`Prepared` outcome leaves the *transaction*
//! byte-for-byte unchanged, and it can reject after the planner has already run (an unsupported
//! effect kind, a duplicate effect identity, the tail hard limit). The driver answers that in two
//! layers:
//!
//! * every refusal the driver itself owns — root authority, focus depth, an unreducible input — is
//!   decided **before** the semantic engine is touched, so it is a genuine zero-mutation rejection;
//! * the engine advance that a successful plan performs is guarded by a staging slot. A second
//!   `plan` without an intervening `note_committed` means the previous plan was discarded while the
//!   engine had already moved, and the driver fails closed with a poison fault that names the only
//!   legal recovery — rebuild from the journal (§8.3).

use std::collections::{BTreeMap, BTreeSet};

use serde::Serialize;

use super::checkpoint::{
    AuthoredMemoryQueryState, AuthoredMemoryWriteState, ChildProcessState, ContextVmStateV1,
    EntropyState, EntropyTurnState, HandleState, InlineMessageBody, KnowledgeSlotState,
    LogicalCompressionEntry, LogicalKernelState, LogicalPlanStep, LogicalStateProjection,
    LogicalTaskState, LogicalToolCall, MessagePartition, MilestoneState, PartitionTokenState,
    PendingPayloadLoadState, PendingProviderCallState, QueuedSignalState, ReferencedMessageBody,
    SchedulerStateV1, SkillLeaseState, StoredMessageBody, StoredMessageState,
    StructuredMessageBody, SyscallStateV1, TaskAttemptState, TaskControlState, WorkflowGraphState,
    WorkflowNodeState,
};
use super::command::{
    ApplyCapabilityPatchCommand, ApplyKnowledgeMutationCommand, ApplyPolicyPatchCommand,
    ApplySkillActivationCommand, CancelCommand, CancellationReason, HostCommand, LivePolicyState,
    SeedKnowledgeCommand, TaskUpdate as WireTaskUpdate, UpdateDeadlineCommand, UpdateTaskCommand,
};
use super::config::ResolvedOperationConfig;
use super::effect::{
    ApprovalRequest as WireApprovalRequest, ArchivePageOutEffect, CallProviderEffect,
    CanonicalMemoryQuery, CanonicalMemoryWrite, EffectKind, EffectKindTag, EffectOutcome,
    EffectSuccess, EvaluateMilestoneEffect, ExecuteToolsEffect, HostEffectFailure, KernelEffect,
    LaunchToken, LoadPayloadEffect, PageOutPayload, PayloadRef, PersistMemoryEffect,
    PreemptTasksEffect, ProviderCompleted, ProviderMessage, ProviderOutcome, QueryMemoryEffect,
    RenderedContext as WireRenderedContext, RequestApprovalEffect, SpawnTasksEffect,
    TaskAttemptRef, TaskLaunch, ToolCall as WireToolCall, ToolResultDisposition,
    ToolResultPayload as WireToolResultPayload, ToolSchema as WireToolSchema,
    WorkflowBudget as WireWorkflowBudget,
};
use super::envelope::{OperationLifecycle, ResolveEffect};
use super::event::{
    ChildCompleted, ChildStatus, DeliverSignal, ExternalEvent, LogicalSignal, SignalSourceKind,
    SignalTarget, SignalUrgency,
};
use super::fault::{KernelFault, KernelFaultCode};
use super::record::NormalizedPayload;
use super::root::{
    AgentIsolation as WireIsolation, AgentRole as WireRole, ExecutionFocus, InitialContext,
    LogicalAgentSpec, LogicalContextInheritance as WireContextInheritance, LogicalTask,
    MessageRole, RootEntry, RootKind, WorkflowNode as WireNode, WorkflowSpec as WireSpec,
};
use super::scalar::{
    AttemptId, EffectId, MemoryBindingId, NodeId, OperationId, TaskId, WireU64, WorkflowId,
};
use super::syscall::{
    ChildAttemptCausation, MemoryKind as WireMemoryKind, ProviderToolCausation, SyscallCausation,
    SyscallRequest,
};
use super::terminal::{
    AgentTerminal, CancelledTerminal, EffectsDisposition, FailedTerminal, KernelFailure,
    KernelFailureCode, KernelTerminal, LoopResult as WireLoopResult, StepDisposition,
    TerminalDisposition, TerminationReason as WireTermination, UsageReport, WorkflowOutcome,
    WorkflowStatus, WorkflowTerminal,
};
use super::transaction::{PlanContext, TransitionStep};

use crate::context::manager::READ_RESULT_TOOL_NAME;
use crate::context::task_state::{CompressionEntry, PlanStep, TaskState};
use crate::mm::handle::{Handle, HandleKind, Residency};
use crate::orchestration::task_graph::TaskStatus;
use crate::orchestration::workflow::run::{WorkflowNodeStatus, WorkflowRuntimeNodeState};
use crate::orchestration::workflow::{
    WorkflowNode as CoreWorkflowNode, WorkflowSpec as CoreWorkflowSpec,
};
use crate::runtime::kernel::{KernelObservation, WorkflowSpawnFailure};
use crate::scheduler::policy::SchedulerBudget;
use crate::scheduler::state_machine::{
    AdjudicatedTurn, AnsweredCall, IdleContinuation, LoopAction, LoopEvent, LoopStateMachine,
};
use crate::scheduler::tcb::{BudgetLedger, ProcInfo, TaskLifecycle, Tcb, WaitReason};
use crate::signals::queue::QueuedSignalRuntimeState;
use crate::signals::router::SignalRouterRuntimeState;
use crate::syscall::{Disposition, Syscall as CoreSyscall};
use crate::types::agent::{
    AgentCapabilityFilter, AgentIdentity, AgentIsolation, AgentRole, AgentRunSpec,
    ContextInheritance, LoopRoundSpec,
};
use crate::types::message::{Content, ContentPart, Message, Role, ToolErrorKind, ToolResult};
use crate::types::result::{
    LoopResult, PaceAction as CorePaceAction, SubAgentResult, TerminationReason,
};
use crate::types::signal::{RuntimeSignal, SignalSource, SignalType, Urgency};
use crate::types::task::{RuntimeTask, TaskLane};

// ---------------------------------------------------------------------------------------------
// the planned step
// ---------------------------------------------------------------------------------------------

/// One planned transition, as the canonical driver produces it.
///
/// The record freezes only this value's **digest** (§22.12), so its shape is what a rebuild has to
/// reproduce bit-for-bit. Three fields, each load-bearing:
///
/// * `root_kind` — the operation's immutable root class *after* this step. Present from the root
///   start onward and never different from the value the start committed;
/// * `focus` — the execution focus after this step. Because it is inside the digest, a focus that
///   moved differently on a replay is a `RecordCorrupted` rebuild failure rather than a silent
///   divergence;
/// * `observations` — facts produced by this exact transition, published only after commit;
/// * `disposition` — effects **or** a terminal, never both (§7.12).
#[derive(Debug, Clone, Serialize)]
pub struct PlannedStep {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub root_kind: Option<RootKind>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub focus: Option<ExecutionFocus>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub observations: Vec<KernelObservation>,
    pub disposition: StepDisposition,
}

impl PartialEq for PlannedStep {
    fn eq(&self, other: &Self) -> bool {
        self.root_kind == other.root_kind
            && self.focus == other.focus
            && self.disposition == other.disposition
            && serde_json::to_vec(&self.observations).ok()
                == serde_json::to_vec(&other.observations).ok()
    }
}

impl PlannedStep {
    fn quiet(root_kind: Option<RootKind>, focus: Option<ExecutionFocus>) -> Self {
        Self {
            root_kind,
            focus,
            observations: Vec::new(),
            disposition: StepDisposition::Effects(EffectsDisposition::default()),
        }
    }
}

impl TransitionStep for PlannedStep {
    fn disposition(&self) -> &StepDisposition {
        &self.disposition
    }
}

// ---------------------------------------------------------------------------------------------
// the driver
// ---------------------------------------------------------------------------------------------

/// The internal id of the root agent task. The kernel's task table has used it since M1d; the
/// canonical `TaskId` is the same string so an `AgentTurn` focus names a row that exists.
pub const ROOT_TASK_ID: &str = "root";

/// The session slot the canonical path fills on the legacy `AgentIdentity` that `AgentRunSpec`
/// still declares (§22.6 · Task 11): nothing. Host session identity is not a kernel fact, the
/// canonical wire has no field for one, and the field itself is deleted with the legacy input enum
/// in Task 23 — until then the canonical path must be the proof that it is never populated.
const NO_HOST_SESSION: &str = "";

#[derive(Debug, Clone, PartialEq)]
struct StagedFocus {
    step_seq: WireU64,
    root_kind: Option<RootKind>,
    focus: Option<ExecutionFocus>,
}

/// What the kernel published on one provider call, kept so a `ProviderTool` causation can be
/// *derived* rather than believed (§7.6).
///
/// Two facts, both kernel-owned:
///
/// * `task_id` — the task whose turn issued the call. This is the caller a syscall inherits; no
///   host names it;
/// * `exposed_tools` — exactly the surface that turn advertised. A tool call naming anything else
///   has no causation to derive from, so it is refused rather than adjudicated.
#[derive(Debug, Clone, PartialEq)]
struct PendingProviderCall {
    task_id: TaskId,
    exposed_tools: BTreeSet<String>,
}

/// A syscall the driver refused *after* its caller was established — a malformed request, a gate
/// denial, a quarantined caller, a memory binding the operation does not hold.
///
/// Deliberately not a [`KernelFault`]. §7.7's GAP-4 and the §7.6 fixture both require that a
/// refused request leaves an **audit fact** and no derived action, while the transition it arrived
/// on still commits: a child's execution is not undone because the parent denied one of the
/// requests it attached, and a model's bad argument is not a host protocol violation.
#[derive(Debug, Clone, PartialEq)]
struct SyscallRejection {
    operation: &'static str,
    /// The derived caller. Present on every rejection raised after causation succeeded, which is
    /// all of them — an audit fact that cannot name who asked is not an audit fact.
    subject: Option<String>,
    reason: String,
}

impl SyscallRejection {
    fn new(operation: &'static str, reason: impl Into<String>) -> Self {
        Self {
            operation,
            subject: None,
            reason: reason.into(),
        }
    }

    fn by(mut self, caller: &TaskId) -> Self {
        self.subject = Some(caller.as_str().to_string());
        self
    }
}

/// The two ways a syscall can fail, kept apart on purpose (§7.13 vs §7.7 GAP-4).
#[derive(Debug, Clone)]
enum SyscallRefusal {
    /// **Who** could not be established, or the transition itself is inadmissible. Zero mutation,
    /// and the whole input is refused.
    Fault(KernelFault),
    /// **What** was asked is refused. The caller was established, so the answer is an audit fact
    /// the model reads on its next turn; the transition still commits.
    Rejected(SyscallRejection),
}

/// Stable wire label of a handle kind, for the §12.1 context-VM projection.
///
/// Written out rather than derived from the internal enum's serde rename: the checkpoint's
/// vocabulary is a contract, and a rename inside `mm::handle` must break this match arm instead of
/// silently changing what a stored checkpoint means.
fn handle_kind_label(kind: &HandleKind) -> &'static str {
    match kind {
        HandleKind::ToolResult => "tool_result",
        HandleKind::MemoryPage => "memory_page",
        HandleKind::KnowledgeEntry => "knowledge_entry",
        HandleKind::SubAgentJoin => "sub_agent_join",
    }
}

fn role_label(role: Role) -> &'static str {
    match role {
        Role::System => "system",
        Role::User => "user",
        Role::Assistant => "assistant",
        Role::Tool => "tool",
    }
}

fn role_from_label(label: &str) -> Option<Role> {
    match label {
        "system" => Some(Role::System),
        "user" => Some(Role::User),
        "assistant" => Some(Role::Assistant),
        "tool" => Some(Role::Tool),
        _ => None,
    }
}

/// Reduce a stored message to `(text, tool_call_id, is_error)`, or report that it does not reduce.
///
/// `None` means "this body is multimodal": it carries an image or audio part, and flattening it to
/// the text parts beside it would drop content a restore could never get back. Those travel as
/// [`StoredMessageBody::Structured`] instead.
#[allow(clippy::type_complexity)]
fn message_body_parts(message: &Message) -> Option<(String, Option<String>, bool)> {
    match &message.content {
        Content::Text(text) => Some((text.clone(), None, false)),
        Content::Parts(parts) => {
            let mut text = String::new();
            let mut tool_call_id = None;
            let mut is_error = false;
            for part in parts {
                match part {
                    ContentPart::Text { text: chunk } => text.push_str(chunk),
                    ContentPart::ToolResult {
                        call_id,
                        output,
                        is_error: failed,
                    } => {
                        if tool_call_id.is_some() {
                            // Two results in one message have two call ids; the pair projection
                            // holds one. Carry the whole content instead of picking a winner.
                            return None;
                        }
                        tool_call_id = Some(call_id.to_string());
                        text.push_str(output);
                        is_error = *failed;
                    }
                    ContentPart::Image { .. } | ContentPart::Audio { .. } => return None,
                }
            }
            Some((text, tool_call_id, is_error))
        }
    }
}

/// Rebuild the stored content a [`StoredMessageBody`] describes.
///
/// The exact inverse of [`message_body_parts`]: a body with a `tool_call_id` was a single-part tool
/// result and goes back as one, everything else was flat text.
fn message_content(text: String, tool_call_id: Option<&str>, is_error: bool) -> Content {
    match tool_call_id {
        Some(call_id) => Content::Parts(vec![ContentPart::ToolResult {
            call_id: call_id.into(),
            output: text,
            is_error,
        }]),
        None => Content::Text(text),
    }
}

fn workflow_kind_label(state: &WorkflowRuntimeNodeState) -> &'static str {
    match state.node.kind {
        crate::orchestration::workflow::NodeKind::Spawn => "spawn",
        crate::orchestration::workflow::NodeKind::Loop { .. } => "loop",
        crate::orchestration::workflow::NodeKind::Classify { .. } => "classify",
        crate::orchestration::workflow::NodeKind::Tournament { .. } => "tournament",
        crate::orchestration::workflow::NodeKind::Reduce { .. } => "reduce",
    }
}

fn workflow_status_label(status: TaskStatus) -> &'static str {
    match status {
        TaskStatus::Pending => "pending",
        TaskStatus::Ready => "ready",
        TaskStatus::Running => "running",
        TaskStatus::Completed => "completed",
        TaskStatus::CompletedPartial => "completed_partial",
        TaskStatus::Failed => "failed",
        TaskStatus::SkippedUpstreamFailed => "skipped_upstream_failed",
    }
}

fn restore_workflow_status(label: &str) -> Result<TaskStatus, KernelFault> {
    match label {
        "pending" => Ok(TaskStatus::Pending),
        "ready" => Ok(TaskStatus::Ready),
        "running" => Ok(TaskStatus::Running),
        "completed" => Ok(TaskStatus::Completed),
        "completed_partial" => Ok(TaskStatus::CompletedPartial),
        "failed" => Ok(TaskStatus::Failed),
        "skipped_upstream_failed" => Ok(TaskStatus::SkippedUpstreamFailed),
        other => Err(KernelFault::new(
            KernelFaultCode::CheckpointIncompatible,
            format!("workflow checkpoint carries unknown node status {other:?}"),
        )),
    }
}

fn agent_role_label(role: AgentRole) -> &'static str {
    match role {
        AgentRole::Explore => "explore",
        AgentRole::Plan => "plan",
        AgentRole::Implement => "implement",
        AgentRole::Verify => "verify",
        AgentRole::Custom => "custom",
    }
}

fn restore_agent_role(label: &str) -> Result<AgentRole, KernelFault> {
    match label {
        "explore" => Ok(AgentRole::Explore),
        "plan" => Ok(AgentRole::Plan),
        "implement" => Ok(AgentRole::Implement),
        "verify" => Ok(AgentRole::Verify),
        "custom" => Ok(AgentRole::Custom),
        other => Err(KernelFault::new(
            KernelFaultCode::CheckpointIncompatible,
            format!("child process carries unknown role {other:?}"),
        )),
    }
}

fn agent_isolation_label(isolation: AgentIsolation) -> &'static str {
    match isolation {
        AgentIsolation::Shared => "shared",
        AgentIsolation::ReadOnly => "read_only",
        AgentIsolation::Worktree => "worktree",
        AgentIsolation::Remote => "remote",
    }
}

fn restore_agent_isolation(label: &str) -> Result<AgentIsolation, KernelFault> {
    match label {
        "shared" => Ok(AgentIsolation::Shared),
        "read_only" => Ok(AgentIsolation::ReadOnly),
        "worktree" => Ok(AgentIsolation::Worktree),
        "remote" => Ok(AgentIsolation::Remote),
        other => Err(KernelFault::new(
            KernelFaultCode::CheckpointIncompatible,
            format!("child process carries unknown isolation {other:?}"),
        )),
    }
}

fn context_inheritance_label(inheritance: ContextInheritance) -> &'static str {
    match inheritance {
        ContextInheritance::None => "none",
        ContextInheritance::SystemOnly => "system_only",
        ContextInheritance::Full => "full",
    }
}

fn restore_context_inheritance(label: &str) -> Result<ContextInheritance, KernelFault> {
    match label {
        "none" => Ok(ContextInheritance::None),
        "system_only" => Ok(ContextInheritance::SystemOnly),
        "full" => Ok(ContextInheritance::Full),
        other => Err(KernelFault::new(
            KernelFaultCode::CheckpointIncompatible,
            format!("child process carries unknown context inheritance {other:?}"),
        )),
    }
}

fn queued_signal_state(queued: &QueuedSignalRuntimeState) -> QueuedSignalState {
    let signal = &queued.signal;
    QueuedSignalState {
        signal_id: super::scalar::SignalId::new(signal.id.as_str())
            .expect("a canonical runtime signal keeps its branded id"),
        source: signal_source_label(signal.source).to_string(),
        signal_type: signal_type_label(signal.signal_type).to_string(),
        urgency: urgency_label(signal.urgency).to_string(),
        summary: signal.summary.to_string(),
        payload: super::scalar::BoundedJson::new(signal.payload.clone())
            .expect("a canonical signal payload remains bounded"),
        dedupe_key: signal.dedupe_key.as_ref().map(ToString::to_string),
        deadline_ms: signal.deadline_ms.map(WireU64::new),
        coalesce_key: signal.coalesce_key.as_ref().map(ToString::to_string),
        coalesced_count: signal.coalesced_count,
        recipient: signal.recipient.as_ref().map(ToString::to_string),
        timestamp_ms: WireU64::new(signal.timestamp_ms),
        deadline_escalated: queued.deadline_escalated,
        dedupe_keys: queued.dedupe_keys.iter().map(ToString::to_string).collect(),
    }
}

fn restore_queued_signal(
    queued: &QueuedSignalState,
) -> Result<QueuedSignalRuntimeState, KernelFault> {
    if queued.coalesced_count == 0 {
        return Err(KernelFault::new(
            KernelFaultCode::CheckpointIncompatible,
            format!(
                "queued signal {} carries a zero coalesced count",
                queued.signal_id
            ),
        ));
    }
    Ok(QueuedSignalRuntimeState {
        signal: RuntimeSignal {
            id: queued.signal_id.as_str().into(),
            source: restore_signal_source(&queued.source)?,
            signal_type: restore_signal_type(&queued.signal_type)?,
            urgency: restore_urgency(&queued.urgency)?,
            summary: queued.summary.as_str().into(),
            payload: queued.payload.get().clone(),
            dedupe_key: queued.dedupe_key.as_deref().map(Into::into),
            deadline_ms: queued.deadline_ms.map(WireU64::get),
            coalesce_key: queued.coalesce_key.as_deref().map(Into::into),
            coalesced_count: queued.coalesced_count,
            recipient: queued.recipient.as_deref().map(Into::into),
            timestamp_ms: queued.timestamp_ms.get(),
        },
        deadline_escalated: queued.deadline_escalated,
        dedupe_keys: queued
            .dedupe_keys
            .iter()
            .map(|key| key.as_str().into())
            .collect(),
    })
}

fn signal_source_label(source: SignalSource) -> &'static str {
    match source {
        SignalSource::Cron => "cron",
        SignalSource::Gateway => "gateway",
        SignalSource::Heartbeat => "heartbeat",
        SignalSource::Custom => "custom",
    }
}

fn restore_signal_source(label: &str) -> Result<SignalSource, KernelFault> {
    match label {
        "cron" => Ok(SignalSource::Cron),
        "gateway" => Ok(SignalSource::Gateway),
        "heartbeat" => Ok(SignalSource::Heartbeat),
        "custom" => Ok(SignalSource::Custom),
        other => Err(KernelFault::new(
            KernelFaultCode::CheckpointIncompatible,
            format!("queued signal carries unknown source {other:?}"),
        )),
    }
}

fn signal_type_label(signal_type: SignalType) -> &'static str {
    match signal_type {
        SignalType::Event => "event",
        SignalType::Job => "job",
        SignalType::Alert => "alert",
    }
}

fn restore_signal_type(label: &str) -> Result<SignalType, KernelFault> {
    match label {
        "event" => Ok(SignalType::Event),
        "job" => Ok(SignalType::Job),
        "alert" => Ok(SignalType::Alert),
        other => Err(KernelFault::new(
            KernelFaultCode::CheckpointIncompatible,
            format!("queued signal carries unknown type {other:?}"),
        )),
    }
}

fn urgency_label(urgency: Urgency) -> &'static str {
    match urgency {
        Urgency::Low => "low",
        Urgency::Normal => "normal",
        Urgency::High => "high",
        Urgency::Critical => "critical",
    }
}

fn restore_urgency(label: &str) -> Result<Urgency, KernelFault> {
    match label {
        "low" => Ok(Urgency::Low),
        "normal" => Ok(Urgency::Normal),
        "high" => Ok(Urgency::High),
        "critical" => Ok(Urgency::Critical),
        other => Err(KernelFault::new(
            KernelFaultCode::CheckpointIncompatible,
            format!("queued signal carries unknown urgency {other:?}"),
        )),
    }
}

/// §12.2 · put the scheduler partition back on the engine.
fn restore_scheduler(
    engine: &mut LoopStateMachine,
    config: &ResolvedOperationConfig,
    state: &SchedulerStateV1,
) -> Result<(), KernelFault> {
    engine.turn = state.turn;
    engine.restore_budget_usage(state.total_tokens.get(), state.rounds_completed);
    engine.restore_started_at_ms(state.started_at_ms.map(WireU64::get));
    engine.set_wall_budget(state.wall_budget_ms.map(WireU64::get));
    engine
        .restore_entropy_checkpoint_state(crate::scheduler::entropy::EntropyTrackerRuntimeState {
            window: state
                .entropy
                .window
                .iter()
                .map(|entry| crate::scheduler::entropy::EntropyTurnRuntimeState {
                    errored_results: entry.errored_results,
                    total_results: entry.total_results,
                    rollbacks: entry.rollbacks,
                })
                .collect(),
            rollbacks_pending: state.entropy.rollbacks_pending,
            disarmed: state.entropy.disarmed,
            last_alert_turn: state.entropy.last_alert_turn,
        })
        .map_err(|error| {
            KernelFault::new(
                KernelFaultCode::CheckpointIncompatible,
                format!("entropy checkpoint could not be rebuilt: {error}"),
            )
        })?;

    let limits = SchedulerBudget {
        max_tokens: config.execution_policy.max_context_tokens,
        max_turns: config.execution_policy.max_turns,
        max_total_tokens: config.execution_policy.max_total_tokens.get(),
        max_wall_ms: state.wall_budget_ms.map(WireU64::get),
    };
    let table = engine.task_table_mut();
    for task in &state.tasks {
        let mut tcb = Tcb::root(task.task_id.as_str(), limits.clone());
        tcb.parent = task
            .parent_task_id
            .as_ref()
            .map(|parent| parent.as_str().into());
        tcb.state = restore_task_lifecycle(task)?;
        tcb.wait = restore_task_wait(task)?;
        tcb.caps = task.capability_ids.iter().map(|cap| cap.into()).collect();
        tcb.proc = task
            .process
            .as_ref()
            .map(|process| {
                let result = process
                    .join_result
                    .as_ref()
                    .map(|value| {
                        serde_json::from_value(value.get().clone()).map_err(|error| {
                            KernelFault::new(
                                KernelFaultCode::CheckpointIncompatible,
                                format!(
                                    "task {} carries an invalid child join result: {error}",
                                    task.task_id
                                ),
                            )
                        })
                    })
                    .transpose()?;
                if result.as_ref().is_some_and(|result: &SubAgentResult| {
                    result.agent_id.as_str() != task.task_id.as_str()
                }) {
                    return Err(KernelFault::new(
                        KernelFaultCode::CheckpointIncompatible,
                        format!(
                            "task {} carries a join result for another child",
                            task.task_id
                        ),
                    ));
                }
                Ok(ProcInfo {
                    role: restore_agent_role(&process.role)?,
                    isolation: restore_agent_isolation(&process.isolation)?,
                    context_inheritance: restore_context_inheritance(&process.context_inheritance)?,
                    result,
                })
            })
            .transpose()?;
        tcb.budget = BudgetLedger {
            limits: limits.clone(),
            turns: task.turns_used,
            total_tokens: task.tokens_used.get(),
            started_at_ms: state.started_at_ms.map(WireU64::get),
        };
        table.insert(tcb);
    }

    let queued = state
        .queued_signals
        .iter()
        .map(restore_queued_signal)
        .collect::<Result<Vec<_>, _>>()?;
    engine
        .restore_signal_checkpoint_state(SignalRouterRuntimeState {
            queued,
            seen_order: state
                .signal_dedupe_keys
                .iter()
                .map(|key| key.as_str().into())
                .collect(),
        })
        .map_err(|error| {
            KernelFault::new(
                KernelFaultCode::CheckpointIncompatible,
                format!("signal checkpoint could not be rebuilt: {error}"),
            )
        })?;

    if let Some(workflow) = &state.workflow {
        let wire_spec = WireSpec {
            name: String::new(),
            nodes: workflow
                .nodes
                .iter()
                .map(|node| WireNode {
                    node_id: node.node_id.clone(),
                    task: node.task.clone(),
                    depends_on: node.depends_on.clone(),
                    run_spec: node.run_spec.clone(),
                })
                .collect(),
        };
        let core_spec = build_core_spec(&wire_spec).map_err(|fault| {
            KernelFault::new(KernelFaultCode::CheckpointIncompatible, fault.message)
        })?;
        let runtime_states: Result<Vec<_>, KernelFault> = workflow
            .nodes
            .iter()
            .enumerate()
            .zip(core_spec.nodes.iter())
            .map(|((index, node), core)| {
                if node.kind != "spawn" {
                    return Err(KernelFault::new(
                        KernelFaultCode::CheckpointIncompatible,
                        format!(
                            "workflow node {} carries unsupported checkpoint kind {:?}",
                            node.node_id, node.kind
                        ),
                    ));
                }
                let result = engine
                    .task_table()
                    .get(&crate::orchestration::workflow::node_agent_id(index))
                    .and_then(|task| task.proc.as_ref())
                    .and_then(|process| process.result.as_ref())
                    .map(|result| result.result.clone());
                Ok(WorkflowRuntimeNodeState {
                    node: core.clone(),
                    status: restore_workflow_status(&node.status)?,
                    result,
                    active_agent_id: node.active_agent_id.clone(),
                    iterations_completed: node.iterations_completed as usize,
                })
            })
            .collect();
        let run = crate::orchestration::workflow::WorkflowRun::restore_from_checkpoint(
            &core_spec,
            &runtime_states?,
        )
        .map_err(|error| {
            KernelFault::new(
                KernelFaultCode::CheckpointIncompatible,
                format!("workflow checkpoint could not be rebuilt: {error}"),
            )
        })?;
        engine.restore_checkpoint_workflow(run);
    }
    Ok(())
}

fn restore_task_lifecycle(task: &TaskControlState) -> Result<TaskLifecycle, KernelFault> {
    let lifecycle = match task.lifecycle.as_str() {
        "pending_launch" => TaskLifecycle::PendingLaunch,
        "starting" => TaskLifecycle::Starting,
        "ready" => TaskLifecycle::Ready,
        "running" => TaskLifecycle::Running,
        "suspended" => TaskLifecycle::Suspended,
        "done" => {
            let label = task.termination.as_deref().ok_or_else(|| {
                incompatible(format!(
                    "task {} is done but the checkpoint does not say why; a finished task without \
                     its termination reason is not restorable",
                    task.task_id
                ))
            })?;
            TaskLifecycle::Done(termination_from_label(label).ok_or_else(|| {
                incompatible(format!(
                    "task {} names termination reason {label:?}, which this kernel does not know",
                    task.task_id
                ))
            })?)
        }
        other => {
            return Err(incompatible(format!(
                "task {} names lifecycle {other:?}, which this kernel does not know",
                task.task_id
            )));
        }
    };
    Ok(lifecycle)
}

fn restore_task_wait(task: &TaskControlState) -> Result<Option<WaitReason>, KernelFault> {
    match task.wait.as_deref() {
        None => Ok(None),
        Some("approval") => Ok(Some(WaitReason::Approval)),
        Some("sub_agent_join") => Ok(Some(WaitReason::SubAgentJoin(
            task.waiting_on
                .iter()
                .map(|child| child.as_str().into())
                .collect(),
        ))),
        Some(other) => Err(incompatible(format!(
            "task {} waits on {other:?}, which this kernel does not know",
            task.task_id
        ))),
    }
}

fn termination_from_label(label: &str) -> Option<TerminationReason> {
    Some(match label {
        "completed" => TerminationReason::Completed,
        "max_turns" => TerminationReason::MaxTurns,
        "token_budget" => TerminationReason::TokenBudget,
        "timeout" => TerminationReason::Timeout,
        "user_abort" => TerminationReason::UserAbort,
        "error" => TerminationReason::Error,
        "milestone_exceeded" => TerminationReason::MilestoneExceeded,
        "context_overflow" => TerminationReason::ContextOverflow,
        "no_progress" => TerminationReason::NoProgress,
        _ => return None,
    })
}

/// §12.2 · put the context-VM partition back on the engine.
///
/// Order matters: the messages are pushed first so the partition token counters land on the
/// checkpoint's own numbers, then the handle table is repopulated **by id** so a handle addresses
/// the same body it addressed before, then the allocator is moved past all of them.
fn restore_context_vm(
    engine: &mut LoopStateMachine,
    state: &ContextVmStateV1,
) -> Result<(), KernelFault> {
    let ctx = &mut engine.ctx;
    for entry in &state.messages {
        let message = restore_message(&entry.role, &entry.body, &entry.tool_calls)?;
        match entry.partition {
            MessagePartition::System => ctx.partitions.system.push(message, entry.tokens),
            MessagePartition::History => ctx.partitions.history.push(message, entry.tokens),
        }
    }
    for slot in &state.knowledge {
        let message = restore_message(&slot.role, &slot.body, &[])?;
        ctx.partitions.knowledge.push_entry(
            slot.key.as_deref().map(Into::into),
            message,
            slot.tokens,
            slot.pinned,
        );
        // The boundary-eviction mark is bookkeeping the push path does not take, so it is set
        // straight onto the entry it belongs to — a slot that was marked for removal must still be
        // marked after a restore, or the next sweep keeps something the run had already dropped.
        if slot.evict_at_boundary
            && let Some(entry) = ctx.partitions.knowledge.entries.last_mut()
        {
            entry.evict_at_boundary = true;
        }
    }
    ctx.partitions.signals = state.signals.clone();
    ctx.partitions.task_state = restore_task_state(&state.task_state);
    ctx.last_activity_ms = state.last_activity_ms.get();
    ctx.last_compact_ms = state.last_compact_ms.map(WireU64::get);
    ctx.active_skills = state
        .active_skills
        .iter()
        .map(|lease| (lease.skill.as_str().into(), lease.lease_until_turn))
        .collect();

    for handle in &state.handles {
        ctx.handles.insert(Handle {
            id: handle.handle_id,
            kind: restore_handle_kind(&handle.kind)?,
            residency: restore_residency(handle)?,
            tokens: handle.tokens,
            source: handle.source.as_deref().map(Into::into),
        });
    }
    ctx.restore_next_handle_id(state.next_handle_id);
    if !ctx.restore_frozen_history_len(state.frozen_history_len as usize) {
        return Err(incompatible(format!(
            "the checkpoint freezes {} history messages but restores only {}",
            state.frozen_history_len,
            ctx.partitions.history.messages.len()
        )));
    }
    Ok(())
}

fn restore_message(
    role: &str,
    body: &StoredMessageBody,
    tool_calls: &[LogicalToolCall],
) -> Result<Message, KernelFault> {
    let role = role_from_label(role)
        .ok_or_else(|| incompatible(format!("the checkpoint carries message role {role:?}")))?;
    let content = match body {
        StoredMessageBody::Inline(inline) => message_content(
            inline.text.clone(),
            inline.tool_call_id.as_deref(),
            inline.is_error,
        ),
        StoredMessageBody::Reference(reference) => message_content(
            reference.preview.clone(),
            reference.tool_call_id.as_deref(),
            reference.is_error,
        ),
        StoredMessageBody::Structured(structured) => serde_json::from_str(&structured.content_json)
            .map_err(|error| {
                incompatible(format!(
                    "the checkpoint carries a structured message body that does not decode: {error}"
                ))
            })?,
    };
    Ok(Message {
        role,
        content,
        tool_calls: tool_calls
            .iter()
            .map(|call| {
                Ok(crate::types::message::ToolCall {
                    id: call.call_id.as_str().into(),
                    name: call.name.as_str().into(),
                    arguments: serde_json::from_str(&call.arguments).map_err(|error| {
                        incompatible(format!(
                            "tool call {} carries arguments that do not decode: {error}",
                            call.call_id
                        ))
                    })?,
                })
            })
            .collect::<Result<Vec<_>, KernelFault>>()?,
        token_count: None,
    })
}

fn restore_handle_kind(label: &str) -> Result<HandleKind, KernelFault> {
    Ok(match label {
        "tool_result" => HandleKind::ToolResult,
        "memory_page" => HandleKind::MemoryPage,
        "knowledge_entry" => HandleKind::KnowledgeEntry,
        "sub_agent_join" => HandleKind::SubAgentJoin,
        other => {
            return Err(incompatible(format!(
                "the checkpoint carries handle kind {other:?}, which this kernel does not know"
            )));
        }
    })
}

fn restore_residency(handle: &HandleState) -> Result<Residency, KernelFault> {
    let missing = |what: &str| {
        incompatible(format!(
            "handle {} is {} but carries no {what}",
            handle.handle_id, handle.residency
        ))
    };
    Ok(match handle.residency.as_str() {
        "resident" => Residency::Resident,
        "collapsed" => Residency::Collapsed,
        "external" => Residency::External {
            payload_ref: handle
                .payload_ref
                .clone()
                .ok_or_else(|| missing("locator"))?,
            digest: handle.digest.clone().ok_or_else(|| missing("digest"))?,
            original_size: handle
                .original_size
                .ok_or_else(|| missing("original size"))?
                .get(),
        },
        "paged_out" => Residency::PagedOut {
            payload_ref: handle
                .payload_ref
                .clone()
                .ok_or_else(|| missing("locator"))?,
            digest: handle.digest.clone().ok_or_else(|| missing("digest"))?,
        },
        other => {
            return Err(incompatible(format!(
                "the checkpoint carries residency {other:?}, which this kernel does not know"
            )));
        }
    })
}

fn incompatible(message: String) -> KernelFault {
    KernelFault::new(KernelFaultCode::CheckpointIncompatible, message)
}

fn project_task_state(state: &TaskState) -> LogicalTaskState {
    LogicalTaskState {
        goal: state.goal.clone(),
        criteria: state.criteria.clone(),
        plan: state
            .plan
            .iter()
            .map(|step| LogicalPlanStep {
                label: step.label.clone(),
                done: step.done,
            })
            .collect(),
        current_step: state.current_step.map(|index| index as u32),
        progress: state.progress.clone(),
        scratchpad: state.scratchpad.clone(),
        blocked_on: state.blocked_on.clone(),
        directives: state.directives.clone(),
        preserved_refs: state.preserved_refs.clone(),
        recent_actions: state.recent_actions.clone(),
        compression_log: state
            .compression_log
            .iter()
            .map(|entry| LogicalCompressionEntry {
                action: entry.action.clone(),
                summary: entry.summary.clone(),
            })
            .collect(),
        compression_log_dropped: WireU64::new(state.compression_log_dropped),
    }
}

fn restore_task_state(state: &LogicalTaskState) -> TaskState {
    TaskState {
        goal: state.goal.clone(),
        criteria: state.criteria.clone(),
        plan: state
            .plan
            .iter()
            .map(|step| PlanStep {
                label: step.label.clone(),
                done: step.done,
            })
            .collect(),
        current_step: state.current_step.map(|index| index as usize),
        progress: state.progress.clone(),
        scratchpad: state.scratchpad.clone(),
        blocked_on: state.blocked_on.clone(),
        directives: state.directives.clone(),
        preserved_refs: state.preserved_refs.clone(),
        recent_actions: state.recent_actions.clone(),
        compression_log: state
            .compression_log
            .iter()
            .map(|entry| CompressionEntry {
                action: entry.action.clone(),
                summary: entry.summary.clone(),
            })
            .collect(),
        compression_log_dropped: state.compression_log_dropped.get(),
    }
}

fn authority(message: &str) -> SyscallRefusal {
    SyscallRefusal::Fault(KernelFault::new(
        KernelFaultCode::InvalidAuthority,
        message.to_string(),
    ))
}

fn denial_reason(disposition: &Disposition, fallback: &str) -> String {
    match disposition {
        Disposition::Deny { stage, reason } => format!("{stage}: {reason}"),
        Disposition::RateLimited { retry_after_ms } => {
            format!("rate limited; retry after {retry_after_ms}ms")
        }
        Disposition::Gate { reason, .. } => format!("awaiting approval: {reason}"),
        Disposition::Defer { slot } => format!("deferred at slot {slot}"),
        Disposition::Allow => fallback.to_string(),
    }
}

/// What the kernel authored for a pending `PersistMemory` effect (§22.13).
#[derive(Debug, Clone, PartialEq)]
struct AuthoredMemoryWrite {
    binding_id: MemoryBindingId,
    name: String,
    kind: WireMemoryKind,
    size_bytes: u32,
}

/// What the kernel authored for a pending `QueryMemory` effect.
#[derive(Debug, Clone, PartialEq)]
struct AuthoredMemoryQuery {
    binding_id: MemoryBindingId,
    text: String,
    requested_k: u32,
}

/// What the kernel published for a pending `LoadPayload` effect (§7.10 rule 4).
#[derive(Debug, Clone, PartialEq)]
struct PendingPayloadLoad {
    /// The wire address of the handle — the tool `call_id` for an external result, the kernel-minted
    /// archive id for a paged-out one.
    handle_id: String,
    /// The digest the body has to reproduce. The kernel never saw the body, so this is the *only*
    /// thing that makes a restored payload the one that left.
    digest: String,
    /// Present only for [`Residency::External`], whose declared size the kernel admitted and can
    /// therefore hold the host to. A page-out archive is checked by digest alone.
    original_size: Option<u64>,
}

/// What one admitted syscall produced.
#[derive(Debug, Default)]
struct SyscallOutcome {
    effects: Vec<KernelEffect>,
    /// Set only by `SubmitWorkflow`'s bootstrap arm — the one syscall that moves the focus.
    focus: Option<ExecutionFocus>,
    /// The DAG grew and still owes a spawn round. Honoured on the provider-tool path; on the
    /// child-completion path the completion's own drive produces the batch (§7.7 ordering).
    needs_workflow_round: bool,
}

/// Reduces the five canonical input classes onto the kernel's existing semantic mechanisms.
///
/// Use it as the plan function of [`KernelTransaction::prepare`](super::transaction::KernelTransaction::prepare):
///
/// ```ignore
/// let preparation = tx.prepare(&envelope, |ctx| driver.plan(ctx));
/// // ... host CAS-appends the record ...
/// let committed = tx.commit(&token, &head)?;
/// driver.note_committed(committed.step_seq)?;
/// ```
pub struct CanonicalOperationDriver {
    engine: Option<LoopStateMachine>,
    root_kind: Option<RootKind>,
    focus: Option<ExecutionFocus>,
    workflow_id: Option<WorkflowId>,
    /// Wire node identity by internal DAG index — the DAG the engine runs is index-addressed, the
    /// wire is not, and the mapping is what keeps a `SpawnTasks` effect nameable by the host.
    node_ids: Vec<NodeId>,
    /// Canonical source DAG in the same index order as `node_ids`. Checkpoint projection pairs it
    /// with the semantic node statuses without serializing the graph's private indexes.
    workflow_nodes: Vec<WireNode>,
    /// The attempt the kernel minted for each **live** task. §10.4: a host may not create or
    /// rewrite child identity through a resolution or a completion, so a completion that names an
    /// attempt this kernel never issued is refused rather than folded in. A completed attempt is
    /// removed here, which is what makes a second completion — and the `parent_requests` riding on
    /// it — a stale causation rather than a second helping of authority.
    attempts: BTreeMap<String, AttemptId>,
    /// §7.6 · the provider calls this operation is waiting on, by effect id. At most one is live
    /// (DEC-3); the map keys it so a resolution names its own call rather than "the current one".
    provider_calls: BTreeMap<EffectId, PendingProviderCall>,
    /// §22.13 · the memory records **this kernel authored** for its pending `PersistMemory`
    /// effects. The resolution reports these, never what the host echoes back: a receipt may carry
    /// its store's own locator and digest and nothing else, so no host reply can restate a name,
    /// kind, size, trust or provenance the kernel derived.
    pending_memory_writes: BTreeMap<EffectId, AuthoredMemoryWrite>,
    /// The same for `QueryMemory`: the query the kernel authored and the width it clamped.
    pending_memory_queries: BTreeMap<EffectId, AuthoredMemoryQuery>,
    /// §7.10 · the handle each pending `LoadPayload` addresses, with the digest the loaded body has
    /// to reproduce. Kept here rather than re-read from the handle table at resolution time, so a
    /// residency that moved in between cannot silently change what a page-in is verified against.
    pending_payload_loads: BTreeMap<EffectId, PendingPayloadLoad>,
    /// Tool call ids that already produced a syscall. A causation is spent once — replaying the
    /// same provider result with a fresh input id must not buy a second workflow append.
    consumed_calls: BTreeSet<String>,
    /// §13.2 / DEC-6 · the live-mutable half of the configuration, plus the revision two concurrent
    /// writers race on. Seeded from the genesis record's resolved configuration; only
    /// `HostCommand::ApplyPolicyPatch` moves it. Boot-only axes are read from `context.config`
    /// instead, which is why the transaction never needs a second copy of this.
    policy: Option<LivePolicyState>,
    /// §7.3 · the verification contract whose cascade is installed on the engine, if any.
    ///
    /// The engine's own `LoopAction::EvaluateMilestone` names only a phase, because internally
    /// there is one cascade and a phase id is enough. The wire needs the pair: `phase_id` is
    /// unique only within its contract, so `(contract_id, phase_id)` is the host's complete lookup
    /// key. Threading it here rather than through the semantic engine keeps the legacy internal
    /// action untouched (Task 23 owns that) while the canonical projection is already total.
    loaded_contract_id: Option<String>,
    staged: Option<StagedFocus>,
    poison: Option<KernelFault>,
}

impl Default for CanonicalOperationDriver {
    fn default() -> Self {
        Self::new()
    }
}

impl CanonicalOperationDriver {
    pub fn new() -> Self {
        Self {
            engine: None,
            root_kind: None,
            focus: None,
            workflow_id: None,
            node_ids: Vec::new(),
            workflow_nodes: Vec::new(),
            attempts: BTreeMap::new(),
            provider_calls: BTreeMap::new(),
            pending_memory_writes: BTreeMap::new(),
            pending_memory_queries: BTreeMap::new(),
            pending_payload_loads: BTreeMap::new(),
            consumed_calls: BTreeSet::new(),
            policy: None,
            loaded_contract_id: None,
            staged: None,
            poison: None,
        }
    }

    // ----- observers -----

    /// The operation's root class. `None` until the root start commits; immutable afterwards.
    pub fn root_kind(&self) -> Option<RootKind> {
        self.root_kind
    }

    /// Where control currently is. Moves only on a committed transition (§7.4).
    pub fn focus(&self) -> Option<&ExecutionFocus> {
        self.focus.as_ref()
    }

    pub fn workflow_id(&self) -> Option<&WorkflowId> {
        self.workflow_id.as_ref()
    }

    /// Return the kernel-issued live attempt for `task_id`.
    ///
    /// Bindings use this read-only projection when a legacy host completion carries only a task
    /// identity. The value comes from checkpointed kernel state; hosts must never synthesize it.
    pub fn attempt_id(&self, task_id: &str) -> Option<&AttemptId> {
        self.attempts.get(task_id)
    }

    pub fn poison(&self) -> Option<&KernelFault> {
        self.poison.as_ref()
    }

    /// Read-only access to the semantic engine, for tests and host projections.
    pub fn engine(&self) -> Option<&LoopStateMachine> {
        self.engine.as_ref()
    }

    /// Where the driver's own fold says the operation is. The transaction stays the authority on
    /// lifecycle; this exists so a host projection never needs a second copy of the rule.
    pub fn lifecycle(&self) -> OperationLifecycle {
        match (self.engine.is_some(), self.root_kind) {
            (false, _) => OperationLifecycle::Created,
            (true, None) => OperationLifecycle::Configured,
            (true, Some(_)) => OperationLifecycle::Running,
        }
    }

    // ----- §12.1 · the logical-state projection -----

    /// Project the three driver-owned partitions of §12.1, plus the two transition fields the
    /// driver rather than the transaction owns.
    ///
    /// Explicitly a **projection**, not a serialisation: every value below is read through a named
    /// accessor and written into a versioned DTO field. That is the whole point of §12.1 — adding a
    /// field to [`LoopStateMachine`] must not change the checkpoint format, and a checkpoint field
    /// must not silently vanish because an internal one was renamed. It is also why the internal
    /// enums travel as their `label()` plus their carried data: `TaskLifecycle::Done(reason)` and
    /// `Residency::External { .. }` are semantic-kernel shapes, and mirroring them would make the
    /// checkpoint a checkpoint of a private layout.
    pub fn project_logical_state(&self) -> LogicalStateProjection {
        LogicalStateProjection {
            root_kind: self.root_kind,
            focus: self.focus.clone(),
            syscall: self.project_syscall_state(),
            scheduler: self.project_scheduler_state(),
            context_vm: self.project_context_vm_state(),
        }
    }

    fn project_syscall_state(&self) -> SyscallStateV1 {
        SyscallStateV1 {
            policy_revision: self.policy.as_ref().map(LivePolicyState::revision),
            live_config: self.policy.as_ref().map(|policy| policy.config().clone()),
            provider_calls: self
                .provider_calls
                .iter()
                .map(|(effect_id, call)| PendingProviderCallState {
                    effect_id: effect_id.clone(),
                    task_id: call.task_id.clone(),
                    exposed_tools: call.exposed_tools.iter().cloned().collect(),
                })
                .collect(),
            consumed_call_ids: self.consumed_calls.iter().cloned().collect(),
            authored_memory_writes: self
                .pending_memory_writes
                .iter()
                .map(|(effect_id, write)| AuthoredMemoryWriteState {
                    effect_id: effect_id.clone(),
                    binding_id: write.binding_id.clone(),
                    name: write.name.clone(),
                    kind: write.kind,
                    size_bytes: write.size_bytes,
                })
                .collect(),
            authored_memory_queries: self
                .pending_memory_queries
                .iter()
                .map(|(effect_id, query)| AuthoredMemoryQueryState {
                    effect_id: effect_id.clone(),
                    binding_id: query.binding_id.clone(),
                    text: query.text.clone(),
                    requested_k: query.requested_k,
                })
                .collect(),
            memory_write_window_ms: self
                .engine
                .as_ref()
                .map(|engine| {
                    engine
                        .memory_write_window()
                        .iter()
                        .copied()
                        .map(WireU64::new)
                        .collect()
                })
                .unwrap_or_default(),
        }
    }

    fn project_scheduler_state(&self) -> SchedulerStateV1 {
        let Some(engine) = self.engine.as_ref() else {
            return SchedulerStateV1::default();
        };
        let (total_tokens, subagents_spawned, rounds_completed) = engine.local_budget_usage();
        let signal_state = engine.signal_checkpoint_state();
        let entropy_state = engine.entropy_checkpoint_state();
        let workflow = engine.workflow_checkpoint_nodes().map(|runtime_nodes| {
            let workflow_id = self
                .workflow_id
                .as_ref()
                .expect("an active canonical workflow has a logical identity");
            assert_eq!(
                runtime_nodes.len(),
                self.workflow_nodes.len(),
                "the semantic workflow and its canonical source DAG stay index-aligned"
            );
            WorkflowGraphState {
                workflow_id: workflow_id.clone(),
                nodes: runtime_nodes
                    .into_iter()
                    .zip(self.workflow_nodes.iter())
                    .map(|(runtime, wire)| WorkflowNodeState {
                        node_id: wire.node_id.clone(),
                        task: wire.task.clone(),
                        depends_on: wire.depends_on.clone(),
                        run_spec: wire.run_spec.clone(),
                        kind: workflow_kind_label(&runtime).to_string(),
                        status: workflow_status_label(runtime.status).to_string(),
                        active_agent_id: runtime.active_agent_id,
                        iterations_completed: runtime.iterations_completed as u32,
                    })
                    .collect(),
            }
        });
        SchedulerStateV1 {
            turn: engine.turn,
            total_tokens: WireU64::new(total_tokens),
            rounds_completed,
            subagents_spawned,
            started_at_ms: engine.started_at_ms().map(WireU64::new),
            wall_budget_ms: engine.wall_budget().map(WireU64::new),
            tasks: engine
                .task_table()
                .all()
                .iter()
                .map(|tcb| TaskControlState {
                    task_id: TaskId::new(tcb.id.as_str())
                        .expect("an internal task id is always a legal branded ref"),
                    parent_task_id: tcb
                        .parent
                        .as_ref()
                        .and_then(|parent| TaskId::new(parent.as_str()).ok()),
                    lifecycle: tcb.state.label().to_string(),
                    termination: match tcb.state {
                        TaskLifecycle::Done(reason) => Some(reason.label().to_string()),
                        _ => None,
                    },
                    wait: tcb.wait.as_ref().map(|wait| wait.label().to_string()),
                    waiting_on: match &tcb.wait {
                        Some(WaitReason::SubAgentJoin(children)) => children
                            .iter()
                            .filter_map(|child| TaskId::new(child.as_str()).ok())
                            .collect(),
                        _ => Vec::new(),
                    },
                    capability_ids: tcb.caps.iter().map(|cap| cap.to_string()).collect(),
                    process: tcb.proc.as_ref().map(|process| ChildProcessState {
                        role: agent_role_label(process.role).to_string(),
                        isolation: agent_isolation_label(process.isolation).to_string(),
                        context_inheritance: context_inheritance_label(process.context_inheritance)
                            .to_string(),
                        join_result: process.result.as_ref().map(|result| {
                            super::scalar::BoundedJson::new(
                                serde_json::to_value(result)
                                    .expect("a child join result is serializable"),
                            )
                            .expect("a child join result is bounded")
                        }),
                    }),
                    tokens_used: WireU64::new(tcb.budget.total_tokens),
                    turns_used: tcb.budget.turns,
                })
                .collect(),
            attempts: self
                .attempts
                .iter()
                .filter_map(|(task_id, attempt_id)| {
                    Some(TaskAttemptState {
                        task_id: TaskId::new(task_id.as_str()).ok()?,
                        attempt_id: attempt_id.clone(),
                    })
                })
                .collect(),
            workflow,
            queued_signals: signal_state
                .queued
                .into_iter()
                .map(|queued| queued_signal_state(&queued))
                .collect(),
            signal_dedupe_keys: signal_state
                .seen_order
                .into_iter()
                .map(|key| key.to_string())
                .collect(),
            milestone: self
                .loaded_contract_id
                .as_ref()
                .map(|contract_id| MilestoneState {
                    contract_id: contract_id.clone(),
                    phase_id: engine.current_milestone_phase_id().map(str::to_string),
                    complete: engine.is_milestone_complete(),
                    blocked_count: engine.milestone_blocked_count(),
                }),
            entropy: EntropyState {
                window: entropy_state
                    .window
                    .into_iter()
                    .map(|entry| EntropyTurnState {
                        errored_results: entry.errored_results,
                        total_results: entry.total_results,
                        rollbacks: entry.rollbacks,
                    })
                    .collect(),
                rollbacks_pending: entropy_state.rollbacks_pending,
                disarmed: entropy_state.disarmed,
                last_alert_turn: entropy_state.last_alert_turn,
            },
        }
    }

    fn project_context_vm_state(&self) -> ContextVmStateV1 {
        let Some(engine) = self.engine.as_ref() else {
            return ContextVmStateV1::default();
        };
        let ctx = &engine.ctx;
        ContextVmStateV1 {
            handles: ctx
                .handles
                .all()
                .iter()
                .map(|handle| {
                    let (payload_ref, digest, original_size) = match &handle.residency {
                        Residency::External {
                            payload_ref,
                            digest,
                            original_size,
                        } => (
                            Some(payload_ref.clone()),
                            Some(digest.clone()),
                            Some(WireU64::new(*original_size)),
                        ),
                        Residency::PagedOut {
                            payload_ref,
                            digest,
                        } => (Some(payload_ref.clone()), Some(digest.clone()), None),
                        Residency::Resident | Residency::Collapsed => (None, None, None),
                    };
                    HandleState {
                        handle_id: handle.id,
                        kind: handle_kind_label(&handle.kind).to_string(),
                        residency: handle.residency.label().to_string(),
                        payload_ref,
                        digest,
                        original_size,
                        tokens: handle.tokens,
                        source: handle.source.as_ref().map(|source| source.to_string()),
                    }
                })
                .collect(),
            next_handle_id: ctx.next_handle_id(),
            pending_payload_loads: self
                .pending_payload_loads
                .iter()
                .map(|(effect_id, load)| PendingPayloadLoadState {
                    effect_id: effect_id.clone(),
                    handle_id: load.handle_id.clone(),
                    digest: load.digest.clone(),
                    original_size: load.original_size.map(WireU64::new),
                })
                .collect(),
            active_skills: ctx
                .active_skills
                .iter()
                .map(|(skill, lease)| SkillLeaseState {
                    skill: skill.to_string(),
                    lease_until_turn: *lease,
                })
                .collect(),
            knowledge: ctx
                .partitions
                .knowledge
                .entries
                .iter()
                .map(|entry| KnowledgeSlotState {
                    key: entry.key.as_ref().map(|key| key.to_string()),
                    role: role_label(entry.message.role).to_string(),
                    body: self.project_body(&entry.message),
                    tokens: entry.tokens,
                    pinned: entry.pinned,
                    evict_at_boundary: entry.evict_at_boundary,
                })
                .collect(),
            signals: ctx.partitions.signals.clone(),
            messages: ctx
                .partitions
                .system
                .messages
                .iter()
                .map(|message| self.project_message(MessagePartition::System, message))
                .chain(
                    ctx.partitions
                        .history
                        .messages
                        .iter()
                        .map(|message| self.project_message(MessagePartition::History, message)),
                )
                .collect(),
            task_state: project_task_state(&ctx.partitions.task_state),
            partition_tokens: PartitionTokenState {
                system: ctx.partitions.system.token_count,
                knowledge: ctx.partitions.knowledge.token_count,
                history: ctx.partitions.history.token_count,
            },
            history_len: ctx.partitions.history.messages.len() as u32,
            frozen_history_len: ctx.frozen_history_len() as u32,
            last_activity_ms: WireU64::new(ctx.last_activity_ms),
            last_compact_ms: ctx.last_compact_ms.map(WireU64::new),
        }
    }

    /// §5q-2 · one stored message, projected.
    fn project_message(
        &self,
        partition: MessagePartition,
        message: &Message,
    ) -> StoredMessageState {
        StoredMessageState {
            partition,
            role: role_label(message.role).to_string(),
            body: self.project_body(message),
            tool_calls: message
                .tool_calls
                .iter()
                .map(|call| LogicalToolCall {
                    call_id: call.id.to_string(),
                    name: call.name.to_string(),
                    arguments: call.arguments.to_string(),
                })
                .collect(),
            tokens: message.token_count.unwrap_or(0),
        }
    }

    /// §7.10 · inline or by reference, decided by the message's own residency.
    ///
    /// The rule is not "is this body big" but "where does this body live": a result whose handle
    /// says `External` was never resident in the first place (only its preview is in the message),
    /// and one that says `PagedOut` left under pressure and lives with the host now. Either way the
    /// checkpoint carries the reference and the digest that verifies a page-in — putting the bytes
    /// back would re-create exactly the round trip §7.10 exists to delete.
    fn project_body(&self, message: &Message) -> StoredMessageBody {
        let Some((text, tool_call_id, is_error)) = message_body_parts(message) else {
            return StoredMessageBody::Structured(StructuredMessageBody {
                content_json: serde_json::to_string(&message.content)
                    .unwrap_or_else(|_| "null".to_string()),
            });
        };
        let Some(call_id) = tool_call_id.as_deref() else {
            return StoredMessageBody::Inline(InlineMessageBody {
                text,
                tool_call_id,
                is_error,
            });
        };
        let referenced = self.engine.as_ref().and_then(|engine| {
            let handle = engine
                .ctx
                .handles
                .all()
                .iter()
                .find(|handle| handle.source.as_deref() == Some(call_id))?;
            let digest = handle.residency.digest()?;
            Some((
                handle.id,
                digest.to_string(),
                matches!(handle.residency, Residency::PagedOut { .. }),
            ))
        });
        match referenced {
            Some((handle_id, digest, is_paged_out)) => {
                StoredMessageBody::Reference(ReferencedMessageBody {
                    handle_id,
                    digest,
                    preview: if is_paged_out {
                        crate::context::renderer::collapse_preview(&text, call_id)
                    } else {
                        truncate_on_char_boundary(&text, self.preview_bytes())
                    },
                    tool_call_id,
                    is_error,
                })
            }
            None => StoredMessageBody::Inline(InlineMessageBody {
                text,
                tool_call_id,
                is_error,
            }),
        }
    }

    fn preview_bytes(&self) -> usize {
        self.policy
            .as_ref()
            .map(|policy| policy.config().payload_policy.preview_bytes as usize)
            .unwrap_or(2 * 1024)
    }

    // ----- §12.2 · the logical-state restore -----

    /// Rebuild a driver from a checkpoint's logical state (§12.2 line 3).
    ///
    /// The exact inverse of [`Self::project_logical_state`], and deliberately nothing more: every
    /// value written here is a value the projection reads back, so "did the restore work" is not a
    /// judgement call — [`super::restore::restore_operation`] re-projects immediately afterwards and
    /// compares the digest. A field this function forgets therefore fails the restore rather than
    /// producing a runtime that is quietly one field short of the one that crashed.
    ///
    /// Task 16b makes every scheduler branch invertible here: workflow source nodes rebuild their
    /// private graph indexes, queued signals rebuild priority and dedupe state, and child process
    /// identity is restored without re-running permission defaults. Unknown labels and inconsistent
    /// relationships still fail closed as `CheckpointIncompatible`.
    pub fn restore_logical_state(
        genesis_config: &ResolvedOperationConfig,
        state: &LogicalKernelState,
    ) -> Result<Self, KernelFault> {
        let mut driver = Self::new();
        let live_config = state
            .syscall
            .live_config
            .clone()
            .unwrap_or_else(|| genesis_config.clone());

        // The engine is built from the configuration and then *moved* onto the checkpointed facts;
        // it is never deserialised. Boot-only axes come from the genesis configuration the record
        // froze, live-mutable ones from the patched configuration the checkpoint carries.
        let mut engine = build_engine(genesis_config);
        install_live_policies(&mut engine, &live_config);
        engine.set_root_workflow(state.transition.root_kind == Some(RootKind::Workflow));
        driver.policy = Some(LivePolicyState::restore(
            state.syscall.policy_revision.unwrap_or(WireU64::ZERO),
            live_config.clone(),
        ));

        restore_scheduler(&mut engine, &live_config, &state.scheduler)?;
        if let Some(preempt) =
            state
                .transition
                .pending_effects
                .iter()
                .find_map(|effect| match &effect.effect {
                    EffectKind::PreemptTasks(preempt) => Some(preempt),
                    _ => None,
                })
        {
            engine.restore_pending_preempt(
                preempt
                    .attempts
                    .iter()
                    .map(|attempt| attempt.task_id.as_str().to_string())
                    .collect(),
                preempt.reason.clone(),
            );
        }
        restore_context_vm(&mut engine, &state.context_vm)?;
        engine.restore_memory_write_window(
            state
                .syscall
                .memory_write_window_ms
                .iter()
                .map(|at| at.get())
                .collect(),
        );

        if let Some(milestone) = &state.scheduler.milestone {
            let contract = live_config
                .verification_contract(&milestone.contract_id)
                .ok_or_else(|| {
                    KernelFault::new(
                        KernelFaultCode::CheckpointIncompatible,
                        format!(
                            "the checkpoint runs verification contract {:?}, which this \
                             operation's configuration no longer declares",
                            milestone.contract_id
                        ),
                    )
                })?;
            engine.load_milestone_contract(core_milestone_contract(contract, &live_config));
            if !engine
                .restore_milestone_cursor(milestone.phase_id.as_deref(), milestone.blocked_count)
            {
                return Err(KernelFault::new(
                    KernelFaultCode::CheckpointIncompatible,
                    format!(
                        "the checkpoint sits on milestone phase {:?} of contract {:?}, which that \
                         contract does not declare",
                        milestone.phase_id, milestone.contract_id
                    ),
                ));
            }
            driver.loaded_contract_id = Some(milestone.contract_id.clone());
        }

        driver.engine = Some(engine);
        driver.root_kind = state.transition.root_kind;
        driver.focus = state.transition.focus.clone();
        if let Some(workflow) = &state.scheduler.workflow {
            driver.workflow_id = Some(workflow.workflow_id.clone());
            driver.node_ids = workflow
                .nodes
                .iter()
                .map(|node| node.node_id.clone())
                .collect();
            driver.workflow_nodes = workflow
                .nodes
                .iter()
                .map(|node| WireNode {
                    node_id: node.node_id.clone(),
                    task: node.task.clone(),
                    depends_on: node.depends_on.clone(),
                    run_spec: node.run_spec.clone(),
                })
                .collect();
        }
        driver.attempts = state
            .scheduler
            .attempts
            .iter()
            .map(|attempt| {
                (
                    attempt.task_id.as_str().to_string(),
                    attempt.attempt_id.clone(),
                )
            })
            .collect();
        driver.provider_calls = state
            .syscall
            .provider_calls
            .iter()
            .map(|call| {
                (
                    call.effect_id.clone(),
                    PendingProviderCall {
                        task_id: call.task_id.clone(),
                        exposed_tools: call.exposed_tools.iter().cloned().collect(),
                    },
                )
            })
            .collect();
        driver.consumed_calls = state.syscall.consumed_call_ids.iter().cloned().collect();
        driver.pending_memory_writes = state
            .syscall
            .authored_memory_writes
            .iter()
            .map(|write| {
                (
                    write.effect_id.clone(),
                    AuthoredMemoryWrite {
                        binding_id: write.binding_id.clone(),
                        name: write.name.clone(),
                        kind: write.kind,
                        size_bytes: write.size_bytes,
                    },
                )
            })
            .collect();
        driver.pending_memory_queries = state
            .syscall
            .authored_memory_queries
            .iter()
            .map(|query| {
                (
                    query.effect_id.clone(),
                    AuthoredMemoryQuery {
                        binding_id: query.binding_id.clone(),
                        text: query.text.clone(),
                        requested_k: query.requested_k,
                    },
                )
            })
            .collect();
        driver.pending_payload_loads = state
            .context_vm
            .pending_payload_loads
            .iter()
            .map(|load| {
                (
                    load.effect_id.clone(),
                    PendingPayloadLoad {
                        handle_id: load.handle_id.clone(),
                        digest: load.digest.clone(),
                        original_size: load.original_size.map(WireU64::get),
                    },
                )
            })
            .collect();
        Ok(driver)
    }

    // ----- the plan function -----

    /// Plan one input.
    ///
    /// Pass this to [`KernelTransaction::prepare`](super::transaction::KernelTransaction::prepare).
    /// The focus/root-kind fold does **not** advance here — call [`Self::note_committed`] once the
    /// host's append and the transaction's commit have both succeeded.
    pub fn plan(&mut self, context: &PlanContext<'_>) -> Result<PlannedStep, KernelFault> {
        if let Some(fault) = &self.poison {
            return Err(fault.clone());
        }
        if let Some(staged) = &self.staged {
            let staged_seq = staged.step_seq;
            return Err(self.poison_with(KernelFault::new(
                KernelFaultCode::TransactionConflict,
                format!(
                    "the driver still holds the plan of step {staged_seq}; its transition never \
                     committed while the semantic kernel already advanced under it, so this \
                     runtime no longer describes the journal — rebuild from the records"
                ),
            )));
        }
        let mut step = self.plan_inner(context)?;
        step.observations = self
            .engine
            .as_mut()
            .map(LoopStateMachine::take_observations)
            .unwrap_or_default();
        self.staged = Some(StagedFocus {
            step_seq: context.step_seq,
            root_kind: step.root_kind,
            focus: step.focus.clone(),
        });
        Ok(step)
    }

    /// Install the staged fold after the transaction committed the record (§7.4: a focus moves only
    /// on a committed transition).
    pub fn note_committed(&mut self, step_seq: WireU64) -> Result<(), KernelFault> {
        if let Some(fault) = &self.poison {
            return Err(fault.clone());
        }
        let Some(staged) = self.staged.take() else {
            return Err(self.poison_with(KernelFault::new(
                KernelFaultCode::TransactionConflict,
                format!("step {step_seq} committed, but the driver planned no such step"),
            )));
        };
        if staged.step_seq != step_seq {
            let planned = staged.step_seq;
            return Err(self.poison_with(KernelFault::new(
                KernelFaultCode::TransactionConflict,
                format!("step {step_seq} committed, but the driver planned step {planned}"),
            )));
        }
        if let Some(kind) = staged.root_kind {
            self.root_kind = Some(kind);
        }
        self.focus = staged.focus;
        Ok(())
    }

    /// Plan **and** fold in one call — the shape
    /// [`rebuild_from_records`](super::transaction::KernelTransaction::rebuild_from_records) needs,
    /// where every record it replays is by definition already durable.
    pub fn fold(&mut self, context: &PlanContext<'_>) -> Result<PlannedStep, KernelFault> {
        let step = self.plan(context)?;
        self.note_committed(context.step_seq)?;
        Ok(step)
    }

    // ----- §10.2 · the agent-authored workflow seam -----

    /// Enter a workflow the agent asked for, inside an agent root (§10.2).
    ///
    /// This is the P1 reduction point Task 10 wires its `SyscallRequest::SubmitWorkflow` gate to;
    /// the authority rules it enforces are already the final ones:
    ///
    /// * the root kind stays `Agent` — a syscall never re-roots an operation;
    /// * the focus moves to `WorkflowController { parent_task_id: Some(agent task) }`;
    /// * depth is at most 1. Asking for a workflow while the focus already *is* a
    ///   `WorkflowController` is an `InvalidAuthority` fault with zero mutation — workflows do not
    ///   stack (§15.4).
    pub fn begin_nested_workflow(
        &mut self,
        context: &PlanContext<'_>,
        spec: &WireSpec,
    ) -> Result<PlannedStep, KernelFault> {
        if let Some(fault) = &self.poison {
            return Err(fault.clone());
        }
        let mut index = 0;
        let outcome = self
            .enter_nested_workflow(context, spec, &mut index)
            .map_err(|refusal| match refusal {
                SyscallRefusal::Fault(fault) => fault,
                // A direct caller has no observation channel, so the gate's denial becomes the
                // transition's refusal. Through the P1 path the same denial is an audit fact.
                SyscallRefusal::Rejected(rejected) => {
                    KernelFault::new(KernelFaultCode::ResourceLimitExceeded, rejected.reason)
                }
            })?;
        let step = PlannedStep {
            root_kind: Some(RootKind::Agent),
            focus: outcome.focus,
            observations: self
                .engine
                .as_mut()
                .map(LoopStateMachine::take_observations)
                .unwrap_or_default(),
            disposition: StepDisposition::Effects(EffectsDisposition {
                effects: outcome.effects,
            }),
        };
        self.staged = Some(StagedFocus {
            step_seq: context.step_seq,
            root_kind: step.root_kind,
            focus: step.focus.clone(),
        });
        Ok(step)
    }

    /// The body of [`Self::begin_nested_workflow`], without the staging — so a syscall batch that
    /// also carries other requests composes it instead of racing it for the staging slot.
    fn enter_nested_workflow(
        &mut self,
        context: &PlanContext<'_>,
        spec: &WireSpec,
        effect_index: &mut u32,
    ) -> Result<SyscallOutcome, SyscallRefusal> {
        let staged = self.staged.as_ref().map(|staged| staged.focus.clone());
        let focus = staged.as_ref().unwrap_or(&self.focus);
        let root_kind = self.root_kind;

        let parent_task_id = match (root_kind, focus) {
            (Some(RootKind::Agent), Some(ExecutionFocus::AgentTurn(turn))) => turn.task_id.clone(),
            (Some(RootKind::Agent), Some(ExecutionFocus::WorkflowController(_))) => {
                return Err(authority(
                    "a workflow is already the execution focus; workflows do not stack, so a \
                     second start request is refused with no spawn effect (§7.4 focus depth ≤ 1)",
                ));
            }
            (Some(RootKind::Workflow), _) => {
                return Err(authority(
                    "this operation's root is a workflow; its focus never moves, and a nested \
                     workflow start is not a transition it admits (§7.4)",
                ));
            }
            _ => {
                return Err(SyscallRefusal::Fault(KernelFault::new(
                    KernelFaultCode::InvalidLifecycle,
                    "no root has started, so there is no agent turn to suspend".to_string(),
                )));
            }
        };

        for node in &spec.nodes {
            self.require_known_contract(context.config, node.run_spec.as_ref())
                .map_err(|fault| {
                    SyscallRefusal::Rejected(SyscallRejection::new("start_workflow", fault.message))
                })?;
        }
        let core_spec = build_core_spec(spec).map_err(SyscallRefusal::Fault)?;
        let node_ids = wire_node_ids(spec);
        let workflow_id = mint_workflow_id(&context.input.operation_id, context.step_seq);
        self.require_effect_support(context.config, EffectKindTag::SpawnTasks)
            .map_err(SyscallRefusal::Fault)?;

        // §10.2 · the resource gate runs before the DAG is installed, so a denial commits with no
        // spawn effect at all rather than with a workflow the run cannot afford.
        let engine = self.engine_mut().map_err(SyscallRefusal::Fault)?;
        let disposition = engine.gate_syscall(&CoreSyscall::LoadWorkflow {
            node_count: spec.nodes.len(),
        });
        if !disposition.is_allowed() {
            return Err(SyscallRefusal::Rejected(SyscallRejection::new(
                "start_workflow",
                denial_reason(&disposition, "workflow authoring denied"),
            )));
        }

        // ----- past this line the semantic engine advances -----
        engine.set_root_workflow(false);
        let action = engine.load_workflow(core_spec);
        self.node_ids = node_ids;
        self.workflow_nodes = spec.nodes.clone();
        self.workflow_id = Some(workflow_id.clone());
        let disposition = self
            .disposition_for_at(context, action, RootKind::Agent, effect_index)
            .map_err(SyscallRefusal::Fault)?;
        let StepDisposition::Effects(effects) = disposition else {
            return Err(SyscallRefusal::Fault(KernelFault::new(
                KernelFaultCode::InvalidLifecycle,
                "entering a nested workflow cannot terminate the operation".to_string(),
            )));
        };
        Ok(SyscallOutcome {
            effects: effects.effects,
            focus: Some(ExecutionFocus::workflow_controller(
                workflow_id,
                Some(parent_task_id),
            )),
            needs_workflow_round: false,
        })
    }

    // ----- §7.6 · P1 syscalls and caller causation -----

    /// Derive the caller of every syscall a provider result carries.
    ///
    /// This is the whole of "the host does not declare a caller". The kernel already knows which
    /// provider effect it is resolving and which surface that call advertised; a `ProviderTool`
    /// causation is that knowledge written down. Three refusals, all before anything moves:
    ///
    /// * the effect is not one this driver published as a provider call — there is no turn to
    ///   attribute the request to;
    /// * the tool name was never exposed on that turn — a result may not invent a surface, and a
    ///   forged `start_workflow` in a run that never offered one dies here;
    /// * the call id already produced a syscall — a causation is spent once, so re-delivering the
    ///   same result under a fresh input id buys nothing.
    fn derive_provider_syscalls(
        &self,
        effect_id: &EffectId,
        calls: &[WireToolCall],
    ) -> Result<Vec<(SyscallCausation, WireToolCall)>, KernelFault> {
        let Some(pending) = self.provider_calls.get(effect_id) else {
            return Err(KernelFault::new(
                KernelFaultCode::InvalidAuthority,
                format!(
                    "effect {effect_id} is not a provider call this kernel published, so a tool \
                     call inside its result has no caller to derive (§7.6)"
                ),
            ));
        };
        let mut seen: BTreeSet<&str> = BTreeSet::new();
        let mut derived = Vec::with_capacity(calls.len());
        for call in calls {
            if !pending.exposed_tools.contains(&call.name) {
                return Err(KernelFault::new(
                    KernelFaultCode::InvalidAuthority,
                    format!(
                        "the turn behind effect {effect_id} exposed no tool named {:?}; a caller is \
                         derived from the surface the kernel published, never from the name a \
                         result carries (§7.6)",
                        call.name
                    ),
                ));
            }
            if self.consumed_calls.contains(call.call_id.as_str())
                || !seen.insert(call.call_id.as_str())
            {
                return Err(KernelFault::new(
                    KernelFaultCode::InvalidAuthority,
                    format!(
                        "call {} already produced a syscall; a causation is consumed once (§7.6)",
                        call.call_id
                    ),
                ));
            }
            derived.push((
                SyscallCausation::ProviderTool(ProviderToolCausation {
                    provider_effect_id: effect_id.clone(),
                    call_id: call.call_id.clone(),
                    task_id: pending.task_id.clone(),
                }),
                call.clone(),
            ));
        }
        Ok(derived)
    }

    /// §7.9 + §7.6 · one completed provider turn, whole.
    ///
    /// The batch a model returns can mix two populations the kernel must not confuse: **P1
    /// syscalls**, which the kernel adjudicates itself, and **host tool calls**, which it
    /// dispatches. Both halves happen here, in this order and for this reason:
    ///
    /// 1. the syscalls are adjudicated *before* the turn is fed, because they change what the next
    ///    rendered context contains — an activated skill, an edited plan, a grown DAG;
    /// 2. the assistant message is fed **whole**, tool calls included, so the model reads back the
    ///    turn it actually emitted; each syscall is closed with the kernel's own answer so every
    ///    call still has a result (the trained convention);
    /// 3. the continuation is the engine's, not this function's — [`LoopStateMachine::feed`] stays
    ///    the single place that decides what a provider turn means. All this reduction contributes
    ///    is *what the kernel already has outstanding*, which is the §5k question: a pure
    ///    control-plane batch publishes no effect, so without an answer the operation would stall.
    fn plan_provider_completed(
        &mut self,
        context: &PlanContext<'_>,
        effect_id: &EffectId,
        completed: &ProviderCompleted,
    ) -> Result<PlannedStep, KernelFault> {
        let root_kind = self.require_root_kind()?;
        self.require_pending_provider_call(effect_id)?;
        let syscalls: Vec<WireToolCall> = completed
            .message
            .tool_calls
            .iter()
            .filter(|call| is_syscall_tool(&call.name))
            .cloned()
            .collect();
        let derived = self.derive_provider_syscalls(effect_id, &syscalls)?;
        let message = core_provider_message(&completed.message)?;

        // ----- past this line the semantic engine advances -----
        let engine = self.engine_mut()?;
        if let Some(tokens) = completed.observed_input_tokens {
            engine.ctx.set_observed_prompt_tokens(tokens);
        }
        // §22.8 · the typed stop reason answers the one question the loop asks, so no vendor text
        // is classified anywhere on this path.
        engine.set_output_truncated(matches!(
            completed.stop_reason,
            Some(super::effect::ProviderStopReason::MaxTokens)
        ));

        let mut index = 0u32;
        let mut effects = Vec::new();
        let mut syscall_focus: Option<ExecutionFocus> = None;
        let mut answered: Vec<AnsweredCall> = Vec::with_capacity(derived.len());
        let mut needs_round = false;
        for (causation, call) in &derived {
            let outcome = match decode_syscall(call) {
                Ok(request) => self.apply_syscall(context, causation, &request, &mut index),
                Err(rejection) => Err(SyscallRefusal::Rejected(rejection)),
            };
            match outcome {
                Ok(outcome) => {
                    answered.push(AnsweredCall {
                        call_id: call.call_id.as_str().into(),
                        output: syscall_ack(&call.name).to_string(),
                        is_error: false,
                    });
                    effects.extend(outcome.effects);
                    if let Some(next) = outcome.focus {
                        syscall_focus = Some(next);
                    }
                    needs_round |= outcome.needs_workflow_round;
                }
                Err(SyscallRefusal::Fault(fault)) => return Err(fault),
                Err(SyscallRefusal::Rejected(rejection)) => {
                    answered.push(AnsweredCall {
                        call_id: call.call_id.as_str().into(),
                        output: rejection.reason.clone(),
                        is_error: true,
                    });
                    self.note_rejection(rejection.by(&causation_task(causation)))
                }
            }
        }
        // §10.3 · a batch that grew the DAG owes it a spawn round, and the author waits for the
        // work it just authored rather than taking another turn.
        let mut awaits_kernel_work = !effects.is_empty();
        if needs_round {
            awaits_kernel_work = true;
            let action = self.engine_mut()?.drive_workflow_round();
            self.extend_with_action(context, action, root_kind, &mut index, &mut effects)?;
        }

        let engine = self.engine_mut()?;
        engine.stage_adjudicated_turn(AdjudicatedTurn {
            answered_calls: answered,
            idle_continuation: if awaits_kernel_work {
                IdleContinuation::Await
            } else {
                IdleContinuation::CallProvider
            },
        });
        // The syscalls' own observations happened before `feed`, which clears the buffer at the
        // head of every event — carry them across, exactly as the child-completion path does.
        let syscall_observations = engine.take_observations();
        let action = engine.feed(LoopEvent::LLMResponse { message });
        let mut step = self.continue_after_at(context, action, root_kind, &mut index)?;
        if let Some(engine) = self.engine.as_mut() {
            engine.observations.splice(0..0, syscall_observations);
        }
        if syscall_focus.is_some() {
            step.focus = syscall_focus;
        }
        if !effects.is_empty() {
            match &mut step.disposition {
                StepDisposition::Effects(published) => {
                    let mut merged = effects;
                    merged.append(&mut published.effects);
                    published.effects = merged;
                }
                StepDisposition::Terminal(_) => {
                    return Err(KernelFault::new(
                        KernelFaultCode::InvalidLifecycle,
                        "a provider turn that terminates the operation cannot also publish the \
                         effects its syscalls asked for (§7.12)"
                            .to_string(),
                    ));
                }
            }
        }

        // The causation ledger is settled last, so a transition that ends in a fault does not leave
        // this driver claiming a spent call id and a resolved provider surface that the journal has
        // no record of. (The engine advance above is guarded the same way every other plan is — by
        // the staging slot, which fails closed on a plan that never commits.)
        self.provider_calls.remove(effect_id);
        for (_, call) in &derived {
            self.consumed_calls
                .insert(call.call_id.as_str().to_string());
        }
        Ok(step)
    }

    /// Apply one already-attributed request. Every arm reads its caller from `causation` and
    /// nothing else — there is no parameter through which a host could name a different one.
    fn apply_syscall(
        &mut self,
        context: &PlanContext<'_>,
        causation: &SyscallCausation,
        request: &SyscallRequest,
        effect_index: &mut u32,
    ) -> Result<SyscallOutcome, SyscallRefusal> {
        let caller = causation_task(causation);
        let quarantined = self
            .engine
            .as_ref()
            .is_some_and(|engine| engine.task_quarantined(caller.as_str()));
        if quarantined && let Some(family) = privileged_family(request) {
            // §7.6 · a quarantined task read untrusted content. Letting it grow the DAG, mutate its
            // capability surface or reach memory would make the untrusted content the author of the
            // escalation. Fails closed on the whole family rather than trusting per-request
            // coercion to be exhaustive.
            return Err(SyscallRefusal::Rejected(SyscallRejection::new(
                family,
                format!(
                    "quarantine: task {caller} is quarantined and may not widen its authority \
                     through a {family} syscall"
                ),
            )));
        }

        match request {
            SyscallRequest::SubmitWorkflow(submit) => {
                if submit.spec.nodes.is_empty() {
                    return Err(SyscallRefusal::Rejected(SyscallRejection::new(
                        "start_workflow",
                        "an authored workflow with no nodes has nothing to spawn",
                    )));
                }
                if self
                    .engine
                    .as_ref()
                    .is_some_and(LoopStateMachine::workflow_active)
                {
                    // §10.2 flatten: the caller is already inside a DAG, so its spec grows that DAG
                    // rather than stacking a second one. One root lifecycle, one quota.
                    self.append_nodes(
                        context.config,
                        &submit.spec.nodes,
                        &caller,
                        CoreSyscall::LoadWorkflow { node_count: 0 },
                        "start_workflow",
                    )
                } else {
                    self.enter_nested_workflow(context, &submit.spec, effect_index)
                }
            }
            SyscallRequest::AppendWorkflowNodes(append) => {
                if append.nodes.is_empty() {
                    return Err(SyscallRefusal::Rejected(SyscallRejection::new(
                        "submit_workflow_nodes",
                        "an empty submission appends nothing",
                    )));
                }
                if !self
                    .engine
                    .as_ref()
                    .is_some_and(LoopStateMachine::workflow_active)
                {
                    return Err(SyscallRefusal::Rejected(SyscallRejection::new(
                        "submit_workflow_nodes",
                        "no workflow is in flight, so there is no graph to append to",
                    )));
                }
                self.append_nodes(
                    context.config,
                    &append.nodes,
                    &caller,
                    CoreSyscall::SubmitNodes { count: 0 },
                    "submit_workflow_nodes",
                )
            }
            SyscallRequest::ActivateSkill(activate) => {
                let engine = self.engine_mut().map_err(SyscallRefusal::Fault)?;
                if !engine.ctx.skill_available(&activate.name) {
                    return Err(SyscallRefusal::Rejected(SyscallRejection::new(
                        "skill",
                        format!(
                            "this operation declares no skill named {:?}; activation is a \
                             capability mutation and is refused rather than invented",
                            activate.name
                        ),
                    )));
                }
                let expires_at_turn = activate
                    .lease_turns
                    .map(|turns| engine.turn.saturating_add(turns));
                engine
                    .ctx
                    .activate_skill_leased(activate.name.as_str(), expires_at_turn);
                Ok(SyscallOutcome::default())
            }
            SyscallRequest::UpdateTask(update) => {
                let engine = self.engine_mut().map_err(SyscallRefusal::Fault)?;
                engine.ctx.update_task(core_task_update(&update.update));
                Ok(SyscallOutcome::default())
            }
            SyscallRequest::RequestMemoryWrite(write) => {
                self.plan_memory_write(context, causation, &write.proposal, effect_index)
            }
            SyscallRequest::RequestMemoryQuery(query) => {
                self.plan_memory_query(context, causation, &query.query, effect_index)
            }
            SyscallRequest::PageIn(page_in) => {
                self.plan_page_in(context, &page_in.handle_id, effect_index)
            }
        }
    }

    /// §10.3 · gate + trust-aware append, with the spawn round deferred to the caller.
    fn append_nodes(
        &mut self,
        config: &ResolvedOperationConfig,
        nodes: &[WireNode],
        caller: &TaskId,
        syscall: CoreSyscall,
        label: &'static str,
    ) -> Result<SyscallOutcome, SyscallRefusal> {
        // §7.3 · an authored node may not name a contract the operation never declared. Same rule
        // as a root spec, and checked on the same side of the gate as identity/acyclicity: a batch
        // with a dangling reference is malformed and must not spend quota.
        for node in nodes {
            self.require_known_contract(config, node.run_spec.as_ref())
                .map_err(|fault| {
                    SyscallRefusal::Rejected(SyscallRejection::new(label, fault.message))
                })?;
        }
        // Batch-relative identity and acyclicity are checked before the gate sees a count, so a
        // malformed batch never spends quota.
        let core_nodes = build_core_spec(&WireSpec {
            name: String::new(),
            nodes: nodes.to_vec(),
        })
        .map_err(|fault| SyscallRefusal::Rejected(SyscallRejection::new(label, fault.message)))?
        .nodes;

        let engine = self.engine_mut().map_err(SyscallRefusal::Fault)?;
        let admitted = engine.append_workflow_nodes(
            core_nodes,
            // §7.6 · the submitter is the derived caller, never an optional host field. The
            // historical `submitter_agent_id: Option<String>` erred open — omitting it skipped the
            // quarantine coercion entirely — and there is no shape here that can omit it.
            Some(caller.as_str()),
            // `append_workflow_nodes` refills the count from the batch, so the two entry points
            // cannot disagree about what they meter.
            syscall,
            label,
        );
        if !admitted {
            // `append_workflow_nodes` already pushed the rejection observation and the model-facing
            // note; re-recording it here would double the audit fact.
            return Ok(SyscallOutcome::default());
        }
        // §10.3 · deterministic node identity is a kernel fact. An appended batch lands at the end
        // of the index-addressed DAG, so mirroring its wire ids here is what lets a later spawn
        // effect and the workflow terminal still name the node the caller declared — rather than
        // falling back to the internal `wf-nodeN` id.
        self.node_ids
            .extend(nodes.iter().map(|node| node.node_id.clone()));
        self.workflow_nodes.extend_from_slice(nodes);
        Ok(SyscallOutcome {
            effects: Vec::new(),
            focus: None,
            needs_workflow_round: true,
        })
    }

    fn plan_memory_write(
        &mut self,
        context: &PlanContext<'_>,
        causation: &SyscallCausation,
        proposal: &super::syscall::MemoryWriteProposal,
        effect_index: &mut u32,
    ) -> Result<SyscallOutcome, SyscallRefusal> {
        let Some(binding) = context.config.memory_access.clone() else {
            return Err(SyscallRefusal::Rejected(SyscallRejection::new(
                "write_memory",
                "this operation holds no memory binding, so it can author no memory record",
            )));
        };
        if !binding.capabilities.write {
            return Err(SyscallRefusal::Rejected(SyscallRejection::new(
                "write_memory",
                "this operation's memory binding is read-only",
            )));
        }
        self.require_effect_support(context.config, EffectKindTag::PersistMemory)
            .map_err(SyscallRefusal::Fault)?;
        let engine = self.engine_mut().map_err(SyscallRefusal::Fault)?;
        let disposition = engine.gate_memory_write_proposal();
        if !disposition.is_allowed() {
            return Err(SyscallRefusal::Rejected(SyscallRejection::new(
                "write_memory",
                denial_reason(&disposition, "memory write denied"),
            )));
        }
        // §22.13 · the proposal contributed name/kind/content/evidence and nothing else. Tenant,
        // author, trust, timestamp and provenance are authored here, from the operation's binding,
        // the envelope's accepted time and the derived causation.
        let authored = AuthoredMemoryWrite {
            binding_id: binding.binding_id.clone(),
            name: proposal.name.clone(),
            kind: proposal.kind,
            size_bytes: proposal.content.len() as u32,
        };
        let effect = EffectKind::PersistMemory(PersistMemoryEffect {
            binding,
            memory: CanonicalMemoryWrite {
                name: proposal.name.clone(),
                kind: proposal.kind,
                content: proposal.content.clone(),
                description: proposal.description.clone(),
                evidence_refs: proposal.evidence_refs.clone(),
                accepted_at_ms: context.input.observed_at_ms,
                causation: causation.clone(),
            },
        });
        let published = self.mint_effect(context, effect, effect_index);
        self.pending_memory_writes
            .insert(published.effect_id.clone(), authored);
        Ok(SyscallOutcome {
            effects: vec![published],
            focus: None,
            needs_workflow_round: false,
        })
    }

    fn plan_memory_query(
        &mut self,
        context: &PlanContext<'_>,
        causation: &SyscallCausation,
        proposal: &super::syscall::MemoryQueryProposal,
        effect_index: &mut u32,
    ) -> Result<SyscallOutcome, SyscallRefusal> {
        let Some(binding) = context.config.memory_access.clone() else {
            return Err(SyscallRefusal::Rejected(SyscallRejection::new(
                "query_memory",
                "this operation holds no memory binding, so it can read no memory record",
            )));
        };
        if !binding.capabilities.read {
            return Err(SyscallRefusal::Rejected(SyscallRejection::new(
                "query_memory",
                "this operation's memory binding is write-only",
            )));
        }
        self.require_effect_support(context.config, EffectKindTag::QueryMemory)
            .map_err(SyscallRefusal::Fault)?;
        // The retrieval width is the operation's policy, clamped — the host does not re-decide it
        // and a model cannot widen it by asking for more.
        let ceiling = context.config.memory_policy.retrieval_top_k;
        let requested_k = proposal.limit.unwrap_or(ceiling).clamp(1, ceiling);
        let authored = AuthoredMemoryQuery {
            binding_id: binding.binding_id.clone(),
            text: proposal.text.clone(),
            requested_k,
        };
        let effect = EffectKind::QueryMemory(QueryMemoryEffect {
            binding,
            query: CanonicalMemoryQuery {
                text: proposal.text.clone(),
                kinds: proposal.kinds.clone(),
                accepted_at_ms: context.input.observed_at_ms,
                causation: causation.clone(),
            },
            requested_k,
        });
        let published = self.mint_effect(context, effect, effect_index);
        self.pending_memory_queries
            .insert(published.effect_id.clone(), authored);
        Ok(SyscallOutcome {
            effects: vec![published],
            focus: None,
            needs_workflow_round: false,
        })
    }

    /// §7.6 / §7.10 rule 4 · `read_result` reduces to exactly one thing: a `LoadPayload` effect for
    /// a body the caller already holds an address for.
    ///
    /// Two refusals, both *rejections* rather than faults — the caller was established, so what is
    /// refused is the address it named, and the transition that carried it still commits with an
    /// audit fact the model reads on its next turn:
    ///
    /// - an address that is not in this operation's handle table. A page-in reaches only what the
    ///   caller already holds, which is what stops `read_result` from becoming a general read
    ///   primitive — the historical SDK answered it by scanning a spool directory and then the
    ///   session log, so any path-shaped string was a readable address.
    /// - an address whose body core still holds. `Resident` and `Collapsed` are not paged out at
    ///   all. Neither yields a locator the kernel could hand back,
    ///   and fabricating one is exactly the confusion the closed union removes.
    fn plan_page_in(
        &mut self,
        context: &PlanContext<'_>,
        handle_id: &super::scalar::HandleId,
        effect_index: &mut u32,
    ) -> Result<SyscallOutcome, SyscallRefusal> {
        let engine = self.engine_mut().map_err(SyscallRefusal::Fault)?;
        let Some(residency) = engine.ctx.payload_residency(handle_id.as_str()).cloned() else {
            return Err(SyscallRefusal::Rejected(SyscallRejection::new(
                READ_RESULT_TOOL_NAME,
                format!(
                    "handle {handle_id} is not reachable in this operation's handle table; a \
                     page-in addresses only what the caller already holds"
                ),
            )));
        };
        let (Some(payload_ref), Some(digest)) = (residency.payload_ref(), residency.digest())
        else {
            return Err(SyscallRefusal::Rejected(SyscallRejection::new(
                READ_RESULT_TOOL_NAME,
                format!(
                    "handle {handle_id} is {} — its body is held by this kernel, not by the \
                     payload store, so there is nothing to page in",
                    residency.label()
                ),
            )));
        };
        let payload_ref = PayloadRef::new(payload_ref).map_err(|error| {
            SyscallRefusal::Fault(KernelFault::new(
                KernelFaultCode::MalformedEnvelope,
                format!(
                    "handle {handle_id} records an unusable payload locator: {}",
                    error.message
                ),
            ))
        })?;
        self.require_effect_support(context.config, EffectKindTag::LoadPayload)
            .map_err(SyscallRefusal::Fault)?;
        let effect = EffectKind::LoadPayload(LoadPayloadEffect {
            handle_id: handle_id.clone(),
            payload_ref,
        });
        let published = self.mint_effect(context, effect, effect_index);
        self.pending_payload_loads.insert(
            published.effect_id.clone(),
            PendingPayloadLoad {
                handle_id: handle_id.as_str().to_string(),
                digest: digest.to_string(),
                original_size: match &residency {
                    Residency::External { original_size, .. } => Some(*original_size),
                    _ => None,
                },
            },
        );
        Ok(SyscallOutcome {
            effects: vec![published],
            focus: None,
            needs_workflow_round: false,
        })
    }

    /// Record a refused request as an audit fact the model reads on its next turn (§7.6, §7.7).
    fn note_rejection(&mut self, rejection: SyscallRejection) {
        let Some(engine) = self.engine.as_mut() else {
            return;
        };
        let note = crate::scheduler::rollback::build_control_rejection_note(
            rejection.operation,
            &rejection.reason,
            engine.ctx.config.verbose_control_notes,
        );
        engine.ctx.push_signal(note);
        let turn = engine.turn;
        engine
            .observations
            .push(KernelObservation::ControlRequestRejected {
                turn,
                operation: rejection.operation.to_string(),
                subject: rejection.subject,
                reason: rejection.reason,
            });
    }

    fn mint_effect(
        &self,
        context: &PlanContext<'_>,
        effect: EffectKind,
        effect_index: &mut u32,
    ) -> KernelEffect {
        let effect_id =
            mint_effect_id(&context.input.operation_id, context.step_seq, *effect_index);
        *effect_index += 1;
        KernelEffect {
            effect_id,
            causation_input_id: context.input.input_id.clone(),
            effect,
        }
    }

    /// Fold one engine action's effects into an accumulating step.
    fn extend_with_action(
        &mut self,
        context: &PlanContext<'_>,
        action: LoopAction,
        root_kind: RootKind,
        effect_index: &mut u32,
        effects: &mut Vec<KernelEffect>,
    ) -> Result<(), KernelFault> {
        match self.disposition_for_at(context, action, root_kind, effect_index)? {
            StepDisposition::Effects(published) => {
                effects.extend(published.effects);
                Ok(())
            }
            StepDisposition::Terminal(_) => Err(KernelFault::new(
                KernelFaultCode::InvalidLifecycle,
                "a syscall batch cannot terminate the operation; §7.12 admits effects or a \
                 terminal, never both in one step"
                    .to_string(),
            )),
        }
    }

    // ----- internals -----

    fn plan_inner(&mut self, context: &PlanContext<'_>) -> Result<PlannedStep, KernelFault> {
        // Every transition reads the observations *its own* semantic call produced. `start`/`feed`
        // clear the buffer themselves; `load_workflow` and `resolve_workflow_spawn` do not, so the
        // driver clears it here rather than letting a stale `WorkflowCompleted` from an earlier
        // step decide a later one's disposition.
        if let Some(engine) = self.engine.as_mut() {
            engine.take_observations();
            // §11.2 · the envelope's accepted time is this operation's only clock, and it is fed
            // once, here, before any semantic call. Every clock-dependent decision the step makes
            // (signal TTL and deadline escalation, rate-limit windows, the wall-time budget axis)
            // therefore reads a fact the journal already holds, so a replay decides identically.
            engine.observe_accepted_time(context.input.observed_at_ms.get());
        }
        match &context.input.input {
            NormalizedPayload::ConfigureOperation(configure) => {
                self.plan_configure(&configure.config)
            }
            NormalizedPayload::StartOperation(start) => {
                self.plan_start(context, &start.entry, &start.initial_context)
            }
            NormalizedPayload::ResolveEffect(resolve) => self.plan_resolve_effect(context, resolve),
            NormalizedPayload::DeliverExternalEvent(event) => {
                self.plan_external_event(context, &event.event)
            }
            NormalizedPayload::HostControl(control) => {
                self.plan_host_control(context, &control.command)
            }
        }
    }

    /// §6.1.2 · genesis. The engine is built from the **resolved** configuration the record froze,
    /// never from this binary's defaults, so a rebuild on a newer kernel plans the same step.
    fn plan_configure(
        &mut self,
        config: &ResolvedOperationConfig,
    ) -> Result<PlannedStep, KernelFault> {
        self.engine = Some(build_engine(config));
        // §13.2 · the live-mutable half starts at revision 0, holding exactly what the genesis
        // record froze. A patch rebases onto this, never onto a compile-time default.
        self.policy = Some(LivePolicyState::new(config.clone()));
        Ok(PlannedStep::quiet(None, None))
    }

    /// §7.4 · the one atomic root start.
    ///
    /// Both arms are ordered the same way and for the same reason: every refusal this transition
    /// can raise is decided while nothing has moved, and only then does the semantic engine
    /// advance. A rejected root start therefore leaves an operation that is still `Configured` and
    /// still free to choose a root.
    fn plan_start(
        &mut self,
        context: &PlanContext<'_>,
        entry: &RootEntry,
        initial: &InitialContext,
    ) -> Result<PlannedStep, KernelFault> {
        if self.root_kind.is_some() || self.staged.is_some() {
            return Err(KernelFault::new(
                KernelFaultCode::InvalidLifecycle,
                "this operation already has a root; a root entry is chosen once and is immutable \
                 (§6.1.3–6.1.5)"
                    .to_string(),
            ));
        }

        match entry {
            RootEntry::Agent(agent) => {
                self.require_effect_support(context.config, EffectKindTag::CallProvider)?;
                self.require_known_contract(context.config, agent.run_spec.as_ref())?;
                let task = runtime_task(&agent.task);
                let run_spec = agent.run_spec.as_ref().map(agent_run_spec);

                // ----- past this line the semantic engine advances -----
                // The cascade is installed before `start`, which is the engine's own precondition:
                // a contract loaded afterwards would leave phase 0 already behind the run.
                self.load_verification_contract(context.config, agent.run_spec.as_ref())?;
                let engine = self.engine_mut()?;
                seed_initial_context(engine, initial);
                engine.run_spec = run_spec;
                let action = engine.start(task);
                let disposition = self.disposition_for(context, action, RootKind::Agent)?;
                if !publishes(&disposition, EffectKindTag::CallProvider) {
                    return Err(KernelFault::new(
                        KernelFaultCode::InvalidLifecycle,
                        "an agent root's first committed step must publish a provider call (§7.4)"
                            .to_string(),
                    ));
                }
                Ok(PlannedStep {
                    root_kind: Some(RootKind::Agent),
                    focus: Some(ExecutionFocus::agent_turn(root_task_id())),
                    observations: Vec::new(),
                    disposition,
                })
            }
            RootEntry::Workflow(workflow) => {
                self.require_effect_support(context.config, EffectKindTag::SpawnTasks)?;
                for node in &workflow.spec.nodes {
                    self.require_known_contract(context.config, node.run_spec.as_ref())?;
                }
                if workflow.spec.nodes.is_empty() {
                    return Err(KernelFault::new(
                        KernelFaultCode::InvalidConfig,
                        "a workflow root with no nodes has no first task to spawn; a root entry \
                         must be able to publish its first effect (§10.1)"
                            .to_string(),
                    ));
                }
                let core_spec = build_core_spec(&workflow.spec)?;
                let node_ids = wire_node_ids(&workflow.spec);
                let workflow_id = mint_workflow_id(&context.input.operation_id, context.step_seq);

                // ----- past this line the semantic engine advances -----
                let engine = self.engine_mut()?;
                seed_initial_context(engine, initial);
                // §6.1.7 — this DAG *is* the root, so its completion is the operation's terminal
                // rather than one more turn of a parent agent loop.
                engine.set_root_workflow(true);
                let action = engine.load_workflow(core_spec);
                self.node_ids = node_ids;
                self.workflow_nodes = workflow.spec.nodes.clone();
                self.workflow_id = Some(workflow_id.clone());
                let disposition = self.disposition_for(context, action, RootKind::Workflow)?;
                if !publishes(&disposition, EffectKindTag::SpawnTasks) {
                    return Err(KernelFault::new(
                        KernelFaultCode::InvalidLifecycle,
                        "a workflow root's first committed step must publish a task spawn, never a \
                         provider call (§10.1)"
                            .to_string(),
                    ));
                }
                Ok(PlannedStep {
                    root_kind: Some(RootKind::Workflow),
                    focus: Some(ExecutionFocus::workflow_controller(workflow_id, None)),
                    observations: Vec::new(),
                    disposition,
                })
            }
        }
    }

    /// §7.9 · effect resolution — the single entry every pending effect is answered through.
    ///
    /// Two arms and no third, mirroring [`EffectOutcome`]. The transaction has already decided
    /// *admissibility* (still pending, kind matches, not a conflicting duplicate — §15.3), so what
    /// is left here is purely semantic: which internal mechanism the outcome feeds.
    fn plan_resolve_effect(
        &mut self,
        context: &PlanContext<'_>,
        resolve: &ResolveEffect,
    ) -> Result<PlannedStep, KernelFault> {
        match &resolve.outcome {
            EffectOutcome::Succeeded(success) => {
                self.plan_effect_success(context, &resolve.effect_id, &success.result)
            }
            EffectOutcome::Failed(failed) => {
                self.plan_effect_failure(context, &resolve.effect_id, &failed.failure)
            }
        }
    }

    /// The success half: each variant reduces onto the mechanism that already owns that fact.
    fn plan_effect_success(
        &mut self,
        context: &PlanContext<'_>,
        effect_id: &EffectId,
        success: &EffectSuccess,
    ) -> Result<PlannedStep, KernelFault> {
        match success {
            EffectSuccess::Provider(provider) => match &provider.outcome {
                ProviderOutcome::Completed(completed) => {
                    self.plan_provider_completed(context, effect_id, completed)
                }
                // §7.9 · a *semantic* outcome, not a transport failure: the kernel compacts and
                // re-emits a provider call, and no vendor text is read to decide that (§22.8).
                ProviderOutcome::ContextOverflow(overflow) => {
                    let root_kind = self.require_root_kind()?;
                    self.require_pending_provider_call(effect_id)?;
                    let engine = self.engine_mut()?;
                    if let Some(tokens) = overflow.observed_input_tokens {
                        engine.ctx.set_observed_prompt_tokens(tokens);
                    }
                    let action = engine.recover_from_context_overflow();
                    self.provider_calls.remove(effect_id);
                    self.continue_after(context, action, root_kind)
                }
            },
            EffectSuccess::Tools(tools) => {
                let root_kind = self.require_root_kind()?;
                // Zero-mutation discipline: the whole batch is adjudicated against the payload
                // policy before any of it reaches the engine, so a batch with one illegal result
                // does not half-land.
                for payload in &tools.results {
                    check_payload_policy(payload, &context.config.payload_policy)?;
                }
                let mut results: Vec<ToolResult> =
                    tools.results.iter().map(core_tool_result).collect();
                results.extend(self.close_out_fatal_batch(&tools.results)?);
                let mut action = self.engine_mut()?.feed(LoopEvent::ToolResults { results });
                self.record_external_payloads(&tools.results)?;
                self.engine_mut()?.refresh_call_llm_action(&mut action);
                self.continue_after(context, action, root_kind)
            }
            EffectSuccess::Approval(approval) => {
                let root_kind = self.require_root_kind()?;
                let approved = approval
                    .approved_call_ids
                    .iter()
                    .map(|id| id.as_str().to_string())
                    .collect();
                let denied = approval
                    .denied_call_ids
                    .iter()
                    .map(|id| id.as_str().to_string())
                    .collect();
                let action = self.engine_mut()?.resolve_approval(approved, denied);
                self.continue_after(context, action, root_kind)
            }
            EffectSuccess::TasksSpawned(spawned) => {
                let mut started: Vec<String> = Vec::new();
                let mut failures: Vec<WorkflowSpawnFailure> = Vec::new();
                for attempt in &spawned.attempts {
                    let agent_id = attempt.task_id.as_str().to_string();
                    match &attempt.outcome {
                        super::effect::TaskLaunchStatus::Started(_) => started.push(agent_id),
                        super::effect::TaskLaunchStatus::Failed(failed) => {
                            // §10.4 · a failed launch terminates the attempt. Dropping it here is
                            // what makes a later completion naming it a stale causation rather than
                            // a resurrection of a task that never started.
                            self.attempts.remove(&agent_id);
                            failures.push(WorkflowSpawnFailure {
                                agent_id,
                                error: failed.failure.message.clone(),
                            });
                        }
                    }
                }
                let root_kind = self.require_root_kind()?;
                let action = self.engine_mut()?.resolve_workflow_spawn(started, failures);
                self.continue_after(context, action, root_kind)
            }
            EffectSuccess::TasksPreempted(preempted) => {
                let root_kind = self.require_root_kind()?;
                // §10.4 · every attempt named here is spent, whichever way it went: a preempted
                // child is gone, and one that had already finished is finished. Either way a later
                // completion naming it is a stale causation.
                for attempt in &preempted.attempts {
                    self.attempts.remove(attempt.task_id.as_str());
                }
                let action = self.engine_mut()?.resolve_preempt();
                self.continue_after(context, action, root_kind)
            }
            EffectSuccess::MemoryPersisted(persisted) => {
                self.commit_memory_write(effect_id, Some(&persisted.receipt), None)
            }
            EffectSuccess::MemoryQueried(queried) => {
                let root_kind = self.require_root_kind()?;
                let Some(query) = self.pending_memory_queries.remove(effect_id) else {
                    return Err(unowned_resolution(effect_id, "memory query"));
                };
                let turn = self.engine_mut()?.turn;
                let mut recalled = Vec::with_capacity(queried.recalls.len());
                for recall in &queried.recalls {
                    // §22.13 · the host answers with records, never with authority: the recall is
                    // rendered into history as content the model reads, and nothing about it
                    // rewrites this operation's binding, trust or provenance.
                    let content = format!(
                        "[MEMORY record_ref={} kind={}] {}",
                        recall.record_ref,
                        wire_memory_kind_label(recall.kind),
                        recall.content
                    );
                    let engine = self.engine_mut()?;
                    let tokens = engine.ctx.engine.count(&content).max(1);
                    engine.ctx.push_history(Message::user(content), tokens);
                    recalled.push(recall.record_ref.as_str().to_string());
                }
                // The recalls are in history now, so the turn that asked for them resumes with a
                // rendered context that contains them. The observation is pushed *after* the
                // resume, which clears the buffer at its head: a fact recorded before it would be
                // erased by the very continuation it describes.
                let engine = self.engine_mut()?;
                let action = engine.resume_after_preload();
                engine.observations.push(KernelObservation::MemoryQueried {
                    turn,
                    scope: binding_scope(&query.binding_id),
                    query: query.text.clone(),
                    requested_k: query.requested_k as usize,
                    requires_async_response: false,
                });
                let mut step = self.continue_after(context, action, root_kind)?;
                step.focus = self.focus.clone();
                Ok(step)
            }
            EffectSuccess::PageOutArchived(archived) => {
                let root_kind = self.require_root_kind()?;
                self.verify_page_out_receipt(context, effect_id, &archived.receipt)?;
                let receipt = &archived.receipt;
                let action = self
                    .engine_mut()?
                    .commit_page_out_archive(Some(receipt.payload_ref.as_str().to_string()));
                // B19 · the archived body is `PagedOut`, not `External`: it *was* resident and left
                // under pressure. Both states page in through the same `LoadPayload` effect, and
                // keeping them apart is what lets a restore say which of the two happened.
                let engine = self.engine_mut()?;
                let turn = engine.turn;
                let previous = engine.ctx.set_payload_residency(
                    receipt.handle_id.as_str(),
                    HandleKind::MemoryPage,
                    0,
                    Residency::PagedOut {
                        payload_ref: receipt.payload_ref.as_str().to_string(),
                        digest: receipt.digest.as_str().to_string(),
                    },
                );
                engine
                    .observations
                    .push(KernelObservation::PayloadResidencyChanged {
                        turn,
                        handle_id: receipt.handle_id.as_str().to_string(),
                        from: previous.map(|residency| residency.label().to_string()),
                        to: "paged_out".to_string(),
                        payload_ref: Some(receipt.payload_ref.as_str().to_string()),
                        original_size: receipt.original_size.get(),
                    });
                self.continue_after(context, action, root_kind)
            }
            EffectSuccess::MilestoneEvaluated(evaluated) => {
                let root_kind = self.require_root_kind()?;
                let action = self.engine_mut()?.feed(LoopEvent::MilestoneResult {
                    result: core_milestone_result(&evaluated.result),
                });
                self.continue_after(context, action, root_kind)
            }
            EffectSuccess::PayloadLoaded(loaded) => {
                self.commit_payload_load(context, effect_id, loaded)
            }
        }
    }

    /// §7.10 · what a `fatal` disposition means on the batch that carried it.
    ///
    /// `fatal` is the host saying "the executor stopped here", so the calls this batch dispatched
    /// but never answered are not pending — they will never be answered at all. Left alone they
    /// would be orphan `tool_call`s: a provider replay with an assistant turn whose calls have no
    /// matching results, which is malformed on every vendor wire and is the exact failure mode the
    /// pairing repair in the SDKs exists to paper over.
    ///
    /// So the kernel closes them out, in the same feed, with the same shape the `ExecuteTools`
    /// *failure* arm uses for a batch that never ran: a committed, model-visible error result that
    /// says the call did not take effect. Same reasoning as v0.2.42's model-facing surface batch —
    /// the model adapts to a failure it can see and cannot adapt to an attempt that was erased.
    ///
    /// Returns an empty vector for an ordinary batch: no fatal result, nothing to close out.
    ///
    /// The fatality scan is **total over residency** (§7.10 rule 9): a call that failed fatally
    /// and spilled a large body is still a call that stopped the executor, so it stops the batch
    /// exactly as an inline one does. Reading only the inline arm would make the close-out depend
    /// on how big the failure's output happened to be.
    fn close_out_fatal_batch(
        &mut self,
        submitted: &[WireToolResultPayload],
    ) -> Result<Vec<ToolResult>, KernelFault> {
        let fatal = submitted
            .iter()
            .any(|payload| payload.disposition().is_fatal());
        if !fatal {
            return Ok(Vec::new());
        }
        let answered: BTreeSet<&str> = submitted
            .iter()
            .map(|payload| payload.call_id().as_str())
            .collect();
        Ok(self
            .engine_mut()?
            .dispatched_tool_calls()
            .iter()
            .filter(|call| !answered.contains(call.id.as_str()))
            .map(|call| ToolResult {
                call_id: call.id.clone(),
                output: Content::Text(
                    "not executed: an earlier call in this batch failed fatally and the executor \
                     stopped. This call did not take effect — re-issue it only if the failure it \
                     followed does not make it pointless."
                        .to_string(),
                ),
                is_error: true,
                is_fatal: false,
                error_kind: Some(ToolErrorKind::Fatal),
                token_count: None,
            })
            .collect())
    }

    /// The failure half (§7.9 · DEC-5).
    ///
    /// One policy decision per effect kind, taken **once**. The kernel never re-emits the same
    /// intent: a host that wants another attempt asks again with a new causation and keeps its own
    /// idempotency on the effect id / launch token. The kind comes from the effect the kernel
    /// itself published — `HostEffectFailure` is deliberately kind-agnostic (§7.9), so it is not,
    /// and must not be, the thing that selects the decision.
    fn plan_effect_failure(
        &mut self,
        context: &PlanContext<'_>,
        effect_id: &EffectId,
        failure: &HostEffectFailure,
    ) -> Result<PlannedStep, KernelFault> {
        let Some(pending) = context.resolving else {
            return Err(unowned_resolution(effect_id, "effect"));
        };
        let tag = pending.tag();
        match tag {
            // The loop cannot advance without a provider turn, and asking again would be the
            // redispatch DEC-5 deletes. Terminal.
            EffectKindTag::CallProvider => {
                self.provider_calls.remove(effect_id);
                self.host_effect_terminal(tag, failure)
            }
            // An unverifiable phase must not advance — that is the whole point of the gate — and a
            // second evaluation is the same intent. Terminal.
            EffectKindTag::EvaluateMilestone => self.host_effect_terminal(tag, failure),
            // The batch did not run. Answer every dispatched call with a visible error result and
            // let the model adapt: that is a *different* next request, not a retry of this one.
            EffectKindTag::ExecuteTools => {
                let root_kind = self.require_root_kind()?;
                let engine = self.engine_mut()?;
                let results = engine
                    .dispatched_tool_calls()
                    .iter()
                    .map(|call| ToolResult {
                        call_id: call.id.clone(),
                        output: Content::Text(format!(
                            "not executed: the executor could not run this batch ({}). The call \
                             did not take effect — try a different approach or a smaller step.",
                            failure.kind.as_str()
                        )),
                        is_error: true,
                        is_fatal: false,
                        error_kind: Some(ToolErrorKind::Fatal),
                        token_count: None,
                    })
                    .collect();
                let action = engine.feed(LoopEvent::ToolResults { results });
                self.continue_after(context, action, root_kind)
            }
            // Fail closed: no approval was obtained, so nothing gated is approved. `resolve_approval`
            // with an empty verdict denies exactly the gated calls and resumes the rest.
            EffectKindTag::RequestApproval => {
                let root_kind = self.require_root_kind()?;
                let engine = self.engine_mut()?;
                let turn = engine.turn;
                // Pushed after the resolution, which clears the buffer at its head: a fact
                // recorded before it would be erased by the continuation it describes.
                let action = engine.resolve_approval(Vec::new(), Vec::new());
                engine
                    .observations
                    .push(KernelObservation::ApprovalResolutionFailed {
                        turn,
                        error: host_failure_text(failure),
                    });
                self.continue_after(context, action, root_kind)
            }
            // No task in the batch started. Charge the failure against exactly the batch the kernel
            // published and let the DAG's own dependency policy decide what that starves.
            EffectKindTag::SpawnTasks => {
                let root_kind = self.require_root_kind()?;
                let error = host_failure_text(failure);
                let engine = self.engine_mut()?;
                let failures: Vec<WorkflowSpawnFailure> = engine
                    .pending_spawn_agent_ids()
                    .into_iter()
                    .map(|agent_id| WorkflowSpawnFailure {
                        agent_id,
                        error: error.clone(),
                    })
                    .collect();
                for failed in &failures {
                    self.attempts.remove(&failed.agent_id);
                }
                let action = self
                    .engine_mut()?
                    .resolve_workflow_spawn(Vec::new(), failures);
                self.continue_after(context, action, root_kind)
            }
            // The children were not stopped. Record it and resume: re-issuing the preemption is the
            // unbounded `retry_preempt` loop DEC-5 deletes.
            EffectKindTag::PreemptTasks => {
                // for the refusal, not for the value: a resolution with no root is not a resolution
                self.require_root_kind()?;
                let error = host_failure_text(failure);
                let engine = self.engine_mut()?;
                let turn = engine.turn;
                let agent_ids: Vec<String> = match &pending.effect {
                    EffectKind::PreemptTasks(preempt) => preempt
                        .attempts
                        .iter()
                        .map(|attempt| attempt.task_id.as_str().to_string())
                        .collect(),
                    _ => Vec::new(),
                };
                engine
                    .observations
                    .push(KernelObservation::AgentPreemptFailed {
                        turn,
                        agent_ids,
                        reason: match &pending.effect {
                            EffectKind::PreemptTasks(preempt) => preempt.reason.clone(),
                            _ => String::new(),
                        },
                        error,
                    });
                Ok(self.quiet_step())
            }
            EffectKindTag::PersistMemory => {
                self.commit_memory_write(effect_id, None, Some(host_failure_text(failure)))
            }
            // The recall did not happen. The turn that asked for it resumes without it — a memory
            // search that found nothing and a memory store that was unreachable are the same shape
            // to the model, and neither is worth stalling the run for.
            EffectKindTag::QueryMemory => {
                let root_kind = self.require_root_kind()?;
                let Some(query) = self.pending_memory_queries.remove(effect_id) else {
                    return Err(unowned_resolution(effect_id, "memory query"));
                };
                let error = host_failure_text(failure);
                let engine = self.engine_mut()?;
                let turn = engine.turn;
                let action = engine.resume_after_preload();
                engine
                    .observations
                    .push(KernelObservation::MemoryQueryFailed {
                        turn,
                        scope: binding_scope(&query.binding_id),
                        query: query.text,
                        error,
                    });
                let mut step = self.continue_after(context, action, root_kind)?;
                step.focus = self.focus.clone();
                Ok(step)
            }
            // Abandon the archive: its compaction already happened in this kernel, so the run stays
            // live and degraded rather than dying on a best-effort durability effect.
            EffectKindTag::ArchivePageOut => {
                let root_kind = self.require_root_kind()?;
                let action = self
                    .engine_mut()?
                    .abandon_page_out_archive(host_failure_text(failure));
                self.continue_after(context, action, root_kind)
            }
            // DEC-5 · abandon the read. A body the host cannot produce leaves the operation exactly
            // where it was — the preview is still in context and the handle still names the
            // reference — so the model is told the read failed and takes its next turn, rather than
            // the kernel re-issuing the same load or killing a live run over one page-in.
            EffectKindTag::LoadPayload => {
                let root_kind = self.require_root_kind()?;
                let handle_id = self
                    .pending_payload_loads
                    .remove(effect_id)
                    .map(|pending| pending.handle_id)
                    .unwrap_or_default();
                let error = host_failure_text(failure);
                let engine = self.engine_mut()?;
                let turn = engine.turn;
                let action = engine.resume_after_preload();
                engine
                    .observations
                    .push(KernelObservation::PayloadLoadFailed {
                        turn,
                        handle_id,
                        error,
                    });
                let mut step = self.continue_after(context, action, root_kind)?;
                step.focus = self.focus.clone();
                Ok(step)
            }
        }
    }

    /// The two effect kinds whose absence makes the operation unsound rather than degraded.
    fn host_effect_terminal(
        &mut self,
        tag: EffectKindTag,
        failure: &HostEffectFailure,
    ) -> Result<PlannedStep, KernelFault> {
        let root_kind = self.require_root_kind()?;
        let usage = self.usage_report();
        // The engine stops too: the operation is over, and a later input must not find a loop that
        // still believes it is running.
        if let Some(engine) = self.engine.as_mut() {
            engine.close_for_host_effect_failure();
        }
        Ok(PlannedStep {
            root_kind: Some(root_kind),
            focus: self.focus.clone(),
            observations: Vec::new(),
            disposition: StepDisposition::Terminal(TerminalDisposition {
                terminal: KernelTerminal::Failed(FailedTerminal {
                    failure: KernelFailure {
                        code: KernelFailureCode::HostEffectFailed,
                        message: format!(
                            "the host could not execute this operation's {tag} effect ({}){}",
                            failure.kind.as_str(),
                            if failure.message.is_empty() {
                                String::new()
                            } else {
                                format!(": {}", failure.message)
                            }
                        ),
                    },
                    usage,
                }),
            }),
        })
    }

    /// A transition that changes kernel state but publishes nothing.
    ///
    /// Reads the folded root kind rather than taking one: a control command is admissible while the
    /// operation is still only `Configured`, so the value it reports may legitimately be `None`,
    /// and every other caller already proved a root exists through `require_root_kind`.
    fn quiet_step(&self) -> PlannedStep {
        PlannedStep {
            root_kind: self.root_kind,
            focus: self.focus.clone(),
            observations: Vec::new(),
            disposition: StepDisposition::Effects(EffectsDisposition::default()),
        }
    }

    /// §22.13 · the memory write resolution. The record the kernel authored is the record; the
    /// host receipt contributes only its own opaque locator and digest, and never a name, kind,
    /// size, trust or provenance the kernel did not derive.
    fn commit_memory_write(
        &mut self,
        effect_id: &EffectId,
        receipt: Option<&super::effect::MemoryPersistReceipt>,
        failure: Option<String>,
    ) -> Result<PlannedStep, KernelFault> {
        // for the refusal, not for the value: a resolution with no root is not a resolution
        self.require_root_kind()?;
        let Some(authored) = self.pending_memory_writes.remove(effect_id) else {
            return Err(unowned_resolution(effect_id, "memory write"));
        };
        let engine = self.engine_mut()?;
        let turn = engine.turn;
        match (receipt, failure) {
            (Some(receipt), _) => {
                engine.observations.push(KernelObservation::MemoryWritten {
                    turn,
                    record_id: receipt.record_ref.as_str().to_string(),
                    scope: binding_scope(&authored.binding_id),
                    memory_kind: core_memory_kind(authored.kind),
                    name: authored.name,
                    size_bytes: authored.size_bytes,
                });
            }
            (None, Some(error)) => {
                engine
                    .observations
                    .push(KernelObservation::MemoryWriteFailed {
                        turn,
                        // No record exists to name, so the audit fact names the *intent* — the
                        // kernel-authored key — instead of a host id it never received.
                        record_id: authored.name,
                        error,
                    });
            }
            (None, None) => unreachable!("a memory resolution is either a receipt or a failure"),
        }
        Ok(self.quiet_step())
    }

    /// §7.10 rule 4 · a body the host paged back in.
    ///
    /// Three things are checked before a byte enters context, and all three are the same question
    /// asked from different sides: *is this the body that left?* The effect must be one this kernel
    /// published, the outcome must name the handle that effect addressed, and the content must
    /// reproduce the digest the residency recorded. The kernel never saw the body, so the digest is
    /// the only evidence there is — which is why a mismatch is an
    /// [`UnexpectedEffectOutcome`](KernelFaultCode::UnexpectedEffectOutcome) with zero mutation and
    /// not a degraded read.
    fn commit_payload_load(
        &mut self,
        context: &PlanContext<'_>,
        effect_id: &EffectId,
        loaded: &super::effect::PayloadLoadedSuccess,
    ) -> Result<PlannedStep, KernelFault> {
        let root_kind = self.require_root_kind()?;
        let Some(pending) = self.pending_payload_loads.get(effect_id).cloned() else {
            return Err(unowned_resolution(effect_id, "payload load"));
        };
        let mismatch = |what: &str| {
            Err(KernelFault::new(
                KernelFaultCode::UnexpectedEffectOutcome,
                format!(
                    "the payload loaded for effect {effect_id} is not the body the kernel paged \
                     out: {what}"
                ),
            ))
        };
        if loaded.handle_id.as_str() != pending.handle_id {
            return mismatch(&format!(
                "it names handle {}, but the effect addressed {}",
                loaded.handle_id, pending.handle_id
            ));
        }
        let content = loaded.payload.content.as_str();
        if loaded.payload.original_size.get() != content.len() as u64 {
            return mismatch(&format!(
                "it declares {} bytes and carries {}",
                loaded.payload.original_size,
                content.len()
            ));
        }
        if let Some(original_size) = pending.original_size
            && loaded.payload.original_size.get() != original_size
        {
            return mismatch(&format!(
                "it carries {} bytes and the handle records {original_size}",
                loaded.payload.original_size
            ));
        }
        let digest = super::record::canonical_digest(content.as_bytes());
        if digest.as_str() != pending.digest {
            return mismatch(&format!(
                "its content digests to {digest}, and the handle records {}",
                pending.digest
            ));
        }

        // ----- past this line the semantic engine advances -----
        self.pending_payload_loads.remove(effect_id);
        let engine = self.engine_mut()?;
        let turn = engine.turn;
        // The body enters as its own unit of history, exactly as a memory recall does: the preview
        // that stands in for it is left untouched, so nothing that was already rendered is
        // rewritten and the model reads the page-in as the answer to the read it asked for.
        let body = format!("[PAYLOAD handle_id={}]\n{content}", pending.handle_id);
        let tokens = engine.ctx.engine.count(&body).max(1);
        engine.ctx.push_history(Message::user(body), tokens);
        let previous = engine.ctx.set_payload_residency(
            &pending.handle_id,
            HandleKind::ToolResult,
            tokens,
            Residency::Resident,
        );
        let action = engine.resume_after_preload();
        engine
            .observations
            .push(KernelObservation::PayloadResidencyChanged {
                turn,
                handle_id: pending.handle_id.clone(),
                from: previous.map(|residency| residency.label().to_string()),
                to: "resident".to_string(),
                payload_ref: None,
                original_size: loaded.payload.original_size.get(),
            });
        let mut step = self.continue_after(context, action, root_kind)?;
        step.focus = self.focus.clone();
        Ok(step)
    }

    /// §7.10 rule 3 / §25.9 · move each external result's P3 handle onto the reference the host
    /// supplied, and record the transfer as a fact.
    ///
    /// Runs **after** the engine accepted the batch, because the handle this moves is minted by the
    /// engine as the result enters history — there is nothing to address before that. What lands in
    /// context is the preview; what the handle now says is where the body actually is.
    fn record_external_payloads(
        &mut self,
        payloads: &[WireToolResultPayload],
    ) -> Result<(), KernelFault> {
        for payload in payloads {
            let WireToolResultPayload::External(external) = payload else {
                continue;
            };
            let engine = self.engine_mut()?;
            let turn = engine.turn;
            let previous = engine.ctx.set_payload_residency(
                external.call_id.as_str(),
                HandleKind::ToolResult,
                // The body was never resident: only the preview is, and it is the anchored
                // message's own weight, not this handle's.
                0,
                Residency::External {
                    payload_ref: external.payload_ref.as_str().to_string(),
                    digest: external.digest.as_str().to_string(),
                    original_size: external.original_size.get(),
                },
            );
            engine
                .observations
                .push(KernelObservation::PayloadResidencyChanged {
                    turn,
                    handle_id: external.call_id.as_str().to_string(),
                    from: previous.map(|residency| residency.label().to_string()),
                    to: "external".to_string(),
                    payload_ref: Some(external.payload_ref.as_str().to_string()),
                    original_size: external.original_size.get(),
                });
        }
        Ok(())
    }

    /// The page-out receipt must describe the body the kernel handed over. A host that answers with
    /// a different handle or digest has archived something else, and accepting it would make a
    /// later page-in restore content this operation never evicted.
    fn verify_page_out_receipt(
        &self,
        context: &PlanContext<'_>,
        effect_id: &EffectId,
        receipt: &super::effect::ArchiveReceipt,
    ) -> Result<(), KernelFault> {
        let Some(KernelEffect {
            effect: EffectKind::ArchivePageOut(published),
            ..
        }) = context.resolving
        else {
            return Err(unowned_resolution(effect_id, "page-out archive"));
        };
        if receipt.handle_id != published.handle_id
            || receipt.digest != published.payload.digest
            || receipt.original_size != published.payload.original_size
        {
            return Err(KernelFault::new(
                KernelFaultCode::UnexpectedEffectOutcome,
                format!(
                    "the archive receipt for effect {effect_id} names handle {} / digest {}, but \
                     the kernel published handle {} / digest {}",
                    receipt.handle_id,
                    receipt.digest,
                    published.handle_id,
                    published.payload.digest
                ),
            ));
        }
        Ok(())
    }

    /// Build the page-out effect for one compaction's archived body.
    fn page_out_effect(
        &self,
        context: &PlanContext<'_>,
        summary: Option<&str>,
        archived: &[Message],
        effect_index: u32,
    ) -> Result<ArchivePageOutEffect, KernelFault> {
        let content = serde_json::to_string(archived).map_err(|error| {
            KernelFault::new(
                KernelFaultCode::MalformedEnvelope,
                format!("archived history is not serialisable: {error}"),
            )
        })?;
        let preview_bytes = context.config.payload_policy.preview_bytes as usize;
        let preview = summary
            .map(str::to_string)
            .unwrap_or_else(|| truncate_on_char_boundary(&content, preview_bytes));
        let handle_id = super::scalar::HandleId::new(format!(
            "{}:step:{}:page-out:{effect_index}",
            context.input.operation_id, context.step_seq
        ))
        .map_err(malformed)?;
        Ok(ArchivePageOutEffect {
            handle_id,
            payload: PageOutPayload {
                digest: super::record::canonical_digest(content.as_bytes()),
                original_size: WireU64::new(content.len() as u64),
                content,
                preview,
            },
        })
    }

    fn require_pending_provider_call(
        &self,
        effect_id: &EffectId,
    ) -> Result<&PendingProviderCall, KernelFault> {
        self.provider_calls.get(effect_id).ok_or_else(|| {
            KernelFault::new(
                KernelFaultCode::InvalidAuthority,
                format!(
                    "effect {effect_id} is not a provider call this kernel published, so its \
                     result has no turn to continue (§7.6)"
                ),
            )
        })
    }

    /// §7.7 · host-observed facts. Two arms and no third: a signal the host observed, and a child
    /// attempt that finished.
    fn plan_external_event(
        &mut self,
        context: &PlanContext<'_>,
        event: &ExternalEvent,
    ) -> Result<PlannedStep, KernelFault> {
        match event {
            ExternalEvent::DeliverSignal(delivery) => self.plan_signal(context, delivery),
            ExternalEvent::ChildCompleted(completed) => {
                self.plan_child_completed(context, completed)
            }
        }
    }

    /// §7.7 · one signal delivery.
    ///
    /// Everything decidable is decided before the router is touched, so a refused delivery is a
    /// genuine zero-mutation rejection:
    ///
    /// * a delivery needs a root to interrupt — a signal to an operation that has not started has
    ///   no attention to compete for;
    /// * `attempt` is 1-based: attempt 0 is a host that did not count its own redelivery, and the
    ///   delivery/attempt pair is the only thing that makes "one signal delivered three times"
    ///   distinguishable from "three signals" (§14.1);
    /// * a task target must name a task this kernel issued and that is still live. `SignalTarget`
    ///   is the kernel's own address space — a host session, process or thread is not a target and
    ///   has no representation on the wire (§5.2).
    ///
    /// Admission itself (TTL sweep, dedupe, deadline escalation, queue displacement) belongs to the
    /// router and runs on the **envelope's accepted time**, already fed in `plan_inner`. The
    /// signal's own `source_timestamp_ms` is audit metadata and never a clock (§11.2).
    ///
    /// The disposition is a *fact*: queueing, dropping, expiring and displacing produce
    /// observations and no effect. The one host action a signal can cause is preempting running
    /// children, and that is published as a `PreemptTasks` effect through the ordinary
    /// action-to-disposition path — committed only when its resolution comes back (§7.8).
    fn plan_signal(
        &mut self,
        context: &PlanContext<'_>,
        delivery: &DeliverSignal,
    ) -> Result<PlannedStep, KernelFault> {
        let root_kind = self.require_root_kind()?;
        if delivery.attempt == 0 {
            return Err(KernelFault::new(
                KernelFaultCode::MalformedEnvelope,
                format!(
                    "delivery {} carries attempt 0; attempts are 1-based, and a delivery that \
                     cannot say which attempt it is cannot be told apart from a redelivery (§7.7)",
                    delivery.delivery_id
                ),
            ));
        }
        if let SignalTarget::Task(target) = &delivery.signal.target {
            let live = self
                .engine
                .as_ref()
                .and_then(|engine| engine.task_lifecycle(target.task_id.as_str()))
                .is_some_and(|lifecycle| !lifecycle.is_terminal());
            if !live {
                return Err(KernelFault::new(
                    KernelFaultCode::InvalidAuthority,
                    format!(
                        "signal {} targets task {}, which this operation has no live attempt for; \
                         a signal addresses the operation or one of its own logical tasks (§7.7)",
                        delivery.signal.signal_id, target.task_id
                    ),
                ));
            }
        }
        // DEC-8 · the one effect a delivery can publish is the preemption of running children, and
        // that is reachable only for a critical signal while this kernel holds live attempts.
        // Checked here, before the router moves, so a host that cannot stop its children refuses
        // the delivery instead of planning an effect it could never execute.
        //
        // The urgency read is the **effective** one: a signal carrying `escalate_after_ms: 0`
        // becomes critical at admission when the operation enabled deadline escalation, so reading
        // the bare wire value would let it reach the router and only then discover the host has no
        // preemption path. A deadline that comes due *later* escalates inside the queue, and that
        // path is covered by the effect-minting `require_effect_support` — a fault rather than a
        // zero-mutation refusal, which is the correct asymmetry: at admission nothing has moved.
        let escalation_enabled = context.config.signal_policy.deadline_escalation;
        if delivery.signal.effective_urgency(escalation_enabled) == SignalUrgency::Critical
            && !self.attempts.is_empty()
        {
            self.require_effect_support(context.config, EffectKindTag::PreemptTasks)?;
        }

        // ----- past this line the semantic engine advances -----
        // DEC-3 · at most one pending effect per kind, so a signal may only force a fresh provider
        // request when this operation is not already waiting on one.
        let may_issue_request = self.provider_calls.is_empty();
        let signal = runtime_signal(&delivery.signal, context.input.observed_at_ms);
        let engine = self.engine_mut()?;
        let action = engine.signal_event(
            context.input.operation_id.as_str().to_string(),
            delivery.delivery_id.as_str().to_string(),
            delivery.attempt,
            signal,
            may_issue_request,
        );
        match action {
            Some(action) => self.continue_after(context, action, root_kind),
            // Queued / observed / ignored / dropped: the disposition observation is the whole of
            // what happened, and the host is asked for nothing.
            None => Ok(self.quiet_step()),
        }
    }

    /// §7.7 · a child completion is the event that drains a workflow DAG.
    fn plan_child_completed(
        &mut self,
        context: &PlanContext<'_>,
        completed: &ChildCompleted,
    ) -> Result<PlannedStep, KernelFault> {
        let root_kind = self.require_root_kind()?;
        match self.attempts.get(completed.task_id.as_str()) {
            Some(minted) if minted == &completed.attempt_id => {}
            issued => {
                return Err(KernelFault::new(
                    KernelFaultCode::InvalidAuthority,
                    format!(
                        "task {} has attempt {} in this kernel, but the completion names {}; a \
                         host does not mint or rewrite child identity (§10.4)",
                        completed.task_id,
                        issued.map_or("none", AttemptId::as_str),
                        completed.attempt_id,
                    ),
                ));
            }
        }

        // ----- past this line the semantic engine advances -----
        // §10.4 · the attempt is spent. A second completion naming it — and any `parent_requests`
        // riding on that second completion — is a stale causation, refused by the check above.
        self.attempts.remove(completed.task_id.as_str());

        // §7.7 · the requests enter P1 first, with `ChildAttempt` causation, and the completion is
        // fed afterwards so its own drive produces the single next ready batch.
        //
        // GAP-4: this loop can neither fail the transition nor skip the completion. Each request is
        // adjudicated on its own; a refused one leaves a structured rejection observation and
        // changes neither its siblings nor the fact that the child ran.
        //
        // No arm can move the focus here, which is why only `effects` is collected: the sole
        // focus-moving syscall is `SubmitWorkflow`'s *bootstrap*, and a child attempt only exists
        // while its DAG is in flight — so that request always takes the flatten arm instead.
        let mut index = 0u32;
        let mut effects = Vec::new();
        for (seq, request) in completed.parent_requests.iter().enumerate() {
            let causation = SyscallCausation::ChildAttempt(ChildAttemptCausation {
                task_id: completed.task_id.clone(),
                attempt_id: completed.attempt_id.clone(),
                request_seq: seq as u32,
            });
            match self.apply_syscall(context, &causation, request, &mut index) {
                Ok(outcome) => effects.extend(outcome.effects),
                Err(SyscallRefusal::Fault(fault)) => self.note_rejection(
                    SyscallRejection::new(
                        "parent_request",
                        format!("request {seq} refused: {}", fault.message),
                    )
                    .by(&completed.task_id),
                ),
                Err(SyscallRefusal::Rejected(rejection)) => {
                    self.note_rejection(rejection.by(&completed.task_id))
                }
            }
        }

        // `feed` clears the observation buffer at the head of every event, and these facts happened
        // *before* it. Carrying them across is what keeps the audit trail of a refused parent
        // request from being erased by the very completion it rode in on.
        let syscall_observations = self
            .engine_mut()?
            .take_observations()
            .into_iter()
            .collect::<Vec<_>>();

        let result = sub_agent_result(completed);
        let engine = self.engine_mut()?;
        let action = engine.feed(LoopEvent::SubAgentCompleted { result });
        let mut step = self.continue_after_at(context, action, root_kind, &mut index)?;
        if let Some(engine) = self.engine.as_mut() {
            engine.observations.splice(0..0, syscall_observations);
        }
        if !effects.is_empty() {
            match &mut step.disposition {
                StepDisposition::Effects(published) => {
                    let mut merged = effects;
                    merged.append(&mut published.effects);
                    published.effects = merged;
                }
                StepDisposition::Terminal(_) => {
                    return Err(KernelFault::new(
                        KernelFaultCode::InvalidLifecycle,
                        "a completion that terminates the operation cannot also publish the \
                         effects its parent requests asked for (§7.12)"
                            .to_string(),
                    ));
                }
            }
        }
        Ok(step)
    }

    // ----- §7.5 · the live control plane -----

    /// §7.5 · reduce one host command onto the mechanism that already owns that state.
    ///
    /// Every arm here carries **host authority**, which is what separates this from the P1 syscall
    /// path in [`Self::apply_syscall`]: a host command is not gated, not quarantine-coerced and not
    /// attributed to a caller, because the host *is* the authority. `UpdateTask` is the pair that
    /// makes the split visible — the same `TaskUpdate` payload, two input classes, two authorities,
    /// preserving the authority distinction that the retired shared task-update input could not
    /// express (§7.5 现状注记).
    ///
    /// Refusals are faults rather than model-facing rejections: a control command is a host bug, so
    /// it is answered to the host and never becomes a note the model reads.
    fn plan_host_control(
        &mut self,
        context: &PlanContext<'_>,
        command: &HostCommand,
    ) -> Result<PlannedStep, KernelFault> {
        match command {
            HostCommand::Cancel(cancel) => self.plan_cancel(context, cancel),
            HostCommand::ForceCompact(_) => {
                let root_kind = self.require_root_kind()?;
                self.engine_mut()?.force_compact();
                // A compaction that archived history owes a `page_out` effect; `continue_after`
                // externalises it through the same one path an in-turn compaction takes.
                self.continue_after(context, LoopAction::AwaitingResume, root_kind)
            }
            HostCommand::UpdateTask(update) => self.plan_host_task_update(update),
            HostCommand::ApplyCapabilityPatch(patch) => self.plan_capability_patch(patch),
            HostCommand::ApplyKnowledgeMutation(mutation) => self.plan_knowledge_mutation(mutation),
            HostCommand::SeedKnowledge(seed) => self.plan_seed_knowledge(seed),
            HostCommand::ApplySkillActivation(activation) => self.plan_skill_activation(activation),
            HostCommand::ApplyPolicyPatch(patch) => self.plan_policy_patch(patch),
            HostCommand::UpdateDeadline(deadline) => self.plan_update_deadline(deadline),
        }
    }

    /// §11.1 · the cancellation ladder, and the only path an operation is cancelled through.
    ///
    /// The order is downstream-first and it happens **inside one transition**, because §7.12 admits
    /// effects or a terminal and never both:
    ///
    /// 1. every child attempt this kernel issued is settled and its wait torn down, every pending
    ///    workflow batch and deferred host effect dropped (`cancel_operation`);
    /// 2. the driver's own ledger of live attempts and pending calls is spent with them, so a late
    ///    completion or resolution naming one is a stale causation rather than a resurrection;
    /// 3. only then is the root terminal minted.
    ///
    /// The *real* I/O stop is the host's: §11.1 has the host stop provider, tool and child I/O
    /// before it submits this command, and the kernel adjudicates the terminal. That is why this
    /// step publishes no `PreemptTasks` effect — asking the host to stop what it already stopped
    /// would need a second transition, and the operation would not be cancelled until it came back.
    ///
    /// The reason is the host's, not the loop's: `cancel_operation` terminates the semantic loop
    /// with its own internal `UserAbort`, but a `Deadline` or `HostShutdown` cancellation must not
    /// be reported as a user abort, so the terminal is built from the command.
    fn plan_cancel(
        &mut self,
        context: &PlanContext<'_>,
        cancel: &CancelCommand,
    ) -> Result<PlannedStep, KernelFault> {
        let root_kind = self.root_kind;
        let focus = self.focus.clone();
        let reason = cancel.reason;
        // §7.5 · the cancel command carries no operation id of its own; the envelope owns it.
        let operation_id = context.input.operation_id.as_str().to_string();
        let engine = self.engine_mut()?;
        let action = engine.cancel_operation(
            operation_id,
            reason,
            cancel
                .pending_call_ids
                .iter()
                .map(|call_id| call_id.as_str().to_string())
                .collect(),
        );
        // Downstream identity is spent in the same transition that settles the tasks it names.
        self.attempts.clear();
        self.provider_calls.clear();
        self.pending_memory_writes.clear();
        self.pending_memory_queries.clear();

        let LoopAction::Done { result } = action else {
            return Err(KernelFault::new(
                KernelFaultCode::InvalidLifecycle,
                format!(
                    "cancelling the operation produced {} instead of a terminal; cancellation is \
                     the one control command that always ends the operation (§11.1)",
                    loop_action_label(&action)
                ),
            ));
        };
        Ok(PlannedStep {
            root_kind,
            focus,
            observations: Vec::new(),
            disposition: StepDisposition::Terminal(TerminalDisposition {
                terminal: KernelTerminal::Cancelled(CancelledTerminal {
                    reason,
                    usage: UsageReport {
                        input_tokens: WireU64::new(result.total_tokens_used),
                        output_tokens: WireU64::ZERO,
                        turns: result.turns_used,
                        cached_input_tokens: None,
                    },
                }),
            }),
        })
    }

    /// §7.5 · the host's own plan edit. Same payload as the model's `update_task` syscall, applied
    /// through the same context mechanism — and deliberately *not* through the P1 gate, because the
    /// authority is the host's rather than a derived caller's.
    fn plan_host_task_update(
        &mut self,
        update: &UpdateTaskCommand,
    ) -> Result<PlannedStep, KernelFault> {
        let engine = self.engine_mut()?;
        engine.ctx.update_task(core_task_update(&update.update));
        Ok(self.quiet_step())
    }

    /// §13.2 · mount/unmount in one command so a swap is atomic. Unmounting something absent errs
    /// open (it is already not mounted), which is the only shape that makes a retry safe.
    fn plan_capability_patch(
        &mut self,
        patch: &ApplyCapabilityPatchCommand,
    ) -> Result<PlannedStep, KernelFault> {
        let engine = self.engine_mut()?;
        for grant in &patch.patch.mount {
            engine.mount_capability(
                crate::types::capability::CapabilityDescriptor {
                    id: grant.id.as_str().into(),
                    kind: core_capability_kind(grant.kind),
                    description: grant.description.clone().unwrap_or_default(),
                    tool_schema: None,
                    skill: None,
                    metadata: serde_json::Value::Null,
                    lease: None,
                    is_pinned: false,
                    version: None,
                    mounted_by: None,
                    mount_reason: None,
                },
                None,
                None,
            );
        }
        for reference in &patch.patch.unmount {
            engine.unmount_capability(core_capability_kind(reference.kind), &reference.id);
        }
        Ok(self.quiet_step())
    }

    /// §13.2 · keyed knowledge upsert + removal. Both directions are boundary-deferred by the
    /// partition itself, so the model never sees system bytes change mid-turn.
    fn plan_knowledge_mutation(
        &mut self,
        mutation: &ApplyKnowledgeMutationCommand,
    ) -> Result<PlannedStep, KernelFault> {
        let engine = self.engine_mut()?;
        seed_knowledge(engine, &mutation.mutation.upsert);
        for key in &mutation.mutation.remove {
            engine.ctx.remove_knowledge(key);
        }
        Ok(self.quiet_step())
    }

    /// DEC-9 · the host seeding the knowledge partition. Same mechanism as the initial context's
    /// `knowledge`, and named apart from the P1 `PageIn { handle_id }` on purpose: the two are
    /// opposite directions and must never share a name again (§7.5).
    fn plan_seed_knowledge(
        &mut self,
        seed: &SeedKnowledgeCommand,
    ) -> Result<PlannedStep, KernelFault> {
        let engine = self.engine_mut()?;
        seed_knowledge(engine, &seed.entries);
        Ok(self.quiet_step())
    }

    /// §13.2 · skill activation state, validated whole before anything moves.
    ///
    /// A name outside the operation's declared catalog is refused rather than invented — the same
    /// rule the model's `ActivateSkill` syscall obeys — and because the command is atomic, one bad
    /// name refuses the whole swap instead of leaving half of it applied.
    fn plan_skill_activation(
        &mut self,
        activation: &ApplySkillActivationCommand,
    ) -> Result<PlannedStep, KernelFault> {
        let engine = self.engine_mut()?;
        for activate in &activation.activate {
            if !engine.ctx.skill_available(&activate.name) {
                return Err(KernelFault::new(
                    KernelFaultCode::InvalidConfig,
                    format!(
                        "this operation declares no skill named {:?}; activating one is a \
                         capability mutation and is refused rather than invented (§13.2)",
                        activate.name
                    ),
                ));
            }
        }
        let turn = engine.turn;
        for activate in &activation.activate {
            let expires_at_turn = activate.lease_turns.map(|turns| turn.saturating_add(turns));
            engine
                .ctx
                .activate_skill_leased(activate.name.as_str(), expires_at_turn);
        }
        for name in &activation.deactivate {
            engine.ctx.deactivate_skill(name);
        }
        Ok(self.quiet_step())
    }

    /// §13.2 / DEC-6 · one revision-guarded policy patch.
    ///
    /// [`LivePolicyState::apply`] is all-or-nothing: a stale revision, a widened quota or a policy
    /// that fails its boot validator leaves both the configuration and the revision exactly as they
    /// were, so a refused patch is a zero-mutation rejection and the writer rebases instead of
    /// silently overwriting whoever won the race. Only after it succeeds are the changed policies
    /// re-installed into the running engine, through the same installers the genesis build uses.
    fn plan_policy_patch(
        &mut self,
        patch: &ApplyPolicyPatchCommand,
    ) -> Result<PlannedStep, KernelFault> {
        let Some(policy) = self.policy.as_mut() else {
            return Err(KernelFault::new(
                KernelFaultCode::InvalidLifecycle,
                "the operation has no genesis configuration, so it has no policy to patch"
                    .to_string(),
            ));
        };
        let revision = policy.apply(patch).map_err(|rejection| {
            KernelFault::new(KernelFaultCode::InvalidConfig, rejection.message)
        })?;
        let config = policy.config().clone();
        let engine = self.engine_mut()?;
        install_live_policies(engine, &config);
        let turn = engine.turn;
        engine
            .observations
            .push(KernelObservation::LivePolicyChanged {
                turn,
                policy: live_policy_label(&patch.patch).to_string(),
                revision: revision.get(),
            });
        Ok(self.quiet_step())
    }

    /// §13.2 · the operation's absolute deadline, projected onto the wall-time budget axis the
    /// scheduler already owns.
    ///
    /// The axis is a duration measured from the operation's first accepted time, so an absolute
    /// deadline becomes `deadline − start`. A deadline already in the past is not an error: it
    /// yields a zero-length budget, and the next scheduling decision terminates on `Deadline` —
    /// the same verdict the axis would have reached on its own a moment later.
    fn plan_update_deadline(
        &mut self,
        deadline: &UpdateDeadlineCommand,
    ) -> Result<PlannedStep, KernelFault> {
        let engine = self.engine_mut()?;
        let started_at_ms = engine.started_at_ms();
        let budget = match (deadline.deadline_ms, started_at_ms) {
            (None, _) => None,
            (Some(deadline_ms), Some(started_at_ms)) => {
                Some(deadline_ms.get().saturating_sub(started_at_ms))
            }
            (Some(_), None) => {
                return Err(KernelFault::new(
                    KernelFaultCode::InvalidLifecycle,
                    "this operation has accepted no timed input yet, so an absolute deadline has \
                     no start to measure from (§11.2)"
                        .to_string(),
                ));
            }
        };
        engine.set_wall_budget(budget);
        Ok(self.quiet_step())
    }

    /// The shared tail of every non-start transition: turn the engine's action into a disposition
    /// and re-derive the focus from what the engine actually did.
    fn continue_after(
        &mut self,
        context: &PlanContext<'_>,
        action: LoopAction,
        root_kind: RootKind,
    ) -> Result<PlannedStep, KernelFault> {
        let mut index = 0;
        self.continue_after_at(context, action, root_kind, &mut index)
    }

    fn continue_after_at(
        &mut self,
        context: &PlanContext<'_>,
        action: LoopAction,
        root_kind: RootKind,
        effect_index: &mut u32,
    ) -> Result<PlannedStep, KernelFault> {
        let workflow_finished = self
            .engine()
            .map(|engine| {
                engine
                    .observations
                    .iter()
                    .any(|o| matches!(o, KernelObservation::WorkflowCompleted { .. }))
            })
            .unwrap_or(false);
        let disposition = self.disposition_for_at(context, action, root_kind, effect_index)?;
        let focus = if workflow_finished {
            // §6.1.7 / §7.4: a nested workflow's completion restores the parent agent turn; a root
            // workflow's completion commits the terminal instead and its focus stops moving.
            match (root_kind, &self.focus) {
                (RootKind::Agent, Some(ExecutionFocus::WorkflowController(controller))) => {
                    controller
                        .parent_task_id
                        .clone()
                        .map(ExecutionFocus::agent_turn)
                }
                (_, current) => current.clone(),
            }
        } else {
            self.focus.clone()
        };
        Ok(PlannedStep {
            root_kind: Some(root_kind),
            focus,
            observations: Vec::new(),
            disposition,
        })
    }

    /// Project one [`LoopAction`] onto §7.12's closed step disposition.
    fn disposition_for(
        &mut self,
        context: &PlanContext<'_>,
        action: LoopAction,
        root_kind: RootKind,
    ) -> Result<StepDisposition, KernelFault> {
        let mut index = 0;
        self.disposition_for_at(context, action, root_kind, &mut index)
    }

    /// The same projection, minting effect ids from a caller-owned counter. A step that reduces a
    /// syscall batch publishes several effects, and each one's identity has to stay unique.
    fn disposition_for_at(
        &mut self,
        context: &PlanContext<'_>,
        action: LoopAction,
        root_kind: RootKind,
        effect_index: &mut u32,
    ) -> Result<StepDisposition, KernelFault> {
        let operation_id = &context.input.operation_id;
        let causation = context.input.input_id.clone();
        let step_seq = context.step_seq;

        // A durability effect produced *inside* the transition (a compaction's page-out) is
        // published first and holds the continuation. The
        // guard inside makes a second call in the same step a no-op, so a step never activates two.
        let mut action = self.engine_mut()?.externalize_pending_host_effect(action);
        // DEC-8 · an archive the host never declared it can perform is not a reason to refuse the
        // transition that produced it: the compaction already happened in-kernel and the archive is
        // best effort. It is abandoned through the same one-decision path a host failure takes, so
        // the audit fact is identical whether the host said "I cannot" or "I could not".
        while matches!(action, LoopAction::ArchivePageOut { .. })
            && !context
                .config
                .host_effect_support
                .supports(EffectKindTag::ArchivePageOut)
        {
            action = self.engine_mut()?.abandon_page_out_archive(
                "this operation's host declares no archive_page_out support".to_string(),
            );
        }

        let effect = match action {
            LoopAction::AwaitingResume => {
                // A root workflow that just drained its DAG terminates here, and publishes nothing.
                if root_kind == RootKind::Workflow
                    && let Some(terminal) = self.root_workflow_terminal()
                {
                    return Ok(StepDisposition::Terminal(TerminalDisposition { terminal }));
                }
                return Ok(StepDisposition::Effects(EffectsDisposition::default()));
            }
            LoopAction::Done { result } => {
                return Ok(StepDisposition::Terminal(TerminalDisposition {
                    terminal: agent_terminal(&result),
                }));
            }
            LoopAction::CallLLM {
                context: rendered,
                tools,
            } => EffectKind::CallProvider(CallProviderEffect {
                context: rendered_context(&rendered),
                tools: tools.iter().map(tool_schema).collect(),
            }),
            LoopAction::ExecuteTools { calls } => EffectKind::ExecuteTools(ExecuteToolsEffect {
                calls: calls.iter().map(wire_tool_call).collect::<Result<_, _>>()?,
            }),
            LoopAction::RequestApproval { requests } => {
                EffectKind::RequestApproval(RequestApprovalEffect {
                    requests: requests
                        .iter()
                        .map(wire_approval_request)
                        .collect::<Result<_, _>>()?,
                })
            }
            LoopAction::SpawnWorkflow { nodes, budget } => {
                let mut tasks = Vec::with_capacity(nodes.len());
                for node in &nodes {
                    tasks.push(self.task_launch(operation_id, step_seq, node)?);
                }
                EffectKind::SpawnTasks(SpawnTasksEffect {
                    tasks,
                    budget: budget.as_ref().map(workflow_budget),
                })
            }
            LoopAction::PreemptSubAgents { agent_ids, reason } => {
                // §10.4 · a preemption names the attempt this kernel issued. A task with no live
                // attempt has nothing to preempt, so it is dropped rather than named with a
                // fabricated one.
                let attempts = agent_ids
                    .iter()
                    .filter_map(|agent_id| {
                        let attempt_id = self.attempts.get(agent_id)?.clone();
                        let task_id = TaskId::new(agent_id).ok()?;
                        Some(TaskAttemptRef {
                            task_id,
                            attempt_id,
                        })
                    })
                    .collect();
                EffectKind::PreemptTasks(PreemptTasksEffect { attempts, reason })
            }
            // The engine's action carries the legacy internal trio — criteria, required evidence
            // and the verifier — and the canonical projection consumes none of them. All three
            // are host-owned by decision (§5.2, adjudication §5m-3/§5p): the host looks the
            // verifier and its criteria up from `(contract_id, phase_id)`, which is why the
            // request carries that pair and nothing else. The internal fields stay for the legacy
            // path until Task 23 deletes it.
            LoopAction::EvaluateMilestone {
                phase_id,
                criteria: _,
                required_evidence: _,
                verifier: _,
            } => EffectKind::EvaluateMilestone(EvaluateMilestoneEffect {
                request: super::effect::MilestoneRequest {
                    contract_id: self.require_loaded_contract()?,
                    phase_id,
                },
            }),
            LoopAction::ArchivePageOut {
                summary, archived, ..
            } => EffectKind::ArchivePageOut(self.page_out_effect(
                context,
                summary.as_deref(),
                &archived,
                *effect_index,
            )?),
            // The engine never emits these two: they exist only for the legacy `WriteMemory` /
            // `QueryMemory` inputs, which the canonical wire replaced with a P1 syscall that mints
            // the effect directly (see `plan_memory_write` / `plan_memory_query`).
            action @ (LoopAction::PersistMemory { .. } | LoopAction::QueryMemory { .. }) => {
                return Err(KernelFault::new(
                    KernelFaultCode::InvalidLifecycle,
                    format!(
                        "the semantic kernel emitted {}, which only the deleted legacy memory \
                         inputs can produce; on the canonical wire a memory effect is minted by \
                         the P1 syscall that proposed it",
                        loop_action_label(&action)
                    ),
                ));
            }
        };

        let tag = effect.tag();
        self.require_effect_support(context.config, tag)?;
        let effect_id = mint_effect_id(operation_id, step_seq, *effect_index);
        *effect_index += 1;

        match &effect {
            // §7.6 · remember what this turn advertised and whose turn it was. That pair *is* the
            // `ProviderTool` causation a syscall in the result will be attributed to.
            EffectKind::CallProvider(call) => {
                self.provider_calls.insert(
                    effect_id.clone(),
                    PendingProviderCall {
                        task_id: self.turn_task_id(),
                        exposed_tools: call.tools.iter().map(|tool| tool.name.clone()).collect(),
                    },
                );
            }
            // §10.4 · the launch is published, so every task it names leaves `PendingLaunch`. Only
            // the host's acknowledgement moves them on to `Running`.
            EffectKind::SpawnTasks(spawn) => {
                let launched: Vec<String> = spawn
                    .tasks
                    .iter()
                    .map(|task| task.task_id.as_str().to_string())
                    .collect();
                if let Some(engine) = self.engine.as_mut() {
                    engine.mark_tasks_starting(&launched);
                }
            }
            _ => {}
        }

        Ok(StepDisposition::Effects(EffectsDisposition {
            effects: vec![KernelEffect {
                effect_id,
                causation_input_id: causation,
                effect,
            }],
        }))
    }

    /// The task whose turn is currently issuing provider calls. An agent focus names it directly;
    /// a workflow controller's provider call is the parent agent's turn resuming, and before the
    /// first focus is folded there is only the root.
    fn turn_task_id(&self) -> TaskId {
        let staged = self
            .staged
            .as_ref()
            .and_then(|staged| staged.focus.as_ref());
        match staged.or(self.focus.as_ref()) {
            Some(ExecutionFocus::AgentTurn(turn)) => turn.task_id.clone(),
            Some(ExecutionFocus::WorkflowController(controller)) => controller
                .parent_task_id
                .clone()
                .unwrap_or_else(root_task_id),
            None => root_task_id(),
        }
    }

    /// Build the workflow terminal from the outcome the engine just published. Reads the
    /// observation rather than re-deriving it: the DAG's own `finish()` is the authority on which
    /// nodes completed.
    fn root_workflow_terminal(&mut self) -> Option<KernelTerminal> {
        let workflow_id = self.workflow_id.clone()?;
        let engine = self.engine.as_mut()?;
        let outcomes = engine
            .observations
            .iter()
            .find_map(|observation| match observation {
                KernelObservation::WorkflowCompleted { node_outcomes, .. } => {
                    Some(node_outcomes.clone())
                }
                _ => None,
            })?;
        let mut completed = Vec::new();
        let mut failed = Vec::new();
        for outcome in &outcomes {
            let node_id = self.node_id_for(&outcome.node_id);
            match outcome.status {
                WorkflowNodeStatus::Completed | WorkflowNodeStatus::CompletedPartial => {
                    completed.push(node_id)
                }
                WorkflowNodeStatus::Failed | WorkflowNodeStatus::SkippedUpstreamFailed => {
                    failed.push(node_id)
                }
            }
        }
        let status = if failed.is_empty() {
            WorkflowStatus::Completed
        } else {
            WorkflowStatus::Failed
        };
        Some(KernelTerminal::Workflow(WorkflowTerminal {
            outcome: WorkflowOutcome {
                workflow_id,
                status,
                completed_nodes: completed,
                failed_nodes: failed,
            },
            usage: self.usage_report(),
        }))
    }

    fn usage_report(&self) -> UsageReport {
        let Some(engine) = self.engine.as_ref() else {
            return UsageReport::default();
        };
        let (tokens, _, _) = engine.local_budget_usage();
        UsageReport {
            input_tokens: WireU64::new(tokens),
            output_tokens: WireU64::ZERO,
            turns: engine.turn,
            cached_input_tokens: None,
        }
    }

    /// One child launch, with kernel-minted identity. The task/attempt/launch triple exists as a
    /// committed fact before the host is ever asked to start anything (§10.4).
    fn task_launch(
        &mut self,
        operation_id: &OperationId,
        step_seq: WireU64,
        info: &crate::orchestration::workflow::WorkflowSpawnInfo,
    ) -> Result<TaskLaunch, KernelFault> {
        let task_id = TaskId::new(&info.agent_id).map_err(malformed)?;
        let attempt_id =
            AttemptId::new(format!("{}:attempt:1", info.agent_id)).map_err(malformed)?;
        let launch_token = LaunchToken::new(format!(
            "{operation_id}:step:{step_seq}:launch:{}",
            info.agent_id
        ))
        .map_err(malformed)?;
        self.attempts
            .insert(info.agent_id.clone(), attempt_id.clone());
        let mut metadata = serde_json::Map::new();
        if let Some(model_hint) = &info.model_hint {
            metadata.insert(
                "model_hint".to_string(),
                serde_json::Value::String(model_hint.clone()),
            );
        }
        if let Some(output_schema) = &info.output_schema {
            metadata.insert("output_schema".to_string(), output_schema.clone());
        }
        if !info.input_agent_ids.is_empty() {
            metadata.insert(
                "input_agent_ids".to_string(),
                serde_json::Value::Array(
                    info.input_agent_ids
                        .iter()
                        .cloned()
                        .map(serde_json::Value::String)
                        .collect(),
                ),
            );
            let dependency_outputs = info
                .input_agent_ids
                .iter()
                .filter_map(|agent_id| {
                    let output = self
                        .engine
                        .as_ref()?
                        .task_table()
                        .get(agent_id)?
                        .proc
                        .as_ref()?
                        .result
                        .as_ref()?
                        .result
                        .final_message
                        .as_ref()
                        .and_then(message_body_parts)?
                        .0;
                    Some((agent_id.clone(), serde_json::Value::String(output)))
                })
                .collect();
            metadata.insert(
                "dependency_outputs".to_string(),
                serde_json::Value::Object(dependency_outputs),
            );
        }
        Ok(TaskLaunch {
            task_id,
            attempt_id,
            launch_token,
            node_id: self.node_id_for(&info.agent_id),
            spec: LogicalAgentSpec {
                goal: info.goal.clone(),
                role: parse_wire_role(&info.role),
                isolation: parse_wire_isolation(&info.isolation),
                context_inheritance: parse_wire_context_inheritance(&info.context_inheritance),
                verification_contract_id: None,
                capability_filter: Default::default(),
                exposure_baseline: None,
                loop_round: None,
                metadata: super::scalar::BoundedJson::new(serde_json::Value::Object(metadata))
                    .map_err(malformed)?,
            },
        })
    }

    /// Wire identity of the DAG node an internal agent id belongs to. Falls back to the internal id
    /// when the node came from a runtime append rather than the original spec (Task 10 territory).
    fn node_id_for(&self, agent_id: &str) -> NodeId {
        parse_node_index(agent_id)
            .and_then(|index| self.node_ids.get(index).cloned())
            .unwrap_or_else(|| {
                NodeId::new(agent_id).expect("an internal agent id is a legal branded ref")
            })
    }

    /// §7.3 · a spec's `verification_contract_id` must name a contract this operation declared.
    ///
    /// A reference that resolves to nothing is a gate the run believes it has and does not: the
    /// agent would start with no phase cascade, never publish an `EvaluateMilestone`, and finish
    /// having passed a contract that was never evaluated. Refused before the engine moves, so a
    /// rejected start leaves the operation free to start again with a spec that resolves.
    fn require_known_contract(
        &self,
        config: &ResolvedOperationConfig,
        spec: Option<&LogicalAgentSpec>,
    ) -> Result<(), KernelFault> {
        let Some(contract_id) = spec.and_then(|spec| spec.verification_contract_id.as_deref())
        else {
            return Ok(());
        };
        if config.verification_contract(contract_id).is_some() {
            return Ok(());
        }
        Err(KernelFault::new(
            KernelFaultCode::InvalidConfig,
            format!(
                "the run spec names verification contract {contract_id:?}, which this operation's \
                 catalog does not declare; a contract reference that resolves to nothing is a \
                 milestone gate the run believes it has (§7.3)"
            ),
        ))
    }

    /// Install the phase cascade an agent's contract reference selects.
    ///
    /// This is `EvaluateMilestone`'s canonical producer (Task 12 SPEC-ISSUE-4): without it the
    /// effect existed in the union, had a resolution path and a failure path, and nothing on the
    /// wire could ever cause the kernel to emit one.
    fn load_verification_contract(
        &mut self,
        config: &ResolvedOperationConfig,
        spec: Option<&LogicalAgentSpec>,
    ) -> Result<(), KernelFault> {
        let Some(contract) = spec
            .and_then(|spec| spec.verification_contract_id.as_deref())
            .and_then(|id| config.verification_contract(id))
        else {
            return Ok(());
        };
        // A contract means the operation *will* ask for a verdict, so the effect it will publish
        // has to be declared now rather than faulting mid-cascade (DEC-8).
        self.require_effect_support(config, EffectKindTag::EvaluateMilestone)?;
        let contract_id = contract.contract_id.clone();
        let cascade = core_milestone_contract(contract, config);
        self.engine_mut()?.load_milestone_contract(cascade);
        // Remembered so every `EvaluateMilestone` the cascade produces can name the contract its
        // phase belongs to — the half of the host's lookup key the semantic engine does not carry.
        self.loaded_contract_id = Some(contract_id);
        Ok(())
    }

    /// The contract id every `EvaluateMilestone` this operation publishes belongs to.
    ///
    /// Fails closed rather than sending an empty id: a cascade can only be running because
    /// `load_verification_contract` installed one, so a milestone request with no contract behind
    /// it means the engine produced a phase the canonical wire never declared — and a request the
    /// host cannot resolve to a verifier is worse than no request at all.
    fn require_loaded_contract(&self) -> Result<String, KernelFault> {
        self.loaded_contract_id.clone().ok_or_else(|| {
            KernelFault::new(
                KernelFaultCode::InvalidLifecycle,
                "the semantic kernel asked for a milestone verdict, but this operation installed \
                 no verification contract; a milestone request names the (contract_id, phase_id) \
                 pair the host looks its verifier up by (§7.8)"
                    .to_string(),
            )
        })
    }

    fn require_effect_support(
        &self,
        config: &ResolvedOperationConfig,
        tag: EffectKindTag,
    ) -> Result<(), KernelFault> {
        if config.host_effect_support.supports(tag) {
            return Ok(());
        }
        Err(KernelFault::new(
            KernelFaultCode::UnsupportedEffect,
            format!(
                "this operation's host does not declare support for {tag} effects, so the \
                 transition that would publish one is refused before anything moves"
            ),
        ))
    }

    fn require_root_kind(&self) -> Result<RootKind, KernelFault> {
        self.root_kind.ok_or_else(|| {
            KernelFault::new(
                KernelFaultCode::InvalidLifecycle,
                "no root has started, so there is nothing to advance".to_string(),
            )
        })
    }

    fn engine_mut(&mut self) -> Result<&mut LoopStateMachine, KernelFault> {
        self.engine.as_mut().ok_or_else(|| {
            KernelFault::new(
                KernelFaultCode::InvalidLifecycle,
                "the operation has no genesis configuration, so it has no semantic kernel to drive"
                    .to_string(),
            )
        })
    }

    fn poison_with(&mut self, fault: KernelFault) -> KernelFault {
        self.staged = None;
        self.poison.get_or_insert(fault).clone()
    }
}

// ---------------------------------------------------------------------------------------------
// wire ⇄ semantic projections
// ---------------------------------------------------------------------------------------------

fn root_task_id() -> TaskId {
    TaskId::new(ROOT_TASK_ID).expect("the root task id is a legal branded ref")
}

// ---------------------------------------------------------------------------------------------
// §7.6 · the model-facing syscall surface
// ---------------------------------------------------------------------------------------------

/// Tool names that reduce to a P1 syscall instead of to a host tool execution.
///
/// This list is what deletes §22.10's bypasses 4 and 5. Historically the SDK *removed*
/// `submit_workflow_nodes` / `start_workflow` from the tool loop, faked a tool result for the
/// model, and re-submitted the request as a separate kernel input with a submitter of its own
/// choosing — so the kernel never saw a `ProviderTool` causation at all. Here the names are the
/// kernel's, the arguments are decoded by the kernel, and the caller comes from the pending call.
///
/// SPEC-ISSUE: `SyscallRequest::RequestMemoryWrite` has no entry here because core advertises no
/// model-facing memory *write* surface — `memory` is a search tool, and long-term writes are
/// extracted host-side today (§22.13's 现状定位). Its only caller channel is therefore a child's
/// `parent_requests`. §7.6 lists the request without saying which tool reaches it, so either the
/// kernel's meta-tool set gains a write surface or the spec should state that memory writes are a
/// child→parent request only.
pub const SYSCALL_TOOL_NAMES: &[&str] = &[
    "start_workflow",
    "submit_workflow_nodes",
    "skill",
    "update_plan",
    crate::context::manager::MEMORY_TOOL_NAME,
    crate::context::manager::READ_RESULT_TOOL_NAME,
];

fn is_syscall_tool(name: &str) -> bool {
    SYSCALL_TOOL_NAMES.contains(&name)
}

/// Decode a recognised meta-tool call into its typed request.
///
/// A decode failure is a *rejection*, never a fault: the model wrote bad arguments, which is a
/// thing to answer with an audit fact rather than a host protocol violation.
fn decode_syscall(call: &WireToolCall) -> Result<SyscallRequest, SyscallRejection> {
    let arguments = call.arguments.get().clone();
    let name: &'static str = SYSCALL_TOOL_NAMES
        .iter()
        .copied()
        .find(|known| *known == call.name.as_str())
        .expect("only recognised syscall tools reach the decoder");

    fn decode<T: serde::de::DeserializeOwned>(
        name: &'static str,
        arguments: serde_json::Value,
    ) -> Result<T, SyscallRejection> {
        serde_json::from_value(arguments)
            .map_err(|error| SyscallRejection::new(name, format!("malformed arguments: {error}")))
    }

    match name {
        "start_workflow" => Ok(SyscallRequest::SubmitWorkflow(
            super::syscall::SubmitWorkflowRequest {
                spec: decode(name, arguments)?,
            },
        )),
        "submit_workflow_nodes" => {
            #[derive(serde::Deserialize)]
            struct Args {
                nodes: Vec<WireNode>,
            }
            let args: Args = decode(name, arguments)?;
            Ok(SyscallRequest::AppendWorkflowNodes(
                super::syscall::AppendWorkflowNodesRequest { nodes: args.nodes },
            ))
        }
        "skill" => {
            #[derive(serde::Deserialize)]
            struct Args {
                name: String,
                #[serde(default)]
                lease_turns: Option<u32>,
            }
            let args: Args = decode(name, arguments)?;
            Ok(SyscallRequest::ActivateSkill(
                super::syscall::ActivateSkillRequest {
                    name: args.name,
                    lease_turns: args.lease_turns,
                },
            ))
        }
        "update_plan" => Ok(SyscallRequest::UpdateTask(
            super::syscall::UpdateTaskRequest {
                update: decode(name, arguments)?,
            },
        )),
        crate::context::manager::MEMORY_TOOL_NAME => {
            #[derive(serde::Deserialize)]
            struct Args {
                #[serde(default)]
                query: String,
                #[serde(default)]
                kinds: Vec<WireMemoryKind>,
                #[serde(default)]
                top_k: Option<u32>,
            }
            let args: Args = decode(name, arguments)?;
            Ok(SyscallRequest::RequestMemoryQuery(
                super::syscall::RequestMemoryQueryRequest {
                    query: super::syscall::MemoryQueryProposal {
                        text: args.query,
                        kinds: args.kinds,
                        limit: args.top_k,
                    },
                },
            ))
        }
        crate::context::manager::READ_RESULT_TOOL_NAME => {
            #[derive(serde::Deserialize)]
            struct Args {
                call_id: String,
            }
            let args: Args = decode(name, arguments)?;
            let handle_id = super::scalar::HandleId::new(args.call_id).map_err(|error| {
                SyscallRejection::new(name, format!("malformed handle: {}", error.message))
            })?;
            Ok(SyscallRequest::PageIn(super::syscall::PageInRequest {
                handle_id,
            }))
        }
        other => unreachable!("unrecognised syscall tool {other}"),
    }
}

/// The task a causation names. Both variants carry one, and neither lets a host choose it.
fn causation_task(causation: &SyscallCausation) -> TaskId {
    match causation {
        SyscallCausation::ProviderTool(provider) => provider.task_id.clone(),
        SyscallCausation::ChildAttempt(child) => child.task_id.clone(),
    }
}

/// §7.6 · the authority families a quarantined caller may not touch. `None` ⇒ the request widens
/// nothing (a plan edit, a page-in of an address the caller already holds).
///
/// SPEC-ISSUE: §7.6 requires that "a quarantined task must not escalate through workflow append,
/// memory scope or capability mutation", but the canonical [`WorkflowNode`](super::root::WorkflowNode)
/// carries no trust level — the internal DAG has `NodeTrust::{Trusted,Quarantined}` and the wire
/// has no field for it. The refusal below is therefore complete but currently unreachable through
/// the contract: no canonical input can declare a node quarantined. Either §7.4's workflow node
/// grows a trust field, or §7.6 has to say where quarantine comes from.
fn privileged_family(request: &SyscallRequest) -> Option<&'static str> {
    match request {
        SyscallRequest::SubmitWorkflow(_) | SyscallRequest::AppendWorkflowNodes(_) => {
            Some("workflow")
        }
        SyscallRequest::RequestMemoryWrite(_) | SyscallRequest::RequestMemoryQuery(_) => {
            Some("memory")
        }
        SyscallRequest::ActivateSkill(_) => Some("capability"),
        SyscallRequest::UpdateTask(_) | SyscallRequest::PageIn(_) => None,
    }
}

fn core_task_update(update: &WireTaskUpdate) -> crate::context::task_state::TaskUpdate {
    crate::context::task_state::TaskUpdate {
        plan: update.plan.clone(),
        current_step: update.current_step.map(|step| step as usize),
        progress: update.progress.clone(),
        scratchpad: update.scratchpad.clone(),
        blocked_on: update.blocked_on.clone(),
        preserved_refs: update.preserved_refs.clone(),
        directives: update.directives.clone(),
    }
}

fn mint_effect_id(operation_id: &OperationId, step_seq: WireU64, index: u32) -> EffectId {
    EffectId::new(format!("{operation_id}:step:{step_seq}:effect:{index}"))
        .expect("an operation-scoped effect id is always a legal branded ref")
}

fn mint_workflow_id(operation_id: &OperationId, step_seq: WireU64) -> WorkflowId {
    WorkflowId::new(format!("{operation_id}:workflow:{step_seq}"))
        .expect("an operation-scoped workflow id is always a legal branded ref")
}

/// `wf-node{N}` / `wf-node{N}-i{k}` → `N`. The internal DAG is index-addressed; the wire is not.
fn parse_node_index(agent_id: &str) -> Option<usize> {
    let rest = agent_id.strip_prefix("wf-node")?;
    let digits: String = rest.chars().take_while(char::is_ascii_digit).collect();
    digits.parse().ok()
}

fn wire_node_ids(spec: &WireSpec) -> Vec<NodeId> {
    spec.nodes.iter().map(|node| node.node_id.clone()).collect()
}

/// Wire DAG → the kernel's index-addressed DAG. Node identity is checked here: a duplicate id or a
/// dependency on a node the spec does not declare is refused before the engine sees the spec.
fn build_core_spec(spec: &WireSpec) -> Result<CoreWorkflowSpec, KernelFault> {
    let mut index_of: BTreeMap<&str, usize> = BTreeMap::new();
    for (index, node) in spec.nodes.iter().enumerate() {
        if index_of.insert(node.node_id.as_str(), index).is_some() {
            return Err(KernelFault::new(
                KernelFaultCode::InvalidConfig,
                format!(
                    "workflow node id {:?} appears twice; node identity is unique within a DAG",
                    node.node_id
                ),
            ));
        }
    }
    let mut nodes = Vec::with_capacity(spec.nodes.len());
    for node in &spec.nodes {
        let role = node
            .run_spec
            .as_ref()
            .and_then(|spec| spec.role)
            .map_or(AgentRole::Custom, core_role);
        let mut core = CoreWorkflowNode::new(runtime_task(&node.task), role);
        if let Some(isolation) = node.run_spec.as_ref().and_then(|spec| spec.isolation) {
            core = core.with_isolation(core_isolation(isolation));
        }
        if let Some(inheritance) = node
            .run_spec
            .as_ref()
            .and_then(|spec| spec.context_inheritance)
        {
            core.context_inheritance = core_context_inheritance(inheritance);
        }
        if let Some(metadata) = node
            .run_spec
            .as_ref()
            .map(|spec| spec.metadata.get())
            .and_then(serde_json::Value::as_object)
        {
            if let Some(model_hint) = metadata
                .get("model_hint")
                .and_then(serde_json::Value::as_str)
            {
                core = core.with_model_hint(model_hint);
            }
            if let Some(output_schema) = metadata.get("output_schema") {
                core = core.with_output_schema(output_schema.clone());
            }
        }
        let mut depends_on = Vec::with_capacity(node.depends_on.len());
        for dependency in &node.depends_on {
            let Some(&index) = index_of.get(dependency.as_str()) else {
                return Err(KernelFault::new(
                    KernelFaultCode::InvalidConfig,
                    format!(
                        "workflow node {:?} depends on {:?}, which this DAG does not declare",
                        node.node_id, dependency
                    ),
                ));
            };
            depends_on.push(index);
        }
        nodes.push(core.with_depends_on(depends_on));
    }
    let core = CoreWorkflowSpec::new(nodes);
    core.validate()
        .map_err(|error| KernelFault::new(KernelFaultCode::InvalidConfig, error.to_string()))?;
    Ok(core)
}

fn runtime_task(task: &LogicalTask) -> RuntimeTask {
    RuntimeTask {
        goal: task.goal.clone(),
        criteria: task.criteria.clone(),
        metadata: task.metadata.get().clone(),
        lane: task.lane.as_ref().map(TaskLane::new).unwrap_or_default(),
    }
}

/// §7.4 · the logical spec carries no host session identity, so the internal identity it builds
/// carries none either. (Task 11 removes the field from `AgentIdentity` outright.)
fn agent_run_spec(spec: &LogicalAgentSpec) -> AgentRunSpec {
    AgentRunSpec {
        identity: AgentIdentity::new(ROOT_TASK_ID, NO_HOST_SESSION),
        role: spec.role.map_or(AgentRole::Custom, core_role),
        isolation: spec
            .isolation
            .map_or(AgentIsolation::Shared, core_isolation),
        goal: spec.goal.clone(),
        verification_contract_id: spec.verification_contract_id.as_deref().map(Into::into),
        capability_filter: AgentCapabilityFilter {
            allowed_kinds: Vec::new(),
            allowed_ids: spec
                .capability_filter
                .allowed_ids
                .iter()
                .map(|id| id.as_str().into())
                .collect(),
        },
        milestones: None,
        metadata: spec.metadata.get().clone(),
        loop_round: spec.loop_round.as_ref().map(|round| LoopRoundSpec {
            max_rounds: round.max_rounds,
            min_sleep_ms: round.min_sleep_ms.map(WireU64::get),
            max_sleep_ms: round.max_sleep_ms.map(WireU64::get),
            default_action: round.default_action.clone(),
        }),
        exposure_baseline: spec
            .exposure_baseline
            .as_ref()
            .map(|ids| ids.iter().map(|id| id.as_str().into()).collect()),
    }
}

fn core_role(role: WireRole) -> AgentRole {
    match role {
        WireRole::Explore => AgentRole::Explore,
        WireRole::Plan => AgentRole::Plan,
        WireRole::Implement => AgentRole::Implement,
        WireRole::Verify => AgentRole::Verify,
        WireRole::Custom => AgentRole::Custom,
    }
}

fn core_isolation(isolation: WireIsolation) -> AgentIsolation {
    match isolation {
        WireIsolation::Shared => AgentIsolation::Shared,
        WireIsolation::ReadOnly => AgentIsolation::ReadOnly,
        WireIsolation::Worktree => AgentIsolation::Worktree,
        WireIsolation::Remote => AgentIsolation::Remote,
    }
}

fn core_context_inheritance(inheritance: WireContextInheritance) -> ContextInheritance {
    match inheritance {
        WireContextInheritance::None => ContextInheritance::None,
        WireContextInheritance::SystemOnly => ContextInheritance::SystemOnly,
        WireContextInheritance::Full => ContextInheritance::Full,
    }
}

/// Internal role/isolation labels back onto the wire vocabulary. `None` is the *absent* field, not
/// a parse failure: `custom`/`shared` are the wire defaults, so omitting them keeps a launch spec
/// minimal instead of restating what the contract already implies.
fn parse_wire_role(label: &str) -> Option<WireRole> {
    match label {
        "explore" => Some(WireRole::Explore),
        "plan" => Some(WireRole::Plan),
        "implement" => Some(WireRole::Implement),
        "verify" => Some(WireRole::Verify),
        _ => None,
    }
}

fn parse_wire_isolation(label: &str) -> Option<WireIsolation> {
    match label {
        "read_only" => Some(WireIsolation::ReadOnly),
        "worktree" => Some(WireIsolation::Worktree),
        "remote" => Some(WireIsolation::Remote),
        _ => None,
    }
}

fn parse_wire_context_inheritance(label: &str) -> Option<WireContextInheritance> {
    match label {
        "none" => Some(WireContextInheritance::None),
        "system_only" => Some(WireContextInheritance::SystemOnly),
        "full" => Some(WireContextInheritance::Full),
        _ => None,
    }
}

/// §7.4 · seed the P3 context partitions from the one initial context the start carried. This is
/// the whole of what used to be a dozen separate accepted inputs.
fn seed_initial_context(engine: &mut LoopStateMachine, initial: &InitialContext) {
    if !initial.messages.is_empty() {
        engine.preload_history(initial.messages.iter().map(logical_message).collect());
    }
    seed_knowledge(engine, &initial.knowledge);
}

/// The one place wire knowledge entries enter the P3 knowledge partition.
///
/// Shared by the initial context (§7.4), `HostCommand::SeedKnowledge` (DEC-9) and the upsert half
/// of `HostCommand::ApplyKnowledgeMutation` (§13.2), so the three cannot drift in how a keyed,
/// pinned or token-counted entry is stored.
fn seed_knowledge(engine: &mut LoopStateMachine, entries: &[super::root::KnowledgeEntry]) {
    if entries.is_empty() {
        return;
    }
    let entries: Vec<crate::mm::PageInEntry> = entries
        .iter()
        .map(|entry| crate::mm::PageInEntry {
            content: entry.content.clone(),
            tokens: entry.tokens,
            source: None,
            key: entry.key.clone(),
            pinned: entry.pinned,
        })
        .collect();
    engine.apply_page_in(&entries);
}

/// §7.7 · project one logical signal onto the runtime signal the in-kernel router works with.
///
/// Three rules are load-bearing:
///
/// * the **business** signal id travels verbatim. It is what the disposition, expiry and
///   displacement audit facts name, so a derived id would report an identity no caller ever wrote;
/// * `timestamp_ms` is the **envelope's accepted time**, not the signal's `source_timestamp_ms`.
///   The source timestamp is audit metadata and stays out of every admission decision (§11.2);
/// * absent optional fields mean "the author did not say", not a default urgency or source that
///   would change how the signal is scheduled;
/// * `escalate_after_ms` is a **duration** the kernel anchors to that same accepted time. That is
///   what closes the old gap where §13.2 admitted `SignalPolicy.deadline_escalation` while §7.7
///   carried nothing that could ever come due, leaving the whole escalation axis unreachable
///   (Task 14 · adjudication §5n item 1). The kernel still invents no deadline from
///   `source_timestamp_ms`, which is not a clock (§11.2).
///
/// SPEC-ISSUE (task-targeted routing): §7.7 defines the address space (operation or logical task)
/// but no per-task attention semantics, and core holds **one** router per operation. A validated
/// task target therefore lands in the operation's queue rather than a queue of its own. Either
/// §7.7 states that the target is audit-only addressing, or per-task queues need a contract.
fn runtime_signal(
    signal: &LogicalSignal,
    accepted_at_ms: WireU64,
) -> crate::types::signal::RuntimeSignal {
    use crate::types::signal::{RuntimeSignal, SignalSource, SignalType, Urgency};

    let source = match signal.source {
        Some(SignalSourceKind::Cron) => SignalSource::Cron,
        Some(SignalSourceKind::Gateway) => SignalSource::Gateway,
        Some(SignalSourceKind::Heartbeat) => SignalSource::Heartbeat,
        Some(SignalSourceKind::Custom) | None => SignalSource::Custom,
    };
    let urgency = match signal.urgency {
        Some(SignalUrgency::Low) => Urgency::Low,
        Some(SignalUrgency::High) => Urgency::High,
        Some(SignalUrgency::Critical) => Urgency::Critical,
        Some(SignalUrgency::Normal) | None => Urgency::Normal,
    };
    let mut runtime = RuntimeSignal::new(
        source,
        // `signal_type` is deliberately not on the canonical wire (adjudication §5n item 3):
        // urgency already expresses priority, nothing branches on the router's event/job/alert
        // distinction, and a second axis that changes no decision is one more thing four hosts
        // would have to agree about. Every canonical signal enters as an event.
        SignalType::Event,
        urgency,
        signal_summary(signal),
    )
    .with_id(signal.signal_id.as_str())
    .with_payload(signal.payload.get().clone())
    .with_timestamp(accepted_at_ms.get());
    if let Some(key) = &signal.dedupe_key {
        runtime = runtime.with_dedupe(key.as_str());
    }
    // §7.7 · `escalate_after_ms` is a duration; the router works in instants. Anchoring it to the
    // envelope's accepted time here is the whole point of carrying a duration on the wire: the
    // same bytes redelivered produce the same deadline relative to *this* admission, and no host
    // clock ever enters the payload (DEC-2).
    if let Some(after) = signal.escalate_after_ms {
        runtime = runtime.with_deadline(accepted_at_ms.get().saturating_add(after.get()));
    }
    runtime
}

/// The model-facing one-liner a queued or interrupting signal becomes.
///
/// §7.7 carries a payload and no summary, so the summary is derived — deterministically, because a
/// replay must produce the same context bytes. A JSON string payload is its own summary; anything
/// else is its canonical serialization, bounded.
fn signal_summary(signal: &LogicalSignal) -> String {
    const SIGNAL_SUMMARY_MAX_BYTES: usize = 512;
    match signal.payload.get() {
        serde_json::Value::Null => signal.signal_id.as_str().to_string(),
        serde_json::Value::String(text) => {
            truncate_on_char_boundary(text, SIGNAL_SUMMARY_MAX_BYTES)
        }
        other => truncate_on_char_boundary(&other.to_string(), SIGNAL_SUMMARY_MAX_BYTES),
    }
}

fn core_capability_kind(
    kind: super::root::CapabilityKind,
) -> crate::types::capability::CapabilityKind {
    use super::root::CapabilityKind as Wire;
    use crate::types::capability::CapabilityKind as Core;
    match kind {
        Wire::Tool => Core::Tool,
        Wire::Skill => Core::Skill,
        Wire::Memory => Core::Memory,
        Wire::Knowledge => Core::Knowledge,
        Wire::McpServer => Core::McpServer,
        Wire::Command => Core::Command,
        Wire::Agent => Core::Agent,
    }
}

fn live_policy_label(patch: &super::command::LivePolicyPatch) -> &'static str {
    use super::command::LivePolicyPatch;
    match patch {
        LivePolicyPatch::ReplaceSignalPolicy(_) => "signal",
        LivePolicyPatch::ReplaceGovernancePolicy(_) => "governance",
        LivePolicyPatch::TightenResourceQuota(_) => "resource_quota",
        LivePolicyPatch::ReplaceRecoveryPolicy(_) => "recovery",
    }
}

fn logical_message(message: &super::root::LogicalMessage) -> Message {
    Message {
        role: core_role_of(message.role),
        content: Content::Text(message.content.clone()),
        tool_calls: Vec::new(),
        token_count: message.tokens,
    }
}

fn core_role_of(role: MessageRole) -> Role {
    match role {
        MessageRole::System => Role::System,
        MessageRole::User => Role::User,
        MessageRole::Assistant => Role::Assistant,
        MessageRole::Tool => Role::Tool,
    }
}

fn wire_role_of(role: Role) -> MessageRole {
    match role {
        Role::System => MessageRole::System,
        Role::User => MessageRole::User,
        Role::Assistant => MessageRole::Assistant,
        Role::Tool => MessageRole::Tool,
    }
}

fn rendered_context(context: &crate::context::renderer::RenderedContext) -> WireRenderedContext {
    WireRenderedContext {
        system_stable: context.system_stable.clone(),
        system_knowledge: context.system_knowledge.clone(),
        turns: context.turns.iter().map(provider_message).collect(),
        state_turn: context.state_turn.as_ref().map(provider_message),
        frozen_prefix_len: context.frozen_prefix_len.map(|len| len as u32),
    }
}

fn provider_message(message: &Message) -> ProviderMessage {
    let (content, tool_call_id) = message_body_parts(message)
        .map(|(text, tool_call_id, _is_error)| (text, tool_call_id))
        .unwrap_or_default();
    ProviderMessage {
        role: wire_role_of(message.role),
        content,
        tool_calls: message
            .tool_calls
            .iter()
            .filter_map(|call| wire_tool_call(call).ok())
            .collect(),
        tool_call_id: tool_call_id.and_then(|call_id| super::scalar::CallId::new(call_id).ok()),
        tokens: message.token_count,
    }
}

fn tool_schema(schema: &crate::types::message::ToolSchema) -> WireToolSchema {
    WireToolSchema {
        name: schema.name.to_string(),
        description: schema.description.clone(),
        parameters: super::scalar::BoundedJson::new(schema.parameters.clone())
            .unwrap_or_else(|_| Default::default()),
    }
}

fn workflow_budget(budget: &crate::orchestration::workflow::WorkflowBudget) -> WireWorkflowBudget {
    WireWorkflowBudget {
        max_total_tokens: budget.tokens_max.map(WireU64::new),
        max_turns: None,
        max_concurrency: budget.max_concurrent_subagents.map(|max| max as u32),
    }
}

fn sub_agent_result(completed: &ChildCompleted) -> SubAgentResult {
    let termination = match completed.result.status {
        ChildStatus::Completed => TerminationReason::Completed,
        ChildStatus::Failed => TerminationReason::Error,
        ChildStatus::Cancelled => TerminationReason::UserAbort,
    };
    SubAgentResult {
        agent_id: completed.task_id.as_str().into(),
        result: LoopResult {
            termination,
            final_message: completed
                .result
                .output
                .as_ref()
                .map(|text| Message::assistant(text.clone())),
            turns_used: completed
                .result
                .usage
                .as_ref()
                .and_then(|usage| usage.turns)
                .unwrap_or(0),
            total_tokens_used: completed
                .result
                .usage
                .as_ref()
                .and_then(|usage| usage.output_tokens)
                .map_or(0, WireU64::get),
            loop_continue: None,
            classify_branch: None,
            pace_decision: None,
            tournament_winner: None,
        },
    }
}

/// §7.12 · how an agent loop's own termination reason becomes an operation terminal.
///
/// The internal vocabulary has two reasons the wire's `TerminationReason` deliberately does not
/// carry: `user_abort` **is** a `Cancelled` terminal and `error` **is** a `Failed` one. Folding
/// either back into `Completed` would give the same event two representations, which is exactly
/// what the canonical union removed.
fn agent_terminal(result: &LoopResult) -> KernelTerminal {
    let usage = UsageReport {
        input_tokens: WireU64::new(result.total_tokens_used),
        output_tokens: WireU64::ZERO,
        turns: result.turns_used,
        cached_input_tokens: None,
    };
    let termination = match result.termination {
        TerminationReason::Completed => WireTermination::Completed,
        TerminationReason::MaxTurns => WireTermination::MaxTurns,
        TerminationReason::TokenBudget => WireTermination::TokenBudget,
        TerminationReason::Timeout => WireTermination::Deadline,
        TerminationReason::ContextOverflow => WireTermination::ContextOverflow,
        TerminationReason::NoProgress => WireTermination::NoProgress,
        TerminationReason::MilestoneExceeded => WireTermination::MilestoneExceeded,
        TerminationReason::UserAbort => {
            return KernelTerminal::Cancelled(CancelledTerminal {
                reason: CancellationReason::User,
                usage,
            });
        }
        TerminationReason::Error => {
            return KernelTerminal::Failed(FailedTerminal {
                failure: KernelFailure {
                    code: KernelFailureCode::InvariantViolated,
                    message: "the agent loop ended in an error state".to_string(),
                },
                usage,
            });
        }
    };
    KernelTerminal::Agent(AgentTerminal {
        result: WireLoopResult {
            termination,
            final_message: result.final_message.as_ref().map(provider_message),
            turns_used: result.turns_used,
            pace_decision: result.pace_decision.as_ref().map(|decision| {
                super::terminal::PaceDecision {
                    action: match decision.action {
                        CorePaceAction::Continue => super::terminal::PaceAction::Continue,
                        CorePaceAction::Sleep => super::terminal::PaceAction::Sleep,
                        CorePaceAction::Stop => super::terminal::PaceAction::Stop,
                    },
                    delay_ms: decision.delay_ms.map(WireU64::new),
                    reason: decision.reason.clone(),
                    coerced_from: decision.coerced_from.clone(),
                }
            }),
        },
        usage,
    })
}

fn publishes(disposition: &StepDisposition, tag: EffectKindTag) -> bool {
    disposition
        .effects()
        .iter()
        .any(|effect| effect.tag() == tag)
}

fn loop_action_label(action: &LoopAction) -> &'static str {
    match action {
        LoopAction::CallLLM { .. } => "call_provider",
        LoopAction::ExecuteTools { .. } => "execute_tools",
        LoopAction::RequestApproval { .. } => "request_approval",
        LoopAction::SpawnWorkflow { .. } => "spawn_tasks",
        LoopAction::PreemptSubAgents { .. } => "preempt_tasks",
        LoopAction::PersistMemory { .. } => "persist_memory",
        LoopAction::QueryMemory { .. } => "query_memory",
        LoopAction::ArchivePageOut { .. } => "archive_page_out",
        LoopAction::EvaluateMilestone { .. } => "evaluate_milestone",
        LoopAction::Done { .. } => "terminal",
        LoopAction::AwaitingResume => "awaiting_resume",
    }
}

/// The model-facing answer to a P1 syscall the kernel executed.
///
/// 下一请求信息最大化: each says what happened *and* where the consequence will show up, so the
/// model's next turn does not have to guess whether a control-plane call took effect.
fn syscall_ack(name: &str) -> &'static str {
    match name {
        "start_workflow" => {
            "workflow accepted: its ready nodes are scheduled; each result arrives as that node \
             completes"
        }
        "submit_workflow_nodes" => {
            "nodes appended to the running workflow; each result arrives as that node completes"
        }
        "skill" => "skill activated: its guidance and tools are in this turn's context",
        "update_plan" => "plan updated: the new state renders in [TASK STATE] from here on",
        crate::context::manager::MEMORY_TOOL_NAME => {
            "memory search issued: matching records are added to this conversation before your \
             next turn"
        }
        crate::context::manager::READ_RESULT_TOOL_NAME => "page-in requested",
        _ => "accepted",
    }
}

/// Wire → semantic projections for the resolution half.
fn core_provider_message(message: &ProviderMessage) -> Result<Message, KernelFault> {
    Ok(Message {
        role: core_role_of(message.role),
        content: Content::Text(message.content.clone()),
        tool_calls: message.tool_calls.iter().map(core_tool_call).collect(),
        token_count: message.tokens,
    })
}

fn core_tool_call(call: &WireToolCall) -> crate::types::message::ToolCall {
    crate::types::message::ToolCall {
        id: call.call_id.as_str().into(),
        name: call.name.as_str().into(),
        arguments: call.arguments.get().clone(),
    }
}

fn wire_tool_call(call: &crate::types::message::ToolCall) -> Result<WireToolCall, KernelFault> {
    Ok(WireToolCall {
        call_id: super::scalar::CallId::new(call.id.as_str()).map_err(malformed)?,
        name: call.name.to_string(),
        arguments: super::scalar::BoundedJson::new(call.arguments.clone())
            .unwrap_or_else(|_| Default::default()),
    })
}

fn wire_approval_request(
    request: &crate::scheduler::state_machine::ApprovalRequest,
) -> Result<WireApprovalRequest, KernelFault> {
    Ok(WireApprovalRequest {
        call_id: super::scalar::CallId::new(request.call_id.as_str()).map_err(malformed)?,
        tool_name: request.tool.clone(),
        arguments: super::scalar::BoundedJson::new(request.arguments.clone())
            .unwrap_or_else(|_| Default::default()),
        reason: (!request.reason.is_empty()).then(|| request.reason.clone()),
    })
}

/// §7.10 · one returned tool result.
///
/// Both arms produce the same thing: the text that enters working context. For `Inline` that is the
/// body; for `External` it is the preview, and the body never crosses core at all — the host
/// persisted it before submitting, and the kernel holds only the reference the
/// [`ToolsSuccess`](super::effect::ToolsSuccess) carried. The residency transfer that records
/// *where* the body went happens after the engine has accepted the batch (see
/// `record_external_payloads`), because the handle it moves does not exist until the result is in
/// history.
///
/// The canonical [`ToolResultDisposition`] is binary, so the projection onto core's historical
/// `is_fatal` + six-way `ToolErrorKind` is total and lossless in the direction that matters: only
/// `Recoverable` and `Fatal` are reachable, and `UserInterrupt` — the one kind that still rolls a
/// turn back — has no canonical spelling at all. Cancellation travels on `HostControl::Cancel`
/// (§7.9), so that retired retry rung is not re-expressible here.
///
/// §7.10 rule 9 · failure is orthogonal to residency, so the two failure facts are read through
/// [`WireToolResultPayload::disposition`] / [`WireToolResultPayload::is_error`] and land in core
/// identically for both arms. A tool that failed *and* produced a body over the inline threshold —
/// the common shape, not a rare one — is now expressible, and its fatality reaches the batch
/// close-out on the same path an inline one does.
fn core_tool_result(payload: &WireToolResultPayload) -> ToolResult {
    let disposition = payload.disposition();
    let is_error = payload.is_error();
    let error_kind = match disposition {
        ToolResultDisposition::Fatal => Some(ToolErrorKind::Fatal),
        ToolResultDisposition::Recoverable => is_error.then_some(ToolErrorKind::Recoverable),
    };
    match payload {
        WireToolResultPayload::Inline(inline) => ToolResult {
            call_id: inline.call_id.as_str().into(),
            output: Content::Text(inline.result.output.clone()),
            is_error,
            is_fatal: disposition.is_fatal(),
            error_kind,
            token_count: inline.result.tokens,
        },
        WireToolResultPayload::External(external) => ToolResult {
            call_id: external.call_id.as_str().into(),
            output: Content::Text(external.preview.clone()),
            is_error,
            is_fatal: disposition.is_fatal(),
            error_kind,
            token_count: None,
        },
    }
}

/// §7.10 rules 1, 2 and 5 · the configured threshold is the **arbiter** of which arm a result may
/// take, checked before the engine sees anything.
///
/// `PayloadPolicy::inline_threshold_bytes` documents a total partition — "results at or above this
/// size are committed as `External` rather than inline" — so both directions are enforced here:
///
/// - an oversized `Inline` is refused rather than externalised by the kernel. The host must persist
///   before submission, so "reject" is the only answer that keeps rule 5 true.
/// - an undersized `External` is refused too, because it costs a `LoadPayload` round trip to read
///   something that would have fitted in the turn that produced it, and it makes the partition —
///   the one thing a host has to agree with the kernel about — untotal.
///
/// The digest must be one this kernel can *verify*: a page-in is checked by recomputing the digest
/// over the returned body, so a foreign algorithm would admit a payload whose restoration could
/// never be proved. The preview is bounded because it is the part that actually occupies context.
fn check_payload_policy(
    payload: &WireToolResultPayload,
    policy: &super::config::ResolvedPayloadPolicy,
) -> Result<(), KernelFault> {
    let threshold = policy.inline_threshold_bytes as u64;
    match payload {
        WireToolResultPayload::Inline(inline) => {
            let size = inline.result.output.len() as u64;
            if size >= threshold {
                return Err(KernelFault::new(
                    KernelFaultCode::ResourceLimitExceeded,
                    format!(
                        "tool result {} is {size} bytes and this operation's payload policy \
                         externalises at {threshold}; the host persists the body and submits an \
                         external result — the kernel does not spool on its behalf (§7.10)",
                        inline.call_id
                    ),
                ));
            }
            Ok(())
        }
        WireToolResultPayload::External(external) => {
            if !is_verifiable_digest(external.digest.as_str()) {
                return Err(KernelFault::new(
                    KernelFaultCode::MalformedEnvelope,
                    format!(
                        "external tool result {} carries digest {}, which this kernel cannot \
                         verify; a paged-in body is checked by recomputing {}:<64 hex> over it",
                        external.call_id,
                        external.digest,
                        super::record::DIGEST_ALGORITHM
                    ),
                ));
            }
            let size = external.original_size.get();
            if size < threshold {
                return Err(KernelFault::new(
                    KernelFaultCode::MalformedEnvelope,
                    format!(
                        "external tool result {} declares {size} bytes but this operation's \
                         payload policy inlines below {threshold}; the threshold is the single \
                         arbiter of which arm a result takes (§7.10)",
                        external.call_id
                    ),
                ));
            }
            let preview = external.preview.len() as u64;
            if preview > policy.preview_bytes as u64 {
                return Err(KernelFault::new(
                    KernelFaultCode::ResourceLimitExceeded,
                    format!(
                        "external tool result {} carries a {preview}-byte preview and this \
                         operation keeps {} bytes resident",
                        external.call_id, policy.preview_bytes
                    ),
                ));
            }
            Ok(())
        }
    }
}

/// Whether `digest` is a digest this kernel can recompute — `sha256:` plus 64 lowercase hex.
fn is_verifiable_digest(digest: &str) -> bool {
    let Some(hex) = digest.strip_prefix(super::record::DIGEST_ALGORITHM) else {
        return false;
    };
    let Some(hex) = hex.strip_prefix(':') else {
        return false;
    };
    hex.len() == 64
        && hex
            .bytes()
            .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
}

fn core_milestone_result(
    result: &super::effect::MilestoneCheckResult,
) -> crate::types::milestone::MilestoneCheckResult {
    crate::types::milestone::MilestoneCheckResult {
        phase_id: result.phase_id.clone(),
        passed: result.passed,
        reason: (!result.passed).then(|| {
            if result.failed_criteria.is_empty() {
                result.notes.clone()
            } else {
                format!("unmet criteria: {}", result.failed_criteria.join("; "))
            }
        }),
    }
}

/// Project a wire contract skeleton onto the engine's phase cascade.
///
/// The skeleton carries the two things core decides — phase order and unlocks — and nothing else,
/// so the projection fills the rest from the engine's own defaults: no criteria (the host owns
/// them, §5.2), the default `HarnessEval` verifier, unlimited retries, terminate-on-exhaustion.
/// The one lookup here is `unlocks` → capability descriptor, and it cannot fail: `resolve` already
/// proved every id names a declared tool or skill, so the fallback marker is unreachable and
/// exists only to keep the projection total.
fn core_milestone_contract(
    contract: &super::config::VerificationContract,
    config: &ResolvedOperationConfig,
) -> crate::types::milestone::MilestoneContract {
    use crate::types::capability::{CapabilityDescriptor, CapabilityKind as CoreCapabilityKind};
    use crate::types::milestone::{MilestoneContract, MilestonePhase};

    let mut cascade = MilestoneContract::new();
    for phase in &contract.phases {
        let unlocks = phase
            .unlocks
            .iter()
            .map(|id| {
                if let Some(tool) = config.tool_catalog.iter().find(|tool| &tool.name == id) {
                    CapabilityDescriptor::tool(core_tool_schema(tool))
                } else if let Some(skill) = config.skill_catalog.iter().find(|s| &s.name == id) {
                    CapabilityDescriptor::skill(core_skill(skill))
                } else {
                    CapabilityDescriptor::marker(
                        CoreCapabilityKind::Tool,
                        id.as_str(),
                        String::new(),
                    )
                }
            })
            .collect();
        cascade = cascade.phase(MilestonePhase {
            unlocks,
            ..MilestonePhase::new(phase.phase_id.clone())
        });
    }
    cascade
}

fn core_memory_kind(kind: WireMemoryKind) -> crate::mm::memory::MemoryKind {
    match kind {
        WireMemoryKind::User => crate::mm::memory::MemoryKind::User,
        WireMemoryKind::Feedback => crate::mm::memory::MemoryKind::Feedback,
        WireMemoryKind::Project => crate::mm::memory::MemoryKind::Project,
        WireMemoryKind::Reference => crate::mm::memory::MemoryKind::Reference,
    }
}

fn wire_memory_kind_label(kind: WireMemoryKind) -> &'static str {
    core_memory_kind(kind).label()
}

/// The canonical memory binding is **opaque** (§7.8): it is not a tenant, not a namespace and not a
/// path. Host-facing observations still need to say which binding a fact belongs to, so the binding
/// id rides in the namespace slot and the tenant stays empty — the kernel derives no tenant because
/// the contract gives it none.
fn binding_scope(binding_id: &MemoryBindingId) -> crate::mm::memory::MemoryScope {
    crate::mm::memory::MemoryScope::new(String::new(), binding_id.as_str().to_string())
}

/// The audit text of a host executor failure. Classification first, host prose second — a kernel
/// decision was already taken on the kind alone (§7.9), and this is only what the operator reads.
fn host_failure_text(failure: &HostEffectFailure) -> String {
    if failure.message.is_empty() {
        failure.kind.as_str().to_string()
    } else {
        format!("{}: {}", failure.kind.as_str(), failure.message)
    }
}

/// A resolution for an effect this driver has no record of authoring. The transaction already
/// refuses one for an effect that is not pending, so reaching this means the driver's own ledger
/// and the journal disagree — a rebuild-from-records failure, not a host protocol error.
fn unowned_resolution(effect_id: &EffectId, what: &str) -> KernelFault {
    KernelFault::new(
        KernelFaultCode::RecordCorrupted,
        format!(
            "effect {effect_id} resolves a {what} this runtime never authored; the driver's ledger \
             no longer describes the journal — rebuild from the records"
        ),
    )
}

fn truncate_on_char_boundary(text: &str, max_bytes: usize) -> String {
    if text.len() <= max_bytes {
        return text.to_string();
    }
    let mut end = max_bytes;
    while end > 0 && !text.is_char_boundary(end) {
        end -= 1;
    }
    text[..end].to_string()
}

// ---------------------------------------------------------------------------------------------
// engine construction from the resolved configuration
// ---------------------------------------------------------------------------------------------

/// Build the semantic kernel this operation runs on, from the configuration its genesis record
/// froze. Nothing here reads a compile-time default: every value comes off the record, which is
/// what makes a rebuild on a newer binary reproduce the same steps (§15.2).
fn build_engine(config: &ResolvedOperationConfig) -> LoopStateMachine {
    let execution = &config.execution_policy;
    let mut engine = LoopStateMachine::new(SchedulerBudget {
        max_tokens: execution.max_context_tokens,
        max_turns: execution.max_turns,
        max_total_tokens: execution.max_total_tokens.get(),
        max_wall_ms: execution.max_wall_ms.map(WireU64::get),
    });
    if let Some(grant) = config.budget_grant.clone() {
        engine.set_budget_grant(grant);
    }
    let scheduler_policy = config.scheduler_policy;
    engine.set_scheduler_policy(crate::scheduler::policy::SchedulerPolicyConfig {
        version: crate::scheduler::policy::SCHEDULER_POLICY_VERSION,
        critical_path_weight: i64::from(scheduler_policy.critical_path_weight),
        fanout_weight: i64::from(scheduler_policy.fanout_weight),
        age_weight: i64::from(scheduler_policy.age_weight),
        token_cost_weight: i64::from(scheduler_policy.token_cost_weight),
    });

    engine.set_criteria_gate(execution.criteria_gate_enabled);
    engine.set_repeat_fuse(crate::governance::repeat_fuse::RepeatFuseConfig {
        enabled: execution.repeat_fuse.enabled,
        deny_after: execution.repeat_fuse.deny_after,
        terminate_after: execution.repeat_fuse.terminate_after,
    });
    engine.set_entropy_watch(crate::scheduler::entropy::EntropyWatchConfig {
        enabled: execution.entropy_watch.enabled,
        threshold: f64::from(execution.entropy_watch.threshold_ppm.get()) / 1_000_000.0,
        hysteresis: f64::from(execution.entropy_watch.hysteresis_ppm.get()) / 1_000_000.0,
        cooldown_turns: execution.entropy_watch.cooldown_turns,
        notify_model: execution.entropy_watch.notify_model,
    });
    install_live_policies(&mut engine, config);
    engine.set_dispatch_gate_exposed(matches!(
        config.feature_policy.tool_dispatch_gate,
        super::config::ToolDispatchGate::Exposed
    ));
    engine
        .ctx
        .set_memory_enabled(config.feature_policy.memory_enabled);
    engine
        .ctx
        .set_knowledge_enabled(config.feature_policy.knowledge_enabled);
    engine
        .ctx
        .set_plan_tool_enabled(config.feature_policy.plan_tool_enabled);
    // §7.6 · the declared skill catalog is what makes `ActivateSkill` checkable: a name outside it
    // is a capability mutation with nothing behind it.
    engine
        .ctx
        .set_available_skills(config.skill_catalog.iter().map(core_skill).collect());
    engine.ctx.set_stable_core_tools(
        config
            .feature_policy
            .stable_core_tool_ids
            .iter()
            .map(|id| id.as_str().into()),
    );
    engine.ctx.config.knowledge_budget_ratio =
        config.context_policy.knowledge_budget_ppm.as_ratio();
    engine.ctx.config.collapse_assistant_narration =
        config.context_policy.collapse_old_assistant_narration;
    engine.tools = config.tool_catalog.iter().map(core_tool_schema).collect();
    engine
}

/// Install the four §13.2 live-mutable policies onto an engine.
///
/// One installer, two callers: the genesis build and `HostCommand::ApplyPolicyPatch`. That is the
/// whole reason it exists — a patched policy that took a different code path into the engine than
/// the booted one is how "the same configuration means two things" starts.
///
/// A policy the operation never declared is deliberately **not** installed: §7.3's "the host never
/// said" is a value, distinct from an all-permissive policy the host did not state.
fn install_live_policies(engine: &mut LoopStateMachine, config: &ResolvedOperationConfig) {
    // §7.6 · the P1 gate is only a gate if the operation's declared caps actually reach it. Without
    // this the trap would allow every syscall on the canonical path regardless of what the genesis
    // record froze.
    if let Some(quota) = core_quota(&config.resource_quota) {
        engine.set_resource_quota(quota);
    }
    // The same argument for the tool gate: a governance policy the genesis record froze but the
    // engine never installed would make `RequestApproval` unpublishable and every declared rule
    // inert.
    if let Some(pipeline) = core_governance(&config.governance_policy) {
        engine.set_governance(pipeline);
    }
    engine.set_signal_policy(
        config.signal_policy.queue_max as usize,
        config.signal_policy.ttl_ms.map(WireU64::get),
        config.signal_policy.deadline_escalation,
    );
    // The two semantic ladders. Before this existed the resolved recovery policy was frozen into
    // the genesis record and then never reached the engine at all, so both the booted policy and
    // `ReplaceRecoveryPolicy` were inert and the engine's own compile-time defaults decided how
    // long a ladder ran — the exact "the record says one thing, the run does another" drift §15.2
    // forbids.
    engine.set_recovery_limits(
        config.recovery_policy.provider_recovery_attempts,
        config.recovery_policy.output_recovery_attempts,
    );
}

/// `None` when the operation declared no axis at all. §7.3: "the host never said" is a value, and
/// it is *not* the same as an all-uncapped quota — an installed quota makes the workflow budget
/// observable, which is a statement the host did not make.
fn core_quota(
    quota: &super::config::ResourceQuota,
) -> Option<crate::governance::quota::ResourceQuota> {
    if quota == &super::config::ResourceQuota::default() {
        return None;
    }
    Some(crate::governance::quota::ResourceQuota {
        max_concurrent_subagents: quota.max_concurrent_subagents,
        max_total_subagents: quota.max_total_subagents,
        max_spawn_depth: quota.max_spawn_depth,
        memory_writes_per_window: quota
            .memory_writes_per_window
            .as_ref()
            .map(|window| (window.max_events, window.window_ms.get())),
        max_workflow_nodes: quota.max_workflow_nodes.map(|max| max as usize),
    })
}

/// `None` when the operation declared no governance at all. Same "the host never said" rule as
/// [`core_quota`]: an installed all-allow pipeline is a statement the host did not make, and it
/// would silently change what a tool call means (every call would pass a gate that does not exist).
fn core_governance(
    policy: &super::config::ResolvedGovernancePolicy,
) -> Option<crate::governance::pipeline::GovernancePipeline> {
    use super::command::{ParamConstraint as WireConstraint, PolicyAction};
    use crate::governance::constraint::{ConstraintRule, ParamConstraint as CoreConstraint};
    use crate::governance::permission::PermissionRule;
    use crate::governance::rate_limit::RateLimit;

    if policy.default_action == PolicyAction::Allow
        && policy.rules.is_empty()
        && policy.vetoed_tools.is_empty()
        && policy.rate_limits.is_empty()
        && policy.constraints.is_empty()
    {
        return None;
    }
    let mut pipeline = crate::governance::pipeline::GovernancePipeline::new(core_policy_action(
        policy.default_action,
    ));
    for rule in &policy.rules {
        pipeline.permission.add_rule(PermissionRule {
            tool_pattern: rule.tool_pattern.as_str().into(),
            action: core_policy_action(rule.action),
        });
    }
    for tool in &policy.vetoed_tools {
        pipeline.veto.block_tool(tool.clone());
    }
    for limit in &policy.rate_limits {
        pipeline.rate_limiter.set_limit(
            limit.tool.clone(),
            RateLimit {
                max_calls: limit.max_calls,
                window_ms: limit.window_ms.get(),
            },
        );
    }
    for constraint in &policy.constraints {
        let rule = match constraint {
            WireConstraint::Required(_) => ConstraintRule::Required,
            WireConstraint::Enum(spec) => ConstraintRule::Enum(spec.values.clone()),
            // §7.1.1 · the wire carries fixed-point micro-units so a bound is replayable; the
            // validator's own arithmetic is float, and this is the single conversion point.
            WireConstraint::Range(spec) => ConstraintRule::Range {
                min: spec.min_micros.map(|micros| micros as f64 / 1_000_000.0),
                max: spec.max_micros.map(|micros| micros as f64 / 1_000_000.0),
            },
        };
        pipeline.constraints.add(CoreConstraint {
            tool_name: constraint.tool().to_string(),
            param_path: constraint.param_path().to_string(),
            rule,
        });
    }
    Some(pipeline)
}

fn core_policy_action(
    action: super::command::PolicyAction,
) -> crate::governance::permission::PermissionAction {
    use crate::governance::permission::PermissionAction;
    match action {
        super::command::PolicyAction::Allow => PermissionAction::Allow,
        super::command::PolicyAction::Deny => PermissionAction::Deny,
        super::command::PolicyAction::AskUser => PermissionAction::AskUser,
    }
}

fn core_skill(skill: &super::config::SkillMetadata) -> crate::types::skill::SkillMetadata {
    crate::types::skill::SkillMetadata {
        name: skill.name.as_str().into(),
        description: skill.description.clone(),
        when_to_use: skill.when_to_use.clone(),
        allowed_tools: skill
            .allowed_tools
            .iter()
            .map(|tool| tool.as_str().into())
            .collect(),
        effort: skill.effort,
        estimated_tokens: skill.estimated_tokens.unwrap_or(0),
    }
}

fn core_tool_schema(schema: &WireToolSchema) -> crate::types::message::ToolSchema {
    crate::types::message::ToolSchema {
        name: schema.name.as_str().into(),
        description: schema.description.clone(),
        parameters: schema.parameters.get().clone(),
    }
}

fn malformed(error: super::scalar::WireScalarError) -> KernelFault {
    KernelFault::new(KernelFaultCode::MalformedEnvelope, error.message)
}

#[cfg(test)]
mod tests {
    use std::fs;
    use std::path::PathBuf;

    use serde_json::{Value, json};

    use super::*;
    use crate::runtime::kernel::wire::checkpoint::{
        CanonicalInput, CheckpointCandidate, CheckpointDraft, KernelCheckpoint,
    };
    use crate::runtime::kernel::wire::config::ConfigDefaults;
    use crate::runtime::kernel::wire::config::TailBounds;
    use crate::runtime::kernel::wire::config::{
        EntropyWatchPolicy, ExecutionPolicy, HostEffectSupport,
        MilestonePhase as WireMilestonePhase, OperationConfig,
        VerificationContract as WireVerificationContract,
    };
    use crate::runtime::kernel::wire::effect::{
        EffectSucceeded, TaskLaunchOutcome, TaskLaunchStarted, TaskLaunchStatus,
        TasksSpawnedSuccess,
    };
    use crate::runtime::kernel::wire::envelope::{
        ConfigureOperation, DeliverExternalEvent, KernelInput, ResolveEffect, StartOperation,
        WireEnvelope,
    };
    use crate::runtime::kernel::wire::event::{ChildResult, DeliverSignal, LogicalSignal};
    use crate::runtime::kernel::wire::fault::PrepareToken;
    use crate::runtime::kernel::wire::record::{
        KernelRecord, RecordPreparation, verify_record_chain,
    };
    use crate::runtime::kernel::wire::restore::{
        RestoreCost, RestoredOperation, restore_operation,
    };
    use crate::runtime::kernel::wire::root::{
        RootAgentEntry, RootWorkflowEntry, WorkflowNode as WireNode,
    };
    use crate::runtime::kernel::wire::scalar::{
        AttemptId as WireAttemptId, DeliveryId, InputId, Ppm, SignalId,
    };
    use crate::runtime::kernel::wire::transaction::{
        CheckpointBoundary, CommittedTransition, InMemoryRecordIndex, KernelTransaction,
        TailPressure,
    };
    use crate::scheduler::tcb::TaskLifecycle;

    const OPERATION: &str = "op-driver-1";

    // -----------------------------------------------------------------------------------------
    // §8.2 host loop: prepare → (CAS append) → commit → fold
    // -----------------------------------------------------------------------------------------

    /// The whole durable path, exactly as a host runs it. Nothing here reaches into the driver
    /// behind the transaction's back: a step is planned by the driver, appended by the "host"
    /// (this in-memory journal), committed by the transaction, and only then folded into the
    /// driver's root-kind/focus state.
    struct Runtime {
        tx: KernelTransaction<PlannedStep, InMemoryRecordIndex>,
        driver: CanonicalOperationDriver,
        journal: Vec<KernelRecord>,
        last_observations: Vec<KernelObservation>,
        /// What the restore that produced this runtime read, when it came from one.
        restore_cost: Option<RestoreCost>,
    }

    impl Runtime {
        fn new() -> Self {
            Self {
                tx: KernelTransaction::new(ConfigDefaults::default(), InMemoryRecordIndex::new()),
                driver: CanonicalOperationDriver::new(),
                journal: Vec::new(),
                last_observations: Vec::new(),
                restore_cost: None,
            }
        }

        fn prepare(&mut self, envelope: &WireEnvelope) -> RecordPreparation<PlannedStep> {
            let Self { tx, driver, .. } = self;
            tx.prepare(envelope, |context| driver.plan(context))
        }

        fn append_and_commit(
            &mut self,
            preparation: RecordPreparation<PlannedStep>,
        ) -> CommittedTransition<PlannedStep> {
            let token: PrepareToken = preparation
                .token()
                .unwrap_or_else(|| {
                    panic!("expected a prepared step, got {:?}", preparation.fault())
                })
                .clone();
            let head = preparation.record().unwrap().record_digest().clone();
            let committed = self.tx.commit(&token, &head).expect("commit must succeed");
            self.journal.push(committed.record.clone());
            self.last_observations = committed.step.observations.clone();
            committed
        }

        fn submit(&mut self, envelope: &WireEnvelope) -> CommittedTransition<PlannedStep> {
            let preparation = self.prepare(envelope);
            let committed = self.append_and_commit(preparation);
            self.driver
                .note_committed(committed.step_seq)
                .expect("the driver folds the step it planned");
            committed
        }

        /// Same durable path, but the step comes from a planner the *test* supplies. Used for the
        /// two transitions Task 9 deliberately leaves to Task 10/12 — the P1 syscall that starts a
        /// nested workflow, and the provider resolution that frees the pending provider effect.
        fn submit_planned<F>(
            &mut self,
            envelope: &WireEnvelope,
            plan: F,
        ) -> CommittedTransition<PlannedStep>
        where
            F: FnOnce(
                &mut CanonicalOperationDriver,
                &PlanContext<'_>,
            ) -> Result<PlannedStep, KernelFault>,
        {
            let preparation = {
                let Self { tx, driver, .. } = self;
                tx.prepare(envelope, |context| plan(driver, context))
            };
            self.append_and_commit(preparation)
        }

        fn reject(&mut self, envelope: &WireEnvelope) -> KernelFault {
            let preparation = self.prepare(envelope);
            preparation
                .fault()
                .unwrap_or_else(|| panic!("expected a rejection, got a prepared step"))
                .clone()
        }

        /// §12.2 · the host's own restore call: a checkpoint blob plus the journal records above the
        /// step it covers. Nothing else — in particular, **not** the records the checkpoint covers,
        /// which is what makes the cost assertions meaningful.
        fn restore(&self, checkpoint: &KernelCheckpoint) -> Runtime {
            let after: Vec<KernelRecord> = self
                .journal
                .iter()
                .filter(|record| record.step_seq().get() > checkpoint.through_step_seq().get())
                .cloned()
                .collect();
            Self::restore_with(Some(checkpoint), &after)
        }

        fn restore_with(
            checkpoint: Option<&KernelCheckpoint>,
            records: &[KernelRecord],
        ) -> Runtime {
            let RestoredOperation {
                transaction,
                driver,
                cost,
            } = restore_operation(
                checkpoint,
                records,
                ConfigDefaults::default(),
                InMemoryRecordIndex::from_records(records),
            )
            .expect("the restore ladder runs to completion");
            Runtime {
                tx: transaction,
                driver,
                journal: Vec::new(),
                last_observations: Vec::new(),
                restore_cost: Some(cost),
            }
        }

        /// The whole journal, as the host holds it.
        fn journal_from(&self, checkpoint: &KernelCheckpoint) -> Vec<KernelRecord> {
            self.journal
                .iter()
                .filter(|record| record.step_seq().get() > checkpoint.through_step_seq().get())
                .cloned()
                .collect()
        }

        fn pending_effect_kinds(&self) -> Vec<EffectKindTag> {
            self.tx.pending_effects().map(|e| e.tag()).collect()
        }

        fn observations(&self) -> &[KernelObservation] {
            &self.last_observations
        }

        /// §12.3 · exactly the host's call: project the driver's three partitions, hand them to
        /// the transaction, get a candidate. Nothing here reaches around either layer.
        fn checkpoint(&self) -> CheckpointCandidate {
            self.tx
                .checkpoint_candidate(self.driver.project_logical_state())
                .expect("a configured operation has a logical state to checkpoint")
        }
    }

    // -----------------------------------------------------------------------------------------
    // envelopes
    // -----------------------------------------------------------------------------------------

    fn operation() -> OperationId {
        OperationId::new(OPERATION).unwrap()
    }

    fn envelope(id: &str, observed_at_ms: u64, input: KernelInput) -> WireEnvelope {
        WireEnvelope::new(
            operation(),
            InputId::new(id).unwrap(),
            WireU64::new(observed_at_ms),
            input,
        )
    }

    fn boot_config(supported: impl IntoIterator<Item = EffectKindTag>) -> OperationConfig {
        OperationConfig {
            execution_policy: Some(ExecutionPolicy {
                max_turns: Some(12),
                ..ExecutionPolicy::default()
            }),
            host_effect_support: HostEffectSupport::new(supported),
            ..OperationConfig::default()
        }
    }

    fn configure() -> WireEnvelope {
        configure_supporting([EffectKindTag::CallProvider, EffectKindTag::SpawnTasks])
    }

    fn configure_supporting(supported: impl IntoIterator<Item = EffectKindTag>) -> WireEnvelope {
        envelope(
            "in-configure",
            1_700_000_000_000,
            KernelInput::ConfigureOperation(ConfigureOperation {
                config: boot_config(supported),
            }),
        )
    }

    fn agent_start(id: &str, observed_at_ms: u64) -> WireEnvelope {
        envelope(
            id,
            observed_at_ms,
            KernelInput::StartOperation(StartOperation {
                entry: RootEntry::Agent(RootAgentEntry {
                    task: LogicalTask::new("write the research brief"),
                    run_spec: None,
                }),
                initial_context: InitialContext::default(),
            }),
        )
    }

    fn wire_node(node_id: &str, goal: &str, depends_on: &[&str]) -> WireNode {
        WireNode {
            node_id: NodeId::new(node_id).unwrap(),
            task: LogicalTask::new(goal),
            depends_on: depends_on
                .iter()
                .map(|id| NodeId::new(*id).unwrap())
                .collect(),
            run_spec: None,
        }
    }

    fn two_node_spec() -> WireSpec {
        WireSpec {
            name: "brief".to_string(),
            nodes: vec![
                wire_node("collect", "collect the sources", &[]),
                wire_node("write", "write the brief", &["collect"]),
            ],
        }
    }

    fn workflow_start(id: &str, observed_at_ms: u64, spec: WireSpec) -> WireEnvelope {
        envelope(
            id,
            observed_at_ms,
            KernelInput::StartOperation(StartOperation {
                entry: RootEntry::Workflow(RootWorkflowEntry { spec }),
                initial_context: InitialContext::default(),
            }),
        )
    }

    fn spawned(
        id: &str,
        observed_at_ms: u64,
        effect_id: &EffectId,
        tasks: &[&str],
    ) -> WireEnvelope {
        envelope(
            id,
            observed_at_ms,
            KernelInput::ResolveEffect(ResolveEffect {
                effect_id: effect_id.clone(),
                outcome: EffectOutcome::Succeeded(EffectSucceeded {
                    result: EffectSuccess::TasksSpawned(TasksSpawnedSuccess {
                        attempts: tasks
                            .iter()
                            .map(|task| TaskLaunchOutcome {
                                task_id: TaskId::new(*task).unwrap(),
                                attempt_id: WireAttemptId::new(format!("{task}:attempt:1"))
                                    .unwrap(),
                                outcome: TaskLaunchStatus::Started(TaskLaunchStarted {}),
                            })
                            .collect(),
                    }),
                }),
            }),
        )
    }

    fn child_done(id: &str, observed_at_ms: u64, task: &str, output: &str) -> WireEnvelope {
        envelope(
            id,
            observed_at_ms,
            KernelInput::DeliverExternalEvent(DeliverExternalEvent {
                event: ExternalEvent::ChildCompleted(ChildCompleted {
                    task_id: TaskId::new(task).unwrap(),
                    attempt_id: WireAttemptId::new(format!("{task}:attempt:1")).unwrap(),
                    result: ChildResult {
                        status: ChildStatus::Completed,
                        output: Some(output.to_string()),
                        ..ChildResult::default()
                    },
                    parent_requests: Vec::new(),
                }),
            }),
        )
    }

    /// Drives [`CanonicalOperationDriver::begin_nested_workflow`] as a direct API call, on an
    /// envelope that carries no request of its own.
    ///
    /// The real P1 carrier is now the provider resolution (see `provider_result` and the §7.6
    /// tests); these Task 9 tests keep using the direct entry point because what they assert is the
    /// focus/authority arc, which both paths share — `plan_provider_syscalls` reduces
    /// `SubmitWorkflow` onto exactly this code.
    fn syscall_carrier(id: &str, observed_at_ms: u64) -> WireEnvelope {
        envelope(
            id,
            observed_at_ms,
            KernelInput::DeliverExternalEvent(DeliverExternalEvent {
                event: ExternalEvent::DeliverSignal(DeliverSignal {
                    delivery_id: DeliveryId::new(format!("delivery-{id}")).unwrap(),
                    attempt: 1,
                    signal: LogicalSignal::new(SignalId::new(format!("sig-{id}")).unwrap()),
                }),
            }),
        )
    }

    fn effect_id(step_seq: WireU64) -> EffectId {
        EffectId::new(format!("{OPERATION}:step:{step_seq}:effect:0")).unwrap()
    }

    fn effect_id_at(step_seq: WireU64, index: u32) -> EffectId {
        EffectId::new(format!("{OPERATION}:step:{step_seq}:effect:{index}")).unwrap()
    }

    // -----------------------------------------------------------------------------------------
    // §7.6 · syscall envelopes
    // -----------------------------------------------------------------------------------------

    fn syscall_tool_catalog() -> Vec<WireToolSchema> {
        SYSCALL_TOOL_NAMES
            .iter()
            .chain(std::iter::once(&"search"))
            .map(|name| WireToolSchema {
                name: (*name).to_string(),
                description: String::new(),
                parameters: Default::default(),
            })
            .collect()
    }

    /// A configuration whose operation can actually reach every P1 syscall: the meta-tool surface
    /// is in the catalog, memory is bound read+write, one skill is declared, and the host supports
    /// the effects the memory syscalls publish.
    fn syscall_config() -> WireEnvelope {
        syscall_config_with(|_| {})
    }

    /// The same configuration with one edit applied — how the effect-resolution tests declare the
    /// extra host support (approval, page-out, milestone) or a governance policy their arc needs.
    fn syscall_config_with(edit: impl FnOnce(&mut OperationConfig)) -> WireEnvelope {
        use crate::runtime::kernel::wire::config::{
            MemoryPolicy, ResourceQuota, SkillMetadata as WireSkill,
        };
        use crate::runtime::kernel::wire::effect::{MemoryAccessBinding, MemoryCapabilities};
        use crate::runtime::kernel::wire::scalar::MemoryBindingId;

        let mut config = {
            OperationConfig {
                execution_policy: Some(ExecutionPolicy {
                    max_turns: Some(12),
                    ..ExecutionPolicy::default()
                }),
                host_effect_support: HostEffectSupport::new([
                    EffectKindTag::CallProvider,
                    EffectKindTag::ExecuteTools,
                    EffectKindTag::LoadPayload,
                    EffectKindTag::SpawnTasks,
                    EffectKindTag::PreemptTasks,
                    EffectKindTag::PersistMemory,
                    EffectKindTag::QueryMemory,
                ]),
                tool_catalog: syscall_tool_catalog(),
                skill_catalog: vec![WireSkill {
                    name: "debug".to_string(),
                    description: "debug helper".to_string(),
                    when_to_use: None,
                    allowed_tools: Vec::new(),
                    effort: None,
                    estimated_tokens: None,
                }],
                memory_access: Some(MemoryAccessBinding {
                    binding_id: MemoryBindingId::new("mem-binding-1").unwrap(),
                    capabilities: MemoryCapabilities {
                        read: true,
                        write: true,
                    },
                }),
                memory_policy: Some(MemoryPolicy {
                    retrieval_top_k: Some(4),
                    ..MemoryPolicy::default()
                }),
                resource_quota: Some(ResourceQuota {
                    max_workflow_nodes: Some(3),
                    ..ResourceQuota::default()
                }),
                ..OperationConfig::default()
            }
        };
        edit(&mut config);
        envelope(
            "in-configure",
            1_700_000_000_000,
            KernelInput::ConfigureOperation(ConfigureOperation { config }),
        )
    }

    fn tool_call(call_id: &str, name: &str, arguments: Value) -> WireToolCall {
        WireToolCall {
            call_id: super::super::scalar::CallId::new(call_id).unwrap(),
            name: name.to_string(),
            arguments: super::super::scalar::BoundedJson::new(arguments).unwrap(),
        }
    }

    /// A provider result carrying tool calls — the only wire shape from which a `ProviderTool`
    /// causation can be derived.
    fn provider_result(
        id: &str,
        observed_at_ms: u64,
        effect: &EffectId,
        calls: Vec<WireToolCall>,
    ) -> WireEnvelope {
        envelope(
            id,
            observed_at_ms,
            KernelInput::ResolveEffect(ResolveEffect {
                effect_id: effect.clone(),
                outcome: EffectOutcome::Succeeded(EffectSucceeded {
                    result: EffectSuccess::Provider(super::super::effect::ProviderSuccess {
                        outcome: super::super::effect::ProviderOutcome::Completed(
                            super::super::effect::ProviderCompleted {
                                message: ProviderMessage {
                                    role: MessageRole::Assistant,
                                    content: String::new(),
                                    tool_calls: calls,
                                    tool_call_id: None,
                                    tokens: None,
                                },
                                observed_input_tokens: None,
                                observed_output_tokens: None,
                                stop_reason: None,
                            },
                        ),
                    }),
                }),
            }),
        )
    }

    fn child_done_with(
        id: &str,
        observed_at_ms: u64,
        task: &str,
        attempt: &str,
        requests: Vec<SyscallRequest>,
    ) -> WireEnvelope {
        envelope(
            id,
            observed_at_ms,
            KernelInput::DeliverExternalEvent(DeliverExternalEvent {
                event: ExternalEvent::ChildCompleted(ChildCompleted {
                    task_id: TaskId::new(task).unwrap(),
                    attempt_id: WireAttemptId::new(attempt).unwrap(),
                    result: ChildResult {
                        status: ChildStatus::Completed,
                        output: Some("done".to_string()),
                        ..ChildResult::default()
                    },
                    parent_requests: requests,
                }),
            }),
        )
    }

    fn node_args(nodes: &[WireNode]) -> Value {
        json!({ "nodes": serde_json::to_value(nodes).unwrap() })
    }

    /// `(operation, subject, reason)` of every structured rejection the last transition recorded.
    fn rejections(runtime: &Runtime) -> Vec<(String, Option<String>, String)> {
        runtime
            .observations()
            .iter()
            .filter_map(|observation| match observation {
                KernelObservation::ControlRequestRejected {
                    operation,
                    subject,
                    reason,
                    ..
                } => Some((operation.clone(), subject.clone(), reason.clone())),
                _ => None,
            })
            .collect()
    }

    fn sole_effect(committed: &CommittedTransition<PlannedStep>) -> &KernelEffect {
        let effects = committed.published_effects();
        assert_eq!(effects.len(), 1, "expected exactly one published effect");
        &effects[0]
    }

    // -----------------------------------------------------------------------------------------
    // fixture: agent-configure-is-genesis
    // -----------------------------------------------------------------------------------------

    #[test]
    fn the_first_accepted_input_must_be_the_configuration() {
        let mut runtime = Runtime::new();

        // a business input before any configuration
        let fault = runtime.reject(&agent_start("in-early-start", 1_700_000_000_100));
        assert_eq!(fault.code, KernelFaultCode::InvalidLifecycle);
        assert_eq!(runtime.tx.head(), None, "a rejection moves nothing");
        assert_eq!(runtime.tx.lifecycle(), OperationLifecycle::Created);
        assert!(
            runtime.driver.engine().is_none(),
            "no semantic kernel exists"
        );

        let genesis = runtime.submit(&configure());
        assert_eq!(genesis.step_seq, WireU64::ZERO);
        assert_eq!(
            genesis.record.previous_record_digest(),
            None,
            "the genesis record has no predecessor (§8.1)"
        );
        assert_eq!(runtime.tx.lifecycle(), OperationLifecycle::Configured);
        assert!(runtime.driver.engine().is_some());
        assert_eq!(
            runtime.driver.root_kind(),
            None,
            "configuring is not starting"
        );

        // the genesis record stores the *resolved* configuration, not the sparse input
        let stored = genesis.record.normalized_input().unwrap();
        let resolved = stored
            .resolved_config()
            .expect("genesis carries the config");
        assert_eq!(resolved.execution_policy.max_turns, 12);
        assert_eq!(
            resolved.execution_policy.max_context_tokens,
            ConfigDefaults::default()
                .baseline
                .execution_policy
                .max_context_tokens,
            "every default this operation runs on is frozen in its first record"
        );
    }

    // -----------------------------------------------------------------------------------------
    // fixture: agent-root-start-is-atomic + agent-start-issues-model-turn
    // -----------------------------------------------------------------------------------------

    #[test]
    fn an_agent_root_start_is_one_atomic_input_that_issues_the_model_turn() {
        let mut runtime = Runtime::new();
        runtime.submit(&configure());
        let started = runtime.submit(&agent_start("in-start", 1_700_000_001_000));

        // root kind and initial context are fixed by this one accepted input
        assert_eq!(started.step.root_kind, Some(RootKind::Agent));
        assert_eq!(runtime.driver.root_kind(), Some(RootKind::Agent));
        assert_eq!(
            runtime.driver.focus(),
            Some(&ExecutionFocus::agent_turn(root_task_id())),
        );
        assert_eq!(runtime.tx.lifecycle(), OperationLifecycle::Running);

        // exactly one pending effect, and it is the provider call
        let effect = sole_effect(&started);
        assert_eq!(effect.tag(), EffectKindTag::CallProvider);
        assert_eq!(
            effect.effect_id,
            effect_id(started.step_seq),
            "the effect id is minted by the kernel from the step it belongs to"
        );
        assert_eq!(
            effect.causation_input_id.as_str(),
            "in-start",
            "every effect names the accepted input that produced it"
        );
        assert_eq!(
            runtime.pending_effect_kinds(),
            vec![EffectKindTag::CallProvider]
        );

        // the rendered context is real: the goal reached the P3 context VM
        let EffectKind::CallProvider(call) = &effect.effect else {
            panic!("expected a provider call");
        };
        let rendered = format!(
            "{}{}",
            call.context.system_stable, call.context.system_knowledge
        );
        let state_turn = call
            .context
            .state_turn
            .as_ref()
            .map(|turn| turn.content.clone())
            .unwrap_or_default();
        assert!(
            rendered.contains("research brief") || state_turn.contains("research brief"),
            "the root task's goal must be in the rendered context, not merely stored"
        );
        assert!(
            !call.context.turns.is_empty(),
            "the start seeded a first turn"
        );
    }

    #[test]
    fn a_second_root_start_is_refused_with_zero_mutation() {
        let mut runtime = Runtime::new();
        runtime.submit(&configure());
        let started = runtime.submit(&agent_start("in-start", 1_700_000_001_000));

        let before = (
            runtime.tx.head(),
            runtime.tx.lifecycle(),
            runtime.pending_effect_kinds(),
            runtime.driver.root_kind(),
            runtime.driver.focus().cloned(),
        );

        let fault = runtime.reject(&agent_start("in-start-again", 1_700_000_002_000));
        assert_eq!(fault.code, KernelFaultCode::InvalidLifecycle);

        let after = (
            runtime.tx.head(),
            runtime.tx.lifecycle(),
            runtime.pending_effect_kinds(),
            runtime.driver.root_kind(),
            runtime.driver.focus().cloned(),
        );
        assert_eq!(before, after, "a refused root start moves nothing");
        assert_eq!(runtime.journal.len(), 2, "no third record exists");
        assert_eq!(started.step.root_kind, Some(RootKind::Agent));
    }

    #[test]
    fn a_workflow_root_start_after_an_agent_root_cannot_re_root_the_operation() {
        let mut runtime = Runtime::new();
        runtime.submit(&configure());
        runtime.submit(&agent_start("in-start", 1_700_000_001_000));

        let fault = runtime.reject(&workflow_start(
            "in-reroot",
            1_700_000_002_000,
            two_node_spec(),
        ));
        assert_eq!(fault.code, KernelFaultCode::InvalidLifecycle);
        assert_eq!(
            runtime.driver.root_kind(),
            Some(RootKind::Agent),
            "the root kind is immutable for the operation's lifetime (§6.1.5)"
        );
    }

    /// DEC-8 fail-closed, at the root start rather than at the first emission. `call_provider`
    /// support is already mandatory at configuration time (every operation reaches a provider
    /// call), so the reachable half of the rule is a workflow root on a host that cannot spawn.
    #[test]
    fn a_workflow_root_is_refused_when_the_host_cannot_spawn_tasks() {
        let mut runtime = Runtime::new();
        runtime.submit(&configure_supporting([EffectKindTag::CallProvider]));
        let fault = runtime.reject(&workflow_start(
            "in-start",
            1_700_000_001_000,
            two_node_spec(),
        ));
        assert_eq!(fault.code, KernelFaultCode::UnsupportedEffect);
        assert_eq!(runtime.driver.root_kind(), None);
        assert_eq!(runtime.tx.lifecycle(), OperationLifecycle::Configured);
        assert_eq!(runtime.journal.len(), 1, "only the genesis record exists");
    }

    // -----------------------------------------------------------------------------------------
    // fixture: workflow-root-entry-is-atomic
    // -----------------------------------------------------------------------------------------

    #[test]
    fn a_workflow_root_start_spawns_tasks_and_never_calls_the_provider() {
        let mut runtime = Runtime::new();
        runtime.submit(&configure());
        let started = runtime.submit(&workflow_start(
            "in-start",
            1_700_000_001_000,
            two_node_spec(),
        ));

        assert_eq!(started.step.root_kind, Some(RootKind::Workflow));
        let effect = sole_effect(&started);
        assert_eq!(
            effect.tag(),
            EffectKindTag::SpawnTasks,
            "a workflow root's first effect is a task spawn (§10.1)"
        );
        assert_eq!(
            runtime.pending_effect_kinds(),
            vec![EffectKindTag::SpawnTasks],
            "no provider effect is published, so none can be overwritten by a workflow load"
        );

        // the DAG's identity is kernel-minted and its first ready node is the only launch
        let EffectKind::SpawnTasks(spawn) = &effect.effect else {
            panic!("expected a task spawn");
        };
        assert_eq!(spawn.tasks.len(), 1, "only `collect` is ready");
        assert_eq!(spawn.tasks[0].node_id.as_str(), "collect");
        assert_eq!(spawn.tasks[0].task_id.as_str(), "wf-node0");
        assert!(
            !spawn.tasks[0].launch_token.as_str().is_empty(),
            "the launch token exists as a committed fact before the host launches anything"
        );

        // focus is the root controller, with no parent to restore
        assert_eq!(
            runtime.driver.focus(),
            Some(&ExecutionFocus::workflow_controller(
                runtime.driver.workflow_id().unwrap().clone(),
                None
            ))
        );
        assert!(
            !runtime.driver.focus().unwrap().is_nested_in_agent(),
            "the root workflow is not nested in an agent"
        );
    }

    #[test]
    fn a_workflow_launch_preserves_logical_context_inheritance() {
        let mut spec = two_node_spec();
        spec.nodes[0].run_spec = Some(LogicalAgentSpec {
            context_inheritance: Some(WireContextInheritance::SystemOnly),
            ..LogicalAgentSpec::new("collect the sources")
        });

        let mut runtime = Runtime::new();
        runtime.submit(&configure());
        let started = runtime.submit(&workflow_start("in-start", 1_700_000_001_000, spec));

        let EffectKind::SpawnTasks(spawn) = &sole_effect(&started).effect else {
            panic!("expected a task spawn");
        };
        assert_eq!(
            spawn.tasks[0].spec.context_inheritance,
            Some(WireContextInheritance::SystemOnly),
        );
    }

    #[test]
    fn a_workflow_root_with_no_nodes_has_no_first_effect_and_is_refused() {
        let mut runtime = Runtime::new();
        runtime.submit(&configure());
        let fault = runtime.reject(&workflow_start(
            "in-start",
            1_700_000_001_000,
            WireSpec::default(),
        ));
        assert_eq!(fault.code, KernelFaultCode::InvalidConfig);
        assert_eq!(runtime.driver.root_kind(), None);
    }

    #[test]
    fn a_workflow_spec_whose_dependency_names_no_declared_node_is_refused() {
        let mut runtime = Runtime::new();
        runtime.submit(&configure());
        let spec = WireSpec {
            name: "broken".to_string(),
            nodes: vec![wire_node("write", "write", &["collect"])],
        };
        let fault = runtime.reject(&workflow_start("in-start", 1_700_000_001_000, spec));
        assert_eq!(fault.code, KernelFaultCode::InvalidConfig);
        assert_eq!(runtime.driver.root_kind(), None);
        assert_eq!(runtime.journal.len(), 1, "only the genesis record exists");
    }

    // -----------------------------------------------------------------------------------------
    // fixture: workflow-root-completion-commits-terminal
    // -----------------------------------------------------------------------------------------

    /// The full §10.1 path: configure → root start → spawn → ack → completions → workflow terminal,
    /// with no `LoadWorkflow`, no placeholder agent run and no host-privileged `CompleteRun`.
    fn drive_workflow_root_to_terminal() -> Runtime {
        let mut runtime = Runtime::new();
        runtime.submit(&configure());
        let started = runtime.submit(&workflow_start(
            "in-start",
            1_700_000_001_000,
            two_node_spec(),
        ));

        runtime.submit(&spawned(
            "in-ack-1",
            1_700_000_002_000,
            &effect_id(started.step_seq),
            &["wf-node0"],
        ));
        let advanced = runtime.submit(&child_done(
            "in-done-1",
            1_700_000_003_000,
            "wf-node0",
            "sources collected",
        ));
        let second = sole_effect(&advanced);
        assert_eq!(second.tag(), EffectKindTag::SpawnTasks);
        let EffectKind::SpawnTasks(spawn) = &second.effect else {
            panic!("expected a task spawn");
        };
        assert_eq!(spawn.tasks[0].node_id.as_str(), "write");

        runtime.submit(&spawned(
            "in-ack-2",
            1_700_000_004_000,
            &effect_id(advanced.step_seq),
            &["wf-node1"],
        ));
        runtime.submit(&child_done(
            "in-done-2",
            1_700_000_005_000,
            "wf-node1",
            "brief written",
        ));
        runtime
    }

    #[test]
    fn a_root_workflow_completion_commits_the_workflow_terminal_itself() {
        let runtime = drive_workflow_root_to_terminal();

        let terminal = runtime.tx.terminal().expect("the run terminated");
        let KernelTerminal::Workflow(workflow) = terminal else {
            panic!("a workflow root terminates with a workflow terminal, got {terminal:?}");
        };
        assert_eq!(workflow.outcome.status, WorkflowStatus::Completed);
        assert_eq!(
            workflow
                .outcome
                .completed_nodes
                .iter()
                .map(|node| node.as_str())
                .collect::<Vec<_>>(),
            vec!["collect", "write"],
            "the terminal names the wire node ids the host declared"
        );
        assert!(workflow.outcome.failed_nodes.is_empty());
        assert_eq!(runtime.tx.lifecycle(), OperationLifecycle::Completed);
        assert_eq!(
            runtime.pending_effect_kinds(),
            Vec::<EffectKindTag>::new(),
            "a root workflow issues no provider call after it completes"
        );
    }

    #[test]
    fn a_completion_naming_an_attempt_the_kernel_never_minted_is_refused() {
        let mut runtime = Runtime::new();
        runtime.submit(&configure());
        let started = runtime.submit(&workflow_start(
            "in-start",
            1_700_000_001_000,
            two_node_spec(),
        ));
        runtime.submit(&spawned(
            "in-ack-1",
            1_700_000_002_000,
            &effect_id(started.step_seq),
            &["wf-node0"],
        ));

        let forged = envelope(
            "in-forged",
            1_700_000_003_000,
            KernelInput::DeliverExternalEvent(DeliverExternalEvent {
                event: ExternalEvent::ChildCompleted(ChildCompleted {
                    task_id: TaskId::new("wf-node0").unwrap(),
                    attempt_id: WireAttemptId::new("wf-node0:attempt:7").unwrap(),
                    result: ChildResult::default(),
                    parent_requests: Vec::new(),
                }),
            }),
        );
        let before = runtime.pending_effect_kinds();
        let fault = runtime.reject(&forged);
        assert_eq!(
            fault.code,
            KernelFaultCode::InvalidAuthority,
            "a host does not mint or rewrite child identity (§10.4)"
        );
        assert_eq!(runtime.pending_effect_kinds(), before);
        assert!(runtime.tx.terminal().is_none());
    }

    #[test]
    fn a_terminated_root_workflow_refuses_every_later_state_changing_input() {
        let mut runtime = drive_workflow_root_to_terminal();
        let fault = runtime.reject(&child_done(
            "in-done-late",
            1_700_000_006_000,
            "wf-node1",
            "again",
        ));
        assert_eq!(fault.code, KernelFaultCode::InvalidLifecycle);
    }

    // -----------------------------------------------------------------------------------------
    // fixture: workflow-no-stack-and-root-kind-immutable
    // -----------------------------------------------------------------------------------------

    /// Free the pending provider effect so the nested-workflow arc has a clean `call_provider`
    /// slot. The real provider reduction has its own tests below; this planner only clears the
    /// registration so the focus assertions read against a quiet step.
    fn provider_settled(
        driver: &mut CanonicalOperationDriver,
        _context: &PlanContext<'_>,
    ) -> Result<PlannedStep, KernelFault> {
        Ok(PlannedStep::quiet(
            driver.root_kind(),
            driver.focus().cloned(),
        ))
    }

    fn agent_root_with_nested_workflow() -> (Runtime, TaskId) {
        let mut runtime = Runtime::new();
        runtime.submit(&configure());
        let started = runtime.submit(&agent_start("in-start", 1_700_000_001_000));

        // settle the provider call the agent root issued
        let settle = envelope(
            "in-provider",
            1_700_000_002_000,
            KernelInput::ResolveEffect(ResolveEffect {
                effect_id: effect_id(started.step_seq),
                outcome: EffectOutcome::Succeeded(EffectSucceeded {
                    result: EffectSuccess::Provider(super::super::effect::ProviderSuccess {
                        outcome: super::super::effect::ProviderOutcome::ContextOverflow(
                            super::super::effect::ProviderContextOverflow::default(),
                        ),
                    }),
                }),
            }),
        );
        runtime.submit_planned(&settle, provider_settled);

        let spec = two_node_spec();
        let carrier = syscall_carrier("in-submit-workflow", 1_700_000_003_000);
        let entered = runtime.submit_planned(&carrier, |driver, context| {
            driver.begin_nested_workflow(context, &spec)
        });
        runtime
            .driver
            .note_committed(entered.step_seq)
            .expect("the nested start folds like any other planned step");
        assert_eq!(sole_effect(&entered).tag(), EffectKindTag::SpawnTasks);
        (runtime, root_task_id())
    }

    /// The historical bootstrap replaced a live provider registration with a workflow spawn, so a
    /// pending provider call could disappear without ever being resolved. Here the two effects are
    /// separate registrations and neither evicts the other.
    #[test]
    fn starting_a_workflow_never_overwrites_a_live_provider_effect() {
        let mut runtime = Runtime::new();
        runtime.submit(&configure());
        let started = runtime.submit(&agent_start("in-start", 1_700_000_001_000));
        let provider_effect = sole_effect(&started).effect_id.clone();

        let spec = two_node_spec();
        let carrier = syscall_carrier("in-submit-workflow", 1_700_000_002_000);
        let entered = runtime.submit_planned(&carrier, |driver, context| {
            driver.begin_nested_workflow(context, &spec)
        });
        runtime.driver.note_committed(entered.step_seq).unwrap();

        let mut pending = runtime.pending_effect_kinds();
        pending.sort();
        assert_eq!(
            pending,
            vec![EffectKindTag::CallProvider, EffectKindTag::SpawnTasks],
            "the provider call is still outstanding; the spawn is a second registration"
        );
        assert!(
            runtime
                .tx
                .pending_effects()
                .any(|effect| effect.effect_id == provider_effect),
            "the provider effect kept its identity, so the host can still resolve it"
        );
    }

    #[test]
    fn an_agent_authored_workflow_moves_the_focus_but_never_the_root_kind() {
        let (runtime, parent) = agent_root_with_nested_workflow();

        assert_eq!(
            runtime.driver.root_kind(),
            Some(RootKind::Agent),
            "a syscall never re-roots an operation (§10.2)"
        );
        let focus = runtime.driver.focus().expect("a focus exists");
        assert!(
            focus.is_nested_in_agent(),
            "the focus records the parent agent task it must restore"
        );
        assert_eq!(
            focus,
            &ExecutionFocus::workflow_controller(
                runtime.driver.workflow_id().unwrap().clone(),
                Some(parent),
            )
        );
    }

    #[test]
    fn a_second_workflow_inside_a_workflow_focus_is_an_authority_refusal_with_no_spawn() {
        let (mut runtime, _) = agent_root_with_nested_workflow();

        let before = (
            runtime.tx.head(),
            runtime.pending_effect_kinds(),
            runtime.driver.root_kind(),
            runtime.driver.focus().cloned(),
        );

        let spec = two_node_spec();
        let carrier = syscall_carrier("in-submit-again", 1_700_000_004_000);
        let preparation = {
            let Runtime { tx, driver, .. } = &mut runtime;
            tx.prepare(&carrier, |context| {
                driver.begin_nested_workflow(context, &spec)
            })
        };
        let fault = preparation.fault().expect("expected a refusal").clone();
        assert_eq!(
            fault.code,
            KernelFaultCode::InvalidAuthority,
            "workflows do not stack — depth is at most 1 (§7.4)"
        );

        let after = (
            runtime.tx.head(),
            runtime.pending_effect_kinds(),
            runtime.driver.root_kind(),
            runtime.driver.focus().cloned(),
        );
        assert_eq!(before, after, "the refusal produced no derived action");
    }

    #[test]
    fn a_nested_workflow_completion_restores_the_parent_agent_and_resumes_its_turn() {
        let (mut runtime, parent) = agent_root_with_nested_workflow();
        let spawn_step = runtime.journal.last().unwrap().step_seq();

        runtime.submit(&spawned(
            "in-ack-1",
            1_700_000_004_000,
            &effect_id(spawn_step),
            &["wf-node0"],
        ));
        let advanced = runtime.submit(&child_done(
            "in-done-1",
            1_700_000_005_000,
            "wf-node0",
            "sources collected",
        ));
        runtime.submit(&spawned(
            "in-ack-2",
            1_700_000_006_000,
            &effect_id(advanced.step_seq),
            &["wf-node1"],
        ));
        let finished = runtime.submit(&child_done(
            "in-done-2",
            1_700_000_007_000,
            "wf-node1",
            "brief written",
        ));

        assert!(
            runtime.tx.terminal().is_none(),
            "a nested workflow's completion is not the operation's terminal (§6.1.7)"
        );
        assert_eq!(
            runtime.driver.focus(),
            Some(&ExecutionFocus::agent_turn(parent)),
            "the focus returns to the agent turn it left"
        );
        assert_eq!(
            sole_effect(&finished).tag(),
            EffectKindTag::CallProvider,
            "the parent agent's turn resumes with a provider call"
        );
        assert_eq!(runtime.driver.root_kind(), Some(RootKind::Agent));
    }

    #[test]
    fn a_workflow_root_admits_no_nested_workflow_at_all() {
        let mut runtime = Runtime::new();
        runtime.submit(&configure());
        runtime.submit(&workflow_start(
            "in-start",
            1_700_000_001_000,
            two_node_spec(),
        ));

        let spec = two_node_spec();
        let carrier = syscall_carrier("in-submit", 1_700_000_002_000);
        let preparation = {
            let Runtime { tx, driver, .. } = &mut runtime;
            tx.prepare(&carrier, |context| {
                driver.begin_nested_workflow(context, &spec)
            })
        };
        assert_eq!(
            preparation.fault().unwrap().code,
            KernelFaultCode::InvalidAuthority
        );
    }

    #[test]
    fn a_workflow_roots_focus_never_moves_while_its_dag_runs() {
        let mut runtime = Runtime::new();
        runtime.submit(&configure());
        let started = runtime.submit(&workflow_start(
            "in-start",
            1_700_000_001_000,
            two_node_spec(),
        ));
        let focus = runtime.driver.focus().cloned();

        runtime.submit(&spawned(
            "in-ack-1",
            1_700_000_002_000,
            &effect_id(started.step_seq),
            &["wf-node0"],
        ));
        assert_eq!(
            runtime.driver.focus().cloned(),
            focus,
            "an ack moves nothing"
        );

        let advanced = runtime.submit(&child_done(
            "in-done-1",
            1_700_000_003_000,
            "wf-node0",
            "done",
        ));
        assert_eq!(
            runtime.driver.focus().cloned(),
            focus,
            "a DAG node's agent execution is a child attempt, not a focus change"
        );
        assert_eq!(sole_effect(&advanced).tag(), EffectKindTag::SpawnTasks);
    }

    // -----------------------------------------------------------------------------------------
    // fixture: agent-syscall-caller-is-derived
    // -----------------------------------------------------------------------------------------

    /// Configure → agent root → provider result carrying one meta-tool call. Returns the runtime
    /// and the provider effect the start published.
    fn agent_awaiting_provider() -> (Runtime, EffectId) {
        let mut runtime = Runtime::new();
        runtime.submit(&syscall_config());
        let started = runtime.submit(&agent_start("in-start", 1_700_000_001_000));
        let effect = sole_effect(&started);
        assert_eq!(effect.tag(), EffectKindTag::CallProvider);
        let EffectKind::CallProvider(call) = &effect.effect else {
            panic!("expected a provider call");
        };
        assert!(
            call.tools.iter().any(|tool| tool.name == "start_workflow"),
            "the turn must actually expose the meta-tool the model is about to call"
        );
        (runtime, effect.effect_id.clone())
    }

    #[test]
    fn a_provider_tool_call_derives_its_caller_and_enters_p1() {
        let (mut runtime, provider) = agent_awaiting_provider();

        let spec = serde_json::to_value(two_node_spec()).unwrap();
        let entered = runtime.submit(&provider_result(
            "in-authored",
            1_700_000_002_000,
            &provider,
            vec![tool_call("call-1", "start_workflow", spec)],
        ));

        // the request became a real transition: a DAG was bootstrapped and its first node launched
        let effect = sole_effect(&entered);
        assert_eq!(effect.tag(), EffectKindTag::SpawnTasks);
        assert_eq!(
            runtime.driver.root_kind(),
            Some(RootKind::Agent),
            "a syscall never re-roots an operation (§10.2)"
        );
        assert!(
            runtime.driver.focus().unwrap().is_nested_in_agent(),
            "the focus moved to the workflow controller, under the agent turn it suspended"
        );

        // and the host declared nobody: the accepted input carries no caller field at all
        let stored = entered.record.normalized_input().unwrap();
        let json = serde_json::to_value(&stored).unwrap();
        let text = json.to_string();
        for forbidden in ["submitter_agent_id", "actor_id", "parent_session_id"] {
            assert!(
                !text.contains(forbidden),
                "the canonical input still carries {forbidden}"
            );
        }
    }

    #[test]
    fn a_tool_the_turn_never_exposed_has_no_caller_to_derive() {
        let (mut runtime, provider) = agent_awaiting_provider();

        // An unexposed *task* tool is not an authority claim — it is a call the model made up, and
        // the kernel's fail-closed dispatch gate answers it the way the model is trained to read:
        // a visible error result, no host dispatch, and the turn continues.
        let committed = runtime.submit(&provider_result(
            "in-forged",
            1_700_000_002_000,
            &provider,
            vec![tool_call(
                "call-1",
                "escalate_privileges",
                json!({"nodes": []}),
            )],
        ));
        assert_eq!(
            committed
                .published_effects()
                .iter()
                .map(|effect| effect.tag())
                .collect::<Vec<_>>(),
            vec![EffectKindTag::CallProvider],
            "a phantom tool is never dispatched; the turn is answered and re-asked"
        );

        // The authority half: a *syscall* name the turn did not advertise. Narrow the recorded
        // surface to nothing and ask again — the request is well-formed and still has no caller.
        let next = effect_id(committed.step_seq);
        runtime
            .driver
            .provider_calls
            .get_mut(&next)
            .expect("the driver recorded the turn it published")
            .exposed_tools
            .clear();
        let before = (
            runtime.tx.head(),
            runtime.pending_effect_kinds(),
            runtime.driver.focus().cloned(),
        );
        let fault = runtime
            .driver
            .derive_provider_syscalls(&next, &[tool_call("call-2", "start_workflow", json!({}))])
            .expect_err("a tool the turn never exposed has no causation");
        assert_eq!(fault.code, KernelFaultCode::InvalidAuthority);
        assert!(fault.message.contains("exposed no tool"));

        let after = (
            runtime.tx.head(),
            runtime.pending_effect_kinds(),
            runtime.driver.focus().cloned(),
        );
        assert_eq!(before, after, "a forged causation moves nothing");
    }

    #[test]
    fn a_provider_effect_this_kernel_never_published_has_no_causation() {
        let (runtime, _) = agent_awaiting_provider();
        let unknown = EffectId::new("op-driver-1:step:99:effect:0").unwrap();
        let fault = runtime
            .driver
            .derive_provider_syscalls(
                &unknown,
                &[tool_call("call-1", "start_workflow", json!({}))],
            )
            .expect_err("an unpublished effect names no turn");
        assert_eq!(fault.code, KernelFaultCode::InvalidAuthority);
        assert!(fault.message.contains("not a provider call"));
    }

    #[test]
    fn a_call_id_that_already_produced_a_syscall_cannot_produce_a_second() {
        let (mut runtime, provider) = agent_awaiting_provider();

        // two calls sharing one id inside a single result: the second has no causation left
        let fault = runtime.reject(&provider_result(
            "in-double",
            1_700_000_002_000,
            &provider,
            vec![
                tool_call("call-1", "skill", json!({"name": "debug"})),
                tool_call("call-1", "skill", json!({"name": "debug"})),
            ],
        ));
        assert_eq!(fault.code, KernelFaultCode::InvalidAuthority);
        assert!(fault.message.contains("consumed once"));

        // and a causation that *was* spent stays spent for the operation's lifetime
        runtime.submit(&provider_result(
            "in-skill",
            1_700_000_002_000,
            &provider,
            vec![tool_call("call-1", "skill", json!({"name": "debug"}))],
        ));
        assert!(
            runtime.driver.consumed_calls.contains("call-1"),
            "the spent causation is remembered, so a redelivery under a fresh input id buys nothing"
        );
        assert!(
            !runtime.driver.provider_calls.contains_key(&provider),
            "a resolved provider call is no longer a surface anything can be attributed to"
        );
    }

    #[test]
    fn a_skill_the_operation_never_declared_cannot_be_activated() {
        let (mut runtime, provider) = agent_awaiting_provider();
        runtime.submit(&provider_result(
            "in-skill",
            1_700_000_002_000,
            &provider,
            vec![tool_call(
                "call-1",
                "skill",
                json!({"name": "not-declared"}),
            )],
        ));
        let rejected = rejections(&runtime);
        assert_eq!(rejected.len(), 1, "the refusal is an audit fact");
        assert_eq!(rejected[0].0, "skill");
        assert_eq!(
            rejected[0].1.as_deref(),
            Some(ROOT_TASK_ID),
            "the audit fact names the caller the kernel derived, not one a host supplied"
        );
        assert!(rejected[0].2.contains("declares no skill"));
        assert_eq!(
            runtime.pending_effect_kinds(),
            vec![EffectKindTag::CallProvider],
            "a rejected capability mutation publishes no effect of its own; the turn still \
             continues (the §5k syscall-only continuation)"
        );
    }

    #[test]
    fn an_append_with_no_graph_to_append_to_is_an_audit_fact_not_a_derived_action() {
        let (mut runtime, provider) = agent_awaiting_provider();
        runtime.submit(&provider_result(
            "in-append",
            1_700_000_002_000,
            &provider,
            vec![tool_call(
                "call-1",
                "submit_workflow_nodes",
                node_args(&[wire_node("stray", "stray", &[])]),
            )],
        ));

        let rejected = rejections(&runtime);
        assert_eq!(rejected.len(), 1);
        assert_eq!(rejected[0].0, "submit_workflow_nodes");
        assert_eq!(
            rejected[0].1.as_deref(),
            Some(ROOT_TASK_ID),
            "a provider-tool causation names the task whose turn issued the call"
        );
        assert!(rejected[0].2.contains("no workflow is in flight"));
        assert_eq!(
            runtime.pending_effect_kinds(),
            vec![EffectKindTag::CallProvider],
            "a refused append spawns nothing; the turn continues with the next provider call"
        );
    }

    // -----------------------------------------------------------------------------------------
    // fixture: workflow-dynamic-append-preserves-authority (+ §7.7 GAP-4)
    // -----------------------------------------------------------------------------------------

    /// Workflow root, first node launched and acknowledged.
    fn workflow_root_awaiting_first_child() -> Runtime {
        let mut runtime = Runtime::new();
        runtime.submit(&syscall_config());
        let started = runtime.submit(&workflow_start(
            "in-start",
            1_700_000_001_000,
            two_node_spec(),
        ));
        runtime.submit(&spawned(
            "in-ack-1",
            1_700_000_002_000,
            &effect_id(started.step_seq),
            &["wf-node0"],
        ));
        runtime
    }

    #[test]
    fn parent_requests_are_adjudicated_independently_and_never_undo_the_completion() {
        let mut runtime = workflow_root_awaiting_first_child();

        let good = SyscallRequest::AppendWorkflowNodes(
            super::super::syscall::AppendWorkflowNodesRequest {
                nodes: vec![wire_node("verify", "verify the sources", &[])],
            },
        );
        // batch-relative dependency that names nothing in its own batch — refused on its own merits
        let bad = SyscallRequest::AppendWorkflowNodes(
            super::super::syscall::AppendWorkflowNodesRequest {
                nodes: vec![wire_node("orphan", "orphan", &["nowhere"])],
            },
        );
        let another_good = SyscallRequest::UpdateTask(super::super::syscall::UpdateTaskRequest {
            update: WireTaskUpdate {
                progress: Some("sources collected".to_string()),
                ..WireTaskUpdate::default()
            },
        });

        let advanced = runtime.submit(&child_done_with(
            "in-done-1",
            1_700_000_003_000,
            "wf-node0",
            "wf-node0:attempt:1",
            vec![good, bad, another_good],
        ));

        // the completion committed unconditionally and drained into the next ready batch
        assert_eq!(
            runtime.tx.lifecycle(),
            OperationLifecycle::Running,
            "a denied parent request does not undo the child's execution (GAP-4)"
        );
        let effect = sole_effect(&advanced);
        assert_eq!(effect.tag(), EffectKindTag::SpawnTasks);
        let EffectKind::SpawnTasks(spawn) = &effect.effect else {
            panic!("expected a task spawn");
        };
        let launched: Vec<&str> = spawn
            .tasks
            .iter()
            .map(|task| task.node_id.as_str())
            .collect();
        assert!(
            launched.contains(&"write") && launched.contains(&"verify"),
            "the admitted append reached the next ready batch alongside the original DAG, got \
             {launched:?}"
        );
        assert!(
            !launched.contains(&"orphan"),
            "the refused append produced no derived action"
        );

        // exactly one structured rejection, and the third request was unaffected by the second
        let rejected = rejections(&runtime);
        assert_eq!(rejected.len(), 1, "each request is adjudicated on its own");
        assert_eq!(rejected[0].0, "submit_workflow_nodes");
        assert_eq!(
            rejected[0].1.as_deref(),
            Some("wf-node0"),
            "the refusal names the child attempt it was derived from"
        );
        assert!(
            runtime
                .driver
                .engine()
                .unwrap()
                .ctx
                .partitions
                .task_state
                .progress
                .contains("sources collected"),
            "a sibling's refusal does not stop the requests after it"
        );
    }

    #[test]
    fn an_append_beyond_the_workflow_node_quota_is_denied_without_touching_the_graph() {
        let mut runtime = workflow_root_awaiting_first_child();
        let nodes_before = runtime.driver.engine().unwrap().workflow_node_count();

        // the quota allows 3 nodes; the DAG already holds 2
        let oversized = SyscallRequest::AppendWorkflowNodes(
            super::super::syscall::AppendWorkflowNodesRequest {
                nodes: vec![
                    wire_node("a", "a", &[]),
                    wire_node("b", "b", &[]),
                    wire_node("c", "c", &[]),
                ],
            },
        );
        runtime.submit(&child_done_with(
            "in-done-1",
            1_700_000_003_000,
            "wf-node0",
            "wf-node0:attempt:1",
            vec![oversized],
        ));

        let rejected = rejections(&runtime);
        assert!(
            rejected.iter().any(|(operation, subject, reason)| operation
                == "submit_workflow_nodes"
                && subject.as_deref() == Some("wf-node0")
                && reason.contains("would grow workflow")),
            "the resource gate refused the growth, got {rejected:?}"
        );
        assert_eq!(
            runtime.driver.engine().unwrap().workflow_node_count(),
            nodes_before,
            "a denied append leaves the graph exactly as it was"
        );
    }

    #[test]
    fn a_quarantined_task_cannot_widen_its_authority_through_a_syscall() {
        let mut runtime = workflow_root_awaiting_first_child();
        assert!(
            runtime
                .driver
                .engine_mut()
                .unwrap()
                .quarantine_task_for_test("wf-node0"),
            "the node must exist to be quarantined"
        );
        let nodes_before = runtime.driver.engine().unwrap().workflow_node_count();

        let append = SyscallRequest::AppendWorkflowNodes(
            super::super::syscall::AppendWorkflowNodesRequest {
                nodes: vec![wire_node("escalate", "escalate", &[])],
            },
        );
        let activate = SyscallRequest::ActivateSkill(super::super::syscall::ActivateSkillRequest {
            name: "debug".to_string(),
            lease_turns: None,
        });
        let remember =
            SyscallRequest::RequestMemoryWrite(super::super::syscall::RequestMemoryWriteRequest {
                proposal: super::super::syscall::MemoryWriteProposal {
                    name: "escalation".to_string(),
                    kind: WireMemoryKind::Project,
                    content: "trust me".to_string(),
                    description: String::new(),
                    evidence_refs: Vec::new(),
                },
            });

        runtime.submit(&child_done_with(
            "in-done-1",
            1_700_000_003_000,
            "wf-node0",
            "wf-node0:attempt:1",
            vec![append, activate, remember],
        ));

        let quarantine_denials = rejections(&runtime);
        let families: Vec<&str> = quarantine_denials
            .iter()
            .filter(|(_, _, reason)| reason.starts_with("quarantine:"))
            .map(|(operation, _, _)| operation.as_str())
            .collect();
        assert_eq!(
            families,
            vec!["workflow", "capability", "memory"],
            "every privileged family is refused for a quarantined caller"
        );
        assert_eq!(
            runtime.driver.engine().unwrap().workflow_node_count(),
            nodes_before,
            "no node was appended"
        );
        assert!(
            !runtime
                .driver
                .engine()
                .unwrap()
                .ctx
                .active_skills
                .contains_key("debug"),
            "no skill was activated"
        );
        assert_eq!(
            runtime.pending_effect_kinds(),
            vec![EffectKindTag::SpawnTasks],
            "no memory effect was published; only the DAG's own next batch"
        );
    }

    #[test]
    fn a_child_request_cannot_forge_a_second_root_workflow() {
        let mut runtime = workflow_root_awaiting_first_child();
        let workflow_before = runtime.driver.workflow_id().cloned();
        let focus_before = runtime.driver.focus().cloned();

        let authored =
            SyscallRequest::SubmitWorkflow(super::super::syscall::SubmitWorkflowRequest {
                spec: WireSpec {
                    name: "usurper".to_string(),
                    nodes: vec![wire_node("usurp", "take over", &[])],
                },
            });
        runtime.submit(&child_done_with(
            "in-done-1",
            1_700_000_003_000,
            "wf-node0",
            "wf-node0:attempt:1",
            vec![authored],
        ));

        assert_eq!(
            runtime.driver.root_kind(),
            Some(RootKind::Workflow),
            "the root kind is immutable (§6.1.5)"
        );
        assert_eq!(
            runtime.driver.workflow_id().cloned(),
            workflow_before,
            "an authored spec flattens into the running DAG; it never becomes a second root"
        );
        assert_eq!(
            runtime.driver.focus().cloned(),
            focus_before,
            "a workflow root's focus never moves (§7.4)"
        );
        assert!(
            rejections(&runtime).is_empty(),
            "flattening is the admitted path, not a refusal"
        );
    }

    // -----------------------------------------------------------------------------------------
    // fixture: workflow-child-identity-is-kernel-issued (TCB launch arc)
    // -----------------------------------------------------------------------------------------

    #[test]
    fn a_child_moves_pending_launch_then_starting_then_running_on_the_acknowledgement() {
        let mut runtime = Runtime::new();
        runtime.submit(&syscall_config());

        // the spawn effect is planned but not yet committed: the identity exists, the launch does not
        let preparation = runtime.prepare(&workflow_start(
            "in-start",
            1_700_000_001_000,
            two_node_spec(),
        ));
        assert_eq!(
            runtime.driver.engine().unwrap().task_lifecycle("wf-node0"),
            Some(TaskLifecycle::Starting),
            "the launch effect is planned, so the task left PendingLaunch and awaits the host"
        );
        let started = runtime.append_and_commit(preparation);
        runtime.driver.note_committed(started.step_seq).unwrap();
        assert_eq!(
            runtime.driver.engine().unwrap().task_lifecycle("wf-node0"),
            Some(TaskLifecycle::Starting),
            "a published launch is not a running task"
        );

        runtime.submit(&spawned(
            "in-ack-1",
            1_700_000_002_000,
            &effect_id(started.step_seq),
            &["wf-node0"],
        ));
        assert_eq!(
            runtime.driver.engine().unwrap().task_lifecycle("wf-node0"),
            Some(TaskLifecycle::Running),
            "only the acknowledgement makes a task Running (§10.4, §15.3)"
        );
    }

    /// The launch arc at its own layer, where all three states are separately observable. Through
    /// the driver `PendingLaunch` and `Starting` both happen inside one `plan` call, because minting
    /// identity and building the launch effect are two moments of the same transition.
    #[test]
    fn an_ack_gated_spawn_mints_identity_in_pending_launch_before_the_effect_is_published() {
        let mut engine = LoopStateMachine::new(SchedulerBudget::default());
        let action = engine.load_workflow(
            build_core_spec(&WireSpec {
                name: String::new(),
                nodes: vec![wire_node("only", "only", &[])],
            })
            .unwrap(),
        );
        assert!(matches!(action, LoopAction::SpawnWorkflow { .. }));
        assert_eq!(
            engine.task_lifecycle("wf-node0"),
            Some(TaskLifecycle::PendingLaunch),
            "identity is minted, the launch is not published yet"
        );

        engine.mark_tasks_starting(&["wf-node0".to_string()]);
        assert_eq!(
            engine.task_lifecycle("wf-node0"),
            Some(TaskLifecycle::Starting),
            "the launch effect is published; the host has not answered"
        );

        engine.resolve_workflow_spawn(vec!["wf-node0".to_string()], Vec::new());
        assert_eq!(
            engine.task_lifecycle("wf-node0"),
            Some(TaskLifecycle::Running),
            "only the acknowledgement makes it Running"
        );
    }

    #[test]
    fn a_failed_launch_ends_the_attempt_and_refuses_any_later_completion_for_it() {
        use crate::runtime::kernel::wire::effect::{TaskLaunchFailed, TaskLaunchStatus};

        // two independent nodes, so failing one leaves the DAG running rather than terminating it
        let parallel = WireSpec {
            name: "parallel".to_string(),
            nodes: vec![
                wire_node("left", "left", &[]),
                wire_node("right", "right", &[]),
            ],
        };
        let mut runtime = Runtime::new();
        runtime.submit(&syscall_config());
        let started = runtime.submit(&workflow_start("in-start", 1_700_000_001_000, parallel));

        let failure = envelope(
            "in-ack-fail",
            1_700_000_002_000,
            KernelInput::ResolveEffect(ResolveEffect {
                effect_id: effect_id(started.step_seq),
                outcome: EffectOutcome::Succeeded(EffectSucceeded {
                    result: EffectSuccess::TasksSpawned(TasksSpawnedSuccess {
                        attempts: vec![
                            TaskLaunchOutcome {
                                task_id: TaskId::new("wf-node0").unwrap(),
                                attempt_id: WireAttemptId::new("wf-node0:attempt:1").unwrap(),
                                outcome: TaskLaunchStatus::Failed(TaskLaunchFailed {
                                    failure: super::super::effect::TaskLaunchFailure {
                                        kind:
                                            super::super::effect::HostEffectFailureKind::StorageUnavailable,
                                        message: "no worker".to_string(),
                                    },
                                }),
                            },
                            TaskLaunchOutcome {
                                task_id: TaskId::new("wf-node1").unwrap(),
                                attempt_id: WireAttemptId::new("wf-node1:attempt:1").unwrap(),
                                outcome: TaskLaunchStatus::Started(TaskLaunchStarted {}),
                            },
                        ],
                    }),
                }),
            }),
        );
        runtime.submit(&failure);
        assert!(
            runtime
                .driver
                .engine()
                .unwrap()
                .task_lifecycle("wf-node0")
                .is_some_and(|state| state.is_terminal()),
            "a failed launch terminates the attempt"
        );

        let fault = runtime.reject(&child_done_with(
            "in-late",
            1_700_000_003_000,
            "wf-node0",
            "wf-node0:attempt:1",
            Vec::new(),
        ));
        assert_eq!(
            fault.code,
            KernelFaultCode::InvalidAuthority,
            "a terminated attempt is a stale causation"
        );
    }

    #[test]
    fn a_second_completion_for_a_spent_attempt_carries_no_authority() {
        let mut runtime = workflow_root_awaiting_first_child();
        runtime.submit(&child_done_with(
            "in-done-1",
            1_700_000_003_000,
            "wf-node0",
            "wf-node0:attempt:1",
            Vec::new(),
        ));
        let nodes_before = runtime.driver.engine().unwrap().workflow_node_count();

        let replayed = child_done_with(
            "in-done-1-again",
            1_700_000_004_000,
            "wf-node0",
            "wf-node0:attempt:1",
            vec![SyscallRequest::AppendWorkflowNodes(
                super::super::syscall::AppendWorkflowNodesRequest {
                    nodes: vec![wire_node("smuggled", "smuggled", &[])],
                },
            )],
        );
        let fault = runtime.reject(&replayed);
        assert_eq!(fault.code, KernelFaultCode::InvalidAuthority);
        assert_eq!(
            runtime.driver.engine().unwrap().workflow_node_count(),
            nodes_before,
            "a refused completion appends nothing"
        );
    }

    // -----------------------------------------------------------------------------------------
    // §7.6 · memory proposals
    // -----------------------------------------------------------------------------------------

    #[test]
    fn a_memory_proposal_becomes_a_kernel_authored_write_with_derived_provenance() {
        let mut runtime = workflow_root_awaiting_first_child();
        let write =
            SyscallRequest::RequestMemoryWrite(super::super::syscall::RequestMemoryWriteRequest {
                proposal: super::super::syscall::MemoryWriteProposal {
                    name: "source-set".to_string(),
                    kind: WireMemoryKind::Project,
                    content: "12 primary sources".to_string(),
                    description: String::new(),
                    evidence_refs: Vec::new(),
                },
            });
        let advanced = runtime.submit(&child_done_with(
            "in-done-1",
            1_700_000_003_000,
            "wf-node0",
            "wf-node0:attempt:1",
            vec![write],
        ));

        let persisted = advanced
            .published_effects()
            .iter()
            .find(|effect| effect.tag() == EffectKindTag::PersistMemory)
            .expect("the proposal published a memory write");
        assert_eq!(
            persisted.effect_id,
            effect_id_at(advanced.step_seq, 0),
            "syscall effects mint their own identity from the step they belong to"
        );
        let EffectKind::PersistMemory(effect) = &persisted.effect else {
            panic!("expected a memory write");
        };
        assert_eq!(effect.binding.binding_id.as_str(), "mem-binding-1");
        assert_eq!(
            effect.memory.accepted_at_ms,
            WireU64::new(1_700_000_003_000),
            "provenance time is the envelope's accepted time, never a host clock (DEC-2)"
        );
        match &effect.memory.causation {
            SyscallCausation::ChildAttempt(child) => {
                assert_eq!(child.task_id.as_str(), "wf-node0");
                assert_eq!(child.attempt_id.as_str(), "wf-node0:attempt:1");
                assert_eq!(child.request_seq, 0, "the seq is the list's own order");
            }
            other => panic!("expected a child-attempt causation, got {other:?}"),
        }
        // the proposal contributed no security field, and the record does not grow one
        let json = serde_json::to_value(&effect.memory).unwrap().to_string();
        for forbidden in ["tenant", "author", "trust_level", "record_id", "session"] {
            assert!(
                !json.contains(forbidden),
                "the kernel-authored write leaked {forbidden}"
            );
        }
    }

    #[test]
    fn a_memory_query_is_clamped_to_the_operations_retrieval_policy() {
        let (mut runtime, provider) = agent_awaiting_provider();
        let resolved = runtime.submit(&provider_result(
            "in-recall",
            1_700_000_002_000,
            &provider,
            vec![tool_call(
                "call-1",
                crate::context::manager::MEMORY_TOOL_NAME,
                json!({"query": "past briefs", "top_k": 999}),
            )],
        ));
        let queried = sole_effect(&resolved);
        let EffectKind::QueryMemory(effect) = &queried.effect else {
            panic!("expected a memory query, got {:?}", queried.tag());
        };
        assert_eq!(
            effect.requested_k, 4,
            "the model cannot widen the operation's retrieval policy by asking for more"
        );
        assert!(matches!(
            effect.query.causation,
            SyscallCausation::ProviderTool(_)
        ));
        // the query the kernel authored is binding + causation + accepted time, nothing else
        let json = serde_json::to_value(&effect.query).unwrap().to_string();
        for forbidden in ["session", "tenant", "author", "trust", "agent_id"] {
            assert!(
                !json.contains(forbidden),
                "the kernel-authored query leaked {forbidden}"
            );
        }
        assert_eq!(effect.binding.binding_id.as_str(), "mem-binding-1");
    }

    // -----------------------------------------------------------------------------------------
    // fixture: no-host-session-identity (§22.6 · Task 11)
    // -----------------------------------------------------------------------------------------

    /// Every JSON key anywhere in `value`.
    fn all_keys(value: &Value, into: &mut BTreeSet<String>) {
        match value {
            Value::Object(map) => {
                for (key, child) in map {
                    into.insert(key.clone());
                    all_keys(child, into);
                }
            }
            Value::Array(items) => items.iter().for_each(|item| all_keys(item, into)),
            _ => {}
        }
    }

    /// One full canonical arc — configure → workflow root start → spawn effect → spawn
    /// acknowledgement → child completion carrying a memory proposal — as a host runs it.
    ///
    /// Returns everything the arc made durable or published: the record chain (bytes and digests),
    /// the effects, and the observations. Nothing here is host-timed or host-named beyond the
    /// opaque ids §5.3 admits.
    fn canonical_arc() -> (Vec<Value>, Vec<Value>, Vec<Value>) {
        let mut runtime = Runtime::new();
        let mut effects = Vec::new();
        let mut observations = Vec::new();
        let collect = |_runtime: &Runtime,
                       committed: &CommittedTransition<PlannedStep>,
                       effects: &mut Vec<Value>,
                       observations: &mut Vec<Value>| {
            for effect in committed.published_effects() {
                effects.push(serde_json::to_value(effect).unwrap());
            }
            for observation in &committed.step.observations {
                observations.push(serde_json::to_value(observation).unwrap());
            }
        };

        let configured = runtime.submit(&syscall_config());
        collect(&runtime, &configured, &mut effects, &mut observations);

        let started = runtime.submit(&workflow_start(
            "in-start",
            1_700_000_001_000,
            two_node_spec(),
        ));
        collect(&runtime, &started, &mut effects, &mut observations);

        let acked = runtime.submit(&spawned(
            "in-ack-1",
            1_700_000_002_000,
            &effect_id(started.step_seq),
            &["wf-node0"],
        ));
        collect(&runtime, &acked, &mut effects, &mut observations);

        let write =
            SyscallRequest::RequestMemoryWrite(super::super::syscall::RequestMemoryWriteRequest {
                proposal: super::super::syscall::MemoryWriteProposal {
                    name: "source-set".to_string(),
                    kind: WireMemoryKind::Project,
                    content: "12 primary sources".to_string(),
                    description: String::new(),
                    evidence_refs: Vec::new(),
                },
            });
        let advanced = runtime.submit(&child_done_with(
            "in-done-1",
            1_700_000_003_000,
            "wf-node0",
            "wf-node0:attempt:1",
            vec![write],
        ));
        collect(&runtime, &advanced, &mut effects, &mut observations);

        let records = runtime
            .journal
            .iter()
            .map(|record| {
                json!({
                    "bytes": String::from_utf8_lossy(record.record_bytes().as_slice()).into_owned(),
                    "digest": record.record_digest().as_str(),
                })
            })
            .collect();
        (records, effects, observations)
    }

    /// §22.6 · "host mapping does not affect kernel deterministic output", proved forwards: the arc
    /// is a pure function of its logical inputs, so two hosts — whatever they call their sessions —
    /// build the same record chain, publish the same effect bytes and read the same observations.
    #[test]
    fn the_canonical_arc_is_byte_identical_for_any_host() {
        let (records_a, effects_a, observations_a) = canonical_arc();
        let (records_b, effects_b, observations_b) = canonical_arc();

        assert!(!records_a.is_empty() && !effects_a.is_empty() && !observations_a.is_empty());
        assert_eq!(records_a, records_b, "the record chain is not reproducible");
        assert_eq!(
            effects_a, effects_b,
            "published effects are not reproducible"
        );
        assert_eq!(
            observations_a, observations_b,
            "observations are not reproducible"
        );
    }

    /// The scan half of Task 11: no session key, and no session *value*, exists anywhere on the
    /// canonical output surface — the durable records, the effects the host executes, or the
    /// observations it projects. A host may keep its own session mapping; the kernel holds none.
    #[test]
    fn no_canonical_record_effect_or_observation_names_a_session() {
        const BANNED_KEYS: [&str; 5] = [
            "session_id",
            "parent_session_id",
            "session",
            "submitter_agent_id",
            "actor_id",
        ];

        let (records, effects, observations) = canonical_arc();
        for (surface, values) in [
            ("record", &records),
            ("effect", &effects),
            ("observation", &observations),
        ] {
            for value in values {
                let mut keys = BTreeSet::new();
                all_keys(value, &mut keys);
                for banned in BANNED_KEYS {
                    assert!(
                        !keys.contains(banned),
                        "canonical {surface} carries the host-owned key {banned:?}: {value}"
                    );
                }
                assert!(
                    !value.to_string().contains("session"),
                    "canonical {surface} mentions a session: {value}"
                );
            }
        }

        // and the child the arc launched is correlated by logical identity alone
        let launch = effects
            .iter()
            .find(|effect| effect["effect"]["kind"] == "spawn_tasks")
            .expect("the arc launched a child");
        let task = &launch["effect"]["tasks"][0];
        assert_eq!(task["task_id"], "wf-node0");
        assert_eq!(task["attempt_id"], "wf-node0:attempt:1");
        assert!(
            task["launch_token"].as_str().is_some_and(|t| !t.is_empty()),
            "a child is named by task/attempt/launch token, never by a session"
        );
    }

    /// The process observation the arc publishes states the logical **parent task**, and the child
    /// spec the kernel derives for the legacy engine carries an empty session — the canonical path
    /// cannot populate one because no canonical input has a field for it.
    #[test]
    fn a_spawned_process_is_reported_by_its_logical_parent_task() {
        let (_, _, observations) = canonical_arc();
        let process = observations
            .iter()
            .find(|observation| observation["kind"] == "agent_process_changed")
            .expect("the arc published a process observation");
        assert_eq!(process["agent_id"], "wf-node0");
        assert_eq!(process["parent_task_id"], "root");

        let spec = agent_run_spec(&LogicalAgentSpec::new("write the brief"));
        assert_eq!(spec.identity.session_id.as_str(), NO_HOST_SESSION);
        assert!(spec.identity.parent_session_id.is_none());
    }

    // -----------------------------------------------------------------------------------------
    // fixture: removed-self-declared-caller
    // -----------------------------------------------------------------------------------------

    /// Every P1 request shape, serialized. None of them has a field through which a host could say
    /// who is asking — §22.10's whole point, and the reason omitting an id can no longer mean
    /// "skip the trust downgrade".
    #[test]
    fn no_syscall_request_shape_carries_a_self_declared_caller() {
        use super::super::syscall::*;

        let requests = vec![
            SyscallRequest::SubmitWorkflow(SubmitWorkflowRequest {
                spec: two_node_spec(),
            }),
            SyscallRequest::AppendWorkflowNodes(AppendWorkflowNodesRequest {
                nodes: vec![wire_node("n", "n", &[])],
            }),
            SyscallRequest::ActivateSkill(ActivateSkillRequest {
                name: "debug".to_string(),
                lease_turns: Some(3),
            }),
            SyscallRequest::UpdateTask(UpdateTaskRequest {
                update: WireTaskUpdate::default(),
            }),
            SyscallRequest::RequestMemoryWrite(RequestMemoryWriteRequest {
                proposal: MemoryWriteProposal {
                    name: "n".to_string(),
                    kind: WireMemoryKind::Project,
                    content: "c".to_string(),
                    description: String::new(),
                    evidence_refs: Vec::new(),
                },
            }),
            SyscallRequest::RequestMemoryQuery(RequestMemoryQueryRequest {
                query: MemoryQueryProposal::default(),
            }),
            SyscallRequest::PageIn(PageInRequest {
                handle_id: super::super::scalar::HandleId::new("h-1").unwrap(),
            }),
        ];

        for request in &requests {
            let text = serde_json::to_value(request).unwrap().to_string();
            for forbidden in [
                "submitter_agent_id",
                "actor_id",
                "agent_id",
                "session_id",
                "parent_session_id",
                "caller",
                "author",
                "trust",
            ] {
                assert!(
                    !text.contains(forbidden),
                    "{request:?} still exposes {forbidden}"
                );
            }
        }

        // a host cannot smuggle one in either: the shapes deny unknown fields
        let smuggled = r#"{"kind":"append_workflow_nodes","nodes":[],"submitter_agent_id":"root"}"#;
        assert!(
            serde_json::from_str::<SyscallRequest>(smuggled).is_err(),
            "a self-declared submitter must not decode"
        );
    }

    /// The three authority families a quarantined caller is refused, kept exhaustive against the
    /// request union so a new syscall cannot be added without a decision about it.
    #[test]
    fn every_syscall_is_classified_against_the_quarantine_rule() {
        use super::super::syscall::*;

        let classified = [
            (
                SyscallRequest::SubmitWorkflow(SubmitWorkflowRequest {
                    spec: WireSpec::default(),
                }),
                Some("workflow"),
            ),
            (
                SyscallRequest::AppendWorkflowNodes(AppendWorkflowNodesRequest {
                    nodes: Vec::new(),
                }),
                Some("workflow"),
            ),
            (
                SyscallRequest::ActivateSkill(ActivateSkillRequest {
                    name: String::new(),
                    lease_turns: None,
                }),
                Some("capability"),
            ),
            (
                SyscallRequest::RequestMemoryWrite(RequestMemoryWriteRequest {
                    proposal: MemoryWriteProposal {
                        name: String::new(),
                        kind: WireMemoryKind::Project,
                        content: String::new(),
                        description: String::new(),
                        evidence_refs: Vec::new(),
                    },
                }),
                Some("memory"),
            ),
            (
                SyscallRequest::RequestMemoryQuery(RequestMemoryQueryRequest {
                    query: MemoryQueryProposal::default(),
                }),
                Some("memory"),
            ),
            (
                SyscallRequest::UpdateTask(UpdateTaskRequest {
                    update: WireTaskUpdate::default(),
                }),
                None,
            ),
            (
                SyscallRequest::PageIn(PageInRequest {
                    handle_id: super::super::scalar::HandleId::new("h-1").unwrap(),
                }),
                None,
            ),
        ];
        for (request, family) in &classified {
            assert_eq!(&privileged_family(request), family, "{request:?}");
        }
    }

    #[test]
    fn a_page_in_of_a_handle_the_caller_does_not_hold_is_refused() {
        let (mut runtime, provider) = agent_awaiting_provider();
        runtime.submit(&provider_result(
            "in-read",
            1_700_000_002_000,
            &provider,
            vec![tool_call(
                "call-1",
                crate::context::manager::READ_RESULT_TOOL_NAME,
                json!({"call_id": "never-existed"}),
            )],
        ));
        let rejected = rejections(&runtime);
        assert_eq!(rejected.len(), 1);
        assert_eq!(rejected[0].0, "read_result");
        assert!(
            rejected[0].2.contains("not reachable"),
            "got {:?}",
            rejected[0].2
        );
        assert_eq!(
            runtime.pending_effect_kinds(),
            vec![EffectKindTag::CallProvider],
            "an address the caller does not hold produces no page-in effect; the turn continues"
        );
    }

    // -----------------------------------------------------------------------------------------
    // §7.10 · external payload (Task 13)
    // -----------------------------------------------------------------------------------------

    /// The body a host persists before it ever submits a result. Long enough to be over the test
    /// threshold, so both directions of the partition are exercised by real sizes.
    const BODY: &str = "the full report body, far larger than this operation keeps resident, \
                        repeated so it clears the inline threshold by a comfortable margin";

    fn body_digest() -> Digest {
        super::super::record::canonical_digest(BODY.as_bytes())
    }

    /// A payload policy small enough to test with real strings: results reaching 64 bytes must be
    /// externalised, and at most 32 bytes of preview stay resident.
    fn payload_config() -> WireEnvelope {
        use crate::runtime::kernel::wire::config::PayloadPolicy;
        syscall_config_with(|config| {
            config.host_effect_support = support_with([EffectKindTag::ArchivePageOut]);
            config.payload_policy = Some(PayloadPolicy {
                inline_threshold_bytes: Some(64),
                preview_bytes: Some(32),
            });
        })
    }

    fn external_payload(
        call_id: &str,
        digest: Digest,
        original_size: u64,
        preview: &str,
    ) -> WireToolResultPayload {
        external_payload_with(
            call_id,
            digest,
            original_size,
            preview,
            false,
            ToolResultDisposition::Recoverable,
        )
    }

    /// The same, with the two §7.10 rule 9 failure facts stated.
    fn external_payload_with(
        call_id: &str,
        digest: Digest,
        original_size: u64,
        preview: &str,
        is_error: bool,
        disposition: ToolResultDisposition,
    ) -> WireToolResultPayload {
        WireToolResultPayload::External(super::super::effect::ExternalToolResult {
            call_id: CallId::new(call_id).unwrap(),
            payload_ref: PayloadRef::new("payload:01J8Y2QK7C4N0V").unwrap(),
            digest,
            original_size: WireU64::new(original_size),
            preview: preview.to_string(),
            is_error,
            disposition,
        })
    }

    fn payloads_resolved(
        id: &str,
        at: u64,
        effect: &EffectId,
        results: Vec<WireToolResultPayload>,
    ) -> WireEnvelope {
        resolved(
            id,
            at,
            effect,
            EffectSuccess::Tools(ToolsSuccess { results }),
        )
    }

    /// An agent that called one host tool and is waiting for its results, under `payload_config`.
    fn agent_awaiting_tool_results() -> (Runtime, EffectId) {
        let mut runtime = Runtime::new();
        runtime.submit(&payload_config());
        let started = runtime.submit(&agent_start("in-start", 1_700_000_001_000));
        let acted = runtime.submit(&provider_result(
            "in-acted",
            1_700_000_002_000,
            &effect_id(started.step_seq),
            vec![tool_call("call-1", "search", json!({"q": "sources"}))],
        ));
        let tools = sole_effect(&acted);
        assert_eq!(tools.tag(), EffectKindTag::ExecuteTools);
        (runtime, tools.effect_id.clone())
    }

    /// The whole point of the contract: the *preview* enters context and the handle says where the
    /// body is. Nothing about the body itself is inside this kernel.
    #[test]
    fn an_external_tool_result_lands_as_a_preview_and_an_external_handle() {
        let (mut runtime, tools) = agent_awaiting_tool_results();
        let committed = runtime.submit(&payloads_resolved(
            "in-results",
            1_700_000_003_000,
            &tools,
            vec![external_payload(
                "call-1",
                body_digest(),
                BODY.len() as u64,
                "the full report body, far la…",
            )],
        ));
        assert_eq!(
            kinds(&committed),
            vec![EffectKindTag::CallProvider],
            "an external result resumes the turn exactly like an inline one"
        );
        let EffectKind::CallProvider(provider) = &sole_effect(&committed).effect else {
            panic!("external residency must resume through the provider");
        };
        assert!(
            provider
                .tools
                .iter()
                .any(|tool| tool.name.as_str() == READ_RESULT_TOOL_NAME),
            "the refreshed provider projection must advertise the newly reachable payload"
        );

        let engine = runtime.driver.engine().expect("the arc built an engine");
        assert_eq!(
            engine.ctx.payload_residency("call-1"),
            Some(&Residency::External {
                payload_ref: "payload:01J8Y2QK7C4N0V".to_string(),
                digest: body_digest().as_str().to_string(),
                original_size: BODY.len() as u64,
            }),
            "§7.10 rule 3 · the P3 handle is where the reference lives"
        );

        let rendered = serde_json::to_string(&engine.ctx.partitions.history.messages).unwrap();
        assert!(
            rendered.contains("the full report body, far la"),
            "the preview is what occupies working context"
        );
        assert!(
            !rendered.contains("clears the inline threshold"),
            "the body must not be in context: {rendered}"
        );
        assert!(observation_kinds(&runtime).contains(&"payload_residency_changed"));
        assert_eq!(
            runtime
                .observations()
                .iter()
                .filter(|observation| matches!(
                    observation,
                    KernelObservation::CheckpointTaken { .. }
                ))
                .count(),
            1,
            "re-projecting external residency must not execute the provider-call boundary twice",
        );
    }

    /// §7.10 rule 2 · a digest this kernel cannot recompute is a payload it could never prove it
    /// restored, so it is refused at admission rather than at page-in.
    #[test]
    fn an_external_tool_result_the_kernel_cannot_verify_is_refused() {
        let (mut runtime, tools) = agent_awaiting_tool_results();
        let fault = runtime.reject(&payloads_resolved(
            "in-results",
            1_700_000_003_000,
            &tools,
            vec![external_payload(
                "call-1",
                Digest::new("md5:deadbeef").unwrap(),
                BODY.len() as u64,
                "preview",
            )],
        ));
        assert_eq!(fault.code, KernelFaultCode::MalformedEnvelope);
        assert!(
            fault.message.contains("sha256:<64 hex>"),
            "the refusal names the only digest shape a page-in can be checked against: {}",
            fault.message
        );
    }

    /// §7.10 rule 2 · the configured threshold is a **total** partition. A body small enough to
    /// inline may not be externalised: it would buy a `LoadPayload` round trip to read something
    /// that fitted in the turn that produced it, and it would leave the one rule a host and the
    /// kernel must agree on with a hole in the middle.
    #[test]
    fn an_external_tool_result_below_the_threshold_is_refused() {
        let (mut runtime, tools) = agent_awaiting_tool_results();
        let small = "tiny";
        let fault = runtime.reject(&payloads_resolved(
            "in-results",
            1_700_000_003_000,
            &tools,
            vec![external_payload(
                "call-1",
                super::super::record::canonical_digest(small.as_bytes()),
                small.len() as u64,
                small,
            )],
        ));
        assert_eq!(fault.code, KernelFaultCode::MalformedEnvelope);
        assert!(
            fault.message.contains("inlines below 64"),
            "{}",
            fault.message
        );
    }

    /// The preview is the part that actually occupies context, so it is the part the policy bounds.
    #[test]
    fn an_external_preview_over_the_resident_budget_is_refused() {
        let (mut runtime, tools) = agent_awaiting_tool_results();
        let fault = runtime.reject(&payloads_resolved(
            "in-results",
            1_700_000_003_000,
            &tools,
            vec![external_payload(
                "call-1",
                body_digest(),
                BODY.len() as u64,
                BODY,
            )],
        ));
        assert_eq!(fault.code, KernelFaultCode::ResourceLimitExceeded);
        assert!(fault.message.contains("preview"), "{}", fault.message);
    }

    /// §7.10 rules 1 and 5 · the kernel does not externalise on the host's behalf. Accepting an
    /// oversized inline result and spooling it back out is the historical round trip where the body
    /// crossed core twice and entered the journal twice; the only answer that keeps rule 5 true is
    /// to refuse it.
    #[test]
    fn an_inline_tool_result_over_the_threshold_is_refused() {
        let (mut runtime, tools) = agent_awaiting_tool_results();
        let fault = runtime.reject(&tools_resolved(
            "in-results",
            1_700_000_003_000,
            &tools,
            &[("call-1", BODY, false)],
        ));
        assert_eq!(fault.code, KernelFaultCode::ResourceLimitExceeded);
        assert!(
            fault.message.contains("externalises at 64"),
            "{}",
            fault.message
        );
        assert_eq!(
            runtime.pending_effect_kinds(),
            vec![EffectKindTag::ExecuteTools],
            "a rejected batch leaves the effect it was answering pending — zero mutation"
        );
    }

    /// A batch is adjudicated whole: one illegal result and none of it lands.
    #[test]
    fn one_illegal_result_rejects_the_whole_batch() {
        let mut runtime = Runtime::new();
        runtime.submit(&payload_config());
        let started = runtime.submit(&agent_start("in-start", 1_700_000_001_000));
        let acted = runtime.submit(&provider_result(
            "in-acted",
            1_700_000_002_000,
            &effect_id(started.step_seq),
            vec![
                tool_call("call-1", "search", json!({"q": "a"})),
                tool_call("call-2", "search", json!({"q": "b"})),
            ],
        ));
        let tools = sole_effect(&acted).effect_id.clone();
        runtime.reject(&payloads_resolved(
            "in-results",
            1_700_000_003_000,
            &tools,
            vec![
                WireToolResultPayload::Inline(InlineToolResult {
                    call_id: CallId::new("call-1").unwrap(),
                    result: WireToolResult {
                        output: "small and legal".to_string(),
                        is_error: false,
                        disposition: ToolResultDisposition::Recoverable,
                        tokens: None,
                    },
                }),
                external_payload("call-2", Digest::new("md5:deadbeef").unwrap(), 9_000, "p"),
            ],
        ));
        let engine = runtime.driver.engine().expect("the arc built an engine");
        assert!(
            engine.ctx.payload_residency("call-1").is_none(),
            "the legal half of a refused batch must not have landed either"
        );
    }

    /// The full §7.10 rule 4 arc: `read_result` → `LoadPayload` → `PayloadLoaded`, with the body
    /// entering context only once the digest proves it is the one that left.
    #[test]
    fn a_page_in_of_an_external_payload_loads_and_verifies_the_body() {
        let (mut runtime, tools) = agent_awaiting_tool_results();
        let stored = runtime.submit(&payloads_resolved(
            "in-results",
            1_700_000_003_000,
            &tools,
            vec![external_payload(
                "call-1",
                body_digest(),
                BODY.len() as u64,
                "the full report body, far la…",
            )],
        ));

        let read = runtime.submit(&provider_result(
            "in-read",
            1_700_000_004_000,
            &effect_id(stored.step_seq),
            vec![tool_call(
                "call-2",
                READ_RESULT_TOOL_NAME,
                json!({"call_id": "call-1"}),
            )],
        ));
        let load = sole_effect(&read);
        assert_eq!(load.tag(), EffectKindTag::LoadPayload);
        let EffectKind::LoadPayload(effect) = &load.effect else {
            panic!("expected a payload load");
        };
        assert_eq!(effect.handle_id.as_str(), "call-1");
        assert_eq!(
            effect.payload_ref.as_str(),
            "payload:01J8Y2QK7C4N0V",
            "the effect hands back the host's own opaque locator, unread"
        );
        let load = load.effect_id.clone();
        assert!(
            rejections(&runtime).is_empty(),
            "a reachable, externally-backed handle is a page-in, not a refusal"
        );

        let restored = runtime.submit(&resolved(
            "in-loaded",
            1_700_000_005_000,
            &load,
            EffectSuccess::PayloadLoaded(PayloadLoadedSuccess {
                handle_id: HandleId::new("call-1").unwrap(),
                payload: InlinePayload {
                    content: BODY.to_string(),
                    digest: body_digest(),
                    original_size: WireU64::new(BODY.len() as u64),
                },
            }),
        ));
        assert_eq!(
            kinds(&restored),
            vec![EffectKindTag::CallProvider],
            "the paged-in body resumes the turn that asked for it"
        );

        let engine = runtime.driver.engine().expect("the arc built an engine");
        assert_eq!(
            engine.ctx.payload_residency("call-1"),
            Some(&Residency::Resident),
            "§25.9 · the handle is the fact, and it says the body came home"
        );
        let rendered = serde_json::to_string(&engine.ctx.partitions.history.messages).unwrap();
        assert!(
            rendered.contains("clears the inline threshold"),
            "the model reads the body it asked for"
        );
        assert!(observation_kinds(&runtime).contains(&"payload_residency_changed"));
    }

    /// The kernel never saw the body, so the digest is the only evidence there is. A mismatch is a
    /// protocol violation with zero mutation, not a degraded read.
    #[test]
    fn a_paged_in_body_that_is_not_the_one_that_left_is_refused() {
        let (mut runtime, load) = agent_awaiting_payload_load();
        // Same length, and self-consistently declared — only the digest can tell the difference,
        // which is exactly the property the contract rests on.
        let substitute = BODY.replace("margin", "MARGIN");
        assert_eq!(substitute.len(), BODY.len());
        let fault = runtime.reject(&resolved(
            "in-loaded",
            1_700_000_005_000,
            &load,
            EffectSuccess::PayloadLoaded(PayloadLoadedSuccess {
                handle_id: HandleId::new("call-1").unwrap(),
                payload: InlinePayload {
                    content: substitute.to_string(),
                    digest: super::super::record::canonical_digest(substitute.as_bytes()),
                    original_size: WireU64::new(substitute.len() as u64),
                },
            }),
        ));
        assert_eq!(fault.code, KernelFaultCode::UnexpectedEffectOutcome);
        assert!(fault.message.contains("digests to"), "{}", fault.message);

        let engine = runtime.driver.engine().expect("the arc built an engine");
        assert!(
            matches!(
                engine.ctx.payload_residency("call-1"),
                Some(Residency::External { .. })
            ),
            "a refused restore leaves the handle exactly where it was"
        );
    }

    /// A loaded payload that does not agree with itself never gets as far as the digest: the size
    /// it declares and the bytes it carries are the same claim stated twice.
    #[test]
    fn a_paged_in_body_that_contradicts_its_own_size_is_refused() {
        let (mut runtime, load) = agent_awaiting_payload_load();
        let fault = runtime.reject(&resolved(
            "in-loaded",
            1_700_000_005_000,
            &load,
            EffectSuccess::PayloadLoaded(PayloadLoadedSuccess {
                handle_id: HandleId::new("call-1").unwrap(),
                payload: InlinePayload {
                    content: BODY.to_string(),
                    digest: body_digest(),
                    original_size: WireU64::new(BODY.len() as u64 + 1),
                },
            }),
        ));
        assert_eq!(fault.code, KernelFaultCode::UnexpectedEffectOutcome);
        assert!(fault.message.contains("and carries"), "{}", fault.message);
    }

    /// The outcome must name the handle its effect addressed — the same rule `verify_page_out_receipt`
    /// applies on the way out.
    #[test]
    fn a_paged_in_body_for_another_handle_is_refused() {
        let (mut runtime, load) = agent_awaiting_payload_load();
        let fault = runtime.reject(&resolved(
            "in-loaded",
            1_700_000_005_000,
            &load,
            EffectSuccess::PayloadLoaded(PayloadLoadedSuccess {
                handle_id: HandleId::new("call-9").unwrap(),
                payload: InlinePayload {
                    content: BODY.to_string(),
                    digest: body_digest(),
                    original_size: WireU64::new(BODY.len() as u64),
                },
            }),
        ));
        assert_eq!(fault.code, KernelFaultCode::UnexpectedEffectOutcome);
        assert!(fault.message.contains("names handle"), "{}", fault.message);
    }

    /// DEC-5 · a body the host cannot produce leaves the operation where it was. The read is
    /// abandoned once, the loop continues, and the kernel does not re-issue the same load.
    #[test]
    fn a_payload_the_host_cannot_produce_abandons_the_read() {
        let (mut runtime, load) = agent_awaiting_payload_load();
        let resumed = runtime.submit(&failed(
            "in-load-failed",
            1_700_000_005_000,
            &load,
            HostEffectFailureKind::StorageUnavailable,
            "the blob store is offline",
        ));
        assert_eq!(
            kinds(&resumed),
            vec![EffectKindTag::CallProvider],
            "one page-in failure does not kill a live run"
        );
        assert!(observation_kinds(&runtime).contains(&"payload_load_failed"));
        let engine = runtime.driver.engine().expect("the arc built an engine");
        assert!(
            matches!(
                engine.ctx.payload_residency("call-1"),
                Some(Residency::External { .. })
            ),
            "the reference survives the failed read: the body is still out there"
        );
    }

    /// An address whose body core still holds is refused with the reason. There is no locator to
    /// hand back, and inventing one is the confusion the closed union removes.
    #[test]
    fn a_page_in_of_a_resident_handle_is_refused() {
        let (mut runtime, tools) = agent_awaiting_tool_results();
        let inlined = runtime.submit(&tools_resolved(
            "in-results",
            1_700_000_003_000,
            &tools,
            &[("call-1", "three sources found", false)],
        ));
        runtime.submit(&provider_result(
            "in-read",
            1_700_000_004_000,
            &effect_id(inlined.step_seq),
            vec![tool_call(
                "call-2",
                READ_RESULT_TOOL_NAME,
                json!({"call_id": "call-1"}),
            )],
        ));
        let rejected = rejections(&runtime);
        assert_eq!(rejected.len(), 1);
        assert_eq!(rejected[0].0, "read_result");
        assert!(
            rejected[0].2.contains("nothing to page in"),
            "got {:?}",
            rejected[0].2
        );
        assert_eq!(
            runtime.pending_effect_kinds(),
            vec![EffectKindTag::CallProvider],
            "a body core already holds publishes no load effect"
        );
    }

    /// B19 · the other half of the axis. A page-out archive becomes a `PagedOut` handle — a
    /// *different* state from `External`, reached from `Resident` — and the model can read it back
    /// through the same `LoadPayload` effect.
    #[test]
    fn a_page_out_archive_becomes_a_readable_paged_out_handle() {
        let (mut runtime, archive) = agent_awaiting_page_out();
        let EffectKind::ArchivePageOut(published) = &runtime
            .tx
            .pending_effects()
            .find(|effect| effect.effect_id == archive)
            .expect("the archive is pending")
            .effect
        else {
            panic!("expected a page-out archive");
        };
        let handle_id = published.handle_id.clone();
        let digest = published.payload.digest.clone();
        let content = published.payload.content.clone();
        let original_size = published.payload.original_size;

        let archived = runtime.submit(&resolved(
            "in-archived",
            1_700_000_003_000,
            &archive,
            EffectSuccess::PageOutArchived(super::super::effect::PageOutArchivedSuccess {
                receipt: ArchiveReceipt {
                    handle_id: handle_id.clone(),
                    payload_ref: PayloadRef::new("payload:archive-1").unwrap(),
                    digest: digest.clone(),
                    original_size,
                },
            }),
        ));
        let engine = runtime.driver.engine().expect("the arc built an engine");
        assert_eq!(
            engine.ctx.payload_residency(handle_id.as_str()),
            Some(&Residency::PagedOut {
                payload_ref: "payload:archive-1".to_string(),
                digest: digest.as_str().to_string(),
            }),
            "an archived body is paged out, never external — it *was* resident"
        );
        assert!(observation_kinds(&runtime).contains(&"payload_residency_changed"));

        // and the same read_result path addresses it
        let read = runtime.submit(&provider_result(
            "in-read",
            1_700_000_004_000,
            &effect_id(archived.step_seq),
            vec![tool_call(
                "call-7",
                READ_RESULT_TOOL_NAME,
                json!({ "call_id": handle_id.as_str() }),
            )],
        ));
        let load = sole_effect(&read);
        assert_eq!(load.tag(), EffectKindTag::LoadPayload);
        let load = load.effect_id.clone();

        runtime.submit(&resolved(
            "in-loaded",
            1_700_000_005_000,
            &load,
            EffectSuccess::PayloadLoaded(PayloadLoadedSuccess {
                handle_id: handle_id.clone(),
                payload: InlinePayload {
                    content: content.clone(),
                    digest,
                    original_size,
                },
            }),
        ));
        let engine = runtime.driver.engine().expect("the arc built an engine");
        assert_eq!(
            engine.ctx.payload_residency(handle_id.as_str()),
            Some(&Residency::Resident),
            "the archived history came home through the same effect the external body uses"
        );
    }

    /// An agent holding one external payload, with a page-in of it pending.
    fn agent_awaiting_payload_load() -> (Runtime, EffectId) {
        let (mut runtime, tools) = agent_awaiting_tool_results();
        let stored = runtime.submit(&payloads_resolved(
            "in-results",
            1_700_000_003_000,
            &tools,
            vec![external_payload(
                "call-1",
                body_digest(),
                BODY.len() as u64,
                "the full report body, far la…",
            )],
        ));
        let read = runtime.submit(&provider_result(
            "in-read",
            1_700_000_004_000,
            &effect_id(stored.step_seq),
            vec![tool_call(
                "call-2",
                READ_RESULT_TOOL_NAME,
                json!({"call_id": "call-1"}),
            )],
        ));
        (runtime, sole_effect(&read).effect_id.clone())
    }

    // -----------------------------------------------------------------------------------------
    // fixture: removed-large-result-spool-effect
    // -----------------------------------------------------------------------------------------

    /// §7.10 rules 5 and 6 / §25.10 · the body does not cross core, in either direction.
    ///
    /// The scan is over everything the arc made durable or published: no record, no effect and no
    /// observation carries the persisted body, and no effect kind exists through which the kernel
    /// could hand a body back out to be persisted. The full output enters only through a verified
    /// host-owned payload reference.
    #[test]
    fn no_canonical_record_effect_or_observation_carries_an_external_body() {
        let (mut runtime, tools) = agent_awaiting_tool_results();
        runtime.submit(&payloads_resolved(
            "in-results",
            1_700_000_003_000,
            &tools,
            vec![external_payload(
                "call-1",
                body_digest(),
                BODY.len() as u64,
                "the full report body, far la…",
            )],
        ));

        // The record's canonical input is a base64 envelope in JSON, so scanning the record bytes
        // alone would pass for free. Every accepted input is decoded back out and scanned as text.
        let mut surfaces: Vec<(String, String)> = Vec::new();
        for record in &runtime.journal {
            surfaces.push((
                "record".to_string(),
                String::from_utf8_lossy(record.record_bytes().as_slice()).into_owned(),
            ));
            surfaces.push((
                "accepted input".to_string(),
                serde_json::to_string(&record.normalized_input().expect("the record decodes"))
                    .unwrap(),
            ));
        }
        for effect in runtime.tx.pending_effects() {
            surfaces.push(("effect".to_string(), serde_json::to_string(effect).unwrap()));
        }
        for observation in runtime.observations() {
            surfaces.push((
                "observation".to_string(),
                serde_json::to_string(observation).unwrap(),
            ));
        }
        assert!(surfaces.len() >= 4, "the arc produced nothing to scan");
        for (surface, text) in &surfaces {
            assert!(
                !text.contains("clears the inline threshold"),
                "the canonical {surface} carries the externalised body: {text}"
            );
            assert!(
                !text.contains("spool"),
                "the canonical {surface} still speaks of spooling: {text}"
            );
        }

        // and no effect kind can express handing a body back out to be written
        for tag in EffectKindTag::ALL {
            assert!(
                !tag.as_str().contains("spool"),
                "{} would be the round trip §7.10 deletes",
                tag.as_str()
            );
        }
    }

    // -----------------------------------------------------------------------------------------
    // fixture: removed-session-log-payload-lookup
    // -----------------------------------------------------------------------------------------

    /// §7.10 rules 4 and 7 · a page-in addresses the handle table and nothing else.
    ///
    /// The historical `read_result` resolved by scanning a spool directory and then falling back to
    /// a linear walk of the SessionLog, so any path-shaped string was a readable address and a body
    /// that had left the table was still reachable. Here the two refusals are total: an address the
    /// caller does not hold, and a locator-shaped string that is not an address at all. Neither
    /// produces an effect, so there is no lookup to fall back *to*.
    #[test]
    fn a_page_in_cannot_address_anything_outside_the_handle_table() {
        let (mut runtime, tools) = agent_awaiting_tool_results();
        let stored = runtime.submit(&payloads_resolved(
            "in-results",
            1_700_000_003_000,
            &tools,
            vec![external_payload(
                "call-1",
                body_digest(),
                BODY.len() as u64,
                "the full report body, far la…",
            )],
        ));
        // the locator the host chose is *not* an address: only the handle is
        runtime.submit(&provider_result(
            "in-read",
            1_700_000_004_000,
            &effect_id(stored.step_seq),
            vec![tool_call(
                "call-2",
                READ_RESULT_TOOL_NAME,
                json!({"call_id": "payload:01J8Y2QK7C4N0V"}),
            )],
        ));
        let rejected = rejections(&runtime);
        assert_eq!(rejected.len(), 1);
        assert!(
            rejected[0].2.contains("not reachable"),
            "got {:?}",
            rejected[0].2
        );
        assert_eq!(
            runtime.pending_effect_kinds(),
            vec![EffectKindTag::CallProvider],
            "no effect is published for an address the table does not hold"
        );
    }

    // -----------------------------------------------------------------------------------------
    // determinism: the record chain replays to the same steps
    // -----------------------------------------------------------------------------------------

    #[test]
    fn the_journal_replays_to_byte_identical_records() {
        let runtime = drive_workflow_root_to_terminal();
        verify_record_chain(&runtime.journal).expect("the chain links up");

        let mut replay = CanonicalOperationDriver::new();
        let rebuilt: KernelTransaction<PlannedStep, InMemoryRecordIndex> =
            KernelTransaction::rebuild_from_records(
                &runtime.journal,
                ConfigDefaults::default(),
                InMemoryRecordIndex::new(),
                |context| replay.fold(context),
            )
            .expect("a deterministic driver rebuilds its own journal");

        assert_eq!(rebuilt.lifecycle(), OperationLifecycle::Completed);
        assert_eq!(rebuilt.terminal(), runtime.tx.terminal());
        assert_eq!(replay.root_kind(), runtime.driver.root_kind());
        assert_eq!(replay.focus(), runtime.driver.focus());
    }

    /// A journal that contains syscall transitions rebuilds to the same steps — the caller a
    /// request was attributed to, the causation it spent and the graph it grew are all kernel state
    /// derived from the records, not host state a resume has to reassemble (§10.3).
    #[test]
    fn a_journal_with_syscall_transitions_replays_to_byte_identical_records() {
        let mut runtime = workflow_root_awaiting_first_child();
        runtime.submit(&child_done_with(
            "in-done-1",
            1_700_000_003_000,
            "wf-node0",
            "wf-node0:attempt:1",
            vec![
                SyscallRequest::AppendWorkflowNodes(
                    super::super::syscall::AppendWorkflowNodesRequest {
                        nodes: vec![wire_node("verify", "verify", &[])],
                    },
                ),
                SyscallRequest::AppendWorkflowNodes(
                    super::super::syscall::AppendWorkflowNodesRequest {
                        nodes: vec![wire_node("orphan", "orphan", &["nowhere"])],
                    },
                ),
            ],
        ));
        verify_record_chain(&runtime.journal).expect("the chain links up");

        let mut replay = CanonicalOperationDriver::new();
        let rebuilt: KernelTransaction<PlannedStep, InMemoryRecordIndex> =
            KernelTransaction::rebuild_from_records(
                &runtime.journal,
                ConfigDefaults::default(),
                InMemoryRecordIndex::new(),
                |context| replay.fold(context),
            )
            .expect("a deterministic driver rebuilds its own syscall journal");

        assert_eq!(rebuilt.lifecycle(), runtime.tx.lifecycle());
        assert_eq!(replay.focus(), runtime.driver.focus());
        assert_eq!(
            replay.engine().unwrap().workflow_node_count(),
            runtime.driver.engine().unwrap().workflow_node_count(),
            "the appended node is a kernel fact the replay reproduces"
        );
        assert_eq!(
            replay.attempts, runtime.driver.attempts,
            "live attempts rebuild identically, so authority after a rebuild is the same"
        );
    }

    #[test]
    fn a_plan_that_never_commits_fails_closed_instead_of_drifting() {
        let mut runtime = Runtime::new();
        runtime.submit(&configure());

        // plan a start, then abandon it: the semantic kernel already moved under that plan
        let preparation = runtime.prepare(&agent_start("in-start", 1_700_000_001_000));
        let token = preparation.token().unwrap().clone();
        runtime.tx.abort(&token).expect("abort before the append");

        let fault = runtime
            .driver
            .plan(&PlanContext {
                input: &runtime.journal[0].normalized_input().unwrap(),
                step_seq: WireU64::new(1),
                previous_head: None,
                config: runtime.tx.config().unwrap(),
                resolving: None,
            })
            .expect_err("a discarded plan poisons the driver");
        assert_eq!(fault.code, KernelFaultCode::TransactionConflict);
        assert!(runtime.driver.poison().is_some(), "the driver is poisoned");
    }

    // -----------------------------------------------------------------------------------------
    // §7.9 · unified effect resolution (Task 12)
    //
    // Three slices, in the order the spec suggests: provider/tool, workflow/control,
    // memory/page-out. Each effect kind gets success, failure and replay.
    // -----------------------------------------------------------------------------------------

    use crate::runtime::kernel::wire::effect::{
        ApprovalSuccess, ArchiveReceipt, EffectFailed, HostEffectFailure, HostEffectFailureKind,
        InlinePayload, InlineToolResult, MemoryPersistReceipt, MemoryPersistedSuccess,
        MemoryQueriedSuccess, MemoryRecall, MilestoneCheckResult as WireMilestoneResult,
        MilestoneEvaluatedSuccess, PageOutArchivedSuccess, PayloadLoadedSuccess,
        ProviderContextOverflow, ProviderStopReason, TaskAlreadyFinished, TaskPreemptOutcome,
        TaskPreemptStatus, TaskPreempted, TasksPreemptedSuccess, ToolResult as WireToolResult,
        ToolsSuccess,
    };
    use crate::runtime::kernel::wire::scalar::{CallId, HandleId};
    use crate::runtime::kernel::wire::syscall::MemoryKind as SyscallMemoryKind;
    use crate::runtime::kernel::wire::{Digest, MemoryRecordRef, PayloadRef};

    /// The support set `syscall_config` declares, plus whatever an arc additionally needs. Adding
    /// rather than replacing keeps §7.3's cross-field rules satisfied (a declared tool catalog
    /// implies `execute_tools` + `load_payload`).
    fn support_with(extra: impl IntoIterator<Item = EffectKindTag>) -> HostEffectSupport {
        HostEffectSupport::new(
            [
                EffectKindTag::CallProvider,
                EffectKindTag::ExecuteTools,
                EffectKindTag::LoadPayload,
                EffectKindTag::SpawnTasks,
                EffectKindTag::PreemptTasks,
                EffectKindTag::PersistMemory,
                EffectKindTag::QueryMemory,
            ]
            .into_iter()
            .chain(extra),
        )
    }

    fn resolved(id: &str, at: u64, effect: &EffectId, result: EffectSuccess) -> WireEnvelope {
        envelope(
            id,
            at,
            KernelInput::ResolveEffect(ResolveEffect {
                effect_id: effect.clone(),
                outcome: EffectOutcome::Succeeded(EffectSucceeded { result }),
            }),
        )
    }

    fn failed(
        id: &str,
        at: u64,
        effect: &EffectId,
        kind: HostEffectFailureKind,
        message: &str,
    ) -> WireEnvelope {
        envelope(
            id,
            at,
            KernelInput::ResolveEffect(ResolveEffect {
                effect_id: effect.clone(),
                outcome: EffectOutcome::Failed(EffectFailed {
                    failure: HostEffectFailure {
                        kind,
                        message: message.to_string(),
                        retryable: None,
                    },
                }),
            }),
        )
    }

    /// A provider turn that finished with plain text and no tool calls.
    fn provider_answer(id: &str, at: u64, effect: &EffectId, text: &str) -> WireEnvelope {
        resolved(
            id,
            at,
            effect,
            EffectSuccess::Provider(super::super::effect::ProviderSuccess {
                outcome: ProviderOutcome::Completed(ProviderCompleted {
                    message: ProviderMessage {
                        role: MessageRole::Assistant,
                        content: text.to_string(),
                        tool_calls: Vec::new(),
                        tool_call_id: None,
                        tokens: None,
                    },
                    observed_input_tokens: None,
                    observed_output_tokens: None,
                    stop_reason: Some(ProviderStopReason::EndTurn),
                }),
            }),
        )
    }

    fn provider_overflow(id: &str, at: u64, effect: &EffectId) -> WireEnvelope {
        resolved(
            id,
            at,
            effect,
            EffectSuccess::Provider(super::super::effect::ProviderSuccess {
                outcome: ProviderOutcome::ContextOverflow(ProviderContextOverflow {
                    observed_input_tokens: Some(999_999),
                }),
            }),
        )
    }

    fn tools_resolved(
        id: &str,
        at: u64,
        effect: &EffectId,
        results: &[(&str, &str, bool)],
    ) -> WireEnvelope {
        resolved(
            id,
            at,
            effect,
            EffectSuccess::Tools(ToolsSuccess {
                results: results
                    .iter()
                    .map(|(call_id, output, is_error)| {
                        WireToolResultPayload::Inline(InlineToolResult {
                            call_id: CallId::new(*call_id).unwrap(),
                            result: WireToolResult {
                                output: (*output).to_string(),
                                is_error: *is_error,
                                disposition: ToolResultDisposition::Recoverable,
                                tokens: None,
                            },
                        })
                    })
                    .collect(),
            }),
        )
    }

    fn kinds(committed: &CommittedTransition<PlannedStep>) -> Vec<EffectKindTag> {
        committed
            .published_effects()
            .iter()
            .map(|effect| effect.tag())
            .collect()
    }

    /// Every observation the last committed transition recorded, by variant name.
    fn observation_kinds(runtime: &Runtime) -> Vec<&'static str> {
        runtime
            .observations()
            .iter()
            .map(observation_label)
            .collect()
    }

    fn observation_label(observation: &KernelObservation) -> &'static str {
        match observation {
            KernelObservation::MemoryWritten { .. } => "memory_written",
            KernelObservation::MemoryWriteFailed { .. } => "memory_write_failed",
            KernelObservation::MemoryQueried { .. } => "memory_queried",
            KernelObservation::MemoryQueryFailed { .. } => "memory_query_failed",
            KernelObservation::PageOutArchived { .. } => "page_out_archived",
            KernelObservation::PageOutArchiveFailed { .. } => "page_out_archive_failed",
            KernelObservation::ApprovalResolutionFailed { .. } => "approval_resolution_failed",
            KernelObservation::AgentPreemptFailed { .. } => "agent_preempt_failed",
            KernelObservation::ControlRequestRejected { .. } => "control_request_rejected",
            KernelObservation::Compressed { .. } => "compressed",
            KernelObservation::WorkflowBatchSpawned { .. } => "workflow_batch_spawned",
            KernelObservation::WorkflowCompleted { .. } => "workflow_completed",
            KernelObservation::MilestoneAdvanced { .. } => "milestone_advanced",
            KernelObservation::MilestoneBlocked { .. } => "milestone_blocked",
            KernelObservation::Resumed { .. } => "resumed",
            KernelObservation::Suspended { .. } => "suspended",
            KernelObservation::SignalDeliveryDisposed { .. } => "signal_delivery_disposed",
            KernelObservation::SignalDisplaced { .. } => "signal_displaced",
            KernelObservation::SignalExpired { .. } => "signal_expired",
            KernelObservation::SignalsPending { .. } => "signals_pending",
            KernelObservation::OperationCancelled { .. } => "operation_cancelled",
            KernelObservation::LivePolicyChanged { .. } => "live_policy_changed",
            KernelObservation::CapabilityChanged { .. } => "capability_changed",
            KernelObservation::AgentPreempted { .. } => "agent_preempted",
            KernelObservation::PayloadResidencyChanged { .. } => "payload_residency_changed",
            KernelObservation::PayloadLoadFailed { .. } => "payload_load_failed",
            _ => "other",
        }
    }

    /// The `(disposition, signal_id, delivery_id, attempt)` of every delivery the last transition
    /// disposed of.
    fn dispositions(runtime: &Runtime) -> Vec<(String, String, String, u32)> {
        runtime
            .observations()
            .iter()
            .filter_map(|observation| match observation {
                KernelObservation::SignalDeliveryDisposed {
                    disposition,
                    signal_id,
                    delivery_id,
                    attempt,
                    ..
                } => Some((
                    disposition.clone(),
                    signal_id.clone(),
                    delivery_id.clone(),
                    *attempt,
                )),
                _ => None,
            })
            .collect()
    }

    /// The rendered history of the operation, as text — what the model will read next turn.
    fn history_text(runtime: &Runtime) -> Vec<String> {
        runtime
            .driver
            .engine()
            .map(|engine| {
                engine
                    .ctx
                    .partitions
                    .history
                    .messages
                    .iter()
                    .map(|message| format!("{:?}:{}", message.role, message_text(message)))
                    .collect()
            })
            .unwrap_or_default()
    }

    fn message_text(message: &Message) -> String {
        match &message.content {
            Content::Text(text) => text.clone(),
            Content::Parts(parts) => parts
                .iter()
                .map(|part| match part {
                    crate::types::message::ContentPart::ToolResult { output, .. } => output.clone(),
                    other => format!("{other:?}"),
                })
                .collect::<Vec<_>>()
                .join(" "),
        }
    }

    // ----- slice 1 · provider / tool -----------------------------------------------------------

    #[test]
    fn a_provider_turn_with_host_tool_calls_publishes_execute_tools() {
        let (mut runtime, provider) = agent_awaiting_provider();
        let dispatched = runtime.submit(&provider_result(
            "in-search",
            1_700_000_002_000,
            &provider,
            vec![tool_call("call-1", "search", json!({"q": "sources"}))],
        ));
        assert_eq!(kinds(&dispatched), vec![EffectKindTag::ExecuteTools]);
        let EffectKind::ExecuteTools(execute) = &sole_effect(&dispatched).effect else {
            panic!("expected a tool batch");
        };
        assert_eq!(execute.calls.len(), 1);
        assert_eq!(execute.calls[0].name, "search");
        assert_eq!(execute.calls[0].call_id.as_str(), "call-1");

        // and the results resolve that effect and re-ask the provider — the ordinary turn cycle
        let resumed = runtime.submit(&tools_resolved(
            "in-results",
            1_700_000_003_000,
            &effect_id(dispatched.step_seq),
            &[("call-1", "three sources found", false)],
        ));
        assert_eq!(kinds(&resumed), vec![EffectKindTag::CallProvider]);
        assert!(
            history_text(&runtime)
                .iter()
                .any(|line| line.contains("three sources found")),
            "the tool output is what the next turn reads"
        );
    }

    #[test]
    fn a_provider_answer_with_no_tool_calls_commits_the_agent_terminal() {
        let (mut runtime, provider) = agent_awaiting_provider();
        let finished = runtime.submit(&provider_answer(
            "in-final",
            1_700_000_002_000,
            &provider,
            "the brief is written",
        ));
        let terminal = finished.terminal().expect("a final answer terminates");
        let KernelTerminal::Agent(agent) = terminal else {
            panic!("expected an agent terminal, got {terminal:?}");
        };
        assert_eq!(agent.result.termination, WireTermination::Completed);
        assert_eq!(
            agent.result.final_message.as_ref().unwrap().content,
            "the brief is written"
        );
        assert!(
            finished.published_effects().is_empty(),
            "§7.12 · a terminal step publishes no effect"
        );
    }

    // fixture: budget-usage-reported-once-at-terminal
    #[test]
    fn the_usage_report_rides_the_terminal_and_only_the_terminal() {
        let (mut runtime, provider) = agent_awaiting_provider();
        let finished = runtime.submit(&provider_answer(
            "in-final",
            1_700_000_002_000,
            &provider,
            "done",
        ));
        let KernelTerminal::Agent(agent) = finished.terminal().unwrap() else {
            panic!("expected an agent terminal");
        };
        let reported = agent.usage.clone();

        // Replaying the very input that committed the terminal answers with the same record and
        // does not mint a second report.
        let replayed = runtime.prepare(&provider_answer(
            "in-final",
            1_700_000_002_000,
            &provider,
            "done",
        ));
        let RecordPreparation::Replayed(replay) = replayed else {
            panic!("an exact replay is a replay, not a new step");
        };
        let Some(committed_step) = &replay.committed_step else {
            panic!("a replay above the checkpoint floor carries its step");
        };
        let StepDisposition::Terminal(terminal) = &committed_step.disposition else {
            panic!("the replayed step is the terminal one");
        };
        let KernelTerminal::Agent(replayed_agent) = &terminal.terminal else {
            panic!("expected an agent terminal");
        };
        assert_eq!(replayed_agent.usage, reported, "one report, one terminal");
        assert_eq!(replay.step_seq, finished.step_seq);
    }

    // fixture: agent-syscall-caller-is-derived (§5k · the syscall-only continuation)
    #[test]
    fn a_syscall_only_turn_continues_with_another_provider_call() {
        let (mut runtime, provider) = agent_awaiting_provider();
        let continued = runtime.submit(&provider_result(
            "in-plan",
            1_700_000_002_000,
            &provider,
            vec![
                tool_call("call-1", "skill", json!({"name": "debug"})),
                tool_call(
                    "call-2",
                    "update_plan",
                    json!({"progress": "sources listed"}),
                ),
            ],
        ));

        assert_eq!(
            kinds(&continued),
            vec![EffectKindTag::CallProvider],
            "a pure control-plane batch publishes no effect of its own, so the kernel continues \
             the turn itself rather than leaving the operation with nothing outstanding"
        );
        let history = history_text(&runtime);
        assert!(
            history.iter().any(|line| line.contains("skill activated")),
            "every syscall the kernel executed is answered so the transcript pairs: {history:?}"
        );
        assert!(
            history.iter().any(|line| line.contains("plan updated")),
            "{history:?}"
        );
        // and the assistant turn the model emitted is in history verbatim, calls included
        assert!(
            runtime
                .driver
                .engine()
                .unwrap()
                .ctx
                .partitions
                .history
                .messages
                .iter()
                .any(|message| message.tool_calls.len() == 2),
            "the model reads back the turn it actually emitted"
        );
    }

    #[test]
    fn a_syscall_batch_that_published_an_effect_waits_instead_of_re_asking() {
        let (mut runtime, provider) = agent_awaiting_provider();
        let queried = runtime.submit(&provider_result(
            "in-memory",
            1_700_000_002_000,
            &provider,
            vec![tool_call(
                "call-1",
                crate::context::manager::MEMORY_TOOL_NAME,
                json!({"query": "prior briefs"}),
            )],
        ));
        assert_eq!(
            kinds(&queried),
            vec![EffectKindTag::QueryMemory],
            "the turn resumes when the recall it asked for resolves; a provider call now would \
             race the very facts it was issued to read"
        );
    }

    #[test]
    fn a_mixed_batch_adjudicates_the_syscall_and_dispatches_the_tool() {
        let (mut runtime, provider) = agent_awaiting_provider();
        let mixed = runtime.submit(&provider_result(
            "in-mixed",
            1_700_000_002_000,
            &provider,
            vec![
                tool_call("call-1", "skill", json!({"name": "debug"})),
                tool_call("call-2", "search", json!({"q": "x"})),
            ],
        ));
        assert_eq!(kinds(&mixed), vec![EffectKindTag::ExecuteTools]);
        let EffectKind::ExecuteTools(execute) = &sole_effect(&mixed).effect else {
            panic!("expected a tool batch");
        };
        assert_eq!(
            execute
                .calls
                .iter()
                .map(|call| call.name.as_str())
                .collect::<Vec<_>>(),
            vec!["search"],
            "a P1 syscall is never dispatched to a host executor"
        );
        assert!(
            history_text(&runtime)
                .iter()
                .any(|line| line.contains("skill activated")),
            "the syscall half still closes its own transcript pair"
        );
    }

    // fixture: fault-effect-resolution-fails-closed
    #[test]
    fn a_context_overflow_compacts_and_re_asks_without_reading_vendor_text() {
        let mut runtime = Runtime::new();
        runtime.submit(&syscall_config());
        let started = runtime.submit(&agent_start_with_history("in-start", 1_700_000_001_000, 14));
        let provider = sole_effect(&started).effect_id.clone();

        let recovered = runtime.submit(&provider_overflow(
            "in-overflow",
            1_700_000_002_000,
            &provider,
        ));
        assert_eq!(
            kinds(&recovered),
            vec![EffectKindTag::CallProvider],
            "an overflow is a semantic outcome the kernel recovers from, not a transport failure"
        );
        assert!(
            observation_kinds(&runtime).contains(&"compressed"),
            "the recovery ladder ran: {:?}",
            observation_kinds(&runtime)
        );
    }

    #[test]
    fn canonical_genesis_installs_the_entropy_watch_on_the_engine() {
        let mut runtime = Runtime::new();
        runtime.submit(&syscall_config_with(|config| {
            config.execution_policy = Some(ExecutionPolicy {
                max_turns: Some(12),
                entropy_watch: Some(EntropyWatchPolicy {
                    enabled: Some(true),
                    threshold_ppm: Some(Ppm::new(100_000).unwrap()),
                    hysteresis_ppm: Some(Ppm::ZERO),
                    cooldown_turns: Some(0),
                    notify_model: Some(true),
                }),
                ..ExecutionPolicy::default()
            });
        }));
        let started = runtime.submit(&agent_start("in-start", 1_700_000_001_000));
        let acted = runtime.submit(&provider_result(
            "in-acted",
            1_700_000_002_000,
            &effect_id(started.step_seq),
            vec![tool_call("call-1", "search", json!({ "q": "same" }))],
        ));
        let resolved = runtime.submit(&tools_resolved(
            "in-results",
            1_700_000_003_000,
            &effect_id(acted.step_seq),
            &[("call-1", "failed", true)],
        ));

        assert!(
            resolved
                .step
                .observations
                .iter()
                .any(|observation| matches!(observation, KernelObservation::EntropyAlert { .. })),
            "the resolved canonical execution policy must arm the semantic engine's entropy watch"
        );
    }

    /// **Task 14 · core does not parse a raw vendor error string.**
    ///
    /// The legacy path classified overflow by substring-matching provider prose
    /// (`state_machine/eviction.rs::is_prompt_too_long`, §22.8). On the canonical face that
    /// reading has no entry point: the same words are ordinary content in a completion, ordinary
    /// diagnostics in a failure message, and neither reaches a recovery decision. Only the typed
    /// `ContextOverflow` outcome does.
    #[test]
    fn vendor_error_prose_is_content_and_never_a_recovery_decision() {
        const VENDOR_PROSE: &str = "HTTP 413: prompt is too long — context_length_exceeded, \
                                    maximum context length is 128000 tokens";

        // (a) as the model's own words: an ordinary completed turn
        let mut runtime = Runtime::new();
        runtime.submit(&syscall_config());
        let started = runtime.submit(&agent_start_with_history("in-start", 1_700_000_001_000, 14));
        let answered = runtime.submit(&provider_answer(
            "in-prose",
            1_700_000_002_000,
            &sole_effect(&started).effect_id.clone(),
            VENDOR_PROSE,
        ));
        assert!(
            answered.terminal().is_some() || kinds(&answered) == vec![EffectKindTag::CallProvider],
            "the words are content, not a classification"
        );
        assert!(
            !observation_kinds(&runtime).contains(&"compressed"),
            "no recovery ladder ran: {:?}",
            observation_kinds(&runtime)
        );

        // (b) as a host failure message: a failed terminal, not an overflow recovery
        let (mut runtime, provider) = agent_awaiting_provider();
        let ended = runtime.submit(&failed(
            "in-prose-failure",
            1_700_000_002_000,
            &provider,
            HostEffectFailureKind::TransportExhausted,
            VENDOR_PROSE,
        ));
        let Some(KernelTerminal::Failed(failure)) = ended.terminal() else {
            panic!("expected a failed terminal, got {:?}", ended.terminal());
        };
        assert_eq!(failure.failure.code, KernelFailureCode::HostEffectFailed);
        assert!(
            !observation_kinds(&runtime).contains(&"compressed"),
            "a failure message is diagnostics; it never selects the recovery ladder: {:?}",
            observation_kinds(&runtime)
        );
    }

    /// **Task 14 · no vendor vocabulary survives anywhere on the canonical face.**
    ///
    /// Every closed vocabulary the wire publishes, scanned against the words hosts historically
    /// forwarded verbatim. This is the regression guard for "the host maps, core never learns":
    /// adding a `rate_limited` failure class or a `stop` stop-reason would break it here rather
    /// than three releases later when something starts branching on it.
    #[test]
    fn no_canonical_vocabulary_contains_a_vendor_word() {
        const VENDOR_WORDS: [&str; 16] = [
            "rate_limit",
            "429",
            "503",
            "413",
            "overloaded",
            // `storage_unavailable` is the deliberate near-miss: a *storage* classification, not a
            // vendor's "service unavailable" — which folds into `transport_exhausted` once the
            // host's own ladder is spent. So the banned word is the vendor's compound, not the
            // bare adjective.
            "service_unavailable",
            "context_length",
            "max_context",
            "too_long",
            "finish_reason",
            "openai",
            "anthropic",
            "gemini",
            "deepseek",
            "qwen",
            "minimax",
        ];

        let mut vocabulary: Vec<&'static str> = Vec::new();
        vocabulary.extend(EffectKindTag::ALL.iter().map(|tag| tag.as_str()));
        vocabulary.extend(
            super::super::effect::EffectSuccessTag::ALL
                .iter()
                .map(|tag| tag.as_str()),
        );
        vocabulary.extend(HostEffectFailureKind::ALL.iter().map(|kind| kind.as_str()));
        vocabulary.extend(ProviderStopReason::ALL.iter().map(|r| r.as_str()));
        vocabulary.extend(ToolResultDisposition::ALL.iter().map(|d| d.as_str()));
        assert!(vocabulary.len() >= 34, "the scan lost a vocabulary");

        for word in vocabulary {
            for vendor in VENDOR_WORDS {
                assert!(
                    !word.contains(vendor),
                    "{word:?} carries the vendor word {vendor:?}; the canonical face is the \
                     host's mapping *target*, never its passthrough"
                );
            }
        }

        // `storage_unavailable` is the one near-miss and it is deliberate: it is a *storage*
        // classification, not a vendor's "service unavailable" — which folds into
        // `transport_exhausted` after the host's own ladder is spent.
        assert!(
            HostEffectFailureKind::ALL
                .iter()
                .any(|kind| kind.as_str() == "storage_unavailable")
        );
        assert!(
            !HostEffectFailureKind::ALL
                .iter()
                .any(|kind| kind.as_str().contains("service"))
        );
    }

    #[test]
    fn the_overflow_ladder_is_bounded_and_ends_in_an_honest_terminal() {
        let mut runtime = Runtime::new();
        runtime.submit(&syscall_config_with(|config| {
            config.recovery_policy = Some(crate::runtime::kernel::wire::command::RecoveryPolicy {
                provider_recovery_attempts: Some(1),
                ..Default::default()
            });
        }));
        let started = runtime.submit(&agent_start_with_history("in-start", 1_700_000_001_000, 14));
        let mut provider = sole_effect(&started).effect_id.clone();

        let first = runtime.submit(&provider_overflow("in-of-1", 1_700_000_002_000, &provider));
        assert_eq!(kinds(&first), vec![EffectKindTag::CallProvider]);
        provider = sole_effect(&first).effect_id.clone();

        let exhausted = runtime.submit(&provider_overflow("in-of-2", 1_700_000_003_000, &provider));
        let KernelTerminal::Agent(agent) = exhausted.terminal().expect("the ladder is bounded")
        else {
            panic!("expected an agent terminal");
        };
        assert_eq!(agent.result.termination, WireTermination::ContextOverflow);
    }

    // fixture: removed-kernel-auto-redispatch
    #[test]
    fn a_provider_failure_commits_a_terminal_and_never_re_asks() {
        let (mut runtime, provider) = agent_awaiting_provider();
        let ended = runtime.submit(&failed(
            "in-dead",
            1_700_000_002_000,
            &provider,
            HostEffectFailureKind::TransportExhausted,
            "the vendor returned 503 five times",
        ));
        assert!(
            ended.published_effects().is_empty(),
            "DEC-5 · the kernel makes one policy decision and does not re-emit the same intent"
        );
        let KernelTerminal::Failed(failure) = ended.terminal().expect("a terminal was committed")
        else {
            panic!("expected a failed terminal, got {:?}", ended.terminal());
        };
        assert_eq!(failure.failure.code, KernelFailureCode::HostEffectFailed);
        assert!(
            failure.failure.message.contains("transport_exhausted"),
            "the classification decides; the prose is only what an operator reads: {}",
            failure.failure.message
        );
    }

    // fixture: removed-kernel-auto-redispatch
    #[test]
    fn a_tool_batch_failure_answers_every_dispatched_call_and_never_re_runs_it() {
        let (mut runtime, provider) = agent_awaiting_provider();
        let dispatched = runtime.submit(&provider_result(
            "in-search",
            1_700_000_002_000,
            &provider,
            vec![
                tool_call("call-1", "search", json!({"q": "a"})),
                tool_call("call-2", "search", json!({"q": "b"})),
            ],
        ));
        let tools = effect_id(dispatched.step_seq);

        let answered = runtime.submit(&failed(
            "in-exec-failed",
            1_700_000_003_000,
            &tools,
            HostEffectFailureKind::PermissionDenied,
            "the executor is not allowed to run tools in this sandbox",
        ));
        assert_eq!(
            kinds(&answered),
            vec![EffectKindTag::CallProvider],
            "the batch is abandoned and the model is asked again — the kernel never re-dispatches \
             the same batch"
        );
        let history = history_text(&runtime);
        let refusals = history
            .iter()
            .filter(|line| line.contains("permission_denied"))
            .count();
        assert_eq!(
            refusals, 2,
            "every dispatched call still gets a result: {history:?}"
        );
    }

    // ----- §7.10 · tool result disposition (Task 14) --------------------------------------------

    /// A batch of two calls, dispatched and awaiting results.
    fn agent_dispatching_two_tools() -> (Runtime, EffectId) {
        let (mut runtime, provider) = agent_awaiting_provider();
        let dispatched = runtime.submit(&provider_result(
            "in-search",
            1_700_000_002_000,
            &provider,
            vec![
                tool_call("call-1", "search", json!({"q": "a"})),
                tool_call("call-2", "search", json!({"q": "b"})),
            ],
        ));
        assert_eq!(kinds(&dispatched), vec![EffectKindTag::ExecuteTools]);
        let tools = effect_id(dispatched.step_seq);
        (runtime, tools)
    }

    fn tool_batch(
        id: &str,
        at: u64,
        effect: &EffectId,
        results: &[(&str, &str, bool, ToolResultDisposition)],
    ) -> WireEnvelope {
        resolved(
            id,
            at,
            effect,
            EffectSuccess::Tools(ToolsSuccess {
                results: results
                    .iter()
                    .map(|(call_id, output, is_error, disposition)| {
                        WireToolResultPayload::Inline(InlineToolResult {
                            call_id: CallId::new(*call_id).unwrap(),
                            result: WireToolResult {
                                output: (*output).to_string(),
                                is_error: *is_error,
                                disposition: *disposition,
                                tokens: None,
                            },
                        })
                    })
                    .collect(),
            }),
        )
    }

    #[test]
    fn a_fatal_result_closes_out_the_calls_its_batch_never_answered() {
        // `fatal` = the executor stopped. The unanswered call is not pending — it will never be
        // answered — so leaving it alone would produce an assistant turn whose tool_call has no
        // matching tool_result, which is malformed on every vendor wire.
        let (mut runtime, tools) = agent_dispatching_two_tools();
        let settled = runtime.submit(&tool_batch(
            "in-fatal",
            1_700_000_003_000,
            &tools,
            &[(
                "call-1",
                "disk corrupt, aborting",
                true,
                ToolResultDisposition::Fatal,
            )],
        ));
        assert_eq!(
            kinds(&settled),
            vec![EffectKindTag::CallProvider],
            "the turn still completes and the model is asked again — a fatal result is model \
             feedback, not a rollback (v0.2.42)"
        );

        let history = history_text(&runtime);
        assert!(
            history.iter().any(|line| line.contains("disk corrupt")),
            "the failure the host reported stays visible: {history:?}"
        );
        // The pairing invariant is the point: both dispatched calls end the turn with a result.
        let answered = tool_result_call_ids(&runtime);
        assert_eq!(
            answered,
            vec!["call-1".to_string(), "call-2".to_string()],
            "the call the batch never answered is closed out"
        );
        assert!(
            history
                .iter()
                .any(|line| line.contains("not executed") && line.contains("fatally")),
            "the close-out says why the call did not run: {history:?}"
        );
    }

    /// Every `call_id` that has a committed tool result in history, in order.
    fn tool_result_call_ids(runtime: &Runtime) -> Vec<String> {
        runtime
            .driver
            .engine()
            .map(|engine| {
                engine
                    .ctx
                    .partitions
                    .history
                    .messages
                    .iter()
                    .flat_map(|message| match &message.content {
                        Content::Parts(parts) => parts
                            .iter()
                            .filter_map(|part| match part {
                                crate::types::message::ContentPart::ToolResult {
                                    call_id, ..
                                } => Some(call_id.to_string()),
                                _ => None,
                            })
                            .collect::<Vec<_>>(),
                        Content::Text(_) => Vec::new(),
                    })
                    .collect()
            })
            .unwrap_or_default()
    }

    #[test]
    fn an_ordinary_batch_closes_out_nothing() {
        // The close-out is fatal-only. A short *recoverable* batch says nothing about the calls it
        // omitted, so the kernel must not invent results for them.
        let (mut runtime, tools) = agent_dispatching_two_tools();
        runtime.submit(&tool_batch(
            "in-partial",
            1_700_000_003_000,
            &tools,
            &[(
                "call-1",
                "no matches",
                false,
                ToolResultDisposition::Recoverable,
            )],
        ));
        let history = history_text(&runtime);
        assert!(
            !history.iter().any(|line| line.contains("not executed")),
            "a recoverable batch closes nothing out: {history:?}"
        );
    }

    /// §7.10 rule 9 · the close-out is **total over residency**.
    ///
    /// A tool that fails after producing a huge diagnostic body is the common shape, not a rare
    /// one. If the fatality scan read only the inline arm, whether a fatal failure stopped the
    /// batch would depend on how many bytes its traceback happened to be.
    #[test]
    fn an_externalised_fatal_stops_the_batch_exactly_as_an_inline_one_does() {
        let (mut runtime, tools) = agent_dispatching_two_tools();
        let settled = runtime.submit(&resolved(
            "in-fatal-external",
            1_700_000_003_000,
            &tools,
            EffectSuccess::Tools(ToolsSuccess {
                results: vec![external_payload_with(
                    "call-1",
                    Digest::new(
                        "sha256:3b1f4a7c9e2d05186a4c7f0b9d3e8c25714f6a0b8c5d2e9f1a3b6c8d0e2f4a61",
                    )
                    .unwrap(),
                    524_288,
                    "Traceback (most recent call last):",
                    true,
                    ToolResultDisposition::Fatal,
                )],
            }),
        ));
        assert_eq!(kinds(&settled), vec![EffectKindTag::CallProvider]);
        assert_eq!(
            tool_result_call_ids(&runtime),
            vec!["call-1".to_string(), "call-2".to_string()],
            "an externalised fatal closes the batch out just like an inline one"
        );
        let history = history_text(&runtime);
        assert!(
            history
                .iter()
                .any(|line| line.contains("not executed") && line.contains("fatally")),
            "{history:?}"
        );

        // …and the failure itself stays visible as an error, which the old shape could not express
        // at all: an externalised failure was indistinguishable from an externalised success.
        assert!(
            runtime
                .driver
                .engine()
                .expect("engine")
                .ctx
                .partitions
                .history
                .messages
                .iter()
                .any(
                    |message| matches!(&message.content, Content::Parts(parts) if parts
                    .iter()
                    .any(|part| matches!(
                        part,
                        crate::types::message::ContentPart::ToolResult {
                            call_id,
                            is_error: true,
                            ..
                        } if call_id.as_str() == "call-1"
                    )))
                ),
            "the externalised failure is committed as an error result"
        );
    }

    #[test]
    fn a_fatal_result_does_not_synthesise_an_answer_the_host_already_gave() {
        // The close-out is keyed on *unanswered* calls, so a complete fatal batch adds nothing —
        // otherwise a call would get two results.
        let (mut runtime, tools) = agent_dispatching_two_tools();
        runtime.submit(&tool_batch(
            "in-fatal-complete",
            1_700_000_003_000,
            &tools,
            &[
                ("call-1", "boom", true, ToolResultDisposition::Fatal),
                (
                    "call-2",
                    "ok anyway",
                    false,
                    ToolResultDisposition::Recoverable,
                ),
            ],
        ));
        let history = history_text(&runtime);
        assert!(
            !history.iter().any(|line| line.contains("not executed")),
            "every call was answered by the host: {history:?}"
        );
        assert_eq!(
            history
                .iter()
                .filter(|line| line.contains("ok anyway"))
                .count(),
            1,
            "no call gets two results: {history:?}"
        );
    }

    // ----- §7.9 · DEC-5 differential (Task 14) --------------------------------------------------

    /// **Recovery exhaustion differential.** The kernel's decision on a failure is chosen by the
    /// effect kind *it* published, never by the failure kind the host reports and never by
    /// `retryable`. The strongest statement of that is byte equality: for every one of the ten
    /// effect kinds, all six failure classes × three `retryable` values must plan the same step.
    ///
    /// If any of those ever became an input, this is the test that breaks — which is the point,
    /// because "the kernel does not retry" is otherwise only a comment.
    #[test]
    fn a_host_failure_plans_the_same_step_whatever_the_host_advises() {
        for kind in HostEffectFailureKind::ALL {
            let mut planned: Option<Value> = None;
            for retryable in [None, Some(true), Some(false)] {
                let (mut runtime, tools) = agent_dispatching_two_tools();
                let settled = runtime.submit(&envelope(
                    "in-failed",
                    1_700_000_003_000,
                    KernelInput::ResolveEffect(ResolveEffect {
                        effect_id: tools.clone(),
                        outcome: EffectOutcome::Failed(EffectFailed {
                            failure: HostEffectFailure {
                                kind,
                                // the message is fixed: only the advice varies
                                message: "the executor could not run".to_string(),
                                retryable,
                            },
                        }),
                    }),
                ));
                let step = serde_json::to_value(&settled.step).unwrap();
                match &planned {
                    None => planned = Some(step),
                    Some(first) => assert_eq!(
                        first, &step,
                        "{kind:?} with retryable={retryable:?} planned a different step; \
                         `retryable` is advice and DEC-5 leaves it no branch to select"
                    ),
                }
            }
        }
    }

    #[test]
    fn the_recovery_decision_reads_the_effect_kind_the_kernel_published() {
        // The other half of the same claim: the *kind* the host reports does not select the
        // decision either. Six wildly different failure classes on the same effect all produce the
        // one decision that effect kind gets — here, "answer every call and ask the model again".
        for kind in HostEffectFailureKind::ALL {
            let (mut runtime, tools) = agent_dispatching_two_tools();
            let settled = runtime.submit(&failed(
                "in-failed",
                1_700_000_003_000,
                &tools,
                kind,
                "the executor could not run",
            ));
            assert_eq!(
                kinds(&settled),
                vec![EffectKindTag::CallProvider],
                "{kind:?} must take the same decision as every other failure class"
            );
            assert!(
                runtime.pending_effect_kinds() == vec![EffectKindTag::CallProvider],
                "{kind:?}: the failed batch is never re-issued (DEC-5)"
            );
        }
    }

    // fixture: fault-effect-resolution-fails-closed
    #[test]
    fn duplicate_and_conflicting_resolutions_are_settled_before_the_driver_sees_them() {
        let (mut runtime, provider) = agent_awaiting_provider();
        let first = runtime.submit(&provider_result(
            "in-skill",
            1_700_000_002_000,
            &provider,
            vec![tool_call("call-1", "skill", json!({"name": "debug"}))],
        ));

        // the same resolution under a fresh input id is a replay of the existing record (DEC-1)
        let replay = runtime.prepare(&provider_result(
            "in-skill-again",
            1_700_000_002_500,
            &provider,
            vec![tool_call("call-1", "skill", json!({"name": "debug"}))],
        ));
        let RecordPreparation::Replayed(replayed) = replay else {
            panic!("a semantically identical redelivery is a replay");
        };
        assert_eq!(replayed.step_seq, first.step_seq);

        // a *different* payload for the same effect is a conflict
        let conflict = runtime.reject(&provider_result(
            "in-skill-conflict",
            1_700_000_002_600,
            &provider,
            vec![tool_call("call-9", "skill", json!({"name": "debug"}))],
        ));
        assert_eq!(conflict.code, KernelFaultCode::UnexpectedEffectOutcome);

        // an effect nobody is waiting on
        let unknown = EffectId::new("op-driver-1:step:77:effect:0").unwrap();
        let stray = runtime.reject(&provider_answer(
            "in-stray",
            1_700_000_002_700,
            &unknown,
            "hello",
        ));
        assert_eq!(stray.code, KernelFaultCode::UnexpectedEffectOutcome);

        // and a result of the wrong *kind* for the pending effect
        let pending = effect_id(first.step_seq);
        let mismatched = runtime.reject(&tools_resolved(
            "in-mismatch",
            1_700_000_002_800,
            &pending,
            &[("call-1", "x", false)],
        ));
        assert_eq!(mismatched.code, KernelFaultCode::UnexpectedEffectOutcome);
    }

    // ----- slice 2 · workflow / control ---------------------------------------------------------

    // fixture: removed-kernel-auto-redispatch
    #[test]
    fn a_spawn_failure_fails_the_whole_batch_and_never_re_launches_it() {
        let mut runtime = Runtime::new();
        runtime.submit(&configure());
        let started = runtime.submit(&workflow_start(
            "in-start",
            1_700_000_001_000,
            two_node_spec(),
        ));
        let spawn = effect_id(started.step_seq);

        let after = runtime.submit(&failed(
            "in-launch-failed",
            1_700_000_002_000,
            &spawn,
            HostEffectFailureKind::ResourceExhausted,
            "no worker slots",
        ));
        assert!(
            !after
                .published_effects()
                .iter()
                .any(|effect| effect.tag() == EffectKindTag::SpawnTasks),
            "DEC-5 · the same launch intent is never re-emitted"
        );
        let KernelTerminal::Workflow(workflow) = after
            .terminal()
            .expect("a DAG whose only ready node could not start drains")
        else {
            panic!("expected a workflow terminal, got {:?}", after.terminal());
        };
        assert_eq!(workflow.outcome.status, WorkflowStatus::Failed);
        assert!(
            runtime.driver.attempts.is_empty(),
            "a launch that never happened leaves no live attempt to complete later"
        );
    }

    #[test]
    fn an_approval_resolution_dispatches_exactly_what_was_approved() {
        let (mut runtime, provider) = agent_awaiting_approval();
        let requested = runtime.submit(&provider_result(
            "in-gated",
            1_700_000_002_000,
            &provider,
            vec![
                tool_call("call-1", "search", json!({"q": "a"})),
                tool_call("call-2", "search", json!({"q": "b"})),
            ],
        ));
        assert_eq!(kinds(&requested), vec![EffectKindTag::RequestApproval]);
        let approval = effect_id(requested.step_seq);

        let resumed = runtime.submit(&resolved(
            "in-approved",
            1_700_000_003_000,
            &approval,
            EffectSuccess::Approval(ApprovalSuccess {
                approved_call_ids: vec![CallId::new("call-1").unwrap()],
                denied_call_ids: vec![CallId::new("call-2").unwrap()],
            }),
        ));
        assert_eq!(kinds(&resumed), vec![EffectKindTag::ExecuteTools]);
        let EffectKind::ExecuteTools(execute) = &sole_effect(&resumed).effect else {
            panic!("expected a tool batch");
        };
        assert_eq!(
            execute
                .calls
                .iter()
                .map(|call| call.call_id.as_str())
                .collect::<Vec<_>>(),
            vec!["call-1"],
            "only the approved call reaches a host"
        );
    }

    // fixture: removed-kernel-auto-redispatch
    #[test]
    fn an_approval_failure_denies_every_gated_call_and_never_re_asks() {
        let (mut runtime, provider) = agent_awaiting_approval();
        let requested = runtime.submit(&provider_result(
            "in-gated",
            1_700_000_002_000,
            &provider,
            vec![tool_call("call-1", "search", json!({"q": "a"}))],
        ));
        let approval = effect_id(requested.step_seq);

        let after = runtime.submit(&failed(
            "in-approval-failed",
            1_700_000_003_000,
            &approval,
            HostEffectFailureKind::StorageUnavailable,
            "the approval queue is down",
        ));
        assert!(
            !after
                .published_effects()
                .iter()
                .any(|effect| effect.tag() == EffectKindTag::RequestApproval),
            "DEC-5 · `retry_approval` is deleted on this wire"
        );
        assert_eq!(kinds(&after), vec![EffectKindTag::CallProvider]);
        assert!(
            observation_kinds(&runtime).contains(&"approval_resolution_failed"),
            "the failure is a typed audit fact: {:?}",
            observation_kinds(&runtime)
        );
        assert!(
            history_text(&runtime)
                .iter()
                .any(|line| line.contains("permission denied")),
            "fail closed: an approval that never arrived approves nothing"
        );
    }

    #[test]
    fn a_preempt_resolution_settles_every_attempt_it_names() {
        let (mut runtime, spawn_effect) = workflow_with_live_child();
        let attempts = vec![
            TaskPreemptOutcome {
                task_id: TaskId::new("wf-node0").unwrap(),
                attempt_id: WireAttemptId::new("wf-node0:attempt:1").unwrap(),
                outcome: TaskPreemptStatus::Preempted(TaskPreempted {}),
            },
            TaskPreemptOutcome {
                task_id: TaskId::new("wf-node1").unwrap(),
                attempt_id: WireAttemptId::new("wf-node1:attempt:1").unwrap(),
                outcome: TaskPreemptStatus::AlreadyFinished(TaskAlreadyFinished {}),
            },
        ];
        // The preempt effect has no canonical producer yet (its only trigger is the signal /
        // cancel path). Publishing it through the driver's own mint is what makes the *resolution*
        // contract — the half Task 12 owns — reachable.
        let carrier = syscall_carrier("in-preempt-effect", 1_700_000_004_000);
        let published = runtime.submit_planned(&carrier, |driver, context| {
            let mut index = 0;
            let effect = driver.mint_effect(
                context,
                EffectKind::PreemptTasks(PreemptTasksEffect {
                    attempts: vec![TaskAttemptRef {
                        task_id: TaskId::new("wf-node0").unwrap(),
                        attempt_id: WireAttemptId::new("wf-node0:attempt:1").unwrap(),
                    }],
                    reason: "cancelled".to_string(),
                }),
                &mut index,
            );
            Ok(PlannedStep {
                root_kind: Some(RootKind::Workflow),
                focus: driver.focus().cloned(),
                observations: Vec::new(),
                disposition: StepDisposition::Effects(EffectsDisposition {
                    effects: vec![effect],
                }),
            })
        });
        let preempt = effect_id(published.step_seq);
        assert!(runtime.driver.attempts.contains_key("wf-node0"));

        runtime.submit(&resolved(
            "in-preempted",
            1_700_000_005_000,
            &preempt,
            EffectSuccess::TasksPreempted(TasksPreemptedSuccess { attempts }),
        ));
        assert!(
            !runtime.driver.attempts.contains_key("wf-node0"),
            "a preempted attempt is spent, so a later completion naming it is a stale causation"
        );
        let _ = spawn_effect;
    }

    // fixture: removed-kernel-auto-redispatch
    #[test]
    fn a_preempt_failure_is_an_audit_fact_and_not_a_second_preemption() {
        let (mut runtime, _) = workflow_with_live_child();
        let carrier = syscall_carrier("in-preempt-effect", 1_700_000_004_000);
        let published = runtime.submit_planned(&carrier, |driver, context| {
            let mut index = 0;
            let effect = driver.mint_effect(
                context,
                EffectKind::PreemptTasks(PreemptTasksEffect {
                    attempts: vec![TaskAttemptRef {
                        task_id: TaskId::new("wf-node0").unwrap(),
                        attempt_id: WireAttemptId::new("wf-node0:attempt:1").unwrap(),
                    }],
                    reason: "cancelled".to_string(),
                }),
                &mut index,
            );
            Ok(PlannedStep {
                root_kind: Some(RootKind::Workflow),
                focus: driver.focus().cloned(),
                observations: Vec::new(),
                disposition: StepDisposition::Effects(EffectsDisposition {
                    effects: vec![effect],
                }),
            })
        });
        let preempt = effect_id(published.step_seq);

        let after = runtime.submit(&failed(
            "in-preempt-failed",
            1_700_000_005_000,
            &preempt,
            HostEffectFailureKind::Unknown,
            "the supervisor did not answer",
        ));
        assert!(
            after.published_effects().is_empty(),
            "DEC-5 · `retry_preempt` is deleted on this wire"
        );
        assert!(
            observation_kinds(&runtime).contains(&"agent_preempt_failed"),
            "{:?}",
            observation_kinds(&runtime)
        );
    }

    #[test]
    fn a_milestone_verdict_advances_the_contract_it_belongs_to() {
        let (mut runtime, provider) = agent_awaiting_milestone_check();
        let requested = runtime.submit(&provider_answer(
            "in-claim",
            1_700_000_002_000,
            &provider,
            "phase one is done",
        ));
        assert_eq!(kinds(&requested), vec![EffectKindTag::EvaluateMilestone]);
        let milestone = effect_id(requested.step_seq);

        let blocked = runtime.submit(&resolved(
            "in-verdict",
            1_700_000_003_000,
            &milestone,
            EffectSuccess::MilestoneEvaluated(MilestoneEvaluatedSuccess {
                result: WireMilestoneResult {
                    phase_id: "collect".to_string(),
                    passed: false,
                    failed_criteria: vec!["no sources cited".to_string()],
                    score: None,
                    notes: String::new(),
                },
            }),
        ));
        assert_eq!(kinds(&blocked), vec![EffectKindTag::CallProvider]);
        assert!(
            observation_kinds(&runtime).contains(&"milestone_blocked"),
            "{:?}",
            observation_kinds(&runtime)
        );
    }

    /// **`EvaluateMilestone`'s canonical producer, end to end** (Task 12 SPEC-ISSUE-4).
    ///
    /// Contract declared in `verification_contracts` → referenced by the root run spec → loaded as
    /// the engine's phase cascade → phase 0 published as an `EvaluateMilestone` → a passing verdict
    /// mounts that phase's `unlocks` and moves the cascade to phase 1. Before Task 14 the middle
    /// three links did not exist on the wire at all: the effect, its resolution and its failure
    /// path were reachable only by reaching into the engine from outside.
    #[test]
    fn a_declared_contract_drives_the_whole_milestone_cascade() {
        let (mut runtime, provider) = agent_awaiting_milestone_check();

        // link 3: the cascade is installed, so the first turn's completion asks for phase 0
        let requested = runtime.submit(&provider_answer(
            "in-claim",
            1_700_000_002_000,
            &provider,
            "sources collected",
        ));
        assert_eq!(kinds(&requested), vec![EffectKindTag::EvaluateMilestone]);
        let EffectKind::EvaluateMilestone(evaluate) = &sole_effect(&requested).effect else {
            panic!("expected a milestone request");
        };
        assert_eq!(
            evaluate.request.phase_id, "collect",
            "the request names the phase the declared cascade is on"
        );
        assert_eq!(
            evaluate.request.contract_id, "brief-quality-v1",
            "a phase id is unique only inside its contract, so the request carries the pair the \
             host looks its verifier up by"
        );
        // and nothing else: criteria, evidence and verifier are host-owned (§5.2)
        let request = serde_json::to_value(&evaluate.request).unwrap();
        assert_eq!(
            request.as_object().unwrap().keys().collect::<Vec<_>>(),
            vec!["contract_id", "phase_id"],
            "{request}"
        );

        // link 4: a pass mounts the phase's unlocks and advances to phase 1
        let advanced = runtime.submit(&resolved(
            "in-verdict",
            1_700_000_003_000,
            &effect_id(requested.step_seq),
            EffectSuccess::MilestoneEvaluated(MilestoneEvaluatedSuccess {
                result: WireMilestoneResult {
                    phase_id: "collect".to_string(),
                    passed: true,
                    failed_criteria: Vec::new(),
                    score: None,
                    notes: String::new(),
                },
            }),
        ));
        assert_eq!(kinds(&advanced), vec![EffectKindTag::CallProvider]);
        let advance = runtime
            .observations()
            .iter()
            .find_map(|observation| match observation {
                KernelObservation::MilestoneAdvanced {
                    phase_id,
                    capabilities_unlocked,
                    ..
                } => Some((phase_id.clone(), capabilities_unlocked.clone())),
                _ => None,
            })
            .unwrap_or_else(|| panic!("{:?}", observation_kinds(&runtime)));
        assert_eq!(advance.0, "collect");
        assert_eq!(
            advance.1,
            vec!["Tool:search".to_string()],
            "the phase's declared unlocks are the ones mounted"
        );
        assert_eq!(
            runtime
                .driver
                .engine()
                .expect("engine")
                .current_milestone_phase_id(),
            Some("write"),
            "the cascade advanced to the next declared phase"
        );

        // link 5: phase 1 unlocks a *skill*, proving the projection covers both directories
        let requested_2 = runtime.submit(&provider_answer(
            "in-claim-2",
            1_700_000_004_000,
            &effect_id(advanced.step_seq),
            "brief written",
        ));
        assert_eq!(kinds(&requested_2), vec![EffectKindTag::EvaluateMilestone]);
        runtime.submit(&resolved(
            "in-verdict-2",
            1_700_000_005_000,
            &effect_id(requested_2.step_seq),
            EffectSuccess::MilestoneEvaluated(MilestoneEvaluatedSuccess {
                result: WireMilestoneResult {
                    phase_id: "write".to_string(),
                    passed: true,
                    failed_criteria: Vec::new(),
                    score: None,
                    notes: String::new(),
                },
            }),
        ));
        let unlocked_by_phase_two =
            runtime
                .observations()
                .iter()
                .find_map(|observation| match observation {
                    KernelObservation::MilestoneAdvanced {
                        capabilities_unlocked,
                        ..
                    } => Some(capabilities_unlocked.clone()),
                    _ => None,
                });
        assert_eq!(unlocked_by_phase_two, Some(vec!["Skill:debug".to_string()]));
    }

    #[test]
    fn a_run_spec_naming_an_undeclared_contract_is_refused_before_anything_moves() {
        // A reference that resolves to nothing is a gate the run believes it has: the agent would
        // start with no cascade, never publish an `EvaluateMilestone`, and finish having "passed"
        // a contract that was never evaluated.
        let mut runtime = Runtime::new();
        runtime.submit(&syscall_config_with(|config| {
            config.host_effect_support = support_with([EffectKindTag::EvaluateMilestone]);
            config.verification_contracts = vec![brief_contract()];
        }));
        let head_before = runtime.tx.head();

        let fault = runtime.reject(&agent_start_under_contract(
            "in-start",
            1_700_000_001_000,
            "brief-quality-v2",
        ));
        assert_eq!(fault.code, KernelFaultCode::InvalidConfig);
        assert!(
            fault.message.contains("brief-quality-v2"),
            "{}",
            fault.message
        );
        assert_eq!(runtime.tx.head(), head_before, "nothing moved");
        assert!(
            runtime.driver.root_kind().is_none(),
            "the operation is still free to start with a spec that resolves"
        );

        // …and the same spec with the declared id starts normally
        runtime.submit(&agent_start_under_contract(
            "in-start-ok",
            1_700_000_001_500,
            "brief-quality-v1",
        ));
        assert_eq!(runtime.driver.root_kind(), Some(RootKind::Agent));
    }

    #[test]
    fn a_workflow_node_may_not_name_an_undeclared_contract_either() {
        // Same rule wherever a `LogicalAgentSpec` enters: a root, a DAG node, an authored append.
        let mut runtime = Runtime::new();
        runtime.submit(&syscall_config_with(|config| {
            config.host_effect_support = support_with([EffectKindTag::EvaluateMilestone]);
            config.verification_contracts = vec![brief_contract()];
        }));
        let mut spec = two_node_spec();
        spec.nodes[1].run_spec = Some(LogicalAgentSpec {
            verification_contract_id: Some("no-such-contract".to_string()),
            ..LogicalAgentSpec::new("write the brief")
        });
        let fault = runtime.reject(&workflow_start("in-start", 1_700_000_001_000, spec));
        assert_eq!(fault.code, KernelFaultCode::InvalidConfig);
        assert!(
            fault.message.contains("no-such-contract"),
            "{}",
            fault.message
        );
    }

    // fixture: removed-kernel-auto-redispatch
    #[test]
    fn a_milestone_that_could_not_be_evaluated_terminates_instead_of_advancing() {
        let (mut runtime, provider) = agent_awaiting_milestone_check();
        let requested = runtime.submit(&provider_answer(
            "in-claim",
            1_700_000_002_000,
            &provider,
            "phase one is done",
        ));
        let milestone = effect_id(requested.step_seq);

        let ended = runtime.submit(&failed(
            "in-verifier-down",
            1_700_000_003_000,
            &milestone,
            HostEffectFailureKind::StorageUnavailable,
            "the verifier could not be reached",
        ));
        assert!(ended.published_effects().is_empty());
        let KernelTerminal::Failed(failure) = ended.terminal().expect("a terminal was committed")
        else {
            panic!("expected a failed terminal, got {:?}", ended.terminal());
        };
        assert_eq!(failure.failure.code, KernelFailureCode::HostEffectFailed);
        assert!(
            failure.failure.message.contains("evaluate_milestone"),
            "{}",
            failure.failure.message
        );
    }

    // ----- slice 3 · memory / page-out ----------------------------------------------------------

    /// §22.13 · a receipt is a locator, never a rewrite of the record the kernel authored.
    #[test]
    fn a_memory_receipt_cannot_restate_what_the_kernel_authored() {
        let (mut runtime, effect) = agent_awaiting_memory_write();
        let settled = runtime.submit(&resolved(
            "in-persisted",
            1_700_000_003_000,
            &effect,
            EffectSuccess::MemoryPersisted(MemoryPersistedSuccess {
                receipt: MemoryPersistReceipt {
                    binding_id: MemoryBindingId::new("some-other-binding").unwrap(),
                    record_ref: MemoryRecordRef::new("rec-7").unwrap(),
                    digest: Digest::new("sha256:".to_string() + &"0".repeat(64)).unwrap(),
                },
            }),
        ));
        assert!(
            settled.published_effects().is_empty(),
            "a persisted record is a fact, not a new obligation"
        );
        let written = runtime
            .observations()
            .iter()
            .find_map(|observation| match observation {
                KernelObservation::MemoryWritten {
                    record_id,
                    scope,
                    name,
                    memory_kind,
                    size_bytes,
                    ..
                } => Some((
                    record_id.clone(),
                    scope.clone(),
                    name.clone(),
                    *memory_kind,
                    *size_bytes,
                )),
                _ => None,
            })
            .expect("the resolution records the write");
        assert_eq!(written.0, "rec-7", "the host contributes its own locator");
        assert_eq!(
            written.1.namespace, "mem-binding-1",
            "and nothing else: the binding is the one the operation holds, not the one echoed back"
        );
        assert_eq!(written.2, "brief-style");
        assert_eq!(written.3, crate::mm::memory::MemoryKind::Project);
        assert_eq!(written.4, "prefers numbered sections".len() as u32);
    }

    #[test]
    fn a_memory_write_failure_names_the_intent_the_kernel_authored() {
        let (mut runtime, effect) = agent_awaiting_memory_write();
        let settled = runtime.submit(&failed(
            "in-persist-failed",
            1_700_000_003_000,
            &effect,
            HostEffectFailureKind::StorageUnavailable,
            "the memory store is offline",
        ));
        assert!(settled.published_effects().is_empty());
        assert!(
            observation_kinds(&runtime).contains(&"memory_write_failed"),
            "{:?}",
            observation_kinds(&runtime)
        );
    }

    #[test]
    fn a_memory_recall_enters_context_before_the_turn_resumes() {
        let (mut runtime, provider) = agent_awaiting_provider();
        let queried = runtime.submit(&provider_result(
            "in-memory",
            1_700_000_002_000,
            &provider,
            vec![tool_call(
                "call-1",
                crate::context::manager::MEMORY_TOOL_NAME,
                json!({"query": "prior briefs", "top_k": 9}),
            )],
        ));
        let query_effect = sole_effect(&queried);
        let EffectKind::QueryMemory(query) = &query_effect.effect else {
            panic!("expected a memory query");
        };
        assert_eq!(
            query.requested_k, 4,
            "retrieval width is the operation's policy, clamped — a model cannot widen it"
        );
        let effect = query_effect.effect_id.clone();

        let resumed = runtime.submit(&resolved(
            "in-recalls",
            1_700_000_003_000,
            &effect,
            EffectSuccess::MemoryQueried(MemoryQueriedSuccess {
                recalls: vec![MemoryRecall {
                    record_ref: MemoryRecordRef::new("rec-1").unwrap(),
                    name: "brief-style".to_string(),
                    kind: SyscallMemoryKind::Project,
                    content: "prefers numbered sections".to_string(),
                    score: None,
                }],
            }),
        ));
        assert_eq!(kinds(&resumed), vec![EffectKindTag::CallProvider]);
        assert!(
            history_text(&runtime)
                .iter()
                .any(|line| line.contains("prefers numbered sections")),
            "the recall is in the context the resumed turn renders"
        );
        assert!(
            observation_kinds(&runtime).contains(&"memory_queried"),
            "{:?}",
            observation_kinds(&runtime)
        );
    }

    #[test]
    fn a_memory_query_failure_resumes_the_turn_without_recalls() {
        let (mut runtime, provider) = agent_awaiting_provider();
        let queried = runtime.submit(&provider_result(
            "in-memory",
            1_700_000_002_000,
            &provider,
            vec![tool_call(
                "call-1",
                crate::context::manager::MEMORY_TOOL_NAME,
                json!({"query": "prior briefs"}),
            )],
        ));
        let effect = effect_id(queried.step_seq);

        let resumed = runtime.submit(&failed(
            "in-query-failed",
            1_700_000_003_000,
            &effect,
            HostEffectFailureKind::StorageUnavailable,
            "the memory store is offline",
        ));
        assert_eq!(
            kinds(&resumed),
            vec![EffectKindTag::CallProvider],
            "a store that could not answer is not a reason to stall the run"
        );
        assert!(
            observation_kinds(&runtime).contains(&"memory_query_failed"),
            "{:?}",
            observation_kinds(&runtime)
        );
    }

    #[test]
    fn a_page_out_archive_holds_the_continuation_until_the_host_commits_it() {
        let (mut runtime, archive) = agent_awaiting_page_out();
        let EffectKind::ArchivePageOut(published) = &runtime
            .tx
            .pending_effects()
            .find(|effect| effect.effect_id == archive)
            .expect("the archive is pending")
            .effect
            .clone()
        else {
            panic!("expected a page-out effect");
        };

        let resumed = runtime.submit(&resolved(
            "in-archived",
            1_700_000_004_000,
            &archive,
            EffectSuccess::PageOutArchived(PageOutArchivedSuccess {
                receipt: ArchiveReceipt {
                    handle_id: published.handle_id.clone(),
                    payload_ref: PayloadRef::new("blob-1").unwrap(),
                    digest: published.payload.digest.clone(),
                    original_size: published.payload.original_size,
                },
            }),
        ));
        assert_eq!(
            kinds(&resumed),
            vec![EffectKindTag::CallProvider],
            "the provider retry the compaction deferred is released by the archive's commit"
        );
        assert!(
            observation_kinds(&runtime).contains(&"page_out_archived"),
            "{:?}",
            observation_kinds(&runtime)
        );
    }

    #[test]
    fn an_archive_receipt_for_another_body_is_refused() {
        let (mut runtime, archive) = agent_awaiting_page_out();
        let fault = runtime.reject(&resolved(
            "in-wrong-archive",
            1_700_000_004_000,
            &archive,
            EffectSuccess::PageOutArchived(PageOutArchivedSuccess {
                receipt: ArchiveReceipt {
                    handle_id: HandleId::new("some-other-handle").unwrap(),
                    payload_ref: PayloadRef::new("blob-1").unwrap(),
                    digest: Digest::new("sha256:".to_string() + &"1".repeat(64)).unwrap(),
                    original_size: WireU64::new(1),
                },
            }),
        ));
        assert_eq!(fault.code, KernelFaultCode::UnexpectedEffectOutcome);
    }

    // fixture: removed-kernel-auto-redispatch
    #[test]
    fn a_failed_archive_is_abandoned_and_the_run_stays_live() {
        let (mut runtime, archive) = agent_awaiting_page_out();
        let resumed = runtime.submit(&failed(
            "in-archive-failed",
            1_700_000_004_000,
            &archive,
            HostEffectFailureKind::StorageUnavailable,
            "the blob store is offline",
        ));
        assert_eq!(
            kinds(&resumed),
            vec![EffectKindTag::CallProvider],
            "DEC-5 · the archive is abandoned once; the compaction it belongs to already happened, \
             so the run continues degraded rather than dying on a best-effort durability effect"
        );
        assert!(
            observation_kinds(&runtime).contains(&"page_out_archive_failed"),
            "{:?}",
            observation_kinds(&runtime)
        );
    }

    /// A load outcome that answers no pending `LoadPayload` effect fails closed: since Task 13 the
    /// producer is the P1 `PageIn { handle_id }` syscall, and a resolution is only reducible
    /// against the effect it actually answers.
    #[test]
    fn a_payload_load_outcome_is_refused_with_its_reason() {
        let (mut runtime, provider) = agent_awaiting_provider();
        let fault = runtime.reject(&resolved(
            "in-loaded",
            1_700_000_002_000,
            &provider,
            EffectSuccess::PayloadLoaded(PayloadLoadedSuccess {
                handle_id: HandleId::new("call-1").unwrap(),
                payload: InlinePayload {
                    content: "body".to_string(),
                    digest: Digest::new("sha256:".to_string() + &"2".repeat(64)).unwrap(),
                    original_size: WireU64::new(4),
                },
            }),
        ));
        // the transaction refuses it first: a provider effect does not accept a payload result
        assert_eq!(fault.code, KernelFaultCode::UnexpectedEffectOutcome);
    }

    // ----- fixtures the failure vocabulary itself has to satisfy --------------------------------

    // fixture: removed-cancellation-as-effect-failure
    #[test]
    fn the_failure_vocabulary_cannot_express_a_cancellation() {
        for kind in HostEffectFailureKind::ALL {
            let label = kind.as_str();
            assert!(
                !label.contains("cancel"),
                "cancellation is a control-plane fact and only reaches the kernel through \
                 HostControl::Cancel; {label} would give one pending effect two meanings"
            );
        }
        assert_eq!(HostEffectFailureKind::ALL.len(), 6);
    }

    // ----- arcs the tests above start from ------------------------------------------------------

    fn agent_start_with_history(id: &str, at: u64, messages: usize) -> WireEnvelope {
        envelope(
            id,
            at,
            KernelInput::StartOperation(StartOperation {
                entry: RootEntry::Agent(RootAgentEntry {
                    task: LogicalTask::new("write the research brief"),
                    run_spec: None,
                }),
                initial_context: InitialContext {
                    messages: (0..messages)
                        .map(|index| super::super::root::LogicalMessage {
                            role: if index % 2 == 0 {
                                MessageRole::User
                            } else {
                                MessageRole::Assistant
                            },
                            content: format!(
                                "turn {index}: a long enough body that compaction has something \
                                 to reclaim when the prompt stops fitting"
                            ),
                            tokens: Some(64),
                            tool_call_id: None,
                        })
                        .collect(),
                    ..InitialContext::default()
                },
            }),
        )
    }

    /// An agent whose governance policy gates `search` behind an approval.
    fn agent_awaiting_approval() -> (Runtime, EffectId) {
        use crate::runtime::kernel::wire::command::{GovernancePolicy, PolicyAction, PolicyRule};

        let mut runtime = Runtime::new();
        runtime.submit(&syscall_config_with(|config| {
            config.host_effect_support = support_with([EffectKindTag::RequestApproval]);
            config.governance_policy = Some(GovernancePolicy {
                rules: vec![PolicyRule {
                    tool_pattern: "search".to_string(),
                    action: PolicyAction::AskUser,
                }],
                ..GovernancePolicy::default()
            });
        }));
        let started = runtime.submit(&agent_start("in-start", 1_700_000_001_000));
        (runtime, sole_effect(&started).effect_id.clone())
    }

    /// The two-phase skeleton the milestone arcs run on: `collect` unlocks the `search` tool,
    /// `write` unlocks the `debug` skill — one of each capability directory, so the projection is
    /// exercised in both directions.
    fn brief_contract() -> WireVerificationContract {
        WireVerificationContract {
            contract_id: "brief-quality-v1".to_string(),
            phases: vec![
                WireMilestonePhase {
                    phase_id: "collect".to_string(),
                    unlocks: vec!["search".to_string()],
                },
                WireMilestonePhase {
                    phase_id: "write".to_string(),
                    unlocks: vec!["debug".to_string()],
                },
            ],
        }
    }

    fn agent_start_under_contract(id: &str, at: u64, contract_id: &str) -> WireEnvelope {
        envelope(
            id,
            at,
            KernelInput::StartOperation(StartOperation {
                entry: RootEntry::Agent(RootAgentEntry {
                    task: LogicalTask::new("write the research brief"),
                    run_spec: Some(LogicalAgentSpec {
                        verification_contract_id: Some(contract_id.to_string()),
                        ..LogicalAgentSpec::new("write the research brief")
                    }),
                }),
                initial_context: InitialContext::default(),
            }),
        )
    }

    /// An agent carrying a two-phase milestone contract, declared the canonical way: the
    /// operation's `verification_contracts` catalog holds the skeleton and the root run spec
    /// references it by id (Task 14 · closes Task 12's SPEC-ISSUE-4, where nothing on the wire
    /// could make the kernel ask for a verdict).
    fn agent_awaiting_milestone_check() -> (Runtime, EffectId) {
        let mut runtime = Runtime::new();
        runtime.submit(&syscall_config_with(|config| {
            config.host_effect_support = support_with([EffectKindTag::EvaluateMilestone]);
            config.verification_contracts = vec![brief_contract()];
        }));
        let started = runtime.submit(&agent_start_under_contract(
            "in-start",
            1_700_000_001_000,
            "brief-quality-v1",
        ));
        (runtime, sole_effect(&started).effect_id.clone())
    }

    /// An agent that has published a `PersistMemory` effect through the child→parent request path.
    fn agent_awaiting_memory_write() -> (Runtime, EffectId) {
        use crate::runtime::kernel::wire::syscall::{
            MemoryWriteProposal, RequestMemoryWriteRequest,
        };

        let mut runtime = Runtime::new();
        runtime.submit(&syscall_config());
        let started = runtime.submit(&workflow_start(
            "in-start",
            1_700_000_001_000,
            two_node_spec(),
        ));
        runtime.submit(&spawned(
            "in-ack",
            1_700_000_002_000,
            &effect_id(started.step_seq),
            &["wf-node0"],
        ));
        let completed = runtime.submit(&child_done_with(
            "in-done",
            1_700_000_002_500,
            "wf-node0",
            "wf-node0:attempt:1",
            vec![SyscallRequest::RequestMemoryWrite(
                RequestMemoryWriteRequest {
                    proposal: MemoryWriteProposal {
                        name: "brief-style".to_string(),
                        kind: SyscallMemoryKind::Project,
                        content: "prefers numbered sections".to_string(),
                        description: String::new(),
                        evidence_refs: Vec::new(),
                    },
                },
            )],
        ));
        let effect = completed
            .published_effects()
            .iter()
            .find(|effect| effect.tag() == EffectKindTag::PersistMemory)
            .expect("the child's request published a memory write")
            .effect_id
            .clone();
        (runtime, effect)
    }

    /// An agent whose context overflowed hard enough to compact, so a page-out archive is pending
    /// and the provider retry it deferred is waiting behind it.
    fn agent_awaiting_page_out() -> (Runtime, EffectId) {
        let mut runtime = Runtime::new();
        runtime.submit(&syscall_config_with(|config| {
            config.host_effect_support = support_with([EffectKindTag::ArchivePageOut]);
        }));
        let started = runtime.submit(&agent_start_with_history("in-start", 1_700_000_001_000, 14));
        let provider = sole_effect(&started).effect_id.clone();
        let compacted = runtime.submit(&provider_overflow(
            "in-overflow",
            1_700_000_002_000,
            &provider,
        ));
        let archive = compacted
            .published_effects()
            .iter()
            .find(|effect| effect.tag() == EffectKindTag::ArchivePageOut)
            .expect("the compaction externalised its archive")
            .effect_id
            .clone();
        (runtime, archive)
    }

    /// A workflow root with its first node launched and acknowledged, so a live attempt exists.
    fn workflow_with_live_child() -> (Runtime, EffectId) {
        let mut runtime = Runtime::new();
        runtime.submit(&syscall_config());
        let started = runtime.submit(&workflow_start(
            "in-start",
            1_700_000_001_000,
            two_node_spec(),
        ));
        let spawn = effect_id(started.step_seq);
        runtime.submit(&spawned("in-ack", 1_700_000_002_000, &spawn, &["wf-node0"]));
        (runtime, spawn)
    }

    // -----------------------------------------------------------------------------------------
    // §7.5 · host control plane, and §7.7 · signal delivery (Task 12b)
    // -----------------------------------------------------------------------------------------

    fn control(id: &str, at: u64, command: HostCommand) -> WireEnvelope {
        envelope(
            id,
            at,
            KernelInput::HostControl(super::super::envelope::HostControl { command }),
        )
    }

    fn cancel_with(id: &str, at: u64, reason: CancellationReason) -> WireEnvelope {
        control(
            id,
            at,
            HostCommand::Cancel(CancelCommand {
                reason,
                pending_call_ids: Vec::new(),
            }),
        )
    }

    fn cancel(id: &str, at: u64) -> WireEnvelope {
        cancel_with(id, at, CancellationReason::User)
    }

    /// One signal delivery. `delivery` and `signal` are separate arguments precisely because they
    /// are separate identities (§7.7).
    fn signal_delivery(
        id: &str,
        at: u64,
        delivery: &str,
        attempt: u32,
        signal: LogicalSignal,
    ) -> WireEnvelope {
        envelope(
            id,
            at,
            KernelInput::DeliverExternalEvent(DeliverExternalEvent {
                event: ExternalEvent::DeliverSignal(DeliverSignal {
                    delivery_id: DeliveryId::new(delivery).unwrap(),
                    attempt,
                    signal,
                }),
            }),
        )
    }

    fn logical_signal(id: &str, urgency: SignalUrgency) -> LogicalSignal {
        LogicalSignal {
            urgency: Some(urgency),
            ..LogicalSignal::new(SignalId::new(id).unwrap())
        }
    }

    /// A configuration with a signal policy, so queue capacity and TTL are the operation's own
    /// facts rather than a compile-time default.
    fn signal_config(queue_max: u32, ttl_ms: Option<u64>) -> WireEnvelope {
        syscall_config_with(|config| {
            config.signal_policy = Some(super::super::command::SignalPolicy {
                queue_max,
                ttl_ms: ttl_ms.map(WireU64::new),
                deadline_escalation: None,
            });
        })
    }

    // fixture: cancel-flows-only-through-host-control
    #[test]
    fn cancellation_enters_only_through_the_control_plane_and_never_as_a_failure() {
        // an effect failure on the very call a cancel would abandon is a *failed* terminal, and no
        // wire shape lets it become a cancellation
        let (mut runtime, provider) = agent_awaiting_provider();
        let failed_out = runtime.submit(&failed(
            "in-transport-dead",
            1_700_000_002_000,
            &provider,
            HostEffectFailureKind::TransportExhausted,
            "the vendor gave up",
        ));
        assert!(
            matches!(failed_out.terminal(), Some(KernelTerminal::Failed(_))),
            "a host effect failure is never a cancellation (§14.3)"
        );

        // the control plane is the path that produces a cancellation
        let (mut runtime, _) = agent_awaiting_provider();
        let cancelled = runtime.submit(&cancel_with(
            "in-cancel",
            1_700_000_002_000,
            CancellationReason::Deadline,
        ));
        let Some(KernelTerminal::Cancelled(terminal)) = cancelled.terminal() else {
            panic!(
                "expected a cancelled terminal, got {:?}",
                cancelled.terminal()
            );
        };
        assert_eq!(
            terminal.reason,
            CancellationReason::Deadline,
            "the reason is the host's, not the loop's internal user-abort"
        );
        assert!(
            observation_kinds(&runtime).contains(&"operation_cancelled"),
            "{:?}",
            observation_kinds(&runtime)
        );

        // identity comes from the envelope alone: a cancel that repeats it does not decode, and a
        // cancel addressed at another operation never reaches the driver
        assert!(
            serde_json::from_value::<CancelCommand>(
                json!({ "reason": "user", "operation_id": "op-driver-1" })
            )
            .is_err(),
            "the envelope owns the operation id (§7.5)"
        );
        let (mut runtime, _) = agent_awaiting_provider();
        let mut foreign = cancel("in-foreign", 1_700_000_002_000);
        foreign.operation_id = OperationId::new("op-someone-else").unwrap();
        assert_eq!(
            runtime.reject(&foreign).code,
            KernelFaultCode::OperationMismatch
        );
    }

    // fixture: cancel-order-is-downstream-first
    #[test]
    fn cancellation_settles_every_downstream_wait_before_it_commits_the_root_terminal() {
        let (mut runtime, _) = workflow_with_live_child();
        assert!(
            runtime.driver.attempts.contains_key("wf-node0"),
            "the arc starts with a live child attempt"
        );

        let cancelled = runtime.submit(&cancel("in-cancel", 1_700_000_003_000));

        // downstream first, inside this one transition
        assert_eq!(
            runtime
                .driver
                .engine()
                .and_then(|engine| engine.task_lifecycle("wf-node0"))
                .map(TaskLifecycle::is_terminal),
            Some(true),
            "a running child is settled by the same step that cancels its parent"
        );
        assert!(
            runtime.driver.attempts.is_empty(),
            "a settled attempt is spent, so nothing downstream can be resumed by a late completion"
        );
        assert_eq!(
            runtime.pending_effect_kinds(),
            Vec::<EffectKindTag>::new(),
            "§11.1 · the cancelling step leaves nothing waiting on the host"
        );

        // and the root terminal is what that same step committed
        assert!(matches!(
            cancelled.terminal(),
            Some(KernelTerminal::Cancelled(_))
        ));
        assert!(
            cancelled.published_effects().is_empty(),
            "§7.12 · effects or a terminal, never both"
        );

        // the child's late completion cannot revive the operation
        assert_eq!(
            runtime
                .reject(&child_done(
                    "in-late-done",
                    1_700_000_004_000,
                    "wf-node0",
                    "finished anyway"
                ))
                .code,
            KernelFaultCode::InvalidLifecycle,
        );
    }

    // fixture: cancel-is-idempotent
    #[test]
    fn a_second_cancellation_commits_no_second_terminal() {
        let (mut runtime, _) = agent_awaiting_provider();
        let first = runtime.submit(&cancel("in-cancel", 1_700_000_002_000));
        let head = runtime.tx.head().map(|head| head.step_seq);

        // a re-issued cancellation under a fresh input id replays the record that already
        // committed the terminal (§18.3) — no second record, no second terminal, no new effect
        let replay = runtime.prepare(&cancel("in-cancel-again", 1_700_000_003_000));
        assert_eq!(replay.step_seq(), Some(first.step_seq));
        assert!(
            replay.record().unwrap().record_digest() == first.record.record_digest(),
            "the replay names the existing record"
        );
        assert_eq!(
            runtime.tx.head().map(|head| head.step_seq),
            head,
            "a replay moves no head"
        );

        // a cancellation that says something *different* is a conflict, not an overwrite
        assert_eq!(
            runtime
                .reject(&cancel_with(
                    "in-cancel-other",
                    1_700_000_003_000,
                    CancellationReason::LeaseLost
                ))
                .code,
            KernelFaultCode::DuplicateInputConflict,
        );
    }

    // fixture: cancel-terminal-rejects-all-state-changing-input
    #[test]
    fn a_terminal_refuses_every_state_changing_input_including_signal_delivery() {
        let (mut runtime, provider) = agent_awaiting_provider();
        runtime.submit(&cancel("in-cancel", 1_700_000_002_000));

        let head = runtime.tx.head().map(|head| head.step_seq);
        let queue_before = signal_queue_depth(&runtime);
        let journal_len = runtime.journal.len();

        for envelope in [
            signal_delivery(
                "in-late-signal",
                1_700_000_003_000,
                "delivery-late",
                1,
                logical_signal("sig-late", SignalUrgency::Critical),
            ),
            child_done("in-late-child", 1_700_000_003_000, "wf-node0", "done"),
            provider_answer("in-late-answer", 1_700_000_003_000, &provider, "too late"),
            agent_start("in-restart", 1_700_000_003_000),
            control(
                "in-late-compact",
                1_700_000_003_000,
                HostCommand::ForceCompact(super::super::command::ForceCompactCommand {}),
            ),
            control(
                "in-late-task",
                1_700_000_003_000,
                HostCommand::UpdateTask(UpdateTaskCommand {
                    update: WireTaskUpdate {
                        progress: Some("still going".to_string()),
                        ..WireTaskUpdate::default()
                    },
                }),
            ),
        ] {
            let input_id = envelope.input_id.clone();
            assert_eq!(
                runtime.reject(&envelope).code,
                KernelFaultCode::InvalidLifecycle,
                "{input_id} must be refused after the terminal",
            );
        }

        assert_eq!(
            runtime.tx.head().map(|head| head.step_seq),
            head,
            "no step sequence advanced"
        );
        assert_eq!(runtime.journal.len(), journal_len, "nothing was journaled");
        assert_eq!(
            signal_queue_depth(&runtime),
            queue_before,
            "a refused signal never reaches the queue"
        );
        assert!(
            runtime.driver.poison().is_none(),
            "a typed rejection is not a driver failure"
        );
    }

    fn signal_queue_depth(runtime: &Runtime) -> usize {
        runtime
            .driver
            .engine()
            .map(LoopStateMachine::signal_queue_depth)
            .unwrap_or(0)
    }

    // fixture: signal-delivery-identity-is-distinct
    #[test]
    fn delivery_identity_and_signal_identity_are_two_separate_facts() {
        let mut runtime = Runtime::new();
        runtime.submit(&signal_config(8, None));
        runtime.submit(&agent_start("in-start", 1_700_000_001_000));

        let first = signal_delivery(
            "in-sig-1",
            1_700_000_002_000,
            "delivery-a",
            1,
            LogicalSignal {
                dedupe_key: Some("nightly".to_string()),
                ..logical_signal("sig-nightly", SignalUrgency::Normal)
            },
        );
        runtime.submit(&first);
        assert_eq!(
            dispositions(&runtime),
            vec![(
                "queue".to_string(),
                "sig-nightly".to_string(),
                "delivery-a".to_string(),
                1
            )],
            "the audit fact names the caller's own signal id, never a minted one"
        );

        // the same delivery, retried: the envelope's idempotency key answers it as a replay, so the
        // signal is not disposed of twice
        let depth = signal_queue_depth(&runtime);
        let replay = runtime.prepare(&first);
        assert!(matches!(
            replay,
            super::super::fault::KernelPreparation::Replayed(_)
        ));
        assert_eq!(signal_queue_depth(&runtime), depth);

        // a *new* delivery of the same business signal is a distinct delivery attempt, and the
        // business dedupe key is what stops it becoming a second queued signal
        runtime.submit(&signal_delivery(
            "in-sig-2",
            1_700_000_003_000,
            "delivery-b",
            2,
            LogicalSignal {
                dedupe_key: Some("nightly".to_string()),
                ..logical_signal("sig-nightly", SignalUrgency::Normal)
            },
        ));
        assert_eq!(
            dispositions(&runtime),
            vec![(
                "ignore".to_string(),
                "sig-nightly".to_string(),
                "delivery-b".to_string(),
                2
            )],
            "a redelivery is recognisable as the same signal and a different delivery"
        );
        assert_eq!(
            signal_queue_depth(&runtime),
            depth,
            "and it does not queue a second copy"
        );

        // a delivery that cannot say which attempt it is fails closed
        assert_eq!(
            runtime
                .reject(&signal_delivery(
                    "in-sig-0",
                    1_700_000_004_000,
                    "delivery-c",
                    0,
                    logical_signal("sig-other", SignalUrgency::Normal),
                ))
                .code,
            KernelFaultCode::MalformedEnvelope,
        );
    }

    // fixture: signal-admission-uses-accepted-time
    #[test]
    fn signal_ttl_is_measured_from_the_accepted_envelope_time() {
        let mut runtime = Runtime::new();
        runtime.submit(&signal_config(8, Some(60_000)));
        runtime.submit(&agent_start("in-start", 1_700_000_001_000));

        // a source timestamp far older than the TTL — if admission read it, this signal would be
        // born expired
        runtime.submit(&signal_delivery(
            "in-sig-1",
            1_700_000_002_000,
            "delivery-a",
            1,
            LogicalSignal {
                source_timestamp_ms: Some(WireU64::new(1_600_000_000_000)),
                ..logical_signal("sig-stale-source", SignalUrgency::Normal)
            },
        ));
        assert_eq!(
            dispositions(&runtime)
                .iter()
                .map(|(disposition, ..)| disposition.clone())
                .collect::<Vec<_>>(),
            vec!["queue".to_string()],
            "admission uses the accepted envelope time; the source timestamp is metadata"
        );
        assert_eq!(signal_queue_depth(&runtime), 1);

        // and expiry is measured on the same clock: the next accepted input is far enough past the
        // *accepted* time of the first signal to expire it
        runtime.submit(&signal_delivery(
            "in-sig-2",
            1_700_000_200_000,
            "delivery-b",
            1,
            logical_signal("sig-fresh", SignalUrgency::Normal),
        ));
        assert!(
            observation_kinds(&runtime).contains(&"signal_expired"),
            "{:?}",
            observation_kinds(&runtime)
        );
        assert_eq!(
            signal_queue_depth(&runtime),
            1,
            "the stale signal left, the fresh one stayed"
        );
    }

    // fixture: signal-target-is-operation-or-task
    #[test]
    fn a_signal_addresses_the_operation_or_one_of_its_own_tasks() {
        let (mut runtime, _) = workflow_with_live_child();

        // the operation itself
        runtime.submit(&signal_delivery(
            "in-sig-op",
            1_700_000_003_000,
            "delivery-a",
            1,
            logical_signal("sig-op", SignalUrgency::Normal),
        ));
        assert_eq!(dispositions(&runtime).len(), 1);

        // one of its own logical tasks
        runtime.submit(&signal_delivery(
            "in-sig-task",
            1_700_000_004_000,
            "delivery-b",
            1,
            LogicalSignal {
                target: SignalTarget::Task(super::super::event::TaskTarget {
                    task_id: TaskId::new("wf-node0").unwrap(),
                }),
                ..logical_signal("sig-task", SignalUrgency::Normal)
            },
        ));
        assert_eq!(dispositions(&runtime).len(), 1);

        // a task this operation does not have
        assert_eq!(
            runtime
                .reject(&signal_delivery(
                    "in-sig-ghost",
                    1_700_000_005_000,
                    "delivery-c",
                    1,
                    LogicalSignal {
                        target: SignalTarget::Task(super::super::event::TaskTarget {
                            task_id: TaskId::new("ghost").unwrap(),
                        }),
                        ..logical_signal("sig-ghost", SignalUrgency::Normal)
                    },
                ))
                .code,
            KernelFaultCode::InvalidAuthority,
        );

        // a host session is not an address, and the wire has no slot for one
        assert!(
            serde_json::from_value::<SignalTarget>(
                json!({ "kind": "task", "task_id": "wf-node0", "session_id": "sess-1" })
            )
            .is_err(),
            "host session identity does not enter the event (§7.7)"
        );
    }

    // fixture: signal-target-is-operation-or-task
    #[test]
    fn a_full_signal_queue_drops_by_policy_and_leaves_an_audit_fact() {
        let mut runtime = Runtime::new();
        runtime.submit(&signal_config(1, None));
        runtime.submit(&agent_start("in-start", 1_700_000_001_000));

        runtime.submit(&signal_delivery(
            "in-sig-1",
            1_700_000_002_000,
            "delivery-a",
            1,
            logical_signal("sig-1", SignalUrgency::Normal),
        ));
        runtime.submit(&signal_delivery(
            "in-sig-2",
            1_700_000_003_000,
            "delivery-b",
            1,
            logical_signal("sig-2", SignalUrgency::Normal),
        ));
        assert_eq!(
            dispositions(&runtime)
                .iter()
                .map(|(disposition, ..)| disposition.clone())
                .collect::<Vec<_>>(),
            vec!["dropped".to_string()],
            "the configured capacity decides, and the loss is an audit fact"
        );
        assert_eq!(signal_queue_depth(&runtime), 1);
    }

    // fixture: signal-disposition-is-a-fact
    #[test]
    fn a_signal_disposition_is_a_fact_and_only_a_preemption_asks_the_host_for_anything() {
        // an ordinary signal: an audit fact and nothing to execute
        let mut runtime = Runtime::new();
        runtime.submit(&signal_config(8, None));
        runtime.submit(&agent_start("in-start", 1_700_000_001_000));
        let queued = runtime.submit(&signal_delivery(
            "in-sig-normal",
            1_700_000_002_000,
            "delivery-a",
            1,
            logical_signal("sig-normal", SignalUrgency::Normal),
        ));
        assert!(
            queued.published_effects().is_empty(),
            "queueing is a fact; it asks the host for nothing"
        );
        assert!(observation_kinds(&runtime).contains(&"signal_delivery_disposed"));

        // an urgent one that must stop running children *is* a host action, published as an effect
        let (mut runtime, _) = workflow_with_live_child();
        let interrupted = runtime.submit(&signal_delivery(
            "in-sig-critical",
            1_700_000_003_000,
            "delivery-b",
            1,
            logical_signal("sig-critical", SignalUrgency::Critical),
        ));
        assert_eq!(kinds(&interrupted), vec![EffectKindTag::PreemptTasks]);
        assert!(
            !observation_kinds(&runtime).contains(&"agent_preempted"),
            "the preemption is requested here, not committed: {:?}",
            observation_kinds(&runtime)
        );

        // only the host's resolution commits it
        let preempt = effect_id(interrupted.step_seq);
        runtime.submit(&resolved(
            "in-preempted",
            1_700_000_004_000,
            &preempt,
            EffectSuccess::TasksPreempted(super::super::effect::TasksPreemptedSuccess {
                attempts: vec![super::super::effect::TaskPreemptOutcome {
                    task_id: TaskId::new("wf-node0").unwrap(),
                    attempt_id: WireAttemptId::new("wf-node0:attempt:1").unwrap(),
                    outcome: super::super::effect::TaskPreemptStatus::Preempted(
                        super::super::effect::TaskPreempted {},
                    ),
                }],
            }),
        ));
        assert!(
            observation_kinds(&runtime).contains(&"agent_preempted"),
            "{:?}",
            observation_kinds(&runtime)
        );
    }

    #[test]
    fn an_operation_that_can_be_interrupted_is_one_that_can_stop_its_children() {
        // DEC-8 · the driver's own pre-check for `preempt_tasks` support before a critical signal
        // routes can never fire, and this is why: an operation that declares spawn capacity is
        // refused at genesis unless it also declares that it can stop what it started. The
        // guarantee lives at config time, so the signal path cannot plan an effect the host would
        // have to refuse.
        let mut runtime = Runtime::new();
        let fault = runtime.reject(&syscall_config_with(|config| {
            config.host_effect_support = HostEffectSupport::new([
                EffectKindTag::CallProvider,
                EffectKindTag::ExecuteTools,
                EffectKindTag::LoadPayload,
                EffectKindTag::SpawnTasks,
                EffectKindTag::PersistMemory,
                EffectKindTag::QueryMemory,
            ]);
        }));
        assert_eq!(fault.code, KernelFaultCode::InvalidConfig);
        assert!(fault.message.contains("preempt_tasks"), "{fault:?}");
    }

    // ----- §7.7 · escalate_after_ms (Task 14, adjudication §5n item 1) --------------------------

    /// A signal config with deadline escalation switched on.
    fn escalating_signal_config(queue_max: u32) -> WireEnvelope {
        syscall_config_with(|config| {
            config.signal_policy = Some(super::super::command::SignalPolicy {
                queue_max,
                ttl_ms: None,
                deadline_escalation: Some(true),
            });
        })
    }

    fn signal_escalating_after(id: &str, urgency: SignalUrgency, after_ms: u64) -> LogicalSignal {
        LogicalSignal {
            escalate_after_ms: Some(WireU64::new(after_ms)),
            ..logical_signal(id, urgency)
        }
    }

    #[test]
    fn a_due_escalation_raises_urgency_one_tier_and_is_anchored_to_the_accepted_time() {
        // A `low` signal is only observed. The same signal with a due `escalate_after_ms` is
        // `normal`, which queues for the next turn boundary — one tier, exactly.
        let mut runtime = Runtime::new();
        runtime.submit(&escalating_signal_config(8));
        runtime.submit(&agent_start("in-start", 1_700_000_001_000));
        runtime.submit(&signal_delivery(
            "in-sig-due",
            1_700_000_002_000,
            "delivery-a",
            1,
            signal_escalating_after("sig-due", SignalUrgency::Low, 0),
        ));
        assert_eq!(
            dispositions(&runtime)
                .iter()
                .map(|(disposition, ..)| disposition.clone())
                .collect::<Vec<_>>(),
            vec!["queue".to_string()],
            "a due deadline escalated low → normal"
        );

        // Not yet due: the same signal, same accepted time, a deadline in the future. This is what
        // "anchored to the envelope's accepted time" buys — the kernel needs no clock of its own to
        // tell the two apart.
        let mut runtime = Runtime::new();
        runtime.submit(&escalating_signal_config(8));
        runtime.submit(&agent_start("in-start", 1_700_000_001_000));
        runtime.submit(&signal_delivery(
            "in-sig-waiting",
            1_700_000_002_000,
            "delivery-a",
            1,
            signal_escalating_after("sig-waiting", SignalUrgency::Low, 60_000),
        ));
        assert_eq!(
            dispositions(&runtime)
                .iter()
                .map(|(disposition, ..)| disposition.clone())
                .collect::<Vec<_>>(),
            vec!["observe".to_string()],
            "a deadline that has not come due changes nothing"
        );
    }

    #[test]
    fn escalation_is_inert_unless_the_operation_asked_for_it() {
        // The field is a request; `signal_policy.deadline_escalation` is the operation's consent.
        // Without it the same bytes decode to the same signal and route the same way.
        let mut runtime = Runtime::new();
        runtime.submit(&signal_config(8, None));
        runtime.submit(&agent_start("in-start", 1_700_000_001_000));
        runtime.submit(&signal_delivery(
            "in-sig",
            1_700_000_002_000,
            "delivery-a",
            1,
            signal_escalating_after("sig-inert", SignalUrgency::Low, 0),
        ));
        assert_eq!(
            dispositions(&runtime)
                .iter()
                .map(|(disposition, ..)| disposition.clone())
                .collect::<Vec<_>>(),
            vec!["observe".to_string()],
            "escalation without the policy is inert"
        );
    }

    #[test]
    fn an_escalation_that_reaches_critical_takes_the_whole_interrupt_arc() {
        // The full arc: a `high` signal that waited long enough becomes `critical`, and critical
        // while children are running is the one disposition that asks the host for something. This
        // is also what the driver's pre-admission `effective_urgency` check has to agree with — it
        // reads the escalated value precisely so a delivery that will publish a `PreemptTasks` is
        // adjudicated against DEC-8 before the router moves.
        let mut runtime = Runtime::new();
        runtime.submit(&escalating_signal_config(8));
        let started = runtime.submit(&workflow_start(
            "in-start",
            1_700_000_001_000,
            two_node_spec(),
        ));
        runtime.submit(&spawned(
            "in-ack",
            1_700_000_002_000,
            &effect_id(started.step_seq),
            &["wf-node0"],
        ));

        let interrupted = runtime.submit(&signal_delivery(
            "in-sig-escalated",
            1_700_000_003_000,
            "delivery-a",
            1,
            signal_escalating_after("sig-escalated", SignalUrgency::High, 0),
        ));
        assert_eq!(
            kinds(&interrupted),
            vec![EffectKindTag::PreemptTasks],
            "high + due deadline = critical, and critical while busy preempts"
        );

        // …and without the escalation the same signal is only a soft interrupt: nothing published.
        let mut runtime = Runtime::new();
        runtime.submit(&escalating_signal_config(8));
        let started = runtime.submit(&workflow_start(
            "in-start",
            1_700_000_001_000,
            two_node_spec(),
        ));
        runtime.submit(&spawned(
            "in-ack",
            1_700_000_002_000,
            &effect_id(started.step_seq),
            &["wf-node0"],
        ));
        let soft = runtime.submit(&signal_delivery(
            "in-sig-plain",
            1_700_000_003_000,
            "delivery-a",
            1,
            logical_signal("sig-plain", SignalUrgency::High),
        ));
        assert!(
            soft.published_effects().is_empty(),
            "an unescalated high signal waits for the next boundary"
        );
    }

    #[test]
    fn a_signal_carries_a_duration_not_a_deadline() {
        // DEC-2 · an absolute instant would be a second host clock on the wire, and the same
        // intent would decode to a different deadline on every redelivery. Only the duration
        // spelling exists.
        let signal = signal_escalating_after("sig", SignalUrgency::Normal, 30_000);
        let value = serde_json::to_value(&signal).unwrap();
        assert_eq!(value["escalate_after_ms"], json!("30000"));
        for banned in ["deadline_ms", "escalate_at_ms", "expires_at_ms", "now_ms"] {
            assert!(
                value.get(banned).is_none(),
                "a signal must not carry {banned}"
            );
            let mut with_instant = value.clone();
            with_instant
                .as_object_mut()
                .unwrap()
                .insert(banned.to_string(), json!("1700000000000"));
            assert!(
                serde_json::from_value::<LogicalSignal>(with_instant).is_err(),
                "{banned} must not decode"
            );
        }

        // and the field is optional: a signal that never escalates is the common case
        let plain = serde_json::to_value(logical_signal("sig", SignalUrgency::Normal)).unwrap();
        assert!(plain.get("escalate_after_ms").is_none());
    }

    #[test]
    fn an_urgent_signal_never_publishes_a_second_provider_request() {
        // DEC-3 · a provider call is already pending, so the interrupt is admitted to the attention
        // partition and read at the next turn boundary rather than re-asking now.
        let (mut runtime, provider) = agent_awaiting_provider();
        let interrupted = runtime.submit(&signal_delivery(
            "in-sig-critical",
            1_700_000_002_000,
            "delivery-a",
            1,
            logical_signal("sig-critical", SignalUrgency::Critical),
        ));
        assert!(
            interrupted.published_effects().is_empty(),
            "a second provider call would be refused by §15.3, so none is planned"
        );
        assert_eq!(
            dispositions(&runtime)
                .iter()
                .map(|(disposition, ..)| disposition.clone())
                .collect::<Vec<_>>(),
            vec!["interrupt".to_string()],
            "the disposition reports what actually happened"
        );

        // and the pending call still resolves normally, carrying the interrupt into that turn
        let answered = runtime.submit(&provider_result(
            "in-answer",
            1_700_000_003_000,
            &provider,
            vec![tool_call("call-1", "search", json!({"q": "now what"}))],
        ));
        assert_eq!(kinds(&answered), vec![EffectKindTag::ExecuteTools]);
    }

    // -----------------------------------------------------------------------------------------
    // §7.5 · the remaining host commands
    // -----------------------------------------------------------------------------------------

    #[test]
    fn a_host_task_update_and_a_model_task_update_share_a_payload_but_not_an_authority() {
        let (mut runtime, provider) = agent_awaiting_provider();
        let committed = runtime.submit(&control(
            "in-host-plan",
            1_700_000_002_000,
            HostCommand::UpdateTask(UpdateTaskCommand {
                update: WireTaskUpdate {
                    plan: Some(vec!["collect".to_string(), "write".to_string()]),
                    progress: Some("host set the plan".to_string()),
                    ..WireTaskUpdate::default()
                },
            }),
        ));
        assert!(
            committed.published_effects().is_empty() && committed.terminal().is_none(),
            "a control command changes kernel state and publishes nothing"
        );
        assert_eq!(
            runtime
                .driver
                .engine()
                .map(|engine| engine.ctx.partitions.task_state.progress.clone()),
            Some("host set the plan".to_string()),
        );

        // the model reaches the same mutation through the P1 syscall path, which *is* gated —
        // it must be a call the turn advertised, attributed to a derived caller
        let acted = runtime.submit(&provider_result(
            "in-model-plan",
            1_700_000_003_000,
            &provider,
            vec![tool_call(
                "call-1",
                "update_plan",
                json!({ "progress": "model set the plan" }),
            )],
        ));
        assert_eq!(kinds(&acted), vec![EffectKindTag::CallProvider]);
        assert_eq!(
            runtime
                .driver
                .engine()
                .map(|engine| engine.ctx.partitions.task_state.progress.clone()),
            Some("model set the plan".to_string()),
        );
    }

    #[test]
    fn seeded_and_mutated_knowledge_enter_the_same_partition() {
        let (mut runtime, _) = agent_awaiting_provider();
        runtime.submit(&control(
            "in-seed",
            1_700_000_002_000,
            HostCommand::SeedKnowledge(SeedKnowledgeCommand {
                entries: vec![super::super::root::KnowledgeEntry {
                    content: "the house style forbids bullet lists".to_string(),
                    key: Some("style".to_string()),
                    tokens: Some(9),
                    pinned: true,
                }],
            }),
        ));
        assert!(
            knowledge_text(&runtime)
                .iter()
                .any(|text| text.contains("house style")),
            "{:?}",
            knowledge_text(&runtime)
        );

        // the keyed removal half is boundary-deferred, so it is accepted here and swept later
        let committed = runtime.submit(&control(
            "in-knowledge",
            1_700_000_003_000,
            HostCommand::ApplyKnowledgeMutation(ApplyKnowledgeMutationCommand {
                mutation: super::super::command::KnowledgeMutation {
                    upsert: vec![super::super::root::KnowledgeEntry {
                        content: "cite at least three sources".to_string(),
                        key: Some("sources".to_string()),
                        tokens: Some(6),
                        pinned: false,
                    }],
                    remove: vec!["style".to_string(), "never-seen".to_string()],
                },
            }),
        ));
        assert!(committed.published_effects().is_empty());
        assert!(
            knowledge_text(&runtime)
                .iter()
                .any(|text| text.contains("three sources")),
            "{:?}",
            knowledge_text(&runtime)
        );
    }

    fn knowledge_text(runtime: &Runtime) -> Vec<String> {
        runtime
            .driver
            .engine()
            .map(|engine| {
                engine
                    .ctx
                    .partitions
                    .knowledge
                    .messages()
                    .map(message_text)
                    .collect()
            })
            .unwrap_or_default()
    }

    #[test]
    fn a_capability_patch_mounts_and_unmounts_in_one_step() {
        use super::super::root::{CapabilityGrant, CapabilityKind as WireCapabilityKind};

        let (mut runtime, _) = agent_awaiting_provider();
        runtime.submit(&control(
            "in-mount",
            1_700_000_002_000,
            HostCommand::ApplyCapabilityPatch(ApplyCapabilityPatchCommand {
                patch: super::super::command::CapabilityPatch {
                    mount: vec![CapabilityGrant {
                        kind: WireCapabilityKind::McpServer,
                        id: "github".to_string(),
                        description: Some("issue and PR access".to_string()),
                    }],
                    unmount: Vec::new(),
                },
            }),
        ));
        assert!(observation_kinds(&runtime).contains(&"capability_changed"));
        assert!(
            runtime
                .driver
                .engine()
                .unwrap()
                .ctx
                .capabilities
                .capabilities()
                .iter()
                .any(|capability| capability.id == "github")
        );

        // withdrawing something already absent errs open, so a retry is safe
        runtime.submit(&control(
            "in-unmount",
            1_700_000_003_000,
            HostCommand::ApplyCapabilityPatch(ApplyCapabilityPatchCommand {
                patch: super::super::command::CapabilityPatch {
                    mount: Vec::new(),
                    unmount: vec![
                        super::super::root::CapabilityRef {
                            kind: WireCapabilityKind::McpServer,
                            id: "github".to_string(),
                        },
                        super::super::root::CapabilityRef {
                            kind: WireCapabilityKind::McpServer,
                            id: "never-mounted".to_string(),
                        },
                    ],
                },
            }),
        ));
        assert!(
            !runtime
                .driver
                .engine()
                .unwrap()
                .ctx
                .capabilities
                .capabilities()
                .iter()
                .any(|capability| capability.id == "github")
        );
    }

    #[test]
    fn a_skill_swap_is_atomic_and_refuses_a_name_outside_the_catalog() {
        let (mut runtime, _) = agent_awaiting_provider();
        let before = runtime.driver.engine().unwrap().ctx.active_skills.len();

        // one undeclared name refuses the whole swap — nothing is half-applied
        assert_eq!(
            runtime
                .reject(&control(
                    "in-bad-skill",
                    1_700_000_002_000,
                    HostCommand::ApplySkillActivation(ApplySkillActivationCommand {
                        activate: vec![
                            super::super::command::SkillActivation {
                                name: "debug".to_string(),
                                lease_turns: None,
                            },
                            super::super::command::SkillActivation {
                                name: "invented".to_string(),
                                lease_turns: None,
                            },
                        ],
                        deactivate: Vec::new(),
                    }),
                ))
                .code,
            KernelFaultCode::InvalidConfig,
        );
        assert_eq!(
            runtime.driver.engine().unwrap().ctx.active_skills.len(),
            before,
            "a refused swap left nothing behind"
        );

        runtime.submit(&control(
            "in-skill",
            1_700_000_002_000,
            HostCommand::ApplySkillActivation(ApplySkillActivationCommand {
                activate: vec![super::super::command::SkillActivation {
                    name: "debug".to_string(),
                    lease_turns: Some(2),
                }],
                deactivate: Vec::new(),
            }),
        ));
        assert!(
            runtime
                .driver
                .engine()
                .unwrap()
                .ctx
                .active_skills
                .iter()
                .any(|(skill, _)| skill == "debug")
        );
    }

    #[test]
    fn a_live_policy_patch_is_revision_guarded_and_takes_effect() {
        let mut runtime = Runtime::new();
        runtime.submit(&signal_config(8, None));
        runtime.submit(&agent_start("in-start", 1_700_000_001_000));

        // a stale revision changes nothing at all
        let stale = control(
            "in-stale-policy",
            1_700_000_002_000,
            HostCommand::ApplyPolicyPatch(ApplyPolicyPatchCommand {
                expected_revision: WireU64::new(7),
                patch: super::super::command::LivePolicyPatch::ReplaceSignalPolicy(
                    super::super::command::ReplaceSignalPolicy {
                        policy: super::super::command::SignalPolicy {
                            queue_max: 1,
                            ttl_ms: None,
                            deadline_escalation: None,
                        },
                    },
                ),
            }),
        );
        let fault = runtime.reject(&stale);
        assert_eq!(fault.code, KernelFaultCode::InvalidConfig);
        assert!(fault.message.contains("revision mismatch"), "{fault:?}");
        assert_eq!(
            runtime
                .driver
                .policy
                .as_ref()
                .map(LivePolicyState::revision),
            Some(WireU64::ZERO),
        );

        // a widening quota is refused rather than clamped
        let widening = control(
            "in-widen",
            1_700_000_002_000,
            HostCommand::ApplyPolicyPatch(ApplyPolicyPatchCommand {
                expected_revision: WireU64::ZERO,
                patch: super::super::command::LivePolicyPatch::TightenResourceQuota(
                    super::super::command::TightenResourceQuota {
                        max_workflow_nodes: Some(999),
                        ..Default::default()
                    },
                ),
            }),
        );
        assert!(
            runtime
                .reject(&widening)
                .message
                .contains("may only tighten")
        );

        // a well-formed patch applies, advances the revision, and reaches the running engine
        runtime.submit(&control(
            "in-policy",
            1_700_000_002_000,
            HostCommand::ApplyPolicyPatch(ApplyPolicyPatchCommand {
                expected_revision: WireU64::ZERO,
                patch: super::super::command::LivePolicyPatch::ReplaceSignalPolicy(
                    super::super::command::ReplaceSignalPolicy {
                        policy: super::super::command::SignalPolicy {
                            queue_max: 1,
                            ttl_ms: None,
                            deadline_escalation: None,
                        },
                    },
                ),
            }),
        ));
        assert_eq!(
            runtime
                .driver
                .policy
                .as_ref()
                .map(LivePolicyState::revision),
            Some(WireU64::new(1)),
        );
        assert!(
            observation_kinds(&runtime).contains(&"live_policy_changed"),
            "{:?}",
            observation_kinds(&runtime)
        );

        // the new capacity is the one the router enforces from here on
        runtime.submit(&signal_delivery(
            "in-sig-1",
            1_700_000_003_000,
            "delivery-a",
            1,
            logical_signal("sig-1", SignalUrgency::Normal),
        ));
        runtime.submit(&signal_delivery(
            "in-sig-2",
            1_700_000_004_000,
            "delivery-b",
            1,
            logical_signal("sig-2", SignalUrgency::Normal),
        ));
        assert_eq!(
            dispositions(&runtime)
                .iter()
                .map(|(disposition, ..)| disposition.clone())
                .collect::<Vec<_>>(),
            vec!["dropped".to_string()],
            "the patched queue_max is what the next admission decision reads"
        );
    }

    #[test]
    fn an_absolute_deadline_becomes_the_wall_budget_axis() {
        let (mut runtime, provider) = agent_awaiting_provider();
        // the operation's clock started at the root start
        runtime.submit(&control(
            "in-deadline",
            1_700_000_002_000,
            HostCommand::UpdateDeadline(UpdateDeadlineCommand {
                deadline_ms: Some(WireU64::new(1_700_000_001_500)),
            }),
        ));

        // the axis is read at the one funnel that issues a provider request, so the deadline is
        // seen the next time the loop asks a question rather than mid-flight
        let acted = runtime.submit(&provider_result(
            "in-acted",
            1_700_000_003_000,
            &provider,
            vec![tool_call("call-1", "search", json!({ "q": "sources" }))],
        ));
        let results = runtime.submit(&tools_resolved(
            "in-results",
            1_700_000_004_000,
            &effect_id(acted.step_seq),
            &[("call-1", "three sources", false)],
        ));
        assert_eq!(
            kinds(&results),
            vec![EffectKindTag::CallProvider],
            "an exhausted budget still buys exactly one bounded final turn"
        );

        let ended = runtime.submit(&provider_answer(
            "in-answer",
            1_700_000_005_000,
            &effect_id(results.step_seq),
            "half a thought",
        ));
        let Some(KernelTerminal::Agent(agent)) = ended.terminal() else {
            panic!("expected the deadline to end the operation, got {ended:?}");
        };
        assert_eq!(agent.result.termination, WireTermination::Deadline);
    }

    #[test]
    fn a_forced_compaction_publishes_the_archive_it_produced() {
        let mut runtime = Runtime::new();
        runtime.submit(&syscall_config_with(|config| {
            config.host_effect_support = support_with([EffectKindTag::ArchivePageOut]);
        }));
        runtime.submit(&agent_start_with_history("in-start", 1_700_000_001_000, 14));

        let compacted = runtime.submit(&control(
            "in-compact",
            1_700_000_002_000,
            HostCommand::ForceCompact(super::super::command::ForceCompactCommand {}),
        ));
        assert_eq!(kinds(&compacted), vec![EffectKindTag::ArchivePageOut]);
        assert!(observation_kinds(&runtime).contains(&"compressed"));
        assert!(
            compacted
                .step
                .observations
                .iter()
                .any(|observation| matches!(observation, KernelObservation::Compressed { .. })),
            "the committed planned step is the host publication channel for observations"
        );
    }

    // -----------------------------------------------------------------------------------------
    // lifecycle goldens
    // -----------------------------------------------------------------------------------------

    fn fixture_dir() -> PathBuf {
        PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/kernel-wire")
    }

    fn golden(name: &str, produced: &Value) -> Value {
        let path = fixture_dir().join(name);
        if std::env::var("BLESS_KERNEL_RECORD_FIXTURES").as_deref() == Ok("1") {
            let mut text = serde_json::to_string_pretty(produced).unwrap();
            text.push('\n');
            fs::write(&path, text).unwrap_or_else(|e| panic!("cannot bless {name}: {e}"));
            return produced.clone();
        }
        let raw = fs::read_to_string(&path).unwrap_or_else(|e| {
            panic!("missing golden {name} ({e}); re-bless with BLESS_KERNEL_RECORD_FIXTURES=1")
        });
        serde_json::from_str(&raw).unwrap_or_else(|e| panic!("{name} is not JSON: {e}"))
    }

    fn link(envelope: &WireEnvelope, committed: &CommittedTransition<PlannedStep>) -> Value {
        json!({
            "envelope": serde_json::to_value(envelope).unwrap(),
            "step": serde_json::to_value(&committed.step).unwrap(),
            "record": serde_json::to_value(&committed.record).unwrap(),
        })
    }

    #[test]
    fn golden_lifecycle_agent_root() {
        let mut runtime = Runtime::new();
        let configure_envelope = configure();
        let start_envelope = agent_start("in-start", 1_700_000_001_000);
        let genesis = runtime.submit(&configure_envelope);
        let started = runtime.submit(&start_envelope);

        let produced = json!({
            "description":
                "Configure → atomic agent root start (spec 6, 7.4). Two accepted inputs reach the \
                 first provider call: the genesis record freezes the resolved configuration, and \
                 the start record's step carries the immutable root kind, the initial execution \
                 focus and the one CallProvider effect the transition published.",
            "genesis_digest": genesis.record.record_digest().as_str(),
            "head_digest": started.record.record_digest().as_str(),
            "links": [
                link(&configure_envelope, &genesis),
                link(&start_envelope, &started),
            ],
        });

        let expected = golden("golden_lifecycle_agent_root.json", &produced);
        assert_eq!(produced, expected, "the agent root lifecycle drifted");
        assert_eq!(expected["links"][1]["step"]["root_kind"], json!("agent"));
        assert_eq!(
            expected["links"][1]["step"]["disposition"]["effects"][0]["effect"]["kind"],
            json!("call_provider"),
        );
    }

    #[test]
    fn golden_lifecycle_agent_full_turn() {
        let mut runtime = Runtime::new();
        let configure_envelope = syscall_config();
        let start_envelope = agent_start("in-start", 1_700_000_001_000);
        let genesis = runtime.submit(&configure_envelope);
        let started = runtime.submit(&start_envelope);

        let acted_envelope = provider_result(
            "in-acted",
            1_700_000_002_000,
            &effect_id(started.step_seq),
            vec![tool_call("call-1", "search", json!({"q": "sources"}))],
        );
        let acted = runtime.submit(&acted_envelope);
        let results_envelope = tools_resolved(
            "in-results",
            1_700_000_003_000,
            &effect_id(acted.step_seq),
            &[("call-1", "three sources found", false)],
        );
        let results = runtime.submit(&results_envelope);
        let answer_envelope = provider_answer(
            "in-answer",
            1_700_000_004_000,
            &effect_id(results.step_seq),
            "the brief cites three sources",
        );
        let answered = runtime.submit(&answer_envelope);

        let produced = json!({
            "description":
                "One whole agent turn cycle on the canonical wire (spec 7.9 · Task 12): configure → \
                 atomic agent start → provider result carrying a tool call → tool results → final \
                 provider answer → agent terminal. Every step after the start is a single \
                 `ResolveEffect` input, and each one publishes exactly the next effect the loop is \
                 waiting on — until the last, whose disposition is the terminal itself and which \
                 publishes nothing.",
            "genesis_digest": genesis.record.record_digest().as_str(),
            "head_digest": answered.record.record_digest().as_str(),
            "links": [
                link(&configure_envelope, &genesis),
                link(&start_envelope, &started),
                link(&acted_envelope, &acted),
                link(&results_envelope, &results),
                link(&answer_envelope, &answered),
            ],
        });

        let expected = golden("golden_lifecycle_agent_full_turn.json", &produced);
        assert_eq!(produced, expected, "the agent turn cycle drifted");
        assert_eq!(
            expected["links"][2]["step"]["disposition"]["effects"][0]["effect"]["kind"],
            json!("execute_tools"),
            "a provider result carrying a host tool call publishes exactly one tool batch",
        );
        assert_eq!(
            expected["links"][3]["step"]["disposition"]["effects"][0]["effect"]["kind"],
            json!("call_provider"),
            "the tool results resume the turn with the next provider call",
        );
        assert_eq!(
            expected["links"][4]["step"]["disposition"]["terminal"]["kind"],
            json!("agent"),
        );
        assert_eq!(
            expected["links"][4]["step"]["disposition"]["effects"],
            Value::Null,
            "§7.12 · effects or a terminal, never both",
        );
    }

    #[test]
    fn golden_lifecycle_workflow_root() {
        let mut runtime = Runtime::new();
        let configure_envelope = configure();
        let start_envelope = workflow_start("in-start", 1_700_000_001_000, two_node_spec());
        let genesis = runtime.submit(&configure_envelope);
        let started = runtime.submit(&start_envelope);

        let ack_envelope = spawned(
            "in-ack-1",
            1_700_000_002_000,
            &effect_id(started.step_seq),
            &["wf-node0"],
        );
        let ack = runtime.submit(&ack_envelope);
        let done_envelope = child_done("in-done-1", 1_700_000_003_000, "wf-node0", "collected");
        let advanced = runtime.submit(&done_envelope);
        let ack2_envelope = spawned(
            "in-ack-2",
            1_700_000_004_000,
            &effect_id(advanced.step_seq),
            &["wf-node1"],
        );
        let ack2 = runtime.submit(&ack2_envelope);
        let done2_envelope = child_done("in-done-2", 1_700_000_005_000, "wf-node1", "written");
        let finished = runtime.submit(&done2_envelope);

        let produced = json!({
            "description":
                "Configure → atomic workflow root start → spawn ack → child completions → workflow \
                 terminal (spec 10.1). No LoadWorkflow, no placeholder agent run, and no host \
                 CompleteRun: the last committed step's disposition is the terminal itself.",
            "genesis_digest": genesis.record.record_digest().as_str(),
            "head_digest": finished.record.record_digest().as_str(),
            "links": [
                link(&configure_envelope, &genesis),
                link(&start_envelope, &started),
                link(&ack_envelope, &ack),
                link(&done_envelope, &advanced),
                link(&ack2_envelope, &ack2),
                link(&done2_envelope, &finished),
            ],
        });

        let expected = golden("golden_lifecycle_workflow_root.json", &produced);
        assert_eq!(produced, expected, "the workflow root lifecycle drifted");
        assert_eq!(expected["links"][1]["step"]["root_kind"], json!("workflow"));
        assert_eq!(
            expected["links"][1]["step"]["disposition"]["effects"][0]["effect"]["kind"],
            json!("spawn_tasks"),
        );
        assert_eq!(
            expected["links"][5]["step"]["disposition"]["terminal"]["kind"],
            json!("workflow"),
        );
    }

    #[test]
    fn golden_lifecycle_cancel_arc() {
        let mut runtime = Runtime::new();
        let configure_envelope = signal_config(8, None);
        let start_envelope = workflow_start("in-start", 1_700_000_001_000, two_node_spec());
        let genesis = runtime.submit(&configure_envelope);
        let started = runtime.submit(&start_envelope);

        let ack_envelope = spawned(
            "in-ack",
            1_700_000_002_000,
            &effect_id(started.step_seq),
            &["wf-node0"],
        );
        let ack = runtime.submit(&ack_envelope);

        let signal_envelope = signal_delivery(
            "in-sig",
            1_700_000_003_000,
            "delivery-a",
            1,
            logical_signal("sig-abort", SignalUrgency::Critical),
        );
        let interrupted = runtime.submit(&signal_envelope);

        let preempted_envelope = resolved(
            "in-preempted",
            1_700_000_004_000,
            &effect_id(interrupted.step_seq),
            EffectSuccess::TasksPreempted(super::super::effect::TasksPreemptedSuccess {
                attempts: vec![super::super::effect::TaskPreemptOutcome {
                    task_id: TaskId::new("wf-node0").unwrap(),
                    attempt_id: WireAttemptId::new("wf-node0:attempt:1").unwrap(),
                    outcome: super::super::effect::TaskPreemptStatus::Preempted(
                        super::super::effect::TaskPreempted {},
                    ),
                }],
            }),
        );
        let preempted = runtime.submit(&preempted_envelope);

        let cancel_envelope = cancel("in-cancel", 1_700_000_005_000);
        let cancelled = runtime.submit(&cancel_envelope);

        let produced = json!({
            "description":
                "The cancellation arc on the canonical wire (spec 7.5 / 7.7 / 11.1 · Task 12b): \
                 configure → workflow root start → spawn ack → a critical signal that preempts the \
                 running child → the host's preempt resolution → `HostControl::Cancel`, whose step \
                 is the cancelled terminal itself. Two things this chain fixes in place: the only \
                 host action a signal can cause is the preemption (its queueing/dropping/expiry are \
                 audit facts and publish nothing), and cancellation settles every downstream wait \
                 inside the same transition that commits the root terminal — §7.12 admits effects or \
                 a terminal and never both, so there is no second round trip in which the operation \
                 is neither running nor cancelled.",
            "genesis_digest": genesis.record.record_digest().as_str(),
            "head_digest": cancelled.record.record_digest().as_str(),
            "links": [
                link(&configure_envelope, &genesis),
                link(&start_envelope, &started),
                link(&ack_envelope, &ack),
                link(&signal_envelope, &interrupted),
                link(&preempted_envelope, &preempted),
                link(&cancel_envelope, &cancelled),
            ],
        });

        let expected = golden("golden_lifecycle_cancel_arc.json", &produced);
        assert_eq!(produced, expected, "the cancellation arc drifted");
        assert_eq!(
            expected["links"][3]["step"]["disposition"]["effects"][0]["effect"]["kind"],
            json!("preempt_tasks"),
            "an urgent signal's one host action is stopping the running child",
        );
        assert_eq!(
            expected["links"][5]["step"]["disposition"]["terminal"]["kind"],
            json!("cancelled"),
        );
        assert_eq!(
            expected["links"][5]["step"]["disposition"]["effects"],
            Value::Null,
            "§11.1 · the cancelling step leaves nothing waiting on the host",
        );
        assert_eq!(
            runtime.pending_effect_kinds(),
            Vec::<EffectKindTag>::new(),
            "and the transaction holds no pending effect after it",
        );
    }

    #[test]
    fn golden_lifecycle_external_payload() {
        let mut runtime = Runtime::new();
        let configure_envelope = payload_config();
        let start_envelope = agent_start("in-start", 1_700_000_001_000);
        let genesis = runtime.submit(&configure_envelope);
        let started = runtime.submit(&start_envelope);

        let acted_envelope = provider_result(
            "in-acted",
            1_700_000_002_000,
            &effect_id(started.step_seq),
            vec![tool_call("call-1", "search", json!({"q": "sources"}))],
        );
        let acted = runtime.submit(&acted_envelope);
        let results_envelope = payloads_resolved(
            "in-results",
            1_700_000_003_000,
            &effect_id(acted.step_seq),
            vec![external_payload(
                "call-1",
                body_digest(),
                BODY.len() as u64,
                "the full report body, far la…",
            )],
        );
        let results = runtime.submit(&results_envelope);
        let read_envelope = provider_result(
            "in-read",
            1_700_000_004_000,
            &effect_id(results.step_seq),
            vec![tool_call(
                "call-2",
                READ_RESULT_TOOL_NAME,
                json!({"call_id": "call-1"}),
            )],
        );
        let read = runtime.submit(&read_envelope);
        let loaded_envelope = resolved(
            "in-loaded",
            1_700_000_005_000,
            &effect_id(read.step_seq),
            EffectSuccess::PayloadLoaded(PayloadLoadedSuccess {
                handle_id: HandleId::new("call-1").unwrap(),
                payload: InlinePayload {
                    content: BODY.to_string(),
                    digest: body_digest(),
                    original_size: WireU64::new(BODY.len() as u64),
                },
            }),
        );
        let loaded = runtime.submit(&loaded_envelope);

        let produced = json!({
            "description":
                "The §7.10 external-payload arc (Task 13): configure → agent start → a provider \
                 turn calling one host tool → an **external** tool result → the model's \
                 `read_result` page-in → the loaded body. Read the records: the persisted body \
                 appears in exactly one place on this whole chain — the `payload_loaded` outcome \
                 the host sent when the kernel asked for it. It is in no effect the kernel \
                 published and in no accepted input the kernel did not ask for, which is what \
                 §25.10 means by \"large bodies do not enter the journal\". What the kernel holds \
                 instead is the reference: an opaque `payload_ref` it never interprets, a digest \
                 it checks the restored body against, and a bounded preview that is the only part \
                 to occupy context.",
            "genesis_digest": genesis.record.record_digest().as_str(),
            "head_digest": loaded.record.record_digest().as_str(),
            "links": [
                link(&configure_envelope, &genesis),
                link(&start_envelope, &started),
                link(&acted_envelope, &acted),
                link(&results_envelope, &results),
                link(&read_envelope, &read),
                link(&loaded_envelope, &loaded),
            ],
        });

        let expected = golden("golden_lifecycle_external_payload.json", &produced);
        assert_eq!(produced, expected, "the external-payload arc drifted");
        assert_eq!(
            expected["links"][3]["step"]["disposition"]["effects"][0]["effect"]["kind"],
            json!("call_provider"),
            "an external result resumes the turn like any other — the body never comes back out",
        );
        assert_eq!(
            expected["links"][4]["step"]["disposition"]["effects"][0]["effect"]["kind"],
            json!("load_payload"),
            "§7.10 rule 4 · `read_result` reduces to exactly one effect",
        );
        assert_eq!(
            expected["links"][4]["step"]["disposition"]["effects"][0]["effect"]["payload_ref"],
            json!("payload:01J8Y2QK7C4N0V"),
            "the effect hands back the host's own opaque locator, unread and unjoined",
        );

        // The body appears exactly once on the whole chain, and only where the model asked for it.
        //
        // SPEC-ISSUE: §25.10 states flatly that large bodies do not enter the journal, but §7.9's
        // `PayloadLoaded` carries `InlinePayload.content` as an accepted input, and §8.1 makes the
        // accepted input part of the durable record. So a *paged-in* body is journalled by the
        // contract's own construction. The invariant that is actually achievable — and the one this
        // golden pins — is narrower: no body enters on the **ingestion** path, and no body is ever
        // carried by an effect the kernel publishes. Either §25.10 should be scoped to those two,
        // or `PayloadLoaded` needs a shape that resolves an effect without riding the record.
        for (index, link) in expected["links"].as_array().unwrap().iter().enumerate() {
            assert_eq!(
                link["envelope"]
                    .to_string()
                    .contains("clears the inline threshold"),
                index == 5,
                "link {index} disagrees about where a large body may enter",
            );
            // A `call_provider` effect legitimately carries whatever is in context once the model
            // has paged a body in. No effect may hand the body back out to be persisted.
            for effect in link["step"]["disposition"]["effects"]
                .as_array()
                .unwrap_or(&Vec::new())
            {
                assert!(
                    !effect.to_string().contains("clears the inline threshold")
                        || effect["effect"]["kind"] == json!("call_provider"),
                    "link {index} hands the body back out in a {} effect",
                    effect["effect"]["kind"],
                );
            }
        }
        for record in &runtime.journal {
            let input = serde_json::to_string(&record.normalized_input().unwrap()).unwrap();
            assert_eq!(
                input.contains("clears the inline threshold"),
                record.step_seq().get() == 5,
                "only the page-in the model asked for may make a body durable, got step {}",
                record.step_seq()
            );
        }
    }

    // -----------------------------------------------------------------------------------------
    // §12 · logical checkpoint goldens
    // -----------------------------------------------------------------------------------------

    /// Drive one whole agent turn and stop mid-flight, so the checkpoint has something to hold:
    /// a live pending effect, a replay ledger, a task table and a live policy.
    fn agent_mid_turn() -> Runtime {
        let mut runtime = Runtime::new();
        runtime.submit(&syscall_config());
        let started = runtime.submit(&agent_start("in-start", 1_700_000_001_000));
        let acted = runtime.submit(&provider_result(
            "in-acted",
            1_700_000_002_000,
            &effect_id(started.step_seq),
            vec![tool_call("call-1", "search", json!({"q": "sources"}))],
        ));
        runtime.submit(&tools_resolved(
            "in-results",
            1_700_000_003_000,
            &effect_id(acted.step_seq),
            &[("call-1", "three sources found", false)],
        ));
        runtime
    }

    #[test]
    fn golden_checkpoint_agent_turn() {
        let runtime = agent_mid_turn();
        let candidate = runtime.checkpoint();
        let checkpoint = candidate.decode().expect("the candidate blob verifies");

        let produced = json!({
            "description":
                "A full-state logical checkpoint taken mid-turn (spec 12.1, 12.3). base == through \
                 == the durable head, so the bounded tail is empty and a restore replays nothing \
                 before the post-checkpoint records. The logical state is partitioned four ways and \
                 the header repeats none of it: the pending `call_provider` effect, the replay \
                 ledger and the terminal slot live in `transition`, the task table in `scheduler`, \
                 the handle table in `context_vm`, the live policy and the provider-tool causation \
                 in `syscall`.",
            "through_step_seq": candidate.through_step_seq.to_string(),
            "covered_head": candidate.covered_head.as_str(),
            "state_digest": candidate.state_digest.as_str(),
            "ack_token": candidate.ack_token.as_str(),
            "checkpoint": serde_json::to_value(&checkpoint).unwrap(),
        });

        let expected = golden("golden_checkpoint_agent_turn.json", &produced);
        assert_eq!(produced, expected, "the logical checkpoint drifted");
        assert_eq!(expected["checkpoint"]["checkpoint_version"], json!(1));
        assert_eq!(
            expected["checkpoint"]["abi_version"],
            json!(super::super::KERNEL_ABI_VERSION),
        );
        assert_eq!(
            expected["checkpoint"]["base_step_seq"], expected["checkpoint"]["through_step_seq"],
            "a full-state candidate carries no tail",
        );
        assert_eq!(expected["checkpoint"]["tail_inputs"], json!([]));
        assert_eq!(
            expected["checkpoint"]["logical_state"]["transition"]["pending_effects"][0]["effect"]["kind"],
            json!("call_provider"),
            "the effect the operation is waiting on is inside the checkpoint, not beside it",
        );
    }

    /// The incremental form of §12.1: an older logical state plus the canonical inputs that carry
    /// it forward to the covered head. This is exactly the shape a Task 16 rebase produces, and
    /// pinning it now is what stops the tail contract from being invented twice.
    #[test]
    fn golden_checkpoint_bounded_tail() {
        let mut runtime = Runtime::new();
        runtime.submit(&syscall_config());
        let started = runtime.submit(&agent_start("in-start", 1_700_000_001_000));

        // the state at the root start — the base a later rebase keeps
        let base = runtime
            .checkpoint()
            .decode()
            .expect("the base candidate verifies");

        let acted = runtime.submit(&provider_result(
            "in-acted",
            1_700_000_002_000,
            &effect_id(started.step_seq),
            vec![tool_call("call-1", "search", json!({"q": "sources"}))],
        ));
        let results = runtime.submit(&tools_resolved(
            "in-results",
            1_700_000_003_000,
            &effect_id(acted.step_seq),
            &[("call-1", "three sources found", false)],
        ));
        runtime.submit(&provider_answer(
            "in-answer",
            1_700_000_004_000,
            &effect_id(results.step_seq),
            "the brief cites three sources",
        ));

        let tail: Vec<CanonicalInput> = runtime.journal[2..]
            .iter()
            .map(|record| CanonicalInput::from_record(record).expect("a record projects"))
            .collect();
        // The transaction is the authority on what the tail still holds; harvesting from the
        // journal and asking the transaction must give the same answer, or a rebase built from
        // either source would produce a different checkpoint.
        assert_eq!(
            runtime
                .tx
                .tail_inputs()
                .into_iter()
                .filter(|entry| entry.step_seq.get() > base.through_step_seq().get())
                .collect::<Vec<_>>(),
            tail,
            "the transaction's own tail and the journal agree on (base, through]",
        );
        let rebased = runtime
            .tx
            .checkpoint_rebase(
                &CheckpointBoundary {
                    through_step_seq: base.through_step_seq(),
                    covered_head: base.covered_transaction_head_digest().clone(),
                },
                base.logical_state().clone(),
            )
            .expect("an older state plus its exact tail is a checkpoint")
            .decode()
            .expect("the rebase blob verifies");

        let produced = json!({
            "description":
                "The incremental form of a logical checkpoint (spec 12.1, 12.2): `logical_state` is \
                 the state after `base_step_seq`, and `tail_inputs` covers (base, through] exactly \
                 — no hole, no duplicate, nothing outside the range. A restore replays this tail on \
                 top of the state, then continues with the journal records after `through_step_seq`. \
                 The state digest is byte-identical to the base checkpoint's, because the state is \
                 the same state; only the tail and the header moved.",
            "base_state_digest": base.state_digest().as_str(),
            "checkpoint": serde_json::to_value(&rebased).unwrap(),
        });

        let expected = golden("golden_checkpoint_bounded_tail.json", &produced);
        assert_eq!(produced, expected, "the bounded-tail checkpoint drifted");
        assert_eq!(
            expected["checkpoint"]["state_digest"], expected["base_state_digest"],
            "a rebase carries the base state forward untouched",
        );
        assert_eq!(
            expected["checkpoint"]["base_step_seq"],
            json!("1"),
            "the base is the root start",
        );
        assert_eq!(expected["checkpoint"]["through_step_seq"], json!("4"));
        let tail = expected["checkpoint"]["tail_inputs"].as_array().unwrap();
        assert_eq!(
            tail.iter()
                .map(|entry| entry["step_seq"].as_str().unwrap())
                .collect::<Vec<_>>(),
            vec!["2", "3", "4"],
            "(1, 4] is exactly steps 2, 3 and 4",
        );
    }

    /// §12.3 rule 1 · a candidate is a read. Appends continue, and the candidate that was handed
    /// out still describes the prefix it was taken over.
    #[test]
    fn a_candidate_neither_blocks_nor_is_invalidated_by_later_appends() {
        let mut runtime = agent_mid_turn();
        let candidate = runtime.checkpoint();
        let before = runtime.tx.head().expect("a head");
        assert_eq!(candidate.through_step_seq, before.step_seq);

        let results_effect = effect_id(before.step_seq);
        runtime.submit(&provider_answer(
            "in-answer",
            1_700_000_004_000,
            &results_effect,
            "the brief cites three sources",
        ));

        let after = runtime.tx.head().expect("a head");
        assert_ne!(after.step_seq, before.step_seq, "the journal moved on");
        assert_eq!(
            candidate.through_step_seq, before.step_seq,
            "the candidate still covers the prefix it was taken over",
        );
        candidate
            .decode()
            .expect("and it still verifies after the journal moved")
            .verify_belongs_to(&operation(), runtime.journal[0].record_digest())
            .expect("it is still this operation's checkpoint");
    }

    // -----------------------------------------------------------------------------------------
    // §12.2 · bounded-tail restore
    // -----------------------------------------------------------------------------------------

    /// Drive an operation to a fixed point through a fixed envelope sequence.
    ///
    /// One function so the two sides of a differential are *the same* sequence by construction
    /// rather than by two copies that agree today. `stop_after` is how many envelopes to submit.
    fn drive(runtime: &mut Runtime, envelopes: &[WireEnvelope]) {
        for envelope in envelopes {
            runtime.submit(envelope);
        }
    }

    /// The canonical agent turn, as a list of envelopes.
    ///
    /// Deterministic in every field a record digests: ids, clocks and payloads are literals, and
    /// the effect ids are derived from the step sequence exactly as the kernel derives them. That is
    /// what makes "byte-identical" a checkable claim rather than a hope.
    fn turn_envelopes() -> Vec<WireEnvelope> {
        vec![
            syscall_config(),
            agent_start("in-start", 1_700_000_001_000),
            provider_result(
                "in-acted",
                1_700_000_002_000,
                &effect_id(WireU64::new(1)),
                vec![tool_call("call-1", "search", json!({"q": "sources"}))],
            ),
            tools_resolved(
                "in-results",
                1_700_000_003_000,
                &effect_id(WireU64::new(2)),
                &[("call-1", "three sources found", false)],
            ),
            provider_result(
                "in-acted-2",
                1_700_000_004_000,
                &effect_id(WireU64::new(3)),
                vec![tool_call("call-2", "search", json!({"q": "more"}))],
            ),
            tools_resolved(
                "in-results-2",
                1_700_000_005_000,
                &effect_id(WireU64::new(4)),
                &[("call-2", "two more sources", false)],
            ),
            provider_answer(
                "in-answer",
                1_700_000_006_000,
                &effect_id(WireU64::new(5)),
                "the brief cites five sources",
            ),
        ]
    }

    fn digests(records: &[KernelRecord]) -> Vec<String> {
        records
            .iter()
            .map(|record| record.record_digest().to_string())
            .collect()
    }

    /// The whole observable surface of a runtime, as bytes.
    ///
    /// Everything a differential must compare and nothing that is allowed to differ: the state
    /// digest a checkpoint would take, the pending effect identities, the terminal, and the head.
    /// If a restored runtime matches an uninterrupted one on all four *and* keeps producing the same
    /// records, "behaves identically" is not a judgement call.
    fn surface(runtime: &Runtime) -> Value {
        json!({
            "head": runtime.tx.head().map(|head| json!({
                "digest": head.digest.as_str(),
                "step_seq": head.step_seq.to_string(),
            })),
            "lifecycle": format!("{:?}", runtime.tx.lifecycle()),
            "pending_effects": runtime
                .tx
                .pending_effects()
                .map(|effect| serde_json::to_value(effect).unwrap())
                .collect::<Vec<_>>(),
            "terminal": runtime.tx.terminal().map(|t| serde_json::to_value(t).unwrap()),
            "logical_state": serde_json::to_value(
                runtime
                    .tx
                    .transition_state_for_restore(
                        runtime.driver.root_kind(),
                        runtime.driver.focus().cloned(),
                    )
                    .expect("a configured runtime has a transition state"),
            )
            .unwrap(),
            "context_vm": serde_json::to_value(
                runtime.driver.project_logical_state().context_vm
            ).unwrap(),
            "scheduler": serde_json::to_value(
                runtime.driver.project_logical_state().scheduler
            ).unwrap(),
        })
    }

    /// Task 16b · a checkpoint owns the active DAG and every child process identity needed to
    /// continue it. Restoring while the first node is live must neither forget the second node nor
    /// rebuild the child under different role/isolation/inheritance.
    #[test]
    fn an_active_workflow_and_its_child_restore_to_the_same_completion() {
        let (mut uninterrupted, _) = workflow_with_live_child();
        let checkpoint = uninterrupted.checkpoint().decode().expect("verifies");
        let mut restored = Runtime::restore_with(Some(&checkpoint), &[]);

        let first_done = child_done(
            "in-done-1",
            1_700_000_003_000,
            "wf-node0",
            "sources collected",
        );
        let uninterrupted_next = uninterrupted.submit(&first_done);
        let restored_next = restored.submit(&first_done);
        assert_eq!(
            restored_next, uninterrupted_next,
            "the restored DAG schedules the same second node",
        );

        let uninterrupted_spawn = effect_id(uninterrupted_next.step_seq);
        let restored_spawn = effect_id(restored_next.step_seq);
        uninterrupted.submit(&spawned(
            "in-ack-2",
            1_700_000_004_000,
            &uninterrupted_spawn,
            &["wf-node1"],
        ));
        restored.submit(&spawned(
            "in-ack-2",
            1_700_000_004_000,
            &restored_spawn,
            &["wf-node1"],
        ));
        let second_done = child_done("in-done-2", 1_700_000_005_000, "wf-node1", "brief written");
        uninterrupted.submit(&second_done);
        restored.submit(&second_done);

        assert_eq!(
            surface(&restored),
            surface(&uninterrupted),
            "workflow state and child permission identity are reversible",
        );
    }

    /// Task 16b · queued signal source state and the router's business-dedupe memory survive a
    /// checkpoint. The queued payload must drive the same follow-up provider request, while a new
    /// delivery with the same key remains ignored on both sides.
    #[test]
    fn a_queued_signal_and_its_dedupe_key_restore_to_the_same_follow_up() {
        let mut uninterrupted = Runtime::new();
        uninterrupted.submit(&signal_config(8, None));
        let started = uninterrupted.submit(&agent_start("in-start", 1_700_000_001_000));
        uninterrupted.submit(&signal_delivery(
            "in-sig-1",
            1_700_000_002_000,
            "delivery-a",
            1,
            LogicalSignal {
                payload: super::super::scalar::BoundedJson::new(json!({
                    "job": "nightly-index"
                }))
                .unwrap(),
                dedupe_key: Some("nightly-index".to_string()),
                ..logical_signal("sig-nightly", SignalUrgency::Normal)
            },
        ));

        let checkpoint = uninterrupted.checkpoint().decode().expect("verifies");
        let mut restored = Runtime::restore_with(Some(&checkpoint), &[]);

        let duplicate = signal_delivery(
            "in-sig-2",
            1_700_000_003_000,
            "delivery-b",
            2,
            LogicalSignal {
                payload: super::super::scalar::BoundedJson::new(json!({
                    "job": "nightly-index"
                }))
                .unwrap(),
                dedupe_key: Some("nightly-index".to_string()),
                ..logical_signal("sig-nightly", SignalUrgency::Normal)
            },
        );
        assert_eq!(
            restored.submit(&duplicate),
            uninterrupted.submit(&duplicate),
            "the restored router remembers the business dedupe key",
        );

        let provider = effect_id(started.step_seq);
        let answer = provider_answer(
            "in-answer",
            1_700_000_004_000,
            &provider,
            "the first request completed",
        );
        assert_eq!(
            restored.submit(&answer),
            uninterrupted.submit(&answer),
            "the queued payload produces the same follow-up provider request",
        );
        assert_eq!(surface(&restored), surface(&uninterrupted));
    }

    /// Task 16b · a completed child remains the same process fact after restore: its role,
    /// isolation, context inheritance, capability ceiling and join result are not inferred anew.
    #[test]
    fn a_subagent_process_and_join_result_restore_without_permission_drift() {
        let mut spec = two_node_spec();
        spec.nodes[0].run_spec = Some(LogicalAgentSpec {
            role: Some(WireRole::Verify),
            isolation: Some(WireIsolation::ReadOnly),
            ..LogicalAgentSpec::new("verify the collected sources")
        });

        let mut uninterrupted = Runtime::new();
        uninterrupted.submit(&syscall_config());
        let started = uninterrupted.submit(&workflow_start("in-start", 1_700_000_001_000, spec));
        uninterrupted.submit(&spawned(
            "in-ack-1",
            1_700_000_002_000,
            &effect_id(started.step_seq),
            &["wf-node0"],
        ));
        let completed = uninterrupted.submit(&child_done(
            "in-done-1",
            1_700_000_003_000,
            "wf-node0",
            "sources verified",
        ));

        let checkpoint = uninterrupted.checkpoint().decode().expect("verifies");
        let mut restored = Runtime::restore_with(Some(&checkpoint), &[]);
        let original_task = uninterrupted
            .driver
            .project_logical_state()
            .scheduler
            .tasks
            .into_iter()
            .find(|task| task.task_id.as_str() == "wf-node0")
            .expect("the completed child remains projected");
        let restored_task = restored
            .driver
            .project_logical_state()
            .scheduler
            .tasks
            .into_iter()
            .find(|task| task.task_id.as_str() == "wf-node0")
            .expect("the restored child remains projected");
        assert_eq!(restored_task, original_task);
        let process = restored_task.process.expect("it is still a child process");
        assert_eq!(process.role, "verify");
        assert_eq!(process.isolation, "read_only");
        assert_eq!(process.context_inheritance, "none");
        assert!(
            process.join_result.is_some(),
            "the join result is source state"
        );

        let next_spawn = effect_id(completed.step_seq);
        let ack = spawned("in-ack-2", 1_700_000_004_000, &next_spawn, &["wf-node1"]);
        assert_eq!(
            restored.submit(&ack),
            uninterrupted.submit(&ack),
            "the restored process table authorizes the same next transition",
        );
        assert_eq!(surface(&restored), surface(&uninterrupted));
    }

    #[test]
    fn a_pending_subagent_preemption_restores_from_the_transition_effect() {
        let (mut uninterrupted, _) = workflow_with_live_child();
        let requested = uninterrupted.submit(&signal_delivery(
            "in-sig-critical",
            1_700_000_003_000,
            "delivery-critical",
            1,
            logical_signal("sig-critical", SignalUrgency::Critical),
        ));
        let checkpoint = uninterrupted.checkpoint().decode().expect("verifies");
        let mut restored = Runtime::restore_with(Some(&checkpoint), &[]);

        let resolved = resolved(
            "in-preempted",
            1_700_000_004_000,
            &effect_id(requested.step_seq),
            EffectSuccess::TasksPreempted(super::super::effect::TasksPreemptedSuccess {
                attempts: vec![super::super::effect::TaskPreemptOutcome {
                    task_id: TaskId::new("wf-node0").unwrap(),
                    attempt_id: WireAttemptId::new("wf-node0:attempt:1").unwrap(),
                    outcome: super::super::effect::TaskPreemptStatus::Preempted(
                        super::super::effect::TaskPreempted {},
                    ),
                }],
            }),
        );
        assert_eq!(
            restored.submit(&resolved),
            uninterrupted.submit(&resolved),
            "the transition-owned preempt intent remains resolvable after restore",
        );
        assert_eq!(surface(&restored), surface(&uninterrupted));
    }

    /// Task 16b · a `PagedOut` handle restored from a checkpoint projects the archived tool body
    /// exactly as the uninterrupted renderer does on the next provider call.
    #[test]
    fn a_paged_out_result_restores_to_the_same_rendered_provider_context() {
        let mut uninterrupted = Runtime::new();
        uninterrupted.submit(&syscall_config());
        let started = uninterrupted.submit(&agent_start("in-start", 1_700_000_001_000));
        let archived_body = "ARCHIVED TOOL OUTPUT ".repeat(300);
        {
            let engine = uninterrupted
                .driver
                .engine
                .as_mut()
                .expect("the configured agent has an engine");
            let mut assistant = Message::assistant("I checked the archive.");
            assistant.tool_calls = vec![crate::types::message::ToolCall {
                id: "call-archived".into(),
                name: "search".into(),
                arguments: json!({"q": "archived evidence"}),
            }];
            engine.ctx.push_history(assistant, 8);
            engine.ctx.push_history(
                Message::tool(vec![ContentPart::ToolResult {
                    call_id: "call-archived".into(),
                    output: archived_body,
                    is_error: false,
                }]),
                1_200,
            );
            let handle_id = engine
                .ctx
                .handles
                .all()
                .iter()
                .find(|handle| handle.source.as_deref() == Some("call-archived"))
                .expect("the tool result is addressable")
                .id;
            engine
                .ctx
                .handles
                .get_mut(handle_id)
                .expect("the handle remains live")
                .residency = Residency::PagedOut {
                payload_ref: "payload:checkpoint-archive".to_string(),
                digest: format!("sha256:{}", "1".repeat(64)),
            };
        }
        let checkpoint = uninterrupted.checkpoint().decode().expect("verifies");
        let mut restored = Runtime::restore_with(Some(&checkpoint), &[]);
        assert_eq!(
            serde_json::to_value(
                &restored
                    .driver
                    .engine()
                    .expect("restored engine")
                    .ctx
                    .render()
                    .turns
            )
            .unwrap(),
            serde_json::to_value(
                &uninterrupted
                    .driver
                    .engine()
                    .expect("uninterrupted engine")
                    .ctx
                    .render()
                    .turns
            )
            .unwrap(),
            "the restored PagedOut preview itself matches before either run advances",
        );

        let acted = provider_result(
            "in-acted",
            1_700_000_002_000,
            &effect_id(started.step_seq),
            vec![tool_call(
                "call-after-page-out",
                "search",
                json!({
                    "q": "fresh evidence"
                }),
            )],
        );
        let uninterrupted_tools = uninterrupted.submit(&acted);
        let restored_tools = restored.submit(&acted);
        assert_eq!(restored_tools, uninterrupted_tools);

        let results = tools_resolved(
            "in-results",
            1_700_000_003_000,
            &effect_id(uninterrupted_tools.step_seq),
            &[("call-after-page-out", "fresh evidence found", false)],
        );
        let uninterrupted_provider = uninterrupted.submit(&results);
        let restored_provider = restored.submit(&results);
        assert_eq!(
            restored_provider, uninterrupted_provider,
            "the post-restore renderer emits the same provider context with a PagedOut preview",
        );
        assert_eq!(surface(&restored), surface(&uninterrupted));
    }

    /// **Verification 1 (full-state form)** · an uninterrupted run and a restored one are
    /// byte-identical.
    ///
    /// Both sides submit the *same* envelope list. The second takes a full-state checkpoint
    /// half-way, throws the runtime away, restores from the blob plus the records above it, and
    /// finishes. Every record digest on both journals must match, and so must the whole observable
    /// surface — including the digest of the logical state, which is the strongest statement
    /// available: the two runtimes would produce the same checkpoint.
    #[test]
    fn an_uninterrupted_run_and_a_full_state_restore_are_byte_identical() {
        let envelopes = turn_envelopes();

        let mut uninterrupted = Runtime::new();
        drive(&mut uninterrupted, &envelopes);

        let mut interrupted = Runtime::new();
        drive(&mut interrupted, &envelopes[..4]);
        let candidate = interrupted.checkpoint();
        let checkpoint = candidate.decode().expect("the candidate blob verifies");
        assert_eq!(
            checkpoint.base_step_seq(),
            checkpoint.through_step_seq(),
            "this half of the differential exercises the full-state form",
        );

        // The crash: everything in memory is gone, and all the host still holds is the blob and the
        // journal. Records at or below `through_step_seq` are deliberately *not* handed back.
        let mut restored = interrupted.restore(&checkpoint);
        assert_eq!(
            restored.restore_cost.unwrap().records_before_checkpoint,
            0,
            "a restore with a checkpoint reads nothing below it",
        );
        assert_eq!(
            surface(&restored),
            surface(&interrupted),
            "the restored runtime is the runtime that crashed",
        );

        drive(&mut restored, &envelopes[4..]);
        assert_eq!(
            digests(&restored.journal),
            digests(&uninterrupted.journal[4..]),
            "every post-restore record is byte-identical to the uninterrupted one",
        );
        assert_eq!(
            surface(&restored),
            surface(&uninterrupted),
            "and so is the state they end in",
        );
    }

    /// **Verification 1 (rebase form)** · the same differential over a checkpoint whose logical
    /// state sits *below* its covered head.
    ///
    /// The restore therefore has real work to do before it touches the journal: it replays the
    /// bounded tail, verifies each replayed record against the digest the checkpoint recorded, and
    /// only then continues.
    #[test]
    fn an_uninterrupted_run_and_a_rebased_restore_are_byte_identical() {
        let envelopes = turn_envelopes();

        let mut uninterrupted = Runtime::new();
        drive(&mut uninterrupted, &envelopes);

        let mut interrupted = Runtime::new();
        drive(&mut interrupted, &envelopes[..2]);
        let base = interrupted
            .checkpoint()
            .decode()
            .expect("the base candidate verifies");
        drive(&mut interrupted, &envelopes[2..4]);

        let rebased = interrupted
            .tx
            .checkpoint_rebase(
                &CheckpointBoundary {
                    through_step_seq: base.through_step_seq(),
                    covered_head: base.covered_transaction_head_digest().clone(),
                },
                base.logical_state().clone(),
            )
            .expect("a rebase over (1, 3] is assemblable")
            .decode()
            .expect("the rebase blob verifies");
        assert!(
            rebased.base_step_seq() < rebased.through_step_seq(),
            "this half of the differential exercises the rebase form",
        );
        assert_eq!(rebased.tail_inputs().len(), 2);

        let mut restored = interrupted.restore(&rebased);
        let cost = restored.restore_cost.unwrap();
        assert_eq!(cost.records_before_checkpoint, 0);
        assert_eq!(
            cost.tail_inputs_replayed, 2,
            "the tail was actually replayed"
        );
        assert_eq!(
            surface(&restored),
            surface(&interrupted),
            "replaying the bounded tail lands on the state the run was in",
        );

        // The rebase form is what Task 16 adds to the checkpoint contract, so its five candidate
        // values and the state a restore lands on are frozen together.
        let produced = json!({
            "description":
                "Spec 12.2 / 12.3 rule 11 · the rebase form end to end. `logical_state` is the \
                 state after `base_step_seq`, `tail_inputs` covers (base, through] exactly, and \
                 `base_record_digest` is the chain anchor the tail replays from — the record an \
                 acked checkpoint is allowed to have reclaimed. Restoring the blob replays that \
                 tail, verifies each replayed record against the digest the checkpoint carries, and \
                 lands on the head the run was at.",
            "base_step_seq": rebased.base_step_seq().to_string(),
            "base_record_digest": rebased.base_record_digest().as_str(),
            "through_step_seq": rebased.through_step_seq().to_string(),
            "covered_head": rebased.covered_transaction_head_digest().as_str(),
            "state_digest": rebased.state_digest().as_str(),
            "tail_steps": rebased
                .tail_inputs()
                .iter()
                .map(|entry| entry.step_seq.to_string())
                .collect::<Vec<_>>(),
            "restored_head": restored.tx.head().unwrap().digest.as_str(),
            "restore_cost": {
                "records_before_checkpoint": cost.records_before_checkpoint,
                "tail_inputs_replayed": cost.tail_inputs_replayed,
                "records_after_checkpoint": cost.records_after_checkpoint,
            },
        });
        let expected = golden("golden_checkpoint_rebase_restore.json", &produced);
        assert_eq!(produced, expected, "the rebase restore drifted");
        assert_eq!(expected["base_step_seq"], json!("1"));
        assert_eq!(expected["through_step_seq"], json!("3"));
        assert_eq!(
            expected["restore_cost"]["records_before_checkpoint"],
            json!(0),
            "the whole point: nothing below the base is read",
        );

        drive(&mut restored, &envelopes[4..]);
        assert_eq!(
            digests(&restored.journal),
            digests(&uninterrupted.journal[4..]),
        );
        assert_eq!(surface(&restored), surface(&uninterrupted));
    }

    /// §7.10 · a body that lives with the host travels as a reference, and comes back as one.
    ///
    /// The half of §5q-2 that keeps the checkpoint from re-inlining what §7.10 spent an effect kind
    /// keeping out: the message carries the preview, the handle carries the locator and the digest,
    /// and the restore rebuilds exactly that pairing.
    #[test]
    fn an_external_body_is_checkpointed_by_reference_and_restored_as_one() {
        let (mut runtime, effect) = agent_awaiting_tool_results();
        let body_digest =
            super::super::record::canonical_digest(b"a body far over the inline threshold");
        runtime.submit(&payloads_resolved(
            "in-results",
            1_700_000_003_000,
            &effect,
            vec![external_payload(
                "call-1",
                body_digest.clone(),
                64 * 1024,
                "the first 2 KiB of it",
            )],
        ));

        let context_vm = runtime.driver.project_logical_state().context_vm;
        let referenced: Vec<&StoredMessageState> = context_vm
            .messages
            .iter()
            .filter(|message| matches!(message.body, StoredMessageBody::Reference(_)))
            .collect();
        assert_eq!(referenced.len(), 1, "exactly the external result");
        let StoredMessageBody::Reference(reference) = &referenced[0].body else {
            unreachable!()
        };
        assert_eq!(reference.digest, body_digest.as_str());
        assert_eq!(reference.tool_call_id.as_deref(), Some("call-1"));
        assert_eq!(reference.preview, "the first 2 KiB of it");
        assert!(
            !serde_json::to_string(&context_vm)
                .unwrap()
                .contains("a body far over the inline threshold"),
            "the body itself is nowhere in the checkpoint",
        );

        let checkpoint = runtime.checkpoint().decode().expect("verifies");
        let restored = Runtime::restore_with(Some(&checkpoint), &[]);
        assert_eq!(surface(&restored), surface(&runtime));
        assert_eq!(
            restored
                .driver
                .project_logical_state()
                .context_vm
                .handles
                .iter()
                .filter_map(|handle| handle.digest.clone())
                .collect::<Vec<_>>(),
            vec![body_digest.to_string()],
            "the handle that addresses the body is restored with its verification digest",
        );
    }

    /// §12.3 rule 11 · the two candidate forms agree on `state_digest` for the same logical state.
    ///
    /// This is what makes them interchangeable rather than merely both legal: a host that switches
    /// from full-state to rebase does not change what its checkpoints *mean*, only how much they
    /// re-serialise.
    #[test]
    fn a_rebase_and_a_full_state_candidate_agree_on_the_state_digest() {
        let envelopes = turn_envelopes();
        let mut runtime = Runtime::new();
        drive(&mut runtime, &envelopes[..2]);
        let base = runtime
            .checkpoint()
            .decode()
            .expect("the base candidate verifies");
        drive(&mut runtime, &envelopes[2..4]);

        let full_state = runtime.checkpoint();
        let rebase = runtime
            .tx
            .checkpoint_rebase(
                &CheckpointBoundary {
                    through_step_seq: base.through_step_seq(),
                    covered_head: base.covered_transaction_head_digest().clone(),
                },
                base.logical_state().clone(),
            )
            .expect("a rebase is assemblable");

        assert_eq!(
            full_state.through_step_seq, rebase.through_step_seq,
            "both cover the same prefix",
        );
        assert_eq!(full_state.covered_head, rebase.covered_head);
        assert_ne!(
            full_state.state_digest, rebase.state_digest,
            "they carry *different* logical states — one at the head, one at the base",
        );
        assert_eq!(
            rebase.state_digest,
            *base.state_digest(),
            "and a rebase carries its base's state forward untouched, byte for byte",
        );

        // Restoring either one lands on the same place, which is the property the digests are
        // evidence *for*.
        let from_full = runtime.restore(&full_state.decode().unwrap());
        let from_rebase = runtime.restore(&rebase.decode().unwrap());
        assert_eq!(surface(&from_full), surface(&from_rebase));
        assert_eq!(surface(&from_full), surface(&runtime));
    }

    /// **Verification 2** · restore cost is bounded by the tail, not by how long the run is.
    ///
    /// Deterministic counters, not a timer: the claim is about how much history is read, and that is
    /// a number the restore can report exactly. The run is driven to three different lengths and the
    /// cost is asserted *equal* across all three — not merely "small".
    #[test]
    fn long_run_restore_cost_is_bounded_by_the_tail_not_the_run() {
        fn cost_after(turns: usize) -> RestoreCost {
            let mut runtime = Runtime::new();
            runtime.submit(&syscall_config_with(|config| {
                config.execution_policy = Some(ExecutionPolicy {
                    max_turns: Some(10_000),
                    ..ExecutionPolicy::default()
                });
            }));
            let started = runtime.submit(&agent_start("in-start", 1_700_000_001_000));
            let mut effect = effect_id(started.step_seq);
            let mut at = 1_700_000_002_000;
            for turn in 0..turns {
                let acted = runtime.submit(&provider_result(
                    &format!("in-acted-{turn}"),
                    at,
                    &effect,
                    vec![tool_call(
                        &format!("call-{turn}"),
                        "search",
                        json!({ "q": turn }),
                    )],
                ));
                at += 1_000;
                let results = runtime.submit(&tools_resolved(
                    &format!("in-results-{turn}"),
                    at,
                    &effect_id(acted.step_seq),
                    &[(&format!("call-{turn}"), "a source", false)],
                ));
                at += 1_000;
                effect = effect_id(results.step_seq);
            }

            // The host checkpoints at the head and hands the restore only what is above it.
            let checkpoint = runtime.checkpoint().decode().expect("verifies");
            let after = runtime.journal_from(&checkpoint);
            assert!(after.is_empty(), "the checkpoint covers the whole journal");
            Runtime::restore_with(Some(&checkpoint), &after)
                .restore_cost
                .expect("a restored runtime reports its cost")
        }

        let short = cost_after(2);
        let medium = cost_after(8);
        let long = cost_after(32);

        assert_eq!(short.total_transitions(), 0, "nothing is replayed at all");
        assert_eq!(
            (short, medium),
            (medium, long),
            "restore cost does not grow with the length of the run",
        );

        // And the contrast that makes the number mean something: without a checkpoint the same
        // restore reads the whole journal.
        let mut runtime = Runtime::new();
        drive(&mut runtime, &turn_envelopes());
        let journal = runtime.journal.clone();
        let from_genesis = Runtime::restore_with(None, &journal);
        assert_eq!(
            from_genesis.restore_cost.unwrap().records_before_checkpoint,
            journal.len() as u64,
            "the no-checkpoint arm is O(run), which is exactly what §12 replaces",
        );
        assert_eq!(surface(&from_genesis), surface(&runtime));
    }

    /// §12.2 last line · with no checkpoint the fold starts at genesis and runs the **same** path.
    #[test]
    fn a_restore_without_a_checkpoint_uses_the_same_transaction_fold() {
        let mut runtime = Runtime::new();
        drive(&mut runtime, &turn_envelopes()[..4]);
        let from_genesis = Runtime::restore_with(None, &runtime.journal);
        assert_eq!(surface(&from_genesis), surface(&runtime));
        assert_eq!(from_genesis.restore_cost.unwrap().tail_inputs_replayed, 0);
    }

    /// **Verification 3, row 1** · §12.3 rule 5: a crash between install and ack still restores
    /// from the installed checkpoint.
    ///
    /// The ack is a *retention* signal, not a durability one. Nothing about the checkpoint's
    /// validity depends on it, which is why this row is a test rather than a caveat.
    #[test]
    fn crash_matrix_install_then_crash_before_ack_restores_from_the_installed_checkpoint() {
        let envelopes = turn_envelopes();
        let mut runtime = Runtime::new();
        drive(&mut runtime, &envelopes[..4]);

        let candidate = runtime.checkpoint();
        let checkpoint = candidate.decode().expect("the host installed this blob");
        // ... and then the process dies. `note_checkpoint_acked` was never called.
        let restored = runtime.restore(&checkpoint);
        assert_eq!(surface(&restored), surface(&runtime));
        assert_eq!(restored.restore_cost.unwrap().records_before_checkpoint, 0);

        // §12.2 line 8 · what the host is handed back: the effects to (re-)execute, or a terminal.
        let recovered = restore_operation(
            Some(&checkpoint),
            &[],
            ConfigDefaults::default(),
            InMemoryRecordIndex::new(),
        )
        .expect("the ladder runs");
        assert_eq!(
            recovered
                .pending_effects()
                .iter()
                .map(|effect| effect.tag())
                .collect::<Vec<_>>(),
            vec![EffectKindTag::CallProvider],
            "§5g-1 · the effect the operation is waiting on is exposed again",
        );
        assert!(recovered.terminal().is_none(), "the run had not ended");
    }

    /// **Verification 3, row 2** · §12.3 rules 1 and 3: records appended after the candidate are
    /// kept as tail, and a restore replays them from the journal.
    #[test]
    fn crash_matrix_appends_after_a_candidate_are_restored_from_the_journal() {
        let envelopes = turn_envelopes();
        let mut runtime = Runtime::new();
        drive(&mut runtime, &envelopes[..4]);

        let checkpoint = runtime.checkpoint().decode().expect("verifies");
        // The candidate is a read: the operation keeps going while the host persists the blob.
        drive(&mut runtime, &envelopes[4..6]);

        let restored = runtime.restore(&checkpoint);
        let cost = restored.restore_cost.unwrap();
        assert_eq!(
            cost.records_after_checkpoint, 2,
            "the two records appended after the candidate are replayed from the journal",
        );
        assert_eq!(cost.records_before_checkpoint, 0);
        assert_eq!(surface(&restored), surface(&runtime));
    }

    /// **Verification 3, row 3** · §12.3 rule 2: install does not require the covered head to still
    /// be the current head.
    #[test]
    fn crash_matrix_install_when_the_covered_head_has_moved_on() {
        let envelopes = turn_envelopes();
        let mut runtime = Runtime::new();
        drive(&mut runtime, &envelopes[..4]);
        let candidate = runtime.checkpoint();
        let covered = candidate.through_step_seq;

        drive(&mut runtime, &envelopes[4..]);
        assert_ne!(
            runtime.tx.head().unwrap().step_seq,
            covered,
            "the journal has moved past the covered head",
        );

        // The ack still names a prefix of *this* journal, which is the only precondition rule 2
        // leaves standing.
        let mut acked = Runtime::new();
        drive(&mut acked, &envelopes);
        let usage = acked
            .tx
            .note_checkpoint_acked(&candidate.boundary())
            .expect("a checkpoint that covers a prefix is ackable after the head moved");
        assert_eq!(
            usage.records, 3,
            "acking reclaims the covered prefix and keeps the rest as tail",
        );

        // And the blob still restores to the prefix it was taken over.
        let restored = runtime.restore(&candidate.decode().unwrap());
        assert_eq!(surface(&restored), surface(&runtime));
    }

    /// **Verification 3, row 4** · §12.3 rules 6, 7 and 10: after an ack the prefix may be
    /// reclaimed, and a redelivery from down there is still answered — by reference.
    ///
    /// This is the row that would have been silently wrong without the ledger: with the record
    /// gone, a `prepare` that consulted only the journal would accept the input a **second** time.
    #[test]
    fn crash_matrix_ack_then_prune_then_restore_still_answers_a_redelivery() {
        let envelopes = turn_envelopes();
        let mut runtime = Runtime::new();
        drive(&mut runtime, &envelopes[..4]);

        let checkpoint = runtime.checkpoint().decode().expect("verifies");
        runtime
            .tx
            .note_checkpoint_acked(&checkpoint.boundary())
            .expect("the boundary names a prefix of this journal");

        // Retention reclaims everything the checkpoint covers: the restore gets the blob and an
        // empty journal, exactly as a pruned host would hand it over.
        let mut restored = Runtime::restore_with(Some(&checkpoint), &[]);
        assert_eq!(surface(&restored), surface(&runtime));

        // A redelivery of an input from the reclaimed prefix is acknowledged, not re-accepted.
        let redelivered = restored.prepare(&envelopes[3]);
        let RecordPreparation::Replayed(replay) = redelivered else {
            panic!("a redelivery below the checkpoint base must not be accepted again");
        };
        assert_eq!(replay.step_seq, WireU64::new(3));
        assert_eq!(
            replay.record_digest,
            *runtime.journal[3].record_digest(),
            "§12.3 rule 10 · the answer is the original step and record digest",
        );
        assert!(
            replay.committed_step.is_none() && replay.record.is_none(),
            "and it carries no step payload — the guarantee down there is idempotent \
             acknowledgement, not step reproduction",
        );

        // The operation still runs forward from where it was.
        drive(&mut restored, &envelopes[4..]);
        let mut uninterrupted = Runtime::new();
        drive(&mut uninterrupted, &envelopes);
        assert_eq!(surface(&restored), surface(&uninterrupted));
    }

    /// §12.3 rule 8 / Task 16 acceptance · identity does not move across a restore.
    #[test]
    fn a_restore_preserves_effect_terminal_attempt_and_handle_identity() {
        let envelopes = turn_envelopes();
        let mut runtime = Runtime::new();
        drive(&mut runtime, &envelopes[..4]);

        let before_effects: Vec<String> = runtime
            .tx
            .pending_effects()
            .map(|effect| effect.effect_id.to_string())
            .collect();
        let before_handles =
            serde_json::to_value(runtime.driver.project_logical_state().context_vm.handles)
                .unwrap();
        let before_attempts =
            serde_json::to_value(runtime.driver.project_logical_state().scheduler.attempts)
                .unwrap();

        let checkpoint = runtime.checkpoint().decode().expect("verifies");
        let mut restored = Runtime::restore_with(Some(&checkpoint), &[]);

        assert!(!before_effects.is_empty(), "the fixture has live identity");
        assert_eq!(
            restored
                .tx
                .pending_effects()
                .map(|effect| effect.effect_id.to_string())
                .collect::<Vec<_>>(),
            before_effects,
            "§5g-1 · appended-but-unpublished effects are re-exposed under their own ids",
        );
        assert_eq!(
            serde_json::to_value(restored.driver.project_logical_state().context_vm.handles)
                .unwrap(),
            before_handles,
            "handle identity survives",
        );
        assert_eq!(
            serde_json::to_value(restored.driver.project_logical_state().scheduler.attempts)
                .unwrap(),
            before_attempts,
            "task attempt identity survives",
        );

        // A terminal survives too, and closes the restored operation to the same inputs.
        drive(&mut restored, &envelopes[4..]);
        let terminal = restored.tx.terminal().cloned().expect("the run ended");
        let after_terminal =
            Runtime::restore_with(Some(&restored.checkpoint().decode().unwrap()), &[]);
        assert_eq!(
            after_terminal.tx.terminal(),
            Some(&terminal),
            "the terminal is restored as the same terminal, not re-derived",
        );
    }

    /// §12.3 · the hard tail limit is a retryable `CheckpointRequired`, and there is no latch.
    ///
    /// The whole arc, because the latch this replaces was only visibly wrong at the *end* of it:
    /// refuse → checkpoint → ack → the same envelope, unchanged, succeeds.
    #[test]
    fn a_full_tail_refuses_retryably_and_the_same_envelope_succeeds_after_an_ack() {
        let mut runtime = Runtime::new();
        runtime.submit(&syscall_config_with(|config| {
            config.recovery_policy = Some(super::super::command::RecoveryPolicy {
                provider_recovery_attempts: None,
                output_recovery_attempts: None,
                tail_bounds: Some(super::super::command::TailBoundsPolicy {
                    soft_records: Some(WireU64::new(2)),
                    hard_records: Some(WireU64::new(3)),
                    soft_bytes: Some(WireU64::new(64 * 1024)),
                    hard_bytes: Some(WireU64::new(1024 * 1024)),
                }),
            });
        }));
        let started = runtime.submit(&agent_start("in-start", 1_700_000_001_000));
        let acted = runtime.submit(&provider_result(
            "in-acted",
            1_700_000_002_000,
            &effect_id(started.step_seq),
            vec![tool_call("call-1", "search", json!({"q": "sources"}))],
        ));

        let next = tools_resolved(
            "in-results",
            1_700_000_003_000,
            &effect_id(acted.step_seq),
            &[("call-1", "three sources found", false)],
        );
        let fault = runtime.reject(&next);
        assert_eq!(fault.code, KernelFaultCode::CheckpointRequired);
        assert!(fault.is_retryable(), "exactly one code says retry");

        // The refusal's shape is a contract in its own right — it is what tells four hosts to take
        // a checkpoint rather than to give up — so it is frozen as a fixture.
        let produced = json!({
            "expect": "checkpoint_required",
            "description":
                "Spec 12.3 · the next transaction would carry the journal tail past its hard limit, \
                 so `prepare` refuses with the one retryable fault code and zero mutation. The \
                 input was never accepted: after a checkpoint candidate is installed and acked, the \
                 *same* envelope — same input id, same clock, same payload — is submitted again and \
                 commits. There is no permanent overflow latch: an acked checkpoint takes the tail \
                 pressure straight back to nominal.",
            "tail_bounds": {
                "soft_records": "2",
                "hard_records": "3",
                "soft_bytes": "65536",
                "hard_bytes": "1048576",
            },
            "tail_usage_at_refusal": {
                "records": runtime.tx.tail_usage().records.to_string(),
            },
            "refused_envelope": serde_json::to_value(&next).unwrap(),
            "fault": serde_json::to_value(&fault).unwrap(),
            "retryable": fault.is_retryable(),
        });
        let expected = golden(
            "reject_transaction_checkpoint_required_tail_full.json",
            &produced,
        );
        assert_eq!(produced, expected, "the CheckpointRequired refusal drifted");

        // Zero mutation: the refusal moved nothing, so the checkpoint below covers the same prefix
        // the refusal saw.
        let head_before = runtime.tx.head().expect("a head");
        let candidate = runtime.checkpoint();
        assert_eq!(candidate.through_step_seq, head_before.step_seq);

        runtime
            .tx
            .note_checkpoint_acked(&candidate.boundary())
            .expect("the ack names this journal's head");

        // The *same* envelope, byte for byte — §5e-3 forbids re-stamping the clock — now commits.
        let committed = runtime.submit(&next);
        assert_eq!(committed.step_seq, WireU64::new(3));
        assert_eq!(
            runtime.tx.tail_pressure(),
            TailPressure::Nominal,
            "an acked checkpoint moves the pressure straight back — there is no latch",
        );
    }

    /// §12.3 · crossing the soft watermark is advice, delivered once, on the commit that crossed it.
    #[test]
    fn the_soft_watermark_is_advice_delivered_once_on_the_crossing() {
        let mut runtime = Runtime::new();
        let genesis = runtime.submit(&syscall_config_with(|config| {
            config.recovery_policy = Some(super::super::command::RecoveryPolicy {
                provider_recovery_attempts: None,
                output_recovery_attempts: None,
                tail_bounds: Some(super::super::command::TailBoundsPolicy {
                    soft_records: Some(WireU64::new(2)),
                    hard_records: Some(WireU64::new(8)),
                    soft_bytes: Some(WireU64::new(64 * 1024)),
                    hard_bytes: Some(WireU64::new(1024 * 1024)),
                }),
            });
        }));
        assert!(
            genesis.checkpoint_advice.is_none(),
            "one record is not a watermark crossing",
        );

        let started = runtime.submit(&agent_start("in-start", 1_700_000_001_000));
        let advice = started
            .checkpoint_advice
            .expect("the second record takes the tail to the soft watermark");
        assert_eq!(advice.through_step_seq, started.step_seq);
        assert_eq!(advice.usage.records, 2);
        assert_eq!(advice.bounds.soft_records, WireU64::new(2));

        let acted = runtime.submit(&provider_result(
            "in-acted",
            1_700_000_002_000,
            &effect_id(started.step_seq),
            vec![tool_call("call-1", "search", json!({"q": "sources"}))],
        ));
        assert!(
            acted.checkpoint_advice.is_none(),
            "advice is edge-triggered: staying over the watermark is not news",
        );
    }

    /// §12.3 rule 9 · rebuilding after a poisoned transaction costs O(tail) when a checkpoint
    /// exists.
    ///
    /// The failure this replaces re-executed every accepted input of the run, so the cost of one
    /// CAS conflict grew with the age of the operation. Here the recovery path *is* the restore
    /// path, and the counter says so.
    #[test]
    fn rebuilding_after_a_conflict_costs_the_tail_not_the_run() {
        let envelopes = turn_envelopes();
        let mut runtime = Runtime::new();
        drive(&mut runtime, &envelopes[..6]);
        let checkpoint = runtime.checkpoint().decode().expect("verifies");

        // A CAS conflict poisons the transaction: the journal moved under this runtime and §8.3
        // leaves exactly one way forward.
        let preparation = runtime.prepare(&envelopes[6]);
        let token = preparation.token().expect("prepared").clone();
        let fault = runtime.tx.note_append_conflict(&token, None);
        assert_eq!(fault.code, KernelFaultCode::TransactionConflict);
        assert!(runtime.tx.is_poisoned());

        let rebuilt = Runtime::restore_with(Some(&checkpoint), &[]);
        let cost = rebuilt.restore_cost.unwrap();
        assert_eq!(
            cost.total_transitions(),
            0,
            "the rebuild reads the checkpoint and nothing else — O(tail), not O(run)",
        );
        assert!(!rebuilt.tx.is_poisoned());
    }

    /// A checkpoint whose logical state was edited fails the restore's own re-projection check.
    ///
    /// The point of the check is that it catches a *hydration* gap as well as a tampered blob: both
    /// present as "the state that came back is not the state that was captured".
    #[test]
    fn a_restore_that_does_not_reproduce_the_captured_state_fails_closed() {
        let envelopes = turn_envelopes();
        let mut runtime = Runtime::new();
        drive(&mut runtime, &envelopes[..4]);
        let checkpoint = runtime.checkpoint().decode().expect("verifies");

        // Re-assemble the same header over a *different* logical state, so every digest is
        // internally consistent and only the state is wrong. A blob edited in storage would be
        // caught by `verify()`; this one gets past it and has to be caught by the re-projection.
        // A *derived* field is the honest probe here: the partition token counters come back from
        // re-pushing the messages, so a forged counter is exactly what a hydration gap would look
        // like from the outside — the state that came back is not the state that was captured.
        let mut state = checkpoint.logical_state().clone();
        state.context_vm.partition_tokens.history += 7;
        let forged = KernelCheckpoint::assemble(CheckpointDraft {
            operation_id: operation(),
            genesis_digest: checkpoint.genesis_digest().clone(),
            base_step_seq: checkpoint.base_step_seq(),
            base_record_digest: checkpoint.base_record_digest().clone(),
            through_step_seq: checkpoint.through_step_seq(),
            covered_transaction_head_digest: checkpoint.covered_transaction_head_digest().clone(),
            logical_state: state,
            tail_inputs: Vec::new(),
        })
        .expect("a self-consistent checkpoint over a different state");

        let error = restore_operation(
            Some(&forged),
            &[],
            ConfigDefaults::default(),
            InMemoryRecordIndex::new(),
        )
        .expect_err("a state that does not come back is a refusal, not a warning");
        assert_eq!(error.code, KernelFaultCode::CheckpointCorrupted);
        assert!(error.message.contains("restored logical state hashes to"));
    }

    /// §5e-5 · the tail bound the transaction enforces is the one the genesis record froze, not
    /// the binary's baseline.
    #[test]
    fn the_tail_bound_comes_from_the_genesis_configuration() {
        let mut runtime = Runtime::new();
        assert_eq!(
            runtime.tx.bounds(),
            TailBounds::DEFAULT,
            "before genesis the transaction runs on the bootstrap baseline",
        );

        runtime.submit(&syscall_config_with(|config| {
            config.recovery_policy = Some(super::super::command::RecoveryPolicy {
                provider_recovery_attempts: None,
                output_recovery_attempts: None,
                tail_bounds: Some(super::super::command::TailBoundsPolicy {
                    soft_records: Some(WireU64::new(4)),
                    hard_records: Some(WireU64::new(8)),
                    soft_bytes: Some(WireU64::new(64 * 1024)),
                    hard_bytes: Some(WireU64::new(256 * 1024)),
                }),
            });
        }));

        assert_eq!(
            runtime.tx.bounds(),
            TailBounds::new(4, 8, 64 * 1024, 256 * 1024).unwrap(),
            "the genesis record's resolved configuration is what bounds the tail",
        );
        assert_eq!(
            runtime
                .tx
                .config()
                .expect("configured")
                .recovery_policy
                .tail_bounds,
            runtime.tx.bounds(),
            "and it is frozen in the record, so a rebuild re-derives the same bound",
        );
    }
}