adk-rag 3.0.0

Retrieval-Augmented Generation for Rust Agent Development Kit (ADK-Rust) agents
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
//! Agentic retrieval tool for ADK agents.
//!
//! The [`RagTool`] wraps a [`RagPipeline`] as an
//! [`adk_core::Tool`] so that agents can perform retrieval as a tool call.
//!
//! The tool searches only the collections it is configured with. A one-collection
//! tool ([`RagTool::new`]) offers the model no `collection` argument; a tool given more
//! collections with [`RagTool::with_allowed_collections`] lists them in its schema and
//! refuses any other name.
//!
//! # Example
//!
//! ```rust,ignore
//! use std::sync::Arc;
//! use adk_rag::{RagPipeline, RagTool};
//!
//! let pipeline = Arc::new(build_pipeline()?);
//! let tool = RagTool::new(pipeline, "docs").with_allowed_collections(["faq"]);
//!
//! // The agent calls the tool with:
//! // { "query": "How do I configure X?", "collection": "faq", "top_k": 5 }
//! // and receives, best first:
//! // [{ "id": "...", "document_id": "...", "text": "...", "metadata": {...},
//! //    "source_uri": "https://...", "title": "...", "score": 0.83 }]
//! ```

use std::collections::HashMap;
use std::sync::Arc;

use adk_core::{
    AdkError, ErrorCategory, ErrorComponent, TOOL_INVALID_ARGUMENTS_CODE, Tool, ToolContext,
};
use async_trait::async_trait;
use serde::Serialize;
use serde_json::{Value, json};
use tracing::{error, info, warn};

use crate::document::SearchResult;
use crate::filter::{Field, Filter};
use crate::pipeline::{QueryOptions, RagPipeline};
use crate::scope::Scope;

/// Resolves the scope a `rag_search` call searches from the call's context.
type ScopeResolver = dyn Fn(&dyn ToolContext) -> adk_core::Result<Scope> + Send + Sync;

/// A retrieval tool that wraps a [`RagPipeline`] for agentic use.
///
/// Implements [`adk_core::Tool`] so it can be attached to any ADK agent.
/// The tool accepts a required `query` string and an optional `top_k`. Every query
/// runs through the pipeline's reranker and similarity threshold, including one that
/// overrides `top_k`.
///
/// The collections are configuration, not model input. [`RagTool::new`] binds the
/// tool to one collection and its schema has no `collection` argument.
/// [`RagTool::with_allowed_collections`] lets the model choose among further
/// collections, which the schema lists; a call that names any other collection fails
/// with `tool.invalid_arguments` before the vector store is queried. One collection per
/// tenant therefore needs one tool per tenant, built with that tenant's collection.
///
/// The result is an array of retrieved chunks, best first, each with its `id`,
/// `document_id`, `text`, `metadata`, and `score`, plus the chunk's `source_uri` and
/// `title` when its document had them, so the model can cite the source. Embedding
/// vectors stay out of the result: they mean nothing to a model and would fill its
/// context.
///
/// Every result with a source URI is recorded as an [`adk_core::GroundingSource`] on the
/// call's actions, so an agent's final answer can cite it and
/// `adk_guardrail::CitationRequired` can check the citation.
///
/// Each call searches one [`Scope`]: [`Scope::single_tenant`] unless
/// [`RagTool::with_scope`] fixes another or [`RagTool::with_scope_from`] resolves it from the
/// call's context. The model cannot name a scope. A policy filter
/// ([`RagTool::with_policy_filter`]) applies to every call, and the model may narrow results
/// further only on the metadata keys [`RagTool::with_model_filterable`] lists.
///
/// The tool is read-only and concurrency-safe, so `DeclarativePolicy::allow_read_only`
/// admits it and `ToolExecutionStrategy::Auto` overlaps it with adjacent read-only calls.
pub struct RagTool {
    pipeline: Arc<RagPipeline>,
    scope: Arc<ScopeResolver>,
    default_collection: String,
    /// Collections a call may name besides the default, in schema order.
    other_collections: Vec<String>,
    /// Applied to every call, beside any filter the model sends.
    policy_filter: Option<Filter>,
    /// Metadata keys the model may filter on, in schema order.
    model_filterable: Vec<String>,
}

impl RagTool {
    /// Create a new `RagTool` that searches `default_collection` only.
    ///
    /// The tool's schema has no `collection` argument. A call that still names a
    /// collection other than `default_collection` fails with `tool.invalid_arguments`.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use std::sync::Arc;
    /// use adk_rag::RagTool;
    ///
    /// // One tool per tenant, each bound to that tenant's collection.
    /// let tool = RagTool::new(Arc::clone(&pipeline), format!("kb_{tenant_id}"));
    /// ```
    pub fn new(pipeline: Arc<RagPipeline>, default_collection: impl Into<String>) -> Self {
        Self {
            pipeline,
            scope: Arc::new(|_: &dyn ToolContext| Ok(Scope::single_tenant())),
            default_collection: default_collection.into(),
            other_collections: vec![],
            policy_filter: None,
            model_filterable: vec![],
        }
    }

    /// Searches `scope` instead of [`Scope::single_tenant`].
    ///
    /// The scope is configuration, never a model argument: build the tool with the
    /// authenticated tenant's scope.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use std::sync::Arc;
    /// use adk_rag::{RagTool, Scope};
    ///
    /// let tool = RagTool::new(pipeline, "kb").with_scope(Scope::from_parts(["support", tenant_id])?);
    /// ```
    #[must_use]
    pub fn with_scope(mut self, scope: Scope) -> Self {
        self.scope = Arc::new(move |_: &dyn ToolContext| Ok(scope.clone()));
        self
    }

    /// Resolves each call's scope from its context, such as the authenticated user, instead
    /// of using one fixed scope.
    ///
    /// The resolver sees the call's [`ToolContext`] — app, user, session, state, identity —
    /// and never the model's arguments. An error fails the call before the vector store is
    /// queried.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use adk_rag::{RagTool, Scope};
    ///
    /// // Each user searches the documents ingested under their organization's scope.
    /// let tool = RagTool::new(pipeline, "kb").with_scope_from(|ctx| {
    ///     let org = ctx.state().get("app:org").and_then(|v| v.as_str().map(str::to_string));
    ///     let org = org.ok_or_else(|| adk_core::AdkError::tool("no organization in state"))?;
    ///     Ok(Scope::from_parts(["support", org.as_str()])?)
    /// });
    /// ```
    #[must_use]
    pub fn with_scope_from<F>(mut self, resolve: F) -> Self
    where
        F: Fn(&dyn ToolContext) -> adk_core::Result<Scope> + Send + Sync + 'static,
    {
        self.scope = Arc::new(resolve);
        self
    }

    /// Searches a scope per user: `Scope::from_parts([app_name, user_id])` of each call.
    ///
    /// Ingest each user's documents under the same scope.
    #[must_use]
    pub fn with_scope_per_user(self) -> Self {
        self.with_scope_from(|ctx| Ok(Scope::from_parts([ctx.app_name(), ctx.user_id()])?))
    }

    /// Applies `filter` to every call, AND-ed with any filter the model sends.
    ///
    /// The policy filter is configuration, such as `visibility = public` for an anonymous
    /// audience; the model can narrow its results but never widen them past it. Its metadata
    /// keys must be declared filterable on the collections the tool searches.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use adk_rag::{Field, Filter, RagTool};
    ///
    /// let tool = RagTool::new(pipeline, "kb")
    ///     .with_policy_filter(Filter::eq(Field::metadata("visibility"), "public"));
    /// ```
    #[must_use]
    pub fn with_policy_filter(mut self, filter: Filter) -> Self {
        self.policy_filter = Some(filter);
        self
    }

    /// Lets the model filter on these metadata keys with a `filter` argument.
    ///
    /// The argument maps each key to a string (equal) or an array of strings (one of), and
    /// every entry must match. A key outside this list fails the call with
    /// `tool.invalid_arguments`. Each key must be declared filterable on the collections the
    /// tool searches.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// // The model may send {"query": "...", "filter": {"section": ["billing", "refunds"]}}.
    /// let tool = RagTool::new(pipeline, "kb").with_model_filterable(["section", "lang"]);
    /// ```
    #[must_use]
    pub fn with_model_filterable<I, S>(mut self, keys: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.model_filterable.clear();
        for key in keys.into_iter().map(Into::into) {
            if !self.model_filterable.contains(&key) {
                self.model_filterable.push(key);
            }
        }
        self
    }

    /// The filter the model's `filter` argument asks for, limited to model-filterable keys.
    fn model_filter(&self, argument: Option<&Value>) -> adk_core::Result<Option<Filter>> {
        let entries = match argument {
            None | Some(Value::Null) => return Ok(None),
            Some(Value::Object(entries)) if entries.is_empty() => return Ok(None),
            Some(Value::Object(entries)) => entries,
            Some(_) => return Err(invalid_arguments("'filter' must be an object")),
        };
        let allowed = self.model_filterable.join("', '");
        let mut filters = Vec::with_capacity(entries.len());
        for (key, value) in entries {
            if !self.model_filterable.contains(key) {
                return Err(invalid_arguments(&format!(
                    "'filter' cannot use '{key}'; the filterable keys are '{allowed}'"
                )));
            }
            let field = Field::metadata(key.clone());
            filters.push(match value {
                Value::String(value) => Filter::eq(field, value.clone()),
                Value::Array(values) if !values.is_empty() => {
                    let values = values
                        .iter()
                        .map(|value| value.as_str().map(str::to_string))
                        .collect::<Option<Vec<_>>>()
                        .ok_or_else(|| {
                            invalid_arguments(&format!("'filter.{key}' must hold strings"))
                        })?;
                    Filter::is_in(field, values)
                }
                _ => {
                    return Err(invalid_arguments(&format!(
                        "'filter.{key}' must be a string or a non-empty array of strings"
                    )));
                }
            });
        }
        Ok(Some(Filter::and(filters)))
    }

    /// Replace the collections the model may name besides the default.
    ///
    /// The schema's `collection` argument lists the default first, then these. A call
    /// without `collection` searches the default; a call naming a collection outside
    /// the list fails with `tool.invalid_arguments`, whose message names the allowed
    /// collections. An empty list returns the tool to its one-collection form.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use std::sync::Arc;
    /// use adk_rag::RagTool;
    ///
    /// // Searches "docs" by default; the model may also choose "faq" or "changelog".
    /// let tool = RagTool::new(pipeline, "docs").with_allowed_collections(["faq", "changelog"]);
    /// ```
    #[must_use]
    pub fn with_allowed_collections<I, S>(mut self, collections: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.other_collections.clear();
        for collection in collections.into_iter().map(Into::into) {
            if collection != self.default_collection
                && !self.other_collections.contains(&collection)
            {
                self.other_collections.push(collection);
            }
        }
        self
    }

    /// The default collection, then the others a call may name.
    fn allowed_collections(&self) -> impl Iterator<Item = &str> {
        std::iter::once(&self.default_collection).chain(&self.other_collections).map(String::as_str)
    }

    /// Tells the model which collections it may name.
    fn collection_guidance(&self) -> String {
        let default = &self.default_collection;
        if self.other_collections.is_empty() {
            return format!("it searches only '{default}', so omit 'collection'");
        }
        let allowed = self
            .allowed_collections()
            .map(|name| format!("'{name}'"))
            .collect::<Vec<_>>()
            .join(", ");
        format!("choose one of {allowed}, or omit 'collection' to search '{default}'")
    }
}

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

    fn description(&self) -> &str {
        "Search a knowledge base for relevant documents given a query"
    }

    fn parameters_schema(&self) -> Option<Value> {
        let mut properties = json!({
            "query": {
                "type": "string",
                "description": "The search query to find relevant documents"
            },
            "top_k": {
                "type": "integer",
                "description": "Maximum number of results to return. Uses the pipeline default if omitted."
            }
        });
        if !self.model_filterable.is_empty() {
            let mut keys = serde_json::Map::new();
            for key in &self.model_filterable {
                keys.insert(
                    key.clone(),
                    json!({
                        "anyOf": [
                            { "type": "string" },
                            { "type": "array", "items": { "type": "string" }, "minItems": 1 }
                        ]
                    }),
                );
            }
            properties["filter"] = json!({
                "type": "object",
                "properties": keys,
                "additionalProperties": false,
                "description": "Only return results whose metadata matches every entry: a string \
                                must equal the value, an array must contain it."
            });
        }
        if !self.other_collections.is_empty() {
            let allowed: Vec<&str> = self.allowed_collections().collect();
            properties["collection"] = json!({
                "type": "string",
                "enum": allowed,
                "description": format!(
                    "The collection to search. Searches '{}' if omitted.",
                    self.default_collection
                )
            });
        }
        Some(json!({
            "type": "object",
            "properties": properties,
            "required": ["query"]
        }))
    }

    fn is_read_only(&self) -> bool {
        true
    }

    fn is_concurrency_safe(&self) -> bool {
        true
    }

    async fn execute(&self, ctx: Arc<dyn ToolContext>, args: Value) -> adk_core::Result<Value> {
        let query = args
            .get("query")
            .and_then(Value::as_str)
            .ok_or_else(|| invalid_arguments("missing required 'query' parameter"))?;

        let collection = match args.get("collection") {
            None | Some(Value::Null) => self.default_collection.as_str(),
            Some(Value::String(requested))
                if self.allowed_collections().any(|allowed| allowed == requested) =>
            {
                requested.as_str()
            }
            Some(Value::String(requested)) => {
                warn!(
                    collection = requested.as_str(),
                    default_collection = %self.default_collection,
                    "rag_search refused a collection outside its allowlist"
                );
                return Err(invalid_arguments(&format!(
                    "collection '{requested}' is not searchable by this tool; {}",
                    self.collection_guidance()
                )));
            }
            Some(_) => {
                return Err(invalid_arguments(&format!(
                    "'collection' must be a string; {}",
                    self.collection_guidance()
                )));
            }
        };

        let top_k = match args.get("top_k") {
            None | Some(Value::Null) => self.pipeline.config().top_k,
            // Some providers send integers as integral floats.
            Some(value) => value
                .as_u64()
                .or_else(|| {
                    value.as_f64().filter(|f| f.fract() == 0.0 && *f >= 1.0).map(|f| f as u64)
                })
                .filter(|top_k| *top_k > 0)
                .and_then(|top_k| usize::try_from(top_k).ok())
                .ok_or_else(|| invalid_arguments("'top_k' must be a positive integer"))?,
        };

        let model_filter = self.model_filter(args.get("filter"))?;
        let scope = (self.scope)(ctx.as_ref())?;
        info!(query, collection, top_k, scope = scope.as_str(), "rag_search tool called");

        let mut options = QueryOptions::new().with_top_k(top_k);
        let filters: Vec<Filter> = self.policy_filter.iter().cloned().chain(model_filter).collect();
        if !filters.is_empty() {
            options = options.with_filter(Filter::and(filters));
        }
        let results =
            self.pipeline.scoped(scope).query_with(collection, query, &options).await.map_err(
                |e| {
                    error!(error = %e, "rag_search failed");
                    AdkError::tool(format!("RAG search failed: {e}"))
                },
            )?;

        // Each result with a source URI is a source the final answer may cite.
        adk_core::record_grounding_sources(
            ctx.as_ref(),
            results.iter().filter_map(|result| {
                let chunk = &result.chunk;
                let mut source =
                    adk_core::GroundingSource::new(self.name(), chunk.source_uri.clone()?)
                        .with_document_id(chunk.document_id.clone())
                        .with_chunk_id(chunk.id.clone());
                if let Some(title) = &chunk.title {
                    source = source.with_title(title.clone());
                }
                Some(source)
            }),
        );

        let retrieved: Vec<RetrievedChunk<'_>> = results.iter().map(RetrievedChunk::from).collect();
        serde_json::to_value(&retrieved).map_err(|e| {
            error!(error = %e, "failed to serialize search results");
            AdkError::tool(format!("failed to serialize results: {e}"))
        })
    }
}

/// One search result as the model receives it: everything but the embedding.
#[derive(Serialize)]
struct RetrievedChunk<'a> {
    id: &'a str,
    document_id: &'a str,
    text: &'a str,
    metadata: &'a HashMap<String, String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    source_uri: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    title: Option<&'a str>,
    score: f32,
}

impl<'a> From<&'a SearchResult> for RetrievedChunk<'a> {
    fn from(result: &'a SearchResult) -> Self {
        Self {
            id: &result.chunk.id,
            document_id: &result.chunk.document_id,
            text: &result.chunk.text,
            metadata: &result.chunk.metadata,
            source_uri: result.chunk.source_uri.as_deref(),
            title: result.chunk.title.as_deref(),
            score: result.score,
        }
    }
}

/// An argument error the model sees in full, so it can correct the call.
fn invalid_arguments(message: &str) -> AdkError {
    AdkError::new(
        ErrorComponent::Tool,
        ErrorCategory::InvalidInput,
        TOOL_INVALID_ARGUMENTS_CODE,
        format!("rag_search: {message}"),
    )
}

#[cfg(test)]
mod tests {
    use std::sync::atomic::{AtomicUsize, Ordering};

    use async_trait::async_trait;

    use super::*;
    use crate::chunking::FixedSizeChunker;
    use crate::config::RagConfig;
    use crate::document::Document;
    use crate::embedding::EmbeddingProvider;
    use crate::inmemory::InMemoryVectorStore;
    use crate::reranker::Reranker;

    /// Embeds each text as its length and vowel count, so results are deterministic.
    struct CountingEmbedder;

    #[async_trait]
    impl EmbeddingProvider for CountingEmbedder {
        async fn embed(&self, text: &str) -> crate::error::Result<Vec<f32>> {
            let vowels = text.chars().filter(|c| "aeiou".contains(*c)).count();
            Ok(vec![text.len() as f32, vowels as f32, 1.0])
        }

        fn dimensions(&self) -> usize {
            3
        }
    }

    /// Reverses the search order and counts how often it ran.
    #[derive(Default)]
    struct ReversingReranker {
        calls: AtomicUsize,
    }

    #[async_trait]
    impl Reranker for ReversingReranker {
        async fn rerank(
            &self,
            _query: &str,
            mut results: Vec<SearchResult>,
        ) -> crate::error::Result<Vec<SearchResult>> {
            self.calls.fetch_add(1, Ordering::SeqCst);
            results.reverse();
            Ok(results)
        }
    }

    /// A pipeline with three collections: "kb" holds documents a, b, and c, "faq" holds
    /// "faq", and "tenant_b" holds "secret", all in the single-tenant scope; "kb" also holds
    /// another tenant's document "a".
    async fn pipeline(reranker: Option<Arc<ReversingReranker>>) -> Arc<RagPipeline> {
        let mut builder = RagPipeline::builder()
            .config(RagConfig::builder().top_k(3).build().expect("valid config"))
            .embedding_provider(Arc::new(CountingEmbedder))
            .vector_store(Arc::new(InMemoryVectorStore::new()))
            .chunker(Arc::new(FixedSizeChunker::try_new(64, 0).expect("valid sizes")));
        if let Some(reranker) = reranker {
            builder = builder.reranker(reranker);
        }
        let pipeline = Arc::new(builder.build().expect("build pipeline"));
        for (collection, id, text) in [
            ("kb", "a", "returns within thirty days"),
            ("kb", "b", "ships in two days"),
            ("kb", "c", "warranty"),
            ("faq", "faq", "returns are free"),
            ("tenant_b", "secret", "returns for tenant b only"),
        ] {
            pipeline.create_collection(collection).await.expect("create collection");
            let mut document = Document::new(id, text).with_metadata("source", format!("{id}.md"));
            if id == "a" {
                document = document.with_source_uri("https://example.com/a").with_title("Returns");
            }
            pipeline
                .scoped(Scope::single_tenant())
                .ingest(collection, &document)
                .await
                .expect("ingest");
        }
        // Another tenant's document in the shared "kb" collection, under the same ID as "a".
        pipeline
            .scoped(other_tenant())
            .ingest("kb", &Document::new("a", "returns for the other tenant"))
            .await
            .expect("ingest");
        pipeline
    }

    fn other_tenant() -> Scope {
        Scope::from_parts(["kb-test", "other"]).expect("valid scope")
    }

    async fn tool(reranker: Option<Arc<ReversingReranker>>) -> RagTool {
        RagTool::new(pipeline(reranker).await, "kb")
    }

    /// The document ids a search returned, sorted.
    fn document_ids(result: &Value) -> Vec<&str> {
        let mut ids: Vec<&str> = result
            .as_array()
            .expect("an array of results")
            .iter()
            .map(|item| item["document_id"].as_str().expect("document_id"))
            .collect();
        ids.sort_unstable();
        ids
    }

    fn context() -> Arc<dyn ToolContext> {
        Arc::new(adk_tool::SimpleToolContext::new("rag-tool-test"))
    }

    #[tokio::test]
    async fn results_carry_text_metadata_and_score_but_no_embedding() {
        let result =
            tool(None).await.execute(context(), json!({"query": "returns"})).await.expect("search");

        let results = result.as_array().expect("an array of results");
        assert_eq!(results.len(), 3);
        for item in results {
            // Key order depends on serde_json's `preserve_order` feature.
            let mut keys: Vec<&str> =
                item.as_object().expect("object").keys().map(String::as_str).collect();
            keys.sort_unstable();
            if item["document_id"] == "a" {
                assert_eq!(
                    keys,
                    ["document_id", "id", "metadata", "score", "source_uri", "text", "title"]
                );
            } else {
                assert_eq!(keys, ["document_id", "id", "metadata", "score", "text"]);
            }
        }
        let first = &results[0];
        assert_eq!(
            first["metadata"]["source"],
            format!("{}.md", first["document_id"].as_str().unwrap())
        );
        assert!(!result.to_string().contains("embedding"));
    }

    #[tokio::test]
    async fn results_with_a_source_uri_are_recorded_as_grounding_sources() {
        let ctx = context();
        tool(None)
            .await
            .execute(ctx.clone(), json!({"query": "returns", "top_k": 3}))
            .await
            .expect("search");
        assert_eq!(
            ctx.actions().grounding_sources,
            [adk_core::GroundingSource::new("rag_search", "https://example.com/a")
                .with_title("Returns")
                .with_document_id("a")
                .with_chunk_id("a_0")]
        );
    }

    #[tokio::test]
    async fn results_carry_the_source_uri_and_title_of_documents_that_have_them() {
        let result = tool(None)
            .await
            .execute(context(), json!({"query": "returns", "top_k": 3}))
            .await
            .expect("search");

        let mut provenance: Vec<(String, Value, Value)> = result
            .as_array()
            .expect("an array of results")
            .iter()
            .map(|item| {
                let id = item["document_id"].as_str().expect("document_id").to_string();
                let field = |name: &str| item.get(name).cloned().unwrap_or(Value::Null);
                (id, field("source_uri"), field("title"))
            })
            .collect();
        provenance.sort_by(|a, b| a.0.cmp(&b.0));
        assert_eq!(
            provenance,
            [
                ("a".to_string(), json!("https://example.com/a"), json!("Returns")),
                ("b".to_string(), Value::Null, Value::Null),
                ("c".to_string(), Value::Null, Value::Null),
            ]
        );
    }

    #[tokio::test]
    async fn the_tool_searches_only_its_configured_scope() {
        let pipeline = pipeline(None).await;
        let texts = |result: Value| -> Vec<String> {
            let mut texts: Vec<String> = result
                .as_array()
                .expect("an array of results")
                .iter()
                .map(|item| item["text"].as_str().expect("text").to_string())
                .collect();
            texts.sort();
            texts
        };

        let single = RagTool::new(pipeline.clone(), "kb");
        let result = single
            .execute(context(), json!({"query": "returns", "top_k": 10}))
            .await
            .expect("search");
        assert_eq!(texts(result), ["returns within thirty days", "ships in two days", "warranty"]);

        // A scope argument from the model is not a parameter and changes nothing.
        let other = RagTool::new(pipeline, "kb").with_scope(other_tenant());
        let result = other
            .execute(context(), json!({"query": "returns", "top_k": 10, "scope": ":single-tenant"}))
            .await
            .expect("search");
        assert_eq!(texts(result), ["returns for the other tenant"]);
    }

    #[tokio::test]
    async fn a_top_k_override_is_reranked_like_the_default_query() {
        let reranker = Arc::new(ReversingReranker::default());
        let tool = tool(Some(reranker.clone())).await;
        let default = tool.execute(context(), json!({"query": "returns"})).await.expect("search");
        let overridden =
            tool.execute(context(), json!({"query": "returns", "top_k": 3})).await.expect("search");

        assert_eq!(reranker.calls.load(Ordering::SeqCst), 2, "both queries are reranked");
        assert_eq!(overridden, default);
        let limited = tool
            .execute(context(), json!({"query": "returns", "top_k": 2.0}))
            .await
            .expect("search");
        assert_eq!(limited.as_array().map(Vec::len), Some(2));
    }

    #[tokio::test]
    async fn a_top_k_that_is_not_a_positive_integer_is_an_argument_error() {
        let tool = tool(None).await;
        for top_k in [json!(0), json!(-1), json!(1.5), json!("3")] {
            let error = tool
                .execute(context(), json!({"query": "returns", "top_k": top_k}))
                .await
                .expect_err("invalid top_k");
            assert_eq!(error.code, TOOL_INVALID_ARGUMENTS_CODE, "top_k {top_k}");
        }
    }

    #[tokio::test]
    async fn a_one_collection_tool_offers_no_collection_and_refuses_another() {
        let tool = tool(None).await;
        assert_eq!(
            tool.parameters_schema(),
            Some(json!({
                "type": "object",
                "properties": {
                    "query": {
                        "type": "string",
                        "description": "The search query to find relevant documents"
                    },
                    "top_k": {
                        "type": "integer",
                        "description": "Maximum number of results to return. Uses the pipeline default if omitted."
                    }
                },
                "required": ["query"]
            }))
        );

        let named = tool
            .execute(context(), json!({"query": "returns", "collection": "kb"}))
            .await
            .expect("its own collection");
        let omitted = tool.execute(context(), json!({"query": "returns"})).await.expect("search");
        assert_eq!(named, omitted);

        // "tenant_b" exists in the store, so only the allowlist keeps it out.
        let error = tool
            .execute(context(), json!({"query": "returns", "collection": "tenant_b"}))
            .await
            .expect_err("another collection");
        assert_eq!(
            (error.category, error.code, error.message.as_str()),
            (
                ErrorCategory::InvalidInput,
                TOOL_INVALID_ARGUMENTS_CODE,
                "rag_search: collection 'tenant_b' is not searchable by this tool; it searches \
                 only 'kb', so omit 'collection'"
            )
        );
    }

    #[tokio::test]
    async fn an_allowed_collection_is_searched_and_any_other_is_refused() {
        let tool =
            RagTool::new(pipeline(None).await, "kb").with_allowed_collections(["faq", "kb", "faq"]);
        assert_eq!(
            tool.parameters_schema().map(|schema| schema["properties"]["collection"].clone()),
            Some(json!({
                "type": "string",
                "enum": ["kb", "faq"],
                "description": "The collection to search. Searches 'kb' if omitted."
            }))
        );

        let faq = tool
            .execute(context(), json!({"query": "returns", "collection": "faq"}))
            .await
            .expect("an allowed collection");
        assert_eq!(document_ids(&faq), ["faq"]);

        let allowed = "choose one of 'kb', 'faq', or omit 'collection' to search 'kb'";
        for (collection, message) in [
            (
                json!("tenant_b"),
                format!(
                    "rag_search: collection 'tenant_b' is not searchable by this tool; {allowed}"
                ),
            ),
            (json!(7), format!("rag_search: 'collection' must be a string; {allowed}")),
        ] {
            let error = tool
                .execute(context(), json!({"query": "returns", "collection": collection}))
                .await
                .expect_err("refused collection");
            assert_eq!(
                (error.category, error.code, error.message),
                (ErrorCategory::InvalidInput, TOOL_INVALID_ARGUMENTS_CODE, message)
            );
        }
    }

    #[tokio::test]
    async fn a_call_without_a_collection_searches_the_default() {
        let tool =
            RagTool::new(pipeline(None).await, "kb").with_allowed_collections(["faq", "tenant_b"]);
        for args in [json!({"query": "returns"}), json!({"query": "returns", "collection": null})] {
            let result = tool.execute(context(), args).await.expect("search");
            assert_eq!(document_ids(&result), ["a", "b", "c"]);
        }
    }

    #[tokio::test]
    async fn the_tool_is_read_only_and_concurrency_safe() {
        let tool = tool(None).await;
        assert_eq!(
            (tool.is_read_only(), tool.is_concurrency_safe(), tool.effect()),
            (true, true, adk_core::ToolEffect::ReadOnly)
        );
    }

    #[tokio::test]
    async fn a_scope_resolved_from_the_context_never_from_arguments() {
        let pipeline = pipeline(None).await;
        let user = Scope::from_parts(["rag-tool-test", "anonymous"]).expect("scope");
        pipeline
            .scoped(user)
            .ingest("kb", &Document::new("mine", "returns of this user"))
            .await
            .expect("ingest");

        let tool = RagTool::new(pipeline.clone(), "kb").with_scope_per_user();
        let result = tool
            .execute(context(), json!({"query": "returns", "top_k": 10, "scope": "x"}))
            .await
            .expect("search");
        assert_eq!(document_ids(&result), ["mine"]);

        let refusing = RagTool::new(pipeline, "kb")
            .with_scope_from(|_| Err(AdkError::tool("no tenant on this request")));
        let error =
            refusing.execute(context(), json!({"query": "returns"})).await.expect_err("refused");
        assert!(error.message.contains("no tenant"), "{error}");
    }

    async fn filtered_tool() -> RagTool {
        let pipeline = pipeline(None).await;
        pipeline
            .create_collection_with_filterable("policies", ["visibility", "section"])
            .await
            .expect("create");
        let rag = pipeline.scoped(Scope::single_tenant());
        for (id, visibility, section) in [
            ("public_billing", "public", "billing"),
            ("public_refunds", "public", "refunds"),
            ("private_billing", "private", "billing"),
        ] {
            let document = Document::new(id, format!("returns {id}"))
                .with_metadata("visibility", visibility)
                .with_metadata("section", section);
            rag.ingest("policies", &document).await.expect("ingest");
        }
        RagTool::new(pipeline, "policies")
            .with_policy_filter(Filter::eq(Field::metadata("visibility"), "public"))
            .with_model_filterable(["section"])
    }

    #[tokio::test]
    async fn the_policy_filter_always_applies_and_the_model_may_only_narrow() {
        let tool = filtered_tool().await;
        let search = |args: Value| tool.execute(context(), args);

        let all = search(json!({"query": "returns", "top_k": 10})).await.expect("search");
        assert_eq!(document_ids(&all), ["public_billing", "public_refunds"]);
        let billing = search(json!({"query": "returns", "filter": {"section": "billing"}}))
            .await
            .expect("search");
        assert_eq!(document_ids(&billing), ["public_billing"]);
        let either = search(json!({"query": "returns", "filter": {"section": ["refunds", "x"]}}))
            .await
            .expect("search");
        assert_eq!(document_ids(&either), ["public_refunds"]);

        for filter in [
            json!({"visibility": "private"}),
            json!({"section": 3}),
            json!({"section": []}),
            json!("section=billing"),
        ] {
            let error = search(json!({"query": "returns", "filter": filter}))
                .await
                .expect_err("invalid filter");
            assert_eq!(error.code, TOOL_INVALID_ARGUMENTS_CODE, "{filter}");
        }

        let schema = tool.parameters_schema().expect("schema");
        let keys: Vec<&String> = schema["properties"]["filter"]["properties"]
            .as_object()
            .expect("keys")
            .keys()
            .collect();
        assert_eq!(keys, ["section"]);
    }
}