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