leindex 1.8.0

LeIndex MCP and semantic code search engine for AI tools and large codebases
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
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
//! Per-tool CLI render functions and the central dispatcher.
//!
//! Each `render_*` function reads a JSON `Value` (the same data the
//! LLM sees) and produces a human-readable, colored string for the
//! CLI. They use shared helpers (`header`, `field`, `status_icon`,
//! `suffix`) defined at the top of this file so the visual style stays
//! consistent across tools.
//!
//! `render_tool_output` is the single entry point used by
//! `leindex tools run` and any other CLI surface; it dispatches on the
//! normalized tool name to the right `render_*` function.

use serde_json::Value;

use super::diff::{render_default, render_diff_value};
use super::{
    normalize_tool_name, truncate_chars, BOLD, DIM, LIGHT_BLUE, LIGHT_CYAN, LIGHT_GREEN,
    LIGHT_GREY, LIGHT_MAGENTA, LIGHT_RED, LIGHT_YELLOW, RESET, WHITE,
};

// =============================================================================
// Shared formatters — small helpers used by multiple render_* fns
// =============================================================================

pub(super) fn header(title: &str, color: bool) -> String {
    if color {
        format!("{}── {} ──{}", LIGHT_CYAN, title, RESET)
    } else {
        format!("── {} ──", title)
    }
}

fn field(name: &str, value: &str, color: bool) -> String {
    if color {
        format!("  {}{}:{} {}{}{}\n", BOLD, name, RESET, LIGHT_CYAN, value, RESET)
    } else {
        format!("  {}: {}\n", name, value)
    }
}

fn status_icon(status: &str) -> (&'static str, &'static str) {
    match status {
        "completed" | "success" | "clean" => ("", LIGHT_GREEN),
        "failed" | "error" | "dirty" | "high" => ("", LIGHT_RED),
        "warning" | "medium" => ("", LIGHT_YELLOW),
        "skipped" => ("", DIM),
        "low" => ("·", LIGHT_GREEN),
        _ => ("", WHITE),
    }
}

fn suffix(symbol_count: u64, color: &str, reset: &str) -> String {
    if symbol_count == 0 {
        String::new()
    } else {
        format!("  {}[{} symbols]{}", color, symbol_count, reset)
    }
}

fn line_for(data: &Value) -> u64 {
    // Top-level `line` is the legacy flat shape (a real source line
    // number). The canonical `AnalysisResult` only carries the
    // anchor node's byte range in `results[0].byte_range` — and
    // `byte_range[0]` is a *byte offset* in the source file, not a
    // line number. Showing a byte offset as a line number is
    // misleading (e.g. an anchor at byte 15342 in a long file would
    // render as "Line: 15342" for a symbol that is actually on a
    // much earlier source line). The renderer does not have file
    // contents available to convert bytes to lines, so for the
    // canonical shape we return 0 — the caller interprets 0 as
    // "no real line available, start the gutter at 1" and omits
    // the `Line` field. See the
    // `byte_range_to_line_range` helper in `helpers.rs` for the
    // proper conversion when file contents are available.
    if let Some(n) = data.get("line").and_then(|v| v.as_u64()) {
        return n;
    }
    0
}

/// Strip ANSI CSI escape sequences from a string. Used by tests
/// (and only by tests) to assert on the visible text of the
/// rendered output. Handles only the SGR (colour) subset that
/// this module emits: `\x1b[<n>m`.
#[cfg(test)]
fn strip_ansi(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    let mut iter = s.chars().peekable();
    while let Some(c) = iter.next() {
        if c == '\x1b' {
            // Skip `ESC[`
            if iter.peek() == Some(&'[') {
                iter.next();
                // Skip parameters (digits and semicolons) and the
                // terminating letter.
                for c2 in iter.by_ref() {
                    if c2.is_ascii_alphabetic() {
                        break;
                    }
                }
                continue;
            }
        }
        out.push(c);
    }
    out
}

fn extract_array(data: &Value, keys: &[&str]) -> Vec<Value> {
    if let Some(arr) = data.as_array() {
        return arr.clone();
    }
    for k in keys {
        if let Some(arr) = data.get(*k).and_then(|v| v.as_array()) {
            return arr.clone();
        }
    }
    Vec::new()
}

// =============================================================================
// Tree rendering
// =============================================================================

/// Render a project structure as an ASCII tree with branch glyphs.
pub fn render_tree(nodes: &[Value], color: bool) -> String {
    let mut out = String::new();
    for (i, node) in nodes.iter().enumerate() {
        out.push_str(&render_tree_node(node, "", i == nodes.len() - 1, color, true));
    }
    out
}

fn render_tree_node(
    node: &Value,
    prefix: &str,
    is_last: bool,
    color: bool,
    is_root: bool,
) -> String {
    let mut out = String::new();
    let name = node.get("name").and_then(|v| v.as_str()).unwrap_or("?");
    let node_type = node.get("type").and_then(|v| v.as_str()).unwrap_or("file");
    let symbol_count = node
        .get("symbol_count")
        .or_else(|| node.get("symbols"))
        .and_then(|v| v.as_u64())
        .unwrap_or(0);
    let children = node.get("children").and_then(|v| v.as_array());

    let connector = if is_root {
        ""
    } else if is_last {
        "└── "
    } else {
        "├── "
    };

    let name_color = if color {
        match node_type {
            "directory" | "dir" => LIGHT_BLUE,
            "module" => LIGHT_MAGENTA,
            _ => WHITE,
        }
    } else {
        ""
    };
    let count_color = if color { DIM } else { "" };
    let reset = if color { RESET } else { "" };

    if is_root {
        out.push_str(&format!(
            "{}{}{}{}\n",
            name_color, name, reset, suffix(symbol_count, count_color, reset),
        ));
    } else {
        // `suffix(symbol_count, count_color, reset)` already wraps the
        // symbol-count line in `count_color` and ends with `reset`.
        // The earlier code emitted `count_color` immediately before
        // the suffix (and a trailing `reset` after it), so the final
        // output was `…reset {color} [N symbols]{reset} reset…` —
        // printing empty ANSI escapes when `symbol_count == 0` and
        // leaving a trailing reset that does nothing useful when it
        // is non-zero. Drop both the leading `count_color` and the
        // trailing `reset`; the suffix already opens and closes
        // colour for the symbol-count segment.
        out.push_str(&format!(
            "{}{}{}{}{}{}\n",
            prefix,
            connector,
            name_color,
            name,
            reset,
            suffix(symbol_count, count_color, reset),
        ));
    }

        if let Some(kids) = children {
            // The child prefix is the vertical continuation that should
            // appear to the left of a grandchild's connector — it shows
            // whether this node has a sibling (│) or is the last ( ).
            // Root nodes pass no continuation because they have no
            // connector themselves; the first level of children sits at
            // column 0.
            let child_prefix = if is_last { "    " } else { "" };
            let combined_prefix = format!("{}{}", prefix, child_prefix);
            for (i, child) in kids.iter().enumerate() {
                out.push_str(&render_tree_node(
                    child,
                    &combined_prefix,
                    i == kids.len() - 1,
                    color,
                    false,
                ));
            }
        }
    out
}

// =============================================================================
// Per-tool render functions
// =============================================================================

fn render_search(data: &Value, query: &str, color: bool) -> String {
    let arr = extract_array(data, &["results", "items"]);
    if arr.is_empty() {
        return format!(
            "{}\n  No results for: {}\n",
            header(&format!("Search: \"{}\"", query), color),
            query,
        );
    }
    let mut out = header(&format!("Search: \"{}\" ({} results)", query, arr.len()), color);
    out.push('\n');
    for (idx, r) in arr.iter().enumerate() {
        let file = r.get("file_path").and_then(|v| v.as_str()).unwrap_or("?");
        let symbol = r
            .get("symbol")
            .or_else(|| r.get("symbol_name"))
            .and_then(|v| v.as_str());
        let symbol_type = r.get("symbol_type").and_then(|v| v.as_str());
        // The score is a `Score` struct with `overall`/`neural`/`text`/
        // `structural`; we want a single 0..1 composite for the CLI.
        let score = r
            .get("score")
            .and_then(|v| v.get("overall"))
            .and_then(|v| v.as_f64())
            .or_else(|| r.get("score").and_then(|v| v.as_f64()));
        let signature = r.get("signature").and_then(|v| v.as_str());
        let context = r.get("context").and_then(|v| v.as_str());
        // `trim_search` emits `snippet` (a one-line preview built from
        // `context` / `content` / `signature`). When the LLM is
        // looking at a trimmed payload we may not have `signature`
        // or `context` to fall back on, so read `snippet` directly.
        let snippet = r.get("snippet").and_then(|v| v.as_str());
        let byte_range = r.get("byte_range").and_then(|v| v.as_array());

        out.push_str(&format!(
            "  {}{}.{} {}",
            if color { BOLD } else { "" },
            idx + 1,
            if color { RESET } else { "" },
            if color { LIGHT_YELLOW } else { "" },
        ));
        out.push_str(file);
        out.push_str(if color { RESET } else { "" });

        if let Some(sym) = symbol {
            out.push_str(&format!(" :: {}{}{}",
                if color { LIGHT_CYAN } else { "" },
                sym,
                if color { RESET } else { "" },
            ));
        }

        if let Some(typ) = symbol_type {
            out.push_str(&format!(" {}[{}]{}",
                if color { DIM } else { "" },
                typ,
                if color { RESET } else { "" },
            ));
        }

        if let Some(sc) = score {
            let pct = (sc * 100.0).round() as usize;
            out.push_str(&format!("  {}{}%{}",
                if color { DIM } else { "" },
                pct,
                if color { RESET } else { "" }
            ));
        }
        out.push('\n');

        // Show the signature or first context line, whichever the handler
        // populated. Trim to keep the CLI output compact. The fallback
        // chain is signature → context → snippet, but a populated-but-
        // empty (whitespace-only) `signature` must not block the
        // fallback to `context` or `snippet`. We track a `printed`
        // flag so an empty signature falls through, and so the byte-
        // range hint only appears when none of the three text sources
        // produced output.
        let mut printed = false;
        if let Some(sig) = signature {
            let trimmed = sig.trim();
            if !trimmed.is_empty() {
                out.push_str(&format!("      {}{}{}\n",
                    if color { DIM } else { "" },
                    truncate_chars(trimmed, 160),
                    if color { RESET } else { "" },
                ));
                printed = true;
            }
        }
        if !printed {
            if let Some(ctx) = context {
                let first = ctx.lines().next().unwrap_or("").trim();
                if !first.is_empty() {
                    out.push_str(&format!("      {}{}{}\n",
                        if color { DIM } else { "" },
                        truncate_chars(first, 160),
                        if color { RESET } else { "" },
                    ));
                    printed = true;
                }
            }
        }
        if !printed {
            if let Some(snip) = snippet {
                // `snippet` is already a single short line; trim()
                // once more to be defensive against leading/trailing
                // whitespace.
                let trimmed = snip.trim();
                if !trimmed.is_empty() {
                    out.push_str(&format!("      {}{}{}\n",
                        if color { DIM } else { "" },
                        truncate_chars(trimmed, 160),
                        if color { RESET } else { "" },
                    ));
                    printed = true;
                }
            }
        }
        // Surface byte ranges when no signature/context/snippet is
        // available — helps the user locate the hit in a very large
        // file. The check is "did we print anything?" rather than
        // "are all three fields None?" so an empty `signature` falls
        // through to a real `context` or `snippet`, and only emits
        // the byte-range hint when there is genuinely no text.
        if !printed {
            if let Some(br) = byte_range {
                if br.len() == 2 {
                    let start = br[0].as_u64().unwrap_or(0);
                    let end = br[1].as_u64().unwrap_or(0);
                    if end > start {
                        out.push_str(&format!("      {}(bytes {}-{}){}\n",
                            if color { DIM } else { "" },
                            start, end,
                            if color { RESET } else { "" },
                        ));
                    }
                }
            }
        }
    }
    out
}

fn render_context(data: &Value, node_id: &str, color: bool) -> String {
    // `ContextHandler` returns a `AnalysisResult` (see
    // `src/cli/leindex/types.rs::AnalysisResult`) with shape:
    //   { query, results: [SearchResult, ...], context: Option<String>,
    //     tokens_used, processing_time_ms }
    // The expanded PDG text lives in `context`, not `content`. Node
    // metadata (symbol, file, type, line) is not on the top level —
    // it lives in `results[0]` (a `SearchResult`). Old callers
    // sometimes still emit a flat `content` / `file_path` /
    // `symbol_type` / `line` / `symbol` shape (e.g. the dispatcher
    // pre-trim payloads), so we fall back to that path before
    // showing the `results` summary.
    let mut out = header(&format!("Context: {}", node_id), color);
    out.push('\n');
    // Pull node metadata from `results[0]` (the canonical anchor
    // node) when present. Fall back to top-level fields for the
    // legacy flat shape.
    let anchor = data
        .get("results")
        .and_then(|v| v.as_array())
        .and_then(|a| a.first());
    if let Some(sym) = anchor
        .and_then(|r| r.get("symbol_name"))
        .and_then(|v| v.as_str())
        .or_else(|| data.get("symbol").and_then(|v| v.as_str()))
    {
        out.push_str(&field("Symbol", sym, color));
    }
    if let Some(file) = anchor
        .and_then(|r| r.get("file_path"))
        .and_then(|v| v.as_str())
        .or_else(|| data.get("file_path").and_then(|v| v.as_str()))
    {
        out.push_str(&field("File", file, color));
    }
    if let Some(typ) = anchor
        .and_then(|r| r.get("symbol_type"))
        .and_then(|v| v.as_str())
        .or_else(|| data.get("symbol_type").and_then(|v| v.as_str()))
    {
        out.push_str(&field("Type", typ, color));
    }
    // `Line` field: only emit when we have a real source-line
    // number. The top-level `line` field (legacy flat shape) is
    // authoritative. The anchor's `byte_range[0]` is a byte offset
    // (not a line) so we must NOT surface it as a line number here
    // — see `line_for`. When the canonical shape is in play and no
    // real line is available, drop the field; the gutter will
    // number the snippet starting at 1.
    if let Some(line) = data.get("line").and_then(|v| v.as_u64()) {
        out.push_str(&field("Line", &line.to_string(), color));
    } else if let Some(br) = anchor
        .and_then(|r| r.get("byte_range"))
        .and_then(|v| v.as_array())
    {
        // Show the byte range as a "Range: bytes X-Y" hint so the
        // user still has a concrete pointer to the location, but
        // don't mislabel a byte offset as a line number.
        if br.len() == 2 {
            let start = br[0].as_u64().unwrap_or(0);
            let end = br[1].as_u64().unwrap_or(0);
            if end > start {
                out.push_str(&field(
                    "Range",
                    &format!("bytes {}-{}", start, end),
                    color,
                ));
            }
        }
    }
    // The expanded PDG body is in `context` (canonical) or
    // `content` (legacy flat shape). Either way it is a multi-line
    // string; we render it with a line-number gutter. When a real
    // source-line number is available the gutter starts at that
    // line; otherwise it starts at 1 (relative to the snippet) so
    // the user does not see a byte offset mislabelled as a line.
    let body = data
        .get("context")
        .and_then(|v| v.as_str())
        .or_else(|| data.get("content").and_then(|v| v.as_str()));
    if let Some(snippet) = body {
        out.push('\n');
        let base = line_for(data);
        let gutter_base = if base == 0 { 1 } else { base };
        for (i, l) in snippet.lines().enumerate() {
            let n = gutter_base.saturating_add(i as u64);
            let gutter = format!("{:>4}", n);
            out.push_str(&format!(
                "  {}{}{}{}\n",
                if color { DIM } else { "" },
                gutter,
                if color { RESET } else { "" },
                l,
            ));
        }
    } else if let Some(results) = data.get("results").and_then(|v| v.as_array()) {
        for r in results {
            let symbol = r.get("symbol_name").and_then(|v| v.as_str()).unwrap_or("?");
            let file = r.get("file_path").and_then(|v| v.as_str()).unwrap_or("?");
            out.push_str(&format!("{}{}{} {}{}{}\n",
                if color { LIGHT_CYAN } else { "" }, symbol, if color { RESET } else { "" },
                if color { DIM } else { "" }, file, if color { RESET } else { "" }));
        }
    }
    out
}

fn render_diagnostics(data: &Value, color: bool) -> String {
    let mut out = header("Diagnostics", color);
    out.push('\n');
    if let Some(p) = data.get("project_path").and_then(|v| v.as_str()) {
        out.push_str(&field("Project", p, color));
    }
    if let Some(v) = data.get("indexed_files").and_then(|v| v.as_u64()) {
        out.push_str(&field("Indexed files", &v.to_string(), color));
    }
    if let Some(v) = data.get("symbol_count").and_then(|v| v.as_u64()) {
        out.push_str(&field("Symbols", &v.to_string(), color));
    }
    if let Some(v) = data.get("index_size_mb").and_then(|v| v.as_f64()) {
        out.push_str(&field("Index size", &format!("{:.2} MB", v), color));
    }
    if let Some(v) = data.get("stale").and_then(|v| v.as_bool()) {
        out.push_str(&field("Stale", &v.to_string(), color));
    }
    if let Some(v) = data.get("last_indexed_secs_ago").and_then(|v| v.as_u64()) {
        out.push_str(&field("Last indexed", &format!("{}s ago", v), color));
    }
    if let Some(arr) = data.get("issues").and_then(|v| v.as_array()) {
        if !arr.is_empty() {
            out.push('\n');
            out.push_str("  Issues:\n");
            for issue in arr.iter().take(10) {
                let sev = issue
                    .get("severity")
                    .and_then(|v| v.as_str())
                    .unwrap_or("info");
                let msg = issue
                    .get("message")
                    .and_then(|v| v.as_str())
                    .unwrap_or("?");
                let sev_color = if color {
                    match sev {
                        "error" => LIGHT_RED,
                        "warning" => LIGHT_YELLOW,
                        _ => LIGHT_BLUE,
                    }
                } else {
                    ""
                };
                out.push_str(&format!(
                    "    {}{}{} {}{}{}\n",
                    sev_color, sev, if color { RESET } else { "" }, msg, "",
                    "",
                ));
            }
        }
    }
    out
}

fn render_project_map(data: &Value, color: bool) -> String {
    let mut out = header("Project Structure", color);
    out.push('\n');
    if let Some(tree) = data.get("tree").and_then(|v| v.as_array()) {
        out.push_str(&render_tree(tree, color));
    } else if let Some(roots) = data.get("root").map(|v| vec![v.clone()]) {
        out.push_str(&render_tree(&roots, color));
    } else if let Some(files) = data.get("files").and_then(|v| v.as_array()) {
        let tree = build_tree_from_files(files);
        if tree.is_empty() {
            // No directory info is available in the file entries (the
            // handler ships basenames, not full paths), so render a flat
            // ranked list rather than fabricating fake directories.
            out.push_str(&render_flat_files(files, color));
        } else {
            out.push_str(&render_tree(&tree, color));
        }
    }
    if let Some(stats) = data.get("stats") {
        out.push('\n');
        if let Some(v) = stats.get("total_files").and_then(|v| v.as_u64()) {
            out.push_str(&field("Files", &v.to_string(), color));
        }
        if let Some(v) = stats.get("total_symbols").and_then(|v| v.as_u64()) {
            out.push_str(&field("Symbols", &v.to_string(), color));
        }
        if let Some(v) = stats.get("avg_complexity").and_then(|v| v.as_f64()) {
            out.push_str(&field("Avg complexity", &format!("{:.1}", v), color));
        }
        if let Some(v) = stats.get("total_loc").and_then(|v| v.as_u64()) {
            out.push_str(&field("Lines of code", &v.to_string(), color));
        }
    }
    out
}

fn render_flat_files(files: &[Value], color: bool) -> String {
    let mut out = String::new();
    out.push_str(&format!(
        "  {}(flat list — files ordered as returned){}\n",
        if color { DIM } else { "" },
        if color { RESET } else { "" },
    ));
    for (i, f) in files.iter().enumerate() {
        let path = f
            .get("path")
            .and_then(|v| v.as_str())
            .or_else(|| f.get("relative_path").and_then(|v| v.as_str()))
            .unwrap_or("?");
        let syms = f.get("symbol_count").and_then(|v| v.as_u64()).unwrap_or(0);
        let cx = f.get("total_complexity").and_then(|v| v.as_u64()).unwrap_or(0);
        let deps = f.get("incoming_dependencies").and_then(|v| v.as_u64()).unwrap_or(0)
            + f.get("outgoing_dependencies").and_then(|v| v.as_u64()).unwrap_or(0);
        // The complexity value is shown as a colour-coded integer
        // (`cx:{N}`); the human-readable label ("low" / "med" /
        // "high") was previously computed alongside the colour but
        // never rendered, so simplify the conditional to just the
        // colour string. When colour is disabled the colour string
        // is the empty literal.
        let cx_color = if color {
            match cx {
                0..=20 => LIGHT_GREEN,
                21..=60 => LIGHT_YELLOW,
                _ => LIGHT_RED,
            }
        } else {
            ""
        };
        out.push_str(&format!(
            "  {}{:>3}.{} {}{}{}  {}{} sym  cx:{}{}{}  deps:{}\n",
            if color { BOLD } else { "" },
            i + 1,
            if color { RESET } else { "" },
            if color { LIGHT_YELLOW } else { "" },
            path,
            if color { RESET } else { "" },
            if color { DIM } else { "" },
            syms,
            cx_color,
            cx,
            if color { RESET } else { "" },
            deps,
        ));
    }
    out
}

/// Convert a flat list of `{path, relative_path, symbol_count, ...}`
/// entries into a nested directory tree suitable for `render_tree`.
/// Returns an empty Vec if the file entries don't carry directory
/// information (caller falls back to flat rendering).
fn build_tree_from_files(files: &[Value]) -> Vec<Value> {
    use std::collections::BTreeMap;

    // Bail out unless at least one entry has a path with a directory
    // separator — otherwise we'd fabricate a meaningless single-level
    // tree from basenames.
    let any_with_dir = files.iter().any(|f| {
        f.get("relative_path")
            .and_then(|v| v.as_str())
            .or_else(|| f.get("path").and_then(|v| v.as_str()))
            .map(|p| p.contains('/') || p.contains('\\'))
            .unwrap_or(false)
    });
    if !any_with_dir {
        return Vec::new();
    }

    // A `Node` here is a tiny tree of name -> (entry, children).
    struct Node {
        entry: Option<Value>,
        children: BTreeMap<String, Node>,
    }

    impl Node {
        fn new() -> Self {
            Self {
                entry: None,
                children: BTreeMap::new(),
            }
        }
        /// Convert a directory node to a `{name, type, children}` JSON
        /// value. File nodes pass through their entry. Each child is
        /// converted using its own key as the name so nested directory
        /// labels stay distinct instead of inheriting the parent's
        /// segment.
        fn into_value(self, name: &str) -> Value {
            let children: Vec<Value> = self
                .children
                .into_iter()
                .map(|(child_name, child)| child.into_value(&child_name))
                .collect();
            if let Some(mut entry) = self.entry {
                if let Some(obj) = entry.as_object_mut() {
                    if !children.is_empty() {
                        obj.insert("children".to_string(), Value::Array(children));
                    }
                }
                entry
            } else {
                serde_json::json!({
                    "name": name,
                    "type": "directory",
                    "children": children,
                })
            }
        }
    }

    let mut root = Node::new();
    for file in files {
        let rel = file
            .get("relative_path")
            .and_then(|v| v.as_str())
            .or_else(|| file.get("path").and_then(|v| v.as_str()))
            .unwrap_or("?");
        // Normalize to forward slashes for stable tree building.
        let rel = rel.replace('\\', "/");
        let parts: Vec<&str> = rel.split('/').filter(|p| !p.is_empty()).collect();
        let mut node = &mut root;
        for (i, part) in parts.iter().enumerate() {
            let key = part.to_string();
            let is_file = i + 1 == parts.len();
            let child = node.children.entry(key.clone()).or_insert_with(Node::new);
            if is_file {
                let mut entry = file.clone();
                if let Some(obj) = entry.as_object_mut() {
                    obj.entry("name".to_string())
                        .or_insert(Value::String((*part).to_string()));
                    obj.entry("type".to_string())
                        .or_insert(Value::String("file".to_string()));
                }
                child.entry = Some(entry);
            }
            node = child;
        }
    }

    // The root is a virtual container with no name of its own — return
    // its top-level children directly so `render_tree` shows them as
    // siblings rather than nested under a fabricated "root" node.
    let mut top: Vec<Value> = Vec::new();
    for (name, child) in root.children.into_iter() {
        top.push(child.into_value(&name));
    }
    top
}

fn render_impact(data: &Value, color: bool) -> String {
    let mut out = header("Impact Analysis", color);
    out.push('\n');
    if let Some(sym) = data.get("symbol").and_then(|v| v.as_str()) {
        out.push_str(&field("Symbol", sym, color));
    }
    if let Some(risk) = data.get("risk_level").and_then(|v| v.as_str()) {
        let (icon, c) = if color {
            match risk.to_lowercase().as_str() {
                "high" => ("", LIGHT_RED),
                "medium" => ("", LIGHT_YELLOW),
                "low" => ("", LIGHT_GREEN),
                _ => ("", WHITE),
            }
        } else {
            ("", "")
        };
        out.push_str(&format!(
            "  {}{}:{} {}{} {}{}\n",
            if color { BOLD } else { "" },
            "Risk",
            if color { RESET } else { "" },
            c,
            icon,
            risk,
            if color { RESET } else { "" },
        ));
    }
    render_impact_side(data, "forward_impact", "Forward (callers of callees)", "", color, &mut out);
    render_impact_side(data, "backward_impact", "Backward (what calls this)", "", color, &mut out);
    out
}

fn render_impact_side(
    data: &Value,
    key: &str,
    label: &str,
    arrow: &str,
    color: bool,
    out: &mut String,
) {
    if let Some(arr) = data.get(key).and_then(|v| v.as_array()) {
        if !arr.is_empty() {
            out.push('\n');
            out.push_str(&format!("  {}:\n", label));
            for item in arr.iter().take(20) {
                let name = item.get("name").and_then(|v| v.as_str()).unwrap_or("?");
                let file = item.get("file").and_then(|v| v.as_str()).unwrap_or("");
                let line = item.get("line").and_then(|v| v.as_u64());
                out.push_str(&format!(
                    "    {}{}{} {}{}{}{}{}\n",
                    if color { LIGHT_CYAN } else { "" },
                    arrow,
                    name,
                    if color { RESET } else { "" },
                    if color { DIM } else { "" },
                    file,
                    line.map(|l| format!(":{}", l)).unwrap_or_default(),
                    if color { RESET } else { "" },
                ));
            }
        }
    }
}

fn render_symbol_lookup(data: &Value, color: bool) -> String {
    // `lookup_single_symbol` returns the shape (see
    // `src/cli/mcp/symbol_lookup_handler.rs::lookup_single_symbol`):
    //   { symbol, type, file, byte_range, complexity, language,
    //     callers, callees, impact_radius, [source] }
    // where each caller/callee entry is { name, file, type } (no
    // `line` field). Older renderers read `file_path` / `line` /
    // `symbol_type` / `signature` and end up emitting mostly blanks.
    //
    // Batch mode (`lookup_symbols_batch`) returns the wrapper
    //   { batch: true, count, results: [ ...singleSymbolEntries ] }
    // The previous renderer silently dropped the wrapper and
    // emitted a header followed by nothing when `symbol` /
    // `file` / `type` were absent at the top level. Branch on
    // `batch:true` and recurse into each entry.
    if data.get("batch").and_then(|v| v.as_bool()) == Some(true) {
        let count = data.get("count").and_then(|v| v.as_u64()).unwrap_or(0);
        let mut out = header("Symbol Lookup (batch)", color);
        out.push('\n');
        out.push_str(&field("Count", &count.to_string(), color));
        if let Some(arr) = data.get("results").and_then(|v| v.as_array()) {
            if arr.is_empty() {
                out.push_str("  (no results)\n");
                return out;
            }
            for (idx, entry) in arr.iter().enumerate() {
                out.push('\n');
                out.push_str(&format!(
                    "  {}{}#{}{} {}\n",
                    if color { BOLD } else { "" },
                    if color { DIM } else { "" },
                    idx + 1,
                    if color { RESET } else { "" },
                    entry
                        .get("symbol")
                        .and_then(|v| v.as_str())
                        .unwrap_or("?"),
                ));
                out.push_str(&render_symbol_lookup_single(entry, color));
            }
        }
        return out;
    }
    let mut out = header("Symbol Lookup", color);
    out.push('\n');
    out.push_str(&render_symbol_lookup_single(data, color));
    out
}

/// Render a single symbol entry (the inner shape returned by
/// `lookup_single_symbol`). Lifted out of `render_symbol_lookup` so
/// the batch wrapper can recurse into each entry.
fn render_symbol_lookup_single(data: &Value, color: bool) -> String {
    let mut out = String::new();
    if let Some(sym) = data.get("symbol").and_then(|v| v.as_str()) {
        out.push_str(&field("Symbol", sym, color));
    }
    if let Some(file) = data.get("file").and_then(|v| v.as_str()) {
        out.push_str(&field("File", file, color));
    }
    if let Some(typ) = data.get("type").and_then(|v| v.as_str()) {
        out.push_str(&field("Type", typ, color));
    }
    if let Some(lang) = data.get("language").and_then(|v| v.as_str()) {
        out.push_str(&field("Language", lang, color));
    }
    if let Some(br) = data.get("byte_range").and_then(|v| v.as_array()) {
        if br.len() == 2 {
            let start = br[0].as_u64().unwrap_or(0);
            let end = br[1].as_u64().unwrap_or(0);
            if end > start {
                out.push_str(&field("Range", &format!("bytes {}-{}", start, end), color));
            }
        }
    }
    if let Some(cx) = data.get("complexity").and_then(|v| v.as_u64()) {
        out.push_str(&field("Complexity", &cx.to_string(), color));
    }
    if let Some(ir) = data.get("impact_radius").and_then(|v| v.as_object()) {
        let syms = ir.get("affected_symbols").and_then(|v| v.as_u64()).unwrap_or(0);
        let files = ir.get("affected_files").and_then(|v| v.as_u64()).unwrap_or(0);
        out.push_str(&field(
            "Impact",
            &format!("{} symbols / {} files", syms, files),
            color,
        ));
    }
    if let Some(src) = data.get("source").and_then(|v| v.as_str()) {
        // `lookup_single_symbol` already truncates to `char_budget/2`
        // chars; render the first 12 non-empty lines as a preview.
        out.push('\n');
        let mut shown = 0usize;
        for l in src.lines() {
            if l.trim().is_empty() {
                continue;
            }
            out.push_str(&format!(
                "      {}{}{}\n",
                if color { DIM } else { "" },
                truncate_chars(l, 160),
                if color { RESET } else { "" },
            ));
            shown += 1;
            if shown >= 12 {
                break;
            }
        }
    }

    if let Some(arr) = data.get("callers").and_then(|v| v.as_array()) {
        if !arr.is_empty() {
            out.push('\n');
            out.push_str("  Callers:\n");
            for c in arr.iter().take(15) {
                let name = c.get("name").and_then(|v| v.as_str()).unwrap_or("?");
                let file = c.get("file").and_then(|v| v.as_str()).unwrap_or("");
                let typ = c.get("type").and_then(|v| v.as_str()).unwrap_or("");
                // 7 placeholders, 7 args. The previous format string
                // had 8 `{}`s with 8 args, where the trailing empty
                // string was silently emitted as empty and could
                // mask a future placeholder/argument mismatch.
                out.push_str(&format!(
                    "{}{}{} {}{}{}{}\n",
                    if color { LIGHT_CYAN } else { "" },
                    name,
                    if color { RESET } else { "" },
                    if color { DIM } else { "" },
                    file,
                    if !typ.is_empty() {
                        format!(" [{}]", typ)
                    } else {
                        String::new()
                    },
                    if color { RESET } else { "" },
                ));
            }
        }
    }
    if let Some(arr) = data.get("callees").and_then(|v| v.as_array()) {
        if !arr.is_empty() {
            out.push('\n');
            out.push_str("  Callees:\n");
            for c in arr.iter().take(15) {
                let name = c.get("name").and_then(|v| v.as_str()).unwrap_or("?");
                let file = c.get("file").and_then(|v| v.as_str()).unwrap_or("");
                let typ = c.get("type").and_then(|v| v.as_str()).unwrap_or("");
                out.push_str(&format!(
                    "{}{}{} {}{}{}{}\n",
                    if color { LIGHT_CYAN } else { "" },
                    name,
                    if color { RESET } else { "" },
                    if color { DIM } else { "" },
                    file,
                    if !typ.is_empty() {
                        format!(" [{}]", typ)
                    } else {
                        String::new()
                    },
                    if color { RESET } else { "" },
                ));
            }
        }
    }
    out
}

fn render_phase(data: &Value, color: bool) -> String {
    let mut out = header("Phase Analysis", color);
    out.push('\n');
    if let Some(mode) = data.get("mode").and_then(|v| v.as_str()) {
        out.push_str(&field("Mode", mode, color));
    }
    if let Some(arr) = data.get("phases").and_then(|v| v.as_array()) {
        out.push('\n');
        for p in arr {
            let num = p.get("phase").and_then(|v| v.as_u64()).unwrap_or(0);
            let name = p.get("name").and_then(|v| v.as_str()).unwrap_or("?");
            let status = p.get("status").and_then(|v| v.as_str()).unwrap_or("?");
            let findings = p.get("findings").and_then(|v| v.as_u64()).unwrap_or(0);
            let (icon, c) = if color {
                status_icon(status)
            } else {
                (status_icon(status).0, "")
            };
            out.push_str(&format!(
                "  {}{}{} {}{}Phase {}{} {} {}{}({} findings){}\n",
                c,
                icon,
                if color { RESET } else { "" },
                if color { BOLD } else { "" },
                if color { RESET } else { "" },
                num,
                if color { RESET } else { "" },
                name,
                if color { DIM } else { "" },
                "",
                findings,
                if color { RESET } else { "" },
            ));
        }
    }
    if let Some(summary) = data.get("summary").and_then(|v| v.as_str()) {
        out.push('\n');
        let truncated = truncate_chars(summary, 300);
        out.push_str(&format!(
            "  {}{}:{} {}{}{}\n",
            if color { BOLD } else { "" },
            "Summary",
            if color { RESET } else { "" },
            if color { DIM } else { "" },
            truncated,
            if color { RESET } else { "" },
        ));
    }
    out
}

fn render_git_status(data: &Value, color: bool) -> String {
    let mut out = header("Git Status", color);
    out.push('\n');
    if let Some(b) = data.get("branch").and_then(|v| v.as_str()) {
        out.push_str(&field("Branch", b, color));
    }
    if let Some(s) = data.get("status").and_then(|v| v.as_str()) {
        let c = if color {
            match s {
                "clean" => LIGHT_GREEN,
                "dirty" => LIGHT_YELLOW,
                _ => WHITE,
            }
        } else {
            ""
        };
        out.push_str(&format!(
            "  {}{}:{} {}{}{}{}\n",
            if color { BOLD } else { "" },
            "Status",
            if color { RESET } else { "" },
            c,
            s,
            if color { RESET } else { "" },
            "",
        ));
    }
    git_status_section(data, "staged", "Staged", "+", LIGHT_GREEN, color, &mut out);
    git_status_section(data, "modified", "Modified", "~", LIGHT_YELLOW, color, &mut out);
    git_status_section(data, "untracked", "Untracked", "?", LIGHT_GREY, color, &mut out);
    git_status_section(data, "deleted", "Deleted", "", LIGHT_RED, color, &mut out);
    out
}

fn git_status_section(
    data: &Value,
    key: &str,
    label: &str,
    marker: &str,
    marker_color: &str,
    color: bool,
    out: &mut String,
) {
    if let Some(arr) = data.get(key).and_then(|v| v.as_array()) {
        if !arr.is_empty() {
            out.push('\n');
            out.push_str(&format!("  {}:\n", label));
            for f in arr.iter().take(20) {
                let path = f.get("path").and_then(|v| v.as_str()).unwrap_or("?");
                out.push_str(&format!(
                    "    {}{}{} {}\n",
                    if color { marker_color } else { "" },
                    marker,
                    if color { RESET } else { "" },
                    path,
                ));
            }
        }
    }
}

fn render_file_summary(data: &Value, color: bool) -> String {
    let mut out = header("File Summary", color);
    out.push('\n');
    if let Some(file) = data.get("file_path").and_then(|v| v.as_str()) {
        out.push_str(&field("File", file, color));
    }
    if let Some(lang) = data.get("language").and_then(|v| v.as_str()) {
        out.push_str(&field("Language", lang, color));
    }
    if let Some(size) = data.get("size").and_then(|v| v.as_u64()) {
        out.push_str(&field("Size", &format!("{} bytes", size), color));
    }
    if let Some(complexity) = data.get("complexity").and_then(|v| v.as_u64()) {
        let (label, c) = if color {
            match complexity {
                0..=5 => ("Low", LIGHT_GREEN),
                6..=15 => ("Medium", LIGHT_YELLOW),
                _ => ("High", LIGHT_RED),
            }
        } else {
            ("", "")
        };
        out.push_str(&format!(
            "  {}{}:{} {}{} {}{}{}\n",
            if color { BOLD } else { "" },
            "Complexity",
            if color { RESET } else { "" },
            c,
            complexity,
            if color { RESET } else { "" },
            label,
            if color { RESET } else { "" },
        ));
    }
    if let Some(arr) = data.get("symbols").and_then(|v| v.as_array()) {
        if !arr.is_empty() {
            out.push('\n');
            out.push_str("  Symbols:\n");
            for sym in arr.iter().take(20) {
                let name = sym.get("name").and_then(|v| v.as_str()).unwrap_or("?");
                let typ = sym.get("type").and_then(|v| v.as_str()).unwrap_or("symbol");
                let line = sym.get("line").and_then(|v| v.as_u64());
                let (icon, c) = if color {
                    match typ {
                        "function" | "fn" => ("ƒ", LIGHT_GREEN),
                        "method" => ("m", LIGHT_CYAN),
                        "struct" => ("S", LIGHT_MAGENTA),
                        "enum" => ("E", LIGHT_YELLOW),
                        "trait" => ("T", LIGHT_BLUE),
                        "impl" => ("I", LIGHT_MAGENTA),
                        "const" | "static" => ("c", LIGHT_CYAN),
                        "field" => ("f", LIGHT_YELLOW),
                        _ => ("", WHITE),
                    }
                } else {
                    ("", "")
                };
                out.push_str(&format!(
                    "    {}{}{} {}{}{}{}{}\n",
                    c,
                    icon,
                    if color { RESET } else { "" },
                    if color { LIGHT_CYAN } else { "" },
                    name,
                    if color { RESET } else { "" },
                    line.map(|l| format!(" :{}", l)).unwrap_or_default(),
                    "",
                ));
            }
        }
    }
    out
}

fn render_read_file(data: &Value, color: bool) -> String {
    let mut out = String::new();
    if let Some(path) = data.get("file_path").and_then(|v| v.as_str()) {
        out.push_str(&header(&format!("Read: {}", path), color));
        out.push('\n');
    }
    let content = data
        .get("content")
        .and_then(|v| v.as_str())
        .unwrap_or_default();
    let start = data.get("start_line").and_then(|v| v.as_u64()).unwrap_or(1);
    for (i, line) in content.lines().enumerate() {
        let n = start + i as u64;
        let gutter = format!("{:>4}", n);
        out.push_str(&format!(
            "  {}{}{}{}\n",
            if color { DIM } else { "" },
            gutter,
            if color { RESET } else { "" },
            line,
        ));
    }
    out
}

fn render_write(data: &Value, color: bool) -> String {
    // `WriteHandler` returns a confirmation payload with shape
    // (see `src/cli/mcp/write_handler.rs::WriteHandler::execute`):
    //   { success, file_path, language, symbols: [{name, type, range}] }
    // Routing it through `render_diff_value` produced an empty
    // string because the handler does not emit `diff` / `diffs` /
    // `diff_text`. Surface the success status, the file path, the
    // language, and the parsed symbol table so the CLI user sees
    // the confirmation and the structural context the handler
    // actually returned.
    let mut out = String::new();
    let success = data.get("success").and_then(|v| v.as_bool()).unwrap_or(false);
    let (status_label, status_color) = if success {
        ("Wrote", if color { LIGHT_GREEN } else { "" })
    } else {
        ("Write failed", if color { LIGHT_RED } else { "" })
    };
    out.push_str(&format!(
        "{}{}{}\n",
        status_color,
        status_label,
        if color { RESET } else { "" },
    ));
    if let Some(path) = data.get("file_path").and_then(|v| v.as_str()) {
        out.push_str(&field("File", path, color));
    }
    if let Some(lang) = data.get("language").and_then(|v| v.as_str()) {
        out.push_str(&field("Language", lang, color));
    }
    if let Some(arr) = data.get("symbols").and_then(|v| v.as_array()) {
        if !arr.is_empty() {
            out.push('\n');
            out.push_str(&format!(
                "  {}Symbols ({}):{}\n",
                if color { DIM } else { "" },
                arr.len(),
                if color { RESET } else { "" },
            ));
            for s in arr.iter().take(20) {
                let name = s.get("name").and_then(|v| v.as_str()).unwrap_or("?");
                let typ = s.get("type").and_then(|v| v.as_str()).unwrap_or("");
                out.push_str(&format!(
                    "    {}{}{} {}{}{}\n",
                    if color { LIGHT_CYAN } else { "" },
                    name,
                    if color { RESET } else { "" },
                    if color { DIM } else { "" },
                    if typ.is_empty() {
                        String::new()
                    } else {
                        format!("[{}]", typ)
                    },
                    if color { RESET } else { "" },
                ));
            }
            if arr.len() > 20 {
                out.push_str(&format!(
                    "    {}…and {} more{}\n",
                    if color { DIM } else { "" },
                    arr.len() - 20,
                    if color { RESET } else { "" },
                ));
            }
        }
    }
    out
}

fn render_edit_apply(data: &Value, color: bool) -> String {
    // `EditApplyHandler` returns a confirmation payload with shape
    // (see `src/cli/mcp/edit_apply_handler.rs::EditApplyHandler::
    // execute`):
    //   { success, changes_applied, file_path, edit_region,
    //     affected_symbols, affected_files, breaking_changes,
    //     [validation], [message] (no-op only) }
    // The handler never emits `diff` / `diffs` / `diff_text`, so
    // `render_diff_value` returns an empty string for the apply
    // response and the CLI prints nothing for a successful (or
    // no-op) apply. Surface the success status, the change count,
    // the file path, the affected-symbol/file summary, breaking
    // changes, and the surrounding-region excerpt.
    let mut out = String::new();
    let success = data.get("success").and_then(|v| v.as_bool()).unwrap_or(false);
    let changes_applied = data
        .get("changes_applied")
        .and_then(|v| v.as_u64())
        .unwrap_or(0);
    let (status_label, status_color) = if !success {
        ("Edit apply failed", if color { LIGHT_RED } else { "" })
    } else if changes_applied == 0 {
        ("No-op (content identical)", if color { LIGHT_YELLOW } else { "" })
    } else {
        ("Applied", if color { LIGHT_GREEN } else { "" })
    };
    out.push_str(&format!(
        "{}{}{}\n",
        status_color,
        status_label,
        if color { RESET } else { "" },
    ));
    if let Some(path) = data.get("file_path").and_then(|v| v.as_str()) {
        out.push_str(&field("File", path, color));
    }
    if let Some(msg) = data.get("message").and_then(|v| v.as_str()) {
        out.push_str(&field("Message", msg, color));
    }
    if let Some(arr) = data.get("affected_symbols").and_then(|v| v.as_array()) {
        if !arr.is_empty() {
            out.push_str(&field(
                "Affected symbols",
                &arr.len().to_string(),
                color,
            ));
        }
    }
    if let Some(arr) = data.get("affected_files").and_then(|v| v.as_array()) {
        if !arr.is_empty() {
            out.push_str(&field("Affected files", &arr.len().to_string(), color));
        }
    }
    if let Some(bc) = data.get("breaking_changes").and_then(|v| v.as_array()) {
        if !bc.is_empty() {
            out.push_str(&field(
                "Breaking changes",
                &bc.len().to_string(),
                color,
            ));
        }
    }
    if let Some(region_value) = data.get("edit_region") {
        // `edit_region` may be a string (surrounding code excerpt)
        // or an object (e.g. `{"start": 10, "end": 25}`) carrying
        // byte ranges from `trim_edit`. The string form is the
        // human-readable preview; the object form is what the
        // trimmer preserves for apply-shaped payloads where the
        // surrounding text was not retained. Render the object
        // form as a compact `{start,end}` so the region context
        // is never dropped from the CLI output.
        let region_text: Option<String> = if let Some(s) = region_value.as_str() {
            if s.is_empty() {
                None
            } else {
                Some(s.to_string())
            }
        } else if let Some(obj) = region_value.as_object() {
            let start = obj.get("start").and_then(|v| v.as_u64());
            let end = obj.get("end").and_then(|v| v.as_u64());
            match (start, end) {
                (Some(s), Some(e)) => Some(format!("bytes {s}..{e}")),
                (Some(s), None) => Some(format!("bytes {s}..")),
                (None, Some(e)) => Some(format!("bytes ..{e}")),
                (None, None) => Some("bytes ?".to_string()),
            }
        } else {
            None
        };
        if let Some(text) = region_text {
            out.push('\n');
            if region_value.is_string() {
                // String form: the surrounding text is a
                // multi-line excerpt. The header is a
                // marker line; the lines themselves are
                // emitted below as a per-line colorized
                // expansion. Putting the raw multi-line
                // `text` on the header would duplicate the
                // expansion and produce messy output.
                out.push_str(&format!(
                    "  {}Surrounding region:{}\n",
                    if color { DIM } else { "" },
                    if color { RESET } else { "" },
                ));
                for l in text.lines() {
                    out.push_str(&format!(
                        "      {}{}{}\n",
                        if color { DIM } else { "" },
                        truncate_chars(l, 160),
                        if color { RESET } else { "" },
                    ));
                }
            } else {
                // Object form (`{start, end}` etc.):
                // the `text` is already a compact single
                // line like `bytes 10..25`, so we put it
                // on the same line as the header and do
                // not expand it per-line.
                out.push_str(&format!(
                    "  {}Surrounding region:{} {}\n",
                    if color { DIM } else { "" },
                    if color { RESET } else { "" },
                    text,
                ));
            }
        }
    }
    out
}

// =============================================================================
// Central dispatch — single entry point for CLI tool rendering
// =============================================================================

/// Render a tool's value for the CLI surface. The MCP transport uses
/// the raw `Value` (clean JSON for the LLM); the CLI uses this function
/// to produce a human-readable, colored view of the same data.
pub fn render_tool_output(name: &str, data: &Value, args: &Value) -> String {
    let normalized = normalize_tool_name(name);
    let color = true;
    let query = args
        .get("query")
        .and_then(|v| v.as_str())
        .unwrap_or("");
    let node_id = args
        .get("node_id")
        .and_then(|v| v.as_str())
        .unwrap_or("");

    match normalized.as_str() {
        "leindex_search" | "search" => render_search(data, query, color),
        "leindex_context" | "context" => render_context(data, node_id, color),
        "leindex_diagnostics" | "diagnostics" => render_diagnostics(data, color),
        "leindex_project_map" | "project_map" => render_project_map(data, color),
        "leindex_impact_analysis" | "impact_analysis" => render_impact(data, color),
        "leindex_symbol_lookup" | "symbol_lookup" => render_symbol_lookup(data, color),
        "leindex_phase_analysis" | "phase_analysis" => render_phase(data, color),
        "leindex_git_status" | "git_status" => render_git_status(data, color),
        "leindex_file_summary" | "file_summary" => render_file_summary(data, color),
        "leindex_read_file" | "read_file" => render_read_file(data, color),
        "leindex_edit_preview" | "edit_preview" => render_diff_value(data, color),
        // `EditApplyHandler` returns a confirmation payload
        // (`success, changes_applied, file_path, edit_region, …`)
        // not a diff, so `render_diff_value` returns an empty
        // string for the apply response. Use the dedicated
        // confirmation renderer instead.
        "leindex_edit_apply" | "edit_apply" => render_edit_apply(data, color),
        // `WriteHandler` returns a confirmation payload
        // (`{success, file_path, language, symbols}`) not a diff, so
        // `render_diff_value` would return an empty string here.
        "leindex_write" | "write" => render_write(data, color),
        "leindex_rename_symbol" | "rename_symbol" => render_diff_value(data, color),
        _ => render_default(data, color),
    }
}

// =============================================================================
// Backward-compatible Formatter structs — thin wrappers for callers
// outside this module that build a formatter explicitly. New code
// should call `render_tool_output` instead.
// =============================================================================

/// Formatter for search results with ranked listings and scores
pub struct SearchFormatter {
    color: bool,
}

impl SearchFormatter {
    /// Create a new SearchFormatter with default settings
    pub fn new() -> Self {
        Self { color: true }
    }

    /// Enable or disable color output
    pub fn with_color(mut self, color: bool) -> Self {
        self.color = color;
        self
    }

    /// Format search results with ranking and scoring
    pub fn format(&self, results: &Value, query: &str) -> String {
        render_search(results, query, self.color)
    }
}

impl Default for SearchFormatter {
    fn default() -> Self {
        Self::new()
    }
}

/// Formatter for project structure/dependency tree visualization
pub struct ProjectMapFormatter {
    color: bool,
}

impl ProjectMapFormatter {
    /// Create a new ProjectMapFormatter with default settings
    pub fn new() -> Self {
        Self { color: true }
    }

    /// Enable or disable color output
    pub fn with_color(mut self, color: bool) -> Self {
        self.color = color;
        self
    }

    /// Format project structure data as a tree view
    pub fn format(&self, data: &Value) -> String {
        render_project_map(data, self.color)
    }
}

impl Default for ProjectMapFormatter {
    fn default() -> Self {
        Self::new()
    }
}

/// Formatter for project diagnostics and index status
pub struct DiagnosticsFormatter {
    color: bool,
}

impl DiagnosticsFormatter {
    /// Create a new DiagnosticsFormatter with default settings
    pub fn new() -> Self {
        Self { color: true }
    }

    /// Enable or disable color output
    pub fn with_color(mut self, color: bool) -> Self {
        self.color = color;
        self
    }

    /// Format diagnostics data including index stats and issues
    pub fn format(&self, data: &Value) -> String {
        render_diagnostics(data, self.color)
    }
}

impl Default for DiagnosticsFormatter {
    fn default() -> Self {
        Self::new()
    }
}

/// Formatter for symbol impact analysis showing forward/backward dependencies
pub struct ImpactFormatter {
    color: bool,
}

impl ImpactFormatter {
    /// Create a new ImpactFormatter with default settings
    pub fn new() -> Self {
        Self { color: true }
    }

    /// Enable or disable color output
    pub fn with_color(mut self, color: bool) -> Self {
        self.color = color;
        self
    }

    /// Format impact analysis data with risk levels and affected symbols
    pub fn format(&self, data: &Value) -> String {
        render_impact(data, self.color)
    }
}

impl Default for ImpactFormatter {
    fn default() -> Self {
        Self::new()
    }
}

/// Formatter for symbol lookup results with callers and callees
pub struct SymbolLookupFormatter {
    color: bool,
}

impl SymbolLookupFormatter {
    /// Create a new SymbolLookupFormatter with default settings
    pub fn new() -> Self {
        Self { color: true }
    }

    /// Enable or disable color output
    pub fn with_color(mut self, color: bool) -> Self {
        self.color = color;
        self
    }

    /// Format symbol lookup data showing definition, callers, and callees
    pub fn format(&self, data: &Value) -> String {
        render_symbol_lookup(data, self.color)
    }
}

impl Default for SymbolLookupFormatter {
    fn default() -> Self {
        Self::new()
    }
}

/// Formatter for phase analysis results
pub struct PhaseFormatter {
    color: bool,
}

impl PhaseFormatter {
    /// Create a new PhaseFormatter with default settings
    pub fn new() -> Self {
        Self { color: true }
    }

    /// Enable or disable color output
    pub fn with_color(mut self, color: bool) -> Self {
        self.color = color;
        self
    }

    /// Format phase analysis data with phase status and summaries
    pub fn format(&self, data: &Value) -> String {
        render_phase(data, self.color)
    }
}

impl Default for PhaseFormatter {
    fn default() -> Self {
        Self::new()
    }
}

/// Formatter for git status with staged, modified, and untracked files
pub struct GitStatusFormatter {
    color: bool,
}

impl GitStatusFormatter {
    /// Create a new GitStatusFormatter with default settings
    pub fn new() -> Self {
        Self { color: true }
    }

    /// Enable or disable color output
    pub fn with_color(mut self, color: bool) -> Self {
        self.color = color;
        self
    }

    /// Format git status data showing branch and file changes
    pub fn format(&self, data: &Value) -> String {
        render_git_status(data, self.color)
    }
}

impl Default for GitStatusFormatter {
    fn default() -> Self {
        Self::new()
    }
}

/// Formatter for file summary with symbols and complexity metrics
pub struct FileSummaryFormatter {
    color: bool,
}

impl FileSummaryFormatter {
    /// Create a new FileSummaryFormatter with default settings
    pub fn new() -> Self {
        Self { color: true }
    }

    /// Enable or disable color output
    pub fn with_color(mut self, color: bool) -> Self {
        self.color = color;
        self
    }

    /// Format file summary data with file info and symbol list
    pub fn format(&self, data: &Value) -> String {
        render_file_summary(data, self.color)
    }
}

impl Default for FileSummaryFormatter {
    fn default() -> Self {
        Self::new()
    }
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cli::mcp::output::trim::trim_search;

    fn v(s: &str) -> Value {
        serde_json::from_str(s).unwrap()
    }

    #[test]
    fn test_render_tree_basic() {
        let tree = v(r#"[
            {"name": "src", "type": "directory", "children": [
                {"name": "main.rs", "type": "file", "symbol_count": 5, "children": []},
                {"name": "lib.rs", "type": "file", "symbol_count": 12, "children": []}
            ]}
        ]"#);
        let s = render_tree(tree.as_array().unwrap(), false);
        assert!(s.contains("src"), "root dir name missing: {}", s);
        assert!(s.contains("main.rs"), "child file missing: {}", s);
        assert!(s.contains("lib.rs"), "child file missing: {}", s);
        // Directory connector at root
        assert!(s.contains("├──"), "missing connector: {}", s);
    }

    #[test]
    fn test_build_tree_preserves_child_names() {
        // Regression: each nested directory's `name` field must equal
        // its own path segment, not the parent's. Otherwise the tree
        // renderer shows duplicated labels like "src → src → main.rs"
        // for any multi-level path.
        let files = v(r#"[
            {"relative_path": "src/cli/main.rs"},
            {"relative_path": "src/cli/sub/lib.rs"},
            {"relative_path": "tests/integration.rs"}
        ]"#);
        let tree = build_tree_from_files(files.as_array().unwrap());
        // The top-level must be src + tests.
        let names: Vec<String> = tree
            .iter()
            .map(|n| n.get("name").and_then(|v| v.as_str()).unwrap_or("").to_string())
            .collect();
        assert_eq!(names, vec!["src", "tests"]);
        // Inside `src`, the child directory must be `cli` (not `src`).
        let src = tree.iter().find(|n| n["name"] == "src").unwrap();
        let src_children = src.get("children").and_then(|v| v.as_array()).unwrap();
        let src_child_names: Vec<String> = src_children
            .iter()
            .map(|n| n.get("name").and_then(|v| v.as_str()).unwrap_or("").to_string())
            .collect();
        assert_eq!(src_child_names, vec!["cli"]);
        // Inside `cli`, the grandchild directory must be `sub`.
        let cli = src_children
            .iter()
            .find(|n| n["name"] == "cli")
            .unwrap();
        let cli_children = cli.get("children").and_then(|v| v.as_array()).unwrap();
        let cli_child_names: Vec<String> = cli_children
            .iter()
            .map(|n| n.get("name").and_then(|v| v.as_str()).unwrap_or("").to_string())
            .collect();
        assert_eq!(cli_child_names, vec!["main.rs", "sub"]);
    }

    #[test]
    fn test_render_tree_indents_children() {
        let tree = v(r#"[
            {"name": "src", "type": "directory", "children": [
                {"name": "a.rs", "type": "file", "symbol_count": 1, "children": []}
            ]},
            {"name": "tests", "type": "directory", "children": [
                {"name": "b.rs", "type": "file", "symbol_count": 1, "children": []}
            ]}
        ]"#);
        let s = render_tree(tree.as_array().unwrap(), false);
        // Both files appear, one per root child.
        assert!(s.contains("a.rs"));
        assert!(s.contains("b.rs"));
        // `src` is the first root child so it has a `│` continuation
        // (its sibling follows), and `tests` is the last so it has a
        // trailing space indent.
        let lines: Vec<&str> = s.lines().collect();
        // The first file line should sit under `src` with a `│` prefix.
        let a_line = lines.iter().find(|l| l.contains("a.rs")).unwrap();
        assert!(a_line.contains("│   └──"), "a.rs should be under 'src' with continuation: {:?}", a_line);
        // The second file line should sit under `tests` with a space
        // prefix (no continuation since tests is the last root child).
        let b_line = lines.iter().find(|l| l.contains("b.rs")).unwrap();
        assert!(b_line.starts_with("    └──"), "b.rs should be under 'tests' with space indent: {:?}", b_line);
    }

    #[test]
    fn test_render_tool_output_dispatches_by_name() {
        let args = v(r#"{"query": "foo", "top_k": 1}"#);
        // Search payload (using trimmed form so the renderer sees what
        // the LLM would see).
        let search_data = trim_search(&v(r#"{"results": [{"file_path": "/p.rs", "symbol_name": "f", "score": {"overall": 0.5}}]}"#));
        let s = render_tool_output("leindex.search", &search_data, &args);
        assert!(s.contains("Search: \"foo\""), "got: {}", s);
        assert!(s.contains("/p.rs"), "got: {}", s);
    }

    #[test]
    fn test_render_search_uses_snippet_field() {
        // Regression: when the trimmed payload only has `snippet` (no
        // `signature` or `context`), the search renderer should still
        // surface a preview line.
        let args = v(r#"{"query": "foo", "top_k": 1}"#);
        let payload = v(r#"{
            "count": 1,
            "results": [{
                "file_path": "/p.rs",
                "symbol": "main",
                "symbol_type": "function",
                "score": 0.9,
                "snippet": "fn main() { return 0; }"
            }]
        }"#);
        let s = render_tool_output("leindex.search", &payload, &args);
        assert!(s.contains("/p.rs"), "got: {}", s);
        assert!(s.contains("fn main()"), "snippet preview missing: {}", s);
    }

    #[test]
    fn test_render_tool_output_falls_back_to_pretty_json() {
        let data = v(r#"{"custom_field": 42, "items": [1, 2, 3]}"#);
        let s = render_tool_output("leindex.unknown_tool", &data, &Value::Null);
        // Default renderer emits pretty JSON
        assert!(s.contains("\"custom_field\""));
        assert!(s.contains("42"));
    }

    #[test]
    fn test_render_search_signature_empty_falls_through_to_snippet() {
        // Regression: a populated-but-empty `signature` must not
        // block the fallback chain — a real `snippet` is still
        // printed instead of leaving the result blank.
        let args = v(r#"{"query": "foo", "top_k": 1}"#);
        let payload = v(r#"{
            "count": 1,
            "results": [{
                "file_path": "/p.rs",
                "symbol": "main",
                "symbol_type": "function",
                "score": 0.9,
                "signature": "   ",
                "snippet": "fn main() { return 0; }"
            }]
        }"#);
        let s = render_tool_output("leindex.search", &payload, &args);
        assert!(s.contains("fn main()"), "snippet must print when signature is empty: {}", s);
    }

    #[test]
    fn test_render_context_reads_from_results_anchor() {
        // Regression: `ContextHandler` returns an `AnalysisResult`
        // whose expanded PDG text is in `context` and whose anchor
        // node lives in `results[0]`. The CLI must surface both.
        let args = v(r#"{"node_id": "main"}"#);
        let payload = v(r#"{
            "query": "Context for main",
            "results": [{
                "rank": 1,
                "node_id": "src/main.rs:main",
                "file_path": "src/main.rs",
                "symbol_name": "main",
                "symbol_type": "function",
                "byte_range": [10, 50]
            }],
            "context": "fn main() { return 0; }\nfn helper() {}",
            "tokens_used": 12,
            "processing_time_ms": 1
        }"#);
        let s = render_tool_output("leindex.context", &payload, &args);
        assert!(s.contains("Symbol"), "missing Symbol field: {}", s);
        assert!(s.contains("main"), "missing symbol name: {}", s);
        assert!(s.contains("src/main.rs"), "missing file path: {}", s);
        assert!(s.contains("fn main()"), "missing expanded body: {}", s);
    }

    #[test]
    fn test_render_symbol_lookup_uses_real_field_names() {
        // Regression: `lookup_single_symbol` returns `file` / `type` /
        // `byte_range` / `complexity` / `language` / `impact_radius`
        // / optional `source` — NOT the legacy `file_path` / `line` /
        // `symbol_type` / `signature` aliases. Verify every real
        // field is surfaced and the legacy aliases are not.
        let args = v(r#"{"symbol": "main", "include_source": true}"#);
        let payload = v(r#"{
            "symbol": "main",
            "type": "function",
            "file": "src/main.rs",
            "byte_range": [10, 60],
            "complexity": 3,
            "language": "rust",
            "callers": [{"name": "caller_a", "file": "src/lib.rs", "type": "function"}],
            "callees": [],
            "impact_radius": {"affected_symbols": 5, "affected_files": 2},
            "source": "fn main() { return 0; }"
        }"#);
        let s = render_tool_output("leindex.symbol-lookup", &payload, &args);
        assert!(s.contains("Symbol"));
        assert!(s.contains("main"));
        assert!(s.contains("File"), "missing File field: {}", s);
        assert!(s.contains("src/main.rs"), "missing real file: {}", s);
        assert!(s.contains("Type"), "missing Type field: {}", s);
        assert!(s.contains("function"), "missing real type: {}", s);
        assert!(s.contains("Language"), "missing Language field: {}", s);
        assert!(s.contains("rust"), "missing language: {}", s);
        assert!(s.contains("Range"), "missing Range field: {}", s);
        assert!(s.contains("bytes 10-60"), "missing byte range: {}", s);
        assert!(s.contains("Complexity"), "missing Complexity field: {}", s);
        assert!(s.contains("Impact"), "missing Impact field: {}", s);
        assert!(s.contains("5 symbols / 2 files"), "missing impact counts: {}", s);
        assert!(s.contains("caller_a"), "missing caller: {}", s);
        assert!(s.contains("Callers"));
        // Source preview (first non-empty line).
        assert!(s.contains("fn main()"), "missing source preview: {}", s);
        // Legacy aliases must NOT appear in the rendered output.
        assert!(!s.contains("file_path"), "renderer still emits file_path alias: {}", s);
        assert!(!s.contains("symbol_type"), "renderer still emits symbol_type alias: {}", s);
        assert!(!s.contains("Signature"), "renderer still emits Signature (legacy alias): {}", s);
    }

    #[test]
    fn test_render_write_shows_confirmation_not_diff() {
        // Regression: `WriteHandler` returns
        // `{success, file_path, language, symbols}` and must NOT be
        // routed through `render_diff_value` (which expects
        // `diff` / `diffs` / `diff_text` and returns empty for the
        // write payload).
        let args = v(r#"{"file_path": "src/lib.rs", "content": "// hello"}"#);
        let payload = v(r#"{
            "success": true,
            "file_path": "src/lib.rs",
            "language": "rust",
            "symbols": [
                {"name": "alpha", "type": "fn() -> ()", "range": [0, 9]},
                {"name": "beta",  "type": "fn() -> ()", "range": [10, 19]}
            ]
        }"#);
        let s = render_tool_output("leindex.write", &payload, &args);
        assert!(s.contains("Wrote"), "missing confirmation header: {}", s);
        assert!(s.contains("src/lib.rs"), "missing file path: {}", s);
        assert!(s.contains("Language"), "missing language field: {}", s);
        assert!(s.contains("rust"), "missing language value: {}", s);
        assert!(s.contains("alpha"), "missing symbol name: {}", s);
        assert!(s.contains("beta"), "missing symbol name: {}", s);
        // Diff-style gutters (line numbers) must NOT appear.
        assert!(!s.contains(""), "write must not render diff gutter: {}", s);
    }

    #[test]
    fn test_render_edit_apply_shows_confirmation_not_diff() {
        // Regression: `EditApplyHandler` returns
        // `{success, changes_applied, file_path, edit_region,
        // affected_symbols, affected_files, breaking_changes}` and
        // must NOT be routed through `render_diff_value` (which
        // expects `diff` / `diffs` / `diff_text` and returns empty
        // for the apply payload).
        let args = v(r#"{"file_path": "src/lib.rs"}"#);
        let payload = v(r#"{
            "success": true,
            "changes_applied": 2,
            "file_path": "src/lib.rs",
            "edit_region": "1: // hello\n2: fn alpha() {}",
            "affected_symbols": ["alpha", "beta"],
            "affected_files": ["src/lib.rs"],
            "breaking_changes": []
        }"#);
        let s = render_tool_output("leindex.edit-apply", &payload, &args);
        assert!(s.contains("Applied"), "missing applied header: {}", s);
        assert!(s.contains("src/lib.rs"), "missing file path: {}", s);
        assert!(s.contains("Affected symbols"), "missing affected symbols: {}", s);
        assert!(s.contains("Affected files"), "missing affected files: {}", s);
        // The surrounding region must be shown.
        assert!(s.contains("// hello"), "missing surrounding region: {}", s);
        // Diff-style gutters must NOT appear.
        assert!(!s.contains(""), "edit-apply must not render diff gutter: {}", s);
    }

    #[test]
    fn test_render_edit_apply_noop_shows_message() {
        // No-op path: changes_applied == 0, message describes why.
        let args = v(r#"{"file_path": "src/lib.rs"}"#);
        let payload = v(r#"{
            "success": true,
            "changes_applied": 0,
            "message": "No changes to apply (content identical)"
        }"#);
        let s = render_tool_output("leindex.edit-apply", &payload, &args);
        assert!(s.contains("No-op"), "missing no-op header: {}", s);
        assert!(s.contains("content identical"), "missing no-op message: {}", s);
    }

    #[test]
    fn test_render_edit_apply_renders_object_edit_region() {
        // Regression: `trim_edit` preserves `edit_region` as an
        // object (e.g. `{"start": 10, "end": 25}`) for apply-
        // shaped payloads. The CLI renderer used to look only for
        // the string form, which would silently drop the region
        // context. Now it renders the object form as
        // `Surrounding region: bytes 10..25` so the LLM-visible
        // payload is never truncated by a shape mismatch.
        let args = v(r#"{"file_path": "src/lib.rs"}"#);
        let payload = v(r#"{
            "success": true,
            "changes_applied": 3,
            "file_path": "src/lib.rs",
            "edit_region": {"start": 10, "end": 25},
            "message": "Applied 3 changes"
        }"#);
        let s = render_tool_output("leindex.edit-apply", &payload, &args);
        assert!(
            s.contains("Surrounding region"),
            "missing surrounding region label: {}",
            s
        );
        assert!(
            s.contains("bytes 10..25"),
            "missing structured edit_region range: {}",
            s
        );
    }

    #[test]
    fn test_render_edit_apply_string_region_no_duplicate_text() {
        // Regression: when `edit_region` is a string (the
        // multi-line surrounding excerpt form), the renderer
        // must NOT print the raw multi-line text on the
        // `Surrounding region:` header line — that would
        // duplicate the per-line colorized expansion emitted
        // below the header. The header is a marker; the lines
        // themselves go on the indented block.
        let args = v(r#"{"file_path": "src/lib.rs"}"#);
        let payload = v(r#"{
            "success": true,
            "changes_applied": 1,
            "file_path": "src/lib.rs",
            "edit_region": "1: // hello\n2: fn alpha() {}\n3: fn beta() {}\n",
            "message": "Applied 1 change"
        }"#);
        let s = render_tool_output("leindex.edit-apply", &payload, &args);
        let stripped = strip_ansi(&s);
        // The string body must appear in the per-line
        // expansion (the `      // hello` indented line), so
        // the comment marker IS in the output.
        assert!(
            stripped.contains("// hello"),
            "per-line expansion missing: {}",
            stripped
        );
        assert!(stripped.contains("fn alpha() {}"));
        assert!(stripped.contains("fn beta() {}"));
        // The header line itself must be a single marker
        // line — NOT the raw multi-line text concatenated.
        // We assert that the `Surrounding region:` line, when
        // stripped, does not also include the function
        // bodies (which would be the duplication symptom).
        let header_line = stripped
            .lines()
            .find(|l| l.contains("Surrounding region"))
            .unwrap_or("");
        assert!(
            !header_line.contains("fn alpha()"),
            "string edit_region body leaked into header line: {:?}",
            header_line
        );
        assert!(
            !header_line.contains("fn beta()"),
            "string edit_region body leaked into header line: {:?}",
            header_line
        );
    }

    #[test]
    fn test_render_edit_apply_renders_partial_object_edit_region() {
        // When the trimmer preserves a half-shape object (only
        // `start`, or only `end`, or neither), the renderer must
        // still surface what is present rather than dropping the
        // region entirely.
        let args = v(r#"{"file_path": "src/lib.rs"}"#);
        let payload = v(r#"{
            "success": true,
            "changes_applied": 1,
            "file_path": "src/lib.rs",
            "edit_region": {"start": 7},
            "message": "Applied 1 change"
        }"#);
        let s = render_tool_output("leindex.edit-apply", &payload, &args);
        assert!(
            s.contains("bytes 7.."),
            "missing open-ended start range: {}",
            s
        );
    }

    #[test]
    fn test_render_context_does_not_mislabel_byte_offset_as_line() {
        // Regression: `results[0].byte_range[0]` is a byte offset,
        // not a line number. The CLI must NOT show the byte offset
        // in the `Line` field or as the gutter base. The byte
        // range is still surfaced as a `Range: bytes X-Y` hint.
        let args = v(r#"{"node_id": "main"}"#);
        let payload = v(r#"{
            "query": "Context for main",
            "results": [{
                "rank": 1,
                "node_id": "src/main.rs:main",
                "file_path": "src/main.rs",
                "symbol_name": "main",
                "symbol_type": "function",
                "byte_range": [15342, 15400]
            }],
            "context": "fn main() {\n    return 0;\n}"
        }"#);
        let s = render_tool_output("leindex.context", &payload, &args);
        // The byte offset (15342) must NOT appear as a line value.
        assert!(
            !s.contains("Line: 15342"),
            "renderer mislabelled byte offset as line number: {}",
            s
        );
        // The byte range is still surfaced as a Range hint.
        assert!(s.contains("Range"), "missing range hint: {}", s);
        assert!(s.contains("bytes 15342-15400"), "missing byte range: {}", s);
        // The snippet gutter must start at 1 (relative to the
        // snippet), not at the byte offset. The gutter value
        // (right-padded to 4 chars) is followed by an ANSI reset
        // and the `│` separator; the gutter text itself sits
        // between the leading "  " (two-space indent) and the `│`
        // separator, so we strip ANSI escapes and look for the
        // gutter lines.
        let stripped = strip_ansi(&s);
        // The first gutter line should be "   1│", not "15342│".
        let first_gutter = stripped
            .lines()
            .find(|l| l.contains(''))
            .unwrap_or("");
        assert!(
            first_gutter.contains("   1│"),
            "first gutter line must start at 1, got {:?}",
            first_gutter
        );
        // No gutter line should start with "15342" (the byte
        // offset) — every gutter should be a small relative line
        // number, since the snippet has at most a handful of
        // lines.
        for l in stripped.lines() {
            if l.contains('') {
                assert!(
                    !l.contains("15342│"),
                    "gutter line must not use byte offset: {:?}",
                    l
                );
            }
        }
    }

    #[test]
    fn test_render_context_uses_legacy_line_field() {
        // Regression: legacy flat-shape payloads carry a real
        // `line` field; the renderer must use that and the gutter
        // must start at that line.
        let args = v(r#"{"node_id": "main"}"#);
        let payload = v(r#"{
            "symbol": "main",
            "file_path": "src/main.rs",
            "symbol_type": "function",
            "line": 42,
            "content": "fn main() { return 0; }"
        }"#);
        let s = render_tool_output("leindex.context", &payload, &args);
        let stripped = strip_ansi(&s);
        assert!(stripped.contains("Line: 42"), "missing line field: {}", stripped);
        // The gutter must start at 42 (right-padded to width 4).
        let first_gutter = stripped
            .lines()
            .find(|l| l.contains(''))
            .unwrap_or("");
        assert!(
            first_gutter.contains("  42│"),
            "gutter must start at 42, got {:?}",
            first_gutter
        );
    }

    #[test]
    fn test_render_flat_files_drops_unused_label_string() {
        // Regression: `cx_label.1` ("low"/"med"/"high") was
        // computed but never used. Confirm the colour-coded
        // integer is still rendered and no `low`/`med`/`high`
        // text leaks into the output.
        let files = v(r#"[
            {"path": "src/main.rs", "symbol_count": 3, "total_complexity": 5, "incoming_dependencies": 0, "outgoing_dependencies": 0},
            {"path": "src/lib.rs",  "symbol_count": 12, "total_complexity": 25, "incoming_dependencies": 1, "outgoing_dependencies": 0}
        ]"#);
        let s = render_flat_files(files.as_array().unwrap(), false);
        assert!(s.contains("src/main.rs"));
        assert!(s.contains("src/lib.rs"));
        assert!(s.contains("cx:5"), "missing complexity value: {}", s);
        assert!(s.contains("cx:25"), "missing complexity value: {}", s);
        // The unused label strings must NOT appear in the output.
        assert!(!s.contains("low"), "unused label leaked: {}", s);
        assert!(!s.contains("med"), "unused label leaked: {}", s);
        assert!(!s.contains("high"), "unused label leaked: {}", s);
    }

    /// Regression for P2 #3342365976: `lookup_symbols_batch` returns
    /// a wrapper {batch:true, count, results:[ ... ]}. The previous
    /// renderer emitted the header plus a Count field but no per-entry
    /// output, because `symbol` / `file` / `type` were at the entry
    /// level, not the top level. The fix branches on `batch:true` and
    /// recurses into each result.
    #[test]
    fn test_render_symbol_lookup_batch_renders_each_entry() {
        let args = v(r#"{"symbols": ["main", "lib_init"]}"#);
        let payload = v(r#"{
            "batch": true,
            "count": 2,
            "results": [
                {
                    "symbol": "main",
                    "type": "function",
                    "file": "src/main.rs",
                    "byte_range": [10, 60],
                    "complexity": 3,
                    "language": "rust",
                    "callers": [],
                    "callees": [],
                    "impact_radius": {"affected_symbols": 5, "affected_files": 2}
                },
                {
                    "symbol": "lib_init",
                    "type": "function",
                    "file": "src/lib.rs",
                    "byte_range": [100, 200],
                    "complexity": 7,
                    "language": "rust",
                    "callers": [],
                    "callees": [],
                    "impact_radius": {"affected_symbols": 0, "affected_files": 0}
                }
            ]
        }"#);
        let s = render_tool_output("leindex.symbol-lookup", &payload, &args);
        // Batch header and count.
        assert!(s.contains("Symbol Lookup (batch)"), "missing batch header: {}", s);
        assert!(s.contains("Count"), "missing Count field: {}", s);
        // Each entry must appear with its own Symbol / File / Type.
        assert!(s.contains("main"), "missing first entry symbol: {}", s);
        assert!(s.contains("lib_init"), "missing second entry symbol: {}", s);
        assert!(s.contains("src/main.rs"), "missing first entry file: {}", s);
        assert!(s.contains("src/lib.rs"), "missing second entry file: {}", s);
        // The byte range from each entry must be present.
        assert!(s.contains("bytes 10-60"), "missing first entry range: {}", s);
        assert!(s.contains("bytes 100-200"), "missing second entry range: {}", s);
    }

    /// Empty batch returns the wrapper with a "(no results)" marker
    /// rather than emitting only a header + Count line.
    #[test]
    fn test_render_symbol_lookup_batch_empty() {
        let args = v(r#"{"symbols": []}"#);
        let payload = v(r#"{"batch": true, "count": 0, "results": []}"#);
        let s = render_tool_output("leindex.symbol-lookup", &payload, &args);
        assert!(s.contains("Symbol Lookup (batch)"), "missing batch header: {}", s);
        assert!(s.contains("(no results)"), "missing empty marker: {}", s);
    }
}