pointlock-runner 0.1.3

The Pointlock execution engine: state machine, verdict fold, crash-safe resume alignment, and localized repair.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
//! The M2 execution engine: tree-walking sequential execution of the
//! control-flow step vocabulary (action/call/if/foreach/let/assert) with
//! the spine §6.1/§6.2 event-order discipline.
//!
//! Ledger discipline per action step (verbatim order, spine §6.1 M1 note):
//! ready (argument snapshot frozen) → `stepEntered` (carries the step's
//! effect/judge hashes and the resolved-inputs snapshot) → probing
//! (declared `preflight` only: fresh observe material → `preflightProbed`)
//! → `actionIntent` (own transaction, fsynced *before* dispatch) →
//! `provider.execute` → `actionSettled` → error classification (spine §5)
//! → `observationRecorded` (evidence localized first, file-before-row-
//! before-log) → `assertionEvaluated` per assertion → `verdictRecorded`
//! (when a verdict exists) + `ProviderSession::record_verdict` write-back →
//! `stepExited` (carries the projected output, when one exists).
//!
//! Control-flow steps (M2):
//! - `call` (07 §1): call-by-value in both directions — inputs evaluated in
//!   the caller scope, snapshotted and schema-gated inbound; the callee
//!   body runs in a fresh frame (`params` = inputs, `env` read-only
//!   pass-through, the caller's steps/vars invisible — 07 §1.2 verbatim);
//!   declared outputs evaluated in the callee scope and schema-gated
//!   outbound; `callFramePushed`/`callFramePopped` bracket the frame; the
//!   call step's verdict *is* the callee's flow verdict (spine §6.3).
//! - `if`: strict-boolean `cond`; the unselected branch's steps each leave
//!   an `entered(resolvedInputs: null)`/`exited(skipped)` pair (the
//!   blocked precedent — ledger completeness); containers yield no verdict
//!   of their own (R4).
//! - `foreach`: `items` must evaluate to an array; each round runs the body
//!   under an `iteration` path frame (`[i]`) with `iter.<as>` bound; the
//!   `stepEntered` snapshot carries `{ items, as }` (the fold's IterState
//!   carrier and the resume-time position authority — 07 §4.6).
//! - `let`: pure bindings into the frame's `vars.*` (SSA; rebinding is a
//!   compiler-refused shape — the runtime check is a defense line).
//! - `assert`: `observe: "fresh"` captures via `session.observe` and goes
//!   through the same localization as action observations; `fromStep`
//!   replays the archived material of a prior action step — zero device
//!   I/O.
//!
//! Evidence-localization degradation (M2): a failure to localize
//! (`fetch_evidence` unsupported, stream rupture, integrity mismatch,
//! `ui.snapshot.get` failure) never aborts the run — the observation
//! record keeps the affected field absent and the dependent verify channel
//! receives a typed gap, degrading honestly toward `unknown` (principle 4).
//!
//! The stop token is honored at step boundaries at any depth
//! (`runSuspended` → [`RunOutcome::Suspended`]); suspension leaves the
//! open spans and live frames in place, and resume walks back into the
//! exact frame position (07 §4.6) by adopting completed step instances
//! path-by-path.

use std::collections::{BTreeMap, BTreeSet};
use std::fmt::Write as _;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};

use futures_util::StreamExt;
use futures_util::future::LocalBoxFuture;
use pointlock_expr::Scope;
use pointlock_ir::{
    ActionExecution, ActionOutcome, ActionResult, ActionStepIR, AssertStepIR, AssertionIR,
    AssetRef, BoundAttempt, CallFrame, CallStepIR, EffectClassAction, ErrorClass, ErrorInfo,
    EvidenceRef, ExecutionMode, FlowIR, ForeachStepIR, HandlerAction, HandlerBinding, HandlerHook,
    HumanMode, HumanPending, HumanPurpose, HumanStepIR, IfStepIR, LetStepIR, Observation,
    ObservationRecord, ObservationSource, ParamDecl, PathFrame, Phase, PredicateIR,
    ProviderStateSummary, RetryPolicy, RunLogPayload, RunPath, StepBase, StepIR, StepId,
    StepRecord, StepState, SupervisePolicy, UiSnapshotOmissionReason, Verdict, VerdictStatus,
    VerifyChannel, render_run_path, to_canonical_json,
};
use pointlock_provider_kit::{
    BoundActionCall, CancellationToken, ObserveRequest, ObserveWant, ProviderSession,
    SessionOutcome, UiSnapshotOutcome, VERDICT_EVIDENCE_MAX_ENTRIES, VERDICT_SUMMARY_MAX_CHARS,
    VerdictWrite,
};
use pointlock_store::Store;
use pointlock_vision::VisionVerifier;
use serde_json::{Map, Value};

use crate::error::{BlockedReason, RunnerError};
use crate::judge::{
    FoldedVerdict, eval_expr_assertion, fold_flow_verdict, fold_step_verdict, project_output,
};
use crate::load::{LoadedFlow, MAX_CALL_DEPTH};
use crate::observe_eval::{
    EvaluatedAssertion, ObserveMaterial, eval_observed_assertion, material_from_observation,
};

/// Terminal outcome of `Runner::run` / `Runner::resume`.
#[derive(Debug, Clone, PartialEq)]
pub enum RunOutcome {
    /// The run reached `runFinished`. `verdict` is the folded flow verdict;
    /// absent when no step produced a verdict (all-unverified flows) or
    /// when the run was aborted by a `cancelled` action terminal.
    Finished {
        /// The folded flow verdict, when one exists.
        verdict: Option<Verdict>,
    },
    /// The run reached `runSuspended` (stop token at a step boundary, or a
    /// provider error left an action without a terminal — resume
    /// reconciles it).
    Suspended,
    /// The run cannot proceed without a human decision: a drifted
    /// preflight whose `onResumeDrift` ladder is exhausted (or absent), or
    /// a reconcile adjudication that could not even be requested (defense
    /// line). The store records a `runSuspended` with the reason.
    Blocked {
        /// Why a human is required.
        reason: BlockedReason,
    },
    /// A human interaction (human step or R13 supervision gate) is
    /// pending: `humanRequested` is fsynced and `runSuspended` recorded —
    /// the runner never blocks waiting. The attached-TTY inline experience
    /// lives in the CLI layer (collect a response through the store
    /// arbitration, then resume in the same process); resume settles the
    /// paired response, re-awaits an unanswered one, or lazily settles an
    /// expired deadline to `unknown` (06 §5.3).
    AwaitingHuman {
        /// The pending request (also materialized in
        /// `CheckpointView.humanPending`).
        pending: HumanPending,
    },
}

/// Shared deadline of the two live capture RPCs (`health()` +
/// `currentCursor()`, 07 §2.2): capture failures degrade the affected
/// fields — they never block the suspend/exit path.
const SUMMARY_CAPTURE_BUDGET_MS: u64 = 2_000;

/// Captures the failure/suspension-instant provider profile (07 §2.2,
/// incorporated 2026-07-18). Pure forensics: nothing downstream may
/// consume it as a control input. Every failure mode degrades honestly:
/// a failed `health()` records `{ ok: false, degraded: <class> }`, a
/// failed `currentCursor()` leaves the cursor absent (never a stale
/// bind-time value — principle 4).
pub(crate) async fn capture_provider_state_summary(
    session: &dyn ProviderSession,
    known_lineage: &[String],
    device_id: &str,
    platform: Option<&str>,
) -> ProviderStateSummary {
    let budget = std::time::Duration::from_millis(SUMMARY_CAPTURE_BUDGET_MS);
    let started = std::time::Instant::now();
    let health = match tokio::time::timeout(budget, session.health()).await {
        Ok(Ok(health)) => pointlock_ir::SessionHealthSnapshot {
            ok: health.ok,
            degraded: health.degraded,
        },
        Ok(Err(error)) => pointlock_ir::SessionHealthSnapshot {
            ok: false,
            degraded: Some(
                serde_json::to_value(error.error_class)
                    .ok()
                    .and_then(|value| value.as_str().map(str::to_owned))
                    .unwrap_or_else(|| "unknown".to_owned()),
            ),
        },
        Err(_) => pointlock_ir::SessionHealthSnapshot {
            ok: false,
            degraded: Some("capture_timeout".to_owned()),
        },
    };
    let remaining = budget.saturating_sub(started.elapsed());
    let event_cursor = match tokio::time::timeout(remaining, session.current_cursor()).await {
        Ok(Ok(cursor)) => Some(cursor),
        _ => None,
    };
    let attestation = session.attestation();
    let mut session_lineage = known_lineage.to_vec();
    if let Some(cursor) = &event_cursor
        && session_lineage.last() != Some(&cursor.session_id)
    {
        session_lineage.push(cursor.session_id.clone());
    }
    ProviderStateSummary {
        session_lineage,
        event_cursor,
        attestation: pointlock_ir::AttestationSnapshot {
            lockfile_digest: attestation.lockfile_digest.clone(),
            attested_at: attestation.attested_at.clone(),
        },
        health,
        device_id: device_id.to_owned(),
        platform: platform.map(str::to_owned),
    }
}

/// The manifest of a locally minted human-evidence asset (item ③):
/// localized by construction (put_evidence wrote the bytes before the
/// verdict cites them).
/// The cited/manifest pair of an escalate ruling's verdict (06 §6): the
/// settlement document when the ruling carries one, empty otherwise.
fn escalate_verdict_material(evidence: &Option<AssetRef>) -> (Vec<AssetRef>, EvidenceManifest) {
    match evidence {
        Some(asset) => (vec![asset.clone()], human_manifest(asset)),
        None => (Vec::new(), EvidenceManifest::default()),
    }
}

fn human_manifest(asset: &AssetRef) -> EvidenceManifest {
    EvidenceManifest {
        localized: vec![pointlock_ir::EvidenceRef {
            asset: asset.clone(),
            sha256: asset.sha256.clone().unwrap_or_default(),
            local_path: asset.uri.clone(),
        }],
        gaps: Vec::new(),
    }
}

/// One judgment's settlement-evidence localization outcome (item ③):
/// what landed and what typed-failed. Rides the `verdictRecorded`
/// payload; observation assets are excluded (they ride
/// `observationRecorded`).
#[derive(Debug, Clone, Default)]
pub(crate) struct EvidenceManifest {
    /// Localized copies (evidence table + `localized` payload field).
    pub localized: Vec<pointlock_ir::EvidenceRef>,
    /// Typed failures (`localizationGaps` payload field).
    pub gaps: Vec<pointlock_ir::EvidenceGap>,
}

/// The act-chain re-entry position of a crash-resume (item ②, 07 §1.4:
/// resume lands at the precise position, never restarts the chain): the
/// recorded 1-based `chainIndex` maps to its 0-based enumeration slot;
/// an out-of-range index is a TYPED refusal (never a guessed position —
/// unreachable via the shipped resume rules); a pre-incorporation
/// ledger (no index) falls back to the head — the pre-ruling behavior,
/// honest under principle 4.
pub(crate) fn chain_start(
    chain_index: Option<u32>,
    step: &ActionStepIR,
) -> Result<usize, RunnerError> {
    match chain_index {
        None => Ok(0),
        Some(index) if index >= 1 && ((index - 1) as usize) < step.binding.attempts.len() => {
            Ok((index - 1) as usize)
        }
        Some(index) => Err(RunnerError::M0Unsupported {
            detail: format!(
                "the pending intent's recorded chainIndex {index} does not exist in the \
                 resumed step's binding chain (len {}); refusing to guess a re-entry \
                 position (unreachable through the shipped resume rules — same-IR chains \
                 cannot shrink and effect-dirty repairs never adopt/replay)",
                step.binding.attempts.len()
            ),
        }),
    }
}

/// SPI ingestion quarantine (M3a viewport review): `Viewport.scaleFactor`
/// is the only f64 in the durable event domain, and serde_json writes a
/// non-finite f64 as `null` — a value the ledger would never read back
/// (every later refold/verify/projection of the run fails permanently).
/// A `succeeded` terminal embedding one is a provider contract violation:
/// it is recorded as a *final failure* with a precise code instead — the
/// honest ledger fact ("the provider reported an unpersistable
/// terminal"), taking the ordinary failure path (handlers may escalate)
/// rather than poisoning the ledger or falsifying the observation.
pub(crate) fn quarantine_unpersistable(outcome: ActionOutcome) -> ActionOutcome {
    let poisoned = match &outcome {
        ActionOutcome::Succeeded { result } => result
            .before
            .iter()
            .chain(result.after.iter())
            .find(|observation| !observation.viewport.scale_factor.is_finite()),
        _ => None,
    };
    match poisoned {
        Some(observation) => ActionOutcome::Failed {
            error: ErrorInfo {
                code: "observation_viewport_invalid".to_owned(),
                message: format!(
                    "provider contract violation: observation {} carries a non-finite \
                     viewport scaleFactor, which cannot be persisted",
                    observation.id
                ),
                retryable: false,
                details: None,
            },
        },
        None => outcome,
    }
}

/// Milliseconds since the Unix epoch (informational `atMs` on events).
pub(crate) fn now_ms() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|duration| duration.as_millis() as u64)
        .unwrap_or(0)
}

/// The root run path of a flow (hard rule: flow frames carry the irHash).
pub(crate) fn root_path(flow: &FlowIR) -> RunPath {
    vec![PathFrame::Flow {
        flow_id: flow.flow_id.clone(),
        ir_hash: flow.ir_hash.clone(),
    }]
}

/// Extracts the last attempt number of a run path.
pub(crate) fn attempt_of(path: &RunPath) -> Option<u64> {
    path.iter().rev().find_map(|frame| match frame {
        PathFrame::Attempt { n } => Some(*n),
        _ => None,
    })
}

/// The IR-version-independent identity of a step *instance*: stepId path
/// plus iteration indexes, with hashes and attempt/phase suffixes
/// stripped. Stable across a repair (irHash change), unique within a run
/// (stepIds are flow-unique; iterations disambiguate rounds). Used to key
/// adoption, open spans, and attempt watermarks.
pub(crate) fn instance_key(path: &[PathFrame]) -> String {
    // The per-attempt suffix (attempt/phase/assertion frames) is never
    // part of the instance identity — but only the TRAILING run of such
    // frames is a suffix. Stripping trailing-only (instead of breaking
    // at the first attempt frame) is byte-identical for every path shape
    // the engine produces today, and stops aliasing distinct
    // interior-attempt instances once the ruled attempt-framed call
    // re-entry of 07 §1 lands (interior attempts render as `#n`).
    let trimmed = {
        let mut end = path.len();
        while end > 0
            && matches!(
                path[end - 1],
                PathFrame::Attempt { .. } | PathFrame::Phase { .. } | PathFrame::Assertion { .. }
            )
        {
            end -= 1;
        }
        &path[..end]
    };
    let mut key = String::new();
    for frame in trimmed {
        match frame {
            PathFrame::Flow { .. } => {}
            PathFrame::Step { step_id } => {
                let _ = write!(key, "/{step_id}");
            }
            PathFrame::Call { step_id, .. } => {
                let _ = match step_id {
                    Some(step_id) => write!(key, "/{step_id}"),
                    None => write!(key, "/hook-call"),
                };
            }
            PathFrame::Iteration { index, key: item } => {
                let _ = match item {
                    Some(item) => write!(key, "[{index}:{item}]"),
                    None => write!(key, "[{index}]"),
                };
            }
            PathFrame::Hook { hook, trigger } => {
                let _ = write!(key, "/hook:{hook:?}:{trigger}");
            }
            PathFrame::Attempt { n } => {
                let _ = write!(key, "#{n}");
            }
            PathFrame::Phase { .. } | PathFrame::Assertion { .. } => {}
        }
    }
    key
}

/// The path frame a step contributes below its parent prefix: call steps
/// contribute a `call` frame (one frame, two rendered segments — 07 §2.1),
/// every other kind a plain `step` frame.
pub(crate) fn child_frame(step: &StepIR) -> PathFrame {
    match step {
        StepIR::Call(call) => PathFrame::Call {
            step_id: Some(call.base.step_id.clone()),
            callee_flow_id: call.flow_ref.flow_id.clone(),
            callee_ir_hash: call.flow_ref.ir_hash.clone(),
        },
        other => PathFrame::Step {
            step_id: other.step_id().clone(),
        },
    }
}

/// Mid-flight work for the frontier step of a resume (07 §4.4 decision
/// table). All variants imply the step's `stepEntered` span is already
/// open in the log — the engine must not re-enter it.
pub(crate) enum FrontierWork {
    /// `reconcile → completed`: adopt the archived terminal — append the
    /// `actionSettled` the crash swallowed, then dispose it through the
    /// exact same settled-outcome path as a live execute (§6.7-B).
    Adopt {
        /// The reconciled callId.
        call_id: String,
        /// The run path of the original `actionIntent` (the settle anchors
        /// to the same attempt).
        intent_path: RunPath,
        /// The archived terminal outcome, verbatim.
        outcome: Box<ActionOutcome>,
        /// The archived `argsSnapshot` (never re-evaluated, spine §6.6).
        args: Value,
        /// The intent's recorded 1-based chain position (item ②): the
        /// act chain re-enters HERE, per 07 §1.4's resume-lands-at-the-
        /// precise-position rule. Absent on pre-incorporation ledgers
        /// (falls back to the chain head — the pre-ruling behavior).
        chain_index: Option<u32>,
    },
    /// `reconcile → neverDispatched` (or an authorized uncertain replay):
    /// dispatch again using the archived args snapshot — never
    /// re-evaluated (spine §6.6).
    Replay {
        /// The archived `argsSnapshot` from the pending intent.
        args: Value,
        /// The intent's recorded chain position (see `Adopt.chain_index`):
        /// the replay re-dispatches THIS attempt — a mid-chain crashed
        /// intent's args belong to that attempt, not the chain head.
        chain_index: Option<u32>,
    },
    /// A human `adopt` adjudication of an uncertain reconcile (07 §4.4):
    /// the ruling says the effect stands, so nothing is dispatched; the
    /// step proceeds straight to the observation-confirmation path
    /// ([`ActPhase::Unconfirmed`]) and its assertions verify the ruled
    /// world.
    ConfirmEffect {
        /// The adjudication context, human-readable.
        message: String,
        /// The archived ready snapshot (the span is open; never
        /// re-evaluated).
        args: Value,
    },
    /// A human `abort` adjudication of an uncertain reconcile (07 §4.4):
    /// close the open span `aborted` and abort the run.
    AbortRuled {
        /// The archived ready snapshot.
        args: Value,
    },
}

/// A reconciled frontier terminal whose WAL intent and `actionSettled` are
/// already on record: the act chain consumes it as the first try's settled
/// outcome instead of dispatching — one disposal code path for live and
/// adopted terminals.
struct AdoptedSettle {
    /// The archived terminal outcome.
    outcome: ActionOutcome,
    /// The `seq` of the appended `actionSettled` event (evidence linking).
    settled_seq: u64,
    /// The attempt number of the original intent.
    attempt_n: u64,
}

/// How a step's execution affects the surrounding body walk.
enum Ctl {
    /// Step concluded (pass/unknown/no-verdict); continue with the next.
    Continue,
    /// A fail verdict: halt — the remaining steps of the current body are
    /// recorded `blocked`, and the halt propagates through enclosing
    /// containers and frames (a callee halt folds into the call step's
    /// verdict, spine §6.3).
    HaltFail,
    /// No terminal could be obtained (transport-class provider error) or a
    /// stop was requested: suspend the run; open spans and live frames
    /// stay open for the frame-precise resume (07 §4.6).
    Suspend(String),
    /// A `cancelled` terminal: the step is recorded `aborted` and the run
    /// finishes without a flow verdict (spine §5).
    Abort,
    /// The run cannot proceed without a human (a drifted preflight whose
    /// `onResumeDrift` ladder is exhausted or absent).
    Blocked(BlockedReason),
    /// A human request is pending (human step or supervision gate): the
    /// run suspends (`runSuspended` after the fsynced `humanRequested`)
    /// and surfaces [`RunOutcome::AwaitingHuman`]. Open spans and live
    /// frames stay open, exactly like `Suspend`.
    AwaitHuman(HumanPending),
}

/// What a handler consultation decided for its host step (spine §3: a
/// disposition, never data — R10).
enum Consulted {
    /// No binding matched, or the trigger budget is exhausted: the
    /// natural path stands.
    None,
    /// Re-enter the failing phase under the handler's retry policy
    /// (budget independent of `StepBase.retry` — spine §6.5 mount 2).
    Retry(RetryPolicy),
    /// Record and release: the verdict stands, downstream is not halted.
    Continue,
    /// Abort the run.
    Abort,
    /// An escalate human superseded the host outcome with this status.
    Escalated {
        status: VerdictStatus,
        summary: String,
        /// The canonical settlement evidence document (06 §6) — cited by
        /// the superseding verdict. Absent only on pre-doc paths.
        evidence: Option<AssetRef>,
    },
    /// An escalate human (repairWorld) declared the world repaired:
    /// re-enter the failing phase once.
    Repaired,
    /// An escalate human is pending: suspend awaiting the response.
    Pending(HumanPending),
    /// The repair subflow completed cleanly: re-enter the failing phase.
    RepairDone,
    /// The repair subflow failed: the host's natural path stands (the
    /// repair flow's own verdict records carry the failure detail).
    RepairFailed,
    /// The repair subflow hit a control outcome (suspend/awaiting-human/
    /// blocked): propagate it.
    Propagate(Ctl),
}

/// The hook audit frame (`/hook:<name>:<n>`, 07 §2.1).
fn hook_frame(hook: HandlerHook, trigger: u64) -> PathFrame {
    PathFrame::Hook { hook, trigger }
}

/// The run path of an escalate hook human: host + hook frame + step frame.
fn hook_child_path(
    step_path: &RunPath,
    hook: HandlerHook,
    trigger: u64,
    human: &HumanStepIR,
) -> RunPath {
    let mut path = step_path.clone();
    path.push(hook_frame(hook, trigger));
    path.push(PathFrame::Step {
        step_id: human.base.step_id.clone(),
    });
    path
}

/// Maps an escalate human's arbitrated response to a consultation outcome
/// (the four-mode table of 06 §2.2, narrowed to the escalate context).
fn map_escalate_response(human: &HumanStepIR, response: &Value) -> Consulted {
    let decision = response
        .get("decision")
        .and_then(Value::as_str)
        .unwrap_or_default();
    match human.mode {
        HumanMode::RepairWorld => match decision {
            // 06 §2.1's closed repairWorld vocabulary. In the escalate
            // ladder `done` re-enters the disposition (re-probe / re-act);
            // `cannotRepair` is the human's explicit "the world cannot be
            // brought back" — the run aborts rather than looping on a
            // declared impossibility. The catch-all is defense: the store
            // arbitrates the vocabulary before anything reaches here.
            "done" => Consulted::Repaired,
            _ => Consulted::Abort,
        },
        HumanMode::Confirm => {
            let first = human
                .decisions
                .as_ref()
                .and_then(|labels| labels.first())
                .map(String::as_str);
            let status = if first == Some(decision) {
                VerdictStatus::Pass
            } else {
                VerdictStatus::Fail
            };
            Consulted::Escalated {
                status,
                summary: format!("escalate confirm decision '{decision}' (position-mapped)"),
                evidence: None,
            }
        }
        // Judge (provideInput escalates are refused at load).
        _ => {
            let status = match response.get("status").and_then(Value::as_str) {
                Some("pass") => VerdictStatus::Pass,
                Some("fail") => VerdictStatus::Fail,
                _ => VerdictStatus::Unknown,
            };
            Consulted::Escalated {
                status,
                evidence: None,
                summary: format!(
                    "escalate judge ruling: {}",
                    response
                        .get("status")
                        .and_then(Value::as_str)
                        .unwrap_or("unknown")
                ),
            }
        }
    }
}

/// The facts of one `humanRequested` on the ledger, harvested for resume
/// settlement (the log is the truth, I1). A supervision `suspend` answer
/// is non-final and never fills `final_response` (spine §6.9).
#[derive(Debug, Clone)]
pub(crate) struct HumanRequestFact {
    /// The request id a response must pair with.
    pub request_id: String,
    /// The request's anchor path (the awaiting/gated step).
    pub run_path: RunPath,
    /// Step vs supervision gate.
    pub purpose: HumanPurpose,
    /// Interaction mode (`purpose="step"` only).
    pub mode: Option<HumanMode>,
    /// The prompt shown to the human.
    pub prompt: String,
    /// The materialized presents snapshot (cited by the evidence doc).
    pub presents: Value,
    /// Absolute deadline; absent for supervision requests.
    pub deadline_at_ms: Option<u64>,
    /// The paired final response payload, when one was arbitrated.
    pub final_response: Option<Value>,
    /// Who gave the final response.
    pub final_actor: Option<String>,
}

/// Outcome of the act phase (attempt chain + in-attempt retry).
enum ActPhase {
    /// A `succeeded` terminal.
    Succeeded {
        result: Box<ActionResult>,
        /// Whether the provider reported an execution mode outside the
        /// attempt's whitelist (§6.4 R-degrade).
        degraded: bool,
        /// The `seq` of the `actionSettled` event (evidence linking).
        settled_seq: u64,
        /// The attempt number the terminal settled on.
        attempt_n: u64,
    },
    /// Step fails (final failure, exhausted retries, invalid arguments).
    StepFail { class: ErrorClass, message: String },
    /// Step folds to unknown (timeout without idempotence/retry; session
    /// degradation).
    StepUnknown {
        message: String,
        /// The error class that produced the unknown, when the class still
        /// governs handler selection. `session_degraded` is the one the
        /// spine §5 error table routes to a flow-level `onError` while the
        /// step itself folds to unknown — dropping the class here would
        /// silently send it to `onUnknown` instead, so a declared
        /// `on_error: { error_classes: [session_degraded] }` would never
        /// fire. A non-idempotent timeout keeps `None`: its row prescribes
        /// the unknown path and no error hook.
        error_class: Option<ErrorClass>,
    },
    /// The act's fate is sealed or ruled but its EFFECT is unproven, and
    /// the step declares assertions that can ask the world. Two producers:
    /// a `timedOut` terminal (spine §5 / 07 §4.3 — a recorded timeout is
    /// certain and reconcile never upgrades it; the observe half of 「先
    /// reconcile/observe 确认」 is what remains), and a human `adopt`
    /// adjudication of an uncertain reconcile (07 §4.4 — the ruling says
    /// the effect stands; the step's own assertions verify). The
    /// settlement loop captures a fresh observation and evaluates the
    /// assertions over it: decisive ones conclude pass/fail, indecisive
    /// ones fold to unknown — exactly the path the bare uncertainty took
    /// before.
    Unconfirmed {
        /// What made the effect unprovable, human-readable (prefixes the
        /// verdict summary).
        message: String,
    },
    /// A `cancelled` terminal.
    Aborted,
    /// No terminal (transport-class provider error) — suspend.
    Suspend(String),
}

/// The localized before/after observation records of an executed action
/// step — the material source of `observe: { fromStep }` assert steps.
pub(crate) struct StepObs {
    /// The localized records, in capture order.
    pub observations: Vec<ObservationRecord>,
    /// The before observation's id, when one was captured.
    pub before_id: Option<String>,
    /// The after observation's id, when one was captured.
    pub after_id: Option<String>,
}

/// A completed step instance carried over by resume (adoption by exact
/// instance path — 07 §4.6: completed steps enter as records, never
/// re-execute).
pub(crate) struct Adopted {
    /// The archived record (fold output).
    pub record: StepRecord,
    /// The before observation's id (harvested from `actionSettled`).
    pub before_id: Option<String>,
    /// The after observation's id (harvested from `actionSettled`).
    pub after_id: Option<String>,
}

/// Whether a record is execution history (vs a blocked/skipped accounting
/// pair, which concluded nothing and seeds nothing).
pub(crate) fn is_history(record: &StepRecord) -> bool {
    !record.attempts.is_empty()
        || record.verdict.is_some()
        || record.output.is_some()
        || !record.resolved_inputs.is_null()
}

/// One live execution frame: the root flow or a callee (07 §1.2 — a
/// frame's full execution semantics are determined by
/// `(calleeIrHash, inputsSnapshot, env)`). Scope contents never cross the
/// frame boundary except read-only `env.*`.
pub(crate) struct FrameState<'a> {
    /// The flow executing in this frame.
    pub flow: &'a FlowIR,
    /// The frame's root path (`[flow]` for the root frame; up to and
    /// including the `call` frame for a callee).
    pub base_path: RunPath,
    /// `params.*`: the run params (root) or the gated inputs snapshot.
    pub params: Map<String, Value>,
    /// `vars.*` accumulated by `let` steps (SSA).
    pub vars: BTreeMap<String, Value>,
    /// Live `iter.<as>` bindings, innermost last.
    pub iters: Vec<(String, Value)>,
    /// `steps.<id>.output` of concluded steps in this frame.
    pub outputs: BTreeMap<String, Value>,
    /// `steps.<id>.verdict` of concluded steps in this frame.
    pub verdicts: BTreeMap<String, (VerdictStatus, bool)>,
    /// Every step-instance verdict produced in this frame, in execution
    /// order — the flow-verdict fold input (iteration instances count
    /// individually; callee-internal verdicts fold through their call
    /// step, never leak here).
    pub fold: Vec<(VerdictStatus, bool)>,
    /// Localized observations per executed action step (assert `fromStep`).
    pub observed: BTreeMap<String, StepObs>,
    /// Call depth (root = 1).
    pub depth: usize,
}

impl<'a> FrameState<'a> {
    /// A fresh frame over `flow`.
    pub fn new(
        flow: &'a FlowIR,
        base_path: RunPath,
        params: Map<String, Value>,
        depth: usize,
    ) -> Self {
        FrameState {
            flow,
            base_path,
            params,
            vars: BTreeMap::new(),
            iters: Vec::new(),
            outputs: BTreeMap::new(),
            verdicts: BTreeMap::new(),
            fold: Vec::new(),
            observed: BTreeMap::new(),
            depth,
        }
    }

    /// Materializes the closed evaluation scope of this frame (spine §7):
    /// `params.* / env.* / vars.* / iter.<as> / steps.<id>.*`, plus an
    /// optional self-output binding (raw output for projection, projected
    /// output for assertions — 02 §4.1.1).
    pub fn scope(&self, env: &[(String, Value)], self_binding: Option<(&str, &Value)>) -> Scope {
        let mut scope = Scope::new();
        for (name, value) in &self.params {
            scope.set_param(name.clone(), value.clone());
        }
        for (name, value) in env {
            scope.set_env(name.clone(), value.clone());
        }
        for (name, value) in &self.vars {
            scope.set_var(name.clone(), value.clone());
        }
        for (name, value) in &self.iters {
            scope.set_iter(name.clone(), value.clone());
        }
        for (step_id, output) in &self.outputs {
            scope.set_step_output(step_id.clone(), output.clone());
        }
        for (step_id, (status, _degraded)) in &self.verdicts {
            let status = serde_json::to_value(status).expect("VerdictStatus serializes");
            scope.set_step_verdict(step_id.clone(), status);
        }
        if let Some((step_id, value)) = self_binding {
            scope.set_step_output(step_id.to_owned(), value.clone());
        }
        scope
    }

    fn seed_verdict(&mut self, step_id: &StepId, status: VerdictStatus, degraded: bool) {
        self.verdicts
            .insert(step_id.as_str().to_owned(), (status, degraded));
        self.fold.push((status, degraded));
    }

    /// Replaces the most recently seeded verdict (an escalate handler's
    /// superseding judgment for the step it was consulted on — the host
    /// verdict is by construction the last seeded entry at consultation
    /// time).
    fn reseed_last(&mut self, step_id: &StepId, status: VerdictStatus, degraded: bool) {
        self.verdicts
            .insert(step_id.as_str().to_owned(), (status, degraded));
        self.fold.pop();
        self.fold.push((status, degraded));
    }
}

/// The single-run execution engine. Owns the provider session; borrows the
/// single-writer store (the runner keeps store use single-threaded — async
/// exists only because the SPI is async).
pub(crate) struct Execution<'a> {
    pub flows: &'a LoadedFlow<'a>,
    pub session: Box<dyn ProviderSession>,
    pub store: &'a mut Store,
    pub run_id: String,
    pub stop: CancellationToken,
    /// `env.*` bindings (deviceId / runId / platform): run-constant,
    /// read-only pass-through across every frame (07 §1.2).
    pub env: Vec<(String, Value)>,
    /// Highest attempt number already used per step instance (resume
    /// continues the numbering; empty on a fresh run).
    pub attempt_base: BTreeMap<String, u64>,
    /// Step spans left open by a crash/suspension: instance key → the
    /// archived ready-phase snapshot. Execution re-enters these spans
    /// without a second `stepEntered`, and containers reuse the archived
    /// snapshot instead of re-evaluating (spine §6.6).
    pub open_spans: BTreeMap<String, Value>,
    /// Call frames already pushed (and not popped) by a previous segment:
    /// instance key → the callee `irHash` the open frame currently claims.
    /// Resume must not push them again; when the pin moved under a
    /// down-drill it re-enters them instead (07 §5.2 case (a)).
    pub live_frames: BTreeMap<String, pointlock_ir::Hash>,
    /// Completed step instances to adopt instead of executing, keyed by
    /// instance path.
    pub adoptable: BTreeMap<String, Adopted>,
    /// Reconciled mid-flight work for the frontier step instance.
    pub frontier: Option<(String, FrontierWork)>,
    /// Whether this segment is a RESUME. It decides where the honest
    /// `unprobed` mark belongs (07 §4.2 rule 1): a fresh run never
    /// re-touches a world it stopped watching, so nothing in it is
    /// unprobed.
    pub resumed: bool,
    /// Step ids released through the 07 §5.4 gate this segment. Step 3 of
    /// that rule extends the preflight guard to every one of them, so each
    /// is an `unprobed` site of its own when it declares no probes.
    pub authorized: BTreeSet<String>,
    /// Latch: the segment's re-entry step has been reached. Set the first
    /// time a step gets as far as probing — adopted steps short-circuit
    /// long before, so the first one that arrives here IS 07 §4.2's
    /// 「resume 的首个待执行 step」.
    pub reentry_seen: bool,
    /// The vision verifier for `vision` verify-chain tails. `None` is
    /// equivalent to the stub: the vision channel cannot complete and
    /// reports `"vision verifier not configured"`.
    pub vision: Option<Arc<dyn VisionVerifier>>,
    /// Known session generations (checkpoint lineage; a fresh run seeds
    /// the bind-time session). Best-effort input of the failure-instant
    /// provider profile (07 §2.2).
    pub session_lineage: Vec<String>,
    /// Failure-instant provider profiles captured at verdict time, keyed
    /// by step-instance key; attached to the span's `stepExited` by
    /// `append` (intensional gate by construction) and discarded on a
    /// superseding pass, an aborted follow-up exit, or span re-entry.
    pub pending_summaries: BTreeMap<String, ProviderStateSummary>,
    /// This segment's supervision policy (R13, spine §6.9): per segment,
    /// never inherited. `None` — unsupervised.
    pub supervise: Option<SupervisePolicy>,
    /// Human requests on the ledger, keyed by step-instance key (resume
    /// settlement input; empty on a fresh run).
    pub human: BTreeMap<String, HumanRequestFact>,
    /// Settled terminals on the ledger, keyed by step-instance key: the
    /// re-entry material for open action spans whose act already settled
    /// before a handler-wave suspension (never re-dispatch, I2).
    pub settled: BTreeMap<String, crate::align::SettledFact>,
    /// Recorded verdicts on the ledger, keyed by step-instance key: the
    /// handler-consultation re-entry point on resume.
    pub recorded_verdicts: BTreeMap<String, (VerdictStatus, bool)>,
    /// Handler trigger watermarks ("{instance}|{hook}" → highest trigger
    /// on the ledger): `maxTriggers` counts across segments, never resets.
    pub hook_triggers: BTreeMap<String, u64>,
    /// Injectable wall clock for deadline computation and lazy timeout
    /// settlement; `None` uses the system clock. The settlement *result*
    /// is a pure function of `deadlineAtMs` and response presence — never
    /// of the settlement instant (06 §5.3).
    pub clock: Option<Arc<dyn Fn() -> u64 + Send + Sync>>,
}

impl<'a> Execution<'a> {
    fn append(&mut self, path: &RunPath, payload: &RunLogPayload) -> Result<u64, RunnerError> {
        // The 07 §2.2 attach point: a fail/unknown-verdict span exiting
        // (any exit site — the gate is intensional, not an enumerated
        // list) carries the verdict-instant provider profile. Aborted
        // follow-up exits make no semantic claim and discard it; span
        // re-entry invalidates a stale capture.
        let enriched;
        let payload = match payload {
            RunLogPayload::StepEntered { .. } => {
                self.pending_summaries.remove(&instance_key(path));
                payload
            }
            RunLogPayload::StepExited {
                state,
                output,
                provider_state_summary: None,
                localized,
                localization_gaps,
            } => match (state, self.pending_summaries.remove(&instance_key(path))) {
                (StepState::Aborted, _) | (_, None) => payload,
                (_, Some(summary)) => {
                    enriched = RunLogPayload::StepExited {
                        state: *state,
                        output: output.clone(),
                        provider_state_summary: Some(summary),
                        localized: localized.clone(),
                        localization_gaps: localization_gaps.clone(),
                    };
                    &enriched
                }
            },
            _ => payload,
        };
        Ok(self
            .store
            .append_event(&self.run_id, now_ms(), path, payload)?)
    }

    /// Pre-stashes resume-generation profiles for crash-opened spans a
    /// sync `record_pairs` cascade is about to close: a span whose ledger
    /// verdict is fail/unknown must not exit summary-less just because
    /// its verdict was recorded by a previous segment (07 §2.2 note 3).
    async fn stash_open_span_summaries(&mut self) {
        let keys: Vec<String> = self
            .open_spans
            .keys()
            .filter(|key| {
                matches!(
                    self.recorded_verdicts.get(*key),
                    Some((VerdictStatus::Fail | VerdictStatus::Unknown, _))
                ) && !self.pending_summaries.contains_key(*key)
            })
            .cloned()
            .collect();
        if keys.is_empty() {
            return;
        }
        let summary = self.capture_summary().await;
        for key in keys {
            self.pending_summaries.insert(key, summary.clone());
        }
    }

    /// Captures the provider profile with this run's identity bindings.
    async fn capture_summary(&self) -> ProviderStateSummary {
        let env_str = |key: &str| {
            self.env
                .iter()
                .find(|(name, _)| name == key)
                .and_then(|(_, value)| value.as_str().map(str::to_owned))
        };
        capture_provider_state_summary(
            self.session.as_ref(),
            &self.session_lineage,
            &env_str("deviceId").unwrap_or_default(),
            env_str("platform").as_deref(),
        )
        .await
    }

    /// The wall clock the human-deadline machinery reads (injectable for
    /// tests; event `atMs` stamps stay on the system clock — they are
    /// informational, deadlines are semantics).
    fn now(&self) -> u64 {
        match &self.clock {
            Some(clock) => clock(),
            None => now_ms(),
        }
    }

    /// Runs the root body from `start` and settles the run terminal.
    pub async fn run(
        mut self,
        mut root: FrameState<'a>,
        start: usize,
    ) -> Result<RunOutcome, RunnerError> {
        let flows = self.flows;
        let body: &'a [StepIR] = &flows.root.body;
        let prefix = root.base_path.clone();
        let ctl = self
            .exec_body(&mut root, prefix.clone(), body, start)
            .await?;
        match ctl {
            Ctl::Continue | Ctl::HaltFail => self.finish(false, &root).await,
            Ctl::Abort => self.finish(true, &root).await,
            Ctl::Suspend(reason) => {
                // Suspension-instant profile (07 §2.2): captured while
                // the session is still live, before teardown.
                let summary = self.capture_summary().await;
                self.append(
                    &prefix,
                    &RunLogPayload::RunSuspended {
                        provider_state_summary: Some(summary),
                        reason: Some(reason),
                    },
                )?;
                self.end_session(SessionOutcome::Shutdown).await;
                Ok(RunOutcome::Suspended)
            }
            Ctl::Blocked(reason) => {
                let summary = self.capture_summary().await;
                self.append(
                    &prefix,
                    &RunLogPayload::RunSuspended {
                        provider_state_summary: Some(summary),
                        reason: Some(reason.to_string()),
                    },
                )?;
                self.end_session(SessionOutcome::Shutdown).await;
                Ok(RunOutcome::Blocked { reason })
            }
            Ctl::AwaitHuman(pending) => {
                // The unified wait semantics: `humanRequested` is already
                // fsynced (its append committed); the segment suspends and
                // the process may exit — notification and collection are
                // the CLI layer's job (spine §6.8, 06 §5.1/§5.2). The
                // session is released while waiting; resume opens a new
                // one (session lineage).
                let summary = self.capture_summary().await;
                self.append(
                    &prefix,
                    &RunLogPayload::RunSuspended {
                        provider_state_summary: Some(summary),
                        reason: Some(format!(
                            "awaiting human response (requestId {})",
                            pending.request_id
                        )),
                    },
                )?;
                self.end_session(SessionOutcome::Shutdown).await;
                Ok(RunOutcome::AwaitingHuman { pending })
            }
        }
    }

    /// Folds the root flow verdict, appends `runFinished`, ends the
    /// session.
    async fn finish(
        mut self,
        aborted: bool,
        root: &FrameState<'a>,
    ) -> Result<RunOutcome, RunnerError> {
        let prefix = root.base_path.clone();
        let verdict = if aborted {
            // An aborted run makes no flow-level semantic claim.
            None
        } else {
            fold_flow_verdict(&root.fold, root.flow.verdict_policy).map(|folded| Verdict {
                status: folded.status,
                degraded: folded.degraded,
                summary: folded.summary,
                evidence: Vec::new(),
                supersedes: None,
            })
        };
        let remote_archival_error = match &verdict {
            // Judgment authority is Pointlock's; the daemon only persists
            // (spine §6.3 write-back). Failure is annotation material,
            // never a run error (04 §5).
            Some(verdict) => self.try_verdict_writeback(verdict).await,
            None => None,
        };
        self.append(
            &prefix,
            &RunLogPayload::RunFinished {
                verdict: verdict.clone(),
                remote_archival_error,
            },
        )?;
        let session_outcome = if aborted {
            SessionOutcome::Cancelled
        } else if verdict
            .as_ref()
            .is_some_and(|verdict| verdict.status == VerdictStatus::Fail)
        {
            SessionOutcome::Failed
        } else {
            SessionOutcome::Completed
        };
        self.end_session(session_outcome).await;
        Ok(RunOutcome::Finished { verdict })
    }

    /// Executes one body level sequentially. The stop token is honored
    /// before every step (step boundaries, any depth); a fail halts the
    /// level and records the remaining steps `blocked`.
    async fn exec_body(
        &mut self,
        frame: &mut FrameState<'a>,
        prefix: RunPath,
        body: &'a [StepIR],
        start: usize,
    ) -> Result<Ctl, RunnerError> {
        for (index, step) in body.iter().enumerate().skip(start) {
            if self.stop.is_cancelled() {
                return Ok(Ctl::Suspend("stop requested".to_owned()));
            }
            match self.exec_step(frame, &prefix, step).await? {
                Ctl::Continue => {}
                Ctl::HaltFail => {
                    // Halt-on-fail: remaining steps of this level are
                    // explicitly recorded blocked (never silently dropped
                    // from the ledger).
                    self.stash_open_span_summaries().await;
                    self.record_pairs(&prefix, &body[index + 1..], StepState::Blocked)?;
                    return Ok(Ctl::HaltFail);
                }
                other => return Ok(other),
            }
        }
        Ok(Ctl::Continue)
    }

    /// Executes (or adopts) one step instance. Boxed: the recursion point
    /// of the tree walk (containers and calls re-enter `exec_body`).
    fn exec_step<'s>(
        &'s mut self,
        frame: &'s mut FrameState<'a>,
        prefix: &'s RunPath,
        step: &'a StepIR,
    ) -> LocalBoxFuture<'s, Result<Ctl, RunnerError>>
    where
        'a: 's,
    {
        Box::pin(async move {
            let mut path = prefix.clone();
            path.push(child_frame(step));
            let key = instance_key(&path);
            // Resume adoption (07 §4.6/I2): a concluded instance enters as
            // its record and never re-executes.
            if self
                .adoptable
                .get(&key)
                .is_some_and(|adopted| is_history(&adopted.record))
            {
                let adopted = self.adoptable.remove(&key).expect("checked present");
                self.adopt_step(frame, step, adopted);
                return Ok(Ctl::Continue);
            }
            match step {
                StepIR::Action(s) => self.exec_action(frame, path, s).await,
                StepIR::Call(s) => self.exec_call(frame, path, s).await,
                StepIR::If(s) => self.exec_if(frame, path, s).await,
                StepIR::Foreach(s) => self.exec_foreach(frame, path, s).await,
                StepIR::Let(s) => self.exec_let(frame, path, s).await,
                StepIR::Assert(s) => self.exec_assert(frame, path, s).await,
                StepIR::Human(s) => self.exec_human(frame, path, s).await,
            }
        })
    }

    /// Seeds a frame with an adopted record's effects (outputs / verdicts /
    /// vars / observation material); containers recursively consume their
    /// children's records using the archived control snapshots — never a
    /// re-evaluation (I3).
    fn adopt_step(&mut self, frame: &mut FrameState<'a>, step: &'a StepIR, adopted: Adopted) {
        let id = step.step_id().as_str().to_owned();
        let record = adopted.record;
        match step {
            StepIR::Action(_) => {
                if let Some(verdict) = &record.verdict {
                    frame.seed_verdict(step.step_id(), verdict.status, verdict.degraded);
                }
                if let Some(output) = record.output.clone() {
                    frame.outputs.insert(id.clone(), output);
                }
                frame.observed.insert(
                    id,
                    StepObs {
                        observations: record.observations,
                        before_id: adopted.before_id,
                        after_id: adopted.after_id,
                    },
                );
            }
            StepIR::Assert(_) | StepIR::Call(_) | StepIR::Human(_) => {
                // A settled human step re-enters as its verdict/output
                // (the response was already arbitrated and folded into the
                // record) — never re-asked.
                if let Some(verdict) = &record.verdict {
                    frame.seed_verdict(step.step_id(), verdict.status, verdict.degraded);
                }
                if let Some(output) = record.output.clone() {
                    frame.outputs.insert(id, output);
                }
            }
            StepIR::Let(_) => {
                // The archived ready snapshot *is* the bindings product.
                if let Value::Object(bindings) = record.resolved_inputs {
                    for (name, value) in bindings {
                        frame.vars.insert(name, value);
                    }
                }
            }
            StepIR::If(s) => {
                // Consume both branches: the selected branch's records seed
                // effects, the unselected branch's skipped pairs seed
                // nothing — both leave the adoption set.
                self.adopt_children(frame, &record.run_path, &s.then);
                if let Some(otherwise) = &s.r#else {
                    self.adopt_children(frame, &record.run_path, otherwise);
                }
            }
            StepIR::Foreach(s) => {
                let rounds = record
                    .resolved_inputs
                    .get("items")
                    .and_then(Value::as_array)
                    .map(Vec::len)
                    .unwrap_or(0);
                for index in 0..rounds {
                    let mut prefix = record.run_path.clone();
                    prefix.push(PathFrame::Iteration {
                        index: index as u64,
                        key: None,
                    });
                    self.adopt_children(frame, &prefix, &s.body);
                }
            }
        }
    }

    fn adopt_children(
        &mut self,
        frame: &mut FrameState<'a>,
        prefix: &RunPath,
        steps: &'a [StepIR],
    ) {
        for step in steps {
            let mut path = prefix.clone();
            path.push(child_frame(step));
            let key = instance_key(&path);
            if let Some(adopted) = self.adoptable.remove(&key) {
                self.adopt_step(frame, step, adopted);
            }
        }
    }

    /// Records `entered(resolvedInputs: null)`/`exited(state)` pairs for a
    /// subtree that will not execute (skipped branches, blocked tails) —
    /// ledger completeness per the blocked precedent. Children are handled
    /// before their container so that crash-opened spans close innermost
    /// first (the fold's exit pairing is positional). Instances already on
    /// the ledger from a previous segment are kept, not re-emitted.
    fn record_pairs(
        &mut self,
        prefix: &RunPath,
        steps: &'a [StepIR],
        state: StepState,
    ) -> Result<(), RunnerError> {
        for step in steps {
            let mut path = prefix.clone();
            path.push(child_frame(step));
            match step {
                StepIR::If(s) => {
                    self.record_pairs(&path, &s.then, state)?;
                    if let Some(otherwise) = &s.r#else {
                        self.record_pairs(&path, otherwise, state)?;
                    }
                }
                StepIR::Foreach(s) => self.record_pairs(&path, &s.body, state)?,
                _ => {}
            }
            let key = instance_key(&path);
            if self.adoptable.remove(&key).is_some() {
                continue;
            }
            if self.open_spans.remove(&key).is_some() {
                // A previous segment opened this span; close it with the
                // terminal state instead of double-entering.
                self.append(
                    &path,
                    &RunLogPayload::StepExited {
                        provider_state_summary: None,
                        state,
                        output: None,
                        localized: Vec::new(),
                        localization_gaps: Vec::new(),
                    },
                )?;
                continue;
            }
            self.append(
                &path,
                &RunLogPayload::StepEntered {
                    step_id: step.step_id().clone(),
                    effect_hash: step.base().effect_hash.clone(),
                    judge_hash: step.base().judge_hash.clone(),
                    resolved_inputs: Value::Null,
                },
            )?;
            self.append(
                &path,
                &RunLogPayload::StepExited {
                    provider_state_summary: None,
                    state,
                    output: None,
                    localized: Vec::new(),
                    localization_gaps: Vec::new(),
                },
            )?;
        }
        Ok(())
    }

    /// The archived ready snapshot of a crash/suspension-opened span, when
    /// this instance has one (peek — `enter_step` consumes it).
    fn open_span_inputs(&self, key: &str) -> Option<Value> {
        self.open_spans.get(key).cloned()
    }

    /// Appends `stepEntered` unless the instance's span is already open
    /// (resume: the log has an unmatched `stepEntered`). The payload
    /// carries the step's dual hashes and the frozen ready-phase input
    /// snapshot (spine §6.1 M1 note).
    fn enter_step(
        &mut self,
        path: &RunPath,
        base: &StepBase,
        resolved_inputs: Value,
    ) -> Result<(), RunnerError> {
        let key = instance_key(path);
        if self.open_spans.remove(&key).is_some() {
            return Ok(());
        }
        self.append(
            path,
            &RunLogPayload::StepEntered {
                step_id: base.step_id.clone(),
                effect_hash: base.effect_hash.clone(),
                judge_hash: base.judge_hash.clone(),
                resolved_inputs,
            },
        )?;
        Ok(())
    }

    /// Evaluates one bound attempt's argument expressions against the
    /// frame scope (the ready-phase resolution).
    fn resolve_args(
        &self,
        frame: &FrameState<'a>,
        attempt: &BoundAttempt,
    ) -> Result<Value, String> {
        let scope = frame.scope(&self.env, None);
        let mut evaluated = serde_json::Map::new();
        for (name, expr) in attempt.args.iter() {
            match pointlock_expr::eval(expr, &scope) {
                Ok(value) => {
                    evaluated.insert(name.as_str().to_owned(), value);
                }
                Err(error) => return Err(format!("argument evaluation failed: {error}")),
            }
        }
        Ok(Value::Object(evaluated))
    }

    // ─── probing (spine §6.2; 07 §4.2) ──────────────────────────────────────

    /// Runs a step's declared `preflight`, or records that there was none
    /// to run (07 §4.2 rule 1 / I3).
    ///
    /// 「该步无声明则跳过并在报告标 `unprobed`(诚实优先于安慰)」. The
    /// carrier is a `preflightProbed` with an EMPTY outcome list, which is
    /// unambiguous rather than clever: `preflight` is `minItems: 1` in the
    /// schema, so a declared probe list can never evaluate to zero
    /// outcomes. No new event type, no new payload field, and old ledgers
    /// — which never emitted it — refold byte-identically.
    ///
    /// It is written at exactly the two places the spec names, and nowhere
    /// else. A step in the middle of a continuously-executing run is not
    /// re-touching a world anyone stopped watching, and marking it would
    /// turn an honest signal into noise:
    /// - the segment's re-entry step, when the segment is a resume
    ///   (§4.2 rule 1);
    /// - every step released through the §5.4 gate (step 3: 「本条
    ///   preflight 守护对 `positionalReplay`/`orderInvalidated`/
    ///   `frontierUnknown` 的步同样强制适用」) — those re-execute onto a
    ///   world that carries the earlier effect, which is the whole reason
    ///   they had to be authorized by name.
    async fn probe_or_note(
        &mut self,
        frame: &mut FrameState<'a>,
        path: &RunPath,
        base: &'a StepBase,
    ) -> Result<Option<Ctl>, RunnerError> {
        let reentry = self.resumed && !self.reentry_seen;
        self.reentry_seen = true;
        if let Some(probes) = &base.preflight {
            return self.probe_preflight(frame, path, base, probes).await;
        }
        if reentry || self.authorized.contains(base.step_id.as_str()) {
            let mut probe_path = path.clone();
            probe_path.push(PathFrame::Phase {
                phase: Phase::Preflight,
            });
            self.append(
                &probe_path,
                &RunLogPayload::PreflightProbed {
                    outcomes: Vec::new(),
                },
            )?;
        }
        Ok(None)
    }

    /// Evaluates a step's declared `preflight` probes over fresh observe
    /// material (spine §6.7-C operationalized). A probe that does not hold
    /// — or cannot be evaluated (exhausted chain) — is drift: the step's
    /// `onResumeDrift` ladder is consulted (repair → re-probe, escalate →
    /// `repairWorld`); with none left the run blocks (`drifted` →
    /// `runSuspended`).
    async fn probe_preflight(
        &mut self,
        frame: &mut FrameState<'a>,
        path: &RunPath,
        base: &'a StepBase,
        probes: &'a [AssertionIR],
    ) -> Result<Option<Ctl>, RunnerError> {
        let step_id = &base.step_id;
        let mut probe_path = path.clone();
        probe_path.push(PathFrame::Phase {
            phase: Phase::Preflight,
        });
        let needs = VerifyNeeds::of(probes);
        let mut active_retry: Option<(RetryPolicy, u32)> = None;
        loop {
            let material = self.fresh_material(&needs, &probe_path).await?;
            let scope = frame.scope(&self.env, None);
            let mut outcomes = Vec::with_capacity(probes.len());
            for probe in probes {
                let evaluated = match &probe.predicate {
                    PredicateIR::Expr { expr } => EvaluatedAssertion {
                        record: eval_expr_assertion(probe, expr, &scope),
                        degraded_verify: false,
                    },
                    _ => eval_observed_assertion(probe, &material, self.vision.as_deref()).await,
                };
                outcomes.push(evaluated.record);
            }
            self.append(
                &probe_path,
                &RunLogPayload::PreflightProbed {
                    outcomes: outcomes.clone(),
                },
            )?;
            let Some(missed) = outcomes
                .iter()
                .find(|outcome| outcome.result != VerdictStatus::Pass)
            else {
                return Ok(None);
            };
            // Unable-to-confirm is drift too (07 §4.2 rule 2: not being
            // able to see the world is not the world being fine —
            // principle 4).
            let what = match missed.result {
                VerdictStatus::Fail => "did not hold",
                _ => "could not be evaluated (treated as drift)",
            };
            let detail = format!("probe '{}' {what}: {}", missed.assert_id, missed.reason);

            // In-force drift-handler retry budget: re-probe (readonly).
            if let Some((policy, used)) = active_retry.take()
                && used < policy.max_attempts
            {
                self.backoff_policy(&policy, used).await;
                active_retry = Some((policy, used + 1));
                continue;
            }

            // A failed probe consults `onResumeDrift` (spine §6.2/§6.7-C:
            // probing → drifted → the drift handler; exhausted budgets
            // block awaiting a human decision).
            match self
                .consult_hook(
                    frame,
                    path,
                    base.handlers.as_deref(),
                    HandlerHook::OnResumeDrift,
                    None,
                )
                .await?
            {
                Consulted::None | Consulted::RepairFailed => {
                    return Ok(Some(Ctl::Blocked(BlockedReason::Drifted {
                        step_id: step_id.as_str().to_owned(),
                        detail,
                    })));
                }
                Consulted::Continue => {
                    // The author accepts the drifted world: proceed.
                    return Ok(None);
                }
                Consulted::Abort => return Ok(Some(Ctl::Abort)),
                Consulted::Escalated { status, .. } => match status {
                    // A human judged the world acceptable: proceed.
                    VerdictStatus::Pass => return Ok(None),
                    _ => {
                        return Ok(Some(Ctl::Blocked(BlockedReason::Drifted {
                            step_id: step_id.as_str().to_owned(),
                            detail: format!("{detail}; escalate ruling: not acceptable"),
                        })));
                    }
                },
                Consulted::Retry(policy) => {
                    self.backoff_policy(&policy, 0).await;
                    active_retry = Some((policy, 1));
                }
                // A repaired world (declared or via the repair flow):
                // re-probe — the probe, not the declaration, readmits.
                Consulted::Repaired | Consulted::RepairDone => {}
                Consulted::Pending(pending) => return Ok(Some(Ctl::AwaitHuman(pending))),
                Consulted::Propagate(ctl) => return Ok(Some(ctl)),
            }
        }
    }

    /// Captures a fresh observation (`session.observe`) sized to the
    /// declared verify needs, localizes it (`observationRecorded`), and
    /// returns the verify-chain material. An observe failure is a typed
    /// material gap, never a run abort — the dependent assertions degrade
    /// toward unknown.
    async fn fresh_material(
        &mut self,
        needs: &VerifyNeeds,
        anchor: &RunPath,
    ) -> Result<ObserveMaterial, RunnerError> {
        let mut wants = Vec::new();
        if needs.ui_tree {
            wants.push(ObserveWant::UiSnapshot);
        }
        if needs.vision {
            wants.push(ObserveWant::Screenshot);
        }
        if wants.is_empty() {
            // Expr-only consumers need no observation channel.
            return Ok(ObserveMaterial::default());
        }
        let observation = match self.session.observe(ObserveRequest { wants }, None).await {
            Ok(observation) => observation,
            Err(error) => {
                return Ok(ObserveMaterial::absent(&format!(
                    "fresh observation failed: {error}"
                )));
            }
        };
        let mut cited = Vec::new();
        let mut material = ObserveMaterial::default();
        let record = self
            .localize_observation(&observation, &mut cited, Some((needs, &mut material)))
            .await?;
        self.append(
            anchor,
            &RunLogPayload::ObservationRecorded {
                observation: record,
            },
        )?;
        Ok(material)
    }

    // ─── action steps ───────────────────────────────────────────────────────

    async fn exec_action(
        &mut self,
        frame: &mut FrameState<'a>,
        step_path: RunPath,
        step: &'a ActionStepIR,
    ) -> Result<Ctl, RunnerError> {
        let step_id = step.base.step_id.clone();
        let key = instance_key(&step_path);
        let work = match &self.frontier {
            Some((frontier_key, _)) if *frontier_key == key => {
                self.frontier.take().map(|(_, work)| work)
            }
            _ => None,
        };

        // Ready precedes entered (spine §6.1 M1 note): the first bound
        // attempt's argument expressions are resolved once and frozen;
        // `stepEntered` carries the snapshot and lands before any
        // preflight probe or `actionIntent`. Frontier work reuses the
        // archived snapshot verbatim — never re-evaluated (spine §6.6).
        let resolved = match &work {
            Some(FrontierWork::Adopt { args, .. })
            | Some(FrontierWork::Replay { args, .. })
            | Some(FrontierWork::ConfirmEffect { args, .. })
            | Some(FrontierWork::AbortRuled { args }) => Ok(args.clone()),
            None => {
                let attempt = step
                    .binding
                    .attempts
                    .first()
                    .expect("sealed action steps carry at least one bound attempt");
                self.resolve_args(frame, attempt)
            }
        };
        let resolved_inputs = match resolved {
            Ok(args) => args,
            Err(message) => {
                // Inputs never resolved: the span still opens and closes
                // (one entered/exited pair per step), with
                // `resolvedInputs: null` — a failing argument evaluation
                // is a compiler/expression bug signal
                // (bind_arguments_invalid discipline): step fails, no
                // retry.
                self.enter_step(&step_path, &step.base, Value::Null)?;
                return self
                    .settle_error(
                        frame,
                        &step_path,
                        &step_id,
                        VerdictStatus::Fail,
                        format!("act phase failed [bind_arguments_invalid]: {message}"),
                    )
                    .await;
            }
        };
        let had_open_span = self.open_span_inputs(&key).is_some();
        self.enter_step(&step_path, &step.base, resolved_inputs.clone())?;

        // Handler-wave resume re-entry (I2): an open span whose act
        // already settled and whose verdict is on the ledger means a
        // previous segment suspended mid-handler (a pending escalate).
        // Never re-dispatch — enter the disposition loop directly from
        // the recorded ruling.
        let resume_ruling = if work.is_none() && had_open_span {
            match (self.settled.get(&key), self.recorded_verdicts.get(&key)) {
                (Some(_), Some(ruling)) => Some(*ruling),
                _ => None,
            }
        } else {
            None
        };

        // Probing (§6.2): declared preflight evaluates between entered and
        // acting. An adopted frontier terminal (or a handler-wave
        // re-entry) means the act already left in a previous life —
        // probing "is the world ready for the act" after the act is
        // meaningless, so it is skipped there.
        if !matches!(work, Some(FrontierWork::Adopt { .. }))
            && resume_ruling.is_none()
            && let Some(ctl) = self.probe_or_note(frame, &step_path, &step.base).await?
        {
            return Ok(ctl);
        }

        // R13 supervision gate (spine §6.9): sits wholly before the
        // `actionIntent` WAL — a refused act never had an intent on the
        // ledger. Adopt/Replay frontier work is never re-gated: the act
        // already happened, or its intent was WAL-authorized by a
        // previous segment.
        if work.is_none()
            && resume_ruling.is_none()
            && let Some(ctl) = self.gate_supervision(&step_path, step, &resolved_inputs)?
        {
            return Ok(ctl);
        }

        let mut acted = if resume_ruling.is_some() {
            None
        } else {
            Some(match work {
                Some(FrontierWork::Adopt {
                    call_id,
                    intent_path,
                    outcome,
                    args,
                    chain_index,
                }) => {
                    // Record the terminal the crash swallowed; this settles
                    // the pending intent in the checkpoint fold.
                    let attempt_n = attempt_of(&intent_path).unwrap_or(1);
                    let outcome = quarantine_unpersistable(*outcome);
                    let settled_seq = self.append(
                        &intent_path,
                        &RunLogPayload::ActionSettled {
                            call_id,
                            outcome: outcome.clone(),
                        },
                    )?;
                    // From here the adopted terminal takes the exact same
                    // settled-outcome path as a live one.
                    let adopted = AdoptedSettle {
                        outcome,
                        settled_seq,
                        attempt_n,
                    };
                    self.act_chain(
                        frame,
                        step,
                        &step_path,
                        Some(args),
                        Some(adopted),
                        chain_start(chain_index, step)?,
                    )
                    .await?
                }
                Some(FrontierWork::Replay { args, chain_index }) => {
                    self.act_chain(
                        frame,
                        step,
                        &step_path,
                        Some(args),
                        None,
                        chain_start(chain_index, step)?,
                    )
                    .await?
                }
                Some(FrontierWork::ConfirmEffect { message, .. }) => {
                    // No dispatch: the adjudication already ruled on the
                    // act; only the world's testimony is still owed.
                    ActPhase::Unconfirmed { message }
                }
                Some(FrontierWork::AbortRuled { .. }) => {
                    // The ruled abort mirrors a `cancelled` terminal's
                    // unwind: the open span closes `aborted` and the run
                    // makes no further semantic claim.
                    ActPhase::Aborted
                }
                // The fresh path hands the ready snapshot to the first
                // chain attempt — resolved exactly once, above.
                None => {
                    self.act_chain(
                        frame,
                        step,
                        &step_path,
                        Some(resolved_inputs.clone()),
                        None,
                        0,
                    )
                    .await?
                }
            })
        };

        // ── the settlement/disposition loop (M2 W3) ─────────────────────
        //
        // One round = one settled act (or the resumed recorded ruling)
        // judged and, on fail/unknown, consulted against the handlers.
        // Retry-class dispositions re-enter the act with the frozen
        // snapshot (new callId, new WAL intent); every re-fold records a
        // new verdict superseding the previous one (spine §2 concept 12).
        let mut last_verdict_seq: Option<u64> = None;
        let mut seeded = false;
        let mut active_retry: Option<(RetryPolicy, u32)> = None;
        let mut ruling = resume_ruling;
        loop {
            // What this round established: (status, degraded, summary,
            // cited evidence, projected output, error-path class).
            let (status, degraded, summary, cited, projected, error_class): (
                Option<VerdictStatus>,
                bool,
                String,
                Vec<AssetRef>,
                Option<Value>,
                Option<ErrorClass>,
            );
            let mut round_manifest = EvidenceManifest::default();
            let mut ruled_from_ledger = false;
            match (acted.take(), ruling.take()) {
                (None, Some((recorded_status, recorded_degraded))) => {
                    // Resumed at the recorded verdict: derive the output
                    // projection from the archived succeeded terminal so
                    // downstream refs keep working (pure re-projection).
                    ruled_from_ledger = true;
                    let recovered = match self.settled.get(&key).map(|fact| &fact.outcome) {
                        Some(ActionOutcome::Succeeded { result }) => {
                            let raw_scope =
                                frame.scope(&self.env, Some((step_id.as_str(), &result.output)));
                            project_output(step, &result.output, &raw_scope).ok()
                        }
                        _ => None,
                    };
                    status = Some(recorded_status);
                    degraded = recorded_degraded;
                    summary = "resumed at the recorded verdict (handler re-entry)".to_owned();
                    cited = Vec::new();
                    projected = recovered;
                    error_class = None;
                    // Cross-segment gate (07 §2.2 note 3): the previous
                    // segment's verdict is in force but its stash died
                    // with the process. Capture the RESUME-generation
                    // profile so the eventual exit still carries one —
                    // self-describing via its own sessionLineage/cursor;
                    // the failure-instant profile rides the prior
                    // segment's runSuspended.
                    if matches!(
                        recorded_status,
                        VerdictStatus::Fail | VerdictStatus::Unknown
                    ) {
                        let captured = self.capture_summary().await;
                        self.pending_summaries.insert(key.clone(), captured);
                    }
                }
                (Some(phase), _) => match phase {
                    ActPhase::Succeeded {
                        result,
                        degraded: degraded_execution,
                        settled_seq,
                        attempt_n,
                    } => {
                        let (round_cited, material, observed, manifest) = self
                            .observing(step, &step_path, attempt_n, &result, settled_seq)
                            .await?;
                        round_manifest = manifest;
                        frame.observed.insert(step_id.as_str().to_owned(), observed);
                        // Output projection (self-refs see the raw output).
                        let raw_scope =
                            frame.scope(&self.env, Some((step_id.as_str(), &result.output)));
                        let round_projected = match project_output(step, &result.output, &raw_scope)
                        {
                            Ok(value) => value,
                            Err(error) => {
                                // A failing output projection is a
                                // compiler/expression bug signal: step
                                // fails, no retry, no consultation
                                // (bind_arguments_invalid discipline).
                                return self
                                    .settle_error(
                                        frame,
                                        &step_path,
                                        &step_id,
                                        VerdictStatus::Fail,
                                        format!("output projection failed: {error}"),
                                    )
                                    .await;
                            }
                        };
                        // Asserting: pure computation over materialized
                        // values (the vision tail is the one declared
                        // exception to purity).
                        let assert_scope =
                            frame.scope(&self.env, Some((step_id.as_str(), &round_projected)));
                        let mut outcomes = Vec::with_capacity(step.assertions.len());
                        let mut degraded_verify = false;
                        for assertion in &step.assertions {
                            let evaluated = match &assertion.predicate {
                                PredicateIR::Expr { expr } => EvaluatedAssertion {
                                    record: eval_expr_assertion(assertion, expr, &assert_scope),
                                    degraded_verify: false,
                                },
                                _ => {
                                    eval_observed_assertion(
                                        assertion,
                                        &material,
                                        self.vision.as_deref(),
                                    )
                                    .await
                                }
                            };
                            degraded_verify |= evaluated.degraded_verify;
                            outcomes.push(evaluated.record);
                        }
                        for outcome in &outcomes {
                            let mut path = step_path.clone();
                            path.push(PathFrame::Phase {
                                phase: Phase::Assert,
                            });
                            path.push(PathFrame::Assertion {
                                assert_id: outcome.assert_id.clone(),
                            });
                            self.append(
                                &path,
                                &RunLogPayload::AssertionEvaluated {
                                    outcome: outcome.clone(),
                                },
                            )?;
                        }
                        if outcomes.is_empty() {
                            // No assertions ⇒ no verdict (spine R4):
                            // execution status only (`unverified`).
                            status = None;
                            degraded = false;
                            summary = String::new();
                        } else {
                            let folded = fold_step_verdict(
                                &outcomes,
                                degraded_execution,
                                degraded_verify,
                                frame.flow.verdict_policy,
                            );
                            status = Some(folded.status);
                            degraded = folded.degraded;
                            summary = folded.summary;
                        }
                        cited = round_cited;
                        projected = Some(round_projected);
                        error_class = None;
                    }
                    ActPhase::Unconfirmed { message } => {
                        // The observe half of 「先 reconcile/observe 确认」:
                        // a fresh observation, the step's own assertions
                        // over it — the same pure evaluation an assert step
                        // runs. Expr assertions reference an output the
                        // timeout never produced and resolve unknown
                        // (`onMissingInput`, principle 4); element and
                        // visual predicates read the world and can be
                        // decisive in both directions. The fold is the
                        // confirmation verdict: pass — the effect is
                        // visibly there; fail — visibly not; unknown —
                        // unconfirmable, exactly the path the bare timeout
                        // always took.
                        let needs = VerifyNeeds::of(&step.assertions);
                        let material = self.fresh_material(&needs, &step_path).await?;
                        let assert_scope = frame.scope(&self.env, None);
                        let mut outcomes = Vec::with_capacity(step.assertions.len());
                        let mut degraded_verify = false;
                        for assertion in &step.assertions {
                            let evaluated = match &assertion.predicate {
                                PredicateIR::Expr { expr } => EvaluatedAssertion {
                                    record: eval_expr_assertion(assertion, expr, &assert_scope),
                                    degraded_verify: false,
                                },
                                _ => {
                                    eval_observed_assertion(
                                        assertion,
                                        &material,
                                        self.vision.as_deref(),
                                    )
                                    .await
                                }
                            };
                            degraded_verify |= evaluated.degraded_verify;
                            outcomes.push(evaluated.record);
                        }
                        for outcome in &outcomes {
                            let mut path = step_path.clone();
                            path.push(PathFrame::Phase {
                                phase: Phase::Assert,
                            });
                            path.push(PathFrame::Assertion {
                                assert_id: outcome.assert_id.clone(),
                            });
                            self.append(
                                &path,
                                &RunLogPayload::AssertionEvaluated {
                                    outcome: outcome.clone(),
                                },
                            )?;
                        }
                        let folded = fold_step_verdict(
                            &outcomes,
                            false,
                            degraded_verify,
                            frame.flow.verdict_policy,
                        );
                        status = Some(folded.status);
                        degraded = folded.degraded;
                        summary = format!(
                            "{message}; assertions over a fresh observation: {}",
                            folded.summary
                        );
                        cited = Vec::new();
                        projected = None;
                        error_class = None;
                    }
                    ActPhase::StepFail { class, message } => {
                        let wire = serde_json::to_value(class).expect("ErrorClass serializes");
                        let wire = wire.as_str().expect("ErrorClass is a string literal");
                        status = Some(VerdictStatus::Fail);
                        degraded = false;
                        summary = format!("act phase failed [{wire}]: {message}");
                        cited = Vec::new();
                        projected = None;
                        error_class = Some(class);
                    }
                    ActPhase::StepUnknown {
                        message,
                        error_class: class,
                    } => {
                        status = Some(VerdictStatus::Unknown);
                        degraded = false;
                        summary = message;
                        cited = Vec::new();
                        projected = None;
                        // Usually none — an unknown has no error to route.
                        // `session_degraded` is the exception the spine §5
                        // table names, and it keeps its class so the hook
                        // selector below reaches `onError`.
                        error_class = class;
                    }
                    ActPhase::Aborted => {
                        self.append(
                            &step_path,
                            &RunLogPayload::StepExited {
                                provider_state_summary: None,
                                state: StepState::Aborted,
                                output: None,
                                localized: Vec::new(),
                                localization_gaps: Vec::new(),
                            },
                        )?;
                        return Ok(Ctl::Abort);
                    }
                    ActPhase::Suspend(reason) => return Ok(Ctl::Suspend(reason)),
                },
                (None, None) => unreachable!("every round has an act result or a ruling"),
            }

            // Record this round's verdict (unless it came off the ledger)
            // and seed/reseed the frame fold.
            let round_status = status;
            if let Some(current) = round_status {
                if !ruled_from_ledger {
                    let folded = FoldedVerdict {
                        status: current,
                        degraded,
                        summary: summary.clone(),
                    };
                    let supersedes = last_verdict_seq.map(|seq| format!("seq:{seq}"));
                    let seq = self
                        .record_step_verdict(
                            &step_path,
                            &folded,
                            cited,
                            supersedes,
                            std::mem::take(&mut round_manifest),
                        )
                        .await?;
                    last_verdict_seq = Some(seq);
                }
                if seeded {
                    frame.reseed_last(&step_id, current, degraded);
                } else {
                    frame.seed_verdict(&step_id, current, degraded);
                    seeded = true;
                }
            }

            // Pass / unverified: exit judged and release.
            if round_status.is_none() || round_status == Some(VerdictStatus::Pass) {
                // An UNVERIFIED exit (R4: no assertions ⇒ no verdict ⇒
                // no verdictRecorded carrier) must not drop its
                // settlement-evidence manifest — it rides the exit
                // instead (item ③ review fix). A pass-verdict exit's
                // manifest already rode its verdictRecorded.
                let exit_manifest = if round_status.is_none() {
                    std::mem::take(&mut round_manifest)
                } else {
                    EvidenceManifest::default()
                };
                self.append(
                    &step_path,
                    &RunLogPayload::StepExited {
                        provider_state_summary: None,
                        state: StepState::Judged,
                        output: projected.clone(),
                        localized: exit_manifest.localized,
                        localization_gaps: exit_manifest.gaps,
                    },
                )?;
                if let Some(projected) = projected {
                    frame.outputs.insert(step_id.as_str().to_owned(), projected);
                }
                return Ok(Ctl::Continue);
            }
            let current = round_status.expect("checked above");

            // In-force handler retry budget (one consultation grants
            // `max_attempts` re-entries with its backoff schedule).
            if let Some((policy, used)) = active_retry.take()
                && used < policy.max_attempts
            {
                self.backoff_policy(&policy, used).await;
                active_retry = Some((policy, used + 1));
                acted = Some(
                    // In-force retry policy: re-enter from the chain head
                    // (item ② ruling — handler retry restarts the chain).
                    self.act_chain(
                        frame,
                        step,
                        &step_path,
                        Some(resolved_inputs.clone()),
                        None,
                        0,
                    )
                    .await?,
                );
                continue;
            }

            // Consult the hook: assertion negatives walk onFail/onUnknown,
            // error-path negatives walk onError, error-path unknowns walk
            // onUnknown (AssertionFailure is a verdict, not an error —
            // spine §5).
            let hook = if error_class.is_some() {
                HandlerHook::OnError
            } else if current == VerdictStatus::Fail {
                HandlerHook::OnFail
            } else {
                HandlerHook::OnUnknown
            };
            match self
                .consult_hook(
                    frame,
                    &step_path,
                    step.base.handlers.as_deref(),
                    hook,
                    error_class,
                )
                .await?
            {
                Consulted::None | Consulted::RepairFailed => {
                    self.append(
                        &step_path,
                        &RunLogPayload::StepExited {
                            provider_state_summary: None,
                            state: StepState::Judged,
                            output: projected.clone(),
                            localized: Vec::new(),
                            localization_gaps: Vec::new(),
                        },
                    )?;
                    if let Some(projected) = projected {
                        frame.outputs.insert(step_id.as_str().to_owned(), projected);
                    }
                    return Ok(if current == VerdictStatus::Fail {
                        Ctl::HaltFail
                    } else {
                        Ctl::Continue
                    });
                }
                Consulted::Continue => {
                    // Record-and-release: the verdict stands, downstream
                    // is not halted (spine §3 disposition table).
                    self.append(
                        &step_path,
                        &RunLogPayload::StepExited {
                            provider_state_summary: None,
                            state: StepState::Judged,
                            output: projected.clone(),
                            localized: Vec::new(),
                            localization_gaps: Vec::new(),
                        },
                    )?;
                    if let Some(projected) = projected {
                        frame.outputs.insert(step_id.as_str().to_owned(), projected);
                    }
                    return Ok(Ctl::Continue);
                }
                Consulted::Abort => {
                    self.append(
                        &step_path,
                        &RunLogPayload::StepExited {
                            provider_state_summary: None,
                            state: StepState::Aborted,
                            output: None,
                            localized: Vec::new(),
                            localization_gaps: Vec::new(),
                        },
                    )?;
                    return Ok(Ctl::Abort);
                }
                Consulted::Escalated {
                    status: ruled,
                    summary: ruled_summary,
                    evidence: ruling_evidence,
                } => {
                    let folded = FoldedVerdict {
                        status: ruled,
                        degraded: false,
                        summary: ruled_summary,
                    };
                    let supersedes = last_verdict_seq.map(|seq| format!("seq:{seq}"));
                    let (cited, manifest) = escalate_verdict_material(&ruling_evidence);
                    let ruled_seq = self
                        .record_step_verdict(&step_path, &folded, cited, supersedes, manifest)
                        .await?;
                    self.link_ruling_evidence(ruled_seq, &ruling_evidence)?;
                    if seeded {
                        frame.reseed_last(&step_id, ruled, false);
                    } else {
                        frame.seed_verdict(&step_id, ruled, false);
                    }
                    self.append(
                        &step_path,
                        &RunLogPayload::StepExited {
                            provider_state_summary: None,
                            state: StepState::Judged,
                            output: projected.clone(),
                            localized: Vec::new(),
                            localization_gaps: Vec::new(),
                        },
                    )?;
                    if let Some(projected) = projected {
                        frame.outputs.insert(step_id.as_str().to_owned(), projected);
                    }
                    return Ok(if ruled == VerdictStatus::Fail {
                        Ctl::HaltFail
                    } else {
                        Ctl::Continue
                    });
                }
                Consulted::Retry(policy) => {
                    self.backoff_policy(&policy, 0).await;
                    active_retry = Some((policy, 1));
                    acted = Some(
                        self.act_chain(
                            frame,
                            step,
                            &step_path,
                            Some(resolved_inputs.clone()),
                            None,
                            0,
                        )
                        .await?,
                    );
                }
                Consulted::Repaired | Consulted::RepairDone => {
                    // The world was (declared) fixed: one re-entry; a
                    // further negative re-consults (maxTriggers bounds
                    // the total).
                    acted = Some(
                        self.act_chain(
                            frame,
                            step,
                            &step_path,
                            Some(resolved_inputs.clone()),
                            None,
                            0,
                        )
                        .await?,
                    );
                }
                Consulted::Pending(pending) => {
                    // The verdict of this round is on the ledger (recorded
                    // above): the resume segment re-enters through the
                    // recorded-ruling jump. Span stays open.
                    return Ok(Ctl::AwaitHuman(pending));
                }
                Consulted::Propagate(ctl) => return Ok(ctl),
            }
        }
    }

    /// Settles a step whose execution ended in a definite negative (fail)
    /// or an unconfirmable state (unknown): verdict, write-back, exit.
    async fn settle_error(
        &mut self,
        frame: &mut FrameState<'a>,
        step_path: &RunPath,
        step_id: &StepId,
        status: VerdictStatus,
        summary: String,
    ) -> Result<Ctl, RunnerError> {
        let folded = FoldedVerdict {
            status,
            degraded: false,
            summary,
        };
        self.record_step_verdict(
            step_path,
            &folded,
            Vec::new(),
            None,
            EvidenceManifest::default(),
        )
        .await?;
        frame.seed_verdict(step_id, status, false);
        self.append(
            step_path,
            &RunLogPayload::StepExited {
                provider_state_summary: None,
                state: StepState::Judged,
                // No output projection completed on the error path.
                output: None,
                localized: Vec::new(),
                localization_gaps: Vec::new(),
            },
        )?;
        if status == VerdictStatus::Fail {
            Ok(Ctl::HaltFail)
        } else {
            Ok(Ctl::Continue)
        }
    }

    /// The act phase: the bound attempt chain with in-attempt retry
    /// (spine §6.5 mount point 1 — every retry is a new callId and a new
    /// WAL intent; the chain advances only on `action_failed_final`).
    async fn act_chain(
        &mut self,
        frame: &FrameState<'a>,
        step: &'a ActionStepIR,
        step_path: &RunPath,
        args_override: Option<Value>,
        mut adopted: Option<AdoptedSettle>,
        start_position: usize,
    ) -> Result<ActPhase, RunnerError> {
        let key = instance_key(step_path);
        let chain_len = step.binding.attempts.len();
        for (position, attempt) in step
            .binding
            .attempts
            .iter()
            .enumerate()
            .skip(start_position)
        {
            // The entry attempt consumes the override snapshot (the ready
            // snapshot on a fresh run, the archived argsSnapshot on a
            // crash re-entry — anchored at the recorded chain position,
            // 07 §1.4); a chain advance re-resolves the next attempt's
            // own argument expressions.
            let args = if position == start_position && args_override.is_some() {
                args_override.clone().expect("checked is_some")
            } else {
                match self.resolve_args(frame, attempt) {
                    Ok(args) => args,
                    Err(message) => {
                        return Ok(ActPhase::StepFail {
                            class: ErrorClass::BindArgumentsInvalid,
                            message,
                        });
                    }
                }
            };

            let mut tries: u32 = 0;
            loop {
                tries += 1;
                let (outcome, settled_seq, attempt_n) = match adopted.take() {
                    // The reconciled terminal is this try's settled
                    // outcome; its WAL intent and `actionSettled` are
                    // already on record — no dispatch.
                    Some(settle) => (settle.outcome, settle.settled_seq, settle.attempt_n),
                    None => {
                        let attempt_n = self.next_attempt_n(&key);
                        let call_id = uuid::Uuid::new_v4().to_string();
                        let mut attempt_path = step_path.clone();
                        attempt_path.push(PathFrame::Attempt { n: attempt_n });
                        // WAL discipline (spine §6.2): the intent commit
                        // *is* the fsync; only after it returns may the
                        // dispatch leave.
                        self.store.write_action_intent(
                            &self.run_id,
                            now_ms(),
                            &attempt_path,
                            &call_id,
                            args.clone(),
                            Some(pointlock_store::IntentDispatch {
                                chain_index: (position + 1) as u32,
                                channel: attempt.channel,
                                action_name: attempt.action_name.clone(),
                            }),
                        )?;
                        let call = BoundActionCall {
                            call_id: call_id.clone(),
                            action_name: attempt.action_name.clone(),
                            arguments: args.clone(),
                            action_timeout_ms: step.base.timeout_ms,
                            request_timeout_ms: None,
                        };
                        let outcome = match self.session.execute(call, None).await {
                            Ok(outcome) => outcome,
                            Err(error) => {
                                // No terminal could be obtained: the intent
                                // stays pending; suspend and reconcile on
                                // resume.
                                return Ok(ActPhase::Suspend(format!(
                                    "no terminal for callId {call_id}: {error}"
                                )));
                            }
                        };
                        let outcome = quarantine_unpersistable(outcome);
                        let settled_seq = self.append(
                            &attempt_path,
                            &RunLogPayload::ActionSettled {
                                call_id: call_id.clone(),
                                outcome: outcome.clone(),
                            },
                        )?;
                        (outcome, settled_seq, attempt_n)
                    }
                };
                match outcome {
                    ActionOutcome::Succeeded { result } => {
                        let degraded = !execution_accepted(attempt, &result.execution);
                        return Ok(ActPhase::Succeeded {
                            result,
                            degraded,
                            settled_seq,
                            attempt_n,
                        });
                    }
                    other => {
                        let class = classify(&other);
                        let message = terminal_message(&other);
                        if class == ErrorClass::ActionCancelled {
                            return Ok(ActPhase::Aborted);
                        }
                        if retry_allowed(step, class, tries) {
                            self.backoff(step, tries).await;
                            continue;
                        }
                        match class {
                            // Chain advance: only a final (possibly
                            // degradable) failure tries the next attempt.
                            ErrorClass::ActionFailedFinal if position + 1 < chain_len => break,
                            ErrorClass::ActionTimedOut => {
                                // A recorded `timedOut` is a certain fate —
                                // reconcile returns it verbatim and adds
                                // nothing — so the only confirmation channel
                                // left is OBSERVATION (spine §5 / 07 §4.3).
                                // With assertions declared, the settlement
                                // loop asks the world; without any there is
                                // nothing that could confirm, and the step
                                // folds to the honest unknown directly.
                                if !step.assertions.is_empty() {
                                    return Ok(ActPhase::Unconfirmed {
                                        message: format!("action timed out ({message})"),
                                    });
                                }
                                return Ok(ActPhase::StepUnknown {
                                    message: format!(
                                        "action timed out and the outcome could not be \
                                         confirmed: {message}"
                                    ),
                                    error_class: None,
                                });
                            }
                            ErrorClass::SessionDegraded => {
                                // spine §5: "当前 step → unknown,触发 flow 级
                                // onError handler" — both halves, so the
                                // class rides along to the hook selector.
                                return Ok(ActPhase::StepUnknown {
                                    message: format!("session degraded: {message}"),
                                    error_class: Some(ErrorClass::SessionDegraded),
                                });
                            }
                            _ => {
                                return Ok(ActPhase::StepFail { class, message });
                            }
                        }
                    }
                }
            }
        }
        unreachable!("the act chain always returns from within its last attempt")
    }

    /// Allocates the next attempt number for a step instance (monotonic
    /// across resume segments — the base is harvested from the log).
    fn next_attempt_n(&mut self, key: &str) -> u64 {
        let counter = self.attempt_base.entry(key.to_owned()).or_insert(0);
        *counter += 1;
        *counter
    }

    /// Waits out the retry backoff (spine §6.5 mount 1).
    async fn backoff(&self, step: &ActionStepIR, tries: u32) {
        let Some(policy) = &step.base.retry else {
            return;
        };
        self.backoff_policy(policy, tries).await;
    }

    /// Sleeps one backoff period of an explicit policy (the handler-retry
    /// disposition carries its own policy, independent of `StepBase.retry`
    /// — spine §6.5 mount 2).
    async fn backoff_policy(&self, policy: &RetryPolicy, tries: u32) {
        let ms = backoff_ms(policy, tries);
        if ms > 0 {
            tokio::time::sleep(std::time::Duration::from_millis(ms)).await;
        }
    }

    // ─── human steps & supervision gate (06 §2/§5; spine §6.8/§6.9) ─────────

    /// The R13 supervision gate of one action step, consulted between the
    /// preflight probes and the act chain (i.e. strictly before any
    /// `actionIntent`). Returns `None` to let the dispatch proceed.
    ///
    /// A pending gate request from a previous segment resolves first —
    /// regardless of *this* segment's policy (the request survives across
    /// segments, spine §6.9): `proceed` falls through to the intent,
    /// `abort` exits the step `aborted` and aborts the run without
    /// consulting any handler, anything else (unanswered, or the non-final
    /// `suspend` ruling) re-awaits. Fresh gating follows this segment's
    /// policy: `mutating` gates mutating action steps only, `all` gates
    /// every action step.
    fn gate_supervision(
        &mut self,
        step_path: &RunPath,
        step: &'a ActionStepIR,
        resolved_inputs: &Value,
    ) -> Result<Option<Ctl>, RunnerError> {
        let key = instance_key(step_path);
        if let Some(fact) = self
            .human
            .get(&key)
            .filter(|fact| fact.purpose == HumanPurpose::Supervision)
        {
            let decision = fact
                .final_response
                .as_ref()
                .and_then(|response| response.get("decision"))
                .and_then(Value::as_str);
            return match decision {
                // humanResponded(proceed) is on the ledger before the
                // intent this clears the way for (§6.9 WAL order).
                Some("proceed") => Ok(None),
                Some("abort") => {
                    // The human ruling is final: no handler is consulted
                    // (§6.9 R13); the step exits `aborted` and the run
                    // takes the existing aborted terminal.
                    self.append(
                        step_path,
                        &RunLogPayload::StepExited {
                            provider_state_summary: None,
                            state: StepState::Aborted,
                            output: None,
                            localized: Vec::new(),
                            localization_gaps: Vec::new(),
                        },
                    )?;
                    Ok(Some(Ctl::Abort))
                }
                _ => Ok(Some(Ctl::AwaitHuman(HumanPending {
                    run_path: fact.run_path.clone(),
                    request_id: fact.request_id.clone(),
                    purpose: HumanPurpose::Supervision,
                    mode: None,
                    prompt: fact.prompt.clone(),
                    deadline_at_ms: None,
                }))),
            };
        }
        let Some(policy) = self.supervise else {
            return Ok(None);
        };
        let gated = match policy {
            SupervisePolicy::All => true,
            SupervisePolicy::Mutating => step.effect == EffectClassAction::Mutating,
        };
        if !gated {
            return Ok(None);
        }
        // Fresh gate: auto-generated description over runPath /
        // actionName / resolvedInputs (§6.9). No mode, no decisions
        // contract, no deadline — the decision vocabulary is the closed
        // proceed | abort | suspend, arbitrated by the store.
        let attempt = step
            .binding
            .attempts
            .first()
            .expect("sealed action steps carry at least one bound attempt");
        let action_name = attempt.action_name.as_str().to_owned();
        let rendered = render_run_path(step_path);
        let request_id = uuid::Uuid::new_v4().to_string();
        let prompt = format!(
            "Supervision gate: approve dispatching action '{action_name}' at {rendered}? \
             The resolved inputs are presented."
        );
        let presents = serde_json::json!([
            { "kind": "value", "label": "runPath", "value": rendered },
            { "kind": "value", "label": "actionName", "value": action_name },
            { "kind": "value", "label": "resolvedInputs", "value": resolved_inputs },
        ]);
        // fsync-before-notify (spine §6.9): the append commit *is* the
        // fsync; the runner then suspends — any notification happens in
        // the CLI layer, strictly after this returns.
        self.append(
            step_path,
            &RunLogPayload::HumanRequested {
                request_id: request_id.clone(),
                purpose: HumanPurpose::Supervision,
                mode: None,
                prompt: prompt.clone(),
                presents,
                decisions: None,
                output_schema: None,
                deadline_at_ms: None,
            },
        )?;
        Ok(Some(Ctl::AwaitHuman(HumanPending {
            run_path: step_path.clone(),
            request_id,
            purpose: HumanPurpose::Supervision,
            mode: None,
            prompt,
            deadline_at_ms: None,
        })))
    }

    /// Executes one human step (06 §5.1 pinned order): ready (presents
    /// materialized once and frozen) → `stepEntered` → declared preflight
    /// → `humanRequested` (fsynced by its commit) → suspend /
    /// [`RunOutcome::AwaitingHuman`]. Resume settles a paired response
    /// through the four-mode mapping, re-awaits an unanswered request
    /// inside its deadline, and lazily settles an expired one to `unknown`
    /// — the settlement result depends only on `deadlineAtMs` and response
    /// presence, never on the settlement instant (06 §5.3).
    async fn exec_human(
        &mut self,
        frame: &mut FrameState<'a>,
        step_path: RunPath,
        step: &'a HumanStepIR,
    ) -> Result<Ctl, RunnerError> {
        let step_id = step.base.step_id.clone();
        let key = instance_key(&step_path);

        // A request already on the ledger for this instance: settle or
        // keep waiting — never a second `humanRequested` while one is
        // pending.
        if let Some(fact) = self
            .human
            .get(&key)
            .filter(|fact| fact.purpose == HumanPurpose::Step)
        {
            let fact = fact.clone();
            if let Some(response) = fact.final_response.clone() {
                // The span is open by construction (settlement closes it
                // for good); enter_step only consumes it.
                self.enter_step(&step_path, &step.base, Value::Null)?;
                return self
                    .settle_human_response(frame, &step_path, step, &fact, response)
                    .await;
            }
            match fact.deadline_at_ms {
                Some(deadline) if self.now() > deadline => {
                    self.enter_step(&step_path, &step.base, Value::Null)?;
                    return self
                        .settle_human_timeout(frame, &step_path, step, &fact)
                        .await;
                }
                // Unanswered and not expired: re-await the same request
                // (no new requestId, no re-notify obligation here).
                _ => {
                    return Ok(Ctl::AwaitHuman(HumanPending {
                        run_path: fact.run_path.clone(),
                        request_id: fact.request_id.clone(),
                        purpose: HumanPurpose::Step,
                        mode: Some(step.mode),
                        prompt: fact.prompt.clone(),
                        deadline_at_ms: fact.deadline_at_ms,
                    }));
                }
            }
        }

        // Ready: materialize `presents` once and freeze — the
        // `resolvedInputs` snapshot discipline (06 §2.3). A
        // crash/suspension-opened span reuses the archived snapshot.
        let snapshot = match self.open_span_inputs(&key) {
            Some(archived) => archived,
            None => {
                let scope = frame.scope(&self.env, None);
                let mut items = Vec::with_capacity(step.presents.len());
                let mut error = None;
                for (index, expr) in step.presents.iter().enumerate() {
                    match pointlock_expr::eval(expr, &scope) {
                        Ok(value) => items.push(value),
                        Err(eval_error) => {
                            error =
                                Some(format!("present #{index} evaluation failed: {eval_error}"));
                            break;
                        }
                    }
                }
                if let Some(message) = error {
                    // A failing presents evaluation is a compiler /
                    // expression bug signal (bind_arguments_invalid
                    // discipline): step fails, nothing is asked.
                    self.enter_step(&step_path, &step.base, Value::Null)?;
                    return self
                        .settle_error(
                            frame,
                            &step_path,
                            &step_id,
                            VerdictStatus::Fail,
                            format!("human presents failed [bind_arguments_invalid]: {message}"),
                        )
                        .await;
                }
                serde_json::json!({ "presents": items })
            }
        };
        self.enter_step(&step_path, &step.base, snapshot.clone())?;
        if let Some(ctl) = self.probe_or_note(frame, &step_path, &step.base).await? {
            return Ok(ctl);
        }
        let presents = snapshot
            .get("presents")
            .cloned()
            .unwrap_or(Value::Array(Vec::new()));
        let request_id = uuid::Uuid::new_v4().to_string();
        // `timeoutMs` converts to the absolute deadline watermark at
        // request creation (06 §5.3) — the sole lazy-settlement input.
        let deadline_at_ms = self.now().saturating_add(step.timeout_ms);
        // fsync-before-notify (spine §6.8, 06 §5.1): the append commit
        // *is* the fsync (WAL + synchronous=FULL); the runner suspends
        // and never notifies — channels are the CLI layer's job.
        self.append(
            &step_path,
            &RunLogPayload::HumanRequested {
                request_id: request_id.clone(),
                purpose: HumanPurpose::Step,
                mode: Some(step.mode),
                prompt: step.prompt.clone(),
                presents,
                decisions: step.decisions.clone(),
                output_schema: step.output_schema.clone(),
                deadline_at_ms: Some(deadline_at_ms),
            },
        )?;
        Ok(Ctl::AwaitHuman(HumanPending {
            run_path: step_path,
            request_id,
            purpose: HumanPurpose::Step,
            mode: Some(step.mode),
            prompt: step.prompt.clone(),
            deadline_at_ms: Some(deadline_at_ms),
        }))
    }

    /// Settles a human step from its arbitrated final response — the
    /// four-mode verdict/output mapping (06 §2.2 as adjudicated):
    ///
    /// | mode | response | verdict | step output |
    /// |---|---|---|---|
    /// | `confirm` | `decision` = first label | pass | the response object |
    /// | `confirm` | `decision` = second label | fail | the response object |
    /// | `judge` | `status` pass/fail/unknown | verbatim | the response object |
    /// | `provideInput` | `input` (schema-checked by the store) | pass | the input value |
    /// | `repairWorld` | `decision: "done"` | pass (testimony, not observation; never degraded) | the response object |
    /// | `repairWorld` | `decision: "cannotRepair"` | fail (06 §2.2 — a verdict, catchable by `onFail`; never a run abort) | the response object |
    ///
    /// Every settlement materializes the response as a canonical JSON
    /// evidence document and cites it from the verdict (06 §6).
    async fn settle_human_response(
        &mut self,
        frame: &mut FrameState<'a>,
        step_path: &RunPath,
        step: &'a HumanStepIR,
        fact: &HumanRequestFact,
        response: Value,
    ) -> Result<Ctl, RunnerError> {
        let step_id = step.base.step_id.clone();
        let decision = response
            .get("decision")
            .and_then(Value::as_str)
            .unwrap_or_default()
            .to_owned();
        let (status, output, summary) = match step.mode {
            HumanMode::Confirm => {
                let labels = step
                    .decisions
                    .as_ref()
                    .expect("load validated confirm decisions");
                // Position-mapped double label: first → pass, second →
                // fail (membership was the store arbitration's check).
                let status = if labels.first().map(String::as_str) == Some(decision.as_str()) {
                    VerdictStatus::Pass
                } else {
                    VerdictStatus::Fail
                };
                let position = if status == VerdictStatus::Pass {
                    "first"
                } else {
                    "second"
                };
                (
                    status,
                    Some(response.clone()),
                    format!(
                        "human confirm decision '{decision}' is the {position} label \
                         (position-mapped verdict)"
                    ),
                )
            }
            HumanMode::Judge => {
                // The human ruling *is* the verdict (spine §6.3).
                let status = match response.get("status").and_then(Value::as_str) {
                    Some("pass") => VerdictStatus::Pass,
                    Some("fail") => VerdictStatus::Fail,
                    _ => VerdictStatus::Unknown,
                };
                let label = response
                    .get("status")
                    .and_then(Value::as_str)
                    .unwrap_or("unknown");
                (
                    status,
                    Some(response.clone()),
                    format!("human judge ruling: {label}"),
                )
            }
            HumanMode::ProvideInput => {
                // The store validated `input` against `outputSchema`; the
                // established fact is "a human provided schema-valid
                // input" — pass, and the input *is* the step output.
                let input = response.get("input").cloned().unwrap_or(Value::Null);
                (
                    VerdictStatus::Pass,
                    Some(input),
                    "human provided input validated against the outputSchema".to_owned(),
                )
            }
            HumanMode::RepairWorld => {
                // 06 §2.2's closed mapping: `done` → pass, `cannotRepair`
                // → fail. Testimony, not observation — pass carries no
                // degraded flag (machine re-checks belong to follow-up
                // assert/preflight steps), and a fail is a verdict the
                // step's own `onFail` may catch, never a unilateral run
                // abort.
                let status = if decision == "done" {
                    VerdictStatus::Pass
                } else {
                    VerdictStatus::Fail
                };
                let summary = if status == VerdictStatus::Pass {
                    "human declared the world repaired (testimony; follow-up machine \
                     re-check advised)"
                        .to_owned()
                } else {
                    "human declared the world unrepairable (cannotRepair)".to_owned()
                };
                (status, Some(response.clone()), summary)
            }
        };
        let asset = self.put_human_evidence(step_path, fact, Some(&response), "response")?;
        let folded = FoldedVerdict {
            status,
            degraded: false,
            summary,
        };
        let verdict_seq = self
            .record_step_verdict(
                step_path,
                &folded,
                vec![asset.clone()],
                None,
                human_manifest(&asset),
            )
            .await?;
        if let Some(sha256) = &asset.sha256 {
            self.store
                .link_evidence(&self.run_id, verdict_seq, &asset.id, sha256)?;
        }
        frame.seed_verdict(&step_id, status, false);
        self.append(
            step_path,
            &RunLogPayload::StepExited {
                provider_state_summary: None,
                state: StepState::Judged,
                output: output.clone(),
                localized: Vec::new(),
                localization_gaps: Vec::new(),
            },
        )?;
        if let Some(output) = output {
            frame.outputs.insert(step_id.as_str().to_owned(), output);
        }
        if status == VerdictStatus::Fail {
            Ok(Ctl::HaltFail)
        } else {
            Ok(Ctl::Continue)
        }
    }

    /// Lazy timeout settlement (06 §5.3): the deadline passed with no
    /// arbitrated response — verdict `unknown` (`onTimeout` is fixed),
    /// no output (downstream consumers of it block). The judgment inputs
    /// are `deadlineAtMs` and response absence only; the settlement
    /// instant leaves no trace in the verdict ("no one came" is itself a
    /// recorded historical fact, 06 §6).
    async fn settle_human_timeout(
        &mut self,
        frame: &mut FrameState<'a>,
        step_path: &RunPath,
        step: &'a HumanStepIR,
        fact: &HumanRequestFact,
    ) -> Result<Ctl, RunnerError> {
        let step_id = &step.base.step_id;
        let deadline = fact.deadline_at_ms.unwrap_or_default();
        let asset = self.put_human_evidence(step_path, fact, None, "timeout")?;
        let folded = FoldedVerdict {
            status: VerdictStatus::Unknown,
            degraded: false,
            summary: format!(
                "human response deadline (deadlineAtMs {deadline}) passed without a \
                 response; onTimeout is fixed to unknown"
            ),
        };
        let verdict_seq = self
            .record_step_verdict(
                step_path,
                &folded,
                vec![asset.clone()],
                None,
                human_manifest(&asset),
            )
            .await?;
        if let Some(sha256) = &asset.sha256 {
            self.store
                .link_evidence(&self.run_id, verdict_seq, &asset.id, sha256)?;
        }
        frame.seed_verdict(step_id, VerdictStatus::Unknown, false);

        // A timed-out human step is an unknown verdict: the onUnknown
        // ladder applies (06's escalation pattern). Re-ask dispositions
        // (retry/repair) need fresh-request machinery — typed M2 refusal.
        match self
            .consult_hook(
                frame,
                step_path,
                step.base.handlers.as_deref(),
                HandlerHook::OnUnknown,
                None,
            )
            .await?
        {
            Consulted::None | Consulted::RepairFailed | Consulted::Continue => {}
            Consulted::Abort => {
                self.append(
                    step_path,
                    &RunLogPayload::StepExited {
                        provider_state_summary: None,
                        state: StepState::Aborted,
                        output: None,
                        localized: Vec::new(),
                        localization_gaps: Vec::new(),
                    },
                )?;
                return Ok(Ctl::Abort);
            }
            Consulted::Escalated {
                status: ruled,
                summary: ruled_summary,
                evidence: ruling_evidence,
            } => {
                let folded = FoldedVerdict {
                    status: ruled,
                    degraded: false,
                    summary: ruled_summary,
                };
                let (cited, manifest) = escalate_verdict_material(&ruling_evidence);
                let ruled_seq = self
                    .record_step_verdict(
                        step_path,
                        &folded,
                        cited,
                        Some(format!("seq:{verdict_seq}")),
                        manifest,
                    )
                    .await?;
                self.link_ruling_evidence(ruled_seq, &ruling_evidence)?;
                frame.reseed_last(step_id, ruled, false);
                self.append(
                    step_path,
                    &RunLogPayload::StepExited {
                        provider_state_summary: None,
                        state: StepState::Judged,
                        output: None,
                        localized: Vec::new(),
                        localization_gaps: Vec::new(),
                    },
                )?;
                return Ok(if ruled == VerdictStatus::Fail {
                    Ctl::HaltFail
                } else {
                    Ctl::Continue
                });
            }
            Consulted::Retry(_) | Consulted::Repaired | Consulted::RepairDone => {
                return Err(RunnerError::M0Unsupported {
                    detail: format!(
                        "re-ask dispositions on the timed-out human step '{step_id}' need \
                         fresh-request machinery — not in the M2 subset \
                         (escalate/continue/abort are supported)"
                    ),
                });
            }
            Consulted::Pending(pending) => return Ok(Ctl::AwaitHuman(pending)),
            Consulted::Propagate(ctl) => return Ok(ctl),
        }

        self.append(
            step_path,
            &RunLogPayload::StepExited {
                provider_state_summary: None,
                state: StepState::Judged,
                output: None,
                localized: Vec::new(),
                localization_gaps: Vec::new(),
            },
        )?;
        Ok(Ctl::Continue)
    }

    /// Materializes a human settlement as a canonical JSON evidence
    /// document in the content-addressed store and mints its citable
    /// [`AssetRef`] (06 §6: who / when-relative-to-deadline / what was
    /// decided / what was presented). Deliberately clock-free: the
    /// document is a pure function of the request and the arbitrated
    /// response (or its absence).
    /// Links an escalate ruling's settlement document to its superseding
    /// verdict row (same file-before-row-before-log join as body humans).
    fn link_ruling_evidence(
        &mut self,
        verdict_seq: u64,
        evidence: &Option<AssetRef>,
    ) -> Result<(), RunnerError> {
        if let Some(asset) = evidence
            && let Some(sha256) = &asset.sha256
        {
            self.store
                .link_evidence(&self.run_id, verdict_seq, &asset.id, sha256)?;
        }
        Ok(())
    }

    fn put_human_evidence(
        &mut self,
        step_path: &RunPath,
        fact: &HumanRequestFact,
        response: Option<&Value>,
        settled_as: &str,
    ) -> Result<AssetRef, RunnerError> {
        let document = serde_json::json!({
            "pointlockEvidence": "humanResponse/1",
            "requestId": fact.request_id,
            "runId": self.run_id,
            "runPath": render_run_path(step_path),
            "purpose": fact.purpose,
            "mode": fact.mode,
            "prompt": fact.prompt,
            "presented": fact.presents,
            "response": response.cloned().unwrap_or(Value::Null),
            "actor": match (settled_as, &fact.final_actor) {
                // Timeout settlements have no actor (06 §6).
                ("timeout", _) => Value::Null,
                (_, Some(actor)) => Value::String(actor.clone()),
                (_, None) => Value::Null,
            },
            "deadlineAtMs": fact.deadline_at_ms,
            "settledAs": settled_as,
        });
        let bytes = to_canonical_json(&document).into_bytes();
        // file-before-row-before-log: the bytes are durable before the
        // verdict event cites them.
        let put = self.store.put_evidence(&bytes, "application/json")?;
        Ok(AssetRef {
            id: format!("humanResponse:{}", fact.request_id),
            media_type: "application/json".to_owned(),
            // Locally minted evidence: the URI is the content-addressed
            // library path (06 §6).
            uri: put.local_path,
            sha256: Some(put.sha256),
        })
    }

    // ─── handler engine (spine §3/§6.5 mount 2; M2 W3) ──────────────────────
    //
    // Handlers are explicit policies on four hooks; they yield a
    // disposition, never data (R10). Consultation happens *inside* the
    // host step's open span, before `stepExited`, so a retry disposition
    // re-enters the failing phase within the same entered/exited pair.
    // The `handlerTriggered` audit event anchors at the host step path;
    // the hook frame (`/hook:<name>:<n>`) anchors the disposition's own
    // work (escalate humans, repair frames).

    /// Resolves the binding a hook consults: the step-level list first
    /// (first match wins), else the flow-level list (spine §3
    /// `StepBase.handlers` overrides `FlowIR.handlers`). `onError`
    /// bindings additionally filter on `errorClasses`.
    fn resolve_binding(
        &self,
        frame: &FrameState<'a>,
        step_handlers: Option<&'a [HandlerBinding]>,
        hook: HandlerHook,
        error_class: Option<ErrorClass>,
    ) -> Option<&'a HandlerBinding> {
        let matches = |binding: &&'a HandlerBinding| {
            binding.hook == hook
                && (hook != HandlerHook::OnError
                    || match (&binding.error_classes, error_class) {
                        (None, _) => true,
                        (Some(filter), Some(class)) => filter.contains(&class),
                        (Some(_), None) => false,
                    })
        };
        step_handlers
            .and_then(|bindings| bindings.iter().find(matches))
            .or_else(|| {
                frame
                    .flow
                    .handlers
                    .as_deref()
                    .and_then(|bindings| bindings.iter().find(matches))
            })
    }

    /// Consults the matching handler binding for `hook` on the host step.
    ///
    /// Trigger counting is per host instance per hook and continues
    /// across segments (harvested from the ledger); an exhausted budget
    /// returns [`Consulted::None`] — the natural path stands. A pending
    /// escalate continuation (a previous segment's unanswered hook human)
    /// is settled or re-awaited *without* consuming a new trigger.
    async fn consult_hook(
        &mut self,
        frame: &mut FrameState<'a>,
        step_path: &RunPath,
        step_handlers: Option<&'a [HandlerBinding]>,
        hook: HandlerHook,
        error_class: Option<ErrorClass>,
    ) -> Result<Consulted, RunnerError> {
        let Some(binding) = self.resolve_binding(frame, step_handlers, hook, error_class) else {
            return Ok(Consulted::None);
        };
        let counter_key = crate::align::hook_trigger_key(&instance_key(step_path), hook);
        let current = self.hook_triggers.get(&counter_key).copied().unwrap_or(0);

        // Escalate continuation: trigger N already on the ledger and its
        // hook human still governs — settle or re-await, no new trigger.
        if current >= 1
            && let HandlerAction::Escalate { human } = &binding.action
        {
            let human_path = hook_child_path(step_path, hook, current, human);
            if self.human.contains_key(&instance_key(&human_path)) {
                // A request exists for the current trigger: it fully
                // governs (settle its response, its timeout, or re-await).
                return self
                    .run_hook_human(frame, step_path, hook, current, human)
                    .await;
            }
        }

        let trigger = current + 1;
        if trigger > u64::from(binding.max_triggers) {
            return Ok(Consulted::None);
        }
        self.hook_triggers.insert(counter_key, trigger);
        let disposition = match &binding.action {
            HandlerAction::Retry { .. } => "retry",
            HandlerAction::Continue => "continue",
            HandlerAction::Abort => "abort",
            HandlerAction::Escalate { .. } => "escalate",
            HandlerAction::Repair { .. } => "repair",
        };
        self.append(
            step_path,
            &RunLogPayload::HandlerTriggered {
                hook,
                trigger,
                disposition: Some(disposition.to_owned()),
            },
        )?;

        match &binding.action {
            HandlerAction::Retry { policy } => Ok(Consulted::Retry(policy.clone())),
            HandlerAction::Continue => Ok(Consulted::Continue),
            HandlerAction::Abort => Ok(Consulted::Abort),
            HandlerAction::Escalate { human } => {
                self.run_hook_human(frame, step_path, hook, trigger, human)
                    .await
            }
            HandlerAction::Repair { flow_ref } => {
                self.run_repair(frame, step_path, hook, trigger, flow_ref)
                    .await
            }
        }
    }

    /// Runs (or settles) an escalate hook human. Hook humans are not body
    /// steps: they open no span and seed no frame verdict — their ruling
    /// is returned to the consultation site, which supersedes the host
    /// verdict and cites the canonical settlement evidence document
    /// minted here (06 §6; the request/response ledger pair is the join).
    async fn run_hook_human(
        &mut self,
        frame: &mut FrameState<'a>,
        step_path: &RunPath,
        hook: HandlerHook,
        trigger: u64,
        human: &'a HumanStepIR,
    ) -> Result<Consulted, RunnerError> {
        let human_path = hook_child_path(step_path, hook, trigger, human);
        let key = instance_key(&human_path);
        if let Some(fact) = self.human.get(&key) {
            let fact = fact.clone();
            if let Some(response) = fact.final_response.clone() {
                // Consume the settlement: a ruling governs exactly once —
                // a later consult on the same host walks a *new* trigger
                // (or exhausts the budget), never re-reads this answer.
                self.human.remove(&key);
                // Every settlement materializes the canonical evidence
                // document (06 §6); the Escalated superseding verdict
                // cites it below. Non-verdict dispositions (Repaired/
                // Abort) keep it durable in the library with the
                // request/response pair as the join.
                let asset =
                    self.put_human_evidence(&human_path, &fact, Some(&response), "response")?;
                return Ok(match map_escalate_response(human, &response) {
                    Consulted::Escalated {
                        status, summary, ..
                    } => Consulted::Escalated {
                        status,
                        summary,
                        evidence: Some(asset),
                    },
                    other => other,
                });
            }
            match fact.deadline_at_ms {
                Some(deadline) if self.now() > deadline => {
                    // Lazy timeout settlement: unknown, fixed (onTimeout);
                    // consumed like any settlement — with the canonical
                    // evidence document (06 §6, actor null on timeout).
                    self.human.remove(&key);
                    let asset = self.put_human_evidence(&human_path, &fact, None, "timeout")?;
                    return Ok(Consulted::Escalated {
                        status: VerdictStatus::Unknown,
                        summary: format!(
                            "escalate human '{}' timed out (deadline watermark passed): unknown",
                            human.base.step_id
                        ),
                        evidence: Some(asset),
                    });
                }
                _ => {
                    return Ok(Consulted::Pending(HumanPending {
                        run_path: fact.run_path.clone(),
                        request_id: fact.request_id.clone(),
                        purpose: HumanPurpose::Step,
                        mode: Some(human.mode),
                        prompt: fact.prompt.clone(),
                        deadline_at_ms: fact.deadline_at_ms,
                    }));
                }
            }
        }

        // First encounter: materialize presents in the host frame's scope
        // and freeze; fsync-before-notify discipline as everywhere.
        let scope = frame.scope(&self.env, None);
        let mut items = Vec::with_capacity(human.presents.len());
        for expr in &human.presents {
            match pointlock_expr::eval(expr, &scope) {
                Ok(value) => items.push(value),
                // A failing presents expression degrades to an empty
                // exhibit — the request still goes out (the human can
                // rule without exhibits; principle 8 over strictness).
                Err(_) => items.push(Value::Null),
            }
        }
        let request_id = uuid::Uuid::new_v4().to_string();
        let deadline_at_ms = self.now().saturating_add(human.timeout_ms);
        self.append(
            &human_path,
            &RunLogPayload::HumanRequested {
                request_id: request_id.clone(),
                purpose: HumanPurpose::Step,
                mode: Some(human.mode),
                prompt: human.prompt.clone(),
                presents: Value::Array(items),
                decisions: human.decisions.clone(),
                output_schema: human.output_schema.clone(),
                deadline_at_ms: Some(deadline_at_ms),
            },
        )?;
        Ok(Consulted::Pending(HumanPending {
            run_path: human_path,
            request_id,
            purpose: HumanPurpose::Step,
            mode: Some(human.mode),
            prompt: human.prompt.clone(),
            deadline_at_ms: Some(deadline_at_ms),
        }))
    }

    /// Runs a repair subflow under the hook frame (a call frame without a
    /// host call step — spine §9). Repair flows take no caller inputs
    /// (their params materialize from declared defaults, 06 §7.5 binding-
    /// flow pattern); they yield no data (R10) — only their flow verdict
    /// comes back as the disposition signal.
    async fn run_repair(
        &mut self,
        frame: &mut FrameState<'a>,
        step_path: &RunPath,
        hook: HandlerHook,
        trigger: u64,
        flow_ref: &'a pointlock_ir::FlowRef,
    ) -> Result<Consulted, RunnerError> {
        let callee = self.flows.callee(flow_ref);
        let mut repair_path = step_path.clone();
        repair_path.push(hook_frame(hook, trigger));
        repair_path.push(PathFrame::Call {
            step_id: None,
            callee_flow_id: callee.flow_id.clone(),
            callee_ir_hash: callee.ir_hash.clone(),
        });
        // Defaults-only inbound materialization.
        let params = match call_inputs_gate(callee, Map::new()) {
            Ok(params) => params,
            // A repair flow whose params cannot materialize from defaults
            // is a repair failure; its detail is compiler-diagnosable.
            Err(_) => return Ok(Consulted::RepairFailed),
        };
        self.append(
            &repair_path,
            &RunLogPayload::CallFramePushed {
                frame: CallFrame {
                    flow_id: callee.flow_id.clone(),
                    ir_hash: callee.ir_hash.clone(),
                    call_step_id: None,
                    inputs_snapshot: Value::Object(params.clone()),
                    vars: BTreeMap::new(),
                    iter_stack: Vec::new(),
                    next_index: 0,
                },
                rebase: false,
            },
        )?;
        let mut repair_frame =
            FrameState::new(callee, repair_path.clone(), params, frame.depth + 1);
        let ctl = self
            .exec_body(&mut repair_frame, repair_path.clone(), &callee.body, 0)
            .await?;
        match ctl {
            Ctl::Continue | Ctl::HaltFail => {}
            other => {
                // Suspension inside a repair leaves its frame live; the
                // resume path refuses live hook frames with a typed error
                // (registered M2 limitation) — the ledger stays honest.
                return Ok(Consulted::Propagate(other));
            }
        }
        self.append(
            &repair_path,
            &RunLogPayload::CallFramePopped { outputs: None },
        )?;
        let verdict = fold_flow_verdict(&repair_frame.fold, callee.verdict_policy);
        match (ctl, verdict) {
            (Ctl::HaltFail, _) => Ok(Consulted::RepairFailed),
            (_, Some(folded)) if folded.status != VerdictStatus::Pass => {
                Ok(Consulted::RepairFailed)
            }
            _ => Ok(Consulted::RepairDone),
        }
    }

    // ─── call steps (07 §1) ─────────────────────────────────────────────────

    async fn exec_call(
        &mut self,
        frame: &mut FrameState<'a>,
        call_path: RunPath,
        step: &'a CallStepIR,
    ) -> Result<Ctl, RunnerError> {
        let step_id = step.base.step_id.clone();
        let key = instance_key(&call_path);
        let callee: &'a FlowIR = self.flows.callee(&step.flow_ref);
        // Runtime defense line for maxCallDepth (the load check already
        // bounds the static closure; this guards the walk itself).
        if frame.depth + 1 > MAX_CALL_DEPTH {
            return Err(RunnerError::CallDepthExceeded {
                depth: frame.depth + 1,
                max: MAX_CALL_DEPTH,
            });
        }

        // Ready: call-by-value inputs snapshot. A suspension-opened span
        // reuses the archived snapshot (already gated) — never
        // re-evaluated (spine §6.6, 07 §5.2 corollary).
        let archived = self.open_span_inputs(&key);
        let gated = match archived {
            Some(Value::Object(map)) => map,
            Some(other) => {
                // A non-object archived snapshot is a ledger anomaly; the
                // honest disposition is a bind-class failure.
                self.enter_step(&call_path, &step.base, other)?;
                return self
                    .settle_error(
                        frame,
                        &call_path,
                        &step_id,
                        VerdictStatus::Fail,
                        "archived call inputs snapshot is not an object".to_owned(),
                    )
                    .await;
            }
            None => {
                // Evaluate each input expression in the *caller* scope.
                let scope = frame.scope(&self.env, None);
                let mut inputs = Map::new();
                let mut eval_error = None;
                for (name, expr) in step.inputs.iter() {
                    match pointlock_expr::eval(expr, &scope) {
                        Ok(value) => {
                            inputs.insert(name.as_str().to_owned(), value);
                        }
                        Err(error) => {
                            eval_error = Some(format!("input '{name}' evaluation failed: {error}"));
                            break;
                        }
                    }
                }
                if let Some(message) = eval_error {
                    self.enter_step(&call_path, &step.base, Value::Null)?;
                    return self
                        .settle_error(
                            frame,
                            &call_path,
                            &step_id,
                            VerdictStatus::Fail,
                            format!("call inputs failed [bind_arguments_invalid]: {message}"),
                        )
                        .await;
                }
                // Inbound gate: defaults + per-param schema validation
                // (07 §1.1 — runtime re-check; failure classifies
                // bind_arguments_invalid, no retry).
                match call_inputs_gate(callee, inputs.clone()) {
                    Ok(gated) => gated,
                    Err(message) => {
                        self.enter_step(&call_path, &step.base, Value::Object(inputs))?;
                        return self
                            .settle_error(
                                frame,
                                &call_path,
                                &step_id,
                                VerdictStatus::Fail,
                                format!("call inputs failed [bind_arguments_invalid]: {message}"),
                            )
                            .await;
                    }
                }
            }
        };
        self.enter_step(&call_path, &step.base, Value::Object(gated.clone()))?;

        if let Some(ctl) = self.probe_or_note(frame, &call_path, &step.base).await? {
            return Ok(ctl);
        }

        // Frame push (07 §3.1 frame-transfer materialization point) —
        // unless a previous segment already pushed it and we are resuming
        // back into the live frame. Re-entering one whose callee pin moved
        // is announced as a `rebase`, so `frames` names the callee actually
        // executing rather than the one the crashed segment entered
        // (07 §5.2 case (a)); the fold updates that frame's `irHash` in
        // place and touches nothing else.
        let open_pin = self.live_frames.remove(&key);
        let rebase = match &open_pin {
            None => false,
            Some(pin) => *pin != callee.ir_hash,
        };
        if open_pin.is_none() || rebase {
            self.append(
                &call_path,
                &RunLogPayload::CallFramePushed {
                    frame: CallFrame {
                        flow_id: callee.flow_id.clone(),
                        ir_hash: callee.ir_hash.clone(),
                        call_step_id: Some(step_id.clone()),
                        inputs_snapshot: Value::Object(gated.clone()),
                        vars: BTreeMap::new(),
                        iter_stack: Vec::new(),
                        next_index: 0,
                    },
                    rebase,
                },
            )?;
        }

        // The callee body runs in a fresh scope: params = inputs, env
        // passes through read-only, the caller's steps/vars are invisible
        // (07 §1.2 — hard boundary).
        let mut callee_frame = FrameState::new(callee, call_path.clone(), gated, frame.depth + 1);
        let ctl = self
            .exec_body(&mut callee_frame, call_path.clone(), &callee.body, 0)
            .await?;
        match ctl {
            Ctl::Continue | Ctl::HaltFail => {}
            Ctl::Abort => {
                // Unwind the frame so the ledger stays balanced; an
                // aborted run makes no semantic claim.
                self.append(
                    &call_path,
                    &RunLogPayload::CallFramePopped { outputs: None },
                )?;
                self.append(
                    &call_path,
                    &RunLogPayload::StepExited {
                        provider_state_summary: None,
                        state: StepState::Aborted,
                        output: None,
                        localized: Vec::new(),
                        localization_gaps: Vec::new(),
                    },
                )?;
                return Ok(Ctl::Abort);
            }
            // Suspension/blocking leaves the frame live: resume falls back
            // into the exact position (07 §4.6), never restarts the frame.
            other => return Ok(other),
        }

        // Outbound gate: declared outputs evaluated in the *callee* scope,
        // schema-validated, snapshotted (07 §1.1). Only a completed body
        // has outputs; a halted callee pops without them.
        let outputs = if matches!(ctl, Ctl::Continue) {
            match self.call_outputs_gate(callee, &callee_frame) {
                Ok(outputs) => Some(outputs),
                Err(message) => {
                    self.append(
                        &call_path,
                        &RunLogPayload::CallFramePopped { outputs: None },
                    )?;
                    return self
                        .settle_error(
                            frame,
                            &call_path,
                            &step_id,
                            VerdictStatus::Fail,
                            format!("callee outputs failed the outbound gate: {message}"),
                        )
                        .await;
                }
            }
        } else {
            None
        };
        self.append(
            &call_path,
            &RunLogPayload::CallFramePopped {
                outputs: outputs.clone(),
            },
        )?;

        // The call step's verdict *is* the callee's flow verdict
        // (spine §6.3); `degraded` propagates verbatim and participates in
        // the caller's fold.
        let callee_verdict = fold_flow_verdict(&callee_frame.fold, callee.verdict_policy);
        let mut status = None;
        let mut verdict_seq = None;
        if let Some(folded) = callee_verdict {
            let folded = FoldedVerdict {
                status: folded.status,
                degraded: folded.degraded,
                summary: format!(
                    "callee '{}' flow verdict: {}",
                    callee.flow_id, folded.summary
                ),
            };
            let seq = self
                .record_step_verdict(
                    &call_path,
                    &folded,
                    Vec::new(),
                    None,
                    EvidenceManifest::default(),
                )
                .await?;
            verdict_seq = Some(seq);
            frame.seed_verdict(&step_id, folded.status, folded.degraded);
            status = Some(folded.status);
        }

        // Handler consultation on the call step's own verdict (the callee
        // handled its internal failures itself; this hook is the caller's
        // policy about the aggregate). Re-invocation dispositions (retry /
        // repair-then-re-call) need the 07 §1 attempt-framed full re-call
        // — a typed M2 refusal, registered.
        if matches!(
            status,
            Some(VerdictStatus::Fail) | Some(VerdictStatus::Unknown)
        ) {
            let hook = if status == Some(VerdictStatus::Fail) {
                HandlerHook::OnFail
            } else {
                HandlerHook::OnUnknown
            };
            match self
                .consult_hook(frame, &call_path, step.base.handlers.as_deref(), hook, None)
                .await?
            {
                Consulted::None | Consulted::RepairFailed => {}
                Consulted::Continue => {
                    self.append(
                        &call_path,
                        &RunLogPayload::StepExited {
                            provider_state_summary: None,
                            state: StepState::Judged,
                            output: outputs.clone(),
                            localized: Vec::new(),
                            localization_gaps: Vec::new(),
                        },
                    )?;
                    if let Some(outputs) = outputs {
                        frame.outputs.insert(step_id.as_str().to_owned(), outputs);
                    }
                    return Ok(Ctl::Continue);
                }
                Consulted::Abort => {
                    self.append(
                        &call_path,
                        &RunLogPayload::StepExited {
                            provider_state_summary: None,
                            state: StepState::Aborted,
                            output: None,
                            localized: Vec::new(),
                            localization_gaps: Vec::new(),
                        },
                    )?;
                    return Ok(Ctl::Abort);
                }
                Consulted::Escalated {
                    status: ruled,
                    summary: ruled_summary,
                    evidence: ruling_evidence,
                } => {
                    let folded = FoldedVerdict {
                        status: ruled,
                        degraded: false,
                        summary: ruled_summary,
                    };
                    let supersedes = verdict_seq.map(|seq| format!("seq:{seq}"));
                    let (cited, manifest) = escalate_verdict_material(&ruling_evidence);
                    let ruled_seq = self
                        .record_step_verdict(&call_path, &folded, cited, supersedes, manifest)
                        .await?;
                    self.link_ruling_evidence(ruled_seq, &ruling_evidence)?;
                    frame.reseed_last(&step_id, ruled, false);
                    self.append(
                        &call_path,
                        &RunLogPayload::StepExited {
                            provider_state_summary: None,
                            state: StepState::Judged,
                            output: outputs.clone(),
                            localized: Vec::new(),
                            localization_gaps: Vec::new(),
                        },
                    )?;
                    if let Some(outputs) = outputs {
                        frame.outputs.insert(step_id.as_str().to_owned(), outputs);
                    }
                    return Ok(if ruled == VerdictStatus::Fail {
                        Ctl::HaltFail
                    } else {
                        Ctl::Continue
                    });
                }
                Consulted::Retry(_) | Consulted::Repaired | Consulted::RepairDone => {
                    return Err(RunnerError::M0Unsupported {
                        detail: format!(
                            "handler re-invocation dispositions on call step '{step_id}' need the 07 §1 \
                             attempt-framed full re-call — not in the M2 subset \
                             (escalate/continue/abort are supported)"
                        ),
                    });
                }
                Consulted::Pending(pending) => {
                    return Ok(Ctl::AwaitHuman(pending));
                }
                Consulted::Propagate(inner) => return Ok(inner),
            }
        }

        self.append(
            &call_path,
            &RunLogPayload::StepExited {
                provider_state_summary: None,
                state: StepState::Judged,
                output: outputs.clone(),
                localized: Vec::new(),
                localization_gaps: Vec::new(),
            },
        )?;
        if let Some(outputs) = outputs {
            frame.outputs.insert(step_id.as_str().to_owned(), outputs);
        }
        if matches!(ctl, Ctl::HaltFail) || status == Some(VerdictStatus::Fail) {
            Ok(Ctl::HaltFail)
        } else {
            Ok(Ctl::Continue)
        }
    }

    /// The outbound gate: callee `outputs` declarations evaluated over the
    /// callee frame's scope and validated against their schemas.
    fn call_outputs_gate(
        &self,
        callee: &FlowIR,
        callee_frame: &FrameState<'a>,
    ) -> Result<Value, String> {
        let scope = callee_frame.scope(&self.env, None);
        let mut outputs = Map::new();
        for decl in &callee.outputs {
            let value = pointlock_expr::eval(&decl.from, &scope)
                .map_err(|error| format!("output '{}' evaluation failed: {error}", decl.name))?;
            jsonschema::validate(decl.schema.as_value(), &value)
                .map_err(|error| format!("output '{}' failed its schema: {error}", decl.name))?;
            outputs.insert(decl.name.as_str().to_owned(), value);
        }
        Ok(Value::Object(outputs))
    }

    // ─── if steps ───────────────────────────────────────────────────────────

    async fn exec_if(
        &mut self,
        frame: &mut FrameState<'a>,
        step_path: RunPath,
        step: &'a IfStepIR,
    ) -> Result<Ctl, RunnerError> {
        let step_id = step.base.step_id.clone();
        let key = instance_key(&step_path);
        // A suspension-opened span reuses the archived branch decision —
        // the snapshot rule (spine §6.6) applies to control values too.
        let cond_value = match self.open_span_inputs(&key) {
            Some(archived) => archived.get("cond").cloned().unwrap_or(Value::Null),
            None => {
                let scope = frame.scope(&self.env, None);
                match pointlock_expr::eval(&step.cond, &scope) {
                    Ok(value) => value,
                    Err(error) => {
                        self.enter_step(&step_path, &step.base, Value::Null)?;
                        return self
                            .settle_error(
                                frame,
                                &step_path,
                                &step_id,
                                VerdictStatus::Fail,
                                format!("if cond evaluation failed: {error}"),
                            )
                            .await;
                    }
                }
            }
        };
        // Strict boolean (02 §4.5): anything else is a compiler/expression
        // bug signal — step fail, no branch is taken.
        let Some(cond) = cond_value.as_bool() else {
            self.enter_step(
                &step_path,
                &step.base,
                serde_json::json!({ "cond": cond_value }),
            )?;
            return self
                .settle_error(
                    frame,
                    &step_path,
                    &step_id,
                    VerdictStatus::Fail,
                    format!("if cond evaluated to a non-boolean value: {cond_value}"),
                )
                .await;
        };
        self.enter_step(&step_path, &step.base, serde_json::json!({ "cond": cond }))?;

        if let Some(ctl) = self.probe_or_note(frame, &step_path, &step.base).await? {
            return Ok(ctl);
        }

        let empty: &'a [StepIR] = &[];
        let (selected, unselected): (&'a [StepIR], &'a [StepIR]) = if cond {
            (&step.then, step.r#else.as_deref().unwrap_or(empty))
        } else {
            (step.r#else.as_deref().unwrap_or(empty), &step.then)
        };
        // The unselected branch's steps each leave an
        // entered(null)/exited(skipped) pair — ledger completeness (the
        // blocked precedent). Recorded before the selected branch runs so
        // a mid-branch suspension leaves a complete account.
        self.stash_open_span_summaries().await;
        self.record_pairs(&step_path, unselected, StepState::Skipped)?;

        let ctl = self
            .exec_body(frame, step_path.clone(), selected, 0)
            .await?;
        match ctl {
            Ctl::Continue | Ctl::HaltFail => {
                // Containers yield no verdict of their own (R4): the exit
                // closes the span; child verdicts already folded.
                self.append(
                    &step_path,
                    &RunLogPayload::StepExited {
                        provider_state_summary: None,
                        state: StepState::Judged,
                        output: None,
                        localized: Vec::new(),
                        localization_gaps: Vec::new(),
                    },
                )?;
                Ok(ctl)
            }
            Ctl::Abort => {
                self.append(
                    &step_path,
                    &RunLogPayload::StepExited {
                        provider_state_summary: None,
                        state: StepState::Aborted,
                        output: None,
                        localized: Vec::new(),
                        localization_gaps: Vec::new(),
                    },
                )?;
                Ok(Ctl::Abort)
            }
            other => Ok(other),
        }
    }

    // ─── foreach steps ──────────────────────────────────────────────────────

    async fn exec_foreach(
        &mut self,
        frame: &mut FrameState<'a>,
        step_path: RunPath,
        step: &'a ForeachStepIR,
    ) -> Result<Ctl, RunnerError> {
        let step_id = step.base.step_id.clone();
        let key = instance_key(&step_path);
        let items_value = match self.open_span_inputs(&key) {
            Some(archived) => archived.get("items").cloned().unwrap_or(Value::Null),
            None => {
                let scope = frame.scope(&self.env, None);
                match pointlock_expr::eval(&step.items, &scope) {
                    Ok(value) => value,
                    Err(error) => {
                        self.enter_step(&step_path, &step.base, Value::Null)?;
                        return self
                            .settle_error(
                                frame,
                                &step_path,
                                &step_id,
                                VerdictStatus::Fail,
                                format!("foreach items evaluation failed: {error}"),
                            )
                            .await;
                    }
                }
            }
        };
        let Some(items) = items_value.as_array().cloned() else {
            self.enter_step(
                &step_path,
                &step.base,
                serde_json::json!({ "items": items_value, "as": step.r#as.as_str() }),
            )?;
            return self
                .settle_error(
                    frame,
                    &step_path,
                    &step_id,
                    VerdictStatus::Fail,
                    format!("foreach items evaluated to a non-array value: {items_value}"),
                )
                .await;
        };
        // The snapshot carries `{ items, as }`: the position authority for
        // the positional (index-keyed) resume regime and the fold's
        // IterState carrier.
        self.enter_step(
            &step_path,
            &step.base,
            serde_json::json!({ "items": items, "as": step.r#as.as_str() }),
        )?;

        if let Some(ctl) = self.probe_or_note(frame, &step_path, &step.base).await? {
            return Ok(ctl);
        }

        for (index, item) in items.iter().enumerate() {
            frame
                .iters
                .push((step.r#as.as_str().to_owned(), item.clone()));
            let mut iter_prefix = step_path.clone();
            iter_prefix.push(PathFrame::Iteration {
                index: index as u64,
                key: None,
            });
            let ctl = self.exec_body(frame, iter_prefix, &step.body, 0).await?;
            frame.iters.pop();
            match ctl {
                Ctl::Continue => {}
                Ctl::HaltFail => {
                    // The failing iteration already blocked its own tail;
                    // later iterations never materialize as instances.
                    self.append(
                        &step_path,
                        &RunLogPayload::StepExited {
                            provider_state_summary: None,
                            state: StepState::Judged,
                            output: None,
                            localized: Vec::new(),
                            localization_gaps: Vec::new(),
                        },
                    )?;
                    return Ok(Ctl::HaltFail);
                }
                Ctl::Abort => {
                    self.append(
                        &step_path,
                        &RunLogPayload::StepExited {
                            provider_state_summary: None,
                            state: StepState::Aborted,
                            output: None,
                            localized: Vec::new(),
                            localization_gaps: Vec::new(),
                        },
                    )?;
                    return Ok(Ctl::Abort);
                }
                other => return Ok(other),
            }
        }
        self.append(
            &step_path,
            &RunLogPayload::StepExited {
                provider_state_summary: None,
                state: StepState::Judged,
                output: None,
                localized: Vec::new(),
                localization_gaps: Vec::new(),
            },
        )?;
        Ok(Ctl::Continue)
    }

    // ─── let steps ──────────────────────────────────────────────────────────

    async fn exec_let(
        &mut self,
        frame: &mut FrameState<'a>,
        step_path: RunPath,
        step: &'a LetStepIR,
    ) -> Result<Ctl, RunnerError> {
        let step_id = step.base.step_id.clone();
        let key = instance_key(&step_path);
        let evaluated = match self.open_span_inputs(&key) {
            // The archived snapshot *is* the bindings product (pure,
            // deterministic) — never re-evaluated on resume.
            Some(Value::Object(map)) => map,
            Some(_) | None => {
                let scope = frame.scope(&self.env, None);
                let mut evaluated = Map::new();
                let mut error = None;
                for (name, expr) in step.bindings.iter() {
                    // SSA single assignment: rebinding is refused by the
                    // compiler check phase; this is the runtime defense
                    // line against hand-built IR.
                    if frame.vars.contains_key(name.as_str()) {
                        error = Some(format!(
                            "binding '{name}' rebinds an existing var (SSA single assignment)"
                        ));
                        break;
                    }
                    match pointlock_expr::eval(expr, &scope) {
                        Ok(value) => {
                            evaluated.insert(name.as_str().to_owned(), value);
                        }
                        Err(eval_error) => {
                            error =
                                Some(format!("binding '{name}' evaluation failed: {eval_error}"));
                            break;
                        }
                    }
                }
                if let Some(message) = error {
                    self.enter_step(&step_path, &step.base, Value::Null)?;
                    return self
                        .settle_error(
                            frame,
                            &step_path,
                            &step_id,
                            VerdictStatus::Fail,
                            format!("let bindings failed: {message}"),
                        )
                        .await;
                }
                evaluated
            }
        };
        // The ready snapshot carries the evaluated bindings — the resume
        // walk re-seeds `vars.*` from exactly this carrier.
        self.enter_step(&step_path, &step.base, Value::Object(evaluated.clone()))?;
        if let Some(ctl) = self.probe_or_note(frame, &step_path, &step.base).await? {
            return Ok(ctl);
        }
        self.append(
            &step_path,
            &RunLogPayload::StepExited {
                provider_state_summary: None,
                state: StepState::Judged,
                output: None,
                localized: Vec::new(),
                localization_gaps: Vec::new(),
            },
        )?;
        for (name, value) in evaluated {
            frame.vars.insert(name, value);
        }
        Ok(Ctl::Continue)
    }

    // ─── assert steps ───────────────────────────────────────────────────────

    async fn exec_assert(
        &mut self,
        frame: &mut FrameState<'a>,
        step_path: RunPath,
        step: &'a AssertStepIR,
    ) -> Result<Ctl, RunnerError> {
        let step_id = step.base.step_id.clone();
        // An assert step resolves no input expressions; the span still
        // opens with an explicitly-null snapshot.
        self.enter_step(&step_path, &step.base, Value::Null)?;
        if let Some(ctl) = self.probe_or_note(frame, &step_path, &step.base).await? {
            return Ok(ctl);
        }
        let needs = VerifyNeeds::of(&step.assertions);
        let mut last_verdict_seq: Option<u64> = None;
        let mut seeded = false;
        let mut active_retry: Option<(RetryPolicy, u32)> = None;
        loop {
            let material = match &step.observe {
                // Fresh capture: one `session.observe`, localized through the
                // same observing pipeline as action observations.
                ObservationSource::Fresh(_) => {
                    let mut anchor = step_path.clone();
                    anchor.push(PathFrame::Phase {
                        phase: Phase::Observe,
                    });
                    self.fresh_material(&needs, &anchor).await?
                }
                // Archive reuse: the referenced action step's localized
                // observation material — zero device I/O, offline
                // re-judgeable by construction.
                ObservationSource::FromStep(from) => {
                    let which = from.which;
                    match frame.observed.get(from.from_step.as_str()) {
                        None => ObserveMaterial::absent(&format!(
                            "step '{}' has no archived observation in this frame",
                            from.from_step
                        )),
                        Some(observed) => {
                            let wanted = match which {
                                pointlock_ir::ObservationWhich::After => &observed.after_id,
                                pointlock_ir::ObservationWhich::Before => &observed.before_id,
                            };
                            match wanted {
                                None => ObserveMaterial::absent(&format!(
                                    "step '{}' recorded no {:?} observation",
                                    from.from_step, which
                                )),
                                Some(observation_id) => {
                                    match observed
                                        .observations
                                        .iter()
                                        .find(|record| record.observation_id == *observation_id)
                                    {
                                        None => ObserveMaterial::absent(
                                            "the referenced observation was never localized",
                                        ),
                                        Some(record) => {
                                            material_from_observation(self.store, record)
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            };
            let scope = frame.scope(&self.env, None);
            let mut outcomes = Vec::with_capacity(step.assertions.len());
            let mut degraded_verify = false;
            for assertion in &step.assertions {
                let evaluated = match &assertion.predicate {
                    PredicateIR::Expr { expr } => EvaluatedAssertion {
                        record: eval_expr_assertion(assertion, expr, &scope),
                        degraded_verify: false,
                    },
                    _ => {
                        eval_observed_assertion(assertion, &material, self.vision.as_deref()).await
                    }
                };
                degraded_verify |= evaluated.degraded_verify;
                outcomes.push(evaluated.record);
            }
            for outcome in &outcomes {
                let mut path = step_path.clone();
                path.push(PathFrame::Phase {
                    phase: Phase::Assert,
                });
                path.push(PathFrame::Assertion {
                    assert_id: outcome.assert_id.clone(),
                });
                self.append(
                    &path,
                    &RunLogPayload::AssertionEvaluated {
                        outcome: outcome.clone(),
                    },
                )?;
            }
            // Assert steps declare ≥ 1 assertion, so a verdict always folds.
            let folded =
                fold_step_verdict(&outcomes, false, degraded_verify, frame.flow.verdict_policy);
            let supersedes = last_verdict_seq.map(|seq| format!("seq:{seq}"));
            let seq = self
                .record_step_verdict(
                    &step_path,
                    &folded,
                    Vec::new(),
                    supersedes,
                    EvidenceManifest::default(),
                )
                .await?;
            last_verdict_seq = Some(seq);
            if seeded {
                frame.reseed_last(&step_id, folded.status, folded.degraded);
            } else {
                frame.seed_verdict(&step_id, folded.status, folded.degraded);
                seeded = true;
            }
            if folded.status == VerdictStatus::Pass {
                self.append(
                    &step_path,
                    &RunLogPayload::StepExited {
                        provider_state_summary: None,
                        state: StepState::Judged,
                        output: None,
                        localized: Vec::new(),
                        localization_gaps: Vec::new(),
                    },
                )?;
                return Ok(Ctl::Continue);
            }

            // In-force handler retry budget (observe + assert re-entry is
            // readonly by construction — always replay-safe).
            if let Some((policy, used)) = active_retry.take()
                && used < policy.max_attempts
            {
                self.backoff_policy(&policy, used).await;
                active_retry = Some((policy, used + 1));
                continue;
            }

            let hook = if folded.status == VerdictStatus::Fail {
                HandlerHook::OnFail
            } else {
                HandlerHook::OnUnknown
            };
            match self
                .consult_hook(frame, &step_path, step.base.handlers.as_deref(), hook, None)
                .await?
            {
                Consulted::None | Consulted::RepairFailed => {
                    self.append(
                        &step_path,
                        &RunLogPayload::StepExited {
                            provider_state_summary: None,
                            state: StepState::Judged,
                            output: None,
                            localized: Vec::new(),
                            localization_gaps: Vec::new(),
                        },
                    )?;
                    return Ok(if folded.status == VerdictStatus::Fail {
                        Ctl::HaltFail
                    } else {
                        Ctl::Continue
                    });
                }
                Consulted::Continue => {
                    self.append(
                        &step_path,
                        &RunLogPayload::StepExited {
                            provider_state_summary: None,
                            state: StepState::Judged,
                            output: None,
                            localized: Vec::new(),
                            localization_gaps: Vec::new(),
                        },
                    )?;
                    return Ok(Ctl::Continue);
                }
                Consulted::Abort => {
                    self.append(
                        &step_path,
                        &RunLogPayload::StepExited {
                            provider_state_summary: None,
                            state: StepState::Aborted,
                            output: None,
                            localized: Vec::new(),
                            localization_gaps: Vec::new(),
                        },
                    )?;
                    return Ok(Ctl::Abort);
                }
                Consulted::Escalated {
                    status: ruled,
                    summary: ruled_summary,
                    evidence: ruling_evidence,
                } => {
                    let folded = FoldedVerdict {
                        status: ruled,
                        degraded: false,
                        summary: ruled_summary,
                    };
                    let supersedes = last_verdict_seq.map(|seq| format!("seq:{seq}"));
                    let (cited, manifest) = escalate_verdict_material(&ruling_evidence);
                    let ruled_seq = self
                        .record_step_verdict(&step_path, &folded, cited, supersedes, manifest)
                        .await?;
                    self.link_ruling_evidence(ruled_seq, &ruling_evidence)?;
                    frame.reseed_last(&step_id, ruled, false);
                    self.append(
                        &step_path,
                        &RunLogPayload::StepExited {
                            provider_state_summary: None,
                            state: StepState::Judged,
                            output: None,
                            localized: Vec::new(),
                            localization_gaps: Vec::new(),
                        },
                    )?;
                    return Ok(if ruled == VerdictStatus::Fail {
                        Ctl::HaltFail
                    } else {
                        Ctl::Continue
                    });
                }
                Consulted::Retry(policy) => {
                    self.backoff_policy(&policy, 0).await;
                    active_retry = Some((policy, 1));
                }
                Consulted::Repaired | Consulted::RepairDone => {}
                Consulted::Pending(pending) => {
                    return Ok(Ctl::AwaitHuman(pending));
                }
                Consulted::Propagate(ctl) => return Ok(ctl),
            }
            // Loop: re-observe and re-evaluate (fresh material each round;
            // fromStep archives are stable, so a re-round only makes sense
            // after a repair — both are honest re-judgements on new/declared
            // world state).
        }
    }

    // ─── observing / localization ───────────────────────────────────────────

    /// The observing phase: localize evidence (spine §6.6 — provider-side
    /// retention is not guaranteed) and record the before/after
    /// observations. Returns the provider asset refs cited by the verdict,
    /// the [`ObserveMaterial`] of the *after* observation, and the
    /// localized records (the `fromStep` archive of this step).
    async fn observing(
        &mut self,
        step: &'a ActionStepIR,
        step_path: &RunPath,
        attempt_n: u64,
        result: &ActionResult,
        settled_seq: u64,
    ) -> Result<(Vec<AssetRef>, ObserveMaterial, StepObs, EvidenceManifest), RunnerError> {
        let mut observe_path = step_path.clone();
        observe_path.push(PathFrame::Attempt { n: attempt_n });
        observe_path.push(PathFrame::Phase {
            phase: Phase::Observe,
        });
        let needs = VerifyNeeds::of(&step.assertions);
        let mut cited = Vec::new();
        let mut material =
            ObserveMaterial::absent("the action result carries no after observation");
        let mut observed = StepObs {
            observations: Vec::new(),
            before_id: None,
            after_id: None,
        };
        if let Some(observation) = &result.before {
            let record = self
                .localize_observation(observation, &mut cited, None)
                .await?;
            // file-before-row-before-log: the bytes are on disk and indexed
            // before this event references them.
            self.append(
                &observe_path,
                &RunLogPayload::ObservationRecorded {
                    observation: record.clone(),
                },
            )?;
            observed.before_id = Some(record.observation_id.clone());
            observed.observations.push(record);
        }
        if let Some(observation) = &result.after {
            material = ObserveMaterial::default();
            let record = self
                .localize_observation(observation, &mut cited, Some((&needs, &mut material)))
                .await?;
            self.append(
                &observe_path,
                &RunLogPayload::ObservationRecorded {
                    observation: record.clone(),
                },
            )?;
            observed.after_id = Some(record.observation_id.clone());
            observed.observations.push(record);
        }
        let mut manifest = EvidenceManifest::default();
        for asset in &result.evidence {
            // Bounded manifest (the cited-list cap's sibling): entries
            // beyond the cap are recorded as typed gaps — bounded DTOs
            // must not silently truncate (spine §10 bounded-render
            // discipline).
            if manifest.localized.len() >= VERDICT_EVIDENCE_MAX_ENTRIES {
                manifest.gaps.push(pointlock_ir::EvidenceGap {
                    asset: asset.clone(),
                    reason: format!(
                        "evidence cap exceeded ({VERDICT_EVIDENCE_MAX_ENTRIES} max per judgment)"
                    ),
                });
                continue;
            }
            // Auxiliary settlement evidence (item ③, 2026-07-18): a
            // success joins this judgment's localized manifest (and the
            // evidence_ref index); a failure is a TYPED gap on the
            // verdict record — never a silent omission (principle 4/R4).
            match self.try_localize(asset).await? {
                Ok((evidence, _bytes)) => {
                    self.store.link_evidence(
                        &self.run_id,
                        settled_seq,
                        &asset.id,
                        &evidence.sha256,
                    )?;
                    cited.push(asset.clone());
                    manifest.localized.push(evidence);
                }
                Err(reason) => {
                    manifest.gaps.push(pointlock_ir::EvidenceGap {
                        asset: asset.clone(),
                        reason,
                    });
                }
            }
        }
        Ok((cited, material, observed, manifest))
    }

    /// Localizes one observation's evidence and builds its durable record.
    /// Omissions are typed data and pass through verbatim; a localization
    /// failure leaves the affected field absent and feeds the dependent
    /// verify channel a typed gap — the run is never aborted over it (M2
    /// degradation rule; principle 4 routes it to `unknown`).
    async fn localize_observation(
        &mut self,
        observation: &Observation,
        cited: &mut Vec<AssetRef>,
        mut material: Option<(&VerifyNeeds, &mut ObserveMaterial)>,
    ) -> Result<ObservationRecord, RunnerError> {
        let screenshot = match &observation.screenshot {
            Some(asset) => match self.try_localize(asset).await? {
                Ok((evidence, bytes)) => {
                    cited.push(asset.clone());
                    if let Some((needs, material)) = material.as_mut()
                        && needs.vision
                    {
                        material.screenshot = Some((bytes, asset.media_type.clone()));
                    }
                    Some(evidence)
                }
                Err(gap) => {
                    if let Some((needs, material)) = material.as_mut()
                        && needs.vision
                    {
                        material.screenshot_gap = Some(gap);
                    }
                    None
                }
            },
            None => {
                if let Some((needs, material)) = material.as_mut()
                    && needs.vision
                {
                    material.screenshot_gap = Some(match observation.screenshot_omission {
                        Some(reason) => {
                            format!("screenshot omitted by the provider ({reason:?})")
                        }
                        None => "the observation carries no screenshot".to_owned(),
                    });
                }
                None
            }
        };
        let mut ui_snapshot_omission = observation.ui_snapshot_omission;
        let ui_snapshot = match &observation.ui_snapshot {
            Some(snapshot) => {
                let wants_tree = matches!(&material, Some((needs, _)) if needs.ui_tree);
                let localized = if wants_tree {
                    self.localize_ui_tree(
                        observation,
                        snapshot,
                        &mut material,
                        &mut ui_snapshot_omission,
                    )
                    .await?
                } else {
                    // No assertion consumes the tree: localize the
                    // provider-side evidence object as before (retention is
                    // not guaranteed), without the `ui.snapshot.get` pull.
                    // A failure is a gap, not an abort.
                    self.try_localize(&snapshot.evidence)
                        .await?
                        .ok()
                        .map(|(evidence, _bytes)| evidence)
                };
                // Cite only what actually landed (item ③ review fix): a
                // citation the local library cannot serve would be a
                // silent gallery omission. Citation granularity is the
                // ASSET (the pointer); the byte-exact truth (sha256 +
                // localPath of what was actually stored — the canonical
                // tree on the wants_tree path) lives on the record's
                // EvidenceRef, which is what consumers resolve.
                if localized.is_some() {
                    cited.push(snapshot.evidence.clone());
                }
                localized
            }
            None => {
                if let Some((needs, material)) = material.as_mut()
                    && needs.ui_tree
                {
                    material.ui_tree_gap = Some(match observation.ui_snapshot_omission {
                        Some(reason) => {
                            format!("uiSnapshot omitted by the provider ({reason:?})")
                        }
                        None => "the observation carries no uiSnapshot".to_owned(),
                    });
                }
                None
            }
        };
        // Finiteness guard: `scaleFactor` is the only f64 in the durable
        // record domain, and serde_json writes a non-finite f64 as `null`
        // — which round-trips into a permanent ledger read failure (every
        // refold/verify/projection of the run errors). A provider that
        // reports a non-finite viewport gets the field honestly absent
        // rather than a poisoned ledger (clamping would falsify evidence;
        // aborting the run over a cosmetic field would violate the M2
        // degradation rule).
        let viewport = observation
            .viewport
            .scale_factor
            .is_finite()
            .then(|| observation.viewport.clone());
        Ok(ObservationRecord {
            observation_id: observation.id.clone(),
            captured_at_ms: observation.captured_at_ms,
            viewport,
            screenshot,
            screenshot_omission: observation.screenshot_omission,
            ui_snapshot,
            ui_snapshot_omission,
        })
    }

    /// Pulls the observation's normalized UI tree (`ui.snapshot.get`),
    /// localizes the canonical bytes, and feeds the verify-chain material.
    /// A typed `Unavailable` — or a provider error on the dereference — is
    /// data, not a run abort: it becomes the uiTree channel's gap (unknown
    /// propagation), never a fabricated tree.
    async fn localize_ui_tree(
        &mut self,
        observation: &Observation,
        snapshot: &pointlock_ir::UiSnapshotRef,
        material: &mut Option<(&VerifyNeeds, &mut ObserveMaterial)>,
        ui_snapshot_omission: &mut Option<UiSnapshotOmissionReason>,
    ) -> Result<Option<EvidenceRef>, RunnerError> {
        match self.session.ui_snapshot(&observation.id).await {
            Ok(UiSnapshotOutcome::Available { snapshot: tree }) => {
                let bytes =
                    serde_json::to_vec(&tree).expect("a serde_json::Value always serializes");
                let put = self.store.put_evidence(&bytes, "application/json")?;
                if let Some((_, material)) = material.as_mut() {
                    material.ui_tree = Some(bytes);
                }
                Ok(Some(EvidenceRef {
                    asset: snapshot.evidence.clone(),
                    sha256: put.sha256,
                    local_path: put.local_path,
                }))
            }
            Ok(UiSnapshotOutcome::Unavailable { reason }) => {
                *ui_snapshot_omission = Some(reason);
                if let Some((_, material)) = material.as_mut() {
                    material.ui_tree_gap =
                        Some(format!("uiSnapshot dereference unavailable ({reason:?})"));
                }
                Ok(None)
            }
            Err(error) => {
                if let Some((_, material)) = material.as_mut() {
                    material.ui_tree_gap = Some(format!("uiSnapshot dereference failed: {error}"));
                }
                Ok(None)
            }
        }
    }

    /// Fetches an asset's bytes into the content-addressed evidence area.
    /// The outer `Result` is infrastructure (store I/O — still fatal); the
    /// inner one is the typed localization gap (fetch unsupported/ruptured,
    /// integrity mismatch — the run degrades, never aborts; 04 §4.3 is
    /// honored by *not using* mismatched bytes, with the reason on record).
    async fn try_localize(
        &mut self,
        asset: &AssetRef,
    ) -> Result<Result<(EvidenceRef, Vec<u8>), String>, RunnerError> {
        let mut stream = match self.session.fetch_evidence(asset).await {
            Ok(stream) => stream,
            Err(error) => {
                return Ok(Err(format!(
                    "evidence fetch failed for asset {}: {error}",
                    asset.id
                )));
            }
        };
        let mut bytes = Vec::new();
        while let Some(chunk) = stream.next().await {
            match chunk {
                Ok(part) => bytes.extend(part),
                Err(error) => {
                    return Ok(Err(format!(
                        "evidence stream failed for asset {}: {error}",
                        asset.id
                    )));
                }
            }
        }
        let put = self.store.put_evidence(&bytes, &asset.media_type)?;
        if let Some(expected) = &asset.sha256
            && expected != &put.sha256
        {
            return Ok(Err(format!(
                "evidence integrity failure for asset {}: sha256 {} != declared {expected}",
                asset.id, put.sha256
            )));
        }
        let evidence = EvidenceRef {
            asset: asset.clone(),
            sha256: put.sha256,
            local_path: put.local_path,
        };
        Ok(Ok((evidence, bytes)))
    }

    /// Appends `verdictRecorded` and writes the verdict back through the
    /// provider (`verdict.record` — the daemon only validates and
    /// persists). Returns the `verdictRecorded` seq (evidence linking).
    async fn record_step_verdict(
        &mut self,
        path: &RunPath,
        folded: &FoldedVerdict,
        cited: Vec<AssetRef>,
        supersedes: Option<String>,
        manifest: EvidenceManifest,
    ) -> Result<u64, RunnerError> {
        let verdict = Verdict {
            status: folded.status,
            degraded: folded.degraded,
            // The local ledger keeps the FULL summary — the 16384-char
            // cap is a wire hard limit, applied at write-back only
            // (04 §5).
            summary: folded.summary.clone(),
            evidence: cited
                .into_iter()
                .take(VERDICT_EVIDENCE_MAX_ENTRIES)
                .collect(),
            supersedes,
        };
        // Failure-instant capture (07 §2.2): the verdict instant IS the
        // failure instant, so the capture runs FIRST — before the
        // write-back RPC below can delay it on a degraded daemon. The
        // profile rides the span's exit through the `append` attach; a
        // superseding pass discards it. Deliberate cost: a retry round
        // that will be superseded still pays a capture (bounded by the
        // 2s budget) — skipping it would need the consult outcome, which
        // is only known after the handler runs, and a wrong skip would
        // ship a summary-less fail exit. Correctness over the bounded
        // RPC.
        let key = instance_key(path);
        match verdict.status {
            VerdictStatus::Fail | VerdictStatus::Unknown => {
                let summary = self.capture_summary().await;
                self.pending_summaries.insert(key, summary);
            }
            VerdictStatus::Pass => {
                self.pending_summaries.remove(&key);
            }
        }
        // Remote archival before the append so its outcome can ride the
        // event; it is archival of an already-derived verdict, not a
        // world effect, so the actionIntent WAL discipline does not
        // apply. A failure never changes the local verdict and never
        // aborts the run — it is annotated here and surfaced by the
        // report (04 §5). Deliberate cost: on a hung daemon the
        // `verdictRecorded` `at_ms` trails the fold instant by the
        // bounded write-back budget — the price of carrying the outcome
        // on the event under the closed §6.1 vocabulary.
        let remote_archival_error = self.try_verdict_writeback(&verdict).await;
        let seq = self.append(
            path,
            &RunLogPayload::VerdictRecorded {
                verdict: verdict.clone(),
                localized: manifest.localized,
                localization_gaps: manifest.gaps,
                remote_archival_error,
            },
        )?;
        Ok(seq)
    }

    /// `ProviderSession::record_verdict` write-back with the wire caps
    /// applied on the runner side (compaction is the runner's job,
    /// 04 §5). Returns the failure rendered for the ledger annotation —
    /// never an error: remote archival failure must not change the local
    /// verdict or abort the run (04 §5, the RunLog is the sole truth).
    async fn try_verdict_writeback(&mut self, verdict: &Verdict) -> Option<String> {
        self.session
            .record_verdict(VerdictWrite {
                status: verdict.status,
                summary: cap_wire_summary(verdict),
                evidence: verdict
                    .evidence
                    .iter()
                    .take(VERDICT_EVIDENCE_MAX_ENTRIES)
                    .cloned()
                    .collect(),
            })
            .await
            .err()
            .map(|error| format!("remote archival failed: {error}"))
    }

    /// Best-effort session teardown (04 §2.1: `end` must not block the
    /// runner's teardown when the transport is already gone).
    async fn end_session(&mut self, outcome: SessionOutcome) {
        let _ = self.session.end(outcome, None).await;
    }
}

/// The inbound gate of a call step (07 §1.1): apply the callee's declared
/// param defaults, refuse undeclared inputs and missing required params,
/// and validate every present value against its `ParamDecl.schema`.
fn call_inputs_gate(
    callee: &FlowIR,
    inputs: Map<String, Value>,
) -> Result<Map<String, Value>, String> {
    for key in inputs.keys() {
        if !callee
            .params
            .iter()
            .any(|decl: &ParamDecl| decl.name.as_str() == key)
        {
            return Err(format!(
                "input '{key}' is not a declared param of callee '{}'",
                callee.flow_id
            ));
        }
    }
    let gated =
        params_with_defaults(callee, Value::Object(inputs)).map_err(|error| error.to_string())?;
    for decl in &callee.params {
        if let Some(value) = gated.get(decl.name.as_str()) {
            jsonschema::validate(decl.schema.as_value(), value)
                .map_err(|error| format!("param '{}' failed its schema: {error}", decl.name))?;
        }
    }
    Ok(gated)
}

/// Applies declared param defaults over the supplied params/inputs;
/// missing required params without defaults are refused. Shared by the run
/// entry (run params) and the call step's inbound gate (07 §1.1).
pub(crate) fn params_with_defaults(
    flow: &FlowIR,
    params: Value,
) -> Result<Map<String, Value>, RunnerError> {
    let mut map = match params {
        Value::Object(map) => map,
        Value::Null => Map::new(),
        other => {
            return Err(RunnerError::InvalidParams {
                reason: format!("params must be a JSON object or null, got {other}"),
            });
        }
    };
    for decl in &flow.params {
        if map.contains_key(decl.name.as_str()) {
            continue;
        }
        if let Some(default) = &decl.default {
            map.insert(decl.name.as_str().to_owned(), default.clone());
        } else if decl.required {
            return Err(RunnerError::InvalidParams {
                reason: format!(
                    "required param '{}' is missing and has no default",
                    decl.name
                ),
            });
        }
    }
    Ok(map)
}

/// Which observation channels a set of assertions' verify chains consume —
/// decides what the observing phase must localize into
/// [`ObserveMaterial`] and what a fresh observe must want.
pub(crate) struct VerifyNeeds {
    /// Some assertion's chain contains `uiTree`.
    pub ui_tree: bool,
    /// Some assertion's chain contains `vision`.
    pub vision: bool,
}

impl VerifyNeeds {
    /// Scans the assertions (expr predicates consume no channel).
    pub fn of(assertions: &[AssertionIR]) -> Self {
        let mut needs = VerifyNeeds {
            ui_tree: false,
            vision: false,
        };
        for assertion in assertions {
            if matches!(assertion.predicate, PredicateIR::Expr { .. }) {
                continue;
            }
            for channel in &assertion.verify_via {
                match channel {
                    VerifyChannel::UiTree => needs.ui_tree = true,
                    VerifyChannel::Vision => needs.vision = true,
                    VerifyChannel::Dom => {}
                }
            }
        }
        needs
    }
}

/// Truncates a verdict summary to the provider wire cap (char-aware),
/// appending a content-hash pointer to the local full verdict when it
/// cuts (04 §5: the RunLog keeps the complete summary; the wire copy
/// points back at it).
pub(crate) fn cap_wire_summary(verdict: &Verdict) -> String {
    if verdict.summary.chars().count() <= VERDICT_SUMMARY_MAX_CHARS {
        return verdict.summary.clone();
    }
    let pointer = format!(
        " …[truncated; full local verdict {}]",
        pointlock_ir::domain_hash(
            "pointlock-runner/1/local-verdict",
            &serde_json::to_value(verdict).expect("a Verdict always serializes"),
        )
    );
    let keep = VERDICT_SUMMARY_MAX_CHARS.saturating_sub(pointer.chars().count());
    let mut capped: String = verdict.summary.chars().take(keep).collect();
    capped.push_str(&pointer);
    capped
}

/// The backoff delay before retry number `tries + 1` (spine §3
/// `RetryPolicy.backoffMs`).
fn backoff_ms(policy: &pointlock_ir::RetryPolicy, tries: u32) -> u64 {
    match &policy.backoff_ms {
        pointlock_ir::BackoffMs::Fixed(number) => number.as_f64().unwrap_or(0.0) as u64,
        pointlock_ir::BackoffMs::Schedule(schedule) => {
            let initial = schedule.initial.as_f64().unwrap_or(0.0);
            let factor = schedule.factor.as_f64().unwrap_or(1.0);
            let max = schedule.max.as_f64().unwrap_or(f64::MAX);
            let exponent = tries.saturating_sub(1);
            (initial * factor.powi(exponent as i32)).min(max) as u64
        }
    }
}

/// Whether an in-attempt retry is allowed (spine §6.5 mount point 1,
/// closed): only `action_failed_retryable`, `target_stale`, and — for
/// idempotent steps — `action_timed_out`, and only when the policy lists
/// the class and the budget is not exhausted.
fn retry_allowed(step: &ActionStepIR, class: ErrorClass, tries: u32) -> bool {
    let Some(policy) = &step.base.retry else {
        return false;
    };
    if tries >= policy.max_attempts {
        return false;
    }
    if !policy.retry_on.contains(&class) {
        return false;
    }
    match class {
        ErrorClass::ActionFailedRetryable | ErrorClass::TargetStale => true,
        ErrorClass::ActionTimedOut => step.idempotent,
        _ => false,
    }
}

/// Maps a non-succeeded terminal onto the closed `ErrorClass` taxonomy
/// (spine §5). When the wire code spells a class verbatim it is adopted
/// (mirrors the store fold's best-effort rule); otherwise `failed` maps by
/// the daemon-declared `retryable` flag.
pub(crate) fn classify(outcome: &ActionOutcome) -> ErrorClass {
    match outcome {
        ActionOutcome::Succeeded { .. } => {
            unreachable!("classify is only called on non-succeeded terminals")
        }
        ActionOutcome::Failed { error } => code_spelled_class(&error.code).unwrap_or({
            if error.retryable {
                ErrorClass::ActionFailedRetryable
            } else {
                ErrorClass::ActionFailedFinal
            }
        }),
        ActionOutcome::TimedOut { .. } => ErrorClass::ActionTimedOut,
        ActionOutcome::Cancelled { .. } => ErrorClass::ActionCancelled,
    }
}

fn code_spelled_class(code: &str) -> Option<ErrorClass> {
    serde_json::from_value(Value::String(code.to_owned())).ok()
}

fn terminal_message(outcome: &ActionOutcome) -> String {
    let error: &ErrorInfo = match outcome {
        ActionOutcome::Failed { error }
        | ActionOutcome::Cancelled { error }
        | ActionOutcome::TimedOut { error } => error,
        ActionOutcome::Succeeded { .. } => {
            unreachable!("terminal_message is only called on non-succeeded terminals")
        }
    };
    format!("{} ({})", error.message, error.code)
}

/// Whether the provider-reported execution mode is inside the attempt's
/// whitelist (§6.4 R-degrade). An absent execution report cannot be
/// audited and is accepted (the DeviceRail adapter always reports it).
fn execution_accepted(attempt: &BoundAttempt, execution: &Option<ActionExecution>) -> bool {
    match execution {
        None => true,
        Some(execution) => {
            let mode = match execution {
                ActionExecution::NativeSemantic { .. } => ExecutionMode::NativeSemantic,
                ActionExecution::WebSemantic { .. } => ExecutionMode::WebSemantic,
                ActionExecution::CoordinateFallback { .. } => ExecutionMode::CoordinateFallback,
            };
            attempt.accept_execution_modes.contains(&mode)
        }
    }
}

/// Whether an action step is effectively mutating for the I2 replay gates
/// (mutating and not declared idempotent).
pub(crate) fn gated_mutating(step: &ActionStepIR) -> bool {
    step.effect == EffectClassAction::Mutating && !step.idempotent
}

/// Whether an uncertain reconcile branch may replay the step
/// (07 §4.4: `idempotent: true` or `effect: "readonly"`).
pub(crate) fn replay_permitted(step: &ActionStepIR) -> bool {
    step.effect == EffectClassAction::Readonly || step.idempotent
}

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

    fn two_attempt_step() -> ActionStepIR {
        serde_json::from_value(serde_json::json!({
            "kind": "action",
            "stepId": "s1",
            "effectHash": format!("sha256:{}", "0".repeat(64)),
            "judgeHash": format!("sha256:{}", "0".repeat(64)),
            "checkpoint": true,
            "effect": "mutating",
            "idempotent": true,
            "binding": { "attempts": [ {
                "channel": "uiTree",
                "actionName": "tapElement",
                "args": {},
                "acceptExecutionModes": ["nativeSemantic"],
                "protection": "standard"
            }, {
                "channel": "uiTree",
                "actionName": "setElementValue",
                "args": {},
                "acceptExecutionModes": ["nativeSemantic"],
                "protection": "standard"
            } ] },
            "assertions": []
        }))
        .expect("fixture step")
    }

    #[test]
    fn maps_recorded_positions_and_refuses_out_of_range() {
        let step = two_attempt_step();
        assert_eq!(chain_start(None, &step).expect("head"), 0);
        assert_eq!(chain_start(Some(1), &step).expect("first"), 0);
        assert_eq!(chain_start(Some(2), &step).expect("second"), 1);
        assert!(chain_start(Some(3), &step).is_err());
        assert!(chain_start(Some(0), &step).is_err());
    }
}

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

    #[test]
    fn wire_summary_truncation_appends_the_local_verdict_pointer() {
        let verdict = Verdict {
            status: VerdictStatus::Fail,
            degraded: false,
            summary: "x".repeat(VERDICT_SUMMARY_MAX_CHARS + 100),
            evidence: Vec::new(),
            supersedes: None,
        };
        let capped = cap_wire_summary(&verdict);
        // Exactly at the wire cap — the fake/devicerail providers reject
        // anything above it fail-closed.
        assert_eq!(capped.chars().count(), VERDICT_SUMMARY_MAX_CHARS);
        // The 04 §5 pointer to the full local verdict rides the tail.
        assert!(
            capped.ends_with(']') && capped.contains("full local verdict sha256:"),
            "pointer missing: …{}",
            &capped[capped.len().saturating_sub(90)..]
        );
        assert!(capped.starts_with("xxx"));

        // Negative control: an in-cap summary passes through verbatim,
        // pointer-free.
        let short = Verdict {
            summary: "all assertions passed".to_owned(),
            ..verdict
        };
        assert_eq!(cap_wire_summary(&short), "all assertions passed");
    }
}