deepstrike-sdk 0.2.65

DeepStrike Rust SDK — agent framework built on deepstrike-core
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
//! Canonical host projection for the Rust production runner.
//!
//! Host events are lowered to wire inputs, committed through [`CanonicalKernelHost`], and projected
//! back to the [`HostAction`] / [`KernelObservation`] shapes the runner matches.

use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

use crate::runtime::CanonicalTransition;
use crate::runtime::canonical_kernel::{
    CanonicalKernel, EffectKind, EffectsDisposition, KernelEffect as WireKernelEffect,
    KernelTerminal, OperationLifecycle, PlannedStep, StepDisposition, TerminalDisposition,
    canonical_digest,
};
use crate::runtime::canonical_kernel_step::CanonicalKernelHost;
use crate::runtime::kernel_journal::KernelJournal;
use crate::{Error, Result};
use compact_str::CompactString;
use deepstrike_core::context::renderer::RenderedContext;
use deepstrike_core::mm::memory::{
    MemoryAuthor, MemoryKind, MemoryProvenance, MemoryQuery, MemoryRecord, MemoryScope,
    MemoryTrustLevel,
};
use deepstrike_core::runtime::kernel::wire::CancellationReason;
use deepstrike_core::runtime::kernel::{
    KernelObservation, KernelPressureAction, PublishedEffectRef,
};
use deepstrike_core::types::message::{Content, Message, Role, ToolCall, ToolSchema};
use deepstrike_core::types::milestone::{MilestoneContract, MilestoneVerifier};
use deepstrike_core::types::result::{LoopResult, PaceAction, PaceDecision, TerminationReason};
use serde_json::{Map, Value, json};

use super::host_projection::{HostAction, HostEffect};

pub(crate) type PersistPayloadFn = Arc<
    dyn Fn(String, String, usize) -> Pin<Box<dyn Future<Output = Result<PersistedPayload>> + Send>>
        + Send
        + Sync,
>;

#[derive(Debug, Clone)]
pub(crate) struct PersistedPayload {
    pub payload_ref: String,
    pub digest: String,
    pub original_size: String,
    pub preview: String,
}

#[derive(Clone)]
pub(crate) struct CanonicalRunnerOptions {
    pub max_context_tokens: u32,
    pub max_turns: Option<u32>,
    pub max_total_tokens: Option<u64>,
    pub max_wall_ms: Option<u64>,
    pub memory_binding_id: String,
    pub persist_payload: Option<PersistPayloadFn>,
}

/// Canonical operation runtime for production runner callsites.
pub(crate) struct CanonicalRunnerRuntime {
    host: CanonicalKernelHost,
    config: Map<String, Value>,
    initial_context: InitialContext,
    memory_binding_id: String,
    persist_payload: Option<PersistPayloadFn>,
    configured: bool,
    started: bool,
    last_action: Option<HostAction>,
    observations: Vec<KernelObservation>,
    milestone_phases: std::collections::HashMap<String, MilestonePhaseProjection>,
    payload_inline_threshold: usize,
    payload_preview_bytes: usize,
}

#[derive(Debug, Clone, Default)]
struct InitialContext {
    messages: Vec<Value>,
    knowledge: Vec<Value>,
    capabilities: Vec<Value>,
}

#[derive(Debug, Clone, Default)]
struct MilestonePhaseProjection {
    criteria: Vec<String>,
    verifier: Option<MilestoneVerifier>,
    required_evidence: Vec<String>,
}

impl CanonicalRunnerRuntime {
    pub fn new(
        kernel: CanonicalKernel,
        journal: Arc<dyn KernelJournal>,
        operation_id: impl Into<String>,
        options: CanonicalRunnerOptions,
    ) -> Result<Self> {
        let mut execution_policy = Map::new();
        execution_policy.insert(
            "max_context_tokens".into(),
            json!(options.max_context_tokens),
        );
        if let Some(max_turns) = options.max_turns {
            execution_policy.insert("max_turns".into(), json!(max_turns));
        }
        if let Some(max_total_tokens) = options.max_total_tokens {
            execution_policy.insert(
                "max_total_tokens".into(),
                Value::String(max_total_tokens.to_string()),
            );
        }
        if let Some(max_wall_ms) = options.max_wall_ms {
            execution_policy.insert("max_wall_ms".into(), Value::String(max_wall_ms.to_string()));
        }

        let mut config = Map::new();
        config.insert("execution_policy".into(), Value::Object(execution_policy));
        config.insert(
            "host_effect_support".into(),
            json!({
                "supported": [
                    "call_provider", "execute_tools", "request_approval", "spawn_tasks",
                    "preempt_tasks", "persist_memory", "query_memory", "archive_page_out",
                    "load_payload", "evaluate_milestone",
                ]
            }),
        );
        config.insert(
            "kernel_limits".into(),
            json!({
                "max_json_depth": 64,
                "max_collection_entries": 65_536,
                "collection_limits": {
                    "tool_catalog": 4_096,
                    "skill_catalog": 4_096,
                    "knowledge_entries": 65_536,
                    "initial_messages": 65_536,
                    "capability_grants": 65_536,
                    "governance_rules": 65_536,
                },
            }),
        );

        Ok(Self {
            host: CanonicalKernelHost::new(kernel, journal, operation_id)?,
            config,
            initial_context: InitialContext::default(),
            memory_binding_id: options.memory_binding_id,
            persist_payload: options.persist_payload,
            configured: false,
            started: false,
            last_action: None,
            observations: Vec::new(),
            milestone_phases: std::collections::HashMap::new(),
            payload_inline_threshold: 50 * 1024,
            payload_preview_bytes: 2 * 1024,
        })
    }

    pub fn operation_id(&self) -> &str {
        self.host.operation_id()
    }

    pub fn turn(&self) -> u32 {
        self.host.turn()
    }

    pub fn is_terminal(&self) -> bool {
        matches!(
            self.host.lifecycle(),
            OperationLifecycle::Completed
                | OperationLifecycle::Cancelled
                | OperationLifecycle::Failed
        )
    }

    pub fn recovery_content_bytes(&self) -> usize {
        if let Some(bytes) = self.host.recovery_content_bytes() {
            return bytes;
        }
        let max = self
            .config
            .get("execution_policy")
            .and_then(|v| v.get("max_context_tokens"))
            .and_then(|v| v.as_u64())
            .unwrap_or(0) as usize;
        max.saturating_mul(4).max(1024)
    }

    pub fn preserved_refs(&self) -> Vec<String> {
        self.host.preserved_refs()
    }

    pub fn count_tokens(&self, text: &str) -> u32 {
        self.host
            .count_tokens(text)
            .unwrap_or_else(|| ((text.len() / 4) as u32).max(1))
    }

    #[cfg(test)]
    pub fn local_subagents_spawned(&self) -> usize {
        self.host.local_subagents_spawned()
    }

    pub fn drain_host_observations(&mut self) -> Vec<KernelObservation> {
        std::mem::take(&mut self.observations)
    }

    pub fn drain_new_messages(&mut self) -> Vec<Message> {
        self.host.new_messages()
    }

    #[cfg(test)]
    pub fn pending_effect_count(&self) -> usize {
        self.host.pending_effects().len()
    }

    pub fn remember_milestone_contract(&mut self, contract: &MilestoneContract) {
        self.milestone_phases.clear();
        for phase in &contract.phases {
            self.milestone_phases.insert(
                phase.id.clone(),
                MilestonePhaseProjection {
                    criteria: phase.criteria.clone(),
                    verifier: phase.verifier.clone(),
                    required_evidence: phase.required_evidence.clone(),
                },
            );
        }
    }

    pub async fn restore(&mut self) -> Result<()> {
        self.host.restore().await?;
        let lifecycle = self.host.lifecycle();
        self.configured = !matches!(lifecycle, OperationLifecycle::Created);
        self.started = !matches!(
            lifecycle,
            OperationLifecycle::Created | OperationLifecycle::Configured
        );
        if let Some(transition) = self.host.drain_outbound_envelope().await? {
            self.apply_transition(&transition, true)?;
        }
        self.last_action = self.current_action()?;
        Ok(())
    }

    pub fn resume_action(&mut self) -> Result<Option<HostAction>> {
        self.last_action = self.current_action()?;
        Ok(self.last_action.clone())
    }

    pub async fn start_agent_value(
        &mut self,
        task: Value,
        run_spec: Option<Value>,
    ) -> Result<Option<HostAction>> {
        self.ensure_configured().await?;
        let task = object(Some(&task));
        let goal = string_field(&task, "goal");
        let mut entry = json!({
            "kind": "agent",
            "task": {
                "goal": goal,
                "criteria": task.get("criteria").cloned().unwrap_or_else(|| json!([])),
            },
        });
        if let Some(run_spec) = run_spec {
            entry.as_object_mut().unwrap().insert(
                "run_spec".into(),
                logical_run_spec(object(Some(&run_spec)), &goal),
            );
        }
        let action = self
            .commit(json!({
                "kind": "start_operation",
                "entry": entry,
                "initial_context": self.initial_context_json(),
            }))
            .await?;
        self.started = true;
        Ok(action)
    }

    #[cfg(test)]
    pub async fn start_workflow_value(&mut self, spec: Value) -> Result<Option<HostAction>> {
        self.ensure_configured().await?;
        let action = self
            .commit(json!({
                "kind": "start_operation",
                "entry": {
                    "kind": "workflow",
                    "spec": self.workflow_spec(object(Some(&spec))),
                },
                "initial_context": self.initial_context_json(),
            }))
            .await?;
        self.started = true;
        Ok(action)
    }

    /// Lower one SDK-owned host fact into the canonical five-class input taxonomy.
    ///
    /// This is intentionally a JSON-shaped host boundary: the canonical wire DTOs remain owned by
    /// core, while the SDK no longer imports or constructs the retired input enum.
    pub async fn apply_host_event(&mut self, event: Value) -> Result<Option<HostAction>> {
        if !self.started && self.apply_bootstrap(&event) {
            return Ok(None);
        }
        let kind = event
            .get("kind")
            .and_then(|v| v.as_str())
            .unwrap_or_default();
        match kind {
            "provider_result" => {
                let message = object(event.get("message"));
                let mut outcome = json!({
                    "kind": "completed",
                    "message": provider_message(&message),
                });
                if let Some(v) = event.get("observed_input_tokens") {
                    outcome
                        .as_object_mut()
                        .unwrap()
                        .insert("observed_input_tokens".into(), v.clone());
                }
                if let Some(v) = event.get("observed_output_tokens") {
                    outcome
                        .as_object_mut()
                        .unwrap()
                        .insert("observed_output_tokens".into(), v.clone());
                }
                if let Some(reason) = provider_stop_reason(event.get("stop_reason")) {
                    outcome
                        .as_object_mut()
                        .unwrap()
                        .insert("stop_reason".into(), Value::String(reason));
                }
                self.resolve(&event, json!({ "kind": "provider", "outcome": outcome }))
                    .await
            }
            "provider_error" => {
                let message = string_value(&event, "message");
                let error_kind = event
                    .get("error_kind")
                    .and_then(Value::as_str)
                    .unwrap_or("unknown");
                if error_kind == "context_overflow" {
                    self.resolve(
                        &event,
                        json!({ "kind": "provider", "outcome": { "kind": "context_overflow" } }),
                    )
                    .await
                } else {
                    let failure_kind = match error_kind {
                        "transport" | "rate_limit" | "model_unavailable" => "transport_exhausted",
                        "auth" | "invalid_request" | "modality" | "protocol" => "protocol_error",
                        _ => "unknown",
                    };
                    let retryable = event
                        .get("retryable")
                        .and_then(Value::as_bool)
                        .unwrap_or(false);
                    self.failed(&event, failure_kind, &message, retryable).await
                }
            }
            "tool_results" => {
                let mut results = Vec::new();
                for raw in event
                    .get("results")
                    .and_then(|v| v.as_array())
                    .cloned()
                    .unwrap_or_default()
                {
                    let result = object(Some(&raw));
                    let call_id = string_field(&result, "call_id");
                    let output = string_field(&result, "output");
                    let is_error = result.get("is_error").and_then(|v| v.as_bool()).unwrap_or(false);
                    let disposition = if result
                        .get("is_fatal")
                        .and_then(|v| v.as_bool())
                        .unwrap_or(false)
                    {
                        "fatal"
                    } else {
                        "recoverable"
                    };
                    let bytes = output.len();
                    if bytes > self.payload_inline_threshold {
                        if let Some(persist) = &self.persist_payload {
                            let persisted =
                                persist(call_id.clone(), output.clone(), self.payload_preview_bytes)
                                    .await?;
                            let mut external = json!({
                                "kind": "external",
                                "call_id": call_id,
                                "payload_ref": persisted.payload_ref,
                                "digest": persisted.digest,
                                "original_size": persisted.original_size,
                                "preview": persisted.preview,
                                "disposition": disposition,
                            });
                            if is_error {
                                external
                                    .as_object_mut()
                                    .unwrap()
                                    .insert("is_error".into(), json!(true));
                            }
                            results.push(external);
                            continue;
                        }
                    }
                    let mut inline_result = json!({
                        "output": output,
                        "disposition": disposition,
                    });
                    if is_error {
                        inline_result
                            .as_object_mut()
                            .unwrap()
                            .insert("is_error".into(), json!(true));
                    }
                    if let Some(tokens) = result.get("token_count") {
                        inline_result
                            .as_object_mut()
                            .unwrap()
                            .insert("tokens".into(), tokens.clone());
                    }
                    results.push(json!({
                        "kind": "inline",
                        "call_id": call_id,
                        "result": inline_result,
                    }));
                }
                self.resolve(&event, json!({ "kind": "tools", "results": results }))
                    .await
            }
            "approval_result" => {
                self.resolve(
                    &event,
                    json!({
                        "kind": "approval",
                        "approved_call_ids": event.get("approved_calls").cloned().unwrap_or_else(|| json!([])),
                        "denied_call_ids": event.get("denied_calls").cloned().unwrap_or_else(|| json!([])),
                    }),
                )
                .await
            }
            "workflow_spawn_result" => {
                let attempts_by_id = self.spawn_attempts();
                let attempts: Vec<Value> = attempts_by_id
                    .iter()
                    .map(|(task_id, attempt_id)| {
                        json!({
                            "task_id": task_id,
                            "attempt_id": attempt_id,
                            "outcome": { "status": "started" },
                        })
                    })
                    .collect();
                self.resolve(
                    &event,
                    json!({ "kind": "tasks_spawned", "attempts": attempts }),
                )
                .await
            }
            "preempt_result" => {
                let attempts_by_id = self.preempt_attempts();
                let attempts: Vec<Value> = attempts_by_id
                    .iter()
                    .map(|(task_id, attempt_id)| {
                        json!({
                            "task_id": task_id,
                            "attempt_id": attempt_id,
                            "outcome": { "status": "preempted" },
                        })
                    })
                    .collect();
                self.resolve(
                    &event,
                    json!({ "kind": "tasks_preempted", "attempts": attempts }),
                )
                .await
            }
            "sub_agent_completed" => {
                let raw = object(event.get("result"));
                let result = object(raw.get("result"));
                let task_id = string_field(&raw, "agent_id");
                let attempt_id = self
                    .host
                    .attempt_id(&task_id)
                    .ok_or_else(|| {
                        Error::Other(format!(
                            "sub-agent completion names task {task_id:?} with no live kernel-issued attempt"
                        ))
                    })?;
                let final_message = object(result.get("final_message"));
                let termination = string_field(&result, "termination");
                let status = if termination == "completed" {
                    "completed"
                } else {
                    "failed"
                };
                let mut child_result = json!({
                    "status": status,
                    "usage": {
                        "input_tokens": "0",
                        "output_tokens": result.get("total_tokens_used").map(|v| v.to_string()).unwrap_or_else(|| "0".into()),
                        "turns": result.get("turns_used").and_then(|v| v.as_u64()).unwrap_or(0),
                    },
                });
                if let Some(content) = final_message.get("content").and_then(|v| v.as_str()) {
                    if !content.is_empty() {
                        child_result
                            .as_object_mut()
                            .unwrap()
                            .insert("output".into(), Value::String(content.to_string()));
                    }
                }
                if !matches!(
                    termination.as_str(),
                    "completed" | "max_turns" | "token_budget" | ""
                ) {
                    child_result.as_object_mut().unwrap().insert(
                        "error".into(),
                        Value::String(if termination.is_empty() {
                            "failed".into()
                        } else {
                            termination
                        }),
                    );
                }
                let mut child = json!({
                    "kind": "child_completed",
                    "task_id": task_id,
                    "attempt_id": attempt_id,
                    "result": child_result,
                });
                let submitted = raw
                    .get("submitted_nodes")
                    .and_then(|v| v.as_array())
                    .cloned()
                    .unwrap_or_default();
                if !submitted.is_empty() {
                    let nodes = self
                        .workflow_spec(object(Some(&json!({ "nodes": submitted }))))
                        .get("nodes")
                        .cloned()
                        .unwrap_or_else(|| json!([]));
                    child.as_object_mut().unwrap().insert(
                        "parent_requests".into(),
                        json!([{ "kind": "append_workflow_nodes", "nodes": nodes }]),
                    );
                }
                self.commit(json!({
                    "kind": "deliver_external_event",
                    "event": child,
                }))
                .await
            }
            "memory_persist_result" => {
                if let Some(error) = event.get("error").filter(|v| !v.is_null()) {
                    self.failed(&event, "storage_unavailable", &error.to_string(), true)
                        .await
                } else {
                    let record_ref = event
                        .get("record_ref")
                        .and_then(|v| v.as_str())
                        .map(|s| s.to_string())
                        .unwrap_or_else(|| format!("memory:{}", uuid::Uuid::new_v4()));
                    let digest = event
                        .get("digest")
                        .and_then(|v| v.as_str())
                        .map(|s| s.to_string())
                        .unwrap_or_else(|| {
                            sha256_digest(
                                event
                                    .get("record_ref")
                                    .or_else(|| event.get("effect_id"))
                                    .and_then(|v| v.as_str())
                                    .unwrap_or(""),
                            )
                        });
                    self.resolve(
                        &event,
                        json!({
                            "kind": "memory_persisted",
                            "receipt": {
                                "binding_id": self.memory_binding_id,
                                "record_ref": record_ref,
                                "digest": digest,
                            },
                        }),
                    )
                    .await
                }
            }
            "memory_query_result" => {
                if let Some(error) = event.get("error").filter(|v| !v.is_null()) {
                    self.failed(&event, "storage_unavailable", &error.to_string(), true)
                        .await
                } else {
                    let recalls: Vec<Value> = event
                        .get("hits")
                        .and_then(|v| v.as_array())
                        .cloned()
                        .unwrap_or_default()
                        .into_iter()
                        .map(|raw| {
                            let hit = object(Some(&raw));
                            let record = object(hit.get("record"));
                            let mut recall = json!({
                                "record_ref": record.get("record_id").and_then(|v| v.as_str()).unwrap_or(&format!("memory:{}", uuid::Uuid::new_v4())),
                                "name": string_field(&record, "name"),
                                "kind": record.get("kind").and_then(|v| v.as_str()).unwrap_or("reference"),
                                "content": string_field(&record, "content"),
                            });
                            if let Some(score) = hit.get("score").filter(|v| v.is_number()) {
                                recall
                                    .as_object_mut()
                                    .unwrap()
                                    .insert("score".into(), score.clone());
                            }
                            recall
                        })
                        .collect();
                    self.resolve(
                        &event,
                        json!({ "kind": "memory_queried", "recalls": recalls }),
                    )
                    .await
                }
            }
            "page_out_archive_result" => {
                if let Some(error) = event.get("error").filter(|v| !v.is_null()) {
                    self.failed(&event, "storage_unavailable", &error.to_string(), true)
                        .await
                } else {
                    let effect_id = string_value(&event, "effect_id");
                    let pending = self
                        .host
                        .pending_effects()
                        .into_iter()
                        .find(|effect| effect.effect_id.as_str() == effect_id);
                    let (handle_id, digest, original_size) = match pending.as_ref().map(|e| &e.effect)
                    {
                        Some(EffectKind::ArchivePageOut(archive)) => (
                            archive.handle_id.as_str().to_string(),
                            archive.payload.digest.as_str().to_string(),
                            archive.payload.original_size.get().to_string(),
                        ),
                        _ => (
                            String::new(),
                            sha256_digest(""),
                            "0".to_string(),
                        ),
                    };
                    let payload_ref = event
                        .get("archive_ref")
                        .or_else(|| event.get("payload_ref"))
                        .and_then(|v| v.as_str())
                        .map(|s| s.to_string())
                        .unwrap_or_else(|| format!("payload:{}", uuid::Uuid::new_v4()));
                    self.resolve(
                        &event,
                        json!({
                            "kind": "page_out_archived",
                            "receipt": {
                                "handle_id": handle_id,
                                "payload_ref": payload_ref,
                                "digest": digest,
                                "original_size": original_size,
                            },
                        }),
                    )
                    .await
                }
            }
            "milestone_result" => {
                let result = object(event.get("result"));
                let mut body = json!({
                    "phase_id": string_field(&result, "phase_id"),
                    "passed": result.get("passed").and_then(|v| v.as_bool()).unwrap_or(false),
                });
                if !body["passed"].as_bool().unwrap_or(false) {
                    if let Some(reason) = result.get("reason").and_then(|v| v.as_str()) {
                        body.as_object_mut()
                            .unwrap()
                            .insert("notes".into(), Value::String(reason.to_string()));
                    }
                }
                self.resolve(
                    &event,
                    json!({ "kind": "milestone_evaluated", "result": body }),
                )
                .await
            }
            "payload_loaded" => {
                let payload = event.get("payload").cloned().unwrap_or_else(|| {
                    json!({
                        "content": string_value(&event, "content"),
                        "digest": string_value(&event, "digest"),
                        "original_size": event
                            .get("original_size")
                            .map(|value| match value {
                                Value::String(value) => value.clone(),
                                other => other.to_string(),
                            })
                            .unwrap_or_else(|| "0".into()),
                    })
                });
                self.resolve(
                    &event,
                    json!({
                        "kind": "payload_loaded",
                        "handle_id": string_value(&event, "handle_id"),
                        "payload": payload,
                    }),
                )
                .await
            }
            "payload_load_failed" => {
                self.failed(
                    &event,
                    "storage_unavailable",
                    &string_value(&event, "error"),
                    true,
                )
                .await
            }
            "deliver_signal" => {
                self.commit(json!({
                    "kind": "deliver_external_event",
                    "event": canonical_signal(&event),
                }))
                .await
            }
            "add_knowledge_message" => {
                if !self.started {
                    return Ok(None);
                }
                let mut entry = json!({ "content": string_value(&event, "content") });
                if let Some(key) = event.get("key") {
                    entry.as_object_mut().unwrap().insert("key".into(), key.clone());
                }
                if let Some(tokens) = event.get("tokens") {
                    entry
                        .as_object_mut()
                        .unwrap()
                        .insert("tokens".into(), tokens.clone());
                }
                if event.get("pinned").and_then(|v| v.as_bool()).unwrap_or(false) {
                    entry
                        .as_object_mut()
                        .unwrap()
                        .insert("pinned".into(), json!(true));
                }
                self.commit(json!({
                    "kind": "host_control",
                    "command": { "kind": "seed_knowledge", "entries": [entry] },
                }))
                .await
            }
            "remove_knowledge" => {
                self.commit(json!({
                    "kind": "host_control",
                    "command": {
                        "kind": "apply_knowledge_mutation",
                        "mutation": { "remove": [string_value(&event, "key")] },
                    },
                }))
                .await
            }
            "skill_deactivated" => {
                self.commit(json!({
                    "kind": "host_control",
                    "command": {
                        "kind": "apply_skill_activation",
                        "deactivate": [string_value(&event, "name")],
                    },
                }))
                .await
            }
            "capability_command" => {
                self.commit(json!({
                    "kind": "host_control",
                    "command": canonical_capability_command(object(event.get("command"))),
                }))
                .await
            }
            "add_history_message" => Err(Error::Other(
                "unsupported_host_event: running ABI v3 operations accept history only through effects or external events".into(),
            )),
            "cancel_operation" => {
                let reason = event
                    .get("reason")
                    .cloned()
                    .unwrap_or_else(|| json!("user"));
                let pending_call_ids = event
                    .get("pending_call_ids")
                    .cloned()
                    .unwrap_or_else(|| json!([]));
                let action = self
                    .commit(json!({
                        "kind": "host_control",
                        "command": {
                            "kind": "cancel",
                            "reason": reason,
                            "pending_call_ids": pending_call_ids,
                        },
                    }))
                    .await?;
                self.observations
                    .push(KernelObservation::OperationCancelled {
                        turn: self.turn(),
                        operation_id: self.operation_id().to_string(),
                        reason: CancellationReason::User,
                        pending_call_ids: event
                            .get("pending_call_ids")
                            .and_then(|v| v.as_array())
                            .map(|arr| {
                                arr.iter()
                                    .filter_map(|v| v.as_str().map(|s| s.to_string()))
                                    .collect()
                            })
                            .unwrap_or_default(),
                    });
                Ok(action)
            }
            "update_task" => {
                self.commit(json!({
                    "kind": "host_control",
                    "command": {
                        "kind": "update_task",
                        "update": event.get("update").cloned().unwrap_or(Value::Null),
                    },
                }))
                .await
            }
            other => Err(Error::Other(format!(
                "unsupported_host_event: canonical ABI has no lowering for {other}"
            ))),
        }
    }

    async fn ensure_configured(&mut self) -> Result<()> {
        if !self.configured {
            self.commit(json!({
                "kind": "configure_operation",
                "config": Value::Object(self.config.clone()),
            }))
            .await?;
            self.configured = true;
        }
        Ok(())
    }

    async fn commit(&mut self, input: Value) -> Result<Option<HostAction>> {
        let transition = self.host.transition_input(input).await?;
        let publish_observations = !transition.replayed;
        self.apply_transition(&transition, publish_observations)
    }

    fn apply_transition(
        &mut self,
        transition: &CanonicalTransition,
        publish_observations: bool,
    ) -> Result<Option<HostAction>> {
        if publish_observations {
            self.observations
                .extend(transition.planned_step.observations.clone());
            // §7.11 · a committed step that publishes effects is a fact worth recording in the
            // host event log: the journal stores only a digest of the step, so this manifest is
            // what makes the published effect ids + kinds recoverable post-hoc without replaying.
            let manifest =
                deepstrike_core::runtime::kernel::wire::projection::published_effects_manifest(
                    &transition.planned_step,
                );
            if !manifest.is_empty() {
                self.observations
                    .push(KernelObservation::StepPublishedEffects {
                        effects: manifest
                            .iter()
                            .map(|effect| PublishedEffectRef {
                                effect_id: effect.effect_id.to_string(),
                                kind: serde_json::to_value(effect.kind)
                                    .ok()
                                    .and_then(|value| value.as_str().map(|tag| tag.to_string()))
                                    .unwrap_or_default(),
                            })
                            .collect(),
                    });
            }
        }
        self.last_action = self.enrich_action(action_from_core_step(&transition.planned_step)?);
        Ok(self.last_action.clone())
    }

    async fn resolve(&mut self, event: &Value, result: Value) -> Result<Option<HostAction>> {
        self.commit(json!({
            "kind": "resolve_effect",
            "effect_id": string_value(event, "effect_id"),
            "outcome": { "status": "succeeded", "result": result },
        }))
        .await
    }

    async fn failed(
        &mut self,
        event: &Value,
        kind: &str,
        message: &str,
        retryable: bool,
    ) -> Result<Option<HostAction>> {
        self.commit(json!({
            "kind": "resolve_effect",
            "effect_id": string_value(event, "effect_id"),
            "outcome": {
                "status": "failed",
                "failure": {
                    "kind": kind,
                    "message": message,
                    "retryable": retryable,
                },
            },
        }))
        .await
    }

    fn current_action(&self) -> Result<Option<HostAction>> {
        if let Some(terminal) = self.host.terminal() {
            return action_from_core_step(&PlannedStep {
                root_kind: None,
                focus: None,
                observations: Vec::new(),
                disposition: StepDisposition::Terminal(TerminalDisposition {
                    terminal: terminal.clone(),
                }),
            });
        }
        let effects = self.host.pending_effects();
        Ok(self.enrich_action(action_from_core_step(&PlannedStep {
            root_kind: None,
            focus: None,
            observations: Vec::new(),
            disposition: StepDisposition::Effects(EffectsDisposition { effects }),
        })?))
    }

    fn enrich_action(&self, mut action: Option<HostAction>) -> Option<HostAction> {
        if let Some(HostAction {
            effect:
                HostEffect::EvaluateMilestone {
                    phase_id,
                    criteria,
                    verifier,
                    required_evidence,
                },
            ..
        }) = action.as_mut()
            && let Some(phase) = self.milestone_phases.get(phase_id)
        {
            *criteria = phase.criteria.clone();
            *verifier = phase.verifier.clone();
            *required_evidence = phase.required_evidence.clone();
        }
        action
    }

    fn spawn_attempts(&self) -> Vec<(String, String)> {
        let mut attempts = Vec::new();
        for effect in self.host.pending_effects() {
            if let EffectKind::SpawnTasks(spawn) = &effect.effect {
                for task in &spawn.tasks {
                    attempts.push((
                        task.task_id.as_str().to_string(),
                        task.attempt_id.as_str().to_string(),
                    ));
                }
            }
        }
        attempts
    }

    fn preempt_attempts(&self) -> Vec<(String, String)> {
        let mut attempts = Vec::new();
        for effect in self.host.pending_effects() {
            if let EffectKind::PreemptTasks(preempt) = &effect.effect {
                for attempt in &preempt.attempts {
                    attempts.push((
                        attempt.task_id.as_str().to_string(),
                        attempt.attempt_id.as_str().to_string(),
                    ));
                }
            }
        }
        attempts
    }

    fn initial_context_json(&self) -> Value {
        json!({
            "messages": self.initial_context.messages,
            "knowledge": self.initial_context.knowledge,
            "capabilities": self.initial_context.capabilities,
        })
    }

    fn feature_policy(&mut self) -> &mut Map<String, Value> {
        if !self.config.contains_key("feature_policy") {
            self.config
                .insert("feature_policy".into(), Value::Object(Map::new()));
        }
        self.config
            .get_mut("feature_policy")
            .unwrap()
            .as_object_mut()
            .unwrap()
    }

    fn execution_policy(&mut self) -> &mut Map<String, Value> {
        if !self.config.contains_key("execution_policy") {
            self.config
                .insert("execution_policy".into(), Value::Object(Map::new()));
        }
        self.config
            .get_mut("execution_policy")
            .unwrap()
            .as_object_mut()
            .unwrap()
    }

    fn context_policy(&mut self) -> &mut Map<String, Value> {
        if !self.config.contains_key("context_policy") {
            self.config
                .insert("context_policy".into(), Value::Object(Map::new()));
        }
        self.config
            .get_mut("context_policy")
            .unwrap()
            .as_object_mut()
            .unwrap()
    }

    fn apply_bootstrap(&mut self, event: &Value) -> bool {
        let kind = event
            .get("kind")
            .and_then(|v| v.as_str())
            .unwrap_or_default();
        match kind {
            "set_tokenizer" => true,
            "set_tools" => {
                self.config.insert(
                    "tool_catalog".into(),
                    event.get("tools").cloned().unwrap_or_else(|| json!([])),
                );
                true
            }
            "add_system_message" => {
                let mut message = json!({
                    "role": "system",
                    "content": string_value(event, "content"),
                });
                if let Some(tokens) = event.get("tokens") {
                    message
                        .as_object_mut()
                        .unwrap()
                        .insert("tokens".into(), tokens.clone());
                }
                self.initial_context.messages.push(message);
                true
            }
            "add_knowledge_message" => {
                let mut entry = json!({ "content": string_value(event, "content") });
                if let Some(key) = event.get("key") {
                    entry
                        .as_object_mut()
                        .unwrap()
                        .insert("key".into(), key.clone());
                }
                if let Some(tokens) = event.get("tokens") {
                    entry
                        .as_object_mut()
                        .unwrap()
                        .insert("tokens".into(), tokens.clone());
                }
                if event
                    .get("pinned")
                    .and_then(|v| v.as_bool())
                    .unwrap_or(false)
                {
                    entry
                        .as_object_mut()
                        .unwrap()
                        .insert("pinned".into(), json!(true));
                }
                self.initial_context.knowledge.push(entry);
                true
            }
            "preload_history" => {
                for message in event
                    .get("messages")
                    .and_then(|v| v.as_array())
                    .cloned()
                    .unwrap_or_default()
                {
                    self.initial_context
                        .messages
                        .push(initial_message(object(Some(&message))));
                }
                true
            }
            "set_available_skills" => {
                self.config.insert(
                    "skill_catalog".into(),
                    event.get("skills").cloned().unwrap_or_else(|| json!([])),
                );
                true
            }
            "load_governance_policy" => {
                let mut governance = if let Some(policy) = event.get("policy") {
                    object(Some(policy))
                } else {
                    let mut flat = object(Some(event));
                    flat.remove("kind");
                    flat
                };
                if let Some(rate_limits) = governance
                    .get_mut("rate_limits")
                    .and_then(|v| v.as_array_mut())
                {
                    for rule in rate_limits {
                        if let Some(obj) = rule.as_object_mut() {
                            if let Some(window) = obj.get("window_ms").cloned() {
                                obj.insert(
                                    "window_ms".into(),
                                    Value::String(match window {
                                        Value::String(s) => s,
                                        other => other.to_string(),
                                    }),
                                );
                            }
                        }
                    }
                }
                self.config
                    .insert("governance_policy".into(), Value::Object(governance));
                true
            }
            "set_signal_policy" => {
                let mut signal = object(event.get("policy"));
                signal.remove("version");
                if let Some(ttl) = signal.get("ttl_ms").cloned() {
                    signal.insert(
                        "ttl_ms".into(),
                        Value::String(match ttl {
                            Value::String(s) => s,
                            other => other.to_string(),
                        }),
                    );
                }
                self.config
                    .insert("signal_policy".into(), Value::Object(signal));
                true
            }
            "set_plan_tool_enabled" => {
                self.feature_policy().insert(
                    "plan_tool_enabled".into(),
                    json!(
                        event
                            .get("enabled")
                            .and_then(|v| v.as_bool())
                            .unwrap_or(false)
                    ),
                );
                true
            }
            "set_stable_core_tools" => {
                self.feature_policy().insert(
                    "stable_core_tool_ids".into(),
                    event.get("tool_ids").cloned().unwrap_or_else(|| json!([])),
                );
                true
            }
            "set_memory_enabled" => {
                let enabled = event
                    .get("enabled")
                    .and_then(|v| v.as_bool())
                    .unwrap_or(false);
                self.feature_policy()
                    .insert("memory_enabled".into(), json!(enabled));
                if enabled {
                    self.config.insert(
                        "memory_access".into(),
                        json!({
                            "binding_id": self.memory_binding_id,
                            "capabilities": { "read": true, "write": true },
                        }),
                    );
                }
                true
            }
            "set_knowledge_enabled" => {
                self.feature_policy().insert(
                    "knowledge_enabled".into(),
                    json!(
                        event
                            .get("enabled")
                            .and_then(|v| v.as_bool())
                            .unwrap_or(false)
                    ),
                );
                true
            }
            "set_resource_quota" => {
                let mut quota = object(event.get("quota"));
                if let Some(window) = quota
                    .get("memory_writes_per_window")
                    .and_then(|v| v.as_array())
                    .cloned()
                {
                    quota.insert(
                        "memory_writes_per_window".into(),
                        json!({
                            "max_events": window.first().and_then(|v| v.as_u64()).unwrap_or(0),
                            "window_ms": window.get(1).map(|v| v.to_string()).unwrap_or_else(|| "0".into()),
                        }),
                    );
                }
                self.config
                    .insert("resource_quota".into(), Value::Object(quota));
                true
            }
            "set_repeat_fuse" => {
                let mut fuse = object(Some(event));
                fuse.remove("kind");
                self.execution_policy()
                    .insert("repeat_fuse".into(), Value::Object(fuse));
                true
            }
            "set_criteria_gate" => {
                self.execution_policy().insert(
                    "criteria_gate_enabled".into(),
                    json!(
                        event
                            .get("enabled")
                            .and_then(|v| v.as_bool())
                            .unwrap_or(true)
                    ),
                );
                true
            }
            "set_knowledge_budget" => {
                let ratio = event.get("ratio").and_then(|v| v.as_f64()).unwrap_or(0.0);
                self.context_policy().insert(
                    "knowledge_budget_ppm".into(),
                    json!((ratio * 1_000_000.0).round() as u64),
                );
                true
            }
            "set_entropy_watch" => {
                let mut watch = object(Some(event));
                watch.remove("kind");
                if let Some(threshold) = watch.remove("threshold").and_then(|v| v.as_f64()) {
                    watch.insert(
                        "threshold_ppm".into(),
                        json!((threshold * 1_000_000.0).round() as u64),
                    );
                }
                if let Some(hysteresis) = watch.remove("hysteresis").and_then(|v| v.as_f64()) {
                    watch.insert(
                        "hysteresis_ppm".into(),
                        json!((hysteresis * 1_000_000.0).round() as u64),
                    );
                }
                self.execution_policy()
                    .insert("entropy_watch".into(), Value::Object(watch));
                true
            }
            "set_memory_policy" => {
                let mut policy = Map::new();
                for key in [
                    "stale_warning_days",
                    "retrieval_top_k",
                    "validation_enabled",
                    "max_content_bytes",
                    "max_name_length",
                ] {
                    if let Some(value) = event.get(key) {
                        policy.insert(key.into(), value.clone());
                    }
                }
                if let Some(value) = event.get("promotion_recall_threshold") {
                    policy.insert(
                        "promotion_recall_threshold".into(),
                        Value::String(match value {
                            Value::String(s) => s.clone(),
                            other => other.to_string(),
                        }),
                    );
                }
                self.config
                    .insert("memory_policy".into(), Value::Object(policy));
                true
            }
            "load_milestone_contract" => {
                let contract = object(event.get("contract"));
                self.milestone_phases.clear();
                let phases: Vec<Value> = contract
                    .get("phases")
                    .and_then(|v| v.as_array())
                    .cloned()
                    .unwrap_or_default()
                    .into_iter()
                    .map(|phase| {
                        let phase = object(Some(&phase));
                        let phase_id = phase
                            .get("id")
                            .or_else(|| phase.get("phase_id"))
                            .and_then(|v| v.as_str())
                            .unwrap_or("")
                            .to_string();
                        let criteria = string_array(phase.get("criteria"));
                        let required_evidence = string_array(phase.get("required_evidence"));
                        let verifier = phase
                            .get("verifier")
                            .filter(|value| !value.is_null())
                            .cloned()
                            .and_then(|value| serde_json::from_value(value).ok());
                        self.milestone_phases.insert(
                            phase_id.clone(),
                            MilestonePhaseProjection {
                                criteria,
                                verifier,
                                required_evidence,
                            },
                        );
                        let unlocks: Vec<Value> = phase
                            .get("unlocks")
                            .and_then(|v| v.as_array())
                            .cloned()
                            .unwrap_or_default()
                            .into_iter()
                            .map(|item| {
                                if let Some(s) = item.as_str() {
                                    json!(s)
                                } else {
                                    json!(string_field(&object(Some(&item)), "id"))
                                }
                            })
                            .collect();
                        json!({
                            "phase_id": phase_id,
                            "unlocks": unlocks,
                        })
                    })
                    .collect();
                self.config.insert(
                    "verification_contracts".into(),
                    json!([{ "contract_id": "rust-default", "phases": phases }]),
                );
                true
            }
            "add_history_message" => {
                self.initial_context
                    .messages
                    .push(initial_message(object(event.get("message"))));
                true
            }
            "configure_run" => {
                self.merge_host_config(object(event.get("config")));
                true
            }
            _ => false,
        }
    }

    fn merge_host_config(&mut self, config: Map<String, Value>) {
        if let Some(governance) = config.get("governance") {
            let mut governance = object(Some(governance));
            if let Some(rate_limits) = governance
                .get_mut("rate_limits")
                .and_then(|v| v.as_array_mut())
            {
                for rule in rate_limits {
                    if let Some(obj) = rule.as_object_mut() {
                        if let Some(window) = obj.get("window_ms").cloned() {
                            obj.insert(
                                "window_ms".into(),
                                Value::String(match window {
                                    Value::String(s) => s,
                                    other => other.to_string(),
                                }),
                            );
                        }
                    }
                }
            }
            self.config
                .insert("governance_policy".into(), Value::Object(governance));
        }
        if let Some(context_policy) = config.get("context_policy") {
            self.config
                .insert("context_policy".into(), context_policy.clone());
        }
        if let Some(signal_policy) = config.get("signal_policy") {
            let mut signal = object(Some(signal_policy));
            signal.remove("version");
            if let Some(ttl) = signal.get("ttl_ms").cloned() {
                signal.insert(
                    "ttl_ms".into(),
                    Value::String(match ttl {
                        Value::String(s) => s,
                        other => other.to_string(),
                    }),
                );
            }
            self.config
                .insert("signal_policy".into(), Value::Object(signal));
        }
        if let Some(scheduler_policy) = config.get("scheduler_policy") {
            let mut scheduler = object(Some(scheduler_policy));
            scheduler.remove("version");
            self.config
                .insert("scheduler_policy".into(), Value::Object(scheduler));
        }
        if let Some(quota) = config.get("resource_quota") {
            let mut quota = object(Some(quota));
            if let Some(window) = quota
                .get("memory_writes_per_window")
                .and_then(|v| v.as_array())
                .cloned()
            {
                quota.insert(
                    "memory_writes_per_window".into(),
                    json!({
                        "max_events": window.first().and_then(|v| v.as_u64()).unwrap_or(0),
                        "window_ms": window.get(1).map(|v| v.to_string()).unwrap_or_else(|| "0".into()),
                    }),
                );
            }
            self.config
                .insert("resource_quota".into(), Value::Object(quota));
        }
        if let Some(grant) = config.get("budget_grant") {
            let mut grant = object(Some(grant));
            if let Some(tokens) = grant.get("tokens").cloned() {
                grant.insert(
                    "tokens".into(),
                    Value::String(match tokens {
                        Value::String(s) => s,
                        other => other.to_string(),
                    }),
                );
            }
            self.config
                .insert("budget_grant".into(), Value::Object(grant));
        }
        if let Some(prompt_budget) = config.get("prompt_budget") {
            self.context_policy()
                .insert("prompt_budget".into(), prompt_budget.clone());
        }
        if let Some(repeat_fuse) = config.get("repeat_fuse") {
            self.execution_policy()
                .insert("repeat_fuse".into(), repeat_fuse.clone());
        }
        if let Some(criteria_gate) = config.get("criteria_gate") {
            self.execution_policy()
                .insert("criteria_gate_enabled".into(), criteria_gate.clone());
        }
        if let Some(ratio) = config
            .get("knowledge_budget_ratio")
            .and_then(|v| v.as_f64())
        {
            self.context_policy().insert(
                "knowledge_budget_ppm".into(),
                json!((ratio * 1_000_000.0).round() as u64),
            );
        }
        if let Some(entropy) = config.get("entropy_watch") {
            let mut entropy = object(Some(entropy));
            if let Some(threshold) = entropy.remove("threshold").and_then(|v| v.as_f64()) {
                entropy.insert(
                    "threshold_ppm".into(),
                    json!((threshold * 1_000_000.0).round() as u64),
                );
            }
            if let Some(hysteresis) = entropy.remove("hysteresis").and_then(|v| v.as_f64()) {
                entropy.insert(
                    "hysteresis_ppm".into(),
                    json!((hysteresis * 1_000_000.0).round() as u64),
                );
            }
            self.execution_policy()
                .insert("entropy_watch".into(), Value::Object(entropy));
        }
        if let Some(reliability) = config.get("reliability") {
            let reliability = object(Some(reliability));
            let mut recovery = Map::new();
            if let Some(v) = reliability.get("provider_recovery_attempts") {
                recovery.insert("provider_recovery_attempts".into(), v.clone());
            }
            if let Some(v) = reliability.get("output_recovery_attempts") {
                recovery.insert("output_recovery_attempts".into(), v.clone());
            }
            if !recovery.is_empty() {
                self.config
                    .insert("recovery_policy".into(), Value::Object(recovery));
            }
            if let Some(max_input_bytes) = reliability.get("max_input_bytes") {
                let mut limits = object(self.config.get("kernel_limits"));
                limits.insert("max_input_bytes".into(), max_input_bytes.clone());
                self.config
                    .insert("kernel_limits".into(), Value::Object(limits));
            }
        }
    }

    fn workflow_spec(&self, raw: Map<String, Value>) -> Value {
        let nodes = raw
            .get("nodes")
            .and_then(|v| v.as_array())
            .cloned()
            .unwrap_or_default();
        let ids: Vec<String> = (0..nodes.len())
            .map(|index| format!("wf-node{index}"))
            .collect();
        let lowered: Vec<Value> = nodes
            .iter()
            .enumerate()
            .map(|(index, value)| {
                let node = object(Some(value));
                let task = object(node.get("task"));
                let goal = if let Some(s) = node.get("task").and_then(|v| v.as_str()) {
                    s.to_string()
                } else {
                    task.get("goal")
                        .or_else(|| node.get("goal"))
                        .and_then(|v| v.as_str())
                        .unwrap_or("")
                        .to_string()
                };
                let dependencies = node
                    .get("depends_on")
                    .or_else(|| node.get("dependsOn"))
                    .and_then(|v| v.as_array())
                    .cloned()
                    .unwrap_or_default();
                let depends_on: Vec<Value> = dependencies
                    .into_iter()
                    .map(|dep| {
                        if let Some(n) = dep.as_u64() {
                            let idx = n as usize;
                            if idx < ids.len() {
                                return json!(ids[idx]);
                            }
                        }
                        dep
                    })
                    .collect();
                let mut run_spec_raw = Map::new();
                run_spec_raw.insert("goal".into(), json!(goal.clone()));
                if let Some(role) = node.get("role") {
                    run_spec_raw.insert("role".into(), role.clone());
                }
                if let Some(isolation) = node.get("isolation") {
                    run_spec_raw.insert("isolation".into(), isolation.clone());
                }
                let mut node_json = json!({
                    "node_id": ids[index],
                    "task": {
                        "goal": goal,
                    },
                    "run_spec": logical_run_spec(run_spec_raw, &string_field(&task, "goal")),
                });
                if let Some(criteria) = task.get("criteria") {
                    node_json["task"]
                        .as_object_mut()
                        .unwrap()
                        .insert("criteria".into(), criteria.clone());
                }
                if !depends_on.is_empty() {
                    node_json
                        .as_object_mut()
                        .unwrap()
                        .insert("depends_on".into(), Value::Array(depends_on));
                }
                node_json
            })
            .collect();
        json!({ "nodes": lowered })
    }
}

pub(crate) async fn canonical_kernel_apply(
    runtime: &mut CanonicalRunnerRuntime,
    pending: &mut Vec<KernelObservation>,
    event: Value,
) -> Result<()> {
    runtime.apply_host_event(event).await?;
    pending.extend(runtime.drain_host_observations());
    Ok(())
}

pub(crate) async fn canonical_kernel_action(
    runtime: &mut CanonicalRunnerRuntime,
    pending: &mut Vec<KernelObservation>,
    event: Value,
) -> Result<HostAction> {
    let action = runtime.apply_host_event(event).await?;
    pending.extend(runtime.drain_host_observations());
    action.ok_or_else(|| {
        Error::Other("kernel transition returned no action and no fault".to_string())
    })
}

pub(crate) fn action_from_core_step(planned: &PlannedStep) -> Result<Option<HostAction>> {
    let projection =
        deepstrike_core::runtime::kernel::wire::projection::project_current_action(planned)
            .map_err(|error| {
                Error::Other(format!("kernel projection failed: {}", error.message))
            })?;
    match projection {
        deepstrike_core::runtime::kernel::wire::projection::CurrentProjection::Terminal(
            terminal,
        ) => Ok(Some(HostAction {
            effect_id: String::new(),
            causation_id: String::new(),
            effect: HostEffect::Done {
                result: loop_result_from_terminal(&terminal)?,
            },
        })),
        deepstrike_core::runtime::kernel::wire::projection::CurrentProjection::Idle => Ok(None),
        deepstrike_core::runtime::kernel::wire::projection::CurrentProjection::Action(_) => {
            let current_effect =
                deepstrike_core::runtime::kernel::wire::projection::current_effect(planned)
                    .expect("action projection must have a current effect");
            // A step that reduces a syscall batch may legitimately publish several effects
            // (different kinds — §15.3 admits at most one pending effect per kind). The host
            // consumes them one pending effect at a time: the first is the current action, and
            // the rest surface again in the pending-effects view once this one resolves.
            // Effect order follows the mint order, which puts the syscalls' own effects ahead
            // of the continuation's.
            Ok(Some(protocol_action_from_wire(
                current_effect,
                &planned.observations,
            )?))
        }
    }
}

fn protocol_action_from_wire(
    effect: &WireKernelEffect,
    observations: &[KernelObservation],
) -> Result<HostAction> {
    let effect_id = effect.effect_id.as_str().to_string();
    let causation_id = effect.causation_input_id.as_str().to_string();
    let mapped = match &effect.effect {
        EffectKind::CallProvider(_) => {
            let canonical =
                deepstrike_core::runtime::kernel::wire::projection::project_effect(effect);
            let deepstrike_core::runtime::kernel::wire::projection::CanonicalHostAction::CallProvider {
                payload: call,
                ..
            } = canonical
            else {
                unreachable!("call_provider effect must project to provider action");
            };
            HostEffect::CallProvider {
                context: rendered_context_from_wire(&call.context)?,
                tools: call
                    .tools
                    .iter()
                    .map(|tool| ToolSchema {
                        name: CompactString::from(tool.name.as_str()),
                        description: tool.description.clone(),
                        parameters: tool.parameters.get().clone(),
                    })
                    .collect(),
            }
        }
        EffectKind::ExecuteTools(_) => {
            let canonical =
                deepstrike_core::runtime::kernel::wire::projection::project_effect(effect);
            let deepstrike_core::runtime::kernel::wire::projection::CanonicalHostAction::ExecuteTools {
                payload: execute,
                ..
            } = canonical
            else {
                unreachable!("execute_tools effect must project to execute action");
            };
            HostEffect::ExecuteTool {
                calls: execute
                    .calls
                    .iter()
                    .map(|call| ToolCall {
                        id: CompactString::from(call.call_id.as_str()),
                        name: CompactString::from(call.name.as_str()),
                        arguments: call.arguments.get().clone(),
                    })
                    .collect(),
            }
        }
        EffectKind::RequestApproval(_) => {
            let canonical =
                deepstrike_core::runtime::kernel::wire::projection::project_effect(effect);
            let deepstrike_core::runtime::kernel::wire::projection::CanonicalHostAction::RequestApproval {
                payload: request,
                ..
            } = canonical
            else {
                unreachable!("request_approval effect must project to approval action");
            };
            HostEffect::RequestApproval {
                requests: request
                    .requests
                    .iter()
                    .map(
                        |item| deepstrike_core::scheduler::state_machine::ApprovalRequest {
                            call_id: item.call_id.as_str().to_string(),
                            tool: item.tool_name.clone(),
                            arguments: item.arguments.get().clone(),
                            reason: item.reason.clone().unwrap_or_default(),
                        },
                    )
                    .collect(),
            }
        }
        EffectKind::SpawnTasks(_) => {
            let canonical =
                deepstrike_core::runtime::kernel::wire::projection::project_effect(effect);
            let deepstrike_core::runtime::kernel::wire::projection::CanonicalHostAction::SpawnTasks { payload: spawn, .. } = canonical else {
                unreachable!("spawn_tasks effect must project to spawn action");
            };
            HostEffect::SpawnWorkflow {
                nodes: spawn
                    .tasks
                    .iter()
                    .map(|task| {
                        let role = task
                            .spec
                            .role
                            .and_then(|role| serde_json::to_value(role).ok())
                            .and_then(|v| v.as_str().map(|s| s.to_string()))
                            .unwrap_or_else(|| "custom".into());
                        let isolation = task
                            .spec
                            .isolation
                            .and_then(|isolation| serde_json::to_value(isolation).ok())
                            .and_then(|v| v.as_str().map(|s| s.to_string()))
                            .unwrap_or_else(|| "shared".into());
                        deepstrike_core::orchestration::workflow::WorkflowSpawnInfo {
                            agent_id: task.task_id.as_str().to_string(),
                            goal: task.spec.goal.clone(),
                            role,
                            isolation,
                            context_inheritance: "none".into(),
                            model_hint: None,
                            trust: "trusted".into(),
                            output_schema: None,
                            reducer: None,
                            input_agent_ids: Vec::new(),
                            judge_match: None,
                            loop_max_iters: None,
                            classify_labels: Vec::new(),
                            token_budget: None,
                            max_turns: None,
                            max_wall_ms: None,
                        }
                    })
                    .collect(),
                budget: None,
            }
        }
        EffectKind::PreemptTasks(_) => {
            let canonical =
                deepstrike_core::runtime::kernel::wire::projection::project_effect(effect);
            let deepstrike_core::runtime::kernel::wire::projection::CanonicalHostAction::PreemptTasks { payload: preempt, .. } = canonical else {
                unreachable!("preempt_tasks effect must project to preempt action");
            };
            HostEffect::PreemptSubAgents {
                agent_ids: preempt
                    .attempts
                    .iter()
                    .map(|attempt| attempt.task_id.as_str().to_string())
                    .collect(),
                reason: preempt.reason.clone(),
            }
        }
        EffectKind::PersistMemory(_) => {
            let canonical =
                deepstrike_core::runtime::kernel::wire::projection::project_effect(effect);
            let deepstrike_core::runtime::kernel::wire::projection::CanonicalHostAction::PersistMemory { payload: persist, .. } = canonical else {
                unreachable!("persist_memory effect must project to persist action");
            };
            HostEffect::PersistMemory {
                memory: memory_record_from_wire(&persist.memory),
            }
        }
        EffectKind::QueryMemory(_) => {
            let canonical =
                deepstrike_core::runtime::kernel::wire::projection::project_effect(effect);
            let deepstrike_core::runtime::kernel::wire::projection::CanonicalHostAction::QueryMemory {
                payload: query,
                ..
            } = canonical
            else {
                unreachable!("query effect must project to query action");
            };
            HostEffect::QueryMemory {
                query: MemoryQuery {
                    scope: MemoryScope::new(String::new(), String::new()),
                    query: query.query.text.clone(),
                    top_k: query.requested_k as usize,
                    kinds: query
                        .query
                        .kinds
                        .iter()
                        .filter_map(|kind| {
                            serde_json::to_value(kind)
                                .ok()
                                .and_then(|v| serde_json::from_value(v).ok())
                        })
                        .collect(),
                    min_score: None,
                },
                requested_k: query.requested_k as usize,
            }
        }
        EffectKind::ArchivePageOut(_) => {
            let canonical =
                deepstrike_core::runtime::kernel::wire::projection::project_effect(effect);
            let deepstrike_core::runtime::kernel::wire::projection::CanonicalHostAction::ArchivePageOut {
                payload: archive,
                ..
            } = canonical
            else {
                unreachable!("archive_page_out effect must project to archive action");
            };
            let archived =
                serde_json::from_str::<Vec<Message>>(&archive.payload.content).unwrap_or_default();
            let compressed = observations.iter().find_map(|obs| match obs {
                KernelObservation::Compressed {
                    action, summary, ..
                } => Some((*action, summary.clone())),
                _ => None,
            });
            let (pressure_action, summary) =
                compressed.unwrap_or((KernelPressureAction::MicroCompact, None));
            let tier = match pressure_action {
                KernelPressureAction::ContextCollapse | KernelPressureAction::AutoCompact => {
                    "semantic".to_string()
                }
                _ => "durable".to_string(),
            };
            HostEffect::ArchivePageOut {
                turn: 0,
                action: pressure_action,
                summary,
                archived,
                tier,
            }
        }
        EffectKind::LoadPayload(_) => {
            let canonical =
                deepstrike_core::runtime::kernel::wire::projection::project_effect(effect);
            let deepstrike_core::runtime::kernel::wire::projection::CanonicalHostAction::LoadPayload { payload: load, .. } = canonical else {
                unreachable!("load_payload effect must project to load action");
            };
            HostEffect::LoadPayload {
                handle_id: load.handle_id.as_str().to_string(),
                payload_ref: load.payload_ref.as_str().to_string(),
            }
        }
        EffectKind::EvaluateMilestone(_) => {
            let canonical =
                deepstrike_core::runtime::kernel::wire::projection::project_effect(effect);
            let deepstrike_core::runtime::kernel::wire::projection::CanonicalHostAction::EvaluateMilestone { payload: eval, .. } = canonical else {
                unreachable!("evaluate_milestone effect must project to milestone action");
            };
            HostEffect::EvaluateMilestone {
                phase_id: eval.request.phase_id.clone(),
                criteria: Vec::new(),
                verifier: None,
                required_evidence: Vec::new(),
            }
        }
        // The wire tag remains reserved, but A-00R removed the non-durable adaptive scheduler
        // producer. No host should receive this effect until request fingerprinting and durable
        // measurement semantics are approved.
        EffectKind::MeasurePrompt(_) => {
            return Err(Error::Other(
                "the Rust runner received reserved measure_prompt effect with no enabled \
                 scheduler producer"
                    .to_string(),
            ));
        }
    };
    Ok(HostAction {
        effect_id,
        causation_id,
        effect: mapped,
    })
}

fn loop_result_from_terminal(terminal: &KernelTerminal) -> Result<LoopResult> {
    let usage_tokens = |usage: &deepstrike_core::runtime::kernel::wire::UsageReport| {
        usage
            .input_tokens
            .get()
            .saturating_add(usage.output_tokens.get())
    };
    match terminal {
        KernelTerminal::Agent(agent) => {
            let result = &agent.result;
            Ok(LoopResult {
                termination: map_termination(result.termination),
                final_message: result
                    .final_message
                    .as_ref()
                    .map(message_from_wire_provider)
                    .transpose()?,
                turns_used: result.turns_used,
                total_tokens_used: usage_tokens(&agent.usage),
                loop_continue: None,
                classify_branch: None,
                pace_decision: result.pace_decision.as_ref().map(|pace| PaceDecision {
                    action: match pace.action {
                        deepstrike_core::runtime::kernel::wire::PaceAction::Continue => {
                            PaceAction::Continue
                        }
                        deepstrike_core::runtime::kernel::wire::PaceAction::Sleep => {
                            PaceAction::Sleep
                        }
                        deepstrike_core::runtime::kernel::wire::PaceAction::Stop => {
                            PaceAction::Stop
                        }
                    },
                    delay_ms: pace.delay_ms.map(|v| v.get()),
                    reason: pace.reason.clone(),
                    coerced_from: pace.coerced_from.clone(),
                }),
                tournament_winner: None,
            })
        }
        KernelTerminal::Workflow(workflow) => Ok(LoopResult {
            termination: match workflow.outcome.status {
                deepstrike_core::runtime::kernel::wire::WorkflowStatus::Completed => {
                    TerminationReason::Completed
                }
                deepstrike_core::runtime::kernel::wire::WorkflowStatus::Failed => {
                    TerminationReason::Error
                }
                deepstrike_core::runtime::kernel::wire::WorkflowStatus::Cancelled => {
                    TerminationReason::UserAbort
                }
            },
            final_message: None,
            turns_used: workflow.usage.turns,
            total_tokens_used: usage_tokens(&workflow.usage),
            loop_continue: None,
            classify_branch: None,
            pace_decision: None,
            tournament_winner: None,
        }),
        KernelTerminal::Cancelled(cancelled) => Ok(LoopResult {
            termination: TerminationReason::UserAbort,
            final_message: None,
            turns_used: cancelled.usage.turns,
            total_tokens_used: usage_tokens(&cancelled.usage),
            loop_continue: None,
            classify_branch: None,
            pace_decision: None,
            tournament_winner: None,
        }),
        KernelTerminal::Failed(failed) => {
            let termination = if failed.failure.code
                == deepstrike_core::runtime::kernel::wire::KernelFailureCode::ProviderRecoveryExhausted
            {
                TerminationReason::ContextOverflow
            } else {
                TerminationReason::Error
            };
            Ok(LoopResult {
                termination,
                final_message: None,
                turns_used: failed.usage.turns,
                total_tokens_used: usage_tokens(&failed.usage),
                loop_continue: None,
                classify_branch: None,
                pace_decision: None,
                tournament_winner: None,
            })
        }
    }
}

fn map_termination(
    reason: deepstrike_core::runtime::kernel::wire::TerminationReason,
) -> TerminationReason {
    use deepstrike_core::runtime::kernel::wire::TerminationReason as Wire;
    match reason {
        Wire::Completed => TerminationReason::Completed,
        Wire::MaxTurns => TerminationReason::MaxTurns,
        Wire::TokenBudget => TerminationReason::TokenBudget,
        Wire::Deadline => TerminationReason::Timeout,
        Wire::ContextOverflow => TerminationReason::ContextOverflow,
        Wire::NoProgress => TerminationReason::NoProgress,
        Wire::MilestoneExceeded => TerminationReason::MilestoneExceeded,
    }
}

fn rendered_context_from_wire(
    context: &deepstrike_core::runtime::kernel::wire::RenderedContext,
) -> Result<RenderedContext> {
    let turns = context
        .turns
        .iter()
        .map(message_from_wire_provider)
        .collect::<Result<Vec<_>>>()?;
    let state_turn = context
        .state_turn
        .as_ref()
        .map(message_from_wire_provider)
        .transpose()?;
    let system_text = if context.system_knowledge.is_empty() {
        context.system_stable.clone()
    } else if context.system_stable.is_empty() {
        context.system_knowledge.clone()
    } else {
        format!("{}\n\n{}", context.system_stable, context.system_knowledge)
    };
    Ok(RenderedContext {
        system_text,
        system_stable: context.system_stable.clone(),
        system_knowledge: context.system_knowledge.clone(),
        turns,
        state_turn,
        frozen_prefix_len: context.frozen_prefix_len.map(|v| v as usize),
        budget_overflow: None,
    })
}

fn message_from_wire_provider(
    message: &deepstrike_core::runtime::kernel::wire::ProviderMessage,
) -> Result<Message> {
    Ok(Message {
        role: match message.role {
            deepstrike_core::runtime::kernel::wire::MessageRole::System => Role::System,
            deepstrike_core::runtime::kernel::wire::MessageRole::User => Role::User,
            deepstrike_core::runtime::kernel::wire::MessageRole::Assistant => Role::Assistant,
            deepstrike_core::runtime::kernel::wire::MessageRole::Tool => Role::Tool,
        },
        content: Content::Text(message.content.clone()),
        tool_calls: message
            .tool_calls
            .iter()
            .map(|call| ToolCall {
                id: CompactString::from(call.call_id.as_str()),
                name: CompactString::from(call.name.as_str()),
                arguments: call.arguments.get().clone(),
            })
            .collect(),
        token_count: message.tokens,
    })
}

fn memory_record_from_wire(
    write: &deepstrike_core::runtime::kernel::wire::CanonicalMemoryWrite,
) -> MemoryRecord {
    let kind = serde_json::to_value(&write.kind)
        .ok()
        .and_then(|v| serde_json::from_value(v).ok())
        .unwrap_or(MemoryKind::Reference);
    let now = write.accepted_at_ms.get();
    MemoryRecord {
        record_id: format!("memory:{}", uuid::Uuid::new_v4()),
        scope: MemoryScope::new(String::new(), String::new()),
        name: write.name.clone(),
        kind,
        content: write.content.clone(),
        description: write.description.clone(),
        provenance: MemoryProvenance {
            session_id: None,
            author: MemoryAuthor::Model,
            trust: MemoryTrustLevel::Untrusted,
            evidence_refs: write.evidence_refs.clone(),
        },
        created_at: now,
        updated_at: now,
        last_recalled_at: None,
        recall_count: 0,
        confidence: 1.0,
        links: Vec::new(),
        pinned: false,
        ttl_days: None,
    }
}

fn logical_run_spec(raw: Map<String, Value>, goal: &str) -> Value {
    let mut out = Map::new();
    out.insert(
        "goal".into(),
        json!(raw.get("goal").and_then(|v| v.as_str()).unwrap_or(goal)),
    );
    for key in [
        "role",
        "isolation",
        "verification_contract_id",
        "exposure_baseline",
        "metadata",
        "capability_filter",
        "loop_round",
    ] {
        if let Some(value) = raw.get(key) {
            out.insert(key.into(), value.clone());
        }
    }
    Value::Object(out)
}

fn canonical_signal(event: &Value) -> Value {
    let signal = object(event.get("signal"));
    let delivery_id = event
        .get("delivery_id")
        .and_then(|v| v.as_str())
        .map(|s| s.to_string())
        .unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
    let payload = if delivery_id.starts_with("injected-") {
        if let Some(summary) = signal.get("summary").filter(|v| v.is_string()) {
            summary.clone()
        } else {
            signal.get("payload").cloned().unwrap_or_else(|| json!({}))
        }
    } else {
        signal.get("payload").cloned().unwrap_or_else(|| json!({}))
    };
    let mut wire_signal = json!({
        "signal_id": signal.get("signal_id").or_else(|| signal.get("id")).and_then(|v| v.as_str()).unwrap_or(&uuid::Uuid::new_v4().to_string()),
        "target": if let Some(recipient) = signal.get("recipient").and_then(|v| v.as_str()) {
            json!({ "kind": "task", "task_id": recipient })
        } else {
            json!({ "kind": "operation" })
        },
        "payload": payload,
    });
    if let Some(source) = signal.get("source") {
        wire_signal
            .as_object_mut()
            .unwrap()
            .insert("source".into(), source.clone());
    }
    if let Some(urgency) = signal.get("urgency") {
        wire_signal
            .as_object_mut()
            .unwrap()
            .insert("urgency".into(), urgency.clone());
    }
    if let Some(ts) = signal.get("timestamp_ms") {
        wire_signal.as_object_mut().unwrap().insert(
            "source_timestamp_ms".into(),
            Value::String(match ts {
                Value::String(s) => s.clone(),
                other => other.to_string(),
            }),
        );
    }
    if let Some(dedupe) = signal.get("dedupe_key") {
        wire_signal
            .as_object_mut()
            .unwrap()
            .insert("dedupe_key".into(), dedupe.clone());
    }
    json!({
        "kind": "deliver_signal",
        "delivery_id": delivery_id,
        "attempt": event.get("attempt").and_then(|v| v.as_u64()).unwrap_or(1),
        "signal": wire_signal,
    })
}

fn canonical_capability_command(command: Map<String, Value>) -> Value {
    let capability = object(command.get("capability"));
    if command.get("action").and_then(|v| v.as_str()) == Some("mount") {
        json!({
            "kind": "apply_capability_patch",
            "patch": {
                "mount": [{
                    "kind": capability.get("kind").and_then(|v| v.as_str()).unwrap_or("tool"),
                    "id": string_field(&capability, "id"),
                    "description": capability.get("description").cloned().unwrap_or(Value::Null),
                }],
            },
        })
    } else {
        json!({
            "kind": "apply_capability_patch",
            "patch": {
                "unmount": [{
                    "kind": command.get("kind").and_then(|v| v.as_str()).unwrap_or("tool"),
                    "id": string_field(&command, "id"),
                }],
            },
        })
    }
}

fn initial_message(raw: Map<String, Value>) -> Value {
    if raw.get("content").map(|v| v.is_array()).unwrap_or(false) {
        return json!({
            "role": raw.get("role").and_then(|v| v.as_str()).unwrap_or("user"),
            "content": raw.get("content").cloned().unwrap_or(json!("")),
            "tokens": raw.get("token_count").cloned(),
        });
    }
    let mut message = provider_message(&raw);
    message.as_object_mut().unwrap().remove("tool_calls");
    message
}

fn provider_message(raw: &Map<String, Value>) -> Value {
    let content = match raw.get("content") {
        Some(Value::String(s)) => Value::String(s.clone()),
        Some(other) => Value::String(other.to_string()),
        None => Value::String(String::new()),
    };
    let mut message = json!({
        "role": raw.get("role").and_then(|v| v.as_str()).unwrap_or("assistant"),
        "content": content,
    });
    if let Some(calls) = raw.get("tool_calls").and_then(|v| v.as_array()) {
        let tool_calls: Vec<Value> = calls
            .iter()
            .map(|call| {
                let call = object(Some(call));
                json!({
                    "call_id": call.get("call_id").or_else(|| call.get("id")).and_then(|v| v.as_str()).unwrap_or(""),
                    "name": string_field(&call, "name"),
                    "arguments": call.get("arguments").cloned().unwrap_or_else(|| json!({})),
                })
            })
            .collect();
        message
            .as_object_mut()
            .unwrap()
            .insert("tool_calls".into(), Value::Array(tool_calls));
    }
    message
}

fn provider_stop_reason(value: Option<&Value>) -> Option<String> {
    let reason = value.and_then(|v| v.as_str())?.to_ascii_lowercase();
    if reason.is_empty() {
        return None;
    }
    Some(match reason.as_str() {
        "end_turn" | "tool_use" | "max_tokens" | "stop_sequence" | "content_filter" => reason,
        _ => "other".into(),
    })
}

fn object(value: Option<&Value>) -> Map<String, Value> {
    value
        .and_then(|v| v.as_object())
        .cloned()
        .unwrap_or_default()
}

fn string_field(map: &Map<String, Value>, key: &str) -> String {
    map.get(key)
        .and_then(|v| v.as_str())
        .unwrap_or("")
        .to_string()
}

fn string_value(value: &Value, key: &str) -> String {
    string_field(&object(Some(value)), key)
}

fn string_array(value: Option<&Value>) -> Vec<String> {
    value
        .and_then(Value::as_array)
        .map(|items| {
            items
                .iter()
                .filter_map(Value::as_str)
                .map(str::to_string)
                .collect()
        })
        .unwrap_or_default()
}

fn sha256_digest(value: &str) -> String {
    canonical_digest(value.as_bytes()).as_str().to_string()
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use deepstrike_core::runtime::kernel::KernelObservation;
    use serde_json::json;

    use super::{CanonicalRunnerOptions, CanonicalRunnerRuntime};
    use crate::runtime::canonical_kernel::{CanonicalKernel, EffectKind, WireEnvelope};
    use crate::runtime::canonical_kernel_step::CanonicalKernelHost;
    use crate::runtime::host_projection::HostEffect;
    use crate::runtime::kernel_journal::{InMemoryKernelJournal, KernelJournal};

    #[test]
    fn production_host_has_no_caller_asserted_skill_activation_pipeline() {
        let forbidden = ["skill", "activated"].join("_");
        assert!(!include_str!("runner.rs").contains(&forbidden));
        assert!(!include_str!("canonical_runner_runtime.rs").contains(&forbidden));
    }

    /// A syscall-batch step may legitimately carry several effects (different kinds — §15.3
    /// admits at most one pending effect per kind). The projection hands the host the first
    /// one; the rest surface again through the pending-effects view once it resolves. The
    /// fixture is the SAME JSON the Node/WASM/Python projections test against.
    #[test]
    fn multi_effect_planned_step_projects_the_first_effect() {
        let fixture: serde_json::Value = serde_json::from_str(include_str!(
            "../../../tests/fixtures/abi/multi_effect_step.json"
        ))
        .expect("the shared multi-effect fixture parses");
        let planned: super::PlannedStep =
            serde_json::from_value(fixture["planned_step"].clone()).expect("a planned step");
        let action = super::action_from_core_step(&planned)
            .expect("the projection succeeds on a multi-effect step")
            .expect("an action");
        let expected = &fixture["expected_action"];
        assert_eq!(
            action.effect_id,
            expected["effect_id"].as_str().unwrap(),
            "the host acts on the first (syscall-minted) effect"
        );
    }

    fn test_options() -> CanonicalRunnerOptions {
        CanonicalRunnerOptions {
            max_context_tokens: 128_000,
            max_turns: None,
            max_total_tokens: None,
            max_wall_ms: None,
            memory_binding_id: "test-binding".into(),
            persist_payload: None,
        }
    }

    #[tokio::test]
    async fn workflow_spawn_result_uses_kernel_issued_attempt_ids() {
        let journal: Arc<dyn KernelJournal> = Arc::new(InMemoryKernelJournal::new());
        let mut runtime = CanonicalRunnerRuntime::new(
            CanonicalKernel::default(),
            journal.clone(),
            "op-spawn-attempts",
            test_options(),
        )
        .expect("runtime");

        let spec = deepstrike_core::orchestration::workflow::WorkflowSpec::new(vec![
            deepstrike_core::orchestration::workflow::WorkflowNode::new(
                deepstrike_core::types::task::RuntimeTask::new("do the thing"),
                deepstrike_core::types::agent::AgentRole::Implement,
            ),
        ]);
        let action = runtime
            .start_workflow_value(serde_json::to_value(spec).expect("workflow spec"))
            .await
            .expect("transition")
            .expect("action");

        let pending = runtime.host.pending_effects();
        let spawn = pending
            .iter()
            .find_map(|e| {
                if let EffectKind::SpawnTasks(spawn) = &e.effect {
                    Some(spawn)
                } else {
                    None
                }
            })
            .expect("a spawn effect is pending");
        assert_eq!(spawn.tasks.len(), 1);
        let task_id = spawn.tasks[0].task_id.as_str().to_string();
        let kernel_attempt_id = spawn.tasks[0].attempt_id.as_str().to_string();
        assert!(
            !kernel_attempt_id.is_empty(),
            "kernel must assign a non-empty attempt_id"
        );

        runtime
            .apply_host_event(json!({
                "kind": "workflow_spawn_result",
                "effect_id": action.effect_id,
                "started_agent_ids": [&task_id],
            }))
            .await
            .expect("resolve spawn");

        assert!(
            runtime.host.pending_effects().is_empty(),
            "spawn effect should be resolved"
        );
        assert_eq!(runtime.local_subagents_spawned(), 1);
        assert_eq!(
            runtime.host.attempt_id(&task_id).as_deref(),
            Some(kernel_attempt_id.as_str())
        );

        drop(runtime);
        let mut restored = CanonicalRunnerRuntime::new(
            CanonicalKernel::default(),
            journal,
            "op-spawn-attempts",
            test_options(),
        )
        .expect("restored runtime");
        restored.restore().await.expect("restore");
        assert_eq!(
            restored.host.attempt_id(&task_id).as_deref(),
            Some(kernel_attempt_id.as_str()),
            "checkpoint/journal restore must preserve the kernel-issued attempt"
        );
        assert_eq!(
            restored.local_subagents_spawned(),
            1,
            "restart must project the kernel-owned spawn count"
        );

        restored
            .apply_host_event(json!({
                "kind": "sub_agent_completed",
                "result": {
                    "agent_id": task_id,
                    "result": {
                        "termination": "completed",
                        "final_message": null,
                        "turns_used": 1,
                        "total_tokens_used": 1
                    }
                }
            }))
            .await
            .expect("resolve child completion after restore");

        // Drain any observations produced by the resolution.
        let _ = restored.drain_host_observations();
    }

    #[tokio::test]
    async fn restore_publishes_observations_from_a_staged_durable_replay() {
        let fixture: serde_json::Value = serde_json::from_str(include_str!(
            "../../../tests/fixtures/kernel-wire/golden_lifecycle_agent_root.json"
        ))
        .expect("fixture");
        let configure: WireEnvelope =
            serde_json::from_value(fixture["links"][0]["envelope"].clone())
                .expect("configure envelope");
        let start: WireEnvelope = serde_json::from_value(fixture["links"][1]["envelope"].clone())
            .expect("start envelope");
        let journal: Arc<dyn KernelJournal> = Arc::new(InMemoryKernelJournal::new());
        let writer = CanonicalKernelHost::new(
            CanonicalKernel::default(),
            journal.clone(),
            configure.operation_id.as_str(),
        )
        .expect("writer");

        writer.transition(configure).await.expect("configure");
        writer.transition(start.clone()).await.expect("start");
        journal
            .stage_outbound_envelope(
                start.operation_id.as_str(),
                &serde_json::to_string(&start).expect("serialize staged envelope"),
            )
            .await
            .expect("stage replay");

        let mut restored = CanonicalRunnerRuntime::new(
            CanonicalKernel::default(),
            journal,
            start.operation_id.as_str(),
            test_options(),
        )
        .expect("restored runtime");
        restored.restore().await.expect("restore");

        assert!(
            restored
                .drain_host_observations()
                .iter()
                .any(|observation| matches!(
                    observation,
                    KernelObservation::CheckpointTaken { .. }
                )),
            "the restarted runner must publish observations from the staged durable transition"
        );
        assert!(restored.resume_action().expect("resume action").is_some());
    }

    #[tokio::test]
    async fn restore_projects_turn_and_messages_from_canonical_state() {
        let fixture: serde_json::Value = serde_json::from_str(include_str!(
            "../../../tests/fixtures/kernel-wire/golden_lifecycle_agent_full_turn.json"
        ))
        .expect("fixture");
        let envelopes: Vec<WireEnvelope> = fixture["links"]
            .as_array()
            .expect("fixture links")
            .iter()
            .map(|link| serde_json::from_value(link["envelope"].clone()).expect("fixture envelope"))
            .collect();
        let operation_id = envelopes[0].operation_id.as_str().to_string();
        let journal: Arc<dyn KernelJournal> = Arc::new(InMemoryKernelJournal::new());
        let writer =
            CanonicalKernelHost::new(CanonicalKernel::default(), journal.clone(), &operation_id)
                .expect("writer");
        for envelope in envelopes {
            writer.transition(envelope).await.expect("transition");
        }

        let mut restored = CanonicalRunnerRuntime::new(
            CanonicalKernel::default(),
            journal,
            operation_id,
            test_options(),
        )
        .expect("restored runtime");
        restored.restore().await.expect("restore");

        assert_eq!(restored.turn(), 1);
        assert!(
            restored
                .drain_new_messages()
                .iter()
                .any(|message| message.role == deepstrike_core::types::message::Role::Assistant),
            "restart must project messages from canonical context state"
        );
    }

    #[tokio::test]
    async fn load_payload_has_a_dedicated_host_action_after_restore() {
        let fixture: serde_json::Value = serde_json::from_str(include_str!(
            "../../../tests/fixtures/kernel-wire/golden_lifecycle_external_payload.json"
        ))
        .expect("fixture");
        let envelopes: Vec<WireEnvelope> = fixture["links"]
            .as_array()
            .expect("fixture links")
            .iter()
            .take(5)
            .map(|link| serde_json::from_value(link["envelope"].clone()).expect("fixture envelope"))
            .collect();
        let operation_id = envelopes[0].operation_id.as_str().to_string();
        let journal: Arc<dyn KernelJournal> = Arc::new(InMemoryKernelJournal::new());
        let writer =
            CanonicalKernelHost::new(CanonicalKernel::default(), journal.clone(), &operation_id)
                .expect("writer");
        for envelope in envelopes {
            writer.transition(envelope).await.expect("transition");
        }

        let mut restored = CanonicalRunnerRuntime::new(
            CanonicalKernel::default(),
            journal,
            operation_id,
            test_options(),
        )
        .expect("restored runtime");
        restored.restore().await.expect("restore");
        let action = restored
            .resume_action()
            .expect("projection")
            .expect("action");
        assert!(matches!(
            &action.effect,
            HostEffect::LoadPayload {
                handle_id,
                payload_ref,
            } if handle_id == "call-1" && payload_ref == "payload:01J8Y2QK7C4N0V"
        ));
        restored
            .apply_host_event(json!({
                "kind": "payload_loaded",
                "effect_id": action.effect_id,
                "handle_id": "call-1",
                "content": "the full report body, far larger than this operation keeps resident, repeated so it clears the inline threshold by a comfortable margin",
                "digest": "sha256:720fdd2a3796213072f120b7217adf73b7cc85a39f2d6dffdd605f9945a6de2a",
                "original_size": 135,
            }))
            .await
            .expect("payload resolution");
        assert!(
            restored
                .host
                .pending_effects()
                .iter()
                .all(|effect| !matches!(&effect.effect, EffectKind::LoadPayload(_)))
        );
    }

    /* ------------------------------------------------------------ *
     * Durable restart recovery — rust restart-equivalence (0.2.65 S2)
     * ------------------------------------------------------------ */

    /// Rust runs the REAL canonical kernel over a **file-backed** journal, so the restart
    /// ladder here is the honest two-instance form: every phase reopens the directory with a
    /// fresh `FileSessionLog` + `FileKernelJournal`, the way a restarted process would. The
    /// crash-window projections (CAS conflict during append, staged-envelope drain on wake)
    /// are proven by the node/python FileKernelJournal suites over the same host protocol.
    ///
    /// Equivalence criterion: the recovered run's next step is the step the original input
    /// determined — the pending effect survives the restart at its exact chain position —
    /// and the run reaches the same terminal an uninterrupted twin reaches, with each effect
    /// executed exactly once across the restart.

    const RESTART_RUN_ID: &str = "rust-restart-op-1";
    const RESTART_SESSION: &str = "durable-restart";
    const RESTART_FINAL_TEXT: &str = "restart-equivalent-finish";

    /// Streams the ping tool call until history holds a tool result, then the final text —
    /// restart-safe by construction: the branch reads the durable history, never a live
    /// counter, so a resumed process cannot re-emit the tool call.
    struct PingThenFinishProvider;

    #[async_trait::async_trait]
    impl crate::providers::LLMProvider for PingThenFinishProvider {
        async fn complete(
            &self,
            context: &deepstrike_core::context::renderer::RenderedContext,
            _tools: &[deepstrike_core::types::message::ToolSchema],
            _extensions: Option<&serde_json::Value>,
        ) -> crate::Result<deepstrike_core::types::message::Message> {
            use deepstrike_core::types::message::{Content, Message, Role, ToolCall};
            let has_tool_result = context.turns.iter().any(|message| message.role == Role::Tool);
            let (content, tool_calls) = if has_tool_result {
                (RESTART_FINAL_TEXT.to_string(), vec![])
            } else {
                (
                    "Let's ping".to_string(),
                    vec![ToolCall {
                        id: compact_str::CompactString::new("call_ping"),
                        name: compact_str::CompactString::new("ping"),
                        arguments: serde_json::json!({}),
                    }],
                )
            };
            Ok(Message {
                role: Role::Assistant,
                content: Content::Text(content),
                tool_calls,
                token_count: None,
            })
        }

        async fn stream(
            &self,
            context: &deepstrike_core::context::renderer::RenderedContext,
            tools: &[deepstrike_core::types::message::ToolSchema],
            extensions: Option<&serde_json::Value>,
            _state: Option<&crate::providers::ProviderRunState>,
        ) -> crate::Result<
            Box<dyn futures::Stream<Item = crate::Result<crate::providers::StreamEvent>> + Send + Unpin>,
        > {
            use crate::providers::StreamEvent;
            use deepstrike_core::types::message::Content;
            let message = self.complete(context, tools, extensions).await?;
            let mut events = vec![];
            for call in &message.tool_calls {
                events.push(Ok(StreamEvent::ToolCall {
                    id: call.id.to_string(),
                    name: call.name.to_string(),
                    arguments: call.arguments.clone(),
                }));
            }
            if message.tool_calls.is_empty() {
                if let Content::Text(text) = &message.content {
                    events.push(Ok(StreamEvent::TextDelta { delta: text.clone() }));
                }
            }
            events.push(Ok(StreamEvent::Done));
            Ok(Box::new(futures::stream::iter(events)))
        }
    }

    /// Unique per-test directory so concurrent restart ladders never share durable state.
    fn restart_dir(tag: &str) -> std::path::PathBuf {
        let dir = std::env::temp_dir().join(format!(
            "ds-restart-{tag}-{}-{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .expect("clock")
                .as_nanos()
        ));
        std::fs::create_dir_all(&dir).expect("temp dir");
        dir
    }

    fn ping_tool(executions: std::sync::Arc<std::sync::atomic::AtomicU32>) -> crate::tools::RegisteredTool {
        crate::tools::RegisteredTool::text(
            "ping",
            "Ping",
            serde_json::json!({ "type": "object", "properties": {} }),
            move |_args| {
                executions.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                Box::pin(async { Ok("pong".to_string()) })
            },
        )
    }

    fn restart_kernel_options() -> CanonicalRunnerOptions {
        CanonicalRunnerOptions {
            max_context_tokens: 8_000,
            max_turns: Some(8),
            max_total_tokens: None,
            max_wall_ms: None,
            memory_binding_id: "restart-memory".into(),
            persist_payload: None,
        }
    }

    /// A fresh runner over the directory — the restarted process. `baseline_tool_ids` carries
    /// the same exposure surface the manual drives seed, or the resumed tool call is denied
    /// instead of executed.
    fn restart_runner(
        dir: &std::path::Path,
        executions: std::sync::Arc<std::sync::atomic::AtomicU32>,
    ) -> crate::runtime::runner::RuntimeRunner {
        use crate::runtime::kernel_journal::FileKernelJournal;
        use crate::runtime::runner::{RuntimeOptions, RuntimeRunner};
        use crate::runtime::session_log::{FileSessionLog, SessionLog};

        let mut plane = crate::runtime::execution_plane::LocalExecutionPlane::new();
        plane.register(ping_tool(executions));
        let session_log: std::sync::Arc<dyn SessionLog> =
            std::sync::Arc::new(FileSessionLog::new(dir));
        let journal: std::sync::Arc<dyn KernelJournal> =
            std::sync::Arc::new(FileKernelJournal::new(dir.join("kernel-journal")));
        RuntimeRunner::new_with_kernel_journal(
            RuntimeOptions {
                provider: Box::new(PingThenFinishProvider),
                execution_plane: Some(Box::new(plane)),
                session_log: Some(session_log),
                compression_store: None,
                payload_store: None,
                kernel_reliability: None,
                session_id: None,
                max_tokens: 8_000,
                max_turns: Some(8),
                timeout_ms: None,
                extensions: None,
                agent_id: None,
                memory_scope: None,
                system_prompt: None,
                initial_memory: vec![],
                skill_dir: None,
                memory_store: None,
                knowledge_source: None,
                signal_source: None,
                governance: None,
                os_profile: None,
                governance_policy: None,
                signal_policy: None,
                scheduler_policy: None,
                resource_quota: None,
                memory_policy: None,
                tokenizer: None,
                enable_plan_tool: None,
                on_tool_suspend: None,
                on_permission_request: None,
                milestone_policy: crate::runtime::MilestonePolicy::AutoPass,
                milestone_contract: None,
                run_spec: None,
                allowed_tool_ids: None,
                baseline_tool_ids: Some(vec!["ping".into()]),
                on_turn_metrics: None,
                stable_core_tool_ids: vec![],
                pre_query_memory: None,
                on_milestone_evaluate: None,
            },
            journal,
        )
    }

    /// Seed the session identity so the wake finds the operation the manual drive committed.
    async fn seed_run_started(
        log: &std::sync::Arc<dyn crate::runtime::session_log::SessionLog>,
    ) {
        log.append(
            RESTART_SESSION,
            deepstrike_core::runtime::session::SessionEvent::RunStarted {
                run_id: RESTART_RUN_ID.to_string(),
                goal: "use ping then finish".to_string(),
                criteria: vec![],
                agent_id: None,
                system_prompt: None,
                attachments: vec![],
            },
        )
        .await
        .expect("seed run_started");
    }

    /// Drive the operation to a pending execute_tool effect — the freeze frame of a run
    /// interrupted between committing the provider turn and executing the requested tool.
    async fn drive_to_pending_tool_effect(runtime: &mut CanonicalRunnerRuntime) {
        runtime
            .apply_host_event(serde_json::json!({
                "kind": "set_tools",
                "tools": [
                    {
                        "name": "ping",
                        "description": "Ping",
                        "parameters": { "type": "object", "properties": {} }
                    }
                ],
            }))
            .await
            .expect("set_tools");
        let first = runtime
            .start_agent_value(
                serde_json::json!({ "goal": "use ping then finish" }),
                Some(serde_json::json!({ "exposure_baseline": ["ping"] })),
            )
            .await
            .expect("start_agent")
            .expect("call_provider action");
        assert!(
            matches!(first.effect, HostEffect::CallProvider { .. }),
            "expected call_provider"
        );
        let pending = runtime
            .apply_host_event(serde_json::json!({
                "kind": "provider_result",
                "effect_id": first.effect_id,
                "message": {
                    "role": "assistant",
                    "content": "",
                    "tool_calls": [{ "id": "call_ping", "name": "ping", "arguments": {} }],
                },
                "stop_reason": "tool_use",
            }))
            .await
            .expect("provider_result")
            .expect("execute_tool action");
        assert!(
            matches!(pending.effect, HostEffect::ExecuteTool { .. }),
            "expected execute_tool"
        );
    }

    /// The journal chain — `JournalEntry` is `PartialEq`, so the bytes compare directly.
    async fn restart_chain(
        journal: &std::sync::Arc<dyn KernelJournal>,
    ) -> Vec<crate::runtime::JournalEntry> {
        journal
            .read_from(RESTART_RUN_ID, 0)
            .await
            .expect("read chain")
    }

    #[tokio::test]
    async fn durable_restart_baseline_fixes_the_terminal() {
        let dir = restart_dir("baseline");
        let executions = std::sync::Arc::new(std::sync::atomic::AtomicU32::new(0));
        let runner = restart_runner(&dir, executions.clone());
        let text = crate::runtime::runner::collect_text(
            runner
                .run_streaming("use ping then finish", &[], None, Some(RESTART_SESSION))
                .await
                .expect("run"),
        )
        .await
        .expect("collect text");
        assert_eq!(text, RESTART_FINAL_TEXT);
        assert_eq!(executions.load(std::sync::atomic::Ordering::SeqCst), 1);
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn durable_restart_journal_only_ladder_resumes_from_journal_bytes() {
        use crate::runtime::canonical_kernel::CanonicalKernel;
        use crate::runtime::kernel_journal::FileKernelJournal;
        use std::sync::atomic::{AtomicU32, Ordering};

        let dir = restart_dir("journal-only");
        let log: std::sync::Arc<dyn crate::runtime::session_log::SessionLog> =
            std::sync::Arc::new(crate::runtime::session_log::FileSessionLog::new(&dir));
        seed_run_started(&log).await;
        let journal: std::sync::Arc<dyn KernelJournal> =
            std::sync::Arc::new(FileKernelJournal::new(dir.join("kernel-journal")));
        {
            let mut runtime = CanonicalRunnerRuntime::new(
                CanonicalKernel::default(),
                journal.clone(),
                RESTART_RUN_ID.to_string(),
                restart_kernel_options(),
            )
            .expect("runtime");
            drive_to_pending_tool_effect(&mut runtime).await;
        }
        let frozen = restart_chain(&journal).await;
        assert!(frozen.len() >= 3, "the frozen chain holds the full prefix");

        // Process restart: a fresh journal instance over the same directory reads the frozen
        // chain byte-identically — the bytes are the whole contract.
        let remounted: std::sync::Arc<dyn KernelJournal> =
            std::sync::Arc::new(FileKernelJournal::new(dir.join("kernel-journal")));
        assert_eq!(restart_chain(&remounted).await, frozen);

        // runner.wake builds a fresh kernel over the journal and resumes the pending effect.
        let executions = std::sync::Arc::new(AtomicU32::new(0));
        let runner = restart_runner(&dir, executions.clone());
        let text = runner.wake(RESTART_SESSION).await.expect("wake");
        assert_eq!(text, RESTART_FINAL_TEXT);
        assert_eq!(executions.load(Ordering::SeqCst), 1);
        let resumed = restart_chain(&remounted).await;
        assert_eq!(&resumed[..frozen.len()], &frozen[..]);
        assert!(resumed.len() > frozen.len());
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn durable_restart_checkpoint_tail_ladder_restores_through_checkpoint() {
        use crate::runtime::canonical_kernel::CanonicalKernel;
        use crate::runtime::kernel_journal::FileKernelJournal;
        use std::sync::atomic::{AtomicU32, Ordering};

        let dir = restart_dir("checkpoint-tail");
        let log: std::sync::Arc<dyn crate::runtime::session_log::SessionLog> =
            std::sync::Arc::new(crate::runtime::session_log::FileSessionLog::new(&dir));
        seed_run_started(&log).await;
        let journal: std::sync::Arc<dyn KernelJournal> =
            std::sync::Arc::new(FileKernelJournal::new(dir.join("kernel-journal")));
        let (installed, retained) = {
            let mut runtime = CanonicalRunnerRuntime::new(
                CanonicalKernel::default(),
                journal.clone(),
                RESTART_RUN_ID.to_string(),
                restart_kernel_options(),
            )
            .expect("runtime");
            drive_to_pending_tool_effect(&mut runtime).await;
            let frozen = restart_chain(&journal).await;

            // The §12.3 boundary — install, ack, reclaim — runs before the process dies. The
            // pending tool effect lives inside the checkpoint; the covered prefix is reclaimed.
            let installed = runtime.host.checkpoint().await.expect("checkpoint");
            assert!(installed.acknowledged);
            let retained = restart_chain(&journal).await;
            assert!(
                retained.len() < frozen.len(),
                "the covered prefix is reclaimed"
            );
            (installed, retained)
        };
        assert!(
            journal
                .latest_checkpoint(RESTART_RUN_ID)
                .await
                .expect("latest checkpoint")
                .expect("installed checkpoint")
                .acknowledged
        );

        // The wake restore takes the checkpoint+tail ladder: latest_checkpoint + records_after(
        // covered_head). The reclaimed prefix never returns; the run continues past it.
        let executions = std::sync::Arc::new(AtomicU32::new(0));
        let runner = restart_runner(&dir, executions.clone());
        let text = runner.wake(RESTART_SESSION).await.expect("wake");
        assert_eq!(text, RESTART_FINAL_TEXT);
        assert_eq!(executions.load(Ordering::SeqCst), 1);
        let resumed = restart_chain(&journal).await;
        assert_eq!(&resumed[..retained.len()], &retained[..]);
        assert!(
            resumed
                .iter()
                .all(|entry| entry.step_seq > installed.through_step_seq)
        );
        let _ = std::fs::remove_dir_all(&dir);
    }
}