lean-ctx 3.9.18

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
//! Connects the RSS-based memory_guard to real cache eviction via HomeostasisController.
//!
//! The orchestrator bridges two systems:
//! - `memory_guard`: monitors process RSS and reports PressureLevel (Normal..Critical)
//! - `homeostasis`: decides which eviction action to take based on token utilization
//!
//! On each pressure callback, the orchestrator queries current cache utilization,
//! feeds it to HomeostasisController, and executes the recommended action.

use std::sync::{Arc, Mutex};

use super::bm25_cache::SharedBm25Cache;
use super::cache::SessionCache;
use super::homeostasis::{HomeostasisAction, HomeostasisController};
use super::memory_guard;

type SharedCache = Arc<tokio::sync::RwLock<SessionCache>>;

pub(crate) struct EvictionOrchestrator {
    cache: SharedCache,
    bm25_cache: SharedBm25Cache,
    controller: Mutex<HomeostasisController>,
    token_budget: usize,
}

#[derive(Default)]
struct EvictionRegistry {
    targets: Vec<std::sync::Weak<EvictionOrchestrator>>,
}

impl EvictionRegistry {
    fn register(&mut self, target: &Arc<EvictionOrchestrator>) {
        self.targets.retain(|target| target.strong_count() > 0);
        self.targets.push(Arc::downgrade(target));
    }

    fn live_targets(&mut self) -> Vec<Arc<EvictionOrchestrator>> {
        let live = self
            .targets
            .iter()
            .filter_map(std::sync::Weak::upgrade)
            .collect();
        self.targets.retain(|target| target.strong_count() > 0);
        live
    }
}

static TARGETS: std::sync::LazyLock<Mutex<EvictionRegistry>> =
    std::sync::LazyLock::new(|| Mutex::new(EvictionRegistry::default()));

/// Register a server-local eviction target without extending its lifetime.
pub(crate) fn register(target: &Arc<EvictionOrchestrator>) {
    TARGETS
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
        .register(target);
}

/// Fan process-wide RSS pressure out to every live server-local cache.
/// Returns whether any eviction target actually reclaimed resident memory.
pub(crate) fn on_memory_pressure(level: memory_guard::PressureLevel) -> bool {
    let live = TARGETS
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
        .live_targets();

    let mut made_progress = false;
    for target in live {
        made_progress |= target.on_pressure(level);
    }

    // These caches are process-global rather than owned by a server target.
    if level >= memory_guard::PressureLevel::Hard {
        let content_bytes = super::content_cache::memory_usage_bytes();
        let ann_bytes = super::ann_cache::memory_usage_bytes();
        super::content_cache::clear();
        super::ann_cache::clear();
        super::search_index::clear_resident();
        super::graph_cache::invalidate(None);
        made_progress |= content_bytes > 0 || ann_bytes > 0;
    }

    made_progress
}

impl EvictionOrchestrator {
    pub(crate) fn new(cache: SharedCache, bm25_cache: SharedBm25Cache) -> Self {
        let token_budget = super::cache::max_cache_tokens();
        Self {
            cache,
            bm25_cache,
            controller: Mutex::new(HomeostasisController::new(token_budget)),
            token_budget,
        }
    }

    /// Called by the memory_guard thread when pressure is detected.
    /// Runs on the guardian thread — must not block on async locks for too long.
    pub(crate) fn on_pressure(&self, level: memory_guard::PressureLevel) -> bool {
        if level == memory_guard::PressureLevel::Normal {
            return false;
        }

        let current_tokens = self.try_read_cache_tokens();
        let bm25_bytes = super::bm25_cache::memory_usage(&self.bm25_cache);

        let effective_tokens = if bm25_bytes > 0 {
            current_tokens + bm25_bytes / 4
        } else {
            current_tokens
        };

        let action = {
            let mut ctrl = self
                .controller
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            ctrl.evaluate(effective_tokens)
        };

        // #685: the homeostasis controller reasons over *cache-token*
        // utilization, but RSS pressure can come from structures the token
        // heuristic cannot see (ANN/HNSW graph, resident trigram + graph
        // indices, transient build state). At Hard/Critical RSS the guard's
        // signal must win: enforce a floor action so eviction always reaches
        // the index caches instead of returning `None` because the session
        // cache happens to be small.
        let action = floor_action_for_rss(level, action);

        if action == HomeostasisAction::None {
            return false;
        }

        tracing::info!(
            "[eviction] pressure={level:?} tokens={current_tokens}/{} bm25={:.1}MB action={action:?}",
            self.token_budget,
            bm25_bytes as f64 / 1_048_576.0,
        );

        let pressure_reduced = self.execute_action(&action, bm25_bytes);

        let mut ctrl = self
            .controller
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        ctrl.report_outcome(pressure_reduced);
        pressure_reduced
    }

    fn execute_action(&self, action: &HomeostasisAction, bm25_bytes: usize) -> bool {
        match action {
            HomeostasisAction::None => false,

            HomeostasisAction::TrimOutputs => {
                let trimmed = self.try_write_cache(SessionCache::trim_compressed_outputs);
                tracing::info!("[eviction] trimmed compressed outputs from {trimmed} entries");
                trimmed > 0
            }

            HomeostasisAction::EvictProbationary { .. } => {
                let evicted = self.try_write_cache(|cache| {
                    let n = cache.evict_probationary();
                    cache.trim_shared_blocks();
                    n
                });
                tracing::info!("[eviction] evicted {evicted} probationary entries");
                evicted > 0
            }

            HomeostasisAction::UnloadIndices => {
                if bm25_bytes > 0 {
                    super::bm25_cache::unload(&self.bm25_cache);
                }
                let content_freed = super::content_cache::memory_usage_bytes();
                super::content_cache::clear();
                // #685: the ANN/HNSW graph, resident trigram indices and the
                // materialized graph indexes were invisible to eviction — on
                // embedding-heavy sessions they dominated RSS while the
                // guardian could only trim the (small) session cache.
                let ann_freed = super::ann_cache::memory_usage_bytes();
                super::ann_cache::clear();
                super::search_index::clear_resident();
                super::graph_cache::invalidate(None);
                let trimmed = self.try_write_cache(SessionCache::trim_compressed_outputs);
                memory_guard::jemalloc_purge();
                tracing::info!(
                    "[eviction] unloaded indices (bm25={:.1}MB + content={:.1}MB + ann={:.1}MB freed, \
                     search/graph residents dropped, {trimmed} outputs trimmed)",
                    bm25_bytes as f64 / 1_048_576.0,
                    content_freed as f64 / 1_048_576.0,
                    ann_freed as f64 / 1_048_576.0,
                );
                bm25_bytes > 0 || content_freed > 0 || ann_freed > 0 || trimmed > 0
            }

            HomeostasisAction::EvictProtected { target_tokens } => {
                let before = self.try_read_cache_tokens();
                self.try_write_cache(|cache| cache.evict_to_budget(*target_tokens));
                let after = self.try_read_cache_tokens();
                memory_guard::jemalloc_purge();
                tracing::info!(
                    "[eviction] evicted protected entries to budget {target_tokens} tokens"
                );
                after < before
            }

            HomeostasisAction::EmergencyDrop => {
                let cleared = self.try_write_cache(SessionCache::clear);
                let content_freed = super::content_cache::memory_usage_bytes();
                let ann_freed = super::ann_cache::memory_usage_bytes();
                super::bm25_cache::unload(&self.bm25_cache);
                super::content_cache::clear();
                // #685: emergency must reach every resident structure.
                super::ann_cache::clear();
                super::search_index::clear_resident();
                super::graph_cache::invalidate(None);
                memory_guard::jemalloc_purge();
                tracing::warn!(
                    "[eviction] EMERGENCY: cleared {cleared} cache entries + unloaded all indices \
                     (bm25, content, ann, search, graph)"
                );
                cleared > 0 || bm25_bytes > 0 || content_freed > 0 || ann_freed > 0
            }
        }
    }

    /// Try to read current token count without blocking.
    /// Falls back to budget (assumes full) if the lock is contended.
    fn try_read_cache_tokens(&self) -> usize {
        match self.cache.try_read() {
            Ok(guard) => guard.total_cached_tokens(),
            Err(_) => self.token_budget,
        }
    }

    /// Try to write to the cache. Returns default value if lock is contended.
    fn try_write_cache<F, R>(&self, f: F) -> R
    where
        F: FnOnce(&mut SessionCache) -> R,
        R: Default,
    {
        if let Ok(mut guard) = self.cache.try_write() {
            f(&mut guard)
        } else {
            tracing::debug!("[eviction] cache write lock contended, skipping");
            R::default()
        }
    }
}

/// RSS floor (#685): guarantee a minimum eviction strength for a given guard
/// pressure level, regardless of the token-based homeostasis verdict. Token
/// utilization measures only the session cache; the structures that actually
/// blew up in #685 (HNSW graph, resident indices, build transients) are
/// invisible to it — Hard/Critical RSS must always at least unload indices.
fn floor_action_for_rss(
    level: memory_guard::PressureLevel,
    action: HomeostasisAction,
) -> HomeostasisAction {
    let rank = |a: &HomeostasisAction| match a {
        HomeostasisAction::None => 0u8,
        HomeostasisAction::TrimOutputs => 1,
        HomeostasisAction::EvictProbationary { .. } => 2,
        HomeostasisAction::UnloadIndices => 3,
        HomeostasisAction::EvictProtected { .. } => 4,
        HomeostasisAction::EmergencyDrop => 5,
    };
    let floor = match level {
        memory_guard::PressureLevel::Critical => HomeostasisAction::EmergencyDrop,
        memory_guard::PressureLevel::Hard => HomeostasisAction::UnloadIndices,
        // Soft/Medium RSS: trust the graduated token-based controller, but do
        // not let real pressure end in a no-op.
        memory_guard::PressureLevel::Soft | memory_guard::PressureLevel::Medium => {
            HomeostasisAction::TrimOutputs
        }
        memory_guard::PressureLevel::Normal => HomeostasisAction::None,
    };
    if rank(&action) >= rank(&floor) {
        action
    } else {
        floor
    }
}

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

    fn make_orchestrator() -> EvictionOrchestrator {
        let cache = Arc::new(tokio::sync::RwLock::new(SessionCache::new()));
        let bm25_cache: SharedBm25Cache = Arc::new(std::sync::Mutex::new(None));
        EvictionOrchestrator::new(cache, bm25_cache)
    }

    #[test]
    fn registry_fans_out_and_prunes_dropped_targets() {
        // #685/#975-class: on_pressure(Critical) floors to EmergencyDrop, which
        // clears the process-wide ann_cache — hold its cross-module test lock
        // so a concurrent ann_cache test can't observe a wiped cache mid-assertion.
        let _ann_lock = crate::core::ann_cache::test_lock();
        let first = Arc::new(make_orchestrator());
        let second = Arc::new(make_orchestrator());
        let first_cache = first.cache.clone();
        let second_cache = second.cache.clone();
        first_cache
            .blocking_write()
            .store("/first.rs", "fn first() {}");
        second_cache
            .blocking_write()
            .store("/second.rs", "fn second() {}");

        let mut registry = EvictionRegistry::default();
        registry.register(&first);
        registry.register(&second);
        let live = registry.live_targets();
        assert_eq!(live.len(), 2);
        for target in live {
            target.on_pressure(memory_guard::PressureLevel::Critical);
        }
        assert_eq!(first_cache.blocking_read().total_cached_tokens(), 0);
        assert_eq!(second_cache.blocking_read().total_cached_tokens(), 0);

        drop(second);
        assert_eq!(registry.live_targets().len(), 1);
        assert_eq!(registry.targets.len(), 1);
    }

    #[test]
    fn normal_pressure_is_noop() {
        let orch = make_orchestrator();
        orch.on_pressure(memory_guard::PressureLevel::Normal);
    }

    #[test]
    fn soft_pressure_with_empty_cache_is_noop() {
        let orch = make_orchestrator();
        orch.on_pressure(memory_guard::PressureLevel::Soft);
    }

    #[test]
    fn emergency_clears_cache() {
        // See `registry_fans_out_and_prunes_dropped_targets` above: EmergencyDrop
        // clears the shared ann_cache, so hold its cross-module test lock.
        let _ann_lock = crate::core::ann_cache::test_lock();
        let cache = Arc::new(tokio::sync::RwLock::new(SessionCache::new()));
        {
            let mut c = cache.blocking_write();
            c.store("/a.rs", "fn a() {}");
            c.store("/b.rs", "fn b() {}");
        }
        let bm25: SharedBm25Cache = Arc::new(std::sync::Mutex::new(None));
        let orch = EvictionOrchestrator {
            cache: cache.clone(),
            bm25_cache: bm25,
            controller: Mutex::new(HomeostasisController::new(100)),
            token_budget: 100,
        };

        orch.execute_action(&HomeostasisAction::EmergencyDrop, 0);
        let c = cache.blocking_read();
        assert_eq!(c.total_cached_tokens(), 0);
    }

    #[test]
    fn trim_outputs_clears_compressed() {
        let cache = Arc::new(tokio::sync::RwLock::new(SessionCache::new()));
        {
            let mut c = cache.blocking_write();
            c.store("/a.rs", "fn main() {}");
            c.set_compressed("/a.rs", "map", "compressed map".to_string());
        }
        let bm25: SharedBm25Cache = Arc::new(std::sync::Mutex::new(None));
        let orch = EvictionOrchestrator {
            cache: cache.clone(),
            bm25_cache: bm25,
            controller: Mutex::new(HomeostasisController::new(100_000)),
            token_budget: 100_000,
        };

        let result = orch.execute_action(&HomeostasisAction::TrimOutputs, 0);
        assert!(result);
        let c = cache.blocking_read();
        assert!(c.get_compressed("/a.rs", "map").is_none());
    }

    #[test]
    fn rss_floor_escalates_weak_actions_under_hard_pressure() {
        // Session cache nearly empty → controller says None; Hard RSS must
        // still unload indices (#685: token heuristic blind to index RAM).
        let floored =
            floor_action_for_rss(memory_guard::PressureLevel::Hard, HomeostasisAction::None);
        assert_eq!(floored, HomeostasisAction::UnloadIndices);

        let floored = floor_action_for_rss(
            memory_guard::PressureLevel::Critical,
            HomeostasisAction::TrimOutputs,
        );
        assert_eq!(floored, HomeostasisAction::EmergencyDrop);

        let floored =
            floor_action_for_rss(memory_guard::PressureLevel::Soft, HomeostasisAction::None);
        assert_eq!(floored, HomeostasisAction::TrimOutputs);
    }

    #[test]
    fn rss_floor_keeps_stronger_controller_actions() {
        // The controller may already demand more than the floor — keep it.
        let strong = HomeostasisAction::EvictProtected {
            target_tokens: 1_000,
        };
        let kept = floor_action_for_rss(memory_guard::PressureLevel::Hard, strong.clone());
        assert_eq!(kept, strong);

        let normal =
            floor_action_for_rss(memory_guard::PressureLevel::Normal, HomeostasisAction::None);
        assert_eq!(normal, HomeostasisAction::None);
    }

    #[test]
    fn evict_probationary_removes_single_reads() {
        let cache = Arc::new(tokio::sync::RwLock::new(SessionCache::new()));
        {
            let mut c = cache.blocking_write();
            c.store("/once.rs", "fn once() {}");
            c.store("/twice.rs", "fn twice() {}");
            c.store("/twice.rs", "fn twice() {}"); // second read → read_count=2
        }
        let bm25: SharedBm25Cache = Arc::new(std::sync::Mutex::new(None));
        let orch = EvictionOrchestrator {
            cache: cache.clone(),
            bm25_cache: bm25,
            controller: Mutex::new(HomeostasisController::new(100_000)),
            token_budget: 100_000,
        };

        let result = orch.execute_action(
            &HomeostasisAction::EvictProbationary { target_tokens: 0 },
            0,
        );
        assert!(result);
        let c = cache.blocking_read();
        assert!(c.get("/once.rs").is_none());
        assert!(c.get("/twice.rs").is_some());
    }
}