colgrep 1.6.0

Semantic code search powered by ColBERT
Documentation
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
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::fs;
use std::path::{Path, PathBuf};
use std::time::SystemTime;
use xxhash_rust::xxh3::xxh3_64;

use super::paths::get_state_path;

/// Version of the on-disk index format (chunk layout, embedding pipeline,
/// metadata schema). Bump ONLY for incompatible changes: a mismatch discards
/// the index and re-embeds the entire project on the next run. Routine CLI
/// releases must NOT bump this.
pub const INDEX_FORMAT_VERSION: u32 = 2;

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct IndexState {
    /// CLI version that created/updated this index (diagnostic only)
    #[serde(default)]
    pub cli_version: String,
    /// Index format version this index was written with. Indexes from before
    /// this field existed deserialize as 0 and rebuild once.
    #[serde(default)]
    pub index_format_version: u32,
    pub files: HashMap<PathBuf, FileInfo>,
    /// Files that failed to parse (e.g. invalid UTF-8) — skipped on future runs
    #[serde(default)]
    pub ignored_files: HashSet<PathBuf>,
    /// Number of searches performed against this index
    #[serde(default)]
    pub search_count: u64,
    /// Set before index writes, cleared after. Allows skipping repair on clean shutdown.
    #[serde(default)]
    pub dirty: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileInfo {
    pub content_hash: u64,
    /// File mtime in **nanoseconds** since the Unix epoch. Nanosecond precision
    /// (not seconds) so that an edit landing in the same wall-clock second as the
    /// last index update is still detected by the stat-only fast path.
    pub mtime: u64,
    /// File size in bytes, compared alongside `mtime` as a cheap second guard.
    /// Legacy states written before this field deserialize as 0, which forces a
    /// one-time content hash and then self-heals once the state is re-saved.
    #[serde(default)]
    pub size: u64,
}

impl FileInfo {
    /// Build a `FileInfo` for `path` with an already-computed content hash,
    /// stamping the current mtime (nanoseconds) and size from a single stat.
    pub fn probe(path: &Path, content_hash: u64) -> Result<Self> {
        let (mtime, size) = file_stat(path)?;
        Ok(Self {
            content_hash,
            mtime,
            size,
        })
    }
}

impl IndexState {
    /// Load state from the given index directory
    pub fn load(index_dir: &Path) -> Result<Self> {
        let state_path = get_state_path(index_dir);
        if state_path.exists() {
            let content = fs::read_to_string(&state_path)?;
            Ok(serde_json::from_str(&content)?)
        } else {
            Ok(Self::default())
        }
    }

    /// Save state to the given index directory.
    ///
    /// Uses atomic write (write to temp file + rename) to prevent corruption
    /// when multiple colgrep processes access the same index concurrently.
    /// Without this, `fs::write` truncates the file before writing, so a
    /// concurrent reader can see an empty file and fail with a parse error.
    pub fn save(&self, index_dir: &Path) -> Result<()> {
        fs::create_dir_all(index_dir)?;

        // Stamp the writing binary's CLI version and index format before saving
        let mut state = self.clone();
        state.cli_version = env!("CARGO_PKG_VERSION").to_string();
        state.index_format_version = INDEX_FORMAT_VERSION;

        let state_path = get_state_path(index_dir);
        let content = serde_json::to_string_pretty(&state)?;

        // Atomic write: write to a temp file in the same directory, then rename.
        // rename(2) on the same filesystem is atomic on POSIX systems.
        // Use PID + thread ID to avoid collisions between concurrent writers.
        let tid =
            format!("{:?}", std::thread::current().id()).replace(|c: char| !c.is_ascii_digit(), "");
        let tmp_name = format!("state.{}.{}.json.tmp", std::process::id(), tid);
        let tmp_path = index_dir.join(tmp_name);
        fs::write(&tmp_path, content)?;
        fs::rename(&tmp_path, &state_path)?;
        Ok(())
    }

    /// Increment the search count
    pub fn increment_search_count(&mut self) {
        self.search_count += 1;
    }

    /// Reset the search count to zero
    pub fn reset_search_count(&mut self) {
        self.search_count = 0;
    }
}

/// Hash file content using xxHash for fast comparison
pub fn hash_file(path: &Path) -> Result<u64> {
    let content = fs::read(path)?;
    Ok(xxh3_64(&content))
}

/// Stat a file once, returning `(mtime_nanos, size_bytes)`.
/// mtime is nanoseconds since the Unix epoch (sub-second precision so same-second
/// edits are detectable); size is the cheap second guard against mtime collisions.
pub fn file_stat(path: &Path) -> Result<(u64, u64)> {
    let metadata = fs::metadata(path)?;
    let mtime = metadata
        .modified()?
        .duration_since(SystemTime::UNIX_EPOCH)?
        .as_nanos() as u64;
    Ok((mtime, metadata.len()))
}

/// Get file modification time in nanoseconds since the Unix epoch.
pub fn get_mtime(path: &Path) -> Result<u64> {
    Ok(file_stat(path)?.0)
}

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

    #[test]
    fn test_index_state_default() {
        let state = IndexState::default();
        assert!(state.cli_version.is_empty());
        assert!(state.files.is_empty());
    }

    #[test]
    fn test_file_info_serialization() {
        let info = FileInfo {
            content_hash: 12345678901234567890,
            mtime: 1700000000,
            size: 0,
        };

        let json = serde_json::to_string(&info).unwrap();
        assert!(json.contains("12345678901234567890"));
        assert!(json.contains("1700000000"));

        let deserialized: FileInfo = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.content_hash, 12345678901234567890);
        assert_eq!(deserialized.mtime, 1700000000);
    }

    #[test]
    fn test_index_state_serialization() {
        let mut files = HashMap::new();
        files.insert(
            PathBuf::from("src/main.rs"),
            FileInfo {
                content_hash: 123456,
                mtime: 1700000000,
                size: 0,
            },
        );
        let state = IndexState {
            cli_version: "1.0.0".to_string(),
            index_format_version: INDEX_FORMAT_VERSION,
            files,
            ignored_files: HashSet::new(),
            search_count: 0,
            dirty: false,
        };

        let json = serde_json::to_string(&state).unwrap();
        assert!(json.contains("1.0.0"));
        assert!(json.contains("src/main.rs"));

        let deserialized: IndexState = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.cli_version, "1.0.0");
        assert!(deserialized
            .files
            .contains_key(&PathBuf::from("src/main.rs")));
    }

    #[test]
    fn test_index_state_load_nonexistent() {
        let temp_dir = TempDir::new().unwrap();
        let result = IndexState::load(temp_dir.path());
        assert!(result.is_ok());
        let state = result.unwrap();
        assert!(state.files.is_empty());
    }

    #[test]
    fn test_index_state_save_and_load() {
        let temp_dir = TempDir::new().unwrap();

        let mut state = IndexState::default();
        state.files.insert(
            PathBuf::from("test.rs"),
            FileInfo {
                content_hash: 999999,
                mtime: 1700000000,
                size: 0,
            },
        );

        // Save
        state.save(temp_dir.path()).unwrap();

        // Load and verify
        let loaded = IndexState::load(temp_dir.path()).unwrap();
        assert!(loaded.files.contains_key(&PathBuf::from("test.rs")));
        let file_info = loaded.files.get(&PathBuf::from("test.rs")).unwrap();
        assert_eq!(file_info.content_hash, 999999);

        // CLI version should be set after saving
        assert!(!loaded.cli_version.is_empty());
    }

    #[test]
    fn test_ignored_files_persisted_to_disk() {
        let temp_dir = TempDir::new().unwrap();

        let mut state = IndexState::default();
        state
            .ignored_files
            .insert(PathBuf::from("bad/binary_file.hpp"));
        state
            .ignored_files
            .insert(PathBuf::from("vendor/nonascii.py"));

        // Save to disk
        state.save(temp_dir.path()).unwrap();

        // Load from disk and verify ignored files survived the round-trip
        let loaded = IndexState::load(temp_dir.path()).unwrap();
        assert_eq!(loaded.ignored_files.len(), 2);
        assert!(loaded
            .ignored_files
            .contains(&PathBuf::from("bad/binary_file.hpp")));
        assert!(loaded
            .ignored_files
            .contains(&PathBuf::from("vendor/nonascii.py")));
    }

    #[test]
    fn test_hash_file() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("test.txt");

        // Create a file with known content
        let mut file = fs::File::create(&file_path).unwrap();
        file.write_all(b"Hello, World!").unwrap();

        let hash = hash_file(&file_path).unwrap();
        assert!(hash > 0);

        // Same content should produce same hash
        let hash2 = hash_file(&file_path).unwrap();
        assert_eq!(hash, hash2);
    }

    #[test]
    fn test_hash_file_different_content() {
        let temp_dir = TempDir::new().unwrap();

        let file1 = temp_dir.path().join("file1.txt");
        let file2 = temp_dir.path().join("file2.txt");

        fs::write(&file1, "Content A").unwrap();
        fs::write(&file2, "Content B").unwrap();

        let hash1 = hash_file(&file1).unwrap();
        let hash2 = hash_file(&file2).unwrap();

        assert_ne!(hash1, hash2);
    }

    #[test]
    fn test_get_mtime() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("test.txt");

        fs::write(&file_path, "test content").unwrap();

        let mtime = get_mtime(&file_path).unwrap();
        // mtime should be a reasonable Unix timestamp (after year 2000)
        assert!(mtime > 946684800); // Jan 1, 2000
    }

    #[test]
    fn test_hash_file_nonexistent() {
        let result = hash_file(Path::new("/nonexistent/file.txt"));
        assert!(result.is_err());
    }

    #[test]
    fn test_get_mtime_nonexistent() {
        let result = get_mtime(Path::new("/nonexistent/file.txt"));
        assert!(result.is_err());
    }

    /// A state.json written before index_format_version existed must load as
    /// version 0 (forcing a one-time rebuild), and save() must stamp the
    /// current format so the rebuild happens exactly once.
    #[test]
    fn test_legacy_state_without_format_version_loads_as_zero() {
        let json = r#"{"cli_version":"1.5.4","files":{},"search_count":3}"#;
        let state: IndexState = serde_json::from_str(json).unwrap();
        assert_eq!(state.index_format_version, 0);

        let temp_dir = TempDir::new().unwrap();
        state.save(temp_dir.path()).unwrap();
        assert_eq!(
            IndexState::load(temp_dir.path())
                .unwrap()
                .index_format_version,
            INDEX_FORMAT_VERSION
        );
    }

    #[test]
    fn test_search_count_increment_and_reset() {
        let temp_dir = TempDir::new().unwrap();

        // Create initial state with zero search count
        let mut state = IndexState::default();
        assert_eq!(state.search_count, 0);

        // Increment search count
        state.increment_search_count();
        assert_eq!(state.search_count, 1);

        state.increment_search_count();
        state.increment_search_count();
        assert_eq!(state.search_count, 3);

        // Save and reload to verify persistence
        state.save(temp_dir.path()).unwrap();
        let loaded = IndexState::load(temp_dir.path()).unwrap();
        assert_eq!(loaded.search_count, 3);

        // Reset search count
        let mut loaded = loaded;
        loaded.reset_search_count();
        assert_eq!(loaded.search_count, 0);

        // Save and reload to verify reset persists
        loaded.save(temp_dir.path()).unwrap();
        let reloaded = IndexState::load(temp_dir.path()).unwrap();
        assert_eq!(reloaded.search_count, 0);
    }

    #[test]
    fn test_concurrent_save_and_load() {
        // Verify that concurrent save + load never sees a truncated/empty file.
        // Before the atomic-write fix, fs::write would truncate state.json
        // before writing, so a concurrent load could read 0 bytes → parse error.
        use std::sync::{Arc, Barrier};

        let temp_dir = TempDir::new().unwrap();
        let index_dir = temp_dir.path().to_path_buf();

        // Seed initial state so load always has something to read
        let mut init = IndexState::default();
        init.files.insert(
            PathBuf::from("seed.rs"),
            FileInfo {
                content_hash: 1,
                mtime: 1700000000,
                size: 0,
            },
        );
        init.save(&index_dir).unwrap();

        let n_threads = 8;
        let iterations = 50;
        let barrier = Arc::new(Barrier::new(n_threads));
        let mut handles = vec![];

        for t in 0..n_threads {
            let dir = index_dir.clone();
            let bar = Arc::clone(&barrier);
            handles.push(std::thread::spawn(move || {
                bar.wait(); // all threads start at once
                for i in 0..iterations {
                    if t % 2 == 0 {
                        // Writer: save with incrementing search_count
                        let mut state = IndexState {
                            search_count: (t * iterations + i) as u64,
                            ..Default::default()
                        };
                        state.files.insert(
                            PathBuf::from(format!("file_{t}_{i}.rs")),
                            FileInfo {
                                content_hash: (t * iterations + i) as u64,
                                mtime: 1700000000,
                                size: 0,
                            },
                        );
                        state.save(&dir).unwrap();
                    } else {
                        // Reader: load must never fail with a parse error
                        let result = IndexState::load(&dir);
                        assert!(
                            result.is_ok(),
                            "Concurrent load failed on thread {t} iteration {i}: {:?}",
                            result.err()
                        );
                    }
                }
            }));
        }

        for h in handles {
            h.join().expect("thread panicked");
        }
    }
}