knot 1.11.6

Codebase Graph + Vector RAG Indexer for Java, TypeScript, JavaScript, Kotlin, Rust, Python, Groovy, C/C++, C#, Build Systems, and HTML/CSS codebases
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
use anyhow::Context;
use anyhow::Result;
use qdrant_client::qdrant::{Condition, Filter, SearchPoints, WithPayloadSelector};

use super::{VectorDb, utils};

/// Build the Qdrant payload filter for a search: repo scope and optional
/// entity-kind filter as exact keyword `must` conditions, plus an optional
/// kind **exclusion** list as `must_not` (the search definition channel
/// uses it to drop prose / config / infra embeddings from a cosine pass).
/// Empty slices are omitted; a filter with no conditions at all is `None`.
pub(crate) fn build_search_filter(
    repo_names: &[String],
    kinds: &[String],
    exclude_kinds: &[String],
) -> Option<Filter> {
    let mut must = Vec::new();
    if !repo_names.is_empty() {
        must.push(utils::any_keyword_condition("repo_name", repo_names));
    }
    if !kinds.is_empty() {
        must.push(utils::any_keyword_condition("kind", kinds));
    }
    let must_not = (!exclude_kinds.is_empty())
        .then(|| vec![utils::any_keyword_condition("kind", exclude_kinds)]);
    if must.is_empty() && must_not.is_none() {
        return None;
    }
    Some(Filter {
        must,
        must_not: must_not.unwrap_or_default(),
        should: Vec::new(),
        min_should: None,
    })
}

/// Build the name-exact probe filter: one `must` arm whose `should` group
/// admits entities matching an exact name OR sharing an identifier token
/// with the query, plus the repo/kind scope arms. `None` when there is
/// nothing lexical to probe (the caller then runs an unfiltered probe).
pub(crate) fn build_probe_filter(
    names: &[String],
    tokens: &[String],
    repo_names: &[String],
    kinds: &[String],
) -> Option<Filter> {
    let mut should = Vec::new();
    if !names.is_empty() {
        should.push(utils::any_keyword_condition("name", names));
    }
    if !tokens.is_empty() {
        should.push(utils::any_keyword_condition("name_tokens", tokens));
    }
    let mut must = Vec::new();
    match (names.is_empty(), tokens.is_empty()) {
        (true, true) => {
            // Nothing lexical to probe: fall back to kind/repo-filtered
            // vector search rather than an unscoped full scan.
        }
        _ => {
            let condition: Condition = Filter::should(should).into();
            must.push(condition);
        }
    }
    if !repo_names.is_empty() {
        must.push(utils::any_keyword_condition("repo_name", repo_names));
    }
    if !kinds.is_empty() {
        must.push(utils::any_keyword_condition("kind", kinds));
    }
    (!must.is_empty()).then(|| Filter::must(must))
}

/// Parameters for the name-exact probe search — bundled in one struct to
/// keep [`VectorSearchExt::search_exact_names`] within clippy's arity
/// threshold.
pub struct ExactNameProbe<'a> {
    /// Query embedding.
    pub vector: &'a [f32],
    /// Entity names to match exactly (case-sensitive keyword OR).
    pub names: &'a [String],
    /// Lowercase identifier tokens (from the query text) to match against
    /// the indexed `name_tokens` payload: an entity whose identifier shares
    /// a word with the query (camel/snake-case sliced) must enter the
    /// candidate pool even when the query never names it verbatim.
    ///
    /// Recall semantics: the probe stays a vector search *restricted to*
    /// lexically-related entities, so the best cosine hits inside the
    /// lexical set are returned, not an unfiltered name scan.
    pub tokens: &'a [String],
    /// Maximum number of hits to return.
    pub limit: usize,
    /// Repository scope (empty = all repositories).
    pub repo_names: &'a [String],
    /// Wire-format entity kinds to restrict hits to (empty = all kinds).
    pub kinds: &'a [String],
}

/// Parameters for a kind-scoped cosine search with an optional exclusion —
/// bundled in one struct to keep [`VectorSearchExt::search_excluding_kinds`]
/// within clippy's arity threshold.
pub struct KindScopeSearch<'a> {
    /// Query embedding.
    pub vector: &'a [f32],
    /// Maximum number of hits to return.
    pub limit: usize,
    /// Repository scope (empty = all repositories).
    pub repo_names: &'a [String],
    /// Wire-format entity kinds to restrict hits to (empty = all kinds).
    pub kinds: &'a [String],
    /// Wire-format entity kinds to exclude (`must_not`); empty = none.
    pub exclude_kinds: &'a [String],
}

/// Extension trait for query and search operations.
#[expect(
    async_fn_in_trait,
    reason = "async trait method is required for the db interfaces"
)]
pub trait VectorSearchExt {
    /// Search for similar vectors in Qdrant.
    ///
    /// `repo_names` scopes the search to repositories (empty = all);
    /// `kinds` restricts hits to wire-format entity kinds (empty = all).
    /// Each result carries its cosine similarity in the `score` field so
    /// callers can re-rank.
    async fn search(
        &self,
        vector: &[f32],
        limit: usize,
        repo_names: &[String],
        kinds: &[String],
    ) -> Result<Vec<serde_json::Value>>;

    /// Search restricted to entities whose *payload kind* is neither in
    /// `request.kinds` (when non-empty) nor in `request.exclude_kinds`.
    /// `search` delegates here with no exclusions; `search_hybrid_context`
    /// runs this as a second cosine pass that excludes prose / config /
    /// infra kinds, so a documentation-heavy repository still fills the
    /// candidate pool with the code definitions a natural-language query
    /// is looking for.
    async fn search_excluding_kinds(
        &self,
        request: KindScopeSearch<'_>,
    ) -> Result<Vec<serde_json::Value>>;

    /// Search restricted to entities whose payload `name` exactly matches
    /// one of the probe names (case-sensitive keyword OR). Used as a
    /// name-exact probe: an identifier the query literally names must enter
    /// the candidate pool with its true cosine, however deep it ranks.
    async fn search_exact_names(&self, probe: ExactNameProbe<'_>)
    -> Result<Vec<serde_json::Value>>;

    /// Scored search restricted to a fixed set of point UUIDs. Used by the
    /// caller-recall bridge in `search_hybrid_context`: entities chosen by
    /// a graph query (without vectors) still need their true cosine to the
    /// query before re-ranking.
    async fn search_by_uuids(&self, probe: UuidProbe<'_>) -> Result<Vec<serde_json::Value>>;
}

/// Parameters for the UUID-restricted scored search — bundled in one
/// struct to keep [`VectorSearchExt::search_by_uuids`] within clippy's
/// arity threshold.
pub struct UuidProbe<'a> {
    /// Query embedding.
    pub vector: &'a [f32],
    /// Point UUIDs to score (keyword OR on the `uuid` payload).
    pub uuids: &'a [String],
    /// Repository scope (empty = all repositories).
    pub repo_names: &'a [String],
    /// Wire-format entity kinds to restrict hits to (empty = all kinds).
    pub kinds: &'a [String],
    /// Maximum number of hits to return.
    pub limit: usize,
}

/// Run a scored points search with the given filter and map hits to JSON
/// payloads carrying the cosine `score`.
async fn scored_search(
    db: &VectorDb,
    vector: &[f32],
    limit: usize,
    filter: Option<Filter>,
) -> Result<Vec<serde_json::Value>> {
    let search_request = SearchPoints {
        collection_name: db.collection.clone(),
        vector: vector.to_vec(),
        limit: limit as u64,
        with_payload: Some(WithPayloadSelector {
            selector_options: Some(
                qdrant_client::qdrant::with_payload_selector::SelectorOptions::Enable(true),
            ),
        }),
        filter,
        ..Default::default()
    };

    let search_result = db
        .client
        .search_points(search_request)
        .await
        .context("Failed to search Qdrant")?;

    Ok(search_result
        .result
        .into_iter()
        .filter_map(|scored_point| {
            if !scored_point.payload.is_empty() {
                let mut json_obj = serde_json::json!({});
                for (key, value) in scored_point.payload {
                    json_obj[&key] = utils::qdrant_value_to_json(&value);
                }
                json_obj["score"] = serde_json::json!(scored_point.score);
                Some(json_obj)
            } else {
                None
            }
        })
        .collect())
}

impl VectorSearchExt for VectorDb {
    /// Search for similar vectors in Qdrant.
    ///
    /// Returns the top N matching points with their payloads (metadata),
    /// plus the cosine score under the `score` key.
    async fn search(
        &self,
        vector: &[f32],
        limit: usize,
        repo_names: &[String],
        kinds: &[String],
    ) -> Result<Vec<serde_json::Value>> {
        self.search_excluding_kinds(KindScopeSearch {
            vector,
            limit,
            repo_names,
            kinds,
            exclude_kinds: &[],
        })
        .await
    }

    /// Definition-channel search (see trait docs).
    async fn search_excluding_kinds(
        &self,
        request: KindScopeSearch<'_>,
    ) -> Result<Vec<serde_json::Value>> {
        let filter = build_search_filter(request.repo_names, request.kinds, request.exclude_kinds);
        scored_search(self, request.vector, request.limit, filter).await
    }

    /// Name-exact probe search (see trait docs).
    async fn search_exact_names(
        &self,
        probe: ExactNameProbe<'_>,
    ) -> Result<Vec<serde_json::Value>> {
        let filter = build_probe_filter(probe.names, probe.tokens, probe.repo_names, probe.kinds);
        scored_search(self, probe.vector, probe.limit, filter).await
    }

    /// UUID-restricted scored search (see trait docs).
    async fn search_by_uuids(&self, probe: UuidProbe<'_>) -> Result<Vec<serde_json::Value>> {
        if probe.uuids.is_empty() {
            return Ok(Vec::new());
        }
        let mut must = vec![utils::any_keyword_condition("uuid", probe.uuids)];
        if !probe.repo_names.is_empty() {
            must.push(utils::any_keyword_condition("repo_name", probe.repo_names));
        }
        if !probe.kinds.is_empty() {
            must.push(utils::any_keyword_condition("kind", probe.kinds));
        }
        scored_search(self, probe.vector, probe.limit, Some(Filter::must(must))).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::db::vector::connection::VectorConnectExt;

    fn repo_names(values: &[&str]) -> Vec<String> {
        values.iter().map(|s| (*s).to_string()).collect()
    }

    fn assert_keyword_condition(cond: &Condition, key: &str, expected: &[&str]) {
        match &cond.condition_one_of {
            Some(qdrant_client::qdrant::condition::ConditionOneOf::Field(field_cond)) => {
                assert_eq!(field_cond.key, key);
                match &field_cond.r#match.as_ref().unwrap().match_value {
                    Some(qdrant_client::qdrant::r#match::MatchValue::Keyword(k)) => {
                        assert_eq!(expected, &[k.as_str()]);
                    }
                    Some(qdrant_client::qdrant::r#match::MatchValue::Keywords(kw)) => {
                        assert_eq!(kw.strings, expected);
                    }
                    other => panic!("Expected keyword match value, got {other:?}"),
                }
            }
            other => panic!("Expected Field condition, got {other:?}"),
        }
    }

    #[ignore = "requires local Qdrant instance running on http://localhost:6334"]
    #[tokio::test]
    async fn test_search_vector() {
        let vector_db = VectorDb::connect("http://localhost:6334", "test_collection_search", 384)
            .await
            .expect("Failed to connect to Qdrant");

        let query_vector = vec![0.5; 384];

        let result = vector_db.search(&query_vector, 10, &[], &[]).await;
        assert!(result.is_ok());
        let results = result.unwrap();
        assert!(results.is_empty() || !results.is_empty()); // Collection might be empty
    }

    #[ignore = "requires local Qdrant instance running on http://localhost:6334"]
    #[tokio::test]
    async fn test_search_vector_with_repo_filter() {
        let vector_db = VectorDb::connect(
            "http://localhost:6334",
            "test_collection_search_filter",
            384,
        )
        .await
        .expect("Failed to connect to Qdrant");

        let query_vector = vec![0.5; 384];

        let result = vector_db
            .search(&query_vector, 10, &["test-repo".to_string()], &[])
            .await;
        assert!(result.is_ok());
        let results = result.unwrap();
        assert!(results.is_empty() || !results.is_empty()); // Collection might be empty
    }

    #[ignore = "requires local Qdrant instance running on http://localhost:6334"]
    #[tokio::test]
    async fn test_search_zero_limit() {
        let vector_db =
            VectorDb::connect("http://localhost:6334", "test_collection_search_zero", 384)
                .await
                .expect("Failed to connect to Qdrant");

        let query_vector = vec![0.5; 384];

        let result = vector_db.search(&query_vector, 0, &[], &[]).await;
        assert!(result.is_ok());
    }

    #[ignore = "requires local Qdrant instance running on http://localhost:6334"]
    #[tokio::test]
    async fn test_search_large_limit() {
        let vector_db =
            VectorDb::connect("http://localhost:6334", "test_collection_search_large", 384)
                .await
                .expect("Failed to connect to Qdrant");

        let query_vector = vec![0.5; 384];

        let result = vector_db.search(&query_vector, 1000, &[], &[]).await;
        assert!(result.is_ok());
    }

    #[test]
    fn build_search_filter_empty_is_none() {
        assert!(build_search_filter(&[], &[], &[]).is_none());
    }

    #[test]
    fn build_search_filter_repo_only_single_keyword() {
        let filter =
            build_search_filter(&repo_names(&["a"]), &[], &[]).expect("filter should be Some");
        assert_eq!(filter.must.len(), 1);
        assert_keyword_condition(&filter.must[0], "repo_name", &["a"]);
    }

    #[test]
    fn build_search_filter_repo_multi_keywords_preserves_order() {
        let filter =
            build_search_filter(&repo_names(&["b", "a"]), &[], &[]).expect("filter should be Some");
        assert_eq!(filter.must.len(), 1);
        assert_keyword_condition(&filter.must[0], "repo_name", &["b", "a"]);
    }

    #[test]
    fn build_search_filter_kinds_only() {
        let filter = build_search_filter(&[], &repo_names(&["rust_function", "method"]), &[])
            .expect("filter should be Some");
        assert_eq!(filter.must.len(), 1);
        assert_keyword_condition(&filter.must[0], "kind", &["rust_function", "method"]);
    }

    #[test]
    fn build_search_filter_repo_and_kinds_combined() {
        let filter = build_search_filter(&repo_names(&["r"]), &repo_names(&["class"]), &[])
            .expect("filter should be Some");
        assert_eq!(filter.must.len(), 2);
        assert_keyword_condition(&filter.must[0], "repo_name", &["r"]);
        assert_keyword_condition(&filter.must[1], "kind", &["class"]);
    }

    // --- kind exclusion (search definition channel) ---

    #[test]
    fn build_search_filter_exclusion_is_must_not() {
        let filter = build_search_filter(
            &[],
            &[],
            &repo_names(&["markdown_section", "markdown_document"]),
        )
        .expect("exclusion alone yields a filter");
        assert!(filter.must.is_empty());
        assert_eq!(filter.must_not.len(), 1);
        assert_keyword_condition(
            &filter.must_not[0],
            "kind",
            &["markdown_section", "markdown_document"],
        );
    }

    #[test]
    fn build_search_filter_exclusion_combines_with_must() {
        let filter = build_search_filter(&repo_names(&["r"]), &[], &repo_names(&["class"]))
            .expect("filter should be Some");
        assert_eq!(filter.must.len(), 1);
        assert_eq!(filter.must_not.len(), 1);
        assert_keyword_condition(&filter.must[0], "repo_name", &["r"]);
        assert_keyword_condition(&filter.must_not[0], "kind", &["class"]);
    }

    #[test]
    fn build_search_filter_all_empty_is_none() {
        assert!(build_search_filter(&[], &[], &[]).is_none());
    }

    // --- build_probe_filter (token-level lexical recall) ---

    fn probe_names(values: &[&str]) -> Vec<String> {
        values.iter().map(|s| (*s).to_string()).collect()
    }

    #[test]
    fn probe_filter_names_and_tokens_form_a_should_or() {
        let filter = build_probe_filter(
            &probe_names(&["useChangePassword"]),
            &probe_names(&["change"]),
            &repo_names(&["ui"]),
            &[],
        )
        .expect("filter should be Some");
        // 1 lexical must-arm + 1 repo arm.
        assert_eq!(filter.must.len(), 2);
        // The lexical arm is a should group with exactly two conditions.
        let lexical = &filter.must[0];
        match &lexical.condition_one_of {
            Some(qdrant_client::qdrant::condition::ConditionOneOf::Filter(inner)) => {
                assert_eq!(inner.should.len(), 2);
                assert_keyword_condition(&inner.should[0], "name", &["useChangePassword"]);
                assert_keyword_condition(&inner.should[1], "name_tokens", &["change"]);
            }
            other => panic!("Expected nested should filter, got {other:?}"),
        }
        assert_keyword_condition(&filter.must[1], "repo_name", &["ui"]);
    }

    #[test]
    fn probe_filter_tokens_alone_suffice() {
        let filter = build_probe_filter(&[], &probe_names(&["similarity", "search"]), &[], &[])
            .expect("filter should be Some");
        assert_eq!(filter.must.len(), 1);
        match &filter.must[0].condition_one_of {
            Some(qdrant_client::qdrant::condition::ConditionOneOf::Filter(inner)) => {
                assert_eq!(inner.should.len(), 1);
                assert_keyword_condition(
                    &inner.should[0],
                    "name_tokens",
                    &["similarity", "search"],
                );
            }
            other => panic!("Expected nested should filter, got {other:?}"),
        }
    }

    #[test]
    fn probe_filter_names_only_keeps_exact_name_contract() {
        let filter = build_probe_filter(&probe_names(&["login", "Login"]), &[], &[], &[])
            .expect("filter should be Some");
        match &filter.must[0].condition_one_of {
            Some(qdrant_client::qdrant::condition::ConditionOneOf::Filter(inner)) => {
                assert_eq!(inner.should.len(), 1);
                assert_keyword_condition(&inner.should[0], "name", &["login", "Login"]);
            }
            other => panic!("Expected nested should filter, got {other:?}"),
        }
    }

    #[test]
    fn probe_filter_without_lexical_input_is_none() {
        assert!(build_probe_filter(&[], &[], &[], &[]).is_none());
    }
}