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
//! Cyclomatic and cognitive complexity metrics for functions/methods.
use super::{resolve_path, EnrichResult};
use crate::CodememEngine;
use codemem_core::{CodememError, NodeKind};
use serde_json::json;
use std::collections::HashMap;
use std::path::Path;
impl CodememEngine {
/// Enrich the graph with cyclomatic and cognitive complexity metrics for functions/methods.
///
/// For each Function/Method node, reads the source file, counts decision points
/// (if/else/match/for/while/loop/&&/||) as cyclomatic complexity and measures
/// max nesting depth as a cognitive complexity proxy. High-complexity functions
/// (cyclomatic > 10) produce Insight memories.
pub fn enrich_complexity(
&self,
namespace: Option<&str>,
project_root: Option<&Path>,
) -> Result<EnrichResult, CodememError> {
let all_nodes = {
let graph = self.lock_graph()?;
graph.get_all_nodes()
};
// Collect function/method nodes with file info
struct SymbolInfo {
node_id: String,
label: String,
file_path: String,
line_start: usize,
line_end: usize,
}
let mut symbols: Vec<SymbolInfo> = Vec::new();
for node in &all_nodes {
if !matches!(node.kind, NodeKind::Function | NodeKind::Method) {
continue;
}
let file_path = match node.payload.get("file_path").and_then(|v| v.as_str()) {
Some(fp) => fp.to_string(),
None => continue,
};
let line_start = node
.payload
.get("line_start")
.and_then(|v| v.as_u64())
.unwrap_or(0) as usize;
let line_end = node
.payload
.get("line_end")
.and_then(|v| v.as_u64())
.unwrap_or(0) as usize;
if line_end <= line_start {
continue;
}
symbols.push(SymbolInfo {
node_id: node.id.clone(),
label: node.label.clone(),
file_path,
line_start,
line_end,
});
}
// Cache file contents to avoid re-reading
let mut file_cache: HashMap<String, Vec<String>> = HashMap::new();
let mut annotated = 0usize;
let mut insights_stored = 0usize;
// Nodes to annotate (collected first, then applied in a single lock scope)
struct ComplexityData {
node_id: String,
cyclomatic: usize,
cognitive: usize,
}
let mut complexity_data: Vec<ComplexityData> = Vec::new();
// Insights to store (collected first, then stored outside the lock)
struct ComplexityInsight {
content: String,
importance: f64,
node_id: String,
}
let mut pending_insights: Vec<ComplexityInsight> = Vec::new();
for sym in &symbols {
let lines = file_cache.entry(sym.file_path.clone()).or_insert_with(|| {
std::fs::read_to_string(resolve_path(&sym.file_path, project_root))
.unwrap_or_default()
.lines()
.map(String::from)
.collect()
});
// Extract the function's lines (1-indexed to 0-indexed)
let start = sym.line_start.saturating_sub(1);
let end = sym.line_end.min(lines.len());
if start >= end {
continue;
}
let fn_lines = &lines[start..end];
// Count cyclomatic complexity: decision points
let mut cyclomatic: usize = 1; // base path
let mut max_depth: usize = 0;
let mut current_depth: usize = 0;
for line in fn_lines {
let trimmed = line.trim();
// Count decision points
for keyword in &[
"if ", "if(", "else if", "match ", "for ", "for(", "while ", "while(", "loop ",
"loop{",
] {
if trimmed.starts_with(keyword) || trimmed.contains(&format!(" {keyword}")) {
cyclomatic += 1;
break;
}
}
// Count logical operators as additional branches
cyclomatic += trimmed.matches("&&").count();
cyclomatic += trimmed.matches("||").count();
// Track nesting depth via braces
for ch in trimmed.chars() {
match ch {
'{' => {
current_depth += 1;
max_depth = max_depth.max(current_depth);
}
'}' => {
current_depth = current_depth.saturating_sub(1);
}
_ => {}
}
}
}
complexity_data.push(ComplexityData {
node_id: sym.node_id.clone(),
cyclomatic,
cognitive: max_depth,
});
annotated += 1;
// High complexity threshold
if cyclomatic > 10 {
let importance = if cyclomatic > 20 { 0.9 } else { 0.7 };
pending_insights.push(ComplexityInsight {
content: format!(
"High complexity: {} — cyclomatic={}, max_nesting={} in {}",
sym.label, cyclomatic, max_depth, sym.file_path
),
importance,
node_id: sym.node_id.clone(),
});
}
}
// Annotate graph nodes
{
let mut graph = self.lock_graph()?;
for data in &complexity_data {
if let Ok(Some(mut node)) = graph.get_node(&data.node_id) {
node.payload
.insert("cyclomatic_complexity".into(), json!(data.cyclomatic));
node.payload
.insert("cognitive_complexity".into(), json!(data.cognitive));
let _ = graph.add_node(node);
}
}
}
// Store insights (outside graph lock)
for insight in &pending_insights {
if self
.store_insight(
&insight.content,
"complexity",
&[],
insight.importance,
namespace,
std::slice::from_ref(&insight.node_id),
)
.is_some()
{
insights_stored += 1;
}
}
self.save_index();
Ok(EnrichResult {
insights_stored,
details: json!({
"symbols_analyzed": annotated,
"high_complexity_count": pending_insights.len(),
"insights_stored": insights_stored,
}),
})
}
}