knot 1.10.0

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
//! Shared entity-kind taxonomy and filtering for CLI and MCP tools.
//!
//! A single place for the wire-format kind tables (the snake_case strings
//! Neo4j stores) and the kind-filter machinery, so `search_hybrid_context`,
//! `find_callers` and future consumers cannot drift apart on what counts as
//! "code" versus "metadata".
//!
//! Wire format note: `EntityKind`'s wire form is its `Display` impl
//! (`src/models/entity.rs`), and every list here must stay in lockstep with
//! it. Unknown kinds score neutral everywhere (no filtering surprises).

// ---------------------------------------------------------------------------
// Kind tables
// ---------------------------------------------------------------------------

/// Wire-format strings of definition kinds that describe behavior. Must stay
/// in lockstep with `src/models/entity.rs`.
pub const CALLABLE_KINDS: &[&str] = &[
    // Generic (language-agnostic Display forms)
    "function",
    "method",
    // Kotlin
    "kotlin_function",
    "kotlin_method",
    // Rust
    "rust_function",
    "rust_method",
    "rust_macro_def",
    // Python
    "python_function",
    "python_method",
    // C / C++
    "c_function",
    "cpp_method",
    "macro_definition",
    // C#
    "csharp_method",
    "csharp_constructor",
    "csharp_local_function",
    "csharp_operator",
    "csharp_indexer",
    // Groovy
    "groovy_method",
    "groovy_function",
    // Stylesheets / Varnish
    "scss_function",
    "scss_mixin",
    "vcl_subroutine",
    "vcl_builtin_sub",
    "vcc_function",
    "vcc_method",
];

/// Wire-format strings of type-declaration kinds.
pub const TYPE_KINDS: &[&str] = &[
    // Generic
    "class",
    "interface",
    "enum",
    // Kotlin
    "kotlin_class",
    "kotlin_interface",
    "kotlin_object",
    "kotlin_companion_object",
    "kotlin_enum",
    // Rust
    "rust_struct",
    "rust_enum",
    "rust_union",
    "rust_trait",
    "rust_type_alias",
    // Python
    "python_class",
    // C / C++
    "c_struct",
    "cpp_class",
    // Groovy
    "groovy_class",
    "groovy_interface",
    "groovy_trait",
    "groovy_enum",
    // C#
    "csharp_class",
    "csharp_interface",
    "csharp_struct",
    "csharp_record",
    "csharp_enum",
    "csharp_delegate",
];

/// Prose kinds whose embeds are long natural-language bodies.
pub const PROSE_KINDS: &[&str] = &["markdown_section", "markdown_document"];

/// Configuration and build-system kinds.
pub const CONFIG_BUILD_KINDS: &[&str] = &[
    "config_property",
    "build_dependency",
    "build_plugin",
    "build_task",
    "pipeline_stage",
    "pipeline_step",
    "cargo_package",
    "cargo_feature",
    "workspace_member",
    "project_identity",
    "helm_value",
];

/// Kubernetes/Helm manifest kinds — infrastructure metadata, never code.
pub const K8S_HELM_KINDS: &[&str] = &[
    "k8s_deployment",
    "k8s_service",
    "k8s_configmap",
    "k8s_secret",
    "k8s_ingress",
    "k8s_namespace",
    "k8s_resource",
    "helm_chart",
    "helm_value",
    "helm_template_var",
];

/// Whether `kind` is documentation / configuration / build / infrastructure
/// metadata: indexed so it can be *searched*, but never the subject of a
/// "who calls this?" question.
///
/// Deliberately a **deny-list** (not an allow-list of code kinds): web and
/// VCL kinds (`html_id`, `css_class`, `vcl_backend`, …) are legitimate
/// reference targets of `REFERENCES_DOM`/`USES_CSS_CLASS`/`USES_BACKEND`
/// edges, and an allow-list would silently drop every future `EntityKind`.
pub fn is_non_code_kind(kind: &str) -> bool {
    PROSE_KINDS.contains(&kind)
        || CONFIG_BUILD_KINDS.contains(&kind)
        || K8S_HELM_KINDS.contains(&kind)
}

/// The full non-code deny-list as concrete wire-format kinds, deduplicated
/// in first-occurrence order (`helm_value` lives in both the config/build
/// and the K8s/Helm tables). Deterministic by construction — the list feeds
/// the Qdrant `must_not` filter of the search definition channel, where a
/// keyword match must serialize identically between runs.
pub fn non_code_kinds() -> Vec<String> {
    let mut kinds: Vec<String> = Vec::new();
    for kind in PROSE_KINDS
        .iter()
        .chain(CONFIG_BUILD_KINDS.iter())
        .chain(K8S_HELM_KINDS.iter())
    {
        let kind = kind.to_string();
        if !kinds.contains(&kind) {
            kinds.push(kind);
        }
    }
    kinds
}

// ---------------------------------------------------------------------------
// Alias expansion (shared with search_hybrid_context's `kinds` parameter)
// ---------------------------------------------------------------------------

/// Expand a user-supplied kind filter into concrete wire-format kinds.
///
/// Accepted aliases (case-insensitive, comma-separated input):
/// - `definition` — every callable and type kind
/// - `callable` / `function` / `method` — every callable kind
/// - `class` / `type` / `struct` — every type kind
/// - `docs` / `prose` — every documentation kind
/// - `config` / `build` — every configuration, build-system and
///   infrastructure kind
/// - anything else — treated as an exact wire-format kind
///   (`rust_function`, `markdown_section`, …)
///
/// Order is preserved and duplicates removed so the generated Qdrant filter
/// is deterministic.
pub fn expand_kinds(specs: &[&str]) -> Vec<String> {
    let mut expanded: Vec<String> = Vec::new();
    for spec in specs {
        let alias = spec.trim().to_lowercase();
        if alias.is_empty() {
            continue;
        }
        let bucket: Vec<&str> = match alias.as_str() {
            "definition" | "definitions" => CALLABLE_KINDS
                .iter()
                .chain(TYPE_KINDS.iter())
                .copied()
                .collect(),
            "callable" | "callables" | "function" | "functions" | "method" | "methods" => {
                CALLABLE_KINDS.to_vec()
            }
            "class" | "classes" | "type" | "types" | "struct" | "structs" => TYPE_KINDS.to_vec(),
            "docs" | "prose" => PROSE_KINDS.to_vec(),
            "config" | "build" => CONFIG_BUILD_KINDS
                .iter()
                .chain(K8S_HELM_KINDS.iter())
                .copied()
                .collect(),
            exact => vec![exact],
        };
        for kind in bucket {
            if !expanded.contains(&kind.to_string()) {
                expanded.push(kind.to_string());
            }
        }
    }
    expanded
}

/// Parse the raw `kinds` parameter (CLI `--kinds`, MCP `kinds`) into the
/// expanded wire-format kind list. `None`/empty → empty list (no filtering).
pub fn parse_kinds(raw: Option<&str>) -> Vec<String> {
    let Some(raw) = raw else {
        return Vec::new();
    };
    let specs: Vec<&str> = raw
        .split(',')
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .collect();
    expand_kinds(&specs)
}

/// Whether an entity passes an (already expanded) kind filter.
/// An empty filter allows everything.
pub fn kinds_allow(expanded: &[String], kind: &str) -> bool {
    expanded.is_empty() || expanded.iter().any(|k| k == kind)
}

// ---------------------------------------------------------------------------
// KindFilter: the find_callers target-resolution filter
// ---------------------------------------------------------------------------

/// How `find_callers` target resolution scopes the entity kinds it may
/// resolve a query against.
///
/// The default is [`KindFilter::CodeOnly`]: a fuzzy query like `cargo` must
/// not present `Cargo.toml` build dependencies and Markdown sections as
/// resolved targets of an impact-analysis question. Documentation and
/// config/build metadata remain fully reachable through
/// [`KindFilter::Any`] (`kinds=all`) or an explicit allow-list.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum KindFilter {
    /// Default. Everything except [`is_non_code_kind`] metadata.
    CodeOnly,
    /// `kinds=all` / `kinds=*` — no kind restriction at all.
    Any,
    /// Explicit allow-list of expanded wire-format kinds.
    Only(Vec<String>),
}

impl KindFilter {
    /// Build the filter from the raw `kinds` parameter shared by CLI and MCP.
    ///
    /// `None`/empty → the code-only default. The sentinel values `all`/`*`
    /// disable filtering entirely; any other spec (alias or exact kind) is
    /// expanded into an explicit allow-list.
    pub fn parse(raw: Option<&str>) -> Self {
        let Some(raw) = raw else {
            return Self::CodeOnly;
        };
        let trimmed = raw.trim();
        if trimmed.is_empty() {
            return Self::CodeOnly;
        }
        if trimmed.eq_ignore_ascii_case("all") || trimmed == "*" {
            return Self::Any;
        }
        let expanded = parse_kinds(Some(trimmed));
        if expanded.is_empty() {
            return Self::CodeOnly;
        }
        Self::Only(expanded)
    }

    /// Whether a wire-format kind passes this filter.
    pub fn allows(&self, kind: &str) -> bool {
        match self {
            Self::Any => true,
            Self::CodeOnly => !is_non_code_kind(kind),
            Self::Only(expanded) => kinds_allow(expanded, kind),
        }
    }

    /// Stable label emitted in the `resolution.kind_filter` field so
    /// consumers can tell which filtering contract produced the target list.
    pub fn wire_label(&self) -> &'static str {
        match self {
            Self::CodeOnly => "code_default",
            Self::Any => "any",
            Self::Only(_) => "explicit",
        }
    }
}

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

    // ---- kind tables ----

    #[test]
    fn tables_contain_the_wire_forms_documented_in_the_readme() {
        assert!(CALLABLE_KINDS.contains(&"rust_function"));
        assert!(CALLABLE_KINDS.contains(&"rust_macro_def"));
        assert!(TYPE_KINDS.contains(&"csharp_record"));
    }

    // ---- is_non_code_kind ----

    #[test]
    fn non_code_kinds_cover_docs_config_build_and_infra() {
        for kind in [
            "markdown_section",
            "markdown_document",
            "config_property",
            "build_dependency",
            "build_plugin",
            "build_task",
            "pipeline_stage",
            "pipeline_step",
            "cargo_package",
            "cargo_feature",
            "workspace_member",
            "project_identity",
            "k8s_deployment",
            "k8s_service",
            "k8s_configmap",
            "k8s_secret",
            "k8s_ingress",
            "k8s_namespace",
            "k8s_resource",
            "helm_chart",
            "helm_value",
            "helm_template_var",
        ] {
            assert!(is_non_code_kind(kind), "{kind} must be non-code");
        }
    }

    #[test]
    fn web_and_vcl_kinds_are_code_reference_targets_not_metadata() {
        // Regression guard for the deny-list decision: these kinds are the
        // targets of REFERENCES_DOM / USES_CSS_CLASS / USES_BACKEND edges
        // and must stay reachable by default.
        for kind in [
            "html_id",
            "html_class",
            "css_class",
            "vcl_backend",
            "vcl_probe",
            "vcl_acl",
            "vcc_module",
            "vcc_object",
        ] {
            assert!(!is_non_code_kind(kind), "{kind} must stay code");
        }
    }

    #[test]
    fn code_kinds_pass_the_non_code_check() {
        for kind in [
            "function",
            "method",
            "class",
            "rust_function",
            "rust_struct",
            "csharp_record",
            "kotlin_class",
            "python_function",
            "rust_constant",
        ] {
            assert!(!is_non_code_kind(kind), "{kind} must be code");
        }
    }

    #[test]
    fn unknown_kinds_are_code_by_default() {
        // Future EntityKind variants must not vanish from resolution.
        assert!(!is_non_code_kind("brand_new_kind"));
    }

    // ---- expand_kinds aliases ----

    #[test]
    fn expand_kinds_definition_includes_callables_and_types() {
        let expanded = expand_kinds(&["definition"]);
        assert!(expanded.contains(&"function".to_string()));
        assert!(expanded.contains(&"rust_struct".to_string()));
        assert!(!expanded.contains(&"markdown_section".to_string()));
    }

    #[test]
    fn expand_kinds_alias_and_exact_mix_dedup() {
        let expanded = expand_kinds(&["callable", "rust_function", "markdown_section"]);
        assert!(expanded.contains(&"rust_function".to_string()));
        assert_eq!(
            expanded.iter().filter(|k| *k == "rust_function").count(),
            1,
            "aliases and exact kinds must deduplicate"
        );
        assert!(
            expanded.contains(&"markdown_section".to_string()),
            "markdown_section must be present in {expanded:?}"
        );
    }

    #[test]
    fn expand_kinds_docs_alias_maps_to_prose() {
        let expanded = expand_kinds(&["docs"]);
        assert_eq!(
            expanded,
            PROSE_KINDS
                .iter()
                .map(|s| s.to_string())
                .collect::<Vec<_>>()
        );
    }

    #[test]
    fn expand_kinds_config_alias_maps_to_config_build_and_infra() {
        let expanded = expand_kinds(&["config"]);
        assert!(expanded.contains(&"build_dependency".to_string()));
        assert!(expanded.contains(&"cargo_feature".to_string()));
        assert!(expanded.contains(&"k8s_deployment".to_string()));
        assert!(!expanded.contains(&"rust_function".to_string()));
    }

    // ---- KindFilter ----

    #[test]
    fn kind_filter_parse_none_is_code_only() {
        assert_eq!(KindFilter::parse(None), KindFilter::CodeOnly);
        assert_eq!(KindFilter::parse(Some("")), KindFilter::CodeOnly);
        assert_eq!(KindFilter::parse(Some("  ")), KindFilter::CodeOnly);
    }

    #[test]
    fn kind_filter_parse_all_sentinels_disable_filtering() {
        assert_eq!(KindFilter::parse(Some("all")), KindFilter::Any);
        assert_eq!(KindFilter::parse(Some("*")), KindFilter::Any);
        assert_eq!(KindFilter::parse(Some(" ALL ")), KindFilter::Any);
    }

    #[test]
    fn kind_filter_code_only_default_behaviour() {
        let filter = KindFilter::parse(None);
        assert_eq!(filter.wire_label(), "code_default");
        assert!(filter.allows("rust_function"));
        assert!(filter.allows("html_id"));
        assert!(filter.allows("vcl_backend"));
        assert!(!filter.allows("build_dependency"));
        assert!(!filter.allows("markdown_section"));
    }

    #[test]
    fn kind_filter_any_allows_everything() {
        let filter = KindFilter::parse(Some("all"));
        assert_eq!(filter.wire_label(), "any");
        assert!(filter.allows("build_dependency"));
        assert!(filter.allows("markdown_section"));
        assert!(filter.allows("rust_function"));
    }

    #[test]
    fn kind_filter_only_respects_explicit_list() {
        let filter = KindFilter::parse(Some("build_dependency,rust_function"));
        assert_eq!(filter.wire_label(), "explicit");
        assert!(filter.allows("build_dependency"));
        assert!(filter.allows("rust_function"));
        assert!(!filter.allows("markdown_section"));
        assert!(!filter.allows("kotlin_class"));
    }

    #[test]
    fn kind_filter_only_supports_aliases() {
        let filter = KindFilter::parse(Some("callable"));
        assert!(filter.allows("python_method"));
        assert!(filter.allows("rust_function"));
        assert!(!filter.allows("rust_struct"));
    }
}