leindex 1.6.0

LeIndex MCP and semantic code search engine for AI tools and large codebases
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
//! Global project registry using local SQLite
//!
//! Provides persistent storage for discovered projects with automatic reconnection.

use crate::global::DEFAULT_DB_PATH;
use crate::storage::UniqueProjectId;
use rusqlite::{params, Connection};
use std::path::{Path, PathBuf};
use thiserror::Error;

/// Errors that can occur in the global registry
#[derive(Debug, Error)]
pub enum GlobalRegistryError {
    /// Database error
    #[error("Database error: {0}")]
    Database(#[from] rusqlite::Error),

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

    /// Project not found
    #[error("Project not found: {0}")]
    NotFound(String),

    /// Invalid project ID
    #[error("Invalid project ID: {0}")]
    InvalidId(String),
}

/// Result type for registry operations
pub type Result<T> = std::result::Result<T, GlobalRegistryError>;

/// Project information stored in the registry
#[derive(Debug, Clone)]
pub struct ProjectInfo {
    /// Unique project identifier
    pub unique_id: UniqueProjectId,
    /// Base name of the project
    pub base_name: String,
    /// Path to the project
    pub path: PathBuf,
    /// Detected language
    pub language: Option<String>,
    /// Number of source files
    pub file_count: usize,
    /// Content fingerprint for clone detection
    pub content_fingerprint: String,
    /// Whether this is a clone
    pub is_clone: bool,
    /// Original project ID if this is a clone
    pub cloned_from: Option<String>,
    /// When the project was registered
    pub registered_at: i64,
    /// Last modified timestamp (as i64 for storage)
    pub last_modified: Option<i64>,
}

/// Global project registry
///
/// Manages persistent storage of discovered projects using local SQLite
pub struct GlobalRegistry {
    /// Database connection
    conn: Connection,
    /// Path to the database file
    db_path: PathBuf,
}

impl GlobalRegistry {
    /// Initialize a new global registry
    ///
    /// Creates the database and schema if they don't exist
    ///
    /// # Arguments
    ///
    /// * `db_path` - Path to the database file (default: ~/.leindex/global.db)
    ///
    /// # Returns
    ///
    /// `Result<Self>` - The initialized registry
    pub fn init<P: AsRef<Path>>(db_path: P) -> Result<Self> {
        let db_path = db_path.as_ref().to_path_buf();

        // Ensure parent directory exists
        if let Some(parent) = db_path.parent() {
            std::fs::create_dir_all(parent)?;
        }

        let conn = Connection::open(&db_path)?;

        let mut registry = Self { conn, db_path };

        registry.initialize_schema()?;

        Ok(registry)
    }

    /// Initialize with default path
    ///
    /// # Returns
    ///
    /// `Result<Self>` - The initialized registry at ~/.leindex/global.db
    pub fn init_default() -> Result<Self> {
        let home = std::env::var("HOME")
            .or_else(|_| std::env::var("USERPROFILE"))
            .unwrap_or_else(|_| ".".to_string());
        let path = PathBuf::from(home).join(DEFAULT_DB_PATH.trim_start_matches('.'));
        Self::init(path)
    }

    /// Initialize the database schema
    fn initialize_schema(&mut self) -> Result<()> {
        // Enable WAL mode for better concurrency
        self.conn.pragma_update(None, "journal_mode", "WAL")?;

        // Set cache size
        self.conn.pragma_update(None, "cache_size", 10000i64)?;

        // Create global_projects table
        self.conn.execute(
            r#"
            CREATE TABLE IF NOT EXISTS global_projects (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                unique_project_id TEXT UNIQUE NOT NULL,
                base_name TEXT NOT NULL,
                canonical_path TEXT NOT NULL UNIQUE,
                language TEXT,
                file_count INTEGER DEFAULT 0,
                content_fingerprint TEXT NOT NULL,
                is_clone BOOLEAN DEFAULT 0,
                cloned_from TEXT,
                registered_at INTEGER NOT NULL,
                last_modified INTEGER,
                path_hash TEXT NOT NULL,
                instance INTEGER DEFAULT 0
            )
            "#,
            [],
        )?;

        // Create indexes for common queries
        let indexes = [
            "CREATE INDEX IF NOT EXISTS idx_global_projects_unique_id ON global_projects(unique_project_id)",
            "CREATE INDEX IF NOT EXISTS idx_global_projects_base_name ON global_projects(base_name)",
            "CREATE INDEX IF NOT EXISTS idx_global_projects_fingerprint ON global_projects(content_fingerprint)",
            "CREATE INDEX IF NOT EXISTS idx_global_projects_language ON global_projects(language)",
        ];

        for index_sql in indexes {
            self.conn.execute(index_sql, [])?;
        }

        Ok(())
    }

    /// Register a project in the registry
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the project
    /// * `language` - Detected language (optional)
    /// * `file_count` - Number of source files
    /// * `content_fingerprint` - Content fingerprint for clone detection
    ///
    /// # Returns
    ///
    /// `Result<String>` - The unique project ID
    pub fn register_project(
        &mut self,
        path: &Path,
        language: Option<String>,
        file_count: usize,
        content_fingerprint: &str,
    ) -> Result<String> {
        // Load existing IDs for conflict detection
        let base_name = path
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("unknown");

        let existing_ids = self.load_existing_ids(base_name)?;
        let unique_id = UniqueProjectId::generate(path, &existing_ids);

        // Check for clones by content fingerprint
        let is_clone;
        let cloned_from;

        if let Some(existing) = self.find_by_fingerprint(content_fingerprint)? {
            is_clone = true;
            cloned_from = Some(existing.unique_id.to_string());
        } else {
            is_clone = false;
            cloned_from = None;
        }

        let canonical_path = path
            .canonicalize()
            .map(|p| p.to_string_lossy().to_string())
            .unwrap_or_else(|_| path.to_string_lossy().to_string());

        let path_hash = blake3::hash(canonical_path.as_bytes()).to_hex()[..8].to_string();

        let registered_now = chrono::Utc::now();
        let registered_at = registered_now.timestamp();

        let last_modified = path
            .metadata()
            .ok()
            .and_then(|m| m.modified().ok())
            .and_then(|t| {
                t.duration_since(std::time::UNIX_EPOCH)
                    .ok()
                    .map(|d| d.as_secs() as i64)
            });

        self.conn.execute(
            r#"
            INSERT INTO global_projects
            (unique_project_id, base_name, canonical_path, language, file_count,
             content_fingerprint, is_clone, cloned_from, registered_at, last_modified,
             path_hash, instance)
            VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12)
            "#,
            params![
                unique_id.to_string(),
                base_name,
                canonical_path,
                language,
                file_count as i64,
                content_fingerprint,
                is_clone,
                cloned_from,
                registered_at,
                last_modified,
                path_hash,
                unique_id.instance,
            ],
        )?;

        Ok(unique_id.to_string())
    }

    /// List all projects in the registry
    ///
    /// # Returns
    ///
    /// `Result<Vec<ProjectInfo>>` - All registered projects
    pub fn list_projects(&self) -> Result<Vec<ProjectInfo>> {
        let mut stmt = self.conn.prepare(
            r#"
            SELECT unique_project_id, base_name, canonical_path, language, file_count,
                   content_fingerprint, is_clone, cloned_from, registered_at, last_modified
            FROM global_projects
            ORDER BY base_name
            "#,
        )?;

        let projects = stmt
            .query_map([], |row| {
                let id_str: String = row.get(0)?;
                let unique_id = UniqueProjectId::parse_id(&id_str)
                    .ok_or_else(|| rusqlite::Error::InvalidQuery)?;

                Ok(ProjectInfo {
                    unique_id,
                    base_name: row.get(1)?,
                    path: PathBuf::from(row.get::<_, String>(2)?),
                    language: row.get(3)?,
                    file_count: row.get::<_, i64>(4)? as usize,
                    content_fingerprint: row.get(5)?,
                    is_clone: row.get(6)?,
                    cloned_from: row.get(7)?,
                    registered_at: row.get(8)?,
                    last_modified: row.get::<_, Option<i64>>(9)?,
                })
            })?
            .collect::<std::result::Result<Vec<_>, _>>()?;

        Ok(projects)
    }

    /// Get a project by its unique ID
    ///
    /// # Arguments
    ///
    /// * `id` - The unique project ID
    ///
    /// # Returns
    ///
    /// `Result<Option<ProjectInfo>>` - The project if found
    pub fn get_project(&self, id: &str) -> Result<Option<ProjectInfo>> {
        let mut stmt = self.conn.prepare(
            r#"
            SELECT unique_project_id, base_name, canonical_path, language, file_count,
                   content_fingerprint, is_clone, cloned_from, registered_at, last_modified
            FROM global_projects
            WHERE unique_project_id = ?1
            "#,
        )?;

        let result = stmt.query_row(params![id], |row| {
            let id_str: String = row.get(0)?;
            let unique_id =
                UniqueProjectId::parse_id(&id_str).ok_or_else(|| rusqlite::Error::InvalidQuery)?;

            Ok(ProjectInfo {
                unique_id,
                base_name: row.get(1)?,
                path: PathBuf::from(row.get::<_, String>(2)?),
                language: row.get(3)?,
                file_count: row.get::<_, i64>(4)? as usize,
                content_fingerprint: row.get(5)?,
                is_clone: row.get(6)?,
                cloned_from: row.get(7)?,
                registered_at: row.get(8)?,
                last_modified: row.get::<_, Option<i64>>(9)?,
            })
        });

        match result {
            Ok(p) => Ok(Some(p)),
            Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
            Err(e) => Err(e.into()),
        }
    }

    /// Delete a project from the registry
    ///
    /// # Arguments
    ///
    /// * `id` - The unique project ID
    pub fn delete_project(&mut self, id: &str) -> Result<()> {
        let rows_affected = self.conn.execute(
            "DELETE FROM global_projects WHERE unique_project_id = ?1",
            params![id],
        )?;

        if rows_affected == 0 {
            return Err(GlobalRegistryError::NotFound(id.to_string()));
        }

        Ok(())
    }

    /// Find a project by content fingerprint (for clone detection)
    ///
    /// # Arguments
    ///
    /// * `fingerprint` - The content fingerprint to search for
    ///
    /// # Returns
    ///
    /// `Result<Option<ProjectInfo>>` - The first project with this fingerprint
    fn find_by_fingerprint(&self, fingerprint: &str) -> Result<Option<ProjectInfo>> {
        let mut stmt = self.conn.prepare(
            r#"
            SELECT unique_project_id, base_name, canonical_path, language, file_count,
                   content_fingerprint, is_clone, cloned_from, registered_at, last_modified
            FROM global_projects
            WHERE content_fingerprint = ?1
            LIMIT 1
            "#,
        )?;

        let result = stmt.query_row(params![fingerprint], |row| {
            let id_str: String = row.get(0)?;
            let unique_id =
                UniqueProjectId::parse_id(&id_str).ok_or_else(|| rusqlite::Error::InvalidQuery)?;

            Ok(ProjectInfo {
                unique_id,
                base_name: row.get(1)?,
                path: PathBuf::from(row.get::<_, String>(2)?),
                language: row.get(3)?,
                file_count: row.get::<_, i64>(4)? as usize,
                content_fingerprint: row.get(5)?,
                is_clone: row.get(6)?,
                cloned_from: row.get(7)?,
                registered_at: row.get(8)?,
                last_modified: row.get::<_, Option<i64>>(9)?,
            })
        });

        match result {
            Ok(p) => Ok(Some(p)),
            Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
            Err(e) => Err(e.into()),
        }
    }

    /// Load existing project IDs for a given base name
    fn load_existing_ids(&self, base_name: &str) -> Result<Vec<UniqueProjectId>> {
        let mut stmt = self
            .conn
            .prepare("SELECT unique_project_id FROM global_projects WHERE base_name = ?1")?;

        let ids: Vec<UniqueProjectId> = stmt
            .query_map(params![base_name], |row| {
                let id_str: String = row.get(0)?;
                UniqueProjectId::parse_id(&id_str).ok_or_else(|| rusqlite::Error::InvalidQuery)
            })?
            .collect::<std::result::Result<Vec<_>, _>>()?;

        Ok(ids)
    }

    /// Check database connection health
    ///
    /// # Returns
    ///
    /// `bool` - true if connection is healthy
    #[must_use]
    pub fn is_healthy(&self) -> bool {
        self.conn
            .query_row("SELECT 1", [], |_row| Ok(()))
            .map(|_| true)
            .unwrap_or(false)
    }

    /// Get the database path
    #[must_use]
    pub const fn db_path(&self) -> &PathBuf {
        &self.db_path
    }

    /// Get a reference to the underlying connection
    #[must_use]
    pub fn conn(&self) -> &Connection {
        &self.conn
    }
}

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

    #[test]
    fn test_registry_init() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("test.db");

        let registry = GlobalRegistry::init(&db_path);

        assert!(registry.is_ok());
        assert!(registry.unwrap().is_healthy());
    }

    #[test]
    fn test_registry_init_default() {
        // Just test that it doesn't crash
        let result = GlobalRegistry::init_default();
        // It might fail if HOME is not writable, which is ok
        assert!(result.is_ok() || result.is_err());
    }

    #[test]
    fn test_register_project() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("test.db");
        let mut registry = GlobalRegistry::init(&db_path).unwrap();

        // Create a temp project
        let project_path = temp_dir.path().join("test-project");
        std::fs::create_dir_all(project_path.join(".git")).unwrap();

        let id = registry.register_project(
            &project_path,
            Some("rust".to_string()),
            42,
            "test-fingerprint",
        );

        assert!(id.is_ok());
        let project_id = id.unwrap();
        assert!(project_id.contains("test-project"));
    }

    #[test]
    fn test_register_and_get_project() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("test.db");
        let mut registry = GlobalRegistry::init(&db_path).unwrap();

        let project_path = temp_dir.path().join("myproject");
        std::fs::create_dir_all(project_path.join(".git")).unwrap();

        let id = registry
            .register_project(&project_path, Some("rust".to_string()), 100, "fp123")
            .unwrap();

        let project = registry.get_project(&id).unwrap();

        assert!(project.is_some());
        let info = project.unwrap();
        assert_eq!(info.base_name, "myproject");
        assert_eq!(info.language, Some("rust".to_string()));
        assert_eq!(info.file_count, 100);
        assert!(!info.is_clone);
    }

    #[test]
    fn test_list_projects() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("test.db");
        let mut registry = GlobalRegistry::init(&db_path).unwrap();

        // Register a few projects
        for i in 0..3 {
            let path = temp_dir.path().join(format!("project{}", i));
            std::fs::create_dir_all(path.join(".git")).unwrap();
            registry
                .register_project(&path, Some("rust".to_string()), 10 + i, &format!("fp{}", i))
                .unwrap();
        }

        let projects = registry.list_projects().unwrap();

        assert_eq!(projects.len(), 3);
        assert!(projects
            .iter()
            .all(|p| p.language == Some("rust".to_string())));
    }

    #[test]
    fn test_list_projects_propagates_invalid_row() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("test.db");
        let registry = GlobalRegistry::init(&db_path).unwrap();

        registry
            .conn
            .execute(
                r#"
                INSERT INTO global_projects (
                    unique_project_id, base_name, canonical_path, language, file_count,
                    content_fingerprint, is_clone, cloned_from, registered_at, last_modified,
                    path_hash, instance
                ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12)
                "#,
                params![
                    "invalid-id",
                    "broken",
                    "/tmp/broken",
                    Option::<String>::None,
                    0i64,
                    "fp",
                    false,
                    Option::<String>::None,
                    0i64,
                    Option::<i64>::None,
                    "deadbeef",
                    0i64,
                ],
            )
            .unwrap();

        let err = registry.list_projects().unwrap_err();
        assert!(matches!(err, GlobalRegistryError::Database(_)));
    }

    #[test]
    fn test_load_existing_ids_propagates_invalid_row() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("test.db");
        let registry = GlobalRegistry::init(&db_path).unwrap();

        registry
            .conn
            .execute(
                r#"
                INSERT INTO global_projects (
                    unique_project_id, base_name, canonical_path, language, file_count,
                    content_fingerprint, is_clone, cloned_from, registered_at, last_modified,
                    path_hash, instance
                ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12)
                "#,
                params![
                    "invalid-id",
                    "broken",
                    "/tmp/broken",
                    Option::<String>::None,
                    0i64,
                    "fp",
                    false,
                    Option::<String>::None,
                    0i64,
                    Option::<i64>::None,
                    "deadbeef",
                    0i64,
                ],
            )
            .unwrap();

        let err = registry.load_existing_ids("broken").unwrap_err();
        assert!(matches!(err, GlobalRegistryError::Database(_)));
    }

    #[test]
    fn test_delete_project() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("test.db");
        let mut registry = GlobalRegistry::init(&db_path).unwrap();

        let project_path = temp_dir.path().join("todelete");
        std::fs::create_dir_all(project_path.join(".git")).unwrap();

        let id = registry
            .register_project(&project_path, None, 5, "fp")
            .unwrap();

        // Delete it
        assert!(registry.delete_project(&id).is_ok());

        // Should be gone
        let result = registry.get_project(&id).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn test_clone_detection() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("test.db");
        let mut registry = GlobalRegistry::init(&db_path).unwrap();

        // Register original project
        let path1 = temp_dir.path().join("original");
        std::fs::create_dir_all(path1.join(".git")).unwrap();
        registry
            .register_project(&path1, Some("rust".to_string()), 50, "same-fp")
            .unwrap();

        // Register clone with same fingerprint
        let path2 = temp_dir.path().join("clone");
        std::fs::create_dir_all(path2.join(".git")).unwrap();
        registry
            .register_project(&path2, Some("rust".to_string()), 50, "same-fp")
            .unwrap();

        let projects = registry.list_projects().unwrap();
        let clone = projects.iter().find(|p| p.base_name == "clone").unwrap();

        assert!(clone.is_clone);
        assert!(clone.cloned_from.is_some());
    }

    #[test]
    fn test_is_healthy() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("test.db");
        let registry = GlobalRegistry::init(&db_path).unwrap();

        assert!(registry.is_healthy());
    }
}