semantex-core 1.1.0

Core library for semantex semantic code search (indexing, embeddings, search)
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
//! Index validation: consistency checks between meta.json, SQLite, dense index, and filesystem.

use crate::config::SemantexConfig;
use crate::index::storage::ChunkStore;
use crate::types::IndexMeta;
use anyhow::{Context, Result};
use std::path::Path;

/// Result of a single validation check.
#[derive(Debug, Clone)]
pub struct CheckResult {
    pub name: String,
    pub passed: bool,
    pub message: String,
}

/// Full validation report.
#[derive(Debug, Clone)]
pub struct ValidationReport {
    pub checks: Vec<CheckResult>,
    pub passed: usize,
    pub failed: usize,
    pub warnings: usize,
}

impl ValidationReport {
    pub fn all_passed(&self) -> bool {
        self.failed == 0
    }

    pub fn summary(&self) -> String {
        format!("{}/{} checks passed", self.passed, self.checks.len())
    }
}

/// Run all validation checks for a project's index.
pub fn validate(project_path: &Path) -> Result<ValidationReport> {
    let index_dir = SemantexConfig::project_index_dir(project_path);
    let checks = vec![
        check_meta_db_consistency(&index_dir),
        check_stale_files(&index_dir, project_path),
        check_dense_index(&index_dir),
        check_sparse_index(&index_dir),
        check_graph_consistency(&index_dir),
    ];

    let passed = checks.iter().filter(|c| c.passed).count();
    let failed = checks.iter().filter(|c| !c.passed).count();

    Ok(ValidationReport {
        checks,
        passed,
        failed,
        warnings: 0,
    })
}

/// Check 1: Meta-DB consistency — compare meta.json counts with SQLite.
fn check_meta_db_consistency(index_dir: &Path) -> CheckResult {
    let name = "meta_db_consistency".to_string();

    let meta_path = index_dir.join("meta.json");
    let meta = match read_meta(&meta_path) {
        Ok(m) => m,
        Err(e) => {
            return CheckResult {
                name,
                passed: false,
                message: format!("Cannot read meta.json: {e}"),
            };
        }
    };

    // Check schema version
    if meta.schema_version != IndexMeta::CURRENT_SCHEMA_VERSION {
        return CheckResult {
            name,
            passed: false,
            message: format!(
                "Schema version mismatch: meta has {}, expected {}",
                meta.schema_version,
                IndexMeta::CURRENT_SCHEMA_VERSION
            ),
        };
    }

    let db_path = index_dir.join("chunks.db");
    let store = match ChunkStore::open_for_search(&db_path) {
        Ok(s) => s,
        Err(e) => {
            return CheckResult {
                name,
                passed: false,
                message: format!("Cannot open chunks.db: {e}"),
            };
        }
    };

    let db_file_count = store.file_count().unwrap_or(0);
    let db_chunk_count = store.chunk_count().unwrap_or(0);

    let file_match = db_file_count == meta.file_count;
    // Chunks within 5% tolerance (incremental indexing can cause minor drift)
    let chunk_tolerance = (meta.chunk_count as f64 * 0.05).max(1.0) as u64;
    let chunk_match = db_chunk_count.abs_diff(meta.chunk_count) <= chunk_tolerance;

    if file_match && chunk_match {
        CheckResult {
            name,
            passed: true,
            message: format!(
                "OK: files={db_file_count} (meta: {}), chunks={db_chunk_count} (meta: {}), schema=v{}",
                meta.file_count, meta.chunk_count, meta.schema_version
            ),
        }
    } else {
        let mut issues = Vec::new();
        if !file_match {
            issues.push(format!(
                "file count mismatch: DB={db_file_count}, meta={}",
                meta.file_count
            ));
        }
        if !chunk_match {
            issues.push(format!(
                "chunk count drift: DB={db_chunk_count}, meta={} (>{chunk_tolerance} tolerance)",
                meta.chunk_count
            ));
        }
        CheckResult {
            name,
            passed: false,
            message: issues.join("; "),
        }
    }
}

/// Check 2: Stale file detection — files deleted or modified since indexing.
fn check_stale_files(index_dir: &Path, project_path: &Path) -> CheckResult {
    let name = "stale_files".to_string();

    let db_path = index_dir.join("chunks.db");
    let store = match ChunkStore::open_for_search(&db_path) {
        Ok(s) => s,
        Err(e) => {
            return CheckResult {
                name,
                passed: false,
                message: format!("Cannot open chunks.db: {e}"),
            };
        }
    };

    let file_paths = match store.get_all_file_paths() {
        Ok(p) => p,
        Err(e) => {
            return CheckResult {
                name,
                passed: false,
                message: format!("Cannot read file paths from DB: {e}"),
            };
        }
    };

    let total_files = file_paths.len();
    let mut missing_count = 0;

    for rel_path in &file_paths {
        let abs_path = project_path.join(rel_path);
        if !abs_path.exists() {
            missing_count += 1;
        }
    }

    // Sample up to 100 files for mtime comparison
    let mtime_sample = store.get_file_mtimes_sample(100).unwrap_or_default();

    let mut stale_count = 0;
    for (rel_path, stored_mtime) in &mtime_sample {
        let abs_path = project_path.join(rel_path);
        if let Ok(metadata) = std::fs::metadata(&abs_path)
            && let Ok(fs_mtime) = metadata.modified()
        {
            let fs_mtime_secs = fs_mtime
                .duration_since(std::time::UNIX_EPOCH)
                .map_or(0, |d| d.as_secs() as i64);
            if fs_mtime_secs != *stored_mtime {
                stale_count += 1;
            }
        }
    }

    let passed = missing_count == 0 && stale_count == 0;
    let mut parts = vec![format!("{total_files} indexed files")];
    if missing_count > 0 {
        parts.push(format!("{missing_count} missing from disk"));
    }
    if stale_count > 0 {
        parts.push(format!(
            "{stale_count}/{} sampled files have changed mtime",
            mtime_sample.len()
        ));
    }
    if passed {
        parts.push("all present, mtime sample OK".to_string());
    }

    CheckResult {
        name,
        passed,
        message: parts.join(", "),
    }
}

/// Check 3: Dense index integrity — the coderank-hnsw vector store sentinel
/// (`dense/coderank-hnsw/index.bin`).
fn check_dense_index(index_dir: &Path) -> CheckResult {
    use crate::search::dense_backend::{
        DenseBackendKind, dense_sentinel_file, resolve_active_dense_dir,
    };
    let name = "dense_index".to_string();

    // S8: validate the LIVE store dir — the ACTIVE versioned dir when a pointer
    // exists, else the legacy plain layout (so pre-versioned indexes still pass).
    let dense_dir = resolve_active_dense_dir(index_dir, DenseBackendKind::CoderankHnsw);
    let sentinel = dense_sentinel_file(DenseBackendKind::CoderankHnsw);
    let vectors_file = dense_dir.join(sentinel);

    if !vectors_file.exists() {
        return CheckResult {
            name,
            passed: false,
            message: format!(
                "Dense index missing: no {} (vector store sentinel)",
                vectors_file.display()
            ),
        };
    }

    let vectors_size = std::fs::metadata(&vectors_file).map_or(0, |m| m.len());
    if vectors_size == 0 {
        return CheckResult {
            name,
            passed: false,
            message: format!("{sentinel} is empty (0 bytes)"),
        };
    }

    CheckResult {
        name,
        passed: true,
        message: format!("OK: dense vector store present, vectors={vectors_size} bytes"),
    }
}

/// Check 4: Sparse index integrity — chunks.db existence and readability.
fn check_sparse_index(index_dir: &Path) -> CheckResult {
    let name = "sparse_index".to_string();

    let db_path = index_dir.join("chunks.db");

    if !db_path.exists() {
        return CheckResult {
            name,
            passed: false,
            message: "chunks.db not found".to_string(),
        };
    }

    match ChunkStore::open_for_search(&db_path) {
        Ok(store) => {
            let chunk_count = store.chunk_count().unwrap_or(0);
            if chunk_count == 0 {
                CheckResult {
                    name,
                    passed: false,
                    message: "chunks.db is readable but contains 0 chunks".to_string(),
                }
            } else {
                CheckResult {
                    name,
                    passed: true,
                    message: format!("OK: chunks.db readable, {chunk_count} chunks"),
                }
            }
        }
        Err(e) => CheckResult {
            name,
            passed: false,
            message: format!("chunks.db is corrupted or unreadable: {e}"),
        },
    }
}

/// Check 5: Graph consistency — symbol defs, call graph, type refs.
fn check_graph_consistency(index_dir: &Path) -> CheckResult {
    let name = "graph_consistency".to_string();

    let db_path = index_dir.join("chunks.db");
    let store = match ChunkStore::open_for_search(&db_path) {
        Ok(s) => s,
        Err(e) => {
            return CheckResult {
                name,
                passed: false,
                message: format!("Cannot open chunks.db: {e}"),
            };
        }
    };

    let chunk_count = store.chunk_count().unwrap_or(0);

    let stats = match store.graph_stats() {
        Ok(s) => s,
        Err(e) => {
            return CheckResult {
                name,
                passed: false,
                message: format!("Cannot read graph stats: {e}"),
            };
        }
    };

    // If there are chunks, we expect at least some symbol definitions
    let has_symbols = chunk_count == 0 || stats.symbol_defs_count > 0;

    CheckResult {
        name,
        passed: has_symbols,
        message: format!(
            "symbol_defs={}, calls_resolved={}, types_resolved={}, hierarchy={}, modules={}{}",
            stats.symbol_defs_count,
            stats.calls_resolved,
            stats.types_resolved,
            stats.hierarchy_resolved,
            stats.module_edges_count,
            if has_symbols {
                ""
            } else {
                " (WARNING: no symbol defs despite having chunks)"
            }
        ),
    }
}

/// Read and parse meta.json.
fn read_meta(meta_path: &Path) -> Result<IndexMeta> {
    let content = std::fs::read_to_string(meta_path)
        .with_context(|| format!("reading {}", meta_path.display()))?;
    let meta: IndexMeta = serde_json::from_str(&content)
        .with_context(|| format!("parsing {}", meta_path.display()))?;
    Ok(meta)
}

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

    fn write_meta(dir: &Path, file_count: u64, chunk_count: u64) {
        let meta = serde_json::json!({
            "schema_version": IndexMeta::CURRENT_SCHEMA_VERSION,
            "project_path": dir.parent().unwrap_or(dir),
            "created_at": "2026-01-01T00:00:00Z",
            "updated_at": "2026-01-01T00:00:00Z",
            "file_count": file_count,
            "chunk_count": chunk_count,
            "embedding_model": "test",
            "embedding_dim": 48,
            // v0.4.1 W-Index #4: use_bm25_stemmer is now part of IndexMeta.
            "use_bm25_stemmer": true,
            // S1: dense_backend is now part of IndexMeta (schema v10).
            "dense_backend": "coderank-hnsw",
            // S8: embedder_fingerprint is now part of IndexMeta.
            "embedder_fingerprint": "test",
        });
        std::fs::write(dir.join("meta.json"), meta.to_string()).unwrap();
    }

    #[test]
    fn test_validate_nonexistent_project() {
        let tmp = TempDir::new().unwrap();
        let fake_project = tmp.path().join("nonexistent");
        let report = validate(&fake_project).unwrap();
        // All checks should fail for a nonexistent project
        assert!(report.failed > 0);
        assert!(!report.all_passed());
    }

    #[test]
    fn test_check_meta_db_consistency_matching() {
        let tmp = TempDir::new().unwrap();
        let index_dir = tmp.path().join(".semantex");
        std::fs::create_dir_all(&index_dir).unwrap();

        // Create a DB with some data
        let db_path = index_dir.join("chunks.db");
        let store = ChunkStore::open(&db_path).unwrap();

        // Insert a file entry
        store
            .set_file_entry(&crate::types::FileEntry {
                path: std::path::PathBuf::from("test.rs"),
                hash: 123,
                size: 100,
                mtime: 1000,
            })
            .unwrap();

        // Insert a chunk
        let chunk = crate::types::Chunk {
            id: 0,
            file_path: std::path::PathBuf::from("test.rs"),
            start_line: 1,
            end_line: 10,
            content: "fn main() {}".to_string(),
            chunk_type: crate::types::ChunkType::TextWindow { window_index: 0 },
        };
        store.insert_chunk(&chunk, 123, 1000).unwrap();

        // Write matching meta.json
        write_meta(&index_dir, 1, 1);

        let result = check_meta_db_consistency(&index_dir);
        assert!(result.passed, "Expected pass, got: {}", result.message);
    }

    #[test]
    fn test_check_meta_db_consistency_mismatch() {
        let tmp = TempDir::new().unwrap();
        let index_dir = tmp.path().join(".semantex");
        std::fs::create_dir_all(&index_dir).unwrap();

        // Create a DB with 1 file, 1 chunk
        let db_path = index_dir.join("chunks.db");
        let store = ChunkStore::open(&db_path).unwrap();
        store
            .set_file_entry(&crate::types::FileEntry {
                path: std::path::PathBuf::from("test.rs"),
                hash: 123,
                size: 100,
                mtime: 1000,
            })
            .unwrap();
        let chunk = crate::types::Chunk {
            id: 0,
            file_path: std::path::PathBuf::from("test.rs"),
            start_line: 1,
            end_line: 10,
            content: "fn main() {}".to_string(),
            chunk_type: crate::types::ChunkType::TextWindow { window_index: 0 },
        };
        store.insert_chunk(&chunk, 123, 1000).unwrap();

        // Write meta.json with wrong counts
        write_meta(&index_dir, 99, 999);

        let result = check_meta_db_consistency(&index_dir);
        assert!(!result.passed, "Expected fail, got: {}", result.message);
    }

    #[test]
    fn test_check_stale_files_all_present() {
        let tmp = TempDir::new().unwrap();
        let project_dir = tmp.path();
        let index_dir = project_dir.join(".semantex");
        std::fs::create_dir_all(&index_dir).unwrap();

        // Create a real file
        let test_file = project_dir.join("test.rs");
        std::fs::write(&test_file, "fn main() {}").unwrap();
        let fs_mtime = std::fs::metadata(&test_file)
            .unwrap()
            .modified()
            .unwrap()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs() as i64;

        // Create DB with matching entry
        let db_path = index_dir.join("chunks.db");
        let store = ChunkStore::open(&db_path).unwrap();
        store
            .set_file_entry(&crate::types::FileEntry {
                path: std::path::PathBuf::from("test.rs"),
                hash: 123,
                size: 100,
                mtime: fs_mtime,
            })
            .unwrap();

        let result = check_stale_files(&index_dir, project_dir);
        assert!(result.passed, "Expected pass, got: {}", result.message);
    }

    #[test]
    fn test_check_stale_files_missing() {
        let tmp = TempDir::new().unwrap();
        let project_dir = tmp.path();
        let index_dir = project_dir.join(".semantex");
        std::fs::create_dir_all(&index_dir).unwrap();

        // Create DB referencing a file that doesn't exist
        let db_path = index_dir.join("chunks.db");
        let store = ChunkStore::open(&db_path).unwrap();
        store
            .set_file_entry(&crate::types::FileEntry {
                path: std::path::PathBuf::from("deleted.rs"),
                hash: 123,
                size: 100,
                mtime: 1000,
            })
            .unwrap();

        let result = check_stale_files(&index_dir, project_dir);
        assert!(!result.passed, "Expected fail, got: {}", result.message);
        assert!(result.message.contains("missing"));
    }
}