leindex 2.0.3

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
use super::edit_cache::{EditCacheEntry, GLOBAL_EDIT_CACHE};
use super::edit_preview_handler::EditPreviewHandler;
use super::helpers::{
    apply_changes_in_memory, extract_bool, extract_string, parse_edit_changes,
    validate_file_within_project, wrap_with_meta,
};
use super::protocol::JsonRpcError;
use crate::cli::registry::{ProjectHandle, ProjectRegistry};
use crate::edit::{ResolvedEditChange, atomic_write_with_expected_async};
use crate::validation::validation_to_json;
use serde_json::Value;
use std::sync::Arc;

type EditImpact = (Vec<String>, std::collections::HashSet<String>, Vec<String>);

type PreparedEdit = (String, String, Vec<crate::edit::EditChange>);

fn apply_request_args(
    args: &Value,
) -> Result<(String, Option<String>, Option<String>), JsonRpcError> {
    Ok((
        extract_string(args, "file_path")?,
        args.get("project_path")
            .and_then(Value::as_str)
            .map(str::to_owned),
        args.get("preview_token")
            .and_then(Value::as_str)
            .map(str::to_owned),
    ))
}

fn cached_edit(entry: Option<EditCacheEntry>, token: &str) -> Result<PreparedEdit, JsonRpcError> {
    let entry = entry.ok_or_else(|| {
        JsonRpcError::invalid_params(
            "No cached preview found for this file — request a new preview",
        )
    })?;
    if entry.preview_token != token {
        return Err(JsonRpcError::invalid_params(
            "preview token mismatch — request a new preview",
        ));
    }
    Ok((entry.original_text, entry.modified_text, entry.changes))
}

async fn apply_atomic(
    path: &std::path::Path,
    original: &str,
    modified: &str,
) -> Result<bool, JsonRpcError> {
    atomic_write_with_expected_async(
        path.to_path_buf(),
        modified.as_bytes().to_vec(),
        original.as_bytes().to_vec(),
    )
    .await
    .map_err(|error| {
        JsonRpcError::internal_error(format!("Failed to write '{}': {}", path.display(), error))
    })
}

async fn ensure_write_succeeded(
    success: bool,
    storage_path: &std::path::Path,
    canonical_path: &std::path::Path,
) -> Result<(), JsonRpcError> {
    if success {
        return Ok(());
    }
    GLOBAL_EDIT_CACHE.clear(storage_path, canonical_path).await;
    Err(JsonRpcError::invalid_params(
        "Edit rejected: file content changed on disk since preview was generated. \
        Please call LeIndex [Edit Preview] again (tool: leindex.edit-preview).",
    ))
}

/// Wrap a preview payload in the explicit dry-run envelope. Delegating raw made
/// the apply renderer read the preview-shaped payload (no `success` field) as
/// "Edit apply failed" with no detail — a dry-run that reports failure without a
/// reason is worse than none (N-09).
fn dry_run_envelope(preview: Value) -> Value {
    let mut envelope = serde_json::json!({
        "success": true,
        "dry_run": true,
        "changes_applied": 0,
        "message": "Dry run: no changes written. See `preview` for the diff and validation.",
    });
    if let (Some(obj), Some(preview_obj)) = (envelope.as_object_mut(), preview.as_object()) {
        for (key, value) in preview_obj {
            if key == "content" || key == "isError" {
                continue;
            }
            obj.insert(key.clone(), value.clone());
        }
    }
    envelope
}

/// Resolve the request's file path and project handle in one step.
async fn resolve_request_target(
    registry: &Arc<ProjectRegistry>,
    args: &Value,
) -> Result<(String, Option<String>, ProjectHandle), JsonRpcError> {
    let (file_path, project_path_arg, provided_token) = apply_request_args(args)?;
    let handle = registry.get_or_create(project_path_arg.as_deref()).await?;
    Ok((file_path, provided_token, handle))
}

/// Best-effort PDG load. Applying a plain text edit must never be blocked by a
/// degraded/unavailable index: the atomic write + expected-content guard is the
/// safety net, and impact/validation simply report as unavailable when no PDG
/// can be loaded.
async fn load_pdg_best_effort(handle: &ProjectHandle) -> bool {
    let mut guard = handle.write().await;
    match guard.ensure_pdg_loaded_graph_only() {
        Ok(()) => guard.pdg().is_some(),
        Err(error) => {
            tracing::warn!(
                project = %guard.project_path().display(),
                "PDG unavailable for edit-apply; continuing without PDG impact analysis: {error}"
            );
            false
        }
    }
}

/// Resolve the target file against the project root and read the storage path.
async fn resolve_paths(
    handle: &ProjectHandle,
    file_path: &str,
) -> Result<(std::path::PathBuf, std::path::PathBuf), JsonRpcError> {
    let guard = handle.read().await;
    let canonical = validate_file_within_project(file_path, guard.project_path())?;
    Ok((canonical, guard.storage_path().to_path_buf()))
}

/// Wrap a response in the live project metadata.
async fn wrap_with_current_meta(handle: &ProjectHandle, response: Value) -> Value {
    let guard = handle.read().await;
    wrap_with_meta(response, &guard)
}

/// Flag a response that was produced without a loaded PDG: the edit is durable,
/// but impact analysis and validation were unavailable for it.
fn annotate_degraded_pdg(response: &mut Value) {
    let Some(object) = response.as_object_mut() else {
        return;
    };
    object.insert("pdg_status".to_string(), serde_json::json!("not_loaded"));
    object.insert(
        "warning".to_string(),
        serde_json::json!(
            "Index unavailable — edit applied with file-level safety only \
            (no PDG impact analysis or validation). Reindex (LeIndex [Index] with \
            force_reindex=true) to restore full editing safeguards."
        ),
    );
}

/// Index maintenance AFTER the response value exists. One-shot CLI processes run
/// it inline (the process exits once the response is printed, so a spawned task
/// would be killed mid-write); the long-running MCP server/daemon spawns it so
/// the caller's latency is exactly the edit, never the reindex.
async fn refresh_index_after_edit(registry: &Arc<ProjectRegistry>, handle: &ProjectHandle) {
    use crate::cli::leindex::indexing::watcher_delta::NotHydratedError;

    // A not-hydrated resident can never take the incremental path — the
    // guard is a persistent condition, not a transient one — so escalate to
    // a full index once instead of leaving the project permanently stale.
    async fn refresh(guard: &mut crate::cli::leindex::LeIndex) -> Result<(), anyhow::Error> {
        if let Err(e) = guard.incremental_reindex_from_watcher() {
            if !e.is::<NotHydratedError>() {
                return Err(e);
            }
            tracing::warn!("Index refresh needs a full index ({e}); escalating");
            guard.index_project(true)?;
        }
        Ok(())
    }

    if registry.is_one_shot() {
        let mut guard = handle.write().await;
        if let Err(e) = refresh(&mut guard).await {
            tracing::warn!("Failed to refresh index after edit-apply: {}", e);
        }
        return;
    }
    let registry = Arc::clone(registry);
    let handle = Arc::clone(handle);
    tokio::spawn(async move {
        // Serialized behind the project write lock: concurrent
        // edit-applies queue their refreshes instead of racing.
        let mut guard = handle.write().await;
        let root = guard.project_path().to_path_buf();
        if let Err(e) = refresh(&mut guard).await {
            tracing::warn!(
                project = %root.display(),
                "Background refresh after edit-apply failed: {e}"
            );
        }
        drop(guard);
        registry.invalidate_stale_cache(&root).await;
        tracing::debug!(
            project = %root.display(),
            "Background refresh after edit-apply complete"
        );
    });
}

fn validate_edit(
    validator: Option<crate::validation::LogicValidator>,
    path: &std::path::Path,
    original: &str,
    modified: &str,
) -> Option<Value> {
    let validator = validator?;
    let change =
        ResolvedEditChange::new(path.to_path_buf(), original.to_owned(), modified.to_owned());
    match validator.validate_changes(&[change]) {
        Ok(result) => Some(validation_to_json(&result)),
        Err(error) => {
            tracing::warn!("Validation check failed: {}", error);
            None
        }
    }
}

fn edit_impact(
    pdg: Option<&crate::graph::pdg::ProgramDependenceGraph>,
    changes: &[crate::edit::EditChange],
    path: &std::path::Path,
) -> EditImpact {
    let mut nodes = Vec::new();
    let mut files = std::collections::HashSet::new();
    files.insert(path.to_string_lossy().to_string());
    let mut breaking = Vec::new();
    let Some(pdg) = pdg else {
        return (nodes, files, breaking);
    };
    for change in changes {
        let crate::edit::EditChange::RenameSymbol { old_name, .. } = change else {
            continue;
        };
        let node_id = pdg
            .find_by_symbol(old_name)
            .or_else(|| pdg.find_by_name(old_name))
            .or_else(|| pdg.find_by_name_in_file(old_name, Some(&path.to_string_lossy())));
        let Some(node_id) = node_id else {
            continue;
        };
        for dependency in pdg.forward_impact(
            node_id,
            &crate::graph::pdg::TraversalConfig::for_impact_analysis(),
        ) {
            if let Some(node) = pdg.get_node(dependency) {
                nodes.push(node.name.clone());
                files.insert(node.file_path.to_string());
            }
        }
        let callers = pdg.backward_impact(
            node_id,
            &crate::graph::pdg::TraversalConfig::for_impact_analysis(),
        );
        if !callers.is_empty() {
            breaking.push(format!(
                "Renaming '{}' may break {} caller(s)",
                old_name,
                callers.len()
            ));
        }
    }
    (nodes, files, breaking)
}

fn edit_region(original: &str, modified: &str) -> String {
    let modified_lines: Vec<&str> = modified.lines().collect();
    let original_lines: Vec<&str> = original.lines().collect();
    let shared_len = original_lines.len().min(modified_lines.len());
    let first_diff = original_lines
        .iter()
        .zip(modified_lines.iter())
        .position(|(old, new)| old != new)
        .unwrap_or(shared_len);
    let start = first_diff.saturating_sub(5);
    let end = (first_diff + 10).min(modified_lines.len());
    modified_lines[start..end]
        .iter()
        .enumerate()
        .map(|(index, line)| format!("{}: {}", start + index + 1, line))
        .collect::<Vec<_>>()
        .join("\n")
}

fn edit_response(
    path: &std::path::Path,
    changes_applied: usize,
    region: String,
    impact: EditImpact,
    validation: Option<Value>,
) -> Value {
    let (nodes, files, breaking) = impact;
    let mut affected_files: Vec<_> = files.into_iter().collect();
    affected_files.sort();
    let mut response = serde_json::json!({
        "success": true,
        "changes_applied": changes_applied,
        "file_path": path.to_string_lossy(),
        "edit_region": region,
        "affected_symbols": nodes,
        "affected_files": affected_files,
        "breaking_changes": breaking,
    });
    if let (Some(validation), Some(object)) = (validation, response.as_object_mut()) {
        object.insert("validation".to_string(), validation);
    }
    response
}

/// Handler for LeIndex [edit_apply — atomic code modifications.
#[derive(Clone)]
pub struct EditApplyHandler;

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

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

    pub fn description(&self) -> &str {
        "PRIMARY file editor — use instead of edit_file. Simple mode: provide file_path + \
old_text + new_text for exact replacement. Advanced mode: use changes[] array for \
multiple or byte-offset edits. Supports dry_run=true for preview."
    }

    pub fn argument_schema(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "file_path": {
                    "type": "string",
                    "description": "Absolute or project-relative path. Relative paths resolve against the project root."
                },
                "old_text": {
                    "type": "string",
                    "description": "Simple mode: text to find and replace (exact match)"
                },
                "old_str": {
                    "type": "string",
                    "description": "Alias for old_text (compatibility with edit_file)"
                },
                "new_text": {
                    "type": "string",
                    "description": "Simple mode: replacement text"
                },
                "new_str": {
                    "type": "string",
                    "description": "Alias for new_text (compatibility with edit_file)"
                },
                "project_path": {
                    "type": "string",
                    "description": "Project directory (auto-indexes on first use; omit to use current project)"
                },
                "changes": {
                    "type": "array",
                    "description": "Advanced mode: list of changes to apply. Each has type (replace_text/rename_symbol) and type-specific fields.",
                    "items": { "type": "object" }
                },
                "dry_run": {
                    "type": "boolean",
                    "description": "If true, return preview without modifying files (default: false). \
        Also accepts compatibility strings: 'true'/'false', '1'/'0', 'yes'/'no'.",
                    "default": false
                },
                "preview_token": {
                    "type": "string",
                    "description": "The token returned by a previous LeIndex [Edit Preview] (tool: leindex.edit-preview) call. Required if using cached preview."
                }
            },
            "required": ["file_path"]
        })
    }

    pub async fn execute(
        &self,
        registry: &Arc<ProjectRegistry>,
        args: Value,
    ) -> Result<Value, JsonRpcError> {
        let dry_run = extract_bool(&args, "dry_run", false);

        if dry_run {
            // Delegate to preview, but wrap the preview payload in an explicit
            // dry-run envelope (see `dry_run_envelope`).
            let preview = EditPreviewHandler.execute(registry, args).await?;
            return Ok(dry_run_envelope(preview));
        }

        let (file_path, provided_token, handle) = resolve_request_target(registry, &args).await?;

        // 0. Best-effort PDG load: applying a plain text edit must never be
        // blocked by a degraded/unavailable index.
        let pdg_loaded = load_pdg_best_effort(&handle).await;

        // 1. Resolve path and check cache (avoid awaiting while holding lock)
        let (canonical_path, storage_path) = resolve_paths(&handle, &file_path).await?;

        let cached_entry = GLOBAL_EDIT_CACHE.get(&storage_path, &canonical_path).await;

        let (original, modified, changes) = self
            .get_edit_content(
                provided_token,
                cached_entry,
                &canonical_path,
                &file_path,
                &args,
            )
            .await?;

        // If no changes, nothing to do
        if modified == original {
            GLOBAL_EDIT_CACHE
                .clear(&storage_path, &canonical_path)
                .await;
            let response = serde_json::json!({
                "success": true,
                "changes_applied": 0,
                "message": "No changes to apply (content identical)"
            });
            return Ok(wrap_with_current_meta(&handle, response).await);
        }

        let validation_json = {
            let guard = handle.read().await;
            validate_edit(
                guard.create_validator(),
                &canonical_path,
                &original,
                &modified,
            )
        };

        let success = apply_atomic(&canonical_path, &original, &modified).await?;

        ensure_write_succeeded(success, &storage_path, &canonical_path).await?;

        // 4. Clear cache after successful apply
        GLOBAL_EDIT_CACHE
            .clear(&storage_path, &canonical_path)
            .await;

        // 5. Invalidate the registry's staleness cache so the next
        // read tool re-runs `is_stale_fast` instead of reusing a
        // pre-write `false` cached result. The watcher (when enabled)
        // does this on its own reindex path; this explicit call
        // covers the watcher-disabled default mode where the
        // 30-second negative-cache TTL would otherwise silently
        // mask the edit.
        let project_root = {
            let guard = handle.read().await;
            guard.project_path().to_path_buf()
        };
        registry.invalidate_stale_cache(&project_root).await;

        // 6. Build the response NOW — the edit is durable and every field
        // is already computed (impact runs against the pre-edit PDG loaded
        // above; validation ran before the write). The MCP caller must
        // receive this the moment it exists: the incremental reindex used
        // to run inline here under the project write lock, holding the
        // response for seconds-to-minutes (and queuing behind any other
        // lock holder) until MCP clients timed out while the file had in
        // fact been edited.
        let impact = {
            let guard = handle.read().await;
            edit_impact(guard.pdg(), &changes, &canonical_path)
        };

        let mut response = edit_response(
            &canonical_path,
            changes.len(),
            edit_region(&original, &modified),
            impact,
            validation_json,
        );
        if !pdg_loaded {
            annotate_degraded_pdg(&mut response);
        }

        let response = wrap_with_current_meta(&handle, response).await;

        // 7. Index maintenance AFTER the response value exists: the caller
        // receives the response now, and the refresh never rides on its
        // latency (see `refresh_index_after_edit`).
        refresh_index_after_edit(registry, &handle).await;

        Ok(response)
    }

    fn get_changes_from_args(&self, args: &Value) -> Result<Value, JsonRpcError> {
        if let Some(changes) = args.get("changes").cloned() {
            Ok(changes)
        } else {
            let old_text = args
                .get("old_text")
                .or_else(|| args.get("old_str"))
                .and_then(|v| v.as_str());
            let new_text = args
                .get("new_text")
                .or_else(|| args.get("new_str"))
                .and_then(|v| v.as_str());
            match (old_text, new_text) {
                (Some(old), Some(new)) => Ok(serde_json::json!([{
                    "type": "replace_text",
                    "old_text": old,
                    "new_text": new
                }])),
                _ => Err(JsonRpcError::invalid_params(
                    "Provide either 'changes' array or 'old_text'+'new_text' for simple replacement",
                )),
            }
        }
    }

    async fn get_edit_content(
        &self,
        provided_token: Option<String>,
        cached_entry: Option<EditCacheEntry>,
        canonical_path: &std::path::Path,
        file_path: &str,
        args: &Value,
    ) -> Result<PreparedEdit, JsonRpcError> {
        if let Some(provided_token) = provided_token {
            cached_edit(cached_entry, &provided_token)
        } else {
            let original = tokio::fs::read_to_string(canonical_path)
                .await
                .map_err(|e| {
                    JsonRpcError::invalid_params(format!("Cannot read file '{}': {}", file_path, e))
                })?;

            let changes_val = self.get_changes_from_args(args)?;
            let changes = parse_edit_changes(&changes_val, Some(&original))?;
            let modified = apply_changes_in_memory(&original, &changes)?;
            Ok((original, modified, changes))
        }
    }
}