trueno-graph 0.1.2

GPU-first embedded graph database for code analysis (call graphs, dependencies, AST traversals)
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
//! Subgraph pattern matching for anti-pattern detection
//!
//! Find specific graph patterns (e.g., code smells, anti-patterns).
//! Based on subgraph isomorphism and motif detection algorithms.
//!
//! # References
//! - Kim et al. (2023): "Efficient Subgraph Matching on Large Graphs"
//! - Cordella et al. (2004): "VF2 algorithm for subgraph isomorphism"

use crate::{CsrGraph, NodeId};
use anyhow::Result;
use std::collections::{HashMap, HashSet};

/// Pattern matching result
#[derive(Debug, Clone)]
pub struct PatternMatch {
    /// Nodes involved in the pattern (pattern node → graph node mapping)
    pub node_mapping: HashMap<u32, NodeId>,

    /// Pattern name (e.g., "God Class", "Circular Dependency")
    pub pattern_name: String,

    /// Severity: Low, Medium, High, Critical
    pub severity: Severity,
}

/// Severity level for anti-patterns
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Severity {
    /// Low severity (minor code smell)
    Low,
    /// Medium severity (should be refactored)
    Medium,
    /// High severity (significant anti-pattern)
    High,
    /// Critical severity (architectural flaw)
    Critical,
}

/// Pattern specification for matching
#[derive(Debug, Clone)]
pub struct Pattern {
    /// Pattern name
    pub name: String,

    /// Pattern edges (source, target) - node IDs are local to the pattern
    pub edges: Vec<(u32, u32)>,

    /// Minimum number of nodes in the pattern
    pub min_nodes: usize,

    /// Maximum number of nodes (None = unbounded)
    pub max_nodes: Option<usize>,

    /// Severity if found
    pub severity: Severity,
}

impl Pattern {
    /// Create a new pattern
    #[must_use]
    pub fn new(name: String, edges: Vec<(u32, u32)>, severity: Severity) -> Self {
        let num_nodes = edges
            .iter()
            .flat_map(|(s, t)| [*s, *t])
            .max()
            .map_or(0, |max| max as usize + 1);

        Self {
            name,
            edges,
            min_nodes: num_nodes,
            max_nodes: None,
            severity,
        }
    }

    /// Create a "God Class" pattern (node with many outgoing edges)
    ///
    /// Detects functions that call too many other functions (high fan-out)
    #[must_use]
    pub fn god_class(min_callees: usize) -> Self {
        Self {
            name: "God Class".to_string(),
            edges: Vec::new(), // Filled dynamically based on min_callees
            min_nodes: min_callees + 1,
            max_nodes: None,
            severity: Severity::High,
        }
    }

    /// Create a "Circular Dependency" pattern (cycle of length N)
    ///
    /// Detects function call cycles
    #[must_use]
    #[allow(clippy::cast_possible_truncation)] // Pattern length >4B nodes not realistic
    pub fn circular_dependency(cycle_length: usize) -> Self {
        let mut edges = Vec::new();
        for i in 0..cycle_length {
            let next = (i + 1) % cycle_length;
            edges.push((i as u32, next as u32));
        }

        Self {
            name: "Circular Dependency".to_string(),
            edges,
            min_nodes: cycle_length,
            max_nodes: Some(cycle_length),
            severity: Severity::Critical,
        }
    }

    /// Create a "Dead Code" pattern (node with no incoming edges)
    ///
    /// Detects uncalled functions
    #[must_use]
    pub fn dead_code() -> Self {
        Self {
            name: "Dead Code".to_string(),
            edges: Vec::new(),
            min_nodes: 1,
            max_nodes: Some(1),
            severity: Severity::Medium,
        }
    }
}

/// Find all pattern matches in the graph
///
/// # Arguments
///
/// * `graph` - The graph to search
/// * `pattern` - The pattern to find
///
/// # Returns
///
/// Vector of pattern matches
///
/// # Errors
///
/// Returns error if pattern is malformed
///
/// # Example
///
/// ```ignore
/// # use trueno_graph::{CsrGraph, NodeId};
/// # use trueno_graph::algorithms::pattern::{Pattern, find_patterns, Severity};
/// let mut graph = CsrGraph::new();
/// // Create a 3-node cycle: 0 -> 1 -> 2 -> 0
/// graph.add_edge(NodeId(0), NodeId(1), 1.0).unwrap();
/// graph.add_edge(NodeId(1), NodeId(2), 1.0).unwrap();
/// graph.add_edge(NodeId(2), NodeId(0), 1.0).unwrap();
///
/// // Find circular dependencies of length 3
/// let pattern = Pattern::circular_dependency(3);
/// let matches = find_patterns(&graph, &pattern).unwrap();
///
/// assert_eq!(matches.len(), 1);
/// ```
pub fn find_patterns(graph: &CsrGraph, pattern: &Pattern) -> Result<Vec<PatternMatch>> {
    match pattern.name.as_str() {
        "God Class" => Ok(find_god_class_patterns(graph, pattern)),
        "Circular Dependency" => Ok(find_cycle_patterns(graph, pattern)),
        "Dead Code" => Ok(find_dead_code_patterns(graph, pattern)),
        _ => find_generic_patterns(graph, pattern),
    }
}

/// Find "God Class" patterns (high fan-out nodes)
fn find_god_class_patterns(graph: &CsrGraph, pattern: &Pattern) -> Vec<PatternMatch> {
    let mut matches = Vec::new();

    for node_id in 0..graph.num_nodes() {
        #[allow(clippy::cast_possible_truncation)]
        let node = NodeId(node_id as u32);

        if let Ok(neighbors) = graph.outgoing_neighbors(node) {
            if neighbors.len() >= pattern.min_nodes - 1 {
                // High fan-out detected
                let mut node_mapping = HashMap::new();
                node_mapping.insert(0, node); // Pattern node 0 = god class

                matches.push(PatternMatch {
                    node_mapping,
                    pattern_name: pattern.name.clone(),
                    severity: pattern.severity,
                });
            }
        }
    }

    matches
}

/// Find "Circular Dependency" patterns (cycles)
fn find_cycle_patterns(graph: &CsrGraph, pattern: &Pattern) -> Vec<PatternMatch> {
    let cycle_length = pattern.min_nodes;
    let mut matches = Vec::new();
    let mut visited_cycles = HashSet::new();

    // DFS to find cycles of specific length
    for start_node in 0..graph.num_nodes() {
        #[allow(clippy::cast_possible_truncation)]
        let start = NodeId(start_node as u32);

        let cycles = find_cycles_from_node(graph, start, cycle_length);

        for cycle in cycles {
            // Normalize cycle to avoid duplicates (start from smallest node)
            let mut normalized = cycle.clone();
            normalized.sort_unstable();

            if visited_cycles.insert(normalized) {
                // Create node mapping
                let mut node_mapping = HashMap::new();
                for (pattern_id, &graph_node) in cycle.iter().enumerate() {
                    #[allow(clippy::cast_possible_truncation)]
                    node_mapping.insert(pattern_id as u32, graph_node);
                }

                matches.push(PatternMatch {
                    node_mapping,
                    pattern_name: pattern.name.clone(),
                    severity: pattern.severity,
                });
            }
        }
    }

    matches
}

/// Find "Dead Code" patterns (nodes with no incoming edges)
fn find_dead_code_patterns(graph: &CsrGraph, pattern: &Pattern) -> Vec<PatternMatch> {
    let mut matches = Vec::new();

    for node_id in 0..graph.num_nodes() {
        #[allow(clippy::cast_possible_truncation)]
        let node = NodeId(node_id as u32);

        if let Ok(incoming) = graph.incoming_neighbors(node) {
            if incoming.is_empty() {
                // No callers = dead code
                let mut node_mapping = HashMap::new();
                node_mapping.insert(0, node);

                matches.push(PatternMatch {
                    node_mapping,
                    pattern_name: pattern.name.clone(),
                    severity: pattern.severity,
                });
            }
        }
    }

    matches
}

/// Find generic patterns using simplified subgraph matching
///
/// Uses backtracking to find all subgraph isomorphisms. Suitable for small patterns
/// (typical in code smell detection). For larger patterns, consider specialized algorithms.
#[allow(clippy::unnecessary_wraps, clippy::cast_possible_truncation)]
fn find_generic_patterns(graph: &CsrGraph, pattern: &Pattern) -> Result<Vec<PatternMatch>> {
    let mut matches = Vec::new();

    // Build pattern adjacency for quick lookup
    let mut pattern_adj: HashMap<u32, HashSet<u32>> = HashMap::new();
    for (src, dst) in &pattern.edges {
        pattern_adj.entry(*src).or_default().insert(*dst);
    }

    // Try matching pattern starting from each node
    for start_node in 0..graph.num_nodes() {
        let mut mapping: HashMap<u32, NodeId> = HashMap::new();
        let mut used_nodes: HashSet<NodeId> = HashSet::new();

        if try_match_pattern(
            graph,
            &pattern_adj,
            pattern.min_nodes,
            0,
            NodeId(start_node as u32),
            &mut mapping,
            &mut used_nodes,
        ) {
            // Found a match
            matches.push(PatternMatch {
                node_mapping: mapping.clone(),
                pattern_name: pattern.name.clone(),
                severity: pattern.severity,
            });
        }
    }

    Ok(matches)
}

/// Recursive helper for pattern matching (backtracking)
#[allow(clippy::cast_possible_truncation)]
fn try_match_pattern(
    graph: &CsrGraph,
    pattern_adj: &HashMap<u32, HashSet<u32>>,
    pattern_size: usize,
    pattern_node: u32,
    graph_node: NodeId,
    mapping: &mut HashMap<u32, NodeId>,
    used_nodes: &mut HashSet<NodeId>,
) -> bool {
    // Check if node already used
    if used_nodes.contains(&graph_node) {
        return false;
    }

    // Add to mapping
    mapping.insert(pattern_node, graph_node);
    used_nodes.insert(graph_node);

    // If mapped all nodes, success
    if mapping.len() == pattern_size {
        return true;
    }

    // Check edges match for all previously mapped nodes
    for (&p_src, &g_src) in mapping.iter() {
        if let Some(p_targets) = pattern_adj.get(&p_src) {
            let g_targets: HashSet<u32> = graph
                .outgoing_neighbors(g_src)
                .unwrap_or_default()
                .iter()
                .copied()
                .collect();

            for &p_target in p_targets {
                if let Some(&g_target) = mapping.get(&p_target) {
                    // Edge should exist in graph
                    if !g_targets.contains(&g_target.0) {
                        // Backtrack
                        mapping.remove(&pattern_node);
                        used_nodes.remove(&graph_node);
                        return false;
                    }
                }
            }
        }
    }

    // Try extending mapping to unmapped pattern nodes
    for next_pattern_node in 0..pattern_size as u32 {
        if !mapping.contains_key(&next_pattern_node) {
            // Try all graph nodes
            for next_graph_node in 0..graph.num_nodes() {
                if try_match_pattern(
                    graph,
                    pattern_adj,
                    pattern_size,
                    next_pattern_node,
                    NodeId(next_graph_node as u32),
                    mapping,
                    used_nodes,
                ) {
                    return true;
                }
            }
            // Backtrack
            mapping.remove(&pattern_node);
            used_nodes.remove(&graph_node);
            return false;
        }
    }

    // Backtrack
    mapping.remove(&pattern_node);
    used_nodes.remove(&graph_node);
    false
}

/// Helper: Find cycles of specific length starting from a node
fn find_cycles_from_node(
    graph: &CsrGraph,
    start: NodeId,
    target_length: usize,
) -> Vec<Vec<NodeId>> {
    let mut cycles = Vec::new();
    let mut path = vec![start];
    let mut visited = HashSet::new();
    visited.insert(start.0);

    dfs_find_cycles(
        graph,
        start,
        start,
        &mut path,
        &mut visited,
        target_length,
        &mut cycles,
    );

    cycles
}

/// DFS helper for cycle detection
#[allow(clippy::too_many_arguments)]
fn dfs_find_cycles(
    graph: &CsrGraph,
    current: NodeId,
    start: NodeId,
    path: &mut Vec<NodeId>,
    visited: &mut HashSet<u32>,
    target_length: usize,
    cycles: &mut Vec<Vec<NodeId>>,
) {
    if path.len() == target_length {
        // Check if current node connects back to start
        if let Ok(neighbors) = graph.outgoing_neighbors(current) {
            if neighbors.contains(&start.0) {
                cycles.push(path.clone());
            }
        }
        return;
    }

    // Explore neighbors
    if let Ok(neighbors) = graph.outgoing_neighbors(current) {
        for &neighbor_id in neighbors {
            let neighbor = NodeId(neighbor_id);

            if !visited.contains(&neighbor_id)
                || (neighbor == start && path.len() == target_length - 1)
            {
                visited.insert(neighbor_id);
                path.push(neighbor);

                dfs_find_cycles(graph, neighbor, start, path, visited, target_length, cycles);

                path.pop();
                visited.remove(&neighbor_id);
            }
        }
    }
}

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

    #[test]
    fn test_god_class_pattern() {
        // Node 0 calls 5 other functions (high fan-out)
        let mut graph = CsrGraph::new();
        for i in 1..=5 {
            graph.add_edge(NodeId(0), NodeId(i), 1.0).unwrap();
        }

        let pattern = Pattern::god_class(5);
        let matches = find_patterns(&graph, &pattern).unwrap();

        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].pattern_name, "God Class");
        assert_eq!(matches[0].severity, Severity::High);
    }

    #[test]
    fn test_circular_dependency_pattern() {
        // Create 3-node cycle: 0 -> 1 -> 2 -> 0
        let mut graph = CsrGraph::new();
        graph.add_edge(NodeId(0), NodeId(1), 1.0).unwrap();
        graph.add_edge(NodeId(1), NodeId(2), 1.0).unwrap();
        graph.add_edge(NodeId(2), NodeId(0), 1.0).unwrap();

        let pattern = Pattern::circular_dependency(3);
        let matches = find_patterns(&graph, &pattern).unwrap();

        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].pattern_name, "Circular Dependency");
        assert_eq!(matches[0].severity, Severity::Critical);
    }

    #[test]
    fn test_dead_code_pattern() {
        // Node 0 -> Node 1, but Node 2 has no incoming edges
        let mut graph = CsrGraph::new();
        graph.add_edge(NodeId(0), NodeId(1), 1.0).unwrap();
        graph.add_edge(NodeId(2), NodeId(1), 1.0).unwrap(); // Node 2 exists but not called

        // Manually expand graph to include node 3 with no callers
        graph.add_edge(NodeId(3), NodeId(4), 1.0).unwrap();

        let pattern = Pattern::dead_code();
        let matches = find_patterns(&graph, &pattern).unwrap();

        // Nodes with no incoming edges (except if they're source nodes in edge list)
        assert!(!matches.is_empty());
    }

    #[test]
    fn test_pattern_new() {
        let edges = vec![(0, 1), (1, 2)];
        let pattern = Pattern::new("Test Pattern".to_string(), edges, Severity::Medium);

        assert_eq!(pattern.name, "Test Pattern");
        assert_eq!(pattern.min_nodes, 3); // Nodes 0, 1, 2
        assert_eq!(pattern.severity, Severity::Medium);
    }

    #[test]
    fn test_no_circular_dependency() {
        // Linear graph: 0 -> 1 -> 2 (no cycle)
        let mut graph = CsrGraph::new();
        graph.add_edge(NodeId(0), NodeId(1), 1.0).unwrap();
        graph.add_edge(NodeId(1), NodeId(2), 1.0).unwrap();

        let pattern = Pattern::circular_dependency(3);
        let matches = find_patterns(&graph, &pattern).unwrap();

        assert_eq!(matches.len(), 0); // No cycles found
    }

    #[test]
    fn test_severity_ordering() {
        assert!(Severity::Low < Severity::Medium);
        assert!(Severity::Medium < Severity::High);
        assert!(Severity::High < Severity::Critical);
    }

    #[test]
    fn test_generic_pattern_triangle() {
        // Create a triangle pattern: 0 -> 1, 1 -> 2, 2 -> 0
        let pattern = Pattern::new(
            "Triangle".to_string(),
            vec![(0, 1), (1, 2), (2, 0)],
            Severity::Low,
        );

        // Graph with triangle
        let mut graph = CsrGraph::new();
        graph.add_edge(NodeId(0), NodeId(1), 1.0).unwrap();
        graph.add_edge(NodeId(1), NodeId(2), 1.0).unwrap();
        graph.add_edge(NodeId(2), NodeId(0), 1.0).unwrap();

        let matches = find_patterns(&graph, &pattern).unwrap();
        assert!(!matches.is_empty());
    }

    #[test]
    fn test_generic_pattern_line() {
        // Create a line pattern: 0 -> 1 -> 2
        let pattern = Pattern::new("Line".to_string(), vec![(0, 1), (1, 2)], Severity::Low);

        // Graph with matching line
        let mut graph = CsrGraph::new();
        graph.add_edge(NodeId(0), NodeId(1), 1.0).unwrap();
        graph.add_edge(NodeId(1), NodeId(2), 1.0).unwrap();

        let matches = find_patterns(&graph, &pattern).unwrap();
        assert!(!matches.is_empty());
    }

    #[test]
    fn test_generic_pattern_no_match() {
        // Create a pattern that won't match
        let pattern = Pattern::new("V-shape".to_string(), vec![(0, 1), (0, 2)], Severity::Low);

        // Graph with different structure (linear)
        let mut graph = CsrGraph::new();
        graph.add_edge(NodeId(0), NodeId(1), 1.0).unwrap();

        let matches = find_patterns(&graph, &pattern).unwrap();
        assert_eq!(matches.len(), 0);
    }

    #[test]
    fn test_pattern_with_max_nodes() {
        let mut pattern = Pattern::circular_dependency(3);
        pattern.max_nodes = Some(3);

        let mut graph = CsrGraph::new();
        graph.add_edge(NodeId(0), NodeId(1), 1.0).unwrap();
        graph.add_edge(NodeId(1), NodeId(2), 1.0).unwrap();
        graph.add_edge(NodeId(2), NodeId(0), 1.0).unwrap();

        let matches = find_patterns(&graph, &pattern).unwrap();
        assert_eq!(matches.len(), 1);
    }

    #[test]
    fn test_multiple_god_classes() {
        // Two nodes with high fan-out
        let mut graph = CsrGraph::new();
        // Node 0 calls 5 functions
        for i in 1..=5 {
            graph.add_edge(NodeId(0), NodeId(i), 1.0).unwrap();
        }
        // Node 6 calls 5 functions
        for i in 7..=11 {
            graph.add_edge(NodeId(6), NodeId(i), 1.0).unwrap();
        }

        let pattern = Pattern::god_class(5);
        let matches = find_patterns(&graph, &pattern).unwrap();

        assert_eq!(matches.len(), 2);
    }

    #[test]
    fn test_multiple_cycles() {
        // Create two separate 3-node cycles
        let mut graph = CsrGraph::new();
        // Cycle 1: 0 -> 1 -> 2 -> 0
        graph.add_edge(NodeId(0), NodeId(1), 1.0).unwrap();
        graph.add_edge(NodeId(1), NodeId(2), 1.0).unwrap();
        graph.add_edge(NodeId(2), NodeId(0), 1.0).unwrap();
        // Cycle 2: 3 -> 4 -> 5 -> 3
        graph.add_edge(NodeId(3), NodeId(4), 1.0).unwrap();
        graph.add_edge(NodeId(4), NodeId(5), 1.0).unwrap();
        graph.add_edge(NodeId(5), NodeId(3), 1.0).unwrap();

        let pattern = Pattern::circular_dependency(3);
        let matches = find_patterns(&graph, &pattern).unwrap();

        assert_eq!(matches.len(), 2);
    }

    #[test]
    fn test_dead_code_with_callers() {
        // Node 0 has callers, node 1 has no callers
        let mut graph = CsrGraph::new();
        graph.add_edge(NodeId(2), NodeId(0), 1.0).unwrap();
        graph.add_edge(NodeId(1), NodeId(3), 1.0).unwrap();

        let pattern = Pattern::dead_code();
        let matches = find_patterns(&graph, &pattern).unwrap();

        // Should find nodes 1 and 2 (no incoming edges)
        assert!(matches.len() >= 2);
    }
}