cora-code 0.8.3

CLI-first AI code review — BYOK, diff/scan/branch, pre-commit hooks
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
//! Symbol index engine — persistent SQLite-backed symbol store.
//!
//! Build, query, and maintain a symbol index for code intelligence.
//! Uses regex-based extraction (same approach as `engine/context/extraction.rs`)
//! stored in SQLite with FTS5 for fast full-text search.

#[cfg(feature = "tree-sitter")]
mod ast;
pub mod brain;
mod extract;
pub mod graph;
pub mod schema;
mod symbols;
pub mod vector;

use std::collections::HashMap;
use std::path::Path;

use rusqlite::Connection;
use sha2::{Digest, Sha256};
use tracing::{debug, info};

#[allow(unused_imports)]
pub use graph::{CallEdge, CalleeResult, CallerResult, ImpactNode};
pub use symbols::{SearchResult, SymbolKind, SymbolQuery};

/// Open or create the **global** symbol index database.
///
/// All projects share a single SQLite database at `~/.codecora/cora-code/graph.db`.
/// Project isolation is handled via the `project_id` foreign key.
pub fn open_global_index() -> anyhow::Result<Connection> {
    crate::data_dir::ensure_data_dir()?;
    let db_path = crate::data_dir::graph_db_path();

    let conn = Connection::open(&db_path)?;
    conn.execute_batch("PRAGMA journal_mode=WAL; PRAGMA foreign_keys=ON;")?;
    schema::run_migrations(&conn)?;

    debug!("Opened global index at {}", db_path.display());
    Ok(conn)
}
/// Resolve the `project_id` for a given root path, creating the project row if needed.
pub fn ensure_project(conn: &Connection, root: &Path) -> anyhow::Result<i64> {
    let root_str = root.to_string_lossy().to_string();
    schema::get_or_create_project(conn, &root_str)
}

/// Detect the project root by walking up from `start` looking for marker files.
///
/// Search order: `.cora.yaml` → `Cargo.toml` → `package.json` → `.git` (dir or file).
/// Returns the directory containing the first marker found, or `None` if none is found.
pub fn resolve_project_root(start: &Path) -> Option<std::path::PathBuf> {
    let dir = if start.is_file() {
        start.parent()?
    } else {
        start
    };

    const MARKERS: &[&str] = &[".cora.yaml", "Cargo.toml", "package.json", ".git"];

    let mut current = dir.to_path_buf();
    loop {
        for marker in MARKERS {
            let candidate = current.join(marker);
            if candidate.exists() {
                debug!(root = %current.display(), marker, "detected project root");
                return Some(current);
            }
        }
        match current.parent() {
            Some(parent) if parent != current => current = parent.to_path_buf(),
            _ => return None,
        }
    }
}

/// Resolve `project_id` from the current directory, using project root detection.
///
/// Walks up from CWD to find a project root (`.cora.yaml`, `Cargo.toml`, etc.).
/// Falls back to CWD if no marker is found.
pub fn resolve_project_id(conn: &Connection) -> anyhow::Result<(i64, std::path::PathBuf)> {
    let cwd = std::env::current_dir()?;
    let root = resolve_project_root(&cwd).unwrap_or_else(|| cwd.clone());
    let project_id = ensure_project(conn, &root)?;
    Ok((project_id, root))
}

/// Index a single file: extract symbols and store in the database.
///
/// `project_id` is written to every row so data is scoped per-project
/// in the global database.
///
/// Returns the number of symbols indexed.
pub fn index_file(
    conn: &Connection,
    project_id: i64,
    file_path: &str,
    content: &str,
    language: &str,
) -> anyhow::Result<usize> {
    let fingerprint = file_fingerprint(content);
    let symbols = extract::extract_symbols(content, language, file_path);

    let tx = conn.unchecked_transaction()?;

    // Delete existing symbols for this file within this project
    tx.execute(
        "DELETE FROM symbols WHERE file = ?1 AND project_id = ?2",
        rusqlite::params![file_path, project_id],
    )?;

    // Upsert file fingerprint
    tx.execute(
        "INSERT INTO files (path, fingerprint, last_indexed, language, symbol_count, project_id)
         VALUES (?1, ?2, datetime('now'), ?3, ?4, ?5)
         ON CONFLICT(path) DO UPDATE SET
           fingerprint = excluded.fingerprint,
           last_indexed = excluded.last_indexed,
           language = excluded.language,
           symbol_count = excluded.symbol_count,
           project_id = excluded.project_id",
        rusqlite::params![
            file_path,
            fingerprint,
            language,
            symbols.len() as i64,
            project_id
        ],
    )?;

    // Insert symbols
    let mut count = 0;
    for sym in &symbols {
        tx.execute(
            "INSERT INTO symbols (name, kind, file, line, signature, language, project_id)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
            rusqlite::params![
                sym.name,
                sym.kind.as_str(),
                sym.file,
                sym.line as i64,
                sym.signature,
                language,
                project_id,
            ],
        )?;
        count += 1;
    }

    // Extract and store call graph edges
    graph::clear_edges_for_file(&tx, file_path, project_id)?;
    let call_sites = extract::extract_calls(content, language, file_path);
    for site in &call_sites {
        tx.execute(
            "INSERT INTO call_graph (caller, callee, file, line, project_id) VALUES (?1, ?2, ?3, ?4, ?5)",
            rusqlite::params![site.caller, site.callee, site.file, site.line as i64, project_id],
        )?;
    }

    #[cfg(feature = "tree-sitter")]
    {
        graph::clear_kg_edges_for_file(&tx, file_path, project_id)?;
        let kg_edges = extract::extract_edges(content, language, file_path);
        for e in &kg_edges {
            tx.execute(
                "INSERT INTO edges (source, kind, target, file, line, project_id) VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
                rusqlite::params![e.source, e.kind.as_str(), e.target, e.file, e.line as i64, project_id],
            )?;
        }
    }

    tx.commit()?;

    debug!(
        "Indexed {file_path}: {count} symbols, {} call edges ({language})",
        call_sites.len()
    );
    Ok(count)
}

/// Check if a file needs re-indexing based on content hash.
pub fn needs_reindex(conn: &Connection, project_id: i64, file_path: &str, content: &str) -> bool {
    let fingerprint = file_fingerprint(content);

    let stored: Option<String> = conn
        .query_row(
            "SELECT fingerprint FROM files WHERE path = ?1 AND project_id = ?2",
            rusqlite::params![file_path, project_id],
            |row| row.get(0),
        )
        .ok();

    match stored {
        Some(fp) => fp != fingerprint,
        None => true,
    }
}

/// Index a project directory, respecting .gitignore.
///
/// Returns summary stats.
pub fn index_project(conn: &Connection, root: &Path, verbose: bool) -> anyhow::Result<IndexStats> {
    let project_id = ensure_project(conn, root)?;
    index_project_with_id(conn, project_id, root, verbose)
}

/// Internal: index a project with an already-resolved `project_id`.
fn index_project_with_id(
    conn: &Connection,
    project_id: i64,
    root: &Path,
    verbose: bool,
) -> anyhow::Result<IndexStats> {
    let mut stats = IndexStats::default();

    let walker = ignore::WalkBuilder::new(root)
        .hidden(true)
        .git_ignore(true)
        .git_exclude(true)
        .build();

    for entry in walker {
        let entry = entry?;
        if !entry.file_type().is_some_and(|ft| ft.is_file()) {
            continue;
        }

        let path = entry.path();
        let rel = path.strip_prefix(root).unwrap_or(path);
        let rel_str = rel.to_string_lossy().to_string();

        let language = crate::engine::diff_parser::detect_language(&rel_str);
        if language == "unknown" || language == "text" {
            continue;
        }

        stats.files_scanned += 1;

        let content = match std::fs::read_to_string(path) {
            Ok(c) => c,
            Err(_) => continue,
        };

        if !needs_reindex(conn, project_id, &rel_str, &content) {
            stats.files_skipped += 1;
            continue;
        }

        stats.files_indexed += 1;
        match index_file(conn, project_id, &rel_str, &content, language) {
            Ok(n) => stats.symbols_indexed += n,
            Err(e) => {
                stats.errors += 1;
                if verbose {
                    eprintln!("  ⚠ Failed to index {rel_str}: {e}");
                }
            }
        }
    }

    // Update project's last_indexed timestamp
    conn.execute(
        "UPDATE projects SET last_indexed = datetime('now') WHERE id = ?1",
        rusqlite::params![project_id],
    )?;

    info!(
        "Index complete: {} files scanned, {} indexed, {} symbols, {} errors",
        stats.files_scanned, stats.files_indexed, stats.symbols_indexed, stats.errors
    );

    // Embed symbols into vector index for brain search
    match brain::embed_project(conn, project_id) {
        Ok(n) => {
            stats.embedded_symbols = Some(n);
            info!("Brain: embedded {n} symbols");
        }
        Err(e) => {
            if verbose {
                eprintln!("  ⚠ Embedding failed (non-fatal): {e}");
            }
            tracing::warn!("Embedding failed: {e}");
        }
    }

    Ok(stats)
}

/// Search the symbol index using FTS5 full-text search, scoped to a project.
pub fn search(
    conn: &Connection,
    project_id: i64,
    query: &SymbolQuery,
) -> anyhow::Result<Vec<SearchResult>> {
    symbols::search(conn, project_id, query)
}

/// Get index statistics for a specific project.
pub fn index_stats(conn: &Connection, project_id: i64) -> anyhow::Result<IndexSummary> {
    let total_symbols: i64 = conn.query_row(
        "SELECT COUNT(*) FROM symbols WHERE project_id = ?1",
        rusqlite::params![project_id],
        |row| row.get(0),
    )?;
    let total_files: i64 = conn.query_row(
        "SELECT COUNT(*) FROM files WHERE project_id = ?1",
        rusqlite::params![project_id],
        |row| row.get(0),
    )?;
    let db_size: i64 = {
        let page_size: i64 = conn
            .query_row("PRAGMA page_size", [], |row| row.get(0))
            .unwrap_or(4096);
        let page_count: i64 = conn
            .query_row("PRAGMA page_count", [], |row| row.get(0))
            .unwrap_or(0);
        page_size * page_count
    };

    let mut kind_counts: HashMap<String, usize> = HashMap::new();
    let mut stmt =
        conn.prepare("SELECT kind, COUNT(*) FROM symbols WHERE project_id = ?1 GROUP BY kind")?;
    let rows = stmt.query_map(rusqlite::params![project_id], |row| {
        Ok((row.get::<_, String>(0)?, row.get::<_, i64>(1)? as usize))
    })?;
    for row in rows {
        let (kind, count) = row?;
        kind_counts.insert(kind, count);
    }

    let mut lang_counts: HashMap<String, usize> = HashMap::new();
    let mut stmt = conn.prepare(
        "SELECT language, COUNT(*) FROM symbols WHERE project_id = ?1 GROUP BY language",
    )?;
    let rows = stmt.query_map(rusqlite::params![project_id], |row| {
        Ok((row.get::<_, String>(0)?, row.get::<_, i64>(1)? as usize))
    })?;
    for row in rows {
        let (lang, count) = row?;
        lang_counts.insert(lang, count);
    }

    Ok(IndexSummary {
        total_symbols: total_symbols as usize,
        total_files: total_files as usize,
        db_size_bytes: db_size as u64,
        symbols_by_kind: kind_counts,
        symbols_by_language: lang_counts,
    })
}

/// Remove symbols for files that no longer exist on disk, scoped to a project.
pub fn prune_deleted(conn: &Connection, project_id: i64, root: &Path) -> anyhow::Result<usize> {
    let mut deleted = 0;

    let mut stmt = conn.prepare("SELECT path FROM files WHERE project_id = ?1")?;
    let paths: Vec<String> = stmt
        .query_map(rusqlite::params![project_id], |row| row.get::<_, String>(0))?
        .filter_map(|r| r.ok())
        .collect();

    let to_prune: Vec<&String> = paths
        .iter()
        .filter(|path| !root.join(path).exists())
        .collect();

    if !to_prune.is_empty() {
        let tx = conn.unchecked_transaction()?;
        for path in &to_prune {
            tx.execute(
                "DELETE FROM symbols WHERE file = ?1 AND project_id = ?2",
                rusqlite::params![path, project_id],
            )?;
            tx.execute(
                "DELETE FROM call_graph WHERE file = ?1 AND project_id = ?2",
                rusqlite::params![path, project_id],
            )?;
            tx.execute(
                "DELETE FROM files WHERE path = ?1 AND project_id = ?2",
                rusqlite::params![path, project_id],
            )?;
        }
        tx.commit()?;
        deleted = to_prune.len();
    }

    if deleted > 0 {
        info!("Pruned {deleted} deleted files from index");
    }

    Ok(deleted)
}

/// Compute a SHA-256 fingerprint for file content.
fn file_fingerprint(content: &str) -> String {
    let mut hasher = Sha256::new();
    hasher.update(content.as_bytes());
    format!("{:x}", hasher.finalize())
}

/// Statistics from an index build run.
#[derive(Debug, Clone, Default)]
pub struct IndexStats {
    pub files_scanned: usize,
    pub files_indexed: usize,
    pub files_skipped: usize,
    pub symbols_indexed: usize,
    pub errors: usize,
    pub embedded_symbols: Option<usize>,
}

/// Summary of the current index state.
#[derive(Debug, Clone)]
pub struct IndexSummary {
    pub total_symbols: usize,
    pub total_files: usize,
    pub db_size_bytes: u64,
    pub symbols_by_kind: HashMap<String, usize>,
    pub symbols_by_language: HashMap<String, usize>,
}

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

    fn mem_conn() -> Connection {
        let conn = Connection::open_in_memory().unwrap();
        conn.execute_batch("PRAGMA foreign_keys=ON;").unwrap();
        schema::run_migrations(&conn).unwrap();
        conn
    }

    /// Create a test project and return its project_id.
    fn test_project(conn: &Connection) -> i64 {
        schema::get_or_create_project(conn, "/tmp/test-project").unwrap()
    }

    #[test]
    fn test_open_and_migrate() {
        let conn = mem_conn();
        // Tables should exist
        let count: i64 = conn
            .query_row("SELECT COUNT(*) FROM symbols", [], |row| row.get(0))
            .unwrap();
        assert_eq!(count, 0);
    }

    #[test]
    fn test_index_rust_file() {
        let conn = mem_conn();
        let project_id = test_project(&conn);
        let code = r#"
use std::collections::HashMap;

pub struct Cache {
    inner: HashMap<String, String>,
}

impl Cache {
    pub fn new() -> Self {
        Self { inner: HashMap::new() }
    }

    pub fn get(&self, key: &str) -> Option<&String> {
        self.inner.get(key)
    }
}
"#;
        let count = index_file(&conn, project_id, "src/cache.rs", code, "rs").unwrap();
        assert!(count > 0, "Should extract symbols from Rust code");
    }

    #[test]
    fn test_needs_reindex() {
        let conn = mem_conn();
        let project_id = test_project(&conn);
        let code = "fn hello() {}";

        // First time → needs reindex
        assert!(needs_reindex(&conn, project_id, "test.rs", code));

        // Index it
        index_file(&conn, project_id, "test.rs", code, "rs").unwrap();

        // Same content → no reindex needed
        assert!(!needs_reindex(&conn, project_id, "test.rs", code));

        // Changed content → needs reindex
        assert!(needs_reindex(&conn, project_id, "test.rs", "fn world() {}"));
    }

    #[test]
    fn test_search() {
        let conn = mem_conn();
        let project_id = test_project(&conn);
        let code = r#"
pub fn authenticate(token: &str) -> bool {
    false
}

pub struct AuthService {
    secret: String,
}
"#;
        index_file(&conn, project_id, "src/auth.rs", code, "rs").unwrap();

        let query = SymbolQuery::text("authenticate");
        let results = search(&conn, project_id, &query).unwrap();
        assert!(!results.is_empty());
        assert!(results[0].symbol.name.contains("authenticate"));
    }

    #[test]
    fn test_index_stats() {
        let conn = mem_conn();
        let project_id = test_project(&conn);
        index_file(&conn, project_id, "a.rs", "fn foo() {}", "rs").unwrap();
        index_file(&conn, project_id, "b.rs", "struct Bar {}", "rs").unwrap();

        let stats = index_stats(&conn, project_id).unwrap();
        assert!(stats.total_symbols >= 2);
        assert_eq!(stats.total_files, 2);
        assert!(stats.symbols_by_kind.contains_key("function"));
        assert!(stats.symbols_by_kind.contains_key("struct"));
    }

    #[test]
    fn test_prune_deleted() {
        let conn = mem_conn();
        let project_id = test_project(&conn);
        index_file(&conn, project_id, "gone.rs", "fn removed() {}", "rs").unwrap();

        // Create temp dir to use as root
        let tmp = tempfile::tempdir().unwrap();
        // gone.rs doesn't exist in temp dir → should be pruned
        let deleted = prune_deleted(&conn, project_id, tmp.path()).unwrap();
        assert_eq!(deleted, 1);

        let stats = index_stats(&conn, project_id).unwrap();
        assert_eq!(stats.total_symbols, 0);
    }

    #[test]
    fn test_reindex_replaces_symbols() {
        let conn = mem_conn();
        let project_id = test_project(&conn);
        index_file(&conn, project_id, "test.rs", "fn old_name() {}", "rs").unwrap();
        index_file(&conn, project_id, "test.rs", "fn new_name() {}", "rs").unwrap();

        let stats = index_stats(&conn, project_id).unwrap();
        // Should have 1 symbol (replaced, not 2)
        assert_eq!(stats.total_symbols, 1);
    }

    #[test]
    fn test_resolve_project_root_finds_cargo_toml() {
        // CWD of the test process is the crate root — Cargo.toml exists here.
        let cwd = std::env::current_dir().unwrap();
        let root = resolve_project_root(&cwd);
        assert!(root.is_some(), "should find project root from CWD");
        let root = root.unwrap();
        assert!(
            root.join("Cargo.toml").exists(),
            "resolved root should contain Cargo.toml"
        );
    }

    #[test]
    fn test_resolve_project_root_finds_cora_yaml() {
        let tmp = tempfile::TempDir::new().unwrap();
        let root = tmp.path().to_path_buf();
        std::fs::write(root.join(".cora.yaml"), "version: 1\n").unwrap();
        let subdir = root.join("src").join("deep").join("nested");
        std::fs::create_dir_all(&subdir).unwrap();
        let resolved = resolve_project_root(&subdir);
        assert_eq!(resolved, Some(root));
    }

    #[test]
    fn test_resolve_project_root_returns_none_in_tmp() {
        // /tmp itself should have no markers (or at least a very deep /tmp subdirectory
        // that we create and then check).
        let tmp = tempfile::TempDir::new().unwrap();
        // Create a deep directory with no markers.
        let deep = tmp.path().join("a").join("b").join("c");
        std::fs::create_dir_all(&deep).unwrap();
        // Walk up from `deep` — tempfile dirs are under /tmp which typically has no
        // Cargo.toml/.cora.yaml/.git. If it does find one we just skip this assertion.
        let resolved = resolve_project_root(&deep);
        // /tmp should not have project markers, but if the test machine has a git
        // repo at /tmp, we can't guarantee this. Only assert if /tmp is clean.
        if !std::path::Path::new("/tmp/.git").exists()
            && !std::path::Path::new("/tmp/Cargo.toml").exists()
            && !std::path::Path::new("/tmp/.cora.yaml").exists()
        {
            assert!(
                resolved.is_none(),
                "should not find project root in empty tmp dir, got {resolved:?}"
            );
        }
    }

    #[test]
    fn test_resolve_project_id_uses_project_root() {
        let conn = mem_conn();
        // resolve_project_id uses CWD — which is the cora-code crate root.
        let (pid, root) = resolve_project_id(&conn).unwrap();
        assert!(pid > 0);
        assert!(
            root.join("Cargo.toml").exists(),
            "resolved root should contain Cargo.toml"
        );
    }
}