colgrep 1.5.0

Semantic code search powered by ColBERT
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
use std::collections::HashMap;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use colored::Colorize;

use colgrep::{
    acquire_index_lock, bre_to_ere, ensure_model, escape_literal_braces, find_parent_index,
    get_index_dir_for_project, get_vector_index_path, index_exists, is_text_format,
    path_contains_ignored_dir, Config, IndexBuilder, IndexState, Searcher, DEFAULT_MODEL,
};

use crate::display::{
    calc_display_ranges, find_representative_lines, group_results_by_file,
    print_highlighted_content, print_highlighted_ranges,
};
use crate::scoring::should_search_from_root;

/// Pre-compiled pattern matcher for efficient repeated matching.
/// Compiling regex is expensive (~microseconds), so we do it once and reuse.
///
/// Backed by `fancy-regex` so lookaround (`(?=...)`, `(?<=...)`) and
/// backreferences (`\1`) work. Standard patterns still go through the
/// fast `regex`-crate engine internally; the fancy backtracking NFA only
/// kicks in for features `regex` cannot handle.
struct PatternMatcher {
    kind: PatternKind,
    case_sensitive: bool,
}

enum PatternKind {
    /// Compiled regex (case-insensitivity is baked into the pattern via
    /// an inline `(?i)` flag — `case_sensitive` is informational here).
    Regex(fancy_regex::Regex),
    /// Pre-normalized literal needle. When `case_sensitive` is false this
    /// has been lowercased and lines are lowercased before `.contains`.
    Literal(String),
}

impl PatternMatcher {
    /// Create a new pattern matcher based on the matching mode.
    /// - `extended_regexp`: Use ERE (extended regular expressions)
    /// - `fixed_strings`: Treat pattern as literal (overrides extended_regexp)
    /// - `word_regexp`: Add word boundaries
    /// - `case_sensitive`: When false, prefixes `(?i)` so matching is
    ///   case-insensitive (colgrep's historical default).
    fn new(
        pattern: &str,
        extended_regexp: bool,
        fixed_strings: bool,
        word_regexp: bool,
        case_sensitive: bool,
    ) -> Self {
        let effective_use_regex = extended_regexp && !fixed_strings;
        let case_prefix = if case_sensitive { "" } else { "(?i)" };
        let literal_needle = || {
            if case_sensitive {
                pattern.to_string()
            } else {
                pattern.to_lowercase()
            }
        };

        if effective_use_regex {
            let ere_pattern = escape_literal_braces(&bre_to_ere(pattern));
            let body = if word_regexp {
                format!(r"\b{}\b", ere_pattern)
            } else {
                ere_pattern
            };
            let regex_pattern = format!("{}{}", case_prefix, body);
            match fancy_regex::Regex::new(&regex_pattern) {
                Ok(re) => PatternMatcher {
                    kind: PatternKind::Regex(re),
                    case_sensitive,
                },
                Err(_) => PatternMatcher {
                    kind: PatternKind::Literal(literal_needle()),
                    case_sensitive,
                },
            }
        } else if word_regexp {
            let escaped = regex::escape(pattern);
            let regex_pattern = format!(r"{}\b{}\b", case_prefix, escaped);
            match fancy_regex::Regex::new(&regex_pattern) {
                Ok(re) => PatternMatcher {
                    kind: PatternKind::Regex(re),
                    case_sensitive,
                },
                Err(_) => PatternMatcher {
                    kind: PatternKind::Literal(literal_needle()),
                    case_sensitive,
                },
            }
        } else {
            PatternMatcher {
                kind: PatternKind::Literal(literal_needle()),
                case_sensitive,
            }
        }
    }

    /// Test whether a single line matches.
    fn line_matches(&self, line: &str) -> bool {
        match &self.kind {
            // `is_match` is `Result<bool>` under fancy-regex (the backtracking
            // engine can fail with `backtrack_limit_exceeded` on adversarial
            // patterns). Treat any such failure as "no match" so a single
            // pathological line cannot abort the whole scan.
            PatternKind::Regex(re) => re.is_match(line).unwrap_or(false),
            PatternKind::Literal(needle) => {
                if self.case_sensitive {
                    line.contains(needle.as_str())
                } else {
                    line.to_lowercase().contains(needle)
                }
            }
        }
    }

    /// Find matching line numbers within a code unit's content.
    /// Returns 1-indexed line numbers where matches were found.
    fn find_matches_in_unit(&self, unit: &colgrep::CodeUnit) -> Vec<usize> {
        let matches: Vec<usize> = unit
            .code
            .lines()
            .enumerate()
            .filter_map(|(i, line)| {
                if self.line_matches(line) {
                    Some(unit.line + i)
                } else {
                    None
                }
            })
            .collect();

        if matches.is_empty() {
            if let PatternKind::Regex(_) = &self.kind {
                return self.literal_fallback(unit);
            }
        }
        matches
    }

    /// Literal fallback for when regex mode produces no matches.
    fn literal_fallback(&self, unit: &colgrep::CodeUnit) -> Vec<usize> {
        let pattern_str = match &self.kind {
            PatternKind::Regex(re) => re.as_str().to_string(),
            PatternKind::Literal(p) => p.clone(),
        };
        let needle = if self.case_sensitive {
            pattern_str
        } else {
            pattern_str.to_lowercase()
        };

        unit.code
            .lines()
            .enumerate()
            .filter_map(|(i, line)| {
                let haystack = if self.case_sensitive {
                    line.to_string()
                } else {
                    line.to_lowercase()
                };
                if haystack.contains(&needle) {
                    Some(unit.line + i)
                } else {
                    None
                }
            })
            .collect()
    }

    /// Scan a file from disk for matching lines. Used by compact regex mode
    /// so we don't miss matches in chunks that were merged into a leader
    /// by `collapse_by_file` (the leader's `unit.code` only carries one
    /// chunk's text). Returns 1-indexed `(line_number, line_content)`
    /// pairs in source order.
    fn find_matches_in_file(&self, file: &Path) -> Vec<(usize, String)> {
        let content = match std::fs::read_to_string(file) {
            Ok(c) => c,
            Err(_) => return Vec::new(),
        };

        let matches: Vec<(usize, String)> = content
            .lines()
            .enumerate()
            .filter_map(|(i, line)| {
                if self.line_matches(line) {
                    Some((i + 1, line.to_string()))
                } else {
                    None
                }
            })
            .collect();

        if !matches.is_empty() {
            return matches;
        }

        // Literal fallback when the regex (often regex metacharacters
        // a user meant literally) finds nothing.
        if let PatternKind::Regex(re) = &self.kind {
            let needle_raw = re.as_str();
            let needle = if self.case_sensitive {
                needle_raw.to_string()
            } else {
                needle_raw.to_lowercase()
            };
            return content
                .lines()
                .enumerate()
                .filter_map(|(i, line)| {
                    let haystack = if self.case_sensitive {
                        line.to_string()
                    } else {
                        line.to_lowercase()
                    };
                    if haystack.contains(&needle) {
                        Some((i + 1, line.to_string()))
                    } else {
                        None
                    }
                })
                .collect();
        }

        matches
    }
}

/// Strip regex special characters from a pattern for use in semantic queries.
///
/// When combining a regex pattern with a semantic query, the regex metacharacters
/// (like `\s`, `\w`, `+`, `*`, etc.) have no semantic meaning and could negatively
/// affect the embedding quality. This function extracts only the meaningful text
/// content from a regex pattern.
///
/// Examples:
/// - `fn\s+\w+` -> `fn`
/// - `async\s+fn` -> `async fn`
/// - `Result<.*>` -> `Result`
/// - `foo|bar` -> `foo bar`
fn strip_regex_for_semantic(pattern: &str) -> String {
    let mut result = String::with_capacity(pattern.len());
    let mut chars = pattern.chars().peekable();

    while let Some(c) = chars.next() {
        match c {
            // Handle backslash escapes
            '\\' => {
                if let Some(&next) = chars.peek() {
                    match next {
                        // Common regex character classes - skip both backslash and class char
                        's' | 'S' | 'w' | 'W' | 'd' | 'D' | 'b' | 'B' | 'n' | 'r' | 't' => {
                            chars.next();
                            // Add a space to separate tokens (e.g., "fn\s+bar" -> "fn bar")
                            if !result.ends_with(' ') && !result.is_empty() {
                                result.push(' ');
                            }
                        }
                        // Escaped literal characters - keep the literal
                        '.' | '*' | '+' | '?' | '[' | ']' | '(' | ')' | '{' | '}' | '^' | '$'
                        | '|' | '\\' => {
                            chars.next();
                            result.push(next);
                        }
                        // Other escapes - skip the backslash, keep the char
                        _ => {
                            chars.next();
                            result.push(next);
                        }
                    }
                }
            }
            // Quantifiers and metacharacters - skip them
            '*' | '+' | '?' => {}
            // Anchors - skip them
            '^' | '$' => {}
            // Character class - skip entire [...] block
            #[allow(clippy::while_let_on_iterator)]
            '[' => {
                // Skip until we find the closing ]
                // Note: using while let because we need to call chars.next() inside for escaped chars
                let mut depth = 1;
                while let Some(inner) = chars.next() {
                    if inner == '\\' {
                        // Skip escaped char inside character class
                        chars.next();
                    } else if inner == '[' {
                        depth += 1;
                    } else if inner == ']' {
                        depth -= 1;
                        if depth == 0 {
                            break;
                        }
                    }
                }
            }
            // Grouping - skip the parens but process contents
            '(' | ')' => {}
            // Alternation - convert to space
            '|' => {
                if !result.ends_with(' ') && !result.is_empty() {
                    result.push(' ');
                }
            }
            // Quantifier ranges {n,m} - skip entire block
            '{' => {
                for inner in chars.by_ref() {
                    if inner == '}' {
                        break;
                    }
                }
            }
            // Dot (any char) - skip
            '.' => {}
            // Regular characters - keep them
            _ => {
                result.push(c);
            }
        }
    }

    // Clean up multiple spaces and trim
    let cleaned: String = result.split_whitespace().collect::<Vec<_>>().join(" ");

    cleaned
}

/// Merge a semantic query with a sanitized regex pattern, removing duplicate tokens.
///
/// This prevents redundant tokens in the final query when the regex pattern
/// contains words that are already in the semantic query.
///
/// Example:
/// - query: "async function", pattern: "async fn" -> "async function fn"
/// - query: "error handling", pattern: "error" -> "error handling"
fn merge_query_with_pattern(query: &str, sanitized_pattern: &str) -> String {
    if sanitized_pattern.is_empty() {
        return query.to_string();
    }

    // Collect query tokens (lowercase for comparison)
    let query_tokens: std::collections::HashSet<String> =
        query.split_whitespace().map(|s| s.to_lowercase()).collect();

    // Filter pattern tokens to only include those not already in the query
    let new_tokens: Vec<&str> = sanitized_pattern
        .split_whitespace()
        .filter(|token| !query_tokens.contains(&token.to_lowercase()))
        .collect();

    if new_tokens.is_empty() {
        query.to_string()
    } else {
        format!("{} {}", query, new_tokens.join(" "))
    }
}

/// Resolve the model to use: CLI arg > saved config > default
pub fn resolve_model(cli_model: Option<&str>) -> String {
    if let Some(model) = cli_model {
        return model.to_string();
    }

    // Try to load from config
    if let Ok(config) = Config::load() {
        if let Some(model) = config.get_default_model() {
            return model.to_string();
        }
    }

    // Fall back to default
    DEFAULT_MODEL.to_string()
}

/// Resolve top_k: CLI arg > saved config > default
pub fn resolve_top_k(cli_k: Option<usize>, default: usize) -> usize {
    if let Some(k) = cli_k {
        return k;
    }

    // Try to load from config
    if let Ok(config) = Config::load() {
        if let Some(k) = config.get_default_k() {
            return k;
        }
    }

    default
}

/// Resolve context_lines (n): CLI arg > saved config > default
pub fn resolve_context_lines(cli_n: Option<usize>, default: usize) -> usize {
    if let Some(n) = cli_n {
        return n;
    }

    // Try to load from config
    if let Ok(config) = Config::load() {
        if let Some(n) = config.get_default_n() {
            return n;
        }
    }

    default
}

/// Resolve relative_paths: saved config > default (true = relative paths)
pub fn resolve_relative_paths() -> bool {
    if let Ok(config) = Config::load() {
        return config.use_relative_paths();
    }
    true
}

/// Format a path for display, using relative or absolute based on config.
fn display_path(path: &Path, use_relative: bool) -> String {
    let current_dir = std::env::current_dir().ok();
    display_path_with_cwd(path, current_dir.as_deref(), use_relative)
}

fn display_path_with_cwd(path: &Path, cwd: Option<&Path>, use_relative: bool) -> String {
    let normalized_path = normalize_windows_path(path);

    if use_relative {
        if let Some(cwd) = cwd {
            let normalized_cwd = normalize_windows_path(cwd);
            return normalized_path
                .strip_prefix(&normalized_cwd)
                .unwrap_or(&normalized_path)
                .display()
                .to_string();
        }
    }

    normalized_path.display().to_string()
}

fn is_external_project_path(search_path: &Path, cwd: Option<&Path>) -> bool {
    let Some(cwd) = cwd else {
        return false;
    };

    let normalized_search_path = normalize_windows_path(search_path);
    let normalized_cwd = normalize_windows_path(cwd);
    !normalized_search_path.starts_with(&normalized_cwd)
}

fn normalize_windows_path(path: &Path) -> PathBuf {
    #[cfg(windows)]
    {
        use std::path::{Component, Prefix};

        let mut components = path.components();
        match components.next() {
            Some(Component::Prefix(prefix_component)) => match prefix_component.kind() {
                Prefix::VerbatimDisk(drive) => {
                    let mut normalized = PathBuf::from(format!("{}:\\", char::from(drive)));
                    normalized.push(components.as_path());
                    normalized
                }
                Prefix::VerbatimUNC(server, share) => {
                    let mut normalized = PathBuf::from(format!(
                        r"\\{}\{}",
                        server.to_string_lossy(),
                        share.to_string_lossy()
                    ));
                    normalized.push(components.as_path());
                    normalized
                }
                _ => path.to_path_buf(),
            },
            _ => path.to_path_buf(),
        }
    }
    #[cfg(not(windows))]
    {
        path.to_path_buf()
    }
}

/// Resolve verbose: saved config > default (false)
pub fn resolve_verbose() -> bool {
    if let Ok(config) = Config::load() {
        return config.is_verbose();
    }
    false
}

/// Resolve pool_factor: --no-pool > --pool-factor > config > default (2)
pub fn resolve_pool_factor(cli_pool_factor: Option<usize>, no_pool: bool) -> Option<usize> {
    if no_pool {
        return Some(1); // Disable pooling
    }

    if let Some(factor) = cli_pool_factor {
        return Some(factor.max(1)); // Minimum is 1
    }

    // Try to load from config
    if let Ok(config) = Config::load() {
        return Some(config.get_pool_factor());
    }

    // Default pool factor
    Some(colgrep::DEFAULT_POOL_FACTOR)
}

#[allow(clippy::too_many_arguments)]
pub fn cmd_search(
    query: &str,
    paths: &[PathBuf],
    top_k: usize,
    top_k_explicit: bool,
    cli_model: Option<&str>,
    json: bool,
    include_patterns: &[String],
    files_only: bool,
    show_content: bool,
    cli_context_lines: Option<usize>,
    text_pattern: Option<&str>,
    extended_regexp: bool,
    fixed_strings: bool,
    word_regexp: bool,
    case_sensitive: bool,
    exclude_patterns: &[String],
    exclude_dirs: &[String],
    code_only: bool,
    no_fts: bool,
    alpha: Option<f32>,
    pool_factor: Option<usize>,
    auto_confirm: bool,
    static_batch: bool,
) -> Result<()> {
    // Resolve context_lines: CLI > config > default (20)
    let context_lines = resolve_context_lines(cli_context_lines, 20);
    // Resolve relative paths: config > default (false = absolute)
    let use_relative = resolve_relative_paths();

    // When -e is used and the user didn't explicitly pass -k, return *all*
    // matching lines (parity with grep, which has no implicit cap). We keep
    // a finite ceiling so the upstream `top_k * 4` / `top_k * 20` multipliers
    // can't overflow; `usize::MAX / 1024` is still ~1.8e16, effectively
    // unbounded for any real index.
    let regex_unbounded = text_pattern.is_some() && !top_k_explicit;
    let effective_top_k = if regex_unbounded {
        usize::MAX / 1024
    } else {
        top_k
    };

    // Collect results from all paths
    let mut all_results: Vec<colgrep::SearchResult> = Vec::new();
    let mut path_errors: Vec<String> = Vec::new();

    for path in paths {
        match search_single_path(
            query,
            path,
            effective_top_k,
            cli_model,
            json,
            include_patterns,
            files_only,
            text_pattern,
            extended_regexp,
            fixed_strings,
            word_regexp,
            case_sensitive,
            exclude_patterns,
            exclude_dirs,
            code_only,
            no_fts,
            alpha,
            pool_factor,
            auto_confirm,
            static_batch,
        ) {
            Ok(results) => all_results.extend(results),
            Err(e) => {
                let err_msg = format!("{}", e);
                // Check if this is a "path does not exist" error
                if err_msg.contains("Path does not exist:") {
                    // Store error message for later display, continue with other paths
                    path_errors.push(err_msg);
                } else {
                    // For other errors, fail immediately
                    return Err(e);
                }
            }
        }
    }

    // If ALL paths failed, return error with all messages
    if all_results.is_empty() && !path_errors.is_empty() {
        anyhow::bail!("{}", path_errors.join("\n\n"));
    }

    // Print warnings for failed paths (but we have some results)
    if !path_errors.is_empty() && !json && !files_only {
        for err in &path_errors {
            eprintln!("⚠️  {}\n", err);
        }
    }

    // Sort all results by score and take top_k
    all_results.sort_by(|a, b| {
        b.score
            .partial_cmp(&a.score)
            .unwrap_or(std::cmp::Ordering::Equal)
    });

    // Filter out text/config files if --code-only is enabled
    let filtered_results: Vec<_> = if code_only {
        all_results
            .into_iter()
            .filter(|r| !is_text_format(r.unit.language))
            .collect()
    } else {
        all_results
    };
    let results: Vec<_> = filtered_results.into_iter().take(effective_top_k).collect();

    // When -e is used without -F, automatically enable regex mode (ERE)
    let effective_extended_regexp = extended_regexp || (text_pattern.is_some() && !fixed_strings);

    // Output
    if files_only {
        // -l mode: show only unique filenames
        let mut seen_files = std::collections::HashSet::new();
        for result in &results {
            let file_str = display_path(&result.unit.file, use_relative);
            if seen_files.insert(file_str.clone()) {
                println!("{}", file_str);
            }
        }
    } else if json {
        println!("{}", serde_json::to_string_pretty(&results)?);
    } else {
        if results.is_empty() {
            println!("No results found for: {}", query);
            return Ok(());
        }

        // Resolve verbose mode from config, but force verbose if -c or -n > 0 is used
        let verbose = if show_content || cli_context_lines.is_some_and(|n| n > 0) {
            true // Force verbose when user explicitly requests content display
        } else {
            resolve_verbose()
        };

        // Maximum characters of matching line content to show in compact mode
        const COMPACT_LINE_MAX_CHARS: usize = 120;

        if !verbose {
            let compact_matcher = text_pattern.map(|p| {
                PatternMatcher::new(
                    p,
                    effective_extended_regexp,
                    fixed_strings,
                    word_regexp,
                    case_sensitive,
                )
            });

            if let Some(ref matcher) = compact_matcher {
                // Regex mode: emit *every* matching line in each result file,
                // not just the lines that happen to fall inside the leader
                // chunk's `unit.code`. `collapse_by_file` merges multiple
                // matching chunks of the same file into one leader and only
                // keeps the leader's text, so a per-`unit.code` scan silently
                // drops matches from the merged-in chunks (a `def foo` line
                // at line 150 plus three comment hits at line 800+ would
                // collapse to just one of those, depending on which chunk
                // led the file).
                //
                // Scanning the file from disk keeps the line-count consistent
                // with `grep` while preserving colgrep's score-based file
                // ordering: results are already sorted by score, so the first
                // occurrence of each file wins.
                use std::collections::HashSet;

                let mut seen: HashSet<PathBuf> = HashSet::new();
                let mut per_file: Vec<(&colgrep::SearchResult, Vec<(usize, String)>)> = Vec::new();

                for result in &results {
                    if !seen.insert(result.unit.file.clone()) {
                        continue;
                    }
                    let file_matches = matcher.find_matches_in_file(&result.unit.file);
                    if !file_matches.is_empty() {
                        per_file.push((result, file_matches));
                    }
                }

                // `-k` caps the number of *documents* (files), not lines.
                // Every match in the kept documents is emitted.
                for (result, matching_lines) in &per_file {
                    let file_path = display_path(&result.unit.file, use_relative);
                    for (line_num, raw_line) in matching_lines {
                        let trimmed = raw_line.trim();
                        let truncated: String =
                            trimmed.chars().take(COMPACT_LINE_MAX_CHARS).collect();
                        let suffix = if trimmed.chars().count() > COMPACT_LINE_MAX_CHARS {
                            "..."
                        } else {
                            ""
                        };
                        println!("{}:{}:{}{}", file_path, line_num, truncated, suffix);
                    }
                }
                if !regex_unbounded {
                    eprintln!(
                        "\nShowing matches from top {} document(s); omit -k to see every match.",
                        top_k
                    );
                }
            } else {
                // Semantic-only mode: show filepath:start-end ordered by score
                for result in &results {
                    let file_path = display_path(&result.unit.file, use_relative);
                    println!(
                        "{}:{}-{}",
                        file_path, result.unit.line, result.unit.end_line
                    );
                }
            }
        } else {
            // Verbose mode: full content grouped by file with syntax highlighting

            // Pre-compile pattern matchers ONCE before the display loop.
            // This avoids expensive regex compilation on every result.
            let text_pattern_matcher = text_pattern.map(|p| {
                PatternMatcher::new(
                    p,
                    effective_extended_regexp,
                    fixed_strings,
                    word_regexp,
                    case_sensitive,
                )
            });

            // For query matching (when no -e pattern), use literal matching
            let query_matcher = PatternMatcher::new(query, false, true, false, case_sensitive);

            // Separate results into code files and documents/config files
            let (code_results, doc_results): (Vec<_>, Vec<_>) = results
                .iter()
                .partition(|r| !is_text_format(r.unit.language));

            let half_context = context_lines / 2;
            let has_text_pattern = text_pattern.is_some();

            // Calculate max line number across all results for consistent alignment
            let max_line_num = results.iter().map(|r| r.unit.end_line).max().unwrap_or(1);
            let line_num_width = max_line_num.to_string().len().max(4);

            // Display code results first, grouped by file
            if !code_results.is_empty() {
                let grouped = group_results_by_file(&code_results);
                for (file, file_results) in grouped {
                    // Print file header with relative path
                    println!("file: {}", display_path(&file, use_relative).cyan());
                    for result in file_results {
                        let file_to_read = &result.unit.file;
                        if let Ok(content) = std::fs::read_to_string(file_to_read) {
                            let lines: Vec<&str> = content.lines().collect();
                            let end = result.unit.end_line.min(lines.len());
                            let max_lines = if show_content {
                                usize::MAX
                            } else {
                                context_lines
                            };

                            if has_text_pattern {
                                let file_matches = text_pattern_matcher
                                    .as_ref()
                                    .unwrap()
                                    .find_matches_in_unit(&result.unit);
                                let ranges = calc_display_ranges(
                                    &file_matches,
                                    result.unit.line,
                                    end,
                                    half_context,
                                    max_lines,
                                    true,
                                );
                                print_highlighted_ranges(
                                    file_to_read,
                                    &lines,
                                    &ranges,
                                    end,
                                    line_num_width,
                                );
                            } else {
                                let query_matches =
                                    query_matcher.find_matches_in_unit(&result.unit);
                                if !query_matches.is_empty() {
                                    let ranges = calc_display_ranges(
                                        &query_matches,
                                        result.unit.line,
                                        end,
                                        half_context,
                                        max_lines,
                                        true,
                                    );
                                    print_highlighted_ranges(
                                        file_to_read,
                                        &lines,
                                        &ranges,
                                        end,
                                        line_num_width,
                                    );
                                } else {
                                    // No exact match - find most representative line(s) based on token overlap
                                    let representative_lines = find_representative_lines(
                                        &result.unit.code,
                                        result.unit.line,
                                        query,
                                    );
                                    if !representative_lines.is_empty() {
                                        let ranges = calc_display_ranges(
                                            &representative_lines,
                                            result.unit.line,
                                            end,
                                            half_context,
                                            max_lines,
                                            true,
                                        );
                                        print_highlighted_ranges(
                                            file_to_read,
                                            &lines,
                                            &ranges,
                                            end,
                                            line_num_width,
                                        );
                                    } else {
                                        // Final fallback: show from beginning
                                        let start = result.unit.line.saturating_sub(1);
                                        if start < lines.len() {
                                            print_highlighted_content(
                                                file_to_read,
                                                &lines,
                                                start,
                                                max_lines,
                                                end,
                                                line_num_width,
                                            );
                                        }
                                    }
                                }
                            }
                        }
                    }
                    println!();
                }
            }

            // Display document/config results after, grouped by file
            if !doc_results.is_empty() {
                let grouped = group_results_by_file(&doc_results);
                for (file, file_results) in grouped {
                    println!("file: {}", display_path(&file, use_relative).cyan());
                    for result in file_results {
                        let file_to_read = &result.unit.file;
                        if let Ok(content) = std::fs::read_to_string(file_to_read) {
                            let lines: Vec<&str> = content.lines().collect();
                            let end = result.unit.end_line.min(lines.len());
                            let max_lines = if show_content { 250 } else { context_lines };

                            if has_text_pattern {
                                let file_matches = text_pattern_matcher
                                    .as_ref()
                                    .unwrap()
                                    .find_matches_in_unit(&result.unit);
                                let ranges = calc_display_ranges(
                                    &file_matches,
                                    result.unit.line,
                                    end,
                                    half_context,
                                    max_lines,
                                    true,
                                );
                                print_highlighted_ranges(
                                    file_to_read,
                                    &lines,
                                    &ranges,
                                    end,
                                    line_num_width,
                                );
                            } else {
                                let query_matches =
                                    query_matcher.find_matches_in_unit(&result.unit);
                                if !query_matches.is_empty() {
                                    let ranges = calc_display_ranges(
                                        &query_matches,
                                        result.unit.line,
                                        end,
                                        half_context,
                                        max_lines,
                                        true,
                                    );
                                    print_highlighted_ranges(
                                        file_to_read,
                                        &lines,
                                        &ranges,
                                        end,
                                        line_num_width,
                                    );
                                } else {
                                    // No exact match - find most representative line(s) based on token overlap
                                    let representative_lines = find_representative_lines(
                                        &result.unit.code,
                                        result.unit.line,
                                        query,
                                    );
                                    if !representative_lines.is_empty() {
                                        let ranges = calc_display_ranges(
                                            &representative_lines,
                                            result.unit.line,
                                            end,
                                            half_context,
                                            max_lines,
                                            true,
                                        );
                                        print_highlighted_ranges(
                                            file_to_read,
                                            &lines,
                                            &ranges,
                                            end,
                                            line_num_width,
                                        );
                                    } else {
                                        // Final fallback: show from beginning
                                        let start = result.unit.line.saturating_sub(1);
                                        if start < lines.len() {
                                            print_highlighted_content(
                                                file_to_read,
                                                &lines,
                                                start,
                                                max_lines,
                                                end,
                                                line_num_width,
                                            );
                                        }
                                    }
                                }
                            }
                        }
                    }
                    println!();
                }
            }
        }
    }

    Ok(())
}

/// Find the lowest existing parent directory and list its contents
fn find_existing_parent_and_list(path: &Path) -> String {
    let mut current = path.to_path_buf();

    // Walk up the path to find the first existing directory
    while !current.exists() {
        if let Some(parent) = current.parent() {
            current = parent.to_path_buf();
        } else {
            break;
        }
    }

    // If we found an existing directory, list its contents
    if current.exists() && current.is_dir() {
        let mut entries: Vec<String> = Vec::new();
        if let Ok(dir_entries) = std::fs::read_dir(&current) {
            for entry in dir_entries.take(30).flatten() {
                let name = entry.file_name().to_string_lossy().to_string();
                let is_dir = entry.file_type().map(|t| t.is_dir()).unwrap_or(false);
                if is_dir {
                    entries.push(format!("  {}/", name));
                } else {
                    entries.push(format!("  {}", name));
                }
            }
        }
        entries.sort();

        let suffix = if entries.len() >= 30 {
            "\n  ... (truncated)"
        } else {
            ""
        };

        format!(
            "Closest existing directory: {}\nContents:\n{}{}",
            current.display(),
            entries.join("\n"),
            suffix
        )
    } else {
        "Could not find any existing parent directory.".to_string()
    }
}

/// Search a single path and return results with absolute file paths
#[allow(clippy::too_many_arguments)]
fn search_single_path(
    query: &str,
    path: &PathBuf,
    top_k: usize,
    cli_model: Option<&str>,
    json: bool,
    include_patterns: &[String],
    files_only: bool,
    text_pattern: Option<&str>,
    extended_regexp: bool,
    fixed_strings: bool,
    word_regexp: bool,
    case_sensitive: bool,
    exclude_patterns: &[String],
    exclude_dirs: &[String],
    code_only: bool,
    no_fts: bool,
    alpha: Option<f32>,
    pool_factor: Option<usize>,
    auto_confirm: bool,
    static_batch: bool,
) -> Result<Vec<colgrep::SearchResult>> {
    let path = match std::fs::canonicalize(path) {
        Ok(p) => p,
        Err(_) => {
            let help = find_existing_parent_and_list(path);
            anyhow::bail!("Path does not exist: {}\n\n{}", path.display(), help);
        }
    };

    // Check if path is a file (not a directory)
    // If so, we'll use the parent directory for indexing and filter to this specific file
    let (search_path, specific_file): (PathBuf, Option<PathBuf>) = if path.is_file() {
        let parent = path
            .parent()
            .ok_or_else(|| anyhow::anyhow!("File has no parent directory: {}", path.display()))?
            .to_path_buf();
        (parent, Some(path.clone()))
    } else {
        (path.clone(), None)
    };

    // When -e is used without -F, automatically enable regex mode (ERE)
    // This makes -e imply -E by default, with -F as the opt-out
    let effective_extended_regexp = extended_regexp || (text_pattern.is_some() && !fixed_strings);

    // Resolve model: CLI > config > default
    let model = resolve_model(cli_model);

    // Load config for settings
    let config = Config::load().unwrap_or_default();

    // Resolve quantized setting from config (default: false = use FP32)
    let quantized = !config.use_fp32();

    let parallel_sessions = config.configured_parallel_sessions();
    let batch_size = config.configured_batch_size();

    // Check if index already exists for this model (suppress model output if so)
    let has_existing_index =
        index_exists(&search_path, &model) || find_parent_index(&search_path, &model)?.is_some();

    // Ensure model is downloaded (quiet if we already have an index)
    let model_path = ensure_model(Some(&model), has_existing_index)?;

    // Check for parent index (scoped to current model) unless the resolved path
    // is outside the current directory (external project)
    let parent_info = {
        let current_dir = std::env::current_dir().ok();
        let is_external_project = is_external_project_path(&search_path, current_dir.as_deref());

        if is_external_project {
            None
        } else {
            find_parent_index(&search_path, &model)?
        }
    };

    // Determine effective project root and subdirectory filter
    let (effective_root, subdir_filter): (PathBuf, Option<PathBuf>) = match &parent_info {
        Some(info) => (
            info.project_path.clone(),
            Some(info.relative_subdir.clone()),
        ),
        None => (search_path.clone(), None),
    };

    // Check if --include patterns would escape the subdirectory
    // If so, search the full project (still within the same index - effective_root)
    // This does NOT escape to a different or parent index, it only removes the subdir restriction
    let subdir_filter = if let Some(ref subdir) = subdir_filter {
        if should_search_from_root(include_patterns, subdir, &effective_root) {
            if !json && !files_only {
                eprintln!("📂 Pattern escapes subdirectory, searching full project");
            }
            None // Skip subdir filter, search full index (still bounded by effective_root)
        } else {
            Some(subdir.clone())
        }
    } else {
        None
    };

    // Get files matching include patterns (for file-type filtering)
    // BUG FIX: Don't scan filesystem for --include patterns.
    // The filesystem scan finds files that aren't in the index, causing
    // filter_by_files() to return empty results. Instead, let the code
    // fall through to filter_by_file_patterns() which queries the index directly.
    let include_files: Option<Vec<String>> = None;

    // Auto-index: try incremental update without blocking on the lock.
    // If another process is indexing, skip the update and search the existing index.
    let mut index_locked = false;
    {
        let mut builder = IndexBuilder::with_options(
            &effective_root,
            &model,
            &model_path,
            quantized,
            pool_factor,
            parallel_sessions,
            batch_size,
        )?;
        builder.set_auto_confirm(auto_confirm);
        builder.set_dynamic_batch(!static_batch);

        // Try non-blocking index update
        match builder.try_index(None, false) {
            Ok(Some(stats)) => {
                let changes = stats.added + stats.changed + stats.deleted;
                if changes > 0 && !json && !files_only {
                    if let Some(ref info) = parent_info {
                        eprintln!(
                            "📂 Using index: {} (subdir: {}): indexed {} files\n",
                            display_path(&info.project_path, false),
                            info.relative_subdir.display(),
                            changes
                        );
                    } else {
                        eprintln!(
                            "📂 Using index: {}: indexed {} files\n",
                            display_path(&effective_root, false),
                            changes
                        );
                    }
                }
            }
            Ok(None) => {
                // Lock held by another process — search existing index
                index_locked = true;
                if !json && !files_only {
                    eprintln!(
                        "📂 Index is being updated by another process, searching existing index..."
                    );
                }
            }
            Err(e) => {
                let err_str = format!("{}", e);
                let err_debug = format!("{:?}", e);
                if err_str.contains("Indexing cancelled by user") {
                    return Err(e);
                }
                if err_str.contains("No data to merge")
                    || err_debug.contains("No data to merge")
                    || err_str.contains("Index load failed")
                {
                    // Index is corrupted - clear and rebuild
                    if !json && !files_only {
                        eprintln!("⚠️  Index corrupted, rebuilding...");
                    }

                    let index_dir = get_index_dir_for_project(&effective_root, &model)?;
                    if index_dir.exists() {
                        let _lock = acquire_index_lock(&index_dir)?;
                        std::fs::remove_dir_all(&index_dir)?;
                    }

                    let mut new_builder = IndexBuilder::with_options(
                        &effective_root,
                        &model,
                        &model_path,
                        quantized,
                        pool_factor,
                        parallel_sessions,
                        batch_size,
                    )?;
                    new_builder.set_auto_confirm(auto_confirm);
                    new_builder.set_dynamic_batch(!static_batch);
                    new_builder.index(None, false)?;
                } else {
                    return Err(e);
                }
            }
        }
    }

    // Verify index exists (at least partially)
    let index_dir = get_index_dir_for_project(&effective_root, &model)?;
    let vector_index_path = get_vector_index_path(&index_dir);
    if !vector_index_path.join("metadata.json").exists() {
        if index_locked {
            // Index is being created for the first time by another process — nothing to search yet
            anyhow::bail!("colgrep index is currently being built, rely on grep for now.");
        }
        // Check if the path contains an ignored directory pattern
        if let Some(ignored_pattern) = path_contains_ignored_dir(&effective_root) {
            anyhow::bail!(
                "No files indexed. The path contains '{}' which is in the default ignore list.\n\
                 Ignored directories: tmp, temp, vendor, node_modules, target, build, dist, .git, etc.\n\
                 Try searching from a different directory or project root.",
                ignored_pattern
            );
        }
        anyhow::bail!("No index found. Index building may have failed (no indexable files found).");
    }

    // Load searcher (from parent index if applicable)
    // If loading fails while another process holds the lock, retry a few times in case
    // the failure is due to a transient mid-write state.
    // If loading fails without a concurrent updater, clear and rebuild the index.
    let load_searcher = || -> Result<Searcher> {
        match &parent_info {
            Some(info) => Searcher::load_from_index_dir_with_quantized(
                &info.index_dir,
                &model_path,
                quantized,
            ),
            None => Searcher::load_with_quantized(&effective_root, &model, &model_path, quantized),
        }
    };

    let searcher = match load_searcher() {
        Ok(s) => s,
        Err(e) if index_locked => {
            // Another process is updating the index — the load failure is likely
            // due to a transient mid-write state. Retry a few times with short delays
            // rather than blocking on the lock (the updater may run for minutes).
            if !json && !files_only {
                eprintln!("⏳ Index load failed during update, retrying...");
            }
            const MAX_RETRIES: u32 = 3;
            const RETRY_DELAY: std::time::Duration = std::time::Duration::from_millis(500);
            let mut last_err = e;
            let mut loaded = None;
            for _ in 0..MAX_RETRIES {
                std::thread::sleep(RETRY_DELAY);
                match load_searcher() {
                    Ok(s) => {
                        loaded = Some(s);
                        break;
                    }
                    Err(e) => last_err = e,
                }
            }
            match loaded {
                Some(s) => s,
                None => {
                    return Err(last_err).with_context(|| {
                        "⏳ Index load failed while another process is updating. \
                         Rely on grep until the update completes."
                    });
                }
            }
        }
        Err(e)
            if {
                let err_debug = format!("{:?}", e);
                let err_display = format!("{}", e);
                err_debug.contains("No data to merge")
                    || err_display.contains("No data to merge")
                    || err_debug.contains("IndexLoad")
                    || err_display.contains("Index load failed")
            } =>
        {
            // Index is corrupted or empty (no concurrent updater) - clear and rebuild
            if !json && !files_only {
                eprintln!("⚠️  Index corrupted, rebuilding...");
            }

            let target_index_dir = match &parent_info {
                Some(info) => &info.index_dir,
                None => &index_dir,
            };
            if target_index_dir.exists() {
                let _lock = acquire_index_lock(target_index_dir)?;
                std::fs::remove_dir_all(target_index_dir)?;
            }

            let mut builder = IndexBuilder::with_options(
                &effective_root,
                &model,
                &model_path,
                quantized,
                pool_factor,
                parallel_sessions,
                batch_size,
            )?;
            builder.set_auto_confirm(auto_confirm);
            builder.set_dynamic_batch(!static_batch);
            builder.index(None, false)?;

            load_searcher()?
        }
        Err(e) => return Err(e),
    };

    // Build subset combining subdirectory filter, text pattern filter, and include patterns
    let subset = {
        let mut combined_ids: Option<Vec<i64>> = None;

        // Apply subdirectory filter first if using parent index
        if let Some(ref subdir) = subdir_filter {
            let subdir_ids = searcher.filter_by_path_prefix(subdir)?;
            if subdir_ids.is_empty() {
                if !json && !files_only {
                    eprintln!(
                        "No indexed code units in subdirectory: {}",
                        subdir.display()
                    );
                }
                return Ok(vec![]);
            }
            combined_ids = Some(subdir_ids);
        }

        // Apply text pattern filter: search indexed code directly (much faster than grep)
        if let Some(pattern) = text_pattern {
            // Use regex-based filtering with full grep flag support:
            // -e now implies ERE by default (no need for -E flag)
            // -F (fixed_strings): literal string matching, disables regex mode
            // -w (word_regexp): whole word matching with \b boundaries
            let pattern_ids = searcher.filter_by_text_pattern_with_options(
                pattern,
                effective_extended_regexp,
                fixed_strings,
                word_regexp,
                case_sensitive,
            )?;

            if pattern_ids.is_empty() {
                if !json && !files_only {
                    eprintln!("No indexed code units contain pattern: {}", pattern);
                }
                return Ok(vec![]);
            }

            combined_ids = match combined_ids {
                Some(existing) => {
                    let existing_set: std::collections::HashSet<_> = existing.into_iter().collect();
                    Some(
                        pattern_ids
                            .into_iter()
                            .filter(|id| existing_set.contains(id))
                            .collect(),
                    )
                }
                None => Some(pattern_ids),
            };
        }

        // Apply include pattern filter (file type filtering)
        // Only use filesystem-scanned files if non-empty; otherwise fall back to index-based pattern matching
        if let Some(files) = include_files.as_ref().filter(|f| !f.is_empty()) {
            let file_ids = searcher.filter_by_files(files)?;
            combined_ids = match combined_ids {
                Some(existing) => {
                    let existing_set: std::collections::HashSet<_> = existing.into_iter().collect();
                    Some(
                        file_ids
                            .into_iter()
                            .filter(|id| existing_set.contains(id))
                            .collect(),
                    )
                }
                None => Some(file_ids),
            };
        } else if !include_patterns.is_empty() {
            let pattern_ids = searcher.filter_by_file_patterns(include_patterns)?;
            combined_ids = match combined_ids {
                Some(existing) => {
                    let existing_set: std::collections::HashSet<_> = existing.into_iter().collect();
                    Some(
                        pattern_ids
                            .into_iter()
                            .filter(|id| existing_set.contains(id))
                            .collect(),
                    )
                }
                None => Some(pattern_ids),
            };
        }

        // Apply specific file filter (when user passes a file path instead of directory)
        if let Some(ref file_path) = specific_file {
            // Convert absolute file path to relative path (relative to effective_root)
            let rel_path = file_path
                .strip_prefix(&effective_root)
                .unwrap_or(file_path)
                .to_string_lossy()
                .to_string();
            let file_ids = searcher.filter_by_files(std::slice::from_ref(&rel_path))?;
            if file_ids.is_empty() {
                if !json && !files_only {
                    eprintln!("No indexed code units in file: {}", file_path.display());
                }
                return Ok(vec![]);
            }
            combined_ids = match combined_ids {
                Some(existing) => {
                    let existing_set: std::collections::HashSet<_> = existing.into_iter().collect();
                    Some(
                        file_ids
                            .into_iter()
                            .filter(|id| existing_set.contains(id))
                            .collect(),
                    )
                }
                None => Some(file_ids),
            };
        }

        // Apply exclude pattern filter (SQL-based: returns IDs that DON'T match patterns)
        if !exclude_patterns.is_empty() {
            let included_ids = searcher.filter_exclude_by_patterns(exclude_patterns)?;
            let included_set: std::collections::HashSet<_> = included_ids.into_iter().collect();
            combined_ids = match combined_ids {
                Some(existing) => Some(
                    existing
                        .into_iter()
                        .filter(|id| included_set.contains(id))
                        .collect(),
                ),
                None => Some(included_set.into_iter().collect()),
            };
        }

        // Apply exclude-dir filter (SQL-based: returns IDs NOT in excluded directories)
        if !exclude_dirs.is_empty() {
            let included_ids = searcher.filter_exclude_by_dirs(exclude_dirs)?;
            let included_set: std::collections::HashSet<_> = included_ids.into_iter().collect();
            combined_ids = match combined_ids {
                Some(existing) => Some(
                    existing
                        .into_iter()
                        .filter(|id| included_set.contains(id))
                        .collect(),
                ),
                None => Some(included_set.into_iter().collect()),
            };
        }

        // Check if subset is empty after combining
        if let Some(ref ids) = combined_ids {
            if ids.is_empty() {
                if !json && !files_only {
                    eprintln!("No indexed code units match the specified filters");
                }
                return Ok(vec![]);
            }
        }

        combined_ids
    };

    // Search with optional filtering
    // Request more results to allow for re-ranking with query boost and test function demotion
    let search_top_k = if code_only { top_k * 4 } else { top_k * 3 };

    // Resolve hybrid search: --semantic-only CLI flag overrides, then config, default is enabled
    let config = colgrep::Config::load().unwrap_or_default();
    let hybrid_disabled = if no_fts {
        true
    } else {
        !config.use_hybrid_search()
    };

    // CLI --alpha overrides config, config overrides default (0.55)
    let hybrid_alpha = alpha.unwrap_or_else(|| config.get_hybrid_alpha());

    // When no -e flag is provided, run BOTH semantic/hybrid search and text-pattern search
    // This ensures exact matches are found even if the vector database doesn't rank them highly
    let results = if let Some(pattern) = &text_pattern {
        // -e flag provided: use existing hybrid search logic
        // Enhance semantic query with -e pattern (strip regex metacharacters and dedupe tokens)
        let sanitized_pattern = strip_regex_for_semantic(pattern);
        let enhanced_query = merge_query_with_pattern(query, &sanitized_pattern);
        if hybrid_disabled {
            searcher.search(&enhanced_query, search_top_k, subset.as_deref())?
        } else {
            searcher.search_hybrid(
                &enhanced_query,
                search_top_k,
                subset.as_deref(),
                hybrid_alpha,
            )?
        }
    } else {
        // Encode query once and reuse across both searches
        let query_emb = searcher.encode_query(query)?;

        // Run FTS5 once and reuse across both searches
        let fts5_results = if hybrid_disabled {
            None
        } else {
            searcher.fts5_search(query, search_top_k * 3, subset.as_deref())
        };

        // 1. Run semantic search (with FTS5 fusion if enabled)
        let semantic_results = if hybrid_disabled {
            searcher.search_with_embedding(&query_emb, search_top_k, subset.as_deref())?
        } else {
            searcher.search_hybrid_with_embedding(
                &query_emb,
                query,
                search_top_k,
                subset.as_deref(),
                hybrid_alpha,
                fts5_results.as_ref(),
            )?
        };

        // 2. Run hybrid search: filter by query text, then semantic rank
        // Use fixed_strings mode to treat the query as a literal pattern.
        // The semantic-side query is *always* case-insensitive — ColBERT
        // embeddings handle case fuzzily and we want broad recall here.
        let text_filtered_ids =
            searcher.filter_by_text_pattern_with_options(query, false, true, false, false)?;

        let hybrid_results = if !text_filtered_ids.is_empty() {
            // Intersect with existing subset if any
            let hybrid_subset: Vec<i64> = match &subset {
                Some(existing) => {
                    let existing_set: std::collections::HashSet<_> =
                        existing.iter().copied().collect();
                    text_filtered_ids
                        .into_iter()
                        .filter(|id| existing_set.contains(id))
                        .collect()
                }
                None => text_filtered_ids,
            };

            if !hybrid_subset.is_empty() {
                // Reuse cached embedding and FTS5 results (filtered to subset)
                if hybrid_disabled {
                    searcher.search_with_embedding(
                        &query_emb,
                        search_top_k,
                        Some(&hybrid_subset),
                    )?
                } else {
                    // Pass `None` so FTS5 is refetched *within* the subset.
                    // Reusing the global `fts5_results` here would carry
                    // BM25 hits from outside the subset; they'd get filtered
                    // down to a tiny intersection, hurting recall.
                    searcher.search_hybrid_with_embedding(
                        &query_emb,
                        query,
                        search_top_k,
                        Some(&hybrid_subset),
                        hybrid_alpha,
                        None,
                    )?
                }
            } else {
                vec![]
            }
        } else {
            vec![]
        };

        // 3. Merge results: one entry per file, span covers every matched
        //    unit, score is the max across both calls.
        //
        // The previous `(file, line)` dedup was buggy: both
        // `search_hybrid_with_embedding` calls run `collapse_by_file`
        // internally, which sets `unit.line = min(line_i)` *across that
        // call's candidate pool*. Two pools → two different mins for the
        // same file → same file occupied two top-K slots.
        use std::collections::hash_map::Entry;
        let mut merged: HashMap<PathBuf, colgrep::SearchResult> = HashMap::new();
        for result in semantic_results.into_iter().chain(hybrid_results) {
            let key = result.unit.file.clone();
            match merged.entry(key) {
                Entry::Occupied(mut e) => {
                    let existing = e.get_mut();
                    let new_start = existing.unit.line.min(result.unit.line);
                    let new_end = existing.unit.end_line.max(result.unit.end_line);
                    if result.score > existing.score {
                        *existing = result;
                    }
                    existing.unit.line = new_start;
                    existing.unit.end_line = new_end;
                }
                Entry::Vacant(e) => {
                    e.insert(result);
                }
            }
        }
        merged.into_values().collect::<Vec<_>>()
    };

    // Note: When -e is used, results are already filtered to units containing the pattern
    // via filter_by_text_pattern_with_options() above, which supports -E, -F, -w flags.
    //
    // The legacy `compute_final_score` test-name demotion was removed; the
    // hybrid pipeline now applies `ranking::file_path_penalty` (a much more
    // complete language-aware test/bench/example/compat penalty) inside
    // `Searcher::search_hybrid_with_embedding`.
    let mut results: Vec<_> = results;
    results.sort_by(|a, b| {
        b.score
            .partial_cmp(&a.score)
            .unwrap_or(std::cmp::Ordering::Equal)
    });

    // Increment search count
    let index_dir = get_index_dir_for_project(&effective_root, &model)?;
    if let Ok(mut state) = IndexState::load(&index_dir) {
        state.increment_search_count();
        let _ = state.save(&index_dir);
    }

    // Convert file paths to absolute for proper display when merging results from multiple paths
    let results: Vec<colgrep::SearchResult> = results
        .into_iter()
        .map(|mut r| {
            if !r.unit.file.is_absolute() {
                r.unit.file = effective_root.join(&r.unit.file);
            }
            r
        })
        .collect();

    Ok(results)
}

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

    // Test resolve_top_k function
    #[test]
    fn test_resolve_top_k_cli_provided() {
        // CLI value should take precedence
        assert_eq!(resolve_top_k(Some(30), 15), 30);
        assert_eq!(resolve_top_k(Some(1), 20), 1);
        assert_eq!(resolve_top_k(Some(100), 15), 100);
    }

    #[test]
    fn test_resolve_top_k_fallback_to_default() {
        // When CLI not provided and no config, should use default
        // Note: This test may be affected by actual config file
        let result = resolve_top_k(None, 15);
        // Should be either 25 (default) or whatever is in config
        assert!(result > 0);
    }

    // Test resolve_context_lines function
    #[test]
    fn test_resolve_context_lines_cli_provided() {
        // CLI value should take precedence
        assert_eq!(resolve_context_lines(Some(10), 20), 10);
        assert_eq!(resolve_context_lines(Some(0), 20), 0);
        assert_eq!(resolve_context_lines(Some(30), 20), 30);
    }

    #[test]
    fn test_resolve_context_lines_fallback_to_default() {
        // When CLI not provided and no config, should use default
        let result = resolve_context_lines(None, 20);
        // Should be either 20 (default) or whatever is in config
        assert!(result <= 100); // sanity check
    }

    // Test strip_regex_for_semantic function
    #[test]
    fn test_strip_regex_basic_patterns() {
        // Character classes should be stripped, leaving meaningful text
        assert_eq!(strip_regex_for_semantic(r"fn\s+\w+"), "fn");
        assert_eq!(strip_regex_for_semantic(r"async\s+fn"), "async fn");
        assert_eq!(strip_regex_for_semantic(r"\btest\b"), "test");
    }

    #[test]
    fn test_strip_regex_quantifiers() {
        // Quantifiers should be stripped
        assert_eq!(strip_regex_for_semantic("foo+"), "foo");
        assert_eq!(strip_regex_for_semantic("bar*"), "bar");
        assert_eq!(strip_regex_for_semantic("baz?"), "baz");
        assert_eq!(strip_regex_for_semantic("qux{2,5}"), "qux");
    }

    #[test]
    fn test_strip_regex_alternation() {
        // Alternation should become space-separated
        assert_eq!(strip_regex_for_semantic("foo|bar"), "foo bar");
        assert_eq!(strip_regex_for_semantic("a|b|c"), "a b c");
    }

    #[test]
    fn test_strip_regex_anchors() {
        // Anchors should be stripped
        assert_eq!(strip_regex_for_semantic("^start"), "start");
        assert_eq!(strip_regex_for_semantic("end$"), "end");
        assert_eq!(strip_regex_for_semantic("^both$"), "both");
    }

    #[test]
    fn test_strip_regex_character_classes() {
        // Character class brackets should be stripped entirely
        assert_eq!(strip_regex_for_semantic("[abc]"), "");
        assert_eq!(strip_regex_for_semantic("pre[abc]post"), "prepost");
        assert_eq!(strip_regex_for_semantic("[a-z]+"), "");
    }

    #[test]
    fn test_strip_regex_groups() {
        // Grouping parens should be stripped but contents kept
        assert_eq!(strip_regex_for_semantic("(foo)"), "foo");
        assert_eq!(strip_regex_for_semantic("(foo)(bar)"), "foobar");
    }

    #[test]
    fn test_strip_regex_escaped_literals() {
        // Escaped metacharacters should become literals
        assert_eq!(strip_regex_for_semantic(r"foo\.bar"), "foo.bar");
        assert_eq!(strip_regex_for_semantic(r"a\*b"), "a*b");
        assert_eq!(strip_regex_for_semantic(r"Result\<T\>"), "Result<T>");
    }

    #[test]
    fn test_strip_regex_dots() {
        // Dots (any char) should be stripped
        assert_eq!(strip_regex_for_semantic("a.b"), "ab");
        assert_eq!(strip_regex_for_semantic("Result<.*>"), "Result<>");
    }

    #[test]
    fn test_strip_regex_plain_text() {
        // Plain text should pass through unchanged
        assert_eq!(strip_regex_for_semantic("hello"), "hello");
        assert_eq!(strip_regex_for_semantic("hello world"), "hello world");
        assert_eq!(strip_regex_for_semantic("foo_bar"), "foo_bar");
    }

    #[test]
    fn test_strip_regex_complex_patterns() {
        // Complex real-world patterns
        assert_eq!(strip_regex_for_semantic(r"impl\s+\w+\s+for"), "impl for");
        assert_eq!(strip_regex_for_semantic(r"fn\s+test_\w+"), "fn test_");
        assert_eq!(
            strip_regex_for_semantic(r"pub\s+(async\s+)?fn"),
            "pub async fn"
        );
    }

    #[test]
    fn test_strip_regex_empty_result() {
        // Patterns that result in empty string
        assert_eq!(strip_regex_for_semantic(r"\s+"), "");
        assert_eq!(strip_regex_for_semantic(r"\w+"), "");
        assert_eq!(strip_regex_for_semantic(r".*"), "");
        assert_eq!(strip_regex_for_semantic(r"[a-z]+"), "");
    }

    // Test merge_query_with_pattern function
    #[test]
    fn test_merge_query_no_duplicates() {
        // No duplicates - all pattern tokens added
        assert_eq!(
            merge_query_with_pattern("error handling", "Result"),
            "error handling Result"
        );
        assert_eq!(
            merge_query_with_pattern("function", "async fn"),
            "function async fn"
        );
    }

    #[test]
    fn test_merge_query_with_duplicates() {
        // Duplicates should be removed (case-insensitive)
        assert_eq!(
            merge_query_with_pattern("async function", "async fn"),
            "async function fn"
        );
        assert_eq!(
            merge_query_with_pattern("error handling", "error"),
            "error handling"
        );
        assert_eq!(
            merge_query_with_pattern("Error Handling", "error handling"),
            "Error Handling"
        );
    }

    #[test]
    fn test_merge_query_all_duplicates() {
        // All pattern tokens are duplicates - just return query
        assert_eq!(merge_query_with_pattern("foo bar", "foo bar"), "foo bar");
        assert_eq!(merge_query_with_pattern("FOO BAR", "foo bar"), "FOO BAR");
    }

    #[test]
    fn test_merge_query_empty_pattern() {
        // Empty pattern - just return query
        assert_eq!(merge_query_with_pattern("query", ""), "query");
    }

    #[test]
    fn test_merge_query_partial_duplicates() {
        // Mix of duplicate and new tokens
        assert_eq!(
            merge_query_with_pattern("impl trait", "impl for"),
            "impl trait for"
        );
        assert_eq!(
            merge_query_with_pattern("pub fn", "pub async fn test"),
            "pub fn async test"
        );
    }

    #[cfg(windows)]
    #[test]
    fn test_display_path_uses_relative_output_for_verbatim_windows_paths() {
        let temp_dir = tempdir().unwrap();
        let canonical_root = std::fs::canonicalize(temp_dir.path()).unwrap();
        let search_result_path = canonical_root.join("src").join("main.rs");
        // Pass the canonical root as cwd so both sides have the same path form
        let display = display_path_with_cwd(&search_result_path, Some(&canonical_root), true);

        assert_eq!(
            display,
            PathBuf::from("src").join("main.rs").display().to_string()
        );
    }

    #[cfg(windows)]
    #[test]
    fn test_display_path_strips_verbatim_windows_prefix_in_absolute_mode() {
        let temp_dir = tempdir().unwrap();
        let canonical_root = std::fs::canonicalize(temp_dir.path()).unwrap();
        let search_result_path = canonical_root.join("src").join("main.rs");

        let display = display_path(&search_result_path, false);

        assert!(!display.starts_with(r"\\?\"));
        assert!(display.ends_with(r"src\main.rs"));
    }

    #[cfg(windows)]
    #[test]
    fn test_is_external_project_path_ignores_windows_verbatim_prefixes() {
        let temp_dir = tempdir().unwrap();
        let canonical_root = std::fs::canonicalize(temp_dir.path()).unwrap();
        // Both paths canonical so the only difference is the verbatim prefix
        assert!(!is_external_project_path(
            &canonical_root,
            Some(&canonical_root)
        ));
    }

    #[cfg(windows)]
    #[test]
    fn test_normalize_windows_path_strips_verbatim_unc_prefix() {
        let raw_path = PathBuf::from(r"\\?\UNC\server\share\repo\src\main.rs");

        assert_eq!(
            normalize_windows_path(&raw_path),
            PathBuf::from(r"\\server\share\repo\src\main.rs")
        );
    }
}