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 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 crate::core::events::emit_tool_call(
234 tool,
235 original as u64,
236 saved as u64,
237 mode.clone(),
238 duration_ms,
239 None,
240 );
241
242 let output_tokens = original.saturating_sub(saved);
243 crate::core::stats::record(tool, original, output_tokens);
244
245 let mut session = self.session.write().await;
246 session.record_tool_call(saved as u64, original as u64);
247 if tool == "ctx_shell" {
248 session.record_command();
249 }
250 if session.should_save() {
251 let _ = session.save();
252 }
253 drop(calls);
254 drop(session);
255
256 self.write_mcp_live_stats().await;
257 }
258
259 pub async fn is_prompt_cache_stale(&self) -> bool {
260 let last = *self.last_call.read().await;
261 last.elapsed().as_secs() > 3600
262 }
263
264 pub fn upgrade_mode_if_stale(mode: &str, stale: bool) -> &str {
265 if !stale {
266 return mode;
267 }
268 match mode {
269 "full" => "full",
270 "map" => "signatures",
271 m => m,
272 }
273 }
274
275 pub fn increment_and_check(&self) -> bool {
276 let count = self.call_count.fetch_add(1, Ordering::Relaxed) + 1;
277 self.checkpoint_interval > 0 && count.is_multiple_of(self.checkpoint_interval)
278 }
279
280 pub async fn auto_checkpoint(&self) -> Option<String> {
281 let cache = self.cache.read().await;
282 if cache.get_all_entries().is_empty() {
283 return None;
284 }
285 let complexity = crate::core::adaptive::classify_from_context(&cache);
286 let checkpoint = ctx_compress::handle(&cache, true, self.crp_mode);
287 drop(cache);
288
289 let mut session = self.session.write().await;
290 let _ = session.save();
291 let session_summary = session.format_compact();
292 let has_insights = !session.findings.is_empty() || !session.decisions.is_empty();
293 let project_root = session.project_root.clone();
294 drop(session);
295
296 if has_insights {
297 if let Some(ref root) = project_root {
298 let root = root.clone();
299 std::thread::spawn(move || {
300 auto_consolidate_knowledge(&root);
301 });
302 }
303 }
304
305 let multi_agent_block = self.auto_multi_agent_checkpoint(&project_root).await;
306
307 self.record_call("ctx_compress", 0, 0, Some("auto".to_string()))
308 .await;
309
310 self.record_cep_snapshot().await;
311
312 Some(format!(
313 "{checkpoint}\n\n--- SESSION STATE ---\n{session_summary}\n\n{}{multi_agent_block}",
314 complexity.instruction_suffix()
315 ))
316 }
317
318 async fn auto_multi_agent_checkpoint(&self, project_root: &Option<String>) -> String {
319 let root = match project_root {
320 Some(r) => r,
321 None => return String::new(),
322 };
323
324 let registry = crate::core::agents::AgentRegistry::load_or_create();
325 let active = registry.list_active(Some(root));
326 if active.len() <= 1 {
327 return String::new();
328 }
329
330 let agent_id = self.agent_id.read().await;
331 let my_id = match agent_id.as_deref() {
332 Some(id) => id.to_string(),
333 None => return String::new(),
334 };
335 drop(agent_id);
336
337 let cache = self.cache.read().await;
338 let entries = cache.get_all_entries();
339 if !entries.is_empty() {
340 let mut by_access: Vec<_> = entries.iter().collect();
341 by_access.sort_by(|a, b| b.1.read_count.cmp(&a.1.read_count));
342 let top_paths: Vec<&str> = by_access
343 .iter()
344 .take(5)
345 .map(|(key, _)| key.as_str())
346 .collect();
347 let paths_csv = top_paths.join(",");
348
349 let _ = ctx_share::handle("push", Some(&my_id), None, Some(&paths_csv), None, &cache);
350 }
351 drop(cache);
352
353 let pending_count = registry
354 .scratchpad
355 .iter()
356 .filter(|e| !e.read_by.contains(&my_id) && e.from_agent != my_id)
357 .count();
358
359 let shared_dir = dirs::home_dir()
360 .unwrap_or_default()
361 .join(".lean-ctx")
362 .join("agents")
363 .join("shared");
364 let shared_count = if shared_dir.exists() {
365 std::fs::read_dir(&shared_dir)
366 .map(|rd| rd.count())
367 .unwrap_or(0)
368 } else {
369 0
370 };
371
372 let agent_names: Vec<String> = active
373 .iter()
374 .map(|a| {
375 let role = a.role.as_deref().unwrap_or(&a.agent_type);
376 format!("{role}({})", &a.agent_id[..8.min(a.agent_id.len())])
377 })
378 .collect();
379
380 format!(
381 "\n\n--- MULTI-AGENT SYNC ---\nAgents: {} | Pending msgs: {} | Shared contexts: {}\nAuto-shared top-5 cached files.\n--- END SYNC ---",
382 agent_names.join(", "),
383 pending_count,
384 shared_count,
385 )
386 }
387
388 pub fn append_tool_call_log(
389 tool: &str,
390 duration_ms: u64,
391 original: usize,
392 saved: usize,
393 mode: Option<&str>,
394 timestamp: &str,
395 ) {
396 const MAX_LOG_LINES: usize = 50;
397 if let Some(dir) = dirs::home_dir().map(|h| h.join(".lean-ctx")) {
398 let log_path = dir.join("tool-calls.log");
399 let mode_str = mode.unwrap_or("-");
400 let slow = if duration_ms > 5000 { " **SLOW**" } else { "" };
401 let line = format!(
402 "{timestamp}\t{tool}\t{duration_ms}ms\torig={original}\tsaved={saved}\tmode={mode_str}{slow}\n"
403 );
404
405 let mut lines: Vec<String> = std::fs::read_to_string(&log_path)
406 .unwrap_or_default()
407 .lines()
408 .map(|l| l.to_string())
409 .collect();
410
411 lines.push(line.trim_end().to_string());
412 if lines.len() > MAX_LOG_LINES {
413 lines.drain(0..lines.len() - MAX_LOG_LINES);
414 }
415
416 let _ = std::fs::write(&log_path, lines.join("\n") + "\n");
417 }
418 }
419
420 fn compute_cep_stats(
421 calls: &[ToolCallRecord],
422 stats: &crate::core::cache::CacheStats,
423 complexity: &crate::core::adaptive::TaskComplexity,
424 ) -> CepComputedStats {
425 let total_original: u64 = calls.iter().map(|c| c.original_tokens as u64).sum();
426 let total_saved: u64 = calls.iter().map(|c| c.saved_tokens as u64).sum();
427 let total_compressed = total_original.saturating_sub(total_saved);
428 let compression_rate = if total_original > 0 {
429 total_saved as f64 / total_original as f64
430 } else {
431 0.0
432 };
433
434 let modes_used: std::collections::HashSet<&str> =
435 calls.iter().filter_map(|c| c.mode.as_deref()).collect();
436 let mode_diversity = (modes_used.len() as f64 / 6.0).min(1.0);
437 let cache_util = stats.hit_rate() / 100.0;
438 let cep_score = cache_util * 0.3 + mode_diversity * 0.2 + compression_rate * 0.5;
439
440 let mut mode_counts: std::collections::HashMap<String, u64> =
441 std::collections::HashMap::new();
442 for call in calls {
443 if let Some(ref mode) = call.mode {
444 *mode_counts.entry(mode.clone()).or_insert(0) += 1;
445 }
446 }
447
448 CepComputedStats {
449 cep_score: (cep_score * 100.0).round() as u32,
450 cache_util: (cache_util * 100.0).round() as u32,
451 mode_diversity: (mode_diversity * 100.0).round() as u32,
452 compression_rate: (compression_rate * 100.0).round() as u32,
453 total_original,
454 total_compressed,
455 total_saved,
456 mode_counts,
457 complexity: format!("{:?}", complexity),
458 cache_hits: stats.cache_hits,
459 total_reads: stats.total_reads,
460 tool_call_count: calls.len() as u64,
461 }
462 }
463
464 async fn write_mcp_live_stats(&self) {
465 let cache = self.cache.read().await;
466 let calls = self.tool_calls.read().await;
467 let stats = cache.get_stats();
468 let complexity = crate::core::adaptive::classify_from_context(&cache);
469
470 let cs = Self::compute_cep_stats(&calls, stats, &complexity);
471
472 drop(cache);
473 drop(calls);
474
475 let live = serde_json::json!({
476 "cep_score": cs.cep_score,
477 "cache_utilization": cs.cache_util,
478 "mode_diversity": cs.mode_diversity,
479 "compression_rate": cs.compression_rate,
480 "task_complexity": cs.complexity,
481 "files_cached": cs.total_reads,
482 "total_reads": cs.total_reads,
483 "cache_hits": cs.cache_hits,
484 "tokens_saved": cs.total_saved,
485 "tokens_original": cs.total_original,
486 "tool_calls": cs.tool_call_count,
487 "updated_at": chrono::Local::now().to_rfc3339(),
488 });
489
490 if let Some(dir) = dirs::home_dir().map(|h| h.join(".lean-ctx")) {
491 let _ = std::fs::write(dir.join("mcp-live.json"), live.to_string());
492 }
493 }
494
495 pub async fn record_cep_snapshot(&self) {
496 let cache = self.cache.read().await;
497 let calls = self.tool_calls.read().await;
498 let stats = cache.get_stats();
499 let complexity = crate::core::adaptive::classify_from_context(&cache);
500
501 let cs = Self::compute_cep_stats(&calls, stats, &complexity);
502
503 drop(cache);
504 drop(calls);
505
506 crate::core::stats::record_cep_session(
507 cs.cep_score,
508 cs.cache_hits,
509 cs.total_reads,
510 cs.total_original,
511 cs.total_compressed,
512 &cs.mode_counts,
513 cs.tool_call_count,
514 &cs.complexity,
515 );
516 }
517}
518
519pub fn create_server() -> LeanCtxServer {
520 LeanCtxServer::new()
521}
522
523fn auto_consolidate_knowledge(project_root: &str) {
524 use crate::core::knowledge::ProjectKnowledge;
525 use crate::core::session::SessionState;
526
527 let session = match SessionState::load_latest() {
528 Some(s) => s,
529 None => return,
530 };
531
532 if session.findings.is_empty() && session.decisions.is_empty() {
533 return;
534 }
535
536 let mut knowledge = ProjectKnowledge::load_or_create(project_root);
537
538 for finding in &session.findings {
539 let key = if let Some(ref file) = finding.file {
540 if let Some(line) = finding.line {
541 format!("{file}:{line}")
542 } else {
543 file.clone()
544 }
545 } else {
546 "finding-auto".to_string()
547 };
548 knowledge.remember("finding", &key, &finding.summary, &session.id, 0.7);
549 }
550
551 for decision in &session.decisions {
552 let key = decision
553 .summary
554 .chars()
555 .take(50)
556 .collect::<String>()
557 .replace(' ', "-")
558 .to_lowercase();
559 knowledge.remember("decision", &key, &decision.summary, &session.id, 0.85);
560 }
561
562 let task_desc = session
563 .task
564 .as_ref()
565 .map(|t| t.description.clone())
566 .unwrap_or_default();
567
568 let summary = format!(
569 "Auto-consolidate session {}: {} — {} findings, {} decisions",
570 session.id,
571 task_desc,
572 session.findings.len(),
573 session.decisions.len()
574 );
575 knowledge.consolidate(&summary, vec![session.id.clone()]);
576 let _ = knowledge.save();
577}