cora-code 0.17.2

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
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
//! 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 queries;
pub mod schema;
pub mod session;
mod symbols;
pub mod vector;

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

use rayon::prelude::*;
use rusqlite::Connection;
#[cfg(test)]
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/cora.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()?;
    open_index_at(&crate::data_dir::graph_db_path())
}

/// Open (creating if absent) the index database at `db_path`, apply the
/// standard PRAGMAs and run migrations.
///
/// This is the single place that opens an index connection; the production path
/// goes through [`open_global_index`], tests may point it at a temp file.
pub fn open_index_at(db_path: &Path) -> anyhow::Result<Connection> {
    let conn = Connection::open(db_path)?;
    // One opener at a time per process: PRAGMA journal_mode and migrations on
    // the same file must not interleave (#604).
    let _guard = schema::open_lock();
    apply_pragmas(&conn)?;
    schema::run_migrations_locked(&conn)?;

    debug!("Opened index at {}", db_path.display());
    Ok(conn)
}

/// The one PRAGMA set every read-write index connection uses.
pub fn apply_pragmas(conn: &Connection) -> anyhow::Result<()> {
    // First: wait for a competing connection instead of failing with
    // "database is locked" while switching the journal mode.
    conn.busy_timeout(std::time::Duration::from_secs(10))?;
    conn.execute_batch(
        "PRAGMA journal_mode=WAL;\
         PRAGMA foreign_keys=ON;\
         PRAGMA synchronous=NORMAL;\
         PRAGMA cache_size=-65536;\
         PRAGMA mmap_size=268435456;\
         PRAGMA temp_store=MEMORY;",
    )?;
    Ok(())
}

/// 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.
///
/// Resolution order:
/// 1. `.cora.yaml` — explicit user override, always wins immediately.
/// 2. A `Cargo.toml` declaring a `[workspace]` section — a Rust workspace root
///    beats a nested member crate's plain `Cargo.toml`, so indexing from inside
///    `crates/*` and resolving from the repo root land on the same project (#522).
/// 3. The first plain marker (`Cargo.toml`, `package.json`, `.git`) as fallback.
///
/// The walk never climbs past a git repository boundary, so an unrelated
/// `[workspace]` outside the repo cannot hijack resolution.
pub fn resolve_project_root(start: &Path) -> Option<std::path::PathBuf> {
    let dir = if start.is_file() {
        start.parent()?
    } else {
        start
    };

    let mut current = dir.to_path_buf();
    let mut fallback: Option<std::path::PathBuf> = None;
    loop {
        // 1. Explicit cora config wins immediately.
        if current.join(".cora.yaml").is_file() {
            debug!(root = %current.display(), marker = ".cora.yaml", "detected project root");
            return Some(current);
        }

        // 2. Cargo workspace root beats a nested member crate manifest.
        let cargo_toml = current.join("Cargo.toml");
        if cargo_toml.is_file()
            && std::fs::read_to_string(&cargo_toml)
                .map(|s| s.contains("[workspace"))
                .unwrap_or(false)
        {
            debug!(root = %current.display(), marker = "[workspace] Cargo.toml", "detected project root");
            return Some(current);
        }

        // 3. First plain marker is the fallback (original behavior).
        if fallback.is_none() {
            const MARKERS: &[&str] = &["Cargo.toml", "package.json", ".git"];
            for marker in MARKERS {
                if current.join(marker).exists() {
                    fallback = Some(current.clone());
                    break;
                }
            }
        }

        // Repo boundary: stop AFTER giving this directory its own chance to
        // match above (a repo root can legitimately be the workspace root).
        if current.join(".git").exists() {
            break;
        }
        match current.parent() {
            Some(parent) if parent != current => current = parent.to_path_buf(),
            _ => break,
        }
    }

    if let Some(root) = &fallback {
        debug!(root = %root.display(), "detected project root");
    }
    fallback
}

#[cfg(test)]
/// Index a single file: extract symbols and store in the database.
/// Test-only — production uses `index_project_with_id` with batch fingerprinting.
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 extracted = extract::extract_all(content, language, file_path);

    let tx = conn.unchecked_transaction()?;
    let count = index_file_in_tx(
        &tx,
        project_id,
        file_path,
        &fingerprint,
        language,
        &extracted,
    )?;

    tx.commit()?;

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

/// Write extracted data for a single file within an existing transaction.
/// Does NOT commit — caller is responsible for `tx.commit()`.
fn index_file_in_tx(
    tx: &rusqlite::Transaction,
    project_id: i64,
    file_path: &str,
    fingerprint: &str,
    language: &str,
    extracted: &extract::ExtractedAll,
) -> anyhow::Result<usize> {
    // 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(project_id, path) DO UPDATE SET
           fingerprint = excluded.fingerprint,
           last_indexed = excluded.last_indexed,
           language = excluded.language,
           symbol_count = excluded.symbol_count",
        rusqlite::params![
            file_path,
            fingerprint,
            language,
            extracted.symbols.len() as i64,
            project_id
        ],
    )?;

    // Batch INSERT symbols using prepared statement
    let count = extracted.symbols.len();
    if !extracted.symbols.is_empty() {
        let mut stmt = tx.prepare(
            "INSERT INTO symbols (name, kind, file, line, signature, language, project_id)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
        )?;
        for sym in &extracted.symbols {
            stmt.execute(rusqlite::params![
                sym.name,
                sym.kind.as_str(),
                sym.file,
                sym.line as i64,
                sym.signature,
                language,
                project_id,
            ])?;
        }
    }

    // Clear and insert call graph edges
    tx.execute(
        "DELETE FROM call_graph WHERE file = ?1 AND project_id = ?2",
        rusqlite::params![file_path, project_id],
    )?;
    if !extracted.calls.is_empty() {
        let mut stmt = tx.prepare(
            "INSERT INTO call_graph (caller, callee, file, line, project_id) VALUES (?1, ?2, ?3, ?4, ?5)"
        )?;
        for site in &extracted.calls {
            stmt.execute(rusqlite::params![
                site.caller,
                site.callee,
                site.file,
                site.line as i64,
                project_id,
            ])?;
        }
    }

    // Clear and insert knowledge graph edges (tree-sitter only)
    #[cfg(feature = "tree-sitter")]
    {
        tx.execute(
            "DELETE FROM edges WHERE file = ?1 AND project_id = ?2",
            rusqlite::params![file_path, project_id],
        )?;
        if !extracted.kg_edges.is_empty() {
            let mut stmt = tx.prepare(
                "INSERT INTO edges (source, kind, target, file, line, project_id) VALUES (?1, ?2, ?3, ?4, ?5, ?6)"
            )?;
            for e in &extracted.kg_edges {
                stmt.execute(rusqlite::params![
                    e.source,
                    e.kind.as_str(),
                    e.target,
                    e.file,
                    e.line as i64,
                    project_id,
                ])?;
            }
        }
    }

    Ok(count)
}

/// Load all file fingerprints for a project in a single query.
/// Returns `HashMap<file_path, fingerprint>` — O(1) lookup per file
/// instead of N individual DB roundtrips.
fn load_all_fingerprints(
    conn: &Connection,
    project_id: i64,
) -> anyhow::Result<HashMap<String, String>> {
    let mut stmt = conn.prepare("SELECT path, fingerprint FROM files WHERE project_id = ?1")?;
    let map: HashMap<String, String> = stmt
        .query_map([project_id], |row| {
            Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?))
        })?
        .filter_map(|r| r.ok())
        .collect();
    Ok(map)
}

/// Exclusion patterns for indexing = review's `ignore.files` plus
/// `index.skip_files`, so every index entry point (index, watch, serve) honors
/// the same ignores (#521). `None` when no config could be loaded.
pub fn skip_patterns_from_config(
    config: Option<&crate::config::schema::Config>,
) -> Option<Vec<String>> {
    config.map(|c| {
        let mut pats = c.ignore.files.clone();
        pats.extend(c.rules_config.index_skip_files.iter().cloned());
        pats.dedup();
        pats
    })
}

/// Index a project directory with NO skip patterns, respecting .gitignore.
/// Test-only: production entry points must go through
/// `index_project_with_skip` with the resolved config.
#[cfg(test)]
pub fn index_project(conn: &Connection, root: &Path, verbose: bool) -> anyhow::Result<IndexStats> {
    index_project_with_id(conn, ensure_project(conn, root)?, root, verbose, None, None)
}

/// Index a project directory, honoring skip patterns (glob `*`/`**`, matched
/// against paths relative to the project root).
///
/// Patterns do two things:
/// 1. Files matching are EXCLUDED from indexing entirely (#521), so index
///    consumers like dead-code and review's index scanners never see them.
/// 2. The pattern list is hashed into the DB; a change forces a full
///    re-index so previously indexed-but-now-excluded files get purged.
pub fn index_project_with_skip(
    conn: &Connection,
    root: &Path,
    verbose: bool,
    skip_patterns: Option<&[String]>,
) -> anyhow::Result<IndexStats> {
    index_project_filtered(conn, root, verbose, skip_patterns, None)
}

/// [`index_project_with_skip`] restricted to files for which `include`
/// (called with the root-relative path) returns true. Files that fail the
/// predicate are neither read nor re-indexed, but still count as present on
/// disk, so their stored rows are not pruned. `None` means every file.
pub fn index_project_filtered(
    conn: &Connection,
    root: &Path,
    verbose: bool,
    skip_patterns: Option<&[String]>,
    include: Option<&dyn Fn(&str) -> bool>,
) -> anyhow::Result<IndexStats> {
    let project_id = ensure_project(conn, root)?;

    // Config hash invalidation: if skip_patterns changed, clear fingerprints
    if let Some(patterns) = skip_patterns {
        let new_hash = schema::compute_index_config_hash(patterns);
        let stored_hash: Option<String> = conn
            .query_row(
                "SELECT index_config_hash FROM projects WHERE id = ?1",
                rusqlite::params![project_id],
                |row| row.get(0),
            )
            .ok()
            .flatten();

        if stored_hash.as_deref() != Some(&new_hash) {
            if verbose {
                eprintln!("  ↻ Config changed — invalidating fingerprints for full re-index");
            }
            // Clear all fingerprints for this project to force re-index
            conn.execute(
                "DELETE FROM files WHERE project_id = ?1",
                rusqlite::params![project_id],
            )?;
            // Also clear symbols/edges so they're rebuilt from scratch
            conn.execute(
                "DELETE FROM symbols WHERE project_id = ?1",
                rusqlite::params![project_id],
            )?;
            conn.execute(
                "DELETE FROM edges WHERE project_id = ?1",
                rusqlite::params![project_id],
            )?;
            conn.execute(
                "DELETE FROM call_graph WHERE project_id = ?1",
                rusqlite::params![project_id],
            )?;
            // Store the new config hash
            conn.execute(
                "UPDATE projects SET index_config_hash = ?1 WHERE id = ?2",
                rusqlite::params![new_hash, project_id],
            )?;
        }
    }

    index_project_with_id(conn, project_id, root, verbose, skip_patterns, include)
}

/// Internal: index a project with an already-resolved `project_id`.
fn index_project_with_id(
    conn: &Connection,
    project_id: i64,
    root: &Path,
    verbose: bool,
    skip_patterns: Option<&[String]>,
    include: Option<&dyn Fn(&str) -> bool>,
) -> anyhow::Result<IndexStats> {
    let mut stats = IndexStats::default();
    let skip_matcher = skip_patterns.map(crate::engine::path_match::PathMatcher::new);
    // Every indexable file seen on disk this run (post language + skip
    // filters). Anything stored for the project but absent here is stale.
    let mut walked: std::collections::HashSet<String> = std::collections::HashSet::new();

    // Collect files to index
    let mut files_to_index: Vec<(String, String, String, String)> = Vec::new(); // (rel_str, content, language, cheap_fp)

    // Batch-load all fingerprints in ONE query — eliminates N per-file DB roundtrips.
    let stored_fingerprints = load_all_fingerprints(conn, project_id).unwrap_or_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;
        }

        // Config-driven exclusion (#521): honor ignore.files /
        // index_skip_files so dead-code, review index scanners, and brain
        // never see these files.
        if skip_matcher.as_ref().is_some_and(|m| m.is_match(&rel_str)) {
            stats.files_excluded += 1;
            continue;
        }

        stats.files_scanned += 1;
        walked.insert(rel_str.clone());

        // Caller-supplied restriction (watch --filter / changed files only).
        if include.is_some_and(|f| !f(&rel_str)) {
            continue;
        }

        // Compute mtime:size fingerprint — cheap, no file read needed.
        // metadata() is a stat() call, ~microseconds per file.
        let metadata = match std::fs::metadata(path) {
            Ok(m) => m,
            Err(_) => continue,
        };
        let mtime = metadata
            .modified()
            .ok()
            .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
            .map(|d| d.as_secs())
            .unwrap_or(0);
        let size = metadata.len();
        let cheap_fp = format!("{mtime}:{size}");

        // O(1) hashmap lookup — skip file entirely if unchanged
        if let Some(stored) = stored_fingerprints.get(&rel_str) {
            if *stored == cheap_fp {
                stats.files_skipped += 1;
                continue;
            }
        }

        // Only read file content when we know it changed
        let content = match std::fs::read_to_string(path) {
            Ok(c) => c,
            Err(_) => continue,
        };

        files_to_index.push((rel_str, content, language.to_string(), cheap_fp));
    }

    if !files_to_index.is_empty() {
        // ── Phase 1: Parallel extraction (CPU-bound tree-sitter + regex) ───
        // Rayon par_iter distributes file parsing across all CPU cores.
        // extract_all is CPU-bound (~4ms/file) and fully thread-safe.
        let t_extract = std::time::Instant::now();
        let extracted_files: Vec<(String, extract::ExtractedAll, String, String)> = files_to_index
            .par_iter()
            .map(|(rel_str, content, language, cheap_fp)| {
                let mut extracted = extract::extract_all(content, language, rel_str);
                // Detect HTTP route edges and append to kg_edges
                extracted
                    .kg_edges
                    .extend(extract::detect_routes(content, language, rel_str));
                (
                    rel_str.clone(),
                    extracted,
                    language.clone(),
                    cheap_fp.clone(),
                )
            })
            .collect();
        let extract_ms = t_extract.elapsed().as_millis();

        // ── Phase 2: Serial SQLite writes (single transaction) ─────────
        let t_db = std::time::Instant::now();
        let tx = conn.unchecked_transaction()?;

        // Disable FTS5 triggers during bulk insert to avoid 3x write amplification.
        tx.execute_batch(
            "DROP TRIGGER IF EXISTS symbols_fts_insert;\
             DROP TRIGGER IF EXISTS symbols_fts_delete;\
             DROP TRIGGER IF EXISTS symbols_fts_update;",
        )?;

        for (rel_str, extracted, language, cheap_fp) in &extracted_files {
            match index_file_in_tx(&tx, project_id, rel_str, cheap_fp, language, extracted) {
                Ok(n) => {
                    stats.files_indexed += 1;
                    stats.symbols_indexed += n;
                }
                Err(e) => {
                    stats.errors += 1;
                    if verbose {
                        eprintln!("  ⚠ Failed to index {rel_str}: {e}");
                    }
                }
            }
        }

        // Rebuild FTS5 index from the content table in one shot.
        // 'rebuild' tells FTS5 to discard its data and re-read from the
        // external content table (symbols) — much cheaper than per-row triggers.
        tx.execute("INSERT INTO symbols_fts(symbols_fts) VALUES('rebuild')", [])?;

        // Recreate FTS5 triggers for incremental updates after bulk load
        tx.execute_batch(
            "CREATE TRIGGER IF NOT EXISTS symbols_fts_insert
             AFTER INSERT ON symbols
             BEGIN
                 INSERT INTO symbols_fts(rowid, name, signature, file)
                 VALUES (new.id, new.name, new.signature, new.file);
             END;

             CREATE TRIGGER IF NOT EXISTS symbols_fts_delete
             AFTER DELETE ON symbols
             BEGIN
                 INSERT INTO symbols_fts(symbols_fts, rowid, name, signature, file)
                 VALUES ('delete', old.id, old.name, old.signature, old.file);
             END;

             CREATE TRIGGER IF NOT EXISTS symbols_fts_update
             AFTER UPDATE ON symbols
             BEGIN
                 INSERT INTO symbols_fts(symbols_fts, rowid, name, signature, file)
                 VALUES ('delete', old.id, old.name, old.signature, old.file);
                 INSERT INTO symbols_fts(rowid, name, signature, file)
                 VALUES (new.id, new.name, new.signature, new.file);
             END;",
        )?;

        tx.commit()?;
        tracing::debug!(
            "extract={}ms (rayon), db={}ms, files={}",
            extract_ms,
            t_db.elapsed().as_millis(),
            files_to_index.len()
        );
    }

    // Drop rows for files deleted, gitignored, or newly excluded since the
    // last run so symbols/call graph/edges never outlive their source file.
    let stale: Vec<String> = stored_fingerprints
        .keys()
        .filter(|p| !walked.contains(*p))
        .cloned()
        .collect();
    stats.files_pruned = prune_paths(conn, project_id, &stale)?;

    // 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 — when files changed,
    // or when the stored index cannot serve keyed search after reload
    // (legacy pre-0.3.0 vecq file, vecq #542: if all files are unchanged this
    // would be the only chance to re-embed — without it the vector signal
    // stays dead until a file actually changes).
    if stats.files_indexed > 0 || stats.files_pruned > 0 || brain::vector_index_needs_rebuild() {
        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 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
        .into_iter()
        .filter(|path| !root.join(path).exists())
        .collect();

    prune_paths(conn, project_id, &to_prune)
}

/// Delete all index data (symbols, call graph, edges, file row) for the given
/// project-relative paths in a single transaction. Returns the number of files.
fn prune_paths(conn: &Connection, project_id: i64, paths: &[String]) -> anyhow::Result<usize> {
    if paths.is_empty() {
        return Ok(0);
    }

    let tx = conn.unchecked_transaction()?;
    for path in paths {
        for table in ["symbols", "call_graph", "edges"] {
            tx.execute(
                &format!("DELETE FROM {table} 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()?;

    info!("Pruned {} deleted files from index", paths.len());
    Ok(paths.len())
}

#[cfg(test)]
/// Compute a SHA-256 fingerprint for file content (test-only).
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,
    /// Files excluded by config skip patterns (ignore.files / index.skip_files).
    pub files_excluded: usize,
    /// Stale files (deleted / ignored / newly excluded) removed from the index.
    pub files_pruned: 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_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);
    }

    /// Regression (#521): skip patterns must EXCLUDE files from indexing
    /// (previously they only invalidated fingerprints), so dead-code and
    /// review's index scanners stop reporting matches from ignored dirs.
    #[test]
    fn test_skip_patterns_exclude_files_from_index() {
        let conn = mem_conn();
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().to_path_buf();
        std::fs::create_dir(root.join("examples")).unwrap();
        std::fs::write(
            root.join("examples").join("demo.py"),
            "def client_method():\n    pass\n",
        )
        .unwrap();
        std::fs::write(root.join("core.py"), "def core_fn():\n    pass\n").unwrap();

        // First run: no patterns → everything indexed.
        let stats = index_project_with_skip(&conn, &root, false, Some(&[])).unwrap();
        assert_eq!(stats.files_scanned, 2);
        assert_eq!(stats.files_excluded, 0);

        // Second run WITH a pattern: examples/ excluded via full re-index…
        let pats = vec!["examples/**".to_string()];
        let stats = index_project_with_skip(&conn, &root, false, Some(&pats)).unwrap();
        assert_eq!(stats.files_excluded, 1);
        assert!(stats.files_indexed > 0, "remaining files get re-indexed");

        let pid = ensure_project(&conn, &root).unwrap();
        let summary = index_stats(&conn, pid).unwrap();
        assert_eq!(
            summary.total_files, 1,
            "only core.py remains indexed after exclusion"
        );
    }

    #[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:?}"
            );
        }
    }

    /// Regression (#522): running `cora index` from inside a workspace member
    /// crate must resolve to the WORKSPACE root (the member's plain
    /// `Cargo.toml` is not the project root), so CLI and MCP agree on one
    /// project_id instead of silently creating two.
    #[test]
    fn test_resolve_project_root_prefers_workspace_root() {
        let tmp = tempfile::TempDir::new().unwrap();
        let ws = tmp.path().join("ws");
        let member = ws.join("crates").join("app");
        std::fs::create_dir_all(&member).unwrap();

        std::fs::write(
            ws.join("Cargo.toml"),
            "[workspace]\nmembers = [\"crates/*\"]\n",
        )
        .unwrap();
        std::fs::write(member.join("Cargo.toml"), "[package]\nname = \"app\"\n").unwrap();
        std::fs::write(member.join("src.rs"), "fn main() {}\n").unwrap();

        let resolved = resolve_project_root(&member);
        assert_eq!(
            resolved.as_deref(),
            Some(ws.as_path()),
            "workspace root should win over a member crate's plain Cargo.toml"
        );
    }

    /// An explicit `.cora.yaml` anywhere along the walk always wins — it is a
    /// deliberate user override of project-root detection.
    #[test]
    fn test_resolve_project_root_cora_yaml_wins_over_workspace() {
        let tmp = tempfile::TempDir::new().unwrap();
        let ws = tmp.path().join("ws");
        let member = ws.join("crates").join("app");
        std::fs::create_dir_all(&member).unwrap();

        std::fs::write(
            ws.join("Cargo.toml"),
            "[workspace]\nmembers = [\"crates/*\"]\n",
        )
        .unwrap();
        std::fs::write(ws.join(".cora.yaml"), "version: 1\n").unwrap();
        std::fs::write(member.join("Cargo.toml"), "[package]\nname = \"app\"\n").unwrap();
        std::fs::write(member.join(".cora.yaml"), "version: 1\n").unwrap();

        let resolved = resolve_project_root(&member);
        assert_eq!(resolved.as_deref(), Some(member.as_path()));
    }

    /// Root detection must not climb above a git repository boundary: an
    /// unrelated `[workspace]` Cargo.toml outside the repo must never hijack
    /// resolution.
    #[test]
    fn test_resolve_project_root_stops_at_git_boundary() {
        let tmp = tempfile::TempDir::new().unwrap();
        let outer_ws = tmp.path().join("outer");
        let repo = outer_ws.join("myrepo");
        std::fs::create_dir_all(repo.join(".git")).unwrap();

        std::fs::write(
            outer_ws.join("Cargo.toml"),
            "[workspace]\nmembers = [\"*\"]\n",
        )
        .unwrap();
        // Repo itself has no markers other than .git and one plain file dir.
        let deep = repo.join("src");
        std::fs::create_dir_all(&deep).unwrap();

        let resolved = resolve_project_root(&deep);
        assert_eq!(
            resolved.as_deref(),
            Some(repo.as_path()),
            ".git must stop the upward walk"
        );
    }

    /// Regression (#522): an incremental no-op re-index (all fingerprints
    /// match) must keep reporting the STORED symbol count — DB state must
    /// survive untouched re-runs.
    #[test]
    fn test_incremental_index_preserves_counts() {
        let conn = mem_conn();
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().to_path_buf();
        std::fs::write(root.join("lib.rs"), "pub fn alpha() {} pub fn beta() {}\n").unwrap();

        let first = index_project(&conn, &root, false).unwrap();
        assert_eq!(first.files_indexed, 1);
        assert!(first.symbols_indexed > 0);

        // Second run: everything unchanged → skipped, nothing wiped.
        let second = index_project(&conn, &root, false).unwrap();
        assert_eq!(second.files_skipped, 1);
        assert_eq!(second.files_indexed, 0);

        let summary = index_stats(&conn, ensure_project(&conn, &root).unwrap()).unwrap();
        assert_eq!(
            summary.total_symbols as usize, first.symbols_indexed,
            "stored symbols must survive an incremental no-op re-run"
        );
        assert_eq!(summary.total_files, 1);
    }

    /// Regression: `files` was keyed by `path` alone, so two projects with
    /// the same relative path overwrote each other's row (fingerprint +
    /// project_id) and both re-indexed forever.
    #[test]
    fn test_same_relative_path_in_two_projects() {
        let conn = mem_conn();
        let mut roots = Vec::new();
        for body in ["pub fn alpha() {}\n", "pub fn beta() {}\n"] {
            let tmp = tempfile::tempdir().unwrap();
            std::fs::write(tmp.path().join("lib.rs"), body).unwrap();
            roots.push(tmp);
        }

        for tmp in &roots {
            let s = index_project(&conn, tmp.path(), false).unwrap();
            assert_eq!(s.files_indexed, 1);
        }
        // Both projects must now be fully up to date.
        for tmp in &roots {
            let s = index_project(&conn, tmp.path(), false).unwrap();
            assert_eq!(s.files_indexed, 0, "unchanged project must not re-index");
            assert_eq!(s.files_skipped, 1);
            let pid = ensure_project(&conn, tmp.path()).unwrap();
            assert_eq!(index_stats(&conn, pid).unwrap().total_files, 1);
        }
        let rows: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM files WHERE path = 'lib.rs'",
                [],
                |r| r.get(0),
            )
            .unwrap();
        assert_eq!(rows, 2);
    }

    fn count_for_file(conn: &Connection, table: &str, project_id: i64, file: &str) -> i64 {
        let col = if table == "files" { "path" } else { "file" };
        conn.query_row(
            &format!("SELECT COUNT(*) FROM {table} WHERE {col} = ?1 AND project_id = ?2"),
            rusqlite::params![file, project_id],
            |r| r.get(0),
        )
        .unwrap()
    }

    /// Regression: stale files were only pruned via `--prune`; a normal index
    /// run must drop symbols, call_graph, edges and the file row.
    #[test]
    fn test_index_run_auto_prunes_deleted_files() {
        let conn = mem_conn();
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path();
        std::fs::write(root.join("keep.rs"), "pub fn keep() { helper(); }\n").unwrap();
        std::fs::write(root.join("gone.rs"), "pub fn gone() { helper(); }\n").unwrap();

        index_project(&conn, root, false).unwrap();
        let pid = ensure_project(&conn, root).unwrap();
        // Seed an edge row directly so the check holds without tree-sitter.
        for f in ["keep.rs", "gone.rs"] {
            conn.execute(
                "INSERT INTO edges (source, kind, target, file, line, project_id)
                 VALUES ('a', 'CALLS', 'b', ?1, 1, ?2)",
                rusqlite::params![f, pid],
            )
            .unwrap();
        }
        assert!(count_for_file(&conn, "symbols", pid, "gone.rs") > 0);
        assert!(count_for_file(&conn, "call_graph", pid, "gone.rs") > 0);

        std::fs::remove_file(root.join("gone.rs")).unwrap();
        let stats = index_project(&conn, root, false).unwrap();
        assert_eq!(stats.files_pruned, 1);

        for table in ["symbols", "call_graph", "edges", "files"] {
            assert_eq!(count_for_file(&conn, table, pid, "gone.rs"), 0, "{table}");
        }
        assert!(count_for_file(&conn, "symbols", pid, "keep.rs") > 0);
        assert_eq!(count_for_file(&conn, "edges", pid, "keep.rs"), 1);
    }

    /// `prune_deleted` (the `--prune` path) must also clear `edges`.
    #[test]
    fn test_prune_deleted_clears_edges() {
        let conn = mem_conn();
        let pid = test_project(&conn);
        index_file(&conn, pid, "gone.rs", "fn removed() {}", "rs").unwrap();
        conn.execute(
            "INSERT INTO edges (source, kind, target, file, line, project_id)
             VALUES ('a', 'CALLS', 'b', 'gone.rs', 1, ?1)",
            [pid],
        )
        .unwrap();
        let tmp = tempfile::tempdir().unwrap();
        assert_eq!(prune_deleted(&conn, pid, tmp.path()).unwrap(), 1);
        assert_eq!(count_for_file(&conn, "edges", pid, "gone.rs"), 0);
    }

    /// Regression: serve/watch called the no-skip entry point, undoing
    /// `ignore.files` / `index.skip_files`. Patterns come from one shared
    /// resolver and the skip-aware entry point must keep excluded files out.
    #[test]
    fn test_skip_patterns_from_config_and_exclusion() {
        let mut config = crate::config::schema::Config::default();
        config.ignore.files = vec!["examples/**".to_string()];
        config.rules_config.index_skip_files = vec!["vendor/**".to_string()];
        let pats = skip_patterns_from_config(Some(&config)).unwrap();
        assert!(pats.contains(&"examples/**".to_string()));
        assert!(pats.contains(&"vendor/**".to_string()));
        assert!(skip_patterns_from_config(None).is_none());

        let conn = mem_conn();
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path();
        std::fs::create_dir(root.join("examples")).unwrap();
        std::fs::write(
            root.join("examples").join("demo.py"),
            "def demo():\n    pass\n",
        )
        .unwrap();
        std::fs::write(root.join("core.py"), "def core_fn():\n    pass\n").unwrap();

        let stats = index_project_with_skip(&conn, root, false, Some(&pats)).unwrap();
        assert_eq!(stats.files_excluded, 1);
        let pid = ensure_project(&conn, root).unwrap();
        assert_eq!(count_for_file(&conn, "files", pid, "examples/demo.py"), 0);
        assert_eq!(count_for_file(&conn, "files", pid, "core.py"), 1);
    }
}