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
//! Event loop orchestration.
//!
//! The event loop coordinates the execution of hats via pub/sub messaging.
mod loop_state;
#[cfg(test)]
mod tests;
pub use loop_state::LoopState;
use crate::config::{HatBackend, InjectMode, RalphConfig, ScratchpadConfig};
use crate::event_parser::{EventParser, MutationEvidence, MutationStatus};
use crate::event_reader::EventReader;
use crate::hat_registry::HatRegistry;
use crate::hatless_ralph::HatlessRalph;
use crate::instructions::InstructionBuilder;
use crate::loop_context::LoopContext;
use crate::memory_store::{MarkdownMemoryStore, format_memories_as_markdown, truncate_to_budget};
use crate::skill_registry::SkillRegistry;
use crate::text::floor_char_boundary;
use ralph_proto::{CheckinContext, Event, EventBus, Hat, HatId, RobotService};
use serde_json::{Map, Value};
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use std::time::Duration;
use tracing::{debug, info, warn};
/// Result of processing events from JSONL.
#[derive(Debug, Clone)]
pub struct ProcessedEvents {
/// Whether any valid events were found and published.
pub had_events: bool,
/// Whether any published events matched the semantic `plan.*` topic family.
pub had_plan_events: bool,
/// Structured context for the first processed `human.interact` event,
/// including the question payload and post-dispatch outcome metadata.
pub human_interact_context: Option<Value>,
/// Whether any events lacked specific hat subscribers (orphans handled by Ralph).
pub has_orphans: bool,
}
/// Result of processing events from JSONL with wave events partitioned out.
#[derive(Debug)]
pub struct ProcessedEventsWithWaves {
/// Normal event processing results.
pub processed: ProcessedEvents,
/// Wave events extracted before normal processing (have wave_id set).
pub wave_events: Vec<crate::event_reader::Event>,
}
/// Reason the event loop terminated.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TerminationReason {
/// Completion promise was detected in output.
CompletionPromise,
/// Maximum iterations reached.
MaxIterations,
/// Maximum runtime exceeded.
MaxRuntime,
/// Maximum cost exceeded.
MaxCost,
/// Too many consecutive failures.
ConsecutiveFailures,
/// Loop thrashing detected (repeated blocked events).
LoopThrashing,
/// Stale loop detected (same topic emitted 3+ times consecutively).
LoopStale,
/// Too many consecutive malformed JSONL lines in events file.
ValidationFailure,
/// Manually stopped.
Stopped,
/// Interrupted by signal (SIGINT/SIGTERM).
Interrupted,
/// Restart requested via Telegram `/restart` command.
RestartRequested,
/// Workspace directory (worktree) was removed externally.
WorkspaceGone,
/// Loop was cancelled gracefully via loop.cancel event (human rejection, timeout).
Cancelled,
}
impl TerminationReason {
/// Returns the exit code for this termination reason per spec.
///
/// Per spec "Loop Termination" section:
/// - 0: Completion promise detected (success)
/// - 1: Consecutive failures or unrecoverable error (failure)
/// - 2: Max iterations, max runtime, or max cost exceeded (limit)
/// - 130: User interrupt (SIGINT = 128 + 2)
pub fn exit_code(&self) -> i32 {
match self {
TerminationReason::CompletionPromise => 0,
TerminationReason::ConsecutiveFailures
| TerminationReason::LoopThrashing
| TerminationReason::LoopStale
| TerminationReason::ValidationFailure
| TerminationReason::Stopped
| TerminationReason::WorkspaceGone => 1,
TerminationReason::MaxIterations
| TerminationReason::MaxRuntime
| TerminationReason::MaxCost => 2,
TerminationReason::Interrupted => 130,
// Restart uses exit code 3 to signal the caller to exec-replace
TerminationReason::RestartRequested => 3,
// Cancelled is a clean exit (0) — the loop stopped intentionally
TerminationReason::Cancelled => 0,
}
}
/// Returns the reason string for use in loop.terminate event payload.
///
/// Per spec event payload format:
/// `completed | max_iterations | max_runtime | consecutive_failures | interrupted | error`
pub fn as_str(&self) -> &'static str {
match self {
TerminationReason::CompletionPromise => "completed",
TerminationReason::MaxIterations => "max_iterations",
TerminationReason::MaxRuntime => "max_runtime",
TerminationReason::MaxCost => "max_cost",
TerminationReason::ConsecutiveFailures => "consecutive_failures",
TerminationReason::LoopThrashing => "loop_thrashing",
TerminationReason::LoopStale => "loop_stale",
TerminationReason::ValidationFailure => "validation_failure",
TerminationReason::Stopped => "stopped",
TerminationReason::Interrupted => "interrupted",
TerminationReason::RestartRequested => "restart_requested",
TerminationReason::WorkspaceGone => "workspace_gone",
TerminationReason::Cancelled => "cancelled",
}
}
/// Returns true if this is a successful completion (not an error or limit).
pub fn is_success(&self) -> bool {
matches!(self, TerminationReason::CompletionPromise)
}
}
/// The main event loop orchestrator.
pub struct EventLoop {
config: RalphConfig,
registry: HatRegistry,
bus: EventBus,
state: LoopState,
instruction_builder: InstructionBuilder,
ralph: HatlessRalph,
/// Cached human guidance messages that should persist across iterations.
robot_guidance: Vec<String>,
/// Event reader for consuming events from JSONL file.
/// Made pub(crate) to allow tests to override the path.
pub(crate) event_reader: EventReader,
diagnostics: crate::diagnostics::DiagnosticsCollector,
/// Loop context for path resolution (None for legacy single-loop mode).
loop_context: Option<LoopContext>,
/// Skill registry for the current loop.
skill_registry: SkillRegistry,
/// Robot service for human-in-the-loop communication.
/// Injected externally when `human.enabled` is true and this is the primary loop.
robot_service: Option<Box<dyn RobotService>>,
}
impl EventLoop {
/// Creates a new event loop from configuration.
pub fn new(config: RalphConfig) -> Self {
// Try to create diagnostics collector, but fall back to disabled if it fails
// (e.g., in tests without proper directory setup)
let diagnostics = crate::diagnostics::DiagnosticsCollector::new(std::path::Path::new("."))
.unwrap_or_else(|e| {
debug!(
"Failed to initialize diagnostics: {}, using disabled collector",
e
);
crate::diagnostics::DiagnosticsCollector::disabled()
});
Self::with_diagnostics(config, diagnostics)
}
/// Creates a new event loop with a loop context for path resolution.
///
/// The loop context determines where events, tasks, and other state files
/// are located. Use this for multi-loop scenarios where each loop runs
/// in an isolated workspace (git worktree).
pub fn with_context(config: RalphConfig, context: LoopContext) -> Self {
let diagnostics = crate::diagnostics::DiagnosticsCollector::new(context.workspace())
.unwrap_or_else(|e| {
debug!(
"Failed to initialize diagnostics: {}, using disabled collector",
e
);
crate::diagnostics::DiagnosticsCollector::disabled()
});
Self::with_context_and_diagnostics(config, context, diagnostics)
}
/// Creates a new event loop with explicit loop context and diagnostics.
pub fn with_context_and_diagnostics(
mut config: RalphConfig,
context: LoopContext,
diagnostics: crate::diagnostics::DiagnosticsCollector,
) -> Self {
// Solo mode safety guard: force scratchpad enabled when no hats defined
if config.hats.is_empty() && !config.core.scratchpad.enabled {
warn!(
"core.scratchpad.enabled is false but no hats are defined. \
Scratchpad is the only continuity mechanism in solo mode — forcing enabled."
);
config.core.scratchpad.enabled = true;
}
let registry = HatRegistry::from_config(&config);
let instruction_builder =
InstructionBuilder::with_events(config.core.clone(), config.events.clone());
let mut bus = EventBus::new();
// Per spec: "Hatless Ralph is constant — Cannot be replaced, overwritten, or configured away"
// Ralph is ALWAYS registered as the universal fallback for orphaned events.
// Custom hats are registered first (higher priority), Ralph catches everything else.
for hat in registry.all() {
bus.register(hat.clone());
}
// Always register Ralph as catch-all coordinator
// Per spec: "Ralph runs when no hat triggered — Universal fallback for orphaned events"
let ralph_hat = ralph_proto::Hat::new("ralph", "Ralph").subscribe("*"); // Subscribe to all events
bus.register(ralph_hat);
if registry.is_empty() {
debug!("Solo mode: Ralph is the only coordinator");
} else {
debug!(
"Multi-hat mode: {} custom hats + Ralph as fallback",
registry.len()
);
}
// Build skill registry from config
let mut skill_registry = if config.skills.enabled {
SkillRegistry::from_config(
&config.skills,
context.workspace(),
Some(config.cli.backend.as_str()),
)
.unwrap_or_else(|e| {
warn!(
"Failed to build skill registry: {}, using empty registry",
e
);
SkillRegistry::new(Some(config.cli.backend.as_str()))
})
} else {
SkillRegistry::new(Some(config.cli.backend.as_str()))
};
// Remove task/memory skills from the index when their config is disabled
if !config.tasks.enabled {
skill_registry.remove("ralph-tools-tasks");
}
if !config.memories.enabled {
skill_registry.remove("ralph-tools-memories");
}
let skill_index = if config.skills.enabled {
skill_registry.build_index(None)
} else {
String::new()
};
// When memories are enabled, add tasks CLI instructions alongside scratchpad
let ralph = HatlessRalph::new(
config.event_loop.completion_promise.clone(),
config.core.clone(),
®istry,
config.event_loop.starting_event.clone(),
)
.with_memories_enabled(config.memories.enabled)
.with_skill_index(skill_index);
// Read timestamped events path from marker file, fall back to default
// The marker file contains a relative path like ".ralph/events-20260127-123456.jsonl"
// which we resolve relative to the workspace root
let events_path = std::fs::read_to_string(context.current_events_marker())
.map(|s| {
let relative = s.trim();
context.workspace().join(relative)
})
.unwrap_or_else(|_| context.events_path());
let event_reader = EventReader::new(&events_path);
Self {
config,
registry,
bus,
state: LoopState::new(),
instruction_builder,
ralph,
robot_guidance: Vec::new(),
event_reader,
diagnostics,
loop_context: Some(context),
skill_registry,
robot_service: None,
}
}
/// Creates a new event loop with explicit diagnostics collector (for testing).
pub fn with_diagnostics(
mut config: RalphConfig,
diagnostics: crate::diagnostics::DiagnosticsCollector,
) -> Self {
// Solo mode safety guard: force scratchpad enabled when no hats defined
if config.hats.is_empty() && !config.core.scratchpad.enabled {
warn!(
"core.scratchpad.enabled is false but no hats are defined. \
Scratchpad is the only continuity mechanism in solo mode — forcing enabled."
);
config.core.scratchpad.enabled = true;
}
let registry = HatRegistry::from_config(&config);
let instruction_builder =
InstructionBuilder::with_events(config.core.clone(), config.events.clone());
let mut bus = EventBus::new();
// Per spec: "Hatless Ralph is constant — Cannot be replaced, overwritten, or configured away"
// Ralph is ALWAYS registered as the universal fallback for orphaned events.
// Custom hats are registered first (higher priority), Ralph catches everything else.
for hat in registry.all() {
bus.register(hat.clone());
}
// Always register Ralph as catch-all coordinator
// Per spec: "Ralph runs when no hat triggered — Universal fallback for orphaned events"
let ralph_hat = ralph_proto::Hat::new("ralph", "Ralph").subscribe("*"); // Subscribe to all events
bus.register(ralph_hat);
if registry.is_empty() {
debug!("Solo mode: Ralph is the only coordinator");
} else {
debug!(
"Multi-hat mode: {} custom hats + Ralph as fallback",
registry.len()
);
}
// Build skill registry from config
let workspace_root = std::path::Path::new(".");
let mut skill_registry = if config.skills.enabled {
SkillRegistry::from_config(
&config.skills,
workspace_root,
Some(config.cli.backend.as_str()),
)
.unwrap_or_else(|e| {
warn!(
"Failed to build skill registry: {}, using empty registry",
e
);
SkillRegistry::new(Some(config.cli.backend.as_str()))
})
} else {
SkillRegistry::new(Some(config.cli.backend.as_str()))
};
// Remove task/memory skills from the index when their config is disabled
if !config.tasks.enabled {
skill_registry.remove("ralph-tools-tasks");
}
if !config.memories.enabled {
skill_registry.remove("ralph-tools-memories");
}
let skill_index = if config.skills.enabled {
skill_registry.build_index(None)
} else {
String::new()
};
// When memories are enabled, add tasks CLI instructions alongside scratchpad
let ralph = HatlessRalph::new(
config.event_loop.completion_promise.clone(),
config.core.clone(),
®istry,
config.event_loop.starting_event.clone(),
)
.with_memories_enabled(config.memories.enabled)
.with_skill_index(skill_index);
// Read events path from marker file, fall back to default if not present
// The marker file is written by run_loop_impl() at run startup
let events_path = std::fs::read_to_string(".ralph/current-events")
.map(|s| s.trim().to_string())
.unwrap_or_else(|_| ".ralph/events.jsonl".to_string());
let event_reader = EventReader::new(&events_path);
Self {
config,
registry,
bus,
state: LoopState::new(),
instruction_builder,
ralph,
robot_guidance: Vec::new(),
event_reader,
diagnostics,
loop_context: None,
skill_registry,
robot_service: None,
}
}
/// Injects a robot service for human-in-the-loop communication.
///
/// Call this after construction to enable `human.interact` event handling,
/// periodic check-ins, and question/response flow. The service is typically
/// created by the CLI layer (e.g., `TelegramService`) and injected here,
/// keeping the core event loop decoupled from any specific communication
/// platform.
pub fn set_robot_service(&mut self, service: Box<dyn RobotService>) {
self.robot_service = Some(service);
}
/// Returns the loop context, if one was provided.
pub fn loop_context(&self) -> Option<&LoopContext> {
self.loop_context.as_ref()
}
/// Returns the tasks path based on loop context or default.
fn tasks_path(&self) -> PathBuf {
self.loop_context
.as_ref()
.map(|ctx| ctx.tasks_path())
.unwrap_or_else(|| PathBuf::from(".ralph/agent/tasks.jsonl"))
}
/// Returns the scratchpad path based on loop context and active scratchpad config.
///
/// When a per-hat scratchpad override is active (path differs from global default),
/// the custom path is resolved relative to the loop context workspace for worktree
/// isolation. When using the default/global path, loop context's standard resolution
/// applies.
fn scratchpad_path(&self) -> PathBuf {
let active_path = &self.ralph.active_scratchpad().path;
match self.loop_context.as_ref() {
Some(ctx) => ctx.workspace().join(active_path),
None => PathBuf::from(active_path),
}
}
/// Returns the global scratchpad path (ignoring per-hat overrides).
/// Used for guidance persistence which is cross-hat state.
fn global_scratchpad_path(&self) -> PathBuf {
self.loop_context
.as_ref()
.map(|ctx| ctx.scratchpad_path())
.unwrap_or_else(|| PathBuf::from(&self.config.core.scratchpad.path))
}
/// Returns the current loop state.
pub fn state(&self) -> &LoopState {
&self.state
}
/// Resets the stale-loop topic counter.
///
/// Call after processing wave results — multiple events with the same topic
/// (e.g. `review.done` from parallel workers) are expected and should not
/// trigger the stale loop detector.
pub fn reset_stale_topic_counter(&mut self) {
self.state.consecutive_same_signature = 0;
self.state.last_emitted_signature = None;
}
/// Returns the configuration.
pub fn config(&self) -> &RalphConfig {
&self.config
}
/// Returns the hat registry.
pub fn registry(&self) -> &HatRegistry {
&self.registry
}
/// Records hook telemetry for diagnostics.
pub fn log_hook_run_telemetry(&self, entry: crate::diagnostics::HookRunTelemetryEntry) {
self.diagnostics.log_hook_run(entry);
}
/// Logs the full prompt for an iteration to the diagnostics session.
pub fn log_prompt(&self, iteration: u32, hat: &str, prompt: &str) {
self.diagnostics.log_prompt(iteration, hat, prompt);
}
/// Gets the backend configuration for a hat.
///
/// If the hat has a backend configured, returns that.
/// Otherwise, returns None (caller should use global backend).
pub fn get_hat_backend(&self, hat_id: &HatId) -> Option<&HatBackend> {
self.registry
.get_config(hat_id)
.and_then(|config| config.backend.as_ref())
}
/// Adds an observer that receives all published events.
///
/// Multiple observers can be added (e.g., session recorder + TUI).
/// Each observer is called before events are routed to subscribers.
pub fn add_observer<F>(&mut self, observer: F)
where
F: Fn(&Event) + Send + 'static,
{
self.bus.add_observer(observer);
}
/// Sets a single observer, clearing any existing observers.
///
/// Prefer `add_observer` when multiple observers are needed.
#[deprecated(since = "2.0.0", note = "Use add_observer instead")]
pub fn set_observer<F>(&mut self, observer: F)
where
F: Fn(&Event) + Send + 'static,
{
#[allow(deprecated)]
self.bus.set_observer(observer);
}
/// Checks if any termination condition is met.
pub fn check_termination(&self) -> Option<TerminationReason> {
let cfg = &self.config.event_loop;
if self.state.iteration >= cfg.max_iterations {
return Some(TerminationReason::MaxIterations);
}
if self.state.elapsed().as_secs() >= cfg.max_runtime_seconds {
return Some(TerminationReason::MaxRuntime);
}
if let Some(max_cost) = cfg.max_cost_usd
&& self.state.cumulative_cost >= max_cost
{
return Some(TerminationReason::MaxCost);
}
if self.state.consecutive_failures >= cfg.max_consecutive_failures {
return Some(TerminationReason::ConsecutiveFailures);
}
// Check for loop thrashing: planner keeps dispatching abandoned tasks
if self.state.abandoned_task_redispatches >= 3 {
return Some(TerminationReason::LoopThrashing);
}
// Check for validation failures: too many consecutive malformed JSONL lines
if self.state.consecutive_malformed_events >= 3 {
return Some(TerminationReason::ValidationFailure);
}
// Check for stale loop: same event signature emitted 3+ times in a row
if self.state.consecutive_same_signature >= 3 {
let topic = self
.state
.last_emitted_signature
.as_ref()
.map(|signature| signature.topic.as_str())
.unwrap_or("?");
warn!(
topic,
count = self.state.consecutive_same_signature,
"Stale loop detected: same event signature emitted consecutively"
);
return Some(TerminationReason::LoopStale);
}
// Check for stop signal from Telegram /stop or CLI stop-requested
let stop_path =
std::path::Path::new(&self.config.core.workspace_root).join(".ralph/stop-requested");
if stop_path.exists() {
let _ = std::fs::remove_file(&stop_path);
return Some(TerminationReason::Stopped);
}
// Check for restart signal from Telegram /restart command
let restart_path =
std::path::Path::new(&self.config.core.workspace_root).join(".ralph/restart-requested");
if restart_path.exists() {
return Some(TerminationReason::RestartRequested);
}
// Check if workspace directory has been removed (zombie worktree detection)
if !std::path::Path::new(&self.config.core.workspace_root).is_dir() {
return Some(TerminationReason::WorkspaceGone);
}
None
}
/// Check if a loop.cancel event was detected.
///
/// Unlike check_completion_event(), this does NOT validate required_events.
/// Cancellation is an explicit abort — it doesn't need the workflow to be complete.
pub fn check_cancellation_event(&mut self) -> Option<TerminationReason> {
if !self.state.cancellation_requested {
return None;
}
self.state.cancellation_requested = false;
info!("Loop cancelled gracefully via loop.cancel event");
self.diagnostics.log_orchestration(
self.state.iteration,
"loop",
crate::diagnostics::OrchestrationEvent::LoopTerminated {
reason: "cancelled".to_string(),
},
);
Some(TerminationReason::Cancelled)
}
/// Request completion from the text fallback path.
///
/// When a backend outputs a completion promise as plain text (without
/// using `ralph emit`), this sets `completion_requested = true` so that
/// `check_completion_event()` can apply all safety checks (persistent mode,
/// required events, runtime tasks) before terminating.
pub fn request_completion_from_text_fallback(&mut self) {
self.state.completion_requested = true;
info!("Completion requested via text fallback (output contained completion promise)");
}
/// Checks if a completion event was received and returns termination reason.
///
/// Completion is accepted via JSONL events (e.g., `ralph emit`) or via
/// [`request_completion_from_text_fallback`].
pub fn check_completion_event(&mut self) -> Option<TerminationReason> {
if !self.state.completion_requested {
return None;
}
// Event chain validation: check required events were seen
let required = &self.config.event_loop.required_events;
if !required.is_empty() {
let missing = self.state.missing_required_events(required);
if !missing.is_empty() {
warn!(
missing = ?missing,
"Rejecting LOOP_COMPLETE: required events not seen during loop lifetime"
);
self.state.completion_requested = false;
// Inject task.resume so the loop continues
let resume_payload = format!(
"LOOP_COMPLETE rejected: missing required events: {:?}. \
The agent must complete all workflow phases before emitting LOOP_COMPLETE. \
Use loop.cancel to abort the workflow instead.",
missing
);
self.bus.publish(Event::new("task.resume", resume_payload));
return None;
}
}
self.state.completion_requested = false;
// In persistent mode, suppress completion and keep the loop alive
if self.config.event_loop.persistent {
info!("Completion event suppressed - persistent mode active, loop staying alive");
self.diagnostics.log_orchestration(
self.state.iteration,
"loop",
crate::diagnostics::OrchestrationEvent::LoopTerminated {
reason: "completion_event_suppressed_persistent".to_string(),
},
);
// Inject a task.resume event so the loop continues with an idle prompt
let resume_event = Event::new(
"task.resume",
"Persistent mode: loop staying alive after completion signal. \
Check for new tasks or await human guidance.",
);
self.bus.publish(resume_event);
return None;
}
// Runtime tasks are the canonical queue when memories/tasks mode is enabled.
if self.config.memories.enabled {
if let Ok(false) = self.verify_tasks_complete() {
let open_tasks = self.get_open_task_list();
warn!(
open_tasks = ?open_tasks,
"Rejecting completion event with {} open task(s)",
open_tasks.len()
);
self.bus.publish(Event::new(
"task.resume",
format!(
"Completion rejected: runtime tasks remain open: {:?}. Close, fail, or reopen outstanding tasks before emitting the completion promise.",
open_tasks
),
));
return None;
}
} else if let Ok(false) = self.verify_scratchpad_complete() {
warn!("Completion event with pending scratchpad tasks - trusting agent decision");
}
info!("Completion event detected - terminating");
// Log loop terminated
self.diagnostics.log_orchestration(
self.state.iteration,
"loop",
crate::diagnostics::OrchestrationEvent::LoopTerminated {
reason: "completion_event".to_string(),
},
);
Some(TerminationReason::CompletionPromise)
}
/// Initializes the loop by publishing the start event.
pub fn initialize(&mut self, prompt_content: &str) {
// Use configured starting_event or default to task.start for backward compatibility
let topic = self
.config
.event_loop
.starting_event
.clone()
.unwrap_or_else(|| "task.start".to_string());
self.initialize_with_topic(&topic, prompt_content);
}
/// Initializes the loop for resume mode by publishing task.resume.
///
/// Per spec: "User can run `ralph resume` to restart reading existing scratchpad."
/// The planner should read the existing scratchpad rather than doing fresh gap analysis.
pub fn initialize_resume(&mut self, prompt_content: &str) {
// Resume always uses task.resume regardless of starting_event config
self.initialize_with_topic("task.resume", prompt_content);
}
/// Common initialization logic with configurable topic.
fn initialize_with_topic(&mut self, topic: &str, prompt_content: &str) {
// Store the objective so it persists across all iterations.
// After iteration 1, bus.take_pending() consumes the start event,
// so without this the objective would be invisible to later hats.
self.ralph.set_objective(prompt_content.to_string());
let start_event = Event::new(topic, prompt_content);
self.bus.publish(start_event);
debug!(topic = topic, "Published {} event", topic);
}
/// Gets the next hat to execute (if any have pending events).
///
/// Per "Hatless Ralph" architecture: When custom hats are defined, Ralph is
/// always the executor. Custom hats define topology (pub/sub contracts) that
/// Ralph uses for coordination context, but Ralph handles all iterations.
///
/// - Solo mode (no custom hats): Returns "ralph" if Ralph has pending events
/// - Multi-hat mode (custom hats defined): Always returns "ralph" if ANY hat has pending events
pub fn next_hat(&self) -> Option<&HatId> {
let next = self.bus.next_hat_with_pending();
// If no pending hat events but human interactions are pending, route to Ralph.
if next.is_none() && self.bus.has_human_pending() {
return self.bus.hat_ids().find(|id| id.as_str() == "ralph");
}
// If no pending events, return None
next.as_ref()?;
// In multi-hat mode, always route to Ralph (custom hats define topology only)
// Ralph's prompt includes the ## HATS section for coordination awareness
if self.registry.is_empty() {
// Solo mode - return the next hat (which is "ralph")
next
} else {
// Return "ralph" - the constant coordinator
// Find ralph in the bus's registered hats
self.bus.hat_ids().find(|id| id.as_str() == "ralph")
}
}
/// Advances the event reader to the current end of the events file.
///
/// Call this after writing observability records (e.g. start event) to the
/// events JSONL file so they are not re-read by `process_events_from_jsonl`.
/// The start event is already published to the bus via `initialize()`, so
/// re-reading it from the file would cause double-delivery.
pub fn sync_event_reader_to_file_end(&mut self) {
let path = self.event_reader.path();
if let Ok(metadata) = std::fs::metadata(path) {
self.event_reader.set_position(metadata.len());
}
}
/// Checks if any hats have pending events.
///
/// Use this after `process_output` to detect if the LLM failed to publish an event.
/// If false after processing, the loop will terminate on the next iteration.
pub fn has_pending_events(&self) -> bool {
self.bus.next_hat_with_pending().is_some() || self.bus.has_human_pending()
}
/// Checks if any pending events are human-related (human.response, human.guidance).
///
/// Used to skip cooldown delays when a human event is next, since we don't
/// want to artificially delay the response to a human interaction.
pub fn has_pending_human_events(&self) -> bool {
self.bus.has_human_pending()
}
/// Injects `human.guidance` events directly into the in-memory bus.
///
/// This is used for local TUI/RPC guidance so the next prompt boundary
/// sees the message immediately without waiting for a JSONL reread.
pub fn inject_human_guidance<I, S>(&mut self, messages: I)
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
for message in messages {
let event = Event::new("human.guidance", message.into());
self.state.record_event(&event);
self.bus.publish(event);
}
}
/// Returns whether unread JSONL events include any semantic `plan.*` topics.
///
/// This allows callers to dispatch `pre.plan.created` hooks before
/// event publication handling without consuming unread events.
pub fn has_pending_plan_events_in_jsonl(&self) -> std::io::Result<bool> {
let result = self.event_reader.peek_new_events()?;
Ok(result
.events
.iter()
.any(|event| event.topic.starts_with("plan.")))
}
/// Returns structured context for the first unread `human.interact` event,
/// if one is present in JSONL without consuming reader state.
pub fn pending_human_interact_context_in_jsonl(&self) -> std::io::Result<Option<Value>> {
let result = self.event_reader.peek_new_events()?;
Ok(result
.events
.iter()
.find(|event| event.topic == "human.interact")
.map(|event| {
Self::parse_human_interact_context(event.payload.as_deref().unwrap_or_default())
}))
}
/// Gets the topics a hat is allowed to publish.
///
/// Used to build retry prompts when the LLM forgets to publish an event.
pub fn get_hat_publishes(&self, hat_id: &HatId) -> Vec<String> {
self.registry
.get(hat_id)
.map(|hat| hat.publishes.iter().map(|t| t.to_string()).collect())
.unwrap_or_default()
}
/// Injects a fallback event to recover from a stalled loop.
///
/// When no hats have pending events (agent failed to publish), this method
/// injects a `task.resume` event which Ralph will handle to attempt recovery.
///
/// Returns true if a fallback event was injected, false if recovery is not possible.
pub fn inject_fallback_event(&mut self) -> bool {
// If a custom hat was last executing, target the fallback back to it
// This preserves hat context instead of always falling back to Ralph
let fallback_event = match &self.state.last_hat {
Some(hat_id) if hat_id.as_str() != "ralph" => {
let publishes = self.get_hat_publishes(hat_id);
let payload = if publishes.is_empty() {
format!(
"RECOVERY: Previous iteration by hat `{}` did not publish an event. \
Emit exactly one valid next event via `ralph emit`, or stop only after \
publishing the configured completion event.",
hat_id.as_str()
)
} else {
format!(
"RECOVERY: Previous iteration by hat `{}` did not publish an event. \
This failed because no event was emitted. Emit exactly ONE valid next \
event via `ralph emit`. Allowed topics: `{}`. Do not only write prose \
or update files. Stop immediately after emitting.",
hat_id.as_str(),
publishes.join("`, `")
)
};
debug!(
hat = %hat_id.as_str(),
"Injecting fallback event to recover - targeting last hat with task.resume"
);
Event::new("task.resume", payload).with_target(hat_id.clone())
}
_ => {
debug!("Injecting fallback event to recover - triggering Ralph with task.resume");
Event::new(
"task.resume",
"RECOVERY: Previous iteration did not publish an event. \
Review the scratchpad and either dispatch the next task or complete the loop.",
)
}
};
self.bus.publish(fallback_event);
true
}
/// Builds the prompt for a hat's execution.
///
/// Per "Hatless Ralph" architecture:
/// - Solo mode: Ralph handles everything with his own prompt
/// - Multi-hat mode: Ralph is the sole executor, custom hats define topology only
///
/// When in multi-hat mode, this method collects ALL pending events across all hats
/// and builds Ralph's prompt with that context. The `## HATS` section in Ralph's
/// prompt documents the topology for coordination awareness.
///
/// If memories are configured with `inject: auto`, this method also prepends
/// primed memories to the prompt context. If a scratchpad file exists and is
/// non-empty, its content is also prepended (before memories).
pub fn build_prompt(&mut self, hat_id: &HatId) -> Option<String> {
// Handle "ralph" hat - the constant coordinator
// Per spec: "Hatless Ralph is constant — Cannot be replaced, overwritten, or configured away"
if hat_id.as_str() == "ralph" {
if self.registry.is_empty() {
// Solo mode - just Ralph's events, no hats to filter
let mut events = self.bus.take_pending(&hat_id.clone());
let mut human_events = self.bus.take_human_pending();
events.append(&mut human_events);
// Separate human.guidance events from regular events
let (guidance_events, regular_events): (Vec<_>, Vec<_>) = events
.into_iter()
.partition(|e| e.topic.as_str() == "human.guidance");
let events_context = regular_events
.iter()
.map(|e| Self::format_event(e))
.collect::<Vec<_>>()
.join("\n");
// Solo mode: set scratchpad and iteration before guidance persistence
self.ralph
.set_active_scratchpad(self.config.core.scratchpad.clone());
self.ralph.set_iteration(self.state.iteration);
// Persist and inject human guidance into prompt if present
self.update_robot_guidance(guidance_events);
self.apply_robot_guidance();
// Build base prompt and prepend memories + scratchpad + ready tasks
let base_prompt = self.ralph.build_prompt(&events_context, &[]);
self.ralph.clear_robot_guidance();
let with_skills = self.prepend_auto_inject_skills(base_prompt);
let with_scratchpad = self.prepend_scratchpad(with_skills);
let final_prompt = self.prepend_ready_tasks(with_scratchpad);
debug!("build_prompt: routing to HatlessRalph (solo mode)");
return Some(final_prompt);
} else {
// Multi-hat mode: collect events and determine active hats
let mut all_hat_ids: Vec<HatId> = self.bus.hat_ids().cloned().collect();
// Deterministic ordering (avoid HashMap iteration order nondeterminism).
all_hat_ids.sort_by(|a, b| a.as_str().cmp(b.as_str()));
let mut all_events = Vec::new();
let mut system_events = Vec::new();
for id in &all_hat_ids {
let pending = self.bus.take_pending(id);
if pending.is_empty() {
continue;
}
let (drop_pending, exhausted_event) = self.check_hat_exhaustion(id, &pending);
if drop_pending {
// Drop the pending events that would have activated the hat.
if let Some(exhausted_event) = exhausted_event {
all_events.push(exhausted_event.clone());
system_events.push(exhausted_event);
}
continue;
}
all_events.extend(pending);
}
let mut human_events = self.bus.take_human_pending();
all_events.append(&mut human_events);
// Publish orchestrator-generated system events after consuming pending events,
// so they become visible in the event log and can be handled next iteration.
for event in system_events {
self.bus.publish(event);
}
// Separate human.guidance events from regular events
let (guidance_events, regular_events): (Vec<_>, Vec<_>) = all_events
.into_iter()
.partition(|e| e.topic.as_str() == "human.guidance");
// Ignore kickoff/recovery noise when a real downstream event is pending.
let effective_regular_events = self.effective_regular_events(®ular_events);
// Determine which hats are active based on regular events
let active_hat_ids = self.determine_active_hat_ids(®ular_events);
self.record_hat_activations(&active_hat_ids);
self.state.last_active_hat_ids = active_hat_ids.clone();
// Resolve scratchpad config for the active hat (or global default).
// Must happen BEFORE guidance persistence so guidance is written
// to the correct hat's scratchpad file.
let resolved_scratchpad = if let Some(hat_id) = active_hat_ids.first() {
let hat_scratchpad = self
.registry
.get_config(hat_id)
.and_then(|c| c.scratchpad.as_ref());
ScratchpadConfig::resolve(hat_scratchpad, &self.config.core.scratchpad)
} else {
// Ralph coordinating — use global
self.config.core.scratchpad.clone()
};
self.ralph.set_active_scratchpad(resolved_scratchpad);
self.ralph.set_iteration(self.state.iteration);
// Persist and inject human guidance after scratchpad resolution
// (must also happen before immutable borrows from determine_active_hats)
self.update_robot_guidance(guidance_events);
self.apply_robot_guidance();
let active_hats = self.determine_active_hats(®ular_events);
// Format events for context
let events_context = effective_regular_events
.iter()
.map(|e| Self::format_event(e))
.collect::<Vec<_>>()
.join("\n");
// Build base prompt and prepend memories + scratchpad if available
let base_prompt = self.ralph.build_prompt(&events_context, &active_hats);
// Build prompt with active hats - filters instructions to only active hats
debug!(
"build_prompt: routing to HatlessRalph (multi-hat coordinator mode), active_hats: {:?}",
active_hats
.iter()
.map(|h| h.id.as_str())
.collect::<Vec<_>>()
);
// Clear guidance after active_hats references are no longer needed
self.ralph.clear_robot_guidance();
let with_skills = self.prepend_auto_inject_skills(base_prompt);
let with_scratchpad = self.prepend_scratchpad(with_skills);
let final_prompt = self.prepend_ready_tasks(with_scratchpad);
return Some(final_prompt);
}
}
// Non-ralph hat requested - this shouldn't happen in multi-hat mode since
// next_hat() always returns "ralph" when custom hats are defined.
// But we keep this code path for backward compatibility and tests.
let events = self.bus.take_pending(&hat_id.clone());
let events_context = events
.iter()
.map(|e| Self::format_event(e))
.collect::<Vec<_>>()
.join("\n");
let hat = self.registry.get(hat_id)?;
// Debug logging to trace hat routing
debug!(
"build_prompt: hat_id='{}', instructions.is_empty()={}",
hat_id.as_str(),
hat.instructions.is_empty()
);
// All hats use build_custom_hat with ghuntley-style prompts
debug!(
"build_prompt: routing to build_custom_hat() for '{}'",
hat_id.as_str()
);
Some(
self.instruction_builder
.build_custom_hat(hat, &events_context),
)
}
/// Stores guidance payloads, persists them to scratchpad, and prepares them for prompt injection.
///
/// Guidance events are ephemeral in the event bus (consumed by `take_pending`).
/// This method both caches them in memory for prompt injection and appends
/// them to the scratchpad file so they survive across process restarts.
fn update_robot_guidance(&mut self, guidance_events: Vec<Event>) {
if guidance_events.is_empty() {
return;
}
// Persist new guidance to scratchpad before caching
self.persist_guidance_to_scratchpad(&guidance_events);
self.robot_guidance
.extend(guidance_events.into_iter().map(|e| e.payload));
}
/// Appends human guidance entries to the scratchpad file for durability.
///
/// Each guidance message is written as a timestamped markdown entry so it
/// appears alongside the agent's own thinking and survives process restarts.
///
/// When scratchpad is disabled for the current hat, persists to the global
/// scratchpad path (guidance is cross-hat state). If global is also disabled,
/// skips persistence.
fn persist_guidance_to_scratchpad(&self, guidance_events: &[Event]) {
use std::io::Write;
// When hat scratchpad is disabled, fall back to global scratchpad
let scratchpad_path = if self.ralph.active_scratchpad().enabled {
self.scratchpad_path()
} else {
if !self.config.core.scratchpad.enabled {
debug!("Both hat and global scratchpad disabled, skipping guidance persistence");
return;
}
self.global_scratchpad_path()
};
let resolved_path = if scratchpad_path.is_relative() {
self.config.core.workspace_root.join(&scratchpad_path)
} else {
scratchpad_path
};
// Create parent directories if needed
if let Some(parent) = resolved_path.parent()
&& !parent.exists()
&& let Err(e) = std::fs::create_dir_all(parent)
{
warn!("Failed to create scratchpad directory: {}", e);
return;
}
let mut file = match std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(&resolved_path)
{
Ok(f) => f,
Err(e) => {
warn!("Failed to open scratchpad for guidance persistence: {}", e);
return;
}
};
let timestamp = chrono::Utc::now().format("%Y-%m-%d %H:%M:%S UTC");
for event in guidance_events {
let entry = format!(
"\n### HUMAN GUIDANCE ({})\n\n{}\n",
timestamp, event.payload
);
if let Err(e) = file.write_all(entry.as_bytes()) {
warn!("Failed to write guidance to scratchpad: {}", e);
}
}
info!(
count = guidance_events.len(),
"Persisted human guidance to scratchpad"
);
}
/// Injects cached guidance into the next prompt build.
fn apply_robot_guidance(&mut self) {
if self.robot_guidance.is_empty() {
return;
}
self.ralph.set_robot_guidance(self.robot_guidance.clone());
}
/// Prepends auto-injected skill content to the prompt.
///
/// This generalizes the former `prepend_memories()` into a skill auto-injection
/// pipeline that handles memories, tools, and any other auto-inject skills.
///
/// Injection order:
/// 1. Memory data + ralph-tools skill (special case: loads memory data from store, applies budget)
/// 2. RObot interaction skill (gated by `robot.enabled`)
/// 3. Other auto-inject skills from the registry (wrapped in XML tags)
fn prepend_auto_inject_skills(&self, prompt: String) -> String {
let mut prefix = String::new();
// 1. Memory data + ralph-tools skill — special case with data loading
self.inject_memories_and_tools_skill(&mut prefix);
// 2. RObot interaction skill — gated by robot.enabled
self.inject_robot_skill(&mut prefix);
// 3. Other auto-inject skills from the registry
self.inject_custom_auto_skills(&mut prefix);
if prefix.is_empty() {
return prompt;
}
prefix.push_str("\n\n");
prefix.push_str(&prompt);
prefix
}
/// Injects memory data and the ralph-tools skill into the prefix.
///
/// Special case: loads memory entries from the store, applies budget
/// truncation, then appends the ralph-tools skill content (which covers
/// both tasks and memories CLI usage).
/// Memory data is gated by `memories.enabled && memories.inject == Auto`.
/// The ralph-tools skill is injected when either memories or tasks are enabled.
fn inject_memories_and_tools_skill(&self, prefix: &mut String) {
let memories_config = &self.config.memories;
// Inject memory DATA if memories are enabled with auto-inject
if memories_config.enabled && memories_config.inject == InjectMode::Auto {
info!(
"Memory injection check: enabled={}, inject={:?}, workspace_root={:?}",
memories_config.enabled, memories_config.inject, self.config.core.workspace_root
);
let workspace_root = &self.config.core.workspace_root;
let store = MarkdownMemoryStore::with_default_path(workspace_root);
let memories_path = workspace_root.join(".ralph/agent/memories.md");
info!(
"Looking for memories at: {:?} (exists: {})",
memories_path,
memories_path.exists()
);
let memories = match store.load() {
Ok(memories) => {
info!("Successfully loaded {} memories from store", memories.len());
memories
}
Err(e) => {
info!(
"Failed to load memories for injection: {} (path: {:?})",
e, memories_path
);
Vec::new()
}
};
if memories.is_empty() {
info!("Memory store is empty - no memories to inject");
} else {
let mut memories_content = format_memories_as_markdown(&memories);
if memories_config.budget > 0 {
let original_len = memories_content.len();
memories_content =
truncate_to_budget(&memories_content, memories_config.budget);
debug!(
"Applied budget: {} chars -> {} chars (budget: {})",
original_len,
memories_content.len(),
memories_config.budget
);
}
info!(
"Injecting {} memories ({} chars) into prompt",
memories.len(),
memories_content.len()
);
prefix.push_str(&memories_content);
}
}
// Inject ralph-tools skills conditionally based on config
let tasks_enabled = self.config.tasks.enabled;
// Base skill (shared commands) when either memories or tasks are enabled
if (memories_config.enabled || tasks_enabled)
&& let Some(skill) = self.skill_registry.get("ralph-tools")
{
if !prefix.is_empty() {
prefix.push_str("\n\n");
}
prefix.push_str(&format!(
"<ralph-tools-skill>\n{}\n</ralph-tools-skill>",
skill.content.trim()
));
debug!("Injected ralph-tools skill from registry");
}
// Tasks skill — only when tasks are enabled
if tasks_enabled && let Some(skill) = self.skill_registry.get("ralph-tools-tasks") {
if !prefix.is_empty() {
prefix.push_str("\n\n");
}
prefix.push_str(&format!(
"<ralph-tools-tasks-skill>\n{}\n</ralph-tools-tasks-skill>",
skill.content.trim()
));
debug!("Injected ralph-tools-tasks skill from registry");
}
// Memories skill — only when memories are enabled
if memories_config.enabled
&& let Some(skill) = self.skill_registry.get("ralph-tools-memories")
{
if !prefix.is_empty() {
prefix.push_str("\n\n");
}
prefix.push_str(&format!(
"<ralph-tools-memories-skill>\n{}\n</ralph-tools-memories-skill>",
skill.content.trim()
));
debug!("Injected ralph-tools-memories skill from registry");
}
}
/// Injects the RObot interaction skill content into the prefix.
///
/// Gated by `robot.enabled`. Teaches agents how and when to interact
/// with humans via `human.interact` events.
fn inject_robot_skill(&self, prefix: &mut String) {
if !self.config.robot.enabled {
return;
}
if let Some(skill) = self.skill_registry.get("robot-interaction") {
if !prefix.is_empty() {
prefix.push_str("\n\n");
}
prefix.push_str(&format!(
"<robot-skill>\n{}\n</robot-skill>",
skill.content.trim()
));
debug!("Injected robot interaction skill from registry");
}
}
/// Injects any user-configured auto-inject skills (excluding built-in skills handled separately).
fn inject_custom_auto_skills(&self, prefix: &mut String) {
for skill in self.skill_registry.auto_inject_skills(None) {
// Skip built-in skills handled above
if matches!(
skill.name.as_str(),
"ralph-tools" | "ralph-tools-tasks" | "ralph-tools-memories" | "robot-interaction"
) {
continue;
}
if !prefix.is_empty() {
prefix.push_str("\n\n");
}
prefix.push_str(&format!(
"<{name}-skill>\n{content}\n</{name}-skill>",
name = skill.name,
content = skill.content.trim()
));
debug!("Injected auto-inject skill: {}", skill.name);
}
}
/// Prepends scratchpad content to the prompt if the file exists and is non-empty.
///
/// The scratchpad is the agent's working memory for the current objective.
/// Auto-injecting saves one tool call per iteration.
/// When the file exceeds the budget, the TAIL is kept (most recent entries).
fn prepend_scratchpad(&self, prompt: String) -> String {
// Skip injection when scratchpad is disabled for the current hat
if !self.ralph.active_scratchpad().enabled {
return prompt;
}
let scratchpad_path = self.scratchpad_path();
let resolved_path = if scratchpad_path.is_relative() {
self.config.core.workspace_root.join(&scratchpad_path)
} else {
scratchpad_path
};
if !resolved_path.exists() {
debug!(
"Scratchpad not found at {:?}, skipping injection",
resolved_path
);
return prompt;
}
let content = match std::fs::read_to_string(&resolved_path) {
Ok(c) => c,
Err(e) => {
info!("Failed to read scratchpad for injection: {}", e);
return prompt;
}
};
if content.trim().is_empty() {
debug!("Scratchpad is empty, skipping injection");
return prompt;
}
// Budget: 4000 tokens ~16000 chars. Keep the TAIL (most recent content).
let char_budget = 4000 * 4;
let content = if content.len() > char_budget {
// Find a line boundary near the start of the tail
let start = content.len() - char_budget;
// Ensure we start at a valid UTF-8 character boundary
let start = floor_char_boundary(&content, start);
let line_start = content[start..].find('\n').map_or(start, |n| start + n + 1);
let discarded = &content[..line_start];
// Summarize discarded content by extracting markdown headings
let headings: Vec<&str> = discarded
.lines()
.filter(|line| line.starts_with('#'))
.collect();
let summary = if headings.is_empty() {
format!(
"<!-- earlier content truncated ({} chars omitted) -->",
line_start
)
} else {
format!(
"<!-- earlier content truncated ({} chars omitted) -->\n\
<!-- discarded sections: {} -->",
line_start,
headings.join(" | ")
)
};
format!("{}\n\n{}", summary, &content[line_start..])
} else {
content
};
info!("Injecting scratchpad ({} chars) into prompt", content.len());
let mut final_prompt = format!(
"<scratchpad path=\"{}\">\n{}\n</scratchpad>\n\n",
self.ralph.active_scratchpad().path,
content
);
final_prompt.push_str(&prompt);
final_prompt
}
/// Prepends ready tasks to the prompt if tasks are enabled and any exist.
///
/// Loads the task store and formats ready (unblocked, open) tasks into
/// a `<ready-tasks>` XML block. This saves the agent a tool call per
/// iteration and puts tasks at the same prominence as the scratchpad.
fn prepend_ready_tasks(&self, prompt: String) -> String {
if !self.config.tasks.enabled {
return prompt;
}
use crate::task::TaskStatus;
use crate::task_store::TaskStore;
let tasks_path = self.tasks_path();
let resolved_path = if tasks_path.is_relative() {
self.config.core.workspace_root.join(&tasks_path)
} else {
tasks_path
};
if !resolved_path.exists() {
return prompt;
}
let store = match TaskStore::load(&resolved_path) {
Ok(s) => s,
Err(e) => {
info!("Failed to load task store for injection: {}", e);
return prompt;
}
};
let current_loop_id = self.current_loop_id();
let ready = Self::filter_tasks_by_loop(store.ready(), current_loop_id.as_deref());
let open = Self::filter_tasks_by_loop(store.open(), current_loop_id.as_deref());
let all_count =
Self::filter_tasks_by_loop(store.all().iter().collect(), current_loop_id.as_deref())
.len();
let closed_count = all_count - open.len();
if open.is_empty() && closed_count == 0 {
return prompt;
}
let mut section = String::from("<ready-tasks>\n");
if ready.is_empty() && open.is_empty() {
section.push_str("No open tasks. Create tasks with `ralph tools task add`.\n");
} else {
section.push_str(&format!(
"## Tasks: {} ready, {} open, {} closed\n\n",
ready.len(),
open.len(),
closed_count
));
for task in &ready {
let status_icon = match task.status {
TaskStatus::Open => "[ ]",
TaskStatus::InProgress => "[~]",
_ => "[?]",
};
section.push_str(&format!(
"- {} [P{}] {} ({}){}\n",
status_icon,
task.priority,
task.title,
task.id,
task.key
.as_deref()
.map(|key| format!(" — key: {key}"))
.unwrap_or_default()
));
}
// Show blocked tasks separately so agent knows they exist
let ready_ids: Vec<&str> = ready.iter().map(|t| t.id.as_str()).collect();
let blocked: Vec<_> = open
.iter()
.filter(|t| !ready_ids.contains(&t.id.as_str()))
.collect();
if !blocked.is_empty() {
section.push_str("\nBlocked:\n");
for task in blocked {
section.push_str(&format!(
"- [blocked] [P{}] {} ({}){} — blocked by: {}\n",
task.priority,
task.title,
task.id,
task.key
.as_deref()
.map(|key| format!(" — key: {key}"))
.unwrap_or_default(),
task.blocked_by.join(", ")
));
}
}
}
section.push_str("</ready-tasks>\n\n");
info!(
"Injecting ready tasks ({} ready, {} open, {} closed) into prompt",
ready.len(),
open.len(),
closed_count
);
let mut final_prompt = section;
final_prompt.push_str(&prompt);
final_prompt
}
/// Builds the Ralph prompt (coordination mode).
pub fn build_ralph_prompt(&self, prompt_content: &str) -> String {
self.ralph.build_prompt(prompt_content, &[])
}
/// Determines which hats should be active based on pending events.
/// Returns list of Hat references that are triggered by any pending event.
fn determine_active_hats(&self, events: &[Event]) -> Vec<&Hat> {
let mut active_hats = Vec::new();
for id in self.determine_active_hat_ids(events) {
if let Some(hat) = self.registry.get(&id) {
active_hats.push(hat);
}
}
active_hats
}
fn determine_active_hat_ids(&self, events: &[Event]) -> Vec<HatId> {
let mut entrypoint_hat_ids = Vec::new();
let mut progressed_hat_ids = Vec::new();
for event in events {
// Prefer direct event target over topic-based lookup
let hat_id = if let Some(target) = &event.target
&& self.registry.get(target).is_some()
{
target.clone()
} else if let Some(hat) = self.registry.get_for_topic(event.topic.as_str()) {
hat.id.clone()
} else {
continue;
};
let list = if self.is_entrypoint_topic(event.topic.as_str()) {
&mut entrypoint_hat_ids
} else {
&mut progressed_hat_ids
};
if !list.iter().any(|id| id == &hat_id) {
list.push(hat_id);
}
}
// Prefer progressed hats over entrypoint hats. Entrypoint events
// (starting_event, task.start, task.resume) linger in the bus after
// the first hat runs. Including them would re-activate the first hat
// alongside downstream hats, confusing the agent with multiple hat
// instructions when only the downstream hat should run.
if progressed_hat_ids.is_empty() {
entrypoint_hat_ids
} else {
progressed_hat_ids
}
}
fn effective_regular_events<'a>(&self, events: &'a [Event]) -> Vec<&'a Event> {
let has_downstream_event = events
.iter()
.any(|event| !Self::is_kickoff_or_recovery_event(event.topic.as_str()));
events
.iter()
.filter(|event| {
!has_downstream_event || !Self::is_kickoff_or_recovery_event(event.topic.as_str())
})
.collect()
}
fn is_kickoff_or_recovery_event(topic: &str) -> bool {
topic == "task.start" || topic == "task.resume" || topic.strip_suffix(".start").is_some()
}
fn is_entrypoint_topic(&self, topic: &str) -> bool {
topic == "task.start"
|| topic == "task.resume"
|| topic.strip_suffix(".start").is_some()
|| self.config.event_loop.starting_event.as_deref() == Some(topic)
}
fn peek_pending_regular_events(&self) -> Vec<Event> {
let mut events = Vec::new();
for hat_id in self.bus.hat_ids() {
if let Some(pending) = self.bus.peek_pending(hat_id) {
events.extend(pending.iter().cloned());
}
}
events
}
/// Formats an event for prompt context.
///
/// For top-level prompts (task.start, task.resume), wraps the payload in
/// `<top-level-prompt>` XML tags to clearly delineate the user's original request.
fn format_event(event: &Event) -> String {
let topic = &event.topic;
let payload = &event.payload;
if topic.as_str() == "task.start" || topic.as_str() == "task.resume" {
format!(
"Event: {} - <top-level-prompt>\n{}\n</top-level-prompt>",
topic, payload
)
} else {
format!("Event: {} - {}", topic, payload)
}
}
fn check_hat_exhaustion(&mut self, hat_id: &HatId, dropped: &[Event]) -> (bool, Option<Event>) {
let Some(config) = self.registry.get_config(hat_id) else {
return (false, None);
};
let Some(max) = config.max_activations else {
return (false, None);
};
let count = *self.state.hat_activation_counts.get(hat_id).unwrap_or(&0);
if count < max {
return (false, None);
}
// Emit only once per hat per run (avoid flooding).
let should_emit = self.state.exhausted_hats.insert(hat_id.clone());
if !should_emit {
// Hat is already exhausted - drop pending events silently.
return (true, None);
}
let mut dropped_topics: Vec<String> = dropped.iter().map(|e| e.topic.to_string()).collect();
dropped_topics.sort();
let payload = format!(
"Hat '{hat}' exhausted.\n- max_activations: {max}\n- activations: {count}\n- dropped_topics:\n - {topics}",
hat = hat_id.as_str(),
max = max,
count = count,
topics = dropped_topics.join("\n - ")
);
warn!(
hat = %hat_id.as_str(),
max_activations = max,
activations = count,
"Hat exhausted (max_activations reached)"
);
(
true,
Some(Event::new(
format!("{}.exhausted", hat_id.as_str()),
payload,
)),
)
}
fn record_hat_activations(&mut self, active_hat_ids: &[HatId]) {
for hat_id in active_hat_ids {
*self
.state
.hat_activation_counts
.entry(hat_id.clone())
.or_insert(0) += 1;
}
}
/// Returns the primary active hat ID for display purposes.
/// Returns the first active hat, or "ralph" if no specific hat is active.
/// BTreeMap iteration is already sorted by key.
pub fn get_active_hat_id(&self) -> HatId {
let pending_events = self.peek_pending_regular_events();
if let Some(active_hat_id) = self
.determine_active_hat_ids(&pending_events)
.into_iter()
.next()
{
return active_hat_id;
}
HatId::new("ralph")
}
/// Injects a default event for a hat when the agent wrote no events.
///
/// Call this after `process_events_from_jsonl` returns `Ok(false)` (no events found).
/// If the hat has `default_publishes` configured, this injects the default event.
///
/// If the default topic matches the completion promise, `completion_requested` is set
/// so the loop can terminate. Without this, completion events injected via
/// `default_publishes` would only be published to the bus (triggering downstream hats)
/// but never detected by `check_completion_event`, causing an infinite loop.
pub fn check_default_publishes(&mut self, hat_id: &HatId) {
if let Some(config) = self.registry.get_config(hat_id)
&& let Some(default_topic) = &config.default_publishes
{
let default_event = Event::new(default_topic.as_str(), "").with_source(hat_id.clone());
debug!(
hat = %hat_id.as_str(),
topic = %default_topic,
"No events written by hat, injecting default_publishes event"
);
self.state.record_event(&default_event);
// If the default topic is the completion promise, set the flag directly.
// The normal path (process_events_from_jsonl) sets this when reading from
// JSONL, but default_publishes bypasses JSONL entirely.
if default_topic.as_str() == self.config.event_loop.completion_promise {
info!(
hat = %hat_id.as_str(),
topic = %default_topic,
"default_publishes matches completion_promise — requesting termination"
);
self.state.completion_requested = true;
}
self.bus.publish(default_event);
}
}
/// Returns a mutable reference to the event bus for direct event publishing.
///
/// This is primarily used for planning sessions to inject user responses
/// as events into the orchestration loop.
pub fn bus(&mut self) -> &mut EventBus {
&mut self.bus
}
/// Processes output from a hat execution.
///
/// Returns the termination reason if the loop should stop.
pub fn process_output(
&mut self,
hat_id: &HatId,
output: &str,
success: bool,
) -> Option<TerminationReason> {
self.state.iteration += 1;
self.state.last_hat = Some(hat_id.clone());
// Periodic robot check-in
if let Some(interval_secs) = self.config.robot.checkin_interval_seconds
&& let Some(ref robot_service) = self.robot_service
{
let elapsed = self.state.elapsed();
let interval = std::time::Duration::from_secs(interval_secs);
let last = self
.state
.last_checkin_at
.map(|t| t.elapsed())
.unwrap_or(elapsed);
if last >= interval {
let context = self.build_checkin_context(hat_id);
match robot_service.send_checkin(self.state.iteration, elapsed, Some(&context)) {
Ok(_) => {
self.state.last_checkin_at = Some(std::time::Instant::now());
debug!(iteration = self.state.iteration, "Sent robot check-in");
}
Err(e) => {
warn!(error = %e, "Failed to send robot check-in");
}
}
}
}
// Log iteration started
self.diagnostics.log_orchestration(
self.state.iteration,
"loop",
crate::diagnostics::OrchestrationEvent::IterationStarted,
);
// Log hat selected
self.diagnostics.log_orchestration(
self.state.iteration,
"loop",
crate::diagnostics::OrchestrationEvent::HatSelected {
hat: hat_id.to_string(),
reason: "process_output".to_string(),
},
);
// Track failures
if success {
self.state.consecutive_failures = 0;
} else {
self.state.consecutive_failures += 1;
}
let _ = output;
// File-modification audit: detect when a hat with disallowed Edit/Write tools
// modified files. This is hard enforcement — emits a scope_violation event.
self.audit_file_modifications(hat_id);
// Events are ONLY read from the JSONL file written by `ralph emit`.
// This enforces tool use and prevents confabulation (agent claiming to emit without actually doing so).
// See process_events_from_jsonl() for event processing.
// Check termination conditions
self.check_termination()
}
/// Audits file modifications after a hat iteration.
///
/// If the hat has `Edit` or `Write` in its `disallowed_tools`, checks whether
/// files were modified (via `git diff --stat HEAD`). If so, emits a
/// `<hat_id>.scope_violation` event.
fn audit_file_modifications(&mut self, hat_id: &HatId) {
let config = match self.registry.get_config(hat_id) {
Some(c) => c,
None => return,
};
let has_write_restriction = config
.disallowed_tools
.iter()
.any(|t| t == "Edit" || t == "Write");
if !has_write_restriction {
return;
}
let workspace = &self.config.core.workspace_root;
let diff_output = std::process::Command::new("git")
.args(["diff", "--stat", "HEAD"])
.current_dir(workspace)
.output();
match diff_output {
Ok(output) if !output.stdout.is_empty() => {
let diff_stat = String::from_utf8_lossy(&output.stdout).trim().to_string();
warn!(
hat = %hat_id.as_str(),
diff = %diff_stat,
"Hat modified files despite tool restrictions (scope violation)"
);
let violation_topic = format!("{}.scope_violation", hat_id.as_str());
let violation = Event::new(
violation_topic.as_str(),
format!(
"Hat '{}' modified files with Edit/Write disallowed:\n{}",
hat_id.as_str(),
diff_stat
),
);
self.bus.publish(violation);
}
Err(e) => {
debug!(error = %e, "Could not run git diff for file-modification audit");
}
_ => {} // No modifications — all good
}
}
/// Extracts task identifier from build.blocked payload.
/// Uses first line of payload as task ID.
fn extract_task_id(payload: &str) -> String {
payload
.lines()
.next()
.unwrap_or("unknown")
.trim()
.to_string()
}
/// Adds cost to the cumulative total.
pub fn add_cost(&mut self, cost: f64) {
self.state.cumulative_cost += cost;
}
/// Verifies all tasks in scratchpad are complete or cancelled.
///
/// Returns:
/// - `Ok(true)` if all tasks are `[x]` or `[~]`, or if scratchpad is disabled
/// - `Ok(false)` if any tasks are `[ ]` (pending)
/// - `Err(...)` if scratchpad doesn't exist or can't be read
fn verify_scratchpad_complete(&self) -> Result<bool, std::io::Error> {
// Nothing to verify when scratchpad is disabled
if !self.ralph.active_scratchpad().enabled {
return Ok(true);
}
let scratchpad_path = self.scratchpad_path();
if !scratchpad_path.exists() {
return Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"Scratchpad does not exist",
));
}
let content = std::fs::read_to_string(scratchpad_path)?;
let has_pending = content
.lines()
.any(|line| line.trim_start().starts_with("- [ ]"));
Ok(!has_pending)
}
/// Reads the current loop ID from the marker file.
///
/// Returns `None` if no marker exists or is empty, which means
/// task queries should be unfiltered (backwards compatible).
fn current_loop_id(&self) -> Option<String> {
self.loop_context
.as_ref()
.and_then(|ctx| {
let marker_path = ctx.ralph_dir().join("current-loop-id");
std::fs::read_to_string(&marker_path).ok()
})
.map(|id| id.trim().to_string())
.filter(|id| !id.is_empty())
}
/// Filters a task list by loop ID. When `loop_id` is `None`, returns all tasks.
fn filter_tasks_by_loop<'a>(
tasks: Vec<&'a crate::task::Task>,
loop_id: Option<&str>,
) -> Vec<&'a crate::task::Task> {
match loop_id {
Some(id) => tasks
.into_iter()
.filter(|t| t.loop_id.as_deref() == Some(id))
.collect(),
None => tasks,
}
}
fn verify_tasks_complete(&self) -> Result<bool, std::io::Error> {
use crate::task_store::TaskStore;
let tasks_path = self.tasks_path();
// No tasks file = no pending tasks = complete
if !tasks_path.exists() {
return Ok(true);
}
let store = TaskStore::load(&tasks_path)?;
let current_loop_id = self.current_loop_id();
let open = Self::filter_tasks_by_loop(store.open(), current_loop_id.as_deref());
Ok(open.is_empty())
}
/// Builds a [`CheckinContext`] with current loop state for robot check-ins.
fn build_checkin_context(&self, hat_id: &HatId) -> CheckinContext {
let (open_tasks, closed_tasks) = self.count_tasks();
CheckinContext {
current_hat: Some(hat_id.as_str().to_string()),
open_tasks,
closed_tasks,
cumulative_cost: self.state.cumulative_cost,
}
}
/// Counts open and closed tasks from the task store.
///
/// Returns `(open_count, closed_count)`. "Open" means non-terminal tasks,
/// "closed" means tasks with `TaskStatus::Closed`.
fn count_tasks(&self) -> (usize, usize) {
use crate::task_store::TaskStore;
let tasks_path = self.tasks_path();
if !tasks_path.exists() {
return (0, 0);
}
match TaskStore::load(&tasks_path) {
Ok(store) => {
let current_loop_id = self.current_loop_id();
let all = Self::filter_tasks_by_loop(
store.all().iter().collect(),
current_loop_id.as_deref(),
);
let open = Self::filter_tasks_by_loop(store.open(), current_loop_id.as_deref());
let closed = all.len() - open.len();
(open.len(), closed)
}
Err(_) => (0, 0),
}
}
/// Returns a list of open task descriptions for logging purposes.
fn get_open_task_list(&self) -> Vec<String> {
use crate::task_store::TaskStore;
let tasks_path = self.tasks_path();
if let Ok(store) = TaskStore::load(&tasks_path) {
let current_loop_id = self.current_loop_id();
let open = Self::filter_tasks_by_loop(store.open(), current_loop_id.as_deref());
return open
.iter()
.map(|t| format!("{}: {}", t.id, t.title))
.collect();
}
vec![]
}
fn warn_on_mutation_evidence(&self, evidence: &crate::event_parser::BackpressureEvidence) {
let threshold = self.config.event_loop.mutation_score_warn_threshold;
match &evidence.mutants {
Some(mutants) => {
if let Some(reason) = Self::mutation_warning_reason(mutants, threshold) {
warn!(
reason = %reason,
mutants_status = ?mutants.status,
mutants_score = mutants.score_percent,
mutants_threshold = threshold,
"Mutation testing warning"
);
}
}
None => {
if let Some(threshold) = threshold {
warn!(
mutants_threshold = threshold,
"Mutation testing warning: missing mutation evidence in build.done payload"
);
}
}
}
}
fn mutation_warning_reason(
mutants: &MutationEvidence,
threshold: Option<f64>,
) -> Option<String> {
match mutants.status {
MutationStatus::Fail => Some("mutation testing failed".to_string()),
MutationStatus::Warn => Some(Self::format_mutation_message(
"mutation score below threshold",
mutants.score_percent,
)),
MutationStatus::Unknown => Some("mutation testing status unknown".to_string()),
MutationStatus::Pass => {
let threshold = threshold?;
match mutants.score_percent {
Some(score) if score < threshold => Some(format!(
"mutation score {:.2}% below threshold {:.2}%",
score, threshold
)),
Some(_) => None,
None => Some(format!(
"mutation score missing (threshold {:.2}%)",
threshold
)),
}
}
}
}
fn format_mutation_message(message: &str, score: Option<f64>) -> String {
match score {
Some(score) => format!("{message} ({score:.2}%)"),
None => message.to_string(),
}
}
fn parse_human_interact_context(payload: &str) -> Value {
let mut context = match serde_json::from_str::<Value>(payload) {
Ok(Value::Object(map)) => map,
Ok(value) => {
let mut map = Map::new();
map.insert("question".to_string(), value);
map
}
Err(_) => {
let mut map = Map::new();
map.insert("question".to_string(), Value::String(payload.to_string()));
map
}
};
if !context.contains_key("question") {
context.insert("question".to_string(), Value::String(payload.to_string()));
}
Value::Object(context)
}
fn is_restart_request_payload(payload: &str) -> bool {
let payload = payload.to_ascii_lowercase();
payload.contains("restart yourself") || payload.contains("restart ralph")
}
fn is_restart_request_event(event: &Event) -> bool {
matches!(event.topic.as_str(), "human.response" | "user.prompt")
&& Self::is_restart_request_payload(&event.payload)
}
fn mark_restart_requested(&self, source: &str) {
let restart_path =
std::path::Path::new(&self.config.core.workspace_root).join(".ralph/restart-requested");
if let Some(parent) = restart_path.parent()
&& let Err(err) = std::fs::create_dir_all(parent)
{
warn!(
error = %err,
path = %parent.display(),
"Failed to create restart-requested parent directory"
);
return;
}
if let Err(err) = std::fs::write(&restart_path, source) {
warn!(
error = %err,
path = %restart_path.display(),
"Failed to write restart-requested signal"
);
return;
}
info!(
source,
path = %restart_path.display(),
"Restart requested from human text"
);
}
/// Processes events from JSONL and routes orphaned events to Ralph.
///
/// Also handles backpressure for malformed JSONL lines by:
/// 1. Emitting `event.malformed` system events for each parse failure
/// 2. Tracking consecutive failures for termination check
/// 3. Resetting counter when valid events are parsed
///
/// Returns [`ProcessedEvents`] indicating whether events were found, whether
/// semantic `plan.*` topics were published, structured `human.interact`
/// context/outcome metadata, and whether any were orphans that Ralph should
/// handle.
pub fn process_events_from_jsonl(&mut self) -> std::io::Result<ProcessedEvents> {
let result = self.event_reader.read_new_events()?;
self.process_parse_result(result)
}
/// Inner event processing that operates on an already-parsed `ParseResult`.
///
/// This is the single source of truth for event validation, backpressure,
/// scope enforcement, and bus publishing. Both `process_events_from_jsonl`
/// and `process_events_from_jsonl_with_waves` delegate to this method.
fn process_parse_result(
&mut self,
result: crate::event_reader::ParseResult,
) -> std::io::Result<ProcessedEvents> {
// Handle malformed lines with backpressure
for malformed in &result.malformed {
let payload = format!(
"Line {}: {}\nContent: {}",
malformed.line_number, malformed.error, &malformed.content
);
let event = Event::new("event.malformed", &payload);
self.bus.publish(event);
self.state.consecutive_malformed_events += 1;
warn!(
line = malformed.line_number,
consecutive = self.state.consecutive_malformed_events,
"Malformed event line detected"
);
}
// Reset counter when valid events are parsed
if !result.events.is_empty() {
self.state.consecutive_malformed_events = 0;
}
if result.events.is_empty() && result.malformed.is_empty() {
return Ok(ProcessedEvents {
had_events: false,
had_plan_events: false,
human_interact_context: None,
has_orphans: false,
});
}
// --- Scope enforcement: filter events against active hat's publishes ---
// Only active when enforce_hat_scope is true in config (opt-in).
let events = if self.config.event_loop.enforce_hat_scope {
let active_hats = self.state.last_active_hat_ids.clone();
let (in_scope, out_of_scope): (Vec<_>, Vec<_>) =
result.events.into_iter().partition(|event| {
if active_hats.is_empty() {
return true; // Ralph coordinating — no scope restriction
}
active_hats
.iter()
.any(|hat_id| self.registry.can_publish(hat_id, event.topic.as_str()))
});
for event in &out_of_scope {
let violation_hat = active_hats.first().map(|h| h.as_str()).unwrap_or("unknown");
warn!(
active_hats = ?active_hats,
topic = %event.topic,
"Scope violation: active hat(s) cannot publish this topic — dropping event"
);
let violation_topic = format!("{}.scope_violation", violation_hat);
let violation_payload = format!(
"Attempted to publish '{}': {}",
event.topic,
event.payload.clone().unwrap_or_default()
);
let violation = Event::new(violation_topic, violation_payload);
self.bus.publish(violation);
}
in_scope
} else {
result.events
};
// --- End scope enforcement ---
let mut has_orphans = false;
// Validate and transform events (apply backpressure for build.done)
let mut validated_events = Vec::new();
let completion_topic = self.config.event_loop.completion_promise.as_str();
let cancellation_topic = self.config.event_loop.cancellation_promise.clone();
let total_events = events.len();
for (index, event) in events.into_iter().enumerate() {
let payload = event.payload.clone().unwrap_or_default();
// Detect loop.cancel — unconditional graceful termination
if !cancellation_topic.is_empty() && event.topic.as_str() == cancellation_topic {
info!(
payload = %payload,
"loop.cancel event detected — scheduling graceful termination"
);
self.state.cancellation_requested = true;
// Continue processing remaining events (they may contain cleanup info)
continue;
}
if event.topic == completion_topic {
if index + 1 == total_events {
self.state.completion_requested = true;
self.diagnostics.log_orchestration(
self.state.iteration,
"jsonl",
crate::diagnostics::OrchestrationEvent::EventPublished {
topic: event.topic.clone(),
},
);
info!(
topic = %event.topic,
"Completion event detected in JSONL"
);
} else {
warn!(
topic = %event.topic,
index = index,
total_events = total_events,
"Completion event ignored because it was not the last event"
);
}
continue;
}
if event.topic == "build.done" {
// Validate build.done events have backpressure evidence
if let Some(evidence) = EventParser::parse_backpressure_evidence(&payload) {
if evidence.all_passed() {
self.warn_on_mutation_evidence(&evidence);
validated_events.push(Event::new(event.topic.as_str(), &payload));
} else {
// Evidence present but checks failed - synthesize build.blocked
warn!(
tests = evidence.tests_passed,
lint = evidence.lint_passed,
typecheck = evidence.typecheck_passed,
audit = evidence.audit_passed,
coverage = evidence.coverage_passed,
complexity = evidence.complexity_score,
duplication = evidence.duplication_passed,
performance = evidence.performance_regression,
specs = evidence.specs_verified,
"build.done rejected: backpressure checks failed"
);
let complexity = evidence
.complexity_score
.map(|value| format!("{value:.2}"))
.unwrap_or_else(|| "missing".to_string());
let performance = match evidence.performance_regression {
Some(true) => "regression".to_string(),
Some(false) => "pass".to_string(),
None => "missing".to_string(),
};
let specs = match evidence.specs_verified {
Some(true) => "pass".to_string(),
Some(false) => "fail".to_string(),
None => "not reported".to_string(),
};
self.diagnostics.log_orchestration(
self.state.iteration,
"jsonl",
crate::diagnostics::OrchestrationEvent::BackpressureTriggered {
reason: format!(
"backpressure checks failed: tests={}, lint={}, typecheck={}, audit={}, coverage={}, complexity={}, duplication={}, performance={}, specs={}",
evidence.tests_passed,
evidence.lint_passed,
evidence.typecheck_passed,
evidence.audit_passed,
evidence.coverage_passed,
complexity,
evidence.duplication_passed,
performance,
specs
),
},
);
validated_events.push(Event::new(
"build.blocked",
"Backpressure checks failed. Fix tests/lint/typecheck/audit/coverage/complexity/duplication/specs before emitting build.done.",
));
}
} else {
// No evidence found - synthesize build.blocked
warn!("build.done rejected: missing backpressure evidence");
self.diagnostics.log_orchestration(
self.state.iteration,
"jsonl",
crate::diagnostics::OrchestrationEvent::BackpressureTriggered {
reason: "missing backpressure evidence".to_string(),
},
);
validated_events.push(Event::new(
"build.blocked",
"Missing backpressure evidence. Include 'tests: pass', 'lint: pass', 'typecheck: pass', 'audit: pass', 'coverage: pass', 'complexity: <score>', 'duplication: pass', 'performance: pass' (optional), 'specs: pass' (optional) in build.done payload.",
));
}
} else if event.topic == "review.done" && !event.is_wave_event() {
// Validate review.done events have verification evidence.
// Wave worker events skip this — wave reviews are read-only
// and don't run tests/builds.
if let Some(evidence) = EventParser::parse_review_evidence(&payload) {
if evidence.is_verified() {
validated_events.push(Event::new(event.topic.as_str(), &payload));
} else {
// Evidence present but checks failed - synthesize review.blocked
warn!(
tests = evidence.tests_passed,
build = evidence.build_passed,
"review.done rejected: verification checks failed"
);
self.diagnostics.log_orchestration(
self.state.iteration,
"jsonl",
crate::diagnostics::OrchestrationEvent::BackpressureTriggered {
reason: format!(
"review verification failed: tests={}, build={}",
evidence.tests_passed, evidence.build_passed
),
},
);
validated_events.push(Event::new(
"review.blocked",
"Review verification failed. Run tests and build before emitting review.done.",
));
}
} else {
// No evidence found - synthesize review.blocked
warn!("review.done rejected: missing verification evidence");
self.diagnostics.log_orchestration(
self.state.iteration,
"jsonl",
crate::diagnostics::OrchestrationEvent::BackpressureTriggered {
reason: "missing review verification evidence".to_string(),
},
);
validated_events.push(Event::new(
"review.blocked",
"Missing verification evidence. Include 'tests: pass' and 'build: pass' in review.done payload.",
));
}
} else if event.topic == "verify.passed" {
if let Some(report) = EventParser::parse_quality_report(&payload) {
if report.meets_thresholds() {
validated_events.push(Event::new(event.topic.as_str(), &payload));
} else {
let failed = report.failed_dimensions();
let reason = if failed.is_empty() {
"quality thresholds failed".to_string()
} else {
format!("quality thresholds failed: {}", failed.join(", "))
};
warn!(
failed_dimensions = ?failed,
"verify.passed rejected: quality thresholds failed"
);
self.diagnostics.log_orchestration(
self.state.iteration,
"jsonl",
crate::diagnostics::OrchestrationEvent::BackpressureTriggered {
reason,
},
);
validated_events.push(Event::new(
"verify.failed",
"Quality thresholds failed. Include quality.tests, quality.coverage, quality.lint, quality.audit, quality.mutation, quality.complexity with thresholds in verify.passed payload.",
));
}
} else {
// No quality report found - synthesize verify.failed
warn!("verify.passed rejected: missing quality report");
self.diagnostics.log_orchestration(
self.state.iteration,
"jsonl",
crate::diagnostics::OrchestrationEvent::BackpressureTriggered {
reason: "missing quality report".to_string(),
},
);
validated_events.push(Event::new(
"verify.failed",
"Missing quality report. Include quality.tests, quality.coverage, quality.lint, quality.audit, quality.mutation, quality.complexity in verify.passed payload.",
));
}
} else if event.topic == "verify.failed" {
if EventParser::parse_quality_report(&payload).is_none() {
warn!("verify.failed missing quality report");
}
validated_events.push(Event::new(event.topic.as_str(), &payload));
} else {
// Non-backpressure events pass through unchanged
validated_events.push(Event::new(event.topic.as_str(), &payload));
}
}
// Track build.blocked events for thrashing detection
let blocked_events: Vec<_> = validated_events
.iter()
.filter(|e| e.topic == "build.blocked".into())
.collect();
for blocked_event in &blocked_events {
let task_id = Self::extract_task_id(&blocked_event.payload);
let count = self
.state
.task_block_counts
.entry(task_id.clone())
.or_insert(0);
*count += 1;
debug!(
task_id = %task_id,
block_count = *count,
"Task blocked"
);
// After 3 blocks on same task, emit build.task.abandoned
if *count >= 3 && !self.state.abandoned_tasks.contains(&task_id) {
warn!(
task_id = %task_id,
"Task abandoned after 3 consecutive blocks"
);
self.state.abandoned_tasks.push(task_id.clone());
self.diagnostics.log_orchestration(
self.state.iteration,
"jsonl",
crate::diagnostics::OrchestrationEvent::TaskAbandoned {
reason: format!(
"3 consecutive build.blocked events for task '{}'",
task_id
),
},
);
let abandoned_event = Event::new(
"build.task.abandoned",
format!(
"Task '{}' abandoned after 3 consecutive build.blocked events",
task_id
),
);
self.bus.publish(abandoned_event);
}
}
// Track hat-level blocking for legacy thrashing detection
let has_blocked_event = !blocked_events.is_empty();
if has_blocked_event {
self.state.consecutive_blocked += 1;
} else {
self.state.consecutive_blocked = 0;
self.state.last_blocked_hat = None;
}
// Handle human.interact blocking behavior:
// When a human.interact event is detected and robot service is active,
// send the question and block until human.response or timeout.
let mut response_event = None;
let mut human_interact_context = None;
let ask_human_idx = validated_events
.iter()
.position(|e| e.topic == "human.interact".into());
if let Some(idx) = ask_human_idx {
let ask_event = &validated_events[idx];
let payload = ask_event.payload.clone();
let mut context = match Self::parse_human_interact_context(&payload) {
Value::Object(map) => map,
_ => Map::new(),
};
if let Some(ref robot_service) = self.robot_service {
info!(
payload = %payload,
"human.interact event detected — sending question via robot service"
);
// Send the question (includes retry with exponential backoff)
let send_ok = match robot_service.send_question(&payload) {
Ok(_message_id) => true,
Err(e) => {
warn!(
error = %e,
"Failed to send human.interact question after retries — treating as timeout"
);
// Log to diagnostics
self.diagnostics.log_error(
self.state.iteration,
"telegram",
crate::diagnostics::DiagnosticError::TelegramSendError {
operation: "send_question".to_string(),
error: e.to_string(),
retry_count: 3,
},
);
context.insert(
"outcome".to_string(),
Value::String("send_failure".to_string()),
);
context.insert("error".to_string(), Value::String(e.to_string()));
false
}
};
// Block: poll events file for human.response
// Per spec, even on send failure we treat as timeout (continue without blocking)
if send_ok {
// Read the active events path from the current-events marker,
// falling back to the default events.jsonl if not available.
let events_path = self
.loop_context
.as_ref()
.and_then(|ctx| {
std::fs::read_to_string(ctx.current_events_marker())
.ok()
.map(|s| ctx.workspace().join(s.trim()))
})
.or_else(|| {
std::fs::read_to_string(".ralph/current-events")
.ok()
.map(|s| PathBuf::from(s.trim()))
})
.unwrap_or_else(|| {
self.loop_context
.as_ref()
.map(|ctx| ctx.events_path())
.unwrap_or_else(|| PathBuf::from(".ralph/events.jsonl"))
});
match robot_service.wait_for_response(&events_path) {
Ok(Some(response)) => {
info!(
response = %response,
"Received human.response — continuing loop"
);
context.insert(
"outcome".to_string(),
Value::String("response".to_string()),
);
context.insert("response".to_string(), Value::String(response.clone()));
// Create a human.response event to inject into the bus
response_event = Some(Event::new("human.response", &response));
}
Ok(None) => {
warn!(
timeout_secs = robot_service.timeout_secs(),
"Human response timeout — injecting human.timeout event"
);
context.insert(
"outcome".to_string(),
Value::String("timeout".to_string()),
);
context.insert(
"timeout_seconds".to_string(),
Value::from(robot_service.timeout_secs()),
);
let timeout_event = Event::new(
"human.timeout",
format!(
"No response after {}s. Original question: {}",
robot_service.timeout_secs(),
payload
),
);
response_event = Some(timeout_event);
}
Err(e) => {
warn!(
error = %e,
"Error waiting for human response — injecting human.timeout event"
);
context.insert(
"outcome".to_string(),
Value::String("wait_error".to_string()),
);
context.insert("error".to_string(), Value::String(e.to_string()));
let timeout_event = Event::new(
"human.timeout",
format!(
"Error waiting for response: {}. Original question: {}",
e, payload
),
);
response_event = Some(timeout_event);
}
}
}
} else {
debug!(
"human.interact event detected but no robot service active — passing through"
);
context.insert(
"outcome".to_string(),
Value::String("no_robot_service".to_string()),
);
}
human_interact_context = Some(Value::Object(context));
}
let restart_requested = validated_events.iter().any(Self::is_restart_request_event)
|| response_event
.as_ref()
.is_some_and(Self::is_restart_request_event);
if restart_requested {
self.mark_restart_requested("human_text");
}
// Track whether any events will be published (before the loop consumes them).
let had_events = !validated_events.is_empty();
let had_plan_events = validated_events
.iter()
.any(|event| event.topic.as_str().starts_with("plan."));
// Publish validated events to the bus.
// Ralph is always registered with subscribe("*"), so every event has at least
// one subscriber. Events without a specific hat subscriber are "orphaned" —
// Ralph handles them as the universal fallback.
for event in validated_events {
// Record topic for event chain validation
self.state.record_event(&event);
self.diagnostics.log_orchestration(
self.state.iteration,
"jsonl",
crate::diagnostics::OrchestrationEvent::EventPublished {
topic: event.topic.to_string(),
},
);
if !self.registry.has_subscriber(event.topic.as_str()) {
has_orphans = true;
}
debug!(
topic = %event.topic,
"Publishing event from JSONL"
);
self.bus.publish(event);
}
// Publish human.response event if one was received during blocking
if let Some(response) = response_event {
self.state.record_event(&response);
info!(
topic = %response.topic,
"Publishing human.response event from robot service"
);
self.bus.publish(response);
}
Ok(ProcessedEvents {
had_events,
had_plan_events,
human_interact_context,
has_orphans,
})
}
/// Process events from JSONL, partitioning wave events from regular events.
///
/// Wave events (those with `wave_id` set and targeting a concurrent hat) are
/// extracted and returned separately. Regular events go through the full
/// backpressure pipeline via `process_parse_result`.
pub fn process_events_from_jsonl_with_waves(
&mut self,
) -> std::io::Result<ProcessedEventsWithWaves> {
let result = self.event_reader.read_new_events()?;
// Partition: wave dispatch events vs regular events.
// Only events that target a concurrent hat (concurrency > 1) are wave dispatches.
// Wave *results* (e.g. review.done) have wave_id set but should be treated as
// regular events so they reach the bus and trigger downstream hats (e.g. aggregator).
//
// Uses find_by_trigger + get_config — the same resolution path as
// detect_wave_events — to ensure partition and detection agree.
let (wave_events, regular_events): (Vec<_>, Vec<_>) =
result.events.into_iter().partition(|e| {
e.wave_id.is_some()
&& self
.registry
.find_by_trigger(e.topic.as_str())
.and_then(|hat_id| self.registry.get_config(hat_id))
.is_some_and(|hat_config| hat_config.concurrency > 1)
});
if !wave_events.is_empty() {
debug!(
wave_count = wave_events.len(),
regular_count = regular_events.len(),
"Partitioned wave events from regular events"
);
}
// Delegate regular events to the full pipeline (backpressure, scope
// enforcement, human.interact, plan detection, etc.)
let regular_result = crate::event_reader::ParseResult {
events: regular_events,
malformed: result.malformed,
};
let processed = self.process_parse_result(regular_result)?;
Ok(ProcessedEventsWithWaves {
processed,
wave_events,
})
}
/// Checks if output contains a completion event from Ralph.
///
/// Completion must be emitted as an `<event>` tag, not plain text.
pub fn check_ralph_completion(&self, output: &str) -> bool {
let events = EventParser::new().parse(output);
events
.iter()
.any(|event| event.topic.as_str() == self.config.event_loop.completion_promise)
}
/// Publishes the loop.terminate system event to observers.
///
/// Per spec: "Published by the orchestrator (not agents) when the loop exits."
/// This is an observer-only event—hats cannot trigger on it.
///
/// Returns the event for logging purposes.
pub fn publish_terminate_event(&mut self, reason: &TerminationReason) -> Event {
// Stop the robot service if it was running
self.stop_robot_service();
let elapsed = self.state.elapsed();
let duration_str = format_duration(elapsed);
let payload = format!(
"## Reason\n{}\n\n## Status\n{}\n\n## Summary\n- Iterations: {}\n- Duration: {}\n- Exit code: {}",
reason.as_str(),
termination_status_text(reason),
self.state.iteration,
duration_str,
reason.exit_code()
);
let event = Event::new("loop.terminate", &payload);
// Publish to bus for observers (but no hat can trigger on this)
self.bus.publish(event.clone());
info!(
reason = %reason.as_str(),
iterations = self.state.iteration,
duration = %duration_str,
"Wrapping up: {}. {} iterations in {}.",
reason.as_str(),
self.state.iteration,
duration_str
);
event
}
/// Returns the robot service's shutdown flag, if active.
///
/// Signal handlers can set this flag to interrupt `wait_for_response()`
/// without waiting for the full timeout.
pub fn robot_shutdown_flag(&self) -> Option<Arc<AtomicBool>> {
self.robot_service.as_ref().map(|s| s.shutdown_flag())
}
/// Stops the robot service if it's running.
///
/// Called during loop termination to cleanly shut down the communication backend.
fn stop_robot_service(&mut self) {
if let Some(service) = self.robot_service.take() {
service.stop();
}
}
// -------------------------------------------------------------------------
// Human-in-the-loop planning support
// -------------------------------------------------------------------------
/// Check if any event is a `user.prompt` event.
///
/// Returns the first user prompt event found, or None.
pub fn check_for_user_prompt(&self, events: &[Event]) -> Option<UserPrompt> {
events
.iter()
.find(|e| e.topic.as_str() == "user.prompt")
.map(|e| UserPrompt {
id: Self::extract_prompt_id(&e.payload),
text: e.payload.clone(),
})
}
/// Extract a prompt ID from the event payload.
///
/// Supports both XML attribute format: `<event topic="user.prompt" id="q1">...</event>`
/// and JSON format in payload.
fn extract_prompt_id(payload: &str) -> String {
// Try to extract id attribute from XML-like format first
if let Some(start) = payload.find("id=\"")
&& let Some(end) = payload[start + 4..].find('"')
{
return payload[start + 4..start + 4 + end].to_string();
}
// Fallback: generate a simple ID based on timestamp
format!("q{}", Self::generate_prompt_id())
}
/// Generate a simple unique ID for prompts.
/// Uses timestamp-based generation since uuid crate isn't available.
fn generate_prompt_id() -> String {
use std::time::{SystemTime, UNIX_EPOCH};
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos();
format!("{:x}", nanos % 0xFFFF_FFFF)
}
}
/// A user prompt that requires human input.
///
/// Created when the agent emits a `user.prompt` event during planning.
#[derive(Debug, Clone)]
pub struct UserPrompt {
/// Unique identifier for this prompt (e.g., "q1", "q2")
pub id: String,
/// The prompt/question text
pub text: String,
}
/// Formats a duration as human-readable string.
fn format_duration(d: Duration) -> String {
let total_secs = d.as_secs();
let hours = total_secs / 3600;
let minutes = (total_secs % 3600) / 60;
let seconds = total_secs % 60;
if hours > 0 {
format!("{}h {}m {}s", hours, minutes, seconds)
} else if minutes > 0 {
format!("{}m {}s", minutes, seconds)
} else {
format!("{}s", seconds)
}
}
/// Returns a human-readable status based on termination reason.
fn termination_status_text(reason: &TerminationReason) -> &'static str {
match reason {
TerminationReason::CompletionPromise => "All tasks completed successfully.",
TerminationReason::MaxIterations => "Stopped at iteration limit.",
TerminationReason::MaxRuntime => "Stopped at runtime limit.",
TerminationReason::MaxCost => "Stopped at cost limit.",
TerminationReason::ConsecutiveFailures => "Too many consecutive failures.",
TerminationReason::LoopThrashing => {
"Loop thrashing detected - same hat repeatedly blocked."
}
TerminationReason::LoopStale => {
"Stale loop detected - same topic emitted 3+ times consecutively."
}
TerminationReason::ValidationFailure => "Too many consecutive malformed JSONL events.",
TerminationReason::Stopped => "Manually stopped.",
TerminationReason::Interrupted => "Interrupted by signal.",
TerminationReason::RestartRequested => "Restarting by human request.",
TerminationReason::WorkspaceGone => "Workspace directory removed externally.",
TerminationReason::Cancelled => "Cancelled gracefully (human rejection or timeout).",
}
}