ck-core 0.4.4

Core types and utilities for ck semantic search tool
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
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
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum CkError {
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    #[error("Regex error: {0}")]
    Regex(#[from] regex::Error),

    #[error("Serialization error: {0}")]
    Serialization(#[from] bincode::Error),

    #[error("JSON error: {0}")]
    Json(#[from] serde_json::Error),

    #[error("Index error: {0}")]
    Index(String),

    #[error("Search error: {0}")]
    Search(String),

    #[error("Embedding error: {0}")]
    Embedding(String),

    #[error("Other error: {0}")]
    Other(String),
}

pub type Result<T> = std::result::Result<T, CkError>;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Language {
    Rust,
    Python,
    JavaScript,
    TypeScript,
    Haskell,
    Go,
    Java,
    C,
    Cpp,
    CSharp,
    Ruby,
    Php,
    Swift,
    Kotlin,
}

impl Language {
    pub fn from_extension(ext: &str) -> Option<Self> {
        match ext {
            "rs" => Some(Language::Rust),
            "py" => Some(Language::Python),
            "js" => Some(Language::JavaScript),
            "ts" | "tsx" => Some(Language::TypeScript),
            "hs" | "lhs" => Some(Language::Haskell),
            "go" => Some(Language::Go),
            "java" => Some(Language::Java),
            "c" => Some(Language::C),
            "cpp" | "cc" | "cxx" | "c++" => Some(Language::Cpp),
            "h" | "hpp" => Some(Language::Cpp), // Assume C++ for headers
            "cs" => Some(Language::CSharp),
            "rb" => Some(Language::Ruby),
            "php" => Some(Language::Php),
            "swift" => Some(Language::Swift),
            "kt" | "kts" => Some(Language::Kotlin),
            _ => None,
        }
    }

    pub fn from_path(path: &Path) -> Option<Self> {
        path.extension()
            .and_then(|ext| ext.to_str())
            .and_then(Self::from_extension)
    }
}

impl std::fmt::Display for Language {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let name = match self {
            Language::Rust => "rust",
            Language::Python => "python",
            Language::JavaScript => "javascript",
            Language::TypeScript => "typescript",
            Language::Haskell => "haskell",
            Language::Go => "go",
            Language::Java => "java",
            Language::C => "c",
            Language::Cpp => "cpp",
            Language::CSharp => "csharp",
            Language::Ruby => "ruby",
            Language::Php => "php",
            Language::Swift => "swift",
            Language::Kotlin => "kotlin",
        };
        write!(f, "{}", name)
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Span {
    pub byte_start: usize,
    pub byte_end: usize,
    pub line_start: usize,
    pub line_end: usize,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileMetadata {
    pub path: PathBuf,
    pub hash: String,
    pub last_modified: u64,
    pub size: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchResult {
    pub file: PathBuf,
    pub span: Span,
    pub score: f32,
    pub preview: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub lang: Option<Language>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub symbol: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub chunk_hash: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub index_epoch: Option<u64>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonSearchResult {
    pub file: String,
    pub span: Span,
    pub lang: Option<Language>,
    pub symbol: Option<String>,
    pub score: f32,
    pub signals: SearchSignals,
    pub preview: String,
    pub model: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonlSearchResult {
    pub path: String,
    pub span: Span,
    pub language: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub snippet: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub score: Option<f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub chunk_hash: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub index_epoch: Option<u64>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchSignals {
    pub lex_rank: Option<usize>,
    pub vec_rank: Option<usize>,
    pub rrf_score: f32,
}

#[derive(Debug, Clone, PartialEq)]
pub enum SearchMode {
    Regex,
    Lexical,
    Semantic,
    Hybrid,
}

#[derive(Debug, Clone)]
pub struct SearchOptions {
    pub mode: SearchMode,
    pub query: String,
    pub path: PathBuf,
    pub top_k: Option<usize>,
    pub threshold: Option<f32>,
    pub case_insensitive: bool,
    pub whole_word: bool,
    pub fixed_string: bool,
    pub line_numbers: bool,
    pub context_lines: usize,
    pub before_context_lines: usize,
    pub after_context_lines: usize,
    pub recursive: bool,
    pub json_output: bool,
    pub jsonl_output: bool,
    pub no_snippet: bool,
    pub reindex: bool,
    pub show_scores: bool,
    pub show_filenames: bool,
    pub files_with_matches: bool,
    pub files_without_matches: bool,
    pub exclude_patterns: Vec<String>,
    pub respect_gitignore: bool,
    pub full_section: bool,
    // Enhanced embedding options (search-time only)
    pub rerank: bool,
    pub rerank_model: Option<String>,
}

impl JsonlSearchResult {
    pub fn from_search_result(result: &SearchResult, include_snippet: bool) -> Self {
        Self {
            path: result.file.to_string_lossy().to_string(),
            span: result.span.clone(),
            language: result.lang.as_ref().map(|l| l.to_string()),
            snippet: if include_snippet {
                Some(result.preview.clone())
            } else {
                None
            },
            score: if result.score >= 0.0 {
                Some(result.score)
            } else {
                None
            },
            chunk_hash: result.chunk_hash.clone(),
            index_epoch: result.index_epoch,
        }
    }
}

impl Default for SearchOptions {
    fn default() -> Self {
        Self {
            mode: SearchMode::Regex,
            query: String::new(),
            path: PathBuf::from("."),
            top_k: None,
            threshold: None,
            case_insensitive: false,
            whole_word: false,
            fixed_string: false,
            line_numbers: false,
            context_lines: 0,
            before_context_lines: 0,
            after_context_lines: 0,
            recursive: true,
            json_output: false,
            jsonl_output: false,
            no_snippet: false,
            reindex: false,
            show_scores: false,
            show_filenames: false,
            files_with_matches: false,
            files_without_matches: false,
            exclude_patterns: get_default_exclude_patterns(),
            respect_gitignore: true,
            full_section: false,
            // Enhanced embedding options (search-time only)
            rerank: false,
            rerank_model: None,
        }
    }
}

/// Get default exclusion patterns for directories that should be skipped during search.
/// These are common cache, build, and system directories that rarely contain user code.
pub fn get_default_exclude_patterns() -> Vec<String> {
    vec![
        // ck's own index directory
        ".ck".to_string(),
        // AI/ML model cache directories
        ".fastembed_cache".to_string(),
        ".cache".to_string(),
        "__pycache__".to_string(),
        // Version control
        ".git".to_string(),
        ".svn".to_string(),
        ".hg".to_string(),
        // Build directories
        "target".to_string(),       // Rust
        "build".to_string(),        // Various
        "dist".to_string(),         // JavaScript/Python
        "node_modules".to_string(), // JavaScript
        ".gradle".to_string(),      // Java
        ".mvn".to_string(),         // Maven
        "bin".to_string(),          // Various
        "obj".to_string(),          // .NET
        // Python virtual environments
        "venv".to_string(),
        ".venv".to_string(),
        "env".to_string(),
        ".env".to_string(),
        "virtualenv".to_string(),
        // IDE/Editor directories
        ".vscode".to_string(),
        ".idea".to_string(),
        ".eclipse".to_string(),
        // Temporary directories
        "tmp".to_string(),
        "temp".to_string(),
        ".tmp".to_string(),
    ]
}

pub fn get_sidecar_path(repo_root: &Path, file_path: &Path) -> PathBuf {
    let relative = file_path.strip_prefix(repo_root).unwrap_or(file_path);
    let mut sidecar = repo_root.join(".ck");
    sidecar.push(relative);
    let ext = relative
        .extension()
        .map(|e| format!("{}.ck", e.to_string_lossy()))
        .unwrap_or_else(|| "ck".to_string());
    sidecar.set_extension(ext);
    sidecar
}

pub fn compute_file_hash(path: &Path) -> Result<String> {
    let data = std::fs::read(path)?;
    let hash = blake3::hash(&data);
    Ok(hash.to_hex().to_string())
}

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

    #[test]
    fn test_span_creation() {
        let span = Span {
            byte_start: 0,
            byte_end: 10,
            line_start: 1,
            line_end: 2,
        };

        assert_eq!(span.byte_start, 0);
        assert_eq!(span.byte_end, 10);
        assert_eq!(span.line_start, 1);
        assert_eq!(span.line_end, 2);
    }

    #[test]
    fn test_search_options_default() {
        let options = SearchOptions::default();
        assert!(matches!(options.mode, SearchMode::Regex));
        assert_eq!(options.query, "");
        assert_eq!(options.path, PathBuf::from("."));
        assert_eq!(options.top_k, None);
        assert_eq!(options.threshold, None);
        assert!(!options.case_insensitive);
        assert!(!options.whole_word);
        assert!(!options.fixed_string);
        assert!(!options.line_numbers);
        assert_eq!(options.context_lines, 0);
        assert!(options.recursive);
        assert!(!options.json_output);
        assert!(!options.reindex);
        assert!(!options.show_scores);
        assert!(!options.show_filenames);
    }

    #[test]
    fn test_file_metadata_serialization() {
        let metadata = FileMetadata {
            path: PathBuf::from("test.txt"),
            hash: "abc123".to_string(),
            last_modified: 1234567890,
            size: 1024,
        };

        let json = serde_json::to_string(&metadata).unwrap();
        let deserialized: FileMetadata = serde_json::from_str(&json).unwrap();

        assert_eq!(metadata.path, deserialized.path);
        assert_eq!(metadata.hash, deserialized.hash);
        assert_eq!(metadata.last_modified, deserialized.last_modified);
        assert_eq!(metadata.size, deserialized.size);
    }

    #[test]
    fn test_search_result_serialization() {
        let result = SearchResult {
            file: PathBuf::from("test.txt"),
            span: Span {
                byte_start: 0,
                byte_end: 10,
                line_start: 1,
                line_end: 1,
            },
            score: 0.95,
            preview: "hello world".to_string(),
            lang: Some(Language::Rust),
            symbol: Some("main".to_string()),
            chunk_hash: Some("abc123".to_string()),
            index_epoch: Some(1699123456),
        };

        let json = serde_json::to_string(&result).unwrap();
        let deserialized: SearchResult = serde_json::from_str(&json).unwrap();

        assert_eq!(result.file, deserialized.file);
        assert_eq!(result.score, deserialized.score);
        assert_eq!(result.preview, deserialized.preview);
        assert_eq!(result.lang, deserialized.lang);
        assert_eq!(result.symbol, deserialized.symbol);
        assert_eq!(result.chunk_hash, deserialized.chunk_hash);
        assert_eq!(result.index_epoch, deserialized.index_epoch);
    }

    #[test]
    fn test_jsonl_search_result_conversion() {
        let result = SearchResult {
            file: PathBuf::from("src/auth.rs"),
            span: Span {
                byte_start: 1203,
                byte_end: 1456,
                line_start: 42,
                line_end: 58,
            },
            score: 0.89,
            preview: "function authenticate(user) {...}".to_string(),
            lang: Some(Language::Rust),
            symbol: Some("authenticate".to_string()),
            chunk_hash: Some("abc123def456".to_string()),
            index_epoch: Some(1699123456),
        };

        // Test with snippet
        let jsonl_with_snippet = JsonlSearchResult::from_search_result(&result, true);
        assert_eq!(jsonl_with_snippet.path, "src/auth.rs");
        assert_eq!(jsonl_with_snippet.span.line_start, 42);
        assert_eq!(jsonl_with_snippet.language, Some("rust".to_string()));
        assert_eq!(
            jsonl_with_snippet.snippet,
            Some("function authenticate(user) {...}".to_string())
        );
        assert_eq!(jsonl_with_snippet.score, Some(0.89));
        assert_eq!(
            jsonl_with_snippet.chunk_hash,
            Some("abc123def456".to_string())
        );
        assert_eq!(jsonl_with_snippet.index_epoch, Some(1699123456));

        // Test without snippet
        let jsonl_no_snippet = JsonlSearchResult::from_search_result(&result, false);
        assert_eq!(jsonl_no_snippet.snippet, None);
        assert_eq!(jsonl_no_snippet.path, "src/auth.rs");
    }

    #[test]
    fn test_get_sidecar_path() {
        let repo_root = PathBuf::from("/home/user/project");
        let file_path = PathBuf::from("/home/user/project/src/main.rs");

        let sidecar = get_sidecar_path(&repo_root, &file_path);
        let expected = PathBuf::from("/home/user/project/.ck/src/main.rs.ck");

        assert_eq!(sidecar, expected);
    }

    #[test]
    fn test_get_sidecar_path_no_extension() {
        let repo_root = PathBuf::from("/project");
        let file_path = PathBuf::from("/project/README");

        let sidecar = get_sidecar_path(&repo_root, &file_path);
        let expected = PathBuf::from("/project/.ck/README.ck");

        assert_eq!(sidecar, expected);
    }

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

        fs::write(&file_path, "hello world").unwrap();

        let hash1 = compute_file_hash(&file_path).unwrap();
        let hash2 = compute_file_hash(&file_path).unwrap();

        // Same content should produce same hash
        assert_eq!(hash1, hash2);
        assert!(!hash1.is_empty());

        // Different content should produce different hash
        fs::write(&file_path, "hello rust").unwrap();
        let hash3 = compute_file_hash(&file_path).unwrap();
        assert_ne!(hash1, hash3);
    }

    #[test]
    fn test_compute_file_hash_nonexistent() {
        let result = compute_file_hash(&PathBuf::from("nonexistent.txt"));
        assert!(result.is_err());
    }

    #[test]
    fn test_json_search_result_serialization() {
        let signals = SearchSignals {
            lex_rank: Some(1),
            vec_rank: Some(2),
            rrf_score: 0.85,
        };

        let result = JsonSearchResult {
            file: "test.txt".to_string(),
            span: Span {
                byte_start: 0,
                byte_end: 5,
                line_start: 1,
                line_end: 1,
            },
            lang: None, // txt is not a supported language
            symbol: None,
            score: 0.95,
            signals,
            preview: "hello".to_string(),
            model: "bge-small".to_string(),
        };

        let json = serde_json::to_string(&result).unwrap();
        let deserialized: JsonSearchResult = serde_json::from_str(&json).unwrap();

        assert_eq!(result.file, deserialized.file);
        assert_eq!(result.score, deserialized.score);
        assert_eq!(result.signals.rrf_score, deserialized.signals.rrf_score);
        assert_eq!(result.model, deserialized.model);
    }

    #[test]
    fn test_language_from_extension() {
        assert_eq!(Language::from_extension("rs"), Some(Language::Rust));
        assert_eq!(Language::from_extension("py"), Some(Language::Python));
        assert_eq!(Language::from_extension("js"), Some(Language::JavaScript));
        assert_eq!(Language::from_extension("ts"), Some(Language::TypeScript));
        assert_eq!(Language::from_extension("tsx"), Some(Language::TypeScript));
        assert_eq!(Language::from_extension("hs"), Some(Language::Haskell));
        assert_eq!(Language::from_extension("lhs"), Some(Language::Haskell));
        assert_eq!(Language::from_extension("go"), Some(Language::Go));
        assert_eq!(Language::from_extension("java"), Some(Language::Java));
        assert_eq!(Language::from_extension("c"), Some(Language::C));
        assert_eq!(Language::from_extension("cpp"), Some(Language::Cpp));
        assert_eq!(Language::from_extension("cs"), Some(Language::CSharp));
        assert_eq!(Language::from_extension("rb"), Some(Language::Ruby));
        assert_eq!(Language::from_extension("php"), Some(Language::Php));
        assert_eq!(Language::from_extension("swift"), Some(Language::Swift));
        assert_eq!(Language::from_extension("kt"), Some(Language::Kotlin));
        assert_eq!(Language::from_extension("kts"), Some(Language::Kotlin));
        assert_eq!(Language::from_extension("unknown"), None);
    }

    #[test]
    fn test_language_from_path() {
        assert_eq!(
            Language::from_path(&PathBuf::from("test.rs")),
            Some(Language::Rust)
        );
        assert_eq!(
            Language::from_path(&PathBuf::from("test.py")),
            Some(Language::Python)
        );
        assert_eq!(
            Language::from_path(&PathBuf::from("test.js")),
            Some(Language::JavaScript)
        );
        assert_eq!(
            Language::from_path(&PathBuf::from("test.hs")),
            Some(Language::Haskell)
        );
        assert_eq!(
            Language::from_path(&PathBuf::from("test.lhs")),
            Some(Language::Haskell)
        );
        assert_eq!(
            Language::from_path(&PathBuf::from("test.go")),
            Some(Language::Go)
        );
        assert_eq!(Language::from_path(&PathBuf::from("test.unknown")), None); // unknown extensions return None
        assert_eq!(Language::from_path(&PathBuf::from("noext")), None); // no extension
    }

    #[test]
    fn test_language_display() {
        assert_eq!(Language::Rust.to_string(), "rust");
        assert_eq!(Language::Python.to_string(), "python");
        assert_eq!(Language::JavaScript.to_string(), "javascript");
        assert_eq!(Language::TypeScript.to_string(), "typescript");
        assert_eq!(Language::Go.to_string(), "go");
        assert_eq!(Language::Java.to_string(), "java");
    }
}