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
// ============================================================================
// SearchWala v7.0.0 - Query Intelligence Engine
//
// Analyzes user queries BEFORE engine dispatch to determine:
// - Intent type (factual, temporal, person, comparison, howto, etc.)
// - Time sensitivity (needs current info vs evergreen)
// - Key entities (proper nouns, subjects)
// - Query complexity (simple fact vs deep research)
// - Optimal source count and response strategy
//
// All detection is rule-based (zero ML, zero latency, zero dependencies).
// ============================================================================

use chrono::Datelike;
use std::collections::HashSet;

/// Dynamic semantic classification of the query's nature.
/// Populated by the LLM Query Understanding pass (with structural fallback when offline).
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum SemanticCategory {
    /// Specific product, software, platform, company, organization, agency, or tool ("Product/Org")
    ProductOrg,
    /// Breaking news, recent events, latest releases, or current status ("Live News")
    LiveNews,
    /// Historical events, biographies, static scientific definitions, or academic background ("History/Static")
    HistoryStatic,
    /// General everyday questions, how-to tutorials, comparisons, or broad conceptual topics ("General Everyday")
    GeneralEveryday,
}

impl SemanticCategory {
    pub fn from_label(label: &str) -> Option<Self> {
        let norm: String = label
            .trim()
            .chars()
            .filter(|c| c.is_ascii_alphanumeric())
            .map(|c| c.to_ascii_lowercase())
            .collect();
        match norm.as_str() {
            "productorg" | "product" | "organization" | "org" => Some(Self::ProductOrg),
            "livenews" | "news" | "live" | "breakingnews" => Some(Self::LiveNews),
            "historystatic" | "history" | "static" | "biography" | "encyclopedic" => {
                Some(Self::HistoryStatic)
            }
            "generaleveryday" | "general" | "everyday" => Some(Self::GeneralEveryday),
            _ => None,
        }
    }

    pub fn default_for_intent(intent: &QueryIntent, is_time_sensitive: bool) -> Self {
        if is_time_sensitive || *intent == QueryIntent::Temporal {
            Self::LiveNews
        } else {
            match intent {
                QueryIntent::Navigational => Self::ProductOrg,
                QueryIntent::Person | QueryIntent::Factual => Self::HistoryStatic,
                QueryIntent::Comparison | QueryIntent::HowTo | QueryIntent::Research => {
                    Self::GeneralEveryday
                }
                QueryIntent::Temporal => Self::LiveNews,
            }
        }
    }
}

impl std::fmt::Display for SemanticCategory {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SemanticCategory::ProductOrg => write!(f, "Product/Org"),
            SemanticCategory::LiveNews => write!(f, "Live News"),
            SemanticCategory::HistoryStatic => write!(f, "History/Static"),
            SemanticCategory::GeneralEveryday => write!(f, "General Everyday"),
        }
    }
}

/// The detected intent category driving prompt selection and ranking behavior.
#[derive(Debug, Clone, PartialEq)]
pub enum QueryIntent {
    /// Direct factual question: "capital of India", "boiling point of water"
    Factual,
    /// Time-sensitive: "latest OpenAI model", "current CEO of Google"
    Temporal,
    /// Person-focused: "Who is Elon Musk", "Tell me about Einstein"
    Person,
    /// Comparison: "X vs Y", "difference between A and B"
    Comparison,
    /// How-to / tutorial: "how to cook pasta", "how does quantum computing work"
    HowTo,
    /// Navigational: user wants a specific site ("reddit", "youtube login")
    Navigational,
    /// Deep research / complex multi-part query
    Research,
}

/// How complex the user's query is — drives source count and response depth.
#[derive(Debug, Clone, PartialEq)]
pub enum QueryComplexity {
    /// 1-3 words, simple factual lookup
    Simple,
    /// 4-8 words, moderate detail needed
    Medium,
    /// 9+ words, multi-part or nuanced
    Complex,
}

/// Full intelligence report for a user query.
#[derive(Debug, Clone)]
pub struct QueryIntelligence {
    /// Primary detected intent
    pub intent: QueryIntent,
    /// Dynamic semantic category (Product/Org, Live News, History/Static, General Everyday)
    pub semantic_category: SemanticCategory,
    /// Whether the query needs current/fresh information
    pub is_time_sensitive: bool,
    /// Extracted temporal hint if any
    pub temporal_hint: Option<String>,
    /// Key entities extracted from the query (proper nouns, subjects)
    pub key_entities: Vec<String>,
    /// Query complexity level
    pub complexity: QueryComplexity,
    /// Recommended number of sources to pass to LLM
    pub optimal_source_count: usize,
    /// Whether to boost news engines in dispatch
    pub boost_news_engines: bool,
    /// Semantic second-pass corrective query when initial evidence is insufficient
    pub corrective_query: Option<String>,
}

// =============================================================================
// Public API
// =============================================================================

/// Analyze a query and produce a full intelligence report.
/// This runs in < 1ms — pure string matching, zero allocations beyond output.
pub fn analyze_query(query: &str) -> QueryIntelligence {
    let q = query.trim();
    let q_lower = q.to_lowercase();
    let words: Vec<&str> = q_lower.split_whitespace().collect();
    let word_count = words.len();

    // ── Step 1: Detect intent ──
    let intent = detect_intent(&q_lower, &words);

    // ── Step 2: Time sensitivity ──
    let (is_time_sensitive, temporal_hint) = detect_temporal(&q_lower, &intent);

    // ── Step 3: Entity extraction ──
    let key_entities = extract_entities(q);

    // ── Step 4: Complexity ──
    let complexity = if word_count <= 3 {
        QueryComplexity::Simple
    } else if word_count <= 8 {
        QueryComplexity::Medium
    } else {
        QueryComplexity::Complex
    };

    // ── Step 5: Optimal source count ──
    let optimal_source_count = match (&intent, &complexity) {
        (QueryIntent::Factual, QueryComplexity::Simple) => 15,
        (QueryIntent::Factual, _) => 20,
        (QueryIntent::Temporal, _) => 25,
        (QueryIntent::Person, _) => 25,
        (QueryIntent::Comparison, _) => 30,
        (QueryIntent::HowTo, _) => 25,
        (QueryIntent::Navigational, _) => 10,
        (QueryIntent::Research, _) => 40,
    };

    // ── Step 6: News engine boost ──
    let boost_news_engines = is_time_sensitive
        || intent == QueryIntent::Temporal
        || q_lower.contains("news")
        || q_lower.contains("breaking")
        || q_lower.contains("announced");

    let semantic_category = SemanticCategory::default_for_intent(&intent, is_time_sensitive);

    QueryIntelligence {
        intent,
        semantic_category,
        is_time_sensitive,
        temporal_hint,
        key_entities,
        complexity,
        optimal_source_count,
        boost_news_engines,
        corrective_query: None,
    }
}

// =============================================================================
// Intent Detection — Pattern-based classification
// =============================================================================

/// Structural navigational check: determines if a token looks like a destination
/// site or brand identifier rather than an interrogative/question word (zero hardcoded site lists).
fn is_structural_site_leader(word: &str) -> bool {
    let clean = word.trim_matches(|c: char| !c.is_alphanumeric() && c != '.');
    if clean.len() < 2 {
        return false;
    }
    if clean.contains('.') && !clean.starts_with('.') && !clean.ends_with('.') {
        return true;
    }
    !matches!(
        clean,
        "what"
            | "how"
            | "why"
            | "who"
            | "when"
            | "where"
            | "which"
            | "is"
            | "are"
            | "was"
            | "were"
            | "can"
            | "could"
            | "should"
            | "does"
            | "do"
            | "did"
            | "will"
            | "would"
            | "explain"
            | "compare"
            | "define"
            | "tell"
            | "show"
            | "list"
            | "give"
            | "the"
            | "a"
            | "an"
            | "best"
            | "top"
            | "latest"
            | "current"
            | "recent"
            | "upcoming"
            | "history"
            | "biography"
            | "overview"
            | "impact"
            | "future"
    )
}

/// Words that mark a query as wanting a specific page on a site.
const NAV_INTENTS: &[&str] = &[
    "login",
    "log",
    "signin",
    "sign",
    "website",
    "site",
    "homepage",
    "home",
    "dashboard",
    "account",
    "settings",
    "tracking",
    "orders",
    "order",
    "trending",
    "search",
    "docs",
    "documentation",
    "repository",
    "repo",
    "questions",
    "profile",
    "inbox",
    "download",
    "pricing",
    "checkout",
];

/// Biographical framing that makes a query about a PERSON even without an
/// explicit "who is" / "biography of" prefix.
const BIOGRAPHICAL: &[&str] = &[
    "achievements",
    "contributions",
    "biography",
    "career",
    "net worth",
    "early life",
    "born",
    "childhood",
    "awards",
    "legacy",
    "inventions",
    "discoveries",
    "known for",
    "life of",
    "education",
    "family",
];

fn detect_intent(q_lower: &str, words: &[&str]) -> QueryIntent {
    // ── Comparison (checked FIRST) ──
    // Must precede the navigational check: "Docker vs Kubernetes differences"
    // leads with a word that is also a site name, and the eval harness caught
    // it being classified navigational.
    // ── Comparison ──────────────────────────────────────────────────────────
    if q_lower.contains(" vs ")
        || q_lower.contains(" vs. ")
        || q_lower.contains(" versus ")
        || q_lower.contains("difference between")
        || q_lower.contains("differences")
        || q_lower.contains("compared to")
        || q_lower.contains("comparison of")
        || q_lower.contains("comparison")
        || (q_lower.contains(" or ") && (q_lower.contains("which") || q_lower.contains("better")))
    {
        return QueryIntent::Comparison;
    }

    // ── Navigational ────────────────────────────────────────────────────────
    // v7.0 required <=3 words AND one of login/website/exact-match, so
    // "github new repository", "amazon order tracking", "youtube trending",
    // "stackoverflow rust questions", "MDN CSS flexbox documentation" and
    // "Rust crates.io search" all fell through to Factual — 6 of the 15
    // misclassifications the eval harness surfaced.
    if words.len() <= 5 {
        let leads_with_site = words
            .first()
            .map(|w| is_structural_site_leader(w))
            .unwrap_or(false);
        let has_domain_token = words
            .iter()
            .any(|w| w.contains('.') && w.len() >= 4 && !w.ends_with('.'));
        let has_nav_word = words.iter().any(|w| NAV_INTENTS.contains(w));

        if (leads_with_site && has_nav_word) || (has_domain_token && words.len() <= 4) {
            return QueryIntent::Navigational;
        }
    }

    // ── Research vs HowTo ───────────────────────────────────────────────────
    // These are genuinely different intents and v7.0 collapsed them:
    //   "how TO do X"        -> procedural instructions   (HowTo)
    //   "how DOES X work"    -> conceptual explanation    (Research)
    // The eval set expects Research for "how does the TCP three-way handshake
    // work in detail", and v7.0 answered HowTo because it matched "how does"
    // in its HowTo list.
    let explanatory = q_lower.starts_with("how does")
        || q_lower.starts_with("how do ")
        || q_lower.starts_with("how is")
        || q_lower.starts_with("how are")
        || q_lower.starts_with("why ")
        || q_lower.starts_with("what happens when");
    let wants_depth = q_lower.contains("in detail")
        || q_lower.contains("step by step")
        || q_lower.contains("under the hood")
        || q_lower.contains("internals")
        || q_lower.contains("use cases")
        || q_lower.contains("implications")
        || q_lower.contains("and what are");

    const RESEARCH_MARKERS: &[&str] = &[
        "explain",
        "analyze",
        "analyse",
        "research",
        "comprehensive",
        "in depth",
        "in-depth",
        "deep dive",
        "overview of",
        "implications of",
        "impact of",
        "future of",
        "trade-offs",
        "tradeoffs",
        "architecture of",
    ];
    if RESEARCH_MARKERS.iter().any(|m| q_lower.contains(m))
        || (explanatory && (wants_depth || q_lower.contains("work")))
    {
        return QueryIntent::Research;
    }

    // ── Person ──────────────────────────────────────────────────────────────
    const PERSON_PREFIXES: &[&str] = &[
        "who is",
        "who was",
        "who are",
        "tell me about",
        "biography of",
        "bio of",
        "life of",
        "about the person",
    ];
    if PERSON_PREFIXES.iter().any(|p| q_lower.contains(p)) {
        return QueryIntent::Person;
    }
    // Biographical framing around a capitalized multi-word name, e.g.
    // "Linus Torvalds contributions to open source", "Alan Turing achievements".
    if BIOGRAPHICAL.iter().any(|b| q_lower.contains(b)) && words.len() >= 2 {
        return QueryIntent::Person;
    }

    // ── HowTo (procedural) ──────────────────────────────────────────────────
    const HOWTO_MARKERS: &[&str] = &[
        "how to",
        "how can i",
        "steps to",
        "guide to",
        "tutorial",
        "instructions for",
        "ways to",
        "tips for",
        "best way to",
        "learn to",
        "teach me",
        "set up",
        "setup",
        "install",
        "configure",
        "deploy",
        "create a",
        "build a",
    ];
    if HOWTO_MARKERS.iter().any(|m| q_lower.contains(m)) {
        return QueryIntent::HowTo;
    }

    // ── Temporal ────────────────────────────────────────────────────────────
    // Single words matched on WORD BOUNDARIES (P0-12); phrases by substring.
    const TEMPORAL_WORDS: &[&str] = &[
        "latest",
        "current",
        "newest",
        "recent",
        "today",
        "breaking",
        "live",
        "upcoming",
        "now",
        "tonight",
        "yesterday",
        "tomorrow",
    ];
    const TEMPORAL_PHRASES: &[&str] = &[
        "right now",
        "as of",
        "just announced",
        "this year",
        "this week",
        "this month",
        "what is the price",
        "stock price",
        "market cap",
        "score of",
        "results of",
        "who won",
        "schedule",
    ];
    if TEMPORAL_WORDS.iter().any(|w| words.iter().any(|t| t == w))
        || TEMPORAL_PHRASES.iter().any(|p| q_lower.contains(p))
    {
        return QueryIntent::Temporal;
    }
    // An explicit recent/near-future year makes the query time-anchored:
    // "2026 FIFA World Cup schedule", "upcoming solar eclipse 2026",
    // "who won the 2025 NBA Finals". v7.0 only checked years in
    // detect_temporal, which runs AFTER intent is already decided.
    {
        let this_year = chrono::Utc::now().year();
        if words.iter().any(|w| {
            w.parse::<i32>()
                .map(|y| (this_year - 3..=this_year + 5).contains(&y))
                .unwrap_or(false)
        }) {
            return QueryIntent::Temporal;
        }
    }

    // Role/status facts that change over time.
    const ROLE_PATTERNS: &[&str] = &[
        "ceo of",
        "president of",
        "prime minister of",
        "leader of",
        "founder of",
        "chairman of",
        "director of",
        "head of",
        "net worth",
        "salary of",
        // "population of" deliberately omitted: it is a slowly-changing
        // reference fact, and treating it as temporal appends a spurious year.
    ];
    if ROLE_PATTERNS.iter().any(|p| q_lower.contains(p)) {
        return QueryIntent::Temporal;
    }

    // ── Long queries default to Research ────────────────────────────────────
    if words.len() >= 9 {
        return QueryIntent::Research;
    }

    QueryIntent::Factual
}

// =============================================================================
// Temporal / Time-Sensitivity Detection
// =============================================================================

/// Tokenize on non-alphanumerics, for word-boundary matching.
fn word_tokens(q: &str) -> Vec<&str> {
    q.split(|c: char| !crate::util::is_word_char(c))
        .filter(|t| !t.is_empty())
        .collect()
}

fn detect_temporal(q_lower: &str, intent: &QueryIntent) -> (bool, Option<String>) {
    // ═══════════════════════════════════════════════════════════════════════
    // v8.0 P0-12: WORD-BOUNDARY matching.
    //
    // v7.0 used `q_lower.contains(kw)` over a list that included "new",
    // "now", "just" and "is it". Substring matching made these all
    // time-sensitive, which appended a year to the search query:
    //
    //   "New York population"      -> matched "new"
    //   "how do I know if..."      -> matched "now"  (k-NOW)
    //   "justice system reform"    -> matched "just"
    //   "renewable energy"         -> matched "new"  (re-NEW-able)
    //   "this item is on sale"     -> matched "is it"
    //
    // A spurious year in the query skews every engine's results and adds a
    // bogus BM25 term.
    // ═══════════════════════════════════════════════════════════════════════
    const RECENCY_WORDS: &[&str] = &[
        "latest",
        "current",
        "currently",
        "newest",
        "recent",
        "recently",
        "today",
        "now",
        "updated",
        "breaking",
        "upcoming",
        "live",
        "ongoing",
        "tonight",
        "yesterday",
        "tomorrow",
    ];
    // "new" and "just" are DELIBERATELY absent — too ambiguous to be useful
    // even with word boundaries ("new car", "just one question").
    const RECENCY_PHRASES: &[&str] = &[
        "this year",
        "this week",
        "this month",
        "this quarter",
        "right now",
        "as of",
        "last week",
        "last month",
        "last year",
        "so far",
        "up to date",
        "at the moment",
        "these days",
    ];

    let toks = word_tokens(q_lower);

    if let Some(w) = RECENCY_WORDS.iter().find(|w| toks.iter().any(|t| t == *w)) {
        return (true, Some((*w).to_string()));
    }
    if let Some(p) = RECENCY_PHRASES.iter().find(|p| q_lower.contains(*p)) {
        return (true, Some((*p).to_string()));
    }

    // Role/status queries: the answer changes over time, so freshness matters
    // even without an explicit recency word. These are multi-word and
    // unambiguous, so phrase matching is safe.
    const DYNAMIC_FACTS: &[&str] = &[
        "ceo of",
        "president of",
        "prime minister of",
        "leader of",
        "founder of",
        "chairman of",
        "director of",
        "head of",
        "owner of",
        "net worth",
        "worth of",
        "stock price",
        "share price",
        "market cap",
        "population of",
        "exchange rate",
        "weather in",
        "score of",
        "who is the",
        "how much is",
        "what is the price",
    ];
    if let Some(p) = DYNAMIC_FACTS.iter().find(|p| q_lower.contains(*p)) {
        return (true, Some((*p).to_string()));
    }

    if *intent == QueryIntent::Temporal {
        return (true, Some("current".to_string()));
    }

    // An explicit year in the query is itself a temporal signal (resolved dynamically).
    let this_year = chrono::Utc::now().year();
    for year in (this_year - 10)..=(this_year + 10) {
        let y = year.to_string();
        if toks.iter().any(|t| *t == y) {
            return (true, Some(y));
        }
    }

    (false, None)
}

// =============================================================================
// Entity Extraction — Lightweight proper noun detection
// =============================================================================

fn is_contraction_token(raw_word: &str) -> bool {
    let trimmed =
        raw_word.trim_matches(|c: char| !crate::util::is_word_char(c) && c != '\'' && c != '’');
    if let Some(rest) = trimmed
        .strip_suffix("'s")
        .or_else(|| trimmed.strip_suffix("’s"))
    {
        return rest.contains('\'') || rest.contains('’');
    }
    trimmed.contains('\'') || trimmed.contains('’')
}

fn strip_possessive(word: &str) -> &str {
    word.strip_suffix("'s")
        .or_else(|| word.strip_suffix("’s"))
        .unwrap_or(word)
}

fn has_internal_caps_or_digits(word: &str) -> bool {
    word.chars()
        .skip(1)
        .any(|c| c.is_uppercase() || c.is_ascii_digit())
}

fn extract_entities(query: &str) -> Vec<String> {
    let stop_words: HashSet<&str> = [
        "what", "is", "the", "a", "an", "of", "in", "to", "for", "and", "or", "how", "does", "do",
        "can", "about", "with", "from", "that", "this", "are", "was", "were", "be", "been",
        "being", "have", "has", "had", "will", "would", "it", "its", "on", "at", "by", "but",
        "not", "so", "if", "than", "then", "my", "me", "we", "you", "your", "who", "which", "when",
        "where", "why", "all", "each", "tell", "give", "show", "find", "between", "vs", "versus",
        "compared", "latest", "current", "recent", "new", "best", "top", "most", "many", "much",
        "very", "just", "also", "now", "today", "explain", "describe", "analyze",
    ]
    .iter()
    .cloned()
    .collect();

    let total_words = query.split_whitespace().count();
    let mut entities = Vec::new();

    // Split on clause/sentence boundaries first so tokens across punctuation
    // (e.g. "... Cowork? Specifically, I’m ...") never merge into one entity.
    let clauses = query.split(|c: char| {
        matches!(
            c,
            '.' | '?'
                | '!'
                | ','
                | ';'
                | ':'
                | '\n'
                | '—'
                | '–'
                | '('
                | ')'
                | '['
                | ']'
                | '"'
                | '“'
                | '”'
        )
    });

    for clause in clauses {
        let mut current_entity: Vec<(String, bool, bool)> = Vec::new(); // (token, is_clause_start, has_internal_caps)
        let mut seen_any_token_in_clause = false;

        for raw_word in clause.split_whitespace() {
            let is_clause_start = !seen_any_token_in_clause;
            seen_any_token_in_clause = true;

            if is_contraction_token(raw_word) {
                if !current_entity.is_empty() {
                    if current_entity.len() >= 2
                        || !current_entity[0].1
                        || current_entity[0].2
                        || total_words <= 4
                    {
                        entities.push(
                            current_entity
                                .iter()
                                .map(|(w, _, _)| w.as_str())
                                .collect::<Vec<_>>()
                                .join(" "),
                        );
                    }
                    current_entity.clear();
                }
                continue;
            }

            let without_possessive = strip_possessive(raw_word);
            let clean = without_possessive.trim_matches(|c: char| !crate::util::is_word_char(c));
            if clean.is_empty() {
                continue;
            }

            let first_char = clean.chars().next().unwrap_or(' ');
            let lower = clean.to_lowercase();

            if first_char.is_uppercase()
                && !stop_words.contains(lower.as_str())
                && clean.chars().count() > 1
            {
                current_entity.push((
                    clean.to_string(),
                    is_clause_start,
                    has_internal_caps_or_digits(clean),
                ));
            } else if !current_entity.is_empty() {
                // Only keep a single-word clause-initial capitalized token if it has
                // internal caps/digits (ClickUp, OpenAI, ISRO) or the whole query is short.
                if current_entity.len() >= 2
                    || !current_entity[0].1
                    || current_entity[0].2
                    || total_words <= 4
                {
                    entities.push(
                        current_entity
                            .iter()
                            .map(|(w, _, _)| w.as_str())
                            .collect::<Vec<_>>()
                            .join(" "),
                    );
                }
                current_entity.clear();
            }
        }

        if !current_entity.is_empty() {
            if current_entity.len() >= 2
                || !current_entity[0].1
                || current_entity[0].2
                || total_words <= 4
            {
                entities.push(
                    current_entity
                        .iter()
                        .map(|(w, _, _)| w.as_str())
                        .collect::<Vec<_>>()
                        .join(" "),
                );
            }
            current_entity.clear();
        }
    }

    // Fallback for scripts without uppercase/lowercase (e.g. Devanagari/Hindi)
    // or all-lowercase queries where the user did not capitalize proper nouns.
    if entities.is_empty() {
        let has_non_ascii_alpha = query.chars().any(|c| !c.is_ascii() && c.is_alphabetic());
        let has_any_uppercase = query.chars().any(|c| c.is_uppercase());
        if has_non_ascii_alpha {
            let non_ascii_tokens: Vec<String> = query
                .split_whitespace()
                .map(|w| w.trim_matches(|c: char| !crate::util::is_word_char(c)))
                .filter(|w| {
                    w.chars().any(|c| !c.is_ascii() && c.is_alphabetic()) && w.chars().count() >= 3
                })
                .take(4)
                .map(|w| w.to_string())
                .collect();
            if !non_ascii_tokens.is_empty() {
                entities.push(non_ascii_tokens.join(" "));
            }
        } else if !has_any_uppercase {
            let mut phrase = Vec::new();
            for raw_word in query.split_whitespace() {
                if is_contraction_token(raw_word) {
                    continue;
                }
                let clean = strip_possessive(raw_word)
                    .trim_matches(|c: char| !crate::util::is_word_char(c));
                let lower = clean.to_lowercase();
                if clean.chars().count() >= 3
                    && !stop_words.contains(lower.as_str())
                    && !clean.chars().all(|c| c.is_ascii_digit())
                {
                    phrase.push(clean.to_string());
                    if phrase.len() >= 3 {
                        break;
                    }
                } else if !phrase.is_empty() {
                    break;
                }
            }
            if !phrase.is_empty() {
                entities.push(phrase.join(" "));
            }
        }
    }

    // Deduplicate case-insensitively while preserving order
    let mut seen = HashSet::new();
    entities.retain(|e| seen.insert(e.to_lowercase()));

    entities
}

// =============================================================================
// Display implementations for logging
// =============================================================================

impl std::fmt::Display for QueryIntent {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            QueryIntent::Factual => write!(f, "factual"),
            QueryIntent::Temporal => write!(f, "temporal"),
            QueryIntent::Person => write!(f, "person"),
            QueryIntent::Comparison => write!(f, "comparison"),
            QueryIntent::HowTo => write!(f, "howto"),
            QueryIntent::Navigational => write!(f, "navigational"),
            QueryIntent::Research => write!(f, "research"),
        }
    }
}

impl std::fmt::Display for QueryComplexity {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            QueryComplexity::Simple => write!(f, "simple"),
            QueryComplexity::Medium => write!(f, "medium"),
            QueryComplexity::Complex => write!(f, "complex"),
        }
    }
}

// =============================================================================
// v7.0: Multi-Query Decomposition
// Instead of searching one query, decompose into multiple targeted sub-queries
// for more comprehensive and relevant results.
// =============================================================================

/// Configuration for multi-query behavior
pub fn multi_query_enabled() -> bool {
    std::env::var("MULTI_QUERY")
        .unwrap_or_else(|_| "true".to_string())
        .to_lowercase()
        == "true"
}

pub fn max_sub_queries() -> usize {
    std::env::var("MAX_SUB_QUERIES")
        .ok()
        .and_then(|v| v.parse().ok())
        .unwrap_or(5)
        .clamp(1, 8)
}

/// Decomposes a user query into multiple targeted sub-queries
/// for more comprehensive search coverage.
/// Rule-based (zero latency, zero dependencies) — uses intent + entity analysis.
pub fn decompose_query(query: &str, intel: &QueryIntelligence) -> Vec<String> {
    // ═══════════════════════════════════════════════════════════════════════
    // v8.0 P1-9: decompose only when it can pay off, and cap hard.
    //
    // v7.0 generated up to 5 sub-queries including:
    //   * midpoint splits of long queries -> "what are the implications of"
    //   * BARE single-word entities        -> "Elon"
    //   * generic suffixes                 -> "{q} definition", "{q} tutorial"
    //
    // Each became a full engine dispatch. With 35 engines x 5 variations x 2-3
    // pages that is 350-525 HTTP requests per search from one IP inside a 6s
    // deadline: it could not complete AND it was the fastest route to the
    // rate limits the audit measured. Worse, `ranking::cross_query_fusion` (now `cross_query_scores`) —
    // which exists specifically to weight the original query 2x — was DEAD
    // CODE, so results from "what are the implications of" carried the same
    // RRF weight as the real query.
    // ═══════════════════════════════════════════════════════════════════════
    let q = query.trim();
    if q.is_empty() {
        return Vec::new();
    }

    // Nothing to decompose: the user wants one specific site, or the query is
    // already minimal.
    if intel.intent == QueryIntent::Navigational || intel.complexity == QueryComplexity::Simple {
        return vec![q.to_string()];
    }

    let mut out = vec![q.to_string()];

    // Comparison split is genuinely useful: "X vs Y" benefits from searching
    // each side, because pages about X alone often beat comparison pages.
    if intel.intent == QueryIntent::Comparison {
        for sep in [" vs ", " vs. ", " versus ", " compared to ", " or "] {
            if let Some((a, b)) = q.split_once(sep) {
                let (a, b) = (a.trim(), b.trim());
                // Both sides must be substantial — "A or B?" fragments are not.
                if crate::util::char_len(a) >= 3 && crate::util::char_len(b) >= 3 {
                    out.push(a.to_string());
                    out.push(b.to_string());
                }
                break;
            }
        }
    }

    // Time-sensitive queries benefit from an explicit year, which is a real
    // recall win on news indices.
    if intel.is_time_sensitive && out.len() < max_sub_queries() {
        let year = chrono::Utc::now().format("%Y").to_string();
        if !q.contains(&year) {
            out.push(format!("{q} {year}"));
        }
    }

    // Multi-word named entities, paired with the query's head term so the
    // sub-query still has topical context. Bare entities are NOT emitted.
    for e in intel
        .key_entities
        .iter()
        .filter(|e| e.split_whitespace().count() >= 2)
    {
        if out.len() >= max_sub_queries() {
            break;
        }
        out.push(e.clone());
    }

    // Dedup case-insensitively, then cap. 3 is the practical ceiling: beyond
    // that the per-host gate serializes requests past the dispatch deadline.
    let mut seen = std::collections::HashSet::new();
    out.retain(|s| seen.insert(s.to_lowercase()));
    out.truncate(max_sub_queries().min(3));
    out
}

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

    // ── P0-12: substring false positives ──

    #[test]
    fn new_york_is_not_time_sensitive() {
        // v7.0: "New York population" matched the substring "new".
        let i = analyze_query("New York population");
        assert!(
            !i.is_time_sensitive || i.temporal_hint.as_deref() != Some("new"),
            "matched on 'new' inside 'New York': {:?}",
            i.temporal_hint
        );
    }

    #[test]
    fn know_renewable_and_justice_are_not_time_sensitive() {
        for q in [
            "how do I know if my code compiles",
            "renewable energy sources explained",
            "justice system reform history",
            "newton laws of motion",
            "snowfall in the alps",
        ] {
            let i = analyze_query(q);
            assert!(
                !i.is_time_sensitive,
                "{q:?} wrongly time-sensitive (hint={:?}) — substring match regression",
                i.temporal_hint
            );
        }
    }

    #[test]
    fn genuinely_recent_queries_are_still_detected() {
        for q in [
            "latest iphone release",
            "current ceo of google",
            "what happened today in markets",
            "breaking news ukraine",
            "openai announcement this week",
            "who is the prime minister of india",
            "bitcoin stock price",
        ] {
            assert!(
                analyze_query(q).is_time_sensitive,
                "{q:?} should be time-sensitive"
            );
        }
    }

    #[test]
    fn explicit_year_is_a_temporal_signal() {
        assert!(analyze_query("cricket world cup 2027 schedule").is_time_sensitive);
        // But a number that merely looks year-ish inside a word must not count.
        assert!(!analyze_query("model 2020x specifications").is_time_sensitive);
    }

    // ── P1-9: decomposition discipline ──

    #[test]
    fn simple_and_navigational_queries_are_not_decomposed() {
        let simple = analyze_query("rust lifetimes");
        assert_eq!(decompose_query("rust lifetimes", &simple).len(), 1);
        let nav = analyze_query("youtube login");
        assert_eq!(decompose_query("youtube login", &nav).len(), 1);
    }

    #[test]
    fn no_midpoint_fragments_or_bare_entities() {
        let q = "what are the long term implications of quantum computing for modern cryptography";
        let intel = analyze_query(q);
        let subs = decompose_query(q, &intel);
        for s in &subs {
            // v7.0 emitted "what are the long term implications of" and "Elon".
            assert!(
                s == q || s.split_whitespace().count() >= 2,
                "bare/fragment sub-query emitted: {s:?}"
            );
            assert!(
                !s.ends_with(" of") && !s.ends_with(" the") && !s.ends_with(" for"),
                "dangling-preposition fragment: {s:?}"
            );
        }
    }

    #[test]
    fn decomposition_is_capped_at_three() {
        let q = "compare Apple Vision Pro versus Meta Quest Three for enterprise training in 2027";
        let intel = analyze_query(q);
        let subs = decompose_query(q, &intel);
        assert!(subs.len() <= 3, "got {} sub-queries: {subs:?}", subs.len());
        assert_eq!(subs[0], q, "the original query must come first");
    }

    #[test]
    fn comparison_queries_split_into_both_sides() {
        let q = "postgres vs mysql for analytics";
        let intel = analyze_query(q);
        let subs = decompose_query(q, &intel);
        assert!(subs.len() >= 2, "comparison should split: {subs:?}");
        assert!(subs.iter().any(|s| s.contains("postgres")));
    }

    #[test]
    fn decomposition_never_returns_duplicates() {
        let q = "latest ai news 2026";
        let intel = analyze_query(q);
        let subs = decompose_query(q, &intel);
        let mut lower: Vec<String> = subs.iter().map(|s| s.to_lowercase()).collect();
        lower.sort();
        let before = lower.len();
        lower.dedup();
        assert_eq!(before, lower.len(), "duplicates in {subs:?}");
    }

    // ── Intent classification, against the golden-set labels ──
    //
    // The eval harness measured 70% (35/50) and named every miss. These lock
    // in the fixes so the classifier cannot silently regress.

    #[test]
    fn navigational_recognises_site_led_queries() {
        // All six of these were classified Factual in v7.0, which requires
        // <=3 words AND an explicit login/website token.
        for q in [
            "github new repository",
            "amazon order tracking",
            "youtube trending",
            "stackoverflow rust questions",
            "MDN CSS flexbox documentation",
            "gmail login",
        ] {
            assert_eq!(
                analyze_query(q).intent,
                QueryIntent::Navigational,
                "{q:?} should be navigational"
            );
        }
    }

    #[test]
    fn comparison_beats_navigational_when_both_match() {
        // "Docker vs Kubernetes differences" leads with a word that is also a
        // site name. Comparison must win — the eval harness caught this.
        assert_eq!(
            analyze_query("Docker vs Kubernetes differences").intent,
            QueryIntent::Comparison
        );
        assert_eq!(
            analyze_query("github vs gitlab for private repos").intent,
            QueryIntent::Comparison
        );
    }

    #[test]
    fn explanatory_how_does_is_research_not_howto() {
        // "how TO do X" is procedural; "how DOES X work" is conceptual.
        // v7.0 collapsed both into HowTo.
        for q in [
            "how does the TCP three-way handshake work in detail",
            "how does HTTPS TLS encryption work step by step",
            "how do bloom filters work and what are their use cases",
        ] {
            assert_eq!(analyze_query(q).intent, QueryIntent::Research, "{q:?}");
        }
        // Procedural queries stay HowTo.
        for q in [
            "how to set up SSH keys on Linux",
            "how to center a div in CSS",
            "how to deploy a Docker container to AWS",
        ] {
            assert_eq!(analyze_query(q).intent, QueryIntent::HowTo, "{q:?}");
        }
    }

    #[test]
    fn biographical_framing_without_who_is_is_person() {
        for q in [
            "Linus Torvalds contributions to open source",
            "Alan Turing achievements",
            "biography of Marie Curie",
            "who is Jensen Huang",
        ] {
            assert_eq!(analyze_query(q).intent, QueryIntent::Person, "{q:?}");
        }
    }

    #[test]
    fn year_anchored_queries_are_temporal() {
        for q in [
            "2026 FIFA World Cup schedule",
            "upcoming solar eclipse 2026",
            "who won the 2025 NBA Finals",
        ] {
            assert_eq!(analyze_query(q).intent, QueryIntent::Temporal, "{q:?}");
        }
    }

    #[test]
    fn stable_reference_facts_stay_factual() {
        // "population of Japan" is a slowly-changing reference fact. Marking
        // it temporal would append a spurious year to the search query.
        for q in [
            "population of Japan",
            "capital of France",
            "boiling point of water",
        ] {
            assert_eq!(analyze_query(q).intent, QueryIntent::Factual, "{q:?}");
        }
    }

    #[test]
    fn empty_query_yields_nothing() {
        let intel = analyze_query("");
        assert!(decompose_query("", &intel).is_empty());
        assert!(decompose_query("   ", &intel).is_empty());
    }

    #[test]
    fn clause_boundaries_and_contractions_never_form_fake_entities() {
        let q = "Wait, what is the latest update on Claude Cowork? Specifically, I’m asking about features.";
        let intel = analyze_query(q);
        assert!(
            intel.key_entities.iter().any(|e| e == "Claude Cowork"),
            "expected 'Claude Cowork' in {:?}",
            intel.key_entities
        );
        assert!(
            !intel
                .key_entities
                .iter()
                .any(|e| e.contains("Specifically") || e.contains("Wait")),
            "conversational clause starters leaked into entities: {:?}",
            intel.key_entities
        );
    }

    #[test]
    fn lowercase_and_non_latin_queries_extract_subject_entities() {
        let lower_intel = analyze_query("what is the dhruv rathi latest video");
        assert!(
            !lower_intel.key_entities.is_empty() && lower_intel.key_entities[0].contains("dhruv"),
            "lowercase query should still extract subject entity: {:?}",
            lower_intel.key_entities
        );

        let hindi_intel = analyze_query("केप्लर टेलिस्कोप की प्रमुख खोजें");
        assert!(
            !hindi_intel.key_entities.is_empty(),
            "non-Latin script query should extract script entities: {:?}",
            hindi_intel.key_entities
        );
    }
}