knot 1.12.2

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
//! Shared rendering helpers for the `resolution` block returned by
//! `find_references`.
//!
//! The graph query resolves an ambiguous entity name to one or more concrete
//! targets and reports how it did so (the match tier), whether the match was
//! fuzzy, and whether the target list was truncated. Both the Markdown
//! formatter (`cli_tools::find_callers`) and the table formatter
//! (`cli_tools::formatters::table`) render that same block, so the parsing and
//! the wording live here to keep them in sync.

use serde_json::Value;

use crate::cli_tools::format_file_line;

/// Borrowed view over the `resolution` object of a `find_references` result.
pub(crate) struct ResolutionView<'a> {
    query: &'a str,
    tier: &'a str,
    targets: &'a [Value],
    fuzzy: bool,
    truncated: bool,
    total_targets: i64,
    kind_filter: &'a str,
    hidden_kinds: Vec<&'a str>,
    hidden_non_code: i64,
}

impl<'a> ResolutionView<'a> {
    /// Parse the `resolution` block out of a `find_references` result.
    ///
    /// Returns `None` when the key is absent or does not carry both a `tier`
    /// and a `targets` array — callers then fall back to their plain header.
    pub(crate) fn from_references(references: &'a Value) -> Option<Self> {
        let resolution = references.get("resolution")?;
        let tier = resolution.get("tier").and_then(Value::as_str)?;
        let targets = resolution.get("targets").and_then(Value::as_array)?;
        let count = targets.len() as i64;

        Some(Self {
            query: resolution
                .get("query")
                .and_then(Value::as_str)
                .unwrap_or_default(),
            tier,
            targets,
            fuzzy: resolution
                .get("fuzzy")
                .and_then(Value::as_bool)
                .unwrap_or(false),
            truncated: resolution
                .get("truncated")
                .and_then(Value::as_bool)
                .unwrap_or(false),
            total_targets: resolution
                .get("total_targets")
                .and_then(Value::as_i64)
                .unwrap_or(count),
            // Older indexes/Knot-server responses predate the kind-filter
            // disclosure; absent fields degrade to "nothing was hidden".
            kind_filter: resolution
                .get("kind_filter")
                .and_then(Value::as_str)
                .unwrap_or("code_default"),
            hidden_kinds: resolution
                .get("hidden_kinds")
                .and_then(Value::as_array)
                .map(|a| a.iter().filter_map(Value::as_str).collect())
                .unwrap_or_default(),
            hidden_non_code: resolution
                .get("hidden_non_code")
                .and_then(Value::as_i64)
                .unwrap_or(0),
        })
    }

    pub(crate) fn query(&self) -> &'a str {
        self.query
    }

    pub(crate) fn count(&self) -> usize {
        self.targets.len()
    }

    pub(crate) fn is_fuzzy(&self) -> bool {
        self.fuzzy
    }

    pub(crate) fn is_truncated(&self) -> bool {
        self.truncated
    }

    pub(crate) fn total_targets(&self) -> i64 {
        self.total_targets
    }

    /// How many matched entities the kind filter removed (`0` when the
    /// resolution block predates the disclosure or nothing was hidden).
    pub(crate) fn hidden_non_code(&self) -> i64 {
        self.hidden_non_code
    }

    /// Stable `resolution.kind_filter` label (`code_default` / `any` /
    /// `explicit`).
    pub(crate) fn kind_filter(&self) -> &'a str {
        self.kind_filter
    }

    /// Shared wording for the hidden-matches disclosure. Empty when there is
    /// nothing to disclose, so callers can unconditionally append the return
    /// value. `markdown` toggles the styling: `**emphasis**` and backticked
    /// option names for the Markdown answer, bare text for the CLI table.
    /// A single parameterized helper so the two renderers cannot drift
    /// (extracted after `cargo dupes` flagged the copy pair).
    pub(crate) fn hidden_notice(&self, markdown: bool) -> String {
        if self.hidden_non_code <= 0 {
            return String::new();
        }
        let entity_word = if self.hidden_non_code == 1 {
            "entity"
        } else {
            "entities"
        };
        let kinds = if self.hidden_kinds.is_empty() {
            "non-code kinds".to_string()
        } else if self.hidden_kinds.len() == 1 {
            format!("kind {}", self.hidden_kinds[0])
        } else {
            format!("kinds {}", self.hidden_kinds.join(", "))
        };
        if markdown {
            format!(
                "**Non-code matches hidden** — {n} {entity_word} matched `{q}` but are \
                 documentation/config/build metadata ({kinds}), not code definitions. \
                 Pass `kinds=all` to include them, or a specific kind \
                 (e.g. `kinds=build_dependency`) to narrow explicitly.",
                n = self.hidden_non_code,
                q = self.query,
            )
        } else {
            format!(
                "Non-code matches hidden — {n} {entity_word} matched `{q}` but are \
                 documentation/config/build metadata ({kinds}), not code definitions. \
                 Pass kinds=all to include them, or a specific kind \
                 (e.g. kinds=build_dependency) to narrow explicitly.",
                n = self.hidden_non_code,
                q = self.query,
            )
        }
    }

    /// Human-readable label for the match tier.
    pub(crate) fn tier_label(&self) -> &'a str {
        match self.tier {
            "exact_fqn" => "exact FQN match",
            "fqn_suffix" => "FQN suffix match",
            "exact_name" => "exact name match",
            "signature_prefix" => "signature prefix match",
            "name_prefix" => "name prefix match",
            "fuzzy" => "fuzzy match",
            other => other,
        }
    }

    /// `"Resolved to 1 target by exact name match"` (no trailing punctuation).
    pub(crate) fn summary(&self) -> String {
        let count = self.count();
        let target_word = if count == 1 { "target" } else { "targets" };
        format!(
            "Resolved to {} {} by {}",
            count,
            target_word,
            self.tier_label()
        )
    }

    /// Caveat stating that the relationship-bucket counts cover only the
    /// shown targets. Empty when the target list is complete, so callers can
    /// unconditionally append the return value:
    ///
    /// ```text
    /// Counts below are partial — they cover only the 25 of 112 targets shown.
    /// ```
    pub(crate) fn partial_counts_caveat(&self) -> String {
        if !self.truncated {
            return String::new();
        }
        format!(
            "Counts below are partial — they cover only the {} of {} targets shown.",
            self.count(),
            self.total_targets()
        )
    }

    /// One `- \`fqn\` (kind) at \`file:line\`  (repo: name)` bullet per resolved target.
    pub(crate) fn target_bullets(&self) -> String {
        let mut out = String::new();
        for target in self.targets {
            let fqn = target.get("fqn").and_then(Value::as_str).unwrap_or("");
            let kind = target.get("kind").and_then(Value::as_str).unwrap_or("");
            let file_path = target
                .get("file_path")
                .and_then(Value::as_str)
                .unwrap_or("");
            let start_line = target
                .get("start_line")
                .and_then(Value::as_i64)
                .unwrap_or(0);
            let repo = target.get("repo_name").and_then(Value::as_str);
            out.push_str(&format!(
                "- `{}` ({}) at {}\n",
                fqn,
                kind,
                format_file_line(&format!("{}:{}", file_path, start_line), repo)
            ));
        }
        out
    }
}

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

    fn sample() -> Value {
        json!({
            "resolution": {
                "query": "Off",
                "tier": "exact_name",
                "fuzzy": false,
                "truncated": false,
                "targets": [
                    {
                        "fqn": "Ns.GestureOwner.Off",
                        "kind": "csharp_record",
                        "file_path": "src/GestureOwner.cs",
                        "start_line": 15
                    }
                ]
            }
        })
    }

    #[test]
    fn test_missing_resolution_returns_none() {
        assert!(ResolutionView::from_references(&json!({"calls": []})).is_none());
    }

    #[test]
    fn test_incomplete_resolution_returns_none() {
        let refs = json!({"resolution": {"tier": "exact_name"}});
        assert!(ResolutionView::from_references(&refs).is_none());
    }

    #[test]
    fn test_summary_singular() {
        let refs = sample();
        let view = ResolutionView::from_references(&refs).expect("resolution");
        assert_eq!(view.summary(), "Resolved to 1 target by exact name match");
    }

    #[test]
    fn test_summary_plural() {
        let refs = json!({
            "resolution": {
                "tier": "fqn_suffix",
                "targets": [{"fqn": "A"}, {"fqn": "B"}]
            }
        });
        let view = ResolutionView::from_references(&refs).expect("resolution");
        assert_eq!(view.summary(), "Resolved to 2 targets by FQN suffix match");
    }

    #[test]
    fn test_unknown_tier_passes_through() {
        let refs = json!({"resolution": {"tier": "brand_new", "targets": []}});
        let view = ResolutionView::from_references(&refs).expect("resolution");
        assert_eq!(view.tier_label(), "brand_new");
    }

    #[test]
    fn test_target_bullets() {
        let refs = sample();
        let view = ResolutionView::from_references(&refs).expect("resolution");
        assert_eq!(
            view.target_bullets(),
            "- `Ns.GestureOwner.Off` (csharp_record) at `src/GestureOwner.cs:15`\n"
        );
    }

    #[test]
    fn target_bullets_include_repo_annotation() {
        let refs = json!({
            "resolution": {
                "tier": "exact_name",
                "targets": [{
                    "fqn": "Ns.GestureOwner.Off",
                    "kind": "csharp_record",
                    "file_path": "src/GestureOwner.cs",
                    "start_line": 15,
                    "repo_name": "scope_alpha"
                }]
            }
        });
        let view = ResolutionView::from_references(&refs).expect("resolution");
        assert_eq!(
            view.target_bullets(),
            "- `Ns.GestureOwner.Off` (csharp_record) at `src/GestureOwner.cs:15`  (repo: scope_alpha)\n"
        );
    }

    #[test]
    fn target_bullets_omit_annotation_when_repo_empty() {
        let refs = json!({
            "resolution": {
                "tier": "exact_name",
                "targets": [{
                    "fqn": "Ns.Off",
                    "kind": "method",
                    "file_path": "src/Off.cs",
                    "start_line": 3,
                    "repo_name": ""
                }]
            }
        });
        let view = ResolutionView::from_references(&refs).expect("resolution");
        assert_eq!(
            view.target_bullets(),
            "- `Ns.Off` (method) at `src/Off.cs:3`\n"
        );
    }

    #[test]
    fn test_total_targets_defaults_to_target_count() {
        let refs = sample();
        let view = ResolutionView::from_references(&refs).expect("resolution");
        assert_eq!(view.total_targets(), 1);
        assert!(!view.is_truncated());
        assert!(!view.is_fuzzy());
        assert_eq!(view.query(), "Off");
    }

    #[test]
    fn test_total_targets_uses_explicit_value() {
        let refs = json!({
            "resolution": {
                "tier": "exact_name",
                "truncated": true,
                "total_targets": 112,
                "targets": [{"fqn": "A"}]
            }
        });
        let view = ResolutionView::from_references(&refs).expect("resolution");
        assert!(view.is_truncated());
        assert_eq!(view.total_targets(), 112);
    }

    // ---- kind-filter disclosure ----

    fn hidden_resolution(n: i64, kinds: &[&str]) -> Value {
        json!({
            "resolution": {
                "query": "cargo",
                "tier": "fuzzy",
                "truncated": false,
                "total_targets": 0,
                "kind_filter": "code_default",
                "hidden_non_code": n,
                "hidden_kinds": kinds,
                "targets": []
            }
        })
    }

    #[test]
    fn resolution_view_defaults_hidden_fields_when_absent() {
        // Pre-fix wire shape: no kind-filter fields at all.
        let refs = sample();
        let view = ResolutionView::from_references(&refs).expect("resolution");
        assert_eq!(view.hidden_non_code(), 0);
        assert_eq!(view.kind_filter(), "code_default");
        assert_eq!(view.hidden_notice(true), "");
        assert_eq!(view.hidden_notice(false), "");
    }

    #[test]
    fn hidden_notice_names_the_kinds_and_how_to_opt_in() {
        let refs = hidden_resolution(93, &["build_dependency", "cargo_package"]);
        let view = ResolutionView::from_references(&refs).expect("resolution");
        let markdown = view.hidden_notice(true);
        assert!(markdown.contains("**Non-code matches hidden** — 93 entities matched `cargo`"));
        assert!(markdown.contains("build_dependency, cargo_package"));
        assert!(markdown.contains("kinds=all"));
        assert!(!markdown.contains("may be unused"));

        let plain = view.hidden_notice(false);
        assert!(plain.contains("Non-code matches hidden — 93"));
        assert!(!plain.contains("**"));
    }

    #[test]
    fn hidden_notice_singularizes_and_handles_empty_kinds() {
        let refs = hidden_resolution(1, &[]);
        let view = ResolutionView::from_references(&refs).expect("res");
        assert!(view.hidden_notice(true).contains("1 entity matched"));
        assert!(view.hidden_notice(true).contains("(non-code kinds)"));

        let refs = hidden_resolution(2, &["md_section"]);
        let view = ResolutionView::from_references(&refs).expect("res");
        assert!(view.hidden_notice(true).contains("2 entities matched"));
        assert!(view.hidden_notice(true).contains("kind md_section"));
    }

    #[test]
    fn name_prefix_tier_renders_label() {
        let refs = json!({
            "resolution": {
                "query": "use",
                "tier": "name_prefix",
                "fuzzy": false,
                "truncated": false,
                "targets": [
                    {
                        "fqn": "useSearch",
                        "kind": "function",
                        "file_path": "src/api/queries.ts",
                        "start_line": 55
                    }
                ]
            }
        });
        let view = ResolutionView::from_references(&refs).expect("resolution");
        assert_eq!(view.tier_label(), "name prefix match");
        assert_eq!(view.summary(), "Resolved to 1 target by name prefix match");
        assert!(!view.is_fuzzy());
    }
}