aidaemon-tools 0.25.0

Ready-made tools for aidaemon agents (files, search, sandboxed shell, web, checklist); implementation crate of the aidaemon SDK, no stability promise (use it through aidaemon-sdk)
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
use std::collections::HashMap;
use std::time::Duration;

use async_trait::async_trait;
use reqwest::Client;
use serde_json::{Value, json};

use serde::Deserialize;

use crate::OutcomeTool as Tool;
use crate::semantics::ToolCapabilities;
use crate::web::{MAX_FETCH_BODY_BYTES, build_browser_client, read_body_capped};

/// Which search backends `web_search` uses (`[search]` in the daemon's config).
#[derive(Deserialize, Clone)]
pub struct SearchConfig {
    #[serde(default)]
    pub backend: SearchBackendKind,
    #[serde(default)]
    pub api_key: String,
    /// Base URL of a self-hosted SearxNG instance, wherever the user hosts it
    /// (e.g. "http://localhost:8080" for the default Docker setup).
    /// When set, SearxNG becomes available as a search backend.
    #[serde(default)]
    pub searxng_url: String,
    /// When true (default), web_search queries every configured backend in
    /// parallel and merges the results. When false, only `backend` is used.
    #[serde(default = "default_true")]
    pub parallel: bool,
}

fn default_true() -> bool {
    true
}

impl Default for SearchConfig {
    fn default() -> Self {
        Self {
            backend: SearchBackendKind::default(),
            api_key: String::new(),
            searxng_url: String::new(),
            parallel: true,
        }
    }
}

impl std::fmt::Debug for SearchConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let redacted = if self.api_key.is_empty() {
            "\"\""
        } else {
            "[REDACTED]"
        };
        f.debug_struct("SearchConfig")
            .field("backend", &self.backend)
            .field("api_key", &redacted)
            .field("searxng_url", &self.searxng_url)
            .field("parallel", &self.parallel)
            .finish()
    }
}

/// The primary search backend.
#[derive(Debug, Deserialize, Clone, Default, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum SearchBackendKind {
    #[default]
    #[serde(alias = "duckduckgo")]
    DuckDuckGo,
    Brave,
    Searxng,
}

const DEFAULT_MAX_RESULTS: usize = 8;
const MAX_MAX_RESULTS: usize = 20;
/// Per-backend wall-clock budget during parallel fan-out; a slow backend is
/// dropped rather than stalling the whole search.
const BACKEND_TIMEOUT: Duration = Duration::from_secs(12);
/// Reciprocal-rank-fusion constant (standard value from the RRF literature).
const RRF_K: f64 = 60.0;

// ---------------------------------------------------------------------------
// SearchBackend trait + result type
// ---------------------------------------------------------------------------

pub struct SearchResult {
    pub title: String,
    pub url: String,
    pub snippet: String,
    /// Publication age/date when the backend provides one (e.g. "2 days ago").
    pub age: Option<String>,
}

/// Recency window for time-sensitive queries, mapped to each backend's
/// native freshness parameter.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Freshness {
    Day,
    Week,
    Month,
    Year,
}

impl Freshness {
    fn parse(raw: &str) -> Option<Self> {
        match raw.trim().to_ascii_lowercase().as_str() {
            "day" | "d" => Some(Self::Day),
            "week" | "w" => Some(Self::Week),
            "month" | "m" => Some(Self::Month),
            "year" | "y" => Some(Self::Year),
            _ => None,
        }
    }

    fn ddg_param(self) -> &'static str {
        match self {
            Self::Day => "d",
            Self::Week => "w",
            Self::Month => "m",
            Self::Year => "y",
        }
    }

    fn brave_param(self) -> &'static str {
        match self {
            Self::Day => "pd",
            Self::Week => "pw",
            Self::Month => "pm",
            Self::Year => "py",
        }
    }

    fn searxng_param(self) -> &'static str {
        match self {
            Self::Day => "day",
            Self::Week => "week",
            Self::Month => "month",
            Self::Year => "year",
        }
    }
}

const DUCKDUCKGO_ENDPOINT: &str = "https://lite.duckduckgo.com/lite/";
const BRAVE_ENDPOINT: &str = "https://api.search.brave.com/res/v1/web/search";

#[async_trait]
pub trait SearchBackend: Send + Sync {
    fn name(&self) -> &'static str;
    /// The URL this backend sends queries to, for the call's declared egress.
    /// `None` when it is not known (the call is then undeclared).
    fn endpoint(&self) -> Option<&str> {
        None
    }
    async fn search(
        &self,
        query: &str,
        max_results: usize,
        freshness: Option<Freshness>,
    ) -> anyhow::Result<Vec<SearchResult>>;
}

/// Build a single backend of the given kind from config.
pub fn build_backend(kind: &SearchBackendKind, config: &SearchConfig) -> Box<dyn SearchBackend> {
    match kind {
        SearchBackendKind::Brave => Box::new(BraveBackend::new(&config.api_key)),
        SearchBackendKind::DuckDuckGo => Box::new(DuckDuckGoBackend::new()),
        SearchBackendKind::Searxng => Box::new(SearxngBackend::new(&config.searxng_url)),
    }
}

/// All backends that should serve a search: the configured primary first,
/// then (when `parallel` is enabled) every other backend that has the
/// credentials/endpoint it needs. DuckDuckGo needs nothing, so it is always
/// available as a supplementary source.
fn configured_backends(config: &SearchConfig) -> Vec<Box<dyn SearchBackend>> {
    let mut kinds = vec![config.backend.clone()];
    if config.parallel {
        for kind in [
            SearchBackendKind::Brave,
            SearchBackendKind::Searxng,
            SearchBackendKind::DuckDuckGo,
        ] {
            if kinds.contains(&kind) {
                continue;
            }
            let available = match kind {
                SearchBackendKind::Brave => !config.api_key.is_empty(),
                SearchBackendKind::Searxng => !config.searxng_url.is_empty(),
                SearchBackendKind::DuckDuckGo => true,
            };
            if available {
                kinds.push(kind);
            }
        }
    }
    kinds.iter().map(|k| build_backend(k, config)).collect()
}

// ---------------------------------------------------------------------------
// DuckDuckGo backend (default, no API key)
// ---------------------------------------------------------------------------

pub struct DuckDuckGoBackend {
    client: Client,
}

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

impl DuckDuckGoBackend {
    pub fn new() -> Self {
        Self {
            client: build_browser_client(),
        }
    }
}

#[async_trait]
impl SearchBackend for DuckDuckGoBackend {
    fn name(&self) -> &'static str {
        "duckduckgo"
    }

    fn endpoint(&self) -> Option<&str> {
        Some(DUCKDUCKGO_ENDPOINT)
    }

    async fn search(
        &self,
        query: &str,
        max_results: usize,
        freshness: Option<Freshness>,
    ) -> anyhow::Result<Vec<SearchResult>> {
        let mut params = vec![("q", query.to_string())];
        if let Some(f) = freshness {
            params.push(("df", f.ddg_param().to_string()));
        }
        let url = reqwest::Url::parse_with_params(
            DUCKDUCKGO_ENDPOINT,
            params.iter().map(|(k, v)| (*k, v.as_str())),
        )?
        .to_string();
        let resp = self.client.get(&url).send().await?;
        let status = resp.status();
        let (body, _truncated) = read_body_capped(resp, MAX_FETCH_BODY_BYTES).await?;
        let html = String::from_utf8_lossy(&body).into_owned();

        if !status.is_success() {
            anyhow::bail!("DuckDuckGo returned HTTP {}", status);
        }
        if is_ddg_challenge(&html) {
            anyhow::bail!("DuckDuckGo blocked the request with a bot challenge");
        }

        Ok(parse_ddg_lite_html(&html, max_results))
    }
}

/// Detect DuckDuckGo's anti-bot challenge page, which returns HTTP 200 with
/// no results. Without this check a block is indistinguishable from a query
/// that genuinely has no matches. Only the page's DOM class/id markers are
/// read (structure), never its sentences, which change with locale.
fn is_ddg_challenge(html: &str) -> bool {
    html.contains("anomaly-modal") || html.contains("challenge-form")
}

/// Parse the DuckDuckGo lite HTML page into results.
///
/// DuckDuckGo lite returns a simple HTML page with results in a table.
/// Each result has: a link (<a> tag with href) and snippet text.
/// We parse it with simple string scanning since it's a minimal page.
fn parse_ddg_lite_html(html: &str, max_results: usize) -> Vec<SearchResult> {
    let mut results = Vec::new();

    // Find all result links: <a rel="nofollow" href="..." class="result-link">Title</a>
    // and their snippets in <td class="result-snippet">...</td>
    let mut pos = 0;
    while results.len() < max_results {
        // Find next result link
        let link_start = match html[pos..].find("class=\"result-link\"") {
            Some(p) => pos + p,
            None => break,
        };

        // Extract href from the <a> tag — scan backward for href="
        let tag_start = html[..link_start].rfind("<a ").unwrap_or(link_start);
        let href = extract_attr(&html[tag_start..], "href").unwrap_or_default();

        // Extract title (text between > and </a>)
        let title_start = match html[link_start..].find('>') {
            Some(p) => link_start + p + 1,
            None => {
                pos = link_start + 20;
                continue;
            }
        };
        let title_end = match html[title_start..].find("</a>") {
            Some(p) => title_start + p,
            None => {
                pos = title_start;
                continue;
            }
        };
        let title = strip_tags(&html[title_start..title_end]);

        // Find snippet after this link
        let snippet = if let Some(sn_pos) = html[title_end..].find("class=\"result-snippet\"") {
            let sn_start = title_end + sn_pos;
            let sn_content_start = match html[sn_start..].find('>') {
                Some(p) => sn_start + p + 1,
                None => sn_start,
            };
            let sn_end = match html[sn_content_start..].find("</td>") {
                Some(p) => sn_content_start + p,
                None => sn_content_start,
            };
            strip_tags(&html[sn_content_start..sn_end])
                .trim()
                .to_string()
        } else {
            String::new()
        };

        if !href.is_empty() && !title.is_empty() {
            results.push(SearchResult {
                title,
                url: decode_ddg_redirect(&href),
                snippet,
                age: None,
            });
        }

        pos = title_end + 1;
    }

    results
}

/// DuckDuckGo lite wraps result URLs in a tracking redirect:
/// `//duckduckgo.com/l/?uddg=<percent-encoded target>&rut=...`.
/// Unwrap it to the real destination so the LLM (and web_fetch) get a
/// usable URL instead of a protocol-relative redirect.
fn decode_ddg_redirect(href: &str) -> String {
    let absolute = if let Some(rest) = href.strip_prefix("//") {
        format!("https://{}", rest)
    } else if href.starts_with('/') {
        format!("https://duckduckgo.com{}", href)
    } else {
        href.to_string()
    };

    if let Ok(url) = reqwest::Url::parse(&absolute) {
        let is_ddg_redirect = url
            .host_str()
            .is_some_and(|h| h == "duckduckgo.com" || h.ends_with(".duckduckgo.com"))
            && url.path() == "/l/";
        if is_ddg_redirect && let Some((_, target)) = url.query_pairs().find(|(k, _)| k == "uddg") {
            return target.into_owned();
        }
        return absolute;
    }

    href.to_string()
}

/// Extract an attribute value from an HTML tag fragment.
fn extract_attr(tag: &str, attr: &str) -> Option<String> {
    let pattern = format!("{}=\"", attr);
    let start = tag.find(&pattern)? + pattern.len();
    let end = tag[start..].find('"')? + start;
    Some(html_decode(&tag[start..end]))
}

/// Strip HTML tags from a string.
fn strip_tags(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    let mut in_tag = false;
    for ch in s.chars() {
        match ch {
            '<' => in_tag = true,
            '>' => in_tag = false,
            _ if !in_tag => out.push(ch),
            _ => {}
        }
    }
    html_decode(&out)
}

/// Decode common HTML entities.
fn html_decode(s: &str) -> String {
    s.replace("&amp;", "&")
        .replace("&lt;", "<")
        .replace("&gt;", ">")
        .replace("&quot;", "\"")
        .replace("&#39;", "'")
        .replace("&#x27;", "'")
        .replace("&nbsp;", " ")
}

// ---------------------------------------------------------------------------
// Brave backend (API key required)
// ---------------------------------------------------------------------------

pub struct BraveBackend {
    client: Client,
    api_key: String,
}

impl BraveBackend {
    pub fn new(api_key: &str) -> Self {
        Self {
            client: Client::builder()
                .timeout(std::time::Duration::from_secs(30))
                .build()
                .expect("failed to build HTTP client"),
            api_key: api_key.to_string(),
        }
    }
}

#[async_trait]
impl SearchBackend for BraveBackend {
    fn name(&self) -> &'static str {
        "brave"
    }

    fn endpoint(&self) -> Option<&str> {
        Some(BRAVE_ENDPOINT)
    }

    async fn search(
        &self,
        query: &str,
        max_results: usize,
        freshness: Option<Freshness>,
    ) -> anyhow::Result<Vec<SearchResult>> {
        let mut params = vec![("q", query.to_string()), ("count", max_results.to_string())];
        if let Some(f) = freshness {
            params.push(("freshness", f.brave_param().to_string()));
        }
        let url = reqwest::Url::parse_with_params(
            BRAVE_ENDPOINT,
            params.iter().map(|(k, v)| (*k, v.as_str())),
        )?
        .to_string();

        // Retry with exponential backoff for rate limiting (429)
        let max_retries = 3;
        let mut last_status = reqwest::StatusCode::OK;

        for attempt in 0..max_retries {
            let resp = self
                .client
                .get(&url)
                .header("X-Subscription-Token", &self.api_key)
                .header("Accept", "application/json")
                .send()
                .await?;

            last_status = resp.status();

            if resp.status().is_success() {
                let (body, _truncated) = read_body_capped(resp, MAX_FETCH_BODY_BYTES).await?;
                let data: Value = serde_json::from_slice(&body)?;
                let empty = vec![];
                let web_results = data["web"]["results"].as_array().unwrap_or(&empty);

                let results = web_results
                    .iter()
                    .take(max_results)
                    .filter_map(|r| {
                        let mut snippet = r["description"].as_str().unwrap_or("").to_string();
                        // Paid plans return extra_snippets with more page context;
                        // fold a couple in so the LLM sees more than one sentence.
                        if let Some(extra) = r["extra_snippets"].as_array() {
                            for s in extra.iter().take(2).filter_map(|s| s.as_str()) {
                                if !snippet.is_empty() {
                                    snippet.push_str(" … ");
                                }
                                snippet.push_str(s);
                            }
                        }
                        Some(SearchResult {
                            title: r["title"].as_str()?.to_string(),
                            url: r["url"].as_str()?.to_string(),
                            snippet,
                            age: r["age"]
                                .as_str()
                                .or_else(|| r["page_age"].as_str())
                                .map(|s| s.to_string()),
                        })
                    })
                    .collect();

                return Ok(results);
            }

            // Retry on 429 (rate limited) with exponential backoff
            if resp.status() == reqwest::StatusCode::TOO_MANY_REQUESTS && attempt < max_retries - 1
            {
                let delay = crate::backoff::exponential_backoff(
                    Duration::from_secs(1),
                    attempt as u32,
                    Duration::MAX,
                ); // 1s, 2s, 4s
                tokio::time::sleep(delay).await;
                continue;
            }

            // Non-retryable error or exhausted retries
            break;
        }

        anyhow::bail!("Brave search API error: HTTP {}", last_status)
    }
}

// ---------------------------------------------------------------------------
// SearxNG backend (self-hosted metasearch, no API key)
// ---------------------------------------------------------------------------

pub struct SearxngBackend {
    client: Client,
    base_url: String,
}

impl SearxngBackend {
    pub fn new(base_url: &str) -> Self {
        Self {
            client: Client::builder()
                .timeout(std::time::Duration::from_secs(20))
                .build()
                .expect("failed to build HTTP client"),
            base_url: base_url.trim_end_matches('/').to_string(),
        }
    }
}

#[async_trait]
impl SearchBackend for SearxngBackend {
    fn name(&self) -> &'static str {
        "searxng"
    }

    fn endpoint(&self) -> Option<&str> {
        (!self.base_url.is_empty()).then_some(self.base_url.as_str())
    }

    async fn search(
        &self,
        query: &str,
        max_results: usize,
        freshness: Option<Freshness>,
    ) -> anyhow::Result<Vec<SearchResult>> {
        anyhow::ensure!(
            !self.base_url.is_empty(),
            "SearxNG backend selected but search.searxng_url is not configured"
        );
        let mut params = vec![("q", query.to_string()), ("format", "json".to_string())];
        if let Some(f) = freshness {
            params.push(("time_range", f.searxng_param().to_string()));
        }
        let url = reqwest::Url::parse_with_params(
            &format!("{}/search", self.base_url),
            params.iter().map(|(k, v)| (*k, v.as_str())),
        )?;

        let resp = self.client.get(url).send().await?;
        anyhow::ensure!(
            resp.status().is_success(),
            "SearxNG returned HTTP {} (ensure 'json' is in search.formats in the SearxNG settings)",
            resp.status()
        );
        let (body, _truncated) = read_body_capped(resp, MAX_FETCH_BODY_BYTES).await?;
        let data: Value = serde_json::from_slice(&body)?;
        let empty = vec![];
        let results = data["results"]
            .as_array()
            .unwrap_or(&empty)
            .iter()
            .take(max_results)
            .filter_map(|r| {
                Some(SearchResult {
                    title: r["title"].as_str()?.to_string(),
                    url: r["url"].as_str()?.to_string(),
                    snippet: r["content"].as_str().unwrap_or("").to_string(),
                    age: r["publishedDate"]
                        .as_str()
                        .and_then(|s| s.split('T').next())
                        .map(|s| s.to_string()),
                })
            })
            .collect();

        Ok(results)
    }
}

// ---------------------------------------------------------------------------
// Result merging (reciprocal rank fusion across backends)
// ---------------------------------------------------------------------------

struct MergedResult {
    title: String,
    url: String,
    snippet: String,
    age: Option<String>,
    sources: Vec<&'static str>,
    score: f64,
}

/// Normalize a URL for cross-backend deduplication: scheme-insensitive,
/// fragment stripped, trailing slash stripped.
fn normalize_url(url: &str) -> String {
    if let Ok(mut u) = reqwest::Url::parse(url) {
        u.set_fragment(None);
        let s = u.to_string();
        let s = s
            .strip_prefix("https://")
            .or_else(|| s.strip_prefix("http://"))
            .unwrap_or(&s);
        return s.trim_end_matches('/').to_ascii_lowercase();
    }
    url.trim_end_matches('/').to_ascii_lowercase()
}

/// Merge ranked result lists from multiple backends using reciprocal rank
/// fusion: each result scores sum(1 / (k + rank)) across the backends that
/// returned it, so results confirmed by several sources rank highest. Ties
/// keep primary-backend order (stable sort, primary list is inserted first).
fn merge_ranked(
    lists: &[(&'static str, Vec<SearchResult>)],
    max_results: usize,
) -> Vec<MergedResult> {
    let mut map: HashMap<String, MergedResult> = HashMap::new();
    let mut order: Vec<String> = Vec::new();

    for (backend, results) in lists {
        for (rank, r) in results.iter().enumerate() {
            let key = normalize_url(&r.url);
            let entry = map.entry(key.clone()).or_insert_with(|| {
                order.push(key.clone());
                MergedResult {
                    title: r.title.clone(),
                    url: r.url.clone(),
                    snippet: String::new(),
                    age: None,
                    sources: Vec::new(),
                    score: 0.0,
                }
            });
            entry.score += 1.0 / (RRF_K + rank as f64 + 1.0);
            if r.snippet.len() > entry.snippet.len() {
                entry.snippet = r.snippet.clone();
            }
            if entry.age.is_none() {
                entry.age = r.age.clone();
            }
            if !entry.sources.contains(backend) {
                entry.sources.push(backend);
            }
        }
    }

    let mut merged: Vec<MergedResult> = order
        .into_iter()
        .filter_map(|key| map.remove(&key))
        .collect();
    merged.sort_by(|a, b| {
        b.score
            .partial_cmp(&a.score)
            .unwrap_or(std::cmp::Ordering::Equal)
    });
    merged.truncate(max_results);
    merged
}

// ---------------------------------------------------------------------------
// WebSearch
// ---------------------------------------------------------------------------

/// The `web_search` tool: every configured backend in parallel, results
/// merged by reciprocal rank fusion.
pub struct WebSearch {
    backends: Vec<Box<dyn SearchBackend>>,
    description: String,
}

/// The description [`WebSearch::new`] gives the model.
pub const DEFAULT_DESCRIPTION: &str = "Search the web. Queries all configured search backends in parallel and returns merged, deduplicated results (titles, URLs, snippets, publication age when known). Use to find current information, research topics, check facts. One focused search is almost always enough; for single-fact lookups do NOT re-search with rephrased queries — synthesize promptly. Results are snippet previews, NOT full pages: never assemble complete lists, rosters, or enumerations from snippets — fetch a source page with web_fetch and answer from its text. Set 'freshness' for time-sensitive queries (news, prices, releases). If results are consistently empty, the search backend may be blocked — configure Brave Search (an API key) or a self-hosted SearxNG instance.";

impl WebSearch {
    pub fn new(config: &SearchConfig) -> Self {
        Self {
            backends: configured_backends(config),
            description: DEFAULT_DESCRIPTION.to_string(),
        }
    }

    /// Replace the description the model sees, for example to name where
    /// the host's users configure search.
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = description.into();
        self
    }
}

#[async_trait]
impl Tool for WebSearch {
    fn name(&self) -> &str {
        "web_search"
    }

    fn description(&self) -> &str {
        &self.description
    }

    fn schema(&self) -> Value {
        json!({
            "name": "web_search",
            "description": self.description(),
            "parameters": {
                "type": "object",
                "properties": {
                    "query": {
                        "type": "string",
                        "description": "The search query"
                    },
                    "max_results": {
                        "type": "integer",
                        "description": "Maximum number of merged results (default 8, max 20)"
                    },
                    "freshness": {
                        "type": "string",
                        "enum": ["day", "week", "month", "year"],
                        "description": "Only return results published within this window. Use for time-sensitive queries."
                    }
                },
                "required": ["query"],
                "additionalProperties": false
            }
        })
    }

    async fn execute(
        &self,
        arguments: &str,
        _facts: &crate::semantics::ToolCallContext,
    ) -> anyhow::Result<crate::semantics::ToolCallOutcome> {
        let output: anyhow::Result<String> = async move {
            let args: Value = serde_json::from_str(arguments)?;
            let query = args["query"]
                .as_str()
                .ok_or_else(|| anyhow::anyhow!("Missing required parameter: query"))?;
            let max_results = args["max_results"]
                .as_u64()
                .map(|n| n as usize)
                .unwrap_or(DEFAULT_MAX_RESULTS)
                .clamp(1, MAX_MAX_RESULTS);
            let freshness = args["freshness"].as_str().and_then(Freshness::parse);

            // Fan out to every backend concurrently; tolerate individual failures.
            let searches = self.backends.iter().map(|backend| async move {
                let name = backend.name();
                match tokio::time::timeout(
                    BACKEND_TIMEOUT,
                    backend.search(query, max_results, freshness),
                )
                .await
                {
                    Ok(Ok(results)) => (name, Ok(results)),
                    Ok(Err(e)) => (name, Err(e.to_string())),
                    Err(_) => (
                        name,
                        Err(format!("timed out after {}s", BACKEND_TIMEOUT.as_secs())),
                    ),
                }
            });
            let outcomes = futures::future::join_all(searches).await;

            let mut successes: Vec<(&'static str, Vec<SearchResult>)> = Vec::new();
            let mut failures: Vec<String> = Vec::new();
            for (name, outcome) in outcomes {
                match outcome {
                    Ok(results) => successes.push((name, results)),
                    Err(err) => failures.push(format!("{}: {}", name, err)),
                }
            }

            if successes.is_empty() {
                anyhow::bail!("All search backends failed — {}", failures.join("; "));
            }

            let merged = merge_ranked(&successes, max_results);

            if merged.is_empty() {
                let mut msg = format!("No results found for: {}", query);
                if !failures.is_empty() {
                    msg.push_str(&format!(
                        "\n(Note: some backends were unavailable — {})",
                        failures.join("; ")
                    ));
                }
                return Ok(msg);
            }

            let multi_source = self.backends.len() > 1;
            Ok(format_search_output(&merged, multi_source, &failures))
        }
        .await;
        output.map(crate::semantics::ToolCallOutcome::from_output)
    }
}

impl crate::semantics::ToolSemantics for WebSearch {
    fn dispatch_facts(&self) -> crate::semantics::ToolDispatchFacts {
        crate::semantics::ToolDispatchFacts {
            offering: crate::semantics::Offering::Core,
            same_tool_streak: crate::semantics::SameToolStreak::Versatile,
            recap: crate::semantics::RecapKind::Search,
            activity: crate::semantics::ToolActivity::Fixed(aidaemon_core::card::ActivityKind::Web),
            channel_reach: crate::semantics::ChannelReach::Public,
            evidence: crate::semantics::EvidenceSource::WebSearch,
            semantic_scope: Some(crate::semantics::ToolSemanticScope::ExternalRemote),
            status_label: Some("searching the web"),
            ..Default::default()
        }
    }

    fn capabilities(&self) -> ToolCapabilities {
        ToolCapabilities::READ_ONLY_EXTERNAL
    }

    /// The query is text the model wrote.
    fn call_outbound(&self, _arguments: &str) -> aidaemon_core::Outbound {
        aidaemon_core::Outbound::Composed
    }

    /// The query goes to every configured backend's host, so the egress
    /// policy scans it and a declined host binds the search.
    fn call_effects(&self, _arguments: &str) -> aidaemon_core::CallEffects {
        let mut keys = Vec::new();
        for backend in &self.backends {
            let Some(key) = backend
                .endpoint()
                .and_then(|endpoint| reqwest::Url::parse(endpoint).ok())
                .and_then(|url| crate::command_effects::url_host_effect(&url))
            else {
                return aidaemon_core::CallEffects::unknown();
            };
            if !keys.contains(&key) {
                keys.push(key);
            }
        }
        if keys.is_empty() {
            return aidaemon_core::CallEffects::unknown();
        }
        aidaemon_core::CallEffects {
            keys,
            complete: true,
            understood: false,
        }
    }
}

/// Appended to every non-empty result set. Snippets are previews — without an
/// explicit guard the model assembles "complete" enumerations (rosters, member
/// lists, counts) out of 8 partial snippets, inventing the entries it cannot
/// see. Same failure mode as passive truncation markers (see
/// truncation_notice patterns in similar tools).
const SNIPPET_GUARD_FOOTER: &str = "[⚠ SNIPPETS ONLY — the entries above are search-result \
     previews, not full page content. Do NOT enumerate lists, rosters, members, or counts \
     assembled from snippets, and do NOT present a \"complete\" answer built from them — \
     inventing entries that are not literally visible is an error. For enumeration or \
     list-type questions, fetch a source page with web_fetch and answer only from text you \
     can see; corroborate factual answers with a second independent source when possible, \
     and name the sources you used. If a fetch fails, try the next result URL — do not \
     retry the same one. If no fetched page contains the full answer, say it is incomplete \
     instead of filling the gaps.]";

fn format_search_output(
    merged: &[MergedResult],
    multi_source: bool,
    failures: &[String],
) -> String {
    let formatted: Vec<String> = merged
        .iter()
        .enumerate()
        .map(|(i, r)| {
            let mut entry = format!("{}. [{}]({})", i + 1, r.title, r.url);
            if !r.snippet.is_empty() {
                entry.push_str(&format!("\n   {}", r.snippet));
            }
            let mut meta = Vec::new();
            if let Some(age) = &r.age {
                meta.push(age.clone());
            }
            if multi_source {
                meta.push(format!("via {}", r.sources.join(", ")));
            }
            if !meta.is_empty() {
                entry.push_str(&format!("\n   ({})", meta.join(" — ")));
            }
            entry
        })
        .collect();

    let mut output = formatted.join("\n\n");
    if !failures.is_empty() {
        output.push_str(&format!(
            "\n\n(Note: some backends were unavailable — {})",
            failures.join("; ")
        ));
    }
    output.push_str("\n\n");
    output.push_str(SNIPPET_GUARD_FOOTER);

    output
}

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

    #[test]
    fn decodes_protocol_relative_ddg_redirect() {
        let href = "//duckduckgo.com/l/?uddg=https%3A%2F%2Fdocs.rs%2Ftokio%2Flatest%2Ftokio%2Fruntime%2F&rut=904d86ad457b";
        assert_eq!(
            decode_ddg_redirect(href),
            "https://docs.rs/tokio/latest/tokio/runtime/"
        );
    }

    #[test]
    fn decodes_relative_ddg_redirect() {
        let href = "/l/?uddg=https%3A%2F%2Ftokio.rs%2F&rut=abc";
        assert_eq!(decode_ddg_redirect(href), "https://tokio.rs/");
    }

    #[test]
    fn passes_through_direct_urls() {
        assert_eq!(
            decode_ddg_redirect("https://example.com/page"),
            "https://example.com/page"
        );
        // Protocol-relative non-DDG URLs become absolute.
        assert_eq!(
            decode_ddg_redirect("//example.com/page"),
            "https://example.com/page"
        );
    }

    #[test]
    fn parses_ddg_lite_fixture_and_unwraps_redirects() {
        let html = r#"
        <table>
        <tr><td>1.&nbsp;</td><td><a rel="nofollow" href="//duckduckgo.com/l/?uddg=https%3A%2F%2Ftokio.rs%2F&amp;rut=abc" class="result-link">Tokio - An asynchronous Rust runtime</a></td></tr>
        <tr><td>&nbsp;</td><td class="result-snippet">Tokio is an asynchronous runtime for the Rust programming language.</td></tr>
        <tr><td>2.&nbsp;</td><td><a rel="nofollow" href="https://docs.rs/tokio" class="result-link">tokio - Rust</a></td></tr>
        <tr><td>&nbsp;</td><td class="result-snippet">API documentation for the tokio crate.</td></tr>
        </table>
        "#;
        let results = parse_ddg_lite_html(html, 10);
        assert_eq!(results.len(), 2);
        assert_eq!(results[0].url, "https://tokio.rs/");
        assert_eq!(results[0].title, "Tokio - An asynchronous Rust runtime");
        assert!(results[0].snippet.contains("asynchronous runtime"));
        assert_eq!(results[1].url, "https://docs.rs/tokio");
    }

    #[test]
    fn detects_ddg_challenge_page() {
        assert!(is_ddg_challenge(
            "<html><div class=\"anomaly-modal\">...</div></html>"
        ));
        assert!(is_ddg_challenge("<form id=\"challenge-form\"></form>"));
        // A result snippet quoting the challenge sentence is not a challenge.
        assert!(!is_ddg_challenge(
            "<td class=\"result-snippet\">Unfortunately, bots use DuckDuckGo too.</td>"
        ));
        assert!(!is_ddg_challenge("<td class=\"result-snippet\">ok</td>"));
    }

    #[test]
    fn normalizes_urls_for_dedup() {
        assert_eq!(
            normalize_url("https://Example.com/Page/"),
            normalize_url("http://example.com/page")
        );
        assert_eq!(
            normalize_url("https://example.com/page#section"),
            normalize_url("https://example.com/page")
        );
        assert_ne!(
            normalize_url("https://example.com/page?a=1"),
            normalize_url("https://example.com/page")
        );
    }

    #[test]
    fn merge_dedups_and_boosts_multi_source_results() {
        let lists = vec![
            (
                "brave",
                vec![
                    SearchResult {
                        title: "Only Brave".into(),
                        url: "https://a.example/".into(),
                        snippet: "short".into(),
                        age: Some("2 days ago".into()),
                    },
                    SearchResult {
                        title: "Both".into(),
                        url: "https://b.example/".into(),
                        snippet: "brave snippet".into(),
                        age: None,
                    },
                ],
            ),
            (
                "duckduckgo",
                vec![SearchResult {
                    title: "Both (ddg)".into(),
                    url: "http://b.example".into(),
                    snippet: "a much longer duckduckgo snippet".into(),
                    age: None,
                }],
            ),
        ];
        let merged = merge_ranked(&lists, 10);
        assert_eq!(merged.len(), 2);
        // b.example appears in both backends → ranks above a.example despite
        // being rank 2 in the brave list.
        assert_eq!(merged[0].url, "https://b.example/");
        assert_eq!(merged[0].sources, vec!["brave", "duckduckgo"]);
        // Longest snippet wins.
        assert_eq!(merged[0].snippet, "a much longer duckduckgo snippet");
        assert_eq!(merged[1].sources, vec!["brave"]);
        assert_eq!(merged[1].age.as_deref(), Some("2 days ago"));
    }

    #[test]
    fn merge_respects_max_results() {
        let lists = vec![(
            "brave",
            (0..5)
                .map(|i| SearchResult {
                    title: format!("r{}", i),
                    url: format!("https://example.com/{}", i),
                    snippet: String::new(),
                    age: None,
                })
                .collect::<Vec<_>>(),
        )];
        assert_eq!(merge_ranked(&lists, 3).len(), 3);
    }

    #[test]
    fn freshness_parses_and_maps() {
        assert_eq!(Freshness::parse("day"), Some(Freshness::Day));
        assert_eq!(Freshness::parse("WEEK"), Some(Freshness::Week));
        assert_eq!(Freshness::parse("m"), Some(Freshness::Month));
        assert_eq!(Freshness::parse("bogus"), None);
        assert_eq!(Freshness::Day.brave_param(), "pd");
        assert_eq!(Freshness::Week.ddg_param(), "w");
        assert_eq!(Freshness::Year.searxng_param(), "year");
    }

    #[test]
    fn backend_selection_respects_config() {
        // DDG only (no key, no searxng).
        let cfg = SearchConfig::default();
        let names: Vec<&str> = configured_backends(&cfg).iter().map(|b| b.name()).collect();
        assert_eq!(names, vec!["duckduckgo"]);

        // Brave primary + key → brave first, ddg supplements.
        let cfg = SearchConfig {
            backend: SearchBackendKind::Brave,
            api_key: "k".into(),
            ..Default::default()
        };
        let names: Vec<&str> = configured_backends(&cfg).iter().map(|b| b.name()).collect();
        assert_eq!(names, vec!["brave", "duckduckgo"]);

        // parallel=false → primary only.
        let cfg = SearchConfig {
            backend: SearchBackendKind::Brave,
            api_key: "k".into(),
            parallel: false,
            ..Default::default()
        };
        let names: Vec<&str> = configured_backends(&cfg).iter().map(|b| b.name()).collect();
        assert_eq!(names, vec!["brave"]);

        // Everything configured → primary first, then brave, searxng, ddg order.
        let cfg = SearchConfig {
            backend: SearchBackendKind::DuckDuckGo,
            api_key: "k".into(),
            searxng_url: "http://localhost:8888".into(),
            parallel: true,
        };
        let names: Vec<&str> = configured_backends(&cfg).iter().map(|b| b.name()).collect();
        assert_eq!(names, vec!["duckduckgo", "brave", "searxng"]);
    }
}

#[cfg(test)]
mod format_output_tests {
    use super::*;
    use crate::semantics::ToolSemantics;

    fn merged(title: &str, snippet: &str) -> MergedResult {
        MergedResult {
            title: title.to_string(),
            url: format!("https://example.com/{}", title),
            snippet: snippet.to_string(),
            age: None,
            sources: vec!["duckduckgo"],
            score: 1.0,
        }
    }

    #[test]
    fn search_output_carries_snippet_honesty_guard() {
        let results = vec![merged("squad", "Ecuador announced its 26-man roster...")];
        let out = format_search_output(&results, false, &[]);
        assert!(out.contains("Ecuador announced"));
        // Snippets are previews — the model must not assemble "complete"
        // enumerations (rosters, lists, counts) out of them.
        assert!(out.contains("SNIPPETS ONLY"));
        assert!(out.contains("Do NOT enumerate"));
        assert!(out.contains("web_fetch"));
        // Factual answers should be corroborated, not single-sourced.
        assert!(out.contains("second independent source"));
        assert!(out.contains("name the sources"));
    }

    #[test]
    fn backend_failures_still_noted_in_output() {
        let results = vec![merged("a", "snippet a")];
        let out = format_search_output(&results, false, &["brave: timeout".to_string()]);
        assert!(out.contains("some backends were unavailable"));
        assert!(out.contains("brave: timeout"));
    }

    #[test]
    fn schema_description_scopes_synthesis_to_non_enumeration() {
        let tool = WebSearch::new(&SearchConfig::default());
        let schema = tool.schema();
        let desc = schema["description"].as_str().unwrap();
        // "synthesize promptly" must not invite assembling full lists from snippets.
        assert!(desc.contains("previews"));
        assert!(desc.contains("web_fetch"));
    }

    /// A read-only search still sends the query out: it declares each
    /// configured backend's host so the egress policy sees it.
    #[test]
    fn web_search_declares_backend_hosts() {
        use aidaemon_core::EffectKey;
        let default = WebSearch::new(&SearchConfig::default());
        let effects = default.call_effects(r#"{"query":"acme"}"#);
        assert!(effects.complete);
        assert_eq!(
            effects.keys,
            vec![EffectKey::egress("https", "lite.duckduckgo.com", None)]
        );
        let config = SearchConfig {
            backend: SearchBackendKind::Searxng,
            searxng_url: "http://localhost:8080".to_string(),
            parallel: false,
            ..SearchConfig::default()
        };
        let searxng = WebSearch::new(&config).call_effects(r#"{"query":"acme"}"#);
        assert!(searxng.complete);
        assert_eq!(searxng.keys[0].target, "localhost:8080");
    }
}