codetether-agent 4.5.2

A2A-native AI coding agent for the CodeTether ecosystem
Documentation
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
//! Parallel execution engine for swarm operations
//!
//! Executes subtasks in parallel across multiple sub-agents,
//! respecting dependencies and optimizing for critical path.

use super::{
    BranchObservation, BranchRuntimeState, CacheConfig, CacheStats, CollapseController,
    CollapsePolicy, DecompositionStrategy, ExecutionMode, StageStats, SwarmCache, SwarmConfig,
    SwarmResult,
    kubernetes_executor::{
        RemoteSubtaskPayload, SWARM_SUBTASK_PAYLOAD_ENV, encode_payload, latest_probe_from_logs,
        probe_changed_files_set, result_from_logs,
    },
    orchestrator::Orchestrator,
    result_store::ResultStore,
    subtask::{SubTask, SubTaskResult, SubTaskStatus},
};
use crate::bus::{AgentBus, BusMessage};
use crate::k8s::{K8sManager, SubagentPodSpec, SubagentPodState};
use crate::tui::swarm_view::{AgentMessageEntry, AgentToolCallDetail, SubTaskInfo, SwarmEvent};

// Re-export swarm types for convenience
pub use super::SwarmMessage;
use crate::{
    agent::Agent,
    provenance::{ExecutionOrigin, ExecutionProvenance},
    provider::{CompletionRequest, ContentPart, FinishReason, Message, Provider, Role},
    rlm::RlmExecutor,
    session::helper::runtime::enrich_tool_input_with_runtime_context,
    swarm::{SwarmArtifact, SwarmStats},
    telemetry::SwarmTelemetryCollector,
    tool::ToolRegistry,
    worktree::{WorktreeInfo, WorktreeManager},
};
use anyhow::Result;
use futures::stream::{FuturesUnordered, StreamExt};
use std::collections::{HashMap, VecDeque};
use std::sync::Arc;
use std::time::Instant;
use tokio::sync::{RwLock, mpsc};
use tokio::task::AbortHandle;
use tokio::time::{Duration, MissedTickBehavior, timeout};

/// Default context limit (256k tokens - conservative for most models)
const DEFAULT_CONTEXT_LIMIT: usize = 256_000;

/// Reserve tokens for the response generation
const RESPONSE_RESERVE_TOKENS: usize = 8_192;

/// Safety margin before we start truncating (90% of limit)
const TRUNCATION_THRESHOLD: f64 = 0.85;

/// Estimate token count from text (rough heuristic: ~4 chars per token)
fn estimate_tokens(text: &str) -> usize {
    // This is a rough estimate - actual tokenization varies by model
    // Most tokenizers average 3-5 chars per token for English text
    // We use 3.5 to be conservative
    (text.len() as f64 / 3.5).ceil() as usize
}

/// Estimate total tokens in a message
fn estimate_message_tokens(message: &Message) -> usize {
    let mut tokens = 4; // Role overhead

    for part in &message.content {
        tokens += match part {
            ContentPart::Text { text } => estimate_tokens(text),
            ContentPart::ToolCall {
                id,
                name,
                arguments,
                ..
            } => estimate_tokens(id) + estimate_tokens(name) + estimate_tokens(arguments) + 10,
            ContentPart::ToolResult {
                tool_call_id,
                content,
            } => estimate_tokens(tool_call_id) + estimate_tokens(content) + 6,
            ContentPart::Image { .. } | ContentPart::File { .. } => 2000, // Binary content is expensive
            ContentPart::Thinking { text } => estimate_tokens(text),
        };
    }

    tokens
}

/// Estimate total tokens in all messages
fn estimate_total_tokens(messages: &[Message]) -> usize {
    messages.iter().map(estimate_message_tokens).sum()
}

/// Truncate messages to fit within context limit
///
/// Strategy:
/// 1. First, aggressively truncate large tool results
/// 2. Keep system message (first) and user message (second) always
/// 3. Keep the most recent assistant + tool result pairs together
/// 4. Drop oldest middle messages in matched pairs
fn truncate_messages_to_fit(messages: &mut Vec<Message>, context_limit: usize) {
    let target_tokens =
        ((context_limit as f64) * TRUNCATION_THRESHOLD) as usize - RESPONSE_RESERVE_TOKENS;

    let current_tokens = estimate_total_tokens(messages);
    if current_tokens <= target_tokens {
        return;
    }

    tracing::warn!(
        current_tokens = current_tokens,
        target_tokens = target_tokens,
        context_limit = context_limit,
        "Context approaching limit, truncating conversation history"
    );

    // FIRST: Aggressively truncate large tool results (this is the main culprit)
    truncate_large_tool_results(messages, 2000); // Max 2k tokens per tool result

    let after_tool_truncation = estimate_total_tokens(messages);
    if after_tool_truncation <= target_tokens {
        tracing::info!(
            old_tokens = current_tokens,
            new_tokens = after_tool_truncation,
            "Truncated large tool results, context now within limits"
        );
        return;
    }

    // Minimum: keep first 2 (system + initial user) and last 4 messages
    if messages.len() <= 6 {
        tracing::warn!(
            tokens = after_tool_truncation,
            target = target_tokens,
            "Cannot truncate further - conversation too short"
        );
        return;
    }

    // Remove messages from the middle, keeping first 2 and last 4
    // But we need to remove in pairs to maintain tool_call_id references
    let keep_start = 2;
    let keep_end = 4;
    let removable_count = messages.len() - keep_start - keep_end;

    if removable_count == 0 {
        return;
    }

    // Remove all middle messages
    let removed_messages: Vec<_> = messages
        .drain(keep_start..keep_start + removable_count)
        .collect();
    let summary = summarize_removed_messages(&removed_messages);

    // Insert a summary message where we removed content
    messages.insert(
        keep_start,
        Message {
            role: Role::User,
            content: vec![ContentPart::Text {
                text: format!(
                    "[Context truncated: {} earlier messages removed to fit context window]\n{}",
                    removed_messages.len(),
                    summary
                ),
            }],
        },
    );

    let new_tokens = estimate_total_tokens(messages);
    tracing::info!(
        removed_messages = removed_messages.len(),
        old_tokens = current_tokens,
        new_tokens = new_tokens,
        "Truncated conversation history"
    );
}

/// Summarize removed messages for context preservation
fn summarize_removed_messages(messages: &[Message]) -> String {
    let mut summary = String::new();
    let mut tool_calls: Vec<String> = Vec::new();

    for msg in messages {
        for part in &msg.content {
            if let ContentPart::ToolCall { name, .. } = part
                && !tool_calls.contains(name)
            {
                tool_calls.push(name.clone());
            }
        }
    }

    if !tool_calls.is_empty() {
        summary.push_str(&format!(
            "Tools used in truncated history: {}",
            tool_calls.join(", ")
        ));
    }

    summary
}

/// Truncate large tool results that exceed a reasonable size
fn truncate_large_tool_results(messages: &mut [Message], max_tokens_per_result: usize) {
    let char_limit = max_tokens_per_result * 3; // ~3 chars per token
    let mut truncated_count = 0;
    let mut saved_tokens = 0usize;

    for message in messages.iter_mut() {
        for part in message.content.iter_mut() {
            if let ContentPart::ToolResult { content, .. } = part {
                let tokens = estimate_tokens(content);
                if tokens > max_tokens_per_result {
                    let old_len = content.len();
                    *content = truncate_single_result(content, char_limit);
                    saved_tokens += tokens.saturating_sub(estimate_tokens(content));
                    if content.len() < old_len {
                        truncated_count += 1;
                    }
                }
            }
        }
    }

    if truncated_count > 0 {
        tracing::info!(
            truncated_count = truncated_count,
            saved_tokens = saved_tokens,
            max_tokens_per_result = max_tokens_per_result,
            "Truncated large tool results"
        );
    }
}

/// Truncate a single result string to a maximum character limit
fn truncate_single_result(content: &str, max_chars: usize) -> String {
    if content.len() <= max_chars {
        return content.to_string();
    }

    // Find a valid char boundary at or before max_chars
    let safe_limit = {
        let mut limit = max_chars.min(content.len());
        while limit > 0 && !content.is_char_boundary(limit) {
            limit -= 1;
        }
        limit
    };

    // Try to find a good break point (newline) near the limit
    let break_point = content[..safe_limit].rfind('\n').unwrap_or(safe_limit);

    let truncated = format!(
        "{}...\n\n[OUTPUT TRUNCATED: {}{} chars to fit context limit]",
        &content[..break_point],
        content.len(),
        break_point
    );

    tracing::debug!(
        original_len = content.len(),
        truncated_len = truncated.len(),
        "Truncated large result"
    );

    truncated
}

/// Threshold for when to use RLM vs truncation (in characters)
const RLM_THRESHOLD_CHARS: usize = 50_000;

/// Max chars for simple truncation (below RLM threshold)
const SIMPLE_TRUNCATE_CHARS: usize = 6000;

/// Collapse controller sampling interval for branch health.
const COLLAPSE_SAMPLE_SECS: u64 = 5;
const SWARM_FALLBACK_PROMPT_ENV: &str = "CODETETHER_SWARM_FALLBACK_PROMPT";
const SWARM_FALLBACK_MODEL_ENV: &str = "CODETETHER_SWARM_FALLBACK_MODEL";
const K8S_PASSTHROUGH_ENV_VARS: &[&str] = &[
    "VAULT_ADDR",
    "VAULT_TOKEN",
    "VAULT_MOUNT",
    "VAULT_SECRETS_PATH",
    "VAULT_NAMESPACE",
    "CODETETHER_AUTH_TOKEN",
];

#[derive(Debug, Clone)]
struct ActiveK8sBranch {
    branch: String,
    started_at: Instant,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AgentLoopExit {
    Completed,
    MaxStepsReached,
    TimedOut,
}

/// Calculate the delay for exponential backoff
///
/// Uses the formula: min(initial_delay * multiplier^attempt, max_delay)
fn calculate_backoff_delay(
    attempt: u32,
    initial_delay_ms: u64,
    max_delay_ms: u64,
    multiplier: f64,
) -> Duration {
    let delay_ms =
        (initial_delay_ms as f64 * multiplier.powi(attempt as i32)).min(max_delay_ms as f64);
    Duration::from_millis(delay_ms as u64)
}

fn compute_resource_health(pod_state: Option<&SubagentPodState>) -> (f32, u32) {
    let Some(pod_state) = pod_state else {
        return (0.2, 1);
    };

    let reason = pod_state
        .reason
        .as_deref()
        .unwrap_or_default()
        .to_ascii_lowercase();
    let phase = pod_state.phase.to_ascii_lowercase();

    if reason.contains("oomkilled") {
        return (0.0, 3);
    }
    if reason.contains("imagepullbackoff") || reason.contains("errimagepull") {
        return (0.0, 3);
    }
    if reason.contains("crashloopbackoff") {
        return (0.1, 2);
    }
    if phase == "failed" {
        return (0.1, 2);
    }

    let mut score = 1.0f32;
    let mut unhealthy_signals = 0u32;

    if !pod_state.ready {
        score -= 0.2;
    }
    if !reason.is_empty() {
        score -= 0.3;
        unhealthy_signals += 1;
    }
    if pod_state.restart_count > 0 {
        score -= (pod_state.restart_count.min(3) as f32) * 0.2;
        unhealthy_signals += 1;
    }

    (score.clamp(0.0, 1.0), unhealthy_signals)
}

/// Process a large tool result using RLM to intelligently summarize it
async fn process_large_result_with_rlm(
    content: &str,
    tool_name: &str,
    provider: Arc<dyn Provider>,
    model: &str,
) -> String {
    if content.len() <= SIMPLE_TRUNCATE_CHARS {
        return content.to_string();
    }

    // For medium-sized content, just truncate
    if content.len() <= RLM_THRESHOLD_CHARS {
        return truncate_single_result(content, SIMPLE_TRUNCATE_CHARS);
    }

    // For very large content, use RLM to intelligently summarize
    tracing::info!(
        tool = %tool_name,
        content_len = content.len(),
        "Using RLM to process large tool result"
    );

    let query = format!(
        "Summarize the key information from this {} output. \
         Focus on: errors, warnings, important findings, and actionable items. \
         Be concise but thorough.",
        tool_name
    );

    let mut executor =
        RlmExecutor::new(content.to_string(), provider, model.to_string()).with_max_iterations(3);

    match executor.analyze(&query).await {
        Ok(result) => {
            let bounded_answer = truncate_single_result(&result.answer, SIMPLE_TRUNCATE_CHARS * 2);
            tracing::info!(
                tool = %tool_name,
                original_len = content.len(),
                summary_len = bounded_answer.len(),
                iterations = result.iterations,
                "RLM summarized large result"
            );

            format!(
                "[RLM Summary of {} output ({} chars → {} chars)]\n\n{}",
                tool_name,
                content.len(),
                bounded_answer.len(),
                bounded_answer
            )
        }
        Err(e) => {
            tracing::warn!(
                tool = %tool_name,
                error = %e,
                "RLM analysis failed, falling back to truncation"
            );
            truncate_single_result(content, SIMPLE_TRUNCATE_CHARS)
        }
    }
}

/// The swarm executor runs subtasks in parallel
pub struct SwarmExecutor {
    config: SwarmConfig,
    /// Optional agent for handling swarm-level coordination (reserved for future use)
    coordinator_agent: Option<Arc<tokio::sync::Mutex<Agent>>>,
    /// Optional event channel for TUI real-time updates
    event_tx: Option<mpsc::Sender<SwarmEvent>>,
    /// Telemetry collector for swarm execution metrics
    telemetry: Arc<tokio::sync::Mutex<SwarmTelemetryCollector>>,
    /// Cache for avoiding duplicate subtask execution
    cache: Option<Arc<tokio::sync::Mutex<SwarmCache>>>,
    /// Shared result store for sub-agent result sharing
    result_store: Arc<ResultStore>,
    /// Optional agent bus for inter-agent communication
    bus: Option<Arc<AgentBus>>,
}

impl SwarmExecutor {
    /// Create a new executor
    pub fn new(config: SwarmConfig) -> Self {
        Self {
            config,
            coordinator_agent: None,
            event_tx: None,
            telemetry: Arc::new(tokio::sync::Mutex::new(SwarmTelemetryCollector::default())),
            cache: None,
            result_store: ResultStore::new_arc(),
            bus: None,
        }
    }

    /// Create a new executor with caching enabled
    pub async fn with_cache(config: SwarmConfig, cache_config: CacheConfig) -> Result<Self> {
        let cache = SwarmCache::new(cache_config).await?;
        Ok(Self {
            config,
            coordinator_agent: None,
            event_tx: None,
            telemetry: Arc::new(tokio::sync::Mutex::new(SwarmTelemetryCollector::default())),
            cache: Some(Arc::new(tokio::sync::Mutex::new(cache))),
            result_store: ResultStore::new_arc(),
            bus: None,
        })
    }

    /// Set a pre-initialized cache
    pub fn with_cache_instance(mut self, cache: Arc<tokio::sync::Mutex<SwarmCache>>) -> Self {
        self.cache = Some(cache);
        self
    }

    /// Set an agent bus for inter-agent communication
    pub fn with_bus(mut self, bus: Arc<AgentBus>) -> Self {
        self.bus = Some(bus);
        self
    }

    /// Get the agent bus if set
    pub fn bus(&self) -> Option<&Arc<AgentBus>> {
        self.bus.as_ref()
    }

    /// Set an event channel for real-time TUI updates
    pub fn with_event_tx(mut self, tx: mpsc::Sender<SwarmEvent>) -> Self {
        self.event_tx = Some(tx);
        self
    }

    /// Set a coordinator agent for swarm-level coordination
    pub fn with_coordinator_agent(mut self, agent: Arc<tokio::sync::Mutex<Agent>>) -> Self {
        tracing::debug!("Setting coordinator agent for swarm execution");
        self.coordinator_agent = Some(agent);
        self
    }

    /// Set a telemetry collector for swarm execution metrics
    pub fn with_telemetry(
        mut self,
        telemetry: Arc<tokio::sync::Mutex<SwarmTelemetryCollector>>,
    ) -> Self {
        self.telemetry = telemetry;
        self
    }

    /// Get the telemetry collector as an Arc
    pub fn telemetry_arc(&self) -> Arc<tokio::sync::Mutex<SwarmTelemetryCollector>> {
        Arc::clone(&self.telemetry)
    }
    /// Get the coordinator agent if set
    pub fn coordinator_agent(&self) -> Option<&Arc<tokio::sync::Mutex<Agent>>> {
        tracing::debug!(
            has_coordinator = self.coordinator_agent.is_some(),
            "Getting coordinator agent"
        );
        self.coordinator_agent.as_ref()
    }

    /// Get the shared result store
    pub fn result_store(&self) -> &Arc<ResultStore> {
        &self.result_store
    }

    /// Get cache statistics if caching is enabled
    pub async fn cache_stats(&self) -> Option<CacheStats> {
        if let Some(ref cache) = self.cache {
            let cache_guard = cache.lock().await;
            Some(cache_guard.stats().clone())
        } else {
            None
        }
    }

    /// Clear the cache if enabled
    pub async fn clear_cache(&self) -> Result<()> {
        if let Some(ref cache) = self.cache {
            let mut cache_guard = cache.lock().await;
            cache_guard.clear().await?;
        }
        Ok(())
    }

    /// Get the retry configuration
    pub fn retry_config(&self) -> (u32, u64, u64, f64) {
        (
            self.config.max_retries,
            self.config.base_delay_ms,
            self.config.max_delay_ms,
            2.0, // backoff multiplier
        )
    }

    /// Check if retries are enabled
    pub fn retries_enabled(&self) -> bool {
        self.config.max_retries > 0
    }

    /// Send an event to the TUI if channel is connected (non-blocking)
    fn try_send_event(&self, event: SwarmEvent) {
        // Also emit on the agent bus if connected
        if let Some(ref bus) = self.bus {
            let handle = bus.handle("swarm-executor");
            match &event {
                SwarmEvent::Started { task, .. } => {
                    handle.send(
                        "broadcast",
                        BusMessage::AgentReady {
                            agent_id: "swarm-executor".to_string(),
                            capabilities: vec![format!("executing:{task}")],
                        },
                    );
                }
                SwarmEvent::Complete { success, .. } => {
                    let state = if *success {
                        crate::a2a::types::TaskState::Completed
                    } else {
                        crate::a2a::types::TaskState::Failed
                    };
                    handle.send_task_update("swarm", state, None);
                }
                _ => {} // Other events are TUI-specific
            }
        }

        if let Some(ref tx) = self.event_tx {
            let _ = tx.try_send(event);
        }
    }

    /// Execute a task using the swarm
    pub async fn execute(
        &self,
        task: &str,
        strategy: DecompositionStrategy,
    ) -> Result<SwarmResult> {
        let start_time = Instant::now();

        // Create orchestrator
        let mut orchestrator = Orchestrator::new(self.config.clone()).await?;

        tracing::info!(provider_name = %orchestrator.provider(), "Starting swarm execution for task");

        // Decompose the task
        let subtasks = orchestrator.decompose(task, strategy).await?;

        if subtasks.is_empty() {
            self.try_send_event(SwarmEvent::Error("No subtasks generated".to_string()));
            return Ok(SwarmResult {
                success: false,
                result: String::new(),
                subtask_results: Vec::new(),
                stats: SwarmStats::default(),
                artifacts: Vec::new(),
                error: Some("No subtasks generated".to_string()),
            });
        }

        tracing::info!(provider_name = %orchestrator.provider(), "Task decomposed into {} subtasks", subtasks.len());

        self.try_send_event(SwarmEvent::Started {
            task: task.to_string(),
            total_subtasks: subtasks.len(),
        });

        // Emit decomposition event for TUI
        self.try_send_event(SwarmEvent::Decomposed {
            subtasks: subtasks
                .iter()
                .map(|s| SubTaskInfo {
                    id: s.id.clone(),
                    name: s.name.clone(),
                    status: SubTaskStatus::Pending,
                    stage: s.stage,
                    dependencies: s.dependencies.clone(),
                    agent_name: s.specialty.clone(),
                    current_tool: None,
                    steps: 0,
                    max_steps: self.config.max_steps_per_subagent,
                    tool_call_history: Vec::new(),
                    messages: Vec::new(),
                    output: None,
                    error: None,
                })
                .collect(),
        });

        // Execute stages in order
        let max_stage = subtasks.iter().map(|s| s.stage).max().unwrap_or(0);
        let mut all_results: Vec<SubTaskResult> = Vec::new();
        let artifacts: Vec<SwarmArtifact> = Vec::new();

        // Initialize telemetry for this swarm execution
        let swarm_id = uuid::Uuid::new_v4().to_string();
        let strategy_str = format!("{:?}", strategy);
        self.telemetry
            .lock()
            .await
            .start_swarm(&swarm_id, subtasks.len(), &strategy_str)
            .await;

        // Shared state for completed results
        let completed_results: Arc<RwLock<HashMap<String, String>>> =
            Arc::new(RwLock::new(HashMap::new()));

        for stage in 0..=max_stage {
            let stage_start = Instant::now();

            let stage_subtasks: Vec<SubTask> = orchestrator
                .subtasks_for_stage(stage)
                .into_iter()
                .cloned()
                .collect();

            tracing::debug!(
                "Stage {} has {} subtasks (max_stage={})",
                stage,
                stage_subtasks.len(),
                max_stage
            );

            if stage_subtasks.is_empty() {
                continue;
            }

            tracing::info!(
                provider_name = %orchestrator.provider(),
                "Executing stage {} with {} subtasks",
                stage,
                stage_subtasks.len()
            );

            // Execute all subtasks in this stage in parallel
            let stage_results = self
                .execute_stage(
                    &orchestrator,
                    stage_subtasks,
                    completed_results.clone(),
                    &swarm_id,
                )
                .await?;

            // Update completed results for next stage
            {
                let mut completed = completed_results.write().await;
                for result in &stage_results {
                    completed.insert(result.subtask_id.clone(), result.result.clone());
                    // Also publish to shared ResultStore for richer querying
                    let tags = vec![
                        format!("stage:{stage}"),
                        format!("subtask:{}", result.subtask_id),
                    ];
                    let _ = self
                        .result_store
                        .publish(
                            &result.subtask_id,
                            &result.subagent_id,
                            &result.result,
                            tags,
                            None,
                        )
                        .await;
                }
            }

            // Update orchestrator stats
            let stage_time = stage_start.elapsed().as_millis() as u64;
            let max_steps = stage_results.iter().map(|r| r.steps).max().unwrap_or(0);
            let total_steps: usize = stage_results.iter().map(|r| r.steps).sum();

            orchestrator.stats_mut().stages.push(StageStats {
                stage,
                subagent_count: stage_results.len(),
                max_steps,
                total_steps,
                execution_time_ms: stage_time,
            });

            // Mark subtasks as completed
            for result in &stage_results {
                orchestrator.complete_subtask(&result.subtask_id, result.clone());
            }

            // Emit stage complete event
            let stage_completed = stage_results.iter().filter(|r| r.success).count();
            let stage_failed = stage_results.iter().filter(|r| !r.success).count();
            self.try_send_event(SwarmEvent::StageComplete {
                stage,
                completed: stage_completed,
                failed: stage_failed,
            });

            all_results.extend(stage_results);
        }

        // Get provider name before mutable borrow
        let provider_name = orchestrator.provider().to_string();

        // Record overall execution latency
        self.telemetry
            .lock()
            .await
            .record_swarm_latency("total_execution", start_time.elapsed())
            .await;

        // Calculate final stats
        let stats = orchestrator.stats_mut();
        stats.execution_time_ms = start_time.elapsed().as_millis() as u64;
        stats.sequential_time_estimate_ms = all_results.iter().map(|r| r.execution_time_ms).sum();
        stats.calculate_critical_path();
        stats.calculate_speedup();

        // Aggregate results
        let success = all_results.iter().all(|r| r.success);

        // Complete telemetry collection
        let _telemetry_metrics = self.telemetry.lock().await.complete_swarm(success).await;
        let result = self.aggregate_results(&all_results).await?;

        tracing::info!(
            provider_name = %provider_name,
            "Swarm execution complete: {} subtasks, {:.1}x speedup",
            all_results.len(),
            stats.speedup_factor
        );

        let final_stats = orchestrator.stats().clone();
        self.try_send_event(SwarmEvent::Complete {
            success,
            stats: final_stats.clone(),
        });

        Ok(SwarmResult {
            success,
            result,
            subtask_results: all_results,
            stats: final_stats,
            artifacts,
            error: None,
        })
    }

    /// Execute a single stage of subtasks in parallel (with rate limiting and worktree isolation)
    async fn execute_stage(
        &self,
        orchestrator: &Orchestrator,
        subtasks: Vec<SubTask>,
        completed_results: Arc<RwLock<HashMap<String, String>>>,
        swarm_id: &str,
    ) -> Result<Vec<SubTaskResult>> {
        if self.config.execution_mode == ExecutionMode::KubernetesPod {
            return self
                .execute_stage_kubernetes(orchestrator, subtasks, completed_results, swarm_id)
                .await;
        }

        let mut handles: FuturesUnordered<
            tokio::task::JoinHandle<(String, Result<SubTaskResult, anyhow::Error>)>,
        > = FuturesUnordered::new();
        let mut abort_handles: HashMap<String, AbortHandle> = HashMap::new();
        let mut task_ids: HashMap<tokio::task::Id, String> = HashMap::new();
        let mut active_worktrees: HashMap<String, WorktreeInfo> = HashMap::new();
        let mut all_worktrees: HashMap<String, WorktreeInfo> = HashMap::new();
        let mut cached_results: Vec<SubTaskResult> = Vec::new();
        let mut completed_entries: Vec<(SubTaskResult, Option<WorktreeInfo>)> = Vec::new();
        let mut kill_reasons: HashMap<String, String> = HashMap::new();
        let mut promoted_subtask_id: Option<String> = None;

        // Rate limiting: semaphore for max concurrent requests
        let semaphore = Arc::new(tokio::sync::Semaphore::new(
            self.config.max_concurrent_requests,
        ));
        let delay_ms = self.config.request_delay_ms;

        // Get provider info for tool registry
        let model = orchestrator.model().to_string();
        let provider_name = orchestrator.provider().to_string();
        let providers = orchestrator.providers();
        let provider = providers
            .get(&provider_name)
            .ok_or_else(|| anyhow::anyhow!("Provider {} not found", provider_name))?;

        tracing::info!(provider_name = %provider_name, "Selected provider for subtask execution");

        // Create base tool registry with provider for ralph and batch tool
        let base_tool_registry =
            ToolRegistry::with_provider_arc(Arc::clone(&provider), model.clone());
        // Filter out 'question' tool - sub-agents must be autonomous, not interactive
        // Include 'swarm_share' definition so LLMs know about it (registered per-agent below)
        let mut tool_definitions: Vec<_> = base_tool_registry
            .definitions()
            .into_iter()
            .filter(|t| t.name != "question")
            .collect();

        // Add swarm_share tool definition so LLMs know it's available
        let swarm_share_def = crate::provider::ToolDefinition {
            name: "swarm_share".to_string(),
            description: "Share results with other sub-agents in the swarm. Actions: publish \
                          (share a result), get (retrieve a result by key), query_tags (find \
                          results by tags), query_prefix (find results by key prefix), list \
                          (show all shared results)."
                .to_string(),
            parameters: serde_json::json!({
                "type": "object",
                "properties": {
                    "action": {
                        "type": "string",
                        "enum": ["publish", "get", "query_tags", "query_prefix", "list"],
                        "description": "Action to perform"
                    },
                    "key": {
                        "type": "string",
                        "description": "Result key (for publish/get)"
                    },
                    "value": {
                        "description": "Result value to publish (any JSON value)"
                    },
                    "tags": {
                        "type": "array",
                        "items": {"type": "string"},
                        "description": "Tags for publish or query_tags"
                    },
                    "prefix": {
                        "type": "string",
                        "description": "Key prefix for query_prefix"
                    }
                },
                "required": ["action"]
            }),
        };
        tool_definitions.push(swarm_share_def);

        // Clone the result store for sub-agent sharing
        let result_store = Arc::clone(&self.result_store);

        // Create worktree manager if enabled
        let worktree_manager = if self.config.worktree_enabled {
            let working_dir = self.config.working_dir.clone().unwrap_or_else(|| {
                std::env::current_dir()
                    .map(|p| p.to_string_lossy().to_string())
                    .unwrap_or_else(|_| ".".to_string())
            });

            let mgr = WorktreeManager::new(&working_dir);
            tracing::info!(
                working_dir = %working_dir,
                "Worktree isolation enabled for parallel sub-agents"
            );
            Some(Arc::new(mgr) as Arc<WorktreeManager>)
        } else {
            None
        };

        for (idx, subtask) in subtasks.into_iter().enumerate() {
            let model = model.clone();
            let _provider_name = provider_name.clone();
            let provider = Arc::clone(&provider);

            // Check cache first
            if let Some(ref cache) = self.cache {
                let mut cache_guard = cache.lock().await;
                if let Some(cached_result) = cache_guard.get(&subtask).await {
                    tracing::info!(
                        subtask_id = %subtask.id,
                        task_name = %subtask.name,
                        "Cache hit for subtask, skipping execution"
                    );
                    self.try_send_event(SwarmEvent::SubTaskUpdate {
                        id: subtask.id.clone(),
                        name: subtask.name.clone(),
                        status: SubTaskStatus::Completed,
                        agent_name: Some("cached".to_string()),
                    });
                    cached_results.push(cached_result);
                    continue;
                }
            }

            // Get context from dependencies
            let context = {
                let completed = completed_results.read().await;
                let mut dep_context = String::new();
                for dep_id in &subtask.dependencies {
                    if let Some(result) = completed.get(dep_id) {
                        dep_context.push_str(&format!(
                            "\n--- Result from dependency {} ---\n{}\n",
                            dep_id, result
                        ));
                    }
                }
                dep_context
            };

            let instruction = subtask.instruction.clone();
            let subtask_name = subtask.name.clone();
            let specialty = subtask.specialty.clone().unwrap_or_default();
            let subtask_id = subtask.id.clone();
            let subtask_id_for_handle = subtask_id.clone();
            let max_steps = self.config.max_steps_per_subagent;
            let timeout_secs = self.config.subagent_timeout_secs;

            // Retry configuration
            let max_retries = self.config.max_retries;
            let base_delay_ms = self.config.base_delay_ms;
            let max_delay_ms = self.config.max_delay_ms;

            // Create worktree for this sub-agent before spawning so the collapse controller
            // can monitor live branches while execution is in-flight.
            let worktree_info = if let Some(ref mgr) = worktree_manager {
                let task_slug = subtask_id.replace("-", "_");
                match mgr.create(&task_slug).await {
                    Ok(wt) => {
                        if let Err(e) = mgr.inject_workspace_stub(&wt.path) {
                            tracing::warn!(
                                subtask_id = %subtask_id,
                                error = %e,
                                "Failed to inject workspace stub into worktree"
                            );
                        }
                        tracing::info!(
                            subtask_id = %subtask_id,
                            worktree_path = %wt.path.display(),
                            worktree_branch = %wt.branch,
                            "Created worktree for sub-agent"
                        );
                        active_worktrees.insert(subtask_id.clone(), wt.clone());
                        all_worktrees.insert(subtask_id.clone(), wt.clone());
                        Some(wt)
                    }
                    Err(e) => {
                        tracing::warn!(
                            subtask_id = %subtask_id,
                            error = %e,
                            "Failed to create worktree, using shared directory"
                        );
                        None
                    }
                }
            } else {
                None
            };

            let working_dir = worktree_info
                .as_ref()
                .map(|wt| wt.path.display().to_string())
                .unwrap_or_else(|| ".".to_string());
            let working_dir_path = worktree_info.as_ref().map(|wt| wt.path.clone());

            // Clone for the async block
            let tools = tool_definitions.clone();
            let _base_registry = Arc::clone(&base_tool_registry);
            let agent_result_store = Arc::clone(&result_store);
            let sem = Arc::clone(&semaphore);
            let stagger_delay = delay_ms * idx as u64; // Stagger start times
            let event_tx = self.event_tx.clone();

            // Generate sub-agent execution ID
            let subagent_id = format!("agent-{}", uuid::Uuid::new_v4());

            // Log telemetry for this sub-agent
            tracing::debug!(subagent_id = %subagent_id, swarm_id = %swarm_id, subtask = %subtask_id, specialty = %specialty, "Starting sub-agent");

            // Spawn the subtask execution with agentic tool loop
            let handle = tokio::spawn(async move {
                // Rate limiting: stagger start and acquire semaphore
                if stagger_delay > 0 {
                    tokio::time::sleep(Duration::from_millis(stagger_delay)).await;
                }
                let _permit = match sem.acquire().await {
                    Ok(permit) => permit,
                    Err(_) => {
                        return (
                            subtask_id.clone(),
                            Err(anyhow::anyhow!("Swarm execution cancelled")),
                        );
                    }
                };

                let _agent_start = Instant::now();

                let start = Instant::now();

                // Load AGENTS.md from working directory
                let working_path = std::path::Path::new(&working_dir);
                let agents_md_content = crate::agent::builtin::load_agents_md(working_path)
                    .map(|(content, _)| {
                        format!("\n\nPROJECT INSTRUCTIONS (from AGENTS.md):\n{content}")
                    })
                    .unwrap_or_default();

                // Build the system prompt for this sub-agent
                let prd_filename = format!("prd_{}.json", subtask_id.replace("-", "_"));
                let system_prompt = format!(
                    "You are a {} specialist sub-agent (ID: {}). You have access to tools to complete your task.

WORKING DIRECTORY: {}
All file operations should be relative to this directory.

IMPORTANT: You MUST use tools to make changes. Do not just describe what to do - actually do it using the tools available.

Available tools:
- read: Read file contents
- write: Write/create files
- edit: Edit existing files (search and replace)
- multiedit: Make multiple edits at once
- glob: Find files by pattern
- grep: Search file contents
- bash: Run shell commands (use cwd: \"{}\" parameter)
- webfetch: Fetch web pages
- prd: Generate structured PRD for complex tasks
- ralph: Run autonomous agent loop on a PRD
- swarm_share: Share results with other sub-agents running in parallel
- agent: Spawn specialized helper agents when needed (smart delegation)

SMART SPAWN POLICY (mandatory):
- Any spawned agent MUST use a different model than your current model ('{}')
- Spawned model MUST be free/subscription-eligible (e.g. '*:free', openai-codex/*, github-copilot/*, gemini-web/*, local_cuda/*)
- Include `model` when calling agent.spawn

SHARING RESULTS:
Use swarm_share to collaborate with other sub-agents:
- swarm_share({{action: 'publish', key: 'my-finding', value: '...', tags: ['research']}}) to share a result
- swarm_share({{action: 'get', key: 'some-key'}}) to retrieve a result from another agent
- swarm_share({{action: 'list'}}) to see all shared results
- swarm_share({{action: 'query_tags', tags: ['research']}}) to find results by tag

COMPLEX TASKS:
If your task is complex and involves multiple implementation steps, use the prd + ralph workflow:
1. Call prd({{action: 'analyze', task_description: '...'}}) to understand what's needed
2. Break down into user stories with acceptance criteria
3. Call prd({{action: 'save', prd_path: '{}', project: '...', feature: '...', stories: [...]}})
4. Call ralph({{action: 'run', prd_path: '{}'}}) to execute

NOTE: Use your unique PRD file '{}' so parallel agents don't conflict.

When done, provide a brief summary of what you accomplished.{agents_md_content}",
                    specialty,
                    subtask_id,
                    working_dir,
                    working_dir,
                    model,
                    prd_filename,
                    prd_filename,
                    prd_filename
                );

                let user_prompt = if context.is_empty() {
                    format!("Complete this task:\n\n{}", instruction)
                } else {
                    format!(
                        "Complete this task:\n\n{}\n\nContext from prior work:\n{}",
                        instruction, context
                    )
                };

                // Emit AgentStarted event
                if let Some(ref tx) = event_tx {
                    let _ = tx.try_send(SwarmEvent::SubTaskUpdate {
                        id: subtask_id.clone(),
                        name: subtask_name.clone(),
                        status: SubTaskStatus::Running,
                        agent_name: Some(format!("agent-{}", subtask_id)),
                    });
                    let _ = tx.try_send(SwarmEvent::AgentStarted {
                        subtask_id: subtask_id.clone(),
                        agent_name: format!("agent-{}", subtask_id),
                        specialty: specialty.clone(),
                    });
                }

                // Run the agentic loop
                // Create per-agent registry with SwarmShareTool for this subtask
                let mut agent_registry =
                    ToolRegistry::with_provider(Arc::clone(&provider), model.clone());
                agent_registry.register(Arc::new(crate::tool::swarm_share::SwarmShareTool::new(
                    Arc::clone(&agent_result_store),
                    subtask_id.clone(),
                )));
                let registry = Arc::new(agent_registry);

                // Execute with exponential backoff retry
                let mut attempt = 0u32;
                let mut result: Result<(String, usize, usize, AgentLoopExit), anyhow::Error> =
                    Err(anyhow::anyhow!("Not executed"));

                while attempt <= max_retries {
                    let _attempt_start = Instant::now();

                    // Run the agent loop
                    result = run_agent_loop(
                        Arc::clone(&provider),
                        &model,
                        &system_prompt,
                        &user_prompt,
                        tools.clone(),
                        Arc::clone(&registry),
                        max_steps,
                        timeout_secs,
                        event_tx.clone(),
                        subtask_id.clone(),
                        None,
                        working_dir_path.clone(),
                    )
                    .await;

                    // Check if the attempt succeeded
                    match &result {
                        Ok((_, _, _, exit_reason)) => {
                            if *exit_reason == AgentLoopExit::Completed {
                                // Success - no need to retry
                                tracing::info!(
                                    subtask_id = %subtask_id,
                                    attempt = attempt + 1,
                                    "Sub-agent completed successfully on first attempt"
                                );
                                break;
                            }
                            // Failed but not an error - check if we should retry
                            let should_retry = attempt < max_retries;
                            if should_retry {
                                let delay = calculate_backoff_delay(
                                    attempt,
                                    base_delay_ms,
                                    max_delay_ms,
                                    2.0,
                                );
                                tracing::warn!(
                                    subtask_id = %subtask_id,
                                    attempt = attempt + 1,
                                    max_retries = max_retries,
                                    exit_reason = ?exit_reason,
                                    delay_ms = delay.as_millis(),
                                    "Sub-agent did not complete, retrying with backoff"
                                );
                                tokio::time::sleep(delay).await;
                            } else {
                                tracing::warn!(
                                    subtask_id = %subtask_id,
                                    attempt = attempt + 1,
                                    max_retries = max_retries,
                                    exit_reason = ?exit_reason,
                                    "Sub-agent did not complete, retries exhausted"
                                );
                            }
                        }
                        Err(e) => {
                            // Error occurred - retry with backoff if we have retries left
                            let should_retry = attempt < max_retries;
                            if should_retry {
                                let delay = calculate_backoff_delay(
                                    attempt,
                                    base_delay_ms,
                                    max_delay_ms,
                                    2.0,
                                );
                                tracing::warn!(
                                    subtask_id = %subtask_id,
                                    attempt = attempt + 1,
                                    max_retries = max_retries,
                                    error = %e,
                                    delay_ms = delay.as_millis(),
                                    "Sub-agent error, retrying with backoff"
                                );
                                tokio::time::sleep(delay).await;
                            } else {
                                tracing::error!(
                                    subtask_id = %subtask_id,
                                    attempt = attempt + 1,
                                    max_retries = max_retries,
                                    error = %e,
                                    "Sub-agent error, retries exhausted"
                                );
                            }
                        }
                    }

                    attempt += 1;
                }

                let result = result;

                let task_result = match result {
                    Ok((output, steps, tool_calls, exit_reason)) => {
                        let (success, status, error) = match exit_reason {
                            AgentLoopExit::Completed => (true, SubTaskStatus::Completed, None),
                            AgentLoopExit::MaxStepsReached => (
                                false,
                                SubTaskStatus::Failed,
                                Some(format!("Sub-agent hit max steps ({max_steps})")),
                            ),
                            AgentLoopExit::TimedOut => (
                                false,
                                SubTaskStatus::TimedOut,
                                Some(format!("Sub-agent timed out after {timeout_secs}s")),
                            ),
                        };

                        // Calculate actual retry info - attempt is 0-indexed, so attempt=0 means 1 attempt, attempt=1 means 2 attempts, etc.
                        let total_attempts = attempt + 1;
                        let actual_retry_attempts = if total_attempts > 1 {
                            total_attempts - 1
                        } else {
                            0
                        };

                        // Emit completion events
                        if let Some(ref tx) = event_tx {
                            let _ = tx.try_send(SwarmEvent::SubTaskUpdate {
                                id: subtask_id.clone(),
                                name: subtask_name.clone(),
                                status,
                                agent_name: Some(format!("agent-{}", subtask_id)),
                            });
                            if let Some(ref message) = error {
                                let _ = tx.try_send(SwarmEvent::AgentError {
                                    subtask_id: subtask_id.clone(),
                                    error: message.clone(),
                                });
                            }
                            let _ = tx.try_send(SwarmEvent::AgentOutput {
                                subtask_id: subtask_id.clone(),
                                output: output.clone(),
                            });
                            let _ = tx.try_send(SwarmEvent::AgentComplete {
                                subtask_id: subtask_id.clone(),
                                success,
                                steps,
                            });
                        }
                        Ok(SubTaskResult {
                            subtask_id: subtask_id.clone(),
                            subagent_id: format!("agent-{}", subtask_id),
                            success,
                            result: output,
                            steps,
                            tool_calls,
                            execution_time_ms: start.elapsed().as_millis() as u64,
                            error,
                            artifacts: Vec::new(),
                            retry_count: actual_retry_attempts,
                        })
                    }
                    Err(e) => {
                        // Calculate actual retry info - attempt is 0-indexed
                        let total_attempts = attempt + 1;
                        let actual_retry_attempts = if total_attempts > 1 {
                            total_attempts - 1
                        } else {
                            0
                        };

                        // Emit error events
                        if let Some(ref tx) = event_tx {
                            let _ = tx.try_send(SwarmEvent::SubTaskUpdate {
                                id: subtask_id.clone(),
                                name: subtask_name.clone(),
                                status: SubTaskStatus::Failed,
                                agent_name: Some(format!("agent-{}", subtask_id)),
                            });
                            let _ = tx.try_send(SwarmEvent::AgentError {
                                subtask_id: subtask_id.clone(),
                                error: e.to_string(),
                            });
                            let _ = tx.try_send(SwarmEvent::AgentComplete {
                                subtask_id: subtask_id.clone(),
                                success: false,
                                steps: 0,
                            });
                        }
                        Ok(SubTaskResult {
                            subtask_id: subtask_id.clone(),
                            subagent_id: format!("agent-{}", subtask_id),
                            success: false,
                            result: String::new(),
                            steps: 0,
                            tool_calls: 0,
                            execution_time_ms: start.elapsed().as_millis() as u64,
                            error: Some(e.to_string()),
                            artifacts: Vec::new(),
                            retry_count: actual_retry_attempts,
                        })
                    }
                };

                (subtask_id.clone(), task_result)
            });

            let abort_handle = handle.abort_handle();
            abort_handles.insert(subtask_id_for_handle.clone(), abort_handle);
            task_ids.insert(handle.id(), subtask_id_for_handle.clone());
            handles.push(handle);
        }

        // Deterministic collapse loop while branches are still running.
        let mut collapse_controller = if worktree_manager.is_some() && active_worktrees.len() > 1 {
            Some(CollapseController::new(CollapsePolicy::default()))
        } else {
            None
        };
        let mut collapse_tick = tokio::time::interval(Duration::from_secs(COLLAPSE_SAMPLE_SECS));
        collapse_tick.set_missed_tick_behavior(MissedTickBehavior::Skip);
        // Consume the immediate first tick so sampling starts after the first interval.
        let _ = collapse_tick.tick().await;

        while !handles.is_empty() {
            tokio::select! {
                maybe_join = handles.next() => {
                    let Some(joined) = maybe_join else {
                        continue;
                    };
                    match joined {
                        Ok((subtask_id, Ok(result))) => {
                            abort_handles.remove(&subtask_id);
                            let wt = active_worktrees.remove(&subtask_id).or_else(|| all_worktrees.get(&subtask_id).cloned());
                            completed_entries.push((result, wt));
                        }
                        Ok((subtask_id, Err(e))) => {
                            abort_handles.remove(&subtask_id);
                            active_worktrees.remove(&subtask_id);
                            let wt = all_worktrees.get(&subtask_id).cloned();
                            completed_entries.push((
                                SubTaskResult {
                                    subtask_id: subtask_id.clone(),
                                    subagent_id: format!("agent-{subtask_id}"),
                                    success: false,
                                    result: String::new(),
                                    steps: 0,
                                    tool_calls: 0,
                                    execution_time_ms: 0,
                                    error: Some(e.to_string()),
                                    artifacts: Vec::new(),
                                    retry_count: 0,
                                },
                                wt,
                            ));
                        }
                        Err(e) => {
                            let subtask_id = task_ids
                                .remove(&e.id())
                                .unwrap_or_else(|| "unknown".to_string());
                            abort_handles.remove(&subtask_id);
                            active_worktrees.remove(&subtask_id);
                            let wt = all_worktrees.get(&subtask_id).cloned();
                            completed_entries.push((
                                SubTaskResult {
                                    subtask_id: subtask_id.clone(),
                                    subagent_id: format!("agent-{subtask_id}"),
                                    success: false,
                                    result: String::new(),
                                    steps: 0,
                                    tool_calls: 0,
                                    execution_time_ms: 0,
                                    error: Some(format!("Task join error: {e}")),
                                    artifacts: Vec::new(),
                                    retry_count: 0,
                                },
                                wt,
                            ));
                        }
                    }
                }
                _ = collapse_tick.tick(), if collapse_controller.is_some() && !active_worktrees.is_empty() => {
                    let branches: Vec<BranchRuntimeState> = active_worktrees
                        .iter()
                        .map(|(subtask_id, wt)| BranchRuntimeState {
                            subtask_id: subtask_id.clone(),
                            branch: wt.branch.clone(),
                            worktree_path: wt.path.clone(),
                        })
                        .collect();

                    if let Some(controller) = collapse_controller.as_mut() {
                        match controller.sample(&branches) {
                            Ok(tick) => {
                                if promoted_subtask_id != tick.promoted_subtask_id {
                                    promoted_subtask_id = tick.promoted_subtask_id.clone();
                                    if let Some(ref promoted) = promoted_subtask_id {
                                        tracing::info!(
                                            subtask_id = %promoted,
                                            "Collapse controller promoted branch"
                                        );
                                        if let Some(audit) = crate::audit::try_audit_log() {
                                            audit.log_with_correlation(
                                                crate::audit::AuditCategory::Swarm,
                                                "collapse_promote_branch",
                                                crate::audit::AuditOutcome::Success,
                                                Some("collapse-controller".to_string()),
                                                Some(serde_json::json!({
                                                    "swarm_id": swarm_id,
                                                    "subtask_id": promoted,
                                                })),
                                                None,
                                                None,
                                                Some(swarm_id.to_string()),
                                                None,
                                            ).await;
                                        }
                                    }
                                }

                                for kill in tick.kills {
                                    if kill_reasons.contains_key(&kill.subtask_id) {
                                        continue;
                                    }
                                    let Some(abort_handle) = abort_handles.get(&kill.subtask_id) else {
                                        continue;
                                    };

                                    abort_handle.abort();
                                    abort_handles.remove(&kill.subtask_id);
                                    active_worktrees.remove(&kill.subtask_id);
                                    kill_reasons.insert(kill.subtask_id.clone(), kill.reason.clone());

                                    tracing::warn!(
                                        subtask_id = %kill.subtask_id,
                                        branch = %kill.branch,
                                        reason = %kill.reason,
                                        "Collapse controller killed branch"
                                    );

                                    if let Some(ref tx) = self.event_tx {
                                        let _ = tx.try_send(SwarmEvent::SubTaskUpdate {
                                            id: kill.subtask_id.clone(),
                                            name: kill.subtask_id.clone(),
                                            status: SubTaskStatus::Cancelled,
                                            agent_name: Some(format!("agent-{}", kill.subtask_id)),
                                        });
                                        let _ = tx.try_send(SwarmEvent::AgentError {
                                            subtask_id: kill.subtask_id.clone(),
                                            error: format!("Cancelled by collapse controller: {}", kill.reason),
                                        });
                                    }

                                    if let Some(audit) = crate::audit::try_audit_log() {
                                        audit.log_with_correlation(
                                            crate::audit::AuditCategory::Swarm,
                                            "collapse_kill_branch",
                                            crate::audit::AuditOutcome::Success,
                                            Some("collapse-controller".to_string()),
                                            Some(serde_json::json!({
                                                "swarm_id": swarm_id,
                                                "subtask_id": kill.subtask_id,
                                                "branch": kill.branch,
                                                "reason": kill.reason,
                                            })),
                                            None,
                                            None,
                                            Some(swarm_id.to_string()),
                                            None,
                                        ).await;
                                    }
                                }
                            }
                            Err(e) => {
                                tracing::warn!(error = %e, "Collapse controller sampling failed");
                            }
                        }
                    }
                }
            }
        }

        // Merge in deterministic order: promoted branch first when present.
        if let Some(ref promoted) = promoted_subtask_id {
            completed_entries.sort_by_key(|(result, _)| {
                if &result.subtask_id == promoted {
                    0usize
                } else {
                    1usize
                }
            });
        }

        let mut results = cached_results;
        let auto_merge = self.config.worktree_auto_merge;

        for (mut result, worktree_info) in completed_entries {
            // Handle worktree merge/cleanup if applicable
            if let Some(wt) = worktree_info {
                if let Some(reason) = kill_reasons.get(&result.subtask_id) {
                    result.error = Some(format!("Cancelled by collapse controller: {reason}"));
                    result.result.push_str(&format!(
                        "\n\n--- Collapse Controller ---\nBranch terminated: {reason}"
                    ));
                    if let Some(ref mgr) = worktree_manager
                        && let Err(e) = mgr.cleanup(&wt.name).await
                    {
                        tracing::warn!(error = %e, "Failed to cleanup killed worktree");
                    }
                } else if result.success && auto_merge {
                    if let Some(ref mgr) = worktree_manager {
                        match mgr.merge(&wt.name).await {
                            Ok(merge_result) => {
                                if merge_result.success {
                                    tracing::info!(
                                        subtask_id = %result.subtask_id,
                                        files_changed = merge_result.files_changed,
                                        "Merged worktree changes successfully"
                                    );
                                    result.result.push_str(&format!(
                                        "\n\n--- Merge Result ---\n{}",
                                        merge_result.summary
                                    ));
                                } else if merge_result.aborted {
                                    tracing::warn!(
                                        subtask_id = %result.subtask_id,
                                        summary = %merge_result.summary,
                                        "Merge was aborted"
                                    );
                                    result.result.push_str(&format!(
                                        "\n\n--- Merge Aborted ---\n{}",
                                        merge_result.summary
                                    ));
                                } else {
                                    tracing::warn!(
                                        subtask_id = %result.subtask_id,
                                        conflicts = ?merge_result.conflicts,
                                        "Merge had conflicts"
                                    );
                                    result.result.push_str(&format!(
                                        "\n\n--- Merge Conflicts ---\n{}",
                                        merge_result.summary
                                    ));
                                }
                                if let Err(e) = mgr.cleanup(&wt.name).await {
                                    tracing::warn!(error = %e, "Failed to cleanup worktree");
                                }
                            }
                            Err(e) => {
                                tracing::error!(
                                    subtask_id = %result.subtask_id,
                                    error = %e,
                                    "Failed to merge worktree"
                                );
                            }
                        }
                    }
                } else if !result.success {
                    tracing::info!(
                        subtask_id = %result.subtask_id,
                        worktree_path = %wt.path.display(),
                        "Keeping worktree for debugging (task failed)"
                    );
                }
            }

            // Cache successful result
            if result.success
                && let Some(ref cache_arc) = self.cache
            {
                let mut cache_guard: tokio::sync::MutexGuard<'_, SwarmCache> =
                    cache_arc.lock().await;
                let cache_subtask = SubTask::new(&result.subtask_id, &result.result);
                if let Err(e) = cache_guard.put(&cache_subtask, &result).await {
                    tracing::warn!(
                        subtask_id = %result.subtask_id,
                        error = %e,
                        "Failed to cache subtask result"
                    );
                }
            }

            results.push(result);
        }

        Ok(results)
    }

    async fn execute_stage_kubernetes(
        &self,
        orchestrator: &Orchestrator,
        subtasks: Vec<SubTask>,
        completed_results: Arc<RwLock<HashMap<String, String>>>,
        swarm_id: &str,
    ) -> Result<Vec<SubTaskResult>> {
        let k8s = K8sManager::new().await;
        if !k8s.is_available() {
            anyhow::bail!(
                "Kubernetes execution mode requested but K8s client is unavailable in this environment"
            );
        }

        let provider_name = orchestrator.provider().to_string();
        let model = orchestrator.model().to_string();
        let pod_budget = self.config.k8s_pod_budget.max(1);
        let mut pending: VecDeque<SubTask> = subtasks.into_iter().collect();
        let mut active: HashMap<String, ActiveK8sBranch> = HashMap::new();
        let mut subtask_names: HashMap<String, String> = HashMap::new();
        let mut results: Vec<SubTaskResult> = Vec::new();
        let mut kill_reasons: HashMap<String, String> = HashMap::new();
        let mut promoted_subtask_id: Option<String> = None;
        let mut collapse_controller = CollapseController::new(CollapsePolicy::default());

        let mut tick = tokio::time::interval(Duration::from_secs(COLLAPSE_SAMPLE_SECS));
        tick.set_missed_tick_behavior(MissedTickBehavior::Skip);
        let _ = tick.tick().await;

        loop {
            while active.len() < pod_budget {
                let Some(subtask) = pending.pop_front() else {
                    break;
                };

                if let Some(ref cache) = self.cache {
                    let mut cache_guard = cache.lock().await;
                    if let Some(cached_result) = cache_guard.get(&subtask).await {
                        tracing::info!(
                            subtask_id = %subtask.id,
                            task_name = %subtask.name,
                            "Cache hit for subtask, skipping Kubernetes execution"
                        );
                        self.try_send_event(SwarmEvent::SubTaskUpdate {
                            id: subtask.id.clone(),
                            name: subtask.name.clone(),
                            status: SubTaskStatus::Completed,
                            agent_name: Some("cached".to_string()),
                        });
                        results.push(cached_result);
                        continue;
                    }
                }

                let context = {
                    let completed = completed_results.read().await;
                    let mut dep_context = String::new();
                    for dep_id in &subtask.dependencies {
                        if let Some(result) = completed.get(dep_id) {
                            dep_context.push_str(&format!(
                                "\n--- Result from dependency {} ---\n{}\n",
                                dep_id, result
                            ));
                        }
                    }
                    dep_context
                };

                let payload = RemoteSubtaskPayload {
                    swarm_id: swarm_id.to_string(),
                    subtask_id: subtask.id.clone(),
                    subtask_name: subtask.name.clone(),
                    specialty: subtask.specialty.clone().unwrap_or_default(),
                    instruction: subtask.instruction.clone(),
                    context: context.clone(),
                    provider: provider_name.clone(),
                    model: model.clone(),
                    max_steps: self.config.max_steps_per_subagent,
                    timeout_secs: self.config.subagent_timeout_secs,
                    working_dir: self.config.working_dir.clone(),
                    probe_interval_secs: COLLAPSE_SAMPLE_SECS,
                };
                let payload_b64 = match encode_payload(&payload) {
                    Ok(payload) => payload,
                    Err(error) => {
                        let error_text = format!("Failed to encode remote payload: {error}");
                        tracing::error!(subtask_id = %subtask.id, error = %error, "K8s payload encoding failed");
                        self.try_send_event(SwarmEvent::SubTaskUpdate {
                            id: subtask.id.clone(),
                            name: subtask.name.clone(),
                            status: SubTaskStatus::Failed,
                            agent_name: Some("k8s-encoder".to_string()),
                        });
                        self.try_send_event(SwarmEvent::AgentError {
                            subtask_id: subtask.id.clone(),
                            error: error_text.clone(),
                        });
                        results.push(SubTaskResult {
                            subtask_id: subtask.id.clone(),
                            subagent_id: format!("agent-{}", subtask.id),
                            success: false,
                            result: String::new(),
                            steps: 0,
                            tool_calls: 0,
                            execution_time_ms: 0,
                            error: Some(error_text),
                            artifacts: Vec::new(),
                            retry_count: 0,
                        });
                        continue;
                    }
                };

                let mut env_vars = HashMap::new();
                env_vars.insert(SWARM_SUBTASK_PAYLOAD_ENV.to_string(), payload_b64);
                for key in K8S_PASSTHROUGH_ENV_VARS {
                    if let Ok(value) = std::env::var(key)
                        && !value.trim().is_empty()
                    {
                        env_vars.insert((*key).to_string(), value);
                    }
                }
                let fallback_prompt = if context.trim().is_empty() {
                    format!(
                        "You are executing swarm subtask '{}'.\n\nTask:\n{}\n\n\
Return only the final subtask answer.",
                        subtask.id, subtask.instruction
                    )
                } else {
                    format!(
                        "You are executing swarm subtask '{}'.\n\nTask:\n{}\n\n\
Dependency context:\n{}\n\nReturn only the final subtask answer.",
                        subtask.id, subtask.instruction, context
                    )
                };
                env_vars.insert(SWARM_FALLBACK_PROMPT_ENV.to_string(), fallback_prompt);
                env_vars.insert(SWARM_FALLBACK_MODEL_ENV.to_string(), model.clone());

                let mut labels = HashMap::new();
                labels.insert("codetether.run/swarm-id".to_string(), swarm_id.to_string());
                labels.insert(
                    "codetether.run/stage".to_string(),
                    subtask.stage.to_string(),
                );

                let spec = SubagentPodSpec {
                    image: self.config.k8s_subagent_image.clone(),
                    env_vars,
                    labels,
                    command: Some(vec!["sh".to_string(), "-lc".to_string()]),
                    args: Some(vec![
                        format!(
                            "if codetether help swarm-subagent >/dev/null 2>&1; then \
exec codetether swarm-subagent --payload-env {payload_env}; \
else \
exec codetether run \"$${fallback_prompt_env}\" --model \"$${fallback_model_env}\"; \
fi",
                            payload_env = SWARM_SUBTASK_PAYLOAD_ENV,
                            fallback_prompt_env = SWARM_FALLBACK_PROMPT_ENV,
                            fallback_model_env = SWARM_FALLBACK_MODEL_ENV,
                        )
                        .replace("$$", "$"),
                    ]),
                };

                if let Err(error) = k8s.spawn_subagent_pod_with_spec(&subtask.id, spec).await {
                    let error_text = format!("Failed to spawn Kubernetes pod: {error}");
                    tracing::error!(subtask_id = %subtask.id, error = %error, "K8s sub-agent pod spawn failed");
                    self.try_send_event(SwarmEvent::SubTaskUpdate {
                        id: subtask.id.clone(),
                        name: subtask.name.clone(),
                        status: SubTaskStatus::Failed,
                        agent_name: Some("k8s-spawn".to_string()),
                    });
                    self.try_send_event(SwarmEvent::AgentError {
                        subtask_id: subtask.id.clone(),
                        error: error_text.clone(),
                    });
                    results.push(SubTaskResult {
                        subtask_id: subtask.id.clone(),
                        subagent_id: format!("agent-{}", subtask.id),
                        success: false,
                        result: String::new(),
                        steps: 0,
                        tool_calls: 0,
                        execution_time_ms: 0,
                        error: Some(error_text),
                        artifacts: Vec::new(),
                        retry_count: 0,
                    });
                    continue;
                }

                let branch = K8sManager::subagent_pod_name(&subtask.id);
                subtask_names.insert(subtask.id.clone(), subtask.name.clone());
                active.insert(
                    subtask.id.clone(),
                    ActiveK8sBranch {
                        branch: branch.clone(),
                        started_at: Instant::now(),
                    },
                );

                self.try_send_event(SwarmEvent::SubTaskUpdate {
                    id: subtask.id.clone(),
                    name: subtask.name.clone(),
                    status: SubTaskStatus::Running,
                    agent_name: Some(format!("k8s-{branch}")),
                });
                self.try_send_event(SwarmEvent::AgentStarted {
                    subtask_id: subtask.id.clone(),
                    agent_name: format!("k8s-{branch}"),
                    specialty: subtask
                        .specialty
                        .clone()
                        .unwrap_or_else(|| "generalist".to_string()),
                });

                tracing::info!(
                    subtask_id = %subtask.id,
                    pod = %branch,
                    "Spawned Kubernetes sub-agent pod"
                );
            }

            if pending.is_empty() && active.is_empty() {
                break;
            }

            tick.tick().await;

            let active_ids: Vec<String> = active.keys().cloned().collect();
            let mut finished_results: Vec<SubTaskResult> = Vec::new();
            for subtask_id in active_ids {
                let Some(active_state) = active.get(&subtask_id).cloned() else {
                    continue;
                };

                if active_state.started_at.elapsed()
                    > Duration::from_secs(self.config.subagent_timeout_secs)
                {
                    let reason = format!(
                        "Timed out after {}s in Kubernetes pod",
                        self.config.subagent_timeout_secs
                    );
                    kill_reasons.insert(subtask_id.clone(), reason.clone());
                    if let Err(error) = k8s.delete_subagent_pod(&subtask_id).await {
                        tracing::warn!(
                            subtask_id = %subtask_id,
                            error = %error,
                            "Failed deleting timed-out Kubernetes pod"
                        );
                    }
                    active.remove(&subtask_id);
                    finished_results.push(SubTaskResult {
                        subtask_id: subtask_id.clone(),
                        subagent_id: format!("agent-{subtask_id}"),
                        success: false,
                        result: String::new(),
                        steps: 0,
                        tool_calls: 0,
                        execution_time_ms: active_state.started_at.elapsed().as_millis() as u64,
                        error: Some(reason),
                        artifacts: Vec::new(),
                        retry_count: 0,
                    });
                    continue;
                }

                let pod_state = match k8s.get_subagent_pod_state(&subtask_id).await {
                    Ok(state) => state,
                    Err(error) => {
                        tracing::warn!(
                            subtask_id = %subtask_id,
                            error = %error,
                            "Failed to query Kubernetes pod state for sub-agent"
                        );
                        continue;
                    }
                };
                let Some(pod_state) = pod_state else {
                    active.remove(&subtask_id);
                    finished_results.push(SubTaskResult {
                        subtask_id: subtask_id.clone(),
                        subagent_id: format!("agent-{subtask_id}"),
                        success: false,
                        result: String::new(),
                        steps: 0,
                        tool_calls: 0,
                        execution_time_ms: active_state.started_at.elapsed().as_millis() as u64,
                        error: Some("Sub-agent pod disappeared".to_string()),
                        artifacts: Vec::new(),
                        retry_count: 0,
                    });
                    continue;
                };

                let phase = pod_state.phase.to_ascii_lowercase();
                let finished = pod_state.terminated || phase == "succeeded" || phase == "failed";
                if !finished {
                    continue;
                }

                let logs = k8s
                    .subagent_logs(&subtask_id, 10_000)
                    .await
                    .unwrap_or_default();
                let mut result = result_from_logs(&logs).unwrap_or_else(|| SubTaskResult {
                    subtask_id: subtask_id.clone(),
                    subagent_id: format!("agent-{subtask_id}"),
                    success: pod_state.exit_code.unwrap_or(1) == 0,
                    result: logs,
                    steps: 0,
                    tool_calls: 0,
                    execution_time_ms: active_state.started_at.elapsed().as_millis() as u64,
                    error: if pod_state.exit_code.unwrap_or(1) == 0 {
                        None
                    } else {
                        Some(
                            pod_state
                                .reason
                                .clone()
                                .unwrap_or_else(|| "Remote sub-agent failed".to_string()),
                        )
                    },
                    artifacts: Vec::new(),
                    retry_count: 0,
                });

                if let Some(reason) = kill_reasons.get(&subtask_id) {
                    result.success = false;
                    result.error = Some(format!("Cancelled by collapse controller: {reason}"));
                    result.result.push_str(&format!(
                        "\n\n--- Collapse Controller ---\nBranch terminated: {reason}"
                    ));
                }

                active.remove(&subtask_id);
                if let Err(error) = k8s.delete_subagent_pod(&subtask_id).await {
                    tracing::warn!(
                        subtask_id = %subtask_id,
                        error = %error,
                        "Failed deleting completed Kubernetes pod"
                    );
                }
                finished_results.push(result);
            }

            for result in finished_results {
                if result.success {
                    completed_results
                        .write()
                        .await
                        .insert(result.subtask_id.clone(), result.result.clone());
                }
                if result.success
                    && let Some(ref cache_arc) = self.cache
                {
                    let mut cache_guard = cache_arc.lock().await;
                    let cache_subtask = SubTask::new(&result.subtask_id, &result.result);
                    let _ = cache_guard.put(&cache_subtask, &result).await;
                }

                self.try_send_event(SwarmEvent::SubTaskUpdate {
                    id: result.subtask_id.clone(),
                    name: subtask_names
                        .get(&result.subtask_id)
                        .cloned()
                        .unwrap_or_else(|| result.subtask_id.clone()),
                    status: if result.success {
                        SubTaskStatus::Completed
                    } else {
                        SubTaskStatus::Failed
                    },
                    agent_name: Some(format!("k8s-{}", result.subtask_id)),
                });
                if let Some(ref error) = result.error {
                    self.try_send_event(SwarmEvent::AgentError {
                        subtask_id: result.subtask_id.clone(),
                        error: error.clone(),
                    });
                }
                self.try_send_event(SwarmEvent::AgentOutput {
                    subtask_id: result.subtask_id.clone(),
                    output: result.result.clone(),
                });
                self.try_send_event(SwarmEvent::AgentComplete {
                    subtask_id: result.subtask_id.clone(),
                    success: result.success,
                    steps: result.steps,
                });
                results.push(result);
            }

            if active.len() > 1 {
                let mut observations = Vec::with_capacity(active.len());
                for (subtask_id, state) in &active {
                    let pod_state = match k8s.get_subagent_pod_state(subtask_id).await {
                        Ok(state) => state,
                        Err(error) => {
                            tracing::warn!(
                                subtask_id = %subtask_id,
                                error = %error,
                                "Failed to query pod state while sampling branch observation"
                            );
                            None
                        }
                    };
                    let (resource_health_score, infra_unhealthy_signals) =
                        compute_resource_health(pod_state.as_ref());

                    let logs = k8s.subagent_logs(subtask_id, 500).await.unwrap_or_default();
                    if let Some(probe) = latest_probe_from_logs(&logs) {
                        let compile_ok = pod_state
                            .as_ref()
                            .map(|p| probe.compile_ok && !p.phase.eq_ignore_ascii_case("failed"))
                            .unwrap_or(probe.compile_ok);
                        observations.push(BranchObservation {
                            subtask_id: subtask_id.clone(),
                            branch: state.branch.clone(),
                            compile_ok,
                            changed_files: probe_changed_files_set(&probe),
                            changed_lines: probe.changed_lines,
                            resource_health_score,
                            infra_unhealthy_signals,
                        });
                        continue;
                    }
                    let compile_ok = pod_state
                        .as_ref()
                        .map(|p| !p.phase.eq_ignore_ascii_case("failed"))
                        .unwrap_or(false);
                    observations.push(BranchObservation {
                        subtask_id: subtask_id.clone(),
                        branch: state.branch.clone(),
                        compile_ok,
                        changed_files: std::collections::HashSet::new(),
                        changed_lines: 0,
                        resource_health_score,
                        infra_unhealthy_signals,
                    });
                }

                let tick = collapse_controller.sample_observations(&observations);
                if promoted_subtask_id != tick.promoted_subtask_id {
                    promoted_subtask_id = tick.promoted_subtask_id.clone();
                    if let Some(ref promoted) = promoted_subtask_id {
                        tracing::info!(subtask_id = %promoted, "Collapse controller promoted branch");
                        if let Some(audit) = crate::audit::try_audit_log() {
                            audit
                                .log_with_correlation(
                                    crate::audit::AuditCategory::Swarm,
                                    "collapse_promote_branch",
                                    crate::audit::AuditOutcome::Success,
                                    Some("collapse-controller".to_string()),
                                    Some(serde_json::json!({
                                        "swarm_id": swarm_id,
                                        "subtask_id": promoted,
                                        "execution_mode": "kubernetes_pod",
                                    })),
                                    None,
                                    None,
                                    Some(swarm_id.to_string()),
                                    None,
                                )
                                .await;
                        }
                    }
                }

                for kill in tick.kills {
                    if kill_reasons.contains_key(&kill.subtask_id) {
                        continue;
                    }
                    if !active.contains_key(&kill.subtask_id) {
                        continue;
                    }

                    if let Err(error) = k8s.delete_subagent_pod(&kill.subtask_id).await {
                        tracing::warn!(
                            subtask_id = %kill.subtask_id,
                            error = %error,
                            "Failed deleting Kubernetes pod after collapse kill"
                        );
                    }
                    kill_reasons.insert(kill.subtask_id.clone(), kill.reason.clone());
                    let elapsed_ms = active
                        .remove(&kill.subtask_id)
                        .map(|s| s.started_at.elapsed().as_millis() as u64)
                        .unwrap_or(0);

                    tracing::warn!(
                        subtask_id = %kill.subtask_id,
                        branch = %kill.branch,
                        reason = %kill.reason,
                        "Collapse controller killed Kubernetes branch"
                    );

                    if let Some(audit) = crate::audit::try_audit_log() {
                        audit
                            .log_with_correlation(
                                crate::audit::AuditCategory::Swarm,
                                "collapse_kill_branch",
                                crate::audit::AuditOutcome::Success,
                                Some("collapse-controller".to_string()),
                                Some(serde_json::json!({
                                    "swarm_id": swarm_id,
                                    "subtask_id": kill.subtask_id.clone(),
                                    "branch": kill.branch.clone(),
                                    "reason": kill.reason.clone(),
                                    "execution_mode": "kubernetes_pod",
                                })),
                                None,
                                None,
                                Some(swarm_id.to_string()),
                                None,
                            )
                            .await;
                    }

                    self.try_send_event(SwarmEvent::SubTaskUpdate {
                        id: kill.subtask_id.clone(),
                        name: subtask_names
                            .get(&kill.subtask_id)
                            .cloned()
                            .unwrap_or_else(|| kill.subtask_id.clone()),
                        status: SubTaskStatus::Cancelled,
                        agent_name: Some(format!("agent-{}", kill.subtask_id)),
                    });
                    self.try_send_event(SwarmEvent::AgentError {
                        subtask_id: kill.subtask_id.clone(),
                        error: format!("Cancelled by collapse controller: {}", kill.reason),
                    });

                    results.push(SubTaskResult {
                        subtask_id: kill.subtask_id.clone(),
                        subagent_id: format!("agent-{}", kill.subtask_id),
                        success: false,
                        result: format!(
                            "\n\n--- Collapse Controller ---\nBranch terminated: {}",
                            kill.reason
                        ),
                        steps: 0,
                        tool_calls: 0,
                        execution_time_ms: elapsed_ms,
                        error: Some(format!("Cancelled by collapse controller: {}", kill.reason)),
                        artifacts: Vec::new(),
                        retry_count: 0,
                    });
                }
            }
        }

        if let Some(ref promoted) = promoted_subtask_id {
            results.sort_by_key(|result| {
                if &result.subtask_id == promoted {
                    0usize
                } else {
                    1usize
                }
            });
        }

        if !active.is_empty() {
            let residual_ids: Vec<String> = active.keys().cloned().collect();
            for subtask_id in residual_ids {
                if let Err(error) = k8s.delete_subagent_pod(&subtask_id).await {
                    tracing::warn!(
                        subtask_id = %subtask_id,
                        error = %error,
                        "Failed deleting residual Kubernetes pod at stage end"
                    );
                }
            }
        }

        Ok(results)
    }

    /// Aggregate results from all subtasks into a final result
    async fn aggregate_results(&self, results: &[SubTaskResult]) -> Result<String> {
        let mut aggregated = String::new();

        for (i, result) in results.iter().enumerate() {
            if result.success {
                aggregated.push_str(&format!("=== Subtask {} ===\n{}\n\n", i + 1, result.result));
            } else {
                aggregated.push_str(&format!(
                    "=== Subtask {} (FAILED) ===\nError: {}\n\n",
                    i + 1,
                    result.error.as_deref().unwrap_or("Unknown error")
                ));
            }
        }

        Ok(aggregated)
    }

    /// Execute a single task without decomposition (for simple cases)
    pub async fn execute_single(&self, task: &str) -> Result<SwarmResult> {
        self.execute(task, DecompositionStrategy::None).await
    }
}

/// Builder for swarm execution
pub struct SwarmExecutorBuilder {
    config: SwarmConfig,
}

impl SwarmExecutorBuilder {
    pub fn new() -> Self {
        Self {
            config: SwarmConfig::default(),
        }
    }

    pub fn max_subagents(mut self, max: usize) -> Self {
        self.config.max_subagents = max;
        self
    }

    pub fn max_steps_per_subagent(mut self, max: usize) -> Self {
        self.config.max_steps_per_subagent = max;
        self
    }

    pub fn max_total_steps(mut self, max: usize) -> Self {
        self.config.max_total_steps = max;
        self
    }

    pub fn timeout_secs(mut self, secs: u64) -> Self {
        self.config.subagent_timeout_secs = secs;
        self
    }

    pub fn parallel_enabled(mut self, enabled: bool) -> Self {
        self.config.parallel_enabled = enabled;
        self
    }

    pub fn build(self) -> SwarmExecutor {
        SwarmExecutor::new(self.config)
    }
}

impl Default for SwarmExecutorBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Run the agentic loop for a sub-agent with tool execution
#[allow(clippy::too_many_arguments)]
/// Run an agentic loop with tools - reusable for Ralph and swarm sub-agents
/// Resolve relative file paths in tool call arguments to use the given working directory.
///
/// When sub-agents run in worktrees, the file tools (read, write, edit, etc.) resolve
/// relative paths from the process CWD (the main repo), not the worktree. This function
/// rewrites relative paths to absolute paths within the worktree before tool execution.
fn resolve_tool_paths(
    tool_name: &str,
    args: &mut serde_json::Value,
    working_dir: &std::path::Path,
) {
    match tool_name {
        "read" | "write" | "list" | "grep" | "codesearch" => {
            if let Some(path) = args.get("path").and_then(|v| v.as_str()).map(String::from)
                && !std::path::Path::new(&path).is_absolute()
            {
                args["path"] = serde_json::json!(working_dir.join(&path).display().to_string());
            }
        }
        "edit" => {
            if let Some(path) = args
                .get("filePath")
                .and_then(|v| v.as_str())
                .map(String::from)
                && !std::path::Path::new(&path).is_absolute()
            {
                args["filePath"] = serde_json::json!(working_dir.join(&path).display().to_string());
            }
        }
        "glob" => {
            if let Some(pattern) = args
                .get("pattern")
                .and_then(|v| v.as_str())
                .map(String::from)
                && !std::path::Path::new(&pattern).is_absolute()
                && !pattern.starts_with("*")
            {
                args["pattern"] =
                    serde_json::json!(working_dir.join(&pattern).display().to_string());
            }
        }
        "multiedit" => {
            if let Some(edits) = args.get_mut("edits").and_then(|v| v.as_array_mut()) {
                for edit in edits.iter_mut() {
                    if let Some(file) = edit.get("file").and_then(|v| v.as_str()).map(String::from)
                        && !std::path::Path::new(&file).is_absolute()
                    {
                        edit["file"] =
                            serde_json::json!(working_dir.join(&file).display().to_string());
                    }
                }
            }
        }
        "patch" => {
            if let Some(path) = args.get("file").and_then(|v| v.as_str()).map(String::from)
                && !std::path::Path::new(&path).is_absolute()
            {
                args["file"] = serde_json::json!(working_dir.join(&path).display().to_string());
            }
        }
        "bash" => {
            // If bash has no cwd, set it to the working directory
            if args.get("cwd").and_then(|v| v.as_str()).is_none() {
                args["cwd"] = serde_json::json!(working_dir.display().to_string());
            }
        }
        _ => {}
    }
}

pub async fn run_agent_loop(
    provider: Arc<dyn Provider>,
    model: &str,
    system_prompt: &str,
    user_prompt: &str,
    tools: Vec<crate::provider::ToolDefinition>,
    registry: Arc<ToolRegistry>,
    max_steps: usize,
    timeout_secs: u64,
    event_tx: Option<mpsc::Sender<SwarmEvent>>,
    subtask_id: String,
    bus: Option<Arc<AgentBus>>,
    working_dir: Option<std::path::PathBuf>,
) -> Result<(String, usize, usize, AgentLoopExit)> {
    // Let the provider handle temperature - K2 models need 0.6 when thinking is disabled
    let temperature = 0.7;

    tracing::info!(
        model = %model,
        max_steps = max_steps,
        timeout_secs = timeout_secs,
        "Sub-agent starting agentic loop"
    );
    tracing::debug!(system_prompt = %system_prompt, "Sub-agent system prompt");
    tracing::debug!(user_prompt = %user_prompt, "Sub-agent user prompt");

    // Initialize conversation with system and user messages
    let mut messages = vec![
        Message {
            role: Role::System,
            content: vec![ContentPart::Text {
                text: system_prompt.to_string(),
            }],
        },
        Message {
            role: Role::User,
            content: vec![ContentPart::Text {
                text: user_prompt.to_string(),
            }],
        },
    ];

    let mut steps = 0;
    let mut total_tool_calls = 0;
    let mut final_output = String::new();

    let mut deadline = Instant::now() + Duration::from_secs(timeout_secs);

    let exit_reason = loop {
        if steps >= max_steps {
            tracing::warn!(max_steps = max_steps, "Sub-agent reached max steps limit");
            break AgentLoopExit::MaxStepsReached;
        }

        if Instant::now() > deadline {
            tracing::warn!(timeout_secs = timeout_secs, "Sub-agent timed out");
            break AgentLoopExit::TimedOut;
        }

        steps += 1;
        tracing::info!(step = steps, "Sub-agent step starting");

        // Check context size and truncate if approaching limit
        truncate_messages_to_fit(&mut messages, DEFAULT_CONTEXT_LIMIT);

        let request = CompletionRequest {
            messages: messages.clone(),
            tools: tools.clone(),
            model: model.to_string(),
            temperature: Some(temperature),
            top_p: None,
            max_tokens: Some(8192),
            stop: Vec::new(),
        };

        let step_start = Instant::now();
        let response = timeout(Duration::from_secs(120), provider.complete(request)).await??;
        let step_duration = step_start.elapsed();

        tracing::info!(
            step = steps,
            duration_ms = step_duration.as_millis() as u64,
            finish_reason = ?response.finish_reason,
            prompt_tokens = response.usage.prompt_tokens,
            completion_tokens = response.usage.completion_tokens,
            "Sub-agent step completed LLM call"
        );

        // Extract text from response
        let mut text_parts = Vec::new();
        let mut thinking_parts = Vec::new();
        let mut tool_calls = Vec::new();

        for part in &response.message.content {
            match part {
                ContentPart::Text { text } => {
                    text_parts.push(text.clone());
                }
                ContentPart::Thinking { text } if !text.is_empty() => {
                    thinking_parts.push(text.clone());
                }
                ContentPart::ToolCall {
                    id,
                    name,
                    arguments,
                    ..
                } => {
                    tool_calls.push((id.clone(), name.clone(), arguments.clone()));
                }
                _ => {}
            }
        }

        // Publish thinking/reasoning to bus for training data capture
        if !thinking_parts.is_empty()
            && let Some(ref bus) = bus
        {
            let thinking_text = thinking_parts.join("\n");
            let handle = bus.handle(&subtask_id);
            handle.send(
                format!("agent.{subtask_id}.thinking"),
                BusMessage::AgentThinking {
                    agent_id: subtask_id.clone(),
                    thinking: thinking_text,
                    step: steps,
                },
            );
        }

        // Log assistant output
        if !text_parts.is_empty() {
            let step_output = text_parts.join("\n");
            if !final_output.is_empty() {
                final_output.push('\n');
            }
            final_output.push_str(&step_output);
            tracing::info!(
                step = steps,
                output_len = final_output.len(),
                "Sub-agent text output"
            );
            tracing::debug!(step = steps, output = %final_output, "Sub-agent full output");

            // Emit assistant message event for TUI detail view
            if let Some(ref tx) = event_tx {
                let preview = if step_output.len() > 500 {
                    let mut end = 500;
                    while end > 0 && !step_output.is_char_boundary(end) {
                        end -= 1;
                    }
                    format!("{}...", &step_output[..end])
                } else {
                    step_output.clone()
                };
                let _ = tx.try_send(SwarmEvent::AgentMessage {
                    subtask_id: subtask_id.clone(),
                    entry: AgentMessageEntry {
                        role: "assistant".to_string(),
                        content: preview,
                        is_tool_call: false,
                    },
                });
            }
        }

        // Log tool calls
        if !tool_calls.is_empty() {
            tracing::info!(
                step = steps,
                num_tool_calls = tool_calls.len(),
                tools = ?tool_calls.iter().map(|(_, name, _)| name.as_str()).collect::<Vec<_>>(),
                "Sub-agent requesting tool calls"
            );
        }

        // Add assistant message to history
        messages.push(response.message.clone());

        // If no tool calls or stop, we're done
        if response.finish_reason != FinishReason::ToolCalls || tool_calls.is_empty() {
            tracing::info!(
                steps = steps,
                total_tool_calls = total_tool_calls,
                "Sub-agent finished"
            );
            break AgentLoopExit::Completed;
        }

        // Execute tool calls
        let mut tool_results = Vec::new();

        for (call_id, tool_name, arguments) in tool_calls {
            total_tool_calls += 1;

            // Emit tool call event
            if let Some(ref tx) = event_tx {
                let _ = tx.try_send(SwarmEvent::AgentToolCall {
                    subtask_id: subtask_id.clone(),
                    tool_name: tool_name.clone(),
                });
            }

            tracing::info!(
                step = steps,
                tool_call_id = %call_id,
                tool = %tool_name,
                "Executing tool"
            );
            tracing::debug!(
                tool = %tool_name,
                arguments = %arguments,
                "Tool call arguments"
            );

            let tool_start = Instant::now();
            let mut tool_success = true;
            let result = if let Some(tool) = registry.get(&tool_name) {
                // Parse arguments as JSON
                let mut args: serde_json::Value =
                    serde_json::from_str(&arguments).unwrap_or_else(|e| {
                        tracing::warn!(tool = %tool_name, error = %e, raw = %arguments, "Failed to parse tool arguments");
                        serde_json::json!({})
                    });

                // Resolve relative file paths to the working directory (critical for worktree isolation)
                if let Some(ref wd) = working_dir {
                    resolve_tool_paths(&tool_name, &mut args, wd);
                }
                let agent_name = format!("agent-{subtask_id}");
                let provenance =
                    ExecutionProvenance::for_operation(&agent_name, ExecutionOrigin::Swarm);
                args = enrich_tool_input_with_runtime_context(
                    &args,
                    working_dir
                        .as_deref()
                        .unwrap_or_else(|| std::path::Path::new(".")),
                    Some(model),
                    &subtask_id,
                    &agent_name,
                    Some(&provenance),
                );

                match tool.execute(args).await {
                    Ok(r) => {
                        if r.success {
                            tracing::info!(
                                tool = %tool_name,
                                duration_ms = tool_start.elapsed().as_millis() as u64,
                                success = true,
                                "Tool execution completed"
                            );
                            r.output
                        } else {
                            tool_success = false;
                            tracing::warn!(
                                tool = %tool_name,
                                error = %r.output,
                                "Tool returned error"
                            );
                            format!("Tool error: {}", r.output)
                        }
                    }
                    Err(e) => {
                        tool_success = false;
                        tracing::error!(
                            tool = %tool_name,
                            error = %e,
                            "Tool execution failed"
                        );
                        format!("Tool execution failed: {}", e)
                    }
                }
            } else {
                tool_success = false;
                tracing::error!(tool = %tool_name, "Unknown tool requested");
                format!("Unknown tool: {}", tool_name)
            };

            // Emit detailed tool call event
            if let Some(ref tx) = event_tx {
                let input_preview = if arguments.len() > 200 {
                    let mut end = 200;
                    while end > 0 && !arguments.is_char_boundary(end) {
                        end -= 1;
                    }
                    format!("{}...", &arguments[..end])
                } else {
                    arguments.clone()
                };
                let output_preview = if result.len() > 500 {
                    let mut end = 500;
                    while end > 0 && !result.is_char_boundary(end) {
                        end -= 1;
                    }
                    format!("{}...", &result[..end])
                } else {
                    result.clone()
                };
                let _ = tx.try_send(SwarmEvent::AgentToolCallDetail {
                    subtask_id: subtask_id.clone(),
                    detail: AgentToolCallDetail {
                        tool_name: tool_name.clone(),
                        input_preview,
                        output_preview,
                        success: tool_success,
                    },
                });
            }

            tracing::debug!(
                tool = %tool_name,
                result_len = result.len(),
                "Tool result"
            );

            // Publish full, untruncated tool output to the bus so other
            // agents and the bus log see the complete result.
            if let Some(ref bus) = bus {
                let handle = bus.handle(&subtask_id);
                handle.send(
                    format!("tools.{tool_name}"),
                    BusMessage::ToolOutputFull {
                        agent_id: subtask_id.clone(),
                        tool_name: tool_name.clone(),
                        output: result.clone(),
                        success: tool_success,
                        step: steps,
                    },
                );
            }

            // Process large results with RLM or truncate smaller ones
            let result = if result.len() > RLM_THRESHOLD_CHARS {
                // Use RLM for very large results
                process_large_result_with_rlm(&result, &tool_name, Arc::clone(&provider), model)
                    .await
            } else {
                // Simple truncation for medium results
                truncate_single_result(&result, SIMPLE_TRUNCATE_CHARS)
            };

            tool_results.push((call_id, tool_name, result));
        }

        // Add tool results to conversation
        for (call_id, _tool_name, result) in tool_results {
            messages.push(Message {
                role: Role::Tool,
                content: vec![ContentPart::ToolResult {
                    tool_call_id: call_id,
                    content: result,
                }],
            });
        }

        // Reset deadline after each successful step — agent is making progress
        deadline = Instant::now() + Duration::from_secs(timeout_secs);
    };

    Ok((final_output, steps, total_tool_calls, exit_reason))
}