1use std::sync::Arc;
2
3use rmcp::handler::server::ServerHandler;
4use rmcp::model::*;
5use rmcp::service::{RequestContext, RoleServer};
6use rmcp::ErrorData;
7use serde_json::{json, Map, Value};
8
9use crate::tools::{CrpMode, LeanCtxServer};
10
11impl ServerHandler for LeanCtxServer {
15 fn get_info(&self) -> ServerInfo {
16 let capabilities = ServerCapabilities::builder().enable_tools().build();
17
18 let instructions = build_instructions(self.crp_mode);
19
20 InitializeResult::new(capabilities)
21 .with_server_info(Implementation::new("lean-ctx", "2.16.2"))
22 .with_instructions(instructions)
23 }
24
25 async fn initialize(
26 &self,
27 request: InitializeRequestParams,
28 _context: RequestContext<RoleServer>,
29 ) -> Result<InitializeResult, ErrorData> {
30 let name = request.client_info.name.clone();
31 tracing::info!("MCP client connected: {:?}", name);
32 *self.client_name.write().await = name.clone();
33
34 tokio::task::spawn_blocking(|| {
35 if let Some(home) = dirs::home_dir() {
36 let _ = crate::rules_inject::inject_all_rules(&home);
37 }
38 crate::hooks::refresh_installed_hooks();
39 crate::core::version_check::check_background();
40 });
41
42 let instructions = build_instructions_with_client(self.crp_mode, &name);
43 let capabilities = ServerCapabilities::builder().enable_tools().build();
44
45 Ok(InitializeResult::new(capabilities)
46 .with_server_info(Implementation::new("lean-ctx", "2.16.2"))
47 .with_instructions(instructions))
48 }
49
50 async fn list_tools(
51 &self,
52 _request: Option<PaginatedRequestParams>,
53 _context: RequestContext<RoleServer>,
54 ) -> Result<ListToolsResult, ErrorData> {
55 if should_use_unified(&self.client_name.read().await) {
56 return Ok(ListToolsResult {
57 tools: unified_tool_defs(),
58 ..Default::default()
59 });
60 }
61
62 Ok(ListToolsResult {
63 tools: vec![
64 tool_def(
65 "ctx_read",
66 "Read file (cached, compressed). Re-reads ~13 tok. Auto-selects optimal mode. \
67Modes: full|map|signatures|diff|aggressive|entropy|task|reference|lines:N-M. fresh=true re-reads.",
68 json!({
69 "type": "object",
70 "properties": {
71 "path": { "type": "string", "description": "Absolute file path to read" },
72 "mode": {
73 "type": "string",
74 "description": "Compression mode (default: full). Use 'map' for context-only files. For line ranges: 'lines:N-M' (e.g. 'lines:400-500')."
75 },
76 "start_line": {
77 "type": "integer",
78 "description": "Read from this line number to end of file. Bypasses cache stub — always returns actual content."
79 },
80 "fresh": {
81 "type": "boolean",
82 "description": "Bypass cache and force a full re-read. Use when running as a subagent that may not have the parent's context."
83 }
84 },
85 "required": ["path"]
86 }),
87 ),
88 tool_def(
89 "ctx_multi_read",
90 "Batch read files in one call. Same modes as ctx_read.",
91 json!({
92 "type": "object",
93 "properties": {
94 "paths": {
95 "type": "array",
96 "items": { "type": "string" },
97 "description": "Absolute file paths to read, in order"
98 },
99 "mode": {
100 "type": "string",
101 "enum": ["full", "signatures", "map", "diff", "aggressive", "entropy"],
102 "description": "Compression mode (default: full)"
103 }
104 },
105 "required": ["paths"]
106 }),
107 ),
108 tool_def(
109 "ctx_tree",
110 "Directory listing with file counts.",
111 json!({
112 "type": "object",
113 "properties": {
114 "path": { "type": "string", "description": "Directory path (default: .)" },
115 "depth": { "type": "integer", "description": "Max depth (default: 3)" },
116 "show_hidden": { "type": "boolean", "description": "Show hidden files" }
117 }
118 }),
119 ),
120 tool_def(
121 "ctx_shell",
122 "Run shell command (compressed output, 90+ patterns). Use raw=true to skip compression and get full output.",
123 json!({
124 "type": "object",
125 "properties": {
126 "command": { "type": "string", "description": "Shell command to execute" },
127 "raw": { "type": "boolean", "description": "Skip compression, return full uncompressed output. Use for small outputs or when full detail is critical." }
128 },
129 "required": ["command"]
130 }),
131 ),
132 tool_def(
133 "ctx_search",
134 "Regex code search (.gitignore aware, compact results).",
135 json!({
136 "type": "object",
137 "properties": {
138 "pattern": { "type": "string", "description": "Regex pattern" },
139 "path": { "type": "string", "description": "Directory to search" },
140 "ext": { "type": "string", "description": "File extension filter" },
141 "max_results": { "type": "integer", "description": "Max results (default: 20)" },
142 "ignore_gitignore": { "type": "boolean", "description": "Set true to scan ALL files including .gitignore'd paths (default: false)" }
143 },
144 "required": ["pattern"]
145 }),
146 ),
147 tool_def(
148 "ctx_compress",
149 "Context checkpoint for long conversations.",
150 json!({
151 "type": "object",
152 "properties": {
153 "include_signatures": { "type": "boolean", "description": "Include signatures (default: true)" }
154 }
155 }),
156 ),
157 tool_def(
158 "ctx_benchmark",
159 "Benchmark compression modes for a file or project.",
160 json!({
161 "type": "object",
162 "properties": {
163 "path": { "type": "string", "description": "File path (action=file) or project directory (action=project)" },
164 "action": { "type": "string", "description": "file (default) or project", "default": "file" },
165 "format": { "type": "string", "description": "Output format for project benchmark: terminal, markdown, json", "default": "terminal" }
166 },
167 "required": ["path"]
168 }),
169 ),
170 tool_def(
171 "ctx_metrics",
172 "Session token stats, cache rates, per-tool savings.",
173 json!({
174 "type": "object",
175 "properties": {}
176 }),
177 ),
178 tool_def(
179 "ctx_analyze",
180 "Entropy analysis — recommends optimal compression mode for a file.",
181 json!({
182 "type": "object",
183 "properties": {
184 "path": { "type": "string", "description": "File path to analyze" }
185 },
186 "required": ["path"]
187 }),
188 ),
189 tool_def(
190 "ctx_cache",
191 "Cache ops: status|clear|invalidate.",
192 json!({
193 "type": "object",
194 "properties": {
195 "action": {
196 "type": "string",
197 "enum": ["status", "clear", "invalidate"],
198 "description": "Cache operation to perform"
199 },
200 "path": {
201 "type": "string",
202 "description": "File path (required for 'invalidate' action)"
203 }
204 },
205 "required": ["action"]
206 }),
207 ),
208 tool_def(
209 "ctx_discover",
210 "Find missed compression opportunities in shell history.",
211 json!({
212 "type": "object",
213 "properties": {
214 "limit": {
215 "type": "integer",
216 "description": "Max number of command types to show (default: 15)"
217 }
218 }
219 }),
220 ),
221 tool_def(
222 "ctx_smart_read",
223 "Auto-select optimal read mode for a file.",
224 json!({
225 "type": "object",
226 "properties": {
227 "path": { "type": "string", "description": "Absolute file path to read" }
228 },
229 "required": ["path"]
230 }),
231 ),
232 tool_def(
233 "ctx_delta",
234 "Incremental diff — sends only changed lines since last read.",
235 json!({
236 "type": "object",
237 "properties": {
238 "path": { "type": "string", "description": "Absolute file path" }
239 },
240 "required": ["path"]
241 }),
242 ),
243 tool_def(
244 "ctx_dedup",
245 "Cross-file dedup: analyze or apply shared block references.",
246 json!({
247 "type": "object",
248 "properties": {
249 "action": {
250 "type": "string",
251 "description": "analyze (default) or apply (register shared blocks for auto-dedup in ctx_read)",
252 "default": "analyze"
253 }
254 }
255 }),
256 ),
257 tool_def(
258 "ctx_fill",
259 "Budget-aware context fill — auto-selects compression per file within token limit.",
260 json!({
261 "type": "object",
262 "properties": {
263 "paths": {
264 "type": "array",
265 "items": { "type": "string" },
266 "description": "File paths to consider"
267 },
268 "budget": {
269 "type": "integer",
270 "description": "Maximum token budget to fill"
271 }
272 },
273 "required": ["paths", "budget"]
274 }),
275 ),
276 tool_def(
277 "ctx_intent",
278 "Intent detection — auto-reads relevant files based on task description.",
279 json!({
280 "type": "object",
281 "properties": {
282 "query": { "type": "string", "description": "Natural language description of the task" },
283 "project_root": { "type": "string", "description": "Project root directory (default: .)" }
284 },
285 "required": ["query"]
286 }),
287 ),
288 tool_def(
289 "ctx_response",
290 "Compress LLM response text (remove filler, apply TDD).",
291 json!({
292 "type": "object",
293 "properties": {
294 "text": { "type": "string", "description": "Response text to compress" }
295 },
296 "required": ["text"]
297 }),
298 ),
299 tool_def(
300 "ctx_context",
301 "Session context overview — cached files, seen files, session state.",
302 json!({
303 "type": "object",
304 "properties": {}
305 }),
306 ),
307 tool_def(
308 "ctx_graph",
309 "Code dependency graph. Actions: build (index project), related (find files connected to path), \
310symbol (lookup definition/usages as file::name), impact (blast radius of changes to path), status (index stats).",
311 json!({
312 "type": "object",
313 "properties": {
314 "action": {
315 "type": "string",
316 "enum": ["build", "related", "symbol", "impact", "status"],
317 "description": "Graph operation: build, related, symbol, impact, status"
318 },
319 "path": {
320 "type": "string",
321 "description": "File path (related/impact) or file::symbol_name (symbol)"
322 },
323 "project_root": {
324 "type": "string",
325 "description": "Project root directory (default: .)"
326 }
327 },
328 "required": ["action"]
329 }),
330 ),
331 tool_def(
332 "ctx_session",
333 "Cross-session memory (CCP). Actions: load (restore previous session ~400 tok), \
334save, status, task (set current task), finding (record discovery), decision (record choice), \
335reset, list (show sessions), cleanup.",
336 json!({
337 "type": "object",
338 "properties": {
339 "action": {
340 "type": "string",
341 "enum": ["status", "load", "save", "task", "finding", "decision", "reset", "list", "cleanup"],
342 "description": "Session operation to perform"
343 },
344 "value": {
345 "type": "string",
346 "description": "Value for task/finding/decision actions"
347 },
348 "session_id": {
349 "type": "string",
350 "description": "Session ID for load action (default: latest)"
351 }
352 },
353 "required": ["action"]
354 }),
355 ),
356 tool_def(
357 "ctx_knowledge",
358 "Persistent project knowledge (survives sessions). Actions: remember (store fact with category+key+value), \
359recall (search by query), pattern (record naming/structure pattern), consolidate (extract session findings into knowledge), \
360status (list all), remove, export.",
361 json!({
362 "type": "object",
363 "properties": {
364 "action": {
365 "type": "string",
366 "enum": ["remember", "recall", "pattern", "consolidate", "status", "remove", "export"],
367 "description": "Knowledge operation to perform"
368 },
369 "category": {
370 "type": "string",
371 "description": "Fact category (architecture, api, testing, deployment, conventions, dependencies)"
372 },
373 "key": {
374 "type": "string",
375 "description": "Fact key/identifier (e.g. 'auth-method', 'db-engine', 'test-framework')"
376 },
377 "value": {
378 "type": "string",
379 "description": "Fact value or pattern description"
380 },
381 "query": {
382 "type": "string",
383 "description": "Search query for recall action (matches against category, key, and value)"
384 },
385 "pattern_type": {
386 "type": "string",
387 "description": "Pattern type for pattern action (naming, structure, testing, error-handling)"
388 },
389 "examples": {
390 "type": "array",
391 "items": { "type": "string" },
392 "description": "Examples for pattern action"
393 },
394 "confidence": {
395 "type": "number",
396 "description": "Confidence score 0.0-1.0 for remember action (default: 0.8)"
397 }
398 },
399 "required": ["action"]
400 }),
401 ),
402 tool_def(
403 "ctx_agent",
404 "Multi-agent coordination (shared message bus). Actions: register (join with agent_type+role), \
405post (broadcast or direct message with category), read (poll messages), status (update state: active|idle|finished), \
406list, info.",
407 json!({
408 "type": "object",
409 "properties": {
410 "action": {
411 "type": "string",
412 "enum": ["register", "list", "post", "read", "status", "info"],
413 "description": "Agent operation to perform"
414 },
415 "agent_type": {
416 "type": "string",
417 "description": "Agent type for register (cursor, claude, codex, gemini, subagent)"
418 },
419 "role": {
420 "type": "string",
421 "description": "Agent role (dev, review, test, plan)"
422 },
423 "message": {
424 "type": "string",
425 "description": "Message text for post action, or status detail for status action"
426 },
427 "category": {
428 "type": "string",
429 "description": "Message category for post (finding, warning, request, status)"
430 },
431 "to_agent": {
432 "type": "string",
433 "description": "Target agent ID for direct message (omit for broadcast)"
434 },
435 "status": {
436 "type": "string",
437 "enum": ["active", "idle", "finished"],
438 "description": "New status for status action"
439 }
440 },
441 "required": ["action"]
442 }),
443 ),
444 tool_def(
445 "ctx_overview",
446 "Task-relevant project map — use at session start.",
447 json!({
448 "type": "object",
449 "properties": {
450 "task": {
451 "type": "string",
452 "description": "Task description for relevance scoring (e.g. 'fix auth bug in login flow')"
453 },
454 "path": {
455 "type": "string",
456 "description": "Project root directory (default: .)"
457 }
458 }
459 }),
460 ),
461 tool_def(
462 "ctx_preload",
463 "Proactive context loader — caches task-relevant files, returns L-curve-optimized summary (~50-100 tokens vs ~5000 for individual reads).",
464 json!({
465 "type": "object",
466 "properties": {
467 "task": {
468 "type": "string",
469 "description": "Task description (e.g. 'fix auth bug in validate_token')"
470 },
471 "path": {
472 "type": "string",
473 "description": "Project root (default: .)"
474 }
475 },
476 "required": ["task"]
477 }),
478 ),
479 tool_def(
480 "ctx_wrapped",
481 "Savings report card. Periods: week|month|all.",
482 json!({
483 "type": "object",
484 "properties": {
485 "period": {
486 "type": "string",
487 "enum": ["week", "month", "all"],
488 "description": "Report period (default: week)"
489 }
490 }
491 }),
492 ),
493 tool_def(
494 "ctx_semantic_search",
495 "BM25 code search by meaning. action=reindex to rebuild.",
496 json!({
497 "type": "object",
498 "properties": {
499 "query": { "type": "string", "description": "Natural language search query" },
500 "path": { "type": "string", "description": "Project root to search (default: .)" },
501 "top_k": { "type": "integer", "description": "Number of results (default: 10)" },
502 "action": { "type": "string", "description": "reindex to rebuild index" }
503 },
504 "required": ["query"]
505 }),
506 ),
507 ],
508 ..Default::default()
509 })
510 }
511
512 async fn call_tool(
513 &self,
514 request: CallToolRequestParams,
515 _context: RequestContext<RoleServer>,
516 ) -> Result<CallToolResult, ErrorData> {
517 self.check_idle_expiry().await;
518
519 let original_name = request.name.as_ref().to_string();
520 let (resolved_name, resolved_args) = if original_name == "ctx" {
521 let sub = request
522 .arguments
523 .as_ref()
524 .and_then(|a| a.get("tool"))
525 .and_then(|v| v.as_str())
526 .map(|s| s.to_string())
527 .ok_or_else(|| {
528 ErrorData::invalid_params("'tool' is required for ctx meta-tool", None)
529 })?;
530 let tool_name = if sub.starts_with("ctx_") {
531 sub
532 } else {
533 format!("ctx_{sub}")
534 };
535 let mut args = request.arguments.unwrap_or_default();
536 args.remove("tool");
537 (tool_name, Some(args))
538 } else {
539 (original_name, request.arguments)
540 };
541 let name = resolved_name.as_str();
542 let args = &resolved_args;
543
544 let auto_context = {
545 let task = {
546 let session = self.session.read().await;
547 session.task.as_ref().map(|t| t.description.clone())
548 };
549 let project_root = {
550 let session = self.session.read().await;
551 session.project_root.clone()
552 };
553 let mut cache = self.cache.write().await;
554 crate::tools::autonomy::session_lifecycle_pre_hook(
555 &self.autonomy,
556 name,
557 &mut cache,
558 task.as_deref(),
559 project_root.as_deref(),
560 self.crp_mode,
561 )
562 };
563
564 let tool_start = std::time::Instant::now();
565 let result_text = match name {
566 "ctx_read" => {
567 let path = get_str(args, "path")
568 .ok_or_else(|| ErrorData::invalid_params("path is required", None))?;
569 let current_task = {
570 let session = self.session.read().await;
571 session.task.as_ref().map(|t| t.description.clone())
572 };
573 let task_ref = current_task.as_deref();
574 let mut mode = match get_str(args, "mode") {
575 Some(m) => m,
576 None => {
577 let cache = self.cache.read().await;
578 crate::tools::ctx_smart_read::select_mode_with_task(&cache, &path, task_ref)
579 }
580 };
581 let fresh = get_bool(args, "fresh").unwrap_or(false);
582 let start_line = get_int(args, "start_line");
583 if let Some(sl) = start_line {
584 let sl = sl.max(1_i64);
585 mode = format!("lines:{sl}-999999");
586 }
587 let stale = self.is_prompt_cache_stale().await;
588 let effective_mode = LeanCtxServer::upgrade_mode_if_stale(&mode, stale).to_string();
589 let mut cache = self.cache.write().await;
590 let output = if fresh {
591 crate::tools::ctx_read::handle_fresh_with_task(
592 &mut cache,
593 &path,
594 &effective_mode,
595 self.crp_mode,
596 task_ref,
597 )
598 } else {
599 crate::tools::ctx_read::handle_with_task(
600 &mut cache,
601 &path,
602 &effective_mode,
603 self.crp_mode,
604 task_ref,
605 )
606 };
607 let stale_note = if effective_mode != mode {
608 format!("[cache stale, {mode}→{effective_mode}]\n")
609 } else {
610 String::new()
611 };
612 let original = cache.get(&path).map_or(0, |e| e.original_tokens);
613 let output_tokens = crate::core::tokens::count_tokens(&output);
614 let saved = original.saturating_sub(output_tokens);
615 let is_cache_hit = output.contains(" cached ");
616 let output = format!("{stale_note}{output}");
617 let file_ref = cache.file_ref_map().get(&path).cloned();
618 drop(cache);
619 {
620 let mut session = self.session.write().await;
621 session.touch_file(&path, file_ref.as_deref(), &effective_mode, original);
622 if is_cache_hit {
623 session.record_cache_hit();
624 }
625 if session.project_root.is_none() {
626 if let Some(root) = detect_project_root(&path) {
627 session.project_root = Some(root.clone());
628 let mut current = self.agent_id.write().await;
629 if current.is_none() {
630 let mut registry =
631 crate::core::agents::AgentRegistry::load_or_create();
632 registry.cleanup_stale(24);
633 let id = registry.register("mcp", None, &root);
634 let _ = registry.save();
635 *current = Some(id);
636 }
637 }
638 }
639 }
640 self.record_call("ctx_read", original, saved, Some(mode.clone()))
641 .await;
642 {
643 let sig =
644 crate::core::mode_predictor::FileSignature::from_path(&path, original);
645 let density = if output_tokens > 0 {
646 original as f64 / output_tokens as f64
647 } else {
648 1.0
649 };
650 let outcome = crate::core::mode_predictor::ModeOutcome {
651 mode: mode.clone(),
652 tokens_in: original,
653 tokens_out: output_tokens,
654 density: density.min(1.0),
655 };
656 let mut predictor = crate::core::mode_predictor::ModePredictor::new();
657 predictor.record(sig, outcome);
658 predictor.save();
659
660 let ext = std::path::Path::new(&path)
661 .extension()
662 .and_then(|e| e.to_str())
663 .unwrap_or("")
664 .to_string();
665 let thresholds = crate::core::adaptive_thresholds::thresholds_for_path(&path);
666 let cache = self.cache.read().await;
667 let stats = cache.get_stats();
668 let feedback_outcome = crate::core::feedback::CompressionOutcome {
669 session_id: format!("{}", std::process::id()),
670 language: ext,
671 entropy_threshold: thresholds.bpe_entropy,
672 jaccard_threshold: thresholds.jaccard,
673 total_turns: stats.total_reads as u32,
674 tokens_saved: saved as u64,
675 tokens_original: original as u64,
676 cache_hits: stats.cache_hits as u32,
677 total_reads: stats.total_reads as u32,
678 task_completed: true,
679 timestamp: chrono::Local::now().to_rfc3339(),
680 };
681 drop(cache);
682 let mut store = crate::core::feedback::FeedbackStore::load();
683 store.record_outcome(feedback_outcome);
684 }
685 output
686 }
687 "ctx_multi_read" => {
688 let paths = get_str_array(args, "paths")
689 .ok_or_else(|| ErrorData::invalid_params("paths array is required", None))?;
690 let mode = get_str(args, "mode").unwrap_or_else(|| "full".to_string());
691 let current_task = {
692 let session = self.session.read().await;
693 session.task.as_ref().map(|t| t.description.clone())
694 };
695 let mut cache = self.cache.write().await;
696 let output = crate::tools::ctx_multi_read::handle_with_task(
697 &mut cache,
698 &paths,
699 &mode,
700 self.crp_mode,
701 current_task.as_deref(),
702 );
703 let mut total_original: usize = 0;
704 for path in &paths {
705 total_original = total_original
706 .saturating_add(cache.get(path).map(|e| e.original_tokens).unwrap_or(0));
707 }
708 let tokens = crate::core::tokens::count_tokens(&output);
709 drop(cache);
710 self.record_call(
711 "ctx_multi_read",
712 total_original,
713 total_original.saturating_sub(tokens),
714 Some(mode),
715 )
716 .await;
717 output
718 }
719 "ctx_tree" => {
720 let path = get_str(args, "path").unwrap_or_else(|| ".".to_string());
721 let depth = get_int(args, "depth").unwrap_or(3) as usize;
722 let show_hidden = get_bool(args, "show_hidden").unwrap_or(false);
723 let (result, original) = crate::tools::ctx_tree::handle(&path, depth, show_hidden);
724 let sent = crate::core::tokens::count_tokens(&result);
725 let saved = original.saturating_sub(sent);
726 self.record_call("ctx_tree", original, saved, None).await;
727 let savings_note = if saved > 0 {
728 format!("\n[saved {saved} tokens vs native ls]")
729 } else {
730 String::new()
731 };
732 format!("{result}{savings_note}")
733 }
734 "ctx_shell" => {
735 let command = get_str(args, "command")
736 .ok_or_else(|| ErrorData::invalid_params("command is required", None))?;
737 let raw = get_bool(args, "raw").unwrap_or(false)
738 || std::env::var("LEAN_CTX_DISABLED").is_ok();
739 let output = execute_command(&command);
740
741 if raw {
742 let original = crate::core::tokens::count_tokens(&output);
743 self.record_call("ctx_shell", original, 0, None).await;
744 output
745 } else {
746 let result = crate::tools::ctx_shell::handle(&command, &output, self.crp_mode);
747 let original = crate::core::tokens::count_tokens(&output);
748 let sent = crate::core::tokens::count_tokens(&result);
749 let saved = original.saturating_sub(sent);
750 self.record_call("ctx_shell", original, saved, None).await;
751
752 let cfg = crate::core::config::Config::load();
753 let tee_hint = match cfg.tee_mode {
754 crate::core::config::TeeMode::Always => {
755 crate::shell::save_tee(&command, &output)
756 .map(|p| format!("\n[full output: {p}]"))
757 .unwrap_or_default()
758 }
759 crate::core::config::TeeMode::Failures
760 if !output.trim().is_empty() && output.contains("error")
761 || output.contains("Error")
762 || output.contains("ERROR") =>
763 {
764 crate::shell::save_tee(&command, &output)
765 .map(|p| format!("\n[full output: {p}]"))
766 .unwrap_or_default()
767 }
768 _ => String::new(),
769 };
770
771 let savings_note = if saved > 0 {
772 format!("\n[saved {saved} tokens vs native Shell]")
773 } else {
774 String::new()
775 };
776 format!("{result}{savings_note}{tee_hint}")
777 }
778 }
779 "ctx_search" => {
780 let pattern = get_str(args, "pattern")
781 .ok_or_else(|| ErrorData::invalid_params("pattern is required", None))?;
782 let path = get_str(args, "path").unwrap_or_else(|| ".".to_string());
783 let ext = get_str(args, "ext");
784 let max = get_int(args, "max_results").unwrap_or(20) as usize;
785 let no_gitignore = get_bool(args, "ignore_gitignore").unwrap_or(false);
786 let crp = self.crp_mode;
787 let respect = !no_gitignore;
788 let search_result = tokio::time::timeout(
789 std::time::Duration::from_secs(30),
790 tokio::task::spawn_blocking(move || {
791 crate::tools::ctx_search::handle(
792 &pattern,
793 &path,
794 ext.as_deref(),
795 max,
796 crp,
797 respect,
798 )
799 }),
800 )
801 .await;
802 let (result, original) = match search_result {
803 Ok(Ok(r)) => r,
804 Ok(Err(e)) => {
805 return Err(ErrorData::internal_error(
806 format!("search task failed: {e}"),
807 None,
808 ))
809 }
810 Err(_) => {
811 let msg = "ctx_search timed out after 30s. Try narrowing the search:\n\
812 • Use a more specific pattern\n\
813 • Specify ext= to limit file types\n\
814 • Specify a subdirectory in path=";
815 self.record_call("ctx_search", 0, 0, None).await;
816 return Ok(CallToolResult::success(vec![Content::text(msg)]));
817 }
818 };
819 let sent = crate::core::tokens::count_tokens(&result);
820 let saved = original.saturating_sub(sent);
821 self.record_call("ctx_search", original, saved, None).await;
822 let savings_note = if saved > 0 {
823 format!("\n[saved {saved} tokens vs native Grep]")
824 } else {
825 String::new()
826 };
827 format!("{result}{savings_note}")
828 }
829 "ctx_compress" => {
830 let include_sigs = get_bool(args, "include_signatures").unwrap_or(true);
831 let cache = self.cache.read().await;
832 let result =
833 crate::tools::ctx_compress::handle(&cache, include_sigs, self.crp_mode);
834 drop(cache);
835 self.record_call("ctx_compress", 0, 0, None).await;
836 result
837 }
838 "ctx_benchmark" => {
839 let path = get_str(args, "path")
840 .ok_or_else(|| ErrorData::invalid_params("path is required", None))?;
841 let action = get_str(args, "action").unwrap_or_default();
842 let result = if action == "project" {
843 let fmt = get_str(args, "format").unwrap_or_default();
844 let bench = crate::core::benchmark::run_project_benchmark(&path);
845 match fmt.as_str() {
846 "json" => crate::core::benchmark::format_json(&bench),
847 "markdown" | "md" => crate::core::benchmark::format_markdown(&bench),
848 _ => crate::core::benchmark::format_terminal(&bench),
849 }
850 } else {
851 crate::tools::ctx_benchmark::handle(&path, self.crp_mode)
852 };
853 self.record_call("ctx_benchmark", 0, 0, None).await;
854 result
855 }
856 "ctx_metrics" => {
857 let cache = self.cache.read().await;
858 let calls = self.tool_calls.read().await;
859 let result = crate::tools::ctx_metrics::handle(&cache, &calls, self.crp_mode);
860 drop(cache);
861 drop(calls);
862 self.record_call("ctx_metrics", 0, 0, None).await;
863 result
864 }
865 "ctx_analyze" => {
866 let path = get_str(args, "path")
867 .ok_or_else(|| ErrorData::invalid_params("path is required", None))?;
868 let result = crate::tools::ctx_analyze::handle(&path, self.crp_mode);
869 self.record_call("ctx_analyze", 0, 0, None).await;
870 result
871 }
872 "ctx_discover" => {
873 let limit = get_int(args, "limit").unwrap_or(15) as usize;
874 let history = crate::cli::load_shell_history_pub();
875 let result = crate::tools::ctx_discover::discover_from_history(&history, limit);
876 self.record_call("ctx_discover", 0, 0, None).await;
877 result
878 }
879 "ctx_smart_read" => {
880 let path = get_str(args, "path")
881 .ok_or_else(|| ErrorData::invalid_params("path is required", None))?;
882 let mut cache = self.cache.write().await;
883 let output = crate::tools::ctx_smart_read::handle(&mut cache, &path, self.crp_mode);
884 let original = cache.get(&path).map_or(0, |e| e.original_tokens);
885 let tokens = crate::core::tokens::count_tokens(&output);
886 drop(cache);
887 self.record_call(
888 "ctx_smart_read",
889 original,
890 original.saturating_sub(tokens),
891 Some("auto".to_string()),
892 )
893 .await;
894 output
895 }
896 "ctx_delta" => {
897 let path = get_str(args, "path")
898 .ok_or_else(|| ErrorData::invalid_params("path is required", None))?;
899 let mut cache = self.cache.write().await;
900 let output = crate::tools::ctx_delta::handle(&mut cache, &path);
901 let original = cache.get(&path).map_or(0, |e| e.original_tokens);
902 let tokens = crate::core::tokens::count_tokens(&output);
903 drop(cache);
904 {
905 let mut session = self.session.write().await;
906 session.mark_modified(&path);
907 }
908 self.record_call(
909 "ctx_delta",
910 original,
911 original.saturating_sub(tokens),
912 Some("delta".to_string()),
913 )
914 .await;
915 output
916 }
917 "ctx_dedup" => {
918 let action = get_str(args, "action").unwrap_or_default();
919 if action == "apply" {
920 let mut cache = self.cache.write().await;
921 let result = crate::tools::ctx_dedup::handle_action(&mut cache, &action);
922 drop(cache);
923 self.record_call("ctx_dedup", 0, 0, None).await;
924 result
925 } else {
926 let cache = self.cache.read().await;
927 let result = crate::tools::ctx_dedup::handle(&cache);
928 drop(cache);
929 self.record_call("ctx_dedup", 0, 0, None).await;
930 result
931 }
932 }
933 "ctx_fill" => {
934 let paths = get_str_array(args, "paths")
935 .ok_or_else(|| ErrorData::invalid_params("paths array is required", None))?;
936 let budget = get_int(args, "budget")
937 .ok_or_else(|| ErrorData::invalid_params("budget is required", None))?
938 as usize;
939 let mut cache = self.cache.write().await;
940 let output =
941 crate::tools::ctx_fill::handle(&mut cache, &paths, budget, self.crp_mode);
942 drop(cache);
943 self.record_call("ctx_fill", 0, 0, Some(format!("budget:{budget}")))
944 .await;
945 output
946 }
947 "ctx_intent" => {
948 let query = get_str(args, "query")
949 .ok_or_else(|| ErrorData::invalid_params("query is required", None))?;
950 let root = get_str(args, "project_root").unwrap_or_else(|| ".".to_string());
951 let mut cache = self.cache.write().await;
952 let output =
953 crate::tools::ctx_intent::handle(&mut cache, &query, &root, self.crp_mode);
954 drop(cache);
955 {
956 let mut session = self.session.write().await;
957 session.set_task(&query, Some("intent"));
958 }
959 self.record_call("ctx_intent", 0, 0, Some("semantic".to_string()))
960 .await;
961 output
962 }
963 "ctx_response" => {
964 let text = get_str(args, "text")
965 .ok_or_else(|| ErrorData::invalid_params("text is required", None))?;
966 let output = crate::tools::ctx_response::handle(&text, self.crp_mode);
967 self.record_call("ctx_response", 0, 0, None).await;
968 output
969 }
970 "ctx_context" => {
971 let cache = self.cache.read().await;
972 let turn = self.call_count.load(std::sync::atomic::Ordering::Relaxed);
973 let result = crate::tools::ctx_context::handle_status(&cache, turn, self.crp_mode);
974 drop(cache);
975 self.record_call("ctx_context", 0, 0, None).await;
976 result
977 }
978 "ctx_graph" => {
979 let action = get_str(args, "action")
980 .ok_or_else(|| ErrorData::invalid_params("action is required", None))?;
981 let path = get_str(args, "path");
982 let root = get_str(args, "project_root").unwrap_or_else(|| ".".to_string());
983 let mut cache = self.cache.write().await;
984 let result = crate::tools::ctx_graph::handle(
985 &action,
986 path.as_deref(),
987 &root,
988 &mut cache,
989 self.crp_mode,
990 );
991 drop(cache);
992 self.record_call("ctx_graph", 0, 0, Some(action)).await;
993 result
994 }
995 "ctx_cache" => {
996 let action = get_str(args, "action")
997 .ok_or_else(|| ErrorData::invalid_params("action is required", None))?;
998 let mut cache = self.cache.write().await;
999 let result = match action.as_str() {
1000 "status" => {
1001 let entries = cache.get_all_entries();
1002 if entries.is_empty() {
1003 "Cache empty — no files tracked.".to_string()
1004 } else {
1005 let mut lines = vec![format!("Cache: {} file(s)", entries.len())];
1006 for (path, entry) in &entries {
1007 let fref = cache
1008 .file_ref_map()
1009 .get(*path)
1010 .map(|s| s.as_str())
1011 .unwrap_or("F?");
1012 lines.push(format!(
1013 " {fref}={} [{}L, {}t, read {}x]",
1014 crate::core::protocol::shorten_path(path),
1015 entry.line_count,
1016 entry.original_tokens,
1017 entry.read_count
1018 ));
1019 }
1020 lines.join("\n")
1021 }
1022 }
1023 "clear" => {
1024 let count = cache.clear();
1025 format!("Cache cleared — {count} file(s) removed. Next ctx_read will return full content.")
1026 }
1027 "invalidate" => {
1028 let path = get_str(args, "path").ok_or_else(|| {
1029 ErrorData::invalid_params("path is required for invalidate", None)
1030 })?;
1031 if cache.invalidate(&path) {
1032 format!(
1033 "Invalidated cache for {}. Next ctx_read will return full content.",
1034 crate::core::protocol::shorten_path(&path)
1035 )
1036 } else {
1037 format!(
1038 "{} was not in cache.",
1039 crate::core::protocol::shorten_path(&path)
1040 )
1041 }
1042 }
1043 _ => "Unknown action. Use: status, clear, invalidate".to_string(),
1044 };
1045 drop(cache);
1046 self.record_call("ctx_cache", 0, 0, Some(action)).await;
1047 result
1048 }
1049 "ctx_session" => {
1050 let action = get_str(args, "action")
1051 .ok_or_else(|| ErrorData::invalid_params("action is required", None))?;
1052 let value = get_str(args, "value");
1053 let sid = get_str(args, "session_id");
1054 let mut session = self.session.write().await;
1055 let result = crate::tools::ctx_session::handle(
1056 &mut session,
1057 &action,
1058 value.as_deref(),
1059 sid.as_deref(),
1060 );
1061 drop(session);
1062 self.record_call("ctx_session", 0, 0, Some(action)).await;
1063 result
1064 }
1065 "ctx_knowledge" => {
1066 let action = get_str(args, "action")
1067 .ok_or_else(|| ErrorData::invalid_params("action is required", None))?;
1068 let category = get_str(args, "category");
1069 let key = get_str(args, "key");
1070 let value = get_str(args, "value");
1071 let query = get_str(args, "query");
1072 let pattern_type = get_str(args, "pattern_type");
1073 let examples = get_str_array(args, "examples");
1074 let confidence: Option<f32> = args
1075 .as_ref()
1076 .and_then(|a| a.get("confidence"))
1077 .and_then(|v| v.as_f64())
1078 .map(|v| v as f32);
1079
1080 let session = self.session.read().await;
1081 let session_id = session.id.clone();
1082 let project_root = session.project_root.clone().unwrap_or_else(|| {
1083 std::env::current_dir()
1084 .map(|p| p.to_string_lossy().to_string())
1085 .unwrap_or_else(|_| "unknown".to_string())
1086 });
1087 drop(session);
1088
1089 let result = crate::tools::ctx_knowledge::handle(
1090 &project_root,
1091 &action,
1092 category.as_deref(),
1093 key.as_deref(),
1094 value.as_deref(),
1095 query.as_deref(),
1096 &session_id,
1097 pattern_type.as_deref(),
1098 examples,
1099 confidence,
1100 );
1101 self.record_call("ctx_knowledge", 0, 0, Some(action)).await;
1102 result
1103 }
1104 "ctx_agent" => {
1105 let action = get_str(args, "action")
1106 .ok_or_else(|| ErrorData::invalid_params("action is required", None))?;
1107 let agent_type = get_str(args, "agent_type");
1108 let role = get_str(args, "role");
1109 let message = get_str(args, "message");
1110 let category = get_str(args, "category");
1111 let to_agent = get_str(args, "to_agent");
1112 let status = get_str(args, "status");
1113
1114 let session = self.session.read().await;
1115 let project_root = session.project_root.clone().unwrap_or_else(|| {
1116 std::env::current_dir()
1117 .map(|p| p.to_string_lossy().to_string())
1118 .unwrap_or_else(|_| "unknown".to_string())
1119 });
1120 drop(session);
1121
1122 let current_agent_id = self.agent_id.read().await.clone();
1123 let result = crate::tools::ctx_agent::handle(
1124 &action,
1125 agent_type.as_deref(),
1126 role.as_deref(),
1127 &project_root,
1128 current_agent_id.as_deref(),
1129 message.as_deref(),
1130 category.as_deref(),
1131 to_agent.as_deref(),
1132 status.as_deref(),
1133 );
1134
1135 if action == "register" {
1136 if let Some(id) = result.split(':').nth(1) {
1137 let id = id.split_whitespace().next().unwrap_or("").to_string();
1138 if !id.is_empty() {
1139 *self.agent_id.write().await = Some(id);
1140 }
1141 }
1142 }
1143
1144 self.record_call("ctx_agent", 0, 0, Some(action)).await;
1145 result
1146 }
1147 "ctx_overview" => {
1148 let task = get_str(args, "task");
1149 let path = get_str(args, "path");
1150 let cache = self.cache.read().await;
1151 let result = crate::tools::ctx_overview::handle(
1152 &cache,
1153 task.as_deref(),
1154 path.as_deref(),
1155 self.crp_mode,
1156 );
1157 drop(cache);
1158 self.record_call("ctx_overview", 0, 0, Some("overview".to_string()))
1159 .await;
1160 result
1161 }
1162 "ctx_preload" => {
1163 let task = get_str(args, "task").unwrap_or_default();
1164 let path = get_str(args, "path");
1165 let mut cache = self.cache.write().await;
1166 let result = crate::tools::ctx_preload::handle(
1167 &mut cache,
1168 &task,
1169 path.as_deref(),
1170 self.crp_mode,
1171 );
1172 drop(cache);
1173 self.record_call("ctx_preload", 0, 0, Some("preload".to_string()))
1174 .await;
1175 result
1176 }
1177 "ctx_wrapped" => {
1178 let period = get_str(args, "period").unwrap_or_else(|| "week".to_string());
1179 let result = crate::tools::ctx_wrapped::handle(&period);
1180 self.record_call("ctx_wrapped", 0, 0, Some(period)).await;
1181 result
1182 }
1183 "ctx_semantic_search" => {
1184 let query = get_str(args, "query")
1185 .ok_or_else(|| ErrorData::invalid_params("query is required", None))?;
1186 let path = get_str(args, "path").unwrap_or_else(|| ".".to_string());
1187 let top_k = get_int(args, "top_k").unwrap_or(10) as usize;
1188 let action = get_str(args, "action").unwrap_or_default();
1189 let result = if action == "reindex" {
1190 crate::tools::ctx_semantic_search::handle_reindex(&path)
1191 } else {
1192 crate::tools::ctx_semantic_search::handle(&query, &path, top_k, self.crp_mode)
1193 };
1194 self.record_call("ctx_semantic_search", 0, 0, Some("semantic".to_string()))
1195 .await;
1196 result
1197 }
1198 _ => {
1199 return Err(ErrorData::invalid_params(
1200 format!("Unknown tool: {name}"),
1201 None,
1202 ));
1203 }
1204 };
1205
1206 let mut result_text = result_text;
1207
1208 if let Some(ctx) = auto_context {
1209 result_text = format!("{ctx}\n\n{result_text}");
1210 }
1211
1212 if name == "ctx_read" {
1213 let read_path = get_str(args, "path").unwrap_or_default();
1214 let project_root = {
1215 let session = self.session.read().await;
1216 session.project_root.clone()
1217 };
1218 let mut cache = self.cache.write().await;
1219 let enrich = crate::tools::autonomy::enrich_after_read(
1220 &self.autonomy,
1221 &mut cache,
1222 &read_path,
1223 project_root.as_deref(),
1224 );
1225 if let Some(hint) = enrich.related_hint {
1226 result_text = format!("{result_text}\n{hint}");
1227 }
1228
1229 crate::tools::autonomy::maybe_auto_dedup(&self.autonomy, &mut cache);
1230 }
1231
1232 if name == "ctx_shell" {
1233 let cmd = get_str(args, "command").unwrap_or_default();
1234 let output_tokens = crate::core::tokens::count_tokens(&result_text);
1235 let calls = self.tool_calls.read().await;
1236 let last_original = calls.last().map(|c| c.original_tokens).unwrap_or(0);
1237 drop(calls);
1238 if let Some(hint) = crate::tools::autonomy::shell_efficiency_hint(
1239 &self.autonomy,
1240 &cmd,
1241 last_original,
1242 output_tokens,
1243 ) {
1244 result_text = format!("{result_text}\n{hint}");
1245 }
1246 }
1247
1248 let skip_checkpoint = matches!(
1249 name,
1250 "ctx_compress"
1251 | "ctx_metrics"
1252 | "ctx_benchmark"
1253 | "ctx_analyze"
1254 | "ctx_cache"
1255 | "ctx_discover"
1256 | "ctx_dedup"
1257 | "ctx_session"
1258 | "ctx_knowledge"
1259 | "ctx_agent"
1260 | "ctx_wrapped"
1261 | "ctx_overview"
1262 | "ctx_preload"
1263 );
1264
1265 if !skip_checkpoint && self.increment_and_check() {
1266 if let Some(checkpoint) = self.auto_checkpoint().await {
1267 let combined = format!(
1268 "{result_text}\n\n--- AUTO CHECKPOINT (every {} calls) ---\n{checkpoint}",
1269 self.checkpoint_interval
1270 );
1271 return Ok(CallToolResult::success(vec![Content::text(combined)]));
1272 }
1273 }
1274
1275 let tool_duration_ms = tool_start.elapsed().as_millis() as u64;
1276 if tool_duration_ms > 100 {
1277 LeanCtxServer::append_tool_call_log(
1278 name,
1279 tool_duration_ms,
1280 0,
1281 0,
1282 None,
1283 &chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string(),
1284 );
1285 }
1286
1287 let current_count = self.call_count.load(std::sync::atomic::Ordering::Relaxed);
1288 if current_count > 0 && current_count.is_multiple_of(100) {
1289 std::thread::spawn(cloud_background_tasks);
1290 }
1291
1292 Ok(CallToolResult::success(vec![Content::text(result_text)]))
1293 }
1294}
1295
1296fn build_instructions(crp_mode: CrpMode) -> String {
1297 build_instructions_with_client(crp_mode, "")
1298}
1299
1300fn build_instructions_with_client(crp_mode: CrpMode, client_name: &str) -> String {
1301 let profile = crate::core::litm::LitmProfile::from_client_name(client_name);
1302 let session_block = match crate::core::session::SessionState::load_latest() {
1303 Some(ref session) => {
1304 let positioned = crate::core::litm::position_optimize(session);
1305 format!(
1306 "\n\n--- ACTIVE SESSION (LITM P1: begin position, profile: {}) ---\n{}\n---\n",
1307 profile.name, positioned.begin_block
1308 )
1309 }
1310 None => String::new(),
1311 };
1312
1313 let knowledge_block = {
1314 let project_root = crate::core::session::SessionState::load_latest()
1315 .and_then(|s| s.project_root)
1316 .or_else(|| {
1317 std::env::current_dir()
1318 .ok()
1319 .map(|p| p.to_string_lossy().to_string())
1320 });
1321 match project_root {
1322 Some(root) => {
1323 let knowledge = crate::core::knowledge::ProjectKnowledge::load(&root);
1324 match knowledge {
1325 Some(k) if !k.facts.is_empty() || !k.patterns.is_empty() => {
1326 format!("\n--- PROJECT KNOWLEDGE ---\n{}\n---\n", k.format_summary())
1327 }
1328 _ => String::new(),
1329 }
1330 }
1331 None => String::new(),
1332 }
1333 };
1334
1335 let mut base = format!("\
1338CRITICAL: NEVER use native Read, Shell, Grep, or ls tools. ALWAYS use ctx_read, ctx_shell, ctx_search, ctx_tree instead.\n\
1339\n\
1340lean-ctx MCP — tool mapping:\n\
1341• Read/cat/head/tail -> ctx_read(path, mode)\n\
1342• Shell/bash -> ctx_shell(command)\n\
1343• Grep/rg -> ctx_search(pattern, path)\n\
1344• ls/find -> ctx_tree(path, depth)\n\
1345• Write, StrReplace, Delete, Glob -> use normally (no replacement)\n\
1346\n\
1347ctx_read modes: full (cached, for edits), map (deps+API), signatures, diff, task (IB-filtered), \
1348reference, aggressive, entropy, lines:N-M. Auto-selects when unspecified. Re-reads ~13 tokens. File refs F1,F2.. persist.\n\
1349If ctx_read returns 'cached': use fresh=true, start_line=N, or mode='lines:N-M' to re-read.\n\
1350\n\
1351AUTONOMY: lean-ctx auto-runs ctx_overview, ctx_preload, ctx_dedup, ctx_compress behind the scenes.\n\
1352Focus on: ctx_read, ctx_shell, ctx_search, ctx_tree. Use ctx_session for memory, ctx_knowledge for project facts.\n\
1353ctx_shell raw=true: skip compression for small/critical outputs. Full output tee files at ~/.lean-ctx/tee/.\n\
1354\n\
1355Auto-checkpoint every 15 calls. Cache clears after 5 min idle.\n\
1356\n\
1357CEP v1: 1.ACT FIRST 2.DELTA ONLY (Fn refs) 3.STRUCTURED (+/-/~) 4.ONE LINE PER ACTION 5.QUALITY ANCHOR\n\
1358\n\
1359{decoder_block}\n\
1360\n\
1361{session_block}\
1362{knowledge_block}\
1363\n\
1364--- TOOL ENFORCEMENT (LITM-END) ---\n\
1365Read/cat/head/tail -> ctx_read | Shell/bash -> ctx_shell | Grep/rg -> ctx_search | ls/find -> ctx_tree\n\
1366Write, StrReplace, Delete, Glob -> use normally",
1367 decoder_block = crate::core::protocol::instruction_decoder_block()
1368 );
1369
1370 if should_use_unified(client_name) {
1371 base.push_str(
1372 "\n\n\
1373UNIFIED TOOL MODE (active):\n\
1374Additional tools are accessed via ctx() meta-tool: ctx(tool=\"<name>\", ...params).\n\
1375See the ctx() tool description for available sub-tools.\n",
1376 );
1377 }
1378
1379 let intelligence_block = build_intelligence_block();
1380
1381 let base = base;
1382 match crp_mode {
1383 CrpMode::Off => format!("{base}\n\n{intelligence_block}"),
1384 CrpMode::Compact => {
1385 format!(
1386 "{base}\n\n\
1387CRP MODE: compact\n\
1388Compact Response Protocol:\n\
1389• Omit filler words, articles, redundant phrases\n\
1390• Abbreviate: fn, cfg, impl, deps, req, res, ctx, err, ret, arg, val, ty, mod\n\
1391• Compact lists over prose, code blocks over explanations\n\
1392• Code changes: diff lines (+/-) only, not full files\n\
1393• TARGET: <=200 tokens per response unless code edits require more\n\
1394• Tool outputs are pre-analyzed and compressed. Trust them directly.\n\n\
1395{intelligence_block}"
1396 )
1397 }
1398 CrpMode::Tdd => {
1399 format!(
1400 "{base}\n\n\
1401CRP MODE: tdd (Token Dense Dialect)\n\
1402Maximize information density. Every token must carry meaning.\n\
1403\n\
1404RESPONSE RULES:\n\
1405• Drop articles, filler words, pleasantries\n\
1406• Reference files by Fn refs only, never full paths\n\
1407• Code changes: diff lines only (+/-), not full files\n\
1408• No explanations unless asked\n\
1409• Tables for structured data\n\
1410• Abbreviations: fn, cfg, impl, deps, req, res, ctx, err, ret, arg, val, ty, mod\n\
1411\n\
1412CHANGE NOTATION:\n\
1413+F1:42 param(timeout:Duration) — added\n\
1414-F1:10-15 — removed\n\
1415~F1:42 validate_token -> verify_jwt — changed\n\
1416\n\
1417STATUS: ctx_read(F1) -> 808L cached ok | cargo test -> 82 passed 0 failed\n\
1418\n\
1419TOKEN BUDGET: <=150 tokens per response. Exceed only for multi-file edits.\n\
1420Tool outputs are pre-analyzed and compressed. Trust them directly.\n\
1421ZERO NARRATION: Act, then report result in 1 line.\n\n\
1422{intelligence_block}"
1423 )
1424 }
1425 }
1426}
1427
1428fn build_intelligence_block() -> String {
1429 "\
1430OUTPUT EFFICIENCY:\n\
1431• NEVER echo back code that was provided in tool outputs — it wastes tokens.\n\
1432• NEVER add narration comments (// Import, // Define, // Return) — code is self-documenting.\n\
1433• For code changes: show only the new/changed code, not unchanged context.\n\
1434• Tool outputs include [TASK:type] and SCOPE hints for context.\n\
1435• Respect the user's intent: architecture tasks need thorough analysis, simple generates need code."
1436 .to_string()
1437}
1438
1439fn tool_def(name: &'static str, description: &'static str, schema_value: Value) -> Tool {
1440 let schema: Map<String, Value> = match schema_value {
1441 Value::Object(map) => map,
1442 _ => Map::new(),
1443 };
1444 Tool::new(name, description, Arc::new(schema))
1445}
1446
1447fn unified_tool_defs() -> Vec<Tool> {
1448 vec![
1449 tool_def(
1450 "ctx_read",
1451 "Read file (cached, compressed). Modes: full|map|signatures|diff|aggressive|entropy|task|reference|lines:N-M. fresh=true re-reads.",
1452 json!({
1453 "type": "object",
1454 "properties": {
1455 "path": { "type": "string", "description": "File path" },
1456 "mode": { "type": "string" },
1457 "start_line": { "type": "integer" },
1458 "fresh": { "type": "boolean" }
1459 },
1460 "required": ["path"]
1461 }),
1462 ),
1463 tool_def(
1464 "ctx_shell",
1465 "Run shell command (compressed output). raw=true skips compression.",
1466 json!({
1467 "type": "object",
1468 "properties": {
1469 "command": { "type": "string", "description": "Shell command" },
1470 "raw": { "type": "boolean", "description": "Skip compression for full output" }
1471 },
1472 "required": ["command"]
1473 }),
1474 ),
1475 tool_def(
1476 "ctx_search",
1477 "Regex code search (.gitignore aware).",
1478 json!({
1479 "type": "object",
1480 "properties": {
1481 "pattern": { "type": "string", "description": "Regex pattern" },
1482 "path": { "type": "string" },
1483 "ext": { "type": "string" },
1484 "max_results": { "type": "integer" },
1485 "ignore_gitignore": { "type": "boolean" }
1486 },
1487 "required": ["pattern"]
1488 }),
1489 ),
1490 tool_def(
1491 "ctx_tree",
1492 "Directory listing with file counts.",
1493 json!({
1494 "type": "object",
1495 "properties": {
1496 "path": { "type": "string" },
1497 "depth": { "type": "integer" },
1498 "show_hidden": { "type": "boolean" }
1499 }
1500 }),
1501 ),
1502 tool_def(
1503 "ctx",
1504 "Meta-tool: set tool= to sub-tool name. Sub-tools: compress (checkpoint), metrics (stats), \
1505analyze (entropy), cache (status|clear|invalidate), discover (missed patterns), smart_read (auto-mode), \
1506delta (incremental diff), dedup (cross-file), fill (budget-aware batch read), intent (auto-read by task), \
1507response (compress LLM text), context (session state), graph (build|related|symbol|impact|status), \
1508session (load|save|task|finding|decision|status|reset|list|cleanup), \
1509knowledge (remember|recall|pattern|consolidate|status|remove|export), \
1510agent (register|post|read|status|list|info), overview (project map), \
1511wrapped (savings report), benchmark (file|project), multi_read (batch), semantic_search (BM25).",
1512 json!({
1513 "type": "object",
1514 "properties": {
1515 "tool": {
1516 "type": "string",
1517 "description": "compress|metrics|analyze|cache|discover|smart_read|delta|dedup|fill|intent|response|context|graph|session|knowledge|agent|overview|wrapped|benchmark|multi_read|semantic_search"
1518 },
1519 "action": { "type": "string" },
1520 "path": { "type": "string" },
1521 "paths": { "type": "array", "items": { "type": "string" } },
1522 "query": { "type": "string" },
1523 "value": { "type": "string" },
1524 "category": { "type": "string" },
1525 "key": { "type": "string" },
1526 "budget": { "type": "integer" },
1527 "task": { "type": "string" },
1528 "mode": { "type": "string" },
1529 "text": { "type": "string" },
1530 "message": { "type": "string" },
1531 "session_id": { "type": "string" },
1532 "period": { "type": "string" },
1533 "format": { "type": "string" },
1534 "agent_type": { "type": "string" },
1535 "role": { "type": "string" },
1536 "status": { "type": "string" },
1537 "pattern_type": { "type": "string" },
1538 "examples": { "type": "array", "items": { "type": "string" } },
1539 "confidence": { "type": "number" },
1540 "project_root": { "type": "string" },
1541 "include_signatures": { "type": "boolean" },
1542 "limit": { "type": "integer" },
1543 "to_agent": { "type": "string" },
1544 "show_hidden": { "type": "boolean" }
1545 },
1546 "required": ["tool"]
1547 }),
1548 ),
1549 ]
1550}
1551
1552fn should_use_unified(client_name: &str) -> bool {
1553 if std::env::var("LEAN_CTX_FULL_TOOLS").is_ok() {
1554 return false;
1555 }
1556 if std::env::var("LEAN_CTX_UNIFIED").is_ok() {
1557 return true;
1558 }
1559 let _ = client_name;
1560 false
1561}
1562
1563fn get_str_array(args: &Option<serde_json::Map<String, Value>>, key: &str) -> Option<Vec<String>> {
1564 let arr = args.as_ref()?.get(key)?.as_array()?;
1565 let mut out = Vec::with_capacity(arr.len());
1566 for v in arr {
1567 let s = v.as_str()?.to_string();
1568 out.push(s);
1569 }
1570 Some(out)
1571}
1572
1573fn get_str(args: &Option<serde_json::Map<String, Value>>, key: &str) -> Option<String> {
1574 args.as_ref()?.get(key)?.as_str().map(|s| s.to_string())
1575}
1576
1577fn get_int(args: &Option<serde_json::Map<String, Value>>, key: &str) -> Option<i64> {
1578 args.as_ref()?.get(key)?.as_i64()
1579}
1580
1581fn get_bool(args: &Option<serde_json::Map<String, Value>>, key: &str) -> Option<bool> {
1582 args.as_ref()?.get(key)?.as_bool()
1583}
1584
1585fn execute_command(command: &str) -> String {
1586 let (shell, flag) = crate::shell::shell_and_flag();
1587 let output = std::process::Command::new(&shell)
1588 .arg(&flag)
1589 .arg(command)
1590 .env("LEAN_CTX_ACTIVE", "1")
1591 .output();
1592
1593 match output {
1594 Ok(out) => {
1595 let stdout = String::from_utf8_lossy(&out.stdout);
1596 let stderr = String::from_utf8_lossy(&out.stderr);
1597 if stdout.is_empty() {
1598 stderr.to_string()
1599 } else if stderr.is_empty() {
1600 stdout.to_string()
1601 } else {
1602 format!("{stdout}\n{stderr}")
1603 }
1604 }
1605 Err(e) => format!("ERROR: {e}"),
1606 }
1607}
1608
1609fn detect_project_root(file_path: &str) -> Option<String> {
1610 let mut dir = std::path::Path::new(file_path).parent()?;
1611 loop {
1612 if dir.join(".git").exists() {
1613 return Some(dir.to_string_lossy().to_string());
1614 }
1615 dir = dir.parent()?;
1616 }
1617}
1618
1619fn cloud_background_tasks() {
1620 use crate::core::config::Config;
1621
1622 let mut config = Config::load();
1623 let today = chrono::Local::now().format("%Y-%m-%d").to_string();
1624
1625 let already_contributed = config
1626 .cloud
1627 .last_contribute
1628 .as_deref()
1629 .map(|d| d == today)
1630 .unwrap_or(false);
1631 let already_synced = config
1632 .cloud
1633 .last_sync
1634 .as_deref()
1635 .map(|d| d == today)
1636 .unwrap_or(false);
1637 let already_pulled = config
1638 .cloud
1639 .last_model_pull
1640 .as_deref()
1641 .map(|d| d == today)
1642 .unwrap_or(false);
1643
1644 if config.cloud.contribute_enabled && !already_contributed {
1645 if let Some(home) = dirs::home_dir() {
1646 let mode_stats_path = home.join(".lean-ctx").join("mode_stats.json");
1647 if let Ok(data) = std::fs::read_to_string(&mode_stats_path) {
1648 if let Ok(predictor) = serde_json::from_str::<serde_json::Value>(&data) {
1649 let mut entries = Vec::new();
1650 if let Some(history) = predictor["history"].as_object() {
1651 for (_key, outcomes) in history {
1652 if let Some(arr) = outcomes.as_array() {
1653 for outcome in arr.iter().rev().take(3) {
1654 let ext = outcome["ext"].as_str().unwrap_or("unknown");
1655 let mode = outcome["mode"].as_str().unwrap_or("full");
1656 let t_in = outcome["tokens_in"].as_u64().unwrap_or(0);
1657 let t_out = outcome["tokens_out"].as_u64().unwrap_or(0);
1658 let ratio = if t_in > 0 {
1659 1.0 - t_out as f64 / t_in as f64
1660 } else {
1661 0.0
1662 };
1663 let bucket = match t_in {
1664 0..=500 => "0-500",
1665 501..=2000 => "500-2k",
1666 2001..=10000 => "2k-10k",
1667 _ => "10k+",
1668 };
1669 entries.push(serde_json::json!({
1670 "file_ext": format!(".{ext}"),
1671 "size_bucket": bucket,
1672 "best_mode": mode,
1673 "compression_ratio": (ratio * 100.0).round() / 100.0,
1674 }));
1675 if entries.len() >= 200 {
1676 break;
1677 }
1678 }
1679 }
1680 if entries.len() >= 200 {
1681 break;
1682 }
1683 }
1684 }
1685 if !entries.is_empty() && crate::cloud_client::contribute(&entries).is_ok() {
1686 config.cloud.last_contribute = Some(today.clone());
1687 }
1688 }
1689 }
1690 }
1691 }
1692
1693 if crate::cloud_client::check_pro() {
1694 if !already_synced {
1695 let stats_data = crate::core::stats::format_gain_json();
1696 if let Ok(parsed) = serde_json::from_str::<serde_json::Value>(&stats_data) {
1697 let entry = serde_json::json!({
1698 "date": &today,
1699 "tokens_original": parsed["total_original_tokens"].as_i64().unwrap_or(0),
1700 "tokens_compressed": parsed["total_compressed_tokens"].as_i64().unwrap_or(0),
1701 "tokens_saved": parsed["total_saved_tokens"].as_i64().unwrap_or(0),
1702 "tool_calls": parsed["total_calls"].as_i64().unwrap_or(0),
1703 "cache_hits": parsed["cache_hits"].as_i64().unwrap_or(0),
1704 "cache_misses": parsed["cache_misses"].as_i64().unwrap_or(0),
1705 });
1706 if crate::cloud_client::sync_stats(&[entry]).is_ok() {
1707 config.cloud.last_sync = Some(today.clone());
1708 }
1709 }
1710 }
1711
1712 if !already_pulled {
1713 if let Ok(data) = crate::cloud_client::pull_pro_models() {
1714 let _ = crate::cloud_client::save_pro_models(&data);
1715 config.cloud.last_model_pull = Some(today.clone());
1716 }
1717 }
1718 }
1719
1720 let _ = config.save();
1721}
1722
1723pub fn build_instructions_for_test(crp_mode: CrpMode) -> String {
1724 build_instructions(crp_mode)
1725}
1726
1727pub fn tool_descriptions_for_test() -> Vec<(&'static str, &'static str)> {
1728 let mut result = Vec::new();
1729 let tools_json = list_all_tool_defs();
1730 for (name, desc, _) in tools_json {
1731 result.push((name, desc));
1732 }
1733 result
1734}
1735
1736pub fn tool_schemas_json_for_test() -> String {
1737 let tools_json = list_all_tool_defs();
1738 let schemas: Vec<String> = tools_json
1739 .iter()
1740 .map(|(name, _, schema)| format!("{}: {}", name, schema))
1741 .collect();
1742 schemas.join("\n")
1743}
1744
1745fn list_all_tool_defs() -> Vec<(&'static str, &'static str, Value)> {
1746 vec![
1747 ("ctx_read", "Read file (cached, compressed). Re-reads ~13 tok. Auto-selects optimal mode. \
1748Modes: full|map|signatures|diff|aggressive|entropy|task|reference|lines:N-M. fresh=true re-reads.", json!({"type": "object", "properties": {"path": {"type": "string"}, "mode": {"type": "string"}, "start_line": {"type": "integer"}, "fresh": {"type": "boolean"}}, "required": ["path"]})),
1749 ("ctx_multi_read", "Batch read files in one call. Same modes as ctx_read.", json!({"type": "object", "properties": {"paths": {"type": "array", "items": {"type": "string"}}, "mode": {"type": "string"}}, "required": ["paths"]})),
1750 ("ctx_tree", "Directory listing with file counts.", json!({"type": "object", "properties": {"path": {"type": "string"}, "depth": {"type": "integer"}, "show_hidden": {"type": "boolean"}}})),
1751 ("ctx_shell", "Run shell command (compressed output, 90+ patterns).", json!({"type": "object", "properties": {"command": {"type": "string"}}, "required": ["command"]})),
1752 ("ctx_search", "Regex code search (.gitignore aware, compact results).", json!({"type": "object", "properties": {"pattern": {"type": "string"}, "path": {"type": "string"}, "ext": {"type": "string"}, "max_results": {"type": "integer"}}, "required": ["pattern"]})),
1753 ("ctx_compress", "Context checkpoint for long conversations.", json!({"type": "object", "properties": {"include_signatures": {"type": "boolean"}}})),
1754 ("ctx_benchmark", "Benchmark compression modes for a file or project.", json!({"type": "object", "properties": {"path": {"type": "string"}, "action": {"type": "string"}, "format": {"type": "string"}}, "required": ["path"]})),
1755 ("ctx_metrics", "Session token stats, cache rates, per-tool savings.", json!({"type": "object", "properties": {}})),
1756 ("ctx_analyze", "Entropy analysis — recommends optimal compression mode for a file.", json!({"type": "object", "properties": {"path": {"type": "string"}}, "required": ["path"]})),
1757 ("ctx_cache", "Cache ops: status|clear|invalidate.", json!({"type": "object", "properties": {"action": {"type": "string"}, "path": {"type": "string"}}, "required": ["action"]})),
1758 ("ctx_discover", "Find missed compression opportunities in shell history.", json!({"type": "object", "properties": {"limit": {"type": "integer"}}})),
1759 ("ctx_smart_read", "Auto-select optimal read mode for a file.", json!({"type": "object", "properties": {"path": {"type": "string"}}, "required": ["path"]})),
1760 ("ctx_delta", "Incremental diff — sends only changed lines since last read.", json!({"type": "object", "properties": {"path": {"type": "string"}}, "required": ["path"]})),
1761 ("ctx_dedup", "Cross-file dedup: analyze or apply shared block references.", json!({"type": "object", "properties": {"action": {"type": "string"}}})),
1762 ("ctx_fill", "Budget-aware context fill — auto-selects compression per file within token limit.", json!({"type": "object", "properties": {"paths": {"type": "array", "items": {"type": "string"}}, "budget": {"type": "integer"}}, "required": ["paths", "budget"]})),
1763 ("ctx_intent", "Intent detection — auto-reads relevant files based on task description.", json!({"type": "object", "properties": {"query": {"type": "string"}, "project_root": {"type": "string"}}, "required": ["query"]})),
1764 ("ctx_response", "Compress LLM response text (remove filler, apply TDD).", json!({"type": "object", "properties": {"text": {"type": "string"}}, "required": ["text"]})),
1765 ("ctx_context", "Session context overview — cached files, seen files, session state.", json!({"type": "object", "properties": {}})),
1766 ("ctx_graph", "Code dependency graph. Actions: build (index project), related (find files connected to path), \
1767symbol (lookup definition/usages as file::name), impact (blast radius of changes to path), status (index stats).", json!({"type": "object", "properties": {"action": {"type": "string"}, "path": {"type": "string"}, "project_root": {"type": "string"}}, "required": ["action"]})),
1768 ("ctx_session", "Cross-session memory (CCP). Actions: load (restore previous session ~400 tok), \
1769save, status, task (set current task), finding (record discovery), decision (record choice), \
1770reset, list (show sessions), cleanup.", json!({"type": "object", "properties": {"action": {"type": "string"}, "value": {"type": "string"}, "session_id": {"type": "string"}}, "required": ["action"]})),
1771 ("ctx_knowledge", "Persistent project knowledge (survives sessions). Actions: remember (store fact with category+key+value), \
1772recall (search by query), pattern (record naming/structure pattern), consolidate (extract session findings into knowledge), \
1773status (list all), remove, export.", json!({"type": "object", "properties": {"action": {"type": "string"}, "category": {"type": "string"}, "key": {"type": "string"}, "value": {"type": "string"}, "query": {"type": "string"}}, "required": ["action"]})),
1774 ("ctx_agent", "Multi-agent coordination (shared message bus). Actions: register (join with agent_type+role), \
1775post (broadcast or direct message with category), read (poll messages), status (update state: active|idle|finished), \
1776list, info.", json!({"type": "object", "properties": {"action": {"type": "string"}, "agent_type": {"type": "string"}, "role": {"type": "string"}, "message": {"type": "string"}}, "required": ["action"]})),
1777 ("ctx_overview", "Task-relevant project map — use at session start.", json!({"type": "object", "properties": {"task": {"type": "string"}, "path": {"type": "string"}}})),
1778 ("ctx_preload", "Proactive context loader — reads and caches task-relevant files, returns compact L-curve-optimized summary with critical lines, imports, and signatures. Costs ~50-100 tokens instead of ~5000 for individual reads.", json!({"type": "object", "properties": {"task": {"type": "string", "description": "Task description (e.g. 'fix auth bug in validate_token')"}, "path": {"type": "string", "description": "Project root (default: .)"}}, "required": ["task"]})),
1779 ("ctx_wrapped", "Savings report card. Periods: week|month|all.", json!({"type": "object", "properties": {"period": {"type": "string"}}})),
1780 ("ctx_semantic_search", "BM25 code search by meaning. action=reindex to rebuild.", json!({"type": "object", "properties": {"query": {"type": "string"}, "path": {"type": "string"}, "top_k": {"type": "integer"}, "action": {"type": "string"}}, "required": ["query"]})),
1781 ]
1782}
1783
1784#[cfg(test)]
1785mod tests {
1786 use super::*;
1787
1788 #[test]
1789 fn test_should_use_unified_defaults_to_false() {
1790 assert!(!should_use_unified("cursor"));
1791 assert!(!should_use_unified("claude-code"));
1792 assert!(!should_use_unified("windsurf"));
1793 assert!(!should_use_unified(""));
1794 assert!(!should_use_unified("some-unknown-client"));
1795 }
1796
1797 #[test]
1798 fn test_unified_tool_count() {
1799 let tools = unified_tool_defs();
1800 assert_eq!(tools.len(), 5, "Expected 5 unified tools");
1801 }
1802}