Skip to main content

lean_ctx/tools/
server_metrics.rs

1use std::sync::atomic::Ordering;
2
3use super::server::{CepComputedStats, CrpMode, LeanCtxServer, ToolCallRecord};
4use super::startup::auto_consolidate_knowledge;
5use super::{ctx_compress, ctx_share};
6
7impl LeanCtxServer {
8    /// Records a tool call's token savings without timing information.
9    pub async fn record_call(
10        &self,
11        tool: &str,
12        original: usize,
13        saved: usize,
14        mode: Option<String>,
15    ) {
16        self.record_call_with_timing(tool, original, saved, mode, 0)
17            .await;
18    }
19
20    /// Records a tool call like `record_call`, but includes an optional file path for observability.
21    pub async fn record_call_with_path(
22        &self,
23        tool: &str,
24        original: usize,
25        saved: usize,
26        mode: Option<String>,
27        path: Option<&str>,
28    ) {
29        self.record_call_with_timing_inner(tool, original, saved, mode, 0, path)
30            .await;
31    }
32
33    /// Records a tool call's token savings, duration, and emits events and stats.
34    pub async fn record_call_with_timing(
35        &self,
36        tool: &str,
37        original: usize,
38        saved: usize,
39        mode: Option<String>,
40        duration_ms: u64,
41    ) {
42        self.record_call_with_timing_inner(tool, original, saved, mode, duration_ms, None)
43            .await;
44    }
45
46    async fn record_call_with_timing_inner(
47        &self,
48        tool: &str,
49        original: usize,
50        saved: usize,
51        mode: Option<String>,
52        duration_ms: u64,
53        path: Option<&str>,
54    ) {
55        let ts = chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string();
56        let mut calls = self.tool_calls.write().await;
57        calls.push(ToolCallRecord {
58            tool: tool.to_string(),
59            original_tokens: original,
60            saved_tokens: saved,
61            mode: mode.clone(),
62            duration_ms,
63            timestamp: ts.clone(),
64        });
65
66        const MAX_TOOL_CALL_RECORDS: usize = 500;
67        if calls.len() > MAX_TOOL_CALL_RECORDS {
68            let excess = calls.len() - MAX_TOOL_CALL_RECORDS;
69            calls.drain(..excess);
70        }
71
72        if duration_ms > 0 {
73            Self::append_tool_call_log(tool, duration_ms, original, saved, mode.as_deref(), &ts);
74        }
75
76        crate::core::events::emit_tool_call(
77            tool,
78            original as u64,
79            saved as u64,
80            mode.clone(),
81            duration_ms,
82            path.map(ToString::to_string),
83        );
84
85        let output_tokens = original.saturating_sub(saved);
86        crate::core::stats::record(tool, original, output_tokens);
87
88        let mut session = self.session.write().await;
89        session.record_tool_call(saved as u64, original as u64);
90        if tool == "ctx_shell" {
91            session.record_command();
92        }
93        let pending_save = if session.should_save() {
94            session.prepare_save().ok()
95        } else {
96            None
97        };
98        drop(calls);
99        drop(session);
100
101        if let Some(prepared) = pending_save {
102            tokio::task::spawn_blocking(move || {
103                let _ = prepared.write_to_disk();
104            });
105        }
106
107        self.write_mcp_live_stats().await;
108    }
109
110    /// Returns true if over an hour has passed since the last tool call.
111    pub async fn is_prompt_cache_stale(&self) -> bool {
112        let last = *self.last_call.read().await;
113        last.elapsed().as_secs() > 3600
114    }
115
116    /// Promotes lightweight read modes to richer ones when the prompt cache is stale.
117    pub fn upgrade_mode_if_stale(mode: &str, stale: bool) -> &str {
118        if !stale {
119            return mode;
120        }
121        match mode {
122            "full" => "full",
123            "map" => "signatures",
124            m => m,
125        }
126    }
127
128    /// Increments the call counter and returns true if a checkpoint is due.
129    pub fn increment_and_check(&self) -> bool {
130        let count = self.call_count.fetch_add(1, Ordering::Relaxed) + 1;
131        let interval = Self::checkpoint_interval_effective();
132        interval > 0 && count.is_multiple_of(interval)
133    }
134
135    /// Generates a compressed context checkpoint with session state and multi-agent sync.
136    pub async fn auto_checkpoint(&self) -> Option<String> {
137        let cache = self.cache.read().await;
138        if cache.get_all_entries().is_empty() {
139            return None;
140        }
141        let complexity = crate::core::adaptive::classify_from_context(&cache);
142        let checkpoint = ctx_compress::handle(&cache, false, CrpMode::effective());
143        drop(cache);
144
145        let mut session = self.session.write().await;
146        let _ = session.save();
147        let session_summary = session.format_compact();
148        let has_insights = !session.findings.is_empty() || !session.decisions.is_empty();
149        let project_root = session.project_root.clone();
150        drop(session);
151
152        if has_insights {
153            if let Some(ref root) = project_root {
154                let root = root.clone();
155                std::thread::spawn(move || {
156                    auto_consolidate_knowledge(&root);
157                });
158            }
159        }
160
161        let multi_agent_block = self
162            .auto_multi_agent_checkpoint(project_root.as_ref())
163            .await;
164
165        self.record_call("ctx_compress", 0, 0, Some("auto".to_string()))
166            .await;
167
168        self.record_cep_snapshot().await;
169
170        Some(format!(
171            "{checkpoint}\n\n--- SESSION STATE ---\n{session_summary}\n\n{}{multi_agent_block}",
172            complexity.instruction_suffix()
173        ))
174    }
175
176    async fn auto_multi_agent_checkpoint(&self, project_root: Option<&String>) -> String {
177        let Some(root) = project_root else {
178            return String::new();
179        };
180
181        let registry = crate::core::agents::AgentRegistry::load_or_create();
182        let active = registry.list_active(Some(root));
183        if active.len() <= 1 {
184            return String::new();
185        }
186
187        let agent_id = self.agent_id.read().await;
188        let my_id = match agent_id.as_deref() {
189            Some(id) => id.to_string(),
190            None => return String::new(),
191        };
192        drop(agent_id);
193
194        let cache = self.cache.read().await;
195        let entries = cache.get_all_entries();
196        if !entries.is_empty() {
197            let mut by_access: Vec<_> = entries.iter().collect();
198            by_access.sort_by_key(|x| std::cmp::Reverse(x.1.read_count));
199            let top_paths: Vec<&str> = by_access
200                .iter()
201                .take(5)
202                .map(|(key, _)| key.as_str())
203                .collect();
204            let paths_csv = top_paths.join(",");
205
206            let _ = ctx_share::handle(
207                "push",
208                Some(&my_id),
209                None,
210                Some(&paths_csv),
211                None,
212                &cache,
213                root,
214            );
215        }
216        drop(cache);
217
218        let pending_count = registry
219            .scratchpad
220            .iter()
221            .filter(|e| !e.read_by.contains(&my_id) && e.from_agent != my_id)
222            .count();
223
224        let shared_dir = crate::core::data_dir::lean_ctx_data_dir()
225            .unwrap_or_default()
226            .join("agents")
227            .join("shared");
228        let shared_count = if shared_dir.exists() {
229            std::fs::read_dir(&shared_dir).map_or(0, std::iter::Iterator::count)
230        } else {
231            0
232        };
233
234        let agent_names: Vec<String> = active
235            .iter()
236            .map(|a| {
237                let role = a.role.as_deref().unwrap_or(&a.agent_type);
238                format!("{role}({})", &a.agent_id[..8.min(a.agent_id.len())])
239            })
240            .collect();
241
242        format!(
243            "\n\n--- MULTI-AGENT SYNC ---\nAgents: {} | Pending msgs: {} | Shared contexts: {}\nAuto-shared top-5 cached files.\n--- END SYNC ---",
244            agent_names.join(", "),
245            pending_count,
246            shared_count,
247        )
248    }
249
250    /// Appends a tool call entry to the rotating `tool-calls.log` file.
251    pub fn append_tool_call_log(
252        tool: &str,
253        duration_ms: u64,
254        original: usize,
255        saved: usize,
256        mode: Option<&str>,
257        timestamp: &str,
258    ) {
259        const MAX_LOG_LINES: usize = 50;
260        if let Ok(dir) = crate::core::data_dir::lean_ctx_data_dir() {
261            let log_path = dir.join("tool-calls.log");
262            let mode_str = mode.unwrap_or("-");
263            let slow = if duration_ms > 5000 { " **SLOW**" } else { "" };
264            let line = format!(
265                "{timestamp}\t{tool}\t{duration_ms}ms\torig={original}\tsaved={saved}\tmode={mode_str}{slow}\n"
266            );
267
268            let mut lines: Vec<String> = std::fs::read_to_string(&log_path)
269                .unwrap_or_default()
270                .lines()
271                .map(std::string::ToString::to_string)
272                .collect();
273
274            lines.push(line.trim_end().to_string());
275            if lines.len() > MAX_LOG_LINES {
276                lines.drain(0..lines.len() - MAX_LOG_LINES);
277            }
278
279            let _ = std::fs::write(&log_path, lines.join("\n") + "\n");
280        }
281    }
282
283    fn compute_cep_stats(
284        calls: &[ToolCallRecord],
285        stats: &crate::core::cache::CacheStats,
286        complexity: &crate::core::adaptive::TaskComplexity,
287    ) -> CepComputedStats {
288        let total_original: u64 = calls.iter().map(|c| c.original_tokens as u64).sum();
289        let total_saved: u64 = calls.iter().map(|c| c.saved_tokens as u64).sum();
290        let total_compressed = total_original.saturating_sub(total_saved);
291        let compression_rate = if total_original > 0 {
292            total_saved as f64 / total_original as f64
293        } else {
294            0.0
295        };
296
297        let modes_used: std::collections::HashSet<&str> =
298            calls.iter().filter_map(|c| c.mode.as_deref()).collect();
299        let mode_diversity = (modes_used.len() as f64 / 10.0).min(1.0);
300        let cache_util = stats.hit_rate() / 100.0;
301        let cep_score = cache_util * 0.3 + mode_diversity * 0.2 + compression_rate * 0.5;
302
303        let mut mode_counts: std::collections::HashMap<String, u64> =
304            std::collections::HashMap::new();
305        for call in calls {
306            if let Some(ref mode) = call.mode {
307                *mode_counts.entry(mode.clone()).or_insert(0) += 1;
308            }
309        }
310
311        CepComputedStats {
312            cep_score: (cep_score * 100.0).round() as u32,
313            cache_util: (cache_util * 100.0).round() as u32,
314            mode_diversity: (mode_diversity * 100.0).round() as u32,
315            compression_rate: (compression_rate * 100.0).round() as u32,
316            total_original,
317            total_compressed,
318            total_saved,
319            mode_counts,
320            complexity: format!("{complexity:?}"),
321            cache_hits: stats.cache_hits,
322            total_reads: stats.total_reads,
323            tool_call_count: calls.len() as u64,
324        }
325    }
326
327    async fn write_mcp_live_stats(&self) {
328        let count = self.call_count.load(Ordering::Relaxed);
329        if count > 1 && !count.is_multiple_of(5) {
330            return;
331        }
332
333        let cache = self.cache.read().await;
334        let calls = self.tool_calls.read().await;
335        let stats = cache.get_stats();
336        let complexity = crate::core::adaptive::classify_from_context(&cache);
337
338        let cs = Self::compute_cep_stats(&calls, stats, &complexity);
339        let started_at = calls
340            .first()
341            .map(|c| c.timestamp.clone())
342            .unwrap_or_default();
343
344        drop(cache);
345        drop(calls);
346        let live = serde_json::json!({
347            "cep_score": cs.cep_score,
348            "cache_utilization": cs.cache_util,
349            "mode_diversity": cs.mode_diversity,
350            "compression_rate": cs.compression_rate,
351            "task_complexity": cs.complexity,
352            "files_cached": cs.total_reads,
353            "total_reads": cs.total_reads,
354            "cache_hits": cs.cache_hits,
355            "tokens_saved": cs.total_saved,
356            "tokens_original": cs.total_original,
357            "tool_calls": cs.tool_call_count,
358            "started_at": started_at,
359            "updated_at": chrono::Local::now().to_rfc3339(),
360        });
361
362        if let Ok(dir) = crate::core::data_dir::lean_ctx_data_dir() {
363            let _ = std::fs::write(dir.join("mcp-live.json"), live.to_string());
364        }
365    }
366
367    /// Persists a CEP (Context Efficiency Protocol) score snapshot for analytics.
368    pub async fn record_cep_snapshot(&self) {
369        let cache = self.cache.read().await;
370        let calls = self.tool_calls.read().await;
371        let stats = cache.get_stats();
372        let complexity = crate::core::adaptive::classify_from_context(&cache);
373
374        let cs = Self::compute_cep_stats(&calls, stats, &complexity);
375
376        drop(cache);
377        drop(calls);
378
379        crate::core::stats::record_cep_session(
380            cs.cep_score,
381            cs.cache_hits,
382            cs.total_reads,
383            cs.total_original,
384            cs.total_compressed,
385            &cs.mode_counts,
386            cs.tool_call_count,
387            &cs.complexity,
388        );
389    }
390}