codenexus 0.3.4

A queryable code knowledge graph tool built on LadybugDB and tree-sitter
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
// Copyright (c) 2026 Kirky.X. All rights reserved.
// SPDX-License-Identifier: MIT

//! Incremental indexing via SHA-256 hash diffing (BR-INDEX-001~003, ADR-009).
//!
//! Compares the set of source files on disk against the `(path, hash)` pairs
//! stored in the database for a project, classifying each file as
//! [`FileDiff::changed`], [`FileDiff::added`], [`FileDiff::unchanged`], or
//! [`FileDiff::deleted`]. The pipeline uses this classification to skip
//! unchanged files (BR-INDEX-001), delete nodes for removed files
//! (BR-INDEX-002), and force a full re-parse when `--force` is set
//! (BR-INDEX-003).

use std::collections::HashMap;

use crate::discover::FileInfo;
use crate::index::hash::compute_file_hash;

/// The diff between on-disk files and the database's stored hashes.
///
/// Produced by [`diff_files`]. Each bucket drives a distinct pipeline stage:
///
/// - `changed` → re-parse and replace nodes for these files.
/// - `added` → parse and insert nodes for these files.
/// - `unchanged` → skip (BR-INDEX-001).
/// - `deleted` → remove nodes and edges for these paths (BR-INDEX-002).
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct FileDiff {
    /// Files whose hash differs from the stored hash (re-parse required).
    pub changed: Vec<FileInfo>,
    /// Files present on disk but absent from the database (new files).
    pub added: Vec<FileInfo>,
    /// Files whose hash matches the stored hash (skip, BR-INDEX-001).
    pub unchanged: Vec<FileInfo>,
    /// Paths present in the database but absent on disk (BR-INDEX-002).
    pub deleted: Vec<String>,
}

impl FileDiff {
    /// Creates an empty `FileDiff` (all buckets empty).
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Returns `true` if every bucket is empty (no work to do).
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.changed.is_empty()
            && self.added.is_empty()
            && self.unchanged.is_empty()
            && self.deleted.is_empty()
    }

    /// Returns the total number of files across all buckets.
    ///
    /// `deleted` counts as one per path; the other buckets count one per
    /// [`FileInfo`].
    #[must_use]
    pub fn total(&self) -> usize {
        self.changed.len() + self.added.len() + self.unchanged.len() + self.deleted.len()
    }

    /// Returns the files that need to be parsed (changed + added).
    #[must_use]
    pub fn to_parse(&self) -> Vec<&FileInfo> {
        self.changed.iter().chain(self.added.iter()).collect()
    }
}

/// Computes the [`FileDiff`] between `disk_files` and `db_hashes`.
///
/// # Arguments
///
/// * `disk_files` - Files discovered on disk by [`crate::discover::Walker`].
/// * `db_hashes` - `(path, hash)` pairs loaded from the database via
///   [`crate::storage::Repository::get_all_file_hashes`].
/// * `force` - When `true`, every disk file is classified as `changed`
///   regardless of its hash (BR-INDEX-003, `--force`). `deleted` is still
///   computed.
///
/// # Errors
///
/// Returns [`std::io::Error`] if a file's hash cannot be computed (e.g. the
/// file was deleted between discovery and hashing).
///
/// # Classification rules
///
/// - BR-INDEX-001: hash matches DB → `unchanged` (skip).
/// - BR-INDEX-002: in DB but not on disk → `deleted`.
/// - BR-INDEX-003: `force=true` → all disk files go to `changed`.
/// - New file (not in DB) → `added`.
/// - Hash differs → `changed`.
pub fn diff_files(
    disk_files: &[FileInfo],
    db_hashes: &[(String, String)],
    force: bool,
) -> Result<FileDiff, std::io::Error> {
    // Index DB hashes by path for O(1) lookup.
    let mut db_map: HashMap<&str, &str> = HashMap::with_capacity(db_hashes.len());
    for (path, hash) in db_hashes {
        db_map.insert(path.as_str(), hash.as_str());
    }

    let mut diff = FileDiff::new();
    let mut seen_on_disk: HashMap<&str, ()> = HashMap::with_capacity(disk_files.len());

    for file in disk_files {
        seen_on_disk.insert(file.relative_path.as_str(), ());
        let disk_hash = compute_file_hash(&file.path)?;

        if force {
            // BR-INDEX-003: --force ignores hashes; every disk file is changed.
            diff.changed.push(file.clone());
            continue;
        }

        match db_map.get(file.relative_path.as_str()) {
            None => {
                // Not in DB → new file.
                diff.added.push(file.clone());
            }
            Some(db_hash) => {
                if *db_hash == disk_hash {
                    // BR-INDEX-001: hash matches → skip.
                    diff.unchanged.push(file.clone());
                } else {
                    // Hash differs → re-parse.
                    diff.changed.push(file.clone());
                }
            }
        }
    }

    // BR-INDEX-002: in DB but not on disk → deleted.
    for (path, _) in db_hashes {
        if !seen_on_disk.contains_key(path.as_str()) {
            diff.deleted.push(path.clone());
        }
    }

    Ok(diff)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::Language;
    use std::fs;
    use std::path::{Path, PathBuf};
    use tempfile::TempDir;

    /// Writes a file at `dir/rel` (creating parent directories as needed) and
    /// returns a `FileInfo` describing it.
    fn make_file(dir: &Path, rel: &str, content: &str, language: Language) -> FileInfo {
        let path = dir.join(rel);
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).unwrap();
        }
        fs::write(&path, content).unwrap();
        let metadata = fs::metadata(&path).unwrap();
        FileInfo {
            path,
            relative_path: rel.to_string(),
            language: Some(language),
            size: metadata.len(),
        }
    }

    /// Computes the SHA-256 hash of `dir/rel` for use in DB hash fixtures.
    fn hash_of(dir: &Path, rel: &str) -> String {
        compute_file_hash(&dir.join(rel)).unwrap()
    }

    // --- FileDiff ---

    #[test]
    fn file_diff_new_is_empty() {
        let diff = FileDiff::new();
        assert!(diff.changed.is_empty());
        assert!(diff.added.is_empty());
        assert!(diff.unchanged.is_empty());
        assert!(diff.deleted.is_empty());
        assert!(diff.is_empty());
        assert_eq!(diff.total(), 0);
    }

    #[test]
    fn file_diff_is_empty_false_when_populated() {
        let mut diff = FileDiff::new();
        diff.added.push(FileInfo {
            path: PathBuf::from("/x.rs"),
            relative_path: "x.rs".to_string(),
            language: Some(Language::Rust),
            size: 0,
        });
        assert!(!diff.is_empty());
        assert_eq!(diff.total(), 1);
    }

    #[test]
    fn file_diff_total_sums_all_buckets() {
        use std::path::PathBuf;
        let mut diff = FileDiff::new();
        diff.changed.push(FileInfo {
            path: PathBuf::from("/a.rs"),
            relative_path: "a.rs".to_string(),
            language: Some(Language::Rust),
            size: 0,
        });
        diff.added.push(FileInfo {
            path: PathBuf::from("/b.rs"),
            relative_path: "b.rs".to_string(),
            language: Some(Language::Rust),
            size: 0,
        });
        diff.unchanged.push(FileInfo {
            path: PathBuf::from("/c.rs"),
            relative_path: "c.rs".to_string(),
            language: Some(Language::Rust),
            size: 0,
        });
        diff.deleted.push("d.rs".to_string());
        assert_eq!(diff.total(), 4);
    }

    #[test]
    fn file_diff_to_parse_combines_changed_and_added() {
        use std::path::PathBuf;
        let mut diff = FileDiff::new();
        diff.changed.push(FileInfo {
            path: PathBuf::from("/a.rs"),
            relative_path: "a.rs".to_string(),
            language: Some(Language::Rust),
            size: 0,
        });
        diff.added.push(FileInfo {
            path: PathBuf::from("/b.rs"),
            relative_path: "b.rs".to_string(),
            language: Some(Language::Rust),
            size: 0,
        });
        diff.unchanged.push(FileInfo {
            path: PathBuf::from("/c.rs"),
            relative_path: "c.rs".to_string(),
            language: Some(Language::Rust),
            size: 0,
        });
        let to_parse = diff.to_parse();
        assert_eq!(to_parse.len(), 2);
        let paths: Vec<&str> = to_parse.iter().map(|f| f.relative_path.as_str()).collect();
        assert!(paths.contains(&"a.rs"));
        assert!(paths.contains(&"b.rs"));
    }

    // --- diff_files: all new files → added ---

    #[test]
    fn diff_files_all_new_files_go_to_added() {
        let tmp = TempDir::new().unwrap();
        let f1 = make_file(tmp.path(), "a.rs", "fn a() {}", Language::Rust);
        let f2 = make_file(tmp.path(), "b.rs", "fn b() {}", Language::Rust);
        let disk = vec![f1, f2];
        let db: Vec<(String, String)> = vec![];

        let diff = diff_files(&disk, &db, false).unwrap();

        assert_eq!(diff.added.len(), 2, "both files should be added");
        assert!(diff.changed.is_empty());
        assert!(diff.unchanged.is_empty());
        assert!(diff.deleted.is_empty());
    }

    // --- diff_files: matching hash → unchanged ---

    #[test]
    fn diff_files_matching_hash_goes_to_unchanged() {
        let tmp = TempDir::new().unwrap();
        let f = make_file(tmp.path(), "a.rs", "fn a() {}", Language::Rust);
        let disk = vec![f];
        let db = vec![("a.rs".to_string(), hash_of(tmp.path(), "a.rs"))];

        let diff = diff_files(&disk, &db, false).unwrap();

        assert_eq!(
            diff.unchanged.len(),
            1,
            "matching hash → unchanged (BR-INDEX-001)"
        );
        assert!(diff.changed.is_empty());
        assert!(diff.added.is_empty());
        assert!(diff.deleted.is_empty());
    }

    // --- diff_files: different hash → changed ---

    #[test]
    fn diff_files_different_hash_goes_to_changed() {
        let tmp = TempDir::new().unwrap();
        let f = make_file(
            tmp.path(),
            "a.rs",
            "fn a() { /* modified */ }",
            Language::Rust,
        );
        let disk = vec![f];
        // DB hash is for the OLD content, so it differs from the current hash.
        let db = vec![("a.rs".to_string(), "0".repeat(64))];

        let diff = diff_files(&disk, &db, false).unwrap();

        assert_eq!(diff.changed.len(), 1, "different hash → changed");
        assert!(diff.unchanged.is_empty());
        assert!(diff.added.is_empty());
        assert!(diff.deleted.is_empty());
    }

    // --- diff_files: in DB not on disk → deleted ---

    #[test]
    fn diff_files_in_db_not_on_disk_goes_to_deleted() {
        let tmp = TempDir::new().unwrap();
        let f = make_file(tmp.path(), "a.rs", "fn a() {}", Language::Rust);
        let disk = vec![f];
        let db = vec![
            ("a.rs".to_string(), hash_of(tmp.path(), "a.rs")),
            ("deleted.rs".to_string(), "deadbeef".to_string()),
        ];

        let diff = diff_files(&disk, &db, false).unwrap();

        assert_eq!(
            diff.deleted.len(),
            1,
            "BR-INDEX-002: in DB not on disk → deleted"
        );
        assert_eq!(diff.deleted[0], "deleted.rs");
        assert_eq!(diff.unchanged.len(), 1);
    }

    // --- diff_files: force=true → all disk files in changed ---

    #[test]
    fn diff_files_force_puts_all_disk_files_in_changed() {
        let tmp = TempDir::new().unwrap();
        let f1 = make_file(tmp.path(), "a.rs", "fn a() {}", Language::Rust);
        let f2 = make_file(tmp.path(), "b.rs", "fn b() {}", Language::Rust);
        let disk = vec![f1, f2];
        // Even though hashes match, force=true must override.
        let db = vec![
            ("a.rs".to_string(), hash_of(tmp.path(), "a.rs")),
            ("b.rs".to_string(), hash_of(tmp.path(), "b.rs")),
        ];

        let diff = diff_files(&disk, &db, true).unwrap();

        assert_eq!(diff.changed.len(), 2, "BR-INDEX-003: force → all changed");
        assert!(
            diff.unchanged.is_empty(),
            "force must skip the unchanged bucket"
        );
        assert!(diff.added.is_empty());
        assert!(
            diff.deleted.is_empty(),
            "force does not affect deleted detection"
        );
    }

    #[test]
    fn diff_files_force_with_new_file_goes_to_changed() {
        let tmp = TempDir::new().unwrap();
        let f = make_file(tmp.path(), "new.rs", "fn new() {}", Language::Rust);
        let disk = vec![f];
        let db: Vec<(String, String)> = vec![];

        let diff = diff_files(&disk, &db, true).unwrap();

        // With force=true, even new files go to changed (not added).
        assert_eq!(diff.changed.len(), 1);
        assert!(diff.added.is_empty());
    }

    // --- diff_files: empty disk, non-empty DB → all deleted ---

    #[test]
    fn diff_files_empty_disk_nonempty_db_all_deleted() {
        let disk: Vec<FileInfo> = vec![];
        let db = vec![
            ("a.rs".to_string(), "hash_a".to_string()),
            ("b.rs".to_string(), "hash_b".to_string()),
            ("c.rs".to_string(), "hash_c".to_string()),
        ];

        let diff = diff_files(&disk, &db, false).unwrap();

        assert_eq!(diff.deleted.len(), 3, "all DB files should be deleted");
        let deleted_paths: Vec<&str> = diff.deleted.iter().map(|s| s.as_str()).collect();
        assert!(deleted_paths.contains(&"a.rs"));
        assert!(deleted_paths.contains(&"b.rs"));
        assert!(deleted_paths.contains(&"c.rs"));
        assert!(diff.changed.is_empty());
        assert!(diff.added.is_empty());
        assert!(diff.unchanged.is_empty());
    }

    // --- diff_files: empty disk + empty DB → empty diff ---

    #[test]
    fn diff_files_empty_disk_empty_db_empty_diff() {
        let disk: Vec<FileInfo> = vec![];
        let db: Vec<(String, String)> = vec![];

        let diff = diff_files(&disk, &db, false).unwrap();

        assert!(diff.is_empty());
        assert_eq!(diff.total(), 0);
    }

    // --- diff_files: mixed scenario ---

    #[test]
    fn diff_files_mixed_scenario() {
        let tmp = TempDir::new().unwrap();
        // a.rs: unchanged (hash matches DB)
        let a = make_file(tmp.path(), "a.rs", "fn a() {}", Language::Rust);
        // b.rs: changed (hash differs from DB)
        let b = make_file(tmp.path(), "b.rs", "fn b() { /* new */ }", Language::Rust);
        // c.rs: added (not in DB)
        let c = make_file(tmp.path(), "c.rs", "fn c() {}", Language::Rust);
        let disk = vec![a, b, c];
        let db = vec![
            ("a.rs".to_string(), hash_of(tmp.path(), "a.rs")), // matches
            ("b.rs".to_string(), "0".repeat(64)),              // differs
            // c.rs not in DB → added
            ("deleted.rs".to_string(), "old_hash".to_string()), // not on disk → deleted
        ];

        let diff = diff_files(&disk, &db, false).unwrap();

        assert_eq!(diff.unchanged.len(), 1, "a.rs unchanged");
        assert_eq!(diff.changed.len(), 1, "b.rs changed");
        assert_eq!(diff.added.len(), 1, "c.rs added");
        assert_eq!(diff.deleted.len(), 1, "deleted.rs deleted");

        let unchanged_paths: Vec<&str> = diff
            .unchanged
            .iter()
            .map(|f| f.relative_path.as_str())
            .collect();
        let changed_paths: Vec<&str> = diff
            .changed
            .iter()
            .map(|f| f.relative_path.as_str())
            .collect();
        let added_paths: Vec<&str> = diff
            .added
            .iter()
            .map(|f| f.relative_path.as_str())
            .collect();
        assert!(unchanged_paths.contains(&"a.rs"));
        assert!(changed_paths.contains(&"b.rs"));
        assert!(added_paths.contains(&"c.rs"));
        assert_eq!(diff.deleted[0], "deleted.rs");
    }

    // --- diff_files: returns error for missing file ---

    #[test]
    fn diff_files_returns_error_when_disk_file_disappears() {
        // A FileInfo whose path does not exist on disk should cause an error.
        let file = FileInfo {
            path: PathBuf::from("/nonexistent/missing.rs"),
            relative_path: "missing.rs".to_string(),
            language: Some(Language::Rust),
            size: 0,
        };
        let disk = vec![file];
        let db: Vec<(String, String)> = vec![];

        let result = diff_files(&disk, &db, false);
        assert!(result.is_err(), "missing disk file should error");
        let err = result.unwrap_err();
        assert_eq!(err.kind(), std::io::ErrorKind::NotFound);
    }

    // --- diff_files: force=true still computes deleted ---

    #[test]
    fn diff_files_force_still_computes_deleted() {
        let tmp = TempDir::new().unwrap();
        let f = make_file(tmp.path(), "a.rs", "fn a() {}", Language::Rust);
        let disk = vec![f];
        let db = vec![
            ("a.rs".to_string(), hash_of(tmp.path(), "a.rs")),
            ("gone.rs".to_string(), "old".to_string()),
        ];

        let diff = diff_files(&disk, &db, true).unwrap();

        // a.rs goes to changed (force), gone.rs goes to deleted.
        assert_eq!(diff.changed.len(), 1);
        assert_eq!(diff.deleted.len(), 1);
        assert_eq!(diff.deleted[0], "gone.rs");
    }

    // --- diff_files: handles nested paths ---

    #[test]
    fn diff_files_handles_nested_paths() {
        let tmp = TempDir::new().unwrap();
        let f1 = make_file(tmp.path(), "src/main.rs", "fn main() {}", Language::Rust);
        let f2 = make_file(
            tmp.path(),
            "src/sub/mod.rs",
            "fn mod_fn() {}",
            Language::Rust,
        );
        let disk = vec![f1, f2];
        let db = vec![
            (
                "src/main.rs".to_string(),
                hash_of(tmp.path(), "src/main.rs"),
            ),
            // src/sub/mod.rs is new
        ];

        let diff = diff_files(&disk, &db, false).unwrap();

        assert_eq!(diff.unchanged.len(), 1);
        assert_eq!(diff.added.len(), 1);
        let added_paths: Vec<&str> = diff
            .added
            .iter()
            .map(|f| f.relative_path.as_str())
            .collect();
        assert!(added_paths.contains(&"src/sub/mod.rs"));
    }

    // --- diff_files: same path different project handled by caller ---

    #[test]
    fn diff_files_uses_relative_path_as_key() {
        // The DB hashes are scoped to a project by the caller (via
        // Repository::get_all_file_hashes(project)). diff_files itself only
        // compares paths, so the same relative path in two projects is handled
        // by calling diff_files twice with different db_hashes.
        let tmp = TempDir::new().unwrap();
        let f = make_file(tmp.path(), "main.rs", "fn main() {}", Language::Rust);
        let disk = vec![f];
        let db = vec![("main.rs".to_string(), hash_of(tmp.path(), "main.rs"))];

        let diff = diff_files(&disk, &db, false).unwrap();
        assert_eq!(diff.unchanged.len(), 1);
    }

    // --- diff_files: force=true with empty DB → all changed ---

    #[test]
    fn diff_files_force_with_empty_db_all_changed() {
        let tmp = TempDir::new().unwrap();
        let f1 = make_file(tmp.path(), "a.rs", "fn a() {}", Language::Rust);
        let f2 = make_file(tmp.path(), "b.rs", "fn b() {}", Language::Rust);
        let disk = vec![f1, f2];
        let db: Vec<(String, String)> = vec![];

        let diff = diff_files(&disk, &db, true).unwrap();

        assert_eq!(diff.changed.len(), 2);
        assert!(diff.added.is_empty(), "force overrides added → changed");
        assert!(diff.unchanged.is_empty());
        assert!(diff.deleted.is_empty());
    }
}