codemem-engine 0.18.0

Domain logic engine for Codemem: indexing, hooks, watching, scoring, recall, consolidation
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
//! Diff-aware review pipeline: parse unified diffs, map changed lines to symbols,
//! compute blast radius via multi-hop graph traversal.

use crate::CodememEngine;
use codemem_core::{CodememError, GraphBackend, MemoryNode, RelationshipType};
use std::collections::{HashMap, HashSet};

// ── Types ────────────────────────────────────────────────────────────────

/// A parsed diff hunk: file path + changed line ranges.
#[derive(Debug, Clone)]
pub struct DiffHunk {
    pub file_path: String,
    pub added_lines: Vec<u32>,
    pub removed_lines: Vec<u32>,
}

/// Mapping from a unified diff to affected symbol IDs in the graph.
#[derive(Debug, Clone, Default)]
pub struct DiffSymbolMapping {
    /// sym:IDs whose definition range overlaps a changed line.
    pub changed_symbols: Vec<String>,
    /// sym:IDs whose body contains changes (parent of a changed symbol).
    pub containing_symbols: Vec<String>,
    /// file:IDs of changed files.
    pub changed_files: Vec<String>,
}

/// Information about a symbol for the blast radius report.
#[derive(Debug, Clone, serde::Serialize)]
pub struct SymbolInfo {
    pub id: String,
    pub label: String,
    pub kind: String,
    pub file_path: Option<String>,
    pub line_start: Option<u32>,
    pub pagerank: f64,
}

/// A potentially missing change detected by pattern analysis.
#[derive(Debug, Clone, serde::Serialize)]
pub struct MissingChange {
    pub symbol: String,
    pub reason: String,
}

/// A file historically coupled with changed files but absent from the diff.
#[derive(Debug, Clone, serde::Serialize)]
pub struct MissingCoChange {
    pub file_path: String,
    pub coupled_with: Vec<String>,
    pub strength: f64,
}

/// Full blast radius report for a diff.
#[derive(Debug, Clone, serde::Serialize)]
pub struct BlastRadiusReport {
    pub changed_symbols: Vec<SymbolInfo>,
    pub direct_dependents: Vec<SymbolInfo>,
    pub transitive_dependents: Vec<SymbolInfo>,
    pub affected_files: Vec<String>,
    pub affected_modules: Vec<String>,
    pub risk_score: f64,
    pub missing_changes: Vec<MissingChange>,
    pub missing_co_changes: Vec<MissingCoChange>,
    pub relevant_memories: Vec<MemorySnippet>,
}

/// Lightweight memory reference for the report (avoids serializing full MemoryNode).
#[derive(Debug, Clone, serde::Serialize)]
pub struct MemorySnippet {
    pub id: String,
    pub content: String,
    pub memory_type: String,
    pub importance: f64,
}

impl From<&MemoryNode> for MemorySnippet {
    fn from(m: &MemoryNode) -> Self {
        Self {
            id: m.id.clone(),
            content: m.content.clone(),
            memory_type: m.memory_type.to_string(),
            importance: m.importance,
        }
    }
}

// ── Diff Parsing ─────────────────────────────────────────────────────────

/// Parse a unified diff into hunks with file paths and changed line numbers.
pub fn parse_diff(diff: &str) -> Vec<DiffHunk> {
    let mut hunks = Vec::new();
    let mut current_file: Option<String> = None;
    let mut added_lines: Vec<u32> = Vec::new();
    let mut removed_lines: Vec<u32> = Vec::new();
    let mut new_line: u32 = 0;
    let mut old_line: u32 = 0;

    for line in diff.lines() {
        if line.starts_with("+++ b/") {
            // Flush previous file
            if let Some(ref file) = current_file {
                if !added_lines.is_empty() || !removed_lines.is_empty() {
                    hunks.push(DiffHunk {
                        file_path: file.clone(),
                        added_lines: std::mem::take(&mut added_lines),
                        removed_lines: std::mem::take(&mut removed_lines),
                    });
                }
            }
            current_file = line.strip_prefix("+++ b/").map(|s| s.to_string());
        } else if line.starts_with("@@ ") {
            // Parse hunk header: @@ -old_start,old_count +new_start,new_count @@
            if let Some((new_start, old_start)) = parse_hunk_header(line) {
                new_line = new_start;
                old_line = old_start;
            }
        } else if current_file.is_some() {
            if line.starts_with('+') && !line.starts_with("+++") {
                added_lines.push(new_line);
                new_line += 1;
            } else if line.starts_with('-') && !line.starts_with("---") {
                removed_lines.push(old_line);
                old_line += 1;
            } else {
                // Context line
                new_line += 1;
                old_line += 1;
            }
        }
    }

    // Flush last file
    if let Some(file) = current_file {
        if !added_lines.is_empty() || !removed_lines.is_empty() {
            hunks.push(DiffHunk {
                file_path: file,
                added_lines,
                removed_lines,
            });
        }
    }

    hunks
}

/// Parse a @@ hunk header, returning (new_start, old_start).
fn parse_hunk_header(line: &str) -> Option<(u32, u32)> {
    // Format: @@ -old_start[,old_count] +new_start[,new_count] @@
    let parts: Vec<&str> = line.split_whitespace().collect();
    if parts.len() < 3 {
        return None;
    }
    let old_part = parts[1].strip_prefix('-')?;
    let new_part = parts[2].strip_prefix('+')?;

    let old_start: u32 = old_part.split(',').next()?.parse().ok()?;
    let new_start: u32 = new_part.split(',').next()?.parse().ok()?;
    Some((new_start, old_start))
}

// ── Diff to Symbols ──────────────────────────────────────────────────────

impl CodememEngine {
    /// Map a unified diff to affected symbol IDs using the graph's line range data.
    pub fn diff_to_symbols(&self, diff: &str) -> Result<DiffSymbolMapping, CodememError> {
        let hunks = parse_diff(diff);
        let graph = self.lock_graph()?;
        let all_nodes = graph.get_all_nodes();

        let mut mapping = DiffSymbolMapping::default();
        let mut seen_symbols: HashSet<String> = HashSet::new();
        let mut seen_files: HashSet<String> = HashSet::new();

        // Build file→symbols index to avoid O(nodes × hunks) scan
        let mut file_symbols: HashMap<&str, Vec<&codemem_core::GraphNode>> = HashMap::new();
        for node in &all_nodes {
            if !node.id.starts_with("sym:") {
                continue;
            }
            if let Some(fp) = node.payload.get("file_path").and_then(|v| v.as_str()) {
                file_symbols.entry(fp).or_default().push(node);
            }
        }

        for hunk in &hunks {
            let file_id = format!("file:{}", hunk.file_path);
            if seen_files.insert(file_id.clone()) {
                mapping.changed_files.push(file_id);
            }

            let changed_lines: HashSet<u32> = hunk
                .added_lines
                .iter()
                .chain(hunk.removed_lines.iter())
                .copied()
                .collect();

            // Only check symbols in this file (indexed lookup)
            if let Some(nodes) = file_symbols.get(hunk.file_path.as_str()) {
                for node in nodes {
                    let line_start = node
                        .payload
                        .get("line_start")
                        .and_then(|v| v.as_u64())
                        .unwrap_or(0) as u32;
                    let line_end = node
                        .payload
                        .get("line_end")
                        .and_then(|v| v.as_u64())
                        .unwrap_or(line_start as u64) as u32;

                    let overlaps = changed_lines
                        .iter()
                        .any(|&l| l >= line_start && l <= line_end);
                    if overlaps && seen_symbols.insert(node.id.clone()) {
                        mapping.changed_symbols.push(node.id.clone());
                    }
                }
            }
        }

        // Find containing symbols (parents of changed symbols via CONTAINS edges)
        let changed_set: HashSet<&str> =
            mapping.changed_symbols.iter().map(|s| s.as_str()).collect();
        for node in &all_nodes {
            if !node.id.starts_with("sym:") || changed_set.contains(node.id.as_str()) {
                continue;
            }
            // Check if this symbol contains any changed symbol
            let edges = graph.get_edges(&node.id).unwrap_or_default();
            let contains_changed = edges.iter().any(|e| {
                e.relationship == RelationshipType::Contains && changed_set.contains(e.dst.as_str())
            });
            if contains_changed && seen_symbols.insert(node.id.clone()) {
                mapping.containing_symbols.push(node.id.clone());
            }
        }

        Ok(mapping)
    }

    /// Compute the blast radius for a diff: changed symbols, dependents, risk score,
    /// relevant memories, and missing change detection.
    pub fn blast_radius(
        &self,
        diff: &str,
        depth: usize,
    ) -> Result<BlastRadiusReport, CodememError> {
        let mapping = self.diff_to_symbols(diff)?;
        let graph = self.lock_graph()?;

        let mut changed_infos = Vec::new();
        let mut direct_deps = Vec::new();
        let mut transitive_deps = Vec::new();
        let mut affected_files: HashSet<String> = HashSet::new();
        let mut affected_modules: HashSet<String> = HashSet::new();
        let mut seen: HashSet<String> = HashSet::new();
        let mut risk_score: f64 = 0.0;

        // Collect changed symbol info + their PageRank for risk scoring
        for sym_id in &mapping.changed_symbols {
            if let Some(info) = node_to_symbol_info(&**graph, sym_id) {
                risk_score += info.pagerank;
                if let Some(ref fp) = info.file_path {
                    affected_files.insert(fp.clone());
                }
                seen.insert(sym_id.clone());
                changed_infos.push(info);
            }
        }
        for sym_id in &mapping.containing_symbols {
            if let Some(info) = node_to_symbol_info(&**graph, sym_id) {
                if let Some(ref fp) = info.file_path {
                    affected_files.insert(fp.clone());
                }
                seen.insert(sym_id.clone());
                changed_infos.push(info);
            }
        }

        // BFS from changed symbols to find dependents
        let all_changed: Vec<&str> = mapping
            .changed_symbols
            .iter()
            .chain(mapping.containing_symbols.iter())
            .map(|s| s.as_str())
            .collect();

        for &start_id in &all_changed {
            // Get direct dependents (1-hop incoming edges: who CALLS/IMPORTS this symbol?)
            let edges = graph.get_edges(start_id).unwrap_or_default();
            for edge in &edges {
                // Incoming edges: other symbols that depend on this one
                let dependent_id = if edge.dst == start_id {
                    &edge.src
                } else {
                    continue; // outgoing edge, skip
                };
                if !dependent_id.starts_with("sym:") || !seen.insert(dependent_id.clone()) {
                    continue;
                }
                if matches!(
                    edge.relationship,
                    RelationshipType::Calls
                        | RelationshipType::Imports
                        | RelationshipType::Inherits
                        | RelationshipType::Implements
                        | RelationshipType::Overrides
                ) {
                    if let Some(info) = node_to_symbol_info(&**graph, dependent_id) {
                        if let Some(ref fp) = info.file_path {
                            affected_files.insert(fp.clone());
                        }
                        direct_deps.push(info);
                    }
                }
            }
        }

        // Transitive dependents (2+ hops) via iterative incoming-edge traversal.
        // BFS follows outgoing edges (wrong direction for "who depends on me?").
        // Instead, walk incoming edges layer by layer.
        if depth > 1 {
            let mut frontier: Vec<String> = direct_deps.iter().map(|d| d.id.clone()).collect();
            for _ in 1..depth {
                let mut next_frontier = Vec::new();
                for node_id in &frontier {
                    let edges = graph.get_edges(node_id).unwrap_or_default();
                    for edge in &edges {
                        // Only follow incoming dependency edges
                        if edge.dst != *node_id {
                            continue;
                        }
                        if !matches!(
                            edge.relationship,
                            RelationshipType::Calls
                                | RelationshipType::Imports
                                | RelationshipType::Inherits
                                | RelationshipType::Implements
                                | RelationshipType::Overrides
                        ) {
                            continue;
                        }
                        let dep_id = &edge.src;
                        if !dep_id.starts_with("sym:") || !seen.insert(dep_id.clone()) {
                            continue;
                        }
                        if let Some(info) = node_to_symbol_info(&**graph, dep_id) {
                            if let Some(ref fp) = info.file_path {
                                affected_files.insert(fp.clone());
                            }
                            if info.kind == "Module" {
                                affected_modules.insert(info.id.clone());
                            }
                            next_frontier.push(dep_id.clone());
                            transitive_deps.push(info);
                        }
                    }
                }
                if next_frontier.is_empty() {
                    break;
                }
                frontier = next_frontier;
            }
        }

        // Detect affected modules from all symbols
        for info in changed_infos.iter().chain(direct_deps.iter()) {
            if info.kind == "Module" {
                affected_modules.insert(info.id.clone());
            }
        }

        // Risk score: Σ(pagerank) + log(transitive_count + 1)
        // Additive so that diffs touching zero-pagerank symbols (common when
        // centrality hasn't been computed or symbols have no edges) still get
        // a nonzero risk score from their dependent count.
        let transitive_count = direct_deps.len() + transitive_deps.len();
        risk_score += (transitive_count as f64 + 1.0).ln();

        // Drop graph lock before accessing storage
        drop(graph);

        // Find relevant memories connected to changed symbols
        let mut relevant_memories = Vec::new();
        for sym_id in mapping
            .changed_symbols
            .iter()
            .chain(mapping.containing_symbols.iter())
            .take(20)
        {
            if let Ok(results) = self.get_node_memories(sym_id, 1, None) {
                for r in &results {
                    relevant_memories.push(MemorySnippet::from(&r.memory));
                }
            }
        }
        // Dedup memories by ID
        let mut seen_mem_ids: HashSet<String> = HashSet::new();
        relevant_memories.retain(|m| seen_mem_ids.insert(m.id.clone()));

        // Missing change detection: find symbols with similar caller patterns
        let graph = self.lock_graph()?;
        let missing_changes = detect_missing_changes(&**graph, &mapping.changed_symbols, &seen);

        // Missing co-change detection: files that historically change together
        // with the diff's changed files but are absent from the diff.
        let missing_co_changes = detect_missing_co_changes(&**graph, &mapping.changed_files);

        let affected_files: Vec<String> = affected_files.into_iter().collect();
        let affected_modules: Vec<String> = affected_modules.into_iter().collect();

        Ok(BlastRadiusReport {
            changed_symbols: changed_infos,
            direct_dependents: direct_deps,
            transitive_dependents: transitive_deps,
            affected_files,
            affected_modules,
            risk_score,
            missing_changes,
            missing_co_changes,
            relevant_memories,
        })
    }
}

// ── Helpers ──────────────────────────────────────────────────────────────

fn node_to_symbol_info(graph: &dyn GraphBackend, node_id: &str) -> Option<SymbolInfo> {
    let node = graph.get_node(node_id).ok()??;
    Some(SymbolInfo {
        id: node.id.clone(),
        label: node.label.clone(),
        kind: node.kind.to_string(),
        file_path: node
            .payload
            .get("file_path")
            .and_then(|v| v.as_str())
            .map(String::from),
        line_start: node
            .payload
            .get("line_start")
            .and_then(|v| v.as_u64())
            .map(|v| v as u32),
        pagerank: graph.get_pagerank(&node.id),
    })
}

/// Detect potentially missing changes: symbols that share callers with changed symbols
/// but aren't in the diff.
fn detect_missing_changes(
    graph: &dyn GraphBackend,
    changed_symbols: &[String],
    already_in_diff: &HashSet<String>,
) -> Vec<MissingChange> {
    let mut missing = Vec::new();

    // For each changed symbol, find its callers. Then find what else those callers call.
    // If a sibling is called by the same callers but not in the diff, flag it.
    let mut caller_sets: HashMap<String, HashSet<String>> = HashMap::new();

    for sym_id in changed_symbols {
        let edges = graph.get_edges(sym_id).unwrap_or_default();
        let callers: HashSet<String> = edges
            .iter()
            .filter(|e| e.dst == *sym_id && e.relationship == RelationshipType::Calls)
            .map(|e| e.src.clone())
            .collect();
        if !callers.is_empty() {
            caller_sets.insert(sym_id.clone(), callers);
        }
    }

    // Find siblings: other symbols called by the same callers
    let mut sibling_counts: HashMap<String, usize> = HashMap::new();
    for callers in caller_sets.values() {
        for caller_id in callers {
            let edges = graph.get_edges(caller_id).unwrap_or_default();
            for edge in &edges {
                if edge.src == *caller_id
                    && edge.relationship == RelationshipType::Calls
                    && edge.dst.starts_with("sym:")
                    && !already_in_diff.contains(&edge.dst)
                {
                    *sibling_counts.entry(edge.dst.clone()).or_insert(0) += 1;
                }
            }
        }
    }

    // Flag siblings that share multiple callers with changed symbols
    let threshold = (changed_symbols.len() / 2).max(2);
    for (sibling, count) in &sibling_counts {
        if *count >= threshold {
            missing.push(MissingChange {
                symbol: sibling.clone(),
                reason: format!(
                    "shares {} callers with {} changed symbols",
                    count,
                    changed_symbols.len()
                ),
            });
        }
    }

    missing
}

/// Detect files historically coupled (CoChanged edges) with changed files but absent
/// from the diff. For each missing file, collects which changed files it's coupled with
/// and averages the coupling strength. Results are sorted by strength descending.
fn detect_missing_co_changes(
    graph: &dyn GraphBackend,
    changed_files: &[String],
) -> Vec<MissingCoChange> {
    let changed_set: HashSet<&str> = changed_files.iter().map(|s| s.as_str()).collect();

    // Map: missing_file_id -> Vec<(coupled_changed_file, weight)>
    let mut candidates: HashMap<String, Vec<(String, f64)>> = HashMap::new();

    for file_id in changed_files {
        let edges = graph.get_edges(file_id).unwrap_or_default();
        for edge in &edges {
            if edge.relationship != RelationshipType::CoChanged {
                continue;
            }
            // Find the other end of the edge
            let other = if edge.src == *file_id {
                &edge.dst
            } else {
                &edge.src
            };
            // Skip files already in the diff
            if changed_set.contains(other.as_str()) {
                continue;
            }
            // Strip "file:" prefix for display
            let file_path = file_id.strip_prefix("file:").unwrap_or(file_id);
            candidates
                .entry(other.clone())
                .or_default()
                .push((file_path.to_string(), edge.weight));
        }
    }

    // Minimum average coupling strength to surface a missing co-change.
    // Below this threshold, the coupling is too weak to be actionable.
    const MIN_CO_CHANGE_STRENGTH: f64 = 0.3;

    let mut result: Vec<MissingCoChange> = candidates
        .into_iter()
        .filter_map(|(file_id, couplings)| {
            let strength = couplings.iter().map(|(_, w)| w).sum::<f64>() / couplings.len() as f64;
            if strength < MIN_CO_CHANGE_STRENGTH {
                return None;
            }
            let coupled_with = couplings.into_iter().map(|(f, _)| f).collect();
            let file_path = file_id
                .strip_prefix("file:")
                .unwrap_or(&file_id)
                .to_string();
            Some(MissingCoChange {
                file_path,
                coupled_with,
                strength,
            })
        })
        .collect();

    result.sort_by(|a, b| {
        b.strength
            .partial_cmp(&a.strength)
            .unwrap_or(std::cmp::Ordering::Equal)
    });
    result
}

#[cfg(test)]
#[path = "tests/review_tests.rs"]
mod tests;