aptu-core 0.10.12

Core library for Aptu - OSS issue triage with AI assistance
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
// SPDX-License-Identifier: Apache-2.0

//! Graph queries: blast-radius BFS, ephemeral Modifies edges, and text rendering.
//!
//! The main entry point is [`blast_radius`], which performs a bounded BFS in both
//! directions from a set of modified nodes over [`Edge::Calls`], [`Edge::Implements`],
//! [`Edge::HasMethod`], and [`Edge::Tests`] edges, returning the induced subgraph.

use std::collections::{HashSet, VecDeque};

use petgraph::Direction;
use petgraph::graph::NodeIndex;
use petgraph::visit::EdgeRef as _;

use super::{Edge, GraphDb, Node};

/// Finds nodes matching the given symbol names and returns their indices. No edges are added.
///
/// A symbol matches if its `Node::name()` exactly equals one of `modified_symbols`.
#[must_use]
pub fn find_modified_nodes(graph: &mut GraphDb, modified_symbols: &[&str]) -> Vec<NodeIndex> {
    let symbol_set: HashSet<&str> = modified_symbols.iter().copied().collect();

    // Collect matching node indices (avoid borrow issues by collecting first).
    let matched: Vec<NodeIndex> = graph
        .node_indices()
        .filter(|&idx| {
            let name = graph[idx].name();
            symbol_set.contains(name)
        })
        .collect();

    matched
}

/// Computes the blast-radius subgraph from a set of `modified_nodes`.
///
/// Performs a bounded BFS in both directions (callers and callees) from each
/// node in `modified_nodes` over [`Edge::Calls`], [`Edge::Implements`],
/// [`Edge::HasMethod`], and [`Edge::Tests`] edges. [`Edge::Contains`] and
/// [`Edge::Modifies`] are excluded.
///
/// The returned graph contains at most `max_nodes` nodes (including the seed
/// nodes). Traversal stops as soon as the cap is reached. `max_depth` caps the
/// number of hop levels traversed from the seed nodes; whichever limit
/// (`max_nodes` or `max_depth`) triggers first stops the BFS.
#[must_use]
pub fn blast_radius(
    graph: &GraphDb,
    modified_nodes: &[NodeIndex],
    max_nodes: usize,
    max_depth: usize,
) -> GraphDb {
    if modified_nodes.is_empty() || max_nodes == 0 || max_depth == 0 {
        return GraphDb::new();
    }

    let relevant_edges = |e: &Edge| {
        matches!(
            e,
            Edge::Calls | Edge::Implements | Edge::HasMethod | Edge::Tests
        )
    };

    let mut visited: HashSet<NodeIndex> = HashSet::new();
    let mut queue: VecDeque<(NodeIndex, usize)> = VecDeque::new();

    for &node in modified_nodes {
        if graph.node_weight(node).is_some() && visited.insert(node) {
            queue.push_back((node, 0));
        }
    }

    while let Some((current, depth)) = queue.pop_front() {
        if visited.len() >= max_nodes {
            break;
        }

        // Walk outgoing edges (callees).
        for edge_ref in graph.edges_directed(current, Direction::Outgoing) {
            if relevant_edges(edge_ref.weight()) {
                let target = edge_ref.target();
                if depth < max_depth && visited.len() < max_nodes && visited.insert(target) {
                    queue.push_back((target, depth + 1));
                }
            }
        }

        // Walk incoming edges (callers).
        for edge_ref in graph.edges_directed(current, Direction::Incoming) {
            if relevant_edges(edge_ref.weight()) {
                let source = edge_ref.source();
                if depth < max_depth && visited.len() < max_nodes && visited.insert(source) {
                    queue.push_back((source, depth + 1));
                }
            }
        }
    }

    // Build induced subgraph from visited nodes.
    build_induced_subgraph(graph, &visited)
}

/// Builds an induced subgraph containing only the nodes in `node_set` and
/// the edges between them (excluding [`Edge::Modifies`] and [`Edge::Contains`]).
fn build_induced_subgraph(graph: &GraphDb, node_set: &HashSet<NodeIndex>) -> GraphDb {
    use std::collections::HashMap;
    let mut sub = GraphDb::new();
    let mut index_map: HashMap<NodeIndex, NodeIndex> = HashMap::new();

    // Add nodes.
    for &old_idx in node_set {
        if let Some(weight) = graph.node_weight(old_idx) {
            let new_idx = sub.add_node(weight.clone());
            index_map.insert(old_idx, new_idx);
        }
    }

    // Add edges between nodes in the subgraph.
    for edge_ref in graph.edge_references() {
        let src = edge_ref.source();
        let dst = edge_ref.target();
        let weight = edge_ref.weight();
        if matches!(weight, Edge::Modifies | Edge::Contains) {
            continue;
        }
        if let (Some(&new_src), Some(&new_dst)) = (index_map.get(&src), index_map.get(&dst)) {
            sub.add_edge(new_src, new_dst, *weight);
        }
    }

    sub
}

/// Renders a structural subgraph as compact human-readable text for prompt injection.
///
/// Each node is rendered as one line in the format:
/// ```text
/// fn foo [calls: bar, baz] [callers: qux]
/// ```
///
/// Only `Function` nodes are listed by default; `Struct`, `Enum`, `Trait`, and
/// `Impl` nodes appear without call/caller annotations. `File` and `Module`
/// nodes are omitted (they are structural, not behavioural).
#[must_use]
pub fn render_subgraph_text(subgraph: &GraphDb) -> String {
    use std::collections::{BTreeMap, BTreeSet, HashMap};

    // Build adjacency maps for calls and callers.
    let mut calls: HashMap<NodeIndex, BTreeSet<String>> = HashMap::new();
    let mut callers: HashMap<NodeIndex, BTreeSet<String>> = HashMap::new();

    for edge_ref in subgraph.edge_references() {
        if matches!(edge_ref.weight(), Edge::Calls) {
            calls
                .entry(edge_ref.source())
                .or_default()
                .insert(subgraph[edge_ref.target()].name().to_string());
            callers
                .entry(edge_ref.target())
                .or_default()
                .insert(subgraph[edge_ref.source()].name().to_string());
        }
        if matches!(edge_ref.weight(), Edge::HasMethod | Edge::Implements) {
            calls
                .entry(edge_ref.source())
                .or_default()
                .insert(subgraph[edge_ref.target()].name().to_string());
        }
    }

    // Group rendered lines by file path so the LLM sees co-located symbols
    // together, reducing the need to mentally reconstruct file layout.
    let mut by_file: BTreeMap<String, Vec<String>> = BTreeMap::new();

    for idx in subgraph.node_indices() {
        let node = &subgraph[idx];
        let prefix = match node {
            Node::Function { .. } => "fn",
            Node::Struct { .. } => "struct",
            Node::Enum { .. } => "enum",
            Node::Trait { .. } => "trait",
            Node::Impl { .. } => "impl",
            Node::File { .. } | Node::Module { .. } => continue,
        };
        let name = node.name();
        let mut parts = vec![format!("{prefix} {name}")];
        if let Some(c) = calls.get(&idx) {
            parts.push(format!(
                "[calls: {}]",
                c.iter().map(String::as_str).collect::<Vec<_>>().join(", ")
            ));
        }
        if let Some(c) = callers.get(&idx) {
            parts.push(format!(
                "[callers: {}]",
                c.iter().map(String::as_str).collect::<Vec<_>>().join(", ")
            ));
        }
        by_file
            .entry(node.path().to_string())
            .or_default()
            .push(parts.join(" "));
    }

    // Emit file headers followed by sorted symbol lines within each file.
    let mut out = String::new();
    for (path, mut lines) in by_file {
        if !out.is_empty() {
            out.push('\n');
        }
        out.push_str("// ");
        out.push_str(&path);
        out.push('\n');
        lines.sort();
        out.push_str(&lines.join("\n"));
    }
    out
}

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

    fn two_caller_graph() -> (GraphDb, NodeIndex, NodeIndex, NodeIndex) {
        let mut graph = GraphDb::new();
        let target = graph.add_node(Node::Function {
            name: "target".to_string(),
            path: "src/lib.rs".to_string(),
            visibility: "pub".to_string(),
        });
        let caller_a = graph.add_node(Node::Function {
            name: "caller_a".to_string(),
            path: "src/lib.rs".to_string(),
            visibility: "private".to_string(),
        });
        let caller_b = graph.add_node(Node::Function {
            name: "caller_b".to_string(),
            path: "src/lib.rs".to_string(),
            visibility: "private".to_string(),
        });
        graph.add_edge(caller_a, target, Edge::Calls);
        graph.add_edge(caller_b, target, Edge::Calls);
        (graph, target, caller_a, caller_b)
    }

    #[test]
    fn test_blast_radius_returns_all_direct_callers_of_modified_node() {
        // Arrange: target has two callers.
        let (graph, target, caller_a, caller_b) = two_caller_graph();

        // Act
        let sub = blast_radius(&graph, &[target], 100, 10);

        // Assert: all three nodes are in the subgraph.
        let names: Vec<&str> = sub.node_weights().map(|n| n.name()).collect();
        assert!(names.contains(&"target"), "target must be in subgraph");
        assert!(names.contains(&"caller_a"), "caller_a must be in subgraph");
        assert!(names.contains(&"caller_b"), "caller_b must be in subgraph");
    }

    #[test]
    fn test_blast_radius_bounded_by_max_nodes() {
        // Arrange: a chain of 10 nodes.
        let mut graph = GraphDb::new();
        let mut prev = graph.add_node(Node::Function {
            name: "n0".to_string(),
            path: "".to_string(),
            visibility: "pub".to_string(),
        });
        let root = prev;
        for i in 1..10usize {
            let next = graph.add_node(Node::Function {
                name: format!("n{i}"),
                path: "".to_string(),
                visibility: "pub".to_string(),
            });
            graph.add_edge(prev, next, Edge::Calls);
            prev = next;
        }

        // Act: cap at 3 nodes.
        let sub = blast_radius(&graph, &[root], 3, 10);

        // Assert: at most 3 nodes in the subgraph.
        assert!(
            sub.node_count() <= 3,
            "blast_radius must respect max_nodes cap; got {}",
            sub.node_count()
        );
    }

    #[test]
    fn test_blast_radius_empty_when_max_nodes_zero() {
        let (graph, target, _, _) = two_caller_graph();
        let sub = blast_radius(&graph, &[target], 0, 10);
        assert_eq!(sub.node_count(), 0);
    }

    #[test]
    fn test_blast_radius_depth_cap_stops_at_max_depth() {
        // Arrange: a fan-out graph: center with 5 callers, each of which has 5
        // callers-of-callers (depth-2 nodes).
        let mut graph = GraphDb::new();
        let center = graph.add_node(Node::Function {
            name: "center".to_string(),
            path: "".to_string(),
            visibility: "pub".to_string(),
        });
        let mut depth1 = Vec::new();
        for i in 0..5 {
            let caller = graph.add_node(Node::Function {
                name: format!("caller{i}"),
                path: "".to_string(),
                visibility: "pub".to_string(),
            });
            graph.add_edge(caller, center, Edge::Calls);
            depth1.push(caller);
        }
        for (i, &d1) in depth1.iter().enumerate() {
            let caller2 = graph.add_node(Node::Function {
                name: format!("caller2_{i}"),
                path: "".to_string(),
                visibility: "pub".to_string(),
            });
            graph.add_edge(caller2, d1, Edge::Calls);
        }

        // Act: cap depth at 1, so depth-2 nodes must be absent.
        let sub = blast_radius(&graph, &[center], 100, 1);

        // Assert: center and depth-1 callers present; depth-2 nodes absent.
        let names: Vec<String> = sub.node_weights().map(|n| n.name().to_string()).collect();
        assert!(
            names.contains(&"center".to_string()),
            "center must be present"
        );
        for i in 0..5 {
            assert!(
                names.contains(&format!("caller{i}")),
                "caller{i} must be present"
            );
        }
        for i in 0..5 {
            assert!(
                !names.contains(&format!("caller2_{i}")),
                "caller2_{i} must be absent at depth 2"
            );
        }
    }

    #[test]
    fn test_blast_radius_max_depth_zero_returns_empty() {
        let (graph, target, _, _) = two_caller_graph();
        let sub = blast_radius(&graph, &[target], 100, 0);
        assert_eq!(sub.node_count(), 0);
    }

    #[test]
    fn test_render_subgraph_text_contains_function_with_caller() {
        // Arrange: two-caller graph.
        let (graph, target, _caller_a, _caller_b) = two_caller_graph();
        let sub = blast_radius(&graph, &[target], 100, 10);

        // Act
        let text = render_subgraph_text(&sub);

        // Assert: rendered text contains the target function.
        assert!(text.contains("fn target"), "must render target function");
        // Must contain at least one caller annotation.
        assert!(text.contains("[callers:"), "must render callers annotation");
    }

    #[test]
    fn test_render_subgraph_text_returns_empty_string_for_empty_graph() {
        // Arrange: an empty GraphDb with no edges and no nodes.
        let graph = super::GraphDb::default();
        let sub = blast_radius(&graph, &[], 100, 10);

        // Act
        let text = render_subgraph_text(&sub);

        // Assert: empty string, no leading newline.
        assert!(text.is_empty(), "empty graph must produce empty string");
    }

    #[test]
    fn test_find_modified_nodes_matches_named_nodes() {
        // Arrange
        let mut graph = GraphDb::new();
        graph.add_node(Node::Function {
            name: "foo".to_string(),
            path: "".to_string(),
            visibility: "pub".to_string(),
        });
        graph.add_node(Node::Function {
            name: "bar".to_string(),
            path: "".to_string(),
            visibility: "pub".to_string(),
        });

        // Act
        let matched = find_modified_nodes(&mut graph, &["foo"]);

        // Assert: exactly one node matched and no edges were added.
        assert_eq!(matched.len(), 1);
        assert_eq!(graph.edge_count(), 0, "no sentinel edges should be added");
    }
}