lean-ctx 3.9.0

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
//! Repo-stack-aware profile recommendation (#851).
//!
//! Powers `lean-ctx profile suggest`: scan the current repo for deterministic
//! signals (languages, source-file count, monorepo layout, build/CI markers,
//! configured LLM providers) and recommend a context profile plus a few key
//! settings (`history_mode`, output density, `effort`).
//!
//! Strictly **read-only and local-only**: it prints a suggestion and the exact
//! commands to apply it, and never writes config. All signals come from the
//! filesystem + local config/env, so the output is a deterministic function of
//! the repo and environment (no network, no telemetry).
//!
//! Reuses existing detectors rather than reinventing them:
//! [`language_for_ext`] for language classification and
//! [`crate::core::pathutil::has_multi_repo_children`] for the monorepo check.
//! The pure mapping ([`suggest`]) is separated from the I/O scan ([`analyze`])
//! so the heuristic is unit-tested without touching disk.

use std::collections::BTreeMap;
use std::path::Path;

use crate::core::config::Config;
use crate::core::language_capabilities::language_for_ext;

/// A repo is "large" past this many indexed source files (favors broad context).
const LARGE_REPO_FILES: usize = 2000;
/// A repo is "small" at or below this many source files (a focused default fits).
const SMALL_REPO_FILES: usize = 60;
/// This many distinct languages counts as polyglot (favors broad context).
const POLYGLOT_LANGS: usize = 4;
/// Hard cap on files visited during the scan, so `suggest` stays fast on huge trees.
const MAX_WALK_FILES: usize = 50_000;

/// Files counted for one language. Serialized for `--json`.
#[derive(Debug, Clone, serde::Serialize)]
pub struct LanguageCount {
    pub language: String,
    pub files: usize,
}

/// Deterministic, locally-detected signals about the repo + environment.
#[derive(Debug, Clone, serde::Serialize)]
pub struct RepoSignals {
    pub root: String,
    pub source_files: usize,
    pub languages: Vec<LanguageCount>,
    pub monorepo: bool,
    pub workspace_markers: Vec<String>,
    pub build_markers: Vec<String>,
    pub ci: bool,
    pub providers: Vec<String>,
    pub proxy_enabled: bool,
}

/// Key settings the suggestion recommends alongside the profile.
#[derive(Debug, Clone, serde::Serialize)]
pub struct RecommendedSettings {
    /// `proxy.history_mode` — `None` ⇒ leave the default untouched.
    pub history_mode: Option<String>,
    /// `output_density`.
    pub output_density: String,
    /// `proxy.effort` — `None` ⇒ leave off (opt-in; never inferred from a repo).
    pub effort: Option<String>,
}

/// A task-oriented profile the user can switch to, with the situation it fits.
#[derive(Debug, Clone, serde::Serialize)]
pub struct ProfileAlternative {
    pub profile: String,
    pub when: String,
}

/// The full recommendation: a primary profile, why, the settings, and
/// task-oriented alternatives.
#[derive(Debug, Clone, serde::Serialize)]
pub struct Suggestion {
    pub profile: String,
    pub rationale: Vec<String>,
    pub settings: RecommendedSettings,
    pub alternatives: Vec<ProfileAlternative>,
}

/// Scans `root` and collects [`RepoSignals`]. Respects `.gitignore` (so vendored
/// / build dirs don't skew the language mix) and is bounded by `MAX_WALK_FILES`.
#[must_use]
pub fn analyze(root: &str) -> RepoSignals {
    let root_path = Path::new(root);

    let mut lang_counts: BTreeMap<&'static str, usize> = BTreeMap::new();
    let mut source_files = 0usize;

    for entry in ignore::WalkBuilder::new(root_path)
        .standard_filters(true)
        .build()
        .flatten()
    {
        if source_files >= MAX_WALK_FILES {
            break;
        }
        if !entry.file_type().is_some_and(|t| t.is_file()) {
            continue;
        }
        let Some(ext) = entry.path().extension().and_then(|e| e.to_str()) else {
            continue;
        };
        if let Some(lang) = language_for_ext(ext) {
            *lang_counts.entry(lang.id_str()).or_insert(0) += 1;
            source_files += 1;
        }
    }

    let mut languages: Vec<LanguageCount> = lang_counts
        .into_iter()
        .map(|(language, files)| LanguageCount {
            language: language.to_string(),
            files,
        })
        .collect();
    // Stable order: most files first, ties broken by name.
    languages.sort_by(|a, b| {
        b.files
            .cmp(&a.files)
            .then_with(|| a.language.cmp(&b.language))
    });

    let workspace_markers = detect_workspace_markers(root_path);
    let monorepo =
        !workspace_markers.is_empty() || crate::core::pathutil::has_multi_repo_children(root_path);

    let build_markers = detect_build_markers(root_path);
    let ci = root_path.join(".github/workflows").is_dir()
        || root_path.join(".gitlab-ci.yml").is_file()
        || root_path.join(".circleci").is_dir()
        || root_path.join("azure-pipelines.yml").is_file();

    let cfg = Config::load();
    let providers = detect_providers(&cfg);
    let proxy_enabled = cfg.proxy_enabled.unwrap_or(false);

    RepoSignals {
        root: root.to_string(),
        source_files,
        languages,
        monorepo,
        workspace_markers,
        build_markers,
        ci,
        providers,
        proxy_enabled,
    }
}

/// Maps signals to a recommendation. Pure and deterministic (no I/O), so the
/// heuristic is fully unit-tested.
#[must_use]
pub fn suggest(signals: &RepoSignals) -> Suggestion {
    let polyglot = signals.languages.len() >= POLYGLOT_LANGS;
    let large = signals.source_files >= LARGE_REPO_FILES;
    let small = signals.source_files <= SMALL_REPO_FILES;

    let mut rationale = Vec::new();

    let profile = if signals.monorepo {
        rationale.push("monorepo layout → broad cross-package context".to_string());
        "exploration"
    } else if large {
        rationale.push(format!(
            "large repo ({} source files) → wider, map-first context",
            signals.source_files
        ));
        "exploration"
    } else if polyglot {
        rationale.push(format!(
            "polyglot ({} languages) → wider context to span stacks",
            signals.languages.len()
        ));
        "exploration"
    } else if small {
        rationale.push(format!(
            "small repo ({} source files) → a focused default is enough",
            signals.source_files
        ));
        "coder"
    } else {
        rationale.push(format!(
            "typical project size ({} source files) → balanced default",
            signals.source_files
        ));
        "coder"
    };

    let broad = signals.monorepo || large || polyglot;
    let output_density = if broad { "terse" } else { "normal" };
    if broad {
        rationale.push("dense output (terse) to fit the larger surface in budget".to_string());
    }

    let history_mode = if signals.proxy_enabled || !signals.providers.is_empty() {
        rationale
            .push("provider proxy active → cache-aware history (cache-stable pruning)".to_string());
        Some("cache-aware".to_string())
    } else {
        None
    };

    // `effort` is a cost/latency knob with no repo signal — never inferred here.
    let effort = None;

    let mut alternatives = Vec::new();
    if signals.ci {
        alternatives.push(ProfileAlternative {
            profile: "ci-debug".to_string(),
            when: "iterating on CI / shell failures".to_string(),
        });
    }
    alternatives.push(ProfileAlternative {
        profile: "hotfix".to_string(),
        when: "urgent one-file fix — minimal context".to_string(),
    });
    alternatives.push(ProfileAlternative {
        profile: "bugfix".to_string(),
        when: "debugging a specific issue".to_string(),
    });
    alternatives.push(ProfileAlternative {
        profile: "review".to_string(),
        when: "read-only code review".to_string(),
    });

    Suggestion {
        profile: profile.to_string(),
        rationale,
        settings: RecommendedSettings {
            history_mode,
            output_density: output_density.to_string(),
            effort,
        },
        alternatives,
    }
}

/// Monorepo / workspace marker files at the repo root (deterministic file probes).
fn detect_workspace_markers(root: &Path) -> Vec<String> {
    const MARKERS: &[&str] = &[
        "pnpm-workspace.yaml",
        "lerna.json",
        "nx.json",
        "turbo.json",
        "rush.json",
        "go.work",
    ];
    let mut found: Vec<String> = MARKERS
        .iter()
        .filter(|m| root.join(m).exists())
        .map(|m| (*m).to_string())
        .collect();
    // A Cargo workspace is expressed inside Cargo.toml rather than its own file.
    if std::fs::read_to_string(root.join("Cargo.toml"))
        .is_ok_and(|s| s.lines().any(|l| l.trim_start().starts_with("[workspace]")))
    {
        found.push("Cargo.toml [workspace]".to_string());
    }
    found
}

/// Build-tool markers at the repo root, de-duplicated by tool name.
fn detect_build_markers(root: &Path) -> Vec<String> {
    const MARKERS: &[(&str, &str)] = &[
        ("Cargo.toml", "cargo"),
        ("package.json", "npm"),
        ("go.mod", "go"),
        ("pyproject.toml", "python"),
        ("requirements.txt", "python"),
        ("setup.py", "python"),
        ("pom.xml", "maven"),
        ("build.gradle", "gradle"),
        ("Gemfile", "bundler"),
        ("composer.json", "composer"),
        ("Makefile", "make"),
        ("Dockerfile", "docker"),
    ];
    let mut found: Vec<String> = Vec::new();
    for (file, name) in MARKERS {
        if root.join(file).exists() && !found.iter().any(|f| f == name) {
            found.push((*name).to_string());
        }
    }
    found
}

/// LLM providers in use, from local config upstreams + environment API keys.
fn detect_providers(cfg: &Config) -> Vec<String> {
    let mut providers: Vec<String> = Vec::new();

    if env_set("ANTHROPIC_API_KEY") || env_set("ANTHROPIC_AUTH_TOKEN") {
        push_unique(&mut providers, "anthropic");
    }
    if env_set("OPENAI_API_KEY") {
        push_unique(&mut providers, "openai");
    }
    if env_set("GEMINI_API_KEY") || env_set("GOOGLE_API_KEY") {
        push_unique(&mut providers, "gemini");
    }
    if cfg.proxy.anthropic_upstream.is_some() {
        push_unique(&mut providers, "anthropic");
    }
    if cfg.proxy.openai_upstream.is_some() {
        push_unique(&mut providers, "openai");
    }
    if cfg.proxy.chatgpt_upstream.is_some() {
        push_unique(&mut providers, "chatgpt");
    }
    if cfg.proxy.gemini_upstream.is_some() {
        push_unique(&mut providers, "gemini");
    }

    providers.sort();
    providers
}

fn push_unique(v: &mut Vec<String>, name: &str) {
    if !v.iter().any(|p| p == name) {
        v.push(name.to_string());
    }
}

fn env_set(key: &str) -> bool {
    std::env::var(key).is_ok_and(|v| !v.trim().is_empty())
}

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

    fn signals(source_files: usize, langs: &[&str], monorepo: bool) -> RepoSignals {
        RepoSignals {
            root: "/tmp/x".to_string(),
            source_files,
            languages: langs
                .iter()
                .map(|l| LanguageCount {
                    language: (*l).to_string(),
                    files: 1,
                })
                .collect(),
            monorepo,
            workspace_markers: vec![],
            build_markers: vec![],
            ci: false,
            providers: vec![],
            proxy_enabled: false,
        }
    }

    #[test]
    fn monorepo_suggests_exploration() {
        let s = suggest(&signals(300, &["rust", "typescript"], true));
        assert_eq!(s.profile, "exploration");
        assert_eq!(s.settings.output_density, "terse");
    }

    #[test]
    fn large_repo_suggests_exploration() {
        let s = suggest(&signals(5000, &["rust"], false));
        assert_eq!(s.profile, "exploration");
        assert_eq!(s.settings.output_density, "terse");
    }

    #[test]
    fn polyglot_suggests_exploration() {
        let s = suggest(&signals(
            300,
            &["rust", "go", "python", "typescript"],
            false,
        ));
        assert_eq!(s.profile, "exploration");
    }

    #[test]
    fn small_repo_suggests_coder_normal_density() {
        let s = suggest(&signals(20, &["rust"], false));
        assert_eq!(s.profile, "coder");
        assert_eq!(s.settings.output_density, "normal");
    }

    #[test]
    fn typical_repo_suggests_coder() {
        let s = suggest(&signals(400, &["rust", "typescript"], false));
        assert_eq!(s.profile, "coder");
        assert_eq!(s.settings.output_density, "normal");
    }

    #[test]
    fn effort_is_never_inferred() {
        let s = suggest(&signals(5000, &["rust", "go", "python", "ts"], true));
        assert!(s.settings.effort.is_none());
    }

    #[test]
    fn history_mode_recommended_only_with_providers() {
        let mut s = signals(400, &["rust"], false);
        assert!(suggest(&s).settings.history_mode.is_none());
        s.providers = vec!["anthropic".to_string()];
        assert_eq!(
            suggest(&s).settings.history_mode.as_deref(),
            Some("cache-aware")
        );
    }

    #[test]
    fn ci_adds_ci_debug_alternative_first() {
        let mut s = signals(400, &["rust"], false);
        s.ci = true;
        let out = suggest(&s);
        assert_eq!(out.alternatives.first().unwrap().profile, "ci-debug");
    }

    #[test]
    fn suggestion_is_deterministic() {
        let s = signals(5000, &["rust", "go"], true);
        let a = suggest(&s);
        let b = suggest(&s);
        assert_eq!(a.profile, b.profile);
        assert_eq!(a.rationale, b.rationale);
    }
}