lean-ctx 3.8.4

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
use crate::core::profiles;
use crate::core::tool_profiles::{self, ToolProfile};

pub fn cmd_profile(args: &[String]) {
    let action = args.first().map_or("list", String::as_str);

    match action {
        // Tool profile subcommands
        "tools" => cmd_tool_profile(&args[1..]),
        "minimal" | "min" | "standard" | "std" | "power" | "full" | "all" | "lean" | "lazy"
        | "reset" => {
            cmd_tool_profile_switch(action);
            println!("  \x1b[2mTip: the canonical command is `lean-ctx tools {action}`.\x1b[0m");
        }

        // Existing compression profile subcommands
        "list" | "ls" => cmd_profile_list(),
        "show" => {
            let name = args
                .get(1)
                .map_or_else(profiles::active_profile_name, Clone::clone);
            cmd_profile_show(&name);
        }
        "active" | "current" => cmd_profile_active(),
        "diff" => {
            if args.len() < 3 {
                eprintln!("Usage: lean-ctx profile diff <profile-a> <profile-b>");
                std::process::exit(1);
            }
            cmd_profile_diff(&args[1], &args[2]);
        }
        "create" => {
            if args.len() < 2 {
                eprintln!("Usage: lean-ctx profile create <name> [--from <base>] [--global]");
                std::process::exit(1);
            }
            let name = &args[1];
            let base = args
                .iter()
                .position(|a| a == "--from")
                .and_then(|i| args.get(i + 1))
                .map(String::as_str);
            let global = args.iter().any(|a| a == "--global");
            cmd_profile_create(name, base, global);
        }
        "set" => {
            if args.len() < 2 {
                eprintln!("Usage: lean-ctx profile set <name>");
                eprintln!("  Sets LEAN_CTX_PROFILE for the current shell.");
                std::process::exit(1);
            }
            cmd_profile_set(&args[1]);
        }
        _ => {
            if profiles::load_profile(action).is_some() {
                cmd_profile_show(action);
            } else {
                print_profile_help();
                std::process::exit(1);
            }
        }
    }
}

fn cmd_profile_list() {
    let list = profiles::list_profiles();
    let active = profiles::active_profile_name();

    let header = format!("  {:<16} {:<10} {}", "Name", "Source", "Description");
    let sep = format!("  {}", "\u{2500}".repeat(60));
    println!("Available profiles:\n");
    println!("{header}");
    println!("{sep}");

    for p in &list {
        let marker = if p.name == active { " *" } else { "  " };
        println!("{marker}{:<16} {:<10} {}", p.name, p.source, p.description);
    }

    println!("\n  Active: {active}");
    println!("  Set via: LEAN_CTX_PROFILE=<name> or lean-ctx profile set <name>");
}

fn cmd_profile_show(name: &str) {
    if let Some(profile) = profiles::load_profile(name) {
        println!("Profile: {name}\n");
        println!("{}", profiles::format_as_toml(&profile));
    } else {
        eprintln!("Profile '{name}' not found.");
        eprintln!("Run 'lean-ctx profile list' to see available profiles.");
        std::process::exit(1);
    }
}

fn cmd_profile_active() {
    let name = profiles::active_profile_name();
    let profile = profiles::active_profile();
    println!("Active profile: {name}\n");
    println!("{}", profiles::format_as_toml(&profile));
}

fn cmd_profile_diff(name_a: &str, name_b: &str) {
    let Some(a) = profiles::load_profile(name_a) else {
        eprintln!("Profile '{name_a}' not found.");
        std::process::exit(1);
    };
    let Some(b) = profiles::load_profile(name_b) else {
        eprintln!("Profile '{name_b}' not found.");
        std::process::exit(1);
    };

    println!("Profile diff: {name_a} vs {name_b}\n");

    let diffs = collect_diffs(&a, &b);
    if diffs.is_empty() {
        println!("  No differences.");
    } else {
        println!("  {:<32} {:<20} {:<20}", "Field", name_a, name_b);
        println!("  {}", "\u{2500}".repeat(72));
        for (field, val_a, val_b) in &diffs {
            println!("  {field:<32} {val_a:<20} {val_b:<20}");
        }
    }
}

fn collect_diffs(a: &profiles::Profile, b: &profiles::Profile) -> Vec<(String, String, String)> {
    let mut diffs = Vec::new();

    macro_rules! cmp {
        ($section:ident . $field:ident) => {
            let va = format!("{:?}", a.$section.$field);
            let vb = format!("{:?}", b.$section.$field);
            if va != vb {
                diffs.push((
                    format!("{}.{}", stringify!($section), stringify!($field)),
                    va,
                    vb,
                ));
            }
        };
    }

    cmp!(read.default_mode);
    cmp!(read.max_tokens_per_file);
    cmp!(read.prefer_cache);
    cmp!(compression.crp_mode);
    cmp!(compression.output_density);
    cmp!(compression.entropy_threshold);
    cmp!(translation.enabled);
    cmp!(translation.ruleset);
    cmp!(layout.enabled);
    cmp!(layout.min_lines);
    cmp!(budget.max_context_tokens);
    cmp!(budget.max_shell_invocations);
    cmp!(budget.max_cost_usd);
    cmp!(pipeline.intent);
    cmp!(pipeline.relevance);
    cmp!(pipeline.compression);
    cmp!(pipeline.translation);
    cmp!(autonomy.enabled);
    cmp!(autonomy.auto_preload);
    cmp!(autonomy.auto_dedup);
    cmp!(autonomy.auto_related);
    cmp!(autonomy.silent_preload);
    cmp!(autonomy.auto_prefetch);
    cmp!(autonomy.auto_response);
    cmp!(autonomy.dedup_threshold);
    cmp!(autonomy.prefetch_max_files);
    cmp!(autonomy.prefetch_budget_tokens);
    cmp!(autonomy.response_min_tokens);
    cmp!(autonomy.checkpoint_interval);

    diffs
}

fn cmd_profile_create(name: &str, base: Option<&str>, global: bool) {
    let base_profile = base
        .and_then(profiles::load_profile)
        .unwrap_or_else(profiles::active_profile);

    let mut new_profile = base_profile;
    new_profile.profile.name = name.to_string();
    new_profile.profile.inherits = base.map(String::from);
    new_profile.profile.description = String::new();

    let dir = if global {
        let Ok(data_dir) = crate::core::data_dir::lean_ctx_data_dir() else {
            eprintln!("Cannot determine global data directory.");
            std::process::exit(1);
        };
        data_dir.join("profiles")
    } else {
        std::env::current_dir()
            .unwrap_or_default()
            .join(".lean-ctx")
            .join("profiles")
    };

    if let Err(e) = std::fs::create_dir_all(&dir) {
        eprintln!("Cannot create directory {}: {e}", dir.display());
        std::process::exit(1);
    }

    let path = dir.join(format!("{name}.toml"));
    let toml_content = profiles::format_as_toml(&new_profile);

    if let Err(e) = std::fs::write(&path, &toml_content) {
        eprintln!("Error writing {}: {e}", path.display());
        std::process::exit(1);
    }

    println!("Created profile '{name}' at {}", path.display());
    if let Some(b) = base {
        println!("  Based on: {b}");
    }
    println!("\nEdit the file to customize, then activate with:");
    println!("  LEAN_CTX_PROFILE={name}");
}

fn cmd_profile_set(name: &str) {
    if profiles::load_profile(name).is_none() {
        eprintln!("Profile '{name}' not found. Available profiles:");
        for p in profiles::list_profiles() {
            eprintln!("  {}", p.name);
        }
        std::process::exit(1);
    }

    println!("To activate profile '{name}', run:\n");
    println!("  export LEAN_CTX_PROFILE={name}\n");
    println!(
        "Or add it to your shell config ({}).",
        crate::shell_hook::shell_rc_file()
    );
}

// ─── Tool Profile Commands ───────────────────────────────────────────────

fn cmd_tool_profile(args: &[String]) {
    let action = args.first().map_or("show", String::as_str);

    match action {
        "list" | "ls" => cmd_tool_profile_list(),
        "show" | "current" => cmd_tool_profile_show(),
        "minimal" | "min" | "standard" | "std" | "power" | "full" | "all" | "lean" | "lazy"
        | "reset" => {
            cmd_tool_profile_switch(action);
        }
        _ => {
            if ToolProfile::parse(action).is_some() {
                cmd_tool_profile_switch(action);
            } else {
                eprintln!("Unknown tool profile '{action}'.");
                eprintln!("Available: lean (default), minimal, standard, power");
                std::process::exit(1);
            }
        }
    }
}

fn cmd_tool_profile_show() {
    let cfg = crate::core::config::Config::load();
    let profile = cfg.tool_profile_effective();
    let registry_count = crate::server::registry::tool_count();
    let pinned = cfg.tool_profile.is_some()
        || std::env::var("LEAN_CTX_TOOL_PROFILE").is_ok()
        || !cfg.tools_enabled.is_empty();

    if !pinned {
        let lazy_count = crate::tool_defs::core_tool_names().len();
        println!("Tool Profile: lean (default)");
        println!("  Tools advertised: {lazy_count} (lazy core)");
        println!("  All {registry_count} registered tools stay callable via ctx_call.");
        println!("\n  Advertised tools:");
        for name in crate::tool_defs::core_tool_names() {
            println!("    {name}");
        }
        println!("\n  Switch with: lean-ctx tools <minimal|standard|power>");
        return;
    }

    let count_str = match &profile {
        ToolProfile::Power => format!("{registry_count}"),
        ToolProfile::Custom(list) => format!("{}", list.len()),
        other => format!("{}", other.tool_count()),
    };

    println!("Tool Profile: {}", profile.as_str());
    println!("  Tools exposed: {count_str}");
    println!("  Description:   {}", profile.description());

    if let Some(ref cfg_val) = cfg.tool_profile {
        println!("  Source:         config.toml (tool_profile = \"{cfg_val}\")");
    }
    if std::env::var("LEAN_CTX_TOOL_PROFILE").is_ok() {
        println!("  Source:         LEAN_CTX_TOOL_PROFILE env var (overrides config)");
    }

    if !matches!(profile, ToolProfile::Power) {
        println!("\n  Enabled tools:");
        let names = profile.tool_names();
        for name in &names {
            println!("    {name}");
        }
    }

    println!("\n  Switch with: lean-ctx tools <lean|minimal|standard|power>");
    if matches!(profile, ToolProfile::Power) {
        println!("  Tip: `lean-ctx tools lean` advertises only the lazy core (lowest overhead).");
    }
}

fn cmd_tool_profile_list() {
    let cfg = crate::core::config::Config::load();
    let active = cfg.tool_profile_effective();
    let registry_count = crate::server::registry::tool_count();
    let pinned = cfg.tool_profile.is_some()
        || std::env::var("LEAN_CTX_TOOL_PROFILE").is_ok()
        || !cfg.tools_enabled.is_empty();
    let active_name = if pinned { active.as_str() } else { "lean" };
    let lazy_count = crate::tool_defs::core_tool_names().len();

    println!("Tool Profiles:\n");
    println!("  {:<12} {:<8} Description", "Name", "Tools");
    println!("  {}", "\u{2500}".repeat(60));

    let lean_marker = if active_name == "lean" { "* " } else { "  " };
    println!(
        "{lean_marker}{:<12} {lazy_count:<8} Lazy core advertised, all tools via ctx_call (default)",
        "lean"
    );
    for info in tool_profiles::list_profiles() {
        let marker = if info.name == active_name { "* " } else { "  " };
        let count = if info.name == "power" {
            format!("{registry_count}")
        } else {
            info.tool_count.to_string()
        };
        println!(
            "{marker}{:<12} {:<8} {}",
            info.name, count, info.description
        );
    }

    println!("\n  Active: {active_name}");
    println!("  Switch: lean-ctx profile <name>");
    println!("  Env:    LEAN_CTX_TOOL_PROFILE=<name>");
}

fn cmd_tool_profile_switch(name: &str) {
    // "lean" is not a pinned profile — it removes the config key, restoring
    // the default: lazy core advertised (~13 schemas), everything reachable
    // through ctx_call (#575).
    if matches!(name, "lean" | "lazy" | "reset") {
        if let Err(e) = tool_profiles::clear_profile_in_config() {
            eprintln!("Error saving profile: {e}");
            std::process::exit(1);
        }
        let lazy_count = crate::tool_defs::core_tool_names().len();
        println!("Tool profile set to: lean (default)");
        println!("  Tools advertised: {lazy_count} (lazy core)");
        println!("  All other tools stay callable via ctx_call.");
        println!("\n  Restart your AI tool / IDE for changes to take effect.");
        return;
    }

    let Some(profile) = ToolProfile::parse(name) else {
        eprintln!("Unknown tool profile '{name}'.");
        eprintln!("Available: lean (default), minimal, standard, power");
        std::process::exit(1);
    };

    let canonical = profile.as_str();

    if let Err(e) = tool_profiles::set_profile_in_config(canonical) {
        eprintln!("Error saving profile: {e}");
        std::process::exit(1);
    }

    let registry_count = crate::server::registry::tool_count();
    let count_str = match &profile {
        ToolProfile::Power => format!("{registry_count}"),
        other => format!("{}", other.tool_count()),
    };

    println!("Tool profile set to: {canonical}");
    println!("  Tools exposed: {count_str}");
    println!("  Description:   {}", profile.description());

    if !matches!(profile, ToolProfile::Power) {
        println!("\n  Enabled tools:");
        for name in profile.tool_names() {
            println!("    {name}");
        }
    }

    println!("\n  Restart your AI tool / IDE for changes to take effect.");
}

fn print_profile_help() {
    eprintln!(
        "lean-ctx has two kinds of profiles — here is which command to use:

TOOL PROFILES — how many MCP tools your agent sees:
  lean-ctx tools                Show current tool profile
  lean-ctx tools lean           Lazy core advertised, all via ctx_call (default)
  lean-ctx tools minimal        6 essential tools
  lean-ctx tools standard       22 balanced tools
  lean-ctx tools power          All tools (highest context overhead)
  lean-ctx tools list           List tool profiles with counts

CONTEXT PROFILES — how lean-ctx compresses and reads (this command):
  lean-ctx profile list         List available context profiles
  lean-ctx profile show [name]  Show context profile details (default: active)
  lean-ctx profile active       Show the currently active context profile
  lean-ctx profile diff <a> <b> Compare two context profiles side by side
  lean-ctx profile create <name> [--from <base>] [--global]
  lean-ctx profile set <name>   Show how to activate a context profile"
    );
}