crw-server 0.14.0

Firecrawl-compatible API server for the CRW web scraper
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
use axum::Json;
use axum::extract::State;
use axum::extract::rejection::JsonRejection;
use crw_core::Deadline;
use crw_core::config::LlmConfig;
use crw_core::error::CrwError;
use crw_core::types::{
    ApiResponse, LlmUsage, OutputFormat, ScrapeData, ScrapeRequest, SearchData, SearchRequest,
    SearchResponse, SearchResponseData, SearchResult, SearchScrapeOptions,
};
use crw_crawl::single::scrape_url;
use crw_extract::answer;
use crw_extract::summary;
use crw_search::{
    SearchError, SearxngClient, SearxngParams, SearxngResponse, map_to_searxng_params,
    transform_flat, transform_flat_reranked, transform_grouped,
};
use futures::stream::{self, StreamExt};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::task::JoinSet;

const DEFAULT_ANSWER_TOP_N: u32 = 5;
/// Default top-N for the calibrated answer path (feeds more sources so the
/// answer in result #6-8, or behind a failed top-5 scrape, still reaches the
/// model). Bounded by `MAX_ANSWER_TOP_N`.
const CALIBRATED_ANSWER_TOP_N: u32 = 8;
const MAX_ANSWER_TOP_N: u32 = 10;
/// Upper bound on query-expansion rewrites fetched + unioned per request, so a
/// request-supplied `query_expand_variants` can't fan out unbounded SearXNG
/// fetches. The extra pools are fetched concurrently in `fetch_expanded`.
const MAX_QUERY_EXPAND_VARIANTS: usize = 5;
/// Adaptive multi-round: max follow-up queries the evidence-scout issues in the
/// extra round, and how many results each contributes to the scrape pool.
/// Bounded so the single extra round stays well within the request deadline.
const MAX_SCOUT_QUERIES: usize = 2;
const SCOUT_FETCH_LIMIT: u32 = 6;
/// Minimum request-deadline budget that must remain before the adaptive
/// multi-round scout is allowed to start its extra round (scout LLM + up to
/// `MAX_SCOUT_QUERIES` fetches + scrapes + one re-synthesis). Below this, an
/// abstaining query returns round-1 immediately instead of risking a deadline
/// overrun (504) — this is what bounds the worst-case latency that enabling
/// `multi_round` adds.
const MULTI_ROUND_MIN_BUDGET_MS: u64 = 20_000;

/// Heuristic: did the synthesized answer ABSTAIN (sources lacked the fact)?
/// Aligned with `answer.rs`'s calibrated clause ("ONLY if the sources genuinely
/// do not contain the information, say so plainly"). Triggers the adaptive
/// multi-round scout. Conservative — only well-known abstention phrasings.
fn is_abstention(answer: &str) -> bool {
    let a = answer.to_lowercase();
    const MARKERS: &[&str] = &[
        "do not contain",
        "does not contain",
        "doesn't contain",
        "cannot answer",
        "can't answer",
        "cannot determine",
        "could not find",
        "couldn't find",
        "no information",
        "do not provide",
        "does not provide",
        "not mentioned in",
        "not specified",
        "unable to answer",
        "cannot be answered",
        "sources do not",
        "i cannot",
    ];
    MARKERS.iter().any(|m| a.contains(m))
}

/// Build a short evidence excerpt from the current candidate pool to brief the
/// evidence-scout (title + a markdown/snippet head per source, bounded).
fn evidence_excerpt(data: &SearchData, max_sources: usize, per_chars: usize) -> String {
    let pool: &Vec<SearchResult> = match data {
        SearchData::Flat(v) => v,
        SearchData::Grouped(g) => match g.web.as_ref() {
            Some(v) => v,
            None => return String::new(),
        },
    };
    let mut out = String::new();
    for r in pool.iter().take(max_sources) {
        let body = r.markdown.as_deref().unwrap_or(r.description.as_str());
        let snip: String = body.chars().take(per_chars).collect();
        out.push_str("- ");
        out.push_str(&r.title);
        out.push_str(" :: ");
        out.push_str(snip.trim());
        out.push('\n');
    }
    out
}

/// Merge freshly-scraped scout rows into the flat answer pool (dedup by URL,
/// only rows that actually carry markdown). Returns true if any were added.
/// Grouped data (the explicit-`sources` path) is left untouched — multi-round
/// targets the flat answer path. Recall-only: never removes existing sources.
fn merge_scraped(data: &mut SearchData, rows: Vec<SearchResult>) -> bool {
    if let SearchData::Flat(pool) = data {
        let mut seen: std::collections::HashSet<String> =
            pool.iter().map(|r| r.url.clone()).collect();
        let mut added = false;
        for r in rows {
            if r.markdown.is_some() && seen.insert(r.url.clone()) {
                pool.push(r);
                added = true;
            }
        }
        added
    } else {
        false
    }
}
const DEFAULT_MAX_CHARS_PER_SOURCE: usize = 8192;

/// Wave 4 (R2): hard cap on `max_tokens` per LLM leg (one summary call OR
/// the answer call). Independent of the user's configured `cfg.max_tokens`
/// because the SaaS-side `estimateMaxCreditCostForSearch` uses this number
/// to pre-reserve credits; a per-leg cap higher than this would let real
/// usage exceed the reservation. Mirror in
/// `crw-saas/src/lib/llm-pricing.ts::legCost` (default 1024).
const SEARCH_LLM_MAX_TOKENS_PER_LEG: u32 = 1024;

use crate::error::AppError;
use crate::state::AppState;

const MAX_QUERY_CHARS: usize = 2000;

/// `POST /v1/search` — search the web via SearXNG, optionally enriching
/// each `web` result by running it through the scrape pipeline in-process.
///
/// Mirrors the public contract exposed by `crw-saas/src/app/api/v1/search/route.ts`
/// (minus the credit / quota wrapper, which lives in the SaaS layer).
pub async fn search(
    State(state): State<AppState>,
    body: Result<Json<SearchRequest>, JsonRejection>,
) -> Result<Json<SearchResponse>, AppError> {
    let Json(req) = body.map_err(AppError::from)?;
    let resp = search_inner(&state, req).await?;
    Ok(Json(resp))
}

/// Shared search logic used by both the HTTP route and the MCP tool dispatcher.
/// Returns the public `SearchResponse` envelope (with `.warning` populated when
/// scrape enrichment partially fails) or a `CrwError` on hard failure.
pub async fn search_inner(
    state: &AppState,
    req: SearchRequest,
) -> Result<SearchResponse, CrwError> {
    validate_request(&req, state.config.search.max_limit)?;

    let client = state
        .searxng
        .as_ref()
        .ok_or_else(|| {
            CrwError::SearchDisabled(
                "Search is disabled. Set [search].searxng_url in config or define \
                 CRW_SEARCH__SEARXNG_URL to point at a SearXNG instance."
                    .into(),
            )
        })?
        .clone();

    let limit = req
        .limit
        .unwrap_or(state.config.search.default_limit)
        .min(state.config.search.max_limit)
        .max(1);

    // Request-deadline clock, started at handler entry. Used by the adaptive
    // multi-round gate (below) to decide whether enough budget remains to run a
    // second scout round without risking a 504.
    let req_deadline = Deadline::from_request_ms(state.config.effective_deadline_ms(None, None));

    // BYOK + LLM config is built up-front because multi-query expansion (below)
    // needs the LLM *before* the SearXNG fetch. Reused by the summarize/answer
    // legs further down. `llm_path` = this request enters LLM mode.
    let server_llm = state.config.extraction.llm.clone();
    let byok_llm = build_byok_search_llm_config(&req, server_llm.as_ref());
    let effective_llm = byok_llm.as_ref().or(server_llm.as_ref());
    let llm_path = req.answer.unwrap_or(false) || req.summarize_results.unwrap_or(false);

    let params = map_to_searxng_params(&req, &state.config.search);
    // Multi-query expansion (gated): on the LLM path, also fetch an
    // entity/keyword rewrite of the query and UNION the pools so the answer's
    // source is more likely to surface. Falls back to the single fetch.
    let mut response = if state.config.search.query_expand
        && llm_path
        && let Some(llm) = effective_llm
    {
        // Per-request override (eval A/B) wins over the server config; clamp so a
        // hostile caller can't fan out unbounded SearXNG fetches.
        let variants_n = req
            .query_expand_variants
            .unwrap_or(state.config.search.query_expand_variants)
            .clamp(1, MAX_QUERY_EXPAND_VARIANTS);
        fetch_expanded(&client, &req.query, &params, llm, variants_n)
            .await
            .map_err(|e| map_search_error(e, state.config.search.timeout_ms, client.base_url()))?
    } else {
        client
            .fetch(&params)
            .await
            .map_err(|e| map_search_error(e, state.config.search.timeout_ms, client.base_url()))?
    };

    let has_sources = req.sources.as_ref().is_some_and(|s| !s.is_empty());
    // The LLM answer / summarize path feeds the top-N flat sources straight to
    // the model, so it must receive a clean, query-relevant pool. Re-rank the
    // flat pool on that path (unless disabled); the plain path keeps the raw
    // SaaS byte-parity `transform_flat` sort.
    let mut data = if has_sources {
        let sources = req.sources.clone().unwrap_or_default();
        SearchData::Grouped(transform_grouped(&response, &sources, limit))
    } else if llm_path && state.config.search.rerank_enabled {
        SearchData::Flat(transform_flat_reranked(
            &response,
            &req.query,
            limit,
            state.config.search.rerank_relevance,
        ))
    } else {
        SearchData::Flat(transform_flat(&response, limit))
    };

    // W0: parse SearXNG's infoboxes[]/answers[] (Wikidata/Wikipedia structured
    // facts the results[] transform discards) into pinned answer sources. Gated
    // default-off; empty when the flag is off or no structured data was returned.
    let mut structured_sources: Vec<answer::Source> = if state.config.search.use_structured_sources
    {
        crw_search::structured_facts(&response)
            .into_iter()
            .map(|f| {
                let md = f.to_markdown();
                (f.url, f.title, md)
            })
            .collect()
    } else {
        Vec::new()
    };

    // W3: deterministic Wikidata entity-relation lookup (gated, answer path).
    // For `<relation> of <entity>` queries the obscure-entity long tail web
    // search can't surface, resolve the fact via Wikidata and PIN it first.
    // 3s-bounded + cached; any miss/error leaves the normal path untouched.
    if state.config.search.wikidata_lookup
        && llm_path
        && let Some(f) = crw_search::wikidata::lookup(&req.query).await
    {
        let md = f.to_markdown();
        structured_sources.insert(0, (f.url, f.title, md));
    }

    // Page-2 fallback (gated, default-off): when the reranked clean pool came
    // back thinner than the answer needs (junk filter stripped a sparse first
    // page), fetch the SAME query's page 2 ONCE, union it in (dedup by URL like
    // `fetch_expanded`), and re-rank. Trigger is evaluated POST-rerank so a
    // junk-heavy first page doesn't suppress it; extra load only fires on
    // already-under-yielding queries. Recall-only — synthesis/abstention in
    // `answer.rs` is untouched.
    if state.config.search.page2_fallback
        && llm_path
        && state.config.search.rerank_enabled
        && !has_sources
    {
        let top_n = req
            .answer_top_n
            .unwrap_or(DEFAULT_ANSWER_TOP_N)
            .min(MAX_ANSWER_TOP_N) as usize;
        let clean_count = match &data {
            SearchData::Flat(v) => v.len(),
            SearchData::Grouped(_) => top_n,
        };
        if clean_count < top_n {
            let mut p2 = params.clone();
            p2.pageno = Some(2);
            if let Ok(resp2) = client.fetch(&p2).await {
                let mut seen: std::collections::HashSet<String> = response
                    .results
                    .iter()
                    .filter_map(|r| r.url.clone())
                    .collect();
                for row in resp2.results {
                    if let Some(u) = row.url.clone()
                        && seen.insert(u)
                    {
                        response.results.push(row);
                    }
                }
                response.number_of_results = response.results.len() as u64;
                data = SearchData::Flat(transform_flat_reranked(
                    &response,
                    &req.query,
                    limit,
                    state.config.search.rerank_relevance,
                ));
            }
        }
    }

    let mut warning: Option<String> = None;
    let mut warnings: Vec<String> = Vec::new();
    if let Some(opts) = req.scrape_options.as_ref() {
        match enrich_with_scrape(&mut data, opts, state).await {
            Ok(()) => {}
            Err(msg) => {
                tracing::warn!(error = %msg, "scrape enrichment failed");
                warning = Some(msg);
            }
        }
    }

    // `effective_llm` / `byok_llm` / `server_llm` were built up-front (above).
    let wants_summaries = req.summarize_results.unwrap_or(false);
    let wants_answer = req.answer.unwrap_or(false);
    // Wave 4 (R1): once we enter LLM mode the response MUST carry a
    // non-null llmUsage object (the always-present invariant the SaaS
    // 5-branch dispatch relies on). We aggregate summary + answer counts
    // into this builder and emit it at every return path below.
    let mut llm_attempted = false;
    let mut agg_input_tokens: u32 = 0;
    let mut agg_output_tokens: u32 = 0;
    let mut agg_cache_hit: u32 = 0;
    let mut agg_cache_miss: u32 = 0;
    let mut agg_calls: u32 = 0;
    let mut agg_executed_summaries: u32 = 0;
    let mut agg_answer_executed = false;
    let mut agg_provider: String = String::new();
    let mut agg_model: String = String::new();
    let mut agg_truncated = false;
    let merge_usage = |agg_input_tokens: &mut u32,
                       agg_output_tokens: &mut u32,
                       agg_cache_hit: &mut u32,
                       agg_cache_miss: &mut u32,
                       agg_calls: &mut u32,
                       agg_provider: &mut String,
                       agg_model: &mut String,
                       agg_truncated: &mut bool,
                       u: &LlmUsage| {
        *agg_input_tokens = agg_input_tokens.saturating_add(u.input_tokens);
        *agg_output_tokens = agg_output_tokens.saturating_add(u.output_tokens);
        *agg_cache_hit = agg_cache_hit.saturating_add(u.cache_hit_input_tokens.unwrap_or(0));
        *agg_cache_miss = agg_cache_miss.saturating_add(u.cache_miss_input_tokens.unwrap_or(0));
        *agg_calls = agg_calls.saturating_add(u.calls.max(1));
        if agg_provider.is_empty() {
            *agg_provider = u.provider.clone();
        }
        if agg_model.is_empty() {
            *agg_model = u.model.clone();
        }
        *agg_truncated = *agg_truncated || u.truncated;
    };

    if (wants_summaries || wants_answer) && req.scrape_options.is_none() {
        warnings.push(
            "summarizeResults / answer require scrapeOptions to populate markdown; skipped".into(),
        );
    } else if wants_summaries || wants_answer {
        match effective_llm {
            None => warnings.push(
                "summarizeResults / answer require an LLM config (set [extraction.llm] or \
                 pass llm_api_key)"
                    .into(),
            ),
            Some(llm) => {
                llm_attempted = true;
                // Wave 4 (R2): cap max_tokens at SEARCH_LLM_MAX_TOKENS_PER_LEG so
                // a single leg can never exceed the SaaS pre-reservation in
                // estimateMaxCreditCostForSearch.
                let mut leg_cfg = llm.clone();
                leg_cfg.max_tokens = leg_cfg.max_tokens.min(SEARCH_LLM_MAX_TOKENS_PER_LEG);
                // Eval determinism: a request-supplied answer temperature (the
                // benchmark harness sets 0) overrides the provider default so
                // A/B runs are reproducible. None = current prod behavior.
                if req.answer_temperature.is_some() {
                    leg_cfg.temperature = req.answer_temperature;
                }
                if wants_summaries {
                    let (count, usages) = attach_result_summaries(
                        &mut data,
                        &leg_cfg,
                        leg_cfg.max_concurrency,
                        req.summary_prompt.as_deref(),
                        req.max_content_chars,
                    )
                    .await;
                    agg_executed_summaries = count.ok as u32;
                    for u in usages.into_iter().flatten() {
                        merge_usage(
                            &mut agg_input_tokens,
                            &mut agg_output_tokens,
                            &mut agg_cache_hit,
                            &mut agg_cache_miss,
                            &mut agg_calls,
                            &mut agg_provider,
                            &mut agg_model,
                            &mut agg_truncated,
                            &u,
                        );
                    }
                    if count.failed > 0 {
                        warnings.push(format!(
                            "{} of {} per-result summaries failed",
                            count.failed,
                            count.failed + count.ok
                        ));
                    }
                }
                if wants_answer {
                    // List-format answer (gated): a request override wins, else
                    // the server flag; either way it only fires on list-intent
                    // queries ("best/top X in Y"). Factual queries keep prose.
                    let list_format = req
                        .answer_list_format
                        .unwrap_or(state.config.search.answer_list_format)
                        && answer::is_list_intent(&req.query);
                    match synthesize_answer(
                        &req,
                        &data,
                        &leg_cfg,
                        state.config.search.passage_select,
                        state.config.search.answer_calibrated,
                        state.config.search.snippet_fallback,
                        state.config.search.answer_guarded,
                        &structured_sources,
                        list_format,
                    )
                    .await
                    {
                        Ok((mut ans, mut cites, ans_usage, mut ans_warns)) => {
                            warnings.append(&mut ans_warns);
                            agg_answer_executed = true;
                            if let Some(ref u) = ans_usage {
                                merge_usage(
                                    &mut agg_input_tokens,
                                    &mut agg_output_tokens,
                                    &mut agg_cache_hit,
                                    &mut agg_cache_miss,
                                    &mut agg_calls,
                                    &mut agg_provider,
                                    &mut agg_model,
                                    &mut agg_truncated,
                                    u,
                                );
                            }
                            // Adaptive multi-round (gated): if round-1 ABSTAINED,
                            // the evidence-scout issues targeted follow-ups; we
                            // scrape them, union into the pool, and re-synthesize
                            // ONCE. Recall-only — a still-abstaining round-2 is
                            // discarded (keep round-1). Only fires on abstention,
                            // so the single-shot fast path is unchanged for hits.
                            let want_multi =
                                req.multi_round.unwrap_or(state.config.search.multi_round);
                            // Deadline budget: the extra scout round can add tens
                            // of seconds. If too little of the request deadline
                            // remains, skip it and return round-1 promptly rather
                            // than risk a 504 — this caps the worst-case latency
                            // multi_round would otherwise add.
                            let multi_budget_ok = req_deadline.remaining().as_millis() as u64
                                >= MULTI_ROUND_MIN_BUDGET_MS;
                            if want_multi && is_abstention(&ans) && !multi_budget_ok {
                                warnings.push(
                                    "multi-round skipped: insufficient deadline budget remaining"
                                        .to_string(),
                                );
                            }
                            if want_multi
                                && is_abstention(&ans)
                                && multi_budget_ok
                                && let Some(opts) = req.scrape_options.as_ref()
                            {
                                let evidence = evidence_excerpt(&data, 5, 400);
                                let scout_qs = crw_extract::llm::scout_followups(
                                    &leg_cfg,
                                    &req.query,
                                    &evidence,
                                    MAX_SCOUT_QUERIES,
                                )
                                .await;
                                let mut grew = false;
                                for sq in scout_qs {
                                    let mut sp = params.clone();
                                    sp.q = sq;
                                    if let Ok(resp2) = client.fetch(&sp).await {
                                        let extra = transform_flat_reranked(
                                            &resp2,
                                            &req.query,
                                            SCOUT_FETCH_LIMIT,
                                            state.config.search.rerank_relevance,
                                        );
                                        let mut sd = SearchData::Flat(extra);
                                        let _ = enrich_with_scrape(&mut sd, opts, state).await;
                                        if let SearchData::Flat(rows) = sd {
                                            grew |= merge_scraped(&mut data, rows);
                                        }
                                    }
                                }
                                if grew
                                    && let Ok((ans2, cites2, usage2, mut warns2)) =
                                        synthesize_answer(
                                            &req,
                                            &data,
                                            &leg_cfg,
                                            state.config.search.passage_select,
                                            state.config.search.answer_calibrated,
                                            state.config.search.snippet_fallback,
                                            state.config.search.answer_guarded,
                                            &structured_sources,
                                            list_format,
                                        )
                                        .await
                                {
                                    // Round-2 actually called the LLM and consumed
                                    // tokens, so its usage MUST be merged for honest
                                    // accounting even if it abstained — only the
                                    // ANSWER/citations are adopted conditionally
                                    // (when round-2 produced a non-abstaining
                                    // answer). Merging usage before the abstention
                                    // check prevents silently dropping the round-2
                                    // token cost.
                                    if let Some(ref u) = usage2 {
                                        merge_usage(
                                            &mut agg_input_tokens,
                                            &mut agg_output_tokens,
                                            &mut agg_cache_hit,
                                            &mut agg_cache_miss,
                                            &mut agg_calls,
                                            &mut agg_provider,
                                            &mut agg_model,
                                            &mut agg_truncated,
                                            u,
                                        );
                                    }
                                    if !is_abstention(&ans2) {
                                        ans = ans2;
                                        cites = cites2;
                                        warnings.append(&mut warns2);
                                    }
                                }
                            }
                            let aggregated = build_aggregated_usage(
                                agg_input_tokens,
                                agg_output_tokens,
                                agg_cache_hit,
                                agg_cache_miss,
                                agg_calls,
                                agg_executed_summaries,
                                agg_answer_executed,
                                agg_provider.clone(),
                                agg_model.clone(),
                                agg_truncated,
                                &leg_cfg,
                            );
                            let wrapped = SearchResponseData {
                                results: data,
                                answer: Some(ans),
                                citations: cites,
                                llm_usage: Some(aggregated),
                                warnings,
                            };
                            let mut resp = ApiResponse::ok(wrapped);
                            resp.warning = warning;
                            return Ok(resp);
                        }
                        Err(msg) => {
                            tracing::warn!(error = %msg, "answer synthesis failed");
                            warnings.push(format!("answer synthesis failed: {msg}"));
                        }
                    }
                }
            }
        }
    }

    // R1 always-present invariant: if we attempted LLM work, emit the
    // aggregated usage even when zero tokens were consumed (e.g. all
    // summaries failed and no answer leg ran). The SaaS dispatch maps
    // (executedSummaries == 0 && answerExecuted == false && tokens == 0)
    // to Branch 1 (no-op refund); anything else routes correctly.
    let final_usage = if llm_attempted {
        Some(build_aggregated_usage(
            agg_input_tokens,
            agg_output_tokens,
            agg_cache_hit,
            agg_cache_miss,
            agg_calls,
            agg_executed_summaries,
            agg_answer_executed,
            if agg_provider.is_empty() {
                effective_llm
                    .map(|c| c.provider.clone())
                    .unwrap_or_default()
            } else {
                agg_provider
            },
            if agg_model.is_empty() {
                effective_llm.map(|c| c.model.clone()).unwrap_or_default()
            } else {
                agg_model
            },
            agg_truncated,
            effective_llm
                .map(|c| {
                    let mut c = c.clone();
                    c.max_tokens = c.max_tokens.min(SEARCH_LLM_MAX_TOKENS_PER_LEG);
                    c
                })
                .as_ref()
                .unwrap_or(&crw_core::config::LlmConfig::default()),
        ))
    } else {
        None
    };

    let wrapped = SearchResponseData {
        results: data,
        answer: None,
        citations: Vec::new(),
        llm_usage: final_usage,
        warnings,
    };
    let mut resp = ApiResponse::ok(wrapped);
    resp.warning = warning;
    Ok(resp)
}

#[allow(clippy::too_many_arguments)]
fn build_aggregated_usage(
    input_tokens: u32,
    output_tokens: u32,
    cache_hit: u32,
    cache_miss: u32,
    calls: u32,
    executed_summaries: u32,
    answer_executed: bool,
    provider: String,
    model: String,
    truncated: bool,
    fallback_cfg: &crw_core::config::LlmConfig,
) -> LlmUsage {
    LlmUsage {
        input_tokens,
        output_tokens,
        total_tokens: input_tokens.saturating_add(output_tokens),
        estimated_cost_usd: None,
        model: if model.is_empty() {
            fallback_cfg.model.clone()
        } else {
            model
        },
        provider: if provider.is_empty() {
            fallback_cfg.provider.clone()
        } else {
            provider
        },
        cache_hit_input_tokens: if cache_hit == 0 {
            None
        } else {
            Some(cache_hit)
        },
        cache_miss_input_tokens: if cache_miss == 0 {
            None
        } else {
            Some(cache_miss)
        },
        truncated,
        calls: calls.max(1),
        executed_summaries,
        answer_executed,
    }
}

#[derive(Default)]
struct SummaryFanoutCount {
    ok: usize,
    failed: usize,
}

/// Fan-out summary calls across all results that have markdown. Bounded by
/// `max_concurrency`. Pattern mirrors `crates/crw-crawl/src/sitemap.rs`.
///
/// Wave 4 (R1): returns the per-call `Option<LlmUsage>` for every job
/// alongside the ok/failed count so the caller can aggregate token totals
/// across summaries + answer.
async fn attach_result_summaries(
    data: &mut SearchData,
    cfg: &LlmConfig,
    max_concurrency: usize,
    user_prompt: Option<&str>,
    max_content_chars: Option<usize>,
) -> (SummaryFanoutCount, Vec<Option<LlmUsage>>) {
    let targets: &mut Vec<SearchResult> = match data {
        SearchData::Flat(v) => v,
        SearchData::Grouped(g) => match g.web.as_mut() {
            Some(v) if !v.is_empty() => v,
            _ => return (SummaryFanoutCount::default(), Vec::new()),
        },
    };
    // Capture markdown + index pairs first so we don't hold a borrow of
    // `targets` across the async fan-out.
    let jobs: Vec<(usize, String)> = targets
        .iter()
        .enumerate()
        .filter_map(|(idx, r)| r.markdown.as_ref().map(|md| (idx, md.clone())))
        .collect();
    if jobs.is_empty() {
        return (SummaryFanoutCount::default(), Vec::new());
    }
    let cfg_owned = cfg.clone();
    let user_prompt_owned: Option<String> = user_prompt.map(str::to_owned);
    let concurrency = max_concurrency.max(1);
    type SummaryOutcome = (usize, Result<(String, Option<LlmUsage>), String>);
    let results: Vec<SummaryOutcome> = stream::iter(jobs)
        .map(|(idx, md)| {
            let cfg = cfg_owned.clone();
            let user_prompt = user_prompt_owned.clone();
            async move {
                let outcome =
                    summary::summarize(&md, &cfg, user_prompt.as_deref(), max_content_chars)
                        .await
                        .map(|r| (r.content, r.usage))
                        .map_err(|e| e.to_string());
                (idx, outcome)
            }
        })
        .buffer_unordered(concurrency)
        .collect()
        .await;

    let mut count = SummaryFanoutCount::default();
    let mut usages: Vec<Option<LlmUsage>> = Vec::with_capacity(results.len());
    for (idx, res) in results {
        match res {
            Ok((text, usage)) => {
                if let Some(slot) = targets.get_mut(idx) {
                    slot.summary = Some(text);
                    count.ok += 1;
                    usages.push(usage);
                }
            }
            Err(_) => {
                count.failed += 1;
                usages.push(None);
            }
        }
    }
    (count, usages)
}

#[allow(clippy::too_many_arguments)]
async fn synthesize_answer(
    req: &SearchRequest,
    data: &SearchData,
    cfg: &LlmConfig,
    passage_select: bool,
    calibrated: bool,
    snippet_fallback: bool,
    guarded: bool,
    // W0: structured facts (infoboxes/answers) to PIN at the front of the pool.
    structured: &[answer::Source],
    list_format: bool,
) -> Result<
    (
        String,
        Vec<crw_core::types::Citation>,
        Option<crw_core::types::LlmUsage>,
        Vec<String>,
    ),
    String,
> {
    // Calibrated answer feeds more sources by default (Pattern A: the answer
    // often sits in result #6-8, or a failed top-5 scrape thinned the pool) and
    // uses the anti-hedge prompt clause. An explicit request `answer_top_n`
    // still wins. Capped by MAX_ANSWER_TOP_N.
    let default_top_n = if calibrated {
        CALIBRATED_ANSWER_TOP_N
    } else {
        DEFAULT_ANSWER_TOP_N
    };
    let top_n = req
        .answer_top_n
        .unwrap_or(default_top_n)
        .min(MAX_ANSWER_TOP_N) as usize;
    let cap = req
        .max_chars_per_source
        .unwrap_or(DEFAULT_MAX_CHARS_PER_SOURCE)
        .min(answer::MAX_CHARS_PER_SOURCE_CEILING);

    let pool: &Vec<SearchResult> = match data {
        SearchData::Flat(v) => v,
        SearchData::Grouped(g) => match g.web.as_ref() {
            Some(v) => v,
            None => return Err("no web results to synthesize from".into()),
        },
    };
    let scraped: Vec<answer::Source> = pool
        .iter()
        .filter_map(|r| {
            if let Some(md) = r.markdown.as_ref() {
                Some((r.url.clone(), r.title.clone(), md.clone()))
            } else if snippet_fallback {
                // Scrape failed/empty — instead of dropping the result (which
                // can lose the answer-bearing page, Pattern A), fall back to the
                // SearXNG snippet. It's verbatim upstream text, so it can only
                // surface a fact already present, never invent one.
                let desc = r.description.trim();
                if desc.is_empty() {
                    None
                } else {
                    Some((r.url.clone(), r.title.clone(), format!("[snippet] {desc}")))
                }
            } else {
                None
            }
        })
        .take(top_n)
        .collect();
    // W0: PIN structured facts (Wikidata/Wikipedia infobox/answers) at the front
    // so the synthesizer sees them first. They are still UNTRUSTED-wrapped by
    // `answer::synthesize` — this widens evidence, it does not bypass safety.
    let sources: Vec<answer::Source> = if structured.is_empty() {
        scraped
    } else {
        structured.iter().cloned().chain(scraped).collect()
    };
    if sources.is_empty() {
        return Err("no results carry markdown to synthesize an answer from".into());
    }
    // Passage-select reduces each large source to its query-relevant passages
    // before synthesis (monotone-safe: falls back to the full source on any
    // failure). Gated; off = byte-identical to plain synthesize.
    let result = if passage_select {
        answer::synthesize_selected(
            &req.query,
            &sources,
            cfg,
            cap,
            req.answer_prompt.as_deref(),
            calibrated,
            guarded,
            list_format,
        )
        .await
    } else {
        answer::synthesize(
            &req.query,
            &sources,
            cfg,
            cap,
            req.answer_prompt.as_deref(),
            calibrated,
            guarded,
            list_format,
        )
        .await
    }
    .map_err(|e| e.to_string())?;
    Ok((
        result.content,
        result.citations,
        result.usage,
        result.warnings,
    ))
}

fn build_byok_search_llm_config(
    req: &SearchRequest,
    server_cfg: Option<&LlmConfig>,
) -> Option<LlmConfig> {
    let api_key = req.llm_api_key.as_ref()?.clone();
    let mut cfg = match server_cfg {
        Some(s) => s.clone(),
        None => LlmConfig::default(),
    };
    cfg.api_key = api_key;
    if let Some(p) = &req.llm_provider {
        cfg.provider = p.clone();
    }
    if let Some(m) = &req.llm_model {
        cfg.model = m.clone();
    }
    if let Some(b) = &req.base_url {
        cfg.base_url = Some(b.clone());
    }
    Some(cfg)
}

fn validate_request(req: &SearchRequest, max_limit: u32) -> Result<(), CrwError> {
    let len = req.query.chars().count();
    if len == 0 {
        return Err(CrwError::InvalidRequest("query is required".into()));
    }
    if len > MAX_QUERY_CHARS {
        return Err(CrwError::InvalidRequest(format!(
            "query length {len} exceeds maximum of {MAX_QUERY_CHARS} characters"
        )));
    }
    if let Some(l) = req.limit
        && (l == 0 || l > max_limit)
    {
        return Err(CrwError::InvalidRequest(format!(
            "limit must be between 1 and {max_limit} (got {l})"
        )));
    }
    if let Some(cats) = &req.categories
        && cats.len() > 5
    {
        return Err(CrwError::InvalidRequest(
            "categories accepts at most 5 entries".into(),
        ));
    }
    if let Some(opts) = req.scrape_options.as_ref() {
        // Search enrichment can only carry formats that fit the
        // `SearchResult` shape. `plainText` and `json` (LLM extract) require
        // fields the search-result envelope doesn't expose; rejecting up-front
        // is clearer than silently dropping them post-scrape.
        for f in &opts.formats {
            if matches!(f, OutputFormat::PlainText | OutputFormat::Json) {
                return Err(CrwError::InvalidRequest(format!(
                    "scrapeOptions.formats does not support {f:?} on /v1/search; use \
                     /v1/scrape for plainText/json (extract). Allowed: markdown, html, \
                     rawHtml, links."
                )));
            }
        }
    }
    Ok(())
}

/// Map a transport/timeout/upstream `SearchError` onto the HTTP `CrwError`.
/// `base_url` is the configured SearXNG URL; the transport (`target_unreachable`)
/// arm names its **origin** (issue #90) so the operator sees *which* host failed
/// — sanitized, so a credentialed URL never reaches the response. Timeouts keep
/// `error_code: "timeout"`; the host is correlated via the startup log instead.
fn map_search_error(err: SearchError, timeout_ms: u64, base_url: &str) -> CrwError {
    match err {
        SearchError::Timeout => CrwError::Timeout(timeout_ms),
        SearchError::Upstream { status, body } => CrwError::HttpError(format!(
            "SearXNG returned HTTP {status}: {}",
            body.chars().take(200).collect::<String>()
        )),
        SearchError::InvalidResponse(msg) => {
            CrwError::HttpError(format!("SearXNG returned invalid JSON: {msg}"))
        }
        SearchError::Transport(msg) => CrwError::TargetUnreachable(format!(
            "SearXNG ({}): {msg}",
            crate::diagnostics::sanitize_url_origin(base_url)
        )),
    }
}

/// Multi-query expansion: fetch the original query plus an LLM-generated
/// entity/keyword rewrite, then UNION the candidate pools (dedupe by URL,
/// original results kept first so they retain priority). Recall can only
/// increase vs a single fetch. The original fetch's error propagates (same
/// failure semantics as the single-fetch path); a failed variant fetch is
/// ignored. If the rewrite is empty/trivial, this is exactly the single fetch.
async fn fetch_expanded(
    client: &SearxngClient,
    query: &str,
    base_params: &SearxngParams,
    llm: &LlmConfig,
    max_variants: usize,
) -> Result<SearxngResponse, SearchError> {
    let mut leg = llm.clone();
    leg.max_tokens = leg.max_tokens.min(SEARCH_LLM_MAX_TOKENS_PER_LEG);
    let variants = crw_extract::llm::expand_query(&leg, query, max_variants).await;
    let mut merged = client.fetch(base_params).await?;
    if variants.is_empty() {
        return Ok(merged);
    }
    // Fetch all variant pools concurrently (bounded by the variant count) so N
    // rewrites cost ~one extra fetch of wall-clock, not N sequential ones, then
    // union by URL. A failed variant fetch is dropped (recall-only, never fatal).
    let variant_responses: Vec<SearxngResponse> = stream::iter(variants)
        .map(|v| {
            let client = client.clone();
            let mut vp = base_params.clone();
            vp.q = v;
            async move { client.fetch(&vp).await.ok() }
        })
        .buffer_unordered(max_variants.max(1))
        .filter_map(|r| async move { r })
        .collect()
        .await;
    let mut seen: std::collections::HashSet<String> = merged
        .results
        .iter()
        .filter_map(|r| r.url.clone())
        .collect();
    for resp in variant_responses {
        for row in resp.results {
            if let Some(u) = row.url.clone()
                && seen.insert(u)
            {
                merged.results.push(row);
            }
        }
    }
    merged.number_of_results = merged.results.len() as u64;
    Ok(merged)
}

/// Enrich `web` (or flat) results in-place by calling the scrape pipeline
/// for each result URL. Bounded by `[crawler].max_concurrency`. On per-URL
/// failure the result is left without `markdown`/`html`/etc. fields — the
/// search response still succeeds.
async fn enrich_with_scrape(
    data: &mut SearchData,
    opts: &SearchScrapeOptions,
    state: &AppState,
) -> Result<(), String> {
    let targets: &mut Vec<SearchResult> = match data {
        SearchData::Flat(v) => v,
        SearchData::Grouped(g) => match g.web.as_mut() {
            Some(v) if !v.is_empty() => v,
            _ => return Ok(()), // nothing to enrich
        },
    };
    if targets.is_empty() {
        return Ok(());
    }

    // Validate each URL and remember which slot it came from.
    let mut jobs: Vec<(usize, String)> = Vec::new();
    for (idx, r) in targets.iter().enumerate() {
        let parsed = match url::Url::parse(&r.url) {
            Ok(u) => u,
            Err(_) => continue,
        };
        if crw_core::url_safety::validate_safe_url_resolved(&parsed)
            .await
            .is_err()
        {
            continue;
        }
        jobs.push((idx, r.url.clone()));
    }
    if jobs.is_empty() {
        return Ok(());
    }

    let formats = opts.formats.clone();
    let only_main = opts.only_main_content;
    let semaphore = Arc::new(tokio::sync::Semaphore::new(
        state.config.crawler.max_concurrency.max(1),
    ));
    let mut set: JoinSet<(usize, Result<ScrapeData, String>)> = JoinSet::new();

    for (idx, url) in jobs {
        let formats = formats.clone();
        let renderer = state.renderer.clone();
        let llm_config = state.config.extraction.llm.clone();
        let extraction_cfg = state.config.extraction.clone();
        let user_agent = state.config.crawler.user_agent.clone();
        let default_stealth =
            state.config.crawler.stealth.enabled && state.config.crawler.stealth.inject_headers;
        let render_js_default = state.config.renderer.render_js_default;
        let deadline_ms = state.config.effective_deadline_ms(None, None);
        let permit_src = semaphore.clone();

        set.spawn(async move {
            let _permit = match permit_src.acquire_owned().await {
                Ok(p) => p,
                Err(e) => return (idx, Err(format!("semaphore closed: {e}"))),
            };
            let scrape_req = ScrapeRequest {
                url: url.clone(),
                formats,
                only_main_content: only_main,
                render_js: None,
                wait_for: None,
                include_tags: vec![],
                exclude_tags: vec![],
                json_schema: None,
                headers: HashMap::new(),
                css_selector: None,
                xpath: None,
                chunk_strategy: None,
                query: None,
                filter_mode: None,
                top_k: None,
                proxy: None,
                country: None,
                stealth: None,
                actions: None,
                extract: None,
                llm_api_key: None,
                llm_provider: None,
                llm_model: None,
                base_url: None,
                summary_prompt: None,
                max_content_chars: None,
                renderer: None,
                deadline_ms: Some(deadline_ms),
                debug: None,
                change_tracking: None,
                goal: None,
                judge_enabled: None,
                parsers: None,
            };
            let deadline = Deadline::from_request_ms(deadline_ms);
            let result = scrape_url(
                &scrape_req,
                &renderer,
                llm_config.as_ref(),
                &extraction_cfg,
                &user_agent,
                default_stealth,
                render_js_default,
                deadline,
            )
            .await
            .map_err(|e| e.to_string());
            (idx, result)
        });
    }

    while let Some(joined) = set.join_next().await {
        let (idx, result) = match joined {
            Ok(pair) => pair,
            Err(join_err) => {
                tracing::warn!(error = %join_err, "scrape enrichment task panicked");
                continue;
            }
        };
        let Some(slot) = targets.get_mut(idx) else {
            continue;
        };
        match result {
            Ok(scrape) => apply_scrape_to_result(slot, scrape, &opts.formats),
            Err(msg) => {
                tracing::debug!(url = %slot.url, error = %msg, "scrape enrichment skipped");
            }
        }
    }
    Ok(())
}

fn apply_scrape_to_result(slot: &mut SearchResult, data: ScrapeData, formats: &[OutputFormat]) {
    if formats.contains(&OutputFormat::Markdown) {
        slot.markdown = data.markdown;
    }
    if formats.contains(&OutputFormat::Html) {
        slot.html = data.html;
    }
    if formats.contains(&OutputFormat::RawHtml) {
        slot.raw_html = data.raw_html;
    }
    if formats.contains(&OutputFormat::Links) {
        slot.links = data.links;
    }
    slot.metadata = Some(data.metadata);
}

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

    fn req(q: &str) -> SearchRequest {
        SearchRequest {
            query: q.into(),
            limit: None,
            lang: None,
            tbs: None,
            sources: None,
            categories: None,
            scrape_options: None,
            summarize_results: None,
            answer: None,
            answer_top_n: None,
            max_chars_per_source: None,
            llm_api_key: None,
            llm_provider: None,
            llm_model: None,
            base_url: None,
            summary_prompt: None,
            answer_prompt: None,
            answer_temperature: None,
            query_expand_variants: None,
            multi_round: None,
            answer_list_format: None,
            max_content_chars: None,
        }
    }

    #[test]
    fn is_abstention_detects_marker_phrases() {
        assert!(is_abstention(
            "The sources do not contain this information."
        ));
        assert!(is_abstention("I cannot answer that from the sources."));
        assert!(is_abstention("That is not mentioned in the sources."));
        assert!(is_abstention("The provided sources do not provide a year."));
        assert!(is_abstention("I could not find the answer."));
        assert!(is_abstention("The date is not specified anywhere."));
    }

    #[test]
    fn is_abstention_false_for_normal_answer() {
        assert!(!is_abstention("The capital of Serbia is Belgrade."));
        assert!(!is_abstention("Rust was first released publicly in 2010."));
    }

    #[test]
    fn validate_rejects_empty_query() {
        assert!(matches!(
            validate_request(&req(""), 20),
            Err(CrwError::InvalidRequest(_))
        ));
    }

    #[test]
    fn validate_rejects_oversized_query() {
        let q = "x".repeat(MAX_QUERY_CHARS + 1);
        assert!(matches!(
            validate_request(&req(&q), 20),
            Err(CrwError::InvalidRequest(_))
        ));
    }

    #[test]
    fn validate_rejects_limit_above_max() {
        let mut r = req("rust");
        r.limit = Some(50);
        assert!(matches!(
            validate_request(&r, 20),
            Err(CrwError::InvalidRequest(_))
        ));
    }

    #[test]
    fn validate_rejects_zero_limit() {
        let mut r = req("rust");
        r.limit = Some(0);
        assert!(matches!(
            validate_request(&r, 20),
            Err(CrwError::InvalidRequest(_))
        ));
    }

    #[test]
    fn validate_accepts_basic_request() {
        assert!(validate_request(&req("rust async"), 20).is_ok());
    }

    #[test]
    fn map_search_error_timeout_to_timeout() {
        assert!(matches!(
            map_search_error(SearchError::Timeout, 7500, "http://searxng:8080"),
            CrwError::Timeout(7500)
        ));
    }

    #[test]
    fn map_search_error_upstream_to_http_error() {
        let err = SearchError::Upstream {
            status: 503,
            body: "down".into(),
        };
        assert!(matches!(
            map_search_error(err, 5000, "http://searxng:8080"),
            CrwError::HttpError(_)
        ));
    }

    #[test]
    fn map_search_error_transport_names_sanitized_host() {
        // issue #90: the unreachable error must name the configured host so the
        // operator knows *what* failed — but origin-only, never the raw URL.
        let err = SearchError::Transport("dns error: failed to lookup address".into());
        let mapped = map_search_error(err, 5000, "https://user:pass@searxng:8080/tok?k=v");
        match mapped {
            CrwError::TargetUnreachable(msg) => {
                assert!(msg.contains("https://searxng:8080"), "{msg}");
                assert!(!msg.contains("user"), "must not leak userinfo: {msg}");
                assert!(!msg.contains("pass"), "must not leak credentials: {msg}");
                assert!(!msg.contains("tok"), "must not leak path token: {msg}");
            }
            other => panic!("expected TargetUnreachable, got {other:?}"),
        }
    }

    #[test]
    fn _suppress_unused_search_source_warning() {
        let _ = SearchSource::Web;
    }
}