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