symforge 8.22.8

SymForge — in-memory code intelligence MCP server for Claude Code, Codex, and Gemini CLI
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
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
use std::io;
use std::path::{Path, PathBuf};

use crate::domain::{AccessErrorKind, ControlStateDir, ControlStatePlacement, ProjectStateDir};

#[must_use]
pub(crate) fn metadata_is_reparse_point(metadata: &std::fs::Metadata) -> bool {
    #[cfg(windows)]
    {
        use std::os::windows::fs::MetadataExt;
        const FILE_ATTRIBUTE_REPARSE_POINT: u32 = 0x400;
        metadata.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT != 0
    }
    #[cfg(not(windows))]
    {
        let _ = metadata;
        false
    }
}

#[must_use]
pub(crate) fn state_directory_entry_type_is_unsafe(
    is_directory: bool,
    is_symlink: bool,
    is_reparse_point: bool,
) -> bool {
    !is_directory || is_symlink || is_reparse_point
}

#[must_use]
pub(crate) fn state_directory_metadata_is_unsafe(metadata: &std::fs::Metadata) -> bool {
    state_directory_entry_type_is_unsafe(
        metadata.is_dir(),
        metadata.file_type().is_symlink(),
        metadata_is_reparse_point(metadata),
    )
}

pub const SYMFORGE_DIR_NAME: &str = ".symforge";

/// Bare db filenames (no `.symforge/` prefix). These are the `name` argument to
/// [`symforge_db_path`] — the ONLY sanctioned way to build a `.symforge/<name>.db`
/// on-disk path. Keeping them bare is the whole point: the prefix lives in exactly
/// one place (the helper), so no store can hand-roll a path and double the prefix
/// (D1-ROOT — the defect that shipped a doubled `root/.symforge/.symforge/...` path).
pub const FRECENCY_DB_NAME: &str = "frecency.db";
pub const COUPLING_DB_NAME: &str = "coupling.db";
pub const ANALYTICS_DB_NAME: &str = "analytics.db";
pub const API_KEYS_DB_NAME: &str = "api-keys.db";
pub const STEL_LEDGER_DB_NAME: &str = "stel-ledger.db";

pub const IDEMPOTENCY_DIR_NAME: &str = "idempotency";
pub const INDEX_SNAPSHOT_QUARANTINE_RELATIVE_PATH: &str = "quarantine/index-snapshots";

pub const SYMFORGE_IDEMPOTENCY_DIR_PATH: &str = ".symforge/idempotency";
pub const SYMFORGE_IDEMPOTENCY_RECORDS_DIR_PATH: &str = ".symforge/idempotency/records";
pub const SYMFORGE_IDEMPOTENCY_QUARANTINE_DIR_PATH: &str = ".symforge/idempotency/quarantine";
pub const SYMFORGE_INDEX_SNAPSHOT_QUARANTINE_DIR_PATH: &str =
    ".symforge/quarantine/index-snapshots";
/// contracts/team-artifact.md § Integrity failure.
pub const SYMFORGE_ARTIFACT_QUARANTINE_DIR_PATH: &str = ".symforge/quarantine/artifacts";

/// OS isolation tag for per-process runtime files (sidecar/daemon port/pid/session).
///
/// This is a pure compile-time constant baked into the binary from its build
/// target (`std::env::consts::OS`): `"windows"`, `"linux"`, `"macos"`, etc. It is
/// NOT a runtime probe, so any two binaries built for the same OS — notably the
/// sidecar/daemon writer and the `symforge hook` reader, which are the SAME crate
/// — always compute the IDENTICAL tag and therefore always agree on filenames.
///
/// Rationale: a Windows symforge and a WSL/Linux symforge can share one physical
/// project-local `.symforge/` directory (a project on a Windows drive opened from
/// both `C:\proj` and `/mnt/c/proj`). Each writes a port that is only valid in its
/// own loopback namespace. Tagging the runtime filenames by OS guarantees neither
/// side ever reads the other's port file. WSL2 reports `"linux"`, which is correct:
/// two Linux processes sharing a dir share the same namespace semantics, so no
/// WSL-vs-native discriminator is needed (adding a `/proc` sniff would make the tag
/// a runtime probe that the Windows side could not reproduce — defeating agreement).
#[must_use]
pub fn os_runtime_tag() -> &'static str {
    std::env::consts::OS
}

/// Build an OS-tagged runtime filename: `sidecar_runtime_file_name("sidecar", "port")`
/// yields e.g. `"sidecar.linux.port"`. The extension is preserved so docs/tools that
/// key on `.port`/`.pid`/`.session` continue to match, and the file stays a sibling
/// in the same `.symforge/` directory.
#[must_use]
pub fn os_tagged_runtime_file_name(stem: &str, ext: &str) -> String {
    format!("{stem}.{tag}.{ext}", tag = os_runtime_tag())
}

/// Resolve the canonical symforge data directory under `base`.
pub fn resolve_symforge_dir(base: &Path) -> PathBuf {
    base.join(SYMFORGE_DIR_NAME)
}

/// Resolve the process-global control-state lane without consulting a source
/// root or launch CWD.
///
/// A relative `SYMFORGE_HOME`, a missing home directory, or an inaccessible
/// private directory yields process-local coordination. It never turns into a
/// relative `.symforge` path.
pub fn resolve_control_state_placement() -> ControlStatePlacement {
    let candidate = std::env::var_os("SYMFORGE_HOME")
        .map(PathBuf::from)
        .or_else(|| dirs::home_dir().map(|home| home.join(SYMFORGE_DIR_NAME)));
    resolve_control_state_placement_with(candidate, prepare_control_state_dir)
}

/// Resolve the process-global control owner exactly once and share that same
/// placement with every control-state reader and writer.
#[must_use]
pub fn process_control_state_placement() -> &'static ControlStatePlacement {
    static PLACEMENT: std::sync::OnceLock<ControlStatePlacement> = std::sync::OnceLock::new();
    PLACEMENT.get_or_init(resolve_control_state_placement)
}

pub(crate) fn resolve_control_state_placement_with<F>(
    candidate: Option<PathBuf>,
    mut prepare: F,
) -> ControlStatePlacement
where
    F: FnMut(&Path) -> Result<(), AccessErrorKind>,
{
    let Some(directory) = candidate else {
        return ControlStatePlacement::ProcessLocal {
            safe_reason: AccessErrorKind::NotFound,
        };
    };

    if !directory.is_absolute() {
        return ControlStatePlacement::ProcessLocal {
            safe_reason: AccessErrorKind::InvalidData,
        };
    }

    match prepare(&directory) {
        Ok(()) => ControlStatePlacement::UserLocal {
            directory: ControlStateDir::new(directory),
        },
        Err(safe_reason) => ControlStatePlacement::ProcessLocal { safe_reason },
    }
}

fn prepare_control_state_dir(directory: &Path) -> Result<(), AccessErrorKind> {
    std::fs::create_dir_all(directory).map_err(|error| access_error_kind(&error))?;

    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;

        std::fs::set_permissions(directory, std::fs::Permissions::from_mode(0o700))
            .map_err(|error| access_error_kind(&error))?;
    }

    Ok(())
}

fn access_error_kind(error: &io::Error) -> AccessErrorKind {
    match error.kind() {
        io::ErrorKind::NotFound => AccessErrorKind::NotFound,
        io::ErrorKind::PermissionDenied => AccessErrorKind::PermissionDenied,
        io::ErrorKind::InvalidData | io::ErrorKind::InvalidInput => AccessErrorKind::InvalidData,
        io::ErrorKind::OutOfMemory => AccessErrorKind::ResourceExhausted,
        _ => AccessErrorKind::Other,
    }
}

/// User-level SymForge home for runtime files when no safe project root exists.
///
/// Honors `SYMFORGE_HOME` when set (the directory itself, no extra nesting).
/// Otherwise uses `~/.symforge`, matching the daemon's global state layout.
pub fn global_symforge_home() -> io::Result<PathBuf> {
    match process_control_state_placement() {
        ControlStatePlacement::UserLocal { directory } => Ok(directory.as_path().to_path_buf()),
        ControlStatePlacement::ProcessLocal { safe_reason } => Err(io::Error::new(
            io_error_kind(*safe_reason),
            format!("durable SymForge control state unavailable: {safe_reason:?}"),
        )),
    }
}

/// Resolve the user-local base for a newly constructed project instance.
///
/// This intentionally does not use [`process_control_state_placement`]: project
/// placement is re-evaluated for each new instance so durability can recover
/// after a transient failure without restarting the process.
pub fn resolve_user_local_project_state_base() -> io::Result<PathBuf> {
    match resolve_control_state_placement() {
        ControlStatePlacement::UserLocal { directory } => Ok(directory.as_path().to_path_buf()),
        ControlStatePlacement::ProcessLocal { safe_reason } => Err(io::Error::new(
            io_error_kind(safe_reason),
            format!("durable user-local project state unavailable: {safe_reason:?}"),
        )),
    }
}

fn io_error_kind(error: AccessErrorKind) -> io::ErrorKind {
    match error {
        AccessErrorKind::NotFound => io::ErrorKind::NotFound,
        AccessErrorKind::PermissionDenied => io::ErrorKind::PermissionDenied,
        AccessErrorKind::InvalidData => io::ErrorKind::InvalidData,
        AccessErrorKind::ResourceExhausted => io::ErrorKind::OutOfMemory,
        AccessErrorKind::Other => io::ErrorKind::Other,
    }
}

/// True when `path` must not host a project-local `.symforge` (sensitive /
/// credential-bearing / too-broad roots). Mirrors the launcher's
/// `discovery::find_project_root` refusal without importing discovery.
fn is_unsafe_data_dir_base(path: &Path) -> bool {
    let canonical = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
    is_sensitive_path(&canonical)
}

/// Choose the base directory for runtime `.symforge` state (sidecar port/pid
/// files, init bootstrap, etc.) without creating it.
///
/// Preference order:
/// 1. `project_root` when supplied and safe
/// 2. Launch cwd when safe (not a sensitive/forbidden root)
/// 3. [`global_symforge_home`]
pub fn select_runtime_data_base(project_root: Option<&Path>, cwd: Option<&Path>) -> PathBuf {
    if let Some(root) = project_root.filter(|p| !is_unsafe_data_dir_base(p)) {
        return resolve_symforge_dir(root);
    }
    if let Some(cwd) = cwd.filter(|p| !is_unsafe_data_dir_base(p)) {
        return resolve_symforge_dir(cwd);
    }
    global_symforge_home().unwrap_or_else(|error| {
        tracing::warn!(
            "falling back to cwd-relative .symforge after global home resolution failed: {error}"
        );
        cwd.as_ref()
            .map(|path| resolve_symforge_dir(path))
            .unwrap_or_else(|| PathBuf::from(SYMFORGE_DIR_NAME))
    })
}

/// Ensure runtime `.symforge` state exists, falling back to [`global_symforge_home`]
/// when the preferred project/cwd base is unsafe or not writable.
pub fn ensure_runtime_symforge_dir(project_root: Option<&Path>) -> io::Result<PathBuf> {
    let cwd = std::env::current_dir().ok();
    let preferred_base = project_root
        .filter(|p| !is_unsafe_data_dir_base(p))
        .map(|p| p.to_path_buf())
        .or_else(|| {
            cwd.as_ref()
                .filter(|p| !is_unsafe_data_dir_base(p))
                .cloned()
        });

    if let Some(base) = preferred_base {
        match ensure_symforge_dir(&base) {
            Ok(dir) => return Ok(dir),
            Err(error) if error.kind() == io::ErrorKind::PermissionDenied => {
                tracing::warn!(
                    path = %base.display(),
                    "cannot create project-local .symforge (permission denied); using global SymForge home"
                );
            }
            Err(error) => return Err(error),
        }
    } else if let Some(ref launch_cwd) = cwd {
        tracing::info!(
            path = %launch_cwd.display(),
            "launch cwd is not a safe project root for .symforge; using global SymForge home"
        );
    }

    global_symforge_home()
}

/// Ensure the canonical symforge data directory exists under `base`.
pub fn ensure_symforge_dir(base: &Path) -> io::Result<PathBuf> {
    let dir = resolve_symforge_dir(base);
    std::fs::create_dir_all(&dir).map_err(|e| {
        io::Error::new(
            e.kind(),
            format!("ensuring symforge data dir at {}: {}", dir.display(), e),
        )
    })?;
    Ok(dir)
}

/// Build the canonical on-disk path for a symforge db: `root/.symforge/<name>`.
///
/// `root` is the project ROOT, NOT the `.symforge` data dir. `name` MUST be a
/// BARE filename (e.g. `"api-keys.db"`) with NO `.symforge/` prefix — this helper
/// owns the single `.symforge` segment. Passing a `.symforge/`-prefixed name, or
/// passing the already-`.symforge` data dir as `root`, would double the prefix to
/// `root/.symforge/.symforge/<name>` — exactly the D1-ROOT defect this helper
/// exists to make impossible. Every db store builds its path through here so the
/// prefix lives in exactly one place.
///
/// This does NOT create the parent directory (SQLite will not create it either);
/// callers that open for write must first run [`ensure_symforge_dir`] (or use
/// [`ensure_symforge_db_path`], which does both).
#[must_use]
pub fn symforge_db_path(root: &Path, name: &str) -> PathBuf {
    debug_assert!(
        !name.contains('/') && !name.contains('\\'),
        "symforge_db_path `name` must be a BARE filename (got {name:?}); \
         the `.symforge/` prefix is owned by this helper"
    );
    root.join(SYMFORGE_DIR_NAME).join(name)
}

/// Ensure `root/.symforge` exists, then return the db path `root/.symforge/<name>`.
///
/// The write-side companion of [`symforge_db_path`]: SQLite will NOT create the
/// parent `.symforge` directory, so a store that opens a db for write calls this
/// to guarantee the parent exists before `Connection::open`. `name` MUST be a
/// BARE filename (same rule as [`symforge_db_path`]).
pub fn ensure_symforge_db_path(root: &Path, name: &str) -> io::Result<PathBuf> {
    ensure_symforge_dir(root)?;
    Ok(symforge_db_path(root, name))
}

/// Build a path owned by an already-resolved project-state directory.
///
/// Unlike [`symforge_db_path`], this helper cannot accept a repository source
/// root and never adds another `.symforge` segment.
#[must_use]
pub fn project_state_path(state_dir: &ProjectStateDir, relative: &str) -> PathBuf {
    debug_assert!(
        !Path::new(relative).is_absolute(),
        "project-state paths must be relative"
    );
    state_dir.as_path().join(relative)
}

/// Ensure the resolved project-state owner exists.
pub fn ensure_project_state_dir(state_dir: &ProjectStateDir) -> io::Result<()> {
    std::fs::create_dir_all(state_dir.as_path()).map_err(|error| {
        io::Error::new(
            error.kind(),
            format!(
                "ensuring project state dir at {}: {error}",
                state_dir.as_path().display()
            ),
        )
    })
}

/// Ensure the resolved project-state owner, then return one bare database path.
pub fn ensure_project_state_db_path(
    state_dir: &ProjectStateDir,
    name: &str,
) -> io::Result<PathBuf> {
    debug_assert!(
        !name.contains('/') && !name.contains('\\'),
        "project-state database name must be bare"
    );
    ensure_project_state_dir(state_dir)?;
    Ok(project_state_path(state_dir, name))
}

/// Build a path owned by the process-global control-state directory.
#[must_use]
pub fn control_state_path(state_dir: &ControlStateDir, relative: &str) -> PathBuf {
    debug_assert!(
        !Path::new(relative).is_absolute(),
        "control-state paths must be relative"
    );
    state_dir.as_path().join(relative)
}

/// Ensure the resolved process-global control-state owner exists.
pub fn ensure_control_state_dir(state_dir: &ControlStateDir) -> io::Result<()> {
    std::fs::create_dir_all(state_dir.as_path()).map_err(|error| {
        io::Error::new(
            error.kind(),
            format!(
                "ensuring control state dir at {}: {error}",
                state_dir.as_path().display()
            ),
        )
    })
}

/// Resolve the canonical idempotency replay directory under `base`.
pub fn resolve_idempotency_dir(base: &Path) -> PathBuf {
    base.join(SYMFORGE_IDEMPOTENCY_DIR_PATH)
}

/// Ensure the canonical idempotency replay directory exists under `base`.
pub fn ensure_idempotency_dir(base: &Path) -> io::Result<PathBuf> {
    let dir = resolve_idempotency_dir(base);
    std::fs::create_dir_all(&dir).map_err(|e| {
        io::Error::new(
            e.kind(),
            format!("ensuring idempotency dir at {}: {}", dir.display(), e),
        )
    })?;
    Ok(dir)
}

/// Resolve the canonical index-snapshot quarantine directory under `base`.
pub fn resolve_index_snapshot_quarantine_dir(base: &Path) -> PathBuf {
    base.join(SYMFORGE_INDEX_SNAPSHOT_QUARANTINE_DIR_PATH)
}

/// Ensure the canonical index-snapshot quarantine directory exists under `base`.
pub fn ensure_index_snapshot_quarantine_dir(base: &Path) -> io::Result<PathBuf> {
    let dir = resolve_index_snapshot_quarantine_dir(base);
    std::fs::create_dir_all(&dir).map_err(|e| {
        io::Error::new(
            e.kind(),
            format!(
                "ensuring index snapshot quarantine dir at {}: {}",
                dir.display(),
                e
            ),
        )
    })?;
    Ok(dir)
}

/// Resolve the canonical team-artifact quarantine directory under `base`
/// (contracts/team-artifact.md § Integrity failure).
pub fn resolve_artifact_quarantine_dir(base: &Path) -> PathBuf {
    base.join(SYMFORGE_ARTIFACT_QUARANTINE_DIR_PATH)
}

/// Ensure the canonical team-artifact quarantine directory exists under `base`.
pub fn ensure_artifact_quarantine_dir(base: &Path) -> io::Result<PathBuf> {
    let dir = resolve_artifact_quarantine_dir(base);
    std::fs::create_dir_all(&dir).map_err(|e| {
        io::Error::new(
            e.kind(),
            format!(
                "ensuring team artifact quarantine dir at {}: {}",
                dir.display(),
                e
            ),
        )
    })?;
    Ok(dir)
}

/// Strip the Windows extended-length / verbatim path prefix (`\\?\` and
/// `\\?\UNC\`) from a path string, returning a plain form suitable for
/// boundary matching. On non-prefixed input the string is returned
/// unchanged. This is intentionally string-level (not `Path::components`),
/// because `std::path` parses verbatim prefixes into opaque `Prefix`
/// components on Windows that do not normalize separators or case the way
/// sensitive-root matching requires.
fn strip_verbatim_prefix(s: &str) -> &str {
    if let Some(rest) = s.strip_prefix(r"\\?\UNC\") {
        // `\\?\UNC\server\share\...` denotes `\\server\share\...`. The
        // leading `\\` is irrelevant for sensitive-root matching (it is a
        // network share, never a local system root), so the share-relative
        // remainder is sufficient.
        rest
    } else if let Some(rest) = s.strip_prefix(r"\\?\") {
        rest
    } else {
        s
    }
}

/// Sensitive top-level Windows system directories (lowercased). Matching the
/// first non-drive component of a path on a boundary (not a substring) blocks
/// the root *and* every subdirectory (`...\Windows\System32\...`). `system32`
/// is listed defensively even though it normally lives under `windows`.
/// `programdata` is credential/state-bearing (machine-wide secrets, package
/// caches) and is blocked together with its descendants.
const WINDOWS_SENSITIVE_SEGMENTS: &[&str] = &[
    "windows",
    "program files",
    "program files (x86)",
    "programdata",
    "system32",
];

/// Returns true if the first non-drive component of an already-lowercased,
/// `/`-separated component slice names a sensitive Windows system directory.
/// `comps[0]` is expected to be the drive (e.g. `c:`); the system directory is
/// `comps[1]`. Used by both the native Windows arm and the WSL mount check.
fn is_windows_sensitive_under_drive(comps: &[&str]) -> bool {
    comps
        .get(1)
        .is_some_and(|first| WINDOWS_SENSITIVE_SEGMENTS.contains(first))
}

/// Returns true if an already-lowercased, `/`-separated component slice points
/// at the Windows user-profile container or a *bare* profile root — both of
/// which are credential-bearing (each `C:\Users\<name>` holds `AppData` OAuth
/// tokens, `.ssh`, `.aws`, `.npmrc`) and must never be indexed wholesale.
///
/// `comps[0]` is the drive (e.g. `c:`). The rule, mirrored on Unix `/home` and
/// `/Users`, blocks exactly two shapes and *allows* anything deeper so genuine
/// projects stay indexable:
///
/// - `[drive, "users"]`           → the profile container (`C:\Users`)         BLOCK
/// - `[drive, "users", <name>]`   → a bare profile root (`C:\Users\alice`)     BLOCK
/// - `[drive, "users", <name>, …]`→ a project under a profile                  ALLOW
///
/// The deeper-than-profile allowance is load-bearing: every normal user keeps
/// repos at `C:\Users\<name>\...`, and the launcher already relies on indexing
/// them, so blocking the bare root must not block its descendants.
fn is_windows_user_container(comps: &[&str]) -> bool {
    matches!(comps.get(1), Some(&"users")) && comps.len() <= 3
}

/// Returns true if `canonical` is a sensitive system or credential-bearing
/// directory (or a descendant of one) that must never be indexed.
///
/// This is the single, canonical trust-boundary guard for *both* attacker-
/// facing index entrypoints (`tools::index_folder`, `daemon::
/// index_folder_for_session`, `daemon::open_project_session`) *and* the trusted
/// launcher (`discovery::is_forbidden_root` delegates here). Keeping one guard
/// means the tool surface can never drift weaker than the launcher again — the
/// exact drift that caused the original daemon bypass.
///
/// Two block classes:
///
/// 1. **System roots — root and every descendant blocked.** `/`, `/etc`,
///    `/proc`, `/usr`, `C:\Windows`, `System32`, `Program Files`,
///    `C:\ProgramData`, the bare drive root, etc. Indexing these reads system
///    files and, on some hosts, drives a reload into a denial-of-service.
///
/// 2. **User-profile containers — container and *bare* profile root blocked,
///    descendants allowed.** `C:\Users`, `C:\Users\<name>`, `/home`,
///    `/home/<name>`, `/Users`, `/Users/<name>`. Each profile root holds
///    credentials (`AppData` OAuth tokens, `.ssh`, `.aws`, `.npmrc`), so
///    indexing it wholesale is credential exfiltration. But real projects live
///    *under* a profile (`C:\Users\<name>\projects\repo`), so anything deeper
///    than the bare profile root stays indexable. Blocking those would be a DoS
///    for every user.
///
/// Matching is component-boundary aware: a project path that merely *contains*
/// a sensitive segment as a substring (e.g. `C:\Users\me\my-windows-project`,
/// `C:\Users\me\system32-emulator`, or `/home/me/etcd-client`) is allowed. On
/// Windows the `\\?\` extended-length prefix is stripped before matching so
/// canonicalized paths (which carry it) are not silently waved through. On Unix
/// the WSL Windows mount (`/mnt/<drive>`, `/mnt/<drive>/Users`,
/// `/mnt/<drive>/Windows`, …) is blocked with the same container semantics.
#[must_use]
pub fn is_sensitive_path(canonical: &Path) -> bool {
    let raw = canonical.to_string_lossy();
    // On Unix the canonical form has no verbatim prefix, but strip defensively
    // in case a Windows-style path is ever routed through this code path.
    let stripped = strip_verbatim_prefix(&raw);

    #[cfg(windows)]
    {
        // Normalize separators so component splitting is uniform regardless
        // of whether the source used `\` (canonicalize) or `/` (user input).
        let lower = stripped.replace('\\', "/").to_ascii_lowercase();

        // Split into non-empty path components. The first component of an
        // absolute Windows path is the drive (e.g. `c:`).
        let comps: Vec<&str> = lower.split('/').filter(|c| !c.is_empty()).collect();

        // Bare drive root: `c:` / `c:\` / `c:/` → exactly one component that
        // looks like a drive letter.
        if let [drive] = comps.as_slice()
            && drive.len() == 2
            && drive.ends_with(':')
            && drive.as_bytes()[0].is_ascii_alphabetic()
        {
            return true;
        }

        is_windows_sensitive_under_drive(&comps) || is_windows_user_container(&comps)
    }

    #[cfg(unix)]
    {
        let path = Path::new(stripped);

        // Class 1 — system roots: blocked together with every descendant,
        // matched on path components (not raw substring), so `/etc/x` and
        // `/usr/lib/y` are caught while `/home/etc-notes` is not. `/root` is a
        // privileged home holding `.ssh`/`.aws` and is treated as a system root
        // (no legitimate project container nests directly under it).
        const BLOCKED_RECURSIVE: &[&str] = &[
            "/bin", "/boot", "/dev", "/etc", "/lib", "/lib64", "/proc", "/run", "/sbin", "/sys",
            "/usr", "/var", "/root", "/Library", "/System", "/private",
        ];

        // Class 2 — block the bare root only; a real project legitimately nests
        // one level under these (`/opt/app`, `/srv/site`, `/tmp/build`), so only
        // the container itself is refused. `/tmp` in particular MUST stay
        // root-only: TempDir-based tests create project dirs under `/tmp`.
        const BLOCKED_ROOT_ONLY: &[&str] = &["/opt", "/srv", "/media", "/tmp", "/snap"];

        // Root `/` itself: a path with no normal components.
        let has_normal_component = path
            .components()
            .any(|c| matches!(c, std::path::Component::Normal(_)));
        if path.is_absolute() && !has_normal_component {
            return true;
        }

        for blocked in BLOCKED_RECURSIVE {
            if path == Path::new(blocked) || path.starts_with(blocked) {
                return true;
            }
        }

        for blocked in BLOCKED_ROOT_ONLY {
            if path == Path::new(blocked) {
                return true;
            }
        }

        let lower = stripped.to_ascii_lowercase();
        let comps: Vec<&str> = lower.split('/').filter(|c| !c.is_empty()).collect();

        // Class 2 — user-profile containers (`/home`, `/Users`): block the
        // container and a *bare* profile root, allow anything deeper so genuine
        // projects (`/home/<name>/repo`) stay indexable. Matched on lowercased
        // components for consistency with the Windows `Users` rule.
        if let [container, rest @ ..] = comps.as_slice()
            && (*container == "home" || *container == "users")
            && rest.len() <= 1
        {
            return true;
        }

        // WSL Windows mount under `/mnt/<drive>`. The bare drive mount and the
        // Windows profile container/root surface a huge, credential-bearing tree
        // over a slow DrvFs mount; the Windows system dirs map onto the host's.
        // Block:
        //   /mnt                              (bare automount root)
        //   /mnt/<drive>                      (bare Windows drive root)
        //   /mnt/<drive>/Users               (profile container)
        //   /mnt/<drive>/Users/<name>        (bare profile root)
        //   /mnt/<drive>/Windows|System32|…  (host system dirs)
        // Allow `/mnt/<drive>/Users/<name>/…` and `/mnt/<drive>/<non-Users>/…`.
        if comps.first() == Some(&"mnt") {
            match comps.as_slice() {
                // Bare `/mnt`.
                [_mnt] => return true,
                [_mnt, drive, rest @ ..]
                    if drive.len() == 1 && drive.as_bytes()[0].is_ascii_alphabetic() =>
                {
                    // Bare drive root `/mnt/<drive>`.
                    if rest.is_empty() {
                        return true;
                    }
                    // Windows profile container/root under the drive mount,
                    // reusing the shared `[drive, users, …]` shape semantics.
                    let mut drive_view = Vec::with_capacity(rest.len() + 1);
                    drive_view.push(*drive);
                    drive_view.extend_from_slice(rest);
                    if is_windows_user_container(&drive_view) {
                        return true;
                    }
                    // Windows system dirs mapped onto host (`/mnt/c/Windows`, …).
                    if is_windows_sensitive_under_drive(&drive_view) {
                        return true;
                    }
                }
                _ => {}
            }
        }

        false
    }
}

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

    use tempfile::TempDir;

    #[test]
    fn every_state_consumer_requires_its_typed_owner() {
        let project_owned = [
            ("idempotency replay", include_str!("idempotency.rs")),
            (
                "coupling store",
                include_str!("live_index/coupling/lifecycle.rs"),
            ),
            ("frecency store", include_str!("live_index/frecency.rs")),
            ("STEL ledger", include_str!("stel_core/ledger_store.rs")),
            ("analytics store", include_str!("analytics/store.rs")),
            ("API-key store", include_str!("server/api_keys.rs")),
            ("edit-safety TEE", include_str!("edit_safety/tee.rs")),
        ];
        let control_owned = [
            ("edit-safety trust", include_str!("edit_safety/trust.rs")),
            ("sidecar port file", include_str!("sidecar/port_file.rs")),
            ("sidecar server", include_str!("sidecar/server.rs")),
            ("daemon control", include_str!("daemon.rs")),
            ("hook adoption", include_str!("cli/hook.rs")),
            ("operator profile", include_str!("cli/operator_profile.rs")),
            ("onboarding state", include_str!("cli/onboarding.rs")),
            ("runtime startup", include_str!("server/serve.rs")),
            ("version registry", include_str!("version_registry.rs")),
            ("updater", include_str!("cli/update.rs")),
        ];

        let mut violations = Vec::new();
        for (consumer, source) in project_owned {
            if !source.contains("ProjectStateDir") {
                violations.push(format!(
                    "{consumer} does not require ProjectStateDir from its resolved project owner"
                ));
            }
        }
        for (consumer, source) in control_owned {
            if !source.contains("ControlStateDir") {
                violations.push(format!(
                    "{consumer} does not require ControlStateDir from its resolved process owner"
                ));
            }
        }

        let forbidden = [
            (
                "sidecar port file",
                include_str!("sidecar/port_file.rs"),
                "select_runtime_data_base(",
            ),
            (
                "sidecar server",
                include_str!("sidecar/server.rs"),
                "select_runtime_data_base(",
            ),
            (
                "updater",
                include_str!("cli/update.rs"),
                "Path::new(\".symforge\")",
            ),
            (
                "edit-safety TEE",
                include_str!("edit_safety/tee.rs"),
                "ensure_symforge_dir(&self.repo_root)",
            ),
        ];
        for (consumer, source, legacy_path) in forbidden {
            if source.contains(legacy_path) {
                violations.push(format!(
                    "{consumer} still reconstructs state through forbidden `{legacy_path}`"
                ));
            }
        }

        assert!(
            violations.is_empty(),
            "typed-owner inventory violations:\n{}",
            violations.join("\n")
        );
    }

    #[test]
    fn control_state_never_falls_back_to_launch_cwd_or_relative_symforge() {
        let mut prepare_calls = 0;
        let relative =
            resolve_control_state_placement_with(Some(PathBuf::from(SYMFORGE_DIR_NAME)), |_| {
                prepare_calls += 1;
                Ok(())
            });
        assert_eq!(
            relative,
            crate::domain::ControlStatePlacement::ProcessLocal {
                safe_reason: crate::domain::AccessErrorKind::InvalidData,
            }
        );
        assert_eq!(
            prepare_calls, 0,
            "relative control state must not be probed"
        );

        let unavailable = resolve_control_state_placement_with(None, |_| {
            prepare_calls += 1;
            Ok(())
        });
        assert_eq!(
            unavailable,
            crate::domain::ControlStatePlacement::ProcessLocal {
                safe_reason: crate::domain::AccessErrorKind::NotFound,
            }
        );
        assert_eq!(
            prepare_calls, 0,
            "missing user-local state must remain process-local, never inspect launch CWD"
        );

        let launch_cwd = TempDir::new().unwrap();
        let denied_control_dir = launch_cwd.path().join("private-control-state");
        let denied = resolve_control_state_placement_with(Some(denied_control_dir), |_| {
            prepare_calls += 1;
            Err(crate::domain::AccessErrorKind::PermissionDenied)
        });
        assert_eq!(
            denied,
            crate::domain::ControlStatePlacement::ProcessLocal {
                safe_reason: crate::domain::AccessErrorKind::PermissionDenied,
            }
        );
        assert_eq!(
            prepare_calls, 1,
            "the chosen user-local path is probed once"
        );
        assert!(
            !launch_cwd.path().join(SYMFORGE_DIR_NAME).exists(),
            "an unavailable user-local directory must not create a launch-CWD fallback"
        );
    }

    #[test]
    fn select_runtime_data_base_prefers_project_root_over_launch_cwd() {
        let tmp = TempDir::new().unwrap();
        let project = tmp.path().join("repo");
        std::fs::create_dir_all(&project).unwrap();
        let chosen = select_runtime_data_base(Some(&project), Some(Path::new("/tmp")));
        assert_eq!(chosen, resolve_symforge_dir(&project));
    }

    #[cfg(windows)]
    #[test]
    fn select_runtime_data_base_skips_system32_launch_cwd() {
        let chosen = select_runtime_data_base(None, Some(Path::new(r"C:\Windows\System32")));
        assert!(
            !chosen
                .to_string_lossy()
                .to_ascii_lowercase()
                .contains("system32"),
            "System32 launch cwd must not host .symforge; got {}",
            chosen.display()
        );
    }

    #[test]
    fn test_resolve_symforge_dir_prefers_existing_canonical_dir() {
        let tmp = TempDir::new().unwrap();
        let symforge_dir = tmp.path().join(SYMFORGE_DIR_NAME);
        std::fs::create_dir_all(&symforge_dir).unwrap();

        let resolved = resolve_symforge_dir(tmp.path());

        assert_eq!(resolved, symforge_dir);
    }

    #[test]
    fn test_ensure_symforge_dir_creates_canonical_dir_when_missing() {
        let tmp = TempDir::new().unwrap();

        let dir = ensure_symforge_dir(tmp.path()).unwrap();

        assert_eq!(dir, tmp.path().join(SYMFORGE_DIR_NAME));
        assert!(dir.exists(), "canonical directory should be created");
    }

    /// D1-ROOT regression: every db store now builds its on-disk path through
    /// [`symforge_db_path`] with a BARE filename. Under the PRODUCTION calling
    /// convention (pass the project ROOT, not the `.symforge` data dir) the path
    /// is the SINGLE-prefixed `root/.symforge/<name>.db`, and the DOUBLED
    /// `root/.symforge/.symforge/<name>.db` is never produced. This is the check
    /// the old per-store tests skipped — they passed a different arg than the
    /// production caller, which is exactly how the doubled-path bug hid (D1/D7).
    #[test]
    fn symforge_db_path_is_single_prefixed_never_doubled() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();
        for name in [
            FRECENCY_DB_NAME,
            COUPLING_DB_NAME,
            ANALYTICS_DB_NAME,
            API_KEYS_DB_NAME,
            STEL_LEDGER_DB_NAME,
        ] {
            let got = symforge_db_path(root, name);
            assert_eq!(
                got,
                root.join(SYMFORGE_DIR_NAME).join(name),
                "{name}: production path must be single-prefixed root/.symforge/<name>"
            );
            let doubled = root
                .join(SYMFORGE_DIR_NAME)
                .join(SYMFORGE_DIR_NAME)
                .join(name);
            assert_ne!(
                got, doubled,
                "{name}: helper must never produce the doubled root/.symforge/.symforge/<name> path"
            );
        }
    }

    /// `ensure_symforge_db_path` creates the parent `.symforge` dir (SQLite will
    /// not) and returns the same single-prefixed path as `symforge_db_path`.
    #[test]
    fn ensure_symforge_db_path_creates_parent_and_returns_single_prefixed() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();
        let db = ensure_symforge_db_path(root, ANALYTICS_DB_NAME).unwrap();
        assert_eq!(db, root.join(SYMFORGE_DIR_NAME).join(ANALYTICS_DB_NAME));
        assert!(
            db.parent().unwrap().is_dir(),
            "parent .symforge dir must exist after ensure_symforge_db_path"
        );
    }

    #[test]
    fn test_idempotency_paths_stay_under_canonical_symforge_dir() {
        let tmp = TempDir::new().unwrap();

        assert_eq!(
            resolve_idempotency_dir(tmp.path()),
            tmp.path().join(SYMFORGE_DIR_NAME).join("idempotency")
        );
        assert_eq!(
            tmp.path().join(SYMFORGE_IDEMPOTENCY_RECORDS_DIR_PATH),
            resolve_idempotency_dir(tmp.path()).join("records")
        );
        assert_eq!(
            tmp.path().join(SYMFORGE_IDEMPOTENCY_QUARANTINE_DIR_PATH),
            resolve_idempotency_dir(tmp.path()).join("quarantine")
        );
    }

    #[test]
    fn test_index_snapshot_quarantine_path_stays_under_canonical_symforge_dir() {
        let tmp = TempDir::new().unwrap();

        assert_eq!(
            resolve_index_snapshot_quarantine_dir(tmp.path()),
            tmp.path()
                .join(SYMFORGE_DIR_NAME)
                .join("quarantine")
                .join("index-snapshots")
        );
    }

    #[test]
    fn test_artifact_quarantine_path_stays_under_canonical_symforge_dir() {
        let tmp = TempDir::new().unwrap();

        assert_eq!(
            resolve_artifact_quarantine_dir(tmp.path()),
            tmp.path()
                .join(SYMFORGE_DIR_NAME)
                .join("quarantine")
                .join("artifacts")
        );
        let dir = ensure_artifact_quarantine_dir(tmp.path()).unwrap();
        assert!(dir.is_dir(), "artifact quarantine dir should be created");
    }

    #[test]
    fn strip_verbatim_prefix_handles_plain_unc_and_verbatim() {
        assert_eq!(strip_verbatim_prefix(r"\\?\C:\Windows"), r"C:\Windows");
        assert_eq!(
            strip_verbatim_prefix(r"\\?\UNC\server\share\proj"),
            r"server\share\proj"
        );
        assert_eq!(
            strip_verbatim_prefix(r"C:\Users\me\proj"),
            r"C:\Users\me\proj"
        );
        assert_eq!(
            strip_verbatim_prefix("/home/user/project"),
            "/home/user/project"
        );
    }

    #[cfg(windows)]
    #[test]
    fn is_sensitive_path_windows_blocks_system_roots_and_subdirs() {
        let blocked = [
            r"\\?\C:\Windows\System32",
            r"\\?\C:\Windows",
            r"C:\Windows",
            "C:/Windows",
            r"C:\Windows\System32\drivers",
            r"C:\Program Files\X",
            r"C:\Program Files (x86)\Y\bin",
            // ProgramData is machine-wide credential/state storage.
            r"C:\ProgramData",
            r"C:\ProgramData\some\creds",
            r"C:\",
            "C:/",
            r"D:\",
        ];
        for path in blocked {
            assert!(
                is_sensitive_path(Path::new(path)),
                "expected `{path}` to be refused as sensitive"
            );
        }
    }

    #[cfg(windows)]
    #[test]
    fn is_sensitive_path_windows_blocks_user_profile_container_and_root() {
        // The profile container and a bare profile root are credential-bearing
        // (each holds AppData OAuth tokens, .ssh, .aws, .npmrc) and must be
        // refused — but a project *under* a profile must stay indexable (asserted
        // separately in the allow test). Verbatim-prefixed forms must also block.
        let blocked = [
            r"C:\Users",
            "C:/Users",
            r"\\?\C:\Users",
            r"C:\Users\victim",
            "C:/Users/victim",
            r"\\?\C:\Users\victim",
        ];
        for path in blocked {
            assert!(
                is_sensitive_path(Path::new(path)),
                "expected `{path}` to be refused as sensitive (profile container/root)"
            );
        }
    }

    #[cfg(windows)]
    #[test]
    fn is_sensitive_path_windows_allows_normal_project_paths() {
        let allowed = [
            // A project under a profile — the no-false-positive case. Blocking
            // these would be a DoS for every user who keeps repos under Users.
            r"\\?\C:\Users\me\project",
            r"C:\Users\me\project",
            r"C:\Users\me\projects\repo",
            // Substring of a blocked segment must NOT false-positive.
            r"C:\Users\me\my-windows-project",
            // Leaf that merely *contains* a blocked segment name, one level under
            // the profile, must be allowed (component-boundary, not substring).
            r"C:\Users\me\system32-emulator",
            r"C:\dev\windows-tools\src",
            r"\\?\UNC\server\share\proj",
        ];
        for path in allowed {
            assert!(
                !is_sensitive_path(Path::new(path)),
                "expected `{path}` to be allowed (not sensitive)"
            );
        }
    }

    #[cfg(unix)]
    #[test]
    fn is_sensitive_path_unix_blocks_roots_and_subdirs() {
        let blocked = [
            "/",
            "/etc",
            "/etc/passwd",
            // Task matrix: an arbitrary subdir of a system root stays blocked.
            "/etc/sub",
            "/usr",
            "/usr/lib/x",
            // Privileged / credential-bearing homes and macOS system roots.
            "/root",
            "/Library",
            "/System",
            "/private",
            // User-profile containers and bare profile roots (credential-bearing).
            "/home",
            "/home/victim",
            "/Users",
            "/Users/victim",
            // Mount/temp/optional containers — bare root only.
            "/mnt",
            "/opt",
            "/srv",
            "/media",
            "/tmp",
            "/snap",
            // WSL drive mount roots and Windows system/profile dirs mapped onto host.
            "/mnt/c",
            "/mnt/c/Users",
            "/mnt/c/Users/victim",
            "/mnt/c/Windows",
            "/mnt/c/Windows/System32",
            "/mnt/c/Program Files/X",
            "/mnt/c/ProgramData",
        ];
        for path in blocked {
            assert!(
                is_sensitive_path(Path::new(path)),
                "expected `{path}` to be refused as sensitive"
            );
        }
    }

    #[cfg(unix)]
    #[test]
    fn is_sensitive_path_unix_allows_normal_project_paths() {
        let allowed = [
            // The no-false-positive cases: real projects under a profile / home.
            "/home/user/project",
            "/home/me/repo",
            // Substring of a blocked root must NOT false-positive.
            "/home/user/etc-notes",
            "/home/user/usr-local-clone",
            // Leaf merely *contains* a blocked name, one level under the home.
            "/home/me/etcd-client",
            // Project nested one level under a root-only container.
            "/opt/app",
            "/srv/site",
            "/tmp/build/proj",
            // A legit WSL project under a drive mount must be allowed.
            "/mnt/c/Users/me/project",
            "/mnt/c/Users/me/repo",
            "/mnt/d/dev/windows-tools",
            // Non-Users subtree of a drive mount stays indexable.
            "/mnt/c/code/proj",
        ];
        for path in allowed {
            assert!(
                !is_sensitive_path(Path::new(path)),
                "expected `{path}` to be allowed (not sensitive)"
            );
        }
    }
}