sqry-mcp 8.0.7

MCP server for sqry semantic code search
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! Workspace path resolution for MCP tools
//!
//! Provides 4-tier resolution strategy:
//! 1. Explicit `path` parameter (highest priority)
//! 2. `SQRY_MCP_WORKSPACE_ROOT` environment variable (primary security boundary)
//! 3. `SQRY_WORKSPACE_ROOT` environment variable (legacy fallback)
//! 4. Upward directory discovery from CWD (fallback)
//!
//! Discovery results are cached in an LRU cache with platform-specific
//! path normalization for case-insensitive filesystems.

use anyhow::{Context, Result, bail};
use sqry_core::config::WorkspaceConfig;
use std::collections::HashSet;
use std::env;
use std::path::{Path, PathBuf};

//=============================================================================
// Discovery Cache
//=============================================================================

/// Global discovery cache mapping normalized path strings to canonical workspace paths.
///
/// Cache keys are normalized based on platform:
/// - Windows: lowercase + backslash separators
/// - macOS: Runtime detection via pathconf (case-sensitive vs case-insensitive)
/// - Unix: unchanged (case-sensitive)
///
/// Uses `Mutex<Option<...>>` so tests can reset state between runs.
/// Must be initialized via `init_discovery_cache()` before first use
/// (typically during server startup).
static DISCOVERY_CACHE: parking_lot::Mutex<Option<lru::LruCache<String, PathBuf>>> =
    parking_lot::Mutex::new(None);

/// Initialize the discovery cache with the specified capacity.
///
/// This function must be called during server initialization before any
/// cache access. Subsequent calls are no-ops (idempotent).
///
/// # Panics
///
/// Panics if capacity is zero (prevented by `NonZeroUsize` type).
pub fn init_discovery_cache(capacity: std::num::NonZeroUsize) {
    let mut cache = DISCOVERY_CACHE.lock();
    if cache.is_none() {
        tracing::info!(capacity = capacity.get(), "Initializing discovery cache");
        *cache = Some(lru::LruCache::new(capacity));
    }
}

/// Normalize a path string for use as a cache key.
///
/// Normalization strategy is platform-specific:
/// - **Windows**: Lowercase + convert forward slashes to backslashes
/// - **macOS**: Runtime detection via `pathconf(_PC_CASE_SENSITIVE)`
///   - Case-insensitive filesystems: lowercase
///   - Case-sensitive filesystems: unchanged
///   - Error/indeterminate: assume case-sensitive (safer default)
/// - **Unix**: Unchanged (case-sensitive)
///
/// # Returns
///
/// A normalized string suitable for hash-based lookup on the current platform.
///
/// **Note**: Not yet integrated into tool implementations. Tool files still use local
/// `resolve_workspace_path()` functions. Will be used after tool refactoring.
#[allow(dead_code)]
fn normalize_discovery_key(path: &str) -> String {
    #[cfg(windows)]
    {
        path.to_lowercase().replace('/', "\\")
    }

    #[cfg(target_os = "macos")]
    {
        // Runtime detection via pathconf
        match is_case_sensitive_macos(path) {
            Ok(false) => path.to_lowercase(), // Case-insensitive filesystem
            Ok(true) | Err(_) => path.to_string(), // Case-sensitive or error (preserve case)
        }
    }

    #[cfg(not(any(windows, target_os = "macos")))]
    {
        path.to_string() // Case-sensitive Unix
    }
}

/// macOS: Check if a path is on a case-sensitive filesystem via pathconf.
///
/// Uses `pathconf(_PC_CASE_SENSITIVE)` to query filesystem properties.
/// If the path doesn't exist, probes the parent directory.
///
/// # Returns
///
/// - `Ok(true)` if filesystem is case-sensitive
/// - `Ok(false)` if filesystem is case-insensitive (HFS+, APFS non-sensitive)
/// - `Err(_)` on pathconf error
///
/// # Error Handling
///
/// - `errno=0, result=-1`: Indeterminate → return `Ok(true)` (conservative)
/// - `errno!=0`: Actual error → return `Err`
#[cfg(target_os = "macos")]
fn is_case_sensitive_macos(path: &str) -> Result<bool> {
    use std::ffi::CString;

    // Probe the path or its parent if path doesn't exist
    let probe_path = if Path::new(path).exists() {
        path.to_string()
    } else {
        Path::new(path)
            .parent()
            .and_then(|p| p.to_str())
            .unwrap_or("/") // Fallback to root if no parent
            .to_string()
    };

    let c_path = CString::new(probe_path.as_bytes())?;

    // SAFETY: pathconf is a standard POSIX function, c_path is a valid C string
    let result = unsafe { libc::pathconf(c_path.as_ptr(), libc::_PC_CASE_SENSITIVE) };

    match result {
        -1 => {
            // Check errno to distinguish error from indeterminate
            let errno = std::io::Error::last_os_error().raw_os_error().unwrap_or(0);
            if errno == 0 {
                // Indeterminate (pathconf returned -1, errno=0)
                // Conservative fallback: assume case-sensitive
                tracing::debug!(path, "pathconf indeterminate, assuming case-sensitive");
                Ok(true)
            } else {
                // Actual error
                bail!(
                    "pathconf(_PC_CASE_SENSITIVE) failed for {}: {}",
                    probe_path,
                    std::io::Error::last_os_error()
                )
            }
        }
        0 => Ok(false), // Case-insensitive
        _ => Ok(true),  // Case-sensitive
    }
}

/// Resolve a workspace path with caching.
///
/// This is the public API for path resolution with discovery caching.
/// Use this instead of `WorkspaceResolver::new(...).resolve()` when caching
/// is desired.
///
/// # Cache Behavior
///
/// - **Cache hit**: Return cached canonical path
/// - **Cache miss**: Resolve via `WorkspaceResolver`, cache result
///
/// # Errors
///
/// Returns an error if:
/// - Cache not initialized (call `init_discovery_cache()` first)
/// - Workspace resolution fails (no .sqry/graph found)
/// - Path canonicalization fails
///
/// **Note**: Used by `engine_for_workspace()` to provide cached workspace discovery.
/// Local tool `resolve_workspace_path()` helpers remain for backwards compatibility.
pub fn resolve_workspace_path(explicit_path: &str) -> Result<PathBuf> {
    // Normalize path for cache lookup
    let key = normalize_discovery_key(explicit_path);

    // Check cache (short lock scope)
    {
        let mut cache = DISCOVERY_CACHE.lock();
        let lru = cache
            .as_mut()
            .context("Discovery cache not initialized - call init_discovery_cache() first")?;
        if let Some(cached) = lru.get(&key) {
            tracing::debug!(
                path = explicit_path,
                cached = %cached.display(),
                "Discovery cache hit"
            );
            return Ok(cached.clone());
        }
    }

    // Cache miss - resolve via WorkspaceResolver (outside lock)
    tracing::debug!(path = explicit_path, "Discovery cache miss, resolving");

    let path_buf = PathBuf::from(explicit_path);
    let resolver = WorkspaceResolver::new(Some(path_buf));
    let resolved_path = resolver.resolve()?;

    // Insert into cache (short lock scope)
    {
        let mut cache = DISCOVERY_CACHE.lock();
        if let Some(lru) = cache.as_mut() {
            lru.put(key, resolved_path.clone());
            tracing::debug!(
                path = explicit_path,
                resolved = %resolved_path.display(),
                cache_size = lru.len(),
                "Discovery result cached"
            );
        }
    }

    Ok(resolved_path)
}

/// Resolves workspace root using 3-tier strategy
pub struct WorkspaceResolver {
    explicit_root: Option<PathBuf>,
}

impl WorkspaceResolver {
    /// Create a new resolver with optional explicit root
    pub fn new(explicit_root: Option<PathBuf>) -> Self {
        Self { explicit_root }
    }

    /// Resolve workspace root using priority order:
    /// 1. Explicit parameter
    /// 2. `SQRY_MCP_WORKSPACE_ROOT` env var (primary security boundary)
    /// 3. `SQRY_WORKSPACE_ROOT` env var (backward compatibility)
    /// 4. Discovery from CWD
    pub fn resolve(&self) -> Result<PathBuf> {
        // Priority 1: Explicit workspace_root parameter
        if let Some(root) = &self.explicit_root {
            tracing::info!("Using explicit workspace_root: {:?}", root);
            return self.validate_and_canonicalize(root);
        }

        // Priority 2: SQRY_MCP_WORKSPACE_ROOT environment variable (primary)
        if let Ok(root) = env::var("SQRY_MCP_WORKSPACE_ROOT") {
            tracing::info!("Using SQRY_MCP_WORKSPACE_ROOT env var: {}", root);
            let path = PathBuf::from(root);
            return self.validate_and_canonicalize(&path);
        }

        // Priority 3: SQRY_WORKSPACE_ROOT environment variable (backward compatibility)
        if let Ok(root) = env::var("SQRY_WORKSPACE_ROOT") {
            tracing::info!("Using SQRY_WORKSPACE_ROOT env var (legacy): {}", root);
            let path = PathBuf::from(root);
            return self.validate_and_canonicalize(&path);
        }

        // Priority 4: Discovery fallback
        tracing::info!("Using workspace discovery from CWD");
        let cwd = env::current_dir()?;
        self.discover_workspace(&cwd)
    }

    fn validate_and_canonicalize(&self, root: &Path) -> Result<PathBuf> {
        let canonical = root.canonicalize()?;
        self.validate_workspace(&canonical)?;
        Ok(canonical)
    }

    #[allow(clippy::unused_self)] // May use self in future for config/caching
    fn validate_workspace(&self, root: &Path) -> Result<()> {
        if !root.is_dir() {
            bail!(
                "Not a valid sqry workspace: {} (not a directory)",
                root.display()
            );
        }
        Ok(())
    }

    #[allow(clippy::unused_self)] // May use self in future for config/caching
    fn discover_workspace(&self, start: &Path) -> Result<PathBuf> {
        let config = WorkspaceConfig::load_or_default()?;
        let max_depth = config.effective_discovery_depth()?;

        let mut visited = HashSet::new();
        let mut current = start.canonicalize()?;

        for _ in 0..max_depth {
            // Symlink loop detection
            if !visited.insert(current.clone()) {
                bail!("Symlink loop detected at {}", current.display());
            }

            // Check for .sqry/graph
            if current.join(".sqry/graph").exists() {
                return Ok(current);
            }

            // Move to parent
            current = match current.parent() {
                Some(p) => p.canonicalize()?,
                None => bail!("No .sqry workspace found in parent directories"),
            };
        }

        bail!("Workspace discovery exceeded depth limit ({max_depth})")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    #[test]
    fn test_explicit_parameter_priority() {
        let temp = TempDir::new().unwrap();
        let workspace = temp.path();
        fs::create_dir_all(workspace.join(".sqry/graph")).unwrap();

        let resolver = WorkspaceResolver::new(Some(workspace.to_path_buf()));
        let result = resolver.resolve();
        assert!(result.is_ok());
    }

    #[test]
    fn test_directory_without_graph_accepted() {
        let temp = TempDir::new().unwrap();
        let workspace = temp.path();
        // Don't create .sqry/graph — should still be accepted as workspace

        let resolver = WorkspaceResolver::new(Some(workspace.to_path_buf()));
        let result = resolver.resolve();
        assert!(
            result.is_ok(),
            "Directory without .sqry/graph should be accepted"
        );
    }

    #[test]
    #[serial_test::serial(workspace_env)]
    fn test_discovery_from_subdirectory() {
        let temp = TempDir::new().unwrap();
        let workspace = temp.path();
        let subdir = workspace.join("src/deep/nested");
        fs::create_dir_all(workspace.join(".sqry/graph")).unwrap();
        fs::create_dir_all(&subdir).unwrap();

        let old_cwd = env::current_dir().unwrap();
        env::set_current_dir(&subdir).unwrap();

        let resolver = WorkspaceResolver::new(None);
        let result = resolver.resolve();

        env::set_current_dir(old_cwd).unwrap();

        assert!(result.is_ok());
        assert_eq!(
            result.unwrap().canonicalize().unwrap(),
            workspace.canonicalize().unwrap()
        );
    }

    #[test]
    #[serial_test::serial(workspace_env)]
    #[ignore = "Symlink loop behavior is platform-dependent and may resolve differently"]
    fn test_symlink_loop_detected() {
        // Symlink loop detection test
        // Skip on platforms without symlink support
        #[cfg(unix)]
        {
            use std::os::unix::fs::symlink;

            let temp = TempDir::new().unwrap();
            let dir_a = temp.path().join("a");
            let dir_b = temp.path().join("b");

            fs::create_dir(&dir_a).unwrap();
            fs::create_dir(&dir_b).unwrap();

            // Create circular symlinks: a/next -> b, b/next -> a
            symlink(&dir_b, dir_a.join("next")).unwrap();
            symlink(&dir_a, dir_b.join("next")).unwrap();

            let old_cwd = env::current_dir().unwrap();
            env::set_current_dir(&dir_a).unwrap();

            let resolver = WorkspaceResolver::new(None);
            let result = resolver.resolve();

            env::set_current_dir(old_cwd).unwrap();

            assert!(result.is_err());
            let err_msg = result.unwrap_err().to_string();
            assert!(
                err_msg.contains("Symlink loop") || err_msg.contains("No .sqry workspace found")
            );
        }
    }

    #[test]
    #[serial_test::serial(workspace_env)]
    fn test_depth_limit_enforced() {
        // Create a deep directory structure without .sqry.
        // Use short segment names ("d0".."dN") to stay within Windows MAX_PATH (260 chars).
        // 30 levels of "dN/" is ~90 chars of segments, well within limits even with temp prefix.
        let temp = TempDir::new().unwrap();
        let mut deep_path = temp.path().to_path_buf();

        for i in 0..30 {
            deep_path = deep_path.join(format!("d{i}"));
        }
        fs::create_dir_all(&deep_path).unwrap();

        let old_cwd = env::current_dir().unwrap();
        env::set_current_dir(&deep_path).unwrap();

        let resolver = WorkspaceResolver::new(None);
        let result = resolver.resolve();

        env::set_current_dir(old_cwd).unwrap();

        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(err_msg.contains("depth limit") || err_msg.contains("No .sqry workspace found"));
    }

    #[test]
    #[serial_test::serial(workspace_env)]
    fn test_env_var_resolution() {
        let temp = TempDir::new().unwrap();
        let workspace = temp.path();
        fs::create_dir_all(workspace.join(".sqry/graph")).unwrap();

        unsafe {
            env::set_var("SQRY_WORKSPACE_ROOT", workspace);
        }
        let resolver = WorkspaceResolver::new(None);
        let result = resolver.resolve();
        unsafe {
            env::remove_var("SQRY_WORKSPACE_ROOT");
        }

        assert!(result.is_ok());
        assert_eq!(
            result.unwrap().canonicalize().unwrap(),
            workspace.canonicalize().unwrap()
        );
    }
}

#[cfg(test)]
mod discovery_cache_tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    /// Reset the discovery cache to uninitialized state for test isolation.
    fn reset_discovery_cache() {
        let mut cache = DISCOVERY_CACHE.lock();
        *cache = None;
    }

    #[test]
    #[serial_test::serial(discovery_cache)]
    fn test_discovery_cache_requires_initialization() {
        reset_discovery_cache();

        // Attempt to resolve before initialization
        let result = resolve_workspace_path("/tmp/test");

        // Should fail with "not initialized" error
        match result {
            Err(e) => assert!(e.to_string().contains("not initialized")),
            Ok(_) => panic!("Expected error, got success"),
        }
    }

    #[test]
    #[serial_test::serial(discovery_cache)]
    fn test_discovery_cache_normalization() {
        // Initialize cache for this test
        init_discovery_cache(std::num::NonZeroUsize::new(100).unwrap());

        let temp = TempDir::new().unwrap();
        let workspace = temp.path();
        fs::create_dir_all(workspace.join(".sqry/graph")).unwrap();

        let path_str = workspace.to_str().unwrap();

        // First resolution should cache
        let result1 = resolve_workspace_path(path_str);
        assert!(result1.is_ok());

        // Second resolution should hit cache
        let result2 = resolve_workspace_path(path_str);
        assert!(result2.is_ok());

        assert_eq!(
            result1.unwrap().canonicalize().unwrap(),
            result2.unwrap().canonicalize().unwrap()
        );
    }

    #[test]
    #[cfg(target_os = "macos")]
    fn test_macos_pathconf_error_handling() {
        // Test pathconf with invalid path
        let result = is_case_sensitive_macos("/nonexistent/path/that/does/not/exist/very/deep");

        // Should either succeed (probed parent) or fail gracefully
        match result {
            Ok(is_sensitive) => {
                // Should assume case-sensitive on error/indeterminate
                assert!(is_sensitive);
            }
            Err(_) => {
                // Error is acceptable if entire path chain is invalid
            }
        }
    }

    #[test]
    #[cfg(target_os = "macos")]
    fn test_macos_pathconf_root() {
        // Test pathconf on root directory (always exists)
        let result = is_case_sensitive_macos("/");

        // Should succeed for root
        assert!(result.is_ok());
    }

    #[test]
    fn test_normalize_discovery_key_idempotent() {
        let path = "/tmp/test";
        let key1 = normalize_discovery_key(path);
        let key2 = normalize_discovery_key(&key1);

        // Normalization should be idempotent
        #[cfg(not(windows))]
        assert_eq!(key1, key2);

        #[cfg(windows)]
        {
            // Windows normalization changes both case and separators
            // Second normalization on already-normalized key should be unchanged
            assert_eq!(key1.to_lowercase(), key2);
        }
    }
}