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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
//! Graph module - Symbol and reference queries using sqlitegraph.
//!
//! This module provides access to code graph for querying symbols,
//! finding references, and running graph algorithms.

use crate::error::Result;
use crate::storage::UnifiedGraphStore;
use crate::types::{Cycle, CycleMember, Reference, ReferenceKind, Symbol, SymbolId};
use std::collections::{HashMap, HashSet, VecDeque};
use std::sync::Arc;

/// Impacted symbol from k-hop impact analysis.
#[derive(Debug, Clone)]
pub struct ImpactedSymbol {
    pub symbol_id: i64,
    pub name: String,
    pub kind: String,
    pub file_path: String,
    pub hop_distance: u32,
    pub edge_type: String,
}

/// Graph module for symbol and reference queries.
///
/// # Examples
///
/// See crate-level documentation for usage examples.
#[derive(Clone)]
pub struct GraphModule {
    store: Arc<UnifiedGraphStore>,
}

impl GraphModule {
    pub(crate) fn new(store: Arc<UnifiedGraphStore>) -> Self {
        Self { store }
    }

    /// Get the underlying store for advanced operations
    pub fn store(&self) -> &UnifiedGraphStore {
        &self.store
    }

    /// Finds symbols by name (exact match).
    ///
    /// # Arguments
    ///
    /// * `name` - The symbol name to search for
    ///
    /// # Returns
    ///
    /// A vector of matching symbols, or empty if the graph DB does not exist.
    pub async fn find_symbol(&self, name: &str) -> Result<Vec<Symbol>> {
        use magellan::CodeGraph;

        let db_path = &self.store.db_path;
        if !db_path.exists() {
            return Ok(Vec::new());
        }

        let graph = CodeGraph::open(db_path).map_err(|e| {
            crate::error::ForgeError::DatabaseError(format!("Failed to open magellan graph: {}", e))
        })?;

        let results = graph.search_symbols_by_name(name).map_err(|e| {
            crate::error::ForgeError::DatabaseError(format!("Symbol search failed: {}", e))
        })?;

        Ok(results
            .into_iter()
            .map(|r| {
                let file_path = std::path::PathBuf::from(&r.file_path);
                let line_number = std::fs::read(&file_path)
                    .map(|content| byte_offset_to_line_number(&content, r.byte_start))
                    .unwrap_or(0);
                Symbol {
                    id: SymbolId(r.entity_id),
                    name: Arc::from(r.name.clone()),
                    fully_qualified_name: Arc::from(r.name.clone()),
                    kind: parse_symbol_kind_str(&r.kind),
                    language: map_magellan_language(&file_path),
                    location: crate::types::Location {
                        file_path,
                        byte_start: r.byte_start as u32,
                        byte_end: r.byte_end as u32,
                        line_number,
                    },
                    parent_id: None,
                    metadata: serde_json::Value::Null,
                }
            })
            .collect())
    }

    /// Finds a symbol by its stable ID.
    ///
    /// # Arguments
    ///
    /// * `id` - The symbol identifier
    ///
    /// # Returns
    ///
    /// The symbol with the given ID
    pub async fn find_symbol_by_id(&self, id: SymbolId) -> Result<Symbol> {
        self.store.get_symbol(id).await
    }

    /// Finds all callers of a symbol by name.
    ///
    /// # Arguments
    ///
    /// * `name` - The symbol name
    ///
    /// # Returns
    ///
    /// A vector of call-references to this symbol, or empty if the graph DB does not exist.
    pub async fn callers_of(&self, name: &str) -> Result<Vec<Reference>> {
        use magellan::CodeGraph;

        let db_path = &self.store.db_path;
        if !db_path.exists() {
            return Ok(Vec::new());
        }

        let mut graph = CodeGraph::open(db_path).map_err(|e| {
            crate::error::ForgeError::DatabaseError(format!("Failed to open magellan graph: {}", e))
        })?;

        let symbols = graph.search_symbols_by_name(name).map_err(|e| {
            crate::error::ForgeError::DatabaseError(format!("Symbol search failed: {}", e))
        })?;

        let mut callers = Vec::new();
        for sym in &symbols {
            if let Ok(call_facts) = graph.callers_of_symbol(&sym.file_path, name) {
                for fact in call_facts {
                    callers.push(Reference {
                        from: SymbolId(0),
                        to: SymbolId(0),
                        from_name: Some(fact.caller.clone()),
                        to_name: Some(fact.callee.clone()),
                        kind: ReferenceKind::Call,
                        location: crate::types::Location {
                            file_path: fact.file_path.clone(),
                            byte_start: fact.byte_start as u32,
                            byte_end: fact.byte_end as u32,
                            line_number: fact.start_line,
                        },
                    });
                }
            }
        }

        Ok(callers)
    }

    /// Finds all cross-file references to a symbol by FQN.
    ///
    /// # Arguments
    ///
    /// * `name` - The symbol fully-qualified name
    ///
    /// # Returns
    ///
    /// A vector of all cross-file references, or empty if the graph DB does not exist.
    pub async fn references(&self, name: &str) -> Result<Vec<Reference>> {
        use magellan::{cross_file_references_to, CodeGraph};

        let db_path = &self.store.db_path;
        if !db_path.exists() {
            return Ok(Vec::new());
        }

        let graph = CodeGraph::open(db_path).map_err(|e| {
            crate::error::ForgeError::DatabaseError(format!("Failed to open magellan graph: {}", e))
        })?;

        let cross_refs = cross_file_references_to(&graph, name).map_err(|e| {
            crate::error::ForgeError::DatabaseError(format!("Reference query failed: {}", e))
        })?;

        Ok(cross_refs
            .into_iter()
            .map(|r| Reference {
                from: SymbolId(0),
                to: SymbolId(0),
                from_name: Some(r.from_symbol_id.clone()),
                to_name: Some(r.to_symbol_id.clone()),
                kind: ReferenceKind::TypeReference,
                location: crate::types::Location {
                    file_path: std::path::PathBuf::from(&r.file_path),
                    byte_start: r.byte_start as u32,
                    byte_end: r.byte_end as u32,
                    line_number: r.line_number,
                },
            })
            .collect())
    }

    /// Finds all symbols reachable from a given symbol.
    ///
    /// Uses BFS traversal to find all symbols that can be reached
    /// from the starting symbol through the call graph.
    ///
    /// # Arguments
    ///
    /// * `id` - The starting symbol ID
    ///
    /// # Returns
    ///
    /// A vector of reachable symbol IDs
    pub async fn reachable_from(&self, id: SymbolId) -> Result<Vec<SymbolId>> {
        // Build adjacency list for BFS
        let mut adjacency: HashMap<SymbolId, Vec<SymbolId>> = HashMap::new();

        // Query references to build the graph
        let refs = self.store.query_references(id).await?;
        for reference in &refs {
            adjacency
                .entry(reference.from)
                .or_default()
                .push(reference.to);
        }

        // BFS from the starting node
        let mut visited = HashSet::new();
        let mut queue = VecDeque::new();
        let mut reachable = Vec::new();

        queue.push_back(id);
        visited.insert(id);

        while let Some(current) = queue.pop_front() {
            if let Some(neighbors) = adjacency.get(&current) {
                for &neighbor in neighbors {
                    if visited.insert(neighbor) {
                        queue.push_back(neighbor);
                        reachable.push(neighbor);
                    }
                }
            }
        }

        Ok(reachable)
    }

    /// Detects cycles in the call graph using SCC condensation.
    ///
    /// Supernodes with more than one member represent strongly connected
    /// components (mutual recursion / call cycles).
    ///
    /// # Returns
    ///
    /// A vector of detected cycles, or empty if the graph DB does not exist.
    pub async fn cycles(&self) -> Result<Vec<Cycle>> {
        use magellan::CodeGraph;

        let db_path = &self.store.db_path;
        if !db_path.exists() {
            return Ok(Vec::new());
        }

        let graph = CodeGraph::open(db_path).map_err(|e| {
            crate::error::ForgeError::DatabaseError(format!("Failed to open magellan graph: {}", e))
        })?;

        let report = graph.detect_cycles().map_err(|e| {
            crate::error::ForgeError::DatabaseError(format!("Cycle detection failed: {}", e))
        })?;

        Ok(report
            .cycles
            .into_iter()
            .map(|c| Cycle {
                members: c
                    .members
                    .into_iter()
                    .map(|si| CycleMember {
                        symbol_id: si.symbol_id,
                        fqn: si.fqn,
                        file_path: si.file_path,
                        kind: si.kind,
                    })
                    .collect(),
            })
            .collect())
    }

    /// Returns the number of symbols in the graph.
    pub async fn symbol_count(&self) -> Result<usize> {
        self.store.symbol_count().await
    }

    /// Analyze the impact of changing a symbol.
    ///
    /// Performs k-hop traversal to find all symbols that would be affected
    /// by modifying the given symbol.
    ///
    /// # Arguments
    ///
    /// * `symbol_name` - The name of the symbol to analyze
    /// * `max_hops` - Maximum traversal depth (default: 2)
    ///
    /// # Returns
    ///
    /// A vector of impacted symbols with their hop distance from the target,
    /// or empty if the graph DB does not exist.
    pub async fn impact_analysis(
        &self,
        symbol_name: &str,
        max_hops: Option<u32>,
    ) -> Result<Vec<ImpactedSymbol>> {
        use magellan::CodeGraph;

        let db_path = &self.store.db_path;
        if !db_path.exists() {
            return Ok(Vec::new());
        }

        let mut graph = CodeGraph::open(db_path).map_err(|e| {
            crate::error::ForgeError::DatabaseError(format!("Failed to open magellan graph: {}", e))
        })?;

        let symbols = graph.search_symbols_by_name(symbol_name).map_err(|e| {
            crate::error::ForgeError::DatabaseError(format!("Symbol search failed: {}", e))
        })?;

        let start_entity_id = match symbols.first() {
            Some(s) => s.entity_id,
            None => return Ok(Vec::new()),
        };

        let hops = max_hops.unwrap_or(2);

        let mut impacted = Vec::new();
        let mut visited = HashSet::new();
        visited.insert(start_entity_id);
        let mut current_level = vec![start_entity_id];

        for hop in 1..=hops {
            let mut next_level = Vec::new();
            for &entity_id in &current_level {
                if let Ok(sym) = graph.symbol_by_entity_id(entity_id) {
                    let file = &sym.file_path;
                    if let Ok(callers) =
                        graph.callers_of_symbol(file, sym.fqn.as_deref().unwrap_or(&sym.kind))
                    {
                        for fact in callers {
                            let caller_entity = graph
                                .symbol_id_by_name(
                                    fact.file_path.to_str().unwrap_or(""),
                                    &fact.caller,
                                )
                                .ok()
                                .flatten();
                            if let Some(cid) = caller_entity {
                                if visited.insert(cid) {
                                    next_level.push(cid);
                                    if let Ok(info) = graph.symbol_by_entity_id(cid) {
                                        impacted.push(ImpactedSymbol {
                                            symbol_id: cid,
                                            name: info.fqn.clone().unwrap_or_default(),
                                            kind: info.kind,
                                            file_path: info.file_path,
                                            hop_distance: hop,
                                            edge_type: "call".to_string(),
                                        });
                                    }
                                }
                            }
                        }
                    }
                }
            }
            current_level = next_level;
            if current_level.is_empty() {
                break;
            }
        }

        Ok(impacted)
    }

    /// Indexes the codebase using magellan.
    ///
    /// This runs the magellan indexer to extract symbols and references
    /// from the codebase and populate the graph database.
    ///
    /// For Native V3 backend, also indexes cross-file references using
    /// sqlitegraph directly (a capability SQLite doesn't support).
    ///
    /// # Returns
    ///
    /// Ok(()) on success, or an error if indexing fails.
    pub async fn index(&self) -> Result<()> {
        use magellan::CodeGraph;
        use std::path::Path;

        let codebase_path = &self.store.codebase_path;
        let db_path = &self.store.db_path;

        let mut graph = CodeGraph::open(db_path).map_err(|e| {
            crate::error::ForgeError::DatabaseError(format!("Failed to open magellan graph: {}", e))
        })?;

        let count = graph
            .scan_directory(Path::new(codebase_path), None)
            .map_err(|e| {
                crate::error::ForgeError::DatabaseError(format!("Failed to scan directory: {}", e))
            })?;

        tracing::info!("Indexed {} symbols from {}", count, codebase_path.display());

        let _ = graph.rebuild_fts5();

        Self::index_references_recursive(&mut graph, codebase_path, codebase_path).await
    }

    async fn index_references_recursive(
        graph: &mut magellan::CodeGraph,
        codebase_path: &std::path::Path,
        current_dir: &std::path::Path,
    ) -> Result<()> {
        use tokio::fs;

        let mut entries = fs::read_dir(current_dir).await.map_err(|e| {
            crate::error::ForgeError::DatabaseError(format!("Failed to read dir: {}", e))
        })?;

        while let Some(entry) = entries.next_entry().await.map_err(|e| {
            crate::error::ForgeError::DatabaseError(format!("Failed to read entry: {}", e))
        })? {
            let path = entry.path();
            if path.is_dir() {
                // Recurse into subdirectories
                Box::pin(Self::index_references_recursive(
                    graph,
                    codebase_path,
                    &path,
                ))
                .await?;
            } else if path.is_file() && path.extension().map(|e| e == "rs").unwrap_or(false) {
                // Get relative path from codebase root
                let relative_path = path
                    .strip_prefix(codebase_path)
                    .unwrap_or(&path)
                    .to_string_lossy();

                if let Ok(source) = fs::read_to_string(&path).await {
                    // Index references using relative path
                    let _ = graph.index_references(&relative_path, source.as_bytes());
                    // Index calls using relative path
                    let _ = graph.index_calls(&relative_path, source.as_bytes());
                }
            }
        }

        Ok(())
    }
}

fn parse_symbol_kind_str(kind: &str) -> crate::types::SymbolKind {
    use crate::types::SymbolKind;
    match kind {
        "fn" | "function" => SymbolKind::Function,
        "method" => SymbolKind::Method,
        "struct" | "class" => SymbolKind::Struct,
        "trait" | "interface" => SymbolKind::Trait,
        "enum" => SymbolKind::Enum,
        "module" | "namespace" => SymbolKind::Module,
        "type_alias" | "type" => SymbolKind::TypeAlias,
        _ => SymbolKind::Function,
    }
}

fn map_magellan_language(file_path: &std::path::Path) -> crate::types::Language {
    use crate::types::Language;

    match file_path.extension().and_then(|e| e.to_str()) {
        Some("rs") => Language::Rust,
        Some("py") => Language::Python,
        Some("c") => Language::C,
        Some("cpp") | Some("cc") | Some("cxx") => Language::Cpp,
        Some("java") => Language::Java,
        Some("js") => Language::JavaScript,
        Some("ts") => Language::TypeScript,
        Some("go") => Language::Go,
        _ => Language::Unknown("other".to_string()),
    }
}

/// Returns the 1-indexed line number for a byte offset within file content.
///
/// Counts `\n` bytes before `byte_offset` and adds 1. If `byte_offset` exceeds
/// the content length, returns the last line number.
pub(crate) fn byte_offset_to_line_number(content: &[u8], byte_offset: usize) -> usize {
    let clamped = byte_offset.min(content.len());
    content[..clamped].iter().filter(|&&b| b == b'\n').count() + 1
}

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

    async fn test_forge(dir: &std::path::Path) -> crate::Forge {
        crate::ForgeBuilder::new()
            .path(dir)
            .db_path(dir.join("test-graph.db"))
            .build()
            .await
            .unwrap()
    }

    #[tokio::test]
    async fn test_graph_module_creation() {
        let temp_dir = tempfile::tempdir().unwrap();
        let forge = test_forge(temp_dir.path()).await;
        let module = forge.graph();
        assert_eq!(
            module.store().db_path,
            temp_dir.path().join("test-graph.db")
        );
    }

    #[tokio::test]
    async fn test_find_symbol_empty() {
        let temp_dir = tempfile::tempdir().unwrap();
        let forge = test_forge(temp_dir.path()).await;
        let module = forge.graph();
        let symbols = module.find_symbol("nonexistent").await.unwrap();
        assert_eq!(symbols.len(), 0);
    }

    #[tokio::test]
    async fn test_find_symbol_by_id_not_found() {
        let temp_dir = tempfile::tempdir().unwrap();
        let forge = test_forge(temp_dir.path()).await;
        let module = forge.graph();
        let result = module.find_symbol_by_id(SymbolId(999)).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_callers_of_empty() {
        let temp_dir = tempfile::tempdir().unwrap();
        let forge = test_forge(temp_dir.path()).await;
        let module = forge.graph();
        let callers = module.callers_of("nonexistent").await.unwrap();
        assert_eq!(callers.len(), 0);
    }

    #[test]
    fn test_byte_offset_to_line_number_first_line() {
        let content = b"fn foo() {}\nfn bar() {}\n";
        assert_eq!(byte_offset_to_line_number(content, 0), 1);
        assert_eq!(byte_offset_to_line_number(content, 5), 1);
    }

    #[test]
    fn test_byte_offset_to_line_number_second_line() {
        let content = b"fn foo() {}\nfn bar() {}\n";
        // byte 12 is start of "fn bar" (after the \n at byte 11)
        assert_eq!(byte_offset_to_line_number(content, 12), 2);
    }

    #[test]
    fn test_byte_offset_to_line_number_third_line() {
        let content = b"line1\nline2\nline3\n";
        assert_eq!(byte_offset_to_line_number(content, 12), 3);
    }

    #[test]
    fn test_byte_offset_to_line_number_clamps_to_end() {
        // Content without trailing newline: "abc\ndef" — 1 newline, so last line is 2
        let content = b"abc\ndef";
        assert_eq!(byte_offset_to_line_number(content, 9999), 2);
    }

    #[test]
    fn test_byte_offset_to_line_number_empty_content() {
        assert_eq!(byte_offset_to_line_number(b"", 0), 1);
    }

    #[tokio::test]
    async fn test_find_symbol_after_index() {
        let temp_dir = tempfile::tempdir().unwrap();
        let src_dir = temp_dir.path().join("src");
        tokio::fs::create_dir_all(&src_dir).await.unwrap();
        tokio::fs::write(
            src_dir.join("lib.rs"),
            "fn hello() {}\nfn world() -> i32 { 42 }\n",
        )
        .await
        .unwrap();

        let forge = test_forge(temp_dir.path()).await;
        forge.graph().index().await.unwrap();

        let symbols = forge.graph().find_symbol("hello").await.unwrap();
        assert!(!symbols.is_empty());
        assert_eq!(symbols[0].name.as_ref(), "hello");
    }

    #[tokio::test]
    async fn test_callers_of_after_index() {
        let temp_dir = tempfile::tempdir().unwrap();
        let src_dir = temp_dir.path().join("src");
        tokio::fs::create_dir_all(&src_dir).await.unwrap();
        tokio::fs::write(
            src_dir.join("lib.rs"),
            "fn helper() -> i32 { 1 }\nfn caller() -> i32 { helper() }\n",
        )
        .await
        .unwrap();

        let forge = test_forge(temp_dir.path()).await;
        forge.graph().index().await.unwrap();

        let callers = forge.graph().callers_of("helper").await.unwrap();
        assert!(!callers.is_empty(), "should find caller calling helper");
    }

    #[tokio::test]
    async fn test_cycles_detect_mutual_recursion() {
        let temp_dir = tempfile::tempdir().unwrap();
        let src_dir = temp_dir.path().join("src");
        tokio::fs::create_dir_all(&src_dir).await.unwrap();
        tokio::fs::write(src_dir.join("lib.rs"), "fn a() { b() }\nfn b() { a() }\n")
            .await
            .unwrap();

        let forge = test_forge(temp_dir.path()).await;
        forge.graph().index().await.unwrap();

        let cycles = forge.graph().cycles().await.unwrap();
        assert!(
            !cycles.is_empty(),
            "should detect mutual recursion between a and b"
        );
        let cycle = &cycles[0];
        assert!(cycle.members.len() >= 2);
        assert!(cycle.members.iter().any(|m| m.fqn.as_deref() == Some("a")));
        assert!(cycle.members.iter().any(|m| m.fqn.as_deref() == Some("b")));
    }

    #[tokio::test]
    async fn test_impact_analysis_after_index() {
        let temp_dir = tempfile::tempdir().unwrap();
        let src_dir = temp_dir.path().join("src");
        tokio::fs::create_dir_all(&src_dir).await.unwrap();
        tokio::fs::write(
            src_dir.join("lib.rs"),
            "fn base() -> i32 { 1 }\nfn mid() -> i32 { base() }\nfn top() -> i32 { mid() }\n",
        )
        .await
        .unwrap();

        let forge = test_forge(temp_dir.path()).await;
        forge.graph().index().await.unwrap();

        let impacted = forge
            .graph()
            .impact_analysis("base", Some(2))
            .await
            .unwrap();
        assert!(
            !impacted.is_empty(),
            "base should have mid and/or top as impacted"
        );
        let has_correct_hop = impacted.iter().any(|s| s.hop_distance == 1);
        assert!(has_correct_hop, "at least one symbol should be at hop 1");
    }
}