forgekit-core 0.5.0

Deterministic code intelligence SDK - Core library
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
//! Search module - Semantic code search via llmgrep
//!
//! This module provides semantic code search by delegating to `llmgrep::forge`
//! convenience functions. When llmgrep is disabled, falls back to regex-based
//! file scanning.

use crate::error::{ForgeError, Result as ForgeResult};
use crate::storage::UnifiedGraphStore;
use crate::types::{Language, Location, Symbol, SymbolId, SymbolKind};
use std::path::PathBuf;
use std::sync::Arc;

/// Search module for semantic code queries.
pub struct SearchModule {
    store: Arc<UnifiedGraphStore>,
}

impl SearchModule {
    /// Create a new SearchModule.
    pub fn new(store: Arc<UnifiedGraphStore>) -> Self {
        Self { store }
    }

    /// Indexes the codebase for search.
    ///
    /// llmgrep reads magellan's DB directly, so this is a no-op.
    /// The graph module's `index()` populates the shared DB.
    pub async fn index(&self) -> ForgeResult<()> {
        Ok(())
    }

    /// Pattern-based search using regex.
    ///
    /// With llmgrep: delegates to `llmgrep::forge::search_symbols_regex`.
    /// Without: scans source files recursively with regex.
    pub async fn pattern_search(&self, pattern: &str) -> ForgeResult<Vec<Symbol>> {
        let db_path = self.store.db_path.clone();
        if db_path.exists() {
            if let Ok(results) = self.search_via_llmgrep(pattern, true).await {
                return Ok(results);
            }
        }

        self.pattern_search_via_files(pattern).await
    }

    /// Pattern-based search (alias for `pattern_search`).
    pub async fn pattern(&self, pattern: &str) -> ForgeResult<Vec<Symbol>> {
        self.pattern_search(pattern).await
    }

    /// Semantic search using natural language.
    ///
    /// With llmgrep: delegates to `llmgrep::forge::search_symbols`.
    /// Without: splits query into keywords and scans files.
    pub async fn semantic_search(&self, query: &str) -> ForgeResult<Vec<Symbol>> {
        if query.trim().is_empty() {
            return Ok(Vec::new());
        }

        let db_path = self.store.db_path.clone();
        if db_path.exists() {
            if let Ok(results) = self.search_via_llmgrep(query, false).await {
                return Ok(results);
            }
        }

        self.semantic_search_via_files(query).await
    }

    /// Semantic search (alias for `semantic_search`).
    pub async fn semantic(&self, query: &str) -> ForgeResult<Vec<Symbol>> {
        self.semantic_search(query).await
    }

    /// Find a specific symbol by name.
    pub async fn symbol_by_name(&self, name: &str) -> ForgeResult<Option<Symbol>> {
        let symbols = self.pattern_search(name).await?;
        Ok(symbols.into_iter().find(|s| s.name == Arc::from(name)))
    }

    /// Find all symbols of a specific kind.
    pub async fn symbols_by_kind(&self, kind: SymbolKind) -> ForgeResult<Vec<Symbol>> {
        let all_symbols = self
            .store
            .get_all_symbols()
            .await
            .map_err(|e| ForgeError::DatabaseError(format!("Kind search failed: {}", e)))?;

        Ok(all_symbols.into_iter().filter(|s| s.kind == kind).collect())
    }

    /// Find all references to a symbol.
    pub async fn references(&self, symbol_name: &str, limit: usize) -> ForgeResult<Vec<Symbol>> {
        let db_path = self.store.db_path.clone();
        if !db_path.exists() {
            return Ok(Vec::new());
        }
        llmgrep::forge::search_references(symbol_name, &db_path, limit)
            .map(|refs| {
                refs.into_iter()
                    .map(|r| Symbol {
                        id: SymbolId(0),
                        name: Arc::from(r.referenced_symbol.clone()),
                        fully_qualified_name: Arc::from(r.referenced_symbol),
                        kind: SymbolKind::Function,
                        language: Language::Unknown("unknown".to_string()),
                        location: Location {
                            file_path: PathBuf::from(&r.span.file_path),
                            byte_start: r.span.byte_start as u32,
                            byte_end: r.span.byte_end as u32,
                            line_number: r.span.start_line as usize,
                        },
                        parent_id: None,
                        metadata: serde_json::Value::Null,
                    })
                    .collect()
            })
            .map_err(|e| ForgeError::DatabaseError(format!("Reference search failed: {}", e)))
    }

    /// Find all calls involving a symbol.
    pub async fn calls(&self, symbol_name: &str, limit: usize) -> ForgeResult<Vec<Symbol>> {
        let db_path = self.store.db_path.clone();
        if !db_path.exists() {
            return Ok(Vec::new());
        }
        llmgrep::forge::search_calls(symbol_name, &db_path, limit)
            .map(|calls| {
                calls
                    .into_iter()
                    .map(|c| Symbol {
                        id: SymbolId(0),
                        name: Arc::from(c.caller.clone()),
                        fully_qualified_name: Arc::from(c.caller.clone()),
                        kind: SymbolKind::Function,
                        language: Language::Unknown("unknown".to_string()),
                        location: Location {
                            file_path: PathBuf::from(&c.span.file_path),
                            byte_start: c.span.byte_start as u32,
                            byte_end: c.span.byte_end as u32,
                            line_number: c.span.start_line as usize,
                        },
                        parent_id: None,
                        metadata: serde_json::Value::Null,
                    })
                    .collect()
            })
            .map_err(|e| ForgeError::DatabaseError(format!("Call search failed: {}", e)))
    }

    /// Lookup a symbol by fully-qualified name.
    pub async fn lookup(&self, fqn: &str) -> ForgeResult<Option<Symbol>> {
        let db_path = self.store.db_path.clone();
        if !db_path.exists() {
            return Ok(None);
        }
        llmgrep::forge::lookup_symbol(fqn, &db_path)
            .map(|m| Some(llmgrep_match_to_symbol(m)))
            .map_err(|e| ForgeError::DatabaseError(format!("Lookup failed: {}", e)))
    }

    // -- llmgrep-backed search --

    async fn search_via_llmgrep(&self, query: &str, use_regex: bool) -> ForgeResult<Vec<Symbol>> {
        let db_path = self.store.db_path.clone();

        let result = if use_regex {
            llmgrep::forge::search_symbols_regex(query, &db_path, 50)
        } else {
            llmgrep::forge::search_symbols(query, &db_path, 50)
        };

        result
            .map(|matches| matches.into_iter().map(llmgrep_match_to_symbol).collect())
            .map_err(|e| ForgeError::DatabaseError(format!("llmgrep search failed: {}", e)))
    }

    // -- File-based fallback search --

    async fn pattern_search_via_files(&self, pattern: &str) -> ForgeResult<Vec<Symbol>> {
        use regex::Regex;

        let regex = Regex::new(pattern)
            .map_err(|e| ForgeError::DatabaseError(format!("Invalid regex pattern: {}", e)))?;

        let mut results = Vec::new();
        let mut files = Vec::new();
        collect_source_files(&self.store.codebase_path, &mut files).await;

        for path in files {
            if let Ok(content) = tokio::fs::read_to_string(&path).await {
                for (line_num, line) in content.lines().enumerate() {
                    if regex.is_match(line) {
                        let symbol_name = extract_symbol_from_line(line);
                        let relative_path = path
                            .strip_prefix(&self.store.codebase_path)
                            .unwrap_or(&path);
                        results.push(Symbol {
                            id: SymbolId(0),
                            name: Arc::from(symbol_name.clone()),
                            fully_qualified_name: Arc::from(symbol_name),
                            kind: SymbolKind::Function,
                            language: Language::Rust,
                            location: Location {
                                file_path: relative_path.to_path_buf(),
                                byte_start: 0,
                                byte_end: line.len() as u32,
                                line_number: line_num + 1,
                            },
                            parent_id: None,
                            metadata: serde_json::Value::Null,
                        });
                    }
                }
            }
        }

        Ok(results)
    }

    async fn semantic_search_via_files(&self, query: &str) -> ForgeResult<Vec<Symbol>> {
        let keywords: Vec<&str> = query
            .split_whitespace()
            .filter(|w| w.len() >= 3)
            .map(|w| w.trim_matches(|c: char| !c.is_alphanumeric()))
            .filter(|w| !w.is_empty())
            .collect();

        if keywords.is_empty() {
            return Ok(Vec::new());
        }

        let mut results = Vec::new();
        let mut files = Vec::new();
        collect_source_files(&self.store.codebase_path, &mut files).await;

        for path in files {
            let Ok(content) = tokio::fs::read_to_string(&path).await else {
                continue;
            };
            for (line_num, line) in content.lines().enumerate() {
                let name = extract_symbol_from_line(line);
                if name.is_empty() || name == "fn" {
                    continue;
                }
                let name_lower = name.to_lowercase();
                let matches_keyword = keywords.iter().any(|kw| {
                    let kw_lower = kw.to_lowercase();
                    name_lower.contains(&kw_lower) || kw_lower.contains(&name_lower)
                });
                if matches_keyword {
                    let relative_path = path
                        .strip_prefix(&self.store.codebase_path)
                        .unwrap_or(&path);
                    results.push(Symbol {
                        id: SymbolId(0),
                        name: Arc::from(name.clone()),
                        fully_qualified_name: Arc::from(name.clone()),
                        kind: if line.contains("struct ") {
                            SymbolKind::Struct
                        } else {
                            SymbolKind::Function
                        },
                        language: Language::Rust,
                        location: Location {
                            file_path: relative_path.to_path_buf(),
                            byte_start: 0,
                            byte_end: line.len() as u32,
                            line_number: line_num + 1,
                        },
                        parent_id: None,
                        metadata: serde_json::Value::Null,
                    });
                }
            }
        }

        let mut seen = std::collections::HashSet::new();
        results.retain(|s| seen.insert(s.name.clone()));

        Ok(results)
    }
}

async fn collect_source_files(dir: &std::path::Path, files: &mut Vec<PathBuf>) {
    let Ok(mut entries) = tokio::fs::read_dir(dir).await else {
        return;
    };
    while let Ok(Some(entry)) = entries.next_entry().await {
        let path = entry.path();
        if path.is_dir() {
            if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
                if matches!(
                    name,
                    "target" | ".git" | ".forge" | ".magellan" | "node_modules"
                ) {
                    continue;
                }
            }
            Box::pin(collect_source_files(&path, files)).await;
        } else if path.is_file()
            && path
                .extension()
                .map(|e| {
                    matches!(
                        e.to_str(),
                        Some("rs" | "py" | "ts" | "js" | "go" | "java" | "c" | "cpp")
                    )
                })
                .unwrap_or(false)
        {
            files.push(path);
        }
    }
}

fn llmgrep_match_to_symbol(m: llmgrep::output::SymbolMatch) -> Symbol {
    let kind = map_llmgrep_kind(&m.kind);
    let language = m
        .language
        .as_deref()
        .map(map_llmgrep_language)
        .unwrap_or(Language::Unknown("unknown".to_string()));
    let fqn: Arc<str> = Arc::from(m.fqn.clone().unwrap_or_else(|| m.name.clone()));

    Symbol {
        id: SymbolId(0),
        name: Arc::from(m.name),
        fully_qualified_name: fqn,
        kind,
        language,
        location: Location {
            file_path: PathBuf::from(&m.span.file_path),
            byte_start: m.span.byte_start as u32,
            byte_end: m.span.byte_end as u32,
            line_number: m.span.start_line as usize,
        },
        parent_id: None,
        metadata: serde_json::Value::Null,
    }
}

fn map_llmgrep_kind(kind: &str) -> SymbolKind {
    match kind {
        "function_item" | "function" => SymbolKind::Function,
        "method_item" | "method" | "impl_item" => SymbolKind::Method,
        "struct_item" | "struct" | "class" => SymbolKind::Struct,
        "trait_item" | "trait" | "interface" => SymbolKind::Trait,
        "enum_item" | "enum" => SymbolKind::Enum,
        "mod_item" | "module" | "namespace" => SymbolKind::Module,
        "type_item" | "type_alias" => SymbolKind::TypeAlias,
        "const_item" | "constant" => SymbolKind::Constant,
        "field" | "property" => SymbolKind::Field,
        _ => SymbolKind::Function,
    }
}

fn map_llmgrep_language(lang: &str) -> Language {
    match lang {
        "rust" => Language::Rust,
        "python" => Language::Python,
        "c" => Language::C,
        "cpp" | "c++" => Language::Cpp,
        "java" => Language::Java,
        "javascript" | "js" => Language::JavaScript,
        "typescript" | "ts" => Language::TypeScript,
        "go" => Language::Go,
        _ => Language::Unknown(lang.to_string()),
    }
}

fn extract_symbol_from_line(line: &str) -> String {
    let line = line.trim();

    if let Some(fn_pos) = line.find("fn ") {
        let after_fn = &line[fn_pos + 3..];
        if let Some(end_pos) = after_fn.find(|c: char| c.is_whitespace() || c == '(') {
            return after_fn[..end_pos].trim().to_string();
        }
    }

    line.split_whitespace().next().unwrap_or("").to_string()
}

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

    #[tokio::test]
    async fn test_search_module_creation() {
        let temp_dir = tempfile::tempdir().unwrap();
        let store = Arc::new(
            UnifiedGraphStore::open(temp_dir.path(), BackendKind::SQLite)
                .await
                .unwrap(),
        );
        let _search = SearchModule::new(Arc::clone(&store));
    }

    #[tokio::test]
    async fn test_pattern_search_empty() {
        let temp_dir = tempfile::tempdir().unwrap();
        let store = Arc::new(
            UnifiedGraphStore::open(temp_dir.path(), BackendKind::SQLite)
                .await
                .unwrap(),
        );
        let search = SearchModule::new(store);

        let results = search.pattern_search("nonexistent").await.unwrap();
        assert_eq!(results.len(), 0);
    }

    #[tokio::test]
    async fn test_symbol_by_name_not_found() {
        let temp_dir = tempfile::tempdir().unwrap();
        let store = Arc::new(
            UnifiedGraphStore::open(temp_dir.path(), BackendKind::SQLite)
                .await
                .unwrap(),
        );
        let search = SearchModule::new(store);

        let result = search.symbol_by_name("nonexistent").await.unwrap();
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn test_symbols_by_kind() {
        let temp_dir = tempfile::tempdir().unwrap();
        let store = Arc::new(
            UnifiedGraphStore::open(temp_dir.path(), BackendKind::SQLite)
                .await
                .unwrap(),
        );
        let search = SearchModule::new(store);

        let functions = search.symbols_by_kind(SymbolKind::Function).await.unwrap();
        assert!(functions.is_empty());
    }

    #[test]
    fn test_extract_symbol_from_line() {
        assert_eq!(
            extract_symbol_from_line("pub fn add(a: i32) -> i32 {"),
            "add"
        );
        assert_eq!(extract_symbol_from_line("fn hello() {"), "hello");
    }
}