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
//! The orchestrator's loop.
//!
//! One scheduler thread owns every decision, over the run's lanes — one per
//! SDD ([`Lane`]). Around it: a monitor thread per running worker (lease
//! renewal, the hard budget, the exit), and per lane one integrator thread
//! (the lane's integration checkout and its gate) and one lease keeper
//! (work waiting for the gate). They report through a single channel, each
//! message naming its lane; nothing but the scheduler changes the run's
//! state. What the lanes share is the scheduler's: the worker cap, the
//! gate pool, the job lock, the event log, the run record.
//!
//! Dispatch is continuous: whenever a slot is free, every claimable unit is
//! a candidate — any two are independent by construction (neither depends on
//! the other, each has its dependencies built, and ownership is exclusive) —
//! and each built unit goes to its lane's integrator as it reports, to be
//! gated with whatever else is ready. State is re-derived from the mainline
//! head, never remembered: what a unit's last attempt left is in git, what
//! a change to the spec staled is in the traces.
//!
//! Work is kept. A unit whose last attempt was built but never landed, and
//! whose inputs have not moved since, is **adopted**: claimed again and sent
//! to the gate with no worker session, taking no slot. A unit whose rows
//! fail on their gate's base too is **rotten**: it is reported with the
//! culprit commit a search finds (off the critical path, on a thread of its
//! own), never rebuilt, and the units that depend on it wait while the rest
//! of the lane keeps going.
//!
//! A project run is the same loop over several lanes. Each pass orders every
//! lane's ready units by their critical-path priority over the whole
//! repository's unit graph (ties by slug, then unit id) and dispatches while
//! workers are below the cap; `run.lane_cap` admits lanes highest-priority
//! work first. A lane whose Verified units a change in another lane reached
//! re-checks them — after the merge, once nothing they depend on has work
//! pending — in one batch with no worker. A blocked, set-aside or rotten unit
//! holds up only what depends on it: every lane keeps going.
use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::Ordering;
use std::sync::mpsc::{self, Receiver, RecvTimeoutError, Sender};
use std::time::{Duration, Instant};
use vitrify_core::cascade::{Anchors, Closures, Ownership};
use vitrify_core::claims::Op;
use vitrify_core::facf;
use vitrify_core::graph::{self as unit_graph, self, Node, Priorities};
use vitrify_core::model::{self, Instance, UtcTime};
use vitrify_core::trace::{self, TraceMap};
use vitrify_core::writeset::WriteSet;
use vitrify_core::{CoreError, journal, ledger};
use super::events::{EventLog, RunEvent};
use super::integrate::{self, HeadCheck, IntegrateRequest, Outcome, Rechecked, RottenRow};
use super::lane::{self, Active, Checkout, Event, Job, Lane, LaneId, Phase, RealLease};
use super::lease::{self, MonitorParams, MonitorVerdict};
use super::mainline::Mainline;
use super::project::{
self, LaneOutcome, LaneReport, LaneState, Plan, ProjectRecord, adopted_line, event_line,
};
use super::{
Ctx, LaneSeed, RunState, VERSION, WorkerRec, escalate, plan, retry, tail, worker,
write_run_json,
};
use crate::cmd::{brief, claim};
use crate::gate;
use crate::incremental::adopt;
use crate::incremental::blame::{PristineCheckout, ShellRunner};
use crate::incremental::culprit;
use crate::incremental::dirty::{Cascade, DiffCache, DirtySet, Mode, Snapshot};
use crate::layout;
use crate::out::{self, J, diag, emit};
use crate::signals;
/// How long the scheduler sleeps when nothing reports.
const TICK: Duration = Duration::from_secs(1);
enum End {
Complete,
Blocked,
/// The escalation judge said the job cannot safely continue.
Aborted(String),
Interrupted(i32),
}
struct Scheduler {
ctx: Ctx,
state: RunState,
started_at: String,
events_tx: Sender<Event>,
events: Receiver<Event>,
params: MonitorParams,
/// Held for the life of the run: a later run reads a free lock as
/// "that run is gone".
job_lock: Option<std::fs::File>,
/// One per SDD the run builds.
lanes: Vec<Lane>,
/// Workers started this run, across lanes: the `w<n>` of holder IDs.
worker_n: u32,
/// Attempts adopted this run: the `a<n>` of their claims' holder IDs.
adopt_n: u32,
last_derive: Instant,
dirty: bool,
abort: Option<String>,
failure: Option<String>,
/// Claims lost to a publish race since the last one that succeeded.
races_lost: u32,
/// This pass changed mainline without starting a worker (a claim made
/// and given back): the derived view is no longer current.
churned: bool,
/// The run's `events.jsonl`, appended to as things happen, so a reader
/// can see the history and not only the latest `run.json`.
log: EventLog,
/// The repository at the head the lanes last derived from.
repo: Option<Repo>,
/// What the dirty set's diffs found at the last head read.
diffs: DiffCache,
/// Derives again in this pass, for what arrived as it derived.
rederived: u8,
/// The plan the header showed; its lanes carry their current states.
plan: Plan,
/// Units of the planned critical path, in the order they landed.
critical_ran: Vec<Node>,
/// Estimated seconds per unit, when `run.priority = "history"`.
weights: Option<BTreeMap<Node, u64>>,
/// The last pass written to the event log: (working, idle slots,
/// ready units left waiting for a slot).
last_pass: Option<(usize, usize, usize)>,
}
/// The whole repository at one head, as every lane's decisions read it: its
/// unit graph, which Verified units a change reached, and the priorities.
struct Repo {
head: String,
snapshot: Snapshot,
/// `None` when the cascade could not be read.
dirty: Option<DirtySet>,
priorities: Option<Priorities>,
/// The cycles of the whole unit graph, when it has any: what is on one
/// or behind one is not dispatched.
cycles: Option<graph::Cycles>,
}
impl Repo {
/// Whether `n` is on a cycle, or waits behind one.
fn cyclic(&self, n: &Node) -> bool {
self.cycles
.as_ref()
.is_some_and(|c| c.waiting.contains(n) || c.cycles.iter().any(|p| p.contains(n)))
}
}
/// The first line of an attempt's reason, at most 200 chars, for
/// `run.json`; `None` when there is no reason (a verified attempt).
fn first_line_clipped(detail: &str) -> Option<String> {
let line = detail.lines().next().unwrap_or("");
if line.is_empty() {
return None;
}
Some(line.chars().take(200).collect())
}
pub(crate) fn orchestrate(ctx: Ctx, lanes: Vec<LaneSeed>, plan: Plan) -> Result<u8, CoreError> {
let mut s = Scheduler::start(ctx, lanes, plan)?;
let end = s.drive();
s.finish(end)
}
// ---------------------------------------------------------- the scheduler
impl Scheduler {
fn start(ctx: Ctx, seeds: Vec<LaneSeed>, plan: Plan) -> Result<Scheduler, CoreError> {
let io = |path: &Path| {
let path = path.to_path_buf();
move |source: std::io::Error| CoreError::Io { path, source }
};
ctx.layout.ensure_scratch()?;
std::fs::create_dir_all(&ctx.logs_dir).map_err(io(&ctx.logs_dir))?;
let log = EventLog::open(&ctx.logs_dir);
let jobs_dir = ctx.layout.scratch.join("jobs");
std::fs::create_dir_all(&jobs_dir).map_err(io(&jobs_dir))?;
let lock_path = jobs_dir.join(format!("{}.lock", ctx.job_id));
let job_lock = std::fs::OpenOptions::new()
.create(true)
.truncate(false)
.write(true)
.open(&lock_path)
.map_err(io(&lock_path))?;
// Nobody else can hold this name for long — a recovering run only
// probes a lock and lets go — so wait for it rather than run without.
job_lock.lock().map_err(io(&lock_path))?;
let params = MonitorParams {
lease_minutes: ctx.cfg.run.lease_minutes,
renew_fraction: ctx.cfg.run.renew_fraction,
worker_timeout_minutes: ctx.cfg.run.worker_timeout_minutes,
tick: TICK,
};
let (events_tx, events) = mpsc::channel::<Event>();
let mut lanes = Vec::with_capacity(seeds.len());
for (id, seed) in seeds.into_iter().enumerate() {
lanes.push(Lane::start(
id,
seed,
&ctx,
events_tx.clone(),
log.clone(),
¶ms,
)?);
}
Ok(Scheduler {
started_at: UtcTime::now().format(),
state: RunState::default(),
events_tx,
events,
params,
job_lock: Some(job_lock),
lanes,
worker_n: 0,
adopt_n: 0,
last_derive: Instant::now(),
dirty: true,
abort: None,
failure: None,
races_lost: 0,
churned: false,
log,
repo: None,
diffs: DiffCache::default(),
rederived: 0,
plan,
critical_ran: Vec::new(),
weights: ctx
.cfg
.run
.priority_by_history()
.then(|| super::history::weights(&ctx.layout.scratch)),
last_pass: None,
ctx,
})
}
/// What the run builds, as the run record names it: the lanes' SDDs.
fn sdd(&self) -> String {
self.lanes
.iter()
.map(Lane::sdd)
.collect::<Vec<_>>()
.join(", ")
}
fn record(&self, terminated: Option<(&str, &str)>) {
if let Err(e) = write_run_json(
&self.ctx,
&self.sdd(),
&self.state,
&self.started_at,
terminated,
) {
diag(&format!("warning: run.json not written: {e}"));
}
}
/// Write `project.json`: the plan, each lane's state as it is now, and
/// the critical path as it ran. `ended`: the run is over.
fn record_lanes(&mut self, ended: bool) {
for lane in &self.lanes {
let slug = lane.graph_slug();
if let Some(p) = self.plan.lanes.iter_mut().find(|p| p.slug == slug) {
p.state = lane.state;
// the counts as they stand now; what the run set out to
// regenerate, adopt and re-check stays the plan's
if !lane.views.is_empty() {
let live = lane.views.iter().filter(|v| v.status != "Retired");
p.units = live.clone().count();
p.verified = live.filter(|v| v.status == "Verified").count();
}
}
}
let record = ProjectRecord {
started_at: self.started_at.clone(),
ended_at: ended.then(|| UtcTime::now().format()),
plan: self.plan.clone(),
critical_path_ran: self.critical_ran.clone(),
};
if let Err(e) = record.write(&self.ctx.logs_dir) {
diag(&format!("warning: project.json not written: {e}"));
}
}
/// Bring each lane's state up to date — what it would be doing now —
/// and rewrite `project.json` when one changed.
fn update_states(&mut self, ready: &BTreeSet<LaneId>, due: &BTreeSet<LaneId>) {
let mut changed = false;
for (l, lane) in self.lanes.iter_mut().enumerate() {
let state = if !lane.blocked.is_empty() {
LaneState::Blocked
} else if lane.rechecking.is_some() {
LaneState::Recheck
} else if !lane.active.is_empty() || ready.contains(&l) {
LaneState::Build
} else if due.contains(&l) {
LaneState::Recheck
} else if lane
.views
.iter()
.any(|v| !matches!(v.status.as_str(), "Verified" | "Retired"))
{
LaneState::Idle
} else {
LaneState::UpToDate
};
if state != lane.state {
lane.state = state;
changed = true;
}
}
if changed {
self.record_lanes(false);
}
}
/// A line of lane `l`'s for stdout: in a project run it names the lane.
fn say(&self, l: LaneId, line: &str) -> String {
self.lanes[l].say(line)
}
/// Log a `waiting: …` line the run printed while it waits under `--wait`.
fn waiting(&self, line: &str) {
self.event(RunEvent::new(UtcTime::now(), "waiting", None, None, line));
}
/// Append one event to the run's event log.
fn event(&self, e: RunEvent) {
self.log.write(&e);
}
/// Append one event of lane `l` to the run's event log, naming the lane.
fn lane_event(&self, l: LaneId, e: RunEvent) {
self.log
.write(&e.with("lane", J::s(self.lanes[l].ml.slug.clone())));
}
/// Every lane's rotten units, as the unit graph names them.
fn rotten_nodes(&self) -> BTreeSet<Node> {
self.lanes
.iter()
.flat_map(|l| l.rotten.keys().map(|u| l.node(u)))
.collect()
}
/// The SDDs this run builds, by the slugs the unit graph names them by.
fn lane_slugs(&self) -> Vec<String> {
self.lanes.iter().map(Lane::graph_slug).collect()
}
fn limit(&self) -> u32 {
self.ctx.cfg.run.abort_failed_waves.max(1) as u32
}
fn lease_minutes(&self) -> i64 {
self.ctx.cfg.run.lease_minutes
}
/// Workers running now, across lanes: what the cap bounds.
fn working(&self) -> usize {
self.lanes.iter().map(Lane::working).sum()
}
// ------------------------------------------------------------ loop
fn drive(&mut self) -> Result<End, CoreError> {
// the run's own record exists before anything is dispatched
self.record(None);
self.record_lanes(false);
let sdd = self.sdd();
let harness = self.ctx.harness.clone();
let cap = self.ctx.cap;
let detail = if harness.is_empty() {
format!("{sdd} · cap {cap}")
} else {
format!("{sdd} · {harness} · cap {cap}")
};
self.event(
RunEvent::new(UtcTime::now(), "run-started", None, None, detail)
.with("cap", J::Int(cap as i64))
.with("harness", J::s(harness))
.with("sdd", J::s(sdd)),
);
self.recover_dead_runs()?;
// With no lane cap, every lane's dynamic preflight runs before the
// first pass, all at once, each on its own integrator's thread; under
// a cap, a lane's runs when it is admitted.
if self.ctx.project && self.ctx.cfg.run.lane_limit().is_none() {
for l in 0..self.lanes.len() {
if self.lanes[l].blocked.is_empty() {
self.ready_for_work(l, &[]);
}
}
while self.lanes.iter().any(|l| l.checkout == Checkout::Preparing)
&& signals::pending().is_none()
{
match self.events.recv_timeout(TICK) {
Ok(event) => self.handle(event)?,
Err(RecvTimeoutError::Timeout) => {}
Err(RecvTimeoutError::Disconnected) => break,
}
}
}
// what the preflight re-check found rotten
for l in 0..self.lanes.len() {
let rows = std::mem::take(&mut self.lanes[l].preflight_rotten);
if !rows.is_empty() {
self.on_rotten(l, rows);
}
}
loop {
if let Some(sig) = signals::pending() {
return Ok(End::Interrupted(sig));
}
if let Some(e) = self.failure.take() {
return Err(CoreError::refusal(1, e));
}
if let Some(reason) = self.abort.take() {
return Ok(End::Aborted(reason));
}
// nothing reported: look at mainline again every poll period —
// spec changes and lapsed leases arrive without an event
self.integrators_alive()?;
let poll = Duration::from_secs(self.ctx.cfg.run.poll_seconds.max(1) as u64);
if self.dirty || self.last_derive.elapsed() >= poll {
if !self.derive()? {
// mainline could not be brought up to date this time:
// nothing is decided on a view that may be stale
std::thread::sleep(Duration::from_millis(300));
self.dirty = true;
continue;
}
// Whatever a lane's threads reported before the head moved —
// a rotten verdict is sent before its batch is published —
// is taken in before anything is decided against it; and
// what was taken in may have moved the head again, so the
// view is derived anew before deciding.
let mut drained = false;
while let Ok(event) = self.events.try_recv() {
self.handle(event)?;
drained = true;
}
// (twice at most: lanes publishing back to back would
// otherwise keep the pass from ever deciding)
if drained && self.rederived < 2 && self.head_moved() {
self.rederived += 1;
self.dirty = true;
continue;
}
self.rederived = 0;
self.churned = false;
let dispatched = self.fill_slots()? + self.start_rechecks();
self.dirty = false;
// a culprit search still running has a finding to report
let idle = self.lanes.iter().all(|l| {
l.active.is_empty()
&& l.searches == 0
&& l.rechecking.is_none()
&& !matches!(l.checkout, Checkout::Preparing | Checkout::Releasing)
});
if idle && dispatched == 0 && self.abort.is_none() {
if self.churned {
// a claim was made and given back: derive again
// before deciding anything (bounded: each such
// attempt counts toward setting the unit aside)
self.dirty = true;
continue;
}
if (1..=3).contains(&self.races_lost) {
// a claim lost only to a busy mainline: look again
self.races_lost += 1;
std::thread::sleep(Duration::from_millis(300));
self.dirty = true;
continue;
}
if let Some(end) = self.conclude()? {
return Ok(end);
}
}
}
match self.events.recv_timeout(TICK) {
Ok(event) => {
self.handle(event)?;
while let Ok(event) = self.events.try_recv() {
self.handle(event)?;
}
self.dirty = true;
}
Err(RecvTimeoutError::Timeout) => {}
Err(RecvTimeoutError::Disconnected) => {
return Err(CoreError::refusal(
1,
"error: the orchestrator's threads are gone",
));
}
}
}
}
/// Derive every lane's view from the mainline head ([`Lane::derive`]).
/// `false` = mainline was too busy to be put right for one of them.
fn derive(&mut self) -> Result<bool, CoreError> {
for l in 0..self.lanes.len() {
if l > 0 {
let (read, rest) = self.lanes.split_at_mut(l);
rest[0].share_graph(&read[0]);
}
let lane = &mut self.lanes[l];
if !lane.derive()? {
return Ok(false);
}
}
self.last_derive = Instant::now();
self.read_repo();
self.join_new_lanes()?;
Ok(true)
}
/// Whether mainline moved past the head the lanes were derived at.
fn head_moved(&self) -> bool {
let Some(lane) = self.lanes.first() else {
return false;
};
lane.ml.head().map_or(true, |h| h != lane.head)
}
/// In a project run over every Active SDD, an SDD that became Active on
/// mainline mid-run joins as a lane, after its own preflight.
fn join_new_lanes(&mut self) -> Result<(), CoreError> {
if !self.ctx.project || self.ctx.upstream {
return Ok(());
}
let Some(repo) = &self.repo else {
return Ok(());
};
let joining: Vec<String> = repo
.snapshot
.sdds
.iter()
.filter(|s| s.inst.meta.get_str_or("status", "Draft") == "Active")
.filter(|s| !self.lanes.iter().any(|l| l.graph_slug() == s.slug))
.map(|s| s.root.clone())
.collect();
for root in joining {
let rel = std::path::PathBuf::from(&root);
// the primary checkout follows mainline: its copy is the one
// preflight judges, as at the start
let Ok(inst) =
crate::cmd::resolve_sdd_in(&self.ctx.primary.dir, &rel).and_then(Instance::load)
else {
continue;
};
let slug = crate::layout::sdd_slug(&inst);
let mut seed = LaneSeed {
ml: self.ctx.mainline_for(rel, slug.clone()),
inst,
cap: usize::MAX,
rotten: Vec::new(),
blocked: Vec::new(),
checked: Vec::new(),
suite_ran: false,
};
if seed.inst.shape == vitrify_core::model::Shape::Single {
seed.cap = 1;
}
// its static preflight here; its dynamic one, like every lane's,
// on its integrator's thread when it is admitted
let mut own: Vec<String> = Vec::new();
if !super::lane_static(&self.ctx, &seed, &mut own, false)? {
return Ok(());
}
for f in &own {
diag(&event_line(&slug, &format!("preflight failed: {f}")));
}
seed.blocked = own;
let id = self.lanes.len();
let lane = Lane::start(
id,
seed,
&self.ctx,
self.events_tx.clone(),
self.log.clone(),
&self.params,
)?;
emit(&lane.say("joins the run: its SDD is Active"));
self.plan.lanes.push(project::Lane {
slug: lane.graph_slug(),
root: lane.sdd(),
records: format!(".vitrify/logs/{}/{}/", self.ctx.job_id, lane.ml.slug),
state: lane.state,
units: 0,
verified: 0,
regenerate: 0,
adopt: 0,
recheck: 0,
});
self.lanes.push(lane);
self.lanes[id].derive()?;
self.recover_lane(id);
self.record_lanes(false);
self.record(None);
}
Ok(())
}
/// Read the whole repository at the head the first lane derived from —
/// once per head: its graph and closures, which Verified units a change
/// reached since their last recorded check, and the priorities.
fn read_repo(&mut self) {
let Some(first) = self.lanes.first() else {
return;
};
let head = first.head.clone();
let git = first.ml.primary.at(&first.head_wt);
// (Every head is read again: a commit of protocol state alone — a
// re-check's record, an integration's journal — moves the anchors
// the dirty set is computed from.)
if self.repo.as_ref().is_some_and(|r| r.head == head) {
return;
}
// an unreadable SDD is left out, and so are the SDDs on a cycle:
// the rest still cascade, re-check and rank
let (snapshot, cycles) = match Snapshot::load_resolvable(&git, &first.head_wt, &head) {
Ok(s) => s,
Err(e) => {
diag(&format!(
"note: the repository could not be read whole ({e})"
));
self.repo = None;
return;
}
};
let dirty = match snapshot.dirty_with(&git, Mode::Changes, &mut self.diffs) {
Ok(Cascade::Resolved(set)) => Some(set),
_ => None,
};
let priorities = snapshot
.resolved
.as_ref()
.ok()
.map(|r| plan::priorities(&snapshot.graph, r, self.weights.as_ref()));
let known = self.repo.as_ref().and_then(|r| r.cycles.as_ref());
if let Some(c) = &cycles
&& known.is_none_or(|k| k.cycles != c.cycles)
{
for path in &c.cycles {
diag(&format!(
"a dependency cycle on mainline: {} — nothing on it or behind it is dispatched",
unit_graph::render_path(path)
));
}
}
self.repo = Some(Repo {
head,
snapshot,
dirty,
priorities,
cycles,
});
}
/// The units Verified at the repository's head: finished, for what
/// depends on them.
fn finished(&self, n: &Node) -> bool {
self.repo
.as_ref()
.is_some_and(|r| plan::finished(&r.snapshot.graph, n))
}
/// Whether lane `l` can take work now: admitted under `run.lane_cap`
/// and its checkout ready. A lane admitted for the first time has its
/// dynamic preflight started on its integrator's thread — it takes work
/// once that passes; one admitted again has its checkout made at its
/// first use.
fn ready_for_work(&mut self, l: LaneId, picked: &[(LaneId, String, bool)]) -> bool {
if !self.admitted_with(l, picked) {
return false;
}
let lane = &mut self.lanes[l];
match lane.checkout {
Checkout::Ready => true,
Checkout::Preparing | Checkout::Releasing => false,
Checkout::Absent if lane.prepared => {
lane.checkout = Checkout::Ready;
true
}
Checkout::Absent => {
lane.checkout = Checkout::Preparing;
let _ = lane.jobs.send(Job::Prepare);
false
}
}
}
/// With `run.lane_cap`, a lane with nothing to do — no claim, no
/// re-check, no culprit search, nothing ready or due — gives its
/// checkouts back, so the next lane by priority can be admitted.
fn release_idle(&mut self, ready: &BTreeSet<LaneId>, due: &BTreeSet<LaneId>) {
if self.ctx.cfg.run.lane_limit().is_none() || !self.ctx.project {
return;
}
for l in 0..self.lanes.len() {
let lane = &mut self.lanes[l];
if lane.checkout == Checkout::Ready
&& lane.active.is_empty()
&& lane.rechecking.is_none()
&& lane.searches == 0
&& !ready.contains(&l)
&& !due.contains(&l)
{
lane.checkout = Checkout::Releasing;
let _ = lane.jobs.send(Job::Release);
}
}
}
/// Whether lane `l` may start more work under `run.lane_cap`, counting
/// as active too the lanes of `picked`, units this pass has picked and
/// not yet claimed.
fn admitted_with(&self, l: LaneId, picked: &[(LaneId, String, bool)]) -> bool {
// a lane counts while it holds checkouts, or has work in flight
let active = |x: LaneId| {
let lane = &self.lanes[x];
lane.checkout != Checkout::Absent
|| !lane.active.is_empty()
|| lane.rechecking.is_some()
|| picked.iter().any(|p| p.0 == x)
};
if active(l) {
return true;
}
let n = (0..self.lanes.len()).filter(|&x| active(x)).count();
self.ctx.cfg.run.lane_limit().is_none_or(|cap| n < cap)
}
/// Lane `l`'s dirty Verified units due a re-check: a change reached them
/// since their last recorded check, and nothing they depend on has work
/// pending — unfinished, in flight, dirty or rotten. Re-checks follow
/// dependency order this way: upstream first.
fn due_rechecks(&self, l: LaneId) -> Vec<String> {
let lane = &self.lanes[l];
let Some(repo) = &self.repo else {
return Vec::new();
};
let Some(dirty) = &repo.dirty else {
return Vec::new();
};
if lane.head != repo.head || !lane.blocked.is_empty() {
return Vec::new();
}
let rotten = self.rotten_nodes();
let in_flight: BTreeSet<Node> = self
.lanes
.iter()
.flat_map(|x| x.active.keys().map(|u| x.node(u)))
.collect();
let slug = lane.graph_slug();
// dirty, or — under --recheck — Verified and not checked this run
let mut wanted: Vec<String> = dirty.of_sdd(&slug).map(|d| d.node.unit.clone()).collect();
if self.ctx.recheck {
for v in lane.views.iter().filter(|v| v.status == "Verified") {
if !lane.checked.contains(&v.unit) && !wanted.contains(&v.unit) {
wanted.push(v.unit.clone());
}
}
wanted.sort();
}
wanted
.into_iter()
.filter(|unit| {
!lane.rotten.contains_key(unit)
&& !lane.active.contains_key(unit)
&& lane.rechecked_at.get(unit) != Some(&lane.head)
// what it depends on in another SDD is settled first;
// what it depends on in its own is re-checked with it
&& !lane.closure(unit).iter().any(|n| {
!self.finished(n)
|| in_flight.contains(n)
|| rotten.contains(n)
|| (n.slug != slug && self.needs_check(n, dirty))
})
})
.collect()
}
/// Whether `n` must be checked again before what depends on it is: a
/// change reached it, or — under `--recheck` — it is a lane's Verified
/// unit this run has not checked yet.
fn needs_check(&self, n: &Node, dirty: &DirtySet) -> bool {
if dirty.is_dirty(n) {
return true;
}
self.ctx.recheck
&& self.lanes.iter().any(|l| {
l.graph_slug() == n.slug
&& !l.checked.contains(&n.unit)
&& l.views
.iter()
.any(|v| v.unit == n.unit && v.status == "Verified")
})
}
/// Under `--recheck`, whether lane `l`'s §5 Test command is still to run
/// this run while nothing else will run it: every unit of the lane is
/// built, and no unit of it is due a re-check.
fn suite_due(&self, l: LaneId) -> bool {
let lane = &self.lanes[l];
let wanted = |v: &&vitrify_core::model::UnitView| {
v.status == "Verified"
&& self
.repo
.as_ref()
.and_then(|r| r.dirty.as_ref())
.is_some_and(|d| self.needs_check(&lane.node(&v.unit), d))
};
self.ctx.recheck
&& !lane.suite_ran
&& lane.blocked.is_empty()
&& lane.active.is_empty()
&& lane.rechecked_at.get("") != Some(&lane.head)
&& !lane.views.is_empty()
&& lane
.views
.iter()
.all(|v| matches!(v.status.as_str(), "Verified" | "Retired"))
// a batch of its units would run the suite anyway
&& !lane.views.iter().any(|v| wanted(&v))
}
/// Start a re-check batch in every lane with units due one and nothing
/// of its own at the gate (a gate re-checks what is dirty anyway).
/// Returns how many batches started.
fn start_rechecks(&mut self) -> usize {
let mut started = 0;
for l in 0..self.lanes.len() {
let lane = &self.lanes[l];
if lane.rechecking.is_some()
|| lane.active.values().any(|a| a.phase == Phase::Integrating)
|| signals::interrupted()
|| self.abort.is_some()
{
continue;
}
let due = self.due_rechecks(l);
if (due.is_empty() && !self.suite_due(l)) || !self.ready_for_work(l, &[]) {
continue;
}
let line = if due.is_empty() {
"re-check the suite".to_string()
} else {
format!("re-check {}", due.join(", "))
};
emit(&self.say(l, &line));
self.lane_event(
l,
RunEvent::new(UtcTime::now(), "recheck", None, None, line).with(
"units",
J::Arr(due.iter().map(|u| J::s(u.clone())).collect()),
),
);
let lane = &mut self.lanes[l];
lane.rechecking = Some(due.clone());
let _ = lane.jobs.send(Job::Recheck(due));
started += 1;
}
started
}
/// Lane `l`'s dynamic preflight ended: it takes work now, or it is
/// blocked — its units, and every unit that depends on them, wait.
fn on_prepared(
&mut self,
l: LaneId,
result: Result<Option<Vec<String>>, CoreError>,
) -> Result<(), CoreError> {
let Some(failures) = result? else {
// interrupted: the run is ending
self.lanes[l].checkout = Checkout::Absent;
return Ok(());
};
self.dirty = true;
let lane = &mut self.lanes[l];
lane.prepared = true;
lane.checkout = Checkout::Ready;
if !failures.is_empty() {
for f in &failures {
diag(&lane.say(&format!("preflight failed: {f}")));
}
lane.blocked = failures;
lane.state = LaneState::Blocked;
}
Ok(())
}
/// A re-check batch of lane `l` ended: its passes are recorded, its
/// failures are rotten.
fn on_rechecked(&mut self, l: LaneId, result: Result<Rechecked, CoreError>) {
let units = self.lanes[l].rechecking.take().unwrap_or_default();
let head = self.lanes[l].head.clone();
// ("" stands for the suite: a batch of no unit is the suite's)
if units.is_empty() {
self.lanes[l]
.rechecked_at
.insert(String::new(), head.clone());
}
for u in &units {
self.lanes[l].rechecked_at.insert(u.clone(), head.clone());
}
let r = match result {
Ok(r) => r,
Err(e) => {
self.failure = Some(e.to_string());
return;
}
};
if r.interrupted {
return;
}
let lane = &mut self.lanes[l];
lane.suite_ran |= r.suite_ran;
if r.broken.is_none() {
lane.checked.extend(units.iter().cloned());
}
if let Some((cmd, output)) = &r.broken {
diag(&self.say(
l,
&format!(
"re-check could not run: `{cmd}` failed: {}",
tail(output, 3)
),
));
return;
}
if !r.passed.is_empty() {
self.lanes[l].rechecked += r.passed.len();
let line = format!("re-checked {} at {}", r.passed.join(", "), short(&r.commit));
emit(&self.say(l, &line));
self.lane_event(
l,
RunEvent::new(UtcTime::now(), "rechecked", None, None, line)
.with("recorded", J::Bool(r.recorded)),
);
}
if !r.rotten.is_empty() {
self.on_rotten(l, r.rotten);
}
}
fn integrators_alive(&self) -> Result<(), CoreError> {
self.lanes.iter().try_for_each(Lane::integrator_alive)
}
/// Start a worker for each claimable unit while slots are free.
/// A built attempt of a claimable unit goes to the gate with no worker
/// and takes no slot; every other claimable unit gets a worker while
/// slots are free. A unit that depends on a rotten one, in any SDD, is
/// not built while it fails.
fn fill_slots(&mut self) -> Result<usize, CoreError> {
let mut dispatched = 0;
let rotten = self.rotten_nodes();
let mut candidates: Vec<(LaneId, String, bool, Node)> = self
.lanes
.iter()
.enumerate()
.filter(|(_, lane)| lane.blocked.is_empty())
.flat_map(|(l, lane)| {
let rotten = &rotten;
lane.views
.iter()
.filter(move |v| {
v.claimable
&& !lane.active.contains_key(&v.unit)
&& !lane.parked.contains_key(&v.unit)
&& lane.rotten_dependency(&v.unit, rotten).is_none()
})
.map(move |v| (l, v.unit.clone(), v.status == "Stale", lane.node(&v.unit)))
})
.collect();
// nothing on a cycle, or behind one, is dispatched
if let Some(repo) = &self.repo {
candidates.retain(|c| !repo.cyclic(&c.3));
}
// critical path first: priority descending, then slug, then unit id
match self.repo.as_ref().and_then(|r| r.priorities.as_ref()) {
Some(p) => candidates.sort_by(|a, b| p.cmp(&a.3, &b.3)),
None => candidates.sort_by(|a, b| a.3.cmp(&b.3)),
}
self.set_lane_priorities();
let ready: BTreeSet<LaneId> = candidates.iter().map(|c| c.0).collect();
let mut waiting = 0;
let mut picks: Vec<(LaneId, String, bool)> = Vec::new();
for (l, unit, was_stale, _) in candidates {
if signals::interrupted() || self.abort.is_some() {
break;
}
if !self.ready_for_work(l, &picks) {
continue;
}
if let Some(adopted) = self.adopt(l, &unit)? {
dispatched += usize::from(adopted);
continue;
}
// the cap bounds running workers; a unit waiting for the gate
// holds a lease, not a slot
let picked_in = |l: LaneId| {
picks
.iter()
.filter(|p: &&(LaneId, String, bool)| p.0 == l)
.count()
};
if self.working() + picks.len() >= self.ctx.cap {
waiting += 1;
continue;
}
if self.lanes[l].working() + picked_in(l) >= self.lanes[l].cap {
continue;
}
picks.push((l, unit, was_stale));
}
dispatched += self.dispatch(picks)?;
let due: BTreeSet<LaneId> = (0..self.lanes.len())
.filter(|&l| !self.due_rechecks(l).is_empty())
.collect();
self.update_states(&ready, &due);
self.release_idle(&ready, &due);
self.record_pass(waiting);
Ok(dispatched)
}
/// Record a pass in the event log when it differs from the last: the
/// workers running, the slots idle, and the ready units left waiting
/// for a slot. A slot is idle only when nothing ready waits for one.
fn record_pass(&mut self, waiting: usize) {
if !self.ctx.project {
return;
}
let working = self.working();
let idle = self.ctx.cap.saturating_sub(working);
let pass = (working, idle, waiting);
if self.last_pass == Some(pass) {
return;
}
self.last_pass = Some(pass);
self.event(
RunEvent::new(
UtcTime::now(),
"pass",
None,
None,
format!("{working} working · {idle} idle · {waiting} waiting"),
)
.with("cap", J::Int(self.ctx.cap as i64))
.with("idle", J::Int(idle as i64))
.with("waiting", J::Int(waiting as i64))
.with("working", J::Int(working as i64)),
);
}
/// Each lane's gate commands wait for the gate pool at the priority of
/// its highest-priority unfinished unit.
fn set_lane_priorities(&self) {
let Some(p) = self.repo.as_ref().and_then(|r| r.priorities.as_ref()) else {
return;
};
for lane in &self.lanes {
let best = lane
.views
.iter()
.filter_map(|v| p.of_weighted(&lane.node(&v.unit)))
.max()
.unwrap_or(0);
lane.priority
.store(i64::try_from(best).unwrap_or(i64::MAX), Ordering::Relaxed);
}
}
/// Adopt `unit`'s built attempt, if it has one that can go to the gate
/// as it is: claim the unit, make the attempt the claim's branch, and
/// queue it for the gate. `None` = nothing to adopt (the unit goes to a
/// worker); `Some(true)` = adopted; `Some(false)` = an adoptable attempt
/// that did not get through (the claim was lost, or given back).
fn adopt(&mut self, l: LaneId, unit: &str) -> Result<Option<bool>, CoreError> {
let lane = &self.lanes[l];
let Some(inst) = &lane.inst else {
return Ok(None);
};
if lane.not_adoptable.contains(unit) {
return Ok(None);
}
let attempt = match adopt::adoptable(&lane.ml.primary, inst, &lane.head_wt, unit) {
// Only an attempt a run set aside — the run that made it, as it
// ended, or the next one, recovering a run that died — is known
// to have no worker left. One whose lease merely lapsed may
// still have its worker writing: it goes to a worker of its own.
Ok(Ok(attempt)) if set_aside(&lane.ml.primary, &attempt.commit) => attempt,
other => {
if let Err(e) = other {
diag(&format!(
"note: {unit}: its last attempt could not be looked at ({e}); it goes to a worker"
));
}
self.lanes[l].not_adoptable.insert(unit.to_string());
return Ok(None);
}
};
self.adopt_n += 1;
let holder = format!("{}-a{}", self.ctx.job_id, self.adopt_n);
let lease = self.lease_minutes();
let outcome = match lane.ml.claim(Op::Claim, unit, &holder, lease) {
Ok(o) => o,
Err(e) if matches!(e.exit_code(), 4 | 5) => {
diag(&format!("claim lost for {unit}: {e}"));
if e.exit_code() == 5 {
self.races_lost += 1;
}
return Ok(Some(false));
}
Err(e) => return Err(e),
};
self.races_lost = 0;
let seq = outcome.claim_seq;
let slug = lane.ml.slug.clone();
let wt = lane.ml.layout.unit_worktree(&slug, unit, seq);
let branch =
match adopt::relabel(&lane.ml.primary, &slug, &attempt, seq, &outcome.commit, &wt) {
Ok(branch) => branch,
Err(e) => {
let why = e.to_string();
let why = why.strip_prefix("error: ").unwrap_or(&why).to_string();
diag(&format!("{unit} (c{seq}): {why}"));
self.lanes[l].release(unit, &holder, lease);
self.lanes[l]
.attempts
.push((unit.to_string(), "not-adopted".to_string(), why));
self.lanes[l].not_adoptable.insert(unit.to_string());
self.count_failure(l, unit, "not-adopted", false)?;
self.churned = true;
return Ok(Some(false));
}
};
let expires = outcome
.claim_expires
.as_deref()
.and_then(UtcTime::parse)
.unwrap_or_else(|| UtcTime::now().add_minutes(lease));
let started = UtcTime::now();
self.state.workers.push(WorkerRec {
lane: slug.clone(),
holder: holder.clone(),
pid: 0,
unit: unit.to_string(),
claim_seq: seq,
branch: branch.clone(),
worktree: wt.display().to_string(),
started_at: started.format(),
ended_at: None,
outcome: "integrating".to_string(),
retry: false,
detail: None,
});
let rec = self.state.workers.len() - 1;
let lane = &mut self.lanes[l];
lane.keeper.hold(unit, &holder, expires);
lane.active.insert(
unit.to_string(),
Active {
holder: holder.clone(),
seq,
branch,
wt,
pid: None,
out_path: PathBuf::new(),
_tmp: None,
trace: attempt.trace.clone(),
phase: Phase::Integrating,
rec,
killed: None,
},
);
emit(&adopted_line(&slug, unit, attempt.seq));
self.lane_event(
l,
RunEvent::new(
started,
"adopted",
Some(unit),
Some(seq),
format!("built attempt c{} → gate", attempt.seq),
)
.with("attempt", J::Int(attempt.seq))
.with("holder", J::s(holder.clone())),
);
self.record(None);
let _ = self.lanes[l].jobs.send(Job::Integrate(IntegrateRequest {
unit: unit.to_string(),
token_seq: seq,
builder_holder: holder,
integrator_id: format!("{}-integrator", self.ctx.job_id),
}));
Ok(Some(true))
}
/// Set the attempt of `unit`'s claim aside on its branch, with why: its
/// worktree's files committed and the worktree removed. An adopted
/// attempt has no worktree; its branch gets the note alone, so a later
/// run sees the verdict and does not adopt it again.
fn abandon(&self, l: LaneId, unit: &str, label: &str, detail: &str) {
let lane = &self.lanes[l];
let Some(a) = lane.active.get(unit) else {
return;
};
let primary = &lane.ml.primary;
if a.pid.is_none()
&& !a.wt.exists()
&& primary.branch_exists(&a.branch).unwrap_or(false)
&& primary.worktree_add(&a.wt, &a.branch, false).is_err()
{
return;
}
retry::abandon(primary, &a.wt, unit, a.seq, label, detail);
}
// -------------------------------------------------------- dispatch
/// Claim `unit` of lane `l`, give it a worktree, start its worker.
fn dispatch(&mut self, picks: Vec<(LaneId, String, bool)>) -> Result<usize, CoreError> {
if picks.is_empty() {
return Ok(0);
}
// every unit this pass picked, claimed in one mainline transaction:
// one commit for them all, each with its own token and event
let lease = self.lease_minutes();
let mut holders: Vec<(u32, String)> = Vec::new();
let mut batch: Vec<claim::BatchClaim> = Vec::new();
for (l, unit, _) in &picks {
self.worker_n += 1;
let holder = format!("{}-w{}", self.ctx.job_id, self.worker_n);
holders.push((self.worker_n, holder.clone()));
batch.push(claim::BatchClaim {
sdd_rel: self.lanes[*l].ml.sdd_rel.clone(),
unit: unit.clone(),
holder,
});
}
let results = claim::claim_many(
&self.ctx.primary,
self.ctx.push,
&self.ctx.branch,
lease,
&batch,
)?;
// a pass that lost the race for mainline counts once, however many
// units it had picked; one that claimed anything resets the count
let busy = results
.iter()
.any(|r| r.as_ref().is_err_and(|e| e.exit_code() == 5));
let claimed = results.iter().any(Result::is_ok);
if claimed {
self.races_lost = 0;
} else if busy {
self.races_lost += 1; // mainline was busy, not the units taken
}
let mut started = 0;
for (((l, unit, was_stale), (n, holder)), result) in
picks.into_iter().zip(holders).zip(results)
{
let outcome = match result {
Ok(o) => o,
Err(e) if matches!(e.exit_code(), 4 | 5) => {
diag(&format!("claim lost for {unit}: {e}"));
continue;
}
Err(e) => return Err(e),
};
if self.start_claimed(l, &unit, was_stale, n, holder, outcome)? {
started += 1;
}
}
Ok(started)
}
/// Start the worker of a unit this run has just claimed: its worktree,
/// what the last attempt left, the prompt, the process.
fn start_claimed(
&mut self,
l: LaneId,
unit: &str,
was_stale: bool,
n: u32,
holder: String,
outcome: claim::ClaimOutcome,
) -> Result<bool, CoreError> {
let lease = self.lease_minutes();
let lane = &self.lanes[l];
let seq = outcome.claim_seq;
let slug = lane.ml.slug.clone();
let branch = layout::unit_branch(&slug, unit, seq);
let wt = lane.ml.layout.unit_worktree(&slug, unit, seq);
let primary = lane.ml.primary.clone();
// Both names belong to this claim alone. If either is taken, it is
// by something this run did not make: leave it exactly as it is.
let taken = if primary.branch_exists(&branch).map_err(CoreError::from)? {
Some(format!("branch {branch}"))
} else if wt.exists() {
Some(format!("worktree {}", wt.display()))
} else {
None
};
if let Some(what) = taken {
let why = format!(
"{what} already exists and is not this run's — left untouched; remove or rename it if it is not wanted"
);
diag(&format!("{unit} (c{seq}): {why}"));
self.lanes[l].release(unit, &holder, lease);
self.lanes[l]
.attempts
.push((unit.to_string(), "name-taken".to_string(), why));
self.count_failure(l, unit, "name-taken", false)?;
// mainline has moved (a claim and its release): what this pass
// derived is no longer the state to conclude on
self.churned = true;
return Ok(false);
}
let expires = outcome
.claim_expires
.as_deref()
.and_then(UtcTime::parse)
.unwrap_or_else(|| UtcTime::now().add_minutes(lease));
match self.launch(
l,
unit,
&holder,
seq,
&branch,
&wt,
&outcome.commit,
was_stale,
) {
Ok((handle, tmp, trace, retried)) => {
let pid = handle.pid;
let out_path = handle.out_path.clone();
let started = UtcTime::now();
self.state.workers.push(WorkerRec {
lane: slug,
holder: holder.clone(),
pid,
unit: unit.to_string(),
claim_seq: seq,
branch: branch.clone(),
worktree: wt.display().to_string(),
started_at: started.format(),
ended_at: None,
outcome: "running".to_string(),
retry: retried,
detail: None,
});
let rec = self.state.workers.len() - 1;
self.monitor(l, handle, unit, &holder, expires);
self.lanes[l].active.insert(
unit.to_string(),
Active {
holder: holder.clone(),
seq,
branch,
wt,
pid: Some(pid),
out_path,
_tmp: Some(tmp),
trace,
phase: Phase::Working,
rec,
killed: None,
},
);
emit(&self.say(
l,
&format!(
"dispatch {unit} (c{seq}) → w{}{}",
n,
if retried { " [retry]" } else { "" }
),
));
self.lane_event(
l,
RunEvent::new(
started,
"dispatched",
Some(unit),
Some(seq),
format!("→ w{}{}", n, if retried { " (retry)" } else { "" }),
)
.with("holder", J::s(holder.clone()))
.with("retry", J::Bool(retried)),
);
self.record(None);
Ok(true)
}
Err(e) => {
// the worktree and branch are this claim's own, made a
// moment ago and holding nothing: take them back
if wt.join(".git").exists() {
let _ = primary.worktree_remove(&wt);
}
let _ = primary.delete_branch(&branch);
self.lanes[l].release(unit, &holder, lease);
if e.exit_code() != 4 {
return Err(e);
}
// The unit cannot be handed to a worker as the spec stands
// at its claim commit — its reading set does not resolve: a
// pinned contract whose authority has moved on since
// preflight, a consumed contract that is gone. No attempt
// would change that, and it is no reason to stop building
// the rest: the unit waits for the spec.
let text = e.to_string();
let why = text
.lines()
.map(|l| l.strip_prefix("error: ").unwrap_or(l))
.collect::<Vec<_>>()
.join("; ");
diag(&format!(
"{unit} (c{seq}): not started — {why}; set aside until the spec changes"
));
self.lanes[l].attempts.push((
unit.to_string(),
"not-started".to_string(),
why.clone(),
));
self.lanes[l].park(unit, format!("its reading set does not resolve: {why}"))?;
// mainline has moved (a claim and its release)
self.churned = true;
Ok(false)
}
}
}
/// The worktree, what the last attempt left, the prompt, the process.
#[allow(clippy::too_many_arguments)]
fn launch(
&self,
l: LaneId,
unit: &str,
holder: &str,
seq: i64,
branch: &str,
wt: &Path,
claim_commit: &str,
was_stale: bool,
) -> Result<(worker::WorkerHandle, gate::TmpSlot, TraceMap, bool), CoreError> {
let lane = &self.lanes[l];
let ml = &lane.ml;
if let Some(parent) = wt.parent() {
std::fs::create_dir_all(parent).map_err(|source| CoreError::Io {
path: parent.to_path_buf(),
source,
})?;
}
ml.primary
.worktree_add_branch(wt, branch, claim_commit)
.map_err(CoreError::from)?;
// everything the worker is told is read from its own worktree, at
// the claim commit it was forked from
let inst = ml.sdd_in(wt)?;
let Some(u) = inst.unit(unit) else {
return Err(CoreError::refusal(
4,
format!("error: unit not found at the claim commit: {unit}"),
));
};
let (trace_at_claim, _) = trace::compute(&inst, u)?;
// what the last attempt left: its owned files, and why it failed
let prev = retry::previous_attempt(&ml.primary, &inst, unit, seq)?;
let mut seeded: Vec<String> = Vec::new();
if let Some(prev) = &prev {
let ws = WriteSet::of(&inst, u, wt)?;
match retry::seed(&ml.primary.at(wt), &ws, claim_commit, prev) {
Ok(paths) => seeded = paths,
Err(e) => diag(&format!(
"warning: {unit}: could not carry over the files of {} ({e}); the worker starts without them",
prev.branch
)),
}
}
let stale_cause = if was_stale {
Some(stale_cause(&inst, unit))
} else {
None
};
let sdd = lane.sdd();
let session = retry::section(
Some(&retry::Orchestrated {
unit,
holder,
seq,
branch,
worktree: wt,
sdd: &sdd,
}),
prev.as_ref(),
&seeded,
stale_cause.as_deref(),
);
let prompt =
brief::build_worker_prompt_at(wt, unit, holder, &ml.sdd_rel, &ml.layout, &session)?;
let _ = std::fs::write(
lane.logs_dir.join(format!("worker-{holder}.prompt.md")),
&prompt,
);
// an attempt after a failed one runs under the retry profile
let argv = if prev.is_some() {
&self.ctx.retry_argv
} else {
&self.ctx.worker_argv
};
// a temp directory no other worker of this repository is using
let tmp = gate::tmp_slot(&ml.layout.wt_root, "w").map_err(|source| CoreError::Io {
path: ml.layout.wt_root.clone(),
source,
})?;
let handle = worker::spawn_in(
argv,
&prompt,
wt,
&lane.logs_dir,
holder,
unit,
Some(&tmp.dir),
)?;
Ok((handle, tmp, trace_at_claim, prev.is_some()))
}
/// One thread per worker: renew its lease, enforce the hard budget,
/// report its exit.
fn monitor(
&self,
l: LaneId,
handle: worker::WorkerHandle,
unit: &str,
holder: &str,
expires: UtcTime,
) {
let tx = self.events_tx.clone();
let params = self.params.clone();
let backend = RealLease {
ml: self.lanes[l].ml.clone(),
lease_minutes: self.lease_minutes(),
};
let unit = unit.to_string();
let holder = holder.to_string();
std::thread::spawn(move || {
let mut child = handle.child;
let pid = handle.pid;
let (verdict, expires) = lease::monitor_worker_tracked(
lease::MonitorTarget {
child: &mut child,
pid,
unit: &unit,
holder: &holder,
expires,
},
¶ms,
&lease::RealClock,
&backend,
);
let _ = tx.send(Event::Exited {
lane: l,
unit,
verdict,
expires,
});
});
}
// ---------------------------------------------------------- events
fn handle(&mut self, event: Event) -> Result<(), CoreError> {
match event {
Event::Exited {
lane,
unit,
verdict,
expires,
} => self.on_exit(lane, &unit, verdict, expires),
Event::Integrated {
lane,
unit,
outcome,
} => self.on_integrated(lane, &unit, outcome),
Event::IntegratorFailed { lane, error } => {
// the run ends in an error; the lane did not finish
self.lanes[lane].state = LaneState::Blocked;
self.failure = Some(error);
Ok(())
}
// only `conclude` asks for one, and it takes the answer itself
Event::HeadChecked { .. } => Ok(()),
Event::Rotten { lane, rows } => {
self.on_rotten(lane, rows);
Ok(())
}
Event::Rechecked { lane, result } => {
self.on_rechecked(lane, *result);
Ok(())
}
Event::Prepared { lane, result } => self.on_prepared(lane, *result),
Event::Released { lane } => {
self.lanes[lane].checkout = Checkout::Absent;
self.dirty = true;
Ok(())
}
Event::Culprit { lane, unit, report } => {
self.on_culprit(lane, &unit, report);
Ok(())
}
}
}
/// Rows of lane `l` that fail on their gate's base too: their units are
/// rotten. Each row's culprit is searched for on a thread of its own.
fn on_rotten(&mut self, l: LaneId, rows: Vec<RottenRow>) {
let slug = self.lanes[l].ml.slug.clone();
for r in rows {
let entry = self.lanes[l].rotten.entry(r.unit.clone()).or_default();
entry
.rows
.push(format!("row {}: `{}`", r.row_number, r.row));
// the event carries the unit; the line on stdout names it
let detail = format!(
"row {} fails on mainline at {} — finding the culprit",
r.row_number,
short(&r.base)
);
emit(&event_line(&slug, &format!("{} rotten: {detail}", r.unit)));
self.lane_event(
l,
RunEvent::new(UtcTime::now(), "rotten", Some(&r.unit), None, detail)
.with("row", J::s(r.row.clone()))
.with("base", J::s(r.base.clone())),
);
self.search_culprit(l, r);
}
self.record(None);
}
/// Start the culprit search for one rotten row: in the lane's probe
/// checkout (one search at a time per lane), its commands under the
/// gate pool, off the critical path.
fn search_culprit(&mut self, l: LaneId, r: RottenRow) {
let lane = &self.lanes[l];
let node = lane.node(&r.unit);
let found = (|| -> Result<_, CoreError> {
let inst = lane
.inst
.as_ref()
.ok_or_else(|| CoreError::refusal(1, "error: the lane has no view of its SDD"))?;
let (events, _) = vitrify_core::journal::read(&inst.handle())?;
let anchor = Anchors::from_events(&events)
.unit(&r.unit)
.unwrap_or(&r.base)
.to_string();
let ownership = Ownership::build(
&lane.head_wt,
lane.sdds.iter().map(|i| (unit_graph::slug_of(i), i)),
)?;
let cmds = integrate::sdd_commands(&model::read_text(&inst.file)?);
let setup: Vec<String> = [cmds.bootstrap, cmds.build].into_iter().flatten().collect();
Ok((anchor, ownership, setup))
})();
let (anchor, ownership, setup) = match found {
Ok(found) => found,
Err(e) => {
let finding = format!("row {}: culprit not searched: {e}", r.row_number);
if let Some(entry) = self.lanes[l].rotten.get_mut(&r.unit) {
entry.findings.push(finding.clone());
}
diag(&format!("warning: {node} {finding}"));
return;
}
};
let closures: Closures = [(node.clone(), lane.closure(&r.unit))].into();
let git = lane.ml.primary.clone();
let prepare = PristineCheckout::new(
git.clone(),
culprit::probe_checkout(&lane.ml.layout.wt_root, &lane.ml.slug),
);
let turn = Arc::clone(&lane.probe_turn);
let tx = self.events_tx.clone();
self.lanes[l].searches += 1;
std::thread::spawn(move || {
let _turn = turn.lock().unwrap_or_else(|e| e.into_inner());
let rotten = culprit::Rotten {
node: &node,
row_number: r.row_number,
row: &r.row,
anchor: &anchor,
base: &r.base,
};
let probe = culprit::Probe {
git: &git,
ownership: &ownership,
closures: &closures,
setup: &setup,
prepare: &prepare,
runner: &ShellRunner,
config: facf::Config::default(),
};
let report = culprit::find(&rotten, &probe).map_err(|e| e.to_string());
let _ = tx.send(Event::Culprit {
lane: l,
unit: r.unit,
report,
});
});
}
/// A culprit search of lane `l` ended: its finding goes to the output,
/// the event log and the final report. A flaky row is not held against
/// its unit, which is checked again.
fn on_culprit(&mut self, l: LaneId, unit: &str, report: Result<culprit::Report, String>) {
let lane = &mut self.lanes[l];
lane.searches = lane.searches.saturating_sub(1);
let slug = lane.ml.slug.clone();
// the finding without the unit: the event and the final report
// carry the unit already
let (finding, flaky) = match &report {
Ok(r) if r.finding == culprit::Finding::Interrupted => return,
Ok(r) => (
r.finding_line(),
matches!(r.finding, culprit::Finding::Flake { .. }),
),
Err(e) => (format!("culprit search failed: {e}"), false),
};
if flaky {
lane.unrot(unit);
} else if let Some(entry) = lane.rotten.get_mut(unit) {
entry.findings.push(finding.clone());
}
emit(&event_line(&slug, &format!("{unit} {finding}")));
self.lane_event(
l,
RunEvent::new(UtcTime::now(), "culprit", Some(unit), None, finding)
.with("flaky", J::Bool(flaky)),
);
}
fn on_exit(
&mut self,
l: LaneId,
unit: &str,
verdict: MonitorVerdict,
expires: UtcTime,
) -> Result<(), CoreError> {
let Some(a) = self.lanes[l].active.get(unit) else {
return Ok(());
};
let (holder, seq, wt, out_path, killed) = (
a.holder.clone(),
a.seq,
a.wt.clone(),
a.out_path.clone(),
a.killed,
);
let w = holder.rsplit('-').next().unwrap_or(&holder).to_string();
if let Some(why) = killed {
// the scheduler stopped this worker; there is no report to read
let detail = match why {
"stale-inputs" => format!(
"the unit's inputs changed on {} while it was being built",
self.ctx.branch
),
_ => "the run ended before the worker finished".to_string(),
};
return self.fail(l, unit, why, &detail, Counts::No);
}
let stdout_text = std::fs::read_to_string(&out_path).unwrap_or_default();
let (report, parsed) = worker::report_or_released(&stdout_text, unit);
self.state.reports.push(render_report_line(&report));
// surface the worker's own account live — this is usually the
// line that names the failure cause
if parsed {
diag(&format!(
"report [{w}] {unit}: {}{}",
report.outcome,
if report.notes.is_empty() {
String::new()
} else {
format!(" — {}", report.notes)
}
));
if report.unit != unit {
diag(&format!(
"warning: [{w}] reported for {} but was dispatched for {unit}; taken as {unit}",
report.unit
));
}
} else {
diag(&format!(
"report [{w}] {unit}: no parseable report line (treated as released) — see {}",
out_path.display()
));
}
match verdict {
MonitorVerdict::TimedOut => {
diag(&format!(
"worker {holder} on {unit} exceeded the hard budget — killed; claim released"
));
self.fail(
l,
unit,
"timeout",
&format!(
"the worker exceeded the hard budget of {} minutes and was stopped",
self.ctx.cfg.run.worker_timeout_minutes
),
Counts::Work,
)
}
MonitorVerdict::LeaseLost => {
diag(&format!(
"worker {holder} on {unit} lost its lease — killed; the unit returns to the pool"
));
self.fail(
l,
unit,
"lease-lost",
"the lease could not be renewed and expired",
Counts::Quietly,
)
}
MonitorVerdict::Exited => match report.outcome.as_str() {
"built" if parsed => {
let lane = &mut self.lanes[l];
commit_handoff_if_dirty(&lane.ml, &wt, unit, seq);
// the lease is the orchestrator's to keep until the
// gate has decided
lane.keeper.hold(unit, &holder, expires);
if let Some(a) = lane.active.get_mut(unit) {
a.phase = Phase::Integrating;
if let Some(rec) = self.state.workers.get_mut(a.rec) {
rec.outcome = "integrating".to_string();
}
}
self.lane_event(
l,
RunEvent::new(
UtcTime::now(),
"integrating",
Some(unit),
Some(seq),
report.notes.clone(),
),
);
self.record(None);
let _ = self.lanes[l].jobs.send(Job::Integrate(IntegrateRequest {
unit: unit.to_string(),
token_seq: seq,
builder_holder: holder,
integrator_id: format!("{}-integrator", self.ctx.job_id),
}));
Ok(())
}
"blocked" if parsed => {
let q = report.q.clone().unwrap_or_else(|| "Q-???".to_string());
self.lanes[l].record_question(&q, unit, &holder, &report.notes)?;
self.lanes[l].park(unit, format!("open {q}: {}", report.notes))?;
self.fail(
l,
unit,
"blocked",
&format!("{q}: {}", report.notes),
Counts::No,
)
}
_ => {
let detail = if report.notes.is_empty() {
"the worker ended without building".to_string()
} else {
report.notes.clone()
};
self.fail(l, unit, "released", &detail, Counts::Work)
}
},
}
}
fn on_integrated(&mut self, l: LaneId, unit: &str, outcome: Outcome) -> Result<(), CoreError> {
self.lanes[l].keeper.drop_unit(unit);
let Some(a) = self.lanes[l].active.get(unit) else {
return Ok(());
};
let (seq, branch, holder) = (a.seq, a.branch.clone(), a.holder.clone());
match outcome {
Outcome::Verified { .. } => {
emit(&self.say(l, &format!("integrated {unit} (c{seq})")));
self.landed(l, unit, holder.contains("-a"));
self.lanes[l].fails.remove(unit);
self.lanes[l].attempts.push((
unit.to_string(),
"verified".to_string(),
String::new(),
));
self.close(l, unit, "verified", "");
Ok(())
}
Outcome::RejectedFencing { reason } => {
diag(&format!("fencing rejection for {unit}: {reason}"));
self.fail(l, unit, "rejected-fencing", &reason, Counts::Quietly)
}
Outcome::SkippedLeaseExpired => {
diag(&format!(
"{unit}: token current but lease expired — back to the pool"
));
self.fail(
l,
unit,
"skipped",
"the lease had expired when the gate started",
Counts::Quietly,
)
}
Outcome::RejectedWriteSet { paths, detail } => {
diag(&format!(
"write-set violation integrating {unit}: {}",
paths.join(", ")
));
self.lanes[l].gate_failures.push((
unit.to_string(),
"write-set check".to_string(),
detail.clone(),
));
self.fail(
l,
unit,
"rejected-write-set",
&format!("the work changed paths outside the unit's write-set:\n{detail}"),
Counts::Work,
)
}
Outcome::RejectedNotBuilt { reason } => {
diag(&format!("{unit}: {reason}"));
self.lanes[l].gate_failures.push((
unit.to_string(),
"Built stamp".to_string(),
reason.clone(),
));
self.fail(l, unit, "rejected-not-built", &reason, Counts::Work)
}
Outcome::RejectedStaleInputs { keys } => {
let detail = format!(
"built from text that has since changed on {}: {}",
self.ctx.branch,
keys.join(", ")
);
diag(&format!(
"{unit}: {detail} — it is rebuilt from the current text"
));
self.fail(l, unit, "stale-inputs", &detail, Counts::No)
}
Outcome::RejectedGate { command, output } => {
diag(&format!("gate red integrating {unit}: {command}"));
self.lanes[l].gate_failures.push((
unit.to_string(),
command.clone(),
output.clone(),
));
self.fail(
l,
unit,
"rejected-gate",
&format!(
"failing gate: `{command}`\n{}",
retry::clip(&output, 40, 4000)
),
Counts::Work,
)
}
Outcome::OwnershipConflict { paths } => {
let verdict = self.escalate(
l,
unit,
&branch,
"merge conflict on paths this unit may write — mainline also changed them after the branch forked",
&paths.join("\n"),
)?;
let detail = format!(
"merge conflict with {} on: {}",
self.ctx.branch,
paths.join(", ")
);
self.enforce(l, unit, &holder, verdict)?;
self.fail(l, unit, "ownership-conflict", &detail, Counts::Quietly)
}
Outcome::LostRace => {
diag(&format!(
"integration of {unit} lost the publish race after retries"
));
self.fail(
l,
unit,
"lost-race",
"mainline kept moving; the integration was not published",
Counts::Quietly,
)
}
// the run is ending: `finish` sets the work aside
Outcome::Interrupted => Ok(()),
}
}
// ------------------------------------------------------- outcomes
/// End this run's record of an attempt, with why it ended (`""` when
/// it verified), and log its outcome.
fn close(&mut self, l: LaneId, unit: &str, outcome: &str, detail: &str) {
let detail = first_line_clipped(detail);
if let Some(a) = self.lanes[l].active.remove(unit)
&& let Some(rec) = self.state.workers.get_mut(a.rec)
{
let ended = UtcTime::now();
let duration_s = UtcTime::parse(&rec.started_at)
.map(|s| ended.0 - s.0)
.unwrap_or(0);
rec.outcome = outcome.to_string();
rec.ended_at = Some(ended.format());
rec.detail = detail.clone();
let text = match &detail {
Some(d) => format!("{outcome} — {d}"),
None => outcome.to_string(),
};
let e = RunEvent::new(ended, "outcome", Some(unit), Some(a.seq), text)
.with("duration_s", J::Int(duration_s))
.with("outcome", J::s(outcome));
self.lane_event(l, e);
}
self.record(None);
}
/// An attempt that did not land: its work goes to its branch with the
/// reason, its worktree is removed, its claim released.
fn fail(
&mut self,
l: LaneId,
unit: &str,
label: &str,
detail: &str,
counts: Counts,
) -> Result<(), CoreError> {
let lease = self.lease_minutes();
self.abandon(l, unit, label, detail);
// the attempt just set aside may be adoptable
self.lanes[l].not_adoptable.remove(unit);
let lane = &mut self.lanes[l];
if let Some(a) = lane.active.get(unit) {
let holder = a.holder.clone();
lane.release(unit, &holder, lease);
}
lane.attempts
.push((unit.to_string(), label.to_string(), detail.to_string()));
self.close(l, unit, label, detail);
match counts {
Counts::No => Ok(()),
Counts::Quietly => self.count_failure(l, unit, label, false),
Counts::Work => self.count_failure(l, unit, label, true),
}
}
/// Count a failed attempt. At the limit, work failures get one
/// escalation call; at twice the limit the unit is set aside.
fn count_failure(
&mut self,
l: LaneId,
unit: &str,
label: &str,
work: bool,
) -> Result<(), CoreError> {
let limit = self.limit();
let lane = &mut self.lanes[l];
let (all, ask) = {
let f = lane.fails.entry(unit.to_string()).or_default();
f.all += 1;
if work {
f.work += 1;
}
// the judge is asked once: when the work itself — not a lost
// race or a lapsed lease in between — has failed `limit` times
let ask = work && f.work >= limit && !f.escalated;
if ask {
f.escalated = true;
}
(f.all, ask)
};
if ask {
let (command, output) = lane
.gate_failures
.iter()
.rev()
.find(|(u, _, _)| u == unit)
.map(|(_, c, o)| (c.clone(), o.clone()))
.unwrap_or_else(|| {
let last = lane
.attempts
.iter()
.rev()
.find(|(u, _, _)| u == unit)
.map(|(_, l, d)| format!("{l}: {d}"))
.unwrap_or_default();
(format!("worker attempt ({label})"), last)
});
let verdict = self.escalate(l, unit, "-", &command, &output)?;
self.enforce(l, unit, "escalation", verdict)?;
}
if all >= limit * 2 && !self.lanes[l].parked.contains_key(unit) && self.abort.is_none() {
diag(&format!(
"{unit}: {all} attempts did not land (last: {label}) — set aside for this run"
));
return self.lanes[l].park(
unit,
format!("set aside after {all} attempts that did not land; last outcome {label}"),
);
}
Ok(())
}
// ------------------------------------------------------ escalation
fn escalate(
&mut self,
l: LaneId,
unit: &str,
branch: &str,
command: &str,
output: &str,
) -> Result<escalate::Verdict, CoreError> {
let lane = &self.lanes[l];
let (journal_tail, fencing) = match &lane.inst {
Some(inst) => {
let (events, _) = journal::read(&inst.handle())?;
let tail: Vec<String> = events
.iter()
.rev()
.take(20)
.rev()
.map(journal::render_line)
.collect();
let fencing = inst
.unit(unit)
.map(|u| {
format!(
"status={}, claim_seq={}, lease to {}",
u.meta.get_str_or("status", "Draft"),
u.meta.get_int("claim_seq"),
u.meta.get_str_or("claim_expires", "—")
)
})
.unwrap_or_else(|| "-".to_string());
(tail.join("\n"), fencing)
}
None => (String::new(), "-".to_string()),
};
let evidence = escalate::Evidence {
unit: unit.to_string(),
branch: branch.to_string(),
fencing,
command: command.to_string(),
output_tail: output.to_string(),
journal_tail,
};
// the escalation role's model/effort from ~/.vitrify/config.toml,
// appended to the selected harness's recorded spelling
let (verdict, evidence_path) = escalate::escalate(
&self.ctx.home,
VERSION,
&self.ctx.escalation_argv,
&evidence,
&lane.logs_dir,
lane.escalation_index,
)?;
self.lanes[l].escalation_index += 1;
self.state.escalations.push((
evidence_path.display().to_string(),
verdict.verdict.as_str().to_string(),
));
emit(&format!(
"escalation verdict: {} ({})",
verdict.verdict.as_str(),
verdict.reason
));
self.lane_event(
l,
RunEvent::new(
UtcTime::now(),
"escalation",
Some(unit),
None,
verdict.reason.clone(),
)
.with("verdict", J::s(verdict.verdict.as_str())),
);
Ok(verdict)
}
/// The orchestrator, not the judge, acts on a verdict. `retry` and
/// `release` leave the unit in the pool (its claim is already
/// released); `block` records the question and parks the unit; `abort`
/// ends the run.
fn enforce(
&mut self,
l: LaneId,
unit: &str,
reporter: &str,
verdict: escalate::Verdict,
) -> Result<(), CoreError> {
match verdict.verdict {
escalate::VerdictKind::Retry | escalate::VerdictKind::Release => Ok(()),
escalate::VerdictKind::Block => {
let q = verdict.q.clone().unwrap_or_else(|| "Q-???".into());
let target = verdict.unit.clone().unwrap_or_else(|| unit.to_string());
let lane = &mut self.lanes[l];
lane.record_question(&q, &target, reporter, &verdict.reason)?;
lane.park(&target, format!("open {q}: {}", verdict.reason))
}
escalate::VerdictKind::Abort => {
self.abort = Some(verdict.reason.clone());
Ok(())
}
}
}
// -------------------------------------------------------- recovery
/// Release the claims of runs that died in this checkout, and save what
/// their workers had written.
fn recover_dead_runs(&mut self) -> Result<(), CoreError> {
self.derive()?;
for l in 0..self.lanes.len() {
self.recover_lane(l);
}
Ok(())
}
/// Release lane `l`'s claims held by runs that are gone, saving what
/// their workers wrote, and sweep what those runs left.
fn recover_lane(&mut self, l: LaneId) {
let lease = self.lease_minutes();
let mut jobs: Vec<String> = Vec::new();
{
let lane = &self.lanes[l];
let Some(inst) = &lane.inst else {
return;
};
for (unit, holder, job, seq) in lane::dead_claims(&self.ctx, inst) {
emit(&lane.say(&format!(
"recovered {unit} (c{seq}): its holder {holder} is gone — claim released"
)));
self.lane_event(
l,
RunEvent::new(
UtcTime::now(),
"recovered",
Some(&unit),
Some(seq),
format!("its holder {holder} is gone — claim released"),
)
.with("holder", J::s(holder.clone())),
);
// whatever its worker wrote becomes the next attempt's draft
let wt = lane.ml.layout.unit_worktree(&lane.ml.slug, &unit, seq);
retry::abandon(
&lane.ml.primary,
&wt,
&unit,
seq,
"interrupted",
&format!("the run that held this claim ({job}) ended without releasing it"),
);
lane.release(&unit, &holder, lease);
if !jobs.contains(&job) {
jobs.push(job);
}
}
}
for job in &jobs {
self.sweep_dead_job(job);
}
if !jobs.is_empty() {
self.dirty = true;
}
}
/// What a dead run left in `.vitrify/`: its lock, its transaction
/// worktrees — and possibly workers that outlived it.
fn sweep_dead_job(&self, job: &str) {
let scratch = &self.ctx.layout.scratch;
let _ = std::fs::remove_file(scratch.join("jobs").join(format!("{job}.lock")));
if let Some((_, pid)) = job.rsplit_once("-p")
&& let Ok(entries) = std::fs::read_dir(scratch)
{
let prefix = format!("tx-{pid}-");
let mut dead: Vec<_> = entries
.flatten()
.filter(|e| e.file_name().to_string_lossy().starts_with(&prefix))
.map(|e| e.path())
.collect();
dead.sort();
for path in dead {
let _ = self.ctx.primary.worktree_remove(&path);
}
}
// a run killed outright cannot stop its workers; say so
let record = self.ctx.layout.job_logs(job).join("run.json");
let Ok(text) = std::fs::read_to_string(record) else {
return;
};
let Ok(v) = serde_json::from_str::<serde_json::Value>(&text) else {
return;
};
for w in v["workers"].as_array().into_iter().flatten() {
if w["outcome"] == "running"
&& let Some(pid) = w["pid"].as_u64()
&& gate::alive(pid as u32, true)
{
diag(&format!(
"note: a worker of {job} (process group {pid}, unit {}) is still running — stop it with: kill -9 -- -{pid}",
w["unit"].as_str().unwrap_or("?")
));
}
}
}
// ----------------------------------------------------- termination
/// Nothing is running and nothing can be started. Decide whether that
/// is the end, and which one: Complete when every lane is, else the
/// first other end a lane reaches.
fn conclude(&mut self) -> Result<Option<End>, CoreError> {
if self.ctx.project {
return self.conclude_project();
}
let mut end = End::Complete;
for l in 0..self.lanes.len() {
match self.conclude_lane(l)? {
None => return Ok(None),
Some(End::Complete) => {}
Some(other) => {
if matches!(end, End::Complete) {
end = other;
}
}
}
}
Ok(Some(end))
}
/// Ask lane `l`'s integrator for the state of the head. `None` = the
/// run was interrupted meanwhile.
fn head_check(&mut self, l: LaneId, want_suite: bool) -> Result<Option<HeadCheck>, CoreError> {
// under a lane cap the head checks take turns, each lane's checkout
// made for its check and removed after it
let release = self.ctx.project && self.ctx.cfg.run.lane_limit().is_some();
let _ = self.lanes[l].jobs.send(Job::CheckHead {
want_suite,
release,
});
if release {
self.lanes[l].checkout = Checkout::Absent;
}
let mut asked_at = Instant::now();
let check = loop {
match self.events.recv_timeout(TICK) {
Ok(Event::HeadChecked { lane, check }) if lane == l => break (*check)?,
Ok(other) => self.handle(other)?,
Err(RecvTimeoutError::Timeout) => {
self.lanes[l].integrator_alive()?;
if signals::pending().is_none() {
asked_at = Instant::now();
} else if asked_at.elapsed() > Duration::from_secs(10) {
return Ok(None);
}
}
Err(RecvTimeoutError::Disconnected) => {
return Err(CoreError::refusal(
1,
"error: the integrator thread is gone",
));
}
}
};
if check.interrupted || signals::pending().is_some() {
return Ok(None);
}
Ok(Some(check))
}
/// Under a lane cap, every lane gives its checkouts back before the
/// head checks, which then take turns.
fn release_all(&mut self) -> Result<(), CoreError> {
if self.ctx.cfg.run.lane_limit().is_none() {
return Ok(());
}
for lane in &mut self.lanes {
if lane.checkout == Checkout::Ready && lane.active.is_empty() && lane.searches == 0 {
lane.checkout = Checkout::Releasing;
let _ = lane.jobs.send(Job::Release);
}
}
let deadline = Instant::now() + Duration::from_secs(60);
while self
.lanes
.iter()
.any(|l| matches!(l.checkout, Checkout::Releasing | Checkout::Preparing))
&& Instant::now() < deadline
&& signals::pending().is_none()
{
match self.events.recv_timeout(TICK) {
Ok(event) => self.handle(event)?,
Err(RecvTimeoutError::Timeout) => {}
Err(RecvTimeoutError::Disconnected) => break,
}
}
Ok(())
}
/// The end of a project run: each lane's head checked, its outcome
/// decided — Complete, Up to date, or Blocked with why — and the report
/// printed: `Complete.` only when no lane is blocked.
fn conclude_project(&mut self) -> Result<Option<End>, CoreError> {
if self.ctx.wait
&& self
.lanes
.iter()
.all(|l| l.checked_head.as_deref() == Some(l.head.as_str()))
{
return Ok(None); // already reported on these heads
}
self.release_all()?;
let rotten = self.rotten_nodes();
let slugs = self.lane_slugs();
let mut reports: Vec<LaneReport> = Vec::new();
let mut details: Vec<String> = Vec::new();
for l in 0..self.lanes.len() {
let slug = self.lanes[l].graph_slug();
if let Some(first) = self.lanes[l].blocked.first() {
let first = first.lines().next().unwrap_or("").to_string();
reports.push(LaneReport {
slug,
outcome: LaneOutcome::Blocked {
detail: format!("preflight failed: {first}"),
},
});
continue;
}
let Some(inst) = self.lanes[l].inst.clone() else {
continue;
};
let all_verified = inst
.units
.iter()
.all(|u| matches!(u.meta.get_str_or("status", "Draft"), "Verified" | "Retired"));
let Some(check) = self.head_check(l, all_verified)? else {
return Ok(signals::pending().map(End::Interrupted));
};
let lane = &self.lanes[l];
let (events, _) = journal::read(&inst.handle()).unwrap_or_default();
let proj = journal::replay(&events);
let replay_clean =
proj.order_findings.is_empty() && journal::verify(&proj, &inst.units).is_empty();
let suite = check.suite.as_ref().map(|(_, passed, _)| *passed);
let complete = all_verified
&& lane.rotten.is_empty()
&& replay_clean
&& !events.is_empty()
&& suite != Some(false)
&& check.findings.is_empty()
&& check.broken.is_none();
let total = inst
.units
.iter()
.filter(|u| u.meta.get_str_or("status", "Draft") != "Retired")
.count();
let verified = inst
.units
.iter()
.filter(|u| u.meta.get_str_or("status", "Draft") == "Verified")
.count();
let outcome = if complete && lane.built + lane.adopted + lane.rechecked == 0 {
// since the newest recorded check: a unit's verification or
// re-check, or the suite's pass
let since = events
.iter()
.rev()
.filter(|e| e.event == "verified" || e.is_suite_pass())
.find_map(|e| e.commit.clone())
.unwrap_or_else(|| short(&check.head).to_string());
LaneOutcome::UpToDate { since }
} else if complete {
let model_calls = self
.state
.workers
.iter()
.filter(|w| w.lane == lane.ml.slug && w.pid != 0)
.count();
LaneOutcome::Complete {
verified,
total,
built: lane.built + lane.adopted,
rechecked: lane.rechecked,
changed: Vec::new(),
suite,
model_calls,
}
} else {
let mut lines: Vec<String> = Vec::new();
for v in &lane.views {
if let Some(r) = lane.rotten.get(&v.unit) {
let found =
r.findings.first().cloned().unwrap_or_else(|| {
format!("{} fails on mainline", r.rows.join(", "))
});
lines.push(format!("{} rotten: {found}", v.unit));
} else if !matches!(v.status.as_str(), "Verified" | "Retired") {
lines.push(format!("{} {}", v.unit, lane.blocker(v, &rotten, &slugs)));
}
}
if let Some((cmd, output)) = &check.broken {
lines.push(format!(
"§5 command failing at the head: `{cmd}`: {}",
tail(output, 1)
));
}
for f in &check.findings {
lines.push(format!(
"audit [check {}] {}: {}",
f.check, f.subject, f.cause
));
}
if suite == Some(false) {
lines.push("§5 Test failing at the head".to_string());
}
if !replay_clean {
lines.push("replay: inconsistent (see `vitrify journal replay`)".to_string());
}
let detail = lines
.first()
.cloned()
.unwrap_or_else(|| "the report names each blocker".to_string());
for line in lines.iter().skip(1) {
details.push(format!(" {slug} {line}"));
}
LaneOutcome::Blocked { detail }
};
reports.push(LaneReport { slug, outcome });
}
let blocked = reports.iter().any(|r| r.state() == LaneState::Blocked);
for r in &reports {
if let Some(l) = self.lanes.iter().position(|x| x.graph_slug() == r.slug) {
self.lanes[l].state = r.state();
}
}
let mut lines = project::report(&reports);
lines.extend(details);
out::emit("");
for line in &lines {
out::emit(line);
}
if blocked && self.ctx.wait {
for lane in &mut self.lanes {
lane.checked_head = Some(lane.head.clone());
}
emit("waiting: the run resumes when the repository changes (interrupt to stop)");
self.waiting(
"waiting: the run resumes when the repository changes (interrupt to stop)",
);
return Ok(None);
}
self.keep_report(lines);
Ok(Some(if blocked { End::Blocked } else { End::Complete }))
}
/// Whether lane `l` has reached its end, and which one.
fn conclude_lane(&mut self, l: LaneId) -> Result<Option<End>, CoreError> {
let Some(inst) = self.lanes[l].inst.clone() else {
return Ok(None);
};
// Verified is a status, not a stamp: a change entry bumps the SDD's
// version and stales only the units whose reading set it touched
// (schema §11, §12); the others stay Verified with the stamp of the
// version they were last built at. Whether each one's trace still
// matches the current content is the head audit's check 5, below.
let all_verified = inst.units.iter().all(|u| {
let status = u.meta.get_str_or("status", "Draft");
status == "Retired" || status == "Verified"
});
if self.ctx.wait {
let lane = &self.lanes[l];
if lane.checked_head.as_deref() == Some(lane.head.as_str()) {
return Ok(None); // already reported on this head
}
if !all_verified {
self.lanes[l].checked_head = Some(self.lanes[l].head.clone());
self.report(l, &inst, None, false, true);
emit("waiting: the run resumes when the spec changes (interrupt to stop)");
self.waiting("waiting: the run resumes when the spec changes (interrupt to stop)");
return Ok(None);
}
}
// the head itself: audit, and — when everything is Verified — the
// whole suite. The lane's integrator owns its checkout; ask it.
let _ = self.lanes[l].jobs.send(Job::CheckHead {
want_suite: all_verified,
release: false,
});
let mut asked_at = Instant::now();
let check = loop {
match self.events.recv_timeout(TICK) {
Ok(Event::HeadChecked { lane, check }) if lane == l => break (*check)?,
Ok(other) => self.handle(other)?,
Err(RecvTimeoutError::Timeout) => {
self.lanes[l].integrator_alive()?;
// an interrupt stops the commands the check runs, and
// the answer follows at once; if it does not, do not
// wait for it
if signals::pending().is_none() {
asked_at = Instant::now();
} else if asked_at.elapsed() > Duration::from_secs(10) {
break HeadCheck {
head: String::new(),
findings: Vec::new(),
broken: None,
suite: None,
interrupted: true,
};
}
}
Err(RecvTimeoutError::Disconnected) => {
return Err(CoreError::refusal(
1,
"error: the integrator thread is gone",
));
}
}
};
if let Some(sig) = signals::pending() {
return Ok(Some(End::Interrupted(sig)));
}
if check.interrupted {
return Ok(None);
}
let (complete, lines) = self.report(l, &inst, Some(&check), all_verified, false);
if complete {
self.lanes[l].state = LaneState::Complete;
self.keep_report(lines);
return Ok(Some(End::Complete));
}
if self.ctx.wait {
self.lanes[l].checked_head = Some(self.lanes[l].head.clone());
emit("waiting: the run resumes when the repository changes (interrupt to stop)");
self.waiting(
"waiting: the run resumes when the repository changes (interrupt to stop)",
);
return Ok(None);
}
self.lanes[l].state = LaneState::Blocked;
self.keep_report(lines);
Ok(Some(End::Blocked))
}
/// Print lane `l`'s termination report; returns whether its
/// done-condition holds, and the lines printed from the status line
/// on. `check` is the state of the head (absent when the run is ending
/// without one: interrupted, aborted, or still waiting).
fn report(
&self,
l: LaneId,
inst: &Instance,
check: Option<&HeadCheck>,
all_verified: bool,
interim: bool,
) -> (bool, Vec<String>) {
let lane = &self.lanes[l];
let mut lines: Vec<String> = Vec::new();
let mut emit = |line: &str| lines.push(line.to_string());
let (events, _) = journal::read(&inst.handle()).unwrap_or_default();
let proj = journal::replay(&events);
// consistent = the journal projects what front matter says (with no
// events at all, that nothing is beyond Ready)
let replay_clean =
proj.order_findings.is_empty() && journal::verify(&proj, &inst.units).is_empty();
let progress = ledger::progress_line(&inst.units);
let suite_ok = check
.and_then(|c| c.suite.as_ref())
.is_none_or(|(_, passed, _)| *passed);
let complete = all_verified
&& lane.rotten.is_empty()
&& replay_clean
&& !events.is_empty()
&& suite_ok
&& check.is_some_and(|c| c.findings.is_empty() && c.broken.is_none());
if complete {
emit("Complete.");
emit(&format!(" {progress}"));
emit(" audit: exit 0 (clean)");
emit(" replay: exit 0 (consistent)");
if let Some((cmd, _, _)) = check.and_then(|c| c.suite.as_ref()) {
emit(&format!(" §5 Test: exit 0 ({cmd})"));
}
} else {
emit(if interim {
"Blocked (waiting)."
} else {
"Blocked."
});
emit(&format!(" {progress}"));
if let Some(c) = check {
if let Some((cmd, output)) = &c.broken {
emit(&format!(
" §5 command failing at the {} head: `{cmd}`: {}",
self.ctx.branch,
tail(output, 3)
));
}
if !c.findings.is_empty() {
emit(&format!(" audit: {} finding(s)", c.findings.len()));
for f in &c.findings {
emit(&format!(
" [check {}] {}: {}",
f.check, f.subject, f.cause
));
}
}
if let Some((cmd, false, output)) = &c.suite {
emit(&format!(
" §5 Test failing at the {} head: `{cmd}`: {}",
self.ctx.branch,
tail(output, 3)
));
}
}
if !replay_clean {
emit(" replay: inconsistent (see `vitrify journal replay`)");
}
let (rotten, lanes) = (self.rotten_nodes(), self.lane_slugs());
for v in &lane.views {
if let Some(r) = lane.rotten.get(&v.unit) {
let found = if r.findings.is_empty() {
"no culprit found".to_string()
} else {
r.findings.join("; ")
};
emit(&format!(
" {}: rotten — {} fails on mainline too; {found}",
v.unit,
r.rows.join(", ")
));
continue;
}
if v.status == "Verified" || v.status == "Retired" {
continue;
}
emit(&format!(
" {}: {}",
v.unit,
lane.blocker(v, &rotten, &lanes)
));
}
}
// the report ends with the journal tail
emit(" journal tail:");
for e in events.iter().rev().take(5).rev() {
emit(&format!(" {}", journal::render_line(e)));
}
out::emit("");
for line in &lines {
out::emit(line);
}
(complete, lines)
}
/// Keep a final report: `report.txt` beside the job's logs, and the
/// run record's `report` (written with the record's next update). A
/// lane's report follows those of the lanes before it.
fn keep_report(&mut self, lines: Vec<String>) {
let report = self.state.report.get_or_insert_with(Vec::new);
report.extend(lines);
let path = self.ctx.logs_dir.join("report.txt");
let mut text = report.join("\n");
text.push('\n');
if let Err(e) =
std::fs::create_dir_all(&self.ctx.logs_dir).and_then(|_| std::fs::write(&path, text))
{
diag(&format!("warning: {} not written: {e}", path.display()));
}
}
// ---------------------------------------------------------- finish
/// Stop whatever is still running, hand back every claim, close the
/// record. Every way out of the loop comes through here.
fn finish(mut self, end: Result<End, CoreError>) -> Result<u8, CoreError> {
// The terminal's Ctrl-C reaches git too: a command cut down by the
// signal fails, and that failure is the interrupt, not an error.
let end = match (end, signals::pending()) {
(Err(_), Some(sig)) => Ok(End::Interrupted(sig)),
(end, _) => end,
};
let (label, detail): (&'static str, String) = match &end {
Ok(End::Complete) => ("complete", "done-condition met".to_string()),
Ok(End::Blocked) => {
// the report's progress line, and where the blockers are
let progress = self
.state
.report
.as_ref()
.and_then(|lines| lines.get(1))
.map(|l| l.trim())
.filter(|l| l.starts_with("Progress:"));
let detail = match progress {
Some(p) => format!("{p} — the report names each blocker"),
None => "the report names each blocker".to_string(),
};
("blocked", detail)
}
Ok(End::Aborted(reason)) => ("blocked", format!("escalation verdict abort: {reason}")),
Ok(End::Interrupted(sig)) => ("interrupted", signals::name(*sig).to_string()),
Err(e) => ("error", e.to_string()),
};
let unfinished = !matches!(&end, Ok(End::Complete) | Ok(End::Blocked));
// What an attempt the run cuts short is recorded as. A run the
// judge aborted ends "blocked", but its workers were not blocked on
// anything: they were stopped.
let cut_short = match &end {
Ok(End::Aborted(_)) => "aborted",
_ => label,
};
if unfinished {
self.stop_work(cut_short);
}
// the threads end with the run
for lane in &mut self.lanes {
lane.stop_threads();
}
if unfinished {
for lane in &mut self.lanes {
if lane.state != LaneState::Complete && lane.state != LaneState::UpToDate {
lane.state = LaneState::Blocked;
}
}
}
// whatever the integrators finished before they stopped
while let Ok(event) = self.events.try_recv() {
self.absorb(event);
}
if unfinished {
self.hand_back(cut_short);
}
if let Ok(End::Aborted(reason)) = &end {
emit(&format!("escalation verdict abort — stopping: {reason}"));
for l in 0..self.lanes.len() {
self.lanes[l].state = LaneState::Blocked;
if self.derive_quietly(l)
&& let Some(inst) = self.lanes[l].inst.clone()
{
let (_, lines) = self.report(l, &inst, None, false, false);
self.keep_report(lines);
}
}
}
self.record(Some((label, &detail)));
self.record_lanes(true);
self.event(
RunEvent::new(
UtcTime::now(),
"run-ended",
None,
None,
format!("{label}: {detail}"),
)
.with("state", J::s(label)),
);
// a primary checkout that could not follow gets one more try, and —
// if it still cannot — the last word on how to bring it up to date
let behind = {
let _turn = claim::tx_lock();
claim::catch_up_primary(&self.ctx.primary, &self.ctx.branch)
};
if let Some(warning) = behind {
diag(&warning);
}
// this run's own scratch
for lane in &self.lanes {
lane.remove_head_checkout();
}
drop(self.job_lock.take());
let _ = std::fs::remove_file(
self.ctx
.layout
.scratch
.join("jobs")
.join(format!("{}.lock", self.ctx.job_id)),
);
match end {
Ok(End::Complete) => Ok(0),
Ok(End::Blocked) | Ok(End::Aborted(_)) => Ok(2),
Ok(End::Interrupted(sig)) => {
emit("");
emit(&format!(
"Interrupted ({}): workers stopped, claims released, work in progress kept on its branches. Run again to continue.",
signals::name(sig)
));
signals::die_by(sig)
}
Err(e) => Err(e),
}
}
/// Stop every worker and the gates, and wait for them to say so.
fn stop_work(&mut self, why: &'static str) {
signals::request_stop();
let mut waiting = 0;
for lane in &mut self.lanes {
for a in lane.active.values_mut() {
if a.phase == Phase::Working
&& let Some(pid) = a.pid
{
if a.killed.is_none() {
a.killed = Some(why);
}
gate::kill_group(pid);
waiting += 1;
}
}
}
// each monitor reports its worker's end
let deadline = Instant::now() + Duration::from_secs(30);
while waiting > 0 && Instant::now() < deadline {
match self.events.recv_timeout(Duration::from_millis(200)) {
Ok(event) => {
if matches!(event, Event::Exited { .. }) {
waiting -= 1;
}
self.absorb(event);
}
Err(RecvTimeoutError::Timeout) => {}
Err(RecvTimeoutError::Disconnected) => break,
}
}
}
/// An event that arrives while the run is ending. A unit the integrator
/// did publish is Verified, whatever else is going on; everything else
/// is left for [`Scheduler::hand_back`].
fn absorb(&mut self, event: Event) {
match event {
// its monitor has seen the worker end; `hand_back` does the rest
Event::Exited { .. } => {}
Event::Integrated {
lane: l,
unit,
outcome: Outcome::Verified { .. },
} => {
self.lanes[l].keeper.drop_unit(&unit);
if let Some(a) = self.lanes[l].active.get(&unit) {
let adopted = a.holder.contains("-a");
emit(&self.say(l, &format!("integrated {unit} (c{})", a.seq)));
self.landed(l, &unit, adopted);
self.lanes[l].attempts.push((
unit.clone(),
"verified".to_string(),
String::new(),
));
self.close(l, &unit, "verified", "");
}
}
// a verdict the gate reached before the run ended is the
// attempt's, whatever is ending the run: set aside under its own
// label, so a later run judges it as the gate did
Event::Integrated {
lane: l,
unit,
outcome,
} => {
self.lanes[l].keeper.drop_unit(&unit);
if let Some((label, detail)) = verdict_label(&outcome)
&& self.lanes[l].active.contains_key(&unit)
{
let _ = self.fail(l, &unit, label, &detail, Counts::No);
}
}
_ => {}
}
}
/// Every claim still held goes back: the work to its branch, the unit
/// to the pool.
fn hand_back(&mut self, why: &'static str) {
let lease = self.lease_minutes();
for l in 0..self.lanes.len() {
let units: Vec<String> = self.lanes[l].active.keys().cloned().collect();
for unit in units {
let Some(a) = self.lanes[l].active.get(&unit) else {
continue;
};
let detail = match a.phase {
Phase::Working => "the run ended before the worker finished",
Phase::Integrating => {
"the work was reported built; the run ended before the gate had decided"
}
};
self.abandon(l, &unit, why, detail);
let lane = &mut self.lanes[l];
lane.keeper.drop_unit(&unit);
let Some(a) = lane.active.get(&unit) else {
continue;
};
let holder = a.holder.clone();
lane.release(&unit, &holder, lease);
lane.attempts
.push((unit.clone(), why.to_string(), detail.to_string()));
self.close(l, &unit, why, detail);
}
}
}
/// A last look at lane `l`'s head for the final report; never fails
/// the exit.
fn derive_quietly(&mut self, l: LaneId) -> bool {
match self.lanes[l].derive() {
Ok(_) => true,
Err(e) => {
diag(&format!("warning: final state not derived: {e}"));
self.lanes[l].inst.is_some()
}
}
}
}
impl Drop for Scheduler {
/// A scheduler that goes away with workers still running — a panic
/// unwinding through it; `finish` leaves none — stops them. Their
/// claims are released by the next run, which finds this run's job
/// lock free.
fn drop(&mut self) {
for lane in &self.lanes {
for a in lane.active.values() {
if a.phase == Phase::Working
&& let Some(pid) = a.pid
{
gate::kill_group(pid);
}
}
}
}
}
/// Whether a failed attempt counts toward the unit's limit, and whether
/// reaching the limit calls the escalation judge.
enum Counts {
/// Not the attempt's doing (the spec changed, the run is ending, a
/// question is open).
No,
/// Counted, but nothing a judge could rule on (a lost race, a lapsed
/// lease, a taken name).
Quietly,
/// The work itself failed.
Work,
}
/// The label and reason an attempt is set aside under for a gate's
/// verdict; `None` for one that is no verdict on it (landed, interrupted).
fn verdict_label(outcome: &Outcome) -> Option<(&'static str, String)> {
Some(match outcome {
Outcome::Verified { .. } | Outcome::Interrupted => return None,
Outcome::RejectedFencing { reason } => ("rejected-fencing", reason.clone()),
Outcome::SkippedLeaseExpired => (
"skipped",
"the lease had expired when the gate started".to_string(),
),
Outcome::RejectedWriteSet { detail, .. } => ("rejected-write-set", detail.clone()),
Outcome::RejectedNotBuilt { reason } => ("rejected-not-built", reason.clone()),
Outcome::RejectedStaleInputs { keys } => (
"stale-inputs",
format!(
"built from text that has since changed: {}",
keys.join(", ")
),
),
Outcome::RejectedGate { command, output } => (
"rejected-gate",
format!(
"failing gate: `{command}`\n{}",
retry::clip(output, 40, 4000)
),
),
Outcome::OwnershipConflict { paths } => (
"ownership-conflict",
format!("merge conflict on: {}", paths.join(", ")),
),
Outcome::LostRace => (
"lost-race",
"mainline kept moving; the integration was not published".to_string(),
),
})
}
/// Why a unit is Stale, from the journal: the declaring event's cause.
fn stale_cause(inst: &Instance, unit: &str) -> String {
let (events, _) = journal::read(&inst.handle()).unwrap_or_default();
for e in events.iter().rev() {
if e.event == "stale" && e.unit.as_deref() == Some(unit) {
return e
.cause
.clone()
.unwrap_or_else(|| "one of its inputs changed".to_string());
}
if e.event == "change" && e.stale.iter().flatten().any(|u| u == unit) {
return match &e.entry {
Some(entry) => format!("change entry {entry}"),
None => "a change entry".to_string(),
};
}
}
"one of its inputs changed".to_string()
}
fn render_report_line(r: &worker::Report) -> String {
format!(
"{{\"unit\":\"{}\",\"outcome\":\"{}\",\"branch\":\"{}\",\"claim_seq\":{},\"q\":{},\"notes\":\"{}\"}}",
r.unit,
r.outcome,
r.branch,
r.claim_seq,
r.q.as_deref()
.map(|q| format!("\"{q}\""))
.unwrap_or_else(|| "null".into()),
r.notes.replace('"', "'")
)
}
/// Commit the handoff worktree with the fixed build message. Workers never
/// run `git commit`: protocol git operations belong to vitrify (RA-6), and a
/// worker is a harness session whose permission policy may forbid commits
/// anyway — a green build is reported `built` with the work left in place.
/// A clean tree (nothing new) is a no-op; a commit failure is diagnosed and
/// left for the integration gate to reject (the branch tip stays at the
/// claim commit).
fn commit_handoff_if_dirty(ml: &Mainline, wt: &Path, unit: &str, seq: i64) {
if !wt.is_dir() {
return;
}
let git = ml.primary.at(wt);
match git.is_dirty() {
Ok(true) => {}
_ => return,
}
let committed = git
.add_all()
.and_then(|()| git.commit(&format!("sdd: build {unit} (c{seq})")));
match committed {
Ok(()) => diag(&format!("committed handoff worktree for {unit} (c{seq})")),
Err(e) => diag(&format!("handoff commit for {unit} (c{seq}) failed: {e}")),
}
}
/// Whether `commit` is a run's note setting an attempt aside.
fn set_aside(git: &crate::git::Git, commit: &str) -> bool {
git.commit_message(commit)
.is_ok_and(|m| m.starts_with("sdd: abandoned attempt "))
}
impl Scheduler {
/// A unit of lane `l` landed: count it, and follow the critical path.
fn landed(&mut self, l: LaneId, unit: &str, adopted: bool) {
self.lanes[l].unrot(unit);
// its gate checked it, and ran the §5 Test command if there is one
self.lanes[l].checked.insert(unit.to_string());
self.lanes[l].suite_ran = true;
if adopted {
self.lanes[l].adopted += 1;
} else {
self.lanes[l].built += 1;
}
let node = self.lanes[l].node(unit);
if self.plan.critical_path.contains(&node) && !self.critical_ran.contains(&node) {
self.critical_ran.push(node);
self.record_lanes(false);
}
}
}
/// The first 7 characters of a commit id.
fn short(sha: &str) -> &str {
&sha[..sha.len().min(7)]
}