lean-ctx 3.9.16

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
use super::builtins::{builtin_coder, builtin_profile, builtin_profiles};
use super::types::{
    BudgetConfig, CompressionConfig, DegradationConfig, LayoutConfig, OutputHints, PipelineConfig,
    Profile, ProfileAutonomy, ProfileMeta, ReadConfig, RoutingConfig, TranslationConfig,
};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::RwLock;

// ── Loading ────────────────────────────────────────────────

fn profiles_dir_global() -> Option<PathBuf> {
    crate::core::data_dir::lean_ctx_data_dir()
        .ok()
        .map(|d| d.join("profiles"))
}

fn profiles_dir_project() -> Option<PathBuf> {
    let mut current = std::env::current_dir().ok()?;
    for _ in 0..12 {
        let candidate = current.join(".lean-ctx").join("profiles");
        if candidate.is_dir() {
            return Some(candidate);
        }
        if !current.pop() {
            break;
        }
    }
    None
}

/// Loads a profile by name with full resolution:
/// 1. Project-local `.lean-ctx/profiles/<name>.toml`
/// 2. Global `~/.lean-ctx/profiles/<name>.toml`
/// 3. Built-in defaults
///
/// Applies inheritance chain (max depth 5 to prevent cycles).
pub(crate) fn load_profile(name: &str) -> Option<Profile> {
    load_profile_recursive(name, 0)
}

fn load_profile_recursive(name: &str, depth: usize) -> Option<Profile> {
    if depth > 5 {
        return None;
    }

    let mut profile = load_profile_from_disk(name).or_else(|| builtin_profile(name))?;
    profile.profile.name = name.to_string();

    if let Some(ref parent_name) = profile.profile.inherits.clone()
        && let Some(parent) = load_profile_recursive(parent_name, depth + 1)
    {
        profile = merge_profiles(parent, profile);
    }

    Some(profile)
}

fn load_profile_from_disk(name: &str) -> Option<Profile> {
    let filename = format!("{name}.toml");

    if let Some(project_dir) = profiles_dir_project() {
        let path = project_dir.join(&filename);
        if let Some(p) = try_load_toml(&path) {
            return Some(p);
        }
    }

    if let Some(global_dir) = profiles_dir_global() {
        let path = global_dir.join(&filename);
        if let Some(p) = try_load_toml(&path) {
            return Some(p);
        }
    }

    None
}

fn try_load_toml(path: &Path) -> Option<Profile> {
    let content = std::fs::read_to_string(path).ok()?;
    toml::from_str(&content).ok()
}

/// Merges parent into child: child values take precedence,
/// parent provides defaults for unspecified fields.
///
/// ALL sections are merged field-by-field using `Option::or()`.
/// A child profile only needs to set the fields it wants to override.
pub(super) fn merge_profiles(parent: Profile, child: Profile) -> Profile {
    let read = ReadConfig {
        default_mode: child.read.default_mode.or(parent.read.default_mode),
        max_tokens_per_file: child
            .read
            .max_tokens_per_file
            .or(parent.read.max_tokens_per_file),
        prefer_cache: child.read.prefer_cache.or(parent.read.prefer_cache),
    };
    let compression = CompressionConfig {
        crp_mode: child.compression.crp_mode.or(parent.compression.crp_mode),
        output_density: child
            .compression
            .output_density
            .or(parent.compression.output_density),
        entropy_threshold: child
            .compression
            .entropy_threshold
            .or(parent.compression.entropy_threshold),
        terse_mode: child
            .compression
            .terse_mode
            .or(parent.compression.terse_mode),
        adaptive: child.compression.adaptive.or(parent.compression.adaptive),
    };
    let translation = TranslationConfig {
        enabled: child.translation.enabled.or(parent.translation.enabled),
        ruleset: child.translation.ruleset.or(parent.translation.ruleset),
    };
    let layout = LayoutConfig {
        enabled: child.layout.enabled.or(parent.layout.enabled),
        min_lines: child.layout.min_lines.or(parent.layout.min_lines),
    };
    let memory = crate::core::memory_policy::MemoryPolicyOverrides {
        knowledge: crate::core::memory_policy::KnowledgePolicyOverrides {
            max_facts: child
                .memory
                .knowledge
                .max_facts
                .or(parent.memory.knowledge.max_facts),
            max_patterns: child
                .memory
                .knowledge
                .max_patterns
                .or(parent.memory.knowledge.max_patterns),
            max_history: child
                .memory
                .knowledge
                .max_history
                .or(parent.memory.knowledge.max_history),
            contradiction_threshold: child
                .memory
                .knowledge
                .contradiction_threshold
                .or(parent.memory.knowledge.contradiction_threshold),
            recall_facts_limit: child
                .memory
                .knowledge
                .recall_facts_limit
                .or(parent.memory.knowledge.recall_facts_limit),
            rooms_limit: child
                .memory
                .knowledge
                .rooms_limit
                .or(parent.memory.knowledge.rooms_limit),
            timeline_limit: child
                .memory
                .knowledge
                .timeline_limit
                .or(parent.memory.knowledge.timeline_limit),
            relations_limit: child
                .memory
                .knowledge
                .relations_limit
                .or(parent.memory.knowledge.relations_limit),
        },
        lifecycle: crate::core::memory_policy::LifecyclePolicyOverrides {
            decay_rate: child
                .memory
                .lifecycle
                .decay_rate
                .or(parent.memory.lifecycle.decay_rate),
            low_confidence_threshold: child
                .memory
                .lifecycle
                .low_confidence_threshold
                .or(parent.memory.lifecycle.low_confidence_threshold),
            stale_days: child
                .memory
                .lifecycle
                .stale_days
                .or(parent.memory.lifecycle.stale_days),
            similarity_threshold: child
                .memory
                .lifecycle
                .similarity_threshold
                .or(parent.memory.lifecycle.similarity_threshold),
            forgetting_model: child
                .memory
                .lifecycle
                .forgetting_model
                .clone()
                .or_else(|| parent.memory.lifecycle.forgetting_model.clone()),
            base_stability_days: child
                .memory
                .lifecycle
                .base_stability_days
                .or(parent.memory.lifecycle.base_stability_days),
            archetype_aware_decay: child
                .memory
                .lifecycle
                .archetype_aware_decay
                .or(parent.memory.lifecycle.archetype_aware_decay),
        },
    };
    let verification = crate::core::output_verification::VerificationConfig {
        enabled: child.verification.enabled.or(parent.verification.enabled),
        mode: child.verification.mode.or(parent.verification.mode),
        strict_mode: child
            .verification
            .strict_mode
            .or(parent.verification.strict_mode),
        check_paths: child
            .verification
            .check_paths
            .or(parent.verification.check_paths),
        check_identifiers: child
            .verification
            .check_identifiers
            .or(parent.verification.check_identifiers),
        check_line_numbers: child
            .verification
            .check_line_numbers
            .or(parent.verification.check_line_numbers),
        check_structure: child
            .verification
            .check_structure
            .or(parent.verification.check_structure),
    };
    let budget = BudgetConfig {
        max_context_tokens: child
            .budget
            .max_context_tokens
            .or(parent.budget.max_context_tokens),
        max_shell_invocations: child
            .budget
            .max_shell_invocations
            .or(parent.budget.max_shell_invocations),
        max_cost_usd: child.budget.max_cost_usd.or(parent.budget.max_cost_usd),
    };
    let pipeline = PipelineConfig {
        intent: child.pipeline.intent.or(parent.pipeline.intent),
        relevance: child.pipeline.relevance.or(parent.pipeline.relevance),
        compression: child.pipeline.compression.or(parent.pipeline.compression),
        translation: child.pipeline.translation.or(parent.pipeline.translation),
    };
    let routing = RoutingConfig {
        max_model_tier: child
            .routing
            .max_model_tier
            .or(parent.routing.max_model_tier),
        degrade_under_pressure: child
            .routing
            .degrade_under_pressure
            .or(parent.routing.degrade_under_pressure),
    };
    let degradation = DegradationConfig {
        enforce: child.degradation.enforce.or(parent.degradation.enforce),
        throttle_ms: child
            .degradation
            .throttle_ms
            .or(parent.degradation.throttle_ms),
    };
    let autonomy = ProfileAutonomy {
        enabled: child.autonomy.enabled.or(parent.autonomy.enabled),
        auto_preload: child.autonomy.auto_preload.or(parent.autonomy.auto_preload),
        auto_dedup: child.autonomy.auto_dedup.or(parent.autonomy.auto_dedup),
        auto_related: child.autonomy.auto_related.or(parent.autonomy.auto_related),
        silent_preload: child
            .autonomy
            .silent_preload
            .or(parent.autonomy.silent_preload),
        auto_prefetch: child
            .autonomy
            .auto_prefetch
            .or(parent.autonomy.auto_prefetch),
        auto_response: child
            .autonomy
            .auto_response
            .or(parent.autonomy.auto_response),
        dedup_threshold: child
            .autonomy
            .dedup_threshold
            .or(parent.autonomy.dedup_threshold),
        prefetch_max_files: child
            .autonomy
            .prefetch_max_files
            .or(parent.autonomy.prefetch_max_files),
        prefetch_budget_tokens: child
            .autonomy
            .prefetch_budget_tokens
            .or(parent.autonomy.prefetch_budget_tokens),
        response_min_tokens: child
            .autonomy
            .response_min_tokens
            .or(parent.autonomy.response_min_tokens),
        checkpoint_interval: child
            .autonomy
            .checkpoint_interval
            .or(parent.autonomy.checkpoint_interval),
    };
    let output_hints = OutputHints {
        compressed_hint: child
            .output_hints
            .compressed_hint
            .or(parent.output_hints.compressed_hint),
        archive_hint: child
            .output_hints
            .archive_hint
            .or(parent.output_hints.archive_hint),
        verify_footer: child
            .output_hints
            .verify_footer
            .or(parent.output_hints.verify_footer),
        related_hint: child
            .output_hints
            .related_hint
            .or(parent.output_hints.related_hint),
        semantic_hint: child
            .output_hints
            .semantic_hint
            .or(parent.output_hints.semantic_hint),
        elicitation_hint: child
            .output_hints
            .elicitation_hint
            .or(parent.output_hints.elicitation_hint),
        checkpoint_in_output: child
            .output_hints
            .checkpoint_in_output
            .or(parent.output_hints.checkpoint_in_output),
        graph_context_block: child
            .output_hints
            .graph_context_block
            .or(parent.output_hints.graph_context_block),
        efficiency_hint: child
            .output_hints
            .efficiency_hint
            .or(parent.output_hints.efficiency_hint),
        cross_source_hint: child
            .output_hints
            .cross_source_hint
            .or(parent.output_hints.cross_source_hint),
        proactive_context: child
            .output_hints
            .proactive_context
            .or(parent.output_hints.proactive_context),
    };
    Profile {
        profile: ProfileMeta {
            name: child.profile.name,
            inherits: child.profile.inherits,
            description: if child.profile.description.is_empty() {
                parent.profile.description
            } else {
                child.profile.description
            },
        },
        read,
        compression,
        translation,
        layout,
        memory,
        verification,
        budget,
        pipeline,
        routing,
        degradation,
        autonomy,
        output_hints,
    }
}

/// Reads the `profile` key directly from `config.toml` without going through
/// `Config::load()`. This avoids a reentrancy deadlock: `Config::load()` →
/// `find_project_root()` (OnceLock) → `SessionState::load_latest()` →
/// `normalize_loaded_session()` → `active_profile()` → here → `Config::load()`.
fn profile_name_from_config_file() -> Option<String> {
    let path = crate::core::config::Config::path()?;
    let content = std::fs::read_to_string(path).ok()?;
    let table: toml::Table = toml::from_str(&content).ok()?;
    table
        .get("profile")?
        .as_str()
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .map(String::from)
}

/// Process-wide active-profile override set by [`set_active_profile`].
///
/// Takes precedence over `LEAN_CTX_PROFILE`. Storing the runtime selection in an
/// in-process cell (rather than mutating the environment) keeps profile
/// switching thread-safe inside the multi-threaded MCP server, where
/// `set_active_profile` may run on a blocking-pool worker while other workers
/// resolve the active profile concurrently.
static ACTIVE_PROFILE_OVERRIDE: RwLock<Option<String>> = RwLock::new(None);

/// Returns the currently active profile name.
///
/// Resolution order: in-process override (see [`set_active_profile`]) →
/// `LEAN_CTX_PROFILE` env var → config.toml `profile` field → "coder".
pub(crate) fn active_profile_name() -> String {
    if let Some(name) = ACTIVE_PROFILE_OVERRIDE
        .read()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
        .clone()
    {
        return name;
    }
    if let Ok(v) = std::env::var("LEAN_CTX_PROFILE") {
        let v = v.trim().to_string();
        if !v.is_empty() {
            return v;
        }
    }
    if let Some(name) = profile_name_from_config_file() {
        return name;
    }
    "coder".to_string()
}

/// Loads the currently active profile.
pub(crate) fn active_profile() -> Profile {
    let name = active_profile_name();
    if let Some(p) = load_profile(&name) {
        p
    } else {
        if name != "coder" {
            tracing::warn!(
                "Profile '{name}' not found (no built-in or disk file). \
                 Falling back to 'coder'. Create it with: lean-ctx profile create {name}"
            );
        }
        builtin_coder()
    }
}

/// Sets the active profile for the current process.
///
/// Records the selection in a thread-safe in-process override (see
/// [`active_profile_name`]) and returns the resolved profile after applying
/// inheritance.
pub(crate) fn set_active_profile(name: &str) -> Result<Profile, String> {
    let name = name.trim();
    if name.is_empty() {
        return Err("profile name is empty".to_string());
    }
    let prev = active_profile_name();
    let profile = load_profile(name).ok_or_else(|| format!("profile '{name}' not found"))?;
    *ACTIVE_PROFILE_OVERRIDE
        .write()
        .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(name.to_string());
    if prev != name {
        crate::core::events::emit_profile_changed(&prev, name);
    }
    Ok(profile)
}

/// Lists all available profile names (built-in + on-disk).
pub(crate) fn list_profiles() -> Vec<ProfileInfo> {
    let mut profiles: HashMap<String, ProfileInfo> = HashMap::new();

    for (name, p) in builtin_profiles() {
        profiles.insert(
            name.clone(),
            ProfileInfo {
                name,
                description: p.profile.description,
                source: ProfileSource::Builtin,
            },
        );
    }

    for (source, dir) in [
        (ProfileSource::Global, profiles_dir_global()),
        (ProfileSource::Project, profiles_dir_project()),
    ] {
        if let Some(dir) = dir
            && let Ok(entries) = std::fs::read_dir(&dir)
        {
            for entry in entries.flatten() {
                let path = entry.path();
                if path.extension().and_then(|e| e.to_str()) == Some("toml")
                    && let Some(stem) = path.file_stem().and_then(|s| s.to_str())
                {
                    let name = stem.to_string();
                    let desc = try_load_toml(&path)
                        .map(|p| p.profile.description)
                        .unwrap_or_default();
                    profiles.insert(
                        name.clone(),
                        ProfileInfo {
                            name,
                            description: desc,
                            source,
                        },
                    );
                }
            }
        }
    }

    let mut result: Vec<ProfileInfo> = profiles.into_values().collect();
    result.sort_by_key(|p| p.name.clone());
    result
}

/// Information about an available profile.
#[derive(Debug, Clone)]
pub(crate) struct ProfileInfo {
    pub name: String,
    pub description: String,
    pub source: ProfileSource,
}

/// Where a profile was loaded from.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ProfileSource {
    Builtin,
    Global,
    Project,
}

impl std::fmt::Display for ProfileSource {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Builtin => write!(f, "built-in"),
            Self::Global => write!(f, "global"),
            Self::Project => write!(f, "project"),
        }
    }
}

/// Formats a profile as TOML for display or file creation.
pub(crate) fn format_as_toml(profile: &Profile) -> String {
    toml::to_string_pretty(profile).unwrap_or_else(|_| "[error serializing profile]".to_string())
}