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