searchwala 8.3.0

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

use std::collections::HashSet;
use std::fmt::Write;
use std::time::Duration;

use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION, CONTENT_TYPE};

use crate::models::{LlmConfig, SourceResult};
use crate::query_intel::{QueryIntelligence, QueryIntent};

/// Language-mirroring instruction appended to every system prompt.
///
/// v7.0's prompts were hardcoded English, so a Hindi query about Hindi sources
/// produced an English answer. For a tool whose best-performing extraction
/// case is Hindi news, that is a product defect, not a nicety.
pub(crate) const LANGUAGE_RULE: &str = " LANGUAGE: Answer in the SAME language the user      asked in. If the question is in Hindi, answer in Hindi; if in Spanish,      answer in Spanish. When the sources are in a different language from the      question, translate the facts into the question's language — never switch      the answer's language to match the sources.";

/// How much context the model may receive.
///
/// v7.0 hardcoded `MAX_CONTEXT_CHARS_LITE = 16_000`. The pipeline scraped up
/// to 420 pages and then handed the LLM 16 KB. After Phase 3 raised extraction
/// to 374k-680k chars per search, that cap was discarding >97% of the evidence
/// it had just gathered. 2026 models have 200K-2M token windows.
///
/// Defaults are deliberately conservative — overshooting costs the user money —
/// and `CONTEXT_MAX_TOKENS` overrides everything.
pub struct ContextBudget {
    /// The model's whole window: prompt + generated output.
    pub max_tokens: usize,
}

/// Tokens reserved for the system prompt, instructions and per-source headers.
const SCAFFOLD_TOKENS: usize = 900;

/// Default window assumed for a LOCAL model server.
///
/// Measured 2026-10-02 against Ollama 0.14.2 (qwen2.5-coder 7B, whose own
/// limit is 32,768): a 15k-token prompt sent to the OpenAI-compatible endpoint
/// came back with `prompt_tokens: 4096`. Ollama silently truncates to its
/// runtime `num_ctx`, dropping the START of the prompt — which is where the
/// system prompt and every instruction live. LM Studio and llama.cpp default
/// to similar windows. SearchWala previously assumed 24k tokens for any
/// unknown model, so local users lost ~80% of each prompt without any error.
const LOCAL_SERVER_DEFAULT_TOKENS: usize = 4_096;

fn is_local_llm(cfg: &LlmConfig) -> bool {
    let provider = cfg.provider.to_lowercase();
    if matches!(
        provider.as_str(),
        "ollama" | "lmstudio" | "lm_studio" | "llamacpp" | "llama.cpp"
    ) {
        return true;
    }
    let Some(base) = cfg.base_url.as_deref() else {
        return false;
    };
    let Ok(u) = url::Url::parse(base.trim()) else {
        return false;
    };
    match u.host() {
        Some(url::Host::Domain(d)) => {
            d == "localhost" || d.ends_with(".local") || d.ends_with(".localhost")
        }
        Some(url::Host::Ipv4(ip)) => ip.is_loopback() || ip.is_private() || ip.is_link_local(),
        Some(url::Host::Ipv6(ip)) => ip.is_loopback(),
        None => false,
    }
}

impl ContextBudget {
    /// Budget for a configured provider. Prefer this over `for_model`.
    pub fn for_config(cfg: &LlmConfig) -> Self {
        // The window the user set in Settings, then the server's env.
        let override_tokens = cfg.context_window.filter(|&t| t > 0).or_else(|| {
            std::env::var("CONTEXT_MAX_TOKENS")
                .ok()
                .and_then(|v| v.parse().ok())
        });
        Self::for_config_with_override(
            cfg,
            override_tokens,
            std::env::var("OLLAMA_CONTEXT_LENGTH")
                .ok()
                .and_then(|v| v.parse().ok()),
        )
    }

    /// Testable core.
    pub fn for_config_with_override(
        cfg: &LlmConfig,
        override_tokens: Option<usize>,
        ollama_ctx: Option<usize>,
    ) -> Self {
        if let Some(v) = override_tokens {
            return Self {
                max_tokens: v.clamp(2_000, 2_000_000),
            };
        }
        if is_local_llm(cfg) {
            // Users who raise Ollama's window usually export
            // OLLAMA_CONTEXT_LENGTH in the same shell — honour it.
            let t = ollama_ctx.unwrap_or(LOCAL_SERVER_DEFAULT_TOKENS);
            return Self {
                max_tokens: t.clamp(2_000, 900_000),
            };
        }
        let base_budget = Self::for_model_with_override(&cfg.model, None);
        if let Some(learned_max) = learned_token_limit(&cfg.provider, &cfg.model) {
            return Self {
                max_tokens: base_budget
                    .max_tokens
                    .min(learned_max)
                    .clamp(2_000, 900_000),
            };
        }
        base_budget
    }

    pub fn for_model(model: &str) -> Self {
        Self::for_model_with_override(
            model,
            std::env::var("CONTEXT_MAX_TOKENS")
                .ok()
                .and_then(|v| v.parse::<usize>().ok()),
        )
    }

    /// Testable core — the override is a parameter, not an env read.
    pub fn for_model_with_override(model: &str, override_tokens: Option<usize>) -> Self {
        if let Some(v) = override_tokens {
            return Self {
                max_tokens: v.clamp(2_000, 900_000),
            };
        }
        let m = model.to_lowercase();
        let max_tokens = if m.contains("gemini") && m.contains("pro") {
            400_000
        } else if m.contains("gemini") {
            200_000
        } else if m.contains("opus") || m.contains("sonnet") || m.contains("fable") {
            150_000
        } else if m.contains("gpt-5") || m.contains("o4") || m.contains("gpt-4.1") {
            120_000
        } else if m.contains("haiku") || m.contains("mini") || m.contains("flash") {
            60_000
        } else {
            // Unknown hosted model: conservative.
            24_000
        };
        Self { max_tokens }
    }

    /// Largest output worth requesting: an eighth of the window, never more
    /// than a third of it. The old fixed 2,048-token floor, on a 4,096-token
    /// local window, left almost nothing for the sources.
    pub fn max_output_tokens(&self) -> usize {
        let floor = if self.max_tokens >= 16_000 {
            2_048
        } else {
            256
        };
        (self.max_tokens / 8)
            .clamp(floor, 32_000)
            .min(self.max_tokens / 3)
    }

    /// Cap a mode's requested output so it fits the window.
    pub fn fit_output(&self, requested: usize) -> usize {
        requested.min(self.max_tokens / 3).max(128)
    }

    /// Tokens left for source text once `output_tokens` and the prompt
    /// scaffolding are reserved. Prompt and output share ONE window.
    pub fn context_tokens(&self, output_tokens: usize) -> usize {
        self.max_tokens
            .saturating_sub(output_tokens + SCAFFOLD_TOKENS)
            .max(400)
    }

    /// Characters of source text at an English-like density, with the default
    /// output reserved. Prefer `chars_for_tokens` with the real sources.
    pub fn max_context_chars(&self) -> usize {
        self.context_tokens(self.max_output_tokens()) * 4
    }
}

/// How many characters of THESE sources fit in `tokens`.
///
/// A fixed chars-per-token ratio is wrong across scripts: English runs ~4
/// characters per token, Devanagari closer to 1-1.5. The previous code used
/// 3.3 for everything (its comment claimed this was "safe" for Indic scripts,
/// which has the direction backwards), so a Hindi context overshot its token
/// budget by roughly 2-3x. This measures the density of the actual text.
pub fn chars_for_tokens(tokens: usize, sources: &[SourceResult]) -> usize {
    let mut chars = 0usize;
    let mut est = 0usize;
    for s in sources.iter().take(40) {
        let sample = crate::util::truncate_chars(&s.extracted_text, 2_000);
        chars += crate::util::char_len(sample);
        est += est_tokens(sample);
    }
    let per_token = if est == 0 {
        4.0
    } else {
        (chars as f64 / est as f64).clamp(0.8, 4.5)
    };
    (tokens as f64 * per_token) as usize
}

/// Rough token estimate that accounts for script density.
pub fn est_tokens(s: &str) -> usize {
    let mut units = 0usize;
    for c in s.chars() {
        units += if c.is_ascii() { 1 } else { 3 };
    }
    units / 4 + 1
}

// MAX_CONTEXT_CHARS_LITE (16_000) REMOVED in v8.0 — superseded by
// ContextBudget::for_model, which scales with the model instead of discarding
// >97% of the evidence the pipeline gathered.
pub(crate) const DEFAULT_LLM_TIMEOUT_MS: u64 = 45_000;

#[derive(Debug, Default)]
pub struct LlmExecutionResult {
    pub llm_answer: Option<String>,
    pub llm_error: Option<String>,
    pub batches_processed: usize,
    /// citation number (1-based) -> index into the sources slice.
    pub citation_map: Vec<usize>,
    /// Support check per citation.
    pub citation_checks: Vec<CitationCheck>,
}

// =============================================================================
// Internal ChatMessage — replaces genai::chat::ChatMessage
// =============================================================================

#[derive(Debug, Clone)]
pub(crate) struct ChatMessage {
    pub role: String,
    pub content: String,
}

impl ChatMessage {
    pub fn system(content: impl Into<String>) -> Self {
        Self {
            role: "system".into(),
            content: content.into(),
        }
    }
    pub fn user(content: impl Into<String>) -> Self {
        Self {
            role: "user".into(),
            content: content.into(),
        }
    }
}

// =============================================================================
// Provider Detection — determines API format, endpoint, and auth strategy
// =============================================================================

#[derive(Debug, Clone, PartialEq)]
#[allow(dead_code)]
enum ApiFormat {
    /// OpenAI-compatible: POST /chat/completions (used by all providers including Gemini)
    OpenAiCompatible,
    /// Google Gemini native: POST /v1beta/models/{model}:generateContent (reserved)
    Gemini,
    /// Anthropic Claude: POST /v1/messages
    Anthropic,
}

struct ProviderInfo {
    format: ApiFormat,
    base_url: String,
}

fn resolve_provider(config: &LlmConfig) -> Result<ProviderInfo, String> {
    let provider = config.provider.trim().to_lowercase();

    // If the user provided a custom base_url, VALIDATE it first (v8.0 P1-12a).
    // This value arrives in the request body, and with permissive CORS v7.0
    // would relay the user's API key to any address an attacker chose.
    if let Some(ref base_url) = config.base_url {
        let url = base_url.trim();
        if !url.is_empty() {
            if let Err(e) = crate::ssrf::validate_llm_base_url(url) {
                // v8.1: this used to fall back to the provider's DEFAULT
                // endpoint. For "openai_compatible" that is api.openai.com, so
                // a key meant for a custom/LAN endpoint was sent to OpenAI.
                tracing::warn!("Rejected caller-supplied base_url: {}", e);
                return Err(format!("base_url rejected: {e}"));
            }
            let format = if provider == "anthropic" {
                ApiFormat::Anthropic
            } else {
                // All other providers (including Gemini) use OpenAI-compatible format
                ApiFormat::OpenAiCompatible
            };
            return Ok(ProviderInfo {
                format,
                base_url: ensure_trailing_slash(&normalize_base_url(url, &provider)),
            });
        }
    }

    Ok(default_provider_for(&provider))
}

/// Trim endpoint paths users paste along with the base URL. The chat path is
/// appended later, so ".../v1/chat/completions" became
/// ".../v1/chat/completions/chat/completions", and an Anthropic base of
/// "https://api.anthropic.com/v1" became ".../v1/v1/messages".
pub(crate) fn normalize_base_url(raw: &str, provider: &str) -> String {
    let mut u = raw.trim().trim_end_matches('/').to_string();
    for suffix in [
        "/chat/completions",
        "/completions",
        "/v1/messages",
        "/messages",
        "/models",
    ] {
        if let Some(stripped) = u.strip_suffix(suffix) {
            u = stripped.trim_end_matches('/').to_string();
            break;
        }
    }
    if provider == "anthropic" {
        if let Some(stripped) = u.strip_suffix("/v1") {
            u = stripped.to_string();
        }
    }
    u
}

/// Normalise and validate a caller-supplied LLM config once, where the
/// request enters the pipeline. Every later stage can then trust it.
///
/// Fixes, each a real failure mode:
///   * keys pasted with a trailing newline/space failed `HeaderValue`
///     parsing, the auth header was silently dropped, and the provider
///     answered 401 with no hint why;
///   * a pasted "Bearer sk-…" became "Bearer Bearer sk-…";
///   * " gpt-4o-mini" (leading space) is a 404 "model not found";
///   * an empty key was sent as "Bearer " instead of being reported;
///   * a rejected base_url fell back to another provider's endpoint;
///   * `timeout_ms` had no upper bound.
pub fn prepare_config(mut c: LlmConfig) -> Result<LlmConfig, String> {
    c.provider = c.provider.trim().to_lowercase();
    c.model = c.model.trim().to_string();
    let mut key = c.api_key.trim();
    for prefix in ["Bearer ", "bearer ", "BEARER "] {
        if let Some(rest) = key.strip_prefix(prefix) {
            key = rest.trim();
        }
    }
    c.api_key = key.to_string();
    c.base_url = c
        .base_url
        .map(|b| b.trim().to_string())
        .filter(|b| !b.is_empty());

    if c.model.is_empty() {
        return Err("llm_config: no model name was given".into());
    }
    if c.api_key
        .chars()
        .any(|ch| ch.is_whitespace() || ch.is_control() || !ch.is_ascii())
    {
        return Err("llm_config: the API key contains spaces, line breaks or non-ASCII characters; copy it again from your provider".into());
    }
    if c.api_key.is_empty() && !is_local_llm(&c) {
        return Err("llm_config: the API key is empty".into());
    }
    if let Some(b) = &c.base_url {
        crate::ssrf::validate_llm_base_url(b)
            .map_err(|e| format!("llm_config: base_url rejected: {e}"))?;
    } else if !KNOWN_PROVIDERS.contains(&c.provider.as_str()) {
        // Unknown names fell through to api.openai.com — the key went to a
        // provider the user never chose.
        return Err(format!(
            "llm_config: provider {:?} needs a base_url (the endpoint of your OpenAI-compatible server)",
            c.provider
        ));
    }
    c.timeout_ms = c.timeout_ms.map(|t| t.clamp(1_000, 600_000));
    Ok(c)
}

/// Provider names with a built-in endpoint (see `default_provider_for`).
const KNOWN_PROVIDERS: &[&str] = &[
    "gemini",
    "google",
    "anthropic",
    "openai",
    "groq",
    "together",
    "openrouter",
    "cerebras",
    "deepseek",
    "xai",
    "ollama",
    "cohere",
    "fireworks",
    "perplexity",
    "mistral_api",
    "mistral",
    "sambanova",
    "nvidia_nim",
];

/// Remove the caller's key from any text that may be shown or logged.
/// Some providers echo the submitted key in their 401 message.
pub(crate) fn redact_key(text: &str, key: &str) -> String {
    if key.len() < 8 {
        return text.to_string();
    }
    text.replace(key, "<redacted>")
}

/// Built-in endpoint for a known provider name.
fn default_provider_for(provider: &str) -> ProviderInfo {
    match provider {
        "gemini" | "google" => ProviderInfo {
            // Use Google's OpenAI-compatible endpoint (same approach as Perplexica)
            format: ApiFormat::OpenAiCompatible,
            base_url: "https://generativelanguage.googleapis.com/v1beta/openai/".to_string(),
        },
        "anthropic" => ProviderInfo {
            format: ApiFormat::Anthropic,
            base_url: "https://api.anthropic.com/".to_string(),
        },
        "openai" => ProviderInfo {
            format: ApiFormat::OpenAiCompatible,
            base_url: "https://api.openai.com/v1/".to_string(),
        },
        "groq" => ProviderInfo {
            format: ApiFormat::OpenAiCompatible,
            base_url: "https://api.groq.com/openai/v1/".to_string(),
        },
        "together" => ProviderInfo {
            format: ApiFormat::OpenAiCompatible,
            base_url: "https://api.together.xyz/v1/".to_string(),
        },
        "openrouter" => ProviderInfo {
            format: ApiFormat::OpenAiCompatible,
            base_url: "https://openrouter.ai/api/v1/".to_string(),
        },
        "cerebras" => ProviderInfo {
            format: ApiFormat::OpenAiCompatible,
            base_url: "https://api.cerebras.ai/v1/".to_string(),
        },
        "deepseek" => ProviderInfo {
            format: ApiFormat::OpenAiCompatible,
            base_url: "https://api.deepseek.com/v1/".to_string(),
        },
        "xai" => ProviderInfo {
            format: ApiFormat::OpenAiCompatible,
            base_url: "https://api.x.ai/v1/".to_string(),
        },
        "ollama" => ProviderInfo {
            format: ApiFormat::OpenAiCompatible,
            base_url: "http://localhost:11434/v1/".to_string(),
        },
        "cohere" => ProviderInfo {
            format: ApiFormat::OpenAiCompatible,
            base_url: "https://api.cohere.ai/compatibility/v1/".to_string(),
        },
        "fireworks" => ProviderInfo {
            format: ApiFormat::OpenAiCompatible,
            base_url: "https://api.fireworks.ai/inference/v1/".to_string(),
        },
        "perplexity" => ProviderInfo {
            format: ApiFormat::OpenAiCompatible,
            base_url: "https://api.perplexity.ai/v1/".to_string(),
        },
        "mistral_api" | "mistral" => ProviderInfo {
            format: ApiFormat::OpenAiCompatible,
            base_url: "https://api.mistral.ai/v1/".to_string(),
        },
        "sambanova" => ProviderInfo {
            format: ApiFormat::OpenAiCompatible,
            base_url: "https://api.sambanova.ai/v1/".to_string(),
        },
        "nvidia_nim" => ProviderInfo {
            format: ApiFormat::OpenAiCompatible,
            base_url: "https://integrate.api.nvidia.com/v1/".to_string(),
        },
        // Default: treat as OpenAI-compatible
        _ => ProviderInfo {
            format: ApiFormat::OpenAiCompatible,
            base_url: "https://api.openai.com/v1/".to_string(),
        },
    }
}

// =============================================================================
// Request/Response Payload Builders
// =============================================================================

/// Build the full URL for an LLM call.
/// base_url already includes the version path (e.g. "https://api.groq.com/openai/v1/")
/// so we just append the endpoint path.
fn build_chat_url(info: &ProviderInfo, model: &str, stream: bool) -> String {
    match info.format {
        ApiFormat::OpenAiCompatible => {
            format!("{}chat/completions", info.base_url)
        }
        ApiFormat::Gemini => {
            // Native Gemini API (only used when no base_url override)
            let action = if stream {
                "streamGenerateContent?alt=sse"
            } else {
                "generateContent"
            };
            format!(
                "{}v1beta/models/{}:{}",
                ensure_trailing_slash(&info.base_url),
                model,
                action
            )
        }
        ApiFormat::Anthropic => {
            // v8.1: this was `base.trim_end_matches('/')` + "v1/messages",
            // i.e. "https://api.anthropic.comv1/messages" — Anthropic calls
            // could never have succeeded.
            format!("{}v1/messages", ensure_trailing_slash(&info.base_url))
        }
    }
}

/// Build auth headers per provider
fn build_auth_headers(config: &LlmConfig, info: &ProviderInfo) -> HeaderMap {
    let mut headers = HeaderMap::new();
    headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));

    match info.format {
        ApiFormat::Gemini => {
            // Gemini uses x-goog-api-key header
            if let Ok(val) = HeaderValue::from_str(&config.api_key) {
                headers.insert("x-goog-api-key", val);
            }
        }
        ApiFormat::Anthropic => {
            // Anthropic uses x-api-key + anthropic-version
            if let Ok(val) = HeaderValue::from_str(&config.api_key) {
                headers.insert("x-api-key", val);
            }
            headers.insert("anthropic-version", HeaderValue::from_static("2023-06-01"));
        }
        ApiFormat::OpenAiCompatible if config.api_key.is_empty() => {
            // Local servers (Ollama, llama.cpp) need no auth.
        }
        ApiFormat::OpenAiCompatible => {
            // Bearer token auth
            let bearer = format!("Bearer {}", config.api_key);
            if let Ok(val) = HeaderValue::from_str(&bearer) {
                headers.insert(AUTHORIZATION, val);
            }
        }
    }

    headers
}

/// Build request JSON body per provider format
fn build_request_body(
    messages: &[ChatMessage],
    model: &str,
    info: &ProviderInfo,
    stream: bool,
    max_output_tokens: usize,
) -> serde_json::Value {
    match info.format {
        ApiFormat::OpenAiCompatible => {
            let msgs: Vec<serde_json::Value> = messages
                .iter()
                .map(|m| {
                    serde_json::json!({
                        "role": m.role,
                        "content": m.content,
                    })
                })
                .collect();

            let mut body = serde_json::json!({
                "model": model,
                "messages": msgs,
                // v8.0: both were ABSENT in v7.0. Without max_tokens the
                // provider applies its own default, which truncates long
                // research reports; without temperature, synthesis was at the
                // provider default (often 1.0) for a task that wants
                // determinism.
                "max_tokens": max_output_tokens,
                "temperature": 0.2,
            });

            if stream {
                body["stream"] = serde_json::json!(true);
            }

            body
        }
        ApiFormat::Gemini => {
            // Gemini uses `system_instruction` (separate from contents)
            // and `contents` array with role "user" / "model"
            let mut system_text = String::new();
            let mut contents: Vec<serde_json::Value> = Vec::new();

            for msg in messages {
                match msg.role.as_str() {
                    "system" => {
                        // Join, don't overwrite: a second system message
                        // replaced the first.
                        if !system_text.is_empty() {
                            system_text.push_str("\n\n");
                        }
                        system_text.push_str(&msg.content);
                    }
                    "user" => {
                        contents.push(serde_json::json!({
                            "role": "user",
                            "parts": [{"text": msg.content}]
                        }));
                    }
                    "assistant" | "model" => {
                        contents.push(serde_json::json!({
                            "role": "model",
                            "parts": [{"text": msg.content}]
                        }));
                    }
                    _ => {
                        contents.push(serde_json::json!({
                            "role": "user",
                            "parts": [{"text": msg.content}]
                        }));
                    }
                }
            }

            let mut body = serde_json::json!({
                "contents": contents,
            });

            if !system_text.is_empty() {
                body["system_instruction"] = serde_json::json!({
                    "parts": [{"text": system_text}]
                });
            }

            body
        }
        ApiFormat::Anthropic => {
            // Anthropic: system is top-level, messages only contain user/assistant
            let mut system_text = String::new();
            let mut msgs: Vec<serde_json::Value> = Vec::new();

            for msg in messages {
                match msg.role.as_str() {
                    "system" => {
                        // Join, don't overwrite: a second system message
                        // replaced the first.
                        if !system_text.is_empty() {
                            system_text.push_str("\n\n");
                        }
                        system_text.push_str(&msg.content);
                    }
                    "user" | "assistant" => {
                        msgs.push(serde_json::json!({
                            "role": msg.role,
                            "content": msg.content,
                        }));
                    }
                    _ => {
                        msgs.push(serde_json::json!({
                            "role": "user",
                            "content": msg.content,
                        }));
                    }
                }
            }

            let mut body = serde_json::json!({
                "model": model,
                "messages": msgs,
                // v7.0 hardcoded 4096, which truncated every research report.
                // v8.1: was `.max(8192)`, which forced even Fast's two-sentence
                // answers onto an 8K budget. Modes now set the length deliberately.
                "max_tokens": max_output_tokens.max(256),
                "temperature": 0.2,
            });

            if !system_text.is_empty() {
                body["system"] = serde_json::json!(system_text);
            }

            if stream {
                body["stream"] = serde_json::json!(true);
            }

            body
        }
    }
}

/// Extract text content from provider-specific JSON response
fn extract_response_text(json: &serde_json::Value, format: &ApiFormat) -> Option<String> {
    match format {
        ApiFormat::OpenAiCompatible => {
            // v8.0: `content` may be a string, null, or an array of typed
            // parts; reasoning models sometimes put the answer only in
            // `reasoning_content`. v7.0 handled the string case alone and
            // reported "empty_response" for everything else.
            let msg = json.get("choices")?.get(0)?.get("message")?;
            match msg.get("content") {
                Some(serde_json::Value::String(s)) if !s.trim().is_empty() => Some(s.clone()),
                Some(serde_json::Value::Array(parts)) => {
                    let s: String = parts
                        .iter()
                        .filter(|p| {
                            p.get("type")
                                .and_then(|t| t.as_str())
                                .is_none_or(|t| t == "text")
                        })
                        .filter_map(|p| p.get("text").and_then(|t| t.as_str()))
                        .collect();
                    (!s.trim().is_empty()).then_some(s)
                }
                _ => msg
                    .get("reasoning_content")
                    .and_then(|t| t.as_str())
                    .filter(|s| !s.trim().is_empty())
                    .map(str::to_string),
            }
        }
        ApiFormat::Gemini => {
            // candidates[0].content.parts[0].text
            json.get("candidates")
                .and_then(|c| c.get(0))
                .and_then(|c| c.get("content"))
                .and_then(|c| c.get("parts"))
                .and_then(|p| p.get(0))
                .and_then(|p| p.get("text"))
                .and_then(|t| t.as_str())
                .map(|s| s.to_string())
        }
        ApiFormat::Anthropic => {
            // v8.0: scan for the first TEXT block instead of assuming index 0.
            //
            // With extended thinking enabled, content[0] is a
            // {"type":"thinking", ...} block with no "text" field, so v7.0's
            // `content[0].text` returned None and the whole call failed with
            // "empty_response" — the answer was silently discarded.
            let blocks = json.get("content")?.as_array()?;
            let mut out = String::new();
            for b in blocks {
                if b.get("type").and_then(|t| t.as_str()) == Some("text") {
                    if let Some(t) = b.get("text").and_then(|t| t.as_str()) {
                        out.push_str(t);
                    }
                }
            }
            (!out.trim().is_empty()).then_some(out)
        }
    }
}

/// Extract error message from API error JSON — universal for all providers.
/// Tries multiple common error structures since each provider uses different nesting.
fn extract_error_message(json: &serde_json::Value, _format: &ApiFormat) -> String {
    // Try: { "error": { "message": "..." } } — OpenAI, Groq, Gemini
    if let Some(msg) = json
        .get("error")
        .and_then(|e| e.get("message"))
        .and_then(|m| m.as_str())
    {
        return msg.to_string();
    }

    // Try: { "error": "string" } — some providers use a flat error string
    if let Some(msg) = json.get("error").and_then(|e| e.as_str()) {
        return msg.to_string();
    }

    // Try: { "error": { "error": { "message": "..." } } } — deeply nested
    if let Some(msg) = json
        .get("error")
        .and_then(|e| e.get("error"))
        .and_then(|e| e.get("message"))
        .and_then(|m| m.as_str())
    {
        return msg.to_string();
    }

    // Try: { "message": "..." } — top-level message (Anthropic, some others)
    if let Some(msg) = json.get("message").and_then(|m| m.as_str()) {
        return msg.to_string();
    }

    // Try: { "detail": "..." } — FastAPI-style errors
    if let Some(msg) = json.get("detail").and_then(|m| m.as_str()) {
        return msg.to_string();
    }

    // Fallback: stringify the first 300 chars of the JSON
    let raw = json.to_string();
    if crate::util::char_len(&raw) > 300 {
        format!("{}...", crate::util::truncate_chars(&raw, 300))
    } else {
        raw
    }
}

// =============================================================================
// Core LLM Call — Non-Streaming (replaces genai client.exec_chat)
// =============================================================================

/// ONE shared client. v7.0 called `build_http_client()` inside every
/// `call_llm`, constructing a fresh TLS stack and connection pool per call —
/// in research mode that is one client per batch.
static LLM_CLIENT: std::sync::OnceLock<reqwest::Client> = std::sync::OnceLock::new();

pub(crate) fn http_client() -> &'static reqwest::Client {
    LLM_CLIENT.get_or_init(|| {
        reqwest::Client::builder()
            .connect_timeout(Duration::from_secs(10))
            .timeout(Duration::from_secs(300))
            // A validated base_url must not be able to redirect us to an
            // address validation would have refused (169.254.169.254, LAN).
            // LLM APIs do not redirect POSTs.
            .redirect(reqwest::redirect::Policy::none())
            .pool_max_idle_per_host(8)
            .tcp_nodelay(true)
            .build()
            .expect("LLM HTTP client error")
    })
}

/// Retryable provider failures: rate limits and transient server errors.
fn is_retryable(err: &str) -> bool {
    err.starts_with("http_429")
        || err.starts_with("http_500")
        || err.starts_with("http_502")
        || err.starts_with("http_503")
        || err.starts_with("http_504")
        || err.starts_with("http_529")
        || err.contains("http_request_failed")
        || err.contains("response_read_error")
}

/// Detects genuine single-request HTTP 413 or context_length_exceeded errors.
/// Rolling-window HTTP 429 rate limits ("Used X / Limit Y, try again in Zs")
/// must NEVER be treated as payload-too-large (they are handled by waiting, not shrinking).
pub(crate) fn is_payload_too_large(err: &str) -> bool {
    if err.starts_with("http_413") {
        return true;
    }
    if !err.starts_with("http_400") {
        return false;
    }
    let lower = err.to_ascii_lowercase();
    lower.contains("request too large")
        || lower.contains("context_length_exceeded")
        || lower.contains("maximum context length")
        || lower.contains("prompt is too long")
        || lower.contains("reduce your message size")
}

/// Parses provider error strings such as "Limit 8000, Requested 9655" to compute
/// an exact safe keep-ratio, falling back to 0.72 when no numbers are present.
pub(crate) fn parse_token_limit_ratio(err: &str) -> Option<f64> {
    let lower = err.to_ascii_lowercase();
    let limit_pos = lower.find("limit")?;
    let req_pos = lower.find("requested")?;

    let parse_first_num = |s: &str| -> Option<usize> {
        let num_str: String = s
            .chars()
            .skip_while(|c| !c.is_ascii_digit())
            .take_while(|c| c.is_ascii_digit())
            .collect();
        num_str.parse::<usize>().ok().filter(|&n| n > 0)
    };

    let limit_val = parse_first_num(&lower[limit_pos..])?;
    let req_val = parse_first_num(&lower[req_pos..])?;
    if req_val > limit_val {
        Some(((limit_val as f64 / req_val as f64) * 0.85).clamp(0.35, 0.88))
    } else {
        None
    }
}

/// Parses provider rate-limit wait hints such as "Please try again in 3.57s"
/// or "try again in 457.5ms" from HTTP 429 error messages.
pub(crate) fn parse_retry_after_delay(err: &str) -> Option<Duration> {
    let lower = err.to_ascii_lowercase();
    let marker = "try again in ";
    let pos = lower.find(marker)?;
    let after = &lower[pos + marker.len()..];
    let num_str: String = after
        .chars()
        .take_while(|c| c.is_ascii_digit() || *c == '.')
        .collect();
    let val = num_str.parse::<f64>().ok()?;
    let unit_rest = after[num_str.len()..].trim_start();
    let ms = if unit_rest.starts_with("ms") {
        val as u64
    } else {
        (val * 1000.0) as u64
    };
    Some(Duration::from_millis((ms + 300).clamp(300, 15_000)))
}

/// Process-wide cache of dynamically discovered provider/model token limits
/// learned from live HTTP response headers (`x-ratelimit-limit-tokens`, etc.)
/// or provider error messages (`Limit XXXX, Requested YYYY`).
static LEARNED_TOKEN_LIMITS: std::sync::LazyLock<
    std::sync::Mutex<std::collections::HashMap<String, usize>>,
> = std::sync::LazyLock::new(|| std::sync::Mutex::new(std::collections::HashMap::new()));

fn provider_model_key(provider: &str, model: &str) -> String {
    format!(
        "{}:{}",
        provider.trim().to_ascii_lowercase(),
        model.trim().to_ascii_lowercase()
    )
}

/// Returns any dynamically learned safe token window for `(provider, model)`.
pub fn learned_token_limit(provider: &str, model: &str) -> Option<usize> {
    let key = provider_model_key(provider, model);
    LEARNED_TOKEN_LIMITS
        .lock()
        .ok()
        .and_then(|m| m.get(&key).copied())
}

/// Records a discovered provider token limit (keeping a 92% window so single-call
/// prompts can pack ~5,400-5,600 tokens / ~20,500 chars on an 8,000 TPM tier).
pub fn record_learned_token_limit(provider: &str, model: &str, raw_limit_tokens: usize) {
    if !(1_500..=500_000).contains(&raw_limit_tokens) {
        return;
    }
    let safe_window = ((raw_limit_tokens * 92) / 100).max(2_000);
    let key = provider_model_key(provider, model);
    if let Ok(mut map) = LEARNED_TOKEN_LIMITS.lock() {
        let prev = map.get(&key).copied();
        if prev != Some(safe_window) {
            tracing::info!(
                "Learned dynamic token limit for {}: raw={} -> safe_window={}",
                key,
                raw_limit_tokens,
                safe_window
            );
            map.insert(key, safe_window);
        }
    }
}

/// Inspects standard provider HTTP rate-limit headers to learn TPM / token ceilings dynamically.
fn learn_rate_limits_from_headers(config: &LlmConfig, headers: &reqwest::header::HeaderMap) {
    for h_name in [
        "x-ratelimit-limit-tokens",
        "x-ratelimit-limit-tokens_usage_based",
        "anthropic-ratelimit-tokens-limit",
        "anthropic-ratelimit-input-tokens-limit",
    ] {
        if let Some(val) = headers.get(h_name).and_then(|v| v.to_str().ok()) {
            let digits: String = val
                .trim()
                .chars()
                .take_while(|c| c.is_ascii_digit())
                .collect();
            if let Ok(limit_tokens) = digits.parse::<usize>() {
                record_learned_token_limit(&config.provider, &config.model, limit_tokens);
                break;
            }
        }
    }
}

/// Inspects an error string for `"Limit XXXX, Requested YYYY"` and records the discovered limit.
pub(crate) fn learn_rate_limit_from_error(config: &LlmConfig, err: &str) {
    let lower = err.to_ascii_lowercase();
    if let Some(limit_pos) = lower.find("limit") {
        let num_str: String = lower[limit_pos..]
            .chars()
            .skip_while(|c| !c.is_ascii_digit())
            .take_while(|c| c.is_ascii_digit())
            .collect();
        if let Ok(limit_tokens) = num_str.parse::<usize>() {
            record_learned_token_limit(&config.provider, &config.model, limit_tokens);
        }
    }
}

/// Trims the source context inside `messages` while preserving the trailing
/// `[USER QUESTION]` or `\n---\n` block so a retry fits the provider's TPM cap.
pub(crate) fn shrink_messages_for_retry(messages: &[ChatMessage], ratio: f64) -> Vec<ChatMessage> {
    let keep_ratio = ratio.clamp(0.25, 0.85);
    messages
        .iter()
        .map(|m| {
            if m.role != "user" || crate::util::char_len(&m.content) < 1_200 {
                return m.clone();
            }
            let content = &m.content;
            let footer_idx = content
                .rfind("\n---\n")
                .or_else(|| content.rfind("\n[USER QUESTION]"));
            let new_content = if let Some(sep_idx) = footer_idx {
                let (sources_part, question_footer) = content.split_at(sep_idx);
                let target_chars =
                    ((crate::util::char_len(sources_part) as f64) * keep_ratio) as usize;
                let truncated = crate::util::truncate_chars(sources_part, target_chars.max(600));
                let clean_sources = truncated
                    .rfind("\n\n[")
                    .filter(|&pos| pos >= 400)
                    .map(|pos| &truncated[..pos])
                    .unwrap_or(truncated);
                format!("{clean_sources}{question_footer}")
            } else {
                let target_chars = ((crate::util::char_len(content) as f64) * keep_ratio) as usize;
                crate::util::truncate_chars(content, target_chars.max(600)).to_string()
            };
            ChatMessage {
                role: m.role.clone(),
                content: new_content,
            }
        })
        .collect()
}

/// `call_llm` with exponential backoff and automatic HTTP 413 / TPM context-trimming recovery.
pub(crate) async fn call_llm_retrying(
    config: &LlmConfig,
    messages: &[ChatMessage],
) -> Result<String, String> {
    const ATTEMPTS: usize = 4;
    let mut delay = Duration::from_millis(700);
    let mut last = String::new();
    let mut current_messages: Vec<ChatMessage> = messages.to_vec();
    let mut current_config: LlmConfig = config.clone();

    for attempt in 1..=ATTEMPTS {
        match call_llm(&current_config, &current_messages).await {
            Ok(v) => return Ok(v),
            Err(e) => {
                last = e;
                if attempt < ATTEMPTS && is_payload_too_large(&last) {
                    let ratio = parse_token_limit_ratio(&last).unwrap_or(0.62);
                    current_messages = shrink_messages_for_retry(&current_messages, ratio);
                    let cur_out = output_tokens_for(&current_config);
                    current_config.max_output_tokens = Some(((cur_out * 3) / 4).max(256));
                    tracing::warn!(
                        "LLM payload exceeded provider token cap on attempt {}/{} ({}); auto-trimmed context to {:.0}% and retrying immediately",
                        attempt,
                        ATTEMPTS,
                        last,
                        ratio * 100.0
                    );
                    tokio::time::sleep(Duration::from_millis(150)).await;
                    continue;
                }
                if attempt == ATTEMPTS || !is_retryable(&last) {
                    break;
                }
                let wait_dur = parse_retry_after_delay(&last).unwrap_or(delay);
                tracing::warn!(
                    "LLM attempt {}/{} hit retryable limit ({}), waiting {:?} with full context intact",
                    attempt, ATTEMPTS, last, wait_dur
                );
                tokio::time::sleep(wait_dur).await;
                delay = (delay * 2).min(Duration::from_secs(8));
            }
        }
    }
    Err(last)
}

/// Output-token budget for one call: the mode's explicit value when set
/// (Fast answers are deliberately short), else the model-derived default.
fn output_tokens_for(config: &LlmConfig) -> usize {
    config
        .max_output_tokens
        .map(|n| n.clamp(128, 64_000))
        .unwrap_or_else(|| ContextBudget::for_config(config).max_output_tokens())
}

/// Make a single non-streaming LLM call. Returns extracted text or error string.
pub(crate) async fn call_llm(
    config: &LlmConfig,
    messages: &[ChatMessage],
) -> Result<String, String> {
    let info = resolve_provider(config)?;
    let url = build_chat_url(&info, &config.model, false);
    let headers = build_auth_headers(config, &info);
    let body = build_request_body(
        messages,
        &config.model,
        &info,
        false,
        output_tokens_for(config),
    );

    tracing::info!(
        "LLM call: provider={} model={} format={:?} url={}",
        config.provider,
        config.model,
        info.format,
        url.split('?').next().unwrap_or(&url) // Don't log API key in Gemini URL params
    );

    let resp = http_client()
        .post(&url)
        .headers(headers)
        .json(&body)
        .send()
        .await
        .map_err(|e| redact_key(&format!("http_request_failed: {e}"), &config.api_key))?;

    let status = resp.status();
    learn_rate_limits_from_headers(config, resp.headers());
    let resp_text = resp
        .text()
        .await
        .map_err(|e| format!("response_read_error: {e}"))?;

    if !status.is_success() {
        let error_msg = match serde_json::from_str::<serde_json::Value>(&resp_text) {
            Ok(json) => extract_error_message(&json, &info.format),
            Err(_) => resp_text.chars().take(500).collect(),
        };
        let full_err = redact_key(
            &format!("http_{}: {}", status.as_u16(), error_msg),
            &config.api_key,
        );
        learn_rate_limit_from_error(config, &full_err);
        return Err(full_err);
    }

    let json: serde_json::Value =
        serde_json::from_str(&resp_text).map_err(|e| format!("json_parse_error: {e}"))?;

    if was_truncated(&json) {
        // Recorded per task so synthesis can tell the user, instead of
        // returning an answer that silently stops mid-section.
        let _ = TRUNCATED.try_with(|t| t.set(true));
        tracing::info!(
            "LLM output hit the max_tokens limit (model={})",
            config.model
        );
    }

    extract_response_text(&json, &info.format).ok_or_else(|| {
        // Log raw response structure for debugging (without API keys)
        let keys: Vec<&str> = json
            .as_object()
            .map(|o| o.keys().map(|k| k.as_str()).collect())
            .unwrap_or_default();
        format!(
            "empty_response: could not extract text from response (keys: {:?})",
            keys
        )
    })
}

tokio::task_local! {
    /// Set when any LLM call in the current scope stopped at its token limit.
    pub(crate) static TRUNCATED: std::cell::Cell<bool>;
}

/// Did the provider stop because it ran out of output tokens?
/// OpenAI-compatible: `finish_reason: "length"`; Anthropic: `stop_reason:
/// "max_tokens"`; Gemini (OpenAI-compatible endpoint) also reports "length".
pub fn was_truncated(json: &serde_json::Value) -> bool {
    let oai = json
        .get("choices")
        .and_then(|c| c.get(0))
        .and_then(|c| c.get("finish_reason"))
        .and_then(|f| f.as_str());
    let anthropic = json.get("stop_reason").and_then(|f| f.as_str());
    matches!(oai, Some("length") | Some("max_tokens")) || anthropic == Some("max_tokens")
}

// =============================================================================
// Core LLM Call — SSE Streaming (replaces genai client.exec_chat_stream)
// =============================================================================

// =============================================================================
// DIRECT SYNTHESIS — Single-call for lite mode
// =============================================================================

// =============================================================================
// v5.2.0: INTENT-AWARE SYNTHESIS — Selects optimal prompt per query intent
// =============================================================================

// =============================================================================
// ITERATIVE DEEP RESEARCH — Multi-batch synthesis for long-form reports
//
// Flow:
//   1. Split all sources into batches of 50
//   2. Batch 1: Generate initial comprehensive report
//   3. Batch 2+: Send previous_report + new sources → LLM expands
//   4. Return final accumulated long-form report
// =============================================================================

// =============================================================================
// CHANNEL-BASED SYNTHESIS — Used by stream.rs (SSE streaming path)
// =============================================================================

// =============================================================================
// Context Building
// =============================================================================

/// Context block plus the mapping needed to resolve `[n]` back to a source.
pub struct BuiltContext {
    pub text: String,
    /// citation number (1-based) -> index into the `sources` slice passed in.
    pub citation_map: Vec<usize>,
}

impl BuiltContext {
    pub fn is_empty(&self) -> bool {
        self.text.is_empty()
    }
    /// Resolve a `[n]` citation to its source.
    pub fn resolve<'a>(&self, n: usize, sources: &'a [SourceResult]) -> Option<&'a SourceResult> {
        let idx = *self.citation_map.get(n.checked_sub(1)?)?;
        sources.get(idx)
    }
}

/// Assemble the LLM context from an ALREADY-RANKED source list.
///
/// ═══════════════════════════════════════════════════════════════════════════
/// v8.0 P1-6: this function no longer re-ranks.
///
/// v7.0 ran `ranking::rank_top_chunks_with_rrf` (BM25+ -> RRF boost -> MMR
/// diversity -> lost-in-middle) and then threw that ordering away here,
/// re-sorting by:
///
/// ```text
/// if src.title.to_lowercase().contains(kw) { score += 3.0 }
/// if text_lower.contains(kw)               { score += 1.0 }
/// ```
///
/// Substring containment. No IDF, no term frequency, no length normalization —
/// a strictly worse ranker, applied last, so it won. The entire BM25+/MMR/
/// lost-in-middle stack was decorative.
///
/// v8.0 P0-11: it also returns a citation map.
///
/// v7.0 numbered the sources `[1]`, `[2]`, ... by their position in the
/// REORDERED list, then `verify_citations` resolved `[n]` against
/// `sources[n-1]` — the original, unreordered slice. Index 0 coincides under
/// the sandwich reorder, but every index from 1 onward pointed at the wrong
/// source. Citations were systematically misattributed.
/// ═══════════════════════════════════════════════════════════════════════════
#[cfg(test)]
pub(crate) fn build_ranked_context(sources: &[SourceResult], budget_chars: usize) -> BuiltContext {
    build_ranked_context_with_intel(sources, budget_chars, None)
}

pub(crate) fn build_ranked_context_with_intel(
    sources: &[SourceResult],
    budget_chars: usize,
    intel: Option<&QueryIntelligence>,
) -> BuiltContext {
    if sources.is_empty() {
        return BuiltContext {
            text: String::new(),
            citation_map: Vec::new(),
        };
    }

    // Respect the incoming order; only decide WHERE each source sits.
    let order = lost_in_middle_order(sources.len());

    let mut text = String::with_capacity(budget_chars.min(64 * 1024));
    let mut citation_map: Vec<usize> = Vec::new();
    let mut used = 0usize;
    let mut seen_sentence_hashes: HashSet<u64> = HashSet::new();
    let entity_tokens: Vec<String> = intel
        .map(|it| {
            it.key_entities
                .iter()
                .flat_map(|e| e.split_whitespace())
                .map(|t| {
                    t.trim_matches(|c: char| !crate::util::is_word_char(c))
                        .to_lowercase()
                })
                .filter(|t| t.chars().count() >= 3)
                .collect()
        })
        .unwrap_or_default();

    for idx in order {
        let src = &sources[idx];
        let body_raw = src.extracted_text.trim();
        if body_raw.is_empty() {
            continue;
        }

        let remaining = budget_chars.saturating_sub(used);
        // Reserve room for the header line.
        if remaining < 400 {
            break;
        }
        let header_budget = 240;
        let body_budget = remaining.saturating_sub(header_budget);

        let pruned_body = prune_and_dedup_chunk_sentences(
            body_raw,
            &entity_tokens,
            &mut seen_sentence_hashes,
            body_budget,
        );
        let body = crate::util::truncate_chars(&pruned_body, body_budget);
        if crate::util::char_len(body) < 80 {
            continue;
        }

        let n = citation_map.len() + 1;
        let cred = credibility_tag_for_source(src, intel);
        let date = src
            .published
            .map(|d| d.format("%Y-%m-%d").to_string())
            .unwrap_or_else(|| "undated".to_string());

        let _ = write!(
            text,
            "[{}] {} {} — published {} ({})\n{}\n\n",
            n, cred, src.title, date, src.url, body
        );
        used = crate::util::char_len(&text);
        citation_map.push(idx);
    }

    BuiltContext { text, citation_map }
}

/// Pre-LLM sentence pruning and cross-chunk deduplication.
/// 1. Deduplicates repeated multi-word sentences (>= 6 words) across chunks using a 64-bit hash.
/// 2. Prunes zero-signal filler sentences from oversized chunks while preserving
///    dates, entity mentions, numbers/versions, headings/tables, and lead sentences.
pub(crate) fn prune_and_dedup_chunk_sentences(
    text: &str,
    entity_tokens: &[String],
    seen_hashes: &mut HashSet<u64>,
    max_chars: usize,
) -> String {
    use std::hash::{Hash, Hasher};

    let raw_len = crate::util::char_len(text);
    if raw_len <= 320 {
        return crate::util::truncate_chars(text, max_chars).to_string();
    }

    // Split into lines/sentences while keeping structure
    let mut segments: Vec<&str> = Vec::new();
    for line in text.lines() {
        let l = line.trim();
        if l.is_empty() {
            continue;
        }
        if l.starts_with('#') || l.starts_with('|') || l.starts_with("[Entry Date:") {
            segments.push(l);
            continue;
        }
        let mut start = 0;
        let bytes = l.as_bytes();
        for i in 0..bytes.len() {
            if matches!(bytes[i], b'.' | b'!' | b'?')
                && (i + 1 == bytes.len() || bytes[i + 1] == b' ')
            {
                let seg = l[start..=i].trim();
                if !seg.is_empty() {
                    segments.push(seg);
                }
                start = i + 1;
            }
        }
        if start < l.len() {
            let tail = l[start..].trim();
            if !tail.is_empty() {
                segments.push(tail);
            }
        }
    }

    if segments.len() <= 2 {
        return crate::util::truncate_chars(text, max_chars).to_string();
    }

    let mut kept: Vec<&str> = Vec::with_capacity(segments.len());
    let mut kept_chars = 0usize;

    for (seg_idx, seg) in segments.iter().copied().enumerate() {
        let words: Vec<String> = seg
            .split(|c: char| !crate::util::is_word_char(c))
            .filter(|w| w.chars().count() >= 2)
            .map(|w| w.to_lowercase())
            .collect();

        // Cross-chunk deduplication for substantial sentences (>= 6 words)
        if words.len() >= 6 {
            let mut hasher = std::collections::hash_map::DefaultHasher::new();
            for w in &words {
                w.hash(&mut hasher);
            }
            let sig = hasher.finish();
            if !seen_hashes.insert(sig) {
                continue;
            }
        }

        // For long chunks (> 500 chars), once we have at least 120 chars of core
        // content, prune mid-body filler sentences that carry zero entity tokens,
        // zero digits/dates/versions, and aren't headings/tables/lead.
        if raw_len > 500 && seg_idx > 0 && kept_chars >= 120 && !entity_tokens.is_empty() {
            let is_structural =
                seg.starts_with('#') || seg.starts_with('|') || seg.starts_with("[Entry Date:");
            let has_digit = seg.chars().any(|c| c.is_ascii_digit());
            let seg_lower = seg.to_lowercase();
            let has_entity = entity_tokens
                .iter()
                .any(|et| seg_lower.contains(et.as_str()));
            if !is_structural && !has_digit && !has_entity && words.len() < 10 {
                continue;
            }
        }

        let seg_len = crate::util::char_len(seg);
        if kept_chars + seg_len + 1 > max_chars && kept_chars >= 120 {
            break;
        }
        kept.push(seg);
        kept_chars += seg_len + 1;
    }

    let joined = kept.join(" ");
    if kept.is_empty() {
        crate::util::truncate_chars(text, max_chars).to_string()
    } else {
        joined
    }
}

/// Lost-in-Middle ordering (Liu et al., TACL 2024, arXiv 2307.03172).
///
/// Models show a U-shaped accuracy curve over context position, so the most
/// relevant items go first and last and the least relevant in the middle.
/// Returns indices into a relevance-descending list.
fn lost_in_middle_order(len: usize) -> Vec<usize> {
    if len <= 2 {
        return (0..len).collect();
    }
    let mut front = Vec::with_capacity(len);
    let mut back = Vec::with_capacity(len);
    for i in 0..len {
        if i % 2 == 0 {
            front.push(i);
        } else {
            back.push(i);
        }
    }
    back.reverse();
    front.extend(back);
    front
}

/// Algorithmically matches query entities against a URL's hostname labels and
/// extracted `site_name` without any hardcoded domain lists.
/// Returns `(matched_entity_tokens, total_entity_tokens)`.
pub fn url_matches_query_entities(
    url: &str,
    site_name: Option<&str>,
    entities: &[String],
) -> (usize, usize) {
    let (host, _) = crate::modes::parse_host_and_path(url);
    if host.is_empty() || entities.is_empty() {
        return (0, 0);
    }

    // Collect distinct normalized entity tokens (len >= 3)
    let mut entity_tokens: Vec<String> = Vec::new();
    for ent in entities {
        for raw_tok in ent.split(|c: char| !c.is_alphanumeric()) {
            let clean = raw_tok.trim().to_lowercase();
            if clean.chars().count() >= 3 && !entity_tokens.contains(&clean) {
                entity_tokens.push(clean);
            }
        }
    }
    if entity_tokens.is_empty() {
        return (0, 0);
    }

    // Split host into meaningful labels (excluding generic DNS TLD/prefix tokens
    // unless a brand TLD itself matches an entity token like `.google`).
    let host_labels: Vec<String> = host
        .trim_start_matches("www.")
        .split(['.', '-', '_'])
        .map(|s| s.trim().to_lowercase())
        .filter(|s| s.len() >= 2)
        .collect();
    let reg_domain = crate::url_utils::registrable_domain(url);
    let reg_concat: String = reg_domain
        .split('.')
        .next()
        .unwrap_or("")
        .chars()
        .filter(|c| c.is_alphanumeric())
        .collect();

    let site_tokens: Vec<String> = site_name
        .unwrap_or("")
        .split(|c: char| !c.is_alphanumeric())
        .map(|s| s.trim().to_lowercase())
        .filter(|s| s.chars().count() >= 3)
        .collect();

    let mut matched = 0usize;
    for et in &entity_tokens {
        let host_hit = host_labels.iter().any(|hl| {
            hl == et
                || (hl.len() >= 4 && et.len() >= 4 && (hl.starts_with(et) || et.starts_with(hl)))
        }) || (!reg_concat.is_empty()
            && reg_concat.len() >= 4
            && (reg_concat == *et || reg_concat.contains(et.as_str())));

        // Accept site_name match when the host root is consistent with site_name
        let site_hit = !site_tokens.is_empty()
            && site_tokens.iter().any(|st| st == et)
            && host_labels.iter().any(|hl| {
                site_tokens
                    .iter()
                    .any(|st| st == hl || (hl.len() >= 3 && st.starts_with(hl)))
            });

        if host_hit || site_hit {
            matched += 1;
        }
    }

    (matched, entity_tokens.len())
}

/// Checks if a URL is an encyclopedia (e.g. Wikipedia/Britannica/Wikibooks)
/// based on its registrable root and `/wiki/` path structure.
pub fn is_encyclopedia_url(url: &str, site_name: Option<&str>) -> bool {
    let (host, path) = crate::modes::parse_host_and_path(url);
    if host.is_empty() {
        return false;
    }
    let reg = crate::url_utils::registrable_domain(url);
    let root = reg.split('.').next().unwrap_or("");
    let site_lower = site_name.unwrap_or("").to_lowercase();
    ((root == "wikipedia" || root == "britannica" || root == "wikibooks")
        && (path.starts_with("/wiki/") || path.starts_with("/topic/") || path.len() > 1))
        || site_lower == "wikipedia"
}

/// Dynamic source credibility label for the LLM prompt context (zero hardcoded site lists).
pub(crate) fn credibility_tag_for_source(
    src: &SourceResult,
    intel: Option<&QueryIntelligence>,
) -> String {
    let (host, path) = crate::modes::parse_host_and_path(&src.url);
    let clean_host = host.trim_start_matches("www.");
    if clean_host.is_empty() {
        return "[Web Source - unknown]".to_string();
    }

    if let Some(it) = intel {
        let (matched, total) =
            url_matches_query_entities(&src.url, src.site_name.as_deref(), &it.key_entities);
        if matched > 0 {
            if matched >= 2 || (matched == 1 && total == 1) {
                return format!("[Official Primary Source - {}]", clean_host);
            } else {
                return format!("[Official Vendor Source - {}]", clean_host);
            }
        }
    }

    if crate::modes::is_institutional_host(&host) {
        return format!("[High Trust - Institutional - {}]", clean_host);
    }
    if crate::modes::is_structural_docs_url(&host, &path) {
        return format!("[Official Documentation - {}]", clean_host);
    }
    if is_encyclopedia_url(&src.url, src.site_name.as_deref()) {
        return format!("[Reference Encyclopedia - {}]", clean_host);
    }
    if let Some(ref sn) = src.site_name {
        let sn_trim = sn.trim();
        if !sn_trim.is_empty() && !sn_trim.eq_ignore_ascii_case(clean_host) {
            return format!("[Web Source - {} ({})]", sn_trim, clean_host);
        }
    }
    format!("[General Web - {}]", clean_host)
}

pub(crate) fn credibility_tag(url: &str) -> String {
    let dummy = SourceResult {
        url: url.to_string(),
        ..Default::default()
    };
    credibility_tag_for_source(&dummy, None)
}

/// Universal dynamic authority score combining:
/// 1. Entity-to-domain primary source matching (`1.45x–1.75x`)
/// 2. Third-party aggregator demotion (`0.78x`) whenever an official entity domain exists in the pool
/// 3. Wikipedia/encyclopedia secondary default (`1.0x`), demoted on live/temporal updates (`0.85x`),
///    and boosted (`1.35x`) ONLY on `Mode::Study` or `SemanticCategory::HistoryStatic`
/// 4. Structural institutional (`.gov`/`.edu`/`.ac.*`/`.int`) and documentation (`docs.*`/`/docs/`) signals
pub fn dynamic_authority_score(
    url: &str,
    site_name: Option<&str>,
    title: &str,
    intel: Option<&QueryIntelligence>,
    mode: crate::modes::Mode,
    has_official_in_pool: bool,
) -> f64 {
    use crate::query_intel::{QueryIntent, SemanticCategory};

    let (host, path) = crate::modes::parse_host_and_path(url);
    if host.is_empty() {
        return 1.0;
    }

    // 1. Check Wikipedia / Encyclopedia rules first: secondary (1.0x) by default
    if is_encyclopedia_url(url, site_name) {
        if mode == crate::modes::Mode::Study {
            return 1.35;
        }
        if let Some(it) = intel {
            let wants_encyclopedia = it
                .key_entities
                .iter()
                .any(|e| e.to_lowercase().contains("wikipedia"));
            if wants_encyclopedia {
                return 1.35;
            }
            if it.is_time_sensitive
                || it.intent == QueryIntent::Temporal
                || it.semantic_category == SemanticCategory::LiveNews
                || (it.semantic_category == SemanticCategory::ProductOrg && it.is_time_sensitive)
            {
                return 0.85;
            }
            if it.semantic_category == SemanticCategory::HistoryStatic && !it.is_time_sensitive {
                return 1.35;
            }
        }
        return 1.0;
    }

    let is_inst = crate::modes::is_institutional_host(&host);
    let is_docs = crate::modes::is_structural_docs_url(&host, &path);
    let is_release_path = path.contains("/changelog")
        || path.contains("/release-notes")
        || path.contains("/releases")
        || path.contains("/announcements")
        || path.contains("/updates")
        || path.starts_with("/blog")
        || path.starts_with("/news");

    // 2. Dynamic Entity-Host Matching
    if let Some(it) = intel {
        let (matched, total) = url_matches_query_entities(url, site_name, &it.key_entities);
        if matched > 0 {
            let mut mult: f64 = if matched >= 2 || (matched == 1 && total == 1) {
                1.60
            } else {
                1.42
            };
            if is_docs || is_release_path || is_inst {
                mult += 0.15;
            }
            return mult.min(1.75);
        }

        // 3. Dynamic Third-Party Aggregator Demotion when an official domain is already in the pool
        if has_official_in_pool && !is_inst && total > 0 {
            let hay = format!("{} {}", title.to_lowercase(), path);
            let mentions_entity = it.key_entities.iter().any(|e| {
                e.split_whitespace()
                    .filter(|t| t.chars().count() >= 3)
                    .any(|t| hay.contains(&t.to_lowercase()))
            });
            if mentions_entity {
                return 0.78;
            }
        }
    }

    // 4. Structural TLD & Documentation authority (zero hardcoded site lists)
    if is_inst {
        return 1.45;
    }
    if is_docs {
        return 1.30;
    }

    1.0
}

/// Structural baseline trust score (zero hardcoded domain lists).
/// Wikipedia is 1.0 (secondary) by default; institutional (.gov/.edu/.int/.ac.*)
/// is 1.45; structural documentation (`docs.*`, `/docs/`, `/std/`) is 1.30.
pub fn trust_score(url: &str) -> f32 {
    dynamic_authority_score(url, None, "", None, crate::modes::Mode::Auto, false) as f32
}

fn ensure_trailing_slash(url: &str) -> String {
    if url.ends_with('/') {
        url.to_string()
    } else {
        format!("{}/", url)
    }
}

// =============================================================================
// Prompt Builders — with time awareness via chrono
// =============================================================================

pub(crate) fn current_datetime_str() -> String {
    let now = chrono::Utc::now();
    format!(
        "{} (Year: {})",
        now.format("%A, %B %d, %Y at %H:%M UTC"),
        now.format("%Y")
    )
}

// =============================================================================
// Answer Post-Processing — strip preamble, question echoing, thinking blocks
// =============================================================================

pub(crate) fn post_process_answer(raw: &str, _query: &str) -> String {
    let mut answer = raw.trim().to_string();

    // 1. Strip <think>/<thought>/<thinking> blocks (various models emit internal reasoning)
    let think_patterns = [
        (r"<think>", r"</think>"),
        (r"<thought>", r"</thought>"),
        (r"<thinking>", r"</thinking>"),
    ];
    for (open, close) in &think_patterns {
        while let Some(start) = answer.to_lowercase().find(&open.to_lowercase()) {
            if let Some(end) = answer.to_lowercase().find(&close.to_lowercase()) {
                let end_pos = end + close.len();
                answer = format!("{}{}", &answer[..start], &answer[end_pos..]);
            } else {
                // Unclosed tag — remove from start to end
                answer = answer[..start].to_string();
                break;
            }
        }
    }

    // 2. Strip common LLM preamble lines (case-insensitive, first line only)
    let preamble_patterns = [
        "based on the search results",
        "based on the provided sources",
        "based on the sources provided",
        "according to the search results",
        "according to the sources",
        "here is what i found",
        "here's what i found",
        "let me summarize",
        "i found the following",
        "from the search results",
        "based on my research",
        "here is a summary",
        "here's a summary",
    ];

    // Check and strip preamble from the first line
    let first_newline = answer.find('\n').unwrap_or(answer.len());
    let first_line_lower = answer[..first_newline].to_lowercase();
    for pattern in &preamble_patterns {
        if first_line_lower.starts_with(pattern) || first_line_lower.contains(pattern) {
            // If the first line is ONLY preamble (no substantive content), remove it
            if first_newline < answer.len() {
                let rest = answer[first_newline..].trim_start();
                if !rest.is_empty() {
                    answer = rest.to_string();
                }
            }
            break;
        }
    }

    // 3. Normalize full-width citation brackets (e.g. 【5】 or [5] -> [5])
    if answer.contains('【')
        || answer.contains('】')
        || answer.contains('[')
        || answer.contains(']')
    {
        answer = answer
            .replace('【', "[")
            .replace('】', "]")
            .replace('[', "[")
            .replace(']', "]");
    }

    answer.trim().to_string()
}

/// v6.1.0: Citation faithfulness verification.
/// Check that each cited [n] in the LLM answer has token overlap with the
/// actual source content. Flag unsupported citations.
///
/// This is the main defense against confident-but-hallucinated attributions.
/// Returns the answer with unsupported citations flagged.
/// Outcome of checking one `[n]` citation against the source it points at.
#[derive(Debug, Clone, serde::Serialize)]
pub struct CitationCheck {
    /// The number as it appears in the answer.
    pub number: usize,
    pub url: String,
    pub title: String,
    /// Fraction of the claim's content words that appear in the source.
    pub overlap: f64,
    pub supported: bool,
}

/// Citation faithfulness check.
///
/// ═══════════════════════════════════════════════════════════════════════════
/// v8.0 fixes two things.
///
/// 1. It RESOLVES CORRECTLY. v7.0 mapped `[n]` to `sources[n-1]`, but the
///    numbers in the answer refer to positions in the lost-in-middle REORDERED
///    context. Index 0 coincides; everything from `[2]` onward pointed at the
///    wrong source. Now it goes through the `citation_map` that built the
///    context.
///
/// 2. It RETURNS SOMETHING. v7.0's body ended with `result` — the unmodified
///    input — so despite its doc comment ("Returns the answer with unsupported
///    citations flagged") it only wrote log lines. It was a no-op that cost a
///    full substring scan per citation, called twice per answer.
///
/// ═══════════════════════════════════════════════════════════════════════════
pub fn verify_citations(
    answer: &str,
    sources: &[SourceResult],
    citation_map: &[usize],
) -> Vec<CitationCheck> {
    if sources.is_empty() || answer.is_empty() || citation_map.is_empty() {
        return Vec::new();
    }

    let mut out: Vec<CitationCheck> = Vec::new();
    let mut seen_numbers: HashSet<usize> = HashSet::new();

    for (n, sentence) in extract_citation_contexts(answer) {
        if !seen_numbers.insert(n) {
            continue; // report each citation number once
        }
        // n is 1-based into the CONTEXT, not into `sources`.
        let Some(&src_idx) = citation_map.get(n.wrapping_sub(1)) else {
            continue;
        };
        let Some(src) = sources.get(src_idx) else {
            continue;
        };

        let source_text = format!("{} {}", src.title, src.extracted_text).to_lowercase();
        let claim = sentence.to_lowercase();
        let claim_tokens: HashSet<&str> = claim
            .split(|c: char| !crate::util::is_word_char(c))
            .filter(|t| crate::util::char_len(t) > 3)
            .collect();
        if claim_tokens.is_empty() {
            continue;
        }
        let hits = claim_tokens
            .iter()
            .filter(|t| source_text.contains(**t))
            .count();
        let overlap = hits as f64 / claim_tokens.len() as f64;

        out.push(CitationCheck {
            number: n,
            url: src.url.clone(),
            title: src.title.clone(),
            overlap,
            supported: overlap >= 0.25,
        });
    }

    let unsupported = out.iter().filter(|c| !c.supported).count();
    if unsupported > 0 {
        tracing::warn!(
            "Citation check: {} of {} citations have <25% token overlap with their source",
            unsupported,
            out.len()
        );
    }
    out
}

/// Find every `[n]` in the answer and the sentence containing it.
/// Byte-boundary-safe throughout.
fn extract_citation_contexts(answer: &str) -> Vec<(usize, String)> {
    let chars: Vec<(usize, char)> = answer.char_indices().collect();
    let mut found = Vec::new();
    let mut i = 0usize;

    while i < chars.len() {
        if chars[i].1 != '[' {
            i += 1;
            continue;
        }
        let bracket_byte = chars[i].0;
        let mut j = i + 1;
        let mut num = String::new();
        while j < chars.len() && chars[j].1.is_ascii_digit() {
            num.push(chars[j].1);
            j += 1;
        }
        if j >= chars.len() || chars[j].1 != ']' || num.is_empty() {
            i += 1;
            continue;
        }
        if let Ok(n) = num.parse::<usize>() {
            let after = chars[j].0 + chars[j].1.len_utf8();
            // Sentence bounds, snapped to char boundaries.
            let start = answer[..bracket_byte]
                .rfind(". ")
                .map(|p| p + 2)
                .unwrap_or(0);
            let end = answer
                .get(after..)
                .and_then(|rest| rest.find(". ").map(|p| after + p + 1))
                .unwrap_or(answer.len());
            let (s, e) = (snap(answer, start), snap(answer, end.min(answer.len())));
            if s < e {
                found.push((n, answer[s..e].to_string()));
            }
        }
        i = j + 1;
    }
    found
}

/// Snap a byte offset down to the nearest char boundary.
fn snap(s: &str, mut idx: usize) -> usize {
    if idx > s.len() {
        return s.len();
    }
    while idx > 0 && !s.is_char_boundary(idx) {
        idx -= 1;
    }
    idx
}

// =============================================================================
// v5.2.0: INTENT-AWARE PROMPT BUILDER
// Selects optimal system/user prompt template based on QueryIntelligence
// =============================================================================

pub(crate) fn build_intent_prompts(
    query: &str,
    context: &str,
    intel: &QueryIntelligence,
) -> (String, String) {
    let now = current_datetime_str();
    let year = chrono::Utc::now().format("%Y").to_string();

    let (intent_instruction, format_instruction) = match &intel.intent {
        QueryIntent::Factual => (
            "Provide a clear, balanced, medium-length answer. Frame the core fact and context up \
             front, then cover the essential details, numbers, and specifications cleanly without \
             bloated prose."
                .to_string(),
            "Structure the response naturally (~160-260 words total):\n\
             1. Start with a concise Markdown heading (`## ...`) or 1-2 sentence opening that states \
             the core answer and frames the context [n].\n\
             2. Follow with 3-5 well-organized bullet points (1-2 crisp sentences each) covering key \
             facts, figures, or specifications with inline [n] citations.\n\
             3. Close with a brief 1-2 sentence concluding paragraph that ties the main points \
             together naturally for the reader — do not use robotic labels like 'Summary:' or \
             'Conclusion:'."
                .to_string(),
        ),

        QueryIntent::Temporal => (
            format!(
                "TODAY IS {}. The user wants CURRENT, UP-TO-DATE information from {}. \
                STRICT TEMPORAL SCOPE: If the question asks for a specific recent window ('past few days', \
                'this week', 'today', 'right now', 'latest'), include ONLY updates whose published or \
                [Entry Date] falls within that recent window. Strictly OMIT older items from months earlier \
                (e.g., do not pad a recent news list with items from earlier quarters/months).",
                now, year
            ),
            "Structure the response naturally (~160-280 words total) covering ONLY genuinely recent \
             updates matching the user's timeframe:\n\
             1. Start with a clean Markdown heading (`## ...`) and a 1-2 sentence opening framing \
             the current situation or headline development [n].\n\
             2. Present 3-5 well-organized bullet points (1-2 crisp sentences each) with exact dates, \
             version/feature names, and inline [n] citations. Never pad with old or undated filler.\n\
             3. End with a brief 1-2 sentence concluding paragraph tying together what these recent \
             updates mean overall — do not use robotic labels like 'Summary:' or 'Conclusion:'."
                .to_string(),
        ),

        QueryIntent::Person => (
            format!(
                "The user is asking about a SPECIFIC PERSON. Provide a balanced, medium-length profile as of {}.",
                now
            ),
            "Structure the profile cleanly (~180-280 words total):\n\
             1. Start with a concise Markdown heading (`## ...`) and a 1-2 sentence opening \
             introducing who they are and their primary role today [n].\n\
             2. Use clear, well-organized bullet points covering **Current Role / Latest Activity** \
             (with exact dates) and **Key Background & Achievements**, citing each claim inline as [n].\n\
             3. Finish with a brief 1-2 sentence concluding paragraph capturing their broader impact \
             or current trajectory — do not use robotic labels like 'Summary:' or 'Conclusion:'."
                .to_string(),
        ),

        QueryIntent::Comparison => (
            "The user wants a clear, well-structured COMPARISON. Frame both sides clearly, compare \
             the core dimensions present in the evidence concisely, and finish with a helpful \
             takeaway without repetitive walls of text."
                .to_string(),
            "Structure the comparison cleanly (~230-380 words total):\n\
             1. Open with a concise Markdown heading (`## ...`) and 1-2 framing sentences defining \
             both sides and their core distinction [n].\n\
             2. Include a compact Markdown comparison table of the main dimensions supported by the \
             sources, followed by 2-3 clear, well-organized bullet points per side covering key \
             strengths and trade-offs with inline [n] citations.\n\
             3. Close with a brief 1-2 sentence concluding paragraph tying the trade-offs together \
             and noting which fits which use case — do not use robotic labels like 'Summary:', \
             'Conclusion:', or 'Bottom Line:'."
                .to_string(),
        ),

        QueryIntent::HowTo => (
            "The user wants PRACTICAL INSTRUCTIONS. Frame the approach clearly, provide actionable \
             guidance, and close with a concise wrap-up."
                .to_string(),
            "Structure the guide cleanly (~160-280 words total):\n\
             1. Start with a concise Markdown heading (`## ...`) and a 1-2 sentence opening framing \
             the approach and any prerequisites [n].\n\
             2. Present 3-6 clear, well-organized bullet points or numbered steps (1-2 crisp \
             sentences each) with exact commands, settings, or parameters and inline [n] citations.\n\
             3. End with a brief 1-2 sentence concluding paragraph summarizing the expected outcome \
             or key tip to keep in mind — do not use robotic labels like 'Summary:' or 'Conclusion:'."
                .to_string(),
        ),

        QueryIntent::Research | QueryIntent::Navigational => (
            format!(
                "Provide a balanced, well-structured synthesis of the key facts, mechanisms, or \
                 historical milestones with smooth, reader-friendly flow. Date: {}.",
                now
            ),
            "Structure the response naturally (~180-300 words total):\n\
             1. Open with a clean Markdown heading (`## ...`) and a 1-2 sentence opening framing \
             the core answer and context [n].\n\
             2. Present 4-6 well-organized bullet points (1-2 crisp sentences each) covering the most \
             important facts, mechanisms, dates, or strategies with inline [n] citations.\n\
             3. End with a brief 1-2 sentence concluding paragraph tying the main points together \
             cleanly for the reader — do not use robotic labels like 'Summary:' or 'Conclusion:'."
                .to_string(),
        ),
    };

    let system = format!(
        "[SYSTEM INSTRUCTIONS]\n\
         You are SearchWala's balanced synthesis engine. TODAY: {now} (Category: {cat}, Intent: {intent}).\n\
         {intent_instruction}\n\
         Follow this strict 3-step reasoning order:\n\
         1. VERIFY AUTHORITY & ENTRY DATE: Check each source's [Official Primary Source] / [Official Vendor Source] tag, published date, and any [Entry Date: YYYY-MM-DD] first.\n\
         2. ENFORCE TEMPORAL SCOPE: Anchor 'latest' or 'past few days' claims strictly to the requested recent window; omit older historical entries from months earlier rather than using them as padding.\n\
         3. SYNTHESIZE WITH CLEAR FLOW: Keep Auto-mode responses balanced and reader-friendly (~160-300 words, or up to ~380 words with a table for comparisons). Use ONLY the provided sources and cite claims inline with [n]. Always follow a smooth 3-part narrative flow: (a) a clean heading or brief opening framing the core answer and context, (b) well-organized bullet points with inline [n] citations, and (c) a brief, natural concluding wrap-up paragraph tying everything together without robotic labels like 'Summary:' or 'Conclusion:'. Never append a trailing 'Sources Used' or raw URL list.{lang}",
        cat = intel.semantic_category,
        intent = intel.intent,
        lang = LANGUAGE_RULE
    );

    let user = format!(
        "[VERIFIED EVIDENCE]\n\
         {context}\n\
         ---\n\
         [USER QUESTION]\n\
         {query}\n\n\
         {format_instruction}\n\
         Cite sources inline as [n] only; do not append a trailing 'Sources Used' or URL list."
    );

    (system, user)
}

// =============================================================================
// SSE Streaming Synthesis — Used by stream.rs
// =============================================================================

// =============================================================================
// Dynamic Model Fetcher — GET /api/models
//
// Called by the UI "Fetch Models" button. base_url comes from the frontend
// presets (e.g. "https://api.groq.com/openai/v1") and already includes the
// version path. We just append "/models".
// =============================================================================

pub async fn fetch_provider_models(
    api_key: &str,
    base_url: &str,
    provider: &str,
) -> Result<Vec<String>, String> {
    let provider = provider.trim().to_lowercase();
    let api_key = api_key.trim().trim_start_matches("Bearer ").trim();
    // No base_url: use the provider's own endpoint. The UI presets for
    // OpenAI, Anthropic and Gemini have none, so "Fetch list" used to refuse
    // to run for exactly the most common providers.
    let base = if base_url.trim().is_empty() {
        default_provider_for(&provider).base_url
    } else {
        normalize_base_url(base_url, &provider)
    };
    let probe = LlmConfig {
        provider: provider.clone(),
        api_key: api_key.to_string(),
        model: String::new(),
        base_url: Some(base.clone()),
        timeout_ms: None,
        max_output_tokens: None,
        context_window: None,
    };
    if api_key.is_empty() && !is_local_llm(&probe) {
        return Err("an API key is required".to_string());
    }
    if api_key
        .chars()
        .any(|c| c.is_whitespace() || c.is_control() || !c.is_ascii())
    {
        return Err("the API key contains spaces, line breaks or non-ASCII characters".to_string());
    }

    let is_anthropic = provider == "anthropic";
    let models_url = if is_anthropic {
        format!(
            "{}v1/models",
            ensure_trailing_slash(base.trim_end_matches("/v1"))
        )
    } else {
        format!("{}models", ensure_trailing_slash(&base))
    };
    tracing::info!("Fetching models from: {}", models_url);

    let client = reqwest::Client::builder()
        .timeout(Duration::from_secs(15))
        .redirect(reqwest::redirect::Policy::none())
        .build()
        .map_err(|e| format!("client_error: {e}"))?;

    let mut req = client.get(&models_url).header("Accept", "application/json");

    if is_anthropic {
        req = req
            .header("x-api-key", api_key)
            .header("anthropic-version", "2023-06-01");
    } else if !api_key.is_empty() {
        req = req.header("Authorization", format!("Bearer {}", api_key));
    }

    let resp = req
        .send()
        .await
        .map_err(|e| redact_key(&format!("request_failed: {e}"), api_key))?;

    if !resp.status().is_success() {
        let status = resp.status().as_u16();
        let err_text = resp.text().await.unwrap_or_default();
        let detail = match serde_json::from_str::<serde_json::Value>(&err_text) {
            Ok(json) => extract_error_message(&json, &ApiFormat::OpenAiCompatible),
            Err(_) => err_text.chars().take(200).collect(),
        };
        return Err(redact_key(&format!("http_{}: {}", status, detail), api_key));
    }

    let json: serde_json::Value = resp
        .json()
        .await
        .map_err(|e| format!("json_parse_error: {e}"))?;

    let mut models: Vec<String> = Vec::new();

    // Format 1: OpenAI-compatible — { "data": [ { "id": "model-name" }, ... ] }
    if let Some(data) = json.get("data").and_then(|v| v.as_array()) {
        for item in data {
            if let Some(id) = item.get("id").and_then(|v| v.as_str()) {
                let id = id.trim();
                if !id.is_empty() {
                    // Strip "models/" prefix if present (Gemini OpenAI-compat returns this)
                    let clean_id = id.strip_prefix("models/").unwrap_or(id);
                    models.push(clean_id.to_string());
                }
            }
        }
    }

    // Format 2: Gemini native — { "models": [ { "name": "models/gemini-2.0-flash", ... } ] }
    if models.is_empty() {
        if let Some(data) = json.get("models").and_then(|v| v.as_array()) {
            for item in data {
                // Only include models that support generateContent
                let methods = item
                    .get("supportedGenerationMethods")
                    .and_then(|v| v.as_array());
                let supports_chat = methods
                    .map(|ms| ms.iter().any(|m| m.as_str() == Some("generateContent")))
                    .unwrap_or(true);

                if supports_chat {
                    if let Some(name) = item.get("name").and_then(|v| v.as_str()) {
                        let name = name.trim();
                        if !name.is_empty() {
                            // Strip "models/" prefix
                            let clean = name.strip_prefix("models/").unwrap_or(name);
                            models.push(clean.to_string());
                        }
                    }
                }
            }
        }
    }

    models.sort();
    tracing::info!("Fetched {} models from {}", models.len(), base_url);
    Ok(models)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::models::SourceResult;

    fn src(url: &str, title: &str, text: &str) -> SourceResult {
        SourceResult {
            url: url.into(),
            title: title.into(),
            extracted_text: text.into(),
            char_count: crate::util::char_len(text),
            engine: "test".into(),
            confidence: 1.0,
            ..Default::default()
        }
    }

    // ── P0-11: the citation index bug ──

    #[test]
    fn citation_map_round_trips_through_lost_in_middle_reorder() {
        // Five distinct sources; the context reorders them into a sandwich.
        // Every [n] in the answer must resolve to the source the MODEL saw as
        // [n] — v7.0 resolved against the unreordered slice, so [2] onward
        // pointed at the wrong source.
        let sources: Vec<SourceResult> = (0..5)
            .map(|i| {
                src(
                    &format!("https://s{i}.test/a"),
                    &format!("Title {i}"),
                    &format!(
                        "Body of source number {i}. {}",
                        "filler words here. ".repeat(20)
                    ),
                )
            })
            .collect();

        let ctx = build_ranked_context(&sources, 100_000);
        assert_eq!(ctx.citation_map.len(), 5, "all five should fit the budget");

        for n in 1..=5 {
            let resolved = ctx.resolve(n, &sources).expect("must resolve");
            // The context block labelled [n] must contain THIS source's url.
            let marker = format!("[{n}] ");
            let pos = ctx.text.find(&marker).expect("label present");
            let block_end = ctx.text[pos + marker.len()..]
                .find("\n\n")
                .map(|e| pos + marker.len() + e)
                .unwrap_or(ctx.text.len());
            let block = &ctx.text[pos..block_end];
            assert!(
                block.contains(&resolved.url),
                "citation [{n}] resolves to {} but its context block is {:?}",
                resolved.url,
                block.chars().take(90).collect::<String>()
            );
        }
    }

    #[test]
    fn lost_in_middle_puts_best_first_and_second_best_last() {
        let order = lost_in_middle_order(5);
        assert_eq!(order.first(), Some(&0), "most relevant must lead");
        assert_eq!(order.last(), Some(&1), "second most relevant must close");
        // Every index present exactly once.
        let mut sorted = order.clone();
        sorted.sort_unstable();
        assert_eq!(sorted, vec![0, 1, 2, 3, 4]);
    }

    #[test]
    fn lost_in_middle_handles_tiny_inputs() {
        assert_eq!(lost_in_middle_order(0), Vec::<usize>::new());
        assert_eq!(lost_in_middle_order(1), vec![0]);
        assert_eq!(lost_in_middle_order(2), vec![0, 1]);
    }

    // ── P1-6: one long source must not truncate the whole context ──

    #[test]
    fn a_long_source_is_truncated_not_allowed_to_end_the_context() {
        // v7.0 estimated the block size and `break`-ed on the first source
        // that did not fit, so one long source at position 3 cut the context
        // to two sources.
        let mut sources = vec![
            src("https://a.test/1", "Short A", &"alpha words. ".repeat(40)),
            src("https://b.test/2", "HUGE", &"beta ".repeat(50_000)), // ~250k chars
        ];
        sources.push(src(
            "https://c.test/3",
            "Short C",
            &"gamma words. ".repeat(40),
        ));
        sources.push(src(
            "https://d.test/4",
            "Short D",
            &"delta words. ".repeat(40),
        ));

        let ctx = build_ranked_context(&sources, 20_000);
        assert!(
            ctx.citation_map.len() >= 3,
            "oversized source truncated the context to {} sources",
            ctx.citation_map.len()
        );
        assert!(crate::util::char_len(&ctx.text) <= 20_500, "budget overrun");
    }

    #[test]
    fn context_includes_publication_dates() {
        // v7.0 told the model to "prefer the most recent sources" while
        // giving it no dates to compare.
        let mut s = src("https://a.test/1", "Dated", &"content words. ".repeat(40));
        s.published = chrono::DateTime::parse_from_rfc3339("2026-10-01T12:00:00Z")
            .ok()
            .map(|d| d.with_timezone(&chrono::Utc));
        let ctx = build_ranked_context(&[s], 50_000);
        assert!(ctx.text.contains("2026-10-01"), "date missing from context");
        let undated = src("https://b.test/2", "Undated", &"content words. ".repeat(40));
        let ctx2 = build_ranked_context(&[undated], 50_000);
        assert!(ctx2.text.contains("undated"));
    }

    // ── verify_citations now actually verifies ──

    #[test]
    fn verify_citations_flags_unsupported_claims() {
        // Sources must clear build_ranked_context's 80-char minimum — a
        // sub-80-char "source" is not evidence and is correctly skipped.
        let sources = vec![
            src(
                "https://a.test/1",
                "Cricket Schedule",
                "The tournament begins in October 2027 across nine venues in India. \
                 Matches will be played in Mumbai, Delhi, Chennai and Kolkata, with \
                 the final scheduled at Ahmedabad in November.",
            ),
            src(
                "https://b.test/2",
                "Unrelated Page",
                "This page discusses marine biology and coral reef conservation \
                 efforts across the Pacific. Researchers monitor bleaching events \
                 and document species recovery after warming periods.",
            ),
        ];
        let ctx = build_ranked_context(&sources, 50_000);
        // [1] makes a claim the first source supports; [2] does not.
        let answer = "The tournament begins in October 2027 across nine venues [1]. \
                      The tournament also features robotic umpires and orbital stadiums [2].";
        let checks = verify_citations(answer, &sources, &ctx.citation_map);
        assert_eq!(
            checks.len(),
            2,
            "both citations should be checked: {checks:?}"
        );
        let c2 = checks.iter().find(|c| c.number == 2).unwrap();
        assert!(
            !c2.supported,
            "fabricated claim should be unsupported (overlap {})",
            c2.overlap
        );
    }

    #[test]
    fn verify_citations_returns_data_not_the_input_string() {
        // v7.0's body ended with `result` — the unmodified input — so despite
        // its doc comment it only wrote log lines.
        let sources = vec![src(
            "https://a.test/1",
            "Topic",
            "alpha beta gamma delta epsilon zeta eta theta iota kappa lambda mu \
             nu xi omicron pi rho sigma tau upsilon phi chi psi omega",
        )];
        let ctx = build_ranked_context(&sources, 50_000);
        assert_eq!(
            ctx.citation_map.len(),
            1,
            "source should have been included"
        );
        let checks = verify_citations(
            "Some claim about alpha beta gamma [1].",
            &sources,
            &ctx.citation_map,
        );
        assert!(!checks.is_empty());
        assert_eq!(checks[0].url, "https://a.test/1");
    }

    #[test]
    fn verify_citations_is_utf8_safe_on_hindi_answers() {
        let sources = vec![src(
            "https://amarujala.test/1",
            "विश्व कप",
            "भारत और पाकिस्तान के बीच मुकाबला दस अक्टूबर को खेला जाएगा। टूर्नामेंट भारत में होगा।",
        )];
        let ctx = build_ranked_context(&sources, 50_000);
        let answer = "भारत और पाकिस्तान के बीच मुकाबला दस अक्टूबर को होगा [1]। यह टूर्नामेंट भारत में है [1]।";
        let checks = verify_citations(answer, &sources, &ctx.citation_map);
        assert!(!checks.is_empty(), "should find the Hindi citation");
    }

    #[test]
    fn extract_citation_contexts_ignores_non_citations() {
        let found = extract_citation_contexts("See [abc] and [12x] but [3] counts.");
        assert_eq!(found.len(), 1);
        assert_eq!(found[0].0, 3);
    }

    #[test]
    fn out_of_range_citations_are_dropped_not_panics() {
        let sources = vec![src(
            "https://a.test/1",
            "Topic",
            "some body text here for the source that is comfortably longer than \
             the eighty character minimum the context builder enforces",
        )];
        let ctx = build_ranked_context(&sources, 50_000);
        // [9] has no mapping.
        let checks = verify_citations("Claim [9].", &sources, &ctx.citation_map);
        assert!(checks.is_empty());
    }

    // ── P1-10: context budget ──

    #[test]
    fn context_budget_scales_with_the_model() {
        let gemini = ContextBudget::for_model_with_override("gemini-3-pro", None);
        let haiku = ContextBudget::for_model_with_override("claude-haiku-4-5", None);
        let unknown = ContextBudget::for_model_with_override("some-local-llama", None);
        assert!(gemini.max_context_chars() > haiku.max_context_chars());
        assert!(haiku.max_context_chars() > unknown.max_context_chars());
        // All of them must beat the v7.0 fixed 16,000.
        assert!(
            unknown.max_context_chars() > 16_000,
            "even the conservative default should beat v7.0"
        );
        assert!(gemini.max_context_chars() > 500_000);
    }

    #[test]
    fn context_budget_env_override_is_clamped() {
        assert!(
            ContextBudget::for_model_with_override("x", Some(999_999_999)).max_tokens <= 900_000
        );
        assert!(ContextBudget::for_model_with_override("x", Some(1)).max_tokens >= 2_000);
        assert_eq!(
            ContextBudget::for_model_with_override("x", Some(50_000)).max_tokens,
            50_000
        );
    }

    #[test]
    fn max_output_tokens_is_never_the_v7_truncating_4096_for_big_models() {
        let b = ContextBudget::for_model_with_override("claude-opus-5", None);
        assert!(
            b.max_output_tokens() > 4096,
            "research reports were truncated at 4096"
        );
    }

    fn cfg(provider: &str, model: &str, base: Option<&str>) -> crate::models::LlmConfig {
        crate::models::LlmConfig {
            provider: provider.into(),
            api_key: "k".into(),
            model: model.into(),
            base_url: base.map(str::to_string),
            timeout_ms: None,
            max_output_tokens: None,
            context_window: None,
        }
    }

    #[test]
    fn detects_truncated_responses_across_providers() {
        assert!(was_truncated(
            &serde_json::json!({"choices":[{"finish_reason":"length"}]})
        ));
        assert!(was_truncated(
            &serde_json::json!({"stop_reason":"max_tokens"})
        ));
        assert!(!was_truncated(
            &serde_json::json!({"choices":[{"finish_reason":"stop"}]})
        ));
        assert!(!was_truncated(
            &serde_json::json!({"stop_reason":"end_turn"})
        ));
        assert!(!was_truncated(&serde_json::json!({})));
    }

    #[test]
    fn local_servers_get_a_window_they_actually_have() {
        // Measured: Ollama truncated a 15k-token prompt to 4,096 tokens.
        let o = ContextBudget::for_config_with_override(
            &cfg(
                "ollama",
                "qwen2.5-coder:7b",
                Some("http://localhost:11434/v1"),
            ),
            None,
            None,
        );
        assert_eq!(o.max_tokens, 4_096);
        // Any loopback/private base URL is treated as a local server.
        let lm = ContextBudget::for_config_with_override(
            &cfg(
                "openai_compatible",
                "local-model",
                Some("http://127.0.0.1:1234/v1"),
            ),
            None,
            None,
        );
        assert_eq!(lm.max_tokens, 4_096);
        // OLLAMA_CONTEXT_LENGTH is honoured, and CONTEXT_MAX_TOKENS wins over all.
        let big =
            ContextBudget::for_config_with_override(&cfg("ollama", "m", None), None, Some(32_768));
        assert_eq!(big.max_tokens, 32_768);
        let forced = ContextBudget::for_config_with_override(
            &cfg("ollama", "m", None),
            Some(8_000),
            Some(32_768),
        );
        assert_eq!(forced.max_tokens, 8_000);
        // Hosted providers keep their model-based budgets.
        let hosted = ContextBudget::for_config_with_override(
            &cfg("anthropic", "claude-opus-5", None),
            None,
            None,
        );
        assert!(hosted.max_tokens >= 100_000);
    }

    #[test]
    fn prompt_and_output_fit_inside_one_window() {
        for w in [4_096usize, 8_000, 32_768, 150_000] {
            let b = ContextBudget { max_tokens: w };
            for requested in [600, 2_000, 4_000, 8_000] {
                let out = b.fit_output(requested);
                let ctx = b.context_tokens(out);
                assert!(
                    out + ctx + 900 <= w.max(out + 400 + 900),
                    "window {w}, requested {requested}: {out}+{ctx} overflows"
                );
                assert!(out <= w / 3 || out == 128);
            }
            assert!(b.max_output_tokens() <= w / 3);
        }
        // The old fixed 2,048 floor would have taken half a 4k window.
        assert!(ContextBudget { max_tokens: 4_096 }.max_output_tokens() <= 1_365);
    }

    #[test]
    fn character_budget_follows_the_script_of_the_sources() {
        let en = vec![src(
            "https://a.test/1",
            "T",
            &"The quick brown fox jumps over the lazy dog. ".repeat(40),
        )];
        let hi = vec![src(
            "https://b.test/1",
            "T",
            &"भारत और पाकिस्तान के बीच मुकाबला खेला जाएगा। ".repeat(40),
        )];
        let en_chars = chars_for_tokens(1_000, &en);
        let hi_chars = chars_for_tokens(1_000, &hi);
        assert!(en_chars >= 3_000, "English ~4 chars/token, got {en_chars}");
        assert!(
            hi_chars < en_chars / 2,
            "Devanagari must get far fewer chars per token: {hi_chars} vs {en_chars}"
        );
        // No sources: falls back to English density instead of dividing by zero.
        assert_eq!(chars_for_tokens(100, &[]), 400);
    }

    #[test]
    fn est_tokens_accounts_for_script_density() {
        // Devanagari costs more tokens per character than ASCII.
        let ascii = "a".repeat(100);
        let hindi = "क".repeat(100);
        assert!(est_tokens(&hindi) > est_tokens(&ascii));
    }

    #[test]
    fn trust_score_is_actually_wired_and_ranks_authorities_higher() {
        // Structural institutional and documentation hosts outrank random blogs,
        // while Wikipedia stays secondary (1.0) by default and boosts only on Study/HistoryStatic.
        assert!(trust_score("https://www.nih.gov/x") > trust_score("https://random-blog.test/x"));
        assert!(
            trust_score("https://docs.rust-lang.org/std/")
                > trust_score("https://random-blog.test/x")
        );
        assert_eq!(trust_score("https://en.wikipedia.org/wiki/X"), 1.0);
        let study_wiki = dynamic_authority_score(
            "https://en.wikipedia.org/wiki/X",
            Some("Wikipedia"),
            "X",
            None,
            crate::modes::Mode::Study,
            false,
        );
        assert!(study_wiki > 1.2);
    }

    #[test]
    fn http_413_payload_overflow_auto_trims_context_preserving_question_footer() {
        let err = "http_413: Request too large for model openai/gpt-oss-120b in organization org_123 on tokens per minute (TPM): Limit 8000, Requested 9655, please reduce your message size";
        assert!(is_payload_too_large(err));
        let ratio = parse_token_limit_ratio(err).expect("should parse Limit and Requested");
        assert!(ratio > 0.50 && ratio < 0.75, "unexpected ratio: {ratio}");

        let body = format!(
            "[1] Source A\n{}\n\n[2] Source B\n{}\n\n---\n\nQuestion: What is the latest release?",
            "alpha ".repeat(400),
            "beta ".repeat(400)
        );
        let msgs = vec![
            ChatMessage::system("System prompt"),
            ChatMessage::user(body.clone()),
        ];
        let trimmed = shrink_messages_for_retry(&msgs, ratio);
        assert!(trimmed[1].content.len() < body.len());
        assert!(
            trimmed[1]
                .content
                .ends_with("Question: What is the latest release?"),
            "Question footer must be preserved after auto-trim: {}",
            trimmed[1].content
        );
    }

    #[test]
    fn dynamic_rate_limit_discovery_learns_from_provider_error_and_updates_budget() {
        let test_cfg = cfg(
            "groq",
            "test-dynamic-model-120b",
            Some("https://api.groq.com/openai/v1"),
        );
        let err = "http_413: Request too large for model test-dynamic-model-120b: Limit 8000, Requested 9200";
        learn_rate_limit_from_error(&test_cfg, err);

        let learned = learned_token_limit("groq", "test-dynamic-model-120b")
            .expect("should learn token limit dynamically");
        assert_eq!(learned, (8000 * 92) / 100);

        let budget = ContextBudget::for_config_with_override(&test_cfg, None, None);
        assert_eq!(budget.max_tokens, learned);

        // Rolling-window HTTP 429 must NEVER be misclassified as payload-too-large (HTTP 413)
        let rate_limit_429 = "http_429: Rate limit reached for model openai/gpt-oss-120b on tokens per minute (TPM): Limit 8000, Used 6773, Requested 4161. Please try again in 3.57s.";
        assert!(
            !is_payload_too_large(rate_limit_429),
            "HTTP 429 rolling rate limit must never trigger context shrinking"
        );
        let delay =
            parse_retry_after_delay(rate_limit_429).expect("should parse 3.57s retry delay");
        assert!(delay >= Duration::from_millis(3570) && delay <= Duration::from_millis(4500));

        // Full-width citations 【5】 normalize cleanly to [5]
        let norm = post_process_answer("Latest video released on Nov 20, 2026 【5】.", "q");
        assert_eq!(norm, "Latest video released on Nov 20, 2026 [5].");
    }

    #[test]
    fn sentence_pruning_deduplicates_repeated_sentences_across_chunks() {
        let mut seen = HashSet::new();
        let entities = vec!["clickup".to_string()];
        let repeated =
            "ClickUp 4.0 introduces autonomous AI agents for workspace automation in 2026.";
        let unique_a = "The release also adds sub-second search indexing across docs and tasks.";
        let unique_b = "Enterprise workspaces can configure custom webhook rate limits per token.";

        let chunk1 = format!(
            "{repeated} {unique_a} {}",
            "Extra context words here for length. ".repeat(10)
        );
        let chunk2 = format!(
            "{repeated} {unique_b} {}",
            "Different supporting details for chunk two. ".repeat(10)
        );

        let out1 = prune_and_dedup_chunk_sentences(&chunk1, &entities, &mut seen, 2000);
        let out2 = prune_and_dedup_chunk_sentences(&chunk2, &entities, &mut seen, 2000);

        assert!(out1.contains("autonomous AI agents"));
        assert!(
            !out2.contains("autonomous AI agents"),
            "duplicate sentence from chunk1 should be deduplicated in chunk2: {out2}"
        );
        assert!(out2.contains("custom webhook rate limits"));
    }

    // ── v8.1: API key / config handling ──

    fn user_cfg(
        provider: &str,
        key: &str,
        model: &str,
        base: Option<&str>,
    ) -> crate::models::LlmConfig {
        crate::models::LlmConfig {
            provider: provider.into(),
            api_key: key.into(),
            model: model.into(),
            base_url: base.map(str::to_string),
            timeout_ms: None,
            max_output_tokens: None,
            context_window: None,
        }
    }

    #[test]
    fn pasted_keys_are_cleaned_not_silently_dropped() {
        let c = prepare_config(user_cfg(
            " Groq ",
            "  gsk_abc123XYZ\n",
            " llama-3.3-70b ",
            Some(" https://api.groq.com/openai/v1 "),
        ))
        .unwrap();
        assert_eq!(c.api_key, "gsk_abc123XYZ");
        assert_eq!(c.model, "llama-3.3-70b");
        assert_eq!(c.provider, "groq");
        assert_eq!(
            c.base_url.as_deref(),
            Some("https://api.groq.com/openai/v1")
        );
        let headers = build_auth_headers(&c, &resolve_provider(&c).unwrap());
        assert_eq!(headers.get(AUTHORIZATION).unwrap(), "Bearer gsk_abc123XYZ");

        let b = prepare_config(user_cfg(
            "openai",
            "Bearer sk-live-123",
            "gpt-4o-mini",
            None,
        ))
        .unwrap();
        assert_eq!(
            b.api_key, "sk-live-123",
            "a pasted 'Bearer ' prefix must not be doubled"
        );
    }

    #[test]
    fn bad_configs_are_reported_with_a_reason() {
        let e = prepare_config(user_cfg("openai", "sk-abc def", "gpt-4o-mini", None)).unwrap_err();
        assert!(e.contains("API key"), "{e}");
        let e =
            prepare_config(user_cfg("openai", "sk-ab\u{200b}c", "gpt-4o-mini", None)).unwrap_err();
        assert!(e.contains("non-ASCII"), "zero-width space: {e}");
        let e = prepare_config(user_cfg("openai", "", "gpt-4o-mini", None)).unwrap_err();
        assert!(e.contains("empty"), "{e}");
        let e = prepare_config(user_cfg("openai", "sk-1", "  ", None)).unwrap_err();
        assert!(e.contains("model"), "{e}");
        let c = prepare_config(user_cfg("openai", "sk-1", "m", None))
            .map(|mut c| {
                c.timeout_ms = Some(u64::MAX);
                c
            })
            .unwrap();
        assert_eq!(prepare_config(c).unwrap().timeout_ms, Some(600_000));
    }

    #[test]
    fn local_models_need_no_key_and_send_no_auth_header() {
        let c = prepare_config(user_cfg("ollama", "", "qwen2.5", None)).unwrap();
        let headers = build_auth_headers(&c, &resolve_provider(&c).unwrap());
        assert!(headers.get(AUTHORIZATION).is_none());
    }

    #[test]
    fn a_rejected_base_url_is_an_error_not_a_fallback() {
        // Previously: fell back to api.openai.com and sent the custom key there.
        let c = user_cfg(
            "openai_compatible",
            "sk-custom",
            "m",
            Some("http://169.254.169.254/v1"),
        );
        assert!(resolve_provider(&c).is_err());
        assert!(prepare_config(c).unwrap_err().contains("base_url rejected"));
    }

    #[test]
    fn pasted_endpoint_paths_are_trimmed_from_base_urls() {
        assert_eq!(
            normalize_base_url("https://api.x.ai/v1/chat/completions", "xai"),
            "https://api.x.ai/v1"
        );
        assert_eq!(
            normalize_base_url("https://api.anthropic.com/v1/", "anthropic"),
            "https://api.anthropic.com"
        );
        assert_eq!(
            normalize_base_url("https://api.anthropic.com/v1/messages", "anthropic"),
            "https://api.anthropic.com"
        );
        assert_eq!(
            normalize_base_url("https://openrouter.ai/api/v1/", "openrouter"),
            "https://openrouter.ai/api/v1"
        );
        let c = user_cfg(
            "anthropic",
            "k",
            "claude",
            Some("https://api.anthropic.com/v1"),
        );
        assert_eq!(
            build_chat_url(&resolve_provider(&c).unwrap(), "claude", false),
            "https://api.anthropic.com/v1/messages"
        );
        let o = user_cfg(
            "openai_compatible",
            "k",
            "m",
            Some("https://llm.example.com/v1/chat/completions"),
        );
        assert_eq!(
            build_chat_url(&resolve_provider(&o).unwrap(), "m", false),
            "https://llm.example.com/v1/chat/completions"
        );
    }

    #[test]
    fn unknown_provider_without_base_url_is_refused() {
        let e = prepare_config(user_cfg("openai_compatible", "sk-x", "m", None)).unwrap_err();
        assert!(e.contains("needs a base_url"), "{e}");
        assert!(prepare_config(user_cfg(
            "openai_compatible",
            "sk-x",
            "m",
            Some("https://llm.example.com/v1")
        ))
        .is_ok());
    }

    #[test]
    fn anthropic_joins_multiple_system_messages() {
        let msgs = vec![
            ChatMessage {
                role: "system".into(),
                content: "A".into(),
            },
            ChatMessage {
                role: "system".into(),
                content: "B".into(),
            },
            ChatMessage {
                role: "user".into(),
                content: "q".into(),
            },
        ];
        let info = ProviderInfo {
            format: ApiFormat::Anthropic,
            base_url: "https://api.anthropic.com/".into(),
        };
        let body = build_request_body(&msgs, "claude", &info, false, 512);
        assert_eq!(body["system"], "A\n\nB");
        assert_eq!(body["messages"].as_array().unwrap().len(), 1);
    }

    #[test]
    fn default_anthropic_endpoint_is_well_formed() {
        let c = user_cfg("anthropic", "k", "claude-haiku", None);
        assert_eq!(
            build_chat_url(&resolve_provider(&c).unwrap(), "claude-haiku", false),
            "https://api.anthropic.com/v1/messages"
        );
    }

    #[test]
    fn keys_are_redacted_from_provider_errors() {
        let msg = "Incorrect API key provided: sk-proj-abcdef123456. Check your key.";
        assert_eq!(
            redact_key(msg, "sk-proj-abcdef123456"),
            "Incorrect API key provided: <redacted>. Check your key."
        );
        assert_eq!(redact_key("x", ""), "x");
    }

    #[test]
    fn intent_prompts_enforce_opening_bullets_and_natural_wrapup() {
        let intents = [
            QueryIntent::Factual,
            QueryIntent::Temporal,
            QueryIntent::Person,
            QueryIntent::Comparison,
            QueryIntent::HowTo,
            QueryIntent::Research,
            QueryIntent::Navigational,
        ];
        for intent in intents {
            let mut intel = crate::query_intel::analyze_query("test topic");
            intel.intent = intent.clone();
            let (sys, user) = build_intent_prompts("test topic", "[1] Source text", &intel);
            assert!(
                sys.contains("SYNTHESIZE WITH CLEAR FLOW"),
                "missing flow rule for {intent:?}"
            );
            assert!(
                user.contains("heading") && user.contains("bullet points"),
                "missing opening/bullet structure for {intent:?}: {user}"
            );
            assert!(
                user.contains("concluding paragraph")
                    && user.contains("'Summary:'")
                    && user.contains("'Conclusion:'"),
                "missing natural wrap-up rule for {intent:?}: {user}"
            );
            assert!(
                !user.contains("End with: **Sources Used:**"),
                "must not append a trailing Sources Used URL list for {intent:?}"
            );
        }
    }
}