๐Ÿ“ฆ Kong / volcano-sdk

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

/* ---------- LLM ---------- */
export type { LLMHandle, ToolDefinition, LLMToolResult };
export const llmOpenAI = llmOpenAIProvider;
export const llmOpenAIResponses = llmOpenAIResponsesProvider;

/* ---------- Errors ---------- */
export interface VolcanoErrorMeta {
  stepId?: number;
  provider?: string;
  requestId?: string;
  retryable?: boolean;
}

export class VolcanoError extends Error {
  meta: VolcanoErrorMeta;
  constructor(message: string, meta: VolcanoErrorMeta = {}, options?: { cause?: unknown }) {
    super(message);
    this.name = this.constructor.name;
    this.meta = meta;
    if (options?.cause) {
      Object.defineProperty(this, 'cause', {
        value: options.cause,
        enumerable: false,
        configurable: true
      });
    }
  }
}
export class AgentConcurrencyError extends VolcanoError {}
export class TimeoutError extends VolcanoError {}
export class ValidationError extends VolcanoError {}
export class RetryExhaustedError extends VolcanoError {}
export class LLMError extends VolcanoError {}
export class MCPError extends VolcanoError {}
export class MCPConnectionError extends MCPError {}
export class MCPToolError extends MCPError {}

function isRetryableStatus(status?: number): boolean {
  if (!status && status !== 0) return false;
  return status >= 500 || status === 429 || status === 408;
}
function classifyProviderFromLlm(usedLlm?: LLMHandle): string | undefined {
  if (!usedLlm) return undefined;
  return `llm:${getLLMProviderId(usedLlm)}`;
}
function classifyProviderFromMcp(handle?: MCPHandle): string | undefined {
  if (!handle) return undefined;
  try { const u = new URL(handle.url); return `mcp:${u.host}`; } catch { return `mcp:${handle.id}`; }
}
function normalizeError(e: any, kind: 'timeout'|'validation'|'llm'|'mcp-conn'|'mcp-tool'|'retry', meta: VolcanoErrorMeta): VolcanoError {
  if (kind === 'timeout') return new TimeoutError(e?.message || 'Step timed out', { ...meta, retryable: true }, { cause: e });
  if (kind === 'validation') return new ValidationError(e?.message || 'Validation failed', { ...meta, retryable: false }, { cause: e });
  if (kind === 'retry') return new RetryExhaustedError(e?.message || 'Retry attempts exhausted', { ...meta }, { cause: e });
  if (kind === 'llm') {
    const status = e?.status ?? e?.response?.status;
    const requestId = e?.response?.headers?.get?.('x-request-id') || e?.id || e?.response?.data?.id;
    const retryable = (status == null ? true : isRetryableStatus(status)) || !!e?.code?.toString?.()?.includes?.('ECONN') || !!e?.code?.toString?.()?.includes?.('ETIMEDOUT');
    return new LLMError(e?.message || 'LLM error', { ...meta, requestId, retryable }, { cause: e });
  }
  if (kind === 'mcp-conn') {
    const retryable = true;
    return new MCPConnectionError(e?.message || 'MCP connection error', { ...meta, retryable }, { cause: e });
  }
  // mcp-tool
  return new MCPToolError(e?.message || 'MCP tool error', { ...meta, retryable: false }, { cause: e });
}

/* ---------- Tool Parallelization ---------- */

/**
 * Determines if a set of tool calls can be safely executed in parallel.
 * Conservative approach: only parallelize when it's obviously safe.
 * 
 * Safe conditions (ALL must be true):
 * 1. All calls are to the SAME tool
 * 2. All calls operate on DIFFERENT resources (different IDs)
 * 3. All arguments are different (no duplicate operations)
 */
function canSafelyParallelize(toolCalls: any[]): boolean {
  if (toolCalls.length <= 1) return false;
  
  // Condition 1: All same tool name
  const toolNames = toolCalls.map(c => c?.name).filter(Boolean);
  if (toolNames.length !== toolCalls.length) return false; // Some missing names
  
  const uniqueTools = new Set(toolNames);
  if (uniqueTools.size !== 1) return false; // Different tools
  
  // Condition 2: Extract resource IDs using pattern matching
  const getResourceId = (args: any): string | undefined => {
    if (!args || typeof args !== 'object') return undefined;
    
    // Find any parameter ending with 'id' (case-insensitive) or exactly named 'id'
    for (const key in args) {
      const lowerKey = key.toLowerCase();
      if (lowerKey === 'id' || lowerKey.endsWith('id')) {
        const value = args[key];
        // Return the ID if it's a non-empty string or number
        if (value !== undefined && value !== null && value !== '') {
          return String(value);
        }
      }
    }
    
    return undefined;
  };
  
  const resourceIds = toolCalls.map(c => getResourceId(c?.arguments));
  
  // All must have resource IDs
  if (!resourceIds.every(id => id !== undefined && id !== null && id !== '')) {
    return false;
  }
  
  // All must be different (no duplicate resources)
  const uniqueIds = new Set(resourceIds);
  if (uniqueIds.size !== resourceIds.length) return false; // Duplicate IDs
  
  // Condition 3: All arguments must be different
  const argStrings = toolCalls.map(c => JSON.stringify(c?.arguments || {}));
  const uniqueArgs = new Set(argStrings);
  if (uniqueArgs.size !== argStrings.length) return false; // Duplicate operations
  
  return true; // Safe to parallelize
}

/* ---------- MCP (Streamable HTTP) ---------- */
export type MCPAuthConfig = {
  type: 'oauth' | 'bearer';
  token?: string;           // For bearer auth: direct token
  clientId?: string;        // For OAuth: client credentials
  clientSecret?: string;
  tokenEndpoint?: string;   // OAuth token endpoint (for OAuth)
  scope?: string;           // OAuth scope (optional, some servers require it)
  refreshToken?: string;    // For OAuth: refresh token to automatically renew expired access tokens
};

export type MCPHandle = { 
  listTools: () => Promise<{ tools: Array<{ name: string; description?: string; inputSchema?: any }> }>;
  callTool: (name: string, args: Record<string, any>) => Promise<any>;
  id: string; 
  url: string; 
  auth?: MCPAuthConfig;
  transport?: 'http' | 'stdio';
  process?: ChildProcess;
  cleanup?: () => Promise<void>;
};

export type MCPStdioConfig = {
  command: string;
  args?: string[];
  env?: Record<string, string>;
  cwd?: string;
};

/**
 * Connect to an MCP (Model Context Protocol) server via HTTP.
 * Supports connection pooling, OAuth/Bearer authentication, and automatic reconnection.
 * 
 * @param url - HTTP endpoint URL for the MCP server (e.g., "http://localhost:3000/mcp")
 * @param options - Optional authentication configuration (OAuth 2.1 or Bearer token)
 * @returns MCPHandle for listing and calling tools
 * 
 * @example
 * // Basic usage
 * const weather = mcp("http://localhost:3000/mcp");
 * const tools = await weather.listTools();
 * const forecast = await weather.callTool("get_forecast", { city: "San Francisco" });
 * 
 * @example
 * // With OAuth authentication
 * const github = mcp("https://api.github.com/mcp", {
 *   auth: {
 *     type: 'oauth',
 *     clientId: process.env.GITHUB_CLIENT_ID!,
 *     clientSecret: process.env.GITHUB_SECRET!,
 *     tokenUrl: 'https://github.com/login/oauth/access_token'
 *   }
 * });
 */
export function mcp(url: string, options?: { auth?: MCPAuthConfig }): MCPHandle {
  // Use hash-based ID to keep tool names under OpenAI's 64-char limit
  // Tool names are: ${id}.${toolName}, so short ID = more room for tool names
  const hash = createHash('md5').update(url).digest('hex').substring(0, 8);
  const id = `mcp_${hash}`; // e.g., "mcp_f3c8a9b1" (12 chars, deterministic)
  
  return { 
    id, 
    url, 
    auth: options?.auth,
    transport: 'http',
    listTools: async () => {
      return withMCP({ id, url, auth: options?.auth, transport: 'http' } as MCPHandle, (c) => c.listTools());
    },
    callTool: async (name, args) => {
      return withMCP({ id, url, auth: options?.auth, transport: 'http' } as MCPHandle, (c) => c.callTool({ name, arguments: args }));
    }
  };
}

/**
 * Connect to an MCP (Model Context Protocol) server via stdio.
 * Spawns a child process to run the MCP server and communicates via stdin/stdout.
 * 
 * @param config - Configuration for the stdio MCP server
 * @returns MCPHandle for listing and calling tools, plus cleanup function
 * 
 * @example
 * // Basic usage
 * const aha = mcpStdio({
 *   command: "node",
 *   args: ["dist/index.js"],
 *   cwd: "/path/to/aha-mcp"
 * });
 * 
 * @example
 * // With environment variables
 * const aha = mcpStdio({
 *   command: "npx",
 *   args: ["-y", "@aha-develop/aha-mcp"],
 *   env: {
 *     AHA_DOMAIN: "yourcompany.aha.io",
 *     AHA_API_KEY: process.env.AHA_API_KEY!
 *   }
 * });
 * 
 * // Remember to cleanup when done
 * await aha.cleanup?.();
 */
export function mcpStdio(config: MCPStdioConfig): MCPHandle {
  const hash = createHash('md5')
    .update(`${config.command}:${config.args?.join(':')}:${config.cwd || ''}`)
    .digest('hex')
    .substring(0, 8);
  const id = `mcp_${hash}`;
  
  // Unique identifier for this stdio server
  const stdioKey = `stdio:${id}`;
  
  const handle: MCPHandle = {
    id,
    url: stdioKey,
    transport: 'stdio',
    listTools: async () => {
      return withMCPStdio(handle, config, (c) => c.listTools());
    },
    callTool: async (name, args) => {
      return withMCPStdio(handle, config, (c) => c.callTool({ name, arguments: args }));
    },
    cleanup: async () => {
      await cleanupStdioServer(stdioKey);
      STDIO_CONFIGS.delete(stdioKey);
    }
  };
  
  // Register the config so discoverTools can use it
  registerStdioConfig(handle, config);
  
  return handle;
}

// Ajv validator instance
const ajv = new Ajv({ allErrors: true, strict: false });
const VALIDATOR_CACHE = new WeakMap<object, any>();
function validateWithSchema(schema: any | undefined, args: any, context: string) {
  if (!schema || typeof schema !== 'object') return; // nothing to validate
  let validate = VALIDATOR_CACHE.get(schema);
  if (!validate) {
    validate = ajv.compile(schema);
    VALIDATOR_CACHE.set(schema, validate);
  }
  const ok = validate(args);
  if (!ok) {
    const msg = (validate.errors || []).map((e: any) => `${e.instancePath || e.schemaPath}: ${e.message}`).join('; ');
    throw new Error(`${context} arguments failed schema validation: ${msg}`);
  }
}
export function __internal_validateToolArgs(schema: any, args: any) { validateWithSchema(schema, args, 'test'); }

type MCPPoolEntry = {
  client: MCPClient;
  transport: StreamableHTTPClientTransport;
  lastUsed: number;
  busyCount: number;
  auth?: MCPAuthConfig;
};

type MCPStdioPoolEntry = {
  client: MCPClient;
  transport: StdioClientTransport;
  process: ChildProcess;
  lastUsed: number;
  busyCount: number;
  config: MCPStdioConfig;
};

const MCP_POOL = new Map<string, MCPPoolEntry>();
const MCP_STDIO_POOL = new Map<string, MCPStdioPoolEntry>();
let MCP_POOL_MAX = CONSTANTS.DEFAULT_MCP_POOL_MAX_SIZE;
let MCP_POOL_IDLE_MS = CONSTANTS.DEFAULT_MCP_POOL_IDLE_MS;

// OAuth token cache: endpoint -> { token, expiresAt }
type TokenCacheEntry = { token: string; expiresAt: number };
const OAUTH_TOKEN_CACHE = new Map<string, TokenCacheEntry>();

async function getOAuthToken(auth: MCPAuthConfig, endpoint: string): Promise<string> {
  const cached = OAUTH_TOKEN_CACHE.get(endpoint);
  if (cached && cached.expiresAt > Date.now() + CONSTANTS.OAUTH_TOKEN_EXPIRY_BUFFER_MS) {
    return cached.token;
  }
  
  if (!auth.tokenEndpoint) {
    throw new Error(`OAuth auth requires tokenEndpoint`);
  }
  
  let params: URLSearchParams;
  
  if (auth.refreshToken) {
    params = new URLSearchParams({
      grant_type: 'refresh_token',
      refresh_token: auth.refreshToken,
      client_id: auth.clientId || '',
      client_secret: auth.clientSecret || ''
    });
  } else {
    if (!auth.clientId || !auth.clientSecret) {
      throw new Error(`OAuth auth requires clientId and clientSecret`);
    }
    
    params = new URLSearchParams({
      grant_type: 'client_credentials',
      client_id: auth.clientId,
      client_secret: auth.clientSecret
    });
    
    if (auth.scope) {
      params.set('scope', auth.scope);
    }
  }
  
  const response = await fetch(auth.tokenEndpoint, {
    method: 'POST',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    body: params.toString()
  });
  
  if (!response.ok) {
    const errorText = await response.text();
    throw new Error(`OAuth token ${auth.refreshToken ? 'refresh' : 'acquisition'} failed: ${response.status} ${errorText}`);
  }
  
  const data = await response.json();
  const token = data.access_token;
  const expiresIn = data.expires_in || 3600;
  
  OAUTH_TOKEN_CACHE.set(endpoint, {
    token,
    expiresAt: Date.now() + (expiresIn * 1000)
  });
  
  return token;
}


async function getPooledClient(url: string, auth?: MCPAuthConfig): Promise<MCPPoolEntry> {
  const poolKey = auth ? `${url}::auth` : url; // Separate pool entries for auth vs non-auth
  let entry = MCP_POOL.get(poolKey);
  
  if (entry) {
    // Reusing existing connection - just update metadata
    entry.busyCount++;
    entry.lastUsed = Date.now();
    return entry;
  }
  
  // No existing connection - create new one
  // Evict LRU idle if over max
  if (MCP_POOL.size >= MCP_POOL_MAX) {
    const idleEntries = Array.from(MCP_POOL.entries()).filter(([, e]) => e.busyCount === 0);
    idleEntries.sort((a, b) => a[1].lastUsed - b[1].lastUsed);
    const toEvict = idleEntries.slice(0, Math.max(0, MCP_POOL.size - MCP_POOL_MAX + 1));
    for (const [k, e] of toEvict) {
      try { 
        await e.client.close();
        if (e.transport && typeof e.transport.close === 'function') {
          await e.transport.close();
        }
      } catch {}
      MCP_POOL.delete(k);
    }
  }
  
  // Create transport
  const transport = new StreamableHTTPClientTransport(new URL(url));
  
  const client = new MCPClient({ name: "volcano-sdk", version: "0.0.1" });
  
  // Connect with auth if needed
  if (auth) {
    await connectWithAuth(transport, client, auth, url);
  } else {
    await client.connect(transport);
  }
  
  entry = { client, transport, lastUsed: Date.now(), busyCount: 1, auth };
  MCP_POOL.set(poolKey, entry);
  return entry;
}

async function connectWithAuth(transport: any, client: MCPClient, auth: MCPAuthConfig, endpoint: string) {
  const getAuthHeaders = async () => {
    const headers: Record<string, string> = {};
    
    if (auth.type === 'oauth') {
      const token = await getOAuthToken(auth, endpoint);
      headers['Authorization'] = `Bearer ${token}`;
    } else if (auth.type === 'bearer') {
      if (auth.refreshToken && auth.tokenEndpoint) {
        const token = await getOAuthToken(auth, endpoint);
        headers['Authorization'] = `Bearer ${token}`;
      } else if (auth.token) {
        headers['Authorization'] = `Bearer ${auth.token}`;
      }
    }
    
    return headers;
  };
  
  const authHeaders = await getAuthHeaders();
  
  const originalFetch = global.fetch;
  global.fetch = async (url: any, init: any = {}) => {
    let mergedHeaders: any = {};
    if (init.headers) {
      if (init.headers instanceof Headers) {
        init.headers.forEach((value: string, key: string) => {
          mergedHeaders[key] = value;
        });
      } else {
        mergedHeaders = { ...init.headers };
      }
    }
    Object.assign(mergedHeaders, authHeaders);
    
    return originalFetch(url, {
      ...init,
      headers: mergedHeaders
    });
  };
  
  try {
    await client.connect(transport);
  } finally {
    global.fetch = originalFetch;
  }
}


async function cleanupIdlePool() {
  const now = Date.now();
  for (const [url, entry] of MCP_POOL) {
    if (entry.busyCount === 0 && now - entry.lastUsed > MCP_POOL_IDLE_MS) {
      try { await entry.client.close(); } catch {}
      MCP_POOL.delete(url);
    }
  }
  // Also cleanup stdio servers
  for (const [key, entry] of MCP_STDIO_POOL) {
    if (entry.busyCount === 0 && now - entry.lastUsed > MCP_POOL_IDLE_MS) {
      try { 
        await entry.client.close(); 
        entry.process.kill();
      } catch {}
      MCP_STDIO_POOL.delete(key);
    }
  }
}

// Periodic cleanup
let POOL_SWEEPER: any = undefined;
function ensurePoolSweeper() {
  // Don't start sweeper in test mode to avoid closing connections during tests
  if (process.env.NODE_ENV === 'test' || process.env.VITEST) {
    return;
  }
  
  if (!POOL_SWEEPER) {
    POOL_SWEEPER = setInterval(() => { cleanupIdlePool(); }, CONSTANTS.DEFAULT_MCP_POOL_SWEEP_INTERVAL_MS);
    // In tests or short-lived processes we don't need to keep the event loop alive
    if (typeof POOL_SWEEPER.unref === 'function') POOL_SWEEPER.unref();
  }
}

// Stdio MCP server management
async function getPooledStdioClient(key: string, config: MCPStdioConfig): Promise<MCPStdioPoolEntry> {
  let entry = MCP_STDIO_POOL.get(key);
  if (!entry) {
    // Spawn the process with merged environment variables
    const env = {
      ...process.env,
      ...config.env
    };
    
    const childProcess = spawn(config.command, config.args || [], {
      cwd: config.cwd,
      env,
      stdio: ['pipe', 'pipe', 'pipe']
    });
    
    // Handle process errors
    childProcess.on('error', (err) => {
      console.error(`MCP stdio process error: ${err.message}`);
    });
    
    // Create stdio transport
    const transport = new StdioClientTransport({
      command: config.command,
      args: config.args,
      env: config.env
    });
    
    const client = new MCPClient({ name: "volcano-sdk", version: "0.0.1" });
    
    // Connect to the spawned process
    await client.connect(transport);
    
    entry = { 
      client, 
      transport, 
      process: childProcess, 
      lastUsed: Date.now(), 
      busyCount: 0,
      config 
    };
    MCP_STDIO_POOL.set(key, entry);
  }
  entry.busyCount++;
  entry.lastUsed = Date.now();
  return entry;
}

async function cleanupStdioServer(key: string) {
  const entry = MCP_STDIO_POOL.get(key);
  if (entry) {
    try {
      await entry.client.close();
      entry.process.kill();
    } catch (err) {
      console.error(`Error cleaning up stdio server: ${err}`);
    }
    MCP_STDIO_POOL.delete(key);
  }
}

async function withMCPStdio<T>(h: MCPHandle, config: MCPStdioConfig, fn: (c: MCPClient) => Promise<T>, telemetry?: any, operation?: string): Promise<T> {
  ensurePoolSweeper();
  const entry = await getPooledStdioClient(h.url, config);
  
  // Start MCP span if telemetry configured
  const mcpSpan = telemetry && operation ? telemetry.startMCPSpan(null, h, operation) : null;
  
  try {
    const result = await fn(entry.client);
    
    telemetry?.endSpan(mcpSpan);
    telemetry?.recordMetric('mcp.call', 1, { endpoint: h.url, error: false });
    
    return result;
  } catch (error) {
    telemetry?.endSpan(mcpSpan, undefined, error);
    telemetry?.recordMetric('mcp.call', 1, { endpoint: h.url, error: true });
    throw error;
  } finally {
    entry.busyCount = Math.max(0, entry.busyCount - 1);
    entry.lastUsed = Date.now();
  }
}

// Store stdio configs for handles that need them
const STDIO_CONFIGS = new Map<string, MCPStdioConfig>();

function registerStdioConfig(handle: MCPHandle, config: MCPStdioConfig) {
  STDIO_CONFIGS.set(handle.url, config);
}

function getStdioConfig(handle: MCPHandle): MCPStdioConfig | undefined {
  return STDIO_CONFIGS.get(handle.url);
}

// Helper to route to the correct withMCP variant based on transport
async function withMCPAny<T>(
  handle: MCPHandle, 
  fn: (c: MCPClient) => Promise<T>, 
  telemetry?: any, 
  operation?: string
): Promise<T> {
  if (handle.transport === 'stdio') {
    const config = getStdioConfig(handle);
    if (!config) {
      throw new Error(`Stdio config not found for handle ${handle.id}`);
    }
    return withMCPStdio(handle, config, fn, telemetry, operation);
  } else {
    return withMCP(handle, fn, telemetry, operation);
  }
}

// Internal test helpers
export function __internal_getMcpPoolStats() {
  return {
    size: MCP_POOL.size,
    entries: Array.from(MCP_POOL.entries()).map(([url, e]) => ({ url, busyCount: e.busyCount, lastUsed: e.lastUsed }))
  };
}
export async function __internal_forcePoolCleanup() { await cleanupIdlePool(); }
export async function __internal_clearAllPools() {
  // Close all HTTP MCP connections
  for (const [, entry] of MCP_POOL) {
    try { await entry.client.close(); } catch {}
  }
  MCP_POOL.clear();
  
  // Close all stdio MCP connections
  for (const [, entry] of MCP_STDIO_POOL) {
    try { 
      await entry.client.close();
      entry.process.kill();
    } catch {}
  }
  MCP_STDIO_POOL.clear();
}
export function __internal_setPoolConfig(max: number, idleMs: number) { MCP_POOL_MAX = max; MCP_POOL_IDLE_MS = idleMs; }
export function __internal_clearOAuthTokenCache() { OAUTH_TOKEN_CACHE.clear(); }
export function __internal_getOAuthTokenCache() { 
  return Array.from(OAUTH_TOKEN_CACHE.entries()).map(([endpoint, entry]) => ({ 
    endpoint, 
    token: entry.token, 
    expiresAt: entry.expiresAt 
  })); 
}

async function withMCP<T>(h: MCPHandle, fn: (c: MCPClient) => Promise<T>, telemetry?: any, operation?: string): Promise<T> {
  ensurePoolSweeper();
  const poolKey = h.auth ? `${h.url}::auth` : h.url;
  const entry = await getPooledClient(h.url, h.auth);
  
  // Start MCP span if telemetry configured
  const mcpSpan = telemetry && operation ? telemetry.startMCPSpan(null, h, operation) : null;
  
  try {
    let result: T;
    // Always wrap with auth if configured (for both connect and tool calls)
    if (h.auth || entry.auth) {
      const authConfig = h.auth || entry.auth!;
      result = await executeWithAuth(authConfig, h.url, () => fn(entry.client));
    } else {
      result = await fn(entry.client);
    }
    
    telemetry?.endSpan(mcpSpan);
    telemetry?.recordMetric('mcp.call', 1, { endpoint: h.url, error: false });
    
    return result;
  } catch (error) {
    telemetry?.endSpan(mcpSpan, undefined, error);
    telemetry?.recordMetric('mcp.call', 1, { endpoint: h.url, error: true });
    throw error;
  } finally { 
    const e = MCP_POOL.get(poolKey);
    if (e) {
      e.busyCount = Math.max(0, e.busyCount - 1);
      e.lastUsed = Date.now();
    }
  }
}

async function executeWithAuth<T>(auth: MCPAuthConfig, endpoint: string, fn: () => Promise<T>): Promise<T> {
  const getAuthHeaders = async () => {
    const headers: Record<string, string> = {};
    
    if (auth.type === 'oauth') {
      const token = await getOAuthToken(auth, endpoint);
      headers['Authorization'] = `Bearer ${token}`;
    } else if (auth.type === 'bearer') {
      if (auth.refreshToken && auth.tokenEndpoint) {
        const token = await getOAuthToken(auth, endpoint);
        headers['Authorization'] = `Bearer ${token}`;
      } else if (auth.token) {
        headers['Authorization'] = `Bearer ${auth.token}`;
      }
    }
    
    return headers;
  };
  
  let authHeaders = await getAuthHeaders();
  
  const originalFetch = global.fetch;
  global.fetch = async (url: any, init: any = {}) => {
    let mergedHeaders: any = {};
    if (init.headers) {
      if (init.headers instanceof Headers) {
        init.headers.forEach((value: string, key: string) => {
          mergedHeaders[key] = value;
        });
      } else {
        mergedHeaders = { ...init.headers };
      }
    }
    Object.assign(mergedHeaders, authHeaders);
    
    const response = await originalFetch(url, {
      ...init,
      headers: mergedHeaders
    });
    
    if (response.status === 401 && auth.refreshToken && auth.tokenEndpoint) {
      OAUTH_TOKEN_CACHE.delete(endpoint);
      authHeaders = await getAuthHeaders();
      
      Object.assign(mergedHeaders, authHeaders);
      return await originalFetch(url, {
        ...init,
        headers: mergedHeaders
      });
    }
    
    return response;
  };
  
  try {
    return await fn();
  } finally {
    global.fetch = originalFetch;
  }
}

function sleep(ms: number) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

function withTimeout<T>(promise: Promise<T>, ms: number, label = 'Step'): Promise<T> {
  let timer: any;
  const timeout = new Promise<never>((_, reject) => {
    timer = setTimeout(() => reject(new Error(`${label} timed out after ${ms}ms`)), ms);
  });
  return Promise.race([promise, timeout]).finally(() => clearTimeout(timer));
}

// Tool discovery cache for automatic selection
const TOOL_CACHE = new Map<string, { tools: ToolDefinition[]; ts: number }>();
let TOOL_CACHE_TTL_MS = CONSTANTS.DEFAULT_TOOL_CACHE_TTL_MS;

/**
 * Discover all available tools from one or more MCP servers.
 * Results are cached for 60 seconds to improve performance.
 * 
 * @param handles - Array of MCP handles to query for tools
 * @returns Combined array of all available tools from all servers
 * 
 * @example
 * const weather = mcp("http://localhost:3000/mcp");
 * const calendar = mcp("http://localhost:4000/mcp");
 * const tools = await discoverTools([weather, calendar]);
 * console.log(tools.map(t => t.name)); // ["get_forecast", "create_event", ...]
 */
export async function discoverTools(handles: MCPHandle[]): Promise<ToolDefinition[]> {
  const allTools: ToolDefinition[] = [];
  
  for (const handle of handles) {
    try {
      const cached = TOOL_CACHE.get(handle.url);
      if (cached && (Date.now() - cached.ts) < TOOL_CACHE_TTL_MS) {
        // reuse cached with endpoint-specific names
        allTools.push(...cached.tools);
        continue;
      }
      
      const fetchFn = async (client: MCPClient) => {
        const result = await client.listTools();
        const mapped = result.tools.map(tool => ({
          name: `${handle.id}.${tool.name}`,
          description: tool.description || `Tool: ${tool.name}`,
          parameters: tool.inputSchema || { type: "object", properties: {} },
          mcpHandle: handle,
        }));
        TOOL_CACHE.set(handle.url, { tools: mapped, ts: Date.now() });
        return mapped;
      };
      
      let tools: ToolDefinition[];
      if (handle.transport === 'stdio') {
        const config = getStdioConfig(handle);
        if (!config) {
          throw new Error(`Stdio config not found for handle ${handle.id}`);
        }
        tools = await withMCPStdio(handle, config, fetchFn);
      } else {
        tools = await withMCP(handle, fetchFn);
      }
      
      allTools.push(...tools);
    } catch (error) {
      // Invalidate cache on failure
      TOOL_CACHE.delete(handle.url);
      // Fail fast - throw connection error
      throw normalizeError(error, 'mcp-conn', { 
        provider: classifyProviderFromMcp(handle),
        retryable: true  // Connection errors are retryable
      });
    }
  }
  
  return allTools;
}

export function __internal_clearDiscoveryCache() { TOOL_CACHE.clear(); }
export function __internal_setDiscoveryTtl(ms: number) { TOOL_CACHE_TTL_MS = ms; }
export function __internal_primeDiscoveryCache(handle: MCPHandle, rawTools: Array<{ name: string; inputSchema?: any; description?: string }>) {
  const tools: ToolDefinition[] = rawTools.map(t => ({
    name: `${handle.id}.${t.name}`,
    description: t.description || `Tool: ${t.name}`,
    parameters: t.inputSchema || { type: 'object', properties: {} },
    mcpHandle: handle,
  }));
  TOOL_CACHE.set(handle.url, { tools, ts: Date.now() });
}

// helper to fetch tool schema for explicit calls
async function getToolSchema(handle: MCPHandle, toolName: string): Promise<any | undefined> {
  const cached = TOOL_CACHE.get(handle.url);
  if (cached) {
    const found = cached.tools.find(t => t.name === `${handle.id}.${toolName}`);
    return found?.parameters;
  }
  try {
    const fetchFn = async (client: MCPClient) => {
      const result = await client.listTools();
      const mapped = result.tools.map(tool => ({
        name: `${handle.id}.${tool.name}`,
        description: tool.description || `Tool: ${tool.name}`,
        parameters: tool.inputSchema || { type: 'object', properties: {} },
        mcpHandle: handle,
      }));
      TOOL_CACHE.set(handle.url, { tools: mapped, ts: Date.now() });
      return mapped;
    };
    
    let tools: ToolDefinition[];
    if (handle.transport === 'stdio') {
      const config = getStdioConfig(handle);
      if (!config) {
        throw new Error(`Stdio config not found for handle ${handle.id}`);
      }
      tools = await withMCPStdio(handle, config, fetchFn);
    } else {
      tools = await withMCP(handle, fetchFn);
    }
    
    const found = tools.find(t => t.name === `${handle.id}.${toolName}`);
    return found?.parameters;
  } catch {
    return undefined;
  }
}

/* ---------- Agent chain ---------- */
export type RetryConfig = {
  delay?: number;      // seconds to wait before each retry (mutually exclusive with backoff)
  backoff?: number;    // exponential factor, waits 1s, factor^n each retry
  retries?: number;    // total attempts including the first one; default 3
};

/**
 * Metadata provided to run-level onToken callback.
 * Allows conditional processing based on whether step-level handler already processed the token.
 * 
 * @property stepIndex - Index of the current step (0-based)
 * @property handledByStep - True if step-level onToken handled this token. Use this to avoid double-processing.
 * @property stepPrompt - The prompt for this step (useful for conditional formatting)
 * @property llmProvider - The LLM provider ID (e.g., "OpenAI-gpt-4o-mini", "Anthropic-claude-3")
 * 
 * @example
 * .run({
 *   onToken: (token, meta) => {
 *     if (!meta.handledByStep) {
 *       // Only process if step didn't handle it
 *       res.write(`data: ${token}\n\n`);
 *     }
 *     // Always log analytics
 *     analytics.track(token, meta.stepIndex);
 *   }
 * })
 */
export type TokenMetadata = {
  stepIndex: number;
  handledByStep: boolean;
  stepPrompt?: string;
  llmProvider?: string;
};

/**
 * Options for the run() method.
 * Supports both token-level and step-level callbacks for maximum flexibility.
 * 
 * @property onToken - Called for each token as it arrives (with metadata). 
 *                     Step-level onToken takes precedence - when a step has its own onToken,
 *                     this callback won't receive tokens from that step (meta.handledByStep will be true).
 * @property onStep - Called when each step completes. Equivalent to the callback in run(callback).
 * 
 * @example
 * // Both token and step callbacks
 * .run({
 *   onToken: (token, meta) => {
 *     console.log(`Token from step ${meta.stepIndex}: ${token}`);
 *   },
 *   onStep: (step, index) => {
 *     console.log(`Step ${index} complete: ${step.durationMs}ms`);
 *   }
 * })
 * 
 * @example
 * // Backward compatible: just a callback
 * .run((step, index) => {
 *   console.log(`Step ${index} done`);
 * })
 */
export type StreamOptions = {
  onToken?: (token: string, meta: TokenMetadata) => void;
  onStep?: (step: StepResult, stepIndex: number) => void;
};

export type Step =
  | { prompt: string; name?: string; llm?: LLMHandle; instructions?: string; timeout?: number; retry?: RetryConfig; contextMaxChars?: number; contextMaxToolResults?: number; pre?: () => void; post?: () => void; onToken?: (token: string) => void }
  | { mcp: MCPHandle; name?: string; tool: string; args?: Record<string, any>; timeout?: number; retry?: RetryConfig; contextMaxChars?: number; contextMaxToolResults?: number; pre?: () => void; post?: () => void; onToolCall?: (toolName: string, args: any, result: any) => void }
  | { prompt: string; name?: string; llm?: LLMHandle; mcp: MCPHandle; tool: string; args?: Record<string, any>; instructions?: string; timeout?: number; retry?: RetryConfig; contextMaxChars?: number; contextMaxToolResults?: number; pre?: () => void; post?: () => void; onToolCall?: (toolName: string, args: any, result: any) => void }
  | { prompt: string; name?: string; llm?: LLMHandle; mcps: MCPHandle[]; instructions?: string; timeout?: number; retry?: RetryConfig; contextMaxChars?: number; contextMaxToolResults?: number; maxToolIterations?: number; pre?: () => void; post?: () => void; onToken?: (token: string) => void; onToolCall?: (toolName: string, args: any, result: any) => void }
  | { prompt: string; name?: string; llm?: LLMHandle; agents: AgentBuilder[]; instructions?: string; timeout?: number; retry?: RetryConfig; contextMaxChars?: number; contextMaxToolResults?: number; pre?: () => void; post?: () => void };

export type StepResult = {
  prompt?: string;
  llmOutput?: string;
  // Total wall time for this step (successful attempt) in milliseconds
  durationMs?: number;
  // Total LLM time spent during this step (sum across iterations) in milliseconds
  llmMs?: number;
  mcp?: { endpoint: string; tool: string; result: any; ms?: number };
  toolCalls?: Array<{ name: string; arguments?: Record<string, any>; endpoint: string; result: any } & { ms?: number }>;
  // Parallel execution results (for parallel steps)
  parallel?: Record<string, StepResult>;
  parallelResults?: StepResult[];
  // Aggregated metrics (populated on the final step of a run)
  totalDurationMs?: number;
  totalLlmMs?: number;
  totalMcpMs?: number;
};

// Internal metadata added to StepResult for tracking
interface StepResultInternal extends StepResult {
  __tokenCount?: number;
  __provider?: string;
  __crewTotalTokens?: number;
  agentCalls?: Array<{
    name: string;
    task: string;
    tokens: number;
    ms: number;
    result?: string;
  }>;
}

export interface AgentResults extends Array<StepResult> {
  ask(llm: LLMHandle, question: string): Promise<string>;
  summary(llm: LLMHandle): Promise<string>;
  toolsUsed(llm: LLMHandle): Promise<string>;
  errors(llm: LLMHandle): Promise<string>;
}

// Extended LLM handle with internal methods
interface LLMHandleInternal {
  id?: string;
  model: string;
  client: any;
  gen: (prompt: string) => Promise<string>;
  genWithTools: (prompt: string, tools: any[]) => Promise<any>;
  genStream?: (prompt: string) => AsyncGenerator<string, void, unknown>;
  getUsage?(): { total_tokens?: number; totalTokens?: number; prompt_tokens?: number; completion_tokens?: number } | undefined;
}

// Extended Step type with internal pattern properties (not actually extending Step to avoid union issues)
interface StepInternal {
  __parallel?: any;
  __branch?: { condition: (history: StepResult[]) => boolean; branches: any };
  __switch?: { selector: (history: StepResult[]) => string; cases: any };
  __while?: { condition: (history: StepResult[]) => boolean; body: any; opts?: any };
  __forEach?: { items: any[]; body: any };
  __retryUntil?: { body: any; successCondition: (result: StepResult) => boolean; opts?: any };
  __runAgent?: { subAgent: AgentBuilder };
  __hooks?: { pre?: () => void; post?: () => void };
  __reset?: boolean;
}

// Extended AgentBuilder with internal methods
interface AgentBuilderInternal extends AgentBuilder {
  _getOpts?(): AgentOptions;
  _getSteps?(): Step[];
  __isSubAgent?: boolean;
  __isExplicitSubAgent?: boolean;
  __parentAgentName?: string;
  __parentStepIndex?: number;
  __parentTotalSteps?: number;
  __parentContext?: StepResult[];
}

type StepFactory = (history: StepResult[]) => Step;

function enhanceResults(results: StepResult[]): AgentResults {
  const enhanced = results as AgentResults;
  
  const buildContext = (results: StepResult[]): string => {
    const context: string[] = [];
    
    // Track agent delegations from prompts
    const agentDelegations: { step: number; agentName: string; task: string }[] = [];
    
    results.forEach((step, idx) => {
      context.push(`Step ${idx + 1}:`);
      
      // Check if this step involved agent delegation
      if (step.prompt) {
        context.push(`  Prompt: ${step.prompt}`);
        
        // Check if LLM output contains agent delegation patterns
        if (step.llmOutput) {
          // Look for coordinator's USE agent pattern
          const useMatch = step.llmOutput.match(/USE\s+(\w+):\s*([^\n]+)/);
          if (useMatch) {
            agentDelegations.push({ 
              step: idx + 1, 
              agentName: useMatch[1], 
              task: useMatch[2].trim() 
            });
            context.push(`  Delegated to Agent: ${useMatch[1]}`);
            context.push(`  Delegation Task: ${useMatch[2].trim()}`);
          }
          
          // Check for completed agent delegations
          const delegationMatch = step.prompt.match(/Agent (\w+) was delegated: ([^\n]+)$/);
          if (delegationMatch) {
            context.push(`  Agent Used: ${delegationMatch[1]}`);
            context.push(`  Agent Task: ${delegationMatch[2]}`);
          }
        }
      }
      
      if (step.llmOutput) context.push(`  LLM Output: ${step.llmOutput}`);
      if (step.toolCalls && step.toolCalls.length > 0) {
        context.push(`  Tools Called (${step.toolCalls.length}):`);
        step.toolCalls.forEach(tc => {
          context.push(`    - ${tc.name}: ${JSON.stringify(tc.arguments || {})}`);
          context.push(`      Result: ${JSON.stringify(tc.result)}`);
        });
      }
      if (step.mcp) {
        context.push(`  MCP Tool: ${step.mcp.tool}`);
        context.push(`  Result: ${JSON.stringify(step.mcp.result)}`);
      }
      if (step.durationMs) context.push(`  Duration: ${step.durationMs}ms`);
      
      // Check for crew total tokens (indicates multi-agent coordination)
      const stepInternal = step as StepResultInternal;
      if (stepInternal.__crewTotalTokens) {
        context.push(`  Total Crew Tokens: ${stepInternal.__crewTotalTokens}`);
      }
      
      // Check for agent calls (multi-agent delegation results)
      if (stepInternal.agentCalls && stepInternal.agentCalls.length > 0) {
        context.push(`  Agents Used:`);
        stepInternal.agentCalls.forEach((call) => {
          context.push(`    - ${call.name}: ${call.task}`);
          context.push(`      Tokens: ${call.tokens}`);
          context.push(`      Duration: ${call.ms}ms`);
        });
      }
      
      context.push('');
    });
    
    // Add summary of agent delegations if any
    if (agentDelegations.length > 0) {
      context.push('\nAgent Delegation Summary:');
      agentDelegations.forEach(d => {
        context.push(`  - Step ${d.step}: ${d.agentName} agent (task: "${d.task}")`);
      });
      context.push('');
    }
    
    return context.join('\n');
  };
  
  enhanced.ask = async (llm: LLMHandle, question: string): Promise<string> => {
    const context = buildContext(results);
    const prompt = `You are analyzing the results of an AI agent workflow.

Agent Execution Results:
${context}

User Question: ${question}

Provide a clear, concise answer based on the execution results above. Be specific and reference actual data from the results.`;

    // Create dedicated progress tracker for ask() operation (suppress header)
    const tracker = new ProgressTracker('untitled', false, false, true);
    
    // Log ask init
    tracker.logEvent({
      agent: 'untitled',
      step: 'ask',
      status: 'init',
      message: `answering "${question}"`
    });
    
    const operationStart = Date.now();
    let tokenCount = 0;
    let output = '';
    
    try {
      if (typeof llm.genStream === 'function') {
        const tokens: string[] = [];
        for await (const token of llm.genStream(prompt)) {
          tokens.push(token);
          tokenCount++;
          tracker.updateTokens(tokenCount, getLLMProviderId(llm), 0);
        }
        output = tokens.join('');
      } else {
        output = await llm.gen(prompt);
      }
    } catch (e) {
      throw e;
    }
    
    const duration = Date.now() - operationStart;
    
    // Get token count from usage if available
    const llmInternal = llm as LLMHandleInternal;
    const rawUsage = llmInternal.getUsage?.();
    const usage = normalizeTokenUsage(rawUsage);
    const actualTokens = usage?.totalTokens || tokenCount;
    
    // Log ask complete
    tracker.ensureSpinnerStopped();
    tracker.logEvent({
      agent: 'untitled',
      step: 'ask',
      status: 'complete',
      message: `โœ” Complete | ${tracker.formatMetrics({ tokens: actualTokens, toolCalls: 0, duration, provider: getLLMProviderId(llm) })}`
    });
    
    return output;
  };
  
  enhanced.summary = async (llm: LLMHandle): Promise<string> => {
    return await enhanced.ask(llm, "Provide a brief summary of what the agent accomplished. Include key metrics and outcomes.");
  };
  
  enhanced.toolsUsed = async (llm: LLMHandle): Promise<string> => {
    return await enhanced.ask(llm, "List all the tools that were called and briefly explain what each tool did.");
  };
  
  enhanced.errors = async (llm: LLMHandle): Promise<string> => {
    return await enhanced.ask(llm, "Were there any errors, failures, or issues? If so, explain them. If not, say 'No errors detected.'");
  };
  
  return enhanced;
}

// Agent builder interface for type safety
export interface AgentBuilder {
  name?: string;
  description?: string;
  _getSteps?(): Array<any>;  // Internal: for recursive step counting
  _getOpts?(): AgentOptions | undefined;  // Internal: for creating fresh instances
  resetHistory(): AgentBuilder;
  then(s: Step | StepFactory): AgentBuilder;
  parallel(stepsOrDict: Step[] | Record<string, Step>, hooks?: { pre?: () => void; post?: () => void }): AgentBuilder;
  branch(condition: (history: StepResult[]) => boolean, branches: { true: (agent: AgentBuilder) => AgentBuilder; false: (agent: AgentBuilder) => AgentBuilder }, hooks?: { pre?: () => void; post?: () => void }): AgentBuilder;
  switch<T = string>(selector: (history: StepResult[]) => T, cases: Record<string, (agent: AgentBuilder) => AgentBuilder> & { default?: (agent: AgentBuilder) => AgentBuilder }, hooks?: { pre?: () => void; post?: () => void }): AgentBuilder;
  while(condition: (history: StepResult[]) => boolean, body: (agent: AgentBuilder) => AgentBuilder, opts?: { maxIterations?: number; timeout?: number; pre?: () => void; post?: () => void }): AgentBuilder;
  forEach<T>(items: T[], body: (item: T, agent: AgentBuilder) => AgentBuilder, hooks?: { pre?: () => void; post?: () => void }): AgentBuilder;
  retryUntil(body: (agent: AgentBuilder) => AgentBuilder, successCondition: (result: StepResult) => boolean, opts?: { maxAttempts?: number; backoff?: number; pre?: () => void; post?: () => void }): AgentBuilder;
  runAgent(subAgent: AgentBuilder, hooks?: { pre?: () => void; post?: () => void }): AgentBuilder;
  run(optionsOrLog?: StreamOptions | ((s: StepResult, stepIndex: number) => void)): Promise<AgentResults>;
}

function safeExecuteHook(hook: (() => void) | undefined, hookName: string): void {
  if (!hook) return;
  try {
    hook();
  } catch (e) {
    console.warn(`${hookName} hook failed:`, e);
  }
}

function buildHistoryContextChunked(history: StepResult[], maxToolResults: number, maxChars: number): string {
  if (history.length === 0) return '';
  
  const chunks: string[] = [];
  
  // Make task context clear for delegated agents
  const delegatedTasks = history.filter(h => h.prompt?.includes('was delegated:'));
  if (delegatedTasks.length > 0) {
    chunks.push('\n=== CONTEXT FOR YOUR TASK ===\n');
    // Show the original task first
    const originalTask = history.find(h => h.prompt && !h.prompt.includes('was delegated:'));
    if (originalTask?.prompt) {
      chunks.push(`Overall goal: ${originalTask.prompt}\n\n`);
    }
    
    // Extract and show the specific delegated task
    const lastDelegation = delegatedTasks[delegatedTasks.length - 1];
    const taskMatch = lastDelegation.prompt?.match(/was delegated: (.+)$/);
    if (taskMatch) {
      chunks.push(`Your specific task: ${taskMatch[1]}\n`);
    }
    
    chunks.push('=== END CONTEXT ===\n\n');
  }
  
  // Include LLM outputs from all steps (not just last one)
  // This is important for subagents to see parent conversation history
  // contextMaxChars will truncate if this gets too long
  const llmOutputs: string[] = [];
  for (const step of history) {
    if (step.llmOutput) {
      llmOutputs.push(step.llmOutput);
    }
  }
  
  if (llmOutputs.length > 0) {
    if (llmOutputs.length === 1) {
    chunks.push('Previous LLM answer:\n');
      chunks.push(llmOutputs[0]);
    } else {
      chunks.push('Previous LLM answers:\n');
      llmOutputs.forEach((output, idx) => {
        chunks.push(`${idx + 1}. ${output}\n`);
      });
    }
    chunks.push('\n');
  }
  
  // Collect tool calls from ALL recent steps (not just last step)
  const allToolCalls: Array<{ name: string; arguments?: Record<string, any>; result: any }> = [];
  for (const step of history) {
    if (step.toolCalls && step.toolCalls.length > 0) {
      allToolCalls.push(...step.toolCalls);
    }
  }
  
  if (allToolCalls.length > 0) {
    chunks.push('Previous tool results:\n');
    // Take the most recent maxToolResults across ALL steps
    const recent = allToolCalls.slice(-maxToolResults);
    for (const t of recent) {
      chunks.push('- ');
      chunks.push(t.name);
      // Include arguments to preserve context like issue numbers, IDs, etc
      if (t.arguments) {
        try {
          chunks.push('(');
          chunks.push(JSON.stringify(t.arguments));
          chunks.push(')');
        } catch {
          // Skip if arguments can't be serialized
        }
      }
      chunks.push(' -> ');
      if (typeof t.result === 'string') {
        chunks.push(t.result);
      } else {
        try { chunks.push(JSON.stringify(t.result)); } catch { chunks.push('[unserializable]'); }
      }
      chunks.push('\n');
    }
  }
  
  // prefix header
  chunks.unshift('\n\n[Context from previous steps]\n');
  // assemble with maxChars cap
  let out = '';
  for (const c of chunks) {
    if (out.length + c.length > maxChars) break;
    out += c;
  }
  return out;
}

/**
 * Helper function to execute LLM generation with optional token streaming.
 * Handles both step-level and stream-level onToken callbacks with proper precedence.
 */
async function executeLLMWithStreaming(
  llm: LLMHandle,
  prompt: string,
  stepOnToken: ((token: string) => void) | undefined,
  streamOnToken: ((token: string, meta: TokenMetadata) => void) | undefined,
  meta: { stepIndex: number; stepPrompt?: string },
  progress?: ReturnType<typeof createProgressHandler> | null,
  progressTokenCallback?: () => void
): Promise<string> {
  const hasStepOnToken = !!stepOnToken;
  const hasStreamOnToken = !!streamOnToken;
  const hasUserCallback = hasStepOnToken || hasStreamOnToken;
  
  // Use streaming if we have any callback OR if genStream is available
  if (typeof llm.genStream === 'function' && (hasUserCallback || progressTokenCallback || progress)) {
    const tokens: string[] = [];
    const tokenMeta: TokenMetadata = {
      stepIndex: meta.stepIndex,
      handledByStep: hasStepOnToken,
      stepPrompt: meta.stepPrompt,
      llmProvider: (llm as LLMHandleInternal).id || llm.model
    };
    
    for await (const token of llm.genStream(prompt)) {
      tokens.push(token);
      
      // Call progress token counter
      if (progressTokenCallback) {
        progressTokenCallback();
      }
      
      
      // Call user callbacks
      try {
        if (hasStepOnToken) {
          stepOnToken!(token);
        } else if (hasStreamOnToken) {
          streamOnToken!(token, tokenMeta);
        }
      } catch (e) {
        console.warn('onToken callback failed:', e);
      }
    }
    return tokens.join('');
  } else {
    return await llm.gen(prompt);
  }
}



/**
 * Centralized Progress Tracker
 * Encapsulates all progress display logic with structured logging and ora spinners.
 */
class ProgressTracker {
  private workflowStart: number;
  private isTTY: boolean;
  private operationStart: number;
  private spinner: ReturnType<typeof ora> | null = null;
  private currentStepIndex = 0;
  private agentDisplayName: string;
  
  constructor(
    agentName?: string,
    private isSubAgent = false,
    private isExplicitSubAgent = false,
    private suppressHeader = false
  ) {
    this.workflowStart = Date.now();
    this.isTTY = process.stdout?.isTTY || false;
    this.operationStart = Date.now();
    this.agentDisplayName = agentName || 'untitled';
    
    if (!isSubAgent && !suppressHeader) {
      this.logEvent({
        agent: this.agentDisplayName,
        status: 'init',
        message: `๐ŸŒ‹ running Volcano agent [volcano-sdk v${CONSTANTS.VOLCANO_SDK_VERSION}] โ€ข docs at https://volcano.dev`
      });
    }
  }
  
  // Utility: Format ISO timestamp
  timestamp(): string {
    return new Date().toISOString();
  }
  
  // Utility: Format structured log line
  logLine(opts: {
    agent: string;
    step?: number | string;
    status: 'init' | 'complete' | 'coordinating';
    message: string;
  }): string {
    const timestamp = this.timestamp();
    const agentPart = ` agent="${opts.agent}"`;
    const stepPart = opts.step !== undefined ? ` step=${opts.step}` : '';
    const statusPart = ` status=${opts.status}`;
    
    return `[${timestamp}${agentPart}${stepPart}${statusPart}] ${opts.message}`;
  }
  
  // Utility: Clear spinner (if active) and print structured log
  logEvent(opts: {
    agent: string;
    step?: number | string;
    status: 'init' | 'complete' | 'coordinating';
    message: string;
  }): void {
    this.stopSpinner();
    console.log(this.logLine(opts));
  }

  // Utility: Format metrics message
  formatMetrics(opts: {
    tokens?: number;
    toolCalls?: number;
    duration: number;
    provider?: string;
  }): string {
    const parts: string[] = [];
    
    if (opts.tokens !== undefined) {
      parts.push(`${opts.tokens.toLocaleString()} token${opts.tokens !== 1 ? 's' : ''}`);
    }
    
    if (opts.toolCalls !== undefined) {
      parts.push(`${opts.toolCalls} tool call${opts.toolCalls !== 1 ? 's' : ''}`);
    }
    
    parts.push(`${(opts.duration / 1000).toFixed(1)}s`);
    
    if (opts.provider) {
      parts.push(opts.provider);
    }
    
    return parts.join(' | ');
  }
  
  // Stop and cleanup spinner  
  private stopSpinner(): void {
    if (this.spinner && this.spinner.isSpinning) {
      this.spinner.stop();
    }
    this.spinner = null;
  }
  
  // Start a new step
  stepStart(stepIndex: number, prompt?: string): void {
    this.currentStepIndex = stepIndex;
    
    this.logEvent({
      agent: this.agentDisplayName,
      step: stepIndex + 1,
      status: 'init',
      message: prompt || 'Processing'
    });
  }
  
  // Start LLM operation with spinner
  startLLM(): void {
    this.operationStart = Date.now();
    // Don't create spinner immediately - only create it when first token arrives
    // This prevents blank lines when operations complete quickly
  }
  
  // Update token progress
  updateTokens(count: number, provider?: string, toolCalls = 0): void {
    // Create spinner on first token if in TTY mode
    if (!this.spinner && this.isTTY && count === 1) {
      // Add blank line before spinner for visual separation
      this.spinner = ora({
        text: 'Waiting for LLM',
        stream: process.stdout,
        discardStdin: false,
        color: 'yellow'
      }).start();
    }
    
    if (!this.spinner) return;
    
    // Update every 10 tokens or on first token
      if (count % 10 === 0 || count === 1) {
      const elapsed = (Date.now() - this.operationStart) / 1000;
      const throughput = count > 0 ? Math.round(count / Math.max(elapsed, 0.1)) : 0;
      const metrics = `${count} tokens | ${throughput} tok/s | ${toolCalls} tool call${toolCalls !== 1 ? 's' : ''}`;
      
      this.spinner.text = provider ? `${metrics} | ${provider}` : metrics;
    }
  }
  
  // Complete a step
  stepComplete(opts: {
    duration: number;
    tokens?: number;
    provider?: string;
    toolCalls?: number;
  }): void {
    // Always show step complete logs (even for crew-delegated agents)
    this.stopSpinner();
    
    const metrics = this.formatMetrics({
      tokens: opts.tokens,
      toolCalls: opts.toolCalls,
      duration: opts.duration,
      provider: opts.provider
    });
    
    this.logEvent({
      agent: this.agentDisplayName,
      step: this.currentStepIndex + 1,
      status: 'complete',
      message: `โœ” Complete | ${metrics}`
    });
  }
  
  // Agent delegation start
  agentDelegateStart(delegatedAgentName: string): void {
    this.logEvent({
      agent: this.agentDisplayName,
      status: 'coordinating',
      message: `๐Ÿง  delegating to "${delegatedAgentName}"`
    });
    
    this.operationStart = Date.now();
    // Don't create spinner immediately - will be created on first token update
  }
  
  // Agent delegation complete
  agentDelegateComplete(opts: {
    agentName: string;
    tokens: number;
    duration: number;
    provider?: string;
    toolCalls?: number;
  }): void {
    this.stopSpinner();
    
    const metrics = this.formatMetrics({
      tokens: opts.tokens,
      toolCalls: opts.toolCalls,
      duration: opts.duration,
      provider: opts.provider
    });
    
    this.logEvent({
      agent: opts.agentName,
      status: 'complete',
      message: `โœ” Complete | ${metrics}`
    });
  }
  
  // Coordinator activity
  coordinatorActivity(message: string): void {
    this.logEvent({
      agent: this.agentDisplayName,
      status: 'init',
      message: `๐Ÿง  ${message}`
    });
  }
  
  // Coordinator complete
  coordinatorComplete(opts: {
    message: string;
    tokens: number;
    duration: number;
    provider?: string;
    toolCalls?: number;
  }): void {
    this.stopSpinner();
    
    const metrics = this.formatMetrics({
      tokens: opts.tokens,
      toolCalls: opts.toolCalls,
      duration: opts.duration,
      provider: opts.provider
    });
    
    this.logEvent({
      agent: this.agentDisplayName,
      status: 'complete',
      message: `๐Ÿง  ${opts.message} | ${metrics}`
    });
  }
  
  // Workflow end summary  
  workflowEnd(opts: {
    stepCount: number;
    totalTokens?: number;
    totalDuration?: number;
    models?: string[];
    totalToolCalls?: number;
  }): void {
    if (this.isSubAgent) return;
    
    if (opts.totalTokens && opts.models && opts.models.length > 0) {
      const modelsList = opts.models.join(', ');
      const metrics = this.formatMetrics({
        tokens: opts.totalTokens,
        toolCalls: opts.totalToolCalls,
        duration: opts.totalDuration || 0,
        provider: modelsList
      });
      this.logEvent({
        agent: this.agentDisplayName,
        status: 'complete',
        message: `๐ŸŽ‰ agent complete | ${metrics}`
      });
      } else {
      const total = Date.now() - this.workflowStart;
      const metrics = this.formatMetrics({
        toolCalls: opts.totalToolCalls,
        duration: total
      });
      this.logEvent({
        agent: this.agentDisplayName,
        status: 'complete',
        message: `๐ŸŽ‰ workflow complete | ${opts.stepCount} step${opts.stepCount > 1 ? 's' : ''} | ${metrics}`
      });
    }
  }
  
  // Ensure spinner is stopped (public utility for edge cases)
  ensureSpinnerStopped(): void {
    this.stopSpinner();
  }
  
  // Get current spinner (for coordinator use case)
  getSpinner(): ReturnType<typeof ora> | null {
    return this.spinner;
  }
  
  // Get operation start time
  getOperationStart(): number {
    return this.operationStart;
  }
}

/**
 * Create progress handler for workflows.
 * Factory function that creates a ProgressTracker with a compatible interface.
 */
function createProgressHandler(
  totalSteps: number, 
  isSubAgent = false, 
  isExplicitSubAgent = false, 
  parentStepIndex?: number, 
  parentTotalSteps?: number, 
  agentName?: string
) {
  const tracker = new ProgressTracker(agentName, isSubAgent, isExplicitSubAgent);
  
  return {
    stepStart: (stepIndex: number, prompt?: string) => {
      tracker.stepStart(stepIndex, prompt);
    },
    startLlmOperation: () => {
      tracker.startLLM();
    },
    llmToken: (count: number, provider?: string, toolCalls?: number) => {
      tracker.updateTokens(count, provider, toolCalls || 0);
    },
    getOperationStart: () => tracker.getOperationStart(),
    getSpinner: () => tracker.getSpinner(),
    agentStart: (delegatedAgentName: string) => {
      tracker.agentDelegateStart(delegatedAgentName);
    },
    agentToken: (count: number, provider?: string, toolCalls?: number) => {
      tracker.updateTokens(count, provider, toolCalls || 0);
    },
    agentComplete: (delegatedAgentName: string, tokens: number, durationMs: number, provider?: string, toolCalls?: number) => {
      tracker.agentDelegateComplete({
        agentName: delegatedAgentName,
        tokens,
        duration: durationMs,
        provider,
        toolCalls
      });
    },
    stepComplete: (opts: {
      duration: number;
      tokens?: number;
      provider?: string;
      toolCalls?: number;
    }) => {
      tracker.stepComplete(opts);
    },
    workflowEnd: (stepCount: number, totalTokens?: number, totalDuration?: number, models?: string[], totalToolCalls?: number) => {
      tracker.workflowEnd({
        stepCount,
        totalTokens,
        totalDuration,
        models,
        totalToolCalls
      });
    },
    // Expose coordinator methods
    coordinatorActivity: (message: string) => {
      tracker.coordinatorActivity(message);
    },
    coordinatorComplete: (opts: {
      message: string;
      tokens: number;
      duration: number;
      provider?: string;
      toolCalls?: number;
    }) => {
      tracker.coordinatorComplete(opts);
    },
    // Internal: Direct access to tracker for advanced use
    _tracker: tracker
  };
}

/**
 * Build agent context string for multi-agent coordination.
 */
function buildAgentContext(agents: AgentBuilder[]): string {
  const agentList = agents
    .filter(a => a.name && a.description)
    .map(a => {
      // Get the agent's steps if available to show what they'll do
      const agentInternal = a as AgentBuilderInternal;
      const steps = agentInternal._getSteps?.() || [];
      const stepDescriptions = steps
        .filter((s) => {
          const sInternal = s as StepInternal;
          return 'prompt' in s && !sInternal.__reset;
        })
        .map((s, i) => `    ${i + 1}) "${'prompt' in s ? s.prompt : ''}"`)
        .join('\n');
      
      const agentDesc = `- ${a.name}: "${a.description}"`;
      if (stepDescriptions) {
        return `${agentDesc}\n  This agent will:\n${stepDescriptions}`;
      }
      return agentDesc;
    })
    .join('\n');
  
  return `

You can coordinate work across the following agents:

${agentList}

Based on the agents described above, determine the execution flow to accurately bring the task to completion.

Instructions:
- Analyze the task and decide if you need help from an agent
- Be efficient: delegate to the right specialists, but don't over-delegate
- If you believe that an agent is not needed, then do not use it
- Don't call the same agent repeatedly unless they failed or you need different information
- Consider the overall goal and delegate to different agents based on what still needs to be done
- To delegate: respond with "USE [agent_name]: [specific task]"
- When you have the complete final answer: respond with "DONE: [your answer]"
- After a task is completed, you are in charge of delegation: if you think we need to delegate, EXPLICITLY say so, if you think we are done, then explicitly say so
- You can either delegate (USE ..) or be done (DONE ..), or execute the actual task that you have been instructed to do: there is no other option
`;
}

/**
 * Parse coordinator LLM response for agent delegation.
 */
function parseAgentDecision(response: string): 
  | { type: 'use_agent'; agentName: string; task: string }
  | { type: 'done'; answer: string }
  | { type: 'continue'; raw: string }
{
  const useMatch = response.match(/USE\s+(\w+):\s*(.+?)(?=\n(?:USE|DONE:)|$)/s);
  if (useMatch) {
    return {
      type: 'use_agent',
      agentName: useMatch[1].trim(),
      task: useMatch[2].trim()
    };
  }
  
  const doneMatch = response.match(/DONE:\s*(.+)/s);
  if (doneMatch) {
    return {
      type: 'done',
      answer: doneMatch[1].trim()
    };
  }
  
  return {
    type: 'continue',
    raw: response
  };
}

type AgentOptions = {
  llm?: LLMHandle;
  instructions?: string;
  name?: string;                       // Agent name for multi-agent coordination
  description?: string;                // Agent description for automatic selection
  hideProgress?: boolean;              // Disable beautiful TTY progress output (progress shown by default)
  timeout?: number;
  retry?: RetryConfig;
  // Context compaction options
  contextMaxChars?: number;            // soft cap for injected context size (default 4000)
  contextMaxToolResults?: number;      // number of recent tool results to include (default 3)
  // MCP authentication configuration per endpoint
  mcpAuth?: Record<string, MCPAuthConfig>;
  // OpenTelemetry observability (opt-in)
  telemetry?: import('./telemetry.js').VolcanoTelemetry;
  // Maximum tool calling iterations for automatic selection (default 4)
  maxToolIterations?: number;
  // Disable parallel tool execution (parallel execution is enabled by default for performance)
  disableParallelToolExecution?: boolean;
};

/**
 * Context interface for executeStepCore function
 */
interface StepExecutionContext {
  step: Step;
  stepIndex: number;
  defaultLlm?: LLMHandle;
  globalInstructions?: string;
  contextHistory: StepResult[];
  contextMaxToolResults: number;
  contextMaxChars: number;
  defaultMaxToolIterations: number;
  agentName?: string;
  applyAgentAuth: (handle: MCPHandle) => MCPHandle;
  telemetry?: import('./telemetry.js').VolcanoTelemetry;
  agentSpan: any;
  progress?: ReturnType<typeof createProgressHandler> | null;
  capturedStreamOnToken?: (token: string, meta: TokenMetadata) => void;
  onToolCall?: (toolName: string, args: any, result: any) => void;
  disableParallelToolExecution?: boolean;
}

/**
 * Shared pattern step execution logic for run() method.
 * Handles all 7 pattern types with hooks and proper result management.
 * 
 * @param raw - The pattern step definition
 * @param out - Current step results array (for computing indices)
 * @param contextHistory - Historical context
 * @param opts - Agent options for creating sub-agents
 * @param planned - Total planned steps (for runAgent context)
 * @returns Object with wasPattern flag and results array (empty if not a pattern)
 */
async function executePatternStep(
  raw: any,
  out: StepResult[],
  contextHistory: StepResult[],
  opts: AgentOptions | undefined,
  planned: any[],
  totalSteps: number
): Promise<{ wasPattern: boolean; results: StepResult[] }> {
  const stepInternal = raw as StepInternal;
  
  if (stepInternal.__parallel) {
    const hooks = stepInternal.__hooks;
    safeExecuteHook(hooks?.pre, 'Pre-parallel');
    
    const parallelResult = await executeParallel(
      stepInternal.__parallel,
      async (step: any) => {
        const subAgent = agent(opts).then(step);
        const results = await subAgent.run();
        return results[0];
      }
    );
    out.push(parallelResult);
    contextHistory.push(parallelResult);
    
    safeExecuteHook(hooks?.post, 'Post-parallel');
    return { wasPattern: true, results: [parallelResult] };
  }
  
  if (stepInternal.__branch) {
    const { condition, branches } = stepInternal.__branch;
    const hooks = stepInternal.__hooks;
    safeExecuteHook(hooks?.pre, 'Pre-branch');
    
    const branchResults = await executeBranch(condition, branches, out, () => agent(opts));
    out.push(...branchResults);
    contextHistory.push(...branchResults);
    
    safeExecuteHook(hooks?.post, 'Post-branch');
    return { wasPattern: true, results: branchResults };
  }
  
  if (stepInternal.__switch) {
    const { selector, cases } = stepInternal.__switch;
    const hooks = stepInternal.__hooks;
    safeExecuteHook(hooks?.pre, 'Pre-switch');
    
    const switchResults = await executeSwitch(selector, cases, out, () => agent(opts));
    out.push(...switchResults);
    contextHistory.push(...switchResults);
    
    safeExecuteHook(hooks?.post, 'Post-switch');
    return { wasPattern: true, results: switchResults };
  }
  
  if (stepInternal.__while) {
    const { condition, body, opts: whileOpts } = stepInternal.__while;
    safeExecuteHook(whileOpts?.pre, 'Pre-while');
    
    const whileResults = await executeWhile(condition, body, out, () => agent(opts), whileOpts);
    out.push(...whileResults);
    contextHistory.push(...whileResults);
    
    safeExecuteHook(whileOpts?.post, 'Post-while');
    return { wasPattern: true, results: whileResults };
  }
  
  if (stepInternal.__forEach) {
    const { items, body } = stepInternal.__forEach;
    const hooks = stepInternal.__hooks;
    safeExecuteHook(hooks?.pre, 'Pre-forEach');
    
    const forEachResults = await executeForEach(items, body, () => agent(opts));
    out.push(...forEachResults);
    contextHistory.push(...forEachResults);
    
    safeExecuteHook(hooks?.post, 'Post-forEach');
    return { wasPattern: true, results: forEachResults };
  }
  
  if (stepInternal.__retryUntil) {
    const { body, successCondition, opts: retryOpts } = stepInternal.__retryUntil;
    safeExecuteHook(retryOpts?.pre, 'Pre-retryUntil');
    
    const retryResults = await executeRetryUntil(body, successCondition, () => agent(opts), retryOpts);
    out.push(...retryResults);
    contextHistory.push(...retryResults);
    
    safeExecuteHook(retryOpts?.post, 'Post-retryUntil');
    return { wasPattern: true, results: retryResults };
  }
  
  if (stepInternal.__runAgent) {
    const { subAgent } = stepInternal.__runAgent;
    const hooks = stepInternal.__hooks;
    safeExecuteHook(hooks?.pre, 'Pre-runAgent');
    
    // Pass parent's context and total step count to subagent
    const subResults = await executeRunAgent(subAgent, out.length, totalSteps, contextHistory);
    out.push(...subResults);
    contextHistory.push(...subResults);
    
    safeExecuteHook(hooks?.post, 'Post-runAgent');
    return { wasPattern: true, results: subResults };
  }
  
  return { wasPattern: false, results: [] }; // Not a pattern step
}

/**
 * Shared retry logic with timeout, error classification, and exponential backoff.
 * Used by run() method to execute steps with retries.
 */
async function executeWithRetry(
  stepFn: () => Promise<StepResult>,
  stepId: number,
  config: {
    attemptsTotal: number;
    stepTimeoutMs: number;
    useDelay?: number;
    useBackoff?: number;
  }
): Promise<StepResult> {
  let lastError: any;
  let result: StepResult | undefined;
  
  for (let attempt = 1; attempt <= config.attemptsTotal; attempt++) {
    try {
      const r = await withTimeout(stepFn(), config.stepTimeoutMs, 'Step');
      result = r;
      break;
    } catch (e) {
      // classify
      const meta = { stepId } as VolcanoErrorMeta;
      let vErr: VolcanoError | undefined;
      if (e instanceof Error && /timed out/i.test(e.message)) {
        vErr = normalizeError(e, 'timeout', meta);
      } else if (e instanceof ValidationError || /failed schema validation/i.test(String((e as any)?.message || ''))) {
        vErr = normalizeError(e, 'validation', meta);
      } else {
        vErr = e as VolcanoError;
      }
      lastError = vErr || e;
      if (lastError instanceof VolcanoError && lastError.meta?.retryable === false) {
        throw lastError; // abort retries immediately for non-retryable errors
      }
      if (attempt >= config.attemptsTotal) break;
      // schedule wait according to policy
      if (typeof config.useBackoff === 'number' && config.useBackoff > 0) {
        const waitMs = CONSTANTS.DEFAULT_RETRY_BACKOFF_BASE_MS * Math.pow(config.useBackoff, attempt - 1);
        await sleep(waitMs);
      } else {
        const waitMs = Math.max(0, (config.useDelay ?? 0) * 1000);
        if (waitMs > 0) await sleep(waitMs);
      }
    }
  }
  
  if (!result) {
    throw (lastError instanceof VolcanoError ? lastError : new RetryExhaustedError('Retry attempts exhausted', { stepId }, { cause: lastError }));
  }
  
  return result;
}

/**
 * Core step execution logic for run() method.
 * Handles all 4 step types:
 * 1. Automatic tool selection (mcps + prompt)
 * 2. Automatic agent delegation (agents + prompt)
 * 3. LLM-only step (prompt without tools/agents)
 * 4. Explicit MCP tool call (mcp + tool)
 */
async function executeStepCore(ctx: StepExecutionContext): Promise<StepResult> {
  const { 
    step: s, 
    stepIndex,
    defaultLlm, 
    globalInstructions, 
    contextHistory, 
    contextMaxToolResults,
    contextMaxChars,
    defaultMaxToolIterations,
    agentName,
    applyAgentAuth,
    telemetry,
    agentSpan,
    progress,
    capturedStreamOnToken,
    onToolCall,
    disableParallelToolExecution
  } = ctx;

  safeExecuteHook('pre' in s ? s.pre : undefined, 'Pre-step');
  
  // Determine step type for telemetry
  let stepType = 'unknown';
  if ("agents" in s) stepType = 'agent_crew';
  else if ("mcps" in s) stepType = 'mcp_auto';
  else if ("mcp" in s) stepType = 'mcp_explicit';
  else if ("prompt" in s) stepType = 'llm';
  
  // Start step span
  const stepPrompt = 'prompt' in s ? s.prompt : undefined;
  const stepName = 'name' in s ? s.name : undefined;
  const stepLlm = ('llm' in s ? s.llm : undefined) || defaultLlm;
  const stepSpan = telemetry?.startStepSpan(agentSpan, stepIndex, stepType, stepPrompt, stepName, stepLlm) || null;
  
  const r: StepResult = {};
  const stepStart = Date.now();
  if (progress) progress.stepStart(stepIndex, stepPrompt);
  let llmTotalMs = 0;
  
  // Automatic tool selection: LLM chooses and calls MCP tools
  if ("mcps" in s && "prompt" in s) {
    const step = s as Extract<Step, { mcps: MCPHandle[]; prompt: string }>;
    
    const usedLlm = step.llm ?? defaultLlm;
    if (!usedLlm) throw new Error("No LLM provided. Pass { llm } to agent(...) or specify per-step.");
    const stepInstructions = step.instructions ?? globalInstructions;
    const maxToolResults = step.contextMaxToolResults ?? contextMaxToolResults;
    const maxContextChars = step.contextMaxChars ?? contextMaxChars;
    const promptWithHistory = step.prompt + buildHistoryContextChunked(contextHistory, maxToolResults, maxContextChars);
    r.prompt = step.prompt;
    // Apply agent-level auth to all MCP handles
    const mcpsWithAuth = step.mcps.map(applyAgentAuth);
    const availableTools = await discoverTools(mcpsWithAuth);
    if (availableTools.length === 0) {
      r.llmOutput = "No tools available for this request.";
    } else {
      const aggregated: Array<{ name: string; endpoint: string; result: any; ms?: number }> = [];
      let currentToolCallCount = 0;  // Track tool calls for real-time progress
      const maxIterations = step.maxToolIterations ?? defaultMaxToolIterations;
      let workingPrompt = (stepInstructions ? stepInstructions + "\n\n" : "") + promptWithHistory;
      for (let i = 0; i < maxIterations; i++) {
        const llmStart = Date.now();
        let toolPlan: LLMToolResult;
        
        // Progress updates happen via tool call counter display below
        
        try {
          toolPlan = await usedLlm.genWithTools(workingPrompt, availableTools);
        } catch (e) {
          const provider = classifyProviderFromLlm(usedLlm);
          throw normalizeError(e, 'llm', { stepId: stepIndex, provider });
        }
        const llmCallDuration = Date.now() - llmStart;
        llmTotalMs += llmCallDuration;
        
        telemetry?.recordMetric('llm.call', 1, { provider: getLLMProviderId(usedLlm), error: false });
        telemetry?.recordMetric('llm.duration', llmCallDuration, { provider: getLLMProviderId(usedLlm), model: usedLlm.model });
        
        const llmInternal = usedLlm as LLMHandleInternal;
        const usage = normalizeTokenUsage(llmInternal.getUsage?.());
        recordTokenMetrics(telemetry, usage, {
          provider: getLLMProviderId(usedLlm),
          model: usedLlm.model,
          agent_name: agentName
        });
        
        const rInternal = r as StepResultInternal;
        if (usage) {
          const totalTokens = (usage.inputTokens || 0) + (usage.outputTokens || 0);
          rInternal.__tokenCount = (rInternal.__tokenCount || 0) + totalTokens;
          rInternal.__provider = getLLMProviderId(usedLlm);
        }
        
        if (!toolPlan || !Array.isArray(toolPlan.toolCalls) || toolPlan.toolCalls.length === 0) {
          // finish with final content
          r.llmOutput = toolPlan?.content || r.llmOutput;
          break;
        }
        
        // Check if we can safely execute tools in parallel
        const canParallelize = !disableParallelToolExecution && canSafelyParallelize(toolPlan.toolCalls);
        let toolResultsAppend = "\n\n[Tool results]\n";
        
        if (canParallelize) {
          // PARALLEL EXECUTION: Execute all tools simultaneously
          telemetry?.recordMetric('tool.execution.parallel', 1, { count: toolPlan.toolCalls.length });
          
          const toolPromises = toolPlan.toolCalls.map(async (call) => {
            const mapped = call;
            let handle = mapped?.mcpHandle;
            if (!handle) return null;
            
            // Apply agent-level auth
            handle = applyAgentAuth(handle);
            
            // Validate args when schema known
            try { 
              validateWithSchema(
                (availableTools.find(t => t.name === mapped.name) as any)?.parameters, 
                mapped.arguments, 
                `Tool ${mapped.name}`
              ); 
            } catch (e) { 
              throw e; 
            }
            
            const idx = mapped.name.indexOf('.');
            const actualToolName = idx >= 0 ? mapped.name.slice(idx + 1) : mapped.name;
            const mcpStart = Date.now();
            
            try {
              const result = await withMCPAny(
                handle, 
                (c) => c.callTool({ name: actualToolName, arguments: mapped.arguments || {} }), 
                telemetry, 
                'call_tool'
              );
              const mcpMs = Date.now() - mcpStart;
              
              return {
                name: mapped.name,
                arguments: mapped.arguments,
                endpoint: handle.url,
                result,
                ms: mcpMs
              };
            } catch (e) {
              const provider = classifyProviderFromMcp(handle);
              throw normalizeError(e, 'mcp-tool', { stepId: stepIndex, provider });
            }
          });
          
          const toolResults = await Promise.all(toolPromises);
          
          // Build results in original order
          for (const toolCall of toolResults) {
            if (!toolCall) continue;
            aggregated.push(toolCall);
            currentToolCallCount++;  // Increment counter
            
            if (progress) {
              const rInternal = r as StepResultInternal;
              progress._tracker.updateTokens(rInternal.__tokenCount || 0, rInternal.__provider || '', currentToolCallCount);
            }
            
            toolResultsAppend += `- ${toolCall.name} -> ${typeof toolCall.result === 'string' ? toolCall.result : JSON.stringify(toolCall.result)}\n`;
            
            // Call onToolCall callback if provided
            if (onToolCall) {
              try {
                onToolCall(toolCall.name, toolCall.arguments, toolCall.result);
              } catch (err) {
                // Don't let callback errors break execution
                console.error('onToolCall callback error:', err);
              }
            }
          }
        } else {
          // SEQUENTIAL EXECUTION: Execute tools one by one (safe default)
          telemetry?.recordMetric('tool.execution.sequential', 1, { count: toolPlan.toolCalls.length });
          
          for (const call of toolPlan.toolCalls) {
            const mapped = call;
            let handle = mapped?.mcpHandle;
            if (!handle) continue;
            
            // Apply agent-level auth
            handle = applyAgentAuth(handle);
            
            // Validate args when schema known
            try { 
              validateWithSchema(
                (availableTools.find(t => t.name === mapped.name) as any)?.parameters, 
                mapped.arguments, 
                `Tool ${mapped.name}`
              ); 
            } catch (e) { 
              throw e; 
            }
            
            const idx = mapped.name.indexOf('.');
            const actualToolName = idx >= 0 ? mapped.name.slice(idx + 1) : mapped.name;
            const mcpStart = Date.now();
            let result: any;
            
            try {
              result = await withMCPAny(
                handle, 
                (c) => c.callTool({ name: actualToolName, arguments: mapped.arguments || {} }), 
                telemetry, 
                'call_tool'
              );
            } catch (e) {
              const provider = classifyProviderFromMcp(handle);
              throw normalizeError(e, 'mcp-tool', { stepId: stepIndex, provider });
            }
            
            const mcpMs = Date.now() - mcpStart;
            const toolCall: any = { 
              name: mapped.name, 
              arguments: mapped.arguments, 
              endpoint: handle.url, 
              result, 
              ms: mcpMs 
            };
            aggregated.push(toolCall);
            currentToolCallCount++;  // Increment counter
            
            if (progress) {
              const rInternal = r as StepResultInternal;
              progress._tracker.updateTokens(rInternal.__tokenCount || 0, rInternal.__provider || '', currentToolCallCount);
            }
            
            toolResultsAppend += `- ${mapped.name} -> ${typeof result === 'string' ? result : JSON.stringify(result)}\n`;
            
            // Call onToolCall callback if provided
            if (onToolCall) {
              try {
                onToolCall(mapped.name, mapped.arguments, result);
              } catch (err) {
                // Don't let callback errors break execution
                console.error('onToolCall callback error:', err);
              }
            }
          }
        }
        
        if (aggregated.length) r.toolCalls = aggregated;
        // Prepare next prompt with appended tool results
        workingPrompt = (stepInstructions ? stepInstructions + "\n\n" : "") + promptWithHistory + toolResultsAppend;
        // On next iteration, model can produce final answer or ask for more tools
      }
      // Ensure toolCalls is always set for automatic tool selection steps
      if (!r.toolCalls) r.toolCalls = [];
    }
  }
  // Multi-agent coordination: Coordinator delegates to specialist agents
  else if ("agents" in s && "prompt" in s) {
    const step = s as Extract<Step, { agents: AgentBuilder[]; prompt: string }>;
    
    const usedLlm = step.llm ?? defaultLlm;
    if (!usedLlm) throw new Error("No LLM provided. Pass { llm } to agent(...) or specify per-step.");
    const stepInstructions = step.instructions ?? globalInstructions;
    const maxToolResults = step.contextMaxToolResults ?? contextMaxToolResults;
    const maxContextChars = step.contextMaxChars ?? contextMaxChars;
    const promptWithHistory = step.prompt + buildHistoryContextChunked(contextHistory, maxToolResults, maxContextChars);
    r.prompt = step.prompt;
    
    const availableAgents = step.agents;
    if (availableAgents.length === 0 || !availableAgents.some(a => a.name && a.description)) {
      r.llmOutput = "No agents available or agents missing name/description.";
    } else {
      const agentContext = buildAgentContext(availableAgents);
      const maxIterations = 10;  // Safety limit for coordinator decisions
      let workingPrompt = (stepInstructions ? stepInstructions + "\n\n" : "") + promptWithHistory + agentContext;
      const agentCalls: Array<{ name: string; task: string; tokens: number; ms: number; result?: string }> = [];
      let totalTokens = 0;
      const modelsUsed = new Set<string>();
      
      for (let i = 0; i < maxIterations; i++) {
        // Show coordinator thinking with structured log
        if (progress) {
          const coordMessage = i === 0 ? 'selecting agents' : 'deciding next step';
          progress.coordinatorActivity(coordMessage);
          progress.startLlmOperation();
        }
        
        const llmStart = Date.now();
        let coordinatorResponse: string;
        let coordTokenCount = 0;
        
        try {
          // Use streaming for coordinator when progress enabled
          if (progress && typeof usedLlm.genStream === 'function') {
            const tokens: string[] = [];
            for await (const token of usedLlm.genStream(workingPrompt)) {
              tokens.push(token);
              coordTokenCount++;
              // Update token progress using centralized tracker
              progress._tracker.updateTokens(coordTokenCount, getLLMProviderId(usedLlm), 0);
            }
            coordinatorResponse = tokens.join('');
          } else {
            coordinatorResponse = await usedLlm.gen(workingPrompt);
          }
        } catch (e) {
          const provider = classifyProviderFromLlm(usedLlm);
          throw normalizeError(e, 'llm', { stepId: stepIndex, provider });
        }
        const coordDuration = Date.now() - llmStart;
        llmTotalMs += coordDuration;
        
        const llmInternal = usedLlm as LLMHandleInternal;
        const coordUsage = normalizeTokenUsage(llmInternal.getUsage?.());
        recordTokenMetrics(telemetry, coordUsage, {
          provider: getLLMProviderId(usedLlm),
          model: usedLlm.model,
          agent_name: agentName || 'coordinator'
        });
        
        const decision = parseAgentDecision(coordinatorResponse);
        
        
        if (decision.type === 'done') {
          totalTokens += coordTokenCount;
          modelsUsed.add(getLLMProviderId(usedLlm));
          if (progress) {
            progress.coordinatorComplete({
              message: 'final answer ready',
              tokens: coordTokenCount,
              duration: coordDuration,
              provider: getLLMProviderId(usedLlm),
              toolCalls: 0
            });
        }
          r.llmOutput = decision.answer;
          break;
        } else if (decision.type === 'use_agent') {
          totalTokens += coordTokenCount;
          modelsUsed.add(getLLMProviderId(usedLlm));
          if (progress) {
            progress.coordinatorComplete({
              message: `use "${decision.agentName}" agent`,
              tokens: coordTokenCount,
              duration: coordDuration,
              provider: getLLMProviderId(usedLlm),
              toolCalls: 0
            });
        }
          const selectedAgent = availableAgents.find(a => a.name === decision.agentName);
          if (!selectedAgent) {
            workingPrompt += `\n\nError: Agent '${decision.agentName}' not found. Available: ${availableAgents.map(a => a.name).join(', ')}`;
            continue;
          }
          
          // Don't log "delegating to X" - it's redundant with the complete message above
          
          const agentStart = Date.now();
          let agentResult: StepResult[];
          
          try {
            // Create a FRESH agent instance to avoid concurrency locks
            const selectedAgentInternal = selectedAgent as AgentBuilderInternal;
            const selectedAgentOpts = selectedAgentInternal._getOpts?.() || {};
            const selectedAgentSteps = selectedAgentInternal._getSteps?.() || [];
            
            // Create fresh agent with same config
            const freshAgent = agent({
              llm: selectedAgentOpts.llm || defaultLlm,
              name: selectedAgent.name,
              description: selectedAgent.description,
              instructions: selectedAgentOpts.instructions,
              timeout: selectedAgentOpts.timeout,
              retry: selectedAgentOpts.retry,
              contextMaxChars: selectedAgentOpts.contextMaxChars,
              contextMaxToolResults: selectedAgentOpts.contextMaxToolResults,
              mcpAuth: selectedAgentOpts.mcpAuth,
              telemetry: selectedAgentOpts.telemetry || telemetry,
              maxToolIterations: selectedAgentOpts.maxToolIterations,
              disableParallelToolExecution: selectedAgentOpts.disableParallelToolExecution
            });
            
            // Add the template steps from original agent
            for (const step of selectedAgentSteps) {
              const stepInternal = step as StepInternal;
              if (!stepInternal.__reset) {
                freshAgent.then(step);
              }
            }
            
            // Mark as sub-agent to suppress banner/footer
            const freshAgentInternal = freshAgent as AgentBuilderInternal;
            freshAgentInternal.__isSubAgent = true;
            freshAgentInternal.__parentAgentName = agentName || 'coordinator';
            
            // Pass full execution history to delegated agent
            const parentContextForAgent: StepResult[] = [
              {
                prompt: (s as any).prompt,
                llmOutput: `You are helping with this task by ${decision.task}`
              },
              ...contextHistory,  // All parent's history up to this point
              // Include ALL previous agent executions for continuity
              ...agentCalls.map(c => ({
                prompt: `Agent ${c.name} was delegated: ${c.task}`,
                llmOutput: c.result
              }))
            ];
            freshAgentInternal.__parentContext = parentContextForAgent;
            
            
            const delegatedAgent = freshAgent;
            
            // Record sub-agent relationship
            if (telemetry) {
              telemetry.recordMetric('agent.subagent_call', 1, {
                parent_agent_name: agentName || 'coordinator',
                agent_name: decision.agentName
              });
            }
            
            // Run the delegated agent WITHOUT parent token tracking
            // Each agent shows its own progress independently
            agentResult = await delegatedAgent.run();
          } catch (e) {
            console.error(`ERROR: Agent '${decision.agentName}' failed:`, e);
            workingPrompt += `\n\nAgent '${decision.agentName}' failed: ${(e as Error).message}`;
            continue;
          }
          const agentMs = Date.now() - agentStart;
          
          // Calculate total tokens from agent results
          const agentTokenCount = agentResult.reduce((sum, step) => {
            const stepInternal = step as StepResultInternal;
            return sum + (stepInternal.__tokenCount || 0);
          }, 0);
          const firstStepInternal = agentResult.length > 0 ? agentResult[0] as StepResultInternal : null;
          const agentProvider = (firstStepInternal?.__provider) || getLLMProviderId(usedLlm);
          
          totalTokens += agentTokenCount;
          modelsUsed.add(agentProvider);
          
          const agentOutput = agentResult[agentResult.length - 1]?.llmOutput || '[no output]';
          const agentToolCalls = agentResult.reduce((sum, step) => sum + (step.toolCalls?.length || 0), 0);
          agentCalls.push({ name: decision.agentName, task: decision.task, tokens: agentTokenCount, ms: agentMs, result: agentOutput });
          
          
          if (progress) progress.agentComplete(decision.agentName, agentTokenCount, agentMs, agentProvider, agentToolCalls);
          
          // Provide clearer feedback to coordinator about what was accomplished
          const feedbackToCoordinator = `\n\nAgent '${decision.agentName}' completed their task "${decision.task}" (${agentMs}ms).\n\nTheir output:\n${agentOutput}\n\nBased on this result, what should we do next? Remember the overall goal: ${step.prompt}`;
          
          workingPrompt += feedbackToCoordinator;
        } else {
          if (i === maxIterations - 1) {
            r.llmOutput = decision.raw;
          } else {
            workingPrompt += `\n\nPlease use the USE or DONE directive.`;
          }
        }
      }
      
      if (!r.llmOutput && agentCalls.length > 0) {
        r.llmOutput = agentCalls[agentCalls.length - 1].result;
      }
      
      if (agentCalls.length > 0) {
        const rInternal = r as StepResultInternal;
        rInternal.agentCalls = agentCalls;
        rInternal.__crewTotalTokens = totalTokens;
        rInternal.__provider = Array.from(modelsUsed).join(',');
        telemetry?.recordMetric('agent.delegation', agentCalls.length, { agents: agentCalls.map(c => c.name).join(',') });
        for (const agentCall of agentCalls) {
          telemetry?.recordMetric('agent.call', 1, { agentName: agentCall.name });
        }
      }
    }
  }
  // LLM-only step: Simple text generation
  else if ("prompt" in s && !("mcp" in s) && !("mcps" in s) && !("agents" in s)) {
    const step = s as Extract<Step, { prompt: string }> & { llm?: LLMHandle; instructions?: string; onToken?: (token: string) => void };
    
    const usedLlm = step.llm ?? defaultLlm;
    if (!usedLlm) throw new Error("No LLM provided. Pass { llm } to agent(...) or specify per-step.");
    const stepInstructions = step.instructions ?? globalInstructions;
    const maxToolResults = step.contextMaxToolResults ?? contextMaxToolResults;
    const maxContextChars = step.contextMaxChars ?? contextMaxChars;
    const promptWithHistory = step.prompt + buildHistoryContextChunked(contextHistory, maxToolResults, maxContextChars);
    const finalPrompt = (stepInstructions ? stepInstructions + "\n\n" : "") + promptWithHistory;
    r.prompt = step.prompt;
    
    const llmSpan = telemetry?.startLLMSpan(stepSpan, usedLlm, finalPrompt) || null;
    
    // Handle different scenarios for onToken and progress
    const stepOnToken = step.onToken;
    const shouldShowProgress = !stepOnToken && !capturedStreamOnToken && progress;
    
    if (shouldShowProgress) progress!.startLlmOperation();
    const llmStart = Date.now();
    try {
      let tokenCount = 0;
      const progressOnToken = shouldShowProgress ? () => {
        tokenCount++;
        progress!.llmToken(tokenCount, getLLMProviderId(usedLlm), 0);  // 0 tool calls for LLM-only steps
      } : undefined;
      
      r.llmOutput = await executeLLMWithStreaming(
        usedLlm,
        finalPrompt,
        stepOnToken,
        capturedStreamOnToken,
        { stepIndex, stepPrompt: step.prompt },
        progress,
        progressOnToken
      );
      const rInternal = r as StepResultInternal;
      rInternal.__tokenCount = tokenCount;
      rInternal.__provider = getLLMProviderId(usedLlm);
      const llmCallDuration = Date.now() - llmStart;
      telemetry?.endSpan(llmSpan);
      telemetry?.recordMetric('llm.call', 1, { provider: getLLMProviderId(usedLlm), error: false });
      telemetry?.recordMetric('llm.duration', llmCallDuration, { provider: getLLMProviderId(usedLlm), model: usedLlm.model });
    
      const llmInternal = usedLlm as LLMHandleInternal;
      const usage = normalizeTokenUsage(llmInternal.getUsage?.());
      recordTokenMetrics(telemetry, usage, {
        provider: getLLMProviderId(usedLlm),
        model: usedLlm.model,
        agent_name: agentName
      });
    } catch (e) {
      telemetry?.endSpan(llmSpan, undefined, e);
      telemetry?.recordMetric('llm.call', 1, { provider: getLLMProviderId(usedLlm), error: true });
      telemetry?.recordMetric('error', 1, { type: 'llm', provider: getLLMProviderId(usedLlm) });
      const provider = classifyProviderFromLlm(usedLlm);
      throw normalizeError(e, 'llm', { stepId: stepIndex, provider });
    }
    llmTotalMs += Date.now() - llmStart;
  }
  // Explicit tool call: Direct invocation of a specific MCP tool
  else if ("mcp" in s && "tool" in s) {
    const step = s as Extract<Step, { mcp: MCPHandle; tool: string }>;
    
    // Apply agent-level auth
    const mcpHandle = applyAgentAuth(step.mcp);
    
    if ("prompt" in step) {
      const stepWithPrompt = step as Extract<Step, { mcp: MCPHandle; tool: string; prompt: string }>;
      const usedLlm = stepWithPrompt.llm ?? defaultLlm;
      if (!usedLlm) throw new Error("No LLM provided. Pass { llm } to agent(...) or specify per-step.");
      const stepInstructions = stepWithPrompt.instructions ?? globalInstructions;
      const maxToolResults = stepWithPrompt.contextMaxToolResults ?? contextMaxToolResults;
      const maxContextChars = stepWithPrompt.contextMaxChars ?? contextMaxChars;
      const promptWithHistory = stepWithPrompt.prompt + buildHistoryContextChunked(contextHistory, maxToolResults, maxContextChars);
      const finalPrompt = (stepInstructions ? stepInstructions + "\n\n" : "") + promptWithHistory;
      r.prompt = stepWithPrompt.prompt;
      const llmStart = Date.now();
      r.llmOutput = await usedLlm.gen(finalPrompt);
      llmTotalMs += Date.now() - llmStart;
    }
    // Validate against tool schema if discoverable
    const schema = await getToolSchema(mcpHandle, step.tool);
    validateWithSchema(schema, step.args ?? {}, `Tool ${mcpHandle.id}.${step.tool}`);
    const mcpStart = Date.now();
    let res: any;
    try {
      res = await withMCP(mcpHandle, (c) => c.callTool({ name: step.tool, arguments: step.args ?? {} }), telemetry, 'call_tool');
    } catch (e) {
      const provider = classifyProviderFromMcp(mcpHandle);
      throw normalizeError(e, 'mcp-tool', { stepId: stepIndex, provider });
    }
    const mcpMs = Date.now() - mcpStart;
    r.mcp = { endpoint: mcpHandle.url, tool: step.tool, result: res, ms: mcpMs };
  }

  r.llmMs = llmTotalMs;
  r.durationMs = Date.now() - stepStart;
  
  // End step span
  telemetry?.endSpan(stepSpan, r);
  telemetry?.recordMetric('step.duration', r.durationMs, { type: stepType });
  
  // Flush telemetry after each step for real-time visibility
  await telemetry?.flush();
  
  safeExecuteHook('post' in s ? s.post : undefined, 'Post-step');
  
  return r;
}

/**
 * Recursively count the total number of steps that will be executed,
 * including steps within sub-agents called via .runAgent()
 */
function countTotalSteps(steps: Array<Step>): number {
  let total = 0;
  
  for (const step of steps) {
    const stepInternal = step as StepInternal;
    if (stepInternal.__runAgent) {
      // This is a runAgent step - recursively count its sub-agent's steps
      const subAgent = stepInternal.__runAgent.subAgent;
      const subAgentInternal = subAgent as AgentBuilderInternal;
      const subSteps = subAgentInternal._getSteps?.() || [];
      total += countTotalSteps(subSteps);
    } else if (stepInternal.__parallel) {
      // Parallel steps count as 1 in the display
      total += 1;
    } else if (stepInternal.__branch || stepInternal.__switch) {
      // Branch/switch counts as 1 (we don't know which branch yet)
      total += 1;
    } else if (stepInternal.__forEach) {
      // forEach counts as number of items
      const items = stepInternal.__forEach.items || [];
      total += items.length;
    } else if (stepInternal.__while || stepInternal.__retryUntil) {
      // Loops/retry count as 1 (unknown iterations)
      total += 1;
    } else if (!stepInternal.__reset) {
      // Regular step
      total += 1;
    }
  }
  
  return total;
}

/**
 * Create an AI agent that chains LLM reasoning with MCP tool calls.
 * 
 * @param opts - Optional configuration including LLM provider, instructions, timeout, retry policy, and observability
 * @returns AgentBuilder for chaining steps with .then() and run()
 * 
 * @example
 * // Simple agent
 * const results = await agent({ llm: llmOpenAI({...}) })
 *   .then({ prompt: "Analyze data" })
 *   .then({ prompt: "Generate insights" })
 *   .run();
 * 
 * @example
 * // With automatic tool selection
 * await agent({ llm })
 *   .then({ 
 *     prompt: "Book a meeting and send confirmation", 
 *     mcps: [calendar, email] 
 *   })
 *   .run();
 */
export function agent(opts?: AgentOptions): AgentBuilder {
  const steps: Array<Step | StepFactory | { __reset: true }> = [];
  const defaultLlm = opts?.llm;
  let contextHistory: StepResult[] = [];
  let inheritedParentContext = false;
  const globalInstructions = opts?.instructions;
  const agentName = opts?.name;
  const agentDescription = opts?.description;
  const showProgress = !opts?.hideProgress;
  const defaultTimeoutMs = (opts?.timeout ?? CONSTANTS.DEFAULT_TIMEOUT_SECONDS) * 1000;
  const defaultRetry: RetryConfig = opts?.retry ?? { delay: CONSTANTS.DEFAULT_RETRY_DELAY_SECONDS, retries: CONSTANTS.DEFAULT_RETRY_ATTEMPTS };
  const contextMaxChars = opts?.contextMaxChars ?? CONSTANTS.DEFAULT_CONTEXT_MAX_CHARS;
  const contextMaxToolResults = opts?.contextMaxToolResults ?? CONSTANTS.DEFAULT_CONTEXT_MAX_TOOL_RESULTS;
  const agentMcpAuth = opts?.mcpAuth || {};
  const telemetry = opts?.telemetry;
  const defaultMaxToolIterations = opts?.maxToolIterations ?? CONSTANTS.DEFAULT_MAX_TOOL_ITERATIONS;
  let isRunning = false;
  
  function applyAgentAuth(handle: MCPHandle): MCPHandle {
    if (handle.auth) return handle; // Handle-level auth takes precedence
    const authConfig = agentMcpAuth[handle.url];
    if (authConfig) {
      return { ...handle, auth: authConfig };
    }
    return handle;
  }
  
  const builder: AgentBuilder = {
    name: agentName,
    description: agentDescription,
    _getSteps() { return steps; },  // Internal helper for recursive step counting
    _getOpts() { return opts; },  // Internal: Store opts for creating fresh instances
    resetHistory() { steps.push({ __reset: true }); return builder; },
    then(s: Step | StepFactory) { steps.push(s); return builder; },
    
    // Parallel execution
    parallel(stepsOrDict: Step[] | Record<string, Step>, hooks?: { pre?: () => void; post?: () => void }) {
      steps.push({ __parallel: stepsOrDict, __hooks: hooks } as any);
      return builder;
    },
    
    // Conditional branching
    branch(condition: (history: StepResult[]) => boolean, branches: { true: (agent: AgentBuilder) => AgentBuilder; false: (agent: AgentBuilder) => AgentBuilder }, hooks?: { pre?: () => void; post?: () => void }) {
      steps.push({ __branch: { condition, branches }, __hooks: hooks } as any);
      return builder;
    },
    
    switch<T = string>(selector: (history: StepResult[]) => T, cases: Record<string, (agent: AgentBuilder) => AgentBuilder> & { default?: (agent: AgentBuilder) => AgentBuilder }, hooks?: { pre?: () => void; post?: () => void }) {
      steps.push({ __switch: { selector, cases }, __hooks: hooks } as any);
      return builder;
    },
    
    // Loops
    while(condition: (history: StepResult[]) => boolean, body: (agent: AgentBuilder) => AgentBuilder, opts?: { maxIterations?: number; timeout?: number; pre?: () => void; post?: () => void }) {
      steps.push({ __while: { condition, body, opts } } as any);
      return builder;
    },
    
    forEach<T>(items: T[], body: (item: T, agent: AgentBuilder) => AgentBuilder, hooks?: { pre?: () => void; post?: () => void }) {
      steps.push({ __forEach: { items, body }, __hooks: hooks } as any);
      return builder;
    },
    
    retryUntil(body: (agent: AgentBuilder) => AgentBuilder, successCondition: (result: StepResult) => boolean, opts?: { maxAttempts?: number; backoff?: number; pre?: () => void; post?: () => void }) {
      steps.push({ __retryUntil: { body, successCondition, opts } } as any);
      return builder;
    },
    
    // Sub-agent composition
    runAgent(subAgent: AgentBuilder, hooks?: { pre?: () => void; post?: () => void }) {
      steps.push({ __runAgent: { subAgent }, __hooks: hooks } as any);
      return builder;
    },
    
    async run(optionsOrLog?: StreamOptions | ((s: StepResult, stepIndex: number) => void)): Promise<AgentResults> {
      if (isRunning) {
        throw new AgentConcurrencyError('This agent is already running. Create a new agent() instance for concurrent runs.');
      }
      isRunning = true;
      
      // Handle both old signature (log callback) and new signature (StreamOptions)
      const log = typeof optionsOrLog === 'function' ? optionsOrLog : optionsOrLog?.onStep;
      const capturedStreamOnToken = typeof optionsOrLog === 'object' ? optionsOrLog?.onToken : undefined;
      
      const builderInternal = builder as AgentBuilderInternal;
      const isSubAgent = builderInternal.__isSubAgent || false;
      const isExplicitSubAgent = builderInternal.__isExplicitSubAgent || false;
      const parentStepIndex = builderInternal.__parentStepIndex;
      const parentTotalSteps = builderInternal.__parentTotalSteps;
      const parentAgentName = builderInternal.__parentAgentName;
      
      // Recursively count total steps (including sub-agent steps)
      const totalSteps = countTotalSteps(steps as Step[]);
      // Show progress unless hideProgress was set (delegated agents show their progress)
      const progress = showProgress ? createProgressHandler(totalSteps, isSubAgent, isExplicitSubAgent, parentStepIndex, parentTotalSteps, agentName) : null;
      
      // Record agent execution (always, even for anonymous agents)
      if (telemetry) {
        telemetry.recordMetric('agent.execution', 1, {
          agent_name: agentName || 'anonymous',
          parent_agent: parentAgentName || 'none',
          is_subagent: isSubAgent.toString()
        });
      }
      
      // Start agent span
      const agentSpan = telemetry?.startAgentSpan(steps.length, agentName) || null;
      const out: StepResult[] = [];
      
      // Inherit parent context if this is a subagent
      if (!inheritedParentContext && builderInternal.__parentContext) {
        contextHistory = [...builderInternal.__parentContext];
        inheritedParentContext = true;
        
      }
      
      try {
        // snapshot steps array to make run isolated from later .then() calls
        const planned = [...steps];
        for (const raw of planned) {
          const rawInternal = raw as StepInternal;
          if (rawInternal.__reset) { contextHistory = []; continue; }
          
          // Handle advanced pattern steps using shared function
          const patternResult = await executePatternStep(raw, out, contextHistory, opts, planned, totalSteps);
          if (patternResult.wasPattern) {
            patternResult.results.forEach((r, i) => {
              log?.(r, out.length - patternResult.results.length + i);
            });
            continue;
          }
          
          const s = typeof raw === 'function' ? (raw as StepFactory)(out) : (raw as Step);
          const stepTimeout = 'timeout' in s ? s.timeout : undefined;
          const stepRetry = 'retry' in s ? s.retry : undefined;
          const stepTimeoutMs = (stepTimeout ?? (defaultTimeoutMs / 1000)) * 1000;
          const retryCfg: RetryConfig = stepRetry ?? defaultRetry;
          const attemptsTotal = retryCfg.retries ?? defaultRetry.retries ?? CONSTANTS.DEFAULT_RETRY_ATTEMPTS;
          const useDelay = retryCfg.delay ?? defaultRetry.delay ?? CONSTANTS.DEFAULT_RETRY_DELAY_SECONDS;
          const useBackoff = retryCfg.backoff;
          if (useDelay && useBackoff) throw new Error('retry: specify either delay or backoff, not both');
  
          const doStep = async (): Promise<StepResult> => {
            return executeStepCore({
              step: s,
              stepIndex: out.length,
              defaultLlm,
              globalInstructions,
              contextHistory,
              contextMaxToolResults,
              contextMaxChars,
              defaultMaxToolIterations,
              agentName,
              applyAgentAuth,
              telemetry,
              agentSpan,
              progress,
              capturedStreamOnToken,
              onToolCall: 'onToolCall' in s ? s.onToolCall : undefined,
              disableParallelToolExecution: opts?.disableParallelToolExecution
            });
          };

          const r = await executeWithRetry(doStep, out.length, {
            attemptsTotal,
            stepTimeoutMs,
            useDelay,
            useBackoff
          });
          if (progress) {
            const crewTokens = (r as any).__crewTotalTokens;
            const toolCallCount = r.toolCalls?.length || (r.mcp ? 1 : 0);
            progress.stepComplete({
              duration: r.durationMs || 0,
              tokens: crewTokens || (r as any).__tokenCount || 0,
              provider: (r as any).__provider,
              toolCalls: toolCallCount
            });
          }
          log?.(r, out.length);
          out.push(r);
          contextHistory.push(r);
        }
        // Populate aggregated totals on the final step
        if (out.length > 0) {
          const totalDuration = out.reduce((acc, s) => acc + (s.durationMs || 0), 0);
          const totalLlm = out.reduce((acc, s) => acc + (s.llmMs || 0), 0);
          const totalMcp = out.reduce((acc, s) => {
            let accStep = acc;
            if (s.mcp?.ms) accStep += s.mcp.ms;
            if (s.toolCalls) accStep += s.toolCalls.reduce((a, t) => a + (t.ms || 0), 0);
            return accStep;
          }, 0);
          const last = out[out.length - 1];
          last.totalDurationMs = totalDuration;
          last.totalLlmMs = totalLlm;
          last.totalMcpMs = totalMcp;
          
          // End agent span and record metrics
          telemetry?.endSpan(agentSpan, last);
          telemetry?.recordMetric('agent.duration', totalDuration, { steps: out.length });
          telemetry?.recordMetric('workflow.steps', out.length, { agent_name: agentName || 'anonymous' });
        }
        return enhanceResults(out);
      } catch (error) {
        // End agent span with error
        telemetry?.endSpan(agentSpan, undefined, error);
        telemetry?.recordMetric('error', 1, { type: 'agent', level: 'workflow' });
        throw error;
      } finally {
        if (progress) {
          // Calculate totals for workflow end
          const totalTokens = out.reduce((acc, s) => {
            const stepTokens = (s as any).__tokenCount || (s as any).__crewTotalTokens || 0;
            return acc + stepTokens;
          }, 0);
          const modelsUsed = new Set<string>();
          out.forEach(s => {
            const provider = (s as any).__provider;
            const crewModels = (s as any).__crewModels;
            if (provider) modelsUsed.add(provider);
            if (crewModels) crewModels.forEach((m: string) => modelsUsed.add(m));
          });
          const totalDuration = out.reduce((acc, s) => acc + (s.durationMs || 0), 0);
          const totalToolCalls = out.reduce((acc, s) => acc + (s.toolCalls?.length || 0) + (s.mcp ? 1 : 0), 0);
          progress.workflowEnd(steps.length, totalTokens, totalDuration, Array.from(modelsUsed), totalToolCalls);
        }
        
        // Flush telemetry after workflow completes to ensure all traces/metrics are exported
        // This is especially important for short-lived processes that exit immediately
        await telemetry?.flush();
        
        isRunning = false;
      }
    },
  };
  
  return builder;
}

// Export for testing
export { enhanceResults };

// Test utility - only for testing
export async function _clearMCPPool() {
  if (process.env.NODE_ENV === 'test' || process.env.VITEST) {
    // Close all MCP connections
    for (const [, entry] of MCP_POOL) {
      try {
        await entry.client.close();
        if (entry.transport && typeof entry.transport.close === 'function') {
          await entry.transport.close();
        }
      } catch {}
    }
    MCP_POOL.clear();
    
    // Also clear stdio pool
    for (const [, entry] of MCP_STDIO_POOL) {
      try {
        await entry.client.close();
        entry.process.kill();
      } catch {}
    }
    MCP_STDIO_POOL.clear();
  }
}