quorum-rs 0.7.0-rc.6

Rust SDK and CLI for multi-agent deliberation systems — ships the `quorum` binary (run / status / trace / tui / init) plus the underlying agent, LLM, tool, prompt, and worker library.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
//! This module contains the implementation of the `NsedAgent`.
use crate::agents::ChatCapable;
use crate::agents::DeliberationPhase;
use crate::agents::normalize_score;
use crate::agents::{
    AgentConfig, AgentContext, CategoryScores, ClaimAssessment, DisagreementPoint, Evaluation,
    NsedAgent, Proposal, Stance, TokenUsage,
};
use crate::emit_for;
use crate::llms::LlmRequestSpan;
use crate::llms::{AiModel, RequestConfig};
use crate::prompts::PromptSet;
use crate::telemetry::RetryReason;
use crate::tools::Tool;
use crate::tools::context::{
    ReadCritiquesTool, ReadOwnProposalTool, ReadProposalTool, SearchDeliberationTool,
};
use crate::tools::user_call::UserCallTool;

use anyhow::{Context, Result};
use async_openai::types::{
    ChatCompletionRequestAssistantMessage, ChatCompletionRequestMessage,
    ChatCompletionRequestSystemMessage, ChatCompletionRequestToolMessage,
    ChatCompletionRequestToolMessageContent, ChatCompletionRequestUserMessage, ChatCompletionTool,
    ChatCompletionToolType, FunctionObject,
};
use async_trait::async_trait;
use llm_repair::{
    clean_json_string, extract_evaluations_from_markdown, extract_proposal_from_markdown,
    extract_python_tool_calls, extract_xml_tool_calls, repair_aggressive_escapes,
    repair_conversational_response, repair_invalid_escapes, repair_tool_calls,
    repair_truncated_json, sanitize_json_string_lossy,
};
use serde::Deserialize;
use serde::de::DeserializeOwned;
use serde_json::{Value, json};
use std::collections::HashMap;
// use std::sync::Arc; // Unused
use tracing::{debug, info, instrument, warn};

/// Per-call cap on tool output as a fraction of the agent's
/// remaining context budget. 10% means a tool result never exceeds
/// 10% of `(context_window − estimated_input_tokens)` (in chars).
const TOOL_OUTPUT_FRACTION: f32 = 0.10;

/// Bytes-per-token rule of thumb. Conservative; matches the
/// `chars_per_token` ratio used elsewhere in the SDK. Used to map
/// the token-budget cap onto a byte cap on the raw `tool_output`.
const CHARS_PER_TOKEN: f32 = 4.0;

/// Trigger condition for the M3 auto-invocation: a serialized
/// message history that's already eaten ≥90% of the agent's
/// context window AND has at least a few tool calls' worth of
/// content to fold (`message_count > 4` ≈ system+user+≥1
/// assistant+≥1 tool). When `context_window <= 0` (provider doesn't
/// expose it) the trigger is disabled.
fn should_auto_compact(
    messages_chars: usize,
    context_window: i32,
    message_count: usize,
    chars_per_token: f32,
) -> bool {
    if context_window <= 0 || message_count <= 4 {
        return false;
    }
    let cpt = if chars_per_token > 0.0 {
        chars_per_token
    } else {
        4.0
    };
    let estimated_tokens = (messages_chars as f32 / cpt) as i32;
    let pct = (estimated_tokens as f64 / context_window as f64) * 100.0;
    pct >= 90.0
}

/// Outcome of a `compact_history` call.
pub struct CompactionResult {
    /// New message history with older tool calls folded into a
    /// synthetic `compact_history` tool-result pair.
    pub new_messages: Vec<ChatCompletionRequestMessage>,
    /// LLM-generated summary text to append to the scratchpad.
    pub summary: String,
    /// Number of tool-call results that were folded.
    pub compacted_count: usize,
}

/// Build a one-shot LLM request that summarises the older portion
/// of the conversation. Returns the new history (preamble + a
/// single synthetic compaction pair + the most-recent N tool
/// calls) and the summary text.
pub async fn compact_message_history(
    llm_client: &dyn crate::llms::AiModel,
    agent_config: &AgentConfig,
    messages: &[ChatCompletionRequestMessage],
    keep_last_n_calls: usize,
) -> anyhow::Result<CompactionResult> {
    use crate::llms::RequestConfig;
    use async_openai::types::ChatCompletionMessageToolCall;
    use async_openai::types::FunctionCall;

    // Treat keep=0 as keep=1: keeping zero recent tool results would
    // index `tool_msg_indices[len()]` and panic.
    let keep_last_n_calls = keep_last_n_calls.max(1);

    // Under `disable_native_tools` tool outputs are rewritten into
    // User messages prefixed with `Tool Output (` — fold those too.
    let is_tool_boundary = |m: &ChatCompletionRequestMessage| -> bool {
        match m {
            ChatCompletionRequestMessage::Tool(_) => true,
            ChatCompletionRequestMessage::User(u) => {
                if let async_openai::types::ChatCompletionRequestUserMessageContent::Text(t) =
                    &u.content
                {
                    t.starts_with("Tool Output (")
                } else {
                    false
                }
            }
            _ => false,
        }
    };
    let tool_msg_indices: Vec<usize> = messages
        .iter()
        .enumerate()
        .filter(|(_, m)| is_tool_boundary(m))
        .map(|(i, _)| i)
        .collect();

    if tool_msg_indices.len() <= keep_last_n_calls {
        return Ok(CompactionResult {
            new_messages: messages.to_vec(),
            summary: String::new(),
            compacted_count: 0,
        });
    }

    // Walk back to the parent Assistant turn so the kept tool_result
    // doesn't reference tool_calls that got folded into the summary —
    // the provider rejects orphaned tool messages.
    let raw_tool_cut = tool_msg_indices[tool_msg_indices.len() - keep_last_n_calls];
    let cut_idx = (0..raw_tool_cut)
        .rev()
        .find(|&i| matches!(messages[i], ChatCompletionRequestMessage::Assistant(_)))
        .unwrap_or(raw_tool_cut);

    // Anchor the preamble at the assistant turn, not the tool turn,
    // so we don't leave an orphan `tool_calls` whose result got folded.
    let first_foldable_tool_idx = messages
        .iter()
        .take(cut_idx)
        .position(is_tool_boundary)
        .unwrap_or(cut_idx);
    let preamble_end = (0..first_foldable_tool_idx)
        .rev()
        .find(|&i| matches!(messages[i], ChatCompletionRequestMessage::Assistant(_)))
        .unwrap_or(first_foldable_tool_idx);

    let to_summarize = &messages[preamble_end..cut_idx];
    let mut to_summarize_text = String::new();
    for m in to_summarize {
        match m {
            ChatCompletionRequestMessage::Tool(t) => {
                if let ChatCompletionRequestToolMessageContent::Text(s) = &t.content {
                    to_summarize_text.push_str("[tool_result] ");
                    to_summarize_text.push_str(s);
                    to_summarize_text.push_str("\n\n");
                }
            }
            ChatCompletionRequestMessage::Assistant(a) => {
                if let Some(tcs) = &a.tool_calls {
                    for tc in tcs {
                        to_summarize_text.push_str(&format!(
                            "[tool_call] {}({})\n",
                            tc.function.name, tc.function.arguments
                        ));
                    }
                }
            }
            ChatCompletionRequestMessage::User(u) => {
                // `disable_native_tools` mode: tool outputs land here
                // as text-rewritten User messages. Without this branch
                // the summariser sees zero tool evidence and the
                // compaction silently strips the actual data.
                if let async_openai::types::ChatCompletionRequestUserMessageContent::Text(t) =
                    &u.content
                    && t.starts_with("Tool Output (")
                {
                    to_summarize_text.push_str("[tool_result] ");
                    to_summarize_text.push_str(t);
                    to_summarize_text.push_str("\n\n");
                }
            }
            _ => {}
        }
    }

    let summarize_prompt = format!(
        "You are compressing an agent's accumulated tool-call history \
         so the agent can keep reasoning under tighter context pressure. \
         The summary will REPLACE the calls below in the conversation, \
         so anything you drop is gone.\n\n\
         Produce a structured summary using this exact section order. \
         Every section starts with an imperative — do what it says.\n\n\
         1. User Request and Primary Intent — Capture all explicit asks, \
         constraints, and the underlying goal. Look for: original request \
         phrasing, mid-task clarifications, success criteria, anything \
         the user explicitly forbade.\n\
         2. Methodology & Technical Concepts — List all approaches, \
         algorithms, APIs, formats, protocols, and libraries in play. \
         Look for: framework names and versions, data formats, patterns \
         being followed (TDD, RAG, …), and the rationale for choosing them.\n\
         3. References and Quotes — Capture all file paths read with \
         line ranges, verbatim quotes the agent has cited (≤ 1 line each), \
         and peer/external outputs referenced. Look for: file path + \
         line range for every read, commit SHAs, URLs, identifier names. \
         Drop full file dumps.\n\
         4. Errors, Fixes and Learnings — List all errors encountered, \
         the fix applied (or that it remains open), and the generalisable \
         lesson. Look for: exact error message, root cause, the change \
         that resolved it, what to avoid next time.\n\
         5. User Messages — Capture all explicit instructions, corrections, \
         and constraints from the user/orchestrator turn-by-turn. Look for: \
         verbatim phrasing that disambiguates intent, `stop`/`don't`/\
         `instead` pivots, deadlines, scope cuts.\n\
         6. Pending Work — List all open tool calls, unfinished sub-goals, \
         and known follow-ups. Look for: what is queued, why it hasn't \
         been done yet, dependencies on other steps.\n\
         7. Current Work — Capture the agent's most recent action and what \
         it was about to do next. Look for: the in-flight tool call, \
         the file/function being inspected, the immediate next move.\n\n\
         === HISTORY TO SUMMARISE ===\n{to_summarize_text}"
    );

    let request_config = RequestConfig {
        messages: vec![
            ChatCompletionRequestUserMessage {
                content: summarize_prompt.into(),
                ..Default::default()
            }
            .into(),
        ],
        tools: None,
        tool_choice: None,
        presence_penalty: None,
    };

    let result = llm_client
        .chat_completion(agent_config, request_config)
        .await
        .map_err(|e| anyhow::anyhow!("compact_history summariser call failed: {e}"))?;
    let summary = result
        .response
        .choices
        .first()
        .and_then(|c| c.message.content.clone())
        .filter(|s| !s.trim().is_empty())
        .ok_or_else(|| anyhow::anyhow!("compact_history summariser returned empty content"))?;

    // `total - keep_last_n_calls` lies when an assistant batch emits
    // multiple tool calls; count what's actually before `cut_idx`.
    let actually_compacted = tool_msg_indices
        .iter()
        .take_while(|&&i| i < cut_idx)
        .count();

    // When `disable_native_tools` is on, the rest of the loop flattens
    // tool traffic into User-message text; injecting native tool_calls
    // + Tool roles here would land in a request that sets `tools: None`
    // and the provider rejects the protocol mismatch. Mirror the
    // existing `Tool Output (...)` rewrite convention so subsequent
    // is_tool_boundary scans still recognise the pair.
    let (synth_assistant, synth_tool_result): (
        ChatCompletionRequestMessage,
        ChatCompletionRequestMessage,
    ) = if agent_config.disable_native_tools {
        let assistant: ChatCompletionRequestMessage = ChatCompletionRequestAssistantMessage {
            content: Some(
                async_openai::types::ChatCompletionRequestAssistantMessageContent::Text(format!(
                    "[compact_history(keep_last_n_calls={keep_last_n_calls})]"
                )),
            ),
            tool_calls: None,
            ..Default::default()
        }
        .into();
        let result: ChatCompletionRequestMessage = ChatCompletionRequestUserMessage {
            content: format!(
                "Tool Output (compact_history): [Compacted {actually_compacted} earlier \
                 tool calls into scratchpad. Summary:]\n{summary}"
            )
            .into(),
            ..Default::default()
        }
        .into();
        (assistant, result)
    } else {
        let compact_tool_call_id = format!("compact_history_{}", uuid::Uuid::new_v4().simple());
        let assistant: ChatCompletionRequestMessage = ChatCompletionRequestAssistantMessage {
            tool_calls: Some(vec![ChatCompletionMessageToolCall {
                id: compact_tool_call_id.clone(),
                r#type: ChatCompletionToolType::Function,
                function: FunctionCall {
                    name: "compact_history".into(),
                    arguments: json!({ "keep_last_n_calls": keep_last_n_calls }).to_string(),
                },
            }]),
            ..Default::default()
        }
        .into();
        let result: ChatCompletionRequestMessage = ChatCompletionRequestToolMessage {
            tool_call_id: compact_tool_call_id,
            content: ChatCompletionRequestToolMessageContent::Text(format!(
                "[Compacted {actually_compacted} earlier tool calls into scratchpad. \
                 Summary:]\n{summary}"
            )),
        }
        .into();
        (assistant, result)
    };

    let mut new_messages = messages[..preamble_end].to_vec();
    new_messages.push(synth_assistant);
    new_messages.push(synth_tool_result);
    new_messages.extend(messages[cut_idx..].iter().cloned());

    Ok(CompactionResult {
        new_messages,
        summary,
        compacted_count: actually_compacted,
    })
}

/// Squeeze a scratchpad whose length has crossed the
/// `agent_config.scratchpad_squeeze_fraction` threshold. Calls the
/// agent's own LLM to compact older sections; preserves the trailing
/// 25% verbatim so the most-recent context stays intact.
pub async fn squeeze_scratchpad_if_full(
    llm_client: &dyn crate::llms::AiModel,
    agent_config: &AgentConfig,
    scratchpad: &str,
    max_scratchpad_size: usize,
) -> anyhow::Result<Option<String>> {
    use crate::llms::RequestConfig;

    if max_scratchpad_size == 0 || scratchpad.is_empty() {
        return Ok(None);
    }
    let ratio = scratchpad.len() as f64 / max_scratchpad_size as f64;
    if ratio < agent_config.scratchpad_squeeze_fraction {
        return Ok(None);
    }

    let cut = scratchpad.len() * 3 / 4;
    let safe_cut = (0..=cut)
        .rev()
        .find(|&i| scratchpad.is_char_boundary(i))
        .unwrap_or(0);
    let (older, recent) = scratchpad.split_at(safe_cut);

    let prompt = format!(
        "The agent's scratchpad is at {pct}% of its capacity \
         ({cur}/{max} chars). Compress the OLDER section using the \
         numbered template below. The RECENT section will be appended \
         verbatim — DO NOT touch it.\n\n\
         The scratchpad is the agent's persistent reasoning notebook \
         across rounds of a multi-agent deliberation. Every section \
         starts with an imperative — do what it says.\n\n\
         1. Primary Request and Intent — Capture the deliberation task \
         verbatim plus the success criterion the orchestrator set. Look \
         for: the framing question, scoring rubric, hard constraints.\n\
         2. Key Findings and Stances — List all conclusions this agent \
         has reached and the position it is defending. Look for: claim \
         + supporting evidence + confidence level.\n\
         3. Rounds and Phases — For each round/phase already assessed, \
         capture a one-line conclusion. Look for: round id, phase \
         (Propose/Evaluate/Refine), what changed since the previous \
         round.\n\
         4. Key Team Disagreements — List all points where peer agents \
         diverged and the evidence each side cited. Look for: peer \
         name, claim, counter-claim, evidence anchor.\n\
         5. Decisions Made — Capture every commitment the agent has \
         made and no longer plans to revisit. Look for: decision + \
         reason + when made.\n\
         6. References and Quotes — Capture all file paths read with \
         line ranges, verbatim quotes (≤ 1 line each), and peer outputs \
         referenced. Look for: path + line range, commit SHAs, URLs, \
         identifier names. Drop full dumps.\n\
         7. Errors, Fixes and Learnings — List all errors, the fix, and \
         the generalisable lesson. Look for: error message, root cause, \
         the change that resolved it.\n\
         8. Current Focus — Capture what the agent is reasoning about \
         right now and what would change its mind. Look for: the open \
         question, the falsifier, the next piece of evidence sought.\n\
         9. Optional Next Step — Capture the single most useful next \
         move, or `None`. Look for: a concrete tool call or peer reply \
         the agent should issue immediately.\n\n\
         Drop boilerplate, process narration, and anything already \
         implied by the RECENT section.\n\n\
         === OLDER (compress this) ===\n{older}\n\n\
         === RECENT (do not touch) ===\n{recent}",
        pct = (ratio * 100.0).round() as u32,
        cur = scratchpad.len(),
        max = max_scratchpad_size,
    );

    let request_config = RequestConfig {
        messages: vec![
            ChatCompletionRequestUserMessage {
                content: prompt.into(),
                ..Default::default()
            }
            .into(),
        ],
        tools: None,
        tool_choice: None,
        presence_penalty: None,
    };
    let result = llm_client
        .chat_completion(agent_config, request_config)
        .await
        .map_err(|e| anyhow::anyhow!("scratchpad squeeze summariser call failed: {e}"))?;
    let compressed = result
        .response
        .choices
        .first()
        .and_then(|c| c.message.content.clone())
        .filter(|s| !s.trim().is_empty())
        .ok_or_else(|| anyhow::anyhow!("scratchpad squeeze summariser returned empty content"))?;

    let candidate = format!("{compressed}\n\n{recent}");
    if candidate.len() >= scratchpad.len() || candidate.len() > max_scratchpad_size {
        warn!(
            old_len = scratchpad.len(),
            new_len = candidate.len(),
            max = max_scratchpad_size,
            "scratchpad squeeze produced no improvement; keeping original"
        );
        return Ok(None);
    }
    Ok(Some(candidate))
}

/// Apply [`TOOL_OUTPUT_FRACTION`] cap to `tool_output` in place.
/// Truncates on a UTF-8 char boundary and appends a marker so the
/// model sees that the tool clipped its result. Returns `true`
/// when the cap engaged.
///
/// `context_window <= 0` (provider doesn't expose it) preserves
/// the legacy uncapped behavior; the SDK's downstream shrink-guard
/// is the only protection in that case.
fn apply_tool_output_cap(
    tool_output: &mut String,
    context_window: i32,
    estimated_input_tokens: u32,
    tool_name: &str,
) -> bool {
    if context_window <= 0 {
        return false;
    }
    let remaining_tokens = (context_window as i64 - estimated_input_tokens as i64).max(0) as f32;
    let cap_bytes = (remaining_tokens * CHARS_PER_TOKEN * TOOL_OUTPUT_FRACTION) as usize;
    if tool_output.len() <= cap_bytes {
        return false;
    }
    // Zero remaining context budget — replace the result with a
    // marker. Anything else lets the SDK shrink-guard fire on the
    // next call with a 200-token output budget, breaking downstream
    // JSON.
    if cap_bytes == 0 {
        let original_len = tool_output.len();
        tool_output.clear();
        tool_output.push_str(
            "[truncated: no remaining context budget; \
             re-call with offset/regex/page to scope the read]",
        );
        warn!(
            tool_name = %tool_name,
            original_len,
            cap_bytes = 0,
            "tool result fully replaced with marker; context exhausted"
        );
        return true;
    }
    let original_len = tool_output.len();
    let safe_cap = (0..=cap_bytes)
        .rev()
        .find(|&i| tool_output.is_char_boundary(i))
        .unwrap_or(0);
    tool_output.truncate(safe_cap);
    tool_output.push_str(&format!(
        "\n[truncated: tool result clipped at {} bytes \
         ({:.0}% of remaining context); original was {} bytes — \
         re-call with offset/regex/page to scope the read]",
        safe_cap,
        TOOL_OUTPUT_FRACTION * 100.0,
        original_len
    ));
    warn!(
        tool_name = %tool_name,
        original_len,
        cap_bytes = safe_cap,
        "tool result truncated to per-call context-budget cap"
    );
    true
}

/// Write a structured failure dump to disk (opt-in via `NSED_FAILURE_DUMPS=1|full`).
///
/// Dumps are organized as **one directory per job** under `failures/`:
///
/// ```text
/// failures/
///   a1b2c3d4_DEFAULT/           # session_id prefix + agent name
///     parse_error_r2.md          # all parse retries for round 2 appended here
///     api_error_r3.md            # API failure in round 3
///   e5f6g7h8_ARCHIT/
///     parse_error_r1.md
/// ```
///
/// Subsequent failures for the same job+kind+round are **appended** rather
/// than creating new files, so you can follow the retry evolution in one doc.
/// Full context (system prompt, request body, messages) is only written on the
/// first entry — retries share the same prompt, so repeating it wastes space.
///
/// Returns `Some(path)` on success so callers can log the filename.
fn write_failure_dump(params: FailureDumpParams<'_>) -> Option<String> {
    // Config value takes precedence over env var. When the config explicitly
    // says "off", we must NOT fall through to the env var.
    let dump_mode = match params.failure_dumps_config {
        Some(v) => {
            let v = v.trim().to_lowercase();
            if v == "off" || v.is_empty() {
                None // Config explicitly disabled — short-circuit, skip env var
            } else {
                Some(v)
            }
        }
        None => std::env::var("NSED_FAILURE_DUMPS").ok(),
    };
    let dump_mode = dump_mode?;
    // Only enable dumps for explicit opt-in values: "1", "on", or "full".
    let is_full = dump_mode.eq_ignore_ascii_case("full");
    if dump_mode != "1" && !dump_mode.eq_ignore_ascii_case("on") && !is_full {
        return None;
    }

    let timestamp = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();
    let safe_name = params.agent_name.replace(['/', '\\', ' ', '(', ')'], "_");

    // ── Job directory: one per session (or fallback key) ─────────────
    let job_dir_name = if let Some(sid) = params.session_id {
        let safe_session = sid.replace(['/', '\\', ' ', '(', ')', '.', ':'], "_");
        let short: String = safe_session.chars().take(8).collect();
        format!("{short}_{safe_name}")
    } else {
        // No session_id: use a timestamp-based directory so we still group
        // within a single invocation (parse retries hit the same dir).
        format!("{timestamp}_{safe_name}")
    };
    let job_dir = format!("failures/{job_dir_name}");

    // File within the job directory: one per error kind + round
    let round = params.round.unwrap_or(0);
    let filename = format!("{job_dir}/{kind}_r{round}.md", kind = params.kind);

    // ── Determine if this is a new file or an append ─────────────────
    let is_append = std::path::Path::new(&filename).exists();

    let mut out = String::with_capacity(4096);

    if is_append {
        out.push_str("\n---\n\n");
    }

    // ── Section header ───────────────────────────────────────────────
    let attempt_label = params
        .attempt
        .map(|a| format!(" (attempt {a})"))
        .unwrap_or_default();
    out.push_str(&format!(
        "# {kind}{attempt_label} — {timestamp}\n\n",
        kind = params.kind,
    ));

    // ── Metadata table ───────────────────────────────────────────────
    out.push_str("| field | value |\n|---|---|\n");
    out.push_str(&format!("| agent | {} |\n", params.agent_name));
    out.push_str(&format!("| model | {} |\n", params.model_name));
    out.push_str(&format!("| provider | {} |\n", params.provider_id));
    if let Some(sid) = params.session_id {
        out.push_str(&format!("| session_id | {sid} |\n"));
    }
    if let Some(phase) = params.phase {
        out.push_str(&format!("| phase | {phase} |\n"));
    }
    out.push_str(&format!("| round | {round} |\n"));
    if let Some(attempt) = params.attempt {
        out.push_str(&format!("| attempt | {attempt} |\n"));
    }
    if let Some(fr) = params.finish_reason {
        out.push_str(&format!("| finish_reason | {fr} |\n"));
    }
    if let Some(t) = params.input_tokens {
        out.push_str(&format!("| input_tokens | {t} |\n"));
    }
    if let Some(t) = params.output_tokens {
        out.push_str(&format!("| output_tokens | {t} |\n"));
    }
    out.push('\n');

    // ── Error ────────────────────────────────────────────────────────
    out.push_str("## Error\n\n```\n");
    out.push_str(params.error);
    out.push_str("\n```\n\n");

    // ── LLM response (parse-error only) ──────────────────────────────
    if let Some(response) = params.response_content {
        let truncated = truncate_for_dump(response, 4000);
        out.push_str("## LLM Response\n\n```\n");
        out.push_str(&truncated);
        out.push_str("\n```\n\n");
    }

    // ── Full context (opt-in, first entry only) ──────────────────────
    // System prompt and messages are identical across retries — only
    // include them on the first write to save space + context.
    if is_full && !is_append {
        if let Some(sys) = params.system_prompt {
            out.push_str("## System Prompt\n\n```\n");
            out.push_str(sys);
            out.push_str("\n```\n\n");
        }
        if let Some(body) = params.request_body {
            let truncated = truncate_for_dump(body, 8000);
            out.push_str("## Request Body\n\n```json\n");
            out.push_str(&truncated);
            out.push_str("\n```\n\n");
        }
        if let Some(msgs) = params.messages {
            let msgs_json =
                serde_json::to_string_pretty(msgs).unwrap_or_else(|_| format!("{msgs:#?}"));
            let truncated = truncate_for_dump(&msgs_json, 12000);
            out.push_str("## Messages\n\n```json\n");
            out.push_str(&truncated);
            out.push_str("\n```\n\n");
        }
    } else if !is_full && !is_append {
        out.push_str(
            "_Set `NSED_FAILURE_DUMPS=full` to include system prompt, request body, and messages._\n",
        );
    }

    // ── Write / append to disk ───────────────────────────────────────
    let write_result = std::fs::create_dir_all(&job_dir).and_then(|_| {
        use std::io::Write;
        let mut file = std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&filename)?;
        file.write_all(out.as_bytes())
    });
    if let Err(io_err) = write_result {
        warn!("Failed to write failure dump: {}", io_err);
        return None;
    }

    // ── Prune old job directories (NSED_FAILURE_DUMPS_MAX, default 20)
    let max_dirs: usize = std::env::var("NSED_FAILURE_DUMPS_MAX")
        .ok()
        .and_then(|v| v.parse().ok())
        .unwrap_or(20);
    prune_failure_dirs("failures", max_dirs);

    Some(filename)
}

/// Remove oldest **job directories** from `failures/` when the count exceeds
/// `max_dirs`. Sorts by filesystem modified-time (oldest first) and deletes
/// the surplus recursively.  Best-effort: silently ignores I/O errors.
fn prune_failure_dirs(parent: &str, max_dirs: usize) {
    let entries: Vec<_> = match std::fs::read_dir(parent) {
        Ok(rd) => rd
            .filter_map(|e| e.ok())
            .filter(|e| e.file_type().map(|ft| ft.is_dir()).unwrap_or(false))
            .collect(),
        Err(_) => return,
    };
    if entries.len() <= max_dirs {
        return;
    }
    let mut by_mtime: Vec<_> = entries
        .into_iter()
        .filter_map(|e| {
            let mtime = e.metadata().ok()?.modified().ok()?;
            Some((mtime, e.path()))
        })
        .collect();
    by_mtime.sort_by_key(|(t, _)| *t);

    let to_remove = by_mtime.len().saturating_sub(max_dirs);
    for (_, path) in by_mtime.into_iter().take(to_remove) {
        let _ = std::fs::remove_dir_all(path);
    }
}

/// Parameters for [`write_failure_dump`].
struct FailureDumpParams<'a> {
    kind: &'a str,
    agent_name: &'a str,
    model_name: &'a str,
    provider_id: &'a str,
    error: &'a str,
    session_id: Option<&'a str>,
    phase: Option<&'a str>,
    round: Option<u32>,
    attempt: Option<usize>,
    finish_reason: Option<&'a str>,
    input_tokens: Option<u32>,
    output_tokens: Option<u32>,
    response_content: Option<&'a str>,
    system_prompt: Option<&'a str>,
    request_body: Option<&'a str>,
    messages: Option<&'a [ChatCompletionRequestMessage]>,
    /// From `AgentConfig.failure_dumps` — takes precedence over env var.
    failure_dumps_config: Option<&'a str>,
}

/// Truncate a string for dump files. Respects char boundaries.
fn truncate_for_dump(s: &str, max_chars: usize) -> String {
    if s.chars().count() <= max_chars {
        s.to_string()
    } else {
        let truncated: String = s.chars().take(max_chars).collect();
        format!("{truncated}\n... (truncated at {max_chars} chars)")
    }
}

// Define AgentResponse locally
#[derive(Debug, Clone)]
pub struct AgentResponse {
    pub content: String,
    pub tool_usage: HashMap<String, usize>,
    pub finish_reason: Option<String>,
    pub input_tokens: Option<u32>,
    pub output_tokens: Option<u32>,
    pub system_prompt: Option<String>,
    pub request_body: Option<String>,
    pub history: Vec<ChatCompletionRequestMessage>,
    pub final_scratchpad: Option<String>,
}

#[derive(Debug, Deserialize, serde::Serialize)]
#[allow(dead_code)]
struct BatchEvaluationItem {
    #[serde(alias = "candidate_id", default)]
    agent_id: String,
    #[serde(alias = "score")]
    endorsement_weight: f32,
    #[serde(default)]
    justification: Option<String>,
    #[serde(default)]
    is_final_solution: bool,
    #[serde(default)]
    stance: Option<Stance>,
    #[serde(default)]
    claim_assessments: Vec<ClaimAssessment>,
    #[serde(default)]
    disagreements: Vec<DisagreementPoint>,
    #[serde(default)]
    category_scores: Option<CategoryScores>,
}

#[derive(Debug, Deserialize, serde::Serialize)]
struct StructuredBatchEvaluationResponse {
    /// Models sometimes use "candidate_evaluations" or "candidates" instead of "evaluations".
    #[serde(alias = "candidate_evaluations", alias = "candidates")]
    evaluations: Vec<BatchEvaluationItem>,
}

#[derive(Debug, Deserialize, serde::Serialize)]
struct StructuredProposalResponse {
    #[serde(deserialize_with = "deserialize_string_or_array_or_object")]
    thought_process: String,
    #[serde(deserialize_with = "deserialize_string_or_array_or_object")]
    solution_content: String,
}

/// Accepts a JSON string, array of strings (joined with "\n"), or any object
/// (serialized to a compact JSON string). Models like Mistral frequently return
/// `"thought_process": ["Step 1: ...", "Step 2: ..."]` instead of a single string,
/// or nest the entire answer inside `"solution_content": { ... }`.
fn deserialize_string_or_array_or_object<'de, D>(deserializer: D) -> Result<String, D::Error>
where
    D: serde::Deserializer<'de>,
{
    use serde_json::Value;
    let value = Value::deserialize(deserializer)?;
    match value {
        Value::String(s) => Ok(s),
        Value::Array(arr) => {
            let parts: Vec<String> = arr
                .into_iter()
                .map(|v| match v {
                    Value::String(s) => s,
                    other => other.to_string(),
                })
                .collect();
            Ok(parts.join("\n"))
        }
        other => Ok(other.to_string()),
    }
}

/// Strips thinking-token prefixes leaked by reasoning models (e.g. gpt-oss-120b).
/// Common patterns:
///   "analysisWe need to...assistantfinal**Critique..."  → "**Critique..."
///   "final**Asset-class allocation..."                  → "**Asset-class allocation..."
///   "commentaryto=functions.submit_proposal json{...}"  → "{...}"
/// Normalize an `agent_response.finish_reason` string to the
/// lowercase wire values documented on `Proposal::finish_reason`
/// (`stop`, `tool_calls`, `length`, ...) before export to DTOs /
/// downstream consumers. The internal producer uses
/// `format!("{r:?}")` on the OpenAI enum, which yields Debug form
/// (`Stop`, `ToolCalls`, `Length`) — fine for intra-module flow
/// checks, wrong for the public contract. Unknown values fall
/// through lowercased so callers see something stable instead of
/// nothing. The synthetic `"max_iterations"` sentinel already
/// matches the wire format and passes through untouched.
fn normalize_finish_reason(raw: &str) -> String {
    match raw {
        "Stop" => "stop".to_string(),
        "ToolCalls" => "tool_calls".to_string(),
        "Length" => "length".to_string(),
        "ContentFilter" => "content_filter".to_string(),
        "FunctionCall" => "function_call".to_string(),
        other => other.to_ascii_lowercase(),
    }
}

fn strip_thinking_prefix(content: &str) -> &str {
    // Trim leading whitespace so prefixes are detected even when LLMs
    // emit leading spaces/newlines.
    let mut s = content.trim_start();
    // Strip "analysis...assistant" prefix (model internal tokens leaked into output)
    if let Some(pos) = s.find("assistant") {
        let after = &s[pos + "assistant".len()..];
        // Only strip if what follows looks like content (not mid-word)
        if after.starts_with("final")
            || after.starts_with("{")
            || after.starts_with("**")
            || after.starts_with('#')
        {
            s = after;
        }
    }
    // Strip "final" prefix
    if s.starts_with("final") {
        s = &s["final".len()..];
    }
    // Strip "commentary..." prefix (gpt-oss commentary tokens)
    if s.starts_with("commentary") {
        // Find the start of actual content after "commentaryto=functions.xxx json"
        if let Some(json_pos) = s.find("json{") {
            s = &s[json_pos + "json".len()..];
        } else if let Some(json_pos) = s.find("json ") {
            s = &s[json_pos + "json ".len()..];
        }
    }
    s.trim()
}

#[derive(Debug, Clone)]
pub struct ProposerEvaluatorAgent {
    pub config: AgentConfig,
    llm: Box<dyn AiModel>,
    prompt_set: Box<dyn PromptSet>,
    pub extra_context_tools: Vec<Box<dyn Tool>>,
    pub sandbox_tools: Vec<Box<dyn Tool>>,
    /// Optional output-leak detector used by `prompt_exposure_guard`.
    /// `None` disables guarding; attach any `OutputLeakDetector` impl
    /// via [`ProposerEvaluatorAgent::with_output_guard`].
    output_guard: Option<std::sync::Arc<dyn crate::agents::OutputLeakDetector>>,
}

impl ProposerEvaluatorAgent {
    /// Assemble the full tool set for a given context.
    ///
    /// Clones extra_context_tools and sandbox_tools, then conditionally injects
    /// NSED protocol tools (read_proposal, read_critiques, read_own_proposal) and
    /// user-defined tools when their prerequisites (store, handler) are available.
    fn aggregate_tools(&self, context: &AgentContext) -> Vec<Box<dyn Tool>> {
        let mut all_tools: Vec<Box<dyn Tool>> = self
            .extra_context_tools
            .iter()
            .map(|t| dyn_clone::clone_box(t.as_ref()))
            .collect::<Vec<_>>();
        all_tools.extend(
            self.sandbox_tools
                .iter()
                .map(|t| dyn_clone::clone_box(t.as_ref())),
        );

        // Automatically inject Context Tools if store is available
        if let Some(store) = &context.store {
            let read_tool = ReadProposalTool::new(store.clone(), context.round_number);
            all_tools.push(Box::new(read_tool));

            let critiques_tool = ReadCritiquesTool::new(store.clone(), context.round_number);
            all_tools.push(Box::new(critiques_tool));

            let own_tool = ReadOwnProposalTool::new(
                store.clone(),
                context.round_number,
                self.config.name.clone(),
            );
            all_tools.push(Box::new(own_tool));

            let search_tool = SearchDeliberationTool::new(store.clone(), context.round_number);
            all_tools.push(Box::new(search_tool));

            debug!(agent=%self.config.name, "Injected NSED protocol tools (read_proposal, search_deliberation, etc.) via persistent store.");
        }

        // Inject user-defined tools if handler is available
        if let Some(ref handler) = context.user_tool_handler {
            for def in &context.user_tools {
                let user_tool = UserCallTool::new(
                    def.clone(),
                    handler.clone(),
                    context.round_number,
                    context.phase,
                );
                all_tools.push(Box::new(user_tool));
            }
            if !context.user_tools.is_empty() {
                debug!(agent=%self.config.name, count=%context.user_tools.len(), "Injected user-defined tools");
            }
        }

        // Only inject the sandboxed read_file tool for openai-family
        // providers. Claude/MCP/exec already have their own filesystem
        // affordances; mounting a second one would broaden capability
        // beyond the scope this PR was approved for.
        if crate::agents::config::is_openai_family_provider(&self.config)
            && !self.config.read_file_roots.is_empty()
        {
            all_tools.push(Box::new(
                crate::tools::scoped_read::ScopedReadFileTool::new(
                    self.config.name.clone(),
                    &self.config.read_file_roots,
                ),
            ));
        }

        // Debug log for active tools
        let tool_names: Vec<String> = all_tools.iter().map(|t| t.name()).collect();
        debug!(agent=%self.config.name, available_tools=?tool_names, "Tools configured for this run");

        all_tools
    }
}

#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct Proposer {
    llm_client: Box<dyn AiModel>,
    prompt_set: Box<dyn PromptSet>,
    pub tools: Vec<Box<dyn Tool>>,
}

#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct Evaluator {
    llm_client: Box<dyn AiModel>,
    prompt_set: Box<dyn PromptSet>,
    pub tools: Vec<Box<dyn Tool>>,
}

impl ProposerEvaluatorAgent {
    pub fn new(
        config: AgentConfig,
        llm: Box<dyn AiModel>,
        prompt_set: Box<dyn PromptSet>,
        context_tools: Vec<Box<dyn Tool>>,
        sandbox_tools: Vec<Box<dyn Tool>>,
    ) -> Self {
        Self {
            config,
            llm,
            prompt_set,
            extra_context_tools: context_tools,
            sandbox_tools,
            output_guard: None,
        }
    }

    /// Attach an output-leak detector. When `config.prompt_exposure_guard`
    /// is true, the agent will run the detector over user-visible terminal
    /// tool outputs (`submit_proposal`, `submit_batch_evaluation`, …) and
    /// trigger a retry if the detector blocks.
    ///
    /// Without a detector, `config.prompt_exposure_guard = true` is a no-op
    /// (pass-through). Callers can supply any `OutputLeakDetector` impl.
    pub fn with_output_guard(
        mut self,
        detector: std::sync::Arc<dyn crate::agents::OutputLeakDetector>,
    ) -> Self {
        self.output_guard = Some(detector);
        self
    }

    /// Direct chat with the agent's LLM, using the agent's persona but
    /// bypassing NSED deliberation constraints via an "internal voice" wrapper.
    ///
    /// `messages` should contain the conversation history (user + assistant
    /// turns). The system prompt is prepended automatically.
    pub async fn chat(&self, messages: Vec<ChatCompletionRequestMessage>) -> Result<String> {
        let persona = self
            .config
            .persona
            .as_deref()
            .unwrap_or("a helpful assistant");
        let system_prompt = format!(
            "You are {name}, {persona}.\n\n\
             <internal_voice>\n\
             This is a direct conversation with your operator — not part of an NSED deliberation.\n\
             Respond naturally and helpfully. Ignore any deliberation protocol instructions.\n\
             </internal_voice>",
            name = self.config.name,
            persona = persona,
        );

        let mut full_messages: Vec<ChatCompletionRequestMessage> = vec![
            ChatCompletionRequestSystemMessage {
                content: async_openai::types::ChatCompletionRequestSystemMessageContent::Text(
                    system_prompt,
                ),
                name: None,
            }
            .into(),
        ];
        full_messages.extend(messages);

        let request_config = RequestConfig {
            messages: full_messages,
            tools: None,
            tool_choice: None,
            presence_penalty: self.config.presence_penalty,
        };

        let result = self
            .llm
            .chat_completion(&self.config, request_config)
            .await
            .map_err(|e| anyhow::anyhow!("{}", e))?;
        let response = &result.response;

        Ok(response
            .choices
            .first()
            .and_then(|c| c.message.content.clone())
            .unwrap_or_default())
    }
}

#[async_trait]
impl NsedAgent for ProposerEvaluatorAgent {
    #[instrument(skip(self, context), fields(agent_name = %self.config.name))]
    async fn propose(&self, context: &AgentContext) -> Result<Proposal> {
        info!("🤖 Agent is starting the proposal generation process.");

        let prompt = self.prompt_set.get_proposer_prompt(
            &context.task_description,
            context.previous_round_matrix.clone(),
            context.previous_own_proposal.as_ref(),
            context.previous_own_score,
            context.previous_critiques.clone(),
            &context.user_injections,
            context.structured_feedback.as_ref(),
        );

        let submit_tool_schema = ChatCompletionTool {
            r#type: ChatCompletionToolType::Function,
            function: FunctionObject {
                name: "submit_proposal".to_string(),
                description: Some("Submit the final proposal.".to_string()),
                parameters: Some(json!({
                    "type": "object",
                    "properties": {
                        "thought_process": { "type": "string" },
                        "solution_content": { "type": "string" }
                    },
                    "required": ["thought_process", "solution_content"],
                    "additionalProperties": false
                })),
                strict: Some(true),
            },
        };

        let all_tools = self.aggregate_tools(context);

        let (response, agent_response): (StructuredProposalResponse, AgentResponse) =
            generate_structured_output(
                &*self.llm,
                &self.config,
                &*self.prompt_set,
                context,
                prompt,
                &all_tools,
                submit_tool_schema,
                "submit_proposal",
                self.output_guard.as_deref(),
            )
            .await?;

        info!("🧠 Agent Reasoning: {}", response.thought_process);

        if let Some(store) = &context.store {
            // Phase 6B: Strip ephemeral <working_memory> before persisting for next round.
            // Retains <key_findings> and <strategy> sections.
            if let Ok(Some(current)) = store.get(&self.config.name).await {
                let cleaned = strip_working_memory(&current);
                if cleaned.len() != current.len() {
                    if let Err(e) = store.set(&self.config.name, &cleaned).await {
                        warn!(error=%e, "Failed to persist scratchpad after working memory rotation");
                    }
                }
            }

            let max_len = self.config.scratchpad_limit as usize;
            let mut note = format!(
                "Round {} Proposal:\nThought Process: {}\nContent: {}\n",
                context.round_number, response.thought_process, response.solution_content
            );
            if note.len() > max_len {
                const TRUNCATION_SUFFIX: &str = "...(truncated)";
                // Truncate to max_len minus suffix length, finding a safe UTF-8 boundary
                let truncate_at = max_len.saturating_sub(TRUNCATION_SUFFIX.len());
                // floor_char_boundary equivalent: find the last char boundary <= truncate_at
                let safe_at = note
                    .char_indices()
                    .map(|(i, _)| i)
                    .take_while(|&i| i <= truncate_at)
                    .last()
                    .unwrap_or(0);
                note.truncate(safe_at);
                note.push_str(TRUNCATION_SUFFIX);
            }
            if let Err(e) = store.append(&self.config.name, &note).await {
                warn!(error=%e, "Failed to persist proposal to scratchpad");
            }
        }

        Ok(Proposal {
            thought_process: response.thought_process,
            content: response.solution_content,
            final_scratchpad: agent_response.final_scratchpad.clone(), // Capture the scratchpad
            token_usage_stats: Some(TokenUsage {
                input_tokens: agent_response.input_tokens.unwrap_or(0),
                output_tokens: agent_response.output_tokens.unwrap_or(0),
            }),
            // Propagate terminal signal ("max_iterations" partial
            // fallback, otherwise LLM-driven stop/tool_calls). Lets
            // the orchestrator + dashboard distinguish partial output
            // from full completions without re-reading LLM internals.
            finish_reason: agent_response
                .finish_reason
                .as_deref()
                .map(normalize_finish_reason),
            ..Default::default()
        })
    }

    #[instrument(skip(self, context), fields(agent_name = %self.config.name))]
    async fn evaluate(&self, context: &AgentContext) -> Result<Vec<(String, Evaluation)>> {
        info!("🕵️ Agent is starting the batch proposal evaluation process.");

        let eval_item_properties = json!({
            "agent_id": { "type": "string" },
            "endorsement_weight": { "type": "number" },
            "justification": { "type": "string" },
            "is_final_solution": { "type": "boolean" },
            "stance": {
                "type": ["string", "null"],
                "enum": ["strong_agree", "agree", "neutral", "disagree", "strong_disagree", null]
            },
            "claim_assessments": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "claim_id": { "type": ["string", "null"] },
                        "claim": { "type": "string" },
                        "verdict": { "type": "string", "enum": ["verified", "contested", "unverified", "wrong"] },
                        "reason": { "type": ["string", "null"] }
                    },
                    "required": ["claim_id", "claim", "verdict", "reason"],
                    "additionalProperties": false
                }
            },
            "disagreements": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "claim_id": { "type": ["string", "null"] },
                        "proposal_claims": { "type": "string" },
                        "evaluator_position": { "type": "string" },
                        "confidence": { "type": "string", "enum": ["high", "medium", "low"] }
                    },
                    "required": ["claim_id", "proposal_claims", "evaluator_position", "confidence"],
                    "additionalProperties": false
                }
            },
            "category_scores": {
                "type": ["object", "null"],
                "properties": {
                    "correctness": { "type": "number" },
                    "completeness": { "type": "number" },
                    "novelty": { "type": "number" },
                    "feasibility": { "type": "number" },
                    "evidence_quality": { "type": "number" }
                },
                "required": ["correctness", "completeness", "novelty", "feasibility", "evidence_quality"],
                "additionalProperties": false
            }
        });

        let batch_evaluation_tool = ChatCompletionTool {
            r#type: ChatCompletionToolType::Function,
            function: FunctionObject {
                name: "submit_batch_evaluation".to_string(),
                description: Some(
                    "Submit batch evaluations with structured claim-level analysis.".to_string(),
                ),
                parameters: Some(json!({
                    "type": "object",
                    "properties": {
                        "evaluations": {
                            "type": "array",
                            "items": {
                                "type": "object",
                                "properties": eval_item_properties,
                                "required": ["agent_id", "endorsement_weight", "justification", "is_final_solution",
                                             "stance", "claim_assessments", "disagreements", "category_scores"],
                                "additionalProperties": false
                            }
                        }
                    },
                    "required": ["evaluations"],
                    "additionalProperties": false
                })),
                strict: Some(true),
            },
        };

        let prompt = self.prompt_set.get_batch_evaluator_prompt(
            &context.task_description,
            &context.candidates,
            context.previous_own_proposal.as_ref(),
            context.round_number as usize,
            &context.user_injections,
        );

        let all_tools = self.aggregate_tools(context);

        let (structured_response, agent_response): (
            StructuredBatchEvaluationResponse,
            AgentResponse,
        ) = generate_structured_output(
            &*self.llm,
            &self.config,
            &*self.prompt_set,
            context,
            prompt,
            &all_tools,
            batch_evaluation_tool,
            "submit_batch_evaluation",
            self.output_guard.as_deref(),
        )
        .await?;

        // Token usage for this entire evaluation batch (one LLM call produces all evals)
        let batch_token_usage = Some(TokenUsage {
            input_tokens: agent_response.input_tokens.unwrap_or(0),
            output_tokens: agent_response.output_tokens.unwrap_or(0),
        });

        // Build the set of valid candidate IDs so we can filter out
        // hallucinated self-evaluations before normalization.
        let valid_ids: std::collections::HashSet<&str> =
            context.candidates.iter().map(|c| c.id.as_str()).collect();

        // Filter to valid candidates and warn about dropped evaluations.
        let valid_evals: Vec<_> = structured_response
            .evaluations
            .into_iter()
            .filter(|e| {
                if valid_ids.contains(e.agent_id.as_str()) {
                    true
                } else {
                    warn!(
                        agent_name = %self.config.name,
                        target = %e.agent_id,
                        "Dropping evaluation for unknown candidate (hallucinated self-eval?)"
                    );
                    false
                }
            })
            .collect();

        // Normalize only over valid evaluations.
        let total_weight: f32 = valid_evals.iter().map(|e| e.endorsement_weight.abs()).sum();
        debug!(
            agent_name = %self.config.name,
            valid_count = valid_evals.len(),
            total_abs_weight = total_weight,
            weights = ?valid_evals.iter().map(|e| (&e.agent_id, e.endorsement_weight)).collect::<Vec<_>>(),
            "Normalization input"
        );
        // Diagnostic: warn when all weights share the same sign (no mixed endorsement/opposition)
        let all_negative = valid_evals.iter().all(|e| e.endorsement_weight <= 0.0);
        let all_positive = valid_evals.iter().all(|e| e.endorsement_weight >= 0.0);
        if valid_evals.len() > 1 && (all_negative || all_positive) {
            debug!(
                agent_name = %self.config.name,
                sign = if all_negative { "all_negative" } else { "all_positive" },
                weights = ?valid_evals.iter().map(|e| (&e.agent_id, e.endorsement_weight)).collect::<Vec<_>>(),
                "All endorsement weights have the same sign — legitimate in signed pipeline"
            );
        }
        let mut results = Vec::new();
        for item in valid_evals {
            let justification = item.justification.unwrap_or_default();
            if justification.is_empty() {
                warn!(
                    agent_name = %self.config.name,
                    target = %item.agent_id,
                    "Evaluation missing justification — LLM omitted required field."
                );
            }
            let raw_score = normalize_score(item.endorsement_weight, total_weight);
            results.push((
                item.agent_id,
                Evaluation {
                    score: raw_score,
                    justification,
                    token_usage: batch_token_usage.clone(),
                    stance: item.stance,
                    claim_assessments: item.claim_assessments,
                    disagreements: item.disagreements,
                    category_scores: item.category_scores,
                    is_final_solution: item.is_final_solution,
                    // Batch-level: one LLM call produced all items,
                    // so every Evaluation in this batch carries the
                    // same finish_reason (e.g. "max_iterations" when
                    // the react ceiling was hit before a terminal
                    // submit_batch_evaluation call).
                    finish_reason: agent_response
                        .finish_reason
                        .as_deref()
                        .map(normalize_finish_reason),
                    ..Default::default()
                },
            ));
        }

        Ok(results)
    }

    fn name(&self) -> String {
        self.config.name.clone()
    }
}

/// Implement [`ChatCapable`] so `ProposerEvaluatorAgent` can be used with the
/// SDK worker's status server chat endpoint.
#[async_trait]
impl ChatCapable for ProposerEvaluatorAgent {
    async fn chat(
        &self,
        messages: Vec<async_openai::types::ChatCompletionRequestMessage>,
    ) -> Result<String> {
        // Delegates to the inherent method
        ProposerEvaluatorAgent::chat(self, messages).await
    }
}

// ... helpers (react_loop, generate_structured_output) ...

#[allow(clippy::too_many_arguments)]
async fn generate_structured_output<T>(
    llm_client: &dyn AiModel,
    agent_config: &AgentConfig,
    prompt_set: &dyn PromptSet,
    context: &AgentContext,
    initial_prompt: String,
    tools: &[Box<dyn Tool>],
    terminal_tool_schema: ChatCompletionTool,
    terminal_tool_name: &str,
    output_guard: Option<&dyn crate::agents::OutputLeakDetector>,
) -> Result<(T, AgentResponse)>
where
    // `Serialize` is required so the prompt-exposure guard (when enabled
    // via `AgentConfig.prompt_exposure_guard`) can serialize the parsed
    // terminal content back to JSON and extract user-visible fields for
    // scanning. Concrete types passed through here (`Proposal`,
    // `BatchEvaluation`) already implement `Serialize`.
    T: DeserializeOwned + serde::Serialize,
{
    let max_retries = agent_config.max_retries.filter(|&v| v > 0).unwrap_or(3) as usize;
    let mut attempts = 0;
    let mut current_prompt = Some(initial_prompt);
    let mut current_history: Option<Vec<ChatCompletionRequestMessage>> = None;
    // Accumulate tokens across retry attempts so failed-attempt tokens aren't lost
    let mut cumulative_input_tokens: u32 = 0;
    let mut cumulative_output_tokens: u32 = 0;
    // Owned here so schema retries inherit prior tool-output bloat.
    let mut running_tool_output_bytes: u64 = 0;

    // Mutable clone so we can escalate max_tokens on Length truncation retries
    // or downgrade reasoning_effort on consecutive Stop+empty failures
    let mut retry_config = agent_config.clone();
    let mut consecutive_empty_stops: u32 = 0;
    let loop_start = std::time::Instant::now();

    loop {
        attempts += 1;
        let agent_response = match react_loop(
            llm_client,
            &mut retry_config,
            prompt_set,
            context,
            current_prompt.clone(),
            current_history.clone(),
            tools,
            vec![terminal_tool_schema.clone()],
            Some(terminal_tool_name),
            attempts as u32,
            &mut running_tool_output_bytes,
        )
        .await
        {
            Ok(resp) => resp,
            Err(e) => {
                // Transport-class errors (connection reset, EOF,
                // timeout, mid-body decode) are retryable — the model
                // never saw our request or the response was lost in
                // transit. Classify via `LlmError` downcast so the
                // taxonomy is compile-time exhaustive instead of
                // string-scraped from the formatted error.
                use crate::telemetry::LlmError;
                let is_transport =
                    matches!(e.downcast_ref::<LlmError>(), Some(LlmError::Transport(_)));

                if is_transport && attempts <= max_retries {
                    // Transport errors interrupt the "consecutive empty-Stop"
                    // streak — don't let mixed network+empty failures trip
                    // the reasoning_effort downgrade prematurely.
                    consecutive_empty_stops = 0;
                    let exp = (attempts as u32).min(5); // cap exponent to avoid overflow (max 2^5 = 32s)
                    let backoff = std::time::Duration::from_secs(2u64.pow(exp));
                    warn!(
                        agent_name = %agent_config.name,
                        attempt = attempts,
                        max_retries = max_retries,
                        error = %format!("{e:#}"),
                        retry_after = ?backoff,
                        "Transport error — retrying with backoff."
                    );
                    tokio::time::sleep(backoff).await;
                    continue;
                }
                return Err(e);
            }
        };

        // Accumulate tokens from this attempt (including any failed attempts)
        cumulative_input_tokens += agent_response.input_tokens.unwrap_or(0);
        cumulative_output_tokens += agent_response.output_tokens.unwrap_or(0);

        let mut cleaned_json = clean_json_string(
            &agent_response.content,
            agent_config.unwrap_hallucinated_tool_calls,
            Some(terminal_tool_name),
        );
        let mut parse_result = serde_json::from_str::<T>(&cleaned_json);

        // Attempt repair if parsing failed
        if parse_result.is_err() {
            // 1. Try repairing truncation
            let repaired = repair_truncated_json(&cleaned_json);
            if let Ok(repaired_obj) = serde_json::from_str::<T>(&repaired) {
                warn!(
                    agent_name = %agent_config.name,
                    "Successfully repaired truncated JSON output."
                );
                parse_result = Ok(repaired_obj);
            } else {
                // 2. Try repairing invalid escapes (common with LaTeX in JSON)
                let repaired_escapes = repair_invalid_escapes(&cleaned_json);
                // Also repair truncation on the escaped version just in case
                let repaired_escapes_truncated = repair_truncated_json(&repaired_escapes);

                if let Ok(repaired_obj) = serde_json::from_str::<T>(&repaired_escapes_truncated) {
                    warn!(
                        agent_name = %agent_config.name,
                        "Successfully repaired invalid JSON escapes (likely LaTeX)."
                    );
                    parse_result = Ok(repaired_obj);
                } else {
                    // 3. Try aggressive escape repair (blindly escape backslashes except quotes/backslashes)
                    let aggressive = repair_aggressive_escapes(&cleaned_json);
                    let aggressive_truncated = repair_truncated_json(&aggressive);

                    if let Ok(repaired_obj) = serde_json::from_str::<T>(&aggressive_truncated) {
                        warn!(
                            agent_name = %agent_config.name,
                            "Successfully repaired JSON using aggressive escaping."
                        );
                        parse_result = Ok(repaired_obj);
                    } else {
                        // 4. Nuclear Option: Strip invalid escapes entirely (lossy)
                        let sanitized = sanitize_json_string_lossy(&cleaned_json);
                        let sanitized_truncated = repair_truncated_json(&sanitized);

                        if let Ok(repaired_obj) = serde_json::from_str::<T>(&sanitized_truncated) {
                            warn!(
                                agent_name = %agent_config.name,
                                "Successfully repaired JSON by stripping invalid escapes (lossy)."
                            );
                            parse_result = Ok(repaired_obj);
                        }
                    }
                }
            }
        }

        // 5. Try conversational repair (Rnj-1 style: "THOUGHT: ... RESPONSE: ...")
        if parse_result.is_err()
            && let Some(repaired_conv) = repair_conversational_response(&agent_response.content)
            && let Ok(repaired_obj) = serde_json::from_str::<T>(&repaired_conv)
        {
            warn!(
                "Successfully repaired conversational JSON for agent {}: {}",
                agent_config.name, repaired_conv
            );
            parse_result = Ok(repaired_obj);
        }

        // 5b. Try extracting proposal from Markdown report (e.g. gpt-oss style)
        if parse_result.is_err()
            && terminal_tool_name == "submit_proposal"
            && let Some(repaired_markdown) = extract_proposal_from_markdown(&agent_response.content)
            && let Ok(repaired_obj) = serde_json::from_str::<T>(&repaired_markdown)
        {
            warn!(
                agent_name = %agent_config.name,
                "Successfully extracted proposal from Markdown report."
            );
            parse_result = Ok(repaired_obj);
        }

        // 5c. Try extracting evaluations from Markdown table
        if parse_result.is_err()
            && terminal_tool_name == "submit_batch_evaluation"
            && let Some(repaired_markdown) =
                extract_evaluations_from_markdown(&agent_response.content)
            && let Ok(repaired_obj) = serde_json::from_str::<T>(&repaired_markdown)
        {
            warn!(
                agent_name = %agent_config.name,
                "Successfully extracted evaluations from Markdown table."
            );
            parse_result = Ok(repaired_obj);
        }

        // 5d. For proposals: split merged thought_process/solution_content.
        // gpt-oss and MiniMax frequently put everything into `thought_process`
        // with a `**Solution Content**` marker, leaving `solution_content` absent.
        if parse_result.is_err() && terminal_tool_name == "submit_proposal" {
            if let Ok(mut obj) = serde_json::from_str::<serde_json::Value>(&cleaned_json) {
                let needs_split = obj.get("solution_content").is_none()
                    && obj
                        .get("thought_process")
                        .and_then(|v| v.as_str())
                        .is_some();
                if needs_split {
                    let tp = obj["thought_process"].as_str().unwrap_or_default();
                    // Look for well-known section markers
                    let markers = [
                        "\n**Solution Content**\n",
                        "\n**Solution Content:**\n",
                        "\n**Solution Content**:",
                        "\n## Solution Content\n",
                        "\n### 1. ",
                        "\n## 1. ",
                    ];
                    let mut split_pos = None;
                    for marker in &markers {
                        if let Some(pos) = tp.find(marker) {
                            split_pos = Some((pos, marker.len()));
                            break;
                        }
                    }
                    if let Some((pos, _marker_len)) = split_pos {
                        let thought = tp[..pos].trim().to_string();
                        // For "### 1." style markers, include the marker in solution_content
                        let solution = if markers.iter().take(4).any(|m| tp[pos..].starts_with(m)) {
                            // Strip the "**Solution Content**" header itself
                            tp[pos..]
                                .trim_start_matches(|c: char| {
                                    c.is_whitespace() || c == '*' || c == '#'
                                })
                                .trim_start_matches("Solution Content")
                                .trim_start_matches(|c: char| {
                                    c == ':' || c == '*' || c == '#' || c.is_whitespace()
                                })
                                .trim()
                                .to_string()
                        } else {
                            // Keep the numbered section header
                            tp[pos..].trim().to_string()
                        };
                        obj["thought_process"] = serde_json::Value::String(thought);
                        obj["solution_content"] = serde_json::Value::String(solution);
                        if let Ok(repaired_obj) = serde_json::from_value::<T>(obj) {
                            warn!(
                                agent_name = %agent_config.name,
                                "Successfully split merged thought_process/solution_content."
                            );
                            parse_result = Ok(repaired_obj);
                        }
                    }
                }
            }
        }

        // 5e. Strip thinking-token prefixes from gpt-oss reasoning models.
        // Models like gpt-oss-120b leak internal tokens: "analysisWe need...assistantfinal**Critique..."
        // or just "final**Asset-class allocation...". Strip these and retry as markdown proposal.
        if parse_result.is_err() && terminal_tool_name == "submit_proposal" {
            let content = agent_response.content.trim();
            let stripped = strip_thinking_prefix(content);
            if stripped.len() < content.len() && !stripped.is_empty() {
                // The stripped content is markdown without code blocks — wrap it as a proposal
                let obj = serde_json::json!({
                    "thought_process": "(extracted from thinking-prefixed markdown)",
                    "solution_content": stripped,
                });
                if let Ok(repaired_obj) = serde_json::from_value::<T>(obj) {
                    warn!(
                        agent_name = %agent_config.name,
                        "Successfully stripped thinking-token prefix and extracted proposal."
                    );
                    parse_result = Ok(repaired_obj);
                }
            }
        }

        // 6. Handle explicit refusal (Safety/Policy) as a valid proposal
        if parse_result.is_err() && terminal_tool_name == "submit_proposal" {
            let lower_content = agent_response
                .content
                .trim()
                .to_lowercase()
                .replace(['\u{2018}', '\u{2019}'], "'")
                .replace(['\u{201c}', '\u{201d}'], "\"");

            if lower_content.starts_with("i cannot")
                || lower_content.starts_with("i can't")
                || lower_content.starts_with("i apologize")
                || lower_content.starts_with("i'm sorry")
                || lower_content.starts_with("sorry")
                || lower_content.contains("as an ai")
                || lower_content.contains("cannot fulfill")
            {
                let refusal_json = serde_json::json!({
                    "thought_process": "The model refused to answer the prompt, likely due to safety guidelines.",
                    "solution_content": agent_response.content
                })
                .to_string();

                if let Ok(repaired_obj) = serde_json::from_str::<T>(&refusal_json) {
                    warn!(
                        agent_name = %agent_config.name,
                        "Detected refusal/safety response. Treating as valid proposal."
                    );
                    parse_result = Ok(repaired_obj);
                }
            }
        }

        // Prompt-exposure guardrail — runs on successfully parsed terminal
        // content BEFORE we return it to the deliberation store. A block
        // converts the Ok into an Err carrying the block reason so the
        // existing retry path feeds it back to the LLM as a `SYSTEM
        // ERROR` user message.
        //
        // Gated by `agent_config.prompt_exposure_guard` so existing
        // deployments are unaffected until they opt in.
        if let Ok(ref parsed) = parse_result
            && agent_config.prompt_exposure_guard
            && let Some(detector) = output_guard
            && let Some(block_result) =
                run_prompt_exposure_guard(parsed, terminal_tool_name, &agent_config.name, detector)
                    .await
        {
            warn!(
                agent_name = %agent_config.name,
                attempt = attempts,
                terminal_tool = terminal_tool_name,
                "prompt_exposure guard blocked terminal content; triggering retry."
            );

            emit_for!(
                context,
                PromptExposureDetected {
                    terminal_tool: terminal_tool_name.to_string(),
                    blocked: true,
                    hit_count: block_result.hit_count,
                    response_length_chars: block_result.response_length_chars,
                    suspicion_score: block_result.suspicion_score,
                    xml_tag_hits: block_result.xml_tag_hits,
                    tool_name_hits: block_result.tool_name_hits,
                    instruction_hits: block_result.instruction_hits,
                    wrong_acronym_hits: block_result.wrong_acronym_hits,
                    sample_hits: block_result.sample_hits,
                }
            );

            // Override cleaned_json so the dump/retry rendering names the
            // actual block reason rather than the (valid) terminal JSON
            // that parsed fine.
            cleaned_json = format!(
                "prompt_exposure guard blocked output: {}",
                block_result.reason
            );
            // Synthesize a serde_json::Error so we can slot back into the
            // existing `Err` arm. Deserializing invalid JSON returns the
            // error type we need without paying the parse.
            let synth_err: serde_json::Error =
                serde_json::from_str::<serde_json::Value>("not-json-prompt-exposure-block")
                    .expect_err("invalid JSON always errors");
            parse_result = Err(synth_err);
        }

        match parse_result {
            Ok(response) => {
                // Return cumulative tokens across all attempts (including retries)
                let mut final_response = agent_response;
                final_response.input_tokens = Some(cumulative_input_tokens);
                final_response.output_tokens = Some(cumulative_output_tokens);
                return Ok((response, final_response));
            }
            Err(e) => {
                let finish_reason = agent_response.finish_reason.as_deref().unwrap_or("unknown");

                // Log the FULL raw output to debug (logfile only)
                debug!(
                    agent_name = %agent_config.name,
                    attempt = attempts,
                    error = %e,
                    finish_reason = %finish_reason,
                    raw_content = %cleaned_json,
                    "Failed to parse structured output. Full raw content logged."
                );

                let truncated_raw = if cleaned_json.chars().count() > 1000 {
                    format!(
                        "{}... (truncated)",
                        cleaned_json.chars().take(1000).collect::<String>()
                    )
                } else {
                    cleaned_json.clone()
                };

                if attempts > max_retries {
                    anyhow::bail!(
                        "Failed to parse structured output after {attempts} attempts. Last error: {e}. FinishReason: {finish_reason}. Raw (truncated): '{truncated_raw}'"
                    );
                }

                // Dump failure details to file for debugging (opt-in via NSED_FAILURE_DUMPS=1|full)
                let phase_str = format!("{:?}", context.phase);
                if let Some(dump_file) = write_failure_dump(FailureDumpParams {
                    kind: "parse_error",
                    agent_name: &agent_config.name,
                    model_name: &agent_config.model_name,
                    provider_id: &agent_config.provider_id,
                    error: &e.to_string(),
                    session_id: context.session_id.as_deref(),
                    phase: Some(&phase_str),
                    round: Some(context.round_number),
                    attempt: Some(attempts),
                    finish_reason: Some(finish_reason),
                    input_tokens: agent_response.input_tokens,
                    output_tokens: agent_response.output_tokens,
                    response_content: Some(&cleaned_json),
                    system_prompt: agent_response.system_prompt.as_deref(),
                    request_body: agent_response.request_body.as_deref(),
                    messages: None,
                    failure_dumps_config: agent_config.failure_dumps.as_deref(),
                }) {
                    debug!(dump_file = %dump_file, "Parse failure dump saved");
                }

                warn!(
                    agent_name = %agent_config.name,
                    attempt = attempts,
                    error = %e,
                    finish_reason = %finish_reason,
                    "Agent failed to produce valid structured output. Retrying with feedback."
                );

                // Escalate max_tokens on Length truncation to give the model more
                // room on the next attempt. Cap at 50% of context_window to leave
                // space for input tokens and safety buffer.
                if finish_reason == "Length" {
                    let old = retry_config.max_tokens;
                    let ceiling = if retry_config.context_window > 0 {
                        (retry_config.context_window as f64 * 0.5) as i32
                    } else {
                        32_768
                    };
                    let new_tokens = ((old as f64 * 1.5).ceil() as i32).min(ceiling);
                    if new_tokens > old {
                        retry_config.max_tokens = new_tokens;
                        warn!(
                            agent_name = %retry_config.name,
                            old_max_tokens = old,
                            new_max_tokens = new_tokens,
                            "Escalating max_tokens after Length truncation."
                        );
                    }
                }

                // Downgrade reasoning_effort after consecutive Stop+empty
                // failures. The model is likely spending all output tokens
                // on internal chain-of-thought (reasoning_effort: medium/high)
                // and producing zero visible content. Removing reasoning
                // effort forces the model to output content directly.
                if cleaned_json.trim().is_empty() && finish_reason == "Stop" {
                    consecutive_empty_stops += 1;
                    if consecutive_empty_stops >= 2 && retry_config.reasoning_effort.is_some() {
                        warn!(
                            agent_name = %retry_config.name,
                            consecutive_empty_stops,
                            old_effort = ?retry_config.reasoning_effort,
                            "Downgrading reasoning_effort after consecutive Stop+empty failures"
                        );
                        retry_config.reasoning_effort = None;
                    }
                } else {
                    consecutive_empty_stops = 0;
                }

                let error_msg = if cleaned_json.trim().is_empty() {
                    // Model consumed output tokens but produced no visible content.
                    // Common with reasoning/thinking models where tokens go to
                    // internal chain-of-thought that doesn't appear in the response.
                    "Your response was EMPTY — no content was returned. You MUST call the tool with a JSON argument. Do NOT just think about the answer — you must actually output the tool call with the required fields.".to_string()
                } else if !cleaned_json.trim().starts_with('{') {
                    format!(
                        "Agent failed to use structured output tool. Returned conversational text instead. Raw: '{truncated_raw}'."
                    )
                } else {
                    let hint = if e.is_eof() && finish_reason == "Length" {
                        "Likely truncated due to max_tokens limit. Check input length."
                    } else if e.is_eof() {
                        "Response appears incomplete despite finish_reason=Stop."
                    } else {
                        ""
                    };
                    format!(
                        "Failed to parse structured output JSON. Error: {e} {hint}. Raw: '{truncated_raw}'."
                    )
                };

                let example_json = match terminal_tool_name {
                    "submit_proposal" => {
                        r#"{ "thought_process": "...", "solution_content": "..." }"#
                    }
                    "submit_batch_evaluation" => {
                        r#"{ "evaluations": [ { "agent_id": "Candidate_A", "endorsement_weight": 80.0, "justification": "...", "is_final_solution": false, "stance": null, "claim_assessments": [], "disagreements": [], "category_scores": null } ] }"#
                    }
                    other => {
                        warn!(tool_name = %other, "No example JSON template for terminal tool");
                        "{}"
                    }
                };

                // Update history with the failed attempt and error message.
                // When the previous attempt failed mid-tool-call (terminal-tool
                // parse failure, streaming truncation, etc.), the assistant
                // turn in `agent_response.history` can contain orphan
                // `tool_calls` with no matching `role: "tool"` follow-up.
                // Strict providers (Cerebras) reject with HTTP 422 — sanitize
                // here so feedback-mode retries always send a spec-compliant
                // history. Redundant with the send-path sanitizer in
                // native.rs::prepare_request, but cheap and keeps the retry
                // path's intermediate state correct for logging/inspection.
                let mut repaired_history = agent_response.history.clone();
                llm_repair::pair_orphan_tool_calls(&mut repaired_history);
                current_history = Some(repaired_history);
                current_prompt = None; // Switch to history mode

                // Fix empty Assistant content to prevent 400 Bad Request on retry
                if let Some(ChatCompletionRequestMessage::Assistant(assistant_msg)) =
                    current_history.as_mut().unwrap().last_mut()
                {
                    if let Some(
                        async_openai::types::ChatCompletionRequestAssistantMessageContent::Text(
                            text,
                        ),
                    ) = &assistant_msg.content
                    {
                        if text.trim().is_empty() && assistant_msg.tool_calls.is_none() {
                            assistant_msg.content = Some(async_openai::types::ChatCompletionRequestAssistantMessageContent::Text("(Empty response)".to_string()));
                        }
                    } else if assistant_msg.content.is_none() && assistant_msg.tool_calls.is_none()
                    {
                        assistant_msg.content = Some(
                            async_openai::types::ChatCompletionRequestAssistantMessageContent::Text(
                                "(Empty response)".to_string(),
                            ),
                        );
                    }
                }

                // Append the error message as a User message to the conversation history
                current_history.as_mut().unwrap().push(
                    ChatCompletionRequestUserMessage {
                        content: format!(
                            "SYSTEM ERROR (Attempt {attempts}/{max_retries}): Your last response failed validation. \
                            Issue: {error_msg}. \
                            You MUST use the `{terminal_tool_name}` tool with valid JSON arguments. Example format: \n{example_json}\n\
                            Do not return raw text. Please try again."
                        )
                        .into(),
                        ..Default::default()
                    }
                    .into(),
                );

                if context.telemetry.is_some() {
                    let cumulative_latency_ms = loop_start.elapsed().as_millis() as u64;
                    let reason = if cleaned_json.trim().is_empty() {
                        RetryReason::EmptyContent
                    } else if finish_reason == "Length" {
                        RetryReason::Truncated
                    } else {
                        RetryReason::SchemaError
                    };
                    let cumulative_cost_usd = estimate_llm_cost_usd(
                        &agent_config.model_name,
                        cumulative_input_tokens,
                        cumulative_output_tokens,
                        0,
                        0,
                    );

                    emit_for!(
                        context,
                        RetryLoopAttempt {
                            attempt: attempts as u32,
                            reason,
                            cumulative_latency_ms,
                            cumulative_cost_usd,
                            cumulative_input_tokens,
                            cumulative_output_tokens,
                        }
                    );
                }
            }
        }
    }
}

/// Strip any `<scratchpad>...</scratchpad>` block (and surrounding whitespace)
/// from text, so that retry paths don't nest scratchpads when the original user
/// Scan the parsed terminal-tool response for prompt-exposure leakage.
///
/// Returns `Some(block_reason)` when the guardrail wants the agent loop to
/// retry, `None` otherwise. The caller converts `Some(reason)` into an
/// `Err` so the existing parse-error retry path takes over and feeds the
/// reason back to the LLM as a `SYSTEM ERROR` user message.
///
/// Only scans the *user-visible* fields of known terminal tools:
///
/// - `submit_proposal` → the `solution_content` string (the final answer
///   shown to the end user). We deliberately skip `thought_process` —
///   that's internal reasoning; mentions of tool names there are
///   intentional and don't reach the user.
/// - `submit_batch_evaluation` → each evaluation's `justification` plus
///   every `claim_assessments[].reason`. The `ClaimAssessment` struct
///   already declares serde aliases for the off-schema keys models
///   drift to (`disagreement`, `explanation`, `reasoning`), so by the
///   time the parsed struct is round-tripped here those keys are all
///   normalised to `reason`. Those texts feed the peer-critiques block
///   in subsequent rounds and can reach users via the deliberation-brief
///   rendering.
///
/// For any other terminal tool we scan the serialized JSON as a
/// best-effort fallback — better to over-trigger than silently let
/// a leak slip through an untested tool.
/// Result from the prompt-exposure guard, returned when a block occurs.
/// Contains enough data for the telemetry emission to populate all fields.
#[derive(Debug)]
struct PromptExposureBlockResult {
    reason: String,
    hit_count: u32,
    response_length_chars: u32,
    suspicion_score: f64,
    xml_tag_hits: u32,
    tool_name_hits: u32,
    instruction_hits: u32,
    wrong_acronym_hits: u32,
    sample_hits: Vec<String>,
}

async fn run_prompt_exposure_guard<T>(
    parsed: &T,
    terminal_tool_name: &str,
    agent_name: &str,
    detector: &dyn crate::agents::OutputLeakDetector,
) -> Option<PromptExposureBlockResult>
where
    T: serde::Serialize,
{
    use crate::middleware::{MiddlewareContext, MiddlewareStage, Verdict};

    // Collect user-visible text snippets for this terminal tool. Anything
    // not enumerated falls back to "scan the whole thing."
    let parsed_value = match serde_json::to_value(parsed) {
        Ok(v) => v,
        Err(e) => {
            // Can't scan what we can't serialize. Log once and pass
            // through — this path is only reached for custom `T` the
            // orchestrator added without implementing Serialize, which
            // shouldn't happen in the built-in agent loop.
            tracing::debug!(
                agent_name = %agent_name,
                error = %e,
                "prompt_exposure guard: could not serialize parsed terminal content; skipping."
            );
            return None;
        }
    };

    let mut snippets: Vec<String> = Vec::new();
    match terminal_tool_name {
        "submit_proposal" => {
            if let Some(s) = parsed_value
                .get("solution_content")
                .and_then(|v| v.as_str())
            {
                snippets.push(s.to_string());
            }
        }
        "submit_batch_evaluation" => {
            if let Some(evals) = parsed_value.get("evaluations").and_then(|v| v.as_array()) {
                for ev in evals {
                    if let Some(j) = ev.get("justification").and_then(|v| v.as_str()) {
                        snippets.push(j.to_string());
                    }
                    if let Some(assessments) =
                        ev.get("claim_assessments").and_then(|v| v.as_array())
                    {
                        // The canonical schema (see `ClaimAssessment`)
                        // stores the user-visible rationale under
                        // `reason`. That struct already
                        // declares serde aliases (`disagreement`,
                        // `explanation`, `reasoning`) so deserialization
                        // normalises every off-schema key the models
                        // commonly drift to into `reason` — by the time
                        // we `to_value` the parsed struct here, only
                        // `reason` exists. A `get("reason")` lookup is
                        // therefore sufficient; reaching for the raw
                        // alias names at this layer would be dead code.
                        for a in assessments {
                            if let Some(c) = a.get("reason").and_then(|v| v.as_str()) {
                                snippets.push(c.to_string());
                            }
                        }
                    }
                }
            }
        }
        _ => {
            // Unknown terminal tool — scan the serialized JSON so a new
            // tool added without guardrail coverage still blocks leaks
            // by default. The reason string may be noisier but it
            // fails closed rather than open.
            snippets.push(parsed_value.to_string());
        }
    }

    for snippet in snippets {
        // Run the scan directly to get per-category counts; the result also
        // carries `response_length_chars` and `suspicion_score` derived from
        // the same scanned text, so we don't recompute either here.
        let scan_result = detector.scan(&snippet);
        if scan_result.hit_count() == 0 {
            continue;
        }
        // Build a context to run the detector's verdict gate.
        let ctx = MiddlewareContext {
            content: serde_json::Value::String(snippet),
            action: "propose".to_string(),
            agent_id: agent_name.to_string(),
            job_id: String::new(),
            round: 0,
            stage: MiddlewareStage::ProviderResponse,
            metadata: serde_json::json!(null),
            hook_state: std::collections::HashMap::new(),
        };
        let verdict = detector.evaluate(&ctx).await;
        if matches!(verdict.verdict, Verdict::Block) {
            return Some(PromptExposureBlockResult {
                reason: verdict
                    .reason
                    .unwrap_or_else(|| "prompt_exposure detector blocked output".to_string()),
                hit_count: scan_result.hit_count(),
                response_length_chars: scan_result.response_length_chars,
                suspicion_score: scan_result.suspicion_score,
                xml_tag_hits: scan_result.xml_tag_hits,
                tool_name_hits: scan_result.tool_name_hits,
                instruction_hits: scan_result.instruction_hits,
                wrong_acronym_hits: scan_result.wrong_acronym_hits,
                sample_hits: scan_result.hits.into_iter().take(5).collect(),
            });
        }
    }

    None
}

/// content is re-merged with a fresh scratchpad injection.
fn strip_scratchpad(text: &str) -> String {
    if let Some(start) = text.find("<scratchpad>") {
        if let Some(end_tag) = text[start..].find("</scratchpad>") {
            let end = start + end_tag + "</scratchpad>".len();
            let before = text[..start].trim_end();
            let after = text[end..].trim_start();
            if before.is_empty() {
                after.to_string()
            } else if after.is_empty() {
                before.to_string()
            } else {
                format!("{}\n\n{}", before, after)
            }
        } else {
            text.to_string()
        }
    } else {
        text.to_string()
    }
}

/// Strip `<working_memory>...</working_memory>` from scratchpad content.
/// Working memory is ephemeral per-round thinking that should not persist across rounds.
/// Falls back to returning the original content if tags are malformed.
fn strip_working_memory(text: &str) -> String {
    let mut result = text.to_string();
    // Remove all <working_memory>...</working_memory> blocks (there may be multiple)
    while let Some(start) = result.find("<working_memory>") {
        if let Some(end_offset) = result[start..].find("</working_memory>") {
            let end = start + end_offset + "</working_memory>".len();
            let before = result[..start].trim_end();
            let after = result[end..].trim_start();
            result = if before.is_empty() {
                after.to_string()
            } else if after.is_empty() {
                before.to_string()
            } else {
                format!("{}\n\n{}", before, after)
            };
        } else {
            // Malformed — no closing tag. Return what we have so far.
            break;
        }
    }
    result
}

/// Extract only `<key_findings>` and `<strategy>` sections from scratchpad content.
/// Used during evaluation phase to provide focused context without ephemeral working memory.
/// Falls back to the full scratchpad if no structured sections are found.
fn extract_evaluation_sections(text: &str) -> String {
    let mut sections = Vec::new();

    // Extract <key_findings>...</key_findings>
    if let Some(start) = text.find("<key_findings>") {
        if let Some(end_offset) = text[start..].find("</key_findings>") {
            let end = start + end_offset + "</key_findings>".len();
            sections.push(&text[start..end]);
        }
    }

    // Extract <strategy>...</strategy>
    if let Some(start) = text.find("<strategy>") {
        if let Some(end_offset) = text[start..].find("</strategy>") {
            let end = start + end_offset + "</strategy>".len();
            sections.push(&text[start..end]);
        }
    }

    if sections.is_empty() {
        // No structured sections found — fall back to full scratchpad
        text.to_string()
    } else {
        sections.join("\n\n")
    }
}

/// Rough cost estimate for an LLM call in USD.
/// Uses approximate per-model pricing; falls back to generic rates.
fn estimate_llm_cost_usd(
    model: &str,
    input_tokens: u32,
    output_tokens: u32,
    _reasoning_tokens: u32,
    cached_tokens: u32,
) -> f64 {
    // Pricing per 1M tokens (approximate, as of 2024-2025)
    let (input_per_m, output_per_m, cached_discount) =
        if model.contains("gpt-4") && model.contains("o") {
            // GPT-4o: $2.50/$10 per 1M
            (2.50, 10.0, 0.5)
        } else if model.contains("gpt-4") {
            // GPT-4: $10/$30 per 1M
            (10.0, 30.0, 0.5)
        } else if model.contains("gpt-3.5") {
            // GPT-3.5-turbo: $0.50/$1.50 per 1M
            (0.50, 1.50, 0.5)
        } else if model.contains("claude") {
            // Claude Sonnet: $3/$15 per 1M
            (3.0, 15.0, 0.9)
        } else if model.contains("gemini") {
            // Gemini Flash: $0.075/$0.30 per 1M (very cheap)
            (0.075, 0.30, 0.75)
        } else {
            // Generic fallback: $1/$3 per 1M
            (1.0, 3.0, 0.5)
        };

    let non_cached_input = (input_tokens as u64).saturating_sub(cached_tokens as u64);
    let cached_cost = (cached_tokens as f64 / 1_000_000.0) * input_per_m * cached_discount;
    let non_cached_cost = (non_cached_input as f64 / 1_000_000.0) * input_per_m;
    let output_cost = (output_tokens as f64 / 1_000_000.0) * output_per_m;

    cached_cost + non_cached_cost + output_cost
}

fn empty_terminal_tool_content(terminal_tool_name: Option<&str>) -> String {
    match terminal_tool_name {
        Some("submit_proposal") => serde_json::json!({
            "thought_process": "Agent reached maximum iterations without producing a final answer.",
            "solution_content": ""
        })
        .to_string(),
        Some("submit_batch_evaluation") => serde_json::json!({
            "evaluations": []
        })
        .to_string(),
        _ => "{}".to_string(),
    }
}

#[allow(clippy::too_many_arguments)]
async fn react_loop(
    llm_client: &dyn AiModel,
    agent_config: &mut AgentConfig,
    prompt_set: &dyn PromptSet,
    context: &AgentContext,
    initial_prompt: Option<String>,
    input_history: Option<Vec<ChatCompletionRequestMessage>>,
    tools: &[Box<dyn Tool>],
    extra_tool_schemas: Vec<ChatCompletionTool>,
    terminal_tool_name: Option<&str>,
    // Outer structured-output retry attempt (1-indexed). Threaded so
    // `LlmRequestStart.attempt` carries the real value rather than `1`.
    outer_attempt: u32,
    running_tool_output_bytes: &mut u64,
) -> Result<AgentResponse> {
    // Telemetry emitter comes from the context (populated by the
    // worker after deserialize). Reading it here keeps the function
    // signature short and avoids the parameter-and-context double
    // threading the prior shape forced.
    let telemetry = context.telemetry.as_ref();
    let mut tool_usage_stats: HashMap<String, usize> = HashMap::new();
    #[allow(unused_assignments)]
    let mut last_system_message: Option<String> = None;
    #[allow(unused_assignments)]
    let mut last_request_body: Option<String> = None;
    let mut total_input_tokens = 0;
    let mut total_output_tokens = 0;
    let mut scratchpad_content = if let Some(store) = &context.store {
        if let Ok(Some(content)) = store.get(&agent_config.name).await {
            info!(agent=%agent_config.name, "Loaded persistent scratchpad from Sovereign Store.");
            // Phase 6C: Evaluators only see <key_findings> + <strategy> sections
            // to keep evaluation focused and reduce context bloat.
            if context.phase == DeliberationPhase::Evaluating {
                extract_evaluation_sections(&content)
            } else {
                content
            }
        } else {
            String::new()
        }
    } else {
        String::new()
    };
    // Matches default_max_react_iterations() in the agent SDK (20).
    // Kept in lockstep so an agent that doesn't configure the field
    // explicitly gets the same ceiling in both the sdk-side config
    // resolver and this worker-side fallback.
    let max_iterations = agent_config
        .max_react_iterations
        .filter(|&v| v > 0)
        .unwrap_or(20) as usize;
    let max_scratchpad_size = agent_config
        .max_scratchpad_size
        .filter(|&v| v > 0)
        .unwrap_or(32_768) as usize;

    // Define the update_scratchpad tool schema
    let scratchpad_tool_schema = ChatCompletionTool {
        r#type: ChatCompletionToolType::Function,
        function: FunctionObject {
            name: "update_scratchpad".to_string(),
            description: Some("Update your persistent scratchpad memory.".to_string()),
            parameters: Some(json!({
                "type": "object",
                "properties": {
                    "content": { "type": "string", "description": "Text to store." },
                    "mode": { "type": "string", "enum": ["append", "overwrite"] }
                },
                "required": ["content", "mode"],
                "additionalProperties": false
            })),
            strict: Some(true),
        },
    };
    let compact_history_tool_schema = ChatCompletionTool {
        r#type: ChatCompletionToolType::Function,
        function: FunctionObject {
            name: "compact_history".to_string(),
            description: Some(
                "Fold older tool-call results in your conversation history into a \
                 single LLM-summarized synopsis (appended to your scratchpad), \
                 keeping the most recent N tool calls verbatim. Call this when \
                 your context utilization climbs into the 75-90% range or when \
                 your scratchpad is nearly full — frees context budget for \
                 further reasoning."
                    .to_string(),
            ),
            parameters: Some(json!({
                "type": "object",
                "properties": {
                    "keep_last_n_calls": {
                        "type": "integer",
                        "minimum": 1,
                        "maximum": 10,
                        "description": "How many of the most recent tool-call results to keep verbatim. Default 2."
                    }
                },
                "required": ["keep_last_n_calls"],
                "additionalProperties": false
            })),
            strict: Some(true),
        },
    };

    let mut messages: Vec<ChatCompletionRequestMessage> = if let Some(h) = input_history {
        h
    } else {
        let p = initial_prompt.clone().unwrap_or_default();
        if agent_config.merge_system_prompt {
            vec![
                ChatCompletionRequestUserMessage {
                    content: p.into(),
                    ..Default::default()
                }
                .into(),
            ]
        } else {
            vec![
                ChatCompletionRequestSystemMessage {
                    content: "".into(), // Placeholder
                    ..Default::default()
                }
                .into(),
                ChatCompletionRequestUserMessage {
                    content: p.into(),
                    ..Default::default()
                }
                .into(),
            ]
        }
    };

    // Capture the original user prompt content to prevent loss or duplication when injecting scratchpad.
    // Strip any pre-existing <scratchpad>...</scratchpad> block so retries (input_history) don't
    // nest scratchpads when the content is re-merged with a fresh scratchpad_text below.
    //
    // When merge_system_prompt is true and this is a retry (input_history), the first User message
    // already contains "{system_prompt}\n\n<scratchpad>...</scratchpad>\n\n{user_query}".
    // We must extract only the user_query part (after </scratchpad>) to avoid duplicating the
    // system prompt on the next merge.
    let original_user_content = {
        let raw = messages
            .iter()
            .find_map(|m| {
                if let ChatCompletionRequestMessage::User(u) = m
                    && let async_openai::types::ChatCompletionRequestUserMessageContent::Text(t) =
                        &u.content
                {
                    return Some(t.clone());
                }
                None
            })
            .unwrap_or_default();
        if agent_config.merge_system_prompt {
            // When merge_system_prompt is active, the scratchpad sits between the system
            // prompt and the user content. Extract only what follows </scratchpad>.
            if let Some(end_idx) = raw.find("</scratchpad>") {
                raw[end_idx + "</scratchpad>".len()..]
                    .trim_start()
                    .to_string()
            } else {
                // No scratchpad found — fresh start or first iteration. strip_scratchpad is
                // a no-op anyway, but keeps the fallback safe.
                strip_scratchpad(&raw)
            }
        } else {
            strip_scratchpad(&raw)
        }
    };

    let mut tool_schemas: Vec<ChatCompletionTool> = tools.iter().map(|t| t.schema()).collect();
    tool_schemas.extend(extra_tool_schemas);
    tool_schemas.push(scratchpad_tool_schema);
    tool_schemas.push(compact_history_tool_schema);

    let tool_map: HashMap<String, &Box<dyn Tool>> = tools.iter().map(|t| (t.name(), t)).collect();

    // Time-aware tool stripping: track iteration durations to detect when the
    // agent is running out of phase budget and should finalize immediately.
    let loop_start = std::time::Instant::now();
    let mut iteration_durations: Vec<std::time::Duration> = Vec::new();

    for iteration_index in 0..max_iterations {
        let iter_start = std::time::Instant::now();

        // Regenerate system message (without scratchpad)
        let mut system_message = agent_config
            .system_prompt_override
            .clone()
            .unwrap_or_else(|| {
                prompt_set.get_system_message(
                    &agent_config.name,
                    context.round_number as usize,
                    context.total_rounds as usize,
                    context.phase, // Fix E0382: Clone phase because get_system_message takes value
                )
            });

        // Construct scratchpad block to be injected into User message
        let scratchpad_text = format!(
            "\n\n<scratchpad>\n(This persists across tool calls. Use the `update_scratchpad` tool to store notes.)\n{}\n</scratchpad>",
            if scratchpad_content.is_empty() {
                "(Empty)"
            } else {
                &scratchpad_content
            }
        );

        // ── Time-aware tool stripping ──────────────────────────────────
        // When remaining phase budget ≤ 3× average iteration time, strip
        // all non-terminal tools so the LLM is forced to finalize.
        let avg_iteration_secs = if iteration_durations.is_empty() {
            0.0
        } else {
            let sum: f64 = iteration_durations.iter().map(|d| d.as_secs_f64()).sum();
            sum / iteration_durations.len() as f64
        };
        let elapsed = loop_start.elapsed().as_secs_f64();
        let remaining_budget = context.phase_budget_remaining_secs - elapsed;
        let remaining_iterations = max_iterations.saturating_sub(iteration_index);
        let force_finalize_time = context.phase_budget_remaining_secs > 0.0
            && ((remaining_budget <= 0.0)
                || (avg_iteration_secs > 0.0 && remaining_budget <= avg_iteration_secs * 3.0));
        let force_finalize_window =
            resolve_finalize_window(std::env::var("NSED_FINALIZE_WINDOW").ok().as_deref());
        let force_finalize_iters =
            max_iterations > force_finalize_window && remaining_iterations <= force_finalize_window;
        let force_finalize = force_finalize_time || force_finalize_iters;

        let active_tool_schemas = if force_finalize {
            warn!(
                agent = %agent_config.name,
                trigger = if force_finalize_iters { "iter_budget_low" } else { "time_budget_low" },
                avg_iter_secs = format!("{:.2}", avg_iteration_secs),
                remaining_budget_secs = format!("{:.2}", remaining_budget),
                remaining_iterations = remaining_iterations,
                "⏱️ Budget low — stripping non-terminal tools to force finalization."
            );
            // Keep only the terminal tool
            if let Some(name) = terminal_tool_name {
                tool_schemas
                    .iter()
                    .filter(|t| t.function.name == name)
                    .cloned()
                    .collect::<Vec<_>>()
            } else {
                vec![]
            }
        } else {
            tool_schemas.clone()
        };

        // Build active tool name set for gating execution (prevents stripped
        // tools from being called via text-extraction path when force_finalize).
        let active_tool_names: std::collections::HashSet<&str> = active_tool_schemas
            .iter()
            .map(|s| s.function.name.as_str())
            .collect();

        // If native tools are disabled, we must inject tool definitions manually into the system prompt
        // so the model knows they exist and how to call them.
        if agent_config.disable_native_tools {
            if agent_config.tool_format.as_deref() == Some("nous") {
                // Inject Nous XML format (Qwen style)
                let mut tool_descs = String::new();
                for schema in &active_tool_schemas {
                    let func_schema = serde_json::json!({
                        "name": schema.function.name,
                        "description": schema.function.description,
                        "parameters": schema.function.parameters
                    });
                    tool_descs.push_str(&serde_json::to_string(&func_schema).unwrap_or_default());
                    tool_descs.push('\n');
                }

                let tool_text = format!(
                    "\n\n# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>\n{}\n</tools>\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{{\"name\": <function-name>, \"arguments\": <args-json-object>}}\n</tool_call>\n",
                    tool_descs
                );
                system_message.push_str(&tool_text);
            } else {
                // Default Text format (Python style)
                let mut tool_text = String::from(
                    "\n\n<tools>\nYou have access to the following tools. To use them, output a function call in square brackets like:\n[tool_name(arg=\"value\")]\n\n",
                );
                for schema in &active_tool_schemas {
                    tool_text.push_str(&format!(
                        "- {}: {}\n",
                        schema.function.name,
                        schema.function.description.clone().unwrap_or_default()
                    ));
                    if let Some(params) = &schema.function.parameters {
                        let args = serde_json::to_string_pretty(params).unwrap_or_default();
                        tool_text.push_str(&format!("  Arguments: {args}\n"));
                    }
                }
                tool_text.push_str("</tools>\n");
                system_message.push_str(&tool_text);
            }
        }
        // Inject user tool awareness only when both tools are defined AND a handler is available
        if !context.user_tools.is_empty() && context.user_tool_handler.is_some() {
            system_message.push_str(
                "\n<external_tools>\n\
                 Tools prefixed 'user_' are serviced outside this system. Responses may be delayed.\n\
                 You MUST supply all required parameters defined in the tool schema. \
                 For messaging tools, always include your question or message in the 'message' parameter — never call with empty arguments.\n\
                 </external_tools>\n",
            );
        }

        last_system_message = Some(system_message.clone());

        // Update the message with system prompt content and scratchpad
        if agent_config.merge_system_prompt {
            if let Some(ChatCompletionRequestMessage::User(user_msg)) = messages.first_mut() {
                let merged =
                    format!("{system_message}\n\n{scratchpad_text}\n\n{original_user_content}");
                user_msg.content =
                    async_openai::types::ChatCompletionRequestUserMessageContent::Text(merged);
            }
        } else {
            if let Some(ChatCompletionRequestMessage::System(sys_msg)) = messages.first_mut() {
                sys_msg.content = system_message.into();
            }
            // Inject scratchpad into the first User message (which contains the prompt)
            for msg in messages.iter_mut() {
                if let ChatCompletionRequestMessage::User(user_msg) = msg {
                    let merged = format!("{scratchpad_text}\n\n{original_user_content}");
                    user_msg.content =
                        async_openai::types::ChatCompletionRequestUserMessageContent::Text(merged);
                    break;
                }
            }
        }

        // Runs AFTER the system + scratchpad merge so it measures the
        // real prompt size. Includes `tools` because large built-in or
        // user tool schemas can be the difference between "under 90%"
        // and "shrink-guard floor" — the original messages-only size
        // check could miss the exact failure mode this path is meant
        // to catch.
        let request_tools_for_sizing: Option<&Vec<ChatCompletionTool>> =
            if active_tool_schemas.is_empty() || agent_config.disable_native_tools {
                None
            } else {
                Some(&active_tool_schemas)
            };
        let request_chars = serde_json::to_string(&json!({
            "messages": &messages,
            "tools": request_tools_for_sizing,
        }))
        .map(|s| s.len())
        .unwrap_or(0);
        let messages_chars = request_chars;
        let chars_per_token = agent_config
            .chars_per_token
            .map(|v| v as f32)
            .unwrap_or(4.0);
        if should_auto_compact(
            messages_chars,
            agent_config.context_window,
            messages.len(),
            chars_per_token,
        ) {
            info!(
                agent = %agent_config.name,
                messages_chars,
                context_window = agent_config.context_window,
                "auto-invoking compact_history"
            );
            match compact_message_history(
                llm_client,
                agent_config,
                &messages,
                agent_config.compact_history_default_keep,
            )
            .await
            {
                Ok(result) if result.compacted_count > 0 => {
                    // A verbose summary can net-grow the request; this
                    // path exists only to dodge the shrink-guard, so
                    // gate the swap on a serialized-size shrink check.
                    // Use the same `{messages, tools}` envelope as
                    // `messages_chars` so the comparison is like-for-like.
                    let new_chars = serde_json::to_string(&json!({
                        "messages": &result.new_messages,
                        "tools": request_tools_for_sizing,
                    }))
                    .map(|s| s.len())
                    .unwrap_or(usize::MAX);
                    if new_chars >= messages_chars {
                        warn!(
                            agent = %agent_config.name,
                            old = messages_chars,
                            new = new_chars,
                            "auto-compact did not shrink request; keeping original history"
                        );
                    } else {
                        messages = result.new_messages;
                        // Stage the appended summary so a too-large
                        // candidate the squeezer can't shrink never
                        // gets persisted and re-bloats next prompt.
                        let mut candidate = scratchpad_content.clone();
                        if !candidate.is_empty() {
                            candidate.push('\n');
                        }
                        candidate.push_str("[compacted_history (auto)]\n");
                        candidate.push_str(&result.summary);
                        let final_scratchpad = match squeeze_scratchpad_if_full(
                            llm_client,
                            agent_config,
                            &candidate,
                            max_scratchpad_size,
                        )
                        .await
                        {
                            Ok(Some(squeezed)) => Some(squeezed),
                            Ok(None) if candidate.len() <= max_scratchpad_size => Some(candidate),
                            Ok(None) => {
                                warn!(
                                    agent = %agent_config.name,
                                    candidate_len = candidate.len(),
                                    max_scratchpad_size,
                                    "auto-squeeze produced no shrink and candidate \
                                     still over cap; keeping original scratchpad"
                                );
                                None
                            }
                            Err(e) => {
                                warn!(error=%e, "auto-squeeze failed; keeping scratchpad");
                                None
                            }
                        };
                        if let Some(committed) = final_scratchpad {
                            scratchpad_content = committed;
                            // In Evaluating phase the in-memory scratchpad
                            // is a subset of the canonical store; writing
                            // back would wipe non-evaluation sections.
                            if context.phase != DeliberationPhase::Evaluating
                                && let Some(store) = &context.store
                                && let Err(e) =
                                    store.set(&agent_config.name, &scratchpad_content).await
                            {
                                warn!(error=%e, "Failed to persist auto-compacted scratchpad");
                            }
                        }
                    }
                }
                Ok(_) => {}
                Err(e) => warn!(error=%e, "auto-compact failed; SDK shrink-guard takes over"),
            }
        }

        // Create our "partial" request config, which the AiModel implementation will complete.
        let request_config = RequestConfig {
            messages: messages.clone(),
            tools: if active_tool_schemas.is_empty() || agent_config.disable_native_tools {
                None
            } else {
                Some(active_tool_schemas.clone())
            },
            tool_choice: None,
            presence_penalty: if context.phase == DeliberationPhase::Evaluating {
                agent_config.presence_penalty.map(|p| p / 2.0)
            } else {
                agent_config.presence_penalty
            },
        };

        // Estimate input tokens for telemetry
        let messages_json = serde_json::to_string(&request_config.messages).unwrap_or_default();
        let estimated_input_tokens = (messages_json.len() as f32 / 3.0) as u32;

        let request_id = uuid::Uuid::new_v4().to_string();
        let ctx = context.telemetry_for();

        // `estimated_input_tokens` is a pre-shrink chars/3 heuristic
        // and can compute > 100%; clamp so dashboards stay honest.
        let context_utilization_pct = if agent_config.context_window > 0 {
            ((estimated_input_tokens as f64 / agent_config.context_window as f64) * 100.0)
                .clamp(0.0, 100.0)
        } else {
            0.0
        };
        if context_utilization_pct >= 90.0 {
            warn!(
                agent = %agent_config.name,
                pct = context_utilization_pct,
                estimated_input_tokens,
                context_window = agent_config.context_window,
                "context utilization ≥90% — shrink-guard imminent"
            );
        } else if context_utilization_pct >= 75.0 {
            warn!(
                agent = %agent_config.name,
                pct = context_utilization_pct,
                estimated_input_tokens,
                context_window = agent_config.context_window,
                "context utilization ≥75%"
            );
        } else if context_utilization_pct >= 50.0 {
            warn!(
                agent = %agent_config.name,
                pct = context_utilization_pct,
                estimated_input_tokens,
                context_window = agent_config.context_window,
                "context utilization ≥50%"
            );
        }
        let mut span = LlmRequestSpan::start(
            telemetry,
            &ctx,
            &request_id,
            outer_attempt,
            &agent_config.model_name,
            &agent_config.provider_id,
            estimated_input_tokens,
            context_utilization_pct,
            *running_tool_output_bytes,
        );

        let result = match llm_client
            .chat_completion(agent_config, request_config)
            .await
        {
            Ok(res) => res,
            Err(e) => {
                span.fail(&e).await;

                // Dump failure details to file for debugging API errors (opt-in via NSED_FAILURE_DUMPS=1|full)
                let phase_str = format!("{:?}", context.phase);
                let dump_file = write_failure_dump(FailureDumpParams {
                    kind: "api_error",
                    agent_name: &agent_config.name,
                    model_name: &agent_config.model_name,
                    provider_id: &agent_config.provider_id,
                    error: &format!("{e:#}"),
                    session_id: context.session_id.as_deref(),
                    phase: Some(&phase_str),
                    round: Some(context.round_number),
                    attempt: None,
                    finish_reason: None,
                    input_tokens: None,
                    output_tokens: None,
                    response_content: None,
                    system_prompt: last_system_message.as_deref(),
                    request_body: None,
                    messages: Some(&messages),
                    failure_dumps_config: agent_config.failure_dumps.as_deref(),
                });
                if let Some(ref path) = dump_file {
                    warn!(
                        agent_name = %agent_config.name,
                        error = %e,
                        dump_file = %path,
                        "API request failed. Dump saved to file."
                    );
                } else {
                    warn!(
                        agent_name = %agent_config.name,
                        error = %e,
                        "API request failed. Set NSED_FAILURE_DUMPS=1 to save debug dumps."
                    );
                }
                // Preserve the typed `LlmError` through the anyhow
                // wrap (`From<LlmError> for anyhow::Error` keeps the
                // concrete type via `Error::source` / downcast). The
                // retry classifier in `generate_structured_output`
                // pattern-matches via `downcast_ref::<LlmError>()`
                // rather than scraping the formatted string.
                return Err(e.into());
            }
        };

        // Complete LLM request span with telemetry
        let cost_usd = {
            let usage = result.response.usage.clone();
            let (input_tokens, output_tokens, reasoning_tokens, cached_tokens) =
                if let Some(u) = &usage {
                    let cached = u
                        .prompt_tokens_details
                        .as_ref()
                        .and_then(|d| d.cached_tokens)
                        .unwrap_or(0);
                    let reasoning = u
                        .completion_tokens_details
                        .as_ref()
                        .and_then(|d| d.reasoning_tokens)
                        .unwrap_or(0);
                    (u.prompt_tokens, u.completion_tokens, reasoning, cached)
                } else {
                    let content_len = result
                        .response
                        .choices
                        .first()
                        .and_then(|c| c.message.content.as_deref())
                        .unwrap_or("")
                        .len();
                    let cpt = agent_config.chars_per_token.unwrap_or(4.0).max(0.1);
                    let out_est = (content_len as f64 / cpt).ceil() as u32;
                    (estimated_input_tokens, out_est, 0, 0)
                };
            estimate_llm_cost_usd(
                &agent_config.model_name,
                input_tokens,
                output_tokens,
                reasoning_tokens,
                cached_tokens,
            )
        };
        // `messages_chars` is the byte-len of the same JSON we already
        // serialized for `estimated_input_tokens`. `max_tokens_requested`
        // is the agent's configured cap (strategy may override
        // internally for vLLM context-shrink, but the requested value
        // is what tools the operator's diagnostic view).
        let messages_chars = messages_json.len() as u32;
        let max_tokens_requested = if agent_config.max_tokens > 0 {
            Some(agent_config.max_tokens as u32)
        } else {
            None
        };
        span.complete(&result, cost_usd, messages_chars, max_tokens_requested)
            .await;

        let response = result.response;
        let request_body = result.raw_request;
        last_request_body = Some(request_body);
        let choice = response
            .choices
            .first()
            .context("No choice in LLM response")?;
        let finish_reason = choice.finish_reason.map(|r| format!("{r:?}"));
        if let Some(usage) = &response.usage {
            total_input_tokens += usage.prompt_tokens;
            total_output_tokens += usage.completion_tokens;
        } else {
            // Fallback: estimate tokens using chars_per_token when provider doesn't return usage
            let cpt = agent_config.chars_per_token.unwrap_or(4.0).max(0.1);
            let input_len = serde_json::to_string(&messages).unwrap_or_default().len();
            let output_len = choice.message.content.as_deref().unwrap_or("").len();
            total_input_tokens += (input_len as f64 / cpt).ceil() as u32;
            total_output_tokens += (output_len as f64 / cpt).ceil() as u32;
        }

        // Clone the message so we can modify (repair) it before pushing to history.
        let mut response_message = choice.message.clone();

        // REPAIR TRUNCATED JSON IN TOOL ARGUMENTS
        // This prevents "400 Bad Request" errors in the NEXT round if we send back
        // a history containing broken JSON strings in tool_calls.
        if agent_config.repair_invalid_escapes {
            repair_tool_calls(&mut response_message, &agent_config.name);
        }

        // Keep the original content for heuristic tool scanning (some models put tool calls inside thinking blocks)
        let full_content = response_message.content.clone().unwrap_or_default();
        let mut content = full_content.clone();

        // Check for native thinking blocks
        if agent_config.supports_native_thinking {
            while let Some(start) = content.find("<think>") {
                if let Some(end_offset) = content[start..].find("</think>") {
                    let end = start + end_offset;
                    let thought = &content[start + 7..end];
                    info!(
                        target: "nsed_activity",
                        event = "native_thinking",
                        agent = %agent_config.name,
                        content = %thought
                    );

                    // Strip the thinking block to save context space in history
                    content.replace_range(start..end + 8, "");
                } else {
                    break;
                }
            }
        }

        messages.push(
            #[allow(deprecated)]
            ChatCompletionRequestAssistantMessage {
                content: Some(
                    async_openai::types::ChatCompletionRequestAssistantMessageContent::Text(
                        content.clone(),
                    ),
                ),
                tool_calls: if agent_config.disable_native_tools {
                    None
                } else {
                    response_message.tool_calls.clone()
                },
                function_call: None,
                refusal: None,
                name: None,
                audio: None,
            }
            .into(),
        );

        // Workaround: Some providers (vLLM) return empty tool_calls vec instead of None.
        // We treat empty tool_calls as no tool calls (final answer).
        let mut tool_calls_list = response_message.tool_calls.clone();
        let mut has_tool_calls = tool_calls_list
            .as_ref()
            .map(|t| !t.is_empty())
            .unwrap_or(false);

        // If no native tool calls, try to extract tool calls from content
        if !has_tool_calls {
            let extracted = if agent_config.tool_format.as_deref() == Some("nous") {
                extract_xml_tool_calls(&full_content)
            } else {
                extract_python_tool_calls(&full_content)
            };

            if !extracted.is_empty() {
                // Patch the last message in history to include these tool calls so vLLM accepts the subsequent Tool messages
                // BUT only if native tools are enabled. If disabled, we keep history as text-only.
                if !agent_config.disable_native_tools
                    && let Some(ChatCompletionRequestMessage::Assistant(last_msg)) =
                        messages.last_mut()
                {
                    last_msg.tool_calls = Some(extracted.clone());
                }

                tool_calls_list = Some(extracted);
                has_tool_calls = true;
                info!("Extracted tool calls from text content.");
            } else {
                // Fallback: Check for implicit JSON tool calls (for models that ignore prompt instructions)
                let json_calls = llm_repair::extraction::heuristic_json_tool_calls(&full_content);
                if !json_calls.is_empty() {
                    tool_calls_list = Some(json_calls);
                    has_tool_calls = true;
                    info!("Extracted implicit JSON tool calls from content.");
                }
            }
        }

        if has_tool_calls {
            let tool_calls = tool_calls_list.as_ref().unwrap();
            let mut tool_outputs_text = Vec::new();

            for tool_call in tool_calls {
                let tool_name = &tool_call.function.name;
                info!(tool_name = %tool_name, "LLM requested a tool call.");
                debug!(tool_arguments = %tool_call.function.arguments, "Tool call arguments.");

                info!(
                    target: "nsed_activity",
                    event = "tool_call",
                    agent = %agent_config.name,
                    tool = %tool_name,
                    arguments = %tool_call.function.arguments
                );

                // Check for terminal tool
                if terminal_tool_name == Some(tool_name) {
                    // Guard: if the response was truncated (finish_reason: Length),
                    // the terminal tool's content is likely clipped. Reject and retry
                    // with escalated max_tokens instead of returning truncated output.
                    let was_truncated = finish_reason.as_deref() == Some("Length");
                    if was_truncated {
                        warn!(
                            agent_name = %agent_config.name,
                            tool = %tool_name,
                            "Terminal tool call truncated by max_tokens. Rejecting and retrying with more tokens."
                        );
                        // Escalate max_tokens for next iteration
                        let old = agent_config.max_tokens;
                        let ceiling = if agent_config.context_window > 0 {
                            (agent_config.context_window as f64 * 0.5) as i32
                        } else {
                            32_768
                        };
                        let new_tokens = ((old as f64 * 1.5).ceil() as i32).min(ceiling);
                        if new_tokens > old {
                            agent_config.max_tokens = new_tokens;
                            warn!(
                                agent_name = %agent_config.name,
                                old_max_tokens = old,
                                new_max_tokens = new_tokens,
                                "Escalating max_tokens in react loop after terminal tool truncation."
                            );
                        }

                        // Feed back truncation error so the model retries the submission
                        let error_text = format!(
                            "ERROR: Your `{tool_name}` call was truncated (output cut off mid-response). \
                            Your submission was NOT accepted. You now have more output space. \
                            Please call `{tool_name}` again with your COMPLETE response. Do NOT shorten or summarize it."
                        );
                        if agent_config.disable_native_tools {
                            // Non-native: use a User message
                            messages.push(
                                ChatCompletionRequestMessage::User(ChatCompletionRequestUserMessage {
                                    content:
                                        async_openai::types::ChatCompletionRequestUserMessageContent::Text(
                                            error_text,
                                        ),
                                    name: None,
                                }),
                            );
                        } else {
                            // Native tools: respond with a Tool message to satisfy API protocol
                            messages.push(
                                ChatCompletionRequestToolMessage {
                                    tool_call_id: tool_call.id.clone(),
                                    content: ChatCompletionRequestToolMessageContent::Text(
                                        error_text,
                                    ),
                                }
                                .into(),
                            );
                        }
                        break; // break out of tool_calls loop, continue react loop
                    }

                    info!(
                        "LLM called terminal tool '{}'. Returning arguments.",
                        tool_name
                    );
                    return Ok(AgentResponse {
                        content: tool_call.function.arguments.clone(),
                        tool_usage: tool_usage_stats,
                        finish_reason: finish_reason.or(response_message.refusal.clone()),
                        input_tokens: Some(total_input_tokens),
                        output_tokens: Some(total_output_tokens),
                        system_prompt: last_system_message.clone(),
                        request_body: last_request_body.clone(),
                        history: messages.clone(),
                        final_scratchpad: if scratchpad_content.is_empty() {
                            None
                        } else {
                            Some(scratchpad_content.clone())
                        },
                    });
                }

                // Increment tool usage stats
                *tool_usage_stats.entry(tool_name.clone()).or_insert(0) += 1;

                let tool_exec_start = std::time::Instant::now();
                // `tool_success` is set explicitly in every branch
                // alongside `tool_output` so the telemetry emission
                // below records the real outcome rather than inferring
                // it from the output string's prefix (which would
                // miss errors that happen to format without "Error:"
                // and would misclassify legitimate outputs that
                // happen to start with one). The initial value is
                // overwritten in every code path; the explicit
                // `false` is a
                // compile-time guarantee against an accidental fall-
                // through emitting `success: true`.
                #[allow(unused_assignments)]
                let mut tool_success: bool = false;
                let mut tool_output = if tool_name == "update_scratchpad"
                    && active_tool_names.contains("update_scratchpad")
                {
                    match serde_json::from_str::<Value>(&tool_call.function.arguments) {
                        Ok(args) => {
                            let content = args["content"].as_str().unwrap_or("").to_string();
                            let mode = args["mode"].as_str().unwrap_or("append");

                            let projected_len =
                                if mode == "overwrite" || scratchpad_content.is_empty() {
                                    content.len()
                                } else {
                                    scratchpad_content.len() + 1 + content.len()
                                };

                            if projected_len > max_scratchpad_size {
                                tool_success = false;
                                format!(
                                    "Error: Scratchpad update would exceed limit of {} chars. Current: {}. Requested: {}.",
                                    max_scratchpad_size,
                                    scratchpad_content.len(),
                                    projected_len
                                )
                            } else {
                                if mode == "overwrite" {
                                    scratchpad_content = content;
                                } else {
                                    if !scratchpad_content.is_empty() {
                                        scratchpad_content.push('\n');
                                    }
                                    scratchpad_content.push_str(&content);
                                }

                                if let Some(store) = &context.store
                                    && let Err(e) =
                                        store.set(&agent_config.name, &scratchpad_content).await
                                {
                                    warn!(error=%e, "Failed to persist scratchpad to stream store.");
                                }

                                tool_success = true;
                                format!(
                                    "Scratchpad updated. Current size: {} chars.",
                                    scratchpad_content.len()
                                )
                            }
                        }
                        Err(e) => {
                            tool_success = false;
                            format!("Error parsing arguments: {e}")
                        }
                    }
                } else if tool_name == "compact_history"
                    && active_tool_names.contains("compact_history")
                {
                    let keep = serde_json::from_str::<Value>(&tool_call.function.arguments)
                        .ok()
                        .and_then(|v| v["keep_last_n_calls"].as_u64())
                        .map(|n| n.clamp(1, 10) as usize)
                        .unwrap_or(agent_config.compact_history_default_keep.max(1));

                    match compact_message_history(llm_client, agent_config, &messages, keep).await {
                        Ok(result) if result.compacted_count == 0 => {
                            tool_success = true;
                            "compact_history: nothing to compact (history already short)."
                                .to_string()
                        }
                        Ok(result) => {
                            messages = result.new_messages;
                            // Stage the appended summary so a too-large
                            // candidate that the squeezer can't shrink
                            // never gets persisted and re-bloats next prompt.
                            let mut candidate = scratchpad_content.clone();
                            if !candidate.is_empty() {
                                candidate.push('\n');
                            }
                            candidate.push_str("[compacted_history]\n");
                            candidate.push_str(&result.summary);

                            let final_scratchpad = match squeeze_scratchpad_if_full(
                                llm_client,
                                agent_config,
                                &candidate,
                                max_scratchpad_size,
                            )
                            .await
                            {
                                Ok(Some(squeezed)) => {
                                    info!(
                                        agent = %agent_config.name,
                                        old_len = candidate.len(),
                                        new_len = squeezed.len(),
                                        "scratchpad auto-squeezed during compact_history"
                                    );
                                    Some(squeezed)
                                }
                                Ok(None) => {
                                    if max_scratchpad_size == 0
                                        || candidate.len() <= max_scratchpad_size
                                    {
                                        Some(candidate)
                                    } else {
                                        warn!(
                                            agent = %agent_config.name,
                                            candidate_len = candidate.len(),
                                            max = max_scratchpad_size,
                                            "compact_history candidate exceeds scratchpad cap and \
                                             squeeze couldn't recover; keeping pre-compaction \
                                             scratchpad"
                                        );
                                        None
                                    }
                                }
                                Err(e) => {
                                    warn!(error=%e, "scratchpad squeeze failed; keeping pre-compaction scratchpad");
                                    None
                                }
                            };

                            if let Some(committed) = final_scratchpad {
                                scratchpad_content = committed;
                                // Same Eval-phase guard as the auto-invoke
                                // path above.
                                if context.phase != DeliberationPhase::Evaluating
                                    && let Some(store) = &context.store
                                    && let Err(e) =
                                        store.set(&agent_config.name, &scratchpad_content).await
                                {
                                    warn!(error=%e, "Failed to persist scratchpad after compact_history.");
                                }
                            }

                            tool_success = true;
                            format!(
                                "Compacted {} earlier tool calls into scratchpad. \
                                 Scratchpad now {} chars. Continue reasoning with \
                                 the compacted history.",
                                result.compacted_count,
                                scratchpad_content.len()
                            )
                        }
                        Err(e) => {
                            warn!(error=%e, "compact_history call failed");
                            tool_success = false;
                            format!("compact_history failed: {e}")
                        }
                    }
                } else if !active_tool_names.contains(tool_name.as_str()) {
                    // Tool was stripped (e.g., force_finalize); reject the call
                    warn!(tool_name = %tool_name, "LLM called a stripped tool (not in active set).");
                    tool_success = false;
                    format!("Error: Tool `{tool_name}` is not available in the current phase.")
                } else if let Some(tool) = tool_map.get(tool_name) {
                    let arg_str = &tool_call.function.arguments;
                    let args_result = if arg_str.trim().is_empty() {
                        Ok(serde_json::json!({}))
                    } else {
                        serde_json::from_str::<Value>(arg_str).map_err(|e| e.to_string())
                    };

                    match args_result {
                        Ok(args) => match tool.call(args).await {
                            Ok(result) => {
                                debug!(tool_name = %tool_name, output_length = result.len(), "Tool executed successfully.");
                                tool_success = true;
                                result
                            }
                            Err(e) => {
                                warn!(tool_name = %tool_name, error = %e, "Tool execution failed.");
                                tool_success = false;
                                format!("Error calling tool `{tool_name}`: {e}")
                            }
                        },
                        Err(e) => {
                            let msg = format!(
                                "Error parsing arguments for tool '{}': {}. Raw arguments: '{}'",
                                tool_name, e, arg_str
                            );
                            warn!(tool_name=%tool_name, error=%e, raw_args=%arg_str, "Failed to parse tool arguments.");
                            tool_success = false;
                            msg
                        }
                    }
                } else {
                    warn!(tool_name = %tool_name, "LLM called a tool that does not exist.");
                    tool_success = false;
                    format!("Error: Tool `{tool_name}` not found.")
                };

                let truncated = apply_tool_output_cap(
                    &mut tool_output,
                    agent_config.context_window,
                    estimated_input_tokens,
                    tool_name,
                );

                let latency_ms = tool_exec_start.elapsed().as_millis() as u64;
                let output_bytes = tool_output.len() as u64;
                // 4 chars/token rule-of-thumb; ceiling so non-empty
                // outputs under 4 bytes still report 1 token.
                let output_tokens_estimated =
                    Some((output_bytes.div_ceil(4)).min(u32::MAX as u64) as u32);
                *running_tool_output_bytes = running_tool_output_bytes.saturating_add(output_bytes);
                emit_for!(
                    context,
                    ToolCallExecuted {
                        tool_name: tool_name.clone(),
                        latency_ms,
                        success: tool_success,
                        output_bytes,
                        output_tokens_estimated,
                        truncated,
                        paginated: false,
                    }
                );

                info!(
                    target: "nsed_activity",
                    event = "tool_output",
                    agent = %agent_config.name,
                    tool = %tool_name,
                    output = %tool_output
                );

                if agent_config.disable_native_tools {
                    tool_outputs_text.push(format!("Tool Output ({tool_name}): {tool_output}"));
                } else {
                    messages.push(
                        ChatCompletionRequestToolMessage {
                            tool_call_id: tool_call.id.clone(),
                            content: ChatCompletionRequestToolMessageContent::Text(tool_output),
                        }
                        .into(),
                    );
                }
            }

            if agent_config.disable_native_tools && !tool_outputs_text.is_empty() {
                // Consolidate all tool outputs into a single User message to satisfy strict chat templates
                let combined_output = tool_outputs_text.join("\n\n");
                messages.push(
                    ChatCompletionRequestUserMessage {
                        content: async_openai::types::ChatCompletionRequestUserMessageContent::Text(
                            combined_output,
                        ),
                        ..Default::default()
                    }
                    .into(),
                );
            }
        } else {
            // No tool calls, this is the final answer.
            info!("LLM provided a final answer without a tool call.");
            return Ok(AgentResponse {
                content,
                tool_usage: tool_usage_stats,
                finish_reason,
                input_tokens: Some(total_input_tokens),
                output_tokens: Some(total_output_tokens),
                system_prompt: last_system_message,
                request_body: last_request_body,
                history: messages.clone(),
                final_scratchpad: if scratchpad_content.is_empty() {
                    None
                } else {
                    Some(scratchpad_content.clone())
                },
            });
        }

        // Record this iteration's duration for the time-aware tool stripping heuristic.
        iteration_durations.push(iter_start.elapsed());
    }

    // Max-iterations exhaustion: instead of bubbling an error that
    // blocks the orchestrator waiting for this agent's proposal /
    // evaluation, synthesize an empty-but-schema-valid terminal tool
    // response so the worker publishes SOMETHING on the agent's phase
    // subject. The orchestrator then aggregates it with the rest and
    // the deliberation proceeds. Erroring here would have left peers
    // blocked on the per-phase SLA floor before the missing agent got
    // an implicit max-score injection — expensive and visible.
    //
    // The synthetic content mirrors the expected terminal tool's JSON
    // shape so the caller's `serde_json::from_str::<T>(...)` succeeds
    // on first try (no retry loop burning budget with a known-stuck
    // agent):
    //   - submit_proposal:          empty solution_content, explanation
    //                                in thought_process so downstream
    //                                scoring sees the agent "voted
    //                                empty" rather than crashed.
    //   - submit_batch_evaluation:  evaluations: []  — no votes cast.
    //   - unknown terminal tool:    {} (fall back to generic object;
    //                                caller may still fail to deserialize,
    //                                but at least we don't crash here).
    warn!(
        agent = %agent_config.name,
        terminal_tool = ?terminal_tool_name,
        "Agent reached maximum iterations without a final answer — emitting empty synthetic terminal tool response so peers are not blocked."
    );
    let synthetic_content = empty_terminal_tool_content(terminal_tool_name);
    Ok(AgentResponse {
        content: synthetic_content,
        tool_usage: tool_usage_stats,
        finish_reason: Some("max_iterations".to_string()),
        input_tokens: Some(total_input_tokens),
        output_tokens: Some(total_output_tokens),
        system_prompt: last_system_message,
        request_body: last_request_body,
        history: messages.clone(),
        final_scratchpad: if scratchpad_content.is_empty() {
            None
        } else {
            Some(scratchpad_content.clone())
        },
    })
}

/// Size of the "terminate or die" tail window — the last N iterations
/// of `react_loop` have non-terminal tools stripped so the model's
/// only legal move is to call the terminal tool. N retries absorb
/// transient malformed terminal calls (truncation, bad JSON).
/// Override per-workload via `NSED_FINALIZE_WINDOW`; default 3
/// balances empirical LLM tool-call error rates (~5–20% per attempt)
/// against wasted free-thinking budget.
///
/// Pure-input form (consumes `Option<&str>` instead of reading the env
/// directly) so the resolution can be unit-tested without env-var
/// race hazards.
const DEFAULT_FINALIZE_WINDOW: usize = 3;

fn resolve_finalize_window(raw: Option<&str>) -> usize {
    raw.and_then(|s| s.parse::<usize>().ok())
        .filter(|n| *n > 0)
        .unwrap_or(DEFAULT_FINALIZE_WINDOW)
}

#[cfg(test)]
mod tests {
    use super::{
        DEFAULT_FINALIZE_WINDOW, FailureDumpParams, StructuredBatchEvaluationResponse,
        StructuredProposalResponse, apply_tool_output_cap, empty_terminal_tool_content,
        extract_evaluation_sections, resolve_finalize_window, strip_scratchpad,
        strip_thinking_prefix, strip_working_memory, write_failure_dump,
    };
    use serial_test::serial;

    #[test]
    fn finalize_window_defaults_when_env_unset() {
        assert_eq!(resolve_finalize_window(None), DEFAULT_FINALIZE_WINDOW);
        assert_eq!(resolve_finalize_window(None), 3);
    }

    #[test]
    fn finalize_window_parses_valid_value() {
        assert_eq!(resolve_finalize_window(Some("5")), 5);
        assert_eq!(resolve_finalize_window(Some("1")), 1);
        assert_eq!(resolve_finalize_window(Some("10")), 10);
    }

    #[test]
    fn finalize_window_falls_back_on_malformed_value() {
        assert_eq!(
            resolve_finalize_window(Some("abc")),
            DEFAULT_FINALIZE_WINDOW
        );
        assert_eq!(resolve_finalize_window(Some("")), DEFAULT_FINALIZE_WINDOW);
        assert_eq!(
            resolve_finalize_window(Some("3.5")),
            DEFAULT_FINALIZE_WINDOW
        );
        assert_eq!(resolve_finalize_window(Some("-1")), DEFAULT_FINALIZE_WINDOW);
    }

    #[test]
    fn finalize_window_rejects_zero() {
        // Zero would disable the tail-window guard entirely (the
        // `max_iterations > N && remaining_iterations <= N` check
        // collapses to "never trigger"); treat as malformed and fall
        // back so a stray `=0` env doesn't silently turn the safety
        // net off.
        assert_eq!(resolve_finalize_window(Some("0")), DEFAULT_FINALIZE_WINDOW);
    }

    use super::{
        ChatCompletionRequestAssistantMessage, ChatCompletionRequestMessage,
        ChatCompletionRequestSystemMessage, ChatCompletionRequestToolMessage,
        ChatCompletionRequestToolMessageContent, ChatCompletionRequestUserMessage,
        ChatCompletionToolType, compact_message_history, should_auto_compact,
        squeeze_scratchpad_if_full,
    };
    use crate::agents::config::AgentConfig;
    use crate::llms::{AiModel, ChatCompletionResult, RequestConfig, TimingMetadata};
    use crate::telemetry::LlmError;
    use async_openai::types::{
        ChatChoice, ChatCompletionMessageToolCall, ChatCompletionResponseMessage, CompletionUsage,
        CreateChatCompletionResponse, FinishReason as OAFinishReason, FunctionCall, Role,
    };
    use async_trait::async_trait;

    #[derive(Clone, Debug)]
    struct CannedSummaryModel {
        text: String,
    }

    #[async_trait]
    impl AiModel for CannedSummaryModel {
        async fn chat_completion(
            &self,
            _agent: &AgentConfig,
            _request_config: RequestConfig,
        ) -> Result<ChatCompletionResult, LlmError> {
            Ok(ChatCompletionResult {
                response: CreateChatCompletionResponse {
                    id: "canned".into(),
                    object: "chat.completion".into(),
                    created: 0,
                    model: "canned".into(),
                    choices: vec![ChatChoice {
                        index: 0,
                        message: ChatCompletionResponseMessage {
                            role: Role::Assistant,
                            content: Some(self.text.clone()),
                            tool_calls: None,
                            #[allow(deprecated)]
                            function_call: None,
                            refusal: None,
                            audio: None,
                        },
                        finish_reason: Some(OAFinishReason::Stop),
                        logprobs: None,
                    }],
                    usage: Some(CompletionUsage {
                        prompt_tokens: 0,
                        completion_tokens: 0,
                        total_tokens: 0,
                        prompt_tokens_details: None,
                        completion_tokens_details: None,
                    }),
                    service_tier: None,
                    system_fingerprint: None,
                },
                raw_request: String::new(),
                timing: TimingMetadata {
                    ttft_ms: None,
                    generation_ms: None,
                },
                provider_backend: None,
                shrink_info: None,
            })
        }
    }

    /// Echoes back the last user message in the request, so tests can
    /// assert that specific text actually reached the summariser
    /// prompt.
    #[derive(Clone, Debug, Default)]
    struct EchoSummaryModel;

    #[async_trait]
    impl AiModel for EchoSummaryModel {
        async fn chat_completion(
            &self,
            _agent: &AgentConfig,
            request_config: RequestConfig,
        ) -> Result<ChatCompletionResult, LlmError> {
            let prompt = request_config
                .messages
                .iter()
                .rev()
                .find_map(|m| {
                    if let ChatCompletionRequestMessage::User(u) = m {
                        if let async_openai::types::ChatCompletionRequestUserMessageContent::Text(
                            t,
                        ) = &u.content
                        {
                            return Some(t.clone());
                        }
                    }
                    None
                })
                .unwrap_or_default();
            Ok(ChatCompletionResult {
                response: CreateChatCompletionResponse {
                    id: "echo".into(),
                    object: "chat.completion".into(),
                    created: 0,
                    model: "echo".into(),
                    choices: vec![ChatChoice {
                        index: 0,
                        message: ChatCompletionResponseMessage {
                            role: Role::Assistant,
                            content: Some(prompt),
                            tool_calls: None,
                            #[allow(deprecated)]
                            function_call: None,
                            refusal: None,
                            audio: None,
                        },
                        finish_reason: Some(OAFinishReason::Stop),
                        logprobs: None,
                    }],
                    usage: Some(CompletionUsage {
                        prompt_tokens: 0,
                        completion_tokens: 0,
                        total_tokens: 0,
                        prompt_tokens_details: None,
                        completion_tokens_details: None,
                    }),
                    service_tier: None,
                    system_fingerprint: None,
                },
                raw_request: String::new(),
                timing: TimingMetadata {
                    ttft_ms: None,
                    generation_ms: None,
                },
                provider_backend: None,
                shrink_info: None,
            })
        }
    }

    fn agent_for_compaction() -> AgentConfig {
        AgentConfig {
            name: "compactor".into(),
            ..Default::default()
        }
    }

    fn synth_tool_pair(
        call_id: &str,
        tool_name: &str,
        result: &str,
    ) -> Vec<ChatCompletionRequestMessage> {
        vec![
            ChatCompletionRequestAssistantMessage {
                tool_calls: Some(vec![ChatCompletionMessageToolCall {
                    id: call_id.into(),
                    r#type: ChatCompletionToolType::Function,
                    function: FunctionCall {
                        name: tool_name.into(),
                        arguments: "{}".into(),
                    },
                }]),
                ..Default::default()
            }
            .into(),
            ChatCompletionRequestToolMessage {
                tool_call_id: call_id.into(),
                content: ChatCompletionRequestToolMessageContent::Text(result.into()),
            }
            .into(),
        ]
    }

    #[tokio::test]
    async fn compact_history_noop_when_below_keep_threshold() {
        let model = CannedSummaryModel {
            text: "should not run".into(),
        };
        let agent = agent_for_compaction();
        let mut messages: Vec<ChatCompletionRequestMessage> = vec![
            ChatCompletionRequestUserMessage {
                content: "go".into(),
                ..Default::default()
            }
            .into(),
        ];
        messages.extend(synth_tool_pair("c1", "read_file", "FILE A CONTENT"));
        messages.extend(synth_tool_pair("c2", "read_file", "FILE B CONTENT"));
        let original = messages.clone();

        let result = compact_message_history(&model, &agent, &messages, 2)
            .await
            .unwrap();
        assert_eq!(result.compacted_count, 0);
        assert_eq!(result.summary, "");
        assert_eq!(result.new_messages, original);
    }

    #[tokio::test]
    async fn compact_history_folds_older_tool_calls_into_summary() {
        let model = CannedSummaryModel {
            text: "Read /linux/scripts/checkpatch.pl lines 1-200; running hypothesis: \
                   `--strict` enabled; cited verbatim: ERROR(\"missing newline\")"
                .into(),
        };
        let agent = agent_for_compaction();
        let mut messages: Vec<ChatCompletionRequestMessage> = vec![
            ChatCompletionRequestSystemMessage {
                content: "system".into(),
                ..Default::default()
            }
            .into(),
            ChatCompletionRequestUserMessage {
                content: "go".into(),
                ..Default::default()
            }
            .into(),
        ];
        messages.extend(synth_tool_pair("c1", "read_file", "FILE A — 60K of source"));
        messages.extend(synth_tool_pair("c2", "grep_search", "8K of matches"));
        messages.extend(synth_tool_pair("c3", "pdf_query", "5K of pdf hits"));
        messages.extend(synth_tool_pair("c4", "read_file", "FILE D — 12K"));
        let pre_len = messages.len();

        let result = compact_message_history(&model, &agent, &messages, 2)
            .await
            .unwrap();

        assert_eq!(
            result.compacted_count, 2,
            "4 tool calls minus keep=2 = 2 folded"
        );
        assert!(
            result.summary.contains("checkpatch.pl"),
            "summary text round-trips"
        );
        assert!(
            result.new_messages.len() < pre_len,
            "compacted history must be shorter than input ({} → {})",
            pre_len,
            result.new_messages.len()
        );
        let has_compact_call = result.new_messages.iter().any(|m| {
            if let ChatCompletionRequestMessage::Assistant(a) = m
                && let Some(tcs) = &a.tool_calls
            {
                tcs.iter().any(|tc| tc.function.name == "compact_history")
            } else {
                false
            }
        });
        assert!(has_compact_call);
    }

    fn synth_user_tool_output(
        call_id: &str,
        tool_name: &str,
        result: &str,
    ) -> Vec<ChatCompletionRequestMessage> {
        vec![
            ChatCompletionRequestAssistantMessage {
                content: Some(
                    async_openai::types::ChatCompletionRequestAssistantMessageContent::Text(
                        format!("calling {tool_name}"),
                    ),
                ),
                ..Default::default()
            }
            .into(),
            ChatCompletionRequestUserMessage {
                content: format!("Tool Output ({call_id} {tool_name}): {result}").into(),
                ..Default::default()
            }
            .into(),
        ]
    }

    #[tokio::test]
    async fn compact_history_text_mode_includes_user_tool_output_in_summary() {
        // Sentinel echoes back its prompt so we can assert that the
        // user-rewritten tool result actually reached the summariser.
        let agent = AgentConfig {
            disable_native_tools: true,
            ..agent_for_compaction()
        };
        let model = EchoSummaryModel;
        let mut messages: Vec<ChatCompletionRequestMessage> = vec![
            ChatCompletionRequestSystemMessage {
                content: "system".into(),
                ..Default::default()
            }
            .into(),
            ChatCompletionRequestUserMessage {
                content: "go".into(),
                ..Default::default()
            }
            .into(),
        ];
        messages.extend(synth_user_tool_output("c1", "read_file", "FILE_A_BODY"));
        messages.extend(synth_user_tool_output("c2", "grep_search", "GREP_HITS"));
        messages.extend(synth_user_tool_output("c3", "pdf_query", "PDF_HITS"));

        let result = compact_message_history(&model, &agent, &messages, 1)
            .await
            .unwrap();

        let echoed = result.summary.clone();
        assert!(
            echoed.contains("FILE_A_BODY") || echoed.contains("GREP_HITS"),
            "summariser must see at least one User(Tool Output) body, got: {echoed}"
        );
        assert!(result.compacted_count >= 1);
    }

    #[tokio::test]
    async fn compact_history_text_mode_emits_text_only_synthetic_pair() {
        let agent = AgentConfig {
            disable_native_tools: true,
            ..agent_for_compaction()
        };
        let model = CannedSummaryModel {
            text: "summary".into(),
        };
        let mut messages: Vec<ChatCompletionRequestMessage> = vec![
            ChatCompletionRequestSystemMessage {
                content: "system".into(),
                ..Default::default()
            }
            .into(),
            ChatCompletionRequestUserMessage {
                content: "go".into(),
                ..Default::default()
            }
            .into(),
        ];
        messages.extend(synth_user_tool_output("c1", "read_file", "A"));
        messages.extend(synth_user_tool_output("c2", "read_file", "B"));
        messages.extend(synth_user_tool_output("c3", "read_file", "C"));

        let result = compact_message_history(&model, &agent, &messages, 1)
            .await
            .unwrap();

        // No native tool_calls Assistant or Tool roles must appear in
        // the rewritten history — would break the next provider call
        // when `tools: None` is sent.
        for m in &result.new_messages {
            assert!(
                !matches!(m, ChatCompletionRequestMessage::Tool(_)),
                "text-mode compact must not emit native Tool role"
            );
            if let ChatCompletionRequestMessage::Assistant(a) = m {
                assert!(
                    a.tool_calls.is_none() || a.tool_calls.as_ref().unwrap().is_empty(),
                    "text-mode compact must not emit native tool_calls"
                );
            }
        }
        // The synthetic compaction must surface as a `Tool Output (compact_history)`
        // user message so subsequent is_tool_boundary scans see it.
        let has_marker = result.new_messages.iter().any(|m| {
            if let ChatCompletionRequestMessage::User(u) = m
                && let async_openai::types::ChatCompletionRequestUserMessageContent::Text(t) =
                    &u.content
            {
                t.contains("Tool Output (compact_history)")
            } else {
                false
            }
        });
        assert!(has_marker, "missing compact_history user-message marker");
    }

    #[tokio::test]
    async fn compact_history_clamps_keep_zero_to_one() {
        // Keep=0 would index `tool_msg_indices[len()]` and panic. The
        // internal `.max(1)` clamp must absorb the bad input.
        let agent = agent_for_compaction();
        let model = CannedSummaryModel {
            text: "summary".into(),
        };
        let mut messages: Vec<ChatCompletionRequestMessage> = vec![
            ChatCompletionRequestSystemMessage {
                content: "system".into(),
                ..Default::default()
            }
            .into(),
            ChatCompletionRequestUserMessage {
                content: "go".into(),
                ..Default::default()
            }
            .into(),
        ];
        messages.extend(synth_tool_pair("c1", "read_file", "A"));
        messages.extend(synth_tool_pair("c2", "read_file", "B"));
        let result = compact_message_history(&model, &agent, &messages, 0)
            .await
            .unwrap();
        assert_eq!(result.compacted_count, 1, "keep=0 coerced to keep=1");
    }

    #[tokio::test]
    async fn scratchpad_squeeze_noop_when_under_threshold() {
        let model = CannedSummaryModel {
            text: "should not run".into(),
        };
        let agent = agent_for_compaction();
        let scratchpad = "x".repeat(500);
        let max = 1000;
        let result = squeeze_scratchpad_if_full(&model, &agent, &scratchpad, max)
            .await
            .unwrap();
        assert!(
            result.is_none(),
            "≤95% scratchpad must not trigger LLM call"
        );
    }

    #[tokio::test]
    async fn scratchpad_squeeze_compresses_when_over_threshold() {
        let model = CannedSummaryModel {
            text: "[compressed older sections]".into(),
        };
        let agent = agent_for_compaction();
        let scratchpad = "x".repeat(960);
        let max = 1000; // 96% full → over the threshold
        let result = squeeze_scratchpad_if_full(&model, &agent, &scratchpad, max)
            .await
            .unwrap();
        let squeezed = result.expect("≥95% scratchpad triggers squeeze");
        assert!(squeezed.contains("[compressed older sections]"));
        assert!(
            squeezed.len() < scratchpad.len(),
            "squeezed must be shorter"
        );
    }

    #[test]
    fn scratchpad_squeeze_fraction_default_pinned() {
        let cfg = AgentConfig::default();
        assert!((cfg.scratchpad_squeeze_fraction - 0.95).abs() < f64::EPSILON);
        assert_eq!(cfg.compact_history_default_keep, 2);
    }

    #[test]
    fn auto_compact_triggers_at_or_above_90_percent() {
        assert!(should_auto_compact(480_000, 131_072, 10, 4.0));
        assert!(should_auto_compact(360, 100, 10, 4.0));
    }

    #[test]
    fn auto_compact_skips_under_90_percent() {
        assert!(!should_auto_compact(200_000, 131_072, 10, 4.0));
    }

    #[test]
    fn auto_compact_disabled_when_context_window_unknown() {
        // ctx <= 0 means provider doesn't expose it; let the SDK
        // shrink-guard alone handle overflow.
        assert!(!should_auto_compact(1_000_000, 0, 100, 4.0));
        assert!(!should_auto_compact(1_000_000, -1, 100, 4.0));
    }

    #[test]
    fn auto_compact_skips_when_history_too_short() {
        // High utilization but only 4 messages: nothing useful to
        // fold, so skip and let the SDK shrink-guard engage.
        assert!(!should_auto_compact(480_000, 131_072, 4, 4.0));
        assert!(!should_auto_compact(480_000, 131_072, 1, 4.0));
    }

    #[test]
    fn auto_compact_honours_configured_chars_per_token() {
        // CJK / code: ~1.5 chars per token. 200_000 chars at 1.5 →
        // 133k tokens which crosses 90% of 131k.
        assert!(should_auto_compact(200_000, 131_072, 10, 1.5));
        // Same char count at default 4.0 stays under threshold.
        assert!(!should_auto_compact(200_000, 131_072, 10, 4.0));
        // Non-positive cpt falls back to the 4.0 default rather than
        // dividing by zero / panicking.
        assert!(!should_auto_compact(200_000, 131_072, 10, 0.0));
    }

    #[test]
    fn tool_cap_truncates_when_output_exceeds_fraction_of_remaining_context() {
        // 131k ctx, 10k input → ~121k tokens remaining, 10% in chars
        // ≈ 48400 bytes. A 100KB result must clip.
        let mut output = "X".repeat(100_000);
        let truncated = apply_tool_output_cap(&mut output, 131_072, 10_000, "read_file");
        assert!(truncated, "100KB result against 131k ctx must clip");
        assert!(
            output.len() < 100_000,
            "output must be shorter than original after cap"
        );
        assert!(
            output.contains("[truncated:"),
            "marker must be present so the model sees the clip"
        );
    }

    #[test]
    fn tool_cap_passthrough_when_under_budget() {
        // 4KB result against 131k ctx → way under 10% cap → no clip.
        let mut output = "Y".repeat(4_096);
        let truncated = apply_tool_output_cap(&mut output, 131_072, 1_000, "read_file");
        assert!(!truncated);
        assert_eq!(output.len(), 4_096, "small output must pass through");
        assert!(!output.contains("[truncated:"));
    }

    #[test]
    fn tool_cap_noop_when_context_window_unknown() {
        let mut output = "Z".repeat(10_000);
        let truncated = apply_tool_output_cap(&mut output, 0, 1_000, "read_file");
        assert!(!truncated);
        assert_eq!(output.len(), 10_000);
    }

    #[test]
    fn tool_cap_respects_utf8_char_boundary() {
        // Every char is the 3-byte 中. If `cap_bytes` lands inside a
        // multi-byte char, naive `truncate` panics — `apply_tool_output_cap`
        // walks back to a boundary first.
        let mut output = "中".repeat(2_000);
        let original_byte_len = output.len();
        let truncated = apply_tool_output_cap(&mut output, 4_096, 0, "read_file");
        assert!(truncated);
        assert!(output.len() < original_byte_len);
        assert!(output.contains("[truncated:"));
        // The truncated prefix (everything before the marker) must
        // be a sequence of full `中` codepoints — no half-character
        // landed at the boundary.
        let prefix = output
            .split("\n[truncated:")
            .next()
            .expect("marker delimiter must exist");
        assert!(
            prefix.chars().all(|c| c == '中'),
            "truncated prefix must end on a full UTF-8 character; got {prefix:?}"
        );
    }

    #[test]
    fn tool_cap_replaces_with_marker_when_remaining_budget_zero() {
        // `estimated_input_tokens >= context_window` means
        // `remaining_tokens == 0` and `cap_bytes == 0`. The cap
        // must replace the entire payload with a marker so the
        // model sees the empty-budget signal instead of inheriting
        // verbatim oversized output.
        let mut output = "X".repeat(50_000);
        let truncated = apply_tool_output_cap(&mut output, 1_000, 1_000, "read_file");
        assert!(truncated);
        assert!(
            output.starts_with("[truncated: no remaining context budget"),
            "marker must replace output when budget is 0; got {:?}",
            &output[..50.min(output.len())]
        );
        assert!(output.len() < 200, "marker must be short");
    }

    // Prompt-exposure guardrail tests live alongside any concrete
    // `OutputLeakDetector` implementations — see the `with_output_guard`
    // doc-comment on `ProposerEvaluatorAgent`.

    // ─── empty_terminal_tool_content — max-iterations fallback ───────
    //
    // On exhaustion, `react_loop` emits a schema-valid but semantically
    // empty response so the orchestrator can proceed without waiting
    // on a stuck agent. The response must deserialize into the expected
    // terminal tool's struct without error.

    #[test]
    fn empty_submit_proposal_content_has_explanatory_thought_and_empty_solution() {
        let json_str = empty_terminal_tool_content(Some("submit_proposal"));
        let parsed: StructuredProposalResponse = serde_json::from_str(&json_str)
            .expect("must round-trip into StructuredProposalResponse");
        assert!(
            parsed.thought_process.contains("maximum iterations"),
            "thought_process should explain why the agent voted empty"
        );
        assert_eq!(
            parsed.solution_content, "",
            "solution_content must be empty"
        );
    }

    #[test]
    fn empty_submit_batch_evaluation_content_is_empty_array() {
        let json_str = empty_terminal_tool_content(Some("submit_batch_evaluation"));
        let parsed: StructuredBatchEvaluationResponse = serde_json::from_str(&json_str)
            .expect("must round-trip into StructuredBatchEvaluationResponse");
        assert!(parsed.evaluations.is_empty(), "evaluations must be empty");
    }

    #[test]
    fn empty_terminal_tool_content_unknown_name_returns_empty_object() {
        assert_eq!(empty_terminal_tool_content(None), "{}");
        assert_eq!(empty_terminal_tool_content(Some("other_tool")), "{}");
    }

    #[test]
    fn strip_scratchpad_removes_block() {
        let input = "\n\n<scratchpad>\n(This persists across tool calls.)\nsome notes\n</scratchpad>\n\nOriginal prompt text here";
        let result = strip_scratchpad(input);
        assert_eq!(result, "Original prompt text here");
        assert!(!result.contains("<scratchpad>"));
    }

    #[test]
    fn strip_scratchpad_preserves_text_without_block() {
        let input = "Just a normal prompt with no scratchpad";
        assert_eq!(strip_scratchpad(input), input);
    }

    #[test]
    fn strip_scratchpad_handles_only_scratchpad() {
        let input = "<scratchpad>\nsome notes\n</scratchpad>";
        let result = strip_scratchpad(input);
        assert_eq!(result, "");
    }

    #[test]
    fn strip_scratchpad_handles_unclosed_tag() {
        let input = "<scratchpad>\nsome notes without closing tag\nOriginal text";
        let result = strip_scratchpad(input);
        // Unclosed tag — return as-is
        assert_eq!(result, input);
    }

    #[test]
    fn strip_scratchpad_preserves_text_before_and_after() {
        let input = "Before text\n\n<scratchpad>\nnotes\n</scratchpad>\n\nAfter text";
        let result = strip_scratchpad(input);
        assert_eq!(result, "Before text\n\nAfter text");
    }

    /// When merge_system_prompt is true and a retry provides input_history that
    /// already contains "{sys_prompt}\n\n<scratchpad>...</scratchpad>\n\n{user_query}",
    /// extracting original_user_content must yield only the user_query (after
    /// </scratchpad>), not the entire text with system prompt prefix.
    #[test]
    fn strip_scratchpad_merge_system_prompt_extracts_after_closing_tag() {
        let merged = "You are a helpful agent.\n\n<scratchpad>\n(This persists across tool calls.)\n(Empty)\n</scratchpad>\n\nWhat is the meaning of life?";
        // Simulates what the merge_system_prompt branch does: take everything
        // after </scratchpad>, trimmed.
        let end_idx = merged.find("</scratchpad>").unwrap();
        let extracted = merged[end_idx + "</scratchpad>".len()..].trim_start();
        assert_eq!(extracted, "What is the meaning of life?");
    }

    #[test]
    fn test_utf8_safe_truncation() {
        // "日本語テスト" is 6 chars but 18 bytes
        let mut note = "日本語テスト".to_string();
        let max_len = 10; // Would land in the middle of a multi-byte char
        let suffix = "...(truncated)";
        if note.len() > max_len {
            let truncate_at = max_len.saturating_sub(suffix.len());
            let safe_at = note
                .char_indices()
                .map(|(i, _)| i)
                .take_while(|&i| i <= truncate_at)
                .last()
                .unwrap_or(0);
            note.truncate(safe_at);
            note.push_str(suffix);
        }
        // Should not panic and should be valid UTF-8
        assert!(note.is_char_boundary(0));
        assert!(note.ends_with(suffix));
    }

    // =========================================================================
    // strip_working_memory() Tests
    // =========================================================================

    #[test]
    fn strip_working_memory_removes_single_block() {
        let input = "<working_memory>ephemeral thoughts</working_memory>";
        assert_eq!(strip_working_memory(input), "");
    }

    #[test]
    fn strip_working_memory_preserves_other_sections() {
        let input = "<key_findings>important stuff</key_findings>\n\n<working_memory>scratch</working_memory>\n\n<strategy>my plan</strategy>";
        let result = strip_working_memory(input);
        assert!(result.contains("<key_findings>important stuff</key_findings>"));
        assert!(result.contains("<strategy>my plan</strategy>"));
        assert!(!result.contains("<working_memory>"));
        assert!(!result.contains("scratch"));
    }

    #[test]
    fn strip_working_memory_handles_multiple_blocks() {
        let input = "<working_memory>block1</working_memory>\n\nsome text\n\n<working_memory>block2</working_memory>";
        let result = strip_working_memory(input);
        assert!(!result.contains("<working_memory>"));
        assert!(result.contains("some text"));
    }

    #[test]
    fn strip_working_memory_handles_unclosed_tag() {
        let input = "<key_findings>ok</key_findings>\n\n<working_memory>unclosed block";
        let result = strip_working_memory(input);
        // Falls back to keeping malformed content as-is
        assert!(result.contains("<working_memory>"));
    }

    #[test]
    fn strip_working_memory_no_op_when_absent() {
        let input = "<key_findings>data</key_findings>\n<strategy>plan</strategy>";
        assert_eq!(strip_working_memory(input), input);
    }

    #[test]
    fn strip_working_memory_preserves_text_before_and_after() {
        let input = "Before\n\n<working_memory>temp</working_memory>\n\nAfter";
        let result = strip_working_memory(input);
        assert_eq!(result, "Before\n\nAfter");
    }

    // =========================================================================
    // extract_evaluation_sections() Tests
    // =========================================================================

    #[test]
    fn extract_evaluation_sections_both_present() {
        let input = "<working_memory>trash</working_memory>\n<key_findings>findings here</key_findings>\n<strategy>my strategy</strategy>";
        let result = extract_evaluation_sections(input);
        assert!(result.contains("<key_findings>findings here</key_findings>"));
        assert!(result.contains("<strategy>my strategy</strategy>"));
        assert!(!result.contains("working_memory"));
        assert!(!result.contains("trash"));
    }

    #[test]
    fn extract_evaluation_sections_only_key_findings() {
        let input = "some preamble\n<key_findings>important data</key_findings>\nother stuff";
        let result = extract_evaluation_sections(input);
        assert_eq!(result, "<key_findings>important data</key_findings>");
    }

    #[test]
    fn extract_evaluation_sections_only_strategy() {
        let input = "prefix\n<strategy>my plan</strategy>\nsuffix";
        let result = extract_evaluation_sections(input);
        assert_eq!(result, "<strategy>my plan</strategy>");
    }

    #[test]
    fn extract_evaluation_sections_fallback_when_none() {
        let input = "No structured sections here, just plain text notes";
        let result = extract_evaluation_sections(input);
        assert_eq!(
            result, input,
            "Should fall back to full text when no sections found"
        );
    }

    #[test]
    fn extract_evaluation_sections_handles_multiline_content() {
        let input = "<key_findings>\n  - Finding 1\n  - Finding 2\n</key_findings>\n<strategy>\n  Step A\n  Step B\n</strategy>";
        let result = extract_evaluation_sections(input);
        assert!(result.contains("Finding 1"));
        assert!(result.contains("Finding 2"));
        assert!(result.contains("Step A"));
    }

    #[test]
    fn extract_evaluation_sections_ignores_unclosed_tags() {
        let input = "<key_findings>open section without closing\n<strategy>also unclosed";
        let result = extract_evaluation_sections(input);
        // Both unclosed → falls back to full text
        assert_eq!(result, input);
    }

    // =========================================================================
    // Regression: StructuredProposalResponse parsing
    // From failures/quant-ml_MACRO/parse_error_r1.md
    // =========================================================================

    /// Regression: Mistral returns thought_process as array of strings instead of
    /// a single string. Previously failed with "invalid type: sequence, expected a string".
    #[test]
    fn regression_proposal_thought_process_as_array() {
        let json = serde_json::json!({
            "thought_process": [
                "Step 1: Asset Class Allocation",
                "Current allocation is split as Equities (50%), Fixed Income (30%), Alternatives (20%).",
                "Step 2: Geographic Tilt",
                "The geographic tilt remains balanced."
            ],
            "solution_content": "Proposed allocation: Equities 45%, Fixed Income 35%, Alternatives 20%"
        });
        let result: StructuredProposalResponse = serde_json::from_value(json).unwrap();
        assert!(
            result
                .thought_process
                .contains("Step 1: Asset Class Allocation")
        );
        assert!(result.thought_process.contains("Step 2: Geographic Tilt"));
        // Array elements joined with newlines
        assert!(result.thought_process.contains('\n'));
        assert_eq!(
            result.solution_content,
            "Proposed allocation: Equities 45%, Fixed Income 35%, Alternatives 20%"
        );
    }

    /// Regression: Mistral wraps solution_content in a nested object instead of a string.
    /// Previously failed with "invalid type: map, expected a string".
    #[test]
    fn regression_proposal_solution_content_as_nested_object() {
        let json = serde_json::json!({
            "solution_content": {
                "thought_process": ["Step 1", "Step 2"],
                "allocations": {"Equities": 45, "Fixed Income": 35},
                "hedge_portfolio": {"Strategy": "Protective Puts"}
            },
            "thought_process": "My reasoning steps."
        });
        let result: StructuredProposalResponse = serde_json::from_value(json).unwrap();
        assert_eq!(result.thought_process, "My reasoning steps.");
        // Nested object serialized to JSON string
        assert!(result.solution_content.contains("Equities"));
        assert!(result.solution_content.contains("Protective Puts"));
    }

    /// Normal string values still work as expected.
    #[test]
    fn proposal_response_with_normal_strings() {
        let json = serde_json::json!({
            "thought_process": "I analyzed the problem carefully.",
            "solution_content": "The answer is 42."
        });
        let result: StructuredProposalResponse = serde_json::from_value(json).unwrap();
        assert_eq!(result.thought_process, "I analyzed the problem carefully.");
        assert_eq!(result.solution_content, "The answer is 42.");
    }

    // =========================================================================
    // strip_thinking_prefix tests
    // =========================================================================

    #[test]
    fn strip_thinking_prefix_final() {
        let input = "final**Critique Integration:** The peer reviews...";
        let result = strip_thinking_prefix(input);
        assert_eq!(result, "**Critique Integration:** The peer reviews...");
    }

    #[test]
    fn strip_thinking_prefix_analysis_assistant_final() {
        let input = "analysisWe need to submit full proposal.assistantfinal**Critique Integration:** ...rest";
        let result = strip_thinking_prefix(input);
        assert_eq!(result, "**Critique Integration:** ...rest");
    }

    #[test]
    fn strip_thinking_prefix_commentary() {
        let input = "commentaryto=functions.submit_proposal json{\"thought_process\": \"test\"}";
        let result = strip_thinking_prefix(input);
        assert_eq!(result, "{\"thought_process\": \"test\"}");
    }

    #[test]
    fn strip_thinking_prefix_clean_content_unchanged() {
        let input = "**Critique Integration:** Normal content.";
        let result = strip_thinking_prefix(input);
        assert_eq!(result, "**Critique Integration:** Normal content.");
    }

    #[test]
    fn strip_thinking_prefix_json_unchanged() {
        let input = "{\"thought_process\": \"test\", \"solution_content\": \"x\"}";
        let result = strip_thinking_prefix(input);
        assert_eq!(result, input);
    }

    // =========================================================================
    // StructuredBatchEvaluationResponse alias tests
    // =========================================================================

    /// Models use "candidate_evaluations" instead of "evaluations"
    #[test]
    fn batch_eval_alias_candidate_evaluations() {
        let json = r#"{"candidate_evaluations": [{"candidate_id": "A", "endorsement_weight": 75, "stance": "agree", "claim_assessments": [], "disagreements": [], "category_scores": {"correctness": 80, "completeness": 75, "novelty": 70, "feasibility": 85, "evidence_quality": 78}}]}"#;
        let result: StructuredBatchEvaluationResponse = serde_json::from_str(json).unwrap();
        assert_eq!(result.evaluations.len(), 1);
        assert_eq!(result.evaluations[0].agent_id, "A");
    }

    /// Models use "candidates" instead of "evaluations"
    #[test]
    fn batch_eval_alias_candidates() {
        let json = r#"{"candidates": [{"agent_id": "B", "endorsement_weight": 60, "stance": "disagree"}]}"#;
        let result: StructuredBatchEvaluationResponse = serde_json::from_str(json).unwrap();
        assert_eq!(result.evaluations.len(), 1);
        assert_eq!(result.evaluations[0].agent_id, "B");
    }

    /// Full gpt-oss MOMENTUM r2 eval payload with "claim_id" only in claim_assessments
    /// (missing "claim" text — uses "verdict" + "claim_id" pattern)
    #[test]
    fn batch_eval_gpt_oss_claim_id_only_assessments() {
        let json = r#"{"evaluations": [{"agent_id": "Candidate_C", "stance": "disagree", "claim_assessments": [{"claim_id": "C1", "verdict": "unverified"}, {"claim_id": "C2", "verdict": "contested"}, {"claim_id": "C3", "verdict": "verified"}], "disagreements": [{"claim_id": "C1", "proposal": "Equity allocation of 40%", "counter": "40% equity exceeds drawdown limit.", "confidence": "high"}], "category_scores": {"correctness": 40, "completeness": 30, "novelty": 20, "feasibility": 45, "evidence_quality": 30}, "endorsement_weight": 25}]}"#;
        let result: StructuredBatchEvaluationResponse = serde_json::from_str(json).unwrap();
        assert_eq!(result.evaluations[0].disagreements.len(), 1);
        assert_eq!(
            result.evaluations[0].disagreements[0].evaluator_position,
            "40% equity exceeds drawdown limit."
        );
        assert_eq!(
            result.evaluations[0].disagreements[0].proposal_claims,
            "Equity allocation of 40%"
        );
    }

    // =========================================================================
    // write_failure_dump config precedence tests
    // =========================================================================

    /// Config "off" must suppress dumps without falling through to env var.
    #[test]
    fn dump_mode_config_off_suppresses_dumps() {
        let result = write_failure_dump(FailureDumpParams {
            kind: "test",
            agent_name: "test-agent",
            model_name: "model",
            provider_id: "provider",
            error: "some error",
            session_id: None,
            phase: None,
            round: None,
            attempt: None,
            finish_reason: None,
            input_tokens: None,
            output_tokens: None,
            response_content: Some("content"),
            system_prompt: None,
            request_body: None,
            messages: None,
            failure_dumps_config: Some("off"),
        });
        // Config "off" should short-circuit and return None without
        // checking the NSED_FAILURE_DUMPS env var.
        assert!(result.is_none(), "config 'off' should suppress dumps");
    }

    /// Config None falls through to env var (no env var set = no dumps).
    #[test]
    fn dump_mode_config_none_no_env_var() {
        let result = write_failure_dump(FailureDumpParams {
            kind: "test",
            agent_name: "test-agent",
            model_name: "model",
            provider_id: "provider",
            error: "some error",
            session_id: None,
            phase: None,
            round: None,
            attempt: None,
            finish_reason: None,
            input_tokens: None,
            output_tokens: None,
            response_content: Some("content"),
            system_prompt: None,
            request_body: None,
            messages: None,
            failure_dumps_config: None,
        });
        // Without config and without env var, no dumps should be written.
        assert!(result.is_none(), "no config + no env var = no dumps");
    }

    // =========================================================================
    // Additional strip_scratchpad tests
    // =========================================================================

    #[test]
    fn test_strip_scratchpad_nested_tags() {
        // Nested scratchpad tags — strip_scratchpad finds the first <scratchpad>
        // and the first </scratchpad> after it, removing that range. The outer
        // closing tag remains as leftover text.
        let input = "<scratchpad><scratchpad>inner</scratchpad>outer</scratchpad>after content";
        let result = strip_scratchpad(input);
        // The function strips from first <scratchpad> to first </scratchpad>,
        // leaving "outer</scratchpad>after content" which gets trimmed.
        assert!(!result.contains("<scratchpad>inner</scratchpad>"));
        // The remaining text should include "after content".
        assert!(result.contains("after content"));
    }

    #[test]
    fn test_strip_scratchpad_empty() {
        let result = strip_scratchpad("");
        assert_eq!(result, "");
    }

    #[test]
    fn test_strip_scratchpad_no_tags() {
        let input = "This is plain text with no scratchpad tags at all.";
        let result = strip_scratchpad(input);
        assert_eq!(result, input);
    }

    #[test]
    fn test_strip_scratchpad_missing_close() {
        // Input with opening tag but no closing tag — should return as-is.
        let input = "<scratchpad>content without closing tag";
        let result = strip_scratchpad(input);
        assert_eq!(result, input);
    }

    // =========================================================================
    // truncate_for_dump tests
    // =========================================================================

    #[test]
    fn test_truncate_for_dump() {
        use super::truncate_for_dump;

        // Short string — unchanged.
        let short = "hello";
        assert_eq!(truncate_for_dump(short, 100), "hello");

        // Long string — truncated with indicator.
        let long: String = "a".repeat(200);
        let result = truncate_for_dump(&long, 50);
        assert!(result.len() < long.len());
        assert!(result.contains("... (truncated at 50 chars)"));
        // The truncated prefix should be exactly 50 chars from the original.
        let prefix_part = result
            .split("\n... (truncated at 50 chars)")
            .next()
            .unwrap();
        assert_eq!(prefix_part.chars().count(), 50);

        // Exact boundary — no truncation.
        let exact: String = "b".repeat(50);
        assert_eq!(truncate_for_dump(&exact, 50), exact);
    }

    // =========================================================================
    // Additional strip_working_memory tests
    // =========================================================================

    #[test]
    fn test_strip_working_memory_nested() {
        // Nested working memory tags — the while loop strips from first open to
        // first close, then repeats.
        let input =
            "<working_memory>outer<working_memory>inner</working_memory>rest</working_memory>final";
        let result = strip_working_memory(input);
        // First iteration strips "<working_memory>outer<working_memory>inner</working_memory>"
        // leaving "rest</working_memory>final". Second iteration finds no proper
        // <working_memory> open tag before </working_memory>, so the loop ends.
        // The key assertion: no <working_memory> open tags remain.
        assert!(!result.contains("<working_memory>"));
        assert!(result.contains("final"));
    }

    // =========================================================================
    // strip_thinking_prefix with JSON tests
    // =========================================================================

    #[test]
    fn test_strip_thinking_prefix_with_json() {
        // The <think> tag stripping happens in the react_loop (not strip_thinking_prefix).
        // strip_thinking_prefix handles leaked model tokens like "final", "analysis", "commentary".
        // For <think> tags, the react_loop uses string replacement (lines 1647-1663).
        // Here we verify that strip_thinking_prefix leaves JSON after known prefixes intact.
        let input = r#"final{"score": 0.5}"#;
        let result = strip_thinking_prefix(input);
        assert_eq!(result, r#"{"score": 0.5}"#);

        // Verify the result is valid JSON.
        let val: serde_json::Value = serde_json::from_str(result).unwrap();
        assert_eq!(val["score"], 0.5);
    }

    // =========================================================================
    // deserialize_string_or_array_or_object — additional coverage
    // =========================================================================

    #[test]
    fn deser_string_or_array_array_of_strings() {
        let json = serde_json::json!({
            "thought_process": ["Alpha", "Beta", "Gamma"],
            "solution_content": "plain"
        });
        let result: StructuredProposalResponse = serde_json::from_value(json).unwrap();
        assert_eq!(result.thought_process, "Alpha\nBeta\nGamma");
        assert_eq!(result.solution_content, "plain");
    }

    #[test]
    fn deser_string_or_array_empty_array() {
        let json = serde_json::json!({
            "thought_process": [],
            "solution_content": "content"
        });
        let result: StructuredProposalResponse = serde_json::from_value(json).unwrap();
        assert_eq!(
            result.thought_process, "",
            "Empty array should produce empty string"
        );
    }

    #[test]
    fn deser_string_or_array_nested_object() {
        let json = serde_json::json!({
            "thought_process": {
                "step1": "Analyze requirements",
                "step2": "Propose solution"
            },
            "solution_content": "answer"
        });
        let result: StructuredProposalResponse = serde_json::from_value(json).unwrap();
        // Object is serialized to compact JSON string
        assert!(result.thought_process.contains("step1"));
        assert!(result.thought_process.contains("Analyze requirements"));
        // Must be valid JSON
        let _: serde_json::Value = serde_json::from_str(&result.thought_process).unwrap();
    }

    #[test]
    fn deser_string_or_array_mixed_array() {
        // Array containing both strings and non-string values (numbers, objects)
        let json = serde_json::json!({
            "thought_process": ["Step 1", 42, {"detail": "nested"}, true],
            "solution_content": "done"
        });
        let result: StructuredProposalResponse = serde_json::from_value(json).unwrap();
        let parts: Vec<&str> = result.thought_process.split('\n').collect();
        assert_eq!(parts.len(), 4);
        assert_eq!(parts[0], "Step 1");
        assert_eq!(parts[1], "42");
        assert!(parts[2].contains("nested"));
        assert_eq!(parts[3], "true");
    }

    #[test]
    fn deser_string_or_array_single_element_array() {
        let json = serde_json::json!({
            "thought_process": ["Only one element"],
            "solution_content": "x"
        });
        let result: StructuredProposalResponse = serde_json::from_value(json).unwrap();
        assert_eq!(result.thought_process, "Only one element");
    }

    #[test]
    fn deser_string_or_array_number_value() {
        // A bare number should be serialized to string via the catch-all branch
        let json = serde_json::json!({
            "thought_process": 12345,
            "solution_content": "x"
        });
        let result: StructuredProposalResponse = serde_json::from_value(json).unwrap();
        assert_eq!(result.thought_process, "12345");
    }

    #[test]
    fn deser_string_or_array_boolean_value() {
        let json = serde_json::json!({
            "thought_process": true,
            "solution_content": false
        });
        let result: StructuredProposalResponse = serde_json::from_value(json).unwrap();
        assert_eq!(result.thought_process, "true");
        assert_eq!(result.solution_content, "false");
    }

    #[test]
    fn deser_string_or_array_null_value() {
        let json = serde_json::json!({
            "thought_process": null,
            "solution_content": "x"
        });
        let result: StructuredProposalResponse = serde_json::from_value(json).unwrap();
        assert_eq!(result.thought_process, "null");
    }

    #[test]
    fn deser_string_or_array_deeply_nested_object() {
        let json = serde_json::json!({
            "thought_process": "simple",
            "solution_content": {
                "level1": {
                    "level2": {
                        "data": [1, 2, 3]
                    }
                }
            }
        });
        let result: StructuredProposalResponse = serde_json::from_value(json).unwrap();
        assert!(result.solution_content.contains("level2"));
        assert!(result.solution_content.contains("[1,2,3]"));
    }

    // =========================================================================
    // strip_thinking_prefix — comprehensive pattern coverage
    // =========================================================================

    #[test]
    fn strip_thinking_prefix_assistant_then_json() {
        // "assistant" followed by "{" should strip the prefix
        let input = "analysisblahassistant{\"key\": \"value\"}";
        let result = strip_thinking_prefix(input);
        assert_eq!(result, "{\"key\": \"value\"}");
    }

    #[test]
    fn strip_thinking_prefix_assistant_then_heading() {
        // "assistant" followed by "#" should strip the prefix
        let input = "some thinking tokensassistant# Heading";
        let result = strip_thinking_prefix(input);
        assert_eq!(result, "# Heading");
    }

    #[test]
    fn strip_thinking_prefix_assistant_then_bold() {
        // "assistant" followed by "**" should strip the prefix
        let input = "reflectionassistant**Bold content**";
        let result = strip_thinking_prefix(input);
        assert_eq!(result, "**Bold content**");
    }

    #[test]
    fn strip_thinking_prefix_assistant_then_final_then_bold() {
        // "assistant" followed by "final" then actual content
        let input = "thinkingassistantfinal**Real content here**";
        let result = strip_thinking_prefix(input);
        assert_eq!(result, "**Real content here**");
    }

    #[test]
    fn strip_thinking_prefix_bare_final_with_heading() {
        let input = "final# Asset Allocation";
        let result = strip_thinking_prefix(input);
        assert_eq!(result, "# Asset Allocation");
    }

    #[test]
    fn strip_thinking_prefix_bare_final_with_json() {
        let input = "final{\"solution\": \"test\"}";
        let result = strip_thinking_prefix(input);
        assert_eq!(result, "{\"solution\": \"test\"}");
    }

    #[test]
    fn strip_thinking_prefix_commentary_json_with_space() {
        let input = "commentaryto=functions.submit json {\"thought_process\": \"x\"}";
        let result = strip_thinking_prefix(input);
        assert_eq!(result, "{\"thought_process\": \"x\"}");
    }

    #[test]
    fn strip_thinking_prefix_no_prefix_plain_text() {
        let input = "This is normal content with no thinking tokens.";
        let result = strip_thinking_prefix(input);
        assert_eq!(result, "This is normal content with no thinking tokens.");
    }

    #[test]
    fn strip_thinking_prefix_assistant_mid_word_no_strip() {
        // "assistant" followed by a regular letter should NOT strip
        let input = "The assistant helped me write code";
        let result = strip_thinking_prefix(input);
        // "assistant" is found, but after it is " helped" which doesn't start
        // with final/{/**/# — so the "assistant" prefix is NOT stripped.
        // "final" prefix check also doesn't match.
        assert_eq!(result, "The assistant helped me write code");
    }

    #[test]
    fn strip_thinking_prefix_empty_string() {
        let result = strip_thinking_prefix("");
        assert_eq!(result, "");
    }

    #[test]
    fn strip_thinking_prefix_only_final() {
        // "final" alone leaves empty string after stripping + trim
        let result = strip_thinking_prefix("final");
        assert_eq!(result, "");
    }

    #[test]
    fn strip_thinking_prefix_whitespace_trimming() {
        let input = "final   **Content with leading spaces**   ";
        let result = strip_thinking_prefix(input);
        assert_eq!(result, "**Content with leading spaces**");
    }

    #[test]
    fn strip_thinking_prefix_commentary_without_json_keyword() {
        // "commentary" prefix without "json{" or "json " — should remain
        let input = "commentarysome other text here";
        let result = strip_thinking_prefix(input);
        // The commentary branch looks for "json{" or "json " — neither is found,
        // so the commentary prefix is not stripped. Trim applies.
        assert_eq!(result, "commentarysome other text here");
    }

    // =========================================================================
    // extract_evaluation_sections — additional edge cases
    // =========================================================================

    #[test]
    fn extract_evaluation_sections_empty_input() {
        let result = extract_evaluation_sections("");
        assert_eq!(result, "", "Empty input should return empty string");
    }

    #[test]
    fn extract_evaluation_sections_empty_tags() {
        // Tags present but with empty content
        let input = "<key_findings></key_findings>\n<strategy></strategy>";
        let result = extract_evaluation_sections(input);
        assert!(result.contains("<key_findings></key_findings>"));
        assert!(result.contains("<strategy></strategy>"));
    }

    #[test]
    fn extract_evaluation_sections_nested_xml_inside_tags() {
        // XML-like content nested inside the sections
        let input = "<key_findings><item>A</item><item>B</item></key_findings>";
        let result = extract_evaluation_sections(input);
        assert_eq!(
            result,
            "<key_findings><item>A</item><item>B</item></key_findings>"
        );
    }

    #[test]
    fn extract_evaluation_sections_whitespace_only_input() {
        let input = "   \n\n   \t  ";
        let result = extract_evaluation_sections(input);
        // No sections found, falls back to full text
        assert_eq!(result, input);
    }

    #[test]
    fn extract_evaluation_sections_duplicate_key_findings() {
        // Only the first occurrence of each tag pair is extracted
        let input =
            "<key_findings>first</key_findings>\nsome gap\n<key_findings>second</key_findings>";
        let result = extract_evaluation_sections(input);
        // The function uses find() which returns the first match
        assert!(result.contains("first"));
    }

    #[test]
    fn extract_evaluation_sections_sections_with_surrounding_junk() {
        let input = "IGNORE THIS PREAMBLE\n\n<key_findings>Core insight: volatility is rising</key_findings>\n\nMORE JUNK\n\n<strategy>Hedge with puts</strategy>\n\nTRAILING GARBAGE";
        let result = extract_evaluation_sections(input);
        assert!(result.contains("<key_findings>Core insight: volatility is rising</key_findings>"));
        assert!(result.contains("<strategy>Hedge with puts</strategy>"));
        assert!(!result.contains("IGNORE THIS PREAMBLE"));
        assert!(!result.contains("MORE JUNK"));
        assert!(!result.contains("TRAILING GARBAGE"));
    }

    #[test]
    fn extract_evaluation_sections_strategy_before_key_findings() {
        // Order in the output: key_findings first, then strategy (regardless of input order)
        let input = "<strategy>plan first</strategy>\n<key_findings>findings second</key_findings>";
        let result = extract_evaluation_sections(input);
        assert!(result.contains("<key_findings>findings second</key_findings>"));
        assert!(result.contains("<strategy>plan first</strategy>"));
        // key_findings should come before strategy in the joined output
        let kf_pos = result.find("<key_findings>").unwrap();
        let st_pos = result.find("<strategy>").unwrap();
        assert!(
            kf_pos < st_pos,
            "key_findings should appear before strategy in output"
        );
    }

    // =========================================================================
    // prune_failure_dirs — filesystem tests with temp directories
    // =========================================================================

    #[test]
    fn prune_failure_dirs_no_op_under_limit() {
        use super::prune_failure_dirs;
        let tmp = std::env::temp_dir().join(format!("nsed_prune_test_noop_{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&tmp);
        std::fs::create_dir_all(&tmp).unwrap();

        // Create 3 directories, max is 5 — no pruning should happen
        for i in 0..3 {
            std::fs::create_dir_all(tmp.join(format!("dir_{i}"))).unwrap();
        }

        prune_failure_dirs(tmp.to_str().unwrap(), 5);

        let remaining: Vec<_> = std::fs::read_dir(&tmp)
            .unwrap()
            .filter_map(|e| e.ok())
            .filter(|e| e.file_type().map(|ft| ft.is_dir()).unwrap_or(false))
            .collect();
        assert_eq!(
            remaining.len(),
            3,
            "All 3 dirs should remain when under limit"
        );

        let _ = std::fs::remove_dir_all(&tmp);
    }

    #[test]
    fn prune_failure_dirs_removes_oldest_when_over_limit() {
        use super::prune_failure_dirs;
        let tmp = std::env::temp_dir().join(format!("nsed_prune_test_over_{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&tmp);
        std::fs::create_dir_all(&tmp).unwrap();

        // Create 5 directories
        for i in 0..5 {
            let dir = tmp.join(format!("dir_{i}"));
            std::fs::create_dir_all(&dir).unwrap();
            // Write a file so the directory is non-empty
            std::fs::write(dir.join("marker.txt"), format!("dir {i}")).unwrap();
            // Small sleep to ensure distinct mtimes on filesystems with coarse timestamps
            std::thread::sleep(std::time::Duration::from_millis(50));
        }

        // Prune to max 2 — should remove 3 oldest
        prune_failure_dirs(tmp.to_str().unwrap(), 2);

        let remaining: Vec<_> = std::fs::read_dir(&tmp)
            .unwrap()
            .filter_map(|e| e.ok())
            .filter(|e| e.file_type().map(|ft| ft.is_dir()).unwrap_or(false))
            .collect();
        assert_eq!(
            remaining.len(),
            2,
            "Should have 2 dirs remaining after pruning from 5"
        );

        let _ = std::fs::remove_dir_all(&tmp);
    }

    #[test]
    fn prune_failure_dirs_nonexistent_parent() {
        use super::prune_failure_dirs;
        // Should not panic when the parent directory doesn't exist
        prune_failure_dirs("/tmp/nsed_nonexistent_prune_dir_12345", 5);
    }

    #[test]
    fn prune_failure_dirs_exact_limit() {
        use super::prune_failure_dirs;
        let tmp =
            std::env::temp_dir().join(format!("nsed_prune_test_exact_{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&tmp);
        std::fs::create_dir_all(&tmp).unwrap();

        // Create exactly 3 directories, max is 3 — no pruning
        for i in 0..3 {
            std::fs::create_dir_all(tmp.join(format!("dir_{i}"))).unwrap();
        }

        prune_failure_dirs(tmp.to_str().unwrap(), 3);

        let remaining: Vec<_> = std::fs::read_dir(&tmp)
            .unwrap()
            .filter_map(|e| e.ok())
            .filter(|e| e.file_type().map(|ft| ft.is_dir()).unwrap_or(false))
            .collect();
        assert_eq!(remaining.len(), 3, "Exactly at limit — no pruning");

        let _ = std::fs::remove_dir_all(&tmp);
    }

    #[test]
    fn prune_failure_dirs_max_zero_removes_all() {
        use super::prune_failure_dirs;
        let tmp = std::env::temp_dir().join(format!("nsed_prune_test_zero_{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&tmp);
        std::fs::create_dir_all(&tmp).unwrap();

        for i in 0..3 {
            std::fs::create_dir_all(tmp.join(format!("dir_{i}"))).unwrap();
        }

        prune_failure_dirs(tmp.to_str().unwrap(), 0);

        let remaining: Vec<_> = std::fs::read_dir(&tmp)
            .unwrap()
            .filter_map(|e| e.ok())
            .filter(|e| e.file_type().map(|ft| ft.is_dir()).unwrap_or(false))
            .collect();
        assert_eq!(remaining.len(), 0, "Max 0 should remove all directories");

        let _ = std::fs::remove_dir_all(&tmp);
    }

    #[test]
    fn prune_failure_dirs_ignores_files() {
        use super::prune_failure_dirs;
        let tmp =
            std::env::temp_dir().join(format!("nsed_prune_test_files_{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&tmp);
        std::fs::create_dir_all(&tmp).unwrap();

        // Create 3 directories and 2 regular files
        for i in 0..3 {
            std::fs::create_dir_all(tmp.join(format!("dir_{i}"))).unwrap();
        }
        std::fs::write(tmp.join("file_a.txt"), "a").unwrap();
        std::fs::write(tmp.join("file_b.txt"), "b").unwrap();

        prune_failure_dirs(tmp.to_str().unwrap(), 1);

        // Only directories should be counted/pruned, not files
        let remaining_dirs: Vec<_> = std::fs::read_dir(&tmp)
            .unwrap()
            .filter_map(|e| e.ok())
            .filter(|e| e.file_type().map(|ft| ft.is_dir()).unwrap_or(false))
            .collect();
        assert_eq!(remaining_dirs.len(), 1, "Should prune down to 1 directory");

        // Files should be untouched
        assert!(tmp.join("file_a.txt").exists());
        assert!(tmp.join("file_b.txt").exists());

        let _ = std::fs::remove_dir_all(&tmp);
    }

    // =========================================================================
    // write_failure_dump — full integration test with temp directory
    // =========================================================================

    #[test]
    #[serial]
    fn write_failure_dump_creates_file_with_correct_format() {
        // write_failure_dump creates "failures/<job_dir>" relative to CWD.
        // We test with config "1" and session_id to verify the format.
        let result = write_failure_dump(FailureDumpParams {
            kind: "parse_error",
            agent_name: "test-agent",
            model_name: "gpt-4",
            provider_id: "openai",
            error: "JSON parse failed: unexpected token",
            session_id: Some("abc12345"),
            phase: Some("propose"),
            round: Some(2),
            attempt: Some(3),
            finish_reason: Some("stop"),
            input_tokens: Some(500),
            output_tokens: Some(200),
            response_content: Some("This is the raw LLM response"),
            system_prompt: None,
            request_body: None,
            messages: None,
            failure_dumps_config: Some("1"),
        });

        // Should have created a file
        assert!(result.is_some(), "Dump should be created with config '1'");
        let filename = result.unwrap();
        assert!(
            filename.contains("abc12345"),
            "Filename should contain session ID prefix"
        );
        assert!(
            filename.contains("parse_error_r2.md"),
            "Filename should contain kind and round"
        );

        // Read and verify content
        let content = std::fs::read_to_string(&filename).unwrap();
        assert!(
            content.contains("# parse_error (attempt 3)"),
            "Should contain header"
        );
        assert!(
            content.contains("| agent | test-agent |"),
            "Should contain agent name"
        );
        assert!(
            content.contains("| model | gpt-4 |"),
            "Should contain model name"
        );
        assert!(
            content.contains("| phase | propose |"),
            "Should contain phase"
        );
        assert!(content.contains("| round | 2 |"), "Should contain round");
        assert!(content.contains("## Error"), "Should contain error section");
        assert!(
            content.contains("JSON parse failed"),
            "Should contain error text"
        );
        assert!(
            content.contains("## LLM Response"),
            "Should contain response section"
        );
        assert!(
            content.contains("This is the raw LLM response"),
            "Should contain response content"
        );
        // Without "full" mode, should NOT contain system prompt section
        assert!(
            content.contains("NSED_FAILURE_DUMPS=full"),
            "Should contain hint about full mode"
        );

        // Cleanup
        let _ = std::fs::remove_dir_all("failures");
    }

    #[test]
    #[serial]
    fn write_failure_dump_full_mode_includes_system_prompt() {
        let result = write_failure_dump(FailureDumpParams {
            kind: "api_error",
            agent_name: "full-test-agent",
            model_name: "model",
            provider_id: "provider",
            error: "timeout",
            session_id: Some("fulltest1"),
            phase: None,
            round: Some(1),
            attempt: None,
            finish_reason: None,
            input_tokens: None,
            output_tokens: None,
            response_content: None,
            system_prompt: Some("You are a helpful assistant."),
            request_body: Some("{\"model\": \"test\"}"),
            messages: None,
            failure_dumps_config: Some("full"),
        });

        assert!(result.is_some(), "Full mode dump should be created");
        let filename = result.unwrap();
        let content = std::fs::read_to_string(&filename).unwrap();
        assert!(
            content.contains("## System Prompt"),
            "Full mode should include system prompt"
        );
        assert!(content.contains("You are a helpful assistant."));
        assert!(
            content.contains("## Request Body"),
            "Full mode should include request body"
        );

        // Cleanup
        let _ = std::fs::remove_dir_all("failures");
    }

    #[test]
    fn write_failure_dump_invalid_mode_returns_none() {
        let result = write_failure_dump(FailureDumpParams {
            kind: "test",
            agent_name: "agent",
            model_name: "model",
            provider_id: "prov",
            error: "err",
            session_id: None,
            phase: None,
            round: None,
            attempt: None,
            finish_reason: None,
            input_tokens: None,
            output_tokens: None,
            response_content: None,
            system_prompt: None,
            request_body: None,
            messages: None,
            failure_dumps_config: Some("maybe"), // Not "1", "on", or "full"
        });
        assert!(result.is_none(), "Invalid mode 'maybe' should return None");
    }

    // =========================================================================
    // truncate_for_dump — additional edge cases
    // =========================================================================

    #[test]
    fn test_truncate_for_dump_empty() {
        use super::truncate_for_dump;
        assert_eq!(truncate_for_dump("", 100), "");
    }

    #[test]
    fn test_truncate_for_dump_unicode() {
        use super::truncate_for_dump;
        // Unicode string: each char may be multi-byte but we count chars not bytes
        let input = "日本語テストデータ"; // 9 chars
        let result = truncate_for_dump(input, 5);
        assert!(result.contains("... (truncated at 5 chars)"));
        // Should contain exactly 5 chars from the original
        let prefix = result.split("\n... (truncated at 5 chars)").next().unwrap();
        assert_eq!(prefix.chars().count(), 5);
        assert_eq!(prefix, "日本語テス");
    }

    #[test]
    fn test_truncate_for_dump_one_char_over() {
        use super::truncate_for_dump;
        let input = "abcdef"; // 6 chars
        let result = truncate_for_dump(input, 5);
        assert!(result.contains("... (truncated at 5 chars)"));
    }

    // =========================================================================
    // strip_thinking_prefix — additional branch coverage
    // =========================================================================

    #[test]
    fn strip_thinking_prefix_commentary_with_json_brace() {
        // "commentary" followed by "json{" path
        let input = "commentaryto=functions.submit_proposal json{\"key\": 1}";
        let result = strip_thinking_prefix(input);
        assert_eq!(result, "{\"key\": 1}");
    }

    #[test]
    fn strip_thinking_prefix_assistant_followed_by_final_then_json() {
        let input = "random tokensassistantfinal{\"data\": true}";
        let result = strip_thinking_prefix(input);
        assert_eq!(result, "{\"data\": true}");
    }

    // =========================================================================
    // Transport error detection — validate retry eligibility
    // =========================================================================

    /// Helper that mirrors the production classifier in
    /// `generate_structured_output`'s Err arm. Tests use this to drive
    /// the same downcast path the retry loop uses, so renaming or
    /// reshaping `LlmError::Transport` is caught at compile time.
    fn is_transport_anyhow(err: &anyhow::Error) -> bool {
        use crate::telemetry::LlmError;
        matches!(err.downcast_ref::<LlmError>(), Some(LlmError::Transport(_)))
    }

    /// `LlmError::Transport` flowing up through `anyhow::Error::from`
    /// must classify as retryable on the inner error path. A bare
    /// `anyhow!("plain string")` (no LlmError chain) must not.
    #[test]
    fn transport_error_detection_covers_known_patterns() {
        use crate::telemetry::LlmError;
        let causes: Vec<Box<dyn std::error::Error + Send + Sync>> = vec![
            Box::new(std::io::Error::new(
                std::io::ErrorKind::UnexpectedEof,
                "unexpected EOF during chunk size line",
            )),
            Box::new(std::io::Error::new(
                std::io::ErrorKind::ConnectionReset,
                "connection reset by peer",
            )),
            Box::new(std::io::Error::new(
                std::io::ErrorKind::BrokenPipe,
                "broken pipe",
            )),
            Box::new(std::io::Error::new(
                std::io::ErrorKind::TimedOut,
                "request timed out after 30s",
            )),
        ];
        for cause in causes {
            let display = cause.to_string();
            let err: anyhow::Error = LlmError::Transport(cause).into();
            assert!(
                is_transport_anyhow(&err),
                "LlmError::Transport({display:?}) must classify as retryable transport"
            );
        }
    }

    /// Non-transport `LlmError` variants and bare `anyhow` errors must
    /// not classify as retryable transport.
    #[test]
    fn non_transport_errors_not_retryable() {
        use crate::telemetry::LlmError;
        let non_transport: Vec<anyhow::Error> = vec![
            // Other LlmError variants
            LlmError::ServerError { status: 500 }.into(),
            LlmError::PaymentRequired { status: 402 }.into(),
            LlmError::ContextOverflow {
                tokens: 5000,
                limit: 4096,
            }
            .into(),
            LlmError::Parse(Box::new(std::io::Error::other("parse failed"))).into(),
            LlmError::RateLimit {
                retry_after_ms: None,
                status: 429,
            }
            .into(),
            // Plain anyhow chain — no LlmError in the source path
            anyhow::anyhow!("Failed to parse structured output after 4 attempts"),
            anyhow::anyhow!("No choice in LLM response"),
        ];
        for err in non_transport {
            assert!(
                !is_transport_anyhow(&err),
                "Error should NOT classify as retryable transport: {err:#}"
            );
        }
    }

    /// Empty response parsing should fail with EOF error — this is the exact
    /// error that gpt-oss-120b triggers when it returns empty content with
    /// finish_reason=Stop (reasoning tokens consumed, no visible output).
    #[test]
    fn empty_response_triggers_eof_parse_error() {
        let result = serde_json::from_str::<StructuredProposalResponse>("");
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(
            err.is_eof(),
            "Empty string should trigger EOF error, got: {}",
            err
        );
    }

    /// Empty response should also fail for batch evaluation parsing.
    #[test]
    fn empty_response_triggers_eof_parse_error_evaluation() {
        let result = serde_json::from_str::<StructuredBatchEvaluationResponse>("");
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(
            err.is_eof(),
            "Empty string should trigger EOF error for evaluation, got: {}",
            err
        );
    }

    /// Verify the empty-response retry message tells the model to USE the tool.
    /// This is the specific feedback sent when gpt-oss-120b returns empty content.
    #[test]
    fn empty_response_retry_message_is_actionable() {
        let cleaned_json = "";
        let error_msg = if cleaned_json.trim().is_empty() {
            "Your response was EMPTY — no content was returned. You MUST call the tool with a JSON argument. Do NOT just think about the answer — you must actually output the tool call with the required fields.".to_string()
        } else {
            "other".to_string()
        };
        assert!(
            error_msg.contains("EMPTY"),
            "Error message should clearly indicate empty response"
        );
        assert!(
            error_msg.contains("MUST call the tool"),
            "Error message should instruct the model to use the tool"
        );
    }

    // =========================================================================
    // deserialize_string_or_array_or_object — float value
    // =========================================================================

    #[test]
    fn deser_string_or_array_float_value() {
        let val = 7.89_f64;
        let json = serde_json::json!({
            "thought_process": val,
            "solution_content": "x"
        });
        let result: StructuredProposalResponse = serde_json::from_value(json).unwrap();
        assert_eq!(result.thought_process, "7.89");
    }

    #[test]
    fn deser_string_or_array_array_with_numbers() {
        // Array containing non-string values should be converted via to_string()
        let json = serde_json::json!({
            "thought_process": [1, 2.5, "text"],
            "solution_content": "y"
        });
        let result: StructuredProposalResponse = serde_json::from_value(json).unwrap();
        let parts: Vec<&str> = result.thought_process.split('\n').collect();
        assert_eq!(parts[0], "1");
        assert_eq!(parts[1], "2.5");
        assert_eq!(parts[2], "text");
    }

    // =========================================================================
    // truncate_for_dump — boundary and edge cases
    // =========================================================================

    #[test]
    fn truncate_for_dump_exact_limit() {
        use super::truncate_for_dump;
        // String with exactly max_chars — no truncation
        let input = "abcde"; // 5 chars
        let result = truncate_for_dump(input, 5);
        assert_eq!(result, "abcde");
        assert!(!result.contains("truncated"));
    }

    #[test]
    fn truncate_for_dump_max_one_char() {
        use super::truncate_for_dump;
        let result = truncate_for_dump("hello", 1);
        assert!(result.starts_with('h'));
        assert!(result.contains("... (truncated at 1 chars)"));
    }

    #[test]
    fn truncate_for_dump_max_zero() {
        use super::truncate_for_dump;
        // max_chars = 0 should produce an empty prefix with truncation note
        let result = truncate_for_dump("hello", 0);
        assert!(result.contains("... (truncated at 0 chars)"));
    }

    #[test]
    fn truncate_for_dump_multibyte_boundary() {
        use super::truncate_for_dump;
        // 4 emoji chars, each is 4 bytes
        let input = "\u{1F600}\u{1F601}\u{1F602}\u{1F603}"; // 4 chars
        let result = truncate_for_dump(input, 2);
        assert!(result.contains("... (truncated at 2 chars)"));
        // The prefix should have exactly 2 emoji chars
        let prefix = result.split("\n... (truncated at 2 chars)").next().unwrap();
        assert_eq!(prefix.chars().count(), 2);
    }

    #[test]
    fn truncate_for_dump_newlines_preserved() {
        use super::truncate_for_dump;
        let input = "line1\nline2\nline3\nline4";
        let result = truncate_for_dump(input, 12);
        // 12 chars = "line1\nline2\n" (12 chars)
        assert!(result.contains("line1\nline2\n"));
        assert!(result.contains("... (truncated at 12 chars)"));
    }

    // =========================================================================
    // write_failure_dump — additional branch coverage
    // =========================================================================

    #[test]
    #[serial]
    fn write_failure_dump_off_config_returns_none() {
        // Explicit "off" config should short-circuit and return None
        let result = write_failure_dump(FailureDumpParams {
            kind: "test",
            agent_name: "agent",
            model_name: "model",
            provider_id: "prov",
            error: "err",
            session_id: None,
            phase: None,
            round: None,
            attempt: None,
            finish_reason: None,
            input_tokens: None,
            output_tokens: None,
            response_content: None,
            system_prompt: None,
            request_body: None,
            messages: None,
            failure_dumps_config: Some("off"),
        });
        assert!(result.is_none(), "'off' config should return None");
    }

    #[test]
    #[serial]
    fn write_failure_dump_empty_config_returns_none() {
        // Empty config string should return None
        let result = write_failure_dump(FailureDumpParams {
            kind: "test",
            agent_name: "agent",
            model_name: "model",
            provider_id: "prov",
            error: "err",
            session_id: None,
            phase: None,
            round: None,
            attempt: None,
            finish_reason: None,
            input_tokens: None,
            output_tokens: None,
            response_content: None,
            system_prompt: None,
            request_body: None,
            messages: None,
            failure_dumps_config: Some(""),
        });
        assert!(result.is_none(), "Empty config should return None");
    }

    #[test]
    #[serial]
    fn write_failure_dump_on_config_creates_file() {
        let result = write_failure_dump(FailureDumpParams {
            kind: "api_error",
            agent_name: "on-test-agent",
            model_name: "test-model",
            provider_id: "test-provider",
            error: "connection timeout",
            session_id: Some("ontest123"),
            phase: Some("evaluate"),
            round: Some(3),
            attempt: None,
            finish_reason: None,
            input_tokens: None,
            output_tokens: None,
            response_content: None,
            system_prompt: None,
            request_body: None,
            messages: None,
            failure_dumps_config: Some("on"),
        });

        assert!(result.is_some(), "'on' config should create a dump");
        let filename = result.unwrap();
        let content = std::fs::read_to_string(&filename).unwrap();
        assert!(content.contains("# api_error"));
        assert!(content.contains("| agent | on-test-agent |"));
        assert!(content.contains("| phase | evaluate |"));
        assert!(content.contains("connection timeout"));

        let _ = std::fs::remove_dir_all("failures");
    }

    #[test]
    #[serial]
    fn write_failure_dump_append_mode_adds_separator() {
        // First write
        let result1 = write_failure_dump(FailureDumpParams {
            kind: "parse_error",
            agent_name: "append-agent",
            model_name: "model",
            provider_id: "prov",
            error: "first error",
            session_id: Some("appendtst"),
            phase: None,
            round: Some(1),
            attempt: Some(1),
            finish_reason: None,
            input_tokens: None,
            output_tokens: None,
            response_content: None,
            system_prompt: None,
            request_body: None,
            messages: None,
            failure_dumps_config: Some("1"),
        });
        assert!(result1.is_some());

        // Second write to same file (same session, kind, round)
        let result2 = write_failure_dump(FailureDumpParams {
            kind: "parse_error",
            agent_name: "append-agent",
            model_name: "model",
            provider_id: "prov",
            error: "second error",
            session_id: Some("appendtst"),
            phase: None,
            round: Some(1),
            attempt: Some(2),
            finish_reason: None,
            input_tokens: None,
            output_tokens: None,
            response_content: None,
            system_prompt: None,
            request_body: None,
            messages: None,
            failure_dumps_config: Some("1"),
        });
        assert!(result2.is_some());

        let filename = result2.unwrap();
        let content = std::fs::read_to_string(&filename).unwrap();
        // Append mode should add "---" separator
        assert!(content.contains("---"), "Append should add separator");
        assert!(
            content.contains("first error"),
            "Should contain first entry"
        );
        assert!(
            content.contains("second error"),
            "Should contain second entry"
        );
        assert!(
            content.contains("(attempt 1)"),
            "First attempt label should be present"
        );
        assert!(
            content.contains("(attempt 2)"),
            "Second attempt label should be present"
        );

        let _ = std::fs::remove_dir_all("failures");
    }

    #[test]
    #[serial]
    fn write_failure_dump_no_session_id_uses_timestamp_dir() {
        let result = write_failure_dump(FailureDumpParams {
            kind: "api_error",
            agent_name: "no-session-agent",
            model_name: "model",
            provider_id: "prov",
            error: "err",
            session_id: None, // No session ID
            phase: None,
            round: None,
            attempt: None,
            finish_reason: None,
            input_tokens: None,
            output_tokens: None,
            response_content: None,
            system_prompt: None,
            request_body: None,
            messages: None,
            failure_dumps_config: Some("1"),
        });
        assert!(result.is_some());
        let filename = result.unwrap();
        // Without session_id, directory name uses timestamp_agentname pattern
        assert!(filename.contains("no-session-agent"));
        // Round defaults to 0
        assert!(filename.contains("_r0.md"));

        let _ = std::fs::remove_dir_all("failures");
    }

    #[test]
    #[serial]
    fn write_failure_dump_full_mode_with_messages() {
        use async_openai::types::{ChatCompletionRequestMessage, ChatCompletionRequestUserMessage};

        let user_msg =
            ChatCompletionRequestMessage::User(ChatCompletionRequestUserMessage::from("Hello!"));
        let messages = vec![user_msg];

        let result = write_failure_dump(FailureDumpParams {
            kind: "parse_error",
            agent_name: "msg-agent",
            model_name: "gpt-4",
            provider_id: "openai",
            error: "parse failed",
            session_id: Some("msgtest12"),
            phase: None,
            round: Some(1),
            attempt: None,
            finish_reason: Some("length"),
            input_tokens: Some(1000),
            output_tokens: Some(500),
            response_content: Some("partial response"),
            system_prompt: Some("You are helpful."),
            request_body: Some("{\"model\": \"gpt-4\"}"),
            messages: Some(&messages),
            failure_dumps_config: Some("full"),
        });
        assert!(result.is_some());
        let filename = result.unwrap();
        let content = std::fs::read_to_string(&filename).unwrap();

        // Full mode: system prompt, request body, AND messages
        assert!(content.contains("## System Prompt"));
        assert!(content.contains("You are helpful."));
        assert!(content.contains("## Request Body"));
        assert!(content.contains("## Messages"));
        // Metadata
        assert!(content.contains("| finish_reason | length |"));
        assert!(content.contains("| input_tokens | 1000 |"));
        assert!(content.contains("| output_tokens | 500 |"));
        // LLM Response section
        assert!(content.contains("## LLM Response"));
        assert!(content.contains("partial response"));

        let _ = std::fs::remove_dir_all("failures");
    }

    #[test]
    #[serial]
    fn write_failure_dump_full_mode_append_skips_context() {
        // First write in full mode — should include system prompt
        let _r1 = write_failure_dump(FailureDumpParams {
            kind: "parse_error",
            agent_name: "ctx-agent",
            model_name: "model",
            provider_id: "prov",
            error: "first",
            session_id: Some("ctxtest12"),
            phase: None,
            round: Some(1),
            attempt: Some(1),
            finish_reason: None,
            input_tokens: None,
            output_tokens: None,
            response_content: None,
            system_prompt: Some("System prompt content here"),
            request_body: Some("{\"body\": true}"),
            messages: None,
            failure_dumps_config: Some("full"),
        });

        // Second write (append) — should NOT repeat system prompt/request body
        let r2 = write_failure_dump(FailureDumpParams {
            kind: "parse_error",
            agent_name: "ctx-agent",
            model_name: "model",
            provider_id: "prov",
            error: "second",
            session_id: Some("ctxtest12"),
            phase: None,
            round: Some(1),
            attempt: Some(2),
            finish_reason: None,
            input_tokens: None,
            output_tokens: None,
            response_content: None,
            system_prompt: Some("System prompt content here"),
            request_body: Some("{\"body\": true}"),
            messages: None,
            failure_dumps_config: Some("full"),
        });

        let filename = r2.unwrap();
        let content = std::fs::read_to_string(&filename).unwrap();

        // System prompt should appear exactly once (from first write only)
        let sys_count = content.matches("## System Prompt").count();
        assert_eq!(
            sys_count, 1,
            "System Prompt should appear only once (first write), found {}",
            sys_count
        );

        let _ = std::fs::remove_dir_all("failures");
    }

    #[test]
    #[serial]
    fn write_failure_dump_special_chars_in_agent_name() {
        let result = write_failure_dump(FailureDumpParams {
            kind: "test",
            agent_name: "agent/with (special) chars\\here",
            model_name: "model",
            provider_id: "prov",
            error: "err",
            session_id: Some("special1"),
            phase: None,
            round: Some(1),
            attempt: None,
            finish_reason: None,
            input_tokens: None,
            output_tokens: None,
            response_content: None,
            system_prompt: None,
            request_body: None,
            messages: None,
            failure_dumps_config: Some("1"),
        });
        assert!(result.is_some());
        let filename = result.unwrap();
        // Special chars should be sanitized to underscores
        assert!(
            !filename.contains('/') || filename.starts_with("failures/"),
            "Agent name special chars should be sanitized"
        );
        assert!(!filename.contains('('));
        assert!(!filename.contains(')'));

        let _ = std::fs::remove_dir_all("failures");
    }

    #[test]
    #[serial]
    fn write_failure_dump_response_truncation() {
        // Verify that long response content is truncated at 4000 chars
        let long_response = "X".repeat(5000);
        let result = write_failure_dump(FailureDumpParams {
            kind: "parse_error",
            agent_name: "trunc-agent",
            model_name: "model",
            provider_id: "prov",
            error: "parse error",
            session_id: Some("trunctest"),
            phase: None,
            round: Some(1),
            attempt: None,
            finish_reason: None,
            input_tokens: None,
            output_tokens: None,
            response_content: Some(&long_response),
            system_prompt: None,
            request_body: None,
            messages: None,
            failure_dumps_config: Some("1"),
        });
        assert!(result.is_some());
        let filename = result.unwrap();
        let content = std::fs::read_to_string(&filename).unwrap();
        // Response should be truncated
        assert!(content.contains("truncated at 4000 chars"));
        // The full 5000-char response should NOT appear
        assert!(!content.contains(&long_response));

        let _ = std::fs::remove_dir_all("failures");
    }

    // =========================================================================
    // prune_failure_dirs — edge case: mixed files and empty subdirs
    // =========================================================================

    #[test]
    fn prune_failure_dirs_with_nested_content() {
        use super::prune_failure_dirs;
        let tmp =
            std::env::temp_dir().join(format!("nsed_prune_test_nested_{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&tmp);
        std::fs::create_dir_all(&tmp).unwrap();

        // Create 4 directories, each with files inside
        for i in 0..4 {
            let dir = tmp.join(format!("job_{i}"));
            std::fs::create_dir_all(&dir).unwrap();
            std::fs::write(dir.join("parse_error_r1.md"), format!("error {i}")).unwrap();
            // Stagger mtimes slightly
            std::thread::sleep(std::time::Duration::from_millis(50));
        }

        prune_failure_dirs(tmp.to_str().unwrap(), 2);

        let remaining: Vec<_> = std::fs::read_dir(&tmp)
            .unwrap()
            .filter_map(|e| e.ok())
            .filter(|e| e.file_type().map(|ft| ft.is_dir()).unwrap_or(false))
            .collect();
        assert_eq!(remaining.len(), 2, "Should prune down to 2 directories");

        let _ = std::fs::remove_dir_all(&tmp);
    }

    // =========================================================================
    // StructuredProposalResponse — additional deserialization edge cases
    // =========================================================================

    #[test]
    fn deser_proposal_object_solution_content() {
        // When solution_content is an object (not a string), it should be serialized
        let json = serde_json::json!({
            "thought_process": "thinking",
            "solution_content": {"code": "fn main() {}", "language": "rust"}
        });
        let result: StructuredProposalResponse = serde_json::from_value(json).unwrap();
        assert!(result.solution_content.contains("code"));
        assert!(result.solution_content.contains("fn main()"));
    }

    #[test]
    fn deser_proposal_array_solution_content() {
        let json = serde_json::json!({
            "thought_process": "thinking",
            "solution_content": ["Part 1", "Part 2", "Part 3"]
        });
        let result: StructuredProposalResponse = serde_json::from_value(json).unwrap();
        assert!(result.solution_content.contains("Part 1"));
        assert!(result.solution_content.contains("Part 2"));
        assert!(result.solution_content.contains("Part 3"));
        // Parts should be joined with newlines
        let parts: Vec<&str> = result.solution_content.split('\n').collect();
        assert_eq!(parts.len(), 3);
    }

    // =========================================================================
    // StructuredBatchEvaluationResponse — alias coverage
    // =========================================================================

    #[test]
    fn deser_batch_eval_candidate_evaluations_alias() {
        // "candidate_evaluations" alias should work
        let json = serde_json::json!({
            "candidate_evaluations": [
                {"agent_id": "a1", "endorsement_weight": 80.0}
            ]
        });
        let result: StructuredBatchEvaluationResponse = serde_json::from_value(json).unwrap();
        assert_eq!(result.evaluations.len(), 1);
        assert_eq!(result.evaluations[0].agent_id, "a1");
        assert_eq!(result.evaluations[0].endorsement_weight, 80.0);
    }

    #[test]
    fn deser_batch_eval_candidates_alias() {
        // "candidates" alias should work
        let json = serde_json::json!({
            "candidates": [
                {"candidate_id": "c1", "score": 65.0, "justification": "Good work"}
            ]
        });
        let result: StructuredBatchEvaluationResponse = serde_json::from_value(json).unwrap();
        assert_eq!(result.evaluations.len(), 1);
        assert_eq!(result.evaluations[0].agent_id, "c1");
        assert_eq!(result.evaluations[0].endorsement_weight, 65.0);
        assert_eq!(
            result.evaluations[0].justification,
            Some("Good work".to_string())
        );
    }

    #[test]
    fn deser_batch_eval_with_all_optional_fields() {
        let json = serde_json::json!({
            "evaluations": [{
                "agent_id": "agent_1",
                "endorsement_weight": 75.0,
                "justification": "Solid approach",
                "is_final_solution": true,
                "stance": "agree",
                "claim_assessments": [{
                    "claim": "Memory safety is guaranteed",
                    "verdict": "verified"
                }],
                "disagreements": [{
                    "what_they_claim": "Always faster",
                    "what_you_believe": "Not always",
                    "confidence": "high"
                }],
                "category_scores": {
                    "correctness": 80.0,
                    "completeness": 70.0,
                    "novelty": 60.0,
                    "feasibility": 90.0,
                    "evidence_quality": 75.0
                }
            }]
        });
        let result: StructuredBatchEvaluationResponse = serde_json::from_value(json).unwrap();
        assert_eq!(result.evaluations.len(), 1);
        assert!(result.evaluations[0].is_final_solution);
        assert!(result.evaluations[0].category_scores.is_some());
    }

    // =========================================================================
    // strip_thinking_prefix — additional branches
    // =========================================================================

    #[test]
    fn strip_thinking_prefix_commentary_with_json_space() {
        // "commentary...json {..." path
        let input = "commentaryto=functions.submit_proposal json {\"key\": 1}";
        let result = strip_thinking_prefix(input);
        assert_eq!(result, "{\"key\": 1}");
    }

    #[test]
    fn strip_thinking_prefix_commentary_no_json_unchanged() {
        // "commentary" prefix without "json" keyword — falls through, returns trimmed
        let input = "commentary some random text without a special keyword";
        let result = strip_thinking_prefix(input);
        // No "json{" or "json " found, so the full string (trimmed) is returned
        assert_eq!(result, input);
    }

    #[test]
    fn strip_thinking_prefix_assistant_then_hash() {
        // "assistant" followed by '#' should strip
        let input = "analysis tokens here assistant# Solution Header";
        let result = strip_thinking_prefix(input);
        assert_eq!(result, "# Solution Header");
    }

    // =========================================================================
    // strip_scratchpad — additional edge cases for coverage
    // =========================================================================

    #[test]
    fn strip_scratchpad_only_text_before() {
        // Text only before scratchpad — should trim trailing whitespace from before
        let input = "Important preamble text\n<scratchpad>scratch content</scratchpad>";
        let result = strip_scratchpad(input);
        assert_eq!(result, "Important preamble text");
        assert!(!result.contains("scratch content"));
    }

    #[test]
    fn strip_scratchpad_only_text_after() {
        // No text before, only text after
        let input = "<scratchpad>notes</scratchpad>\nAfter the scratchpad";
        let result = strip_scratchpad(input);
        assert_eq!(result, "After the scratchpad");
    }

    #[test]
    fn strip_scratchpad_whitespace_between() {
        // Whitespace around scratchpad block — should be trimmed
        let input = "Before   \n\n   <scratchpad>internal</scratchpad>   \n\n   After";
        let result = strip_scratchpad(input);
        assert_eq!(result, "Before\n\nAfter");
    }

    #[test]
    fn strip_scratchpad_empty_tags() {
        let input = "Text before <scratchpad></scratchpad> text after";
        let result = strip_scratchpad(input);
        assert_eq!(result, "Text before\n\ntext after");
    }

    #[test]
    fn strip_scratchpad_closing_tag_only() {
        // Closing tag without opening tag — no match, returns as-is
        let input = "</scratchpad>Content after closing tag";
        let result = strip_scratchpad(input);
        assert_eq!(result, input);
    }

    // =========================================================================
    // strip_working_memory — additional edge cases
    // =========================================================================

    #[test]
    fn strip_working_memory_empty_tags() {
        let input = "Before <working_memory></working_memory> After";
        let result = strip_working_memory(input);
        assert_eq!(result, "Before\n\nAfter");
    }

    #[test]
    fn strip_working_memory_only_working_memory() {
        let input = "<working_memory>all content is ephemeral</working_memory>";
        let result = strip_working_memory(input);
        assert_eq!(result, "");
    }

    #[test]
    fn strip_working_memory_three_blocks() {
        let input = "<working_memory>a</working_memory>\n\ntext1\n\n<working_memory>b</working_memory>\n\ntext2\n\n<working_memory>c</working_memory>";
        let result = strip_working_memory(input);
        assert!(!result.contains("<working_memory>"));
        assert!(result.contains("text1"));
        assert!(result.contains("text2"));
    }

    #[test]
    fn strip_working_memory_with_whitespace_only_content() {
        let input = "<working_memory>   \n\t  </working_memory>";
        let result = strip_working_memory(input);
        assert_eq!(result, "");
    }

    // =========================================================================
    // extract_evaluation_sections — additional edge cases
    // =========================================================================

    #[test]
    fn extract_evaluation_sections_one_closed_one_unclosed() {
        // key_findings is properly closed, strategy is unclosed
        let input = "<key_findings>data here</key_findings>\n<strategy>unclosed";
        let result = extract_evaluation_sections(input);
        // Only key_findings should be extracted (strategy is unclosed, so not captured)
        assert_eq!(result, "<key_findings>data here</key_findings>");
        assert!(!result.contains("unclosed"));
    }

    #[test]
    fn extract_evaluation_sections_only_closing_tags() {
        // Closing tags without opening tags — no sections found
        let input = "</key_findings></strategy>some text";
        let result = extract_evaluation_sections(input);
        assert_eq!(result, input, "Should fall back to full text");
    }

    #[test]
    fn extract_evaluation_sections_interleaved_with_working_memory() {
        // Test realistic scenario with all three section types
        let input = "<key_findings>Important findings</key_findings>\n<working_memory>ephemeral</working_memory>\n<strategy>Long-term plan</strategy>";
        let result = extract_evaluation_sections(input);
        assert!(result.contains("<key_findings>Important findings</key_findings>"));
        assert!(result.contains("<strategy>Long-term plan</strategy>"));
        // working_memory should NOT appear in extracted sections
        assert!(!result.contains("working_memory"));
        assert!(!result.contains("ephemeral"));
    }

    // =========================================================================
    // strip_thinking_prefix — remaining uncovered branches
    // =========================================================================

    #[test]
    fn strip_thinking_prefix_commentary_only() {
        // "commentary" without any "json" keyword at all
        let input = "commentary";
        let result = strip_thinking_prefix(input);
        assert_eq!(result, "commentary");
    }

    #[test]
    fn strip_thinking_prefix_assistant_mid_content_not_at_start() {
        // "assistant" found but followed by something that doesn't match the pattern
        let input = "My assistant model works well";
        let result = strip_thinking_prefix(input);
        assert_eq!(result, "My assistant model works well");
    }

    #[test]
    fn strip_thinking_prefix_final_with_leading_whitespace() {
        let input = "  final  **Bold**  ";
        let result = strip_thinking_prefix(input);
        // Leading whitespace is trimmed first, then "final" prefix is detected
        // and stripped, then trailing trim produces the final result.
        assert_eq!(result, "**Bold**");
    }

    #[test]
    fn strip_thinking_prefix_multiple_assistant_occurrences() {
        // find() returns the FIRST occurrence of "assistant"
        let input = "analysisassistantfinal**Content**assistantmore";
        let result = strip_thinking_prefix(input);
        // First "assistant" is found, followed by "final" → strip to "final**Content**assistantmore"
        // Then "final" prefix is stripped → "**Content**assistantmore"
        assert_eq!(result, "**Content**assistantmore");
    }

    // =========================================================================
    // prune_failure_dirs — single directory
    // =========================================================================

    #[test]
    fn prune_failure_dirs_single_dir_over_limit() {
        use super::prune_failure_dirs;
        let tmp =
            std::env::temp_dir().join(format!("nsed_prune_test_single_{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&tmp);
        std::fs::create_dir_all(&tmp).unwrap();

        std::fs::create_dir_all(tmp.join("only_dir")).unwrap();

        // Max 0 means remove all
        prune_failure_dirs(tmp.to_str().unwrap(), 0);

        let remaining: Vec<_> = std::fs::read_dir(&tmp)
            .unwrap()
            .filter_map(|e| e.ok())
            .filter(|e| e.file_type().map(|ft| ft.is_dir()).unwrap_or(false))
            .collect();
        assert_eq!(
            remaining.len(),
            0,
            "Single dir should be removed when max=0"
        );

        let _ = std::fs::remove_dir_all(&tmp);
    }

    #[test]
    fn prune_failure_dirs_empty_dir() {
        use super::prune_failure_dirs;
        let tmp =
            std::env::temp_dir().join(format!("nsed_prune_test_empty_{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&tmp);
        std::fs::create_dir_all(&tmp).unwrap();

        // No directories inside — should be a no-op
        prune_failure_dirs(tmp.to_str().unwrap(), 5);
        // Just verify no panic occurred

        let _ = std::fs::remove_dir_all(&tmp);
    }

    // =========================================================================
    // deserialize_string_or_array_or_object — additional edge cases
    // =========================================================================

    #[test]
    fn deser_string_or_array_empty_string() {
        let json = serde_json::json!({
            "thought_process": "",
            "solution_content": ""
        });
        let result: StructuredProposalResponse = serde_json::from_value(json).unwrap();
        assert_eq!(result.thought_process, "");
        assert_eq!(result.solution_content, "");
    }

    #[test]
    fn deser_string_or_array_whitespace_only_string() {
        let json = serde_json::json!({
            "thought_process": "   \n\t  ",
            "solution_content": "x"
        });
        let result: StructuredProposalResponse = serde_json::from_value(json).unwrap();
        assert_eq!(result.thought_process, "   \n\t  ");
    }

    #[test]
    fn deser_string_or_array_array_of_empty_strings() {
        let json = serde_json::json!({
            "thought_process": ["", "", ""],
            "solution_content": "x"
        });
        let result: StructuredProposalResponse = serde_json::from_value(json).unwrap();
        assert_eq!(result.thought_process, "\n\n");
    }

    // =========================================================================
    // StructuredBatchEvaluationResponse — edge cases
    // =========================================================================

    #[test]
    fn batch_eval_empty_evaluations_array() {
        let json = serde_json::json!({ "evaluations": [] });
        let result: StructuredBatchEvaluationResponse = serde_json::from_value(json).unwrap();
        assert!(result.evaluations.is_empty());
    }

    #[test]
    fn batch_eval_minimal_item() {
        // Minimum required fields
        let json = serde_json::json!({
            "evaluations": [{
                "agent_id": "a",
                "endorsement_weight": 50.0
            }]
        });
        let result: StructuredBatchEvaluationResponse = serde_json::from_value(json).unwrap();
        assert_eq!(result.evaluations.len(), 1);
        assert_eq!(result.evaluations[0].agent_id, "a");
        assert_eq!(result.evaluations[0].endorsement_weight, 50.0);
        assert!(result.evaluations[0].justification.is_none());
        assert!(!result.evaluations[0].is_final_solution);
        assert!(result.evaluations[0].stance.is_none());
        assert!(result.evaluations[0].claim_assessments.is_empty());
        assert!(result.evaluations[0].disagreements.is_empty());
        assert!(result.evaluations[0].category_scores.is_none());
    }

    #[test]
    fn batch_eval_candidate_id_alias_for_agent_id() {
        let json = serde_json::json!({
            "evaluations": [{
                "candidate_id": "candidate_x",
                "endorsement_weight": 70.0
            }]
        });
        let result: StructuredBatchEvaluationResponse = serde_json::from_value(json).unwrap();
        assert_eq!(result.evaluations[0].agent_id, "candidate_x");
    }

    #[test]
    fn batch_eval_score_alias_for_endorsement_weight() {
        let json = serde_json::json!({
            "evaluations": [{
                "agent_id": "b",
                "score": 85.0
            }]
        });
        let result: StructuredBatchEvaluationResponse = serde_json::from_value(json).unwrap();
        assert_eq!(result.evaluations[0].endorsement_weight, 85.0);
    }

    // =========================================================================
    // truncate_for_dump — zero-length input
    // =========================================================================

    #[test]
    fn truncate_for_dump_max_exceeds_length() {
        use super::truncate_for_dump;
        let input = "short";
        let result = truncate_for_dump(input, 1000);
        assert_eq!(result, "short");
    }

    // =========================================================================
    // truncate_for_dump — additional edge cases
    // =========================================================================

    #[test]
    fn truncate_for_dump_empty_input() {
        use super::truncate_for_dump;
        let result = truncate_for_dump("", 100);
        assert_eq!(result, "");
    }

    #[test]
    fn truncate_for_dump_exact_length() {
        use super::truncate_for_dump;
        let input = "12345";
        let result = truncate_for_dump(input, 5);
        assert_eq!(result, "12345");
    }

    #[test]
    fn truncate_for_dump_one_over() {
        use super::truncate_for_dump;
        let input = "123456";
        let result = truncate_for_dump(input, 5);
        assert!(result.starts_with("12345"));
        assert!(result.contains("truncated at 5 chars"));
    }

    #[test]
    fn truncate_for_dump_unicode() {
        use super::truncate_for_dump;
        // Each emoji is 1 char but multiple bytes
        let input = "\u{1F600}\u{1F601}\u{1F602}\u{1F603}\u{1F604}";
        assert_eq!(input.chars().count(), 5);
        let result = truncate_for_dump(input, 3);
        assert!(result.starts_with("\u{1F600}\u{1F601}\u{1F602}"));
        assert!(result.contains("truncated at 3 chars"));
    }

    #[test]
    fn truncate_for_dump_zero_max() {
        use super::truncate_for_dump;
        let input = "something";
        let result = truncate_for_dump(input, 0);
        assert!(result.contains("truncated at 0 chars"));
        assert!(!result.starts_with('s'));
    }

    // =========================================================================
    // strip_thinking_prefix — commentary branches
    // =========================================================================

    #[test]
    fn strip_thinking_prefix_commentary_json_brace() {
        let input = r#"commentaryto=functions.submit_proposal json{"solution": "42"}"#;
        let result = strip_thinking_prefix(input);
        assert_eq!(result, r#"{"solution": "42"}"#);
    }

    #[test]
    fn strip_thinking_prefix_commentary_json_space() {
        let input = r#"commentaryto=functions.submit_proposal json {"solution": "42"}"#;
        let result = strip_thinking_prefix(input);
        assert_eq!(result, r#"{"solution": "42"}"#);
    }

    #[test]
    fn strip_thinking_prefix_final_bold() {
        let input = "final**My bold answer**";
        let result = strip_thinking_prefix(input);
        assert_eq!(result, "**My bold answer**");
    }

    #[test]
    fn strip_thinking_prefix_final_heading() {
        let input = "final# Heading";
        let result = strip_thinking_prefix(input);
        assert_eq!(result, "# Heading");
    }

    #[test]
    fn strip_thinking_prefix_assistant_final_json() {
        let input = r#"analysisThinking about itassistantfinal{"key": "val"}"#;
        let result = strip_thinking_prefix(input);
        assert_eq!(result, r#"{"key": "val"}"#);
    }

    #[test]
    fn strip_thinking_prefix_assistant_json_direct() {
        let input = r#"analysisassistant{"tool": true}"#;
        let result = strip_thinking_prefix(input);
        assert_eq!(result, r#"{"tool": true}"#);
    }

    #[test]
    fn strip_thinking_prefix_plain_text() {
        let input = "Just regular content with no prefix tokens";
        let result = strip_thinking_prefix(input);
        assert_eq!(result, input);
    }

    // =========================================================================
    // StructuredBatchEvaluationResponse — more alias & field tests
    // =========================================================================

    #[test]
    fn batch_eval_with_justification() {
        let json = serde_json::json!({
            "evaluations": [{
                "agent_id": "a1",
                "endorsement_weight": 90.0,
                "justification": "Strong argument with evidence"
            }]
        });
        let result: StructuredBatchEvaluationResponse = serde_json::from_value(json).unwrap();
        assert_eq!(
            result.evaluations[0].justification.as_deref(),
            Some("Strong argument with evidence")
        );
    }

    #[test]
    fn batch_eval_with_is_final_solution_true() {
        let json = serde_json::json!({
            "evaluations": [{
                "agent_id": "a1",
                "endorsement_weight": 95.0,
                "is_final_solution": true
            }]
        });
        let result: StructuredBatchEvaluationResponse = serde_json::from_value(json).unwrap();
        assert!(result.evaluations[0].is_final_solution);
    }

    #[test]
    fn batch_eval_multiple_items() {
        let json = serde_json::json!({
            "evaluations": [
                {"agent_id": "a", "endorsement_weight": 80.0},
                {"agent_id": "b", "endorsement_weight": 60.0},
                {"agent_id": "c", "endorsement_weight": 40.0}
            ]
        });
        let result: StructuredBatchEvaluationResponse = serde_json::from_value(json).unwrap();
        assert_eq!(result.evaluations.len(), 3);
        assert_eq!(result.evaluations[2].endorsement_weight, 40.0);
    }

    // =========================================================================
    // deserialize_string_or_array_or_object — object input
    // =========================================================================

    #[test]
    fn deser_string_or_array_nested_object_with_multiple_keys() {
        let json = serde_json::json!({
            "thought_process": {"step1": "analyze", "step2": "conclude"},
            "solution_content": "x"
        });
        let result: StructuredProposalResponse = serde_json::from_value(json).unwrap();
        // Object should be serialized to JSON string
        assert!(result.thought_process.contains("step1"));
        assert!(result.thought_process.contains("analyze"));
    }

    #[test]
    fn deser_string_or_array_number() {
        let json = serde_json::json!({
            "thought_process": 42,
            "solution_content": "x"
        });
        let result: StructuredProposalResponse = serde_json::from_value(json).unwrap();
        assert_eq!(result.thought_process, "42");
    }

    #[test]
    fn deser_string_or_array_bool() {
        let json = serde_json::json!({
            "thought_process": true,
            "solution_content": "x"
        });
        let result: StructuredProposalResponse = serde_json::from_value(json).unwrap();
        assert_eq!(result.thought_process, "true");
    }

    #[test]
    fn deser_string_or_array_null_becomes_string() {
        let json = serde_json::json!({
            "thought_process": null,
            "solution_content": "x"
        });
        let result: StructuredProposalResponse = serde_json::from_value(json).unwrap();
        assert_eq!(result.thought_process, "null");
    }

    #[test]
    fn deser_string_or_array_mixed_types_in_array() {
        let json = serde_json::json!({
            "thought_process": ["text", 123, true, null, {"k": "v"}],
            "solution_content": "x"
        });
        let result: StructuredProposalResponse = serde_json::from_value(json).unwrap();
        // Strings stay as-is, non-strings get serialized
        assert!(result.thought_process.contains("text"));
        assert!(result.thought_process.contains("123"));
        assert!(result.thought_process.contains("true"));
    }

    // =========================================================================
    // strip_scratchpad — nested and multiline edge cases
    // =========================================================================

    #[test]
    fn strip_scratchpad_multiline_content() {
        let input = "Start\n<scratchpad>\nLine 1\nLine 2\nLine 3\n</scratchpad>\nEnd";
        let result = strip_scratchpad(input);
        assert!(!result.contains("Line 1"));
        assert!(!result.contains("Line 2"));
        assert!(!result.contains("Line 3"));
        assert!(result.contains("Start"));
        assert!(result.contains("End"));
    }

    #[test]
    fn strip_scratchpad_no_tags() {
        let input = "Content without any scratchpad tags";
        let result = strip_scratchpad(input);
        assert_eq!(result, input);
    }

    // =========================================================================
    // strip_working_memory — no tags
    // =========================================================================

    #[test]
    fn strip_working_memory_no_tags() {
        let input = "Content without any working_memory tags";
        let result = strip_working_memory(input);
        assert_eq!(result, input);
    }
}