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
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
//! Diff-aware impact tests (map_hunks_to_functions, analyze_diff_impact)

mod common;

use common::{mock_embedding, TestStore};
use cqs::parser::{CallSite, Chunk, ChunkType, FunctionCalls, Language};
use cqs::{analyze_diff_impact, map_hunks_to_functions, ChangedFunction};
use cqs::{parse_unified_diff, DiffHunk};
use std::path::{Path, PathBuf};

/// Create a test chunk with custom file and line range
fn chunk_at(name: &str, file: &str, line_start: u32, line_end: u32) -> Chunk {
    let content = format!("fn {}() {{ }}", name);
    let hash = blake3::hash(content.as_bytes()).to_hex().to_string();
    Chunk {
        id: format!("{}:{}:{}", file, line_start, &hash[..8]),
        file: PathBuf::from(file),
        language: Language::Rust,
        chunk_type: ChunkType::Function,
        name: name.to_string(),
        signature: format!("fn {}()", name),
        content,
        doc: None,
        line_start,
        line_end,
        content_hash: hash,
        parent_id: None,
        window_idx: None,
        parent_type_name: None,
    }
}

/// Insert chunks into the store
fn insert_chunks(store: &TestStore, chunks: &[Chunk]) {
    let emb = mock_embedding(1.0);
    let pairs: Vec<_> = chunks.iter().map(|c| (c.clone(), emb.clone())).collect();
    store.upsert_chunks_batch(&pairs, Some(12345)).unwrap();
}

/// Insert function call graph entries
fn insert_calls(store: &TestStore, file: &str, calls: &[(&str, u32, &[(&str, u32)])]) {
    let fc: Vec<FunctionCalls> = calls
        .iter()
        .map(|(name, line, callees)| FunctionCalls {
            name: name.to_string(),
            line_start: *line,
            calls: callees
                .iter()
                .map(|(callee, cline)| CallSite {
                    callee_name: callee.to_string(),
                    line_number: *cline,
                })
                .collect(),
        })
        .collect();
    store.upsert_function_calls(Path::new(file), &fc).unwrap();
}

// ===== map_hunks_to_functions tests =====

#[test]
fn test_map_hunks_to_functions() {
    let store = TestStore::new();
    let chunks = vec![
        chunk_at("foo", "src/lib.rs", 10, 20),
        chunk_at("bar", "src/lib.rs", 30, 40),
    ];
    insert_chunks(&store, &chunks);

    // Hunk at lines 15-17 overlaps foo (10-20)
    let hunks = vec![DiffHunk {
        file: PathBuf::from("src/lib.rs"),
        start: 15,
        count: 3,
    }];

    let result = map_hunks_to_functions(&store, &hunks);
    assert_eq!(result.len(), 1);
    assert_eq!(result[0].name, "foo");
}

#[test]
fn test_map_hunks_no_overlap() {
    let store = TestStore::new();
    let chunks = vec![
        chunk_at("foo", "src/lib.rs", 10, 20),
        chunk_at("bar", "src/lib.rs", 30, 40),
    ];
    insert_chunks(&store, &chunks);

    // Hunk at lines 22-28 — between foo and bar
    let hunks = vec![DiffHunk {
        file: PathBuf::from("src/lib.rs"),
        start: 22,
        count: 7,
    }];

    let result = map_hunks_to_functions(&store, &hunks);
    assert!(result.is_empty(), "Should find no functions in the gap");
}

#[test]
fn test_map_hunks_partial_overlap() {
    let store = TestStore::new();
    let chunks = vec![chunk_at("foo", "src/lib.rs", 10, 20)];
    insert_chunks(&store, &chunks);

    // Hunk starts at line 10 (first line of foo)
    let hunks = vec![DiffHunk {
        file: PathBuf::from("src/lib.rs"),
        start: 10,
        count: 1,
    }];

    let result = map_hunks_to_functions(&store, &hunks);
    assert_eq!(result.len(), 1);
    assert_eq!(result[0].name, "foo");
}

#[test]
fn test_map_hunks_boundary_off_by_one() {
    let store = TestStore::new();
    // Function at lines 10-20
    let chunks = vec![chunk_at("foo", "src/lib.rs", 10, 20)];
    insert_chunks(&store, &chunks);

    // Hunk covers lines [8, 10) — exclusive end at 10 means it doesn't touch foo
    let hunks = vec![DiffHunk {
        file: PathBuf::from("src/lib.rs"),
        start: 8,
        count: 2, // lines 8, 9 — exclusive end at 10
    }];

    let result = map_hunks_to_functions(&store, &hunks);
    assert!(
        result.is_empty(),
        "Hunk ending at line 10 (exclusive) should NOT match chunk starting at line 10"
    );
}

#[test]
fn test_map_hunks_non_indexed_file() {
    let store = TestStore::new();
    // No chunks in the store for this file

    let hunks = vec![DiffHunk {
        file: PathBuf::from("src/unknown.rs"),
        start: 1,
        count: 10,
    }];

    let result = map_hunks_to_functions(&store, &hunks);
    assert!(result.is_empty(), "Non-indexed file should return empty");
}

// ===== analyze_diff_impact tests =====

#[test]
fn test_diff_impact_aggregation() {
    let store = TestStore::new();

    // Two changed functions
    let chunks = vec![
        chunk_at("fn_a", "src/lib.rs", 10, 20),
        chunk_at("fn_b", "src/lib.rs", 30, 40),
        // shared_caller calls both fn_a and fn_b
        chunk_at("shared_caller", "src/app.rs", 1, 10),
        // test calls shared_caller
        chunk_at("test_shared", "tests/test.rs", 1, 10),
    ];
    insert_chunks(&store, &chunks);

    insert_calls(
        &store,
        "src/app.rs",
        &[("shared_caller", 1, &[("fn_a", 5), ("fn_b", 8)])],
    );
    insert_calls(
        &store,
        "tests/test.rs",
        &[("test_shared", 1, &[("shared_caller", 3)])],
    );

    let changed = map_hunks_to_functions(
        &store,
        &[
            DiffHunk {
                file: PathBuf::from("src/lib.rs"),
                start: 15,
                count: 1,
            },
            DiffHunk {
                file: PathBuf::from("src/lib.rs"),
                start: 35,
                count: 1,
            },
        ],
    );

    assert_eq!(changed.len(), 2, "Should find fn_a and fn_b");

    let result = analyze_diff_impact(&store, changed, std::path::Path::new("/test")).unwrap();
    // shared_caller appears once even though it calls both changed functions
    assert!(
        result.all_callers.len() <= 1,
        "Shared caller should be deduped: got {}",
        result.all_callers.len()
    );
}

#[test]
fn test_diff_impact_empty_functions() {
    let store = TestStore::new();

    let result = analyze_diff_impact(&store, vec![], std::path::Path::new("/test")).unwrap();
    assert!(result.changed_functions.is_empty());
    assert!(result.all_callers.is_empty());
    assert!(result.all_tests.is_empty());
    assert_eq!(result.summary.changed_count, 0);
    assert_eq!(result.summary.caller_count, 0);
    assert_eq!(result.summary.test_count, 0);
}

// ===== End-to-end: parse diff → map → analyze =====

#[test]
fn test_diff_to_impact_end_to_end() {
    let store = TestStore::new();

    let chunks = vec![
        chunk_at("search_fn", "src/search.rs", 10, 30),
        chunk_at("cmd_query", "src/cli.rs", 1, 20),
    ];
    insert_chunks(&store, &chunks);

    insert_calls(
        &store,
        "src/cli.rs",
        &[("cmd_query", 1, &[("search_fn", 10)])],
    );

    let diff = "\
diff --git a/src/search.rs b/src/search.rs
--- a/src/search.rs
+++ b/src/search.rs
@@ -15,3 +15,4 @@ fn search_fn() {
     let x = 1;
+    let y = 2;
";

    let hunks = parse_unified_diff(diff);
    assert_eq!(hunks.len(), 1);

    let changed = map_hunks_to_functions(&store, &hunks);
    assert_eq!(changed.len(), 1);
    assert_eq!(changed[0].name, "search_fn");

    let result = analyze_diff_impact(&store, changed, std::path::Path::new("/test")).unwrap();

    assert_eq!(result.summary.changed_count, 1);
    // cmd_query should be in callers
    assert!(
        result.all_callers.iter().any(|c| c.name == "cmd_query"),
        "cmd_query should be an affected caller"
    );
}

// ===== Test discovery verification (TC-9) =====

/// Create a test chunk (name starts with "test_") recognized as a test by the store
fn test_chunk_at(name: &str, file: &str, line_start: u32, line_end: u32) -> Chunk {
    let content = format!("#[test] fn {}() {{ }}", name);
    let hash = blake3::hash(content.as_bytes()).to_hex().to_string();
    Chunk {
        id: format!("{}:{}:{}", file, line_start, &hash[..8]),
        file: PathBuf::from(file),
        language: Language::Rust,
        chunk_type: ChunkType::Function,
        name: name.to_string(),
        signature: format!("fn {}()", name),
        content,
        doc: None,
        line_start,
        line_end,
        content_hash: hash,
        parent_id: None,
        window_idx: None,
        parent_type_name: None,
    }
}

#[test]
fn test_diff_impact_all_tests_populated() {
    let store = TestStore::new();

    // Setup: changed_fn -> caller_fn, test_fn -> caller_fn
    let chunks = vec![
        chunk_at("changed_fn", "src/lib.rs", 10, 20),
        chunk_at("caller_fn", "src/app.rs", 1, 15),
        test_chunk_at("test_caller", "tests/app_test.rs", 1, 10),
    ];
    insert_chunks(&store, &chunks);

    insert_calls(
        &store,
        "src/app.rs",
        &[("caller_fn", 1, &[("changed_fn", 5)])],
    );
    insert_calls(
        &store,
        "tests/app_test.rs",
        &[("test_caller", 1, &[("caller_fn", 3)])],
    );

    let changed = vec![ChangedFunction {
        name: "changed_fn".to_string(),
        file: PathBuf::from("src/lib.rs"),
        line_start: 10,
    }];

    let result = analyze_diff_impact(&store, changed, std::path::Path::new("/test")).unwrap();

    // all_tests should contain test_caller
    assert_eq!(result.summary.test_count, 1, "Should find 1 affected test");
    assert!(
        result.all_tests.iter().any(|t| t.name == "test_caller"),
        "test_caller should be in all_tests, got: {:?}",
        result.all_tests.iter().map(|t| &t.name).collect::<Vec<_>>()
    );

    // Verify the test's via field points to the changed function
    let test_entry = result
        .all_tests
        .iter()
        .find(|t| t.name == "test_caller")
        .expect("test_caller should be present");
    assert_eq!(
        test_entry.via, "changed_fn",
        "via should point to the changed function that leads to this test"
    );
    assert!(
        test_entry.call_depth > 0,
        "call_depth should be > 0 (test is not the changed function itself)"
    );
}

#[test]
fn test_diff_impact_via_attribution_multiple_functions() {
    let store = TestStore::new();

    // Two changed functions, each with a dedicated test path:
    //   test_alpha -> mid_alpha -> fn_alpha (changed)
    //   test_beta -> fn_beta (changed)
    let chunks = vec![
        chunk_at("fn_alpha", "src/lib.rs", 10, 20),
        chunk_at("fn_beta", "src/lib.rs", 30, 40),
        chunk_at("mid_alpha", "src/app.rs", 1, 10),
        test_chunk_at("test_alpha", "tests/test.rs", 1, 10),
        test_chunk_at("test_beta", "tests/test.rs", 20, 30),
    ];
    insert_chunks(&store, &chunks);

    // mid_alpha calls fn_alpha
    insert_calls(
        &store,
        "src/app.rs",
        &[("mid_alpha", 1, &[("fn_alpha", 5)])],
    );
    // test_alpha calls mid_alpha, test_beta calls fn_beta
    insert_calls(
        &store,
        "tests/test.rs",
        &[
            ("test_alpha", 1, &[("mid_alpha", 3)]),
            ("test_beta", 20, &[("fn_beta", 25)]),
        ],
    );

    let changed = vec![
        ChangedFunction {
            name: "fn_alpha".to_string(),
            file: PathBuf::from("src/lib.rs"),
            line_start: 10,
        },
        ChangedFunction {
            name: "fn_beta".to_string(),
            file: PathBuf::from("src/lib.rs"),
            line_start: 30,
        },
    ];

    let result = analyze_diff_impact(&store, changed, std::path::Path::new("/test")).unwrap();

    assert_eq!(result.summary.test_count, 2, "Should find 2 affected tests");

    // Verify via attribution: test_alpha should point to fn_alpha
    let alpha_test = result
        .all_tests
        .iter()
        .find(|t| t.name == "test_alpha")
        .expect("test_alpha should be found");
    assert_eq!(
        alpha_test.via, "fn_alpha",
        "test_alpha's via should be fn_alpha (shortest path: test_alpha -> mid_alpha -> fn_alpha)"
    );

    // test_beta should point to fn_beta (direct call, depth 1)
    let beta_test = result
        .all_tests
        .iter()
        .find(|t| t.name == "test_beta")
        .expect("test_beta should be found");
    assert_eq!(
        beta_test.via, "fn_beta",
        "test_beta's via should be fn_beta (direct call)"
    );
    assert_eq!(
        beta_test.call_depth, 1,
        "test_beta is a direct caller of fn_beta, so depth should be 1"
    );
}

#[test]
fn test_diff_impact_shared_test_via_minimum_depth() {
    let store = TestStore::new();

    // A single test reaches two changed functions at different depths:
    //   test_shared -> fn_close (changed, depth 1)
    //   test_shared -> mid -> fn_far (changed, depth 2)
    // The via field should attribute to fn_close (minimum depth)
    let chunks = vec![
        chunk_at("fn_close", "src/lib.rs", 10, 20),
        chunk_at("fn_far", "src/lib.rs", 30, 40),
        chunk_at("mid", "src/app.rs", 1, 10),
        test_chunk_at("test_shared", "tests/test.rs", 1, 10),
    ];
    insert_chunks(&store, &chunks);

    insert_calls(&store, "src/app.rs", &[("mid", 1, &[("fn_far", 5)])]);
    insert_calls(
        &store,
        "tests/test.rs",
        &[("test_shared", 1, &[("fn_close", 3), ("mid", 5)])],
    );

    let changed = vec![
        ChangedFunction {
            name: "fn_close".to_string(),
            file: PathBuf::from("src/lib.rs"),
            line_start: 10,
        },
        ChangedFunction {
            name: "fn_far".to_string(),
            file: PathBuf::from("src/lib.rs"),
            line_start: 30,
        },
    ];

    let result = analyze_diff_impact(&store, changed, std::path::Path::new("/test")).unwrap();

    assert_eq!(
        result.summary.test_count, 1,
        "test_shared should appear once (deduplicated)"
    );
    let test = &result.all_tests[0];
    assert_eq!(test.name, "test_shared");
    assert_eq!(
        test.via, "fn_close",
        "via should attribute to fn_close (minimum depth path)"
    );
    assert_eq!(
        test.call_depth, 1,
        "call_depth should be 1 (direct call to fn_close)"
    );
}