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
//! Common AI command runner layer for unified stagger state management.
//!
//! This module provides a shared execution layer for all AI-driven commands
//! (apply, archive, resolve, analyze) to ensure consistent stagger delays
//! across every execution frontend.
use crate::command_queue::{CommandQueue, CommandQueueConfig};
use crate::config::OrchestratorConfig;
use crate::error::{OrchestratorError, Result};
use crate::process_manager::{
cleanup_process_group_verified, CommandTermination, ManagedChild, ProcessGroupCleanupReport,
StreamingChildHandle, DEFAULT_PROCESS_GROUP_CLEANUP_TIMEOUT_MS,
DEFAULT_PROCESS_GROUP_SIGTERM_GRACE_MS,
};
use crate::stream_json_textifier::{process_stdout_line, StreamJsonTextBuffer};
use std::collections::HashMap;
use std::path::Path;
use std::process::Stdio;
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::io::{AsyncBufReadExt, BufReader};
use tokio::process::Command;
use tokio::sync::mpsc;
use tokio::sync::Mutex;
use tracing::{debug, error, warn};
/// Shared stagger state type: Arc<Mutex<Option<Instant>>>
/// This type is shared across all AI command executions to coordinate stagger delays
pub type SharedStaggerState = Arc<Mutex<Option<Instant>>>;
// ---------------------------------------------------------------------------
// Run command scope
// ---------------------------------------------------------------------------
/// Bounded budget for proving that every run-owned AI command reached
/// quiescence after shutdown started.
///
/// It exceeds one command's SIGTERM grace plus process-group verification path
/// and assumes active cleanups run concurrently, so together with the existing
/// 90-second pending merge/base-lane drain it stays inside the scheduler's
/// 120-second outer cancellation boundary instead of adding a new layer on top
/// of it.
pub const RUN_COMMAND_CLEANUP_DEADLINE: Duration = Duration::from_secs(30);
/// SIGTERM grace used when the scope itself force-cleans a retained identity.
const SCOPE_ESCALATION_SIGTERM_GRACE_MS: u64 = 500;
/// Total budget for one retained-identity managed escalation sweep.
const SCOPE_ESCALATION_TOTAL_MS: u64 = 5_000;
/// Bound on proving that one targeted force-stop's SIGKILL emptied the group.
///
/// It bounds a *proof*, not a grace window: nothing is given time to shut down
/// cooperatively, and the budget only limits how long membership is polled
/// before the result is reported as unconfirmed.
pub const FORCE_STOP_CHANGE_KILL_BUDGET: Duration = Duration::from_secs(5);
/// Evidence from one targeted force-stop of a single change's process groups.
///
/// Every field describes only the addressed change: an unrelated change's
/// processes are not reachable from the path that produces this, so a report
/// can never account for one.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ChangeForceStopReport {
/// Owned process identities this force-stop signalled.
pub identities: usize,
/// Identities proven empty after SIGKILL.
pub confirmed: usize,
/// One bounded diagnostic per identity whose emptiness was not proven.
pub unconfirmed: Vec<String>,
}
impl ChangeForceStopReport {
/// Whether every signalled identity was proven reaped.
///
/// A target that owned no process at all is confirmed: there was nothing to
/// terminate, which is exactly the dequeue-only case.
pub fn is_confirmed(&self) -> bool {
self.unconfirmed.is_empty()
}
/// One bounded operator-facing summary of what this force-stop proved.
pub fn diagnostics(&self) -> String {
if self.is_confirmed() {
return format!(
"targeted force-stop confirmed (identities={}, killed={})",
self.identities, self.confirmed
);
}
format!(
"targeted force-stop unconfirmed (identities={}, killed={}): {}",
self.identities,
self.confirmed,
self.unconfirmed.join("; ")
)
}
}
/// Lifecycle of one run-owned AI command execution inside its scope.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ExecutionPhase {
/// Admission is reserved; the runner task has not spawned a process yet.
WaitingToSpawn,
/// A real owned process group exists for the current attempt.
Running,
/// Termination and verification are in progress; the identity stays registered.
Cleaning,
/// The runner task ended without proving quiescence. The identity is kept
/// for bounded managed escalation and diagnostics.
UnconfirmedRetained,
}
impl ExecutionPhase {
/// Whether the runner task behind this registration is still running.
fn is_active(self) -> bool {
!matches!(self, Self::UnconfirmedRetained)
}
}
/// One run-owned execution tracked by the scope.
#[derive(Debug, Clone)]
struct ScopeEntry {
operation: Option<String>,
change_id: Option<String>,
phase: ExecutionPhase,
/// Owned process identities (PGID == leader PID) whose quiescence is not
/// yet proven. Retained across attempts so an earlier unproven attempt
/// cannot be forgotten by a later one.
unproven_pids: Vec<u32>,
/// Last actionable cleanup diagnostic recorded for this execution.
detail: Option<String>,
}
impl ScopeEntry {
fn describe(&self) -> String {
format!(
"op={}, change_id={}, pgids={:?}: {}",
self.operation.as_deref().unwrap_or("unknown"),
self.change_id.as_deref().unwrap_or("none"),
self.unproven_pids,
self.detail
.as_deref()
.unwrap_or("owned process-set quiescence was never proven")
)
}
}
#[derive(Debug, Default)]
struct ScopeState {
/// Once closed, no new execution and no new process spawn is admitted.
closed: bool,
next_id: u64,
entries: std::collections::BTreeMap<u64, ScopeEntry>,
}
struct RunCommandScopeInner {
state: std::sync::Mutex<ScopeState>,
cancel: tokio_util::sync::CancellationToken,
quiescence: tokio::sync::Notify,
}
/// Ephemeral, clone-shared ownership of every AI command one orchestration
/// invocation launches.
///
/// The scope is the missing layer above `StreamingChildHandle`: it closes final
/// spawn admission atomically, notifies runner tasks directly, and retains each
/// execution and owned process identity until the runner task ended *and*
/// typed cleanup evidence confirmed quiescence. It is process-local, is
/// recreated for every run, and is never persisted or used for restart routing.
#[derive(Clone)]
pub struct RunCommandScope {
inner: Arc<RunCommandScopeInner>,
}
impl Default for RunCommandScope {
fn default() -> Self {
Self::new()
}
}
impl RunCommandScope {
/// Create one fresh, open scope for a single orchestration invocation.
pub fn new() -> Self {
Self {
inner: Arc::new(RunCommandScopeInner {
state: std::sync::Mutex::new(ScopeState::default()),
cancel: tokio_util::sync::CancellationToken::new(),
quiescence: tokio::sync::Notify::new(),
}),
}
}
fn lock(&self) -> std::sync::MutexGuard<'_, ScopeState> {
self.inner
.state
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
}
/// Cancellation observed directly by runner tasks.
///
/// Runner shutdown never depends on a caller-held `StreamingChildHandle`:
/// dropping the handle cannot silence this token.
pub fn cancel_token(&self) -> tokio_util::sync::CancellationToken {
self.inner.cancel.clone()
}
/// Whether admission is already closed.
pub fn is_closed(&self) -> bool {
self.lock().closed
}
/// Whether both handles are clones of the *same* scope.
///
/// Scope identity is what the run's cleanup barrier is built on: a runner
/// carrying an equal-but-separate scope reports into a barrier nobody waits
/// on.
#[allow(dead_code)] // Read by scope-ownership coverage, not by the binary.
pub fn is_same(&self, other: &RunCommandScope) -> bool {
Arc::ptr_eq(&self.inner, &other.inner)
}
/// Atomically close final spawn admission and broadcast runner shutdown.
///
/// Idempotent. A command parked in stagger or retry delay cannot reach
/// `Command::spawn` after this returns, because the admission check and the
/// spawn share this lock.
pub fn close(&self) {
{
let mut state = self.lock();
state.closed = true;
}
self.inner.cancel.cancel();
self.inner.quiescence.notify_waiters();
}
/// Close this scope as soon as `token` is cancelled.
///
/// The scope observes the run's global token directly, so a caller blocked
/// on command output far from the scheduler loop still starts cleaning up
/// the instant cancellation happens.
pub fn link_cancellation(&self, token: tokio_util::sync::CancellationToken) {
let scope = self.clone();
tokio::spawn(async move {
token.cancelled().await;
scope.close();
});
}
/// Number of registrations whose runner task has not ended yet.
pub fn active_executions(&self) -> usize {
self.lock()
.entries
.values()
.filter(|entry| entry.phase.is_active())
.count()
}
/// Owned process identities the scope still cannot prove quiescent.
#[allow(dead_code)] // Consumed by TUI escalation coverage and heavy regressions.
pub fn retained_process_ids(&self) -> Vec<u32> {
let state = self.lock();
let mut pids: Vec<u32> = state
.entries
.values()
.flat_map(|entry| entry.unproven_pids.iter().copied())
.collect();
pids.sort_unstable();
pids.dedup();
pids
}
/// Whether every run-owned command for `change_id` reached terminal cleanup.
///
/// A change with no registration at all is quiescent: it owns no command.
/// A change with a live or retained registration is not, and its execution
/// `done` handshake must stay unfired.
pub fn change_is_quiescent(&self, change_id: &str) -> bool {
!self
.lock()
.entries
.values()
.any(|entry| entry.change_id.as_deref() == Some(change_id))
}
/// Whether this scope owns a live managed process group for `change_id`.
///
/// The eligibility fact a targeted force-stop needs, read from the same
/// ownership graph the kill itself walks: a change with a registration but
/// no spawned identity is admitted work with nothing to signal, and a change
/// with no registration owns no command at all.
pub fn change_owns_managed_process(&self, change_id: &str) -> bool {
self.lock().entries.values().any(|entry| {
entry.change_id.as_deref() == Some(change_id) && !entry.unproven_pids.is_empty()
})
}
/// Immediately SIGKILL every process group this scope owns for `change_id`
/// and prove each one was reaped.
///
/// Target-scoped by construction: the entry filter is the *only* way a PGID
/// reaches the signal, so an unrelated change's process group is
/// unreachable from here — there is no PID lookup, no "kill everything
/// retained", and no scope closure. Admission stays open and every other
/// registration keeps running, which is what separates this from
/// [`Self::force_cleanup_retained`].
///
/// No SIGTERM is sent. `budget` bounds the proof of quiescence, not a grace
/// window, and an identity whose emptiness cannot be proven is reported as
/// unconfirmed rather than dropped from the ownership graph.
pub async fn force_stop_change(
&self,
change_id: &str,
budget: Duration,
) -> ChangeForceStopReport {
let targets: Vec<(u64, ScopeEntry)> = {
let state = self.lock();
state
.entries
.iter()
.filter(|(_, entry)| entry.change_id.as_deref() == Some(change_id))
.filter(|(_, entry)| !entry.unproven_pids.is_empty())
.map(|(id, entry)| (*id, entry.clone()))
.collect()
};
let per_identity_ms = budget.as_millis().min(u64::MAX as u128) as u64;
let mut report = ChangeForceStopReport::default();
for (id, entry) in targets {
let mut proven = Vec::new();
for pid in &entry.unproven_pids {
report.identities += 1;
let evidence = crate::process_manager::kill_process_group_immediately(
*pid,
per_identity_ms,
entry.operation.as_deref(),
entry.change_id.as_deref(),
)
.await;
if evidence.is_confirmed() {
report.confirmed += 1;
proven.push(*pid);
} else {
report
.unconfirmed
.push(format!("pgid={pid}: {}", evidence.diagnostics()));
}
}
let mut state = self.lock();
if let Some(current) = state.entries.get_mut(&id) {
current.unproven_pids.retain(|pid| !proven.contains(pid));
if current.unproven_pids.is_empty() && !current.phase.is_active() {
state.entries.remove(&id);
}
}
drop(state);
self.inner.quiescence.notify_waiters();
}
report
}
/// Reserve one execution before its runner task is spawned.
///
/// Returns `None` when the scope is already closing, which is what refuses
/// a command that raced shutdown before anything was launched.
fn register(&self, operation: Option<&str>, change_id: Option<&str>) -> Option<ScopeExecution> {
let mut state = self.lock();
if state.closed {
return None;
}
state.next_id += 1;
let id = state.next_id;
state.entries.insert(
id,
ScopeEntry {
operation: operation.map(|s| s.to_string()),
change_id: change_id.map(|s| s.to_string()),
phase: ExecutionPhase::WaitingToSpawn,
unproven_pids: Vec::new(),
detail: None,
},
);
drop(state);
Some(ScopeExecution {
scope: self.clone(),
id,
finished: std::sync::atomic::AtomicBool::new(false),
})
}
fn set_phase(&self, id: u64, phase: ExecutionPhase) {
let mut state = self.lock();
if let Some(entry) = state.entries.get_mut(&id) {
entry.phase = phase;
}
drop(state);
self.inner.quiescence.notify_waiters();
}
fn remove_entry(&self, id: u64) {
let mut state = self.lock();
state.entries.remove(&id);
drop(state);
self.inner.quiescence.notify_waiters();
}
/// Close the barrier on this scope and wait for bounded quiescence.
pub async fn shutdown(&self, deadline: Duration) -> RunCommandScopeCleanup {
self.close();
self.wait_quiescent(deadline).await
}
/// Await runner-task exit for every registration, then escalate whatever
/// could not be proven quiescent, all inside one absolute `deadline`.
pub async fn wait_quiescent(&self, deadline: Duration) -> RunCommandScopeCleanup {
let started = Instant::now();
let mut timed_out = false;
loop {
if self.active_executions() == 0 {
break;
}
let elapsed = started.elapsed();
if elapsed >= deadline {
timed_out = true;
break;
}
// Register interest before re-reading so a completion that lands
// between the check and the wait cannot be missed.
let notified = self.inner.quiescence.notified();
if self.active_executions() == 0 {
break;
}
if tokio::time::timeout(deadline - elapsed, notified)
.await
.is_err()
{
timed_out = true;
break;
}
}
let remaining = deadline.saturating_sub(started.elapsed());
self.escalate_retained(remaining, timed_out).await
}
/// Force-clean and verify every retained owned identity.
///
/// Used by the local TUI supervisor when the orchestrator task itself
/// stopped cooperating: the scope is retained outside that task, so it is
/// still the path to the PGIDs the run owns.
pub async fn force_cleanup_retained(&self, budget: Duration) -> RunCommandScopeCleanup {
self.close();
self.escalate_retained(budget, self.active_executions() > 0)
.await
}
async fn escalate_retained(&self, budget: Duration, timed_out: bool) -> RunCommandScopeCleanup {
let pending: Vec<(u64, ScopeEntry)> = {
let state = self.lock();
state
.entries
.iter()
.filter(|(_, entry)| !entry.unproven_pids.is_empty())
.map(|(id, entry)| (*id, entry.clone()))
.collect()
};
let per_identity_ms = if budget.is_zero() {
0
} else {
SCOPE_ESCALATION_TOTAL_MS.min(budget.as_millis() as u64)
};
let mut escalated = 0usize;
for (id, entry) in pending {
// Per entry: only this entry's own confirmed identities may be
// dropped from its list.
let mut proven = Vec::new();
let mut first_failure = None;
for pid in &entry.unproven_pids {
let report = crate::process_manager::cleanup_process_group_verified(
*pid,
SCOPE_ESCALATION_SIGTERM_GRACE_MS.min(per_identity_ms),
per_identity_ms,
entry.operation.as_deref(),
entry.change_id.as_deref(),
)
.await;
if report.is_confirmed() {
proven.push(*pid);
} else if first_failure.is_none() {
first_failure = Some(report.diagnostics());
}
}
escalated += proven.len();
let mut state = self.lock();
if let Some(current) = state.entries.get_mut(&id) {
current.unproven_pids.retain(|pid| !proven.contains(pid));
if let Some(detail) = first_failure {
current.detail = Some(detail);
}
if current.unproven_pids.is_empty() && !current.phase.is_active() {
state.entries.remove(&id);
}
}
}
let state = self.lock();
let unconfirmed: Vec<String> = state.entries.values().map(ScopeEntry::describe).collect();
drop(state);
RunCommandScopeCleanup {
escalated,
unconfirmed,
timed_out,
}
}
}
/// Result of one bounded run command scope cleanup barrier.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RunCommandScopeCleanup {
/// Owned identities that only managed escalation could prove quiescent.
pub escalated: usize,
/// One bounded actionable diagnostic per unproven registration.
pub unconfirmed: Vec<String>,
/// Whether the bounded barrier expired before every runner task ended.
pub timed_out: bool,
}
impl RunCommandScopeCleanup {
/// Whether the run may treat every owned command as quiescent.
pub fn is_quiescent(&self) -> bool {
self.unconfirmed.is_empty() && !self.timed_out
}
/// One bounded operator-facing summary of what could not be proven.
pub fn diagnostics(&self) -> String {
if self.is_quiescent() {
return format!(
"run command cleanup confirmed (escalated={})",
self.escalated
);
}
format!(
"run command cleanup unconfirmed (timed_out={}, escalated={}): {}",
self.timed_out,
self.escalated,
self.unconfirmed.join("; ")
)
}
}
/// A run-owned execution reserved in a [`RunCommandScope`].
///
/// Held by the detached runner task for its whole life. Dropping it without
/// [`ScopeExecution::finish`] — a panicked task — leaves the registration
/// retained as unconfirmed rather than silently quiescent.
struct ScopeExecution {
scope: RunCommandScope,
id: u64,
finished: std::sync::atomic::AtomicBool,
}
impl ScopeExecution {
/// Whether scope shutdown has already started.
fn is_shutdown(&self) -> bool {
self.scope.is_closed()
}
fn cancel_token(&self) -> tokio_util::sync::CancellationToken {
self.scope.cancel_token()
}
fn mark_waiting_to_spawn(&self) {
self.scope
.set_phase(self.id, ExecutionPhase::WaitingToSpawn);
}
fn mark_cleaning(&self) {
self.scope.set_phase(self.id, ExecutionPhase::Cleaning);
}
/// Final admission serialized with scope shutdown.
///
/// The admission check and `spawn` share one critical section, so a scope
/// closed anywhere in between cannot leave a started process behind.
/// Returns `None` when admission was refused.
fn admit_spawn<F>(&self, spawn: F) -> Option<std::io::Result<tokio::process::Child>>
where
F: FnOnce() -> std::io::Result<tokio::process::Child>,
{
let mut state = self.scope.lock();
if state.closed {
return None;
}
let result = spawn();
if let Ok(child) = &result {
if let Some(entry) = state.entries.get_mut(&self.id) {
entry.phase = ExecutionPhase::Running;
if let Some(pid) = child.id() {
entry.unproven_pids.push(pid);
}
}
}
Some(result)
}
/// Record typed cleanup evidence for one owned identity.
fn record_cleanup(&self, pid: u32, report: &ProcessGroupCleanupReport) {
let mut state = self.scope.lock();
if let Some(entry) = state.entries.get_mut(&self.id) {
if report.is_confirmed() {
entry.unproven_pids.retain(|owned| *owned != pid);
} else {
entry.detail = Some(report.diagnostics());
}
}
drop(state);
self.scope.inner.quiescence.notify_waiters();
}
/// The runner task is ending. The registration disappears only when every
/// owned identity is already proven quiescent.
fn finish(&self) {
if self.finished.swap(true, Ordering::SeqCst) {
return;
}
let retained = {
let mut state = self.scope.lock();
match state.entries.get_mut(&self.id) {
Some(entry) if entry.unproven_pids.is_empty() => false,
Some(entry) => {
entry.phase = ExecutionPhase::UnconfirmedRetained;
true
}
None => false,
}
};
if retained {
self.scope.inner.quiescence.notify_waiters();
} else {
self.scope.remove_entry(self.id);
}
}
}
/// A scope registration created directly by in-crate tests.
///
/// Lets a caller hold the run's cleanup barrier open — and release it as either
/// confirmed or unproven — without spawning a real process.
#[cfg(test)]
pub(crate) struct ScopeTestRegistration(ScopeExecution);
#[cfg(test)]
impl ScopeTestRegistration {
/// Release as a runner task that ended with proven quiescence.
pub(crate) fn release_confirmed(self) {
self.0.finish();
}
}
#[cfg(test)]
impl RunCommandScope {
/// Reserve an execution the way `execute_streaming_with_retry` does.
pub(crate) fn register_for_test(
&self,
operation: &str,
change_id: Option<&str>,
) -> Option<ScopeTestRegistration> {
self.register(Some(operation), change_id)
.map(ScopeTestRegistration)
}
/// Reserve an execution that already owns an unproven process identity.
pub(crate) fn register_unproven_for_test(
&self,
operation: &str,
change_id: Option<&str>,
pid: u32,
) -> ScopeTestRegistration {
let execution = self
.register(Some(operation), change_id)
.expect("an open scope admits the registration");
{
let mut state = self.lock();
if let Some(entry) = state.entries.get_mut(&execution.id) {
entry.phase = ExecutionPhase::Running;
entry.unproven_pids.push(pid);
}
}
ScopeTestRegistration(execution)
}
}
impl Drop for ScopeExecution {
fn drop(&mut self) {
if self.finished.swap(true, Ordering::SeqCst) {
return;
}
// A runner task that never reached its own finalization proved nothing:
// retain the registration so the barrier still sees it.
let mut state = self.scope.lock();
if let Some(entry) = state.entries.get_mut(&self.id) {
entry.phase = ExecutionPhase::UnconfirmedRetained;
if entry.detail.is_none() {
entry.detail =
Some("the runner task ended without publishing cleanup evidence".to_string());
}
if entry.unproven_pids.is_empty() {
state.entries.remove(&self.id);
}
}
drop(state);
self.scope.inner.quiescence.notify_waiters();
}
}
/// Output line from a child process
#[derive(Debug, Clone)]
#[allow(dead_code)] // Infrastructure ready, integration pending (tasks 3.2, 3.3, 4.1-4.3)
pub enum OutputLine {
Stdout(String),
Stderr(String),
}
/// Common AI command runner with shared stagger state.
///
/// This runner wraps CommandQueue and provides streaming execution
/// for AI-driven commands (apply, archive, resolve, analyze).
/// The shared stagger state ensures consistent delays across all
/// parallel workspaces and command types.
#[derive(Clone)]
#[allow(dead_code)] // Infrastructure ready, integration pending (tasks 3.2, 3.3, 4.1-4.3)
pub struct AiCommandRunner {
command_queue: CommandQueue,
/// When true, stdout lines that are Claude Code stream-json (NDJSON) events are
/// converted to human-readable text before being emitted to the output channel.
stream_json_textify: bool,
/// When true, perform a strict post-completion SIGTERM→SIGKILL sweep on the spawned
/// process group after every command outcome (success, failure, cancellation, or
/// inactivity timeout) to prevent orphaned background processes.
strict_process_cleanup: bool,
/// Bounded budget for proving that the owned process group became quiescent
/// after termination was requested. Callers that gate repository work on
/// cleanup evidence fail when this budget expires without proof.
process_group_cleanup_timeout_ms: u64,
command_envs: HashMap<String, String>,
/// Invocation-scoped ownership of every command this runner launches.
///
/// `None` is the caller-owned lifecycle used outside a scheduler run (the
/// TUI's standalone worktree command and in-crate tests): such a command is
/// deliberately not attached to a later run's scope.
run_command_scope: Option<RunCommandScope>,
}
impl AiCommandRunner {
/// Create a new AiCommandRunner with shared stagger state.
///
/// Stream-JSON textification is enabled by default. Use
/// [`AiCommandRunner::set_stream_json_textify`] to override.
///
/// # Arguments
///
/// * `config` - CommandQueue configuration
/// * `shared_state` - Shared last execution timestamp for stagger coordination
pub fn new(config: CommandQueueConfig, shared_state: SharedStaggerState) -> Self {
Self {
command_queue: CommandQueue::new_with_shared_state(config, shared_state),
stream_json_textify: true,
strict_process_cleanup: true,
process_group_cleanup_timeout_ms: DEFAULT_PROCESS_GROUP_CLEANUP_TIMEOUT_MS,
command_envs: HashMap::new(),
run_command_scope: None,
}
}
/// Create a runner with every command-related setting from the orchestrator config.
pub fn from_orchestrator_config(
config: &OrchestratorConfig,
shared_state: SharedStaggerState,
) -> Self {
let mut runner = Self::new(CommandQueueConfig::from(config), shared_state);
runner.set_stream_json_textify(config.get_stream_json_textify());
runner.set_strict_process_cleanup(config.get_command_strict_process_cleanup());
runner.set_command_envs(config.get_command_envs());
runner
}
/// Create a run-owned runner: same settings, bound to `scope`.
///
/// Every production command surface of one orchestration invocation is
/// constructed through here (or cloned from a runner that was), so no run
/// path can build an unscoped runner out of a bare stagger timestamp.
pub fn for_run(
config: &OrchestratorConfig,
shared_state: SharedStaggerState,
scope: RunCommandScope,
) -> Self {
let mut runner = Self::from_orchestrator_config(config, shared_state);
runner.run_command_scope = Some(scope);
runner
}
/// The invocation scope this runner is bound to, if any.
#[allow(dead_code)] // Read by scope-ownership coverage, not by the binary.
pub fn run_command_scope(&self) -> Option<&RunCommandScope> {
self.run_command_scope.as_ref()
}
/// Bind (or rebind) this runner to an invocation scope.
pub fn set_run_command_scope(&mut self, scope: RunCommandScope) {
self.run_command_scope = Some(scope);
}
pub fn set_command_envs(&mut self, envs: HashMap<String, String>) {
self.command_envs = envs;
}
/// Override stream-JSON textification setting.
///
/// When `false`, raw stdout lines are forwarded unchanged (useful for troubleshooting).
pub fn set_stream_json_textify(&mut self, enabled: bool) {
self.stream_json_textify = enabled;
}
/// Override strict post-completion process-group cleanup setting.
///
/// When `false`, no SIGTERM/SIGKILL sweep is performed after a command completes
/// successfully. Cancellation and inactivity-timeout paths continue to clean up
/// regardless (they have independent termination logic). Set to `false` only for
/// debugging workflows where intentional background processes must outlive the command.
pub fn set_strict_process_cleanup(&mut self, enabled: bool) {
self.strict_process_cleanup = enabled;
}
/// Override the bounded budget for proving process-group quiescence.
///
/// A shorter budget makes an unconfirmed cleanup surface sooner; it never
/// makes an unproven group count as quiescent.
#[allow(dead_code)] // Exercised by the process-group barrier tests.
pub fn set_process_group_cleanup_timeout_ms(&mut self, timeout_ms: u64) {
self.process_group_cleanup_timeout_ms = timeout_ms;
}
/// Get access to the underlying CommandQueue configuration.
///
/// This is useful for implementing custom retry logic that respects
/// the configured retry parameters.
#[allow(dead_code)] // Used by parallel executor for retry logic
pub fn queue_config(&self) -> &crate::command_queue::CommandQueueConfig {
self.command_queue.config()
}
#[cfg(test)]
pub(crate) fn shared_stagger_state(&self) -> SharedStaggerState {
self.command_queue.shared_stagger_state()
}
/// Execute a command with streaming output, stagger delay, and automatic retry.
///
/// Returns a real process handle ([`StreamingChildHandle`]) that targets the actual
/// spawned command (or its process group) rather than a placeholder. Cancellation and
/// inactivity-timeout termination send SIGTERM/SIGKILL to the full process group, so
/// pipeline children (e.g. `claude | jq`) cannot be left as orphans.
///
/// # Arguments
///
/// * `command` - The shell command to execute (run via `sh -c`)
/// * `cwd` - Optional working directory (for worktree execution)
/// * `operation_type` - Optional operation type for logging (apply/archive/resolve/analyze/acceptance)
/// * `change_id` - Optional change ID for logging context
///
/// # Returns
///
/// A tuple of (`StreamingChildHandle`, `Receiver<OutputLine>`). Drain the receiver first
/// (it closes when all retries complete), then call `.wait()` on the handle to obtain
/// the final exit status.
///
/// # Retry Behaviour
///
/// Retries are governed by the `CommandQueueConfig`:
/// - Error pattern matching (`retry_error_patterns`)
/// - Short execution duration (`retry_if_duration_under_secs`)
/// - Non-zero exit code (agent crash)
///
/// Retry notifications are emitted as stderr lines on the output channel.
pub async fn execute_streaming_with_retry(
&self,
command: &str,
cwd: Option<&Path>,
operation_type: Option<&str>,
change_id: Option<&str>,
) -> Result<(StreamingChildHandle, mpsc::Receiver<OutputLine>)> {
// Admission is reserved before anything is launched. A scope that is
// already closing refuses the execution outright, so a command that
// raced shutdown never reaches stagger, retry, or `Command::spawn`.
let scope_execution = match &self.run_command_scope {
Some(scope) => match scope.register(operation_type, change_id) {
Some(execution) => Some(execution),
None => {
return Ok(refused_after_shutdown(
"run command admission is closed",
operation_type,
change_id,
));
}
},
None => None,
};
// Output channel that callers drain while the background task streams.
let (out_tx, out_rx) = mpsc::channel::<OutputLine>(1024);
// Cancel signal: StreamingChildHandle.terminate() → background task.
let (cancel_tx, cancel_rx) = tokio::sync::oneshot::channel::<()>();
// Shared current PID (0 = no process running).
let current_pid = Arc::new(AtomicU32::new(0));
// Completion signal: background task → StreamingChildHandle.wait().
let (status_tx, status_rx) = tokio::sync::oneshot::channel::<std::process::ExitStatus>();
// Process-group cleanup evidence: background task → callers that gate
// repository finalization on confirmed quiescence. Always published
// before the final status so a caller that observes completion can also
// observe why cleanup succeeded or failed.
let (cleanup_tx, cleanup_rx) = tokio::sync::oneshot::channel::<ProcessGroupCleanupReport>();
// Why this invocation ended: an ordinary exit, the inactivity timeout,
// the absolute runtime limit, or a deliberate termination. Published
// before the final status so a caller that observes completion can also
// decide whether another attempt is admissible at all.
let (termination_tx, termination_rx) =
tokio::sync::oneshot::channel::<CommandTermination>();
// Clone values for the background task.
let command_queue = self.command_queue.clone();
let command_str = command.to_string();
let cwd_owned = cwd.map(|p| p.to_path_buf());
let operation_type_owned = operation_type.map(|s| s.to_string());
let change_id_owned = change_id.map(|s| s.to_string());
let pid_arc = current_pid.clone();
let stream_json_textify = self.stream_json_textify;
let strict_process_cleanup = self.strict_process_cleanup;
let cleanup_timeout_ms = self.process_group_cleanup_timeout_ms;
let command_envs = self.command_envs.clone();
// Spawn the background retry task. It owns the real child processes and responds
// to the cancel signal by terminating the current process group via SIGTERM/SIGKILL.
tokio::spawn(async move {
// Owned for the whole runner task: the registration is what keeps
// the run's cleanup barrier waiting, independently of whether the
// caller still holds its `StreamingChildHandle`.
let scope_execution = scope_execution;
let scope_cancel = scope_execution.as_ref().map(ScopeExecution::cancel_token);
let scoped = scope_execution.is_some();
let max_retries = command_queue.config().max_retries;
let retry_delay_ms = command_queue.config().retry_delay_ms;
let inactivity_timeout_secs = command_queue.config().inactivity_timeout_secs;
let kill_grace_secs = command_queue.config().inactivity_kill_grace_secs;
let inactivity_timeout_max_retries =
command_queue.config().inactivity_timeout_max_retries;
// Absolute invocation deadline, selected here from the operation
// type this invocation already declared: Acceptance carries its own
// shorter deadline, every other class keeps `command_max_runtime_secs`
// and its `0`-disable semantics. Resolving it inside the common
// runner rather than at the call site is what keeps a bounded class
// from being an optional per-caller decision.
let max_runtime_secs = command_queue
.config()
.effective_max_runtime_secs(operation_type_owned.as_deref());
// cancel_rx is wrapped in Option so we can neutralise it after first use.
let mut cancel_rx_opt = Some(cancel_rx);
let mut cancel_observed = false;
// Terminal reason for this invocation, published exactly once.
let mut termination_tx = Some(termination_tx);
// Cleanup evidence for the attempt that ends this execution. A run
// that never spawns a process has no owned group to prove quiescent.
let mut cleanup_tx = Some(cleanup_tx);
let mut cleanup_report = ProcessGroupCleanupReport::not_applicable(
"no command process was started, so there is no owned process group",
);
let mut attempt = 0u32;
let mut inactivity_retries_used = 0u32;
let mut final_exit_status: Option<std::process::ExitStatus> = None;
// Stays `NotStarted` for every `break 'retry` that never reached a
// completed attempt (refused admission, launch failure, unwaitable
// child), so those are never reported as an ordinary exit.
let mut final_termination = CommandTermination::NotStarted;
'retry: loop {
// Shutdown recheck before every attempt. A scope that closed
// while the previous retry delay was sleeping admits no further
// attempt, so the counter never advances after closure.
if let Some(execution) = &scope_execution {
if execution.is_shutdown() {
let _ = out_tx
.send(OutputLine::Stderr(shutdown_refusal_line(
"retry",
operation_type_owned.as_deref(),
change_id_owned.as_deref(),
attempt + 1,
)))
.await;
break 'retry;
}
execution.mark_waiting_to_spawn();
}
attempt += 1;
let start_time = Instant::now();
// Build the real command and attach it to a new process group so the
// entire pipeline (sh + agent + filter) can be killed as one unit.
let mut cmd = Command::new("sh");
cmd.arg("-c")
.arg(&command_str)
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped());
if let Some(ref dir) = cwd_owned {
cmd.current_dir(dir);
}
cmd.envs(
command_envs
.iter()
.map(|(key, value)| (key.as_str(), value.as_str())),
);
// Set the spawned process as its own process group leader (PGID = PID).
// This allows killpg to reach all pipeline children.
#[cfg(unix)]
{
use crate::process_manager::configure_process_group;
configure_process_group(&mut cmd);
}
// Apply the stagger delay first, then take final scope
// admission and spawn inside one critical section. Checking
// admission before the delay would leave a window in which
// shutdown starts and a process is still launched.
command_queue.wait_for_stagger_slot().await;
let spawned = match &scope_execution {
Some(execution) => execution.admit_spawn(|| cmd.spawn()),
None => Some(cmd.spawn()),
};
let child = match spawned {
None => {
// Admission closed between registration and spawn: the
// command body never runs.
let _ = out_tx
.send(OutputLine::Stderr(shutdown_refusal_line(
"spawn",
operation_type_owned.as_deref(),
change_id_owned.as_deref(),
attempt,
)))
.await;
break 'retry;
}
Some(Ok(c)) => c,
Some(Err(e)) => {
error!(
op = ?operation_type_owned,
change_id = ?change_id_owned,
attempt,
"Failed to spawn command: {}",
e
);
// The caller only ever sees a synthetic failure status
// for this path, so the cause travels on the output
// channel it already collects. That is what puts a
// launch failure into Apply history, the Acceptance
// command diagnostic, and the cleanup-review diagnosis
// instead of leaving them with a bare exit code.
let _ = out_tx
.send(OutputLine::Stderr(launch_failure_line(
"spawn",
operation_type_owned.as_deref(),
change_id_owned.as_deref(),
attempt,
&e.to_string(),
)))
.await;
break 'retry;
}
};
let mut managed_child = match ManagedChild::new(child) {
Ok(mc) => mc,
Err(e) => {
error!(
op = ?operation_type_owned,
change_id = ?change_id_owned,
"Failed to wrap child in ManagedChild: {}",
e
);
let _ = out_tx
.send(OutputLine::Stderr(launch_failure_line(
"process-manager",
operation_type_owned.as_deref(),
change_id_owned.as_deref(),
attempt,
&e.to_string(),
)))
.await;
break 'retry;
}
};
// Publish the real PID so StreamingChildHandle.id() is accurate.
let pid = managed_child.id().unwrap_or(0);
pid_arc.store(pid, Ordering::SeqCst);
// The absolute deadline starts here — at successful child spawn
// — not at admission, stagger, or retry-delay time, so queueing
// never eats the agent's own budget.
let runtime_deadline = (max_runtime_secs > 0)
.then(|| tokio::time::Instant::now() + Duration::from_secs(max_runtime_secs));
debug!(
pid,
op = ?operation_type_owned,
change_id = ?change_id_owned,
attempt,
"Streaming child started"
);
// Take stdout/stderr handles before lending managed_child to the
// inactivity/cancel select loop.
let stdout = managed_child.child.stdout.take();
let stderr = managed_child.child.stderr.take();
// Activity channel: readers signal liveness to the inactivity monitor.
let (activity_tx, mut activity_rx) = mpsc::channel::<()>(100);
// Stderr accumulator (for retry-condition check after exit).
let (stderr_acc_tx, mut stderr_acc_rx) = mpsc::channel::<String>(2);
// Spawn stdout reader.
let out_tx_stdout = out_tx.clone();
let activity_tx_stdout = activity_tx.clone();
let textify = stream_json_textify;
let stdout_handle = tokio::spawn(async move {
if let Some(stdout) = stdout {
let mut lines = BufReader::new(stdout).lines();
let mut text_buf = StreamJsonTextBuffer::new();
while let Ok(Some(line)) = lines.next_line().await {
let _ = activity_tx_stdout.send(()).await;
if textify {
let emitted = process_stdout_line(&line, &mut text_buf);
for l in emitted {
let _ = out_tx_stdout.send(OutputLine::Stdout(l)).await;
}
} else {
let _ = out_tx_stdout.send(OutputLine::Stdout(line)).await;
}
}
// Flush any remaining partial line in the buffer.
if textify {
if let Some(tail) = text_buf.finalize() {
if !tail.is_empty() {
let _ = out_tx_stdout.send(OutputLine::Stdout(tail)).await;
}
}
}
}
});
// Spawn stderr reader.
let out_tx_stderr = out_tx.clone();
let activity_tx_stderr = activity_tx.clone();
let stderr_handle = tokio::spawn(async move {
let mut buf = String::new();
if let Some(stderr) = stderr {
let mut lines = BufReader::new(stderr).lines();
while let Ok(Some(line)) = lines.next_line().await {
let _ = activity_tx_stderr.send(()).await;
buf.push_str(&line);
buf.push('\n');
let _ = out_tx_stderr.send(OutputLine::Stderr(line)).await;
}
}
let _ = stderr_acc_tx.send(buf).await;
});
// Drop the extra activity sender so the channel closes naturally when
// both reader tasks finish.
drop(activity_tx);
// --- Monitoring loop: activity reset, inactivity timeout, cancellation ---
let mut inactivity_triggered = false;
if inactivity_timeout_secs > 0 {
let mut last_activity = Instant::now();
let timeout_dur = Duration::from_secs(inactivity_timeout_secs);
'watch: loop {
let elapsed = last_activity.elapsed();
let remaining = if elapsed < timeout_dur {
timeout_dur - elapsed
} else {
Duration::from_secs(0)
};
tokio::select! {
biased;
// Scope shutdown reaches the runner task directly,
// so it is not silenced by a dropped handle.
_ = wait_for_scope_shutdown(&scope_cancel) => {
warn!(
pid,
op = ?operation_type_owned,
change_id = ?change_id_owned,
"Run command scope shutdown, terminating process group (pid={})", pid
);
if let Some(execution) = &scope_execution {
execution.mark_cleaning();
}
let _ = managed_child
.terminate_with_timeout(Duration::from_secs(5))
.await;
let report = verify_owned_process_group(
pid,
cleanup_timeout_ms,
operation_type_owned.as_deref(),
change_id_owned.as_deref(),
&out_tx,
)
.await;
pid_arc.store(0, Ordering::SeqCst);
if let Some(execution) = &scope_execution {
execution.record_cleanup(pid, &report);
}
publish_cleanup_report(&mut cleanup_tx, report);
publish_termination(
&mut termination_tx,
CommandTermination::Cancelled,
);
let _ = status_tx.send(make_fail_status());
if let Some(execution) = &scope_execution {
execution.finish();
}
return;
}
// Cancellation from StreamingChildHandle.terminate().
result = async {
match cancel_rx_opt {
Some(ref mut rx) => rx.await,
None => std::future::pending().await,
}
}, if !cancel_observed => {
cancel_observed = true;
cancel_rx_opt = None;
// A scoped runner treats handle-channel closure
// as cancellation too: losing the caller's
// handle is never permission to detach.
if result.is_ok() || scoped {
warn!(
pid,
op = ?operation_type_owned,
change_id = ?change_id_owned,
handle_dropped = result.is_err(),
"Streaming command cancelled, terminating process group (pid={})", pid
);
if let Some(execution) = &scope_execution {
execution.mark_cleaning();
}
let _ = managed_child
.terminate_with_timeout(Duration::from_secs(5))
.await;
// Reaping the leader is not proof that the
// group is empty: verify quiescence before
// the caller may touch the worktree.
let report = verify_owned_process_group(
pid,
cleanup_timeout_ms,
operation_type_owned.as_deref(),
change_id_owned.as_deref(),
&out_tx,
)
.await;
pid_arc.store(0, Ordering::SeqCst);
if let Some(execution) = &scope_execution {
execution.record_cleanup(pid, &report);
}
publish_cleanup_report(&mut cleanup_tx, report);
publish_termination(
&mut termination_tx,
CommandTermination::Cancelled,
);
let _ = status_tx.send(make_fail_status());
if let Some(execution) = &scope_execution {
execution.finish();
}
return;
}
// Err = handle was dropped without calling terminate() — continue.
}
// Absolute runtime limit reached. Evaluated in the
// same loop as the inactivity timer but from an
// independent deadline, so a command that keeps
// printing cannot postpone it.
_ = wait_for_runtime_limit(runtime_deadline) => {
warn!(
pid,
max_runtime_secs,
op = ?operation_type_owned,
change_id = ?change_id_owned,
cwd = ?cwd_owned,
"Absolute runtime limit reached, terminating process group \
(pid={}, limit={}s)",
pid, max_runtime_secs
);
let _ = out_tx
.send(OutputLine::Stderr(runtime_limit_line(
max_runtime_secs,
operation_type_owned.as_deref(),
change_id_owned.as_deref(),
pid,
)))
.await;
if let Some(execution) = &scope_execution {
execution.mark_cleaning();
}
let _ = managed_child
.terminate_with_timeout(Duration::from_secs(5))
.await;
let report = verify_owned_process_group(
pid,
cleanup_timeout_ms,
operation_type_owned.as_deref(),
change_id_owned.as_deref(),
&out_tx,
)
.await;
pid_arc.store(0, Ordering::SeqCst);
if let Some(execution) = &scope_execution {
execution.record_cleanup(pid, &report);
}
publish_cleanup_report(&mut cleanup_tx, report);
// Retry admission for this invocation closes
// here: the reason is published before the
// status, and the task returns without ever
// re-entering the retry loop.
publish_termination(
&mut termination_tx,
CommandTermination::RuntimeLimit,
);
let _ = status_tx.send(make_fail_status());
if let Some(execution) = &scope_execution {
execution.finish();
}
return;
}
// Output activity resets the inactivity timer.
a = activity_rx.recv() => {
if a.is_some() {
last_activity = Instant::now();
} else {
// All readers finished.
break 'watch;
}
}
// Inactivity timeout reached.
_ = tokio::time::sleep(remaining) => {
inactivity_triggered = true;
let last_activity_age_secs = last_activity.elapsed().as_secs();
// Get PGID for structured logging (Unix only).
#[cfg(unix)]
let pgid_opt: Option<u32> = {
use nix::unistd::{getpgid, Pid};
getpgid(Some(Pid::from_raw(pid as i32)))
.ok()
.map(|p| p.as_raw() as u32)
};
#[cfg(not(unix))]
let pgid_opt: Option<u32> = None;
warn!(
pid,
pgid = pgid_opt,
timeout_secs = inactivity_timeout_secs,
grace_secs = kill_grace_secs,
last_activity_age_secs,
op = ?operation_type_owned,
change_id = ?change_id_owned,
cwd = ?cwd_owned,
"Inactivity timeout triggered: no output for {}s \
(pid={}, pgid={:?}, timeout={}s, grace={}s, \
last_activity_age={}s, op={:?}, change_id={:?}, cwd={:?})",
last_activity_age_secs, pid, pgid_opt,
inactivity_timeout_secs, kill_grace_secs,
last_activity_age_secs,
operation_type_owned, change_id_owned, cwd_owned
);
// Emit a user-facing message so callers see the timeout context.
let timeout_msg = format!(
"Command terminated by inactivity timeout after {}s \
(op={}, change_id={}, pid={}, last_activity_age={}s)",
inactivity_timeout_secs,
operation_type_owned.as_deref().unwrap_or("unknown"),
change_id_owned.as_deref().unwrap_or("none"),
pid,
last_activity_age_secs,
);
let _ = out_tx.send(OutputLine::Stderr(timeout_msg)).await;
tokio::time::sleep(Duration::from_secs(kill_grace_secs)).await;
if managed_child.id().is_some() {
warn!(
pid,
pgid = pgid_opt,
signal = "SIGTERM",
op = ?operation_type_owned,
change_id = ?change_id_owned,
"Grace period expired, sending SIGTERM to process group \
(pid={}, pgid={:?})",
pid, pgid_opt
);
match managed_child.terminate() {
Ok(()) => {
debug!(
pid,
signal = "SIGTERM",
target_pgid = pgid_opt,
"SIGTERM delivered to process group"
);
}
Err(e) => {
warn!(
pid,
signal = "SIGTERM",
target_pid = pid,
target_pgid = pgid_opt,
errno = %e,
op = ?operation_type_owned,
change_id = ?change_id_owned,
"SIGTERM failed for process group \
(pid={}, pgid={:?}): {}",
pid, pgid_opt, e
);
}
}
tokio::time::sleep(Duration::from_millis(500)).await;
warn!(
pid,
pgid = pgid_opt,
signal = "SIGKILL",
op = ?operation_type_owned,
change_id = ?change_id_owned,
"Sending SIGKILL to process group (pid={}, pgid={:?})",
pid, pgid_opt
);
match managed_child.force_kill().await {
Ok(()) => {
debug!(
pid,
signal = "SIGKILL",
target_pgid = pgid_opt,
"SIGKILL delivered to process group"
);
}
Err(e) => {
warn!(
pid,
signal = "SIGKILL",
target_pid = pid,
target_pgid = pgid_opt,
errno = %e,
op = ?operation_type_owned,
change_id = ?change_id_owned,
"SIGKILL failed for process group \
(pid={}, pgid={:?}): {}",
pid, pgid_opt, e
);
}
}
}
break 'watch;
}
}
}
} else {
// No inactivity timeout — only watch for cancel and reader completion.
'watch_no_timeout: loop {
tokio::select! {
biased;
_ = wait_for_scope_shutdown(&scope_cancel) => {
warn!(
pid,
op = ?operation_type_owned,
change_id = ?change_id_owned,
"Run command scope shutdown, terminating process group (pid={})", pid
);
if let Some(execution) = &scope_execution {
execution.mark_cleaning();
}
let _ = managed_child
.terminate_with_timeout(Duration::from_secs(5))
.await;
let report = verify_owned_process_group(
pid,
cleanup_timeout_ms,
operation_type_owned.as_deref(),
change_id_owned.as_deref(),
&out_tx,
)
.await;
pid_arc.store(0, Ordering::SeqCst);
if let Some(execution) = &scope_execution {
execution.record_cleanup(pid, &report);
}
publish_cleanup_report(&mut cleanup_tx, report);
publish_termination(
&mut termination_tx,
CommandTermination::Cancelled,
);
let _ = status_tx.send(make_fail_status());
if let Some(execution) = &scope_execution {
execution.finish();
}
return;
}
result = async {
match cancel_rx_opt {
Some(ref mut rx) => rx.await,
None => std::future::pending().await,
}
}, if !cancel_observed => {
cancel_observed = true;
cancel_rx_opt = None;
if result.is_ok() || scoped {
warn!(
pid,
op = ?operation_type_owned,
change_id = ?change_id_owned,
handle_dropped = result.is_err(),
"Streaming command cancelled, terminating process group (pid={})", pid
);
if let Some(execution) = &scope_execution {
execution.mark_cleaning();
}
let _ = managed_child
.terminate_with_timeout(Duration::from_secs(5))
.await;
let report = verify_owned_process_group(
pid,
cleanup_timeout_ms,
operation_type_owned.as_deref(),
change_id_owned.as_deref(),
&out_tx,
)
.await;
pid_arc.store(0, Ordering::SeqCst);
if let Some(execution) = &scope_execution {
execution.record_cleanup(pid, &report);
}
publish_cleanup_report(&mut cleanup_tx, report);
publish_termination(
&mut termination_tx,
CommandTermination::Cancelled,
);
let _ = status_tx.send(make_fail_status());
if let Some(execution) = &scope_execution {
execution.finish();
}
return;
}
}
// The absolute deadline is enforced whether or not
// the inactivity timeout is configured: they are
// independent limits.
_ = wait_for_runtime_limit(runtime_deadline) => {
warn!(
pid,
max_runtime_secs,
op = ?operation_type_owned,
change_id = ?change_id_owned,
cwd = ?cwd_owned,
"Absolute runtime limit reached, terminating process group \
(pid={}, limit={}s)",
pid, max_runtime_secs
);
let _ = out_tx
.send(OutputLine::Stderr(runtime_limit_line(
max_runtime_secs,
operation_type_owned.as_deref(),
change_id_owned.as_deref(),
pid,
)))
.await;
if let Some(execution) = &scope_execution {
execution.mark_cleaning();
}
let _ = managed_child
.terminate_with_timeout(Duration::from_secs(5))
.await;
let report = verify_owned_process_group(
pid,
cleanup_timeout_ms,
operation_type_owned.as_deref(),
change_id_owned.as_deref(),
&out_tx,
)
.await;
pid_arc.store(0, Ordering::SeqCst);
if let Some(execution) = &scope_execution {
execution.record_cleanup(pid, &report);
}
publish_cleanup_report(&mut cleanup_tx, report);
publish_termination(
&mut termination_tx,
CommandTermination::RuntimeLimit,
);
let _ = status_tx.send(make_fail_status());
if let Some(execution) = &scope_execution {
execution.finish();
}
return;
}
a = activity_rx.recv() => {
if a.is_none() {
break 'watch_no_timeout;
}
}
}
}
}
// Wait for readers to finish before collecting status.
let _ = stdout_handle.await;
let _ = stderr_handle.await;
let stderr_collected = stderr_acc_rx.recv().await.unwrap_or_default();
// Collect the child's exit status.
let status = match managed_child.wait().await {
Ok(s) => s,
Err(e) => {
error!(
op = ?operation_type_owned,
change_id = ?change_id_owned,
"Failed to wait for child process: {}", e
);
break 'retry;
}
};
pid_arc.store(0, Ordering::SeqCst);
if let Some(execution) = &scope_execution {
execution.mark_cleaning();
}
// Strict post-completion cleanup: sweep the process group after every
// command outcome (success, failure, inactivity timeout) to ensure no
// background processes spawned by the agent command outlive it.
let attempt_cleanup = if strict_process_cleanup {
verify_owned_process_group(
pid,
cleanup_timeout_ms,
operation_type_owned.as_deref(),
change_id_owned.as_deref(),
&out_tx,
)
.await
} else {
ProcessGroupCleanupReport::not_applicable(
"strict post-completion process-group cleanup is disabled",
)
};
if let Some(execution) = &scope_execution {
execution.record_cleanup(pid, &attempt_cleanup);
}
// An earlier attempt that could not be proven quiescent stays
// the published verdict: its survivors may still be running.
if cleanup_report.is_confirmed() {
cleanup_report = attempt_cleanup;
}
// Handle inactivity-timeout exits with dedicated retry policy.
if inactivity_triggered {
if inactivity_timeout_max_retries > 0
&& inactivity_retries_used < inactivity_timeout_max_retries
// Shutdown is rechecked immediately before the retry
// delay, not only at the top of the loop, so a scope
// that closed during this attempt never buys a sleep.
&& !scope_execution
.as_ref()
.is_some_and(ScopeExecution::is_shutdown)
{
inactivity_retries_used += 1;
warn!(
inactivity_retries_used,
inactivity_timeout_max_retries,
op = ?operation_type_owned,
change_id = ?change_id_owned,
"Inactivity timeout retry {}/{}, retrying in {}ms",
inactivity_retries_used, inactivity_timeout_max_retries,
retry_delay_ms
);
let _ = managed_child.terminate();
tokio::time::sleep(Duration::from_millis(100)).await;
let _ = managed_child.force_kill().await;
let retry_msg = format!(
"[Retry {}/{}] Inactivity timeout, retrying in {}ms \
(op={}, change_id={})",
inactivity_retries_used,
inactivity_timeout_max_retries,
retry_delay_ms,
operation_type_owned.as_deref().unwrap_or("unknown"),
change_id_owned.as_deref().unwrap_or("none"),
);
let _ = out_tx.send(OutputLine::Stderr(retry_msg)).await;
tokio::time::sleep(Duration::from_millis(retry_delay_ms)).await;
continue 'retry;
}
// Exhausted inactivity retries (or retries disabled): emit final message.
if inactivity_timeout_max_retries > 0 {
let exhausted_msg = format!(
"Inactivity timeout: exhausted all {} retries \
(op={}, change_id={})",
inactivity_timeout_max_retries,
operation_type_owned.as_deref().unwrap_or("unknown"),
change_id_owned.as_deref().unwrap_or("none"),
);
let _ = out_tx.send(OutputLine::Stderr(exhausted_msg)).await;
}
// Do not fall through to the crash/pattern retry check.
final_exit_status = Some(status);
final_termination = CommandTermination::InactivityTimeout;
break 'retry;
}
// Check whether a retry is warranted for non-inactivity exits.
if !status.success() {
let exit_code = status.code().unwrap_or(-1);
let duration = start_time.elapsed();
// Shutdown suppresses the ordinary retry branch as well: an
// observed closure is checked before the delay and again at
// final spawn admission.
if command_queue.should_retry(attempt, duration, &stderr_collected, exit_code)
&& !scope_execution
.as_ref()
.is_some_and(ScopeExecution::is_shutdown)
{
warn!(
attempt,
max_retries,
exit_code,
op = ?operation_type_owned,
change_id = ?change_id_owned,
"Retryable error detected, retrying in {}ms", retry_delay_ms
);
// Enforce full process-group cleanup before the next attempt:
// 1. SIGTERM → cooperative shutdown of all PGID members.
// 2. 100ms grace period → let SIGTERM-responsive processes exit.
// 3. SIGKILL → force-kill any survivors (e.g. SIGTERM-immune loops).
// managed_child.wait() has already reaped `sh`, but pipeline siblings
// sharing the same PGID may still be running. terminate() alone is
// best-effort; force_kill() after the grace window ensures they die.
let _ = managed_child.terminate();
tokio::time::sleep(Duration::from_millis(100)).await;
let _ = managed_child.force_kill().await;
let retry_msg = format!(
"[Retry {}/{}] Command crashed, retrying in {}ms...",
attempt, max_retries, retry_delay_ms
);
let _ = out_tx.send(OutputLine::Stderr(retry_msg)).await;
tokio::time::sleep(Duration::from_millis(retry_delay_ms)).await;
continue 'retry;
}
}
final_exit_status = Some(status);
final_termination = CommandTermination::Exited;
break 'retry;
}
// Send final exit status (failure if we exited the retry loop without one).
// An unconfirmed process-group cleanup can never be published as a
// successful completion: descendants may still be mutating the
// worktree the caller is about to finalize.
let mut final_status = final_exit_status.unwrap_or_else(make_fail_status);
if !cleanup_report.is_confirmed() && final_status.success() {
warn!(
op = ?operation_type_owned,
change_id = ?change_id_owned,
"Downgrading successful command status: {}",
cleanup_report.diagnostics()
);
final_status = make_fail_status();
}
publish_cleanup_report(&mut cleanup_tx, cleanup_report);
publish_termination(&mut termination_tx, final_termination);
let _ = status_tx.send(final_status);
// The registration is released only here: the runner task has
// ended, and it disappears from the scope only when every owned
// identity was already proven quiescent.
if let Some(execution) = &scope_execution {
execution.finish();
}
// Dropping out_tx closes the output channel, signalling end-of-output to callers.
});
let handle = StreamingChildHandle::new(
cancel_tx,
current_pid,
status_rx,
cleanup_rx,
termination_rx,
);
Ok((handle, out_rx))
}
/// Execute a command with streaming output and stagger delay.
///
/// This is the core execution method used by all AI-driven commands.
/// It spawns the command through CommandQueue (with stagger), then
/// streams stdout/stderr to an mpsc channel.
///
/// # Arguments
///
/// * `command` - The shell command to execute (will be run via `sh -c`)
/// * `cwd` - Optional working directory (for worktree execution)
///
/// # Returns
///
/// A tuple of (ManagedChild, Receiver<OutputLine>) for process control and output streaming
#[allow(dead_code)] // Infrastructure ready, integration pending (tasks 3.2, 3.3, 4.1-4.3)
pub async fn execute_streaming(
&self,
command: &str,
cwd: Option<&Path>,
) -> Result<(ManagedChild, mpsc::Receiver<OutputLine>)> {
debug!(
module = module_path!(),
"Executing shell command with stagger: sh -c {} (cwd: {:?})", command, cwd
);
let child = self
.command_queue
.execute_with_stagger(move || {
let mut cmd = Command::new("sh");
cmd.arg("-c")
.arg(command)
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped());
if let Some(dir) = cwd {
cmd.current_dir(dir);
}
cmd
})
.await?;
// Wrap in ManagedChild for proper cleanup
let mut managed = ManagedChild::new(child)?;
// Take stdout/stderr from the child field
let stdout = managed.child.stdout.take().ok_or_else(|| {
OrchestratorError::AgentCommand(format!(
"Failed to capture stdout for command '{}' (cwd: {:?})",
command, cwd
))
})?;
let stderr = managed.child.stderr.take().ok_or_else(|| {
OrchestratorError::AgentCommand(format!(
"Failed to capture stderr for command '{}' (cwd: {:?})",
command, cwd
))
})?;
// Create channel for output streaming
let (tx, rx) = mpsc::channel(1024);
// Spawn stdout reader
let tx_stdout = tx.clone();
tokio::spawn(async move {
let mut reader = BufReader::new(stdout).lines();
while let Ok(Some(line)) = reader.next_line().await {
if tx_stdout.send(OutputLine::Stdout(line)).await.is_err() {
break;
}
}
});
// Spawn stderr reader
let tx_stderr = tx;
tokio::spawn(async move {
let mut reader = BufReader::new(stderr).lines();
while let Ok(Some(line)) = reader.next_line().await {
if tx_stderr.send(OutputLine::Stderr(line)).await.is_err() {
break;
}
}
});
Ok((managed, rx))
}
}
/// Longest launch-error cause carried on the output channel.
///
/// The cause is operational evidence for the next agent prompt and for terminal
/// diagnostics, so it is bounded exactly like every other tail this workflow
/// carries.
const MAX_LAUNCH_ERROR_CHARS: usize = 400;
/// One bounded stderr line describing why a command never started.
///
/// A command that fails before it runs produces no output of its own and only a
/// synthetic failure status, so this line is the only evidence the caller can
/// collect. It is emitted on the same output channel every operation already
/// drains, which is what carries it into Apply history, the Acceptance command
/// diagnostic, and the cleanup-review diagnosis.
fn launch_failure_line(
stage: &str,
operation_type: Option<&str>,
change_id: Option<&str>,
attempt: u32,
cause: &str,
) -> String {
let single_line = cause.split_whitespace().collect::<Vec<_>>().join(" ");
let bounded = match single_line.char_indices().nth(MAX_LAUNCH_ERROR_CHARS) {
Some((idx, _)) => format!("{}...", &single_line[..idx]),
None => single_line,
};
format!(
"Command launch failed (stage={}, op={}, change_id={}, attempt={}): {}",
stage,
operation_type.unwrap_or("unknown"),
change_id.unwrap_or("none"),
attempt,
bounded
)
}
/// One bounded stderr line describing a command refused by scope shutdown.
///
/// `stage` distinguishes the two closure points a command can hit: `spawn` is
/// the final serialized admission check, `retry` is a later attempt that was
/// never admitted at all.
fn shutdown_refusal_line(
stage: &str,
operation_type: Option<&str>,
change_id: Option<&str>,
attempt: u32,
) -> String {
format!(
"Command refused by run command scope shutdown (stage={}, op={}, change_id={}, attempt={})",
stage,
operation_type.unwrap_or("unknown"),
change_id.unwrap_or("none"),
attempt
)
}
/// Result handed back when the scope refuses an execution before it starts.
///
/// The caller gets the same shape it always gets — a handle plus an output
/// receiver — so no operation needs a shutdown-specific code path: it drains
/// one diagnostic line and observes a failure status.
fn refused_after_shutdown(
reason: &str,
operation_type: Option<&str>,
change_id: Option<&str>,
) -> (StreamingChildHandle, mpsc::Receiver<OutputLine>) {
let (out_tx, out_rx) = mpsc::channel::<OutputLine>(1);
let (cancel_tx, _cancel_rx) = tokio::sync::oneshot::channel::<()>();
let (status_tx, status_rx) = tokio::sync::oneshot::channel::<std::process::ExitStatus>();
let (cleanup_tx, cleanup_rx) = tokio::sync::oneshot::channel::<ProcessGroupCleanupReport>();
let (termination_tx, termination_rx) = tokio::sync::oneshot::channel::<CommandTermination>();
let _ = out_tx.try_send(OutputLine::Stderr(shutdown_refusal_line(
"admission",
operation_type,
change_id,
1,
)));
let _ = cleanup_tx.send(ProcessGroupCleanupReport::not_applicable(reason));
let _ = termination_tx.send(CommandTermination::NotStarted);
let _ = status_tx.send(make_fail_status());
drop(out_tx);
(
StreamingChildHandle::new(
cancel_tx,
Arc::new(AtomicU32::new(0)),
status_rx,
cleanup_rx,
termination_rx,
),
out_rx,
)
}
/// Await scope shutdown, or never resolve for an unscoped runner.
async fn wait_for_scope_shutdown(token: &Option<tokio_util::sync::CancellationToken>) {
match token {
Some(token) => token.cancelled().await,
None => std::future::pending().await,
}
}
/// Await the absolute runtime deadline, or never resolve when it is disabled.
///
/// The deadline is absolute rather than a per-iteration duration, so re-entering
/// the monitoring loop after each output line cannot push it further away.
async fn wait_for_runtime_limit(deadline: Option<tokio::time::Instant>) {
match deadline {
Some(deadline) => tokio::time::sleep_until(deadline).await,
None => std::future::pending().await,
}
}
/// One bounded stderr line describing an invocation stopped by its runtime limit.
///
/// It names the limit rather than the elapsed time so an operator can act on the
/// configuration knob directly.
fn runtime_limit_line(
max_runtime_secs: u64,
operation_type: Option<&str>,
change_id: Option<&str>,
pid: u32,
) -> String {
format!(
"Command terminated by absolute runtime limit after {}s \
(op={}, change_id={}, pid={}); this invocation is not retried",
max_runtime_secs,
operation_type.unwrap_or("unknown"),
change_id.unwrap_or("none"),
pid
)
}
/// Publishes the terminal reason for this invocation exactly once.
fn publish_termination(
termination_tx: &mut Option<tokio::sync::oneshot::Sender<CommandTermination>>,
termination: CommandTermination,
) {
if let Some(tx) = termination_tx.take() {
let _ = tx.send(termination);
}
}
/// Runs bounded cleanup on the owned process group and returns typed evidence.
///
/// The report is the only accepted proof that no owned descendant can still be
/// touching the managed worktree; unconfirmed cleanup is surfaced on the output
/// stream so operators see the same diagnostics the caller acts on.
async fn verify_owned_process_group(
pid: u32,
cleanup_timeout_ms: u64,
op: Option<&str>,
change_id: Option<&str>,
out_tx: &mpsc::Sender<OutputLine>,
) -> ProcessGroupCleanupReport {
if pid == 0 {
return ProcessGroupCleanupReport::not_applicable(
"no process group id was available for cleanup",
);
}
let report = cleanup_process_group_verified(
pid,
DEFAULT_PROCESS_GROUP_SIGTERM_GRACE_MS,
cleanup_timeout_ms,
op,
change_id,
)
.await;
if !report.is_confirmed() {
let _ = out_tx.send(OutputLine::Stderr(report.diagnostics())).await;
}
report
}
/// Publishes cleanup evidence exactly once for this execution.
fn publish_cleanup_report(
cleanup_tx: &mut Option<tokio::sync::oneshot::Sender<ProcessGroupCleanupReport>>,
report: ProcessGroupCleanupReport,
) {
if let Some(tx) = cleanup_tx.take() {
let _ = tx.send(report);
}
}
/// Construct a synthetic failure [`std::process::ExitStatus`] for error paths.
fn make_fail_status() -> std::process::ExitStatus {
#[cfg(unix)]
{
use std::os::unix::process::ExitStatusExt;
std::process::ExitStatus::from_raw(1)
}
#[cfg(not(unix))]
{
use std::os::windows::process::ExitStatusExt;
std::process::ExitStatus::from_raw(1)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::defaults::*;
/// The absolute deadline is a decision about *when*, so it is verified with
/// paused time rather than by waiting: nothing here spawns a process, and no
/// assertion depends on how long the test actually took.
mod absolute_runtime_deadline {
use super::*;
/// A disabled limit is a future that never resolves, which is what lets
/// it sit in the same `select!` as every other lifecycle arm without
/// needing a separate code path for "no deadline".
#[tokio::test(start_paused = true)]
async fn a_disabled_limit_never_fires() {
// A full simulated day passes and the arm still has not resolved.
let elapsed =
tokio::time::timeout(Duration::from_secs(86_400), wait_for_runtime_limit(None))
.await;
assert!(
elapsed.is_err(),
"`0` must disable the deadline entirely, not defer it"
);
}
/// Re-entering the monitoring loop cannot postpone the deadline: it is
/// an absolute instant, so awaiting it repeatedly still resolves at the
/// same moment. This is precisely why output activity cannot extend it.
#[tokio::test(start_paused = true)]
async fn an_absolute_deadline_is_not_pushed_back_by_re_entry() {
let start = tokio::time::Instant::now();
let deadline = Some(start + Duration::from_secs(60));
// Ten monitoring-loop iterations, each one abandoning the wait after
// five seconds the way an output line does.
for _ in 0..10 {
let _ =
tokio::time::timeout(Duration::from_secs(5), wait_for_runtime_limit(deadline))
.await;
}
wait_for_runtime_limit(deadline).await;
assert_eq!(
start.elapsed(),
Duration::from_secs(60),
"the deadline must fire 60s after it was set, regardless of how \
many times the loop re-awaited it"
);
}
/// The runner reads the limit from its own queue config, so the
/// configured value is what an invocation is actually bounded by.
#[test]
fn the_runner_carries_the_configured_limit() {
let config = OrchestratorConfig {
command_max_runtime_secs: Some(120),
..OrchestratorConfig::default()
};
let runner =
AiCommandRunner::from_orchestrator_config(&config, Arc::new(Mutex::new(None)));
assert_eq!(runner.queue_config().max_runtime_secs, 120);
let disabled = OrchestratorConfig {
command_max_runtime_secs: Some(0),
..OrchestratorConfig::default()
};
let runner =
AiCommandRunner::from_orchestrator_config(&disabled, Arc::new(Mutex::new(None)));
assert_eq!(
runner.queue_config().max_runtime_secs,
0,
"an explicit disable must survive into the runner"
);
}
/// The operator-facing line names the limit rather than the elapsed
/// time, so the diagnostic points at the knob that has to change.
#[test]
fn the_runtime_limit_line_names_the_limit_and_the_no_retry_decision() {
let line = runtime_limit_line(3600, Some("apply"), Some("change-a"), 4242);
assert!(line.contains("3600s"), "the configured limit: {line}");
assert!(line.contains("op=apply"), "the operation: {line}");
assert!(line.contains("change_id=change-a"), "the change: {line}");
assert!(line.contains("pid=4242"), "the owned identity: {line}");
assert!(
line.contains("not retried"),
"the retry decision must be visible to the operator: {line}"
);
}
}
/// A termination reason exists to answer one question: may this invocation
/// be attempted again? A boundary decision may not; a command that ended on
/// its own may.
mod command_termination {
use crate::process_manager::CommandTermination;
#[test]
fn deliberate_terminations_never_permit_a_retry() {
for termination in [
CommandTermination::RuntimeLimit,
CommandTermination::Cancelled,
CommandTermination::NotStarted,
] {
assert!(
!termination.permits_retry(),
"{} must not admit a retry",
termination.as_str()
);
}
}
#[test]
fn command_owned_endings_stay_retryable() {
for termination in [
CommandTermination::Exited,
CommandTermination::InactivityTimeout,
] {
assert!(
termination.permits_retry(),
"{} is a command outcome the existing retry policy owns",
termination.as_str()
);
}
}
/// Only the runtime limit answers to `is_runtime_limit`, so an operator
/// stop and a runaway command stay distinguishable in reporting.
#[test]
fn only_the_runtime_limit_identifies_as_one() {
assert!(CommandTermination::RuntimeLimit.is_runtime_limit());
for other in [
CommandTermination::Exited,
CommandTermination::InactivityTimeout,
CommandTermination::Cancelled,
CommandTermination::NotStarted,
] {
assert!(!other.is_runtime_limit(), "{}", other.as_str());
}
}
}
#[test]
fn configured_constructor_preserves_runner_settings() {
let config = OrchestratorConfig {
command_queue_stagger_delay_ms: Some(41),
stream_json_textify: Some(false),
command_strict_process_cleanup: Some(false),
envs: Some(HashMap::from([(
"CFLX_TEST_ENV".to_string(),
"configured-value".to_string(),
)])),
..OrchestratorConfig::default()
};
let shared_state = Arc::new(Mutex::new(None));
let runner = AiCommandRunner::from_orchestrator_config(&config, shared_state.clone());
assert!(Arc::ptr_eq(&runner.shared_stagger_state(), &shared_state));
assert_eq!(runner.queue_config().stagger_delay_ms, 41);
assert!(!runner.stream_json_textify);
assert!(!runner.strict_process_cleanup);
assert_eq!(
runner.command_envs,
HashMap::from([("CFLX_TEST_ENV".to_string(), "configured-value".to_string())])
);
}
#[tokio::test]
async fn test_shared_stagger_state() {
let shared_state = Arc::new(Mutex::new(None));
let config = CommandQueueConfig {
acceptance_max_runtime_secs:
crate::config::defaults::DEFAULT_ACCEPTANCE_MAX_RUNTIME_SECS,
stagger_delay_ms: 100,
max_retries: DEFAULT_MAX_RETRIES,
retry_delay_ms: DEFAULT_RETRY_DELAY_MS,
retry_error_patterns: vec![],
retry_if_duration_under_secs: DEFAULT_RETRY_IF_DURATION_UNDER_SECS,
inactivity_timeout_secs: 0,
inactivity_kill_grace_secs: 10,
inactivity_timeout_max_retries: 0,
strict_process_cleanup: true,
max_runtime_secs: 0,
};
let runner1 = AiCommandRunner::new(config.clone(), shared_state.clone());
let runner2 = AiCommandRunner::new(config.clone(), shared_state.clone());
// Execute first command
let start = Instant::now();
let (mut child1, _rx1) = runner1.execute_streaming("echo test1", None).await.unwrap();
let _ = child1.wait().await;
// Execute second command - should wait for stagger
let (mut child2, _rx2) = runner2.execute_streaming("echo test2", None).await.unwrap();
let elapsed = start.elapsed();
let _ = child2.wait().await;
// Second command should have waited at least 100ms
assert!(
elapsed.as_millis() >= 90,
"Stagger delay not applied: {:?}",
elapsed
);
}
#[tokio::test]
async fn test_streaming_with_retry_applies_configured_envs_without_logging_values() {
let shared_state = Arc::new(Mutex::new(None));
let config = CommandQueueConfig {
acceptance_max_runtime_secs:
crate::config::defaults::DEFAULT_ACCEPTANCE_MAX_RUNTIME_SECS,
stagger_delay_ms: 0,
max_retries: 0,
retry_delay_ms: 0,
retry_error_patterns: vec![],
retry_if_duration_under_secs: 0,
inactivity_timeout_secs: 0,
inactivity_kill_grace_secs: 10,
inactivity_timeout_max_retries: 0,
strict_process_cleanup: true,
max_runtime_secs: 0,
};
let mut runner = AiCommandRunner::new(config, shared_state);
runner.set_command_envs(HashMap::from([(
"CFLX_TEST_AGENT_ENV".to_string(),
"secret-value".to_string(),
)]));
let command = "printf %s \"$CFLX_TEST_AGENT_ENV\"";
assert!(!command.contains("secret-value"));
let (mut handle, mut rx) = runner
.execute_streaming_with_retry(command, None, Some("test"), None)
.await
.unwrap();
let mut stdout = String::new();
while let Some(line) = rx.recv().await {
if let OutputLine::Stdout(s) = line {
stdout.push_str(&s);
}
}
let status = handle.wait().await.unwrap();
assert!(status.success());
assert_eq!(stdout, "secret-value");
}
/// Verify that execute_streaming_with_retry returns a real child PID (not 0).
#[tokio::test]
async fn test_streaming_with_retry_real_pid() {
let shared_state = Arc::new(Mutex::new(None));
let config = CommandQueueConfig {
acceptance_max_runtime_secs:
crate::config::defaults::DEFAULT_ACCEPTANCE_MAX_RUNTIME_SECS,
stagger_delay_ms: 0,
max_retries: DEFAULT_MAX_RETRIES,
retry_delay_ms: DEFAULT_RETRY_DELAY_MS,
retry_error_patterns: vec![],
retry_if_duration_under_secs: DEFAULT_RETRY_IF_DURATION_UNDER_SECS,
inactivity_timeout_secs: 0,
inactivity_kill_grace_secs: 10,
inactivity_timeout_max_retries: 0,
strict_process_cleanup: true,
max_runtime_secs: 0,
};
let runner = AiCommandRunner::new(config, shared_state);
let (mut handle, mut rx) = runner
.execute_streaming_with_retry("sleep 0.2", None, Some("test"), None)
.await
.unwrap();
// Give the background task time to spawn the real child.
tokio::time::sleep(Duration::from_millis(100)).await;
// The handle must expose the PID of the real child, not 0.
let pid = handle.id();
assert!(pid.is_some(), "Expected a real PID, got None");
assert!(pid.unwrap() > 0, "Expected PID > 0");
// Drain output and wait.
while rx.recv().await.is_some() {}
let _ = handle.wait().await;
}
/// Verify that retry-attempt cleanup does not leave leaked processes.
///
/// Spawns a command that starts a lingering background subprocess (`sleep 30`) then
/// exits with failure, triggering a retry. After all retries complete the test asserts
/// that the process group from attempt 1 has no surviving members.
///
/// This is the regression test for the "Streaming retry does not leak processes across
/// attempts" scenario from Acceptance #2 Follow-up.
#[cfg(unix)]
#[tokio::test]
async fn test_streaming_retry_no_leaked_processes() {
let shared_state = Arc::new(Mutex::new(None));
let config = CommandQueueConfig {
acceptance_max_runtime_secs:
crate::config::defaults::DEFAULT_ACCEPTANCE_MAX_RUNTIME_SECS,
stagger_delay_ms: 0,
max_retries: 2,
retry_delay_ms: 300,
retry_error_patterns: vec![],
retry_if_duration_under_secs: 5, // treat short exits as retryable
inactivity_timeout_secs: 0,
inactivity_kill_grace_secs: 5,
inactivity_timeout_max_retries: 0,
strict_process_cleanup: true,
max_runtime_secs: 0,
};
let runner = AiCommandRunner::new(config, shared_state);
// Each attempt writes its sh PID (= PGID because configure_process_group makes sh
// the group leader) to a temp file, spawns a lingering `sleep 30` that shares the
// PGID, then exits with failure so a retry is triggered.
let pgid_file =
std::env::temp_dir().join(format!("retry_leak_pgid_{}.txt", std::process::id()));
let pgid_path = pgid_file.display().to_string();
// Redirect sleep's I/O away from the inherited pipes so the stdout/stderr readers
// reach EOF immediately when sh exits (instead of waiting 30 s for sleep to end).
// sleep 30 stays in the same PGID as sh and is the "orphan candidate" we verify
// is killed by the retry cleanup before the next attempt begins.
let cmd = format!(
"echo $$ >> {path}; sleep 30 >/dev/null 2>&1 </dev/null & exit 1",
path = pgid_path
);
let (mut handle, mut rx) = runner
.execute_streaming_with_retry(&cmd, None, Some("test"), None)
.await
.unwrap();
// Drain output to avoid backpressure stalling the background task.
while rx.recv().await.is_some() {}
let _ = handle.wait().await;
// Read PGIDs recorded by the attempts.
assert!(
pgid_file.exists(),
"PGID file should have been created by at least one attempt"
);
let content = std::fs::read_to_string(&pgid_file).unwrap_or_default();
let _ = std::fs::remove_file(&pgid_file);
let pgids: Vec<i32> = content
.lines()
.filter_map(|l| l.trim().parse().ok())
.collect();
assert!(
pgids.len() >= 2,
"Expected PGIDs from at least 2 attempts (attempt 1 + retry), got: {:?}",
pgids
);
// Give a brief moment for OS signal delivery to fully propagate.
tokio::time::sleep(Duration::from_millis(150)).await;
// The retry cleanup fires only between attempts (before `continue 'retry`).
// The *final* attempt has no subsequent retry, so its background process is not
// cleaned up by the retry logic. We verify all non-final attempt PGIDs are dead.
//
// `killpg(pgid, 0)` returns 0 if any process in the group is alive (ESRCH otherwise).
let non_final_count = pgids.len() - 1;
for pgid in &pgids[..non_final_count] {
let result = unsafe { libc::killpg(*pgid, 0) };
assert_eq!(
result, -1,
"Process group {} (non-final attempt) should be dead after retry cleanup, \
but it still has live members (killpg returned 0)",
pgid
);
}
// Clean up the final attempt's background sleep so the test does not leak
// a `sleep 30` process into the test runner's process table.
if let Some(last_pgid) = pgids.last() {
unsafe {
libc::killpg(*last_pgid, libc::SIGKILL);
}
}
}
/// Invocation-scoped command ownership.
///
/// These drive the real runner control flow — a real scope, a real retry
/// loop, a real `Command::spawn` — because the defect they pin is precisely
/// that a command could still start, or still retry, after the run that
/// owned it had already begun shutting down.
mod run_command_scope {
use super::*;
fn scoped_runner(
config: CommandQueueConfig,
stagger: SharedStaggerState,
) -> (AiCommandRunner, RunCommandScope) {
let scope = RunCommandScope::new();
let mut runner = AiCommandRunner::new(config, stagger);
runner.set_run_command_scope(scope.clone());
(runner, scope)
}
fn marker_path(label: &str) -> std::path::PathBuf {
std::env::temp_dir().join(format!(
"cflx_scope_{}_{}_{}.txt",
label,
std::process::id(),
Instant::now().elapsed().as_nanos()
))
}
/// Closing the scope while a command is parked at the stagger delay
/// must refuse the spawn itself, not merely the next retry.
///
/// The marker file is the proof: it exists only if the command body ran.
#[cfg(unix)]
#[tokio::test]
async fn run_command_scope_refuses_spawn_after_shutdown() {
let marker = marker_path("refuse");
let _ = std::fs::remove_file(&marker);
// Pre-armed stagger timestamp: the runner parks in the stagger wait
// with its final admission still pending, which is exactly the
// window a check taken *before* the delay would miss.
let stagger: SharedStaggerState = Arc::new(Mutex::new(Some(Instant::now())));
let config = CommandQueueConfig {
acceptance_max_runtime_secs:
crate::config::defaults::DEFAULT_ACCEPTANCE_MAX_RUNTIME_SECS,
stagger_delay_ms: 250,
max_retries: 3,
retry_delay_ms: 0,
retry_error_patterns: vec![],
retry_if_duration_under_secs: 0,
inactivity_timeout_secs: 0,
inactivity_kill_grace_secs: 1,
inactivity_timeout_max_retries: 0,
strict_process_cleanup: true,
max_runtime_secs: 0,
};
let (runner, scope) = scoped_runner(config, stagger);
let command = format!("touch {}", marker.display());
let (mut handle, mut rx) = runner
.execute_streaming_with_retry(&command, None, Some("apply"), Some("change-a"))
.await
.expect("the call itself succeeds; the spawn is what is refused");
tokio::time::sleep(Duration::from_millis(60)).await;
scope.close();
let mut stderr_lines = Vec::new();
while let Some(line) = rx.recv().await {
if let OutputLine::Stderr(s) = line {
stderr_lines.push(s);
}
}
let status = handle.wait().await.expect("a final status is reported");
assert!(
!marker.exists(),
"the command body must never run after admission closed"
);
assert!(!status.success(), "a refused command is not a success");
assert!(
stderr_lines.iter().any(|line| line
.contains("refused by run command scope shutdown")
&& line.contains("op=apply")
&& line.contains("change_id=change-a")),
"the refusal must be legible to the caller: {stderr_lines:?}"
);
assert_eq!(
scope.active_executions(),
0,
"a command that never spawned holds no barrier open"
);
let _ = std::fs::remove_file(&marker);
}
/// A scope that closes while an attempt sits in its retry delay must
/// stop the attempt counter where it is.
#[cfg(unix)]
#[tokio::test]
async fn run_command_scope_suppresses_retry_after_shutdown() {
let attempts = marker_path("retry");
let _ = std::fs::remove_file(&attempts);
let config = CommandQueueConfig {
acceptance_max_runtime_secs:
crate::config::defaults::DEFAULT_ACCEPTANCE_MAX_RUNTIME_SECS,
stagger_delay_ms: 0,
max_retries: 5,
retry_delay_ms: 300,
retry_error_patterns: vec![],
// Treat the fast failing exit as retryable, so an unscoped run
// would keep going.
retry_if_duration_under_secs: 30,
inactivity_timeout_secs: 0,
inactivity_kill_grace_secs: 1,
inactivity_timeout_max_retries: 0,
strict_process_cleanup: true,
max_runtime_secs: 0,
};
let (runner, scope) = scoped_runner(config, Arc::new(Mutex::new(None)));
let command = format!("echo attempt >> {}; exit 1", attempts.display());
let (mut handle, mut rx) = runner
.execute_streaming_with_retry(&command, None, Some("apply"), Some("change-a"))
.await
.expect("the first attempt is admitted");
// Attempt 1 has failed and is sleeping out its retry delay by now.
tokio::time::sleep(Duration::from_millis(120)).await;
scope.close();
let mut stderr_lines = Vec::new();
while let Some(line) = rx.recv().await {
if let OutputLine::Stderr(s) = line {
stderr_lines.push(s);
}
}
let _ = handle.wait().await;
let recorded = std::fs::read_to_string(&attempts).unwrap_or_default();
let _ = std::fs::remove_file(&attempts);
assert_eq!(
recorded.lines().count(),
1,
"no attempt may start after shutdown, got: {recorded:?}"
);
assert!(
stderr_lines.iter().any(|line| line.contains("stage=retry")
&& line.contains("refused by run command scope shutdown")),
"the suppressed retry must say why: {stderr_lines:?}"
);
assert_eq!(scope.active_executions(), 0);
}
/// A registered execution keeps the barrier open, and multiple
/// registrations are awaited concurrently under one deadline rather
/// than one after another.
#[tokio::test]
async fn run_command_scope_awaits_registrations_concurrently() {
let scope = RunCommandScope::new();
let held: Vec<_> = ["alpha", "beta", "gamma"]
.iter()
.map(|change| {
scope
.register_for_test("apply", Some(change))
.expect("an open scope admits registrations")
})
.collect();
assert_eq!(scope.active_executions(), 3);
let releasing = scope.clone();
tokio::spawn(async move {
for (index, registration) in held.into_iter().enumerate() {
tokio::time::sleep(Duration::from_millis(40 * (index as u64 + 1))).await;
registration.release_confirmed();
}
let _ = releasing;
});
let started = Instant::now();
let cleanup = scope.shutdown(Duration::from_secs(2)).await;
let elapsed = started.elapsed();
assert!(
cleanup.is_quiescent(),
"every registration reported quiescence: {}",
cleanup.diagnostics()
);
assert!(
elapsed < Duration::from_millis(400),
"cleanups share one absolute budget instead of running back to back: {elapsed:?}"
);
}
/// An execution that never reports leaves the barrier at its deadline
/// with diagnostics naming the operation and change.
#[tokio::test]
async fn run_command_scope_reports_bounded_escalation_diagnostics() {
let scope = RunCommandScope::new();
let _held = scope
.register_for_test("acceptance", Some("change-a"))
.expect("an open scope admits the registration");
let cleanup = scope.shutdown(Duration::from_millis(60)).await;
assert!(
!cleanup.is_quiescent(),
"an unreported execution is never silently quiescent"
);
assert!(cleanup.timed_out, "the bounded barrier must expire");
let diagnostics = cleanup.diagnostics();
assert!(
diagnostics.contains("op=acceptance") && diagnostics.contains("change_id=change-a"),
"diagnostics must be actionable: {diagnostics}"
);
}
/// An owned identity that is already gone is proven quiescent by the
/// scope's own managed escalation, and the change stops blocking its
/// completion handshake only then.
#[cfg(unix)]
#[tokio::test]
async fn run_command_scope_escalates_a_retained_identity() {
// A short-lived real child gives a genuinely dead PGID rather than a
// number that might belong to an unrelated live process.
let child = std::process::Command::new("sh")
.arg("-c")
.arg("exit 0")
.spawn()
.expect("spawn");
let pid = child.id();
let mut child = child;
let _ = child.wait();
let scope = RunCommandScope::new();
let registration = scope.register_unproven_for_test("apply", Some("change-a"), pid);
assert!(
!scope.change_is_quiescent("change-a"),
"a retained identity keeps the change unproven"
);
registration.release_confirmed();
assert!(
!scope.change_is_quiescent("change-a"),
"runner-task exit alone is not cleanup evidence"
);
assert_eq!(scope.retained_process_ids(), vec![pid]);
let cleanup = scope.shutdown(Duration::from_secs(2)).await;
assert!(
cleanup.is_quiescent(),
"managed escalation proves the dead group quiescent: {}",
cleanup.diagnostics()
);
assert!(
scope.change_is_quiescent("change-a"),
"only confirmed cleanup releases the change"
);
}
/// A targeted force-stop kills exactly one change's process group and
/// proves it reaped, while an unrelated change's group keeps running.
///
/// Integration evidence, deliberately: the property is about real
/// processes and real signals, and a double could not show that an
/// unrelated PGID survived. Both children are `setsid` leaders so each
/// PGID names one owned group and nothing else.
#[cfg(unix)]
#[tokio::test]
async fn force_stop_change_kills_only_the_named_changes_process_group() {
fn spawn_group() -> (tokio::process::Child, u32) {
let mut command = tokio::process::Command::new("sh");
command
.arg("-c")
.arg("sleep 30")
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null());
crate::process_manager::configure_process_group(&mut command);
let child = command.spawn().expect("spawn a real process group");
let pid = child.id().expect("a spawned child has a pid");
(child, pid)
}
fn group_is_alive(pgid: u32) -> bool {
use nix::sys::signal::killpg;
use nix::unistd::Pid;
killpg(Pid::from_raw(pgid as i32), None).is_ok()
}
let (target_child, target_pgid) = spawn_group();
let (bystander_child, bystander_pgid) = spawn_group();
let scope = RunCommandScope::new();
let _target = scope.register_unproven_for_test("apply", Some("alpha"), target_pgid);
let _bystander =
scope.register_unproven_for_test("apply", Some("beta"), bystander_pgid);
assert!(scope.change_owns_managed_process("alpha"));
assert!(scope.change_owns_managed_process("beta"));
// The owner of the target child reaps it, exactly as the workspace
// task that spawned it does in production. Without a reaper the
// killed leader stays a zombie and the group is *not* provably
// empty — which is the point: settlement waits for reaping, not
// merely for the signal.
let mut target_child = target_child;
let reaper = tokio::spawn(async move { target_child.wait().await });
let report = scope
.force_stop_change("alpha", FORCE_STOP_CHANGE_KILL_BUDGET)
.await;
assert!(
report.is_confirmed(),
"the target group must be proven empty: {}",
report.diagnostics()
);
assert_eq!(report.identities, 1);
assert_eq!(report.confirmed, 1);
assert!(
!group_is_alive(target_pgid),
"the target's process group must be gone before settlement"
);
assert!(
!scope.change_owns_managed_process("alpha"),
"a proven identity is released from the ownership graph"
);
// The unrelated change is untouched: same registration, same live
// group, and admission is still open for the rest of the run.
assert!(
group_is_alive(bystander_pgid),
"an unrelated change's process group must keep running"
);
assert!(scope.change_owns_managed_process("beta"));
assert!(
!scope.is_closed(),
"a targeted force-stop must not close run admission"
);
let status = reaper
.await
.expect("the reaper task")
.expect("the killed child is waitable");
assert!(
!status.success(),
"a SIGKILLed child never exits successfully"
);
// Leave no live process behind.
let mut bystander_child = bystander_child;
let _ = bystander_child.kill().await;
let _ = bystander_child.wait().await;
}
/// A target that owns no identity is neither signalled nor claimed.
#[tokio::test]
async fn force_stop_change_signals_nothing_for_a_change_with_no_process_group() {
let scope = RunCommandScope::new();
let _registration = scope
.register_for_test("apply", Some("alpha"))
.expect("an open scope admits the registration");
assert!(
!scope.change_owns_managed_process("alpha"),
"a reserved execution that never spawned owns no process group"
);
let report = scope
.force_stop_change("alpha", FORCE_STOP_CHANGE_KILL_BUDGET)
.await;
assert!(report.is_confirmed());
assert_eq!(
report.identities, 0,
"nothing was signalled, so nothing needed reaping"
);
}
/// A closed scope admits nothing at all, so a later operation cannot
/// slip a command into a run that has already stopped.
#[tokio::test]
async fn run_command_scope_refuses_registration_once_closed() {
let scope = RunCommandScope::new();
scope.close();
assert!(scope
.register_for_test("archive", Some("change-a"))
.is_none());
assert!(scope.cancel_token().is_cancelled());
}
/// Losing the caller's handle is treated as cancellation, never as
/// permission to keep the process group running.
#[cfg(unix)]
#[tokio::test]
async fn run_command_scope_treats_handle_loss_as_cancellation() {
let config = CommandQueueConfig {
acceptance_max_runtime_secs:
crate::config::defaults::DEFAULT_ACCEPTANCE_MAX_RUNTIME_SECS,
stagger_delay_ms: 0,
max_retries: 1,
retry_delay_ms: 0,
retry_error_patterns: vec![],
retry_if_duration_under_secs: 0,
inactivity_timeout_secs: 0,
inactivity_kill_grace_secs: 1,
inactivity_timeout_max_retries: 0,
strict_process_cleanup: true,
max_runtime_secs: 0,
};
let (runner, scope) = scoped_runner(config, Arc::new(Mutex::new(None)));
let (handle, _rx) = runner
.execute_streaming_with_retry(
"sleep 300 >/dev/null 2>&1 </dev/null & sleep 300",
None,
Some("apply"),
Some("change-a"),
)
.await
.expect("admitted");
tokio::time::sleep(Duration::from_millis(150)).await;
let pgid = handle.id().expect("a real pid") as i32;
// The workspace future was aborted: its handle is simply gone.
drop(handle);
let cleanup = scope.wait_quiescent(Duration::from_secs(5)).await;
// Reap before asserting so a failure cannot leak the group.
let survived = unsafe { libc::killpg(pgid, 0) } == 0;
if survived {
unsafe { libc::killpg(pgid, libc::SIGKILL) };
}
assert!(
cleanup.is_quiescent(),
"a dropped handle must still reach cleanup: {}",
cleanup.diagnostics()
);
assert!(
!survived,
"process group {pgid} outlived the run that owned it"
);
}
}
/// A command that never starts still owes the caller its cause.
///
/// Apply history, the Acceptance command diagnostic, and the cleanup-review
/// diagnosis are all built from the bounded tails collected off this output
/// channel, so forwarding the launch error here is what makes a launch
/// failure legible to all three instead of a bare synthetic exit code.
mod launch_failure_diagnostics {
use super::*;
#[test]
fn the_line_names_the_stage_operation_change_and_cause() {
let line = launch_failure_line(
"spawn",
Some("acceptance"),
Some("change-a"),
2,
"No such file or directory (os error 2)",
);
assert!(line.contains("stage=spawn"), "{line}");
assert!(line.contains("op=acceptance"), "{line}");
assert!(line.contains("change_id=change-a"), "{line}");
assert!(line.contains("attempt=2"), "{line}");
assert!(
line.contains("No such file or directory (os error 2)"),
"the cause must survive, not just the fact of failure: {line}"
);
}
#[test]
fn an_unlabelled_invocation_still_reports_its_cause() {
let line = launch_failure_line("process-manager", None, None, 1, "broken pipe");
assert!(line.contains("op=unknown"), "{line}");
assert!(line.contains("change_id=none"), "{line}");
assert!(line.contains("broken pipe"), "{line}");
}
#[test]
fn the_cause_is_bounded_and_single_line() {
let line = launch_failure_line(
"spawn",
Some("apply"),
Some("change-a"),
1,
&format!("failure\n{}", "x".repeat(5_000)),
);
assert_eq!(line.lines().count(), 1, "diagnostics stay one line");
assert!(line.ends_with("..."), "an over-long cause is truncated");
assert!(
line.chars().count() < MAX_LAUNCH_ERROR_CHARS + 200,
"the bounded cause keeps the line small: {} chars",
line.chars().count()
);
}
/// End to end over the real channel: a command that cannot be spawned at
/// all reaches the caller as a stderr line carrying the cause, followed
/// by the failure status.
#[cfg(unix)]
#[tokio::test]
async fn a_command_that_cannot_start_reports_its_cause_on_the_output_channel() {
let config = CommandQueueConfig {
acceptance_max_runtime_secs:
crate::config::defaults::DEFAULT_ACCEPTANCE_MAX_RUNTIME_SECS,
stagger_delay_ms: 0,
max_retries: 0,
retry_delay_ms: 0,
retry_error_patterns: vec![],
retry_if_duration_under_secs: 0,
inactivity_timeout_secs: 0,
inactivity_kill_grace_secs: 1,
inactivity_timeout_max_retries: 0,
strict_process_cleanup: true,
max_runtime_secs: 0,
};
let runner = AiCommandRunner::new(config, Arc::new(Mutex::new(None)));
// A working directory that does not exist makes the spawn itself
// fail, which is the path that previously produced no output at all.
let missing_cwd = std::path::Path::new("/nonexistent-cflx-launch-failure-dir");
let (mut handle, mut rx) = runner
.execute_streaming_with_retry(
"echo never-runs",
Some(missing_cwd),
Some("acceptance"),
Some("change-a"),
)
.await
.expect("the call itself succeeds; the launch is what fails");
let mut stderr_lines = Vec::new();
while let Some(line) = rx.recv().await {
if let OutputLine::Stderr(s) = line {
stderr_lines.push(s);
}
}
let status = handle.wait().await.expect("a final status is reported");
assert!(!status.success(), "a launch failure is not a success");
let joined = stderr_lines.join("\n");
assert!(
joined.contains("Command launch failed"),
"the failure must be visible to every diagnostic built from this channel: \
{stderr_lines:?}"
);
assert!(
joined.contains("op=acceptance") && joined.contains("change_id=change-a"),
"{stderr_lines:?}"
);
}
}
/// Verify that terminating a pipeline via StreamingChildHandle kills the entire
/// process group (sh + children), leaving no orphaned processes.
#[cfg(unix)]
#[tokio::test]
async fn test_streaming_with_retry_terminates_pipeline() {
let shared_state = Arc::new(Mutex::new(None));
let config = CommandQueueConfig {
acceptance_max_runtime_secs:
crate::config::defaults::DEFAULT_ACCEPTANCE_MAX_RUNTIME_SECS,
stagger_delay_ms: 0,
max_retries: 1,
retry_delay_ms: 50,
retry_error_patterns: vec![],
retry_if_duration_under_secs: 0,
inactivity_timeout_secs: 0,
inactivity_kill_grace_secs: 10,
inactivity_timeout_max_retries: 0,
strict_process_cleanup: true,
max_runtime_secs: 0,
};
let runner = AiCommandRunner::new(config, shared_state);
// Pipeline: sleep 999 | cat — both processes should be killed by terminate().
let (mut handle, _rx) = runner
.execute_streaming_with_retry("sleep 999 | cat", None, Some("test"), None)
.await
.unwrap();
// Wait for the child to be spawned.
tokio::time::sleep(Duration::from_millis(150)).await;
let pid = handle.id();
assert!(pid.is_some(), "Should have a real PID");
// Terminate the process group.
let outcome = handle
.terminate_with_timeout(Duration::from_secs(5))
.await
.unwrap();
// Process should have exited (not timed out).
assert!(
!matches!(
outcome,
crate::process_manager::TerminationOutcome::TimedOut
),
"Expected process to exit after termination, got TimedOut"
);
}
#[cfg(feature = "heavy-tests")]
#[tokio::test]
async fn test_inactivity_timeout_streaming_pipeline() {
let shared_state = Arc::new(Mutex::new(None));
let config = CommandQueueConfig {
acceptance_max_runtime_secs:
crate::config::defaults::DEFAULT_ACCEPTANCE_MAX_RUNTIME_SECS,
stagger_delay_ms: 0,
max_retries: 1,
retry_delay_ms: 50,
retry_error_patterns: vec![],
retry_if_duration_under_secs: 0,
inactivity_timeout_secs: 2,
inactivity_kill_grace_secs: 1,
inactivity_timeout_max_retries: 0,
strict_process_cleanup: true,
max_runtime_secs: 0,
};
let runner = AiCommandRunner::new(config, shared_state);
let start = Instant::now();
// Pipeline with no output — sleep 30 piped through cat produces nothing.
let (mut handle, mut rx) = runner
.execute_streaming_with_retry(
"sleep 30 | cat",
None,
Some("apply"),
Some("test-change"),
)
.await
.unwrap();
// Collect all output lines emitted before the channel closes.
let mut lines: Vec<String> = Vec::new();
while let Some(line) = rx.recv().await {
match line {
OutputLine::Stdout(s) | OutputLine::Stderr(s) => lines.push(s),
}
}
let _ = handle.wait().await;
let elapsed = start.elapsed();
// Should complete after timeout + grace (2s + 1s = ~3s), well under 15s.
assert!(
elapsed.as_secs() >= 2 && elapsed.as_secs() <= 15,
"Expected completion between 2–15s, got {:?}",
elapsed
);
// The output channel should contain a message about inactivity timeout.
let has_timeout_msg = lines
.iter()
.any(|l| l.contains("inactivity timeout") && l.contains("2s"));
assert!(
has_timeout_msg,
"Expected inactivity timeout message in output (with timeout seconds), got: {:?}",
lines
);
}
#[cfg(feature = "heavy-tests")]
#[tokio::test]
async fn test_inactivity_timeout_retry() {
let shared_state = Arc::new(Mutex::new(None));
let config = CommandQueueConfig {
acceptance_max_runtime_secs:
crate::config::defaults::DEFAULT_ACCEPTANCE_MAX_RUNTIME_SECS,
stagger_delay_ms: 0,
max_retries: 1,
retry_delay_ms: 100,
retry_error_patterns: vec![],
retry_if_duration_under_secs: 0,
inactivity_timeout_secs: 2,
inactivity_kill_grace_secs: 1,
inactivity_timeout_max_retries: 3,
strict_process_cleanup: true,
max_runtime_secs: 0,
};
let runner = AiCommandRunner::new(config, shared_state);
// Command that produces no output — will trigger inactivity timeout on every attempt.
let (mut handle, mut rx) = runner
.execute_streaming_with_retry(
"sleep 30 | cat",
None,
Some("apply"),
Some("test-change-retry"),
)
.await
.unwrap();
let mut lines: Vec<String> = Vec::new();
while let Some(line) = rx.recv().await {
match line {
OutputLine::Stdout(s) | OutputLine::Stderr(s) => lines.push(s),
}
}
let _ = handle.wait().await;
// Expect 3 retry messages ("[Retry 1/3]", "[Retry 2/3]", "[Retry 3/3]").
for i in 1u32..=3 {
let expected = format!("[Retry {}/3]", i);
let found = lines
.iter()
.any(|l| l.contains(&expected) && l.contains("Inactivity timeout"));
assert!(
found,
"Expected retry message '{}' with 'Inactivity timeout' in output, got: {:?}",
expected, lines
);
}
// Expect the exhaustion message.
let exhausted = lines
.iter()
.any(|l| l.contains("Inactivity timeout") && l.contains("exhausted all 3 retries"));
assert!(
exhausted,
"Expected 'exhausted all 3 retries' message in output, got: {:?}",
lines
);
}
/// Regression test (task 1.6): a successful command that backgrounds a child process is
/// cleaned up by strict_process_cleanup. After the command exits with status 0, no
/// members should remain in its process group.
#[cfg(unix)]
#[tokio::test]
async fn test_post_completion_cleanup_on_success() {
let shared_state = Arc::new(Mutex::new(None));
let config = CommandQueueConfig {
acceptance_max_runtime_secs:
crate::config::defaults::DEFAULT_ACCEPTANCE_MAX_RUNTIME_SECS,
stagger_delay_ms: 0,
max_retries: 1,
retry_delay_ms: 50,
retry_error_patterns: vec![],
retry_if_duration_under_secs: 0,
inactivity_timeout_secs: 0,
inactivity_kill_grace_secs: 5,
inactivity_timeout_max_retries: 0,
strict_process_cleanup: true,
max_runtime_secs: 0,
};
let runner = AiCommandRunner::new(config, shared_state);
let pgid_file =
std::env::temp_dir().join(format!("post_cleanup_success_{}.txt", std::process::id()));
let pgid_path = pgid_file.display().to_string();
// Write the sh PID (= PGID after setsid) to a file, background a long sleep, then exit 0.
let cmd = format!(
"echo $$ > {path}; sleep 30 >/dev/null 2>&1 </dev/null & exit 0",
path = pgid_path
);
let (mut handle, mut rx) = runner
.execute_streaming_with_retry(&cmd, None, Some("test"), None)
.await
.unwrap();
while rx.recv().await.is_some() {}
let status = handle.wait().await.expect("wait");
assert!(status.success(), "Command should succeed");
// Allow signal delivery to propagate.
tokio::time::sleep(Duration::from_millis(250)).await;
let content = std::fs::read_to_string(&pgid_file).unwrap_or_default();
let _ = std::fs::remove_file(&pgid_file);
let pgid: i32 = content.trim().parse().expect("valid pgid");
// killpg(pgid, 0) should return -1 with ESRCH — no live members remain.
let result = unsafe { libc::killpg(pgid, 0) };
if result == 0 {
// Kill the leaked process so the test doesn't leave orphans.
unsafe { libc::killpg(pgid, libc::SIGKILL) };
panic!(
"post-cleanup: process group {} still has live members after successful command completion",
pgid
);
}
let errno = std::io::Error::last_os_error().raw_os_error().unwrap_or(0);
assert_eq!(
errno,
libc::ESRCH,
"post-cleanup: expected ESRCH for pgid={}, got errno={}",
pgid,
errno
);
}
/// Regression test (task 1.7): a failed command that backgrounds a child process is
/// cleaned up by strict_process_cleanup. After the command exits with status 1, no
/// members should remain in its process group.
#[cfg(unix)]
#[tokio::test]
async fn test_post_completion_cleanup_on_failure() {
let shared_state = Arc::new(Mutex::new(None));
let config = CommandQueueConfig {
acceptance_max_runtime_secs:
crate::config::defaults::DEFAULT_ACCEPTANCE_MAX_RUNTIME_SECS,
stagger_delay_ms: 0,
max_retries: 1, // One attempt only — no retry on failure
retry_delay_ms: 50,
retry_error_patterns: vec![],
retry_if_duration_under_secs: 0, // Disable short-duration retry
inactivity_timeout_secs: 0,
inactivity_kill_grace_secs: 5,
inactivity_timeout_max_retries: 0,
strict_process_cleanup: true,
max_runtime_secs: 0,
};
let runner = AiCommandRunner::new(config, shared_state);
let pgid_file =
std::env::temp_dir().join(format!("post_cleanup_failure_{}.txt", std::process::id()));
let pgid_path = pgid_file.display().to_string();
// Write the sh PID, background a long sleep, then exit 1.
let cmd = format!(
"echo $$ > {path}; sleep 30 >/dev/null 2>&1 </dev/null & exit 1",
path = pgid_path
);
let (mut handle, mut rx) = runner
.execute_streaming_with_retry(&cmd, None, Some("test"), None)
.await
.unwrap();
while rx.recv().await.is_some() {}
let _ = handle.wait().await;
// Allow signal delivery to propagate.
tokio::time::sleep(Duration::from_millis(250)).await;
let content = std::fs::read_to_string(&pgid_file).unwrap_or_default();
let _ = std::fs::remove_file(&pgid_file);
let pgid: i32 = content.trim().parse().expect("valid pgid");
// killpg(pgid, 0) should return -1 with ESRCH — no live members remain.
let result = unsafe { libc::killpg(pgid, 0) };
if result == 0 {
unsafe { libc::killpg(pgid, libc::SIGKILL) };
panic!(
"post-cleanup: process group {} still has live members after failed command completion",
pgid
);
}
let errno = std::io::Error::last_os_error().raw_os_error().unwrap_or(0);
assert_eq!(
errno,
libc::ESRCH,
"post-cleanup: expected ESRCH for pgid={}, got errno={}",
pgid,
errno
);
}
/// A natural, clean completion publishes confirmed cleanup evidence, so
/// callers that gate repository work on it may proceed.
#[cfg(unix)]
#[tokio::test]
async fn apply_completion_publishes_confirmed_cleanup_for_clean_exit() {
let shared_state = Arc::new(Mutex::new(None));
let config = CommandQueueConfig {
acceptance_max_runtime_secs:
crate::config::defaults::DEFAULT_ACCEPTANCE_MAX_RUNTIME_SECS,
stagger_delay_ms: 0,
max_retries: 1,
retry_delay_ms: 50,
retry_error_patterns: vec![],
retry_if_duration_under_secs: 0,
inactivity_timeout_secs: 0,
inactivity_kill_grace_secs: 5,
inactivity_timeout_max_retries: 0,
strict_process_cleanup: true,
max_runtime_secs: 0,
};
let runner = AiCommandRunner::new(config, shared_state);
let (mut handle, mut rx) = runner
.execute_streaming_with_retry("echo done", None, Some("apply"), Some("change-a"))
.await
.unwrap();
while rx.recv().await.is_some() {}
let status = handle.wait().await.expect("wait");
let report = handle.process_group_cleanup().await;
assert!(status.success());
assert!(
report.is_confirmed(),
"clean completion must publish confirmed quiescence: {}",
report.diagnostics()
);
}
/// Completion-grace cancellation must not report success when the owned
/// process group cannot be proven quiescent within the cleanup budget.
#[cfg(unix)]
#[tokio::test]
async fn apply_completion_grace_termination_reports_unconfirmed_cleanup() {
let shared_state = Arc::new(Mutex::new(None));
let config = CommandQueueConfig {
acceptance_max_runtime_secs:
crate::config::defaults::DEFAULT_ACCEPTANCE_MAX_RUNTIME_SECS,
stagger_delay_ms: 0,
max_retries: 1,
retry_delay_ms: 50,
retry_error_patterns: vec![],
retry_if_duration_under_secs: 0,
inactivity_timeout_secs: 0,
inactivity_kill_grace_secs: 5,
inactivity_timeout_max_retries: 0,
strict_process_cleanup: true,
max_runtime_secs: 0,
};
let mut runner = AiCommandRunner::new(config, shared_state);
// Zero budget: a SIGTERM-immune descendant can never be proven gone.
runner.set_process_group_cleanup_timeout_ms(0);
let (mut handle, _rx) = runner
.execute_streaming_with_retry(
"sh -c 'trap \"\" TERM; while :; do sleep 0.2; done' >/dev/null 2>&1 </dev/null & \
sleep 120",
None,
Some("apply"),
Some("change-a"),
)
.await
.unwrap();
tokio::time::sleep(Duration::from_millis(200)).await;
let pgid = handle.id().expect("real pid") as i32;
// Simulate the apply completion-grace termination: signal, then wait for
// the runner to publish its status and cleanup evidence.
handle.terminate().expect("terminate");
let status = handle.wait().await.expect("wait");
let report = handle.process_group_cleanup().await;
// Reap the survivor before asserting so a failure cannot leak it.
unsafe { libc::killpg(pgid, libc::SIGKILL) };
assert!(
!report.is_confirmed(),
"a surviving descendant must not be published as quiescent: {}",
report.diagnostics()
);
assert!(
!status.success(),
"unconfirmed cleanup must not be published as a successful completion"
);
assert!(
report.diagnostics().contains("cleanup budget expired"),
"diagnostics must be actionable: {}",
report.diagnostics()
);
}
/// Completion-grace termination of a cooperative group publishes confirmed
/// quiescence only after every owned member is gone.
#[cfg(unix)]
#[tokio::test]
async fn apply_completion_grace_termination_confirms_quiescent_group() {
let shared_state = Arc::new(Mutex::new(None));
let config = CommandQueueConfig {
acceptance_max_runtime_secs:
crate::config::defaults::DEFAULT_ACCEPTANCE_MAX_RUNTIME_SECS,
stagger_delay_ms: 0,
max_retries: 1,
retry_delay_ms: 50,
retry_error_patterns: vec![],
retry_if_duration_under_secs: 0,
inactivity_timeout_secs: 0,
inactivity_kill_grace_secs: 5,
inactivity_timeout_max_retries: 0,
strict_process_cleanup: true,
max_runtime_secs: 0,
};
let runner = AiCommandRunner::new(config, shared_state);
// Leader plus a descendant that outlives it until the group is swept.
let (mut handle, _rx) = runner
.execute_streaming_with_retry(
"sleep 300 >/dev/null 2>&1 </dev/null & sleep 300",
None,
Some("apply"),
Some("change-a"),
)
.await
.unwrap();
tokio::time::sleep(Duration::from_millis(200)).await;
let pgid = handle.id().expect("real pid") as i32;
handle.terminate().expect("terminate");
let _ = handle.wait().await;
let report = handle.process_group_cleanup().await;
assert!(
report.is_confirmed(),
"cooperative group must reach confirmed quiescence: {}",
report.diagnostics()
);
// Independently verify the published evidence against the real group.
let result = unsafe { libc::killpg(pgid, 0) };
if result == 0 {
unsafe { libc::killpg(pgid, libc::SIGKILL) };
panic!("cleanup reported quiescence while pgid {pgid} still has live members");
}
}
/// Regression test (task 1.8): cancellation via StreamingChildHandle triggers full
/// process-group cleanup. After terminate_with_timeout, no members should remain.
#[cfg(unix)]
#[tokio::test]
async fn test_post_completion_cleanup_on_cancellation() {
let shared_state = Arc::new(Mutex::new(None));
let config = CommandQueueConfig {
acceptance_max_runtime_secs:
crate::config::defaults::DEFAULT_ACCEPTANCE_MAX_RUNTIME_SECS,
stagger_delay_ms: 0,
max_retries: 1,
retry_delay_ms: 50,
retry_error_patterns: vec![],
retry_if_duration_under_secs: 0,
inactivity_timeout_secs: 0,
inactivity_kill_grace_secs: 5,
inactivity_timeout_max_retries: 0,
strict_process_cleanup: true,
max_runtime_secs: 0,
};
let runner = AiCommandRunner::new(config, shared_state);
// Long-running command: background a sleep then loop so the shell itself stays alive.
let (mut handle, _rx) = runner
.execute_streaming_with_retry(
"sleep 999 >/dev/null 2>&1 </dev/null & sleep 999",
None,
Some("test"),
None,
)
.await
.unwrap();
// Give the child time to start.
tokio::time::sleep(Duration::from_millis(150)).await;
let pid = handle.id().expect("should have a real PID") as i32;
// Cancel via the handle.
let outcome = handle
.terminate_with_timeout(Duration::from_secs(10))
.await
.unwrap();
assert!(
!matches!(
outcome,
crate::process_manager::TerminationOutcome::TimedOut
),
"Expected termination, not timeout"
);
// Allow OS signal delivery to settle.
tokio::time::sleep(Duration::from_millis(250)).await;
// The process group (PGID == PID for setsid'd process) should be fully gone.
let result = unsafe { libc::killpg(pid, 0) };
if result == 0 {
unsafe { libc::killpg(pid, libc::SIGKILL) };
panic!(
"post-cleanup: process group {} still has live members after cancellation",
pid
);
}
let errno = std::io::Error::last_os_error().raw_os_error().unwrap_or(0);
assert_eq!(
errno,
libc::ESRCH,
"post-cleanup: expected ESRCH for pgid={} after cancellation, got errno={}",
pid,
errno
);
}
}