cqs 1.25.0

Code intelligence and RAG for AI agents. Semantic search, call graphs, impact analysis, type dependencies, and smart context assembly — in single tool calls. 54 languages + L5X/L5K PLC exports, 91.2% Recall@1 (BGE-large), 0.951 MRR (296 queries). Local ML, GPU-accelerated.
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
//! Drift detection — find functions that changed semantically between snapshots
//!
//! Thin wrapper over `semantic_diff()` focused on the "modified" entries.
//! Sorts by drift magnitude (most changed first), supports min-drift filtering.

use std::path::PathBuf;

use crate::diff::semantic_diff;
use crate::store::{Store, StoreError};

/// A function that drifted between snapshots.
#[derive(Debug, Clone, serde::Serialize)]
pub struct DriftEntry {
    /// Function/class name
    pub name: String,
    /// Source file path
    #[serde(serialize_with = "crate::serialize_path_normalized")]
    pub file: PathBuf,
    /// Type of code element
    pub chunk_type: crate::language::ChunkType,
    /// Cosine similarity (lower = more drift)
    pub similarity: f32,
    /// 1.0 - similarity (higher = more drift)
    pub drift: f32,
}

/// Result of drift detection.
#[derive(Debug, Clone, serde::Serialize)]
pub struct DriftResult {
    /// Reference name compared against
    pub reference: String,
    /// Similarity threshold used
    pub threshold: f32,
    /// Minimum drift filter applied
    pub min_drift: f32,
    /// Functions that drifted, sorted by drift descending
    pub drifted: Vec<DriftEntry>,
    /// Total matched pairs compared (drifted + unchanged)
    pub total_compared: usize,
    /// Count of functions below threshold (not drifted)
    pub unchanged: usize,
}

/// Detect semantic drift between a reference and the project.
/// Uses `semantic_diff()` internally, filtering to only the "modified" entries
/// and presenting them as drift (1.0 - similarity).
pub fn detect_drift(
    ref_store: &Store,
    project_store: &Store,
    ref_name: &str,
    threshold: f32,
    min_drift: f32,
    language_filter: Option<&str>,
) -> Result<DriftResult, StoreError> {
    let _span =
        tracing::info_span!("detect_drift", reference = ref_name, threshold, min_drift).entered();

    let diff = semantic_diff(
        ref_store,
        project_store,
        ref_name,
        "project",
        threshold,
        language_filter,
    )?;

    let total_compared = diff.modified.len() + diff.unchanged_count;

    let mut drifted: Vec<DriftEntry> = diff
        .modified
        .into_iter()
        .filter_map(|entry| {
            let sim = entry.similarity?; // skip entries with unknown similarity
            let drift = 1.0 - sim;
            if drift >= min_drift {
                Some(DriftEntry {
                    name: entry.name,
                    file: entry.file,
                    chunk_type: entry.chunk_type,
                    similarity: sim,
                    drift,
                })
            } else {
                None
            }
        })
        .collect();

    // Sort by drift desc (most changed first)
    drifted.sort_by(|a, b| b.drift.total_cmp(&a.drift));

    tracing::info!(
        drifted = drifted.len(),
        total_compared,
        "Drift detection complete"
    );

    Ok(DriftResult {
        reference: ref_name.to_string(),
        threshold,
        min_drift,
        drifted,
        total_compared,
        unchanged: diff.unchanged_count,
    })
}

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

    /// Creates a new in-memory Store instance with a temporary database for testing purposes.
    /// # Returns
    /// A tuple containing:
    /// - `Store`: A newly initialized store instance
    /// - `TempDir`: The temporary directory containing the database file, kept alive for the store's lifetime
    /// # Panics
    /// Panics if temporary directory creation, database opening, or store initialization fails.
    fn make_store() -> (Store, TempDir) {
        let dir = TempDir::new().unwrap();
        let db_path = dir.path().join("index.db");
        let store = Store::open(&db_path).unwrap();
        store.init(&crate::store::ModelInfo::default()).unwrap();
        (store, dir)
    }

    #[test]
    fn test_drift_empty_stores() {
        let (ref_store, _d1) = make_store();
        let (proj_store, _d2) = make_store();

        let result = detect_drift(&ref_store, &proj_store, "test-ref", 0.95, 0.0, None).unwrap();
        assert!(result.drifted.is_empty());
        assert_eq!(result.total_compared, 0);
        assert_eq!(result.unchanged, 0);
    }

    #[test]
    fn test_drift_entry_fields() {
        let entry = DriftEntry {
            name: "foo".into(),
            file: "src/foo.rs".into(),
            chunk_type: ChunkType::Function,
            similarity: 0.7,
            drift: 0.3,
        };
        assert!((entry.drift - (1.0 - entry.similarity)).abs() < f32::EPSILON);
    }

    #[test]
    fn test_drift_sort_order() {
        let mut entries = vec![
            DriftEntry {
                name: "a".into(),
                file: "a.rs".into(),
                chunk_type: ChunkType::Function,
                similarity: 0.9,
                drift: 0.1,
            },
            DriftEntry {
                name: "b".into(),
                file: "b.rs".into(),
                chunk_type: ChunkType::Function,
                similarity: 0.5,
                drift: 0.5,
            },
            DriftEntry {
                name: "c".into(),
                file: "c.rs".into(),
                chunk_type: ChunkType::Function,
                similarity: 0.7,
                drift: 0.3,
            },
        ];
        entries.sort_by(|a, b| b.drift.total_cmp(&a.drift));
        assert_eq!(entries[0].name, "b"); // most drift
        assert_eq!(entries[1].name, "c");
        assert_eq!(entries[2].name, "a"); // least drift
    }

    #[test]
    fn test_drift_min_filter() {
        // Verify that entries below min_drift are excluded
        let entries = vec![
            DriftEntry {
                name: "small".into(),
                file: "a.rs".into(),
                chunk_type: ChunkType::Function,
                similarity: 0.92,
                drift: 0.08,
            },
            DriftEntry {
                name: "big".into(),
                file: "b.rs".into(),
                chunk_type: ChunkType::Function,
                similarity: 0.5,
                drift: 0.5,
            },
        ];
        let min_drift = 0.1;
        let filtered: Vec<_> = entries
            .into_iter()
            .filter(|e| e.drift >= min_drift)
            .collect();
        assert_eq!(filtered.len(), 1);
        assert_eq!(filtered[0].name, "big");
    }

    // --- Helpers for populated-store tests ---

    use crate::embedder::Embedding;
    use crate::language::ChunkType;
    use crate::parser::types::{Chunk, Language};
    use std::path::PathBuf;

    /// Creates a mock Chunk representing a function with a generated blake3 content hash.
    /// # Arguments
    /// * `name` - The name of the function for the chunk
    /// * `file` - The file path where the chunk is located
    /// * `lang` - The programming language of the chunk
    /// # Returns
    /// A fully initialized Chunk struct with synthetic function content, a computed blake3 hash, and default metadata. The chunk spans lines 1-5 with a placeholder function body.
    fn make_chunk(name: &str, file: &str, lang: Language) -> Chunk {
        let content = format!("fn {}() {{ /* body */ }}", name);
        let hash = blake3::hash(content.as_bytes()).to_hex().to_string();
        Chunk {
            id: format!("{}:1:{}", file, &hash[..8]),
            file: PathBuf::from(file),
            language: lang,
            chunk_type: ChunkType::Function,
            name: name.to_string(),
            signature: format!("fn {}()", name),
            content,
            doc: None,
            line_start: 1,
            line_end: 5,
            content_hash: hash,
            parent_id: None,
            window_idx: None,
            parent_type_name: None,
        }
    }

    /// Create an embedding with a distinct direction based on `seed`.
    /// Uses `seed` as the index of a "hot" dimension (set to 1.0) while the
    /// rest are 0.0, ensuring different seeds produce orthogonal vectors
    /// (cosine similarity ≈ 0). The seed is taken modulo `EMBEDDING_DIM`.
    fn make_emb(seed: f32) -> Embedding {
        let mut v = vec![0.0f32; crate::EMBEDDING_DIM];
        let idx = (seed.abs() as usize) % crate::EMBEDDING_DIM;
        v[idx] = 1.0;
        Embedding::new(v)
    }

    // --- Populated-store tests (TC-14) ---

    #[test]
    fn test_drift_with_matching_functions() {
        let (ref_store, _d1) = make_store();
        let (proj_store, _d2) = make_store();

        let chunk = make_chunk("process_data", "src/lib.rs", Language::Rust);
        let emb = make_emb(1.0);

        ref_store.upsert_chunk(&chunk, &emb, Some(100)).unwrap();
        proj_store.upsert_chunk(&chunk, &emb, Some(100)).unwrap();

        let result = detect_drift(&ref_store, &proj_store, "test-ref", 0.95, 0.0, None).unwrap();

        assert_eq!(result.total_compared, 1);
        assert_eq!(result.unchanged, 1);
        assert!(
            result.drifted.is_empty(),
            "identical embeddings should produce no drift"
        );
    }

    #[test]
    fn test_drift_with_different_embeddings() {
        let (ref_store, _d1) = make_store();
        let (proj_store, _d2) = make_store();

        let chunk = make_chunk("process_data", "src/lib.rs", Language::Rust);
        let emb_ref = make_emb(1.0);
        let emb_proj = make_emb(2.0);

        ref_store.upsert_chunk(&chunk, &emb_ref, Some(100)).unwrap();
        proj_store
            .upsert_chunk(&chunk, &emb_proj, Some(100))
            .unwrap();

        let result = detect_drift(&ref_store, &proj_store, "test-ref", 0.95, 0.0, None).unwrap();

        assert!(
            result.total_compared >= 1,
            "should compare at least one pair"
        );
        assert!(
            !result.drifted.is_empty(),
            "different embeddings should produce drift"
        );
        let entry = &result.drifted[0];
        assert_eq!(entry.name, "process_data");
        assert!(entry.drift > 0.0, "drift should be positive");
        assert!(
            entry.similarity < 0.95,
            "similarity should be below threshold"
        );
    }

    #[test]
    fn test_drift_min_drift_filter_with_stores() {
        let (ref_store, _d1) = make_store();
        let (proj_store, _d2) = make_store();

        // Use partially overlapping embeddings so drift is moderate (not 0 or 1).
        // Ref: hot at index 0. Project: equal weight at indices 0 and 1.
        // Cosine similarity = 1 * (1/sqrt(2)) / (1 * 1) = 0.707...
        // Drift = 1 - 0.707 ≈ 0.293
        let mut ref_v = vec![0.0f32; crate::EMBEDDING_DIM];
        ref_v[0] = 1.0;
        let emb_ref = Embedding::new(ref_v);

        let mut proj_v = vec![0.0f32; crate::EMBEDDING_DIM];
        proj_v[0] = 1.0;
        proj_v[1] = 1.0;
        let norm = (2.0f32).sqrt();
        proj_v[0] /= norm;
        proj_v[1] /= norm;
        let emb_proj = Embedding::new(proj_v);

        let chunk = make_chunk("process_data", "src/lib.rs", Language::Rust);

        ref_store.upsert_chunk(&chunk, &emb_ref, Some(100)).unwrap();
        proj_store
            .upsert_chunk(&chunk, &emb_proj, Some(100))
            .unwrap();

        // First, confirm drift exists with min_drift=0.0
        let baseline = detect_drift(&ref_store, &proj_store, "test-ref", 0.95, 0.0, None).unwrap();
        assert!(
            !baseline.drifted.is_empty(),
            "precondition: drift should exist (drift ≈ 0.29)"
        );
        let actual_drift = baseline.drifted[0].drift;
        assert!(
            actual_drift > 0.2 && actual_drift < 0.4,
            "expected drift ≈ 0.29, got {actual_drift}"
        );

        // Now filter with min_drift above the actual drift — should exclude everything
        let result = detect_drift(&ref_store, &proj_store, "test-ref", 0.95, 0.5, None).unwrap();

        assert!(
            result.drifted.is_empty(),
            "min_drift=0.5 should filter out drift of ≈0.29"
        );
        // total_compared still counts the pair (it was compared, just filtered from output)
        assert!(result.total_compared >= 1);
    }

    #[test]
    fn test_drift_language_filter() {
        let (ref_store, _d1) = make_store();
        let (proj_store, _d2) = make_store();

        // Insert a Rust function with different embeddings
        let rust_chunk = make_chunk("rust_func", "src/lib.rs", Language::Rust);
        let emb_a = make_emb(1.0);
        let emb_b = make_emb(2.0);

        ref_store
            .upsert_chunk(&rust_chunk, &emb_a, Some(100))
            .unwrap();
        proj_store
            .upsert_chunk(&rust_chunk, &emb_b, Some(100))
            .unwrap();

        // Insert a Python function with different embeddings
        let py_chunk = make_chunk("py_func", "src/lib.py", Language::Python);
        let emb_c = make_emb(3.0);
        let emb_d = make_emb(4.0);

        ref_store
            .upsert_chunk(&py_chunk, &emb_c, Some(100))
            .unwrap();
        proj_store
            .upsert_chunk(&py_chunk, &emb_d, Some(100))
            .unwrap();

        // Filter to Rust only
        let result =
            detect_drift(&ref_store, &proj_store, "test-ref", 0.95, 0.0, Some("rust")).unwrap();

        // Should only see Rust function
        let names: Vec<&str> = result.drifted.iter().map(|e| e.name.as_str()).collect();
        assert!(
            !names.contains(&"py_func"),
            "Python function should be excluded by language filter"
        );
        // Rust func should be present (different embeddings → drift)
        assert!(
            names.contains(&"rust_func"),
            "Rust function should appear in drift results"
        );
        // total_compared should only count the Rust pair
        assert_eq!(
            result.total_compared, 1,
            "only one pair (rust) should be compared"
        );
    }
}