a3s-code-core 6.8.0

A3S Code Core - Embeddable AI agent library with tool execution
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
//! Web search tool - Search the web via a3s-search

mod engines;
mod fallback;

#[cfg(feature = "headless-search")]
use crate::config::{BrowserBackend, HeadlessConfig};
use crate::tools::types::{Tool, ToolContext, ToolErrorKind, ToolOutput};
#[cfg(feature = "headless-search")]
use a3s_search::a3s_use_browser::{BrowserPool, BrowserPoolConfig, BrowserProvider};
use a3s_search::proxy::ProxyConfig;
use a3s_search::{
    EngineFailure, Metrics, MetricsSnapshot, RetrievalHealth, RetrievalRequirements, Search,
    SearchCascade, SearchCoalescerSnapshot, SearchQuery, SearchResult, SearchResults,
};
use anyhow::Result;
use async_trait::async_trait;
use regex::Regex;
use std::sync::Arc;
use std::sync::OnceLock;
use std::time::{Duration, Instant};

const MIN_FULL_TEXT_BYTES: usize = 512;
const MAX_FULL_TEXT_BYTES: usize = 32 * 1024;
const MAX_JSON_TITLE_BYTES: usize = 2 * 1024;
const MAX_JSON_CONTENT_BYTES: usize = 4 * 1024;
const JSON_OUTPUT_RESERVE_BYTES: usize = 4 * 1024;
const MAX_JSON_OUTPUT_BYTES: usize = crate::tools::MAX_OUTPUT_SIZE - JSON_OUTPUT_RESERVE_BYTES;

#[cfg(feature = "headless-search")]
const WEB_SEARCH_DESCRIPTION: &str =
    "Search the web through a structurally gated cascade: headless Google/Baidu first, HTTP/RSS engines \
     only when needed, and native APIs only when earlier tiers remain insufficient. \
     Unavailable engines are skipped through session-scoped circuit state, and all executed tiers \
     are deduplicated and ranked together. An explicit engines list runs only those requested tiers. \
     Supports proxy configuration for conventional and headless search transports.";

#[cfg(not(feature = "headless-search"))]
const WEB_SEARCH_DESCRIPTION: &str =
    "Search the web through a structurally gated cascade of HTTP/RSS engines and native APIs. \
     Unavailable engines are skipped through session-scoped circuit state, and all executed tiers \
     are deduplicated and ranked together. An explicit engines list runs only those requested tiers. \
     Supports proxy configuration for conventional search transports.";

#[cfg(feature = "headless-search")]
const ENGINE_CATALOG_DESCRIPTION: &str =
    "Optional. List of search engines or native providers to use. Without explicit configuration, \
     all built-in providers that advertise anonymous access are combined with the public HTTP \
     defaults. Available: anysearch (anonymous or authenticated native provider), tavily (keyless \
     or authenticated native provider), ddg (DuckDuckGo), brave (Brave Search), bing (Bing RSS), \
     wiki (Wikipedia), sogou (Sogou), 360 / so360 (360 Search), bing_cn (Bing China RSS), \
     g / google (Google, headless), baidu (Baidu, headless).";

#[cfg(not(feature = "headless-search"))]
const ENGINE_CATALOG_DESCRIPTION: &str =
    "Optional. List of search engines or native providers to use. Without explicit configuration, \
     all built-in providers that advertise anonymous access are combined with the public HTTP \
     defaults. Available: anysearch (anonymous or authenticated native provider), tavily (keyless \
     or authenticated native provider), ddg (DuckDuckGo), brave (Brave Search), bing (Bing RSS), \
     wiki (Wikipedia), sogou (Sogou), 360 / so360 (360 Search), and bing_cn (Bing China RSS).";

#[cfg(feature = "headless-search")]
use engines::add_headless_engine;
use engines::{add_http_engine, default_engine_selection, EngineTier};
use fallback::{
    automatic_tier_order, failure_metadata, failure_summary, outcome_metadata, text_notice_note,
    tier_timeout, tiered_engine_plan, tool_error_kind_for_failures, usable_result_count,
};

pub struct WebSearchTool;

impl WebSearchTool {
    pub fn new() -> Self {
        Self
    }

    /// Create an execution-scoped browser pool for headless engines.
    ///
    /// A persistent pool survives a cancelled tool future and can retain the
    /// Chrome process for the rest of the TUI session. Keeping the pool scoped
    /// to one invocation lets the cleanup guard deterministically close it on
    /// success, error, timeout, or caller cancellation.
    #[cfg(feature = "headless-search")]
    fn create_pool(config: &HeadlessConfig) -> Arc<BrowserPool> {
        let executable = config.browser_path.as_ref().map(std::path::PathBuf::from);
        let provider = match (config.backend, executable) {
            (BrowserBackend::Chrome, Some(path)) => BrowserProvider::ChromeExecutable(path),
            (BrowserBackend::Chrome, None) => BrowserProvider::DiscoveredChrome,
            (BrowserBackend::Lightpanda, Some(path)) => BrowserProvider::LightpandaExecutable(path),
            (BrowserBackend::Lightpanda, None) => BrowserProvider::DiscoveredLightpanda,
        };

        let pool_config = BrowserPoolConfig {
            max_tabs: config.max_tabs,
            headless: true,
            provider,
            proxy_url: config.proxy_url.clone(),
            launch_args: config.launch_args.clone(),
        };

        Arc::new(BrowserPool::new(pool_config))
    }
}

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

#[cfg(feature = "headless-search")]
struct BrowserPoolCleanup {
    pool: Option<Arc<BrowserPool>>,
}

#[cfg(feature = "headless-search")]
impl BrowserPoolCleanup {
    fn new(pool: Option<Arc<BrowserPool>>) -> Self {
        Self { pool }
    }

    async fn shutdown(&mut self) {
        if let Some(pool) = self.pool.as_ref() {
            if tokio::time::timeout(std::time::Duration::from_secs(2), pool.shutdown())
                .await
                .is_err()
            {
                tracing::warn!(
                    "Headless browser cleanup exceeded the 2s foreground grace; continuing in background"
                );
                return;
            }
        }
        self.pool = None;
    }
}

#[cfg(feature = "headless-search")]
impl Drop for BrowserPoolCleanup {
    fn drop(&mut self) {
        let Some(pool) = self.pool.take() else {
            return;
        };
        match tokio::runtime::Handle::try_current() {
            Ok(runtime) => {
                runtime.spawn(async move {
                    pool.shutdown().await;
                });
            }
            Err(error) => tracing::warn!(
                "Could not schedule headless browser cleanup outside a Tokio runtime: {}",
                error
            ),
        }
    }
}

#[cfg(feature = "headless-search")]
fn managed_headless_config() -> Option<HeadlessConfig> {
    managed_headless_config_from_statuses(&crate::search_runtime::browser_statuses())
}

#[cfg(feature = "headless-search")]
fn managed_headless_config_from_statuses(
    statuses: &[crate::search_runtime::BrowserRuntimeStatus],
) -> Option<HeadlessConfig> {
    let status = statuses
        .iter()
        .find(|status| status.available && status.path.is_some())?;
    let backend = match status.browser {
        crate::search_runtime::ManagedBrowser::Chrome => BrowserBackend::Chrome,
        crate::search_runtime::ManagedBrowser::Lightpanda => BrowserBackend::Lightpanda,
    };
    Some(HeadlessConfig {
        backend,
        browser_path: status
            .path
            .as_ref()
            .map(|path| path.to_string_lossy().into_owned()),
        ..HeadlessConfig::default()
    })
}

#[cfg(feature = "headless-search")]
fn effective_headless_config(
    configured: Option<&HeadlessConfig>,
    proxy_url: Option<&str>,
) -> Option<HeadlessConfig> {
    let mut config = configured.cloned().or_else(managed_headless_config)?;
    if let Some(proxy_url) = proxy_url {
        config.proxy_url = Some(proxy_url.to_string());
    }
    Some(config)
}

fn search_result_json(result: &SearchResult, full_text_bytes: Option<usize>) -> serde_json::Value {
    let engines = sorted_search_engines(result);
    let safe_url = safe_search_result_url(result);
    let safe_title = sanitize_http_urls(&result.title);
    let safe_title = crate::text::truncate_utf8(&safe_title, MAX_JSON_TITLE_BYTES);
    let safe_content = sanitize_http_urls(&result.content);
    let safe_content = crate::text::truncate_utf8(&safe_content, MAX_JSON_CONTENT_BYTES);
    let mut value = serde_json::json!({
        "title": safe_title,
        "url": safe_url,
        "content": safe_content,
        "engines": engines,
        "score": result.score,
        "published_date": result.published_date,
    });
    if let (Some(maximum), Some(full_text)) = (full_text_bytes, result.full_text.as_deref()) {
        let sanitized = sanitize_http_urls(full_text);
        let bounded = crate::text::truncate_utf8(&sanitized, maximum);
        if !bounded.trim().is_empty() {
            value["full_text"] = serde_json::Value::String(bounded.to_string());
        }
    }
    value
}

fn bounded_json_search_results(
    results: &[&SearchResult],
    full_text_bytes: Option<usize>,
) -> Vec<serde_json::Value> {
    let mut bounded = Vec::with_capacity(results.len());
    for result in results {
        bounded.push(search_result_json(result, full_text_bytes));
        if serde_json::to_vec(&bounded).is_ok_and(|encoded| encoded.len() <= MAX_JSON_OUTPUT_BYTES)
        {
            continue;
        }
        bounded.pop();
        break;
    }
    bounded
}

fn json_search_payload(
    results: Vec<serde_json::Value>,
    requirements_met: bool,
    health: &RetrievalHealth,
    requirements: &RetrievalRequirements,
) -> serde_json::Value {
    if requirements_met {
        serde_json::Value::Array(results)
    } else {
        serde_json::json!({
            "status": "retrieval_requirements_not_met",
            "message": "Search exhausted the available tiers without meeting the structural retrieval requirements; reformulate the query before treating these candidates as evidence.",
            "retrieval_health": health,
            "retrieval_requirements": requirements,
            "results": results,
        })
    }
}

fn safe_search_result_url(result: &SearchResult) -> String {
    let Some(url) = super::safe_http_source_url(&result.url) else {
        return String::new();
    };
    let Ok(parsed) = reqwest::Url::parse(&url) else {
        return String::new();
    };
    let host = parsed.host_str().unwrap_or_default().to_ascii_lowercase();
    let search_navigation = matches!(
        host.as_str(),
        "search.brave.com" | "duckduckgo.com" | "www.sogou.com" | "www.so.com"
    ) || ((host == "google.com" || host.ends_with(".google.com"))
        && parsed.path().starts_with("/search"));
    if search_navigation {
        String::new()
    } else {
        url
    }
}

fn sorted_search_engines(result: &SearchResult) -> Vec<&str> {
    let mut engines = result
        .engines
        .iter()
        .map(String::as_str)
        .collect::<Vec<_>>();
    engines.sort_unstable();
    engines
}

fn text_search_result(index: usize, result: &SearchResult) -> String {
    let safe_url = safe_search_result_url(result);
    let safe_title = sanitize_http_urls(&result.title);
    let safe_content = sanitize_http_urls(&result.content);
    let published = result
        .published_date
        .as_deref()
        .filter(|date| !date.trim().is_empty())
        .map(|date| format!("   Published: {}\n", date.trim()))
        .unwrap_or_default();
    format!(
        "{}. {}\n   URL: {}\n{}   {}\n   (via {})\n\n",
        index + 1,
        safe_title,
        safe_url,
        published,
        safe_content,
        sorted_search_engines(result).join(", "),
    )
}

fn sanitize_http_urls(text: &str) -> String {
    static URL_RE: OnceLock<Regex> = OnceLock::new();
    let url_re = URL_RE.get_or_init(|| {
        Regex::new(r#"(?i)https?://[^\s<>"'`]+"#).expect("static search URL regex")
    });
    url_re
        .replace_all(text, |captures: &regex::Captures<'_>| {
            let mut candidate = captures[0].to_string();
            let mut suffix = String::new();
            while candidate
                .chars()
                .last()
                .is_some_and(|ch| matches!(ch, ')' | ',' | '.' | ';' | ':' | '!' | '?' | ']' | '}'))
            {
                if let Some(ch) = candidate.pop() {
                    suffix.insert(0, ch);
                }
            }
            super::safe_http_source_url(&candidate)
                .map(|safe| format!("{safe}{suffix}"))
                .unwrap_or_default()
        })
        .into_owned()
}

fn search_metrics_json(snapshot: &MetricsSnapshot) -> serde_json::Value {
    let error_counts = snapshot
        .error_counts
        .iter()
        .map(|(kind, count)| (kind.clone(), *count))
        .collect::<std::collections::BTreeMap<_, _>>();
    serde_json::json!({
        "total_requests": snapshot.total_requests(),
        "successes": snapshot.successes,
        "failures": snapshot.failures,
        "transient_failures": snapshot.transient_failures,
        "permanent_failures": snapshot.permanent_failures,
        "success_rate": snapshot.success_rate(),
        "transient_failure_rate": snapshot.transient_failure_rate(),
        "error_counts": error_counts,
        "latency_p50_ms": snapshot.latency_p50_ms,
        "latency_p95_ms": snapshot.latency_p95_ms,
        "latency_p99_ms": snapshot.latency_p99_ms,
    })
}

fn search_coalescer_json(snapshot: &SearchCoalescerSnapshot) -> serde_json::Value {
    serde_json::json!({
        "max_in_flight": snapshot.max_in_flight,
        "in_flight": snapshot.in_flight,
        "leader_requests": snapshot.leader_requests,
        "shared_requests": snapshot.shared_requests,
        "bypassed_requests": snapshot.bypassed_requests,
        "abandoned_requests": snapshot.abandoned_requests,
    })
}

fn tier_search(ctx: &ToolContext, metrics: Arc<Metrics>) -> Search {
    Search::new()
        .with_metrics(metrics)
        .with_circuit_breaker(ctx.search_circuit_breaker())
        .with_bulkhead(ctx.search_bulkhead())
        .with_request_coalescer(ctx.search_request_coalescer())
}

fn search_error_failure(engine: &str, error: &a3s_search::SearchError) -> EngineFailure {
    let mut failure = EngineFailure::new(engine, error.kind(), error.to_string())
        .with_transient(error.is_transient());
    if let Some(retry_after_seconds) = error.retry_after_seconds() {
        failure = failure.with_retry_after(retry_after_seconds);
    }
    failure
}

async fn execute_search_stage(
    mut search: Search,
    mut results: SearchResults,
    query: &str,
    stage_name: &str,
    deadline: Instant,
    remaining_tiers: usize,
) -> SearchResults {
    if search.engine_count() == 0 {
        return results;
    }

    let remaining = deadline.saturating_duration_since(Instant::now());
    if remaining.is_zero() {
        results.add_failure(
            EngineFailure::new(
                stage_name,
                "timeout",
                "search deadline was exhausted before this tier could start",
            )
            .with_transient(true),
        );
        return results;
    }

    let stage_budget = tier_timeout(remaining, remaining_tiers);
    let engine_budget = stage_budget
        .saturating_sub(Duration::from_millis(100))
        .max(Duration::from_millis(1));
    search.set_timeout(engine_budget);
    match tokio::time::timeout(stage_budget, search.search(SearchQuery::new(query))).await {
        Ok(Ok(stage_results)) => results.merge(stage_results),
        Ok(Err(error)) => results.add_failure(search_error_failure(stage_name, &error)),
        Err(_) => results.add_failure(
            EngineFailure::new(stage_name, "timeout", "search tier timed out").with_transient(true),
        ),
    }
    results
        .items_mut()
        .retain(|result| !safe_search_result_url(result).is_empty());
    results.count = results.items().len();
    results
}

struct SearchStageContext<'a> {
    tool_context: &'a ToolContext,
    query: &'a str,
    proxy_url: Option<&'a str>,
    metrics: &'a Arc<Metrics>,
    deadline: Instant,
}

async fn execute_network_stage(
    context: &SearchStageContext<'_>,
    shortcuts: &[String],
    stage_name: &str,
    remaining_tiers: usize,
) -> SearchResults {
    let mut search = tier_search(context.tool_context, Arc::clone(context.metrics));
    let mut results = SearchResults::new();
    for shortcut in shortcuts {
        match add_http_engine(&mut search, shortcut, context.proxy_url) {
            Ok(true) => {}
            Ok(false) => results.add_failure(EngineFailure::new(
                shortcut,
                "unsupported_engine",
                "engine is not available in this search tier",
            )),
            Err(failure) => {
                context
                    .metrics
                    .record_failure(&failure.kind, failure.transient);
                results.add_failure(failure);
            }
        }
    }
    execute_search_stage(
        search,
        results,
        context.query,
        stage_name,
        context.deadline,
        remaining_tiers,
    )
    .await
}

#[cfg(feature = "headless-search")]
async fn execute_headless_stage(
    context: &SearchStageContext<'_>,
    shortcuts: &[String],
    config: Option<&crate::config::SearchConfig>,
    remaining_tiers: usize,
) -> SearchResults {
    let mut results = SearchResults::new();
    if context
        .deadline
        .saturating_duration_since(Instant::now())
        .is_zero()
    {
        results.add_failure(
            EngineFailure::new(
                "Headless search tier",
                "timeout",
                "search deadline was exhausted before the headless tier could start",
            )
            .with_transient(true),
        );
        return results;
    }

    let headless_config = effective_headless_config(
        config.and_then(|config| config.headless.as_ref()),
        context.proxy_url,
    );
    let Some(headless_config) = headless_config else {
        results.add_failure(EngineFailure::new(
            "Headless search tier",
            "headless_unavailable",
            "no managed headless browser is available",
        ));
        return results;
    };

    let pool = WebSearchTool::create_pool(&headless_config);
    let mut cleanup = BrowserPoolCleanup::new(Some(Arc::clone(&pool)));
    let mut search = tier_search(context.tool_context, Arc::clone(context.metrics));
    let retry_budget = context.tool_context.search_retry_budget();
    for shortcut in shortcuts {
        if !add_headless_engine(
            &mut search,
            shortcut,
            &pool,
            headless_config.backend,
            &retry_budget,
        ) {
            results.add_failure(EngineFailure::new(
                shortcut,
                "unsupported_engine",
                "headless engine is not available",
            ));
        }
    }
    results = execute_search_stage(
        search,
        results,
        context.query,
        "Headless search tier",
        context.deadline,
        remaining_tiers,
    )
    .await;
    cleanup.shutdown().await;
    results
}

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

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

    fn parameters(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "query": {
                    "type": "string",
                    "description": "Required. The search query. Always provide this exact field name: 'query'."
                },
                "engines": {
                    "type": "array",
                    "items": {
                        "type": "string"
                    },
                    "description": ENGINE_CATALOG_DESCRIPTION
                },
                "limit": {
                    "type": "integer",
                    "description": "Optional. Maximum number of results to return. Default: 10. Maximum: 50."
                },
                "timeout": {
                    "type": "integer",
                    "description": "Optional. Search timeout in seconds. Default: 20. Maximum: 60."
                },
                "proxy": {
                    "type": "string",
                    "description": "Optional. Proxy URL, for example http://127.0.0.1:8080 or socks5://127.0.0.1:1080."
                },
                "format": {
                    "type": "string",
                    "enum": ["text", "json"],
                    "description": "Optional. Output format. Default: text."
                },
                "full_text_bytes": {
                    "type": "integer",
                    "minimum": MIN_FULL_TEXT_BYTES,
                    "maximum": MAX_FULL_TEXT_BYTES,
                    "description": "Optional. For JSON output, include at most this many UTF-8 bytes of provider-returned full source text per result. Omitted by default."
                }
            },
            "required": ["query"],
            "examples": [
                {
                    "query": "Rust async trait"
                },
                {
                    "query": "A3S Code GitHub",
                    "engines": ["ddg", "wiki"],
                    "limit": 5,
                    "format": "json"
                },
                {
                    "query": "最新新闻",
                    "engines": ["sogou", "bing_cn"],
                    "limit": 10
                }
            ]
        })
    }

    fn capabilities(&self, _args: &serde_json::Value) -> crate::tools::ToolCapabilities {
        crate::tools::ToolCapabilities::parallel_safe_read(8)
    }

    async fn execute(&self, args: &serde_json::Value, ctx: &ToolContext) -> Result<ToolOutput> {
        // Validate: return error on unknown fields to catch misconfiguration like `engine` vs `engines`
        if let Some(obj) = args.as_object() {
            let valid_fields = [
                "query",
                "engines",
                "limit",
                "timeout",
                "proxy",
                "format",
                "full_text_bytes",
            ];
            for key in obj.keys() {
                if !valid_fields.contains(&key.as_str()) {
                    return Ok(ToolOutput::error(format!(
                        "web_search: unknown parameter '{}' - did you mean 'engines'? \
                         Use 'engines' (plural) as the field name, not 'engine' (singular)",
                        key
                    )));
                }
            }
        }

        let raw_query = match args.get("query").and_then(|v| v.as_str()) {
            Some(q) => q,
            None => return Ok(ToolOutput::error("query parameter is required")),
        };

        if raw_query.trim().is_empty() {
            return Ok(ToolOutput::error("query must not be empty"));
        }
        let query_str = sanitize_http_urls(raw_query);
        if query_str.trim().is_empty() {
            return Ok(ToolOutput::error(
                "query must not be empty after URL sanitization",
            ));
        }

        // Get configuration from context or use defaults
        let config = ctx.search_config.as_ref();
        let default_timeout = config.map(|c| c.timeout).unwrap_or(10);
        let (default_engines, default_engine_selection_source) =
            default_engine_selection(config.map(Arc::as_ref));

        let engine_selection_source = if args.get("engines").is_some() {
            "request"
        } else {
            default_engine_selection_source
        };
        let engines: Vec<&str> = args
            .get("engines")
            .and_then(|v| {
                if let Some(arr) = v.as_array() {
                    Some(arr.iter().filter_map(|v| v.as_str()).collect::<Vec<_>>())
                } else {
                    // Handle comma-separated string like "baidu,ddg" or single engine like "baidu"
                    v.as_str().map(|s| {
                        s.split(',')
                            .map(str::trim)
                            .filter(|s| !s.is_empty())
                            .collect()
                    })
                }
            })
            .unwrap_or_else(|| default_engines.clone());
        let selected_engines = engines
            .iter()
            .map(|engine| engine.to_string())
            .collect::<Vec<_>>();

        let limit = args
            .get("limit")
            .and_then(|v| v.as_u64())
            .unwrap_or(10)
            .min(50) as usize;

        let timeout_secs = args
            .get("timeout")
            .and_then(|v| v.as_u64())
            .unwrap_or(default_timeout)
            .min(60);
        let total_timeout = Duration::from_secs(timeout_secs.max(1));
        let search_started = Instant::now();
        let search_deadline = search_started + total_timeout;

        let output_format = args
            .get("format")
            .and_then(|v| v.as_str())
            .unwrap_or("text");
        let full_text_bytes = match args.get("full_text_bytes") {
            None => None,
            Some(value) => match value.as_u64().and_then(|value| usize::try_from(value).ok()) {
                Some(value) if (MIN_FULL_TEXT_BYTES..=MAX_FULL_TEXT_BYTES).contains(&value) => {
                    Some(value)
                }
                _ => {
                    return Ok(ToolOutput::error(format!(
                        "full_text_bytes must be an integer between {MIN_FULL_TEXT_BYTES} and {MAX_FULL_TEXT_BYTES}"
                    )))
                }
            },
        };

        let mut proxy_url = args
            .get("proxy")
            .and_then(|v| v.as_str())
            .map(str::to_string)
            .or_else(|| {
                config
                    .and_then(|config| config.headless.as_ref())
                    .and_then(|config| config.proxy_url.clone())
            })
            .or_else(super::safe_http::explicit_web_proxy_from_env);
        if proxy_url.is_none() {
            let remaining = search_deadline.saturating_duration_since(Instant::now());
            if !remaining.is_zero() {
                proxy_url = tokio::time::timeout(remaining, super::safe_http::system_web_proxy())
                    .await
                    .ok()
                    .flatten();
            }
        }
        if let Some(proxy) = proxy_url.as_deref() {
            if parse_proxy_url(proxy).is_none() {
                let message = "proxy must include a supported scheme, host, and port".to_string();
                return Ok(ToolOutput::error(&message)
                    .with_error_kind(ToolErrorKind::InvalidArgument { message }));
            }
        }

        let search_metrics = Arc::new(Metrics::new());
        let automatic_fallback = engine_selection_source != "request";
        let tier_plan = tiered_engine_plan(&engines, config.map(Arc::as_ref), automatic_fallback);
        if tier_plan.is_empty() {
            let message = format!("No valid engines found in: {:?}", engines);
            return Ok(ToolOutput::error(&message)
                .with_error_kind(ToolErrorKind::InvalidArgument { message })
                .with_metadata(serde_json::json!({
                    "status": "failed",
                    "engine_selection_source": engine_selection_source,
                    "selected_engines": &selected_engines,
                })));
        }

        let retrieval_requirements = RetrievalRequirements::for_limit(limit);
        let mut cascade = SearchCascade::new(SearchQuery::new(&query_str), retrieval_requirements);
        let stage_context = SearchStageContext {
            tool_context: ctx,
            query: &query_str,
            proxy_url: proxy_url.as_deref(),
            metrics: &search_metrics,
            deadline: search_deadline,
        };

        let active_tiers = automatic_tier_order()
            .into_iter()
            .filter(|tier| match tier {
                #[cfg(feature = "headless-search")]
                EngineTier::Headless => !tier_plan.headless.is_empty(),
                EngineTier::Http => !tier_plan.http.is_empty(),
                EngineTier::Api => !tier_plan.api.is_empty(),
            })
            .collect::<Vec<_>>();

        for (index, tier) in active_tiers.iter().copied().enumerate() {
            if !cascade.needs_next_tier() {
                break;
            }
            let remaining_tiers = active_tiers.len().saturating_sub(index + 1);
            let (name, results) = match tier {
                #[cfg(feature = "headless-search")]
                EngineTier::Headless => (
                    "headless",
                    execute_headless_stage(
                        &stage_context,
                        &tier_plan.headless,
                        config.map(Arc::as_ref),
                        remaining_tiers,
                    )
                    .await,
                ),
                EngineTier::Http => (
                    "http",
                    execute_network_stage(
                        &stage_context,
                        &tier_plan.http,
                        "HTTP search tier",
                        remaining_tiers,
                    )
                    .await,
                ),
                EngineTier::Api => (
                    "api",
                    execute_network_stage(
                        &stage_context,
                        &tier_plan.api,
                        "API search tier",
                        remaining_tiers,
                    )
                    .await,
                ),
            };
            cascade.push_tier(name, results);
        }

        let retrieval_health = cascade.health();
        let requirements_met = retrieval_requirements.is_met(&retrieval_health);
        let tier_reports = cascade.reports().to_vec();
        let mut search_results = cascade.into_results();
        search_results
            .set_duration(u64::try_from(search_started.elapsed().as_millis()).unwrap_or(u64::MAX));

        let mut notices = Vec::new();
        let failure_summary = failure_summary(search_results.failures());
        if usable_result_count(&search_results) > 0 && !failure_summary.is_empty() {
            notices.push(format!(
                "Search completed with degraded engines: {failure_summary}."
            ));
        }
        if !requirements_met {
            notices.push(
                "Search exhausted the available tiers before the structural retrieval requirements were met."
                    .to_string(),
            );
        }
        let executed_engines = search_results
            .outcomes()
            .iter()
            .map(|outcome| outcome.shortcut.clone())
            .collect::<Vec<_>>();
        let search_fallback = serde_json::json!({
            "trigger": "retrieval_requirements",
            "mode": "tiered",
            "attempted": tier_reports.len() > 1,
            "engines": executed_engines,
            "successful": requirements_met,
            "failures": failure_metadata(search_results.failures()),
        });
        let metrics = search_metrics.snapshot().await;
        let metrics_json = search_metrics_json(&metrics);
        let coalescer_json = search_coalescer_json(&ctx.search_request_coalescer().snapshot());

        let items = search_results.items();
        let results: Vec<_> = items
            .iter()
            .filter(|result| !safe_search_result_url(result).is_empty())
            .take(limit)
            .collect();

        // Report engine errors if any
        let errors = search_results.errors();
        let engine_errors = errors
            .iter()
            .map(|(engine, error)| {
                serde_json::json!({
                    "engine": engine,
                    "message": crate::text::truncate_utf8(
                        &sanitize_http_urls(&error.to_string()),
                        512,
                    ),
                })
            })
            .collect::<Vec<_>>();
        let engine_failures = failure_metadata(search_results.failures());
        let error_note = if errors.is_empty() {
            String::new()
        } else {
            let mut note = String::from("\nEngine errors:\n");
            for (engine, error) in errors {
                note.push_str(&format!("  - {}: {}\n", engine, error));
            }
            note
        };
        let notice_note = text_notice_note(&notices);

        if results.is_empty() {
            let metadata = serde_json::json!({
                "status": if requirements_met && errors.is_empty() { "complete" } else { "failed" },
                "engine_selection_source": engine_selection_source,
                "selected_engines": &selected_engines,
                "engine_fallback": (tier_reports.len() > 1).then_some("structurally_gated_tiers"),
                "notices": &notices,
                "search_fallback": &search_fallback,
                "retrieval_health": &retrieval_health,
                "retrieval_requirements": &retrieval_requirements,
                "search_tiers": &tier_reports,
                "engine_outcomes": outcome_metadata(search_results.outcomes()),
                "search_metrics": metrics_json,
                "search_coalescing": coalescer_json,
                "engine_errors": engine_errors,
                "engine_failures": engine_failures,
            });
            let message = format!(
                "No results found for query: \"{}\"{}{}",
                query_str, notice_note, error_note
            );
            if requirements_met && errors.is_empty() {
                return Ok(ToolOutput::success(message).with_metadata(metadata));
            }
            let mut output = ToolOutput::error(message).with_metadata(metadata);
            if let Some(error_kind) =
                tool_error_kind_for_failures(search_results.failures(), total_timeout)
            {
                output = output.with_error_kind(error_kind);
            }
            return Ok(output);
        }

        let (output, source_anchors, returned_result_count) = if output_format == "json" {
            let json_results = bounded_json_search_results(&results, full_text_bytes);
            let returned_result_count = json_results.len();
            if returned_result_count < results.len() {
                notices.push(format!(
                    "JSON output retained {returned_result_count} of {} usable results within the bounded tool transport; use a narrower query or lower limit to retrieve additional results.",
                    results.len()
                ));
            }
            let source_anchors = json_results
                .iter()
                .filter_map(|result| result.get("url").and_then(serde_json::Value::as_str))
                .map(str::to_string)
                .collect::<Vec<_>>();
            (
                serde_json::to_string_pretty(&json_search_payload(
                    json_results,
                    requirements_met,
                    &retrieval_health,
                    &retrieval_requirements,
                ))
                .unwrap_or_default(),
                source_anchors,
                returned_result_count,
            )
        } else {
            let mut text = format!(
                "Search results for \"{}\" ({} results, {}ms):\n\n",
                query_str,
                results.len(),
                search_results.duration_ms,
            );
            for (i, result) in results.iter().enumerate() {
                text.push_str(&text_search_result(i, result));
            }
            if !notice_note.is_empty() {
                text.push_str(&notice_note);
            }
            if !error_note.is_empty() {
                text.push_str(&error_note);
            }
            let source_anchors = results
                .iter()
                .map(|result| safe_search_result_url(result))
                .filter(|url| !url.is_empty())
                .collect::<Vec<_>>();
            (text, source_anchors, results.len())
        };

        let tool_output = if requirements_met {
            ToolOutput::success(output)
        } else {
            ToolOutput::error(output)
        };
        Ok(
            tool_output.with_metadata(serde_json::json!({
                "status": if !requirements_met { "failed" } else if errors.is_empty() { "complete" } else { "partial" },
                "engine_selection_source": engine_selection_source,
                "selected_engines": &selected_engines,
                "source_anchors": source_anchors,
                "engine_fallback": (tier_reports.len() > 1).then_some("structurally_gated_tiers"),
                "notices": &notices,
                "search_fallback": &search_fallback,
                "retrieval_health": &retrieval_health,
                "retrieval_requirements": &retrieval_requirements,
                "search_tiers": &tier_reports,
                "engine_outcomes": outcome_metadata(search_results.outcomes()),
                "search_metrics": metrics_json,
                "search_coalescing": coalescer_json,
                "engine_errors": engine_errors,
                "engine_failures": engine_failures,
                "available_result_count": results.len(),
                "returned_result_count": returned_result_count,
                "output_limited": returned_result_count < results.len(),
            })),
        )
    }
}

/// Parse a proxy URL string like "http://host:port" into a ProxyConfig
fn parse_proxy_url(url: &str) -> Option<ProxyConfig> {
    let url = url.trim();
    if url.is_empty() {
        return None;
    }

    // Parse scheme
    let (scheme, rest) = if let Some(rest) = url.strip_prefix("socks5://") {
        ("socks5", rest)
    } else if let Some(rest) = url.strip_prefix("https://") {
        ("https", rest)
    } else if let Some(rest) = url.strip_prefix("http://") {
        ("http", rest)
    } else {
        ("http", url)
    };

    // Parse host:port
    let (host, port) = {
        let colon_pos = rest.rfind(':')?;
        let host = &rest[..colon_pos];
        let port_str = &rest[colon_pos + 1..];
        match port_str.parse::<u16>() {
            Ok(p) => (host, p),
            Err(_) => return None,
        }
    };

    let mut config = ProxyConfig::new(host, port);
    config = match scheme {
        "socks5" => config.with_protocol(a3s_search::proxy::ProxyProtocol::Socks5),
        "https" => config.with_protocol(a3s_search::proxy::ProxyProtocol::Https),
        _ => config, // default is Http
    };

    Some(config)
}

#[cfg(test)]
#[path = "web_search/tests.rs"]
mod tests;