agentd-core 1.3.2

Minimal, MCP-native agent runtime as a library: the agentic loop, supervisor, workflows, and code-registered tools (the agentd engine)
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
// SPDX-License-Identifier: AGPL-3.0-only
//! **Runs and steps**: arming start nodes,
//! turning start events into durable runs, scheduling ready steps every tick,
//! executing the step kinds (data steps in-loop, MCP calls on executor
//! threads, `agent`/`think` in turn workers, `sleep` on durable timers,
//! `finish` closing the run), retries + `on_error` routing, and the
//! `workflow.*` tools.

use super::children::ChildKind;
use super::events::kinds;
use super::reactor::{PendingKind, Runtime, Target};
use super::tools::{ToolCaller, ToolOutcome};
use crate::config::v2::substitute_config_vars;
use crate::context::Msg;
use crate::engine::model::{OnError, Step, Workflow, parse_workflow};
use crate::engine::run::{
    self, Next, RunState, RunStatus, Start, StepStatus, env_view, render_spec,
};
use crate::engine::template;
use crate::governor::Admission;
use crate::registry::Caller;
use crate::state::{InboxEvent, Kind, now_ms, ulid};
use crate::subagent::protocol::{TurnKind, TurnResult, TurnSpec};
use serde_json::{Map, Value, json};
use std::collections::BTreeMap;

/// The memory key prefix runtime-created workflow definitions are stored under.
const WORKFLOW_DEF_PREFIX: &str = "_workflows/";

impl Runtime {
    // ---- definitions -----------------------------------------------------------

    /// The breaker policy a step actually runs under: its own `breaker:` wins;
    /// an `mcp.tool` step against a server that names a catalog `service:`
    /// otherwise inherits that entry's `breaker:` default. The admission gate
    /// and the outcome recorder both call this, so they can never disagree
    /// about which policy a given step is being judged by.
    pub(crate) fn effective_breaker(
        &self,
        step: &crate::engine::model::Step,
    ) -> Option<super::breaker::Config> {
        if let Some(cfg) = super::breaker::Config::of(step.spec.get("breaker")) {
            return Some(cfg);
        }
        if step.kind != "mcp.tool" {
            return None;
        }
        let server = step.field_str("server")?;
        let svc = self
            .settings
            .mcp
            .servers
            .iter()
            .find(|s| s.name == server)
            .and_then(|s| s.service.as_ref())?;
        super::breaker::Config::of(self.settings.services.get(svc)?.breaker.as_ref())
    }

    /// Resolve a parsed definition's durability class against the store
    /// default (`store.durability.work`): an explicit `durable:` wins; absent,
    /// `ephemeral` deployments run everything memory-only.
    pub(crate) fn fill_durable_default(&self, w: &mut crate::engine::model::Workflow) {
        if w.durable.is_none() {
            w.durable = Some(self.work_durable_default());
        }
    }

    /// Load the configured workflows (inline / file / uri) plus the
    /// runtime-created ones from the store. Errors are collected rather than
    /// returned on the first failure, so one bad definition is refused with a
    /// message instead of hiding the rest.
    pub(crate) fn load_workflows(&mut self) -> Result<(), Vec<String>> {
        let mut errs = Vec::new();
        // A `{dir}` entry expands into one entry per matching file BEFORE
        // resolution, so everything downstream — parsing, naming, the duplicate
        // check — sees a plain list of documents and needs no directory case.
        let mut docs: Vec<Value> = Vec::new();
        for doc in self.settings.workflows.clone() {
            // The ENTRY fold runs here, BEFORE the dir expansion, because a
            // `dir:` is consumed by that expansion and would never reach the
            // per-document fold below — `{{config.wf_dir}}` went to the
            // filesystem verbatim and failed as "not a directory". `file:` and
            // `url:` are folded again below (idempotent: a folded string has no
            // tokens left), which keeps the inline case reporting an unresolved
            // reference exactly once.
            let mut doc = doc;
            if doc.get("steps").is_none() {
                substitute_config_vars(&mut doc, &self.settings.vars, "workflow entry", &mut errs);
            }
            match doc.get("dir").and_then(Value::as_str) {
                None => docs.push(doc),
                Some(dir) => {
                    let pattern = doc
                        .get("glob")
                        .and_then(Value::as_str)
                        .unwrap_or("*.yaml,*.yml,*.json");
                    match expand_dir(dir, pattern) {
                        Ok(paths) if paths.is_empty() => {
                            // Silence here would mean a schedule that never
                            // fires and no way to tell why.
                            errs.push(format!("workflow dir {dir}: no file matched {pattern:?}"));
                        }
                        Ok(paths) => {
                            for path in paths {
                                let mut d = json!({"file": path});
                                if let Some(a) = doc.get("armed") {
                                    d["armed"] = a.clone();
                                }
                                docs.push(d);
                            }
                        }
                        Err(e) => errs.push(format!("workflow dir {dir}: {e}")),
                    }
                }
            }
        }
        for doc in docs {
            // `{{config.*}}` folds in at load, in two passes: the ENTRY first —
            // so a var can sit in a `file:`, `url:` or `dir:` reference and in
            // the headers that fetch it — and the RESOLVED document after, so a
            // definition arriving from a file or URL is treated exactly like an
            // inline one. Folding here (rather than at render time) puts the
            // substituted values in the definition hash: a var change is a
            // definition change, and in-flight runs stay pinned to what they
            // started with.
            let mut doc = doc;
            // The entry pass covers only REFERENCE entries (a var in a `file:`,
            // `url:` or the headers that fetch it). An inline definition skips
            // it — the resolved pass below sees the same document, and running
            // both would report every unresolved reference twice.
            if doc.get("steps").is_none() {
                substitute_config_vars(&mut doc, &self.settings.vars, "workflow entry", &mut errs);
            }
            let resolved = match (
                doc.get("file").and_then(Value::as_str),
                doc.get("uri").and_then(Value::as_str),
            ) {
                (Some(path), _) => match std::fs::read_to_string(path)
                    .map_err(|e| e.to_string())
                    .and_then(|t| {
                        crate::config::file::parse_document(
                            &t,
                            crate::config::file::Format::detect(
                                Some(std::path::Path::new(path)),
                                &t,
                            ),
                        )
                    }) {
                    Ok(mut d) => {
                        if d.get("name").is_none()
                            && let Some(n) = doc.get("name")
                        {
                            d["name"] = n.clone();
                        }
                        d
                    }
                    Err(e) => {
                        errs.push(format!("workflow file {path}: {e}"));
                        continue;
                    }
                },
                // A `url:` is fetched over HTTP(S), with operator-declared
                // headers — the shape people already have for a definitions
                // service or a raw git URL. Distinct from `uri:`, which is an
                // MCP resource: both name "somewhere else", but only one of
                // them makes the daemon dial.
                (None, None) if doc.get("url").is_some() => {
                    let url = doc["url"].as_str().unwrap_or_default().to_string();
                    match self.fetch_workflow_url(&doc, &url) {
                        Ok(mut d) => {
                            if d.get("name").is_none()
                                && let Some(n) = doc.get("name")
                            {
                                d["name"] = n.clone();
                            }
                            d
                        }
                        Err(e) => {
                            errs.push(format!("workflow url {url}: {e}"));
                            continue;
                        }
                    }
                }
                (None, Some(uri)) => match self.read_resource_any(uri) {
                    Ok(text) => match crate::config::file::parse_document(
                        &text,
                        crate::config::file::Format::detect(Some(std::path::Path::new(uri)), &text),
                    ) {
                        Ok(mut d) => {
                            if d.get("name").is_none()
                                && let Some(n) = doc.get("name")
                            {
                                d["name"] = n.clone();
                            }
                            d
                        }
                        Err(e) => {
                            errs.push(format!("workflow uri {uri}: {e}"));
                            continue;
                        }
                    },
                    Err(e) => {
                        errs.push(format!("workflow uri {uri}: {e}"));
                        continue;
                    }
                },
                _ => doc.clone(),
            };
            let mut resolved = resolved;
            substitute_config_vars(&mut resolved, &self.settings.vars, "workflow", &mut errs);
            match parse_workflow(&resolved) {
                Ok(mut w) => {
                    self.fill_durable_default(&mut w);
                    self.log.info("workflow.loaded", json!({"name": w.name, "hash": &w.hash[..12], "steps": w.steps.len(), "durable": w.durable, "starts": w.start_steps().iter().map(|s| s.kind.clone()).collect::<Vec<_>>()}));
                    self.workflows
                        .insert(w.name.clone(), std::sync::Arc::new(w));
                }
                Err(e) => errs.extend(e),
            }
        }
        // Runtime-created definitions (durable under memory/_workflows/<name>).
        if let Ok(list) = self.durable.list(Kind::Memory) {
            for ks in list {
                let Some((_, id)) = crate::store::parse_key(
                    self.durable.prefix(),
                    self.durable.instance(),
                    &ks.key,
                ) else {
                    continue;
                };
                if let Some(name) = id.strip_prefix(WORKFLOW_DEF_PREFIX)
                    && !self.workflows.contains_key(name)
                    && let Ok(Some(env)) = self.durable.get(Kind::Memory, id)
                    && let Some(def) = env.state.get("value")
                {
                    match parse_workflow(def) {
                        Ok(mut w) => {
                            self.fill_durable_default(&mut w);
                            self.log.info(
                                "workflow.loaded",
                                json!({"name": w.name, "source": "store"}),
                            );
                            self.workflows
                                .insert(w.name.clone(), std::sync::Arc::new(w));
                        }
                        Err(e) => self.log.warn(
                            "workflow.stored.invalid",
                            json!({"name": name, "errors": e}),
                        ),
                    }
                }
            }
        }
        // Validate tool/server references against the registry.
        for w in self.workflows.values() {
            for s in w.steps.values() {
                match s.kind.as_str() {
                    "tool" => {
                        if let Some(n) = s.field_str("name")
                            && !self.registry.allowed(&Caller::Workflow, n)
                        {
                            errs.push(format!("workflow {:?} step {:?}: tool {n:?} is unknown, disabled or not granted to workflows", w.name, s.id));
                        }
                    }
                    "mcp.tool" => {
                        if let Some(srv) = s.field_str("server")
                            && !self.mcp.contains_key(srv)
                        {
                            errs.push(format!(
                                "workflow {:?} step {:?}: mcp server {srv:?} is not connected",
                                w.name, s.id
                            ));
                        }
                    }
                    k if (k.starts_with("memory.")
                        || k.starts_with("artifact.")
                        || k.starts_with("knowledge.")
                        || k.starts_with("search."))
                        && !self.registry.allowed(&Caller::Workflow, k) =>
                    {
                        errs.push(format!("workflow {:?} step {:?}: {k} is unavailable (map it with tools.overrides or configure its server)", w.name, s.id));
                    }
                    _ => {}
                }
            }
        }
        // Mixed durability is legal but has one sharp edge worth a loud line:
        // a DURABLE parent step waiting on a NON-durable child run resumes
        // after a restart to find the run gone (the wait fails with "run does
        // not exist"). Say so at load, where the shape is still a choice.
        for w in self.workflows.values() {
            if w.durable == Some(false) {
                continue;
            }
            for st in w.steps.values() {
                if st.kind == "workflow"
                    && st.field_str("mode").unwrap_or("sync") != "detached"
                    && let Some(target) = st.field_str("name")
                    && self
                        .workflows
                        .get(target)
                        .is_some_and(|t| t.durable == Some(false))
                {
                    self.log.warn(
                        "workflow.durability_mix",
                        json!({"workflow": w.name, "step": st.id, "child": target,
                               "note": "a durable parent waits on a non-durable child; after a restart the wait fails (the child run does not survive)"}),
                    );
                }
            }
        }
        // Streams are fail-closed at load: an `emit` or `stream` node naming an
        // undeclared stream is a config error now, not a step failure later.
        for w in self.workflows.values() {
            for st in w.steps.values() {
                if matches!(st.kind.as_str(), "emit" | "stream")
                    && let Some(name) = st.field_str("stream")
                    && !self.settings.streams.contains_key(name)
                {
                    errs.push(format!(
                        "workflow {:?} step {:?}: stream {name:?} is not declared under `streams:`",
                        w.name, st.id
                    ));
                }
            }
        }
        if errs.is_empty() { Ok(()) } else { Err(errs) }
    }

    /// Fetch a workflow definition over HTTP(S).
    ///
    /// Startup-time and fail-closed: an unreachable definitions service is a
    /// daemon that would otherwise come up with a schedule silently missing, so
    /// it refuses to start and says which URL. That matches how a required MCP
    /// server behaves and is the safer half of the trade.
    ///
    /// Headers are operator-declared and resolve `{{secret:…}}` like every
    /// other credential, so a token never sits in the config file. The URL is
    /// SSRF-guarded like any other outbound request — an operator-chosen URL is
    /// far more trustworthy than a model-chosen one, but `allow_private` is
    /// still an explicit decision rather than an assumption.
    fn fetch_workflow_url(&self, doc: &Value, url: &str) -> Result<Value, String> {
        let headers: Vec<(String, String)> = doc
            .get("headers")
            .and_then(Value::as_object)
            .map(|m| {
                m.iter()
                    .map(|(k, v)| (k.clone(), v.as_str().unwrap_or_default().to_string()))
                    .collect()
            })
            .unwrap_or_default();
        let headers =
            crate::mcp::auth::resolve_headers(&headers).map_err(|e| format!("headers: {e}"))?;
        let timeout = doc
            .get("timeout")
            .and_then(Value::as_str)
            .and_then(|t| crate::config::parse_duration(t).ok())
            .unwrap_or(std::time::Duration::from_secs(20));
        let allow_private = doc
            .get("allow_private")
            .and_then(Value::as_bool)
            .unwrap_or(false);
        let text = crate::runtime::http_node::fetch_text(url, &headers, timeout, allow_private)?;
        crate::config::file::parse_document(
            &text,
            crate::config::file::Format::detect(Some(std::path::Path::new(url)), &text),
        )
    }

    /// Read a resource URI (`mcp://<server>/<uri>` or a URI a connected server lists).
    pub(crate) fn read_resource_any(&self, uri: &str) -> Result<String, String> {
        if let Some(rest) = uri.strip_prefix("mcp://") {
            let (server, res) = rest
                .split_once('/')
                .ok_or("mcp:// uri needs <server>/<resource-uri>")?;
            let c = self
                .mcp
                .get(server)
                .ok_or_else(|| format!("mcp server {server:?} is not connected"))?;
            return c
                .read_resource(res)
                .map(|r| r.text())
                .map_err(|e| e.to_string());
        }
        let mut last = String::from("no connected server serves it");
        for c in self.mcp.values() {
            match c.read_resource(uri) {
                Ok(r) => return Ok(r.text()),
                Err(e) => last = e.to_string(),
            }
        }
        Err(last)
    }

    /// Arm start nodes. `once` fires immediately unless a live run of the
    /// workflow came back from the store (`policy: ensure`, the default), which
    /// keeps a restart from starting a second copy of work already in flight;
    /// `policy: always` fires regardless.
    pub(crate) fn arm_workflows(&mut self) {
        let names: Vec<String> = self.workflows.keys().cloned().collect();
        for name in names {
            let Some(w) = self.workflows.get(&name) else {
                continue;
            };
            if !w.armed {
                continue;
            }
            let starts: Vec<(String, String, Map<String, Value>)> = w
                .start_steps()
                .iter()
                .map(|s| (s.id.clone(), s.kind.clone(), s.spec.clone()))
                .collect();
            for (id, kind, spec) in starts {
                match kind.as_str() {
                    "once" => {
                        let policy = spec
                            .get("policy")
                            .and_then(Value::as_str)
                            .unwrap_or("ensure");
                        let live = self
                            .runs
                            .values()
                            .any(|r| r.workflow == name && !r.status.is_terminal());
                        let ever = self
                            .runs
                            .values()
                            .any(|r| r.workflow == name && r.start.node == id);
                        // A replayed (still pending) firing counts too — never fire twice.
                        let pending = self.inbox_queue.iter().any(|e| {
                            e.kind == kinds::START_FIRED
                                && e.payload["workflow"] == name.as_str()
                                && e.payload["node"] == id.as_str()
                        });
                        if policy == "ensure" && (live || ever || pending) {
                            self.log.info("start.once.skipped", json!({"workflow": name, "node": id, "live": live, "pending": pending}));
                            continue;
                        }
                        let inputs = spec.get("inputs").cloned().unwrap_or(json!({}));
                        // A `once` start is autonomous work like any other
                        // trigger, so it is attributed the same way.
                        let acting = self.settings.identity.autonomous_id().to_string();
                        let _ = self.accept_event(kinds::START_FIRED, Some(acting), json!({"workflow": name, "node": id, "payload": {"fired_at": now_ms()}, "inputs": inputs}));
                    }
                    "manual" => {}
                    // Long-lived starts are armed by arm_long_lived_starts.
                    _ => {}
                }
            }
        }
    }

    /// A start event → a run. Returns `true` when the event is consumed.
    pub(crate) fn on_start_event(&mut self, ev: &InboxEvent) -> bool {
        let name = ev.payload["workflow"].as_str().unwrap_or("").to_string();
        let Some(w) = self.workflows.get(&name).cloned() else {
            self.log.warn(
                "start.unknown_workflow",
                json!({"inbox_event": ev.id, "workflow": name}),
            );
            return true;
        };
        let node = ev.payload["node"]
            .as_str()
            .map(str::to_string)
            .unwrap_or_else(|| default_start(&w).unwrap_or_default());
        // Concurrency: a new run must fit both the workflow's own `max_runs`
        // and the instance-wide `limits.max_runs`; whichever binds first
        // decides the `on_overflow` outcome.
        //
        // `scope: key` counts only the runs about the SAME THING, which is the
        // difference between a queue and a lock: `max_runs: 1` under
        // `scope: workflow` serialises every customer behind one run, so
        // per-entity ordering used to mean one workflow definition per entity.
        // A firing whose key did not render counts under the workflow scope —
        // sharing an "unkeyed" bucket with every other such firing would
        // silently serialise unrelated work.
        let this_key = ev.payload["key"].as_str();
        let keyed = w.concurrency.scope == crate::engine::model::ConcurrencyScope::Key
            && this_key.is_some();
        let live = self
            .runs
            .values()
            .filter(|r| r.workflow == name && !r.status.is_terminal())
            .filter(|r| !keyed || r.key.as_deref() == this_key)
            .count() as u32;
        let global_live = self
            .runs
            .values()
            .filter(|r| !r.status.is_terminal())
            .count() as u32;
        if live >= w.concurrency.max_runs
            || global_live >= self.settings.limits.max_runs.unwrap_or(8)
        {
            match w.concurrency.on_overflow {
                crate::engine::model::OnOverflow::Queue => {
                    // Keep the event pending; it is retried on a LATER tick.
                    // Nothing this tick can relieve the cap — only a live run
                    // reaching a terminal status in `schedule_runs` does, and
                    // that step has not run yet — so this must not be re-offered
                    // now. `process_inbox` drains a snapshot for exactly that
                    // reason: this push lands on the next tick's queue.
                    self.inbox_queue.push_back(ev.clone());
                    return false;
                }
                crate::engine::model::OnOverflow::Drop => {
                    self.log.warn(
                        "run.dropped",
                        json!({"workflow": name, "reason": "concurrency"}),
                    );
                    return true;
                }
                crate::engine::model::OnOverflow::Replace => {
                    if let Some(oldest) = self
                        .runs
                        .values()
                        .filter(|r| r.workflow == name && !r.status.is_terminal())
                        .min_by_key(|r| r.created)
                        .map(|r| r.id.clone())
                    {
                        self.cancel_run(&oldest, "replaced by a newer run");
                    }
                }
            }
        }
        // Inputs.
        let inputs = ev.payload.get("inputs").cloned().unwrap_or(json!({}));
        if let Some(schema) = &w.inputs_schema
            && let Err(e) = crate::jsonschema::validate(schema, &inputs)
        {
            self.log
                .warn("run.inputs.invalid", json!({"workflow": name, "errors": e}));
            return true;
        }
        // A2A pre-generates the run id so its task can link before the run starts.
        let run_id = ev
            .payload
            .get("run_id")
            .and_then(Value::as_str)
            .map(str::to_string)
            .unwrap_or_else(|| format!("{}-{}", name, ulid::new()));
        // The definition this run starts under survives us durably: a restart
        // that also changed or removed the workflow still finishes this run.
        self.ensure_pin(&w);
        let mut run = RunState::new(
            &run_id,
            &w,
            Start {
                node: node.clone(),
                payload: ev.payload.get("payload").cloned().unwrap_or(Value::Null),
                ts: now_ms(),
            },
            inputs,
        );
        run.principal = ev.principal.clone();
        run.parent = ev.payload.get("parent").cloned().filter(|p| !p.is_null());
        run.conversation = ev
            .payload
            .get("conversation")
            .and_then(Value::as_str)
            .map(str::to_string);
        run.task = ev
            .payload
            .get("task")
            .and_then(Value::as_str)
            .map(str::to_string);
        // A run inherits the message-hop depth of whatever asked for it, so a
        // `message` inside it extends that chain rather than starting a fresh
        // one. Triggers carry nothing and so start at 0.
        run.msg_depth = ev.payload["msg_depth"].as_u64().unwrap_or(0) as u32;
        run.key = ev.payload["key"].as_str().map(str::to_string);
        // Durable before anything runs — unless the workflow opted out of the
        // class entirely (`durable: false`): a memory-only run writes nothing,
        // here or at any checkpoint.
        if run.durable
            && let Err(e) = self.durable.put(
                Kind::Run,
                &run_id,
                serde_json::to_value(&run).unwrap_or(Value::Null),
                Some(w.hash.clone()),
            )
        {
            self.log.error(
                "run.create.fail",
                json!({"workflow": name, "err": e.to_string()}),
            );
            // The event stays pending in the DURABLE inbox (only a consumed
            // start is marked done), so dropping it from the in-memory queue
            // here would make the start silently vanish until a restart
            // replays it. Requeue it like an overflow: the next tick retries.
            self.inbox_queue.push_back(ev.clone());
            return false;
        }
        run.dirty = false;
        // The actor is on the line: "every effect names the human or the
        // schedule that caused it" is only true if you can read it back.
        self.log.info(
            "run.start",
            json!({"run": run_id, "workflow": name, "node": node, "inbox_event": ev.id,
                   "acting_for": run.principal, "key": run.key}),
        );
        self.counters.runs_started += 1;
        crate::obs::metrics::record_run_started();
        if node_kind(&w, &node) == Some("once") && self.job_shape {
            self.job_runs.push(run_id.clone());
        }
        // Answer a `workflow.run` waiter that asked for the id.
        if ev.kind == kinds::WORKFLOW_RUN
            && let Some(req) = ev.payload.get("request").and_then(Value::as_object)
        {
            let target = match (
                req.get("node").and_then(Value::as_u64),
                req.get("req").and_then(Value::as_u64),
                req.get("run").and_then(Value::as_str),
                req.get("step").and_then(Value::as_str),
            ) {
                (Some(n), Some(r), _, _) => {
                    Some(Target::Child(crate::supervisor::tree::NodeId(n), r))
                }
                (None, None, Some(r), Some(s)) => Some(Target::Step(r.to_string(), s.to_string())),
                _ => None,
            };
            if let Some(t) = target {
                if req.get("wait").and_then(Value::as_bool).unwrap_or(false) {
                    let deadline = now_ms()
                        + req
                            .get("timeout_ms")
                            .and_then(Value::as_u64)
                            .unwrap_or(3_600_000);
                    self.push_pending(super::reactor::PendingTool {
                        target: t,
                        name: "workflow.run".into(),
                        kind: PendingKind::Run {
                            run: run_id.clone(),
                            deadline_ms: deadline,
                        },
                        started_ms: now_ms(),
                    });
                } else {
                    self.reply(
                        &t,
                        json!({"run": run_id, "status": "running", "workflow": name}),
                        false,
                    );
                }
            }
        }
        self.runs.insert(run_id, run);
        true
    }

    // ---- scheduling ------------------------------------------------------------

    /// Every tick: advance every live run.
    pub(crate) fn schedule_runs(&mut self) {
        if self.paused {
            return; // operator hold (a2a.pause) — steps park until resume
        }
        // Higher-priority runs schedule first each tick, so under contention
        // (fan-out slots, per-tick capacity) their ready steps win. Stable
        // within a priority: BTreeMap order = name, then creation (ULID).
        let mut ids: Vec<(std::cmp::Reverse<crate::engine::model::Priority>, String)> = self
            .runs
            .iter()
            .filter(|(_, r)| !r.status.is_terminal() && r.status != RunStatus::Paused)
            .map(|(id, r)| {
                let pr = self
                    .workflows
                    .get(&r.workflow)
                    .map(|w| w.priority)
                    .unwrap_or_default();
                (std::cmp::Reverse(pr), id.clone())
            })
            .collect();
        ids.sort_by_key(|a| a.0);
        for (_, id) in ids {
            self.schedule_run(&id);
        }
    }

    /// [`Self::definition_for_run`] addressed by `(name, hash)` — for callers
    /// that already hold the run record.
    pub(crate) fn definition_for_run_ref(&self, workflow: &str, hash: &str) -> Option<&Workflow> {
        if let Some(w) = self.workflows.get(workflow)
            && w.hash == hash
        {
            return Some(w.as_ref());
        }
        self.pinned.get(hash).map(|w| w.as_ref())
    }

    /// The definition a run executes against: the one it started with,
    /// identified by hash — the current definition while it is unchanged, else
    /// the copy a reload pinned. A restored run whose definition changed
    /// underneath it matches neither, and `resume_policy: refuse` then stops
    /// it rather than silently running it against a different graph.
    pub(crate) fn definition_for_run(&self, run_id: &str) -> Option<std::sync::Arc<Workflow>> {
        let run = self.runs.get(run_id)?;
        if let Some(w) = self.workflows.get(&run.workflow)
            && w.hash == run.workflow_hash
        {
            // An Arc clone: a refcount bump, not a graph copy — this is on
            // the per-step hot path (measured ~10% of a chain's cycles as a
            // deep clone).
            return Some(w.clone());
        }
        self.pinned.get(&run.workflow_hash).cloned()
    }

    fn schedule_run(&mut self, run_id: &str) {
        let Some(wf) = self.definition_for_run(run_id) else {
            // The definition vanished or changed (hash mismatch): refuse to
            // continue the run (`resume_policy: refuse`).
            let (name, hash) = self
                .runs
                .get(run_id)
                .map(|r| (r.workflow.clone(), r.workflow_hash.clone()))
                .unwrap_or_default();
            let reason = if self.workflows.contains_key(&name) {
                format!(
                    "workflow {name:?} definition changed (run pinned to hash {}); resume_policy refuse",
                    &hash[..hash.len().min(12)]
                )
            } else {
                format!("workflow {name:?} definition is gone")
            };
            self.log
                .warn("run.refused", json!({"run": run_id, "reason": reason}));
            if let Some(r) = self.runs.get_mut(run_id) {
                r.finish(RunStatus::Refused, None, Some(reason));
            }
            self.on_run_terminal(run_id);
            return;
        };
        if let Some(r) = self.runs.get(run_id)
            && run::deadline_passed(r)
        {
            self.log.warn("run.deadline", json!({"run": run_id}));
            self.cancel_children_of_run(run_id, "run deadline");
            self.runs.get_mut(run_id).expect("present").finish(
                RunStatus::Failed,
                None,
                Some("deadline exceeded".into()),
            );
            self.on_run_terminal(run_id);
            return;
        }
        // A workflow that declares no budget inherits the instance's
        // `limits.run.*`, so a definition silent about limits is still bounded.
        // An unbounded run is the one shape where a single mistake can spend
        // the whole day's tokens, and the instance-wide knob exists precisely
        // to cap that.
        let step_cap = wf.limits.steps.or(self.settings.limits.run.steps);
        let token_cap = wf.limits.tokens.or(self.settings.limits.run.tokens);
        if let Some(cap) = step_cap
            && self.runs.get(run_id).is_some_and(|r| r.steps_run >= cap)
        {
            self.runs.get_mut(run_id).expect("present").finish(
                RunStatus::Failed,
                None,
                Some(format!("exhausted steps: limit {cap}")),
            );
            self.on_run_terminal(run_id);
            return;
        }
        if let Some(cap) = token_cap
            && self.runs.get(run_id).is_some_and(|r| r.tokens >= cap)
        {
            self.runs.get_mut(run_id).expect("present").finish(
                RunStatus::Failed,
                None,
                Some(format!("exhausted tokens: limits.tokens = {cap}")),
            );
            self.on_run_terminal(run_id);
            return;
        }
        let data = self.run_data(run_id);
        let next = {
            let run = self.runs.get_mut(run_id).expect("present");
            run::schedule(&wf, run, &data)
        };
        // Nested parents in flight advance every tick (rate pacing, timeouts,
        // fresh iterations).
        let nested: Vec<String> = self
            .runs
            .get(run_id)
            .map(|r| {
                r.steps
                    .iter()
                    .filter(|(_, st)| {
                        st.status == StepStatus::Running
                            && st.wait.as_ref().is_some_and(|w| {
                                matches!(
                                    w["kind"].as_str(),
                                    Some("foreach")
                                        | Some("batch")
                                        | Some("iterate")
                                        | Some("parallel")
                                        | Some("race")
                                        | Some("subgraph")
                                )
                            })
                    })
                    .map(|(id, _)| id.clone())
                    .collect()
            })
            .unwrap_or_default();
        for id in nested {
            self.nested_advance(run_id, &id);
        }
        // Draining stops starting new steps — with one deliberate exception:
        // a workflow that declares a `lifecycle.shutdown` start exists to run
        // DURING the drain (deregister the webhook, flush the summary), and
        // the drain gate waits for it. Everything else parks where it is,
        // checkpointed, and resumes next life.
        let shutdown_capable = self.draining
            && wf
                .start_steps()
                .iter()
                .any(|s| s.kind == "event" && s.field_str("on") == Some("lifecycle.shutdown"));
        match next {
            Ok(Next::Ready(steps)) => {
                for s in steps {
                    if self.draining && !shutdown_capable {
                        return;
                    }
                    self.execute_step(run_id, &s);
                }
            }
            Ok(Next::Waiting) | Ok(Next::Terminal) => {}
            Ok(Next::Stalled) => {
                // "No ready step" is a symptom, not a diagnosis. Almost always
                // something upstream failed and its dependents could never
                // become ready — so name the first failed ancestor rather than
                // leaving whoever reads this to walk the graph themselves.
                let culprit = self.first_failed_step(run_id);
                let why = match &culprit {
                    Some((sid, err)) => format!(
                        "no ready step and no finish reached — step {sid:?} failed first: {err}"
                    ),
                    None => "no ready step and no finish reached".to_string(),
                };
                self.log.warn(
                    "run.stalled",
                    json!({"run": run_id, "blocked_by": culprit.as_ref().map(|(s, _)| s)}),
                );
                // A stall caused by a failure is a FAILED run, not a stalled
                // one: the distinction matters to an exit code and to a caller.
                let status = if culprit.is_some() {
                    RunStatus::Failed
                } else {
                    RunStatus::Stalled
                };
                self.runs
                    .get_mut(run_id)
                    .expect("present")
                    .finish(status, None, Some(why));
                self.on_run_terminal(run_id);
            }
            Err(e) => {
                self.runs.get_mut(run_id).expect("present").finish(
                    RunStatus::Failed,
                    None,
                    Some(e),
                );
                self.on_run_terminal(run_id);
            }
        }
    }

    /// Refuse a definition-mutating tool when `security.workflows.immutable`.
    ///
    /// Applies to everyone — the model, a subagent, an operator over A2A —
    /// because the point is that definitions are reviewed before they ship, and
    /// a lock one caller can talk its way past is not a lock. Says how to change
    /// them properly rather than only saying no.
    fn workflows_locked(&self, tool: &str) -> Option<super::tools::ToolOutcome> {
        if !self.settings.security.workflows.immutable {
            return None;
        }
        self.log.warn("workflow.locked", json!({"tool": tool}));
        Some(super::tools::ToolOutcome::Ready(
            json!({"error": format!(
                "{tool}: workflow definitions are immutable \
                 (security.workflows.immutable) — edit the config, file or \
                 directory they load from and reload"
            )}),
            true,
        ))
    }

    /// The earliest step of a run that failed, with its error.
    ///
    /// This is what explains a stall: a run with no ready step is usually a run
    /// whose dependency chain is blocked behind a failure that was routed away
    /// from `on_error: fail`, and that failure is what a person needs to see.
    fn first_failed_step(&self, run_id: &str) -> Option<(String, String)> {
        let run = self.runs.get(run_id)?;
        run.steps
            .iter()
            .filter(|(_, st)| {
                matches!(
                    st.status,
                    StepStatus::Failed | StepStatus::Timeout | StepStatus::Cancelled
                )
            })
            .min_by_key(|(_, st)| st.finished.unwrap_or(u64::MAX))
            .map(|(id, st)| {
                (
                    id.clone(),
                    st.error
                        .clone()
                        .unwrap_or_else(|| "no error recorded".into()),
                )
            })
    }

    /// The template data a run's specs render against: the `env` view, the
    /// run's own fields, and `memory` as a read-through `{key: value}` map
    /// holding exactly the `memory.<key>` references the definition names.
    pub(crate) fn run_data(&mut self, run_id: &str) -> template::Data {
        let env = env_view(
            &self.instance,
            run_id,
            Some(&self.instruction.text),
            self.settings.agent.prompt.as_deref(),
        );
        // Memory read-through: resolve every `memory.<key>` the definition
        // names. The key scan walks the whole definition, so it is memoized
        // per content hash — this runs per STEP, and re-walking an unchanged
        // definition on every one of them lands squarely on the hot path.
        let mut memory = Map::new();
        let hash = self
            .runs
            .get(run_id)
            .map(|r| r.workflow_hash.clone())
            .unwrap_or_default();
        if !hash.is_empty() {
            if !self.memory_keys.contains_key(&hash) {
                let mut keys: Vec<String> = Vec::new();
                if let Some(wf) = self.definition_for_run(run_id) {
                    for s in wf.steps.values() {
                        for (_, v) in &s.spec {
                            collect_memory_keys(v, &mut keys);
                        }
                        if let Some(w) = &s.when {
                            collect_memory_keys(&Value::String(w.clone()), &mut keys);
                        }
                    }
                }
                self.memory_keys.insert(hash.clone(), keys);
            }
            for k in self.memory_keys.get(&hash).cloned().unwrap_or_default() {
                if let Ok(v) = self.memory.get(&self.durable, &k)
                    && v["found"] == json!(true)
                {
                    memory.insert(k, v["value"].clone());
                }
            }
        }
        let mut data = self
            .runs
            .get(run_id)
            .map(|r| r.data(env, Value::Object(memory)))
            .unwrap_or_default();
        // Artifact-backed values (`{"$artifact": id}`) dereference transparently
        // so a template sees the content, not the reference — a step never has
        // to know whether an upstream output was spilled to an artifact.
        for key in ["steps", "vars", "inputs"] {
            if let Some(v) = data.get_mut(key) {
                self.deref_artifacts(v);
            }
        }
        data
    }

    /// Replace `{"$artifact": id, …}` objects with the artifact's content.
    pub(crate) fn deref_artifacts(&self, v: &mut Value) {
        match v {
            Value::Object(o) => {
                if let Some(id) = o.get("$artifact").and_then(Value::as_str) {
                    if let Some(a) = self.artifacts.get(id) {
                        *v = a.content.clone();
                    }
                    return;
                }
                for x in o.values_mut() {
                    self.deref_artifacts(x);
                }
            }
            Value::Array(a) => {
                for x in a.iter_mut() {
                    self.deref_artifacts(x);
                }
            }
            _ => {}
        }
    }

    // ---- execution -------------------------------------------------------------

    /// Execute one ready step of a run.
    pub(crate) fn execute_step_pub(&mut self, run_id: &str, step_id: &str) {
        self.execute_step(run_id, step_id)
    }

    fn execute_step(&mut self, run_id: &str, step_id: &str) {
        if self.runs.get(run_id).is_none_or(|r| r.status.is_terminal()) {
            return;
        }
        let Some(wf) = self
            .runs
            .get(run_id)
            .and_then(|_| self.definition_for_run(run_id))
        else {
            return;
        };
        let Some((step, scope)) = self
            .runs
            .get(run_id)
            .and_then(|r| self.resolve_step(&wf, r, step_id))
        else {
            return;
        };
        // Outbound throttling (`rate:` on a remote-effect kind): consulted
        // BEFORE `begin_step`, because a parked step has not attempted
        // anything — waiting for a token must consume neither an attempt nor
        // a retry. On an empty bucket the step suspends on a durable timer one
        // token-interval out and re-enters this gate when it fires.
        if matches!(
            step.kind.as_str(),
            "http" | "mcp.tool" | "a2a.send" | "a2a.delegate"
        ) && let Some(rate) = step.spec.get("rate").and_then(Value::as_str)
            && let Ok((burst, secs)) = crate::supervisor::tree::parse_rate(rate)
        {
            let workflow = self
                .runs
                .get(run_id)
                .map(|r| r.workflow.clone())
                .unwrap_or_default();
            let key = super::breaker::key(&workflow, step_id);
            let (bucket, window_s, b) = self.step_rates.entry(key.clone()).or_insert_with(|| {
                (
                    crate::supervisor::tree::TokenBucket::new(burst, burst as f64 / secs),
                    secs,
                    burst,
                )
            });
            if !bucket.try_take() {
                // One token-interval, floored so a tight rate still parks
                // meaningfully rather than hot-looping the scheduler.
                let wait = ((*window_s * 1000.0) / (*b).max(1) as f64).max(20.0) as u64;
                match self.timers.arm(
                    &self.durable,
                    now_ms() + wait,
                    json!({"kind": "step_budget", "run": run_id, "step": step_id}),
                    Value::Null,
                ) {
                    Ok(id) => {
                        self.log.info(
                            "step.rate_wait",
                            json!({"run": run_id, "step": step_id, "rate": rate, "wait_ms": wait}),
                        );
                        self.runs
                            .get_mut(run_id)
                            .expect("present")
                            .suspend_step(step_id, json!({"kind": "rate_wait", "timer": id}));
                        self.checkpoint(false);
                        return;
                    }
                    Err(e) => {
                        // A store that cannot arm the wait must not turn a
                        // throttle into a hot loop; proceed unthrottled and say so.
                        self.log.warn(
                            "step.rate_wait_fail",
                            json!({"run": run_id, "step": step_id, "err": e.to_string()}),
                        );
                    }
                }
            }
        }
        let attempt = self
            .runs
            .get_mut(run_id)
            .expect("present")
            .begin_step(step_id);
        // A breakpoint set with `workflow.pause {before_step}` stops here — the
        // step has not begun, so the run can be inspected in the state it is in
        // rather than one effect later.
        if self
            .runs
            .get(run_id)
            .and_then(|r| r.break_before.as_deref())
            == Some(step_id)
        {
            if let Some(r) = self.runs.get_mut(run_id) {
                r.status = RunStatus::Paused;
                r.break_before = None;
                r.dirty = true;
                r.steps.entry(step_id.to_string()).or_default().status = StepStatus::Pending;
            }
            self.log
                .info("run.paused", json!({"run": run_id, "before_step": step_id}));
            self.checkpoint(true);
            return;
        }
        // Mark the step durably `running` BEFORE its effect leaves the process,
        // so a crash replays an effect that may already have happened rather
        // than losing one entirely. A pure data step has no effect to guard —
        // a crash replays it deterministically from the last checkpoint — so an
        // inline chain batches into the tick's single checkpoint instead of
        // paying a serialize+write per step.
        crate::state::kill_point("step.running");
        if !crate::engine::model::pure_data_kind(&step.kind) {
            self.checkpoint(false);
        }
        self.log.info(
            "step.start",
            json!({"run": run_id, "step": step_id, "kind": step.kind, "attempt": attempt}),
        );
        // One feed event per step transition: run-level counts alone ("3 done,
        // 1 running") tell a display client that a run is moving but never WHAT
        // is moving. Operator-scoped, because a step id and its kind describe
        // the workflow's internals. The feed is the A2A interface surface, so
        // without that feature there is nothing to push to.
        #[cfg(feature = "a2a")]
        self.feed_push(
            "step",
            crate::runtime::a2a_server::FeedVis::Operator,
            json!({"run": run_id, "step": step_id, "kind": step.kind,
                   "phase": "start", "attempt": attempt}),
        );
        let mut data = match &scope {
            Some(sc) => self.scoped_data(run_id, sc),
            None => self.run_data(run_id),
        };
        // Step-scoped env: the identity a RETRY of this step shares. Anything a
        // template derives from these is stable across attempts — which is what
        // makes `env.idempotency_key` an idempotency key and not a fresh id per
        // try. (`env.ts` already exists for when a per-attempt value is what
        // you want; the two must not be confused.)
        if let Some(env) = data.get_mut("env") {
            env["step"] = json!(step_id);
            env["attempt"] = json!(attempt);
            env["idempotency_key"] = json!(crate::engine::run::idempotency_key(run_id, step_id));
        }
        let spec = match render_spec(&step, &data) {
            Ok(s) => s,
            Err(e) => {
                self.finish_step(run_id, step_id, StepStatus::Failed, None, Some(e), 0);
                return;
            }
        };
        let step_caller = ToolCaller {
            run: Some(run_id.to_string()),
            step: Some(step_id.to_string()),
            req: attempt as u64,
            principal: self.runs.get(run_id).and_then(|r| r.principal.clone()),
            ctx: self.runs.get(run_id).and_then(|r| r.conversation.clone()),
            msg_depth: self.runs.get(run_id).map(|r| r.msg_depth).unwrap_or(0),
            ..Default::default()
        };
        // `cache {key, ttl}`: a fresh memoized output skips the effect.
        let cache_key = match self.cache_lookup(&step, &spec, &data) {
            Some((_key, Some(hit))) => {
                self.log
                    .info("step.cache_hit", json!({"run": run_id, "step": step_id}));
                self.finish_step(run_id, step_id, StepStatus::Done, Some(hit), None, 0);
                return;
            }
            Some((key, None)) => Some(key),
            None => None,
        };
        if let Some(k) = cache_key
            && let Some(st) = self
                .runs
                .get_mut(run_id)
                .and_then(|r| r.steps.get_mut(step_id))
        {
            st.cache_key = Some(k);
        }
        // The circuit breaker (`breaker:` on a remote-effect kind): consulted
        // BEFORE the effect is dispatched, on the loop, so an open circuit
        // costs a map lookup instead of a connection + timeout. A fast-fail is
        // an ordinary step failure carrying `breaker::OPEN_ERR` — retry and
        // on_error compose with it; the recorder in `finish_step` skips it.
        if matches!(
            step.kind.as_str(),
            "http" | "mcp.tool" | "a2a.send" | "a2a.delegate"
        ) && let Some(cfg) = self.effective_breaker(&step)
        {
            let workflow = self
                .runs
                .get(run_id)
                .map(|r| r.workflow.clone())
                .unwrap_or_default();
            let key = super::breaker::key(&workflow, step_id);
            let mut st = self
                .durable
                .manifest()
                .breakers
                .get(&key)
                .cloned()
                .unwrap_or_else(|| json!({}));
            match super::breaker::gate(&mut st, cfg, now_ms()) {
                super::breaker::Gate::Proceed => {}
                super::breaker::Gate::Probe => {
                    // This attempt claimed the half-open probe slot; the claim
                    // is durable so a concurrent run (or a restart) sees it.
                    self.durable.manifest_update(|m| {
                        m.breakers.insert(key.clone(), st);
                    });
                    self.log
                        .info("breaker.probe", json!({"breaker": key, "run": run_id}));
                }
                super::breaker::Gate::FastFail { retry_in_ms } => {
                    self.finish_step(
                        run_id,
                        step_id,
                        StepStatus::Failed,
                        None,
                        Some(format!(
                            "{} — failing fast; next probe in {}ms",
                            super::breaker::OPEN_ERR,
                            retry_in_ms
                        )),
                        0,
                    );
                    return;
                }
            }
        }
        match step.kind.as_str() {
            "checkpoint" => {
                // Documented as "force a durable checkpoint here rather than at
                // the next natural boundary", and implemented as an alias for
                // `noop` — so the one step whose entire purpose is to write did
                // not write. `true` forces the write rather than letting the
                // policy decide.
                self.checkpoint(true);
                self.finish_step(
                    run_id,
                    step_id,
                    StepStatus::Done,
                    Some(Value::Null),
                    None,
                    0,
                );
            }
            "noop" => self.finish_step(
                run_id,
                step_id,
                StepStatus::Done,
                Some(Value::Null),
                None,
                0,
            ),
            "assign" | "transform" => {
                let value = spec.get("value").cloned().unwrap_or(Value::Null);
                let key = spec
                    .get("writes")
                    .and_then(Value::as_str)
                    .unwrap_or(step_id)
                    .to_string();
                // A declared `state` key carries a schema; a write that breaks
                // it fails the step where the bad value is produced, rather
                // than three steps later where a template reads a shape nobody
                // expected. This is the whole reason to declare state.
                if let Some(schema) = self
                    .definition_for_run(run_id)
                    .and_then(|wf| wf.state.get(&key).and_then(|d| d.schema.clone()))
                    && let Err(errs) = crate::jsonschema::validate(&schema, &value)
                {
                    self.finish_step_pub(
                        run_id,
                        step_id,
                        StepStatus::Failed,
                        None,
                        Some(format!(
                            "assign: value does not match the schema declared for state \
                             {key:?}: {}",
                            errs.join("; ")
                        )),
                        0,
                    );
                    return;
                }
                let mode = spec
                    .get("mode")
                    .and_then(Value::as_str)
                    .unwrap_or("overwrite")
                    .to_string();
                self.runs
                    .get_mut(run_id)
                    .expect("present")
                    .write_var(&key, value.clone(), &mode);
                self.finish_step(run_id, step_id, StepStatus::Done, Some(value), None, 0);
            }
            "template" => {
                let out = spec
                    .get("text")
                    .cloned()
                    .or_else(|| spec.get("value").cloned())
                    .unwrap_or(Value::String(String::new()));
                self.finish_step(run_id, step_id, StepStatus::Done, Some(out), None, 0);
            }
            "validate" => {
                let value = spec.get("value").cloned().unwrap_or(Value::Null);
                let schema = spec.get("schema").cloned().unwrap_or(json!({}));
                match crate::jsonschema::validate(&schema, &value) {
                    Ok(()) => {
                        self.finish_step(run_id, step_id, StepStatus::Done, Some(value), None, 0)
                    }
                    Err(e) => self.finish_step(
                        run_id,
                        step_id,
                        StepStatus::Failed,
                        Some(value),
                        Some(format!(
                            "validation failed: {}",
                            crate::jsonschema::explain(&e)
                        )),
                        0,
                    ),
                }
            }
            "assert" => {
                let cond = step
                    .field_str("condition")
                    .unwrap_or("false")
                    .trim()
                    .trim_start_matches("CEL:")
                    .trim()
                    .to_string();
                let vars: Vec<(&str, &Value)> = data.iter().map(|(k, v)| (k.as_str(), v)).collect();
                match crate::cel::eval_bool(&cond, &vars) {
                    Ok(true) => self.finish_step(
                        run_id,
                        step_id,
                        StepStatus::Done,
                        Some(json!(true)),
                        None,
                        0,
                    ),
                    Ok(false) => self.finish_step(
                        run_id,
                        step_id,
                        StepStatus::Failed,
                        Some(json!(false)),
                        Some(
                            spec.get("message")
                                .and_then(Value::as_str)
                                .map(str::to_string)
                                .unwrap_or_else(|| format!("assertion failed: {cond}")),
                        ),
                        0,
                    ),
                    Err(e) => self.finish_step(
                        run_id,
                        step_id,
                        StepStatus::Failed,
                        None,
                        Some(format!("assert: {e}")),
                        0,
                    ),
                }
            }
            "fail" => {
                let msg = spec
                    .get("message")
                    .and_then(Value::as_str)
                    .unwrap_or("deliberate failure")
                    .to_string();
                self.finish_step(
                    run_id,
                    step_id,
                    StepStatus::Failed,
                    spec.get("code").cloned(),
                    Some(msg),
                    0,
                );
            }
            "emit" => {
                // With `stream:` this publishes an event to that stream;
                // without one it emits a note/audit record. One step name,
                // addressed by which fields are present.
                if let Some(stream) = spec.get("stream").and_then(Value::as_str) {
                    let stream = stream.to_string();
                    let subject = spec
                        .get("subject")
                        .and_then(Value::as_str)
                        .unwrap_or("")
                        .to_string();
                    let correlation = spec
                        .get("correlation")
                        .and_then(Value::as_str)
                        .map(str::to_string);
                    let data = spec.get("data").cloned().unwrap_or(Value::Null);
                    // The event id IS the step's derived idempotency key: a
                    // crash-replayed emit appends a second copy under the same
                    // id, and consumers drop it from their recent-id ring, so
                    // delivery is at-least-once without a duplicate surviving.
                    let id = crate::engine::run::idempotency_key(run_id, step_id);
                    let source = self
                        .runs
                        .get(run_id)
                        .map(|r| r.workflow.clone())
                        .unwrap_or_default();
                    match self.append_event(
                        &stream,
                        &subject,
                        correlation.as_deref(),
                        data,
                        &id,
                        &source,
                    ) {
                        Ok(seq) => {
                            self.log.info(
                                "stream.emit",
                                json!({"run": run_id, "step": step_id, "stream": stream,
                                       "subject": subject, "seq": seq}),
                            );
                            self.finish_step(
                                run_id,
                                step_id,
                                StepStatus::Done,
                                Some(json!({"id": id, "seq": seq, "stream": stream,
                                           "subject": subject})),
                                None,
                                0,
                            );
                        }
                        Err(e) => {
                            self.finish_step(run_id, step_id, StepStatus::Failed, None, Some(e), 0)
                        }
                    }
                    return;
                }
                if let Some(n) = spec.get("note").and_then(Value::as_str) {
                    let text = format!("run {run_id}: {n}");
                    self.note_root(text);
                }
                if let Some(a) = spec.get("audit") {
                    self.log.info(
                        "audit.emit",
                        json!({"run": run_id, "step": step_id, "audit": a}),
                    );
                }
                self.finish_step(
                    run_id,
                    step_id,
                    StepStatus::Done,
                    spec.get("value").cloned().or(Some(Value::Null)),
                    None,
                    0,
                );
            }
            "finish" => {
                let status = match spec
                    .get("status")
                    .and_then(Value::as_str)
                    .unwrap_or("completed")
                {
                    "completed" => RunStatus::Completed,
                    "refused" => RunStatus::Refused,
                    "cancelled" => RunStatus::Cancelled,
                    _ => RunStatus::Failed,
                };
                let output = spec.get("output").cloned();
                // `outputs.schema` was checked for well-formedness at parse time
                // and then never applied — a workflow could declare the shape of
                // its result and return anything at all. Enforce it here, where
                // the result actually exists. A completed run whose output does
                // not match what it promised is a FAILED run: a caller reading
                // the declared shape is the whole reason to declare one.
                if matches!(status, RunStatus::Completed)
                    && let Some(schema) = self
                        .definition_for_run(run_id)
                        .and_then(|wf| wf.outputs_schema.clone())
                {
                    let value = output.clone().unwrap_or(Value::Null);
                    if let Err(errs) = crate::jsonschema::validate(&schema, &value) {
                        self.finish_step_pub(
                            run_id,
                            step_id,
                            StepStatus::Failed,
                            None,
                            Some(format!(
                                "finish: output does not match the workflow's declared \
                                 outputs.schema: {}",
                                errs.join("; ")
                            )),
                            0,
                        );
                        return;
                    }
                }
                let reason = spec
                    .get("reason")
                    .and_then(Value::as_str)
                    .map(str::to_string);
                self.runs.get_mut(run_id).expect("present").end_step(
                    step_id,
                    StepStatus::Done,
                    output.clone(),
                    None,
                );
                self.runs
                    .get_mut(run_id)
                    .expect("present")
                    .finish(status, output, reason);
                self.on_run_terminal(run_id);
            }
            "sleep" => {
                let ms = spec
                    .get("duration")
                    .map(crate::engine::model::duration_ms)
                    .unwrap_or(Ok(0))
                    .unwrap_or(0);
                match self.timers.arm(
                    &self.durable,
                    now_ms() + ms,
                    json!({"kind": "step", "run": run_id, "step": step_id}),
                    json!({"slept_ms": ms}),
                ) {
                    Ok(id) => {
                        self.runs.get_mut(run_id).expect("present").suspend_step(
                            step_id,
                            json!({"kind": "sleep", "timer": id, "deadline_ms": now_ms() + ms}),
                        );
                        self.checkpoint(false);
                    }
                    Err(e) => self.finish_step(
                        run_id,
                        step_id,
                        StepStatus::Failed,
                        None,
                        Some(format!("sleep: {e}")),
                        0,
                    ),
                }
            }
            "tool" => {
                let name = spec
                    .get("name")
                    .and_then(Value::as_str)
                    .unwrap_or("")
                    .to_string();
                let args = spec.get("args").cloned().unwrap_or(json!({}));
                self.step_tool_call(run_id, step_id, &step_caller, &name, args);
            }
            "http" => self.step_http(run_id, step_id, &spec),
            k if k.starts_with("memory.")
                || k.starts_with("artifact.")
                || k.starts_with("knowledge.")
                || k.starts_with("search.") =>
            {
                let mut args = spec.clone();
                // `ttl` etc. pass through as-is; the contract validates.
                args.retain(|_, v| !v.is_null());
                self.step_tool_call(run_id, step_id, &step_caller, k, Value::Object(args));
            }
            "mcp.tool" => {
                let server = spec
                    .get("server")
                    .and_then(Value::as_str)
                    .unwrap_or("")
                    .to_string();
                let tool = spec
                    .get("tool")
                    .and_then(Value::as_str)
                    .unwrap_or("")
                    .to_string();
                let args = spec.get("args").cloned().unwrap_or(json!({}));
                // Pace calls toward a rated catalog service. A dry bucket fails
                // the step — a refusal the workflow's `retry:` can absorb —
                // rather than blocking the single-writer loop.
                if let Err(e) = self.service_rate_take(&server) {
                    self.finish_step(run_id, step_id, StepStatus::Failed, None, Some(e), 0);
                    return;
                }
                let Some(client) = self.mcp.get(&server).cloned() else {
                    self.finish_step(
                        run_id,
                        step_id,
                        StepStatus::Failed,
                        None,
                        Some(format!("mcp server {server:?} is not connected")),
                        0,
                    );
                    return;
                };
                // The key must NOT vary by attempt: a retry that presents a
                // fresh key is exactly the duplicate the key exists to prevent,
                // so nothing per-attempt may enter it.
                // The attempt rides separately for servers that want to
                // OBSERVE retries without keying on them, and `idempotency:
                // {value: …}` substitutes an application-level key (an order
                // id) when one exists — which beats any run-derived key, since
                // it also collides two different RUNS attempting the same
                // real-world operation.
                let key = spec
                    .get("idempotency")
                    .and_then(|i| i.get("value"))
                    .and_then(Value::as_str)
                    .map(str::to_string)
                    .unwrap_or_else(|| crate::engine::run::idempotency_key(run_id, step_id));
                let meta = json!({"agent/idempotency_key": key, "agent/attempt": attempt, "agent/instance": self.instance, "agent/run": run_id});
                let timeout = step
                    .timeout_ms
                    .map(std::time::Duration::from_millis)
                    .unwrap_or(
                        self.settings
                            .limits
                            .step_timeout
                            .map(|d| d.0)
                            .unwrap_or(std::time::Duration::from_secs(600)),
                    );
                let tx = self.events_tx.clone();
                let (r, s) = (run_id.to_string(), step_id.to_string());
                self.executing
                    .insert(format!("{run_id}/{step_id}"), std::time::Instant::now());
                std::thread::Builder::new()
                    .name(format!("step:{server}.{tool}"))
                    .spawn(move || {
                        let (output, is_error, error) = match client.call_tool_with_meta_within(
                            &tool,
                            Some(args),
                            meta,
                            timeout,
                        ) {
                            Ok(res) => {
                                let v = super::worker::tool_result_value(&res);
                                if res.is_error() {
                                    (v.clone(), true, Some(res.text()))
                                } else {
                                    (v, false, None)
                                }
                            }
                            Err(e) => (Value::Null, true, Some(format!("transport error: {e}"))),
                        };
                        let _ = tx.send(super::events::Event::StepDone {
                            run: r,
                            step: s,
                            output,
                            is_error,
                            error,
                            tokens: 0,
                        });
                    })
                    .ok();
            }
            "agent" | "think" => self.step_turn(run_id, step_id, &step, &spec, &data),
            "foreach" | "batch" | "iterate" | "parallel" | "race" | "subgraph" => {
                self.nested_start(run_id, step_id, &step, &spec)
            }
            "wait" | "join" | "workflow" | "message" | "workflow.signal" | "workflow.wait"
            | "workflow.cancel" | "subagent" | "human" | "mcp.resource" | "a2a.delegate"
            | "a2a.send" | "a2a.wait" | "classify" | "extract" | "summarize" | "judge"
            | "route" => {
                self.execute_orchestration_step(run_id, step_id, &step, &spec, &data, &step_caller)
            }
            "switch" => {
                let on = spec.get("on").cloned().unwrap_or(Value::Null);
                let key = match &on {
                    Value::String(x) => x.clone(),
                    other => other.to_string(),
                };
                let cases = step
                    .field("cases")
                    .and_then(Value::as_object)
                    .cloned()
                    .unwrap_or_default();
                let target = cases
                    .get(&key)
                    .and_then(Value::as_str)
                    .map(str::to_string)
                    .or_else(|| step.field_str("default").map(str::to_string));
                match target {
                    Some(t) => {
                        // The chosen case runs even without its deps being terminal
                        // (an explicit routing edge); the other cases are skipped.
                        let scope_prefix = step_id
                            .rsplit_once('.')
                            .map(|(p, _)| format!("{p}."))
                            .unwrap_or_default();
                        let mut skipped = Vec::new();
                        // Every other target (cases + default) still pending is skipped;
                        // the chosen one is forced (runs even without its deps).
                        let mut others: Vec<String> = cases
                            .values()
                            .filter_map(Value::as_str)
                            .map(str::to_string)
                            .collect();
                        if let Some(d) = step.field_str("default") {
                            others.push(d.to_string());
                        }
                        if let Some(run) = self.runs.get_mut(run_id) {
                            for tid in others {
                                if tid == t {
                                    continue;
                                }
                                let sid = format!("{scope_prefix}{tid}");
                                if let Some(st) = run.steps.get_mut(&sid)
                                    && st.status == StepStatus::Pending
                                {
                                    // Pruned, not skipped: the case was not
                                    // chosen, so its whole tail is dead.
                                    st.status = StepStatus::Pruned;
                                    skipped.push(sid);
                                }
                            }
                            let sid = format!("{scope_prefix}{t}");
                            if let Some(st) = run.steps.get_mut(&sid) {
                                st.status = StepStatus::Pending;
                                st.forced = true;
                            }
                        }
                        self.finish_step(
                            run_id,
                            step_id,
                            StepStatus::Done,
                            Some(json!({"case": key, "goto": t, "skipped": skipped})),
                            None,
                            0,
                        );
                    }
                    // No case, no default: `on_no_match: skip` prunes every
                    // branch and completes — for a switch whose "else" is
                    // honestly "do nothing". The default stays fail-closed.
                    None if step.field_str("on_no_match") == Some("skip") => {
                        let scope_prefix = step_id
                            .rsplit_once('.')
                            .map(|(p, _)| format!("{p}."))
                            .unwrap_or_default();
                        let mut skipped = Vec::new();
                        let others: Vec<String> = cases
                            .values()
                            .filter_map(Value::as_str)
                            .map(str::to_string)
                            .collect();
                        if let Some(run) = self.runs.get_mut(run_id) {
                            for tid in others {
                                let sid = format!("{scope_prefix}{tid}");
                                if let Some(st) = run.steps.get_mut(&sid)
                                    && st.status == StepStatus::Pending
                                {
                                    st.status = StepStatus::Pruned;
                                    skipped.push(sid);
                                }
                            }
                        }
                        self.finish_step(
                            run_id,
                            step_id,
                            StepStatus::Done,
                            Some(json!({"case": key, "matched": false, "skipped": skipped})),
                            None,
                            0,
                        );
                    }
                    None => self.finish_step(
                        run_id,
                        step_id,
                        StepStatus::Failed,
                        Some(json!({"case": key})),
                        Some(format!("switch: no case for {key:?} and no default")),
                        0,
                    ),
                }
            }
            "map" | "filter" | "reduce" | "sort" | "dedupe" | "chunk" | "parse" => {
                let out = match step.kind.as_str() {
                    "map" => crate::engine::data::map(
                        spec.get("over").unwrap_or(&Value::Null),
                        step.field_str("expr").unwrap_or(""),
                        step.field_str("as").unwrap_or("item"),
                        &data,
                    ),
                    "filter" => crate::engine::data::filter(
                        spec.get("over").unwrap_or(&Value::Null),
                        step.field_str("expr").unwrap_or(""),
                        step.field_str("as").unwrap_or("item"),
                        &data,
                    ),
                    "reduce" => crate::engine::data::reduce(
                        spec.get("over").unwrap_or(&Value::Null),
                        step.field_str("expr").unwrap_or(""),
                        spec.get("initial").cloned().unwrap_or(Value::Null),
                        step.field_str("as").unwrap_or("item"),
                        step.field_str("acc").unwrap_or("acc"),
                        &data,
                    ),
                    "sort" => crate::engine::data::sort(
                        spec.get("over").unwrap_or(&Value::Null),
                        spec.get("by").and_then(Value::as_str),
                        spec.get("order").and_then(Value::as_str),
                    ),
                    "dedupe" => crate::engine::data::dedupe(
                        spec.get("over").unwrap_or(&Value::Null),
                        spec.get("by").and_then(Value::as_str),
                    ),
                    "chunk" => crate::engine::data::chunk(
                        spec.get("value").unwrap_or(&Value::Null),
                        spec.get("by").and_then(Value::as_str),
                        spec.get("size").and_then(Value::as_u64).unwrap_or(0) as usize,
                        spec.get("overlap").and_then(Value::as_u64).unwrap_or(0) as usize,
                    ),
                    _ => crate::engine::data::parse(
                        spec.get("text").and_then(Value::as_str).unwrap_or(""),
                        spec.get("format").and_then(Value::as_str),
                    ),
                };
                match out {
                    Ok(v) => self.finish_step(run_id, step_id, StepStatus::Done, Some(v), None, 0),
                    Err(e) => {
                        self.finish_step(run_id, step_id, StepStatus::Failed, None, Some(e), 0)
                    }
                }
            }
            other => self.finish_step(
                run_id,
                step_id,
                StepStatus::Failed,
                None,
                Some(format!(
                    "step kind {other:?} is not executable in this build"
                )),
                0,
            ),
        }
    }

    /// A step's internal tool call (in-loop or deferred/executor).
    fn step_tool_call(
        &mut self,
        run_id: &str,
        step_id: &str,
        caller: &ToolCaller,
        name: &str,
        args: Value,
    ) {
        match self.execute_tool(caller, name, args) {
            ToolOutcome::Ready(v, is_error) => {
                let err = is_error.then(|| match &v {
                    Value::String(s) => s.clone(),
                    o => o.to_string(),
                });
                self.finish_step(
                    run_id,
                    step_id,
                    if is_error {
                        StepStatus::Failed
                    } else {
                        StepStatus::Done
                    },
                    Some(v),
                    err,
                    0,
                );
            }
            ToolOutcome::Deferred(kind) => {
                let wait = match &kind {
                    PendingKind::Timer { id } => json!({"kind": "timer", "timer": id}),
                    PendingKind::Subagent { handle } => {
                        json!({"kind": "subagent", "handle": handle})
                    }
                    PendingKind::Think { .. } => json!({"kind": "think"}),
                    PendingKind::Run { run, .. } => json!({"kind": "run", "run": run}),
                    PendingKind::Await {
                        condition,
                        deadline_ms,
                    } => {
                        json!({"kind": "await", "condition": condition, "deadline_ms": deadline_ms})
                    }
                    PendingKind::Human {
                        task, deadline_ms, ..
                    } => {
                        json!({"kind": "human", "task": task, "deadline_ms": deadline_ms})
                    }
                };
                self.runs
                    .get_mut(run_id)
                    .expect("present")
                    .suspend_step(step_id, wait);
                if !matches!(kind, PendingKind::Timer { .. }) {
                    self.push_pending(super::reactor::PendingTool {
                        target: Target::Step(run_id.to_string(), step_id.to_string()),
                        name: name.to_string(),
                        kind,
                        started_ms: now_ms(),
                    });
                }
                self.checkpoint(false);
            }
            ToolOutcome::Executing => {
                self.executing
                    .insert(format!("{run_id}/{step_id}"), std::time::Instant::now());
            }
        }
    }

    pub(crate) fn step_turn_pub(
        &mut self,
        run_id: &str,
        step_id: &str,
        step: &Step,
        spec: &Map<String, Value>,
        data: &template::Data,
    ) {
        self.step_turn(run_id, step_id, step, spec, data)
    }

    /// An `agent`/`think` step → a turn worker (budget-admitted).
    fn step_turn(
        &mut self,
        run_id: &str,
        step_id: &str,
        step: &Step,
        spec: &Map<String, Value>,
        data: &template::Data,
    ) {
        let is_think = step.kind == "think";
        let prompt = if is_think {
            spec.get("prompt").and_then(Value::as_str).unwrap_or("")
        } else {
            spec.get("instruction")
                .and_then(Value::as_str)
                .unwrap_or("")
        }
        .to_string();
        let output_schema = spec.get("output_schema").cloned();
        let mut messages = Vec::new();
        // `reads`: fold named run data into the prompt.
        if let Some(reads) = spec.get("reads").and_then(Value::as_array) {
            for path in reads.iter().filter_map(Value::as_str) {
                if let Some(v) = template::lookup(path, data) {
                    messages.push(Msg::system(format!("{path} = {v}")));
                }
            }
        }
        // `context`: seed messages — a bare array, or the object form
        // `{cards: [...], seed: [...]}` where `cards` controls which
        // environment sections THIS step's system prompt carries (node-level
        // context control; the config's `context.cards` is the default).
        let step_template: Option<String> = spec
            .get("context")
            .and_then(Value::as_object)
            .and_then(|o| o.get("template"))
            .and_then(Value::as_str)
            .map(str::to_string);
        let seed_list = spec.get("context").and_then(Value::as_array).or_else(|| {
            spec.get("context")
                .and_then(Value::as_object)
                .and_then(|o| o.get("seed"))
                .and_then(Value::as_array)
        });
        if let Some(seed) = seed_list {
            for m in seed {
                match (m["role"].as_str(), m["content"].as_str()) {
                    (Some("system"), Some(c)) => messages.push(Msg::system(c)),
                    (Some("assistant"), Some(c)) => {
                        messages.push(Msg::assistant(Some(c.to_string()), vec![]))
                    }
                    (_, Some(c)) => messages.push(Msg::user(c, None)),
                    _ => {}
                }
            }
        }
        let mut user = prompt.clone();
        if let Some(c) = spec.get("output_contract").and_then(Value::as_str) {
            user.push_str(&format!("\n\nOutput contract:\n{c}"));
        }
        if let Some(s) = &output_schema {
            user.push_str(&format!(
                "\n\nReply with ONLY one JSON object matching this JSON Schema:\n{s}"
            ));
        }
        messages.push(Msg::user(user, None));
        // Skills for the step.
        let skill_bodies: Vec<String> = step
            .skills
            .iter()
            .chain(
                spec.get("skills")
                    .and_then(Value::as_array)
                    .map(|a| {
                        a.iter()
                            .filter_map(Value::as_str)
                            .map(str::to_string)
                            .collect::<Vec<_>>()
                    })
                    .unwrap_or_default()
                    .iter(),
            )
            .filter_map(|name| {
                let mcp = self.mcp.clone();
                let resolver = move |server: &str| -> Option<
                    std::sync::Arc<dyn crate::context::skills::SkillServer>,
                > {
                    mcp.get(server).map(|c| {
                        c.clone() as std::sync::Arc<dyn crate::context::skills::SkillServer>
                    })
                };
                self.skills
                    .load(name, None, &resolver)
                    .ok()
                    .map(|b| format!("### Skill: {}\n{}", b.name, b.body))
            })
            .collect();
        let extra = if skill_bodies.is_empty() {
            None
        } else {
            Some(format!(
                "Loaded skills — follow these instructions when relevant:\n{}",
                skill_bodies.join("\n\n")
            ))
        };
        let system = match spec.get("system").and_then(Value::as_str) {
            Some(s) => s.to_string(),
            None if is_think => format!(
                "You are the reasoning module of {}. Reply with {}. No tools are available.",
                self.instance,
                if output_schema.is_some() {
                    "ONLY one JSON object matching the schema"
                } else {
                    "your conclusion"
                }
            ),
            None => self.system_prompt_named(None, extra.as_deref(), step_template.as_deref()),
        };
        let (tools, internal, routes) = if is_think {
            (Vec::new(), Vec::new(), BTreeMap::new())
        } else {
            let allow: Option<Vec<String>> = spec.get("tools").and_then(Value::as_array).map(|a| {
                a.iter()
                    .filter_map(Value::as_str)
                    .map(str::to_string)
                    .collect()
            });
            self.tool_plan(&Caller::Workflow, allow.as_deref())
        };
        let servers: Vec<String> = match spec.get("servers").and_then(Value::as_array) {
            Some(a) => a
                .iter()
                .filter_map(Value::as_str)
                .map(str::to_string)
                .collect(),
            None => routes
                .values()
                .map(|(s, _)| s.clone())
                .collect::<std::collections::BTreeSet<_>>()
                .into_iter()
                .collect(),
        };
        // Budget admission: the estimate is charged against every scope this
        // run belongs to as well as the instance, so the tightest one binds.
        let est: u64 = messages.iter().map(Msg::est_tokens).sum::<u64>()
            + crate::context::tokens::estimate(&system)
            + 4096;
        let scopes = self.run_scopes(run_id);
        let reservation = match self.governor.admit(est, &scopes, now_ms()) {
            Admission::Ok { reservation, model } => {
                if let Some(m) = model {
                    self.log.info(
                        "budget.degraded",
                        json!({"run": run_id, "step": step_id, "model": m}),
                    );
                }
                Some(reservation)
            }
            Admission::Wait { until_ms, reason } => {
                self.log.info(
                    "budget.wait",
                    json!({"run": run_id, "step": step_id, "until_ms": until_ms, "reason": reason}),
                );
                crate::state::kill_point("budget.waiting");
                match self.timers.arm(
                    &self.durable,
                    until_ms,
                    json!({"kind": "step_budget", "run": run_id, "step": step_id}),
                    Value::Null,
                ) {
                    Ok(id) => {
                        self.runs.get_mut(run_id).expect("present").suspend_step(step_id, json!({"kind": "waiting_budget", "timer": id, "until_ms": until_ms, "reason": reason}));
                        self.checkpoint(false);
                    }
                    Err(e) => self.finish_step(
                        run_id,
                        step_id,
                        StepStatus::Failed,
                        None,
                        Some(format!("budget wait: {e}")),
                        0,
                    ),
                }
                return;
            }
            Admission::Refuse { reason } | Admission::Fail { reason } => {
                self.finish_step(run_id, step_id, StepStatus::Failed, None, Some(reason), 0);
                return;
            }
        };
        // `model:` on an `agent`/`think` node: cost tiering inside one
        // workflow, without forking a subagent process just to change a model
        // string. Read here, because `spec` is shadowed by the `TurnSpec`
        // below.
        let node_model = spec
            .get("model")
            .and_then(Value::as_str)
            .map(str::to_string);
        let limits = spec.get("limits").cloned().unwrap_or(json!({}));
        let max_steps = limits
            .get("steps")
            .and_then(Value::as_u64)
            .map(|s| s as u32)
            .unwrap_or(self.settings.limits.run.steps());
        let max_tokens = step
            .budget
            .or_else(|| limits.get("tokens").and_then(Value::as_u64))
            .unwrap_or(self.settings.limits.run.tokens());
        let deadline_ms = step.timeout_ms.unwrap_or(
            self.settings
                .limits
                .step_timeout
                .map(|d| d.0.as_millis() as u64)
                .unwrap_or(600_000),
        );
        let spec = TurnSpec {
            kind: if is_think {
                TurnKind::Think
            } else {
                TurnKind::Agent
            },
            system,
            messages,
            tools,
            internal,
            mcp_routes: routes,
            output_schema,
            max_rounds: if is_think { 3 } else { 0 },
            budget_admission: self.governor.is_active(),
            idempotency_prefix: format!("{}/{run_id}/{step_id}", self.instance),
            tool_meta: Some(
                json!({"agent/run": run_id, "agent/step": step_id, "agent/instance": self.instance}),
            ),
            temperature: None,
            max_tokens_per_call: 0,
            turn_id: format!(
                "{run_id}/{step_id}#{}",
                self.runs
                    .get(run_id)
                    .and_then(|r| r.step(step_id))
                    .map(|s| s.attempt)
                    .unwrap_or(1)
            ),
        };
        let launch = super::turns::TurnLaunch {
            spec,
            kind: ChildKind::StepTurn {
                run: run_id.to_string(),
                step: step_id.to_string(),
                reservation,
            },
            servers,
            max_steps,
            max_tokens,
            deadline_ms,
            agent_path: format!("run/{run_id}/{step_id}"),
            model: node_model,
        };
        match self.spawn_turn(launch) {
            Ok(node) => {
                if let Some(st) = self
                    .runs
                    .get_mut(run_id)
                    .and_then(|r| r.steps.get_mut(step_id))
                {
                    st.worker = Some(node.0.to_string());
                }
                self.log.info(
                    "step.turn.spawn",
                    json!({"run": run_id, "step": step_id, "node": node.0}),
                );
            }
            Err(e) => {
                if let Some(r) = reservation {
                    self.governor.release(r);
                }
                self.finish_step(
                    run_id,
                    step_id,
                    StepStatus::Failed,
                    None,
                    Some(format!("spawn: {e}")),
                    0,
                );
            }
        }
    }

    /// The governor scopes a run's turns are charged to (workflow budget → run scope).
    fn run_scopes(&mut self, run_id: &str) -> Vec<String> {
        // A run's model spend is charged to the principal it is being done for
        // as well as to the run itself, so a per-person ceiling covers the
        // work someone STARTED, not only the turns they typed.
        let mut scopes = self.principal_scopes(
            self.runs
                .get(run_id)
                .and_then(|r| r.principal.clone())
                .as_deref(),
        );
        let Some(wf) = self.definition_for_run(run_id) else {
            return scopes;
        };
        if let Some(b) = wf
            .limits
            .budget
            .as_ref()
            .and_then(|b| serde_json::from_value::<crate::config::v2::Budget>(b.clone()).ok())
        {
            let key = format!("run:{run_id}");
            self.governor.ensure_scope(&key, &b);
            scopes.push(key);
        }
        scopes
    }

    // ---- outcomes --------------------------------------------------------------

    /// An executor / deferred tool finished a step.
    pub(crate) fn on_step_done(
        &mut self,
        run_id: &str,
        step_id: &str,
        output: Value,
        is_error: bool,
        error: Option<String>,
        tokens: u64,
    ) {
        self.executing.remove(&format!("{run_id}/{step_id}"));
        self.finish_step(
            run_id,
            step_id,
            if is_error {
                StepStatus::Failed
            } else {
                StepStatus::Done
            },
            Some(output),
            error,
            tokens,
        );
    }

    /// A step's turn worker finished.
    pub(crate) fn on_step_turn_done(&mut self, run_id: &str, step_id: &str, turn: TurnResult) {
        let tokens = turn.usage.total();
        if turn.status == "completed" {
            let output = turn
                .value
                .clone()
                .or_else(|| turn.finish.as_ref().and_then(|f| f.get("output").cloned()))
                .or_else(|| turn.text.clone().map(Value::String))
                .unwrap_or(Value::Null);
            // A `finish {status: failed|refused}` from an agent step fails the step.
            let failed = turn
                .finish
                .as_ref()
                .and_then(|f| f.get("status"))
                .and_then(Value::as_str)
                .is_some_and(|s| s != "completed");
            if failed {
                let reason = turn
                    .finish
                    .as_ref()
                    .and_then(|f| f.get("reason"))
                    .and_then(Value::as_str)
                    .unwrap_or("agent finished with a non-completed status")
                    .to_string();
                self.finish_step(
                    run_id,
                    step_id,
                    StepStatus::Failed,
                    Some(output),
                    Some(reason),
                    tokens,
                );
            } else {
                self.finish_step(
                    run_id,
                    step_id,
                    StepStatus::Done,
                    Some(output),
                    None,
                    tokens,
                );
            }
        } else {
            let status = if turn.status == "deadline" {
                StepStatus::Timeout
            } else {
                StepStatus::Failed
            };
            self.finish_step(
                run_id,
                step_id,
                status,
                turn.value
                    .clone()
                    .or_else(|| turn.text.clone().map(Value::String)),
                Some(format!(
                    "turn {}{}",
                    turn.status,
                    turn.error
                        .as_deref()
                        .map(|e| format!(": {e}"))
                        .unwrap_or_default()
                )),
                tokens,
            );
        }
    }

    /// A step timer fired (`sleep` done, or a budget window opened).
    pub(crate) fn on_step_timer(
        &mut self,
        run_id: &str,
        step_id: &str,
        budget: bool,
        payload: &Value,
    ) {
        if budget {
            if let Some(st) = self
                .runs
                .get_mut(run_id)
                .and_then(|r| r.steps.get_mut(step_id))
            {
                st.status = StepStatus::Pending;
                st.wait = None;
            }
            if let Some(r) = self.runs.get_mut(run_id) {
                r.touch();
            }
            return;
        }
        self.finish_step(
            run_id,
            step_id,
            StepStatus::Done,
            Some(payload.clone()),
            None,
            0,
        );
    }

    /// Record a step's terminal outcome; retry / route failures; checkpoint.
    pub(crate) fn finish_step_pub(
        &mut self,
        run_id: &str,
        step_id: &str,
        status: StepStatus,
        output: Option<Value>,
        error: Option<String>,
        tokens: u64,
    ) {
        self.finish_step(run_id, step_id, status, output, error, tokens)
    }

    fn finish_step(
        &mut self,
        run_id: &str,
        step_id: &str,
        status: StepStatus,
        output: Option<Value>,
        error: Option<String>,
        tokens: u64,
    ) {
        // A terminal step may make dependents ready this very iteration — tell
        // the loop to re-run scheduling before it parks (the inline fixpoint).
        self.resched = true;
        let Some(wf) = self
            .runs
            .get(run_id)
            .and_then(|_| self.definition_for_run(run_id))
        else {
            return;
        };
        let Some((step, scope)) = self
            .runs
            .get(run_id)
            .and_then(|r| self.resolve_step(&wf, r, step_id))
        else {
            return;
        };
        {
            let run = self.runs.get_mut(run_id).expect("present");
            if run.status.is_terminal() {
                return; // a late result for a finished run
            }
            run.tokens += tokens;
        }
        crate::obs::metrics::record_step(match status {
            StepStatus::Done => "done",
            StepStatus::Failed => "failed",
            _ => "other",
        });
        // An output past `limits.inline_max_bytes` is stored as an artifact and
        // replaced by a reference, keeping the run record (and every checkpoint
        // that serializes it) bounded. A failed artifact write keeps the inline
        // value rather than losing the output.
        let output = match output {
            Some(v) if !v.is_null() => {
                let cap = self.settings.limits.inline_max_bytes.unwrap_or(65_536) as usize;
                if v.to_string().len() > cap {
                    match self.artifacts.create(
                        &self.durable,
                        super::artifacts::NewArtifact {
                            name: &format!("{run_id}/{step_id}/output.json"),
                            mime: Some("application/json"),
                            content: v.clone(),
                            created_by: Some("engine"),
                            sensitive: false,
                            owner: Some(run_id),
                        },
                    ) {
                        Ok(meta) => {
                            self.log.info("step.output.artifact", json!({"run": run_id, "step": step_id, "artifact": meta["id"], "size": meta["size"]}));
                            Some(json!({"$artifact": meta["id"], "size": meta["size"]}))
                        }
                        Err(e) => {
                            self.log.warn(
                                "step.output.artifact_fail",
                                json!({"run": run_id, "step": step_id, "err": e}),
                            );
                            Some(v)
                        }
                    }
                } else {
                    Some(v)
                }
            }
            other => other,
        };
        // A declared `output_schema` is enforced here: a step that completed
        // but produced a shape its consumers cannot parse fails instead.
        let (status, error) = match (&status, &step.output_schema, &output) {
            (StepStatus::Done, Some(schema), Some(out)) => {
                match crate::jsonschema::validate(schema, out) {
                    Ok(()) => (status, error),
                    Err(e) => (
                        StepStatus::Failed,
                        Some(format!(
                            "output does not match output_schema: {}",
                            crate::jsonschema::explain(&e)
                        )),
                    ),
                }
            }
            _ => (status, error),
        };
        let attempt = self
            .runs
            .get(run_id)
            .and_then(|r| r.step(step_id))
            .map(|s| s.attempt)
            .unwrap_or(1);
        self.log.info("step.done", json!({"run": run_id, "step": step_id, "status": status, "attempt": attempt, "tokens": tokens, "err": error}));
        // Feed the circuit breaker, when this step keeps one. Every ATTEMPT
        // counts (a breaker measures calls, not runs) — except our own
        // fast-fails: refusing to dial is not evidence about the remote.
        if matches!(
            step.kind.as_str(),
            "http" | "mcp.tool" | "a2a.send" | "a2a.delegate"
        ) && let Some(cfg) = self.effective_breaker(&step)
            && !matches!(status, StepStatus::Failed | StepStatus::Timeout if error
                .as_deref()
                .is_some_and(|e| e.starts_with(super::breaker::OPEN_ERR)))
            && matches!(
                status,
                StepStatus::Done | StepStatus::Failed | StepStatus::Timeout
            )
        {
            let workflow = self
                .runs
                .get(run_id)
                .map(|r| r.workflow.clone())
                .unwrap_or_default();
            let key = super::breaker::key(&workflow, step_id);
            let mut st = self
                .durable
                .manifest()
                .breakers
                .get(&key)
                .cloned()
                .unwrap_or_else(|| json!({}));
            let t = super::breaker::record(&mut st, cfg, status == StepStatus::Done, now_ms());
            self.durable.manifest_update(|m| {
                m.breakers.insert(key.clone(), st);
            });
            match t {
                super::breaker::Transition::Opened { fails } => self.log.warn(
                    "breaker.open",
                    json!({"breaker": key, "consecutive_failures": fails,
                           "cooldown": format!("{}ms", cfg.cooldown_ms)}),
                ),
                super::breaker::Transition::Reopened => self.log.warn(
                    "breaker.reopen",
                    json!({"breaker": key, "probe_failed": true}),
                ),
                super::breaker::Transition::Closed => {
                    self.log.info("breaker.closed", json!({"breaker": key}))
                }
                super::breaker::Transition::None => {}
            }
        }
        #[cfg(feature = "a2a")]
        self.feed_push(
            "step",
            crate::runtime::a2a_server::FeedVis::Operator,
            json!({"run": run_id, "step": step_id, "phase": "done",
                   "status": crate::runtime::nested::StatusLabel::as_label(&status), "attempt": attempt, "tokens": tokens,
                   // Same 2 KiB cap the run drill-down applies: a step output can
                   // be an entire document, and a feed is not the place for it.
                   "err": error.as_deref().map(|e| {
                       e.chars().take(2048).collect::<String>()
                   })}),
        );
        if matches!(status, StepStatus::Failed | StepStatus::Timeout) {
            // Retry?
            if let Some(retry) = &step.retry
                && attempt <= retry.max
            {
                // Exponential, with jitter. Without it every step that failed
                // in the same wave — the usual case, since they usually failed
                // for the same upstream reason — retries in lockstep and
                // rebuilds the thundering herd the backoff exists to break up.
                // Deterministic per (run, step, attempt): no RNG, so a replay
                // reproduces the same schedule.
                let base = retry
                    .backoff_ms
                    .saturating_mul(1u64 << (attempt.saturating_sub(1)).min(10));
                let backoff = if base == 0 {
                    0
                } else {
                    let mut h: u64 = 1469598103934665603;
                    for b in run_id.bytes().chain(step_id.bytes()).chain([attempt as u8]) {
                        h ^= b as u64;
                        h = h.wrapping_mul(1099511628211);
                    }
                    // ±20% around the base.
                    let spread = (base / 5).max(1);
                    base.saturating_sub(spread) + (h % (spread * 2 + 1))
                };
                self.log.info("step.retry", json!({"run": run_id, "step": step_id, "attempt": attempt, "backoff_ms": backoff}));
                if backoff == 0 {
                    if let Some(st) = self
                        .runs
                        .get_mut(run_id)
                        .and_then(|r| r.steps.get_mut(step_id))
                    {
                        st.status = StepStatus::Pending;
                        st.error = error;
                    }
                } else {
                    match self.timers.arm(
                        &self.durable,
                        now_ms() + backoff,
                        json!({"kind": "step_budget", "run": run_id, "step": step_id}),
                        Value::Null,
                    ) {
                        Ok(id) => {
                            self.runs.get_mut(run_id).expect("present").suspend_step(
                                step_id,
                                json!({"kind": "retry_backoff", "timer": id, "error": error}),
                            );
                        }
                        Err(_) => {
                            if let Some(st) = self
                                .runs
                                .get_mut(run_id)
                                .and_then(|r| r.steps.get_mut(step_id))
                            {
                                st.status = StepStatus::Pending;
                            }
                        }
                    }
                }
                self.checkpoint(false);
                return;
            }
            let err_text = error.clone().unwrap_or_else(|| "failed".into());
            self.runs
                .get_mut(run_id)
                .expect("present")
                .end_step(step_id, status, output, error);
            // `on_timeout`: a deadline expiring on a wait is usually an
            // EXPECTED branch (nobody replied, the alert never cleared), not
            // a failure — route to the named step, forced, and keep the run
            // alive. Only a real Timeout takes this edge; other errors still
            // answer to `on_error`. The step itself stays `Timeout`, which
            // does NOT satisfy dependents — so the success path and the
            // timeout path are mutually exclusive by construction.
            if status == StepStatus::Timeout
                && let Some(t) = step.field_str("on_timeout")
            {
                let target = match &scope {
                    Some(sc) => super::nested::scoped_id(&sc.parent, t),
                    None => t.to_string(),
                };
                if let Some(st) = self
                    .runs
                    .get_mut(run_id)
                    .and_then(|r| r.steps.get_mut(&target))
                {
                    st.status = StepStatus::Pending;
                    st.forced = true;
                }
                self.log.info(
                    "step.timeout_routed",
                    json!({"run": run_id, "step": step_id, "to": t}),
                );
                crate::state::kill_point("step.before_done");
                self.checkpoint(false);
                if scope.is_some() {
                    self.on_scoped_step_done(run_id, step_id);
                }
                return;
            }
            if let Some(sc) = &scope {
                // Inside a body: `continue` marks done-with-error, `goto` re-arms
                // a sibling, `fail` leaves the step failed for the parent to judge.
                match &step.on_error {
                    OnError::Continue => {
                        if let Some(st) = self
                            .runs
                            .get_mut(run_id)
                            .and_then(|r| r.steps.get_mut(step_id))
                        {
                            st.status = StepStatus::Done;
                            st.error = Some(err_text.clone());
                            if st.output.is_none() {
                                st.output = Some(json!({"error": err_text}));
                            }
                        }
                    }
                    OnError::Goto(t) => {
                        let sid = super::nested::scoped_id(&sc.parent, t);
                        if let Some(st) = self
                            .runs
                            .get_mut(run_id)
                            .and_then(|r| r.steps.get_mut(&sid))
                        {
                            st.status = StepStatus::Pending;
                            st.forced = true;
                        }
                    }
                    OnError::Fail => {}
                }
                crate::state::kill_point("step.before_done");
                if !crate::engine::model::pure_data_kind(&step.kind) {
                    self.checkpoint(false);
                }
                self.on_scoped_step_done(run_id, step_id);
                return;
            }
            let routed = run::route_failure(
                &wf,
                self.runs.get_mut(run_id).expect("present"),
                &step,
                &err_text,
            );
            match routed {
                Ok(next) => {
                    if !next.is_empty() {
                        self.log.info(
                            "step.goto",
                            json!({"run": run_id, "from": step_id, "to": next}),
                        );
                    }
                }
                Err(reason) => {
                    self.cancel_children_of_run(run_id, "run failed");
                    self.runs.get_mut(run_id).expect("present").finish(
                        RunStatus::Failed,
                        None,
                        Some(reason),
                    );
                    self.on_run_terminal(run_id);
                    return;
                }
            }
            if let OnError::Continue = step.on_error {
                // Already marked done-with-error by route_failure.
            }
        } else {
            // Memoize (`cache`).
            if step.cache.is_some()
                && status == StepStatus::Done
                && let Some(key) = self
                    .runs
                    .get(run_id)
                    .and_then(|r| r.steps.get(step_id))
                    .and_then(|st| st.cache_key.clone())
                && let Some(out) = &output
            {
                self.cache_store(&key, out);
            }
            self.runs
                .get_mut(run_id)
                .expect("present")
                .end_step(step_id, status, output, error);
        }
        crate::state::kill_point("step.before_done");
        // Pure steps ride the tick's checkpoint — unless this completion made
        // the RUN terminal (a routed failure), which must land durably now.
        let terminal_now = self
            .runs
            .get(run_id)
            .is_some_and(|r| r.status.is_terminal());
        if terminal_now || !crate::engine::model::pure_data_kind(&step.kind) {
            self.checkpoint(false);
        }
        if scope.is_some() {
            self.on_scoped_step_done(run_id, step_id);
        }
    }

    /// The run reached a terminal state: report, wake, plan bindings, counters.
    /// Evict terminal runs beyond `store.retention.runs`.
    ///
    /// Without this a long-lived instance keeps one durable record per run
    /// forever — on a laptop, the difference between an agent that runs for a
    /// month and one that fills a disk. Only TERMINAL runs are candidates:
    /// nothing in flight is ever dropped, whatever the policy says. Default is
    /// unbounded, so an operator who has not thought about it keeps today's
    /// behaviour.
    fn evict_terminal_runs(&mut self) {
        let policy = &self.settings.store.retention.runs;
        let keep_last = policy.keep_last;
        let ttl_ms = policy.ttl.as_ref().map(|d| d.0.as_millis() as u64);
        if keep_last.is_none() && ttl_ms.is_none() {
            return;
        }
        let now = now_ms();
        // Newest first, so "keep the last N" is a prefix.
        let mut terminal: Vec<(String, u64)> = self
            .runs
            .values()
            .filter(|r| r.status.is_terminal())
            .map(|r| (r.id.clone(), r.finished.unwrap_or(0)))
            .collect();
        terminal.sort_by_key(|(_, finished)| std::cmp::Reverse(*finished));

        let mut drop: Vec<String> = Vec::new();
        for (i, (id, finished)) in terminal.iter().enumerate() {
            let over_count = keep_last.is_some_and(|k| i >= k as usize);
            let over_age = ttl_ms.is_some_and(|t| now.saturating_sub(*finished) > t);
            if over_count || over_age {
                drop.push(id.clone());
            }
        }
        for id in drop {
            // A non-durable run has nothing in the store to evict.
            let was_durable = self.runs.get(&id).is_none_or(|r| r.durable);
            self.runs.remove(&id);
            if was_durable && let Err(e) = self.durable.delete(crate::state::Kind::Run, &id) {
                self.log
                    .warn("run.evict.fail", json!({"run": id, "err": e.to_string()}));
                continue;
            }
            self.log.info("run.evicted", json!({"run": id}));
        }
    }

    pub(crate) fn on_run_terminal(&mut self, run_id: &str) {
        let Some(run) = self.runs.get(run_id) else {
            return;
        };
        let (status, output, error, workflow) = (
            run.status,
            run.output.clone(),
            run.error.clone(),
            run.workflow.clone(),
        );
        #[cfg(feature = "a2a")]
        let a2a_task = run.task.clone();
        // Eviction runs here because this is the only moment the candidate set
        // grows. Deferred to the end of the function so the run's own
        // completion handling (webhook reply, A2A task, feed) happens first —
        // evicting a record before its result was delivered would be a fine way
        // to lose an answer.
        let evict_after = true;
        // A `respond: sync` webhook awaiting this run gets its result now.
        #[cfg(feature = "a2a")]
        self.webhook_sync_reply(run_id);
        // A queued child run (`child_run` wait) resolves its parent step.
        if let Some(parent) = self.runs.get(run_id).and_then(|r| r.parent.clone())
            && let (Some(pr), Some(ps)) = (
                parent["run"].as_str().map(str::to_string),
                parent["step"].as_str().map(str::to_string),
            )
            && self
                .runs
                .get(&pr)
                .and_then(|r| r.steps.get(&ps))
                .is_some_and(|st| {
                    st.status == StepStatus::Suspended
                        && st.wait.as_ref().is_some_and(|w| w["kind"] == "child_run")
                })
        {
            self.finish_step_pub(
                &pr,
                &ps,
                if status == RunStatus::Completed {
                    StepStatus::Done
                } else {
                    StepStatus::Failed
                },
                Some(json!({"run": run_id, "status": status, "output": output, "error": error})),
                (status != RunStatus::Completed).then(|| {
                    error
                        .clone()
                        .unwrap_or_else(|| format!("child run {}", status.as_str()))
                }),
                0,
            );
        }
        self.counters.runs_finished += 1;
        crate::obs::metrics::record_run(match status {
            RunStatus::Completed => crate::obs::metrics::RunOutcome::Completed,
            RunStatus::Cancelled => crate::obs::metrics::RunOutcome::Killed,
            _ => crate::obs::metrics::RunOutcome::Failed,
        });
        crate::obs::metrics::record_run_status(status.as_str());
        self.log.info("run.done", json!({"run": run_id, "workflow": workflow, "status": status, "err": error, "output": if self.log.content_capture() { output.clone().unwrap_or(Value::Null) } else { Value::Null }}));
        self.governor.drop_scope(&format!("run:{run_id}"));
        self.retire_sweep();
        // Durable-pin GC for the ordinary path: when the LAST run of a
        // definition version lands, its stored pin has no reader left. (A
        // still-armed workflow re-pins on its next run's first start.)
        if let Some(hash) = self.runs.get(run_id).map(|r| r.workflow_hash.clone())
            && !self
                .runs
                .values()
                .any(|r| !r.status.is_terminal() && r.workflow_hash == hash)
        {
            let _ = self.durable.delete(
                crate::state::Kind::Memory,
                &format!("{}{hash}", super::retire::PIN_PREFIX),
            );
            self.pin_written.remove(&hash);
        }
        // Answer waiters (workflow.wait / run sync).
        let waiting: Vec<Target> = self
            .pending
            .iter()
            .filter(|p| matches!(&p.kind, PendingKind::Run { run, .. } if run == run_id))
            .map(|p| p.target.clone())
            .collect();
        self.pending
            .retain(|p| !matches!(&p.kind, PendingKind::Run { run, .. } if run == run_id));
        for t in waiting {
            self.reply(
                &t,
                json!({"run": run_id, "status": status, "output": output, "error": error}),
                false,
            );
        }
        // Plan bindings + the root note (wake policy).
        let ok = status == RunStatus::Completed;
        let note = error
            .clone()
            .or_else(|| output.as_ref().map(|o| o.to_string()))
            .unwrap_or_default();
        self.settle_plan_bindings(
            &crate::context::plan::Binding::Run {
                id: run_id.to_string(),
            },
            ok,
            &note,
        );
        let wake = self.settings.agent.wake_on();
        let notify = match self.settings.agent.on_workflow_finished {
            crate::config::v2::OnWorkflowFinished::Ignore => false,
            _ => {
                ok && wake.contains(&crate::config::v2::WakeEvent::WorkflowFinished)
                    || !ok && wake.contains(&crate::config::v2::WakeEvent::WorkflowFailed)
            }
        };
        if notify && !self.job_shape {
            let short = if note.chars().count() > 400 {
                format!("{}", note.chars().take(400).collect::<String>())
            } else {
                note.clone()
            };
            let line = format!(
                "workflow {workflow} run {run_id} {}: {short}",
                status.as_str()
            );
            match self.settings.agent.on_workflow_finished {
                // `note` appends to the root transcript and waits for whatever
                // happens next to read it. `think` delivers, which starts a
                // turn: the difference between leaving a message and making
                // the call. The hop depth continues this run's chain, so a
                // workflow the agent started cannot wake it without bound.
                crate::config::v2::OnWorkflowFinished::Think => {
                    let depth = self.runs.get(run_id).map(|r| r.msg_depth).unwrap_or(0) + 1;
                    let cap = self.settings.limits.message_depth();
                    if depth > cap {
                        self.log.warn(
                            "message.too_deep",
                            json!({"run": run_id, "reason": "on_workflow_finished",
                                   "depth": depth, "max": cap}),
                        );
                        self.note_root(line);
                    } else {
                        let principal = self.runs.get(run_id).and_then(|r| r.principal.clone());
                        if let Err(e) = self.accept_event(
                            kinds::A2A_MESSAGE,
                            principal,
                            json!({"text": line.clone(), "context_id": crate::context::ROOT,
                                   "msg_depth": depth}),
                        ) {
                            self.log
                                .warn("workflow.think.fail", json!({"run": run_id, "err": e}));
                            self.note_root(line);
                        }
                    }
                }
                _ => self.note_root(line),
            }
        }
        // A `loop` start re-arms the next iteration; `event` start nodes fire on
        // workflow.finished/failed.
        if let Some((wf, node, spec, kind)) = self.run_start_spec(run_id)
            && kind == "loop"
        {
            self.on_loop_run_finished(
                &wf,
                &node,
                &spec,
                ok,
                &output.clone().unwrap_or(Value::Null),
            );
        }
        if let Some(ev) = super::starts::run_event(status) {
            self.fire_event_starts(
                ev,
                &json!({"run": run_id, "workflow": workflow, "status": status.as_str()}),
            );
        }
        // A run started over A2A drives its task to the run's outcome.
        #[cfg(feature = "a2a")]
        if let Some(tid) = &a2a_task {
            self.a2a_task_for_run(tid, status.as_str(), output.as_ref(), error.as_deref());
        }
        self.checkpoint(false);
        if evict_after {
            self.evict_terminal_runs();
        }
    }

    /// Cancel a run: cancel its children, fail suspended waits, mark cancelled.
    pub(crate) fn cancel_run(&mut self, run_id: &str, reason: &str) {
        // Cascade to child runs started with `cascade: true` — a cancelled
        // parent must not leave its children running unattended.
        let kids: Vec<String> = self
            .runs
            .values()
            .filter(|r| {
                !r.status.is_terminal()
                    && r.parent.as_ref().is_some_and(|p| {
                        p["run"].as_str() == Some(run_id) && p["cascade"].as_bool().unwrap_or(true)
                    })
            })
            .map(|r| r.id.clone())
            .collect();
        for k in kids {
            self.cancel_run(&k, "parent run cancelled");
        }
        self.cancel_children_of_run(run_id, reason);
        let timers = self.timers.owned_by(|o| o["run"].as_str() == Some(run_id));
        for t in timers {
            let _ = self.timers.disarm(&self.durable, &t);
        }
        self.pending
            .retain(|p| !matches!(&p.target, Target::Step(r, _) if r == run_id));
        if let Some(r) = self.runs.get_mut(run_id)
            && !r.status.is_terminal()
        {
            r.finish(RunStatus::Cancelled, None, Some(reason.to_string()));
            self.on_run_terminal(run_id);
        }
    }

    fn cancel_children_of_run(&mut self, run_id: &str, reason: &str) {
        let nodes: Vec<_> = self
            .children
            .iter()
            .filter(|(_, c)| matches!(&c.kind, ChildKind::StepTurn { run, .. } if run == run_id))
            .map(|(n, _)| *n)
            .collect();
        for n in nodes {
            self.children.cancel(n, reason);
        }
    }

    // ---- workflow.* tools ------------------------------------------------------

    pub(crate) fn workflow_tool(
        &mut self,
        caller: &ToolCaller,
        name: &str,
        args: Value,
    ) -> ToolOutcome {
        let err = |e: String| ToolOutcome::Ready(Value::String(e), true);
        match name {
            "workflow.run" => {
                let wname = args["name"].as_str().unwrap_or("").to_string();
                let Some(w) = self.workflows.get(&wname) else {
                    return err(format!("no such workflow {wname:?}"));
                };
                if let Some(cause) = self
                    .pressure
                    .refusal(w.priority == crate::engine::model::Priority::Low)
                {
                    return err(format!(
                        "workflow.run refused: {cause}; retry when it clears"
                    ));
                }
                let start = match args.get("start").and_then(Value::as_str) {
                    Some(s) => match w.step(s) {
                        Some(st) if st.is_start() => s.to_string(),
                        _ => return err(format!("workflow {wname:?} has no start node {s:?}")),
                    },
                    None => match default_start(w) {
                        Some(s) => s,
                        None => return err(format!("workflow {wname:?} has no start node")),
                    },
                };
                let wait = args.get("wait").and_then(Value::as_bool).unwrap_or(false);
                let timeout_ms = args
                    .get("timeout")
                    .and_then(Value::as_str)
                    .and_then(|t| crate::config::parse_duration(t).ok())
                    .map(|d| d.as_millis() as u64)
                    .unwrap_or(3_600_000);
                let request = match (caller.node, &caller.run, &caller.step) {
                    (Some(n), _, _) => {
                        json!({"node": n.0, "req": caller.req, "wait": wait, "timeout_ms": timeout_ms})
                    }
                    (None, Some(r), Some(s)) => {
                        json!({"run": r, "step": s, "wait": wait, "timeout_ms": timeout_ms})
                    }
                    _ => Value::Null,
                };
                let payload = json!({"workflow": wname, "node": start, "payload": {"requested_by": caller.label_pub()}, "inputs": args.get("inputs").cloned().unwrap_or(json!({})), "request": request, "conversation": caller.ctx, "msg_depth": caller.msg_depth});
                match self.accept_event(kinds::WORKFLOW_RUN, caller.principal.clone(), payload) {
                    Ok(_) => {
                        // Process it right away so the caller learns the run id.
                        if let Some(ev) = self.inbox_queue.pop_back() {
                            let done = self.on_start_event(&ev);
                            if done {
                                self.inbox_done(&ev.id);
                            }
                        }
                        // The reply is (or will be) delivered through the request
                        // target: immediately with the run id, or when the run
                        // finishes for `wait: true` (registered by `on_start_event`).
                        ToolOutcome::Executing
                    }
                    Err(e) => err(e),
                }
            }
            "workflow.list" => ToolOutcome::Ready(
                json!({"workflows": self.workflows.values().map(|w| json!({
                    "name": w.name, "description": w.description, "armed": w.armed, "hash": w.hash,
                    "starts": w.start_steps().iter().map(|s| json!({"node": s.id, "kind": s.kind})).collect::<Vec<_>>(),
                    "runs": self.runs.values().filter(|r| r.workflow == w.name).map(|r| json!({"id": r.id, "status": r.status})).collect::<Vec<_>>(),
                })).collect::<Vec<_>>()}),
                false,
            ),
            "workflow.status" => {
                let runs: Vec<Value> = match (
                    args.get("run").and_then(Value::as_str),
                    args.get("name").and_then(Value::as_str),
                ) {
                    (Some(id), _) => self
                        .runs
                        .get(id)
                        .map(|r| vec![run_detail(r)])
                        .unwrap_or_default(),
                    (None, Some(n)) => self
                        .runs
                        .values()
                        .filter(|r| r.workflow == n)
                        .map(RunState::summary)
                        .collect(),
                    _ => self.runs.values().map(RunState::summary).collect(),
                };
                ToolOutcome::Ready(json!({"runs": runs}), false)
            }
            "workflow.cancel" => {
                let id = args["run"].as_str().unwrap_or("").to_string();
                if !self.runs.contains_key(&id) {
                    return err(format!("no such run {id:?}"));
                }
                let reason = args
                    .get("reason")
                    .and_then(Value::as_str)
                    .unwrap_or("cancelled by request")
                    .to_string();
                self.cancel_run(&id, &reason);
                ToolOutcome::Ready(
                    json!({"ok": true, "status": self.runs.get(&id).map(|r| r.status.as_str()).unwrap_or("cancelled")}),
                    false,
                )
            }
            "workflow.wait" => {
                let id = args["run"].as_str().unwrap_or("").to_string();
                let timeout_ms = args
                    .get("timeout")
                    .and_then(Value::as_str)
                    .and_then(|t| crate::config::parse_duration(t).ok())
                    .map(|d| d.as_millis() as u64)
                    .unwrap_or(3_600_000);
                match self.runs.get(&id) {
                    None => err(format!("no such run {id:?}")),
                    Some(r) if r.status.is_terminal() => ToolOutcome::Ready(
                        json!({"run": id, "status": r.status, "output": r.output, "error": r.error}),
                        false,
                    ),
                    Some(_) => ToolOutcome::Deferred(PendingKind::Run {
                        run: id,
                        deadline_ms: now_ms() + timeout_ms,
                    }),
                }
            }
            "workflow.pause" | "workflow.resume" => {
                let pause = name == "workflow.pause";
                // `before_step`: pause the run the moment a named step is about
                // to start, rather than immediately. This is a breakpoint —
                // "stop when you reach `notify`" — which is what you actually
                // want when debugging a graph, and it needs no new surface
                // because pause already exists and already survives a restart.
                if pause
                    && let Some(id) = args.get("run").and_then(Value::as_str)
                    && let Some(step) = args.get("before_step").and_then(Value::as_str)
                {
                    let known = self
                        .definition_for_run(id)
                        .is_some_and(|wf| wf.steps.contains_key(step));
                    if !known {
                        return err(format!(
                            "before_step {step:?} is not a step of this run's workflow"
                        ));
                    }
                    match self.runs.get_mut(id) {
                        None => return err(format!("no such run {id:?}")),
                        Some(r) => {
                            r.break_before = Some(step.to_string());
                            r.dirty = true;
                        }
                    }
                    self.log
                        .info("run.breakpoint", json!({"run": id, "before_step": step}));
                    return ToolOutcome::Ready(json!({"run": id, "break_before": step}), false);
                }
                if let Some(id) = args.get("run").and_then(Value::as_str) {
                    match self.runs.get_mut(id) {
                        None => return err(format!("no such run {id:?}")),
                        Some(r) if r.status.is_terminal() => {
                            return err(format!("run {id:?} is already {}", r.status.as_str()));
                        }
                        Some(r) => {
                            r.status = if pause {
                                RunStatus::Paused
                            } else {
                                RunStatus::Running
                            };
                            r.touch();
                        }
                    }
                    return ToolOutcome::Ready(json!({"ok": true}), false);
                }
                if let Some(n) = args.get("name").and_then(Value::as_str) {
                    match self.workflows.get_mut(n) {
                        None => return err(format!("no such workflow {n:?}")),
                        // The definitions are shared (`Arc`) on the hot path;
                        // arming is the one mutation, and it is rare —
                        // copy-on-write is the honest cost here.
                        Some(w) => std::sync::Arc::make_mut(w).armed = !pause,
                    }
                    if !pause {
                        self.arm_workflows();
                    }
                    return ToolOutcome::Ready(json!({"ok": true}), false);
                }
                err(format!("{name}: give run or name"))
            }
            "workflow.create" | "workflow.update" => {
                // Workflows are STANDING instructions — what the agent does
                // when a schedule fires or a webhook lands, unattended. An
                // agent that can rewrite them changes what happens next time,
                // and the change outlives the conversation that caused it.
                if let Some(e) = self.workflows_locked(name) {
                    return e;
                }
                let def = args["definition"].clone();
                match parse_workflow(&def) {
                    Err(e) => err(format!("{name}: {}", e.join("; "))),
                    Ok(w) if w.tool.is_some() => err(format!(
                        "{name}: a `tool:` block may only be declared in the startup config.                          The tool registry is built once and validated fail-closed; minting                          or shadowing a tool name at runtime would put no operator in the                          loop. (workflow {:?})",
                        w.name
                    )),
                    Ok(mut w) => {
                        self.fill_durable_default(&mut w);
                        if name == "workflow.create" && self.workflows.contains_key(&w.name) {
                            return err(format!(
                                "workflow {:?} exists (use workflow.update)",
                                w.name
                            ));
                        }
                        if name == "workflow.update" && !self.workflows.contains_key(&w.name) {
                            return err(format!(
                                "workflow {:?} does not exist (use workflow.create)",
                                w.name
                            ));
                        }
                        let (wname, hash) = (w.name.clone(), w.hash.clone());
                        // Durable definition (memory/_workflows/<name>).
                        let rec = crate::context::memory::Record {
                            value: def,
                            ts: now_ms(),
                            ttl_ms: None,
                            by: Some(caller.label_pub()),
                        };
                        if let Err(e) = self.durable.put(
                            Kind::Memory,
                            &format!("{WORKFLOW_DEF_PREFIX}{wname}"),
                            serde_json::to_value(&rec).unwrap_or(Value::Null),
                            None,
                        ) {
                            return err(format!("{name}: store: {e}"));
                        }
                        let arm = args.get("arm").and_then(Value::as_bool).unwrap_or(true);
                        let mut w = w;
                        w.armed = arm;
                        self.workflows.insert(wname.clone(), std::sync::Arc::new(w));
                        self.log.info(
                            "workflow.defined",
                            json!({"name": wname, "hash": &hash[..12], "op": name}),
                        );
                        if arm {
                            self.arm_workflows();
                        }
                        ToolOutcome::Ready(
                            json!({"name": wname, "hash": hash, "armed": arm}),
                            false,
                        )
                    }
                }
            }
            "workflow.delete" => {
                if let Some(e) = self.workflows_locked(name) {
                    return e;
                }
                let wname = args["name"].as_str().unwrap_or("").to_string();
                let Some(wf) = self.workflows.remove(&wname) else {
                    return err(format!("no such workflow {wname:?}"));
                };
                let _ = self
                    .durable
                    .delete(Kind::Memory, &format!("{WORKFLOW_DEF_PREFIX}{wname}"));
                // Retire rather than drop: retirement pins the definition so
                // live runs keep resolving `definition_for_run` mid-flight, and
                // applies the workflow's `unload:` policy to them. Delete means
                // "stop being a workflow", not "strand whatever is in flight".
                self.retire_workflow(&wf, "deleted");
                self.log.info("workflow.deleted", json!({"name": wname}));
                ToolOutcome::Ready(json!({"ok": true}), false)
            }
            "workflow.signal" => {
                let sname = args["name"].as_str().unwrap_or("").to_string();
                let _ = self.accept_event(kinds::SIGNAL, caller.principal.clone(), json!({"name": sname, "payload": args.get("payload").cloned().unwrap_or(Value::Null), "run": args.get("run"), "from": caller.label_pub()}));
                // The signal goes on the durable inbox; waits and `signal`
                // start nodes are woken when the loop drains it, so no
                // delivery count is available at this point.
                ToolOutcome::Ready(
                    json!({"delivered": 0, "note": "signal recorded; waits and signal start nodes are woken when the loop drains the inbox"}),
                    false,
                )
            }
            _ => err(format!("unknown workflow tool {name}")),
        }
    }
}

impl ToolCaller {
    pub(crate) fn label_pub(&self) -> String {
        if let Some(s) = &self.subagent {
            return format!("subagent:{s}");
        }
        if let (Some(r), Some(s)) = (&self.run, &self.step) {
            return format!("step:{r}/{s}");
        }
        format!(
            "ctx:{}",
            self.ctx.as_deref().unwrap_or(crate::context::ROOT)
        )
    }
}

/// The start node `workflow.run` uses by default: `manual`, else the first.
fn default_start(w: &Workflow) -> Option<String> {
    let starts = w.start_steps();
    starts
        .iter()
        .find(|s| s.kind == "manual")
        .or_else(|| starts.first())
        .map(|s| s.id.clone())
}

fn node_kind<'a>(w: &'a Workflow, node: &str) -> Option<&'a str> {
    w.step(node).map(|s| s.kind.as_str())
}

fn run_detail(r: &RunState) -> Value {
    let mut v = r.summary();
    v["step_states"] = json!(r.steps);
    v["vars"] = Value::Object(r.vars.clone());
    v
}

/// The `memory.<key>` roots a value's templates reference.
fn collect_memory_keys(v: &Value, out: &mut Vec<String>) {
    match v {
        Value::String(s) => {
            let mut rest = s.as_str();
            while let Some(i) = rest.find("memory.") {
                let after = &rest[i + "memory.".len()..];
                let key: String = after
                    .chars()
                    .take_while(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '-' | '/' | ':'))
                    .collect();
                if !key.is_empty() && !out.contains(&key) {
                    out.push(key.clone());
                }
                rest = &after[key.len().min(after.len())..];
            }
        }
        Value::Array(a) => a.iter().for_each(|x| collect_memory_keys(x, out)),
        Value::Object(o) => o.values().for_each(|x| collect_memory_keys(x, out)),
        _ => {}
    }
}

/// Expand a workflow directory into the files it contains.
///
/// `pattern` is a comma-separated list of shell-style globs relative to `dir`.
/// `**` crosses directory boundaries, so `**/*.yaml` walks the tree and
/// `*.yaml` does not — the distinction people already expect from every other
/// tool that takes a glob.
///
/// Results are SORTED. A directory listing is in whatever order the filesystem
/// feels like, and load order decides which of two same-named workflows is
/// reported as the duplicate — a diagnostic that changed between machines would
/// be worse than useless.
fn expand_dir(dir: &str, pattern: &str) -> Result<Vec<String>, String> {
    let root = std::path::Path::new(dir);
    if !root.is_dir() {
        return Err(format!("not a directory ({})", root.display()));
    }
    let pats: Vec<&str> = pattern
        .split(',')
        .map(str::trim)
        .filter(|p| !p.is_empty())
        .collect();
    let recursive = pats.iter().any(|p| p.contains("**"));
    let mut out = Vec::new();
    let mut stack = vec![root.to_path_buf()];
    while let Some(d) = stack.pop() {
        let rd = std::fs::read_dir(&d).map_err(|e| e.to_string())?;
        for ent in rd.flatten() {
            let path = ent.path();
            if path.is_dir() {
                if recursive {
                    stack.push(path);
                }
                continue;
            }
            let rel = path.strip_prefix(root).unwrap_or(&path);
            let rels = rel.to_string_lossy();
            if pats.iter().any(|p| glob_match(p, &rels)) {
                out.push(path.to_string_lossy().into_owned());
            }
        }
    }
    out.sort();
    Ok(out)
}

/// Shell-style glob matching: `*` within a segment, `**` across segments, `?`
/// for one character. Small on purpose — a workflow directory does not need
/// brace expansion or character classes, and a dependency for this would be a
/// poor trade in a tree that counts them.
fn glob_match(pat: &str, text: &str) -> bool {
    // `**/x` should also match a bare `x` at the root: people write it meaning
    // "at any depth", which includes none.
    if let Some(rest) = pat.strip_prefix("**/")
        && glob_match(rest, text)
    {
        return true;
    }
    let (p, t): (Vec<char>, Vec<char>) = (pat.chars().collect(), text.chars().collect());
    fn go(p: &[char], t: &[char]) -> bool {
        match p.first() {
            None => t.is_empty(),
            Some('*') => {
                let doubled = p.get(1) == Some(&'*');
                let rest = if doubled { &p[2..] } else { &p[1..] };
                // A single `*` stops at a separator; `**` does not.
                let mut i = 0;
                loop {
                    if go(rest, &t[i..]) {
                        return true;
                    }
                    if i >= t.len() {
                        return false;
                    }
                    if !doubled && t[i] == '/' {
                        return false;
                    }
                    i += 1;
                }
            }
            Some('?') if !t.is_empty() => go(&p[1..], &t[1..]),
            Some(c) if t.first() == Some(c) => go(&p[1..], &t[1..]),
            _ => false,
        }
    }
    go(&p, &t)
}

#[cfg(test)]
mod glob_tests {
    use super::glob_match;

    #[test]
    fn a_single_star_stays_inside_one_segment_and_double_crosses() {
        // The distinction people expect from every other tool that takes a glob.
        assert!(glob_match("*.yaml", "nightly.yaml"));
        assert!(
            !glob_match("*.yaml", "team/nightly.yaml"),
            "* must not cross /"
        );
        assert!(glob_match("**/*.yaml", "team/nightly.yaml"));
        assert!(glob_match("**/*.yaml", "a/b/c/deep.yaml"));
        // `**/x` means "at any depth", and no depth is a depth — otherwise a
        // recursive pattern silently skips the files at the root.
        assert!(glob_match("**/*.yaml", "nightly.yaml"));

        assert!(glob_match("flows/*.json", "flows/a.json"));
        assert!(!glob_match("flows/*.json", "flows/a.yaml"));
        assert!(!glob_match("*.yaml", "yaml"), "the dot is literal");
        assert!(glob_match("?.yaml", "a.yaml"));
        assert!(!glob_match("?.yaml", "ab.yaml"));
        // A pattern with no wildcard is an exact name.
        assert!(glob_match("nightly.yaml", "nightly.yaml"));
        assert!(!glob_match("nightly.yaml", "nightly.yml"));
    }
}