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
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
use chrono::Datelike;
// ============================================================================
// SearchWala v7.0.0 - Search Orchestrator (Intelligence-Driven)
// Features:
//   - QueryIntelligence: intent detection, temporal classification, entity extraction
//   - RRF k=60 (Cormack 2009): cross-engine consensus ranking
//   - BM25+ (Lv & Zhai 2011) + MMR (Carbonell & Goldstein 1998) chunk ranking
//   - Lost-in-Middle context ordering (Liu et al. 2024)
//   - JoinSet engine dispatch with deadline-based early cancellation
//   - Per-stage timing instrumentation (engine, scrape, rank, llm)
//   - Citation faithfulness verification + quality-gated LLM retry
//   - Intent-aware LLM prompts and source count optimization
//   - Iterative deep research: multi-batch LLM synthesis
//   - TempDb session tracking; conversations are saved by the caller (chats.rs)
// ============================================================================

use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;

use scraper::Html;
use tokio::sync::Semaphore;
use tokio::task::JoinSet;

use crate::config;
use crate::engines;
use crate::extractor;
use crate::fetcher::{self, Fetcher};
use crate::llm;
use crate::models::*;
use crate::modes;
use crate::proxy_pool::ProxyPoolManager;
use crate::query_intel;
use crate::ranking;
use crate::synthesis;
use crate::url_utils;

/// Optional domain-level inclusion/exclusion rules for surgical source control.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct DomainFilter {
    /// Normalized lowercase domains to query and prioritize (or restrict to when `strict == true`).
    pub include: Vec<String>,
    /// Normalized lowercase domains to drop before triage, summarization, and scraping.
    pub exclude: Vec<String>,
    /// When true and `include` is non-empty, strictly restrict results and scraping to `include`.
    /// When false (default), SearchWala keeps its own organic consensus answers intact while
    /// prioritizing `include` at the top.
    pub strict: bool,
}

impl DomainFilter {
    pub fn is_excluded(&self, url: &str) -> bool {
        self.exclude
            .iter()
            .any(|d| url_utils::host_matches_domain(url, d))
    }

    pub fn is_included(&self, url: &str) -> bool {
        self.include
            .iter()
            .any(|d| url_utils::host_matches_domain(url, d))
    }

    pub fn is_strict_active(&self) -> bool {
        self.strict && !self.include.is_empty()
    }

    pub fn allows_url(&self, url: &str) -> bool {
        if self.is_excluded(url) {
            return false;
        }
        if self.is_strict_active() {
            return self.is_included(url);
        }
        true
    }
}

/// Parameters for one search request.
#[derive(Debug, Clone, Default)]
pub struct SearchParams {
    pub query: String,
    /// Explicit URL budget. Clamped into the mode's bounds; `None` means the
    /// mode decides.
    pub max_results: Option<usize>,
    pub focus_mode: Option<String>,
    /// Subject-area focus, composable with any mode.
    pub domain: Option<String>,
    pub llm: Option<LlmConfig>,
    pub enable_copilot: bool,
    /// Earlier turns of the conversation for the LLM, oldest first. Empty
    /// when memory is off.
    pub memory: Vec<crate::chats::PriorTurn>,
    /// Optional domain include/exclude rules and strict-mode toggle.
    pub domain_filter: DomainFilter,
}

/// Pipeline progress, emitted to streaming clients.
#[derive(Debug, Clone, serde::Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Progress {
    Plan {
        mode: String,
        tier: String,
        domain: String,
        engines: usize,
        sub_queries: Vec<String>,
        max_urls: usize,
    },
    Engines {
        raw_results: usize,
        families: usize,
        ms: u128,
    },
    Sources {
        results: Vec<SearchHit>,
    },
    Extracted {
        sources: usize,
        chars: usize,
        ms: u128,
    },
    Synthesizing {
        synthesis: String,
    },
    /// Best verbatim answer sentence(s), before synthesis starts.
    Highlight {
        highlight: crate::highlight::Highlight,
    },
}

async fn emit(tx: &Option<tokio::sync::mpsc::Sender<Progress>>, p: Progress) {
    if let Some(t) = tx {
        let _ = t.send(p).await;
    }
}

/// One consensus-ranked search hit returned by [`search_serp_only`].
#[derive(Debug, Clone, serde::Serialize)]
pub struct SerpHit {
    pub rank: usize,
    pub url: String,
    pub title: String,
    pub snippet: String,
    pub engines: Vec<String>,
    pub found_by_families: usize,
    pub rrf_score: f64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub published: Option<chrono::DateTime<chrono::Utc>>,
    /// Engine-supplied abstract/text when available without scraping (e.g. OpenAlex, arXiv).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub abstract_text: Option<String>,
}

/// Output of [`search_serp_only`] (Stages 1–4: engine dispatch, RRF consensus,
/// and pre-scrape triage — without page scraping or LLM synthesis).
#[derive(Debug, serde::Serialize)]
pub struct SerpSearchResponse {
    pub query: String,
    pub mode: String,
    pub tier: String,
    pub domain: String,
    pub intent: String,
    pub is_time_sensitive: bool,
    pub sub_queries: Vec<String>,
    pub elapsed_seconds: f64,
    pub engine_stats: EngineStats,
    pub results: Vec<SerpHit>,
    pub discovered_domains: Vec<DiscoveredDomain>,
    pub total_discovered_domains: usize,
}

/// Options for [`fetch_and_extract_url`].
#[derive(Debug, Clone)]
pub struct FetchExtractOptions {
    pub timeout: Duration,
    pub max_bytes: usize,
    pub allow_amp_fallback: bool,
    pub use_cache: bool,
    /// Accept `text/plain` and `text/markdown` responses directly (useful for
    /// docs, RFCs, and `llms.txt` when called from `extract_url`).
    pub allow_plain_text: bool,
}

impl Default for FetchExtractOptions {
    fn default() -> Self {
        Self {
            timeout: Duration::from_secs(12),
            max_bytes: config::max_html_bytes(),
            allow_amp_fallback: true,
            use_cache: true,
            allow_plain_text: true,
        }
    }
}

/// Result of [`fetch_and_extract_url`].
#[derive(Debug, Clone)]
pub struct ExtractedPage {
    pub url: String,
    pub final_url: String,
    pub extracted: crate::extractor::Extracted,
    pub char_count: usize,
    pub cached: bool,
    pub elapsed_ms: u64,
}

/// Typed failure modes for [`fetch_and_extract_url`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ExtractError {
    InvalidUrl(String),
    BlockedBySsrf(String),
    UnsupportedContentType(String),
    AntiBotBlocked { status: u16 },
    HttpStatus { status: u16 },
    Timeout,
    NetworkError(String),
    UnusableContent { chars: usize },
}

impl std::fmt::Display for ExtractError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ExtractError::InvalidUrl(msg) => write!(f, "invalid URL: {msg}"),
            ExtractError::BlockedBySsrf(msg) => write!(f, "URL blocked by SSRF guard: {msg}"),
            ExtractError::UnsupportedContentType(ct) => {
                write!(f, "unsupported content type: {ct}")
            }
            ExtractError::AntiBotBlocked { status } => {
                write!(f, "blocked by anti-bot protection (HTTP {status})")
            }
            ExtractError::HttpStatus { status } => {
                write!(f, "upstream returned HTTP {status}")
            }
            ExtractError::Timeout => write!(f, "request timed out"),
            ExtractError::NetworkError(msg) => write!(f, "network error: {msg}"),
            ExtractError::UnusableContent { chars } => {
                write!(
                    f,
                    "extracted content too short or unreadable ({chars} chars)"
                )
            }
        }
    }
}

impl std::error::Error for ExtractError {}

/// Back-compatible entry point (no domain, no progress).
pub async fn execute_search(
    query: &str,
    max_results: Option<usize>,
    focus_mode: Option<String>,
    llm_config: Option<LlmConfig>,
    enable_copilot: Option<bool>,
    temp_db: Option<&crate::cache::TempDb>,
) -> SearchResponse {
    execute_search_full(
        SearchParams {
            query: query.to_string(),
            max_results,
            focus_mode,
            domain: None,
            llm: llm_config,
            enable_copilot: enable_copilot.unwrap_or(false),
            memory: Vec::new(),
            domain_filter: DomainFilter::default(),
        },
        temp_db,
        None,
    )
    .await
}

/// Fast SERP-only consensus search (Stages 1–4): runs query intelligence,
/// mode-appropriate parallel engine dispatch, cross-family RRF consensus, and
/// pre-scrape triage without fetching target pages.
pub async fn search_serp_only(
    query: &str,
    focus_mode: Option<&str>,
    domain: Option<&str>,
    max_results: Option<usize>,
    domain_filter: &DomainFilter,
    progress: Option<tokio::sync::mpsc::Sender<Progress>>,
) -> SerpSearchResponse {
    let start = std::time::Instant::now();
    let trimmed = query.trim().to_string();
    let (mode, domain_val) = modes::parse(focus_mode, domain);
    let intel = query_intel::analyze_query(&trimmed);
    let profile = modes::resolve(mode, domain_val, &intel, None);
    let result_limit = max_results.unwrap_or(15).clamp(1, 50);

    if trimmed.is_empty() {
        return SerpSearchResponse {
            query: trimmed,
            mode: mode.as_str().to_string(),
            tier: profile.tier.to_string(),
            domain: domain_val.as_str().to_string(),
            intent: format!("{:?}", intel.intent),
            is_time_sensitive: intel.is_time_sensitive,
            sub_queries: Vec::new(),
            elapsed_seconds: start.elapsed().as_secs_f64(),
            engine_stats: EngineStats::default(),
            results: Vec::new(),
            discovered_domains: Vec::new(),
            total_discovered_domains: 0,
        };
    }

    let effective_query = apply_recency_hint(&trimmed, &intel, mode);
    let mut query_variations = if profile.max_sub_queries > 1 && query_intel::multi_query_enabled()
    {
        modes::facet_queries(
            &effective_query,
            &intel,
            profile.facets,
            profile.max_sub_queries,
        )
    } else {
        vec![effective_query.clone()]
    };
    if query_variations.is_empty() {
        query_variations.push(effective_query.clone());
    }
    if profile.site_filter.is_some() {
        query_variations = query_variations
            .iter()
            .map(|q| modes::apply_site_filter(q, profile.site_filter))
            .collect();
    }

    let engine_list = engines::filter_healthy(profile.engines.clone());
    let engine_instances = engines::get_engines(&engine_list);

    emit(
        &progress,
        Progress::Plan {
            mode: mode.as_str().into(),
            tier: profile.tier.into(),
            domain: domain_val.as_str().into(),
            engines: engine_instances.len(),
            sub_queries: query_variations.clone(),
            max_urls: result_limit,
        },
    )
    .await;

    let (plain_fetcher, stealth_fetcher) = fetcher::build_pair(None);
    let stealth_engine_set: std::collections::HashSet<String> =
        config::stealth_engines().into_iter().collect();
    let stealth_mode = config::stealth_mode();
    let proxy_pool = ProxyPoolManager::from_env();
    let engine_concurrency = config::engine_concurrency().clamp(1, 40);
    let engine_semaphore = Arc::new(Semaphore::new(engine_concurrency));

    let stage_start = std::time::Instant::now();
    let (tagged, all_queries) = dispatch_with_domain_targets(
        &engine_instances,
        &query_variations,
        &effective_query,
        domain_filter,
        profile.site_filter.is_some(),
        &plain_fetcher,
        &stealth_fetcher,
        &stealth_engine_set,
        &stealth_mode,
        &proxy_pool,
        &engine_semaphore,
        config::jitter_min_ms(),
        config::jitter_max_ms(),
        engine_concurrency,
        profile.engine_deadline,
        profile.page_budget,
        profile.engine_early_exit,
    )
    .await;

    let fused_scores = compute_fused_scores(&tagged, &all_queries);
    let mut all_results: Vec<RawSearchResult> = tagged.into_iter().flat_map(|(_, v)| v).collect();

    if profile.site_filter.is_some() {
        all_results.retain(|r| modes::matches_site_filter(&r.url, profile.site_filter));
    }

    let engine_dispatch_ms = stage_start.elapsed().as_millis();
    let families: std::collections::HashSet<&str> = all_results
        .iter()
        .map(|r| engines::engine_family(&r.engine))
        .collect();
    emit(
        &progress,
        Progress::Engines {
            raw_results: all_results.len(),
            families: families.len(),
            ms: engine_dispatch_ms,
        },
    )
    .await;

    let total_raw = all_results.len();
    let sel = fuse_and_select_candidates(
        &all_results,
        fused_scores,
        &trimmed,
        &effective_query,
        &intel,
        &profile,
        result_limit,
        domain_filter,
    );
    let deduped_count = sel.deduplicated_urls;
    let hits = candidates_to_serp_hits(&sel.candidates, result_limit);
    let discovered_domains = sel.discovered_domains;
    let total_discovered_domains = sel.total_discovered_domains;

    let progress_hits: Vec<SearchHit> = hits
        .iter()
        .map(|h| SearchHit {
            url: h.url.clone(),
            title: h.title.clone(),
            snippet: h.snippet.clone(),
            engine: h.engines.join(","),
            rrf_score: h.rrf_score,
            found_by_families: h.found_by_families,
        })
        .collect();
    emit(
        &progress,
        Progress::Sources {
            results: progress_hits,
        },
    )
    .await;

    SerpSearchResponse {
        query: trimmed,
        mode: mode.as_str().to_string(),
        tier: profile.tier.to_string(),
        domain: domain_val.as_str().to_string(),
        intent: format!("{:?}", intel.intent),
        is_time_sensitive: intel.is_time_sensitive,
        sub_queries: query_variations,
        elapsed_seconds: start.elapsed().as_secs_f64(),
        engine_stats: EngineStats {
            engines_queried: engine_list,
            total_raw_results: total_raw,
            deduplicated_urls: deduped_count,
        },
        results: hits,
        discovered_domains,
        total_discovered_domains,
    }
}

/// Everything known about one candidate URL before it is scraped.
struct Candidate {
    url: String,
    title: String,
    snippet: String,
    engines: Vec<String>,
    families: Vec<&'static str>,
    rrf: f64,
    published: Option<chrono::DateTime<chrono::Utc>>,
    /// Text supplied by the engine itself (paper abstracts).
    full_text: Option<String>,
}

/// Append the dynamic current year for time-sensitive non-evergreen searches
/// when the query does not already name a year.
fn apply_recency_hint(
    effective_query: &str,
    intel: &query_intel::QueryIntelligence,
    mode: modes::Mode,
) -> String {
    if intel.is_time_sensitive && !matches!(mode, modes::Mode::Study | modes::Mode::Academic) {
        let q_lower = effective_query.to_lowercase();
        let current_year = chrono::Utc::now().year();
        let has_year =
            ((current_year - 15)..=(current_year + 10)).any(|y| q_lower.contains(&y.to_string()));
        if !has_year {
            return format!("{} {}", effective_query, current_year);
        }
    }
    effective_query.to_string()
}

/// Compute weighted cross-query fusion scores when multiple sub-queries ran.
fn compute_fused_scores(
    tagged: &[(usize, Vec<RawSearchResult>)],
    query_variations: &[String],
) -> Option<HashMap<String, f64>> {
    if query_variations.len() <= 1 {
        return None;
    }
    // Regroup per sub-query and fuse with the ORIGINAL weighted 2x. The
    // original is `query_variations[0]` — it already carries any site:
    // filter, so comparing against the bare query would never match and
    // the 2x weight would silently never apply.
    let mut grouped: Vec<Vec<RawSearchResult>> = vec![Vec::new(); query_variations.len()];
    for (idx, batch) in tagged {
        if let Some(slot) = grouped.get_mut(*idx) {
            slot.extend(batch.iter().cloned());
        }
    }
    let per_query: Vec<(String, Vec<RawSearchResult>)> =
        query_variations.iter().cloned().zip(grouped).collect();
    Some(ranking::cross_query_scores(
        &per_query,
        &query_variations[0],
    ))
}

/// Maximum targeted `site:` queries emitted for `include_domains`.
const MAX_TARGETED_DOMAIN_QUERIES: usize = 5;
/// Maximum distinct domains packaged in `discovered_domains`.
const MAX_DISCOVERED_DOMAINS: usize = 15;

/// Build clean `site:{domain}` queries for `include_domains` (only dispatched to
/// site-capable engines so JSON API engines never search literal `site:` strings).
fn build_targeted_domain_queries(
    effective_query: &str,
    domain_filter: &DomainFilter,
    has_mode_site_filter: bool,
) -> Vec<String> {
    if has_mode_site_filter || domain_filter.include.is_empty() {
        return Vec::new();
    }
    let q = effective_query.trim();
    if q.is_empty() || q.to_lowercase().contains("site:") {
        return Vec::new();
    }
    domain_filter
        .include
        .iter()
        .take(MAX_TARGETED_DOMAIN_QUERIES)
        .map(|d| format!("{q} site:{d}"))
        .collect()
}

/// Dispatch the primary mode roster concurrently with targeted `site:` queries
/// for `include_domains` (on site-capable engines only).
#[allow(clippy::too_many_arguments)]
async fn dispatch_with_domain_targets(
    engine_instances: &[std::sync::Arc<dyn engines::SearchEngine>],
    query_variations: &[String],
    effective_query: &str,
    domain_filter: &DomainFilter,
    has_mode_site_filter: bool,
    plain_fetcher: &Fetcher,
    stealth_fetcher: &Fetcher,
    stealth_engine_set: &std::collections::HashSet<String>,
    stealth_mode: &str,
    proxy_pool: &ProxyPoolManager,
    engine_semaphore: &Arc<Semaphore>,
    jitter_min: u64,
    jitter_max: u64,
    engine_concurrency: usize,
    deadline: Duration,
    page_budget: usize,
    early_exit: Option<(usize, usize)>,
) -> (Vec<(usize, Vec<RawSearchResult>)>, Vec<String>) {
    let targeted_queries =
        build_targeted_domain_queries(effective_query, domain_filter, has_mode_site_filter);

    if targeted_queries.is_empty() {
        let tagged = dispatch_engines(
            engine_instances,
            query_variations,
            plain_fetcher,
            stealth_fetcher,
            stealth_engine_set,
            stealth_mode,
            proxy_pool,
            engine_semaphore,
            jitter_min,
            jitter_max,
            engine_concurrency,
            deadline,
            page_budget,
            early_exit,
        )
        .await;
        return (tagged, query_variations.to_vec());
    }

    // Build site-capable engine instances for Track B (ensuring targeted `site:`
    // queries work even in study/academic modes whose rosters are API-heavy).
    // If primary site-capable engines have been quarantined mid-session (e.g.
    // Bing soft-block + DDG 202), automatically top up from fallback site-capable
    // engines so strict/targeted searches stay resilient.
    let mut site_engine_names = engines::filter_healthy(modes::site_capable_engines());
    if site_engine_names.len() < 3 {
        for backup in engines::filter_healthy(modes::site_capable_fallback_engines()) {
            if !site_engine_names.contains(&backup) {
                site_engine_names.push(backup);
            }
        }
    }
    let site_engine_instances = engines::get_engines(&site_engine_names);

    // When `strict_domains` is ON, skip throwaway unscoped web queries so we do
    // not double requests to rate-limited engines (Brave/DDG/Bing) for hits we
    // would immediately discard. Keep Wikipedia/arXiv API engines on the bare
    // query only if those domains are explicitly included.
    if domain_filter.is_strict_active() {
        let mut specialized_names: Vec<String> = Vec::new();
        if domain_filter
            .include
            .iter()
            .any(|d| d == "wikipedia.org" || d.ends_with(".wikipedia.org"))
        {
            specialized_names.push("wikipedia".to_string());
        }
        if domain_filter
            .include
            .iter()
            .any(|d| d == "arxiv.org" || d.ends_with(".arxiv.org"))
        {
            specialized_names.push("arxiv".to_string());
        }

        let strict_pages = page_budget.clamp(1, 2);
        let (mut tagged, mut all_queries) = if specialized_names.is_empty() {
            let targeted_tagged = dispatch_engines(
                &site_engine_instances,
                &targeted_queries,
                plain_fetcher,
                stealth_fetcher,
                stealth_engine_set,
                stealth_mode,
                proxy_pool,
                engine_semaphore,
                jitter_min,
                jitter_max,
                engine_concurrency,
                deadline,
                strict_pages,
                None,
            )
            .await;
            (targeted_tagged, targeted_queries)
        } else {
            let spec_instances = engines::get_engines(&engines::filter_healthy(specialized_names));
            let bare_queries = vec![effective_query.to_string()];
            let (mut targeted_tagged, spec_tagged) = tokio::join!(
                dispatch_engines(
                    &site_engine_instances,
                    &targeted_queries,
                    plain_fetcher,
                    stealth_fetcher,
                    stealth_engine_set,
                    stealth_mode,
                    proxy_pool,
                    engine_semaphore,
                    jitter_min,
                    jitter_max,
                    engine_concurrency,
                    deadline,
                    strict_pages,
                    None,
                ),
                dispatch_engines(
                    &spec_instances,
                    &bare_queries,
                    plain_fetcher,
                    stealth_fetcher,
                    stealth_engine_set,
                    stealth_mode,
                    proxy_pool,
                    engine_semaphore,
                    jitter_min,
                    jitter_max,
                    engine_concurrency,
                    deadline,
                    1,
                    None,
                )
            );
            let offset = targeted_queries.len();
            for (idx, batch) in spec_tagged {
                targeted_tagged.push((offset + idx, batch));
            }
            let mut combined = targeted_queries;
            combined.extend(bare_queries);
            (targeted_tagged, combined)
        };

        // Targeted `site:` batches must only retain results that actually match
        // `include_domains` (dropping any engine soft-block spam that ignored `site:`).
        for (_, batch) in &mut tagged {
            batch.retain(|r| {
                !domain_filter.is_excluded(&r.url) && domain_filter.is_included(&r.url)
            });
        }

        // Resilience fallback: if all targeted `site:` queries produced zero
        // URLs matching `include_domains` (e.g., transient Brave HTTP 429 burst
        // while Bing/DDG are quarantined), retry once with plain-text domain
        // queries (`"{query} {domain}"`) across the mode's healthy engines
        // (including API engines like Marginalia). Strict filtering still
        // guarantees zero domain leakage.
        let has_included_hit = tagged.iter().any(|(_, batch)| !batch.is_empty());
        if !has_included_hit && !engine_instances.is_empty() {
            let fallback_queries: Vec<String> = domain_filter
                .include
                .iter()
                .take(3)
                .map(|d| format!("{} {d}", effective_query.trim()))
                .collect();
            if !fallback_queries.is_empty() {
                let fallback_tagged = dispatch_engines(
                    engine_instances,
                    &fallback_queries,
                    plain_fetcher,
                    stealth_fetcher,
                    stealth_engine_set,
                    stealth_mode,
                    proxy_pool,
                    engine_semaphore,
                    jitter_min,
                    jitter_max,
                    engine_concurrency,
                    deadline.min(Duration::from_millis(2_500)),
                    1,
                    None,
                )
                .await;
                let offset = all_queries.len();
                for (idx, mut batch) in fallback_tagged {
                    batch.retain(|r| {
                        !domain_filter.is_excluded(&r.url) && domain_filter.is_included(&r.url)
                    });
                    if !batch.is_empty() {
                        tagged.push((offset + idx, batch));
                    }
                }
                all_queries.extend(fallback_queries);
            }
        }

        return (tagged, all_queries);
    }

    let (mut base_tagged, targeted_tagged) = tokio::join!(
        dispatch_engines(
            engine_instances,
            query_variations,
            plain_fetcher,
            stealth_fetcher,
            stealth_engine_set,
            stealth_mode,
            proxy_pool,
            engine_semaphore,
            jitter_min,
            jitter_max,
            engine_concurrency,
            deadline,
            page_budget,
            // Do not early-exit generic dispatch while targeted domain tasks run.
            None,
        ),
        dispatch_engines(
            &site_engine_instances,
            &targeted_queries,
            plain_fetcher,
            stealth_fetcher,
            stealth_engine_set,
            stealth_mode,
            proxy_pool,
            engine_semaphore,
            jitter_min,
            jitter_max,
            engine_concurrency,
            deadline,
            1,
            None,
        )
    );

    let offset = query_variations.len();
    for (idx, mut batch) in targeted_tagged {
        // Track B ran explicit `site:` queries; drop any engine soft-block junk
        // that does not actually belong to `include_domains`.
        batch.retain(|r| !domain_filter.is_excluded(&r.url) && domain_filter.is_included(&r.url));
        if !batch.is_empty() {
            base_tagged.push((offset + idx, batch));
        }
    }

    let mut all_queries = query_variations.to_vec();
    all_queries.extend(targeted_queries);
    (base_tagged, all_queries)
}

/// Summarize distinct domains discovered across the full post-exclusion sorted
/// candidate pool (before `max_urls` / `max_results` truncation).
fn summarize_discovered_domains(
    sorted_candidates: &[Candidate],
    max_domains: usize,
) -> (Vec<DiscoveredDomain>, usize) {
    let mut index_by_domain: HashMap<String, usize> = HashMap::new();
    let mut domains: Vec<DiscoveredDomain> = Vec::new();

    for c in sorted_candidates {
        let dom = url_utils::display_domain(&c.url);
        if dom.is_empty() {
            continue;
        }
        if let Some(&pos) = index_by_domain.get(&dom) {
            let entry = &mut domains[pos];
            entry.result_count += 1;
            if c.families.len() > entry.max_families {
                entry.max_families = c.families.len();
            }
            if c.rrf > entry.top_rrf_score {
                entry.top_rrf_score = c.rrf;
            }
        } else {
            index_by_domain.insert(dom.clone(), domains.len());
            domains.push(DiscoveredDomain {
                domain: dom,
                result_count: 1,
                max_families: c.families.len(),
                top_rrf_score: c.rrf,
                top_url: c.url.clone(),
                top_title: crate::util::truncate_chars(c.title.trim(), 120).to_string(),
            });
        }
    }

    let total = domains.len();
    domains.truncate(max_domains);
    (domains, total)
}

/// Output of [`fuse_and_select_candidates`], preserving true pre-truncation
/// deduplicated URL and discovered-domain counts alongside the truncated pool.
struct CandidateSelection {
    rrf_scores: HashMap<String, f64>,
    candidates: Vec<Candidate>,
    deduplicated_urls: usize,
    discovered_domains: Vec<DiscoveredDomain>,
    total_discovered_domains: usize,
}

/// Collapse raw engine results by canonical URL, apply domain exclusion/inclusion
/// rules, compute cross-family RRF consensus and pre-scrape triage, build the
/// compact `discovered_domains` rollup, and truncate to `max_urls`.
#[allow(clippy::too_many_arguments)]
fn fuse_and_select_candidates(
    all_results: &[RawSearchResult],
    fused_scores: Option<HashMap<String, f64>>,
    raw_query: &str,
    effective_query: &str,
    intel: &query_intel::QueryIntelligence,
    profile: &modes::ModeProfile,
    max_urls: usize,
    domain_filter: &DomainFilter,
) -> CandidateSelection {
    let rrf_scores = fused_scores.unwrap_or_else(|| ranking::compute_rrf_scores(all_results));

    let mut by_url: HashMap<String, Candidate> = HashMap::new();
    for item in all_results {
        if domain_filter.is_excluded(&item.url) {
            continue;
        }
        let key = url_utils::canonical_key(&item.url);
        let c = by_url.entry(key).or_insert_with(|| Candidate {
            url: item.url.clone(),
            title: item.title.clone(),
            snippet: item.snippet.clone(),
            engines: Vec::new(),
            families: Vec::new(),
            rrf: 0.0,
            published: item.published,
            full_text: None,
        });
        if c.snippet.len() < item.snippet.len() {
            c.snippet = item.snippet.clone();
        }
        if c.title.is_empty() {
            c.title = item.title.clone();
        }
        if c.published.is_none() {
            c.published = item.published;
        }
        if c.full_text.is_none() {
            c.full_text = item.full_text.clone();
        }
        if !c.engines.contains(&item.engine) {
            c.engines.push(item.engine.clone());
        }
        let fam = engines::engine_family(&item.engine);
        if !c.families.contains(&fam) {
            c.families.push(fam);
        }
    }

    let site_mode = profile.site_filter.is_some();
    let mut ranked: Vec<Candidate> = by_url
        .into_iter()
        .filter_map(|(key, mut c)| {
            // Site-filtered modes and explicitly included domains bypass the
            // general skip list.
            let bypass_skip = site_mode || domain_filter.is_included(&c.url);
            if !bypass_skip && url_utils::should_skip(&c.url) {
                return None;
            }
            c.rrf = rrf_scores.get(&key).copied().unwrap_or(0.0);
            Some(c)
        })
        .collect();

    // Selection score = consensus × mode/domain source preference + triage.
    let triage_terms = ranking::query_terms(&format!("{} {}", raw_query, effective_query));
    let selection_score = |c: &Candidate| -> f64 {
        pre_scrape_triage_score(
            c,
            raw_query,
            &triage_terms,
            intel,
            profile.mode,
            profile.domain,
        )
    };
    ranked.sort_by(|a, b| {
        selection_score(b)
            .partial_cmp(&selection_score(a))
            .unwrap_or(std::cmp::Ordering::Equal)
            .then_with(|| b.families.len().cmp(&a.families.len()))
    });

    // When strict domain restriction is active, enforce it BEFORE summarizing
    // `discovered_domains` and counting `deduplicated_urls` so external/spam
    // domains can never leak into `discovered_domains` or `content[0].text`.
    if domain_filter.is_strict_active() {
        ranked.retain(|c| domain_filter.is_included(&c.url));
    }

    let deduplicated_urls = ranked.len();
    let (discovered_domains, total_discovered_domains) =
        summarize_discovered_domains(&ranked, MAX_DISCOVERED_DOMAINS);

    if domain_filter.is_strict_active() {
        ranked.truncate(max_urls);
    } else if !domain_filter.include.is_empty() && max_urls > 0 {
        // Balanced soft priority (`strict_domains: false`): prioritize included
        // domains at the top (round-robin across included domains, capped at
        // half the budget so SearchWala's organic consensus answers stay intact),
        // then fill remaining slots with organic consensus results and any
        // leftover included hits if organic runs short.
        let mut by_inc_domain: Vec<std::collections::VecDeque<Candidate>> =
            (0..domain_filter.include.len())
                .map(|_| std::collections::VecDeque::new())
                .collect();
        let mut organic: Vec<Candidate> = Vec::new();
        for c in ranked {
            if let Some(pos) = domain_filter
                .include
                .iter()
                .position(|d| url_utils::host_matches_domain(&c.url, d))
            {
                by_inc_domain[pos].push_back(c);
            } else {
                organic.push(c);
            }
        }
        // Order included domain buckets by their best candidate's score so the
        // strongest matching included domain always takes rank #1.
        by_inc_domain.sort_by(|a, b| {
            let sa = a.front().map(&selection_score).unwrap_or(f64::NEG_INFINITY);
            let sb = b.front().map(&selection_score).unwrap_or(f64::NEG_INFINITY);
            sb.partial_cmp(&sa).unwrap_or(std::cmp::Ordering::Equal)
        });

        let priority_cap = (max_urls / 2).max(1);
        let mut prioritized: Vec<Candidate> = Vec::with_capacity(max_urls);
        let mut overflow_included: Vec<Candidate> = Vec::new();
        loop {
            let mut progressed = false;
            for bucket in &mut by_inc_domain {
                if let Some(cand) = bucket.pop_front() {
                    progressed = true;
                    if prioritized.len() < priority_cap {
                        prioritized.push(cand);
                    } else {
                        overflow_included.push(cand);
                    }
                }
            }
            if !progressed {
                break;
            }
        }

        for cand in organic {
            if prioritized.len() >= max_urls {
                break;
            }
            prioritized.push(cand);
        }
        for cand in overflow_included {
            if prioritized.len() >= max_urls {
                break;
            }
            prioritized.push(cand);
        }
        ranked = prioritized;
    } else {
        ranked.truncate(max_urls);
    }

    CandidateSelection {
        rrf_scores,
        candidates: ranked,
        deduplicated_urls,
        discovered_domains,
        total_discovered_domains,
    }
}

/// Project ranked candidates into [`SerpHit`] items for SERP-only callers.
fn candidates_to_serp_hits(candidates: &[Candidate], limit: usize) -> Vec<SerpHit> {
    candidates
        .iter()
        .take(limit)
        .enumerate()
        .map(|(i, c)| {
            let abstract_text = c
                .full_text
                .as_deref()
                .filter(|t| crate::util::char_len(t) >= 80)
                .map(|t| crate::util::truncate_chars(t.trim(), 2_000).to_string());
            SerpHit {
                rank: i + 1,
                url: c.url.clone(),
                title: c.title.clone(),
                snippet: crate::util::truncate_chars(&c.snippet, 400).to_string(),
                engines: c.engines.clone(),
                found_by_families: c.families.len(),
                rrf_score: c.rrf,
                published: c.published,
                abstract_text,
            }
        })
        .collect()
}

/// The page title to show and cite.
///
/// Trafilatura's title for Wikipedia is the article's short description, so
/// "Bain-marie" was cited as "type of heated bath" and "Boiling" as "type of
/// vaporization; bulk phenomenon". When the extracted title shares no word
/// with the search engine's title, the engine's is kept.
fn pick_title(url: &str, engine_title: &str, extracted: String) -> String {
    // Wikipedia: the article title is in the URL, exactly.
    if let Some(t) = wikipedia_title(url) {
        return t;
    }
    if extracted.trim().is_empty() {
        return engine_title.to_string();
    }
    if engine_title.trim().is_empty() {
        return extracted;
    }
    // Content words only: "the" shared by "The Legend of Tennessee
    // Moltisanti" and "episode of The Sopranos" is not agreement.
    let words = |s: &str| -> std::collections::HashSet<String> {
        crate::ranking::query_terms(s)
            .into_iter()
            .map(|w| crate::highlight::stem(&w))
            .collect()
    };
    if words(engine_title).is_disjoint(&words(&extracted)) {
        engine_title.to_string()
    } else {
        extracted
    }
}

/// "https://en.wikipedia.org/wiki/Invention_of_the_telephone" ->
/// "Invention of the telephone".
fn wikipedia_title(url: &str) -> Option<String> {
    let u = url::Url::parse(url).ok()?;
    if !u.host_str()?.ends_with("wikipedia.org") {
        return None;
    }
    let slug = u.path().strip_prefix("/wiki/")?;
    if slug.is_empty() || slug.contains(':') {
        return None; // Special:, Talk:, File: pages
    }
    let title = urlencoding::decode(slug).ok()?.replace('_', " ");
    (!title.trim().is_empty()).then_some(title)
}

/// The highest-ranked pages a scrape early exit must wait for.
const MUST_FINISH_TOP: usize = 3;

/// The whole pipeline. Every mode-dependent decision is read from the
/// `ModeProfile` — see `modes.rs` for why that matters.
pub async fn execute_search_full(
    params: SearchParams,
    temp_db: Option<&crate::cache::TempDb>,
    progress: Option<tokio::sync::mpsc::Sender<Progress>>,
) -> SearchResponse {
    let start = std::time::Instant::now();
    let query = params.query.trim().to_string();
    let (mode, domain) = modes::parse(params.focus_mode.as_deref(), params.domain.as_deref());

    // Validate the caller's LLM config once. A bad config is reported in
    // `llm_error`; the search itself still runs.
    let mut params = params;
    let mut llm_config_error: Option<String> = None;
    if let Some(cfg) = params.llm.take() {
        match llm::prepare_config(cfg) {
            Ok(cfg) => params.llm = Some(cfg),
            Err(e) => {
                tracing::warn!("LLM config rejected: {}", e);
                llm_config_error = Some(e);
            }
        }
    }

    // ── Conversation memory formatting ──
    let (memory_text, memory_used) = match &params.llm {
        Some(cfg) if !params.memory.is_empty() => {
            // About a fifth of the model's window, at ~4 characters a token.
            let window = llm::ContextBudget::for_config(cfg).max_tokens;
            crate::chats::memory_block(&params.memory, (window * 4 / 5).clamp(1_500, 60_000))
        }
        _ => (String::new(), 0),
    };

    // ── Unified Semantic Query Understanding (LLM-First) ──
    // Runs a single fast semantic understanding pass that classifies category
    // (Product/Org, Live News, History/Static, General Everyday), resolves
    // follow-up context while locking entities, fixes phonetic/voice typos,
    // neutralizes stale years dynamically, and plans semantic sub-queries.
    let mut effective_query = query.clone();
    let mut search_query = None;
    let mut copilot_out = None;
    let mut semantic_plan: Option<crate::copilot::SemanticQueryPlan> = None;

    let should_run_semantic_plan = params.llm.is_some()
        && (!matches!(mode, modes::Mode::Fast)
            || params.enable_copilot
            || !params.memory.is_empty()
            || query.split_whitespace().count() > 5
            || !query.is_ascii());

    if should_run_semantic_plan {
        if let Some(cfg) = &params.llm {
            semantic_plan = crate::copilot::understand_query(&query, &params.memory, cfg, 3).await;
        }
    }

    if let Some(ref plan) = semantic_plan {
        tracing::info!(
            "Semantic Query Plan: category={} intent={:?} time_sensitive={} entities={:?} primary=[{}] sub_queries={:?}",
            plan.category,
            plan.intent,
            plan.is_time_sensitive,
            plan.entities,
            plan.primary_query,
            plan.sub_queries
        );
        effective_query = plan.primary_query.clone();
        if !effective_query.eq_ignore_ascii_case(&query) {
            search_query = Some(effective_query.clone());
        }
        if params.enable_copilot {
            copilot_out = Some(effective_query.clone());
        }
    } else {
        // Fallback if semantic understanding timed out or was skipped
        if let (Some(cfg), false) = (&params.llm, params.memory.is_empty()) {
            if let Some(q) = synthesis::standalone_query(&query, &params.memory, cfg).await {
                tracing::info!("Follow-up searched as: [{}]", q);
                effective_query = q.clone();
                search_query = Some(q);
            }
        }
        if params.enable_copilot {
            if let Some(cfg) = &params.llm {
                let rewritten = crate::copilot::rewrite_query(&effective_query, cfg).await;
                tracing::info!(
                    "Copilot rewrote query: [{}] -> [{}]",
                    effective_query,
                    rewritten
                );
                copilot_out = Some(rewritten.clone());
                effective_query = rewritten;
            }
        }
    }

    // ── Query intelligence + mode profile ──
    let mut intel = query_intel::analyze_query(&effective_query);
    // Merge entities from raw query as well so nothing from the raw prompt is lost
    for raw_ent in query_intel::analyze_query(&query).key_entities {
        if !intel
            .key_entities
            .iter()
            .any(|e| e.eq_ignore_ascii_case(&raw_ent))
        {
            intel.key_entities.push(raw_ent);
        }
    }
    if let Some(ref plan) = semantic_plan {
        intel.semantic_category = plan.category;
        if let Some(ref sem_intent) = plan.intent {
            intel.intent = sem_intent.clone();
        }
        if plan.is_time_sensitive {
            intel.is_time_sensitive = true;
        }
        if intel.semantic_category == query_intel::SemanticCategory::HistoryStatic
            && intel.intent == query_intel::QueryIntent::HowTo
        {
            intel.intent = query_intel::QueryIntent::Research;
        }
        if intel.is_time_sensitive && intel.intent == query_intel::QueryIntent::Factual {
            intel.intent = query_intel::QueryIntent::Temporal;
        }
        intel.boost_news_engines = intel.is_time_sensitive
            || intel.intent == query_intel::QueryIntent::Temporal
            || intel.semantic_category == query_intel::SemanticCategory::LiveNews;
        intel.corrective_query = plan.corrective_query.clone();

        // When the LLM Semantic Query Plan extracted canonical entities, use them
        // directly (avoiding naive lowercase prefix tokens from raw query fallback).
        if !plan.entities.is_empty() {
            let mut clean_entities = Vec::new();
            for ent in &plan.entities {
                let trimmed = ent.trim();
                if !trimmed.is_empty()
                    && !clean_entities
                        .iter()
                        .any(|e: &String| e.eq_ignore_ascii_case(trimmed))
                {
                    clean_entities.push(trimmed.to_string());
                }
            }
            intel.key_entities = clean_entities;
        }
    }

    let intel_query = effective_query.clone();
    let profile = modes::resolve(mode, domain, &intel, params.max_results);
    tracing::info!(
        "Mode {} [{}] domain={} category={} intent={} complexity={} | engines={} pages={} deadline={:?} \
         urls={} subq={} top_k={} synthesis={:?}",
        mode.as_str(), profile.tier, domain.as_str(), intel.semantic_category, intel.intent, intel.complexity,
        profile.engines.len(), profile.page_budget, profile.engine_deadline,
        profile.max_urls, profile.max_sub_queries, profile.llm_top_k, profile.synthesis
    );

    // ── Recency hint for engines (resolved dynamically via chrono::Utc::now().year()) ──
    // Skipped for Study and Academic: those topics are evergreen.
    effective_query = apply_recency_hint(&effective_query, &intel, mode);

    // ── Sub-queries ──
    let sub_query_cap = if semantic_plan.is_some() && !matches!(mode, modes::Mode::Fast) {
        profile.max_sub_queries.max(2)
    } else {
        profile.max_sub_queries
    };

    let mut query_variations: Vec<String> = if sub_query_cap > 1
        && query_intel::multi_query_enabled()
    {
        if let Some(ref plan) = semantic_plan {
            let mut v = vec![effective_query.clone()];
            for sq in &plan.sub_queries {
                if !v.iter().any(|existing| existing.eq_ignore_ascii_case(sq)) {
                    v.push(sq.clone());
                }
            }
            v.truncate(sub_query_cap);
            v
        } else {
            let mut planned = None;
            if profile.llm_query_planning {
                if let Some(cfg) = &params.llm {
                    planned = synthesis::plan_subqueries(
                        &effective_query,
                        cfg,
                        sub_query_cap.saturating_sub(1),
                    )
                    .await;
                }
            }
            match planned {
                Some(p) => {
                    tracing::info!("LLM planned {} sub-queries", p.len());
                    let mut v = vec![effective_query.clone()];
                    v.extend(p);
                    v.truncate(sub_query_cap);
                    v
                }
                None => {
                    modes::facet_queries(&effective_query, &intel, profile.facets, sub_query_cap)
                }
            }
        }
    } else {
        vec![effective_query.clone()]
    };
    if query_variations.is_empty() {
        query_variations.push(effective_query.clone());
    }
    // `site:` only goes to engines that honour it — the profile's roster for
    // site-filtered modes contains nothing else.
    if profile.site_filter.is_some() {
        query_variations = query_variations
            .iter()
            .map(|q| modes::apply_site_filter(q, profile.site_filter))
            .collect();
    }
    tracing::info!(
        "Sub-queries: {:?}",
        query_variations
            .iter()
            .map(|q| crate::util::truncate_chars(q, 60))
            .collect::<Vec<_>>()
    );

    let engine_list = engines::filter_healthy(profile.engines.clone());
    let engine_instances = engines::get_engines(&engine_list);

    emit(
        &progress,
        Progress::Plan {
            mode: mode.as_str().into(),
            tier: profile.tier.into(),
            domain: domain.as_str().into(),
            engines: engine_instances.len(),
            sub_queries: query_variations.clone(),
            max_urls: profile.max_urls,
        },
    )
    .await;

    // ── Dispatch ──
    let (plain_fetcher, stealth_fetcher) = fetcher::build_pair(None);
    let stealth_engine_set: std::collections::HashSet<String> =
        config::stealth_engines().into_iter().collect();
    let stealth_mode = config::stealth_mode();
    let proxy_pool = ProxyPoolManager::from_env();
    let engine_concurrency = config::engine_concurrency().clamp(1, 40);
    let engine_semaphore = Arc::new(Semaphore::new(engine_concurrency));

    let stage_start = std::time::Instant::now();
    let (tagged, all_queries) = dispatch_with_domain_targets(
        &engine_instances,
        &query_variations,
        &effective_query,
        &params.domain_filter,
        profile.site_filter.is_some(),
        &plain_fetcher,
        &stealth_fetcher,
        &stealth_engine_set,
        &stealth_mode,
        &proxy_pool,
        &engine_semaphore,
        config::jitter_min_ms(),
        config::jitter_max_ms(),
        engine_concurrency,
        profile.engine_deadline,
        profile.page_budget,
        profile.engine_early_exit,
    )
    .await;

    // Multi-query modes score each page by weighted fusion across sub-queries;
    // the raw results are kept either way so engine/family lists stay exact.
    let fused_scores = compute_fused_scores(&tagged, &all_queries);
    let mut all_results: Vec<RawSearchResult> = tagged.into_iter().flat_map(|(_, v)| v).collect();

    if profile.site_filter.is_some() {
        let before = all_results.len();
        all_results.retain(|r| modes::matches_site_filter(&r.url, profile.site_filter));
        tracing::info!("Site filter kept {}/{} results", all_results.len(), before);
    }

    let engine_dispatch_ms = stage_start.elapsed().as_millis();
    let families: std::collections::HashSet<&str> = all_results
        .iter()
        .map(|r| engines::engine_family(&r.engine))
        .collect();
    tracing::info!(
        "⏱ Stage [engine_dispatch]: {}ms — {} results, {} families",
        engine_dispatch_ms,
        all_results.len(),
        families.len()
    );
    emit(
        &progress,
        Progress::Engines {
            raw_results: all_results.len(),
            families: families.len(),
            ms: engine_dispatch_ms,
        },
    )
    .await;

    // ── Consensus ranking + source selection ──
    let stage_start = std::time::Instant::now();
    let total_raw = all_results.len();
    let sel = fuse_and_select_candidates(
        &all_results,
        fused_scores,
        &query,
        &effective_query,
        &intel,
        &profile,
        profile.max_urls,
        &params.domain_filter,
    );
    let rrf_ms = stage_start.elapsed().as_millis();
    let rrf_scores = sel.rrf_scores;
    let ranked = sel.candidates;
    let discovered_domains = sel.discovered_domains;
    let total_discovered_domains = sel.total_discovered_domains;
    let deduped_count = sel.deduplicated_urls;

    let search_results: Vec<SearchHit> = ranked
        .iter()
        .map(|c| SearchHit {
            url: c.url.clone(),
            title: c.title.clone(),
            snippet: crate::util::truncate_chars(&c.snippet, 400).to_string(),
            engine: c.engines.join(","),
            rrf_score: c.rrf,
            found_by_families: c.families.len(),
        })
        .collect();
    emit(
        &progress,
        Progress::Sources {
            results: search_results.clone(),
        },
    )
    .await;

    // ── Scrape ──
    let concurrency = config::concurrency().min(40);
    let semaphore = Arc::new(Semaphore::new(concurrency));
    let scrape_client = shared_scrape_client().clone();
    let scrape_max_bytes = config::max_html_bytes();
    let scrape_timeout = profile.scrape_timeout;
    let snippet_fallback = profile.snippet_fallback;

    let stage_start = std::time::Instant::now();
    let mut join_set = JoinSet::new();
    // Early exit used to take whichever pages finished FIRST. In Fast mode
    // (exit at 6 sources) that dropped the #1 and #3 ranked pages for "boiling
    // point of water" — Wikipedia's "Boiling point", found by two engines —
    // and kept "Bain-marie", which then supplied the top passage. The
    // top-ranked pages must finish (or fail) before an early exit; the stage
    // deadline still caps the wait.
    let must_finish = MUST_FINISH_TOP.min(ranked.len());
    let mut pending_top: std::collections::HashSet<usize> = (0..must_finish).collect();
    let selection_rank: HashMap<String, usize> = ranked
        .iter()
        .enumerate()
        .map(|(i, c)| (c.url.clone(), i))
        .collect();
    for (rank_idx, c) in ranked.into_iter().enumerate() {
        let sem = semaphore.clone();
        let client = scrape_client.clone();
        join_set.spawn(async move {
            let result = async move {
                let engine_name = c.engines.join(",");

                // Engine-supplied text (OpenAlex / arXiv abstracts): no fetch.
                // Publisher landing pages are slow, often paywalled, often blocked.
                if let Some(ft) = c
                    .full_text
                    .as_ref()
                    .filter(|t| crate::util::char_len(t) >= 150)
                {
                    return Some(SourceResult {
                        url: c.url.clone(),
                        title: c.title.clone(),
                        extracted_text: ft.clone(),
                        char_count: crate::util::char_len(ft),
                        engine: engine_name,
                        published: c.published,
                        language: None,
                        byline: None,
                        site_name: None,
                        confidence: 0.9,
                        extractor: "abstract",
                    });
                }

                if let Some(cached) = crate::cache_layer::get_page(&c.url).await {
                    return Some(SourceResult {
                        url: c.url.clone(),
                        title: pick_title(&c.url, &c.title, cached.title.clone()),
                        extracted_text: cached.text.clone(),
                        char_count: crate::util::char_len(&cached.text),
                        engine: engine_name,
                        published: cached.published.or(c.published),
                        language: cached.language.clone(),
                        byline: cached.byline.clone(),
                        site_name: cached.site_name.clone(),
                        confidence: cached.confidence,
                        extractor: cached.source,
                    });
                }

                let scraped = {
                    let _permit = sem.acquire().await.ok()?;
                    scrape_url(&client, &c.url, scrape_timeout, scrape_max_bytes, mode).await
                };
                match scraped {
                    Some(e) => {
                        crate::cache_layer::put_page(&c.url, e.clone()).await;
                        let title = pick_title(&c.url, &c.title, e.title);
                        Some(SourceResult {
                            char_count: crate::util::char_len(&e.text),
                            url: c.url,
                            title,
                            extracted_text: e.text,
                            engine: engine_name,
                            published: e.published.or(c.published),
                            language: e.language,
                            byline: e.byline,
                            site_name: e.site_name,
                            confidence: e.confidence,
                            extractor: e.source,
                        })
                    }
                    // Blocked or empty page: in modes where that is routine
                    // (Reddit CAPTCHA walls, YouTube description-only pages) the
                    // engine snippet is the best text available. Low confidence so
                    // it ranks below any real extraction.
                    None if snippet_fallback && crate::util::char_len(&c.snippet) >= 80 => {
                        let text = format!("{}\n\n{}", c.title, c.snippet);
                        Some(SourceResult {
                            char_count: crate::util::char_len(&text),
                            url: c.url,
                            title: c.title,
                            extracted_text: text,
                            engine: engine_name,
                            published: c.published,
                            language: None,
                            byline: None,
                            site_name: None,
                            confidence: 0.35,
                            extractor: "snippet",
                        })
                    }
                    None => None,
                }
            }
            .await;
            (rank_idx, result)
        });
    }

    // Collect, stopping early once BOTH the source and text targets are met.
    // The thresholds come from the profile — v8.0 applied Auto's 25-source /
    // 80k-char exit to every mode, so Deep stopped as early as Auto. An
    // explicitly-set env var still overrides, for operators.
    let want_sources = config::env_usize("SCRAPE_MIN_SOURCES")
        .unwrap_or(profile.scrape_min_sources)
        .min(deduped_count.max(1));
    let want_chars = config::env_usize("SCRAPE_MIN_CHARS").unwrap_or(profile.scrape_min_chars);
    let stage_deadline = config::env_usize("SCRAPE_STAGE_DEADLINE")
        .map(|s| Duration::from_secs(s as u64))
        .unwrap_or(profile.scrape_stage_deadline);
    let scrape_deadline = tokio::time::Instant::now() + stage_deadline;

    let mut raw_scraped_results: Vec<SourceResult> = Vec::new();
    let mut scraped_chars = 0usize;
    let mut done = 0usize;
    let total_tasks = join_set.len();
    loop {
        if done >= total_tasks {
            break;
        }
        if raw_scraped_results.len() >= want_sources
            && scraped_chars >= want_chars
            && pending_top.is_empty()
        {
            tracing::info!(
                "Scrape early-exit: {} sources / {} chars ({}/{} tasks)",
                raw_scraped_results.len(),
                scraped_chars,
                done,
                total_tasks
            );
            join_set.abort_all();
            break;
        }
        let next = tokio::select! {
            res = join_set.join_next() => res,
            _ = tokio::time::sleep_until(scrape_deadline) => {
                if !join_set.is_empty() {
                    tracing::info!(
                        "Scrape deadline ({:?}): {} sources from {}/{} tasks",
                        stage_deadline, raw_scraped_results.len(), done, total_tasks
                    );
                    join_set.abort_all();
                }
                break;
            }
        };
        match next {
            Some(Ok((idx, Some(r)))) => {
                pending_top.remove(&idx);
                scraped_chars += r.char_count;
                raw_scraped_results.push(r);
                done += 1;
            }
            Some(Ok((idx, None))) => {
                pending_top.remove(&idx);
                done += 1;
            }
            Some(Err(e)) => {
                if !e.is_cancelled() {
                    tracing::debug!("Scrape task error: {}", e);
                }
                done += 1;
            }
            None => break,
        }
    }
    while let Some(j) = join_set.join_next().await {
        if let Ok((_, Some(r))) = j {
            scraped_chars += r.char_count;
            raw_scraped_results.push(r);
        }
    }

    let scrape_ms = stage_start.elapsed().as_millis();
    let scraped_count = raw_scraped_results.len();
    let by_extractor = {
        let mut m: std::collections::BTreeMap<&str, usize> = Default::default();
        for r in &raw_scraped_results {
            *m.entry(r.extractor).or_insert(0) += 1;
        }
        m
    };
    tracing::info!(
        "⏱ Stage [scrape]: {}ms — {} sources from {} URLs | extracted {} chars (avg {}/source) | {:?}",
        scrape_ms,
        scraped_count,
        deduped_count,
        scraped_chars,
        scraped_chars / scraped_count.max(1),
        by_extractor
    );
    emit(
        &progress,
        Progress::Extracted {
            sources: scraped_count,
            chars: scraped_chars,
            ms: scrape_ms,
        },
    )
    .await;

    // ── Rank chunks ──
    // Build the lexical ranking query: preserve the user's raw question while
    // appending any resolved/typo-corrected entities from semantic understanding
    // so both native-language and English/resolved entity passages score fairly.
    let ranking_query = build_ranking_query(&query, &intel_query, &intel);
    let mut rrf_scores = rrf_scores;
    let rank_opts = ranking::RankOptions::from_profile(&profile);

    let stage_start = std::time::Instant::now();
    let mut results = ranking::rank_with_options(
        &ranking_query,
        &raw_scraped_results,
        profile.llm_top_k,
        Some(&rrf_scores),
        Some(&intel),
        &rank_opts,
    );

    // ── Self-Correcting Second-Pass Retrieval ──
    // If the top ranked chunks fail entity or current-year coverage on a non-Fast
    // query, dispatch a targeted corrective query and merge the new sources.
    if !matches!(mode, modes::Mode::Fast)
        && profile.site_filter.is_none()
        && !params.domain_filter.is_strict_active()
        && !evidence_is_sufficient(&results, &intel)
    {
        if let Some(corrective_q) = build_corrective_query(&intel, &query_variations) {
            tracing::info!(
                "🔄 Self-correcting retrieval triggered (insufficient pass-1 coverage): [{}]",
                corrective_q
            );
            let corr_variations = vec![corrective_q.clone()];
            let corr_deadline = Duration::from_millis(2_500);
            let corr_tagged = dispatch_engines(
                &engine_instances,
                &corr_variations,
                &plain_fetcher,
                &stealth_fetcher,
                &stealth_engine_set,
                &stealth_mode,
                &proxy_pool,
                &engine_semaphore,
                config::jitter_min_ms(),
                config::jitter_max_ms(),
                engine_concurrency,
                corr_deadline,
                1,
                Some((18, 2)),
            )
            .await;

            let corr_raw: Vec<RawSearchResult> =
                corr_tagged.into_iter().flat_map(|(_, v)| v).collect();
            if !corr_raw.is_empty() {
                let corr_rrf = ranking::compute_rrf_scores(&corr_raw);
                for (k, v) in &corr_rrf {
                    let entry = rrf_scores.entry(k.clone()).or_insert(0.0);
                    if *v > *entry {
                        *entry = *v;
                    }
                }

                let mut seen_urls: std::collections::HashSet<String> = selection_rank
                    .keys()
                    .map(|u| url_utils::canonical_key(u))
                    .collect();
                for r in &raw_scraped_results {
                    seen_urls.insert(url_utils::canonical_key(&r.url));
                }

                let mut corr_candidates: Vec<Candidate> = Vec::new();
                for item in corr_raw {
                    let key = url_utils::canonical_key(&item.url);
                    if !params.domain_filter.allows_url(&item.url)
                        || !seen_urls.insert(key.clone())
                        || (!params.domain_filter.is_included(&item.url)
                            && url_utils::should_skip(&item.url))
                    {
                        continue;
                    }
                    corr_candidates.push(Candidate {
                        url: item.url,
                        title: item.title,
                        snippet: item.snippet,
                        engines: vec![item.engine.clone()],
                        families: vec![engines::engine_family(&item.engine)],
                        rrf: corr_rrf.get(&key).copied().unwrap_or(0.01),
                        published: item.published,
                        full_text: item.full_text,
                    });
                }

                corr_candidates.sort_by(|a, b| {
                    b.rrf
                        .partial_cmp(&a.rrf)
                        .unwrap_or(std::cmp::Ordering::Equal)
                });
                corr_candidates.truncate(6);

                if !corr_candidates.is_empty() {
                    let mut corr_join = JoinSet::new();
                    for c in corr_candidates {
                        let sem = semaphore.clone();
                        let client = scrape_client.clone();
                        corr_join.spawn(async move {
                            let engine_name = c.engines.join(",");
                            if let Some(cached) = crate::cache_layer::get_page(&c.url).await {
                                return Some(SourceResult {
                                    url: c.url.clone(),
                                    title: pick_title(&c.url, &c.title, cached.title.clone()),
                                    extracted_text: cached.text.clone(),
                                    char_count: crate::util::char_len(&cached.text),
                                    engine: engine_name,
                                    published: cached.published.or(c.published),
                                    language: cached.language.clone(),
                                    byline: cached.byline.clone(),
                                    site_name: cached.site_name.clone(),
                                    confidence: cached.confidence,
                                    extractor: cached.source,
                                });
                            }
                            let _permit = sem.acquire().await.ok()?;
                            let scraped =
                                scrape_url(&client, &c.url, scrape_timeout, scrape_max_bytes, mode)
                                    .await?;
                            crate::cache_layer::put_page(&c.url, scraped.clone()).await;
                            let title = pick_title(&c.url, &c.title, scraped.title);
                            Some(SourceResult {
                                char_count: crate::util::char_len(&scraped.text),
                                url: c.url,
                                title,
                                extracted_text: scraped.text,
                                engine: engine_name,
                                published: scraped.published.or(c.published),
                                language: scraped.language,
                                byline: scraped.byline,
                                site_name: scraped.site_name,
                                confidence: scraped.confidence,
                                extractor: scraped.source,
                            })
                        });
                    }

                    let corr_scrape_deadline =
                        tokio::time::Instant::now() + Duration::from_millis(3_000);
                    let mut added_sources = 0usize;
                    loop {
                        let next = tokio::select! {
                            res = corr_join.join_next() => res,
                            _ = tokio::time::sleep_until(corr_scrape_deadline) => {
                                corr_join.abort_all();
                                break;
                            }
                        };
                        match next {
                            Some(Ok(Some(src))) => {
                                raw_scraped_results.push(src);
                                added_sources += 1;
                            }
                            Some(_) => {}
                            None => break,
                        }
                    }

                    if added_sources > 0 {
                        tracing::info!(
                            "🔄 Self-correcting pass added {} new sources; re-ranking pool ({} total sources)",
                            added_sources,
                            raw_scraped_results.len()
                        );
                        results = ranking::rank_with_options(
                            &ranking_query,
                            &raw_scraped_results,
                            profile.llm_top_k,
                            Some(&rrf_scores),
                            Some(&intel),
                            &rank_opts,
                        );
                    }
                }
            }
        }
    }

    let rank_ms = stage_start.elapsed().as_millis();
    let scraped_count = raw_scraped_results.len();
    tracing::info!(
        "⏱ Stage [rank]: {}ms — {} chunks from {} sources",
        rank_ms,
        results.len(),
        scraped_count
    );

    // The verbatim answer sentence is known as soon as passages are ranked;
    // streaming clients show it while the LLM is still writing. The opening
    // of the best-ranked pages is considered too: ranked passages favour
    // sentences dense in query words, and the lead is where an article
    // states its answer.
    let leads =
        crate::highlight::leads(&raw_scraped_results, |url| selection_rank.get(url).copied());
    let highlight = crate::highlight::extract(&effective_query, &intel, &results, &leads);
    crate::highlight::dump_case(
        &effective_query,
        &intel_query,
        &results,
        &leads,
        highlight.as_ref(),
    );
    if let Some(h) = &highlight {
        emit(
            &progress,
            Progress::Highlight {
                highlight: h.clone(),
            },
        )
        .await;
    }

    // ── Synthesis ──
    let session_id = match temp_db {
        Some(db) => Some(db.create_session(&query).await),
        None => None,
    };
    if let (Some(db), Some(sid)) = (temp_db, session_id.as_deref()) {
        db.update_status(sid, "scraping_done", "").await;
        db.update_sources(sid, scraped_count).await;
    }

    let stage_start = std::time::Instant::now();
    let llm_result = match params.llm {
        Some(cfg) if !results.is_empty() => {
            emit(
                &progress,
                Progress::Synthesizing {
                    synthesis: format!("{:?}", profile.synthesis),
                },
            )
            .await;
            synthesis::synthesize(
                &query,
                cfg,
                &results,
                &intel,
                &profile,
                &memory_text,
                temp_db,
                session_id.as_deref(),
            )
            .await
        }
        Some(_) => llm::LlmExecutionResult {
            llm_error: Some("llm_skipped: no scraped content available".to_string()),
            ..Default::default()
        },
        None => llm::LlmExecutionResult {
            llm_error: llm_config_error,
            ..Default::default()
        },
    };
    let llm_ms = stage_start.elapsed().as_millis();

    if let (Some(db), Some(sid)) = (temp_db, session_id.as_deref()) {
        db.wipe_session(sid).await;
    }

    let elapsed = start.elapsed().as_secs_f64();
    tracing::info!(
        "⏱ Pipeline TOTAL: {:.0}ms — engines={}ms scrape={}ms rank={}ms rrf={}ms llm={}ms | \
         raw={} selected={} scraped={} chunks={} mode={}",
        elapsed * 1000.0,
        engine_dispatch_ms,
        scrape_ms,
        rank_ms,
        rrf_ms,
        llm_ms,
        total_raw,
        deduped_count,
        scraped_count,
        results.len(),
        profile.tier
    );

    let sources_processed = results.len();

    SearchResponse {
        query,
        search_query,
        memory_used,
        conversation_id: None,
        turn_id: None,
        mode: mode.as_str().to_string(),
        tier: profile.tier.to_string(),
        domain: domain.as_str().to_string(),
        sub_queries: query_variations,
        sources_found: total_raw,
        sources_processed,
        sources_extracted: scraped_count,
        chars_extracted: scraped_chars,
        results,
        search_results,
        discovered_domains,
        total_discovered_domains,
        copilot_query: copilot_out,
        llm_answer: llm_result.llm_answer,
        llm_error: llm_result.llm_error,
        citation_map: llm_result.citation_map,
        citation_checks: llm_result.citation_checks,
        highlight,
        elapsed_seconds: elapsed,
        engine_stats: EngineStats {
            engines_queried: engine_list,
            total_raw_results: total_raw,
            deduplicated_urls: deduped_count,
        },
    }
}

// =============================================================================
// Engine Dispatch Helper — v6.1.0: JoinSet with early-cancel
//
// Uses JoinSet instead of join_all so results stream as they complete.
// Cancels remaining engines once we have enough good sources OR the
// deadline expires. This controls p99 latency, which fixed concurrency
// can't.
//
// Deadline: lite 2.5s, research 8s, specialized 5s, default 6s
// =============================================================================

#[allow(clippy::too_many_arguments)]
async fn dispatch_engines(
    engine_instances: &[std::sync::Arc<dyn engines::SearchEngine>],
    query_variations: &[String],
    plain_fetcher: &Fetcher,
    stealth_fetcher: &Fetcher,
    stealth_engine_set: &std::collections::HashSet<String>,
    stealth_mode: &str,
    proxy_pool: &ProxyPoolManager,
    engine_semaphore: &Arc<Semaphore>,
    jitter_min: u64,
    jitter_max: u64,
    engine_concurrency: usize,
    deadline: Duration,
    page_budget: usize,
    early_exit: Option<(usize, usize)>,
) -> Vec<(usize, Vec<RawSearchResult>)> {
    let mut join_set = JoinSet::new();
    let mut dispatch_index: u64 = 0;

    for (variation_idx, variation) in query_variations.iter().enumerate() {
        for engine in engine_instances {
            // v8.0: clone the Arc instead of re-running get_engines() inside the
            // task. That call re-entered generic::spec_for(), which used
            // Box::leak to fabricate &'static str templates for Google
            // variants — an unbounded leak, once per alias per variation per
            // search, for the process lifetime.
            let engine = std::sync::Arc::clone(engine);
            let ename = engine.name().to_string();

            // ── Pick the fetcher. THIS is the line v7.0 was missing. ──
            // The stealth client existed, was constructed per search, and was
            // passed in as `_stealth_client` — unreachable behind an
            // underscore, because the trait only took &reqwest::Client.
            let use_stealth = match stealth_mode {
                "off" => false,
                "on" => true,
                // "auto": the engine declares its own need, with the
                // STEALTH_ENGINES env list as an override.
                _ => engine.needs_stealth() || stealth_engine_set.contains(&ename),
            };
            let fetcher = if use_stealth {
                stealth_fetcher.clone()
            } else {
                plain_fetcher.clone()
            };

            let query_variant = variation.clone();
            let jitter = config::random_jitter_ms(jitter_min, jitter_max);
            let proxy_hint = proxy_pool.next_proxy();
            let pool = proxy_pool.clone();
            let sem = engine_semaphore.clone();
            let extra_spread = (dispatch_index % engine_concurrency.max(1) as u64) * 15;
            dispatch_index += 1;

            join_set.spawn(async move {
                // ── SERP cache (P2-3) ──
                // Checked BEFORE the jitter sleep and the semaphore, so a warm
                // query skips engine HTTP entirely. Without this the dispatch
                // stage always consumed its full deadline waiting on slow
                // engines, which made a repeated query exactly as slow as a
                // cold one even with a warm page cache.
                // Keyed by page budget too: a 1-page Fast result must not be
                // served to a 3-page Deep search for the same query.
                if let Some(cached) =
                    crate::cache_layer::get_serp(&ename, &query_variant, page_budget).await
                {
                    tracing::debug!("SERP cache hit: {} ({} results)", ename, cached.len());
                    return (variation_idx, cached);
                }

                // ── Sleep BEFORE acquiring the permit (v8.0 P0-9) ──
                // v7.0 acquired the semaphore permit and *then* slept for
                // jitter + spread (up to ~785ms). Each task therefore occupied
                // a concurrency slot doing nothing. At ENGINE_CONCURRENCY=20
                // with 175 tasks that is ~7s of queued sleeping against a 6s
                // deadline, so most engines were aborted before they ever
                // issued a request. The measured run completed "33/35 engines"
                // while only 4 produced data.
                let delay = jitter.saturating_add(extra_spread);
                if delay > 0 {
                    tokio::time::sleep(Duration::from_millis(delay)).await;
                }

                let _permit = match sem.acquire().await {
                    Ok(p) => p,
                    Err(_) => return (variation_idx, Vec::new()),
                };

                // Per-engine proxy still needs its own client; keep it rare.
                let fetcher = match proxy_hint.as_deref() {
                    Some(proxy) => {
                        tracing::debug!("Engine {} via proxy {}", ename, proxy);
                        if fetcher.is_stealth() {
                            Fetcher::Stealth(config::build_stealth_client(Some(proxy)))
                        } else {
                            Fetcher::Plain(fetcher::build_plain_client(Some(proxy)))
                        }
                    }
                    None => fetcher,
                };

                let mut results =
                    engines::with_page_budget(page_budget, engine.search(&fetcher, &query_variant))
                        .await;

                // A 200 page of filler that ignores the query is a block, not
                // data: drop it (and never cache it). Mixed pages lose only
                // their filler.
                match engines::health::screen_serp(&ename, &query_variant, &mut results) {
                    engines::health::SerpVerdict::Clean => {}
                    engines::health::SerpVerdict::Trimmed(n) => {
                        tracing::info!("{}: dropped {} off-topic results", ename, n);
                    }
                    engines::health::SerpVerdict::Blocked => {
                        tracing::warn!(
                            "{}: results unrelated to the query — soft block, discarded",
                            ename
                        );
                        engines::health::record_block(&ename, 0);
                    }
                }

                if let Some(proxy) = proxy_hint {
                    if results.is_empty() {
                        pool.mark_proxy_failure(&proxy);
                    } else {
                        pool.mark_proxy_success(&proxy);
                    }
                }

                // Empty results are deliberately NOT cached — an empty result
                // is usually a block or selector rot, and caching it would
                // persist the failure for the whole TTL.
                crate::cache_layer::put_serp(&ename, &query_variant, page_budget, results.clone())
                    .await;

                (variation_idx, results)
            });
        }
    }

    // ── Collect with a deadline-based early cancel ──
    let deadline_instant = tokio::time::Instant::now() + deadline;
    let mut all: Vec<(usize, Vec<RawSearchResult>)> = Vec::new();
    let mut tasks_completed = 0usize;
    let total_tasks = join_set.len();

    // Early-exit thresholds. The deadline should be a CEILING, not a floor:
    // in the measured run it was consumed in full on every query because four
    // engines always fail and the collector waited for them, so a fully warm
    // cache still cost 6,002 ms here.
    // Per-mode (see ModeProfile::engine_early_exit); an explicitly-set env
    // var still overrides for operators.
    let (min_results_to_exit, min_families_to_exit) = match (
        config::env_usize("EARLY_EXIT_MIN_RESULTS"),
        config::env_usize("EARLY_EXIT_MIN_FAMILIES"),
    ) {
        (Some(r), f) => (r, f.unwrap_or(3)),
        (None, _) => early_exit.unwrap_or((usize::MAX, usize::MAX)),
    };

    // Soft deadline: past 60% of the window, accept one family fewer. The
    // hard requirement is right while engines are still arriving, but when the
    // rest are hung or rate-limited (measured: Brave 429 + a stalled Marginalia
    // left Fast waiting the full 2.5 s for a third family that never came),
    // holding out only adds latency.
    let soft_instant = tokio::time::Instant::now() + deadline.mul_f64(0.6);
    let mut soft_passed = false;

    let sufficient =
        |all: &Vec<(usize, Vec<RawSearchResult>)>, relaxed: bool| -> Option<(usize, usize)> {
            let flat_len: usize = all.iter().map(|(_, v)| v.len()).sum();
            // Past the soft deadline, two thirds of the target count is enough.
            let need_results = if relaxed {
                min_results_to_exit.saturating_mul(2) / 3
            } else {
                min_results_to_exit
            };
            if flat_len < need_results {
                return None;
            }
            let fams: std::collections::HashSet<&str> = all
                .iter()
                .flat_map(|(_, v)| v.iter())
                .map(|r| engines::engine_family(&r.engine))
                .collect();
            let need = if relaxed {
                min_families_to_exit.saturating_sub(1).max(2)
            } else {
                min_families_to_exit
            };
            (fams.len() >= need).then_some((flat_len, fams.len()))
        };

    loop {
        if tasks_completed >= total_tasks {
            break;
        }
        // Stop as soon as coverage is sufficient for this mode.
        if let Some((n, f)) = sufficient(&all, soft_passed) {
            tracing::info!(
                "Engine early-exit{}: {} results from {} independent families \
                 ({}/{} tasks done) — aborting {} slower task(s)",
                if soft_passed { " (soft deadline)" } else { "" },
                n,
                f,
                tasks_completed,
                total_tasks,
                join_set.len()
            );
            join_set.abort_all();
            break;
        }

        let early_exit_enabled = min_results_to_exit != usize::MAX;
        let result = tokio::select! {
            res = join_set.join_next() => res,
            _ = tokio::time::sleep_until(soft_instant), if early_exit_enabled && !soft_passed => {
                soft_passed = true;
                continue;
            }
            _ = tokio::time::sleep_until(deadline_instant) => {
                let remaining = join_set.len();
                if remaining > 0 {
                    tracing::info!(
                        "Engine deadline ({:?}) reached: {}/{} tasks done, aborting {} (have {} batches)",
                        deadline, tasks_completed, total_tasks, remaining, all.len()
                    );
                    join_set.abort_all();
                }
                break;
            }
        };

        match result {
            Some(Ok(tagged)) => {
                all.push(tagged);
                tasks_completed += 1;
            }
            Some(Err(err)) => {
                if !err.is_cancelled() {
                    tracing::debug!("Engine task join error: {}", err);
                }
                tasks_completed += 1;
            }
            None => {
                tracing::debug!(
                    "Engine collect: set drained at {}/{} tasks",
                    tasks_completed,
                    total_tasks
                );
                break;
            }
        }
    }

    // Drain anything that finished between the abort and now.
    while let Some(result) = join_set.join_next().await {
        if let Ok(tagged) = result {
            all.push(tagged);
        }
    }

    // v8.0: count DISTINCT engines that produced results, not tasks. v7.0
    // logged "engines completed" for engines x variations, which made a
    // single-engine run look like 33 engines succeeding.
    let flat: Vec<&RawSearchResult> = all.iter().flat_map(|(_, v)| v.iter()).collect();
    let producing: std::collections::HashSet<&str> =
        flat.iter().map(|r| r.engine.as_str()).collect();
    let families: std::collections::HashSet<&str> = flat
        .iter()
        .map(|r| engines::engine_family(&r.engine))
        .collect();
    tracing::info!(
        "Engine dispatch: {}/{} tasks done | {} engines produced results | {} independent families | {} raw results",
        tasks_completed, total_tasks, producing.len(), families.len(), flat.len()
    );
    all
}

// =============================================================================
// Scrape & Single-URL Extraction Pipeline — with SSRF guard & retry on 403/429/503
// =============================================================================

/// Shared lazily-initialized scrape client (SSRF-guarded, pooled across calls).
static SCRAPE_CLIENT: std::sync::LazyLock<reqwest::Client> =
    std::sync::LazyLock::new(build_scrape_client);

/// Access the shared SSRF-guarded scrape client.
pub fn shared_scrape_client() -> &'static reqwest::Client {
    &SCRAPE_CLIENT
}

/// Fetch and extract readable content + metadata from a single public HTTP(S) URL.
///
/// Enforces SearchWala's full SSRF policy (`scrape_url_allowed`,
/// `PublicOnlyResolver`, per-hop redirect checks), `old.reddit.com` rewrite,
/// YouTube fast path, charset-aware decoding, bot-wall screening, Readability +
/// conditional Trafilatura + JSON-LD + Deep Reader + optional AMP fallback, and
/// the 6-hour in-memory page cache.
pub async fn fetch_and_extract_url(
    url: &str,
    opts: &FetchExtractOptions,
) -> Result<ExtractedPage, ExtractError> {
    fetch_and_extract_with_client(shared_scrape_client(), url, opts, true, false).await
}

async fn scrape_url(
    client: &reqwest::Client,
    url: &str,
    timeout: Duration,
    max_bytes: usize,
    mode: modes::Mode,
) -> Option<crate::extractor::Extracted> {
    let opts = FetchExtractOptions {
        timeout,
        max_bytes,
        allow_amp_fallback: mode != modes::Mode::Fast,
        // `execute_search_full` handles page cache lookup/insert before acquiring
        // the scrape semaphore so cache hits never consume a scrape slot.
        use_cache: false,
        allow_plain_text: false,
    };
    fetch_and_extract_with_client(client, url, &opts, true, mode == modes::Mode::YouTube)
        .await
        .ok()
        .map(|p| p.extracted)
}

async fn fetch_and_extract_with_client(
    client: &reqwest::Client,
    raw_url: &str,
    opts: &FetchExtractOptions,
    enforce_ssrf: bool,
    youtube_mode: bool,
) -> Result<ExtractedPage, ExtractError> {
    let start = std::time::Instant::now();
    let trimmed = raw_url.trim();
    if trimmed.is_empty() {
        return Err(ExtractError::InvalidUrl("URL cannot be empty".into()));
    }
    let parsed_raw = url::Url::parse(trimmed)
        .map_err(|e| ExtractError::InvalidUrl(format!("failed to parse URL: {e}")))?;

    // Check scheme & SSRF before rewriting or checking cache.
    if !matches!(parsed_raw.scheme(), "http" | "https") {
        return Err(ExtractError::BlockedBySsrf(format!(
            "scheme {:?} is not allowed",
            parsed_raw.scheme()
        )));
    }

    // New Reddit is a JavaScript shell; old.reddit serves real HTML. Applied in
    // every mode, since Reddit threads surface in ordinary searches too.
    let target_url = reddit_to_old(trimmed);
    let parsed_target = url::Url::parse(&target_url)
        .map_err(|e| ExtractError::InvalidUrl(format!("invalid rewritten URL: {e}")))?;
    if enforce_ssrf && !crate::ssrf::scrape_url_allowed(&parsed_target) {
        tracing::warn!("Refusing to scrape non-public URL: {}", target_url);
        return Err(ExtractError::BlockedBySsrf(format!(
            "non-public host rejected: {}",
            parsed_target.host_str().unwrap_or("<unknown>")
        )));
    }

    if opts.use_cache {
        if let Some(cached) = crate::cache_layer::get_page(&target_url).await {
            let extracted = (*cached).clone();
            let char_count = crate::util::char_len(&extracted.text);
            return Ok(ExtractedPage {
                url: trimmed.to_string(),
                final_url: target_url,
                extracted,
                char_count,
                cached: true,
                elapsed_ms: start.elapsed().as_millis() as u64,
            });
        }
    }

    let attempt =
        fetch_and_extract_attempt(client, &target_url, opts, enforce_ssrf, youtube_mode).await;
    let (final_url, extracted) = match attempt {
        Ok(ok) => ok,
        Err(ExtractError::AntiBotBlocked { .. }) => {
            // ── Retry once with fresh browser profile after jitter ──
            let delay = config::random_jitter_ms(200, 500);
            tokio::time::sleep(Duration::from_millis(delay)).await;
            tracing::debug!("Retry after block: {}", target_url);
            fetch_and_extract_attempt(client, &target_url, opts, enforce_ssrf, youtube_mode).await?
        }
        Err(err) => return Err(err),
    };

    if opts.use_cache {
        crate::cache_layer::put_page(&target_url, extracted.clone()).await;
    }

    let char_count = crate::util::char_len(&extracted.text);
    Ok(ExtractedPage {
        url: trimmed.to_string(),
        final_url,
        extracted,
        char_count,
        cached: false,
        elapsed_ms: start.elapsed().as_millis() as u64,
    })
}

async fn fetch_and_extract_attempt(
    client: &reqwest::Client,
    target_url: &str,
    opts: &FetchExtractOptions,
    enforce_ssrf: bool,
    youtube_mode: bool,
) -> Result<(String, crate::extractor::Extracted), ExtractError> {
    // Accept-Encoding is NOT set by hand: reqwest advertises exactly what it
    // can decode (the Phase-1 zstd fix), and a manual header would override it.
    let mut req = client.get(target_url).header(
        "Accept",
        "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
    );

    req = config::apply_browser_headers(req, target_url);

    if !opts.timeout.is_zero() {
        req = req.timeout(opts.timeout);
    }

    let resp = match req.send().await {
        Ok(r) => r,
        Err(e) => {
            tracing::debug!("Fetch failed {}: {}", target_url, e);
            if e.is_timeout() {
                return Err(ExtractError::Timeout);
            }
            let msg = e.to_string();
            if msg.contains("no public address") {
                return Err(ExtractError::BlockedBySsrf(
                    "host resolved to a non-public IP address".into(),
                ));
            }
            return Err(ExtractError::NetworkError(msg));
        }
    };

    let status = resp.status().as_u16();
    // ── Detect anti-bot blocks and retry ──
    if status == 403 || status == 429 || status == 503 {
        tracing::debug!("Blocked (HTTP {}) at {}", status, target_url);
        return Err(ExtractError::AntiBotBlocked { status });
    }

    // Redirect stopped by `scrape_redirect_policy` when targeting a non-public host.
    if resp.status().is_redirection() {
        if let Some(loc) = resp
            .headers()
            .get(reqwest::header::LOCATION)
            .and_then(|v| v.to_str().ok())
        {
            if let Ok(resolved) = resp.url().join(loc) {
                if !crate::ssrf::scrape_url_allowed(&resolved) {
                    return Err(ExtractError::BlockedBySsrf(format!(
                        "redirect target rejected: {}",
                        resolved.host_str().unwrap_or("<unknown>")
                    )));
                }
            }
        }
        return Err(ExtractError::HttpStatus { status });
    }

    if !resp.status().is_success() {
        return Err(ExtractError::HttpStatus { status });
    }

    let final_url = resp.url().to_string();
    let content_type = resp
        .headers()
        .get("content-type")
        .and_then(|ct| ct.to_str().ok())
        .unwrap_or("")
        .to_ascii_lowercase();

    let is_plain_text = content_type.contains("text/plain")
        || content_type.contains("text/markdown")
        || content_type.contains("text/x-markdown");
    let is_html = content_type.is_empty()
        || content_type.contains("text/html")
        || content_type.contains("application/xhtml");

    if !is_html && !(opts.allow_plain_text && is_plain_text) {
        return Err(ExtractError::UnsupportedContentType(content_type));
    }

    let bytes = resp
        .bytes()
        .await
        .map_err(|e| ExtractError::NetworkError(e.to_string()))?;

    // Charset-aware decode (v8.0). `from_utf8_lossy` mangled every
    // windows-1252 / ISO-8859-1 / GBK / Shift_JIS page into replacement chars.
    let capped_bytes = &bytes[..bytes.len().min(opts.max_bytes)];
    let body = crate::fetcher::decode_body(capped_bytes);

    if !is_html && is_plain_text {
        let trimmed = body.trim().to_string();
        if trimmed.is_empty() {
            return Err(ExtractError::UnusableContent { chars: 0 });
        }
        let title = plain_text_title(&trimmed, &final_url);
        return Ok((
            final_url,
            crate::extractor::Extracted {
                title,
                text: trimmed,
                confidence: 0.95,
                source: "plain-text",
                ..Default::default()
            },
        ));
    }

    // Bot walls served with HTTP 200 (Reddit's "Welcome to Reddit" CAPTCHA,
    // Cloudflare interstitials). v8.0 only screened SERPs, so these pages were
    // extracted and handed to the LLM as sources.
    if crate::engines::health::looks_like_blocked_page(&body) {
        tracing::debug!("Blocked/interstitial page at {}", target_url);
        return Err(ExtractError::AntiBotBlocked { status: 200 });
    }

    // ── Single-pass DOM extraction: title + text in one parse ──
    // v5.0 fix: eliminates the double Html::parse_document() bottleneck
    if youtube_mode || target_url.contains("youtube.com/watch") {
        // YouTube fast path: try JSON-LD first (no DOM parse needed)
        if let Some(desc) = extract_youtube_json_ld(&body) {
            return Ok((
                final_url,
                crate::extractor::Extracted {
                    title: extractor::extract_title_from_html(&body),
                    text: desc,
                    confidence: 0.6,
                    source: "youtube",
                    ..Default::default()
                },
            ));
        }
        // Fallback: single DOM parse for both meta desc + title
        let doc = Html::parse_document(&body);
        let title = extractor::extract_title_from_doc_pub(&doc);
        if let Some(desc) = extractor::extract_youtube_meta(&doc) {
            return Ok((
                final_url,
                crate::extractor::Extracted {
                    title,
                    text: desc,
                    confidence: 0.6,
                    source: "youtube",
                    ..Default::default()
                },
            ));
        }
    }

    // Normal pages: Readability + conditional Trafilatura + JSON-LD + Deep Reader.
    // Extraction is CPU-bound; run on Tokio's blocking pool so it cannot stall
    // network I/O. AMP fallback runs when canonical extraction is thin.
    let amp = if opts.allow_amp_fallback {
        crate::deep_reader::amp_url(&body, &final_url)
    } else {
        None
    };
    let page_url = final_url.clone();
    let extracted = tokio::task::spawn_blocking(move || extractor::extract(&body, &page_url))
        .await
        .map_err(|e| ExtractError::NetworkError(format!("extraction task failed: {e}")))?;

    if !extractor::is_usable(&extracted) {
        if let Some(amp_url) = amp {
            if let Some(mut e) = fetch_amp_fallback(client, &amp_url, opts, enforce_ssrf).await {
                tracing::debug!(
                    "AMP fallback read {} chars for {}",
                    e.text.len(),
                    target_url
                );
                e.source = "amp";
                return Ok((amp_url, e));
            }
        }
        let chars = crate::util::char_len(&extracted.text);
        return Err(ExtractError::UnusableContent { chars });
    }
    Ok((final_url, extracted))
}

/// Infer a readable title for a plain-text or Markdown document.
fn plain_text_title(text: &str, url: &str) -> String {
    if let Some(first) = text.lines().map(str::trim).find(|l| !l.is_empty()) {
        if let Some(heading) = first.strip_prefix("# ") {
            let h = heading.trim();
            if !h.is_empty() {
                return crate::util::truncate_chars(h, 160).to_string();
            }
        }
    }
    if let Ok(u) = url::Url::parse(url) {
        if let Some(seg) = u
            .path_segments()
            .and_then(|mut s| s.next_back())
            .filter(|s| !s.is_empty())
        {
            return seg.to_string();
        }
        if let Some(host) = u.host_str() {
            return host.to_string();
        }
    }
    String::new()
}

/// Plain fetch + extract for a secondary URL (the AMP variant). No retries.
async fn fetch_amp_fallback(
    client: &reqwest::Client,
    url: &str,
    opts: &FetchExtractOptions,
    enforce_ssrf: bool,
) -> Option<crate::extractor::Extracted> {
    if enforce_ssrf && !url::Url::parse(url).is_ok_and(|u| crate::ssrf::scrape_url_allowed(&u)) {
        return None;
    }
    let mut req = client
        .get(url)
        .header("Accept", "text/html,application/xhtml+xml;q=0.9,*/*;q=0.8");
    req = config::apply_browser_headers(req, url);
    if !opts.timeout.is_zero() {
        req = req.timeout(opts.timeout);
    }
    let resp = req.send().await.ok()?;
    if !resp.status().is_success() {
        return None;
    }
    let bytes = resp.bytes().await.ok()?;
    let html = crate::fetcher::decode_body(&bytes[..bytes.len().min(opts.max_bytes)]);
    if crate::engines::health::looks_like_blocked_page(&html) {
        return None;
    }
    let page_url = url.to_string();
    let e = tokio::task::spawn_blocking(move || extractor::extract(&html, &page_url))
        .await
        .ok()?;
    extractor::is_usable(&e).then_some(e)
}

/// Rewrite any reddit.com URL to old.reddit.com.
///
/// v8.0 did `.replace("www.reddit.com", "old.reddit.com").replace("reddit.com",
/// "old.reddit.com")` — the second replace also matched inside the first's
/// output, turning www.reddit.com into `old.old.reddit.com`. Reddit mode never
/// fetched a valid thread URL.
pub fn reddit_to_old(url: &str) -> String {
    let Ok(mut u) = url::Url::parse(url) else {
        return url.to_string();
    };
    let host = u.host_str().unwrap_or_default().to_lowercase();
    let is_reddit = host == "reddit.com" || host.ends_with(".reddit.com");
    if !is_reddit || host == "old.reddit.com" {
        return url.to_string();
    }
    // Media/CDN subdomains are not thread pages.
    if host.starts_with("i.") || host.starts_with("v.") || host.starts_with("preview.") {
        return url.to_string();
    }
    if u.set_host(Some("old.reddit.com")).is_err() {
        return url.to_string();
    }
    u.to_string()
}

/// Extract YouTube shortDescription from JSON-LD without any DOM parsing.
fn extract_youtube_json_ld(html: &str) -> Option<String> {
    // Try JSON-LD shortDescription (zero DOM parse — pure string scan)
    let marker = "\"shortDescription\":\"";
    let start = html.find(marker)? + marker.len();
    let rest = &html[start..];

    let mut end = 0;
    let mut escaped = false;
    for (i, c) in rest.char_indices() {
        if escaped {
            escaped = false;
            continue;
        }
        if c == '\\' {
            escaped = true;
            continue;
        }
        if c == '"' {
            end = i;
            break;
        }
    }

    if end == 0 {
        return None;
    }

    let raw = &rest[..end];
    let cleaned = raw
        .replace("\\n", " ")
        .replace("\\\"", "\"")
        .replace("\\u0026", "&")
        .replace("\\/", "/")
        .trim()
        .to_string();

    if cleaned.is_empty() {
        None
    } else {
        Some(cleaned)
    }
}

// =============================================================================
// HTTP Client Builders
// =============================================================================

/// Build a properly configured scrape client.
fn build_scrape_client() -> reqwest::Client {
    reqwest::Client::builder()
        .connect_timeout(Duration::from_secs(5))
        .timeout(Duration::from_secs(15))
        .redirect(crate::ssrf::scrape_redirect_policy(5))
        .dns_resolver(std::sync::Arc::new(crate::ssrf::PublicOnlyResolver))
        .pool_max_idle_per_host(12)
        .user_agent(config::random_user_agent())
        .tcp_nodelay(true)
        .cookie_store(true)
        .gzip(true)
        .brotli(true)
        .deflate(true)
        .zstd(true)
        .build()
        .expect("Failed to build scrape HTTP client")
}

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

    #[test]
    fn reddit_rewrite_never_doubles_the_prefix() {
        // v8.0 produced https://old.old.reddit.com/... for this input.
        assert_eq!(
            reddit_to_old("https://www.reddit.com/r/rust/comments/abc/title/"),
            "https://old.reddit.com/r/rust/comments/abc/title/"
        );
        assert_eq!(
            reddit_to_old("https://reddit.com/r/x"),
            "https://old.reddit.com/r/x"
        );
        assert_eq!(
            reddit_to_old("https://m.reddit.com/r/x"),
            "https://old.reddit.com/r/x"
        );
        assert_eq!(
            reddit_to_old("https://old.reddit.com/r/x"),
            "https://old.reddit.com/r/x"
        );
    }

    #[test]
    fn reddit_rewrite_leaves_other_hosts_and_media_alone() {
        assert_eq!(
            reddit_to_old("https://notreddit.com/r/x"),
            "https://notreddit.com/r/x"
        );
        assert_eq!(
            reddit_to_old("https://i.reddit.com/x.png"),
            "https://i.reddit.com/x.png"
        );
        assert_eq!(
            reddit_to_old("https://example.com/reddit.com/x"),
            "https://example.com/reddit.com/x"
        );
        assert_eq!(reddit_to_old("not a url"), "not a url");
    }
}

#[cfg(test)]
mod title_tests {
    use super::{pick_title, wikipedia_title};

    #[test]
    fn keeps_engine_title_when_extracted_title_is_unrelated() {
        assert_eq!(
            pick_title(
                "https://x.test/a",
                "Bain-marie",
                "type of heated bath".into()
            ),
            "Bain-marie"
        );
        assert_eq!(
            pick_title("https://x.test/p", "Paris - Travel guide", "Paris".into()),
            "Paris",
            "a related extracted title (site suffix stripped) is preferred"
        );
        assert_eq!(
            pick_title("https://x.test/e", "", "Extracted".into()),
            "Extracted"
        );
        assert_eq!(
            pick_title("https://x.test/e", "Engine", "".into()),
            "Engine"
        );
        // Shared stopwords are not agreement.
        assert_eq!(
            pick_title(
                "https://x.test/s",
                "The Legend of Tennessee Moltisanti",
                "episode of The Sopranos (S1 E8)".into()
            ),
            "The Legend of Tennessee Moltisanti"
        );
    }

    #[test]
    fn wikipedia_titles_come_from_the_url() {
        assert_eq!(
            pick_title(
                "https://en.wikipedia.org/wiki/Invention_of_the_telephone",
                "",
                "aspect of history relating to telephones".into()
            ),
            "Invention of the telephone"
        );
        assert_eq!(
            wikipedia_title("https://hi.wikipedia.org/wiki/%E0%A4%AD%E0%A4%BE%E0%A4%B0%E0%A4%A4")
                .as_deref(),
            Some("भारत")
        );
        assert_eq!(
            wikipedia_title("https://en.wikipedia.org/wiki/Special:Search"),
            None
        );
        assert_eq!(wikipedia_title("https://example.org/wiki/X"), None);
    }
}

/// Combines the user's raw query with any resolved or typo-corrected entities
/// from Semantic Query Understanding so BM25 scores both raw terms and resolved
/// canonical entities (including cross-lingual technical entities).
fn build_ranking_query(
    raw_query: &str,
    effective_query: &str,
    intel: &query_intel::QueryIntelligence,
) -> String {
    let mut combined = raw_query.trim().to_string();
    let lower_combined = combined.to_lowercase();

    // Append resolved entity tokens that aren't already verbatim in raw_query
    for ent in &intel.key_entities {
        for tok in ent.split_whitespace() {
            let clean = tok.trim_matches(|c: char| !crate::util::is_word_char(c));
            if clean.chars().count() >= 3 && !lower_combined.contains(&clean.to_lowercase()) {
                combined.push(' ');
                combined.push_str(clean);
            }
        }
    }

    // If follow-up or typo resolution produced additional content words in effective_query, include them
    if !effective_query.eq_ignore_ascii_case(raw_query) {
        let current_lower = combined.to_lowercase();
        for tok in effective_query.split_whitespace() {
            let clean = tok.trim_matches(|c: char| !crate::util::is_word_char(c));
            if clean.chars().count() >= 3
                && !clean.chars().all(|c| c.is_ascii_digit())
                && !current_lower.contains(&clean.to_lowercase())
            {
                combined.push(' ');
                combined.push_str(clean);
            }
        }
    }

    combined
}

/// Evaluates whether the top-5 ranked chunks have sufficient entity coverage
/// and (when `intel.is_time_sensitive` is true) current-year evidence.
pub(crate) fn evidence_is_sufficient(
    results: &[SourceResult],
    intel: &query_intel::QueryIntelligence,
) -> bool {
    if results.is_empty() {
        return false;
    }

    let top_chunks: Vec<&SourceResult> = results.iter().take(5).collect();

    // 1. Check entity coverage across the top-5 chunks
    if !intel.key_entities.is_empty() {
        let mut matched_entities = 0usize;
        for ent in &intel.key_entities {
            let tokens: Vec<String> = ent
                .split_whitespace()
                .map(|t| {
                    t.trim_matches(|c: char| !crate::util::is_word_char(c))
                        .to_lowercase()
                })
                .filter(|t| t.chars().count() >= 3)
                .collect();
            if tokens.is_empty() {
                matched_entities += 1;
                continue;
            }
            let found = top_chunks.iter().any(|chunk| {
                let hay = format!(
                    "{} {} {} {}",
                    chunk.title.to_lowercase(),
                    chunk.url.to_lowercase(),
                    chunk.site_name.as_deref().unwrap_or("").to_lowercase(),
                    chunk.extracted_text.to_lowercase()
                );
                tokens.iter().any(|tok| hay.contains(tok.as_str()))
            });
            if found {
                matched_entities += 1;
            }
        }

        if matched_entities * 2 < intel.key_entities.len() {
            return false;
        }
    }

    // 2. For time-sensitive queries, verify at least one top-5 chunk carries
    // current-year evidence (resolved dynamically via chrono::Utc::now().year()).
    if intel.is_time_sensitive {
        let current_year = chrono::Utc::now().year();
        let current_year_str = current_year.to_string();
        let has_current_year_evidence = top_chunks.iter().any(|chunk| {
            let date_is_current = chunk
                .published
                .map(|d| d.year() >= current_year)
                .unwrap_or(false);
            date_is_current
                || chunk.extracted_text.contains(&current_year_str)
                || chunk.title.contains(&current_year_str)
        });
        if !has_current_year_evidence {
            return false;
        }
    }

    true
}

/// Builds the second-pass corrective query from the semantic plan (or extracted
/// entities + dynamic current year) if it hasn't already been dispatched.
fn build_corrective_query(
    intel: &query_intel::QueryIntelligence,
    already_searched: &[String],
) -> Option<String> {
    let candidate = if let Some(ref cq) = intel.corrective_query {
        cq.trim().to_string()
    } else if !intel.key_entities.is_empty() {
        let base = intel.key_entities.join(" ");
        if intel.is_time_sensitive {
            format!("{} {}", base, chrono::Utc::now().year())
        } else {
            base
        }
    } else {
        return None;
    };

    if candidate.chars().count() < 3 {
        return None;
    }
    let already_ran = already_searched
        .iter()
        .any(|q| q.eq_ignore_ascii_case(&candidate));
    (!already_ran).then_some(candidate)
}

/// Adaptive Pre-Scrape Title, Subdomain & Snippet Triage (zero hardcoded domain or year lists).
/// Evaluates a SERP candidate's title, URL/subdomain structure, and snippet against
/// target entities, query terms, and dynamic temporal context BEFORE scraping full HTML.
fn pre_scrape_triage_score(
    c: &Candidate,
    raw_query: &str,
    query_terms: &[String],
    intel: &query_intel::QueryIntelligence,
    mode: modes::Mode,
    domain: modes::Domain,
) -> f64 {
    let mut s = c.rrf * modes::source_boost(&c.url, mode, domain);
    let (host, path) = modes::parse_host_and_path(&c.url);

    // 1. Subdomain & Host Entity Alignment
    let (matched_ent, _) =
        crate::llm::url_matches_query_entities(&c.url, None, &intel.key_entities);
    if matched_ent > 0 {
        s *= 1.50 + (0.15 * (matched_ent.min(2) as f64));
        if modes::is_structural_docs_url(&host, &path)
            || path.contains("/changelog")
            || path.contains("/releases")
            || path.contains("/release-notes")
            || path.contains("/announcements")
            || path.starts_with("/blog")
            || path.starts_with("/news")
        {
            s *= 1.20;
        }
    }

    // 2. Title & Snippet Entity + Specific Sub-Topic Qualifier Overlap
    let title_lower = c.title.to_lowercase();
    let snippet_lower = c.snippet.to_lowercase();
    let url_lower = c.url.to_lowercase();

    let mut entity_token_set: std::collections::HashSet<String> = std::collections::HashSet::new();
    let mut entity_hits_in_meta = 0usize;
    for ent in &intel.key_entities {
        let mut hit = false;
        for t in ent
            .split_whitespace()
            .map(|t| {
                t.trim_matches(|ch: char| !crate::util::is_word_char(ch))
                    .to_lowercase()
            })
            .filter(|t| t.chars().count() >= 3)
        {
            if title_lower.contains(&t) || snippet_lower.contains(&t) || url_lower.contains(&t) {
                hit = true;
            }
            entity_token_set.insert(t);
        }
        if hit {
            entity_hits_in_meta += 1;
        }
    }

    let mut term_hits_title = 0usize;
    let mut term_hits_snippet = 0usize;
    let mut subtopic_term_count = 0usize;
    let mut subtopic_hits = 0usize;

    for term in query_terms {
        if term.chars().count() < 3 {
            continue;
        }
        let in_title_or_url =
            title_lower.contains(term.as_str()) || url_lower.contains(term.as_str());
        let in_snippet = snippet_lower.contains(term.as_str());
        if in_title_or_url {
            term_hits_title += 1;
        } else if in_snippet {
            term_hits_snippet += 1;
        }
        if !entity_token_set.contains(term.as_str()) {
            subtopic_term_count += 1;
            if in_title_or_url || in_snippet {
                subtopic_hits += 1;
            }
        }
    }

    if entity_hits_in_meta > 0 && term_hits_title > 0 {
        s *= 1.25;
    } else if !query_terms.is_empty()
        && matched_ent == 0
        && entity_hits_in_meta == 0
        && term_hits_title == 0
        && term_hits_snippet == 0
    {
        // Off-topic SERP noise with zero entity or query term overlap in title/URL/snippet
        s *= 0.30;
    }

    // Sub-topic specificity boost: when a query asks about specific aspects/clauses
    // of an entity (e.g., "administration" and "land grants" in "Aihole inscription"),
    // strongly boost candidates whose title/snippet explicitly covers those sub-topics,
    // and demote generic encyclopedia summaries that only name the entity without sub-topics.
    let is_encyc = crate::llm::is_encyclopedia_url(&c.url, None);
    if subtopic_term_count > 0 && (entity_hits_in_meta > 0 || matched_ent > 0) {
        if subtopic_hits > 0 {
            s *= 1.0 + 0.25 * (subtopic_hits.min(3) as f64);
        } else if is_encyc && mode != modes::Mode::Study {
            s *= 0.68;
        }
    }

    // Boost scholarly, institutional, or primary documentation sources on research/historical queries
    if (entity_hits_in_meta > 0 || term_hits_title > 0)
        && (modes::is_structural_scholarly_url(&host, &path) || modes::is_institutional_host(&host))
    {
        s *= 1.28;
    }

    // 3. Dynamic Temporal Alignment in Title, URL & Snippet (resolved via chrono::Utc::now().year())
    if intel.is_time_sensitive && !matches!(mode, modes::Mode::Study | modes::Mode::Academic) {
        let current_year = chrono::Utc::now().year();
        let cur_year_str = current_year.to_string();
        let meta_text = format!("{} {} {}", title_lower, snippet_lower, path);

        let mentions_current_year = meta_text.contains(&cur_year_str)
            || c.published
                .map(|d| d.year() >= current_year)
                .unwrap_or(false);

        if mentions_current_year {
            s *= 1.25;
        } else {
            // Penalize candidates whose title/URL/date explicitly anchors to an unrequested past year
            let raw_lower = raw_query.to_lowercase();
            let has_stale_year = ((current_year - 10)..current_year).any(|y| {
                let ys = y.to_string();
                !raw_lower.contains(&ys)
                    && (title_lower.contains(&ys)
                        || path.contains(&ys)
                        || c.published.map(|d| d.year() == y).unwrap_or(false))
            });
            if has_stale_year {
                s *= 0.55;
            }
        }
    }

    // 4. Encyclopedia Pre-Scrape Demotion on Live / Product Queries
    if crate::llm::is_encyclopedia_url(&c.url, None)
        && mode != modes::Mode::Study
        && (intel.is_time_sensitive
            || matches!(
                intel.semantic_category,
                query_intel::SemanticCategory::LiveNews | query_intel::SemanticCategory::ProductOrg
            ))
    {
        s *= 0.65;
    }

    if c.full_text.is_some() && matches!(mode, modes::Mode::Academic | modes::Mode::Study) {
        s *= 1.5;
    }

    s
}

#[cfg(test)]
mod stage1_sufficiency_tests {
    use super::*;
    use chrono::{Datelike, TimeZone, Utc};

    #[test]
    fn evidence_sufficiency_checks_entity_and_dynamic_current_year_coverage() {
        let current_year = chrono::Utc::now().year();
        let past_year = current_year - 2;

        let mut intel = crate::query_intel::analyze_query("latest Google Antigravity release");
        intel.is_time_sensitive = true;
        intel.key_entities = vec!["Google Antigravity".to_string()];

        let stale_unrelated = vec![SourceResult {
            url: "https://en.wikipedia.org/wiki/Google_Play".to_string(),
            title: "Google Play".to_string(),
            extracted_text: format!(
                "Google Play is a digital distribution service updated in {past_year}."
            ),
            char_count: 72,
            engine: "wikipedia".to_string(),
            published: Utc.with_ymd_and_hms(past_year, 5, 10, 0, 0, 0).single(),
            language: Some("en".to_string()),
            byline: None,
            site_name: Some("Wikipedia".to_string()),
            confidence: 0.9,
            extractor: "dom_smoothie",
        }];
        assert!(
            !evidence_is_sufficient(&stale_unrelated, &intel),
            "stale source should fail sufficiency on time-sensitive query"
        );

        let fresh_matching = vec![SourceResult {
            url: "https://antigravity.google/changelog".to_string(),
            title: "Google Antigravity Changelog".to_string(),
            extracted_text: format!(
                "Google Antigravity release notes and platform updates ({current_year})."
            ),
            char_count: 75,
            engine: "brave".to_string(),
            published: Utc.with_ymd_and_hms(current_year, 3, 15, 0, 0, 0).single(),
            language: Some("en".to_string()),
            byline: None,
            site_name: Some("Google Antigravity".to_string()),
            confidence: 0.95,
            extractor: "dom_smoothie",
        }];
        assert!(
            evidence_is_sufficient(&fresh_matching, &intel),
            "fresh entity-matching source should pass sufficiency"
        );
    }

    #[test]
    fn pre_scrape_triage_prioritizes_official_current_candidates_over_stale_or_offtopic() {
        let current_year = chrono::Utc::now().year();
        let stale_year = current_year - 2;

        let raw_q = "Google Antigravity latest release features";
        let mut intel = crate::query_intel::analyze_query(raw_q);
        intel.is_time_sensitive = true;
        intel.semantic_category = crate::query_intel::SemanticCategory::ProductOrg;
        intel.key_entities = vec!["Google Antigravity".to_string()];
        let terms = crate::ranking::query_terms(raw_q);

        let official_cand = Candidate {
            url: "https://antigravity.google/changelog".to_string(),
            title: format!("Google Antigravity Changelog ({current_year})"),
            snippet: "Latest release features, agentic IDE tools, and workspace updates."
                .to_string(),
            engines: vec!["brave".to_string()],
            families: vec!["brave"],
            rrf: 0.02,
            published: None,
            full_text: None,
        };
        let stale_blog_cand = Candidate {
            url: format!("https://some-random-techblog.test/review-{stale_year}"),
            title: format!("Early review from {stale_year}"),
            snippet: "An old post about Google Antigravity.".to_string(),
            engines: vec!["brave".to_string()],
            families: vec!["brave"],
            rrf: 0.02,
            published: None,
            full_text: None,
        };
        let offtopic_wiki_cand = Candidate {
            url: "https://en.wikipedia.org/wiki/Google_Doodle".to_string(),
            title: "Google Doodle - Wikipedia".to_string(),
            snippet: "A special temporary alteration of the logo on homepages.".to_string(),
            engines: vec!["wikipedia".to_string()],
            families: vec!["wikipedia"],
            rrf: 0.02,
            published: None,
            full_text: None,
        };

        let s_official = pre_scrape_triage_score(
            &official_cand,
            raw_q,
            &terms,
            &intel,
            modes::Mode::Auto,
            modes::Domain::General,
        );
        let s_stale = pre_scrape_triage_score(
            &stale_blog_cand,
            raw_q,
            &terms,
            &intel,
            modes::Mode::Auto,
            modes::Domain::General,
        );
        let s_wiki = pre_scrape_triage_score(
            &offtopic_wiki_cand,
            raw_q,
            &terms,
            &intel,
            modes::Mode::Auto,
            modes::Domain::General,
        );

        assert!(
            s_official > s_stale * 3.0,
            "official ({s_official}) must beat stale ({s_stale})"
        );
        assert!(
            s_official > s_wiki * 3.0,
            "official ({s_official}) must beat off-topic wiki ({s_wiki})"
        );
    }
}

#[cfg(test)]
mod mcp_foundation_tests {
    use super::*;
    use axum::{
        http::{header, StatusCode},
        response::IntoResponse,
        routing::get,
        Router,
    };

    fn long_article_html(title: &str, tag: &str) -> String {
        let body: String = (0..6)
            .map(|i| {
                format!(
                    "<p>{tag} paragraph {i}: SearchWala extracts structured prose cleanly from \
                     real-world HTML documents without relying on third-party SDKs or cloud APIs.</p>"
                )
            })
            .collect();
        format!(
            "<!DOCTYPE html><html lang=\"en\"><head><title>{title}</title></head>\
             <body><article><h1>{title}</h1>{body}</article></body></html>"
        )
    }

    async fn spawn_mock_site() -> String {
        let app = Router::new()
            .route(
                "/article",
                get(|| async {
                    (
                        [(header::CONTENT_TYPE, "text/html; charset=utf-8")],
                        long_article_html("Rust MCP Architecture", "ARTICLE"),
                    )
                }),
            )
            .route(
                "/llms.txt",
                get(|| async {
                    (
                        [(header::CONTENT_TYPE, "text/markdown; charset=utf-8")],
                        "# SearchWala Docs\n\nSearchWala exposes consensus search and deep extraction over MCP.",
                    )
                }),
            )
            .route(
                "/file.pdf",
                get(|| async {
                    (
                        [(header::CONTENT_TYPE, "application/pdf")],
                        b"%PDF-1.7 binary payload".as_slice(),
                    )
                }),
            )
            .route(
                "/blocked-403",
                get(|| async { (StatusCode::FORBIDDEN, "Forbidden") }),
            )
            .route(
                "/captcha-200",
                get(|| async {
                    (
                        [(header::CONTENT_TYPE, "text/html")],
                        "<html><head><title>Just a moment...</title></head><body>Checking your browser before accessing</body></html>",
                    )
                }),
            )
            .route(
                "/missing",
                get(|| async { (StatusCode::NOT_FOUND, "Not found") }),
            )
            .route(
                "/thin-with-amp",
                get(|| async {
                    (
                        [(header::CONTENT_TYPE, "text/html")],
                        "<html><head><title>Thin</title><link rel=\"amphtml\" href=\"/amp/story\"></head><body><p>Short teaser.</p></body></html>",
                    )
                }),
            )
            .route(
                "/amp/story",
                get(|| async {
                    (
                        [(header::CONTENT_TYPE, "text/html")],
                        long_article_html("AMP Full Story", "AMPBODY"),
                    )
                }),
            )
            .route(
                "/thin-only",
                get(|| async {
                    (
                        [(header::CONTENT_TYPE, "text/html")],
                        "<html><head><title>Tiny</title></head><body><p>Hi</p></body></html>",
                    )
                }),
            )
            .route(
                "/redirect-imds",
                get(|| async {
                    (
                        StatusCode::FOUND,
                        [(header::LOCATION, "http://169.254.169.254/latest/meta-data/")],
                    )
                        .into_response()
                }),
            );

        let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind local mock listener");
        let addr = listener.local_addr().expect("local addr");
        tokio::spawn(async move {
            let _ = axum::serve(listener, app).await;
        });
        format!("http://{}", addr)
    }

    fn local_test_client() -> reqwest::Client {
        reqwest::Client::builder()
            .timeout(Duration::from_secs(5))
            .redirect(crate::ssrf::scrape_redirect_policy(5))
            .build()
            .expect("test reqwest client")
    }

    #[tokio::test]
    async fn search_serp_only_handles_empty_query_offline() {
        let resp = search_serp_only(
            "   ",
            Some("fast"),
            Some("tech"),
            Some(10),
            &DomainFilter::default(),
            None,
        )
        .await;
        assert!(resp.query.is_empty());
        assert_eq!(resp.mode, "fast");
        assert_eq!(resp.domain, "tech");
        assert!(resp.results.is_empty());
        assert!(resp.discovered_domains.is_empty());
        assert_eq!(resp.total_discovered_domains, 0);
        assert_eq!(resp.engine_stats.total_raw_results, 0);
    }

    #[test]
    fn fuse_and_select_candidates_dedups_and_preserves_abstracts() {
        let raw = vec![
            RawSearchResult {
                url: "https://arxiv.org/abs/2601.12345".into(),
                title: "Consensus Meta-Search".into(),
                snippet: "Short snippet".into(),
                engine: "arxiv".into(),
                rank_position: 1,
                published: None,
                full_text: Some(
                    "This paper evaluates cross-family Reciprocal Rank Fusion across independent \
                     web and scholarly indexes with measurable nDCG improvements."
                        .into(),
                ),
            },
            RawSearchResult {
                url: "https://www.arxiv.org/abs/2601.12345?utm_source=test".into(),
                title: "Consensus Meta-Search".into(),
                snippet: "Longer consensus snippet from Brave".into(),
                engine: "brave".into(),
                rank_position: 2,
                published: None,
                full_text: None,
            },
            RawSearchResult {
                url: "https://facebook.com/skipped-post".into(),
                title: "Should be skipped".into(),
                snippet: "Skipped domain".into(),
                engine: "bing".into(),
                rank_position: 1,
                published: None,
                full_text: None,
            },
        ];

        let query = "consensus meta-search";
        let intel = query_intel::analyze_query(query);
        let profile = modes::resolve(modes::Mode::Academic, modes::Domain::Science, &intel, None);
        let sel = fuse_and_select_candidates(
            &raw,
            None,
            query,
            query,
            &intel,
            &profile,
            10,
            &DomainFilter::default(),
        );
        let ranked = sel.candidates;
        let discovered = sel.discovered_domains;
        let total_domains = sel.total_discovered_domains;
        assert_eq!(
            ranked.len(),
            1,
            "www/utm variant must collapse and facebook skipped"
        );
        assert_eq!(sel.deduplicated_urls, 1);
        assert_eq!(ranked[0].families.len(), 2, "arxiv + brave are 2 families");
        assert_eq!(total_domains, 1);
        assert_eq!(discovered.len(), 1);
        assert_eq!(discovered[0].domain, "arxiv.org");
        assert_eq!(discovered[0].max_families, 2);

        let hits = candidates_to_serp_hits(&ranked, 5);
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].rank, 1);
        assert_eq!(hits[0].found_by_families, 2);
        assert_eq!(hits[0].snippet, "Longer consensus snippet from Brave");
        assert!(
            hits[0]
                .abstract_text
                .as_deref()
                .is_some_and(|a| a.contains("Reciprocal Rank Fusion")),
            "engine-supplied abstract must be preserved"
        );
    }

    #[test]
    fn domain_filter_supports_soft_priority_strict_restriction_exclusion_and_discovered_rollup() {
        let raw = vec![
            RawSearchResult {
                url: "https://en.wikipedia.org/wiki/Rust_(programming_language)".into(),
                title: "Rust programming language".into(),
                snippet: "Rust async and memory safety overview.".into(),
                engine: "wikipedia".into(),
                rank_position: 1,
                published: None,
                full_text: None,
            },
            RawSearchResult {
                url: "https://en.wikipedia.org/wiki/Rust_(programming_language)".into(),
                title: "Rust programming language".into(),
                snippet: "Rust async overview from Bing.".into(),
                engine: "bing".into(),
                rank_position: 1,
                published: None,
                full_text: None,
            },
            RawSearchResult {
                url: "https://docs.rs/tokio/latest/tokio/macro.select.html".into(),
                title: "tokio::select cancellation safety".into(),
                snippet: "Detailed docs on tokio select cancellation safety.".into(),
                engine: "brave".into(),
                rank_position: 4,
                published: None,
                full_text: None,
            },
            RawSearchResult {
                url: "https://docs.rs/tokio/latest/tokio/task/index.html".into(),
                title: "tokio::task".into(),
                snippet: "Tokio async tasks documentation.".into(),
                engine: "bing".into(),
                rank_position: 6,
                published: None,
                full_text: None,
            },
            RawSearchResult {
                url: "https://noisy-blog.example/rust-select".into(),
                title: "Noisy blog post".into(),
                snippet: "Low quality scraped tutorial.".into(),
                engine: "bing".into(),
                rank_position: 2,
                published: None,
                full_text: None,
            },
            RawSearchResult {
                url: "https://www.amazon.com/dp/B0RUSTBOOK".into(),
                title: "Programming Rust Book".into(),
                snippet: "Rust async programming book.".into(),
                engine: "bing".into(),
                rank_position: 5,
                published: None,
                full_text: None,
            },
        ];

        let query = "rust async tokio select cancellation safety";
        let intel = query_intel::analyze_query(query);
        let profile = modes::resolve(modes::Mode::Auto, modes::Domain::Tech, &intel, None);

        // 1. Soft mode (strict=false, default): docs.rs + amazon.com prioritized & bypass SKIP_DOMAINS;
        // noisy-blog.example excluded; organic wikipedia result preserved.
        let soft_filter = DomainFilter {
            include: vec!["docs.rs".into(), "amazon.com".into()],
            exclude: vec!["noisy-blog.example".into()],
            strict: false,
        };
        let soft_sel = fuse_and_select_candidates(
            &raw,
            None,
            query,
            query,
            &intel,
            &profile,
            10,
            &soft_filter,
        );
        let soft_ranked = soft_sel.candidates;
        let soft_discovered = soft_sel.discovered_domains;
        let soft_total = soft_sel.total_discovered_domains;
        assert!(
            soft_ranked
                .iter()
                .all(|c| !c.url.contains("noisy-blog.example")),
            "excluded domain must never survive triage"
        );
        assert!(
            soft_discovered
                .iter()
                .all(|d| d.domain != "noisy-blog.example"),
            "excluded domain must not appear in discovered_domains"
        );
        assert!(
            soft_ranked[0].url.contains("docs.rs"),
            "included domain must be prioritized at top: {:?}",
            soft_ranked.iter().map(|c| &c.url).collect::<Vec<_>>()
        );
        assert!(
            soft_ranked.iter().any(|c| c.url.contains("wikipedia.org")),
            "soft mode (strict=false) must keep SearchWala's organic consensus results intact"
        );
        assert!(
            soft_ranked.iter().any(|c| c.url.contains("amazon.com")),
            "explicit include_domains must bypass SKIP_DOMAINS"
        );
        assert_eq!(soft_total, 3, "docs.rs + amazon.com + en.wikipedia.org");
        let docs_entry = soft_discovered
            .iter()
            .find(|d| d.domain == "docs.rs")
            .unwrap();
        assert_eq!(docs_entry.result_count, 2);

        // 2. Strict mode (strict=true): ONLY docs.rs returned in BOTH candidates
        // AND discovered_domains (external/spam domains must never leak).
        let strict_filter = DomainFilter {
            include: vec!["docs.rs".into()],
            exclude: vec!["noisy-blog.example".into()],
            strict: true,
        };
        let strict_sel = fuse_and_select_candidates(
            &raw,
            None,
            query,
            query,
            &intel,
            &profile,
            10,
            &strict_filter,
        );
        let strict_ranked = strict_sel.candidates;
        let strict_discovered = strict_sel.discovered_domains;
        assert_eq!(strict_ranked.len(), 2);
        assert_eq!(strict_sel.deduplicated_urls, 2);
        assert!(strict_ranked.iter().all(|c| c.url.contains("docs.rs")));
        assert!(
            strict_discovered.iter().all(|d| d.domain == "docs.rs"),
            "strict mode must not leak non-included domains into discovered_domains: {strict_discovered:?}"
        );

        // 3. Balanced soft priority: when many included hits compete with organic
        // consensus results under strict=false, included domains take top priority
        // slots (round-robin across included domains, capped at half the budget)
        // while keeping SearchWala's organic consensus answers intact.
        let mut crowded_raw = Vec::new();
        for i in 1..=6 {
            crowded_raw.push(RawSearchResult {
                url: format!("https://docs.rs/tokio/item-{i}"),
                title: format!("Tokio docs item {i}"),
                snippet: "Rust tokio select cancellation safety".into(),
                engine: "brave".into(),
                rank_position: i,
                published: None,
                full_text: None,
            });
            crowded_raw.push(RawSearchResult {
                url: format!("https://tokio.rs/tutorial/item-{i}"),
                title: format!("Tokio tutorial item {i}"),
                snippet: "Rust tokio select cancellation safety".into(),
                engine: "bing".into(),
                rank_position: i,
                published: None,
                full_text: None,
            });
            crowded_raw.push(RawSearchResult {
                url: format!("https://organic{i}.example/rust-select"),
                title: format!("Organic rust guide {i}"),
                snippet: "Rust tokio select cancellation safety".into(),
                engine: "duckduckgo".into(),
                rank_position: i,
                published: None,
                full_text: None,
            });
        }
        let balanced_filter = DomainFilter {
            include: vec!["docs.rs".into(), "tokio.rs".into()],
            exclude: vec![],
            strict: false,
        };
        let balanced_sel = fuse_and_select_candidates(
            &crowded_raw,
            None,
            query,
            query,
            &intel,
            &profile,
            10,
            &balanced_filter,
        );
        let balanced = balanced_sel.candidates;
        assert_eq!(
            balanced_sel.deduplicated_urls, 18,
            "deduplicated_urls must reflect pre-truncation pool (18), not truncated cap (10)"
        );
        assert_eq!(balanced.len(), 10);
        let inc_cnt = balanced
            .iter()
            .filter(|c| balanced_filter.is_included(&c.url))
            .count();
        let org_cnt = balanced.len() - inc_cnt;
        assert_eq!(
            inc_cnt, 5,
            "priority slots capped at half of max_urls (5/10)"
        );
        assert_eq!(org_cnt, 5, "organic consensus answers keep 5/10 slots");
        assert!(balanced[..5]
            .iter()
            .all(|c| balanced_filter.is_included(&c.url)));
        assert!(balanced[..5].iter().any(|c| c.url.contains("docs.rs")));
        assert!(balanced[..5].iter().any(|c| c.url.contains("tokio.rs")));

        // 4. Targeted site: query generation caps cleanly and avoids double site:.
        let tq = build_targeted_domain_queries(query, &soft_filter, false);
        assert_eq!(
            tq,
            vec![
                format!("{query} site:docs.rs"),
                format!("{query} site:amazon.com")
            ]
        );
        assert!(build_targeted_domain_queries(query, &soft_filter, true).is_empty());
        assert!(build_targeted_domain_queries("foo site:bar.com", &soft_filter, false).is_empty());
    }

    #[test]
    fn apply_recency_hint_skips_evergreen_modes_and_existing_years() {
        let mut intel = query_intel::analyze_query("latest rust release");
        intel.is_time_sensitive = true;
        let cur_year = chrono::Utc::now().year().to_string();

        let auto_q = apply_recency_hint("latest rust release", &intel, modes::Mode::Auto);
        assert!(auto_q.contains(&cur_year));

        let study_q = apply_recency_hint("latest rust release", &intel, modes::Mode::Study);
        assert_eq!(study_q, "latest rust release");

        let explicit = format!("rust release {cur_year}");
        assert_eq!(
            apply_recency_hint(&explicit, &intel, modes::Mode::Auto),
            explicit
        );
    }

    #[tokio::test]
    async fn fetch_and_extract_url_blocks_invalid_and_ssrf_targets() {
        let opts = FetchExtractOptions::default();

        assert!(matches!(
            fetch_and_extract_url("", &opts).await,
            Err(ExtractError::InvalidUrl(_))
        ));
        assert!(matches!(
            fetch_and_extract_url("not a url", &opts).await,
            Err(ExtractError::InvalidUrl(_))
        ));

        for blocked in [
            "http://127.0.0.1:8000/api/chats",
            "http://localhost:8000/",
            "http://169.254.169.254/latest/meta-data/",
            "http://[::1]:8000/",
            "http://10.0.0.1/",
            "file:///etc/passwd",
            "ftp://example.com/file",
        ] {
            let res = fetch_and_extract_url(blocked, &opts).await;
            assert!(
                matches!(res, Err(ExtractError::BlockedBySsrf(_))),
                "expected BlockedBySsrf for {blocked}, got {res:?}"
            );
        }
    }

    #[tokio::test]
    async fn fetch_and_extract_url_serves_from_page_cache_without_network() {
        let url = "https://cached-offline-check.example/docs/guide";
        crate::cache_layer::put_page(
            url,
            crate::extractor::Extracted {
                title: "Cached Guide".into(),
                text: "Cached article body served purely from Moka without network I/O.".into(),
                confidence: 0.92,
                source: "readability",
                ..Default::default()
            },
        )
        .await;

        let page = fetch_and_extract_url(url, &FetchExtractOptions::default())
            .await
            .expect("should hit cache");
        assert!(page.cached);
        assert_eq!(page.extracted.title, "Cached Guide");
        assert!(page.extracted.text.contains("purely from Moka"));
    }

    #[tokio::test]
    async fn fetch_and_extract_local_mock_server_end_to_end() {
        let base = spawn_mock_site().await;

        // 1. Production entrypoint MUST block the local 127.0.0.1 mock server via SSRF guard.
        let prod_res =
            fetch_and_extract_url(&format!("{base}/article"), &FetchExtractOptions::default())
                .await;
        assert!(
            matches!(prod_res, Err(ExtractError::BlockedBySsrf(_))),
            "production fetch_and_extract_url must reject loopback: {prod_res:?}"
        );

        let client = local_test_client();
        let opts = FetchExtractOptions {
            use_cache: true,
            ..Default::default()
        };

        // 2. HTML article extraction + cache miss then cache hit.
        let art_url = format!("{base}/article");
        let page = fetch_and_extract_with_client(&client, &art_url, &opts, false, false)
            .await
            .expect("article extraction");
        assert!(!page.cached);
        assert_eq!(page.extracted.title, "Rust MCP Architecture");
        assert!(page.extracted.text.contains("ARTICLE paragraph 5"));
        assert!(page.char_count > 300);

        let cached = fetch_and_extract_with_client(&client, &art_url, &opts, false, false)
            .await
            .expect("cached article");
        assert!(cached.cached);

        let bypassed = fetch_and_extract_with_client(
            &client,
            &art_url,
            &FetchExtractOptions {
                use_cache: false,
                ..opts.clone()
            },
            false,
            false,
        )
        .await
        .expect("bypassed cache");
        assert!(!bypassed.cached);

        // 3. Markdown/plain-text extraction with H1 title inference.
        let md = fetch_and_extract_with_client(
            &client,
            &format!("{base}/llms.txt"),
            &opts,
            false,
            false,
        )
        .await
        .expect("markdown extraction");
        assert_eq!(md.extracted.source, "plain-text");
        assert_eq!(md.extracted.title, "SearchWala Docs");
        assert!(md.extracted.text.contains("consensus search"));

        // 4. Binary content-type rejected cleanly.
        let pdf = fetch_and_extract_with_client(
            &client,
            &format!("{base}/file.pdf"),
            &opts,
            false,
            false,
        )
        .await;
        assert!(
            matches!(pdf, Err(ExtractError::UnsupportedContentType(ref ct)) if ct.contains("application/pdf")),
            "expected UnsupportedContentType, got {pdf:?}"
        );

        // 5. Anti-bot 403 and HTTP 200 Cloudflare interstitial.
        let f403 = fetch_and_extract_with_client(
            &client,
            &format!("{base}/blocked-403"),
            &opts,
            false,
            false,
        )
        .await;
        assert_eq!(
            f403.unwrap_err(),
            ExtractError::AntiBotBlocked { status: 403 }
        );

        let cap = fetch_and_extract_with_client(
            &client,
            &format!("{base}/captcha-200"),
            &opts,
            false,
            false,
        )
        .await;
        assert_eq!(
            cap.unwrap_err(),
            ExtractError::AntiBotBlocked { status: 200 }
        );

        // 6. HTTP 404 status error.
        let miss =
            fetch_and_extract_with_client(&client, &format!("{base}/missing"), &opts, false, false)
                .await;
        assert_eq!(miss.unwrap_err(), ExtractError::HttpStatus { status: 404 });

        // 7. Thin canonical HTML + AMP fallback recovers full story.
        let amp = fetch_and_extract_with_client(
            &client,
            &format!("{base}/thin-with-amp"),
            &opts,
            false,
            false,
        )
        .await
        .expect("AMP fallback should recover full body");
        assert_eq!(amp.extracted.source, "amp");
        assert!(amp.extracted.text.contains("AMPBODY paragraph 5"));

        // 8. Thin page with no AMP returns UnusableContent.
        let thin = fetch_and_extract_with_client(
            &client,
            &format!("{base}/thin-only"),
            &opts,
            false,
            false,
        )
        .await;
        assert!(
            matches!(thin, Err(ExtractError::UnusableContent { .. })),
            "expected UnusableContent, got {thin:?}"
        );

        // 9. Redirect to cloud metadata IP is caught and reported as BlockedBySsrf.
        let redir = fetch_and_extract_with_client(
            &client,
            &format!("{base}/redirect-imds"),
            &opts,
            false,
            false,
        )
        .await;
        assert!(
            matches!(redir, Err(ExtractError::BlockedBySsrf(_))),
            "redirect to IMDS must return BlockedBySsrf, got {redir:?}"
        );
    }
}