1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
//! Flow/Stage workflow system.
//!
//! A **Flow** is a declarative YAML-defined workflow containing:
//! - Named **Stages** (sequential steps with persona/provider/instructions)
//! - **Rules** for conditional routing between stages
//! - **Output contracts** for schema validation
//!
//! Example YAML:
//! ```yaml
//! name: default
//! description: "Standard development workflow"
//! max_stages: 30
//! initial_movement: plan
//!
//! stages:
//! - id: plan
//! persona: planner
//! instruction: "Analyze the task and create a plan"
//! tools: [read, grep, glob]
//! permission: readonly
//! rules:
//! - condition: success
//! next: implement
//! - condition: needs_clarification
//! next: clarify
//!
//! - id: implement
//! persona: coder
//! instruction: "Implement the plan"
//! tools: [read, write, edit, bash]
//! permission: edit
//! rules:
//! - condition: success
//! next: review
//! - condition: test_failure
//! next: fix
//! ```
use anyhow::{Context, Result};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::Path;
use tracing::{debug, info, warn};
/// A Flow is a complete workflow definition loaded from YAML
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Flow {
/// Flow name (unique identifier)
pub name: String,
/// Human-readable description
#[serde(default)]
pub description: String,
/// Maximum *total* stage executions across the whole run before abort
/// (complementary to `max_stage_visits`, which bounds each stage
/// individually)
#[serde(default = "default_max_movements")]
pub max_stages: u32,
/// Maximum visits per *individual* stage before abort (bounds review→fix loops)
#[serde(default = "default_max_stage_visits")]
pub max_stage_visits: u32,
/// Fallback providers to try, in order, when a provider call fails with a
/// rate-limit error (takt's `rate_limit_fallback.switch_chain`). Empty by
/// default: rate limits surface as ordinary provider errors.
#[serde(default)]
pub on_rate_limit: Vec<FallbackTarget>,
/// ID of the first stage to execute
pub initial_movement: String,
/// List of stages in this flow
pub stages: Vec<Stage>,
/// Global variables for the flow
#[serde(default)]
pub variables: HashMap<String, serde_json::Value>,
/// Flow-level metadata
#[serde(default)]
pub metadata: HashMap<String, String>,
/// Default interactive mode for this flow
#[serde(default)]
pub interactive_mode: Option<super::interactive::InteractiveMode>,
}
fn default_max_movements() -> u32 {
30
}
fn default_max_stage_visits() -> u32 {
3
}
/// One entry in a flow's `on_rate_limit` fallback chain.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FallbackTarget {
/// Provider to switch to (claude | codex).
pub provider: String,
/// Optional model override for the fallback provider.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub model: Option<String>,
}
/// A Stage is a single step in a Flow workflow
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Stage {
/// Unique stage identifier within the flow
pub id: String,
/// Persona to use (references a persona YAML file or inline prompt)
#[serde(default)]
pub persona: Option<String>,
/// Policy to apply (references a policy YAML file or inline constraints)
#[serde(default)]
pub policy: Option<String>,
/// Knowledge facet (references a knowledge YAML file or inline context)
#[serde(default)]
pub knowledge: Option<String>,
/// Provider to use (claude, codex, etc.)
#[serde(default)]
pub provider: Option<String>,
/// Model to use (overrides provider default)
#[serde(default)]
pub model: Option<String>,
/// Instruction for the agent
#[serde(default)]
pub instruction: String,
/// Tools available to this stage
#[serde(default)]
pub tools: Vec<String>,
/// Permission level for this stage
#[serde(default)]
pub permission: MovementPermission,
/// Routing rules evaluated after stage completes
#[serde(default)]
pub rules: Vec<MovementRule>,
/// Whether this stage executes sub-stages in parallel
#[serde(default)]
pub parallel: bool,
/// Sub-stages for parallel execution
#[serde(default)]
pub sub_movements: Vec<String>,
/// Output contract for validation
#[serde(default)]
pub output_contract: Option<OutputContract>,
/// Maximum execution time in seconds
#[serde(default)]
pub timeout: Option<u32>,
/// Number of retries on failure
#[serde(default)]
pub max_retries: u32,
/// Claude Code agent name for --agent flag routing
#[serde(default)]
pub agent: Option<String>,
/// Working directory override for this stage
#[serde(default)]
pub working_dir: Option<String>,
/// Retry delay in milliseconds (default: 1000)
#[serde(default = "default_retry_delay")]
pub retry_delay_ms: u64,
/// Whether to pass previous stage's response as context (default: true)
/// Set to false for fix stages where fresh context is preferred
#[serde(default = "default_true")]
pub pass_previous_response: bool,
/// Invoke another flow as this stage's body. When set, the stage's
/// `instruction` / `persona` / `policy` are ignored and the named child
/// flow runs end-to-end; the child's final output becomes this stage's
/// output, which downstream rules then evaluate as usual. Adopted from
/// takt's `kind: workflow_call`.
///
/// Variables in `call.args` are injected into the child's initial state
/// after template-expanding values against the parent's variables. The
/// child writes reports into the same `.ccswarm/runs/<id>/reports/`
/// directory as the parent; declared report names must be globally
/// unique within a run (v0.7.0 first-cut; namespacing is deferred).
#[serde(default)]
pub call: Option<WorkflowCallSpec>,
/// Provider/model escalation rules applied from the Nth visit of this
/// stage onward (takt's `promotion`). The last matching entry wins.
/// Ignored on parallel sub-stages, where visit counts track the parent.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub promotion: Vec<PromotionRule>,
/// Machine-executed quality gates run after the agent completes this
/// stage (takt's command quality gates). On failure, bounded command
/// output is appended to the instruction and the stage re-runs, up to
/// `max_retries` additional attempts. Gates run in the stage's working
/// directory. Flow YAML already executes arbitrary edit-permission
/// prompts, so gates add no new trust surface.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub gates: Vec<CommandGate>,
/// Orchestrator-worker decomposition (takt's `team_leader`): a leader
/// call splits this stage's task into parts at runtime, the parts execute
/// concurrently as synthesized worker stages, and their outputs aggregate
/// into the parallel shape so `all()`/`any()` rules work unchanged.
/// Mutually exclusive with `parallel` and `call`.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub team_leader: Option<super::team_leader::TeamLeaderSpec>,
/// Sangha consensus round: multiple independent members evaluate the
/// stage and quorum decides whether the stage advances. Mutually
/// exclusive with `parallel`, `call`, and `team_leader`.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub sangha: Option<super::sangha::SanghaSpec>,
}
/// One machine-executed gate command on a stage.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommandGate {
/// Display name, used in events and failure feedback.
pub name: String,
/// Shell command executed via `sh -c` in the stage's working directory.
pub command: String,
/// Per-gate timeout in seconds (default 300).
#[serde(default = "default_gate_timeout_secs")]
pub timeout_secs: u64,
}
fn default_gate_timeout_secs() -> u64 {
300
}
/// One provider/model escalation rule on a stage.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PromotionRule {
/// Applies from this visit number onward (1 = first execution).
pub at: u32,
/// Provider to switch to (claude | codex). `None` keeps the current one.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub provider: Option<String>,
/// Model to switch to. `None` keeps the current one.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub model: Option<String>,
}
/// Specification for invoking another flow as a single stage's body.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkflowCallSpec {
/// Name of the child flow to invoke. Must be registered with the
/// `FlowEngine` (built-in or loaded). Unknown names fail the stage.
pub flow: String,
/// Variables to seed into the child flow's initial state. Values are
/// expanded against the parent's variables (e.g. `"task": "{task}"` pipes
/// the parent's `task` through).
#[serde(default)]
pub args: HashMap<String, String>,
}
fn default_true() -> bool {
true
}
fn default_retry_delay() -> u64 {
1000
}
/// Permission level for a stage
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
#[serde(rename_all = "lowercase")]
pub enum MovementPermission {
/// Read-only access (can read files, search, but not modify)
Readonly,
/// Edit access (can modify existing files)
#[default]
Edit,
/// Full access (can create, delete, execute commands)
Full,
}
/// A routing rule that determines the next stage
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MovementRule {
/// Condition to evaluate (string match, AI evaluation, or built-in)
pub condition: RuleCondition,
/// Next stage ID if condition matches
pub next: String,
/// Optional priority for rule ordering (higher = checked first)
#[serde(default)]
pub priority: u8,
/// Skip this rule entirely in non-interactive (pipeline / queue drain / CI)
/// runs. Use for review-fix loops that only make sense when a human is in
/// the loop. Adopted from takt's `interactive_only` rule field.
#[serde(default)]
pub interactive_only: bool,
/// Indicates the rule's `next` stage expects human input. Pipeline runs
/// treat this rule as inert (same as `interactive_only`); interactive runs
/// flag the upcoming stage as awaiting user reply so the UI can prompt.
/// Adopted from takt's `requires_user_input` rule field.
#[serde(default)]
pub requires_user_input: bool,
}
/// Condition types for stage routing
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum RuleCondition {
/// Simple string condition (matched against output markers)
Simple(String),
/// AI-evaluated condition
AiCondition {
/// The AI prompt to evaluate
ai: String,
},
/// Compound condition with all/any aggregation
Compound(CompoundCondition),
}
/// Compound condition with logical operators
#[derive(Debug, Clone, Deserialize)]
pub enum CompoundCondition {
/// All conditions must match
#[serde(rename = "all")]
All(Vec<String>),
/// Any condition must match
#[serde(rename = "any")]
Any(Vec<String>),
}
// Hand-written Serialize: the derived (externally tagged) form serializes to
// a YAML `!all`/`!any` tag, which the untagged `RuleCondition` wrapper cannot
// re-parse — breaking the serialize→parse round trip `flow check` does on
// builtin flows. Emitting a single-entry map (`all: [..]`) matches the YAML
// authors write and what the derived Deserialize accepts.
impl Serialize for CompoundCondition {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
use serde::ser::SerializeMap;
let mut map = serializer.serialize_map(Some(1))?;
match self {
Self::All(conditions) => map.serialize_entry("all", conditions)?,
Self::Any(conditions) => map.serialize_entry("any", conditions)?,
}
map.end()
}
}
/// Output contract for validating stage results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OutputContract {
/// Expected output format (markdown, json, text, yaml, code)
#[serde(default = "default_format")]
pub format: String,
/// Required sections in the output
#[serde(default)]
pub required_sections: Vec<String>,
/// JSON schema for structured output validation
#[serde(default)]
pub schema: Option<serde_json::Value>,
/// Output file name (for file-based contracts)
#[serde(default)]
pub output_file: Option<String>,
/// Required JSON keys (for quick validation without full schema)
#[serde(default)]
pub required_keys: Vec<String>,
/// Minimum output length in characters
#[serde(default)]
pub min_length: Option<usize>,
/// Maximum output length in characters
#[serde(default)]
pub max_length: Option<usize>,
/// Regular expression patterns the output must match
#[serde(default)]
pub must_match: Vec<String>,
/// Regular expression patterns the output must NOT match
#[serde(default)]
pub must_not_match: Vec<String>,
/// Allow-list of file globs the stage is permitted to create or modify.
///
/// Empty means "no restriction". If present, files written by the stage that
/// don't match any pattern are surfaced as `UnexpectedFile` observations — not
/// hard violations, so the run still completes, but the user sees what the AI
/// added beyond the spec. This closes JTBD issue #44 (spec-drift detection).
#[serde(default)]
pub allowed_files: Vec<String>,
/// Named reports the stage produces under `.ccswarm/runs/<run-id>/reports/`.
///
/// Each entry maps to a deterministically named file (e.g. `plan.md`) that
/// downstream stages can reference via the `{report:<name>}` template
/// variable. Adopted from takt's `output_contracts.report` to replace the
/// brittle `{plan_output}` state-variable wiring with a contract that's
/// readable from disk after the run.
///
/// v0.7.0 semantics: if exactly one report is declared, the stage's full
/// response is written verbatim to it. Multi-report support (with AI-emitted
/// `<<<REPORT:name>>>` delimiters) is intentionally deferred.
#[serde(default)]
pub reports: Vec<ReportContract>,
}
/// A named report file a stage produces under the run's `reports/` directory.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReportContract {
/// File name written under `.ccswarm/runs/<run-id>/reports/`. Must not contain
/// path separators — validated at flow-load time.
pub name: String,
/// Format hint surfaced in the prompt to guide the model (e.g. `markdown`,
/// `json`, `text`).
#[serde(default = "default_format")]
pub format: String,
/// Optional one-line description rendered into the prompt's output-contract
/// block to clarify intent (e.g. "investigation summary").
#[serde(default)]
pub description: Option<String>,
}
/// Result of output contract validation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContractValidationResult {
/// Whether the contract was satisfied
pub valid: bool,
/// List of violations found
pub violations: Vec<ContractViolation>,
}
/// A single contract violation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContractViolation {
/// Violation type
pub kind: ViolationKind,
/// Human-readable message
pub message: String,
}
/// Types of contract violations
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum ViolationKind {
/// Missing required section
MissingSection,
/// Missing required JSON key
MissingKey,
/// Invalid format
InvalidFormat,
/// Output too short
TooShort,
/// Output too long
TooLong,
/// Schema validation failure
SchemaViolation,
/// Pattern match failure
PatternViolation,
/// Forbidden pattern found
ForbiddenPattern,
/// File created/modified by the stage that is not covered by allowed_files.
/// Observation-level — does not fail the run by itself.
UnexpectedFile,
}
fn default_format() -> String {
"text".to_string()
}
/// Runtime state of a flow execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FlowState {
/// Flow being executed
pub flow_name: String,
/// Current stage ID
pub current_movement: String,
/// Number of stages executed so far
pub movement_count: u32,
/// History of stage transitions
pub history: Vec<MovementTransition>,
/// Accumulated variables/outputs
pub variables: HashMap<String, serde_json::Value>,
/// Current status
pub status: FlowStatus,
/// Started at
pub started_at: DateTime<Utc>,
/// Completed at
pub completed_at: Option<DateTime<Utc>>,
}
/// A recorded transition between stages
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MovementTransition {
/// Source stage ID
pub from: String,
/// Destination stage ID
pub to: String,
/// Condition that triggered the transition
pub condition: String,
/// Timestamp
pub timestamp: DateTime<Utc>,
/// Output from the source stage
pub output: Option<serde_json::Value>,
}
/// Flow execution status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum FlowStatus {
/// Not yet started
Pending,
/// Currently executing
Running,
/// Completed successfully (reached terminal stage or explicit completion)
Completed,
/// Aborted (max stages exceeded or error)
Aborted,
/// Failed with error
Failed,
}
impl Flow {
/// Load a flow from a YAML file
pub async fn load_from_file(path: &Path) -> Result<Self> {
let contents = tokio::fs::read_to_string(path)
.await
.with_context(|| format!("Failed to read flow file: {}", path.display()))?;
Self::from_yaml(&contents)
}
/// Parse a flow from YAML string
pub fn from_yaml(yaml: &str) -> Result<Self> {
let flow: Self = serde_yml::from_str(yaml).context("Failed to parse flow YAML")?;
flow.validate()?;
Ok(flow)
}
/// Validate flow structure
pub fn validate(&self) -> Result<()> {
// Check initial stage exists
if !self.stages.iter().any(|m| m.id == self.initial_movement) {
return Err(anyhow::anyhow!(
"Initial stage '{}' not found in flow '{}'",
self.initial_movement,
self.name
));
}
for target in &self.on_rate_limit {
if crate::providers::ProviderKind::parse(&target.provider).is_none() {
return Err(anyhow::anyhow!(
"Flow '{}' has unknown on_rate_limit provider '{}' (expected: claude | codex | copilot)",
self.name,
target.provider
));
}
}
// Check all rule targets reference valid stages
for stage in &self.stages {
if let Some(provider) = &stage.provider
&& crate::providers::ProviderKind::parse(provider).is_none()
{
return Err(anyhow::anyhow!(
"Stage '{}' has unknown provider '{}' (expected: claude | codex | copilot)",
stage.id,
provider
));
}
for rule in &stage.promotion {
if let Some(provider) = &rule.provider
&& crate::providers::ProviderKind::parse(provider).is_none()
{
return Err(anyhow::anyhow!(
"Stage '{}' has unknown promotion provider '{}' (expected: claude | codex | copilot)",
stage.id,
provider
));
}
}
for rule in &stage.rules {
if !self.stages.iter().any(|m| m.id == rule.next) {
return Err(anyhow::anyhow!(
"Rule in stage '{}' references unknown stage '{}'",
stage.id,
rule.next
));
}
}
// Check parallel sub-stages exist
if stage.parallel {
for sub in &stage.sub_movements {
if !self.stages.iter().any(|m| m.id == *sub) {
return Err(anyhow::anyhow!(
"Parallel stage '{}' references unknown sub-stage '{}'",
stage.id,
sub
));
}
}
}
// team_leader is its own execution mode — combining it with
// declared-parallel or workflow_call would be ambiguous.
if let Some(spec) = &stage.team_leader {
if stage.parallel {
return Err(anyhow::anyhow!(
"Stage '{}' combines team_leader with parallel — pick one",
stage.id
));
}
if stage.call.is_some() {
return Err(anyhow::anyhow!(
"Stage '{}' combines team_leader with call — pick one",
stage.id
));
}
if spec.max_parts == 0 {
return Err(anyhow::anyhow!(
"Stage '{}': team_leader.max_parts must be >= 1",
stage.id
));
}
}
// sangha is its own execution mode — combining consensus with
// other body execution modes would make routing ambiguous.
if let Some(spec) = &stage.sangha {
if stage.parallel {
return Err(anyhow::anyhow!(
"Stage '{}' combines sangha with parallel — pick one",
stage.id
));
}
if stage.call.is_some() {
return Err(anyhow::anyhow!(
"Stage '{}' combines sangha with call — pick one",
stage.id
));
}
if stage.team_leader.is_some() {
return Err(anyhow::anyhow!(
"Stage '{}' combines sangha with team_leader — pick one",
stage.id
));
}
if spec.quorum == 0 {
return Err(anyhow::anyhow!(
"Stage '{}': sangha.quorum must be >= 1",
stage.id
));
}
}
}
// Check for duplicate stage IDs
let mut seen = std::collections::HashSet::new();
for stage in &self.stages {
if !seen.insert(&stage.id) {
return Err(anyhow::anyhow!(
"Duplicate stage ID '{}' in flow '{}'",
stage.id,
self.name
));
}
}
Ok(())
}
/// Get a stage by ID
pub fn get_movement(&self, id: &str) -> Option<&Stage> {
self.stages.iter().find(|m| m.id == id)
}
/// Check if a stage is terminal (has no rules / no transitions)
pub fn is_terminal(&self, movement_id: &str) -> bool {
self.get_movement(movement_id)
.map(|m| m.rules.is_empty())
.unwrap_or(true)
}
/// Create initial execution state
pub fn create_state(&self) -> FlowState {
FlowState {
flow_name: self.name.clone(),
current_movement: self.initial_movement.clone(),
movement_count: 0,
history: Vec::new(),
variables: self.variables.clone(),
status: FlowStatus::Pending,
started_at: Utc::now(),
completed_at: None,
}
}
}
/// Flow engine that executes flow workflows
pub struct FlowEngine {
/// Loaded flows
flows: HashMap<String, Flow>,
/// Stage judge for tag/AI-based condition evaluation
judge: super::judge::MovementJudge,
/// Facet registry for prompt composition
facet_registry: super::facets::FacetRegistry,
/// Bridge for A2A/local provider execution and result management.
bridge: Option<std::sync::Arc<crate::session::bridge::A2ABridge>>,
/// Working directory for agent execution
working_dir: std::path::PathBuf,
/// Optional event recorder for NDJSON observability
event_recorder: Option<crate::events::EventRecorder>,
/// Last known execution state (for partial result recovery on timeout)
last_state: std::sync::Arc<tokio::sync::RwLock<Option<FlowState>>>,
/// Progress callback for real-time stage completion notifications
progress_tx: Option<tokio::sync::mpsc::UnboundedSender<MovementProgress>>,
/// Per-stage cost cap forwarded to providers that understand it (Claude's
/// `--max-budget-usd`). `None` leaves it to the provider's own default.
budget_usd: Option<f64>,
/// Cumulative input+output token cap across the whole run. When exceeded, the
/// flow aborts before the next stage starts. Complements `budget_usd`, which
/// only caps a single stage and only works for Claude.
run_token_cap: Option<u64>,
/// Whether this engine instance runs in interactive mode. Pipeline / queue
/// drain / CI runs default to `false` and skip rules tagged
/// `interactive_only` or `requires_user_input`. Interactive entry points
/// (e.g. `ccswarm` with no subcommand) call `set_interactive(true)` so
/// human-in-the-loop rules are honored. Adopted from takt's
/// interactive_only / requires_user_input rule fields.
interactive: bool,
/// Default provider for stages that don't pin one in flow YAML
/// (`--provider` flag). Sits between stage YAML and the CCSWARM_PROVIDER
/// env var in the resolution order.
default_provider: Option<crate::providers::ProviderKind>,
/// CLI model override applied to every live stage.
model_override: Option<String>,
/// Optional isolated worktree name forwarded through A2ABridge.
worktree_name: Option<String>,
}
/// Progress notification sent after each stage completes
#[derive(Debug, Clone)]
pub struct MovementProgress {
/// Stage ID
pub movement_id: String,
/// Duration in milliseconds
pub duration_ms: u64,
/// Whether it succeeded
pub success: bool,
/// Stage count so far
pub movements_completed: usize,
}
impl FlowEngine {
pub fn new() -> Self {
let mut facet_registry = super::facets::FacetRegistry::new();
// Register built-in facets
for persona in super::facets::builtin_personas() {
facet_registry.register_persona(persona);
}
for policy in super::facets::builtin_policies() {
facet_registry.register_policy(policy);
}
// Register built-in flows so they're available by default
let mut flows = HashMap::new();
for flow in builtin_flows() {
flows.insert(flow.name.clone(), flow);
}
Self {
flows,
judge: super::judge::MovementJudge::default(),
facet_registry,
bridge: None,
working_dir: std::path::PathBuf::from("."),
event_recorder: None,
last_state: std::sync::Arc::new(tokio::sync::RwLock::new(None)),
progress_tx: None,
budget_usd: None,
run_token_cap: None,
interactive: false,
default_provider: None,
model_override: None,
worktree_name: None,
}
}
/// Mark this engine as running in interactive mode. Pipeline / queue drain
/// runs leave the default (`false`), which filters out rules tagged
/// `interactive_only` or `requires_user_input`.
pub fn set_interactive(&mut self, interactive: bool) {
self.interactive = interactive;
}
/// Set a per-stage budget cap in USD. Forwarded to Claude via `--max-budget-usd`;
/// other providers currently ignore it (they don't expose an equivalent flag).
pub fn set_budget(&mut self, budget_usd: f64) {
self.budget_usd = Some(budget_usd);
}
/// Cap cumulative input+output tokens across the run. Checked between stages;
/// when exceeded, the flow aborts with `FlowStatus::Aborted` and emits a
/// `budget_exceeded` event. Provider-agnostic since token estimates are
/// produced by the bridge regardless of backend.
pub fn set_run_token_cap(&mut self, cap: u64) {
self.run_token_cap = Some(cap);
}
/// Create with custom judge config
pub fn with_judge_config(config: super::judge::JudgeConfig) -> Self {
let mut engine = Self::new();
engine.judge = super::judge::MovementJudge::new(config);
engine
}
/// Get a mutable reference to the facet registry for loading custom facets
pub fn facet_registry_mut(&mut self) -> &mut super::facets::FacetRegistry {
&mut self.facet_registry
}
/// Set the A2ABridge for live stage execution.
pub fn set_bridge(&mut self, bridge: std::sync::Arc<crate::session::bridge::A2ABridge>) {
self.bridge = Some(bridge);
}
/// Set the default provider for stages that don't pin one in flow YAML
/// (`--provider` flag). Overrides the CCSWARM_PROVIDER env var.
pub(crate) fn set_default_provider(&mut self, provider: crate::providers::ProviderKind) {
self.default_provider = Some(provider);
}
/// Set a CLI model override for all stages in this engine run.
pub(crate) fn set_model_override(&mut self, model: impl Into<String>) {
self.model_override = Some(model.into());
}
/// Set the provider worktree isolation name for live stage execution.
pub(crate) fn set_worktree_name(&mut self, name: impl Into<String>) {
self.worktree_name = Some(name.into());
}
/// Resolve the provider and model a stage execution should use.
///
/// Base precedence: stage YAML `provider:` > `--provider` flag >
/// `CCSWARM_PROVIDER` env > Claude default (stage YAML wins because it
/// expresses deliberate per-stage intent). On top of that, `promotion`
/// rules escalate provider/model from the Nth visit of the stage onward
/// (takt-style, last matching entry wins). Promotion is skipped when no
/// visit count is available — notably for parallel sub-stages, whose
/// count would otherwise reflect the parent stage.
fn resolve_effective_provider(
&self,
stage: &Stage,
state: &FlowState,
) -> (Option<crate::providers::ProviderKind>, Option<String>) {
let mut provider = stage
.provider
.as_deref()
.and_then(crate::providers::ProviderKind::parse)
.or(self.default_provider)
.or_else(|| {
std::env::var("CCSWARM_PROVIDER")
.ok()
.as_deref()
.and_then(crate::providers::ProviderKind::parse)
});
let mut model = stage.model.clone();
let visit_count = state
.variables
.get("__visit_count")
.and_then(|v| v.as_u64())
.unwrap_or(0) as u32;
if visit_count >= 1 {
// Reverse scan = last matching entry wins (takt semantics).
if let Some(rule) = stage
.promotion
.iter()
.rev()
.find(|rule| visit_count >= rule.at.max(1))
{
info!(
"Stage '{}' visit {} matched promotion rule (at: {}): provider={:?} model={:?}",
stage.id, visit_count, rule.at, rule.provider, rule.model
);
if let Some(p) = rule
.provider
.as_deref()
.and_then(crate::providers::ProviderKind::parse)
{
provider = Some(p);
}
if rule.model.is_some() {
model = rule.model.clone();
}
}
}
if self.model_override.is_some() {
model = self.model_override.clone();
}
(provider, model)
}
/// Set the working directory for agent execution
pub fn set_working_dir(&mut self, dir: std::path::PathBuf) {
self.working_dir = dir;
}
/// Set the event recorder for NDJSON observability
pub fn set_event_recorder(&mut self, recorder: crate::events::EventRecorder) {
self.event_recorder = Some(recorder);
}
/// Get the last known execution state (for partial result recovery on timeout)
pub async fn get_last_state(&self) -> Option<FlowState> {
self.last_state.read().await.clone()
}
/// Set a progress channel for real-time stage completion notifications
pub fn set_progress_channel(
&mut self,
tx: tokio::sync::mpsc::UnboundedSender<MovementProgress>,
) {
self.progress_tx = Some(tx);
}
/// Load a flow from a YAML file
pub async fn load_flow(&mut self, path: &Path) -> Result<String> {
let flow = Flow::load_from_file(path).await?;
let name = flow.name.clone();
info!("Loaded flow '{}' with {} stages", name, flow.stages.len());
self.flows.insert(name.clone(), flow);
Ok(name)
}
/// Load all flows from a directory
pub async fn load_pieces_from_dir(&mut self, dir: &Path) -> Result<Vec<String>> {
let mut loaded = Vec::new();
if !dir.exists() {
return Ok(loaded);
}
let mut entries = tokio::fs::read_dir(dir).await?;
while let Some(entry) = entries.next_entry().await? {
let path = entry.path();
if path.extension().and_then(|e| e.to_str()) == Some("yaml")
|| path.extension().and_then(|e| e.to_str()) == Some("yml")
{
match self.load_flow(&path).await {
Ok(name) => loaded.push(name),
Err(e) => warn!("Failed to load flow from {}: {}", path.display(), e),
}
}
}
Ok(loaded)
}
/// Load all builtin flows into the engine
pub fn load_builtin_flows(&mut self) {
for flow in builtin_flows() {
self.flows.insert(flow.name.clone(), flow);
}
}
/// Get a loaded flow
pub fn get_flow(&self, name: &str) -> Option<&Flow> {
self.flows.get(name)
}
/// List all loaded flows
pub fn list_flows(&self) -> Vec<&Flow> {
self.flows.values().collect()
}
/// Register a flow directly (for programmatic / test usage)
pub fn register_flow(&mut self, flow: Flow) {
self.flows.insert(flow.name.clone(), flow);
}
/// Record an event if recorder is configured (best-effort, logs on failure)
async fn record_event(&self, event: crate::events::Event) {
let Some(ref recorder) = self.event_recorder else {
return;
};
if let Err(e) = recorder.record(event).await {
warn!("Failed to record event: {}", e);
}
}
/// Execute a flow workflow with a task description injected as context
pub async fn execute_piece_with_task(&self, name: &str, task_text: &str) -> Result<FlowState> {
let flow = self
.flows
.get(name)
.ok_or_else(|| anyhow::anyhow!("Flow '{}' not found", name))?;
let mut state = flow.create_state();
// Inject task text as a variable so stages can reference it
state
.variables
.insert("task".to_string(), serde_json::json!(task_text));
state.status = FlowStatus::Running;
self.execute_piece_state(name, flow, state).await
}
/// Execute a flow workflow
pub async fn execute_piece(&self, name: &str) -> Result<FlowState> {
let flow = self
.flows
.get(name)
.ok_or_else(|| anyhow::anyhow!("Flow '{}' not found", name))?;
let mut state = flow.create_state();
state.status = FlowStatus::Running;
self.execute_piece_state(name, flow, state).await
}
/// Internal flow execution with pre-configured state
#[tracing::instrument(name = "flow.run", skip_all, fields(flow = %name))]
async fn execute_piece_state(
&self,
name: &str,
flow: &Flow,
mut state: FlowState,
) -> Result<FlowState> {
let run_id = self
.event_recorder
.as_ref()
.map(|r| r.run_id().to_string())
.unwrap_or_default();
self.record_event(crate::events::Event::new(
&run_id,
crate::events::EventLevel::Info,
crate::events::EventType::TaskStart,
format!("Starting flow '{}'", name),
))
.await;
info!(
"Starting flow '{}' at stage '{}'",
name, state.current_movement
);
// Stash run_id in state so expand_template can resolve `{report:<name>}`
// by reading from `.ccswarm/runs/<run_id>/reports/`. Uses a double-underscore
// prefix to flag as internal — user instructions should never name a variable
// `__run_id`.
if !run_id.is_empty() {
state.variables.insert(
"__run_id".to_string(),
serde_json::Value::String(run_id.clone()),
);
}
let mut cumulative_tokens: u64 = 0;
let mut loop_tracker = super::cycle::LoopTracker::new(flow.max_stage_visits);
loop {
// Check max stages
if state.movement_count >= flow.max_stages {
warn!("Flow '{}' exceeded max stages ({})", name, flow.max_stages);
state.status = FlowStatus::Aborted;
state.completed_at = Some(Utc::now());
break;
}
// Get current stage
let stage = match flow.get_movement(&state.current_movement) {
Some(m) => m.clone(),
None => {
state.status = FlowStatus::Failed;
state.completed_at = Some(Utc::now());
return Err(anyhow::anyhow!(
"Stage '{}' not found in flow '{}'",
state.current_movement,
name
));
}
};
// Per-stage loop guard: abort before executing a stage whose visit
// count exceeds max_stage_visits, so a stuck review→fix loop stops
// before burning another provider call.
if loop_tracker.record_visit(&stage.id) {
let pattern = loop_tracker.detect_pattern();
warn!(
"Flow '{}' aborted: stage '{}' visited {} times (max {}){}",
name,
stage.id,
loop_tracker.visit_count(&stage.id),
flow.max_stage_visits,
pattern
.as_ref()
.map(|p| format!(", repeating pattern: {}", p.join(" -> ")))
.unwrap_or_default()
);
self.record_event(
crate::events::Event::new(
&run_id,
crate::events::EventLevel::Warn,
crate::events::EventType::TaskEnd,
format!(
"Loop detected: stage '{}' exceeded max_stage_visits ({})",
stage.id, flow.max_stage_visits
),
)
.with_movement(&stage.id)
.with_metadata(serde_json::json!({
"reason": "loop_detected",
"stage": stage.id,
"visits": loop_tracker.visit_count(&stage.id),
"max_stage_visits": flow.max_stage_visits,
"pattern": pattern,
})),
)
.await;
state.status = FlowStatus::Aborted;
state.completed_at = Some(Utc::now());
break;
}
// Expose the per-stage visit count so promotion rules can match
// (internal `__` prefix keeps it out of prompt template expansion,
// same convention as `__run_id`).
state.variables.insert(
"__visit_count".to_string(),
serde_json::json!(loop_tracker.visit_count(&stage.id)),
);
debug!(
"Executing stage '{}' (#{}) in flow '{}'",
stage.id, state.movement_count, name
);
// Issue #23 fix: derive the agent label used by this stage so that
// RunSummary::agents_used is populated. Preference order mirrors what the
// bridge actually uses to route the call: an explicit `.claude/agents/<name>`
// file (stage.agent) wins; otherwise we fall back to the persona facet.
let movement_agent = stage.agent.clone().or_else(|| stage.persona.clone());
// Record stage start
let mut ev_start = crate::events::Event::new(
&run_id,
crate::events::EventLevel::Info,
crate::events::EventType::MovementStart,
format!("Stage '{}' started", stage.id),
)
.with_movement(&stage.id);
if let Some(ref a) = movement_agent {
ev_start = ev_start.with_agent(a);
}
self.record_event(ev_start).await;
// Execute the stage with timing and optional per-stage timeout
let movement_start = std::time::Instant::now();
let output = if let Some(timeout_secs) = stage.timeout {
let timeout = std::time::Duration::from_secs(timeout_secs as u64);
match tokio::time::timeout(timeout, self.execute_movement(&stage, &state)).await {
Ok(result) => result?,
Err(_) => {
warn!("Stage '{}' timed out after {}s", stage.id, timeout_secs);
serde_json::json!({
"stage": stage.id,
"status": "timeout",
"error": format!("Stage timed out after {}s", timeout_secs),
})
}
}
} else {
self.execute_movement(&stage, &state).await?
};
let movement_duration_ms = movement_start.elapsed().as_millis() as u64;
state.movement_count += 1;
let movement_succeeded = stage_output_succeeded(&output);
// Save intermediate state for timeout recovery
*self.last_state.write().await = Some(state.clone());
// Notify progress listener
if let Some(ref tx) = self.progress_tx {
let _ = tx.send(MovementProgress {
movement_id: stage.id.clone(),
duration_ms: movement_duration_ms,
success: movement_succeeded,
movements_completed: state.movement_count as usize,
});
}
// Record stage end with duration metadata
// Issue #28 fix: forward token estimates from the bridge into the event so
// `ccswarm cost` has something to aggregate.
let tokens_in = output
.as_object()
.and_then(|o| o.get("tokens_in"))
.and_then(|v| v.as_u64());
let tokens_out = output
.as_object()
.and_then(|o| o.get("tokens_out"))
.and_then(|v| v.as_u64());
let attention = output
.as_object()
.and_then(|o| o.get("attention"))
.and_then(|v| v.as_str())
.unwrap_or("idle")
.to_string();
let mut ev_end = crate::events::Event::new(
&run_id,
crate::events::EventLevel::Info,
crate::events::EventType::MovementEnd,
format!("Stage '{}' completed", stage.id),
)
.with_movement(&stage.id)
.with_metadata(serde_json::json!({
"duration_ms": movement_duration_ms,
"tokens_in": tokens_in,
"tokens_out": tokens_out,
"attention": attention,
"output_preview": output
.as_object()
.and_then(|o| o.get("output"))
.and_then(|v| v.as_str())
.map(|s| truncate_for_context(s, 500))
.unwrap_or_default(),
"status": output
.as_object()
.and_then(|o| o.get("status"))
.and_then(|v| v.as_str())
.unwrap_or("unknown"),
}));
if let Some(ref a) = movement_agent {
ev_end = ev_end.with_agent(a);
}
self.record_event(ev_end).await;
// Enforce the run-level token budget. Done after the stage records its
// end event so `ccswarm cost` still sees the usage of the stage that
// pushed us over. We abort on the *next* iteration boundary rather
// than mid-stage: stages aren't cancellable, so stopping earlier
// would waste the in-flight work without preventing the spend.
cumulative_tokens = cumulative_tokens
.saturating_add(tokens_in.unwrap_or(0))
.saturating_add(tokens_out.unwrap_or(0));
if let Some(cap) = self.run_token_cap
&& cumulative_tokens > cap
{
warn!(
"Run token cap exceeded: {} > {} (aborting after stage '{}')",
cumulative_tokens, cap, stage.id
);
self.record_event(
crate::events::Event::new(
&run_id,
crate::events::EventLevel::Warn,
crate::events::EventType::TaskEnd,
format!("Run token cap exceeded: {} > {}", cumulative_tokens, cap),
)
.with_metadata(serde_json::json!({
"reason": "budget_exceeded",
"cumulative_tokens": cumulative_tokens,
"cap": cap,
"last_stage": stage.id,
})),
)
.await;
state.status = FlowStatus::Aborted;
state.completed_at = Some(Utc::now());
break;
}
// Store output in variables
state
.variables
.insert(format!("{}_output", stage.id), output.clone());
// Save stage report to .ccswarm/runs/{run-id}/reports/.
// Always writes a `<stage.id>.md` for backward-compat; additionally
// writes any declared `output_contract.reports[*].name` files so
// downstream stages can pull them via `{report:<name>}`.
if !run_id.is_empty() {
let report_dir = std::path::PathBuf::from(".ccswarm")
.join("runs")
.join(&run_id)
.join("reports");
let _ = tokio::fs::create_dir_all(&report_dir).await;
let report_content = output
.as_object()
.and_then(|o| o.get("output"))
.and_then(|v| v.as_str())
.unwrap_or("");
if !report_content.is_empty() {
let default_path = report_dir.join(format!("{}.md", stage.id));
let _ = tokio::fs::write(&default_path, report_content).await;
if let Some(contract) = stage.output_contract.as_ref() {
for report in &contract.reports {
if !is_safe_report_name(&report.name) {
warn!(
"Skipping declared report '{}' on stage '{}': name must not contain path separators or '..'",
report.name, stage.id
);
continue;
}
let report_path = report_dir.join(&report.name);
if let Err(e) = tokio::fs::write(&report_path, report_content).await {
warn!(
"Failed to write declared report '{}' for stage '{}': {}",
report.name, stage.id, e
);
}
}
}
}
}
// Check if terminal (no rules = done)
if stage.rules.is_empty() {
if movement_succeeded {
info!("Flow '{}' completed at terminal stage '{}'", name, stage.id);
state.status = FlowStatus::Completed;
} else {
warn!("Flow '{}' failed at terminal stage '{}'", name, stage.id);
state.status = FlowStatus::Failed;
}
state.completed_at = Some(Utc::now());
break;
}
// Evaluate rules to determine next stage
let next = self.evaluate_rules(&stage.rules, &output, &state).await?;
match next {
Some(next_id) => {
state.history.push(MovementTransition {
from: stage.id.clone(),
to: next_id.clone(),
condition: "matched".to_string(),
timestamp: Utc::now(),
output: Some(output),
});
state.current_movement = next_id;
}
None => {
// No rule matched - treat as completion
if movement_succeeded {
info!("No rule matched in stage '{}', completing flow", stage.id);
state.status = FlowStatus::Completed;
} else {
warn!("No rule matched failed stage '{}'", stage.id);
state.status = FlowStatus::Failed;
}
state.completed_at = Some(Utc::now());
break;
}
}
}
// Record flow completion and write summary
let completed = state.status == FlowStatus::Completed;
self.record_event(crate::events::Event::new(
&run_id,
if completed {
crate::events::EventLevel::Info
} else {
crate::events::EventLevel::Warn
},
crate::events::EventType::TaskEnd,
format!("Flow '{}' finished with status {:?}", name, state.status),
))
.await;
if let Some(ref recorder) = self.event_recorder {
// Issue #23 fix: agents_used was previously `state.history[].from` which is
// a *stage* ID, not an agent. Derive the real agent label from each
// stage definition (explicit `agent:` file route wins over persona).
let mut agents_used: std::collections::HashSet<String> =
std::collections::HashSet::new();
for m in flow.stages.iter() {
if let Some(a) = m.agent.clone().or_else(|| m.persona.clone()) {
agents_used.insert(a);
}
}
let summary = crate::events::RunSummary {
run_id: run_id.clone(),
started_at: state.started_at,
ended_at: state.completed_at,
total_events: recorder.event_count(),
tasks_completed: if completed { 1 } else { 0 },
tasks_failed: if completed { 0 } else { 1 },
agents_used: agents_used.into_iter().collect(),
};
if let Err(e) = recorder.write_summary(&summary).await {
warn!("Failed to write run summary: {}", e);
}
}
Ok(state)
}
/// Execute a single stage via A2A/local provider execution or prompt-only fallback.
#[tracing::instrument(
name = "flow.stage",
skip_all,
fields(stage = %stage.id, persona = stage.persona.as_deref(), provider = stage.provider.as_deref())
)]
async fn execute_movement(
&self,
stage: &Stage,
state: &FlowState,
) -> Result<serde_json::Value> {
info!(
"Stage '{}': persona={:?}, permission={:?}",
stage.id, stage.persona, stage.permission
);
// Sub-workflow dispatch (takt-style `kind: workflow_call`). Resolved
// before the local/parallel/CLI branches: a workflow_call stage's
// instruction is ignored, so the empty-instruction check below would
// otherwise short-circuit it to a local summary.
if let Some(call) = stage.call.as_ref() {
return self.execute_workflow_call(stage, call, state).await;
}
// If instruction is empty or starts with "_local", skip Claude and produce local summary
if stage.instruction.is_empty() || stage.instruction.starts_with("_local") {
let summary = self.build_local_summary(state);
return Ok(summary);
}
// Orchestrator-worker: the leader decomposes, workers run in parallel.
// Dispatched before the declared-parallel branch (validate() rejects
// combining the two).
if let Some(spec) = stage.team_leader.as_ref() {
return self.execute_team_leader(stage, spec, state).await;
}
// Sangha consensus: multiple independent members vote; no leader.
if let Some(spec) = stage.sangha.as_ref() {
return self.execute_sangha(stage, spec, state).await;
}
// Parallel execution: run sub-stages concurrently
if stage.parallel && !stage.sub_movements.is_empty() {
return self.execute_parallel_movements(stage, state).await;
}
// Build the prompt from instruction + persona + context
let prompt = self.build_movement_prompt(stage, state);
let output = if let Some(ref bridge) = self.bridge {
// Live execution via A2A or a local provider CLI.
let agent_id = stage.persona.as_deref().unwrap_or("default");
// Create a minimal identity for the stage
let identity = crate::identity::AgentIdentity {
agent_id: agent_id.to_string(),
specialization: crate::identity::AgentRole::Frontend {
technologies: Vec::new(),
responsibilities: Vec::new(),
boundaries: Vec::new(),
},
workspace_path: self.working_dir.clone(),
env_vars: std::collections::HashMap::new(),
session_id: uuid::Uuid::new_v4().to_string(),
parent_process_id: std::process::id().to_string(),
initialized_at: chrono::Utc::now(),
};
// Determine working directory (stage override or engine default)
let work_dir = stage
.working_dir
.as_ref()
.map(std::path::PathBuf::from)
.unwrap_or_else(|| self.working_dir.clone());
let (provider, model) = self.resolve_effective_provider(stage, state);
// Resolve effective tool list from the stage's permission level + explicit `tools:`.
// Without this, `permission: readonly` would still send no `--allowed-tools`,
// so the provider CLI would fall back to its own (permissive) default.
let effective_tools = super::permissions::PermissionEnforcer::from_movement(
stage.permission.clone(),
&stage.tools,
)
.available_tools();
// Flow-level rate-limit fallback chain, parsed into provider kinds.
let rate_limit_fallbacks: Vec<(crate::providers::ProviderKind, Option<String>)> = self
.flows
.get(&state.flow_name)
.map(|flow| {
flow.on_rate_limit
.iter()
.filter_map(|target| {
crate::providers::ProviderKind::parse(&target.provider)
.map(|kind| (kind, target.model.clone()))
})
.collect()
})
.unwrap_or_default();
let exec_options = crate::session::bridge::MovementExecOptions {
provider,
tools: effective_tools,
model,
system_prompt: stage.persona.as_ref().and_then(|p| {
self.facet_registry.get_persona(p).and_then(|f| {
if f.system_prompt.is_empty() {
None
} else {
Some(f.system_prompt.clone())
}
})
}),
max_budget: self.budget_usd,
worktree_name: self.worktree_name.clone(),
session_id: None,
continuation: crate::session::bridge::ContinuationPolicy::SingleTurn,
a2a_endpoint: None,
rate_limit_fallbacks,
};
// Command-gate loop: when the agent call succeeds but a declared
// gate fails, bounded gate output is appended to the prompt and
// the stage re-runs (up to max_retries additional attempts).
let mut gate_feedback: Option<String> = None;
let mut gate_attempts_left = stage.max_retries;
loop {
let effective_prompt = match &gate_feedback {
Some(feedback) => format!("{}\n\n{}", prompt, feedback),
None => prompt.clone(),
};
let attempt_output = match bridge
.execute_with_retry(
agent_id,
&effective_prompt,
&identity,
&work_dir,
stage.agent.as_deref(),
stage.max_retries,
stage.retry_delay_ms,
&exec_options,
)
.await
{
Ok(result) => {
// Rate-limit fallbacks that fired on the way to this result
// are recorded as ProviderError events so the switch is
// visible in the run's audit trail.
if !result.fallbacks_used.is_empty() {
let run_id = self
.event_recorder
.as_ref()
.map(|r| r.run_id().to_string())
.unwrap_or_default();
self.record_event(
crate::events::Event::new(
&run_id,
crate::events::EventLevel::Warn,
crate::events::EventType::ProviderError,
format!(
"Rate-limit fallback engaged for stage '{}': skipped [{}]",
stage.id,
result.fallbacks_used.join(", ")
),
)
.with_movement(&stage.id)
.with_metadata(serde_json::json!({
"reason": "rate_limit_fallback",
"rate_limited_providers": result.fallbacks_used,
})),
)
.await;
}
// Stream-json metadata (tool names, cost) rides on
// BridgeResult; surface it as a ProviderCall event so
// events.ndjson carries per-call telemetry. Token counts are
// deliberately NOT repeated here — they ride on the stage's
// MovementEnd event, and `ccswarm cost` sums token fields
// across all events, so duplicating them would double-count.
if !result.tool_names.is_empty() || result.total_cost_usd.is_some() {
let run_id = self
.event_recorder
.as_ref()
.map(|r| r.run_id().to_string())
.unwrap_or_default();
self.record_event(
crate::events::Event::new(
&run_id,
crate::events::EventLevel::Info,
crate::events::EventType::ProviderCall,
format!("Provider call completed for stage '{}'", stage.id),
)
.with_movement(&stage.id)
.with_metadata(serde_json::json!({
"tool_names": result.tool_names,
"cost_usd": result.total_cost_usd,
})),
)
.await;
}
serde_json::json!({
"stage": stage.id,
"output": result.raw,
"parsed": format!("{:?}", result.parsed),
"status": if result.success { "completed" } else { "failed" },
"duration_ms": result.duration_ms,
"tokens_in": result.tokens_in,
"tokens_out": result.tokens_out,
"attention": result.attention.to_string(),
})
}
Err(e) => {
warn!("Stage '{}' execution failed: {}", stage.id, e);
serde_json::json!({
"stage": stage.id,
"error": e.to_string(),
"status": "failed",
})
}
};
// Gates only run after a successful agent call; a failed call
// already routes through the stage's failure rules.
let call_succeeded = attempt_output
.get("status")
.and_then(|s| s.as_str())
.is_some_and(|s| s == "completed");
if stage.gates.is_empty() || !call_succeeded {
break attempt_output;
}
match run_command_gates(&stage.gates, &work_dir).await {
None => break attempt_output,
Some((gate_name, feedback)) => {
warn!(
"Stage '{}' gate '{}' failed ({} retry attempts left)",
stage.id, gate_name, gate_attempts_left
);
if gate_attempts_left == 0 {
let mut failed = attempt_output;
if let Some(obj) = failed.as_object_mut() {
obj.insert("status".into(), serde_json::json!("failed"));
obj.insert("gate_failed".into(), serde_json::json!(gate_name));
}
break failed;
}
gate_attempts_left -= 1;
gate_feedback = Some(feedback);
}
}
}
} else {
// No bridge configured - return prompt as output (for testing/offline use)
serde_json::json!({
"stage": stage.id,
"instruction": stage.instruction,
"prompt": prompt,
"status": "completed",
})
};
// Validate output contract if specified
if let Some(ref contract) = stage.output_contract {
self.validate_output_contract(contract, &output)?;
}
Ok(output)
}
/// Execute sub-stages in parallel across multiple agents.
async fn execute_parallel_movements(
&self,
parent: &Stage,
state: &FlowState,
) -> Result<serde_json::Value> {
info!(
"Parallel execution: {} sub-stages for '{}'",
parent.sub_movements.len(),
parent.id
);
// Find all sub-stage definitions from the flow
let flow = self
.flows
.values()
.find(|p| p.stages.iter().any(|m| m.id == parent.id))
.ok_or_else(|| anyhow::anyhow!("Parent flow not found for stage '{}'", parent.id))?;
let sub_movs: Vec<Stage> = parent
.sub_movements
.iter()
.filter_map(|id| flow.stages.iter().find(|m| m.id == *id))
.cloned()
.collect();
if sub_movs.is_empty() {
return Err(anyhow::anyhow!(
"No valid sub-stages found for parallel execution"
));
}
self.run_stages_parallel(&parent.id, &sub_movs, state).await
}
/// Run a set of stages concurrently and aggregate their outputs into the
/// parallel shape `{"parallel": true, "agents": {id: output}, ...}` that
/// `evaluate_rules` and `all()`/`any()` aggregation understand. Shared by
/// declared `parallel:` stages and team_leader-synthesized workers.
async fn run_stages_parallel(
&self,
parent_id: &str,
stages: &[Stage],
state: &FlowState,
) -> Result<serde_json::Value> {
// Strip the parent's visit count so promotion rules don't fire on
// sub-stages (takt excludes promotion on parallel sub-steps — the
// count tracks the parent, not them).
let mut sub_state = state.clone();
sub_state.variables.remove("__visit_count");
let futures: Vec<_> = stages
.iter()
.map(|m| self.execute_movement(m, &sub_state))
.collect();
let results = futures::future::join_all(futures).await;
// Aggregate results
let mut outputs = serde_json::Map::new();
let mut all_success = true;
for (i, result) in results.into_iter().enumerate() {
let sub_id = &stages[i].id;
match result {
Ok(output) => {
outputs.insert(sub_id.clone(), output);
}
Err(e) => {
warn!("Parallel sub-stage '{}' failed: {}", sub_id, e);
all_success = false;
outputs.insert(
sub_id.clone(),
serde_json::json!({"status": "failed", "error": e.to_string()}),
);
}
}
}
Ok(serde_json::json!({
"stage": parent_id,
"parallel": true,
"status": if all_success { "completed" } else { "partial" },
"agents": outputs,
}))
}
/// Execute a team_leader stage: a leader call decomposes the task into
/// parts (JSON), the parts run concurrently as synthesized worker stages,
/// and the outputs aggregate into the parallel shape.
///
/// Robustness ladder: a malformed leader reply gets ONE retry with the
/// parse error attached; if that also fails, the stage degrades to a
/// single worker executing the original instruction — a decomposition
/// failure must not kill work a single agent could do.
async fn execute_team_leader(
&self,
stage: &Stage,
spec: &super::team_leader::TeamLeaderSpec,
state: &FlowState,
) -> Result<serde_json::Value> {
use super::team_leader;
let expanded_instruction = expand_template(&stage.instruction, &state.variables);
let max_parts = spec.max_parts.max(1);
// Leader phase: readonly decomposition call (the leader plans; the
// workers edit).
let mut leader_stage = stage.clone();
leader_stage.team_leader = None;
leader_stage.gates = Vec::new();
leader_stage.permission = MovementPermission::Readonly;
leader_stage.tools = Vec::new();
let mut parts = None;
let mut last_parse_error = String::new();
for attempt in 0..2 {
let mut prompt_stage = leader_stage.clone();
prompt_stage.instruction =
team_leader::decomposition_prompt(&expanded_instruction, max_parts);
if attempt > 0 {
prompt_stage.instruction.push_str(&format!(
"\n\n# Previous attempt failed\n{}\nReply with ONLY the JSON array this time.",
last_parse_error
));
}
// Box::pin: execute_movement → execute_team_leader →
// execute_movement would otherwise be an infinite-sized future.
let leader_output = Box::pin(self.execute_movement(&prompt_stage, state)).await?;
let leader_text = leader_output
.get("output")
.and_then(|v| v.as_str())
.unwrap_or_default();
match team_leader::parse_parts(leader_text, max_parts) {
Ok(p) => {
parts = Some(p);
break;
}
Err(e) => {
warn!(
"team_leader '{}' decomposition attempt {} unparsable: {}",
stage.id,
attempt + 1,
e
);
last_parse_error = e;
}
}
}
// Graceful degradation: run the whole task as one worker.
let parts = parts.unwrap_or_else(|| {
warn!(
"team_leader '{}' falling back to a single worker (decomposition failed twice)",
stage.id
);
vec![super::team_leader::TaskPart {
id: format!("{}-worker", stage.id),
title: String::new(),
instruction: expanded_instruction.clone(),
}]
});
info!(
"team_leader '{}' decomposed into {} part(s): [{}]",
stage.id,
parts.len(),
parts
.iter()
.map(|p| p.id.as_str())
.collect::<Vec<_>>()
.join(", ")
);
let workers: Vec<Stage> = parts
.iter()
.map(|part| team_leader::worker_stage(stage, spec, part))
.collect();
Box::pin(self.run_stages_parallel(&stage.id, &workers, state)).await
}
/// Execute a Sangha consensus stage. Members run independently, then their
/// explicit `SANGHA_DECISION=*` lines are tallied. The stage succeeds only
/// when approvals meet quorum.
async fn execute_sangha(
&self,
stage: &Stage,
spec: &super::sangha::SanghaSpec,
state: &FlowState,
) -> Result<serde_json::Value> {
use super::sangha;
let expanded_instruction = expand_template(&stage.instruction, &state.variables);
let mut prompt_parent = stage.clone();
prompt_parent.instruction = expanded_instruction;
let members = sangha::members_or_default(spec);
let quorum = spec.quorum.max(1) as usize;
let member_stages = members
.iter()
.map(|member| sangha::member_stage(&prompt_parent, spec, member))
.collect::<Vec<_>>();
info!(
"sangha '{}' collecting consensus from {} member(s), quorum={}",
stage.id,
member_stages.len(),
quorum
);
let parallel_output =
Box::pin(self.run_stages_parallel(&stage.id, &member_stages, state)).await?;
let member_outputs = parallel_output
.get("agents")
.and_then(|agents| agents.as_object())
.cloned()
.unwrap_or_default();
let mut approvals = 0usize;
let mut revisions = 0usize;
let mut abstentions = 0usize;
let mut decisions = serde_json::Map::new();
for (member_id, output) in &member_outputs {
let text = output
.get("output")
.and_then(|value| value.as_str())
.unwrap_or_default();
let decision = sangha::extract_decision(text);
match decision {
sangha::SanghaDecision::Approve => approvals += 1,
sangha::SanghaDecision::Revise => revisions += 1,
sangha::SanghaDecision::Abstain => abstentions += 1,
}
decisions.insert(
member_id.clone(),
serde_json::json!({
"decision": decision.as_str(),
"status": output.get("status").cloned().unwrap_or_else(|| serde_json::json!("unknown")),
}),
);
}
let accepted = approvals >= quorum;
Ok(serde_json::json!({
"stage": stage.id,
"sangha": true,
"parallel": true,
"status": if accepted { "completed" } else { "failed" },
"decision": if accepted { "accepted" } else { "needs_revision" },
"quorum": quorum,
"approvals": approvals,
"revisions": revisions,
"abstentions": abstentions,
"decisions": decisions,
"members": member_outputs,
}))
}
/// Execute a sub-workflow as a single stage. The child flow inherits the
/// parent's variables, additionally seeded with any `call.args` (with `{key}`
/// substitution against parent variables). On success the child's final
/// FlowState is collapsed into a JSON value the parent's rules can match
/// against; on failure the error bubbles up so the parent can route via a
/// FAIL rule. Re-entrant: the child runs through the same `execute_piece_state`
/// loop, so nesting depth is bounded only by `flow.max_stages` per level.
async fn execute_workflow_call(
&self,
stage: &Stage,
call: &WorkflowCallSpec,
parent_state: &FlowState,
) -> Result<serde_json::Value> {
// Resolve the child flow up front so misspellings fail fast with a
// useful message rather than a downstream "stage not found".
let child_flow = self
.flows
.get(&call.flow)
.ok_or_else(|| {
anyhow::anyhow!(
"Stage '{}' calls unknown flow '{}'. Registered flows: {}",
stage.id,
call.flow,
self.flows.keys().cloned().collect::<Vec<_>>().join(", ")
)
})?
.clone();
info!(
"Stage '{}' invoking sub-workflow '{}' (parent_variables={}, args={})",
stage.id,
call.flow,
parent_state.variables.len(),
call.args.len()
);
// Seed child state with the parent's variables so `{task}`, prior
// `{<stage>_output}`, and `__run_id` flow through unchanged. Then layer
// explicit `call.args` on top with template expansion against the parent.
let mut child_variables = parent_state.variables.clone();
for (k, v) in &call.args {
let expanded = expand_template(v, &parent_state.variables);
child_variables.insert(k.clone(), serde_json::Value::String(expanded));
}
let child_state = FlowState {
flow_name: child_flow.name.clone(),
current_movement: child_flow.initial_movement.clone(),
movement_count: 0,
history: vec![],
variables: child_variables,
status: FlowStatus::Running,
started_at: Utc::now(),
completed_at: None,
};
// Box::pin the recursive call: Rust async fns can't have infinite-sized
// futures, and execute_workflow_call → execute_piece_state → execute_movement
// → execute_workflow_call would otherwise form a cycle.
let final_state =
Box::pin(self.execute_piece_state(&child_flow.name, &child_flow, child_state))
.await
.map_err(|e| anyhow::anyhow!("Sub-workflow '{}' failed: {}", call.flow, e))?;
// Collapse the child's terminal output into a value the parent's rules
// can match. Surfacing both `status` and the last `_output` keeps
// tag-style ("COMPLETE") and content-style judging both viable.
let last_output = final_state
.history
.last()
.and_then(|t| t.output.clone())
.unwrap_or(serde_json::Value::Null);
Ok(serde_json::json!({
"stage": stage.id,
"workflow_call": call.flow,
"status": format!("{:?}", final_state.status).to_lowercase(),
"output": last_output,
"stages_executed": final_state.movement_count,
}))
}
/// Build a local summary from state without calling Claude Code CLI.
/// Used for terminal/complete stages to avoid unnecessary LLM calls.
fn build_local_summary(&self, state: &FlowState) -> serde_json::Value {
let stages: Vec<String> = state
.history
.iter()
.map(|t| format!("{} -> {}", t.from, t.to))
.collect();
let outputs: serde_json::Map<String, serde_json::Value> = state
.variables
.iter()
.filter(|(k, _)| k.ends_with("_output"))
.map(|(k, v)| {
let key = k.trim_end_matches("_output").to_string();
let preview = v
.as_object()
.and_then(|obj| obj.get("status"))
.and_then(|s| s.as_str())
.unwrap_or("unknown");
(key, serde_json::json!(preview))
})
.collect();
serde_json::json!({
"stage": "complete",
"status": "completed",
"summary": {
"flow": state.flow_name,
"movements_executed": state.movement_count,
"transitions": stages,
"step_results": outputs,
}
})
}
/// Build the prompt for a stage using faceted prompting.
///
/// Composition order (takt-style):
/// - System: persona (via FacetRegistry)
/// - User: knowledge → instruction → policy → output contract → tools → tags
fn build_movement_prompt(&self, stage: &Stage, state: &FlowState) -> String {
// Build output contract text if present
let contract_text = stage.output_contract.as_ref().map(|c| {
let mut parts = vec![format!("Format: {}", c.format)];
if !c.required_sections.is_empty() {
parts.push(format!(
"Required sections: {}",
c.required_sections.join(", ")
));
}
if let Some(ref file) = c.output_file {
parts.push(format!("Write output to: {}", file));
}
for report in &c.reports {
let mut line = format!(
"This stage produces a named report `{}` ({}); downstream stages reference it as `{{report:{}}}`",
report.name, report.format, report.name
);
if let Some(desc) = &report.description {
line.push_str(" — ");
line.push_str(desc);
}
parts.push(line);
}
parts.join("\n")
});
// Expand template variables in instruction: {key} -> state.variables[key]
let expanded_instruction = expand_template(&stage.instruction, &state.variables);
// Use faceted prompting to compose system + user message
let composed = self.facet_registry.compose(
stage.persona.as_deref(),
stage.policy.as_deref(),
stage.knowledge.as_deref(),
&expanded_instruction,
contract_text.as_deref(),
);
let mut parts = Vec::new();
// Inject task description when the stage instruction did not already
// expand `{task}` into the user prompt.
if let Some(task_text) = state.variables.get("task").and_then(|v| v.as_str())
&& !expanded_instruction.contains(task_text)
{
parts.push(format!("## User Task\n\n{}", task_text));
}
// User message (knowledge → instruction → policy → output contract).
// The persona system prompt is forwarded separately through
// ProviderOptions.system_prompt during live execution.
if !composed.user.is_empty() {
parts.push(composed.user);
}
// Add available tools
if !stage.tools.is_empty() {
parts.push(format!("Available tools: {}", stage.tools.join(", ")));
}
// Add permission context
parts.push(format!("Permission level: {:?}", stage.permission));
// Inject context from previous stages for continuity
// (skip if pass_previous_response is false — used for fix stages)
if stage.pass_previous_response && !state.variables.is_empty() {
let var_summary: Vec<String> = state
.variables
.iter()
.filter(|(k, _)| k.ends_with("_output"))
.map(|(k, v)| {
let key = k.trim_end_matches("_output");
// Extract the actual output text from JSON if possible
let output_text = v
.as_object()
.and_then(|obj| obj.get("output"))
.and_then(|o| o.as_str())
.map(|s| truncate_for_context(s, 2000))
.unwrap_or_else(|| truncate_for_context(&v.to_string(), 500));
format!("[Previous '{}' result]:\n{}", key, output_text)
})
.collect();
if !var_summary.is_empty() {
parts.push(format!(
"## Context from previous steps\n\n{}",
var_summary.join("\n\n")
));
}
}
// Also inject native execution context if bridge is available.
if let Some(ref bridge) = self.bridge {
let agent_id = stage.persona.as_deref().unwrap_or("default");
let recent = bridge.get_recent_context(agent_id, 3);
if !recent.is_empty() {
parts.push(format!(
"## Recent conversation context\n\n{}",
recent.join("\n")
));
}
}
// Inject tag instructions for routing (takt-style [STEP:N] tags)
if !stage.rules.is_empty() {
let tag_instructions =
super::judge::MovementJudge::generate_tag_instructions(&stage.rules);
parts.push(tag_instructions);
}
parts.join("\n\n")
}
/// Evaluate routing rules against stage output using the MovementJudge.
///
/// Evaluation priority (takt-style):
/// 1. Aggregate conditions (all/any) for parallel outputs
/// 2. [STEP:N] tag detection
/// 3. Simple string conditions
/// 4. AI judge evaluation
/// 5. Fallback to first "success" rule
async fn evaluate_rules(
&self,
rules: &[MovementRule],
output: &serde_json::Value,
_state: &FlowState,
) -> Result<Option<String>> {
// Filter rules tagged interactive_only / requires_user_input when this
// engine is in pipeline mode. Both flags imply "skip without a human in
// the loop"; requires_user_input additionally signals to interactive UIs
// that the next stage expects user input (consumed by the front-end, not
// here).
let filtered: Vec<&MovementRule> = rules
.iter()
.filter(|r| self.interactive || !(r.interactive_only || r.requires_user_input))
.collect();
if filtered.is_empty() {
return Ok(None);
}
let output_str = serde_json::to_string(output).unwrap_or_default();
// Parallel stages return {"parallel": true, "agents": {sub_id: output}}
// (built by execute_parallel_movements). Extract per-agent texts so the
// judge can evaluate all()/any() aggregate conditions across them.
let parallel_outputs: Option<HashMap<String, String>> = output
.get("parallel")
.and_then(|v| v.as_bool())
.unwrap_or(false)
.then(|| output.get("agents").and_then(|v| v.as_object()))
.flatten()
.map(|agents| {
agents
.iter()
.map(|(sub_id, val)| {
let text = val
.get("output")
.and_then(|v| v.as_str())
.map(str::to_string)
.unwrap_or_else(|| serde_json::to_string(val).unwrap_or_default());
(sub_id.clone(), text)
})
.collect()
});
// Judge takes a slice of owned rules; clone the filtered references into
// a temporary Vec. Rules are tiny structs (4 short strings + flags) so
// this is a negligible allocation in practice.
let filtered_owned: Vec<MovementRule> = filtered.iter().map(|&r| r.clone()).collect();
// Real-LLM judgments for ai() rules (opt-in via CCSWARM_LLM_JUDGE=1).
// Computed here — the async side — and handed to the sync judge as a
// verdict map so rule-priority ordering stays intact.
let llm_verdicts = self
.llm_evaluate_ai_rules(&filtered_owned, &output_str)
.await;
let judge_result = self.judge.evaluate(
&output_str,
&filtered_owned,
parallel_outputs.as_ref(),
llm_verdicts.as_ref(),
)?;
if let Some(index) = judge_result.matched_rule_index
&& index < filtered_owned.len()
{
debug!(
"Judge matched rule {}: method={:?}, confidence={:.2}, next={}",
index,
judge_result.match_method,
judge_result.confidence,
filtered_owned[index].next
);
return Ok(Some(filtered_owned[index].next.clone()));
}
Ok(None)
}
/// Evaluate `ai()` rule conditions with a real LLM call (one short YES/NO
/// question per rule). Returns a verdict map keyed by rule index, or
/// `None` when disabled (`CCSWARM_LLM_JUDGE` unset), no bridge is
/// configured, or no `ai()` rules exist. Per-rule failures are logged at
/// warn and left out of the map so the lexical heuristic covers them —
/// never a silent fallback.
async fn llm_evaluate_ai_rules(
&self,
rules: &[MovementRule],
output: &str,
) -> Option<HashMap<usize, bool>> {
let enabled = std::env::var("CCSWARM_LLM_JUDGE")
.map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
.unwrap_or(false);
if !enabled {
return None;
}
let bridge = self.bridge.as_ref()?;
if !rules
.iter()
.any(|r| matches!(r.condition, RuleCondition::AiCondition { .. }))
{
return None;
}
let identity = crate::identity::AgentIdentity {
agent_id: "llm-judge".to_string(),
specialization: crate::identity::AgentRole::Frontend {
technologies: Vec::new(),
responsibilities: Vec::new(),
boundaries: Vec::new(),
},
workspace_path: self.working_dir.clone(),
env_vars: std::collections::HashMap::new(),
session_id: uuid::Uuid::new_v4().to_string(),
parent_process_id: std::process::id().to_string(),
initialized_at: chrono::Utc::now(),
};
let options = crate::session::bridge::MovementExecOptions {
provider: self.default_provider.or_else(|| {
std::env::var("CCSWARM_PROVIDER")
.ok()
.as_deref()
.and_then(crate::providers::ProviderKind::parse)
}),
..Default::default()
};
let mut verdicts = HashMap::new();
for (index, rule) in rules.iter().enumerate() {
let RuleCondition::AiCondition { ai: condition } = &rule.condition else {
continue;
};
let prompt = format!(
"You are a routing judge for an automated workflow. Decide whether \
the condition holds for the agent output below.\n\n\
# Condition\n{}\n\n# Agent output (truncated)\n{}\n\n\
Reply with exactly YES or NO on the first line. No other text.",
condition,
truncate_for_context(output, 1500)
);
match bridge
.execute_with_retry(
"llm-judge",
&prompt,
&identity,
&self.working_dir,
None,
0,
0,
&options,
)
.await
{
Ok(result) => match super::judge::parse_judge_reply(&result.raw) {
Some(verdict) => {
verdicts.insert(index, verdict);
}
None => warn!(
"LLM judge gave an ambiguous reply for ai() rule {} — \
falling back to the lexical heuristic for it",
index
),
},
Err(e) => warn!(
"LLM judge call failed for ai() rule {}: {} — \
falling back to the lexical heuristic for it",
index, e
),
}
}
(!verdicts.is_empty()).then_some(verdicts)
}
/// Validate output against a contract, returning detailed violation info.
fn validate_output_contract(
&self,
contract: &OutputContract,
output: &serde_json::Value,
) -> Result<()> {
let result = Self::validate_contract(contract, output);
if !result.valid {
let messages: Vec<&str> = result
.violations
.iter()
.map(|v| v.message.as_str())
.collect();
return Err(anyhow::anyhow!(
"Output contract violations:\n- {}",
messages.join("\n- ")
));
}
Ok(())
}
/// Validate output against a contract, returning a detailed result.
pub fn validate_contract(
contract: &OutputContract,
output: &serde_json::Value,
) -> ContractValidationResult {
let mut violations = Vec::new();
let output_str = serde_json::to_string_pretty(output).unwrap_or_default();
// 1. Check required sections
for section in &contract.required_sections {
if !output_str.contains(section) {
violations.push(ContractViolation {
kind: ViolationKind::MissingSection,
message: format!("Missing required section: '{}'", section),
});
}
}
// 2. Check required JSON keys
if let serde_json::Value::Object(map) = output {
for key in &contract.required_keys {
if !map.contains_key(key) {
violations.push(ContractViolation {
kind: ViolationKind::MissingKey,
message: format!("Missing required key: '{}'", key),
});
}
}
} else if !contract.required_keys.is_empty() {
violations.push(ContractViolation {
kind: ViolationKind::InvalidFormat,
message: "Output is not a JSON object but required_keys are specified".to_string(),
});
}
// 3. Check format
match contract.format.as_str() {
"json" => {
// Already JSON (since output is serde_json::Value)
}
"markdown"
// Markdown should contain at least one heading or list
if !output_str.contains('#') && !output_str.contains("- ") => {
violations.push(ContractViolation {
kind: ViolationKind::InvalidFormat,
message:
"Output does not appear to be valid markdown (no headings or lists)"
.to_string(),
});
}
"yaml"
// Check for YAML-like structure (key: value patterns)
if !output_str.contains(':') => {
violations.push(ContractViolation {
kind: ViolationKind::InvalidFormat,
message: "Output does not appear to be valid YAML".to_string(),
});
}
"code"
// Code should have some structure
if output_str.len() < 10 => {
violations.push(ContractViolation {
kind: ViolationKind::InvalidFormat,
message: "Output appears too short to be code".to_string(),
});
}
_ => {
// text or unknown format - no specific validation
}
}
// 4. Check length constraints
if let Some(min_len) = contract.min_length
&& output_str.len() < min_len
{
violations.push(ContractViolation {
kind: ViolationKind::TooShort,
message: format!(
"Output too short: {} chars (minimum: {})",
output_str.len(),
min_len
),
});
}
if let Some(max_len) = contract.max_length
&& output_str.len() > max_len
{
violations.push(ContractViolation {
kind: ViolationKind::TooLong,
message: format!(
"Output too long: {} chars (maximum: {})",
output_str.len(),
max_len
),
});
}
// 5. Check must_match patterns
for pattern_str in &contract.must_match {
match regex::Regex::new(pattern_str) {
Ok(re) => {
if !re.is_match(&output_str) {
violations.push(ContractViolation {
kind: ViolationKind::PatternViolation,
message: format!(
"Output does not match required pattern: '{}'",
pattern_str
),
});
}
}
Err(e) => {
warn!("Invalid regex in must_match: '{}': {}", pattern_str, e);
}
}
}
// 6. Check must_not_match patterns
for pattern_str in &contract.must_not_match {
match regex::Regex::new(pattern_str) {
Ok(re) => {
if re.is_match(&output_str) {
violations.push(ContractViolation {
kind: ViolationKind::ForbiddenPattern,
message: format!("Output matches forbidden pattern: '{}'", pattern_str),
});
}
}
Err(e) => {
warn!("Invalid regex in must_not_match: '{}': {}", pattern_str, e);
}
}
}
// 7. JSON schema validation (basic key/type checking)
if let Some(serde_json::Value::Object(schema_obj)) = contract.schema.as_ref()
&& let Some(serde_json::Value::Object(required)) = schema_obj.get("properties")
&& let serde_json::Value::Object(output_obj) = output
{
for (key, _prop_schema) in required {
if !output_obj.contains_key(key) {
violations.push(ContractViolation {
kind: ViolationKind::SchemaViolation,
message: format!("Schema violation: missing property '{key}'"),
});
}
}
}
ContractValidationResult {
valid: violations.is_empty(),
violations,
}
}
}
impl Default for FlowEngine {
fn default() -> Self {
Self::new()
}
}
/// Built-in flow templates
/// Builtin `team-dynamic` flow: plan → team_leader implement → review.
/// Defined in YAML for legibility; parse+validate happen once at registry
/// construction and the unit tests cover both.
fn team_dynamic_flow() -> Flow {
const YAML: &str = r#"
name: team-dynamic
description: "Orchestrator-worker: planner designs, a team leader decomposes the implementation into parallel parts at runtime, reviewer validates"
max_stages: 12
initial_movement: plan
stages:
- id: plan
persona: planner
instruction: |
Analyze the following task and produce a concise implementation plan
(key components, order of work, risks):
{task}
permission: readonly
rules:
- condition: success
next: implement
- id: implement
persona: coder
instruction: |
Implement the following task according to the plan.
# Task
{task}
# Plan
{plan_output}
permission: edit
team_leader:
max_parts: 3
part_persona: coder
part_permission: edit
rules:
- condition:
all: ["completed"]
next: review
- condition:
any: ["failed"]
next: review
- id: review
persona: reviewer
instruction: |
Review the implementation work for the task below. Identify bugs,
missing pieces, and quality issues. End with APPROVED or NEEDS_FIX.
# Task
{task}
permission: readonly
rules:
- condition: success
next: complete
- id: complete
instruction: "_local"
"#;
Flow::from_yaml(YAML).expect("builtin team-dynamic flow must parse")
}
pub fn builtin_flows() -> Vec<Flow> {
vec![
team_dynamic_flow(),
// Default development workflow
Flow {
name: "default".to_string(),
description: "Sangha consensus workflow: plan → sangha consensus → implement → review → fix"
.to_string(),
max_stages: 30,
max_stage_visits: 3,
on_rate_limit: Vec::new(),
initial_movement: "plan".to_string(),
stages: vec![
Stage {
id: "plan".to_string(),
persona: Some("planner".to_string()),
policy: None,
knowledge: None,
provider: None,
model: None,
instruction: "Analyze the task and create an implementation plan".to_string(),
tools: vec!["read".to_string(), "grep".to_string(), "glob".to_string()],
permission: MovementPermission::Readonly,
rules: vec![MovementRule {
condition: RuleCondition::Simple("success".to_string()),
next: "sangha".to_string(),
priority: 0,
interactive_only: false,
requires_user_input: false, }],
parallel: false,
sub_movements: vec![],
output_contract: None,
timeout: None,
max_retries: 0,
agent: None,
working_dir: None,
retry_delay_ms: default_retry_delay(),
pass_previous_response: true,
call: None, promotion: Vec::new(),
gates: Vec::new(),
team_leader: None,
sangha: None, },
Stage {
id: "sangha".to_string(),
persona: Some("planner".to_string()),
policy: Some("review".to_string()),
knowledge: None,
provider: None,
model: None,
instruction: "Review the plan and task as a Sangha. Approve only when the implementation direction is clear, scoped, and testable.\n\n# Task\n{task}\n\n# Plan\n{plan_output}".to_string(),
tools: vec!["read".to_string(), "grep".to_string(), "glob".to_string()],
permission: MovementPermission::Readonly,
rules: vec![MovementRule {
condition: RuleCondition::Simple("success".to_string()),
next: "implement".to_string(),
priority: 0,
interactive_only: false,
requires_user_input: false,
}],
parallel: false,
sub_movements: vec![],
output_contract: None,
timeout: None,
max_retries: 0,
agent: None,
working_dir: None,
retry_delay_ms: default_retry_delay(),
pass_previous_response: true,
call: None,
promotion: Vec::new(),
gates: Vec::new(),
team_leader: None,
sangha: Some(super::sangha::SanghaSpec {
quorum: 2,
members: Vec::new(),
member_permission: None,
member_tools: None,
member_timeout_secs: None,
}),
},
Stage {
id: "implement".to_string(),
persona: Some("coder".to_string()),
policy: Some("coding".to_string()),
knowledge: None,
provider: None,
model: None,
instruction: "Implement the planned changes according to the Sangha consensus.\n\n# Consensus\n{sangha_output}".to_string(),
tools: vec![
"read".to_string(),
"write".to_string(),
"edit".to_string(),
"bash".to_string(),
],
permission: MovementPermission::Edit,
rules: vec![
MovementRule {
condition: RuleCondition::Simple("success".to_string()),
next: "review".to_string(),
priority: 0,
interactive_only: false,
requires_user_input: false, },
MovementRule {
condition: RuleCondition::Simple("error".to_string()),
next: "fix".to_string(),
priority: 1,
interactive_only: false,
requires_user_input: false, },
],
parallel: false,
sub_movements: vec![],
output_contract: None,
timeout: None,
max_retries: 1,
agent: None,
working_dir: None,
retry_delay_ms: default_retry_delay(),
pass_previous_response: true,
call: None, promotion: Vec::new(),
gates: Vec::new(),
team_leader: None,
sangha: None, },
Stage {
id: "review".to_string(),
persona: Some("reviewer".to_string()),
policy: Some("review".to_string()),
knowledge: None,
provider: None,
model: None,
instruction: "Review the implementation for quality and correctness"
.to_string(),
tools: vec!["read".to_string(), "grep".to_string(), "bash".to_string()],
permission: MovementPermission::Readonly,
rules: vec![
MovementRule {
condition: RuleCondition::Simple("success".to_string()),
next: "complete".to_string(),
priority: 0,
interactive_only: false,
requires_user_input: false, },
MovementRule {
condition: RuleCondition::Simple("fixes_needed".to_string()),
next: "fix".to_string(),
priority: 1,
interactive_only: false,
requires_user_input: false, },
],
parallel: false,
sub_movements: vec![],
output_contract: None,
timeout: None,
max_retries: 0,
agent: None,
working_dir: None,
retry_delay_ms: default_retry_delay(),
pass_previous_response: true,
call: None, promotion: Vec::new(),
gates: Vec::new(),
team_leader: None,
sangha: None, },
Stage {
id: "fix".to_string(),
persona: Some("coder".to_string()),
policy: Some("coding".to_string()),
knowledge: None,
provider: None,
model: None,
instruction: "Fix the identified issues".to_string(),
tools: vec![
"read".to_string(),
"write".to_string(),
"edit".to_string(),
"bash".to_string(),
],
permission: MovementPermission::Edit,
rules: vec![MovementRule {
condition: RuleCondition::Simple("success".to_string()),
next: "review".to_string(),
priority: 0,
interactive_only: false,
requires_user_input: false, }],
parallel: false,
sub_movements: vec![],
output_contract: None,
timeout: None,
max_retries: 2,
agent: None,
working_dir: None,
retry_delay_ms: default_retry_delay(),
pass_previous_response: true,
call: None, promotion: Vec::new(),
gates: Vec::new(),
team_leader: None,
sangha: None, },
Stage {
id: "complete".to_string(),
persona: None,
policy: None,
knowledge: None,
provider: None,
model: None,
instruction: String::new(), // Empty = local summary, no Claude call
tools: vec![],
permission: MovementPermission::Readonly,
rules: vec![], // Terminal
parallel: false,
sub_movements: vec![],
output_contract: None,
timeout: None,
max_retries: 0,
agent: None,
working_dir: None,
retry_delay_ms: default_retry_delay(),
pass_previous_response: true,
call: None, promotion: Vec::new(),
gates: Vec::new(),
team_leader: None,
sangha: None, },
],
variables: HashMap::new(),
metadata: HashMap::new(),
interactive_mode: None,
},
// Research workflow
Flow {
name: "research".to_string(),
description: "Autonomous research and investigation workflow".to_string(),
max_stages: 20,
max_stage_visits: 3,
on_rate_limit: Vec::new(),
initial_movement: "investigate".to_string(),
stages: vec![
Stage {
id: "investigate".to_string(),
persona: Some("researcher".to_string()),
policy: None,
knowledge: None,
provider: None,
model: None,
instruction: "Research and investigate the topic".to_string(),
tools: vec![
"read".to_string(),
"grep".to_string(),
"glob".to_string(),
"search".to_string(),
],
permission: MovementPermission::Readonly,
rules: vec![MovementRule {
condition: RuleCondition::Simple("success".to_string()),
next: "summarize".to_string(),
priority: 0,
interactive_only: false,
requires_user_input: false, }],
parallel: false,
sub_movements: vec![],
output_contract: None,
timeout: None,
max_retries: 0,
agent: None,
working_dir: None,
retry_delay_ms: default_retry_delay(),
pass_previous_response: true,
call: None, promotion: Vec::new(),
gates: Vec::new(),
team_leader: None,
sangha: None, },
Stage {
id: "summarize".to_string(),
persona: Some("writer".to_string()),
policy: None,
knowledge: None,
provider: None,
model: None,
instruction: "Summarize findings into a report".to_string(),
tools: vec!["read".to_string(), "write".to_string()],
permission: MovementPermission::Edit,
rules: vec![], // Terminal
parallel: false,
sub_movements: vec![],
output_contract: Some(OutputContract {
format: "markdown".to_string(),
required_sections: vec!["summary".to_string(), "findings".to_string()],
schema: None,
output_file: Some("research-report.md".to_string()),
required_keys: vec![],
min_length: None,
max_length: None,
must_match: vec![],
must_not_match: vec![],
allowed_files: vec![],
reports: vec![],
}),
timeout: None,
max_retries: 0,
agent: None,
working_dir: None,
retry_delay_ms: default_retry_delay(),
pass_previous_response: true,
call: None, promotion: Vec::new(),
gates: Vec::new(),
team_leader: None,
sangha: None, },
],
variables: HashMap::new(),
metadata: HashMap::new(),
interactive_mode: None,
},
// Review-fix minimal workflow
Flow {
name: "review-fix".to_string(),
description: "Minimal review and fix cycle".to_string(),
max_stages: 10,
max_stage_visits: 3,
on_rate_limit: Vec::new(),
initial_movement: "review".to_string(),
stages: vec![
Stage {
id: "review".to_string(),
persona: Some("reviewer".to_string()),
policy: Some("review".to_string()),
knowledge: None,
provider: None,
model: None,
instruction: "Review the code for issues".to_string(),
tools: vec!["read".to_string(), "grep".to_string(), "bash".to_string()],
permission: MovementPermission::Readonly,
rules: vec![
MovementRule {
condition: RuleCondition::Simple("fixes_needed".to_string()),
next: "fix".to_string(),
priority: 1,
interactive_only: false,
requires_user_input: false, },
MovementRule {
condition: RuleCondition::Simple("success".to_string()),
next: "done".to_string(),
priority: 0,
interactive_only: false,
requires_user_input: false, },
],
parallel: false,
sub_movements: vec![],
output_contract: None,
timeout: None,
max_retries: 0,
agent: None,
working_dir: None,
retry_delay_ms: default_retry_delay(),
pass_previous_response: true,
call: None, promotion: Vec::new(),
gates: Vec::new(),
team_leader: None,
sangha: None, },
Stage {
id: "fix".to_string(),
persona: Some("coder".to_string()),
policy: Some("coding".to_string()),
knowledge: None,
provider: None,
model: None,
instruction: "Fix the identified issues".to_string(),
tools: vec![
"read".to_string(),
"write".to_string(),
"edit".to_string(),
"bash".to_string(),
],
permission: MovementPermission::Edit,
rules: vec![MovementRule {
condition: RuleCondition::Simple("success".to_string()),
next: "review".to_string(),
priority: 0,
interactive_only: false,
requires_user_input: false, }],
parallel: false,
sub_movements: vec![],
output_contract: None,
timeout: None,
max_retries: 2,
agent: None,
working_dir: None,
retry_delay_ms: default_retry_delay(),
pass_previous_response: true,
call: None, promotion: Vec::new(),
gates: Vec::new(),
team_leader: None,
sangha: None, },
Stage {
id: "done".to_string(),
persona: None,
policy: None,
knowledge: None,
provider: None,
model: None,
instruction: "Review complete".to_string(),
tools: vec![],
permission: MovementPermission::Readonly,
rules: vec![], // Terminal
parallel: false,
sub_movements: vec![],
output_contract: None,
timeout: None,
max_retries: 0,
agent: None,
working_dir: None,
retry_delay_ms: default_retry_delay(),
pass_previous_response: true,
call: None, promotion: Vec::new(),
gates: Vec::new(),
team_leader: None,
sangha: None, },
],
variables: HashMap::new(),
metadata: HashMap::new(),
interactive_mode: None,
},
// Quick single-shot workflow (1 Claude call, fastest)
Flow {
name: "quick".to_string(),
description: "Single-shot execution: one Claude call, no plan/review overhead"
.to_string(),
max_stages: 1,
max_stage_visits: 3,
on_rate_limit: Vec::new(),
initial_movement: "execute".to_string(),
stages: vec![Stage {
id: "execute".to_string(),
persona: Some("coder".to_string()),
policy: Some("coding".to_string()),
knowledge: None,
provider: None,
model: None,
instruction: "Execute the task directly. Write clean, working code.".to_string(),
tools: vec![
"read".to_string(),
"write".to_string(),
"edit".to_string(),
"bash".to_string(),
"grep".to_string(),
"glob".to_string(),
],
permission: MovementPermission::Edit,
rules: vec![], // Terminal - single shot
parallel: false,
sub_movements: vec![],
output_contract: None,
timeout: None,
max_retries: 1,
agent: None,
working_dir: None,
retry_delay_ms: default_retry_delay(),
pass_previous_response: true,
call: None, promotion: Vec::new(),
gates: Vec::new(),
team_leader: None,
sangha: None, }],
variables: HashMap::new(),
metadata: HashMap::new(),
interactive_mode: None,
},
// Multi-agent team workflow: plan → parallel(frontend + backend) → review → complete
Flow {
name: "team".to_string(),
description: "Multi-agent orchestration: planner designs, frontend & backend agents execute in parallel, reviewer validates".to_string(),
max_stages: 10,
max_stage_visits: 3,
on_rate_limit: Vec::new(),
initial_movement: "plan".to_string(),
stages: vec![
Stage {
id: "plan".to_string(),
persona: Some("planner".to_string()),
policy: Some("coding".to_string()),
knowledge: None,
provider: None,
model: None,
instruction: "Analyze the task and create a plan that splits work between frontend and backend agents. Define clear interfaces and contracts between them.".to_string(),
tools: vec!["read".to_string(), "grep".to_string(), "glob".to_string()],
permission: MovementPermission::Readonly,
rules: vec![MovementRule {
condition: RuleCondition::Simple("success".to_string()),
next: "parallel-implement".to_string(),
priority: 0,
interactive_only: false,
requires_user_input: false, }],
parallel: false,
sub_movements: vec![],
output_contract: None,
timeout: None,
max_retries: 0,
agent: None,
working_dir: None,
retry_delay_ms: default_retry_delay(),
pass_previous_response: true,
call: None, promotion: Vec::new(),
gates: Vec::new(),
team_leader: None,
sangha: None, },
// Parallel hub: dispatches to frontend-impl and backend-impl simultaneously
Stage {
id: "parallel-implement".to_string(),
persona: None,
policy: None,
knowledge: None,
provider: None,
model: None,
instruction: "Execute frontend and backend in parallel".to_string(),
tools: vec![],
permission: MovementPermission::Edit,
rules: vec![MovementRule {
condition: RuleCondition::Simple("success".to_string()),
next: "review".to_string(),
priority: 0,
interactive_only: false,
requires_user_input: false, }],
parallel: true,
sub_movements: vec!["frontend-impl".to_string(), "backend-impl".to_string()],
output_contract: None,
timeout: None,
max_retries: 0,
agent: None,
working_dir: None,
retry_delay_ms: default_retry_delay(),
pass_previous_response: true,
call: None, promotion: Vec::new(),
gates: Vec::new(),
team_leader: None,
sangha: None, },
// Frontend agent (runs in parallel)
Stage {
id: "frontend-impl".to_string(),
persona: Some("coder".to_string()),
policy: Some("coding".to_string()),
knowledge: None,
provider: None,
model: None,
instruction: "Implement the frontend portion of the plan. Focus on UI, user interaction, and client-side logic.".to_string(),
tools: vec!["read".to_string(), "write".to_string(), "edit".to_string(), "bash".to_string()],
permission: MovementPermission::Edit,
rules: vec![],
parallel: false,
sub_movements: vec![],
output_contract: None,
timeout: None,
max_retries: 1,
agent: Some("frontend-specialist".to_string()),
working_dir: None,
retry_delay_ms: default_retry_delay(),
pass_previous_response: true,
call: None, promotion: Vec::new(),
gates: Vec::new(),
team_leader: None,
sangha: None, },
// Backend agent (runs in parallel)
Stage {
id: "backend-impl".to_string(),
persona: Some("coder".to_string()),
policy: Some("coding".to_string()),
knowledge: None,
provider: None,
model: None,
instruction: "Implement the backend portion of the plan. Focus on APIs, data models, and server-side logic.".to_string(),
tools: vec!["read".to_string(), "write".to_string(), "edit".to_string(), "bash".to_string()],
permission: MovementPermission::Edit,
rules: vec![],
parallel: false,
sub_movements: vec![],
output_contract: None,
timeout: None,
max_retries: 1,
agent: Some("backend-specialist".to_string()),
working_dir: None,
retry_delay_ms: default_retry_delay(),
pass_previous_response: true,
call: None, promotion: Vec::new(),
gates: Vec::new(),
team_leader: None,
sangha: None, },
// Supervisor reviews the combined output
Stage {
id: "review".to_string(),
persona: Some("supervisor".to_string()),
policy: Some("review".to_string()),
knowledge: None,
provider: None,
model: None,
instruction: "Review the combined frontend and backend implementation. Verify integration points, check for inconsistencies, and ensure the plan was followed.".to_string(),
tools: vec!["read".to_string(), "grep".to_string(), "bash".to_string()],
permission: MovementPermission::Readonly,
rules: vec![MovementRule {
condition: RuleCondition::Simple("success".to_string()),
next: "complete".to_string(),
priority: 0,
interactive_only: false,
requires_user_input: false, }],
parallel: false,
sub_movements: vec![],
output_contract: None,
timeout: None,
max_retries: 0,
agent: None,
working_dir: None,
retry_delay_ms: default_retry_delay(),
pass_previous_response: true,
call: None, promotion: Vec::new(),
gates: Vec::new(),
team_leader: None,
sangha: None, },
// Local completion
Stage {
id: "complete".to_string(),
persona: None,
policy: None,
knowledge: None,
provider: None,
model: None,
instruction: String::new(),
tools: vec![],
permission: MovementPermission::Readonly,
rules: vec![],
parallel: false,
sub_movements: vec![],
output_contract: None,
timeout: None,
max_retries: 0,
agent: None,
working_dir: None,
retry_delay_ms: default_retry_delay(),
pass_previous_response: true,
call: None, promotion: Vec::new(),
gates: Vec::new(),
team_leader: None,
sangha: None, },
],
variables: HashMap::new(),
metadata: HashMap::new(),
interactive_mode: None,
},
]
}
/// Reject report names that could escape `.ccswarm/runs/<id>/reports/`.
/// Allowed chars: ASCII alphanumerics, '-', '_', '.'. Must not be `.` / `..` /
/// contain consecutive dots or path separators.
fn is_safe_report_name(name: &str) -> bool {
if name.is_empty() || name.len() > 128 {
return false;
}
if name == "." || name == ".." || name.contains("..") {
return false;
}
name.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '.')
}
/// Expand `{key}` template variables in a string using state variables.
///
/// Recognized forms:
/// - `{key}`: replaced with the string form of `variables[key]`.
/// - `{key_output}`: prefers the `output` field from a JSON object value (the
/// standard shape stages emit).
/// - `{report:<name>}`: reads `.ccswarm/runs/<run_id>/reports/<name>` from disk,
/// using the `__run_id` variable stashed by `execute_piece_state`. Missing /
/// unsafe names expand to an empty string so the prompt doesn't leak the
/// literal token. Adopted from takt's `output_contracts` — replaces brittle
/// `{plan_output}` state-variable chaining with a named on-disk contract.
fn expand_template(template: &str, variables: &HashMap<String, serde_json::Value>) -> String {
let mut result = template.to_string();
// First handle `{report:<name>}` so it doesn't collide with the `{key}` loop.
result = expand_report_references(&result, variables);
for (key, value) in variables {
if key.starts_with("__") {
// Internal book-keeping (e.g. `__run_id`) — never user-substitutable.
continue;
}
let placeholder = format!("{{{}}}", key);
if result.contains(&placeholder) {
let replacement = value
.as_str()
.map(|s| s.to_string())
.or_else(|| {
value
.as_object()
.and_then(|obj| obj.get("output"))
.and_then(|o| o.as_str())
.map(|s| truncate_for_context(s, 2000))
})
.unwrap_or_else(|| value.to_string());
result = result.replace(&placeholder, &replacement);
}
}
result
}
fn expand_report_references(
template: &str,
variables: &HashMap<String, serde_json::Value>,
) -> String {
const PREFIX: &str = "{report:";
if !template.contains(PREFIX) {
return template.to_string();
}
let run_id = variables
.get("__run_id")
.and_then(|v| v.as_str())
.unwrap_or("");
let mut out = String::with_capacity(template.len());
let mut rest = template;
while let Some(start) = rest.find(PREFIX) {
out.push_str(&rest[..start]);
let after_prefix = &rest[start + PREFIX.len()..];
let Some(end) = after_prefix.find('}') else {
// Unterminated; emit verbatim and stop.
out.push_str(&rest[start..]);
return out;
};
let name = &after_prefix[..end];
let remainder = &after_prefix[end + 1..];
if run_id.is_empty() || !is_safe_report_name(name) {
// Silent empty-expand: surfacing the raw token would leak internals
// into the prompt and confuse the model.
tracing::warn!(
"Skipping `{{report:{}}}` expansion (run_id_empty={}, unsafe_name={})",
name,
run_id.is_empty(),
!is_safe_report_name(name)
);
} else {
let path = std::path::PathBuf::from(".ccswarm")
.join("runs")
.join(run_id)
.join("reports")
.join(name);
match std::fs::read_to_string(&path) {
Ok(content) => out.push_str(&truncate_for_context(&content, 8000)),
Err(e) => tracing::warn!(
"Failed to read report '{}' for template expansion: {}",
path.display(),
e
),
}
}
rest = remainder;
}
out.push_str(rest);
out
}
fn truncate_for_context(s: &str, max_len: usize) -> String {
if s.len() <= max_len {
s.to_string()
} else {
let boundary = s
.char_indices()
.map(|(index, _)| index)
.take_while(|index| *index <= max_len)
.last()
.unwrap_or(0);
format!("{}... [truncated]", &s[..boundary])
}
}
/// Run a stage's command gates sequentially in `work_dir`. Returns `None`
/// when every gate passes, or `Some((gate_name, feedback))` for the first
/// failure — a bounded, prompt-ready block (stdout/stderr each ≤1000 chars)
/// the engine appends to the instruction before re-running the stage.
async fn run_command_gates(
gates: &[CommandGate],
work_dir: &std::path::Path,
) -> Option<(String, String)> {
for gate in gates {
info!("Running gate '{}': {}", gate.name, gate.command);
let result = tokio::time::timeout(
std::time::Duration::from_secs(gate.timeout_secs),
tokio::process::Command::new("sh")
.arg("-c")
.arg(&gate.command)
.current_dir(work_dir)
.output(),
)
.await;
let feedback = match result {
Err(_) => format!(
"# Gate failure: {}\nCommand `{}` timed out after {}s. \
Make the change converge faster or fix what the command checks.",
gate.name, gate.command, gate.timeout_secs
),
Ok(Err(e)) => format!(
"# Gate failure: {}\nCommand `{}` could not be spawned: {}",
gate.name, gate.command, e
),
Ok(Ok(output)) if output.status.success() => continue,
Ok(Ok(output)) => {
let stdout = truncate_for_context(&String::from_utf8_lossy(&output.stdout), 1000);
let stderr = truncate_for_context(&String::from_utf8_lossy(&output.stderr), 1000);
format!(
"# Gate failure: {} (exit code {})\nCommand: `{}`\n\n\
## stdout\n{}\n\n## stderr\n{}\n\n\
Fix the issues above and ensure `{}` passes.",
gate.name,
output.status.code().unwrap_or(-1),
gate.command,
stdout,
stderr,
gate.command
)
}
};
return Some((gate.name.clone(), feedback));
}
None
}
fn stage_output_succeeded(output: &serde_json::Value) -> bool {
output
.get("status")
.and_then(|status| status.as_str())
.is_some_and(|status| status == "completed")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_piece_from_yaml() {
let yaml = r#"
name: test-flow
description: "A test flow"
max_stages: 10
initial_movement: start
stages:
- id: start
persona: planner
instruction: "Plan the task"
tools: [read, grep]
permission: readonly
rules:
- condition: success
next: end
- id: end
instruction: "Done"
"#;
let flow = Flow::from_yaml(yaml).expect("Failed to parse YAML");
assert_eq!(flow.name, "test-flow");
assert_eq!(flow.stages.len(), 2);
assert_eq!(flow.initial_movement, "start");
assert_eq!(flow.stages[0].permission, MovementPermission::Readonly);
}
#[test]
fn test_piece_validation_invalid_initial() {
let yaml = r#"
name: bad-flow
initial_movement: nonexistent
stages:
- id: start
instruction: "Hello"
"#;
let result = Flow::from_yaml(yaml);
assert!(result.is_err());
}
#[test]
fn test_piece_validation_invalid_rule_target() {
let yaml = r#"
name: bad-rules
initial_movement: start
stages:
- id: start
instruction: "Hello"
rules:
- condition: success
next: nonexistent
"#;
let result = Flow::from_yaml(yaml);
assert!(result.is_err());
}
#[test]
fn test_piece_validation_duplicate_ids() {
let yaml = r#"
name: dup-ids
initial_movement: start
stages:
- id: start
instruction: "First"
- id: start
instruction: "Duplicate"
"#;
let result = Flow::from_yaml(yaml);
assert!(result.is_err());
}
#[test]
fn test_builtin_pieces() {
let flows = builtin_flows();
assert!(!flows.is_empty());
for flow in &flows {
flow.validate()
.unwrap_or_else(|_| panic!("Built-in flow '{}' failed validation", flow.name));
}
}
#[test]
fn test_piece_create_state() {
let yaml = r#"
name: state-test
initial_movement: start
stages:
- id: start
instruction: "Begin"
"#;
let flow = Flow::from_yaml(yaml).expect("parse failed");
let state = flow.create_state();
assert_eq!(state.flow_name, "state-test");
assert_eq!(state.current_movement, "start");
assert_eq!(state.movement_count, 0);
assert_eq!(state.status, FlowStatus::Pending);
}
#[test]
fn test_terminal_movement() {
let yaml = r#"
name: terminal-test
initial_movement: start
stages:
- id: start
instruction: "Begin"
rules:
- condition: success
next: end
- id: end
instruction: "Done"
"#;
let flow = Flow::from_yaml(yaml).expect("parse failed");
assert!(!flow.is_terminal("start"));
assert!(flow.is_terminal("end"));
}
#[tokio::test]
async fn test_piece_engine_execute() {
let yaml = r#"
name: exec-test
initial_movement: step1
stages:
- id: step1
instruction: "Step 1"
rules:
- condition: success
next: step2
- id: step2
instruction: "Step 2 (terminal)"
"#;
let flow = Flow::from_yaml(yaml).expect("parse failed");
let mut engine = FlowEngine::new();
engine.flows.insert("exec-test".to_string(), flow);
let state = engine
.execute_piece("exec-test")
.await
.expect("execution failed");
assert_eq!(state.status, FlowStatus::Completed);
assert_eq!(state.movement_count, 2);
}
#[tokio::test]
async fn test_loop_guard_aborts_cyclic_flow() {
// ping <-> pong cycle, bounded by max_stage_visits rather than max_stages
let yaml = r#"
name: loop-test
max_stages: 30
max_stage_visits: 2
initial_movement: ping
stages:
- id: ping
instruction: "Ping"
rules:
- condition: success
next: pong
- id: pong
instruction: "Pong"
rules:
- condition: success
next: ping
"#;
let flow = Flow::from_yaml(yaml).expect("parse failed");
let mut engine = FlowEngine::new();
engine.flows.insert("loop-test".to_string(), flow);
let state = engine
.execute_piece("loop-test")
.await
.expect("execution failed");
assert_eq!(state.status, FlowStatus::Aborted);
// Two visits each executed; the third visit to 'ping' aborts before
// execution, so only 4 stages ran.
assert_eq!(state.movement_count, 4);
}
#[tokio::test]
async fn test_parallel_all_aggregate_routes_when_every_output_matches() {
// Without a bridge, sub-stage output JSON embeds the instruction text,
// so marker words in instructions drive the aggregate conditions.
let yaml = r#"
name: par-all
max_stages: 10
initial_movement: fanout
stages:
- id: fanout
instruction: "Fan out to reviewers"
parallel: true
sub_movements: [left, right]
rules:
- condition:
all: ["approved"]
next: done
- condition:
any: ["needs_fix"]
next: fixit
- id: left
instruction: "left review approved"
- id: right
instruction: "right review approved"
- id: fixit
instruction: "apply fixes"
- id: done
instruction: "terminal"
"#;
let flow = Flow::from_yaml(yaml).expect("parse failed");
let mut engine = FlowEngine::new();
engine.flows.insert("par-all".to_string(), flow);
let state = engine
.execute_piece("par-all")
.await
.expect("execution failed");
assert_eq!(state.status, FlowStatus::Completed);
let visited: Vec<&str> = state.history.iter().map(|t| t.to.as_str()).collect();
assert_eq!(
visited,
vec!["done"],
"all(approved) should route fanout -> done, got history: {:?}",
visited
);
}
#[tokio::test]
async fn test_parallel_any_aggregate_routes_on_single_match() {
let yaml = r#"
name: par-any
max_stages: 10
initial_movement: fanout
stages:
- id: fanout
instruction: "Fan out to reviewers"
parallel: true
sub_movements: [left, right]
rules:
- condition:
all: ["approved"]
next: done
- condition:
any: ["needs_fix"]
next: fixit
- id: left
instruction: "left review approved"
- id: right
instruction: "right review needs_fix"
- id: fixit
instruction: "apply fixes"
- id: done
instruction: "terminal"
"#;
let flow = Flow::from_yaml(yaml).expect("parse failed");
let mut engine = FlowEngine::new();
engine.flows.insert("par-any".to_string(), flow);
let state = engine
.execute_piece("par-any")
.await
.expect("execution failed");
assert_eq!(state.status, FlowStatus::Completed);
let visited: Vec<&str> = state.history.iter().map(|t| t.to.as_str()).collect();
assert_eq!(
visited,
vec!["fixit"],
"any(needs_fix) should route fanout -> fixit when all(approved) fails, got history: {:?}",
visited
);
}
#[test]
fn test_promotion_last_match_wins() {
let yaml = r#"
name: promo
initial_movement: fix
stages:
- id: fix
instruction: "fix it"
promotion:
- { at: 2, model: opus }
- { at: 3, provider: codex, model: gpt-5 }
"#;
let flow = Flow::from_yaml(yaml).expect("parse failed");
let mut engine = FlowEngine::new();
engine.flows.insert("promo".to_string(), flow);
let stage = engine.flows["promo"].stages[0].clone();
let mut state = engine.flows["promo"].create_state();
// Visit 1: no rule matches — base resolution.
state
.variables
.insert("__visit_count".to_string(), serde_json::json!(1));
let (provider, model) = engine.resolve_effective_provider(&stage, &state);
assert_eq!(provider, None);
assert_eq!(model, None);
// Visit 2: only `at: 2` matches.
state
.variables
.insert("__visit_count".to_string(), serde_json::json!(2));
let (_, model) = engine.resolve_effective_provider(&stage, &state);
assert_eq!(model.as_deref(), Some("opus"));
// Visit 3+: both match — the LAST entry wins.
state
.variables
.insert("__visit_count".to_string(), serde_json::json!(5));
let (provider, model) = engine.resolve_effective_provider(&stage, &state);
assert_eq!(provider, Some(crate::providers::ProviderKind::Codex));
assert_eq!(model.as_deref(), Some("gpt-5"));
}
#[test]
fn test_promotion_skipped_without_visit_count() {
// Parallel sub-stages run with __visit_count stripped; promotion must
// not fire there.
let yaml = r#"
name: promo-skip
initial_movement: a
stages:
- id: a
instruction: "x"
promotion:
- { at: 1, model: opus }
"#;
let flow = Flow::from_yaml(yaml).expect("parse failed");
let mut engine = FlowEngine::new();
engine.flows.insert("promo-skip".to_string(), flow);
let stage = engine.flows["promo-skip"].stages[0].clone();
let state = engine.flows["promo-skip"].create_state(); // no __visit_count
let (_, model) = engine.resolve_effective_provider(&stage, &state);
assert_eq!(model, None, "promotion must not fire without a visit count");
}
#[test]
fn test_on_rate_limit_parses_from_yaml() {
let yaml = r#"
name: fallback
initial_movement: a
on_rate_limit:
- { provider: codex, model: gpt-5 }
- { provider: claude }
stages:
- id: a
instruction: "x"
"#;
let flow = Flow::from_yaml(yaml).expect("parse failed");
assert_eq!(flow.on_rate_limit.len(), 2);
assert_eq!(flow.on_rate_limit[0].provider, "codex");
assert_eq!(flow.on_rate_limit[0].model.as_deref(), Some("gpt-5"));
assert_eq!(flow.on_rate_limit[1].provider, "claude");
assert!(flow.on_rate_limit[1].model.is_none());
}
#[test]
fn test_flow_validation_rejects_unknown_providers() {
let stage_provider = r#"
name: bad-stage-provider
initial_movement: a
stages:
- id: a
provider: madeup
instruction: "x"
"#;
assert!(
Flow::from_yaml(stage_provider)
.expect_err("unknown stage provider must fail validation")
.to_string()
.contains("unknown provider")
);
let promotion_provider = r#"
name: bad-promotion-provider
initial_movement: a
stages:
- id: a
instruction: "x"
promotion:
- { at: 2, provider: madeup }
"#;
assert!(
Flow::from_yaml(promotion_provider)
.expect_err("unknown promotion provider must fail validation")
.to_string()
.contains("unknown promotion provider")
);
let fallback_provider = r#"
name: bad-fallback-provider
initial_movement: a
on_rate_limit:
- { provider: madeup }
stages:
- id: a
instruction: "x"
"#;
assert!(
Flow::from_yaml(fallback_provider)
.expect_err("unknown fallback provider must fail validation")
.to_string()
.contains("unknown on_rate_limit provider")
);
}
#[tokio::test]
async fn test_command_gates_pass_returns_none() {
let gates = vec![CommandGate {
name: "noop".to_string(),
command: "true".to_string(),
timeout_secs: 30,
}];
let result = run_command_gates(&gates, std::path::Path::new("/tmp")).await;
assert!(result.is_none());
}
#[tokio::test]
async fn test_command_gates_failure_returns_bounded_feedback() {
let gates = vec![
CommandGate {
name: "ok".to_string(),
command: "true".to_string(),
timeout_secs: 30,
},
CommandGate {
name: "boom".to_string(),
command: "echo broken output; echo to stderr 1>&2; exit 3".to_string(),
timeout_secs: 30,
},
];
let (name, feedback) = run_command_gates(&gates, std::path::Path::new("/tmp"))
.await
.expect("second gate fails");
assert_eq!(name, "boom");
assert!(feedback.contains("# Gate failure: boom (exit code 3)"));
assert!(feedback.contains("broken output"));
assert!(feedback.contains("to stderr"));
}
#[tokio::test]
async fn test_command_gates_truncate_long_output() {
let gates = vec![CommandGate {
name: "noisy".to_string(),
command: "yes x | head -c 5000; exit 1".to_string(),
timeout_secs: 30,
}];
let (_, feedback) = run_command_gates(&gates, std::path::Path::new("/tmp"))
.await
.expect("gate fails");
assert!(feedback.contains("[truncated]"));
// 5000 chars of stdout must have been bounded to ~1000.
assert!(
feedback.len() < 2500,
"feedback too long: {}",
feedback.len()
);
}
#[tokio::test]
async fn test_command_gates_timeout_is_reported() {
let gates = vec![CommandGate {
name: "slow".to_string(),
command: "sleep 5".to_string(),
timeout_secs: 1,
}];
let (name, feedback) = run_command_gates(&gates, std::path::Path::new("/tmp"))
.await
.expect("gate times out");
assert_eq!(name, "slow");
assert!(feedback.contains("timed out after 1s"));
}
#[test]
fn test_gates_parse_from_yaml_with_default_timeout() {
let yaml = r#"
name: gated
initial_movement: build
stages:
- id: build
instruction: "implement"
gates:
- { name: build, command: "cargo build" }
- { name: lint, command: "cargo clippy", timeout_secs: 120 }
"#;
let flow = Flow::from_yaml(yaml).expect("parse failed");
let gates = &flow.stages[0].gates;
assert_eq!(gates.len(), 2);
assert_eq!(gates[0].timeout_secs, 300, "default timeout");
assert_eq!(gates[1].timeout_secs, 120);
}
#[test]
fn test_team_leader_parses_with_defaults_and_legacy_yaml_unaffected() {
let yaml = r#"
name: tl
initial_movement: build
stages:
- id: build
instruction: "implement {task}"
team_leader:
part_persona: coder
"#;
let flow = Flow::from_yaml(yaml).expect("parse failed");
let spec = flow.stages[0].team_leader.as_ref().expect("spec");
assert_eq!(spec.max_parts, 3, "default max_parts");
assert_eq!(spec.part_persona.as_deref(), Some("coder"));
// Legacy YAML without the field still parses.
let legacy = r#"
name: legacy
initial_movement: a
stages:
- id: a
instruction: "x"
"#;
let flow = Flow::from_yaml(legacy).expect("legacy parse failed");
assert!(flow.stages[0].team_leader.is_none());
}
#[test]
fn test_team_leader_validate_rejects_parallel_and_call_combos() {
let with_parallel = r#"
name: bad1
initial_movement: a
stages:
- id: a
instruction: "x"
parallel: true
sub_movements: [b]
team_leader: {}
- id: b
instruction: "y"
"#;
assert!(
Flow::from_yaml(with_parallel)
.expect_err("must reject")
.to_string()
.contains("team_leader with parallel")
);
let with_call = r#"
name: bad2
initial_movement: a
stages:
- id: a
instruction: "x"
team_leader: {}
call:
flow: other
"#;
assert!(
Flow::from_yaml(with_call)
.expect_err("must reject")
.to_string()
.contains("team_leader with call")
);
let zero_parts = r#"
name: bad3
initial_movement: a
stages:
- id: a
instruction: "x"
team_leader:
max_parts: 0
"#;
assert!(
Flow::from_yaml(zero_parts)
.expect_err("must reject")
.to_string()
.contains("max_parts")
);
}
#[test]
fn test_sangha_parses_and_rejects_ambiguous_modes() {
let yaml = r#"
name: sangha-test
initial_movement: decide
stages:
- id: decide
instruction: "Review the task"
sangha:
quorum: 2
members:
- { id: planner, persona: planner }
- { id: reviewer, persona: reviewer }
"#;
let flow = Flow::from_yaml(yaml).expect("sangha flow should parse");
let spec = flow.stages[0].sangha.as_ref().expect("sangha spec");
assert_eq!(spec.quorum, 2);
assert_eq!(spec.members.len(), 2);
let bad = r#"
name: bad-sangha
initial_movement: decide
stages:
- id: decide
instruction: "Review the task"
parallel: true
sangha: {}
"#;
assert!(
Flow::from_yaml(bad)
.expect_err("sangha must reject ambiguous execution modes")
.to_string()
.contains("sangha with parallel")
);
}
#[test]
fn default_flow_uses_sangha_consensus() {
let default = builtin_flows()
.into_iter()
.find(|flow| flow.name == "default")
.expect("default flow should exist");
let stage_ids = default
.stages
.iter()
.map(|stage| stage.id.as_str())
.collect::<Vec<_>>();
assert!(stage_ids.contains(&"sangha"));
assert!(
default
.stages
.iter()
.any(|stage| stage.id == "sangha" && stage.sangha.is_some())
);
}
#[test]
fn cli_model_override_wins_for_all_stage_execution() {
let flow = Flow::from_yaml(
r#"
name: model-test
initial_movement: build
stages:
- id: build
provider: codex
model: stage-model
instruction: "Build"
promotion:
- at: 1
model: promoted-model
"#,
)
.expect("flow should parse");
let mut state = flow.create_state();
state
.variables
.insert("__visit_count".to_string(), serde_json::json!(1));
let stage = flow.stages[0].clone();
let mut engine = FlowEngine::new();
engine.set_model_override("cli-model");
engine.set_worktree_name("ccswarm-test-run");
let (_, model) = engine.resolve_effective_provider(&stage, &state);
assert_eq!(model.as_deref(), Some("cli-model"));
assert_eq!(engine.worktree_name.as_deref(), Some("ccswarm-test-run"));
}
#[tokio::test]
async fn test_team_leader_offline_aggregates_parallel_shape() {
// Without a bridge, the leader's "output" is the prompt echo (no JSON
// array with parts)… the no-bridge path returns {"instruction", "prompt"}
// with no "output" key, so decomposition fails twice and degrades to a
// single worker — exercising the graceful-degradation path end-to-end.
let yaml = r#"
name: tl-offline
initial_movement: build
stages:
- id: build
instruction: "implement the thing"
team_leader:
max_parts: 2
rules:
- condition:
all: ["completed"]
next: done
- id: done
instruction: "terminal"
"#;
let flow = Flow::from_yaml(yaml).expect("parse failed");
let mut engine = FlowEngine::new();
engine.flows.insert("tl-offline".to_string(), flow);
let state = engine
.execute_piece("tl-offline")
.await
.expect("execution failed");
assert_eq!(state.status, FlowStatus::Completed);
// The team_leader stage output must carry the parallel shape with the
// degraded single worker, and all("completed") must route to done.
let visited: Vec<&str> = state.history.iter().map(|t| t.to.as_str()).collect();
assert_eq!(visited, vec!["done"]);
let build_output = state
.history
.first()
.and_then(|t| t.output.as_ref())
.expect("build output");
assert_eq!(build_output.get("parallel"), Some(&serde_json::json!(true)));
let agents = build_output
.get("agents")
.and_then(|v| v.as_object())
.expect("agents map");
assert_eq!(agents.len(), 1, "degraded to a single worker");
assert!(agents.contains_key("build-worker"));
}
#[test]
fn test_builtin_flows_roundtrip_through_yaml() {
// `flow check <builtin>` serializes a builtin flow to YAML and
// re-parses it; every builtin must survive the round trip.
for flow in builtin_flows() {
let yaml = serde_yml::to_string(&flow).expect("serialize");
if let Err(e) = Flow::from_yaml(&yaml) {
panic!(
"builtin flow '{}' failed YAML round-trip: {:#}\n--- yaml ---\n{}",
flow.name, e, yaml
);
}
}
}
#[test]
fn test_max_stage_visits_defaults_to_three() {
let yaml = r#"
name: defaults
initial_movement: a
stages:
- id: a
instruction: "x"
"#;
let flow = Flow::from_yaml(yaml).expect("parse failed");
assert_eq!(flow.max_stage_visits, 3);
}
/// `readonly` stages without an explicit `tools:` list must still restrict the
/// provider via the permission level — otherwise the provider CLI falls back to
/// its permissive default and can write/exec despite `permission: readonly`.
#[test]
fn test_readonly_stage_resolves_to_readonly_tools() {
use super::super::permissions::PermissionEnforcer;
let yaml = r#"
name: perm-test
initial_movement: review
stages:
- id: review
instruction: "Read-only review"
permission: readonly
"#;
let flow = Flow::from_yaml(yaml).expect("parse failed");
let stage = &flow.stages[0];
assert!(stage.tools.is_empty(), "precondition: no explicit tools");
let resolved = PermissionEnforcer::from_movement(stage.permission.clone(), &stage.tools)
.available_tools();
assert!(resolved.contains(&"read".to_string()));
assert!(resolved.contains(&"grep".to_string()));
assert!(
!resolved
.iter()
.any(|t| t == "bash" || t == "write" || t == "edit"),
"readonly must not expose edit/exec tools, got: {:?}",
resolved,
);
}
/// `set_run_token_cap` installs the cap on the engine. Regression guard
/// so the public setter keeps working for the CLI `--run-budget-tokens`
/// path that plumbs into this field.
#[test]
fn test_run_token_cap_setter() {
let mut engine = FlowEngine::new();
assert_eq!(engine.run_token_cap, None);
engine.set_run_token_cap(5000);
assert_eq!(engine.run_token_cap, Some(5000));
}
/// The in-loop abort condition: cumulative strictly greater than the cap.
/// Keeping this as a standalone assertion so the cap semantics (`>`, not `>=`)
/// are pinned even if the enforcement site gets refactored.
#[test]
fn test_run_token_cap_abort_condition() {
let exceeds_cap = |used: u64, cap: u64| used > cap;
// strictly greater than the cap — abort
assert!(exceeds_cap(10, 5));
// equal — do NOT abort (the stage that lands exactly on the cap still
// gets its usage recorded and the next stage is allowed to start; a
// stricter check would surprise users who set the cap to the exact
// expected total).
assert!(!exceeds_cap(5, 5));
}
#[test]
fn build_prompt_does_not_duplicate_persona_or_expanded_task() {
let yaml = r#"
name: prompt-test
initial_movement: plan
stages:
- id: plan
persona: coder
instruction: "Plan this task: {task}"
"#;
let flow = Flow::from_yaml(yaml).expect("parse failed");
let stage = &flow.stages[0];
let mut state = flow.create_state();
state.variables.insert(
"task".to_string(),
serde_json::Value::String("Fix duplicated prompt content".to_string()),
);
let engine = FlowEngine::new();
let prompt = engine.build_movement_prompt(stage, &state);
assert!(
!prompt.contains("You are the implementer"),
"persona system prompt must be passed via ProviderOptions, not duplicated in the user prompt"
);
assert_eq!(
prompt.matches("Fix duplicated prompt content").count(),
1,
"task text should appear once when instruction already expands {{task}}"
);
}
/// An explicit `tools:` list on a stage is honored verbatim, even if the
/// permission level would allow a broader set.
#[test]
fn test_explicit_tools_override_permission_defaults() {
use super::super::permissions::PermissionEnforcer;
let yaml = r#"
name: perm-test
initial_movement: narrow
stages:
- id: narrow
instruction: "Narrow tools"
permission: full
tools: [read, grep]
"#;
let flow = Flow::from_yaml(yaml).expect("parse failed");
let stage = &flow.stages[0];
let resolved = PermissionEnforcer::from_movement(stage.permission.clone(), &stage.tools)
.available_tools();
let mut sorted = resolved.clone();
sorted.sort();
assert_eq!(sorted, vec!["grep".to_string(), "read".to_string()]);
}
#[test]
fn is_safe_report_name_rejects_traversal_and_separators() {
assert!(super::is_safe_report_name("plan.md"));
assert!(super::is_safe_report_name("test-report.json"));
assert!(super::is_safe_report_name("a_b_c.txt"));
assert!(!super::is_safe_report_name(""));
assert!(!super::is_safe_report_name("."));
assert!(!super::is_safe_report_name(".."));
assert!(!super::is_safe_report_name("a/b"));
assert!(!super::is_safe_report_name("a\\b"));
assert!(!super::is_safe_report_name("../etc/passwd"));
assert!(!super::is_safe_report_name("a..b"));
assert!(!super::is_safe_report_name(&"a".repeat(200)));
}
#[test]
fn expand_template_resolves_report_reference_from_disk() {
use std::collections::HashMap;
use tempfile::TempDir;
let tmp = TempDir::new().expect("tempdir");
let prev = std::env::current_dir().expect("cwd");
std::env::set_current_dir(tmp.path()).expect("chdir");
let reports_dir = std::path::Path::new(".ccswarm")
.join("runs")
.join("test-run-1")
.join("reports");
std::fs::create_dir_all(&reports_dir).expect("create reports dir");
std::fs::write(reports_dir.join("plan.md"), "PLAN_BODY").expect("write report");
let mut vars: HashMap<String, serde_json::Value> = HashMap::new();
vars.insert(
"__run_id".to_string(),
serde_json::Value::String("test-run-1".to_string()),
);
let out = super::expand_template("Plan was: {report:plan.md}", &vars);
// Restore CWD before asserting so a failure doesn't leak directory state.
std::env::set_current_dir(prev).expect("restore cwd");
assert_eq!(out, "Plan was: PLAN_BODY");
}
#[test]
fn expand_template_unsafe_report_name_expands_to_empty() {
use std::collections::HashMap;
let mut vars: HashMap<String, serde_json::Value> = HashMap::new();
vars.insert(
"__run_id".to_string(),
serde_json::Value::String("test-run-2".to_string()),
);
let out = super::expand_template("escape: {report:../passwd}", &vars);
assert_eq!(out, "escape: ");
}
#[tokio::test]
async fn workflow_call_unknown_flow_fails_fast() {
let engine = FlowEngine::new();
let parent_state = FlowState {
flow_name: "parent".to_string(),
current_movement: "call-stage".to_string(),
movement_count: 0,
history: vec![],
variables: std::collections::HashMap::new(),
status: FlowStatus::Running,
started_at: Utc::now(),
completed_at: None,
};
let stage = Stage {
id: "call-stage".to_string(),
persona: None,
policy: None,
knowledge: None,
provider: None,
model: None,
instruction: String::new(),
tools: vec![],
permission: MovementPermission::Readonly,
rules: vec![],
parallel: false,
sub_movements: vec![],
output_contract: None,
timeout: None,
max_retries: 0,
agent: None,
working_dir: None,
retry_delay_ms: 0,
pass_previous_response: true,
call: Some(WorkflowCallSpec {
flow: "no-such-flow".to_string(),
args: std::collections::HashMap::new(),
}),
promotion: Vec::new(),
gates: Vec::new(),
team_leader: None,
sangha: None,
};
let call = stage.call.clone().expect("call");
let err = engine
.execute_workflow_call(&stage, &call, &parent_state)
.await
.expect_err("unknown flow must error");
let msg = err.to_string();
assert!(
msg.contains("no-such-flow"),
"error should name the missing flow, got: {msg}"
);
}
#[tokio::test]
async fn evaluate_rules_drops_interactive_only_in_pipeline_mode() {
let engine = FlowEngine::new(); // interactive defaults to false
let rules = vec![
MovementRule {
condition: RuleCondition::Simple("review".to_string()),
next: "ask-human".to_string(),
priority: 0,
interactive_only: true,
requires_user_input: false,
},
MovementRule {
condition: RuleCondition::Simple("review".to_string()),
next: "complete".to_string(),
priority: 0,
interactive_only: false,
requires_user_input: false,
},
];
let state = FlowState {
flow_name: "t".to_string(),
current_movement: "start".to_string(),
movement_count: 0,
status: FlowStatus::Running,
started_at: Utc::now(),
completed_at: None,
history: vec![],
variables: std::collections::HashMap::new(),
};
let output = serde_json::json!({"output": "review"});
let next = engine
.evaluate_rules(&rules, &output, &state)
.await
.expect("evaluate");
// In pipeline mode the interactive_only rule is filtered out, so the
// fallback "complete" rule wins.
assert_eq!(next.as_deref(), Some("complete"));
}
#[tokio::test]
async fn evaluate_rules_keeps_interactive_only_in_interactive_mode() {
let mut engine = FlowEngine::new();
engine.set_interactive(true);
let rules = vec![MovementRule {
condition: RuleCondition::Simple("review".to_string()),
next: "ask-human".to_string(),
priority: 0,
interactive_only: true,
requires_user_input: false,
}];
let state = FlowState {
flow_name: "t".to_string(),
current_movement: "start".to_string(),
movement_count: 0,
status: FlowStatus::Running,
started_at: Utc::now(),
completed_at: None,
history: vec![],
variables: std::collections::HashMap::new(),
};
let output = serde_json::json!({"output": "review"});
let next = engine
.evaluate_rules(&rules, &output, &state)
.await
.expect("evaluate");
assert_eq!(next.as_deref(), Some("ask-human"));
}
#[test]
fn expand_template_skips_internal_run_id_variable() {
use std::collections::HashMap;
let mut vars: HashMap<String, serde_json::Value> = HashMap::new();
vars.insert(
"__run_id".to_string(),
serde_json::Value::String("secret-run".to_string()),
);
let out = super::expand_template("run is {__run_id}", &vars);
assert_eq!(out, "run is {__run_id}");
}
#[test]
fn truncate_for_context_is_utf8_safe() {
let input = "abあcd";
assert_eq!(super::truncate_for_context(input, 4), "ab... [truncated]");
assert_eq!(super::truncate_for_context(input, 5), "abあ... [truncated]");
assert_eq!(super::truncate_for_context(input, 99), input);
}
#[test]
fn stage_output_succeeded_only_accepts_completed_status() {
assert!(super::stage_output_succeeded(&serde_json::json!({
"status": "completed"
})));
assert!(!super::stage_output_succeeded(&serde_json::json!({
"status": "failed"
})));
assert!(!super::stage_output_succeeded(&serde_json::json!({
"status": "timeout"
})));
assert!(!super::stage_output_succeeded(&serde_json::json!({})));
}
}