leindex 1.6.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
#![warn(missing_docs)]

//! lephase - 5-phase analysis workflow for LeIndexer.

/// Summary cache helpers.
pub mod cache;
/// Shared runtime context and incremental preparation pipeline.
pub mod context;
/// Optional markdown/text analysis utilities.
pub mod docs;
/// Output formatting modes and truncation helpers.
pub mod format;
/// Freshness detection and generation hashing.
pub mod freshness;
/// User-facing analysis options.
pub mod options;
/// Orchestration engine and loop state.
pub mod orchestrate;
/// PDG merge/build helper functions.
pub mod pdg_utils;
/// Phase 1 structural scan.
pub mod phase1;
/// Phase 2 dependency map.
pub mod phase2;
/// Phase 3 logic flow.
pub mod phase3;
/// Phase 4 critical path.
pub mod phase4;
/// Phase 5 optimization synthesis.
pub mod phase5;
/// Recommendation models.
pub mod recommendations;
/// Shared file/path utility helpers.
pub mod utils;

use anyhow::Result;
use cache::PhaseCache;
use context::PhaseExecutionContext;
use format::TokenFormatter;
use serde::{Deserialize, Serialize};

pub use format::FormatMode;
pub use options::{DocsMode, PhaseOptions};
pub use phase1::Phase1Summary;
pub use phase2::Phase2Summary;
pub use phase3::Phase3Summary;
pub use phase4::Phase4Summary;
pub use phase5::Phase5Summary;

/// Which phase(s) to execute.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum PhaseSelection {
    /// Run one specific phase 1..=5.
    Single(u8),
    /// Run all phases in order.
    All,
}

impl PhaseSelection {
    /// Validate a raw phase number.
    pub fn from_number(phase: u8) -> Option<Self> {
        if (1..=5).contains(&phase) {
            Some(Self::Single(phase))
        } else {
            None
        }
    }
}

/// Top-level phase-analysis report payload.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PhaseAnalysisReport {
    /// Project id.
    pub project_id: String,
    /// Freshness generation hash.
    pub generation: String,
    /// Executed phase list.
    pub executed_phases: Vec<u8>,
    /// Whether any summary cache hit occurred.
    pub cache_hit: bool,
    /// Changed file count.
    pub changed_files: usize,
    /// Deleted file count.
    pub deleted_files: usize,

    /// Phase 1 summary.
    pub phase1: Option<Phase1Summary>,
    /// Phase 2 summary.
    pub phase2: Option<Phase2Summary>,
    /// Phase 3 summary.
    pub phase3: Option<Phase3Summary>,
    /// Phase 4 summary.
    pub phase4: Option<Phase4Summary>,
    /// Phase 5 summary.
    pub phase5: Option<Phase5Summary>,

    /// Human-readable report text.
    pub formatted_output: String,
}

/// Run phase analysis for a project with incremental freshness and shared context.
pub fn run_phase_analysis(
    options: PhaseOptions,
    selection: PhaseSelection,
) -> Result<PhaseAnalysisReport> {
    let options = options.normalized();
    let context = PhaseExecutionContext::prepare(&options)?;
    let cache = PhaseCache::new(&context.root);

    let mut executed_phases = Vec::new();
    let mut cache_hit = false;

    let mut phase1_summary = None;
    let mut phase2_summary = None;
    let mut phase3_summary = None;
    let mut phase4_summary = None;
    let mut phase5_summary = None;

    if should_run(1, selection) {
        if let Some(cached) =
            cache.load::<Phase1Summary>(&context.project_id, &context.generation_hash, 1)?
        {
            phase1_summary = Some(cached.payload);
            cache_hit = true;
        } else {
            let value = phase1::run(&context);
            cache.save(&context.project_id, &context.generation_hash, 1, &value)?;
            phase1_summary = Some(value);
        }
        executed_phases.push(1);
    }

    if should_run(2, selection) {
        if let Some(cached) =
            cache.load::<Phase2Summary>(&context.project_id, &context.generation_hash, 2)?
        {
            phase2_summary = Some(cached.payload);
            cache_hit = true;
        } else {
            let value = phase2::run(&context);
            cache.save(&context.project_id, &context.generation_hash, 2, &value)?;
            phase2_summary = Some(value);
        }
        executed_phases.push(2);
    }

    if should_run(3, selection) {
        let phase3_key = options_hash_for_phase(3, &options);
        if let Some(cached) = cache.load_with_options::<Phase3Summary>(
            &context.project_id,
            &context.generation_hash,
            3,
            phase3_key.as_deref(),
        )? {
            phase3_summary = Some(cached.payload);
            cache_hit = true;
        } else {
            let value = phase3::run(&context, &options);
            cache.save_with_options(
                &context.project_id,
                &context.generation_hash,
                3,
                phase3_key.as_deref(),
                &value,
            )?;
            phase3_summary = Some(value);
        }
        executed_phases.push(3);
    }

    if should_run(4, selection) {
        let phase4_key = options_hash_for_phase(4, &options);
        if let Some(cached) = cache.load_with_options::<Phase4Summary>(
            &context.project_id,
            &context.generation_hash,
            4,
            phase4_key.as_deref(),
        )? {
            phase4_summary = Some(cached.payload);
            cache_hit = true;
        } else {
            let value = phase4::run(&context, &options);
            cache.save_with_options(
                &context.project_id,
                &context.generation_hash,
                4,
                phase4_key.as_deref(),
                &value,
            )?;
            phase4_summary = Some(value);
        }
        executed_phases.push(4);
    }

    if should_run(5, selection) {
        let p1 = phase1_summary
            .clone()
            .unwrap_or_else(|| phase1::run(&context));
        let p2 = phase2_summary
            .clone()
            .unwrap_or_else(|| phase2::run(&context));
        let p3 = phase3_summary
            .clone()
            .unwrap_or_else(|| phase3::run(&context, &options));
        let p4 = phase4_summary
            .clone()
            .unwrap_or_else(|| phase4::run(&context, &options));

        let phase5_key = options_hash_for_phase(5, &options);
        if let Some(cached) = cache.load_with_options::<Phase5Summary>(
            &context.project_id,
            &context.generation_hash,
            5,
            phase5_key.as_deref(),
        )? {
            phase5_summary = Some(cached.payload);
            cache_hit = true;
        } else {
            let value = phase5::run(&context, &p1, &p2, &p3, &p4);
            cache.save_with_options(
                &context.project_id,
                &context.generation_hash,
                5,
                phase5_key.as_deref(),
                &value,
            )?;
            phase5_summary = Some(value);
        }
        executed_phases.push(5);
    }

    let formatted_output = format_report(
        &context,
        &executed_phases,
        phase1_summary.as_ref(),
        phase2_summary.as_ref(),
        phase3_summary.as_ref(),
        phase4_summary.as_ref(),
        phase5_summary.as_ref(),
        options.max_output_chars,
    );

    Ok(PhaseAnalysisReport {
        project_id: context.project_id,
        generation: context.generation_hash,
        executed_phases,
        cache_hit,
        changed_files: context.changed_files.len(),
        deleted_files: context.deleted_files.len(),
        phase1: phase1_summary,
        phase2: phase2_summary,
        phase3: phase3_summary,
        phase4: phase4_summary,
        phase5: phase5_summary,
        formatted_output,
    })
}

fn should_run(phase: u8, selection: PhaseSelection) -> bool {
    match selection {
        PhaseSelection::Single(p) => p == phase,
        PhaseSelection::All => true,
    }
}

fn options_hash_for_phase(phase: u8, options: &PhaseOptions) -> Option<String> {
    let key = match phase {
        3 => format!(
            "phase3:top_n={}:max_focus_files={}",
            options.top_n, options.max_focus_files
        ),
        4 => format!("phase4:top_n={}", options.top_n),
        5 => format!(
            "phase5:top_n={}:max_focus_files={}",
            options.top_n, options.max_focus_files
        ),
        _ => return None,
    };

    Some(blake3::hash(key.as_bytes()).to_hex().to_string()[..8].to_string())
}

#[allow(clippy::too_many_arguments)]
fn format_report(
    context: &PhaseExecutionContext,
    executed_phases: &[u8],
    phase1: Option<&Phase1Summary>,
    phase2: Option<&Phase2Summary>,
    phase3: Option<&Phase3Summary>,
    phase4: Option<&Phase4Summary>,
    phase5: Option<&Phase5Summary>,
    max_chars: usize,
) -> String {
    let mut lines = Vec::new();
    lines.push(format!(
        "5-Phase Analysis :: project={} generation={} phases={:?}",
        context.project_id, context.generation_hash, executed_phases
    ));
    lines.push(format!(
        "freshness: changed={} deleted={} inventory={}",
        context.changed_files.len(),
        context.deleted_files.len(),
        context.file_inventory.len()
    ));

    if let Some(p1) = phase1 {
        let avg_completeness = if p1.parser_completeness.is_empty() {
            0.0
        } else {
            p1.parser_completeness
                .iter()
                .map(|entry| entry.score)
                .sum::<f32>()
                / p1.parser_completeness.len() as f32
        };

        lines.push(format!(
            "phase1: files={} parsed={} failures={} signatures={} parser_completeness_avg={:.2}",
            p1.total_files, p1.parsed_files, p1.parse_failures, p1.signatures, avg_completeness
        ));
    }

    if let Some(p2) = phase2 {
        lines.push(format!(
            "phase2: import_edges internal={} external={} unresolved_modules={}",
            p2.internal_import_edges, p2.external_import_edges, p2.unresolved_modules
        ));
    }

    if let Some(p3) = phase3 {
        lines.push(format!(
            "phase3: entry_points={} impacted_nodes={} focus_files={}",
            p3.entry_points.len(),
            p3.impacted_nodes,
            p3.focus_files.len()
        ));
    }

    if let Some(p4) = phase4 {
        lines.push(format!("phase4: hotspots={}", p4.hotspots.len()));
    }

    if let Some(p5) = phase5 {
        lines.push(format!(
            "phase5: recommendations={} public_symbol_hints={}",
            p5.recommendations.len(),
            p5.public_symbol_hints
        ));
    }

    if let Some(docs) = &context.docs_summary {
        lines.push(format!(
            "docs: files={} headings={} todos={}",
            docs.files_scanned, docs.heading_count, docs.todo_count
        ));
    }

    TokenFormatter::truncate(&lines.join("\n"), max_chars)
}

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

    #[test]
    fn phase_selection_validation_works() {
        assert_eq!(
            PhaseSelection::from_number(1),
            Some(PhaseSelection::Single(1))
        );
        assert_eq!(
            PhaseSelection::from_number(5),
            Some(PhaseSelection::Single(5))
        );
        assert_eq!(PhaseSelection::from_number(0), None);
        assert_eq!(PhaseSelection::from_number(6), None);
    }

    #[test]
    fn single_phase_report_contains_only_requested_phase() {
        let dir = tempdir().expect("tempdir");
        std::fs::create_dir_all(dir.path().join("src")).expect("mkdir");
        std::fs::write(dir.path().join("src/lib.rs"), "pub fn f()->i32{1}\n").expect("write");

        let report = run_phase_analysis(
            PhaseOptions {
                root: dir.path().to_path_buf(),
                ..PhaseOptions::default()
            },
            PhaseSelection::Single(2),
        )
        .expect("phase run");

        assert_eq!(report.executed_phases, vec![2]);
        assert!(report.phase2.is_some());
        assert!(report.phase1.is_none());
        assert!(report.phase3.is_none());
    }

    #[test]
    fn phase3_cache_key_changes_with_top_n() {
        let dir = tempdir().expect("tempdir");
        std::fs::create_dir_all(dir.path().join("src")).expect("mkdir");
        std::fs::write(
            dir.path().join("src/lib.rs"),
            "pub fn a(){}\npub fn b(x:i32){}\npub fn c(x:i32,y:i32){}\n",
        )
        .expect("write");

        let first = run_phase_analysis(
            PhaseOptions {
                root: dir.path().to_path_buf(),
                top_n: 1,
                ..PhaseOptions::default()
            },
            PhaseSelection::Single(3),
        )
        .expect("first run");
        assert!(!first.cache_hit);

        let second = run_phase_analysis(
            PhaseOptions {
                root: dir.path().to_path_buf(),
                top_n: 2,
                ..PhaseOptions::default()
            },
            PhaseSelection::Single(3),
        )
        .expect("second run");

        assert!(
            !second.cache_hit,
            "phase 3 cache must miss when top_n changes"
        );
    }
}