Skip to main content

aptu_coder/
lib.rs

1// SPDX-FileCopyrightText: 2026 aptu-coder contributors
2// SPDX-License-Identifier: Apache-2.0
3//! Rust MCP server for code structure analysis using tree-sitter.
4//!
5//! This crate exposes four MCP tools for multiple programming languages:
6//!
7//! - **`analyze_directory`**: Directory tree with file counts and structure
8//! - **`analyze_file`**: Semantic extraction (functions, classes, imports)
9//! - **`analyze_symbol`**: Call graph analysis (callers and callees)
10//! - **`analyze_module`**: Lightweight function and import index
11//!
12//! Key entry points:
13//! - [`analyze::analyze_directory`]: Analyze entire directory tree
14//! - [`analyze::analyze_file`]: Analyze single file
15//!
16//! Languages supported: Rust, Go, Java, Python, TypeScript, TSX, Fortran, JavaScript, C/C++, C#.
17
18pub mod logging;
19pub mod metrics;
20
21pub use aptu_coder_core::analyze;
22use aptu_coder_core::types::STDIN_MAX_BYTES;
23use aptu_coder_core::{cache, completion, graph, traversal, types};
24
25pub(crate) const EXCLUDED_DIRS: &[&str] = &[
26    "node_modules",
27    "vendor",
28    ".git",
29    "__pycache__",
30    "target",
31    "dist",
32    "build",
33    ".venv",
34];
35
36use aptu_coder_core::cache::AnalysisCache;
37use aptu_coder_core::formatter::{
38    format_file_details_paginated, format_file_details_summary, format_focused_paginated,
39    format_module_info, format_structure_paginated, format_summary,
40};
41use aptu_coder_core::formatter_defuse::format_focused_paginated_defuse;
42use aptu_coder_core::pagination::{
43    CursorData, DEFAULT_PAGE_SIZE, PaginationMode, decode_cursor, encode_cursor, paginate_slice,
44};
45use aptu_coder_core::traversal::{
46    WalkEntry, changed_files_from_git_ref, filter_entries_by_git_ref, walk_directory,
47};
48use aptu_coder_core::types::{
49    AnalysisMode, AnalyzeDirectoryParams, AnalyzeFileParams, AnalyzeModuleParams,
50    AnalyzeSymbolParams, EditOverwriteOutput, EditOverwriteParams, EditReplaceOutput,
51    EditReplaceParams, SymbolMatchMode,
52};
53use logging::LogEvent;
54use rmcp::handler::server::tool::{ToolRouter, schema_for_type};
55use rmcp::handler::server::wrapper::Parameters;
56use rmcp::model::{
57    CallToolResult, CancelledNotificationParam, CompleteRequestParams, CompleteResult,
58    CompletionInfo, Content, ErrorData, Implementation, InitializeRequestParams, InitializeResult,
59    LoggingLevel, LoggingMessageNotificationParam, Meta, Notification, NumberOrString,
60    ProgressNotificationParam, ProgressToken, ServerCapabilities, ServerNotification,
61    SetLevelRequestParams,
62};
63use rmcp::service::{NotificationContext, RequestContext};
64use rmcp::{Peer, RoleServer, ServerHandler, tool, tool_handler, tool_router};
65use serde_json::Value;
66use std::path::{Path, PathBuf};
67use std::sync::{Arc, Mutex};
68use tokio::sync::{Mutex as TokioMutex, RwLock, mpsc};
69use tracing::{instrument, warn};
70use tracing_subscriber::filter::LevelFilter;
71
72#[cfg(unix)]
73use nix::sys::resource::{Resource, setrlimit};
74
75static GLOBAL_SESSION_COUNTER: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
76
77const SIZE_LIMIT: usize = 50_000;
78
79/// Returns `true` when `summary=true` and a `cursor` are both provided, which is an invalid
80/// combination since summary mode and pagination are mutually exclusive.
81#[must_use]
82pub fn summary_cursor_conflict(summary: Option<bool>, cursor: Option<&str>) -> bool {
83    summary == Some(true) && cursor.is_some()
84}
85
86#[must_use]
87fn error_meta(
88    category: &'static str,
89    is_retryable: bool,
90    suggested_action: &'static str,
91) -> serde_json::Value {
92    serde_json::json!({
93        "errorCategory": category,
94        "isRetryable": is_retryable,
95        "suggestedAction": suggested_action,
96    })
97}
98
99#[must_use]
100fn err_to_tool_result(e: ErrorData) -> CallToolResult {
101    CallToolResult::error(vec![Content::text(e.message)])
102}
103
104fn err_to_tool_result_from_pagination(
105    e: aptu_coder_core::pagination::PaginationError,
106) -> CallToolResult {
107    let msg = format!("Pagination error: {}", e);
108    CallToolResult::error(vec![Content::text(msg)])
109}
110
111fn no_cache_meta() -> Meta {
112    let mut m = serde_json::Map::new();
113    m.insert(
114        "cache_hint".to_string(),
115        serde_json::Value::String("no-cache".to_string()),
116    );
117    Meta(m)
118}
119
120/// Validates that a path is within the current working directory.
121/// For `require_exists=true`, the path must exist and be canonicalizable.
122/// For `require_exists=false`, the parent directory must exist and be canonicalizable.
123fn validate_path(path: &str, require_exists: bool) -> Result<std::path::PathBuf, ErrorData> {
124    // Canonicalize the allowed root (CWD) to resolve symlinks
125    let allowed_root = std::fs::canonicalize(std::env::current_dir().map_err(|_| {
126        ErrorData::new(
127            rmcp::model::ErrorCode::INVALID_PARAMS,
128            "path is outside the allowed root".to_string(),
129            Some(error_meta(
130                "validation",
131                false,
132                "ensure the working directory is accessible",
133            )),
134        )
135    })?)
136    .unwrap_or_else(|_| std::env::current_dir().unwrap_or_default());
137
138    let canonical_path = if require_exists {
139        std::fs::canonicalize(path).map_err(|e| {
140            let msg = match e.kind() {
141                std::io::ErrorKind::NotFound => format!("path not found: {path}"),
142                std::io::ErrorKind::PermissionDenied => format!("permission denied: {path}"),
143                _ => "path is outside the allowed root".to_string(),
144            };
145            ErrorData::new(
146                rmcp::model::ErrorCode::INVALID_PARAMS,
147                msg,
148                Some(error_meta(
149                    "validation",
150                    false,
151                    "provide a valid path within the working directory",
152                )),
153            )
154        })?
155    } else {
156        // For non-existent files (edit_overwrite), walk up the path until we find an existing ancestor
157        let p = std::path::Path::new(path);
158        let mut ancestor = p.to_path_buf();
159        let mut suffix = std::path::PathBuf::new();
160
161        loop {
162            if ancestor.exists() {
163                break;
164            }
165            if let Some(parent) = ancestor.parent() {
166                if let Some(file_name) = ancestor.file_name() {
167                    suffix = std::path::PathBuf::from(file_name).join(&suffix);
168                }
169                ancestor = parent.to_path_buf();
170            } else {
171                // No existing ancestor found — use allowed_root as anchor
172                ancestor = allowed_root.clone();
173                break;
174            }
175        }
176
177        let canonical_base =
178            std::fs::canonicalize(&ancestor).unwrap_or_else(|_| allowed_root.clone());
179        canonical_base.join(&suffix)
180    };
181
182    if !canonical_path.starts_with(&allowed_root) {
183        return Err(ErrorData::new(
184            rmcp::model::ErrorCode::INVALID_PARAMS,
185            "path is outside the allowed root".to_string(),
186            Some(error_meta(
187                "validation",
188                false,
189                "provide a path within the current working directory",
190            )),
191        ));
192    }
193
194    Ok(canonical_path)
195}
196
197/// Maps an io::Error to an ErrorData with kind-specific message and preserved context.
198fn io_error_to_path_error(
199    err: &std::io::Error,
200    path_context: &str,
201    suggested_action: &'static str,
202) -> ErrorData {
203    let msg = match err.kind() {
204        std::io::ErrorKind::NotFound => format!("{path_context} not found"),
205        std::io::ErrorKind::PermissionDenied => format!("permission denied: {path_context}"),
206        _ => format!("{path_context} is invalid"),
207    };
208    let mut meta = error_meta("validation", false, suggested_action);
209    // Preserve io::Error context in data field
210    if let Some(obj) = meta.as_object_mut() {
211        obj.insert(
212            "ioErrorKind".to_string(),
213            serde_json::json!(format!("{:?}", err.kind())),
214        );
215        obj.insert(
216            "ioErrorSource".to_string(),
217            serde_json::json!(err.to_string()),
218        );
219    }
220    ErrorData::new(rmcp::model::ErrorCode::INVALID_PARAMS, msg, Some(meta))
221}
222
223/// Validates a path relative to a working directory.
224/// The working_dir itself must be within the server CWD.
225/// The resolved path must also be within the working_dir.
226fn validate_path_in_dir(
227    path: &str,
228    require_exists: bool,
229    working_dir: &std::path::Path,
230) -> Result<std::path::PathBuf, ErrorData> {
231    // Canonicalize the working_dir to resolve symlinks
232    let canonical_working_dir = std::fs::canonicalize(working_dir).map_err(|e| {
233        io_error_to_path_error(&e, "working_dir", "provide a valid working directory")
234    })?;
235
236    // Verify working_dir is actually a directory
237    if !std::fs::metadata(&canonical_working_dir)
238        .map(|m| m.is_dir())
239        .unwrap_or(false)
240    {
241        return Err(ErrorData::new(
242            rmcp::model::ErrorCode::INVALID_PARAMS,
243            "working_dir must be a directory".to_string(),
244            Some(error_meta(
245                "validation",
246                false,
247                "provide a valid directory path",
248            )),
249        ));
250    }
251
252    // Verify working_dir is within the server CWD (same bounds check as validate_path)
253    let allowed_root = std::fs::canonicalize(std::env::current_dir().map_err(|_| {
254        ErrorData::new(
255            rmcp::model::ErrorCode::INVALID_PARAMS,
256            "path is outside the allowed root".to_string(),
257            Some(error_meta(
258                "validation",
259                false,
260                "ensure the working directory is accessible",
261            )),
262        )
263    })?)
264    .unwrap_or_else(|_| std::env::current_dir().unwrap_or_default());
265
266    if !canonical_working_dir.starts_with(&allowed_root) {
267        return Err(ErrorData::new(
268            rmcp::model::ErrorCode::INVALID_PARAMS,
269            "working_dir is outside the allowed root".to_string(),
270            Some(error_meta(
271                "validation",
272                false,
273                "provide a working directory within the current working directory",
274            )),
275        ));
276    }
277
278    // Now resolve the target path relative to working_dir
279    let canonical_path = if require_exists {
280        let target_path = canonical_working_dir.join(path);
281        std::fs::canonicalize(&target_path).map_err(|e| {
282            io_error_to_path_error(
283                &e,
284                path,
285                "provide a valid path within the working directory",
286            )
287        })?
288    } else {
289        // For non-existent files, walk up the path until we find an existing ancestor
290        let p = std::path::Path::new(path);
291        let mut ancestor = p.to_path_buf();
292        let mut suffix = std::path::PathBuf::new();
293
294        loop {
295            let full_path = canonical_working_dir.join(&ancestor);
296            if full_path.exists() {
297                break;
298            }
299            if let Some(parent) = ancestor.parent() {
300                if let Some(file_name) = ancestor.file_name() {
301                    suffix = std::path::PathBuf::from(file_name).join(&suffix);
302                }
303                ancestor = parent.to_path_buf();
304            } else {
305                // No existing ancestor found — use working_dir as anchor
306                ancestor = std::path::PathBuf::new();
307                break;
308            }
309        }
310
311        let canonical_base = canonical_working_dir.join(&ancestor);
312        let canonical_base =
313            std::fs::canonicalize(&canonical_base).unwrap_or(canonical_working_dir.clone());
314        canonical_base.join(&suffix)
315    };
316
317    // Verify the resolved path is within working_dir.
318    // PathBuf::starts_with compares path *components*, not raw bytes, so
319    // a sibling directory whose name shares our prefix (e.g. "/work_evil"
320    // when the allowed root is "/work") is correctly rejected -- this is
321    // the exact prefix-confusion vector exploited in CVE-2025-53110 against
322    // @modelcontextprotocol/server-filesystem.  Do not replace this check
323    // with a string-level prefix comparison.
324    if !canonical_path.starts_with(&canonical_working_dir) {
325        return Err(ErrorData::new(
326            rmcp::model::ErrorCode::INVALID_PARAMS,
327            "path is outside the working directory".to_string(),
328            Some(error_meta(
329                "validation",
330                false,
331                "provide a path within the working directory",
332            )),
333        ));
334    }
335
336    Ok(canonical_path)
337}
338
339/// Helper function for paginating focus chains (callers or callees).
340/// Returns (items, re-encoded_cursor_option).
341fn paginate_focus_chains(
342    chains: &[graph::InternalCallChain],
343    mode: PaginationMode,
344    offset: usize,
345    page_size: usize,
346) -> Result<(Vec<graph::InternalCallChain>, Option<String>), ErrorData> {
347    let paginated = paginate_slice(chains, offset, page_size, mode).map_err(|e| {
348        ErrorData::new(
349            rmcp::model::ErrorCode::INTERNAL_ERROR,
350            e.to_string(),
351            Some(error_meta("transient", true, "retry the request")),
352        )
353    })?;
354
355    if paginated.next_cursor.is_none() && offset == 0 {
356        return Ok((paginated.items, None));
357    }
358
359    let next = if let Some(raw_cursor) = paginated.next_cursor {
360        let decoded = decode_cursor(&raw_cursor).map_err(|e| {
361            ErrorData::new(
362                rmcp::model::ErrorCode::INVALID_PARAMS,
363                e.to_string(),
364                Some(error_meta("validation", false, "invalid cursor format")),
365            )
366        })?;
367        Some(
368            encode_cursor(&CursorData {
369                mode,
370                offset: decoded.offset,
371            })
372            .map_err(|e| {
373                ErrorData::new(
374                    rmcp::model::ErrorCode::INVALID_PARAMS,
375                    e.to_string(),
376                    Some(error_meta("validation", false, "invalid cursor format")),
377                )
378            })?,
379        )
380    } else {
381        None
382    };
383
384    Ok((paginated.items, next))
385}
386
387/// Resolve the preferred shell for command execution.
388/// Priority: APTU_SHELL env var > bash (PATH search) > /bin/sh (unix) / cmd (windows).
389/// APTU_SHELL is honored on all platforms so callers can override the shell uniformly.
390fn resolve_shell() -> String {
391    if let Ok(shell) = std::env::var("APTU_SHELL") {
392        return shell;
393    }
394    #[cfg(unix)]
395    {
396        if which::which("bash").is_ok() {
397            return "bash".to_string();
398        }
399        "/bin/sh".to_string()
400    }
401    #[cfg(not(unix))]
402    {
403        "cmd".to_string()
404    }
405}
406
407/// MCP server handler that wires the four analysis tools to the rmcp transport.
408///
409/// Holds shared state: tool router, analysis cache, peer connection, log-level filter,
410/// log event channel, metrics sender, and per-session sequence tracking.
411#[derive(Clone)]
412pub struct CodeAnalyzer {
413    // Wrapped in Arc<RwLock> to enable interior mutability for profile-based tool routing.
414    // All clones share the same router instance (per-session state).
415    // Read lock acquired by list_tools/call_tool; write lock acquired during on_initialized
416    // to disable tools based on client profile.
417    // IMPORTANT: Do not perform long-running I/O while holding the write lock in
418    // on_initialized. The write lock blocks all concurrent list_tools/call_tool calls
419    // for the duration. Keep the critical section to disable_route() calls only.
420    #[allow(dead_code)]
421    pub(crate) tool_router: Arc<RwLock<ToolRouter<Self>>>,
422    cache: AnalysisCache,
423    exec_cache: moka::future::Cache<(String, String), types::ShellOutput>,
424    peer: Arc<TokioMutex<Option<Peer<RoleServer>>>>,
425    log_level_filter: Arc<Mutex<LevelFilter>>,
426    event_rx: Arc<TokioMutex<Option<mpsc::UnboundedReceiver<LogEvent>>>>,
427    metrics_tx: crate::metrics::MetricsSender,
428    session_call_seq: Arc<std::sync::atomic::AtomicU32>,
429    session_id: Arc<TokioMutex<Option<String>>>,
430    // Store profile metadata from initialize request for use in on_initialized
431    profile_meta: Arc<TokioMutex<Option<serde_json::Map<String, serde_json::Value>>>>,
432}
433
434#[tool_router]
435impl CodeAnalyzer {
436    #[must_use]
437    pub fn list_tools() -> Vec<rmcp::model::Tool> {
438        Self::tool_router().list_all()
439    }
440
441    pub fn new(
442        peer: Arc<TokioMutex<Option<Peer<RoleServer>>>>,
443        log_level_filter: Arc<Mutex<LevelFilter>>,
444        event_rx: mpsc::UnboundedReceiver<LogEvent>,
445        metrics_tx: crate::metrics::MetricsSender,
446    ) -> Self {
447        let file_cap: usize = std::env::var("CODE_ANALYZE_FILE_CACHE_CAPACITY")
448            .ok()
449            .and_then(|v| v.parse().ok())
450            .unwrap_or(100);
451        let exec_cache_ttl_secs: u64 = std::env::var("APTU_CODER_EXEC_CACHE_TTL_SECS")
452            .ok()
453            .and_then(|v| v.parse().ok())
454            .unwrap_or(10);
455        let exec_cache_capacity: u64 = std::env::var("APTU_CODER_EXEC_CACHE_CAPACITY")
456            .ok()
457            .and_then(|v| v.parse().ok())
458            .unwrap_or(64);
459        let exec_cache = moka::future::Cache::builder()
460            .max_capacity(exec_cache_capacity)
461            .time_to_live(std::time::Duration::from_secs(exec_cache_ttl_secs))
462            .build();
463        CodeAnalyzer {
464            tool_router: Arc::new(RwLock::new(Self::tool_router())),
465            cache: AnalysisCache::new(file_cap),
466            exec_cache,
467            peer,
468            log_level_filter,
469            event_rx: Arc::new(TokioMutex::new(Some(event_rx))),
470            metrics_tx,
471            session_call_seq: Arc::new(std::sync::atomic::AtomicU32::new(0)),
472            session_id: Arc::new(TokioMutex::new(None)),
473            profile_meta: Arc::new(TokioMutex::new(None)),
474        }
475    }
476
477    #[instrument(skip(self))]
478    async fn emit_progress(
479        &self,
480        peer: Option<Peer<RoleServer>>,
481        token: &ProgressToken,
482        progress: f64,
483        total: f64,
484        message: String,
485    ) {
486        if let Some(peer) = peer {
487            let notification = ServerNotification::ProgressNotification(Notification::new(
488                ProgressNotificationParam {
489                    progress_token: token.clone(),
490                    progress,
491                    total: Some(total),
492                    message: Some(message),
493                },
494            ));
495            if let Err(e) = peer.send_notification(notification).await {
496                warn!("Failed to send progress notification: {}", e);
497            }
498        }
499    }
500
501    /// Private helper: Extract analysis logic for overview mode (`analyze_directory`).
502    /// Returns the complete analysis output and a cache_hit bool after spawning and monitoring progress.
503    /// Cancels the blocking task when `ct` is triggered; returns an error on cancellation.
504    #[allow(clippy::too_many_lines)] // long but cohesive analysis loop; extracting sub-functions would obscure the control flow
505    #[allow(clippy::cast_precision_loss)] // progress percentage display; precision loss acceptable for usize counts
506    #[instrument(skip(self, params, ct))]
507    async fn handle_overview_mode(
508        &self,
509        params: &AnalyzeDirectoryParams,
510        ct: tokio_util::sync::CancellationToken,
511    ) -> Result<(std::sync::Arc<analyze::AnalysisOutput>, bool), ErrorData> {
512        let path = Path::new(&params.path);
513        let counter = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0));
514        let counter_clone = counter.clone();
515        let path_owned = path.to_path_buf();
516        let max_depth = params.max_depth;
517        let ct_clone = ct.clone();
518
519        // Single unbounded walk; filter in-memory to respect max_depth for analysis.
520        let all_entries = walk_directory(path, None).map_err(|e| {
521            ErrorData::new(
522                rmcp::model::ErrorCode::INTERNAL_ERROR,
523                format!("Failed to walk directory: {e}"),
524                Some(error_meta(
525                    "resource",
526                    false,
527                    "check path permissions and availability",
528                )),
529            )
530        })?;
531
532        // Canonicalize max_depth: Some(0) is semantically identical to None (unlimited).
533        let canonical_max_depth = max_depth.and_then(|d| if d == 0 { None } else { Some(d) });
534
535        // Build cache key from all_entries (before depth filtering).
536        // git_ref is included in the key so filtered and unfiltered results have distinct entries.
537        let git_ref_val = params.git_ref.as_deref().filter(|s| !s.is_empty());
538        let cache_key = cache::DirectoryCacheKey::from_entries(
539            &all_entries,
540            canonical_max_depth,
541            AnalysisMode::Overview,
542            git_ref_val,
543        );
544
545        // Check cache
546        if let Some(cached) = self.cache.get_directory(&cache_key) {
547            return Ok((cached, true));
548        }
549
550        // Apply git_ref filter when requested (non-empty string only).
551        let all_entries = if let Some(ref git_ref) = params.git_ref
552            && !git_ref.is_empty()
553        {
554            let changed = changed_files_from_git_ref(path, git_ref).map_err(|e| {
555                ErrorData::new(
556                    rmcp::model::ErrorCode::INVALID_PARAMS,
557                    format!("git_ref filter failed: {e}"),
558                    Some(error_meta(
559                        "resource",
560                        false,
561                        "ensure git is installed and path is inside a git repository",
562                    )),
563                )
564            })?;
565            filter_entries_by_git_ref(all_entries, &changed, path)
566        } else {
567            all_entries
568        };
569
570        // Compute subtree counts from the full entry set before filtering.
571        let subtree_counts = if max_depth.is_some_and(|d| d > 0) {
572            Some(traversal::subtree_counts_from_entries(path, &all_entries))
573        } else {
574            None
575        };
576
577        // Filter to depth-bounded subset for analysis.
578        let entries: Vec<traversal::WalkEntry> = if let Some(depth) = max_depth
579            && depth > 0
580        {
581            all_entries
582                .into_iter()
583                .filter(|e| e.depth <= depth as usize)
584                .collect()
585        } else {
586            all_entries
587        };
588
589        // Get total file count for progress reporting
590        let total_files = entries.iter().filter(|e| !e.is_dir).count();
591
592        // Spawn blocking analysis with progress tracking
593        let handle = tokio::task::spawn_blocking(move || {
594            analyze::analyze_directory_with_progress(&path_owned, entries, counter_clone, ct_clone)
595        });
596
597        // Poll and emit progress every 100ms
598        let token = ProgressToken(NumberOrString::String(
599            format!(
600                "analyze-overview-{}",
601                std::time::SystemTime::now()
602                    .duration_since(std::time::UNIX_EPOCH)
603                    .map(|d| d.as_nanos())
604                    .unwrap_or(0)
605            )
606            .into(),
607        ));
608        let peer = self.peer.lock().await.clone();
609        let mut last_progress = 0usize;
610        let mut cancelled = false;
611        loop {
612            tokio::time::sleep(std::time::Duration::from_millis(100)).await;
613            if ct.is_cancelled() {
614                cancelled = true;
615                break;
616            }
617            let current = counter.load(std::sync::atomic::Ordering::Relaxed);
618            if current != last_progress && total_files > 0 {
619                self.emit_progress(
620                    peer.clone(),
621                    &token,
622                    current as f64,
623                    total_files as f64,
624                    format!("Analyzing {current}/{total_files} files"),
625                )
626                .await;
627                last_progress = current;
628            }
629            if handle.is_finished() {
630                break;
631            }
632        }
633
634        // Emit final 100% progress only if not cancelled
635        if !cancelled && total_files > 0 {
636            self.emit_progress(
637                peer.clone(),
638                &token,
639                total_files as f64,
640                total_files as f64,
641                format!("Completed analyzing {total_files} files"),
642            )
643            .await;
644        }
645
646        match handle.await {
647            Ok(Ok(mut output)) => {
648                output.subtree_counts = subtree_counts;
649                let arc_output = std::sync::Arc::new(output);
650                self.cache.put_directory(cache_key, arc_output.clone());
651                Ok((arc_output, false))
652            }
653            Ok(Err(analyze::AnalyzeError::Cancelled)) => Err(ErrorData::new(
654                rmcp::model::ErrorCode::INTERNAL_ERROR,
655                "Analysis cancelled".to_string(),
656                Some(error_meta("transient", true, "analysis was cancelled")),
657            )),
658            Ok(Err(e)) => Err(ErrorData::new(
659                rmcp::model::ErrorCode::INTERNAL_ERROR,
660                format!("Error analyzing directory: {e}"),
661                Some(error_meta(
662                    "resource",
663                    false,
664                    "check path and file permissions",
665                )),
666            )),
667            Err(e) => Err(ErrorData::new(
668                rmcp::model::ErrorCode::INTERNAL_ERROR,
669                format!("Task join error: {e}"),
670                Some(error_meta("transient", true, "retry the request")),
671            )),
672        }
673    }
674
675    /// Private helper: Extract analysis logic for file details mode (`analyze_file`).
676    /// Returns the cached or newly analyzed file output along with a cache_hit bool.
677    #[instrument(skip(self, params))]
678    async fn handle_file_details_mode(
679        &self,
680        params: &AnalyzeFileParams,
681    ) -> Result<(std::sync::Arc<analyze::FileAnalysisOutput>, bool), ErrorData> {
682        // Build cache key from file metadata
683        let cache_key = std::fs::metadata(&params.path).ok().and_then(|meta| {
684            meta.modified().ok().map(|mtime| cache::CacheKey {
685                path: std::path::PathBuf::from(&params.path),
686                modified: mtime,
687                mode: AnalysisMode::FileDetails,
688            })
689        });
690
691        // Check cache first
692        if let Some(ref key) = cache_key
693            && let Some(cached) = self.cache.get(key)
694        {
695            return Ok((cached, true));
696        }
697
698        // Cache miss or no cache key, analyze and optionally store
699        match analyze::analyze_file(&params.path, params.ast_recursion_limit) {
700            Ok(output) => {
701                let arc_output = std::sync::Arc::new(output);
702                if let Some(key) = cache_key {
703                    self.cache.put(key, arc_output.clone());
704                }
705                Ok((arc_output, false))
706            }
707            Err(e) => Err(ErrorData::new(
708                rmcp::model::ErrorCode::INTERNAL_ERROR,
709                format!("Error analyzing file: {e}"),
710                Some(error_meta(
711                    "resource",
712                    false,
713                    "check file path and permissions",
714                )),
715            )),
716        }
717    }
718
719    // Validate impl_only: only valid for directories that contain Rust source files.
720    fn validate_impl_only(entries: &[WalkEntry]) -> Result<(), ErrorData> {
721        let has_rust = entries.iter().any(|e| {
722            !e.is_dir
723                && e.path
724                    .extension()
725                    .and_then(|x: &std::ffi::OsStr| x.to_str())
726                    == Some("rs")
727        });
728
729        if !has_rust {
730            return Err(ErrorData::new(
731                rmcp::model::ErrorCode::INVALID_PARAMS,
732                "impl_only=true requires Rust source files. No .rs files found in the given path. Use analyze_symbol without impl_only for cross-language analysis.".to_string(),
733                Some(error_meta(
734                    "validation",
735                    false,
736                    "remove impl_only or point to a directory containing .rs files",
737                )),
738            ));
739        }
740        Ok(())
741    }
742
743    /// Validate that `import_lookup=true` is accompanied by a non-empty symbol (the module path).
744    fn validate_import_lookup(import_lookup: Option<bool>, symbol: &str) -> Result<(), ErrorData> {
745        if import_lookup == Some(true) && symbol.is_empty() {
746            return Err(ErrorData::new(
747                rmcp::model::ErrorCode::INVALID_PARAMS,
748                "import_lookup=true requires symbol to contain the module path to search for"
749                    .to_string(),
750                Some(error_meta(
751                    "validation",
752                    false,
753                    "set symbol to the module path when using import_lookup=true",
754                )),
755            ));
756        }
757        Ok(())
758    }
759
760    // Poll progress until analysis task completes.
761    #[allow(clippy::cast_precision_loss)] // progress percentage display; precision loss acceptable for usize counts
762    async fn poll_progress_until_done(
763        &self,
764        analysis_params: &FocusedAnalysisParams,
765        counter: std::sync::Arc<std::sync::atomic::AtomicUsize>,
766        ct: tokio_util::sync::CancellationToken,
767        entries: std::sync::Arc<Vec<WalkEntry>>,
768        total_files: usize,
769        symbol_display: &str,
770    ) -> Result<analyze::FocusedAnalysisOutput, ErrorData> {
771        let counter_clone = counter.clone();
772        let ct_clone = ct.clone();
773        let entries_clone = std::sync::Arc::clone(&entries);
774        let path_owned = analysis_params.path.clone();
775        let symbol_owned = analysis_params.symbol.clone();
776        let match_mode_owned = analysis_params.match_mode.clone();
777        let follow_depth = analysis_params.follow_depth;
778        let max_depth = analysis_params.max_depth;
779        let ast_recursion_limit = analysis_params.ast_recursion_limit;
780        let use_summary = analysis_params.use_summary;
781        let impl_only = analysis_params.impl_only;
782        let def_use = analysis_params.def_use;
783        let parse_timeout_micros = analysis_params.parse_timeout_micros;
784        let handle = tokio::task::spawn_blocking(move || {
785            let params = analyze::FocusedAnalysisConfig {
786                focus: symbol_owned,
787                match_mode: match_mode_owned,
788                follow_depth,
789                max_depth,
790                ast_recursion_limit,
791                use_summary,
792                impl_only,
793                def_use,
794                parse_timeout_micros,
795            };
796            analyze::analyze_focused_with_progress_with_entries(
797                &path_owned,
798                &params,
799                &counter_clone,
800                &ct_clone,
801                &entries_clone,
802            )
803        });
804
805        let token = ProgressToken(NumberOrString::String(
806            format!(
807                "analyze-symbol-{}",
808                std::time::SystemTime::now()
809                    .duration_since(std::time::UNIX_EPOCH)
810                    .map(|d| d.as_nanos())
811                    .unwrap_or(0)
812            )
813            .into(),
814        ));
815        let peer = self.peer.lock().await.clone();
816        let mut last_progress = 0usize;
817        let mut cancelled = false;
818
819        loop {
820            tokio::time::sleep(std::time::Duration::from_millis(100)).await;
821            if ct.is_cancelled() {
822                cancelled = true;
823                break;
824            }
825            let current = counter.load(std::sync::atomic::Ordering::Relaxed);
826            if current != last_progress && total_files > 0 {
827                self.emit_progress(
828                    peer.clone(),
829                    &token,
830                    current as f64,
831                    total_files as f64,
832                    format!(
833                        "Analyzing {current}/{total_files} files for symbol '{symbol_display}'"
834                    ),
835                )
836                .await;
837                last_progress = current;
838            }
839            if handle.is_finished() {
840                break;
841            }
842        }
843
844        if !cancelled && total_files > 0 {
845            self.emit_progress(
846                peer.clone(),
847                &token,
848                total_files as f64,
849                total_files as f64,
850                format!("Completed analyzing {total_files} files for symbol '{symbol_display}'"),
851            )
852            .await;
853        }
854
855        match handle.await {
856            Ok(Ok(output)) => Ok(output),
857            Ok(Err(analyze::AnalyzeError::Cancelled)) => Err(ErrorData::new(
858                rmcp::model::ErrorCode::INTERNAL_ERROR,
859                "Analysis cancelled".to_string(),
860                Some(error_meta("transient", true, "analysis was cancelled")),
861            )),
862            Ok(Err(e)) => Err(ErrorData::new(
863                rmcp::model::ErrorCode::INTERNAL_ERROR,
864                format!("Error analyzing symbol: {e}"),
865                Some(error_meta("resource", false, "check symbol name and file")),
866            )),
867            Err(e) => Err(ErrorData::new(
868                rmcp::model::ErrorCode::INTERNAL_ERROR,
869                format!("Task join error: {e}"),
870                Some(error_meta("transient", true, "retry the request")),
871            )),
872        }
873    }
874
875    // Run focused analysis with auto-summary retry on SIZE_LIMIT overflow.
876    async fn run_focused_with_auto_summary(
877        &self,
878        params: &AnalyzeSymbolParams,
879        analysis_params: &FocusedAnalysisParams,
880        counter: std::sync::Arc<std::sync::atomic::AtomicUsize>,
881        ct: tokio_util::sync::CancellationToken,
882        entries: std::sync::Arc<Vec<WalkEntry>>,
883        total_files: usize,
884    ) -> Result<analyze::FocusedAnalysisOutput, ErrorData> {
885        let use_summary_for_task = params.output_control.force != Some(true)
886            && params.output_control.summary == Some(true);
887
888        let analysis_params_initial = FocusedAnalysisParams {
889            use_summary: use_summary_for_task,
890            ..analysis_params.clone()
891        };
892
893        let mut output = self
894            .poll_progress_until_done(
895                &analysis_params_initial,
896                counter.clone(),
897                ct.clone(),
898                entries.clone(),
899                total_files,
900                &params.symbol,
901            )
902            .await?;
903
904        if params.output_control.summary.is_none()
905            && params.output_control.force != Some(true)
906            && output.formatted.len() > SIZE_LIMIT
907        {
908            let counter2 = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0));
909            let analysis_params_retry = FocusedAnalysisParams {
910                use_summary: true,
911                ..analysis_params.clone()
912            };
913            let summary_result = self
914                .poll_progress_until_done(
915                    &analysis_params_retry,
916                    counter2,
917                    ct,
918                    entries,
919                    total_files,
920                    &params.symbol,
921                )
922                .await;
923
924            if let Ok(summary_output) = summary_result {
925                output.formatted = summary_output.formatted;
926            } else {
927                let estimated_tokens = output.formatted.len() / 4;
928                let message = format!(
929                    "Output exceeds 50K chars ({} chars, ~{} tokens). Use summary=true or force=true.",
930                    output.formatted.len(),
931                    estimated_tokens
932                );
933                return Err(ErrorData::new(
934                    rmcp::model::ErrorCode::INVALID_PARAMS,
935                    message,
936                    Some(error_meta(
937                        "validation",
938                        false,
939                        "use summary=true or force=true",
940                    )),
941                ));
942            }
943        } else if output.formatted.len() > SIZE_LIMIT
944            && params.output_control.force != Some(true)
945            && params.output_control.summary == Some(false)
946        {
947            let estimated_tokens = output.formatted.len() / 4;
948            let message = format!(
949                "Output exceeds 50K chars ({} chars, ~{} tokens). Use one of:\n\
950                 - force=true to return full output\n\
951                 - summary=true to get compact summary\n\
952                 - Narrow your scope (smaller directory, specific file)",
953                output.formatted.len(),
954                estimated_tokens
955            );
956            return Err(ErrorData::new(
957                rmcp::model::ErrorCode::INVALID_PARAMS,
958                message,
959                Some(error_meta(
960                    "validation",
961                    false,
962                    "use force=true, summary=true, or narrow scope",
963                )),
964            ));
965        }
966
967        Ok(output)
968    }
969
970    /// Private helper: Extract analysis logic for focused mode (`analyze_symbol`).
971    /// Returns the complete focused analysis output after spawning and monitoring progress.
972    /// Cancels the blocking task when `ct` is triggered; returns an error on cancellation.
973    #[instrument(skip(self, params, ct))]
974    async fn handle_focused_mode(
975        &self,
976        params: &AnalyzeSymbolParams,
977        ct: tokio_util::sync::CancellationToken,
978    ) -> Result<analyze::FocusedAnalysisOutput, ErrorData> {
979        let path = Path::new(&params.path);
980        let raw_entries = match walk_directory(path, params.max_depth) {
981            Ok(e) => e,
982            Err(e) => {
983                return Err(ErrorData::new(
984                    rmcp::model::ErrorCode::INTERNAL_ERROR,
985                    format!("Failed to walk directory: {e}"),
986                    Some(error_meta(
987                        "resource",
988                        false,
989                        "check path permissions and availability",
990                    )),
991                ));
992            }
993        };
994        // Apply git_ref filter when requested (non-empty string only).
995        let filtered_entries = if let Some(ref git_ref) = params.git_ref
996            && !git_ref.is_empty()
997        {
998            let changed = changed_files_from_git_ref(path, git_ref).map_err(|e| {
999                ErrorData::new(
1000                    rmcp::model::ErrorCode::INVALID_PARAMS,
1001                    format!("git_ref filter failed: {e}"),
1002                    Some(error_meta(
1003                        "resource",
1004                        false,
1005                        "ensure git is installed and path is inside a git repository",
1006                    )),
1007                )
1008            })?;
1009            filter_entries_by_git_ref(raw_entries, &changed, path)
1010        } else {
1011            raw_entries
1012        };
1013        let entries = std::sync::Arc::new(filtered_entries);
1014
1015        if params.impl_only == Some(true) {
1016            Self::validate_impl_only(&entries)?;
1017        }
1018
1019        let total_files = entries.iter().filter(|e| !e.is_dir).count();
1020        let counter = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0));
1021
1022        let analysis_params = FocusedAnalysisParams {
1023            path: path.to_path_buf(),
1024            symbol: params.symbol.clone(),
1025            match_mode: params.match_mode.clone().unwrap_or_default(),
1026            follow_depth: params.follow_depth.unwrap_or(1),
1027            max_depth: params.max_depth,
1028            ast_recursion_limit: params.ast_recursion_limit,
1029            use_summary: false,
1030            impl_only: params.impl_only,
1031            def_use: params.def_use.unwrap_or(false),
1032            parse_timeout_micros: None,
1033        };
1034
1035        let mut output = self
1036            .run_focused_with_auto_summary(
1037                params,
1038                &analysis_params,
1039                counter,
1040                ct,
1041                entries,
1042                total_files,
1043            )
1044            .await?;
1045
1046        if params.impl_only == Some(true) {
1047            let filter_line = format!(
1048                "FILTER: impl_only=true ({} of {} callers shown)\n",
1049                output.impl_trait_caller_count, output.unfiltered_caller_count
1050            );
1051            output.formatted = format!("{}{}", filter_line, output.formatted);
1052
1053            if output.impl_trait_caller_count == 0 {
1054                output.formatted.push_str(
1055                    "\nNOTE: No impl-trait callers found. The symbol may be a plain function or struct, not a trait method. Remove impl_only to see all callers.\n"
1056                );
1057            }
1058        }
1059
1060        Ok(output)
1061    }
1062
1063    #[instrument(skip(self, context))]
1064    #[tool(
1065        name = "analyze_directory",
1066        title = "Analyze Directory",
1067        description = "Tree-view of directory with LOC, function/class counts, test markers. Respects .gitignore. Returns per-file stats plus next_cursor for pagination. Fails if summary=true and cursor. For 1000+ files, use max_depth=2-3 and summary=true. git_ref restricts to files changed since a branch/tag/commit. Empty directories return zero counts. Example queries: Analyze the src/ directory to understand module structure; What files are in the tests/ directory and how large are they?",
1068        output_schema = schema_for_type::<analyze::AnalysisOutput>(),
1069        annotations(
1070            title = "Analyze Directory",
1071            read_only_hint = true,
1072            destructive_hint = false,
1073            idempotent_hint = true,
1074            open_world_hint = false
1075        )
1076    )]
1077    async fn analyze_directory(
1078        &self,
1079        params: Parameters<AnalyzeDirectoryParams>,
1080        context: RequestContext<RoleServer>,
1081    ) -> Result<CallToolResult, ErrorData> {
1082        let params = params.0;
1083        let _validated_path = match validate_path(&params.path, true) {
1084            Ok(p) => p,
1085            Err(e) => return Ok(err_to_tool_result(e)),
1086        };
1087        let ct = context.ct.clone();
1088        let t_start = std::time::Instant::now();
1089        let param_path = params.path.clone();
1090        let max_depth_val = params.max_depth;
1091        let seq = self
1092            .session_call_seq
1093            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1094        let sid = self.session_id.lock().await.clone();
1095
1096        // Call handler for analysis and progress tracking
1097        let (arc_output, dir_cache_hit) = match self.handle_overview_mode(&params, ct).await {
1098            Ok(v) => v,
1099            Err(e) => return Ok(err_to_tool_result(e)),
1100        };
1101        // Extract the value from Arc for modification. On a cache hit the Arc is shared,
1102        // so try_unwrap may fail; fall back to cloning the underlying value in that case.
1103        let mut output = match std::sync::Arc::try_unwrap(arc_output) {
1104            Ok(owned) => owned,
1105            Err(arc) => (*arc).clone(),
1106        };
1107
1108        // summary=true (explicit) and cursor are mutually exclusive.
1109        // Auto-summarization (summary=None + large output) must NOT block cursor pagination.
1110        if summary_cursor_conflict(
1111            params.output_control.summary,
1112            params.pagination.cursor.as_deref(),
1113        ) {
1114            return Ok(err_to_tool_result(ErrorData::new(
1115                rmcp::model::ErrorCode::INVALID_PARAMS,
1116                "summary=true is incompatible with a pagination cursor; use one or the other"
1117                    .to_string(),
1118                Some(error_meta(
1119                    "validation",
1120                    false,
1121                    "remove cursor or set summary=false",
1122                )),
1123            )));
1124        }
1125
1126        // Apply summary/output size limiting logic
1127        let use_summary = if params.output_control.force == Some(true) {
1128            false
1129        } else if params.output_control.summary == Some(true) {
1130            true
1131        } else if params.output_control.summary == Some(false) {
1132            false
1133        } else {
1134            output.formatted.len() > SIZE_LIMIT
1135        };
1136
1137        if use_summary {
1138            output.formatted = format_summary(
1139                &output.entries,
1140                &output.files,
1141                params.max_depth,
1142                output.subtree_counts.as_deref(),
1143            );
1144        }
1145
1146        // Decode pagination cursor if provided
1147        let page_size = params.pagination.page_size.unwrap_or(DEFAULT_PAGE_SIZE);
1148        let offset = if let Some(ref cursor_str) = params.pagination.cursor {
1149            let cursor_data = match decode_cursor(cursor_str).map_err(|e| {
1150                ErrorData::new(
1151                    rmcp::model::ErrorCode::INVALID_PARAMS,
1152                    e.to_string(),
1153                    Some(error_meta("validation", false, "invalid cursor format")),
1154                )
1155            }) {
1156                Ok(v) => v,
1157                Err(e) => return Ok(err_to_tool_result(e)),
1158            };
1159            cursor_data.offset
1160        } else {
1161            0
1162        };
1163
1164        // Apply pagination to files
1165        let paginated =
1166            match paginate_slice(&output.files, offset, page_size, PaginationMode::Default) {
1167                Ok(v) => v,
1168                Err(e) => {
1169                    return Ok(err_to_tool_result(ErrorData::new(
1170                        rmcp::model::ErrorCode::INTERNAL_ERROR,
1171                        e.to_string(),
1172                        Some(error_meta("transient", true, "retry the request")),
1173                    )));
1174                }
1175            };
1176
1177        let verbose = params.output_control.verbose.unwrap_or(false);
1178        if !use_summary {
1179            output.formatted = format_structure_paginated(
1180                &paginated.items,
1181                paginated.total,
1182                params.max_depth,
1183                Some(Path::new(&params.path)),
1184                verbose,
1185            );
1186        }
1187
1188        // Update next_cursor in output after pagination (unless using summary mode)
1189        if use_summary {
1190            output.next_cursor = None;
1191        } else {
1192            output.next_cursor.clone_from(&paginated.next_cursor);
1193        }
1194
1195        // Build final text output with pagination cursor if present (unless using summary mode)
1196        let mut final_text = output.formatted.clone();
1197        if !use_summary && let Some(cursor) = paginated.next_cursor {
1198            final_text.push('\n');
1199            final_text.push_str("NEXT_CURSOR: ");
1200            final_text.push_str(&cursor);
1201        }
1202
1203        let mut result = CallToolResult::success(vec![Content::text(final_text.clone())])
1204            .with_meta(Some(no_cache_meta()));
1205        let structured = serde_json::to_value(&output).unwrap_or(Value::Null);
1206        result.structured_content = Some(structured);
1207        let dur = t_start.elapsed().as_millis().try_into().unwrap_or(u64::MAX);
1208        self.metrics_tx.send(crate::metrics::MetricEvent {
1209            ts: crate::metrics::unix_ms(),
1210            tool: "analyze_directory",
1211            duration_ms: dur,
1212            output_chars: final_text.len(),
1213            param_path_depth: crate::metrics::path_component_count(&param_path),
1214            max_depth: max_depth_val,
1215            result: "ok",
1216            error_type: None,
1217            session_id: sid,
1218            seq: Some(seq),
1219            cache_hit: Some(dir_cache_hit),
1220        });
1221        Ok(result)
1222    }
1223
1224    #[instrument(skip(self, _context))]
1225    #[tool(
1226        name = "analyze_file",
1227        title = "Analyze File",
1228        description = "Functions, types, classes, and imports from a single source file. Returns functions (name, signature, line range), classes (methods, fields, inheritance), imports; paginate with cursor/page_size. Use fields=[\"functions\",\"classes\",\"imports\"] to limit output sections. Fails if directory path supplied; use analyze_directory instead. Fails if summary=true and cursor. git_ref not supported for single-file analysis. Use analyze_module for lightweight function/import index (~75% smaller). Supported: Rust, Go, Java, Python, TypeScript, TSX, Fortran, JavaScript, C/C++, C#. Example queries: What functions are defined in src/lib.rs?; Show me the classes and their methods in src/analyzer.py.",
1229        output_schema = schema_for_type::<analyze::FileAnalysisOutput>(),
1230        annotations(
1231            title = "Analyze File",
1232            read_only_hint = true,
1233            destructive_hint = false,
1234            idempotent_hint = true,
1235            open_world_hint = false
1236        )
1237    )]
1238    async fn analyze_file(
1239        &self,
1240        params: Parameters<AnalyzeFileParams>,
1241        _context: RequestContext<RoleServer>,
1242    ) -> Result<CallToolResult, ErrorData> {
1243        let params = params.0;
1244        let _validated_path = match validate_path(&params.path, true) {
1245            Ok(p) => p,
1246            Err(e) => return Ok(err_to_tool_result(e)),
1247        };
1248        let t_start = std::time::Instant::now();
1249        let param_path = params.path.clone();
1250        let seq = self
1251            .session_call_seq
1252            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1253        let sid = self.session_id.lock().await.clone();
1254
1255        // Check if path is a directory (not allowed for analyze_file)
1256        if std::path::Path::new(&params.path).is_dir() {
1257            return Ok(err_to_tool_result(ErrorData::new(
1258                rmcp::model::ErrorCode::INVALID_PARAMS,
1259                format!(
1260                    "'{}' is a directory; use analyze_directory instead",
1261                    params.path
1262                ),
1263                Some(error_meta(
1264                    "validation",
1265                    false,
1266                    "pass a file path, not a directory",
1267                )),
1268            )));
1269        }
1270
1271        // summary=true and cursor are mutually exclusive
1272        if summary_cursor_conflict(
1273            params.output_control.summary,
1274            params.pagination.cursor.as_deref(),
1275        ) {
1276            return Ok(err_to_tool_result(ErrorData::new(
1277                rmcp::model::ErrorCode::INVALID_PARAMS,
1278                "summary=true is incompatible with a pagination cursor; use one or the other"
1279                    .to_string(),
1280                Some(error_meta(
1281                    "validation",
1282                    false,
1283                    "remove cursor or set summary=false",
1284                )),
1285            )));
1286        }
1287
1288        // Call handler for analysis and caching
1289        let (arc_output, file_cache_hit) = match self.handle_file_details_mode(&params).await {
1290            Ok(v) => v,
1291            Err(e) => return Ok(err_to_tool_result(e)),
1292        };
1293
1294        // Clone only the two fields that may be mutated per-request (formatted and
1295        // next_cursor). The heavy SemanticAnalysis data is shared via Arc and never
1296        // modified, so we borrow it directly from the cached pointer.
1297        let mut formatted = arc_output.formatted.clone();
1298        let line_count = arc_output.line_count;
1299
1300        // Apply summary/output size limiting logic
1301        let use_summary = if params.output_control.force == Some(true) {
1302            false
1303        } else if params.output_control.summary == Some(true) {
1304            true
1305        } else if params.output_control.summary == Some(false) {
1306            false
1307        } else {
1308            formatted.len() > SIZE_LIMIT
1309        };
1310
1311        if use_summary {
1312            formatted = format_file_details_summary(&arc_output.semantic, &params.path, line_count);
1313        } else if formatted.len() > SIZE_LIMIT && params.output_control.force != Some(true) {
1314            let estimated_tokens = formatted.len() / 4;
1315            let message = format!(
1316                "Output exceeds 50K chars ({} chars, ~{} tokens). Use one of:\n\
1317                 - force=true to return full output\n\
1318                 - Use fields to limit output to specific sections (functions, classes, or imports)\n\
1319                 - Use summary=true for a compact overview",
1320                formatted.len(),
1321                estimated_tokens
1322            );
1323            return Ok(err_to_tool_result(ErrorData::new(
1324                rmcp::model::ErrorCode::INVALID_PARAMS,
1325                message,
1326                Some(error_meta(
1327                    "validation",
1328                    false,
1329                    "use force=true, fields, or summary=true",
1330                )),
1331            )));
1332        }
1333
1334        // Decode pagination cursor if provided (analyze_file)
1335        let page_size = params.pagination.page_size.unwrap_or(DEFAULT_PAGE_SIZE);
1336        let offset = if let Some(ref cursor_str) = params.pagination.cursor {
1337            let cursor_data = match decode_cursor(cursor_str).map_err(|e| {
1338                ErrorData::new(
1339                    rmcp::model::ErrorCode::INVALID_PARAMS,
1340                    e.to_string(),
1341                    Some(error_meta("validation", false, "invalid cursor format")),
1342                )
1343            }) {
1344                Ok(v) => v,
1345                Err(e) => return Ok(err_to_tool_result(e)),
1346            };
1347            cursor_data.offset
1348        } else {
1349            0
1350        };
1351
1352        // Filter to top-level functions only (exclude methods) before pagination
1353        let top_level_fns: Vec<crate::types::FunctionInfo> = arc_output
1354            .semantic
1355            .functions
1356            .iter()
1357            .filter(|func| {
1358                !arc_output
1359                    .semantic
1360                    .classes
1361                    .iter()
1362                    .any(|class| func.line >= class.line && func.end_line <= class.end_line)
1363            })
1364            .cloned()
1365            .collect();
1366
1367        // Paginate top-level functions only
1368        let paginated =
1369            match paginate_slice(&top_level_fns, offset, page_size, PaginationMode::Default) {
1370                Ok(v) => v,
1371                Err(e) => {
1372                    return Ok(err_to_tool_result(ErrorData::new(
1373                        rmcp::model::ErrorCode::INTERNAL_ERROR,
1374                        e.to_string(),
1375                        Some(error_meta("transient", true, "retry the request")),
1376                    )));
1377                }
1378            };
1379
1380        // Regenerate formatted output using the paginated formatter (handles verbose and pagination correctly)
1381        let verbose = params.output_control.verbose.unwrap_or(false);
1382        if !use_summary {
1383            // fields: serde rejects unknown enum variants at deserialization; no runtime validation required
1384            formatted = format_file_details_paginated(
1385                &paginated.items,
1386                paginated.total,
1387                &arc_output.semantic,
1388                &params.path,
1389                line_count,
1390                offset,
1391                verbose,
1392                params.fields.as_deref(),
1393            );
1394        }
1395
1396        // Capture next_cursor from pagination result (unless using summary mode)
1397        let next_cursor = if use_summary {
1398            None
1399        } else {
1400            paginated.next_cursor.clone()
1401        };
1402
1403        // Build final text output with pagination cursor if present (unless using summary mode)
1404        let mut final_text = formatted.clone();
1405        if !use_summary && let Some(ref cursor) = next_cursor {
1406            final_text.push('\n');
1407            final_text.push_str("NEXT_CURSOR: ");
1408            final_text.push_str(cursor);
1409        }
1410
1411        // Build the response output, sharing SemanticAnalysis from the Arc to avoid cloning it.
1412        let response_output = analyze::FileAnalysisOutput::new(
1413            formatted,
1414            arc_output.semantic.clone(),
1415            line_count,
1416            next_cursor,
1417        );
1418
1419        let mut result = CallToolResult::success(vec![Content::text(final_text.clone())])
1420            .with_meta(Some(no_cache_meta()));
1421        let structured = serde_json::to_value(&response_output).unwrap_or(Value::Null);
1422        result.structured_content = Some(structured);
1423        let dur = t_start.elapsed().as_millis().try_into().unwrap_or(u64::MAX);
1424        self.metrics_tx.send(crate::metrics::MetricEvent {
1425            ts: crate::metrics::unix_ms(),
1426            tool: "analyze_file",
1427            duration_ms: dur,
1428            output_chars: final_text.len(),
1429            param_path_depth: crate::metrics::path_component_count(&param_path),
1430            max_depth: None,
1431            result: "ok",
1432            error_type: None,
1433            session_id: sid,
1434            seq: Some(seq),
1435            cache_hit: Some(file_cache_hit),
1436        });
1437        Ok(result)
1438    }
1439
1440    #[instrument(skip(self, context))]
1441    #[tool(
1442        name = "analyze_symbol",
1443        title = "Analyze Symbol",
1444        description = "Call graph for a named symbol across all files in a directory. Returns callers and callees. Modes: call graph (default), import_lookup (files importing a module path), def_use (write/read sites). Fails if file path supplied; fails if impl_only=true on non-Rust directory; fails if import_lookup=true with empty symbol; fails if summary=true and cursor. match_mode controls name matching (exact/insensitive/prefix/contains). git_ref restricts to changed files. Example queries: Find all callers of parse_config; Find all files that import std::collections.",
1445        output_schema = schema_for_type::<analyze::FocusedAnalysisOutput>(),
1446        annotations(
1447            title = "Analyze Symbol",
1448            read_only_hint = true,
1449            destructive_hint = false,
1450            idempotent_hint = true,
1451            open_world_hint = false
1452        )
1453    )]
1454    async fn analyze_symbol(
1455        &self,
1456        params: Parameters<AnalyzeSymbolParams>,
1457        context: RequestContext<RoleServer>,
1458    ) -> Result<CallToolResult, ErrorData> {
1459        let params = params.0;
1460        let _validated_path = match validate_path(&params.path, true) {
1461            Ok(p) => p,
1462            Err(e) => return Ok(err_to_tool_result(e)),
1463        };
1464        let ct = context.ct.clone();
1465        let t_start = std::time::Instant::now();
1466        let param_path = params.path.clone();
1467        let max_depth_val = params.follow_depth;
1468        let seq = self
1469            .session_call_seq
1470            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1471        let sid = self.session_id.lock().await.clone();
1472
1473        // Check if path is a file (not allowed for analyze_symbol)
1474        if std::path::Path::new(&params.path).is_file() {
1475            return Ok(err_to_tool_result(ErrorData::new(
1476                rmcp::model::ErrorCode::INVALID_PARAMS,
1477                format!(
1478                    "'{}' is a file; analyze_symbol requires a directory path",
1479                    params.path
1480                ),
1481                Some(error_meta(
1482                    "validation",
1483                    false,
1484                    "pass a directory path, not a file",
1485                )),
1486            )));
1487        }
1488
1489        // summary=true and cursor are mutually exclusive
1490        if summary_cursor_conflict(
1491            params.output_control.summary,
1492            params.pagination.cursor.as_deref(),
1493        ) {
1494            return Ok(err_to_tool_result(ErrorData::new(
1495                rmcp::model::ErrorCode::INVALID_PARAMS,
1496                "summary=true is incompatible with a pagination cursor; use one or the other"
1497                    .to_string(),
1498                Some(error_meta(
1499                    "validation",
1500                    false,
1501                    "remove cursor or set summary=false",
1502                )),
1503            )));
1504        }
1505
1506        // import_lookup=true is mutually exclusive with a non-empty symbol.
1507        if let Err(e) = Self::validate_import_lookup(params.import_lookup, &params.symbol) {
1508            return Ok(err_to_tool_result(e));
1509        }
1510
1511        // import_lookup mode: scan for files importing `params.symbol` as a module path.
1512        if params.import_lookup == Some(true) {
1513            let path_owned = PathBuf::from(&params.path);
1514            let symbol = params.symbol.clone();
1515            let git_ref = params.git_ref.clone();
1516            let max_depth = params.max_depth;
1517            let ast_recursion_limit = params.ast_recursion_limit;
1518
1519            let handle = tokio::task::spawn_blocking(move || {
1520                let path = path_owned.as_path();
1521                let raw_entries = match walk_directory(path, max_depth) {
1522                    Ok(e) => e,
1523                    Err(e) => {
1524                        return Err(ErrorData::new(
1525                            rmcp::model::ErrorCode::INTERNAL_ERROR,
1526                            format!("Failed to walk directory: {e}"),
1527                            Some(error_meta(
1528                                "resource",
1529                                false,
1530                                "check path permissions and availability",
1531                            )),
1532                        ));
1533                    }
1534                };
1535                // Apply git_ref filter when requested (non-empty string only).
1536                let entries = if let Some(ref git_ref_val) = git_ref
1537                    && !git_ref_val.is_empty()
1538                {
1539                    let changed = match changed_files_from_git_ref(path, git_ref_val) {
1540                        Ok(c) => c,
1541                        Err(e) => {
1542                            return Err(ErrorData::new(
1543                                rmcp::model::ErrorCode::INVALID_PARAMS,
1544                                format!("git_ref filter failed: {e}"),
1545                                Some(error_meta(
1546                                    "resource",
1547                                    false,
1548                                    "ensure git is installed and path is inside a git repository",
1549                                )),
1550                            ));
1551                        }
1552                    };
1553                    filter_entries_by_git_ref(raw_entries, &changed, path)
1554                } else {
1555                    raw_entries
1556                };
1557                let output = match analyze::analyze_import_lookup(
1558                    path,
1559                    &symbol,
1560                    &entries,
1561                    ast_recursion_limit,
1562                ) {
1563                    Ok(v) => v,
1564                    Err(e) => {
1565                        return Err(ErrorData::new(
1566                            rmcp::model::ErrorCode::INTERNAL_ERROR,
1567                            format!("import_lookup failed: {e}"),
1568                            Some(error_meta(
1569                                "resource",
1570                                false,
1571                                "check path and file permissions",
1572                            )),
1573                        ));
1574                    }
1575                };
1576                Ok(output)
1577            });
1578
1579            let output = match handle.await {
1580                Ok(Ok(v)) => v,
1581                Ok(Err(e)) => return Ok(err_to_tool_result(e)),
1582                Err(e) => {
1583                    return Ok(err_to_tool_result(ErrorData::new(
1584                        rmcp::model::ErrorCode::INTERNAL_ERROR,
1585                        format!("spawn_blocking failed: {e}"),
1586                        Some(error_meta("resource", false, "internal error")),
1587                    )));
1588                }
1589            };
1590
1591            let final_text = output.formatted.clone();
1592            let mut result = CallToolResult::success(vec![Content::text(final_text.clone())])
1593                .with_meta(Some(no_cache_meta()));
1594            let structured = serde_json::to_value(&output).unwrap_or(Value::Null);
1595            result.structured_content = Some(structured);
1596            let dur = t_start.elapsed().as_millis().try_into().unwrap_or(u64::MAX);
1597            self.metrics_tx.send(crate::metrics::MetricEvent {
1598                ts: crate::metrics::unix_ms(),
1599                tool: "analyze_symbol",
1600                duration_ms: dur,
1601                output_chars: final_text.len(),
1602                param_path_depth: crate::metrics::path_component_count(&param_path),
1603                max_depth: max_depth_val,
1604                result: "ok",
1605                error_type: None,
1606                session_id: sid,
1607                seq: Some(seq),
1608                cache_hit: Some(false),
1609            });
1610            return Ok(result);
1611        }
1612
1613        // Call handler for analysis and progress tracking
1614        let mut output = match self.handle_focused_mode(&params, ct).await {
1615            Ok(v) => v,
1616            Err(e) => return Ok(err_to_tool_result(e)),
1617        };
1618
1619        // Decode pagination cursor if provided (analyze_symbol)
1620        let page_size = params.pagination.page_size.unwrap_or(DEFAULT_PAGE_SIZE);
1621        let offset = if let Some(ref cursor_str) = params.pagination.cursor {
1622            let cursor_data = match decode_cursor(cursor_str).map_err(|e| {
1623                ErrorData::new(
1624                    rmcp::model::ErrorCode::INVALID_PARAMS,
1625                    e.to_string(),
1626                    Some(error_meta("validation", false, "invalid cursor format")),
1627                )
1628            }) {
1629                Ok(v) => v,
1630                Err(e) => return Ok(err_to_tool_result(e)),
1631            };
1632            cursor_data.offset
1633        } else {
1634            0
1635        };
1636
1637        // SymbolFocus pagination: decode cursor mode to determine callers vs callees
1638        let cursor_mode = if let Some(ref cursor_str) = params.pagination.cursor {
1639            decode_cursor(cursor_str)
1640                .map(|c| c.mode)
1641                .unwrap_or(PaginationMode::Callers)
1642        } else {
1643            PaginationMode::Callers
1644        };
1645
1646        let mut use_summary = params.output_control.summary == Some(true);
1647        if params.output_control.force == Some(true) {
1648            use_summary = false;
1649        }
1650        let verbose = params.output_control.verbose.unwrap_or(false);
1651
1652        let mut callee_cursor = match cursor_mode {
1653            PaginationMode::Callers => {
1654                let (paginated_items, paginated_next) = match paginate_focus_chains(
1655                    &output.prod_chains,
1656                    PaginationMode::Callers,
1657                    offset,
1658                    page_size,
1659                ) {
1660                    Ok(v) => v,
1661                    Err(e) => return Ok(err_to_tool_result(e)),
1662                };
1663
1664                if !use_summary
1665                    && (paginated_next.is_some()
1666                        || offset > 0
1667                        || !verbose
1668                        || !output.outgoing_chains.is_empty())
1669                {
1670                    let base_path = Path::new(&params.path);
1671                    output.formatted = format_focused_paginated(
1672                        &paginated_items,
1673                        output.prod_chains.len(),
1674                        PaginationMode::Callers,
1675                        &params.symbol,
1676                        &output.prod_chains,
1677                        &output.test_chains,
1678                        &output.outgoing_chains,
1679                        output.def_count,
1680                        offset,
1681                        Some(base_path),
1682                        verbose,
1683                    );
1684                    paginated_next
1685                } else {
1686                    None
1687                }
1688            }
1689            PaginationMode::Callees => {
1690                let (paginated_items, paginated_next) = match paginate_focus_chains(
1691                    &output.outgoing_chains,
1692                    PaginationMode::Callees,
1693                    offset,
1694                    page_size,
1695                ) {
1696                    Ok(v) => v,
1697                    Err(e) => return Ok(err_to_tool_result(e)),
1698                };
1699
1700                if paginated_next.is_some() || offset > 0 || !verbose {
1701                    let base_path = Path::new(&params.path);
1702                    output.formatted = format_focused_paginated(
1703                        &paginated_items,
1704                        output.outgoing_chains.len(),
1705                        PaginationMode::Callees,
1706                        &params.symbol,
1707                        &output.prod_chains,
1708                        &output.test_chains,
1709                        &output.outgoing_chains,
1710                        output.def_count,
1711                        offset,
1712                        Some(base_path),
1713                        verbose,
1714                    );
1715                    paginated_next
1716                } else {
1717                    None
1718                }
1719            }
1720            PaginationMode::Default => {
1721                return Ok(err_to_tool_result(ErrorData::new(
1722                    rmcp::model::ErrorCode::INVALID_PARAMS,
1723                    "invalid cursor: unknown pagination mode".to_string(),
1724                    Some(error_meta(
1725                        "validation",
1726                        false,
1727                        "use a cursor returned by a previous analyze_symbol call",
1728                    )),
1729                )));
1730            }
1731            PaginationMode::DefUse => {
1732                let total_sites = output.def_use_sites.len();
1733                let (paginated_sites, paginated_next) = match paginate_slice(
1734                    &output.def_use_sites,
1735                    offset,
1736                    page_size,
1737                    PaginationMode::DefUse,
1738                ) {
1739                    Ok(r) => (r.items, r.next_cursor),
1740                    Err(e) => return Ok(err_to_tool_result_from_pagination(e)),
1741                };
1742
1743                // Always regenerate formatted output for DefUse mode so the
1744                // first page (offset=0, verbose=true) is not skipped.
1745                if !use_summary {
1746                    let base_path = Path::new(&params.path);
1747                    output.formatted = format_focused_paginated_defuse(
1748                        &paginated_sites,
1749                        total_sites,
1750                        &params.symbol,
1751                        offset,
1752                        Some(base_path),
1753                        verbose,
1754                    );
1755                }
1756
1757                // Slice output.def_use_sites to the current page window so
1758                // structuredContent only contains the paginated subset.
1759                output.def_use_sites = paginated_sites;
1760
1761                paginated_next
1762            }
1763        };
1764
1765        // When callers are exhausted and callees exist, bootstrap callee pagination
1766        // by emitting a {mode:callees, offset:0} cursor. This makes PaginationMode::Callees
1767        // reachable; without it the branch was dead code. Suppressed in summary mode
1768        // because summary and pagination are mutually exclusive.
1769        if callee_cursor.is_none()
1770            && cursor_mode == PaginationMode::Callers
1771            && !output.outgoing_chains.is_empty()
1772            && !use_summary
1773            && let Ok(cursor) = encode_cursor(&CursorData {
1774                mode: PaginationMode::Callees,
1775                offset: 0,
1776            })
1777        {
1778            callee_cursor = Some(cursor);
1779        }
1780
1781        // When callees are exhausted and def_use_sites exist, bootstrap defuse cursor
1782        // by emitting a {mode:defuse, offset:0} cursor. This makes PaginationMode::DefUse
1783        // reachable. Suppressed in summary mode because summary and pagination are mutually exclusive.
1784        // Also bootstrap directly from Callers mode when there are no outgoing chains
1785        // (e.g. SymbolNotFound path or symbols with no callees) so def-use pagination
1786        // is reachable even without a Callees phase.
1787        if callee_cursor.is_none()
1788            && matches!(
1789                cursor_mode,
1790                PaginationMode::Callees | PaginationMode::Callers
1791            )
1792            && !output.def_use_sites.is_empty()
1793            && !use_summary
1794            && let Ok(cursor) = encode_cursor(&CursorData {
1795                mode: PaginationMode::DefUse,
1796                offset: 0,
1797            })
1798        {
1799            // Only bootstrap from Callers when callees are empty (otherwise
1800            // the Callees bootstrap above takes priority).
1801            if cursor_mode == PaginationMode::Callees || output.outgoing_chains.is_empty() {
1802                callee_cursor = Some(cursor);
1803            }
1804        }
1805
1806        // Update next_cursor in output
1807        output.next_cursor.clone_from(&callee_cursor);
1808
1809        // Build final text output with pagination cursor if present
1810        let mut final_text = output.formatted.clone();
1811        if let Some(cursor) = callee_cursor {
1812            final_text.push('\n');
1813            final_text.push_str("NEXT_CURSOR: ");
1814            final_text.push_str(&cursor);
1815        }
1816
1817        let mut result = CallToolResult::success(vec![Content::text(final_text.clone())])
1818            .with_meta(Some(no_cache_meta()));
1819        // Only include def_use_sites in structuredContent when in DefUse mode.
1820        // In Callers/Callees modes, clearing the vec prevents large def-use
1821        // payloads from leaking into paginated non-def-use responses.
1822        if cursor_mode != PaginationMode::DefUse {
1823            output.def_use_sites = Vec::new();
1824        }
1825        let structured = serde_json::to_value(&output).unwrap_or(Value::Null);
1826        result.structured_content = Some(structured);
1827        let dur = t_start.elapsed().as_millis().try_into().unwrap_or(u64::MAX);
1828        self.metrics_tx.send(crate::metrics::MetricEvent {
1829            ts: crate::metrics::unix_ms(),
1830            tool: "analyze_symbol",
1831            duration_ms: dur,
1832            output_chars: final_text.len(),
1833            param_path_depth: crate::metrics::path_component_count(&param_path),
1834            max_depth: max_depth_val,
1835            result: "ok",
1836            error_type: None,
1837            session_id: sid,
1838            seq: Some(seq),
1839            cache_hit: Some(false),
1840        });
1841        Ok(result)
1842    }
1843
1844    #[instrument(skip(self, _context))]
1845    #[tool(
1846        name = "analyze_module",
1847        title = "Analyze Module",
1848        description = "Function and import index for a single source file with minimal token cost: name, line_count, language, function names with line numbers, import list only (~75% smaller than analyze_file). Fails if directory path supplied. Pagination, summary, force, verbose, git_ref not supported. Use analyze_file when you need signatures, types, or class details. Supported: Rust, Go, Java, Python, TypeScript, TSX, Fortran, JavaScript, C/C++, C#. Example queries: What functions are defined in src/analyze.rs?",
1849        output_schema = schema_for_type::<types::ModuleInfo>(),
1850        annotations(
1851            title = "Analyze Module",
1852            read_only_hint = true,
1853            destructive_hint = false,
1854            idempotent_hint = true,
1855            open_world_hint = false
1856        )
1857    )]
1858    async fn analyze_module(
1859        &self,
1860        params: Parameters<AnalyzeModuleParams>,
1861        _context: RequestContext<RoleServer>,
1862    ) -> Result<CallToolResult, ErrorData> {
1863        let params = params.0;
1864        let _validated_path = match validate_path(&params.path, true) {
1865            Ok(p) => p,
1866            Err(e) => return Ok(err_to_tool_result(e)),
1867        };
1868        let t_start = std::time::Instant::now();
1869        let param_path = params.path.clone();
1870        let seq = self
1871            .session_call_seq
1872            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1873        let sid = self.session_id.lock().await.clone();
1874
1875        // Issue 340: Guard against directory paths
1876        if std::fs::metadata(&params.path)
1877            .map(|m| m.is_dir())
1878            .unwrap_or(false)
1879        {
1880            let dur = t_start.elapsed().as_millis().try_into().unwrap_or(u64::MAX);
1881            self.metrics_tx.send(crate::metrics::MetricEvent {
1882                ts: crate::metrics::unix_ms(),
1883                tool: "analyze_module",
1884                duration_ms: dur,
1885                output_chars: 0,
1886                param_path_depth: crate::metrics::path_component_count(&param_path),
1887                max_depth: None,
1888                result: "error",
1889                error_type: Some("invalid_params".to_string()),
1890                session_id: sid.clone(),
1891                seq: Some(seq),
1892                cache_hit: None,
1893            });
1894            return Ok(err_to_tool_result(ErrorData::new(
1895                rmcp::model::ErrorCode::INVALID_PARAMS,
1896                format!(
1897                    "'{}' is a directory. Use analyze_directory to analyze a directory, or pass a specific file path to analyze_module.",
1898                    params.path
1899                ),
1900                Some(error_meta(
1901                    "validation",
1902                    false,
1903                    "use analyze_directory for directories",
1904                )),
1905            )));
1906        }
1907
1908        // Check file cache using mtime-keyed CacheKey (same pattern as handle_file_details_mode).
1909        let module_cache_key = std::fs::metadata(&params.path).ok().and_then(|meta| {
1910            meta.modified().ok().map(|mtime| cache::CacheKey {
1911                path: std::path::PathBuf::from(&params.path),
1912                modified: mtime,
1913                mode: AnalysisMode::FileDetails,
1914            })
1915        });
1916        let (module_info, module_cache_hit) = if let Some(ref key) = module_cache_key
1917            && let Some(cached_file) = self.cache.get(key)
1918        {
1919            // Reconstruct ModuleInfo from the cached FileAnalysisOutput.
1920            // Path and language are derived from params.path since FileAnalysisOutput
1921            // does not store them.
1922            let file_path = std::path::Path::new(&params.path);
1923            let name = file_path
1924                .file_name()
1925                .and_then(|n: &std::ffi::OsStr| n.to_str())
1926                .unwrap_or("unknown")
1927                .to_string();
1928            let language = file_path
1929                .extension()
1930                .and_then(|e| e.to_str())
1931                .and_then(aptu_coder_core::lang::language_for_extension)
1932                .unwrap_or("unknown")
1933                .to_string();
1934            let mut mi = types::ModuleInfo::default();
1935            mi.name = name;
1936            mi.line_count = cached_file.line_count;
1937            mi.language = language;
1938            mi.functions = cached_file
1939                .semantic
1940                .functions
1941                .iter()
1942                .map(|f| {
1943                    let mut mfi = types::ModuleFunctionInfo::default();
1944                    mfi.name = f.name.clone();
1945                    mfi.line = f.line;
1946                    mfi
1947                })
1948                .collect();
1949            mi.imports = cached_file
1950                .semantic
1951                .imports
1952                .iter()
1953                .map(|i| {
1954                    let mut mii = types::ModuleImportInfo::default();
1955                    mii.module = i.module.clone();
1956                    mii.items = i.items.clone();
1957                    mii
1958                })
1959                .collect();
1960            (mi, true)
1961        } else {
1962            // Cache miss: call analyze_file (returns FileAnalysisOutput) so we can populate
1963            // the file cache for future calls. Then reconstruct ModuleInfo from the result,
1964            // mirroring the cache-hit path above.
1965            let file_output = match analyze::analyze_file(&params.path, None) {
1966                Ok(v) => v,
1967                Err(e) => {
1968                    let error_data = match &e {
1969                        analyze::AnalyzeError::Io(io_err) => match io_err.kind() {
1970                            std::io::ErrorKind::NotFound | std::io::ErrorKind::PermissionDenied => {
1971                                ErrorData::new(
1972                                    rmcp::model::ErrorCode::INVALID_PARAMS,
1973                                    format!("Failed to analyze module: {e}"),
1974                                    Some(error_meta(
1975                                        "validation",
1976                                        false,
1977                                        "ensure file exists, is readable, and has a supported extension",
1978                                    )),
1979                                )
1980                            }
1981                            _ => ErrorData::new(
1982                                rmcp::model::ErrorCode::INTERNAL_ERROR,
1983                                format!("Failed to analyze module: {e}"),
1984                                Some(error_meta("internal", false, "report this as a bug")),
1985                            ),
1986                        },
1987                        analyze::AnalyzeError::UnsupportedLanguage(_)
1988                        | analyze::AnalyzeError::InvalidRange { .. }
1989                        | analyze::AnalyzeError::NotAFile(_) => ErrorData::new(
1990                            rmcp::model::ErrorCode::INVALID_PARAMS,
1991                            format!("Failed to analyze module: {e}"),
1992                            Some(error_meta(
1993                                "validation",
1994                                false,
1995                                "ensure the path is a supported source file",
1996                            )),
1997                        ),
1998                        _ => ErrorData::new(
1999                            rmcp::model::ErrorCode::INTERNAL_ERROR,
2000                            format!("Failed to analyze module: {e}"),
2001                            Some(error_meta("internal", false, "report this as a bug")),
2002                        ),
2003                    };
2004                    return Ok(err_to_tool_result(error_data));
2005                }
2006            };
2007            let arc_output = std::sync::Arc::new(file_output);
2008            if let Some(key) = module_cache_key.clone() {
2009                self.cache.put(key, arc_output.clone());
2010            }
2011            let file_path = std::path::Path::new(&params.path);
2012            let name = file_path
2013                .file_name()
2014                .and_then(|n: &std::ffi::OsStr| n.to_str())
2015                .unwrap_or("unknown")
2016                .to_string();
2017            let language = file_path
2018                .extension()
2019                .and_then(|e| e.to_str())
2020                .and_then(aptu_coder_core::lang::language_for_extension)
2021                .unwrap_or("unknown")
2022                .to_string();
2023            let mut mi = types::ModuleInfo::default();
2024            mi.name = name;
2025            mi.line_count = arc_output.line_count;
2026            mi.language = language;
2027            mi.functions = arc_output
2028                .semantic
2029                .functions
2030                .iter()
2031                .map(|f| {
2032                    let mut mfi = types::ModuleFunctionInfo::default();
2033                    mfi.name = f.name.clone();
2034                    mfi.line = f.line;
2035                    mfi
2036                })
2037                .collect();
2038            mi.imports = arc_output
2039                .semantic
2040                .imports
2041                .iter()
2042                .map(|i| {
2043                    let mut mii = types::ModuleImportInfo::default();
2044                    mii.module = i.module.clone();
2045                    mii.items = i.items.clone();
2046                    mii
2047                })
2048                .collect();
2049            (mi, false)
2050        };
2051
2052        let text = format_module_info(&module_info);
2053        let mut result = CallToolResult::success(vec![Content::text(text.clone())])
2054            .with_meta(Some(no_cache_meta()));
2055        let structured = match serde_json::to_value(&module_info).map_err(|e| {
2056            ErrorData::new(
2057                rmcp::model::ErrorCode::INTERNAL_ERROR,
2058                format!("serialization failed: {e}"),
2059                Some(error_meta("internal", false, "report this as a bug")),
2060            )
2061        }) {
2062            Ok(v) => v,
2063            Err(e) => return Ok(err_to_tool_result(e)),
2064        };
2065        result.structured_content = Some(structured);
2066        let dur = t_start.elapsed().as_millis().try_into().unwrap_or(u64::MAX);
2067        self.metrics_tx.send(crate::metrics::MetricEvent {
2068            ts: crate::metrics::unix_ms(),
2069            tool: "analyze_module",
2070            duration_ms: dur,
2071            output_chars: text.len(),
2072            param_path_depth: crate::metrics::path_component_count(&param_path),
2073            max_depth: None,
2074            result: "ok",
2075            error_type: None,
2076            session_id: sid,
2077            seq: Some(seq),
2078            cache_hit: Some(module_cache_hit),
2079        });
2080        Ok(result)
2081    }
2082
2083    #[instrument(skip(self, _context))]
2084    #[tool(
2085        name = "edit_overwrite",
2086        title = "Edit Overwrite",
2087        description = "Creates or overwrites a file with UTF-8 content; creates parent directories if needed. Returns path, bytes_written. Fails if directory path supplied. AST-unaware (no language constraint). Use edit_replace for targeted single-block edits. working_dir sets the base directory for path resolution (default: server CWD). Example queries: Overwrite src/config.rs with updated content.",
2088        output_schema = schema_for_type::<EditOverwriteOutput>(),
2089        annotations(
2090            title = "Edit Overwrite",
2091            read_only_hint = false,
2092            destructive_hint = true,
2093            idempotent_hint = false,
2094            open_world_hint = false
2095        )
2096    )]
2097    async fn edit_overwrite(
2098        &self,
2099        params: Parameters<EditOverwriteParams>,
2100        _context: RequestContext<RoleServer>,
2101    ) -> Result<CallToolResult, ErrorData> {
2102        let params = params.0;
2103        let _validated_path = if let Some(ref wd) = params.working_dir {
2104            match validate_path_in_dir(&params.path, false, std::path::Path::new(wd)) {
2105                Ok(p) => p,
2106                Err(e) => return Ok(err_to_tool_result(e)),
2107            }
2108        } else {
2109            match validate_path(&params.path, false) {
2110                Ok(p) => p,
2111                Err(e) => return Ok(err_to_tool_result(e)),
2112            }
2113        };
2114        let t_start = std::time::Instant::now();
2115        let param_path = params.path.clone();
2116        let seq = self
2117            .session_call_seq
2118            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
2119        let sid = self.session_id.lock().await.clone();
2120
2121        // Guard against directory paths
2122        if std::fs::metadata(&params.path)
2123            .map(|m| m.is_dir())
2124            .unwrap_or(false)
2125        {
2126            let dur = t_start.elapsed().as_millis().try_into().unwrap_or(u64::MAX);
2127            self.metrics_tx.send(crate::metrics::MetricEvent {
2128                ts: crate::metrics::unix_ms(),
2129                tool: "edit_overwrite",
2130                duration_ms: dur,
2131                output_chars: 0,
2132                param_path_depth: crate::metrics::path_component_count(&param_path),
2133                max_depth: None,
2134                result: "error",
2135                error_type: Some("invalid_params".to_string()),
2136                session_id: sid.clone(),
2137                seq: Some(seq),
2138                cache_hit: None,
2139            });
2140            return Ok(err_to_tool_result(ErrorData::new(
2141                rmcp::model::ErrorCode::INVALID_PARAMS,
2142                "path is a directory; cannot write to a directory".to_string(),
2143                Some(error_meta(
2144                    "validation",
2145                    false,
2146                    "provide a file path, not a directory",
2147                )),
2148            )));
2149        }
2150
2151        let path = std::path::PathBuf::from(&params.path);
2152        let content = params.content.clone();
2153        let handle = tokio::task::spawn_blocking(move || {
2154            aptu_coder_core::edit_overwrite_content(&path, &content)
2155        });
2156
2157        let output = match handle.await {
2158            Ok(Ok(v)) => v,
2159            Ok(Err(aptu_coder_core::EditError::NotAFile(_))) => {
2160                let dur = t_start.elapsed().as_millis().try_into().unwrap_or(u64::MAX);
2161                self.metrics_tx.send(crate::metrics::MetricEvent {
2162                    ts: crate::metrics::unix_ms(),
2163                    tool: "edit_overwrite",
2164                    duration_ms: dur,
2165                    output_chars: 0,
2166                    param_path_depth: crate::metrics::path_component_count(&param_path),
2167                    max_depth: None,
2168                    result: "error",
2169                    error_type: Some("invalid_params".to_string()),
2170                    session_id: sid.clone(),
2171                    seq: Some(seq),
2172                    cache_hit: None,
2173                });
2174                return Ok(err_to_tool_result(ErrorData::new(
2175                    rmcp::model::ErrorCode::INVALID_PARAMS,
2176                    "path is a directory".to_string(),
2177                    Some(error_meta(
2178                        "validation",
2179                        false,
2180                        "provide a file path, not a directory",
2181                    )),
2182                )));
2183            }
2184            Ok(Err(e)) => {
2185                let dur = t_start.elapsed().as_millis().try_into().unwrap_or(u64::MAX);
2186                self.metrics_tx.send(crate::metrics::MetricEvent {
2187                    ts: crate::metrics::unix_ms(),
2188                    tool: "edit_overwrite",
2189                    duration_ms: dur,
2190                    output_chars: 0,
2191                    param_path_depth: crate::metrics::path_component_count(&param_path),
2192                    max_depth: None,
2193                    result: "error",
2194                    error_type: Some("internal_error".to_string()),
2195                    session_id: sid.clone(),
2196                    seq: Some(seq),
2197                    cache_hit: None,
2198                });
2199                return Ok(err_to_tool_result(ErrorData::new(
2200                    rmcp::model::ErrorCode::INTERNAL_ERROR,
2201                    e.to_string(),
2202                    Some(error_meta(
2203                        "resource",
2204                        false,
2205                        "check file path and permissions",
2206                    )),
2207                )));
2208            }
2209            Err(e) => {
2210                let dur = t_start.elapsed().as_millis().try_into().unwrap_or(u64::MAX);
2211                self.metrics_tx.send(crate::metrics::MetricEvent {
2212                    ts: crate::metrics::unix_ms(),
2213                    tool: "edit_overwrite",
2214                    duration_ms: dur,
2215                    output_chars: 0,
2216                    param_path_depth: crate::metrics::path_component_count(&param_path),
2217                    max_depth: None,
2218                    result: "error",
2219                    error_type: Some("internal_error".to_string()),
2220                    session_id: sid.clone(),
2221                    seq: Some(seq),
2222                    cache_hit: None,
2223                });
2224                return Ok(err_to_tool_result(ErrorData::new(
2225                    rmcp::model::ErrorCode::INTERNAL_ERROR,
2226                    e.to_string(),
2227                    Some(error_meta(
2228                        "resource",
2229                        false,
2230                        "check file path and permissions",
2231                    )),
2232                )));
2233            }
2234        };
2235
2236        let text = format!("Wrote {} bytes to {}", output.bytes_written, output.path);
2237        let mut result = CallToolResult::success(vec![Content::text(text.clone())])
2238            .with_meta(Some(no_cache_meta()));
2239        let structured = match serde_json::to_value(&output).map_err(|e| {
2240            ErrorData::new(
2241                rmcp::model::ErrorCode::INTERNAL_ERROR,
2242                format!("serialization failed: {e}"),
2243                Some(error_meta("internal", false, "report this as a bug")),
2244            )
2245        }) {
2246            Ok(v) => v,
2247            Err(e) => return Ok(err_to_tool_result(e)),
2248        };
2249        result.structured_content = Some(structured);
2250        self.cache
2251            .invalidate_file(&std::path::PathBuf::from(&param_path));
2252        let dur = t_start.elapsed().as_millis().try_into().unwrap_or(u64::MAX);
2253        self.metrics_tx.send(crate::metrics::MetricEvent {
2254            ts: crate::metrics::unix_ms(),
2255            tool: "edit_overwrite",
2256            duration_ms: dur,
2257            output_chars: text.len(),
2258            param_path_depth: crate::metrics::path_component_count(&param_path),
2259            max_depth: None,
2260            result: "ok",
2261            error_type: None,
2262            session_id: sid,
2263            seq: Some(seq),
2264            cache_hit: None,
2265        });
2266        Ok(result)
2267    }
2268
2269    #[instrument(skip(self, _context))]
2270    #[tool(
2271        name = "edit_replace",
2272        title = "Edit Replace",
2273        description = "Replaces a unique exact text block; old_text must match character-for-character and appear exactly once. Returns path, bytes_before, bytes_after. Fails if zero matches; fails if multiple matches (extend old_text to be more specific). Whitespace-sensitive exact match. Use edit_overwrite to replace the whole file. working_dir sets the base directory for path resolution (default: server CWD). Example queries: Update the function signature in lib.rs.",
2274        output_schema = schema_for_type::<EditReplaceOutput>(),
2275        annotations(
2276            title = "Edit Replace",
2277            read_only_hint = false,
2278            destructive_hint = true,
2279            idempotent_hint = false,
2280            open_world_hint = false
2281        )
2282    )]
2283    async fn edit_replace(
2284        &self,
2285        params: Parameters<EditReplaceParams>,
2286        _context: RequestContext<RoleServer>,
2287    ) -> Result<CallToolResult, ErrorData> {
2288        let params = params.0;
2289        let _validated_path = if let Some(ref wd) = params.working_dir {
2290            match validate_path_in_dir(&params.path, true, std::path::Path::new(wd)) {
2291                Ok(p) => p,
2292                Err(e) => return Ok(err_to_tool_result(e)),
2293            }
2294        } else {
2295            match validate_path(&params.path, true) {
2296                Ok(p) => p,
2297                Err(e) => return Ok(err_to_tool_result(e)),
2298            }
2299        };
2300        let t_start = std::time::Instant::now();
2301        let param_path = params.path.clone();
2302        let seq = self
2303            .session_call_seq
2304            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
2305        let sid = self.session_id.lock().await.clone();
2306
2307        // Guard against directory paths
2308        if std::fs::metadata(&params.path)
2309            .map(|m| m.is_dir())
2310            .unwrap_or(false)
2311        {
2312            let dur = t_start.elapsed().as_millis().try_into().unwrap_or(u64::MAX);
2313            self.metrics_tx.send(crate::metrics::MetricEvent {
2314                ts: crate::metrics::unix_ms(),
2315                tool: "edit_replace",
2316                duration_ms: dur,
2317                output_chars: 0,
2318                param_path_depth: crate::metrics::path_component_count(&param_path),
2319                max_depth: None,
2320                result: "error",
2321                error_type: Some("invalid_params".to_string()),
2322                session_id: sid.clone(),
2323                seq: Some(seq),
2324                cache_hit: None,
2325            });
2326            return Ok(err_to_tool_result(ErrorData::new(
2327                rmcp::model::ErrorCode::INVALID_PARAMS,
2328                "path is a directory; cannot edit a directory".to_string(),
2329                Some(error_meta(
2330                    "validation",
2331                    false,
2332                    "provide a file path, not a directory",
2333                )),
2334            )));
2335        }
2336
2337        let path = std::path::PathBuf::from(&params.path);
2338        let old_text = params.old_text.clone();
2339        let new_text = params.new_text.clone();
2340        let handle = tokio::task::spawn_blocking(move || {
2341            aptu_coder_core::edit_replace_block(&path, &old_text, &new_text)
2342        });
2343
2344        let output = match handle.await {
2345            Ok(Ok(v)) => v,
2346            Ok(Err(aptu_coder_core::EditError::NotFound { path: _ })) => {
2347                let dur = t_start.elapsed().as_millis().try_into().unwrap_or(u64::MAX);
2348                self.metrics_tx.send(crate::metrics::MetricEvent {
2349                    ts: crate::metrics::unix_ms(),
2350                    tool: "edit_replace",
2351                    duration_ms: dur,
2352                    output_chars: 0,
2353                    param_path_depth: crate::metrics::path_component_count(&param_path),
2354                    max_depth: None,
2355                    result: "error",
2356                    error_type: Some("invalid_params".to_string()),
2357                    session_id: sid.clone(),
2358                    seq: Some(seq),
2359                    cache_hit: None,
2360                });
2361                return Ok(err_to_tool_result(ErrorData::new(
2362                    rmcp::model::ErrorCode::INVALID_PARAMS,
2363                    "old_text not found in file — verify the text matches exactly, including whitespace and newlines".to_string(),
2364                    Some(error_meta(
2365                        "validation",
2366                        false,
2367                        "check that old_text appears in the file",
2368                    )),
2369                )));
2370            }
2371            Ok(Err(aptu_coder_core::EditError::Ambiguous { count, path: _ })) => {
2372                let dur = t_start.elapsed().as_millis().try_into().unwrap_or(u64::MAX);
2373                self.metrics_tx.send(crate::metrics::MetricEvent {
2374                    ts: crate::metrics::unix_ms(),
2375                    tool: "edit_replace",
2376                    duration_ms: dur,
2377                    output_chars: 0,
2378                    param_path_depth: crate::metrics::path_component_count(&param_path),
2379                    max_depth: None,
2380                    result: "error",
2381                    error_type: Some("invalid_params".to_string()),
2382                    session_id: sid.clone(),
2383                    seq: Some(seq),
2384                    cache_hit: None,
2385                });
2386                return Ok(err_to_tool_result(ErrorData::new(
2387                    rmcp::model::ErrorCode::INVALID_PARAMS,
2388                    format!(
2389                        "old_text appears {count} times in file — make old_text longer and more specific to uniquely identify the block"
2390                    ),
2391                    Some(error_meta(
2392                        "validation",
2393                        false,
2394                        "include more context in old_text to make it unique",
2395                    )),
2396                )));
2397            }
2398            Ok(Err(aptu_coder_core::EditError::NotAFile(_))) => {
2399                let dur = t_start.elapsed().as_millis().try_into().unwrap_or(u64::MAX);
2400                self.metrics_tx.send(crate::metrics::MetricEvent {
2401                    ts: crate::metrics::unix_ms(),
2402                    tool: "edit_replace",
2403                    duration_ms: dur,
2404                    output_chars: 0,
2405                    param_path_depth: crate::metrics::path_component_count(&param_path),
2406                    max_depth: None,
2407                    result: "error",
2408                    error_type: Some("invalid_params".to_string()),
2409                    session_id: sid.clone(),
2410                    seq: Some(seq),
2411                    cache_hit: None,
2412                });
2413                return Ok(err_to_tool_result(ErrorData::new(
2414                    rmcp::model::ErrorCode::INVALID_PARAMS,
2415                    "path is a directory".to_string(),
2416                    Some(error_meta(
2417                        "validation",
2418                        false,
2419                        "provide a file path, not a directory",
2420                    )),
2421                )));
2422            }
2423            Ok(Err(e)) => {
2424                let dur = t_start.elapsed().as_millis().try_into().unwrap_or(u64::MAX);
2425                self.metrics_tx.send(crate::metrics::MetricEvent {
2426                    ts: crate::metrics::unix_ms(),
2427                    tool: "edit_replace",
2428                    duration_ms: dur,
2429                    output_chars: 0,
2430                    param_path_depth: crate::metrics::path_component_count(&param_path),
2431                    max_depth: None,
2432                    result: "error",
2433                    error_type: Some("internal_error".to_string()),
2434                    session_id: sid.clone(),
2435                    seq: Some(seq),
2436                    cache_hit: None,
2437                });
2438                return Ok(err_to_tool_result(ErrorData::new(
2439                    rmcp::model::ErrorCode::INTERNAL_ERROR,
2440                    e.to_string(),
2441                    Some(error_meta(
2442                        "resource",
2443                        false,
2444                        "check file path and permissions",
2445                    )),
2446                )));
2447            }
2448            Err(e) => {
2449                let dur = t_start.elapsed().as_millis().try_into().unwrap_or(u64::MAX);
2450                self.metrics_tx.send(crate::metrics::MetricEvent {
2451                    ts: crate::metrics::unix_ms(),
2452                    tool: "edit_replace",
2453                    duration_ms: dur,
2454                    output_chars: 0,
2455                    param_path_depth: crate::metrics::path_component_count(&param_path),
2456                    max_depth: None,
2457                    result: "error",
2458                    error_type: Some("internal_error".to_string()),
2459                    session_id: sid.clone(),
2460                    seq: Some(seq),
2461                    cache_hit: None,
2462                });
2463                return Ok(err_to_tool_result(ErrorData::new(
2464                    rmcp::model::ErrorCode::INTERNAL_ERROR,
2465                    e.to_string(),
2466                    Some(error_meta(
2467                        "resource",
2468                        false,
2469                        "check file path and permissions",
2470                    )),
2471                )));
2472            }
2473        };
2474
2475        let text = format!(
2476            "Edited {}: {} bytes -> {} bytes",
2477            output.path, output.bytes_before, output.bytes_after
2478        );
2479        let mut result = CallToolResult::success(vec![Content::text(text.clone())])
2480            .with_meta(Some(no_cache_meta()));
2481        let structured = match serde_json::to_value(&output).map_err(|e| {
2482            ErrorData::new(
2483                rmcp::model::ErrorCode::INTERNAL_ERROR,
2484                format!("serialization failed: {e}"),
2485                Some(error_meta("internal", false, "report this as a bug")),
2486            )
2487        }) {
2488            Ok(v) => v,
2489            Err(e) => return Ok(err_to_tool_result(e)),
2490        };
2491        result.structured_content = Some(structured);
2492        self.cache
2493            .invalidate_file(&std::path::PathBuf::from(&param_path));
2494        let dur = t_start.elapsed().as_millis().try_into().unwrap_or(u64::MAX);
2495        self.metrics_tx.send(crate::metrics::MetricEvent {
2496            ts: crate::metrics::unix_ms(),
2497            tool: "edit_replace",
2498            duration_ms: dur,
2499            output_chars: text.len(),
2500            param_path_depth: crate::metrics::path_component_count(&param_path),
2501            max_depth: None,
2502            result: "ok",
2503            error_type: None,
2504            session_id: sid,
2505            seq: Some(seq),
2506            cache_hit: None,
2507        });
2508        Ok(result)
2509    }
2510
2511    #[tool(
2512        name = "exec_command",
2513        title = "Exec Command",
2514        description = "Execute shell command via sh -c (or $SHELL if set). Returns stdout, stderr, interleaved, exit_code, timed_out, output_truncated. Output capped at 2000 lines and 50 KB per stream; use timeout_secs to limit execution time. working_dir sets initial working directory; cd and absolute paths in command string bypass this restriction. Fails if working_dir does not exist, is not a directory, or is outside CWD. Pass stdin to pipe UTF-8 content into the process (max 1 MB). For file creation and edits, prefer the edit_* tools. Example queries: Run the test suite and capture output.",
2515        output_schema = schema_for_type::<types::ShellOutput>(),
2516        annotations(
2517            title = "Exec Command",
2518            read_only_hint = false,
2519            destructive_hint = true,
2520            idempotent_hint = false,
2521            open_world_hint = true
2522        )
2523    )]
2524    #[instrument(skip(self, _context))]
2525    pub async fn exec_command(
2526        &self,
2527        params: Parameters<types::ExecCommandParams>,
2528        _context: RequestContext<RoleServer>,
2529    ) -> Result<CallToolResult, ErrorData> {
2530        let t_start = std::time::Instant::now();
2531        let params = params.0;
2532
2533        // Validate working_dir if provided
2534        let working_dir_path = if let Some(ref wd) = params.working_dir {
2535            match validate_path(wd, true) {
2536                Ok(p) => {
2537                    // Verify it's a directory
2538                    if !std::fs::metadata(&p).map(|m| m.is_dir()).unwrap_or(false) {
2539                        return Ok(err_to_tool_result(ErrorData::new(
2540                            rmcp::model::ErrorCode::INVALID_PARAMS,
2541                            "working_dir must be a directory".to_string(),
2542                            Some(error_meta(
2543                                "validation",
2544                                false,
2545                                "provide a valid directory path",
2546                            )),
2547                        )));
2548                    }
2549                    Some(p)
2550                }
2551                Err(e) => {
2552                    return Ok(err_to_tool_result(e));
2553                }
2554            }
2555        } else {
2556            None
2557        };
2558
2559        let param_path = params.working_dir.clone();
2560        let seq = self
2561            .session_call_seq
2562            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
2563        let sid = self.session_id.lock().await.clone();
2564
2565        // Validate stdin size cap (1 MB)
2566        if let Some(ref stdin_content) = params.stdin
2567            && stdin_content.len() > STDIN_MAX_BYTES
2568        {
2569            return Ok(err_to_tool_result(ErrorData::new(
2570                rmcp::model::ErrorCode::INVALID_PARAMS,
2571                "stdin exceeds 1 MB limit".to_string(),
2572                Some(error_meta("validation", false, "reduce stdin content size")),
2573            )));
2574        }
2575
2576        let command = params.command.clone();
2577        let timeout_secs = params.timeout_secs;
2578
2579        // Determine cache key and whether to use cache
2580        let cache_key = (
2581            command.clone(),
2582            working_dir_path
2583                .as_ref()
2584                .map(|p| p.display().to_string())
2585                .unwrap_or_default(),
2586        );
2587        let use_cache = params.cache.unwrap_or(true) && params.stdin.is_none();
2588
2589        // Check if result is already cached (for metrics)
2590        let was_cached = if use_cache {
2591            self.exec_cache.contains_key(&cache_key)
2592        } else {
2593            false
2594        };
2595
2596        // Execute command with caching
2597        let output = if use_cache {
2598            self.exec_cache
2599                .get_with(cache_key.clone(), async {
2600                    run_exec_impl(
2601                        command.clone(),
2602                        working_dir_path.clone(),
2603                        timeout_secs,
2604                        params.memory_limit_mb,
2605                        params.cpu_limit_secs,
2606                        params.stdin.clone(),
2607                        seq,
2608                    )
2609                    .await
2610                })
2611                .await
2612        } else {
2613            run_exec_impl(
2614                command.clone(),
2615                working_dir_path.clone(),
2616                timeout_secs,
2617                params.memory_limit_mb,
2618                params.cpu_limit_secs,
2619                params.stdin.clone(),
2620                seq,
2621            )
2622            .await
2623        };
2624
2625        // Invalidate cache entry if command failed (non-zero exit)
2626        if use_cache && output.exit_code.map(|c| c != 0).unwrap_or(false) {
2627            self.exec_cache.invalidate(&cache_key).await;
2628        }
2629
2630        let exit_code = output.exit_code;
2631        let timed_out = output.timed_out;
2632        let output_truncated = output.output_truncated;
2633
2634        // Use interleaved if non-empty; fall back to separated stdout/stderr for empty-output commands
2635        let output_text = if output.interleaved.is_empty() {
2636            format!("Stdout:\n{}\n\nStderr:\n{}", output.stdout, output.stderr)
2637        } else {
2638            format!("Output:\n{}", output.interleaved)
2639        };
2640
2641        let text = format!(
2642            "Command: {}\nExit code: {}\nTimed out: {}\nOutput truncated: {}\n\n{}",
2643            params.command,
2644            exit_code
2645                .map(|c| c.to_string())
2646                .unwrap_or_else(|| "null".to_string()),
2647            timed_out,
2648            output_truncated,
2649            output_text,
2650        );
2651
2652        let content_blocks = vec![Content::text(text.clone()).with_priority(0.0)];
2653
2654        // Determine if command failed: timeout or non-zero exit code.
2655        // exit_code is None when: (a) process killed by O1 post-exit drain timeout (background child
2656        // holding pipes -- command work was done, treat as success) or (b) externally killed; both
2657        // cases use unwrap_or(false) to avoid false negatives.
2658        let command_failed = timed_out || exit_code.map(|c| c != 0).unwrap_or(false);
2659
2660        let mut result = if command_failed {
2661            CallToolResult::error(content_blocks)
2662        } else {
2663            CallToolResult::success(content_blocks)
2664        }
2665        .with_meta(Some(no_cache_meta()));
2666
2667        let structured = match serde_json::to_value(&output).map_err(|e| {
2668            ErrorData::new(
2669                rmcp::model::ErrorCode::INTERNAL_ERROR,
2670                format!("serialization failed: {e}"),
2671                Some(error_meta("internal", false, "report this as a bug")),
2672            )
2673        }) {
2674            Ok(v) => v,
2675            Err(e) => {
2676                let dur = t_start.elapsed().as_millis().try_into().unwrap_or(u64::MAX);
2677                self.metrics_tx.send(crate::metrics::MetricEvent {
2678                    ts: crate::metrics::unix_ms(),
2679                    tool: "exec_command",
2680                    duration_ms: dur,
2681                    output_chars: 0,
2682                    param_path_depth: crate::metrics::path_component_count(
2683                        param_path.as_deref().unwrap_or(""),
2684                    ),
2685                    max_depth: None,
2686                    result: "error",
2687                    error_type: Some("internal_error".to_string()),
2688                    session_id: sid.clone(),
2689                    seq: Some(seq),
2690                    cache_hit: Some(was_cached),
2691                });
2692                return Ok(err_to_tool_result(e));
2693            }
2694        };
2695
2696        result.structured_content = Some(structured);
2697        let dur = t_start.elapsed().as_millis().try_into().unwrap_or(u64::MAX);
2698        self.metrics_tx.send(crate::metrics::MetricEvent {
2699            ts: crate::metrics::unix_ms(),
2700            tool: "exec_command",
2701            duration_ms: dur,
2702            output_chars: text.len(),
2703            param_path_depth: crate::metrics::path_component_count(
2704                param_path.as_deref().unwrap_or(""),
2705            ),
2706            max_depth: None,
2707            result: "ok",
2708            error_type: None,
2709            session_id: sid,
2710            seq: Some(seq),
2711            cache_hit: Some(was_cached),
2712        });
2713        Ok(result)
2714    }
2715}
2716
2717/// Executes a shell command and returns the output.
2718/// This is a free async function (not a method) to allow use in moka::future::Cache::get_with().
2719/// It spawns the command, collects output with timeout handling, and persists output to slot files.
2720async fn run_exec_impl(
2721    command: String,
2722    working_dir_path: Option<std::path::PathBuf>,
2723    timeout_secs: Option<u64>,
2724    memory_limit_mb: Option<u64>,
2725    cpu_limit_secs: Option<u64>,
2726    stdin: Option<String>,
2727    seq: u32,
2728) -> types::ShellOutput {
2729    use tokio::io::AsyncBufReadExt as _;
2730    use tokio_stream::StreamExt as TokioStreamExt;
2731    use tokio_stream::wrappers::LinesStream;
2732
2733    let shell = resolve_shell();
2734    let mut cmd = tokio::process::Command::new(shell);
2735    cmd.arg("-c").arg(&command);
2736
2737    if let Some(ref wd) = working_dir_path {
2738        cmd.current_dir(wd);
2739    }
2740
2741    cmd.stdout(std::process::Stdio::piped())
2742        .stderr(std::process::Stdio::piped());
2743
2744    if stdin.is_some() {
2745        cmd.stdin(std::process::Stdio::piped());
2746    } else {
2747        cmd.stdin(std::process::Stdio::null());
2748    }
2749
2750    #[cfg(unix)]
2751    {
2752        #[cfg(not(target_os = "linux"))]
2753        if memory_limit_mb.is_some() {
2754            warn!("memory_limit_mb is not enforced on this platform (Linux only)");
2755        }
2756        if memory_limit_mb.is_some() || cpu_limit_secs.is_some() {
2757            unsafe {
2758                cmd.pre_exec(move || {
2759                    #[cfg(target_os = "linux")]
2760                    if let Some(mb) = memory_limit_mb {
2761                        let bytes = mb.saturating_mul(1024 * 1024);
2762                        setrlimit(Resource::RLIMIT_AS, bytes, bytes)
2763                            .map_err(|e| std::io::Error::from_raw_os_error(e as i32))?;
2764                    }
2765                    if let Some(cpu) = cpu_limit_secs {
2766                        setrlimit(Resource::RLIMIT_CPU, cpu, cpu)
2767                            .map_err(|e| std::io::Error::from_raw_os_error(e as i32))?;
2768                    }
2769                    Ok(())
2770                });
2771            }
2772        }
2773    }
2774
2775    let mut child = match cmd.spawn() {
2776        Ok(c) => c,
2777        Err(e) => {
2778            return types::ShellOutput::new(
2779                String::new(),
2780                format!("failed to spawn command: {e}"),
2781                format!("failed to spawn command: {e}"),
2782                None,
2783                false,
2784                false,
2785            );
2786        }
2787    };
2788
2789    let stdout_pipe = child.stdout.take();
2790    let stderr_pipe = child.stderr.take();
2791
2792    if let Some(stdin_content) = stdin
2793        && let Some(mut stdin_handle) = child.stdin.take()
2794    {
2795        use tokio::io::AsyncWriteExt as _;
2796        match stdin_handle.write_all(stdin_content.as_bytes()).await {
2797            Ok(()) => {
2798                drop(stdin_handle);
2799            }
2800            Err(e) if e.kind() == std::io::ErrorKind::BrokenPipe => {}
2801            Err(e) => {
2802                warn!("failed to write stdin: {e}");
2803            }
2804        }
2805    }
2806
2807    let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel::<(bool, String)>();
2808
2809    let mut drain_task = tokio::spawn(async move {
2810        let so_stream = stdout_pipe.map(|p| {
2811            LinesStream::new(tokio::io::BufReader::new(p).lines()).map(|l| l.map(|s| (false, s)))
2812        });
2813        let se_stream = stderr_pipe.map(|p| {
2814            LinesStream::new(tokio::io::BufReader::new(p).lines()).map(|l| l.map(|s| (true, s)))
2815        });
2816
2817        match (so_stream, se_stream) {
2818            (Some(so), Some(se)) => {
2819                let mut merged = so.merge(se);
2820                while let Some(Ok((is_stderr, line))) = merged.next().await {
2821                    let _ = tx.send((is_stderr, line));
2822                }
2823            }
2824            (Some(so), None) => {
2825                let mut stream = so;
2826                while let Some(Ok((_, line))) = stream.next().await {
2827                    let _ = tx.send((false, line));
2828                }
2829            }
2830            (None, Some(se)) => {
2831                let mut stream = se;
2832                while let Some(Ok((_, line))) = stream.next().await {
2833                    let _ = tx.send((true, line));
2834                }
2835            }
2836            (None, None) => {}
2837        }
2838    });
2839
2840    let (exit_code, timed_out, mut output_truncated, output_collection_error) = tokio::select! {
2841        _ = &mut drain_task => {
2842            let (status, drain_truncated) = match tokio::time::timeout(
2843                std::time::Duration::from_millis(500),
2844                child.wait()
2845            ).await {
2846                Ok(Ok(s)) => (Some(s), false),
2847                Ok(Err(_)) => (None, false),
2848                Err(_) => {
2849                    child.start_kill().ok();
2850                    let _ = child.wait().await;
2851                    (None, true)
2852                }
2853            };
2854            let exit_code = status.and_then(|s| s.code());
2855            let ocerr = if drain_truncated {
2856                Some("post-exit drain timeout: background process held pipes".to_string())
2857            } else {
2858                None
2859            };
2860            (exit_code, false, drain_truncated, ocerr)
2861        }
2862        _ = async {
2863            if let Some(secs) = timeout_secs {
2864                tokio::time::sleep(std::time::Duration::from_secs(secs)).await;
2865            } else {
2866                std::future::pending::<()>().await;
2867            }
2868        } => {
2869            let _ = child.kill().await;
2870            let _ = child.wait().await;
2871            drain_task.abort();
2872            (None, true, false, None)
2873        }
2874    };
2875
2876    let mut lines: Vec<(bool, String)> = Vec::new();
2877    while let Some(item) = rx.recv().await {
2878        lines.push(item);
2879    }
2880
2881    // Split tagged lines into stdout, stderr, interleaved post-facto (no locks needed).
2882    const MAX_BYTES: usize = 50 * 1024;
2883    let mut stdout_str = String::new();
2884    let mut stderr_str = String::new();
2885    let mut interleaved_str = String::new();
2886    let mut so_bytes = 0usize;
2887    let mut se_bytes = 0usize;
2888    let mut il_bytes = 0usize;
2889    for (is_stderr, line) in &lines {
2890        let entry = format!("{line}\n");
2891        if il_bytes < 2 * MAX_BYTES {
2892            il_bytes += entry.len();
2893            interleaved_str.push_str(&entry);
2894        }
2895        if *is_stderr {
2896            if se_bytes < MAX_BYTES {
2897                se_bytes += entry.len();
2898                stderr_str.push_str(&entry);
2899            }
2900        } else if so_bytes < MAX_BYTES {
2901            so_bytes += entry.len();
2902            stdout_str.push_str(&entry);
2903        }
2904    }
2905
2906    let slot = seq % 8;
2907    let (stdout, stderr, stdout_path, stderr_path) =
2908        handle_output_persist(stdout_str, stderr_str, slot);
2909    output_truncated = output_truncated || stdout_path.is_some();
2910
2911    let mut output = types::ShellOutput::new(
2912        stdout,
2913        stderr,
2914        interleaved_str,
2915        exit_code,
2916        timed_out,
2917        output_truncated,
2918    );
2919    output.output_collection_error = output_collection_error;
2920    output.stdout_path = stdout_path;
2921    output.stderr_path = stderr_path;
2922
2923    output
2924}
2925
2926/// Handles output persistence by writing to slot files only when output overflows the line limit.
2927/// Writes full stdout/stderr to:
2928///   {temp_dir}/aptu-coder-overflow/slot-{slot}/{stdout,stderr}
2929/// Returns (stdout_out, stderr_out, stdout_path, stderr_path).
2930/// On overflow: truncates to last 50 lines and sets paths to Some.
2931/// Under limit: returns output unchanged and paths as None (no I/O).
2932fn handle_output_persist(
2933    stdout: String,
2934    stderr: String,
2935    slot: u32,
2936) -> (String, String, Option<String>, Option<String>) {
2937    const MAX_OUTPUT_LINES: usize = 2000;
2938    const OVERFLOW_PREVIEW_LINES: usize = 50;
2939
2940    let stdout_lines: Vec<&str> = stdout.lines().collect();
2941    let stderr_lines: Vec<&str> = stderr.lines().collect();
2942
2943    // No overflow: return as-is with no I/O.
2944    if stdout_lines.len() <= MAX_OUTPUT_LINES && stderr_lines.len() <= MAX_OUTPUT_LINES {
2945        return (stdout, stderr, None, None);
2946    }
2947
2948    // Overflow: write slot files and return last-N-lines preview.
2949    let base = std::env::temp_dir()
2950        .join("aptu-coder-overflow")
2951        .join(format!("slot-{slot}"));
2952    let _ = std::fs::create_dir_all(&base);
2953
2954    let stdout_path = base.join("stdout");
2955    let stderr_path = base.join("stderr");
2956
2957    let _ = std::fs::write(&stdout_path, stdout.as_bytes());
2958    let _ = std::fs::write(&stderr_path, stderr.as_bytes());
2959
2960    let stdout_path_str = stdout_path.display().to_string();
2961    let stderr_path_str = stderr_path.display().to_string();
2962
2963    let stdout_preview = if stdout_lines.len() > MAX_OUTPUT_LINES {
2964        stdout_lines[stdout_lines.len().saturating_sub(OVERFLOW_PREVIEW_LINES)..].join("\n")
2965    } else {
2966        stdout
2967    };
2968    let stderr_preview = if stderr_lines.len() > MAX_OUTPUT_LINES {
2969        stderr_lines[stderr_lines.len().saturating_sub(OVERFLOW_PREVIEW_LINES)..].join("\n")
2970    } else {
2971        stderr
2972    };
2973
2974    (
2975        stdout_preview,
2976        stderr_preview,
2977        Some(stdout_path_str),
2978        Some(stderr_path_str),
2979    )
2980}
2981
2982/// Truncates output to a maximum number of lines and bytes.
2983/// Returns (truncated_output, was_truncated).
2984
2985#[derive(Clone)]
2986struct FocusedAnalysisParams {
2987    path: std::path::PathBuf,
2988    symbol: String,
2989    match_mode: SymbolMatchMode,
2990    follow_depth: u32,
2991    max_depth: Option<u32>,
2992    ast_recursion_limit: Option<usize>,
2993    use_summary: bool,
2994    impl_only: Option<bool>,
2995    def_use: bool,
2996    parse_timeout_micros: Option<u64>,
2997}
2998
2999#[tool_handler]
3000impl ServerHandler for CodeAnalyzer {
3001    async fn initialize(
3002        &self,
3003        _request: InitializeRequestParams,
3004        context: RequestContext<RoleServer>,
3005    ) -> Result<InitializeResult, ErrorData> {
3006        // The _meta field is extracted from params and stored in request extensions.
3007        // Extract it and store for use in on_initialized.
3008        if let Some(meta) = context.extensions.get::<Meta>() {
3009            let mut meta_lock = self.profile_meta.lock().await;
3010            *meta_lock = Some(meta.0.clone());
3011        }
3012        Ok(self.get_info())
3013    }
3014
3015    fn get_info(&self) -> InitializeResult {
3016        let excluded = crate::EXCLUDED_DIRS.join(", ");
3017        let instructions = format!(
3018            "Recommended workflow:\n\
3019            1. Start with analyze_directory(path=<repo_root>, max_depth=2, summary=true) to identify source package (largest by file count; exclude {excluded}).\n\
3020            2. Re-run analyze_directory(path=<source_package>, max_depth=2, summary=true) for module map. Include test directories (tests/, *_test.go, test_*.py, test_*.rs, *.spec.ts, *.spec.js).\n\
3021            3. For key files, prefer analyze_module for function/import index; use analyze_file for signatures and types.\n\
3022            4. Use analyze_symbol to trace call graphs.\n\
3023            Prefer summary=true on 1000+ files. Set max_depth=2; increase if packages too large. Paginate with cursor/page_size. For subagents: DISABLE_PROMPT_CACHING=1."
3024        );
3025        let capabilities = ServerCapabilities::builder()
3026            .enable_logging()
3027            .enable_tools()
3028            .enable_tool_list_changed()
3029            .enable_completions()
3030            .build();
3031        let server_info = Implementation::new("aptu-coder", env!("CARGO_PKG_VERSION"))
3032            .with_title("Aptu Coder")
3033            .with_description("MCP server for code structure analysis using tree-sitter");
3034        InitializeResult::new(capabilities)
3035            .with_server_info(server_info)
3036            .with_instructions(&instructions)
3037    }
3038
3039    async fn list_tools(
3040        &self,
3041        _request: Option<rmcp::model::PaginatedRequestParams>,
3042        _context: RequestContext<RoleServer>,
3043    ) -> Result<rmcp::model::ListToolsResult, ErrorData> {
3044        let router = self.tool_router.read().await;
3045        Ok(rmcp::model::ListToolsResult {
3046            tools: router.list_all(),
3047            meta: None,
3048            next_cursor: None,
3049        })
3050    }
3051
3052    async fn call_tool(
3053        &self,
3054        request: rmcp::model::CallToolRequestParams,
3055        context: RequestContext<RoleServer>,
3056    ) -> Result<CallToolResult, ErrorData> {
3057        let tcc = rmcp::handler::server::tool::ToolCallContext::new(self, request, context);
3058        let router = self.tool_router.read().await;
3059        router.call(tcc).await
3060    }
3061
3062    async fn on_initialized(&self, context: NotificationContext<RoleServer>) {
3063        let mut peer_lock = self.peer.lock().await;
3064        *peer_lock = Some(context.peer.clone());
3065        drop(peer_lock);
3066
3067        // Generate session_id in MILLIS-N format
3068        let millis = std::time::SystemTime::now()
3069            .duration_since(std::time::UNIX_EPOCH)
3070            .unwrap_or_default()
3071            .as_millis()
3072            .try_into()
3073            .unwrap_or(u64::MAX);
3074        let counter = GLOBAL_SESSION_COUNTER.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
3075        let sid = format!("{millis}-{counter}");
3076        {
3077            let mut session_id_lock = self.session_id.lock().await;
3078            *session_id_lock = Some(sid);
3079        }
3080        self.session_call_seq
3081            .store(0, std::sync::atomic::Ordering::Relaxed);
3082
3083        // NON-STANDARD VENDOR EXTENSION: profile-based tool filtering.
3084        // The MCP 2025-11-25 spec has no profile or tool-subset concept; tools/list returns
3085        // all tools with no filtering parameters. This mechanism is retained solely for
3086        // controlled benchmarking (wave10/11). Do not promote or document it as a product
3087        // feature. The spec-compliant way to restrict tools is for the orchestrator to pass
3088        // a filtered `tools` array in the API call, or for clients to use tool annotations
3089        // (readOnlyHint/destructiveHint) to apply their own policy.
3090        // Profiles: "edit" (3 tools), "analyze" (5 tools), absent/unknown (all 7 tools).
3091        // _meta key "io.clouatre-labs/profile" takes precedence over APTU_CODER_PROFILE env var.
3092        let meta_lock = self.profile_meta.lock().await;
3093        let meta_profile = meta_lock
3094            .as_ref()
3095            .and_then(|m| m.get("io.clouatre-labs/profile"))
3096            .and_then(|v| v.as_str())
3097            .map(str::to_owned);
3098        drop(meta_lock);
3099
3100        // Resolve the active profile: _meta wins; fall back to env var.
3101        let active_profile = meta_profile.or(std::env::var("APTU_CODER_PROFILE").ok());
3102
3103        if let Some(ref profile) = active_profile {
3104            let mut router = self.tool_router.write().await;
3105            match profile.as_str() {
3106                "edit" => {
3107                    // Enable only: edit_replace, edit_overwrite, exec_command
3108                    router.disable_route("analyze_directory");
3109                    router.disable_route("analyze_file");
3110                    router.disable_route("analyze_module");
3111                    router.disable_route("analyze_symbol");
3112                }
3113                "analyze" => {
3114                    // Enable only: analyze_directory, analyze_file, analyze_module, analyze_symbol, exec_command
3115                    router.disable_route("edit_replace");
3116                    router.disable_route("edit_overwrite");
3117                }
3118                _ => {
3119                    // Unknown profile: leave all tools enabled (lenient fallback)
3120                }
3121            }
3122            // Bind peer notifier after disabling tools to send tools/list_changed notification
3123            router.bind_peer_notifier(&context.peer);
3124        }
3125
3126        // Spawn consumer task to drain log events from channel with batching.
3127        let peer = self.peer.clone();
3128        let event_rx = self.event_rx.clone();
3129
3130        tokio::spawn(async move {
3131            let rx = {
3132                let mut rx_lock = event_rx.lock().await;
3133                rx_lock.take()
3134            };
3135
3136            if let Some(mut receiver) = rx {
3137                let mut buffer = Vec::with_capacity(64);
3138                loop {
3139                    // Drain up to 64 events from channel
3140                    receiver.recv_many(&mut buffer, 64).await;
3141
3142                    if buffer.is_empty() {
3143                        // Channel closed, exit consumer task
3144                        break;
3145                    }
3146
3147                    // Acquire peer lock once per batch
3148                    let peer_lock = peer.lock().await;
3149                    if let Some(peer) = peer_lock.as_ref() {
3150                        for log_event in buffer.drain(..) {
3151                            let notification = ServerNotification::LoggingMessageNotification(
3152                                Notification::new(LoggingMessageNotificationParam {
3153                                    level: log_event.level,
3154                                    logger: Some(log_event.logger),
3155                                    data: log_event.data,
3156                                }),
3157                            );
3158                            if let Err(e) = peer.send_notification(notification).await {
3159                                warn!("Failed to send logging notification: {}", e);
3160                            }
3161                        }
3162                    }
3163                }
3164            }
3165        });
3166    }
3167
3168    #[instrument(skip(self, _context))]
3169    async fn on_cancelled(
3170        &self,
3171        notification: CancelledNotificationParam,
3172        _context: NotificationContext<RoleServer>,
3173    ) {
3174        tracing::info!(
3175            request_id = ?notification.request_id,
3176            reason = ?notification.reason,
3177            "Received cancellation notification"
3178        );
3179    }
3180
3181    #[instrument(skip(self, _context))]
3182    async fn complete(
3183        &self,
3184        request: CompleteRequestParams,
3185        _context: RequestContext<RoleServer>,
3186    ) -> Result<CompleteResult, ErrorData> {
3187        // Dispatch on argument name: "path" or "symbol"
3188        let argument_name = &request.argument.name;
3189        let argument_value = &request.argument.value;
3190
3191        let completions = match argument_name.as_str() {
3192            "path" => {
3193                // Path completions: use current directory as root
3194                let root = Path::new(".");
3195                completion::path_completions(root, argument_value)
3196            }
3197            "symbol" => {
3198                // Symbol completions: need the path argument from context
3199                let path_arg = request
3200                    .context
3201                    .as_ref()
3202                    .and_then(|ctx| ctx.get_argument("path"));
3203
3204                match path_arg {
3205                    Some(path_str) => {
3206                        let path = Path::new(path_str);
3207                        completion::symbol_completions(&self.cache, path, argument_value)
3208                    }
3209                    None => Vec::new(),
3210                }
3211            }
3212            _ => Vec::new(),
3213        };
3214
3215        // Create CompletionInfo with has_more flag if >100 results
3216        let total_count = u32::try_from(completions.len()).unwrap_or(u32::MAX);
3217        let (values, has_more) = if completions.len() > 100 {
3218            (completions.into_iter().take(100).collect(), true)
3219        } else {
3220            (completions, false)
3221        };
3222
3223        let completion_info =
3224            match CompletionInfo::with_pagination(values, Some(total_count), has_more) {
3225                Ok(info) => info,
3226                Err(_) => {
3227                    // Graceful degradation: return empty on error
3228                    CompletionInfo::with_all_values(Vec::new())
3229                        .unwrap_or_else(|_| CompletionInfo::new(Vec::new()).unwrap())
3230                }
3231            };
3232
3233        Ok(CompleteResult::new(completion_info))
3234    }
3235
3236    async fn set_level(
3237        &self,
3238        params: SetLevelRequestParams,
3239        _context: RequestContext<RoleServer>,
3240    ) -> Result<(), ErrorData> {
3241        let level_filter = match params.level {
3242            LoggingLevel::Debug => LevelFilter::DEBUG,
3243            LoggingLevel::Info | LoggingLevel::Notice => LevelFilter::INFO,
3244            LoggingLevel::Warning => LevelFilter::WARN,
3245            LoggingLevel::Error
3246            | LoggingLevel::Critical
3247            | LoggingLevel::Alert
3248            | LoggingLevel::Emergency => LevelFilter::ERROR,
3249        };
3250
3251        let mut filter_lock = self
3252            .log_level_filter
3253            .lock()
3254            .unwrap_or_else(|e| e.into_inner());
3255        *filter_lock = level_filter;
3256        Ok(())
3257    }
3258}
3259
3260#[cfg(test)]
3261mod tests {
3262    use super::*;
3263
3264    #[tokio::test]
3265    async fn test_emit_progress_none_peer_is_noop() {
3266        let peer = Arc::new(TokioMutex::new(None));
3267        let log_level_filter = Arc::new(Mutex::new(LevelFilter::INFO));
3268        let (_tx, rx) = tokio::sync::mpsc::unbounded_channel();
3269        let (metrics_tx, _metrics_rx) = tokio::sync::mpsc::unbounded_channel();
3270        let analyzer = CodeAnalyzer::new(
3271            peer,
3272            log_level_filter,
3273            rx,
3274            crate::metrics::MetricsSender(metrics_tx),
3275        );
3276        let token = ProgressToken(NumberOrString::String("test".into()));
3277        // Should complete without panic
3278        analyzer
3279            .emit_progress(None, &token, 0.0, 10.0, "test".to_string())
3280            .await;
3281    }
3282
3283    fn make_analyzer() -> CodeAnalyzer {
3284        let peer = Arc::new(TokioMutex::new(None));
3285        let log_level_filter = Arc::new(Mutex::new(LevelFilter::INFO));
3286        let (_tx, rx) = tokio::sync::mpsc::unbounded_channel();
3287        let (metrics_tx, _metrics_rx) = tokio::sync::mpsc::unbounded_channel();
3288        CodeAnalyzer::new(
3289            peer,
3290            log_level_filter,
3291            rx,
3292            crate::metrics::MetricsSender(metrics_tx),
3293        )
3294    }
3295
3296    #[test]
3297    fn test_summary_cursor_conflict() {
3298        assert!(summary_cursor_conflict(Some(true), Some("cursor")));
3299        assert!(!summary_cursor_conflict(Some(true), None));
3300        assert!(!summary_cursor_conflict(None, Some("x")));
3301        assert!(!summary_cursor_conflict(None, None));
3302    }
3303
3304    #[tokio::test]
3305    async fn test_validate_impl_only_non_rust_returns_invalid_params() {
3306        use tempfile::TempDir;
3307
3308        let dir = TempDir::new().unwrap();
3309        std::fs::write(dir.path().join("main.py"), "def foo(): pass").unwrap();
3310
3311        let analyzer = make_analyzer();
3312        // Call analyze_symbol with impl_only=true on a Python-only directory via the tool API.
3313        // We use handle_focused_mode which calls validate_impl_only internally.
3314        let entries: Vec<traversal::WalkEntry> =
3315            traversal::walk_directory(dir.path(), None).unwrap_or_default();
3316        let result = CodeAnalyzer::validate_impl_only(&entries);
3317        assert!(result.is_err());
3318        let err = result.unwrap_err();
3319        assert_eq!(err.code, rmcp::model::ErrorCode::INVALID_PARAMS);
3320        drop(analyzer); // ensure it compiles with analyzer in scope
3321    }
3322
3323    #[tokio::test]
3324    async fn test_no_cache_meta_on_analyze_directory_result() {
3325        use aptu_coder_core::types::{
3326            AnalyzeDirectoryParams, OutputControlParams, PaginationParams,
3327        };
3328        use tempfile::TempDir;
3329
3330        let dir = TempDir::new().unwrap();
3331        std::fs::write(dir.path().join("main.rs"), "fn main() {}").unwrap();
3332
3333        let analyzer = make_analyzer();
3334        let params: AnalyzeDirectoryParams = serde_json::from_value(serde_json::json!({
3335            "path": dir.path().to_str().unwrap(),
3336        }))
3337        .unwrap();
3338        let ct = tokio_util::sync::CancellationToken::new();
3339        let (arc_output, _cache_hit) = analyzer.handle_overview_mode(&params, ct).await.unwrap();
3340        // Verify the no_cache_meta shape by constructing it directly and checking the shape
3341        let meta = no_cache_meta();
3342        assert_eq!(
3343            meta.0.get("cache_hint").and_then(|v| v.as_str()),
3344            Some("no-cache"),
3345        );
3346        drop(arc_output);
3347    }
3348
3349    #[test]
3350    fn test_complete_path_completions_returns_suggestions() {
3351        // Test the underlying completion function (same code path as complete()) directly
3352        // to avoid needing a constructed RequestContext<RoleServer>.
3353        // CARGO_MANIFEST_DIR is <workspace>/aptu-coder; parent is the workspace root,
3354        // which contains aptu-coder-core/ and aptu-coder/ matching the "aptu-" prefix.
3355        let manifest_dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
3356        let workspace_root = manifest_dir.parent().expect("manifest dir has parent");
3357        let suggestions = completion::path_completions(workspace_root, "aptu-");
3358        assert!(
3359            !suggestions.is_empty(),
3360            "expected completions for prefix 'aptu-' in workspace root"
3361        );
3362    }
3363
3364    #[tokio::test]
3365    async fn test_handle_overview_mode_verbose_no_summary_block() {
3366        use aptu_coder_core::pagination::{PaginationMode, paginate_slice};
3367        use aptu_coder_core::types::{
3368            AnalyzeDirectoryParams, OutputControlParams, PaginationParams,
3369        };
3370        use tempfile::TempDir;
3371
3372        let tmp = TempDir::new().unwrap();
3373        std::fs::write(tmp.path().join("main.rs"), "fn main() {}").unwrap();
3374
3375        let peer = Arc::new(TokioMutex::new(None));
3376        let log_level_filter = Arc::new(Mutex::new(LevelFilter::INFO));
3377        let (_tx, rx) = tokio::sync::mpsc::unbounded_channel();
3378        let (metrics_tx, _metrics_rx) = tokio::sync::mpsc::unbounded_channel();
3379        let analyzer = CodeAnalyzer::new(
3380            peer,
3381            log_level_filter,
3382            rx,
3383            crate::metrics::MetricsSender(metrics_tx),
3384        );
3385
3386        let params: AnalyzeDirectoryParams = serde_json::from_value(serde_json::json!({
3387            "path": tmp.path().to_str().unwrap(),
3388            "verbose": true,
3389        }))
3390        .unwrap();
3391
3392        let ct = tokio_util::sync::CancellationToken::new();
3393        let (output, _cache_hit) = analyzer.handle_overview_mode(&params, ct).await.unwrap();
3394
3395        // Replicate the handler's formatting path (the fix site)
3396        let use_summary = output.formatted.len() > SIZE_LIMIT; // summary=None, force=None, small output
3397        let paginated =
3398            paginate_slice(&output.files, 0, DEFAULT_PAGE_SIZE, PaginationMode::Default).unwrap();
3399        let verbose = true;
3400        let formatted = if !use_summary {
3401            format_structure_paginated(
3402                &paginated.items,
3403                paginated.total,
3404                params.max_depth,
3405                Some(std::path::Path::new(&params.path)),
3406                verbose,
3407            )
3408        } else {
3409            output.formatted.clone()
3410        };
3411
3412        // After the fix: verbose=true must not emit the SUMMARY: block
3413        assert!(
3414            !formatted.contains("SUMMARY:"),
3415            "verbose=true must not emit SUMMARY: block; got: {}",
3416            &formatted[..formatted.len().min(300)]
3417        );
3418        assert!(
3419            formatted.contains("PAGINATED:"),
3420            "verbose=true must emit PAGINATED: header"
3421        );
3422        assert!(
3423            formatted.contains("FILES [LOC, FUNCTIONS, CLASSES]"),
3424            "verbose=true must emit FILES section header"
3425        );
3426    }
3427
3428    // --- cache_hit integration tests ---
3429
3430    #[tokio::test]
3431    async fn test_analyze_directory_cache_hit_metrics() {
3432        use aptu_coder_core::types::{
3433            AnalyzeDirectoryParams, OutputControlParams, PaginationParams,
3434        };
3435        use tempfile::TempDir;
3436
3437        // Arrange: a temp dir with one file
3438        let dir = TempDir::new().unwrap();
3439        std::fs::write(dir.path().join("lib.rs"), "fn foo() {}").unwrap();
3440        let analyzer = make_analyzer();
3441        let params: AnalyzeDirectoryParams = serde_json::from_value(serde_json::json!({
3442            "path": dir.path().to_str().unwrap(),
3443        }))
3444        .unwrap();
3445
3446        // Act: first call (cache miss)
3447        let ct1 = tokio_util::sync::CancellationToken::new();
3448        let (_out1, hit1) = analyzer.handle_overview_mode(&params, ct1).await.unwrap();
3449
3450        // Act: second call (cache hit)
3451        let ct2 = tokio_util::sync::CancellationToken::new();
3452        let (_out2, hit2) = analyzer.handle_overview_mode(&params, ct2).await.unwrap();
3453
3454        // Assert
3455        assert!(!hit1, "first call must be a cache miss");
3456        assert!(hit2, "second call must be a cache hit");
3457    }
3458
3459    #[tokio::test]
3460    async fn test_analyze_module_cache_hit_metrics() {
3461        use std::io::Write as _;
3462        use tempfile::NamedTempFile;
3463
3464        // Arrange: create a temp Rust file; prime the file cache via analyze_file handler
3465        let mut f = NamedTempFile::with_suffix(".rs").unwrap();
3466        writeln!(f, "fn bar() {{}}").unwrap();
3467        let path = f.path().to_str().unwrap().to_string();
3468
3469        let analyzer = make_analyzer();
3470
3471        // Prime the file cache by calling handle_file_details_mode once
3472        let mut file_params = aptu_coder_core::types::AnalyzeFileParams::default();
3473        file_params.path = path.clone();
3474        file_params.ast_recursion_limit = None;
3475        file_params.fields = None;
3476        file_params.pagination.cursor = None;
3477        file_params.pagination.page_size = None;
3478        file_params.output_control.summary = None;
3479        file_params.output_control.force = None;
3480        file_params.output_control.verbose = None;
3481        let (_cached, _) = analyzer
3482            .handle_file_details_mode(&file_params)
3483            .await
3484            .unwrap();
3485
3486        // Act: now call analyze_module; the cache key is mtime-based so same file = hit
3487        let mut module_params = aptu_coder_core::types::AnalyzeModuleParams::default();
3488        module_params.path = path.clone();
3489
3490        // Replicate the cache lookup the handler does (no public method; test via build path)
3491        let module_cache_key = std::fs::metadata(&path).ok().and_then(|meta| {
3492            meta.modified()
3493                .ok()
3494                .map(|mtime| aptu_coder_core::cache::CacheKey {
3495                    path: std::path::PathBuf::from(&path),
3496                    modified: mtime,
3497                    mode: aptu_coder_core::types::AnalysisMode::FileDetails,
3498                })
3499        });
3500        let cache_hit = module_cache_key
3501            .as_ref()
3502            .and_then(|k| analyzer.cache.get(k))
3503            .is_some();
3504
3505        // Assert: the file cache must have been populated by the earlier handle_file_details_mode call
3506        assert!(
3507            cache_hit,
3508            "analyze_module should find the file in the shared file cache"
3509        );
3510        drop(module_params);
3511    }
3512
3513    // --- import_lookup tests ---
3514
3515    #[test]
3516    fn test_analyze_symbol_import_lookup_invalid_params() {
3517        // Arrange: empty symbol with import_lookup=true (violates the guard:
3518        // symbol must hold the module path when import_lookup=true).
3519        // Act: call the validate helper directly (same pattern as validate_impl_only).
3520        let result = CodeAnalyzer::validate_import_lookup(Some(true), "");
3521
3522        // Assert: INVALID_PARAMS is returned.
3523        assert!(
3524            result.is_err(),
3525            "import_lookup=true with empty symbol must return Err"
3526        );
3527        let err = result.unwrap_err();
3528        assert_eq!(
3529            err.code,
3530            rmcp::model::ErrorCode::INVALID_PARAMS,
3531            "expected INVALID_PARAMS; got {:?}",
3532            err.code
3533        );
3534    }
3535
3536    #[tokio::test]
3537    async fn test_analyze_symbol_import_lookup_found() {
3538        use tempfile::TempDir;
3539
3540        // Arrange: a Rust file that imports "std::collections"
3541        let dir = TempDir::new().unwrap();
3542        std::fs::write(
3543            dir.path().join("main.rs"),
3544            "use std::collections::HashMap;\nfn main() {}\n",
3545        )
3546        .unwrap();
3547
3548        let entries = traversal::walk_directory(dir.path(), None).unwrap();
3549
3550        // Act: search for the module "std::collections"
3551        let output =
3552            analyze::analyze_import_lookup(dir.path(), "std::collections", &entries, None).unwrap();
3553
3554        // Assert: one match found
3555        assert!(
3556            output.formatted.contains("MATCHES: 1"),
3557            "expected 1 match; got: {}",
3558            output.formatted
3559        );
3560        assert!(
3561            output.formatted.contains("main.rs"),
3562            "expected main.rs in output; got: {}",
3563            output.formatted
3564        );
3565    }
3566
3567    #[tokio::test]
3568    async fn test_analyze_symbol_import_lookup_empty() {
3569        use tempfile::TempDir;
3570
3571        // Arrange: a Rust file that does NOT import "no_such_module"
3572        let dir = TempDir::new().unwrap();
3573        std::fs::write(dir.path().join("main.rs"), "fn main() {}\n").unwrap();
3574
3575        let entries = traversal::walk_directory(dir.path(), None).unwrap();
3576
3577        // Act
3578        let output =
3579            analyze::analyze_import_lookup(dir.path(), "no_such_module", &entries, None).unwrap();
3580
3581        // Assert: zero matches
3582        assert!(
3583            output.formatted.contains("MATCHES: 0"),
3584            "expected 0 matches; got: {}",
3585            output.formatted
3586        );
3587    }
3588
3589    // --- git_ref tests ---
3590
3591    #[tokio::test]
3592    async fn test_analyze_directory_git_ref_non_git_repo() {
3593        use aptu_coder_core::traversal::changed_files_from_git_ref;
3594        use tempfile::TempDir;
3595
3596        // Arrange: a temp dir that is NOT a git repository
3597        let dir = TempDir::new().unwrap();
3598        std::fs::write(dir.path().join("main.rs"), "fn main() {}").unwrap();
3599
3600        // Act: attempt git_ref resolution in a non-git dir
3601        let result = changed_files_from_git_ref(dir.path(), "HEAD~1");
3602
3603        // Assert: must return a GitError
3604        assert!(result.is_err(), "non-git dir must return an error");
3605        let err_msg = result.unwrap_err().to_string();
3606        assert!(
3607            err_msg.contains("git"),
3608            "error must mention git; got: {err_msg}"
3609        );
3610    }
3611
3612    #[tokio::test]
3613    async fn test_analyze_directory_git_ref_filters_changed_files() {
3614        use aptu_coder_core::traversal::{changed_files_from_git_ref, filter_entries_by_git_ref};
3615        use std::collections::HashSet;
3616        use tempfile::TempDir;
3617
3618        // Arrange: build a set of fake "changed" paths and a walk entry list
3619        let dir = TempDir::new().unwrap();
3620        let changed_file = dir.path().join("changed.rs");
3621        let unchanged_file = dir.path().join("unchanged.rs");
3622        std::fs::write(&changed_file, "fn changed() {}").unwrap();
3623        std::fs::write(&unchanged_file, "fn unchanged() {}").unwrap();
3624
3625        let entries = traversal::walk_directory(dir.path(), None).unwrap();
3626        let total_files = entries.iter().filter(|e| !e.is_dir).count();
3627        assert_eq!(total_files, 2, "sanity: 2 files before filtering");
3628
3629        // Simulate: only changed.rs is in the changed set
3630        let mut changed: HashSet<std::path::PathBuf> = HashSet::new();
3631        changed.insert(changed_file.clone());
3632
3633        // Act: filter entries
3634        let filtered = filter_entries_by_git_ref(entries, &changed, dir.path());
3635        let filtered_files: Vec<_> = filtered.iter().filter(|e| !e.is_dir).collect();
3636
3637        // Assert: only changed.rs remains
3638        assert_eq!(
3639            filtered_files.len(),
3640            1,
3641            "only 1 file must remain after git_ref filter"
3642        );
3643        assert_eq!(
3644            filtered_files[0].path, changed_file,
3645            "the remaining file must be the changed one"
3646        );
3647
3648        // Verify changed_files_from_git_ref is at least callable (tested separately for non-git error)
3649        let _ = changed_files_from_git_ref;
3650    }
3651
3652    #[tokio::test]
3653    async fn test_handle_overview_mode_git_ref_filters_via_handler() {
3654        use aptu_coder_core::types::{
3655            AnalyzeDirectoryParams, OutputControlParams, PaginationParams,
3656        };
3657        use std::process::Command;
3658        use tempfile::TempDir;
3659
3660        // Arrange: create a real git repo with two commits.
3661        let dir = TempDir::new().unwrap();
3662        let repo = dir.path();
3663
3664        // Init repo and configure minimal identity so git commit works.
3665        // Use no-hooks to avoid project-local commit hooks that enforce email allowlists.
3666        let git_no_hook = |repo_path: &std::path::Path, args: &[&str]| {
3667            let mut cmd = std::process::Command::new("git");
3668            cmd.args(["-c", "core.hooksPath=/dev/null"]);
3669            cmd.args(args);
3670            cmd.current_dir(repo_path);
3671            let out = cmd.output().unwrap();
3672            assert!(out.status.success(), "{out:?}");
3673        };
3674        git_no_hook(repo, &["init"]);
3675        git_no_hook(
3676            repo,
3677            &[
3678                "-c",
3679                "user.email=ci@example.com",
3680                "-c",
3681                "user.name=CI",
3682                "commit",
3683                "--allow-empty",
3684                "-m",
3685                "initial",
3686            ],
3687        );
3688
3689        // Commit file_a.rs in the first commit.
3690        std::fs::write(repo.join("file_a.rs"), "fn a() {}").unwrap();
3691        git_no_hook(repo, &["add", "file_a.rs"]);
3692        git_no_hook(
3693            repo,
3694            &[
3695                "-c",
3696                "user.email=ci@example.com",
3697                "-c",
3698                "user.name=CI",
3699                "commit",
3700                "-m",
3701                "add a",
3702            ],
3703        );
3704
3705        // Add file_b.rs in a second commit (this is what HEAD changes relative to HEAD~1).
3706        std::fs::write(repo.join("file_b.rs"), "fn b() {}").unwrap();
3707        git_no_hook(repo, &["add", "file_b.rs"]);
3708        git_no_hook(
3709            repo,
3710            &[
3711                "-c",
3712                "user.email=ci@example.com",
3713                "-c",
3714                "user.name=CI",
3715                "commit",
3716                "-m",
3717                "add b",
3718            ],
3719        );
3720
3721        // Act: call handle_overview_mode with git_ref=HEAD~1.
3722        // `git diff --name-only HEAD~1` compares working tree against HEAD~1, returning
3723        // only file_b.rs (added in the last commit, so present in working tree but not in HEAD~1).
3724        // Use the canonical path so walk entries match what `git rev-parse --show-toplevel` returns
3725        // (macOS /tmp is a symlink to /private/tmp; without canonicalization paths would differ).
3726        let canon_repo = std::fs::canonicalize(repo).unwrap();
3727        let analyzer = make_analyzer();
3728        let params: AnalyzeDirectoryParams = serde_json::from_value(serde_json::json!({
3729            "path": canon_repo.to_str().unwrap(),
3730            "git_ref": "HEAD~1",
3731        }))
3732        .unwrap();
3733        let ct = tokio_util::sync::CancellationToken::new();
3734        let (arc_output, _cache_hit) = analyzer
3735            .handle_overview_mode(&params, ct)
3736            .await
3737            .expect("handle_overview_mode with git_ref must succeed");
3738
3739        // Assert: only file_b.rs (changed since HEAD~1) appears; file_a.rs must be absent.
3740        let formatted = &arc_output.formatted;
3741        assert!(
3742            formatted.contains("file_b.rs"),
3743            "git_ref=HEAD~1 output must include file_b.rs; got:\n{formatted}"
3744        );
3745        assert!(
3746            !formatted.contains("file_a.rs"),
3747            "git_ref=HEAD~1 output must exclude file_a.rs; got:\n{formatted}"
3748        );
3749    }
3750
3751    #[test]
3752    fn test_validate_path_rejects_absolute_path_outside_cwd() {
3753        // S4: Verify that absolute paths outside the current working directory are rejected.
3754        // This test directly calls validate_path with /etc/passwd, which should fail.
3755        let result = validate_path("/etc/passwd", true);
3756        assert!(
3757            result.is_err(),
3758            "validate_path should reject /etc/passwd (outside CWD)"
3759        );
3760        let err = result.unwrap_err();
3761        let err_msg = err.message.to_lowercase();
3762        assert!(
3763            err_msg.contains("outside") || err_msg.contains("not found"),
3764            "Error message should mention 'outside' or 'not found': {}",
3765            err.message
3766        );
3767    }
3768
3769    #[test]
3770    fn test_validate_path_accepts_relative_path_in_cwd() {
3771        // Happy path: relative path within CWD should be accepted.
3772        // Use Cargo.toml which exists in the crate root.
3773        let result = validate_path("Cargo.toml", true);
3774        assert!(
3775            result.is_ok(),
3776            "validate_path should accept Cargo.toml (exists in CWD)"
3777        );
3778    }
3779
3780    #[test]
3781    fn test_validate_path_creates_parent_for_nonexistent_file() {
3782        // Edge case: non-existent file with non-existent parent should still be accepted
3783        // if the ancestor chain leads back to CWD.
3784        let result = validate_path("nonexistent_dir/nonexistent_file.txt", false);
3785        assert!(
3786            result.is_ok(),
3787            "validate_path should accept non-existent file with non-existent parent (require_exists=false)"
3788        );
3789        let path = result.unwrap();
3790        let cwd = std::env::current_dir().expect("should get cwd");
3791        let canonical_cwd = std::fs::canonicalize(&cwd).unwrap_or(cwd);
3792        assert!(
3793            path.starts_with(&canonical_cwd),
3794            "Resolved path should be within CWD: {:?} should start with {:?}",
3795            path,
3796            canonical_cwd
3797        );
3798    }
3799
3800    #[test]
3801    fn test_edit_overwrite_with_working_dir() {
3802        // Arrange: create a temporary directory within CWD to use as working_dir
3803        let cwd = std::env::current_dir().expect("should get cwd");
3804        let temp_dir = tempfile::TempDir::new_in(&cwd).expect("should create temp dir in cwd");
3805        let temp_path = temp_dir.path();
3806
3807        // Act: call validate_path_in_dir with a relative path
3808        let result = validate_path_in_dir("test_file.txt", false, temp_path);
3809
3810        // Assert: path should be resolved relative to working_dir
3811        assert!(
3812            result.is_ok(),
3813            "validate_path_in_dir should accept relative path in valid working_dir: {:?}",
3814            result.err()
3815        );
3816        let resolved = result.unwrap();
3817        assert!(
3818            resolved.starts_with(temp_path),
3819            "Resolved path should be within working_dir: {:?} should start with {:?}",
3820            resolved,
3821            temp_path
3822        );
3823    }
3824
3825    #[test]
3826    fn test_edit_overwrite_working_dir_traversal() {
3827        // Arrange: create a temporary directory within CWD to use as working_dir
3828        let cwd = std::env::current_dir().expect("should get cwd");
3829        let temp_dir = tempfile::TempDir::new_in(&cwd).expect("should create temp dir in cwd");
3830        let temp_path = temp_dir.path();
3831
3832        // Act: try to traverse outside working_dir with ../../../etc/passwd
3833        let result = validate_path_in_dir("../../../etc/passwd", false, temp_path);
3834
3835        // Assert: should reject path traversal attack
3836        assert!(
3837            result.is_err(),
3838            "validate_path_in_dir should reject path traversal outside working_dir"
3839        );
3840        let err = result.unwrap_err();
3841        let err_msg = err.message.to_lowercase();
3842        assert!(
3843            err_msg.contains("outside") || err_msg.contains("working"),
3844            "Error message should mention 'outside' or 'working': {}",
3845            err.message
3846        );
3847    }
3848
3849    #[test]
3850    fn test_edit_replace_with_working_dir() {
3851        // Arrange: create a temporary directory within CWD and file
3852        let cwd = std::env::current_dir().expect("should get cwd");
3853        let temp_dir = tempfile::TempDir::new_in(&cwd).expect("should create temp dir in cwd");
3854        let temp_path = temp_dir.path();
3855        let file_path = temp_path.join("test.txt");
3856        std::fs::write(&file_path, "hello world").expect("should write test file");
3857
3858        // Act: call validate_path_in_dir with require_exists=true
3859        let result = validate_path_in_dir("test.txt", true, temp_path);
3860
3861        // Assert: should find the file relative to working_dir
3862        assert!(
3863            result.is_ok(),
3864            "validate_path_in_dir should find existing file in working_dir: {:?}",
3865            result.err()
3866        );
3867        let resolved = result.unwrap();
3868        assert_eq!(
3869            resolved, file_path,
3870            "Resolved path should match the actual file path"
3871        );
3872    }
3873
3874    #[test]
3875    fn test_edit_overwrite_no_working_dir() {
3876        // Arrange: use validate_path without working_dir (existing behavior)
3877        // Use Cargo.toml which exists in the crate root
3878
3879        // Act: call validate_path with require_exists=true
3880        let result = validate_path("Cargo.toml", true);
3881
3882        // Assert: should work as before
3883        assert!(
3884            result.is_ok(),
3885            "validate_path should still work without working_dir"
3886        );
3887    }
3888
3889    #[test]
3890    fn test_edit_overwrite_working_dir_is_file() {
3891        // Arrange: create a temporary file (not directory) to use as working_dir
3892        let cwd = std::env::current_dir().expect("should get cwd");
3893        let temp_dir = tempfile::TempDir::new_in(&cwd).expect("should create temp dir in cwd");
3894        let temp_file = temp_dir.path().join("test_file.txt");
3895        std::fs::write(&temp_file, "test content").expect("should write test file");
3896
3897        // Act: call validate_path_in_dir with a file as working_dir
3898        let result = validate_path_in_dir("some_file.txt", false, &temp_file);
3899
3900        // Assert: should reject because working_dir is not a directory
3901        assert!(
3902            result.is_err(),
3903            "validate_path_in_dir should reject a file as working_dir"
3904        );
3905        let err = result.unwrap_err();
3906        let err_msg = err.message.to_lowercase();
3907        assert!(
3908            err_msg.contains("directory"),
3909            "Error message should mention 'directory': {}",
3910            err.message
3911        );
3912    }
3913
3914    #[test]
3915    fn test_tool_annotations() {
3916        // Arrange: get tool list via static method
3917        let tools = CodeAnalyzer::list_tools();
3918
3919        // Act: find specific tools by name
3920        let analyze_directory = tools.iter().find(|t| t.name == "analyze_directory");
3921        let exec_command = tools.iter().find(|t| t.name == "exec_command");
3922
3923        // Assert: analyze_directory has correct annotations
3924        let analyze_dir_tool = analyze_directory.expect("analyze_directory tool should exist");
3925        let analyze_dir_annot = analyze_dir_tool
3926            .annotations
3927            .as_ref()
3928            .expect("analyze_directory should have annotations");
3929        assert_eq!(
3930            analyze_dir_annot.read_only_hint,
3931            Some(true),
3932            "analyze_directory read_only_hint should be true"
3933        );
3934        assert_eq!(
3935            analyze_dir_annot.destructive_hint,
3936            Some(false),
3937            "analyze_directory destructive_hint should be false"
3938        );
3939
3940        // Assert: exec_command has correct annotations
3941        let exec_cmd_tool = exec_command.expect("exec_command tool should exist");
3942        let exec_cmd_annot = exec_cmd_tool
3943            .annotations
3944            .as_ref()
3945            .expect("exec_command should have annotations");
3946        assert_eq!(
3947            exec_cmd_annot.open_world_hint,
3948            Some(true),
3949            "exec_command open_world_hint should be true"
3950        );
3951    }
3952
3953    #[test]
3954    fn test_exec_stdin_size_cap_validation() {
3955        // Test: stdin size cap check (1 MB limit)
3956        // Arrange: create oversized stdin
3957        let oversized_stdin = "x".repeat(STDIN_MAX_BYTES + 1);
3958
3959        // Act & Assert: verify size exceeds limit
3960        assert!(
3961            oversized_stdin.len() > STDIN_MAX_BYTES,
3962            "test setup: oversized stdin should exceed 1 MB"
3963        );
3964
3965        // Verify that a 1 MB stdin is accepted
3966        let max_stdin = "y".repeat(STDIN_MAX_BYTES);
3967        assert_eq!(
3968            max_stdin.len(),
3969            STDIN_MAX_BYTES,
3970            "test setup: max stdin should be exactly 1 MB"
3971        );
3972    }
3973
3974    #[tokio::test]
3975    async fn test_exec_stdin_cat_roundtrip() {
3976        // Test: stdin content is piped to process and readable via stdout
3977        // Arrange: prepare stdin content
3978        let stdin_content = "hello world";
3979
3980        // Act: execute cat with stdin via shell
3981        let mut child = tokio::process::Command::new("sh")
3982            .arg("-c")
3983            .arg("cat")
3984            .stdin(std::process::Stdio::piped())
3985            .stdout(std::process::Stdio::piped())
3986            .stderr(std::process::Stdio::piped())
3987            .spawn()
3988            .expect("spawn cat");
3989
3990        if let Some(mut stdin_handle) = child.stdin.take() {
3991            use tokio::io::AsyncWriteExt as _;
3992            stdin_handle
3993                .write_all(stdin_content.as_bytes())
3994                .await
3995                .expect("write stdin");
3996            drop(stdin_handle);
3997        }
3998
3999        let output = child.wait_with_output().await.expect("wait for cat");
4000
4001        // Assert: stdout contains the piped stdin content
4002        let stdout_str = String::from_utf8_lossy(&output.stdout);
4003        assert!(
4004            stdout_str.contains(stdin_content),
4005            "stdout should contain stdin content: {}",
4006            stdout_str
4007        );
4008    }
4009
4010    #[tokio::test]
4011    async fn test_exec_stdin_none_no_regression() {
4012        // Test: command without stdin executes normally (no regression)
4013        // Act: execute echo without stdin
4014        let child = tokio::process::Command::new("sh")
4015            .arg("-c")
4016            .arg("echo hi")
4017            .stdin(std::process::Stdio::null())
4018            .stdout(std::process::Stdio::piped())
4019            .stderr(std::process::Stdio::piped())
4020            .spawn()
4021            .expect("spawn echo");
4022
4023        let output = child.wait_with_output().await.expect("wait for echo");
4024
4025        // Assert: command executes successfully
4026        let stdout_str = String::from_utf8_lossy(&output.stdout);
4027        assert!(
4028            stdout_str.contains("hi"),
4029            "stdout should contain echo output: {}",
4030            stdout_str
4031        );
4032    }
4033
4034    #[test]
4035    fn test_validate_path_in_dir_rejects_sibling_prefix() {
4036        // Arrange: create a parent temp dir, then two subdirs:
4037        //   allowed/   -- the working_dir
4038        //   allowed_sibling/  -- a sibling whose name shares the prefix
4039        // This mirrors CVE-2025-53110: "/work_evil" must not match "/work".
4040        let cwd = std::env::current_dir().expect("should get cwd");
4041        let parent = tempfile::TempDir::new_in(&cwd).expect("should create parent temp dir");
4042        let allowed = parent.path().join("allowed");
4043        let sibling = parent.path().join("allowed_sibling");
4044        std::fs::create_dir_all(&allowed).expect("should create allowed dir");
4045        std::fs::create_dir_all(&sibling).expect("should create sibling dir");
4046
4047        // Act: ask for a file inside the sibling dir, using a path that
4048        // traverses from allowed/ into allowed_sibling/
4049        let result = validate_path_in_dir("../allowed_sibling/secret.txt", false, &allowed);
4050
4051        // Assert: must be rejected even though "allowed_sibling" starts with "allowed"
4052        assert!(
4053            result.is_err(),
4054            "validate_path_in_dir must reject a path resolving to a sibling directory \
4055             sharing the working_dir name prefix (CVE-2025-53110 pattern)"
4056        );
4057        let err = result.unwrap_err();
4058        let msg = err.message.to_lowercase();
4059        assert!(
4060            msg.contains("outside") || msg.contains("working"),
4061            "Error should mention 'outside' or 'working', got: {}",
4062            err.message
4063        );
4064    }
4065}