lean-ctx 3.9.8

Context Runtime for AI Agents with CCP. 71 MCP tools, 10 read modes, 95+ compression patterns, cross-session memory (CCP), persistent AI knowledge with temporal facts + contradiction detection, multi-agent context sharing, LITM-aware positioning, AAAK compact format, adaptive compression with Thompson Sampling bandits. Supports 24+ AI tools. Reduces LLM token consumption by up to 99%.
Documentation
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
use std::collections::HashMap;
use std::path::Path;
use std::time::{Duration, Instant, UNIX_EPOCH};

pub(crate) fn cmd_index(args: &[String]) {
    let project_root = super::common::detect_project_root(args);
    let root = Path::new(&project_root);

    // #735: install the per-run filter overlay (repeatable --include/--exclude
    // globs, --no-gitignore/--respect-gitignore) before any builder runs, so
    // BM25 + graph + semantic + watch share the declared corpus for this run.
    install_filter_overlay(args);

    let sub = find_subcommand(args);
    match sub {
        Some("status") => {
            let json_flag = args.iter().any(|a| a == "--json");
            if json_flag {
                println!(
                    "{}",
                    crate::core::index_orchestrator::status_json(&project_root)
                );
            } else {
                print_human_status(&project_root);
            }
        }
        Some("build") => {
            // #790: activate memory guardian for CLI builds so graph/BM25 abort
            // checks actually fire (previously only started in daemon mode).
            crate::core::memory_guard::start_guard(std::sync::Arc::new(|level| {
                tracing::warn!(
                    "[index build] memory pressure: {level:?} — background tasks will throttle"
                );
                if level >= crate::core::memory_guard::PressureLevel::Hard {
                    crate::core::content_cache::clear();
                }
                crate::core::memory_guard::force_purge();
            }));
            crate::core::index_orchestrator::ensure_all_background(&project_root);

            let started = std::time::Instant::now();
            let timeout = Duration::from_mins(5);
            eprint!("building indexes (graph + BM25)");
            loop {
                std::thread::sleep(Duration::from_millis(500));
                let status = crate::core::index_orchestrator::status_json(&project_root);
                if !status.contains("\"building\"") {
                    break;
                }
                eprint!(".");
                if started.elapsed() > timeout {
                    eprintln!(" timeout (background build continues)");
                    return;
                }
            }
            eprintln!(" done");

            // Surface the BM25 build outcome so the operator knows the index
            // state (issue #249).
            let summary = crate::core::index_orchestrator::bm25_summary(&project_root);
            if let Some(note) = summary.note {
                eprintln!("  BM25: {note}");
            }
            if let Some(err) = summary.last_error {
                eprintln!("  BM25 error: {err}");
            }
        }
        Some("build-full") => {
            // #790: activate memory guardian for full builds too.
            crate::core::memory_guard::start_guard(std::sync::Arc::new(|level| {
                tracing::warn!("[index build-full] memory pressure: {level:?}");
                if level >= crate::core::memory_guard::PressureLevel::Hard {
                    crate::core::content_cache::clear();
                }
                crate::core::memory_guard::force_purge();
            }));
            crate::core::interrupt::install_ctrlc_handler();
            let bm25_path = crate::core::bm25_index::BM25Index::index_file_path(root);
            let _ = std::fs::remove_file(&bm25_path);
            // #696 C4: purge the property graph (graph.db + wal/shm + meta) and
            // any retired JSON/call-graph artifacts so the rebuild starts clean.
            crate::core::graph_index::purge_index(&project_root);
            // Purge old embeddings so the full rebuild starts from scratch and
            // does not re-use stale vectors from a different model or project
            // state.
            let vectors_dir = crate::core::index_namespace::vectors_dir(root);
            let embedding_bin = vectors_dir.join("embeddings.bin");
            if embedding_bin.exists() {
                let _ = std::fs::remove_file(&embedding_bin);
            }
            let embedding_json = vectors_dir.join("embeddings.json");
            if embedding_json.exists() {
                let _ = std::fs::remove_file(&embedding_json);
            }
            crate::core::index_orchestrator::ensure_all_background(&project_root);

            let started = std::time::Instant::now();
            let timeout = Duration::from_mins(5);
            eprintln!("rebuilding indexes (graph + BM25)");
            loop {
                std::thread::sleep(Duration::from_millis(500));
                let status = crate::core::index_orchestrator::status_json(&project_root);
                if !status.contains("\"building\"") {
                    break;
                }
                eprint!(".");
                if started.elapsed() > timeout {
                    eprintln!(" timeout (background build continues)");
                    return;
                }
            }
            eprintln!(" done");

            // Surface the BM25 build outcome (chunk count + persisted size, or the
            // "too large to persist" remedy) so the operator is never left guessing.
            let summary = crate::core::index_orchestrator::bm25_summary(&project_root);
            if let Some(note) = summary.note {
                eprintln!("  BM25: {note}");
            }
            if let Some(err) = summary.last_error {
                eprintln!("  BM25 error: {err}");
            }

            // The property graph was already mirrored from the graph_index
            // extractor inside ensure_all_background above (#682.2).
            eprintln!("property graph mirrored from graph_index during index build");

            // Build semantic (dense embedding) index on top of the fresh BM25.
            eprintln!("building semantic (dense embedding) index ...");
            crate::core::index_orchestrator::build_semantic(&project_root);
            let sem = crate::core::index_orchestrator::semantic_summary(&project_root);
            match sem.state {
                "ready" => eprintln!("  semantic index ready"),
                "failed" => eprintln!(
                    "  semantic index failed: {}",
                    sem.last_error.unwrap_or_else(|| String::from("unknown"))
                ),
                _ => {
                    if let Some(note) = sem.note {
                        eprintln!("  semantic: {note}");
                    }
                }
            }

            // build-full is an explicit "make everything fresh". Drop the in-process
            // graph cache and flush the running daemon's read cache too, so ctx_read
            // map/signatures don't keep serving pre-rebuild output from the daemon's
            // long-lived SessionCache in another process (#420).
            crate::core::graph_cache::invalidate(Some(&project_root));
            if crate::daemon_client::notify_cache_clear() {
                eprintln!("  Daemon read cache flushed — ctx_read re-derives on next read.");
            }
        }
        Some("build-graph") => {
            // #682.1: mirror the proven graph_index extractor into the property
            // graph (complete symbols + file_catalog).
            match crate::core::graph_provider::build_property_graph(&project_root) {
                Ok(()) => match crate::core::property_graph::CodeGraph::open(&project_root) {
                    Ok(g) => println!(
                        "property graph built from graph_index: {} nodes, {} edges, {} files",
                        g.node_count().unwrap_or(0),
                        g.edge_count().unwrap_or(0),
                        g.file_catalog_count().unwrap_or(0),
                    ),
                    Err(_) => println!("property graph built from graph_index"),
                },
                Err(e) => eprintln!("property graph build failed: {e}"),
            }
        }
        Some("build-semantic") => {
            // #790: activate memory guardian for semantic builds too.
            crate::core::memory_guard::start_guard(std::sync::Arc::new(|level| {
                tracing::warn!("[index build-semantic] memory pressure: {level:?}");
                if level >= crate::core::memory_guard::PressureLevel::Hard {
                    crate::core::content_cache::clear();
                }
                crate::core::memory_guard::force_purge();
            }));
            crate::core::interrupt::install_ctrlc_handler();
            // Build the dense embedding index on top of BM25.  If BM25 is not yet
            // built, build graph + BM25 first, then build semantic.
            let disk = crate::core::index_orchestrator::disk_status(&project_root);
            if !disk.bm25_index.exists {
                eprintln!("BM25 index not found — building graph + BM25 first ...");
                crate::core::index_orchestrator::ensure_all_background(&project_root);
                let started = std::time::Instant::now();
                let timeout = Duration::from_mins(5);
                loop {
                    std::thread::sleep(Duration::from_millis(500));
                    let status = crate::core::index_orchestrator::status_json(&project_root);
                    if !status.contains("\"building\"") {
                        break;
                    }
                    eprint!(".");
                    if started.elapsed() > timeout {
                        eprintln!(" timeout");
                        return;
                    }
                }
                eprintln!(" done");
            }

            eprintln!("building semantic (dense embedding) index ...");
            crate::core::index_orchestrator::build_semantic(&project_root);
            let sem = crate::core::index_orchestrator::semantic_summary(&project_root);
            match sem.state {
                "ready" => eprintln!("semantic index ready"),
                "failed" => eprintln!(
                    "semantic index failed: {}",
                    sem.last_error.unwrap_or_else(|| String::from("unknown"))
                ),
                _ => {
                    eprintln!("semantic index not available");
                    if let Some(ref note) = sem.note {
                        eprintln!("  reason: {note}");
                    }
                }
            }
        }
        Some("watch") => run_watcher(root),
        _ => {
            eprintln!(
                "Usage: lean-ctx index <status|build|build-full|build-graph|build-semantic|watch> [--root <path>]\n\
                 Filter flags (#735, apply to this run; persist via [index] config):\n\
                   --exclude <glob>       drop matching files from the corpus (repeatable)\n\
                   --include <glob>       corpus = matching files only (repeatable)\n\
                   --no-gitignore         index .gitignore'd files too\n\
                   --respect-gitignore    honor .gitignore (default)\n\
                 Examples:\n\
                   lean-ctx index status\n\
                   lean-ctx index build              (graph + BM25 indexes)\n\
                   lean-ctx index build-full         (force rebuild all indexes)\n\
                   lean-ctx index build-full --exclude \"**/*.csv\" --exclude \"**/*.jsonl\"\n\
                   lean-ctx index build-semantic --include \"**/*.{{java,kt,ts}}\"\n\
                   lean-ctx index build-graph        (SQLite property graph for impact analysis)\n\
                   lean-ctx index build-semantic     (dense embedding index, builds BM25 first if needed)\n\
                   lean-ctx index watch"
            );
        }
    }
}

/// First non-flag token, skipping the values of value-taking flags — so
/// `index build --exclude "**/*.csv"` resolves the subcommand `build`, not the
/// glob (#735).
fn find_subcommand(args: &[String]) -> Option<&str> {
    const VALUE_FLAGS: [&str; 4] = ["--exclude", "--include", "--root", "--project-root"];
    let mut it = args.iter();
    while let Some(a) = it.next() {
        if VALUE_FLAGS.contains(&a.as_str()) {
            let _ = it.next();
            continue;
        }
        if a.starts_with("--") {
            continue;
        }
        return Some(a.as_str());
    }
    None
}

/// Parse the #735 filter flags and install the per-run overlay. No-op when no
/// filter flag is present, so config-only runs take the config path.
fn install_filter_overlay(args: &[String]) {
    let (include, exclude, respect_gitignore) = parse_filter_flags(args);
    if !include.is_empty() || !exclude.is_empty() || respect_gitignore.is_some() {
        crate::core::index_filter::set_cli_overlay(include, exclude, respect_gitignore);
    }
}

/// Pure #735 flag parser: `--exclude` / `--include` are repeatable and accept
/// both `--flag value` and `--flag=value` forms; the last of `--no-gitignore`
/// / `--respect-gitignore` wins.
fn parse_filter_flags(args: &[String]) -> (Vec<String>, Vec<String>, Option<bool>) {
    let mut include: Vec<String> = Vec::new();
    let mut exclude: Vec<String> = Vec::new();
    let mut respect_gitignore: Option<bool> = None;

    let mut it = args.iter();
    while let Some(a) = it.next() {
        match a.as_str() {
            "--exclude" => {
                if let Some(v) = it.next() {
                    exclude.push(v.clone());
                } else {
                    eprintln!("--exclude requires a glob argument");
                }
            }
            "--include" => {
                if let Some(v) = it.next() {
                    include.push(v.clone());
                } else {
                    eprintln!("--include requires a glob argument");
                }
            }
            "--no-gitignore" => respect_gitignore = Some(false),
            "--respect-gitignore" => respect_gitignore = Some(true),
            other => {
                if let Some(v) = other.strip_prefix("--exclude=") {
                    exclude.push(v.to_string());
                } else if let Some(v) = other.strip_prefix("--include=") {
                    include.push(v.to_string());
                }
            }
        }
    }

    (include, exclude, respect_gitignore)
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct FileState {
    mtime_ms: u64,
    size_bytes: u64,
}

fn run_watcher(project_root: &Path) {
    let hash = crate::core::index_namespace::namespace_hash(project_root);
    let lock_name = format!("index-watch-{}", &hash[..8.min(hash.len())]);
    let Some(lock) = crate::core::startup_guard::try_acquire_lock(
        &lock_name,
        Duration::from_millis(800),
        Duration::from_secs(8),
    ) else {
        eprintln!("index watcher already running");
        return;
    };

    let mut last = snapshot_code_files(project_root);
    let mut pending: Option<Instant> = None;
    let poll = Duration::from_millis(700);
    let debounce = Duration::from_millis(900);

    loop {
        lock.touch();
        std::thread::sleep(poll);

        let cur = snapshot_code_files(project_root);
        if cur != last {
            last = cur;
            pending = Some(Instant::now());
            continue;
        }

        if let Some(t) = pending
            && t.elapsed() >= debounce
        {
            crate::core::index_orchestrator::ensure_all_background(
                project_root.to_string_lossy().as_ref(),
            );
            pending = None;
        }
    }
}

fn snapshot_code_files(project_root: &Path) -> HashMap<String, FileState> {
    // #735: the watcher observes the same declared corpus as the builders it
    // triggers — a change to an excluded file must not cause rebuild churn.
    let filter = crate::core::index_filter::IndexFileFilter::effective();
    let walker = ignore::WalkBuilder::new(project_root)
        .hidden(true)
        .git_ignore(filter.respect_gitignore)
        .git_global(filter.respect_gitignore)
        .git_exclude(filter.respect_gitignore)
        .require_git(false)
        .filter_entry(crate::core::walk_filter::keep_entry)
        .build();

    let mut out: HashMap<String, FileState> = HashMap::new();
    for entry in walker.flatten() {
        let path = entry.path();
        if !path.is_file() {
            continue;
        }
        if path.components().any(|c| c.as_os_str() == ".git") {
            continue;
        }
        if !crate::core::ingestion::is_ingestible(path) {
            continue;
        }
        let Ok(meta) = path.metadata() else {
            continue;
        };
        let Ok(modified) = meta.modified() else {
            continue;
        };
        let Some(mtime_ms) = modified
            .duration_since(UNIX_EPOCH)
            .ok()
            .map(|d| d.as_millis() as u64)
        else {
            continue;
        };

        let rel = path
            .strip_prefix(project_root)
            .unwrap_or(path)
            .to_string_lossy()
            .to_string();
        if rel.is_empty() {
            continue;
        }
        if filter.is_excluded(&rel.replace('\\', "/")) {
            continue;
        }

        out.insert(
            rel,
            FileState {
                mtime_ms,
                size_bytes: meta.len(),
            },
        );
    }
    out
}

fn print_human_status(project_root: &str) {
    let disk = crate::core::index_orchestrator::disk_status(project_root);

    println!("  Project:        {project_root}");
    println!(
        "  Graph Index:    {}",
        format_disk_line(&disk.graph_index, "files")
    );
    println!(
        "  BM25 Index:     {}",
        format_disk_line(&disk.bm25_index, "chunks")
    );
    println!(
        "  Code Graph:     {}",
        format_disk_line(&disk.code_graph, "nodes")
    );
    println!(
        "  Semantic Index: {}",
        format_disk_line(&disk.semantic_index, "vectors")
    );
    // #735: surface the active corpus filter so a filtered index is always
    // recognizable. Absent for the default (unfiltered) config, keeping the
    // default output byte-identical.
    if let Some(summary) = crate::core::index_filter::IndexFileFilter::effective().summary() {
        println!("  Index Filters:  {summary}");
    }
}

fn format_disk_line(ds: &crate::core::index_orchestrator::DiskStatus, count_label: &str) -> String {
    if !ds.exists {
        return "not built".to_string();
    }
    let mut parts = vec!["ready".to_string()];
    if let Some(count) = ds.file_count {
        parts.push(format!("{count} {count_label}"));
    }
    if let Some(bytes) = ds.size_bytes {
        parts.push(format_bytes(bytes));
    }
    if let Some(ref t) = ds.modified_at {
        parts.push(format!("built {t}"));
    }
    format!("({})", parts.join(", "))
}

fn format_bytes(bytes: u64) -> String {
    if bytes >= 1_073_741_824 {
        format!("{:.1} GB", bytes as f64 / 1_073_741_824.0)
    } else if bytes >= 1_048_576 {
        format!("{:.1} MB", bytes as f64 / 1_048_576.0)
    } else if bytes >= 1024 {
        format!("{:.1} KB", bytes as f64 / 1024.0)
    } else {
        format!("{bytes} B")
    }
}

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

    fn args(list: &[&str]) -> Vec<String> {
        list.iter().map(ToString::to_string).collect()
    }

    #[test]
    fn subcommand_found_before_flags() {
        assert_eq!(
            find_subcommand(&args(&["build", "--root", "/x"])),
            Some("build")
        );
        assert_eq!(find_subcommand(&args(&["status"])), Some("status"));
    }

    #[test]
    fn subcommand_skips_filter_flag_values() {
        // The glob value must never be mistaken for the subcommand (#735).
        assert_eq!(
            find_subcommand(&args(&["--exclude", "**/*.csv", "build-full"])),
            Some("build-full")
        );
        assert_eq!(
            find_subcommand(&args(&["build-semantic", "--include", "**/*.java"])),
            Some("build-semantic")
        );
        assert_eq!(
            find_subcommand(&args(&["--root", "/x", "--no-gitignore", "watch"])),
            Some("watch")
        );
    }

    #[test]
    fn subcommand_none_for_flag_only_args() {
        assert_eq!(find_subcommand(&args(&["--exclude", "**/*.csv"])), None);
        assert_eq!(find_subcommand(&[]), None);
    }

    #[test]
    fn filter_flags_repeatable_and_both_forms() {
        let (include, exclude, gitignore) = parse_filter_flags(&args(&[
            "build-full",
            "--exclude",
            "**/*.csv",
            "--exclude=**/*.jsonl",
            "--include",
            "**/*.rs",
            "--include=**/*.ts",
        ]));
        assert_eq!(exclude, vec!["**/*.csv", "**/*.jsonl"]);
        assert_eq!(include, vec!["**/*.rs", "**/*.ts"]);
        assert_eq!(gitignore, None);
    }

    #[test]
    fn gitignore_flags_last_one_wins() {
        let (_, _, gitignore) =
            parse_filter_flags(&args(&["build", "--no-gitignore", "--respect-gitignore"]));
        assert_eq!(gitignore, Some(true));
        let (_, _, gitignore) = parse_filter_flags(&args(&["build", "--no-gitignore"]));
        assert_eq!(gitignore, Some(false));
    }

    #[test]
    fn no_filter_flags_yields_empty_overlay_inputs() {
        let (include, exclude, gitignore) = parse_filter_flags(&args(&["build", "--root", "/x"]));
        assert!(include.is_empty());
        assert!(exclude.is_empty());
        assert_eq!(gitignore, None);
    }
}