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