pathfinder-mcp-common 0.5.2

Shared types, errors, and infrastructure for Pathfinder MCP server
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
//! Shared domain types for Pathfinder.
//!
//! These types are used across all crates to ensure consistent
//! representation of semantic paths, version hashes, and filter modes.

use crate::error::PathfinderError;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::path::{Path, PathBuf};

/// A parsed semantic path in the format `file_path[::symbol_chain]`.
///
/// The semantic path is the unified addressing scheme used by all
/// symbol-level tools. See PRD §1.3 for the full grammar:
///
/// ```ebnf
/// semantic_path   = file_path ["::"] symbol_chain]
/// file_path       = relative_path
/// symbol_chain    = symbol ("." symbol)*
/// symbol          = identifier [overload_suffix]
/// overload_suffix = "#" digit+
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SemanticPath {
    /// Relative file path within the workspace.
    pub file_path: PathBuf,
    /// Optional symbol chain (e.g., `AuthService.login`).
    pub symbol_chain: Option<SymbolChain>,
}

impl SemanticPath {
    /// Parse a semantic path string.
    ///
    /// # Examples
    /// - `"src/auth.ts::AuthService.login"` → file + symbol chain
    /// - `"src/utils.ts"` → bare file path (no symbol)
    /// - `"src/auth.ts::AuthService.login#2"` → overloaded method
    #[must_use]
    pub fn parse(input: &str) -> Option<Self> {
        if input.is_empty() {
            return None;
        }

        if let Some((file_part, symbol_part)) = input.split_once("::") {
            if file_part.is_empty() {
                return None;
            }
            let symbol_chain = SymbolChain::parse(symbol_part)?;
            Some(Self {
                file_path: PathBuf::from(file_part),
                symbol_chain: Some(symbol_chain),
            })
        } else {
            Some(Self {
                file_path: PathBuf::from(input),
                symbol_chain: None,
            })
        }
    }

    /// Returns `true` if this is a bare file path (no `::` symbol chain).
    #[must_use]
    pub const fn is_bare_file(&self) -> bool {
        self.symbol_chain.is_none()
    }
}

impl fmt::Display for SemanticPath {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.file_path.display())?;
        if let Some(chain) = &self.symbol_chain {
            write!(f, "::{chain}")?;
        }
        Ok(())
    }
}

/// A chain of symbols separated by dots.
///
/// Example: `AuthService.login` → `["AuthService", "login"]`
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SymbolChain {
    /// The segments that make up the symbol chain.
    pub segments: Vec<Symbol>,
}

impl SymbolChain {
    /// Parse a symbol chain from the part after `::`.
    #[must_use]
    pub fn parse(input: &str) -> Option<Self> {
        if input.is_empty() {
            return None;
        }

        let segments: Vec<Symbol> = input.split('.').filter_map(Symbol::parse).collect();

        if segments.is_empty() {
            return None;
        }

        Some(Self { segments })
    }
}

impl fmt::Display for SymbolChain {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let parts: Vec<String> = self.segments.iter().map(ToString::to_string).collect();
        write!(f, "{}", parts.join("."))
    }
}

/// A single symbol, optionally with an overload suffix.
///
/// Example: `login` or `login#2`
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Symbol {
    /// The name of the symbol.
    pub name: String,
    /// The optional overload index of the symbol.
    pub overload_index: Option<u32>,
}

impl Symbol {
    /// Parse a single symbol segment.
    #[must_use]
    pub fn parse(input: &str) -> Option<Self> {
        if input.is_empty() {
            return None;
        }

        if let Some((name, suffix)) = input.split_once('#') {
            let index = suffix.parse::<u32>().ok()?;
            Some(Self {
                name: name.to_owned(),
                overload_index: Some(index),
            })
        } else {
            Some(Self {
                name: input.to_owned(),
                overload_index: None,
            })
        }
    }
}

impl fmt::Display for Symbol {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.name)?;
        if let Some(idx) = self.overload_index {
            write!(f, "#{idx}")?;
        }
        Ok(())
    }
}

/// A SHA-256 version hash of file content, used as a content fingerprint to detect changes.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct VersionHash(String);

impl VersionHash {
    /// Internal prefix stored in every hash value.
    const PREFIX: &'static str = "sha256:";
    /// Minimum number of hex chars accepted as a valid short hash.
    const MIN_HEX_CHARS: usize = 7;

    /// Compute the SHA-256 hash of file content.
    #[must_use]
    pub fn compute(content: &[u8]) -> Self {
        use sha2::{Digest, Sha256};
        let hash = Sha256::digest(content);
        Self(format!("sha256:{hash:x}"))
    }

    /// Create from a raw hash string (for deserialization from client input).
    #[must_use]
    pub const fn from_raw(hash: String) -> Self {
        Self(hash)
    }

    /// Get the full internal hash string (`sha256:<64 hex chars>`).
    ///
    /// Use [`short`] for agent-facing responses; use this only for
    /// diagnostic messages and error context where precision is needed.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Compact 7-hex-char hash for agent-facing responses.
    ///
    /// Omits the `sha256:` prefix — the field name `version_hash` already
    /// communicates the purpose. This cuts per-hash token cost from 71 to 7
    /// characters, reducing agent context window pressure across multi-file
    /// editing sessions.
    ///
    /// # Example
    /// ```
    /// # use pathfinder_common::types::VersionHash;
    /// let h = VersionHash::compute(b"hello");
    /// assert_eq!(h.short().len(), 7);
    /// // short() is the first 7 hex chars of the full hash (no prefix)
    /// assert!(h.as_str()[7..].starts_with(h.short()));
    /// ```
    #[must_use]
    pub fn short(&self) -> &str {
        // Internal layout: "sha256:" (7 bytes) + 64 hex chars
        // Return chars [7..14] — the first 7 hex chars, no prefix.
        &self.0[Self::PREFIX.len()..Self::PREFIX.len() + Self::MIN_HEX_CHARS]
    }

    /// Check whether an agent-supplied hash token matches this hash.
    ///
    /// This is the single authoritative hash comparison — it replaces all raw
    /// `==` / `!=` string comparisons and `check_occ` prefix logic. Accepting
    /// all formats prevents version-mismatch failures when agents supply the
    /// short form produced by [`short`].
    ///
    /// # Accepted formats
    ///
    /// | Format | Example | Notes |
    /// |--------|---------|-------|
    /// | Short (no prefix) | `"e3dc7f9"` | Preferred — what [`short`] emits |
    /// | Short (with prefix) | `"sha256:e3dc7f9"` | Legacy short form |
    /// | Full (with prefix) | `"sha256:<64 hex>"` | Full hash |
    ///
    /// Returns `false` if the input has fewer than 7 hex chars.
    #[must_use]
    pub fn matches(&self, agent_input: &str) -> bool {
        let full_hex = &self.0[Self::PREFIX.len()..]; // 64 hex chars
        let input_hex = agent_input
            .strip_prefix(Self::PREFIX)
            .unwrap_or(agent_input);
        input_hex.len() >= Self::MIN_HEX_CHARS && full_hex.starts_with(input_hex)
    }
}

impl fmt::Display for VersionHash {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// The exact source code and metadata for an AST symbol.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SymbolScope {
    /// The source code snippet of the symbol block.
    pub content: String,
    /// The zero-indexed starting line.
    pub start_line: usize,
    /// The zero-indexed ending line.
    pub end_line: usize,
    /// The zero-indexed column where the symbol's **name identifier** begins.
    ///
    /// For `pub fn dedent(code: &str)`, this is the column of the `d` in `dedent`
    /// (not the `p` in `pub`). Used by LSP navigation tools (`get_definition`,
    /// `analyze_impact`, `read_with_deep_context`) to position the cursor on the
    /// symbol name, which is required for rust-analyzer to resolve the symbol.
    pub name_column: usize,
    /// The language of the file.
    pub language: String,
}

/// The workspace root path. All file operations are relative to this.
#[derive(Debug, Clone)]
pub struct WorkspaceRoot(PathBuf);

impl WorkspaceRoot {
    /// Create a new workspace root, verifying the directory exists.
    ///
    /// # Errors
    /// Returns `std::io::Error` if the path does not exist or cannot be canonicalized.
    pub fn new(path: impl Into<PathBuf>) -> std::io::Result<Self> {
        let path = path.into();
        let canonical = path.canonicalize()?;
        Ok(Self(canonical))
    }

    /// Resolve a relative path against the workspace root.
    ///
    /// # Path traversal protection
    ///
    /// This function returns a joined `PathBuf` even if the input contains
    /// `..` components. The caller's Sandbox is the primary security boundary;
    /// it rejects traversal before any I/O. This method normalizes the path but
    /// does not perform access control.
    ///
    /// # Symlink behavior
    ///
    /// This method does not resolve symlinks. Symlinks are handled at the
    /// Sandbox layer for security enforcement. If you need the canonical path,
    /// use `WorkspaceRoot::path()` and canonicalize manually with appropriate
    /// error handling.
    ///
    /// # Security
    /// This function performs a defense-in-depth traversal check: if the
    /// relative path contains `..` components, a warning is logged.
    /// This guard ensures internal callers that bypass the Sandbox are warned.
    #[must_use]
    pub fn resolve(&self, relative: &Path) -> PathBuf {
        let is_absolute = relative.is_absolute();
        let has_traversal = relative
            .components()
            .any(|c| c == std::path::Component::ParentDir);

        // Defense-in-depth: detect path traversal even without sandbox
        if is_absolute || has_traversal {
            tracing::warn!(
                relative = %relative.display(),
                workspace = %self.0.display(),
                "WorkspaceRoot::resolve: absolute path or traversal detected; sandbox will reject"
            );
        }

        let mut normalized = PathBuf::default();
        for comp in relative.components() {
            if matches!(
                comp,
                std::path::Component::Prefix(_) | std::path::Component::RootDir
            ) {
                continue;
            }
            normalized.push(comp);
        }

        self.0.join(normalized)
    }

    /// Strict variant of resolve that rejects path traversal attempts.
    ///
    /// Use this in security-critical paths (file operations, edit tools).
    /// Returns an error if the relative path contains `..` components or is absolute.
    ///
    /// # Errors
    /// Returns `PathfinderError::PathTraversal` if the path contains traversal
    /// components or is absolute.
    pub fn resolve_strict(&self, relative: &Path) -> Result<PathBuf, PathfinderError> {
        let is_absolute = relative.is_absolute();
        let has_traversal = relative
            .components()
            .any(|c| c == std::path::Component::ParentDir);

        if is_absolute || has_traversal {
            return Err(PathfinderError::PathTraversal {
                path: relative.to_path_buf(),
                workspace_root: self.0.clone(),
            });
        }

        // Delegate to resolve for the actual normalization
        // (which still warns but since we've already filtered, it won't trigger)
        Ok(self.resolve(relative))
    }

    /// Get the workspace root path.
    #[must_use]
    pub fn path(&self) -> &Path {
        &self.0
    }
}

/// Filter mode for `search_codebase`.
#[derive(
    Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema,
)]
#[serde(rename_all = "snake_case")]
pub enum FilterMode {
    /// Only matches in code (exclude comments and string literals).
    #[default]
    CodeOnly,
    /// Only matches in comments.
    CommentsOnly,
    /// All matches (no filtering).
    All,
}

/// Visibility filter for `get_repo_map`.
#[derive(
    Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema,
)]
#[serde(rename_all = "snake_case")]
pub enum Visibility {
    /// Only exported/public symbols.
    #[default]
    Public,
    /// All symbols including private/internal.
    All,
}

/// Import inclusion mode for `get_repo_map`.
#[derive(
    Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema,
)]
#[serde(rename_all = "snake_case")]
pub enum IncludeImports {
    /// Omit all imports.
    None,
    /// Include only external/package imports.
    #[default]
    ThirdParty,
    /// Include all import statements.
    All,
}

/// Reason for degraded mode in tool responses.
///
/// Standardized enum for all degraded reasons across tools. Provides
/// machine-parsable values for agents to understand and handle degraded responses.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum DegradedReason {
    /// LSP is not available for this language.
    NoLsp,
    /// LSP is warming up and returned empty unverified results.
    LspWarmupEmptyUnverified,
    /// LSP returned no result (likely warming up); fell back to grep.
    LspWarmupGrepFallback,
    /// LSP timed out; fell back to grep.
    LspTimeoutGrepFallback,
    /// LSP returned an error; fell back to grep.
    LspErrorGrepFallback,
    /// LSP unavailable; fell back to grep.
    NoLspGrepFallback,
    /// Grep fallback result from file-scoped search.
    GrepFallbackFileScoped,
    /// Grep fallback result from impl-scoped search.
    GrepFallbackImplScoped,
    /// Grep fallback result from global search.
    GrepFallbackGlobal,
    /// Language unsupported; filter was bypassed to return results.
    UnsupportedLanguageFilterBypassed,
    /// Language is not supported.
    UnsupportedLanguage,
    /// Git error (e.g., `get_repo_map` `changed_since` filter failed).
    GitError,
}

impl fmt::Display for DegradedReason {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = match self {
            DegradedReason::NoLsp => "no_lsp",
            DegradedReason::LspWarmupEmptyUnverified => "lsp_warmup_empty_unverified",
            DegradedReason::LspWarmupGrepFallback => "lsp_warmup_grep_fallback",
            DegradedReason::LspTimeoutGrepFallback => "lsp_timeout_grep_fallback",
            DegradedReason::LspErrorGrepFallback => "lsp_error_grep_fallback",
            DegradedReason::NoLspGrepFallback => "no_lsp_grep_fallback",
            DegradedReason::GrepFallbackFileScoped => "grep_fallback_file_scoped",
            DegradedReason::GrepFallbackImplScoped => "grep_fallback_impl_scoped",
            DegradedReason::GrepFallbackGlobal => "grep_fallback_global",
            DegradedReason::UnsupportedLanguageFilterBypassed => {
                "unsupported_language_filter_bypassed"
            }
            DegradedReason::UnsupportedLanguage => "unsupported_language",
            DegradedReason::GitError => "git_error",
        };
        write!(f, "{s}")
    }
}

#[cfg(test)]
#[allow(clippy::expect_used)]
mod tests {
    use super::*;

    #[test]
    fn test_semantic_path_with_symbol() {
        let sp = SemanticPath::parse("src/auth.ts::AuthService.login").expect("should parse");
        assert_eq!(sp.file_path, PathBuf::from("src/auth.ts"));
        assert!(!sp.is_bare_file());

        let chain = sp.symbol_chain.as_ref().expect("should have symbol chain");
        assert_eq!(chain.segments.len(), 2);
        assert_eq!(chain.segments[0].name, "AuthService");
        assert_eq!(chain.segments[1].name, "login");
    }

    #[test]
    fn test_semantic_path_bare_file() {
        let sp = SemanticPath::parse("src/utils.ts").expect("should parse");
        assert_eq!(sp.file_path, PathBuf::from("src/utils.ts"));
        assert!(sp.is_bare_file());
    }

    #[test]
    fn test_semantic_path_with_overload() {
        let sp =
            SemanticPath::parse("src/auth.ts::AuthService.refreshToken#2").expect("should parse");
        let chain = sp.symbol_chain.as_ref().expect("should have symbol chain");
        let last = chain.segments.last().expect("should have segments");
        assert_eq!(last.name, "refreshToken");
        assert_eq!(last.overload_index, Some(2));
    }

    #[test]
    fn test_semantic_path_display_roundtrip() {
        let input = "src/auth.ts::AuthService.login#2";
        let sp = SemanticPath::parse(input).expect("should parse");
        assert_eq!(sp.to_string(), input);
    }

    #[test]
    fn test_semantic_path_empty_input() {
        assert!(SemanticPath::parse("").is_none());
    }

    #[test]
    fn test_semantic_path_empty_file_part() {
        assert!(SemanticPath::parse("::AuthService").is_none());
    }

    #[test]
    fn test_semantic_path_default_export() {
        let sp = SemanticPath::parse("src/auth.ts::default").expect("should parse");
        let chain = sp.symbol_chain.as_ref().expect("should have chain");
        assert_eq!(chain.segments.len(), 1);
        assert_eq!(chain.segments[0].name, "default");
    }

    #[test]
    fn test_version_hash_compute() {
        let hash = VersionHash::compute(b"hello world");
        assert!(hash.as_str().starts_with("sha256:"));
        // SHA-256 of "hello world" is well-known
        assert!(hash.as_str().contains("b94d27b9934d3e08a52e52d7"));
    }

    #[test]
    fn test_version_hash_equality() {
        let h1 = VersionHash::compute(b"same content");
        let h2 = VersionHash::compute(b"same content");
        assert_eq!(h1, h2);

        let h3 = VersionHash::compute(b"different content");
        assert_ne!(h1, h3);
    }

    // ── VersionHash::short() tests ────────────────────────────────────────────

    /// `short()` must return exactly 7 hex characters with no prefix.
    #[test]
    fn test_version_hash_short_is_7_hex_chars() {
        let hash = VersionHash::compute(b"hello world");
        let s = hash.short();
        assert_eq!(s.len(), 7, "short() must be exactly 7 chars");
        assert!(
            s.chars().all(|c| c.is_ascii_hexdigit()),
            "short() must be hex chars only, got: {s}"
        );
    }

    /// `short()` must NOT contain the 'sha256:' prefix.
    #[test]
    fn test_version_hash_short_has_no_prefix() {
        let hash = VersionHash::compute(b"test content");
        assert!(
            !hash.short().starts_with("sha256:"),
            "short() must not start with 'sha256:'"
        );
    }

    /// `short()` must be the start of the hex portion of `as_str()`.
    #[test]
    fn test_version_hash_short_is_prefix_of_full_hex() {
        let hash = VersionHash::compute(b"hello world");
        let full = hash.as_str(); // "sha256:<64 hex>"
        assert!(
            full["sha256:".len()..].starts_with(hash.short()),
            "full hex must start with short()"
        );
    }

    // ── VersionHash::matches() tests ──────────────────────────────────────────

    /// The preferred format: 7 hex chars, no prefix — what `short()` emits.
    #[test]
    fn test_matches_short_no_prefix() {
        let hash = VersionHash::compute(b"hello world");
        assert!(
            hash.matches(hash.short()),
            "hash.matches(hash.short()) must be true — roundtrip test"
        );
    }

    /// Short hash with the legacy sha256: prefix.
    #[test]
    fn test_matches_short_with_legacy_prefix() {
        let hash = VersionHash::compute(b"hello world");
        let with_prefix = format!("sha256:{}", hash.short());
        assert!(
            hash.matches(&with_prefix),
            "7-char hash with sha256: prefix must match"
        );
    }

    /// Full 71-char hash with prefix (backward compatibility).
    #[test]
    fn test_matches_full_hash_with_prefix() {
        let hash = VersionHash::compute(b"hello world");
        assert!(
            hash.matches(hash.as_str()),
            "full hash as_str() must match itself"
        );
    }

    /// 8-char prefix should also be accepted (> minimum).
    #[test]
    fn test_matches_8_char_prefix_accepted() {
        let hash = VersionHash::compute(b"hello world");
        let eight = &hash.as_str()["sha256:".len().."sha256:".len() + 8];
        assert!(hash.matches(eight), "8-char prefix must be accepted");
    }

    /// Inputs shorter than 7 hex chars must be rejected.
    #[test]
    fn test_matches_too_short_rejected() {
        let hash = VersionHash::compute(b"hello world");
        assert!(!hash.matches("e3dc7f"), "6 hex chars must be rejected");
        assert!(
            !hash.matches("sha256:abc"),
            "3 hex chars with prefix rejected"
        );
        assert!(!hash.matches(""), "empty string must be rejected");
    }

    /// Wrong prefix must not match.
    #[test]
    fn test_matches_wrong_hex_fails() {
        let hash = VersionHash::compute(b"hello world");
        assert!(!hash.matches("0000000"), "wrong 7-char hex must not match");
        assert!(
            !hash.matches("sha256:0000000"),
            "wrong prefixed hex must not match"
        );
    }

    /// Hashes of different content must not match each other.
    #[test]
    fn test_matches_different_content_fails() {
        let hash_a = VersionHash::compute(b"content A");
        let hash_b = VersionHash::compute(b"content B");
        assert!(
            !hash_a.matches(hash_b.short()),
            "short hash from different content must not match"
        );
    }

    #[test]
    fn test_filter_mode_default() {
        assert_eq!(FilterMode::default(), FilterMode::CodeOnly);
    }

    #[test]
    fn test_resolve_path_traversal_is_detected() {
        // WorkspaceRoot::resolve must still return the joined path (so the
        // Sandbox can do its job), but the traversal-detection branch must
        // fire without panicking.
        let dir = tempfile::tempdir().expect("create tempdir");
        let root = WorkspaceRoot::new(dir.path()).expect("create workspace root");

        let traversal = std::path::Path::new("../../etc/passwd");
        // Should not panic; the sandbox is the primary enforcement layer.
        let resolved = root.resolve(traversal);
        // The resolved path escapes the workspace — that is expected here.
        // The Sandbox (not resolve) is responsible for rejection.
        assert!(resolved.to_string_lossy().contains("etc/passwd"));
    }

    #[test]
    fn test_resolve_strict_rejects_traversal() {
        let dir = tempfile::tempdir().expect("create tempdir");
        let root = WorkspaceRoot::new(dir.path()).expect("create workspace root");

        let traversal = std::path::Path::new("../../etc/passwd");
        let result = root.resolve_strict(traversal);

        assert!(result.is_err());
        assert!(matches!(result, Err(PathfinderError::PathTraversal { .. })));
    }

    #[test]
    fn test_resolve_strict_rejects_absolute_path() {
        let dir = tempfile::tempdir().expect("create tempdir");
        let root = WorkspaceRoot::new(dir.path()).expect("create workspace root");

        let absolute = std::path::Path::new("/etc/passwd");
        let result = root.resolve_strict(absolute);

        assert!(result.is_err());
        assert!(matches!(result, Err(PathfinderError::PathTraversal { .. })));
    }

    #[test]
    fn test_resolve_strict_accepts_relative_path() {
        let dir = tempfile::tempdir().expect("create tempdir");
        let root = WorkspaceRoot::new(dir.path()).expect("create workspace root");

        let relative = std::path::Path::new("src/main.rs");
        let result = root.resolve_strict(relative);

        assert!(result.is_ok());
        let resolved = result.expect("should be Ok");
        assert!(resolved.to_string_lossy().contains("src/main.rs"));
    }

    // ── DegradedReason tests ────────────────────────────────────────────────

    #[test]
    fn test_degraded_reason_serde_snake_case() {
        // Verify serde serialization produces snake_case strings (backward compatible)
        use super::DegradedReason;

        assert_eq!(
            serde_json::to_string(&DegradedReason::NoLsp).expect("NoLsp should serialize to JSON"),
            "\"no_lsp\""
        );
        assert_eq!(
            serde_json::to_string(&DegradedReason::LspWarmupGrepFallback)
                .expect("LspWarmupGrepFallback should serialize to JSON"),
            "\"lsp_warmup_grep_fallback\""
        );
        assert_eq!(
            serde_json::to_string(&DegradedReason::GitError)
                .expect("GitError should serialize to JSON"),
            "\"git_error\""
        );
    }

    #[test]
    fn test_degraded_reason_display() {
        use super::DegradedReason;

        assert_eq!(DegradedReason::NoLsp.to_string(), "no_lsp");
        assert_eq!(
            DegradedReason::LspWarmupEmptyUnverified.to_string(),
            "lsp_warmup_empty_unverified"
        );
        assert_eq!(
            DegradedReason::GrepFallbackGlobal.to_string(),
            "grep_fallback_global"
        );
    }
}