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.15.0"))
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.15.0"))
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 result_text = match name {
565 "ctx_read" => {
566 let path = get_str(args, "path")
567 .ok_or_else(|| ErrorData::invalid_params("path is required", None))?;
568 let current_task = {
569 let session = self.session.read().await;
570 session.task.as_ref().map(|t| t.description.clone())
571 };
572 let task_ref = current_task.as_deref();
573 let mut mode = match get_str(args, "mode") {
574 Some(m) => m,
575 None => {
576 let cache = self.cache.read().await;
577 crate::tools::ctx_smart_read::select_mode_with_task(&cache, &path, task_ref)
578 }
579 };
580 let fresh = get_bool(args, "fresh").unwrap_or(false);
581 let start_line = get_int(args, "start_line");
582 if let Some(sl) = start_line {
583 let sl = sl.max(1_i64);
584 mode = format!("lines:{sl}-999999");
585 }
586 let stale = self.is_prompt_cache_stale().await;
587 let effective_mode = LeanCtxServer::upgrade_mode_if_stale(&mode, stale).to_string();
588 let mut cache = self.cache.write().await;
589 let output = if fresh {
590 crate::tools::ctx_read::handle_fresh_with_task(
591 &mut cache,
592 &path,
593 &effective_mode,
594 self.crp_mode,
595 task_ref,
596 )
597 } else {
598 crate::tools::ctx_read::handle_with_task(
599 &mut cache,
600 &path,
601 &effective_mode,
602 self.crp_mode,
603 task_ref,
604 )
605 };
606 let stale_note = if effective_mode != mode {
607 format!("[cache stale, {mode}→{effective_mode}]\n")
608 } else {
609 String::new()
610 };
611 let original = cache.get(&path).map_or(0, |e| e.original_tokens);
612 let output_tokens = crate::core::tokens::count_tokens(&output);
613 let saved = original.saturating_sub(output_tokens);
614 let is_cache_hit = output.contains(" cached ");
615 let output = format!("{stale_note}{output}");
616 let file_ref = cache.file_ref_map().get(&path).cloned();
617 drop(cache);
618 {
619 let mut session = self.session.write().await;
620 session.touch_file(&path, file_ref.as_deref(), &effective_mode, original);
621 if is_cache_hit {
622 session.record_cache_hit();
623 }
624 if session.project_root.is_none() {
625 if let Some(root) = detect_project_root(&path) {
626 session.project_root = Some(root.clone());
627 let mut current = self.agent_id.write().await;
628 if current.is_none() {
629 let mut registry =
630 crate::core::agents::AgentRegistry::load_or_create();
631 registry.cleanup_stale(24);
632 let id = registry.register("mcp", None, &root);
633 let _ = registry.save();
634 *current = Some(id);
635 }
636 }
637 }
638 }
639 self.record_call("ctx_read", original, saved, Some(mode.clone()))
640 .await;
641 {
642 let sig =
643 crate::core::mode_predictor::FileSignature::from_path(&path, original);
644 let density = if output_tokens > 0 {
645 original as f64 / output_tokens as f64
646 } else {
647 1.0
648 };
649 let outcome = crate::core::mode_predictor::ModeOutcome {
650 mode: mode.clone(),
651 tokens_in: original,
652 tokens_out: output_tokens,
653 density: density.min(1.0),
654 };
655 let mut predictor = crate::core::mode_predictor::ModePredictor::new();
656 predictor.record(sig, outcome);
657 predictor.save();
658
659 let ext = std::path::Path::new(&path)
660 .extension()
661 .and_then(|e| e.to_str())
662 .unwrap_or("")
663 .to_string();
664 let thresholds = crate::core::adaptive_thresholds::thresholds_for_path(&path);
665 let cache = self.cache.read().await;
666 let stats = cache.get_stats();
667 let feedback_outcome = crate::core::feedback::CompressionOutcome {
668 session_id: format!("{}", std::process::id()),
669 language: ext,
670 entropy_threshold: thresholds.bpe_entropy,
671 jaccard_threshold: thresholds.jaccard,
672 total_turns: stats.total_reads as u32,
673 tokens_saved: saved as u64,
674 tokens_original: original as u64,
675 cache_hits: stats.cache_hits as u32,
676 total_reads: stats.total_reads as u32,
677 task_completed: true,
678 timestamp: chrono::Local::now().to_rfc3339(),
679 };
680 drop(cache);
681 let mut store = crate::core::feedback::FeedbackStore::load();
682 store.record_outcome(feedback_outcome);
683 }
684 output
685 }
686 "ctx_multi_read" => {
687 let paths = get_str_array(args, "paths")
688 .ok_or_else(|| ErrorData::invalid_params("paths array is required", None))?;
689 let mode = get_str(args, "mode").unwrap_or_else(|| "full".to_string());
690 let current_task = {
691 let session = self.session.read().await;
692 session.task.as_ref().map(|t| t.description.clone())
693 };
694 let mut cache = self.cache.write().await;
695 let output = crate::tools::ctx_multi_read::handle_with_task(
696 &mut cache,
697 &paths,
698 &mode,
699 self.crp_mode,
700 current_task.as_deref(),
701 );
702 let mut total_original: usize = 0;
703 for path in &paths {
704 total_original = total_original
705 .saturating_add(cache.get(path).map(|e| e.original_tokens).unwrap_or(0));
706 }
707 let tokens = crate::core::tokens::count_tokens(&output);
708 drop(cache);
709 self.record_call(
710 "ctx_multi_read",
711 total_original,
712 total_original.saturating_sub(tokens),
713 Some(mode),
714 )
715 .await;
716 output
717 }
718 "ctx_tree" => {
719 let path = get_str(args, "path").unwrap_or_else(|| ".".to_string());
720 let depth = get_int(args, "depth").unwrap_or(3) as usize;
721 let show_hidden = get_bool(args, "show_hidden").unwrap_or(false);
722 let (result, original) = crate::tools::ctx_tree::handle(&path, depth, show_hidden);
723 let sent = crate::core::tokens::count_tokens(&result);
724 let saved = original.saturating_sub(sent);
725 self.record_call("ctx_tree", original, saved, None).await;
726 let savings_note = if saved > 0 {
727 format!("\n[saved {saved} tokens vs native ls]")
728 } else {
729 String::new()
730 };
731 format!("{result}{savings_note}")
732 }
733 "ctx_shell" => {
734 let command = get_str(args, "command")
735 .ok_or_else(|| ErrorData::invalid_params("command is required", None))?;
736 let raw = get_bool(args, "raw").unwrap_or(false)
737 || std::env::var("LEAN_CTX_DISABLED").is_ok();
738 let output = execute_command(&command);
739
740 if raw {
741 let original = crate::core::tokens::count_tokens(&output);
742 self.record_call("ctx_shell", original, 0, None).await;
743 output
744 } else {
745 let result = crate::tools::ctx_shell::handle(&command, &output, self.crp_mode);
746 let original = crate::core::tokens::count_tokens(&output);
747 let sent = crate::core::tokens::count_tokens(&result);
748 let saved = original.saturating_sub(sent);
749 self.record_call("ctx_shell", original, saved, None).await;
750
751 let cfg = crate::core::config::Config::load();
752 let tee_hint = match cfg.tee_mode {
753 crate::core::config::TeeMode::Always => {
754 crate::shell::save_tee(&command, &output)
755 .map(|p| format!("\n[full output: {p}]"))
756 .unwrap_or_default()
757 }
758 crate::core::config::TeeMode::Failures
759 if !output.trim().is_empty() && output.contains("error")
760 || output.contains("Error")
761 || output.contains("ERROR") =>
762 {
763 crate::shell::save_tee(&command, &output)
764 .map(|p| format!("\n[full output: {p}]"))
765 .unwrap_or_default()
766 }
767 _ => String::new(),
768 };
769
770 let savings_note = if saved > 0 {
771 format!("\n[saved {saved} tokens vs native Shell]")
772 } else {
773 String::new()
774 };
775 format!("{result}{savings_note}{tee_hint}")
776 }
777 }
778 "ctx_search" => {
779 let pattern = get_str(args, "pattern")
780 .ok_or_else(|| ErrorData::invalid_params("pattern is required", None))?;
781 let path = get_str(args, "path").unwrap_or_else(|| ".".to_string());
782 let ext = get_str(args, "ext");
783 let max = get_int(args, "max_results").unwrap_or(20) as usize;
784 let no_gitignore = get_bool(args, "ignore_gitignore").unwrap_or(false);
785 let (result, original) = crate::tools::ctx_search::handle(
786 &pattern,
787 &path,
788 ext.as_deref(),
789 max,
790 self.crp_mode,
791 !no_gitignore,
792 );
793 let sent = crate::core::tokens::count_tokens(&result);
794 let saved = original.saturating_sub(sent);
795 self.record_call("ctx_search", original, saved, None).await;
796 let savings_note = if saved > 0 {
797 format!("\n[saved {saved} tokens vs native Grep]")
798 } else {
799 String::new()
800 };
801 format!("{result}{savings_note}")
802 }
803 "ctx_compress" => {
804 let include_sigs = get_bool(args, "include_signatures").unwrap_or(true);
805 let cache = self.cache.read().await;
806 let result =
807 crate::tools::ctx_compress::handle(&cache, include_sigs, self.crp_mode);
808 drop(cache);
809 self.record_call("ctx_compress", 0, 0, None).await;
810 result
811 }
812 "ctx_benchmark" => {
813 let path = get_str(args, "path")
814 .ok_or_else(|| ErrorData::invalid_params("path is required", None))?;
815 let action = get_str(args, "action").unwrap_or_default();
816 let result = if action == "project" {
817 let fmt = get_str(args, "format").unwrap_or_default();
818 let bench = crate::core::benchmark::run_project_benchmark(&path);
819 match fmt.as_str() {
820 "json" => crate::core::benchmark::format_json(&bench),
821 "markdown" | "md" => crate::core::benchmark::format_markdown(&bench),
822 _ => crate::core::benchmark::format_terminal(&bench),
823 }
824 } else {
825 crate::tools::ctx_benchmark::handle(&path, self.crp_mode)
826 };
827 self.record_call("ctx_benchmark", 0, 0, None).await;
828 result
829 }
830 "ctx_metrics" => {
831 let cache = self.cache.read().await;
832 let calls = self.tool_calls.read().await;
833 let result = crate::tools::ctx_metrics::handle(&cache, &calls, self.crp_mode);
834 drop(cache);
835 drop(calls);
836 self.record_call("ctx_metrics", 0, 0, None).await;
837 result
838 }
839 "ctx_analyze" => {
840 let path = get_str(args, "path")
841 .ok_or_else(|| ErrorData::invalid_params("path is required", None))?;
842 let result = crate::tools::ctx_analyze::handle(&path, self.crp_mode);
843 self.record_call("ctx_analyze", 0, 0, None).await;
844 result
845 }
846 "ctx_discover" => {
847 let limit = get_int(args, "limit").unwrap_or(15) as usize;
848 let history = crate::cli::load_shell_history_pub();
849 let result = crate::tools::ctx_discover::discover_from_history(&history, limit);
850 self.record_call("ctx_discover", 0, 0, None).await;
851 result
852 }
853 "ctx_smart_read" => {
854 let path = get_str(args, "path")
855 .ok_or_else(|| ErrorData::invalid_params("path is required", None))?;
856 let mut cache = self.cache.write().await;
857 let output = crate::tools::ctx_smart_read::handle(&mut cache, &path, self.crp_mode);
858 let original = cache.get(&path).map_or(0, |e| e.original_tokens);
859 let tokens = crate::core::tokens::count_tokens(&output);
860 drop(cache);
861 self.record_call(
862 "ctx_smart_read",
863 original,
864 original.saturating_sub(tokens),
865 Some("auto".to_string()),
866 )
867 .await;
868 output
869 }
870 "ctx_delta" => {
871 let path = get_str(args, "path")
872 .ok_or_else(|| ErrorData::invalid_params("path is required", None))?;
873 let mut cache = self.cache.write().await;
874 let output = crate::tools::ctx_delta::handle(&mut cache, &path);
875 let original = cache.get(&path).map_or(0, |e| e.original_tokens);
876 let tokens = crate::core::tokens::count_tokens(&output);
877 drop(cache);
878 {
879 let mut session = self.session.write().await;
880 session.mark_modified(&path);
881 }
882 self.record_call(
883 "ctx_delta",
884 original,
885 original.saturating_sub(tokens),
886 Some("delta".to_string()),
887 )
888 .await;
889 output
890 }
891 "ctx_dedup" => {
892 let action = get_str(args, "action").unwrap_or_default();
893 if action == "apply" {
894 let mut cache = self.cache.write().await;
895 let result = crate::tools::ctx_dedup::handle_action(&mut cache, &action);
896 drop(cache);
897 self.record_call("ctx_dedup", 0, 0, None).await;
898 result
899 } else {
900 let cache = self.cache.read().await;
901 let result = crate::tools::ctx_dedup::handle(&cache);
902 drop(cache);
903 self.record_call("ctx_dedup", 0, 0, None).await;
904 result
905 }
906 }
907 "ctx_fill" => {
908 let paths = get_str_array(args, "paths")
909 .ok_or_else(|| ErrorData::invalid_params("paths array is required", None))?;
910 let budget = get_int(args, "budget")
911 .ok_or_else(|| ErrorData::invalid_params("budget is required", None))?
912 as usize;
913 let mut cache = self.cache.write().await;
914 let output =
915 crate::tools::ctx_fill::handle(&mut cache, &paths, budget, self.crp_mode);
916 drop(cache);
917 self.record_call("ctx_fill", 0, 0, Some(format!("budget:{budget}")))
918 .await;
919 output
920 }
921 "ctx_intent" => {
922 let query = get_str(args, "query")
923 .ok_or_else(|| ErrorData::invalid_params("query is required", None))?;
924 let root = get_str(args, "project_root").unwrap_or_else(|| ".".to_string());
925 let mut cache = self.cache.write().await;
926 let output =
927 crate::tools::ctx_intent::handle(&mut cache, &query, &root, self.crp_mode);
928 drop(cache);
929 {
930 let mut session = self.session.write().await;
931 session.set_task(&query, Some("intent"));
932 }
933 self.record_call("ctx_intent", 0, 0, Some("semantic".to_string()))
934 .await;
935 output
936 }
937 "ctx_response" => {
938 let text = get_str(args, "text")
939 .ok_or_else(|| ErrorData::invalid_params("text is required", None))?;
940 let output = crate::tools::ctx_response::handle(&text, self.crp_mode);
941 self.record_call("ctx_response", 0, 0, None).await;
942 output
943 }
944 "ctx_context" => {
945 let cache = self.cache.read().await;
946 let turn = self.call_count.load(std::sync::atomic::Ordering::Relaxed);
947 let result = crate::tools::ctx_context::handle_status(&cache, turn, self.crp_mode);
948 drop(cache);
949 self.record_call("ctx_context", 0, 0, None).await;
950 result
951 }
952 "ctx_graph" => {
953 let action = get_str(args, "action")
954 .ok_or_else(|| ErrorData::invalid_params("action is required", None))?;
955 let path = get_str(args, "path");
956 let root = get_str(args, "project_root").unwrap_or_else(|| ".".to_string());
957 let mut cache = self.cache.write().await;
958 let result = crate::tools::ctx_graph::handle(
959 &action,
960 path.as_deref(),
961 &root,
962 &mut cache,
963 self.crp_mode,
964 );
965 drop(cache);
966 self.record_call("ctx_graph", 0, 0, Some(action)).await;
967 result
968 }
969 "ctx_cache" => {
970 let action = get_str(args, "action")
971 .ok_or_else(|| ErrorData::invalid_params("action is required", None))?;
972 let mut cache = self.cache.write().await;
973 let result = match action.as_str() {
974 "status" => {
975 let entries = cache.get_all_entries();
976 if entries.is_empty() {
977 "Cache empty — no files tracked.".to_string()
978 } else {
979 let mut lines = vec![format!("Cache: {} file(s)", entries.len())];
980 for (path, entry) in &entries {
981 let fref = cache
982 .file_ref_map()
983 .get(*path)
984 .map(|s| s.as_str())
985 .unwrap_or("F?");
986 lines.push(format!(
987 " {fref}={} [{}L, {}t, read {}x]",
988 crate::core::protocol::shorten_path(path),
989 entry.line_count,
990 entry.original_tokens,
991 entry.read_count
992 ));
993 }
994 lines.join("\n")
995 }
996 }
997 "clear" => {
998 let count = cache.clear();
999 format!("Cache cleared — {count} file(s) removed. Next ctx_read will return full content.")
1000 }
1001 "invalidate" => {
1002 let path = get_str(args, "path").ok_or_else(|| {
1003 ErrorData::invalid_params("path is required for invalidate", None)
1004 })?;
1005 if cache.invalidate(&path) {
1006 format!(
1007 "Invalidated cache for {}. Next ctx_read will return full content.",
1008 crate::core::protocol::shorten_path(&path)
1009 )
1010 } else {
1011 format!(
1012 "{} was not in cache.",
1013 crate::core::protocol::shorten_path(&path)
1014 )
1015 }
1016 }
1017 _ => "Unknown action. Use: status, clear, invalidate".to_string(),
1018 };
1019 drop(cache);
1020 self.record_call("ctx_cache", 0, 0, Some(action)).await;
1021 result
1022 }
1023 "ctx_session" => {
1024 let action = get_str(args, "action")
1025 .ok_or_else(|| ErrorData::invalid_params("action is required", None))?;
1026 let value = get_str(args, "value");
1027 let sid = get_str(args, "session_id");
1028 let mut session = self.session.write().await;
1029 let result = crate::tools::ctx_session::handle(
1030 &mut session,
1031 &action,
1032 value.as_deref(),
1033 sid.as_deref(),
1034 );
1035 drop(session);
1036 self.record_call("ctx_session", 0, 0, Some(action)).await;
1037 result
1038 }
1039 "ctx_knowledge" => {
1040 let action = get_str(args, "action")
1041 .ok_or_else(|| ErrorData::invalid_params("action is required", None))?;
1042 let category = get_str(args, "category");
1043 let key = get_str(args, "key");
1044 let value = get_str(args, "value");
1045 let query = get_str(args, "query");
1046 let pattern_type = get_str(args, "pattern_type");
1047 let examples = get_str_array(args, "examples");
1048 let confidence: Option<f32> = args
1049 .as_ref()
1050 .and_then(|a| a.get("confidence"))
1051 .and_then(|v| v.as_f64())
1052 .map(|v| v as f32);
1053
1054 let session = self.session.read().await;
1055 let session_id = session.id.clone();
1056 let project_root = session.project_root.clone().unwrap_or_else(|| {
1057 std::env::current_dir()
1058 .map(|p| p.to_string_lossy().to_string())
1059 .unwrap_or_else(|_| "unknown".to_string())
1060 });
1061 drop(session);
1062
1063 let result = crate::tools::ctx_knowledge::handle(
1064 &project_root,
1065 &action,
1066 category.as_deref(),
1067 key.as_deref(),
1068 value.as_deref(),
1069 query.as_deref(),
1070 &session_id,
1071 pattern_type.as_deref(),
1072 examples,
1073 confidence,
1074 );
1075 self.record_call("ctx_knowledge", 0, 0, Some(action)).await;
1076 result
1077 }
1078 "ctx_agent" => {
1079 let action = get_str(args, "action")
1080 .ok_or_else(|| ErrorData::invalid_params("action is required", None))?;
1081 let agent_type = get_str(args, "agent_type");
1082 let role = get_str(args, "role");
1083 let message = get_str(args, "message");
1084 let category = get_str(args, "category");
1085 let to_agent = get_str(args, "to_agent");
1086 let status = get_str(args, "status");
1087
1088 let session = self.session.read().await;
1089 let project_root = session.project_root.clone().unwrap_or_else(|| {
1090 std::env::current_dir()
1091 .map(|p| p.to_string_lossy().to_string())
1092 .unwrap_or_else(|_| "unknown".to_string())
1093 });
1094 drop(session);
1095
1096 let current_agent_id = self.agent_id.read().await.clone();
1097 let result = crate::tools::ctx_agent::handle(
1098 &action,
1099 agent_type.as_deref(),
1100 role.as_deref(),
1101 &project_root,
1102 current_agent_id.as_deref(),
1103 message.as_deref(),
1104 category.as_deref(),
1105 to_agent.as_deref(),
1106 status.as_deref(),
1107 );
1108
1109 if action == "register" {
1110 if let Some(id) = result.split(':').nth(1) {
1111 let id = id.split_whitespace().next().unwrap_or("").to_string();
1112 if !id.is_empty() {
1113 *self.agent_id.write().await = Some(id);
1114 }
1115 }
1116 }
1117
1118 self.record_call("ctx_agent", 0, 0, Some(action)).await;
1119 result
1120 }
1121 "ctx_overview" => {
1122 let task = get_str(args, "task");
1123 let path = get_str(args, "path");
1124 let cache = self.cache.read().await;
1125 let result = crate::tools::ctx_overview::handle(
1126 &cache,
1127 task.as_deref(),
1128 path.as_deref(),
1129 self.crp_mode,
1130 );
1131 drop(cache);
1132 self.record_call("ctx_overview", 0, 0, Some("overview".to_string()))
1133 .await;
1134 result
1135 }
1136 "ctx_preload" => {
1137 let task = get_str(args, "task").unwrap_or_default();
1138 let path = get_str(args, "path");
1139 let mut cache = self.cache.write().await;
1140 let result = crate::tools::ctx_preload::handle(
1141 &mut cache,
1142 &task,
1143 path.as_deref(),
1144 self.crp_mode,
1145 );
1146 drop(cache);
1147 self.record_call("ctx_preload", 0, 0, Some("preload".to_string()))
1148 .await;
1149 result
1150 }
1151 "ctx_wrapped" => {
1152 let period = get_str(args, "period").unwrap_or_else(|| "week".to_string());
1153 let result = crate::tools::ctx_wrapped::handle(&period);
1154 self.record_call("ctx_wrapped", 0, 0, Some(period)).await;
1155 result
1156 }
1157 "ctx_semantic_search" => {
1158 let query = get_str(args, "query")
1159 .ok_or_else(|| ErrorData::invalid_params("query is required", None))?;
1160 let path = get_str(args, "path").unwrap_or_else(|| ".".to_string());
1161 let top_k = get_int(args, "top_k").unwrap_or(10) as usize;
1162 let action = get_str(args, "action").unwrap_or_default();
1163 let result = if action == "reindex" {
1164 crate::tools::ctx_semantic_search::handle_reindex(&path)
1165 } else {
1166 crate::tools::ctx_semantic_search::handle(&query, &path, top_k, self.crp_mode)
1167 };
1168 self.record_call("ctx_semantic_search", 0, 0, Some("semantic".to_string()))
1169 .await;
1170 result
1171 }
1172 _ => {
1173 return Err(ErrorData::invalid_params(
1174 format!("Unknown tool: {name}"),
1175 None,
1176 ));
1177 }
1178 };
1179
1180 let mut result_text = result_text;
1181
1182 if let Some(ctx) = auto_context {
1183 result_text = format!("{ctx}\n\n{result_text}");
1184 }
1185
1186 if name == "ctx_read" {
1187 let read_path = get_str(args, "path").unwrap_or_default();
1188 let project_root = {
1189 let session = self.session.read().await;
1190 session.project_root.clone()
1191 };
1192 let mut cache = self.cache.write().await;
1193 let enrich = crate::tools::autonomy::enrich_after_read(
1194 &self.autonomy,
1195 &mut cache,
1196 &read_path,
1197 project_root.as_deref(),
1198 );
1199 if let Some(hint) = enrich.related_hint {
1200 result_text = format!("{result_text}\n{hint}");
1201 }
1202
1203 crate::tools::autonomy::maybe_auto_dedup(&self.autonomy, &mut cache);
1204 }
1205
1206 if name == "ctx_shell" {
1207 let cmd = get_str(args, "command").unwrap_or_default();
1208 let output_tokens = crate::core::tokens::count_tokens(&result_text);
1209 let calls = self.tool_calls.read().await;
1210 let last_original = calls.last().map(|c| c.original_tokens).unwrap_or(0);
1211 drop(calls);
1212 if let Some(hint) = crate::tools::autonomy::shell_efficiency_hint(
1213 &self.autonomy,
1214 &cmd,
1215 last_original,
1216 output_tokens,
1217 ) {
1218 result_text = format!("{result_text}\n{hint}");
1219 }
1220 }
1221
1222 let skip_checkpoint = matches!(
1223 name,
1224 "ctx_compress"
1225 | "ctx_metrics"
1226 | "ctx_benchmark"
1227 | "ctx_analyze"
1228 | "ctx_cache"
1229 | "ctx_discover"
1230 | "ctx_dedup"
1231 | "ctx_session"
1232 | "ctx_knowledge"
1233 | "ctx_agent"
1234 | "ctx_wrapped"
1235 | "ctx_overview"
1236 | "ctx_preload"
1237 );
1238
1239 if !skip_checkpoint && self.increment_and_check() {
1240 if let Some(checkpoint) = self.auto_checkpoint().await {
1241 let combined = format!(
1242 "{result_text}\n\n--- AUTO CHECKPOINT (every {} calls) ---\n{checkpoint}",
1243 self.checkpoint_interval
1244 );
1245 return Ok(CallToolResult::success(vec![Content::text(combined)]));
1246 }
1247 }
1248
1249 let current_count = self.call_count.load(std::sync::atomic::Ordering::Relaxed);
1250 if current_count > 0 && current_count.is_multiple_of(100) {
1251 std::thread::spawn(cloud_background_tasks);
1252 }
1253
1254 Ok(CallToolResult::success(vec![Content::text(result_text)]))
1255 }
1256}
1257
1258fn build_instructions(crp_mode: CrpMode) -> String {
1259 build_instructions_with_client(crp_mode, "")
1260}
1261
1262fn build_instructions_with_client(crp_mode: CrpMode, client_name: &str) -> String {
1263 let profile = crate::core::litm::LitmProfile::from_client_name(client_name);
1264 let session_block = match crate::core::session::SessionState::load_latest() {
1265 Some(ref session) => {
1266 let positioned = crate::core::litm::position_optimize(session);
1267 format!(
1268 "\n\n--- ACTIVE SESSION (LITM P1: begin position, profile: {}) ---\n{}\n---\n",
1269 profile.name, positioned.begin_block
1270 )
1271 }
1272 None => String::new(),
1273 };
1274
1275 let knowledge_block = {
1276 let project_root = crate::core::session::SessionState::load_latest()
1277 .and_then(|s| s.project_root)
1278 .or_else(|| {
1279 std::env::current_dir()
1280 .ok()
1281 .map(|p| p.to_string_lossy().to_string())
1282 });
1283 match project_root {
1284 Some(root) => {
1285 let knowledge = crate::core::knowledge::ProjectKnowledge::load(&root);
1286 match knowledge {
1287 Some(k) if !k.facts.is_empty() || !k.patterns.is_empty() => {
1288 format!("\n--- PROJECT KNOWLEDGE ---\n{}\n---\n", k.format_summary())
1289 }
1290 _ => String::new(),
1291 }
1292 }
1293 None => String::new(),
1294 }
1295 };
1296
1297 let mut base = format!("\
1300CRITICAL: NEVER use native Read, Shell, Grep, or ls tools. ALWAYS use ctx_read, ctx_shell, ctx_search, ctx_tree instead.\n\
1301\n\
1302lean-ctx MCP — tool mapping:\n\
1303• Read/cat/head/tail -> ctx_read(path, mode)\n\
1304• Shell/bash -> ctx_shell(command)\n\
1305• Grep/rg -> ctx_search(pattern, path)\n\
1306• ls/find -> ctx_tree(path, depth)\n\
1307• Write, StrReplace, Delete, Glob -> use normally (no replacement)\n\
1308\n\
1309ctx_read modes: full (cached, for edits), map (deps+API), signatures, diff, task (IB-filtered), \
1310reference, aggressive, entropy, lines:N-M. Auto-selects when unspecified. Re-reads ~13 tokens. File refs F1,F2.. persist.\n\
1311If ctx_read returns 'cached': use fresh=true, start_line=N, or mode='lines:N-M' to re-read.\n\
1312\n\
1313AUTONOMY: lean-ctx auto-runs ctx_overview, ctx_preload, ctx_dedup, ctx_compress behind the scenes.\n\
1314Focus on: ctx_read, ctx_shell, ctx_search, ctx_tree. Use ctx_session for memory, ctx_knowledge for project facts.\n\
1315ctx_shell raw=true: skip compression for small/critical outputs. Full output tee files at ~/.lean-ctx/tee/.\n\
1316\n\
1317Auto-checkpoint every 15 calls. Cache clears after 5 min idle.\n\
1318\n\
1319CEP v1: 1.ACT FIRST 2.DELTA ONLY (Fn refs) 3.STRUCTURED (+/-/~) 4.ONE LINE PER ACTION 5.QUALITY ANCHOR\n\
1320\n\
1321{decoder_block}\n\
1322\n\
1323{session_block}\
1324{knowledge_block}\
1325\n\
1326--- TOOL ENFORCEMENT (LITM-END) ---\n\
1327Read/cat/head/tail -> ctx_read | Shell/bash -> ctx_shell | Grep/rg -> ctx_search | ls/find -> ctx_tree\n\
1328Write, StrReplace, Delete, Glob -> use normally",
1329 decoder_block = crate::core::protocol::instruction_decoder_block()
1330 );
1331
1332 if should_use_unified(client_name) {
1333 base.push_str(
1334 "\n\n\
1335UNIFIED TOOL MODE (active):\n\
1336Additional tools are accessed via ctx() meta-tool: ctx(tool=\"<name>\", ...params).\n\
1337See the ctx() tool description for available sub-tools.\n",
1338 );
1339 }
1340
1341 let base = base;
1342 match crp_mode {
1343 CrpMode::Off => base,
1344 CrpMode::Compact => {
1345 format!(
1346 "{base}\n\n\
1347CRP MODE: compact\n\
1348Compact Response Protocol:\n\
1349• Omit filler words, articles, redundant phrases\n\
1350• Abbreviate: fn, cfg, impl, deps, req, res, ctx, err, ret, arg, val, ty, mod\n\
1351• Compact lists over prose, code blocks over explanations\n\
1352• Code changes: diff lines (+/-) only, not full files\n\
1353• TARGET: <=200 tokens per response unless code edits require more\n\
1354• THINK LESS: Tool outputs are pre-analyzed. Trust summaries directly."
1355 )
1356 }
1357 CrpMode::Tdd => {
1358 format!(
1359 "{base}\n\n\
1360CRP MODE: tdd (Token Dense Dialect)\n\
1361Maximize information density. Every token must carry meaning.\n\
1362\n\
1363RESPONSE RULES:\n\
1364• Drop articles, filler words, pleasantries\n\
1365• Reference files by Fn refs only, never full paths\n\
1366• Code changes: diff lines only (+/-), not full files\n\
1367• No explanations unless asked\n\
1368• Tables for structured data\n\
1369• Abbreviations: fn, cfg, impl, deps, req, res, ctx, err, ret, arg, val, ty, mod\n\
1370\n\
1371CHANGE NOTATION:\n\
1372+F1:42 param(timeout:Duration) — added\n\
1373-F1:10-15 — removed\n\
1374~F1:42 validate_token -> verify_jwt — changed\n\
1375\n\
1376STATUS: ctx_read(F1) -> 808L cached ok | cargo test -> 82 passed 0 failed\n\
1377\n\
1378TOKEN BUDGET: <=150 tokens per response. Exceed only for multi-file edits.\n\
1379THINK LESS: Tool outputs are pre-analyzed. Trust compressed outputs directly.\n\
1380ZERO NARRATION: Act, then report result in 1 line."
1381 )
1382 }
1383 }
1384}
1385
1386fn tool_def(name: &'static str, description: &'static str, schema_value: Value) -> Tool {
1387 let schema: Map<String, Value> = match schema_value {
1388 Value::Object(map) => map,
1389 _ => Map::new(),
1390 };
1391 Tool::new(name, description, Arc::new(schema))
1392}
1393
1394fn unified_tool_defs() -> Vec<Tool> {
1395 vec![
1396 tool_def(
1397 "ctx_read",
1398 "Read file (cached, compressed). Modes: full|map|signatures|diff|aggressive|entropy|task|reference|lines:N-M. fresh=true re-reads.",
1399 json!({
1400 "type": "object",
1401 "properties": {
1402 "path": { "type": "string", "description": "File path" },
1403 "mode": { "type": "string" },
1404 "start_line": { "type": "integer" },
1405 "fresh": { "type": "boolean" }
1406 },
1407 "required": ["path"]
1408 }),
1409 ),
1410 tool_def(
1411 "ctx_shell",
1412 "Run shell command (compressed output). raw=true skips compression.",
1413 json!({
1414 "type": "object",
1415 "properties": {
1416 "command": { "type": "string", "description": "Shell command" },
1417 "raw": { "type": "boolean", "description": "Skip compression for full output" }
1418 },
1419 "required": ["command"]
1420 }),
1421 ),
1422 tool_def(
1423 "ctx_search",
1424 "Regex code search (.gitignore aware).",
1425 json!({
1426 "type": "object",
1427 "properties": {
1428 "pattern": { "type": "string", "description": "Regex pattern" },
1429 "path": { "type": "string" },
1430 "ext": { "type": "string" },
1431 "max_results": { "type": "integer" },
1432 "ignore_gitignore": { "type": "boolean" }
1433 },
1434 "required": ["pattern"]
1435 }),
1436 ),
1437 tool_def(
1438 "ctx_tree",
1439 "Directory listing with file counts.",
1440 json!({
1441 "type": "object",
1442 "properties": {
1443 "path": { "type": "string" },
1444 "depth": { "type": "integer" },
1445 "show_hidden": { "type": "boolean" }
1446 }
1447 }),
1448 ),
1449 tool_def(
1450 "ctx",
1451 "Meta-tool: set tool= to sub-tool name. Sub-tools: compress (checkpoint), metrics (stats), \
1452analyze (entropy), cache (status|clear|invalidate), discover (missed patterns), smart_read (auto-mode), \
1453delta (incremental diff), dedup (cross-file), fill (budget-aware batch read), intent (auto-read by task), \
1454response (compress LLM text), context (session state), graph (build|related|symbol|impact|status), \
1455session (load|save|task|finding|decision|status|reset|list|cleanup), \
1456knowledge (remember|recall|pattern|consolidate|status|remove|export), \
1457agent (register|post|read|status|list|info), overview (project map), \
1458wrapped (savings report), benchmark (file|project), multi_read (batch), semantic_search (BM25).",
1459 json!({
1460 "type": "object",
1461 "properties": {
1462 "tool": {
1463 "type": "string",
1464 "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"
1465 },
1466 "action": { "type": "string" },
1467 "path": { "type": "string" },
1468 "paths": { "type": "array", "items": { "type": "string" } },
1469 "query": { "type": "string" },
1470 "value": { "type": "string" },
1471 "category": { "type": "string" },
1472 "key": { "type": "string" },
1473 "budget": { "type": "integer" },
1474 "task": { "type": "string" },
1475 "mode": { "type": "string" },
1476 "text": { "type": "string" },
1477 "message": { "type": "string" },
1478 "session_id": { "type": "string" },
1479 "period": { "type": "string" },
1480 "format": { "type": "string" },
1481 "agent_type": { "type": "string" },
1482 "role": { "type": "string" },
1483 "status": { "type": "string" },
1484 "pattern_type": { "type": "string" },
1485 "examples": { "type": "array", "items": { "type": "string" } },
1486 "confidence": { "type": "number" },
1487 "project_root": { "type": "string" },
1488 "include_signatures": { "type": "boolean" },
1489 "limit": { "type": "integer" },
1490 "to_agent": { "type": "string" },
1491 "show_hidden": { "type": "boolean" }
1492 },
1493 "required": ["tool"]
1494 }),
1495 ),
1496 ]
1497}
1498
1499fn should_use_unified(client_name: &str) -> bool {
1500 if std::env::var("LEAN_CTX_FULL_TOOLS").is_ok() {
1501 return false;
1502 }
1503 if std::env::var("LEAN_CTX_UNIFIED").is_ok() {
1504 return true;
1505 }
1506 let _ = client_name;
1507 false
1508}
1509
1510fn get_str_array(args: &Option<serde_json::Map<String, Value>>, key: &str) -> Option<Vec<String>> {
1511 let arr = args.as_ref()?.get(key)?.as_array()?;
1512 let mut out = Vec::with_capacity(arr.len());
1513 for v in arr {
1514 let s = v.as_str()?.to_string();
1515 out.push(s);
1516 }
1517 Some(out)
1518}
1519
1520fn get_str(args: &Option<serde_json::Map<String, Value>>, key: &str) -> Option<String> {
1521 args.as_ref()?.get(key)?.as_str().map(|s| s.to_string())
1522}
1523
1524fn get_int(args: &Option<serde_json::Map<String, Value>>, key: &str) -> Option<i64> {
1525 args.as_ref()?.get(key)?.as_i64()
1526}
1527
1528fn get_bool(args: &Option<serde_json::Map<String, Value>>, key: &str) -> Option<bool> {
1529 args.as_ref()?.get(key)?.as_bool()
1530}
1531
1532fn execute_command(command: &str) -> String {
1533 let (shell, flag) = crate::shell::shell_and_flag();
1534 let output = std::process::Command::new(&shell)
1535 .arg(&flag)
1536 .arg(command)
1537 .env("LEAN_CTX_ACTIVE", "1")
1538 .output();
1539
1540 match output {
1541 Ok(out) => {
1542 let stdout = String::from_utf8_lossy(&out.stdout);
1543 let stderr = String::from_utf8_lossy(&out.stderr);
1544 if stdout.is_empty() {
1545 stderr.to_string()
1546 } else if stderr.is_empty() {
1547 stdout.to_string()
1548 } else {
1549 format!("{stdout}\n{stderr}")
1550 }
1551 }
1552 Err(e) => format!("ERROR: {e}"),
1553 }
1554}
1555
1556fn detect_project_root(file_path: &str) -> Option<String> {
1557 let mut dir = std::path::Path::new(file_path).parent()?;
1558 loop {
1559 if dir.join(".git").exists() {
1560 return Some(dir.to_string_lossy().to_string());
1561 }
1562 dir = dir.parent()?;
1563 }
1564}
1565
1566fn cloud_background_tasks() {
1567 use crate::core::config::Config;
1568
1569 let mut config = Config::load();
1570 let today = chrono::Local::now().format("%Y-%m-%d").to_string();
1571
1572 let already_contributed = config
1573 .cloud
1574 .last_contribute
1575 .as_deref()
1576 .map(|d| d == today)
1577 .unwrap_or(false);
1578 let already_synced = config
1579 .cloud
1580 .last_sync
1581 .as_deref()
1582 .map(|d| d == today)
1583 .unwrap_or(false);
1584 let already_pulled = config
1585 .cloud
1586 .last_model_pull
1587 .as_deref()
1588 .map(|d| d == today)
1589 .unwrap_or(false);
1590
1591 if config.cloud.contribute_enabled && !already_contributed {
1592 if let Some(home) = dirs::home_dir() {
1593 let mode_stats_path = home.join(".lean-ctx").join("mode_stats.json");
1594 if let Ok(data) = std::fs::read_to_string(&mode_stats_path) {
1595 if let Ok(predictor) = serde_json::from_str::<serde_json::Value>(&data) {
1596 let mut entries = Vec::new();
1597 if let Some(history) = predictor["history"].as_object() {
1598 for (_key, outcomes) in history {
1599 if let Some(arr) = outcomes.as_array() {
1600 for outcome in arr.iter().rev().take(3) {
1601 let ext = outcome["ext"].as_str().unwrap_or("unknown");
1602 let mode = outcome["mode"].as_str().unwrap_or("full");
1603 let t_in = outcome["tokens_in"].as_u64().unwrap_or(0);
1604 let t_out = outcome["tokens_out"].as_u64().unwrap_or(0);
1605 let ratio = if t_in > 0 {
1606 1.0 - t_out as f64 / t_in as f64
1607 } else {
1608 0.0
1609 };
1610 let bucket = match t_in {
1611 0..=500 => "0-500",
1612 501..=2000 => "500-2k",
1613 2001..=10000 => "2k-10k",
1614 _ => "10k+",
1615 };
1616 entries.push(serde_json::json!({
1617 "file_ext": format!(".{ext}"),
1618 "size_bucket": bucket,
1619 "best_mode": mode,
1620 "compression_ratio": (ratio * 100.0).round() / 100.0,
1621 }));
1622 if entries.len() >= 200 {
1623 break;
1624 }
1625 }
1626 }
1627 if entries.len() >= 200 {
1628 break;
1629 }
1630 }
1631 }
1632 if !entries.is_empty() && crate::cloud_client::contribute(&entries).is_ok() {
1633 config.cloud.last_contribute = Some(today.clone());
1634 }
1635 }
1636 }
1637 }
1638 }
1639
1640 if crate::cloud_client::check_pro() {
1641 if !already_synced {
1642 let stats_data = crate::core::stats::format_gain_json();
1643 if let Ok(parsed) = serde_json::from_str::<serde_json::Value>(&stats_data) {
1644 let entry = serde_json::json!({
1645 "date": &today,
1646 "tokens_original": parsed["total_original_tokens"].as_i64().unwrap_or(0),
1647 "tokens_compressed": parsed["total_compressed_tokens"].as_i64().unwrap_or(0),
1648 "tokens_saved": parsed["total_saved_tokens"].as_i64().unwrap_or(0),
1649 "tool_calls": parsed["total_calls"].as_i64().unwrap_or(0),
1650 "cache_hits": parsed["cache_hits"].as_i64().unwrap_or(0),
1651 "cache_misses": parsed["cache_misses"].as_i64().unwrap_or(0),
1652 });
1653 if crate::cloud_client::sync_stats(&[entry]).is_ok() {
1654 config.cloud.last_sync = Some(today.clone());
1655 }
1656 }
1657 }
1658
1659 if !already_pulled {
1660 if let Ok(data) = crate::cloud_client::pull_pro_models() {
1661 let _ = crate::cloud_client::save_pro_models(&data);
1662 config.cloud.last_model_pull = Some(today.clone());
1663 }
1664 }
1665 }
1666
1667 let _ = config.save();
1668}
1669
1670pub fn build_instructions_for_test(crp_mode: CrpMode) -> String {
1671 build_instructions(crp_mode)
1672}
1673
1674pub fn tool_descriptions_for_test() -> Vec<(&'static str, &'static str)> {
1675 let mut result = Vec::new();
1676 let tools_json = list_all_tool_defs();
1677 for (name, desc, _) in tools_json {
1678 result.push((name, desc));
1679 }
1680 result
1681}
1682
1683pub fn tool_schemas_json_for_test() -> String {
1684 let tools_json = list_all_tool_defs();
1685 let schemas: Vec<String> = tools_json
1686 .iter()
1687 .map(|(name, _, schema)| format!("{}: {}", name, schema))
1688 .collect();
1689 schemas.join("\n")
1690}
1691
1692fn list_all_tool_defs() -> Vec<(&'static str, &'static str, Value)> {
1693 vec![
1694 ("ctx_read", "Read file (cached, compressed). Re-reads ~13 tok. Auto-selects optimal mode. \
1695Modes: 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"]})),
1696 ("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"]})),
1697 ("ctx_tree", "Directory listing with file counts.", json!({"type": "object", "properties": {"path": {"type": "string"}, "depth": {"type": "integer"}, "show_hidden": {"type": "boolean"}}})),
1698 ("ctx_shell", "Run shell command (compressed output, 90+ patterns).", json!({"type": "object", "properties": {"command": {"type": "string"}}, "required": ["command"]})),
1699 ("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"]})),
1700 ("ctx_compress", "Context checkpoint for long conversations.", json!({"type": "object", "properties": {"include_signatures": {"type": "boolean"}}})),
1701 ("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"]})),
1702 ("ctx_metrics", "Session token stats, cache rates, per-tool savings.", json!({"type": "object", "properties": {}})),
1703 ("ctx_analyze", "Entropy analysis — recommends optimal compression mode for a file.", json!({"type": "object", "properties": {"path": {"type": "string"}}, "required": ["path"]})),
1704 ("ctx_cache", "Cache ops: status|clear|invalidate.", json!({"type": "object", "properties": {"action": {"type": "string"}, "path": {"type": "string"}}, "required": ["action"]})),
1705 ("ctx_discover", "Find missed compression opportunities in shell history.", json!({"type": "object", "properties": {"limit": {"type": "integer"}}})),
1706 ("ctx_smart_read", "Auto-select optimal read mode for a file.", json!({"type": "object", "properties": {"path": {"type": "string"}}, "required": ["path"]})),
1707 ("ctx_delta", "Incremental diff — sends only changed lines since last read.", json!({"type": "object", "properties": {"path": {"type": "string"}}, "required": ["path"]})),
1708 ("ctx_dedup", "Cross-file dedup: analyze or apply shared block references.", json!({"type": "object", "properties": {"action": {"type": "string"}}})),
1709 ("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"]})),
1710 ("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"]})),
1711 ("ctx_response", "Compress LLM response text (remove filler, apply TDD).", json!({"type": "object", "properties": {"text": {"type": "string"}}, "required": ["text"]})),
1712 ("ctx_context", "Session context overview — cached files, seen files, session state.", json!({"type": "object", "properties": {}})),
1713 ("ctx_graph", "Code dependency graph. Actions: build (index project), related (find files connected to path), \
1714symbol (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"]})),
1715 ("ctx_session", "Cross-session memory (CCP). Actions: load (restore previous session ~400 tok), \
1716save, status, task (set current task), finding (record discovery), decision (record choice), \
1717reset, list (show sessions), cleanup.", json!({"type": "object", "properties": {"action": {"type": "string"}, "value": {"type": "string"}, "session_id": {"type": "string"}}, "required": ["action"]})),
1718 ("ctx_knowledge", "Persistent project knowledge (survives sessions). Actions: remember (store fact with category+key+value), \
1719recall (search by query), pattern (record naming/structure pattern), consolidate (extract session findings into knowledge), \
1720status (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"]})),
1721 ("ctx_agent", "Multi-agent coordination (shared message bus). Actions: register (join with agent_type+role), \
1722post (broadcast or direct message with category), read (poll messages), status (update state: active|idle|finished), \
1723list, info.", json!({"type": "object", "properties": {"action": {"type": "string"}, "agent_type": {"type": "string"}, "role": {"type": "string"}, "message": {"type": "string"}}, "required": ["action"]})),
1724 ("ctx_overview", "Task-relevant project map — use at session start.", json!({"type": "object", "properties": {"task": {"type": "string"}, "path": {"type": "string"}}})),
1725 ("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"]})),
1726 ("ctx_wrapped", "Savings report card. Periods: week|month|all.", json!({"type": "object", "properties": {"period": {"type": "string"}}})),
1727 ("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"]})),
1728 ]
1729}
1730
1731#[cfg(test)]
1732mod tests {
1733 use super::*;
1734
1735 #[test]
1736 fn test_should_use_unified_defaults_to_false() {
1737 assert!(!should_use_unified("cursor"));
1738 assert!(!should_use_unified("claude-code"));
1739 assert!(!should_use_unified("windsurf"));
1740 assert!(!should_use_unified(""));
1741 assert!(!should_use_unified("some-unknown-client"));
1742 }
1743
1744 #[test]
1745 fn test_unified_tool_count() {
1746 let tools = unified_tool_defs();
1747 assert_eq!(tools.len(), 5, "Expected 5 unified tools");
1748 }
1749}