codescout 0.14.0

High-performance coding agent toolkit MCP server
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
use std::path::PathBuf;

use serde::{Deserialize, Serialize};

use crate::tools::RecoverableError;

// ── Public types ─────────────────────────────────────────────────────────────

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum Direction {
    Callers,
    Callees,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum EdgeSource {
    Lsp,
    Ts,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Edge {
    pub caller_sym: String,
    pub callee_sym: String,
    pub file: PathBuf,
    pub line: u32,
    pub col: u32,
    pub source: EdgeSource,
}

// ── Main entry point ─────────────────────────────────────────────────────────

/// Resolve one hop of the call graph for the symbol at `(sym_path, sym_line, sym_col)`.
///
/// ## Strategy
///
/// 1. **LSP path** — if `prepare_call_hierarchy` returns `Some(item)`:
///    - `Direction::Callers`: calls `incoming_calls` and maps each result to an `Edge`.
///    - `Direction::Callees`: calls `outgoing_calls` and maps each result to an `Edge`.
///
/// 2. **Tree-sitter fallback** — if `prepare_call_hierarchy` returns `None`:
///    - `Direction::Callers`: uses `references()` + tree-sitter to filter real call
///      sites and walk up the AST to find the enclosing function name.
///    - `Direction::Callees`: returns a [`RecoverableError`] — finding callees without
///      LSP call-hierarchy support is not viable (references() finds refs *to* a symbol,
///      not calls *from* it).
pub async fn resolve_one_hop(
    client: &dyn crate::lsp::ops::LspClientOps,
    sym_name: &str,
    sym_path: &std::path::Path,
    sym_line: u32,
    sym_col: u32,
    language_id: &str,
    direction: Direction,
) -> anyhow::Result<Vec<Edge>> {
    let item = client
        .prepare_call_hierarchy(sym_path, sym_line, sym_col, language_id)
        .await?;

    match item {
        Some(hier_item) => {
            resolve_via_lsp(client, sym_name, language_id, direction, &hier_item).await
        }
        None => {
            resolve_via_ts(
                client,
                sym_name,
                sym_path,
                sym_line,
                sym_col,
                language_id,
                direction,
            )
            .await
        }
    }
}

// ── LSP path ─────────────────────────────────────────────────────────────────

async fn resolve_via_lsp(
    client: &dyn crate::lsp::ops::LspClientOps,
    sym_name: &str,
    language_id: &str,
    direction: Direction,
    item: &lsp_types::CallHierarchyItem,
) -> anyhow::Result<Vec<Edge>> {
    match direction {
        Direction::Callers => {
            let calls = client.incoming_calls(item, language_id).await?;
            let mut edges = Vec::new();
            for c in &calls {
                let file = lsp_uri_to_path(&c.from.uri)
                    .unwrap_or_else(|| PathBuf::from(c.from.uri.path().as_str()));
                for range in &c.from_ranges {
                    edges.push(Edge {
                        caller_sym: c.from.name.clone(),
                        callee_sym: sym_name.to_owned(),
                        file: file.clone(),
                        line: range.start.line,
                        col: range.start.character,
                        source: EdgeSource::Lsp,
                    });
                }
                // If from_ranges is empty, emit one edge at the symbol's own position.
                if c.from_ranges.is_empty() {
                    edges.push(Edge {
                        caller_sym: c.from.name.clone(),
                        callee_sym: sym_name.to_owned(),
                        file: file.clone(),
                        line: c.from.range.start.line,
                        col: c.from.range.start.character,
                        source: EdgeSource::Lsp,
                    });
                }
            }
            Ok(edges)
        }
        Direction::Callees => {
            let calls = client.outgoing_calls(item, language_id).await?;
            let mut edges = Vec::new();
            for c in &calls {
                let file = lsp_uri_to_path(&c.to.uri)
                    .unwrap_or_else(|| PathBuf::from(c.to.uri.path().as_str()));
                for range in &c.from_ranges {
                    edges.push(Edge {
                        caller_sym: sym_name.to_owned(),
                        callee_sym: c.to.name.clone(),
                        file: file.clone(),
                        line: range.start.line,
                        col: range.start.character,
                        source: EdgeSource::Lsp,
                    });
                }
                // If from_ranges is empty, emit one edge at the callee's own position.
                if c.from_ranges.is_empty() {
                    edges.push(Edge {
                        caller_sym: sym_name.to_owned(),
                        callee_sym: c.to.name.clone(),
                        file: file.clone(),
                        line: c.to.range.start.line,
                        col: c.to.range.start.character,
                        source: EdgeSource::Lsp,
                    });
                }
            }
            Ok(edges)
        }
    }
}

// ── Tree-sitter fallback ─────────────────────────────────────────────────────

async fn resolve_via_ts(
    client: &dyn crate::lsp::ops::LspClientOps,
    sym_name: &str,
    sym_path: &std::path::Path,
    sym_line: u32,
    sym_col: u32,
    language_id: &str,
    direction: Direction,
) -> anyhow::Result<Vec<Edge>> {
    match direction {
        Direction::Callees => {
            // LIMIT-001 fix: walk the AST descendants of the enclosing
            // function and collect every direct call expression.
            resolve_callees_via_ts(sym_name, sym_path, sym_line, sym_col, language_id)
        }
        Direction::Callers => {
            let refs = client
                .references(sym_path, sym_line, sym_col, language_id)
                .await?;

            let mut edges = Vec::new();

            for loc in &refs {
                let ref_path = lsp_uri_to_path(&loc.uri)
                    .unwrap_or_else(|| PathBuf::from(loc.uri.path().as_str()));

                let src = match std::fs::read_to_string(&ref_path) {
                    Ok(s) => s,
                    Err(_) => continue,
                };

                let Some(tree) = parse_ts_tree(&src, language_id) else {
                    continue;
                };

                let byte = position_to_byte(&src, loc.range.start.line, loc.range.start.character);

                if !super::ts_classifier::position_is_call(&tree, byte, language_id) {
                    continue;
                }

                let caller = enclosing_function_name(&tree, &src, byte, language_id)
                    .unwrap_or_else(|| "<anonymous>".to_owned());

                // Skip self-edges: when the LSP can't model a symbol (e.g.
                // #[test] fns), `references` may return locations inside the
                // symbol's own body, which the AST walk-up resolves back to
                // the symbol itself. Real self-recursion goes through
                // `incoming_calls` (LSP path), not this fallback.
                if caller == sym_name {
                    continue;
                }

                edges.push(Edge {
                    caller_sym: caller,
                    callee_sym: sym_name.to_owned(),
                    file: ref_path,
                    line: loc.range.start.line,
                    col: loc.range.start.character,
                    source: EdgeSource::Ts,
                });
            }

            Ok(edges)
        }
    }
}

// ── Helpers ──────────────────────────────────────────────────────────────────

/// Convert an LSP URI to a filesystem path.
///
/// Delegates to [`crate::util::file_address::FileAddress::from_lsp_uri`].
fn lsp_uri_to_path(uri: &lsp_types::Uri) -> Option<PathBuf> {
    crate::util::file_address::FileAddress::from_lsp_uri(uri)
        .map(crate::util::file_address::FileAddress::into_path)
}

/// Convert a `(line, col)` pair (0-indexed, UTF-16 columns) to a byte offset
/// within `src`.
///
/// We treat `col` as a UTF-8 byte offset within the line — a slight
/// simplification that is correct for ASCII identifiers (which covers all
/// realistic symbol names) and sufficient for call-site detection.
fn position_to_byte(src: &str, line: u32, col: u32) -> usize {
    let mut current_line = 0u32;
    let mut offset = 0usize;
    for ch in src.chars() {
        if current_line == line {
            break;
        }
        if ch == '\n' {
            current_line += 1;
        }
        offset += ch.len_utf8();
    }
    // Advance by col bytes within the line (clamped to line length).
    let remaining = &src[offset..];
    let col_bytes = (col as usize).min(remaining.len());
    offset + col_bytes
}

/// Parse `src` with the tree-sitter grammar for `language_id`.
///
/// Returns `None` if the language is not supported or the parse fails.
fn parse_ts_tree(src: &str, language_id: &str) -> Option<tree_sitter::Tree> {
    let lang = crate::ast::get_ts_language(language_id)?;
    let mut parser = tree_sitter::Parser::new();
    parser.set_language(&lang).ok()?;
    parser.parse(src, None)
}

/// Walk up the tree-sitter AST from `byte_offset` to find the innermost
/// enclosing function/method node, and return its declared name.
///
/// `src` must be the same source text that was used to produce `tree`.
///
/// Recognised node kinds per language:
/// - Rust: `function_item`
/// - Python: `function_definition`
/// - TypeScript/JavaScript/TSX/JSX: `function_declaration`, `method_definition`, `arrow_function`
/// - Kotlin: `function_declaration`
/// - Java: `method_declaration`
///
/// Returns `None` if no enclosing function can be found (e.g. top-level code).
fn enclosing_function_name(
    tree: &tree_sitter::Tree,
    src: &str,
    byte_offset: usize,
    language_id: &str,
) -> Option<String> {
    let fn_kinds: &[&str] = match language_id {
        "rust" => &["function_item"],
        "python" => &["function_definition"],
        "typescript" | "javascript" | "tsx" | "jsx" => &[
            "function_declaration",
            "method_definition",
            "arrow_function",
        ],
        "kotlin" => &["function_declaration"],
        "java" => &["method_declaration"],
        _ => return None,
    };

    let src_bytes = src.as_bytes();
    let root = tree.root_node();
    let mut node = root.descendant_for_byte_range(byte_offset, byte_offset)?;

    loop {
        if fn_kinds.contains(&node.kind()) {
            // Walk immediate children to find the name identifier node.
            for i in 0..node.child_count() {
                let child = node.child(i as u32)?;
                if matches!(
                    child.kind(),
                    "identifier" | "simple_identifier" | "property_identifier"
                ) {
                    return child.utf8_text(src_bytes).ok().map(str::to_owned);
                }
            }
            // Function node found but no name child (e.g. anonymous arrow function).
            return None;
        }
        match node.parent() {
            Some(p) => node = p,
            None => return None,
        }
    }
}

/// Tree-sitter call-kind node names per language.
///
/// Mirrors the set used by `position_is_call`, but exposed as a slice so the
/// callees fallback can pre-filter descendant nodes during the AST walk.
/// Returns an empty slice for languages we don't classify.
fn call_kinds_for(language_id: &str) -> &'static [&'static str] {
    match language_id {
        "rust" => &[
            "call_expression",
            "method_call_expression",
            "macro_invocation",
        ],
        "python" => &["call"],
        "typescript" | "javascript" | "tsx" | "jsx" => &["call_expression", "new_expression"],
        "kotlin" => &["call_expression"],
        "java" => &["method_invocation", "object_creation_expression"],
        _ => &[],
    }
}

/// Walk up the AST from `byte_offset` to find the innermost enclosing
/// function/method node. Returns the node itself (rather than just its name
/// like `enclosing_function_name`) so callers can recurse into its body.
fn enclosing_function_node<'tree>(
    tree: &'tree tree_sitter::Tree,
    byte_offset: usize,
    language_id: &str,
) -> Option<tree_sitter::Node<'tree>> {
    let fn_kinds: &[&str] = match language_id {
        "rust" => &["function_item"],
        "python" => &["function_definition"],
        "typescript" | "javascript" | "tsx" | "jsx" => &[
            "function_declaration",
            "method_definition",
            "arrow_function",
        ],
        "kotlin" => &["function_declaration"],
        "java" => &["method_declaration"],
        _ => return None,
    };

    let root = tree.root_node();
    let mut node = root.descendant_for_byte_range(byte_offset, byte_offset)?;
    loop {
        if fn_kinds.contains(&node.kind()) {
            return Some(node);
        }
        match node.parent() {
            Some(p) => node = p,
            None => return None,
        }
    }
}

/// Extract the callee identifier from a call-expression node.
///
/// Returns the rightmost / final segment of the call target:
/// - Rust: `b()` → `b`; `obj.m()` → `m`; `d::e()` → `e`; `m!(...)` → `m`.
/// - Python: `b()` → `b`; `o.c()` → `c`.
/// - TS/JS/TSX/JSX: `b()` → `b`; `o.c()` → `c`; `new D(...)` → `D`.
/// - Kotlin: `b()` → `b`; `o.c()` → `c`.
/// - Java: `b()` → `b`; `o.c()` → `c`; `new D(...)` → `D`.
fn callee_identifier(node: tree_sitter::Node<'_>, src: &str, language_id: &str) -> Option<String> {
    let src_bytes = src.as_bytes();
    let text = |n: tree_sitter::Node<'_>| n.utf8_text(src_bytes).ok().map(str::to_owned);

    // Walk a "scoped"/"path"/"member"/"navigation" expression down to its
    // rightmost identifier-like child.
    fn rightmost_ident<'a>(mut n: tree_sitter::Node<'a>, src_bytes: &[u8]) -> Option<String> {
        loop {
            match n.kind() {
                "identifier"
                | "simple_identifier"
                | "property_identifier"
                | "type_identifier"
                | "field_identifier"
                | "shorthand_property_identifier" => {
                    return n.utf8_text(src_bytes).ok().map(str::to_owned);
                }
                _ => {}
            }
            // Drop to the last non-trivial named child and keep descending.
            let count = n.named_child_count() as u32;
            if count == 0 {
                return n.utf8_text(src_bytes).ok().map(str::to_owned);
            }
            let mut next: Option<tree_sitter::Node<'a>> = None;
            for i in (0..count).rev() {
                if let Some(c) = n.named_child(i) {
                    next = Some(c);
                    break;
                }
            }
            match next {
                Some(c) if c.id() != n.id() => n = c,
                _ => return n.utf8_text(src_bytes).ok().map(str::to_owned),
            }
        }
    }

    match (language_id, node.kind()) {
        // ── Rust ──────────────────────────────────────────────────────────
        ("rust", "call_expression") => {
            let f = node.child_by_field_name("function")?;
            match f.kind() {
                "identifier" => text(f),
                _ => rightmost_ident(f, src_bytes),
            }
        }
        ("rust", "method_call_expression") => {
            let name = node.child_by_field_name("method")?;
            text(name)
        }
        ("rust", "macro_invocation") => {
            let name = node.child_by_field_name("macro")?;
            // `macro` can be `identifier` or `scoped_identifier`.
            match name.kind() {
                "identifier" => text(name),
                _ => rightmost_ident(name, src_bytes),
            }
        }
        // ── Python ────────────────────────────────────────────────────────
        ("python", "call") => {
            let f = node.child_by_field_name("function")?;
            match f.kind() {
                "identifier" => text(f),
                "attribute" => {
                    let attr = f.child_by_field_name("attribute")?;
                    text(attr)
                }
                _ => rightmost_ident(f, src_bytes),
            }
        }
        // ── TS / JS / TSX / JSX ──────────────────────────────────────────
        ("typescript" | "javascript" | "tsx" | "jsx", "call_expression") => {
            let f = node.child_by_field_name("function")?;
            match f.kind() {
                "identifier" => text(f),
                "member_expression" => {
                    let p = f.child_by_field_name("property")?;
                    text(p)
                }
                _ => rightmost_ident(f, src_bytes),
            }
        }
        ("typescript" | "javascript" | "tsx" | "jsx", "new_expression") => {
            let c = node.child_by_field_name("constructor")?;
            match c.kind() {
                "identifier" => text(c),
                _ => rightmost_ident(c, src_bytes),
            }
        }
        // ── Kotlin ────────────────────────────────────────────────────────
        ("kotlin", "call_expression") => {
            // Kotlin grammar: the callee is the first named child; for
            // `obj.foo()` the call_expression is wrapped in a
            // navigation_expression where the rightmost simple_identifier is
            // the method name. For bare `foo()` the first child is the
            // simple_identifier directly.
            let first = node.named_child(0)?;
            rightmost_ident(first, src_bytes)
        }
        // ── Java ──────────────────────────────────────────────────────────
        ("java", "method_invocation") => {
            let name = node.child_by_field_name("name")?;
            text(name)
        }
        ("java", "object_creation_expression") => {
            let typ = node.child_by_field_name("type")?;
            match typ.kind() {
                "type_identifier" => text(typ),
                _ => rightmost_ident(typ, src_bytes),
            }
        }
        _ => None,
    }
}

/// Tree-sitter fallback for `Direction::Callees` — used when LSP
/// `callHierarchy` is unavailable for the language/file.
///
/// Strategy: parse the source, locate the function node enclosing the symbol
/// at `(sym_line, sym_col)`, then walk its descendants collecting every
/// call-expression node. For each call we extract the callee identifier with
/// per-language rules (see [`callee_identifier`]) and emit one [`Edge`].
///
/// Returns `RecoverableError` if the language is not in the supported set
/// (Rust, Python, TS/JS/TSX/JSX, Kotlin, Java), if the source can't be read,
/// or if no enclosing function can be located.
fn resolve_callees_via_ts(
    sym_name: &str,
    sym_path: &std::path::Path,
    sym_line: u32,
    sym_col: u32,
    language_id: &str,
) -> anyhow::Result<Vec<Edge>> {
    let kinds = call_kinds_for(language_id);
    if kinds.is_empty() {
        return Err(RecoverableError::with_hint(
            "call_graph direction=callees requires LSP callHierarchy support (not available for this language/file)",
            "Activate a language server for this file, or use direction=callers which has a tree-sitter fallback.",
        )
        .into());
    }

    let src = std::fs::read_to_string(sym_path).map_err(|e| {
        RecoverableError::with_hint(
            format!("could not read source for callees fallback: {e}"),
            "Verify the file path resolves to a readable file.",
        )
    })?;

    let tree = parse_ts_tree(&src, language_id).ok_or_else(|| {
        RecoverableError::with_hint(
            "tree-sitter parse failed for callees fallback",
            "The grammar may not be registered for this language; activate an LSP if available.",
        )
    })?;

    let byte = position_to_byte(&src, sym_line, sym_col);
    let fn_node = enclosing_function_node(&tree, byte, language_id).ok_or_else(|| {
        RecoverableError::with_hint(
            "could not locate enclosing function for callees fallback",
            "Ensure (sym_line, sym_col) points inside a function/method body.",
        )
    })?;

    let mut edges = Vec::new();
    let mut cursor = fn_node.walk();
    let mut stack: Vec<tree_sitter::Node<'_>> = vec![fn_node];
    while let Some(n) = stack.pop() {
        if kinds.contains(&n.kind()) {
            if let Some(callee) = callee_identifier(n, &src, language_id) {
                let start = n.start_position();
                edges.push(Edge {
                    caller_sym: sym_name.to_owned(),
                    callee_sym: callee,
                    file: sym_path.to_path_buf(),
                    line: start.row as u32,
                    col: start.column as u32,
                    source: EdgeSource::Ts,
                });
            }
        }
        for child in n.children(&mut cursor) {
            stack.push(child);
        }
    }

    Ok(edges)
}

// ── Tests ────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::lsp::mock::MockLspClient;

    // ── Test 1: LSP success — Callers ────────────────────────────────────────

    #[tokio::test]
    async fn resolve_one_hop_uses_lsp_when_available() {
        let mock = MockLspClient::new();

        // Build the CallHierarchyItem for "a" at a.rs:10:5
        let a_uri: lsp_types::Uri = "file:///a.rs".parse().unwrap();
        let a_item = lsp_types::CallHierarchyItem {
            name: "a".into(),
            kind: lsp_types::SymbolKind::FUNCTION,
            tags: None,
            detail: None,
            uri: a_uri.clone(),
            range: lsp_types::Range {
                start: lsp_types::Position {
                    line: 10,
                    character: 5,
                },
                end: lsp_types::Position {
                    line: 15,
                    character: 1,
                },
            },
            selection_range: lsp_types::Range {
                start: lsp_types::Position {
                    line: 10,
                    character: 5,
                },
                end: lsp_types::Position {
                    line: 10,
                    character: 6,
                },
            },
            data: None,
        };

        // Seed prepare_call_hierarchy
        mock.prepare_call_hierarchy_results.lock().unwrap().insert(
            (std::path::PathBuf::from("/a.rs"), 10, 5),
            Some(a_item.clone()),
        );

        // Build caller "b" at b.rs:3:0
        let b_uri: lsp_types::Uri = "file:///b.rs".parse().unwrap();
        let b_item = lsp_types::CallHierarchyItem {
            name: "b".into(),
            kind: lsp_types::SymbolKind::FUNCTION,
            tags: None,
            detail: None,
            uri: b_uri,
            range: lsp_types::Range::default(),
            selection_range: lsp_types::Range::default(),
            data: None,
        };
        let incoming = lsp_types::CallHierarchyIncomingCall {
            from: b_item,
            from_ranges: vec![lsp_types::Range {
                start: lsp_types::Position {
                    line: 3,
                    character: 0,
                },
                end: lsp_types::Position {
                    line: 3,
                    character: 1,
                },
            }],
        };

        // Seed incoming_calls keyed by "a"
        mock.incoming_calls_results
            .lock()
            .unwrap()
            .insert("a".into(), vec![incoming]);

        let edges = resolve_one_hop(
            &mock,
            "a",
            std::path::Path::new("/a.rs"),
            10,
            5,
            "rust",
            Direction::Callers,
        )
        .await
        .unwrap();

        assert_eq!(edges.len(), 1);
        let e = &edges[0];
        assert_eq!(e.source, EdgeSource::Lsp);
        assert_eq!(e.caller_sym, "b");
        assert_eq!(e.callee_sym, "a");
        assert_eq!(e.line, 3);
        assert_eq!(e.col, 0);
    }

    // ── Test 2: TS fallback — Callers ────────────────────────────────────────

    #[tokio::test]
    async fn resolve_one_hop_ts_fallback_callers() {
        // Rust source fixture: "bar" calls "foo"
        let src = "fn bar() { foo(1); }\nfn foo(x: i32) {}\n";

        // Write fixture to a temp file
        let dir = tempfile::tempdir().unwrap();
        let fixture = dir.path().join("fixture.rs");
        std::fs::write(&fixture, src).unwrap();

        let fixture_uri = {
            // Build file:// URI in a way that works on both Unix and Windows.
            // Unix: /tmp/foo            → file:///tmp/foo (already starts with /)
            // Windows: C:\Users\foo    → file:///C:/Users/foo
            let path_str = fixture.to_string_lossy().replace('\\', "/");
            if path_str.starts_with('/') {
                format!("file://{}", path_str)
            } else {
                format!("file:///{}", path_str)
            }
        };

        // Build a reference location pointing at the "foo" identifier inside `bar`
        // "fn bar() { foo(1); }" — "foo" starts at col 11 on line 0
        let ref_loc = lsp_types::Location {
            uri: fixture_uri.parse::<lsp_types::Uri>().unwrap(),
            range: lsp_types::Range {
                start: lsp_types::Position {
                    line: 0,
                    character: 11,
                },
                end: lsp_types::Position {
                    line: 0,
                    character: 14,
                },
            },
        };

        // Mock: prepare_call_hierarchy returns None (no LSP support)
        // references returns our fixture location
        let mock = MockLspClient::new();
        // prepare_call_hierarchy_results is empty → returns None for any key
        mock.references_results
            .lock()
            .unwrap()
            .insert(fixture.clone(), vec![ref_loc]);

        let edges = resolve_one_hop(
            &mock,
            "foo",
            &fixture,
            1, // sym_line for "foo" (line 1)
            3, // sym_col
            "rust",
            Direction::Callers,
        )
        .await
        .unwrap();

        // Exactly one call-site edge, coming from "bar"
        assert_eq!(edges.len(), 1, "expected 1 edge, got {:?}", edges);
        let e = &edges[0];
        assert_eq!(e.source, EdgeSource::Ts);
        assert_eq!(e.caller_sym, "bar");
        assert_eq!(e.callee_sym, "foo");
    }

    /// Regression test for docs/issues/2026-05-25-call-graph-tree-sitter-self-edges.md.
    /// Pre-fix: resolve_one_hop emits a spurious self-edge when references
    /// returns a location inside the queried symbol's own body (which happens
    /// for symbols RA cannot fully model, e.g. #[test] fns).
    #[tokio::test]
    async fn resolve_one_hop_ts_fallback_callers_skips_self_edge() {
        let src = "fn bar(x: i32) {}\nfn foo() { bar(1); }\n";

        let dir = tempfile::tempdir().unwrap();
        let fixture = dir.path().join("fixture.rs");
        std::fs::write(&fixture, src).unwrap();

        let fixture_uri = {
            let path_str = fixture.to_string_lossy().replace('\\', "/");
            if path_str.starts_with('/') {
                format!("file://{}", path_str)
            } else {
                format!("file:///{}", path_str)
            }
        };

        // Reference INSIDE foo's body — at the `bar(1)` call site.
        // Simulates RA returning internal call sites (rather than external
        // callers) when asked references on a symbol it doesn't fully model.
        let ref_loc = lsp_types::Location {
            uri: fixture_uri.parse::<lsp_types::Uri>().unwrap(),
            range: lsp_types::Range {
                start: lsp_types::Position {
                    line: 1,
                    character: 11,
                },
                end: lsp_types::Position {
                    line: 1,
                    character: 14,
                },
            },
        };

        let mock = MockLspClient::new();
        mock.references_results
            .lock()
            .unwrap()
            .insert(fixture.clone(), vec![ref_loc]);

        let edges = resolve_one_hop(&mock, "foo", &fixture, 1, 3, "rust", Direction::Callers)
            .await
            .unwrap();

        for e in &edges {
            assert_ne!(
                (e.caller_sym.as_str(), e.callee_sym.as_str()),
                ("foo", "foo"),
                "spurious self-edge: tree-sitter fallback resolved a reference \
                 inside foo's own body as a caller-of-foo edge. \
                 Pre-fix this returned 1 edge foo→foo; post-fix should return 0."
            );
        }
    }

    // ── Test 3: Callees with no LSP → RecoverableError ───────────────────────

    #[tokio::test]
    async fn resolve_one_hop_callees_without_lsp_returns_recoverable_error() {
        // Mock returns None from prepare_call_hierarchy (map is empty).
        // Use an unsupported language ("go") so the TS callees fallback is not
        // available either, exercising the RecoverableError branch.
        let mock = MockLspClient::new();

        let result = resolve_one_hop(
            &mock,
            "foo",
            std::path::Path::new("/a.go"),
            0,
            0,
            "go",
            Direction::Callees,
        )
        .await;

        let err = result.expect_err("expected an error for callees without LSP");
        assert!(
            err.downcast_ref::<RecoverableError>().is_some(),
            "expected RecoverableError, got: {err}"
        );
    }

    // ── Test 4: TS fallback — Callees (LIMIT-001 fix) ────────────────────────

    #[tokio::test]
    async fn resolve_callees_via_ts_rust_finds_direct_calls() {
        let src = "fn a() {\n    b();\n    c();\n    d::e();\n}\nfn b() {}\nfn c() {}\nmod d { pub fn e() {} }\n";

        let dir = tempfile::tempdir().unwrap();
        let fixture = dir.path().join("fixture.rs");
        std::fs::write(&fixture, src).unwrap();

        // Mock: prepare_call_hierarchy returns None → forces TS fallback.
        let mock = MockLspClient::new();

        let edges = resolve_one_hop(
            &mock,
            "a",
            &fixture,
            0, // sym_line — start of `fn a`
            3, // sym_col — inside the identifier `a`
            "rust",
            Direction::Callees,
        )
        .await
        .unwrap();

        let callees: Vec<&str> = edges.iter().map(|e| e.callee_sym.as_str()).collect();
        assert!(callees.contains(&"b"), "missing b in {:?}", callees);
        assert!(callees.contains(&"c"), "missing c in {:?}", callees);
        assert!(callees.contains(&"e"), "missing e in {:?}", callees);
        for e in &edges {
            assert_eq!(e.caller_sym, "a", "wrong caller in {:?}", e);
            assert_eq!(e.source, EdgeSource::Ts);
        }
        assert_eq!(edges.len(), 3, "unexpected edges: {:?}", edges);
    }

    #[tokio::test]
    async fn resolve_callees_via_ts_python_finds_direct_calls() {
        let src = "def a():\n    b()\n    obj.c()\n\ndef b():\n    pass\n";

        let dir = tempfile::tempdir().unwrap();
        let fixture = dir.path().join("fixture.py");
        std::fs::write(&fixture, src).unwrap();

        let mock = MockLspClient::new();

        let edges = resolve_one_hop(
            &mock,
            "a",
            &fixture,
            0,
            4, // sym_col inside `a`
            "python",
            Direction::Callees,
        )
        .await
        .unwrap();

        let callees: Vec<&str> = edges.iter().map(|e| e.callee_sym.as_str()).collect();
        assert!(callees.contains(&"b"), "missing b in {:?}", callees);
        assert!(callees.contains(&"c"), "missing c in {:?}", callees);
    }

    #[tokio::test]
    async fn resolve_callees_via_ts_typescript_finds_direct_calls() {
        let src = "function a() {\n    b();\n    obj.c();\n    new D();\n}\nfunction b() {}\n";

        let dir = tempfile::tempdir().unwrap();
        let fixture = dir.path().join("fixture.ts");
        std::fs::write(&fixture, src).unwrap();

        let mock = MockLspClient::new();

        let edges = resolve_one_hop(
            &mock,
            "a",
            &fixture,
            0,
            9, // sym_col inside `a`
            "typescript",
            Direction::Callees,
        )
        .await
        .unwrap();

        let callees: Vec<&str> = edges.iter().map(|e| e.callee_sym.as_str()).collect();
        assert!(callees.contains(&"b"), "missing b in {:?}", callees);
        assert!(callees.contains(&"c"), "missing c in {:?}", callees);
        assert!(callees.contains(&"D"), "missing D in {:?}", callees);
    }

    #[tokio::test]
    async fn resolve_callees_via_ts_kotlin_finds_direct_calls() {
        let src = "fun a() {\n    b()\n    obj.c()\n}\nfun b() {}\n";

        let dir = tempfile::tempdir().unwrap();
        let fixture = dir.path().join("fixture.kt");
        std::fs::write(&fixture, src).unwrap();

        let mock = MockLspClient::new();

        let edges = resolve_one_hop(&mock, "a", &fixture, 0, 4, "kotlin", Direction::Callees)
            .await
            .unwrap();

        let callees: Vec<&str> = edges.iter().map(|e| e.callee_sym.as_str()).collect();
        assert!(callees.contains(&"b"), "missing b in {:?}", callees);
        assert!(callees.contains(&"c"), "missing c in {:?}", callees);
    }

    #[tokio::test]
    async fn resolve_callees_via_ts_java_finds_direct_calls() {
        let src = "class X {\n    void a() {\n        b();\n        obj.c();\n    }\n    void b() {}\n}\n";

        let dir = tempfile::tempdir().unwrap();
        let fixture = dir.path().join("Fixture.java");
        std::fs::write(&fixture, src).unwrap();

        let mock = MockLspClient::new();

        let edges = resolve_one_hop(
            &mock,
            "a",
            &fixture,
            1,
            9, // sym_col inside `a`
            "java",
            Direction::Callees,
        )
        .await
        .unwrap();

        let callees: Vec<&str> = edges.iter().map(|e| e.callee_sym.as_str()).collect();
        assert!(callees.contains(&"b"), "missing b in {:?}", callees);
        assert!(callees.contains(&"c"), "missing c in {:?}", callees);
    }

    // ── Helper unit tests ────────────────────────────────────────────────────

    #[test]
    fn position_to_byte_first_line() {
        let src = "hello world\nsecond line\n";
        // col 6 on line 0 → byte 6 ('w')
        assert_eq!(position_to_byte(src, 0, 6), 6);
    }

    #[test]
    fn position_to_byte_second_line() {
        let src = "hello\nworld\n";
        // line 1, col 0 → byte 6 (start of "world")
        assert_eq!(position_to_byte(src, 1, 0), 6);
    }

    #[test]
    fn enclosing_function_name_rust() {
        let src = "fn my_func() { foo(); }\n";
        let tree = parse_ts_tree(src, "rust").unwrap();
        // byte offset of "foo" = 15
        let byte = position_to_byte(src, 0, 15);
        let name = enclosing_function_name(&tree, src, byte, "rust");
        assert_eq!(name.as_deref(), Some("my_func"));
    }

    #[test]
    fn lsp_uri_to_path_round_trips() {
        let uri: lsp_types::Uri = "file:///tmp/hello.rs".parse().unwrap();
        let path = lsp_uri_to_path(&uri).unwrap();
        assert_eq!(path, PathBuf::from("/tmp/hello.rs"));
    }
}