greplm-core 0.6.0

Core indexing and search engine for greplm: a trigram code index for LLM agents.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
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
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
//! Query execution: trigram candidate filtering, then exact verification.

use std::collections::{HashMap, HashSet};
use std::path::{Component, Path, PathBuf};
use std::sync::{Arc, Mutex};

use lru::LruCache;
use memchr::memmem;
use rayon::prelude::*;
use regex::bytes::Regex as BytesRegex;
use serde::{Deserialize, Serialize};

use crate::config::Config;
use crate::error::{Error, Result};
use crate::lang::Language;
use crate::meta::Meta;
use crate::paths::Paths;
use crate::segment::{RefKind, Segment};
use crate::trigram::{self, TrigramQuery};

/// A content search request.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct SearchQuery {
    pub pattern: String,
    pub regex: bool,
    pub case_insensitive: bool,
    /// Match only whole identifiers (word boundaries on both sides).
    pub whole_word: bool,
    pub lang: Option<String>,
    pub path: Option<String>,
    pub limit: usize,
    /// Skip the first N ranked results (for pagination).
    pub offset: usize,
    pub max_per_file: usize,
    /// Return EVERY match in deterministic (path, line) order: no ranking, no
    /// global `limit`, and no per-file caps (`max_per_file` and the internal
    /// pathological-input cap are both lifted). This is grep-equivalent
    /// completeness; use it when "find every occurrence" matters more than
    /// relevance ranking. `offset`/`limit` are ignored when set.
    pub exhaustive: bool,
}

impl Default for SearchQuery {
    fn default() -> Self {
        Self {
            pattern: String::new(),
            regex: false,
            case_insensitive: false,
            whole_word: false,
            lang: None,
            path: None,
            limit: 50,
            offset: 0,
            max_per_file: 20,
            exhaustive: false,
        }
    }
}

/// A single content match.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchHit {
    pub path: String,
    pub lang: String,
    pub line: u32,
    pub column: u32,
    pub text: String,
    pub score: f32,
}

/// A symbol lookup request.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct SymbolQuery {
    pub name: String,
    pub kind: Option<String>,
    pub exact: bool,
    pub limit: usize,
    pub offset: usize,
}

impl Default for SymbolQuery {
    fn default() -> Self {
        Self {
            name: String::new(),
            kind: None,
            exact: false,
            limit: 50,
            offset: 0,
        }
    }
}

/// A single symbol match.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SymbolHit {
    pub path: String,
    pub lang: String,
    pub name: String,
    pub kind: String,
    pub line_start: u32,
    pub line_end: u32,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub container: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub signature: Option<String>,
    pub score: f32,
}

/// A resolved reference to an identifier: a definition, a call site, or an
/// import. Unlike text search, these come from the structural reference index.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RefHit {
    pub path: String,
    pub lang: String,
    pub name: String,
    /// "definition", "call", or "import".
    pub kind: String,
    pub line: u32,
    pub column: u32,
    /// The enclosing symbol at this location, when known.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub container: Option<String>,
}

/// One edge of the call graph: a call site linking a caller symbol to a callee.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CallSite {
    /// The enclosing symbol the call is made from (None at file scope).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub caller: Option<String>,
    /// The called identifier.
    pub callee: String,
    pub path: String,
    pub lang: String,
    pub line: u32,
    pub column: u32,
}

/// A symbol affected by a change to a target symbol, with its BFS distance from
/// the target along the reverse call graph.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ImpactNode {
    pub name: String,
    pub kind: String,
    pub path: String,
    pub lang: String,
    pub line_start: u32,
    pub line_end: u32,
    /// Hops along the caller chain from the target (0 = the target itself).
    pub distance: u32,
}

/// A candidate definition for an identifier at a source position, ranked by
/// resolution confidence. `resolved` marks a single high-confidence target.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DefHit {
    pub path: String,
    pub lang: String,
    pub name: String,
    pub kind: String,
    pub line_start: u32,
    pub line_end: u32,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub container: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub signature: Option<String>,
    pub score: f32,
    /// True when this is the unambiguous resolution target.
    pub resolved: bool,
}

/// The git history of a resolved symbol.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SymbolHistory {
    pub name: String,
    pub path: String,
    pub line_start: u32,
    pub line_end: u32,
    pub commits: Vec<crate::git::Commit>,
}

/// A changed file annotated with the symbols it defines.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChangedSymbols {
    pub path: String,
    pub status: String,
    pub symbols: Vec<String>,
}

/// A structural (AST) search match, with its captured meta-variables.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StructHit {
    pub path: String,
    pub lang: String,
    pub line_start: u32,
    pub line_end: u32,
    /// Kind of the matched node.
    pub kind: String,
    /// First line of the match, for display.
    pub text: String,
    pub captures: Vec<crate::structural::StructCapture>,
}

enum Matcher {
    Literal(Vec<u8>),
    Regex(BytesRegex),
}

impl Matcher {
    fn build(query: &SearchQuery) -> Result<Matcher> {
        if query.regex {
            let re = regex::bytes::RegexBuilder::new(&query.pattern)
                .case_insensitive(query.case_insensitive)
                .build()?;
            Ok(Matcher::Regex(re))
        } else if query.case_insensitive {
            let re = regex::bytes::RegexBuilder::new(&regex::escape(&query.pattern))
                .case_insensitive(true)
                .build()?;
            Ok(Matcher::Regex(re))
        } else {
            Ok(Matcher::Literal(query.pattern.as_bytes().to_vec()))
        }
    }

    /// Collect the byte offsets of matches in `hay`, up to `cap`. Scanning the
    /// whole buffer (rather than line-by-line) lets regex patterns span newlines.
    /// When `whole_word` is set, only matches bounded by non-identifier bytes
    /// count.
    fn match_starts(&self, hay: &[u8], whole_word: bool, cap: usize) -> Vec<(usize, usize)> {
        let mut out = Vec::new();
        match self {
            Matcher::Literal(needle) => {
                if needle.is_empty() {
                    return out;
                }
                for pos in memmem::find_iter(hay, needle) {
                    let end = pos + needle.len();
                    if !whole_word || boundary_ok(hay, pos, end) {
                        out.push((pos, end));
                        if out.len() >= cap {
                            break;
                        }
                    }
                }
            }
            Matcher::Regex(re) => {
                for m in re.find_iter(hay) {
                    // Skip zero-width matches (e.g. `a*`, `^`): they carry no
                    // displayable span and would flag every line.
                    if m.start() == m.end() {
                        continue;
                    }
                    if !whole_word || boundary_ok(hay, m.start(), m.end()) {
                        out.push((m.start(), m.end()));
                        if out.len() >= cap {
                            break;
                        }
                    }
                }
            }
        }
        out
    }
}

/// Fuzz-only entry: build a literal/regex matcher and scan `hay` for matches.
#[doc(hidden)]
pub fn fuzz_match_starts(
    pattern: &str,
    hay: &[u8],
    regex: bool,
    case_insensitive: bool,
    whole_word: bool,
) {
    let query = SearchQuery {
        pattern: pattern.to_string(),
        regex,
        case_insensitive,
        whole_word,
        ..Default::default()
    };
    if let Ok(m) = Matcher::build(&query) {
        let _ = m.match_starts(hay, whole_word, PER_FILE_MATCH_CAP);
    }
}

/// Identifier byte for word-boundary checks. Bytes >= 0x80 are treated as
/// identifier bytes so multibyte UTF-8 (Unicode) identifiers are respected.
fn is_ident_byte(b: u8) -> bool {
    b == b'_' || b.is_ascii_alphanumeric() || b >= 0x80
}

/// True if the byte range `[start, end)` is bounded by non-identifier bytes.
fn boundary_ok(line: &[u8], start: usize, end: usize) -> bool {
    let left = start == 0 || !is_ident_byte(line[start - 1]);
    let right = end >= line.len() || !is_ident_byte(line[end]);
    left && right
}

/// Memory budget (in bytes) for the verification content cache. Eviction is
/// driven by total cached bytes rather than a file count, so a query that
/// touches many large files can't balloon resident memory. The cache is
/// content-addressed by hash, so stale entries fall out when files change and
/// are re-indexed. ~256 MiB.
const CONTENT_CACHE_BYTES: u64 = 256 * 1024 * 1024;

/// Hard cap on matches collected per file before ranking, to bound work on
/// pathological inputs (e.g. a minified file where every line matches).
const PER_FILE_MATCH_CAP: usize = 4096;

struct CacheInner {
    map: LruCache<u64, Arc<[u8]>>,
    bytes: u64,
}

/// A thread-safe, content-addressed, byte-budgeted cache of recently read
/// files. Entries are evicted least-recently-used until total cached bytes fit
/// within the budget.
struct ContentCache {
    inner: Mutex<CacheInner>,
    budget: u64,
}

impl ContentCache {
    fn new(budget_bytes: u64) -> Self {
        Self {
            inner: Mutex::new(CacheInner {
                map: LruCache::unbounded(),
                bytes: 0,
            }),
            budget: budget_bytes.max(1),
        }
    }

    /// Return the bytes for `path`, reusing a cached copy keyed by `hash`. The
    /// file is read outside the lock so concurrent verifiers don't serialize.
    fn get_or_read(&self, hash: u64, path: &Path) -> Option<Arc<[u8]>> {
        if let Ok(mut guard) = self.inner.lock() {
            if let Some(v) = guard.map.get(&hash) {
                return Some(v.clone());
            }
        }
        let data = std::fs::read(path).ok()?;
        let arc: Arc<[u8]> = Arc::from(data.into_boxed_slice());
        let len = arc.len() as u64;
        if let Ok(mut guard) = self.inner.lock() {
            // A single file larger than the whole budget is returned but not
            // cached; storing it would just evict everything else and itself.
            if len <= self.budget {
                if let Some(prev) = guard.map.put(hash, arc.clone()) {
                    guard.bytes = guard.bytes.saturating_sub(prev.len() as u64);
                }
                guard.bytes += len;
                while guard.bytes > self.budget {
                    match guard.map.pop_lru() {
                        Some((_, evicted)) => {
                            guard.bytes = guard.bytes.saturating_sub(evicted.len() as u64);
                        }
                        None => break,
                    }
                }
            }
        }
        Some(arc)
    }
}

/// Loaded, searchable index.
pub struct Searcher {
    paths: Paths,
    segments: Vec<Segment>,
    /// Live document lookup by relative path -> (segment index, doc id).
    /// Makes path-keyed queries (outline, imports, changed-since) O(1)
    /// instead of a scan over every doc table.
    ///
    /// Built on first use rather than at open: it owns a copy of every live
    /// document's path, so constructing it costs an allocation per file, and
    /// only three query paths need it. Content search, symbol lookup and
    /// context packs never touch it.
    by_path: std::sync::OnceLock<HashMap<String, (usize, u32)>>,
    /// Shared so a reloaded searcher (daemon hot-swap) keeps its warm,
    /// content-addressed file cache.
    content: Arc<ContentCache>,
}

impl Searcher {
    /// Open the index described by `meta`.
    pub fn open(paths: &Paths) -> Result<Searcher> {
        Self::open_inner(paths, None)
    }

    /// Open the index, reusing as much of `prev` as possible: segments whose
    /// id is unchanged share their parsed tables and lookup maps (only the
    /// live bitmap is re-read), and the verification content cache carries
    /// over warm. Sound because segment ids are never reused, so an id always
    /// names the same immutable content. This is what makes the daemon's
    /// per-save searcher hot-swap cheap on large repositories.
    pub fn open_reusing(paths: &Paths, prev: &Searcher) -> Result<Searcher> {
        Self::open_inner(paths, Some(prev))
    }

    fn open_inner(paths: &Paths, prev: Option<&Searcher>) -> Result<Searcher> {
        if !paths.exists() {
            return Err(Error::IndexMissing(paths.base.clone()));
        }
        let meta = Meta::load(&paths.meta_file())?;
        // Open segments concurrently: each is an independent set of files whose
        // integrity checks dominate the work. `collect` into `Result<Vec<_>>`
        // keeps manifest order, which the doc-id bookkeeping depends on.
        let mut segments: Vec<Segment> = meta
            .segments
            .par_iter()
            .map(|&seg_id| {
                match prev.and_then(|p| p.segments.iter().find(|s| s.id == seg_id)) {
                    // An id always names the same immutable content, so a
                    // reusable segment only re-reads its live bitmap.
                    Some(seg) => seg.reopen(paths),
                    None => Segment::open(paths, seg_id),
                }
            })
            .collect::<Result<_>>()?;
        // Honor deletes that are published in the manifest but not yet applied
        // to the on-disk live bitmaps (the atomic-tombstone window).
        for pt in &meta.pending_tombstones {
            if let Some(seg) = segments.iter_mut().find(|s| s.id == pt.segment_id) {
                seg.subtract_live(&pt.doc_ids);
            }
        }
        let content = match prev {
            Some(p) => p.content.clone(),
            None => Arc::new(ContentCache::new(CONTENT_CACHE_BYTES)),
        };
        Ok(Searcher {
            paths: paths.clone(),
            segments,
            by_path: std::sync::OnceLock::new(),
            content,
        })
    }

    /// The live path -> (segment, doc) lookup, built on first use.
    fn by_path(&self) -> &HashMap<String, (usize, u32)> {
        self.by_path
            .get_or_init(|| build_path_index(&self.segments))
    }

    /// Run a content search.
    pub fn search(&self, query: &SearchQuery) -> Result<Vec<SearchHit>> {
        if query.pattern.is_empty() {
            return Ok(Vec::new());
        }
        let matcher = Matcher::build(query)?;
        let tq: TrigramQuery = if query.regex {
            trigram::regex_trigrams(&query.pattern, query.case_insensitive)
        } else if query.case_insensitive {
            // Fold ASCII case into per-position trigram clauses so we still prune
            // candidates instead of scanning the whole repository.
            TrigramQuery::from_literal_ci(query.pattern.as_bytes())
        } else {
            TrigramQuery::from_literal(query.pattern.as_bytes())
        };

        let path_filter = query.path.as_deref();
        let lang_filter = query.lang.as_deref();

        // Gather candidate (segment, doc) pairs after cheap metadata filters.
        let mut targets: Vec<(usize, u32, f32)> = Vec::new();
        for (si, seg) in self.segments.iter().enumerate() {
            let candidates = seg.candidates(&tq)?;
            for doc_id in candidates.iter() {
                if !seg.is_live(doc_id) {
                    continue;
                }
                let doc = match seg.doc(doc_id) {
                    Some(d) => d,
                    None => continue,
                };
                if let Some(lf) = lang_filter {
                    if doc.lang != lf {
                        continue;
                    }
                }
                if let Some(pf) = path_filter {
                    if !doc.path.contains(pf) {
                        continue;
                    }
                }
                targets.push((si, doc_id, 0.0));
            }
        }

        // Verify candidates in parallel: each reads its file (cache/page-cache
        // backed) and scans the buffer with the real matcher.
        let root = &self.paths.root;
        let segments = &self.segments;
        let content: &ContentCache = &self.content;
        let max_per_file = query.max_per_file;
        let whole_word = query.whole_word;
        let exhaustive = query.exhaustive;
        let verify = |&(si, doc_id, _): &(usize, u32, f32)| {
            verify_doc(
                &segments[si],
                doc_id,
                root,
                content,
                &matcher,
                max_per_file,
                whole_word,
                exhaustive,
            )
            .into_iter()
        };

        if query.exhaustive {
            // Grep-equivalent: every match, deterministic (path, line, column)
            // order, no ranking and no offset/limit truncation.
            let mut hits: Vec<SearchHit> = targets.par_iter().flat_map_iter(verify).collect();
            hits.sort_by(|a, b| {
                a.path
                    .cmp(&b.path)
                    .then_with(|| a.line.cmp(&b.line))
                    .then_with(|| a.column.cmp(&b.column))
            });
            return Ok(hits);
        }

        let need = query.offset.saturating_add(query.limit);
        if need == 0 {
            return Ok(Vec::new());
        }

        // Ranked mode: verify in descending max-possible-score order and stop
        // once `need` collected hits *strictly* outrank everything still
        // unverified — no unverified doc can then reach the returned page,
        // including via tie-breaks. A hit's score is its base path score plus
        // at most 4.0 (the 1.0 match constant + the 3.0 symbol-line bonus).
        // When one batch covers every candidate, early termination can never
        // fire, so skip the per-candidate scoring and sort entirely.
        let chunk = need.saturating_mul(4).clamp(256, 4096);
        let mut hits: Vec<SearchHit>;
        if targets.len() <= chunk {
            hits = targets.par_iter().flat_map_iter(verify).collect();
        } else {
            for t in &mut targets {
                t.2 = self.segments[t.0].doc_path_score(t.1);
            }
            targets.sort_unstable_by(|a, b| {
                b.2.partial_cmp(&a.2).unwrap_or(std::cmp::Ordering::Equal)
            });
            hits = Vec::new();
            let mut start = 0usize;
            while start < targets.len() {
                let end = (start + chunk).min(targets.len());
                let mut batch: Vec<SearchHit> = targets[start..end]
                    .par_iter()
                    .flat_map_iter(verify)
                    .collect();
                hits.append(&mut batch);
                start = end;
                if start < targets.len() {
                    let remaining_max = targets[start].2 + 4.0;
                    let outranking = hits.iter().filter(|h| h.score > remaining_max).count();
                    if outranking >= need {
                        break;
                    }
                }
            }
        }

        let cmp = |a: &SearchHit, b: &SearchHit| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
                .then_with(|| a.path.cmp(&b.path))
                .then_with(|| a.line.cmp(&b.line))
        };
        Ok(rank_paginate(hits, cmp, query.offset, query.limit))
    }

    /// Look up symbols by name. Exact queries go through the per-segment name
    /// index (O(results)); fuzzy queries scan, since prefix/substring/
    /// subsequence matching has no exact key.
    pub fn symbols(&self, query: &SymbolQuery) -> Result<Vec<SymbolHit>> {
        let needle = query.name.to_ascii_lowercase();
        let mut hits: Vec<SymbolHit> = Vec::new();
        // Score a row that already passed the name match: decode it (rows are
        // decoded only for matches), then apply the liveness/kind filters.
        let mut consider = |seg: &Segment, i: u32, score: f32| {
            let sym = match seg.sym(i) {
                Some(s) => s,
                None => return,
            };
            if !seg.is_live(sym.doc_id) {
                return;
            }
            if let Some(k) = &query.kind {
                if &sym.kind != k {
                    return;
                }
            }
            let doc = match seg.doc(sym.doc_id) {
                Some(d) => d,
                None => return,
            };
            hits.push(SymbolHit {
                path: doc.path.clone(),
                lang: doc.lang.clone(),
                name: sym.name,
                kind: sym.kind,
                line_start: sym.line_start,
                line_end: sym.line_end,
                container: sym.container,
                signature: sym.signature,
                score: score + seg.doc_path_score(sym.doc_id),
            });
        };
        for seg in &self.segments {
            if query.exact {
                let rows: Vec<u32> = seg.syms_by_lower(&needle).collect();
                for i in rows {
                    let score =
                        match match_symbol(seg.sym_name(i), seg.sym_name_lower(i), &needle, true) {
                            Some(s) => s,
                            None => continue,
                        };
                    consider(seg, i, score);
                }
            } else {
                // Fuzzy scan: prefix/substring/subsequence matching has no exact
                // key, so every symbol's name has to be looked at. Walk the
                // packed name columns in parallel — the per-row test only reads
                // the mmap, and rayon's `collect` preserves sequence order, so
                // equal-scoring rows keep the deterministic order the ranking
                // tie-breaks rely on. Only matching rows are ever decoded.
                let matches: Vec<(u32, f32)> = (0..seg.sym_count() as u32)
                    .into_par_iter()
                    .filter_map(|i| {
                        match_symbol(seg.sym_name(i), seg.sym_name_lower(i), &needle, false)
                            .map(|s| (i, s))
                    })
                    .collect();
                for (i, score) in matches {
                    consider(seg, i, score);
                }
            }
        }
        let cmp = |a: &SymbolHit, b: &SymbolHit| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
                .then_with(|| a.name.len().cmp(&b.name.len()))
                .then_with(|| a.path.cmp(&b.path))
        };
        Ok(rank_paginate(hits, cmp, query.offset, query.limit))
    }

    /// Return the symbol outline for a single file (by relative path).
    pub fn outline(&self, rel_path: &str) -> Result<Vec<SymbolHit>> {
        let mut out = Vec::new();
        if let Some(&(si, doc_id)) = self.by_path().get(rel_path) {
            let seg = &self.segments[si];
            if let Some(doc) = seg.doc(doc_id) {
                for sym in seg.doc_syms(doc_id) {
                    out.push(SymbolHit {
                        path: doc.path.clone(),
                        lang: doc.lang.clone(),
                        name: sym.name.clone(),
                        kind: sym.kind.clone(),
                        line_start: sym.line_start,
                        line_end: sym.line_end,
                        container: sym.container.clone(),
                        signature: sym.signature.clone(),
                        score: 1.0,
                    });
                }
            }
        }
        out.sort_by_key(|s| s.line_start);
        Ok(out)
    }

    /// Find references to an identifier (whole-word occurrences across the repo).
    pub fn references(&self, name: &str, limit: usize, offset: usize) -> Result<Vec<SearchHit>> {
        self.search(&SearchQuery {
            pattern: name.to_string(),
            whole_word: true,
            limit,
            offset,
            ..Default::default()
        })
    }

    /// All live symbol definitions whose name matches `name` exactly
    /// (case-sensitive), as `(segment index, symbol index, symbol)` tuples.
    /// O(results) via the per-segment name index — this is the inner loop of
    /// `blast_radius` and `context_pack`, so it must not scan.
    fn defs_by_name(&self, name: &str) -> Vec<(usize, usize, crate::segment::SymbolEntry)> {
        let lower = name.to_ascii_lowercase();
        let mut out = Vec::new();
        for (si, seg) in self.segments.iter().enumerate() {
            for idx in seg.syms_by_lower(&lower) {
                // Cheap exact-case check on the name column before decoding.
                if seg.sym_name(idx) != name {
                    continue;
                }
                if let Some(sym) = seg.sym(idx) {
                    if seg.is_live(sym.doc_id) {
                        out.push((si, idx as usize, sym));
                    }
                }
            }
        }
        out
    }

    /// Number of live call sites targeting `name` (call-graph in-degree),
    /// computed via the per-segment callee-name index (no full ref scan).
    ///
    /// Counts through borrowed row views: the callee name is already known, so
    /// decoding an owned `RefEntry` per site would allocate a `String` only to
    /// drop it. `context_pack` calls this once per surviving candidate.
    fn call_indegree(&self, name: &str) -> u32 {
        let mut n = 0u32;
        for seg in &self.segments {
            for r in seg.ref_views_named(name) {
                if r.kind == RefKind::Call && seg.is_live(r.doc_id) {
                    n += 1;
                }
            }
        }
        n
    }

    /// Resolved references to `name`: its definitions, call sites, and imports,
    /// drawn from the structural reference index (not text matching). Ranked
    /// definitions first, then calls, then imports.
    pub fn references_resolved(&self, name: &str, limit: usize, offset: usize) -> Vec<RefHit> {
        let lower = name.to_ascii_lowercase();
        let mut hits: Vec<RefHit> = Vec::new();
        for seg in &self.segments {
            // Definitions and references are both looked up through the
            // per-segment name indexes (O(results), no table scans).
            let def_rows: Vec<u32> = seg.syms_by_lower(&lower).collect();
            for i in def_rows {
                if seg.sym_name(i) != name {
                    continue;
                }
                let sym = match seg.sym(i) {
                    Some(s) => s,
                    None => continue,
                };
                if seg.is_live(sym.doc_id) {
                    if let Some(doc) = seg.doc(sym.doc_id) {
                        hits.push(RefHit {
                            path: doc.path.clone(),
                            lang: doc.lang.clone(),
                            name: sym.name,
                            kind: "definition".to_string(),
                            line: sym.line_start,
                            column: 1,
                            container: sym.container,
                        });
                    }
                }
            }
            // As in `callers`: references to one name cluster into a few files,
            // so resolve each document's symbol spans once.
            let mut ranges: HashMap<u32, DocSymbolRanges> = HashMap::new();
            let mut container_names: HashMap<u32, Option<String>> = HashMap::new();
            for r in seg.ref_views_named(name) {
                if seg.is_live(r.doc_id) {
                    if let Some(doc) = seg.doc(r.doc_id) {
                        let row = ranges
                            .entry(r.doc_id)
                            .or_insert_with(|| DocSymbolRanges::load(seg, r.doc_id))
                            .enclosing_row(r.line);
                        let container = match row {
                            Some(i) => container_names
                                .entry(i)
                                .or_insert_with(|| seg.sym(i).map(|s| s.name))
                                .clone(),
                            None => None,
                        };
                        hits.push(RefHit {
                            path: doc.path.clone(),
                            lang: doc.lang.clone(),
                            name: r.name.to_string(),
                            kind: r.kind.as_str().to_string(),
                            line: r.line,
                            column: r.column,
                            container,
                        });
                    }
                }
            }
        }
        let rank = |k: &str| match k {
            "definition" => 0,
            "call" => 1,
            _ => 2,
        };
        hits.sort_by(|a, b| {
            rank(&a.kind)
                .cmp(&rank(&b.kind))
                .then_with(|| a.path.cmp(&b.path))
                .then_with(|| a.line.cmp(&b.line))
        });
        paginate(hits, offset, limit)
    }

    /// Call sites *inside* `name`'s body: what `name` calls. Built by locating
    /// the definition(s) of `name` and collecting "call" refs within range.
    pub fn callees(&self, name: &str, limit: usize, offset: usize) -> Vec<CallSite> {
        let mut out: Vec<CallSite> = Vec::new();
        let mut seen: HashSet<(String, String, u32, u32)> = HashSet::new();
        for (si, _, sym) in self.defs_by_name(name) {
            let seg = &self.segments[si];
            let doc = match seg.doc(sym.doc_id) {
                Some(d) => d,
                None => continue,
            };
            for r in seg.doc_ref_views(sym.doc_id) {
                if r.kind == RefKind::Call && r.line >= sym.line_start && r.line <= sym.line_end {
                    let key = (doc.path.clone(), r.name.to_string(), r.line, r.column);
                    if !seen.insert(key) {
                        continue;
                    }
                    out.push(CallSite {
                        caller: Some(name.to_string()),
                        callee: r.name.to_string(),
                        path: doc.path.clone(),
                        lang: doc.lang.clone(),
                        line: r.line,
                        column: r.column,
                    });
                }
            }
        }
        out.sort_by(|a, b| {
            a.callee
                .cmp(&b.callee)
                .then_with(|| a.path.cmp(&b.path))
                .then_with(|| a.line.cmp(&b.line))
        });
        paginate(out, offset, limit)
    }

    /// Call sites that target `name`: who calls it. Each is attributed to its
    /// enclosing caller symbol when one can be determined.
    pub fn callers(&self, name: &str, limit: usize, offset: usize) -> Vec<CallSite> {
        let mut out: Vec<CallSite> = Vec::new();
        for seg in &self.segments {
            // A hot callee's call sites cluster into a few files, so cache each
            // document's symbol spans and each resolved caller name rather than
            // re-deriving them per site.
            let mut ranges: HashMap<u32, DocSymbolRanges> = HashMap::new();
            let mut caller_names: HashMap<u32, Option<String>> = HashMap::new();
            // O(results) via the prebuilt callee-name index instead of a full
            // scan of every ref — this is the inner loop of `blast_radius`.
            for r in seg.ref_views_named(name) {
                if r.kind != RefKind::Call || !seg.is_live(r.doc_id) {
                    continue;
                }
                let doc = match seg.doc(r.doc_id) {
                    Some(d) => d,
                    None => continue,
                };
                let row = ranges
                    .entry(r.doc_id)
                    .or_insert_with(|| DocSymbolRanges::load(seg, r.doc_id))
                    .enclosing_row(r.line);
                let caller = match row {
                    Some(i) => caller_names
                        .entry(i)
                        .or_insert_with(|| seg.sym(i).map(|s| s.name))
                        .clone(),
                    None => None,
                };
                out.push(CallSite {
                    caller,
                    callee: name.to_string(),
                    path: doc.path.clone(),
                    lang: doc.lang.clone(),
                    line: r.line,
                    column: r.column,
                });
            }
        }
        out.sort_by(|a, b| a.path.cmp(&b.path).then_with(|| a.line.cmp(&b.line)));
        paginate(out, offset, limit)
    }

    /// Blast radius: the symbols transitively affected if `name` changes, found
    /// by walking the reverse call graph (callers, then their callers, ...) up
    /// to `depth` hops. Distance 0 is `name`'s own definition(s).
    ///
    /// Resolution is by name, so results are an approximation that can include
    /// unrelated same-named symbols; it is a guide, not a proof.
    pub fn blast_radius(&self, name: &str, depth: u32, limit: usize) -> Vec<ImpactNode> {
        let mut out: Vec<ImpactNode> = Vec::new();
        let mut visited: HashSet<String> = HashSet::new();
        visited.insert(name.to_string());

        // Distance 0: the target's own definitions.
        for (si, _, sym) in self.defs_by_name(name) {
            if let Some(doc) = self.segments[si].doc(sym.doc_id) {
                out.push(ImpactNode {
                    name: sym.name.clone(),
                    kind: sym.kind.clone(),
                    path: doc.path.clone(),
                    lang: doc.lang.clone(),
                    line_start: sym.line_start,
                    line_end: sym.line_end,
                    distance: 0,
                });
            }
        }

        let mut frontier: Vec<String> = vec![name.to_string()];
        'expand: for dist in 1..=depth {
            let mut next: Vec<String> = Vec::new();
            for target in &frontier {
                for site in self.callers(target, usize::MAX, 0) {
                    let caller = match site.caller {
                        Some(c) => c,
                        None => continue,
                    };
                    if !visited.insert(caller.clone()) {
                        continue;
                    }
                    for (si, _, sym) in self.defs_by_name(&caller) {
                        if let Some(doc) = self.segments[si].doc(sym.doc_id) {
                            out.push(ImpactNode {
                                name: sym.name.clone(),
                                kind: sym.kind.clone(),
                                path: doc.path.clone(),
                                lang: doc.lang.clone(),
                                line_start: sym.line_start,
                                line_end: sym.line_end,
                                distance: dist,
                            });
                        }
                    }
                    next.push(caller);
                }
                // Stop expanding entirely once the limit is reached; deeper
                // levels could only produce nodes that get truncated anyway.
                if out.len() >= limit {
                    break 'expand;
                }
            }
            if next.is_empty() {
                break;
            }
            frontier = next;
        }
        out.truncate(limit);
        out
    }

    /// Typed go-to-definition: resolve the identifier at `rel_path:line:col` to
    /// its most likely definition(s), combining scope/usage context with the
    /// global symbol table. Returns candidates ranked by confidence; the unique
    /// best is flagged `resolved`. Falls back to whole-word text hits (marked
    /// unresolved) when the name has no indexed definition.
    pub fn definition(&self, rel_path: &str, line: u32, col: u32) -> Result<Vec<DefHit>> {
        let full = self.resolve_within_root(rel_path)?;
        let source = std::fs::read(&full).map_err(|e| Error::io(&full, e))?;
        let ext = Path::new(rel_path)
            .extension()
            .and_then(|e| e.to_str())
            .unwrap_or("");
        let lang = crate::lang::Language::from_extension(ext);

        let ident = match crate::resolve::identifier_at(lang, &source, line, col) {
            Some(i) => i,
            None => {
                return Err(Error::other(format!(
                    "no identifier at {rel_path}:{line}:{col}"
                )))
            }
        };

        // Imports referenced by the use-file: a name imported here is likely
        // defined elsewhere, which lets us prefer cross-file definitions.
        let imported_here = self.imported_names(rel_path);

        let mut cands: Vec<DefHit> = Vec::new();
        for (si, _, sym) in self.defs_by_name(&ident.name) {
            let seg = &self.segments[si];
            let doc = match seg.doc(sym.doc_id) {
                Some(d) => d,
                None => continue,
            };
            let mut score = 10.0f32 + seg.doc_path_score(sym.doc_id);
            let same_file = doc.path == rel_path;
            if same_file {
                score += 40.0;
            }
            score += 2.0 * shared_prefix_len(rel_path, &doc.path) as f32;
            // Usage-context preference.
            let method_like = matches!(sym.kind.as_str(), "method" | "field" | "property");
            if ident.is_member && method_like {
                score += 25.0;
            } else if !ident.is_member && !method_like {
                score += 8.0;
            }
            if ident.is_call
                && matches!(
                    sym.kind.as_str(),
                    "function" | "method" | "macro" | "constructor"
                )
            {
                score += 6.0;
            }
            if ident.is_type
                && matches!(
                    sym.kind.as_str(),
                    "struct" | "class" | "interface" | "enum" | "type" | "trait" | "record"
                )
            {
                score += 12.0;
            }
            // If the name is imported into the use-file, a cross-file definition
            // is the likely target.
            if imported_here.contains(&ident.name) && !same_file {
                score += 15.0;
            }
            cands.push(DefHit {
                path: doc.path.clone(),
                lang: doc.lang.clone(),
                name: sym.name.clone(),
                kind: sym.kind.clone(),
                line_start: sym.line_start,
                line_end: sym.line_end,
                container: sym.container.clone(),
                signature: sym.signature.clone(),
                score,
                resolved: false,
            });
        }

        if cands.is_empty() {
            // Fallback: whole-word text occurrences, marked unresolved.
            let hits = self.references(&ident.name, 50, 0)?;
            return Ok(hits
                .into_iter()
                .map(|h| DefHit {
                    path: h.path,
                    lang: h.lang,
                    name: ident.name.clone(),
                    kind: "text".to_string(),
                    line_start: h.line,
                    line_end: h.line,
                    container: None,
                    signature: Some(h.text),
                    score: h.score,
                    resolved: false,
                })
                .collect());
        }

        cands.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
                .then_with(|| a.path.cmp(&b.path))
                .then_with(|| a.line_start.cmp(&b.line_start))
        });
        // Mark the unique best as resolved when it clears the runner-up.
        let unique_top =
            cands.len() == 1 || (cands.len() >= 2 && cands[0].score - cands[1].score >= 12.0);
        if unique_top {
            cands[0].resolved = true;
        }
        Ok(cands)
    }

    /// Resolved references for the identifier at `rel_path:line:col`: its
    /// definitions, call sites, and imports across the repo.
    pub fn references_of(&self, rel_path: &str, line: u32, col: u32) -> Result<Vec<RefHit>> {
        let full = self.resolve_within_root(rel_path)?;
        let source = std::fs::read(&full).map_err(|e| Error::io(&full, e))?;
        let ext = Path::new(rel_path)
            .extension()
            .and_then(|e| e.to_str())
            .unwrap_or("");
        let lang = crate::lang::Language::from_extension(ext);
        let ident = crate::resolve::identifier_at(lang, &source, line, col)
            .ok_or_else(|| Error::other(format!("no identifier at {rel_path}:{line}:{col}")))?;
        Ok(self.references_resolved(&ident.name, usize::MAX, 0))
    }

    /// The set of names imported into `rel_path` (from the reference index).
    fn imported_names(&self, rel_path: &str) -> HashSet<String> {
        let mut out = HashSet::new();
        if let Some(&(si, doc_id)) = self.by_path().get(rel_path) {
            for r in self.segments[si].doc_ref_views(doc_id) {
                if r.kind == RefKind::Import {
                    out.insert(r.name.to_string());
                }
            }
        }
        out
    }

    /// Resolve a caller-supplied path against the project root, rejecting
    /// anything that would escape it: absolute paths (which would make
    /// `root.join(..)` discard the root entirely), `..` traversal, and symlinks
    /// that resolve outside the tree. Returns the absolute path to read.
    fn resolve_within_root(&self, rel_path: &str) -> Result<PathBuf> {
        let candidate = Path::new(rel_path);
        if candidate.is_absolute() {
            return Err(Error::other(format!(
                "path {rel_path:?} must be relative to the project root"
            )));
        }
        // Reject parent/prefix components before touching the filesystem.
        if candidate
            .components()
            .any(|c| matches!(c, Component::ParentDir | Component::Prefix(_)))
        {
            return Err(Error::other(format!(
                "path {rel_path:?} escapes the project root"
            )));
        }
        // Canonicalize both sides so symlinks can't redirect the read outside
        // the root, then require the resolved path to stay under it.
        let root = self
            .paths
            .root
            .canonicalize()
            .map_err(|e| Error::io(&self.paths.root, e))?;
        let full = root.join(candidate);
        let resolved = full.canonicalize().map_err(|e| Error::io(&full, e))?;
        if !resolved.starts_with(&root) {
            return Err(Error::other(format!(
                "path {rel_path:?} escapes the project root"
            )));
        }
        Ok(resolved)
    }

    /// Read a slice of a file with surrounding context lines.
    pub fn read_snippet(
        &self,
        rel_path: &str,
        start_line: u32,
        end_line: u32,
        context: u32,
    ) -> Result<Snippet> {
        let full = self.resolve_within_root(rel_path)?;
        let data = std::fs::read_to_string(&full).map_err(|e| Error::io(&full, e))?;
        let lines: Vec<&str> = data.lines().collect();
        let total = lines.len() as u32;
        let to = end_line.saturating_add(context).min(total.max(1));
        // Clamp the start into the file as well so an out-of-range request never
        // reports a `start_line` past EOF or an inverted (start > end) range.
        let from = start_line
            .saturating_sub(context)
            .max(1)
            .min(total.max(1))
            .min(to);
        let mut body = String::new();
        let mut last = from;
        for ln in from..=to {
            if let Some(text) = lines.get((ln - 1) as usize) {
                if !body.is_empty() {
                    body.push('\n');
                }
                body.push_str(text);
                last = ln;
            }
        }
        Ok(Snippet {
            path: rel_path.to_string(),
            start_line: from,
            end_line: last,
            total_lines: total,
            text: body,
        })
    }

    /// Build a token-budgeted context pack for `task`: the symbols (with
    /// signatures and code snippets) most relevant to the task, ranked by
    /// lexical relevance and call-graph centrality, plus their immediate
    /// dependency neighborhood. Designed to hand an agent exactly the code it
    /// needs without reading whole files.
    pub fn context_pack(&self, task: &str, budget_tokens: u64) -> crate::context::ContextPack {
        use crate::context::{self, ContextPack, PackItem};

        let terms = context::tokenize(task);

        // A candidate symbol (decoded once) with its location and score.
        struct Cand {
            seg: usize,
            sym: crate::segment::SymbolEntry,
            score: f32,
            reason: String,
        }
        // Scan every live document's symbols, in parallel across documents.
        //
        // This sweep touches every symbol in the repository and keeps only the
        // handful that score above zero, so the per-row work has to be as close
        // to free as possible. Two things make it so:
        //
        //  * rows are *borrowed* from the mmap (`doc_sym_views`) instead of
        //    decoded into owned `SymbolEntry`s — only survivors are
        //    materialized, via `to_entry`;
        //  * the per-document part of the score (the path term bonus and
        //    `path_score`) is computed once per file rather than once per
        //    symbol, and each worker reuses one `ScoreScratch` for the
        //    lowercase/tokenize buffers.
        //
        // A document's path matching a task term lifts *every* symbol in that
        // file above zero, so the name column alone can't prune the scan; the
        // cheap-per-row scoring above is what makes the full sweep affordable.
        // Live (segment, doc) pairs, materialized so the parallel scan runs over
        // an indexed iterator: rayon then preserves input order in the collected
        // output, keeping equal-scoring candidates in a deterministic sequence.
        let mut doc_targets: Vec<(usize, u32)> = Vec::new();
        for (si, seg) in self.segments.iter().enumerate() {
            for doc_id in 0..seg.docs.len() as u32 {
                if seg.is_live(doc_id) {
                    doc_targets.push((si, doc_id));
                }
            }
        }

        let mut cands: Vec<Cand> = doc_targets
            .par_iter()
            .flat_map_iter(|&(si, doc_id)| {
                let seg = &self.segments[si];
                let mut out: Vec<Cand> = Vec::new();
                let doc = match seg.doc(doc_id) {
                    Some(d) => d,
                    None => return out.into_iter(),
                };
                let mut scratch = context::ScoreScratch::default();
                let path_bonus = context::path_term_bonus(&doc.path, &terms, &mut scratch);
                for view in seg.doc_sym_views(doc_id) {
                    let score = context::lexical_score_with(
                        view.name,
                        view.kind,
                        view.signature,
                        view.container,
                        path_bonus,
                        &terms,
                        &mut scratch,
                    );
                    if score <= 0.0 {
                        continue;
                    }
                    out.push(Cand {
                        seg: si,
                        sym: view.to_entry(),
                        score,
                        reason: "match".to_string(),
                    });
                }
                out.into_iter()
            })
            .collect();

        // Call-graph centrality and the path preference, applied only to the
        // symbols that already cleared the lexical filter. Kept in this order
        // (centrality, then path) so the accumulated float score is bit-identical
        // to evaluating it inline, and the ranking below cannot shift.
        //
        // A task whose terms appear in a directory name lifts every symbol in
        // those files above zero, so this can run over thousands of candidates,
        // each doing an independent name-index lookup — worth spreading out.
        cands.par_iter_mut().for_each(|c| {
            let deg = self.call_indegree(&c.sym.name) as f32;
            c.score += (1.0 + deg).ln() * 1.5;
            c.score += self.segments[c.seg].doc_path_score(c.sym.doc_id);
        });

        cands.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Expand the dependency neighborhood of the strongest seeds: include the
        // callees of the top matches so the agent sees what they depend on.
        let mut seen: HashSet<(String, u32)> = HashSet::new();
        for c in &cands {
            seen.insert((c.sym.name.clone(), c.sym.line_start));
        }
        let mut extra: Vec<Cand> = Vec::new();
        for c in cands.iter().take(8) {
            for callee in self.callees(&c.sym.name, 12, 0) {
                for (si2, _, def) in self.defs_by_name(&callee.callee) {
                    let key = (def.name.clone(), def.line_start);
                    if !seen.insert(key) {
                        continue;
                    }
                    extra.push(Cand {
                        seg: si2,
                        sym: def,
                        score: c.score * 0.3,
                        reason: format!("callee of {}", c.sym.name),
                    });
                }
            }
        }
        cands.extend(extra);
        cands.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Greedily pack within budget. Lines are read once per file through the
        // content cache and split once (cached by content hash), so multiple
        // packed symbols from the same file don't re-read or re-split it.
        //
        // Sizing a candidate requires its file, and a candidate that does not
        // fit is skipped rather than ending the loop (a smaller one further down
        // may still fit), so in the worst case every candidate's file is
        // touched: on a task whose terms appear in directory names that measured
        // 1,477 files and 55 MB read to fill an 8,000-token budget, and it was
        // 90% of the whole operation. So walk the candidates in chunks and warm
        // each chunk's files concurrently before packing it. The greedy pass
        // still sees candidates in exactly the same order, and stopping early
        // wastes at most one chunk of prefetching.
        let mut items: Vec<PackItem> = Vec::new();
        let mut used: u64 = 0;
        let mut truncated = false;
        let mut file_lines: HashMap<u64, Arc<FileLines>> = HashMap::new();
        const MAX_ITEM_LINES: u32 = 60;
        /// Candidates whose files are warmed per round. Large enough to keep the
        /// prefetch wide, small enough that an early stop wastes little.
        const PACK_PREFETCH_CHUNK: usize = 256;
        'packing: for chunk in cands.chunks(PACK_PREFETCH_CHUNK) {
            let mut wanted: Vec<(u64, &str)> = Vec::new();
            let mut seen_hash: HashSet<u64> = HashSet::new();
            for c in chunk {
                if let Some(doc) = self.segments[c.seg].doc(c.sym.doc_id) {
                    if !file_lines.contains_key(&doc.hash) && seen_hash.insert(doc.hash) {
                        wanted.push((doc.hash, doc.path.as_str()));
                    }
                }
            }
            let loaded: Vec<(u64, Arc<FileLines>)> = wanted
                .par_iter()
                .map(|&(hash, path)| {
                    let full = self.paths.root.join(path);
                    let data = self.content.get_or_read(hash, &full);
                    (
                        hash,
                        Arc::new(FileLines::new(data.as_deref().unwrap_or(&[]))),
                    )
                })
                .collect();
            file_lines.extend(loaded);

            for c in chunk {
                let seg = &self.segments[c.seg];
                let sym = &c.sym;
                let doc = match seg.doc(sym.doc_id) {
                    Some(d) => d,
                    None => continue,
                };
                let end = sym
                    .line_end
                    .min(sym.line_start.saturating_add(MAX_ITEM_LINES));
                // Warmed by the prefetch above; the fallback keeps this correct if a
                // candidate's document went missing between the two passes.
                let lines = match file_lines.get(&doc.hash) {
                    Some(l) => l.clone(),
                    None => {
                        let full = self.paths.root.join(&doc.path);
                        let data = self.content.get_or_read(doc.hash, &full);
                        let l = Arc::new(FileLines::new(data.as_deref().unwrap_or(&[])));
                        file_lines.insert(doc.hash, l.clone());
                        l
                    }
                };
                let from = sym.line_start.max(1);
                let to = end.min(lines.len() as u32);
                // Size the snippet before building it. Once the budget is nearly
                // full most candidates no longer fit, and this loop keeps scanning
                // them (a later, smaller one may still fit), so assembling a
                // `String` per rejected candidate was pure waste. Summing the line
                // lengths gives exactly `code.len()`: a separator is added only
                // when something has already been written, so `code_len > 0` stands
                // in for `!code.is_empty()` — which matters when a leading line is
                // itself empty.
                let mut code_len = 0usize;
                for ln in from..=to {
                    if let Some(text) = lines.get((ln - 1) as usize) {
                        if code_len > 0 {
                            code_len += 1;
                        }
                        code_len += text.len();
                    }
                }
                let chars: u64 =
                    code_len as u64 + sym.signature.as_ref().map(|s| s.len() as u64).unwrap_or(0);
                let cost = context::est_tokens(chars).max(1);
                if used + cost > budget_tokens && !items.is_empty() {
                    truncated = true;
                    continue;
                }
                let mut code = String::with_capacity(code_len);
                for ln in from..=to {
                    if let Some(text) = lines.get((ln - 1) as usize) {
                        if !code.is_empty() {
                            code.push('\n');
                        }
                        code.push_str(text);
                    }
                }
                debug_assert_eq!(code.len(), code_len, "snippet cost estimate must be exact");
                used += cost;
                items.push(PackItem {
                    path: doc.path.clone(),
                    lang: doc.lang.clone(),
                    name: sym.name.clone(),
                    kind: sym.kind.clone(),
                    line_start: sym.line_start,
                    line_end: sym.line_end,
                    signature: sym.signature.clone(),
                    snippet_start: from,
                    code,
                    reason: c.reason.clone(),
                    score: c.score,
                });
                if used >= budget_tokens {
                    truncated = truncated || items.len() < cands.len();
                    break 'packing;
                }
            }
        }

        ContextPack {
            task: task.to_string(),
            budget_tokens,
            used_tokens: used,
            truncated,
            items,
        }
    }

    /// Blame a single line: the commit and author that last touched it.
    pub fn blame(&self, rel_path: &str, line: u32) -> Result<crate::git::BlameLine> {
        // Validate the path stays within the project root.
        self.resolve_within_root(rel_path)?;
        crate::git::blame(&self.paths.root, rel_path, line)
    }

    /// The commit history of a symbol: resolve `name` to its definition and list
    /// the commits that touched that line range, newest first.
    pub fn symbol_history(&self, name: &str, limit: usize) -> Result<SymbolHistory> {
        // Prefer the highest-ranked (non-test/vendor) definition.
        let defs = self.defs_by_name(name);
        let best = defs
            .iter()
            .max_by(|a, b| {
                let pa = self.segments[a.0].doc_path_score(a.2.doc_id);
                let pb = self.segments[b.0].doc_path_score(b.2.doc_id);
                pa.partial_cmp(&pb).unwrap_or(std::cmp::Ordering::Equal)
            })
            .ok_or_else(|| Error::other(format!("no definition found for {name:?}")))?;
        let (si, _, sym) = best;
        let si = *si;
        let doc = self.segments[si]
            .doc(sym.doc_id)
            .ok_or_else(|| Error::other("definition document missing".to_string()))?;
        let commits = crate::git::line_history(
            &self.paths.root,
            &doc.path,
            sym.line_start,
            sym.line_end,
            limit,
        )
        .or_else(|_| crate::git::file_history(&self.paths.root, &doc.path, limit))?;
        Ok(SymbolHistory {
            name: name.to_string(),
            path: doc.path.clone(),
            line_start: sym.line_start,
            line_end: sym.line_end,
            commits,
        })
    }

    /// Files changed since `rev`, annotated with the symbols defined in each
    /// (from the index) so an agent sees the affected API surface at a glance.
    pub fn changed_since(&self, rev: &str) -> Result<Vec<ChangedSymbols>> {
        let changed = crate::git::changed_since(&self.paths.root, rev)?;
        let mut out = Vec::with_capacity(changed.len());
        for cf in changed {
            let mut symbols = Vec::new();
            if let Some(&(si, doc_id)) = self.by_path().get(&cf.path) {
                for s in self.segments[si].doc_syms(doc_id) {
                    symbols.push(s.name.clone());
                }
            }
            symbols.sort();
            symbols.dedup();
            out.push(ChangedSymbols {
                path: cf.path,
                status: cf.status,
                symbols,
            });
        }
        Ok(out)
    }

    /// Structural (AST) search: match a tree-sitter query or `$NAME`
    /// meta-variable pattern across documents of one language. Literal tokens in
    /// the pattern prune candidates via the trigram index before parsing.
    pub fn structural_search(
        &self,
        pattern: &str,
        lang: &str,
        limit: usize,
        offset: usize,
    ) -> Result<Vec<StructHit>> {
        let language = crate::lang::Language::from_id(lang)
            .ok_or_else(|| Error::other(format!("unknown language id: {lang:?}")))?;
        if language.grammar().is_none() {
            return Err(Error::other(format!(
                "language {lang} is not parseable for structural search"
            )));
        }
        let compiled = crate::structural::compile(language, pattern)?;

        // Prefilter on the most selective literal anchor, if any.
        let anchor = compiled.anchors.iter().max_by_key(|a| a.len()).cloned();
        let tq = anchor
            .as_ref()
            .map(|a| TrigramQuery::from_literal(a.as_bytes()));

        let mut targets: Vec<(usize, u32)> = Vec::new();
        for (si, seg) in self.segments.iter().enumerate() {
            let candidates = match &tq {
                Some(q) => seg.candidates(q)?,
                None => seg.all_live(),
            };
            for doc_id in candidates.iter() {
                if !seg.is_live(doc_id) {
                    continue;
                }
                match seg.doc(doc_id) {
                    Some(d) if d.lang == lang => targets.push((si, doc_id)),
                    _ => {}
                }
            }
        }

        let root = &self.paths.root;
        let segments = &self.segments;
        let content = &self.content;
        let compiled_ref = &compiled;
        let hits: Vec<StructHit> = targets
            .par_iter()
            .flat_map_iter(|&(si, doc_id)| {
                let seg = &segments[si];
                let doc = match seg.doc(doc_id) {
                    Some(d) => d,
                    None => return Vec::new().into_iter(),
                };
                let full = root.join(&doc.path);
                let data = match content.get_or_read(doc.hash, &full) {
                    Some(d) => d,
                    None => return Vec::new().into_iter(),
                };
                let matches = crate::structural::run(language, compiled_ref, &data);
                let line_starts = line_starts(&data);
                let out: Vec<StructHit> = matches
                    .into_iter()
                    .map(|m| {
                        let li = (m.line_start.saturating_sub(1)) as usize;
                        let text = line_starts
                            .get(li)
                            .map(|_| snippet(line_slice(&data, &line_starts, li)))
                            .unwrap_or_default();
                        StructHit {
                            path: doc.path.clone(),
                            lang: doc.lang.clone(),
                            line_start: m.line_start,
                            line_end: m.line_end,
                            kind: m.kind,
                            text,
                            captures: m.captures,
                        }
                    })
                    .collect();
                out.into_iter()
            })
            .collect();

        let cmp = |a: &StructHit, b: &StructHit| {
            a.path
                .cmp(&b.path)
                .then_with(|| a.line_start.cmp(&b.line_start))
        };
        let mut hits = hits;
        hits.sort_by(cmp);
        Ok(paginate(hits, offset, limit))
    }

    /// Summarize the indexed repository.
    pub fn summary(&self) -> RepoSummary {
        use std::collections::HashMap;
        let mut by_lang: HashMap<String, LangStat> = HashMap::new();
        let mut by_dir: HashMap<String, u64> = HashMap::new();
        let mut files = 0u64;
        let mut bytes = 0u64;
        let mut symbols = 0u64;
        for seg in &self.segments {
            for (doc_id, doc) in seg.docs.iter().enumerate() {
                if !seg.is_live(doc_id as u32) {
                    continue;
                }
                files += 1;
                bytes += doc.size;
                let e = by_lang.entry(doc.lang.clone()).or_default();
                e.files += 1;
                e.bytes += doc.size;
                let dir = doc.path.split('/').next().unwrap_or("").to_string();
                *by_dir.entry(dir).or_default() += 1;
                // Symbol counts come from the doc CSR — no row is decoded.
                symbols += u64::from(seg.doc_sym_count(doc_id as u32));
            }
        }
        let mut languages: Vec<LangStat> = by_lang
            .into_iter()
            .map(|(lang, mut s)| {
                s.lang = lang;
                s
            })
            .collect();
        // Break count ties by name. Both lists are collected from a `HashMap`,
        // whose iteration order varies per process, and a stable sort preserved
        // it — so `summary` returned a different ordering on every run, and
        // because `top_dirs` is truncated, a different *set* of directories too.
        languages.sort_by(|a, b| b.files.cmp(&a.files).then_with(|| a.lang.cmp(&b.lang)));
        let mut top_dirs: Vec<(String, u64)> = by_dir.into_iter().collect();
        top_dirs.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.cmp(&b.0)));
        top_dirs.truncate(15);
        RepoSummary {
            files,
            bytes,
            symbols,
            segments: self.segments.len(),
            languages,
            top_dirs: top_dirs
                .into_iter()
                .map(|(name, files)| DirStat { name, files })
                .collect(),
        }
    }
}

/// A file slice with context, returned by [`Searcher::read_snippet`].
///
/// The body is a single `text` blob (lines joined by `\n`) rather than an array
/// of per-line objects: line N is `start_line + i` for the i-th line, so the
/// numbers are implicit and never repeated on the wire. This keeps the payload
/// compact for agents while staying exactly reconstructable.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Snippet {
    pub path: String,
    pub start_line: u32,
    pub end_line: u32,
    pub total_lines: u32,
    pub text: String,
}

/// Repository summary returned by [`Searcher::summary`].
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RepoSummary {
    pub files: u64,
    pub bytes: u64,
    pub symbols: u64,
    pub segments: usize,
    pub languages: Vec<LangStat>,
    pub top_dirs: Vec<DirStat>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct LangStat {
    pub lang: String,
    pub files: u64,
    pub bytes: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DirStat {
    pub name: String,
    pub files: u64,
}

/// Read a single candidate file and collect matching lines. Matches are found
/// over the whole buffer (so regexes may span lines), mapped to line numbers,
/// then ranked so the highest-scored matches survive `max_per_file` truncation.
#[allow(clippy::too_many_arguments)] // hot path; threading a struct adds churn without clarity
fn verify_doc(
    seg: &Segment,
    doc_id: u32,
    root: &Path,
    content: &ContentCache,
    matcher: &Matcher,
    max_per_file: usize,
    whole_word: bool,
    exhaustive: bool,
) -> Vec<SearchHit> {
    let doc = match seg.doc(doc_id) {
        Some(d) => d,
        None => return Vec::new(),
    };
    let full = root.join(&doc.path);
    let data = match content.get_or_read(doc.hash, &full) {
        Some(d) => d,
        None => return Vec::new(),
    };

    // Exhaustive search lifts the pathological-input cap so no match is dropped.
    let cap = if exhaustive {
        usize::MAX
    } else {
        PER_FILE_MATCH_CAP
    };
    let matches = matcher.match_starts(&data, whole_word, cap);
    if matches.is_empty() {
        return Vec::new();
    }

    let line_starts = line_starts(&data);
    let sym_lines = symbol_lines(seg, doc_id);
    let base = seg.doc_path_score(doc_id);

    let mut out = Vec::new();
    let mut last_line = 0u32;
    for (start, _end) in matches {
        let li = line_of(start, &line_starts);
        let line_no = li as u32 + 1;
        // One hit per line; matches are in ascending offset order.
        if line_no == last_line {
            continue;
        }
        last_line = line_no;
        let col = (start - line_starts[li]) as u32 + 1;
        let line_bytes = line_slice(&data, &line_starts, li);
        let mut score = 1.0 + base;
        if sym_lines.binary_search(&line_no).is_ok() {
            score += 3.0;
        }
        out.push(SearchHit {
            path: doc.path.clone(),
            lang: doc.lang.clone(),
            line: line_no,
            column: col,
            text: snippet(line_bytes),
            score,
        });
    }

    // Keep the highest-scored matches when a file has more than the cap.
    // Exhaustive mode keeps every line.
    if !exhaustive && out.len() > max_per_file {
        out.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
                .then_with(|| a.line.cmp(&b.line))
        });
        out.truncate(max_per_file);
    }
    out
}

/// A file's text together with the byte span of each of its lines, so snippets
/// can be sliced out without allocating a `String` per line.
///
/// Context packing needs the lines of every candidate file, and a candidate that
/// does not fit the remaining budget is skipped — but the file still had to be
/// split to find that out. Splitting into `Vec<String>` cost one allocation per
/// line of every file touched (thousands per file on a large source tree); this
/// costs one copy of the file plus one span table.
struct FileLines {
    text: String,
    /// `(start, end)` byte offsets within `text`, excluding line terminators.
    spans: Vec<(usize, usize)>,
}

impl FileLines {
    fn new(data: &[u8]) -> FileLines {
        // Source files are almost always valid UTF-8, and validating is the bulk
        // of the conversion for a multi-MB candidate set. `simdutf8` checks that
        // vectorized; lossy conversion of already-valid input yields the same
        // bytes, so the fast path is exactly equivalent.
        let text = match simdutf8::basic::from_utf8(data) {
            Ok(s) => s.to_owned(),
            Err(_) => String::from_utf8_lossy(data).into_owned(),
        };
        // Derive the spans from `str::lines` itself rather than reimplementing
        // its rules (`\n` vs `\r\n`, optional final terminator): each yielded
        // slice points into `text`, so its offset is exact by construction.
        //
        // Offsets are `usize`, not `u32`: `max_file_size` is configurable
        // (`GREPLM_MAX_FILE_SIZE`) and files are re-read at query time, so a
        // 4 GiB+ file is reachable, and truncating an offset would produce an
        // inverted range and panic on the slice.
        let base = text.as_ptr() as usize;
        let spans = text
            .lines()
            .map(|line| {
                let start = line.as_ptr() as usize - base;
                (start, start + line.len())
            })
            .collect();
        FileLines { text, spans }
    }

    fn len(&self) -> usize {
        self.spans.len()
    }

    fn get(&self, i: usize) -> Option<&str> {
        self.spans.get(i).map(|&(s, e)| &self.text[s..e])
    }
}

/// The symbol line spans of one document, in storage order, so that many
/// positions can be resolved against the same document without re-reading its
/// line column each time.
///
/// A hot callee has thousands of call sites concentrated in a few files, and
/// attributing each one to its enclosing caller is the inner loop of `callers`
/// and therefore of `blast_radius`. Reading the document's spans once turns that
/// from O(sites x symbols-per-document) into O(symbols-per-document + sites).
struct DocSymbolRanges {
    /// `(line_start, line_end, row id)`, in storage order.
    rows: Vec<(u32, u32, u32)>,
}

impl DocSymbolRanges {
    fn load(seg: &Segment, doc_id: u32) -> DocSymbolRanges {
        let rows = seg
            .doc_sym_rows(doc_id)
            .filter_map(|i| seg.sym_view(i).map(|v| (v.line_start, v.line_end, i)))
            .collect();
        DocSymbolRanges { rows }
    }

    /// Row id of the innermost symbol whose range contains `line`.
    ///
    /// Among equally tight ranges the earliest row in storage order wins, which
    /// is the tie-break the original per-call scan had.
    fn enclosing_row(&self, line: u32) -> Option<u32> {
        let mut best: Option<(u32, u32)> = None; // (row id, span)
        for &(start, end, i) in &self.rows {
            if start <= line && line <= end {
                let span = end - start;
                match best {
                    Some((_, best_span)) if best_span <= span => {}
                    _ => best = Some((i, span)),
                }
            }
        }
        best.map(|(i, _)| i)
    }
}

/// Build the path -> (segment index, doc id) lookup over live documents.
/// A path is live in exactly one segment (changed files tombstone the old
/// copy), so the map is unambiguous.
fn build_path_index(segments: &[Segment]) -> HashMap<String, (usize, u32)> {
    let mut map = HashMap::new();
    for (si, seg) in segments.iter().enumerate() {
        for (doc_id, doc) in seg.docs.iter().enumerate() {
            let doc_id = doc_id as u32;
            if seg.is_live(doc_id) {
                map.insert(doc.path.clone(), (si, doc_id));
            }
        }
    }
    map
}

/// Byte offsets at which each line begins (index 0 is the start of the file).
fn line_starts(data: &[u8]) -> Vec<usize> {
    let mut starts = Vec::with_capacity(64);
    starts.push(0usize);
    for p in memchr::memchr_iter(b'\n', data) {
        starts.push(p + 1);
    }
    starts
}

/// Zero-based line index containing byte offset `off`.
fn line_of(off: usize, starts: &[usize]) -> usize {
    // Greatest line start that is <= off.
    starts.partition_point(|&s| s <= off).saturating_sub(1)
}

/// The bytes of line `li` (without the trailing newline).
fn line_slice<'a>(data: &'a [u8], starts: &[usize], li: usize) -> &'a [u8] {
    let begin = starts[li];
    let end = if li + 1 < starts.len() {
        starts[li + 1].saturating_sub(1)
    } else {
        data.len()
    };
    &data[begin..end.min(data.len())]
}

/// ASCII-case-insensitive substring test. `needle` must already be ASCII
/// lowercase. Avoids materializing a lowercased copy of `hay`, which is what
/// made [`path_score`] too expensive to call per hit.
///
/// Naive offset scan: both strings are short (a repo-relative path and a
/// handful of literals), so this beats any setup cost.
fn contains_ascii_ci(hay: &str, needle: &[u8]) -> bool {
    let h = hay.as_bytes();
    if needle.len() > h.len() {
        return false;
    }
    (0..=h.len() - needle.len()).any(|i| {
        h[i..i + needle.len()]
            .iter()
            .zip(needle)
            .all(|(a, b)| a.to_ascii_lowercase() == *b)
    })
}

/// Ranking adjustment based on the file path: prefer shallow paths and
/// non-generated/non-test files.
///
/// Called once per hit, per ranked candidate and per scored symbol, so it must
/// not allocate — it used to build a lowercased copy of the path.
///
/// The test-path check needs just two probes, not five: `/tests/`, `__tests__`
/// and `.test.` all contain `test`, so they can only match when `test` does.
pub(crate) fn path_score(path: &str) -> f32 {
    let mut s = 0.0f32;
    let depth = memchr::memchr_iter(b'/', path.as_bytes()).count() as f32;
    s -= depth * 0.05;
    if contains_ascii_ci(path, b"test") || contains_ascii_ci(path, b".spec.") {
        s -= 1.0;
    }
    if contains_ascii_ci(path, b"/vendor/")
        || contains_ascii_ci(path, b"/generated/")
        || contains_ascii_ci(path, b".min.")
    {
        s -= 1.5;
    }
    s
}

/// Sorted, deduplicated set of lines on which `doc_id` defines a symbol, used
/// to bonus-score matches that land on a definition.
///
/// Runs for every file that produced a match, so it reads the line column
/// through borrowed views rather than materializing each symbol's strings. A
/// sorted `Vec` beats a `HashSet` here: the counts are small and it is one
/// allocation instead of a table plus hashing per row.
fn symbol_lines(seg: &Segment, doc_id: u32) -> Vec<u32> {
    let mut lines: Vec<u32> = seg.doc_sym_views(doc_id).map(|s| s.line_start).collect();
    lines.sort_unstable();
    lines.dedup();
    lines
}

fn match_symbol(name: &str, lower: &str, needle: &str, exact: bool) -> Option<f32> {
    if exact {
        return if lower == needle { Some(100.0) } else { None };
    }
    if lower == needle {
        Some(100.0)
    } else if lower.starts_with(needle) {
        Some(70.0)
    } else if acronym_eq(name, needle) {
        // e.g. "lc" matches loadConfig / load_config.
        Some(60.0)
    } else if lower.contains(needle) {
        Some(50.0)
    } else if is_subsequence(needle, lower) {
        Some(30.0)
    } else {
        None
    }
}

/// True when `needle` equals the acronym of `name` — the lowercased first
/// letter of each identifier token, split on camelCase and snake/kebab (so "lc"
/// matches `loadConfig` and `load_config`).
///
/// Streams the comparison instead of building the acronym. The fuzzy symbol
/// scan reaches this branch for nearly every symbol in the repository (only
/// exact and prefix matches short-circuit before it), so the old version's
/// `Vec<String>` of tokens per symbol dominated that path.
fn acronym_eq(name: &str, needle: &str) -> bool {
    let mut want = needle.chars();
    let mut prev_lower = false;
    // Whether we are inside a token; mirrors the tokenizer's `!cur.is_empty()`.
    let mut open = false;
    for ch in name.chars() {
        if ch == '_' || ch == '-' || ch == ' ' {
            open = false;
            prev_lower = false;
            continue;
        }
        if ch.is_uppercase() && prev_lower && open {
            open = false;
        }
        if !open {
            open = true;
            // This character starts a token, so the first `char` of its
            // lowercased form is the acronym letter this token contributes.
            let c = match ch.to_lowercase().next() {
                Some(c) => c,
                None => continue,
            };
            if want.next() != Some(c) {
                return false;
            }
        }
        prev_lower = ch.is_lowercase() || ch.is_numeric();
    }
    // Every token consumed exactly one needle character, and none are left.
    want.next().is_none()
}

/// Rank `items` best-first and apply offset/limit. Uses a partial selection so
/// we only fully sort the `offset + limit` items we actually return.
fn rank_paginate<T, F>(mut items: Vec<T>, cmp: F, offset: usize, limit: usize) -> Vec<T>
where
    F: Fn(&T, &T) -> std::cmp::Ordering,
{
    let need = offset.saturating_add(limit);
    if need == 0 {
        return Vec::new();
    }
    if need < items.len() {
        items.select_nth_unstable_by(need - 1, |a, b| cmp(a, b));
        items.truncate(need);
    }
    items.sort_by(|a, b| cmp(a, b));
    if offset >= items.len() {
        return Vec::new();
    }
    items.drain(0..offset);
    items.truncate(limit);
    items
}

/// Index-free fallback search: walk the working tree and scan every file with
/// the matcher, with no trigram prefilter. Used when the index is missing or
/// errors, so `search` still returns grep-equivalent results instead of failing.
/// Honors the same `lang`/`path` filters, `exhaustive` mode, and ordering as the
/// indexed path. Slower (reads every candidate file) but correct and complete.
pub fn grep_walk(paths: &Paths, config: &Config, query: &SearchQuery) -> Result<Vec<SearchHit>> {
    if query.pattern.is_empty() {
        return Ok(Vec::new());
    }
    let matcher = Matcher::build(query)?;
    let walked = crate::walk::walk(paths, config)?;
    let path_filter = query.path.as_deref();
    let lang_filter = query.lang.as_deref();
    let max_per_file = query.max_per_file;
    let whole_word = query.whole_word;
    let exhaustive = query.exhaustive;
    let index_binary = config.index_binary;
    let cap = if exhaustive {
        usize::MAX
    } else {
        PER_FILE_MATCH_CAP
    };

    let mut hits: Vec<SearchHit> = walked
        .entries
        .par_iter()
        .flat_map_iter(|e| {
            if path_filter.is_some_and(|pf| !e.rel.contains(pf)) {
                return Vec::new().into_iter();
            }
            let ext = e
                .path
                .extension()
                .and_then(|x| x.to_str())
                .unwrap_or("")
                .to_ascii_lowercase();
            let lang_id = Language::from_extension(&ext).id().to_string();
            if lang_filter.is_some_and(|lf| lang_id != lf) {
                return Vec::new().into_iter();
            }
            let data = match std::fs::read(&e.path) {
                Ok(d) => d,
                Err(_) => return Vec::new().into_iter(),
            };
            if !index_binary && memchr::memchr(0, &data).is_some() {
                return Vec::new().into_iter();
            }
            let matches = matcher.match_starts(&data, whole_word, cap);
            if matches.is_empty() {
                return Vec::new().into_iter();
            }
            let starts = line_starts(&data);
            let base = path_score(&e.rel);
            let mut out = Vec::new();
            let mut last_line = 0u32;
            for (start, _end) in matches {
                let li = line_of(start, &starts);
                let line_no = li as u32 + 1;
                if line_no == last_line {
                    continue;
                }
                last_line = line_no;
                let col = (start - starts[li]) as u32 + 1;
                out.push(SearchHit {
                    path: e.rel.clone(),
                    lang: lang_id.clone(),
                    line: line_no,
                    column: col,
                    text: snippet(line_slice(&data, &starts, li)),
                    score: 1.0 + base,
                });
            }
            if !exhaustive && out.len() > max_per_file {
                out.sort_by(|a, b| {
                    b.score
                        .partial_cmp(&a.score)
                        .unwrap_or(std::cmp::Ordering::Equal)
                        .then_with(|| a.line.cmp(&b.line))
                });
                out.truncate(max_per_file);
            }
            out.into_iter()
        })
        .collect();

    if exhaustive {
        hits.sort_by(|a, b| {
            a.path
                .cmp(&b.path)
                .then_with(|| a.line.cmp(&b.line))
                .then_with(|| a.column.cmp(&b.column))
        });
        return Ok(hits);
    }
    let cmp = |a: &SearchHit, b: &SearchHit| {
        b.score
            .partial_cmp(&a.score)
            .unwrap_or(std::cmp::Ordering::Equal)
            .then_with(|| a.path.cmp(&b.path))
            .then_with(|| a.line.cmp(&b.line))
    };
    Ok(rank_paginate(hits, cmp, query.offset, query.limit))
}

/// Number of leading path components shared by two relative paths.
fn shared_prefix_len(a: &str, b: &str) -> usize {
    a.split('/')
        .zip(b.split('/'))
        .take_while(|(x, y)| x == y)
        .count()
}

/// Apply offset/limit to an already-ordered vector.
fn paginate<T>(mut items: Vec<T>, offset: usize, limit: usize) -> Vec<T> {
    if offset >= items.len() {
        return Vec::new();
    }
    items.drain(0..offset);
    items.truncate(limit);
    items
}

fn is_subsequence(needle: &str, haystack: &str) -> bool {
    if needle.is_empty() {
        return true;
    }
    let mut chars = needle.chars();
    let mut cur = chars.next();
    for h in haystack.chars() {
        if let Some(c) = cur {
            if c == h {
                cur = chars.next();
            }
        } else {
            break;
        }
    }
    cur.is_none()
}

/// Trim and bound a matched line for display.
fn snippet(line: &[u8]) -> String {
    let s = String::from_utf8_lossy(line);
    let trimmed = s.trim_end();
    const MAX: usize = 320;
    if trimmed.len() > MAX {
        let mut end = MAX;
        while !trimmed.is_char_boundary(end) {
            end -= 1;
        }
        format!("{}", &trimmed[..end])
    } else {
        trimmed.to_string()
    }
}

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

    /// The allocating acronym the fuzzy matcher used before `acronym_eq`, kept
    /// as the reference definition of the behavior.
    fn reference_acronym(s: &str) -> String {
        crate::context::split_identifier(s)
            .iter()
            .filter_map(|t| t.chars().next())
            .collect()
    }

    /// Identifiers covering every tokenizer branch, plus the non-ASCII cases
    /// where `to_lowercase` can expand a single `char`.
    const NAMES: &[&str] = &[
        "",
        "x",
        "flush",
        "loadConfig",
        "load_config",
        "LoadConfig",
        "HTTPServer",
        "parseHTTP2Frame",
        "v2Handler",
        "vfs_read",
        "__init_waitqueue_head",
        "trailing__",
        "a__b",
        "kebab-case-name",
        "with space",
        "snake_And_Camel",
        "ALLCAPS",
        "ÄÖÜ_grüß",
        "İstanbul",
        "page_cache_sync_readahead",
    ];

    /// `acronym_eq` must agree with the allocating implementation for every
    /// name, both on its own acronym and on near-miss needles.
    #[test]
    fn acronym_eq_matches_reference() {
        for name in NAMES {
            let want = reference_acronym(name);
            assert!(
                acronym_eq(name, &want),
                "{name:?} should match its own acronym {want:?}"
            );
            // Perturbations that must not match.
            let mut wrong = vec![format!("{want}z"), format!("z{want}")];
            if !want.is_empty() {
                wrong.push(want[..want.len() - 1].to_string());
                wrong.push(want.to_uppercase());
            }
            for w in wrong {
                if w == want {
                    continue;
                }
                assert_eq!(
                    acronym_eq(name, &w),
                    reference_acronym(name) == w,
                    "{name:?} vs needle {w:?}"
                );
            }
        }
    }

    /// Cross-check against every other name's acronym, so a needle that happens
    /// to collide is treated identically by both implementations.
    #[test]
    fn acronym_eq_agrees_on_all_pairs() {
        for name in NAMES {
            for other in NAMES {
                let needle = reference_acronym(other);
                assert_eq!(
                    acronym_eq(name, &needle),
                    reference_acronym(name) == needle,
                    "name {name:?} vs needle {needle:?}"
                );
            }
        }
    }

    /// The original `path_score`: one lowercased copy, five test-path probes,
    /// three vendor probes. Kept as the reference definition of the scoring.
    fn reference_path_score(path: &str) -> f32 {
        let mut s = 0.0f32;
        let depth = path.matches('/').count() as f32;
        s -= depth * 0.05;
        let lower = path.to_ascii_lowercase();
        if lower.contains("test")
            || lower.contains("/tests/")
            || lower.contains("__tests__")
            || lower.contains(".test.")
            || lower.contains(".spec.")
        {
            s -= 1.0;
        }
        if lower.contains("/vendor/") || lower.contains("/generated/") || lower.contains(".min.") {
            s -= 1.5;
        }
        s
    }

    /// The allocation-free rewrite must score every path exactly as before,
    /// including the collapsed test-path disjunction and ASCII case folding.
    #[test]
    fn path_score_matches_reference() {
        let paths = [
            "",
            "a.c",
            "fs/read_write.c",
            "a/b/c/d/e/f/g.rs",
            "src/tests/mod.rs",
            "src/TESTS/mod.rs",
            "Test.java",
            "TEST.java",
            "foo/__tests__/bar.js",
            "foo/bar.test.ts",
            "foo/bar.spec.ts",
            "foo/bar.SPEC.ts",
            "protest/attestation.c",
            "third_party/vendor/lib.go",
            "third_party/VENDOR/lib.go",
            "out/generated/api.rs",
            "web/app.min.js",
            "web/app.MIN.js",
            "vendor/nested/test/.spec.x",
            "no_slashes_or_markers",
            "spec.rs",
            ".spec.",
            "tes",
            "t",
        ];
        for p in paths {
            assert_eq!(
                path_score(p),
                reference_path_score(p),
                "path_score mismatch for {p:?}"
            );
        }
    }

    /// `contains_ascii_ci` must agree with lowercase-then-`contains`.
    #[test]
    fn contains_ascii_ci_matches_std() {
        let hays = [
            "", "a", "Test", "tEsT", "xxtestxx", "TES", "/Vendor/", ".MIN.", "aaa",
        ];
        for h in hays {
            for n in [
                &b"test"[..],
                b".spec.",
                b"/vendor/",
                b"/generated/",
                b".min.",
                b"a",
            ] {
                let needle = std::str::from_utf8(n).unwrap();
                assert_eq!(
                    contains_ascii_ci(h, n),
                    h.to_ascii_lowercase().contains(needle),
                    "{h:?} contains {needle:?}"
                );
            }
        }
    }

    /// `FileLines` must be indistinguishable from the
    /// `String::from_utf8_lossy(..).lines().map(to_string).collect()` it
    /// replaced — including CRLF, blank lines, a missing final terminator, and
    /// invalid UTF-8 (which lossy conversion turns into U+FFFD, changing byte
    /// lengths and therefore the packing cost).
    #[test]
    fn file_lines_match_str_lines() {
        let cases: &[&[u8]] = &[
            b"",
            b"\n",
            b"a",
            b"a\n",
            b"a\nb",
            b"a\nb\n",
            b"\na",
            b"a\n\nb\n",
            b"a\r\nb\r\n",
            b"a\r\nb",
            b"a\r",
            b"\r\n\r\n",
            b"no terminator at all",
            b"trailing blank lines\n\n\n",
            b"tabs\tand  spaces\n  indented\n",
            &[0xC3, 0x28, b'\n', b'o', b'k'],        // invalid UTF-8
            &[b'a', b'\n', 0xFF, 0xFE, b'\n', b'z'], // invalid UTF-8
            "héllo\nwörld\n".as_bytes(),             // multibyte
        ];
        for data in cases {
            let want: Vec<String> = String::from_utf8_lossy(data)
                .lines()
                .map(|s| s.to_string())
                .collect();
            let got = FileLines::new(data);
            assert_eq!(got.len(), want.len(), "line count for {data:?}");
            for (i, line) in want.iter().enumerate() {
                assert_eq!(got.get(i), Some(line.as_str()), "line {i} of {data:?}");
            }
            assert_eq!(got.get(want.len()), None, "past-the-end for {data:?}");
        }
    }

    /// The enclosing-symbol rule: innermost (tightest) range wins, and among
    /// equally tight ranges the earliest row in storage order wins. Both
    /// `callers` and `references_resolved` attribute references with this, so
    /// its tie-break decides user-visible output.
    #[test]
    fn enclosing_row_picks_innermost_then_earliest() {
        // Row 0 spans the whole file, row 1 is a method inside it, rows 2 and 3
        // are equally tight and overlapping, row 4 is a single line.
        let r = DocSymbolRanges {
            rows: vec![
                (1, 100, 0),
                (10, 20, 1),
                (30, 40, 2),
                (30, 40, 3),
                (50, 50, 4),
            ],
        };
        assert_eq!(
            r.enclosing_row(5),
            Some(0),
            "only the outer range contains it"
        );
        assert_eq!(
            r.enclosing_row(15),
            Some(1),
            "innermost wins over the outer"
        );
        assert_eq!(r.enclosing_row(35), Some(2), "earliest of two equal spans");
        assert_eq!(
            r.enclosing_row(50),
            Some(4),
            "single-line range is tightest"
        );
        assert_eq!(r.enclosing_row(200), None, "outside every range");
        assert_eq!(DocSymbolRanges { rows: vec![] }.enclosing_row(1), None);
        // Boundaries are inclusive on both ends.
        assert_eq!(r.enclosing_row(10), Some(1));
        assert_eq!(r.enclosing_row(20), Some(1));
        assert_eq!(r.enclosing_row(9), Some(0));
    }

    /// The documented behavior of the acronym branch.
    #[test]
    fn acronym_matches_camel_and_snake() {
        assert!(acronym_eq("loadConfig", "lc"));
        assert!(acronym_eq("load_config", "lc"));
        assert!(acronym_eq("vfs_read", "vr"));
        assert!(!acronym_eq("loadConfig", "l"));
        assert!(!acronym_eq("loadConfig", "lcx"));
    }
}