claudix 0.2.0

Local semantic search plugin for Claude Code
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
use std::fmt;
use std::path::{Component, Path, PathBuf};

use crate::error::{ClaudixError, RecoveryHint, Result};

/// Content-addressed chunk identifier: xxh3 of `(file_hash, byte_range)`.
///
/// Deliberately NOT row-unique. `FileHash` is xxh3 of file content, so two
/// different files with byte-identical content and matching byte ranges hash to
/// the same `ChunkId`. Storage keys rows by `(file_path, byte_start, chunk_id)`
/// — `file_path` disambiguates such collisions — so this is correct for the
/// current store. Do NOT promote `ChunkId` to a standalone primary or dedup key
/// without folding `file_path` into the hash first, or cross-file duplicates
/// will silently merge.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct ChunkId(pub u64);

impl fmt::Display for ChunkId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// 16-byte xxh3 file hash.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct FileHash(pub [u8; 16]);

impl fmt::Display for FileHash {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for b in &self.0 {
            write!(f, "{b:02x}")?;
        }
        Ok(())
    }
}

/// Embedding vector dimensionality.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct Dimension(pub u16);

/// Repo-relative path, always forward-slash normalized.
///
/// INVARIANT (advisory): `new`/`from_path` only normalize separators — they do
/// NOT enforce relativity. An absolute (`/etc/passwd`) or escaping (`../..`,
/// `C:\…`) string is accepted as-is. Any caller that joins this against a root
/// or crosses a trust boundary (hook payloads, stored rows, tool input) MUST
/// call [`RelativePath::reject_escape`] first to keep operations inside
/// `$CLAUDE_PROJECT_DIR`.
#[derive(
    Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
)]
pub struct RelativePath(String);

pub(crate) fn path_prefix_matches(path: &str, prefix: &str) -> bool {
    let prefix = prefix.trim_end_matches('/');
    let Some(rest) = path.strip_prefix(prefix) else {
        return false;
    };
    rest.is_empty()
        || rest.starts_with('/')
        || rest
            .strip_prefix('.')
            .is_some_and(|extension| !extension.contains(['/', '.']))
}

/// Reject absolute paths and parent-dir / root-dir / drive-prefix components.
///
/// Used wherever a project-relative path crosses a trust boundary (config
/// keys, hook payloads, search results) to prevent operations outside
/// `$CLAUDE_PROJECT_DIR`. `recovery` is the hint surfaced to the user when
/// rejection fires.
pub(crate) fn reject_path_escape(path: &Path, recovery: &'static str) -> Result<()> {
    if path.is_absolute() {
        return Err(ClaudixError::PathTraversal {
            path: path.to_path_buf(),
            recovery: RecoveryHint(recovery),
        });
    }
    for component in path.components() {
        if matches!(
            component,
            Component::ParentDir | Component::RootDir | Component::Prefix(_)
        ) {
            return Err(ClaudixError::PathTraversal {
                path: path.to_path_buf(),
                recovery: RecoveryHint(recovery),
            });
        }
    }
    Ok(())
}

impl RelativePath {
    pub(crate) fn reject_escape(&self, recovery: &'static str) -> Result<()> {
        reject_path_escape(&self.to_path_buf(), recovery)
    }

    /// Normalize separators only. Does NOT enforce relativity — see the type
    /// docs; trust-boundary callers must follow with [`Self::reject_escape`].
    pub fn new(s: impl Into<String>) -> Self {
        let raw = s.into();
        let normalized = raw.replace('\\', "/");
        Self(normalized)
    }

    pub fn from_path(path: &Path) -> Self {
        Self::new(path.to_string_lossy().as_ref())
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }

    pub fn starts_with(&self, prefix: &RelativePath) -> bool {
        path_prefix_matches(&self.0, prefix.as_str())
    }

    pub fn to_path_buf(&self) -> PathBuf {
        PathBuf::from(&self.0)
    }
}

impl fmt::Display for RelativePath {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

/// Byte range within a file (0-indexed, half-open).
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ByteRange {
    pub start: u32,
    pub end: u32,
}

/// Line range within a file (1-indexed, inclusive).
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct LineRange {
    pub start: u32,
    pub end: u32,
}

/// Source language.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Language {
    Rust,
    Python,
    JavaScript,
    TypeScript,
    Go,
    Java,
    C,
    Cpp,
    CSharp,
    Sql,
    Unknown,
}

impl Language {
    pub fn from_extension(ext: &str) -> Self {
        match ext {
            "rs" => Self::Rust,
            "py" => Self::Python,
            "js" | "mjs" | "cjs" => Self::JavaScript,
            "ts" | "tsx" => Self::TypeScript,
            "go" => Self::Go,
            "java" => Self::Java,
            "c" | "h" => Self::C,
            "cpp" | "cc" | "cxx" | "hpp" | "hxx" => Self::Cpp,
            "cs" => Self::CSharp,
            "sql" => Self::Sql,
            _ => Self::Unknown,
        }
    }

    /// Parse the canonical form used in storage / serialized chunks. Unknown
    /// strings round-trip to `Unknown` so corrupt data doesn't propagate.
    pub fn from_storage(value: &str) -> Self {
        match value {
            "rust" => Self::Rust,
            "python" => Self::Python,
            "javascript" => Self::JavaScript,
            "typescript" => Self::TypeScript,
            "go" => Self::Go,
            "java" => Self::Java,
            "c" => Self::C,
            "cpp" => Self::Cpp,
            "csharp" => Self::CSharp,
            "sql" => Self::Sql,
            _ => Self::Unknown,
        }
    }

    /// Parse a user-supplied language filter (CLI / MCP input). Accepts
    /// short aliases the storage form doesn't carry.
    pub fn from_filter_input(value: &str) -> Option<Self> {
        match value.trim().to_ascii_lowercase().as_str() {
            "rust" => Some(Self::Rust),
            "python" => Some(Self::Python),
            "javascript" | "js" => Some(Self::JavaScript),
            "typescript" | "ts" => Some(Self::TypeScript),
            "go" => Some(Self::Go),
            "java" => Some(Self::Java),
            "c" => Some(Self::C),
            "cpp" | "c++" => Some(Self::Cpp),
            "csharp" | "c#" | "cs" => Some(Self::CSharp),
            "sql" => Some(Self::Sql),
            "unknown" => Some(Self::Unknown),
            _ => None,
        }
    }

    pub fn as_str(self) -> &'static str {
        match self {
            Self::Rust => "rust",
            Self::Python => "python",
            Self::JavaScript => "javascript",
            Self::TypeScript => "typescript",
            Self::Go => "go",
            Self::Java => "java",
            Self::C => "c",
            Self::Cpp => "cpp",
            Self::CSharp => "csharp",
            Self::Sql => "sql",
            Self::Unknown => "unknown",
        }
    }
}

impl fmt::Display for Language {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Semantic kind of a code chunk.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ChunkKind {
    Function,
    Method,
    Struct,
    Class,
    Enum,
    Trait,
    Interface,
    Module,
    Impl,
    Macro,
    Table,
    View,
    Trigger,
    Other,
}

impl ChunkKind {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Function => "function",
            Self::Method => "method",
            Self::Struct => "struct",
            Self::Class => "class",
            Self::Enum => "enum",
            Self::Trait => "trait",
            Self::Interface => "interface",
            Self::Module => "module",
            Self::Impl => "impl",
            Self::Macro => "macro",
            Self::Table => "table",
            Self::View => "view",
            Self::Trigger => "trigger",
            Self::Other => "other",
        }
    }

    /// Parse the canonical form used in storage. Unknown strings map to
    /// `Other` so legacy or corrupt rows don't break read paths.
    pub fn from_storage(value: &str) -> Self {
        match value {
            "function" => Self::Function,
            "method" => Self::Method,
            "struct" => Self::Struct,
            "class" => Self::Class,
            "enum" => Self::Enum,
            "trait" => Self::Trait,
            "interface" => Self::Interface,
            "module" => Self::Module,
            "impl" => Self::Impl,
            "macro" => Self::Macro,
            "table" => Self::Table,
            "view" => Self::View,
            "trigger" => Self::Trigger,
            _ => Self::Other,
        }
    }
}

impl fmt::Display for ChunkKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Fundamental indexable unit of source code.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Chunk {
    pub id: ChunkId,
    pub file_path: RelativePath,
    pub language: Language,
    pub kind: ChunkKind,
    pub name: Option<String>,
    pub line_range: LineRange,
    pub byte_range: ByteRange,
    pub file_hash: FileHash,
    pub content: String,
}

/// Chunk with its embedding vector.
#[derive(Debug, Clone)]
pub struct EmbeddedChunk {
    pub chunk: Chunk,
    pub vector: Vec<f32>,
}

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

    #[test]
    fn relative_path_normalizes_windows_separators() {
        let path = RelativePath::new(r"src\nested\file.rs");

        assert_eq!(path.as_str(), "src/nested/file.rs");
        assert_eq!(path.to_string(), "src/nested/file.rs");
    }

    #[test]
    fn relative_path_prefix_match_uses_normalized_form() {
        let path = RelativePath::new("src/nested/file.rs");
        let prefix = RelativePath::new(r"src\nested");

        assert!(path.starts_with(&prefix));
    }

    #[test]
    fn path_prefix_matches_exact_directory_and_extension_boundaries() {
        assert!(path_prefix_matches("src/math", "src/math"));
        assert!(path_prefix_matches("src/math/add.rs", "src/math"));
        assert!(path_prefix_matches("src/math.rs", "src/math"));
        assert!(!path_prefix_matches("src/math_extra.rs", "src/math"));
        assert!(!path_prefix_matches("src/math.rs.bak", "src/math"));
    }

    #[test]
    fn file_hash_displays_as_lowercase_hex() {
        let hash = FileHash([0xAB; 16]);

        assert_eq!(hash.to_string(), "abababababababababababababababab");
    }

    #[test]
    fn chunk_id_display_is_decimal() {
        assert_eq!(ChunkId(42).to_string(), "42");
    }

    #[test]
    fn language_detects_known_extensions() {
        assert_eq!(Language::from_extension("rs"), Language::Rust);
        assert_eq!(Language::from_extension("tsx"), Language::TypeScript);
        assert_eq!(Language::from_extension("hpp"), Language::Cpp);
        assert_eq!(Language::from_extension("cs"), Language::CSharp);
        assert_eq!(Language::from_extension("sql"), Language::Sql);
        assert_eq!(Language::from_extension("unknown"), Language::Unknown);
    }

    #[test]
    fn csharp_and_sql_round_trip_through_storage_and_filters() {
        for lang in [Language::CSharp, Language::Sql] {
            assert_eq!(Language::from_storage(lang.as_str()), lang);
        }
        assert_eq!(Language::from_filter_input("c#"), Some(Language::CSharp));
        assert_eq!(
            Language::from_filter_input("csharp"),
            Some(Language::CSharp)
        );
        assert_eq!(Language::from_filter_input("sql"), Some(Language::Sql));

        for kind in [ChunkKind::Table, ChunkKind::View, ChunkKind::Trigger] {
            assert_eq!(ChunkKind::from_storage(kind.as_str()), kind);
        }
    }

    #[test]
    fn language_display_matches_serialized_name() {
        assert_eq!(Language::JavaScript.to_string(), "javascript");
        assert_eq!(Language::Unknown.to_string(), "unknown");
    }

    #[test]
    fn chunk_kind_display_matches_kind_name() {
        assert_eq!(ChunkKind::Function.to_string(), "function");
        assert_eq!(ChunkKind::Macro.to_string(), "macro");
        assert_eq!(ChunkKind::Other.to_string(), "other");
    }

    #[test]
    fn relative_path_starts_with_respects_segment_boundary() {
        let prefix = RelativePath::new("src/math");
        assert!(RelativePath::new("src/math.rs").starts_with(&prefix));
        assert!(RelativePath::new("src/math/util.rs").starts_with(&prefix));
        assert!(RelativePath::new("src/math").starts_with(&prefix));
        assert!(!RelativePath::new("src/mathematics.rs").starts_with(&prefix));
        assert!(!RelativePath::new("src/mathx").starts_with(&prefix));

        let prefix_slash = RelativePath::new("src/math/");
        assert!(RelativePath::new("src/math/util.rs").starts_with(&prefix_slash));
        assert!(!RelativePath::new("src/mathematics.rs").starts_with(&prefix_slash));
    }
}