semantex-core 1.0.3

Core library for semantex semantic code search (indexing, embeddings, search)
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
//! Cross-file symbol resolution for the code graph.
//!
//! After all files have been chunked and stored, this module runs a global
//! resolution pass that links call targets, type references, and type hierarchy
//! entries across file boundaries.

use std::collections::HashMap;
use std::path::Path;

use anyhow::Result;

use super::storage::{ChunkStore, GraphStats};

/// A single symbol definition: (chunk_id, file_path, symbol_kind).
type SymbolDef = (u64, String, String);

/// Global post-indexing pass: resolve cross-file call targets,
/// type references, and type hierarchy entries.
///
/// Called once after all files have been chunked and inserted.
pub fn resolve_cross_file_graph(store: &ChunkStore) -> Result<GraphStats> {
    let mut stats = GraphStats::default();

    // Phase 1: Build symbol definition index
    let symbol_defs = store.get_all_symbol_defs()?;
    let name_to_defs = build_name_index(&symbol_defs);
    stats.symbol_defs_count = symbol_defs.len();

    // Phase 2: Resolve call_graph.callee_chunk_id
    stats.calls_resolved = resolve_call_targets(store, &name_to_defs)?;

    // Phase 3: Resolve type_refs.defining_chunk
    stats.types_resolved = resolve_type_targets(store, &name_to_defs)?;

    // Phase 4: Resolve type_hierarchy chunk IDs
    stats.hierarchy_resolved = resolve_hierarchy_targets(store, &name_to_defs)?;

    Ok(stats)
}

/// Build a lookup table mapping symbol_name -> Vec<(chunk_id, file_path, symbol_kind)>.
fn build_name_index(defs: &[(u64, String, String, String)]) -> HashMap<String, Vec<SymbolDef>> {
    let mut index: HashMap<String, Vec<SymbolDef>> = HashMap::with_capacity(defs.len());
    for (chunk_id, name, kind, file_path) in defs {
        index
            .entry(name.clone())
            .or_default()
            .push((*chunk_id, file_path.clone(), kind.clone()));
    }
    index
}

/// Resolve unresolved call graph edges by matching callee_name to symbol defs.
fn resolve_call_targets(
    store: &ChunkStore,
    name_index: &HashMap<String, Vec<SymbolDef>>,
) -> Result<usize> {
    let unresolved = store.get_unresolved_call_edges()?;
    let mut resolved_count = 0;

    for (rowid, callee_name, caller_file_path) in &unresolved {
        let target = lookup_symbol(store, name_index, callee_name, caller_file_path);

        if let Some(chunk_id) = target {
            store.update_callee_chunk_id(*rowid, chunk_id)?;
            resolved_count += 1;
        }
    }

    Ok(resolved_count)
}

/// Resolve unresolved type references by matching type_name to symbol defs
/// filtered to type-like kinds. Uses the same disambiguation heuristic as call
/// resolution when multiple definitions match.
fn resolve_type_targets(
    store: &ChunkStore,
    name_index: &HashMap<String, Vec<SymbolDef>>,
) -> Result<usize> {
    let unresolved = store.get_unresolved_type_refs()?;
    let mut resolved_count = 0;

    for (rowid, type_name, ref_file_path) in &unresolved {
        let chunk_id = lookup_type(store, name_index, type_name, ref_file_path);

        if let Some(id) = chunk_id {
            store.update_type_ref_defining_chunk(*rowid, id)?;
            resolved_count += 1;
        }
    }

    Ok(resolved_count)
}

/// Look up a type name in the index, with disambiguation and module prefix stripping.
fn lookup_type(
    store: &ChunkStore,
    name_index: &HashMap<String, Vec<SymbolDef>>,
    type_name: &str,
    ref_file: &str,
) -> Option<u64> {
    // Try exact name first
    if let Some(id) = try_resolve_type(store, name_index, type_name, ref_file) {
        return Some(id);
    }

    // Try stripping module prefix (e.g. "collections::HashMap" -> "HashMap")
    let stripped = strip_module_prefix(type_name);
    #[allow(clippy::collapsible_if)]
    if stripped != type_name {
        if let Some(id) = try_resolve_type(store, name_index, stripped, ref_file) {
            return Some(id);
        }
    }

    None
}

/// Attempt to resolve a type name to a single chunk ID, filtering to type-defining kinds
/// and using disambiguation for ambiguous matches.
fn try_resolve_type(
    store: &ChunkStore,
    name_index: &HashMap<String, Vec<SymbolDef>>,
    name: &str,
    ref_file: &str,
) -> Option<u64> {
    let defs = name_index.get(name)?;
    let type_defs: Vec<SymbolDef> = defs
        .iter()
        .filter(|(_, _, kind)| is_type_kind(kind))
        .cloned()
        .collect();

    match type_defs.len() {
        0 => None,
        1 => Some(type_defs[0].0),
        _ => Some(disambiguate_symbol(store, &type_defs, ref_file)),
    }
}

/// Resolve type hierarchy entries by linking child_name and parent_name
/// to their defining chunks.
fn resolve_hierarchy_targets(
    store: &ChunkStore,
    name_index: &HashMap<String, Vec<SymbolDef>>,
) -> Result<usize> {
    let unresolved = store.get_unresolved_hierarchy()?;
    let mut resolved_count = 0;

    for (rowid, child_name, parent_name) in &unresolved {
        let child_chunk = find_type_chunk(name_index, child_name);
        let parent_chunk = find_type_chunk(name_index, parent_name);

        // Only update if we resolved at least one side
        if child_chunk.is_some() || parent_chunk.is_some() {
            store.update_hierarchy_chunks(*rowid, child_chunk, parent_chunk)?;
            if child_chunk.is_some() && parent_chunk.is_some() {
                resolved_count += 1;
            }
        }
    }

    Ok(resolved_count)
}

/// Look up a symbol name in the index, with disambiguation for ambiguous matches.
///
/// Tries the full name first, then strips module prefixes (e.g. `utils.helper` -> `helper`).
fn lookup_symbol(
    store: &ChunkStore,
    name_index: &HashMap<String, Vec<SymbolDef>>,
    name: &str,
    caller_file: &str,
) -> Option<u64> {
    // Try exact name first
    if let Some(chunk_id) = try_resolve(store, name_index, name, caller_file) {
        return Some(chunk_id);
    }

    // Try stripping module/namespace prefix (e.g. "module.func" -> "func", "Ns::func" -> "func")
    let stripped = strip_module_prefix(name);
    #[allow(clippy::collapsible_if)]
    if stripped != name {
        if let Some(chunk_id) = try_resolve(store, name_index, stripped, caller_file) {
            return Some(chunk_id);
        }
    }

    None
}

/// Attempt to resolve a symbol name to a single chunk ID.
fn try_resolve(
    store: &ChunkStore,
    name_index: &HashMap<String, Vec<SymbolDef>>,
    name: &str,
    caller_file: &str,
) -> Option<u64> {
    let defs = name_index.get(name)?;
    match defs.len() {
        0 => None,
        1 => Some(defs[0].0),
        _ => Some(disambiguate_symbol(store, defs, caller_file)),
    }
}

/// Disambiguate among multiple candidate definitions for a symbol.
///
/// Priority order:
/// 1. Import-linked: caller's file imports candidate's file
/// 2. Same directory
/// 3. Same parent directory
/// 4. First match (fallback)
fn disambiguate_symbol(store: &ChunkStore, candidates: &[SymbolDef], caller_file: &str) -> u64 {
    let caller_path = Path::new(caller_file);
    let caller_dir = caller_path.parent();
    let caller_parent_dir = caller_dir.and_then(Path::parent);

    // Check import edges for the caller's file
    let imported_paths = store
        .get_module_edges_for_file(caller_file)
        .unwrap_or_else(|e| {
            tracing::warn!("Failed to get module edges for {caller_file}: {e}");
            Vec::new()
        });

    // Priority 1: Candidate whose file is imported by the caller
    for (chunk_id, file_path, _) in candidates {
        if imported_paths.iter().any(|imp| imp == file_path) {
            return *chunk_id;
        }
    }

    // Priority 2: Same directory
    if let Some(cdir) = caller_dir {
        for (chunk_id, file_path, _) in candidates {
            if Path::new(file_path).parent() == Some(cdir) {
                return *chunk_id;
            }
        }
    }

    // Priority 3: Same parent directory
    if let Some(pdir) = caller_parent_dir {
        for (chunk_id, file_path, _) in candidates {
            let candidate_parent = Path::new(file_path).parent().and_then(Path::parent);
            if candidate_parent == Some(pdir) {
                return *chunk_id;
            }
        }
    }

    // Fallback: first candidate
    candidates[0].0
}

/// Find the chunk ID for a type name, preferring type-defining kinds.
fn find_type_chunk(name_index: &HashMap<String, Vec<SymbolDef>>, name: &str) -> Option<u64> {
    let defs = name_index.get(name)?;
    // Prefer type-defining kinds
    for (chunk_id, _, kind) in defs {
        if is_type_kind(kind) {
            return Some(*chunk_id);
        }
    }
    // Fall back to any definition
    defs.first().map(|(chunk_id, _, _)| *chunk_id)
}

/// Whether a symbol_kind represents a type definition.
fn is_type_kind(kind: &str) -> bool {
    matches!(
        kind,
        "class" | "struct" | "enum" | "trait" | "interface" | "type_alias"
    )
}

/// Strip module/namespace prefix from a qualified name.
///
/// Handles both dot-separated (`module.func`) and double-colon (`Ns::func`) forms.
fn strip_module_prefix(name: &str) -> &str {
    // Try :: separator first (Rust, C++)
    if let Some(pos) = name.rfind("::") {
        return &name[pos + 2..];
    }
    // Try dot separator (Python, JS, Java)
    if let Some(pos) = name.rfind('.') {
        return &name[pos + 1..];
    }
    name
}

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

    #[test]
    fn test_strip_module_prefix() {
        assert_eq!(strip_module_prefix("utils.helper"), "helper");
        assert_eq!(strip_module_prefix("std::collections::HashMap"), "HashMap");
        assert_eq!(strip_module_prefix("simple_name"), "simple_name");
        assert_eq!(strip_module_prefix("a.b.c.deep"), "deep");
    }

    #[test]
    fn test_is_type_kind() {
        assert!(is_type_kind("class"));
        assert!(is_type_kind("struct"));
        assert!(is_type_kind("enum"));
        assert!(is_type_kind("trait"));
        assert!(is_type_kind("interface"));
        assert!(is_type_kind("type_alias"));
        assert!(!is_type_kind("function"));
        assert!(!is_type_kind("method"));
        assert!(!is_type_kind("variable"));
    }

    #[test]
    fn test_build_name_index() {
        let defs = vec![
            (
                1,
                "foo".to_string(),
                "function".to_string(),
                "a.rs".to_string(),
            ),
            (
                2,
                "bar".to_string(),
                "class".to_string(),
                "b.rs".to_string(),
            ),
            (
                3,
                "foo".to_string(),
                "method".to_string(),
                "c.rs".to_string(),
            ),
        ];
        let index = build_name_index(&defs);
        assert_eq!(index.len(), 2);
        assert_eq!(index["foo"].len(), 2);
        assert_eq!(index["bar"].len(), 1);
    }

    #[test]
    fn test_find_type_chunk_prefers_type_kinds() {
        let defs = vec![
            (
                1,
                "Foo".to_string(),
                "function".to_string(),
                "a.rs".to_string(),
            ),
            (
                2,
                "Foo".to_string(),
                "class".to_string(),
                "b.rs".to_string(),
            ),
            (
                3,
                "Bar".to_string(),
                "struct".to_string(),
                "c.rs".to_string(),
            ),
        ];
        let index = build_name_index(&defs);

        // Foo: should prefer chunk 2 (class) over chunk 1 (function)
        assert_eq!(find_type_chunk(&index, "Foo"), Some(2));
        // Bar: only one def, a struct
        assert_eq!(find_type_chunk(&index, "Bar"), Some(3));
        // Missing name
        assert_eq!(find_type_chunk(&index, "Baz"), None);
    }

    #[test]
    fn test_try_resolve_type_filters_to_type_kinds() {
        // try_resolve_type should only consider type-defining kinds
        let defs = vec![
            (
                1,
                "Config".to_string(),
                "function".to_string(),
                "a.rs".to_string(),
            ),
            (
                2,
                "Config".to_string(),
                "struct".to_string(),
                "b.rs".to_string(),
            ),
        ];
        let index = build_name_index(&defs);

        // Without a store we can't call try_resolve_type directly, but we can
        // verify the filtering logic via find_type_chunk (same filter).
        assert_eq!(find_type_chunk(&index, "Config"), Some(2));
    }

    #[test]
    fn test_strip_module_prefix_for_types() {
        // Verifies that the same prefix stripping used for calls also works for
        // qualified type names (fix for issue #7).
        assert_eq!(strip_module_prefix("collections::HashMap"), "HashMap");
        assert_eq!(strip_module_prefix("com.example.UserConfig"), "UserConfig");
        assert_eq!(strip_module_prefix("HashMap"), "HashMap");
    }
}