rsclaw-runtime 2026.6.26

rsclaw composition root: AppState/RPC handlers (a2a, cmd, cron, gateway, hooks, server, ws) + process entry point
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
//! Gateway startup orchestration.
//!
//! Wires together: config, store, providers, agent runtimes, channels,
//! cron scheduler, and HTTP server into a running gateway.

use std::{collections::HashMap, net::SocketAddr, sync::Arc, time::Duration};

use anyhow::{Context, Result};
use tokio::sync::{broadcast, mpsc};
use tracing::{debug, error, info, warn};

use super::channels::{start_channels, start_custom_channels};
use rsclaw_provider::build::build_providers;
use crate::{
    MemoryTier,
    cron::CronRunner,
    gateway::{
        LiveConfig,
        hot_reload::{ConfigChange, FileWatcher},
    },
    server::{AppState, serve},
};
use rsclaw_agent::{
        AgentMessage, AgentRegistry, AgentReply, AgentRuntime, AgentSpawner, MemoryStore,
        PendingAnalysis,
    };
use rsclaw_channel::OutboundMessage;
use rsclaw_config::{self as config, runtime::RuntimeConfig, schema::BindMode};
use rsclaw_plugin::{MemoryStoreSlot, PluginRegistry, load_all_plugins};
use rsclaw_provider::registry::ProviderRegistry;
use rsclaw_skill::{SkillRegistry, load_skills};
use rsclaw_store::Store;

// ---------------------------------------------------------------------------
// Sync-only channel allow-list
// ---------------------------------------------------------------------------

/// Channels whose responses arrive back at the caller through a non-
/// notification transport — currently the HTTP `/api/v1/message`
/// endpoint, which uses a `oneshot::reply_tx` on the `AgentMessage`
/// plus an optional SSE byte stream.
///
/// The agent runtime still fans the assistant reply out onto the
/// notification bus for observers (UI, telemetry), so this list
/// suppresses the otherwise-misleading `WARN: no channel sender
/// registered` log for those known-by-design absences. Future
/// sync-only entry points (stdio MCP, in-process embeds, etc.)
/// should be added here.
const SYNC_ONLY_CHANNELS: &[&str] = &["api"];

fn is_sync_only_channel(name: &str) -> bool {
    SYNC_ONLY_CHANNELS.contains(&name)
}

// ---------------------------------------------------------------------------
// Gateway entry point
// ---------------------------------------------------------------------------

/// Start the full gateway. Blocks until shutdown (Ctrl-C).
/// Prepend common tool directories to the process `PATH`, in priority order,
/// so subprocesses (cap-rs coding agents, shell exec, MCP servers) can find
/// user-installed binaries even when the gateway was launched from a desktop
/// app / launchd (which inherit a stripped PATH without the shell profile).
///
/// Priority (earlier wins): system package dirs first, then user-local dirs,
/// then per-tool dirs, finally the inherited PATH. Only existing dirs and
/// not-already-present entries are added, so this is idempotent and never
/// shadows a dir the user already ordered ahead.
fn enrich_process_path() {
    let sep = if cfg!(windows) { ';' } else { ':' };
    let current = std::env::var("PATH").unwrap_or_default();
    let existing: std::collections::HashSet<&str> = current.split(sep).collect();

    let mut prefix: Vec<String> = Vec::new();
    let mut push_if = |p: String| {
        if std::path::Path::new(&p).is_dir() && !existing.contains(p.as_str()) {
            prefix.push(p);
        }
    };

    // HIGHEST priority: rsclaw-managed tools under {base_dir}/tools, winning
    // over any system-wide install; respects --profile / --base-dir.
    let tools = rsclaw_config::loader::base_dir().join("tools");
    #[cfg(unix)]
    {
        // One clean shim dir of symlinks (sync_tool_shims) to each
        // install_tool-managed binary under its canonical command name.
        push_if(tools.join("bin").to_string_lossy().to_string());
    }
    #[cfg(windows)]
    {
        // No shim dir on Windows (symlinks need privilege; an .exe must stay
        // beside its sibling DLLs). Add each tool's own dir + bin/ +
        // node_modules/.bin so `<tool>.exe`/`.cmd` resolves by name in place.
        if let Ok(entries) = std::fs::read_dir(&tools) {
            for entry in entries.flatten() {
                let p = entry.path();
                if p.is_dir() {
                    push_if(p.join("node_modules").join(".bin").to_string_lossy().to_string());
                    push_if(p.join("bin").to_string_lossy().to_string());
                    push_if(p.to_string_lossy().to_string());
                }
            }
        }
        // claude-code's Windows binary lives deep under the npm package tree:
        // tools/claude-code/node_modules/@anthropic-ai/claude-code/bin/claude.exe
        push_if(
            tools
                .join("claude-code")
                .join("node_modules")
                .join("@anthropic-ai")
                .join("claude-code")
                .join("bin")
                .to_string_lossy()
                .to_string(),
        );
    }

    // System / package-manager dirs next.
    #[cfg(target_os = "macos")]
    {
        for p in ["/usr/local/bin", "/opt/homebrew/bin", "/opt/homebrew/sbin"] {
            push_if(p.to_owned());
        }
    }
    #[cfg(target_os = "linux")]
    {
        for p in ["/usr/local/bin"] {
            push_if(p.to_owned());
        }
    }
    // User-local dirs next.
    if let Some(home) = dirs_next::home_dir() {
        for rel in [
            ".local/bin",
            ".cargo/bin",
            "bin",
            "go/bin",
            ".bun/bin",
            ".opencode/bin",
        ] {
            push_if(home.join(rel).to_string_lossy().to_string());
        }
    }

    if prefix.is_empty() {
        return;
    }
    // Append the inherited PATH entry-by-entry, then dedup the whole list
    // preserving first occurrence (which preserves priority order). The
    // inherited PATH can already contain duplicates — e.g. a launcher that
    // double-adds a plugin bin dir, as Claude Code does with
    // rust-analyzer-lsp — and pushing `current` verbatim would propagate
    // those dupes into every subprocess's PATH.
    for entry in current.split(sep) {
        if !entry.is_empty() {
            prefix.push(entry.to_string());
        }
    }
    let mut seen = std::collections::HashSet::new();
    prefix.retain(|p| seen.insert(p.clone()));
    let joined = prefix.join(&sep.to_string());
    // SAFETY: called once at startup before any worker thread spawns a child.
    unsafe {
        std::env::set_var("PATH", &joined);
    }
    tracing::info!(path = %joined, "enriched process PATH for subprocess tool resolution");
}

pub async fn start_gateway(config: Arc<RuntimeConfig>, tier: MemoryTier) -> Result<()> {
    // 0. (Restart safety net.) When this process was spawned as a
    //    replacement during a `gateway restart`, the parent sets
    //    `RSCLAW_PARENT_PID=<self>` on the child's env before spawn.
    //    On macOS the native-respawn path uses `execv` so the parent's
    //    fds (including redb's exclusive file lock) close atomically
    //    when the new image takes over — no race. But on platforms
    //    where exec isn't available, OR when the user starts a fresh
    //    `rsclaw gateway start` while a previous gateway is still
    //    holding the lock during graceful drain, opening redb here
    //    would fail with "Database locked by another gateway
    //    instance". Wait up to 5s for the parent to actually exit
    //    before letting MemoryStore / KB / a2a tasks open their redbs.
    wait_for_parent_release();
    // 0. Refresh the tools/bin shim dir, then enrich the process PATH before
    //    anything spawns a subprocess. Desktop/launchd-started gateways inherit
    //    a stripped PATH; the shim dir + PATH prepend let cap-rs coding-agent
    //    drivers and shell tools resolve every binary by bare name from one
    //    place. Sync first so the shims exist before PATH points at them.
    crate::cmd::tools::sync_tool_shims();
    enrich_process_path();

    // 0. Apply global proxy env vars before any HTTP clients are created.
    rsclaw_config::apply_proxy_env(&config);

    // 0a. Initialize the self-evolution config singleton from
    //     `[ext.evolution]` (or built-in defaults if absent). Read by memory
    //     tier transition, crystallizer, and meditation phases.
    rsclaw_agent::evolution::init_evolution_config(
        rsclaw_agent::evolution::EvolutionConfig::from_raw(config.ext.evolution.as_ref()),
    );

    // 0b. Propagate skill-registry credentials from rsclaw.json5 into
    //     process env. Spawned skill subprocesses (python CLIs etc.)
    //     inherit env, so this is the bridge that lets users keep keys
    //     in the config file instead of shell rc / launchctl. Done here,
    //     pre-runtime, while the process is still single-threaded.
    propagate_skill_registry_env(&config);

    // 0c. Propagate the root-level `env: { … }` map into process env.
    //     Same purpose as 0b but generic: lets users define arbitrary
    //     KV pairs (e.g. ASTOCK, LOG_DIR) used by /watch slash command
    //     and gateway-spawned subprocesses. Shell-provided env wins so
    //     `ASTOCK=... cargo run -- gateway restart` still overrides.
    propagate_user_env(&config);

    // 1. Resolve data directory — respects RSCLAW_BASE_DIR for --dev/--profile.
    let base_dir = rsclaw_config::loader::base_dir();
    let data_dir = base_dir.join("var/data");
    std::fs::create_dir_all(&data_dir).context("create data dir")?;

    // 1b. Seed tool prompts if not present.
    {
        let lang = config
            .raw
            .gateway
            .as_ref()
            .and_then(|g| g.language.as_deref());
        if let Err(e) = rsclaw_agent::bootstrap::seed_tools(&base_dir, lang) {
            warn!("failed to seed tool prompts: {e:#}");
        }
    }

    // 2. Open store. If the database is locked by another instance, exit cleanly so
    //    systemd won't keep restarting.
    let store = match Store::open(&data_dir, tier) {
        Ok(s) => Arc::new(s),
        Err(e) => {
            let msg = format!("{e:#}");
            if msg.contains("already open") || msg.contains("Cannot acquire lock") {
                eprintln!("  [!] Database locked by another gateway instance. Exiting cleanly.");
                std::process::exit(0);
            }
            return Err(e).context("open store");
        }
    };
    info!("store opened at {}", data_dir.display());

    // 2b. Bind cron storage to redb. From this point on
    // `crate::cron::load_cron_jobs` / `save_cron_jobs` and the
    // CronRunner's `save_store` operate on redb instead of cron.json5.
    // The file is still maintained as a best-effort export for
    // `cat` / `git diff`.
    crate::cron::init_cron_store(Arc::clone(&store.db));

    // 3. Build provider registry.
    let providers = Arc::new(build_providers(&config));
    info!("{} provider(s) registered", providers.names().len());

    // 4. Load skills.
    let global_skills = base_dir.join("skills");
    let skills = Arc::new(
        load_skills(&global_skills, None, config.ext.skills.as_ref()).unwrap_or_else(|e| {
            warn!("failed to load skills: {e:#}");
            SkillRegistry::new()
        }),
    );
    info!("{} skill(s) loaded", skills.len());

    // Auto-install allowlist (security gate for the skill_install tool): load
    // the local cache now so the gate has data immediately, then refresh from
    // the hub in the background. Fail-closed — no cache + failed fetch = empty.
    let (al_s, al_p) = rsclaw_skill::allowlist::load_cached();
    info!(
        skills = al_s,
        plugins = al_p,
        "allowlist: loaded from cache"
    );
    tokio::spawn(async {
        if let Err(e) = rsclaw_skill::allowlist::refresh().await {
            warn!("allowlist refresh failed (keeping cache; fail-closed): {e:#}");
        }
    });
    // First-startup-only fetch of the tools manifest (fail-open to the
    // compiled-in baseline). NOT an auto-update poll — later startups skip.
    tokio::spawn(async {
        crate::cmd::tools::fetch_manifest_if_missing().await;
    });

    // 5. Build agent registry with live receivers.
    let (registry, receivers) =
        AgentRegistry::from_config_with_receivers(&config, Arc::clone(&providers));
    let registry = Arc::new(registry);
    info!("{} agent(s) registered", registry.len());

    // Create notification broadcast channel early so background model downloads
    // can also send notifications to users via channels.
    let (notification_tx, notification_rx) =
        broadcast::channel::<rsclaw_channel::OutboundMessage>(64);

    // Restart-required event channel + latch. Published into by the file
    // watcher bridge and the BGE auto-downloader; subscribed to by WS dispatch
    // so UI clients see banners. Allocated early so the BGE downloader (next
    // step) and the file-watcher bridge (later) can both publish.
    let (restart_request_tx, _restart_request_rx) =
        tokio::sync::broadcast::channel::<rsclaw_events::RestartRequest>(16);
    let pending_restart: Arc<std::sync::RwLock<Option<rsclaw_events::RestartRequest>>> =
        Arc::new(std::sync::RwLock::new(None));

    // Graceful-shutdown coordinator — wired to task queue worker, axum graceful
    // shutdown, and the /api/v1/restart drain handler. Created here (before the
    // BGE block) so `publish_restart` can stamp the live inflight count on
    // every event, including the BGE auto-download notifications.
    let shutdown = crate::gateway::ShutdownCoordinator::new();

    // 6. Resolve and validate the BGE embedding model BEFORE opening the
    // memory store. Production must run with semantic search; failures here
    // abort startup so users notice immediately rather than silently
    // degrading to keyword-only retrieval.
    //
    // Priority: bge-base-zh > bge-small-zh > bge-small-en. If none of these
    // dirs already contains a usable model, sync-download bge-small-zh.
    let search_cfg = config.raw.memory_search.as_ref();
    let model_dir = {
        let base_zh = base_dir.join("models/bge-base-zh");
        let zh = base_dir.join("models/bge-small-zh");
        let en = base_dir.join("models/bge-small-en");
        if base_zh.join("model.safetensors").exists() {
            base_zh
        } else if zh.join("model.safetensors").exists() {
            zh
        } else if en.join("model.safetensors").exists() {
            en
        } else {
            zh // default download target
        }
    };
    ensure_bge_model_present(&model_dir, search_cfg).await?;

    // `memory` is an `Option<Arc<…>>`: clone it freely at every sink below
    // (agent tasks, heartbeat, AppState ~795) — clones are cheap refcount bumps
    // and order-independent. Never move it; the final owner is `AppState.memory`.
    let memory = match MemoryStore::open(&data_dir, Some(&model_dir), tier, search_cfg).await {
        Ok(mut m) => {
            // Wire the shared BM25 index so every extractor / WS / CLI write
            // dual-writes into tantivy and `search_hybrid` can fuse keyword
            // hits with vector hits. Then reindex existing docs so memories
            // written before this code shipped are also keyword-searchable.
            m.set_search_index(Arc::clone(&store.search));
            match m.reindex_bm25() {
                Ok(n) if n > 0 => info!(indexed = n, "memory: BM25 backfill complete"),
                Ok(_) => {}
                Err(e) => warn!("memory: BM25 reindex failed (vector still works): {e:#}"),
            }
            info!("memory store opened");
            let arc = Arc::new(tokio::sync::Mutex::new(m));
            rsclaw_agent::memory::set_global_store(Arc::clone(&arc));
            Some(arc)
        }
        Err(e) => {
            // Memory store opening should not fail once the model is
            // validated by ensure_bge_model_present — propagate so startup
            // surfaces the underlying issue (disk full, redb corruption…).
            return Err(anyhow::anyhow!("failed to open memory store: {e:#}"));
        }
    };

    // Embedder upgrade detection: if the active model produces a different
    // vector dimension than what's stored in redb (e.g. user dropped a
    // bge-base-zh dir and restarted), kick off a background re-embed via the
    // two-index hot-swap API. The gateway keeps serving — search returns
    // empty for the migrating docs until the swap commits, then catches up.
    if let Some(mem_arc) = memory.as_ref() {
        let pending = {
            let mem = mem_arc.lock().await;
            mem.pending_migration_count()
        };
        if pending > 0 {
            info!(
                pending,
                "embedder dimension changed since last run; spawning background re-embed"
            );
            let bg_mem = Arc::clone(mem_arc);
            tokio::spawn(async move {
                if let Err(e) = run_embedder_reembed(&bg_mem).await {
                    warn!(
                        "background re-embed failed ({e:#}); search will be partial until next restart"
                    );
                }
            });
        }
    }

    // 7. Load all plugins (JS + WASM) and register built-in memory slot.
    let plugins_dir = base_dir.join("plugins");
    let wasm_browser: Arc<tokio::sync::Mutex<Option<rsclaw_browser::BrowserSession>>> =
        Arc::new(tokio::sync::Mutex::new(None));
    // Resolve default vision model for host-vlm interface.
    let vision_model = config
        .raw
        .agents
        .as_ref()
        .and_then(|a| a.defaults.as_ref())
        .and_then(|d| d.model.as_ref())
        .and_then(|m| m.vision_head().map(String::from));

    let mut plugin_registry = load_all_plugins(
        &plugins_dir,
        config.ext.plugins.as_ref(),
        Arc::clone(&wasm_browser),
        Some(notification_tx.clone()),
        Some(Arc::clone(&providers)),
        vision_model,
    )
    .await
    .unwrap_or_else(|e| {
        warn!("plugin load error: {e:#}");
        PluginRegistry::new()
    });
    if let Some(ref mem_arc) = memory
        && !plugin_registry.slots.has_memory()
    {
        let slot = MemoryStoreSlot::new(Arc::clone(mem_arc));
        let _ = plugin_registry.slots.set_memory(Arc::new(slot), "built-in");
    }
    info!(
        "{} plugin(s) loaded (js={}, wasm={}), memory slot: {}",
        plugin_registry.len(),
        plugin_registry.js_count(),
        plugin_registry.wasm_count(),
        plugin_registry.slots.has_memory()
    );

    let wasm_plugins = Arc::new(plugin_registry.take_wasm_plugins());
    let plugins = Arc::new(plugin_registry);
    for handle in registry.all() {
        handle.set_wasm_plugins(Arc::clone(&wasm_plugins));
        handle.set_notification_tx(notification_tx.clone());
    }

    // Create the SSE broadcast channel once so agents and the HTTP server
    // share the same sender.
    let (event_tx, _) = broadcast::channel::<rsclaw_events::AgentEvent>(1024);

    // Construct the cap manager once — shared by every AgentRuntime so
    // `tool_cap` works in production (not just unit tests).
    let cap_manager = std::sync::Arc::new(rsclaw_cap::CapAgentManager::new(event_tx.clone()));

    // Interactive cap session manager (Phase 1 of cap multi-instance
    // sessions). Shared the same way as `cap_manager` so every
    // AgentRuntime can offer the `cap_live` tool to its LLM.
    //
    // Wire the outbound notification channel BEFORE wrapping in Arc so
    // preparse-side `/cap` follow-up tasks (resume-id hint after Ready
    // fires) can push to IM without per-channel plumbing.
    let cap_live_manager = {
        let mut m = rsclaw_cap::CapLiveManager::new(event_tx.clone());
        m.set_notification_tx(notification_tx.clone());
        std::sync::Arc::new(m)
    };
    // Publish a process-wide handle so preparse (`/cap`, `/cap-exit`)
    // can register sticky IM bindings without per-channel plumbing.
    // Set BEFORE any channel inbound task is spawned; runtime code
    // accesses the manager via `self.cap_live_manager` directly.
    rsclaw_cap::set_global_cap_live(std::sync::Arc::clone(&cap_live_manager));

    // Build LiveConfig BEFORE the spawner: hot-reloadable per-domain locks
    // that AgentRuntime reads for live-mutable fields (temperature, etc.).
    let live = Arc::new(LiveConfig::new((*config).clone()));

    // Shared per-model health table — one Arc cloned into AppState (for
    // the /api/v1/models/health endpoint) AND into every FailoverManager
    // via the spawner, so writes from the runtime are visible to the
    // HTTP layer in real time.
    let model_health = rsclaw_provider::health::ProviderHealthRegistry::new();

    // Create AgentSpawner — enables agent-to-agent dynamic spawning.
    let spawner = AgentSpawner::new_arc(
        Arc::clone(&registry),
        Arc::clone(&config),
        Arc::clone(&live),
        Arc::clone(&providers),
        Arc::clone(&skills),
        Arc::clone(&store),
        memory.clone(),
        event_tx.clone(),
        Some(Arc::clone(&plugins)),
        model_health.clone(),
        Some(Arc::clone(&cap_manager)),
        Some(Arc::clone(&cap_live_manager)),
    );

    // Spawn MCP servers and discover tools (before agent tasks so tools are
    // available).
    let mcp_registry = Arc::new(rsclaw_mcp::McpRegistry::new());
    spawn_mcp_servers(&config, Arc::clone(&mcp_registry)).await;

    // Clone memory before passing to agent tasks so heartbeat can also use it.
    let heartbeat_memory = memory.clone();

    // Shared computer_use permission store + broadcast channel.
    // `bypass_all` is read from `tools.computerUse.bypassAll` (default
    // false). The settings UI exposes a runtime toggle that mutates the
    // store's AtomicBool without touching the config file.
    let bypass_all_default = config
        .raw
        .tools
        .as_ref()
        .and_then(|t| t.computer_use.as_ref())
        .and_then(|cu| cu.bypass_all)
        .unwrap_or(false);
    let computer_permission = Arc::new(rsclaw_computer::permission::RedbPermissionStore::new(
        Arc::clone(&store.db),
        bypass_all_default,
    ));
    let (computer_permission_tx, _) =
        broadcast::channel::<rsclaw_computer::permission::PermissionRequest>(64);
    // Status events are higher-frequency than permission requests (one
    // per VLM step), so give a larger buffer. Subscribers that lag
    // simply drop events — status is best-effort UX, not load-bearing.
    let (computer_status_tx, _) =
        broadcast::channel::<rsclaw_computer::status::ComputerUseStatus>(256);
    let computer_runs: Arc<
        tokio::sync::RwLock<std::collections::HashMap<String, Arc<std::sync::atomic::AtomicBool>>>,
    > = Arc::new(tokio::sync::RwLock::new(std::collections::HashMap::new()));

    spawn_agent_tasks(
        receivers,
        Arc::clone(&registry),
        Arc::clone(&config),
        Arc::clone(&live),
        Arc::clone(&store),
        Arc::clone(&skills),
        Arc::clone(&providers),
        memory.clone(),
        event_tx.clone(),
        Some(Arc::clone(&spawner)),
        Some(Arc::clone(&plugins)),
        Some(Arc::clone(&mcp_registry)),
        Some(notification_tx.clone()),
        Arc::clone(&wasm_plugins),
        Arc::clone(&computer_permission),
        computer_permission_tx.clone(),
        computer_status_tx.clone(),
        Arc::clone(&computer_runs),
        model_health.clone(),
        Arc::clone(&cap_manager),
        Arc::clone(&cap_live_manager),
    );

    // Set i18n default language from gateway config.
    let lang = config
        .raw
        .gateway
        .as_ref()
        .and_then(|g| g.language.as_deref());
    info!(lang = ?lang, "i18n: gateway language config");
    if let Some(lang) = lang {
        rsclaw_i18n::set_default_lang(lang);
        info!(
            resolved = rsclaw_i18n::default_lang(),
            "i18n: default language set"
        );
    }

    // 8. Build channel manager and start channels.
    let mut channel_manager = rsclaw_channel::ChannelManager::new(tier);
    let feishu_slot: Arc<tokio::sync::OnceCell<Arc<rsclaw_channel::feishu::FeishuChannel>>> =
        Arc::new(tokio::sync::OnceCell::new());
    let wecom_slot: Arc<tokio::sync::OnceCell<Arc<rsclaw_channel::wecom::WeComChannel>>> =
        Arc::new(tokio::sync::OnceCell::new());
    let whatsapp_slot: Arc<tokio::sync::OnceCell<Arc<rsclaw_channel::whatsapp::WhatsAppChannel>>> =
        Arc::new(tokio::sync::OnceCell::new());
    let line_slot: Arc<tokio::sync::OnceCell<Arc<rsclaw_channel::line::LineChannel>>> =
        Arc::new(tokio::sync::OnceCell::new());
    let zalo_slot: Arc<tokio::sync::OnceCell<Arc<rsclaw_channel::zalo::ZaloChannel>>> =
        Arc::new(tokio::sync::OnceCell::new());
    let dm_enforcers: Arc<
        std::sync::RwLock<std::collections::HashMap<String, Arc<rsclaw_channel::DmPolicyEnforcer>>>,
    > = Arc::new(std::sync::RwLock::new(std::collections::HashMap::new()));

    // Channel sender registry for notification routing.
    let channel_senders: Arc<
        std::sync::RwLock<std::collections::HashMap<String, mpsc::Sender<OutboundMessage>>>,
    > = Arc::new(std::sync::RwLock::new(std::collections::HashMap::new()));
    // Make the senders reachable from inside TaskQueueManager::submit so the
    // user-facing "task received" ack can fire without threading the map
    // through every submit call site.
    super::task_queue::install_channel_senders(Arc::clone(&channel_senders));

    // Create task queue manager before channels so channels can submit to it.
    let task_queue_mgr = Arc::new(super::task_queue::TaskQueueManager::new(Arc::clone(
        &store.db,
    )));
    // Publish a global handle so the agent's `task` function-call tool can
    // submit follow-up tasks without threading the manager through every
    // tool-dispatch surface.
    super::task_queue::install_task_queue(Arc::clone(&task_queue_mgr));
    // crate-split P3: inject the task-queue host so rsclaw-agent's `task` tool
    // can enqueue without depending on the gateway crate.
    rsclaw_types::set_task_queue_host(Arc::new(super::task_queue::GatewayTaskQueueHost));
    rsclaw_plugin::set_plugin_background_host(Arc::new(
        super::task_queue::GatewayPluginBackgroundHost,
    ));

    start_channels(
        &config,
        Arc::clone(&registry),
        &mut channel_manager,
        Arc::clone(&feishu_slot),
        Arc::clone(&wecom_slot),
        Arc::clone(&whatsapp_slot),
        Arc::clone(&line_slot),
        Arc::clone(&zalo_slot),
        Arc::clone(&dm_enforcers),
        Arc::clone(&store.db),
        Arc::clone(&channel_senders),
        Arc::clone(&task_queue_mgr),
        shutdown.clone(),
    );

    // Spawn task queue worker — processes queued tasks in priority order.
    {
        let worker = Arc::new(super::task_queue::TaskQueueWorker::new(
            Arc::clone(&task_queue_mgr),
            Arc::clone(&registry),
            Arc::clone(&channel_senders),
            shutdown.clone(),
            (*config).clone(),
        ));
        tokio::spawn(async move { worker.run().await });
        info!("task queue worker started");
    }

    // Spawn external-jobs worker — drives long-running provider tasks
    // (video / image generation) to completion across gateway restarts.
    {
        let worker = Arc::new(super::external_jobs_worker::ExternalJobsWorker::new(
            Arc::clone(&store.db),
            notification_tx.clone(),
            shutdown.clone(),
            Arc::clone(&config),
        ));
        tokio::spawn(async move { worker.run().await });
        info!("external jobs worker started");
    }

    // Spawn notification router task — routes OutboundMessages from ACP tools
    // (OpenCode, ClaudeCode) to the correct channel based on msg.channel.
    {
        let senders = Arc::clone(&channel_senders);
        let mut rx = notification_rx;
        tokio::spawn(async move {
            info!("notification router started");
            while let Ok(msg) = rx.recv().await {
                if let Some(ref ch_name) = msg.channel {
                    // Try per-account sender first (wechat/acct → wechat),
                    // same pattern as task_queue::channel_tx_for.
                    let tx = {
                        let senders_guard =
                            senders.read().expect("channel_senders RwLock poisoned");
                        msg.account
                            .as_ref()
                            .filter(|a| !a.is_empty())
                            .and_then(|acct| {
                                let key = format!("{ch_name}/{acct}");
                                senders_guard.get(&key).cloned()
                            })
                            .or_else(|| senders_guard.get(ch_name).cloned())
                    };
                    if let Some(tx) = tx {
                        info!(channel = %ch_name, target_id = %msg.target_id, "routing notification");
                        if let Err(e) = tx.send(msg.clone()).await {
                            tracing::warn!(error = %e, "notification send failed");
                        }
                    } else if is_sync_only_channel(ch_name) {
                        // sync-only channels (HTTP /api/v1/message, future stdio
                        // MCP, etc.) carry their reply back through their own
                        // transport — the oneshot reply_tx on the AgentMessage,
                        // an SSE byte stream, etc. The notification fan-out
                        // here is a fire-and-forget event-bus emit for
                        // observers (UI, telemetry) and the absence of a
                        // registered sender is by design, not a misconfig.
                        // Surface at debug so operators can still see the
                        // dispatch path without flooding logs on every API
                        // call.
                        tracing::debug!(
                            channel = %ch_name,
                            "notification: sync-only channel, no sender expected"
                        );
                    } else {
                        warn!(channel = %ch_name, "no channel sender registered for notification");
                    }
                } else {
                    // No channel specified — send to first registered channel (default)
                    let first = {
                        let guard = senders.read().expect("channel_senders RwLock poisoned");
                        guard.iter().next().map(|(k, v)| (k.clone(), v.clone()))
                    };
                    if let Some((ch_name, tx)) = first {
                        info!(channel = %ch_name, "routing notification to default channel");
                        if let Err(e) = tx.send(msg.clone()).await {
                            tracing::warn!(error = %e, "notification send failed");
                        }
                    } else {
                        warn!("notification: no channels registered");
                    }
                }
            }
            info!("notification router ended");
        });
    }

    // 9. Start heartbeat runner — scans agent workspaces for HEARTBEAT.md.
    let hb_enabled = config
        .agents
        .defaults
        .heartbeat
        .as_ref()
        .and_then(|h| h.enabled)
        .unwrap_or(true);
    if hb_enabled {
        let hb_host: std::sync::Arc<dyn rsclaw_heartbeat::HeartbeatHost> =
            std::sync::Arc::new(crate::gateway::heartbeat_host::RuntimeHeartbeatHost {
                registry: Arc::clone(&registry),
                shutdown: Some(shutdown.clone()),
                defaults: config.agents.defaults.clone(),
            });
        let runner = rsclaw_heartbeat::HeartbeatRunner::new(
            hb_host,
            &data_dir,
            heartbeat_memory,
        )
        .with_meditation_deps(rsclaw_heartbeat::MeditationDeps {
            config: Arc::clone(&config),
            db: Arc::clone(&store.db),
        });
        let runner = std::sync::Arc::new(runner);
        runner.run();
        info!("heartbeat runner started");
    }

    // 11. Write PID file early so the hot-reload task can clean it on restart.
    let pid_file = rsclaw_config::loader::pid_file();
    if let Some(parent) = pid_file.parent() {
        if let Err(e) = std::fs::create_dir_all(parent) {
            warn!("could not create PID file directory: {e}");
        }
    }
    let pid = std::process::id();
    if let Err(e) = std::fs::write(&pid_file, pid.to_string()) {
        warn!("could not write PID file: {e}");
    }
    info!(pid, "gateway PID written to {}", pid_file.display());

    // 12. Start config hot-reload watcher (if config file is detectable).
    if let Some(config_path) = config::loader::detect_config_path() {
        let (mut watcher, mut reload_rx) = FileWatcher::new(config_path);
        tokio::spawn(async move { watcher.run().await });
        let live_reload = Arc::clone(&live);
        let (restart_tx, _) = broadcast::channel::<Vec<String>>(8);
        let bridge_tx = restart_request_tx.clone();
        let bridge_pending = Arc::clone(&pending_restart);
        let bridge_shutdown = shutdown.clone();
        let cfg_lang = config
            .raw
            .gateway
            .as_ref()
            .and_then(|g| g.language.as_deref())
            .map(str::to_owned);
        tokio::spawn(async move {
            let lang = rsclaw_i18n::resolve_lang(cfg_lang.as_deref().unwrap_or("en")).to_owned();
            loop {
                match reload_rx.recv().await {
                    Ok(ConfigChange::FullReload(new_cfg)) => {
                        // `apply` now uses `diff_restart_sections` as the
                        // single source of truth: empty = hot-safe (already
                        // written into live locks); non-empty = a restart is
                        // recommended for the listed sections.
                        let new_owned = (*new_cfg).clone();
                        let needs_restart = live_reload.apply(new_owned, &restart_tx).await;
                        if needs_restart.is_empty() {
                            info!("config hot-reload applied (hot-safe fields only)");
                        } else {
                            warn!(?needs_restart, "config change requires gateway restart");
                            // FullReload doesn't fully propagate to running
                            // agents/channels (providers/prompts/credentials
                            // are snapshotted at spawn). Surface a Recommended
                            // banner so the user can apply changes cleanly.
                            publish_restart(
                                &bridge_tx,
                                &bridge_pending,
                                &bridge_shutdown,
                                rsclaw_events::RestartRequest::new(
                                    rsclaw_events::RestartReason::ConfigChanged {
                                        sections: needs_restart,
                                    },
                                    rsclaw_events::RestartUrgency::Recommended,
                                    rsclaw_i18n::t("restart_required_config_changed", &lang),
                                ),
                            );
                        }
                    }
                    Ok(ConfigChange::RequiresRestart(fields)) => {
                        warn!(?fields, "config change requires restart — surfacing banner");
                        publish_restart(
                            &bridge_tx,
                            &bridge_pending,
                            &bridge_shutdown,
                            rsclaw_events::RestartRequest::new(
                                rsclaw_events::RestartReason::ConfigChanged { sections: fields },
                                rsclaw_events::RestartUrgency::Required,
                                rsclaw_i18n::t("restart_required_config_changed", &lang),
                            ),
                        );
                    }
                    Ok(_) => {}
                    Err(_) => break,
                }
            }
        });
    }

    // 13. Start HTTP server.
    let devices_path = rsclaw_config::loader::base_dir().join("var/data/devices.json");
    let devices = Arc::new(crate::ws::DeviceStore::new(devices_path));
    let ws_conns = Arc::new(crate::ws::ConnRegistry::new());

    // Start custom channels (webhook + websocket).
    let custom_webhooks: Arc<
        std::sync::RwLock<
            std::collections::HashMap<String, Arc<rsclaw_channel::custom::CustomWebhookChannel>>,
        >,
    > = Arc::new(std::sync::RwLock::new(std::collections::HashMap::new()));
    start_custom_channels(
        &config,
        Arc::clone(&registry),
        &mut channel_manager,
        Arc::clone(&custom_webhooks),
        Arc::clone(&channel_senders),
        Arc::clone(&store.db),
        shutdown.clone(),
    );

    // Register desktop channel — routes cron delivery to connected WS clients.
    {
        let desktop_ch = Arc::new(crate::gateway::desktop_channel::DesktopChannel::new(Arc::clone(
            &ws_conns,
        )));
        // Bridge the notification_tx → DesktopChannel path so AgentRuntime
        // (which only has notification_tx, not ChannelManager) can route
        // short-delay reminders through the same broadcast path cron uses.
        let (desktop_out_tx, mut desktop_out_rx) = mpsc::channel::<OutboundMessage>(64);
        {
            let mut senders = channel_senders
                .write()
                .expect("channel_senders lock poisoned");
            senders.insert("desktop".to_string(), desktop_out_tx.clone());
            // "ws" is the channel name used by WS-originated agent runs (see
            // ws/methods/chat.rs where AgentMessage.channel = "ws"). Without
            // this alias, OutboundMessages tagged channel="ws" — e.g. WASM
            // plugin progress notify(), async task completion messages — hit
            // the notification router's "no channel sender registered" warn
            // and get dropped, leaving the desktop UI without progress pings.
            senders.insert("ws".to_string(), desktop_out_tx);
        }
        let desktop_for_bridge = Arc::clone(&desktop_ch);
        tokio::spawn(async move {
            use rsclaw_channel::Channel;
            while let Some(msg) = desktop_out_rx.recv().await {
                if let Err(e) = desktop_for_bridge.send(msg).await {
                    warn!(error = %e, "desktop notification bridge: send failed");
                }
            }
        });
        if let Err(e) = channel_manager.register(desktop_ch as Arc<dyn rsclaw_channel::Channel>) {
            warn!("failed to register desktop channel: {e}");
        }
    }

    // All channels registered - now wrap for sharing with cron runner
    let channel_manager = Arc::new(channel_manager);

    // /watch registry — events fly directly from sources to chat without going
    // through the agent. Must be initialized after channels are registered so
    // delivery can resolve any channel name (esp. for /loop /watch composition).
    crate::gateway::watch::WatchRegistry::init(Arc::clone(&channel_manager));
    tracing::info!("watch registry initialized");

    // Create cron reload broadcast channel (used to notify CronRunner of new jobs)
    let (cron_reload_tx, _cron_reload_rx) = tokio::sync::broadcast::channel::<()>(16);
    // Make the sender reachable from non-server paths (fast preparse `/loop`).
    crate::cron::install_reload_sender(cron_reload_tx.clone());

    // Start cron runner — jobs loaded from base_dir/cron.json5
    {
        let cron_cfg =
            config
                .ops
                .cron
                .clone()
                .unwrap_or_else(|| rsclaw_config::schema::CronConfig {
                    enabled: Some(true),
                    max_concurrent_runs: None,
                    session_retention: None,
                    run_log: None,
                    jobs: None,
                    default_delivery: None,
                });
        let cron_enabled = cron_cfg.enabled.unwrap_or(true);

        // Load jobs from openclaw-compatible path
        let cron_file = crate::cron::resolve_cron_store_path();
        let (jobs, parse_ok) = crate::cron::load_cron_jobs();
        if !parse_ok {
            error!(file = %cron_file.display(), "cron.json5 has syntax errors - jobs will NOT run until file is fixed");
        } else if !jobs.is_empty() {
            info!(file = %cron_file.display(), count = jobs.len(), "loaded cron jobs");
        }

        if cron_enabled {
            let cron_data_dir = base_dir.join("var").join("data");
            let runner = CronRunner::new_with_shutdown(
                &cron_cfg,
                jobs,
                !parse_ok, // skip_initial_save if parse failed
                Arc::clone(&registry),
                Arc::clone(&channel_manager),
                cron_data_dir,
                cron_reload_tx.clone(),
                Arc::clone(&ws_conns),
                Some(shutdown.clone()),
            )
            .with_daemon_agent_ids(config.agents.daemon_agent_ids());
            tokio::spawn(async move {
                if let Err(e) = runner.run().await {
                    error!("cron runner error: {e:#}");
                }
            });
            info!("cron runner started");
        }
    }

    // A2A v1.0 plumbing — task event bus, persistent store, push dispatcher
    // all share the same instances so events flow end-to-end.
    let a2a_bus = crate::a2a::event::TaskEventBus::new();
    let a2a_task_store = {
        let path = rsclaw_config::loader::base_dir().join("var/data/a2a/tasks.redb");
        Arc::new(crate::a2a::store::TaskStore::open(&path).expect("open A2A task store"))
    };
    let a2a_push_dispatcher = Arc::new(crate::a2a::push::PushDispatcher::new(
        Arc::clone(&a2a_task_store),
        a2a_bus.clone(),
    ));
    let a2a_relay_hub = Arc::new(crate::a2a::relay::RelayHub::new());

    // Production safety net: a relay hub with no inbound A2A auth is a
    // pass-through that lets any HTTP caller invoke any spoke agent.
    // That's the "dev pass-through" by design, but it's easy to deploy
    // by accident. Surface a single loud warning so an operator can fix
    // it before the box is reachable from anywhere it shouldn't be.
    if config.gateway.a2a_relay.mode == rsclaw_config::runtime::A2aRelayModeRuntime::Hub
        && config.gateway.auth_token.is_none()
        && config.gateway.a2a_principals.is_empty()
    {
        warn!(
            relay_id = %config.gateway.a2a_relay.relay_id,
            "a2a relay is running in Hub mode without inbound auth — any HTTP client \
             can invoke any connected spoke agent. Set `gateway.auth.token` or define \
             `gateway.a2a.clients` before exposing this gateway to a network."
        );
    }

    // User-managed RAG knowledge base. Open once; spawn the async indexing
    // worker that drains embed jobs in the background. A failure here (corrupt
    // store, bad permissions, full disk) disables KB but must not abort the
    // gateway — every other channel and provider stays up.
    let knowledge_svc = match rsclaw_kb::KnowledgeService::open(base_dir.join("kb")) {
        Ok(svc) => {
            let svc = Arc::new(svc);
            svc.spawn_worker();
            // Register the single live instance so agent tools (knowledge_base)
            // reach it without re-opening redb (which is exclusively locked).
            rsclaw_kb::set_global_service(Arc::clone(&svc));
            Some(svc)
        }
        Err(e) => {
            tracing::error!("knowledge base disabled: failed to open store: {e:#}");
            None
        }
    };

    let state = AppState {
        config: Arc::clone(&config),
        live: Arc::clone(&live),
        agents: Arc::clone(&registry),
        store: Arc::clone(&store),
        event_bus: event_tx,
        computer_permission: Arc::clone(&computer_permission),
        computer_permission_tx: computer_permission_tx.clone(),
        computer_status_tx: computer_status_tx.clone(),
        computer_runs: Arc::clone(&computer_runs),
        devices,
        ws_conns,
        feishu: Arc::clone(&feishu_slot),
        wecom: Arc::clone(&wecom_slot),
        whatsapp: Arc::clone(&whatsapp_slot),
        line: Arc::clone(&line_slot),
        zalo: Arc::clone(&zalo_slot),
        started_at: std::time::Instant::now(),
        dm_enforcers: Arc::clone(&dm_enforcers),
        custom_webhooks: Arc::clone(&custom_webhooks),
        cron_reload: cron_reload_tx,
        notification_tx: notification_tx.clone(),
        wasm_plugins: Arc::clone(&wasm_plugins),
        plugins: Arc::clone(&plugins),
        restart_request_tx: restart_request_tx.clone(),
        pending_restart: Arc::clone(&pending_restart),
        shutdown: shutdown.clone(),
        task_event_bus: a2a_bus,
        task_cancels: Arc::new(dashmap::DashMap::new()),
        suspended_tasks: Arc::new(dashmap::DashMap::new()),
        task_store: a2a_task_store,
        push_dispatcher: a2a_push_dispatcher,
        relay_hub: a2a_relay_hub,
        knowledge: knowledge_svc,
        memory: memory.clone(),
        model_health: model_health.clone(),
    };
    crate::a2a::relay::start_spoke_if_configured(state.clone());
    crate::ws::tick::start_tick_loop(Arc::clone(&state.ws_conns));

    // Relay stream deadline sweeper. The hub-side `stream_pending`
    // map has no end-to-end timeout (reqwest's was removed in b94c40f
    // to support long video-gen flows). Without this loop a stuck
    // spoke + healthy WS + slow consumer could pin entries forever.
    {
        let relay_hub = Arc::clone(&state.relay_hub);
        tokio::spawn(async move {
            let mut interval = tokio::time::interval(std::time::Duration::from_secs(60));
            interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
            loop {
                interval.tick().await;
                let swept = relay_hub.sweep_expired_streams();
                if swept > 0 {
                    tracing::info!(swept, "relay stream deadline sweeper");
                }
            }
        });
    }

    // Start browser pool idle reaper (checks every 60s).
    tokio::spawn(async {
        let mut interval = tokio::time::interval(std::time::Duration::from_secs(60));
        interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
        loop {
            interval.tick().await;
            rsclaw_browser::pool::BrowserPool::global()
                .reap_if_idle()
                .await;
        }
    });

    let bind_addr = resolve_bind_addr(&config);
    info!("starting HTTP server on {bind_addr}");

    // macOS permission preflight — turn a SILENT TCC denial into a LOUD one.
    // A rebuild re-signs the binary with a new cdhash; an ad-hoc-signed grant
    // is cdhash-pinned, so Screen Recording / Accessibility silently stop
    // applying (capture hangs ~30s → `Failed to copy data` → wrong-region
    // fallback; input no-ops) even while System Settings shows them enabled.
    for w in rsclaw_desktop::macos_perm::preflight_warnings() {
        warn!("macOS permission preflight: {w}");
    }

    // Background update check (non-blocking)
    tokio::spawn(async {
        tokio::time::sleep(std::time::Duration::from_secs(5)).await;

        let client = reqwest::Client::builder()
            .user_agent("rsclaw/dev")
            .timeout(std::time::Duration::from_secs(10))
            .build();

        let Ok(client) = client else { return };

        // Primary: app.rsclaw.ai/api/version (array of release objects);
        // fallback: GitHub releases list. Both go through the shared
        // `parse_release_body` (array-or-object, v*-tag filtered) used by
        // `rsclaw update`, so the gateway's passive check and the CLI
        // updater agree on what "latest" means.
        let sources = [
            crate::cmd::update::RSCLAW_VERSION_URL,
            "https://api.github.com/repos/rsclaw-ai/rsclaw/releases?per_page=10",
        ];
        let mut release: Option<serde_json::Value> = None;
        for url in sources {
            if let Ok(resp) = client.get(url).send().await {
                if resp.status().is_success() {
                    let body = resp.bytes().await.unwrap_or_default();
                    if let Some(found) = crate::cmd::update::parse_release_body(&body) {
                        release = Some(found);
                        break;
                    }
                }
            }
        }

        if let Some(release) = release {
            {
                let latest_raw = release["tag_name"].as_str().unwrap_or("");
                let current_raw = option_env!("RSCLAW_BUILD_VERSION").unwrap_or("dev");
                // Extract bare version: "2026.4.1 (abc123)" -> "2026.4.1",
                // "2026.4.1-beta" -> "2026.4.1".
                fn strip_ver(s: &str) -> &str {
                    let s = s.trim_start_matches('v');
                    let s = s.split_once(' ').map(|(v, _)| v).unwrap_or(s);
                    s.split_once('-').map(|(v, _)| v).unwrap_or(s)
                }
                fn ver_newer(latest: &str, current: &str) -> bool {
                    let parse = |s: &str| -> Vec<u32> {
                        s.split('.').filter_map(|p| p.parse().ok()).collect()
                    };
                    let l = parse(latest);
                    let c = parse(current);
                    for i in 0..l.len().max(c.len()) {
                        let lv = l.get(i).copied().unwrap_or(0);
                        let cv = c.get(i).copied().unwrap_or(0);
                        if lv > cv {
                            return true;
                        }
                        if lv < cv {
                            return false;
                        }
                    }
                    false
                }
                let latest = strip_ver(latest_raw);
                let current = strip_ver(current_raw);
                if !latest.is_empty() && ver_newer(latest, current) {
                    info!(
                        current = current_raw,
                        latest = latest_raw,
                        "new rsclaw version available -- run `rsclaw update` to upgrade"
                    );
                }
            }
        }
    });

    // Background remote defaults.toml refresh (non-blocking).
    //
    // Fetches `https://app.rsclaw.ai/defaults.toml` and merges it into the
    // on-disk file via the same version-gated merge as the embedded
    // upgrade path (refresh shipped entries, preserve user-added, back up
    // first). This lets new providers/channels/models ship WITHOUT a
    // binary release. Throttled to once per 2h via a `$base_dir/.defaults_fetch`
    // timestamp (persisted across restarts) so frequent reboots don't hammer
    // the CDN. ANY failure — network, timeout, non-200, invalid TOML, or a
    // remote that isn't newer — is a silent no-op: the local (embedded-merged)
    // defaults.toml is always the fallback. Takes effect on the next config
    // load / restart, never mutating the already-running config in place.
    tokio::spawn(async {
        const DEFAULTS_URL: &str = "https://app.rsclaw.ai/defaults.toml";
        const THROTTLE: std::time::Duration = std::time::Duration::from_secs(2 * 60 * 60);

        tokio::time::sleep(std::time::Duration::from_secs(8)).await;

        let stamp = rsclaw_config::loader::base_dir().join(".defaults_fetch");
        let now = std::time::SystemTime::now();
        if let Ok(meta) = std::fs::metadata(&stamp) {
            if let Ok(modified) = meta.modified() {
                if now.duration_since(modified).unwrap_or(THROTTLE) < THROTTLE {
                    debug!("remote defaults: throttled (checked < 2h ago)");
                    return;
                }
            }
        }

        let Ok(client) = reqwest::Client::builder()
            .user_agent("rsclaw/dev")
            .timeout(std::time::Duration::from_secs(10))
            .build()
        else {
            return;
        };

        match client.get(DEFAULTS_URL).send().await {
            Ok(resp) if resp.status().is_success() => {
                // Touch the throttle stamp on a successful fetch so a
                // transient outage retries on the next boot instead of
                // being suppressed for 2h.
                if let Err(e) = std::fs::write(&stamp, b"") {
                    tracing::debug!(error = %e, "failed to write defaults fetch throttle stamp");
                }
                const MAX_DEFAULTS_BYTES: usize = 1_048_576;
                // Fast-path reject on an honest Content-Length, but never
                // TRUST it: a chunked / header-less / lying response would
                // otherwise let resp.text() buffer an unbounded body into
                // memory. Enforce the cap on the actual bytes by streaming.
                if let Some(len) = resp.content_length() {
                    if len > MAX_DEFAULTS_BYTES as u64 {
                        tracing::warn!(size = len, "remote defaults.toml too large, skipping");
                        return;
                    }
                }
                use futures::StreamExt;
                let mut stream = resp.bytes_stream();
                let mut buf: Vec<u8> = Vec::new();
                let mut overflow = false;
                while let Some(chunk) = stream.next().await {
                    match chunk {
                        Ok(bytes) => {
                            if buf.len() + bytes.len() > MAX_DEFAULTS_BYTES {
                                overflow = true;
                                break;
                            }
                            buf.extend_from_slice(&bytes);
                        }
                        Err(e) => {
                            debug!(error = %e, "remote defaults.toml read failed; keeping local");
                            return;
                        }
                    }
                }
                if overflow {
                    tracing::warn!(
                        cap = MAX_DEFAULTS_BYTES,
                        "remote defaults.toml exceeded size cap mid-stream, skipping"
                    );
                    return;
                }
                match String::from_utf8(buf) {
                    Ok(body) => match rsclaw_config::loader::merge_remote_defaults(&body) {
                        Ok(true) => info!(
                            "remote defaults.toml updated; takes effect on next restart"
                        ),
                        Ok(false) => debug!("remote defaults.toml not newer than local"),
                        Err(e) => {
                            debug!(error = %e, "remote defaults.toml invalid; keeping local")
                        }
                    },
                    Err(e) => debug!(error = %e, "remote defaults.toml not valid UTF-8; keeping local"),
                }
            }
            Ok(resp) => debug!(status = %resp.status(), "remote defaults.toml fetch non-200"),
            Err(e) => debug!(error = %e, "remote defaults.toml fetch failed; keeping local"),
        }
    });

    // Global signal handler: a single SIGINT or SIGTERM triggers the
    // shared graceful drain (same path as POST /api/v1/shutdown). Without
    // this, no component listens for OS signals — Ctrl-C would be silently
    // absorbed by tokio's signal subsystem and the gateway would only
    // exit via HTTP or kill -9.
    {
        let sd = shutdown.clone();
        tokio::spawn(async move {
            #[cfg(unix)]
            {
                use tokio::signal::unix::{SignalKind, signal};
                let mut sigterm = match signal(SignalKind::terminate()) {
                    Ok(s) => s,
                    Err(e) => {
                        warn!("failed to install SIGTERM handler: {e:#}");
                        return;
                    }
                };
                tokio::select! {
                    res = tokio::signal::ctrl_c() => {
                        if let Err(e) = res {
                            warn!("ctrl_c handler error: {e:#}");
                            return;
                        }
                        info!("SIGINT received, beginning graceful shutdown");
                    }
                    _ = sigterm.recv() => {
                        info!("SIGTERM received, beginning graceful shutdown");
                    }
                }
            }
            #[cfg(not(unix))]
            {
                if let Err(e) = tokio::signal::ctrl_c().await {
                    warn!("ctrl_c handler error: {e:#}");
                    return;
                }
                info!("Ctrl-C received, beginning graceful shutdown");
            }
            // Stop /watch source/processor tasks before draining HTTP so SSE
            // and subprocesses get a clean exit instead of dangling.
            if let Some(reg) = crate::gateway::watch::WatchRegistry::global() {
                reg.shutdown_all().await;
            }
            sd.begin_drain();
        });
    }

    // Run serve with a hard-timeout safety net. Axum's
    // `with_graceful_shutdown` waits for ALL existing connections to
    // close after the drain signal — but long-lived WS connections
    // (the Tauri UI keeps one open indefinitely) never close on
    // their own, so without an outer timeout the process can hang
    // forever after SIGTERM. We give the drain 60s (mirrors the
    // existing inflight-drain timeout in the restart branch below);
    // after that we log a warning and bail so the process actually
    // exits and the restart path (or systemd / CI) can proceed.
    const SHUTDOWN_HARD_TIMEOUT_SECS: u64 = 60;
    let shutdown_for_timeout = shutdown.clone();
    let result = tokio::select! {
        r = serve(state, bind_addr) => r,
        _ = async {
            // Wait for the drain to begin first — the timeout only
            // makes sense once shutdown is requested. Before drain we
            // never hit the sleep branch.
            shutdown_for_timeout.notified().await;
            tokio::time::sleep(Duration::from_secs(SHUTDOWN_HARD_TIMEOUT_SECS)).await;
            warn!(
                timeout_secs = SHUTDOWN_HARD_TIMEOUT_SECS,
                "graceful shutdown hard timeout reached — connections still open, forcing serve to return"
            );
        } => Ok(()),
    };

    // At this point `axum::serve` has returned, which means the listener has
    // been dropped — so the port is free for whatever runs next. Two paths:
    //   - clean shutdown (Ctrl-C, SIGTERM, /api/v1/shutdown): just clean up the PID
    //     file and return.
    //   - restart requested (/api/v1/restart, system.restart): wait for non-HTTP
    //     inflight to drain, spawn the replacement, then exit. We spawn HERE rather
    //     than in the restart handler to avoid the race where the child's `bind()`
    //     runs before the parent's listener drops; that race could cause
    //     `cmd_gateway` to see "port in use" and exit cleanly, leaving the gateway
    //     dead.
    if shutdown.is_restart_requested() {
        // Channel-origin turns cannot deliver their result anymore — the
        // outbound channel senders stopped when drain began — so waiting
        // for them only delays the restart (observed: 81/108 historical
        // restarts burned the full 60s on exactly this). Cancel their
        // turns now; HTTP/API turns still hold an open connection that CAN
        // answer, so they keep the full drain window. Explicit allowlist:
        // only origins whose delivery path is provably dead.
        const UNDELIVERABLE_ORIGINS: &[&str] = &[
            "wechat", "feishu", "telegram", "discord", "qq", "dingtalk",
            "wecom", "slack", "whatsapp", "line", "matrix", "signal", "cron",
        ];
        let mut cancelled = 0usize;
        for handle in registry.all() {
            if let Ok(map) = handle.cancel_tokens.read() {
                for (sk, tok) in map.iter() {
                    // Channel session keys: "agent:<id>:<channel>:..."
                    let channel = sk.split(':').nth(2).unwrap_or("");
                    if UNDELIVERABLE_ORIGINS.contains(&channel) {
                        tok.cancel();
                        cancelled += 1;
                    }
                }
            }
        }
        if cancelled > 0 {
            info!(
                cancelled,
                "drain: cancelled channel-origin turns whose delivery path already stopped"
            );
        }

        info!("restart requested - waiting for inflight drain (max 60s)");
        let deadline = std::time::Instant::now() + Duration::from_secs(60);
        loop {
            let n = shutdown.inflight();
            if n == 0 {
                info!("graceful drain: inflight cleared");
                break;
            }
            if std::time::Instant::now() >= deadline {
                // Identify WHICH turns are still holding the inflight guard so
                // the log says "stuck on heartbeat" / "stuck on this HTTP turn"
                // instead of just a bare count. Live turns register a hard-
                // cancel token under their session key (`agent:<id>:<channel>:..`).
                let stuck: Vec<String> = registry
                    .all()
                    .iter()
                    .filter_map(|h| h.cancel_tokens.read().ok().map(|m| {
                        m.keys().cloned().collect::<Vec<_>>()
                    }))
                    .flatten()
                    .collect();
                warn!(
                    inflight = n,
                    stuck = ?stuck,
                    "graceful drain: 60s timeout reached, restarting anyway"
                );
                break;
            }
            tokio::time::sleep(Duration::from_millis(100)).await;
        }

        // If a service manager (launchd/systemd) is supervising this process,
        // ask it to restart us so the new instance picks up the supervisor's
        // env, ulimits, log routing — instead of doing a naked re-exec that
        // bypasses the manager. `try_service_self_restart` returns true only
        // when it has confirmed delivery to the supervisor; on success it
        // does NOT return (the supervisor SIGTERMs us as part of the kickstart).
        if try_service_self_restart() {
            info!("service-manager restart dispatched; exiting for supervisor takeover");
            std::process::exit(0);
        }

        let exe = match std::env::current_exe() {
            Ok(p) => p,
            Err(e) => {
                error!("current_exe failed; cannot respawn replacement: {e:#}");
                // Don't remove the PID file - we'd rather leave a stale PID
                // than wipe it and bail with no replacement running.
                return result;
            }
        };
        let mut cmd = std::process::Command::new(&exe);
        // Forward --dev, --profile, --base-dir flags so the replacement
        // process uses the same isolation mode as the original.
        let original_args: Vec<String> = std::env::args().collect();
        let mut extra_args: Vec<String> = Vec::new();
        let mut i = 1; // skip argv[0]
        while i < original_args.len() {
            match original_args[i].as_str() {
                "--dev" => {
                    extra_args.push("--dev".to_owned());
                }
                "--profile" => {
                    extra_args.push("--profile".to_owned());
                    if let Some(val) = original_args.get(i + 1) {
                        extra_args.push(val.clone());
                        i += 1;
                    }
                }
                "--base-dir" => {
                    extra_args.push("--base-dir".to_owned());
                    if let Some(val) = original_args.get(i + 1) {
                        extra_args.push(val.clone());
                        i += 1;
                    }
                }
                s if s.starts_with("--profile=") => {
                    extra_args.push(s.to_owned());
                }
                s if s.starts_with("--base-dir=") => {
                    extra_args.push(s.to_owned());
                }
                _ => {}
            }
            i += 1;
        }
        extra_args.extend(["gateway".to_owned(), "run".to_owned()]);
        cmd.args(&extra_args);
        // Pass our PID so the replacement's `wait_for_parent_release`
        // safety net knows whose exit to wait for before opening
        // redb. Even on the Unix exec path (where the fd / lock
        // release is atomic) we set this so a re-exec from a SIGTERM
        // path that didn't quite finish drain still has a fallback
        // signal to wait on. Cheap to set, narrows the window of any
        // future race introduction.
        cmd.env("RSCLAW_PARENT_PID", std::process::id().to_string());
        // Re-attach stdio to the canonical gateway log. fd inheritance
        // usually carries this through exec(), but the spawn() fallback —
        // and an exec that failed mid-rebuild (ETXTBSY) — leaves the
        // replacement logging into whatever pipe the restart requester
        // happened to hold. Observed 2026-06-12: a post-restart instance
        // logged nowhere for 85 minutes (wechat outage invisible).
        // Explicit re-attach makes both paths deterministic; Rust's
        // CommandExt::exec applies configured stdio before the exec.
        {
            let log_path = rsclaw_config::loader::log_file();
            if let Some(parent) = log_path.parent() {
                let _ = std::fs::create_dir_all(parent);
            }
            if let Ok(f) = std::fs::OpenOptions::new()
                .create(true)
                .append(true)
                .open(&log_path)
                && let Ok(f2) = f.try_clone()
            {
                cmd.stdout(std::process::Stdio::from(f));
                cmd.stderr(std::process::Stdio::from(f2));
            }
        }
        // Windows: suppress the console flash when re-execing from a GUI app.
        #[cfg(target_os = "windows")]
        {
            use std::os::windows::process::CommandExt;
            const CREATE_NO_WINDOW: u32 = 0x0800_0000;
            cmd.creation_flags(CREATE_NO_WINDOW);
        }
        // Unix: use `exec()` instead of `spawn() + exit(0)`. The race
        // we kept hitting on every `gateway restart`:
        //   1. parent spawns child (clone semantics: child inherits
        //      our open fds, but the redb file LOCK is process-scoped
        //      and NOT inherited — both processes are now contending)
        //   2. parent calls exit(0), eventually closes redb and
        //      releases the lock — but not before
        //   3. child reaches MemoryStore::open and fails with
        //      "Database locked by another gateway instance"
        // `exec` replaces the current process image in place: all
        // file descriptors (and therefore the redb lock) close
        // atomically before the new image runs `main`. Zero overlap
        // window, no spawn+exit race. Doesn't return on success.
        #[cfg(unix)]
        {
            use std::os::unix::process::CommandExt;
            let err = cmd.exec();
            // exec() only returns on failure (e.g. EACCES, ENOENT on
            // the binary itself). Fall through to the legacy
            // spawn+exit path so the user at least gets SOMETHING
            // running — even if it does hit the lock race.
            error!(
                "exec for replacement gateway failed: {err:#}; \
                 falling back to spawn+exit (may hit lock race)"
            );
        }
        match cmd.spawn() {
            Ok(_) => info!("replacement gateway spawned"),
            Err(e) => error!("failed to spawn replacement gateway: {e:#}"),
        }
        // Do NOT remove the PID file - the new gateway process overwrites it
        // with its own PID on startup. Removing here races and can leave us
        // with no PID file after a successful restart.
        std::process::exit(0);
    }

    // Clean shutdown path - remove the PID file before returning.
    if let Err(e) = std::fs::remove_file(&pid_file) {
        warn!("could not remove PID file on exit: {e}");
    }
    result
}

/// Best-effort: if this process is currently supervised by launchd (macOS)
/// or systemd (Linux), dispatch the restart through the service manager
/// rather than via naked `cmd.spawn()` from `current_exe`.
///
/// Why: a supervisor-managed restart respects the supervisor's env, ulimits,
/// log routing, and respawn policy. A naked exec orphans the new process to
/// PID 1 and loses anything the supervisor would have re-applied.
///
/// Detection is **runtime-only** (env vars set by the supervisor on the
/// Safety net for the replacement gateway during a `gateway restart`.
///
/// The parent sets `RSCLAW_PARENT_PID=<pid>` on the child's env right
/// before respawn. On the macOS / Linux exec path this is mostly
/// belt-and-suspenders (exec replaces the process image in place, so
/// fds + the redb file lock close atomically before `main` runs).
/// But it also covers the cases exec doesn't:
///   * Windows native respawn — still uses spawn+exit, so a window
///     where parent + child both contend the redb lock is real.
///   * `rsclaw gateway start` invoked by a human while a previous
///     gateway is still in the middle of graceful drain (60s
///     window): the new process inherits no env from the old one,
///     `RSCLAW_PARENT_PID` is unset, and we just proceed; if it
///     genuinely conflicts the existing PID-file check + redb's
///     own error surface that condition cleanly.
///   * Future codepaths that bypass exec for whatever reason.
///
/// Polls `kill(pid, 0)` every 50ms for up to 5s. Returns immediately
/// when the env var is unset (clean cold start) or the parent has
/// already exited. Logs at info on actual wait so the next
/// "why did startup take 3 seconds" question is answerable from the
/// log alone.
fn wait_for_parent_release() {
    let Ok(pid_str) = std::env::var("RSCLAW_PARENT_PID") else {
        return;
    };
    let Ok(pid) = pid_str.trim().parse::<i32>() else {
        // Malformed env — degrade silently rather than blocking
        // startup forever. The PID file / redb error path takes
        // over if there's actually a conflict.
        return;
    };
    // Don't wait on ourselves. Can happen on weird launchd / shell
    // setups that re-export the env across a re-exec.
    if pid == std::process::id() as i32 {
        return;
    }
    let start = std::time::Instant::now();
    let deadline = start + std::time::Duration::from_secs(5);
    let mut waited = false;
    while std::time::Instant::now() < deadline {
        if !parent_alive(pid) {
            if waited {
                info!(
                    parent_pid = pid,
                    elapsed_ms = start.elapsed().as_millis() as u64,
                    "parent gateway exited; lock released"
                );
            }
            return;
        }
        waited = true;
        std::thread::sleep(std::time::Duration::from_millis(50));
    }
    warn!(
        parent_pid = pid,
        "parent gateway still alive after 5s; opening anyway (may hit lock race)"
    );
    // Best-effort clean of the env so any further re-execs from THIS
    // process don't replay the wait.
    unsafe {
        std::env::remove_var("RSCLAW_PARENT_PID");
    }
}

/// Cross-platform "is this PID still running" probe.
///
/// Unix uses `kill(pid, 0)` — the kernel rejects it with `ESRCH` when
/// no process is running with that pid (or `EPERM` if it IS running
/// but we lack permission; either way the process exists). Windows
/// is handled by shelling out to `tasklist` — adds a few-ms cost,
/// but the Windows native-respawn path is rare enough that the
/// extra spawn isn't worth a `windows-sys` dependency in Cargo.toml.
fn parent_alive(pid: i32) -> bool {
    #[cfg(unix)]
    {
        // libc::kill returns 0 on success, -1 on failure with errno
        // set; ESRCH (3) = "no such process".
        // SAFETY: kill is async-signal-safe; signal 0 is the
        // "existence check" syscall that doesn't actually deliver.
        let rc = unsafe { libc::kill(pid, 0) };
        if rc == 0 {
            return true;
        }
        // Any errno OTHER than ESRCH means the process exists but we
        // can't signal it — treat as alive.
        std::io::Error::last_os_error().raw_os_error() != Some(libc::ESRCH)
    }
    #[cfg(not(unix))]
    {
        // Windows fallback: `tasklist /FI "PID eq <n>" /NH` prints a
        // single matching row when the pid is alive, or "INFO: No
        // tasks ..." when it's gone. Cheap and dependency-free.
        //
        // CREATE_NO_WINDOW = 0x08000000 — without it the tasklist
        // process briefly flashes a console window. Every gateway
        // restart's parent-PID wait would otherwise pop a momentary
        // window each iteration.
        use std::os::windows::process::CommandExt;
        match std::process::Command::new("tasklist")
            .args([
                "/FI",
                &format!("PID eq {pid}"),
                "/NH",
                "/FO",
                "CSV",
            ])
            .creation_flags(0x08000000)
            .output()
        {
            Ok(out) => {
                let s = String::from_utf8_lossy(&out.stdout);
                s.lines().any(|l| l.contains(&pid.to_string()))
            }
            // tasklist unavailable (very old Windows / WSL edge) —
            // treat as dead so we don't deadlock startup; the redb
            // lock surface will error cleanly on actual conflict.
            Err(_) => false,
        }
    }
}

/// running process), distinct from `cmd::gateway::try_service_start` /
/// `try_service_stop` which probe install-file presence.
///
/// Returns true when the supervisor command was dispatched successfully —
/// caller should then `exit(0)` to let the supervisor finish the lifecycle.
/// Returns false in all other cases (not supervised, supervisor command
/// failed, or unsupported OS); caller falls back to native respawn.
fn try_service_self_restart() -> bool {
    #[cfg(target_os = "macos")]
    {
        // launchd sets XPC_SERVICE_NAME (= job label) for managed processes.
        // BUT macOS sets it to the literal string "0" for processes that are
        // NOT launched as a managed service (shells, `cargo run`, Terminal
        // children). Treating "0" (or empty) as "supervised" sends us to
        // `launchctl kickstart gui/<uid>/0`, which fails with "Could not find
        // service 0" on every restart before falling back. Only a real job
        // label means we're actually supervised.
        let label = match std::env::var("XPC_SERVICE_NAME") {
            Ok(l) if !l.is_empty() && l != "0" => l,
            _ => return false,
        };
        info!(label = %label, "detected launchd supervision; kickstarting via launchctl");
        // Try the user agent domain first (gui/<uid>/<label>), then the
        // system daemon domain. `-k` kills the existing job before starting.
        let uid = unsafe { libc::getuid() };
        for target in [
            format!("gui/{}/{}", uid, label),
            format!("system/{}", label),
        ] {
            let status = std::process::Command::new("launchctl")
                .args(["kickstart", "-k", &target])
                .status();
            if matches!(status, Ok(s) if s.success()) {
                info!(target = %target, "launchctl kickstart dispatched");
                return true;
            }
        }
        // Routine on every non-launchd dev gateway (nohup/CLI starts) —
        // info, not warn: the native respawn below is the expected path.
        info!("launchctl kickstart unavailable for label {label}; using native respawn");
        false
    }

    #[cfg(target_os = "linux")]
    {
        // systemd sets INVOCATION_ID for service units. Without it, no
        // service supervision is in play.
        if std::env::var("INVOCATION_ID").is_err() {
            return false;
        }
        // Derive the unit name from /proc/self/cgroup — systemd encodes it
        // in the cgroup path (e.g. `/system.slice/rsclaw.service`).
        let unit = std::fs::read_to_string("/proc/self/cgroup")
            .ok()
            .and_then(|s| {
                s.lines()
                    .find_map(|l| l.rsplit('/').next().filter(|t| t.ends_with(".service")))
                    .map(str::to_owned)
            });
        let Some(unit) = unit else {
            warn!("INVOCATION_ID set but unit name not found in /proc/self/cgroup; falling back");
            return false;
        };
        info!(unit = %unit, "detected systemd supervision; restarting via systemctl");
        // Try --user first; if it fails (not a user unit), retry system-wide.
        for args in [
            vec!["--user", "restart", unit.as_str()],
            vec!["restart", unit.as_str()],
        ] {
            let status = std::process::Command::new("systemctl").args(&args).status();
            if matches!(status, Ok(s) if s.success()) {
                info!(unit = %unit, "systemctl restart dispatched");
                return true;
            }
        }
        warn!("systemctl restart failed for unit {unit}; falling back to native respawn");
        false
    }

    #[cfg(target_os = "windows")]
    {
        // The Service Control Manager runs services in session 0 with
        // SESSIONNAME=Services. Absent or different means we're interactive.
        let in_services_session = std::env::var("SESSIONNAME")
            .map(|s| s.eq_ignore_ascii_case("Services"))
            .unwrap_or(false);
        if !in_services_session {
            return false;
        }
        info!("detected Windows service supervision; signaling SCM via sc stop");
        // Note: a service can't `sc restart` itself — stop is one-way and
        // start runs in a detached SCM context. The actual restart is gated
        // by the service's FailureActions policy. The installer should set
        //   sc failure rsclaw reset=86400 actions=restart/5000
        // so SCM auto-respawns us. If FailureActions isn't configured, this
        // path stops us and leaves it to a human / monitoring system.
        use std::os::windows::process::CommandExt;
        let status = std::process::Command::new("sc")
            .args(["stop", "rsclaw"])
            .creation_flags(0x08000000)
            .status();
        if matches!(status, Ok(s) if s.success()) {
            info!("sc stop dispatched; SCM FailureActions will respawn if configured");
            return true;
        }
        warn!("sc stop failed; falling back to native respawn");
        false
    }

    #[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
    {
        // No supported service manager on this OS — caller will native-respawn.
        false
    }
}

// ---------------------------------------------------------------------------
// Agent task spawning
// ---------------------------------------------------------------------------

#[allow(clippy::too_many_arguments)]
/// Map a registry name to the env vars it cares about: `(api_key_var,
/// base_url_var)`. Hard-coded here because the env name is part of each
/// registry's published contract — we don't want users renaming their
/// existing env vars by editing defaults.toml.
fn registry_env_names(name: &str) -> Option<(&'static str, &'static str)> {
    match name {
        "iwencai" => Some(("IWENCAI_API_KEY", "IWENCAI_BASE_URL")),
        // Future registries with paid keys go here.
        _ => None,
    }
}

/// Read the root-level `env: { K: V, ... }` map and export each entry to
/// the gateway process env. Values may reference other env vars via
/// `${VAR}` — those are expanded against the current process env at the
/// time this function runs (so `${HOME}` etc work out of the box).
///
/// **Precedence:** shell-provided env wins. If a key already exists in
/// the process env (e.g. inherited from the launching shell), the config
/// value is skipped. This lets users override per-launch without editing
/// rsclaw.json5.
///
/// SAFETY: called once during single-threaded gateway startup, before
/// any async runtime tasks spawn — matches the existing precedent in
/// `apply_proxy_env` and `propagate_skill_registry_env`.
fn propagate_user_env(config: &RuntimeConfig) {
    let Some(env_map) = config.raw.env.as_ref() else {
        return;
    };
    apply_user_env_map(&env_map.0);
}

/// Inner helper, separated from `propagate_user_env` so it can be unit-tested
/// without constructing a full `RuntimeConfig`.
fn apply_user_env_map(env_map: &std::collections::HashMap<String, String>) {
    for (key, raw_val) in env_map {
        if key.is_empty() {
            continue;
        }
        if std::env::var(key).is_ok() {
            // Already set by shell / launchd / systemd — don't clobber.
            continue;
        }
        let expanded = rsclaw_config::loader::expand_env_vars(raw_val);
        // SAFETY: pre-async-runtime, single-threaded.
        unsafe { std::env::set_var(key, &expanded) };
        info!(key = %key, "exported user env var from rsclaw.json5");
    }
}

/// Read `skill_registries.<name>.{apiKey,baseUrl}` from the resolved
/// config and export each non-empty value to the corresponding env var.
/// SAFETY: called once during single-threaded gateway startup, before any
/// async runtime tasks spawn — matches the existing precedent in
/// `apply_proxy_env`.
fn propagate_skill_registry_env(config: &RuntimeConfig) {
    let Some(map) = config.raw.skill_registries.as_ref() else {
        return;
    };
    for (name, entry) in map {
        let Some((api_key_var, base_url_var)) = registry_env_names(name) else {
            continue;
        };
        if let Some(key_field) = entry.api_key.as_ref() {
            if let Some(val) = key_field.resolve_early().filter(|s| !s.is_empty()) {
                if std::env::var(api_key_var).is_err() {
                    // SAFETY: pre-async-runtime, single-threaded.
                    unsafe { std::env::set_var(api_key_var, &val) };
                    info!(registry = %name, env = api_key_var, "exported registry api key to env");
                }
            }
        }
        if let Some(url_field) = entry.base_url.as_ref() {
            if let Some(val) = url_field.resolve_early().filter(|s| !s.is_empty()) {
                if std::env::var(base_url_var).is_err() {
                    unsafe { std::env::set_var(base_url_var, &val) };
                    info!(registry = %name, env = base_url_var, "exported registry base url to env");
                }
            }
        }
    }
}

fn spawn_agent_tasks(
    receivers: HashMap<String, mpsc::Receiver<AgentMessage>>,
    registry: Arc<AgentRegistry>,
    config: Arc<RuntimeConfig>,
    live: Arc<LiveConfig>,
    store: Arc<Store>,
    skills: Arc<SkillRegistry>,
    providers: Arc<ProviderRegistry>,
    memory: Option<Arc<tokio::sync::Mutex<MemoryStore>>>,
    event_tx: broadcast::Sender<rsclaw_events::AgentEvent>,
    spawner: Option<Arc<AgentSpawner>>,
    plugins: Option<Arc<rsclaw_plugin::PluginRegistry>>,
    mcp: Option<Arc<rsclaw_mcp::McpRegistry>>,
    notification_tx: Option<broadcast::Sender<rsclaw_channel::OutboundMessage>>,
    wasm_plugins: Arc<Vec<rsclaw_plugin::WasmPlugin>>,
    computer_permission: Arc<rsclaw_computer::permission::RedbPermissionStore>,
    computer_permission_tx: broadcast::Sender<rsclaw_computer::permission::PermissionRequest>,
    computer_status_tx: broadcast::Sender<rsclaw_computer::status::ComputerUseStatus>,
    computer_runs: Arc<
        tokio::sync::RwLock<std::collections::HashMap<String, Arc<std::sync::atomic::AtomicBool>>>,
    >,
    model_health: rsclaw_provider::health::ProviderHealthRegistry,
    cap_manager: std::sync::Arc<rsclaw_cap::CapAgentManager>,
    cap_live_manager: std::sync::Arc<rsclaw_cap::CapLiveManager>,
) {
    for (agent_id, mut rx) in receivers {
        let handle = match registry.get(&agent_id) {
            Ok(h) => h,
            Err(e) => {
                error!(agent_id, "agent handle not found: {e:#}");
                continue;
            }
        };

        // Collect fallback models from agent config → global defaults.
        let fallback_models = handle
            .config
            .model
            .as_ref()
            .and_then(|m| m.fallbacks.clone())
            .or_else(|| {
                config
                    .agents
                    .defaults
                    .model
                    .as_ref()
                    .and_then(|m| m.fallbacks.clone())
            })
            .unwrap_or_default();

        let mut runtime = AgentRuntime::new(
            Arc::clone(&handle),
            Arc::clone(&config),
            Arc::clone(&live),
            Arc::clone(&providers),
            fallback_models,
            Arc::clone(&skills),
            Arc::clone(&store),
            memory.clone(),
            Some(Arc::clone(&registry)),
            Some(event_tx.clone()),
            spawner.clone(),
            plugins.clone(),
            mcp.clone(),
            notification_tx.clone(),
            model_health.clone(),
            Some(Arc::clone(&cap_manager)),
            Some(Arc::clone(&cap_live_manager)),
        );

        // Inject WASM plugins into the agent runtime.
        runtime.wasm_plugins = Arc::clone(&wasm_plugins);

        // Share the computer_use permission store + broadcast channel
        // so `tool_vlm_drive` can register pending requests that the WS
        // handler can resolve.
        runtime.computer_permission = Some(Arc::clone(&computer_permission));
        runtime.computer_permission_tx = Some(computer_permission_tx.clone());
        runtime.computer_status_tx = Some(computer_status_tx.clone());
        runtime.computer_runs = Some(Arc::clone(&computer_runs));

        let event_tx_task = event_tx.clone();
        let config_for_task = Arc::clone(&config);
        tokio::spawn(async move {
            info!(agent_id = %handle.id, "agent runtime task started");
            while let Some(msg) = rx.recv().await {
                info!(
                    agent_id = %handle.id,
                    session_key = %msg.session_key,
                    channel = %msg.channel,
                    "agent runtime: received msg from queue"
                );
                let AgentMessage {
                    session_key,
                    text,
                    channel,
                    peer_id,
                    chat_id,
                    reply_tx,
                    extra_tools,
                    images,
                    files,
                    account,
                    task_id,
                    context_id,
                    cancel_token,
                    event_tx,
                    input_request_tx,
                } = msg;
                // Hard-cancel token for this turn. A2A callers supply their own
                // (CancelTask). For everyone else (WS chat.abort, channels) we
                // mint one and register it under the session key so chat.abort
                // can fire `.cancel()` — the `tokio::select!` below then drops
                // the in-flight `run_turn` future immediately, even if it's
                // parked on a stalled LLM stream. `registered` tracks whether
                // we own the map entry so we clean it up afterwards.
                let (turn_token, registered) = match cancel_token {
                    Some(t) => (t, false),
                    None => {
                        let t = tokio_util::sync::CancellationToken::new();
                        if let Ok(mut toks) = handle.cancel_tokens.write() {
                            toks.insert(session_key.clone(), t.clone());
                        }
                        (t, true)
                    }
                };

                // Build a TurnContext from the A2A wires on AgentMessage.
                // `cancel_token` is now always set, so the runtime's
                // cooperative `is_cancelled()` checks (between iterations and
                // at tool-dispatch boundaries) also observe WS aborts.
                let is_daemon = runtime.is_daemon_agent(&handle.id);
                // Progress heartbeat for the daemon watchdog (bumped once per
                // agent-loop iteration via TurnContext::progress_tick).
                let progress = std::sync::Arc::new(std::sync::atomic::AtomicU64::new(0));
                let turn_ctx = rsclaw_agent::registry::TurnContext {
                    task_id,
                    context_id,
                    event_tx,
                    cancel_token: Some(turn_token.clone()),
                    input_request_tx,
                    progress: Some(progress.clone()),
                };
                // Stuck-turn watchdog. Two modes:
                //  - Normal turns: flat 20-min wall-clock cap (a single turn
                //    should complete; if not, something's wedged).
                //  - Daemon agents (agent_wechat monitor): they loop FOREVER, so
                //    a wall-clock cap can't apply. Instead watch PROGRESS — the
                //    loop bumps a counter every iteration; if it stops advancing
                //    for STALL_LIMIT, a tool is genuinely wedged (e.g. enter_chat
                //    spinning), so cancel and let the cron backstop restart. A
                //    healthy forever-loop bumps every few seconds → never killed.
                let watchdog_token = turn_token.clone();
                let watchdog_agent = handle.id.clone();
                let watchdog_session = session_key.clone();
                let watchdog = tokio::spawn(async move {
                    use std::sync::atomic::Ordering;
                    if is_daemon {
                        const POLL: std::time::Duration = std::time::Duration::from_secs(30);
                        const STALL_LIMIT: std::time::Duration =
                            std::time::Duration::from_secs(180);
                        let mut last = progress.load(Ordering::Relaxed);
                        let mut stalled = std::time::Duration::ZERO;
                        loop {
                            tokio::time::sleep(POLL).await;
                            if watchdog_token.is_cancelled() {
                                break;
                            }
                            let cur = progress.load(Ordering::Relaxed);
                            if cur != last {
                                last = cur;
                                stalled = std::time::Duration::ZERO;
                                continue;
                            }
                            stalled += POLL;
                            if stalled >= STALL_LIMIT {
                                tracing::error!(
                                    agent = %watchdog_agent,
                                    session = %watchdog_session,
                                    stall_s = stalled.as_secs(),
                                    "stuck-turn watchdog (daemon): no loop progress — a tool is \
                                     wedged; firing cancel_token (cron will restart)"
                                );
                                watchdog_token.cancel();
                                break;
                            }
                        }
                    } else {
                        const TURN_WALL_CLOCK_LIMIT: std::time::Duration =
                            std::time::Duration::from_secs(20 * 60);
                        tokio::time::sleep(TURN_WALL_CLOCK_LIMIT).await;
                        if !watchdog_token.is_cancelled() {
                            tracing::error!(
                                agent = %watchdog_agent,
                                session = %watchdog_session,
                                limit_s = TURN_WALL_CLOCK_LIMIT.as_secs(),
                                "stuck-turn watchdog: firing cancel_token — turn exceeded \
                                 wall-clock limit; the agent queue must not stay dark"
                            );
                            watchdog_token.cancel();
                        }
                    }
                });
                let result = tokio::select! {
                    biased;
                    // Hard cancel: drops the run_turn future (and every await
                    // it holds — LLM stream, tool calls) the moment the token
                    // fires. This is what frees the single-threaded queue when
                    // a turn is wedged on a non-yielding await.
                    _ = turn_token.cancelled() => Err(anyhow::anyhow!("turn aborted")),
                    r = runtime.run_turn(
                        &session_key,
                        &text,
                        &channel,
                        &peer_id,
                        &chat_id,
                        account.as_deref(),
                        extra_tools,
                        images,
                        files,
                        turn_ctx,
                    ) => r,
                };
                // Turn completed (success/error/cancel) — stop the watchdog.
                // If the watchdog already fired the cancel, the select! above
                // already exited via the cancellation branch; this abort is
                // just hygiene to release the spawned task.
                watchdog.abort();
                // Drop our registered token so a later abort for this session
                // can't cancel a future turn, and the map doesn't leak.
                if registered {
                    if let Ok(mut toks) = handle.cancel_tokens.write() {
                        toks.remove(&session_key);
                    }
                }
                let turn_errored = result.is_err();
                let reply = result.unwrap_or_else(|e| {
                    // A2A consumers key off `outcome` to publish the right
                    // terminal status (Failed vs Canceled). Without this
                    // distinction the A2A reply-watcher saw `Ok(reply)` and
                    // always published Completed — so cancellations and
                    // LLM/tool errors were silently reported as success.
                    //
                    // Both A2A CancelTask and WS chat.abort produce
                    // cancellations from the user; only genuine LLM/tool
                    // failures should map to Error and show
                    // "backend_unavailable" to the user. "turn aborted" is
                    // the WS abort path (agent/runtime.rs:5457), "canceled
                    // by A2A CancelTask" is the A2A path.
                    let err_str = e.to_string();
                    let is_user_cancel = err_str.contains("canceled by A2A CancelTask")
                        || err_str.contains("turn aborted");
                    // A user-initiated cancel is not an error — log at INFO so
                    // it doesn't pollute error dashboards / alerting.
                    if is_user_cancel {
                        info!(agent = %handle.id, "turn canceled by user: {e:#}");
                    } else {
                        error!(agent = %handle.id, "turn error: {e:#}");
                    }
                    let outcome = if is_user_cancel {
                        rsclaw_agent::registry::ReplyOutcome::Canceled
                    } else {
                        rsclaw_agent::registry::ReplyOutcome::Error
                    };
                    // User-facing text: don't leak the raw anyhow Error
                    // (HTTP status codes, internal IDs, JSON error bodies)
                    // to the chat channel — that material is operator-
                    // debug-only and lives in the ERROR log above. The
                    // end user sees an i18n'd "service unavailable" line
                    // for real LLM / transport errors; the original
                    // outcome tag (Error / Canceled) survives so A2A
                    // consumers still key off the terminal status.
                    let i18n_lang = config_for_task
                        .raw
                        .gateway
                        .as_ref()
                        .and_then(|g| g.language.as_deref())
                        .map(rsclaw_i18n::resolve_lang)
                        .unwrap_or("en");
                    let user_text = match outcome {
                        rsclaw_agent::registry::ReplyOutcome::Canceled => "[canceled]".to_owned(),
                        _ => rsclaw_i18n::t("backend_unavailable", i18n_lang),
                    };
                    AgentReply {
                        text: user_text,
                        is_empty: false,
                        tool_calls: None,
                        images: vec![],
                        files: vec![],
                        pending_analysis: None,
                        needs_outer_done_emit: false,
                        outcome,
                    }
                });
                // Emit to event_bus for any reply path that bypassed
                // agent_loop (preparse, file-attach short-circuits, /btw,
                // disk-low, __DIRECT_REPLY__, etc.) *and* for turns that
                // failed with Err (agent_loop returns early via `?` on LLM
                // errors and never gets to emit done — WS clients would hang
                // waiting for the terminator forever). Normal LLM turns
                // already emit deltas + done from inside agent_loop, so a
                // second emit would duplicate the done frame.
                if reply.needs_outer_done_emit || turn_errored {
                    if !reply.text.is_empty() {
                        // receiver may have been dropped
                        let _ = event_tx_task.send(rsclaw_events::AgentEvent {
                            session_id: session_key.clone(),
                            agent_id: handle.id.clone(),
                            delta: reply.text.clone(),
                            done: false,
                            files: vec![],
                            images: vec![],
                            tool_log: vec![],
                            question: None,
                            channel: None,
                        });
                    }
                    // receiver may have been dropped
                    let _ = event_tx_task.send(rsclaw_events::AgentEvent {
                        session_id: session_key.clone(),
                        agent_id: handle.id.clone(),
                        delta: String::new(),
                        done: true,
                        files: vec![],
                        images: vec![],
                        tool_log: vec![],
                        question: None,
                        channel: None,
                    });
                }
                // /goal — completion-driven turn loop. See
                // `src/agent/goal.rs`. After every turn, if the
                // session has an active goal we either:
                //   * append a terminal status (✅/❌/⚠) to the reply
                //     text so the user sees it in the same chat
                //     bubble, AND clear the goal state, OR
                //   * schedule the next iteration via the task queue.
                //
                // Mutating `reply.text` is safe because nothing on the
                // path from here to the channel send re-evaluates the
                // text against goal markers — the hook ran, the
                // decision is made. Submitting the next turn happens
                // INSIDE this match arm (not via reply_tx) because the
                // channel reply path delivers `reply` to the user; the
                // next /goal turn is a separate enqueued message that
                // arrives through the normal worker loop.
                let mut reply = reply;
                if !turn_errored
                    && let Some(reaction) =
                        rsclaw_agent::goal::check_after_turn(&session_key, &reply.text).await
                {
                    use rsclaw_agent::goal::Reaction;
                    match reaction {
                        Reaction::Done(status_line) => {
                            // Strip the GOAL_ marker line itself so it
                            // doesn't read as machine output in the
                            // user's chat — the human-friendly status
                            // line we append takes its place.
                            reply.text = strip_trailing_goal_marker(&reply.text);
                            if !reply.text.is_empty() {
                                reply.text.push_str("\n\n");
                            }
                            reply.text.push_str(&status_line);
                            reply.is_empty = reply.text.is_empty()
                                && reply.images.is_empty()
                                && reply.files.is_empty();
                        }
                        Reaction::Continue(next_prompt) => {
                            // Strip the user-visible reply of any
                            // accidentally-leaked GOAL_* marker
                            // (parse_terminal said Continue, so there
                            // can't be one — but defensive).
                            if let Some(tq) =
                                crate::gateway::task_queue::get_task_queue()
                            {
                                let delivery_channel: &str =
                                    if channel == "ws" { "desktop" } else { &channel };
                                if let Err(e) =
                                    crate::gateway::task_queue::submit_to_queue(
                                        &tq,
                                        &session_key,
                                        &next_prompt,
                                        delivery_channel,
                                        &peer_id,
                                        &peer_id,
                                        false,
                                        crate::gateway::task_queue::Priority::Cron,
                                    )
                                {
                                    warn!(
                                        session = %session_key,
                                        error = %e,
                                        "/goal: failed to enqueue continuation turn"
                                    );
                                }
                            } else {
                                warn!(
                                    "/goal: task_queue not installed; cannot enqueue continuation"
                                );
                            }
                        }
                    }
                }
                // receiver may have been dropped (e.g. channel timeout)
                let _ = reply_tx.send(reply);
            }
            info!(agent_id = %handle.id, "agent runtime task ended (channel closed)");
        });
    }
}

// ---------------------------------------------------------------------------
// Bind address helper
// ---------------------------------------------------------------------------

/// Drop the trailing `GOAL_ACHIEVED` / `GOAL_FAILED ...` line from a
/// reply when it terminated a `/goal` loop. Anything after the marker
/// on the same line is also dropped (per spec the marker must be the
/// last non-blank line). We then trim any leftover trailing blank
/// lines so the appended "✅ Goal achieved" status reads as a clean
/// continuation, not as a margin-floating block.
fn strip_trailing_goal_marker(text: &str) -> String {
    let mut lines: Vec<&str> = text.lines().collect();
    // Find the last non-blank line. If it carries a marker, remove it
    // plus any blank lines that immediately precede it.
    while let Some(last) = lines.last() {
        if last.trim().is_empty() {
            lines.pop();
            continue;
        }
        break;
    }
    if let Some(last) = lines.last() {
        let t = last.trim();
        if t == "GOAL_ACHIEVED" || t.starts_with("GOAL_FAILED") {
            lines.pop();
            // Strip any blank lines now trailing.
            while let Some(last) = lines.last() {
                if last.trim().is_empty() {
                    lines.pop();
                } else {
                    break;
                }
            }
        }
    }
    lines.join("\n")
}

fn resolve_bind_addr(config: &RuntimeConfig) -> SocketAddr {
    let port = config.gateway.port;
    // If a custom bind_address is set, parse and use it.
    if let Some(ref addr) = config.gateway.bind_address {
        if let Ok(ip) = addr.parse::<std::net::IpAddr>() {
            return SocketAddr::new(ip, port);
        }
        tracing::warn!(
            addr = addr.as_str(),
            "invalid bind_address, falling back to bind mode"
        );
    }
    match config.gateway.bind {
        BindMode::Auto | BindMode::Lan => SocketAddr::from(([0, 0, 0, 0], port)),
        BindMode::Loopback => SocketAddr::from(([127, 0, 0, 1], port)),
        BindMode::All => SocketAddr::from(([0, 0, 0, 0], port)),
        BindMode::Custom => SocketAddr::from(([0, 0, 0, 0], port)),
        BindMode::Tailnet => SocketAddr::from(([127, 0, 0, 1], port)),
    }
}

// ---------------------------------------------------------------------------
// MCP server process management
// ---------------------------------------------------------------------------

async fn spawn_mcp_servers(config: &RuntimeConfig, registry: Arc<rsclaw_mcp::McpRegistry>) {
    let mcp = match config.raw.mcp.as_ref() {
        Some(m) => m,
        None => return,
    };

    if mcp.enabled == Some(false) {
        return;
    }

    let servers = match mcp.servers.as_ref() {
        Some(s) => s,
        None => return,
    };

    for server_cfg in servers {
        match rsclaw_mcp::McpClient::spawn(server_cfg).await {
            Ok(mut client) => {
                // Initialize + discover tools.
                if let Err(e) = client.initialize().await {
                    error!(name = %server_cfg.name, error = %e, "MCP initialize failed");
                    continue;
                }
                match client.list_tools().await {
                    Ok(tools) => {
                        info!(
                            name = %server_cfg.name,
                            tools = tools.len(),
                            "MCP server ready"
                        );
                    }
                    Err(e) => {
                        warn!(name = %server_cfg.name, error = %e, "MCP tools/list failed");
                    }
                }
                registry.register(Arc::new(client)).await;
            }
            Err(e) => {
                error!(name = %server_cfg.name, error = %e, "failed to start MCP server");
            }
        }
    }

    let total = registry.clients.lock().await.len();
    if total > 0 {
        info!(count = total, "MCP server(s) registered");
    }
}

// ---------------------------------------------------------------------------
// QQ Official Bot (QQ机器人)
// ---------------------------------------------------------------------------

// ---------------------------------------------------------------------------
// Pending file analysis helper
// ---------------------------------------------------------------------------

/// Process a pending file analysis: send the analysis text to the agent for
/// LLM processing and deliver the result (or timeout/error message) as a
/// follow-up outbound message.
pub(crate) async fn handle_pending_analysis(
    analysis: PendingAnalysis,
    handle: Arc<rsclaw_agent::AgentHandle>,
    out_tx: &mpsc::Sender<rsclaw_channel::OutboundMessage>,
    target_id: String,
    is_group: bool,
    config: &RuntimeConfig,
) {
    let i18n_lang = config
        .raw
        .gateway
        .as_ref()
        .and_then(|g| g.language.as_deref())
        .map(rsclaw_i18n::resolve_lang)
        .unwrap_or("en");

    let (reply_tx, reply_rx) = tokio::sync::oneshot::channel();
    let msg = AgentMessage {
        session_key: analysis.session_key,
        text: analysis.text,
        channel: analysis.channel,
        peer_id: analysis.peer_id.clone(),
        chat_id: String::new(),
        reply_tx,
        task_id: None,
        context_id: None,
        event_tx: None,
        cancel_token: None,
        input_request_tx: None,
        extra_tools: vec![],
        images: vec![],
        files: vec![],
        account: None,
    };
    if handle.tx.send(msg).await.is_err() {
        // receiver may have been dropped
        let _ = out_tx
            .send(rsclaw_channel::OutboundMessage {
                target_id,
                is_group,
                text: rsclaw_i18n::t("analysis_failed", i18n_lang),
                reply_to: None,
                images: vec![],
                channel: None,

                account: None,
                files: vec![],
            })
            .await;
        return;
    }
    match tokio::time::timeout(Duration::from_secs(600), reply_rx).await {
        Ok(Ok(r)) if !r.text.is_empty() || !r.images.is_empty() || !r.files.is_empty() => {
            // receiver may have been dropped
            let _ = out_tx
                .send(rsclaw_channel::OutboundMessage {
                    target_id,
                    is_group,
                    text: r.text,
                    reply_to: None,
                    images: r.images,
                    files: r.files,
                    account: None,
                    channel: None,
                })
                .await;
        }
        Ok(Ok(_)) => {} // empty reply, nothing to send
        Ok(Err(_)) => {
            // receiver may have been dropped
            let _ = out_tx
                .send(rsclaw_channel::OutboundMessage {
                    target_id,
                    is_group,
                    text: rsclaw_i18n::t("analysis_failed", i18n_lang),
                    reply_to: None,
                    images: vec![],
                    channel: None,

                    account: None,
                    files: vec![],
                })
                .await;
        }
        Err(_) => {
            // receiver may have been dropped
            let _ = out_tx
                .send(rsclaw_channel::OutboundMessage {
                    target_id,
                    is_group,
                    text: rsclaw_i18n::t("analysis_timeout", i18n_lang),
                    reply_to: None,
                    images: vec![],
                    channel: None,

                    account: None,
                    files: vec![],
                })
                .await;
        }
    }
}

// ---------------------------------------------------------------------------
// Embedder migration driver — backs the dim-mismatch background re-embed
// kicked off in start_gateway after MemoryStore::open.
// ---------------------------------------------------------------------------

/// Drive a re-embed pass over all docs in `mem_arc` whose stored vector
/// dimension doesn't match the active embedder. Uses the standard two-index
/// `begin_swap` / `swap_apply_batch` / `commit_swap` machinery so reads stay
/// served from primary (empty for the affected docs) and writes dual-write
/// to both indexes throughout. Heavy embedding work happens off-lock; each
/// lock window is bounded to a single batch.
async fn run_embedder_reembed(
    mem_arc: &std::sync::Arc<tokio::sync::Mutex<rsclaw_agent::MemoryStore>>,
) -> anyhow::Result<()> {
    /// Docs embedded per batch. Keeps each lock window to a few hundred ms
    /// even with the slowest CPU-only BGE inference.
    const BATCH: usize = 50;

    let (embedder, expected_total) = {
        let mut mem = mem_arc.lock().await;
        let e = mem.embedder_arc();
        let pending_count = mem.pending_migration_count();
        mem.begin_swap(std::sync::Arc::clone(&e))?;
        (e, pending_count)
    };
    let started = std::time::Instant::now();

    let mut total = 0usize;
    let mut batch_no = 0usize;
    loop {
        let pending = {
            let mem = mem_arc.lock().await;
            mem.swap_pending(BATCH)
        };
        if pending.is_empty() {
            break;
        }
        batch_no += 1;
        // Heavy work off-lock — parallel across the rayon pool so a 100-doc
        // batch finishes in batch_size/num_cores * inference time instead of
        // sequential. BertModel::forward takes `&self`, so concurrent embed
        // calls are safe.
        let batch_started = std::time::Instant::now();
        use rayon::prelude::*;
        let batch: Vec<(usize, Vec<f32>)> = pending
            .into_par_iter()
            .map(|(idx, text)| (idx, embedder.embed(&text)))
            .collect();
        let applied = {
            let mut mem = mem_arc.lock().await;
            match mem.swap_apply_batch(batch) {
                Ok(n) => n,
                Err(e) => {
                    mem.abort_swap();
                    return Err(e);
                }
            }
        };
        // applied == 0 used to abort, but with the swap_apply_batch
        // idempotency guard a concurrent `add` dual-write can legitimately
        // cover the entire batch before we land. Trust `swap_pending` to
        // exclude the now-covered docs on the next iteration; the loop
        // terminates naturally when nothing remains. Cap at 2x expected
        // to make pathological cases (embedder always returning wrong
        // dim) surface as a bounded failure instead of an infinite spin.
        total += applied;
        if batch_no > expected_total.saturating_mul(2).max(64) {
            let mut mem = mem_arc.lock().await;
            mem.abort_swap();
            anyhow::bail!(
                "embedder re-embed: ran {batch_no} batches against {expected_total} expected docs without converging — aborting"
            );
        }
        info!(
            batch = batch_no,
            applied,
            total,
            expected = expected_total,
            batch_ms = batch_started.elapsed().as_millis() as u64,
            "embedder re-embed: batch complete"
        );
    }

    let migrated = {
        let mut mem = mem_arc.lock().await;
        mem.commit_swap()?
    };
    info!(
        total,
        migrated,
        elapsed_secs = started.elapsed().as_secs(),
        "embedder re-embed complete; semantic search now full-coverage"
    );
    Ok(())
}

// ---------------------------------------------------------------------------
// BGE model: validate-or-download with atomic install
// ---------------------------------------------------------------------------

/// Conservative liveness check: does a process with `pid` exist? Used by
/// the staging-dir sweep to clean up after crashed previous runs without
/// disturbing concurrent processes that are mid-download. Errs on the
/// side of "alive" so we never wipe an active stage dir.
fn pid_alive(pid: u32) -> bool {
    #[cfg(unix)]
    {
        // kill(pid, 0) returns 0 if the process exists (any state, incl
        // zombie); ESRCH means no such process. EPERM means it exists but
        // we can't signal it — still alive. Use std's portable errno read
        // (libc::__error is macOS, __errno_location is Linux — std hides
        // the difference).
        let rc = unsafe { libc::kill(pid as libc::pid_t, 0) };
        if rc == 0 {
            return true;
        }
        std::io::Error::last_os_error().raw_os_error() == Some(libc::EPERM)
    }
    #[cfg(windows)]
    {
        // Open a handle with PROCESS_QUERY_LIMITED_INFORMATION — sufficient
        // to test existence. NULL handle == doesn't exist.
        use winapi::um::{
            handleapi::CloseHandle, processthreadsapi::OpenProcess,
            winnt::PROCESS_QUERY_LIMITED_INFORMATION,
        };
        unsafe {
            let h = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, 0, pid);
            if h.is_null() {
                false
            } else {
                CloseHandle(h);
                true
            }
        }
    }
    #[cfg(not(any(unix, windows)))]
    {
        // Unknown platform: assume alive so we never wrongly delete.
        let _ = pid;
        true
    }
}

/// Make sure the BGE model at `model_dir` is present AND loadable. The
/// gateway must not start without semantic search — if validation fails
/// here, the error propagates and the process exits with a clear message.
///
/// Algorithm:
///   1. If `model_dir/model.safetensors` exists → try `LocalBgeEmbedder::load`.
///      Pass: return Ok. Fail: bail (don't auto-delete; might be a user-placed
///      model or upgrade in flight).
///   2. Otherwise, sync-download into
///      `model_dir.with_extension("downloading")/`, validate by attempting to
///      load it, then atomically rename into place.
///   3. Any failure cleans up the tmp dir and bails.
/// Sentinel filename + content schema for "rsclaw owns this model dir".
/// Presence = managed (we may freely wipe / re-download on failure).
/// Absence = user-placed (preserve files; fail loudly, never auto-delete).
/// Body is one `key=value` per line, intentionally readable + grep-able.
const SENTINEL_FILE: &str = ".rsclaw-managed";

fn write_managed_sentinel(model_dir: &std::path::Path, url: &str, bytes: u64) {
    let now_ms = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_millis())
        .unwrap_or(0);
    let body = format!(
        "version={ver}\nurl={url}\nbytes={bytes}\ninstalled_at_ms={now_ms}\n",
        ver = env!("CARGO_PKG_VERSION"),
    );
    if let Err(e) = std::fs::write(model_dir.join(SENTINEL_FILE), body) {
        tracing::warn!(
            error = %e,
            "failed to write {SENTINEL_FILE} — re-download recovery on next start will be disabled"
        );
    }
}

/// Read the `bytes=` field of the sentinel, if present and parseable.
/// Used to detect silent corruption (file size shifted since install).
fn read_sentinel_bytes(model_dir: &std::path::Path) -> Option<u64> {
    let body = std::fs::read_to_string(model_dir.join(SENTINEL_FILE)).ok()?;
    body.lines()
        .find_map(|line| line.strip_prefix("bytes="))
        .and_then(|v| v.trim().parse::<u64>().ok())
}

pub(crate) async fn ensure_bge_model_present(
    model_dir: &std::path::Path,
    search_cfg: Option<&rsclaw_config::schema::MemorySearchConfig>,
) -> anyhow::Result<()> {
    use rsclaw_agent::memory::LocalBgeEmbedder;

    // Wait for an in-progress Tauri seed to finish before deciding whether
    // to download. The Tauri app writes `<model_dir>.seeding.tauri` with its
    // PID while copying the bundled model into place; if that PID is still
    // alive we hold off up to 30s rather than redundantly hitting the CDN.
    // Stale lock files (PID gone) are ignored.
    let seeding_lock = model_dir.with_extension("seeding.tauri");
    if seeding_lock.exists() {
        let lock_pid = std::fs::read_to_string(&seeding_lock)
            .ok()
            .and_then(|s| s.trim().parse::<u32>().ok());
        if let Some(pid) = lock_pid {
            if pid_alive(pid) {
                tracing::info!(
                    pid,
                    lock = %seeding_lock.display(),
                    "BGE model seed in progress (Tauri); waiting up to 30s"
                );
                let deadline = std::time::Instant::now() + Duration::from_secs(30);
                while seeding_lock.exists() && std::time::Instant::now() < deadline {
                    tokio::time::sleep(Duration::from_millis(500)).await;
                    // Re-check liveness — Tauri might have crashed mid-copy.
                    let still_alive = std::fs::read_to_string(&seeding_lock)
                        .ok()
                        .and_then(|s| s.trim().parse::<u32>().ok())
                        .map(pid_alive)
                        .unwrap_or(false);
                    if !still_alive {
                        let _ = std::fs::remove_file(&seeding_lock);
                        break;
                    }
                }
            } else {
                tracing::debug!(stale_pid = pid, "stale seed lock from dead PID, removing");
                let _ = std::fs::remove_file(&seeding_lock);
            }
        } else {
            // Unparseable PID — treat as stale.
            let _ = std::fs::remove_file(&seeding_lock);
        }
    }

    let weights_path = model_dir.join("model.safetensors");
    if weights_path.exists() {
        let weights_bytes = std::fs::metadata(&weights_path).map(|m| m.len()).ok();
        let dir_is_managed = model_dir.join(SENTINEL_FILE).exists();

        // Sentinel + size-mismatch = silent corruption (truncated file from a
        // crash mid-extract, partial write to a full disk, etc.). For OUR
        // installs we can safely auto-recover by deleting and re-downloading.
        if dir_is_managed {
            if let (Some(actual), Some(expected)) = (weights_bytes, read_sentinel_bytes(model_dir))
            {
                if actual != expected {
                    tracing::warn!(
                        actual,
                        expected,
                        "BGE model.safetensors size differs from sentinel; re-downloading"
                    );
                    let _ = std::fs::remove_dir_all(model_dir);
                    // Fall through to download path below.
                }
            }
        }

        // If we still have files (size matched OR not managed), try loading.
        if model_dir.join("model.safetensors").exists() {
            match LocalBgeEmbedder::load(model_dir) {
                Ok(_) => return Ok(()),
                Err(e) if dir_is_managed => {
                    tracing::warn!(
                        error = %format!("{e:#}"),
                        dir = %model_dir.display(),
                        "managed BGE model failed to load; auto-recovering"
                    );
                    let _ = std::fs::remove_dir_all(model_dir);
                    // Fall through to download path.
                }
                Err(e) => {
                    anyhow::bail!(
                        "BGE model at {} failed to load: {e:#}\n\
                         This is a user-placed directory (no {SENTINEL_FILE} sentinel) — \
                         fix the files or remove the directory to trigger a fresh \
                         download, then restart.",
                        model_dir.display()
                    );
                }
            }
        }
    }

    let local_cfg = search_cfg.and_then(|c| c.local.as_ref());
    let url = local_cfg
        .and_then(|c| c.model_download_url.as_deref())
        .unwrap_or("https://gitfast.org/tools/models/bge-small-zh-v1.5.zip")
        .to_owned();

    // Per-PID staging directory. Two concurrent gateway processes hitting the
    // same model_dir (e.g. a fast `gateway restart` overlap or two CLIs) must
    // not write into the same staging dir — one would overwrite the other's
    // half-extracted state and the load-test would see Frankenstein files.
    // Each process gets its own stage; whichever finishes first atomic-renames
    // into model_dir, the other's load-test passes against the now-existing
    // files (same model URL → same content) or its rename overwrites cleanly.
    //
    // Resume semantics: download_resumable's sidecar `.meta` lives next to the
    // archive in the per-PID dir, so resume only kicks in if the SAME process
    // restarts. Across-process resume isn't worth the file-locking complexity
    // for a one-shot bootstrap.
    let pid = std::process::id();
    let tmp_dir = model_dir.with_extension(format!("downloading.pid{pid}"));
    // Sweep stale per-PID staging dirs from crashed previous runs (any dir
    // matching `<basename>.downloading.pid*` whose pid is no longer alive).
    if let Some(parent) = model_dir.parent() {
        let prefix = model_dir
            .file_name()
            .and_then(|n| n.to_str())
            .map(|n| format!("{n}.downloading.pid"))
            .unwrap_or_default();
        if !prefix.is_empty()
            && let Ok(entries) = std::fs::read_dir(parent)
        {
            for entry in entries.flatten() {
                let p = entry.path();
                if let Some(name) = p.file_name().and_then(|n| n.to_str())
                    && let Some(other_pid_str) = name.strip_prefix(&prefix)
                    && let Ok(other_pid) = other_pid_str.parse::<u32>()
                    && other_pid != pid
                    && !pid_alive(other_pid)
                {
                    tracing::info!(stale = %p.display(), "sweeping stale staging dir");
                    let _ = std::fs::remove_dir_all(&p);
                }
            }
        }
    }
    std::fs::create_dir_all(&tmp_dir)
        .with_context(|| format!("failed to create download dir {}", tmp_dir.display()))?;

    let archive_name = url.rsplit('/').next().unwrap_or("bge-model.zip");
    let archive_path = tmp_dir.join(archive_name);

    info!(
        "BGE model not present; downloading from {url} -> {}",
        archive_path.display()
    );
    let client = reqwest::Client::new();
    let download_result =
        crate::cmd::tools::download_resumable(&client, &url, &archive_path, "BGE model").await;
    if let Err(e) = download_result {
        // Leave the partial archive in place so the next run resumes from
        // the same byte. No clean-up here.
        anyhow::bail!(
            "BGE model download failed: {e:#}\n\
             URL: {url}\n\
             Partial download retained at {} for resume on next start.\n\
             Or manually place model files at {} and restart.",
            archive_path.display(),
            model_dir.display()
        );
    }

    // Wipe any stale extracted files from a prior failed run before extracting
    // fresh.
    for entry in std::fs::read_dir(&tmp_dir)?.flatten() {
        let p = entry.path();
        if p == archive_path {
            continue;
        }
        if p.is_dir() {
            let _ = std::fs::remove_dir_all(&p);
        } else {
            let _ = std::fs::remove_file(&p);
        }
    }
    if let Err(e) = crate::cmd::tools::extract_zip_public(&archive_path, &tmp_dir) {
        let _ = std::fs::remove_dir_all(&tmp_dir);
        anyhow::bail!(
            "BGE model archive extraction failed: {e:#}\n\
             The downloaded file at {} may be corrupted. Re-run after deleting it.",
            archive_path.display()
        );
    }

    // Load-test before commit — this is our only completeness guarantee.
    if let Err(e) = LocalBgeEmbedder::load(&tmp_dir) {
        let _ = std::fs::remove_dir_all(&tmp_dir);
        anyhow::bail!(
            "downloaded BGE model failed validation: {e:#}\n\
             The download may have been corrupted. Retry by restarting; if this\n\
             persists, the upstream model URL may be broken: {url}"
        );
    }

    // Drop the archive — only the extracted files matter from here on.
    let _ = std::fs::remove_file(&archive_path);

    // Install. The presence of `SENTINEL_FILE` in the existing model_dir
    // tells us "rsclaw owns this dir" (managed) — we may freely wipe and
    // rename. Without the sentinel we treat the dir as user-managed and
    // copy-merge to preserve hand-placed files (different config.json,
    // partial transfer in progress, etc.). model.safetensors is always
    // overwritten when it's our turn to install — its mismatch is what
    // brought us into this branch in the first place.
    let dir_is_managed = model_dir.exists() && model_dir.join(SENTINEL_FILE).exists();
    if !model_dir.exists() || dir_is_managed {
        if model_dir.exists() {
            std::fs::remove_dir_all(model_dir)
                .with_context(|| format!("failed to clear managed dir {}", model_dir.display()))?;
        }
        std::fs::rename(&tmp_dir, model_dir).with_context(|| {
            format!(
                "failed to install model: rename {} -> {}",
                tmp_dir.display(),
                model_dir.display()
            )
        })?;
    } else {
        std::fs::create_dir_all(model_dir)
            .with_context(|| format!("failed to ensure install dir {}", model_dir.display()))?;
        for entry in std::fs::read_dir(&tmp_dir)?.flatten() {
            let src = entry.path();
            let Some(name) = src.file_name() else {
                continue;
            };
            let dst = model_dir.join(name);
            if dst.exists() && name != "model.safetensors" {
                tracing::debug!(file = %dst.display(), "preserving user-placed file");
                continue;
            }
            if let Err(e) = std::fs::rename(&src, &dst) {
                // Cross-device rename can fail on overlay filesystems; fall
                // back to copy + remove.
                std::fs::copy(&src, &dst).with_context(|| {
                    format!(
                        "failed to install {} -> {}: rename {e}",
                        src.display(),
                        dst.display()
                    )
                })?;
                let _ = std::fs::remove_file(&src);
            }
        }
        let _ = std::fs::remove_dir_all(&tmp_dir);
    }

    // Stamp the sentinel so a future restart can size-check the install and
    // safely auto-recover from corruption / truncation.
    let installed_bytes = std::fs::metadata(model_dir.join("model.safetensors"))
        .map(|m| m.len())
        .unwrap_or(0);
    write_managed_sentinel(model_dir, &url, installed_bytes);

    info!("BGE model installed at {}", model_dir.display());
    Ok(())
}

/// Publish a `RestartRequest` into the broadcast channel and store it in the
/// `pending_restart` latch so late-connecting UI clients see it on handshake.
///
/// `send` failure (no live subscribers) is normal and ignored — the latch
/// guarantees the next subscriber will pick it up.
///
/// Stamps the request with the current `shutdown.inflight()` count so the UI
/// can decide whether to restart immediately (idle) or show the countdown
/// banner (busy). When the initial count is non-zero, spawns a watcher that
/// re-publishes (latch + broadcast) with `inflight = 0` as soon as the
/// gateway drains, capped at 60s. The frontend treats `inflight = 0` as
/// "ready to restart now" and short-circuits its countdown.
pub(crate) fn publish_restart(
    tx: &tokio::sync::broadcast::Sender<rsclaw_events::RestartRequest>,
    latch: &Arc<std::sync::RwLock<Option<rsclaw_events::RestartRequest>>>,
    shutdown: &crate::gateway::ShutdownCoordinator,
    mut req: rsclaw_events::RestartRequest,
) {
    let initial = shutdown.inflight() as u64;
    req.inflight = initial;

    if let Ok(mut guard) = latch.write() {
        *guard = Some(req.clone());
    } else {
        warn!("pending_restart lock poisoned; restart event still broadcast");
    }
    let _ = tx.send(req.clone());

    if initial == 0 {
        return;
    }

    // Busy at publish time: poll until idle (or 60s deadline) and re-publish
    // with inflight = 0 so the UI restarts immediately.
    let tx = tx.clone();
    let latch = Arc::clone(latch);
    let shutdown = shutdown.clone();
    tokio::spawn(async move {
        let deadline = std::time::Instant::now() + Duration::from_secs(60);
        loop {
            tokio::time::sleep(Duration::from_millis(200)).await;
            if shutdown.inflight() == 0 {
                let mut updated = req;
                updated.inflight = 0;
                if let Ok(mut guard) = latch.write() {
                    *guard = Some(updated.clone());
                }
                let _ = tx.send(updated);
                return;
            }
            if std::time::Instant::now() >= deadline {
                return;
            }
        }
    });
}

#[cfg(test)]
mod user_env_tests {
    use std::collections::HashMap;

    use super::*;

    #[test]
    fn sets_unset_variables() {
        unsafe { std::env::remove_var("RSCLAW_TEST_USER_ENV_NEW") };
        let mut map = HashMap::new();
        map.insert(
            "RSCLAW_TEST_USER_ENV_NEW".to_owned(),
            "from-config".to_owned(),
        );
        apply_user_env_map(&map);
        assert_eq!(
            std::env::var("RSCLAW_TEST_USER_ENV_NEW").as_deref(),
            Ok("from-config")
        );
        unsafe { std::env::remove_var("RSCLAW_TEST_USER_ENV_NEW") };
    }

    #[test]
    fn preexisting_shell_value_wins() {
        unsafe { std::env::set_var("RSCLAW_TEST_USER_ENV_KEEP", "from-shell") };
        let mut map = HashMap::new();
        map.insert(
            "RSCLAW_TEST_USER_ENV_KEEP".to_owned(),
            "from-config".to_owned(),
        );
        apply_user_env_map(&map);
        assert_eq!(
            std::env::var("RSCLAW_TEST_USER_ENV_KEEP").as_deref(),
            Ok("from-shell"),
            "shell-provided value must not be overwritten"
        );
        unsafe { std::env::remove_var("RSCLAW_TEST_USER_ENV_KEEP") };
    }

    #[test]
    fn expands_nested_var_refs() {
        unsafe { std::env::set_var("RSCLAW_TEST_USER_ENV_REF", "/var/log/foo") };
        unsafe { std::env::remove_var("RSCLAW_TEST_USER_ENV_TARGET") };
        let mut map = HashMap::new();
        map.insert(
            "RSCLAW_TEST_USER_ENV_TARGET".to_owned(),
            "${RSCLAW_TEST_USER_ENV_REF}/app.log".to_owned(),
        );
        apply_user_env_map(&map);
        assert_eq!(
            std::env::var("RSCLAW_TEST_USER_ENV_TARGET").as_deref(),
            Ok("/var/log/foo/app.log")
        );
        unsafe { std::env::remove_var("RSCLAW_TEST_USER_ENV_REF") };
        unsafe { std::env::remove_var("RSCLAW_TEST_USER_ENV_TARGET") };
    }
}