cqs 1.22.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
//! Tests for impact.rs (P3-11: suggest_tests, P3-12: analyze_impact)

mod common;

use common::{mock_embedding, TestStore};
use cqs::parser::{CallSite, Chunk, ChunkType, FunctionCalls, Language};
use cqs::{analyze_impact, suggest_tests, ImpactResult};
use std::path::{Path, PathBuf};

/// Create a chunk at a specific file and line
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,
    }
}

/// Create a test chunk (name starts with "test_")
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,
    }
}

/// 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();
}

// ===== analyze_impact tests (P3-12) =====

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

    let chunks = vec![
        chunk_at("target_fn", "src/lib.rs", 1, 10),
        chunk_at("caller_a", "src/app.rs", 1, 15),
        chunk_at("caller_b", "src/cli.rs", 1, 20),
    ];
    insert_chunks(&store, &chunks);

    insert_calls(
        &store,
        "src/app.rs",
        &[("caller_a", 1, &[("target_fn", 5)])],
    );
    insert_calls(
        &store,
        "src/cli.rs",
        &[("caller_b", 1, &[("target_fn", 10)])],
    );

    let result = analyze_impact(
        &store,
        "target_fn",
        std::path::Path::new("/test"),
        &cqs::ImpactOptions {
            depth: 1,
            include_types: false,
        },
    )
    .unwrap();
    assert_eq!(result.function_name, "target_fn");
    assert!(
        result.callers.len() >= 2,
        "Should have at least 2 callers, got {}",
        result.callers.len()
    );
    let caller_names: Vec<&str> = result.callers.iter().map(|c| c.name.as_str()).collect();
    assert!(caller_names.contains(&"caller_a"));
    assert!(caller_names.contains(&"caller_b"));
}

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

    let chunks = vec![
        chunk_at("target_fn", "src/lib.rs", 1, 10),
        chunk_at("caller_fn", "src/app.rs", 1, 15),
        test_chunk_at("test_caller", "tests/test.rs", 1, 10),
    ];
    insert_chunks(&store, &chunks);

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

    let result = analyze_impact(
        &store,
        "target_fn",
        std::path::Path::new("/test"),
        &cqs::ImpactOptions {
            depth: 1,
            include_types: false,
        },
    )
    .unwrap();
    assert!(
        result.tests.iter().any(|t| t.name == "test_caller"),
        "test_caller should be found via BFS: test_caller -> caller_fn -> target_fn"
    );
}

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

    let chunks = vec![chunk_at("isolated_fn", "src/lib.rs", 1, 10)];
    insert_chunks(&store, &chunks);

    let result = analyze_impact(
        &store,
        "isolated_fn",
        std::path::Path::new("/test"),
        &cqs::ImpactOptions {
            depth: 1,
            include_types: false,
        },
    )
    .unwrap();
    assert_eq!(result.function_name, "isolated_fn");
    assert!(result.callers.is_empty(), "Should have no callers");
    assert!(result.tests.is_empty(), "Should have no tests");
}

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

    let chunks = vec![
        chunk_at("target_fn", "src/lib.rs", 1, 10),
        chunk_at("direct", "src/lib.rs", 20, 30),
        chunk_at("indirect", "src/app.rs", 1, 15),
    ];
    insert_chunks(&store, &chunks);

    // indirect -> direct -> target_fn
    insert_calls(
        &store,
        "src/lib.rs",
        &[("direct", 20, &[("target_fn", 25)])],
    );
    insert_calls(&store, "src/app.rs", &[("indirect", 1, &[("direct", 5)])]);

    // depth=2 should find transitive callers
    let result = analyze_impact(
        &store,
        "target_fn",
        std::path::Path::new("/test"),
        &cqs::ImpactOptions {
            depth: 2,
            include_types: false,
        },
    )
    .unwrap();
    let trans_names: Vec<&str> = result
        .transitive_callers
        .iter()
        .map(|c| c.name.as_str())
        .collect();
    assert!(
        trans_names.contains(&"direct"),
        "direct should be a transitive caller"
    );
    assert!(
        trans_names.contains(&"indirect"),
        "indirect should be a transitive caller at depth 2"
    );
}

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

    let chunks = vec![
        chunk_at("target_fn", "src/lib.rs", 1, 10),
        chunk_at("direct", "src/lib.rs", 20, 30),
        chunk_at("indirect", "src/app.rs", 1, 15),
    ];
    insert_chunks(&store, &chunks);

    insert_calls(
        &store,
        "src/lib.rs",
        &[("direct", 20, &[("target_fn", 25)])],
    );
    insert_calls(&store, "src/app.rs", &[("indirect", 1, &[("direct", 5)])]);

    // depth=1 should NOT include transitive callers
    let result = analyze_impact(
        &store,
        "target_fn",
        std::path::Path::new("/test"),
        &cqs::ImpactOptions {
            depth: 1,
            include_types: false,
        },
    )
    .unwrap();
    assert!(
        result.transitive_callers.is_empty(),
        "depth=1 should not include transitive callers"
    );
}

// ===== suggest_tests tests (P3-11) =====

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

    // target_fn has caller_fn (untested) and test_caller (a test)
    let chunks = vec![
        chunk_at("target_fn", "src/lib.rs", 1, 10),
        chunk_at("untested_caller", "src/app.rs", 1, 15),
    ];
    insert_chunks(&store, &chunks);

    insert_calls(
        &store,
        "src/app.rs",
        &[("untested_caller", 1, &[("target_fn", 5)])],
    );

    let impact = analyze_impact(
        &store,
        "target_fn",
        std::path::Path::new("/test"),
        &cqs::ImpactOptions {
            depth: 1,
            include_types: false,
        },
    )
    .unwrap();
    let suggestions = suggest_tests(&store, &impact, std::path::Path::new("/test"));

    // untested_caller has no tests reaching it, should get a suggestion
    assert!(
        suggestions
            .iter()
            .any(|s| s.for_function == "untested_caller"),
        "Should suggest test for untested_caller, got: {:?}",
        suggestions
            .iter()
            .map(|s| &s.for_function)
            .collect::<Vec<_>>()
    );
}

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

    let chunks = vec![
        chunk_at("target_fn", "src/lib.rs", 1, 10),
        chunk_at("caller_fn", "src/app.rs", 1, 15),
        test_chunk_at("test_caller", "tests/test.rs", 1, 10),
    ];
    insert_chunks(&store, &chunks);

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

    let impact = analyze_impact(
        &store,
        "target_fn",
        std::path::Path::new("/test"),
        &cqs::ImpactOptions {
            depth: 1,
            include_types: false,
        },
    )
    .unwrap();
    let suggestions = suggest_tests(&store, &impact, std::path::Path::new("/test"));

    // caller_fn is tested via test_caller — no suggestion needed
    assert!(
        !suggestions.iter().any(|s| s.for_function == "caller_fn"),
        "Should not suggest test for already-tested caller_fn"
    );
}

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

    let chunks = vec![chunk_at("lonely_fn", "src/lib.rs", 1, 10)];
    insert_chunks(&store, &chunks);

    let impact = ImpactResult {
        function_name: "lonely_fn".to_string(),
        callers: Vec::new(),
        tests: Vec::new(),
        transitive_callers: Vec::new(),
        type_impacted: Vec::new(),
        degraded: false,
    };
    let suggestions = suggest_tests(&store, &impact, std::path::Path::new("/test"));
    assert!(suggestions.is_empty(), "No callers means no suggestions");
}

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

    let chunks = vec![
        chunk_at("target_fn", "src/lib.rs", 1, 10),
        chunk_at("process_data", "src/app.rs", 1, 15),
    ];
    insert_chunks(&store, &chunks);

    insert_calls(
        &store,
        "src/app.rs",
        &[("process_data", 1, &[("target_fn", 5)])],
    );

    let impact = analyze_impact(
        &store,
        "target_fn",
        std::path::Path::new("/test"),
        &cqs::ImpactOptions {
            depth: 1,
            include_types: false,
        },
    )
    .unwrap();
    let suggestions = suggest_tests(&store, &impact, std::path::Path::new("/test"));

    if let Some(suggestion) = suggestions
        .iter()
        .find(|s| s.for_function == "process_data")
    {
        assert_eq!(
            suggestion.test_name, "test_process_data",
            "Rust test name should be test_ prefixed"
        );
    }
}