leindex 2.1.1

LeIndex MCP and semantic code search engine for AI tools and large codebases
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
583
584
585
586
587
588
589
590
use super::helpers::{extract_bool, extract_usize, resolve_scope, wrap_with_meta};
use super::protocol::JsonRpcError;
use crate::cli::registry::ProjectRegistry;
use serde_json::Value;
#[cfg(feature = "community")]
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;

type FileMap = std::collections::HashMap<String, (usize, u32, Vec<String>, usize, usize)>;

/// Handler for LeIndex [project_map — annotated project tree replacing Glob/ls.
#[derive(Clone)]
pub struct ProjectMapHandler;

#[allow(missing_docs)]
impl ProjectMapHandler {
    pub fn name(&self) -> &str {
        "leindex_project_map"
    }

    pub fn title(&self) -> &str {
        "LeIndex [Project Map]"
    }

    pub fn description(&self) -> &str {
        "Project structure map — use instead of Glob/ls for directory listing. Shows files \
with symbol counts, complexity hotspots, and inter-module dependency arrows. Supports \
scoping to subdirectories, sorting, and pagination."
    }

    pub fn argument_schema(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "path": {
                    "type": "string",
                    "description": "Subdirectory to scope to (default: project root)"
                },
                "group_by": {
                    "type": "string",
                    "enum": ["tree", "community"],
                    "description": "Grouping mode: tree (default, flat file list) or community (Leiden communities — module boundaries the codebase itself may not have named)",
                    "default": "tree"
                },
                "project_path": {
                    "type": "string",
                    "description": "Project directory (auto-indexes on first use; omit to use current project)"
                },
                "depth": {
                    "type": "integer",
                    "description": "Tree depth (default: 3, max: 10)",
                    "default": 3,
                    "minimum": 1,
                    "maximum": 10
                },
                "token_budget": {
                    "type": "integer",
                    "description": "Max tokens for response (default: 2000)",
                    "default": 2000
                },
                "sort_by": {
                    "type": "string",
                    "enum": ["complexity", "name", "dependencies", "size"],
                    "description": "Sort order (default: complexity)",
                    "default": "complexity"
                },
                "include_symbols": {
                    "type": "boolean",
                    "description": "Include top symbols per file (default: false). \
        Also accepts compatibility strings: 'true'/'false', '1'/'0', 'yes'/'no'.",
                    "default": false
                },
                "offset": {
                    "type": "integer",
                    "description": "Skip the first N files for pagination (default: 0)",
                    "default": 0,
                    "minimum": 0
                },
                "limit": {
                    "type": "integer",
                    "description": "Maximum number of files to return (default: unlimited, subject to token_budget)",
                    "minimum": 1
                },
                "focus": {
                    "type": "string",
                    "description": "Semantic focus area — ranks files by relevance to this topic (e.g., 'authentication', 'database layer', 'payment flow')"
                }
            },
            "required": []
        })
    }

    pub async fn execute(
        &self,
        registry: &Arc<ProjectRegistry>,
        args: Value,
    ) -> Result<Value, JsonRpcError> {
        let sort_by = args
            .get("sort_by")
            .and_then(|v| v.as_str())
            .unwrap_or("complexity")
            .to_owned();
        let depth = extract_usize(&args, "depth", 3)?.min(10);
        let token_budget = extract_usize(&args, "token_budget", 2000)?;
        let include_symbols = extract_bool(&args, "include_symbols", false);
        let group_by = args
            .get("group_by")
            .and_then(Value::as_str)
            .unwrap_or("tree")
            .to_string();
        let offset = extract_usize(&args, "offset", 0)?;
        let limit = args
            .get("limit")
            .and_then(|v| v.as_u64())
            .map(|v| v as usize);
        let focus = args.get("focus").and_then(|v| v.as_str()).map(String::from);

        let project_path = args.get("project_path").and_then(|v| v.as_str());
        let handle = registry.get_or_create(project_path).await?;
        let mut guard = handle.write().await;

        // Graph-only hydration: this tool never queries the search engine,
        // so skip snapshot/embedding-mmap restoration (~1s per cold call).
        guard
            .ensure_pdg_loaded_graph_only()
            .map_err(|e| JsonRpcError::indexing_failed(format!("Failed to load PDG: {}", e)))?;

        if guard.pdg().is_none() {
            return Err(JsonRpcError::project_not_indexed(
                guard.project_path().display().to_string(),
            ));
        }

        let project_root = guard.project_path().to_path_buf();

        let (scope_str, scope_path, scope_base) =
            Self::scope_paths(&args, guard.project_path(), &project_root)?;

        let file_map = Self::build_file_map(&mut guard);

        // Get PDG for scope filtering (no degree computation needed — cached in file_map)
        let _pdg = guard
            .pdg()
            .ok_or_else(|| JsonRpcError::project_not_indexed(project_root.display().to_string()))?;

        let mut files = Self::files_in_scope(
            &file_map,
            &scope_str,
            &scope_path,
            &scope_base,
            depth,
            include_symbols || focus.is_some(),
        );
        Self::sort_and_rank_files(
            &mut guard,
            &mut files,
            &sort_by,
            focus.as_deref(),
            include_symbols || focus.is_some(),
        );

        let (total_before_pagination, truncated_files) =
            Self::paginate_and_truncate(files, offset, limit, token_budget);

        if group_by == "community" {
            return Ok(wrap_with_meta(
                self.community_grouped_response(
                    &guard,
                    &truncated_files,
                    total_before_pagination,
                    &project_root.display().to_string(),
                    &scope_path.display().to_string(),
                    offset,
                ),
                &guard,
            ));
        }

        Ok(wrap_with_meta(
            serde_json::json!({
                "project_root": project_root.display().to_string(),
                "scope": scope_path.display().to_string(),
                "total_files_in_scope": total_before_pagination,
                "offset": offset,
                "count": truncated_files.len(),
                "has_more": offset + truncated_files.len() < total_before_pagination,
                "files": truncated_files
            }),
            &guard,
        ))
    }

    /// Grouped-by-community response: reads persisted Leiden labels and
    /// buckets the already-paginated file entries using memberships on the
    /// resident PDG. Labels remain derived metadata in `intel_communities`.
    #[cfg(feature = "community")]
    fn community_grouped_response(
        &self,
        guard: &crate::cli::leindex::LeIndex,
        files: &[Value],
        total: usize,
        project_root: &str,
        scope: &str,
        offset: usize,
    ) -> Value {
        let labels = crate::storage::community_store::load_community_labels(
            &guard.storage,
            guard.project_id(),
            crate::graph::community::COMMUNITY_ALGORITHM,
            crate::graph::community::COMMUNITY_QUALITY,
            crate::graph::community::COMMUNITY_RESOLUTION,
        )
        .unwrap_or_default();

        let label_by_community: HashMap<i64, String> = labels
            .iter()
            .map(|(community, _, label)| (*community, label.clone()))
            .collect();

        // The resident PDG carries validated derived memberships; aggregate
        // node memberships to file memberships without graph-table SQL.
        let mut file_community: HashMap<String, i64> = HashMap::new();
        if let Some(pdg) = guard.pdg() {
            for (&node_id, &community) in &pdg.communities {
                if let Some(node) = pdg.get_node(node_id) {
                    file_community.insert(node.file_path.to_string(), i64::from(community));
                }
            }
        }

        let mut groups: HashMap<i64, Vec<&Value>> = HashMap::new();
        let mut ungrouped: Vec<&Value> = Vec::new();
        for file in files {
            let path = file.get("path").and_then(Value::as_str).unwrap_or("");
            match file_community.get(path) {
                Some(&community) => groups.entry(community).or_default().push(file),
                None => ungrouped.push(file),
            }
        }

        let mut communities: Vec<Value> = groups
            .into_iter()
            .map(|(community, files)| {
                serde_json::json!({
                    "community": community,
                    "label": label_by_community
                        .get(&community)
                        .cloned()
                        .unwrap_or_else(|| format!("community {community}")),
                    "file_count": files.len(),
                    "files": files,
                })
            })
            .collect();
        communities
            .sort_by_key(|entry| entry.get("file_count").and_then(Value::as_u64).unwrap_or(0));
        communities.reverse();

        serde_json::json!({
            "project_root": project_root,
            "scope": scope,
            "group_by": "community",
            "total_files_in_scope": total,
            "offset": offset,
            "count": files.len(),
            "communities": communities,
            "ungrouped_files": ungrouped,
        })
    }

    #[cfg(not(feature = "community"))]
    fn community_grouped_response(
        &self,
        _guard: &crate::cli::leindex::LeIndex,
        files: &[Value],
        total: usize,
        project_root: &str,
        scope: &str,
        offset: usize,
    ) -> Value {
        serde_json::json!({
            "project_root": project_root,
            "scope": scope,
            "group_by": "community",
            "total_files_in_scope": total,
            "offset": offset,
            "count": files.len(),
            "communities": [],
            "ungrouped_files": files,
            "note": "community feature not compiled into this build",
        })
    }

    fn scope_paths(
        args: &Value,
        project_path: &Path,
        project_root: &Path,
    ) -> Result<(String, PathBuf, PathBuf), JsonRpcError> {
        // Allow legacy "path" param; map it into "scope" for resolution.
        let mut args_with_scope = args.clone();
        if let Some(obj) = args_with_scope.as_object_mut() {
            if !obj.contains_key("scope") {
                if let Some(path) = obj.get("path").cloned() {
                    obj.insert("scope".to_string(), path);
                }
            }
        }
        let scope = resolve_scope(&args_with_scope, project_path)?;
        let scope_str = scope.unwrap_or_else(|| {
            let mut root = project_root.to_string_lossy().to_string();
            if !root.ends_with(std::path::MAIN_SEPARATOR) {
                root.push(std::path::MAIN_SEPARATOR);
            }
            root
        });
        let scope_path = PathBuf::from(&scope_str);
        let scope_base =
            PathBuf::from(scope_str.trim_end_matches(['/', std::path::MAIN_SEPARATOR]));
        Ok((scope_str, scope_path, scope_base))
    }

    fn paginate_and_truncate(
        files: Vec<Value>,
        offset: usize,
        limit: Option<usize>,
        token_budget: usize,
    ) -> (usize, Vec<Value>) {
        let total_before_pagination = files.len();
        let files: Vec<Value> = files
            .into_iter()
            .skip(offset)
            .take(limit.unwrap_or(usize::MAX))
            .collect();

        let char_budget = token_budget * 4;
        let mut total_chars = 0;
        let mut truncated_files = Vec::new();
        for file in files {
            total_chars += file.to_string().len();
            if total_chars > char_budget {
                break;
            }
            truncated_files.push(file);
        }
        (total_before_pagination, truncated_files)
    }

    fn build_file_map(guard: &mut crate::cli::leindex::LeIndex) -> FileMap {
        // Collect source paths first to avoid borrow conflicts with file_stats()/pdg().
        let source_paths = guard.source_file_paths().unwrap_or_default();
        if guard.file_stats().is_none() {
            guard.build_file_stats_cache();
        }

        // file → (symbol_count, total_complexity, symbol_names, incoming_deps, outgoing_deps)
        let mut file_map: FileMap = source_paths
            .into_iter()
            .map(|path| (path.display().to_string(), (0, 0, Vec::new(), 0, 0)))
            .collect();

        // Overlay cached statistics, capping symbol_names to top 5.
        if let Some(cache) = guard.file_stats() {
            for (path, stats) in cache.iter() {
                let capped = stats.symbol_names.iter().take(5).cloned().collect();
                file_map.insert(
                    path.clone(),
                    (
                        stats.symbol_count,
                        stats.total_complexity,
                        capped,
                        stats.incoming_deps,
                        stats.outgoing_deps,
                    ),
                );
            }
        }

        file_map
    }

    fn files_in_scope(
        file_map: &FileMap,
        scope_str: &str,
        scope_path: &Path,
        scope_base: &Path,
        depth: usize,
        include_symbols: bool,
    ) -> Vec<Value> {
        file_map
            .iter()
            .filter(|(fp, _)| {
                fp.starts_with(scope_str) || fp.as_str() == scope_path.to_str().unwrap_or("")
            })
            .filter_map(|(fp, (count, complexity, syms, in_deg, out_deg))| {
                let path = std::path::Path::new(fp);
                let rel = path.strip_prefix(scope_base).ok()?;
                let directory_depth = rel
                    .parent()
                    .map(|parent| parent.components().count())
                    .unwrap_or(0);
                if directory_depth > depth {
                    return None;
                }

                let mut entry = serde_json::json!({
                    "path": fp,
                    "relative_path": rel.display().to_string(),
                    "symbol_count": count,
                    "total_complexity": complexity,
                    "incoming_dependencies": in_deg,
                    "outgoing_dependencies": out_deg
                });
                if include_symbols {
                    entry["top_symbols"] =
                        Value::Array(syms.iter().map(|s| Value::String(s.clone())).collect());
                }
                Some(entry)
            })
            .collect()
    }

    fn sort_and_rank_files(
        guard: &mut crate::cli::leindex::LeIndex,
        files: &mut [Value],
        sort_by: &str,
        focus: Option<&str>,
        include_symbols: bool,
    ) {
        match sort_by {
            "complexity" => files.sort_by(|a, b| {
                b["total_complexity"]
                    .as_u64()
                    .unwrap_or(0)
                    .cmp(&a["total_complexity"].as_u64().unwrap_or(0))
            }),
            "name" => files.sort_by(|a, b| {
                a["relative_path"]
                    .as_str()
                    .unwrap_or("")
                    .cmp(b["relative_path"].as_str().unwrap_or(""))
            }),
            "dependencies" => files.sort_by(|a, b| {
                let a_deg = a["incoming_dependencies"].as_u64().unwrap_or(0)
                    + a["outgoing_dependencies"].as_u64().unwrap_or(0);
                let b_deg = b["incoming_dependencies"].as_u64().unwrap_or(0)
                    + b["outgoing_dependencies"].as_u64().unwrap_or(0);
                b_deg.cmp(&a_deg)
            }),
            "size" => files.sort_by(|a, b| {
                b["symbol_count"]
                    .as_u64()
                    .unwrap_or(0)
                    .cmp(&a["symbol_count"].as_u64().unwrap_or(0))
            }),
            _ => {}
        }

        let Some(focus_text) = focus else {
            return;
        };
        let focus_emb = guard.generate_query_embedding(focus_text);
        let mut emb_cache = std::collections::HashMap::new();
        for entry in files.iter_mut() {
            let file_text = entry["top_symbols"]
                .as_array()
                .map(|arr| {
                    arr.iter()
                        .filter_map(|v| v.as_str())
                        .collect::<Vec<_>>()
                        .join(" ")
                })
                .unwrap_or_default();
            if file_text.is_empty() {
                entry["relevance_score"] = serde_json::json!(0.0);
                continue;
            }
            let file_emb = emb_cache
                .entry(file_text.clone())
                .or_insert_with(|| guard.generate_query_embedding(&file_text));
            let score = crate::search::vector::cosine_similarity(&focus_emb, file_emb);
            entry["relevance_score"] = serde_json::json!(score);
        }
        files.sort_by(|a, b| {
            let sa = a["relevance_score"].as_f64().unwrap_or(0.0);
            let sb = b["relevance_score"].as_f64().unwrap_or(0.0);
            sb.partial_cmp(&sa).unwrap_or(std::cmp::Ordering::Equal)
        });
        if !include_symbols {
            for entry in files.iter_mut() {
                entry.as_object_mut().map(|o| o.remove("top_symbols"));
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cli::mcp::helpers::test_registry_for;
    use tempfile::tempdir;

    #[tokio::test]
    async fn test_project_map_auto_indexes_empty_project() {
        // With auto-indexing, an empty project returns an empty file list (not an error)
        let dir = tempdir().unwrap();
        // Create a minimal source file so indexing has something to find
        let src = dir.path().join("main.rs");
        std::fs::write(&src, "fn main() {}\n").unwrap();
        let registry = test_registry_for(dir.path());
        let args = serde_json::json!({});
        let result = ProjectMapHandler.execute(&registry, args).await;
        assert!(result.is_ok(), "auto-indexing should succeed");
    }

    #[tokio::test]
    async fn test_project_map_cache_preserves_nested_and_symbol_less_files_with_directory_depth() {
        let dir = tempdir().unwrap();
        let nested_dir = dir.path().join("src").join("nested");
        std::fs::create_dir_all(&nested_dir).unwrap();
        std::fs::write(dir.path().join("main.rs"), "fn main() {}\n").unwrap();
        std::fs::write(dir.path().join("src").join("empty.rs"), "\n").unwrap();
        std::fs::write(nested_dir.join("mod.rs"), "pub fn helper() {}\n").unwrap();

        let registry = test_registry_for(dir.path());
        let args = serde_json::json!({
            "depth": 2,
            "sort_by": "name",
            "token_budget": 10_000
        });
        let result = ProjectMapHandler
            .execute(&registry, args.clone())
            .await
            .unwrap();
        let files = result["files"].as_array().unwrap();
        let relative_paths: Vec<String> = files
            .iter()
            .filter_map(|entry| entry["relative_path"].as_str())
            .map(|p| p.replace('\\', "/"))
            .collect();

        assert!(relative_paths.iter().any(|p| p == "main.rs"));
        assert!(relative_paths.iter().any(|p| p == "src/empty.rs"));
        assert!(relative_paths.iter().any(|p| p == "src/nested/mod.rs"));

        let cached_result = ProjectMapHandler.execute(&registry, args).await.unwrap();
        assert_eq!(cached_result["files"], result["files"]);
    }

    #[test]
    fn test_project_map_pagination_applies_offset_and_limit() {
        let files = vec![
            serde_json::json!({"relative_path": "first.rs"}),
            serde_json::json!({"relative_path": "second.rs"}),
            serde_json::json!({"relative_path": "third.rs"}),
        ];

        let (total, files) = ProjectMapHandler::paginate_and_truncate(files, 1, Some(1), 10_000);

        assert_eq!(total, 3);
        assert_eq!(
            files,
            vec![serde_json::json!({"relative_path": "second.rs"})]
        );
    }

    #[test]
    fn test_project_map_truncation_respects_token_budget() {
        let (_, files) = ProjectMapHandler::paginate_and_truncate(
            vec![serde_json::json!({"relative_path": "file.rs"})],
            0,
            None,
            1,
        );

        assert!(files.is_empty());
    }

    #[test]
    fn test_project_map_schema_has_pagination() {
        let schema = ProjectMapHandler.argument_schema();
        let props = schema.get("properties").unwrap();
        assert!(props.get("offset").is_some(), "should have 'offset'");
        assert!(props.get("limit").is_some(), "should have 'limit'");
        assert!(
            props.get("project_path").is_some(),
            "should have 'project_path'"
        );
    }
}