Skip to main content

lean_ctx/tools/
mod.rs

1use std::sync::atomic::{AtomicUsize, Ordering};
2use std::sync::Arc;
3use std::time::Instant;
4use tokio::sync::RwLock;
5
6use crate::core::cache::SessionCache;
7use crate::core::session::SessionState;
8
9pub mod autonomy;
10pub mod ctx_agent;
11pub mod ctx_analyze;
12pub mod ctx_architecture;
13pub mod ctx_benchmark;
14pub mod ctx_callees;
15pub mod ctx_callers;
16pub mod ctx_compress;
17pub mod ctx_compress_memory;
18pub mod ctx_context;
19pub mod ctx_cost;
20pub mod ctx_dedup;
21pub mod ctx_delta;
22pub mod ctx_discover;
23pub mod ctx_edit;
24pub mod ctx_execute;
25pub mod ctx_fill;
26pub mod ctx_graph;
27pub mod ctx_graph_diagram;
28pub mod ctx_heatmap;
29pub mod ctx_impact;
30pub mod ctx_intent;
31pub mod ctx_knowledge;
32pub mod ctx_metrics;
33pub mod ctx_multi_read;
34pub mod ctx_outline;
35pub mod ctx_overview;
36pub mod ctx_preload;
37pub mod ctx_read;
38pub mod ctx_response;
39pub mod ctx_routes;
40pub mod ctx_search;
41pub mod ctx_semantic_search;
42pub mod ctx_session;
43pub mod ctx_share;
44pub mod ctx_shell;
45pub mod ctx_smart_read;
46pub mod ctx_symbol;
47pub mod ctx_task;
48pub mod ctx_tree;
49pub mod ctx_wrapped;
50
51const DEFAULT_CACHE_TTL_SECS: u64 = 300;
52
53struct CepComputedStats {
54    cep_score: u32,
55    cache_util: u32,
56    mode_diversity: u32,
57    compression_rate: u32,
58    total_original: u64,
59    total_compressed: u64,
60    total_saved: u64,
61    mode_counts: std::collections::HashMap<String, u64>,
62    complexity: String,
63    cache_hits: u64,
64    total_reads: u64,
65    tool_call_count: u64,
66}
67
68#[derive(Clone, Copy, Debug, PartialEq, Eq)]
69pub enum CrpMode {
70    Off,
71    Compact,
72    Tdd,
73}
74
75impl CrpMode {
76    pub fn from_env() -> Self {
77        match std::env::var("LEAN_CTX_CRP_MODE")
78            .unwrap_or_default()
79            .to_lowercase()
80            .as_str()
81        {
82            "off" => Self::Off,
83            "compact" => Self::Compact,
84            _ => Self::Tdd,
85        }
86    }
87
88    pub fn is_tdd(&self) -> bool {
89        *self == Self::Tdd
90    }
91}
92
93pub type SharedCache = Arc<RwLock<SessionCache>>;
94
95#[derive(Clone)]
96pub struct LeanCtxServer {
97    pub cache: SharedCache,
98    pub session: Arc<RwLock<SessionState>>,
99    pub tool_calls: Arc<RwLock<Vec<ToolCallRecord>>>,
100    pub call_count: Arc<AtomicUsize>,
101    pub checkpoint_interval: usize,
102    pub cache_ttl_secs: u64,
103    pub last_call: Arc<RwLock<Instant>>,
104    pub crp_mode: CrpMode,
105    pub agent_id: Arc<RwLock<Option<String>>>,
106    pub client_name: Arc<RwLock<String>>,
107    pub autonomy: Arc<autonomy::AutonomyState>,
108    pub loop_detector: Arc<RwLock<crate::core::loop_detection::LoopDetector>>,
109}
110
111#[derive(Clone, Debug)]
112pub struct ToolCallRecord {
113    pub tool: String,
114    pub original_tokens: usize,
115    pub saved_tokens: usize,
116    pub mode: Option<String>,
117    pub duration_ms: u64,
118    pub timestamp: String,
119}
120
121impl Default for LeanCtxServer {
122    fn default() -> Self {
123        Self::new()
124    }
125}
126
127impl LeanCtxServer {
128    pub fn new() -> Self {
129        let config = crate::core::config::Config::load();
130
131        let interval = std::env::var("LEAN_CTX_CHECKPOINT_INTERVAL")
132            .ok()
133            .and_then(|v| v.parse().ok())
134            .unwrap_or(config.checkpoint_interval as usize);
135
136        let ttl = std::env::var("LEAN_CTX_CACHE_TTL")
137            .ok()
138            .and_then(|v| v.parse().ok())
139            .unwrap_or(DEFAULT_CACHE_TTL_SECS);
140
141        let crp_mode = CrpMode::from_env();
142
143        let session = SessionState::load_latest().unwrap_or_default();
144        Self {
145            cache: Arc::new(RwLock::new(SessionCache::new())),
146            session: Arc::new(RwLock::new(session)),
147            tool_calls: Arc::new(RwLock::new(Vec::new())),
148            call_count: Arc::new(AtomicUsize::new(0)),
149            checkpoint_interval: interval,
150            cache_ttl_secs: ttl,
151            last_call: Arc::new(RwLock::new(Instant::now())),
152            crp_mode,
153            agent_id: Arc::new(RwLock::new(None)),
154            client_name: Arc::new(RwLock::new(String::new())),
155            autonomy: Arc::new(autonomy::AutonomyState::new()),
156            loop_detector: Arc::new(RwLock::new(
157                crate::core::loop_detection::LoopDetector::with_config(
158                    &crate::core::config::Config::load().loop_detection,
159                ),
160            )),
161        }
162    }
163
164    /// Resolves a (possibly relative) tool path against the session's project_root.
165    /// Absolute paths and "." are returned as-is. Relative paths like "src/main.rs"
166    /// are joined with project_root so tools work regardless of the server's cwd.
167    pub async fn resolve_path(&self, path: &str) -> String {
168        let normalized = crate::hooks::normalize_tool_path(path);
169        if normalized.is_empty() || normalized == "." {
170            return normalized;
171        }
172        let p = std::path::Path::new(&normalized);
173        if p.is_absolute() || p.exists() {
174            return normalized;
175        }
176        let session = self.session.read().await;
177        if let Some(ref root) = session.project_root {
178            let resolved = std::path::Path::new(root).join(&normalized);
179            if resolved.exists() {
180                return resolved.to_string_lossy().to_string();
181            }
182        }
183        if let Some(ref cwd) = session.shell_cwd {
184            let resolved = std::path::Path::new(cwd).join(&normalized);
185            if resolved.exists() {
186                return resolved.to_string_lossy().to_string();
187            }
188        }
189        normalized
190    }
191
192    pub async fn check_idle_expiry(&self) {
193        if self.cache_ttl_secs == 0 {
194            return;
195        }
196        let last = *self.last_call.read().await;
197        if last.elapsed().as_secs() >= self.cache_ttl_secs {
198            {
199                let mut session = self.session.write().await;
200                let _ = session.save();
201            }
202            let mut cache = self.cache.write().await;
203            let count = cache.clear();
204            if count > 0 {
205                tracing::info!(
206                    "Cache auto-cleared after {}s idle ({count} file(s))",
207                    self.cache_ttl_secs
208                );
209            }
210        }
211        *self.last_call.write().await = Instant::now();
212    }
213
214    pub async fn record_call(
215        &self,
216        tool: &str,
217        original: usize,
218        saved: usize,
219        mode: Option<String>,
220    ) {
221        self.record_call_with_timing(tool, original, saved, mode, 0)
222            .await;
223    }
224
225    pub async fn record_call_with_timing(
226        &self,
227        tool: &str,
228        original: usize,
229        saved: usize,
230        mode: Option<String>,
231        duration_ms: u64,
232    ) {
233        let ts = chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string();
234        let mut calls = self.tool_calls.write().await;
235        calls.push(ToolCallRecord {
236            tool: tool.to_string(),
237            original_tokens: original,
238            saved_tokens: saved,
239            mode: mode.clone(),
240            duration_ms,
241            timestamp: ts.clone(),
242        });
243
244        if duration_ms > 0 {
245            Self::append_tool_call_log(tool, duration_ms, original, saved, mode.as_deref(), &ts);
246        }
247
248        crate::core::events::emit_tool_call(
249            tool,
250            original as u64,
251            saved as u64,
252            mode.clone(),
253            duration_ms,
254            None,
255        );
256
257        let output_tokens = original.saturating_sub(saved);
258        crate::core::stats::record(tool, original, output_tokens);
259
260        let mut session = self.session.write().await;
261        session.record_tool_call(saved as u64, original as u64);
262        if tool == "ctx_shell" {
263            session.record_command();
264        }
265        if session.should_save() {
266            let _ = session.save();
267        }
268        drop(calls);
269        drop(session);
270
271        self.write_mcp_live_stats().await;
272    }
273
274    pub async fn is_prompt_cache_stale(&self) -> bool {
275        let last = *self.last_call.read().await;
276        last.elapsed().as_secs() > 3600
277    }
278
279    pub fn upgrade_mode_if_stale(mode: &str, stale: bool) -> &str {
280        if !stale {
281            return mode;
282        }
283        match mode {
284            "full" => "full",
285            "map" => "signatures",
286            m => m,
287        }
288    }
289
290    pub fn increment_and_check(&self) -> bool {
291        let count = self.call_count.fetch_add(1, Ordering::Relaxed) + 1;
292        self.checkpoint_interval > 0 && count.is_multiple_of(self.checkpoint_interval)
293    }
294
295    pub async fn auto_checkpoint(&self) -> Option<String> {
296        let cache = self.cache.read().await;
297        if cache.get_all_entries().is_empty() {
298            return None;
299        }
300        let complexity = crate::core::adaptive::classify_from_context(&cache);
301        let checkpoint = ctx_compress::handle(&cache, true, self.crp_mode);
302        drop(cache);
303
304        let mut session = self.session.write().await;
305        let _ = session.save();
306        let session_summary = session.format_compact();
307        let has_insights = !session.findings.is_empty() || !session.decisions.is_empty();
308        let project_root = session.project_root.clone();
309        drop(session);
310
311        if has_insights {
312            if let Some(ref root) = project_root {
313                let root = root.clone();
314                std::thread::spawn(move || {
315                    auto_consolidate_knowledge(&root);
316                });
317            }
318        }
319
320        let multi_agent_block = self.auto_multi_agent_checkpoint(&project_root).await;
321
322        self.record_call("ctx_compress", 0, 0, Some("auto".to_string()))
323            .await;
324
325        self.record_cep_snapshot().await;
326
327        Some(format!(
328            "{checkpoint}\n\n--- SESSION STATE ---\n{session_summary}\n\n{}{multi_agent_block}",
329            complexity.instruction_suffix()
330        ))
331    }
332
333    async fn auto_multi_agent_checkpoint(&self, project_root: &Option<String>) -> String {
334        let root = match project_root {
335            Some(r) => r,
336            None => return String::new(),
337        };
338
339        let registry = crate::core::agents::AgentRegistry::load_or_create();
340        let active = registry.list_active(Some(root));
341        if active.len() <= 1 {
342            return String::new();
343        }
344
345        let agent_id = self.agent_id.read().await;
346        let my_id = match agent_id.as_deref() {
347            Some(id) => id.to_string(),
348            None => return String::new(),
349        };
350        drop(agent_id);
351
352        let cache = self.cache.read().await;
353        let entries = cache.get_all_entries();
354        if !entries.is_empty() {
355            let mut by_access: Vec<_> = entries.iter().collect();
356            by_access.sort_by(|a, b| b.1.read_count.cmp(&a.1.read_count));
357            let top_paths: Vec<&str> = by_access
358                .iter()
359                .take(5)
360                .map(|(key, _)| key.as_str())
361                .collect();
362            let paths_csv = top_paths.join(",");
363
364            let _ = ctx_share::handle("push", Some(&my_id), None, Some(&paths_csv), None, &cache);
365        }
366        drop(cache);
367
368        let pending_count = registry
369            .scratchpad
370            .iter()
371            .filter(|e| !e.read_by.contains(&my_id) && e.from_agent != my_id)
372            .count();
373
374        let shared_dir = dirs::home_dir()
375            .unwrap_or_default()
376            .join(".lean-ctx")
377            .join("agents")
378            .join("shared");
379        let shared_count = if shared_dir.exists() {
380            std::fs::read_dir(&shared_dir)
381                .map(|rd| rd.count())
382                .unwrap_or(0)
383        } else {
384            0
385        };
386
387        let agent_names: Vec<String> = active
388            .iter()
389            .map(|a| {
390                let role = a.role.as_deref().unwrap_or(&a.agent_type);
391                format!("{role}({})", &a.agent_id[..8.min(a.agent_id.len())])
392            })
393            .collect();
394
395        format!(
396            "\n\n--- MULTI-AGENT SYNC ---\nAgents: {} | Pending msgs: {} | Shared contexts: {}\nAuto-shared top-5 cached files.\n--- END SYNC ---",
397            agent_names.join(", "),
398            pending_count,
399            shared_count,
400        )
401    }
402
403    pub fn append_tool_call_log(
404        tool: &str,
405        duration_ms: u64,
406        original: usize,
407        saved: usize,
408        mode: Option<&str>,
409        timestamp: &str,
410    ) {
411        const MAX_LOG_LINES: usize = 50;
412        if let Some(dir) = dirs::home_dir().map(|h| h.join(".lean-ctx")) {
413            let log_path = dir.join("tool-calls.log");
414            let mode_str = mode.unwrap_or("-");
415            let slow = if duration_ms > 5000 { " **SLOW**" } else { "" };
416            let line = format!(
417                "{timestamp}\t{tool}\t{duration_ms}ms\torig={original}\tsaved={saved}\tmode={mode_str}{slow}\n"
418            );
419
420            let mut lines: Vec<String> = std::fs::read_to_string(&log_path)
421                .unwrap_or_default()
422                .lines()
423                .map(|l| l.to_string())
424                .collect();
425
426            lines.push(line.trim_end().to_string());
427            if lines.len() > MAX_LOG_LINES {
428                lines.drain(0..lines.len() - MAX_LOG_LINES);
429            }
430
431            let _ = std::fs::write(&log_path, lines.join("\n") + "\n");
432        }
433    }
434
435    fn compute_cep_stats(
436        calls: &[ToolCallRecord],
437        stats: &crate::core::cache::CacheStats,
438        complexity: &crate::core::adaptive::TaskComplexity,
439    ) -> CepComputedStats {
440        let total_original: u64 = calls.iter().map(|c| c.original_tokens as u64).sum();
441        let total_saved: u64 = calls.iter().map(|c| c.saved_tokens as u64).sum();
442        let total_compressed = total_original.saturating_sub(total_saved);
443        let compression_rate = if total_original > 0 {
444            total_saved as f64 / total_original as f64
445        } else {
446            0.0
447        };
448
449        let modes_used: std::collections::HashSet<&str> =
450            calls.iter().filter_map(|c| c.mode.as_deref()).collect();
451        let mode_diversity = (modes_used.len() as f64 / 6.0).min(1.0);
452        let cache_util = stats.hit_rate() / 100.0;
453        let cep_score = cache_util * 0.3 + mode_diversity * 0.2 + compression_rate * 0.5;
454
455        let mut mode_counts: std::collections::HashMap<String, u64> =
456            std::collections::HashMap::new();
457        for call in calls {
458            if let Some(ref mode) = call.mode {
459                *mode_counts.entry(mode.clone()).or_insert(0) += 1;
460            }
461        }
462
463        CepComputedStats {
464            cep_score: (cep_score * 100.0).round() as u32,
465            cache_util: (cache_util * 100.0).round() as u32,
466            mode_diversity: (mode_diversity * 100.0).round() as u32,
467            compression_rate: (compression_rate * 100.0).round() as u32,
468            total_original,
469            total_compressed,
470            total_saved,
471            mode_counts,
472            complexity: format!("{:?}", complexity),
473            cache_hits: stats.cache_hits,
474            total_reads: stats.total_reads,
475            tool_call_count: calls.len() as u64,
476        }
477    }
478
479    async fn write_mcp_live_stats(&self) {
480        let cache = self.cache.read().await;
481        let calls = self.tool_calls.read().await;
482        let stats = cache.get_stats();
483        let complexity = crate::core::adaptive::classify_from_context(&cache);
484
485        let cs = Self::compute_cep_stats(&calls, stats, &complexity);
486
487        drop(cache);
488        drop(calls);
489
490        let live = serde_json::json!({
491            "cep_score": cs.cep_score,
492            "cache_utilization": cs.cache_util,
493            "mode_diversity": cs.mode_diversity,
494            "compression_rate": cs.compression_rate,
495            "task_complexity": cs.complexity,
496            "files_cached": cs.total_reads,
497            "total_reads": cs.total_reads,
498            "cache_hits": cs.cache_hits,
499            "tokens_saved": cs.total_saved,
500            "tokens_original": cs.total_original,
501            "tool_calls": cs.tool_call_count,
502            "updated_at": chrono::Local::now().to_rfc3339(),
503        });
504
505        if let Some(dir) = dirs::home_dir().map(|h| h.join(".lean-ctx")) {
506            let _ = std::fs::write(dir.join("mcp-live.json"), live.to_string());
507        }
508    }
509
510    pub async fn record_cep_snapshot(&self) {
511        let cache = self.cache.read().await;
512        let calls = self.tool_calls.read().await;
513        let stats = cache.get_stats();
514        let complexity = crate::core::adaptive::classify_from_context(&cache);
515
516        let cs = Self::compute_cep_stats(&calls, stats, &complexity);
517
518        drop(cache);
519        drop(calls);
520
521        crate::core::stats::record_cep_session(
522            cs.cep_score,
523            cs.cache_hits,
524            cs.total_reads,
525            cs.total_original,
526            cs.total_compressed,
527            &cs.mode_counts,
528            cs.tool_call_count,
529            &cs.complexity,
530        );
531    }
532}
533
534pub fn create_server() -> LeanCtxServer {
535    LeanCtxServer::new()
536}
537
538fn auto_consolidate_knowledge(project_root: &str) {
539    use crate::core::knowledge::ProjectKnowledge;
540    use crate::core::session::SessionState;
541
542    let session = match SessionState::load_latest() {
543        Some(s) => s,
544        None => return,
545    };
546
547    if session.findings.is_empty() && session.decisions.is_empty() {
548        return;
549    }
550
551    let mut knowledge = ProjectKnowledge::load_or_create(project_root);
552
553    for finding in &session.findings {
554        let key = if let Some(ref file) = finding.file {
555            if let Some(line) = finding.line {
556                format!("{file}:{line}")
557            } else {
558                file.clone()
559            }
560        } else {
561            "finding-auto".to_string()
562        };
563        knowledge.remember("finding", &key, &finding.summary, &session.id, 0.7);
564    }
565
566    for decision in &session.decisions {
567        let key = decision
568            .summary
569            .chars()
570            .take(50)
571            .collect::<String>()
572            .replace(' ', "-")
573            .to_lowercase();
574        knowledge.remember("decision", &key, &decision.summary, &session.id, 0.85);
575    }
576
577    let task_desc = session
578        .task
579        .as_ref()
580        .map(|t| t.description.clone())
581        .unwrap_or_default();
582
583    let summary = format!(
584        "Auto-consolidate session {}: {} — {} findings, {} decisions",
585        session.id,
586        task_desc,
587        session.findings.len(),
588        session.decisions.len()
589    );
590    knowledge.consolidate(&summary, vec![session.id.clone()]);
591    let _ = knowledge.save();
592}