codemem-engine 0.19.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
//! Dead code detection: identifies symbols with zero inbound CALLS/IMPORTS edges,
//! applying framework-aware exemptions (decorators, constructors, tests, etc.).

use super::EnrichResult;
use crate::CodememEngine;
use codemem_core::config::DeadCodeConfig;
use codemem_core::{CodememError, Edge, GraphNode, RelationshipType};
use serde_json::json;
use std::collections::HashSet;

/// A symbol detected as potentially dead (unreferenced) code.
#[derive(Debug, Clone)]
pub struct DeadCodeEntry {
    /// Graph node ID of the unreferenced symbol.
    pub node_id: String,
    /// Human-readable symbol name (from `node.label`).
    pub label: String,
    /// The symbol kind string from `node.payload["kind"]`.
    pub kind: String,
    /// Optional file path from `node.payload["file_path"]`.
    pub file_path: Option<String>,
}

/// Keyword fragments in decorator/attribute values that signal framework entry points.
const FRAMEWORK_KEYWORDS: &[&str] = &["route", "endpoint", "export", "api"];

/// Analyze the graph for unreferenced symbols (dead code candidates).
///
/// Symbols are considered dead when they have no inbound `Calls`, `Imports`,
/// `Inherits`, or `Implements` edges and do not match any exemption rule.
pub fn find_dead_code(
    nodes: &[GraphNode],
    edges: &[Edge],
    config: &DeadCodeConfig,
) -> Vec<DeadCodeEntry> {
    // Step 1: filter to symbol nodes (those with a "kind" key in payload).
    let symbol_nodes: Vec<&GraphNode> = nodes
        .iter()
        .filter(|n| n.payload.contains_key("kind"))
        .collect();

    // Step 2: min_symbols threshold — avoid false positives on tiny graphs.
    if symbol_nodes.len() < config.min_symbols {
        return Vec::new();
    }

    // Step 3: build set of node IDs that have inbound referencing edges.
    let referenced: HashSet<&str> = edges
        .iter()
        .filter(|e| {
            matches!(
                e.relationship,
                RelationshipType::Calls
                    | RelationshipType::Imports
                    | RelationshipType::Inherits
                    | RelationshipType::Implements
            )
        })
        .map(|e| e.dst.as_str())
        .collect();

    // Step 4: collect unreferenced, non-exempt symbols.
    let mut dead: Vec<DeadCodeEntry> = Vec::new();

    for node in &symbol_nodes {
        if referenced.contains(node.id.as_str()) {
            continue;
        }

        if is_exempt(node, config) {
            continue;
        }

        let kind = node
            .payload
            .get("kind")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();

        let file_path = node
            .payload
            .get("file_path")
            .and_then(|v| v.as_str())
            .map(String::from);

        dead.push(DeadCodeEntry {
            node_id: node.id.clone(),
            label: node.label.clone(),
            kind,
            file_path,
        });
    }

    dead
}

/// Check whether a symbol node should be exempt from dead code detection.
fn is_exempt(node: &GraphNode, config: &DeadCodeConfig) -> bool {
    // Kind exemption: payload["kind"] in config.exempt_kinds
    if let Some(kind_val) = node.payload.get("kind").and_then(|v| v.as_str()) {
        let kind_lower = kind_val.to_lowercase();
        if config
            .exempt_kinds
            .iter()
            .any(|k| k.to_lowercase() == kind_lower)
        {
            return true;
        }
    }

    // Decorator/attribute exemption
    if let Some(attrs) = node.payload.get("attributes").and_then(|v| v.as_array()) {
        for attr in attrs {
            if let Some(attr_str) = attr.as_str() {
                let attr_lower = attr_str.to_lowercase();
                // Exact match against configured exempt decorators
                if config
                    .exempt_decorators
                    .iter()
                    .any(|d| attr_lower.contains(&d.to_lowercase()))
                {
                    return true;
                }
                // Framework keyword match
                if FRAMEWORK_KEYWORDS.iter().any(|kw| attr_lower.contains(kw)) {
                    return true;
                }
            }
        }
    }

    // Main entry point
    if node.label == "main" || node.label == "Main" {
        return true;
    }

    // Dunder methods (Python __init__, __str__, etc.)
    if node.label.starts_with("__") && node.label.ends_with("__") {
        return true;
    }

    // Public visibility — public symbols may be part of library API
    if let Some(vis) = node.payload.get("visibility").and_then(|v| v.as_str()) {
        if vis == "public" {
            return true;
        }
    }

    false
}

impl CodememEngine {
    /// Run dead code detection on the current graph and store insights for
    /// unreferenced symbols.
    pub fn enrich_dead_code(&self, namespace: Option<&str>) -> Result<EnrichResult, CodememError> {
        let config = &self.config.enrichment.dead_code;
        if !config.enabled {
            return Ok(EnrichResult {
                insights_stored: 0,
                details: json!({"skipped": true, "reason": "dead_code disabled"}),
            });
        }

        // Collect all nodes from the in-memory graph and all edges from storage.
        // Using storage.all_graph_edges() avoids N+1 per-node get_edges calls.
        let all_nodes = self.lock_graph()?.get_all_nodes();
        let all_edges = self.storage.all_graph_edges()?;

        let dead_entries = find_dead_code(&all_nodes, &all_edges, config);

        let mut insights_stored = 0;
        for entry in &dead_entries {
            let file_info = entry
                .file_path
                .as_deref()
                .map(|fp| format!(" in {fp}"))
                .unwrap_or_default();
            let content = format!(
                "Dead code candidate: `{}` ({}) has no callers or importers{}",
                entry.label, entry.kind, file_info,
            );
            let links = vec![entry.node_id.clone()];
            if self
                .store_insight(
                    &content,
                    "dead-code",
                    &["dead-code"],
                    0.6,
                    namespace,
                    &links,
                )
                .is_some()
            {
                insights_stored += 1;
            }
        }

        self.save_index();

        Ok(EnrichResult {
            insights_stored,
            details: json!({
                "dead_code_candidates": dead_entries.len(),
                "insights_stored": insights_stored,
            }),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use codemem_core::{GraphNode, NodeKind};
    use std::collections::HashMap;

    /// Helper: create a GraphNode with the given label, kind payload, and optional attributes.
    fn make_graph_node(name: &str, kind_str: &str, attrs: Option<Vec<&str>>) -> GraphNode {
        let mut payload: HashMap<String, serde_json::Value> = HashMap::new();
        payload.insert("kind".into(), json!(kind_str));
        if let Some(attr_list) = attrs {
            payload.insert("attributes".into(), json!(attr_list));
        }
        GraphNode {
            id: format!("sym:{name}"),
            kind: NodeKind::Function,
            label: name.to_string(),
            payload,
            centrality: 0.0,
            memory_id: None,
            namespace: None,
            valid_from: None,
            valid_to: None,
        }
    }

    /// Helper: create an Edge with the given src, dst, and relationship.
    fn make_edge(src: &str, dst: &str, rel: RelationshipType) -> Edge {
        Edge {
            id: format!("{src}-{:?}-{dst}", rel),
            src: src.to_string(),
            dst: dst.to_string(),
            relationship: rel,
            weight: 1.0,
            properties: HashMap::new(),
            created_at: chrono::Utc::now(),
            valid_from: None,
            valid_to: None,
        }
    }

    fn test_config() -> DeadCodeConfig {
        DeadCodeConfig {
            min_symbols: 2,
            ..DeadCodeConfig::default()
        }
    }

    #[test]
    fn unreachable_function_detected() {
        let nodes = vec![
            make_graph_node("main", "function", None),
            make_graph_node("helper", "function", None),
            make_graph_node("unused_fn", "function", None),
        ];
        let edges = vec![make_edge("sym:main", "sym:helper", RelationshipType::Calls)];
        let config = test_config();

        let dead = find_dead_code(&nodes, &edges, &config);

        // unused_fn has no callers and is not main → should be detected.
        // main is exempt (entry point). helper is called by main → not dead.
        let dead_labels: Vec<&str> = dead.iter().map(|d| d.label.as_str()).collect();
        assert!(
            dead_labels.contains(&"unused_fn"),
            "unused_fn should be detected as dead code"
        );
        assert!(
            !dead_labels.contains(&"helper"),
            "helper is called by main, should not be dead"
        );
        assert!(
            !dead_labels.contains(&"main"),
            "main is exempt as entry point"
        );
    }

    #[test]
    fn decorated_symbols_exempt() {
        let nodes = vec![
            make_graph_node("index", "function", Some(vec!["app.route"])),
            make_graph_node("unused", "function", None),
            make_graph_node("api_handler", "function", Some(vec!["get_endpoint"])),
        ];
        let edges = vec![];
        let config = test_config();

        let dead = find_dead_code(&nodes, &edges, &config);

        let dead_labels: Vec<&str> = dead.iter().map(|d| d.label.as_str()).collect();
        assert!(
            !dead_labels.contains(&"index"),
            "app.route decorated should be exempt"
        );
        assert!(
            !dead_labels.contains(&"api_handler"),
            "endpoint keyword in attribute should be exempt"
        );
        assert!(
            dead_labels.contains(&"unused"),
            "unused with no decorators should be detected"
        );
    }

    #[test]
    fn constructors_and_tests_exempt() {
        let nodes = vec![
            make_graph_node("__init__", "constructor", None),
            make_graph_node("test_foo", "test", None),
            make_graph_node("orphan", "function", None),
        ];
        let edges = vec![];
        let config = test_config();

        let dead = find_dead_code(&nodes, &edges, &config);

        let dead_labels: Vec<&str> = dead.iter().map(|d| d.label.as_str()).collect();
        assert!(
            !dead_labels.contains(&"__init__"),
            "constructor kind should be exempt"
        );
        assert!(
            !dead_labels.contains(&"test_foo"),
            "test kind should be exempt"
        );
        assert!(
            dead_labels.contains(&"orphan"),
            "orphan function should be detected"
        );
    }

    #[test]
    fn min_symbols_threshold_respected() {
        let nodes = vec![make_graph_node("lonely", "function", None)];
        let edges = vec![];
        let config = DeadCodeConfig {
            min_symbols: 10,
            ..DeadCodeConfig::default()
        };

        let dead = find_dead_code(&nodes, &edges, &config);
        assert!(
            dead.is_empty(),
            "Should return empty when symbol count < min_symbols"
        );
    }

    #[test]
    fn public_symbols_exempt() {
        let mut node = make_graph_node("pub_fn", "function", None);
        node.payload.insert("visibility".into(), json!("public"));
        let nodes = vec![node, make_graph_node("priv_fn", "function", None)];
        let edges = vec![];
        let config = test_config();

        let dead = find_dead_code(&nodes, &edges, &config);
        let dead_labels: Vec<&str> = dead.iter().map(|d| d.label.as_str()).collect();
        assert!(
            !dead_labels.contains(&"pub_fn"),
            "public symbol should be exempt"
        );
        assert!(
            dead_labels.contains(&"priv_fn"),
            "private symbol with no callers should be detected"
        );
    }

    #[test]
    fn dunder_methods_exempt() {
        let nodes = vec![
            make_graph_node("__str__", "method", None),
            make_graph_node("orphan_method", "method", None),
        ];
        let edges = vec![];
        let config = test_config();

        let dead = find_dead_code(&nodes, &edges, &config);
        let dead_labels: Vec<&str> = dead.iter().map(|d| d.label.as_str()).collect();
        assert!(
            !dead_labels.contains(&"__str__"),
            "dunder method should be exempt"
        );
        assert!(
            dead_labels.contains(&"orphan_method"),
            "non-dunder method should be detected"
        );
    }

    #[test]
    fn inherits_and_implements_count_as_references() {
        let nodes = vec![
            make_graph_node("BaseClass", "class", None),
            make_graph_node("MyTrait", "trait", None),
            make_graph_node("orphan_class", "class", None),
        ];
        let edges = vec![
            make_edge(
                "sym:orphan_class",
                "sym:BaseClass",
                RelationshipType::Inherits,
            ),
            make_edge(
                "sym:orphan_class",
                "sym:MyTrait",
                RelationshipType::Implements,
            ),
        ];
        let config = test_config();

        let dead = find_dead_code(&nodes, &edges, &config);
        let dead_labels: Vec<&str> = dead.iter().map(|d| d.label.as_str()).collect();
        assert!(
            !dead_labels.contains(&"BaseClass"),
            "inherited class should not be dead"
        );
        assert!(
            !dead_labels.contains(&"MyTrait"),
            "implemented trait should not be dead"
        );
    }
}