loregrep 0.6.0

Repository indexing library for AI coding assistants. Tree-sitter parsing, fast in-memory indexing, and tool APIs for LLM 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
//! # Loregrep: Fast Repository Indexing for Coding Assistants
//!
//! **Loregrep** is a high-performance repository indexing library that parses codebases into
//! fast, searchable in-memory indexes. It's designed to provide coding assistants and AI tools
//! with structured access to code functions, structures, dependencies, and call graphs.
//!
//! ## What It Does
//!
//! - **Parses** code files using tree-sitter for accurate syntax analysis
//! - **Indexes** functions, structs, imports, exports, and relationships in memory
//! - **Provides** standardized tools that coding assistants can call to query the codebase
//! - **Enables** AI systems to understand code structure without re-parsing
//!
//! ## What It's NOT
//!
//! - ❌ Not an AI tool itself (provides data TO AI systems)
//! - ❌ Not a traditional code analysis tool (no linting, metrics, complexity analysis)
//!
//! ## Core Architecture
//!
//! ```text
//! ┌─────────────────┐    ┌──────────────────┐    ┌─────────────────┐
//! │   Code Files    │───▶│   Tree-sitter    │───▶│   In-Memory     │
//! │  (.rs, .py,     │    │    Parsing       │    │    RepoMap      │
//! │   .ts, etc.)    │    │                  │    │    Indexes      │
//! └─────────────────┘    └──────────────────┘    └─────────────────┘
//!//!//! ┌─────────────────┐    ┌──────────────────┐    ┌─────────────────┐
//! │ Coding Assistant│◀───│   Query Tools    │◀───│   Fast Lookups  │
//! │   (Claude, GPT, │    │ (search, analyze,│    │  (functions,    │
//! │   Cursor, etc.) │    │  dependencies)   │    │   structs, etc.)│
//! └─────────────────┘    └──────────────────┘    └─────────────────┘
//! ```
//!
//! ## Quick Start
//!
//! ### Zero-Configuration Auto-Discovery (Recommended)
//!
//! ```ignore
//! use loregrep::LoreGrep;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     // One-line setup with automatic project detection
//!     let mut loregrep = LoreGrep::auto_discover(".")?;
//!     // 🔍 Detected project languages: rust, python
//!     // ✅ Rust analyzer registered successfully
//!     // ✅ Python analyzer registered successfully  
//!     // 📁 Configuring file patterns for detected languages
//!     // 🎆 LoreGrep configured with 2 language(s): rust, python
//!
//!     // Scan with comprehensive feedback
//!     let scan_result = loregrep.scan(".").await?;
//!     // 🔍 Starting repository scan... 📁 Found X files... 📊 Summary
//!     
//!     println!("Indexed {} files with {} functions",
//!              scan_result.files_scanned,
//!              scan_result.functions_found);
//!     
//!     Ok(())
//! }
//! ```
//!
//! ### Manual Configuration with Enhanced Builder
//!
//! ```ignore
//! use loregrep::LoreGrep;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     // Full control with enhanced builder pattern
//!     let mut loregrep = LoreGrep::builder()
//!         .with_rust_analyzer()           // ✅ Real-time feedback
//!         .with_python_analyzer()         // ✅ Registration confirmation
//!         .optimize_for_performance()     // 🚀 Speed-optimized preset
//!         .exclude_test_dirs()            // 🚫 Skip test directories
//!         .max_file_size(1024 * 1024)     // 1MB limit
//!         .max_depth(10)                  // Directory depth limit
//!         .build()?;                      // 🎆 Configuration summary
//!
//!     let scan_result = loregrep.scan("/path/to/your/repo").await?;
//!     
//!     Ok(())
//! }
//! ```
//!
//! ### Integration with Coding Assistants
//!
//! The library provides standardized tools that AI coding assistants can call:
//!
//! ```ignore
//! use loregrep::LoreGrep;
//! use serde_json::json;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     // Option 1: Zero-configuration setup
//!     let mut loregrep = LoreGrep::auto_discover(".")?;
//!     // Auto-detects languages and configures appropriate analyzers
//!     
//!     // Option 2: Manual setup with presets
//!     let mut loregrep = LoreGrep::rust_project(".")?;  // Rust-optimized
//!     // Or: LoreGrep::python_project(".")?  // Python-optimized
//!     // Or: LoreGrep::polyglot_project(".")?  // Multi-language
//!     
//!     // Scan with enhanced feedback
//!     loregrep.scan(".").await?;
//!
//!     // Tool 1: Search for functions (with file path information)
//!     let result = loregrep.execute_tool("search_functions", json!({
//!         "pattern": "parse",
//!         "limit": 20
//!     })).await?;
//!
//!     // Tool 2: Find function callers with cross-file analysis
//!     let callers = loregrep.execute_tool("find_callers", json!({
//!         "function_name": "parse_config"
//!     })).await?;
//!
//!     // Tool 3: Analyze specific file
//!     let analysis = loregrep.execute_tool("analyze_file", json!({
//!         "file_path": "src/main.rs"
//!     })).await?;
//!
//!     Ok(())
//! }
//! ```
//!
//! ### Available Tools for AI Integration
//!
//! ```ignore
//! // Get tool definitions for your AI system
//! let tools = LoreGrep::get_tool_definitions();
//!
//! // Current tool set (get_tool_definitions() is always authoritative):
//! // 1. search_functions      - Find functions by name/pattern
//! // 2. search_structs        - Find structures by name/pattern
//! // 3. analyze_file          - Get detailed file analysis
//! // 4. get_dependencies      - Find imports/exports for a file
//! // 5. find_callers          - Get direct function call sites
//! // 6. trace_callers         - Trace transitive (upstream) callers via the call graph
//! // 7. analyze_impact        - Compute change blast radius via the call graph
//! // 8. get_repository_tree   - Get repository structure and overview
//! // 9. find_importers        - Find files that import a given file
//! // 10. get_dependency_graph - Resolved import graph plus import cycles
//! ```
//!
//! ## Architecture Overview
//!
//! ### Core Components
//!
//! - **`LoreGrep`**: Main API facade with builder pattern configuration
//! - **`RepoMap`**: Fast in-memory indexes with lookup optimization
//! - **`RepositoryScanner`**: File discovery with gitignore support
//! - **Language Analyzers**: Tree-sitter based parsing (Rust complete, others on roadmap)
//! - **Tool System**: standardized tools for AI integration
//!
//! ### Design Characteristics
//!
//! - **Architecture**: Fast in-memory indexing with tree-sitter parsing
//! - **Concurrency**: Thread-safe with `Arc<Mutex<>>` design
//! - **Scalability**: Memory usage scales linearly with codebase size
//!
//! ## Language Support
//!
//! | Language   | Status     | Functions | Structs | Imports | Calls |
//! |------------|------------|-----------|---------|---------|-------|
//! | Rust       | ✅ Full    | ✅        | ✅      | ✅      | ✅    |
//! | Python     | ✅ Full    | ✅        | ✅      | ✅      | ✅    |
//! | TypeScript | ✅ Full    | ✅        | ✅      | ✅      | ✅    |
//! | JavaScript | 📋 Roadmap | -         | -       | -       | -     |
//! | Go         | 📋 Roadmap | -         | -       | -       | -     |
//!
//! *Note: Languages marked "📋 Roadmap" are future planned additions.*
//!
//! ## Integration Examples
//!
//! ### With Claude/OpenAI
//!
//! ```ignore
//! // Provide tools to your AI client
//! let tools = LoreGrep::get_tool_definitions();
//!
//! // Send to Claude/OpenAI as available tools
//! // When AI calls a tool, execute it:
//! let result = loregrep.execute_tool(&tool_name, tool_args).await?;
//! ```
//!
//! ### With MCP (Model Context Protocol)
//!
//! ```ignore
//! // MCP server integration is planned for future releases
//! // Will provide standard MCP interface for tool calling
//! ```
//!
//! ## Configuration Options
//!
//! ### Enhanced Builder with Convenience Methods
//!
//! ```ignore
//! use loregrep::LoreGrep;
//!
//! // Performance-optimized configuration
//! let fast_loregrep = LoreGrep::builder()
//!     .with_rust_analyzer()           // ✅ Analyzer registration feedback
//!     .optimize_for_performance()     // 🚀 512KB limit, depth 8, skip binaries
//!     .exclude_test_dirs()            // 🚫 Skip test directories  
//!     .exclude_vendor_dirs()          // 🚫 Skip vendor/dependencies
//!     .build()?;                      // 🎆 Configuration summary
//!
//! // Comprehensive analysis configuration  
//! let thorough_loregrep = LoreGrep::builder()
//!     .with_all_analyzers()           // ✅ All available language analyzers
//!     .comprehensive_analysis()       // 🔍 5MB limit, depth 20, more file types
//!     .include_config_files()         // ✅ Include TOML, JSON, YAML configs
//!     .build()?;
//!
//! // Traditional manual configuration (still supported)
//! let manual_loregrep = LoreGrep::builder()
//!     .max_file_size(2 * 1024 * 1024)     // 2MB file size limit
//!     .max_depth(15)                       // Max directory depth
//!     .file_patterns(vec!["*.rs", "*.py"]) // File extensions to scan
//!     .exclude_patterns(vec!["target/"])   // Directories to skip
//!     .respect_gitignore(true)             // Honor .gitignore files
//!     .build()?;
//! ```
//!
//! ## Thread Safety
//!
//! All operations are thread-safe. Multiple threads can query the same `LoreGrep` instance
//! concurrently. Scanning operations are synchronized to prevent data races.
//!
//! ```ignore
//! use std::sync::Arc;
//! use tokio::task;
//!
//! let loregrep = Arc::new(loregrep);
//!
//! // Multiple concurrent queries
//! let handles: Vec<_> = (0..10).map(|i| {
//!     let lg = loregrep.clone();
//!     task::spawn(async move {
//!         lg.execute_tool("search_functions", json!({"pattern": "test"})).await
//!     })
//! }).collect();
//! ```
//!
//! ## Error Handling
//!
//! The library uses comprehensive error types for different failure modes:
//!
//! ```ignore
//! use loregrep::{LoreGrep, LoreGrepError};
//!
//! match loregrep.scan("/invalid/path").await {
//!     Ok(result) => println!("Success: {:?}", result),
//!     Err(LoreGrepError::Io(e)) => println!("IO error: {}", e),
//!     Err(LoreGrepError::Parse(e)) => println!("Parse error: {}", e),
//!     Err(LoreGrepError::Config(e)) => println!("Config error: {}", e),
//!     Err(e) => println!("Other error: {}", e),
//! }
//! ```
//!
//! ## Use Cases
//!
//! - **AI Code Assistants**: Provide structured code context to LLMs
//! - **Code Search Tools**: Fast symbol and pattern searching
//! - **Refactoring Tools**: Impact analysis and dependency tracking
//! - **Documentation Generators**: Extract API surfaces automatically
//! - **Code Quality Tools**: Analyze code patterns and relationships
//!
//! ## Performance Notes
//!
//! - Indexes are built in memory for fast access
//! - Scanning is parallelized across CPU cores
//! - Query results are cached for repeated access
//! - Memory usage scales linearly with codebase size
//! - No external dependencies required at runtime
//!
//! ## Future Roadmap
//!
//! ### Language Support
//! - **JavaScript Analyzer**: Support for modern JS features including ES6+ syntax (TypeScript/TSX is already supported)
//! - **Go Analyzer**: Package declarations, interfaces, and Go-specific function signatures
//!
//! ### Advanced Analysis Features  
//! - **Call Graph Analysis**: Function call extraction and visualization across files
//! - **Dependency Tracking**: Advanced import/export analysis and impact assessment
//! - **Incremental Updates**: Smart re-indexing when files change to avoid full rescans
//!
//! ### Performance & Optimization
//! - **Memory Optimization**: Improved handling of large repositories with better memory management
//! - **Query Performance**: Enhanced caching and lookup optimization for faster results
//! - **Database Persistence**: Optional disk-based storage for very large codebases
//!
//! ### Integration & Architecture
//! - **MCP Server Integration**: Standard Model Context Protocol interface for tool calling
//! - **Editor Integrations**: VS Code, IntelliJ, and other popular editor plugins
//! - **API Enhancements**: Additional tools and query capabilities for LLM integration

// ================================================================================================
// PUBLIC API EXPORTS
// ================================================================================================

// Internal modules (not part of public API)
mod analyzers;
pub(crate) mod internal;
mod parser;
mod scanner;
mod storage;
mod types;

// CLI module (temporary public access for binary, will be refactored in Task 4C.4)
#[doc(hidden)]
pub mod cli_main;

// Public API modules
pub mod core;
mod loregrep;

// PyO3 imports for Python bindings
#[cfg(feature = "python")]
use pyo3::prelude::*;

// ================================================================================================
// CLEAN PUBLIC API EXPORTS
// ================================================================================================

/// Main LoreGrep API - the primary interface for code analysis
///
/// **Quick Start Options:**
/// - [`LoreGrep::auto_discover()`] - Zero-configuration setup with automatic project detection
/// - [`LoreGrep::builder()`] - Full control with enhanced builder pattern
/// - [`LoreGrep::rust_project()`] - Rust-optimized preset
/// - [`LoreGrep::python_project()`] - Python-optimized preset  
/// - [`LoreGrep::polyglot_project()`] - Multi-language preset
pub use crate::loregrep::{LoreGrep, LoreGrepBuilder};

/// Core types for tool definitions and results
///
/// These types are designed for seamless integration with LLM tool calling systems.
pub use crate::core::types::{ScanResult, ToolResult, ToolSchema};

/// Error handling types
///
/// All operations return `Result<T, LoreGrepError>` for consistent error handling.
pub use crate::core::errors::{LoreGrepError, Result};

/// Current library version
///
/// Useful for version checking and compatibility verification.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

// ================================================================================================
// RE-EXPORTS FOR COMPATIBILITY
// ================================================================================================

// NOTE: LoreGrepConfig is intentionally not exported as it's an implementation detail.
// Users should configure through the builder pattern instead.

/// Creates the Python module
#[cfg(feature = "python")]
#[pymodule]
#[pyo3(name = "loregrep")]
fn loregrep_py(m: &Bound<'_, PyModule>) -> PyResult<()> {
    // Register the main high-level API only
    m.add_class::<python_bindings::PyLoreGrep>()?;
    m.add_class::<python_bindings::PyLoreGrepBuilder>()?;
    m.add_class::<python_bindings::PyScanResult>()?;
    m.add_class::<python_bindings::PyToolResult>()?;
    m.add_class::<python_bindings::PyToolSchema>()?;
    m.add_class::<python_bindings::PyIndexCoverage>()?;

    // Add module version
    m.add("__version__", env!("CARGO_PKG_VERSION"))?;

    Ok(())
}

#[cfg(feature = "python")]
pub mod python_bindings {
    use super::*;
    use crate::core::types::ScanResult;
    use crate::loregrep::{LoreGrep, LoreGrepBuilder};
    use pyo3::types::PyDict;
    use serde_json::Value;
    use std::sync::{Arc, Mutex};

    // ============================================================================
    // API PARITY MAP  (Rust `LoreGrep`/`LoreGrepBuilder` -> Python)
    //
    // These bindings are meant to be a COMPLETE mirror of the Rust public API.
    // Every `pub fn` on `LoreGrep` and `LoreGrepBuilder` in `src/loregrep.rs` is
    // either bound below or listed here with the reason it is not. If you add a
    // `pub fn` there, add it here too — this list is the audit trail that stops
    // the bindings from silently drifting behind the Rust surface again.
    //
    // Deliberately NOT exposed:
    //
    //   LoreGrep::save_index
    //   LoreGrep::load_index
    //   LoreGrep::load_index_if_fresh
    //   LoreGrep::is_cache_fresh
    //   LoreGrep::cache_path_for   (and whatever else this group becomes)
    //       The on-disk index cache. Deferred, not rejected: these signatures are
    //       actively being reworked, so binding them now would bind an API about
    //       to change. Bind them as a group once they settle.
    //
    //   (`with_go_analyzer` and `cache_ttl` are absent from BOTH surfaces now —
    //   they were no-ops, and a bound no-op tells an agent a feature exists and
    //   then fails silently. See the comments at their former sites in
    //   `src/loregrep.rs`. Re-binding them when they do something is additive.)
    //
    //   LoreGrep::coverage_handle
    //       Returns a `CoverageHandle`, internal plumbing that exists so the tool
    //       layer can share the coverage cell with `LoreGrep`. A Python host has
    //       nothing to wire it into; `index_coverage()` gives it the same data as
    //       a value.
    //
    // Everything else is bound below.
    // ============================================================================

    /// High-level Python API for LoreGrep - matches the Rust API exactly
    #[pyclass(name = "LoreGrep")]
    pub struct PyLoreGrep {
        inner: Arc<Mutex<LoreGrep>>,
    }

    #[pymethods]
    impl PyLoreGrep {
        /// Create a new LoreGrep builder for manual configuration
        #[staticmethod]
        fn builder() -> PyLoreGrepBuilder {
            PyLoreGrepBuilder {
                inner: LoreGrep::builder(),
            }
        }

        /// Zero-configuration setup with automatic project detection
        #[staticmethod]
        fn auto_discover(path: &str) -> PyResult<PyLoreGrep> {
            let loregrep = LoreGrep::auto_discover(path).map_err(|e| {
                PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                    "Auto-discovery failed: {}",
                    e
                ))
            })?;

            Ok(PyLoreGrep {
                inner: Arc::new(Mutex::new(loregrep)),
            })
        }

        /// Rust-optimized preset configuration
        #[staticmethod]
        fn rust_project(path: &str) -> PyResult<PyLoreGrep> {
            let loregrep = LoreGrep::rust_project(path).map_err(|e| {
                PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                    "Rust project setup failed: {}",
                    e
                ))
            })?;

            Ok(PyLoreGrep {
                inner: Arc::new(Mutex::new(loregrep)),
            })
        }

        /// Python-optimized preset configuration
        #[staticmethod]
        fn python_project(path: &str) -> PyResult<PyLoreGrep> {
            let loregrep = LoreGrep::python_project(path).map_err(|e| {
                PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                    "Python project setup failed: {}",
                    e
                ))
            })?;

            Ok(PyLoreGrep {
                inner: Arc::new(Mutex::new(loregrep)),
            })
        }

        /// Multi-language preset configuration
        #[staticmethod]
        fn polyglot_project(path: &str) -> PyResult<PyLoreGrep> {
            let loregrep = LoreGrep::polyglot_project(path).map_err(|e| {
                PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                    "Polyglot project setup failed: {}",
                    e
                ))
            })?;

            Ok(PyLoreGrep {
                inner: Arc::new(Mutex::new(loregrep)),
            })
        }

        /// Scan a repository and build the index
        fn scan<'py>(&self, py: Python<'py>, path: &str) -> PyResult<Bound<'py, PyAny>> {
            let inner = self.inner.clone();
            let path = path.to_string();

            pyo3_async_runtimes::tokio::future_into_py(py, async move {
                // Clone the LoreGrep to avoid holding the mutex guard across await
                let mut loregrep = {
                    let guard = inner.lock().map_err(|e| {
                        PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                            "Failed to acquire lock: {}",
                            e
                        ))
                    })?;
                    guard.clone()
                }; // mutex guard is dropped here

                let result = loregrep.scan(&path).await.map_err(|e| match e {
                    crate::LoreGrepError::IoError(io_err) => {
                        PyErr::new::<pyo3::exceptions::PyOSError, _>(format!(
                            "IO error during scan: {}",
                            io_err
                        ))
                    }
                    crate::LoreGrepError::AnalysisError(analysis_err) => {
                        PyErr::new::<pyo3::exceptions::PyValueError, _>(format!(
                            "Analysis error: {}",
                            analysis_err
                        ))
                    }
                    _ => PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                        "Scan failed: {}",
                        e
                    )),
                })?;

                // Update the shared state with the scanned data
                {
                    let mut guard = inner.lock().map_err(|e| {
                        PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                            "Failed to acquire lock for update: {}",
                            e
                        ))
                    })?;
                    *guard = loregrep;
                } // mutex guard is dropped here

                Ok(PyScanResult::from_scan_result(result))
            })
        }

        /// Execute one of the AI tools (see `get_tool_definitions`)
        fn execute_tool<'py>(
            &self,
            py: Python<'py>,
            tool_name: &str,
            args: &Bound<'py, PyDict>,
        ) -> PyResult<Bound<'py, PyAny>> {
            let inner = self.inner.clone();
            let tool_name = tool_name.to_string();

            // Convert PyDict to serde_json::Value with better error handling
            let args_json: Value = pythonize::depythonize(args).map_err(|e| {
                PyErr::new::<pyo3::exceptions::PyValueError, _>(format!(
                    "Invalid tool arguments - could not convert to JSON: {}",
                    e
                ))
            })?;

            pyo3_async_runtimes::tokio::future_into_py(py, async move {
                // Clone the LoreGrep to avoid holding the mutex guard across await
                let loregrep = {
                    let guard = inner.lock().map_err(|e| {
                        PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                            "Failed to acquire lock: {}",
                            e
                        ))
                    })?;
                    guard.clone()
                }; // mutex guard is dropped here

                let result =
                    loregrep
                        .execute_tool(&tool_name, args_json)
                        .await
                        .map_err(|e| match e {
                            crate::LoreGrepError::ToolError(tool_err) => {
                                PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                                    "Tool '{}' execution failed: {}",
                                    tool_name, tool_err
                                ))
                            }
                            crate::LoreGrepError::JsonError(json_err) => {
                                PyErr::new::<pyo3::exceptions::PyValueError, _>(format!(
                                    "Tool JSON error: {}",
                                    json_err
                                ))
                            }
                            crate::LoreGrepError::IoError(io_err) => {
                                PyErr::new::<pyo3::exceptions::PyOSError, _>(format!(
                                    "Tool IO error: {}",
                                    io_err
                                ))
                            }
                            _ => PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                                "Tool execution failed: {}",
                                e
                            )),
                        })?;

                // Convert ToolResult to Python-compatible format with better error handling
                let metadata_str = serde_json::to_string(&result.data).map_err(|e| {
                    PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                        "Failed to serialize tool result metadata: {}",
                        e
                    ))
                })?;

                let content = if result.success {
                    serde_json::to_string(&result.data).map_err(|e| {
                        PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                            "Failed to serialize tool result data: {}",
                            e
                        ))
                    })?
                } else {
                    result
                        .error
                        .unwrap_or_else(|| "Unknown tool error".to_string())
                };

                Ok(PyToolResult {
                    content,
                    metadata: metadata_str,
                })
            })
        }

        /// Get available tool definitions for AI systems
        #[staticmethod]
        fn get_tool_definitions() -> Vec<PyToolSchema> {
            LoreGrep::get_tool_definitions()
                .iter()
                .map(|schema| PyToolSchema {
                    name: schema.name.clone(),
                    description: schema.description.clone(),
                    parameters: serde_json::to_string(&schema.input_schema)
                        .unwrap_or_else(|_| "{}".to_string()),
                })
                .collect()
        }

        /// Get current version
        #[staticmethod]
        fn version() -> &'static str {
            env!("CARGO_PKG_VERSION")
        }

        /// Repository statistics for the current in-memory index.
        ///
        /// Unlike `scan()` this does not touch the filesystem; `duration_ms` is
        /// always 0.
        fn get_stats(&self) -> PyResult<PyScanResult> {
            let loregrep = self.lock()?;
            let stats = loregrep.get_stats().map_err(|e| {
                PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                    "Failed to read repository stats: {}",
                    e
                ))
            })?;
            Ok(PyScanResult::from_scan_result(stats))
        }

        /// How much of the repository the current index covers.
        ///
        /// Consult this before reporting "not found" to a user or an agent: a
        /// `max_files` limit can truncate the index, and an empty result set
        /// from a truncated index does not mean the symbol does not exist.
        fn index_coverage(&self) -> PyResult<PyIndexCoverage> {
            let coverage = self.lock()?.index_coverage();
            Ok(PyIndexCoverage {
                files_indexed: coverage.files_indexed,
                files_discovered: coverage.files_discovered,
                truncated: coverage.truncated,
                note: coverage.note(),
            })
        }

        /// Whether a repository has been scanned into this index.
        fn is_scanned(&self) -> PyResult<bool> {
            Ok(self.lock()?.is_scanned())
        }

        /// Record the analysis root on the index.
        ///
        /// A scan sets this itself, so this is only needed by hosts that
        /// populate an index by other means.
        fn set_scan_root(&self, root: &str) -> PyResult<()> {
            self.lock()?.set_scan_root(root);
            Ok(())
        }

        /// The first indexed file path that no longer exists on disk, if any.
        ///
        /// A cheap staleness probe: `None` means every indexed path still
        /// resolves under the recorded analysis root.
        fn first_missing_indexed_path(&self) -> PyResult<Option<String>> {
            Ok(self.lock()?.first_missing_indexed_path())
        }

        /// Reset the in-memory index to empty, including its coverage.
        ///
        /// `scan()` replaces the index wholesale, so this is not required before
        /// a rescan; use it to release the memory of an index you are done with
        /// while keeping the configured instance alive.
        fn clear_index(&self) -> PyResult<()> {
            self.lock()?.clear_index();
            Ok(())
        }

        fn __repr__(&self) -> String {
            "LoreGrep(configured and ready for repository analysis)".to_string()
        }
    }

    impl PyLoreGrep {
        /// Lock the shared instance, turning a poisoned mutex into a Python
        /// exception rather than a panic across the FFI boundary.
        fn lock(&self) -> PyResult<std::sync::MutexGuard<'_, LoreGrep>> {
            self.inner.lock().map_err(|e| {
                PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                    "Failed to acquire lock: {}",
                    e
                ))
            })
        }
    }

    /// Python wrapper for LoreGrepBuilder - enables fluent configuration
    #[pyclass(name = "LoreGrepBuilder")]
    pub struct PyLoreGrepBuilder {
        inner: LoreGrepBuilder,
    }

    #[pymethods]
    impl PyLoreGrepBuilder {
        /// Create a builder with the default configuration
        ///
        /// Equivalent to `LoreGrep.builder()`.
        #[new]
        fn new() -> Self {
            PyLoreGrepBuilder {
                inner: LoreGrepBuilder::new(),
            }
        }

        /// Set maximum file size to process
        fn max_file_size(mut slf: PyRefMut<Self>, size: u64) -> PyRefMut<Self> {
            slf.inner = slf.inner.clone().max_file_size(size);
            slf
        }

        /// Set maximum directory depth to scan
        fn max_depth(mut slf: PyRefMut<Self>, depth: u32) -> PyRefMut<Self> {
            slf.inner = slf.inner.clone().max_depth(depth);
            slf
        }

        /// Set maximum directory depth to unlimited
        fn unlimited_depth(mut slf: PyRefMut<Self>) -> PyRefMut<Self> {
            slf.inner = slf.inner.clone().unlimited_depth();
            slf
        }

        /// Set the maximum number of files to index
        ///
        /// A scan that hits this limit produces a TRUNCATED index; check
        /// `LoreGrep.index_coverage()` before treating an empty result as
        /// "does not exist".
        fn max_files(mut slf: PyRefMut<Self>, limit: usize) -> PyRefMut<Self> {
            slf.inner = slf.inner.clone().max_files(limit);
            slf
        }

        /// Set file patterns to include
        fn file_patterns(mut slf: PyRefMut<Self>, patterns: Vec<String>) -> PyRefMut<Self> {
            slf.inner = slf.inner.clone().file_patterns(patterns);
            slf
        }

        /// Set file patterns to include (same as `file_patterns`)
        fn include_patterns(mut slf: PyRefMut<Self>, patterns: Vec<String>) -> PyRefMut<Self> {
            slf.inner = slf.inner.clone().include_patterns(patterns);
            slf
        }

        /// Enable or disable following symbolic links while scanning
        fn follow_symlinks(mut slf: PyRefMut<Self>, follow: bool) -> PyRefMut<Self> {
            slf.inner = slf.inner.clone().follow_symlinks(follow);
            slf
        }

        /// Configure file patterns from a list of language names
        ///
        /// e.g. `["rust", "python"]` -> `*.rs`, `*.py`, ...
        fn configure_patterns_for_languages(
            mut slf: PyRefMut<Self>,
            languages: Vec<String>,
        ) -> PyRefMut<Self> {
            slf.inner = slf
                .inner
                .clone()
                .configure_patterns_for_languages(&languages);
            slf
        }

        /// Set patterns to exclude
        fn exclude_patterns(mut slf: PyRefMut<Self>, patterns: Vec<String>) -> PyRefMut<Self> {
            slf.inner = slf.inner.clone().exclude_patterns(patterns);
            slf
        }

        /// Enable or disable gitignore respect
        fn respect_gitignore(mut slf: PyRefMut<Self>, respect: bool) -> PyRefMut<Self> {
            slf.inner = slf.inner.clone().respect_gitignore(respect);
            slf
        }

        /// Add Rust language analyzer with feedback
        fn with_rust_analyzer(mut slf: PyRefMut<Self>) -> PyRefMut<Self> {
            slf.inner = slf.inner.clone().with_rust_analyzer();
            slf
        }

        /// Add Python language analyzer with feedback
        fn with_python_analyzer(mut slf: PyRefMut<Self>) -> PyRefMut<Self> {
            slf.inner = slf.inner.clone().with_python_analyzer();
            slf
        }

        /// Add TypeScript/TSX language analyzer
        fn with_typescript_analyzer(mut slf: PyRefMut<Self>) -> PyRefMut<Self> {
            slf.inner = slf.inner.clone().with_typescript_analyzer();
            slf
        }

        /// Enable all available analyzers
        fn with_all_analyzers(mut slf: PyRefMut<Self>) -> PyRefMut<Self> {
            slf.inner = slf.inner.clone().with_all_analyzers();
            slf
        }

        /// Optimize for performance (speed-focused configuration)
        fn optimize_for_performance(mut slf: PyRefMut<Self>) -> PyRefMut<Self> {
            slf.inner = slf.inner.clone().optimize_for_performance();
            slf
        }

        /// Comprehensive analysis (thorough configuration)
        fn comprehensive_analysis(mut slf: PyRefMut<Self>) -> PyRefMut<Self> {
            slf.inner = slf.inner.clone().comprehensive_analysis();
            slf
        }

        /// Exclude common build directories
        fn exclude_common_build_dirs(mut slf: PyRefMut<Self>) -> PyRefMut<Self> {
            slf.inner = slf.inner.clone().exclude_common_build_dirs();
            slf
        }

        /// Exclude test directories
        fn exclude_test_dirs(mut slf: PyRefMut<Self>) -> PyRefMut<Self> {
            slf.inner = slf.inner.clone().exclude_test_dirs();
            slf
        }

        /// Exclude vendor/dependency directories
        fn exclude_vendor_dirs(mut slf: PyRefMut<Self>) -> PyRefMut<Self> {
            slf.inner = slf.inner.clone().exclude_vendor_dirs();
            slf
        }

        /// Include common source files
        fn include_source_files(mut slf: PyRefMut<Self>) -> PyRefMut<Self> {
            slf.inner = slf.inner.clone().include_source_files();
            slf
        }

        /// Include configuration files
        fn include_config_files(mut slf: PyRefMut<Self>) -> PyRefMut<Self> {
            slf.inner = slf.inner.clone().include_config_files();
            slf
        }

        /// Build the configured LoreGrep instance
        fn build(slf: PyRefMut<Self>) -> PyResult<PyLoreGrep> {
            let loregrep = slf.inner.clone().build().map_err(|e| {
                PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!("Build failed: {}", e))
            })?;

            Ok(PyLoreGrep {
                inner: Arc::new(Mutex::new(loregrep)),
            })
        }

        fn __repr__(&self) -> String {
            "LoreGrepBuilder(configurable repository analyzer)".to_string()
        }
    }

    /// Python wrapper for ScanResult
    #[pyclass(name = "ScanResult")]
    pub struct PyScanResult {
        #[pyo3(get)]
        pub files_scanned: usize,
        #[pyo3(get)]
        pub functions_found: usize,
        #[pyo3(get)]
        pub structs_found: usize,
        #[pyo3(get)]
        pub errors: Vec<String>,
        #[pyo3(get)]
        pub duration_ms: u64,
        #[pyo3(get)]
        pub languages: Vec<String>,
    }

    impl PyScanResult {
        fn from_scan_result(result: ScanResult) -> Self {
            PyScanResult {
                files_scanned: result.files_scanned,
                functions_found: result.functions_found,
                structs_found: result.structs_found,
                errors: Vec::new(), // TODO: Collect actual errors from scan
                duration_ms: result.duration_ms,
                languages: result.languages,
            }
        }
    }

    #[pymethods]
    impl PyScanResult {
        fn __repr__(&self) -> String {
            format!(
                "ScanResult(files={}, functions={}, structs={}, duration={}ms)",
                self.files_scanned, self.functions_found, self.structs_found, self.duration_ms
            )
        }
    }

    /// Python wrapper for IndexCoverage - how much of the repository is indexed
    #[pyclass(name = "IndexCoverage")]
    pub struct PyIndexCoverage {
        /// Files actually analyzed and stored in the index
        #[pyo3(get)]
        pub files_indexed: usize,
        /// Files discovery found before any `max_files` limit was applied
        #[pyo3(get)]
        pub files_discovered: usize,
        /// True when a limit stopped the scan short of everything discovered
        #[pyo3(get)]
        pub truncated: bool,
        /// A human-readable coverage note, or None when the index is complete
        #[pyo3(get)]
        pub note: Option<String>,
    }

    #[pymethods]
    impl PyIndexCoverage {
        fn __repr__(&self) -> String {
            format!(
                "IndexCoverage(indexed={}, discovered={}, truncated={})",
                self.files_indexed,
                self.files_discovered,
                // Python spelling: this repr is read in a Python REPL.
                if self.truncated { "True" } else { "False" }
            )
        }
    }

    /// Python wrapper for ToolResult
    #[pyclass(name = "ToolResult")]
    pub struct PyToolResult {
        #[pyo3(get)]
        pub content: String,
        #[pyo3(get)]
        pub metadata: String,
    }

    #[pymethods]
    impl PyToolResult {
        fn __repr__(&self) -> String {
            format!("ToolResult(content_len={})", self.content.len())
        }
    }

    /// Python wrapper for ToolSchema
    #[pyclass(name = "ToolSchema")]
    pub struct PyToolSchema {
        #[pyo3(get)]
        pub name: String,
        #[pyo3(get)]
        pub description: String,
        #[pyo3(get)]
        pub parameters: String,
    }

    #[pymethods]
    impl PyToolSchema {
        fn __repr__(&self) -> String {
            format!("ToolSchema(name='{}')", self.name)
        }
    }
}

// Re-export Python types when python feature is enabled
#[cfg(feature = "python")]
pub use python_bindings::*;