leindex 1.6.0

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
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;
use std::path::PathBuf;
use std::sync::Arc;

/// 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 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)"
                },
                "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 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;

        guard.ensure_pdg_loaded()
            .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();

        // 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(p) = obj.get("path").cloned() {
                    obj.insert("scope".to_string(), p);
                }
            }
        }
        let scope = resolve_scope(&args_with_scope, guard.project_path())?;
        let scope_str = scope.unwrap_or_else(|| {
            let mut s = project_root.to_string_lossy().to_string();
            if !s.ends_with(std::path::MAIN_SEPARATOR) {
                s.push(std::path::MAIN_SEPARATOR);
            }
            s
        });
        let scope_path = PathBuf::from(&scope_str);
        let scope_base = PathBuf::from(
            scope_str.trim_end_matches(['/', std::path::MAIN_SEPARATOR]),
        );

        // Use cached file stats if available, otherwise build from PDG
        // Collect source paths first to avoid borrow conflicts with file_stats()/pdg()
        let source_paths = guard.source_file_paths().unwrap_or_default();

        // file → (symbol_count, total_complexity, symbol_names, incoming_deps, outgoing_deps)
        let file_map: std::collections::HashMap<String, (usize, u32, Vec<String>, usize, usize)> =
            if let Some(cache) = guard.file_stats() {
                // Fast path: use cached statistics (includes pre-computed dep counts)
                let mut map: std::collections::HashMap<
                    String,
                    (usize, u32, Vec<String>, usize, usize),
                > = 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
                for (path, stats) in cache.iter() {
                    let capped: Vec<String> = stats.symbol_names.iter().take(5).cloned().collect();
                    map.insert(
                        path.clone(),
                        (
                            stats.symbol_count,
                            stats.total_complexity,
                            capped,
                            stats.incoming_deps,
                            stats.outgoing_deps,
                        ),
                    );
                }
                map
            } else {
                // Fallback: build from PDG via the same method used at index time
                guard.build_file_stats_cache();
                let mut map: std::collections::HashMap<
                    String,
                    (usize, u32, Vec<String>, usize, usize),
                > = source_paths
                    .into_iter()
                    .map(|path| (path.display().to_string(), (0, 0, Vec::new(), 0, 0)))
                    .collect();

                if let Some(cache) = guard.file_stats() {
                    for (path, stats) in cache.iter() {
                        let capped: Vec<String> =
                            stats.symbol_names.iter().take(5).cloned().collect();
                        map.insert(
                            path.clone(),
                            (
                                stats.symbol_count,
                                stats.total_complexity,
                                capped,
                                stats.incoming_deps,
                                stats.outgoing_deps,
                            ),
                        );
                    }
                }
                map
            }; // file → (node_count, total_complexity, symbol_names)

        // 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()))?;

        // Filter to scope path and respect depth.
        // Files must either be exactly in the scope directory or in a subdirectory.
        let mut files: Vec<Value> = file_map
            .iter()
            .filter(|(fp, _)| {
                // File is in scope if its path starts with scope_str (directory prefix)
                // or if the file IS the scope path (exact match for single-file scope)
                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 || focus.is_some() {
                    entry["top_symbols"] =
                        Value::Array(syms.iter().map(|s| Value::String(s.clone())).collect());
                }
                Some(entry)
            })
            .collect();

        // Sort
        match sort_by.as_str() {
            "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))
            }),
            _ => {}
        }

        // Semantic focus ranking: when focus is provided, re-rank files by
        // cosine similarity between the focus embedding and per-file symbol embeddings.
        if let Some(ref focus_text) = focus {
            let focus_emb = guard.generate_query_embedding(focus_text);
            // Cache file embeddings by symbol text to avoid recomputing
            // for files with identical symbol sets.
            let mut emb_cache: std::collections::HashMap<String, Vec<f32>> =
                std::collections::HashMap::new();
            for entry in &mut files {
                let syms = entry["top_symbols"].as_array();
                let file_text = syms
                    .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)
            });
            // Remove top_symbols from output if not requested
            if !include_symbols {
                for entry in &mut files {
                    entry.as_object_mut().map(|o| o.remove("top_symbols"));
                }
            }
        }

        // Apply pagination: offset + limit
        let total_before_pagination = files.len();
        let files: Vec<Value> = files.into_iter().skip(offset).collect();
        let files: Vec<Value> = if let Some(lim) = limit {
            files.into_iter().take(lim).collect()
        } else {
            files
        };

        // Truncate to token budget
        let char_budget = token_budget * 4;
        let mut total_chars = 0;
        let mut truncated_files: Vec<Value> = Vec::new();
        for f in files {
            let s = f.to_string();
            total_chars += s.len();
            if total_chars > char_budget {
                break;
            }
            truncated_files.push(f);
        }

        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,
        ))
    }
}

#[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_includes_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).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"));
    }

    #[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'"
        );
    }
}