recursive-agent 0.8.3

A Rust coding-agent platform: kernel, tools, multi-agent orchestration, HTTP API, MCP, and TUI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
//! `WebSearch`: search the web using a configurable provider.
//!
//! Configuration (via environment variables):
//!   `RECURSIVE_WEB_SEARCH_PROVIDER` — one of `brave`, `tavily`, `serper`, `bocha`, `bing`.
//!   `RECURSIVE_WEB_SEARCH_API_KEY`  — API key for the chosen provider.
//!
//! When no provider/API key is configured, the tool falls back in order:
//!   1. DuckDuckGo HTML scrape (zero-config)
//!   2. Bing HTML scrape (if DDG is challenged / empty)
//!   3. Jina AI Search (`s.jina.ai`, optional `RECURSIVE_WEB_SEARCH_JINA_KEY`)
//!
//! Result format (lightweight): numbered list of `title / url / snippet` entries.

use async_trait::async_trait;
use base64::{engine::general_purpose, Engine as _};
use regex::Regex;
use reqwest::Client;
use serde_json::{json, Value};
use std::sync::OnceLock;
use std::time::Duration;

use super::Tool;
use crate::acp::ToolKind;
use crate::error::{Error, Result};
use crate::llm::ToolSpec;

const REQUEST_TIMEOUT_SECS: u64 = 15;
const CONNECT_TIMEOUT_SECS: u64 = 5;
const DEFAULT_NUM_RESULTS: u64 = 5;
const MAX_NUM_RESULTS: u64 = 10;

// Default base URLs — overridable in tests via `WebSearch::with_test_base`.
const BRAVE_BASE: &str = "https://api.search.brave.com";
const TAVILY_BASE: &str = "https://api.tavily.com";
const SERPER_BASE: &str = "https://google.serper.dev";
const BOCHA_BASE: &str = "https://api.bochaai.com";
const BING_BASE: &str = "https://api.bing.microsoft.com";
const DUCKDUCKGO_HTML_BASE: &str = "https://html.duckduckgo.com";
const BING_HTML_BASE: &str = "https://www.bing.com";
/// Browser-like UA for HTML SERP scraping (bot-detection sensitive endpoints).
const BROWSER_USER_AGENT: &str = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15";

/// Supported search providers.
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum Provider {
    Brave,
    Tavily,
    Serper,
    Bocha,
    Bing,
}

impl Provider {
    /// Case-insensitive name lookup for a known provider.
    /// `pub(crate)` so `config.rs` can validate `web_search_provider` at
    /// config time without duplicating the valid set (the crate-internal
    /// visibility keeps clippy's `should_implement_trait` from suggesting a
    /// `FromStr` impl — this is a lookup, not a fallible parse).
    pub(crate) fn from_str(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "brave" => Some(Self::Brave),
            "tavily" => Some(Self::Tavily),
            "serper" => Some(Self::Serper),
            "bocha" => Some(Self::Bocha),
            "bing" => Some(Self::Bing),
            _ => None,
        }
    }
}

#[derive(Debug, Clone)]
pub struct WebSearch {
    client: Client,
    /// When set (tests only), replaces the provider's real base URL.
    /// A single override is sufficient because integration tests run one
    /// provider at a time against a mockito server.
    test_base_url: Option<String>,
    /// Optional search provider name override (from Config).
    /// Takes precedence over `RECURSIVE_WEB_SEARCH_PROVIDER` env var.
    provider_override: Option<String>,
    /// Optional search API key override (from Config).
    /// Takes precedence over `RECURSIVE_WEB_SEARCH_API_KEY` env var.
    api_key_override: Option<String>,
    /// Optional Jina API key override (from Config).
    /// Takes precedence over `RECURSIVE_WEB_SEARCH_JINA_KEY` env var.
    jina_key_override: Option<String>,
}

impl Default for WebSearch {
    fn default() -> Self {
        Self::new()
    }
}

impl WebSearch {
    pub fn new() -> Self {
        let client = Client::builder()
            .timeout(Duration::from_secs(REQUEST_TIMEOUT_SECS))
            .connect_timeout(Duration::from_secs(CONNECT_TIMEOUT_SECS))
            .user_agent(format!("recursive-agent/{}", env!("CARGO_PKG_VERSION")))
            // Construction carve-out: if TLS backend fails to initialize, the
            // process cannot perform HTTP requests at all. This is a fatal
            // startup condition equivalent to the providers.rs TOML parse
            // (Invariant #5 §construction).
            .build();
        #[allow(
            clippy::expect_used,
            reason = "TLS backend unavailable is a fatal startup error"
        )]
        let client = client.expect("reqwest client build: TLS backend unavailable");
        Self {
            client,
            test_base_url: None,
            provider_override: None,
            api_key_override: None,
            jina_key_override: None,
        }
    }

    /// Configure search provider and API keys from Config values.
    /// When set, these take precedence over `RECURSIVE_WEB_SEARCH_*` env vars.
    pub fn with_search_config(
        mut self,
        provider: Option<String>,
        api_key: Option<String>,
        jina_key: Option<String>,
    ) -> Self {
        self.provider_override = provider;
        self.api_key_override = api_key;
        self.jina_key_override = jina_key;
        self
    }

    /// Test-only constructor that redirects all provider requests to `base_url`.
    #[cfg(test)]
    fn with_test_base(base_url: impl Into<String>) -> Self {
        let client = Client::builder()
            .timeout(Duration::from_secs(2))
            .connect_timeout(Duration::from_secs(1))
            .user_agent("recursive-test")
            .build()
            .expect("reqwest client build");
        Self {
            client,
            test_base_url: Some(base_url.into()),
            provider_override: None,
            api_key_override: None,
            jina_key_override: None,
        }
    }

    /// Resolve the effective base URL for a provider.
    fn base_url(&self, default: &str) -> String {
        self.test_base_url
            .as_deref()
            .unwrap_or(default)
            .trim_end_matches('/')
            .to_string()
    }

    /// Read provider + api_key from config override or env. Returns `None` if not configured.
    fn load_config(&self) -> Option<(Provider, String)> {
        // Check struct overrides first, then fall back to env vars.
        let provider_str = self
            .provider_override
            .clone()
            .or_else(|| std::env::var("RECURSIVE_WEB_SEARCH_PROVIDER").ok())
            .filter(|s| !s.is_empty())?;
        let api_key = self
            .api_key_override
            .clone()
            .or_else(|| std::env::var("RECURSIVE_WEB_SEARCH_API_KEY").ok())
            .filter(|s| !s.is_empty())?;
        let provider = Provider::from_str(&provider_str)?;
        Some((provider, api_key))
    }

    async fn search_brave(
        &self,
        query: &str,
        num: u64,
        api_key: &str,
    ) -> Result<Vec<SearchResult>> {
        let url = format!("{}/res/v1/web/search", self.base_url(BRAVE_BASE));
        let resp = self
            .client
            .get(&url)
            .header("Accept", "application/json")
            .header("X-Subscription-Token", api_key)
            .query(&[("q", query), ("count", &num.to_string())])
            .send()
            .await
            .map_err(|e| Error::Tool {
                name: "WebSearch".into(),
                call_id: None,
                message: format!("Brave request failed: {e}"),
            })?;

        let status = resp.status();
        let body_text = resp.text().await.map_err(|e| Error::Tool {
            name: "WebSearch".into(),
            call_id: None,
            message: format!("Brave response read failed: {e}"),
        })?;

        if !status.is_success() {
            return Err(Error::Tool {
                name: "WebSearch".into(),
                call_id: None,
                message: format!("Brave HTTP {status}: {body_text}"),
            });
        }

        // SAFETY: serde_json::from_str is safe on arbitrary input and only
        // returns an Err on malformed JSON.
        #[allow(clippy::unwrap_in_result)]
        let data: Value = serde_json::from_str(&body_text).map_err(|e| Error::Tool {
            name: "WebSearch".into(),
            call_id: None,
            message: format!("Brave response parse failed: {e}"),
        })?;

        let results = data["web"]["results"]
            .as_array()
            .cloned()
            .unwrap_or_default();

        Ok(results
            .iter()
            .map(|r| SearchResult {
                title: r["title"].as_str().unwrap_or("").to_string(),
                url: r["url"].as_str().unwrap_or("").to_string(),
                snippet: r["description"].as_str().unwrap_or("").to_string(),
            })
            .collect())
    }

    async fn search_tavily(
        &self,
        query: &str,
        num: u64,
        api_key: &str,
    ) -> Result<Vec<SearchResult>> {
        let url = format!("{}/search", self.base_url(TAVILY_BASE));
        let body = json!({
            "api_key": api_key,
            "query": query,
            "max_results": num,
            "search_depth": "basic",
            "include_answer": false,
        });

        let resp = self
            .client
            .post(&url)
            .json(&body)
            .send()
            .await
            .map_err(|e| Error::Tool {
                name: "WebSearch".into(),
                call_id: None,
                message: format!("Tavily request failed: {e}"),
            })?;

        let status = resp.status();
        let body_text = resp.text().await.map_err(|e| Error::Tool {
            name: "WebSearch".into(),
            call_id: None,
            message: format!("Tavily response read failed: {e}"),
        })?;

        if !status.is_success() {
            return Err(Error::Tool {
                name: "WebSearch".into(),
                call_id: None,
                message: format!("Tavily HTTP {status}: {body_text}"),
            });
        }

        // SAFETY: serde_json::from_str is safe on arbitrary input.
        #[allow(clippy::unwrap_in_result)]
        let data: Value = serde_json::from_str(&body_text).map_err(|e| Error::Tool {
            name: "WebSearch".into(),
            call_id: None,
            message: format!("Tavily response parse failed: {e}"),
        })?;

        let results = data["results"].as_array().cloned().unwrap_or_default();
        Ok(results
            .iter()
            .map(|r| SearchResult {
                title: r["title"].as_str().unwrap_or("").to_string(),
                url: r["url"].as_str().unwrap_or("").to_string(),
                snippet: r["content"].as_str().unwrap_or("").to_string(),
            })
            .collect())
    }

    async fn search_serper(
        &self,
        query: &str,
        num: u64,
        api_key: &str,
    ) -> Result<Vec<SearchResult>> {
        let url = format!("{}/search", self.base_url(SERPER_BASE));
        let body = json!({
            "q": query,
            "num": num,
        });

        let resp = self
            .client
            .post(&url)
            .header("X-API-KEY", api_key)
            .header("Content-Type", "application/json")
            .json(&body)
            .send()
            .await
            .map_err(|e| Error::Tool {
                name: "WebSearch".into(),
                call_id: None,
                message: format!("Serper request failed: {e}"),
            })?;

        let status = resp.status();
        let body_text = resp.text().await.map_err(|e| Error::Tool {
            name: "WebSearch".into(),
            call_id: None,
            message: format!("Serper response read failed: {e}"),
        })?;

        if !status.is_success() {
            return Err(Error::Tool {
                name: "WebSearch".into(),
                call_id: None,
                message: format!("Serper HTTP {status}: {body_text}"),
            });
        }

        // SAFETY: serde_json::from_str is safe on arbitrary input.
        #[allow(clippy::unwrap_in_result)]
        let data: Value = serde_json::from_str(&body_text).map_err(|e| Error::Tool {
            name: "WebSearch".into(),
            call_id: None,
            message: format!("Serper response parse failed: {e}"),
        })?;

        let results = data["organic"].as_array().cloned().unwrap_or_default();
        Ok(results
            .iter()
            .take(num as usize)
            .map(|r| SearchResult {
                title: r["title"].as_str().unwrap_or("").to_string(),
                url: r["link"].as_str().unwrap_or("").to_string(),
                snippet: r["snippet"].as_str().unwrap_or("").to_string(),
            })
            .collect())
    }

    async fn search_bocha(
        &self,
        query: &str,
        num: u64,
        api_key: &str,
    ) -> Result<Vec<SearchResult>> {
        let url = format!("{}/v1/web-search", self.base_url(BOCHA_BASE));
        let body = json!({
            "query": query,
            "count": num,
        });

        let resp = self
            .client
            .post(&url)
            .header("Authorization", format!("Bearer {api_key}"))
            .header("Content-Type", "application/json")
            .json(&body)
            .send()
            .await
            .map_err(|e| Error::Tool {
                name: "WebSearch".into(),
                call_id: None,
                message: format!("Bocha request failed: {e}"),
            })?;

        let status = resp.status();
        let body_text = resp.text().await.map_err(|e| Error::Tool {
            name: "WebSearch".into(),
            call_id: None,
            message: format!("Bocha response read failed: {e}"),
        })?;

        if !status.is_success() {
            return Err(Error::Tool {
                name: "WebSearch".into(),
                call_id: None,
                message: format!("Bocha HTTP {status}: {body_text}"),
            });
        }

        // SAFETY: serde_json::from_str is safe on arbitrary input.
        #[allow(clippy::unwrap_in_result)]
        let data: Value = serde_json::from_str(&body_text).map_err(|e| Error::Tool {
            name: "WebSearch".into(),
            call_id: None,
            message: format!("Bocha response parse failed: {e}"),
        })?;

        // Bocha returns { data: { webPages: { value: [...] } } }
        let results = data["data"]["webPages"]["value"]
            .as_array()
            .cloned()
            .unwrap_or_default();

        Ok(results
            .iter()
            .map(|r| SearchResult {
                title: r["name"].as_str().unwrap_or("").to_string(),
                url: r["url"].as_str().unwrap_or("").to_string(),
                snippet: r["snippet"].as_str().unwrap_or("").to_string(),
            })
            .collect())
    }

    async fn search_bing(&self, query: &str, num: u64, api_key: &str) -> Result<Vec<SearchResult>> {
        let url = format!("{}/v7.0/search", self.base_url(BING_BASE));
        let resp = self
            .client
            .get(&url)
            .header("Ocp-Apim-Subscription-Key", api_key)
            .query(&[("q", query), ("count", &num.to_string())])
            .send()
            .await
            .map_err(|e| Error::Tool {
                name: "WebSearch".into(),
                call_id: None,
                message: format!("Bing request failed: {e}"),
            })?;

        let status = resp.status();
        let body_text = resp.text().await.map_err(|e| Error::Tool {
            name: "WebSearch".into(),
            call_id: None,
            message: format!("Bing response read failed: {e}"),
        })?;

        if !status.is_success() {
            return Err(Error::Tool {
                name: "WebSearch".into(),
                call_id: None,
                message: format!("Bing HTTP {status}: {body_text}"),
            });
        }

        // SAFETY: serde_json::from_str is safe on arbitrary input.
        #[allow(clippy::unwrap_in_result)]
        let data: Value = serde_json::from_str(&body_text).map_err(|e| Error::Tool {
            name: "WebSearch".into(),
            call_id: None,
            message: format!("Bing response parse failed: {e}"),
        })?;

        let results = data["webPages"]["value"]
            .as_array()
            .cloned()
            .unwrap_or_default();

        Ok(results
            .iter()
            .map(|r| SearchResult {
                title: r["name"].as_str().unwrap_or("").to_string(),
                url: r["url"].as_str().unwrap_or("").to_string(),
                snippet: r["snippet"].as_str().unwrap_or("").to_string(),
            })
            .collect())
    }

    /// Zero-config HTML fallback: DuckDuckGo scrape, then Bing scrape.
    ///
    /// Used when no API provider/key is configured. Returns structured
    /// `title / url / snippet` results when either engine yields parseable
    /// hits; callers should fall through to Jina when this returns empty.
    async fn search_html_fallback(&self, query: &str, num: u64) -> Result<Vec<SearchResult>> {
        match self.search_duckduckgo_html(query, num).await {
            Ok(results) if !results.is_empty() => return Ok(results),
            Ok(_) => {}
            Err(err) => {
                tracing::debug!(error = %err, "DuckDuckGo HTML search failed; trying Bing");
            }
        }

        match self.search_bing_html(query, num).await {
            Ok(results) if !results.is_empty() => Ok(results),
            Ok(_) => Ok(Vec::new()),
            Err(err) => {
                tracing::debug!(error = %err, "Bing HTML search failed");
                Ok(Vec::new())
            }
        }
    }

    async fn search_duckduckgo_html(&self, query: &str, num: u64) -> Result<Vec<SearchResult>> {
        let base = self.base_url(DUCKDUCKGO_HTML_BASE);
        let url = format!("{base}/html/");
        let resp = self
            .client
            .get(&url)
            .query(&[("q", query)])
            .header("User-Agent", BROWSER_USER_AGENT)
            .header(
                "Accept",
                "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
            )
            .header("Accept-Language", "en-US,en;q=0.5")
            .send()
            .await
            .map_err(|e| Error::Tool {
                name: "WebSearch".into(),
                call_id: None,
                message: format!("DuckDuckGo HTML request failed: {e}"),
            })?;

        let status = resp.status();
        let body = resp.text().await.map_err(|e| Error::Tool {
            name: "WebSearch".into(),
            call_id: None,
            message: format!("DuckDuckGo HTML response read failed: {e}"),
        })?;

        if !status.is_success() {
            return Err(Error::Tool {
                name: "WebSearch".into(),
                call_id: None,
                message: format!("DuckDuckGo HTML HTTP {status}"),
            });
        }

        if is_duckduckgo_challenge(&body) {
            return Ok(Vec::new());
        }

        Ok(parse_duckduckgo_results(&body, num as usize))
    }

    async fn search_bing_html(&self, query: &str, num: u64) -> Result<Vec<SearchResult>> {
        let base = self.base_url(BING_HTML_BASE);
        let url = format!("{base}/search");
        let resp = self
            .client
            .get(&url)
            .query(&[("q", query)])
            .header("User-Agent", BROWSER_USER_AGENT)
            .header(
                "Accept",
                "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
            )
            .header("Accept-Language", "en-US,en;q=0.5")
            .send()
            .await
            .map_err(|e| Error::Tool {
                name: "WebSearch".into(),
                call_id: None,
                message: format!("Bing HTML request failed: {e}"),
            })?;

        let status = resp.status();
        let body = resp.text().await.map_err(|e| Error::Tool {
            name: "WebSearch".into(),
            call_id: None,
            message: format!("Bing HTML response read failed: {e}"),
        })?;

        if !status.is_success() {
            return Err(Error::Tool {
                name: "WebSearch".into(),
                call_id: None,
                message: format!("Bing HTML HTTP {status}"),
            });
        }

        Ok(parse_bing_results(&body, num as usize))
    }

    /// Zero-config fallback using Jina AI Search (`s.jina.ai`).
    ///
    /// Does not require an API key — Jina provides a free anonymous tier.
    /// Optionally, set `RECURSIVE_WEB_SEARCH_JINA_KEY` for a higher quota.
    /// Returns the raw Markdown content from Jina (not a structured list).
    async fn search_jina_fallback(&self, query: &str) -> Result<String> {
        let jina_base = self
            .test_base_url
            .as_deref()
            .unwrap_or("https://s.jina.ai")
            .trim_end_matches('/');

        // URL-encode the query
        let encoded: String = query
            .chars()
            .flat_map(|c| {
                if c.is_alphanumeric() || matches!(c, '-' | '_' | '.' | '~' | ' ') {
                    if c == ' ' {
                        vec!['+']
                    } else {
                        vec![c]
                    }
                } else {
                    // percent-encode
                    format!("%{:02X}", c as u32).chars().collect()
                }
            })
            .collect();

        let url = format!("{jina_base}/{encoded}");

        let mut req = self
            .client
            .get(&url)
            .header("Accept", "text/markdown")
            .header("X-No-Cache", "true");

        // Optional: use a Jina API key for higher quota (override or env var)
        let jina_key = self
            .jina_key_override
            .clone()
            .or_else(|| std::env::var("RECURSIVE_WEB_SEARCH_JINA_KEY").ok())
            .filter(|k| !k.is_empty());
        if let Some(key) = jina_key {
            req = req.header("Authorization", format!("Bearer {key}"));
        }

        let resp = req.send().await.map_err(|e| Error::Tool {
            name: "WebSearch".into(),
            call_id: None,
            message: format!("Jina Search request failed: {e}"),
        })?;

        let status = resp.status();
        let body = resp.text().await.map_err(|e| Error::Tool {
            name: "WebSearch".into(),
            call_id: None,
            message: format!("Jina Search response read failed: {e}"),
        })?;

        if !status.is_success() {
            return Err(Error::Tool {
                name: "WebSearch".into(),
                call_id: None,
                message: format!("Jina Search HTTP {status}"),
            });
        }

        // Truncate to avoid flooding the context
        const JINA_MAX_CHARS: usize = 4096;
        if body.len() > JINA_MAX_CHARS {
            let mut end = JINA_MAX_CHARS;
            while !body.is_char_boundary(end) {
                end -= 1;
            }
            Ok(format!(
                "{}\n\n[...truncated at {JINA_MAX_CHARS} chars]",
                &body[..end]
            ))
        } else {
            Ok(body)
        }
    }

    /// Format results as a numbered list.
    fn format_results(results: &[SearchResult]) -> String {
        if results.is_empty() {
            return "No results found.".to_string();
        }
        results
            .iter()
            .enumerate()
            .map(|(i, r)| {
                format!(
                    "{}. Title: {}\n   URL: {}\n   Summary: {}",
                    i + 1,
                    r.title,
                    r.url,
                    r.snippet
                )
            })
            .collect::<Vec<_>>()
            .join("\n\n")
    }
}

#[derive(Debug)]
struct SearchResult {
    title: String,
    url: String,
    snippet: String,
}

// ── HTML SERP parsing (DuckDuckGo / Bing zero-config fallback) ───────────────

static DDG_TITLE_RE: OnceLock<Regex> = OnceLock::new();
static DDG_SNIPPET_RE: OnceLock<Regex> = OnceLock::new();
static TAG_RE: OnceLock<Regex> = OnceLock::new();
static BING_RESULT_RE: OnceLock<Regex> = OnceLock::new();
static BING_TITLE_RE: OnceLock<Regex> = OnceLock::new();
static BING_SNIPPET_RE: OnceLock<Regex> = OnceLock::new();

fn ddg_title_re() -> &'static Regex {
    DDG_TITLE_RE.get_or_init(|| {
        #[allow(
            clippy::expect_used,
            reason = "static regex pattern is a compile-time constant"
        )]
        Regex::new(r#"<a[^>]*class=\"result__a\"[^>]*href=\"([^\"]+)\"[^>]*>(.*?)</a>"#)
            .expect("ddg title regex")
    })
}

fn ddg_snippet_re() -> &'static Regex {
    DDG_SNIPPET_RE.get_or_init(|| {
        #[allow(
            clippy::expect_used,
            reason = "static regex pattern is a compile-time constant"
        )]
        Regex::new(
            r#"<a[^>]*class=\"result__snippet\"[^>]*>(.*?)</a>|<div[^>]*class=\"result__snippet\"[^>]*>(.*?)</div>"#,
        )
        .expect("ddg snippet regex")
    })
}

fn tag_re() -> &'static Regex {
    TAG_RE.get_or_init(|| {
        #[allow(
            clippy::expect_used,
            reason = "static regex pattern is a compile-time constant"
        )]
        Regex::new(r"<[^>]+>").expect("tag regex")
    })
}

fn bing_result_re() -> &'static Regex {
    BING_RESULT_RE.get_or_init(|| {
        #[allow(
            clippy::expect_used,
            reason = "static regex pattern is a compile-time constant"
        )]
        Regex::new(r#"(?is)<li[^>]*class=\"[^\"]*\bb_algo\b[^\"]*\"[^>]*>(.*?)</li>"#)
            .expect("bing result regex")
    })
}

fn bing_title_re() -> &'static Regex {
    BING_TITLE_RE.get_or_init(|| {
        #[allow(
            clippy::expect_used,
            reason = "static regex pattern is a compile-time constant"
        )]
        Regex::new(r#"(?is)<h2[^>]*>.*?<a[^>]*href=\"([^\"]+)\"[^>]*>(.*?)</a>"#)
            .expect("bing title regex")
    })
}

fn bing_snippet_re() -> &'static Regex {
    BING_SNIPPET_RE.get_or_init(|| {
        #[allow(
            clippy::expect_used,
            reason = "static regex pattern is a compile-time constant"
        )]
        Regex::new(r#"(?is)<div[^>]*class=\"[^\"]*\bb_caption\b[^\"]*\"[^>]*>.*?<p[^>]*>(.*?)</p>"#)
            .expect("bing snippet regex")
    })
}

fn is_duckduckgo_challenge(html: &str) -> bool {
    html.contains("anomaly-modal") || html.contains("Unfortunately, bots use DuckDuckGo too")
}

fn parse_duckduckgo_results(html: &str, max_results: usize) -> Vec<SearchResult> {
    let snippets: Vec<String> = ddg_snippet_re()
        .captures_iter(html)
        .filter_map(|cap| cap.get(1).or_else(|| cap.get(2)))
        .map(|m| normalize_text(m.as_str()))
        .collect();

    let mut results = Vec::new();
    for (idx, cap) in ddg_title_re().captures_iter(html).enumerate() {
        if results.len() >= max_results {
            break;
        }
        let href = cap.get(1).map(|m| m.as_str()).unwrap_or("");
        let title = normalize_text(cap.get(2).map(|m| m.as_str()).unwrap_or(""));
        if title.is_empty() {
            continue;
        }
        let snippet = snippets
            .get(idx)
            .cloned()
            .filter(|s| !s.is_empty())
            .unwrap_or_default();
        results.push(SearchResult {
            title,
            url: normalize_ddg_url(href),
            snippet,
        });
    }

    if is_likely_spam_results(&results) {
        return Vec::new();
    }
    results
}

fn parse_bing_results(html: &str, max_results: usize) -> Vec<SearchResult> {
    let mut results = Vec::new();
    for cap in bing_result_re().captures_iter(html) {
        if results.len() >= max_results {
            break;
        }
        let Some(block) = cap.get(1).map(|m| m.as_str()) else {
            continue;
        };
        let Some(title_cap) = bing_title_re().captures(block) else {
            continue;
        };
        let href = title_cap.get(1).map(|m| m.as_str()).unwrap_or("");
        let title = normalize_text(title_cap.get(2).map(|m| m.as_str()).unwrap_or(""));
        if title.is_empty() {
            continue;
        }
        let snippet = bing_snippet_re()
            .captures(block)
            .and_then(|c| c.get(1))
            .map(|m| normalize_text(m.as_str()))
            .filter(|s| !s.is_empty())
            .unwrap_or_default();
        results.push(SearchResult {
            title,
            url: normalize_bing_url(href),
            snippet,
        });
    }

    if is_likely_spam_results(&results) {
        return Vec::new();
    }
    results
}

/// Drop SERP batches dominated by a single root domain (SEO spam / stuffed pages).
fn is_likely_spam_results(results: &[SearchResult]) -> bool {
    if results.len() < 3 {
        return false;
    }
    let mut counts: std::collections::HashMap<String, usize> = std::collections::HashMap::new();
    for r in results {
        if let Some(host) = root_domain(&r.url) {
            *counts.entry(host).or_insert(0) += 1;
        }
    }
    let Some(&max) = counts.values().max() else {
        return false;
    };
    max * 5 >= results.len() * 3
}

fn root_domain(url: &str) -> Option<String> {
    let after_scheme = url.split_once("://").map(|(_, r)| r).unwrap_or(url);
    let host = after_scheme.split(['/', '?', '#']).next()?;
    let host = host.split('@').next_back()?;
    let host = host.split(':').next()?.to_ascii_lowercase();
    if host.is_empty() {
        return None;
    }
    let labels: Vec<&str> = host.split('.').filter(|s| !s.is_empty()).collect();
    if labels.len() <= 2 {
        return Some(host);
    }
    Some(labels[labels.len().saturating_sub(2)..].join("."))
}

fn normalize_text(text: &str) -> String {
    let stripped = tag_re().replace_all(text, "").to_string();
    let decoded = decode_html_entities(&stripped);
    decoded.split_whitespace().collect::<Vec<_>>().join(" ")
}

fn decode_html_entities(text: &str) -> String {
    static ENTITY_RE: OnceLock<Regex> = OnceLock::new();
    let re = ENTITY_RE.get_or_init(|| {
        #[allow(
            clippy::expect_used,
            reason = "static regex pattern is a compile-time constant"
        )]
        Regex::new(r"&(?:#(\d+)|#x([0-9A-Fa-f]+)|([a-zA-Z]+));").expect("HTML entity regex")
    });

    re.replace_all(text, |caps: &regex::Captures| {
        if let Some(dec) = caps.get(1) {
            return dec
                .as_str()
                .parse::<u32>()
                .ok()
                .and_then(std::char::from_u32)
                .unwrap_or('\u{FFFD}')
                .to_string();
        }
        if let Some(hex) = caps.get(2) {
            return u32::from_str_radix(hex.as_str(), 16)
                .ok()
                .and_then(std::char::from_u32)
                .unwrap_or('\u{FFFD}')
                .to_string();
        }
        match caps.get(3).map(|m| m.as_str()) {
            Some("amp") => "&".to_string(),
            Some("lt") => "<".to_string(),
            Some("gt") => ">".to_string(),
            Some("quot") => "\"".to_string(),
            Some("apos") => "'".to_string(),
            Some("nbsp") => " ".to_string(),
            Some("mdash") => "\u{2014}".to_string(),
            Some("ndash") => "\u{2013}".to_string(),
            Some("hellip") => "\u{2026}".to_string(),
            _ => caps.get(0).map(|m| m.as_str()).unwrap_or("").to_string(),
        }
    })
    .to_string()
}

fn percent_decode(input: &str) -> String {
    let bytes = input.as_bytes();
    let mut out = Vec::new();
    let mut i = 0;
    while i < bytes.len() {
        match bytes[i] {
            b'%' if i + 2 < bytes.len() => {
                let hex = &input[i + 1..i + 3];
                if let Ok(val) = u8::from_str_radix(hex, 16) {
                    out.push(val);
                    i += 3;
                    continue;
                }
                out.push(bytes[i]);
            }
            b'+' => out.push(b' '),
            c => out.push(c),
        }
        i += 1;
    }
    String::from_utf8_lossy(&out).to_string()
}

fn extract_query_param(url: &str, key: &str) -> Option<String> {
    let query = url.split_once('?')?.1;
    for part in query.split('&') {
        let mut iter = part.splitn(2, '=');
        let name = iter.next().unwrap_or("");
        if name == key {
            return iter.next().map(str::to_string);
        }
    }
    None
}

fn normalize_ddg_url(href: &str) -> String {
    if let Some(uddg) = extract_query_param(href, "uddg") {
        let decoded = percent_decode(&uddg);
        if !decoded.is_empty() {
            return decoded;
        }
    }
    if href.starts_with("//") {
        return format!("https:{href}");
    }
    if href.starts_with('/') {
        return format!("https://duckduckgo.com{href}");
    }
    href.to_string()
}

fn normalize_bing_url(href: &str) -> String {
    // Bing wraps SERP links in `/ck/a?...&u=<base64>` redirects; HTML often
    // encodes separators as `&amp;`, so decode entities before parsing `u=`.
    let href = decode_html_entities(href);
    let href = href.as_str();
    if let Some(encoded) = extract_query_param(href, "u") {
        let decoded = percent_decode(&encoded);
        let token = decoded.strip_prefix("a1").unwrap_or(&decoded);
        let mut padded = token.replace('-', "+").replace('_', "/");
        while padded.len() % 4 != 0 {
            padded.push('=');
        }
        if let Ok(bytes) = general_purpose::STANDARD.decode(padded) {
            if let Ok(url) = String::from_utf8(bytes) {
                if url.starts_with("http://") || url.starts_with("https://") {
                    return url;
                }
            }
        }
    }
    if href.starts_with("//") {
        return format!("https:{href}");
    }
    if href.starts_with('/') {
        return format!("https://www.bing.com{href}");
    }
    href.to_string()
}

#[async_trait]
impl Tool for WebSearch {
    fn spec(&self) -> ToolSpec {
        ToolSpec {
            name: "WebSearch".into(),
            description: "Search the web and return relevant results. When \
                           RECURSIVE_WEB_SEARCH_PROVIDER and RECURSIVE_WEB_SEARCH_API_KEY are \
                           set, returns a structured list (title, URL, summary) via the \
                           configured provider (brave | tavily | serper | bocha | bing). \
                           When no provider is configured, tries DuckDuckGo HTML then Bing \
                           HTML (zero-config scrape), then falls back to Jina AI Search \
                           (Markdown). Optionally set RECURSIVE_WEB_SEARCH_JINA_KEY for a \
                           higher Jina quota."
                .into(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "query": {
                        "type": "string",
                        "description": "The search query."
                    },
                    "num_results": {
                        "type": "integer",
                        "description": "Number of results to return (1-10, default 5)."
                    }
                },
                "required": ["query"]
            }),
        }
    }

    fn side_effect_class(&self) -> crate::tools::ToolSideEffect {
        crate::tools::ToolSideEffect::ReadOnly
    }

    fn kind(&self) -> ToolKind {
        ToolKind::WebSearch
    }

    async fn execute(&self, args: Value) -> Result<String> {
        let query = args["query"].as_str().ok_or_else(|| Error::BadToolArgs {
            name: "WebSearch".into(),
            message: "missing `query`".into(),
        })?;

        if query.trim().is_empty() {
            return Err(Error::BadToolArgs {
                name: "WebSearch".into(),
                message: "`query` must not be empty".into(),
            });
        }

        let num = args
            .get("num_results")
            .and_then(|v| v.as_u64())
            .unwrap_or(DEFAULT_NUM_RESULTS)
            .clamp(1, MAX_NUM_RESULTS);

        // If no provider is configured, try HTML scrape (DDG → Bing), then Jina.
        let Some((provider, api_key)) = self.load_config() else {
            let html_results = self.search_html_fallback(query, num).await?;
            if !html_results.is_empty() {
                return Ok(Self::format_results(&html_results));
            }
            return self.search_jina_fallback(query).await;
        };

        let results = match provider {
            Provider::Brave => self.search_brave(query, num, &api_key).await?,
            Provider::Tavily => self.search_tavily(query, num, &api_key).await?,
            Provider::Serper => self.search_serper(query, num, &api_key).await?,
            Provider::Bocha => self.search_bocha(query, num, &api_key).await?,
            Provider::Bing => self.search_bing(query, num, &api_key).await?,
        };

        Ok(Self::format_results(&results))
    }
}

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

    // ── Unit tests (no network) ──────────────────────────────────────────────

    #[test]
    fn provider_from_str_recognises_all() {
        assert_eq!(Provider::from_str("brave"), Some(Provider::Brave));
        assert_eq!(Provider::from_str("BRAVE"), Some(Provider::Brave));
        assert_eq!(Provider::from_str("tavily"), Some(Provider::Tavily));
        assert_eq!(Provider::from_str("serper"), Some(Provider::Serper));
        assert_eq!(Provider::from_str("bocha"), Some(Provider::Bocha));
        assert_eq!(Provider::from_str("bing"), Some(Provider::Bing));
        assert_eq!(Provider::from_str("unknown"), None);
    }

    #[test]
    fn format_results_empty() {
        assert_eq!(WebSearch::format_results(&[]), "No results found.");
    }

    #[test]
    fn format_results_numbered() {
        let results = vec![
            SearchResult {
                title: "Rust lang".to_string(),
                url: "https://rust-lang.org".to_string(),
                snippet: "A systems programming language.".to_string(),
            },
            SearchResult {
                title: "Cargo docs".to_string(),
                url: "https://doc.rust-lang.org/cargo/".to_string(),
                snippet: "Rust's package manager.".to_string(),
            },
        ];
        let out = WebSearch::format_results(&results);
        assert!(out.contains("1. Title: Rust lang"));
        assert!(out.contains("URL: https://rust-lang.org"));
        assert!(out.contains("2. Title: Cargo docs"));
    }

    #[tokio::test]
    async fn rejects_empty_query() {
        let tool = WebSearch::new();
        let err = tool.execute(json!({"query": ""})).await.unwrap_err();
        assert!(err.to_string().contains("must not be empty"));
    }

    #[tokio::test]
    async fn rejects_missing_query() {
        let tool = WebSearch::new();
        let err = tool.execute(json!({})).await.unwrap_err();
        assert!(err.to_string().contains("missing `query`"));
    }

    #[test]
    fn num_results_clamped_to_max() {
        let clamped = 100u64.clamp(1, MAX_NUM_RESULTS);
        assert_eq!(clamped, 10);
    }

    #[test]
    fn num_results_clamped_to_min() {
        // kills `clamp(1, ...)` lower-bound mutation: 0 must become 1
        let clamped = 0u64.clamp(1, MAX_NUM_RESULTS);
        assert_eq!(clamped, 1, "0 must clamp to minimum 1");
    }

    #[test]
    fn format_results_uses_double_newline_separator() {
        // kills `join("\n")` vs `join("\n\n")` mutations
        let results = vec![
            SearchResult {
                title: "A".to_string(),
                url: "http://a.com".to_string(),
                snippet: "snippet-a".to_string(),
            },
            SearchResult {
                title: "B".to_string(),
                url: "http://b.com".to_string(),
                snippet: "snippet-b".to_string(),
            },
        ];
        let out = WebSearch::format_results(&results);
        assert!(
            out.contains("\n\n"),
            "results must be separated by double newline; got: {out:?}"
        );
    }

    #[test]
    fn spec_name_and_description() {
        let spec = WebSearch::new().spec();
        assert_eq!(spec.name, "WebSearch");
        assert!(spec.description.contains("RECURSIVE_WEB_SEARCH_PROVIDER"));
        assert!(spec.description.contains("DuckDuckGo"));
        assert!(spec.description.contains("Jina"));
    }

    #[test]
    fn web_search_construction_smoke() {
        let tool = WebSearch::new();
        assert_eq!(tool.spec().name, "WebSearch");
    }

    #[test]
    fn parse_duckduckgo_html_extracts_title_url_snippet() {
        let html = r#"
            <a class="result__a" href="//duckduckgo.com/l/?uddg=https%3A%2F%2Frust-lang.org">Rust</a>
            <a class="result__snippet">A systems programming language</a>
            <a class="result__a" href="https://doc.rust-lang.org/book/">The Book</a>
            <div class="result__snippet">Official Rust book</div>
        "#;
        let results = parse_duckduckgo_results(html, 5);
        assert_eq!(results.len(), 2);
        assert_eq!(results[0].title, "Rust");
        assert_eq!(results[0].url, "https://rust-lang.org");
        assert_eq!(results[0].snippet, "A systems programming language");
        assert_eq!(results[1].title, "The Book");
        assert_eq!(results[1].url, "https://doc.rust-lang.org/book/");
    }

    #[test]
    fn parse_duckduckgo_respects_max_results() {
        let html = r#"
            <a class="result__a" href="https://a.example">A</a>
            <a class="result__snippet">sa</a>
            <a class="result__a" href="https://b.example">B</a>
            <a class="result__snippet">sb</a>
            <a class="result__a" href="https://c.example">C</a>
            <a class="result__snippet">sc</a>
        "#;
        let results = parse_duckduckgo_results(html, 2);
        assert_eq!(results.len(), 2);
    }

    #[test]
    fn duckduckgo_challenge_page_detected() {
        assert!(is_duckduckgo_challenge(
            r#"<div class="anomaly-modal">Unfortunately, bots use DuckDuckGo too</div>"#
        ));
        assert!(!is_duckduckgo_challenge(
            r#"<a class="result__a" href="https://example.com">Ok</a>"#
        ));
    }

    #[test]
    fn parse_bing_html_extracts_results_and_decodes_ck_url() {
        // Bing `u=` is often `a1` + base64(url). Decode strips the prefix first.
        let real = b"https://example.com/page";
        let encoded = general_purpose::STANDARD.encode(real);
        let encoded = format!("a1{}", encoded.replace('+', "-").replace('/', "_"));
        let href = format!("/ck/a?&amp;u={encoded}");
        let html = format!(
            r#"<li class="b_algo"><h2><a href="{href}">Example Page</a></h2>
               <div class="b_caption"><p>An example snippet</p></div></li>"#
        );
        let results = parse_bing_results(&html, 5);
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].title, "Example Page");
        assert_eq!(results[0].url, "https://example.com/page");
        assert_eq!(results[0].snippet, "An example snippet");
    }

    #[test]
    fn spam_heuristic_drops_single_domain_stuffed_results() {
        let results = vec![
            SearchResult {
                title: "1".into(),
                url: "https://spam.example/a".into(),
                snippet: "a".into(),
            },
            SearchResult {
                title: "2".into(),
                url: "https://spam.example/b".into(),
                snippet: "b".into(),
            },
            SearchResult {
                title: "3".into(),
                url: "https://spam.example/c".into(),
                snippet: "c".into(),
            },
            SearchResult {
                title: "4".into(),
                url: "https://spam.example/d".into(),
                snippet: "d".into(),
            },
            SearchResult {
                title: "5".into(),
                url: "https://other.org/e".into(),
                snippet: "e".into(),
            },
        ];
        assert!(is_likely_spam_results(&results));
    }

    #[test]
    fn spam_heuristic_keeps_mixed_domains() {
        let results = vec![
            SearchResult {
                title: "1".into(),
                url: "https://a.example/x".into(),
                snippet: "a".into(),
            },
            SearchResult {
                title: "2".into(),
                url: "https://b.example/x".into(),
                snippet: "b".into(),
            },
            SearchResult {
                title: "3".into(),
                url: "https://c.example/x".into(),
                snippet: "c".into(),
            },
            SearchResult {
                title: "4".into(),
                url: "https://d.example/x".into(),
                snippet: "d".into(),
            },
            SearchResult {
                title: "5".into(),
                url: "https://e.example/x".into(),
                snippet: "e".into(),
            },
        ];
        assert!(!is_likely_spam_results(&results));
    }

    // ── Integration tests with mockito (no real API key needed) ─────────────
    //
    // Per AGENTS.md: env-var tests MUST be ONE test function to avoid races.
    // This single test covers all 5 providers sequentially.

    #[tokio::test]
    async fn mock_providers_return_expected_results() {
        use mockito::Server;

        let mut server = Server::new_async().await;
        let base = server.url();

        // ── Brave ──
        let brave_mock = server
            .mock("GET", "/res/v1/web/search")
            .match_query(mockito::Matcher::Any)
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                r#"{"web":{"results":[{"title":"Rust","url":"https://rust-lang.org","description":"A systems language"}]}}"#,
            )
            .create_async()
            .await;

        let tool = WebSearch::with_test_base(&base);
        let out = tool
            .search_brave("rust lang", 5, "test-key")
            .await
            .expect("brave mock");
        assert_eq!(out.len(), 1);
        assert_eq!(out[0].title, "Rust");
        assert_eq!(out[0].url, "https://rust-lang.org");
        assert_eq!(out[0].snippet, "A systems language");
        brave_mock.assert_async().await;

        // ── Tavily ──
        let tavily_mock = server
            .mock("POST", "/search")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                r#"{"results":[{"title":"Tavily","url":"https://tavily.com","content":"AI search"}]}"#,
            )
            .create_async()
            .await;

        let out = tool
            .search_tavily("ai search", 5, "test-key")
            .await
            .expect("tavily mock");
        assert_eq!(out.len(), 1);
        assert_eq!(out[0].title, "Tavily");
        assert_eq!(out[0].snippet, "AI search");
        tavily_mock.assert_async().await;

        // ── Serper ──
        let serper_mock = server
            .mock("POST", "/search")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                r#"{"organic":[{"title":"Google","link":"https://google.com","snippet":"Search engine"}]}"#,
            )
            .create_async()
            .await;

        let out = tool
            .search_serper("google", 5, "test-key")
            .await
            .expect("serper mock");
        assert_eq!(out.len(), 1);
        assert_eq!(out[0].url, "https://google.com");
        serper_mock.assert_async().await;

        // ── Bocha ──
        let bocha_mock = server
            .mock("POST", "/v1/web-search")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                r#"{"data":{"webPages":{"value":[{"name":"Bocha","url":"https://bochaai.com","snippet":"National AI search"}]}}}"#,
            )
            .create_async()
            .await;

        let out = tool
            .search_bocha("bocha", 5, "test-key")
            .await
            .expect("bocha mock");
        assert_eq!(out.len(), 1);
        assert_eq!(out[0].title, "Bocha");
        bocha_mock.assert_async().await;

        // ── Bing ──
        let bing_mock = server
            .mock("GET", "/v7.0/search")
            .match_query(mockito::Matcher::Any)
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                r#"{"webPages":{"value":[{"name":"Bing","url":"https://bing.com","snippet":"Microsoft search"}]}}"#,
            )
            .create_async()
            .await;

        let out = tool
            .search_bing("bing", 5, "test-key")
            .await
            .expect("bing mock");
        assert_eq!(out.len(), 1);
        assert_eq!(out[0].snippet, "Microsoft search");
        bing_mock.assert_async().await;
    }

    #[tokio::test]
    async fn mock_provider_http_error_propagates() {
        use mockito::Server;

        let mut server = Server::new_async().await;
        let _m = server
            .mock("GET", "/res/v1/web/search")
            .match_query(mockito::Matcher::Any)
            .with_status(401)
            .with_header("content-type", "application/json")
            .with_body(r#"{"error":"Unauthorized"}"#)
            .create_async()
            .await;

        let tool = WebSearch::with_test_base(server.url());
        let err = tool.search_brave("rust", 5, "bad-key").await.unwrap_err();
        assert!(err.to_string().contains("401"));
    }

    #[tokio::test]
    async fn jina_fallback_returns_markdown_content() {
        use mockito::Server;

        let mut server = Server::new_async().await;
        let _m = server
            .mock("GET", "/rust+programming+language")
            .with_status(200)
            .with_header("content-type", "text/markdown")
            .with_body("# Rust Programming Language\n\nRust is a systems language...")
            .create_async()
            .await;

        let tool = WebSearch::with_test_base(server.url());
        let out = tool
            .search_jina_fallback("rust programming language")
            .await
            .expect("jina fallback mock");
        assert!(out.contains("Rust"));
    }

    #[tokio::test]
    async fn jina_fallback_truncates_long_response() {
        use mockito::Server;

        let mut server = Server::new_async().await;
        let long_body = "x".repeat(5000);
        let _m = server
            .mock("GET", mockito::Matcher::Any)
            .with_status(200)
            .with_header("content-type", "text/markdown")
            .with_body(long_body.as_str())
            .create_async()
            .await;

        let tool = WebSearch::with_test_base(server.url());
        let out = tool
            .search_jina_fallback("anything")
            .await
            .expect("jina truncation mock");
        assert!(out.contains("truncated"));
        assert!(out.len() < 5000);
    }

    #[tokio::test]
    async fn html_fallback_uses_duckduckgo_when_available() {
        use mockito::Server;

        let mut server = Server::new_async().await;
        let ddg_body = r#"
            <a class="result__a" href="https://rust-lang.org">Rust</a>
            <a class="result__snippet">Systems language</a>
        "#;
        let ddg_mock = server
            .mock("GET", "/html/")
            .match_query(mockito::Matcher::Any)
            .with_status(200)
            .with_header("content-type", "text/html")
            .with_body(ddg_body)
            .create_async()
            .await;

        let tool = WebSearch::with_test_base(server.url());
        let out = tool
            .search_html_fallback("rust", 5)
            .await
            .expect("html fallback");
        assert_eq!(out.len(), 1);
        assert_eq!(out[0].title, "Rust");
        assert_eq!(out[0].url, "https://rust-lang.org");
        ddg_mock.assert_async().await;
    }

    #[tokio::test]
    async fn html_fallback_uses_bing_when_duckduckgo_challenged() {
        use mockito::Server;

        let mut server = Server::new_async().await;
        let _ddg = server
            .mock("GET", "/html/")
            .match_query(mockito::Matcher::Any)
            .with_status(200)
            .with_header("content-type", "text/html")
            .with_body(r#"<div class="anomaly-modal">Unfortunately, bots use DuckDuckGo too</div>"#)
            .create_async()
            .await;

        let bing_body = r#"
            <li class="b_algo"><h2><a href="https://bing-result.example">Bing Hit</a></h2>
            <div class="b_caption"><p>From Bing HTML</p></div></li>
        "#;
        let bing_mock = server
            .mock("GET", "/search")
            .match_query(mockito::Matcher::Any)
            .with_status(200)
            .with_header("content-type", "text/html")
            .with_body(bing_body)
            .create_async()
            .await;

        let tool = WebSearch::with_test_base(server.url());
        let out = tool
            .search_html_fallback("rust", 5)
            .await
            .expect("bing html fallback");
        assert_eq!(out.len(), 1);
        assert_eq!(out[0].title, "Bing Hit");
        assert_eq!(out[0].snippet, "From Bing HTML");
        bing_mock.assert_async().await;
    }

    #[tokio::test]
    async fn execute_without_config_prefers_html_over_jina() {
        use mockito::Server;

        // Ensure env vars from other tests don't leak into this path.
        // Per AGENTS.md: keep env mutation inside a single test body.
        let previous_provider = std::env::var("RECURSIVE_WEB_SEARCH_PROVIDER").ok();
        let previous_key = std::env::var("RECURSIVE_WEB_SEARCH_API_KEY").ok();
        // SAFETY: test-only env mutation, restored before return.
        unsafe {
            std::env::remove_var("RECURSIVE_WEB_SEARCH_PROVIDER");
            std::env::remove_var("RECURSIVE_WEB_SEARCH_API_KEY");
        }

        let mut server = Server::new_async().await;
        let ddg_body = r#"
            <a class="result__a" href="https://example.com/html-hit">HTML Hit</a>
            <a class="result__snippet">from duckduckgo</a>
        "#;
        let _ddg = server
            .mock("GET", "/html/")
            .match_query(mockito::Matcher::Any)
            .with_status(200)
            .with_body(ddg_body)
            .create_async()
            .await;

        let tool = WebSearch::with_test_base(server.url());
        let out = tool
            .execute(json!({"query": "html fallback"}))
            .await
            .expect("execute html path");
        assert!(out.contains("HTML Hit"), "got: {out}");
        assert!(out.contains("https://example.com/html-hit"));

        // SAFETY: restore prior env if any.
        unsafe {
            match previous_provider {
                Some(v) => std::env::set_var("RECURSIVE_WEB_SEARCH_PROVIDER", v),
                None => std::env::remove_var("RECURSIVE_WEB_SEARCH_PROVIDER"),
            }
            match previous_key {
                Some(v) => std::env::set_var("RECURSIVE_WEB_SEARCH_API_KEY", v),
                None => std::env::remove_var("RECURSIVE_WEB_SEARCH_API_KEY"),
            }
        }
    }

    #[test]
    fn format_results_empty_returns_no_results_sentinel() {
        // kills `if results.is_empty()` guard removal mutation
        let out = WebSearch::format_results(&[]);
        assert_eq!(
            out, "No results found.",
            "empty results must return the sentinel string"
        );
    }

    #[test]
    fn format_results_includes_numbered_index() {
        // kills `i + 1` → `i` or `i + 2` mutations
        let results = vec![SearchResult {
            title: "Rust".to_string(),
            url: "https://www.rust-lang.org".to_string(),
            snippet: "A systems programming language.".to_string(),
        }];
        let out = WebSearch::format_results(&results);
        assert!(
            out.starts_with("1."),
            "first result must start with '1.'; got: {out:?}"
        );
        assert!(out.contains("Title: Rust"), "result must include the title");
        assert!(
            out.contains("URL: https://www.rust-lang.org"),
            "result must include the URL"
        );
        assert!(
            out.contains("Summary: A systems programming language."),
            "result must include the snippet"
        );
    }
}