searchwala 8.3.1

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
// ============================================================================
// SearchWala v8.1 — Search modes
//
// ONE place that decides how much work a search does.
//
// WHY THIS EXISTS
// Before v8.1, mode behaviour was spread across a dozen `if is_fast_mode /
// is_research_mode / is_specialized` branches in search.rs, a second copy in
// stream.rs, and hard-coded numbers in the UI. Measured on 2026-10-02 with the
// same query in every mode, the result was close to inverted:
//
//   mode       time    chars extracted   engine tasks
//   fast       5.6 s   1,101,005          9
//   auto       4.6 s     454,170         17
//   research   9.3 s     181,249         62  (27 "Unknown engine" warnings)
//
// Fast was slower than Auto and extracted the MOST text; Deep extracted the
// LEAST. The causes were all mode-plumbing bugs:
//   * the scrape-stage early-exit (25 sources / 80k chars) applied to every
//     mode, so Deep stopped as early as Auto;
//   * Fast had no source or text budget at all, only a URL cap;
//   * Deep/Academic/Reddit/YouTube fell through to `config::enabled_engines()`
//     — the legacy 90-name list, including walled Google aliases and dead
//     hosts that have no spec;
//   * Deep took the non-ranked path: no RRF, no freshness, and BM25 scored
//     against the engine-syntax query.
//
// Every mode now resolves to a `ModeProfile`, and search.rs reads only that.
// ============================================================================

use std::time::Duration;

use crate::engines;
use crate::query_intel::{QueryComplexity, QueryIntelligence, QueryIntent};

// ─────────────────────────────────────────────────────────────────────────────
// Mode and domain
// ─────────────────────────────────────────────────────────────────────────────

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Mode {
    /// Speed first: few engines, one SERP page, a handful of sources.
    Fast,
    /// Sizes itself to the query (see `auto_tier`).
    Auto,
    /// Maximum coverage: every engine, deep decomposition, map-reduce report.
    Deep,
    /// Structured study notes from educational sources.
    Study,
    /// Scholarly sources (OpenAlex, arXiv) and a literature-review answer.
    Academic,
    /// Reddit discussions.
    Reddit,
    /// YouTube videos.
    YouTube,
}

impl Mode {
    pub fn as_str(&self) -> &'static str {
        match self {
            Mode::Fast => "fast",
            Mode::Auto => "auto",
            Mode::Deep => "deep",
            Mode::Study => "study",
            Mode::Academic => "academic",
            Mode::Reddit => "reddit",
            Mode::YouTube => "youtube",
        }
    }
}

/// Subject-area focus. ORTHOGONAL to the mode: "Deep + Health" is a valid
/// combination.
///
/// v7.0/v8.0 encoded a domain as its own mode string (`specialized_health`),
/// and the UI built that string by DISCARDING the selected mode — choosing
/// "Deep Research" and then "Health" silently produced a non-deep search.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Domain {
    General,
    Tech,
    Science,
    Finance,
    Health,
    News,
}

impl Domain {
    pub fn parse(s: &str) -> Domain {
        match s.trim().to_lowercase().as_str() {
            "tech" | "technology" | "code" | "programming" => Domain::Tech,
            "science" | "sci" => Domain::Science,
            "finance" | "business" | "markets" => Domain::Finance,
            "health" | "medical" | "medicine" => Domain::Health,
            "news" => Domain::News,
            _ => Domain::General,
        }
    }

    pub fn as_str(&self) -> &'static str {
        match self {
            Domain::General => "general",
            Domain::Tech => "tech",
            Domain::Science => "science",
            Domain::Finance => "finance",
            Domain::Health => "health",
            Domain::News => "news",
        }
    }
}

/// Parse the request's `focus_mode` and optional `domain`.
///
/// Accepts every legacy spelling so existing API clients keep working:
/// `lite` → Auto, `research` → Deep, `specialized_<domain>` → Auto + domain.
pub fn parse(focus_mode: Option<&str>, domain: Option<&str>) -> (Mode, Domain) {
    let raw = focus_mode
        .map(|m| m.trim().to_lowercase())
        .unwrap_or_default();

    let (mode, legacy_domain) = match raw.as_str() {
        "fast" | "quick" => (Mode::Fast, None),
        "deep" | "research" | "deep_research" => (Mode::Deep, None),
        "study" | "learn" | "notes" => (Mode::Study, None),
        "academic" | "scholar" | "papers" => (Mode::Academic, None),
        "reddit" => (Mode::Reddit, None),
        "youtube" | "video" => (Mode::YouTube, None),
        s if s.starts_with("specialized") => {
            let d = s.strip_prefix("specialized_").unwrap_or("");
            (Mode::Auto, Some(Domain::parse(d)))
        }
        // "auto", "lite", "", and anything unrecognised.
        _ => (Mode::Auto, None),
    };

    let domain = domain
        .map(Domain::parse)
        .filter(|d| *d != Domain::General)
        .or(legacy_domain)
        .unwrap_or(Domain::General);

    (mode, domain)
}

// ─────────────────────────────────────────────────────────────────────────────
// Profile
// ─────────────────────────────────────────────────────────────────────────────

/// How the answer is synthesized.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Synthesis {
    /// Direct answer + a few cited bullets.
    Concise,
    /// Intent-aware single call (the pre-v8.1 default).
    Standard,
    /// Map-reduce: per-batch cited notes, then one report over all notes.
    Report,
    /// Structured study notes.
    StudyNotes,
    /// Literature review.
    Literature,
    /// Summary of community discussion.
    Community,
    /// Overview of videos found.
    Video,
}

/// How sub-queries are generated.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Facets {
    /// The pre-v8.1 conservative decomposition (comparison sides, year,
    /// multi-word entities).
    Basic,
    /// Aspect expansion by intent — overview, evidence, limitations, latest.
    Deep,
    /// Learning-oriented: explanation and worked examples.
    Study,
    /// Review / meta-analysis phrasing for scholarly indices.
    Academic,
}

#[derive(Debug, Clone)]
pub struct ModeProfile {
    pub mode: Mode,
    pub domain: Domain,
    /// Human-readable tier, e.g. "auto:light", for logs and the API response.
    pub tier: &'static str,

    // ── Retrieval ──
    pub engines: Vec<String>,
    /// SERP pages per engine. Fast uses 1: a 3-page engine aborted at a 2.5 s
    /// deadline LOSES the pages it already fetched (measured: 2 of 9 Fast
    /// tasks aborted), so asking for fewer pages is strictly better.
    pub page_budget: usize,
    pub engine_deadline: Duration,
    /// Stop waiting for engines once (results, independent families) are in.
    ///
    /// Only Fast and Auto:lookup use it. Phase 5 measured that exiting early
    /// cost Auto ~60% of its raw results and most cross-engine agreement — the
    /// wrong trade when consensus is the point. For Fast, speed IS the point:
    /// one hung engine was holding the stage to the full 2.5 s deadline when
    /// three families had answered by ~1.3 s.
    pub engine_early_exit: Option<(usize, usize)>,
    /// Restrict every engine query to one site (reddit.com, youtube.com).
    pub site_filter: Option<&'static str>,

    // ── Sub-queries ──
    pub max_sub_queries: usize,
    pub facets: Facets,
    /// Let the LLM plan sub-queries when one is configured (Deep only).
    pub llm_query_planning: bool,

    // ── Scraping ──
    pub max_urls: usize,
    pub scrape_timeout: Duration,
    pub scrape_stage_deadline: Duration,
    /// Early-exit once BOTH thresholds are met.
    pub scrape_min_sources: usize,
    pub scrape_min_chars: usize,
    /// Use the search-engine snippet when a page cannot be fetched. Only worth
    /// it where pages are routinely blocked (Reddit, YouTube).
    pub snippet_fallback: bool,

    // ── Ranking ──
    pub llm_top_k: usize,
    pub max_chunks_per_url: usize,
    /// Apply recency decay (still gated on the query being time-sensitive).
    pub freshness: bool,

    // ── Synthesis ──
    pub synthesis: Synthesis,
    /// Hard ceiling on context characters, below the model's own budget.
    pub context_char_cap: usize,
    pub max_output_tokens: usize,
}

/// URL budget bounds per mode: (floor, ceiling). An explicit `max_results`
/// from the client is clamped into this range; otherwise the profile default
/// is used.
fn url_bounds(mode: Mode) -> (usize, usize) {
    match mode {
        Mode::Fast => (3, 20),
        Mode::Auto => (5, 100),
        Mode::Deep => (40, 300),
        Mode::Study => (10, 80),
        Mode::Academic => (10, 80),
        Mode::Reddit | Mode::YouTube => (5, 50),
    }
}

/// Engines reliable enough for a sub-3-second answer: three JSON APIs plus the
/// three HTML engines that worked consistently with TLS impersonation.
fn fast_roster() -> Vec<String> {
    ["wikipedia", "bing", "duckduckgo", "brave", "marginalia_api"]
        .iter()
        .map(|s| s.to_string())
        .collect()
}

/// Engines that honour a `site:` operator. v8.0 appended `site:reddit.com` to
/// the query and sent it to EVERY engine — including the Wikipedia and
/// Marginalia JSON APIs, which searched for the literal string.
fn site_capable_roster() -> Vec<String> {
    ["bing", "duckduckgo", "brave", "yahoo"]
        .iter()
        .map(|s| s.to_string())
        .collect()
}

/// Public list of reliable engines that honour `site:` queries (including an
/// operator-configured SearXNG instance when enabled).
pub fn site_capable_engines() -> Vec<String> {
    with_searxng(site_capable_roster())
}

/// Secondary site-capable engines used automatically when primary site-capable
/// engines (`bing`, `duckduckgo`, `brave`, `yahoo`) are quarantined mid-session.
pub fn site_capable_fallback_engines() -> Vec<String> {
    ["ecosia", "mojeek", "yandex", "bing_uk", "bing_in"]
        .iter()
        .map(|s| s.to_string())
        .collect()
}

/// True if `name` honours a `site:` operator cleanly.
pub fn is_site_capable_engine(name: &str) -> bool {
    matches!(name, "bing" | "duckduckgo" | "brave" | "yahoo" | "searxng")
}

fn add(list: &mut Vec<String>, names: &[&str]) {
    for n in names {
        if !list.iter().any(|e| e == n) {
            list.push((*n).to_string());
        }
    }
}

/// Add an operator-configured SearXNG instance to any roster. It federates
/// many engines through the operator's own IP, so it helps every mode.
fn with_searxng(mut list: Vec<String>) -> Vec<String> {
    if engines::searxng::is_enabled() {
        add(&mut list, &["searxng"]);
    }
    list
}

/// Domain-specific engine additions.
fn domain_engines(domain: Domain, list: &mut Vec<String>) {
    match domain {
        Domain::News | Domain::Finance => add(list, &["bing_news", "brave_news"]),
        Domain::Science => add(list, &["openalex", "arxiv", "wikipedia"]),
        Domain::Health => add(list, &["openalex", "wikipedia"]),
        Domain::Tech => add(list, &["marginalia_api"]),
        Domain::General => {}
    }
}

/// The three Auto tiers. Auto used to scale only `max_urls`, and the Complex
/// branch did `max_urls.max(100)` — RAISING a caller's explicit cap of 20 to
/// 100.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AutoTier {
    /// Navigational lookups: the user wants a site, not an essay.
    Lookup,
    /// Short factual questions.
    Light,
    Standard,
    /// Long, comparative or explanatory questions.
    Heavy,
}

pub fn auto_tier(intel: &QueryIntelligence) -> AutoTier {
    match intel.intent {
        QueryIntent::Navigational => return AutoTier::Lookup,
        QueryIntent::Research | QueryIntent::Comparison => return AutoTier::Heavy,
        _ => {}
    }
    match intel.complexity {
        QueryComplexity::Simple => AutoTier::Light,
        QueryComplexity::Medium => AutoTier::Standard,
        QueryComplexity::Complex => AutoTier::Heavy,
    }
}

/// Resolve the full profile for one request.
pub fn resolve(
    mode: Mode,
    domain: Domain,
    intel: &QueryIntelligence,
    requested_urls: Option<usize>,
) -> ModeProfile {
    let mut p = match mode {
        // ── FAST ────────────────────────────────────────────────────────────
        Mode::Fast => ModeProfile {
            mode,
            domain,
            tier: "fast",
            engines: fast_roster(),
            page_budget: 1,
            engine_deadline: Duration::from_millis(2_500),
            engine_early_exit: Some((28, 3)),
            site_filter: None,
            max_sub_queries: 1,
            facets: Facets::Basic,
            llm_query_planning: false,
            max_urls: 12,
            scrape_timeout: Duration::from_secs(3),
            scrape_stage_deadline: Duration::from_millis(3_500),
            scrape_min_sources: 8,
            scrape_min_chars: 35_000,
            snippet_fallback: false,
            llm_top_k: 12,
            max_chunks_per_url: 3,
            freshness: true,
            synthesis: Synthesis::Concise,
            context_char_cap: 12_000,
            max_output_tokens: 1_400,
        },

        // ── AUTO ────────────────────────────────────────────────────────────
        Mode::Auto => {
            let tier = auto_tier(intel);
            let (
                name,
                engines_list,
                pages,
                deadline_ms,
                urls,
                min_src,
                min_chars,
                subs,
                top_k,
                out,
            ) = match tier {
                AutoTier::Lookup => (
                    "auto:lookup",
                    fast_roster(),
                    1,
                    2_500,
                    8,
                    5,
                    15_000,
                    1,
                    8,
                    1_400,
                ),
                AutoTier::Light => (
                    "auto:light",
                    engines::primary_engines(),
                    1,
                    4_000,
                    20,
                    10,
                    50_000,
                    1,
                    15,
                    1_200,
                ),
                AutoTier::Standard => (
                    "auto:standard",
                    engines::all_engines(),
                    2,
                    6_000,
                    35,
                    20,
                    120_000,
                    2,
                    25,
                    2_000,
                ),
                AutoTier::Heavy => (
                    "auto:heavy",
                    engines::all_engines(),
                    3,
                    8_000,
                    55,
                    30,
                    250_000,
                    3,
                    35,
                    3_000,
                ),
            };
            let mut engines_list = engines_list;
            // Time-sensitive questions get the news verticals.
            if intel.is_time_sensitive || intel.intent == QueryIntent::Temporal {
                add(&mut engines_list, &["bing_news", "brave_news"]);
            }
            ModeProfile {
                mode,
                domain,
                tier: name,
                engines: engines_list,
                page_budget: pages,
                engine_deadline: Duration::from_millis(deadline_ms),
                engine_early_exit: if tier == AutoTier::Lookup {
                    Some((20, 3))
                } else {
                    None
                },
                site_filter: None,
                max_sub_queries: subs,
                facets: Facets::Basic,
                llm_query_planning: false,
                max_urls: urls,
                scrape_timeout: Duration::from_secs(if tier == AutoTier::Lookup { 3 } else { 8 }),
                scrape_stage_deadline: Duration::from_secs(match tier {
                    AutoTier::Lookup => 4,
                    AutoTier::Light => 6,
                    AutoTier::Standard => 9,
                    AutoTier::Heavy => 12,
                }),
                scrape_min_sources: min_src,
                scrape_min_chars: min_chars,
                snippet_fallback: false,
                llm_top_k: top_k,
                max_chunks_per_url: 3,
                freshness: true,
                synthesis: if tier == AutoTier::Lookup {
                    Synthesis::Concise
                } else {
                    Synthesis::Standard
                },
                context_char_cap: match tier {
                    AutoTier::Lookup => 10_000,
                    AutoTier::Light => 30_000,
                    AutoTier::Standard => 60_000,
                    AutoTier::Heavy => 90_000,
                },
                max_output_tokens: out,
            }
        }

        // ── DEEP ────────────────────────────────────────────────────────────
        Mode::Deep => {
            let mut list = engines::all_engines();
            add(&mut list, &["bing_news", "brave_news"]);
            // Scholarly indices only where they are likely to be relevant.
            // Measured: with Factual included, Deep on "capital of France"
            // ranked three economics papers about financial CAPITAL (Journal of
            // Finance, QJE) above Wikipedia's "Paris".
            if intel.intent == QueryIntent::Research
                || matches!(domain, Domain::Science | Domain::Health | Domain::Tech)
            {
                add(&mut list, &["openalex"]);
            }
            ModeProfile {
                mode,
                domain,
                tier: "deep",
                engines: list,
                page_budget: 3,
                engine_deadline: Duration::from_secs(15),
                engine_early_exit: None,
                site_filter: None,
                max_sub_queries: 5,
                facets: Facets::Deep,
                llm_query_planning: true,
                max_urls: 150,
                scrape_timeout: Duration::from_secs(10),
                scrape_stage_deadline: Duration::from_secs(40),
                // Effectively "scrape everything selected". The 25-source
                // early-exit used to apply here too, which is why Deep
                // extracted less than Fast.
                scrape_min_sources: 120,
                scrape_min_chars: 2_500_000,
                snippet_fallback: false,
                llm_top_k: 120,
                max_chunks_per_url: 4,
                freshness: true,
                synthesis: Synthesis::Report,
                context_char_cap: 400_000,
                max_output_tokens: 8_000,
            }
        }

        // ── STUDY ───────────────────────────────────────────────────────────
        Mode::Study => ModeProfile {
            mode,
            domain,
            tier: "study",
            engines: vec![
                "wikipedia".into(),
                "marginalia_api".into(),
                "bing".into(),
                "duckduckgo".into(),
                "brave".into(),
                "yahoo".into(),
            ],
            page_budget: 2,
            engine_deadline: Duration::from_secs(6),
            engine_early_exit: None,
            site_filter: None,
            max_sub_queries: 3,
            facets: Facets::Study,
            llm_query_planning: false,
            max_urls: 35,
            scrape_timeout: Duration::from_secs(8),
            scrape_stage_deadline: Duration::from_secs(12),
            scrape_min_sources: 20,
            scrape_min_chars: 150_000,
            snippet_fallback: false,
            llm_top_k: 30,
            max_chunks_per_url: 4,
            // Study topics are evergreen; recency bias would demote the
            // canonical explanation in favour of this week's blog post.
            freshness: false,
            synthesis: Synthesis::StudyNotes,
            context_char_cap: 90_000,
            max_output_tokens: 4_000,
        },

        // ── ACADEMIC ────────────────────────────────────────────────────────
        // v8.0 appended `site:edu OR site:gov OR site:nature.com` to the query
        // and sent it to every engine. The JSON APIs searched for that literal
        // text; HTML engines returned university homepages. Academic now goes
        // to scholarly indices directly.
        Mode::Academic => ModeProfile {
            mode,
            domain,
            tier: "academic",
            engines: vec![
                "openalex".into(),
                "arxiv".into(),
                "wikipedia".into(),
                "bing".into(),
                "duckduckgo".into(),
                "brave".into(),
            ],
            page_budget: 2,
            engine_deadline: Duration::from_secs(8),
            engine_early_exit: None,
            site_filter: None,
            max_sub_queries: 2,
            facets: Facets::Academic,
            llm_query_planning: false,
            max_urls: 40,
            scrape_timeout: Duration::from_secs(10),
            scrape_stage_deadline: Duration::from_secs(14),
            scrape_min_sources: 25,
            scrape_min_chars: 120_000,
            snippet_fallback: false,
            llm_top_k: 35,
            max_chunks_per_url: 3,
            freshness: false,
            synthesis: Synthesis::Literature,
            context_char_cap: 90_000,
            max_output_tokens: 4_000,
        },

        // ── REDDIT / YOUTUBE ───────────────────────────────────────────────
        Mode::Reddit | Mode::YouTube => ModeProfile {
            mode,
            domain,
            tier: if mode == Mode::Reddit {
                "reddit"
            } else {
                "youtube"
            },
            engines: site_capable_roster(),
            page_budget: 2,
            engine_deadline: Duration::from_secs(6),
            engine_early_exit: None,
            site_filter: Some(if mode == Mode::Reddit {
                "reddit.com"
            } else {
                "youtube.com"
            }),
            max_sub_queries: 1,
            facets: Facets::Basic,
            llm_query_planning: false,
            max_urls: 25,
            scrape_timeout: Duration::from_secs(8),
            scrape_stage_deadline: Duration::from_secs(10),
            scrape_min_sources: 15,
            scrape_min_chars: 40_000,
            // Reddit thread pages come back as a "Welcome to Reddit" CAPTCHA
            // from many IPs, and YouTube pages carry only a description. The
            // engine snippet is often the best text available.
            snippet_fallback: true,
            llm_top_k: 25,
            max_chunks_per_url: 2,
            freshness: true,
            synthesis: if mode == Mode::Reddit {
                Synthesis::Community
            } else {
                Synthesis::Video
            },
            context_char_cap: 40_000,
            max_output_tokens: 2_000,
        },
    };

    // ── Domain overlay ──
    if mode != Mode::Reddit && mode != Mode::YouTube {
        domain_engines(domain, &mut p.engines);
    }
    if domain == Domain::News {
        p.freshness = true;
    }

    // Fast stays small even with a domain overlay.
    if mode == Mode::Fast {
        p.engines.truncate(7);
    }

    if mode != Mode::Reddit && mode != Mode::YouTube {
        p.engines = with_searxng(std::mem::take(&mut p.engines));
    }

    // ── Explicit URL budget from the client ──
    if let Some(req) = requested_urls {
        let (lo, hi) = url_bounds(mode);
        p.max_urls = req.clamp(lo, hi);
    }
    // The early-exit source threshold can never exceed what we scrape.
    p.scrape_min_sources = p.scrape_min_sources.min(p.max_urls.max(1));

    p
}

/// Every engine any mode can dispatch — what `/health` reports.
pub fn all_roster_engines() -> Vec<String> {
    let intel = crate::query_intel::analyze_query("example research question about a topic");
    let mut all: Vec<String> = Vec::new();
    for mode in [
        Mode::Fast,
        Mode::Auto,
        Mode::Deep,
        Mode::Study,
        Mode::Academic,
        Mode::Reddit,
        Mode::YouTube,
    ] {
        for domain in [
            Domain::General,
            Domain::Tech,
            Domain::Science,
            Domain::Finance,
            Domain::Health,
            Domain::News,
        ] {
            for e in resolve(mode, domain, &intel, None).engines {
                if !all.contains(&e) {
                    all.push(e);
                }
            }
        }
    }
    all.sort();
    all
}

/// One-line summary of a mode's default profile, for `/config`.
pub fn describe_modes() -> serde_json::Value {
    let intel =
        crate::query_intel::analyze_query("how does a typical example topic work in practice");
    let mut out = serde_json::Map::new();
    for mode in [
        Mode::Fast,
        Mode::Auto,
        Mode::Deep,
        Mode::Study,
        Mode::Academic,
        Mode::Reddit,
        Mode::YouTube,
    ] {
        let p = resolve(mode, Domain::General, &intel, None);
        out.insert(
            mode.as_str().to_string(),
            serde_json::json!({
                "tier_for_sample_query": p.tier,
                "engines": p.engines,
                "serp_pages_per_engine": p.page_budget,
                "engine_deadline_ms": p.engine_deadline.as_millis() as u64,
                "max_urls": p.max_urls,
                "sub_queries": p.max_sub_queries,
                "scrape_deadline_ms": p.scrape_stage_deadline.as_millis() as u64,
                "chunks_to_llm": p.llm_top_k,
                "synthesis": format!("{:?}", p.synthesis),
                "max_output_tokens": p.max_output_tokens,
            }),
        );
    }
    serde_json::Value::Object(out)
}

// ─────────────────────────────────────────────────────────────────────────────
// Source preferences per mode and domain
// ─────────────────────────────────────────────────────────────────────────────

/// Structural URL authority signals (zero hardcoded domain lists).
fn host_of(url: &str) -> String {
    parse_host_and_path(url).0
}

pub(crate) fn parse_host_and_path(url: &str) -> (String, String) {
    match url::Url::parse(url) {
        Ok(u) => (
            u.host_str().map(|h| h.to_lowercase()).unwrap_or_default(),
            u.path().to_lowercase(),
        ),
        Err(_) => (String::new(), String::new()),
    }
}

/// Public institutional TLD hierarchy (.gov, .edu, .int, .mil, .ac.*, .gov.*, .edu.*, .nic.*).
pub(crate) fn is_institutional_host(host: &str) -> bool {
    host.ends_with(".gov")
        || host.ends_with(".edu")
        || host.ends_with(".int")
        || host.ends_with(".mil")
        || host.contains(".gov.")
        || host.contains(".edu.")
        || host.contains(".ac.")
        || host.contains(".nic.")
}

/// Structural documentation / developer reference subdomain or path.
pub(crate) fn is_structural_docs_url(host: &str, path: &str) -> bool {
    host.starts_with("docs.")
        || host.starts_with("doc.")
        || host.starts_with("developer.")
        || host.starts_with("developers.")
        || host.starts_with("learn.")
        || host.starts_with("reference.")
        || host.starts_with("manual.")
        || path.starts_with("/docs/")
        || path.starts_with("/doc/")
        || path.starts_with("/documentation/")
        || path.starts_with("/reference/")
        || path.starts_with("/manual/")
        || path.starts_with("/std/")
        || path.starts_with("/api/")
}

/// Structural scholarly/research article path or institutional research host.
pub(crate) fn is_structural_scholarly_url(host: &str, path: &str) -> bool {
    is_institutional_host(host)
        || path.starts_with("/articles/")
        || path.starts_with("/article/")
        || path.starts_with("/abs/")
        || path.starts_with("/pdf/")
        || path.starts_with("/doi/")
        || path.starts_with("/papers/")
        || path.starts_with("/journals/")
}

/// Structural educational or encyclopedic reference URL (verified against lookalike hosts
/// by checking the registrable domain root and path structure).
pub(crate) fn is_structural_encyclopedia_or_study_url(url: &str, host: &str, path: &str) -> bool {
    if is_institutional_host(host)
        || path.starts_with("/textbook")
        || path.starts_with("/courses/")
        || path.starts_with("/lesson")
    {
        return true;
    }
    let reg = crate::url_utils::registrable_domain(url);
    let root_label = reg.split('.').next().unwrap_or("");
    (root_label == "wikipedia" || root_label == "britannica" || root_label == "wikibooks")
        && (path.starts_with("/wiki/") || path.starts_with("/topic/") || path.len() > 1)
}

/// Ranking multiplier for structural mode/domain alignment (zero hardcoded site lists).
pub fn source_boost(url: &str, mode: Mode, domain: Domain) -> f64 {
    let (host, path) = parse_host_and_path(url);
    if host.is_empty() {
        return 1.0;
    }
    let mut b: f64 = 1.0;
    match mode {
        Mode::Study if is_structural_encyclopedia_or_study_url(url, &host, &path) => {
            b *= 1.35;
        }
        Mode::Academic if is_structural_scholarly_url(&host, &path) => {
            b *= 1.4;
        }
        _ => {}
    }
    let domain_match = match domain {
        Domain::Tech => is_structural_docs_url(&host, &path),
        Domain::Science => is_structural_scholarly_url(&host, &path),
        Domain::Health | Domain::Finance => is_institutional_host(&host),
        Domain::News => {
            path.starts_with("/news")
                || path.starts_with("/press")
                || path.starts_with("/announcements")
                || path.starts_with("/releases")
        }
        Domain::General => false,
    };
    if domain_match {
        b *= 1.3;
    }
    b.min(1.8)
}

// ─────────────────────────────────────────────────────────────────────────────
// Sub-query facets
// ─────────────────────────────────────────────────────────────────────────────

/// Strip question scaffolding so facets attach to the topic, not the phrasing:
/// "explain photosynthesis for class 10" -> "photosynthesis for class 10".
pub fn core_topic(query: &str) -> String {
    let q = query.trim().trim_end_matches(['?', '.', '!']);
    let lower = q.to_lowercase();
    const PREFIXES: &[&str] = &[
        "please explain ",
        "can you explain ",
        "explain to me ",
        "explain ",
        "what is the ",
        "what is a ",
        "what is an ",
        "what is ",
        "what are the ",
        "what are ",
        "who is ",
        "who was ",
        "how does ",
        "how do ",
        "how is ",
        "tell me about ",
        "notes on ",
        "study notes on ",
        "study notes for ",
        "teach me ",
        "define ",
        "definition of ",
        "describe ",
        "overview of ",
    ];
    for p in PREFIXES {
        if lower.starts_with(p) {
            // Byte offset is safe: every prefix is ASCII, so it ends on a
            // character boundary in the original string too.
            let rest = q[p.len()..].trim();
            let rest = rest
                .strip_suffix(" work")
                .or_else(|| rest.strip_suffix(" works"))
                .unwrap_or(rest)
                .trim();
            if crate::util::char_len(rest) >= 3 {
                return rest.to_string();
            }
        }
    }
    // Hindi question scaffolding usually comes AFTER the topic:
    // "प्रकाश संश्लेषण क्या है" = "photosynthesis what-is".
    const HI_SUFFIXES: &[&str] = &[
        " क्या होता है",
        " क्या होती है",
        " क्या हैं",
        " क्या है",
        " कैसे काम करता है",
        " कैसे काम करती है",
        " के बारे में बताइए",
        " के बारे में",
        " समझाइए",
        " समझाओ",
    ];
    for s in HI_SUFFIXES {
        if let Some(rest) = q.strip_suffix(s) {
            let rest = rest.trim();
            if crate::util::char_len(rest) >= 2 {
                return rest.to_string();
            }
        }
    }
    q.to_string()
}

/// Mode-specific sub-queries. The original query always comes first.
pub fn facet_queries(
    query: &str,
    intel: &QueryIntelligence,
    facets: Facets,
    max: usize,
) -> Vec<String> {
    let q = query.trim();
    if q.is_empty() || max == 0 {
        return Vec::new();
    }
    let max = max.max(1);
    let topic = core_topic(q);
    let year = chrono::Utc::now().format("%Y").to_string();

    let mut out: Vec<String> = vec![q.to_string()];

    let extra: Vec<String> = match facets {
        Facets::Basic => {
            // Reuse the existing conservative decomposer.
            let base = crate::query_intel::decompose_query(q, intel);
            base.into_iter().skip(1).collect()
        }
        Facets::Deep => {
            let mut v = Vec::new();
            // Comparison sides are the most valuable split there is.
            if intel.intent == QueryIntent::Comparison {
                for sep in [" vs ", " vs. ", " versus ", " compared to "] {
                    if let Some((a, b)) = q.split_once(sep) {
                        v.push(a.trim().to_string());
                        v.push(b.trim().to_string());
                        break;
                    }
                }
            }
            match intel.intent {
                QueryIntent::Temporal => {
                    v.push(format!("{topic} latest news {year}"));
                    v.push(format!("{topic} timeline"));
                    v.push(format!("{topic} analysis"));
                }
                QueryIntent::Person => {
                    v.push(format!("{topic} biography"));
                    v.push(format!("{topic} latest news"));
                    v.push(format!("{topic} interview"));
                }
                QueryIntent::HowTo => {
                    v.push(format!("{topic} step by step guide"));
                    v.push(format!("{topic} common mistakes"));
                    v.push(format!("{topic} best practices"));
                }
                QueryIntent::Navigational => {}
                // A factual question needs corroboration, not "limitations" or
                // "research evidence" — those facets changed the meaning of
                // "capital of France" into economics papers about capital.
                QueryIntent::Factual => {
                    v.push(format!("{topic} overview"));
                    v.push(format!("{topic} history"));
                    v.push(format!("{topic} key facts"));
                }
                _ => {
                    v.push(format!("{topic} explained"));
                    v.push(format!("{topic} research evidence"));
                    v.push(format!("{topic} limitations challenges"));
                    v.push(format!("{topic} latest developments {year}"));
                }
            }
            v
        }
        Facets::Study => vec![
            format!("{topic} explained simply"),
            format!("{topic} examples"),
            format!("{topic} definition key concepts"),
        ],
        Facets::Academic => vec![format!("{topic} review"), format!("{topic} meta-analysis")],
    };

    for e in extra {
        if out.len() >= max {
            break;
        }
        let e = e.split_whitespace().collect::<Vec<_>>().join(" ");
        if crate::util::char_len(&e) < 3 {
            continue;
        }
        if !out.iter().any(|x| x.eq_ignore_ascii_case(&e)) {
            out.push(e);
        }
    }
    out.truncate(max);
    out
}

/// Apply a `site:` filter for Reddit/YouTube modes.
pub fn apply_site_filter(query: &str, site: Option<&str>) -> String {
    match site {
        Some(s) if !query.to_lowercase().contains("site:") => {
            format!("{} site:{}", query.trim(), s)
        }
        _ => query.trim().to_string(),
    }
}

/// Should this result be kept under the profile's site filter?
pub fn matches_site_filter(url: &str, site: Option<&str>) -> bool {
    let Some(site) = site else { return true };
    let host = host_of(url);
    match site {
        "youtube.com" => host.ends_with("youtube.com") || host == "youtu.be",
        s => host == s || host.ends_with(&format!(".{s}")),
    }
}

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

    fn prof(mode: &str, domain: Option<&str>, q: &str) -> ModeProfile {
        let (m, d) = parse(Some(mode), domain);
        resolve(m, d, &analyze_query(q), None)
    }

    #[test]
    fn parses_every_legacy_spelling() {
        assert_eq!(parse(None, None), (Mode::Auto, Domain::General));
        assert_eq!(parse(Some("lite"), None).0, Mode::Auto);
        assert_eq!(parse(Some("research"), None).0, Mode::Deep);
        assert_eq!(parse(Some("DEEP"), None).0, Mode::Deep);
        assert_eq!(parse(Some("study"), None).0, Mode::Study);
        assert_eq!(
            parse(Some("specialized_health"), None),
            (Mode::Auto, Domain::Health)
        );
        assert_eq!(parse(Some("nonsense"), None).0, Mode::Auto);
    }

    #[test]
    fn domain_composes_with_mode_instead_of_replacing_it() {
        // v8.0's UI turned "Deep + Health" into "specialized_health" and lost
        // the Deep. Both now survive.
        assert_eq!(
            parse(Some("deep"), Some("health")),
            (Mode::Deep, Domain::Health)
        );
        // An explicit domain wins over a legacy specialized_ string.
        assert_eq!(
            parse(Some("specialized_tech"), Some("news")).1,
            Domain::News
        );
    }

    #[test]
    fn modes_are_ordered_by_depth() {
        let q = "how does mRNA vaccine technology work";
        let fast = prof("fast", None, q);
        let auto = prof("auto", None, q);
        let deep = prof("deep", None, q);

        // The measured v8.0 inversion: Fast extracted more than Deep.
        assert!(fast.max_urls < auto.max_urls && auto.max_urls < deep.max_urls);
        assert!(fast.engine_deadline < auto.engine_deadline);
        assert!(auto.engine_deadline < deep.engine_deadline);
        assert!(fast.scrape_stage_deadline < auto.scrape_stage_deadline);
        assert!(fast.scrape_min_chars < auto.scrape_min_chars);
        assert!(auto.scrape_min_chars < deep.scrape_min_chars);
        assert!(fast.llm_top_k < auto.llm_top_k && auto.llm_top_k < deep.llm_top_k);
        assert!(fast.engines.len() < deep.engines.len());
        assert!(fast.max_sub_queries < deep.max_sub_queries);
        assert!(fast.page_budget < deep.page_budget);
    }

    #[test]
    fn fast_is_genuinely_small() {
        let p = prof("fast", None, "capital of france");
        assert_eq!(p.page_budget, 1);
        assert_eq!(p.max_sub_queries, 1);
        assert!(p.max_urls <= 12);
        assert!(p.engine_deadline <= Duration::from_millis(2_500));
        assert!(p.scrape_stage_deadline <= Duration::from_secs(4));
        assert_eq!(p.synthesis, Synthesis::Concise);
        // 1,400 completion tokens ensures reasoning models (e.g. gpt-oss-120b) never
        // truncate mid-sentence after spending ~600 tokens on internal reasoning.
        assert!(p.max_output_tokens >= 1_400 && p.max_output_tokens <= 1_600);
    }

    #[test]
    fn only_fast_paths_exit_engines_early() {
        // Early exit trades consensus for speed — right for Fast, wrong for
        // everything that ranks on cross-engine agreement.
        assert!(prof("fast", None, "capital of france")
            .engine_early_exit
            .is_some());
        assert!(prof("auto", None, "github login")
            .engine_early_exit
            .is_some());
        for (mode, q) in [
            ("auto", "capital of france"),
            ("auto", "rust vs go for servers"),
            ("deep", "x y z"),
            ("study", "photosynthesis"),
            ("academic", "crispr"),
        ] {
            assert!(
                prof(mode, None, q).engine_early_exit.is_none(),
                "{mode}/{q}"
            );
        }
    }

    #[test]
    fn deep_does_not_stop_early() {
        let p = prof(
            "deep",
            None,
            "impact of large language models on software engineering",
        );
        assert!(
            p.scrape_min_sources >= 100,
            "Deep must not early-exit at Auto's thresholds"
        );
        assert!(p.scrape_min_chars >= 1_000_000);
        assert_eq!(p.synthesis, Synthesis::Report);
        assert!(p.max_sub_queries >= 4);
        assert!(p.llm_query_planning);
    }

    #[test]
    fn auto_scales_with_the_query() {
        let nav = prof("auto", None, "github login");
        let light = prof("auto", None, "capital of france");
        let heavy = prof(
            "auto",
            None,
            "postgres vs mysql for analytics workloads at scale",
        );
        assert_eq!(nav.tier, "auto:lookup");
        assert_eq!(light.tier, "auto:light");
        assert_eq!(heavy.tier, "auto:heavy");
        assert!(nav.max_urls < light.max_urls && light.max_urls < heavy.max_urls);
        assert!(nav.engines.len() < heavy.engines.len());
        assert!(light.max_sub_queries < heavy.max_sub_queries);
    }

    #[test]
    fn auto_adds_news_for_time_sensitive_queries() {
        let p = prof("auto", None, "latest iphone release news");
        assert!(p.engines.iter().any(|e| e == "bing_news"));
        let q = prof("auto", None, "capital of france");
        assert!(!q.engines.iter().any(|e| e == "bing_news"));
    }

    #[test]
    fn explicit_url_budget_is_clamped_never_inverted() {
        let intel = analyze_query("postgres vs mysql for analytics");
        // v8.0 Auto did max_urls.max(100), raising a caller's 20 to 100.
        let p = resolve(Mode::Auto, Domain::General, &intel, Some(20));
        assert_eq!(p.max_urls, 20);
        let f = resolve(Mode::Fast, Domain::General, &intel, Some(500));
        assert!(
            f.max_urls <= 20,
            "Fast must stay fast even if asked for 500"
        );
        let d = resolve(Mode::Deep, Domain::General, &intel, Some(5));
        assert!(d.max_urls >= 40, "Deep has a floor");
        // The early-exit threshold never exceeds what is scraped.
        assert!(p.scrape_min_sources <= p.max_urls);
    }

    #[test]
    fn rosters_only_name_engines_that_exist() {
        // v8.0's specialized lists still named google, google_scholar,
        // startpage, qwant, stract and duckduckgo_news. Every name in every
        // profile must now build an engine.
        let q = "how does mRNA vaccine technology work";
        for mode in [
            "fast", "auto", "deep", "study", "academic", "reddit", "youtube",
        ] {
            for dom in ["", "tech", "science", "finance", "health", "news"] {
                let p = prof(mode, Some(dom), q);
                for name in &p.engines {
                    let built = crate::engines::get_engines(std::slice::from_ref(name));
                    assert_eq!(
                        built.len(),
                        1,
                        "mode={mode} domain={dom}: {name:?} builds nothing"
                    );
                }
                for walled in [
                    "google",
                    "google_scholar",
                    "startpage",
                    "qwant",
                    "stract",
                    "duckduckgo_news",
                ] {
                    assert!(
                        !p.engines.iter().any(|e| e == walled),
                        "mode={mode} domain={dom} dispatches walled engine {walled}"
                    );
                }
            }
        }
    }

    #[test]
    fn site_filtered_modes_only_use_site_capable_engines() {
        for mode in ["reddit", "youtube"] {
            let p = prof(mode, None, "best mechanical keyboard");
            assert!(p.site_filter.is_some());
            for e in &p.engines {
                assert!(
                    !["wikipedia", "marginalia_api", "wiby", "openalex", "arxiv"]
                        .contains(&e.as_str()),
                    "{mode} sends a site: operator to {e}, which searches for it literally"
                );
            }
            assert!(p.snippet_fallback);
        }
    }

    #[test]
    fn academic_uses_scholarly_indices() {
        let p = prof("academic", None, "CRISPR off-target effects");
        assert!(p.engines.iter().any(|e| e == "openalex"));
        assert!(p.engines.iter().any(|e| e == "arxiv"));
        assert_eq!(p.synthesis, Synthesis::Literature);
        assert!(
            p.site_filter.is_none(),
            "no more site:edu OR site:gov string"
        );
    }

    #[test]
    fn study_is_evergreen_and_structured() {
        let p = prof("study", None, "explain photosynthesis");
        assert_eq!(p.synthesis, Synthesis::StudyNotes);
        assert!(!p.freshness);
        assert_eq!(p.facets, Facets::Study);
    }

    #[test]
    fn domains_add_relevant_engines() {
        assert!(prof("auto", Some("news"), "election results")
            .engines
            .iter()
            .any(|e| e == "bing_news"));
        assert!(prof("auto", Some("science"), "dark matter")
            .engines
            .iter()
            .any(|e| e == "openalex"));
        assert!(prof("fast", Some("finance"), "nifty today").engines.len() <= 7);
    }

    #[test]
    fn source_boost_prefers_mode_and_domain_authorities() {
        assert!(
            source_boost(
                "https://en.wikipedia.org/wiki/X",
                Mode::Study,
                Domain::General
            ) > 1.0
        );
        assert!(
            source_boost(
                "https://ncert.nic.in/textbook",
                Mode::Study,
                Domain::General
            ) > 1.0
        );
        assert!(
            source_boost(
                "https://www.nature.com/articles/x",
                Mode::Academic,
                Domain::General
            ) > 1.0
        );
        assert!(source_boost("https://www.who.int/news", Mode::Auto, Domain::Health) > 1.0);
        assert!(source_boost("https://docs.rs/tokio", Mode::Auto, Domain::Tech) > 1.0);
        assert_eq!(
            source_boost("https://random-blog.test/x", Mode::Study, Domain::Health),
            1.0
        );
        // Capped.
        assert!(source_boost("https://www.nih.gov/x", Mode::Academic, Domain::Health) <= 1.8);
        // Lookalike hosts do not match.
        assert_eq!(
            source_boost(
                "https://notwikipedia.org.evil.test/",
                Mode::Study,
                Domain::General
            ),
            1.0
        );
    }

    #[test]
    fn core_topic_strips_question_scaffolding() {
        assert_eq!(
            core_topic("explain photosynthesis for class 10"),
            "photosynthesis for class 10"
        );
        assert_eq!(
            core_topic("What is quantum entanglement?"),
            "quantum entanglement"
        );
        assert_eq!(
            core_topic("how does TCP congestion control work"),
            "TCP congestion control"
        );
        assert_eq!(core_topic("photosynthesis"), "photosynthesis");
        // Non-ASCII queries pass through untouched and never panic.
        assert_eq!(core_topic("प्रकाश संश्लेषण"), "प्रकाश संश्लेषण");
        // Hindi question words trail the topic.
        assert_eq!(core_topic("प्रकाश संश्लेषण क्या है"), "प्रकाश संश्लेषण");
        assert_eq!(core_topic("प्रकाश संश्लेषण क्या है?"), "प्रकाश संश्लेषण");
        assert_eq!(core_topic("इंटरनेट कैसे काम करता है"), "इंटरनेट");
    }

    #[test]
    fn facets_respect_the_cap_and_keep_the_original_first() {
        let q = "how does mRNA vaccine technology work";
        let intel = analyze_query(q);
        for (facets, max) in [
            (Facets::Deep, 5),
            (Facets::Study, 3),
            (Facets::Academic, 2),
            (Facets::Basic, 3),
        ] {
            let v = facet_queries(q, &intel, facets, max);
            assert!(!v.is_empty() && v.len() <= max, "{facets:?}: {v:?}");
            assert_eq!(v[0], q);
            let mut lower: Vec<String> = v.iter().map(|s| s.to_lowercase()).collect();
            lower.sort();
            lower.dedup();
            assert_eq!(lower.len(), v.len(), "duplicates in {v:?}");
        }
        assert_eq!(
            facet_queries(q, &intel, Facets::Deep, 1),
            vec![q.to_string()]
        );
    }

    #[test]
    fn deep_facets_do_not_change_the_meaning_of_factual_questions() {
        let q = "capital of France";
        let v = facet_queries(q, &analyze_query(q), Facets::Deep, 5);
        for bad in ["limitations", "research evidence", "challenges"] {
            assert!(
                !v.iter().any(|s| s.contains(bad)),
                "{bad} facet on a factual query: {v:?}"
            );
        }
        // Explanatory questions still get the research facets.
        let r = "how does mRNA vaccine technology work";
        let v = facet_queries(r, &analyze_query(r), Facets::Deep, 5);
        assert!(v.iter().any(|s| s.contains("research evidence")), "{v:?}");
    }

    #[test]
    fn deep_only_adds_scholarly_indices_for_research_questions() {
        assert!(!prof("deep", None, "capital of France")
            .engines
            .iter()
            .any(|e| e == "openalex"));
        assert!(prof("deep", None, "how does mRNA vaccine technology work")
            .engines
            .iter()
            .any(|e| e == "openalex"));
        assert!(prof("deep", Some("health"), "capital of France")
            .engines
            .iter()
            .any(|e| e == "openalex"));
    }

    #[test]
    fn deep_facets_split_comparisons() {
        let q = "rust vs go for backend services";
        let v = facet_queries(q, &analyze_query(q), Facets::Deep, 5);
        assert!(v.iter().any(|s| s == "rust"), "{v:?}");
        assert!(v.iter().any(|s| s.starts_with("go")), "{v:?}");
    }

    #[test]
    fn site_filter_helpers() {
        assert_eq!(
            apply_site_filter("best keyboard", Some("reddit.com")),
            "best keyboard site:reddit.com"
        );
        assert_eq!(
            apply_site_filter("x site:foo.com", Some("reddit.com")),
            "x site:foo.com"
        );
        assert_eq!(apply_site_filter("x", None), "x");
        assert!(matches_site_filter(
            "https://old.reddit.com/r/x",
            Some("reddit.com")
        ));
        assert!(!matches_site_filter(
            "https://notreddit.com/r/x",
            Some("reddit.com")
        ));
        assert!(matches_site_filter(
            "https://youtu.be/abc",
            Some("youtube.com")
        ));
        assert!(matches_site_filter("https://anything.test/", None));
    }
}