lean-ctx-engine 3.12.11

LeanCTX | OSS library: the open-source runtime for the Context Gateway for AI agents behind the lean-ctx CLI, MCP server and proxy integrations.
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
// SPDX-License-Identifier: Apache-2.0
//! Project auto-binding for sessions that start without a project.
//!
//! GUI and roots-less hosts start the MCP server in `~` (or an agent config
//! directory) and report no project, so walks refuse and the agent stalls on
//! "ask the user to run allow-path". When such a session names its first
//! absolute path inside a project, the server adopts that project as the
//! session root — the same writable session project the #580/#649 re-root
//! has always produced, now chosen by a hardened predicate, once per session,
//! and audited.
//!
//! Security model:
//! - [`adoptable_root`] decides which directory may be adopted. It resolves the
//!   candidate before any check: the longest existing prefix is canonicalized
//!   with the failing (never "or self") security canonicalizer, so symlinks,
//!   `..` and letter case are resolved where the path lands; a `..` or `.` in
//!   the missing remainder is refused outright, and a prefix that cannot be
//!   resolved fails closed. The chosen root is canonicalized again before every
//!   acceptance check and returned canonical, so the session stores the real
//!   directory, never the spelling the agent sent.
//! - It refuses the home directory, `/`, system directories, every protected
//!   home zone (`~/.ssh`, `~/.aws`, `~/.gnupg`, `~/Library` keychains, any other
//!   dot folder), secret-like paths, any root that contains the home directory,
//!   other users' homes, directories not owned by the home's owner (Unix),
//!   LeanCTX's own stores and agent config directories. A path without a
//!   project marker is never adopted, nor is a path inside VCS metadata
//!   (`.git/hooks/pre-commit` names no project to write in). macOS
//!   `/Users/Shared` is not another user's home and may hold a project; the
//!   ownership check still applies there.
//! - The ownership check is the planted-marker defence: a `.git` or
//!   `package.json` another account (or root) dropped into a world-writable
//!   tree must not turn that tree into this user's writable session project.
//!   It compares owners with the home directory's, so it holds under `sudo`
//!   with the invoking user's `HOME` as well. Windows has no such check yet.
//! - The root is the OUTERMOST project marker below home, matching
//!   `detect_project_root` (#1437): a monorepo sub-package or a submodule
//!   adopts its enclosing repository. A directory directly below home becomes
//!   the root only as a git repository: a stray `~/Documents/package.json`
//!   never makes all of `~/Documents` writable.
//! - Without a usable home (`HOME=/`, or a home that does not resolve) this
//!   adoption fails closed: [`adoptable_root`] adopts nothing and
//!   [`hardened_adoption_available`] is `false`. The caller then keeps its
//!   previous re-root verbatim, so such hosts are never worse off than before.
//! - Agent config directories get no widening here: their read-only admission
//!   stays the per-client #899 rule (`detect_agent_instruction_root`,
//!   `detect_host_session_output_root`, `detect_host_scratch_root`).
//! - A path in VCS metadata or an execution hook (`.git/hooks/pre-commit`,
//!   `.githooks/`, `.husky/`, `.envrc`, `.vscode/tasks.json`) names no project
//!   to adopt. After an adoption, [`adopted_write_denial`] keeps refusing writes
//!   there (and below git's `core.hooksPath`) until the user declares the root:
//!   a prompt-injected first read must not open the hooks that run code on the
//!   user's next commit or `cd`.
//! - Personal containers (`~/Documents`, `~/Desktop`, `~/Downloads`) and
//!   their direct children are roots only as git repositories.
//! - [`record_autobind`] commits the adoption to the local audit trail with
//!   the adopted root as a non-blocking `x-project_adopted` event; no tool
//!   output carries it (output determinism #498).

use std::path::{Component, Path, PathBuf};

/// System directories a project is never adopted from, also when the user's
/// home directory is elsewhere. Compared on canonical paths. `/Volumes` is
/// deliberately absent: on macOS it holds external and secondary drives,
/// where users keep real checkouts.
#[cfg(unix)]
const SYSTEM_DIRS: &[&str] = &[
    "/bin",
    "/boot",
    "/dev",
    "/etc",
    "/lib",
    "/lib32",
    "/lib64",
    "/libx32",
    "/proc",
    "/run",
    "/sbin",
    "/sys",
    "/usr",
    "/var/lib",
    "/var/log",
    "/var/run",
    "/var/db",
    "/var/spool",
    "/var/cache",
    "/opt/homebrew",
    "/System",
    "/Library",
    "/Applications",
    "/private/etc",
    "/private/var/db",
    "/private/var/log",
    "/private/var/root",
    "/cores",
];

#[cfg(windows)]
const SYSTEM_DIRS: &[&str] = &[
    "C:\\Windows",
    "C:\\Program Files",
    "C:\\Program Files (x86)",
    "C:\\ProgramData",
];

/// The real directory behind `path`, or `None` when it cannot be resolved:
/// fails closed instead of handing back the unresolved spelling.
fn real_dir(path: &Path) -> Option<PathBuf> {
    crate::core::pathutil::canonicalize_secure(path).ok()
}

/// The canonical home directory, `None` when it is the filesystem root.
fn canonical_home() -> Option<PathBuf> {
    canonical_home_of(&dirs::home_dir()?)
}

/// `home` resolved, or `None` when it does not resolve or is the filesystem
/// root (`HOME=/`, `C:\`).
fn canonical_home_of(home: &Path) -> Option<PathBuf> {
    let home = real_dir(home)?;
    home.parent().is_some().then_some(home)
}

/// `true` when [`adoptable_root`] can judge candidates: the home directory
/// resolves and is not `/`. Otherwise the caller keeps its previous behaviour,
/// so `HOME=/` never disables project binding.
pub fn hardened_adoption_available() -> bool {
    canonical_home().is_some()
}

/// Shared folders below the homes directory (macOS `/Users/Shared`): no
/// account's home, so a project there is judged like any other (ownership
/// included).
#[cfg(target_os = "macos")]
const SHARED_AREAS: &[&str] = &["/Users/Shared"];
#[cfg(not(target_os = "macos"))]
const SHARED_AREAS: &[&str] = &[];

/// Version-control metadata: a path inside it (`.git/hooks/pre-commit`)
/// names no project to adopt.
const VCS_METADATA: &[&str] = &[".git", ".hg", ".svn", ".jj"];

/// Folders whose files a tool runs on its own (git hook managers).
const HOOK_DIRS: &[&str] = &[".githooks", ".husky"];

/// Files a tool runs on its own: direnv on `cd`, VS Code tasks on open.
const HOOK_FILES: &[&[&str]] = &[&[".envrc"], &[".vscode", "tasks.json"]];

/// Folders below the home directory that hold personal files, not projects:
/// they and their direct children are roots only as git repositories.
const PERSONAL_CONTAINERS: &[&str] = &["Documents", "Desktop", "Downloads"];

fn same_name(a: &std::ffi::OsStr, b: &str) -> bool {
    a.to_string_lossy().eq_ignore_ascii_case(b)
}

/// `true` when `path` lies in VCS metadata or a hook folder, or is a hook
/// file: a write there runs code later, outside any tool call.
fn names_vcs_or_hook(path: &Path) -> bool {
    let names: Vec<&std::ffi::OsStr> = path
        .components()
        .filter_map(|part| match part {
            Component::Normal(name) => Some(name),
            _ => None,
        })
        .collect();
    let in_dir = names.iter().any(|name| {
        VCS_METADATA
            .iter()
            .chain(HOOK_DIRS)
            .any(|meta| same_name(name, meta))
    });
    in_dir
        || HOOK_FILES.iter().any(|file| {
            names.len() >= file.len()
                && names[names.len() - file.len()..]
                    .iter()
                    .zip(file.iter())
                    .all(|(name, want)| same_name(name, want))
        })
}

/// Hook directories named by git's `core.hooksPath` in `root/.git/config`,
/// absolute. Read only when `.git` is a plain directory; anything unreadable
/// yields none (the fixed list still applies).
fn configured_hook_dirs(root: &Path) -> Vec<PathBuf> {
    const MAX_CONFIG_BYTES: u64 = 64 * 1024;
    let config = root.join(".git").join("config");
    let small = std::fs::symlink_metadata(&config)
        .is_ok_and(|meta| meta.is_file() && meta.len() <= MAX_CONFIG_BYTES);
    let Some(text) = small
        .then(|| std::fs::read_to_string(&config).ok())
        .flatten()
    else {
        return Vec::new();
    };
    let mut in_core = false;
    let mut out = Vec::new();
    for line in text.lines() {
        let line = line.trim();
        if line.starts_with('[') {
            let header = line.trim_matches(|c| c == '[' || c == ']').trim();
            in_core = header.eq_ignore_ascii_case("core");
            continue;
        }
        let Some((key, value)) = line.split_once('=') else {
            continue;
        };
        if !in_core || !key.trim().eq_ignore_ascii_case("hookspath") {
            continue;
        }
        let value = value.trim().trim_matches('"');
        let dir = match value.strip_prefix("~/") {
            Some(rest) => match dirs::home_dir() {
                Some(home) => home.join(rest),
                None => continue,
            },
            None => root.join(value),
        };
        out.push(crate::core::pathjail::canonical_target(&dir));
    }
    out
}

/// Projects this process adopted from a first path (canonical). Bounded by
/// one adoption per session.
static ADOPTED_ROOTS: std::sync::Mutex<Vec<PathBuf>> = std::sync::Mutex::new(Vec::new());

/// Marks `root` as adopted, not declared: writes into its VCS metadata and
/// execution hooks are refused until the user declares it.
pub fn mark_adopted(root: &Path) {
    let mut roots = ADOPTED_ROOTS
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner);
    if !roots.iter().any(|known| known == root) {
        roots.push(root.to_path_buf());
    }
}

/// The refusal for a write to canonical `target` inside an adopted (never
/// declared) project's VCS metadata or execution hooks, else `None`. A root
/// the user declared with `lean-ctx allow-path` is theirs to write anywhere.
pub fn adopted_write_denial(target: &Path) -> Option<String> {
    let roots = ADOPTED_ROOTS
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
        .clone();
    let root = roots.into_iter().find(|root| target.starts_with(root))?;
    let inside = target.strip_prefix(&root).ok()?;
    let guarded = names_vcs_or_hook(inside)
        || configured_hook_dirs(&root)
            .iter()
            .any(|hooks| target.starts_with(hooks));
    if !guarded {
        return None;
    }
    let declared = crate::core::pathjail::allow_paths_from_env_and_config()
        .iter()
        .any(|allowed| root.starts_with(allowed));
    if declared {
        return None;
    }
    Some(format!(
        "Write refused: {} is version-control metadata or an execution hook of {}, a project \
         this session adopted from its first path; neither the host nor the user declared it. \
         Agent: edit project files instead. To allow writes there, the user runs in their \
         terminal: lean-ctx allow-path {} — takes effect immediately, no restart.",
        target.display(),
        root.display(),
        root.display()
    ))
}

/// Compared by components: canonical Windows paths come back as `C:/Windows/…`
/// (verbatim prefix stripped, `/` separators) while the list spells
/// `C:\Windows`, so a string prefix never matched there. Windows compares
/// each component without letter case. `path` is always the output of
/// `real_dir`, which never keeps a `\\?\` prefix.
fn under_system_dir(path: &Path, system_dirs: &[&Path]) -> bool {
    system_dirs.iter().any(|dir| {
        #[cfg(windows)]
        {
            let mut parts = path.components();
            dir.components().all(|want| {
                parts.next().is_some_and(|have| {
                    have.as_os_str()
                        .to_string_lossy()
                        .eq_ignore_ascii_case(&want.as_os_str().to_string_lossy())
                })
            })
        }
        #[cfg(not(windows))]
        {
            path.starts_with(dir)
        }
    })
}

/// `true` when `dir` is owned by the same user as `home` (Unix): never adopt
/// a root-owned tree or another account's checkout.
#[cfg(unix)]
fn owned_like_home(dir: &Path, home: &Path) -> bool {
    use std::os::unix::fs::MetadataExt;
    match (std::fs::metadata(dir), std::fs::metadata(home)) {
        (Ok(dir), Ok(home)) => dir.uid() == home.uid(),
        _ => false,
    }
}

#[cfg(not(unix))]
fn owned_like_home(_dir: &Path, _home: &Path) -> bool {
    true
}

/// `candidate` with its longest existing prefix canonicalized and the missing
/// remainder appended. `None` when the remainder holds anything but plain
/// names (`..` below a missing directory cannot be resolved honestly) or the
/// existing prefix cannot be canonicalized.
fn resolve_candidate(candidate: &Path) -> Option<PathBuf> {
    let parts: Vec<Component<'_>> = candidate.components().collect();
    // Windows resolves `missing\..` by its spelling, so such a prefix
    // "exists" there: refuse a `..` below a missing directory before asking.
    let mut walked = PathBuf::new();
    for part in &parts {
        if matches!(part, Component::ParentDir) && !walked.is_dir() {
            return None;
        }
        walked.push(part);
    }
    for split in (1..=parts.len()).rev() {
        let prefix: PathBuf = parts[..split].iter().collect();
        if !prefix.exists() {
            continue;
        }
        let rest = &parts[split..];
        if !rest.iter().all(|part| matches!(part, Component::Normal(_))) {
            return None;
        }
        let mut real = real_dir(&prefix)?;
        real.extend(rest);
        return Some(real);
    }
    None
}

/// The project directory an unbound session may adopt when it names
/// `candidate`, canonical, or `None` (see the module docs for the refusal
/// list).
///
/// The root is the outermost directory carrying a project marker, `.git`
/// included, like `detect_project_root` (#1437: a monorepo sub-package or a
/// submodule never becomes the root). The walk stops strictly below the
/// home directory — `~` (a dotfiles repository) and anything above it are
/// neither adopted nor allowed to swallow the project — and below `/`
/// outside home.
pub fn adoptable_root(candidate: &Path) -> Option<PathBuf> {
    let home = canonical_home()?;
    let system_dirs: Vec<&Path> = SYSTEM_DIRS.iter().map(Path::new).collect();
    let shared_areas: Vec<&Path> = SHARED_AREAS.iter().map(Path::new).collect();
    adoptable_root_with(candidate, &home, &system_dirs, &shared_areas)
}

/// [`adoptable_root`] judged against the canonical `home` (from
/// [`canonical_home`]): every home-relative refusal, protected zones
/// included, uses this one home.
fn adoptable_root_with(
    candidate: &Path,
    home: &Path,
    system_dirs: &[&Path],
    shared_areas: &[&Path],
) -> Option<PathBuf> {
    if !candidate.is_absolute() {
        return None;
    }
    // Judged where the path lands: a symlink into `~/.ssh`, `dir/../..`, or a
    // case variant of a protected folder all resolve before any check.
    let real = resolve_candidate(candidate)?;
    if real == home || home.starts_with(&real) {
        return None;
    }
    if refused_location(&real, home, system_dirs) || names_vcs_or_hook(&real) {
        return None;
    }
    let below_home = real.starts_with(home);
    // Another account's home (a sibling of ours) is never adopted. A home
    // directly below `/` (`/root`) has no sibling directory of homes, and
    // macOS `/Users/Shared` is nobody's home.
    let homes = home.parent().filter(|homes| homes.parent().is_some());
    let shared = shared_areas.iter().any(|area| real.starts_with(area));
    if !below_home && !shared && homes.is_some_and(|homes| real.starts_with(homes)) {
        return None;
    }
    let start = if real.is_dir() {
        real.clone()
    } else {
        real.parent()?.to_path_buf()
    };
    let found = project_root_below(&start, below_home.then_some(home))?;
    // The walk ran on canonical components, but the root is resolved again so
    // every acceptance check — and the session — sees the real directory.
    let root = real_dir(&found)?;
    let acceptable = root != home
        && !home.starts_with(&root)
        && root.parent().is_some()
        && (!below_home || root.starts_with(home))
        && !crate::core::pathutil::is_suspicious_root(&root)
        && !refused_location(&root, home, system_dirs)
        && owned_like_home(&root, home);
    acceptable.then_some(root)
}

/// A protected zone of `home`, a secret-like path or a system directory.
fn refused_location(path: &Path, home: &Path, system_dirs: &[&Path]) -> bool {
    super::pathjail_scope::protected_zone_in(path, home).is_some()
        || crate::core::io_boundary::is_secret_like(path).is_some()
        || under_system_dir(path, system_dirs)
}

/// The outermost marker directory at or above `start`, never at or above
/// `stop` (exclusive) nor the filesystem root. A directory directly below
/// `stop` (home) counts only as a git repository: `~/Documents` with a stray
/// `package.json` is no project, a repository inside it still is. Marker
/// probes keep the TCC guard of `has_project_marker` (fail closed).
fn project_root_below(start: &Path, stop: Option<&Path>) -> Option<PathBuf> {
    let mut outermost: Option<PathBuf> = None;
    let mut cur = start;
    loop {
        if stop.is_some_and(|stop| cur == stop || !cur.starts_with(stop)) {
            break;
        }
        let Some(parent) = cur.parent() else {
            break;
        };
        let top_level = stop.is_some_and(|stop| git_only_below(parent, stop));
        let marked = if top_level {
            crate::core::pathutil::may_probe_path(cur) && cur.join(".git").exists()
        } else {
            crate::core::pathutil::has_project_marker(cur)
        };
        if marked {
            outermost = Some(cur.to_path_buf());
        }
        cur = parent;
    }
    outermost
}

/// `true` when a directory whose parent is `parent` is a root only as a git
/// repository: a folder directly below home, or a personal container's
/// direct child (`~/Documents/Work` with a stray `package.json`).
fn git_only_below(parent: &Path, home: &Path) -> bool {
    if parent == home {
        return true;
    }
    parent.parent() == Some(home)
        && parent.file_name().is_some_and(|name| {
            PERSONAL_CONTAINERS
                .iter()
                .any(|container| same_name(name, container))
        })
}

/// `root` for the `|`-separated audit action: `%` and `|` percent-encoded,
/// so a directory name never forges another field.
fn audit_escaped(root: &Path) -> String {
    root.to_string_lossy()
        .replace('%', "%25")
        .replace('|', "%7C")
}

/// Records the adoption in the local audit trail: the dispatching tool, the
/// session and the adopted root (escaped, last). The trail is the user's own
/// record; no tool output and no log line carries the root. It is written as
/// [`crate::core::audit_trail::AuditEventType::ProjectAdopted`]: no refusal,
/// so no block, cross-project or security tally counts it.
pub fn record_autobind(tool: &str, agent_id: &str, root: &Path) {
    let session = crate::core::value::current_session()
        .map(|id| format!("|session={id}"))
        .unwrap_or_default();
    crate::core::audit_trail::record(crate::core::audit_trail::AuditEntryData {
        agent_id: agent_id.to_string(),
        tool: tool.to_string(),
        action: Some(format!(
            "jail_autobind:adopted{session}|root={}",
            audit_escaped(root)
        )),
        input_hash: String::new(),
        output_tokens: 0,
        role: crate::core::roles::active_role_name(),
        event_type: crate::core::audit_trail::AuditEventType::ProjectAdopted,
    });
}

/// A temporary directory for adoption test fixtures, outside every system
/// directory adoption refuses. A `TMPDIR` below `/run/user/<uid>` (a tmpfs
/// runtime directory) is such a system directory, so the fixture then falls
/// back to `/tmp` or `/var/tmp`. Never below a source checkout: its `.git`
/// would become the outermost project marker of every fixture.
#[cfg(any(test, feature = "test-support"))]
#[must_use]
pub fn adoption_fixture_tempdir() -> tempfile::TempDir {
    let usable = |dir: &Path| {
        real_dir(dir).is_some_and(|real| {
            real.is_dir() && !SYSTEM_DIRS.iter().any(|sys| real.starts_with(sys))
        })
    };
    #[cfg(unix)]
    let candidates = vec![
        std::env::temp_dir(),
        PathBuf::from("/tmp"),
        PathBuf::from("/var/tmp"),
    ];
    #[cfg(not(unix))]
    let candidates = vec![std::env::temp_dir()];
    let base = candidates
        .into_iter()
        .find(|dir| usable(dir))
        .expect("a temp directory outside the system directories");
    tempfile::tempdir_in(base).expect("adoption fixture tempdir")
}

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

    // Fields drop in order: HOME is restored while the env lock is held.
    struct Fixture {
        _home_var: crate::test_env::VarGuard,
        home: PathBuf,
        _tmp: tempfile::TempDir,
        _data: crate::core::data_dir::IsolatedDataDir,
    }

    fn fixture() -> Fixture {
        let data = crate::core::data_dir::isolated_data_dir();
        let tmp = adoption_fixture_tempdir();
        let home = tmp.path().join("home");
        std::fs::create_dir_all(&home).unwrap();
        // Spelled the way the product resolves paths (verbatim prefix stripped).
        let home = real_dir(&home).unwrap();
        let home_var = crate::test_env::VarGuard::set("HOME", &home);
        Fixture {
            _home_var: home_var,
            home,
            _tmp: tmp,
            _data: data,
        }
    }

    /// [`adoptable_root`] judged against the fixture's home. On Windows the
    /// product's home is the profile folder, which ignores `HOME`, and the
    /// temp directory lies in its protected `AppData` zone; so the fixture
    /// home is handed in as the profile home would be.
    fn adopt(f: &Fixture, candidate: &Path) -> Option<PathBuf> {
        let system_dirs: Vec<&Path> = SYSTEM_DIRS.iter().map(Path::new).collect();
        let shared_areas: Vec<&Path> = SHARED_AREAS.iter().map(Path::new).collect();
        adoptable_root_with(candidate, &f.home, &system_dirs, &shared_areas)
    }

    fn git_project(dir: &Path) -> PathBuf {
        std::fs::create_dir_all(dir.join(".git")).unwrap();
        std::fs::create_dir_all(dir.join("src")).unwrap();
        std::fs::write(dir.join("src/main.rs"), "fn main() {}\n").unwrap();
        real_dir(dir).unwrap()
    }

    #[test]
    fn adopts_the_git_project_of_a_file_below_home() {
        let f = fixture();
        let repo = git_project(&f.home.join("code/app"));
        assert_eq!(adopt(&f, &repo.join("src/main.rs")), Some(repo.clone()));
        // A file the agent is about to create adopts its project as well.
        assert_eq!(adopt(&f, &repo.join("src/new/mod.rs")), Some(repo));
    }

    /// Lead decision 1 (#1437): the outermost marker wins, like
    /// `detect_project_root`, so a submodule or a monorepo package adopts its
    /// superproject, never itself.
    #[test]
    fn a_submodule_adopts_its_superproject() {
        let f = fixture();
        let repo = git_project(&f.home.join("code/super"));
        let sub = repo.join("vendor/lib");
        std::fs::create_dir_all(sub.join("src")).unwrap();
        std::fs::write(sub.join(".git"), "gitdir: ../../.git/modules/lib\n").unwrap();
        std::fs::write(sub.join("Cargo.toml"), "[package]\n").unwrap();
        assert_eq!(adopt(&f, &sub.join("src/lib.rs")), Some(repo));
    }

    /// `HOME=/` (containers, systemd units) leaves nothing to judge against:
    /// this adoption fails closed, and the caller keeps its previous re-root.
    #[test]
    fn a_root_home_hands_adoption_back_to_the_caller() {
        let f = fixture();
        assert!(canonical_home_of(&f.home).is_some());
        // A filesystem root is no home on any platform (`/` resolves to the
        // current drive's root on Windows).
        assert!(canonical_home_of(Path::new("/")).is_none());
        // Unix reads the home from `HOME`; Windows' profile folder ignores it.
        #[cfg(unix)]
        {
            assert!(hardened_adoption_available());
            let repo = git_project(&f.home.join("code/app"));
            let _root_home = crate::test_env::VarGuard::set("HOME", "/");
            assert!(!hardened_adoption_available());
            assert_eq!(adoptable_root(&repo.join("src/main.rs")), None);
        }
    }

    #[test]
    fn a_dotfiles_repository_at_home_neither_wins_nor_blocks() {
        let f = fixture();
        std::fs::create_dir_all(f.home.join(".git")).unwrap();
        let repo = git_project(&f.home.join("code/app"));
        assert_eq!(adopt(&f, &repo.join("src/main.rs")), Some(repo));
        std::fs::create_dir_all(f.home.join("notes")).unwrap();
        std::fs::write(f.home.join("notes/a.md"), "x").unwrap();
        assert_eq!(adopt(&f, &f.home.join("notes/a.md")), None);
    }

    #[test]
    fn refuses_home_root_and_paths_outside_any_project() {
        let f = fixture();
        std::fs::create_dir_all(f.home.join(".git")).unwrap();
        assert_eq!(adopt(&f, &f.home), None);
        assert_eq!(adopt(&f, Path::new("/")), None);
        std::fs::create_dir_all(f.home.join("loose")).unwrap();
        std::fs::write(f.home.join("loose/taxes.txt"), "x").unwrap();
        assert_eq!(adopt(&f, &f.home.join("loose/taxes.txt")), None);
        assert_eq!(adopt(&f, Path::new("relative/path.rs")), None);
    }

    /// Review F1: a missing leaf behind `..` used to skip canonicalization, so
    /// `~/Documents/../nope.md` walked to `~/.git` and adopted the home
    /// directory, and `app/../../.config/x` adopted `~/.config`.
    #[test]
    fn a_missing_leaf_behind_dot_dot_never_adopts_home_or_a_zone() {
        let f = fixture();
        std::fs::create_dir_all(f.home.join(".git")).unwrap();
        std::fs::create_dir_all(f.home.join("Documents")).unwrap();
        assert_eq!(adopt(&f, &f.home.join("Documents/../nope.md")), None);

        git_project(&f.home.join(".config"));
        let repo = git_project(&f.home.join("code/app"));
        // Existing directories before the `..`: resolved into `~/.config`.
        assert_eq!(adopt(&f, &repo.join("../../.config/x")), None);
        // A missing directory before the `..`: refused unresolved.
        assert_eq!(
            adopt(&f, &f.home.join("code/missing/../../.config/x")),
            None
        );
        assert_eq!(adopt(&f, &repo.join("missing/../src/main.rs")), None);
    }

    #[test]
    fn refuses_projects_in_protected_zones() {
        let f = fixture();
        for zone in [".ssh", ".aws", ".gnupg", "Library/Keychains", ".config"] {
            let repo = git_project(&f.home.join(zone).join("repo"));
            assert_eq!(
                adopt(&f, &repo.join("src/main.rs")),
                None,
                "{zone} must never be adopted"
            );
        }
    }

    #[cfg(unix)]
    #[test]
    fn refuses_a_symlink_that_escapes_into_a_protected_zone() {
        let f = fixture();
        let secret = git_project(&f.home.join(".ssh/keys"));
        let repo = git_project(&f.home.join("code/app"));
        std::os::unix::fs::symlink(&secret, repo.join("link")).unwrap();
        assert_eq!(adopt(&f, &repo.join("link/src/main.rs")), None);
    }

    /// Review F1: a symlink followed by a missing leaf, absolute or relative.
    #[cfg(unix)]
    #[test]
    fn a_symlink_with_a_missing_leaf_is_judged_where_it_lands() {
        let f = fixture();
        let secret = git_project(&f.home.join(".ssh/keys"));
        let repo = git_project(&f.home.join("code/app"));
        std::os::unix::fs::symlink(&secret, repo.join("abs")).unwrap();
        std::os::unix::fs::symlink("../../.ssh/keys", repo.join("rel")).unwrap();
        assert_eq!(adopt(&f, &repo.join("abs/missing.rs")), None);
        assert_eq!(adopt(&f, &repo.join("rel/missing.rs")), None);
        assert_eq!(adopt(&f, &repo.join("rel/new/dir/missing.rs")), None);
    }

    /// Review F1: on a case-insensitive macOS volume a case variant names the
    /// same directory; it is judged — and stored — by its real spelling.
    #[cfg(target_os = "macos")]
    #[test]
    fn a_case_variant_is_judged_and_stored_by_its_real_spelling() {
        let f = fixture();
        let repo = git_project(&f.home.join("code/app"));
        git_project(&f.home.join(".ssh/keys"));
        let variant = f.home.join("CODE/App/src/main.rs");
        if !variant.exists() {
            return; // a case-sensitive volume: no variant to resolve
        }
        assert_eq!(adopt(&f, &variant), Some(repo));
        assert_eq!(adopt(&f, &f.home.join(".SSH/keys/src/main.rs")), None);
        assert_eq!(adopt(&f, &f.home.join(".SSH/keys/src/new.rs")), None);
    }

    #[test]
    fn refuses_a_root_above_home_and_other_accounts() {
        let f = fixture();
        // A marker above home (`/Users/.git`, `/home/Makefile`) never makes
        // the directory holding every account's home a project.
        let above = f.home.parent().unwrap();
        std::fs::create_dir_all(above.join(".git")).unwrap();
        assert_eq!(adopt(&f, above), None);
        let other = git_project(&above.join("other-user/app"));
        assert_eq!(adopt(&f, &other.join("src/main.rs")), None);
    }

    /// Review 33452: a shared area beside the homes (macOS `/Users/Shared`)
    /// is nobody's home, so its projects stay adoptable; without the
    /// exception the same directory is refused as another account's home.
    #[test]
    fn a_project_in_the_shared_area_beside_the_homes_is_adopted() {
        let f = fixture();
        let shared = f.home.parent().unwrap().join("Shared");
        let repo = git_project(&shared.join("team/app"));
        let file = repo.join("src/main.rs");
        assert_eq!(adoptable_root_with(&file, &f.home, &[], &[]), None);
        assert_eq!(
            adoptable_root_with(&file, &f.home, &[], &[shared.as_path()]),
            Some(repo)
        );
    }

    /// Review 33452 (planted marker): a project not owned by the home's
    /// owner is refused. Home is set to a root-owned directory, so this
    /// user's own checkout is foreign to it.
    #[cfg(unix)]
    #[test]
    fn refuses_a_project_owned_by_another_account() {
        use std::os::unix::fs::MetadataExt;
        let _f = fixture();
        let outside = adoption_fixture_tempdir();
        let repo = git_project(&outside.path().join("app"));
        let file = repo.join("src/main.rs");
        assert_eq!(adoptable_root(&file), Some(repo.clone()));
        let root_owned = Path::new("/usr");
        if std::fs::metadata(&repo).unwrap().uid() == std::fs::metadata(root_owned).unwrap().uid() {
            eprintln!(
                "skipped: running as root, so /usr is no foreign owner to compare with \
                 (refuses_a_project_owned_by_another_account)"
            );
            return;
        }
        let _root_home = crate::test_env::VarGuard::set("HOME", root_owned);
        assert_eq!(adoptable_root(&file), None);
    }

    /// Review 33452 S2/S3: a first path inside `.git` adopts nothing, and a
    /// directory directly below home is a root only as a git repository.
    #[test]
    fn refuses_vcs_metadata_and_a_markered_top_level_folder() {
        let f = fixture();
        let repo = git_project(&f.home.join("code/app"));
        std::fs::create_dir_all(repo.join(".git/hooks")).unwrap();
        assert_eq!(adopt(&f, &repo.join(".git/hooks/pre-commit")), None);
        assert_eq!(adopt(&f, &repo.join(".git/config")), None);

        let documents = f.home.join("Documents");
        std::fs::create_dir_all(&documents).unwrap();
        std::fs::write(documents.join("package.json"), "{}").unwrap();
        std::fs::write(documents.join("notes.md"), "x").unwrap();
        assert_eq!(adopt(&f, &documents.join("notes.md")), None);
        let inner = git_project(&documents.join("site"));
        assert_eq!(adopt(&f, &inner.join("src/main.rs")), Some(inner));

        // Review r2 S3: a personal container's direct child is a root only
        // as a git repository too.
        let work = documents.join("Work");
        std::fs::create_dir_all(&work).unwrap();
        std::fs::write(work.join("package.json"), "{}").unwrap();
        assert_eq!(adopt(&f, &work.join("package.json")), None);
        let work = git_project(&work);
        assert_eq!(adopt(&f, &work.join("package.json")), Some(work));
    }

    /// Review r2 R2-2 (one step): a first path that is an execution hook
    /// names no project to adopt, so it cannot open the hook for writing.
    #[test]
    fn a_first_path_in_an_execution_hook_adopts_nothing() {
        let f = fixture();
        let repo = git_project(&f.home.join("code/app"));
        for hook in [
            ".githooks/pre-commit",
            ".husky/pre-commit",
            ".envrc",
            ".vscode/tasks.json",
            ".hg/hgrc",
            ".svn/entries",
            ".jj/repo/config.toml",
            ".GIT/hooks/pre-commit",
            ".Husky/pre-push",
        ] {
            assert_eq!(adopt(&f, &repo.join(hook)), None, "{hook}");
        }
        assert_eq!(adopt(&f, &repo.join(".vscode/settings.json")), Some(repo));
    }

    /// Review r2 R2-1/R2-5: an adoption is audited as `x-project_adopted`
    /// (readable by older readers as an additive event), never as a block,
    /// cross-project access or security finding, and a `|` in the root
    /// cannot forge another field of the action.
    #[test]
    fn an_adoption_is_audited_as_no_block_and_no_cross_project_access() {
        use crate::core::audit_trail::{AuditEventType, PROJECT_ADOPTED_EVENT};
        let _f = fixture();
        // Only written to the trail, never probed: a name Windows would refuse.
        let root = Path::new("/srv/code/a|session=forged");
        record_autobind("ctx_read", "agent", root);
        let entry = crate::core::audit_trail::load_recent(100)
            .into_iter()
            .find(|e| e.event_type == AuditEventType::ProjectAdopted)
            .expect("adoption audited");
        let action = entry.action.clone().unwrap();
        assert!(
            action.ends_with("|root=/srv/code/a%7Csession=forged"),
            "{action}"
        );
        assert!(crate::core::security_events::parse_action(&action).is_none());

        let line = serde_json::to_string(&entry).unwrap();
        assert!(line.contains(&format!("\"event_type\":\"{PROJECT_ADOPTED_EVENT}\"")));
        assert!(
            PROJECT_ADOPTED_EVENT.starts_with("x-"),
            "older readers accept x- only"
        );
        let back: crate::core::audit_trail::AuditEntry = serde_json::from_str(&line).unwrap();
        assert_eq!(back.event_type, AuditEventType::ProjectAdopted);

        let at = lean_ctx_time::OffsetDateTime::parse_from_rfc3339(&entry.timestamp).unwrap();
        let agg =
            crate::core::compliance_evidence::aggregate::aggregate_str(&line, at, at).unwrap();
        assert_eq!(agg.entries, 1);
        assert_eq!((agg.blocked, agg.other_security), (0, 0));
        assert!(
            agg.by_event
                .iter()
                .all(|(label, _)| label != "cross_project_access"),
            "{:?}",
            agg.by_event
        );
    }

    /// The system-directory check on a project that carries a marker, so the
    /// marker walk would accept it: only the system list refuses it. The
    /// system directory is spelled with native separators like
    /// [`SYSTEM_DIRS`] (`C:\Windows`), while canonical paths come back with
    /// `/` on Windows: a string-prefix comparison never matched there.
    #[test]
    fn refuses_projects_in_system_directories() {
        let f = fixture();
        let outside = adoption_fixture_tempdir();
        let system = PathBuf::from(
            real_dir(outside.path())
                .unwrap()
                .join("sysroot")
                .to_string_lossy()
                .replace('/', std::path::MAIN_SEPARATOR_STR),
        );
        let repo = git_project(&system.join("share/app"));
        let file = repo.join("src/main.rs");
        assert_eq!(
            adoptable_root_with(&file, &f.home, &[], &[]),
            Some(repo.clone())
        );
        assert_eq!(
            adoptable_root_with(&file, &f.home, &[system.as_path()], &[]),
            None
        );
        #[cfg(windows)]
        {
            // Windows paths ignore letter case: `c:\windows` is `C:\Windows`.
            let upper = PathBuf::from(system.to_string_lossy().to_uppercase());
            assert_eq!(
                adoptable_root_with(&file, &f.home, &[upper.as_path()], &[]),
                None
            );
        }
        let file_in_home = git_project(&f.home.join("code/app")).join("src/main.rs");
        assert_eq!(
            adoptable_root_with(&file_in_home, &f.home, &[f.home.as_path()], &[]),
            None
        );
        #[cfg(unix)]
        {
            assert_eq!(adoptable_root(Path::new("/etc/hosts")), None);
            assert_eq!(adoptable_root(Path::new("/usr/lib")), None);
        }
    }
}