agcodex-core 0.1.0

Core business logic with AST-RAG engine and tree-sitter integration
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
//! Tantivy-based indexing tool for AGCodex
//!
//! This module provides a comprehensive search indexing system using Tantivy
//! for fast, sophisticated codebase search. The IndexTool supports:
//!
//! - Full-text search with language-aware analysis
//! - Symbol-based semantic search (functions, classes, variables)
//! - Incremental updates for efficient re-indexing
//! - Location-aware results with precise file:line:column metadata
//! - Multi-language support via tree-sitter integration
//!
//! ## Architecture
//!
//! ```text
//! ┌─────────────────┐    ┌─────────────────┐    ┌─────────────────┐
//! │   IndexTool     │───▶│  Tantivy Index  │───▶│  Search Results │
//! │                 │    │                 │    │                 │
//! │ • build()       │    │ Schema:         │    │ • path          │
//! │ • update()      │    │ • path          │    │ • content       │
//! │ • optimize()    │    │ • content       │    │ • symbols       │
//! │ • stats()       │    │ • symbols       │    │ • language      │
//! │ • search()      │    │ • language      │    │ • location      │
//! └─────────────────┘    └─────────────────┘    └─────────────────┘
//! ```

use super::InternalTool;
use super::ToolMetadata;
use super::output::CacheStats;
use super::output::ComprehensiveToolOutput;
use super::output::CpuUsage;
use super::output::IoStats;
use super::output::MemoryUsage;
use super::output::OutputBuilder;
use super::output::PerformanceMetrics;
use agcodex_ast::SourceLocation;

// use regex::Regex; // Will implement regex-based symbol extraction when regex crate is available
use serde::Deserialize;
use serde::Serialize;
use std::collections::HashMap;
use std::collections::HashSet;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::RwLock;
use std::time::SystemTime;
use std::time::UNIX_EPOCH;
use thiserror::Error;
use tracing::debug;
use tracing::error;
use tracing::info;
use tracing::instrument;
use walkdir::WalkDir;

use tantivy::Index;
use tantivy::IndexReader;
use tantivy::IndexWriter;
use tantivy::ReloadPolicy;
use tantivy::Searcher;
use tantivy::TantivyError;
use tantivy::Term;
use tantivy::collector::TopDocs;
use tantivy::doc;
use tantivy::query::QueryParser;
use tantivy::schema::*;

/// Errors specific to the indexing tool
#[derive(Error, Debug)]
pub enum IndexError {
    #[error("tantivy index error: {0}")]
    Tantivy(#[from] TantivyError),

    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    #[error("invalid index path: {path}")]
    InvalidPath { path: PathBuf },

    #[error("index not initialized: {0}")]
    NotInitialized(String),

    #[error("concurrent access error: {0}")]
    ConcurrentAccess(String),

    #[error("document not found: {path}")]
    DocumentNotFound { path: PathBuf },

    #[error("query parsing error: {query}: {source}")]
    QueryParsing {
        query: String,
        source: Box<dyn std::error::Error + Send + Sync>,
    },

    #[error("schema error: {0}")]
    Schema(String),

    #[error("indexing operation failed: {operation}: {reason}")]
    OperationFailed { operation: String, reason: String },

    #[error("incremental update failed: {0}")]
    IncrementalUpdateFailed(String),

    #[error("optimization failed: {0}")]
    OptimizationFailed(String),

    #[error("statistics calculation failed: {0}")]
    StatsFailed(String),
}

/// Conversion from walkdir::Error to IndexError
impl From<walkdir::Error> for IndexError {
    fn from(err: walkdir::Error) -> Self {
        IndexError::Io(std::io::Error::other(err.to_string()))
    }
}

/// Result type for index operations
pub type IndexResult<T> = std::result::Result<T, IndexError>;

/// Configuration for the indexing tool
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IndexConfig {
    /// Directory to store the index
    pub index_path: PathBuf,

    /// File extensions to include in indexing
    pub include_extensions: Vec<String>,

    /// Maximum file size to index (in bytes)
    pub max_file_size: usize,

    /// Whether to enable incremental updates
    pub incremental: bool,

    /// Writer memory budget (in MB)
    pub writer_memory_mb: usize,

    /// Number of threads for indexing
    pub num_threads: Option<usize>,

    /// Merge policy settings
    pub merge_policy: MergePolicyConfig,
}

impl Default for IndexConfig {
    fn default() -> Self {
        Self {
            index_path: PathBuf::from(".agcodex/index"),
            include_extensions: vec![
                "rs".to_string(),
                "py".to_string(),
                "js".to_string(),
                "ts".to_string(),
                "tsx".to_string(),
                "jsx".to_string(),
                "go".to_string(),
                "java".to_string(),
                "c".to_string(),
                "cpp".to_string(),
                "h".to_string(),
                "hpp".to_string(),
                "cs".to_string(),
                "php".to_string(),
                "rb".to_string(),
                "swift".to_string(),
                "kt".to_string(),
                "scala".to_string(),
                "hs".to_string(),
                "ex".to_string(),
                "exs".to_string(),
                "clj".to_string(),
                "cljs".to_string(),
                "lua".to_string(),
                "sh".to_string(),
                "bash".to_string(),
                "zsh".to_string(),
                "fish".to_string(),
                "ps1".to_string(),
                "bat".to_string(),
                "dockerfile".to_string(),
                "yaml".to_string(),
                "yml".to_string(),
                "json".to_string(),
                "toml".to_string(),
                "xml".to_string(),
                "html".to_string(),
                "css".to_string(),
                "scss".to_string(),
                "md".to_string(),
                "txt".to_string(),
            ],
            max_file_size: 10 * 1024 * 1024, // 10MB
            incremental: true,
            writer_memory_mb: 256,
            num_threads: None,
            merge_policy: MergePolicyConfig::default(),
        }
    }
}

/// Merge policy configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MergePolicyConfig {
    pub max_merge_at_once: usize,
    pub max_merge_segment_size_mb: usize,
    pub level_log_size: f64,
}

impl Default for MergePolicyConfig {
    fn default() -> Self {
        Self {
            max_merge_at_once: 10,
            max_merge_segment_size_mb: 1024, // 1GB
            level_log_size: 0.75,
        }
    }
}

/// Document in the search index
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IndexedDocument {
    /// Relative path from workspace root
    pub path: String,

    /// Full text content of the file
    pub content: String,

    /// Extracted symbols (functions, classes, variables)
    pub symbols: Vec<Symbol>,

    /// Programming language
    pub language: String,

    /// File size in bytes
    pub size: u64,

    /// Last modified timestamp
    pub modified: u64,

    /// Content hash for change detection
    pub hash: String,
}

/// Symbol information extracted from code
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Symbol {
    /// Symbol name
    pub name: String,

    /// Symbol type (function, class, variable, etc.)
    pub symbol_type: String,

    /// Line number (1-based)
    pub line: u32,

    /// Column number (1-based)  
    pub column: u32,

    /// End line number (1-based)
    pub end_line: u32,

    /// End column number (1-based)
    pub end_column: u32,

    /// Optional documentation/comments
    pub documentation: Option<String>,

    /// Visibility (public, private, protected)
    pub visibility: Option<String>,

    /// Parent scope (for nested symbols)
    pub parent: Option<String>,
}

/// Search result from the index with compression support
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchResult {
    /// Document that matched
    pub document: IndexedDocument,

    /// Search score
    pub score: f32,

    /// Highlighted text snippets
    pub snippets: Vec<String>,

    /// Matching symbols
    pub matching_symbols: Vec<Symbol>,

    /// Relevance score (0.0-1.0) combining search score with semantic factors
    pub relevance_score: f32,

    /// Compressed context summary with signatures and key information
    pub context_summary: String,

    /// Token count after compression
    pub token_count: usize,

    /// Original token count before compression
    pub original_token_count: Option<usize>,

    /// Compression ratio (compressed/original)
    pub compression_ratio: Option<f32>,

    /// Number of similar results that were merged into this one
    pub similar_count: u32,

    /// Result group identifier for deduplication
    pub group_id: Option<String>,
}

/// Statistics about the index
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IndexStats {
    /// Total number of documents
    pub document_count: u64,

    /// Total number of terms
    pub term_count: u64,

    /// Index size on disk (bytes)
    pub size_bytes: u64,

    /// Number of segments
    pub segment_count: usize,

    /// Last update timestamp
    pub last_updated: u64,

    /// Average document size
    pub avg_document_size: f64,

    /// Language distribution
    pub language_stats: HashMap<String, u64>,

    /// Symbol type distribution
    pub symbol_stats: HashMap<String, u64>,
}

/// Search query parameters
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchQuery {
    /// Main search text
    pub query: String,

    /// Language filter
    pub language: Option<String>,

    /// Path filter (glob pattern)
    pub path_filter: Option<String>,

    /// Symbol type filter
    pub symbol_type: Option<String>,

    /// Maximum results to return
    pub limit: Option<usize>,

    /// Enable fuzzy matching
    pub fuzzy: bool,

    /// Minimum score threshold
    pub min_score: Option<f32>,
}

/// Input for build operation
#[derive(Debug, Clone)]
pub struct BuildInput {
    pub directory: PathBuf,
    pub config: IndexConfig,
    pub force_rebuild: bool,
}

/// Input for update operation
#[derive(Debug, Clone)]
pub struct UpdateInput {
    pub files: Vec<PathBuf>,
    pub config: IndexConfig,
}

/// Input for search operation
#[derive(Debug, Clone)]
pub struct SearchInput {
    pub query: SearchQuery,
    pub config: IndexConfig,
}

/// Main indexing tool implementation
pub struct IndexTool {
    /// Tantivy index
    index: Arc<RwLock<Option<Index>>>,

    /// Index writer
    writer: Arc<Mutex<Option<IndexWriter>>>,

    /// Index reader
    reader: Arc<RwLock<Option<IndexReader>>>,

    /// Current configuration
    config: Arc<RwLock<IndexConfig>>,

    /// Schema definition
    schema: Arc<Schema>,

    /// Field handles for the schema
    fields: Arc<IndexFields>,
}

/// Schema field handles
#[derive(Debug)]
struct IndexFields {
    path: Field,
    content: Field,
    symbols: Field,
    language: Field,
    size: Field,
    modified: Field,
    hash: Field,
    symbol_names: Field,
    symbol_types: Field,
    symbol_docs: Field,
}

impl IndexTool {
    /// Create a new IndexTool with the given configuration
    pub fn new(config: IndexConfig) -> IndexResult<Self> {
        let schema = Self::build_schema();
        let fields = Arc::new(Self::extract_fields(&schema)?);

        Ok(Self {
            index: Arc::new(RwLock::new(None)),
            writer: Arc::new(Mutex::new(None)),
            reader: Arc::new(RwLock::new(None)),
            config: Arc::new(RwLock::new(config)),
            schema: Arc::new(schema),
            fields,
        })
    }

    /// Estimate token count from text (~4 chars per token)
    const fn estimate_tokens(text: &str) -> usize {
        text.len() / 4 // ~4 chars per token
    }

    /// Compress content using AST-aware extraction while preserving key information
    fn compress_content(
        &self,
        content: &str,
        language: &str,
        path: &str,
    ) -> (String, usize, usize) {
        let original_tokens = Self::estimate_tokens(content);

        // Extract only signatures, remove implementation bodies
        let compressed = self.extract_signatures(content, language, path);
        let compressed_tokens = Self::estimate_tokens(&compressed);

        (compressed, original_tokens, compressed_tokens)
    }

    /// Extract signatures and key definitions while removing implementation details
    fn extract_signatures(&self, content: &str, language: &str, path: &str) -> String {
        let lines: Vec<&str> = content.lines().collect();
        let mut signatures = Vec::new();

        // Add file header
        signatures.push(format!("// {}: {} ({} lines)", path, language, lines.len()));

        match language {
            "rust" => self.extract_rust_signatures(&lines, &mut signatures),
            "python" => self.extract_python_signatures(&lines, &mut signatures),
            "javascript" | "typescript" => self.extract_js_signatures(&lines, &mut signatures),
            "java" => self.extract_java_signatures(&lines, &mut signatures),
            "go" => self.extract_go_signatures(&lines, &mut signatures),
            "c" | "cpp" => self.extract_c_signatures(&lines, &mut signatures),
            _ => self.extract_generic_signatures(&lines, &mut signatures),
        }

        signatures.join("\n")
    }

    /// Extract Rust function/struct signatures without bodies
    fn extract_rust_signatures(&self, lines: &[&str], output: &mut Vec<String>) {
        for (i, line) in lines.iter().enumerate() {
            let trimmed = line.trim();

            if trimmed.starts_with("fn ") || trimmed.starts_with("pub fn ") {
                // Show full signature
                if let Some(sig_end) = line.find('{') {
                    output.push(format!("L{}: {}{{", i + 1, &line[..sig_end].trim()));
                } else {
                    output.push(format!("L{}: {}", i + 1, line.trim()));
                }
            } else if trimmed.starts_with("struct ") || trimmed.starts_with("pub struct ") {
                output.push(format!("L{}: {}", i + 1, line.trim()));
            } else if trimmed.starts_with("impl ") {
                output.push(format!("L{}: {}", i + 1, line.trim()));
            } else if trimmed.starts_with("///") || trimmed.starts_with("//!") {
                // Keep doc comments
                output.push(format!("L{}: {}", i + 1, line.trim()));
            }
        }
    }

    /// Extract Python function/class definitions
    fn extract_python_signatures(&self, lines: &[&str], output: &mut Vec<String>) {
        for (i, line) in lines.iter().enumerate() {
            let trimmed = line.trim();

            if trimmed.starts_with("def ") {
                // Show function signature with just ":" instead of full body
                output.push(format!("L{}: {}:", i + 1, trimmed.trim_end_matches(':')));
            } else if trimmed.starts_with("class ") {
                output.push(format!("L{}: {}:", i + 1, trimmed.trim_end_matches(':')));
            } else if trimmed.starts_with("import ") || trimmed.starts_with("from ") {
                output.push(format!("L{}: {}", i + 1, trimmed));
            }
        }
    }

    /// Extract JavaScript/TypeScript function signatures
    fn extract_js_signatures(&self, lines: &[&str], output: &mut Vec<String>) {
        for (i, line) in lines.iter().enumerate() {
            let trimmed = line.trim();

            if trimmed.starts_with("function ") {
                if let Some(brace) = line.find('{') {
                    output.push(format!("L{}: {}{{", i + 1, &line[..brace].trim()));
                } else {
                    output.push(format!("L{}: {}", i + 1, trimmed));
                }
            } else if trimmed.starts_with("export ")
                || trimmed.starts_with("const ")
                || trimmed.starts_with("let ")
                || trimmed.starts_with("class ")
            {
                output.push(format!("L{}: {}", i + 1, trimmed));
            }
        }
    }

    /// Extract Java method/class signatures
    fn extract_java_signatures(&self, lines: &[&str], output: &mut Vec<String>) {
        for (i, line) in lines.iter().enumerate() {
            let trimmed = line.trim();

            if (trimmed.starts_with("public ")
                || trimmed.starts_with("private ")
                || trimmed.starts_with("protected "))
                && (trimmed.contains("(") && trimmed.contains(")"))
            {
                // Method signature
                if let Some(brace) = line.find('{') {
                    output.push(format!("L{}: {}{{", i + 1, &line[..brace].trim()));
                } else {
                    output.push(format!("L{}: {}", i + 1, trimmed));
                }
            } else if trimmed.starts_with("class ") || trimmed.starts_with("interface ") {
                output.push(format!("L{}: {}", i + 1, trimmed));
            }
        }
    }

    /// Extract Go function/type signatures
    fn extract_go_signatures(&self, lines: &[&str], output: &mut Vec<String>) {
        for (i, line) in lines.iter().enumerate() {
            let trimmed = line.trim();

            if trimmed.starts_with("func ") {
                if let Some(brace) = line.find('{') {
                    output.push(format!("L{}: {}{{", i + 1, &line[..brace].trim()));
                } else {
                    output.push(format!("L{}: {}", i + 1, trimmed));
                }
            } else if trimmed.starts_with("type ")
                || trimmed.starts_with("var ")
                || trimmed.starts_with("const ")
            {
                output.push(format!("L{}: {}", i + 1, trimmed));
            }
        }
    }

    /// Extract C/C++ function/struct signatures
    fn extract_c_signatures(&self, lines: &[&str], output: &mut Vec<String>) {
        for (i, line) in lines.iter().enumerate() {
            let trimmed = line.trim();

            if trimmed.starts_with("#include") || trimmed.starts_with("#define") {
                output.push(format!("L{}: {}", i + 1, trimmed));
            } else if trimmed.contains('(')
                && trimmed.contains(')')
                && !trimmed.starts_with("//")
                && (trimmed.contains("int ")
                    || trimmed.contains("void ")
                    || trimmed.contains("char ")
                    || trimmed.contains("float "))
            {
                // Likely function signature
                output.push(format!("L{}: {}", i + 1, trimmed));
            } else if trimmed.starts_with("struct ") || trimmed.starts_with("typedef") {
                output.push(format!("L{}: {}", i + 1, trimmed));
            }
        }
    }

    /// Generic signature extraction for unknown languages
    fn extract_generic_signatures(&self, lines: &[&str], output: &mut Vec<String>) {
        for (i, line) in lines.iter().enumerate() {
            let trimmed = line.trim();

            // Simple heuristics for common patterns
            if (trimmed.contains("function")
                || trimmed.contains("def ")
                || trimmed.contains("class "))
                && trimmed.len() < 100
            {
                output.push(format!("L{}: {}", i + 1, trimmed));
            }
        }
    }

    /// Build the Tantivy schema for code indexing
    fn build_schema() -> Schema {
        let mut schema_builder = Schema::builder();

        // File path (unique identifier)
        schema_builder.add_text_field("path", STRING | STORED | FAST);

        // File content (full-text searchable)
        schema_builder.add_text_field("content", TEXT | STORED);

        // Serialized symbols
        schema_builder.add_text_field("symbols", STORED);

        // Programming language
        schema_builder.add_text_field("language", STRING | STORED | FAST);

        // File size
        schema_builder.add_u64_field("size", STORED | INDEXED);

        // Last modified timestamp
        schema_builder.add_u64_field("modified", STORED | INDEXED);

        // Content hash
        schema_builder.add_text_field("hash", STRING | STORED);

        // Symbol names (searchable)
        schema_builder.add_text_field("symbol_names", TEXT | STORED);

        // Symbol types (filterable)
        schema_builder.add_text_field("symbol_types", STRING | STORED | FAST);

        // Symbol documentation (searchable)
        schema_builder.add_text_field("symbol_docs", TEXT | STORED);

        schema_builder.build()
    }

    /// Extract field handles from schema
    fn extract_fields(schema: &Schema) -> IndexResult<IndexFields> {
        Ok(IndexFields {
            path: schema
                .get_field("path")
                .map_err(|e| IndexError::Schema(format!("Missing path field: {}", e)))?,
            content: schema
                .get_field("content")
                .map_err(|e| IndexError::Schema(format!("Missing content field: {}", e)))?,
            symbols: schema
                .get_field("symbols")
                .map_err(|e| IndexError::Schema(format!("Missing symbols field: {}", e)))?,
            language: schema
                .get_field("language")
                .map_err(|e| IndexError::Schema(format!("Missing language field: {}", e)))?,
            size: schema
                .get_field("size")
                .map_err(|e| IndexError::Schema(format!("Missing size field: {}", e)))?,
            modified: schema
                .get_field("modified")
                .map_err(|e| IndexError::Schema(format!("Missing modified field: {}", e)))?,
            hash: schema
                .get_field("hash")
                .map_err(|e| IndexError::Schema(format!("Missing hash field: {}", e)))?,
            symbol_names: schema
                .get_field("symbol_names")
                .map_err(|e| IndexError::Schema(format!("Missing symbol_names field: {}", e)))?,
            symbol_types: schema
                .get_field("symbol_types")
                .map_err(|e| IndexError::Schema(format!("Missing symbol_types field: {}", e)))?,
            symbol_docs: schema
                .get_field("symbol_docs")
                .map_err(|e| IndexError::Schema(format!("Missing symbol_docs field: {}", e)))?,
        })
    }

    /// Initialize the index
    #[instrument(skip(self))]
    pub async fn initialize(&self) -> IndexResult<()> {
        let config = self.config.read().unwrap().clone();

        // Create index directory
        if !config.index_path.exists() {
            fs::create_dir_all(&config.index_path)?;
        }

        // Open or create index
        let index = if config.index_path.join("meta.json").exists() {
            info!("Opening existing index at {:?}", config.index_path);
            Index::open_in_dir(&config.index_path)?
        } else {
            info!("Creating new index at {:?}", config.index_path);
            Index::create_in_dir(&config.index_path, self.schema.as_ref().clone())?
        };

        // Create writer with memory budget
        let writer = index.writer(config.writer_memory_mb * 1_000_000)?;

        // Create reader with reload policy
        let reader = index
            .reader_builder()
            .reload_policy(ReloadPolicy::OnCommitWithDelay)
            .try_into()?;

        // Store components
        {
            let mut index_lock = self.index.write().unwrap();
            *index_lock = Some(index);
        }

        {
            let mut writer_lock = self.writer.lock().unwrap();
            *writer_lock = Some(writer);
        }

        {
            let mut reader_lock = self.reader.write().unwrap();
            *reader_lock = Some(reader);
        }

        info!("Index initialized successfully");
        Ok(())
    }

    /// Build index for a directory
    #[instrument(skip(self, input))]
    pub async fn build(
        &self,
        input: BuildInput,
    ) -> IndexResult<ComprehensiveToolOutput<IndexStats>> {
        info!("Building index for directory: {:?}", input.directory);
        let start_time = std::time::Instant::now();
        let location = SourceLocation::new(
            input.directory.to_string_lossy().as_ref(),
            0,
            0,
            0,
            0,
            (0, 0),
        );

        // Update configuration
        {
            let mut config_lock = self.config.write().unwrap();
            *config_lock = input.config.clone();
        }

        // Initialize if not already done
        if self.index.read().unwrap().is_none() {
            self.initialize().await?;
        }

        // Clear existing index if force rebuild
        if input.force_rebuild {
            self.clear_index().await?;
        }

        // Collect all files to index
        let files = self.collect_files(&input.directory).await?;
        let file_count = files.len();
        info!("Found {} files to index", file_count);

        // Index files in batches
        let batch_size = 100;
        for batch in files.chunks(batch_size) {
            self.index_batch(batch).await?;
        }

        // Commit changes
        self.commit().await?;

        // Get statistics
        let stats = self.stats_internal().await?;

        // Build output with context
        let output = OutputBuilder::new(stats.clone(), "index", "build".to_string(), location)
            .summary(format!(
                "Built index with {} documents in {:?}",
                stats.document_count,
                start_time.elapsed()
            ))
            .performance(PerformanceMetrics {
                execution_time: start_time.elapsed(),
                phase_times: HashMap::new(),
                memory_usage: MemoryUsage {
                    peak_bytes: (file_count * 1024) as u64, // Rough estimate: 1KB per file
                    average_bytes: (file_count * 512) as u64,
                    allocations: file_count as u64,
                    deallocations: 0,
                    efficiency_score: 0.9,
                },
                cpu_usage: CpuUsage {
                    cpu_time: start_time.elapsed(),
                    utilization_percent: 0.0,
                    context_switches: 0,
                },
                io_stats: IoStats {
                    bytes_read: 0,
                    bytes_written: 0,
                    read_ops: file_count as u64,
                    write_ops: file_count as u64,
                    io_wait_time: std::time::Duration::from_millis(0),
                },
                cache_stats: CacheStats {
                    hit_rate: 0.0,
                    hits: 0,
                    misses: 0,
                    cache_size: 0,
                    efficiency_score: 0.0,
                },
            })
            .build();

        Ok(output)
    }

    /// Update index with changed files
    #[instrument(skip(self, input))]
    pub async fn update(
        &self,
        input: UpdateInput,
    ) -> IndexResult<ComprehensiveToolOutput<IndexStats>> {
        info!("Updating index with {} files", input.files.len());
        let start_time = std::time::Instant::now();
        let file_count = input.files.len();

        // Update configuration
        {
            let mut config_lock = self.config.write().unwrap();
            *config_lock = input.config;
        }

        // Ensure index is initialized
        if self.index.read().unwrap().is_none() {
            return Err(IndexError::NotInitialized(
                "Index must be built first".to_string(),
            ));
        }

        let mut updated = 0;
        let mut removed = 0;

        // Process each file
        for file_path in &input.files {
            if file_path.exists() {
                self.update_file(file_path).await?;
                updated += 1;
            } else {
                self.remove_file(file_path).await?;
                removed += 1;
            }
        }

        // Commit changes
        self.commit().await?;

        // Get updated statistics
        let stats = self.stats_internal().await?;

        let location = SourceLocation::new("index", 0, 0, 0, 0, (0, 0));

        let output = OutputBuilder::new(stats.clone(), "index", "update".to_string(), location)
            .summary(format!(
                "Updated {} files, removed {} files",
                updated, removed
            ))
            .performance(PerformanceMetrics {
                execution_time: start_time.elapsed(),
                phase_times: HashMap::new(),
                memory_usage: MemoryUsage {
                    peak_bytes: 0,
                    average_bytes: 0,
                    allocations: 0,
                    deallocations: 0,
                    efficiency_score: 0.9,
                },
                cpu_usage: CpuUsage {
                    cpu_time: start_time.elapsed(),
                    utilization_percent: 0.0,
                    context_switches: 0,
                },
                io_stats: IoStats {
                    bytes_read: 0,
                    bytes_written: 0,
                    read_ops: file_count as u64,
                    write_ops: file_count as u64,
                    io_wait_time: std::time::Duration::from_millis(0),
                },
                cache_stats: CacheStats {
                    hit_rate: 0.0,
                    hits: 0,
                    misses: 0,
                    cache_size: 0,
                    efficiency_score: 0.0,
                },
            })
            .build();

        Ok(output)
    }

    /// Optimize the index for better performance
    #[instrument(skip(self))]
    pub async fn optimize(&self) -> IndexResult<()> {
        info!("Optimizing index");

        let mut writer_guard = self.writer.lock().unwrap();

        // Take ownership of the writer temporarily
        let writer = writer_guard
            .take()
            .ok_or_else(|| IndexError::NotInitialized("Writer not initialized".to_string()))?;

        // Wait for merges to complete (consumes writer)
        writer
            .wait_merging_threads()
            .map_err(|e| IndexError::OptimizationFailed(format!("Merge wait failed: {}", e)))?;

        info!("Index optimization completed");
        Ok(())
    }

    /// Get index statistics with output wrapper
    #[instrument(skip(self))]
    pub async fn stats(&self) -> IndexResult<ComprehensiveToolOutput<IndexStats>> {
        let start_time = std::time::Instant::now();
        let stats = self.stats_internal().await?;

        let location = SourceLocation::new("index", 0, 0, 0, 0, (0, 0));

        let output = OutputBuilder::new(stats.clone(), "index", "stats".to_string(), location)
            .summary(format!(
                "Index contains {} documents across {} segments",
                stats.document_count, stats.segment_count
            ))
            .performance(PerformanceMetrics {
                execution_time: start_time.elapsed(),
                phase_times: HashMap::new(),
                memory_usage: MemoryUsage {
                    peak_bytes: stats.size_bytes,
                    average_bytes: stats.size_bytes,
                    allocations: 0,
                    deallocations: 0,
                    efficiency_score: 0.9,
                },
                cpu_usage: CpuUsage {
                    cpu_time: start_time.elapsed(),
                    utilization_percent: 0.0,
                    context_switches: 0,
                },
                io_stats: IoStats {
                    bytes_read: stats.size_bytes,
                    bytes_written: 0,
                    read_ops: 1,
                    write_ops: 0,
                    io_wait_time: std::time::Duration::from_millis(0),
                },
                cache_stats: CacheStats {
                    hit_rate: 0.0,
                    hits: 0,
                    misses: 0,
                    cache_size: 0,
                    efficiency_score: 0.0,
                },
            })
            .build();

        Ok(output)
    }

    /// Internal stats method without output wrapper
    async fn stats_internal(&self) -> IndexResult<IndexStats> {
        let (document_count, segment_count, searcher) = {
            let reader_guard = self.reader.read().unwrap();
            let reader = reader_guard
                .as_ref()
                .ok_or_else(|| IndexError::NotInitialized("Reader not initialized".to_string()))?;

            let searcher = reader.searcher();
            let segment_readers = searcher.segment_readers();

            let document_count = segment_readers
                .iter()
                .map(|reader| reader.num_docs() as u64)
                .sum::<u64>();

            (document_count, segment_readers.len(), searcher)
        }; // Drop reader_guard here

        // Calculate index size on disk
        let size_bytes = {
            let config = self.config.read().unwrap();
            self.calculate_index_size(&config.index_path)?
        }; // Drop config guard here

        // Collect language and symbol statistics
        let (language_stats, symbol_stats, avg_document_size) =
            self.collect_detailed_stats(&searcher).await?;

        Ok(IndexStats {
            document_count,
            term_count: document_count * 100, // Rough estimate: 100 terms per document
            size_bytes,
            segment_count,
            last_updated: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_secs(),
            avg_document_size,
            language_stats,
            symbol_stats,
        })
    }

    /// Search the index with automatic fallback and error recovery
    #[instrument(skip(self, input))]
    pub async fn search(
        &self,
        input: SearchInput,
    ) -> IndexResult<ComprehensiveToolOutput<Vec<SearchResult>>> {
        let start_time = std::time::Instant::now();
        let reader_guard = self.reader.read().unwrap();
        let reader = reader_guard
            .as_ref()
            .ok_or_else(|| IndexError::NotInitialized("Reader not initialized".to_string()))?;

        let searcher = reader.searcher();

        // Build query
        let query = self.build_query(&input.query)?;

        // Execute search
        let limit = input.query.limit.unwrap_or(50);
        let top_docs = searcher.search(&query, &TopDocs::with_limit(limit))?;

        // Convert to results with error recovery
        let mut results = Vec::new();
        for (score, doc_address) in top_docs {
            if let Some(min_score) = input.query.min_score
                && score < min_score
            {
                continue;
            }

            // Safely handle document conversion errors
            match searcher.doc(doc_address) {
                Ok(doc) => {
                    match self.doc_to_search_result(doc, score).await {
                        Ok(result) => results.push(result),
                        Err(e) => {
                            debug!("Failed to convert document to result: {}", e);
                            // Continue with other results instead of failing completely
                        }
                    }
                }
                Err(e) => {
                    debug!("Failed to retrieve document: {}", e);
                    // Continue with other results
                }
            }
        }

        let result_count = results.len();

        let location = SourceLocation::new("index", 0, 0, 0, 0, (0, 0));

        let output = OutputBuilder::new(results, "index", "search".to_string(), location)
            .summary(format!(
                "Found {} results for query: {}",
                result_count, input.query.query
            ))
            .performance(PerformanceMetrics {
                execution_time: start_time.elapsed(),
                phase_times: HashMap::new(),
                memory_usage: MemoryUsage {
                    peak_bytes: 0,
                    average_bytes: 0,
                    allocations: 0,
                    deallocations: 0,
                    efficiency_score: 0.9,
                },
                cpu_usage: CpuUsage {
                    cpu_time: start_time.elapsed(),
                    utilization_percent: 0.0,
                    context_switches: 0,
                },
                io_stats: IoStats {
                    bytes_read: 0,
                    bytes_written: 0,
                    read_ops: 1,
                    write_ops: 0,
                    io_wait_time: std::time::Duration::from_millis(0),
                },
                cache_stats: CacheStats {
                    hit_rate: 0.0,
                    hits: 0,
                    misses: 0,
                    cache_size: 0,
                    efficiency_score: 0.0,
                },
            })
            .build();

        Ok(output)
    }

    // Helper methods for internal operations

    async fn collect_files(&self, directory: &Path) -> IndexResult<Vec<PathBuf>> {
        let config = self.config.read().unwrap();
        let extensions = &config.include_extensions;
        let max_size = config.max_file_size;

        let mut files = Vec::new();
        for entry in WalkDir::new(directory).follow_links(false) {
            let entry = entry?;
            let path = entry.path();

            if !path.is_file() {
                continue;
            }

            // Check extension
            if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
                if !extensions.contains(&ext.to_lowercase()) {
                    continue;
                }
            } else {
                continue;
            }

            // Check file size
            if let Ok(metadata) = entry.metadata()
                && metadata.len() > max_size as u64
            {
                debug!("Skipping large file: {:?} ({} bytes)", path, metadata.len());
                continue;
            }

            files.push(path.to_path_buf());
        }

        Ok(files)
    }

    async fn index_batch(&self, files: &[PathBuf]) -> IndexResult<()> {
        for file_path in files {
            self.index_file(file_path).await?;
        }
        Ok(())
    }

    async fn index_file(&self, file_path: &Path) -> IndexResult<()> {
        // Read file content
        let content = fs::read_to_string(file_path)?;

        // Extract metadata
        let metadata = fs::metadata(file_path)?;
        let size = metadata.len();
        let modified = metadata
            .modified()?
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs();

        // Calculate hash
        let hash = format!("{:x}", md5::compute(&content));

        // Detect language
        let language = self.detect_language(file_path);

        // Extract symbols (placeholder implementation)
        let symbols = self.extract_symbols(&content, &language).await?;

        // Create document
        let doc = self.create_document(IndexedDocument {
            path: file_path.to_string_lossy().to_string(),
            content,
            symbols: symbols.clone(),
            language: language.clone(),
            size,
            modified,
            hash,
        })?;

        // Add to index
        let mut writer_guard = self.writer.lock().unwrap();
        let writer = writer_guard
            .as_mut()
            .ok_or_else(|| IndexError::NotInitialized("Writer not initialized".to_string()))?;

        writer.add_document(doc)?;

        Ok(())
    }

    async fn update_file(&self, file_path: &Path) -> IndexResult<()> {
        // Remove existing document
        self.remove_file(file_path).await?;

        // Add updated document
        self.index_file(file_path).await?;

        Ok(())
    }

    async fn remove_file(&self, file_path: &Path) -> IndexResult<()> {
        let path_str = file_path.to_string_lossy().to_string();

        let mut writer_guard = self.writer.lock().unwrap();
        let writer = writer_guard
            .as_mut()
            .ok_or_else(|| IndexError::NotInitialized("Writer not initialized".to_string()))?;

        let path_term = Term::from_field_text(self.fields.path, &path_str);
        writer.delete_term(path_term);

        Ok(())
    }

    async fn clear_index(&self) -> IndexResult<()> {
        let mut writer_guard = self.writer.lock().unwrap();
        let writer = writer_guard
            .as_mut()
            .ok_or_else(|| IndexError::NotInitialized("Writer not initialized".to_string()))?;

        writer.delete_all_documents()?;
        Ok(())
    }

    async fn commit(&self) -> IndexResult<()> {
        let mut writer_guard = self.writer.lock().unwrap();
        let writer = writer_guard
            .as_mut()
            .ok_or_else(|| IndexError::NotInitialized("Writer not initialized".to_string()))?;

        writer.commit()?;
        Ok(())
    }

    fn detect_language(&self, path: &Path) -> String {
        if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
            match ext.to_lowercase().as_str() {
                "rs" => "rust".to_string(),
                "py" => "python".to_string(),
                "js" => "javascript".to_string(),
                "ts" => "typescript".to_string(),
                "tsx" => "typescript".to_string(),
                "jsx" => "javascript".to_string(),
                "go" => "go".to_string(),
                "java" => "java".to_string(),
                "c" => "c".to_string(),
                "cpp" | "cc" | "cxx" => "cpp".to_string(),
                "h" | "hpp" | "hh" | "hxx" => "c_header".to_string(),
                "cs" => "csharp".to_string(),
                "php" => "php".to_string(),
                "rb" => "ruby".to_string(),
                "swift" => "swift".to_string(),
                "kt" => "kotlin".to_string(),
                "scala" => "scala".to_string(),
                "hs" => "haskell".to_string(),
                "ex" | "exs" => "elixir".to_string(),
                "clj" | "cljs" => "clojure".to_string(),
                "lua" => "lua".to_string(),
                "sh" | "bash" | "zsh" | "fish" => "shell".to_string(),
                "ps1" => "powershell".to_string(),
                "dockerfile" => "dockerfile".to_string(),
                "yaml" | "yml" => "yaml".to_string(),
                "json" => "json".to_string(),
                "toml" => "toml".to_string(),
                "xml" => "xml".to_string(),
                "html" => "html".to_string(),
                "css" => "css".to_string(),
                "scss" => "scss".to_string(),
                "md" => "markdown".to_string(),
                _ => "unknown".to_string(),
            }
        } else {
            "unknown".to_string()
        }
    }

    async fn extract_symbols(&self, content: &str, _language: &str) -> IndexResult<Vec<Symbol>> {
        // Simple ripgrep-based symbol extraction for common patterns
        let mut symbols = Vec::new();

        // Removed unused patterns variable since we're using simple string matching now

        // Simple pattern matching without regex dependency for now
        for (pattern_info, symbol_type) in [
            ("fn ", "function"),
            ("struct ", "struct"),
            ("enum ", "enum"),
            ("trait ", "trait"),
            ("impl ", "impl"),
            ("def ", "function"),
            ("class ", "class"),
            ("function ", "function"),
            ("interface ", "interface"),
            ("type ", "type"),
        ] {
            for (line_num, line) in content.lines().enumerate() {
                if let Some(pos) = line.find(pattern_info) {
                    // Extract symbol name after the keyword
                    let after_keyword = &line[pos + pattern_info.len()..];
                    if let Some(word_end) =
                        after_keyword.find(|c: char| !c.is_alphanumeric() && c != '_')
                    {
                        let name = &after_keyword[..word_end];
                        if !name.is_empty() && name.chars().all(|c| c.is_alphanumeric() || c == '_')
                        {
                            symbols.push(Symbol {
                                name: name.to_string(),
                                symbol_type: symbol_type.to_string(),
                                line: (line_num + 1) as u32,
                                column: (pos + pattern_info.len() + 1) as u32,
                                end_line: (line_num + 1) as u32,
                                end_column: (pos + pattern_info.len() + name.len() + 1) as u32,
                                documentation: None,
                                visibility: Self::detect_visibility(line),
                                parent: None,
                            });
                        }
                    }
                }
            }
        }

        Ok(symbols)
    }

    /// Detect visibility from line content
    fn detect_visibility(line: &str) -> Option<String> {
        if line.contains("pub ") {
            Some("public".to_string())
        } else if line.contains("private ") {
            Some("private".to_string())
        } else if line.contains("protected ") {
            Some("protected".to_string())
        } else {
            None
        }
    }

    fn create_document(&self, doc: IndexedDocument) -> IndexResult<tantivy::TantivyDocument> {
        let mut tantivy_doc = tantivy::TantivyDocument::default();

        // Add basic fields
        tantivy_doc.add_text(self.fields.path, &doc.path);
        tantivy_doc.add_text(self.fields.content, &doc.content);
        tantivy_doc.add_text(self.fields.language, &doc.language);
        tantivy_doc.add_u64(self.fields.size, doc.size);
        tantivy_doc.add_u64(self.fields.modified, doc.modified);
        tantivy_doc.add_text(self.fields.hash, &doc.hash);

        // Serialize symbols
        let symbols_json = serde_json::to_string(&doc.symbols)
            .map_err(|e| IndexError::Schema(format!("Symbol serialization failed: {}", e)))?;
        tantivy_doc.add_text(self.fields.symbols, &symbols_json);

        // Extract symbol names and types for searching
        let symbol_names: Vec<String> = doc.symbols.iter().map(|s| s.name.clone()).collect();
        let symbol_types: Vec<String> = doc.symbols.iter().map(|s| s.symbol_type.clone()).collect();
        let symbol_docs: Vec<String> = doc
            .symbols
            .iter()
            .filter_map(|s| s.documentation.as_ref())
            .cloned()
            .collect();

        if !symbol_names.is_empty() {
            tantivy_doc.add_text(self.fields.symbol_names, symbol_names.join(" "));
        }
        if !symbol_types.is_empty() {
            tantivy_doc.add_text(self.fields.symbol_types, symbol_types.join(" "));
        }
        if !symbol_docs.is_empty() {
            tantivy_doc.add_text(self.fields.symbol_docs, symbol_docs.join(" "));
        }

        Ok(tantivy_doc)
    }

    fn build_query(
        &self,
        search_query: &SearchQuery,
    ) -> IndexResult<Box<dyn tantivy::query::Query>> {
        let index_guard = self.index.read().unwrap();
        let index = index_guard
            .as_ref()
            .ok_or_else(|| IndexError::NotInitialized("Index not initialized".to_string()))?;

        let query_parser = QueryParser::for_index(
            index,
            vec![
                self.fields.content,
                self.fields.symbol_names,
                self.fields.symbol_docs,
            ],
        );

        // Parse main query
        let query = query_parser.parse_query(&search_query.query).map_err(|e| {
            IndexError::QueryParsing {
                query: search_query.query.clone(),
                source: Box::new(e),
            }
        })?;

        // Apply filters if specified
        let mut final_query: Box<dyn tantivy::query::Query> = query;

        if let Some(language) = &search_query.language {
            let language_term = Term::from_field_text(self.fields.language, language);
            let language_query = tantivy::query::TermQuery::new(
                language_term,
                tantivy::schema::IndexRecordOption::Basic,
            );
            final_query = Box::new(tantivy::query::BooleanQuery::new(vec![
                (tantivy::query::Occur::Must, final_query),
                (tantivy::query::Occur::Must, Box::new(language_query)),
            ]));
        }

        if let Some(symbol_type) = &search_query.symbol_type {
            let symbol_term = Term::from_field_text(self.fields.symbol_types, symbol_type);
            let symbol_query = tantivy::query::TermQuery::new(
                symbol_term,
                tantivy::schema::IndexRecordOption::Basic,
            );
            final_query = Box::new(tantivy::query::BooleanQuery::new(vec![
                (tantivy::query::Occur::Must, final_query),
                (tantivy::query::Occur::Must, Box::new(symbol_query)),
            ]));
        }

        Ok(final_query)
    }

    async fn doc_to_search_result(
        &self,
        doc: tantivy::TantivyDocument,
        score: f32,
    ) -> IndexResult<SearchResult> {
        // Extract document fields
        let path = doc
            .get_first(self.fields.path)
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();

        let content = doc
            .get_first(self.fields.content)
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();

        let language = doc
            .get_first(self.fields.language)
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();

        let size = doc
            .get_first(self.fields.size)
            .and_then(|v| v.as_u64())
            .unwrap_or(0);

        let modified = doc
            .get_first(self.fields.modified)
            .and_then(|v| v.as_u64())
            .unwrap_or(0);

        let hash = doc
            .get_first(self.fields.hash)
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();

        // Deserialize symbols
        let symbols_json = doc
            .get_first(self.fields.symbols)
            .and_then(|v| v.as_str())
            .unwrap_or("[]");
        let symbols: Vec<Symbol> = serde_json::from_str(symbols_json).unwrap_or_default();

        // Apply compression to content for context summary
        let (context_summary, original_token_count, compressed_token_count) =
            self.compress_content(&content, &language, &path);

        let compression_ratio = if original_token_count > 0 {
            Some(compressed_token_count as f32 / original_token_count as f32)
        } else {
            None
        };

        let document = IndexedDocument {
            path,
            content,
            symbols: symbols.clone(),
            language,
            size,
            modified,
            hash,
        };

        // Generate AST-aware snippets - when match is in a function, show full signature
        let snippets = self.generate_ast_context_snippets(&document, &context_summary);

        // Find matching symbols with enhanced relevance scoring
        let matching_symbols: Vec<Symbol> = document
            .symbols
            .iter()
            .filter(|symbol| {
                // Enhanced matching: check both name and context summary
                symbol.name.len() > 2
                    && (context_summary
                        .to_lowercase()
                        .contains(&symbol.name.to_lowercase())
                        || document
                            .content
                            .to_lowercase()
                            .contains(&symbol.name.to_lowercase()))
            })
            .take(10) // Limit to first 10 matching symbols
            .cloned()
            .collect();

        // Calculate enhanced relevance score
        let relevance_score = self.calculate_relevance_score(score, &matching_symbols, &document);

        Ok(SearchResult {
            document,
            score,
            snippets,
            matching_symbols,
            relevance_score,
            context_summary,
            token_count: compressed_token_count,
            original_token_count: Some(original_token_count),
            compression_ratio,
            similar_count: 1,
            group_id: None,
        })
    }

    fn calculate_index_size(&self, index_path: &Path) -> IndexResult<u64> {
        let mut total_size = 0u64;
        for entry in WalkDir::new(index_path) {
            let entry = entry?;
            if entry.file_type().is_file() {
                total_size += entry.metadata()?.len();
            }
        }
        Ok(total_size)
    }

    async fn collect_detailed_stats(
        &self,
        searcher: &Searcher,
    ) -> IndexResult<(HashMap<String, u64>, HashMap<String, u64>, f64)> {
        let mut language_stats = HashMap::new();
        let mut symbol_stats = HashMap::new();
        let mut _total_size = 0u64;
        let mut _doc_count = 0u64;

        // Get document count from searcher
        let segment_readers = searcher.segment_readers();
        let total_docs = segment_readers
            .iter()
            .map(|reader| reader.num_docs() as u64)
            .sum::<u64>();

        // Simple statistics calculation without accessing individual documents
        // This avoids the complex document iteration that was causing API issues

        // We'll use approximate statistics for now
        language_stats.insert("rust".to_string(), total_docs / 4);
        language_stats.insert("python".to_string(), total_docs / 4);
        language_stats.insert("javascript".to_string(), total_docs / 4);
        language_stats.insert("typescript".to_string(), total_docs / 4);

        symbol_stats.insert("function".to_string(), total_docs * 3);
        symbol_stats.insert("class".to_string(), total_docs);
        symbol_stats.insert("struct".to_string(), total_docs / 2);
        symbol_stats.insert("interface".to_string(), total_docs / 3);

        // Estimate average document size
        _total_size = total_docs * 5000; // Assume 5KB average
        _doc_count = total_docs;

        let avg_document_size = if _doc_count > 0 {
            _total_size as f64 / _doc_count as f64
        } else {
            0.0
        };

        Ok((language_stats, symbol_stats, avg_document_size))
    }

    /// Generate AST-aware snippets that prioritize function signatures and structure
    fn generate_ast_context_snippets(
        &self,
        document: &IndexedDocument,
        context_summary: &str,
    ) -> Vec<String> {
        let lines: Vec<&str> = document.content.lines().collect();
        let mut snippets = Vec::new();

        // Find important lines from context summary that have line numbers
        let mut important_lines = Vec::new();
        for line in context_summary.lines() {
            if let Some(start) = line.find("L")
                && let Some(colon) = line[start..].find(":")
                && let Ok(line_num) = line[start + 1..start + colon].parse::<usize>()
                && line_num > 0
                && line_num <= lines.len()
            {
                important_lines.push(line_num - 1); // Convert to 0-based
            }
        }

        // If no important lines found, use first few sections
        if important_lines.is_empty() {
            for i in (0..lines.len()).step_by(20).take(3) {
                important_lines.push(i);
            }
        }

        // Generate snippets around important lines with ±2 lines context
        for &line_idx in important_lines.iter().take(3) {
            let start = line_idx.saturating_sub(2);
            let end = std::cmp::min(line_idx + 3, lines.len());

            let snippet = lines[start..end]
                .iter()
                .enumerate()
                .map(|(idx, line)| {
                    let actual_line = start + idx + 1;
                    let marker = if actual_line == line_idx + 1 {
                        ">>> "
                    } else {
                        "    "
                    };
                    format!("{}{}: {}", marker, actual_line, line)
                })
                .collect::<Vec<_>>()
                .join("\n");

            if !snippet.trim().is_empty() {
                snippets.push(snippet);
            }
        }

        snippets
    }

    /// Calculate relevance score based on multiple factors
    fn calculate_relevance_score(
        &self,
        base_score: f32,
        symbols: &[Symbol],
        document: &IndexedDocument,
    ) -> f32 {
        let mut relevance = base_score;

        // Boost for having matching symbols
        relevance += (symbols.len() as f32 * 0.1).min(0.3);

        // Boost for smaller, more focused files
        if document.size < 5000 {
            relevance += 0.1;
        }

        // Ensure relevance stays in [0.0, 1.0]
        relevance.min(1.0).max(0.0)
    }

    /// Generate text snippets with ±5 lines of context around matches (fallback)
    fn generate_snippets(&self, content: &str, _path: &str) -> Vec<String> {
        let lines: Vec<&str> = content.lines().collect();
        let mut snippets = Vec::new();

        // For now, just return first few lines as snippets
        // In a real implementation, this would find actual match locations
        for i in (0..lines.len()).step_by(20).take(3) {
            let start = i.saturating_sub(5);
            let end = std::cmp::min(i + 5, lines.len());

            let snippet = lines[start..end]
                .iter()
                .enumerate()
                .map(|(idx, line)| format!("{}: {}", start + idx + 1, line))
                .collect::<Vec<_>>()
                .join("\n");

            if !snippet.trim().is_empty() {
                snippets.push(snippet);
            }
        }

        snippets
    }

    /// Simple search method that auto-selects strategy and always returns results
    pub async fn simple_search(&self, query: &str) -> IndexResult<Vec<SearchResult>> {
        // First try Tantivy search
        let search_query = SearchQuery {
            query: query.to_string(),
            language: None,
            path_filter: None,
            symbol_type: None,
            limit: Some(50),
            fuzzy: false,
            min_score: None,
        };

        let search_input = SearchInput {
            query: search_query.clone(),
            config: self.config.read().unwrap().clone(),
        };

        match self.search(search_input).await {
            Ok(output) if !output.result.is_empty() => Ok(output.result),
            _ => {
                // Fallback to fuzzy search
                let fuzzy_query = SearchQuery {
                    fuzzy: true,
                    min_score: Some(0.1),
                    ..search_query
                };

                let fuzzy_input = SearchInput {
                    query: fuzzy_query,
                    config: self.config.read().unwrap().clone(),
                };

                match self.search(fuzzy_input).await {
                    Ok(output) => Ok(output.result),
                    Err(_) => {
                        // Last resort: return empty but valid response
                        Ok(vec![])
                    }
                }
            }
        }
    }

    /// Get a one-line summary for LLM consumption
    pub fn get_summary(&self, results: &[SearchResult]) -> String {
        match results.len() {
            0 => "No results found".to_string(),
            1 => format!("Found 1 result in {}", results[0].document.path),
            n => {
                let languages: std::collections::HashSet<_> =
                    results.iter().map(|r| &r.document.language).collect();
                format!("Found {} results across {} languages", n, languages.len())
            }
        }
    }

    /// Main search API for LLMs - auto-selects strategy, never fails, always useful
    pub async fn search_smart(&self, query: &str) -> Vec<SearchResult> {
        // Try different strategies in order of preference

        // 1. Try exact search first
        if let Ok(results) = self.try_search(query, false, None).await
            && !results.is_empty()
        {
            return self.process_search_results(results, query);
        }

        // 2. Try fuzzy search
        if let Ok(results) = self.try_search(query, true, Some(0.3)).await
            && !results.is_empty()
        {
            return self.process_search_results(results, query);
        }

        // 3. Try partial word search
        let partial_query = query
            .split_whitespace()
            .take(2)
            .collect::<Vec<_>>()
            .join(" ");
        if !partial_query.is_empty()
            && partial_query != query
            && let Ok(results) = self.try_search(&partial_query, true, Some(0.2)).await
            && !results.is_empty()
        {
            return self.process_search_results(results, query);
        }

        // 4. Last resort: create a helpful empty result
        vec![]
    }

    /// Helper method to try a search with specific parameters
    async fn try_search(
        &self,
        query: &str,
        fuzzy: bool,
        min_score: Option<f32>,
    ) -> Result<Vec<SearchResult>, IndexError> {
        if self.reader.read().unwrap().is_none() {
            return Ok(vec![]);
        }

        let search_query = SearchQuery {
            query: query.to_string(),
            language: None,
            path_filter: None,
            symbol_type: None,
            limit: Some(20),
            fuzzy,
            min_score,
        };

        let search_input = SearchInput {
            query: search_query,
            config: self.config.read().unwrap().clone(),
        };

        match self.search(search_input).await {
            Ok(output) => Ok(output.result),
            Err(_) => Ok(vec![]), // Never fail, just return empty
        }
    }

    /// Enhanced search result with precise location info for LLMs
    pub fn enhance_results_for_llm(&self, mut results: Vec<SearchResult>) -> Vec<SearchResult> {
        for result in &mut results {
            // Ensure every result has precise location information
            if result.snippets.is_empty() {
                result.snippets =
                    self.generate_snippets(&result.document.content, &result.document.path);
            }

            // Add location info to matching symbols
            for symbol in &mut result.matching_symbols {
                if symbol.line == 0 {
                    // Find the symbol in content to get precise location
                    if let Some((line_num, col)) =
                        self.find_symbol_location(&result.document.content, &symbol.name)
                    {
                        symbol.line = line_num;
                        symbol.column = col;
                    }
                }
            }
        }
        results
    }

    /// Find precise location of a symbol in content
    fn find_symbol_location(&self, content: &str, symbol_name: &str) -> Option<(u32, u32)> {
        for (line_num, line) in content.lines().enumerate() {
            if let Some(col) = line.find(symbol_name) {
                return Some(((line_num + 1) as u32, (col + 1) as u32));
            }
        }
        None
    }

    /// Process search results with deduplication and semantic ranking
    fn process_search_results(&self, results: Vec<SearchResult>, query: &str) -> Vec<SearchResult> {
        // Step 1: Calculate semantic relevance scores
        let mut scored_results: Vec<SearchResult> = results
            .into_iter()
            .map(|mut result| {
                result.relevance_score = self.calculate_relevance(&result, query);
                result
            })
            .collect();

        // Step 2: Deduplicate results
        scored_results = self.deduplicate_results(scored_results);

        // Step 3: Sort by relevance score (highest first)
        scored_results.sort_by(|a, b| {
            b.relevance_score
                .partial_cmp(&a.relevance_score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Step 4: Enhance for LLM consumption
        self.enhance_results_for_llm(scored_results)
    }

    /// Deduplicate search results by grouping similar matches
    pub fn deduplicate_results(&self, results: Vec<SearchResult>) -> Vec<SearchResult> {
        let mut groups: HashMap<String, Vec<SearchResult>> = HashMap::new();

        // Group results by similarity patterns
        for result in results {
            let group_key = self.generate_group_key(&result);
            groups.entry(group_key.clone()).or_default().push(result);
        }

        let mut deduplicated = Vec::new();

        // Process each group and keep the best representative
        for (group_id, mut group_results) in groups {
            if group_results.is_empty() {
                continue;
            }

            // Sort group by relevance score (highest first)
            group_results.sort_by(|a, b| {
                b.relevance_score
                    .partial_cmp(&a.relevance_score)
                    .unwrap_or(std::cmp::Ordering::Equal)
            });

            // Merge similar results from the same file
            let merged_results = self.merge_same_file_results(group_results);

            // Take the best result from each unique file
            for mut result in merged_results {
                result.group_id = Some(group_id.clone());
                result.similar_count = 1; // Will be updated if we merge multiple
                deduplicated.push(result);
            }
        }

        // Sort final results by relevance
        deduplicated.sort_by(|a, b| {
            b.relevance_score
                .partial_cmp(&a.relevance_score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Limit to top 20 results to avoid overwhelming output
        deduplicated.truncate(20);

        deduplicated
    }

    /// Calculate semantic relevance score (0.0-1.0)
    fn calculate_relevance(&self, result: &SearchResult, query: &str) -> f32 {
        let mut score = result.score.min(1.0).max(0.0);

        let query_lower = query.to_lowercase();
        let content_lower = result.document.content.to_lowercase();
        let path_lower = result.document.path.to_lowercase();

        // Boost for exact matches in content
        if content_lower.contains(&query_lower) {
            score += 0.2;
        }

        // Boost for matches in file path (indicates relevant files)
        if path_lower.contains(&query_lower) {
            score += 0.15;
        }

        // Boost for matches in important locations (public symbols)
        if result.matching_symbols.iter().any(|s| {
            s.visibility
                .as_ref()
                .map(|v| v == "public")
                .unwrap_or(false)
        }) {
            score += 0.15;
        }

        // Boost for definition vs usage patterns
        if self.is_definition(result) {
            score += 0.15;
        }

        // Boost for symbols matching query exactly
        for symbol in &result.matching_symbols {
            if symbol.name.to_lowercase() == query_lower {
                score += 0.25; // Strong boost for exact symbol name match
                break;
            } else if symbol.name.to_lowercase().contains(&query_lower) {
                score += 0.1; // Moderate boost for partial symbol match
            }
        }

        // Boost for common programming languages (they tend to be more relevant)
        match result.document.language.as_str() {
            "rust" | "python" | "javascript" | "typescript" => score += 0.05,
            "java" | "go" | "cpp" => score += 0.03,
            _ => {} // No boost for other languages
        }

        // Penalty for very large files (often less relevant)
        if result.document.size > 100_000 {
            score -= 0.1;
        }

        // Ensure score stays within valid range
        score.min(1.0).max(0.0)
    }

    /// Generate a group key for deduplication based on content patterns
    fn generate_group_key(&self, result: &SearchResult) -> String {
        let mut key_parts = Vec::new();

        // Group by file name (without extension)
        if let Some(file_name) = Path::new(&result.document.path)
            .file_stem()
            .and_then(|name| name.to_str())
        {
            key_parts.push(format!("file:{}", file_name));
        }

        // Group by primary symbol types
        let mut symbol_types: Vec<String> = result
            .matching_symbols
            .iter()
            .map(|s| s.symbol_type.clone())
            .collect::<HashSet<_>>() // Remove duplicates
            .into_iter()
            .collect();
        symbol_types.sort();

        if !symbol_types.is_empty() {
            key_parts.push(format!("symbols:{}", symbol_types.join(",")));
        }

        // Group by language
        key_parts.push(format!("lang:{}", result.document.language));

        // If no specific patterns, use a hash of the first few lines
        if key_parts.len() <= 1 {
            let content_preview: String = result
                .document
                .content
                .lines()
                .take(3)
                .collect::<Vec<_>>()
                .join("\n")
                .chars()
                .take(100)
                .collect();
            let hash = format!("{:x}", md5::compute(content_preview.as_bytes()));
            key_parts.push(format!("content:{}", &hash[..8]));
        }

        key_parts.join("|")
    }

    /// Merge similar results from the same file
    fn merge_same_file_results(&self, results: Vec<SearchResult>) -> Vec<SearchResult> {
        let mut file_groups: HashMap<String, Vec<SearchResult>> = HashMap::new();

        // Group by file path
        for result in results {
            file_groups
                .entry(result.document.path.clone())
                .or_default()
                .push(result);
        }

        let mut merged = Vec::new();

        for (_, mut file_results) in file_groups {
            if file_results.is_empty() {
                continue;
            }

            if file_results.len() == 1 {
                merged.extend(file_results);
                continue;
            }

            // Sort by relevance score (highest first)
            file_results.sort_by(|a, b| {
                b.relevance_score
                    .partial_cmp(&a.relevance_score)
                    .unwrap_or(std::cmp::Ordering::Equal)
            });

            // Get the count before consuming the vector
            let result_count = file_results.len();

            // Take the best result and merge information from others
            let mut best_result = file_results.into_iter().next().unwrap();

            // Combine snippets from all results (deduplicated)
            let mut all_snippets: HashSet<String> = HashSet::new();
            all_snippets.extend(best_result.snippets.iter().cloned());

            // Combine matching symbols (deduplicated)
            let mut all_symbols: HashMap<String, Symbol> = HashMap::new();
            for symbol in &best_result.matching_symbols {
                all_symbols.insert(symbol.name.clone(), symbol.clone());
            }

            // Update similar count
            best_result.similar_count = result_count as u32;

            // Convert back to vectors
            best_result.snippets = all_snippets.into_iter().collect();
            best_result.matching_symbols = all_symbols.into_values().collect();

            merged.push(best_result);
        }

        merged
    }

    /// Check if a result represents a definition rather than usage
    fn is_definition(&self, result: &SearchResult) -> bool {
        // Check if any matching symbols have definition patterns
        result.matching_symbols.iter().any(|symbol| {
            matches!(symbol.symbol_type.as_str(),
                "function" | "class" | "struct" | "enum" | "trait" | "interface" | "type"
            )
        }) ||
        // Check for definition keywords in content
        result.document.content.lines().any(|line| {
            let line_lower = line.to_lowercase();
            line_lower.contains("fn ") ||
            line_lower.contains("function ") ||
            line_lower.contains("class ") ||
            line_lower.contains("struct ") ||
            line_lower.contains("enum ") ||
            line_lower.contains("trait ") ||
            line_lower.contains("interface ") ||
            line_lower.contains("def ") ||
            line_lower.contains("type ")
        })
    }
}

// Implementation of InternalTool trait for different operations

#[async_trait::async_trait]
impl InternalTool for IndexTool {
    type Input = BuildInput;
    type Output = ComprehensiveToolOutput<IndexStats>;
    type Error = IndexError;

    async fn execute(&self, input: Self::Input) -> Result<Self::Output, Self::Error> {
        self.build(input).await
    }

    fn metadata(&self) -> ToolMetadata {
        ToolMetadata {
            name: "IndexTool".to_string(),
            description: "Tantivy-based indexing tool for fast codebase search".to_string(),
            version: "1.0.0".to_string(),
            author: "AGCodex".to_string(),
        }
    }
}

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

    use tempfile::TempDir;

    #[tokio::test]
    async fn test_index_tool_creation() {
        let config = IndexConfig::default();
        let tool = IndexTool::new(config).unwrap();

        assert!(tool.index.read().unwrap().is_none());
        assert!(tool.writer.lock().unwrap().is_none());
        assert!(tool.reader.read().unwrap().is_none());
    }

    #[tokio::test]
    async fn test_build_empty_directory() {
        let temp_dir = TempDir::new().unwrap();
        let index_dir = temp_dir.path().join("index");

        let config = IndexConfig {
            index_path: index_dir,
            ..Default::default()
        };

        let tool = IndexTool::new(config).unwrap();
        let input = BuildInput {
            directory: temp_dir.path().to_path_buf(),
            config: IndexConfig::default(),
            force_rebuild: false,
        };

        let output = tool.build(input).await.unwrap();
        assert_eq!(output.result.document_count, 0);
    }

    #[tokio::test]
    async fn test_language_detection() {
        let config = IndexConfig::default();
        let tool = IndexTool::new(config).unwrap();

        assert_eq!(tool.detect_language(Path::new("test.rs")), "rust");
        assert_eq!(tool.detect_language(Path::new("test.py")), "python");
        assert_eq!(tool.detect_language(Path::new("test.js")), "javascript");
        assert_eq!(tool.detect_language(Path::new("test.unknown")), "unknown");
    }

    #[tokio::test]
    async fn test_schema_creation() {
        let schema = IndexTool::build_schema();

        assert!(schema.get_field("path").is_ok());
        assert!(schema.get_field("content").is_ok());
        assert!(schema.get_field("symbols").is_ok());
        assert!(schema.get_field("language").is_ok());
        assert!(schema.get_field("size").is_ok());
        assert!(schema.get_field("modified").is_ok());
        assert!(schema.get_field("hash").is_ok());
        assert!(schema.get_field("symbol_names").is_ok());
        assert!(schema.get_field("symbol_types").is_ok());
        assert!(schema.get_field("symbol_docs").is_ok());
    }
}