leankg 0.19.10

Lightweight Knowledge Graph for AI-Assisted Development
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
use crate::db::schema::CozoDb;
use crate::graph::GraphEngine;
use std::collections::HashMap;

pub struct CommunityDetector {
    graph_engine: GraphEngine,
}

impl CommunityDetector {
    pub fn new(db: &CozoDb) -> Self {
        Self {
            graph_engine: GraphEngine::new(db.clone()),
        }
    }

    /// Louvain-inspired community detection with modularity optimization
    /// This implements the core Louvain algorithm principles:
    /// 1. Initialize each node in its own community
    /// 2. Greedily move nodes to communities that maximize modularity gain
    /// 3. Aggregate the graph and repeat
    /// 4. Refine partitions on original graph
    pub fn detect_communities(
        &self,
    ) -> Result<HashMap<String, Cluster>, Box<dyn std::error::Error>> {
        let elements = self.graph_engine.all_elements()?;
        let relationships = self.graph_engine.all_relationships()?;

        if elements.is_empty() {
            return Ok(HashMap::new());
        }

        // Build adjacency list with edge weights
        let mut adjacency: HashMap<String, Vec<(String, f64)>> = HashMap::new();
        let mut total_weight: f64 = 0.0;

        for elem in &elements {
            adjacency.entry(elem.qualified_name.clone()).or_default();
        }

        // Count weighted edges for CALLS and IMPORTS relationships
        for rel in &relationships {
            if rel.rel_type == "calls" || rel.rel_type == "imports" {
                let weight = if rel.rel_type == "calls" { 2.0 } else { 1.0 };
                total_weight += weight;

                adjacency
                    .entry(rel.source_qualified.clone())
                    .or_default()
                    .push((rel.target_qualified.clone(), weight));
                adjacency
                    .entry(rel.target_qualified.clone())
                    .or_default()
                    .push((rel.source_qualified.clone(), weight));
            }
        }

        if adjacency.is_empty() || total_weight == 0.0 {
            // Fall back to folder-based clustering if no edges
            return self.fallback_folder_clustering(&elements);
        }

        let node_ids: Vec<String> = elements.iter().map(|e| e.qualified_name.clone()).collect();
        let _n = node_ids.len();

        // Initialize: each node in its own community
        let mut community: HashMap<String, usize> = HashMap::new();
        let mut community_nodes: HashMap<usize, Vec<String>> = HashMap::new();
        let mut community_weights: HashMap<usize, f64> = HashMap::new();

        for (i, node_id) in node_ids.iter().enumerate() {
            community.insert(node_id.clone(), i);
            community_nodes.insert(i, vec![node_id.clone()]);
            let w: f64 = adjacency
                .get(node_id)
                .map(|neighbors| neighbors.iter().map(|(_, w)| w).sum())
                .unwrap_or(0.0);
            community_weights.insert(i, w);
        }

        // Pre-compute node total weights
        let node_weights: HashMap<String, f64> = adjacency
            .iter()
            .map(|(k, v)| (k.clone(), v.iter().map(|(_, w)| w).sum()))
            .collect();

        // Louvain iterations: greedily optimize modularity
        let resolution = 1.0;
        let m2 = total_weight * 2.0; // Total edge weight * 2 (undirected)

        let mut improved = true;
        let mut iterations = 0;
        let max_iterations = 10;

        while improved && iterations < max_iterations {
            improved = false;
            iterations += 1;

            for node_id in &node_ids {
                let current_comm = *community.get(node_id).unwrap_or(&0);
                let node_w = *node_weights.get(node_id).unwrap_or(&0.0);

                // Calculate current modularity contribution
                let neighbors = adjacency.get(node_id).cloned().unwrap_or_default();
                if neighbors.is_empty() {
                    continue;
                }

                // Find best community to join
                let mut best_comm = current_comm;
                let mut best_gain = 0.0;

                for (neighbor, _edge_weight) in &neighbors {
                    if let Some(&neighbor_comm) = community.get(neighbor) {
                        if neighbor_comm == current_comm {
                            continue;
                        }

                        // Calculate modularity gain using Louvain formula
                        // gain = (Incoming / 2m) - (total_weight * community_weight / (2m)^2) * resolution
                        let incoming: f64 = neighbors
                            .iter()
                            .filter(|(_, _w)| {
                                *community.get(neighbor).unwrap_or(&0) == neighbor_comm
                            })
                            .map(|(_, w)| w)
                            .sum();

                        let comm_weight = *community_weights.get(&neighbor_comm).unwrap_or(&0.0);
                        let gain = incoming - (node_w * comm_weight / m2) * resolution;

                        if gain > best_gain {
                            best_gain = gain;
                            best_comm = neighbor_comm;
                        }
                    }
                }

                // Move node to best community if gain is positive
                if best_gain > 0.001 && best_comm != current_comm {
                    // Remove from current community
                    if let Some(current_members) = community_nodes.get_mut(&current_comm) {
                        current_members.retain(|n| n != node_id);
                    }
                    if let Some(current_weight) = community_weights.get_mut(&current_comm) {
                        *current_weight -= node_w;
                    }

                    // Add to new community
                    community.insert(node_id.clone(), best_comm);
                    community_nodes
                        .entry(best_comm)
                        .or_default()
                        .push(node_id.clone());
                    if let Some(new_weight) = community_weights.get_mut(&best_comm) {
                        *new_weight += node_w;
                    }

                    improved = true;
                }
            }
        }

        // Build clusters from communities
        let mut clusters: HashMap<String, Cluster> = HashMap::new();
        let mut cluster_id_counter = 0;

        for (comm_id, members) in community_nodes {
            if members.is_empty() {
                continue;
            }

            // Use representative file to generate cluster label
            let representative = members.first().unwrap();
            let elem = elements
                .iter()
                .find(|e| &e.qualified_name == representative);
            let file_path = elem.map(|e| e.file_path.as_str()).unwrap_or("");
            let cluster_label =
                self.generate_cluster_label(&format!("comm_{}", comm_id), file_path);

            let cluster_id = format!("cluster_{}", cluster_id_counter);
            cluster_id_counter += 1;

            // Calculate representative files
            let mut file_counts: HashMap<String, usize> = HashMap::new();
            for member in &members {
                if let Some(elem) = elements.iter().find(|e| &e.qualified_name == member) {
                    *file_counts.entry(elem.file_path.clone()).or_insert(0) += 1;
                }
            }
            let mut files: Vec<(String, usize)> = file_counts.into_iter().collect();
            files.sort_by_key(|b| std::cmp::Reverse(b.1));
            let representative_files: Vec<String> =
                files.into_iter().take(5).map(|(path, _)| path).collect();

            clusters.insert(
                cluster_id.clone(),
                Cluster {
                    id: cluster_id.clone(),
                    label: cluster_label,
                    members,
                    representative_files,
                },
            );
        }

        Ok(clusters)
    }

    /// Fallback clustering when no edges exist - groups by folder
    fn fallback_folder_clustering(
        &self,
        elements: &[crate::db::models::CodeElement],
    ) -> Result<HashMap<String, Cluster>, Box<dyn std::error::Error>> {
        let mut folder_groups: HashMap<String, Vec<String>> = HashMap::new();

        for elem in elements {
            let folder = if let Some(last_slash) = elem.file_path.rfind('/') {
                elem.file_path[..last_slash].to_string()
            } else {
                "root".to_string()
            };
            folder_groups
                .entry(folder)
                .or_default()
                .push(elem.qualified_name.clone());
        }

        let mut clusters: HashMap<String, Cluster> = HashMap::new();
        let mut cluster_id_counter = 0;

        for (folder, members) in folder_groups {
            if members.is_empty() {
                continue;
            }

            let cluster_label = folder.split('/').next_back().unwrap_or(&folder).to_string();

            let cluster_id = format!("cluster_{}", cluster_id_counter);
            cluster_id_counter += 1;

            clusters.insert(
                cluster_id.clone(),
                Cluster {
                    id: cluster_id.clone(),
                    label: cluster_label,
                    members,
                    representative_files: vec![folder],
                },
            );
        }

        Ok(clusters)
    }

    fn generate_cluster_label(&self, cluster_id: &str, file_path: &str) -> String {
        let path_parts: Vec<&str> = file_path.split('/').collect();
        if path_parts.len() >= 2 {
            let dir = path_parts[path_parts.len() - 2];
            let normalized = dir
                .chars()
                .map(|c| {
                    if c.is_alphanumeric() {
                        c.to_ascii_lowercase()
                    } else {
                        '_'
                    }
                })
                .collect::<String>();
            if !normalized.is_empty() && normalized != "_" {
                return normalized;
            }
        }
        cluster_id
            .replace("cluster_", "module_")
            .replace("comm_", "module_")
    }

    pub fn assign_clusters_to_elements(&self) -> Result<(), Box<dyn std::error::Error>> {
        let clusters = self.detect_communities()?;

        for cluster in clusters.values() {
            for member_qualified in &cluster.members {
                self.graph_engine.update_element_cluster(
                    member_qualified,
                    Some(cluster.id.clone()),
                    Some(cluster.label.clone()),
                )?;
            }
        }

        Ok(())
    }
}

/// FR-CL-MEGA-01: serve clusters already written to `cluster_id` / `cluster_label`
/// without running live Louvain (unsafe on mega-graphs).
pub fn load_precomputed_clusters(
    engine: &GraphEngine,
    limit: usize,
) -> Result<(Vec<Cluster>, ClusterStats), Box<dyn std::error::Error>> {
    let limit = limit.clamp(1, 500);
    let db = engine.db();
    // Probe arity once via a cheap limit-0 script (same pattern as GraphEngine).
    let tail = if crate::db::schema::run_script(
        db,
        "?[qualified_name] := *code_elements[qualified_name, element_type, name, file_path, line_start, line_end, language, parent_qualified, cluster_id, cluster_label, metadata, env, ontology_layer] :limit 0",
        Default::default(),
    )
    .is_ok()
    {
        ", env, ontology_layer"
    } else {
        ", env"
    };

    let ids_query = format!(
        r#"?[cluster_id, cluster_label] := *code_elements[qualified_name, element_type, name, file_path, line_start, line_end, language, parent_qualified, cluster_id, cluster_label, metadata{tail}],
            cluster_id != null,
            cluster_id != ""
            :limit {limit}"#
    );
    let id_rows = crate::db::schema::run_script(db, &ids_query, Default::default())?;

    let mut by_id: HashMap<String, Cluster> = HashMap::new();
    for row in &id_rows.rows {
        let id = row[0].get_str().unwrap_or("").to_string();
        if id.is_empty() {
            continue;
        }
        let label = row[1]
            .get_str()
            .map(String::from)
            .filter(|s| !s.is_empty())
            .unwrap_or_else(|| id.clone());
        by_id.entry(id.clone()).or_insert_with(|| Cluster {
            id: id.clone(),
            label,
            members: Vec::new(),
            representative_files: Vec::new(),
        });
    }

    if by_id.is_empty() {
        return Ok((
            Vec::new(),
            ClusterStats {
                total_clusters: 0,
                total_members: 0,
                avg_cluster_size: 0.0,
            },
        ));
    }

    for (cid, cluster) in by_id.iter_mut() {
        let safe_cid = cid.replace('\\', "\\\\").replace('"', "\\\"");
        let mem_q = format!(
            r#"?[qualified_name, file_path] := *code_elements[qualified_name, element_type, name, file_path, line_start, line_end, language, parent_qualified, cluster_id, cluster_label, metadata{tail}],
                cluster_id = "{safe_cid}"
                :limit 40"#
        );
        if let Ok(mem_rows) = crate::db::schema::run_script(db, &mem_q, Default::default()) {
            for row in &mem_rows.rows {
                let qn = row[0].get_str().unwrap_or("").to_string();
                let fp = row[1].get_str().unwrap_or("").to_string();
                if !qn.is_empty() {
                    cluster.members.push(qn);
                }
                if !fp.is_empty()
                    && cluster.representative_files.len() < 5
                    && !cluster.representative_files.contains(&fp)
                {
                    cluster.representative_files.push(fp);
                }
            }
        }
    }

    let mut clusters: Vec<Cluster> = by_id.into_values().collect();
    clusters.sort_by_key(|b| std::cmp::Reverse(b.members.len()));
    let map: HashMap<String, Cluster> =
        clusters.iter().map(|c| (c.id.clone(), c.clone())).collect();
    let stats = get_cluster_stats(&map);
    Ok((clusters.into_iter().take(limit).collect(), stats))
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Cluster {
    pub id: String,
    pub label: String,
    pub members: Vec<String>,
    pub representative_files: Vec<String>,
}

pub fn get_cluster_stats(clusters: &HashMap<String, Cluster>) -> ClusterStats {
    let total_members: usize = clusters.values().map(|c| c.members.len()).sum();
    let avg_cluster_size = if clusters.is_empty() {
        0.0
    } else {
        total_members as f64 / clusters.len() as f64
    };

    ClusterStats {
        total_clusters: clusters.len(),
        total_members,
        avg_cluster_size,
    }
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ClusterStats {
    pub total_clusters: usize,
    pub total_members: usize,
    pub avg_cluster_size: f64,
}

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

    #[test]
    fn test_cluster_stats() {
        let mut clusters = HashMap::new();
        clusters.insert(
            "c1".to_string(),
            Cluster {
                id: "c1".to_string(),
                label: "auth".to_string(),
                members: vec!["a".to_string(), "b".to_string()],
                representative_files: vec!["auth.rs".to_string()],
            },
        );
        clusters.insert(
            "c2".to_string(),
            Cluster {
                id: "c2".to_string(),
                label: "api".to_string(),
                members: vec!["c".to_string(), "d".to_string(), "e".to_string()],
                representative_files: vec!["api.rs".to_string()],
            },
        );

        let stats = get_cluster_stats(&clusters);
        assert_eq!(stats.total_clusters, 2);
        assert_eq!(stats.total_members, 5);
        assert!((stats.avg_cluster_size - 2.5).abs() < 0.001);
    }
}