code-kb-core 1.0.3

Core library for code-kb AST fact querying, slicing, and progressive disclosure
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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
use std::path::{Path, PathBuf};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum WorkspaceError {
    #[error("Failed to canonicalize path {path}: {source}")]
    CanonicalizationFailed {
        path: PathBuf,
        #[source]
        source: std::io::Error,
    },
    #[error("Path '{0}' is outside workspace root '{1}'")]
    PathOutsideWorkspace(PathBuf, PathBuf),
    #[error("Could not discover workspace root from '{0}'")]
    DiscoveryFailed(PathBuf),
    #[error("Database artifact not found at '{0}'")]
    ArtifactNotFound(PathBuf),
}

/// Lexically clean a path by collapsing `.` and `..` components.
pub fn clean_path(path: &Path) -> PathBuf {
    use std::path::Component;
    let s = path.to_string_lossy();
    let norm = if cfg!(not(windows)) && s.contains('\\') {
        std::borrow::Cow::Owned(PathBuf::from(s.replace('\\', "/")))
    } else {
        std::borrow::Cow::Borrowed(path)
    };
    let mut stack = Vec::new();
    for comp in norm.components() {
        match comp {
            Component::CurDir => {}
            Component::ParentDir => {
                if let Some(Component::Normal(_)) = stack.last() {
                    stack.pop();
                } else {
                    stack.push(comp);
                }
            }
            _ => stack.push(comp),
        }
    }
    stack.into_iter().collect()
}

fn percent_decode(input: &str) -> String {
    let mut bytes = Vec::with_capacity(input.len());
    let input_bytes = input.as_bytes();
    let mut i = 0;
    while i < input_bytes.len() {
        if input_bytes[i] == b'%'
            && i + 2 < input_bytes.len()
            && let Ok(hex) = std::str::from_utf8(&input_bytes[i + 1..i + 3])
            && let Ok(byte) = u8::from_str_radix(hex, 16)
        {
            bytes.push(byte);
            i += 3;
            continue;
        }
        bytes.push(input_bytes[i]);
        i += 1;
    }
    String::from_utf8_lossy(&bytes).into_owned()
}

/// Extract drive letter and remainder if the string begins with a drive specification
/// delimited by ':', '|', or percent-encoded "%7C" / "%3A".
fn extract_drive_letter_and_remainder(s: &str) -> Option<(char, &str)> {
    let bytes = s.as_bytes();
    if bytes.is_empty() || !bytes[0].is_ascii_alphabetic() {
        return None;
    }
    let drive = bytes[0] as char;

    // Single-byte delimiters: ':' or '|'
    if bytes.len() >= 2
        && (bytes[1] == b':' || bytes[1] == b'|')
        && (bytes.len() == 2
            || bytes[2] == b'/'
            || bytes[2] == b'\\'
            || bytes[2] == b'?'
            || bytes[2] == b'#')
    {
        return Some((drive, &s[2..]));
    }

    // Three-byte percent-encoded delimiters: "%7C", "%7c", "%3A", "%3a"
    if bytes.len() >= 4 {
        let delim = &bytes[1..4];
        if (delim.eq_ignore_ascii_case(b"%7c") || delim.eq_ignore_ascii_case(b"%3a"))
            && (bytes.len() == 4
                || bytes[4] == b'/'
                || bytes[4] == b'\\'
                || bytes[4] == b'?'
                || bytes[4] == b'#')
        {
            return Some((drive, &s[4..]));
        }
    }

    None
}

/// Strip an optional "localhost/" or "localhost\" prefix (with or without a leading slash).
fn strip_localhost_prefix(s: &str) -> &str {
    let without_slash = s.strip_prefix('/').unwrap_or(s);
    let bytes = without_slash.as_bytes();
    if bytes.len() >= 10
        && bytes[..9].eq_ignore_ascii_case(b"localhost")
        && (bytes[9] == b'/' || bytes[9] == b'\\')
    {
        &without_slash[10..]
    } else {
        s
    }
}

/// Convert a path string starting with a pipe drive specification (e.g. "C|/..." or "/C|/...")
/// to use a standard colon ':' delimiter (e.g. "C:/...").
fn normalize_drive_pipe_str(s: &str) -> String {
    let clean = strip_localhost_prefix(s);
    let target = clean.strip_prefix('/').unwrap_or(clean);
    let target = strip_localhost_prefix(target);
    if let Some((drive, remainder)) = extract_drive_letter_and_remainder(target) {
        if remainder.is_empty() || remainder.starts_with('?') || remainder.starts_with('#') {
            format!("{}:/{}", drive, remainder)
        } else {
            format!("{}:{}", drive, remainder)
        }
    } else {
        s.to_string()
    }
}

/// Parse an MCP file URI or plain path into a normalized PathBuf.
/// Handles standard file URIs (`file:///path`), two-slash drive letter URIs (`file://C:/...`),
/// pipe drive delimiters (`file:///C|/...`, `file://C|/...`), percent-encoding (`%20`, `%7C`), and plain paths.
pub fn parse_file_uri(cand: &str) -> Option<PathBuf> {
    if let Some(rest) = cand.strip_prefix("file://") {
        let path_part = rest.strip_prefix('/').unwrap_or(rest);
        let path_part = strip_localhost_prefix(path_part);
        let normalized_cand = if let Some((drive, remainder)) =
            extract_drive_letter_and_remainder(path_part)
        {
            if remainder.is_empty() || remainder.starts_with('?') || remainder.starts_with('#') {
                format!("file:///{}:/{}", drive, remainder)
            } else if remainder.starts_with('/') || remainder.starts_with('\\') {
                format!("file:///{}:{}", drive, remainder)
            } else {
                format!("file:///{}:/{}", drive, remainder)
            }
        } else {
            cand.to_string()
        };

        let file_path = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            url::Url::parse(&normalized_cand)
                .ok()
                .and_then(|url| url.to_file_path().ok())
        }))
        .ok()
        .flatten();

        if let Some(path) = file_path {
            return Some(normalize_path(&path));
        }
        // Fallback for non-standard file:// patterns with percent decoding
        if let Some(s) = cand.strip_prefix("file:///") {
            let decoded = percent_decode(s);
            let normalized = normalize_drive_pipe_str(&decoded);
            if cfg!(windows) {
                Some(normalize_path(Path::new(&normalized)))
            } else {
                Some(normalize_path(&PathBuf::from(format!("/{}", normalized))))
            }
        } else {
            let s = cand.strip_prefix("file://").unwrap_or(cand);
            let decoded = percent_decode(s);
            let normalized = normalize_drive_pipe_str(&decoded);
            Some(normalize_path(Path::new(&normalized)))
        }
    } else {
        let normalized = normalize_drive_pipe_str(cand);
        Some(normalize_path(Path::new(&normalized)))
    }
}

/// Strip Windows verbatim prefix (\\?\, \\?\UNC\) using dunce.
pub fn normalize_path(path: &Path) -> PathBuf {
    let s = path.to_string_lossy();
    if let Some(rest) = s.strip_prefix(r"\\?\UNC\") {
        let unc = format!(r"\\{rest}");
        return dunce::simplified(Path::new(&unc)).to_path_buf();
    }
    if let Some(rest) = s.strip_prefix(r"\\?\") {
        return dunce::simplified(Path::new(rest)).to_path_buf();
    }
    dunce::simplified(path).to_path_buf()
}

/// Convert a path to forward-slash string representation for stable relative paths.
pub fn to_forward_slash(path: &Path) -> String {
    let s = path.to_string_lossy();
    s.replace('\\', "/")
}

/// Compare two path components for equality.
/// On Windows, compares `Component::Normal` case-insensitively and drive letters in `Component::Prefix` case-insensitively.
#[cfg(windows)]
fn components_equal(c1: &std::path::Component, c2: &std::path::Component) -> bool {
    if c1 == c2 {
        return true;
    }
    {
        use std::path::Component;
        match (c1, c2) {
            (Component::Normal(s1), Component::Normal(s2)) => s1
                .to_string_lossy()
                .eq_ignore_ascii_case(&s2.to_string_lossy()),
            (Component::Prefix(p1), Component::Prefix(p2)) => {
                use std::path::Prefix;
                match (p1.kind(), p2.kind()) {
                    (Prefix::Disk(d1), Prefix::Disk(d2))
                    | (Prefix::VerbatimDisk(d1), Prefix::VerbatimDisk(d2))
                    | (Prefix::Disk(d1), Prefix::VerbatimDisk(d2))
                    | (Prefix::VerbatimDisk(d1), Prefix::Disk(d2)) => d1.eq_ignore_ascii_case(&d2),
                    (Prefix::UNC(s1, sh1), Prefix::UNC(s2, sh2))
                    | (Prefix::VerbatimUNC(s1, sh1), Prefix::VerbatimUNC(s2, sh2))
                    | (Prefix::UNC(s1, sh1), Prefix::VerbatimUNC(s2, sh2))
                    | (Prefix::VerbatimUNC(s1, sh1), Prefix::UNC(s2, sh2)) => {
                        s1.to_string_lossy()
                            .eq_ignore_ascii_case(&s2.to_string_lossy())
                            && sh1
                                .to_string_lossy()
                                .eq_ignore_ascii_case(&sh2.to_string_lossy())
                    }
                    (Prefix::DeviceNS(d1), Prefix::DeviceNS(d2))
                    | (Prefix::Verbatim(d1), Prefix::Verbatim(d2)) => d1
                        .to_string_lossy()
                        .eq_ignore_ascii_case(&d2.to_string_lossy()),
                    _ => false,
                }
            }
            _ => false,
        }
    }
}

/// Strips `base` from `path`. On Windows, if standard `strip_prefix` fails,
/// performs case-insensitive component comparison to support Windows case-preserving filesystems.
pub fn strip_prefix_lossy<'a>(path: &'a Path, base: &Path) -> Option<&'a Path> {
    if let Ok(rel) = path.strip_prefix(base) {
        return Some(rel);
    }

    #[cfg(windows)]
    {
        let mut path_comps = path.components();
        for base_comp in base.components() {
            let path_comp = path_comps.next()?;
            if !components_equal(&base_comp, &path_comp) {
                return None;
            }
        }
        Some(path_comps.as_path())
    }
    #[cfg(not(windows))]
    {
        None
    }
}

/// Compare two paths for logical identity.
/// On Windows, normalizes verbatim prefixes via dunce and compares disk prefixes and components case-insensitively.
/// On non-Windows, compares paths directly.
pub fn paths_equal(p1: &Path, p2: &Path) -> bool {
    let p1_norm = normalize_path(p1);
    let p2_norm = normalize_path(p2);
    if p1_norm == p2_norm {
        return true;
    }
    if to_forward_slash(&p1_norm) == to_forward_slash(&p2_norm) {
        return true;
    }
    if let (Ok(c1), Ok(c2)) = (dunce::canonicalize(p1), dunce::canonicalize(p2)) {
        let c1_norm = normalize_path(&c1);
        let c2_norm = normalize_path(&c2);
        if c1_norm == c2_norm || to_forward_slash(&c1_norm) == to_forward_slash(&c2_norm) {
            return true;
        }
    }
    #[cfg(windows)]
    {
        let mut c1 = p1_norm.components();
        let mut c2 = p2_norm.components();
        loop {
            match (c1.next(), c2.next()) {
                (None, None) => return true,
                (Some(comp1), Some(comp2)) => {
                    if !components_equal(&comp1, &comp2) {
                        return false;
                    }
                }
                _ => return false,
            }
        }
    }
    #[cfg(not(windows))]
    {
        false
    }
}

/// Check if a relative path contains directories or file patterns that must never be indexed or watched.
pub fn is_hard_excluded(rel_path: &str) -> bool {
    let p = rel_path.replace('\\', "/");
    let has_excluded_dir = p.split('/').any(|component| {
        matches!(
            component,
            ".git"
                | ".hg"
                | ".svn"
                | ".julie"
                | ".code-kb"
                | ".memories"
                | ".agents"
                | ".razorback"
                | ".worktrees"
                | "worktrees"
                | ".claude"
                | ".venv"
                | "venv"
                | ".env"
                | ".tox"
                | ".vs"
                | "node_modules"
                | "vendor"
                | "target"
                | "dist"
                | "build"
                | ".cache"
                | "obj"
                | "TestResults"
                | ".idea"
                | ".vscode"
        )
    });

    if has_excluded_dir {
        return true;
    }

    const EXCLUDED_SUFFIXES: &[&str] = &[
        ".min.js",
        ".bundle.js",
        ".generated.js",
        ".generated.jsx",
        ".generated.ts",
        ".generated.tsx",
        ".generated.d.ts",
        ".tmp",
        ".swp",
        "~",
    ];

    EXCLUDED_SUFFIXES.iter().any(|suffix| p.ends_with(suffix))
}

/// Represents a bound workspace session.
#[derive(Debug, Clone)]
pub struct Workspace {
    pub root: PathBuf,
    pub canonical_root: PathBuf,
    pub repo_name: String,
}

fn trim_trailing_slash(p: &Path) -> PathBuf {
    let s = p.to_string_lossy();
    if s.len() > 1 && (s.ends_with('/') || s.ends_with('\\')) {
        let trimmed = s.trim_end_matches(['/', '\\']);
        if trimmed.is_empty() {
            return PathBuf::from(if cfg!(windows) && s.starts_with('\\') {
                "\\"
            } else {
                "/"
            });
        }
        if cfg!(windows)
            && trimmed.len() == 2
            && trimmed.as_bytes()[0].is_ascii_alphabetic()
            && trimmed.as_bytes()[1] == b':'
        {
            return PathBuf::from(format!("{}\\", trimmed));
        }
        return PathBuf::from(trimmed);
    }
    p.to_path_buf()
}

/// True when `root` carries a repository or language project marker.
pub fn is_project_root(root: &Path) -> bool {
    [
        ".git",
        "Cargo.toml",
        "package.json",
        "go.mod",
        "pyproject.toml",
    ]
    .iter()
    .any(|marker| root.join(marker).exists())
}

impl Workspace {
    /// Discover and bind a workspace from an optional path, falling back to CWD and upward traversal.
    pub fn discover(start_path: Option<&Path>) -> Result<Self, WorkspaceError> {
        let current = match start_path {
            Some(p) => p.to_path_buf(),
            None => std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")),
        };

        let root = Self::find_workspace_root(&current)?;
        Ok(Self::new(root))
    }

    /// Create workspace binding directly for a known root directory.
    pub fn new(root: PathBuf) -> Self {
        let root_str = root.to_string_lossy();
        let root = if root_str.starts_with("file://") {
            parse_file_uri(&root_str).unwrap_or_else(|| normalize_path(&root))
        } else {
            normalize_path(&root)
        };
        let root = trim_trailing_slash(&root);
        let canonical_root =
            normalize_path(&dunce::canonicalize(&root).unwrap_or_else(|_| root.clone()));
        let repo_name = canonical_root
            .file_name()
            .map(|n| n.to_string_lossy().to_string())
            .unwrap_or_else(|| "repo".to_string());

        Self {
            root,
            canonical_root,
            repo_name,
        }
    }

    /// Find root by searching upwards for .git, .code-kb, or workspace markers.
    pub fn find_workspace_root(start: &Path) -> Result<PathBuf, WorkspaceError> {
        let raw = start.to_string_lossy();
        let parsed = if raw.starts_with("file://") {
            parse_file_uri(&raw).unwrap_or_else(|| start.to_path_buf())
        } else {
            start.to_path_buf()
        };
        let parsed = trim_trailing_slash(&parsed);
        let curr = if parsed.is_file() {
            parsed.parent().unwrap_or(&parsed).to_path_buf()
        } else {
            parsed.clone()
        };

        // Pass 1: Look for .git or .code-kb all the way up
        let mut probe = curr.clone();
        loop {
            if probe.join(".code-kb").exists() || probe.join(".git").exists() {
                let canon = dunce::canonicalize(&probe).unwrap_or(probe);
                return Ok(normalize_path(&canon));
            }
            if let Some(name) = probe.file_name().and_then(|n| n.to_str())
                && is_hard_excluded(name)
            {
                break;
            }
            if let Some(parent) = probe.parent() {
                if parent == probe {
                    break;
                }
                probe = parent.to_path_buf();
            } else {
                break;
            }
        }

        // Pass 2: Look for language project markers
        let mut curr_marker = curr.clone();
        loop {
            if curr_marker.join("Cargo.toml").exists()
                || curr_marker.join("package.json").exists()
                || curr_marker.join("go.mod").exists()
                || curr_marker.join("pyproject.toml").exists()
            {
                let canon = dunce::canonicalize(&curr_marker).unwrap_or(curr_marker);
                return Ok(normalize_path(&canon));
            }
            if let Some(name) = curr_marker.file_name().and_then(|n| n.to_str())
                && is_hard_excluded(name)
            {
                break;
            }

            if let Some(parent) = curr_marker.parent() {
                if parent == curr_marker {
                    break;
                }
                curr_marker = parent.to_path_buf();
            } else {
                break;
            }
        }

        // Default to start directory if no markers found
        let start_dir = if parsed.is_file() {
            parsed.parent().unwrap_or(&parsed).to_path_buf()
        } else {
            parsed
        };
        let canon = dunce::canonicalize(&start_dir).unwrap_or(start_dir);
        Ok(normalize_path(&canon))
    }

    /// Resolves an input path (relative, absolute, or file:// URI) to a canonical absolute path and relative path.
    pub fn resolve_path(&self, input: &Path) -> Result<(PathBuf, String), WorkspaceError> {
        let raw_str = input.to_string_lossy();
        let path = if raw_str.starts_with("file://") {
            parse_file_uri(&raw_str).unwrap_or_else(|| input.to_path_buf())
        } else {
            input.to_path_buf()
        };
        let path = normalize_path(&path);

        let is_abs = path.is_absolute()
            || (cfg!(windows) && (path.to_string_lossy().chars().nth(1) == Some(':')));

        let joined = if is_abs {
            path
        } else {
            let rel_str = if cfg!(not(windows)) && path.to_string_lossy().contains('\\') {
                path.to_string_lossy().replace('\\', "/")
            } else {
                path.to_string_lossy().to_string()
            };
            self.canonical_root.join(Path::new(&rel_str))
        };

        // Lexically clean the path to collapse `.` and `..` components
        let cleaned = clean_path(&joined);
        let abs_path = normalize_path(&cleaned);

        // If file exists, canonicalize to resolve any symlinks
        let effective_abs = if abs_path.exists() {
            dunce::canonicalize(&abs_path)
                .map(|p| normalize_path(&p))
                .unwrap_or_else(|_| abs_path.clone())
        } else {
            abs_path.clone()
        };

        let norm_root = dunce::canonicalize(&self.canonical_root)
            .map(|p| normalize_path(&p))
            .unwrap_or_else(|_| self.canonical_root.clone());

        // Check if within canonical root (trying multiple normalization variants with case-insensitivity on Windows)
        let rel = match strip_prefix_lossy(&effective_abs, &norm_root)
            .or_else(|| strip_prefix_lossy(&effective_abs, &self.canonical_root))
            .or_else(|| {
                // Only fall back to uncanonicalized abs_path if the file does not exist yet (e.g. filters or uncreated files)
                if !abs_path.exists() {
                    strip_prefix_lossy(&abs_path, &norm_root)
                        .or_else(|| strip_prefix_lossy(&abs_path, &self.canonical_root))
                } else {
                    None
                }
            }) {
            Some(r) => {
                let forward = to_forward_slash(r);
                if forward.starts_with("../") || forward == ".." {
                    return Err(WorkspaceError::PathOutsideWorkspace(
                        abs_path,
                        self.canonical_root.clone(),
                    ));
                }
                forward
            }
            None => {
                return Err(WorkspaceError::PathOutsideWorkspace(
                    abs_path,
                    self.canonical_root.clone(),
                ));
            }
        };

        Ok((effective_abs, rel))
    }

    /// Relativizes a path filter string (which may be absolute, file:// URI, or relative)
    /// against this workspace root into a forward-slash relative path suitable for SQLite queries.
    pub fn relativize_filter(&self, filter: &str) -> String {
        let trimmed = filter.trim();
        if trimmed.is_empty() {
            return String::new();
        }

        // Handle file:// URI
        let path_str = if trimmed.starts_with("file://") {
            parse_file_uri(trimmed)
                .map(|p| p.to_string_lossy().to_string())
                .unwrap_or_else(|| trimmed.to_string())
        } else {
            trimmed.to_string()
        };

        let raw_path = Path::new(&path_str);
        let simplified = dunce::simplified(raw_path);

        if simplified.is_absolute() {
            if let Ok((_, rel)) = self.resolve_path(simplified) {
                return rel;
            }
            // If resolve_path failed (e.g. non-existent path), try prefix stripping on normalized strings
            let norm_simplified = normalize_path(simplified);
            let norm_root = normalize_path(&self.canonical_root);
            if let Some(rel) = strip_prefix_lossy(&norm_simplified, &norm_root)
                .or_else(|| strip_prefix_lossy(&norm_simplified, &self.root))
            {
                let forward = to_forward_slash(rel);
                if !forward.starts_with("../") && forward != ".." {
                    return forward.trim_matches('/').to_string();
                }
            }
        }

        // Relative path: pass through clean_path to collapse `.` and `..`, normalize slashes, and trim leading ./ or /
        let cleaned = clean_path(Path::new(&path_str));
        let forward = to_forward_slash(&cleaned);
        let trimmed = forward.trim_start_matches("./").trim_matches('/');
        if trimmed == "." {
            String::new()
        } else {
            trimmed.to_string()
        }
    }

    /// Resolve candidate database paths for this workspace:
    /// 1. Explicit override path (if provided)
    /// 2. In-tree `.code-kb/artifact.db` or `.code-kb/store.db`
    pub fn candidate_db_paths(&self, explicit_db: Option<&Path>) -> Vec<PathBuf> {
        let mut candidates = Vec::new();

        if let Some(p) = explicit_db {
            candidates.push(normalize_path(p));
        }

        // In-tree options
        candidates.push(normalize_path(
            &self.canonical_root.join(".code-kb").join("artifact.db"),
        ));
        candidates.push(normalize_path(
            &self.canonical_root.join(".code-kb").join("store.db"),
        ));
        candidates.push(normalize_path(&self.canonical_root.join("artifact.db")));

        candidates
    }

    /// Finds the first existing database file, or returns the default target location.
    pub fn locate_db(&self, explicit_db: Option<&Path>) -> Result<PathBuf, WorkspaceError> {
        if let Some(p) = explicit_db {
            return Ok(normalize_path(p));
        }

        let candidates = self.candidate_db_paths(None);
        for candidate in &candidates {
            if candidate.exists() && candidate.is_file() {
                return Ok(normalize_path(candidate));
            }
        }

        Ok(normalize_path(
            &self.canonical_root.join(".code-kb").join("artifact.db"),
        ))
    }
}

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

    #[test]
    #[cfg(windows)]
    fn test_normalize_path() {
        let p = PathBuf::from(r"\\?\C:\source\code-kb\src\main.rs");
        let norm = normalize_path(&p);
        assert!(!norm.to_string_lossy().starts_with(r"\\?\"));
    }

    #[test]
    fn test_to_forward_slash() {
        let p = PathBuf::from(r"src\models\mod.rs");
        assert_eq!(to_forward_slash(&p), "src/models/mod.rs");
    }

    #[test]
    fn test_workspace_resolve_path() {
        let ws = Workspace::new(PathBuf::from("C:/source/test-project"));
        let (abs, rel) = ws.resolve_path(Path::new("src/lib.rs")).unwrap();
        assert_eq!(rel, "src/lib.rs");
        assert!(abs.to_string_lossy().contains("test-project"));

        #[cfg(windows)]
        {
            // Lowercase drive letter
            let (_abs2, rel2) = ws
                .resolve_path(Path::new("c:/source/test-project/src/lib.rs"))
                .unwrap();
            assert_eq!(rel2, "src/lib.rs");

            // Case-insensitive directory on Windows
            let (_abs3, rel3) = ws
                .resolve_path(Path::new("C:/SOURCE/test-project/src/lib.rs"))
                .unwrap();
            assert_eq!(rel3, "src/lib.rs");

            // file:// URI
            let (_abs4, rel4) = ws
                .resolve_path(Path::new("file:///C:/source/test-project/src/lib.rs"))
                .unwrap();
            assert_eq!(rel4, "src/lib.rs");

            // file:// URI with lowercase drive letter
            let (_abs5, rel5) = ws
                .resolve_path(Path::new("file:///c:/source/test-project/src/lib.rs"))
                .unwrap();
            assert_eq!(rel5, "src/lib.rs");
        }
    }

    #[test]
    fn test_workspace_resolve_path_traversal_escape() {
        let temp = crate::safe_tempdir();
        let ws = Workspace::new(temp.path().to_path_buf());
        let res = ws.resolve_path(Path::new("sub/../../outside.rs"));
        assert!(
            matches!(res, Err(WorkspaceError::PathOutsideWorkspace(..))),
            "Expected PathOutsideWorkspace error, but got: {:?}",
            res
        );
    }

    #[test]
    fn test_parse_file_uri() {
        #[cfg(windows)]
        let (uri, expected) = ("file:///C:/my%20folder/project", "C:/my folder/project");
        #[cfg(not(windows))]
        let (uri, expected) = ("file:///tmp/my%20folder/project", "/tmp/my folder/project");

        let p1 = parse_file_uri(uri).unwrap();
        assert_eq!(p1, normalize_path(Path::new(expected)));

        // Plain path fallback
        let p2 = parse_file_uri("C:/direct/path").unwrap();
        assert_eq!(p2, normalize_path(Path::new("C:/direct/path")));
    }

    #[test]
    fn test_relativize_filter() {
        let temp = crate::safe_tempdir();
        let ws = Workspace::new(temp.path().to_path_buf());

        // Relative path
        assert_eq!(ws.relativize_filter("."), "");
        assert_eq!(ws.relativize_filter("./"), "");
        assert_eq!(ws.relativize_filter("src/models"), "src/models");
        assert_eq!(ws.relativize_filter("./src/models/"), "src/models");
        assert_eq!(
            ws.relativize_filter(r"src\models\mod.rs"),
            "src/models/mod.rs"
        );

        // Relative path with ..
        assert_eq!(
            ws.relativize_filter("src/../src/models/mod.rs"),
            "src/models/mod.rs"
        );

        // Absolute path inside workspace
        let abs_file = temp.path().join("src").join("lib.rs");
        std::fs::create_dir_all(abs_file.parent().unwrap()).unwrap();
        std::fs::write(&abs_file, "").unwrap();

        assert_eq!(
            ws.relativize_filter(&abs_file.to_string_lossy()),
            "src/lib.rs"
        );

        // File URI
        let uri = format!("file://{}", abs_file.to_string_lossy().replace('\\', "/"));
        assert_eq!(ws.relativize_filter(&uri), "src/lib.rs");

        #[cfg(windows)]
        {
            // Case-insensitive absolute path for existing file resolves to canonical disk casing
            let upper_abs = abs_file.to_string_lossy().to_uppercase();
            assert_eq!(ws.relativize_filter(&upper_abs), "src/lib.rs");

            // File URI with alternate case
            let uri_cased = format!(
                "file:///{}",
                abs_file.to_string_lossy().replace('\\', "/").to_lowercase()
            );
            assert_eq!(ws.relativize_filter(&uri_cased), "src/lib.rs");
        }
    }

    #[test]
    fn test_paths_equal() {
        assert!(paths_equal(
            Path::new("src/lib.rs"),
            Path::new("src/lib.rs")
        ));
        assert!(!paths_equal(
            Path::new("src/lib.rs"),
            Path::new("src/main.rs")
        ));

        #[cfg(windows)]
        {
            // Case-insensitive drive letters and paths
            assert!(paths_equal(
                Path::new(r"C:\source\code-kb\src\lib.rs"),
                Path::new(r"c:\source\code-kb\src\lib.rs")
            ));
            assert!(paths_equal(
                Path::new(r"C:\source\code-kb\src\lib.rs"),
                Path::new(r"c:\SOURCE\CODE-KB\SRC\LIB.RS")
            ));
            // Verbatim prefixes
            assert!(paths_equal(
                Path::new(r"\\?\C:\source\code-kb\src\lib.rs"),
                Path::new(r"C:\source\code-kb\src\lib.rs")
            ));
            assert!(paths_equal(
                Path::new(r"\\?\c:\source\code-kb\src\lib.rs"),
                Path::new(r"C:\source\code-kb\src\lib.rs")
            ));
            // UNC paths
            assert!(paths_equal(
                Path::new(r"\\server\share\file"),
                Path::new(r"\\SERVER\SHARE\file")
            ));
            assert!(paths_equal(
                Path::new(r"\\server\share\file"),
                Path::new(r"\\server\share\file")
            ));
            assert!(!paths_equal(
                Path::new(r"\\server\share1\file"),
                Path::new(r"\\server\share2\file")
            ));
        }
    }

    #[test]
    fn test_strip_prefix_lossy() {
        let base = Path::new("src");
        assert_eq!(
            strip_prefix_lossy(Path::new("src/lib.rs"), base),
            Some(Path::new("lib.rs"))
        );
        assert_eq!(strip_prefix_lossy(Path::new("tests/foo.rs"), base), None);

        #[cfg(windows)]
        {
            let base_win = Path::new(r"C:\source\code-kb");
            // Standard path
            assert_eq!(
                strip_prefix_lossy(Path::new(r"C:\source\code-kb\src\lib.rs"), base_win),
                Some(Path::new(r"src\lib.rs"))
            );
            // Disk prefix casing
            assert_eq!(
                strip_prefix_lossy(Path::new(r"c:\source\code-kb\src\lib.rs"), base_win),
                Some(Path::new(r"src\lib.rs"))
            );
            assert_eq!(
                strip_prefix_lossy(Path::new(r"c:\SOURCE\CODE-KB\src\lib.rs"), base_win),
                Some(Path::new(r"src\lib.rs"))
            );
            // Verbatim prefixes
            assert_eq!(
                strip_prefix_lossy(Path::new(r"\\?\C:\source\code-kb\src\lib.rs"), base_win),
                Some(Path::new(r"src\lib.rs"))
            );
            assert_eq!(
                strip_prefix_lossy(Path::new(r"\\?\c:\source\code-kb\src\lib.rs"), base_win),
                Some(Path::new(r"src\lib.rs"))
            );
            // Negative non-matching paths
            assert_eq!(
                strip_prefix_lossy(Path::new(r"C:\other\code-kb\src\lib.rs"), base_win),
                None
            );
            assert_eq!(
                strip_prefix_lossy(Path::new(r"D:\source\code-kb\src\lib.rs"), base_win),
                None
            );
        }
    }

    #[test]
    fn test_parse_file_uri_two_slash_and_percent() {
        #[cfg(windows)]
        {
            let p1 = parse_file_uri("file://C:/my%20folder/lib.rs").unwrap();
            assert_eq!(p1, normalize_path(Path::new("C:/my folder/lib.rs")));

            let p2 = parse_file_uri("file://c:/my%20folder/lib.rs").unwrap();
            assert_eq!(p2, normalize_path(Path::new("c:/my folder/lib.rs")));

            let p3 = parse_file_uri("file:///C:/my%20folder/lib.rs").unwrap();
            assert_eq!(p3, normalize_path(Path::new("C:/my folder/lib.rs")));
        }
        #[cfg(not(windows))]
        {
            let p1 = parse_file_uri("file:///my%20folder/lib.rs").unwrap();
            assert_eq!(p1, normalize_path(Path::new("/my folder/lib.rs")));
        }
    }

    #[test]
    fn test_workspace_verbatim_root_and_db_cleanup() {
        let temp = crate::safe_tempdir();
        let verbatim_path = format!(r"\\?\{}", temp.path().display());
        let ws = Workspace::new(PathBuf::from(&verbatim_path));
        assert!(!ws.root.to_string_lossy().starts_with(r"\\?\"));
        assert!(!ws.canonical_root.to_string_lossy().starts_with(r"\\?\"));

        let explicit = PathBuf::from(format!(r"\\?\{}\test.db", temp.path().display()));
        let located = ws.locate_db(Some(&explicit)).unwrap();
        assert!(!located.to_string_lossy().starts_with(r"\\?\"));
    }

    #[test]
    fn test_trim_trailing_slash_edge_cases() {
        assert_eq!(trim_trailing_slash(Path::new("/")), PathBuf::from("/"));
        assert_eq!(trim_trailing_slash(Path::new("///")), PathBuf::from("/"));
        assert_eq!(
            trim_trailing_slash(Path::new("/a/b/")),
            PathBuf::from("/a/b")
        );
        assert_eq!(
            trim_trailing_slash(Path::new("foo/bar/")),
            PathBuf::from("foo/bar")
        );

        #[cfg(windows)]
        {
            assert_eq!(
                trim_trailing_slash(Path::new("C:\\")),
                PathBuf::from("C:\\")
            );
            assert_eq!(trim_trailing_slash(Path::new("C:/")), PathBuf::from("C:\\"));
            assert_eq!(
                trim_trailing_slash(Path::new("C://")),
                PathBuf::from("C:\\")
            );
            assert_eq!(
                trim_trailing_slash(Path::new("C:\\\\")),
                PathBuf::from("C:\\")
            );
            assert_eq!(
                trim_trailing_slash(Path::new("C:/foo/")),
                PathBuf::from("C:/foo")
            );
        }
    }

    #[test]
    fn test_unicode_and_emoji_uri_safety() {
        // Must not panic on non-ASCII character boundaries
        let p1 = parse_file_uri("file:///a๐Ÿ˜€/x");
        assert!(p1.is_some());

        let p2 = parse_file_uri("file:///c๐Ÿ˜€/x");
        assert!(p2.is_some());

        let p3 = parse_file_uri("file:///localhost๐Ÿ˜€/x");
        assert!(p3.is_some());

        let p4 = parse_file_uri("file://C:/๐Ÿ˜€๐Ÿ˜€/main.rs");
        assert!(p4.is_some());
    }

    #[test]
    #[cfg(unix)]
    fn test_escaping_symlink_rejected() {
        let ws_dir = crate::safe_tempdir();
        let ext_dir = crate::safe_tempdir();

        let ext_file = ext_dir.path().join("secret.txt");
        std::fs::write(&ext_file, "secret").unwrap();

        let symlink_path = ws_dir.path().join("link.txt");
        std::os::unix::fs::symlink(&ext_file, &symlink_path).unwrap();
        let ws = Workspace::new(ws_dir.path().to_path_buf());
        let res = ws.resolve_path(&symlink_path);
        assert!(
            matches!(res, Err(WorkspaceError::PathOutsideWorkspace(..))),
            "Expected PathOutsideWorkspace, got: {res:?}"
        );
    }
}